Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

breakpoints - break *main VS break main() in GDB

what is the difference between break *main and break main() in essence? for example:

    #include <iostream>
    using namespace std;
    int main()
    {
        int x=30;
        int y=40;
        x=y;
        return 0;
   }

when I use break *main and watch x, it is this:

(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) 

but when I use break main() and watch x, it is this:

(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) 

why are they different? And what is the difference in essence?

And when I watch an array, if I use break main(), it will appear:

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.

but if I use break *main, it will not appear, why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

And what is the difference in essence

The difference is that b *main breaks on the first instruction of main, while b main breaks on the first instruction after the function prologue.

In my build (g++ -g t.cc, using gcc 4.8.4-2ubuntu1~14.04.3 and gdb 7.9), disassembly of your source looks like this:

(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.

And setting b *main vs. b main produces:

(gdb) b *main
Breakpoint 1 at 0x4006cd: file t.c, line 4.
(gdb) b main
Breakpoint 2 at 0x4006d1: file t.c, line 5.

I can not reproduce the problem you observed:

(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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...