GDB: advanced usages

Sometimes running program in Unix will fail without any debugging info or warnings because of the laziness of programmer.. So if need to find the right place where the program stops and start debugging right from there, set the coredump file size to some desired size by first checking ..:

udump -a | grep core

[root@os-t1 ~]# ulimit -a | grep core

core file size          (blocks, -c) 0

[root@os-t1 ~]#

 

Use the following command to set the limit of coredump file size:

ulimit -c unlimited

[root@os-t1 ~]# ulimit -c unlimited

[root@os-t1 ~]# ulimit -a | grep core

core file size          (blocks, -c) unlimited

[root@os-t1 ~]#

 

       Make sure the program is generated by gcc/g++ using flags of -O0 and -g. After running the program that will fail will generate the Core.[0-9]+ file after exit, this file contains the stack/mem/register infos.

       use gdb to restore the environment when the bug appears and program exits:

gdb program core.[0-9]+

gdb will point to the line will the program exits, use bt to view the stack infos, use up to get to the upper entry of the stack. Easy to find the problem in the program we write.

normal gdb debugging:

gdb set watch points, uses watch, rwatch, awatch, see this passage. note that if rerun the program by r, all the set watchpoints are outdated, unlike the normal breakpoints. use inf b to see all the watchpoints/breakpoints.

rwatch/awatch uses hardware detection, so only powerpc/x86 based machine are able to set these points, and in a limited number. use watch expr mask 0xff00 to watch some bits.

gdb expressions:

print construct gdb arrays at given len: p *lpstr@len

print certain var in function foo scope: p foo::var

catch points: catch signal ~

gdb handles exceptions: handle ~ keywords

condition stop: if expr true then stop, condition breakpointNB expr, the expr is evaluated when gdb running to the script, rather than right at the command is given. if condition breakpointNB is typed again, the condition is removed

another condition is to ignore the breakpoint for a given times, using ignore breakpointNB count to do it. only apply to bp, wp, cp.. [not tracepoints.. then .. what is tp?..

breakpoint & commands to execute:

.. if some breakpoints are activated.. some commands are able to be defined to run .. e.g. print values, enable other breakpoints..

format is:

commands [bp list]

command lists..

..

end

the last end ends the command list. if need to delete all defined commands for bps, using command \nend

the command list after resuming the program will be ignored.

     break foo if x>0
     commands
     silent
     printf "x is %d\n",x
     cont
     end

silent is to remove the message which says the bp is reached.. and can only be put at the first line of the command snippet.

this technique can also be used to pass the vars some desired value when debugging, and resume debugging for the rest codes.. like ..

     break 403
     commands
     silent
     set x = y + 4
     cont
     end

Dynamic print:

adding printf expression while debugging (other than a breakpoint) can use:

dprintf location,template,expression[,expression...]

where location is line number, template is "%d" like, expr is vars. before using it, some configurations should be set:

set dprintf-style gdb/call/agent
set dprintf-function printf/fprintf/printk..
set dprintf-channel channel/stdout/stderr/pfile..

the 2nd command can be used only when using style-call. the 3rd command can be used in fpf as the first argument as the dest channel, e.g.

          (gdb) set dprintf-style call
          (gdb) set dprintf-function fprintf
          (gdb) set dprintf-channel mylog
          (gdb) dprintf 25,"at line 25, glob=%d\n",glob
          Dprintf 1 at 0x123456: file main.c, line 25.
          (gdb) info break
          1       dprintf        keep y   0x00123456 in main at main.c:25
                  call (void) fprintf (mylog,"at line 25, glob=%d\n",glob)
                  continue
          (gdb)

using save breakpoints file to save all the bps, for later loading

loading settings in gdb, always use source file.

adding breakpoints in a ambiguous way..

     (gdb) b String::after
     [0] cancel
     [1] all
     [2] file:String.cc; line number:867
     [3] file:String.cc; line number:860
     [4] file:String.cc; line number:875
     [5] file:String.cc; line number:853
     [6] file:String.cc; line number:846
     [7] file:String.cc; line number:735
     > 2 4 6
     Breakpoint 1 at 0xb26c: file String.cc, line 867.
     Breakpoint 2 at 0xb344: file String.cc, line 875.
     Breakpoint 3 at 0xafcc: file String.cc, line 846.
     Multiple breakpoints were set.
     Use the "delete" command to delete unwanted
      breakpoints.
     (gdb)    

set multiple-symbols ask/all, if ask, the above is done, if all, automatically choose [1]all.

 

tui usages:

enter tui: c-x c-a  || c-x s || tui enable/disable

cls: c-l

commands: 

 c d f n q r s u v w

c/f/n/q/r/s: continue/fin/nex/quit/run/step

d/u/v/w: down/up/view/where

lay src/asm/split/regs

GDB free chars:-----

etyiopaghjkl;0-9'zxbm,./?`~-=_+

 

to break at conditions:

# This breaks when char* s satisfies strcmp(s, "hello")==0 at lineNo.
break lineNo if strcmp(s, "hello")==0
# This breaks when i==3 
break lineNo if i==3
# Anything legal in the context at lineNo can be put after if..
break lineNo if selfdefinedfunction()==true

 

gdb also has a condition syntax.. to quick add conditions..

# This is the same as the above line..
break lineNo
#   # A break point numbered N has been set
condition N strcmp(s,"hello")==0

 

set breakpoints for a certain thread.. check current thread use command thr

# First checkout all existing threads
info thread
# then break
b lineNo/funcname (if ...) thread id

gdb may set convenient vars: some more gdb-defined c-vars can be viewed here. (Some not available till recent version of gdb ..)

set $foo = *object_ptr
# Using a convenience variable for the first time creates it, but its value is void until you assign a new value. You can alter the value with another assignment at any time.
show convenience
init-if-undefined $variable = expression
set $i = 0
print bar[$i++]->contents

gdb can also debug several programs/processes at a time, as inferiors..further info be viewed here.

gdb can run process in background mode, by continue& ..

debug one/several threads while others keep running, other than stop together with the ones being debugged, by: (default off)

# If using the CLI, pagination breaks non-stop.
set pagination off

# Finally, turn it on!
set non-stop on

show non-stop

# To revert this setting
set non-stop off

 

use gdbserver to enable remote debugging: (why?) sometimes need local gdbinit? or ?

# target:
gdbserver ip:port prog
# host:
gdb
> target remote ip:port
> continue

---------

Sometimes need to debug into python third-party libraries.. and without source code.. If with source code, it is easy to debug python with pdb, however if without source code, we only can debug in byte-code form with installed python-debuginfo (centos). By:

gdb python pid

 

posted on 2016-09-06 18:41  三叁  阅读(714)  评论(0编辑  收藏  举报

导航