]>
Commit | Line | Data |
---|---|---|
08388c79 DE |
1 | /* The find command. |
2 | ||
3 | Copyright (C) 2008 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 | #include <ctype.h> | |
22 | #include "gdb_string.h" | |
23 | #include "gdbcmd.h" | |
24 | #include "value.h" | |
25 | #include "target.h" | |
26 | ||
27 | /* Copied from bfd_put_bits. */ | |
28 | ||
29 | static void | |
30 | put_bits (bfd_uint64_t data, char *buf, int bits, bfd_boolean big_p) | |
31 | { | |
32 | int i; | |
33 | int bytes; | |
34 | ||
35 | gdb_assert (bits % 8 == 0); | |
36 | ||
37 | bytes = bits / 8; | |
38 | for (i = 0; i < bytes; i++) | |
39 | { | |
40 | int index = big_p ? bytes - i - 1 : i; | |
41 | ||
42 | buf[index] = data & 0xff; | |
43 | data >>= 8; | |
44 | } | |
45 | } | |
46 | ||
47 | /* Subroutine of find_command to simplify it. | |
48 | Parse the arguments of the "find" command. */ | |
49 | ||
50 | static void | |
51 | parse_find_args (char *args, ULONGEST *max_countp, | |
52 | char **pattern_bufp, ULONGEST *pattern_lenp, | |
53 | CORE_ADDR *start_addrp, ULONGEST *search_space_lenp) | |
54 | { | |
55 | /* Default to using the specified type. */ | |
56 | char size = '\0'; | |
57 | ULONGEST max_count = ~(ULONGEST) 0; | |
58 | /* Buffer to hold the search pattern. */ | |
59 | char *pattern_buf; | |
60 | /* Current size of search pattern buffer. | |
61 | We realloc space as needed. */ | |
62 | #define INITIAL_PATTERN_BUF_SIZE 100 | |
63 | ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE; | |
64 | /* Pointer to one past the last in-use part of pattern_buf. */ | |
65 | char *pattern_buf_end; | |
66 | ULONGEST pattern_len; | |
67 | CORE_ADDR start_addr; | |
68 | ULONGEST search_space_len; | |
69 | char *s = args; | |
70 | bfd_boolean big_p = gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG; | |
71 | struct cleanup *old_cleanups; | |
72 | struct value *v; | |
73 | ||
74 | if (args == NULL) | |
5e1471f5 | 75 | error (_("Missing search parameters.")); |
08388c79 DE |
76 | |
77 | pattern_buf = xmalloc (pattern_buf_size); | |
78 | pattern_buf_end = pattern_buf; | |
79 | old_cleanups = make_cleanup (free_current_contents, &pattern_buf); | |
80 | ||
81 | /* Get search granularity and/or max count if specified. | |
82 | They may be specified in either order, together or separately. */ | |
83 | ||
84 | while (*s == '/') | |
85 | { | |
86 | ++s; | |
87 | ||
88 | while (*s != '\0' && *s != '/' && !isspace (*s)) | |
89 | { | |
90 | if (isdigit (*s)) | |
91 | { | |
92 | max_count = atoi (s); | |
93 | while (isdigit (*s)) | |
94 | ++s; | |
95 | continue; | |
96 | } | |
97 | ||
98 | switch (*s) | |
99 | { | |
100 | case 'b': | |
101 | case 'h': | |
102 | case 'w': | |
103 | case 'g': | |
104 | size = *s++; | |
105 | break; | |
106 | default: | |
5e1471f5 | 107 | error (_("Invalid size granularity.")); |
08388c79 DE |
108 | } |
109 | } | |
110 | ||
111 | while (isspace (*s)) | |
112 | ++s; | |
113 | } | |
114 | ||
115 | /* Get the search range. */ | |
116 | ||
117 | v = parse_to_comma_and_eval (&s); | |
118 | start_addr = value_as_address (v); | |
119 | ||
120 | if (*s == ',') | |
121 | ++s; | |
122 | while (isspace (*s)) | |
123 | ++s; | |
124 | ||
125 | if (*s == '+') | |
126 | { | |
127 | LONGEST len; | |
128 | ++s; | |
129 | v = parse_to_comma_and_eval (&s); | |
130 | len = value_as_long (v); | |
131 | if (len == 0) | |
132 | { | |
5e1471f5 | 133 | printf_filtered (_("Empty search range.\n")); |
08388c79 DE |
134 | return; |
135 | } | |
136 | if (len < 0) | |
5e1471f5 | 137 | error (_("Invalid length.")); |
08388c79 DE |
138 | /* Watch for overflows. */ |
139 | if (len > CORE_ADDR_MAX | |
140 | || (start_addr + len - 1) < start_addr) | |
5e1471f5 | 141 | error (_("Search space too large.")); |
08388c79 DE |
142 | search_space_len = len; |
143 | } | |
144 | else | |
145 | { | |
146 | CORE_ADDR end_addr; | |
147 | v = parse_to_comma_and_eval (&s); | |
148 | end_addr = value_as_address (v); | |
149 | if (start_addr > end_addr) | |
5e1471f5 | 150 | error (_("Invalid search space, end preceeds start.")); |
08388c79 DE |
151 | search_space_len = end_addr - start_addr + 1; |
152 | /* We don't support searching all of memory | |
153 | (i.e. start=0, end = 0xff..ff). | |
154 | Bail to avoid overflows later on. */ | |
155 | if (search_space_len == 0) | |
5e1471f5 | 156 | error (_("Overflow in address range computation, choose smaller range.")); |
08388c79 DE |
157 | } |
158 | ||
159 | if (*s == ',') | |
160 | ++s; | |
161 | ||
162 | /* Fetch the search string. */ | |
163 | ||
164 | while (*s != '\0') | |
165 | { | |
166 | LONGEST x; | |
167 | int val_bytes; | |
168 | ||
169 | while (isspace (*s)) | |
170 | ++s; | |
171 | ||
172 | v = parse_to_comma_and_eval (&s); | |
173 | val_bytes = TYPE_LENGTH (value_type (v)); | |
174 | ||
175 | /* Keep it simple and assume size == 'g' when watching for when we | |
176 | need to grow the pattern buf. */ | |
177 | if ((pattern_buf_end - pattern_buf + max (val_bytes, sizeof (int64_t))) | |
178 | > pattern_buf_size) | |
179 | { | |
180 | size_t current_offset = pattern_buf_end - pattern_buf; | |
181 | pattern_buf_size *= 2; | |
182 | pattern_buf = xrealloc (pattern_buf, pattern_buf_size); | |
183 | pattern_buf_end = pattern_buf + current_offset; | |
184 | } | |
185 | ||
186 | if (size != '\0') | |
187 | { | |
188 | x = value_as_long (v); | |
189 | switch (size) | |
190 | { | |
191 | case 'b': | |
192 | *pattern_buf_end++ = x; | |
193 | break; | |
194 | case 'h': | |
195 | put_bits (x, pattern_buf_end, 16, big_p); | |
196 | pattern_buf_end += sizeof (int16_t); | |
197 | break; | |
198 | case 'w': | |
199 | put_bits (x, pattern_buf_end, 32, big_p); | |
200 | pattern_buf_end += sizeof (int32_t); | |
201 | break; | |
202 | case 'g': | |
203 | put_bits (x, pattern_buf_end, 64, big_p); | |
204 | pattern_buf_end += sizeof (int64_t); | |
205 | break; | |
206 | } | |
207 | } | |
208 | else | |
209 | { | |
210 | memcpy (pattern_buf_end, value_contents_raw (v), val_bytes); | |
211 | pattern_buf_end += val_bytes; | |
212 | } | |
213 | ||
214 | if (*s == ',') | |
215 | ++s; | |
216 | while (isspace (*s)) | |
217 | ++s; | |
218 | } | |
219 | ||
220 | if (pattern_buf_end == pattern_buf) | |
5e1471f5 | 221 | error (_("Missing search pattern.")); |
08388c79 DE |
222 | |
223 | pattern_len = pattern_buf_end - pattern_buf; | |
224 | ||
225 | if (search_space_len < pattern_len) | |
5e1471f5 | 226 | error (_("Search space too small to contain pattern.")); |
08388c79 DE |
227 | |
228 | *max_countp = max_count; | |
229 | *pattern_bufp = pattern_buf; | |
230 | *pattern_lenp = pattern_len; | |
231 | *start_addrp = start_addr; | |
232 | *search_space_lenp = search_space_len; | |
233 | ||
234 | /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF | |
235 | to the caller now. */ | |
236 | discard_cleanups (old_cleanups); | |
237 | } | |
238 | ||
239 | static void | |
240 | find_command (char *args, int from_tty) | |
241 | { | |
242 | /* Command line parameters. | |
243 | These are initialized to avoid uninitialized warnings from -Wall. */ | |
244 | ULONGEST max_count = 0; | |
245 | char *pattern_buf = 0; | |
246 | ULONGEST pattern_len = 0; | |
247 | CORE_ADDR start_addr = 0; | |
248 | ULONGEST search_space_len = 0; | |
249 | /* End of command line parameters. */ | |
250 | unsigned int found_count; | |
251 | CORE_ADDR last_found_addr; | |
252 | struct cleanup *old_cleanups; | |
253 | ||
254 | parse_find_args (args, &max_count, &pattern_buf, &pattern_len, | |
255 | &start_addr, &search_space_len); | |
256 | ||
257 | old_cleanups = make_cleanup (free_current_contents, &pattern_buf); | |
258 | ||
259 | /* Perform the search. */ | |
260 | ||
261 | found_count = 0; | |
262 | last_found_addr = 0; | |
263 | ||
264 | while (search_space_len >= pattern_len | |
265 | && found_count < max_count) | |
266 | { | |
267 | /* Offset from start of this iteration to the next iteration. */ | |
268 | ULONGEST next_iter_incr; | |
269 | CORE_ADDR found_addr; | |
270 | int found = target_search_memory (start_addr, search_space_len, | |
271 | pattern_buf, pattern_len, &found_addr); | |
272 | ||
273 | if (found <= 0) | |
274 | break; | |
275 | ||
276 | print_address (found_addr, gdb_stdout); | |
277 | printf_filtered ("\n"); | |
278 | ++found_count; | |
279 | last_found_addr = found_addr; | |
280 | ||
281 | /* Begin next iteration at one byte past this match. */ | |
282 | next_iter_incr = (found_addr - start_addr) + 1; | |
283 | ||
284 | /* For robustness, we don't let search_space_len go -ve here. */ | |
285 | if (search_space_len >= next_iter_incr) | |
286 | search_space_len -= next_iter_incr; | |
287 | else | |
288 | search_space_len = 0; | |
289 | start_addr += next_iter_incr; | |
290 | } | |
291 | ||
292 | /* Record and print the results. */ | |
293 | ||
294 | set_internalvar (lookup_internalvar ("numfound"), | |
8b9b9e1a | 295 | value_from_longest (builtin_type_int32, |
08388c79 DE |
296 | (LONGEST) found_count)); |
297 | if (found_count > 0) | |
298 | { | |
8b9b9e1a UW |
299 | struct gdbarch *gdbarch = current_gdbarch; |
300 | struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr; | |
08388c79 | 301 | set_internalvar (lookup_internalvar ("_"), |
8b9b9e1a | 302 | value_from_pointer (ptr_type, last_found_addr)); |
08388c79 DE |
303 | } |
304 | ||
305 | if (found_count == 0) | |
5e1471f5 | 306 | printf_filtered ("Pattern not found.\n"); |
08388c79 | 307 | else |
5e1471f5 | 308 | printf_filtered ("%d pattern%s found.\n", found_count, |
08388c79 DE |
309 | found_count > 1 ? "s" : ""); |
310 | ||
311 | do_cleanups (old_cleanups); | |
312 | } | |
313 | ||
314 | void | |
315 | _initialize_mem_search (void) | |
316 | { | |
317 | add_cmd ("find", class_vars, find_command, _("\ | |
318 | Search memory for a sequence of bytes.\n\ | |
319 | Usage:\n\ | |
320 | find [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\ | |
321 | find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\ | |
322 | size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\ | |
323 | and if not specified the size is taken from the type of the expression\n\ | |
324 | in the current language.\n\ | |
325 | Note that this means for example that in the case of C-like languages\n\ | |
326 | a search for an untyped 0x42 will search for \"(int) 0x42\"\n\ | |
327 | which is typically four bytes.\n\ | |
328 | \n\ | |
329 | The address of the last match is stored as the value of \"$_\".\n\ | |
330 | Convenience variable \"$numfound\" is set to the number of matches."), | |
331 | &cmdlist); | |
332 | } |