顯示具有 Linux Debug 標籤的文章。 顯示所有文章
顯示具有 Linux Debug 標籤的文章。 顯示所有文章

2009年8月26日 星期三

Linux debug by using strace

We can use strace to find which libraries are used of the specified program.
Another point is that you can find which configure file was opened by the specified program. Does the specified program open the wrong configuration files not as you expected?

# strace ./myprog -l 1234 -d 4
execve("./tsa", ["./myprog", "-l", "1234", "-d", "4"], [/* 53 vars */]) = 0
brk(0) = 0x9adb000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=108397, ...}) = 0
mmap2(NULL, 108397, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f7e000
close(3) = 0
open("/lib/libpthread.so.0", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0 \30l\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=131528, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f7d000
mmap2(0x6bd000, 98784, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x6bd000
mmap2(0x6d2000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14) = 0x6d2000
mmap2(0x6d4000, 4576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x6d4000
close(3) = 0
open("/lib/libm.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\20\344h\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=210324, ...}) = 0
mmap2(0x68b000, 163952, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x68b000
mmap2(0x6b2000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x26) = 0x6b2000
close(3) = 0
open("/lib/libc.so.6", O_RDONLY) = 3

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