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. Lawrence Worthen said,

    April 24, 2017 @ 00:21 am

    Howdy! This blog post couldn�t be written much better! Going through this
    article reminds me of my previous roommate! He constantly kept preaching about this.
    I’ll forward this article to him. Pretty sure he’ll have
    a great read. Thanks for sharing!

  2. coreyjdvo666blog.shotblogs.com said,

    April 24, 2017 @ 00:22 am

    Heya i’m for the first time here. I came across this board
    and I find It truly useful & it helped me out much. I hope to give something back and aid others like you aided me.

  3. Cheap Jerseys From China said,

    April 24, 2017 @ 01:07 am

    My secretary Cindy has to delete a lot of uneventful email every single day. Wells Fargo, San Bernardino Superior Court Case No. Some are even accused of having advised homeowners to not make mortgage payments to increase their chances of qualifying for modifications, and then foreclosing anyway.”Ventura has made the phrase “one day at a time” a talking point, helping produce the White Sox first-placestanding for 106 days this season and every day since July 24. It’s going to become less difficult if you possibly could thoroughly clean these individuals effectively from home without hiring somebody.

  4. Wholesale NFL Jerseys China said,

    April 24, 2017 @ 03:17 am

    J. This can give you a clear picture of that which you have done and the work that you have remaining to do.It is sometimes complicated to aside these individuals. Having fit will make you feel good! What have you been waiting for?锘?Finding things to do with the entire family in tow is not always an easy feat. Further, they are increasingly turning to consumer abuses to collect mortgage payments, at a time when working with and helping out distressed consumers would obviously be in everyone?s best interests.
    Wholesale NFL Jerseys China [url=http://www.cheapsportsnfljerseys.us.com/]Wholesale NFL Jerseys China[/url]

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...