The interested reader can refer part 1 of the current article here – <How to look at corefiles using gdb – Part 1>.
Having looked at setting the core file path for Linux in the previous article, let us run the sample program to obtain a Linux corefile.
We shall create a sample program which creates a Core-file and traverse back to find the error by passing the core-file through the gdb tool. The current sample program generates a Divide by Zero error.
/* Sample Program */
compile the above program and executing the program generates a core file. We have re-directed the core file to /tmp folder as described in the previous article.
Now to run the core-file through gdb and to debug the issue as to what caused the error.
The gdb command executed above is
gdb <sample_program.exe> <path/core_file_name>
The sample program is compiled with optimization level set to zero and containing all debug symbols. Also, gdb could find the source files for the program in the current directory.
In case the path to the source/include files need to be set – use the below command. To show the use of the command – the source file for the current Sample program has been moved to “src” directory.
other commands to set the source and include directories is the “set substitute-path” command. refer gdb help to know more of this command.
now we can run the back-trace command on gdb to show us the entire stack trace
By running the core file – we realize that the function – func_1 had a divide by zero error.
The gdb tool is a very powerful tool that can be used for the purpose of debugging. It has numerous facilities and features and the few articles that are posted is to get one started on gdb.