Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / findcmd.c
1 /* The find command.
2
3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21
22 /* Standard C includes. */
23 #include <ctype.h>
24
25 /* Standard C++ includes. */
26 #include <algorithm>
27
28 /* Local non-gdb includes. */
29 #include "arch-utils.h"
30 #include "cli/cli-utils.h"
31 #include "common/byte-vector.h"
32 #include "gdbcmd.h"
33 #include "target.h"
34 #include "value.h"
35
36 /* Copied from bfd_put_bits. */
37
38 static void
39 put_bits (bfd_uint64_t data, gdb::byte_vector &buf, int bits, bfd_boolean big_p)
40 {
41 int i;
42 int bytes;
43
44 gdb_assert (bits % 8 == 0);
45
46 bytes = bits / 8;
47 size_t last = buf.size ();
48 buf.resize (last + bytes);
49 for (i = 0; i < bytes; i++)
50 {
51 int index = big_p ? bytes - i - 1 : i;
52
53 buf[last + index] = data & 0xff;
54 data >>= 8;
55 }
56 }
57
58 /* Subroutine of find_command to simplify it.
59 Parse the arguments of the "find" command. */
60
61 static gdb::byte_vector
62 parse_find_args (const char *args, ULONGEST *max_countp,
63 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
64 bfd_boolean big_p)
65 {
66 /* Default to using the specified type. */
67 char size = '\0';
68 ULONGEST max_count = ~(ULONGEST) 0;
69 /* Buffer to hold the search pattern. */
70 gdb::byte_vector pattern_buf;
71 CORE_ADDR start_addr;
72 ULONGEST search_space_len;
73 const char *s = args;
74 struct value *v;
75
76 if (args == NULL)
77 error (_("Missing search parameters."));
78
79 /* Get search granularity and/or max count if specified.
80 They may be specified in either order, together or separately. */
81
82 while (*s == '/')
83 {
84 ++s;
85
86 while (*s != '\0' && *s != '/' && !isspace (*s))
87 {
88 if (isdigit (*s))
89 {
90 max_count = atoi (s);
91 while (isdigit (*s))
92 ++s;
93 continue;
94 }
95
96 switch (*s)
97 {
98 case 'b':
99 case 'h':
100 case 'w':
101 case 'g':
102 size = *s++;
103 break;
104 default:
105 error (_("Invalid size granularity."));
106 }
107 }
108
109 s = skip_spaces (s);
110 }
111
112 /* Get the search range. */
113
114 v = parse_to_comma_and_eval (&s);
115 start_addr = value_as_address (v);
116
117 if (*s == ',')
118 ++s;
119 s = skip_spaces (s);
120
121 if (*s == '+')
122 {
123 LONGEST len;
124
125 ++s;
126 v = parse_to_comma_and_eval (&s);
127 len = value_as_long (v);
128 if (len == 0)
129 {
130 printf_filtered (_("Empty search range.\n"));
131 return pattern_buf;
132 }
133 if (len < 0)
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;
140 }
141 else
142 {
143 CORE_ADDR end_addr;
144
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."));
156 }
157
158 if (*s == ',')
159 ++s;
160
161 /* Fetch the search string. */
162
163 while (*s != '\0')
164 {
165 LONGEST x;
166 struct type *t;
167
168 s = skip_spaces (s);
169
170 v = parse_to_comma_and_eval (&s);
171 t = value_type (v);
172
173 if (size != '\0')
174 {
175 x = value_as_long (v);
176 switch (size)
177 {
178 case 'b':
179 pattern_buf.push_back (x);
180 break;
181 case 'h':
182 put_bits (x, pattern_buf, 16, big_p);
183 break;
184 case 'w':
185 put_bits (x, pattern_buf, 32, big_p);
186 break;
187 case 'g':
188 put_bits (x, pattern_buf, 64, big_p);
189 break;
190 }
191 }
192 else
193 {
194 const gdb_byte *contents = value_contents (v);
195 pattern_buf.insert (pattern_buf.end (), contents,
196 contents + TYPE_LENGTH (t));
197 }
198
199 if (*s == ',')
200 ++s;
201 s = skip_spaces (s);
202 }
203
204 if (pattern_buf.empty ())
205 error (_("Missing search pattern."));
206
207 if (search_space_len < pattern_buf.size ())
208 error (_("Search space too small to contain pattern."));
209
210 *max_countp = max_count;
211 *start_addrp = start_addr;
212 *search_space_lenp = search_space_len;
213
214 return pattern_buf;
215 }
216
217 static void
218 find_command (const char *args, int from_tty)
219 {
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;
230
231 gdb::byte_vector pattern_buf = parse_find_args (args, &max_count,
232 &start_addr,
233 &search_space_len,
234 big_p);
235
236 /* Perform the search. */
237
238 found_count = 0;
239 last_found_addr = 0;
240
241 while (search_space_len >= pattern_buf.size ()
242 && found_count < max_count)
243 {
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,
248 pattern_buf.data (),
249 pattern_buf.size (),
250 &found_addr);
251
252 if (found <= 0)
253 break;
254
255 print_address (gdbarch, found_addr, gdb_stdout);
256 printf_filtered ("\n");
257 ++found_count;
258 last_found_addr = found_addr;
259
260 /* Begin next iteration at one byte past this match. */
261 next_iter_incr = (found_addr - start_addr) + 1;
262
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;
266 else
267 search_space_len = 0;
268 start_addr += next_iter_incr;
269 }
270
271 /* Record and print the results. */
272
273 set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
274 if (found_count > 0)
275 {
276 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
277
278 set_internalvar (lookup_internalvar ("_"),
279 value_from_pointer (ptr_type, last_found_addr));
280 }
281
282 if (found_count == 0)
283 printf_filtered ("Pattern not found.\n");
284 else
285 printf_filtered ("%d pattern%s found.\n", found_count,
286 found_count > 1 ? "s" : "");
287 }
288
289 void
290 _initialize_mem_search (void)
291 {
292 add_cmd ("find", class_vars, find_command, _("\
293 Search memory for a sequence of bytes.\n\
294 Usage:\nfind \
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\
305 \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."),
308 &cmdlist);
309 }
This page took 0.037487 seconds and 4 git commands to generate.