1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Assignment</title> 18 19<meta name="description" content="Debugging with GDB: Assignment"> 20<meta name="keywords" content="Debugging with GDB: Assignment"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Altering.html#Altering" rel="up" title="Altering"> 29<link href="Jumping.html#Jumping" rel="next" title="Jumping"> 30<link href="Altering.html#Altering" rel="previous" title="Altering"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Assignment"></a> 65<div class="header"> 66<p> 67Next: <a href="Jumping.html#Jumping" accesskey="n" rel="next">Jumping</a>, Up: <a href="Altering.html#Altering" accesskey="u" rel="up">Altering</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> 68</div> 69<hr> 70<a name="Assignment-to-Variables"></a> 71<h3 class="section">17.1 Assignment to Variables</h3> 72 73<a name="index-assignment"></a> 74<a name="index-setting-variables"></a> 75<p>To alter the value of a variable, evaluate an assignment expression. 76See <a href="Expressions.html#Expressions">Expressions</a>. For example, 77</p> 78<div class="smallexample"> 79<pre class="smallexample">print x=4 80</pre></div> 81 82<p>stores the value 4 into the variable <code>x</code>, and then prints the 83value of the assignment expression (which is 4). 84See <a href="Languages.html#Languages">Using <small>GDB</small> with Different Languages</a>, for more 85information on operators in supported languages. 86</p> 87<a name="index-set-variable"></a> 88<a name="index-variables_002c-setting"></a> 89<p>If you are not interested in seeing the value of the assignment, use the 90<code>set</code> command instead of the <code>print</code> command. <code>set</code> is 91really the same as <code>print</code> except that the expression’s value is 92not printed and is not put in the value history (see <a href="Value-History.html#Value-History">Value History</a>). The expression is evaluated only for its effects. 93</p> 94<p>If the beginning of the argument string of the <code>set</code> command 95appears identical to a <code>set</code> subcommand, use the <code>set 96variable</code> command instead of just <code>set</code>. This command is identical 97to <code>set</code> except for its lack of subcommands. For example, if your 98program has a variable <code>width</code>, you get an error if you try to set 99a new value with just ‘<samp>set width=13</samp>’, because <small>GDB</small> has the 100command <code>set width</code>: 101</p> 102<div class="smallexample"> 103<pre class="smallexample">(gdb) whatis width 104type = double 105(gdb) p width 106$4 = 13 107(gdb) set width=47 108Invalid syntax in expression. 109</pre></div> 110 111<p>The invalid expression, of course, is ‘<samp>=47</samp>’. In 112order to actually set the program’s variable <code>width</code>, use 113</p> 114<div class="smallexample"> 115<pre class="smallexample">(gdb) set var width=47 116</pre></div> 117 118<p>Because the <code>set</code> command has many subcommands that can conflict 119with the names of program variables, it is a good idea to use the 120<code>set variable</code> command instead of just <code>set</code>. For example, if 121your program has a variable <code>g</code>, you run into problems if you try 122to set a new value with just ‘<samp>set g=4</samp>’, because <small>GDB</small> has 123the command <code>set gnutarget</code>, abbreviated <code>set g</code>: 124</p> 125<div class="smallexample"> 126<pre class="smallexample">(gdb) whatis g 127type = double 128(gdb) p g 129$1 = 1 130(gdb) set g=4 131(gdb) p g 132$2 = 1 133(gdb) r 134The program being debugged has been started already. 135Start it from the beginning? (y or n) y 136Starting program: /home/smith/cc_progs/a.out 137"/home/smith/cc_progs/a.out": can't open to read symbols: 138 Invalid bfd target. 139(gdb) show g 140The current BFD target is "=4". 141</pre></div> 142 143<p>The program variable <code>g</code> did not change, and you silently set the 144<code>gnutarget</code> to an invalid value. In order to set the variable 145<code>g</code>, use 146</p> 147<div class="smallexample"> 148<pre class="smallexample">(gdb) set var g=4 149</pre></div> 150 151<p><small>GDB</small> allows more implicit conversions in assignments than C; you can 152freely store an integer value into a pointer variable or vice versa, 153and you can convert any structure to any other structure that is the 154same length or shorter. 155</p> 156<p>To store values into arbitrary places in memory, use the ‘<samp>{…}</samp>’ 157construct to generate a value of specified type at a specified address 158(see <a href="Expressions.html#Expressions">Expressions</a>). For example, <code>{int}0x83040</code> refers 159to memory location <code>0x83040</code> as an integer (which implies a certain size 160and representation in memory), and 161</p> 162<div class="smallexample"> 163<pre class="smallexample">set {int}0x83040 = 4 164</pre></div> 165 166<p>stores the value 4 into that memory location. 167</p> 168<hr> 169<div class="header"> 170<p> 171Next: <a href="Jumping.html#Jumping" accesskey="n" rel="next">Jumping</a>, Up: <a href="Altering.html#Altering" accesskey="u" rel="up">Altering</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> 172</div> 173 174 175 176</body> 177</html> 178