Trazas con gdb

Todo programador ha pasado alguna vez por la experiencia de depurar un programa a golpe de printf(), con los inconvenientes que ello supone, sobre todo recompilar cada vez que se quiere cambiar la información que se imprime en la traza.

Pues resulta que es posible hacer las cosas “bien” :)

Con gdb podemos imprimir los valores de variables de nuestro programa, eso es algo que todos sabemos PERO tambien podemos darle una serie de órdenes a ejecutar al pasar por un breakpoint.

Veamos un ejemplo. Sea el siguiente programa en C:

#include <stdio.h>

int main()
{
        int i;
        double x=2.0f;
        for (i=0; i<64; i++)
                x*=2;
        return 0;
}

Si compilamos con -g (para debug) y lo lanzamos en gdb podemos visualizar los valores de x así:


(gdb) break 8
Breakpoint 1 at 0x400463: file a.c, line 8.
(gdb) commands 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>silent
>printf "x=%g\n",x
>cont
>end
(gdb) r
Starting program: /home/slack/a.out
x=2
x=4
x=8
x=16
x=32
x=64
[...]

El “silent” del principio es para que gdb no imprima la información que suele mostrar al pararse en un breakpoint (fichero, número de línea, contador de programa, etc) y la traza salga más limpia.

Por supuesto se pueden hacer mas cosas. gdb permite definir variables externas al programa y ejecución condicional, así que podemos contar las veces que pasamos por algun sitio y lanzar órdenes en momentos concretos y tonterías similares :D

Happy coding!

1 Comment »

  1. High Gloss Flower Mdf Board said,

    August 12, 2018 @ 02:46 am

    Our items are widely recognized and trusted by users and can meet continuously developing economic and social needs. We welcome new and old customers from all walks of life to contact us for future business relationships and achieving mutual success!

  2. ecco Shoes said,

    August 12, 2018 @ 03:13 am

    The noises and stressful the playoffs.”We are eager to match.”Owen said.Knight should first game will be on Thursday in east, and before them
    ecco Shoes

  3. adidas ultra boost said,

    August 12, 2018 @ 03:17 am

    The NBA playoffs now for today, have determined the warriors in the western conference and will compete for a spot in the finals quota, knight still don’t know who his opponent was.However, even in the face of the celtics or the wizards, presumably knight will be
    adidas ultra boost

  4. Adidas Casual said,

    August 12, 2018 @ 04:58 am

    NBA players in addition to playing badly, and also an art cells, for two consecutive years in the NBA finals two teams join forces with the warriors and knight, but only two dancers, two teams played neck and neck, dancing is close, take a look at
    Adidas Casual

  5. Connector Agent said,

    August 12, 2018 @ 05:18 am

    Abiding by our motto of “Hold well the quality and services, Customers Satisfaction”, So we give our clients with high quality products and solutions and excellent service. Be sure to feel free to contact us for further information.

  6. curry 3 shoes said,

    August 12, 2018 @ 05:20 am

    Because Michael Owen at the side of huang zhan, so his nickname called small report.The earliest Owen left the impression of the world, is probably the flashy passing movements and dazzling breakthroughs, remember in the rookie challenge, he drew down two slipped past knight
    curry 3 shoes

  7. kd 11 said,

    August 12, 2018 @ 06:56 am

    Draft of the same year on the bottom rookie will be what?In general the draft should be able to blow the other party, who would have thought them took Owen on the 60th pick small Thomas in the same year, he turned out to be in the position of the challenger?
    kd 11

  8. puma fenty said,

    August 12, 2018 @ 07:06 am

    Carey – Owen about injury of varese, said it happened out how knight players want to play games.”I was about to enter a 5 on 5 game, but after three seconds, varese is injured
    puma fenty

  9. kd shoes said,

    August 12, 2018 @ 07:44 am

    Obviously, the right to occupy a team-high 21.6 shots of Michael Owen, but failed to hit as James scores, this let a person feel, Owen has become a “cancer” of the knight.But many fans also feel,
    kd shoes

  10. adidas superstar Shoes said,

    August 12, 2018 @ 07:53 am

    Draft of the same year on the bottom rookie will be what?In general the draft should be able to blow the other party, who would have thought them took Owen on the 60th pick small Thomas in the same year, he turned out to be in the position of the challenger?
    adidas superstar Shoes

  11. nmd human race said,

    August 12, 2018 @ 09:40 am

    The noises and stressful the playoffs.”We are eager to match.”Owen said.Knight should first game will be on Thursday in east, and before them
    nmd human race

  12. ultra boost 3.0 said,

    August 12, 2018 @ 12:04 pm

    NBA players in addition to playing badly, and also an art cells, for two consecutive years in the NBA finals two teams join forces with the warriors and knight, but only two dancers, two teams played neck and neck, dancing is close, take a look at
    ultra boost 3.0

  13. Cheap MLB Jerseys 2018 said,

    August 15, 2018 @ 03:24 am

    , the region can be a two-letter state abbreviation or zip code range where the “*” is used as a wildcard (i.
    Cheap MLB Jerseys 2018 [url=http://www.mlbcheapjerseys.us.com/]Cheap MLB Jerseys 2018[/url]

  14. NFL Jerseys China said,

    August 15, 2018 @ 05:32 am

    We can use CSS selectors, so the same selectors we use to add style to a web page, or we can use XPath, a query language for selecting nodes in a document (selecting the HTML tags).
    NFL Jerseys China

  15. Cheap NFL Jerseys China said,

    August 15, 2018 @ 07:29 am

    How your site looks matters.
    Cheap NFL Jerseys China

RSS feed for comments on this post · TrackBack URI

Leave a Comment

This blog is protected by Dave\'s Spam Karma 2: 17770 Spams eaten and counting...