GDB - how to get into the stepping from the very beginning

Typically, the break main command is used to enter step mode from the very beginning of a C ++ program in GDB. But this interrupts the program only when entering main() .

The question is how to break the program into the very first operation written by the user (say, on the constructor of a statically defined instance of a class)?

For example, if I had the following code, how could I break into A() without using the break 5 command?

 #include <iostream> struct A { A() { std::cout << "A()" << std::endl; } }; static A a; int main() { return 0; } 

Update: I am actually debugging very large code written by someone else. The code contains many instances of a static class scattered across different source files. It is not possible to manually set breakpoints for each of the constructors.

+5
source share
1 answer

You can define a breakpoint in the constructor.

 (gdb) break 'A::A()' Breakpoint 1 at 0x8048724: file x.cc, line 4. (gdb) run Starting program: /.../a.out Breakpoint 1, A::A (this=0x804a0ce <a>) at x.cc:4 4 std::cout << __func__ << std::endl; (gdb) bt #0 A::A (this=0x804a0ce <a>) at x.cc:4 #1 0x08048700 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at x.cc:8 #2 0x0804871c in _GLOBAL__sub_I_main () at x.cc:10 #3 0x080487a2 in __libc_csu_init () #4 0xb7d44a1a in __libc_start_main (main=0x80486ad <main()>, argc=1, argv=0xbffff184, init=0x8048750 <__libc_csu_init>, fini=0x80487c0 <__libc_csu_fini>, rtld_fini=0xb7fed180 <_dl_fini>, stack_end=0xbffff17c) at libc-start.c:246 #5 0x080485d1 in _start () (gdb) 

Note the use of single quotes to indicate that the C ++ identifier is malformed. Also note that the stack trace shows that main() has not yet been called.

From the stack trace, there are many options for setting a breakpoint that will exist before the global constructor is called. One such breakpoint is on _start .

 (gdb) break _start Breakpoint 1 at 0x80485b0 (gdb) run Starting program: /.../a.out Breakpoint 1, 0x080485b0 in _start () (gdb) bt #0 0x080485b0 in _start () (gdb) 
+2
source

Source: https://habr.com/ru/post/1201041/


All Articles