gdb调试tips
small tips:
set print element 0 ?# 打印所有字符串,默認(rèn)長(zhǎng)度200
?
1、內(nèi)存查看命令x.
x/x 以十六進(jìn)制輸出
x/d 以十進(jìn)制輸出
x/c 以單字符輸出
x/i ?反匯編 – 通常,我們會(huì)使用?x/10i $ip-20 來(lái)查看當(dāng)前的匯編($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、內(nèi)存斷點(diǎn)break
break *addr 表示在地址addr處打斷點(diǎn).
(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、設(shè)置觀察點(diǎn)
?當(dāng)我們需要關(guān)注每次執(zhí)行后ebp的值,可以用display設(shè)置觀察點(diǎn)
(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為每個(gè)線程分配的ID,前面有*的是當(dāng)前調(diào)試的線程。thread <ID> 切換到指定IDset scheduler-locking off|on|step off 不鎖定任何線程,也就是所有線程都執(zhí)行,這是默認(rèn)值 on 只有當(dāng)前被調(diào)試程序會(huì)執(zhí)行 step 在單步的時(shí)候,除了next過(guò)一個(gè)函數(shù)的情況以外,只有當(dāng)前線程會(huì)執(zhí)行?
6、條件斷點(diǎn)
break [where] if [condition]?
7、fork調(diào)試
follow-fork-mode
在2.5.60版Linux內(nèi)核及以后,GDB對(duì)使用fork/vfork創(chuàng)建子進(jìn)程的程序提供了follow-fork-mode選項(xiàng)來(lái)支持多進(jìn)程調(diào)試。
follow-fork-mode的用法為:
set follow-fork-mode [parent|child]
- parent: fork之后繼續(xù)調(diào)試父進(jìn)程,子進(jìn)程不受影響。
- child: fork之后調(diào)試子進(jìn)程,父進(jìn)程不受影響。
因此如果需要調(diào)試子進(jìn)程,在啟動(dòng)gdb后:
(gdb) set follow-fork-mode child并在子進(jìn)程代碼設(shè)置斷點(diǎn)。
此外還有detach-on-fork參數(shù),指示GDB在fork之后是否斷開(kāi)(detach)某個(gè)進(jìn)程的調(diào)試,或者都交由GDB控制:
set detach-on-fork [on|off]
- on: 斷開(kāi)調(diào)試follow-fork-mode指定的進(jìn)程。
- off: gdb將控制父進(jìn)程和子進(jìn)程。follow-fork-mode指定的進(jìn)程將被調(diào)試,另一個(gè)進(jìn)程置于暫停(suspended)狀態(tài)。
轉(zhuǎn)載于:https://www.cnblogs.com/ym65536/p/4264731.html
總結(jié)
- 上一篇: 内盘和外盘是什么意思
- 下一篇: 初中生可以办银行卡吗