Go to the previous, next section.
A target is the execution environment occupied by your program.
Often, runs in the same host environment as your program; in
that case, the debugging target is specified as a side effect when you
use the file
or core
commands. When you need more
flexibility--for example, running on a physically separate
host, or controlling a standalone system over a serial port or a
realtime system over a TCP/IP connection--you
can use the target
command to specify one of the target types
configured for (see section Commands for managing targets).
There are three classes of targets: processes, core files, and executable files. can work concurrently on up to three active targets, one in each class. This allows you to (for example) start a process and inspect its activity without abandoning your work on a core file.
For example, if you execute `gdb a.out', then the executable file
a.out
is the only active target. If you designate a core file as
well--presumably from a prior run that crashed and coredumped--then
has two active targets and uses them in tandem, looking
first in the corefile target, then in the executable file, to satisfy
requests for memory addresses. (Typically, these two classes of target
are complementary, since core files contain only a program's
read-write memory--variables and so on--plus machine status, while
executable files contain only the program text and initialized data.)
When you type run
, your executable file becomes an active process
target as well. When a process target is active, all commands
requesting memory addresses refer to that target; addresses in an
active core file or
executable file target are obscured while the process
target is active.
Use the core-file
and exec-file
commands to select a
new core file or executable target (see section Commands to specify files). To specify as a target a process that is already running, use
the attach
command (see section Debugging an already-running process).
target type parameters
Further parameters are interpreted by the target protocol, but typically include things like device names or host names to connect with, process numbers, and baud rates.
The target
command does not repeat if you press RET again
after executing the command.
help target
info target
or info files
(see section Commands to specify files).
help target name
Here are some common targets (available, or not, depending on the GDB configuration):
target exec program
target core filename
If you are trying to debug a program running on a machine that cannot run GDB in the usual way, it is often useful to use remote debugging. For example, you might use remote debugging on an operating system kernel, or on a small system which does not have a general purpose operating system powerful enough to run a full-featured debugger.
Some configurations of GDB have special serial or TCP/IP interfaces to make this work with particular debugging targets. In addition, GDB comes with a generic serial protocol (specific to GDB, but not specific to any particular target system) which you can use if you write the remote stubs--the code that runs on the remote system to communicate with GDB.
Other remote targets may be available in your
configuration of GDB; use help targets
to list them.
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 :
sparc-stub.c
m68k-stub.c
i386-stub.c
The `README' file in the distribution may list other recently added stubs.
The debugging stub for your architecture supplies these three subroutines:
set_debug_traps
handle_exception
to run when your
program stops. You must call this subroutine explicitly near the
beginning of your program.
handle_exception
handle_exception
to
run when a trap is triggered.
handle_exception
takes control when your program stops during
execution (for example, on a breakpoint), and mediates communications
with on the host machine. This is where the communications
protocol is implemented; handle_exception
acts as the
representative on the target machine; it begins by sending summary
information on the state of your program, then continues to execute,
retrieving and transmitting any information needs, until you
execute a command that makes your program resume; at that point,
handle_exception
returns control to your own code on the target
machine.
breakpoint
handle_exception
---in effect, to . On some machines,
simply receiving characters on the serial port may also trigger a trap;
again, in that situation, you don't need to call breakpoint
from
your own program--simply running `target remote' from the host
session gets control.
Call breakpoint
if none of these is true, or if you simply want
to make certain your program stops at a predetermined point for the
start of your debugging session.
The debugging stubs that come with are set up for a particular chip architecture, but they have no information about the rest of your debugging target machine.
First of all you need to tell the stub how to communicate with the serial port.
int getDebugChar()
getchar
for your target system; a
different name is used to allow you to distinguish the two if you wish.
void putDebugChar(int)
putchar
for your target system; a
different name is used to allow you to distinguish the two if you wish.
If you want to be able to stop your program while it is
running, you need to use an interrupt-driven serial driver, and arrange
for it to stop when it receives a ^C
(`\003', the control-C
character). That is the character which uses to tell the
remote system to stop.
Getting the debugging target to return the proper status to
probably requires changes to the standard stub; one quick and dirty way
is to just execute a breakpoint instruction (the "dirty" part is that
reports a SIGTRAP
instead of a SIGINT
).
Other routines you need to supply are:
void exceptionHandler (int exception_number, void *exception_address)
For the 386, exception_address should be installed as an interrupt
gate so that interrupts are masked while the handler runs. The gate
should be at privilege level 0 (the most privileged level). The
SPARC and 68k stubs are able to mask interrupts themself without
help from exceptionHandler
.
void flush_i_cache()
On target machines that have instruction caches, requires this function to make certain that the state of your program is stable.
You must also make sure this library routine is available:
void *memset(void *, int, int)
memset
that sets an area of
memory to a known value. If you have one of the free versions of
libc.a
, memset
can be found there; otherwise, you must
either obtain it from your hardware manufacturer, or write your own.
If you do not use the GNU C compiler, you may need other standard
library subroutines as well; this varies from one stub to another,
but in general the stubs are likely to use any of the common library
subroutines which gcc
generates as inline code.
In summary, when your program is ready to debug, you must follow these steps.
getDebugChar
,putDebugChar
,flush_i_cache
,memset
,exceptionHandler
.
set_debug_traps(); breakpoint();
exceptionHook
. Normally you just use
void (*exceptionHook)() = 0;
but if before calling set_debug_traps
, you set it to point to a
function in your program, that function is called when
continues after stopping on a trap (for example, bus
error). The function indicated by
exceptionHook
is called with
one parameter: an int
which is the exception number.
Then establish communication using the target remote
command.
Its argument specifies how to communicate with the target
machine--either via a devicename attached to a direct serial line, or a
TCP port (usually to a terminal server which in turn has a serial line
to the target). For example, to use a serial line connected to the
device named `/dev/ttyb':
target remote /dev/ttyb
To use a TCP connection, use an argument of the form
host:port
. For example, to connect to port 2828 on a
terminal server named manyfarms
:
target remote manyfarms:2828
Now you can use all the usual commands to examine and change data and to step and continue the remote program.
To resume the remote program and stop debugging it, use the detach
command.
Whenever is waiting for the remote program, if you type the interrupt character (often C-C), attempts to stop the program. This may or may not succeed, depending in part on the hardware and the serial drivers the remote system uses. If you type the interrupt character once again, displays this prompt:
Interrupted while waiting for the program. Give up (and stop debugging it)? (y or n)
If you type y, abandons the remote debugging session. (If you decide you want to try again later, you can use `target remote' again to connect once more.) If you type n, goes back to waiting.
The stub files provided with implement the target side of the communication protocol, and the side is implemented in the source file `remote.c'. Normally, you can simply allow these subroutines to communicate, and ignore the details. (If you're implementing your own stub file, you can still ignore the details: start with one of the existing stub files. `sparc-stub.c' is the best organized, and therefore the easiest to read.)
However, there may be occasions when you need to know something about the protocol--for example, if there is only one serial port to your target machine, you might want your program to do something special if it recognizes a packet meant for .
All commands and responses (other than acknowledgements, which are single characters) are sent as a packet which includes a checksum. A packet is introduced with the character `$', and ends with the character `#' followed by a two-digit checksum:
$packet info#checksum
checksum is computed as the modulo 256 sum of the packet info characters.
When either the host or the target machine receives a packet, the first response expected is an acknowledgement: a single character, either `+' (to indicate the package was received correctly) or `-' (to request retransmission).
The host () sends commands, and the target (the debugging stub incorporated in your program) sends data in response. The target also sends data when your program stops.
Command packets are distinguished by their first character, which identifies the kind of command.
These are the commands currently supported:
g
G
maddr,count
Maddr,count:...
c
caddr
s
saddr
k
?
If you have trouble with the serial connection, you can use the command
set remotedebug
. This makes report on all packets sent
back and forth across the serial line to the remote machine. The
packet-debugging information is printed on the standard output
stream. set remotedebug off
turns it off, and show
remotedebug
shows you its current state.
target sim
After specifying this target, you can debug programs for the simulated
CPU in the same style as programs for your host computer; use the
file
command to load a new program image, the run
command
to run your program, and so on.
As well as making available all the usual machine registers (see
info reg
), this debugging target provides three additional items
of information as specially named registers:
cycles
insts
time
You can refer to these values in expressions with the usual conventions; for example, `b fputc if $cycles>5000' sets a conditional breakpoint that suspends only after at least 5000 simulated clock ticks.
Go to the previous, next section.