Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

177 rindas
7.4KB

  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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Assignment (Debugging with GDB)</title>
  17. <meta name="description" content="Assignment (Debugging with GDB)">
  18. <meta name="keywords" content="Assignment (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="prev" title="Altering">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Assignment"></a>
  58. <div class="header">
  59. <p>
  60. 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>
  61. </div>
  62. <hr>
  63. <a name="Assignment-to-Variables"></a>
  64. <h3 class="section">17.1 Assignment to Variables</h3>
  65. <a name="index-assignment"></a>
  66. <a name="index-setting-variables"></a>
  67. <p>To alter the value of a variable, evaluate an assignment expression.
  68. See <a href="Expressions.html#Expressions">Expressions</a>. For example,
  69. </p>
  70. <div class="smallexample">
  71. <pre class="smallexample">print x=4
  72. </pre></div>
  73. <p>stores the value 4 into the variable <code>x</code>, and then prints the
  74. value of the assignment expression (which is 4).
  75. See <a href="Languages.html#Languages">Using <small>GDB</small> with Different Languages</a>, for more
  76. information on operators in supported languages.
  77. </p>
  78. <a name="index-set-variable"></a>
  79. <a name="index-variables_002c-setting"></a>
  80. <p>If you are not interested in seeing the value of the assignment, use the
  81. <code>set</code> command instead of the <code>print</code> command. <code>set</code> is
  82. really the same as <code>print</code> except that the expression&rsquo;s value is
  83. 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.
  84. </p>
  85. <p>If the beginning of the argument string of the <code>set</code> command
  86. appears identical to a <code>set</code> subcommand, use the <code>set
  87. variable</code> command instead of just <code>set</code>. This command is identical
  88. to <code>set</code> except for its lack of subcommands. For example, if your
  89. program has a variable <code>width</code>, you get an error if you try to set
  90. a new value with just &lsquo;<samp>set width=13</samp>&rsquo;, because <small>GDB</small> has the
  91. command <code>set width</code>:
  92. </p>
  93. <div class="smallexample">
  94. <pre class="smallexample">(gdb) whatis width
  95. type = double
  96. (gdb) p width
  97. $4 = 13
  98. (gdb) set width=47
  99. Invalid syntax in expression.
  100. </pre></div>
  101. <p>The invalid expression, of course, is &lsquo;<samp>=47</samp>&rsquo;. In
  102. order to actually set the program&rsquo;s variable <code>width</code>, use
  103. </p>
  104. <div class="smallexample">
  105. <pre class="smallexample">(gdb) set var width=47
  106. </pre></div>
  107. <p>Because the <code>set</code> command has many subcommands that can conflict
  108. with the names of program variables, it is a good idea to use the
  109. <code>set variable</code> command instead of just <code>set</code>. For example, if
  110. your program has a variable <code>g</code>, you run into problems if you try
  111. to set a new value with just &lsquo;<samp>set g=4</samp>&rsquo;, because <small>GDB</small> has
  112. the command <code>set gnutarget</code>, abbreviated <code>set g</code>:
  113. </p>
  114. <div class="smallexample">
  115. <pre class="smallexample">(gdb) whatis g
  116. type = double
  117. (gdb) p g
  118. $1 = 1
  119. (gdb) set g=4
  120. (gdb) p g
  121. $2 = 1
  122. (gdb) r
  123. The program being debugged has been started already.
  124. Start it from the beginning? (y or n) y
  125. Starting program: /home/smith/cc_progs/a.out
  126. &quot;/home/smith/cc_progs/a.out&quot;: can't open to read symbols:
  127. Invalid bfd target.
  128. (gdb) show g
  129. The current BFD target is &quot;=4&quot;.
  130. </pre></div>
  131. <p>The program variable <code>g</code> did not change, and you silently set the
  132. <code>gnutarget</code> to an invalid value. In order to set the variable
  133. <code>g</code>, use
  134. </p>
  135. <div class="smallexample">
  136. <pre class="smallexample">(gdb) set var g=4
  137. </pre></div>
  138. <p><small>GDB</small> allows more implicit conversions in assignments than C; you can
  139. freely store an integer value into a pointer variable or vice versa,
  140. and you can convert any structure to any other structure that is the
  141. same length or shorter.
  142. </p>
  143. <p>To store values into arbitrary places in memory, use the &lsquo;<samp>{&hellip;}</samp>&rsquo;
  144. construct to generate a value of specified type at a specified address
  145. (see <a href="Expressions.html#Expressions">Expressions</a>). For example, <code>{int}0x83040</code> refers
  146. to memory location <code>0x83040</code> as an integer (which implies a certain size
  147. and representation in memory), and
  148. </p>
  149. <div class="smallexample">
  150. <pre class="smallexample">set {int}0x83040 = 4
  151. </pre></div>
  152. <p>stores the value 4 into that memory location.
  153. </p>
  154. <hr>
  155. <div class="header">
  156. <p>
  157. 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>
  158. </div>
  159. </body>
  160. </html>