29#include "readline/tilde.h"
33#include "gdbsupport/filestuff.h"
34#include "gdbsupport/byte-vector.h"
37static gdb::unique_xmalloc_ptr<char>
40 if ((*cmd) == NULL || (**cmd) ==
'\0')
41 return make_unique_xstrdup (def);
47 end = (*cmd) + strcspn (*cmd,
" \t");
48 exp = savestring ((*cmd), end - (*cmd));
49 (*cmd) = skip_spaces (end);
50 return gdb::unique_xmalloc_ptr<char> (exp);
55static gdb::unique_xmalloc_ptr<char>
58 gdb::unique_xmalloc_ptr<char> filename;
66 error (_(
"Missing filename."));
67 filename.reset (xstrdup (defname));
74 (*cmd) = skip_spaces (*cmd);
75 end = *cmd + strcspn (*cmd,
" \t");
76 filename.reset (savestring ((*cmd), end - (*cmd)));
77 (*cmd) = skip_spaces (end);
79 gdb_assert (filename != NULL);
81 return gdb::unique_xmalloc_ptr<char> (tilde_expand (filename.get ()));
89 error (_(
"Failed to open %s: %s."), filename,
90 bfd_errmsg (bfd_get_error ()));
92 if (!bfd_check_format (ibfd.get (), bfd_object))
93 error (_(
"'%s' is not a recognized file format."), filename);
107 error (_(
"Failed to open %s: %s."), filename,
108 bfd_errmsg (bfd_get_error ()));
109 if (!bfd_set_format (
obfd.get (), bfd_object))
110 error (_(
"bfd_openw_or_error: %s."), bfd_errmsg (bfd_get_error ()));
112 else if (*mode ==
'a')
114 error (_(
"bfd_openw does not work with append."));
117 error (_(
"bfd_openw_or_error: unknown mode %s."), mode);
133 const bfd_byte *buf, ULONGEST len)
137 gdb_file_up file = gdb_fopen_cloexec (filename, mode);
139 perror_with_name (filename);
141 status = fwrite (buf, len, 1, file.get ());
143 perror_with_name (filename);
148 const char *target, CORE_ADDR vaddr,
149 const bfd_byte *buf, ULONGEST len)
154 osection = bfd_make_section_anyway (
obfd.get (),
".newsec");
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));
159 osection->entsize = 0;
160 if (!bfd_set_section_contents (
obfd.get (), osection, buf, 0, len))
161 warning (_(
"writing dump file '%s' (%s)"), filename,
162 bfd_errmsg (bfd_get_error ()));
174 gdb::unique_xmalloc_ptr<char> filename =
scan_filename (&cmd, NULL);
177 if (cmd == NULL || *cmd ==
'\0')
178 error (_(
"Missing start address."));
182 if (cmd == NULL || *cmd ==
'\0')
183 error (_(
"Missing stop address."));
189 error (_(
"Invalid memory address range (start >= end)."));
194 gdb::byte_vector buf (count);
198 if (file_format == NULL || strcmp (file_format,
"binary") == 0)
201 dump_bfd_file (filename.get (), mode, file_format, lo, buf.data (), count);
216 gdb::unique_xmalloc_ptr<char> filename =
scan_filename (&cmd, NULL);
219 if (cmd == NULL || *cmd ==
'\0')
220 error (_(
"No value to %s."), *mode ==
'a' ?
"append" :
"dump");
223 error (_(
"Invalid expression."));
226 if (file_format == NULL || strcmp (file_format,
"binary") == 0)
240 warning (_(
"value is not an lval: address assumed to be zero"));
343 void (*
func) (
const char *args,
const char *
mode),
368 if ( c->
doc[0] ==
'W'
374 c->
doc = concat (
"Append ", c->
doc + 6, (
char *)NULL);
381 CORE_ADDR load_offset,
382 CORE_ADDR load_start,
385 bfd_vma sec_start = bfd_section_vma (isec);
386 bfd_size_type
size = bfd_section_size (isec);
387 bfd_vma sec_end = sec_start +
size;
388 bfd_size_type sec_offset = 0;
389 bfd_size_type sec_load_count =
size;
393 if (!(bfd_section_flags (isec) & SEC_LOAD))
397 if (sec_end <= load_start
398 || (load_end > 0 && sec_start >= load_end))
402 bfd_section_name (isec));
409 if (sec_start < load_start)
410 sec_offset = load_start - sec_start;
412 sec_load_count -= sec_offset;
413 if (load_end > 0 && sec_end > load_end)
414 sec_load_count -= sec_end - load_end;
417 gdb::byte_vector buf (
size);
418 if (!bfd_get_section_contents (ibfd, isec, buf.data (), 0,
size))
419 error (_(
"Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd),
420 bfd_errmsg (bfd_get_error ()));
422 gdb_printf (
"Restoring section %s (0x%lx to 0x%lx)",
423 bfd_section_name (isec),
424 (
unsigned long) sec_start,
425 (
unsigned long) sec_end);
427 if (load_offset != 0 || load_start != 0 || load_end != 0)
430 (
unsigned long) sec_start
431 + sec_offset + load_offset),
433 (
unsigned long) sec_start + sec_offset
434 + load_offset + sec_load_count));
440 &buf[sec_offset], sec_load_count);
442 warning (_(
"restore: memory write failed (%s)."), safe_strerror (ret));
447 CORE_ADDR load_start, CORE_ADDR load_end)
450 gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB);
454 error (_(
"Failed to open %s: %s"), filename, safe_strerror (errno));
457 if (fseek (file.get (), 0, SEEK_END) == 0)
459 len = ftell (file.get ());
461 perror_with_name (filename);
464 perror_with_name (filename);
466 if (len <= load_start)
467 error (_(
"Start address is greater than length of binary file %s."),
471 if (load_end != 0 && load_end < len)
478 (
"Restoring binary file %s into memory (0x%lx to 0x%lx)\n",
480 (
unsigned long) (load_start + load_offset),
481 (
unsigned long) (load_start + load_offset + len));
484 if (fseek (file.get (), load_start,
SEEK_SET) != 0)
485 perror_with_name (filename);
488 gdb::byte_vector buf (len);
489 if (fread (buf.data (), 1, len, file.get ()) != len)
490 perror_with_name (filename);
495 warning (_(
"restore: memory write failed (%s)."), safe_strerror (len));
506 CORE_ADDR load_offset = 0;
507 CORE_ADDR load_start = 0;
508 CORE_ADDR load_end = 0;
511 gdb::unique_xmalloc_ptr<char> filename =
scan_filename (&args, NULL);
512 if (args != NULL && *args !=
'\0')
514 static const char binary_string[] =
"binary";
517 if (startswith (args, binary_string))
520 args += strlen (binary_string);
521 args = skip_spaces (args);
524 if (args != NULL && *args !=
'\0')
529 if (args != NULL && *args !=
'\0')
534 if (args != NULL && *args !=
'\0')
538 if (load_end <= load_start)
539 error (_(
"Start must be less than end."));
545 gdb_printf (
"Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n",
546 filename.get (), (
unsigned long) load_offset,
547 (
unsigned long) load_start,
548 (
unsigned long) load_end);
574 _(
"Dump target code/data to a local file."),
579 _(
"Append target code/data to a local file."),
585Write contents of memory to a raw binary file.\n\
586Arguments are FILE START STOP. Writes the contents of memory within the\n\
587range [START .. STOP) to the specified FILE in raw target ordered bytes.");
590Write the value of an expression to a raw binary file.\n\
591Arguments are FILE EXPRESSION. Writes the value of EXPRESSION to\n\
592the specified FILE in raw target ordered bytes.");
595 _(
"Write target code/data to an srec file."),
601 _(
"Write target code/data to an intel hex file."),
607 _(
"Write target code/data to a verilog hex file."),
613 _(
"Write target code/data to a tekhex file."),
619 _(
"Write target code/data to a raw binary file."),
625 _(
"Append target code/data to a raw binary file."),
631Write contents of memory to an srec file.\n\
632Arguments are FILE START STOP. Writes the contents of memory\n\
633within the range [START .. STOP) to the specified FILE in srec format."),
637Write the value of an expression to an srec file.\n\
638Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
639to the specified FILE in srec format."),
643Write contents of memory to an ihex file.\n\
644Arguments are FILE START STOP. Writes the contents of memory within\n\
645the range [START .. STOP) to the specified FILE in intel hex format."),
649Write the value of an expression to an ihex file.\n\
650Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
651to the specified FILE in intel hex format."),
655Write contents of memory to a verilog hex file.\n\
656Arguments are FILE START STOP. Writes the contents of memory within\n\
657the range [START .. STOP) to the specified FILE in verilog hex format."),
661Write the value of an expression to a verilog hex file.\n\
662Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
663to the specified FILE in verilog hex format."),
667Write contents of memory to a tekhex file.\n\
668Arguments are FILE START STOP. Writes the contents of memory\n\
669within the range [START .. STOP) to the specified FILE in tekhex format."),
673Write the value of an expression to a tekhex file.\n\
674Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
675to the specified FILE in tekhex format."),
679Write contents of memory to a raw binary file.\n\
680Arguments are FILE START STOP. Writes the contents of memory\n\
681within the range [START .. STOP) to the specified FILE in binary format."),
685Write the value of an expression to a raw binary file.\n\
686Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
687to the specified FILE in raw target ordered bytes."),
691Append contents of memory to a raw binary file.\n\
692Arguments are FILE START STOP. Writes the contents of memory within the\n\
693range [START .. STOP) to the specified FILE in raw target ordered bytes."),
697Append the value of an expression to a raw binary file.\n\
698Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
699to the specified FILE in raw target ordered bytes."),
703Restore the contents of FILE to target memory.\n\
704Arguments are FILE OFFSET START END where all except FILE are optional.\n\
705OFFSET will be added to the base address of the file (default zero).\n\
706If START and END are given, only the file contents within that range\n\
707(file relative) will be restored to target memory."));
constexpr string_view get()
struct gdbarch * target_gdbarch(void)
struct cmd_list_element * cmdlist
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
struct cmd_list_element * add_com(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc)
struct cmd_list_element * add_basic_prefix_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **subcommands, int allow_unknown, struct cmd_list_element **list)
static void restore_binary_file(const char *filename, CORE_ADDR load_offset, CORE_ADDR load_start, CORE_ADDR load_end)
static void dump_verilog_value(const char *args, int from_tty)
static void dump_tekhex_memory(const char *args, int from_tty)
static struct cmd_list_element * binary_append_cmdlist
static struct cmd_list_element * ihex_cmdlist
static void restore_command(const char *args, int from_tty)
static void dump_ihex_memory(const char *args, int from_tty)
static void dump_binary_memory(const char *args, int from_tty)
static struct cmd_list_element * dump_cmdlist
static void dump_binary_file(const char *filename, const char *mode, const bfd_byte *buf, ULONGEST len)
static void dump_tekhex_value(const char *args, int from_tty)
static void dump_value_command(const char *cmd, const char *mode)
static void dump_bfd_file(const char *filename, const char *mode, const char *target, CORE_ADDR vaddr, const bfd_byte *buf, ULONGEST len)
void _initialize_cli_dump()
static void call_dump_func(const char *args, int from_tty, cmd_list_element *c)
static void add_dump_command(const char *name, void(*func)(const char *args, const char *mode), const char *descr)
static void dump_memory_command(const char *cmd, const char *mode)
static struct cmd_list_element * verilog_cmdlist
static void dump_memory_to_file(const char *cmd, const char *mode, const char *file_format)
static void dump_binary_value(const char *args, int from_tty)
static void dump_verilog_memory(const char *args, int from_tty)
static void restore_one_section(bfd *ibfd, asection *isec, CORE_ADDR load_offset, CORE_ADDR load_start, CORE_ADDR load_end)
static void dump_srec_value(const char *args, int from_tty)
static gdb_bfd_ref_ptr bfd_openr_or_error(const char *filename, const char *target)
static struct cmd_list_element * srec_cmdlist
static struct cmd_list_element * tekhex_cmdlist
static void dump_value_to_file(const char *cmd, const char *mode, const char *file_format)
static struct cmd_list_element * binary_dump_cmdlist
static gdb::unique_xmalloc_ptr< char > scan_filename(const char **cmd, const char *defname)
static struct cmd_list_element * append_cmdlist
static void append_binary_memory(const char *args, int from_tty)
static gdb_bfd_ref_ptr bfd_openw_or_error(const char *filename, const char *target, const char *mode)
static gdb::unique_xmalloc_ptr< char > scan_expression(const char **cmd, const char *def)
static void dump_ihex_value(const char *args, int from_tty)
static void dump_srec_memory(const char *args, int from_tty)
static void append_binary_value(const char *args, int from_tty)
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
void read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
struct value * parse_and_eval(const char *exp, parser_flags flags)
LONGEST parse_and_eval_long(const char *exp)
CORE_ADDR parse_and_eval_address(const char *exp)
gdb_bfd_ref_ptr gdb_bfd_openr(const char *filename, const char *target)
gdb_bfd_ref_ptr gdb_bfd_openw(const char *filename, const char *target)
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
static gdb_bfd_section_range gdb_bfd_sections(bfd *abfd)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
void(* func)(remote_target *remote, char *)
completer_ftype * completer
void set_context(void *context)
void(* func)(const char *cmd, const char *mode)
gdb::array_view< const gdb_byte > contents()
struct type * type() const
enum lval_type lval() const
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
bool target_has_execution(inferior *inf)
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
void gdb_printf(struct ui_file *stream, const char *format,...)
void gdb_puts(const char *linebuffer, struct ui_file *stream)