Sunday, November 2, 2014


  • GDB or GNU debugger is a standard debugger for GNU based OS like Linux.

                             How to start gdb?

  •  We compile our program like this 
cc -g -o program program.c
gdb program

The g option helps us to know where the program is failing.

  • Now we enter gdb program
  • How our gdb program is running over our program named "program".
  • To run a program now with gdb debugger we enter the command run.
  • Whenever the program faults the gdb gives the location and reason for failure.

             Stacktrace and Examining Variables

  • To back trace the call flow of program to get to know from where the problem would have originated in program execution we enter backtrace command while running gdb.
  • We can also know the values of variables in current state when the program had faulted by using print command, eg. print i; this will give us the value of variable i when the program had faulted.
  • gdb keeps these results in a pseudo variable starting with $1, $2 and so.
  • To print a number of consecutive items we use print array[0]@n  , where n is the number of items that we want to print and array is the array name.

    Listing the program and Setting Breakpoints

  • When we are within gdb and want to view the source code of the program then we use list command, this will print the few lines before and few lines after the program faulted while running.
  • To display more lines we use list command again.
  • To set a breakpoint at a particular line we enter these set of commands
gdb program
break lineno

  • The program stops where we have set the breakpoint and prints the value of variable at that point belonging to that line.
  • To continue the program we use cont command.
  • We can use display command to set gdb to display the values of variables whenever the program stops at a breakpoint eg. display k; display array[0]@4.
  • To come out of gdb anytime we use quit command.
  • We can see the breakpoints and displays we have enabled using these commands
info break

info display

  • To disable breakpoint and display we use
 disable break #  (# is the breakpoint number we see using info break command).

disable display # (# is the display we have set using info display command).


  1. I feel awesome about posts on linux concepts.sir could you write more info about GDB with examples and system calls associated with debugging and their usage with practical examples?

  2. Please explain in the case of multi threaded programming.

  3. how we can debug running multi threads using gdb.