Debugging with GDB Manual (5900-1473; WDB 6.2; January 2011)

Table Of Contents
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.
11.1 Assignment to variables
To alter the value of a variable, evaluate an assignment expression. See “Expressions
(page 76). 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 94), 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 89)). 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:
11.1 Assignment to variables 113