如何在gdb断点的命令

时间:2016-11-09 11:56:09

标签: 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

它执行了p *xn,但没有执行n p *x之后的命令。

cfins ...

也会发生这种情况

1 个答案:

答案 0 :(得分: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)

所以基本上如果我需要在nsfin之后做任何其他事情......我在此之后定义了一个中断,并为此新断点创建了一个新命令做任何我想做的事。

相关问题