* configure.ac: Check for the GNU/Linux ptrace signature.
[deliverable/binutils-gdb.git] / gdb / findcmd.c
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)
75 error (_("Missing search parameters."));
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:
107 error (_("Invalid size granularity."));
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 {
133 printf_filtered (_("Empty search range.\n"));
134 return;
135 }
136 if (len < 0)
137 error (_("Invalid length."));
138 /* Watch for overflows. */
139 if (len > CORE_ADDR_MAX
140 || (start_addr + len - 1) < start_addr)
141 error (_("Search space too large."));
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)
150 error (_("Invalid search space, end preceeds start."));
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)
156 error (_("Overflow in address range computation, choose smaller range."));
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)
221 error (_("Missing search pattern."));
222
223 pattern_len = pattern_buf_end - pattern_buf;
224
225 if (search_space_len < pattern_len)
226 error (_("Search space too small to contain pattern."));
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"),
295 value_from_longest (builtin_type_int,
296 (LONGEST) found_count));
297 if (found_count > 0)
298 {
299 set_internalvar (lookup_internalvar ("_"),
300 value_from_pointer (builtin_type_void_data_ptr,
301 last_found_addr));
302 }
303
304 if (found_count == 0)
305 printf_filtered ("Pattern not found.\n");
306 else
307 printf_filtered ("%d pattern%s found.\n", found_count,
308 found_count > 1 ? "s" : "");
309
310 do_cleanups (old_cleanups);
311 }
312
313 void
314 _initialize_mem_search (void)
315 {
316 add_cmd ("find", class_vars, find_command, _("\
317 Search memory for a sequence of bytes.\n\
318 Usage:\n\
319 find [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
320 find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
321 size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
322 and if not specified the size is taken from the type of the expression\n\
323 in the current language.\n\
324 Note that this means for example that in the case of C-like languages\n\
325 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
326 which is typically four bytes.\n\
327 \n\
328 The address of the last match is stored as the value of \"$_\".\n\
329 Convenience variable \"$numfound\" is set to the number of matches."),
330 &cmdlist);
331 }
This page took 0.036748 seconds and 4 git commands to generate.