]>
Commit | Line | Data |
---|---|---|
f02df580 MS |
1 | /* Dump-to-file commands, for GDB, the GNU debugger. |
2 | ||
1d506c26 | 3 | Copyright (C) 2002-2024 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" |
99d9c3b9 | 36 | #include "inferior.h" |
f02df580 | 37 | |
ee0c3293 TT |
38 | static gdb::unique_xmalloc_ptr<char> |
39 | scan_expression (const char **cmd, const char *def) | |
f02df580 MS |
40 | { |
41 | if ((*cmd) == NULL || (**cmd) == '\0') | |
b02f78f9 | 42 | return make_unique_xstrdup (def); |
f02df580 MS |
43 | else |
44 | { | |
45 | char *exp; | |
93db0d79 | 46 | const char *end; |
f02df580 MS |
47 | |
48 | end = (*cmd) + strcspn (*cmd, " \t"); | |
49 | exp = savestring ((*cmd), end - (*cmd)); | |
f1735a53 | 50 | (*cmd) = skip_spaces (end); |
ee0c3293 | 51 | return gdb::unique_xmalloc_ptr<char> (exp); |
f02df580 MS |
52 | } |
53 | } | |
54 | ||
55 | ||
ee0c3293 TT |
56 | static gdb::unique_xmalloc_ptr<char> |
57 | scan_filename (const char **cmd, const char *defname) | |
f02df580 | 58 | { |
ee0c3293 | 59 | gdb::unique_xmalloc_ptr<char> filename; |
f02df580 MS |
60 | |
61 | /* FIXME: Need to get the ``/a(ppend)'' flag from somewhere. */ | |
62 | ||
63 | /* File. */ | |
64 | if ((*cmd) == NULL) | |
65 | { | |
66 | if (defname == NULL) | |
8a3fe4f8 | 67 | error (_("Missing filename.")); |
ee0c3293 | 68 | filename.reset (xstrdup (defname)); |
f02df580 MS |
69 | } |
70 | else | |
71 | { | |
72 | /* FIXME: should parse a possibly quoted string. */ | |
93db0d79 | 73 | const char *end; |
f02df580 | 74 | |
f1735a53 | 75 | (*cmd) = skip_spaces (*cmd); |
f02df580 | 76 | end = *cmd + strcspn (*cmd, " \t"); |
ee0c3293 | 77 | filename.reset (savestring ((*cmd), end - (*cmd))); |
f1735a53 | 78 | (*cmd) = skip_spaces (end); |
f02df580 MS |
79 | } |
80 | gdb_assert (filename != NULL); | |
81 | ||
ee0c3293 | 82 | return gdb::unique_xmalloc_ptr<char> (tilde_expand (filename.get ())); |
f02df580 MS |
83 | } |
84 | ||
192b62ce TT |
85 | static gdb_bfd_ref_ptr |
86 | bfd_openr_or_error (const char *filename, const char *target) | |
f02df580 | 87 | { |
192b62ce | 88 | gdb_bfd_ref_ptr ibfd (gdb_bfd_openr (filename, target)); |
5cb316ef | 89 | if (ibfd == NULL) |
192b62ce | 90 | error (_("Failed to open %s: %s."), filename, |
f02df580 MS |
91 | bfd_errmsg (bfd_get_error ())); |
92 | ||
192b62ce | 93 | if (!bfd_check_format (ibfd.get (), bfd_object)) |
8a3fe4f8 | 94 | error (_("'%s' is not a recognized file format."), filename); |
f02df580 MS |
95 | |
96 | return ibfd; | |
97 | } | |
98 | ||
192b62ce TT |
99 | static gdb_bfd_ref_ptr |
100 | bfd_openw_or_error (const char *filename, const char *target, const char *mode) | |
f02df580 | 101 | { |
192b62ce | 102 | gdb_bfd_ref_ptr obfd; |
f02df580 MS |
103 | |
104 | if (*mode == 'w') /* Write: create new file */ | |
105 | { | |
64c31149 | 106 | obfd = gdb_bfd_openw (filename, target); |
5cb316ef | 107 | if (obfd == NULL) |
192b62ce | 108 | error (_("Failed to open %s: %s."), filename, |
f02df580 | 109 | bfd_errmsg (bfd_get_error ())); |
192b62ce TT |
110 | if (!bfd_set_format (obfd.get (), bfd_object)) |
111 | error (_("bfd_openw_or_error: %s."), bfd_errmsg (bfd_get_error ())); | |
f02df580 | 112 | } |
ebcd3b23 MS |
113 | else if (*mode == 'a') /* Append to existing file. */ |
114 | { /* FIXME -- doesn't work... */ | |
8a3fe4f8 | 115 | error (_("bfd_openw does not work with append.")); |
f02df580 MS |
116 | } |
117 | else | |
192b62ce | 118 | error (_("bfd_openw_or_error: unknown mode %s."), mode); |
f02df580 MS |
119 | |
120 | return obfd; | |
121 | } | |
122 | ||
28578e6b YQ |
123 | static struct cmd_list_element *dump_cmdlist; |
124 | static struct cmd_list_element *append_cmdlist; | |
125 | static struct cmd_list_element *srec_cmdlist; | |
126 | static struct cmd_list_element *ihex_cmdlist; | |
cf75d6c3 | 127 | static struct cmd_list_element *verilog_cmdlist; |
28578e6b YQ |
128 | static struct cmd_list_element *tekhex_cmdlist; |
129 | static struct cmd_list_element *binary_dump_cmdlist; | |
130 | static struct cmd_list_element *binary_append_cmdlist; | |
f02df580 | 131 | |
f02df580 | 132 | static void |
c26b8e3b | 133 | dump_binary_file (const char *filename, const char *mode, |
5005c8a9 | 134 | const bfd_byte *buf, ULONGEST len) |
f02df580 | 135 | { |
f02df580 MS |
136 | int status; |
137 | ||
d419f42d | 138 | gdb_file_up file = gdb_fopen_cloexec (filename, mode); |
bea3329b SM |
139 | if (file == nullptr) |
140 | perror_with_name (filename); | |
141 | ||
d419f42d | 142 | status = fwrite (buf, len, 1, file.get ()); |
f02df580 MS |
143 | if (status != 1) |
144 | perror_with_name (filename); | |
145 | } | |
146 | ||
147 | static void | |
c26b8e3b AC |
148 | dump_bfd_file (const char *filename, const char *mode, |
149 | const char *target, CORE_ADDR vaddr, | |
5005c8a9 | 150 | const bfd_byte *buf, ULONGEST len) |
f02df580 | 151 | { |
f02df580 MS |
152 | asection *osection; |
153 | ||
192b62ce TT |
154 | gdb_bfd_ref_ptr obfd (bfd_openw_or_error (filename, target, mode)); |
155 | osection = bfd_make_section_anyway (obfd.get (), ".newsec"); | |
fd361982 AM |
156 | bfd_set_section_size (osection, len); |
157 | bfd_set_section_vma (osection, vaddr); | |
158 | bfd_set_section_alignment (osection, 0); | |
159 | bfd_set_section_flags (osection, (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD)); | |
f02df580 | 160 | osection->entsize = 0; |
192b62ce TT |
161 | if (!bfd_set_section_contents (obfd.get (), osection, buf, 0, len)) |
162 | warning (_("writing dump file '%s' (%s)"), filename, | |
53624a93 | 163 | bfd_errmsg (bfd_get_error ())); |
f02df580 MS |
164 | } |
165 | ||
166 | static void | |
93db0d79 | 167 | dump_memory_to_file (const char *cmd, const char *mode, const char *file_format) |
f02df580 | 168 | { |
f02df580 MS |
169 | CORE_ADDR lo; |
170 | CORE_ADDR hi; | |
171 | ULONGEST count; | |
93db0d79 | 172 | const char *hi_exp; |
f02df580 MS |
173 | |
174 | /* Open the file. */ | |
ee0c3293 | 175 | gdb::unique_xmalloc_ptr<char> filename = scan_filename (&cmd, NULL); |
f02df580 MS |
176 | |
177 | /* Find the low address. */ | |
178 | if (cmd == NULL || *cmd == '\0') | |
8a3fe4f8 | 179 | error (_("Missing start address.")); |
ee0c3293 | 180 | gdb::unique_xmalloc_ptr<char> lo_exp = scan_expression (&cmd, NULL); |
f02df580 MS |
181 | |
182 | /* Find the second address - rest of line. */ | |
183 | if (cmd == NULL || *cmd == '\0') | |
8a3fe4f8 | 184 | error (_("Missing stop address.")); |
f02df580 MS |
185 | hi_exp = cmd; |
186 | ||
ee0c3293 | 187 | lo = parse_and_eval_address (lo_exp.get ()); |
f02df580 MS |
188 | hi = parse_and_eval_address (hi_exp); |
189 | if (hi <= lo) | |
8a3fe4f8 | 190 | error (_("Invalid memory address range (start >= end).")); |
f02df580 MS |
191 | count = hi - lo; |
192 | ||
193 | /* FIXME: Should use read_memory_partial() and a magic blocking | |
194 | value. */ | |
d5722aa2 PA |
195 | gdb::byte_vector buf (count); |
196 | read_memory (lo, buf.data (), count); | |
f02df580 MS |
197 | |
198 | /* Have everything. Open/write the data. */ | |
199 | if (file_format == NULL || strcmp (file_format, "binary") == 0) | |
ee0c3293 | 200 | dump_binary_file (filename.get (), mode, buf.data (), count); |
f02df580 | 201 | else |
ee0c3293 | 202 | dump_bfd_file (filename.get (), mode, file_format, lo, buf.data (), count); |
f02df580 MS |
203 | } |
204 | ||
205 | static void | |
2d0ac106 | 206 | dump_memory_command (const char *cmd, const char *mode) |
f02df580 MS |
207 | { |
208 | dump_memory_to_file (cmd, mode, "binary"); | |
209 | } | |
210 | ||
211 | static void | |
93db0d79 | 212 | dump_value_to_file (const char *cmd, const char *mode, const char *file_format) |
f02df580 | 213 | { |
f02df580 | 214 | struct value *val; |
f02df580 MS |
215 | |
216 | /* Open the file. */ | |
ee0c3293 | 217 | gdb::unique_xmalloc_ptr<char> filename = scan_filename (&cmd, NULL); |
f02df580 MS |
218 | |
219 | /* Find the value. */ | |
220 | if (cmd == NULL || *cmd == '\0') | |
8a3fe4f8 | 221 | error (_("No value to %s."), *mode == 'a' ? "append" : "dump"); |
f02df580 MS |
222 | val = parse_and_eval (cmd); |
223 | if (val == NULL) | |
8a3fe4f8 | 224 | error (_("Invalid expression.")); |
f02df580 MS |
225 | |
226 | /* Have everything. Open/write the data. */ | |
227 | if (file_format == NULL || strcmp (file_format, "binary") == 0) | |
efaf1ae0 | 228 | dump_binary_file (filename.get (), mode, val->contents ().data (), |
d0c97917 | 229 | val->type ()->length ()); |
f02df580 MS |
230 | else |
231 | { | |
232 | CORE_ADDR vaddr; | |
233 | ||
736355f2 | 234 | if (val->lval ()) |
f02df580 | 235 | { |
9feb2d07 | 236 | vaddr = val->address (); |
f02df580 MS |
237 | } |
238 | else | |
239 | { | |
240 | vaddr = 0; | |
8a3fe4f8 | 241 | warning (_("value is not an lval: address assumed to be zero")); |
f02df580 MS |
242 | } |
243 | ||
ee0c3293 | 244 | dump_bfd_file (filename.get (), mode, file_format, vaddr, |
efaf1ae0 | 245 | val->contents ().data (), |
d0c97917 | 246 | val->type ()->length ()); |
f02df580 | 247 | } |
f02df580 MS |
248 | } |
249 | ||
250 | static void | |
2d0ac106 | 251 | dump_value_command (const char *cmd, const char *mode) |
f02df580 MS |
252 | { |
253 | dump_value_to_file (cmd, mode, "binary"); | |
254 | } | |
255 | ||
f02df580 | 256 | static void |
2d0ac106 | 257 | dump_srec_memory (const char *args, int from_tty) |
f02df580 | 258 | { |
5d1d95de | 259 | dump_memory_to_file (args, FOPEN_WB, "srec"); |
f02df580 MS |
260 | } |
261 | ||
262 | static void | |
2d0ac106 | 263 | dump_srec_value (const char *args, int from_tty) |
f02df580 | 264 | { |
5d1d95de | 265 | dump_value_to_file (args, FOPEN_WB, "srec"); |
f02df580 MS |
266 | } |
267 | ||
268 | static void | |
2d0ac106 | 269 | dump_ihex_memory (const char *args, int from_tty) |
f02df580 | 270 | { |
5d1d95de | 271 | dump_memory_to_file (args, FOPEN_WB, "ihex"); |
f02df580 MS |
272 | } |
273 | ||
274 | static void | |
2d0ac106 | 275 | dump_ihex_value (const char *args, int from_tty) |
f02df580 | 276 | { |
5d1d95de | 277 | dump_value_to_file (args, FOPEN_WB, "ihex"); |
f02df580 MS |
278 | } |
279 | ||
cf75d6c3 | 280 | static void |
2d0ac106 | 281 | dump_verilog_memory (const char *args, int from_tty) |
cf75d6c3 AB |
282 | { |
283 | dump_memory_to_file (args, FOPEN_WB, "verilog"); | |
284 | } | |
285 | ||
286 | static void | |
2d0ac106 | 287 | dump_verilog_value (const char *args, int from_tty) |
cf75d6c3 AB |
288 | { |
289 | dump_value_to_file (args, FOPEN_WB, "verilog"); | |
290 | } | |
291 | ||
f02df580 | 292 | static void |
2d0ac106 | 293 | dump_tekhex_memory (const char *args, int from_tty) |
f02df580 | 294 | { |
5d1d95de | 295 | dump_memory_to_file (args, FOPEN_WB, "tekhex"); |
f02df580 MS |
296 | } |
297 | ||
298 | static void | |
2d0ac106 | 299 | dump_tekhex_value (const char *args, int from_tty) |
f02df580 | 300 | { |
5d1d95de | 301 | dump_value_to_file (args, FOPEN_WB, "tekhex"); |
f02df580 MS |
302 | } |
303 | ||
304 | static void | |
2d0ac106 | 305 | dump_binary_memory (const char *args, int from_tty) |
f02df580 | 306 | { |
5d1d95de | 307 | dump_memory_to_file (args, FOPEN_WB, "binary"); |
f02df580 MS |
308 | } |
309 | ||
310 | static void | |
2d0ac106 | 311 | dump_binary_value (const char *args, int from_tty) |
f02df580 | 312 | { |
5d1d95de | 313 | dump_value_to_file (args, FOPEN_WB, "binary"); |
f02df580 MS |
314 | } |
315 | ||
316 | static void | |
2d0ac106 | 317 | append_binary_memory (const char *args, int from_tty) |
f02df580 | 318 | { |
5d1d95de | 319 | dump_memory_to_file (args, FOPEN_AB, "binary"); |
f02df580 MS |
320 | } |
321 | ||
322 | static void | |
2d0ac106 | 323 | append_binary_value (const char *args, int from_tty) |
f02df580 | 324 | { |
5d1d95de | 325 | dump_value_to_file (args, FOPEN_AB, "binary"); |
f02df580 MS |
326 | } |
327 | ||
328 | struct dump_context | |
329 | { | |
2d0ac106 | 330 | void (*func) (const char *cmd, const char *mode); |
a121b7c1 | 331 | const char *mode; |
f02df580 MS |
332 | }; |
333 | ||
334 | static void | |
5538b03c | 335 | call_dump_func (const char *args, int from_tty, cmd_list_element *c) |
f02df580 | 336 | { |
0f8e2034 | 337 | struct dump_context *d = (struct dump_context *) c->context (); |
cdb27c12 | 338 | |
f02df580 MS |
339 | d->func (args, d->mode); |
340 | } | |
341 | ||
db229724 | 342 | static void |
a121b7c1 | 343 | add_dump_command (const char *name, |
2d0ac106 | 344 | void (*func) (const char *args, const char *mode), |
a121b7c1 | 345 | const char *descr) |
f02df580 MS |
346 | |
347 | { | |
348 | struct cmd_list_element *c; | |
349 | struct dump_context *d; | |
350 | ||
0450cc4c | 351 | c = add_cmd (name, all_commands, descr, &dump_cmdlist); |
f02df580 | 352 | c->completer = filename_completer; |
70ba0933 | 353 | d = XNEW (struct dump_context); |
f02df580 | 354 | d->func = func; |
5d1d95de | 355 | d->mode = FOPEN_WB; |
0f8e2034 | 356 | c->set_context (d); |
f02df580 MS |
357 | c->func = call_dump_func; |
358 | ||
0450cc4c | 359 | c = add_cmd (name, all_commands, descr, &append_cmdlist); |
f02df580 | 360 | c->completer = filename_completer; |
70ba0933 | 361 | d = XNEW (struct dump_context); |
f02df580 | 362 | d->func = func; |
5d1d95de | 363 | d->mode = FOPEN_AB; |
0f8e2034 | 364 | c->set_context (d); |
f02df580 MS |
365 | c->func = call_dump_func; |
366 | ||
cb1a6d5f | 367 | /* Replace "Dump " at start of docstring with "Append " (borrowed |
eefe576e | 368 | from [deleted] deprecated_add_show_from_set). */ |
f02df580 MS |
369 | if ( c->doc[0] == 'W' |
370 | && c->doc[1] == 'r' | |
371 | && c->doc[2] == 'i' | |
372 | && c->doc[3] == 't' | |
373 | && c->doc[4] == 'e' | |
374 | && c->doc[5] == ' ') | |
1754f103 | 375 | c->doc = concat ("Append ", c->doc + 6, (char *)NULL); |
f02df580 MS |
376 | } |
377 | ||
03cd72b8 | 378 | /* Selectively loads the sections into memory. */ |
f02df580 MS |
379 | |
380 | static void | |
03cd72b8 TT |
381 | restore_one_section (bfd *ibfd, asection *isec, |
382 | CORE_ADDR load_offset, | |
383 | CORE_ADDR load_start, | |
384 | CORE_ADDR load_end) | |
f02df580 | 385 | { |
fd361982 AM |
386 | bfd_vma sec_start = bfd_section_vma (isec); |
387 | bfd_size_type size = bfd_section_size (isec); | |
f02df580 MS |
388 | bfd_vma sec_end = sec_start + size; |
389 | bfd_size_type sec_offset = 0; | |
390 | bfd_size_type sec_load_count = size; | |
f02df580 MS |
391 | int ret; |
392 | ||
ebcd3b23 | 393 | /* Ignore non-loadable sections, eg. from elf files. */ |
fd361982 | 394 | if (!(bfd_section_flags (isec) & SEC_LOAD)) |
f02df580 MS |
395 | return; |
396 | ||
397 | /* Does the section overlap with the desired restore range? */ | |
03cd72b8 TT |
398 | if (sec_end <= load_start |
399 | || (load_end > 0 && sec_start >= load_end)) | |
f02df580 | 400 | { |
ebcd3b23 | 401 | /* No, no useable data in this section. */ |
6cb06a8c TT |
402 | gdb_printf (_("skipping section %s...\n"), |
403 | bfd_section_name (isec)); | |
f02df580 MS |
404 | return; |
405 | } | |
406 | ||
407 | /* Compare section address range with user-requested | |
408 | address range (if any). Compute where the actual | |
409 | transfer should start and end. */ | |
03cd72b8 TT |
410 | if (sec_start < load_start) |
411 | sec_offset = load_start - sec_start; | |
ebcd3b23 | 412 | /* Size of a partial transfer. */ |
f02df580 | 413 | sec_load_count -= sec_offset; |
03cd72b8 TT |
414 | if (load_end > 0 && sec_end > load_end) |
415 | sec_load_count -= sec_end - load_end; | |
f02df580 MS |
416 | |
417 | /* Get the data. */ | |
26fcd5d7 TT |
418 | gdb::byte_vector buf (size); |
419 | if (!bfd_get_section_contents (ibfd, isec, buf.data (), 0, size)) | |
8a3fe4f8 | 420 | error (_("Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd), |
f02df580 MS |
421 | bfd_errmsg (bfd_get_error ())); |
422 | ||
6cb06a8c TT |
423 | gdb_printf ("Restoring section %s (0x%lx to 0x%lx)", |
424 | bfd_section_name (isec), | |
425 | (unsigned long) sec_start, | |
426 | (unsigned long) sec_end); | |
f02df580 | 427 | |
03cd72b8 | 428 | if (load_offset != 0 || load_start != 0 || load_end != 0) |
6cb06a8c | 429 | gdb_printf (" into memory (%s to %s)\n", |
99d9c3b9 | 430 | paddress (current_inferior ()->arch (), |
6cb06a8c TT |
431 | (unsigned long) sec_start |
432 | + sec_offset + load_offset), | |
99d9c3b9 | 433 | paddress (current_inferior ()->arch (), |
6cb06a8c TT |
434 | (unsigned long) sec_start + sec_offset |
435 | + load_offset + sec_load_count)); | |
f02df580 | 436 | else |
0426ad51 | 437 | gdb_puts ("\n"); |
f02df580 MS |
438 | |
439 | /* Write the data. */ | |
03cd72b8 | 440 | ret = target_write_memory (sec_start + sec_offset + load_offset, |
26fcd5d7 | 441 | &buf[sec_offset], sec_load_count); |
f02df580 | 442 | if (ret != 0) |
8a3fe4f8 | 443 | warning (_("restore: memory write failed (%s)."), safe_strerror (ret)); |
f02df580 MS |
444 | } |
445 | ||
446 | static void | |
03cd72b8 TT |
447 | restore_binary_file (const char *filename, CORE_ADDR load_offset, |
448 | CORE_ADDR load_start, CORE_ADDR load_end) | |
449 | ||
f02df580 | 450 | { |
d419f42d | 451 | gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB); |
f02df580 MS |
452 | long len; |
453 | ||
94c18618 SDJ |
454 | if (file == NULL) |
455 | error (_("Failed to open %s: %s"), filename, safe_strerror (errno)); | |
456 | ||
f02df580 | 457 | /* Get the file size for reading. */ |
d419f42d | 458 | if (fseek (file.get (), 0, SEEK_END) == 0) |
5e9e105f | 459 | { |
d419f42d | 460 | len = ftell (file.get ()); |
5e9e105f MS |
461 | if (len < 0) |
462 | perror_with_name (filename); | |
463 | } | |
f02df580 MS |
464 | else |
465 | perror_with_name (filename); | |
466 | ||
03cd72b8 | 467 | if (len <= load_start) |
8a3fe4f8 | 468 | error (_("Start address is greater than length of binary file %s."), |
f02df580 MS |
469 | filename); |
470 | ||
ebcd3b23 | 471 | /* Chop off "len" if it exceeds the requested load_end addr. */ |
03cd72b8 TT |
472 | if (load_end != 0 && load_end < len) |
473 | len = load_end; | |
ebcd3b23 | 474 | /* Chop off "len" if the requested load_start addr skips some bytes. */ |
03cd72b8 TT |
475 | if (load_start > 0) |
476 | len -= load_start; | |
f02df580 | 477 | |
6cb06a8c | 478 | gdb_printf |
f02df580 MS |
479 | ("Restoring binary file %s into memory (0x%lx to 0x%lx)\n", |
480 | filename, | |
03cd72b8 TT |
481 | (unsigned long) (load_start + load_offset), |
482 | (unsigned long) (load_start + load_offset + len)); | |
f02df580 MS |
483 | |
484 | /* Now set the file pos to the requested load start pos. */ | |
03cd72b8 | 485 | if (fseek (file.get (), load_start, SEEK_SET) != 0) |
f02df580 MS |
486 | perror_with_name (filename); |
487 | ||
488 | /* Now allocate a buffer and read the file contents. */ | |
d5722aa2 | 489 | gdb::byte_vector buf (len); |
d419f42d | 490 | if (fread (buf.data (), 1, len, file.get ()) != len) |
f02df580 MS |
491 | perror_with_name (filename); |
492 | ||
ebcd3b23 | 493 | /* Now write the buffer into target memory. */ |
03cd72b8 | 494 | len = target_write_memory (load_start + load_offset, buf.data (), len); |
f02df580 | 495 | if (len != 0) |
8a3fe4f8 | 496 | warning (_("restore: memory write failed (%s)."), safe_strerror (len)); |
f02df580 MS |
497 | } |
498 | ||
499 | static void | |
0b39b52e | 500 | restore_command (const char *args, int from_tty) |
f02df580 | 501 | { |
f02df580 MS |
502 | int binary_flag = 0; |
503 | ||
55f6301a | 504 | if (!target_has_execution ()) |
f02df580 MS |
505 | noprocess (); |
506 | ||
03cd72b8 TT |
507 | CORE_ADDR load_offset = 0; |
508 | CORE_ADDR load_start = 0; | |
509 | CORE_ADDR load_end = 0; | |
f02df580 | 510 | |
ebcd3b23 | 511 | /* Parse the input arguments. First is filename (required). */ |
ee0c3293 | 512 | gdb::unique_xmalloc_ptr<char> filename = scan_filename (&args, NULL); |
f02df580 MS |
513 | if (args != NULL && *args != '\0') |
514 | { | |
a121b7c1 | 515 | static const char binary_string[] = "binary"; |
f02df580 MS |
516 | |
517 | /* Look for optional "binary" flag. */ | |
61012eef | 518 | if (startswith (args, binary_string)) |
f02df580 MS |
519 | { |
520 | binary_flag = 1; | |
521 | args += strlen (binary_string); | |
f1735a53 | 522 | args = skip_spaces (args); |
f02df580 | 523 | } |
ebcd3b23 | 524 | /* Parse offset (optional). */ |
f02df580 | 525 | if (args != NULL && *args != '\0') |
03cd72b8 TT |
526 | load_offset |
527 | = (binary_flag | |
528 | ? parse_and_eval_address (scan_expression (&args, NULL).get ()) | |
529 | : parse_and_eval_long (scan_expression (&args, NULL).get ())); | |
f02df580 MS |
530 | if (args != NULL && *args != '\0') |
531 | { | |
ebcd3b23 | 532 | /* Parse start address (optional). */ |
03cd72b8 | 533 | load_start = |
ee0c3293 | 534 | parse_and_eval_long (scan_expression (&args, NULL).get ()); |
f02df580 MS |
535 | if (args != NULL && *args != '\0') |
536 | { | |
ebcd3b23 | 537 | /* Parse end address (optional). */ |
03cd72b8 TT |
538 | load_end = parse_and_eval_long (args); |
539 | if (load_end <= load_start) | |
8a3fe4f8 | 540 | error (_("Start must be less than end.")); |
f02df580 MS |
541 | } |
542 | } | |
543 | } | |
544 | ||
545 | if (info_verbose) | |
6cb06a8c TT |
546 | gdb_printf ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n", |
547 | filename.get (), (unsigned long) load_offset, | |
548 | (unsigned long) load_start, | |
549 | (unsigned long) load_end); | |
f02df580 MS |
550 | |
551 | if (binary_flag) | |
552 | { | |
03cd72b8 TT |
553 | restore_binary_file (filename.get (), load_offset, load_start, |
554 | load_end); | |
f02df580 MS |
555 | } |
556 | else | |
557 | { | |
ebcd3b23 | 558 | /* Open the file for loading. */ |
ee0c3293 | 559 | gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename.get (), NULL)); |
f02df580 | 560 | |
ebcd3b23 | 561 | /* Process the sections. */ |
03cd72b8 TT |
562 | for (asection *sect : gdb_bfd_sections (ibfd)) |
563 | restore_one_section (ibfd.get (), sect, load_offset, load_start, | |
564 | load_end); | |
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."), | |
2f822da5 | 576 | &dump_cmdlist, |
0743fc83 TT |
577 | 0/*allow-unknown*/, |
578 | &cmdlist); | |
579 | add_basic_prefix_cmd ("append", class_vars, | |
580 | _("Append target code/data to a local file."), | |
2f822da5 | 581 | &append_cmdlist, |
0743fc83 TT |
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."), | |
2f822da5 | 597 | &srec_cmdlist, |
0743fc83 TT |
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."), | |
2f822da5 | 603 | &ihex_cmdlist, |
0743fc83 TT |
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."), | |
2f822da5 | 609 | &verilog_cmdlist, |
0743fc83 TT |
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."), | |
2f822da5 | 615 | &tekhex_cmdlist, |
0743fc83 TT |
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."), | |
2f822da5 | 621 | &binary_dump_cmdlist, |
0743fc83 TT |
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."), | |
2f822da5 | 627 | &binary_append_cmdlist, |
0743fc83 TT |
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 | } |