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