Here is a simple way to do post-mortem debugging for a program that crashes. First enable the saving of core files when program crashes by running the following command in shell before starting the program you want to test:
ulimit -c unlimited
When program crashes Linux creates core file named core to program running directory
You can look at the details on core file with gdb:
gdb -c core programbinary
In gdb run command:
The amount of details depends on how program is compiled (is there debug information in the program or has it been stripped off).
When you get the program running properly, the next task is to optimize it. First you need to know in which parts of the code the CPU spends most time. Profiling gives up information on this. There are Gnu profiling tools that are useful to know. GNU gprof is quite easy to use tool for profiling and worth to check out.
In this first step, we need to make sure that the profiling is enabled when the compilation of the code is done. This is made possible by adding the ‘-pg’ option in the compilation step.
gcc -Wall -pg myprogram.c -o myprogram
When the program execution ends (naturally or ended nicely) it produces gmon.out file to program directory. Now you run
The output will give you lots of details how the program has run on the system, and which functions take most of the execution time. Check Gprof documentation for information how to read the program output.