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