Debugging With GDB: by L.Mohan
Debugging With GDB: by L.Mohan
by
L.Mohan
lmohan@au-kbc.org
Objective
This is a very basic introduction to using the
excellent GNU Debugger, GDB.
What is Debugging?
Debugging is the process of removing bugs
from computer programs.
In other words debugging means starting at
your source code untill you see the bug.
An infinitely more effective method is to use a
special program called a ”debugger”
What is Debugger?
Debugger is nothing but a special program
used to find errors (bugs) in other programs.
What is gdb?
gdb is the GNU Debugger.
It is the standard debugger for the GNU
software system.
It is a portable debugger that runs on many
Unixlike systems and works for many
programming languages.
What is gdb?
GDB can be used to debug C, C++,
ObjectiveC, Fortran, Java ,pascal,and
Assembly programs.
History
GDB was first written by Richard Stallman in
1986 as part of his GNU system.
History
GDB is free software released under the GNU
General Public License (GPL)
History
It was modeled after the Dbx debugger, which
came with Berkeley Unix distributions.
dbx is a popular, Unixbased sourcelevel
debugger found primarily on Solaris, and BSD
Unix systems.
Features of GDB
GDB's thread support works for embedded
RTOSes, such as eCos.
For embedded programming, GDB has a
number of special features.
GDB is also distinctive as open source
debugger.
Features of GDB
KGDB is a source level debugger for linux
kernel. It is used along with gdb to debug linux
kernel.
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.
Limitations
The debugger does not contain its own
graphical user interface, and defaults to a
commandline interface.
Limitations
Several frontends have been built for it, such
as DDD, Eclipse CDT and the "GUD mode" in
GNU Emacs.
Limitations
GDB does not correctly support C++ templates.
Limitations
Some other debugging tools have been
designed to work with GDB, such as memory
leak detectors.
Invoking gdb
Type the shell command gdb at shell prompt
to start gdb.
Type q (or) quit (or) ctrld to exit
To compile a program for use with gdb, use the
‘g’ compiler switch.Ex: $gcc g p1.c
Invoking gdb
The most usual way to start gdb is with one
argument
Ex:
$gdb ./a.out
where a.out is executable
how to get help in gdb?
If you just type "help" with no arguments, you
will get a list of help topics similar to the
following:
(gdb)help
how to get help in gdb?
Type ”help” followed by a class name for a list
of command in that class.
Ex:
(gdb)help breakpoint
how to get help in gdb?
Type "help" followed by command name for full
documentation.
(gdb)help break
Type "help all" for the list of all commands.
info
Get information
‘info’ alone prints a list of info commands
‘info br’ : a table of all breakpoints and
watchpoints
‘info reg’ : the machine registers
Breakpoints
Breakpoints are a way of telling gdb that you
want to stop your program at certain lines of
code.
Breakpoint on a Line
The command to set a breakpoint is break.
If you only have one source file, you can set a
breakpoint like
(gdb)break 11
If you have more than one file, you must give
the break command a filename as well.
(gdb)break test.c:19
Breakpoint on a C Function
To set a breakpoint on a C function, pass it's name
to break command.
(gdb)break function_name.
Ex: $gdb list
(gdb)run
(gdb)break function1
(gdb)run
Temporary Breakpoint
Use the tbreak command instead of break. A
temporary breakpoint only stops the program
once, and is then removed.
Ex:$gdb list
(gdb)tbreak 11
(gdb)run
(gdb)run
View list of Breakpoints
Use the info breakpoint command.
It tells you how many breakpoints.
(gdb)info breakpoint
Disable Breakpoints
Use the disable command.
use breakpoint number as argument
(gdb)break 11,14,18
(gdb)run
(gdb)info breakpoint
(gdb)disable 2
(gdb)run
Skip Breakpoints
To skip a breakpoint a certain number of times,
we use the ignore command.
The ignore command takes two arguments
(gdb)ignore 3 5
it skip breakpoint 3 to 5 times.
Delete Breakpoints
Delete command is used to delete a breakpoint.
(gdb)info breakpoint
(gdb)delete 3
(gdb)info brakpoint
Examine Memory
Use the x command to examine memory.
The syntax for the x command is x/FMT
ADDRESS.
FMT may be format letter and a size letter.
format(s,c,o,x,a,t) and size tells how many char.
ADDRESS may be variable or memory
address.
Demo Program
Ex:
#include<stdio.h>
int main()
{
char *ptos=”Welcome to NRCFOSS”;
return(0);
}
Demo Step
$gcc g mem.c o mem
$gdb mem
(gdb)break 4
(gdb)run
(gdb)x/s ptos
(gdb)x/c ptos
Demo Step
(gdb)x/4c ptos
(gdb)x/d ptos decimal format
(gdb)x/o ptosoctal format
(gdb)x/x ptoshexa decimal
(gdb)x/a ptosaddress in hex
(gdb)x/t ptos binary
(gdb)help xprints help
Processer Register
Type info register
The output of this command depends on the
hardware architecture.
(gdb)info register
see the Assembly Code of Program
(gdb)disassemble main
Here is an example of the disassembly for the
main function of a simple program on an intel
machine:
What is Stack Frame?
When a function is called, it creates a stack
frame that tells the computer how to return
control to its caller function after it has finished
executing.
Stack frames are also where local variables and
function arguments are 'stored'.
Get a Backtrace
Finding the list of stack frames below the
current frame is called a backtrace.
Use the gdb command backtrace.
In the backtrace below, we can see that we are
currently inside func2(), which was called bu
func1(), which was called from main()
(gdb)backtrace
Change Stack Frames
Use the gdb command frame.
Notice in the backtrace above that each frame
has a number beside it.
Pass the number of the frame you want as an
argument to the command.
(gdb)frame 2
Examine Stack Frames
There are 3 useful gdb commands.
1.info frame displays information about the
current stack frame.
(gdb) info frame
2.info locals displays the list of local variables
and their values for the current stack frame.
(gdb)info local
Examine Stack Frames
3.info args displays the list of arguments.
(gdb)info args
Printing Source Lines
Display source code (useful for setting
breakpoints).
To print lines from a source file ,use the list
command.
(gdb)list or l
Printing Source Lines
By default 10 lines are printed
(gdb)show listsize
You can change this using set listsize count
(gdb)set listsize 20
(gdb)show listsize
Demo Debugging
Sample Debugging Demo
by
L . Mohan
NRCFOSS