When I run my C program on Ubuntu 20.04, I get this run-time error:
Segmentation fault (core dumped)
I really need to find and view the core
file, but I can't find it anywhere. Where is it, and how do I view the backtrace in it?
When I run my C program on Ubuntu 20.04, I get this run-time error:
Segmentation fault (core dumped)
I really need to find and view the core
file, but I can't find it anywhere. Where is it, and how do I view the backtrace in it?
Tested in Ubuntu 20.04.
1. Enable core files
First off, run
ulimit -c
to see what the max allowed size is for core files on your system. On Ubuntu 20.04 for me, mine returns0
, which means no core file can be created.ulimit --help
shows the meaning of-c
:So, set the allowed core file size to
unlimited
, as shown below. Note that I think this only applies to the one terminal you run this in, and I do not think it's persistent across reboots, so you have to run this each time you want core files to be created, and in each terminal you want it to work in:That's it! Now, run your program and if it crashes and does a "core dump" it will dump the core as a
core
file into the same directory you were in when you called the executable. The name of the file is simply "core".2. View the backtrace in gdb
You should have built your C or C++ program with debug symbols on, in order to see useful information in your core file. Without debug symbols, you can only see the addresses of the functions called, not the actual names or line numbers.
In gcc, use
-ggdb -O0
to turn on debug
symbols optimized for thegdb
G
NUd
eb
ugger. You can also use-g -O0
,-g3 -O0
, etc, but-ggdb -O0
is best. Do we really need optimization level 0 (-O0
) for this? Yes, yes we do. See my answer here: Stack Overflow: What's the difference between a compiler's-O0
option and-Og
option?Example build and run commands in C and C++: so, your full build and run commands in C or C++ might look like this:
Open the core file in
gdb
like this:Assuming you just ran
path/to/my/executable
, then thecore
file will be in the same directory you were just in when the core was dumped, so you can just run this:In
gdb
, view the backtrace (function call stack at the time of the crash) with:IMPORTANT: when a core dump occurs, it does NOT automatically overwrite any pre-existing
core
file in your current directory with a new one, so you must manually remove the oldcore
file withrm core
PRIOR TO generating the new core file when your program crashes, in order to always have the latest core file to analyze.3. Try it out
sleep 30
to start a process sleeping for 30 seconds.core
file in the directory you are in.gdb -c core
to open the core file just created by the forced crash.sleep 30
) when the core dump occurred:bt
orwhere
to see the backtrace. You'll see this:4. Forget about core files and just run the program to the crash point in gdb directly!
As @Peter Cordes states in the comments below, you can also just run the program inside gdb directly, letting it crash there, so you have no need to open up a core file after-the-fact! He stated:
As stated above, you should have compiled your program with debugging symbols on and with Optimization Level 0, using
-ggdb -O0
. See the full example build and run commands in C and C++ above.Now run the program in gdb:
And if you ever need to manually log the backtrace to a log file to analyze later, you can do so like this (adapted from notes in my eRCaGuy_dotfiles repo here):
Done! You've now saved the gdb backtrace in file "gdb_log.txt".
References:
-O0
option and-Og
option?Additional reading to do
LD_PRELOAD
withgdb
: https://stackoverflow.com/questions/10448254/how-to-use-gdb-with-ld-preloadFound via search. I'm running Ubuntu Mate 21.10. For those running late model Ubuntu,
apport
will generate dumps in/var/lib/apport/coredump
.If you can't find your core dump file,
cat /var/log/apport.log
. When I did that, I saw:Notice the core limit 0, that means no core dump file will be generated. So, I ran the command shown in this post (
ulimit -c unlimited
), and this timeapport.log
showed this:I couldn't find this in the current directory or the directory containing the executable, so I did a find on the entire system and found it in
/var/lib/apport/coredump
.