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