简体   繁体   English

如何使用 GDB 在可执行文件的 c++ 代码的第一行停止?

[英]How to use GDB to stop at the first line of c++ code of an executable?

I have a C++ project.我有一个 C++ 项目。 After using CMake compile in debug mode, it generates many executables.使用 CMake 在调试模式下编译后,它会生成许多可执行文件。

I want to debug an executable suppose it called A.我想调试一个可执行文件,假设它名为 A。

But I don't sure where is the A's main function, so I want to use gdb to debug A and want to stop at its main function's first line.但是我不确定A的主function在哪里,所以我想用gdb调试A,想停在它的主函数的第一行。 How should I do?我应该怎么做? Thank you very much!非常感谢!

To simply run until we reach main, we simply can要简单地运行直到我们到达 main,我们可以简单地

> start

which will set a temporary breakpoint at main and starts execution.这将在 main 处设置一个临时断点并开始执行。

If you always want to break at main, even if you restart your program, you can add a persistent breakpoint and run.如果您总是想在 main 处中断,即使您重新启动程序,您也可以添加一个持久断点并运行。

>b main
>run

As we have maybe a lot of code running before main, especially if constructors of global objects are needed, it makes sense to beak already on the first instruction the program have.因为我们可能在 main 之前运行了很多代码,特别是如果需要全局对象的构造函数,那么在程序的第一条指令上就开始使用是有意义的。 In that case we can use在这种情况下,我们可以使用

> starti

which will stop on the first executed assembler instruction这将在第一个执行的汇编指令处停止

We also can go to dedicated sections of the startup procedure, if we know the symbol names here.如果我们知道这里的符号名称,我们也可以 go 到启动过程的专用部分。 One example is _init on which you also can stop if needed.一个示例是_init ,如果需要,您也可以在其上停止。 To get a more detailed list of possible breakpoints see: gcc initialization要获得更详细的可能断点列表,请参阅: gcc 初始化

If you need to stop on a known constructor, you can also set a breakpoint on all constructors of a class type if you simply use the constructors name, here given for a class A as example.如果您需要在已知的构造函数上停止,您还可以在 class 类型的所有构造函数上设置断点,前提是您只需使用构造函数名称,此处以class A为例。 This is very helpful if a global object is created before main is started.如果在main启动之前创建了全局 object,这将非常有用。 It helps a lot in the case you have dependent global objects which may fail as we all know static initialization order fiasco如果您有依赖的全局对象可能会失败,这很有帮助,因为我们都知道static 初始化顺序惨败

>b A::A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM