/* Dump-to-file commands, for GDB, the GNU debugger.
- Copyright (C) 2002-2015 Free Software Foundation, Inc.
+ Copyright (C) 2002-2017 Free Software Foundation, Inc.
Contributed by Red Hat.
return file;
}
-static bfd *
-bfd_openr_with_cleanup (const char *filename, const char *target)
+static gdb_bfd_ref_ptr
+bfd_openr_or_error (const char *filename, const char *target)
{
- bfd *ibfd;
-
- ibfd = gdb_bfd_openr (filename, target);
+ gdb_bfd_ref_ptr ibfd (gdb_bfd_openr (filename, target));
if (ibfd == NULL)
- error (_("Failed to open %s: %s."), filename,
+ error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
- make_cleanup_bfd_unref (ibfd);
- if (!bfd_check_format (ibfd, bfd_object))
+ if (!bfd_check_format (ibfd.get (), bfd_object))
error (_("'%s' is not a recognized file format."), filename);
return ibfd;
}
-static bfd *
-bfd_openw_with_cleanup (const char *filename, const char *target,
- const char *mode)
+static gdb_bfd_ref_ptr
+bfd_openw_or_error (const char *filename, const char *target, const char *mode)
{
- bfd *obfd;
+ gdb_bfd_ref_ptr obfd;
if (*mode == 'w') /* Write: create new file */
{
obfd = gdb_bfd_openw (filename, target);
if (obfd == NULL)
- error (_("Failed to open %s: %s."), filename,
+ error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
- make_cleanup_bfd_unref (obfd);
- if (!bfd_set_format (obfd, bfd_object))
- error (_("bfd_openw_with_cleanup: %s."), bfd_errmsg (bfd_get_error ()));
+ if (!bfd_set_format (obfd.get (), bfd_object))
+ error (_("bfd_openw_or_error: %s."), bfd_errmsg (bfd_get_error ()));
}
else if (*mode == 'a') /* Append to existing file. */
{ /* FIXME -- doesn't work... */
error (_("bfd_openw does not work with append."));
}
else
- error (_("bfd_openw_with_cleanup: unknown mode %s."), mode);
+ error (_("bfd_openw_or_error: unknown mode %s."), mode);
return obfd;
}
const char *target, CORE_ADDR vaddr,
const bfd_byte *buf, ULONGEST len)
{
- bfd *obfd;
asection *osection;
- obfd = bfd_openw_with_cleanup (filename, target, mode);
- osection = bfd_make_section_anyway (obfd, ".newsec");
- bfd_set_section_size (obfd, osection, len);
- bfd_set_section_vma (obfd, osection, vaddr);
- bfd_set_section_alignment (obfd, osection, 0);
- bfd_set_section_flags (obfd, osection, (SEC_HAS_CONTENTS
- | SEC_ALLOC
- | SEC_LOAD));
+ gdb_bfd_ref_ptr obfd (bfd_openw_or_error (filename, target, mode));
+ osection = bfd_make_section_anyway (obfd.get (), ".newsec");
+ bfd_set_section_size (obfd.get (), osection, len);
+ bfd_set_section_vma (obfd.get (), osection, vaddr);
+ bfd_set_section_alignment (obfd.get (), osection, 0);
+ bfd_set_section_flags (obfd.get (), osection, (SEC_HAS_CONTENTS
+ | SEC_ALLOC
+ | SEC_LOAD));
osection->entsize = 0;
- if (!bfd_set_section_contents (obfd, osection, buf, 0, len))
- warning (_("writing dump file '%s' (%s)"), filename,
+ if (!bfd_set_section_contents (obfd.get (), osection, buf, 0, len))
+ warning (_("writing dump file '%s' (%s)"), filename,
bfd_errmsg (bfd_get_error ()));
}
CORE_ADDR hi;
ULONGEST count;
const char *filename;
- void *buf;
const char *lo_exp;
const char *hi_exp;
/* FIXME: Should use read_memory_partial() and a magic blocking
value. */
- buf = xmalloc (count);
- make_cleanup (xfree, buf);
- read_memory (lo, buf, count);
+ std::unique_ptr<gdb_byte[]> buf (new gdb_byte[count]);
+ read_memory (lo, buf.get (), count);
/* Have everything. Open/write the data. */
if (file_format == NULL || strcmp (file_format, "binary") == 0)
{
- dump_binary_file (filename, mode, buf, count);
+ dump_binary_file (filename, mode, buf.get (), count);
}
else
{
- dump_bfd_file (filename, mode, file_format, lo, buf, count);
+ dump_bfd_file (filename, mode, file_format, lo, buf.get (), count);
}
do_cleanups (old_cleanups);
}
static void
-dump_memory_command (char *cmd, char *mode)
+dump_memory_command (char *cmd, const char *mode)
{
dump_memory_to_file (cmd, mode, "binary");
}
}
static void
-dump_value_command (char *cmd, char *mode)
+dump_value_command (char *cmd, const char *mode)
{
dump_value_to_file (cmd, mode, "binary");
}
struct dump_context
{
- void (*func) (char *cmd, char *mode);
- char *mode;
+ void (*func) (char *cmd, const char *mode);
+ const char *mode;
};
static void
call_dump_func (struct cmd_list_element *c, char *args, int from_tty)
{
- struct dump_context *d = get_cmd_context (c);
+ struct dump_context *d = (struct dump_context *) get_cmd_context (c);
d->func (args, d->mode);
}
static void
-add_dump_command (char *name, void (*func) (char *args, char *mode),
- char *descr)
+add_dump_command (const char *name,
+ void (*func) (char *args, const char *mode),
+ const char *descr)
{
struct cmd_list_element *c;
static void
restore_section_callback (bfd *ibfd, asection *isec, void *args)
{
- struct callback_data *data = args;
+ struct callback_data *data = (struct callback_data *) args;
bfd_vma sec_start = bfd_section_vma (ibfd, isec);
bfd_size_type size = bfd_section_size (ibfd, isec);
bfd_vma sec_end = sec_start + size;
sec_load_count -= sec_end - data->load_end;
/* Get the data. */
- buf = xmalloc (size);
+ buf = (gdb_byte *) xmalloc (size);
old_chain = make_cleanup (xfree, buf);
if (!bfd_get_section_contents (ibfd, isec, buf, 0, size))
error (_("Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd),
{
struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
FILE *file = fopen_with_cleanup (filename, FOPEN_RB);
- gdb_byte *buf;
long len;
/* Get the file size for reading. */
perror_with_name (filename);
/* Now allocate a buffer and read the file contents. */
- buf = xmalloc (len);
- make_cleanup (xfree, buf);
- if (fread (buf, 1, len, file) != len)
+ std::unique_ptr<gdb_byte[]> buf (new gdb_byte[len]);
+ if (fread (buf.get (), 1, len, file) != len)
perror_with_name (filename);
/* Now write the buffer into target memory. */
- len = target_write_memory (data->load_start + data->load_offset, buf, len);
+ len = target_write_memory (data->load_start + data->load_offset,
+ buf.get (), len);
if (len != 0)
warning (_("restore: memory write failed (%s)."), safe_strerror (len));
do_cleanups (cleanup);
filename = scan_filename_with_cleanup (&args, NULL);
if (args != NULL && *args != '\0')
{
- char *binary_string = "binary";
+ static const char binary_string[] = "binary";
/* Look for optional "binary" flag. */
if (startswith (args, binary_string))
}
/* Parse offset (optional). */
if (args != NULL && *args != '\0')
- data.load_offset =
- parse_and_eval_address (scan_expression_with_cleanup (&args, NULL));
+ data.load_offset = binary_flag ?
+ parse_and_eval_address (scan_expression_with_cleanup (&args, NULL)) :
+ parse_and_eval_long (scan_expression_with_cleanup (&args, NULL));
if (args != NULL && *args != '\0')
{
/* Parse start address (optional). */
else
{
/* Open the file for loading. */
- ibfd = bfd_openr_with_cleanup (filename, NULL);
+ gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename, NULL));
/* Process the sections. */
- bfd_map_over_sections (ibfd, restore_section_callback, &data);
+ bfd_map_over_sections (ibfd.get (), restore_section_callback, &data);
}
- return;
}
static void