Commit | Line | Data |
---|---|---|
f02df580 MS |
1 | /* Dump-to-file commands, for GDB, the GNU debugger. |
2 | ||
61baf725 | 3 | Copyright (C) 2002-2017 Free Software Foundation, Inc. |
f02df580 MS |
4 | |
5 | Contributed by Red Hat. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
f02df580 MS |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
f02df580 MS |
21 | |
22 | #include "defs.h" | |
f02df580 MS |
23 | #include "cli/cli-decode.h" |
24 | #include "cli/cli-cmds.h" | |
25 | #include "value.h" | |
26 | #include "completer.h" | |
f02df580 MS |
27 | #include <ctype.h> |
28 | #include "target.h" | |
dbda9972 | 29 | #include "readline/readline.h" |
c0ac0ec7 | 30 | #include "gdbcore.h" |
e9cafbcc | 31 | #include "cli/cli-utils.h" |
cbb099e8 | 32 | #include "gdb_bfd.h" |
614c279d | 33 | #include "filestuff.h" |
d5722aa2 | 34 | #include "common/byte-vector.h" |
f02df580 | 35 | |
93db0d79 TT |
36 | static const char * |
37 | scan_expression_with_cleanup (const char **cmd, const char *def) | |
f02df580 MS |
38 | { |
39 | if ((*cmd) == NULL || (**cmd) == '\0') | |
40 | { | |
41 | char *exp = xstrdup (def); | |
cdb27c12 | 42 | |
f02df580 MS |
43 | make_cleanup (xfree, exp); |
44 | return exp; | |
45 | } | |
46 | else | |
47 | { | |
48 | char *exp; | |
93db0d79 | 49 | const char *end; |
f02df580 MS |
50 | |
51 | end = (*cmd) + strcspn (*cmd, " \t"); | |
52 | exp = savestring ((*cmd), end - (*cmd)); | |
53 | make_cleanup (xfree, exp); | |
93db0d79 | 54 | (*cmd) = skip_spaces_const (end); |
f02df580 MS |
55 | return exp; |
56 | } | |
57 | } | |
58 | ||
59 | ||
db229724 | 60 | static char * |
93db0d79 | 61 | scan_filename_with_cleanup (const char **cmd, const char *defname) |
f02df580 MS |
62 | { |
63 | char *filename; | |
64 | char *fullname; | |
65 | ||
66 | /* FIXME: Need to get the ``/a(ppend)'' flag from somewhere. */ | |
67 | ||
68 | /* File. */ | |
69 | if ((*cmd) == NULL) | |
70 | { | |
71 | if (defname == NULL) | |
8a3fe4f8 | 72 | error (_("Missing filename.")); |
f02df580 MS |
73 | filename = xstrdup (defname); |
74 | make_cleanup (xfree, filename); | |
75 | } | |
76 | else | |
77 | { | |
78 | /* FIXME: should parse a possibly quoted string. */ | |
93db0d79 | 79 | const char *end; |
f02df580 | 80 | |
93db0d79 | 81 | (*cmd) = skip_spaces_const (*cmd); |
f02df580 MS |
82 | end = *cmd + strcspn (*cmd, " \t"); |
83 | filename = savestring ((*cmd), end - (*cmd)); | |
84 | make_cleanup (xfree, filename); | |
93db0d79 | 85 | (*cmd) = skip_spaces_const (end); |
f02df580 MS |
86 | } |
87 | gdb_assert (filename != NULL); | |
88 | ||
89 | fullname = tilde_expand (filename); | |
90 | make_cleanup (xfree, fullname); | |
91 | ||
92 | return fullname; | |
93 | } | |
94 | ||
192b62ce TT |
95 | static gdb_bfd_ref_ptr |
96 | bfd_openr_or_error (const char *filename, const char *target) | |
f02df580 | 97 | { |
192b62ce | 98 | gdb_bfd_ref_ptr ibfd (gdb_bfd_openr (filename, target)); |
5cb316ef | 99 | if (ibfd == NULL) |
192b62ce | 100 | error (_("Failed to open %s: %s."), filename, |
f02df580 MS |
101 | bfd_errmsg (bfd_get_error ())); |
102 | ||
192b62ce | 103 | if (!bfd_check_format (ibfd.get (), bfd_object)) |
8a3fe4f8 | 104 | error (_("'%s' is not a recognized file format."), filename); |
f02df580 MS |
105 | |
106 | return ibfd; | |
107 | } | |
108 | ||
192b62ce TT |
109 | static gdb_bfd_ref_ptr |
110 | bfd_openw_or_error (const char *filename, const char *target, const char *mode) | |
f02df580 | 111 | { |
192b62ce | 112 | gdb_bfd_ref_ptr obfd; |
f02df580 MS |
113 | |
114 | if (*mode == 'w') /* Write: create new file */ | |
115 | { | |
64c31149 | 116 | obfd = gdb_bfd_openw (filename, target); |
5cb316ef | 117 | if (obfd == NULL) |
192b62ce | 118 | error (_("Failed to open %s: %s."), filename, |
f02df580 | 119 | bfd_errmsg (bfd_get_error ())); |
192b62ce TT |
120 | if (!bfd_set_format (obfd.get (), bfd_object)) |
121 | error (_("bfd_openw_or_error: %s."), bfd_errmsg (bfd_get_error ())); | |
f02df580 | 122 | } |
ebcd3b23 MS |
123 | else if (*mode == 'a') /* Append to existing file. */ |
124 | { /* FIXME -- doesn't work... */ | |
8a3fe4f8 | 125 | error (_("bfd_openw does not work with append.")); |
f02df580 MS |
126 | } |
127 | else | |
192b62ce | 128 | error (_("bfd_openw_or_error: unknown mode %s."), mode); |
f02df580 MS |
129 | |
130 | return obfd; | |
131 | } | |
132 | ||
28578e6b YQ |
133 | static struct cmd_list_element *dump_cmdlist; |
134 | static struct cmd_list_element *append_cmdlist; | |
135 | static struct cmd_list_element *srec_cmdlist; | |
136 | static struct cmd_list_element *ihex_cmdlist; | |
cf75d6c3 | 137 | static struct cmd_list_element *verilog_cmdlist; |
28578e6b YQ |
138 | static struct cmd_list_element *tekhex_cmdlist; |
139 | static struct cmd_list_element *binary_dump_cmdlist; | |
140 | static struct cmd_list_element *binary_append_cmdlist; | |
f02df580 MS |
141 | |
142 | static void | |
143 | dump_command (char *cmd, int from_tty) | |
144 | { | |
a3f17187 | 145 | printf_unfiltered (_("\"dump\" must be followed by a subcommand.\n\n")); |
635c7e8a | 146 | help_list (dump_cmdlist, "dump ", all_commands, gdb_stdout); |
f02df580 MS |
147 | } |
148 | ||
149 | static void | |
150 | append_command (char *cmd, int from_tty) | |
151 | { | |
a3f17187 | 152 | printf_unfiltered (_("\"append\" must be followed by a subcommand.\n\n")); |
635c7e8a | 153 | help_list (dump_cmdlist, "append ", all_commands, gdb_stdout); |
f02df580 MS |
154 | } |
155 | ||
156 | static void | |
c26b8e3b | 157 | dump_binary_file (const char *filename, const char *mode, |
5005c8a9 | 158 | const bfd_byte *buf, ULONGEST len) |
f02df580 | 159 | { |
f02df580 MS |
160 | int status; |
161 | ||
d419f42d TT |
162 | gdb_file_up file = gdb_fopen_cloexec (filename, mode); |
163 | status = fwrite (buf, len, 1, file.get ()); | |
f02df580 MS |
164 | if (status != 1) |
165 | perror_with_name (filename); | |
166 | } | |
167 | ||
168 | static void | |
c26b8e3b AC |
169 | dump_bfd_file (const char *filename, const char *mode, |
170 | const char *target, CORE_ADDR vaddr, | |
5005c8a9 | 171 | const bfd_byte *buf, ULONGEST len) |
f02df580 | 172 | { |
f02df580 MS |
173 | asection *osection; |
174 | ||
192b62ce TT |
175 | gdb_bfd_ref_ptr obfd (bfd_openw_or_error (filename, target, mode)); |
176 | osection = bfd_make_section_anyway (obfd.get (), ".newsec"); | |
177 | bfd_set_section_size (obfd.get (), osection, len); | |
178 | bfd_set_section_vma (obfd.get (), osection, vaddr); | |
179 | bfd_set_section_alignment (obfd.get (), osection, 0); | |
180 | bfd_set_section_flags (obfd.get (), osection, (SEC_HAS_CONTENTS | |
181 | | SEC_ALLOC | |
182 | | SEC_LOAD)); | |
f02df580 | 183 | osection->entsize = 0; |
192b62ce TT |
184 | if (!bfd_set_section_contents (obfd.get (), osection, buf, 0, len)) |
185 | warning (_("writing dump file '%s' (%s)"), filename, | |
53624a93 | 186 | bfd_errmsg (bfd_get_error ())); |
f02df580 MS |
187 | } |
188 | ||
189 | static void | |
93db0d79 | 190 | dump_memory_to_file (const char *cmd, const char *mode, const char *file_format) |
f02df580 MS |
191 | { |
192 | struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL); | |
193 | CORE_ADDR lo; | |
194 | CORE_ADDR hi; | |
195 | ULONGEST count; | |
93db0d79 | 196 | const char *filename; |
93db0d79 TT |
197 | const char *lo_exp; |
198 | const char *hi_exp; | |
f02df580 MS |
199 | |
200 | /* Open the file. */ | |
201 | filename = scan_filename_with_cleanup (&cmd, NULL); | |
202 | ||
203 | /* Find the low address. */ | |
204 | if (cmd == NULL || *cmd == '\0') | |
8a3fe4f8 | 205 | error (_("Missing start address.")); |
f02df580 MS |
206 | lo_exp = scan_expression_with_cleanup (&cmd, NULL); |
207 | ||
208 | /* Find the second address - rest of line. */ | |
209 | if (cmd == NULL || *cmd == '\0') | |
8a3fe4f8 | 210 | error (_("Missing stop address.")); |
f02df580 MS |
211 | hi_exp = cmd; |
212 | ||
213 | lo = parse_and_eval_address (lo_exp); | |
214 | hi = parse_and_eval_address (hi_exp); | |
215 | if (hi <= lo) | |
8a3fe4f8 | 216 | error (_("Invalid memory address range (start >= end).")); |
f02df580 MS |
217 | count = hi - lo; |
218 | ||
219 | /* FIXME: Should use read_memory_partial() and a magic blocking | |
220 | value. */ | |
d5722aa2 PA |
221 | gdb::byte_vector buf (count); |
222 | read_memory (lo, buf.data (), count); | |
f02df580 MS |
223 | |
224 | /* Have everything. Open/write the data. */ | |
225 | if (file_format == NULL || strcmp (file_format, "binary") == 0) | |
226 | { | |
d5722aa2 | 227 | dump_binary_file (filename, mode, buf.data (), count); |
f02df580 MS |
228 | } |
229 | else | |
230 | { | |
d5722aa2 | 231 | dump_bfd_file (filename, mode, file_format, lo, buf.data (), count); |
f02df580 MS |
232 | } |
233 | ||
234 | do_cleanups (old_cleanups); | |
235 | } | |
236 | ||
237 | static void | |
a121b7c1 | 238 | dump_memory_command (char *cmd, const char *mode) |
f02df580 MS |
239 | { |
240 | dump_memory_to_file (cmd, mode, "binary"); | |
241 | } | |
242 | ||
243 | static void | |
93db0d79 | 244 | dump_value_to_file (const char *cmd, const char *mode, const char *file_format) |
f02df580 MS |
245 | { |
246 | struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL); | |
247 | struct value *val; | |
93db0d79 | 248 | const char *filename; |
f02df580 MS |
249 | |
250 | /* Open the file. */ | |
251 | filename = scan_filename_with_cleanup (&cmd, NULL); | |
252 | ||
253 | /* Find the value. */ | |
254 | if (cmd == NULL || *cmd == '\0') | |
8a3fe4f8 | 255 | error (_("No value to %s."), *mode == 'a' ? "append" : "dump"); |
f02df580 MS |
256 | val = parse_and_eval (cmd); |
257 | if (val == NULL) | |
8a3fe4f8 | 258 | error (_("Invalid expression.")); |
f02df580 MS |
259 | |
260 | /* Have everything. Open/write the data. */ | |
261 | if (file_format == NULL || strcmp (file_format, "binary") == 0) | |
262 | { | |
0fd88904 | 263 | dump_binary_file (filename, mode, value_contents (val), |
df407dfe | 264 | TYPE_LENGTH (value_type (val))); |
f02df580 MS |
265 | } |
266 | else | |
267 | { | |
268 | CORE_ADDR vaddr; | |
269 | ||
270 | if (VALUE_LVAL (val)) | |
271 | { | |
42ae5230 | 272 | vaddr = value_address (val); |
f02df580 MS |
273 | } |
274 | else | |
275 | { | |
276 | vaddr = 0; | |
8a3fe4f8 | 277 | warning (_("value is not an lval: address assumed to be zero")); |
f02df580 MS |
278 | } |
279 | ||
280 | dump_bfd_file (filename, mode, file_format, vaddr, | |
0fd88904 | 281 | value_contents (val), |
df407dfe | 282 | TYPE_LENGTH (value_type (val))); |
f02df580 MS |
283 | } |
284 | ||
285 | do_cleanups (old_cleanups); | |
286 | } | |
287 | ||
288 | static void | |
a121b7c1 | 289 | dump_value_command (char *cmd, const char *mode) |
f02df580 MS |
290 | { |
291 | dump_value_to_file (cmd, mode, "binary"); | |
292 | } | |
293 | ||
f02df580 MS |
294 | static void |
295 | dump_srec_memory (char *args, int from_tty) | |
296 | { | |
5d1d95de | 297 | dump_memory_to_file (args, FOPEN_WB, "srec"); |
f02df580 MS |
298 | } |
299 | ||
300 | static void | |
301 | dump_srec_value (char *args, int from_tty) | |
302 | { | |
5d1d95de | 303 | dump_value_to_file (args, FOPEN_WB, "srec"); |
f02df580 MS |
304 | } |
305 | ||
306 | static void | |
307 | dump_ihex_memory (char *args, int from_tty) | |
308 | { | |
5d1d95de | 309 | dump_memory_to_file (args, FOPEN_WB, "ihex"); |
f02df580 MS |
310 | } |
311 | ||
312 | static void | |
313 | dump_ihex_value (char *args, int from_tty) | |
314 | { | |
5d1d95de | 315 | dump_value_to_file (args, FOPEN_WB, "ihex"); |
f02df580 MS |
316 | } |
317 | ||
cf75d6c3 AB |
318 | static void |
319 | dump_verilog_memory (char *args, int from_tty) | |
320 | { | |
321 | dump_memory_to_file (args, FOPEN_WB, "verilog"); | |
322 | } | |
323 | ||
324 | static void | |
325 | dump_verilog_value (char *args, int from_tty) | |
326 | { | |
327 | dump_value_to_file (args, FOPEN_WB, "verilog"); | |
328 | } | |
329 | ||
f02df580 MS |
330 | static void |
331 | dump_tekhex_memory (char *args, int from_tty) | |
332 | { | |
5d1d95de | 333 | dump_memory_to_file (args, FOPEN_WB, "tekhex"); |
f02df580 MS |
334 | } |
335 | ||
336 | static void | |
337 | dump_tekhex_value (char *args, int from_tty) | |
338 | { | |
5d1d95de | 339 | dump_value_to_file (args, FOPEN_WB, "tekhex"); |
f02df580 MS |
340 | } |
341 | ||
342 | static void | |
343 | dump_binary_memory (char *args, int from_tty) | |
344 | { | |
5d1d95de | 345 | dump_memory_to_file (args, FOPEN_WB, "binary"); |
f02df580 MS |
346 | } |
347 | ||
348 | static void | |
349 | dump_binary_value (char *args, int from_tty) | |
350 | { | |
5d1d95de | 351 | dump_value_to_file (args, FOPEN_WB, "binary"); |
f02df580 MS |
352 | } |
353 | ||
354 | static void | |
355 | append_binary_memory (char *args, int from_tty) | |
356 | { | |
5d1d95de | 357 | dump_memory_to_file (args, FOPEN_AB, "binary"); |
f02df580 MS |
358 | } |
359 | ||
360 | static void | |
361 | append_binary_value (char *args, int from_tty) | |
362 | { | |
5d1d95de | 363 | dump_value_to_file (args, FOPEN_AB, "binary"); |
f02df580 MS |
364 | } |
365 | ||
366 | struct dump_context | |
367 | { | |
a121b7c1 PA |
368 | void (*func) (char *cmd, const char *mode); |
369 | const char *mode; | |
f02df580 MS |
370 | }; |
371 | ||
372 | static void | |
373 | call_dump_func (struct cmd_list_element *c, char *args, int from_tty) | |
374 | { | |
9a3c8263 | 375 | struct dump_context *d = (struct dump_context *) get_cmd_context (c); |
cdb27c12 | 376 | |
f02df580 MS |
377 | d->func (args, d->mode); |
378 | } | |
379 | ||
db229724 | 380 | static void |
a121b7c1 PA |
381 | add_dump_command (const char *name, |
382 | void (*func) (char *args, const char *mode), | |
383 | const char *descr) | |
f02df580 MS |
384 | |
385 | { | |
386 | struct cmd_list_element *c; | |
387 | struct dump_context *d; | |
388 | ||
389 | c = add_cmd (name, all_commands, NULL, descr, &dump_cmdlist); | |
390 | c->completer = filename_completer; | |
70ba0933 | 391 | d = XNEW (struct dump_context); |
f02df580 | 392 | d->func = func; |
5d1d95de | 393 | d->mode = FOPEN_WB; |
f02df580 MS |
394 | set_cmd_context (c, d); |
395 | c->func = call_dump_func; | |
396 | ||
397 | c = add_cmd (name, all_commands, NULL, descr, &append_cmdlist); | |
398 | c->completer = filename_completer; | |
70ba0933 | 399 | d = XNEW (struct dump_context); |
f02df580 | 400 | d->func = func; |
5d1d95de | 401 | d->mode = FOPEN_AB; |
f02df580 MS |
402 | set_cmd_context (c, d); |
403 | c->func = call_dump_func; | |
404 | ||
cb1a6d5f | 405 | /* Replace "Dump " at start of docstring with "Append " (borrowed |
eefe576e | 406 | from [deleted] deprecated_add_show_from_set). */ |
f02df580 MS |
407 | if ( c->doc[0] == 'W' |
408 | && c->doc[1] == 'r' | |
409 | && c->doc[2] == 'i' | |
410 | && c->doc[3] == 't' | |
411 | && c->doc[4] == 'e' | |
412 | && c->doc[5] == ' ') | |
1754f103 | 413 | c->doc = concat ("Append ", c->doc + 6, (char *)NULL); |
f02df580 MS |
414 | } |
415 | ||
ebcd3b23 | 416 | /* Opaque data for restore_section_callback. */ |
f02df580 | 417 | struct callback_data { |
1fac167a | 418 | CORE_ADDR load_offset; |
f02df580 MS |
419 | CORE_ADDR load_start; |
420 | CORE_ADDR load_end; | |
421 | }; | |
422 | ||
423 | /* Function: restore_section_callback. | |
424 | ||
425 | Callback function for bfd_map_over_sections. | |
426 | Selectively loads the sections into memory. */ | |
427 | ||
428 | static void | |
429 | restore_section_callback (bfd *ibfd, asection *isec, void *args) | |
430 | { | |
9a3c8263 | 431 | struct callback_data *data = (struct callback_data *) args; |
f02df580 MS |
432 | bfd_vma sec_start = bfd_section_vma (ibfd, isec); |
433 | bfd_size_type size = bfd_section_size (ibfd, isec); | |
434 | bfd_vma sec_end = sec_start + size; | |
435 | bfd_size_type sec_offset = 0; | |
436 | bfd_size_type sec_load_count = size; | |
437 | struct cleanup *old_chain; | |
47b667de | 438 | gdb_byte *buf; |
f02df580 MS |
439 | int ret; |
440 | ||
ebcd3b23 | 441 | /* Ignore non-loadable sections, eg. from elf files. */ |
f02df580 MS |
442 | if (!(bfd_get_section_flags (ibfd, isec) & SEC_LOAD)) |
443 | return; | |
444 | ||
445 | /* Does the section overlap with the desired restore range? */ | |
446 | if (sec_end <= data->load_start | |
447 | || (data->load_end > 0 && sec_start >= data->load_end)) | |
448 | { | |
ebcd3b23 | 449 | /* No, no useable data in this section. */ |
a3f17187 | 450 | printf_filtered (_("skipping section %s...\n"), |
f02df580 MS |
451 | bfd_section_name (ibfd, isec)); |
452 | return; | |
453 | } | |
454 | ||
455 | /* Compare section address range with user-requested | |
456 | address range (if any). Compute where the actual | |
457 | transfer should start and end. */ | |
458 | if (sec_start < data->load_start) | |
459 | sec_offset = data->load_start - sec_start; | |
ebcd3b23 | 460 | /* Size of a partial transfer. */ |
f02df580 MS |
461 | sec_load_count -= sec_offset; |
462 | if (data->load_end > 0 && sec_end > data->load_end) | |
463 | sec_load_count -= sec_end - data->load_end; | |
464 | ||
465 | /* Get the data. */ | |
224c3ddb | 466 | buf = (gdb_byte *) xmalloc (size); |
f02df580 MS |
467 | old_chain = make_cleanup (xfree, buf); |
468 | if (!bfd_get_section_contents (ibfd, isec, buf, 0, size)) | |
8a3fe4f8 | 469 | error (_("Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd), |
f02df580 MS |
470 | bfd_errmsg (bfd_get_error ())); |
471 | ||
472 | printf_filtered ("Restoring section %s (0x%lx to 0x%lx)", | |
473 | bfd_section_name (ibfd, isec), | |
474 | (unsigned long) sec_start, | |
475 | (unsigned long) sec_end); | |
476 | ||
477 | if (data->load_offset != 0 || data->load_start != 0 || data->load_end != 0) | |
5af949e3 | 478 | printf_filtered (" into memory (%s to %s)\n", |
f5656ead | 479 | paddress (target_gdbarch (), |
5af949e3 | 480 | (unsigned long) sec_start |
f5db4da3 | 481 | + sec_offset + data->load_offset), |
f5656ead | 482 | paddress (target_gdbarch (), |
5af949e3 UW |
483 | (unsigned long) sec_start + sec_offset |
484 | + data->load_offset + sec_load_count)); | |
f02df580 MS |
485 | else |
486 | puts_filtered ("\n"); | |
487 | ||
488 | /* Write the data. */ | |
489 | ret = target_write_memory (sec_start + sec_offset + data->load_offset, | |
490 | buf + sec_offset, sec_load_count); | |
491 | if (ret != 0) | |
8a3fe4f8 | 492 | warning (_("restore: memory write failed (%s)."), safe_strerror (ret)); |
f02df580 MS |
493 | do_cleanups (old_chain); |
494 | return; | |
495 | } | |
496 | ||
497 | static void | |
93db0d79 | 498 | restore_binary_file (const char *filename, struct callback_data *data) |
f02df580 | 499 | { |
d419f42d | 500 | gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB); |
f02df580 MS |
501 | long len; |
502 | ||
503 | /* Get the file size for reading. */ | |
d419f42d | 504 | if (fseek (file.get (), 0, SEEK_END) == 0) |
5e9e105f | 505 | { |
d419f42d | 506 | len = ftell (file.get ()); |
5e9e105f MS |
507 | if (len < 0) |
508 | perror_with_name (filename); | |
509 | } | |
f02df580 MS |
510 | else |
511 | perror_with_name (filename); | |
512 | ||
513 | if (len <= data->load_start) | |
8a3fe4f8 | 514 | error (_("Start address is greater than length of binary file %s."), |
f02df580 MS |
515 | filename); |
516 | ||
ebcd3b23 | 517 | /* Chop off "len" if it exceeds the requested load_end addr. */ |
f02df580 MS |
518 | if (data->load_end != 0 && data->load_end < len) |
519 | len = data->load_end; | |
ebcd3b23 | 520 | /* Chop off "len" if the requested load_start addr skips some bytes. */ |
f02df580 MS |
521 | if (data->load_start > 0) |
522 | len -= data->load_start; | |
523 | ||
524 | printf_filtered | |
525 | ("Restoring binary file %s into memory (0x%lx to 0x%lx)\n", | |
526 | filename, | |
1fac167a UW |
527 | (unsigned long) (data->load_start + data->load_offset), |
528 | (unsigned long) (data->load_start + data->load_offset + len)); | |
f02df580 MS |
529 | |
530 | /* Now set the file pos to the requested load start pos. */ | |
d419f42d | 531 | if (fseek (file.get (), data->load_start, SEEK_SET) != 0) |
f02df580 MS |
532 | perror_with_name (filename); |
533 | ||
534 | /* Now allocate a buffer and read the file contents. */ | |
d5722aa2 | 535 | gdb::byte_vector buf (len); |
d419f42d | 536 | if (fread (buf.data (), 1, len, file.get ()) != len) |
f02df580 MS |
537 | perror_with_name (filename); |
538 | ||
ebcd3b23 | 539 | /* Now write the buffer into target memory. */ |
cd9da5b0 | 540 | len = target_write_memory (data->load_start + data->load_offset, |
d5722aa2 | 541 | buf.data (), len); |
f02df580 | 542 | if (len != 0) |
8a3fe4f8 | 543 | warning (_("restore: memory write failed (%s)."), safe_strerror (len)); |
f02df580 MS |
544 | } |
545 | ||
546 | static void | |
93db0d79 | 547 | restore_command (char *args_in, int from_tty) |
f02df580 MS |
548 | { |
549 | char *filename; | |
550 | struct callback_data data; | |
551 | bfd *ibfd; | |
552 | int binary_flag = 0; | |
93db0d79 | 553 | const char *args = args_in; |
f02df580 MS |
554 | |
555 | if (!target_has_execution) | |
556 | noprocess (); | |
557 | ||
558 | data.load_offset = 0; | |
559 | data.load_start = 0; | |
560 | data.load_end = 0; | |
561 | ||
ebcd3b23 | 562 | /* Parse the input arguments. First is filename (required). */ |
f02df580 MS |
563 | filename = scan_filename_with_cleanup (&args, NULL); |
564 | if (args != NULL && *args != '\0') | |
565 | { | |
a121b7c1 | 566 | static const char binary_string[] = "binary"; |
f02df580 MS |
567 | |
568 | /* Look for optional "binary" flag. */ | |
61012eef | 569 | if (startswith (args, binary_string)) |
f02df580 MS |
570 | { |
571 | binary_flag = 1; | |
572 | args += strlen (binary_string); | |
93db0d79 | 573 | args = skip_spaces_const (args); |
f02df580 | 574 | } |
ebcd3b23 | 575 | /* Parse offset (optional). */ |
f02df580 | 576 | if (args != NULL && *args != '\0') |
cbd641ed PS |
577 | data.load_offset = binary_flag ? |
578 | parse_and_eval_address (scan_expression_with_cleanup (&args, NULL)) : | |
579 | parse_and_eval_long (scan_expression_with_cleanup (&args, NULL)); | |
f02df580 MS |
580 | if (args != NULL && *args != '\0') |
581 | { | |
ebcd3b23 | 582 | /* Parse start address (optional). */ |
f02df580 | 583 | data.load_start = |
de530e84 | 584 | parse_and_eval_long (scan_expression_with_cleanup (&args, NULL)); |
f02df580 MS |
585 | if (args != NULL && *args != '\0') |
586 | { | |
ebcd3b23 | 587 | /* Parse end address (optional). */ |
de530e84 | 588 | data.load_end = parse_and_eval_long (args); |
f02df580 | 589 | if (data.load_end <= data.load_start) |
8a3fe4f8 | 590 | error (_("Start must be less than end.")); |
f02df580 MS |
591 | } |
592 | } | |
593 | } | |
594 | ||
595 | if (info_verbose) | |
596 | printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n", | |
597 | filename, (unsigned long) data.load_offset, | |
598 | (unsigned long) data.load_start, | |
599 | (unsigned long) data.load_end); | |
600 | ||
601 | if (binary_flag) | |
602 | { | |
603 | restore_binary_file (filename, &data); | |
604 | } | |
605 | else | |
606 | { | |
ebcd3b23 | 607 | /* Open the file for loading. */ |
192b62ce | 608 | gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename, NULL)); |
f02df580 | 609 | |
ebcd3b23 | 610 | /* Process the sections. */ |
192b62ce | 611 | bfd_map_over_sections (ibfd.get (), restore_section_callback, &data); |
f02df580 | 612 | } |
f02df580 MS |
613 | } |
614 | ||
615 | static void | |
616 | srec_dump_command (char *cmd, int from_tty) | |
617 | { | |
6faec16b | 618 | printf_unfiltered (_("\"dump srec\" must be followed by a subcommand.\n")); |
635c7e8a | 619 | help_list (srec_cmdlist, "dump srec ", all_commands, gdb_stdout); |
f02df580 MS |
620 | } |
621 | ||
622 | static void | |
623 | ihex_dump_command (char *cmd, int from_tty) | |
624 | { | |
6faec16b | 625 | printf_unfiltered (_("\"dump ihex\" must be followed by a subcommand.\n")); |
635c7e8a | 626 | help_list (ihex_cmdlist, "dump ihex ", all_commands, gdb_stdout); |
f02df580 MS |
627 | } |
628 | ||
cf75d6c3 AB |
629 | static void |
630 | verilog_dump_command (char *cmd, int from_tty) | |
631 | { | |
632 | printf_unfiltered (_("\"dump verilog\" must be followed by a subcommand.\n")); | |
633 | help_list (verilog_cmdlist, "dump verilog ", all_commands, gdb_stdout); | |
634 | } | |
635 | ||
f02df580 MS |
636 | static void |
637 | tekhex_dump_command (char *cmd, int from_tty) | |
638 | { | |
6faec16b | 639 | printf_unfiltered (_("\"dump tekhex\" must be followed by a subcommand.\n")); |
635c7e8a | 640 | help_list (tekhex_cmdlist, "dump tekhex ", all_commands, gdb_stdout); |
f02df580 MS |
641 | } |
642 | ||
643 | static void | |
644 | binary_dump_command (char *cmd, int from_tty) | |
645 | { | |
6faec16b | 646 | printf_unfiltered (_("\"dump binary\" must be followed by a subcommand.\n")); |
635c7e8a | 647 | help_list (binary_dump_cmdlist, "dump binary ", all_commands, gdb_stdout); |
f02df580 MS |
648 | } |
649 | ||
650 | static void | |
651 | binary_append_command (char *cmd, int from_tty) | |
652 | { | |
6faec16b | 653 | printf_unfiltered (_("\"append binary\" must be followed by a subcommand.\n")); |
635c7e8a TT |
654 | help_list (binary_append_cmdlist, "append binary ", all_commands, |
655 | gdb_stdout); | |
f02df580 MS |
656 | } |
657 | ||
b9362cc7 AC |
658 | extern initialize_file_ftype _initialize_cli_dump; /* -Wmissing-prototypes */ |
659 | ||
f02df580 MS |
660 | void |
661 | _initialize_cli_dump (void) | |
662 | { | |
663 | struct cmd_list_element *c; | |
cdb27c12 | 664 | |
9a2b4c1b MS |
665 | add_prefix_cmd ("dump", class_vars, dump_command, |
666 | _("Dump target code/data to a local file."), | |
f02df580 MS |
667 | &dump_cmdlist, "dump ", |
668 | 0/*allow-unknown*/, | |
669 | &cmdlist); | |
9a2b4c1b MS |
670 | add_prefix_cmd ("append", class_vars, append_command, |
671 | _("Append target code/data to a local file."), | |
f02df580 MS |
672 | &append_cmdlist, "append ", |
673 | 0/*allow-unknown*/, | |
674 | &cmdlist); | |
675 | ||
676 | add_dump_command ("memory", dump_memory_command, "\ | |
677 | Write contents of memory to a raw binary file.\n\ | |
678 | Arguments are FILE START STOP. Writes the contents of memory within the\n\ | |
64b9b334 | 679 | range [START .. STOP) to the specified FILE in raw target ordered bytes."); |
f02df580 MS |
680 | |
681 | add_dump_command ("value", dump_value_command, "\ | |
682 | Write the value of an expression to a raw binary file.\n\ | |
683 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION to\n\ | |
684 | the specified FILE in raw target ordered bytes."); | |
685 | ||
9a2b4c1b MS |
686 | add_prefix_cmd ("srec", all_commands, srec_dump_command, |
687 | _("Write target code/data to an srec file."), | |
f02df580 MS |
688 | &srec_cmdlist, "dump srec ", |
689 | 0 /*allow-unknown*/, | |
690 | &dump_cmdlist); | |
691 | ||
9a2b4c1b MS |
692 | add_prefix_cmd ("ihex", all_commands, ihex_dump_command, |
693 | _("Write target code/data to an intel hex file."), | |
f02df580 MS |
694 | &ihex_cmdlist, "dump ihex ", |
695 | 0 /*allow-unknown*/, | |
696 | &dump_cmdlist); | |
697 | ||
cf75d6c3 AB |
698 | add_prefix_cmd ("verilog", all_commands, verilog_dump_command, |
699 | _("Write target code/data to a verilog hex file."), | |
700 | &verilog_cmdlist, "dump verilog ", | |
701 | 0 /*allow-unknown*/, | |
702 | &dump_cmdlist); | |
703 | ||
9a2b4c1b MS |
704 | add_prefix_cmd ("tekhex", all_commands, tekhex_dump_command, |
705 | _("Write target code/data to a tekhex file."), | |
f02df580 MS |
706 | &tekhex_cmdlist, "dump tekhex ", |
707 | 0 /*allow-unknown*/, | |
708 | &dump_cmdlist); | |
709 | ||
9a2b4c1b MS |
710 | add_prefix_cmd ("binary", all_commands, binary_dump_command, |
711 | _("Write target code/data to a raw binary file."), | |
f02df580 MS |
712 | &binary_dump_cmdlist, "dump binary ", |
713 | 0 /*allow-unknown*/, | |
714 | &dump_cmdlist); | |
715 | ||
9a2b4c1b MS |
716 | add_prefix_cmd ("binary", all_commands, binary_append_command, |
717 | _("Append target code/data to a raw binary file."), | |
f02df580 MS |
718 | &binary_append_cmdlist, "append binary ", |
719 | 0 /*allow-unknown*/, | |
720 | &append_cmdlist); | |
721 | ||
1a966eab | 722 | add_cmd ("memory", all_commands, dump_srec_memory, _("\ |
f02df580 MS |
723 | Write contents of memory to an srec file.\n\ |
724 | Arguments are FILE START STOP. Writes the contents of memory\n\ | |
64b9b334 | 725 | within the range [START .. STOP) to the specified FILE in srec format."), |
f02df580 MS |
726 | &srec_cmdlist); |
727 | ||
1a966eab | 728 | add_cmd ("value", all_commands, dump_srec_value, _("\ |
f02df580 MS |
729 | Write the value of an expression to an srec file.\n\ |
730 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\ | |
1a966eab | 731 | to the specified FILE in srec format."), |
f02df580 MS |
732 | &srec_cmdlist); |
733 | ||
1a966eab | 734 | add_cmd ("memory", all_commands, dump_ihex_memory, _("\ |
f02df580 MS |
735 | Write contents of memory to an ihex file.\n\ |
736 | Arguments are FILE START STOP. Writes the contents of memory within\n\ | |
64b9b334 | 737 | the range [START .. STOP) to the specified FILE in intel hex format."), |
f02df580 MS |
738 | &ihex_cmdlist); |
739 | ||
1a966eab | 740 | add_cmd ("value", all_commands, dump_ihex_value, _("\ |
f02df580 MS |
741 | Write the value of an expression to an ihex file.\n\ |
742 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\ | |
1a966eab | 743 | to the specified FILE in intel hex format."), |
f02df580 MS |
744 | &ihex_cmdlist); |
745 | ||
cf75d6c3 AB |
746 | add_cmd ("memory", all_commands, dump_verilog_memory, _("\ |
747 | Write contents of memory to a verilog hex file.\n\ | |
748 | Arguments are FILE START STOP. Writes the contents of memory within\n\ | |
749 | the range [START .. STOP) to the specified FILE in verilog hex format."), | |
750 | &verilog_cmdlist); | |
751 | ||
752 | add_cmd ("value", all_commands, dump_verilog_value, _("\ | |
753 | Write the value of an expression to a verilog hex file.\n\ | |
754 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\ | |
755 | to the specified FILE in verilog hex format."), | |
756 | &verilog_cmdlist); | |
757 | ||
1a966eab | 758 | add_cmd ("memory", all_commands, dump_tekhex_memory, _("\ |
f02df580 MS |
759 | Write contents of memory to a tekhex file.\n\ |
760 | Arguments are FILE START STOP. Writes the contents of memory\n\ | |
64b9b334 | 761 | within the range [START .. STOP) to the specified FILE in tekhex format."), |
f02df580 MS |
762 | &tekhex_cmdlist); |
763 | ||
1a966eab | 764 | add_cmd ("value", all_commands, dump_tekhex_value, _("\ |
f02df580 MS |
765 | Write the value of an expression to a tekhex file.\n\ |
766 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\ | |
1a966eab | 767 | to the specified FILE in tekhex format."), |
f02df580 MS |
768 | &tekhex_cmdlist); |
769 | ||
1a966eab | 770 | add_cmd ("memory", all_commands, dump_binary_memory, _("\ |
f02df580 MS |
771 | Write contents of memory to a raw binary file.\n\ |
772 | Arguments are FILE START STOP. Writes the contents of memory\n\ | |
64b9b334 | 773 | within the range [START .. STOP) to the specified FILE in binary format."), |
f02df580 MS |
774 | &binary_dump_cmdlist); |
775 | ||
1a966eab | 776 | add_cmd ("value", all_commands, dump_binary_value, _("\ |
f02df580 MS |
777 | Write the value of an expression to a raw binary file.\n\ |
778 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\ | |
1a966eab | 779 | to the specified FILE in raw target ordered bytes."), |
f02df580 MS |
780 | &binary_dump_cmdlist); |
781 | ||
1a966eab | 782 | add_cmd ("memory", all_commands, append_binary_memory, _("\ |
f02df580 MS |
783 | Append contents of memory to a raw binary file.\n\ |
784 | Arguments are FILE START STOP. Writes the contents of memory within the\n\ | |
64b9b334 | 785 | range [START .. STOP) to the specified FILE in raw target ordered bytes."), |
f02df580 MS |
786 | &binary_append_cmdlist); |
787 | ||
1a966eab | 788 | add_cmd ("value", all_commands, append_binary_value, _("\ |
f02df580 MS |
789 | Append the value of an expression to a raw binary file.\n\ |
790 | Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\ | |
1a966eab | 791 | to the specified FILE in raw target ordered bytes."), |
f02df580 MS |
792 | &binary_append_cmdlist); |
793 | ||
1bedd215 AC |
794 | c = add_com ("restore", class_vars, restore_command, _("\ |
795 | Restore the contents of FILE to target memory.\n\ | |
f02df580 MS |
796 | Arguments are FILE OFFSET START END where all except FILE are optional.\n\ |
797 | OFFSET will be added to the base address of the file (default zero).\n\ | |
9eb6e5a1 | 798 | If START and END are given, only the file contents within that range\n\ |
1bedd215 | 799 | (file relative) will be restored to target memory.")); |
f02df580 | 800 | c->completer = filename_completer; |
ebcd3b23 | 801 | /* FIXME: completers for other commands. */ |
f02df580 | 802 | } |