2016-11-09 139 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任何我想要的东西。