When C++/Python/ROS Programs Encounter Segfaults Without Showing the Error Location: How to Quickly Locate the Bug?
Python Programs
When a Python program encounters a direct segfault, it’s likely due to an error in a C-written API. You can use Python’s built-in faulthandler module to display the exact location of the error.
Documentation: https://docs.python.org/3/library/faulthandler.html
C/C++ Programs (Including ROS)
- Check if Core Dumps Are Allowed:
- Run
ulimit -c. - If it returns
0, it means core dumps are not allowed. Useulimit -c unlimitedto allow unlimited core dumps, then rerun the program.
- Run
- Locate the Core Dump File:
- On Ubuntu, core dump files are usually managed by the
apportsystem. - The
apportscript (a Python 3 program) is located at/usr/share/apport/apport. - The
apportlog, which records the activities related to crash handling and report generation, can be found at/var/log/apport.log. - By checking the
apportlog, you can determine the location of the core dump file, typically found in/var/crash/with names like_executable_path.crash.
- On Ubuntu, core dump files are usually managed by the
- Launch GDB:
- Use the command
gdb /path/to/executable /path/to/coredumpto load the executable and core dump files. - For ROS1, the executable can be found in the workspace under
devel/lib/<node_name>. - For ROS2, the executable will be in
install/<node_name>.
- Use the command
- Get the Stack Trace at the Time of the Crash:
- Inside GDB, use
btorbacktraceto get the function call stack. Usually, locating the part of the call stack that with your code will reveal the bug.
- Inside GDB, use
- Select and Inspect Specific Frames:
- Use
frame <frame_number>to select an interesting stack frame. - Use
info localsto inspect local variables within that frame. - Use
listto view the surrounding source code.
- Use
- Inspect and Print Variables:
- Use
print <variable_name>to check the value of specific variables.
- Use