1 /* Dump-to-file commands, for GDB, the GNU debugger.
3 Copyright (C) 2002-2013 Free Software Foundation, Inc.
5 Contributed by Red Hat.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
24 #include "cli/cli-decode.h"
25 #include "cli/cli-cmds.h"
27 #include "completer.h"
28 #include "gdb_assert.h"
31 #include "readline/readline.h"
33 #include "cli/cli-utils.h"
35 #include "filestuff.h"
37 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
41 scan_expression_with_cleanup (char **cmd, const char *def)
43 if ((*cmd) == NULL || (**cmd) == '\0')
45 char *exp = xstrdup (def);
47 make_cleanup (xfree, exp);
55 end = (*cmd) + strcspn (*cmd, " \t");
56 exp = savestring ((*cmd), end - (*cmd));
57 make_cleanup (xfree, exp);
58 (*cmd) = skip_spaces (end);
65 scan_filename_with_cleanup (char **cmd, const char *defname)
70 /* FIXME: Need to get the ``/a(ppend)'' flag from somewhere. */
76 error (_("Missing filename."));
77 filename = xstrdup (defname);
78 make_cleanup (xfree, filename);
82 /* FIXME: should parse a possibly quoted string. */
85 (*cmd) = skip_spaces (*cmd);
86 end = *cmd + strcspn (*cmd, " \t");
87 filename = savestring ((*cmd), end - (*cmd));
88 make_cleanup (xfree, filename);
89 (*cmd) = skip_spaces (end);
91 gdb_assert (filename != NULL);
93 fullname = tilde_expand (filename);
94 make_cleanup (xfree, fullname);
100 fopen_with_cleanup (const char *filename, const char *mode)
102 FILE *file = gdb_fopen_cloexec (filename, mode);
105 perror_with_name (filename);
106 make_cleanup_fclose (file);
111 bfd_openr_with_cleanup (const char *filename, const char *target)
115 ibfd = gdb_bfd_openr (filename, target);
117 error (_("Failed to open %s: %s."), filename,
118 bfd_errmsg (bfd_get_error ()));
120 make_cleanup_bfd_unref (ibfd);
121 if (!bfd_check_format (ibfd, bfd_object))
122 error (_("'%s' is not a recognized file format."), filename);
128 bfd_openw_with_cleanup (const char *filename, const char *target,
133 if (*mode == 'w') /* Write: create new file */
135 obfd = gdb_bfd_openw (filename, target);
137 error (_("Failed to open %s: %s."), filename,
138 bfd_errmsg (bfd_get_error ()));
139 make_cleanup_bfd_unref (obfd);
140 if (!bfd_set_format (obfd, bfd_object))
141 error (_("bfd_openw_with_cleanup: %s."), bfd_errmsg (bfd_get_error ()));
143 else if (*mode == 'a') /* Append to existing file. */
144 { /* FIXME -- doesn't work... */
145 error (_("bfd_openw does not work with append."));
148 error (_("bfd_openw_with_cleanup: unknown mode %s."), mode);
153 static struct cmd_list_element *dump_cmdlist;
154 static struct cmd_list_element *append_cmdlist;
155 static struct cmd_list_element *srec_cmdlist;
156 static struct cmd_list_element *ihex_cmdlist;
157 static struct cmd_list_element *tekhex_cmdlist;
158 static struct cmd_list_element *binary_dump_cmdlist;
159 static struct cmd_list_element *binary_append_cmdlist;
162 dump_command (char *cmd, int from_tty)
164 printf_unfiltered (_("\"dump\" must be followed by a subcommand.\n\n"));
165 help_list (dump_cmdlist, "dump ", -1, gdb_stdout);
169 append_command (char *cmd, int from_tty)
171 printf_unfiltered (_("\"append\" must be followed by a subcommand.\n\n"));
172 help_list (dump_cmdlist, "append ", -1, gdb_stdout);
176 dump_binary_file (const char *filename, const char *mode,
177 const bfd_byte *buf, ULONGEST len)
182 file = fopen_with_cleanup (filename, mode);
183 status = fwrite (buf, len, 1, file);
185 perror_with_name (filename);
189 dump_bfd_file (const char *filename, const char *mode,
190 const char *target, CORE_ADDR vaddr,
191 const bfd_byte *buf, ULONGEST len)
196 obfd = bfd_openw_with_cleanup (filename, target, mode);
197 osection = bfd_make_section_anyway (obfd, ".newsec");
198 bfd_set_section_size (obfd, osection, len);
199 bfd_set_section_vma (obfd, osection, vaddr);
200 bfd_set_section_alignment (obfd, osection, 0);
201 bfd_set_section_flags (obfd, osection, (SEC_HAS_CONTENTS
204 osection->entsize = 0;
205 if (!bfd_set_section_contents (obfd, osection, buf, 0, len))
206 warning (_("writing dump file '%s' (%s)"), filename,
207 bfd_errmsg (bfd_get_error ()));
211 dump_memory_to_file (char *cmd, char *mode, char *file_format)
213 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
223 filename = scan_filename_with_cleanup (&cmd, NULL);
225 /* Find the low address. */
226 if (cmd == NULL || *cmd == '\0')
227 error (_("Missing start address."));
228 lo_exp = scan_expression_with_cleanup (&cmd, NULL);
230 /* Find the second address - rest of line. */
231 if (cmd == NULL || *cmd == '\0')
232 error (_("Missing stop address."));
235 lo = parse_and_eval_address (lo_exp);
236 hi = parse_and_eval_address (hi_exp);
238 error (_("Invalid memory address range (start >= end)."));
241 /* FIXME: Should use read_memory_partial() and a magic blocking
243 buf = xmalloc (count);
244 make_cleanup (xfree, buf);
245 read_memory (lo, buf, count);
247 /* Have everything. Open/write the data. */
248 if (file_format == NULL || strcmp (file_format, "binary") == 0)
250 dump_binary_file (filename, mode, buf, count);
254 dump_bfd_file (filename, mode, file_format, lo, buf, count);
257 do_cleanups (old_cleanups);
261 dump_memory_command (char *cmd, char *mode)
263 dump_memory_to_file (cmd, mode, "binary");
267 dump_value_to_file (char *cmd, char *mode, char *file_format)
269 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
274 filename = scan_filename_with_cleanup (&cmd, NULL);
276 /* Find the value. */
277 if (cmd == NULL || *cmd == '\0')
278 error (_("No value to %s."), *mode == 'a' ? "append" : "dump");
279 val = parse_and_eval (cmd);
281 error (_("Invalid expression."));
283 /* Have everything. Open/write the data. */
284 if (file_format == NULL || strcmp (file_format, "binary") == 0)
286 dump_binary_file (filename, mode, value_contents (val),
287 TYPE_LENGTH (value_type (val)));
293 if (VALUE_LVAL (val))
295 vaddr = value_address (val);
300 warning (_("value is not an lval: address assumed to be zero"));
303 dump_bfd_file (filename, mode, file_format, vaddr,
304 value_contents (val),
305 TYPE_LENGTH (value_type (val)));
308 do_cleanups (old_cleanups);
312 dump_value_command (char *cmd, char *mode)
314 dump_value_to_file (cmd, mode, "binary");
318 dump_srec_memory (char *args, int from_tty)
320 dump_memory_to_file (args, FOPEN_WB, "srec");
324 dump_srec_value (char *args, int from_tty)
326 dump_value_to_file (args, FOPEN_WB, "srec");
330 dump_ihex_memory (char *args, int from_tty)
332 dump_memory_to_file (args, FOPEN_WB, "ihex");
336 dump_ihex_value (char *args, int from_tty)
338 dump_value_to_file (args, FOPEN_WB, "ihex");
342 dump_tekhex_memory (char *args, int from_tty)
344 dump_memory_to_file (args, FOPEN_WB, "tekhex");
348 dump_tekhex_value (char *args, int from_tty)
350 dump_value_to_file (args, FOPEN_WB, "tekhex");
354 dump_binary_memory (char *args, int from_tty)
356 dump_memory_to_file (args, FOPEN_WB, "binary");
360 dump_binary_value (char *args, int from_tty)
362 dump_value_to_file (args, FOPEN_WB, "binary");
366 append_binary_memory (char *args, int from_tty)
368 dump_memory_to_file (args, FOPEN_AB, "binary");
372 append_binary_value (char *args, int from_tty)
374 dump_value_to_file (args, FOPEN_AB, "binary");
379 void (*func) (char *cmd, char *mode);
384 call_dump_func (struct cmd_list_element *c, char *args, int from_tty)
386 struct dump_context *d = get_cmd_context (c);
388 d->func (args, d->mode);
392 add_dump_command (char *name, void (*func) (char *args, char *mode),
396 struct cmd_list_element *c;
397 struct dump_context *d;
399 c = add_cmd (name, all_commands, NULL, descr, &dump_cmdlist);
400 c->completer = filename_completer;
401 d = XMALLOC (struct dump_context);
404 set_cmd_context (c, d);
405 c->func = call_dump_func;
407 c = add_cmd (name, all_commands, NULL, descr, &append_cmdlist);
408 c->completer = filename_completer;
409 d = XMALLOC (struct dump_context);
412 set_cmd_context (c, d);
413 c->func = call_dump_func;
415 /* Replace "Dump " at start of docstring with "Append " (borrowed
416 from [deleted] deprecated_add_show_from_set). */
417 if ( c->doc[0] == 'W'
423 c->doc = concat ("Append ", c->doc + 6, (char *)NULL);
426 /* Opaque data for restore_section_callback. */
427 struct callback_data {
428 CORE_ADDR load_offset;
429 CORE_ADDR load_start;
433 /* Function: restore_section_callback.
435 Callback function for bfd_map_over_sections.
436 Selectively loads the sections into memory. */
439 restore_section_callback (bfd *ibfd, asection *isec, void *args)
441 struct callback_data *data = args;
442 bfd_vma sec_start = bfd_section_vma (ibfd, isec);
443 bfd_size_type size = bfd_section_size (ibfd, isec);
444 bfd_vma sec_end = sec_start + size;
445 bfd_size_type sec_offset = 0;
446 bfd_size_type sec_load_count = size;
447 struct cleanup *old_chain;
451 /* Ignore non-loadable sections, eg. from elf files. */
452 if (!(bfd_get_section_flags (ibfd, isec) & SEC_LOAD))
455 /* Does the section overlap with the desired restore range? */
456 if (sec_end <= data->load_start
457 || (data->load_end > 0 && sec_start >= data->load_end))
459 /* No, no useable data in this section. */
460 printf_filtered (_("skipping section %s...\n"),
461 bfd_section_name (ibfd, isec));
465 /* Compare section address range with user-requested
466 address range (if any). Compute where the actual
467 transfer should start and end. */
468 if (sec_start < data->load_start)
469 sec_offset = data->load_start - sec_start;
470 /* Size of a partial transfer. */
471 sec_load_count -= sec_offset;
472 if (data->load_end > 0 && sec_end > data->load_end)
473 sec_load_count -= sec_end - data->load_end;
476 buf = xmalloc (size);
477 old_chain = make_cleanup (xfree, buf);
478 if (!bfd_get_section_contents (ibfd, isec, buf, 0, size))
479 error (_("Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd),
480 bfd_errmsg (bfd_get_error ()));
482 printf_filtered ("Restoring section %s (0x%lx to 0x%lx)",
483 bfd_section_name (ibfd, isec),
484 (unsigned long) sec_start,
485 (unsigned long) sec_end);
487 if (data->load_offset != 0 || data->load_start != 0 || data->load_end != 0)
488 printf_filtered (" into memory (%s to %s)\n",
489 paddress (target_gdbarch (),
490 (unsigned long) sec_start
491 + sec_offset + data->load_offset),
492 paddress (target_gdbarch (),
493 (unsigned long) sec_start + sec_offset
494 + data->load_offset + sec_load_count));
496 puts_filtered ("\n");
498 /* Write the data. */
499 ret = target_write_memory (sec_start + sec_offset + data->load_offset,
500 buf + sec_offset, sec_load_count);
502 warning (_("restore: memory write failed (%s)."), safe_strerror (ret));
503 do_cleanups (old_chain);
508 restore_binary_file (char *filename, struct callback_data *data)
510 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
511 FILE *file = fopen_with_cleanup (filename, FOPEN_RB);
515 /* Get the file size for reading. */
516 if (fseek (file, 0, SEEK_END) == 0)
520 perror_with_name (filename);
523 perror_with_name (filename);
525 if (len <= data->load_start)
526 error (_("Start address is greater than length of binary file %s."),
529 /* Chop off "len" if it exceeds the requested load_end addr. */
530 if (data->load_end != 0 && data->load_end < len)
531 len = data->load_end;
532 /* Chop off "len" if the requested load_start addr skips some bytes. */
533 if (data->load_start > 0)
534 len -= data->load_start;
537 ("Restoring binary file %s into memory (0x%lx to 0x%lx)\n",
539 (unsigned long) (data->load_start + data->load_offset),
540 (unsigned long) (data->load_start + data->load_offset + len));
542 /* Now set the file pos to the requested load start pos. */
543 if (fseek (file, data->load_start, SEEK_SET) != 0)
544 perror_with_name (filename);
546 /* Now allocate a buffer and read the file contents. */
548 make_cleanup (xfree, buf);
549 if (fread (buf, 1, len, file) != len)
550 perror_with_name (filename);
552 /* Now write the buffer into target memory. */
553 len = target_write_memory (data->load_start + data->load_offset, buf, len);
555 warning (_("restore: memory write failed (%s)."), safe_strerror (len));
556 do_cleanups (cleanup);
560 restore_command (char *args, int from_tty)
563 struct callback_data data;
567 if (!target_has_execution)
570 data.load_offset = 0;
574 /* Parse the input arguments. First is filename (required). */
575 filename = scan_filename_with_cleanup (&args, NULL);
576 if (args != NULL && *args != '\0')
578 char *binary_string = "binary";
580 /* Look for optional "binary" flag. */
581 if (strncmp (args, binary_string, strlen (binary_string)) == 0)
584 args += strlen (binary_string);
585 args = skip_spaces (args);
587 /* Parse offset (optional). */
588 if (args != NULL && *args != '\0')
590 parse_and_eval_address (scan_expression_with_cleanup (&args, NULL));
591 if (args != NULL && *args != '\0')
593 /* Parse start address (optional). */
595 parse_and_eval_long (scan_expression_with_cleanup (&args, NULL));
596 if (args != NULL && *args != '\0')
598 /* Parse end address (optional). */
599 data.load_end = parse_and_eval_long (args);
600 if (data.load_end <= data.load_start)
601 error (_("Start must be less than end."));
607 printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n",
608 filename, (unsigned long) data.load_offset,
609 (unsigned long) data.load_start,
610 (unsigned long) data.load_end);
614 restore_binary_file (filename, &data);
618 /* Open the file for loading. */
619 ibfd = bfd_openr_with_cleanup (filename, NULL);
621 /* Process the sections. */
622 bfd_map_over_sections (ibfd, restore_section_callback, &data);
628 srec_dump_command (char *cmd, int from_tty)
630 printf_unfiltered ("\"dump srec\" must be followed by a subcommand.\n");
631 help_list (srec_cmdlist, "dump srec ", -1, gdb_stdout);
635 ihex_dump_command (char *cmd, int from_tty)
637 printf_unfiltered ("\"dump ihex\" must be followed by a subcommand.\n");
638 help_list (ihex_cmdlist, "dump ihex ", -1, gdb_stdout);
642 tekhex_dump_command (char *cmd, int from_tty)
644 printf_unfiltered ("\"dump tekhex\" must be followed by a subcommand.\n");
645 help_list (tekhex_cmdlist, "dump tekhex ", -1, gdb_stdout);
649 binary_dump_command (char *cmd, int from_tty)
651 printf_unfiltered ("\"dump binary\" must be followed by a subcommand.\n");
652 help_list (binary_dump_cmdlist, "dump binary ", -1, gdb_stdout);
656 binary_append_command (char *cmd, int from_tty)
658 printf_unfiltered ("\"append binary\" must be followed by a subcommand.\n");
659 help_list (binary_append_cmdlist, "append binary ", -1, gdb_stdout);
662 extern initialize_file_ftype _initialize_cli_dump; /* -Wmissing-prototypes */
665 _initialize_cli_dump (void)
667 struct cmd_list_element *c;
669 add_prefix_cmd ("dump", class_vars, dump_command,
670 _("Dump target code/data to a local file."),
671 &dump_cmdlist, "dump ",
674 add_prefix_cmd ("append", class_vars, append_command,
675 _("Append target code/data to a local file."),
676 &append_cmdlist, "append ",
680 add_dump_command ("memory", dump_memory_command, "\
681 Write contents of memory to a raw binary file.\n\
682 Arguments are FILE START STOP. Writes the contents of memory within the\n\
683 range [START .. STOP) to the specified FILE in raw target ordered bytes.");
685 add_dump_command ("value", dump_value_command, "\
686 Write the value of an expression to a raw binary file.\n\
687 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION to\n\
688 the specified FILE in raw target ordered bytes.");
690 add_prefix_cmd ("srec", all_commands, srec_dump_command,
691 _("Write target code/data to an srec file."),
692 &srec_cmdlist, "dump srec ",
696 add_prefix_cmd ("ihex", all_commands, ihex_dump_command,
697 _("Write target code/data to an intel hex file."),
698 &ihex_cmdlist, "dump ihex ",
702 add_prefix_cmd ("tekhex", all_commands, tekhex_dump_command,
703 _("Write target code/data to a tekhex file."),
704 &tekhex_cmdlist, "dump tekhex ",
708 add_prefix_cmd ("binary", all_commands, binary_dump_command,
709 _("Write target code/data to a raw binary file."),
710 &binary_dump_cmdlist, "dump binary ",
714 add_prefix_cmd ("binary", all_commands, binary_append_command,
715 _("Append target code/data to a raw binary file."),
716 &binary_append_cmdlist, "append binary ",
720 add_cmd ("memory", all_commands, dump_srec_memory, _("\
721 Write contents of memory to an srec file.\n\
722 Arguments are FILE START STOP. Writes the contents of memory\n\
723 within the range [START .. STOP) to the specified FILE in srec format."),
726 add_cmd ("value", all_commands, dump_srec_value, _("\
727 Write the value of an expression to an srec file.\n\
728 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
729 to the specified FILE in srec format."),
732 add_cmd ("memory", all_commands, dump_ihex_memory, _("\
733 Write contents of memory to an ihex file.\n\
734 Arguments are FILE START STOP. Writes the contents of memory within\n\
735 the range [START .. STOP) to the specified FILE in intel hex format."),
738 add_cmd ("value", all_commands, dump_ihex_value, _("\
739 Write the value of an expression to an ihex file.\n\
740 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
741 to the specified FILE in intel hex format."),
744 add_cmd ("memory", all_commands, dump_tekhex_memory, _("\
745 Write contents of memory to a tekhex file.\n\
746 Arguments are FILE START STOP. Writes the contents of memory\n\
747 within the range [START .. STOP) to the specified FILE in tekhex format."),
750 add_cmd ("value", all_commands, dump_tekhex_value, _("\
751 Write the value of an expression to a tekhex file.\n\
752 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
753 to the specified FILE in tekhex format."),
756 add_cmd ("memory", all_commands, dump_binary_memory, _("\
757 Write contents of memory to a raw binary file.\n\
758 Arguments are FILE START STOP. Writes the contents of memory\n\
759 within the range [START .. STOP) to the specified FILE in binary format."),
760 &binary_dump_cmdlist);
762 add_cmd ("value", all_commands, dump_binary_value, _("\
763 Write the value of an expression to a raw binary file.\n\
764 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
765 to the specified FILE in raw target ordered bytes."),
766 &binary_dump_cmdlist);
768 add_cmd ("memory", all_commands, append_binary_memory, _("\
769 Append contents of memory to a raw binary file.\n\
770 Arguments are FILE START STOP. Writes the contents of memory within the\n\
771 range [START .. STOP) to the specified FILE in raw target ordered bytes."),
772 &binary_append_cmdlist);
774 add_cmd ("value", all_commands, append_binary_value, _("\
775 Append the value of an expression to a raw binary file.\n\
776 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
777 to the specified FILE in raw target ordered bytes."),
778 &binary_append_cmdlist);
780 c = add_com ("restore", class_vars, restore_command, _("\
781 Restore the contents of FILE to target memory.\n\
782 Arguments are FILE OFFSET START END where all except FILE are optional.\n\
783 OFFSET will be added to the base address of the file (default zero).\n\
784 If START and END are given, only the file contents within that range\n\
785 (file relative) will be restored to target memory."));
786 c->completer = filename_completer;
787 /* FIXME: completers for other commands. */