这是我要调试的程序:
#include <stdio.h>
int i = 5;
int main(void)
{
int x = 3;
display(x);
return 0;
}
void display(int x)
{
for ( i=0; i<x; ++i ) {
printf("i is %d.\n", i);
}
}
这段代码来自http://www.dirac.org/linux/gdb/05-Stepping_And_Resuming.php#breakpointsandwatchpoints。这是问题所在:
(gdb) break display
Breakpoint 1 at 0x40051e: file try5.c, line 15.
(gdb) run
Starting program: /home/ja/gdb/learning/try5
Breakpoint 1, display (x=3) at try5.c:15
(gdb) frame 1
#1 0x000000000040050c in main () at try5.c:8
(gdb) break
Breakpoint 2 at 0x40050c: file try5.c, line 8.
(gdb) c
Continuing.
i is 0.
i is 1.
i is 2.
Breakpoint 2, main () at try5.c:9
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040051e in display at try5.c:15
breakpoint already hit 1 time
2 breakpoint keep y 0x000000000040050c in main at try5.c:8
breakpoint already hit 1 time
(gdb) c
Continuing.
Program exited normally.
(gdb) q
Debugger finished
它本应在main()的第8行停止,但在它的main()的第9行停止。对我来说,这是一种误导。我认为它应该停在第9行,因为这是'break'命令的作用-在下一条指令处设置一个断点。但是,为什么“信息断点”将断点设置在第8行?
最佳答案
如您所见,断点放置在正确的位置,因为它在从函数返回后确实断了。如果进行反汇编,您还将看到断点位于正确的指令上(在此示例中为0x00401192):
b display
r
f 1
b
disassemble $pc
...
0x0040118d <+29>: call 0x401199 <display>
=> 0x00401192 <+34>: mov $0x0,%eax
0x00401197 <+39>: leave
i b
...
2 breakpoint keep y 0x00401192 in main at try5.c:8
但是它显示了错误的行号。首先,我认为这可能与函数返回有关,因此在显示调用之后添加了额外的指令,但是它仍然显示错误的行。
在我看来,这是一个错误。
关于c - 从函数返回时,为什么gdb停止在与 “i b”显示的行不同的行上?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11702143/