Searching-Memory.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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: Searching Memory</title>
  16. <meta name="description" content="Debugging with GDB: Searching Memory">
  17. <meta name="keywords" content="Debugging with GDB: Searching Memory">
  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="Data.html#Data" rel="up" title="Data">
  26. <link href="Value-Sizes.html#Value-Sizes" rel="next" title="Value Sizes">
  27. <link href="Caching-Target-Data.html#Caching-Target-Data" rel="previous" title="Caching Target Data">
  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="Searching-Memory"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Value-Sizes.html#Value-Sizes" accesskey="n" rel="next">Value Sizes</a>, Previous: <a href="Caching-Target-Data.html#Caching-Target-Data" accesskey="p" rel="previous">Caching Target Data</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</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="Search-Memory"></a>
  65. <h3 class="section">10.22 Search Memory</h3>
  66. <a name="index-searching-memory"></a>
  67. <p>Memory can be searched for a particular sequence of bytes with the
  68. <code>find</code> command.
  69. </p>
  70. <dl compact="compact">
  71. <dd><a name="index-find"></a>
  72. </dd>
  73. <dt><code>find <span class="roman">[</span>/<var>sn</var><span class="roman">]</span> <var>start_addr</var>, +<var>len</var>, <var>val1</var> <span class="roman">[</span>, <var>val2</var>, &hellip;<span class="roman">]</span></code></dt>
  74. <dt><code>find <span class="roman">[</span>/<var>sn</var><span class="roman">]</span> <var>start_addr</var>, <var>end_addr</var>, <var>val1</var> <span class="roman">[</span>, <var>val2</var>, &hellip;<span class="roman">]</span></code></dt>
  75. <dd><p>Search memory for the sequence of bytes specified by <var>val1</var>, <var>val2</var>,
  76. etc. The search begins at address <var>start_addr</var> and continues for either
  77. <var>len</var> bytes or through to <var>end_addr</var> inclusive.
  78. </p></dd>
  79. </dl>
  80. <p><var>s</var> and <var>n</var> are optional parameters.
  81. They may be specified in either order, apart or together.
  82. </p>
  83. <dl compact="compact">
  84. <dt><span class="roman"><var>s</var>, search query size</span></dt>
  85. <dd><p>The size of each search query value.
  86. </p>
  87. <dl compact="compact">
  88. <dt><code>b</code></dt>
  89. <dd><p>bytes
  90. </p></dd>
  91. <dt><code>h</code></dt>
  92. <dd><p>halfwords (two bytes)
  93. </p></dd>
  94. <dt><code>w</code></dt>
  95. <dd><p>words (four bytes)
  96. </p></dd>
  97. <dt><code>g</code></dt>
  98. <dd><p>giant words (eight bytes)
  99. </p></dd>
  100. </dl>
  101. <p>All values are interpreted in the current language.
  102. This means, for example, that if the current source language is C/C<tt>++</tt>
  103. then searching for the string &ldquo;hello&rdquo; includes the trailing &rsquo;\0&rsquo;.
  104. The null terminator can be removed from searching by using casts,
  105. e.g.: &lsquo;<samp>{char[5]}&quot;hello&quot;</samp>&rsquo;.
  106. </p>
  107. <p>If the value size is not specified, it is taken from the
  108. value&rsquo;s type in the current language.
  109. This is useful when one wants to specify the search
  110. pattern as a mixture of types.
  111. Note that this means, for example, that in the case of C-like languages
  112. a search for an untyped 0x42 will search for &lsquo;<samp>(int) 0x42</samp>&rsquo;
  113. which is typically four bytes.
  114. </p>
  115. </dd>
  116. <dt><span class="roman"><var>n</var>, maximum number of finds</span></dt>
  117. <dd><p>The maximum number of matches to print. The default is to print all finds.
  118. </p></dd>
  119. </dl>
  120. <p>You can use strings as search values. Quote them with double-quotes
  121. (<code>&quot;</code>).
  122. The string value is copied into the search pattern byte by byte,
  123. regardless of the endianness of the target and the size specification.
  124. </p>
  125. <p>The address of each match found is printed as well as a count of the
  126. number of matches found.
  127. </p>
  128. <p>The address of the last value found is stored in convenience variable
  129. &lsquo;<samp>$_</samp>&rsquo;.
  130. A count of the number of matches is stored in &lsquo;<samp>$numfound</samp>&rsquo;.
  131. </p>
  132. <p>For example, if stopped at the <code>printf</code> in this function:
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample">void
  136. hello ()
  137. {
  138. static char hello[] = &quot;hello-hello&quot;;
  139. static struct { char c; short s; int i; }
  140. __attribute__ ((packed)) mixed
  141. = { 'c', 0x1234, 0x87654321 };
  142. printf (&quot;%s\n&quot;, hello);
  143. }
  144. </pre></div>
  145. <p>you get during debugging:
  146. </p>
  147. <div class="smallexample">
  148. <pre class="smallexample">(gdb) find &amp;hello[0], +sizeof(hello), &quot;hello&quot;
  149. 0x804956d &lt;hello.1620+6&gt;
  150. 1 pattern found
  151. (gdb) find &amp;hello[0], +sizeof(hello), 'h', 'e', 'l', 'l', 'o'
  152. 0x8049567 &lt;hello.1620&gt;
  153. 0x804956d &lt;hello.1620+6&gt;
  154. 2 patterns found.
  155. (gdb) find &amp;hello[0], +sizeof(hello), {char[5]}&quot;hello&quot;
  156. 0x8049567 &lt;hello.1620&gt;
  157. 0x804956d &lt;hello.1620+6&gt;
  158. 2 patterns found.
  159. (gdb) find /b1 &amp;hello[0], +sizeof(hello), 'h', 0x65, 'l'
  160. 0x8049567 &lt;hello.1620&gt;
  161. 1 pattern found
  162. (gdb) find &amp;mixed, +sizeof(mixed), (char) 'c', (short) 0x1234, (int) 0x87654321
  163. 0x8049560 &lt;mixed.1625&gt;
  164. 1 pattern found
  165. (gdb) print $numfound
  166. $1 = 1
  167. (gdb) print $_
  168. $2 = (void *) 0x8049560
  169. </pre></div>
  170. <hr>
  171. <div class="header">
  172. <p>
  173. Next: <a href="Value-Sizes.html#Value-Sizes" accesskey="n" rel="next">Value Sizes</a>, Previous: <a href="Caching-Target-Data.html#Caching-Target-Data" accesskey="p" rel="previous">Caching Target Data</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</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>
  174. </div>
  175. </body>
  176. </html>