Gice

Technology and General Blog

If you have been programming for a while, you have arrive across the expression main dump.

If you appear at the core man web page, it defines as core dump as “a file that contains an graphic of the process’s memory at the time of termination. This impression can be employed in a debugger (e.g.) gdb to inspect the point out of the software at the time that it terminated”.

In very simple terms, a main dump file is a file that includes memory data about a approach when the distinct system terminates.

There are several reasons why processes may perhaps crash and produce a core dump file. This tutorial will clearly show you how to use GDB to view the core dump file and print the stack trace.

Action 1: Get a Main Dump

Just before we start off examining the core dump file, we need to crank out one particular. To do this, we can use a buggy software.

Take into consideration the code buggy.c under:

#involve

#consist of

#incorporate

int principal()
int num =
int *ptr = malloc(sizeof(int))
int *noptr = NULL

for (inti= i<10 i++)
if (i == 5)
            *noptr = i
       
else
            *ptr = i
       
printf(“i is %dn, i)
   

In the above code, we create a for loop between 0 and 10. If the value of i is 5, we reference a bad pointer, thereby causing segfault and creating a core dump.

Now run the code as:

The above code will give the output as:

i is 0

i is 1

i is 2

i is 3

i is 4

Segmentation fault (core dumped)

Step 2: Open Core Dump with GDB

To open the core dump file with GDB, use the gdb command and pass the path to the executable and core dump as parameters as:

This will launch GDB and include the core dump as shown in the output below:

To show the stack trace of the core file, use the backtrace command:

Conclusion

This tutorial showed you how to use GDB to open a core dump file and get the stack trace.

Leave a Reply

Your email address will not be published. Required fields are marked *