break *main和break main()在本质上有什么区别?例如:
#include <iostream>
using namespace std;
int main()
{
int x=30;
int y=40;
x=y;
return 0;
}当我使用break *main和watch x时,如下所示:
(gdb) b *main
Breakpoint 1 at 0x400674: file aa.cpp, line 4.
(gdb) r
Starting program: /root/dd/aa.out
Breakpoint 1, main () at aa.cpp:4
4 {
(gdb) n
5 int x=30;
(gdb) watch x
Hardware watchpoint 2: x
(gdb) c
Continuing.
Hardware watchpoint 2: x
Old value = 0
New value = 30
main () at aa.cpp:6
6 int y=40;
(gdb) c
Continuing.
Hardware watchpoint 2: x
Old value = 30
New value = 40
main () at aa.cpp:8
8 return 0;
(gdb) 但是当我使用break main()和watch x时,它是这样的:
(gdb) b main()
Breakpoint 1 at 0x400678: file aa.cpp, line 5.
(gdb) r
Starting program: /root/dd/aa.out
Breakpoint 1, main () at aa.cpp:5
5 int x=30;
(gdb) watch x
Hardware watchpoint 2: x
(gdb) c
Continuing.
Hardware watchpoint 2: x
Old value = 0
New value = 40
main () at aa.cpp:8
8 return 0;
(gdb) 他们为什么不一样?本质上有什么区别呢?
当我观察一个数组时,如果我使用break main(),它将显示:
Watchpoint 2 deleted because the program has left the block in
which its expression is valid.但是如果我使用break *main,它就不会出现,为什么?
发布于 2016-12-04 18:57:17
在本质上有什么区别?
区别在于b *main在main的第一条指令上中断,而b main在https://en.wikipedia.org/wiki/Function_prologue之后的第一条指令上中断。
在我的构建(g++ -g t.cc,使用gcc 4.8.4-2ubuntu1~14.04.3和gdb 7.9)中,源代码的反汇编如下所示:
(gdb) disas main
Dump of assembler code for function main():
0x00000000004006cd <+0>: push %rbp
0x00000000004006ce <+1>: mov %rsp,%rbp
0x00000000004006d1 <+4>: movl $0x1e,-0x8(%rbp)
0x00000000004006d8 <+11>: movl $0x28,-0x4(%rbp)
0x00000000004006df <+18>: mov -0x4(%rbp),%eax
0x00000000004006e2 <+21>: mov %eax,-0x8(%rbp)
0x00000000004006e5 <+24>: mov $0x0,%eax
0x00000000004006ea <+29>: pop %rbp
0x00000000004006eb <+30>: retq
End of assembler dump.设置b *main和b main会产生以下结果:
(gdb) b *main
Breakpoint 1 at 0x4006cd: file t.c, line 4.
(gdb) b main
Breakpoint 2 at 0x4006d1: file t.c, line 5.我无法重现你所观察到的问题:
(gdb) r
Starting program: /tmp/a.out
Breakpoint 1, main () at t.c:4
4 {
(gdb) c
Continuing.
Breakpoint 2, main () at t.c:5
5 int x=30;
(gdb) p x
$1 = 0
(gdb) watch x
Hardware watchpoint 3: x
(gdb) c
Continuing.
Hardware watchpoint 3: x
Old value = 0
New value = 30
main () at t.c:6
6 int y=40;https://stackoverflow.com/questions/40960758
复制相似问题