[contents] [usage] [execution] [stack] [breakpoints] [watchpoints] [advanced]

1. How do I use gdb?

When you compile your program, you must tell the compiler to produce a program that is compatible with the debugger. The debugger needs special information to run properly. To do this, you must compile your program with the debugger flag, -g. This step is critical. Without it, the debugger won't have the program symbol information. That means it won't know what your functions and variables are called, and it won't understand when you ask it about them.

How do I...?

  1. compile with debugging symbols?
  2. run programs with the debugger?
  3. restart a program running in the debugger?
  4. exit the debugger?
  5. get help on debugger commands?


1.1 How do I compile with debugging symbols? [top]   [toc]

Pass the -g flag to your compiler:

prompt > gcc -g program.c -o programname

NOTE: If you have a larger program with several files, each must be compiled with the -g flag, and it must also be set when you link.


1.2 How do I run programs with the debugger? [top]   [toc]

First start the debugger with your program name as the first argument.

prompt> gdb programname

Next use the run command in gdb to start execution. Pass your arguments to this command.

(gdb) run arg1 "arg2" ...


1.3 How do I restart a program running in the debugger? [top]   [toc]

Use the kill command in gdb to stop execution. The you can use the run command as shown above to start it again.

(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) run ...


1.4 How do I exit the debugger? [top]   [toc]

Use the quit command.

(gdb) quit

NOTE: You may be asked if you want to kill the program. Answer yes.

(gdb) quit
The program is running. Exit anyway? (y or n) y
prompt >


1.5 How do I get help on debugger commands? [top]   [toc]

Use the help command. Gdb has a description for every command it understand, and there are many, many more then this tutorial covers. The argument to help is the command you want information about. If you just type "help" with no arguments, you will get a list of help topics similar to the following:

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.





[contents] [usage] [execution] [stack] [breakpoints] [watchpoints] [advanced]

Questions? Comments? Flames? email rms@unknownroad.com