Chapter 17. Altering Execution

Table of Contents
17.1. Assignment to Variables
17.2. Continuing at a Different Address
17.3. Returning from a Function
17.4. Calling Program Functions
17.5. Patching Programs

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 debugger features for altering execution of the program.

For example, you can store new values into variables or memory locations, restart your program at a different address, or even return prematurely from a function.

17.1. Assignment to Variables

To alter the value of a variable, evaluate an assignment expression. See Chapter 28. 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 15, 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 Section 14.8.). The expression is evaluated only for its effects.

If the beginning of the argument string of the set command appears identical to a set subcommand, use the set variable command instead of just set. This command is identical to set except for its lack of subcommands. For example, if your program has a variable width, you get an error if you try to set a new value with just set width=13, because the debugger has the command set width:

(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

(gdb) set var width=47

The debugger allows more implicit conversions in assignments than C; you can freely store an integer value into a pointer variable or vice versa, and you can convert any structure to any other structure that is the same length or shorter.

To store values into arbitrary places in memory, use the {...} construct to generate a value of specified type at a specified address (see Chapter 28.). For example, {int}0x83040 refers to memory location 0x83040 as an integer (which implies a certain size and representation in memory), and

set {int}0x83040 = 4

stores the value 4 into that memory location.