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