The document discusses the GNU Debugger (GDB) and how to use it. GDB allows debugging programs by setting breakpoints, examining variables, and stepping through code. It supports remote debugging of embedded systems. Key GDB commands include break, run, print, step, and next. Compiling with -g enables debugging. GDB can examine local variables, arguments, registers, and struct pointer fields.
2. Presentation on ARM Basics
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
Presentation on
GDB
3. Presentation on ARM Basics
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
The GNU Debugger, usually called just GDB it is a portable debugger that runs
on many Unix-like systems and works for many programming
languages, including Ada, C, C++, Objective-C, Free Pascal, Fortran, Java and
partially others.
GDB offers extensive facilities for tracing and altering the execution of
computer programs. The user can monitor and modify the values of programs'
internal variables, and even call functions independently of the program's
normal behavior.
Remote debugging
GDB offers a 'remote' mode often used when debugging embedded
systems. Remote operation is when GDB runs on one machine and the
program being debugged runs on another.
The GNU Debugger
4. Compiling for debugging
• We usually compile a program as
gcc [flags] <source files> -o <output file>
• A -g option is added to enable the built-in debugging support
(which gdb needs):
gcc [other flags] -g <source files> -o <output file>
Ex: gcc -Wall -Werror -ansi -pedantic-errors -g prog1.c -o prog1.x
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
5. Starting up gdb
• To get the gdb prompt, type “gdb”
• The program to be debugged now is loaded using
(gdb) file prog1.x
• Here, prog1.x is the program you want to load, and “file” is
the command to load it.
• To run the program, we use
(gdb) run
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
6. Setting breakpoints
• Breakpoints can be used to stop the program run in the middle,
at a designated point. The simplest way is the command
“break.” This sets a breakpoint at a specified file-line pair
(gdb) break file1.c:6
• This sets a breakpoint at line 6, of file1.c. Now, if the program
ever reaches that location when running, the program will
pause and prompt you for another command.
• To break at a particular function, we use
(gdb) break function
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
7. • We can proceed onto the next breakpoint by typing “continue”
(gdb) continue
• step Executes the current line of the program and stops on the next
statement to be executed
(gdb) step
• Like step, however, if the current line of the program contains a
function call, it executes the function and stops at the next line.
(gdb) next
• Until is like next, except that if you are at the end of a loop, until will
continue execution until the loop is exited, whereas next will just take
you back up to the beginning of the loop. This is convenient if you
want to see what happens after the loop, but don't want to step through
every iteration.
(gdb) until
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
8. Print and Watchpoints
• The print command prints the value of the variable specified, and
print/x prints the value in hexadecimal:
(gdb) print variable
(gdb) print/x variable
• We can also modify variables' values by
(gdb) set < variable > = <value>
• Whereas breakpoints interrupt the program at a particular line or
function, watchpoints act on variables. They pause the program
whenever a watched variable’s value is modified.
(gdb) watch variable
• Now, whenever my var’s value is modified, the program will
interrupt and print out the old and new values.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
9. • backtrace - produces a stack trace of the function calls that
lead to a segmentation fault
• where - same as backtrace; you can think of this version as
working even when you’re still in the middle of the program
• finish - runs until the current function is finished
• delete N - deletes a specified N breakpoint
• info breakpoints - shows information about all declared
breakpoints
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
10. info commands for examining
runtime debugger state:
• gdb has a large set of info X commands for displaying information about
different types of runtime state and about debugger state. Here is how to list
all the info commands in help, and a description of what a few of the info
commands do:
• (gdb) help status lists a bunch of info X commands
• (gdb) info frame list information about the current stack
frame
• (gdb) info locals list local variable values of current stack
frame
• (gdb) info args list argument values of current stack frame
• (gdb) info registers list register values
• (gdb) info breakpoints list status of all breakpoints
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
11. .
• (gdb) up – Move to the function that called the present function. Useful if
your program crashes in a library function; use up to get to the last function
call in your program
• (gdb) down – Reverses the action of up
• (gdb) delete – Removes breakpoint by number (see example following). If
no number, all deleted.
• (gdb) kill – Terminates the program.
• (gdb) list – List 10 lines of the program being debugged. The sixth line is
the preset statement. Subsequent, consecutive entry of list will list the next
10 lines.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
12. Breakpoints
• In this, we get to specify some criterion that must be met for
the breakpoint to trigger. We use the same break command as
before:
(gdb) break file1.c:6 if i >= ARRAYSIZE
• This command sets a breakpoint at line 6 of file file1.c, which
triggers only if the variable i is greater than or equal to the size
of the array
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
13. Using pointers with gdb
• Suppose we’re in gdb, and are at some point in the execution
after a line that looks like:
struct entry * e1 = <something>;
• To see the value (memory address) of the pointer:
(gdb) print e1
• To see a particular field of the struct the pointer is referencing:
(gdb) print e1->key
(gdb) print e1->name
(gdb) print e1->price
(gdb) print e1->serial number
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
14. • We can also use the dereference (*) and dot (.) operators in
place of the arrow operator (->):
(gdb) print (*e1).key
(gdb) print (*e1).name
(gdb) print (*e1).price
(gdb) print (*e1).serial number
• To see the entire contents of the struct the pointer references
(gdb) print *e1
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
Using pointers with gdb