Assignment.html 7.5 KB

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