Basics of the GDB Debugger

Basic Seg Falut Finding

As you surely have noticed by now when you compile and run a program and results in a segmentation falut you dont get any information just a line of text that says "Segmentation Falut". This obviously is not very helpful, thats where the GDB debugger comes in. Using this you can pinpoint the exact line number that the segfault came from and the proceeding stack trace the system had before the falut.

To find the location of an existing seg fault follow these steps:

(What you type is in

Compile the file normally except add the '-g' flag after the filename like so.

[pawprint@babbage currentDir]$
gcc fileName.c -g

To run gdb just type gdb and pass in your compiled output file (like an argument... it's all comming together)

[pawprint@babbage currentDir]$
gdb a.out

A bunch of info about the program will pop up with a new prompt.

In the prompt type run and then your aurgments if you have any (just think of run as ./a.out)

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-64.el6_5.2)



run 'optional arguments'

The program will run and if a segfault happens a buch of stuff will pop up. For example:

Program received signal SIGSEGV, Segmentation fault.

0x00000000004009f5 in functionName (myPtr=0x7fffffffe020, myInt=3, myCharPtr=0x7fffffffe4a5 "input.txt") at myLab.c:

if (myPtr->myStructVar + someThing < 0)


Note in
the line number where the segfalut occured.

For a more complete stack trace:


This will display the trace of the function calls that lead to this.

#0 0x00000000004009f5 in functionName (myPtr=0x7fffffffe020, myInt=3, myCharPtr=0x7fffffffe4a5 "input.txt") at myLab.c:

#1 0x0000000000400611 in main (argc=3, argv=0x7fffffffe1b8) at myLab.c:30


For this example a function was called in "main" and in the function "functionName" on line "112" a seg fault happened.

To quit simply type 'q', it may ask you to say 'y' to stop debugging.