Core Dump and GDB
Introduction
GDB can be used for debugging computer program bugs, such as segmentation faults. The key file used for debugging is the core dump file, which is generated when a program crashes. The core dump file contains a snapshot of the program’s memory at the time of the crash, allowing developers to analyze the state of the program and identify the cause of the crash.
In this blog post, I would like to discuss how to generate a core dump file from a Docker container and how to use GDB to debug quickly.
Core Dump and GDB
Core Dump File Size Limit
Make sure the core dump file size limit is set to unlimited. It can be checked using the ulimit command in the terminal.
1 | $ ulimit -c |
It can be set to unlimited, if it is not, using the ulimit command as well.
1 | $ ulimit -c unlimited |
Core Dump File Name Pattern
To save a core dump file in a Docker container, we have to set the core_pattern, i.e., the file name pattern for the core dump files. This has to be set from the host computer before Docker container is launched. We could set the core_pattern to /tmp/core.%e.%p, which will save the core dump files in the /tmp directory with the format core.<executable_name>.<pid>.
1 | # The /proc/sys/kernel/core_pattern will be reset after reboot. |
The /proc/sys/kernel/core_pattern will be reset to default after rebooting the host computer.
Launch Docker Container
We could use the Docker container prepared for debugging from the previous article “Debug C/C++ Programs In Docker Container”.
1 | $ docker run -it --rm --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v $(pwd):/mnt -w /mnt cpp-debug:0.0.1 |
Run Computer Programs
The problematic C++ program from the previous article “Illegal Memory Access and Segmentation Fault” was used to demonstrate how to generate a core dump file.
In this case, we build the C++ program with and without debugging symbols. When we run the computer programs, the program will crash with segmentation faults and generate a core dump file in the /tmp directory following the core_pattern we set earlier.
1 | $ g++ oob.cpp -o oob |
Examine Core Dump File Using GDB
GDB can be used to examine the core dump files generated by computer programs built with or without debugging symbols.
1 | $ gdb oob /tmp/core.oob.22 |
1 | $ gdb oob_debug /tmp/core.oob_debug.24 |
In both cases, GDB informs the user that the program terminated at the function call of access_out_of_bounds_element. In practice, there can be multiple calls of the function from the program, and we would like to know which exact call caused the segmentation fault. The core dump file from the program built with debugging symbols provides such additional information. Therefore, to debug more effectively, it is recommended to build the program with debugging symbols for debugging.
GDB Is Necessary
Since GDB is necessary to examine the core dump file and the executable is required for tracing, sometimes it is not quite necessary to save the core dump file. GDB can be used to run the computer program and the dump file will be recorded by GDB automatically.
Conclusions
The core dump can hardly be used alone without GDB.
References
Core Dump and GDB