[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Once you think you have found an error in your program, you might want to find out for certain whether correcting the apparent error would lead to correct results in the rest of the run. You can find the answer by experiment, using the GDB features for altering execution of the program.
For example, you can store new values into variables or memory locations, give your program a signal, restart it at a different address, or even return prematurely from a function.
14.1 Assignment to Variables Assignment to variables 14.2 Continuing at a Different Address Continuing at a different address 14.3 Giving your Program a Signal Giving your program a signal 14.4 Returning from a Function Returning from a function 14.5 Calling Program Functions Calling your program's functions 14.6 Patching Programs Patching your program
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
To alter the value of a variable, evaluate an assignment expression. See section Expressions. For example,
print x=4 |
(gdb) whatis width type = double (gdb) p width $4 = 13 (gdb) set width=47 Invalid syntax in expression. |
(gdb) set var width=47 |
(gdb) whatis g type = double (gdb) p g $1 = 1 (gdb) set g=4 (gdb) p g $2 = 1 (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/smith/cc_progs/a.out "/home/smith/cc_progs/a.out": can't open to read symbols: Invalid bfd target. (gdb) show g The current BFD target is "=4". |
(gdb) set var g=4 |
set {int}0x83040 = 4 |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Ordinarily, when you continue your program, you do so at the place where
it stopped, with the continue
command. You can instead continue at
an address of your own choosing, with the following commands:
jump linespec
jump location
tbreak
command in conjunction with
jump
. See section Setting Breakpoints.
The jump
command does not change the current stack frame, or
the stack pointer, or the contents of any memory location or any
register other than the program counter. If line linespec is in
a different function from the one currently executing, the results may
be bizarre if the two functions expect different patterns of arguments or
of local variables. For this reason, the jump
command requests
confirmation if the specified line is not in the function currently
executing. However, even bizarre results are predictable if you are
well acquainted with the machine-language code of your program.
On many systems, you can get much the same effect as the jump
command by storing a new value into the register $pc
. The
difference is that this does not start your program running; it only
changes the address of where it will run when you continue. For
example,
set $pc = 0x485 |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
signal signal
signal 2
and signal
SIGINT
are both ways of sending an interrupt signal.
Alternatively, if signal is zero, continue execution without
giving a signal. This is useful when your program stopped on account of
a signal and would ordinary see the signal when resumed with the
continue
command; `signal 0' causes it to resume without a
signal.
signal
does not repeat when you press RET a second time
after executing the command.
Invoking the signal
command is not the same as invoking the
kill
utility from the shell. Sending a signal with kill
causes GDB to decide what to do with the signal depending on
the signal handling tables (see section 5.3 Signals). The signal
command
passes the signal directly to your program.
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
return
return expression
return
command. If you give an
expression argument, its value is used as the function's return
value.
When you use return
, GDB discards the selected stack frame
(and all frames within it). You can think of this as making the
discarded frame return prematurely. If you wish to specify a value to
be returned, give that value as the argument to return
.
This pops the selected stack frame (see section Selecting a Frame), and any other frames inside of it, leaving its caller as the innermost remaining frame. That frame becomes selected. The specified value is stored in the registers used for returning values of functions.
The return
command does not resume execution; it leaves the
program stopped in the state that would exist if the function had just
returned. In contrast, the finish
command (see section Continuing and Stepping) resumes execution until the
selected stack frame returns naturally.
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
print expr
call expr
void
returned values.
You can use this variant of the print
command if you want to
execute a function from your program that does not return anything
(a.k.a. a void function), but without cluttering the output
with void
returned values that GDB will otherwise
print. If the result is not void, it is printed and saved in the
value history.
It is possible for the function you call via the print
or
call
command to generate a signal (e.g., if there's a bug in
the function, or if you passed it incorrect arguments). What happens
in that case is controlled by the set unwindonsignal
command.
set unwindonsignal
Set unwinding of the stack if a signal is received while in a function that GDB called in the program being debugged. If set to on, GDB unwinds the stack it created for the call and restores the context to what it was before the call. If set to off (the default), GDB stops in the frame where the signal was received.
show unwindonsignal
Show the current setting of stack unwinding in the functions called by GDB.
Sometimes, a function you wish to call is actually a weak alias for another function. In such case, GDB might not pick up the type information, including the types of the function arguments, which causes GDB to call the inferior function incorrectly. As a result, the called function will function erroneously and may even crash. A solution to that is to use the name of the aliased function instead.
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
By default, GDB opens the file containing your program's executable code (or the corefile) read-only. This prevents accidental alterations to machine code; but it also prevents you from intentionally patching your program's binary.
If you'd like to be able to patch the binary, you can specify that
explicitly with the set write
command. For example, you might
want to turn on internal debugging flags, or even to make emergency
repairs.
set write on
set write off
If you have already loaded a file, you must load it again (using the
exec-file
or core-file
command) after changing set
write
, for your new setting to take effect.
show write
Display whether executable files and core files are opened for writing as well as reading.
[ <<] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Please send FSF & GNU inquiries & questions to gnu@gnu.org. There are also other ways to contact the FSF.
These pages are maintained by the GDB developers.
Copyright Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.
This document was generated by GDB Administrator on March, 27 2008 using texi2html