2016-11-09 217 views
1

無論何時爲斷點定義命令,都無法執行例如:步驟,否則以下命令不會執行。如何在gdb斷點的命令中執行和執行更多命令

代碼例如:

[/tmp]$ cat a.c 
void increment(int* x) { 
    *x = (*x) + 1; 
} 

int main() { 
    int a = 1; 
    for (int i = 0; i < 10; i++) 
    increment(&a); 
    return 0; 
} 

[/tmp]$ gcc --std=c99 a.c -O0 -g 
[/tmp]$ gdb a.out 

GDB:

(gdb) b increment 
Breakpoint 1 at 0x10000600: file a.c, line 2. 
(gdb) command 1 
Type commands for breakpoint(s) 1, one per line. 
End with a line saying just "end". 
>p *x 
>n 
>p *x 
>end 
(gdb) r 
Starting program: /tmp/a.out 

Breakpoint 1, increment (x=0x3ffffffff670) at a.c:2 
2   *x = (*x) + 1; 
$1 = 1 
3  } 
(gdb) p *x 
$2 = 2 

n那是p *x之後執行的p *xn,但不是該命令。

這也恰好與cfins ...

+0

從[用戶手冊](https://sourceware.org/gdb/current/onlinedocs/gdb/Break-Commands.html#Break-Commands): '命令列表中的任何其他命令,在命令恢復執行,將被忽略。這是因爲任何時候你恢復執行(即使是簡單的下一步或步驟),你都可能遇到另一個斷點 - 它可能有自己的命令列表,導致關於執行哪個列表的含糊不清。你不能! – gut

回答

0

我發現了一個辦法,但它是一個解決辦法....

讓我們反思的是,gdb的劇本我寫:

(gdb) b increment 
Breakpoint 1 at 0x10000600: file a.c, line 2. 
(gdb) command 1 
Type commands for breakpoint(s) 1, one per line. 
End with a line saying just "end". 
>p *x 
>n 
>end 
(gdb) r 
Starting program: /tmp/a.out 

Breakpoint 1, increment (x=0x3ffffffff670) at a.c:2 
2   *x = (*x) + 1; 
$1 = 1 
3  } 
(gdb) b 
Breakpoint 2 at 0x1000061c: file a.c, line 3. 
(gdb) command 2 
Type commands for breakpoint(s) 2, one per line. 
End with a line saying just "end". 
>p *x 
>end 
(gdb) p *x 
$2 = 2 
(gdb) c 
Continuing. 

Breakpoint 1, increment (x=0x3ffffffff670) at a.c:2 
2   *x = (*x) + 1; 
$3 = 2 

Breakpoint 2, increment (x=0x3ffffffff670) at a.c:3 
3  } 
$4 = 3 
(gdb) 

因此,基本上如果我需要做任何事情之後ns,fin ...我定義了一個休息之後,併爲這個新的斷點一個新的命令將d任何我想要的東西。