1. GNU DEBUGGER / GDB
OPERATING SYSTEMS
AGH UNIVERSITY SCIENCE AND TECHNOLOGY
2. What is Debugger?
• A debugger is a program that is used to test and debug
other programs.
There are many ways to do debugging such as
• printing out messages to the screen using a debugger
• just thinking about what the program is doing and
making an educated guess as to what the problem is.
3. WHAT IS DEBUGGER?
• Step by step running of program
• Stopping debugging when
program encounter a breakpoint
• Tracking values of variables.
• Debugging while program is
running.
Debuggers
have
functions
like
4. BASIC FUNCTIONS OF DEBUGGER
• To learn which statement or expression causes a error
• To specify which line/lines contains error
• To learn the instant values of variables at particular point during
execution of program.
• To learn result of program at particular point
• To learn execution sequence of statements in a program.
5. What is GDB?
GNU Debugger which is also known as GDB is debugger for GNU operating system
that provides to see and distunguish the errors inside of computer program while
the program executes.
6. History of GDB
• Written by Richard Stallman
in 1986.
• Developed by John Gilmore
between 1990 and 1993.
• Now maintained by GDB
Steering Committee which is
part of Free Software
Foundation.
Mascot of GDB
7. FEATURES OF GDB
GDB has mainly 4 functionality to find bugs in program
• GDB can specify any thread in program which might affect program’s
behavior
• User can monitor and modify values of programs’ variables
• GDB can be stop your program in specific conditions.
• GDB can check and analyze all program when program stops.
• GDB can correct bugs in program after detecting it.
8. USAGE AREAS OF GDB
• GDB is not only used in GNU operating system but also use like
portable debugger which runs Unix-like systems. GDB can run on
UNIX and Microsoft Windows systems
GDB Works for
• Ada
• C++
• C
• Objective C
• Pascal languages
9. TECHNICAL DETAILS
Main Target Processors of GDB
• Alpha
• ARM
• AVR
• H8/300
• System/370
• System/390
• x86 / x86-64
• Motorola 68000
• PowerPC
• GDB is actively developing and
New versions are releasing
periodically. With version 7.0
It supports ‘reverse
debugging’.
• Reverse debugging in gdb
means gdb can allow you to
"step" or "continue" your
program backward in time,
reverting it to an earlier
execution state.
10. REMOTE DEBUGGING
Remote operation is when GDB runs on one machine and the program being
debugged runs on another.
• For example, you might use remote debugging on an operating system
kernel or on a another system which does not have a general purpose
operating system powerful enough to run a full featured debugger.
If you want to remote debugging there are 5 stages that you can do
1. Connecting: Connecting to a remote target
2. File Transfer: Sending files to a remote system
3. Server: Using the gdbserver program
4. Remote Configuration: Remote configuration
5. Remote Stub: Implementing a remote stub
11. REMOTE DEBUGGING
1. Connecting
GDB can communicate with the target over a serial line, or over an IP network
using TCP or UDP. In each case, gdb uses the same protocol for debugging your
program.The target remote command establishes a connection to the target.
For example, to connect to port 8080 on a terminal server named webpages:
target remote webpages:8080
Disconnecting:
The disconnect command disconnects the connection with remote target. After
this command gdb is free to connect another target.
12. REMOTE DEBUGGING
2. File Transfer
Commands for sending to remote system:
remote put hostfile targetfile:
Copy file hostfile from the host system to targetfile on the target system.
remote get targetfile hostfile:
Copy file targetfile from the target system to hostfile on the host system.
remote delete targetfile:
Delete targetfile from the target system.
13. REMOTE DEBUGGING
3. Server
• gdbserver is a control program for Unix-like systems, which allows you to
connect your program with a remote gdb via target remote but without
linking in the usual debugging stub.
• To use the server, you must tell it how to communicate with gdb; the
name of your program; and the arguments for your program. The usual
syntax is:
target> gdbserver comm program [ args ... ]
14. REMOTE DEBUGGING
4. Remote Configuration
Remote Configuration commands show configuration options available when
debugging remote programs.
set remoteaddresssize bits:
Set the maximum size of address in a memory packet to the specified number of
bits.
show remoteaddresssize:
Show the current value of remote address size in bits.
15. REMOTE DEBUGGING
• 5. Remote Stub
• The stub files provided with gdb implement the target side of the communication protocol, and
the gdb side is implemented in the gdb source file for example in remote.c
• The debugging stub is specific to the architecture of the remote machine; for example, use
sparc-stub.c to debug programs on sparc boards.
• These working remote stubs are distributed with gdb:
• i386-stub.c
• For Intel 386 and compatible architectures.
• m68k-stub.c
• For Motorola 680x0 architectures.
16. FEATURES OF GDB
GDB uses command line interface.
GDB has 3 feature which are commonly used:
1. Compiling
2. Invoking and Quitting GDB
3. Commands
17. COMPILING
To prepare program for
debugging with gdb, it must be
compiled it with the -g flag. So,
if your program is in a source
file called gizem.cpp and you
want to put the executable in
the file gizem;
$ g++ -g gizem.cpp
$ gdb ./a.out
$ (gdb) run
18. GCC’s C++ Compiler
The g++ compiler accepts both single-letter options, such as -o, and
multiletter options, such as -ansi. Because it accepts both types of options
you cannot group multiple single-letter options together, as you may be
used to doing in many GNU and Unix/Linux programs.
• For example, the multiletter option -pg is not the same as the two
single-letter options -p -g. The -pg option creates extra code in the final
binary that outputs profile information for the GNU code profiler, gprof.
On the other hand, the -p -g options generate extra code in the resulting
binary that produces profiling information for use by the prof code
profiler (-p) and causes gcc to generate debugging information using the
operating system’s normal format (-g).
19. COMPILING AND RUNNING PROGRAM
C++ CODE EXAMPLE
• $ g++ -g gizem.cpp:
Command for compiling source file. Invoke g++ passing name of the source file.
–g flag used in order to include appropriate debug information on the binary
generated.
• $ gdb ./a.out:
Result on Linux and Unix systems generates executable file named a.out in the
current directory. We can execute this executable file by typing ./a.out
• $ (gdb) run:
You can run the program by typing run. Program runs with current arguments.
20. GCC’s C++ Compiler
Despite its sensitivity to the grouping of multiple single-letter options, you are generally free to
mix the order of options and compiler arguments on the gcc command line.
$ g++ example.cpp -g –o example = $ g++ -g –o gcc example.cpp example
• g++: compiler is for c++ language.
• example.cpp: Source file name of c++ code
• -g: Flag of Compiling for given source file
• -o: Flag for specifying the name of the output file. The executable will be named a.out unless you
use this option.
• example: Output file name
./example
This command used to execute output of compiled program. After executing this code output of
program can be seen.
21. COMPILING C CODE
• Compiling a single source file, add.c, using gcc is easy—just invoke gcc, passing the
name of the source file as the argument.
$ gcc add.c
$ ./a.out
• To define the name of output file that gcc produces, -o option can be used as in c++
mode.
$gcc add.c –o output
$./output
• If you are compiling multiple source files using gcc, you can simply specify them all on
the gcc command line, as in the following example, which leaves the compiled and
linked executable in the file named addition.
$gcc add.c helper.c –o addition
./addition
23. Debugging
Debugging program named
broken.cpp with logical error:
After completing compile and
execute processes of program in
order to start to debugger using
output file named broken:
$ gdb broken
To set breakpoint at specific line
we can use b command and
number of line
(gdb) b 43
24. Debugging
• After setting breakpoints, we start to run program in debugger with;
(gdb) run
• Now program runs and ask us for input after entering inputs of program,
program execution stops at first breakpoint.
25. Debugging
• If we want to investigate function’s inside where program stopped executing at
breakpoint, we can step into function’s inside with
(gdb) step
• Program controls it’s first statement of the function ComputeSeriesValue (x=2,
n=3)
26. Debugging
• To continue to debug program we can
use some specific commands such as
(gdb) next
• Next command is similar to step
command except it will step over
functions and also we can use n and s
instead of next and step respectively.
(gdb) n
(gdb) s
• If the command is simply a repeat of the
previous command, you can just hit
return, which will execute the last
command.
(gdb)
27. Debugging
• If you want to know where you are in the program's execution you
can view the contents of the stack using the backtrace command.
(gdb) bt
28. Debugging
• We can step through the program and examine the values using the print
command.
• The print command reveals that the value of fact variable never
changes. Note that the function is returning a value of 0 for the
function call ComputeFactorial(number=0). This is an ERROR.
29. Debugging program with Floating Point exception
error
After debugging program and executing it we see that program has
floating point exception error.
30. Debugging program with Floating Point exception
error
This means problems occur in line 17 and in return a/b expression.
31. Invoking and Quitting GDB
To start gdb, just type gdb at the unix prompt. Gdb will give you a prompt that
looks like this:
We can quit GDB using quit command
(gdb) quit
32. COMMANDS
• Help
• File
• Run
• Break
• Delete
• Clear
• Continue
• Step
• Next
• Until
• List
• print