1 /* Dump-to-file commands, for GDB, the GNU debugger.
3 Copyright 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
26 #include "cli/cli-decode.h"
27 #include "cli/cli-cmds.h"
29 #include "completer.h"
30 #include "cli/cli-dump.h"
31 #include "gdb_assert.h"
34 #include "readline/readline.h"
36 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
40 skip_spaces (char *chp
)
44 while (isspace (*chp
))
50 scan_expression_with_cleanup (char **cmd
, const char *def
)
52 if ((*cmd
) == NULL
|| (**cmd
) == '\0')
54 char *exp
= xstrdup (def
);
55 make_cleanup (xfree
, exp
);
63 end
= (*cmd
) + strcspn (*cmd
, " \t");
64 exp
= savestring ((*cmd
), end
- (*cmd
));
65 make_cleanup (xfree
, exp
);
66 (*cmd
) = skip_spaces (end
);
73 do_fclose_cleanup (void *arg
)
79 static struct cleanup
*
80 make_cleanup_fclose (FILE *file
)
82 return make_cleanup (do_fclose_cleanup
, file
);
86 scan_filename_with_cleanup (char **cmd
, const char *defname
)
91 /* FIXME: Need to get the ``/a(ppend)'' flag from somewhere. */
97 error ("Missing filename.");
98 filename
= xstrdup (defname
);
99 make_cleanup (xfree
, filename
);
103 /* FIXME: should parse a possibly quoted string. */
106 (*cmd
) = skip_spaces (*cmd
);
107 end
= *cmd
+ strcspn (*cmd
, " \t");
108 filename
= savestring ((*cmd
), end
- (*cmd
));
109 make_cleanup (xfree
, filename
);
110 (*cmd
) = skip_spaces (end
);
112 gdb_assert (filename
!= NULL
);
114 fullname
= tilde_expand (filename
);
115 make_cleanup (xfree
, fullname
);
121 fopen_with_cleanup (char *filename
, const char *mode
)
123 FILE *file
= fopen (filename
, mode
);
125 perror_with_name (filename
);
126 make_cleanup_fclose (file
);
131 bfd_openr_with_cleanup (const char *filename
, const char *target
)
135 ibfd
= bfd_openr (filename
, target
);
137 error ("Failed to open %s: %s.", filename
,
138 bfd_errmsg (bfd_get_error ()));
140 make_cleanup_bfd_close (ibfd
);
141 if (!bfd_check_format (ibfd
, bfd_object
))
142 error ("'%s' is not a recognized file format.", filename
);
148 bfd_openw_with_cleanup (char *filename
, const char *target
, char *mode
)
152 if (*mode
== 'w') /* Write: create new file */
154 obfd
= bfd_openw (filename
, target
);
156 error ("Failed to open %s: %s.", filename
,
157 bfd_errmsg (bfd_get_error ()));
158 make_cleanup_bfd_close (obfd
);
159 if (!bfd_set_format (obfd
, bfd_object
))
160 error ("bfd_openw_with_cleanup: %s.", bfd_errmsg (bfd_get_error ()));
162 else if (*mode
== 'a') /* Append to existing file */
163 { /* FIXME -- doesn't work... */
164 error ("bfd_openw does not work with append.");
167 error ("bfd_openw_with_cleanup: unknown mode %s.", mode
);
172 struct cmd_list_element
*dump_cmdlist
;
173 struct cmd_list_element
*append_cmdlist
;
174 struct cmd_list_element
*srec_cmdlist
;
175 struct cmd_list_element
*ihex_cmdlist
;
176 struct cmd_list_element
*tekhex_cmdlist
;
177 struct cmd_list_element
*binary_dump_cmdlist
;
178 struct cmd_list_element
*binary_append_cmdlist
;
181 dump_command (char *cmd
, int from_tty
)
183 printf_unfiltered ("\"dump\" must be followed by a subcommand.\n\n");
184 help_list (dump_cmdlist
, "dump ", -1, gdb_stdout
);
188 append_command (char *cmd
, int from_tty
)
190 printf_unfiltered ("\"append\" must be followed by a subcommand.\n\n");
191 help_list (dump_cmdlist
, "append ", -1, gdb_stdout
);
195 dump_binary_file (char *filename
, char *mode
,
201 file
= fopen_with_cleanup (filename
, mode
);
202 status
= fwrite (buf
, len
, 1, file
);
204 perror_with_name (filename
);
208 dump_bfd_file (char *filename
, char *mode
,
209 char *target
, CORE_ADDR vaddr
,
215 obfd
= bfd_openw_with_cleanup (filename
, target
, mode
);
216 osection
= bfd_make_section_anyway (obfd
, ".newsec");
217 bfd_set_section_size (obfd
, osection
, len
);
218 bfd_set_section_vma (obfd
, osection
, vaddr
);
219 bfd_set_section_alignment (obfd
, osection
, 0);
220 bfd_set_section_flags (obfd
, osection
, 0x203);
221 osection
->entsize
= 0;
222 bfd_set_section_contents (obfd
, osection
, buf
, 0, len
);
226 dump_memory_to_file (char *cmd
, char *mode
, char *file_format
)
228 struct cleanup
*old_cleanups
= make_cleanup (null_cleanup
, NULL
);
239 filename
= scan_filename_with_cleanup (&cmd
, NULL
);
241 /* Find the low address. */
242 if (cmd
== NULL
|| *cmd
== '\0')
243 error ("Missing start address.");
244 lo_exp
= scan_expression_with_cleanup (&cmd
, NULL
);
246 /* Find the second address - rest of line. */
247 if (cmd
== NULL
|| *cmd
== '\0')
248 error ("Missing stop address.");
251 lo
= parse_and_eval_address (lo_exp
);
252 hi
= parse_and_eval_address (hi_exp
);
254 error ("Invalid memory address range (start >= end).");
257 /* FIXME: Should use read_memory_partial() and a magic blocking
259 buf
= xmalloc (count
);
260 make_cleanup (xfree
, buf
);
261 target_read_memory (lo
, buf
, count
);
263 /* Have everything. Open/write the data. */
264 if (file_format
== NULL
|| strcmp (file_format
, "binary") == 0)
266 dump_binary_file (filename
, mode
, buf
, count
);
270 dump_bfd_file (filename
, mode
, file_format
, lo
, buf
, count
);
273 do_cleanups (old_cleanups
);
277 dump_memory_command (char *cmd
, char *mode
)
279 dump_memory_to_file (cmd
, mode
, "binary");
283 dump_value_to_file (char *cmd
, char *mode
, char *file_format
)
285 struct cleanup
*old_cleanups
= make_cleanup (null_cleanup
, NULL
);
290 filename
= scan_filename_with_cleanup (&cmd
, NULL
);
292 /* Find the value. */
293 if (cmd
== NULL
|| *cmd
== '\0')
294 error ("No value to %s.", *mode
== 'a' ? "append" : "dump");
295 val
= parse_and_eval (cmd
);
297 error ("Invalid expression.");
299 /* Have everything. Open/write the data. */
300 if (file_format
== NULL
|| strcmp (file_format
, "binary") == 0)
302 dump_binary_file (filename
, mode
, VALUE_CONTENTS (val
),
303 TYPE_LENGTH (VALUE_TYPE (val
)));
309 if (VALUE_LVAL (val
))
311 vaddr
= VALUE_ADDRESS (val
);
316 warning ("value is not an lval: address assumed to be zero");
319 dump_bfd_file (filename
, mode
, file_format
, vaddr
,
320 VALUE_CONTENTS (val
),
321 TYPE_LENGTH (VALUE_TYPE (val
)));
324 do_cleanups (old_cleanups
);
328 dump_value_command (char *cmd
, char *mode
)
330 dump_value_to_file (cmd
, mode
, "binary");
334 dump_srec_memory (char *args
, int from_tty
)
336 dump_memory_to_file (args
, FOPEN_WB
, "srec");
340 dump_srec_value (char *args
, int from_tty
)
342 dump_value_to_file (args
, FOPEN_WB
, "srec");
346 dump_ihex_memory (char *args
, int from_tty
)
348 dump_memory_to_file (args
, FOPEN_WB
, "ihex");
352 dump_ihex_value (char *args
, int from_tty
)
354 dump_value_to_file (args
, FOPEN_WB
, "ihex");
358 dump_tekhex_memory (char *args
, int from_tty
)
360 dump_memory_to_file (args
, FOPEN_WB
, "tekhex");
364 dump_tekhex_value (char *args
, int from_tty
)
366 dump_value_to_file (args
, FOPEN_WB
, "tekhex");
370 dump_binary_memory (char *args
, int from_tty
)
372 dump_memory_to_file (args
, FOPEN_WB
, "binary");
376 dump_binary_value (char *args
, int from_tty
)
378 dump_value_to_file (args
, FOPEN_WB
, "binary");
382 append_binary_memory (char *args
, int from_tty
)
384 dump_memory_to_file (args
, FOPEN_AB
, "binary");
388 append_binary_value (char *args
, int from_tty
)
390 dump_value_to_file (args
, FOPEN_AB
, "binary");
395 void (*func
) (char *cmd
, char *mode
);
400 call_dump_func (struct cmd_list_element
*c
, char *args
, int from_tty
)
402 struct dump_context
*d
= get_cmd_context (c
);
403 d
->func (args
, d
->mode
);
407 add_dump_command (char *name
, void (*func
) (char *args
, char *mode
),
411 struct cmd_list_element
*c
;
412 struct dump_context
*d
;
414 c
= add_cmd (name
, all_commands
, NULL
, descr
, &dump_cmdlist
);
415 c
->completer
= filename_completer
;
416 d
= XMALLOC (struct dump_context
);
419 set_cmd_context (c
, d
);
420 c
->func
= call_dump_func
;
422 c
= add_cmd (name
, all_commands
, NULL
, descr
, &append_cmdlist
);
423 c
->completer
= filename_completer
;
424 d
= XMALLOC (struct dump_context
);
427 set_cmd_context (c
, d
);
428 c
->func
= call_dump_func
;
430 /* Replace "Dump " at start of docstring with "Append "
431 (borrowed from add_show_from_set). */
432 if ( c
->doc
[0] == 'W'
438 c
->doc
= concat ("Append ", c
->doc
+ 6, NULL
);
441 /* Opaque data for restore_section_callback. */
442 struct callback_data
{
443 unsigned long load_offset
;
444 CORE_ADDR load_start
;
448 /* Function: restore_section_callback.
450 Callback function for bfd_map_over_sections.
451 Selectively loads the sections into memory. */
454 restore_section_callback (bfd
*ibfd
, asection
*isec
, void *args
)
456 struct callback_data
*data
= args
;
457 bfd_vma sec_start
= bfd_section_vma (ibfd
, isec
);
458 bfd_size_type size
= bfd_section_size (ibfd
, isec
);
459 bfd_vma sec_end
= sec_start
+ size
;
460 bfd_size_type sec_offset
= 0;
461 bfd_size_type sec_load_count
= size
;
462 struct cleanup
*old_chain
;
466 /* Ignore non-loadable sections, eg. from elf files. */
467 if (!(bfd_get_section_flags (ibfd
, isec
) & SEC_LOAD
))
470 /* Does the section overlap with the desired restore range? */
471 if (sec_end
<= data
->load_start
472 || (data
->load_end
> 0 && sec_start
>= data
->load_end
))
474 /* No, no useable data in this section. */
475 printf_filtered ("skipping section %s...\n",
476 bfd_section_name (ibfd
, isec
));
480 /* Compare section address range with user-requested
481 address range (if any). Compute where the actual
482 transfer should start and end. */
483 if (sec_start
< data
->load_start
)
484 sec_offset
= data
->load_start
- sec_start
;
485 /* Size of a partial transfer: */
486 sec_load_count
-= sec_offset
;
487 if (data
->load_end
> 0 && sec_end
> data
->load_end
)
488 sec_load_count
-= sec_end
- data
->load_end
;
491 buf
= xmalloc (size
);
492 old_chain
= make_cleanup (xfree
, buf
);
493 if (!bfd_get_section_contents (ibfd
, isec
, buf
, 0, size
))
494 error ("Failed to read bfd file %s: '%s'.", bfd_get_filename (ibfd
),
495 bfd_errmsg (bfd_get_error ()));
497 printf_filtered ("Restoring section %s (0x%lx to 0x%lx)",
498 bfd_section_name (ibfd
, isec
),
499 (unsigned long) sec_start
,
500 (unsigned long) sec_end
);
502 if (data
->load_offset
!= 0 || data
->load_start
!= 0 || data
->load_end
!= 0)
503 printf_filtered (" into memory (0x%s to 0x%s)\n",
504 paddr_nz ((unsigned long) sec_start
505 + sec_offset
+ data
->load_offset
),
506 paddr_nz ((unsigned long) sec_start
+ sec_offset
507 + data
->load_offset
+ sec_load_count
));
509 puts_filtered ("\n");
511 /* Write the data. */
512 ret
= target_write_memory (sec_start
+ sec_offset
+ data
->load_offset
,
513 buf
+ sec_offset
, sec_load_count
);
515 warning ("restore: memory write failed (%s).", safe_strerror (ret
));
516 do_cleanups (old_chain
);
521 restore_binary_file (char *filename
, struct callback_data
*data
)
523 FILE *file
= fopen_with_cleanup (filename
, FOPEN_RB
);
528 /* Get the file size for reading. */
529 if (fseek (file
, 0, SEEK_END
) == 0)
532 perror_with_name (filename
);
534 if (len
<= data
->load_start
)
535 error ("Start address is greater than length of binary file %s.",
538 /* Chop off "len" if it exceeds the requested load_end addr. */
539 if (data
->load_end
!= 0 && data
->load_end
< len
)
540 len
= data
->load_end
;
541 /* Chop off "len" if the requested load_start addr skips some bytes. */
542 if (data
->load_start
> 0)
543 len
-= data
->load_start
;
546 ("Restoring binary file %s into memory (0x%lx to 0x%lx)\n",
548 (unsigned long) data
->load_start
+ data
->load_offset
,
549 (unsigned long) data
->load_start
+ data
->load_offset
+ len
);
551 /* Now set the file pos to the requested load start pos. */
552 if (fseek (file
, data
->load_start
, SEEK_SET
) != 0)
553 perror_with_name (filename
);
555 /* Now allocate a buffer and read the file contents. */
557 make_cleanup (xfree
, buf
);
558 if (fread (buf
, 1, len
, file
) != len
)
559 perror_with_name (filename
);
561 /* Now write the buffer into target memory. */
562 len
= target_write_memory (data
->load_start
+ data
->load_offset
, buf
, len
);
564 warning ("restore: memory write failed (%s).", safe_strerror (len
));
569 restore_command (char *args
, int from_tty
)
572 struct callback_data data
;
576 if (!target_has_execution
)
579 data
.load_offset
= 0;
583 /* Parse the input arguments. First is filename (required). */
584 filename
= scan_filename_with_cleanup (&args
, NULL
);
585 if (args
!= NULL
&& *args
!= '\0')
587 char *binary_string
= "binary";
589 /* Look for optional "binary" flag. */
590 if (strncmp (args
, binary_string
, strlen (binary_string
)) == 0)
593 args
+= strlen (binary_string
);
594 args
= skip_spaces (args
);
596 /* Parse offset (optional). */
597 if (args
!= NULL
&& *args
!= '\0')
599 parse_and_eval_long (scan_expression_with_cleanup (&args
, NULL
));
600 if (args
!= NULL
&& *args
!= '\0')
602 /* Parse start address (optional). */
604 parse_and_eval_long (scan_expression_with_cleanup (&args
, NULL
));
605 if (args
!= NULL
&& *args
!= '\0')
607 /* Parse end address (optional). */
608 data
.load_end
= parse_and_eval_long (args
);
609 if (data
.load_end
<= data
.load_start
)
610 error ("Start must be less than end.");
616 printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n",
617 filename
, (unsigned long) data
.load_offset
,
618 (unsigned long) data
.load_start
,
619 (unsigned long) data
.load_end
);
623 restore_binary_file (filename
, &data
);
627 /* Open the file for loading. */
628 ibfd
= bfd_openr_with_cleanup (filename
, NULL
);
630 /* Process the sections. */
631 bfd_map_over_sections (ibfd
, restore_section_callback
, &data
);
637 srec_dump_command (char *cmd
, int from_tty
)
639 printf_unfiltered ("\"dump srec\" must be followed by a subcommand.\n");
640 help_list (srec_cmdlist
, "dump srec ", -1, gdb_stdout
);
644 ihex_dump_command (char *cmd
, int from_tty
)
646 printf_unfiltered ("\"dump ihex\" must be followed by a subcommand.\n");
647 help_list (ihex_cmdlist
, "dump ihex ", -1, gdb_stdout
);
651 tekhex_dump_command (char *cmd
, int from_tty
)
653 printf_unfiltered ("\"dump tekhex\" must be followed by a subcommand.\n");
654 help_list (tekhex_cmdlist
, "dump tekhex ", -1, gdb_stdout
);
658 binary_dump_command (char *cmd
, int from_tty
)
660 printf_unfiltered ("\"dump binary\" must be followed by a subcommand.\n");
661 help_list (binary_dump_cmdlist
, "dump binary ", -1, gdb_stdout
);
665 binary_append_command (char *cmd
, int from_tty
)
667 printf_unfiltered ("\"append binary\" must be followed by a subcommand.\n");
668 help_list (binary_append_cmdlist
, "append binary ", -1, gdb_stdout
);
671 extern initialize_file_ftype _initialize_cli_dump
; /* -Wmissing-prototypes */
674 _initialize_cli_dump (void)
676 struct cmd_list_element
*c
;
677 add_prefix_cmd ("dump", class_vars
, dump_command
, "\
678 Dump target code/data to a local file.",
679 &dump_cmdlist
, "dump ",
682 add_prefix_cmd ("append", class_vars
, append_command
, "\
683 Append target code/data to a local file.",
684 &append_cmdlist
, "append ",
688 add_dump_command ("memory", dump_memory_command
, "\
689 Write contents of memory to a raw binary file.\n\
690 Arguments are FILE START STOP. Writes the contents of memory within the\n\
691 range [START .. STOP) to the specifed FILE in raw target ordered bytes.");
693 add_dump_command ("value", dump_value_command
, "\
694 Write the value of an expression to a raw binary file.\n\
695 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION to\n\
696 the specified FILE in raw target ordered bytes.");
698 add_prefix_cmd ("srec", all_commands
, srec_dump_command
, "\
699 Write target code/data to an srec file.",
700 &srec_cmdlist
, "dump srec ",
704 add_prefix_cmd ("ihex", all_commands
, ihex_dump_command
, "\
705 Write target code/data to an intel hex file.",
706 &ihex_cmdlist
, "dump ihex ",
710 add_prefix_cmd ("tekhex", all_commands
, tekhex_dump_command
, "\
711 Write target code/data to a tekhex file.",
712 &tekhex_cmdlist
, "dump tekhex ",
716 add_prefix_cmd ("binary", all_commands
, binary_dump_command
, "\
717 Write target code/data to a raw binary file.",
718 &binary_dump_cmdlist
, "dump binary ",
722 add_prefix_cmd ("binary", all_commands
, binary_append_command
, "\
723 Append target code/data to a raw binary file.",
724 &binary_append_cmdlist
, "append binary ",
728 add_cmd ("memory", all_commands
, dump_srec_memory
, "\
729 Write contents of memory to an srec file.\n\
730 Arguments are FILE START STOP. Writes the contents of memory\n\
731 within the range [START .. STOP) to the specifed FILE in srec format.",
734 add_cmd ("value", all_commands
, dump_srec_value
, "\
735 Write the value of an expression to an srec file.\n\
736 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
737 to the specified FILE in srec format.",
740 add_cmd ("memory", all_commands
, dump_ihex_memory
, "\
741 Write contents of memory to an ihex file.\n\
742 Arguments are FILE START STOP. Writes the contents of memory within\n\
743 the range [START .. STOP) to the specifed FILE in intel hex format.",
746 add_cmd ("value", all_commands
, dump_ihex_value
, "\
747 Write the value of an expression to an ihex file.\n\
748 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
749 to the specified FILE in intel hex format.",
752 add_cmd ("memory", all_commands
, dump_tekhex_memory
, "\
753 Write contents of memory to a tekhex file.\n\
754 Arguments are FILE START STOP. Writes the contents of memory\n\
755 within the range [START .. STOP) to the specifed FILE in tekhex format.",
758 add_cmd ("value", all_commands
, dump_tekhex_value
, "\
759 Write the value of an expression to a tekhex file.\n\
760 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
761 to the specified FILE in tekhex format.",
764 add_cmd ("memory", all_commands
, dump_binary_memory
, "\
765 Write contents of memory to a raw binary file.\n\
766 Arguments are FILE START STOP. Writes the contents of memory\n\
767 within the range [START .. STOP) to the specifed FILE in binary format.",
768 &binary_dump_cmdlist
);
770 add_cmd ("value", all_commands
, dump_binary_value
, "\
771 Write the value of an expression to a raw binary file.\n\
772 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
773 to the specified FILE in raw target ordered bytes.",
774 &binary_dump_cmdlist
);
776 add_cmd ("memory", all_commands
, append_binary_memory
, "\
777 Append contents of memory to a raw binary file.\n\
778 Arguments are FILE START STOP. Writes the contents of memory within the\n\
779 range [START .. STOP) to the specifed FILE in raw target ordered bytes.",
780 &binary_append_cmdlist
);
782 add_cmd ("value", all_commands
, append_binary_value
, "\
783 Append the value of an expression to a raw binary file.\n\
784 Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
785 to the specified FILE in raw target ordered bytes.",
786 &binary_append_cmdlist
);
788 c
= add_com ("restore", class_vars
, restore_command
,
789 "Restore the contents of FILE to target memory.\n\
790 Arguments are FILE OFFSET START END where all except FILE are optional.\n\
791 OFFSET will be added to the base address of the file (default zero).\n\
792 If START and END are given, only the file contents within that range\n\
793 (file relative) will be restored to target memory.");
794 c
->completer
= filename_completer
;
795 /* FIXME: completers for other commands. */