| OLD | NEW |
| 1 /* The find command. | 1 /* The find command. |
| 2 | 2 |
| 3 Copyright (C) 2008-2012 Free Software Foundation, Inc. | 3 Copyright (C) 2008-2013 Free Software Foundation, Inc. |
| 4 | 4 |
| 5 This file is part of GDB. | 5 This file is part of GDB. |
| 6 | 6 |
| 7 This program is free software; you can redistribute it and/or modify | 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 | 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 | 9 the Free Software Foundation; either version 3 of the License, or |
| 10 (at your option) any later version. | 10 (at your option) any later version. |
| 11 | 11 |
| 12 This program is distributed in the hope that it will be useful, | 12 This program is distributed in the hope that it will be useful, |
| 13 but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 GNU General Public License for more details. | 15 GNU General Public License for more details. |
| 16 | 16 |
| 17 You should have received a copy of the GNU General Public License | 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/>. */ | 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | 19 |
| 20 #include "defs.h" | 20 #include "defs.h" |
| 21 #include "arch-utils.h" | 21 #include "arch-utils.h" |
| 22 #include <ctype.h> | 22 #include <ctype.h> |
| 23 #include "gdb_string.h" | 23 #include <string.h> |
| 24 #include "gdbcmd.h" | 24 #include "gdbcmd.h" |
| 25 #include "value.h" | 25 #include "value.h" |
| 26 #include "target.h" | 26 #include "target.h" |
| 27 #include "cli/cli-utils.h" |
| 27 | 28 |
| 28 /* Copied from bfd_put_bits. */ | 29 /* Copied from bfd_put_bits. */ |
| 29 | 30 |
| 30 static void | 31 static void |
| 31 put_bits (bfd_uint64_t data, char *buf, int bits, bfd_boolean big_p) | 32 put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p) |
| 32 { | 33 { |
| 33 int i; | 34 int i; |
| 34 int bytes; | 35 int bytes; |
| 35 | 36 |
| 36 gdb_assert (bits % 8 == 0); | 37 gdb_assert (bits % 8 == 0); |
| 37 | 38 |
| 38 bytes = bits / 8; | 39 bytes = bits / 8; |
| 39 for (i = 0; i < bytes; i++) | 40 for (i = 0; i < bytes; i++) |
| 40 { | 41 { |
| 41 int index = big_p ? bytes - i - 1 : i; | 42 int index = big_p ? bytes - i - 1 : i; |
| 42 | 43 |
| 43 buf[index] = data & 0xff; | 44 buf[index] = data & 0xff; |
| 44 data >>= 8; | 45 data >>= 8; |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 | 48 |
| 48 /* Subroutine of find_command to simplify it. | 49 /* Subroutine of find_command to simplify it. |
| 49 Parse the arguments of the "find" command. */ | 50 Parse the arguments of the "find" command. */ |
| 50 | 51 |
| 51 static void | 52 static void |
| 52 parse_find_args (char *args, ULONGEST *max_countp, | 53 parse_find_args (char *args, ULONGEST *max_countp, |
| 53 » » char **pattern_bufp, ULONGEST *pattern_lenp, | 54 » » gdb_byte **pattern_bufp, ULONGEST *pattern_lenp, |
| 54 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp, | 55 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp, |
| 55 bfd_boolean big_p) | 56 bfd_boolean big_p) |
| 56 { | 57 { |
| 57 /* Default to using the specified type. */ | 58 /* Default to using the specified type. */ |
| 58 char size = '\0'; | 59 char size = '\0'; |
| 59 ULONGEST max_count = ~(ULONGEST) 0; | 60 ULONGEST max_count = ~(ULONGEST) 0; |
| 60 /* Buffer to hold the search pattern. */ | 61 /* Buffer to hold the search pattern. */ |
| 61 char *pattern_buf; | 62 gdb_byte *pattern_buf; |
| 62 /* Current size of search pattern buffer. | 63 /* Current size of search pattern buffer. |
| 63 We realloc space as needed. */ | 64 We realloc space as needed. */ |
| 64 #define INITIAL_PATTERN_BUF_SIZE 100 | 65 #define INITIAL_PATTERN_BUF_SIZE 100 |
| 65 ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE; | 66 ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE; |
| 66 /* Pointer to one past the last in-use part of pattern_buf. */ | 67 /* Pointer to one past the last in-use part of pattern_buf. */ |
| 67 char *pattern_buf_end; | 68 gdb_byte *pattern_buf_end; |
| 68 ULONGEST pattern_len; | 69 ULONGEST pattern_len; |
| 69 CORE_ADDR start_addr; | 70 CORE_ADDR start_addr; |
| 70 ULONGEST search_space_len; | 71 ULONGEST search_space_len; |
| 71 char *s = args; | 72 const char *s = args; |
| 72 struct cleanup *old_cleanups; | 73 struct cleanup *old_cleanups; |
| 73 struct value *v; | 74 struct value *v; |
| 74 | 75 |
| 75 if (args == NULL) | 76 if (args == NULL) |
| 76 error (_("Missing search parameters.")); | 77 error (_("Missing search parameters.")); |
| 77 | 78 |
| 78 pattern_buf = xmalloc (pattern_buf_size); | 79 pattern_buf = xmalloc (pattern_buf_size); |
| 79 pattern_buf_end = pattern_buf; | 80 pattern_buf_end = pattern_buf; |
| 80 old_cleanups = make_cleanup (free_current_contents, &pattern_buf); | 81 old_cleanups = make_cleanup (free_current_contents, &pattern_buf); |
| 81 | 82 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 102 case 'h': | 103 case 'h': |
| 103 case 'w': | 104 case 'w': |
| 104 case 'g': | 105 case 'g': |
| 105 size = *s++; | 106 size = *s++; |
| 106 break; | 107 break; |
| 107 default: | 108 default: |
| 108 error (_("Invalid size granularity.")); | 109 error (_("Invalid size granularity.")); |
| 109 } | 110 } |
| 110 } | 111 } |
| 111 | 112 |
| 112 while (isspace (*s)) | 113 s = skip_spaces_const (s); |
| 113 » ++s; | |
| 114 } | 114 } |
| 115 | 115 |
| 116 /* Get the search range. */ | 116 /* Get the search range. */ |
| 117 | 117 |
| 118 v = parse_to_comma_and_eval (&s); | 118 v = parse_to_comma_and_eval (&s); |
| 119 start_addr = value_as_address (v); | 119 start_addr = value_as_address (v); |
| 120 | 120 |
| 121 if (*s == ',') | 121 if (*s == ',') |
| 122 ++s; | 122 ++s; |
| 123 while (isspace (*s)) | 123 s = skip_spaces_const (s); |
| 124 ++s; | |
| 125 | 124 |
| 126 if (*s == '+') | 125 if (*s == '+') |
| 127 { | 126 { |
| 128 LONGEST len; | 127 LONGEST len; |
| 129 | 128 |
| 130 ++s; | 129 ++s; |
| 131 v = parse_to_comma_and_eval (&s); | 130 v = parse_to_comma_and_eval (&s); |
| 132 len = value_as_long (v); | 131 len = value_as_long (v); |
| 133 if (len == 0) | 132 if (len == 0) |
| 134 { | 133 { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 162 } | 161 } |
| 163 | 162 |
| 164 if (*s == ',') | 163 if (*s == ',') |
| 165 ++s; | 164 ++s; |
| 166 | 165 |
| 167 /* Fetch the search string. */ | 166 /* Fetch the search string. */ |
| 168 | 167 |
| 169 while (*s != '\0') | 168 while (*s != '\0') |
| 170 { | 169 { |
| 171 LONGEST x; | 170 LONGEST x; |
| 172 int val_bytes; | 171 struct type *t; |
| 173 ULONGEST pattern_buf_size_need; | 172 ULONGEST pattern_buf_size_need; |
| 174 | 173 |
| 175 while (isspace (*s)) | 174 s = skip_spaces_const (s); |
| 176 » ++s; | |
| 177 | 175 |
| 178 v = parse_to_comma_and_eval (&s); | 176 v = parse_to_comma_and_eval (&s); |
| 179 val_bytes = TYPE_LENGTH (value_type (v)); | 177 t = value_type (v); |
| 180 | 178 |
| 181 /* Keep it simple and assume size == 'g' when watching for when we | 179 /* Keep it simple and assume size == 'g' when watching for when we |
| 182 need to grow the pattern buf. */ | 180 need to grow the pattern buf. */ |
| 183 pattern_buf_size_need = (pattern_buf_end - pattern_buf | 181 pattern_buf_size_need = (pattern_buf_end - pattern_buf |
| 184 » » » + max (val_bytes, sizeof (int64_t))); | 182 » » » + max (TYPE_LENGTH (t), sizeof (int64_t))); |
| 185 if (pattern_buf_size_need > pattern_buf_size) | 183 if (pattern_buf_size_need > pattern_buf_size) |
| 186 { | 184 { |
| 187 size_t current_offset = pattern_buf_end - pattern_buf; | 185 size_t current_offset = pattern_buf_end - pattern_buf; |
| 188 | 186 |
| 189 pattern_buf_size = pattern_buf_size_need * 2; | 187 pattern_buf_size = pattern_buf_size_need * 2; |
| 190 pattern_buf = xrealloc (pattern_buf, pattern_buf_size); | 188 pattern_buf = xrealloc (pattern_buf, pattern_buf_size); |
| 191 pattern_buf_end = pattern_buf + current_offset; | 189 pattern_buf_end = pattern_buf + current_offset; |
| 192 } | 190 } |
| 193 | 191 |
| 194 if (size != '\0') | 192 if (size != '\0') |
| (...skipping 13 matching lines...) Expand all Loading... |
| 208 pattern_buf_end += sizeof (int32_t); | 206 pattern_buf_end += sizeof (int32_t); |
| 209 break; | 207 break; |
| 210 case 'g': | 208 case 'g': |
| 211 put_bits (x, pattern_buf_end, 64, big_p); | 209 put_bits (x, pattern_buf_end, 64, big_p); |
| 212 pattern_buf_end += sizeof (int64_t); | 210 pattern_buf_end += sizeof (int64_t); |
| 213 break; | 211 break; |
| 214 } | 212 } |
| 215 } | 213 } |
| 216 else | 214 else |
| 217 { | 215 { |
| 218 » memcpy (pattern_buf_end, value_contents (v), val_bytes); | 216 » memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t)); |
| 219 » pattern_buf_end += val_bytes; | 217 » pattern_buf_end += TYPE_LENGTH (t); |
| 220 } | 218 } |
| 221 | 219 |
| 222 if (*s == ',') | 220 if (*s == ',') |
| 223 ++s; | 221 ++s; |
| 224 while (isspace (*s)) | 222 s = skip_spaces_const (s); |
| 225 » ++s; | |
| 226 } | 223 } |
| 227 | 224 |
| 228 if (pattern_buf_end == pattern_buf) | 225 if (pattern_buf_end == pattern_buf) |
| 229 error (_("Missing search pattern.")); | 226 error (_("Missing search pattern.")); |
| 230 | 227 |
| 231 pattern_len = pattern_buf_end - pattern_buf; | 228 pattern_len = pattern_buf_end - pattern_buf; |
| 232 | 229 |
| 233 if (search_space_len < pattern_len) | 230 if (search_space_len < pattern_len) |
| 234 error (_("Search space too small to contain pattern.")); | 231 error (_("Search space too small to contain pattern.")); |
| 235 | 232 |
| 236 *max_countp = max_count; | 233 *max_countp = max_count; |
| 237 *pattern_bufp = pattern_buf; | 234 *pattern_bufp = pattern_buf; |
| 238 *pattern_lenp = pattern_len; | 235 *pattern_lenp = pattern_len; |
| 239 *start_addrp = start_addr; | 236 *start_addrp = start_addr; |
| 240 *search_space_lenp = search_space_len; | 237 *search_space_lenp = search_space_len; |
| 241 | 238 |
| 242 /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF | 239 /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF |
| 243 to the caller now. */ | 240 to the caller now. */ |
| 244 discard_cleanups (old_cleanups); | 241 discard_cleanups (old_cleanups); |
| 245 } | 242 } |
| 246 | 243 |
| 247 static void | 244 static void |
| 248 find_command (char *args, int from_tty) | 245 find_command (char *args, int from_tty) |
| 249 { | 246 { |
| 250 struct gdbarch *gdbarch = get_current_arch (); | 247 struct gdbarch *gdbarch = get_current_arch (); |
| 251 bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG; | 248 bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG; |
| 252 /* Command line parameters. | 249 /* Command line parameters. |
| 253 These are initialized to avoid uninitialized warnings from -Wall. */ | 250 These are initialized to avoid uninitialized warnings from -Wall. */ |
| 254 ULONGEST max_count = 0; | 251 ULONGEST max_count = 0; |
| 255 char *pattern_buf = 0; | 252 gdb_byte *pattern_buf = 0; |
| 256 ULONGEST pattern_len = 0; | 253 ULONGEST pattern_len = 0; |
| 257 CORE_ADDR start_addr = 0; | 254 CORE_ADDR start_addr = 0; |
| 258 ULONGEST search_space_len = 0; | 255 ULONGEST search_space_len = 0; |
| 259 /* End of command line parameters. */ | 256 /* End of command line parameters. */ |
| 260 unsigned int found_count; | 257 unsigned int found_count; |
| 261 CORE_ADDR last_found_addr; | 258 CORE_ADDR last_found_addr; |
| 262 struct cleanup *old_cleanups; | 259 struct cleanup *old_cleanups; |
| 263 | 260 |
| 264 parse_find_args (args, &max_count, &pattern_buf, &pattern_len, | 261 parse_find_args (args, &max_count, &pattern_buf, &pattern_len, |
| 265 &start_addr, &search_space_len, big_p); | 262 &start_addr, &search_space_len, big_p); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 and if not specified the size is taken from the type of the expression\n\ | 331 and if not specified the size is taken from the type of the expression\n\ |
| 335 in the current language.\n\ | 332 in the current language.\n\ |
| 336 Note that this means for example that in the case of C-like languages\n\ | 333 Note that this means for example that in the case of C-like languages\n\ |
| 337 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\ | 334 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\ |
| 338 which is typically four bytes.\n\ | 335 which is typically four bytes.\n\ |
| 339 \n\ | 336 \n\ |
| 340 The address of the last match is stored as the value of \"$_\".\n\ | 337 The address of the last match is stored as the value of \"$_\".\n\ |
| 341 Convenience variable \"$numfound\" is set to the number of matches."), | 338 Convenience variable \"$numfound\" is set to the number of matches."), |
| 342 &cmdlist); | 339 &cmdlist); |
| 343 } | 340 } |
| OLD | NEW |