Using gdb to Do Simple Debugging

Sometimes we get a bus error or a segmentation fault. But we have no idea where it is happening. The debugger program called gdb can help. Here's how to see where the bug happened in your code.

  1. Compile your code with the -g option. This tells the compiler to annotate your executtable with the names of variables, locations in the code etc.

  2. Then type gdb.

  3. At the prompt type file nameOfExecutable. Then use the set command to set any parameters that you would type after the executable. For example if you would normally type fred arg1 arg2 then you can do:
    file [filename]
    set args [arg1] [arg2]
    gdb [filename]
    run arg1 arg2
    The r command will run the debugger.

  4. When it gets the bus error, seg fault or whatever, the location in your source code will be displayed. If you want to see the whole stack to help find out where the failing routine was called from etc you can use the bt command which backtraces the execution stack.

  5. quit gets you out of the debugger. Go to the gnu documention on the web for details or just type help in gdb.

The Man Page for gdb

Here is the man page for gdb. As you can see it can do a lot more than just the tiny example above.
GNU Tools						   gdb(1)

     gdb - The GNU Debugger

     gdb  [-help] [-nx]	[-q] [-batch] [-cd=dir]	[-f] [-b bps]
	  [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c core]
	  [-x cmds] [-d	dir] [prog[core|procID]]

     The purpose of a debugger such as GDB is to allow you to see
     what  is  going  on  ``inside''  another  program	while  it
     executes-or what another program was doing	at the moment  it

     GDB can do	four main kinds	of things (plus	other  things  in
     support of	these) to help you catch bugs in the act:

	o Start	your  program,	specifying  anything  that  might
	  affect its behavior.

	o Make your program stop on specified conditions.

	o Examine  what	 has  happened,	 when  your  program  has

	o Change things	in your	program, so  you  can  experiment
	  with	correcting  the	 effects  of one bug and go on to
	  learn	about another.

     You can use GDB to	debug programs written	in  C,	C++,  and
     Modula-2.	 Fortran support will be added when a GNU Fortran
     compiler is ready.

     GDB is invoked with the shell command gdb.	 Once started, it
     reads  commands  from the terminal	until you tell it to exit
     with the GDB command quit.	 You can get online help from gdb
     itself by using the command help.

     You can run gdb with no arguments or options; but	the  most
     usual way to start	GDB is with one	argument or two, specify-
     ing an executable program as the argument:

     gdb program

     You can also start	with both an  executable  program  and	a
     core file specified:

GNU Tools	      Last change: 4nov1991			1

GNU Tools						   gdb(1)

     gdb program core

     You can, instead, specify a process ID as a second	argument,
     if	you want to debug a running process:

     gdb program 1234

     would attach GDB to process 1234 (unless  you  also  have	a
     file named	`1234';	GDB does check for a core file first).

     Here are some of the most frequently needed GDB commands:

     break [file:]function
	   Set a breakpoint at function	(in file).

     run [arglist]
	  Start	your program (with arglist, if specified).

     bt	  Backtrace: display the program stack.

     print expr
	   Display the value of	an expression.

     c	  Continue running your	program	(after stopping, e.g.  at
	  a breakpoint).

     next Execute next program line (after stopping);  step  over
	  any function calls in	the line.

     step Execute next program line (after stopping);  step  into
	  any function calls in	the line.

     help [name]
	  Show information about GDB  command  name,  or  general
	  information about using GDB.

     quit Exit from GDB.

     For full details on GDB, see Using	GDB: A Guide to	 the  GNU
     Source-Level  Debugger, by	Richard	M. Stallman and	Roland H.
     Pesch.  The same text is available	online as the  gdb  entry
     in	the info program.

     Any arguments other than options specify an executable  file
     and  core	file (or process ID); that is, the first argument
     encountered with no associated option flag	is equivalent  to
     a	`-se'  option, and the second, if any, is equivalent to	a
     `-c' option if it's the name of a file.  Many  options  have
     both  long	 and  short forms; both	are shown here.	 The long

GNU Tools	      Last change: 4nov1991			2

GNU Tools						   gdb(1)

     forms are also recognized if you truncate them, so	 long  as
     enough  of	the option is present to be unambiguous.  (If you
     prefer, you can flag option arguments with	`+'  rather  than
     `-', though we illustrate the more	usual convention.)

     All the options and command line arguments	you give are pro-
     cessed  in	 sequential  order.  The order makes a difference
     when the `-x' option is used.


     -h	  List all options, with brief explanations.


     -s	file
	   Read	symbol table from file file.

	  Enable writing into executable and core files.


     -e	file
	   Use file file as the	executable file	to  execute  when
	  appropriate, and for examining pure data in conjunction
	  with a core dump.

	   Read	symbol table from file file and	 use  it  as  the
	  executable file.


     -c	file
	   Use file file as a core dump	to examine.


     -x	file
	   Execute GDB commands	from file file.


GNU Tools	      Last change: 4nov1991			3

GNU Tools						   gdb(1)

     -d	directory
	   Add directory to the	path to	search for source files.


     -n	  Do not execute commands from any `.gdbinit' initializa-
	  tion	files.	Normally, the commands in these	files are
	  executed after all the command  options  and	arguments
	  have been processed.


     -q	  ``Quiet''.  Do not print the introductory and	copyright
	  messages.   These messages are also suppressed in batch

	  Run in batch mode.  Exit with	status 0 after processing
	  all	the   command  files  specified	 with  `-x'  (and
	  `.gdbinit',  if  not	inhibited).   Exit  with  nonzero
	  status if an error occurs in executing the GDB commands
	  in the command files.

	  Batch	mode may be useful for running GDB as  a  filter,
	  for  example	to  download and run a program on another
	  computer; in order to	make this more useful,	the  mes-

	  Program exited normally.

	  (which is ordinarily issued whenever a program  running
	  under	 GDB  control terminates) is not issued	when run-
	  ning in batch	mode.

	   Run GDB using  directory  as	 its  working  directory,
	  instead of the current directory.


     -f	  Emacs	sets this option when it runs GDB  as  a  subpro-
	  cess.	  It  tells  GDB to output the full file name and
	  line number in a standard,  recognizable  fashion  each
	  time	a  stack  frame	is displayed (which includes each
	  time the  program  stops).   This  recognizable  format
	  looks	 like  two ` 32' characters, followed by the file

GNU Tools	      Last change: 4nov1991			4

GNU Tools						   gdb(1)

	  name,	line number and	character position  separated  by
	  colons, and a	newline.  The Emacs-to-GDB interface pro-
	  gram uses the	two  ` 32'  characters	as  a  signal  to
	  display the source code for the frame.

     -b	bps
	   Set the line	speed (baud rate or bits per  second)  of
	  any serial interface used by GDB for remote debugging.

	   Run using device for	your program's standard	input and

     `gdb' entry in info; Using	GDB: A Guide to	the  GNU  Source-
     Level  Debugger,  Richard	M.  Stallman and Roland	H. Pesch,
     July 1991.

     Copyright (c) 1991	Free Software Foundation, Inc.

     Permission	is granted to make and distribute verbatim copies
     of	 this  manual provided the copyright notice and	this per-
     mission notice are	preserved on all copies.

     Permission	is granted to copy and distribute  modified  ver-
     sions of this manual under	the conditions for verbatim copy-
     ing, provided that	the entire resulting derived work is dis-
     tributed under the	terms of a permission notice identical to
     this one.

     Permission	is granted to copy and distribute translations of
     this  manual  into	 another language, under the above condi-
     tions for modified	versions,  except  that	 this  permission
     notice  may be included in	translations approved by the Free
     Software Foundation instead of in the original English.

GNU Tools	      Last change: 4nov1991			5