Basic features of a debugger
When you execute a program that does not behave as you like, you need some way to step through your logic other than just looking at your code. Some things you want to know are:
need program args.c
You need to tell the gcc compiler that you plan to debug your program. You use the -g flag to do this. The command will now look something like:
gcc -g -Wall -o args args.cwhich would create the example executable. To run this under the control of gdb, you type
gdb argsThis starts up the text interface to the debugger.
Starts your program as if you had typed
trees command-line-arguments
Creates a breakpoint; the program will halt when it gets there. The most common breakpoints are at a specific line in a source file:
(gdb) break 20You can also set breakpoints at the beginnings of functions, as in:
Breakpoint 2 at 0x2290: file main.c, line 20
(gdb) break TraverseThe command break main stops at the beginning of execution.
Breakpoint 2 at 0x2290: file main.c, line 20
If you desire for a breakpoint to be in a different file from the one containing
main()then you must state the name of the file with a colon, then the line number as such:
break somefile.c:20When you run your program and it hits a breakpoint, you'll get a message and prompt like this.
Breakpoint 1, Traverse(head=0x6110, NumNodes=4)
at main.c:16
(gdb)
Removes breakpoint number N. Leave off N to remove all breakpoints. info break gives info about each breakpoint
Clear breakpoint at specified line or function. Argument may be line number or function name. No argument to clear all breakpoints. info break gives info about each breakpoint
Lists information about every breakpoint that is currently set.
Provides a brief description of a GDB command or topic. Plain help lists the possible topics. help topic displays help for the specific topic.
Gives the line and file you are currently located at.
Adds more directories to the search path for gdb. Allows the debugging of source files located in multiple directories.
Executes the current line of the program and stops on the next statement to be executed
Like step, however, if the current line of the program contains a function call, it executes the function and stops at the next line. step however would put you at the first line at the start of the called function
Continues regular execution of the program until a breakpoint is hit or the program stops
Produces a backtrace - the chain of function calls that brought the program to its current place. The command backtrace is equivalent. This command is especially useful for when your program crashes. You may see the method and line number of the crash and may examine the values of data at that time.
prints the value of E in the specified format, variables accessible are those of the lexical environment of the selected stack frame, plus all those whose scope is global or an entire file. E can be a C expression (usually just a variable though). /F is a format type, which can be:
Leave GDB. Answer yes to exit even though your program is still running.