當前位置:
首頁 >
gdb调试tips
發布時間:2024/10/12
37
豆豆
small tips:
set print element 0 ?# 打印所有字符串,默認長度200
?
1、內存查看命令x.
x/x 以十六進制輸出
x/d 以十進制輸出
x/c 以單字符輸出
x/i ?反匯編 – 通常,我們會使用?x/10i $ip-20 來查看當前的匯編($ip是指令寄存器)
x/s 以字符串輸出
(gdb) x/10i main0x80483bb <main>: push %ebp0x80483bc <main+1>: mov %esp,%ebp0x80483be <main+3>: and $0xfffffff0,%esp0x80483c1 <main+6>: sub $0x20,%esp0x80483c4 <main+9>: movl $0x0,0x1c(%esp)0x80483cc <main+17>: movl $0x5,(%esp) => 0x80483d3 <main+24>: call 0x8048394 <fact>0x80483d8 <main+29>: mov %eax,0x1c(%esp)0x80483dc <main+33>: mov 0x1c(%esp),%eax0x80483e0 <main+37>: leave?
2、內存斷點break
break *addr 表示在地址addr處打斷點.
(gdb) disassemble main Dump of assembler code for function main:0x080483bb <+0>: push %ebp0x080483bc <+1>: mov %esp,%ebp0x080483be <+3>: and $0xfffffff0,%esp0x080483c1 <+6>: sub $0x20,%esp0x080483c4 <+9>: movl $0x0,0x1c(%esp)0x080483cc <+17>: movl $0x5,(%esp)0x080483d3 <+24>: call 0x8048394 <fact>0x080483d8 <+29>: mov %eax,0x1c(%esp)0x080483dc <+33>: mov 0x1c(%esp),%eax0x080483e0 <+37>: leave 0x080483e1 <+38>: ret End of assembler dump. (gdb) b *0x080483d3 Breakpoint 1 at 0x80483d3: file test.c, line 14. (gdb) run Starting program: /mnt/hgfs/Share/csapp/a.out Breakpoint 1, 0x080483d3 in main () at test.c:14 14 c = fact(5);?
3、打印寄存器
可以使用 info registers查看所有寄存器:
(gdb) info registers eax 0xbffff6c4 -1073744188 ecx 0x6400d69f 1677776543 edx 0x1 1 ebx 0x288ff4 2658292 esp 0xbffff5f0 0xbffff5f0 ebp 0xbffff618 0xbffff618 esi 0x0 0 edi 0x0 0 eip 0x80483c4 0x80483c4 <main+9> eflags 0x286 [ PF SF IF ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51或者使用 p/x $ebp打印ebp寄存器的值
(gdb) p/x $ebp $2 = 0xbffff618?
4、設置觀察點
?當我們需要關注每次執行后ebp的值,可以用display設置觀察點
(gdb) display $ebp 1: $ebp = (void *) 0xbffff618 (gdb) si 14 c = fact(5); 1: $ebp = (void *) 0xbffff618 (gdb) si 0x080483d3 14 c = fact(5); 1: $ebp = (void *) 0xbffff618?
5、多線程
info thread 顯示gdb為每個線程分配的ID,前面有*的是當前調試的線程。thread <ID> 切換到指定IDset scheduler-locking off|on|step off 不鎖定任何線程,也就是所有線程都執行,這是默認值 on 只有當前被調試程序會執行 step 在單步的時候,除了next過一個函數的情況以外,只有當前線程會執行?
6、條件斷點
break [where] if [condition]?
7、fork調試
follow-fork-mode
在2.5.60版Linux內核及以后,GDB對使用fork/vfork創建子進程的程序提供了follow-fork-mode選項來支持多進程調試。
follow-fork-mode的用法為:
set follow-fork-mode [parent|child]
- parent: fork之后繼續調試父進程,子進程不受影響。
- child: fork之后調試子進程,父進程不受影響。
因此如果需要調試子進程,在啟動gdb后:
(gdb) set follow-fork-mode child并在子進程代碼設置斷點。
此外還有detach-on-fork參數,指示GDB在fork之后是否斷開(detach)某個進程的調試,或者都交由GDB控制:
set detach-on-fork [on|off]
- on: 斷開調試follow-fork-mode指定的進程。
- off: gdb將控制父進程和子進程。follow-fork-mode指定的進程將被調試,另一個進程置于暫停(suspended)狀態。
轉載于:https://www.cnblogs.com/ym65536/p/4264731.html
總結
- 上一篇: 内盘和外盘是什么意思
- 下一篇: BI Content、Metadata