2008年5月22日 星期四

Linux Debug - gdb

set argument parameter
(gdb) set args -f config -c callee

set breaking point
(gdb) break a.c:100

print variable hex values
(gdb) p/x abc

show the memory of variable "buf" 5 string lines
(gdb) x/5s buf

Show the current information
(gdb) info program

Show the breakpoints info
(gdb) info br

(gdb) info br
Num Type Disp Enb Address What
1 breakpoint keep y 0x0805244f in fun1 at file.c:1419
breakpoint already hit 1 time
2 breakpoint keep y 0x001370ff in fun2 at file.c:297


Disable breakpoint
(gdb) disable 1
Num Type Disp Enb Address What
1 breakpoint keep n 0x0805244f in fun1 at file.c:1419
breakpoint already hit 1 time
2 breakpoint keep y 0x001370ff in fun2 at file.c:297

disable breakpoint num 4
(gdb) disable br 4

We can use "bt" (backtrace) command to find out How does the program reach current function stack. Like following example. The function is following the function stack sequence as main->dfun->cfun->bfun->afun and the current program counter is paused in the aaaa.c:590 position
(gdb) bt
#0 afun (sr=0x8df9008, je=0xb7d02808) at aaaa.c:590
#1 0x0013e748 in bfun (pVoid=0xbf97ce94) at bbbb.c:323
#2 0x0013d4b3 in cfun (pVoid=0xbf97ce94) at cccc.c:53
#3 0x0804b3cc in dfun (pVoid=0xbf97ce94) at dddd.c:471
#4 0x0804ad42 in main (argc=5, argv=0xbf97e0b4) at sip8d.c:396



How do I examine memory?
Use the x/FMT ADDRESS format (see the detailed description after the section)
Ex. see the 100 bytes after address 0xbff6eec8
x/100x 0xbff6eec8

Use the x command to examine memory. The syntax for the x command is x/FMT ADDRESS. The FMT field is a count followed by a format letter and a size letter. There are many options here, use the help command 'help x' to see them all. The ADDRESS argument can either be a symbol name, such as a variable, or a memory address.

If we have char *s = "Hello World\n", some uses of the x command could be:

Examine the variable as a string:

(gdb) x/s s
0x8048434 <_io_stdin_used+4>: "Hello World\n"

Examine the variable as a character:

(gdb) x/c s
0x8048434 <_io_stdin_used+4>: 72 'H'

Examine the variable as 4 characters:

(gdb) x/4c s
0x8048434 <_io_stdin_used+4>: 72 'H' 101 'e' 108 'l' 108 'l'

Examine the first 32 bits of the variable:

(gdb) x/t s
0x8048434 <_io_stdin_used+4>: 01101100011011000110010101001000

Examine the first 24 bytes of the variable in hex:

(gdb) x/3x s
0x8048434 <_io_stdin_used+4>: 0x6c6c6548 0x6f57206f 0x0a646c72


# Automatic print specified variables every step (e.g. set the tmp, string variables as automatic variables)
(gdb) display string
(gdb) display tmp
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
2: y tmp
1: y string

沒有留言: