Friday, January 6, 2017

How to Use Debugger

when developing code you frequently need to run debugger to debug code.  Here are some tips I found useful.


C/C++:  gdb

Attach to a live process

Use "ps" to find out the pid, then:

$ gdb [path/to/binfile] [pid]

or,

$ gdb [path/to/binfile]
(gdb)  attach [pid]


Pretty print

(gdb) set print pretty on
(gdb) print <an object>

Print array

Show array variable "arr1" first 10 elements:
(gdb) p  arr1[0]@10


Continue running to end of current method

Continue running until just after function in the selected stack frame returns.  Step out of current method:
(gdb)  fin[ish]


Run until some location

Continue running until "either the specified location is reached", or "current stack frame returns".


(gdb)  u  [location]

The "location" can be "lino no", "method",  "file:method", "file:line_no" "address".


Breakpoints

(gdb)  b  [line no]
(gdb)  b  [filename:line_no]
(gdb)  b  [method name]
(gdb)  b  *address        // set a bp at this address,  useful when no src file info available

// set a conditional bp  if "condition" is evaluated to be true.
(gdb)  b  [xxxx]  if [condition]

(gdb)  info b   // show all breakpoints

(gdb)  info b  N    // show bp N

Java

Jdb

check status of a live program

First find process id of the program:
$ jps
Then dump the entire java threads stack to a file:
$ jstack [pid] > [filename]     




No comments:

Post a Comment