Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)
11 Altering Execution
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.
Assignment to variables
To alter the value of a variable, evaluate an assignment expression. See “Expressions” (page 58).
For example,
print x=4
stores the value 4 into the variable x, and then prints the value of the assignment expression (which
is 4). See Chapter 9 (page 71), for more information on operators in supported languages.
If you are not interested in seeing the value of the assignment, use the set command instead of
the print command. set is really the same as print except that the expression's value is not
printed and is not put in the value history (see “Value history” (page 67)). The expression is
evaluated only for its effects.
The set command has a number of subcommands that conflict with the names of program variables.
The set variable command is a better alternative for setting program variables. The following
two examples illustrate the same:
• Example 1
((gdb)) whatis width
type = double
((gdb)) p width
$4 = 13
((gdb)) set width=47
Invalid syntax in expression.
The invalid expression, of course, is '=47'. In order to actually set the program's variable
width, use
• Example 2
((gdb)) set var width=47
if your program has a variable g, you run into problems if you try to set a new value with just
'set g=4', because GDB has the command set gnutarget, abbreviated set g:
((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".
84 Altering Execution