Chitika

Tuesday, June 25, 2013

GDB debugging

Following are the useful commands to debug:

debug with program arguments:

    gdb --args prog arg1 arg2 arg3 

Debugging an application with complex configuration files setup in different directories:

    compile the program with -g switch, and install it
    debug the installed program as follows:
    sudo gdb --args prog arg1 arg2 arg3

add break point to function:

    b func_foo

add break point to line in file:

    b file:line_num

step into:

    s

step over:

    n

continue program execution:

    c

start/restart program:

    r

print variable:

    p var
    x/s str_var

print 10 bytes in hex

    x/10x str_var

list program statements being in debug
  
    l

get call stack: (backtrace)
 
    bt

get value of some variable in function call in back in call stack at row 2:

    f 2
    p var

get all break points (info break):

    i b

delete a break point 3:

    d 3

watch a variable for change:

    wa var

get threads list (info threads):

    i th

change to different thread context (e.g. thread 2):

    t 2

attaching to an already running process for debugging (first get process id using ps -ef):

    at process_id

detaching to debugging process:

    det

debugging save state (checkpoint, only for single threaded application):

   checkpoint

   info checkpoint

restore program state to the checkpoint (e.g. 2):

   restart 2

GDB hex dump

Create file in home folder, named '.gdbinit', and put following contents in it:

define xxd
dump binary memory dump.bin $arg0 $arg0+$arg1
shell xxd dump.bin
end


during debugging execute following command to get hex dump of any memory area, by specifying starting address (or name of variable) and length:

(gdb) xxd var 112
0000000: aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000010:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000020:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000030:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000040:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000050:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000060:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................



Ref:

http://stackoverflow.com/questions/9233095/memory-dump-formatted-like-xxd-from-gdb
http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_chapter/gdb_18.html#SEC195