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