[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
14.1 Assignment to Variables
To alter the value of a variable, evaluate an assignment expression. See section Expressions. 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 section Using No value for GDBN with Different Languages, 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 Value History). 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 No value for GDBN has the
command set width
:
(No value for GDBP) whatis width type = double (No value for GDBP) p width $4 = 13 (No value for GDBP) 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
(No value for GDBP) set var width=47 |
Because the set
command has many subcommands that can conflict
with the names of program variables, it is a good idea to use the
set variable
command instead of just set
. For example, 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 No value for GDBN has
the command set gnutarget
, abbreviated set g
:
(No value for GDBP) whatis g type = double (No value for GDBP) p g $1 = 1 (No value for GDBP) set g=4 (No value for GDBP) p g $2 = 1 (No value for GDBP) 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. (No value for GDBP) show g The current BFD target is "=4". |
The program variable g
did not change, and you silently set the
gnutarget
to an invalid value. In order to set the variable
g
, use
(No value for GDBP) set var g=4 |
No value for GDBN 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 section Expressions). 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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |