GNU Binutils with patches for OS216
修訂 | d6b9b80f9483b6c1a3a018c0fcaf813ca098d8af (tree) |
---|---|
時間 | 2017-09-12 07:15:10 |
作者 | Tom Tromey <tom@trom...> |
Commiter | Tom Tromey |
Remove cleanups from findcmd.c
This removes cleanups from findcmd.c, replacing manual buffer
management with a gdb::byte_vector.
ChangeLog
2017-09-11 Tom Tromey <tom@tromey.com>
* findcmd.c (put_bits): Take a gdb::byte_vector.
(parse_find_args): Return gdb::byte_vector. "args" now const.
Remove "pattern_bufp" and "pattern_lenp" parameters. Remove
cleanups.
(find_command): Update.
@@ -1,5 +1,13 @@ | ||
1 | 1 | 2017-09-11 Tom Tromey <tom@tromey.com> |
2 | 2 | |
3 | + * findcmd.c (put_bits): Take a gdb::byte_vector. | |
4 | + (parse_find_args): Return gdb::byte_vector. "args" now const. | |
5 | + Remove "pattern_bufp" and "pattern_lenp" parameters. Remove | |
6 | + cleanups. | |
7 | + (find_command): Update. | |
8 | + | |
9 | +2017-09-11 Tom Tromey <tom@tromey.com> | |
10 | + | |
3 | 11 | * cli/cli-script.c (class scoped_restore_hook_in): New. |
4 | 12 | (clear_hook_in_cleanup): Remove. |
5 | 13 | (execute_cmd_pre_hook, execute_cmd_post_hook): Use |
@@ -25,11 +25,12 @@ | ||
25 | 25 | #include "target.h" |
26 | 26 | #include "cli/cli-utils.h" |
27 | 27 | #include <algorithm> |
28 | +#include "common/byte-vector.h" | |
28 | 29 | |
29 | 30 | /* Copied from bfd_put_bits. */ |
30 | 31 | |
31 | 32 | static void |
32 | -put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p) | |
33 | +put_bits (bfd_uint64_t data, gdb::byte_vector &buf, int bits, bfd_boolean big_p) | |
33 | 34 | { |
34 | 35 | int i; |
35 | 36 | int bytes; |
@@ -37,11 +38,13 @@ put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p) | ||
37 | 38 | gdb_assert (bits % 8 == 0); |
38 | 39 | |
39 | 40 | bytes = bits / 8; |
41 | + size_t last = buf.size (); | |
42 | + buf.resize (last + bytes); | |
40 | 43 | for (i = 0; i < bytes; i++) |
41 | 44 | { |
42 | 45 | int index = big_p ? bytes - i - 1 : i; |
43 | 46 | |
44 | - buf[index] = data & 0xff; | |
47 | + buf[last + index] = data & 0xff; | |
45 | 48 | data >>= 8; |
46 | 49 | } |
47 | 50 | } |
@@ -49,9 +52,8 @@ put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p) | ||
49 | 52 | /* Subroutine of find_command to simplify it. |
50 | 53 | Parse the arguments of the "find" command. */ |
51 | 54 | |
52 | -static void | |
53 | -parse_find_args (char *args, ULONGEST *max_countp, | |
54 | - gdb_byte **pattern_bufp, ULONGEST *pattern_lenp, | |
55 | +static gdb::byte_vector | |
56 | +parse_find_args (const char *args, ULONGEST *max_countp, | |
55 | 57 | CORE_ADDR *start_addrp, ULONGEST *search_space_lenp, |
56 | 58 | bfd_boolean big_p) |
57 | 59 | { |
@@ -59,27 +61,15 @@ parse_find_args (char *args, ULONGEST *max_countp, | ||
59 | 61 | char size = '\0'; |
60 | 62 | ULONGEST max_count = ~(ULONGEST) 0; |
61 | 63 | /* Buffer to hold the search pattern. */ |
62 | - gdb_byte *pattern_buf; | |
63 | - /* Current size of search pattern buffer. | |
64 | - We realloc space as needed. */ | |
65 | -#define INITIAL_PATTERN_BUF_SIZE 100 | |
66 | - ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE; | |
67 | - /* Pointer to one past the last in-use part of pattern_buf. */ | |
68 | - gdb_byte *pattern_buf_end; | |
69 | - ULONGEST pattern_len; | |
64 | + gdb::byte_vector pattern_buf; | |
70 | 65 | CORE_ADDR start_addr; |
71 | 66 | ULONGEST search_space_len; |
72 | 67 | const char *s = args; |
73 | - struct cleanup *old_cleanups; | |
74 | 68 | struct value *v; |
75 | 69 | |
76 | 70 | if (args == NULL) |
77 | 71 | error (_("Missing search parameters.")); |
78 | 72 | |
79 | - pattern_buf = (gdb_byte *) xmalloc (pattern_buf_size); | |
80 | - pattern_buf_end = pattern_buf; | |
81 | - old_cleanups = make_cleanup (free_current_contents, &pattern_buf); | |
82 | - | |
83 | 73 | /* Get search granularity and/or max count if specified. |
84 | 74 | They may be specified in either order, together or separately. */ |
85 | 75 |
@@ -131,9 +121,8 @@ parse_find_args (char *args, ULONGEST *max_countp, | ||
131 | 121 | len = value_as_long (v); |
132 | 122 | if (len == 0) |
133 | 123 | { |
134 | - do_cleanups (old_cleanups); | |
135 | 124 | printf_filtered (_("Empty search range.\n")); |
136 | - return; | |
125 | + return pattern_buf; | |
137 | 126 | } |
138 | 127 | if (len < 0) |
139 | 128 | error (_("Invalid length.")); |
@@ -169,53 +158,36 @@ parse_find_args (char *args, ULONGEST *max_countp, | ||
169 | 158 | { |
170 | 159 | LONGEST x; |
171 | 160 | struct type *t; |
172 | - ULONGEST pattern_buf_size_need; | |
173 | 161 | |
174 | 162 | s = skip_spaces (s); |
175 | 163 | |
176 | 164 | v = parse_to_comma_and_eval (&s); |
177 | 165 | t = value_type (v); |
178 | 166 | |
179 | - /* Keep it simple and assume size == 'g' when watching for when we | |
180 | - need to grow the pattern buf. */ | |
181 | - pattern_buf_size_need = (pattern_buf_end - pattern_buf | |
182 | - + std::max (TYPE_LENGTH (t), | |
183 | - (unsigned) sizeof (int64_t))); | |
184 | - if (pattern_buf_size_need > pattern_buf_size) | |
185 | - { | |
186 | - size_t current_offset = pattern_buf_end - pattern_buf; | |
187 | - | |
188 | - pattern_buf_size = pattern_buf_size_need * 2; | |
189 | - pattern_buf = (gdb_byte *) xrealloc (pattern_buf, pattern_buf_size); | |
190 | - pattern_buf_end = pattern_buf + current_offset; | |
191 | - } | |
192 | - | |
193 | 167 | if (size != '\0') |
194 | 168 | { |
195 | 169 | x = value_as_long (v); |
196 | 170 | switch (size) |
197 | 171 | { |
198 | 172 | case 'b': |
199 | - *pattern_buf_end++ = x; | |
173 | + pattern_buf.push_back (x); | |
200 | 174 | break; |
201 | 175 | case 'h': |
202 | - put_bits (x, pattern_buf_end, 16, big_p); | |
203 | - pattern_buf_end += sizeof (int16_t); | |
176 | + put_bits (x, pattern_buf, 16, big_p); | |
204 | 177 | break; |
205 | 178 | case 'w': |
206 | - put_bits (x, pattern_buf_end, 32, big_p); | |
207 | - pattern_buf_end += sizeof (int32_t); | |
179 | + put_bits (x, pattern_buf, 32, big_p); | |
208 | 180 | break; |
209 | 181 | case 'g': |
210 | - put_bits (x, pattern_buf_end, 64, big_p); | |
211 | - pattern_buf_end += sizeof (int64_t); | |
182 | + put_bits (x, pattern_buf, 64, big_p); | |
212 | 183 | break; |
213 | 184 | } |
214 | 185 | } |
215 | 186 | else |
216 | 187 | { |
217 | - memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t)); | |
218 | - pattern_buf_end += TYPE_LENGTH (t); | |
188 | + const gdb_byte *contents = value_contents (v); | |
189 | + pattern_buf.insert (pattern_buf.end (), contents, | |
190 | + contents + TYPE_LENGTH (t)); | |
219 | 191 | } |
220 | 192 | |
221 | 193 | if (*s == ',') |
@@ -223,23 +195,17 @@ parse_find_args (char *args, ULONGEST *max_countp, | ||
223 | 195 | s = skip_spaces (s); |
224 | 196 | } |
225 | 197 | |
226 | - if (pattern_buf_end == pattern_buf) | |
198 | + if (pattern_buf.empty ()) | |
227 | 199 | error (_("Missing search pattern.")); |
228 | 200 | |
229 | - pattern_len = pattern_buf_end - pattern_buf; | |
230 | - | |
231 | - if (search_space_len < pattern_len) | |
201 | + if (search_space_len < pattern_buf.size ()) | |
232 | 202 | error (_("Search space too small to contain pattern.")); |
233 | 203 | |
234 | 204 | *max_countp = max_count; |
235 | - *pattern_bufp = pattern_buf; | |
236 | - *pattern_lenp = pattern_len; | |
237 | 205 | *start_addrp = start_addr; |
238 | 206 | *search_space_lenp = search_space_len; |
239 | 207 | |
240 | - /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF | |
241 | - to the caller now. */ | |
242 | - discard_cleanups (old_cleanups); | |
208 | + return pattern_buf; | |
243 | 209 | } |
244 | 210 | |
245 | 211 | static void |
@@ -250,33 +216,32 @@ find_command (char *args, int from_tty) | ||
250 | 216 | /* Command line parameters. |
251 | 217 | These are initialized to avoid uninitialized warnings from -Wall. */ |
252 | 218 | ULONGEST max_count = 0; |
253 | - gdb_byte *pattern_buf = 0; | |
254 | - ULONGEST pattern_len = 0; | |
255 | 219 | CORE_ADDR start_addr = 0; |
256 | 220 | ULONGEST search_space_len = 0; |
257 | 221 | /* End of command line parameters. */ |
258 | 222 | unsigned int found_count; |
259 | 223 | CORE_ADDR last_found_addr; |
260 | - struct cleanup *old_cleanups; | |
261 | 224 | |
262 | - parse_find_args (args, &max_count, &pattern_buf, &pattern_len, | |
263 | - &start_addr, &search_space_len, big_p); | |
264 | - | |
265 | - old_cleanups = make_cleanup (free_current_contents, &pattern_buf); | |
225 | + gdb::byte_vector pattern_buf = parse_find_args (args, &max_count, | |
226 | + &start_addr, | |
227 | + &search_space_len, | |
228 | + big_p); | |
266 | 229 | |
267 | 230 | /* Perform the search. */ |
268 | 231 | |
269 | 232 | found_count = 0; |
270 | 233 | last_found_addr = 0; |
271 | 234 | |
272 | - while (search_space_len >= pattern_len | |
235 | + while (search_space_len >= pattern_buf.size () | |
273 | 236 | && found_count < max_count) |
274 | 237 | { |
275 | 238 | /* Offset from start of this iteration to the next iteration. */ |
276 | 239 | ULONGEST next_iter_incr; |
277 | 240 | CORE_ADDR found_addr; |
278 | 241 | int found = target_search_memory (start_addr, search_space_len, |
279 | - pattern_buf, pattern_len, &found_addr); | |
242 | + pattern_buf.data (), | |
243 | + pattern_buf.size (), | |
244 | + &found_addr); | |
280 | 245 | |
281 | 246 | if (found <= 0) |
282 | 247 | break; |
@@ -313,8 +278,6 @@ find_command (char *args, int from_tty) | ||
313 | 278 | else |
314 | 279 | printf_filtered ("%d pattern%s found.\n", found_count, |
315 | 280 | found_count > 1 ? "s" : ""); |
316 | - | |
317 | - do_cleanups (old_cleanups); | |
318 | 281 | } |
319 | 282 | |
320 | 283 | void |