- Debug C++ program in Linux
- 5 Answers 5
- Debugging
- Check availability of a core dump
- Segmentation faults
- Gdb
- Valgrind
- Missing files or libraries
- Strace
- LD_DEBUG
- Readelf
- If it is not written in C or C++, but perhaps in Python
- Report the bug
- See also
- How to Use GDB to Debug Programs in Ubuntu 20.04
- Prerequisites
- Installing Packages
- C-Program example for debugging
- Code
- Compile C Program using GCC
- Execute the test program
- Debugging an Application on Linux
- Initiate Debugger
- Debug Breakpoints
- Add Breakpoints
- Step through Breakpoints
- Information About Breakpoints
- Delete Breakpoints
- Watch Variables
- Quit Debugger
- Conclusion
- Search
- About This Site
- Latest Tutorials
Debug C++ program in Linux
I know it’s a joke, but I dislike how the focus of debuggers is on removing bugs. Debuggers allow us to understand programs in ways that would be impossible by just inspecting code.
5 Answers 5
This will start gdb and prompt you for what to do next. The next command executes one line of source and stops at the next line.
I found a basic GDB tutorial that may be helpful.
Don’t forget to compile your source code using -g option. Like this: g++ -g helloWorld.cc This is going to create an a.out executable file. You’ll be able to debug your a.out exe using gdb ./a.out command. Another tool you may use it’s ddd basically a GUI for gdb.
I always thought emacs provided a pretty user-friendly front-end to gdb.
- % g++ hello.cc -g -o hello
- emacs hello.cc
- [ In Emacs ] Escape-x gdb
- Emacs will say «Run gdb (like this): gdb «.
- Add your binary name («hello»). (E.g. «Run gdb (like this): gdb hello».)
- Go to your hello.cc buffer.
- Use the emacs command ‘gud-break’ to set a breakpoint in gdb from your hello.cc buffer. (Normally bound to «C-x space».)
- Go to your *gud-hello* buffer.
- Type «run» at the (gdb) prompt.
- Use [ N ] Next or [ S ] Step. Or [ C ] Continue. [ BT ] Backtrace is also useful.
- Note what happens to the little triangle in the leftmost column of your hello.cc buffer.
(That should suffice to get you started. Emacs being emacs, there are always more features. )
Debugging
This page is mainly about how to gather more information in connection with bug reports. Even though the word «debug» is used, it is not intended as a guide for how to debug programs while developing.
Check availability of a core dump
A core dump is a file containing a process’s address space (memory) when the process terminates unexpectedly. If the application is compiled in a debug-friendly way, the «core» file can be used to find out where things went wrong.
The location of core dumps may vary depending on the operating system configuration. See core dump to find whether generation of core dump files is enabled on your system and where do they go.
Segmentation faults
There are several techniques that can be used to figure out what went wrong. Put your detective hat on.
Gdb
gdb is an ancient and well tested application for debugging applications. See Debugging/Getting traces#Getting the trace for more instructions how to use it to obtain a trace. While running from gdb , you might have to wait for the segfault. Afterwards, post the trace to a pastebin service and include the URL in your bug report.
If you have a «core» file, it can be used together with gdb to get a backtrace:
$ gdb appname core bt full
Valgrind
Assuming you have an unstripped binary without inlined functions, it is usually a good idea to also run that program through valgrind . valgrind is a tool that emulates a CPU and usually shows where things go wrong or provide info in addition to gdb.
it will provide a lot of helpful debug output if there is a crash. Consider -v and —leak-check=full to get even more info.
$ valgrind --tool=callgrind appname
and run the output through kcachegrind to graphically explore the functions the program uses. If a program hangs, this makes it easier to pinpoint the location of the error.
Missing files or libraries
Strace
strace finds out in detail what an application is actually doing. If an application tries to open a file that is not there, it can be discovered by strace.
For finding which files a program named appname tries to open:
$ strace -eopen appname
Tip: If you wish to grep the output from strace, you can try: strace -o /dev/stdout appname | grep string .
LD_DEBUG
Setting LD_DEBUG=files gives another overview of what files an application is looking for. For an application named appname:
$ LD_DEBUG=files appname > appname.log 2>&1
The output will end up in appname.log .
Readelf
If you get no such file or directory when running an application, try the following command:
$ readelf -a /usr/bin/appname | grep interp
(replace /usr/bin/appname with the location of your executable)
Make sure the interpreter in question (like /lib/ld-linux-x86-64.so.2 ) actually exists. Install ld-lsb if need be.
If it is not written in C or C++, but perhaps in Python
Use file on the executable to get more information:
If it says ELF , it is a binary executable and is usually written in C or C++. If it says Python script , you know you are dealing with an application written in Python.
If it is a shell script, open up the shell script in a text editor and see (usually at the bottom of the file) if you can find the name of the real application (ELF file). You can then temporarily put «gdb» right in the shellscript, before the name of the executable, for debugging purposes. See the sections about gdb further up. Prefix the command with gdb —args if the executable in question needs arguments as well.
For pure shell scripts, you can also use bash -x script_name or bash -xv script_name .
For Python applications, the output will often say which file and line number the crash occurred at. If you are proficient with Python, you can try to fix this and include the fix in the bug report.
Report the bug
First check if the bug in question is a packaging bug. If the bug is introduced due to how Arch Linux packages this application, report it to https://bugs.archlinux.org. This also includes issues with libraries or dependencies (e.g if one of them is not built with a specific feature that is needed). Inspect the PKGBUILD of the package, which is possible with the Arch Build System, to see how it gets packaged. See Bug reporting guidelines#Upstream or Arch? for more information.
If the bug is not related to Arch Linux and is reproducible anywhere else, only report it to upstream. Arch Linux can not magically fix upstream bugs. Reporting it to the Arch bugtracker would not help and might even be counterproductive because it tends to waste time of the bug wranglers.
See also
How to Use GDB to Debug Programs in Ubuntu 20.04
GNU Debugger (GDB) is an open-source debugger for GNU Systems. The debugger is portable and can be used for multiple languages as C/C++ and Fortran. It can be used for debugging programs by observing their states on specific breakpoints and even altering the flow or values for further execution. Usually, the GDB runs in command-line but several GUI has been developed for it as well.
In this article, we explore how to debug C programs using GDB in Ubuntu 20.04 LTS (Focal Fossa).
Prerequisites
Note: The commands discussed in this article have been tested on Ubuntu 20.04 LTS (Focal Fossa).
Installing Packages
Install prerequisite GN packages for compiling and debugging. Run the following command in the terminal:
$ sudo apt install gcc gdb -y
C-Program example for debugging
Code
Before running, a program needs to be compiled. We are going to compile the following C code in the file main.c.
#include int main() < for (int i=0; ireturn 0; >
Compile C Program using GCC
Usually, a C code is compiled in GCC using the following command:
Another argument needs to be provided to include symbols in the binary. These symbols are used by GDB to track and debug the program. Run the following command in terminal to compile the C code:
An executable file named bin will appear.
Execute the test program
The binary file named bin can be executed like any other executable file on a command-line interface. Use the following command to run it in terminal:
The output of the code will appear.
Debugging an Application on Linux
Initiate Debugger
Run the GDB utility using following command in the terminal:
Press enter. The console for GDB terminal will appear. Enter the run command in this console to run the executable provided to the utility as an argument.
Debug Breakpoints
Add Breakpoints
Breakpoints can be added in several ways. We will be adding a breakpoint on the printf function in our code. Run the following command in terminal to add a breakpoint:
Alternatively, a line number can be used to add a breakpoint as well.
Enter the run command and the program will stop at the breakpoint.
Step through Breakpoints
Use the command continue to continue the execution of the program.
There are two other commands for different purposes of continuing the execution of the program:
- Step: steps through the next machine instruction.
- Next: steps to through the next line of code.
Abbreviations of commands can also be used. Like abbreviation of continue command is c.
Information About Breakpoints
Information about breakpoints can be observed using info command of gdb. Run the following command the terminal:
The information about breakpoints will appear.
Note: The number on the left of the breakpoint is used to refer to it by other commands.
Delete Breakpoints
A breakpoint can be deleted using the delete command and by referring to the breakpoint number observed in the output of the info utility.
Now the breakpoint has been deleted and if run, the program will execute straight to the end.
Watch Variables
Variables can be watched using the watch utility. First, we need to enter the scope in which the variable exists. For this purpose, add a breakpoint first using the following command:
Then run the code that hits this breakpoint.
Now we are in the loop where the variable i exists.
The watch command will be used to observe the previous and new value of the variable i in the loop.
Now the breakpoint generated by watch command will appear in the list of breakpoints as well. The list of breakpoints can be shown using the following command:
Moreover, we do not need the breakpoint inserted earlier. It can be easily removed using the following command:
Now if continued, the code will view values whenever the variable has changed the value and show both old and new values.
Further iterations of the program can be observed as well, using the same command.
Quit Debugger
Run the following command in the terminal to exit the debugger.
This close gdb utility and the default command-line prompt will appear.
Conclusion
In this article, we explored how to run and break a program in GDB. Moreover, it was also configured to break itself when the value of a variable has changed. We hope you can easily debug your programs in GDB after following this article.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.