Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 289
李鹏程 Avatar asked Jan 05 '23 23:01

李鹏程


1 Answers

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;
like image 147
Employed Russian Avatar answered Jan 22 '23 11:01

Employed Russian