简体   繁体   English

如何在gdb断点的命令中执行和执行更多命令

[英]How to step and execute more commands inside a gdb breakpoint's command

Whenever a command is defined for a breakpoint, it can't perform eg: steps otherwise the following commands don't execute. 每当为断点定义命令时,它就不能执行例如:否则以下命令不会执行。

code example: 代码示例:

[/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:

(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

It executed the p *x and the n , but not the command after n that was the p *x . 它执行了p *xn ,但不是n后面的命令,即p *x

It also happens with c , fin , s ... 它也发生在cfins ......

I found a way out, but it's a workaround.... 我找到了出路,但这是一个解决方法......

Let's rethink that gdb script I was writing: 让我们重新考虑一下我写的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)

So basically if I need to do anything else after a n , s , fin ... I define a break after that and a new command for this new breakpoint that will do whatever I wanted to after. 所以基本上如果我需要在nsfin之后做任何其他事情......我在那之后定义一个中断,并为这个新断点创建一个新命令,它将执行我想做的任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM