7

这是我要调试的程序:

#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 行呢?

4

1 回答 1

4

如您所见,断点放置在正确的位置,因为它在从函数返回后确实中断了。如果您进行反汇编,您还会看到断点位于正确的指令处(在此示例中为 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

但它显示错误的行号。一开始我以为可能和函数返回有关,所以我在显示调用后添加了额外的指令,但它仍然显示错误的行。

这在我看来是一个错误。

于 2012-07-28T15:38:43.393 回答