简体   繁体   English

如何在 C++ 程序中设置自定义断点?

[英]How to setup custom breakpoints in the C++ program?

I'm working on a project, where I cannot disclose the details of the code.我正在做一个项目,我不能透露代码的细节。 So, the application is all written in C and C++.因此,该应用程序都是用 C 和 C++ 编写的。 Since, a particular file which wanted to debug has a lot of dependencies and exports, I need to debug the whole project.由于要调试的特定文件有很多依赖项和导出,因此我需要调试整个项目。 How do I set breakpoints in the code itself so that the debugging would stop at that particular point?如何在代码本身中设置断点,以便调试在该特定点停止? I'm using Ubuntu 14.04 (since the project is compatible with this environment) and gdb debugger.我正在使用 Ubuntu 14.04(因为该项目与此环境兼容)和 gdb 调试器。

I've tried using我试过使用

#include <csignal>

// Generate an interrupt
std::raise(SIGINT);

But I keep getting error但我不断收到错误

error: ‘raise’ is not a member of ‘std’

Even this also didn't work即使这也不起作用

#include <signal.h>
raise(SIGINT);

Plus the debugging wont stop at that point, so that I could foresee the function at that point.加上调试不会在那个点停止,所以我可以预见到那个点的功能。 I only want to debug it from console, rather using any IDE.我只想从控制台调试它,而不是使用任何 IDE。

Since the programfile I want to debug has lot many header files which it imports, I'm unable to make a executable to use gdb.由于我要调试的程序文件有很多导入的头文件,因此我无法制作可执行文件以使用 gdb。 So, while make clean build of my MakeFile I want to debug the particular program file at a particular function.因此,在对我的 MakeFile make clean build ,我想在特定函数中调试特定程序文件。 So, for that I want to add breakpoints in the program.所以,为此我想在程序中添加断点。 I cannot use any GUI for debugging since I should not use.我不能使用任何 GUI 进行调试,因为我不应该使用。

您是否尝试过使用 GDB 命令?
b lineno - set a break point at line 'lineno' b srcfile:lineno - set a break point in source file 'srcfile' at line 'lineno'

Read more about debugging with gdb .阅读有关使用gdb调试的更多信息。 Be sure to compile all your code with DWARF debug information (so use g++ -Wall -Wextra -g to compile it with GCC ).确保使用DWARF调试信息编译所有代码(因此使用g++ -Wall -Wextra -g使用GCC编译它)。

GDB is extensible and you can define your own gdb commands at startup in your init file, probably .gdbinit and put some initial commands there. GDB 是可扩展的,您可以在 启动时在 init 文件中 定义自己的gdb命令, 可能是.gdbinit并在那里放置一些初始命令。

BTW, on Linux, debugging (so the gdb debugger) is using ptrace(2) facilities.顺便说一句,在 Linux 上,调试(所以gdb调试器)使用ptrace(2)工具。 And you can use gdb non-interactively on the command line, using scripts.您可以使用脚本在命令行上以非交互方式使用gdb

How do I set breakpoints in the code itself如何在代码本身中设置断点

I don't recommend adding specific C code for breakpoints.我不建议为断点添加特定的 C 代码。 So don't do that in your C code.所以不要在你的 C 代码中这样 But see also this .但也看到这个

Perhaps you want some backtrace library, like Ian Taylor's libbacktrace ?也许您想要一些回溯库,例如 Ian Taylor 的libbacktrace

I cannot use any GUI for debugging我无法使用任何 GUI 进行调试

You don't need to.你不需要。 You'll use gdb on the command line.您将在命令行上使用gdb With an appropriate gdb script, you can even use it non-interactively (eg in a Makefile )使用适当的gdb脚本,您甚至可以以非交互方式使用它(例如在Makefile

I only want to debug it from console, rather using any IDE.我只想从控制台调试它,而不是使用任何 IDE。

Please realize that IDE s are only glorified source code editor s capable of running other external tools (including the GCC compiler and the gdb debugger).请意识到IDE只是能够运行其他外部工具(包括 GCC 编译器和gdb调试器)的美化源代码编辑器。 You certainly don't need -on Linux- any IDE to run a compiler or a debugger (but IDEs could be convenient , but not necessary , for that), because you can (and should) run your compiler, your debugger, your build automation tool, on the command line.你当然不需要 - 在 Linux 上 - 任何 IDE 来运行编译器或调试器(但 IDE 可能很方便,但不是必需的),因为你可以(并且应该)运行你的编译器、调试器、构建自动化工具,在命令行上。

Since the program file I want to debug has lot many header files which it imports, I'm unable to make a executable由于我要调试的程序文件有很多导入的头文件,因此我无法制作可执行文件

You should fix that first.你应该先解决这个问题。 You need to make an executable .您需要制作一个可执行文件 BTW, there is no "import" involved at run time, since header files are relevant only at compile time.顺便说一句,运行时不涉及“导入”,因为头文件仅在编译时相关。 Read more about the cpp preprocessor .阅读有关cpp预处理器的更多信息。 You probably should invoke GCC (eg the g++ compiler, since you have C++ code) with appropriate preprocessor options (sometimes, tools like pkg-config are useful for that).您可能应该使用适当的预处理器选项调用 GCC (例如g++编译器,因为您有 C++ 代码)(有时,像pkg-config这样的工具对此很有用)。 You probably should use some build automation tool such as GNU make ( with your Makefile ; see this for inspiration) or ninja .您可能应该使用一些构建自动化工具,例如GNU make您的Makefile ;请参阅获取灵感)或ninja You could add ad hoc gdb commands to your build procedure (eg with some additional and specific rules and/or recipes in your Makefile ).您可以将临时gdb命令添加到您的构建过程中(例如,在您的Makefile添加一些额外的和特定的规则和/或配方)。

First, make sure you have compiled with -g.首先,确保您已使用 -g 进行编译。 There are other gdb specific flags in gcc. gcc 中还有其他特定于 gdb 的标志。 You could add them in too.你也可以添加它们。

Try using ddd, the graphical version of gdb.尝试使用 ddd,gdb 的图形版本。 Great tool if you don't know the gdb command line.如果您不知道 gdb 命令行,这是一个很好的工具。 Just open up the relevant source file, select the line then click on breakpoint on the toolbar.只需打开相关的源文件,选择该行,然后单击工具栏上的断点。 It will tell you on the console section, what command was actually executed (good way to learn).它会在控制台部分告诉您实际执行了什么命令(学习的好方法)。 There is a floating button list with run, next etc. for stepping through your code.有一个带有运行、下一步等的浮动按钮列表,用于单步执行您的代码。

ddd will work on most of the gcc toolchain. ddd 将适用于大多数 gcc 工具链。

EDIT: Say your code is made up of 2 files main.cpp and child.cpp.编辑:假设您的代码由 2 个文件 main.cpp 和 child.cpp 组成。 main.cpp contains main(). main.cpp 包含 main()。 The executable is called a.out.可执行文件称为 a.out。

To start开始

ddd a.out &

It will open in main.cpp.它将在 main.cpp 中打开。 To put a breakpoint in child.cpp, click on File/Open Source... and select child.cpp.要在 child.cpp 中放置断点,请单击 File/Open Source... 并选择 child.cpp。 Then scroll to where you want a breakpoint.然后滚动到您想要断点的位置。 Put your cursor on the line, then click on break in the toolbar.将光标放在该行上,然后单击工具栏中的中断。

To run, either type run in the gdb window below or click on Run in the floating button dialog.要运行,请在下面的 gdb 窗口中键入 run 或单击浮动按钮对话框中的 Run。

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

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