3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Standard C includes. */
25 /* Standard C++ includes. */
28 /* Local non-gdb includes. */
29 #include "arch-utils.h"
30 #include "cli/cli-utils.h"
31 #include "common/byte-vector.h"
36 /* Copied from bfd_put_bits. */
39 put_bits (bfd_uint64_t data
, gdb::byte_vector
&buf
, int bits
, bfd_boolean big_p
)
44 gdb_assert (bits
% 8 == 0);
47 size_t last
= buf
.size ();
48 buf
.resize (last
+ bytes
);
49 for (i
= 0; i
< bytes
; i
++)
51 int index
= big_p
? bytes
- i
- 1 : i
;
53 buf
[last
+ index
] = data
& 0xff;
58 /* Subroutine of find_command to simplify it.
59 Parse the arguments of the "find" command. */
61 static gdb::byte_vector
62 parse_find_args (const char *args
, ULONGEST
*max_countp
,
63 CORE_ADDR
*start_addrp
, ULONGEST
*search_space_lenp
,
66 /* Default to using the specified type. */
68 ULONGEST max_count
= ~(ULONGEST
) 0;
69 /* Buffer to hold the search pattern. */
70 gdb::byte_vector pattern_buf
;
72 ULONGEST search_space_len
;
77 error (_("Missing search parameters."));
79 /* Get search granularity and/or max count if specified.
80 They may be specified in either order, together or separately. */
86 while (*s
!= '\0' && *s
!= '/' && !isspace (*s
))
105 error (_("Invalid size granularity."));
112 /* Get the search range. */
114 v
= parse_to_comma_and_eval (&s
);
115 start_addr
= value_as_address (v
);
126 v
= parse_to_comma_and_eval (&s
);
127 len
= value_as_long (v
);
130 printf_filtered (_("Empty search range.\n"));
134 error (_("Invalid length."));
135 /* Watch for overflows. */
136 if (len
> CORE_ADDR_MAX
137 || (start_addr
+ len
- 1) < start_addr
)
138 error (_("Search space too large."));
139 search_space_len
= len
;
145 v
= parse_to_comma_and_eval (&s
);
146 end_addr
= value_as_address (v
);
147 if (start_addr
> end_addr
)
148 error (_("Invalid search space, end precedes start."));
149 search_space_len
= end_addr
- start_addr
+ 1;
150 /* We don't support searching all of memory
151 (i.e. start=0, end = 0xff..ff).
152 Bail to avoid overflows later on. */
153 if (search_space_len
== 0)
154 error (_("Overflow in address range "
155 "computation, choose smaller range."));
161 /* Fetch the search string. */
170 v
= parse_to_comma_and_eval (&s
);
175 x
= value_as_long (v
);
179 pattern_buf
.push_back (x
);
182 put_bits (x
, pattern_buf
, 16, big_p
);
185 put_bits (x
, pattern_buf
, 32, big_p
);
188 put_bits (x
, pattern_buf
, 64, big_p
);
194 const gdb_byte
*contents
= value_contents (v
);
195 pattern_buf
.insert (pattern_buf
.end (), contents
,
196 contents
+ TYPE_LENGTH (t
));
204 if (pattern_buf
.empty ())
205 error (_("Missing search pattern."));
207 if (search_space_len
< pattern_buf
.size ())
208 error (_("Search space too small to contain pattern."));
210 *max_countp
= max_count
;
211 *start_addrp
= start_addr
;
212 *search_space_lenp
= search_space_len
;
218 find_command (const char *args
, int from_tty
)
220 struct gdbarch
*gdbarch
= get_current_arch ();
221 bfd_boolean big_p
= gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
;
222 /* Command line parameters.
223 These are initialized to avoid uninitialized warnings from -Wall. */
224 ULONGEST max_count
= 0;
225 CORE_ADDR start_addr
= 0;
226 ULONGEST search_space_len
= 0;
227 /* End of command line parameters. */
228 unsigned int found_count
;
229 CORE_ADDR last_found_addr
;
231 gdb::byte_vector pattern_buf
= parse_find_args (args
, &max_count
,
236 /* Perform the search. */
241 while (search_space_len
>= pattern_buf
.size ()
242 && found_count
< max_count
)
244 /* Offset from start of this iteration to the next iteration. */
245 ULONGEST next_iter_incr
;
246 CORE_ADDR found_addr
;
247 int found
= target_search_memory (start_addr
, search_space_len
,
255 print_address (gdbarch
, found_addr
, gdb_stdout
);
256 printf_filtered ("\n");
258 last_found_addr
= found_addr
;
260 /* Begin next iteration at one byte past this match. */
261 next_iter_incr
= (found_addr
- start_addr
) + 1;
263 /* For robustness, we don't let search_space_len go -ve here. */
264 if (search_space_len
>= next_iter_incr
)
265 search_space_len
-= next_iter_incr
;
267 search_space_len
= 0;
268 start_addr
+= next_iter_incr
;
271 /* Record and print the results. */
273 set_internalvar_integer (lookup_internalvar ("numfound"), found_count
);
276 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
278 set_internalvar (lookup_internalvar ("_"),
279 value_from_pointer (ptr_type
, last_found_addr
));
282 if (found_count
== 0)
283 printf_filtered ("Pattern not found.\n");
285 printf_filtered ("%d pattern%s found.\n", found_count
,
286 found_count
> 1 ? "s" : "");
290 _initialize_mem_search (void)
292 add_cmd ("find", class_vars
, find_command
, _("\
293 Search memory for a sequence of bytes.\n\
295 [/SIZE-CHAR] [/MAX-COUNT] START-ADDRESS, END-ADDRESS, EXPR1 [, EXPR2 ...]\n\
296 find [/SIZE-CHAR] [/MAX-COUNT] START-ADDRESS, +LENGTH, EXPR1 [, EXPR2 ...]\n\
297 SIZE-CHAR is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
298 and if not specified the size is taken from the type of the expression\n\
299 in the current language.\n\
300 Note that this means for example that in the case of C-like languages\n\
301 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
302 which is typically four bytes, and a search for a string \"hello\" will\n\
303 include the trailing '\\0'. The null terminator can be removed from\n\
304 searching by using casts, e.g.: {char[5]}\"hello\".\n\
306 The address of the last match is stored as the value of \"$_\".\n\
307 Convenience variable \"$numfound\" is set to the number of matches."),