* [PATCH 01/10] change gdb to refcount bfd everywhere
@ 2012-07-18 19:32 Tom Tromey
2012-07-19 14:18 ` Jan Kratochvil
0 siblings, 1 reply; 30+ messages in thread
From: Tom Tromey @ 2012-07-18 19:32 UTC (permalink / raw)
To: gdb-patches
Right now we reference count BFDs in a few places.
This patch changes the reference counting to be universal.
I think this is an improvement because it means that code in GDB
can manipulate BFDs without having to know where they come from.
In a few spots this could perhaps be dispensed with, but I think
that the resulting uniformity is, in this case, more valuable than
the potential micro-optimization.
* dwarf2read.c (try_open_dwo_file): Use gdb_bfd_ref and
gdb_bfd_unref.
(free_dwo_file): Use gdb_bfd_unref.
* cli/cli-dump.c: Include gdb_bfd.h.
(bfd_openw_with_cleanup): Use gdb_bfd_ref.
(bfd_openr_with_cleanup): Likewise.
* windows-nat.c (windows_make_so): Use gdb_bfd_ref,
gdb_bfd_unref.
* utils.c: Include gdb_bfd.h.
(do_bfd_close_cleanup): Use gdb_bfd_unref.
* symfile.c: Include gdb_bfd.h.
(separate_debug_file_exists): Use gdb_bfd_unref.
(bfd_open_maybe_remote): Use gdb_bfd_ref.
(symfile_bfd_open): Use gdb_bfd_ref, gdb_bfd_unref.
(generic_load): Use gdb_bfd_ref.
(reread_symbols): Use gdb_bfd_unref.
* symfile-mem.c: Include gdb_bfd.h.
(symbol_file_add_from_memory): Use make_cleanup_bfd_close.
* spu-linux-nat.c (spu_bfd_open): Use gdb_bfd_ref, gdb_bfd_unref.
* solib.c: Include gdb_bfd.h.
(solib_bfd_fopen): Use gdb_bfd_ref.
(solib_bfd_open): Use gdb_bfd_unref.
(free_so_symbols): Use gdb_bfd_unref.
(reload_shared_libraries_1): Use gdb_bfd_unref.
* solib-spu.c: Include gdb_bfd.h.
(spu_bfd_fopen): Use gdb_bfd_ref, gdb_bfd_unref.
* solib-pa64.c (pa64_solib_create_inferior_hook): Use gdb_bfd_ref,
gdb_bfd_unref.
* solib-frv.c: Include gdb_bfd.h.
(enable_break2): Use gdb_bfd_unref.
* solib-dsbt.c: Include gdb_bfd.h.
(enable_break2): Use gdb_bfd_unref.
* solib-darwin.c: Include gdb_bfd.h.
(darwin_solib_get_all_image_info_addr_at_init): Use gdb_bfd_ref,
gdb_bfd_unref.
(darwin_bfd_open): Use gdb_bfd_unref.
* rs6000-nat.c (add_vmap): Use gdb_bfd_ref, gdb_bfd_unref.
* remote-mips.c: Include gdb_bfd.h.
(mips_load_srec): Use gdb_bfd_ref.
(pmon_load_fast): Use gdb_bfd_ref.
* remote-m32r-sdi.c: Include gdb_bfd.h.
(m32r_load): Use gdb_bfd_ref.
* record.c: Include gdb_bfd.h.
(record_save_cleanups): Use gdb_bfd_unref.
(cmd_record_save): Use gdb_bfd_unref.
* procfs.c (insert_dbx_link_bpt_in_file): Use gdb_bfd_ref,
gdb_bfd_unref.
* objfiles.h (gdb_bfd_close_or_warn): Remove.
(gdb_bfd_ref, gdb_bfd_unref): Move to gdb_bfd.h.
* objfiles.c: Include gdb_bfd.h.
(free_objfile): Use gdb_bfd_unref.
(gdb_bfd_close_or_warn, gdb_bfd_ref, gdb_bfd_unref): Move to
gdb_bfd.c.
* machoread.c (macho_add_oso_symfile): Use gdb_bfd_unref.
(macho_symfile_read_all_oso): Use gdb_bfd_ref, gdb_bfd_unref.
(macho_check_dsym): Likewise.
* m32r-rom.c: Include gdb_bfd.h.
(m32r_load): Use gdb_bfd_ref.
(m32r_upload_command): Use gdb_bfd_ref.
* jit.c: Include gdb_bfd.h.
(jit_bfd_try_read_symtab): Use gdb_bfd_ref, gdb_bfd_unref.
* gdb_bfd.h: New file.
* gdb_bfd.c: New file.
* gcore.c: Include gdb_bfd.h.
(create_gcore_bfd): Use gdb_bfd_ref.
(do_bfd_delete_cleanup): Use gdb_bfd_unref.
(gcore_command): Use gdb_bfd_unref.
* exec.c: Include gdb_bfd.h.
(exec_close): Use gdb_bfd_unref.
(exec_close_1): Use gdb_bfd_unref.
(exec_file_attach): Use gdb_bfd_ref.
* elfread.c: Include gdb_bfd.h.
(build_id_verify): Use gdb_bfd_unref.
* dsrec.c: Include gdb_bfd.h.
(load_srec): Use gdb_bfd_ref.
* corelow.c: Include gdb_bfd.h.
(core_close): Use gdb_bfd_unref.
(core_open): Use gdb_bfd_ref.
* bfd-target.c: Include gdb_bfd.h.
(target_bfd_xclose): Use gdb_bfd_unref.
(target_bfd_reopen): Use gdb_bfd_ref.
* Makefile.in (SFILES): Add gdb_bfd.c.
(HFILES_NO_SRCDIR): Add gdb_bfd.h.
(COMMON_OBS): Add gdb_bfd.o.
---
gdb/Makefile.in | 6 ++--
gdb/bfd-target.c | 5 ++-
gdb/cli/cli-dump.c | 5 ++-
gdb/corelow.c | 9 +++--
gdb/dsrec.c | 3 +-
gdb/dwarf2read.c | 10 ++---
gdb/elfread.c | 3 +-
gdb/exec.c | 12 +++---
gdb/gcore.c | 7 ++--
gdb/gdb_bfd.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++
gdb/gdb_bfd.h | 35 +++++++++++++++++++
gdb/jit.c | 4 ++-
gdb/m32r-rom.c | 5 ++-
gdb/machoread.c | 32 +++++++++--------
gdb/objfiles.c | 74 ++--------------------------------------
gdb/objfiles.h | 4 --
gdb/procfs.c | 10 +++---
gdb/record.c | 5 ++-
gdb/remote-m32r-sdi.c | 3 +-
gdb/remote-mips.c | 5 ++-
gdb/rs6000-nat.c | 13 ++++---
gdb/solib-darwin.c | 9 +++--
gdb/solib-dsbt.c | 7 ++--
gdb/solib-frv.c | 13 ++++---
gdb/solib-pa64.c | 6 ++--
gdb/solib-spu.c | 11 +++---
gdb/solib.c | 11 +++---
gdb/spu-linux-nat.c | 3 +-
gdb/symfile-mem.c | 7 ++--
gdb/symfile.c | 37 +++++++-------------
gdb/utils.c | 3 +-
gdb/windows-nat.c | 6 ++--
32 files changed, 257 insertions(+), 196 deletions(-)
create mode 100644 gdb/gdb_bfd.c
create mode 100644 gdb/gdb_bfd.h
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index a41cff9..a257d9a 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -704,7 +704,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
exceptions.c expprint.c \
f-exp.y f-lang.c f-typeprint.c f-valprint.c filesystem.c \
findcmd.c findvar.c frame.c frame-base.c frame-unwind.c \
- gdbarch.c arch-utils.c gdbtypes.c gnu-v2-abi.c gnu-v3-abi.c \
+ gdbarch.c arch-utils.c gdb_bfd.c gdbtypes.c gnu-v2-abi.c gnu-v3-abi.c \
go-exp.y go-lang.c go-typeprint.c go-valprint.c \
inf-loop.c \
infcall.c \
@@ -829,7 +829,7 @@ gnulib/import/extra/snippet/warn-on-use.h \
gnulib/import/stddef.in.h gnulib/import/inttypes.in.h inline-frame.h skip.h \
common/common-utils.h common/xml-utils.h common/buffer.h common/ptid.h \
common/format.h \
-common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h
+common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h gdb_bfd.h
# Header files that already have srcdir in them, or which are in objdir.
@@ -879,7 +879,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
macrotab.o macrocmd.o macroexp.o macroscope.o \
mi-common.o \
event-loop.o event-top.o inf-loop.o completer.o \
- gdbarch.o arch-utils.o gdbtypes.o osabi.o copying.o \
+ gdbarch.o arch-utils.o gdbtypes.o gdb_bfd.o osabi.o copying.o \
memattr.o mem-break.o target.o parse.o language.o buildsym.o \
findcmd.o \
std-regs.o \
diff --git a/gdb/bfd-target.c b/gdb/bfd-target.c
index 6728800..21f53bb 100644
--- a/gdb/bfd-target.c
+++ b/gdb/bfd-target.c
@@ -21,6 +21,7 @@
#include "target.h"
#include "bfd-target.h"
#include "exec.h"
+#include "gdb_bfd.h"
/* The object that is stored in the target_ops->to_data field has this
type. */
@@ -70,7 +71,7 @@ target_bfd_xclose (struct target_ops *t, int quitting)
{
struct target_bfd_data *data = t->to_data;
- bfd_close (data->bfd);
+ gdb_bfd_unref (data->bfd);
xfree (data->table.sections);
xfree (data);
xfree (t);
@@ -83,7 +84,7 @@ target_bfd_reopen (struct bfd *abfd)
struct target_bfd_data *data;
data = XZALLOC (struct target_bfd_data);
- data->bfd = abfd;
+ data->bfd = gdb_bfd_ref (abfd);
build_section_table (abfd, &data->table.sections, &data->table.sections_end);
t = XZALLOC (struct target_ops);
diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index 4a8b5d1..7dce32a 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -32,6 +32,7 @@
#include "readline/readline.h"
#include "gdbcore.h"
#include "cli/cli-utils.h"
+#include "gdb_bfd.h"
#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
@@ -111,7 +112,7 @@ bfd_openr_with_cleanup (const char *filename, const char *target)
{
bfd *ibfd;
- ibfd = bfd_openr (filename, target);
+ ibfd = gdb_bfd_ref (bfd_openr (filename, target));
if (ibfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
@@ -131,7 +132,7 @@ bfd_openw_with_cleanup (const char *filename, const char *target,
if (*mode == 'w') /* Write: create new file */
{
- obfd = bfd_openw (filename, target);
+ obfd = gdb_bfd_ref (bfd_openw (filename, target));
if (obfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/corelow.c b/gdb/corelow.c
index dd62560..3dfa2f4 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -46,6 +46,7 @@
#include "filenames.h"
#include "progspace.h"
#include "objfiles.h"
+#include "gdb_bfd.h"
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
@@ -216,7 +217,7 @@ core_close (int quitting)
}
name = bfd_get_filename (core_bfd);
- gdb_bfd_close_or_warn (core_bfd);
+ gdb_bfd_unref (core_bfd);
xfree (name);
core_bfd = NULL;
}
@@ -319,9 +320,9 @@ core_open (char *filename, int from_tty)
if (scratch_chan < 0)
perror_with_name (filename);
- temp_bfd = bfd_fopen (filename, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
+ temp_bfd = gdb_bfd_ref (bfd_fopen (filename, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan));
if (temp_bfd == NULL)
perror_with_name (filename);
diff --git a/gdb/dsrec.c b/gdb/dsrec.c
index d2c99b2..54f2971 100644
--- a/gdb/dsrec.c
+++ b/gdb/dsrec.c
@@ -23,6 +23,7 @@
#include <time.h>
#include "gdb_assert.h"
#include "gdb_string.h"
+#include "gdb_bfd.h"
extern void report_transfer_performance (unsigned long, time_t, time_t);
@@ -59,7 +60,7 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
srec = (char *) alloca (maxrecsize + 1);
- abfd = bfd_openr (file, 0);
+ abfd = gdb_bfd_ref (bfd_openr (file, 0));
if (!abfd)
{
printf_filtered (_("Unable to open file %s\n"), file);
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 833a590..4c976f3 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -64,6 +64,7 @@
#include "gdbcore.h" /* for gnutarget */
#include "gdb/gdb-index.h"
#include <ctype.h>
+#include "gdb_bfd.h"
#include <fcntl.h>
#include "gdb_string.h"
@@ -8114,7 +8115,7 @@ try_open_dwo_file (const char *file_name)
if (desc < 0)
return NULL;
- sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
+ sym_bfd = gdb_bfd_ref (bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc));
if (!sym_bfd)
{
xfree (absolute_name);
@@ -8124,14 +8125,11 @@ try_open_dwo_file (const char *file_name)
if (!bfd_check_format (sym_bfd, bfd_object))
{
- bfd_close (sym_bfd); /* This also closes desc. */
xfree (absolute_name);
+ gdb_bfd_unref (sym_bfd); /* This also closes desc. */
return NULL;
}
- /* bfd_usrdata exists for applications and libbfd must not touch it. */
- gdb_assert (bfd_usrdata (sym_bfd) == NULL);
-
return sym_bfd;
}
@@ -8323,7 +8321,7 @@ free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
struct dwarf2_section_info *section;
gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
- bfd_close (dwo_file->dwo_bfd);
+ gdb_bfd_unref (dwo_file->dwo_bfd);
munmap_section_buffer (&dwo_file->sections.abbrev);
munmap_section_buffer (&dwo_file->sections.info);
diff --git a/gdb/elfread.c b/gdb/elfread.c
index 350ad0e..de61a9a 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -44,6 +44,7 @@
#include "gdbthread.h"
#include "regcache.h"
#include "bcache.h"
+#include "gdb_bfd.h"
extern void _initialize_elfread (void);
@@ -1123,7 +1124,7 @@ build_id_verify (const char *filename, struct build_id *check)
else
retval = 1;
- gdb_bfd_close_or_warn (abfd);
+ gdb_bfd_unref (abfd);
xfree (found);
diff --git a/gdb/exec.c b/gdb/exec.c
index 6ba1986..3d26bcc 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -33,6 +33,7 @@
#include "arch-utils.h"
#include "gdbthread.h"
#include "progspace.h"
+#include "gdb_bfd.h"
#include <fcntl.h>
#include "readline/readline.h"
@@ -100,7 +101,7 @@ exec_close (void)
bfd *abfd = exec_bfd;
char *name = bfd_get_filename (abfd);
- gdb_bfd_close_or_warn (abfd);
+ gdb_bfd_unref (abfd);
xfree (name);
/* Removing target sections may close the exec_ops target.
@@ -137,8 +138,7 @@ exec_close_1 (int quitting)
need_symtab_cleanup = 1;
}
else if (vp->bfd != exec_bfd)
- /* FIXME-leak: We should be freeing vp->name too, I think. */
- gdb_bfd_close_or_warn (vp->bfd);
+ gdb_bfd_unref (vp->bfd);
xfree (vp);
}
@@ -232,9 +232,9 @@ exec_file_attach (char *filename, int from_tty)
#endif
if (scratch_chan < 0)
perror_with_name (filename);
- exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
+ exec_bfd = gdb_bfd_ref (bfd_fopen (scratch_pathname, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan));
if (!exec_bfd)
{
diff --git a/gdb/gcore.c b/gdb/gcore.c
index aedda41..486ea5f 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -33,6 +33,7 @@
#include <fcntl.h>
#include "regcache.h"
#include "regset.h"
+#include "gdb_bfd.h"
/* The largest amount of memory to read from the target at once. We
must throttle it to limit the amount of memory used by GDB during
@@ -50,7 +51,7 @@ static int gcore_memory_sections (bfd *);
bfd *
create_gcore_bfd (char *filename)
{
- bfd *obfd = bfd_openw (filename, default_gcore_target ());
+ bfd *obfd = gdb_bfd_ref (bfd_openw (filename, default_gcore_target ()));
if (!obfd)
error (_("Failed to open '%s' for output."), filename);
@@ -110,7 +111,7 @@ do_bfd_delete_cleanup (void *arg)
bfd *obfd = arg;
const char *filename = obfd->filename;
- bfd_close (arg);
+ gdb_bfd_unref (arg);
unlink (filename);
}
@@ -154,7 +155,7 @@ gcore_command (char *args, int from_tty)
fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
discard_cleanups (old_chain);
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
}
static unsigned long
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
new file mode 100644
index 0000000..160e9e6
--- /dev/null
+++ b/gdb/gdb_bfd.c
@@ -0,0 +1,90 @@
+/* Definitions for BFD wrappers used by GDB.
+
+ Copyright (C) 2011
+ Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "gdb_bfd.h"
+#include "gdb_assert.h"
+
+/* Close ABFD, and warn if that fails. */
+
+static int
+gdb_bfd_close_or_warn (struct bfd *abfd)
+{
+ int ret;
+ char *name = bfd_get_filename (abfd);
+
+ ret = bfd_close (abfd);
+
+ if (!ret)
+ warning (_("cannot close \"%s\": %s"),
+ name, bfd_errmsg (bfd_get_error ()));
+
+ return ret;
+}
+
+/* Add reference to ABFD. Returns ABFD. */
+
+struct bfd *
+gdb_bfd_ref (struct bfd *abfd)
+{
+ int *p_refcount;
+
+ if (abfd == NULL)
+ return NULL;
+
+ p_refcount = bfd_usrdata (abfd);
+
+ if (p_refcount != NULL)
+ {
+ *p_refcount += 1;
+ return abfd;
+ }
+
+ p_refcount = xmalloc (sizeof (*p_refcount));
+ *p_refcount = 1;
+ bfd_usrdata (abfd) = p_refcount;
+
+ return abfd;
+}
+
+/* Unreference and possibly close ABFD. */
+
+void
+gdb_bfd_unref (struct bfd *abfd)
+{
+ int *p_refcount;
+ char *name;
+
+ if (abfd == NULL)
+ return;
+
+ p_refcount = bfd_usrdata (abfd);
+ gdb_assert (*p_refcount >= 1);
+
+ *p_refcount -= 1;
+ if (*p_refcount > 0)
+ return;
+
+ xfree (p_refcount);
+ bfd_usrdata (abfd) = NULL; /* Paranoia. */
+
+ name = bfd_get_filename (abfd);
+ gdb_bfd_close_or_warn (abfd);
+}
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
new file mode 100644
index 0000000..c6d94a0
--- /dev/null
+++ b/gdb/gdb_bfd.h
@@ -0,0 +1,35 @@
+/* Definitions for BFD wrappers used by GDB.
+
+ Copyright (C) 2011
+ Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDB_BFD_H
+#define GDB_BFD_H
+
+/* Acquire a new reference to ABFD. Returns ABFD for convenience.
+ It is fine for ABFD to be NULL; in this case the function does
+ nothing and returns NULL. */
+
+struct bfd *gdb_bfd_ref (struct bfd *abfd);
+
+/* Release a reference to ABFD. If this is the last reference, ABFD
+ will be freed. If ABFD is NULL, this function does nothing. */
+
+void gdb_bfd_unref (struct bfd *abfd);
+
+#endif /* GDB_BFD_H */
diff --git a/gdb/jit.c b/gdb/jit.c
index 568d17b..6a1425c 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -38,6 +38,7 @@
#include "gdb-dlfcn.h"
#include "gdb_stat.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
static const char *jit_reader_dir = NULL;
@@ -861,6 +862,7 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
return;
}
+ nbfd = gdb_bfd_ref (nbfd);
/* Check the format. NOTE: This initializes important data that GDB uses!
We would segfault later without this line. */
@@ -868,7 +870,7 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
{
printf_unfiltered (_("\
JITed symbol file is not an object file, ignoring it.\n"));
- bfd_close (nbfd);
+ gdb_bfd_unref (nbfd);
return;
}
diff --git a/gdb/m32r-rom.c b/gdb/m32r-rom.c
index 76e4bf1..92a73d0 100644
--- a/gdb/m32r-rom.c
+++ b/gdb/m32r-rom.c
@@ -40,6 +40,7 @@
#include "inferior.h"
#include <ctype.h>
#include "regcache.h"
+#include "gdb_bfd.h"
/*
* All this stuff just to get my host computer's IP address!
@@ -128,7 +129,7 @@ m32r_load (char *filename, int from_tty)
if (filename == NULL || filename[0] == 0)
filename = get_exec_file (1);
- abfd = bfd_openr (filename, 0);
+ abfd = gdb_bfd_ref (bfd_openr (filename, 0));
if (!abfd)
error (_("Unable to open file %s."), filename);
if (bfd_check_format (abfd, bfd_object) == 0)
@@ -524,7 +525,7 @@ m32r_upload_command (char *args, int from_tty)
printf_filtered (" -- Ethernet load complete.\n");
gettimeofday (&end_time, NULL);
- abfd = bfd_openr (args, 0);
+ abfd = gdb_bfd_ref (bfd_openr (args, 0));
if (abfd != NULL)
{ /* Download is done -- print section statistics. */
if (bfd_check_format (abfd, bfd_object) == 0)
diff --git a/gdb/machoread.c b/gdb/machoread.c
index 22530ab..eb56f14 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -466,14 +466,14 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
{
warning (_("`%s': can't read symbols: %s."), oso->name,
bfd_errmsg (bfd_get_error ()));
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return;
}
if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd))
{
warning (_("`%s': file time stamp mismatch."), oso->name);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return;
}
@@ -482,7 +482,7 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
oso->nbr_syms))
{
warning (_("`%s': can't create hash table"), oso->name);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return;
}
@@ -686,7 +686,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
}
/* Open the archive and check the format. */
- archive_bfd = bfd_openr (archive_name, gnutarget);
+ archive_bfd = gdb_bfd_ref (bfd_openr (archive_name, gnutarget));
if (archive_bfd == NULL)
{
warning (_("Could not open OSO archive file \"%s\""),
@@ -698,17 +698,18 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
{
warning (_("OSO archive file \"%s\" not an archive."),
archive_name);
- bfd_close (archive_bfd);
+ gdb_bfd_unref (archive_bfd);
ix = last_ix;
continue;
}
- member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
+ member_bfd = gdb_bfd_ref (bfd_openr_next_archived_file (archive_bfd,
+ NULL));
if (member_bfd == NULL)
{
warning (_("Could not read archive members out of "
"OSO archive \"%s\""), archive_name);
- bfd_close (archive_bfd);
+ gdb_bfd_unref (archive_bfd);
ix = last_ix;
continue;
}
@@ -738,12 +739,13 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
}
prev = member_bfd;
- member_bfd = bfd_openr_next_archived_file
- (archive_bfd, member_bfd);
+ member_bfd
+ = gdb_bfd_ref (bfd_openr_next_archived_file (archive_bfd,
+ member_bfd));
/* Free previous member if not referenced by an oso. */
if (ix2 >= last_ix)
- bfd_close (prev);
+ gdb_bfd_unref (prev);
}
for (ix2 = ix; ix2 < last_ix; ix2++)
{
@@ -759,7 +761,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
{
bfd *abfd;
- abfd = bfd_openr (oso->name, gnutarget);
+ abfd = gdb_bfd_ref (bfd_openr (oso->name, gnutarget));
if (!abfd)
warning (_("`%s': can't open to read symbols: %s."), oso->name,
bfd_errmsg (bfd_get_error ()));
@@ -809,7 +811,7 @@ macho_check_dsym (struct objfile *objfile)
return NULL;
}
dsym_filename = xstrdup (dsym_filename);
- dsym_bfd = bfd_openr (dsym_filename, gnutarget);
+ dsym_bfd = gdb_bfd_ref (bfd_openr (dsym_filename, gnutarget));
if (dsym_bfd == NULL)
{
warning (_("can't open dsym file %s"), dsym_filename);
@@ -819,7 +821,7 @@ macho_check_dsym (struct objfile *objfile)
if (!bfd_check_format (dsym_bfd, bfd_object))
{
- bfd_close (dsym_bfd);
+ gdb_bfd_unref (dsym_bfd);
warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
xfree (dsym_filename);
return NULL;
@@ -829,7 +831,7 @@ macho_check_dsym (struct objfile *objfile)
BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
{
warning (_("can't find UUID in %s"), dsym_filename);
- bfd_close (dsym_bfd);
+ gdb_bfd_unref (dsym_bfd);
xfree (dsym_filename);
return NULL;
}
@@ -837,7 +839,7 @@ macho_check_dsym (struct objfile *objfile)
sizeof (main_uuid->command.uuid.uuid)))
{
warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
- bfd_close (dsym_bfd);
+ gdb_bfd_unref (dsym_bfd);
xfree (dsym_filename);
return NULL;
}
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index f5e5c75..5ff0eb2 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -53,6 +53,7 @@
#include "complaints.h"
#include "psymtab.h"
#include "solist.h"
+#include "gdb_bfd.h"
/* Prototypes for local functions */
@@ -193,9 +194,9 @@ allocate_objfile (bfd *abfd, int flags)
/* Update the per-objfile information that comes from the bfd, ensuring
that any data that is reference is saved in the per-objfile data
- region. */
+ region. Note that we steal a reference to ABFD. */
- objfile->obfd = gdb_bfd_ref (abfd);
+ objfile->obfd = abfd;
if (abfd != NULL)
{
/* Look up the gdbarch associated with the BFD. */
@@ -1456,75 +1457,6 @@ objfiles_changed (void)
get_objfile_pspace_data (current_program_space)->objfiles_changed_p = 1;
}
-/* Close ABFD, and warn if that fails. */
-
-int
-gdb_bfd_close_or_warn (struct bfd *abfd)
-{
- int ret;
- char *name = bfd_get_filename (abfd);
-
- ret = bfd_close (abfd);
-
- if (!ret)
- warning (_("cannot close \"%s\": %s"),
- name, bfd_errmsg (bfd_get_error ()));
-
- return ret;
-}
-
-/* Add reference to ABFD. Returns ABFD. */
-struct bfd *
-gdb_bfd_ref (struct bfd *abfd)
-{
- int *p_refcount;
-
- if (abfd == NULL)
- return NULL;
-
- p_refcount = bfd_usrdata (abfd);
-
- if (p_refcount != NULL)
- {
- *p_refcount += 1;
- return abfd;
- }
-
- p_refcount = xmalloc (sizeof (*p_refcount));
- *p_refcount = 1;
- bfd_usrdata (abfd) = p_refcount;
-
- return abfd;
-}
-
-/* Unreference and possibly close ABFD. */
-void
-gdb_bfd_unref (struct bfd *abfd)
-{
- int *p_refcount;
- char *name;
-
- if (abfd == NULL)
- return;
-
- p_refcount = bfd_usrdata (abfd);
-
- /* Valid range for p_refcount: a pointer to int counter, which has a
- value of 1 (single owner) or 2 (shared). */
- gdb_assert (*p_refcount == 1 || *p_refcount == 2);
-
- *p_refcount -= 1;
- if (*p_refcount > 0)
- return;
-
- xfree (p_refcount);
- bfd_usrdata (abfd) = NULL; /* Paranoia. */
-
- name = bfd_get_filename (abfd);
- gdb_bfd_close_or_warn (abfd);
- xfree (name);
-}
-
/* The default implementation for the "iterate_over_objfiles_in_search_order"
gdbarch method. It is equivalent to use the ALL_OBJFILES macro,
searching the objfiles in the order they are stored internally,
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 01c3aea..0df5798 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -522,10 +522,6 @@ extern void set_objfile_data (struct objfile *objfile,
extern void *objfile_data (struct objfile *objfile,
const struct objfile_data *data);
-extern struct bfd *gdb_bfd_ref (struct bfd *abfd);
-extern void gdb_bfd_unref (struct bfd *abfd);
-extern int gdb_bfd_close_or_warn (struct bfd *abfd);
-
extern void default_iterate_over_objfiles_in_search_order
(struct gdbarch *gdbarch,
iterate_over_objfiles_in_search_order_cb_ftype *cb,
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 774df2e..32cb468 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3486,7 +3486,7 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
long storage_needed;
CORE_ADDR sym_addr;
- abfd = bfd_fdopenr ("unamed", 0, fd);
+ abfd = gdb_bfd_ref (bfd_fdopenr ("unamed", 0, fd));
if (abfd == NULL)
{
warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
@@ -3497,7 +3497,7 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
{
/* Not the correct format, so we can not possibly find the dbx_link
symbol in it. */
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
@@ -3511,14 +3511,14 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
if (dbx_link_bpt == NULL)
{
warning (_("Failed to insert dbx_link breakpoint."));
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 1;
}
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
diff --git a/gdb/record.c b/gdb/record.c
index bb0fe52..ec42aac 100644
--- a/gdb/record.c
+++ b/gdb/record.c
@@ -32,6 +32,7 @@
#include "gcore.h"
#include "event-loop.h"
#include "inf-loop.h"
+#include "gdb_bfd.h"
#include <signal.h>
@@ -2638,7 +2639,7 @@ record_save_cleanups (void *data)
bfd *obfd = data;
char *pathname = xstrdup (bfd_get_filename (obfd));
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
unlink (pathname);
xfree (pathname);
}
@@ -2854,7 +2855,7 @@ cmd_record_save (char *args, int from_tty)
}
do_cleanups (set_cleanups);
- bfd_close (obfd);
+ gdb_bfd_unref (obfd);
discard_cleanups (old_cleanups);
/* Succeeded. */
diff --git a/gdb/remote-m32r-sdi.c b/gdb/remote-m32r-sdi.c
index 47f4405..0536ccf 100644
--- a/gdb/remote-m32r-sdi.c
+++ b/gdb/remote-m32r-sdi.c
@@ -39,6 +39,7 @@
#include <sys/time.h>
#include <signal.h>
#include <time.h>
+#include "gdb_bfd.h"
#include "serial.h"
@@ -1257,7 +1258,7 @@ m32r_load (char *args, int from_tty)
if (!filename)
filename = get_exec_file (1);
- pbfd = bfd_openr (filename, gnutarget);
+ pbfd = gdb_bfd_ref (bfd_openr (filename, gnutarget));
if (pbfd == NULL)
{
perror_with_name (filename);
diff --git a/gdb/remote-mips.c b/gdb/remote-mips.c
index babbf19..30fd948 100644
--- a/gdb/remote-mips.c
+++ b/gdb/remote-mips.c
@@ -36,6 +36,7 @@
#include <ctype.h>
#include "mips-tdep.h"
#include "gdbthread.h"
+#include "gdb_bfd.h"
\f
/* Breakpoint types. Values 0, 1, and 2 must agree with the watch
@@ -2787,7 +2788,7 @@ mips_load_srec (char *args)
buffer = alloca (srec_frame * 2 + 256);
- abfd = bfd_openr (args, 0);
+ abfd = gdb_bfd_ref (bfd_openr (args, 0));
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", args);
@@ -3370,7 +3371,7 @@ pmon_load_fast (char *file)
buffer = (char *) xmalloc (MAXRECSIZE + 1);
binbuf = (unsigned char *) xmalloc (BINCHUNK);
- abfd = bfd_openr (file, 0);
+ abfd = gdb_bfd_ref (bfd_openr (file, 0));
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", file);
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 0a2000a..1aa4a17 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -752,6 +752,7 @@ add_vmap (LdInfo *ldi)
abfd = bfd_openr (objname, gnutarget);
else
abfd = bfd_fdopenr (objname, gnutarget, fd);
+ abfd = gdb_bfd_ref (abfd);
if (!abfd)
{
warning (_("Could not open `%s' as an executable file: %s"),
@@ -768,14 +769,14 @@ add_vmap (LdInfo *ldi)
{
last = 0;
/* FIXME??? am I tossing BFDs? bfd? */
- while ((last = bfd_openr_next_archived_file (abfd, last)))
+ while ((last = gdb_bfd_ref (bfd_openr_next_archived_file (abfd, last))))
if (strcmp (mem, last->filename) == 0)
break;
if (!last)
{
warning (_("\"%s\": member \"%s\" missing."), objname, mem);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return NULL;
}
@@ -783,8 +784,8 @@ add_vmap (LdInfo *ldi)
{
warning (_("\"%s\": member \"%s\" not in executable format: %s."),
objname, mem, bfd_errmsg (bfd_get_error ()));
- bfd_close (last);
- bfd_close (abfd);
+ gdb_bfd_unref (last);
+ gdb_bfd_unref (abfd);
return NULL;
}
@@ -794,10 +795,10 @@ add_vmap (LdInfo *ldi)
{
warning (_("\"%s\": not in executable format: %s."),
objname, bfd_errmsg (bfd_get_error ()));
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return NULL;
}
- obj = allocate_objfile (vp->bfd, 0);
+ obj = allocate_objfile (gdb_bfd_ref (vp->bfd), 0);
vp->objfile = obj;
/* Always add symbols for the main objfile. */
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index d52fb46..bc2cd79 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -28,6 +28,7 @@
#include "inferior.h"
#include "regcache.h"
#include "gdbthread.h"
+#include "gdb_bfd.h"
#include "gdb_assert.h"
@@ -367,7 +368,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
return;
/* Create a bfd for the interpreter. */
- dyld_bfd = bfd_openr (interp_name, gnutarget);
+ dyld_bfd = gdb_bfd_ref (bfd_openr (interp_name, gnutarget));
if (dyld_bfd)
{
bfd *sub;
@@ -378,7 +379,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
dyld_bfd = sub;
else
{
- bfd_close (dyld_bfd);
+ gdb_bfd_unref (dyld_bfd);
dyld_bfd = NULL;
}
}
@@ -395,7 +396,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
info->all_image_addr =
lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");
- bfd_close (dyld_bfd);
+ gdb_bfd_unref (dyld_bfd);
if (info->all_image_addr == 0)
return;
@@ -509,7 +510,7 @@ darwin_bfd_open (char *pathname)
gdbarch_bfd_arch_info (target_gdbarch));
if (!res)
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
make_cleanup (xfree, found_pathname);
error (_("`%s': not a shared-library: %s"),
found_pathname, bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 2500c1f..fcc01a8 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -30,6 +30,7 @@
#include "gdbcmd.h"
#include "elf-bfd.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
#define GOT_MODULE_OFFSET 4
@@ -899,7 +900,7 @@ enable_break2 (void)
{
warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
@@ -948,13 +949,13 @@ enable_break2 (void)
"(at address %s) from dynamic linker"),
hex_string_custom (addr + 8, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
/* We're done with the temporary bfd. */
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
/* We're also done with the loadmap. */
xfree (ldm);
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 2f2c8b0..0f59535 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -31,6 +31,7 @@
#include "gdbcmd.h"
#include "elf/frv.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
/* Flag which indicates whether internal debug messages should be printed. */
static int solib_frv_debug;
@@ -574,7 +575,7 @@ enable_break2 (void)
{
warning (_("Unable to determine dynamic linker loadmap address."));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
@@ -589,7 +590,7 @@ enable_break2 (void)
warning (_("Unable to load dynamic linker loadmap at address %s."),
hex_string_custom (interp_loadmap_addr, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
@@ -623,7 +624,7 @@ enable_break2 (void)
warning (_("Could not find symbol _dl_debug_addr "
"in dynamic linker"));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
@@ -674,7 +675,7 @@ enable_break2 (void)
"(at address %s) from dynamic linker"),
hex_string_custom (addr + 8, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
@@ -686,13 +687,13 @@ enable_break2 (void)
"(at address %s) from dynamic linker"),
hex_string_custom (addr, 8));
enable_break_failure_warning ();
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return 0;
}
addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
/* We're done with the temporary bfd. */
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
/* We're also done with the loadmap. */
xfree (ldm);
diff --git a/gdb/solib-pa64.c b/gdb/solib-pa64.c
index 2b8d061..b55b67c 100644
--- a/gdb/solib-pa64.c
+++ b/gdb/solib-pa64.c
@@ -362,7 +362,7 @@ manpage for methods to privately map shared library text."));
to find any magic formula to find it for Solaris (appears to
be trivial on GNU/Linux). Therefore, we have to try an alternate
mechanism to find the dynamic linker's base address. */
- tmp_bfd = bfd_openr (buf, gnutarget);
+ tmp_bfd = gdb_bfd_ref (bfd_openr (buf, gnutarget));
if (tmp_bfd == NULL)
return;
@@ -371,7 +371,7 @@ manpage for methods to privately map shared library text."));
{
warning (_("Unable to grok dynamic linker %s as an object file"),
buf);
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
return;
}
@@ -401,7 +401,7 @@ manpage for methods to privately map shared library text."));
}
/* We're done with the temporary bfd. */
- bfd_close (tmp_bfd);
+ gdb_bfd_unref (tmp_bfd);
}
}
diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
index b5454e7..5f03a42 100644
--- a/gdb/solib-spu.c
+++ b/gdb/solib-spu.c
@@ -36,6 +36,7 @@
#include "breakpoint.h"
#include "gdbthread.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
#include "spu-tdep.h"
@@ -325,16 +326,16 @@ spu_bfd_fopen (char *name, CORE_ADDR addr)
CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
*open_closure = addr;
- nbfd = bfd_openr_iovec (xstrdup (name), "elf32-spu",
- spu_bfd_iovec_open, open_closure,
- spu_bfd_iovec_pread, spu_bfd_iovec_close,
- spu_bfd_iovec_stat);
+ nbfd = gdb_bfd_ref (bfd_openr_iovec (xstrdup (name), "elf32-spu",
+ spu_bfd_iovec_open, open_closure,
+ spu_bfd_iovec_pread, spu_bfd_iovec_close,
+ spu_bfd_iovec_stat));
if (!nbfd)
return NULL;
if (!bfd_check_format (nbfd, bfd_object))
{
- bfd_close (nbfd);
+ gdb_bfd_unref (nbfd);
return NULL;
}
diff --git a/gdb/solib.c b/gdb/solib.c
index 90439ba..7b9f473 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -46,6 +46,7 @@
#include "solib.h"
#include "interps.h"
#include "filesystem.h"
+#include "gdb_bfd.h"
/* Architecture-specific operations. */
@@ -389,7 +390,7 @@ solib_bfd_fopen (char *pathname, int fd)
pathname, bfd_errmsg (bfd_get_error ()));
}
- return abfd;
+ return gdb_bfd_ref (abfd);
}
/* Find shared library PATHNAME and open a BFD for it. */
@@ -420,7 +421,7 @@ solib_bfd_open (char *pathname)
/* Check bfd format. */
if (!bfd_check_format (abfd, bfd_object))
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
make_cleanup (xfree, found_pathname);
error (_("`%s': not in executable format: %s"),
found_pathname, bfd_errmsg (bfd_get_error ()));
@@ -466,7 +467,7 @@ solib_map_sections (struct so_list *so)
return 0;
/* Leave bfd open, core_xfer_memory and "info files" need it. */
- so->abfd = gdb_bfd_ref (abfd);
+ so->abfd = abfd;
/* copy full path name into so_name, so that later symbol_file_add
can find it. */
@@ -608,7 +609,7 @@ solib_read_symbols (struct so_list *so, int flags)
sap = build_section_addr_info_from_section_table (so->sections,
so->sections_end);
- so->objfile = symbol_file_add_from_bfd (so->abfd,
+ so->objfile = symbol_file_add_from_bfd (gdb_bfd_ref (so->abfd),
flags, sap, OBJF_SHARED,
NULL);
so->objfile->addr_low = so->addr_low;
@@ -1233,7 +1234,7 @@ reload_shared_libraries_1 (int from_tty)
{
found_pathname = xstrdup (bfd_get_filename (abfd));
make_cleanup (xfree, found_pathname);
- gdb_bfd_close_or_warn (abfd);
+ gdb_bfd_unref (abfd);
}
/* If this shared library is no longer associated with its previous
diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
index 2dfec8c..aeb7242 100644
--- a/gdb/spu-linux-nat.c
+++ b/gdb/spu-linux-nat.c
@@ -321,10 +321,11 @@ spu_bfd_open (ULONGEST addr)
spu_bfd_iovec_stat);
if (!nbfd)
return NULL;
+ nbfd = gdb_bfd_ref (nbfd);
if (!bfd_check_format (nbfd, bfd_object))
{
- bfd_close (nbfd);
+ gdb_bfd_unref (nbfd);
return NULL;
}
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index e80fd25..1c306fa 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -54,6 +54,7 @@
#include "observer.h"
#include "auxv.h"
#include "elf/common.h"
+#include "gdb_bfd.h"
/* Verify parameters of target_read_memory_bfd and target_read_memory are
compatible. */
@@ -100,6 +101,7 @@ symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr, char *name,
if (nbfd == NULL)
error (_("Failed to read a valid object file image from memory."));
+ gdb_bfd_ref (nbfd);
if (name == NULL)
nbfd->filename = xstrdup ("shared object read from target memory");
else
@@ -107,10 +109,7 @@ symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr, char *name,
if (!bfd_check_format (nbfd, bfd_object))
{
- /* FIXME: should be checking for errors from bfd_close (for one thing,
- on error it does not free all the storage associated with the
- bfd). */
- bfd_close (nbfd);
+ make_cleanup_bfd_close (nbfd);
error (_("Got object file from memory but can't read symbols: %s."),
bfd_errmsg (bfd_get_error ()));
}
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 01252e2..2ad72c5 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -55,6 +55,7 @@
#include "solib.h"
#include "remote.h"
#include "stack.h"
+#include "gdb_bfd.h"
#include <sys/types.h>
#include <fcntl.h>
@@ -1372,7 +1373,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
if (abfd_stat.st_dev == parent_stat.st_dev
&& abfd_stat.st_ino == parent_stat.st_ino)
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return 0;
}
verified_as_different = 1;
@@ -1382,7 +1383,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
file_crc_p = get_file_crc (abfd, &file_crc);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
if (!file_crc_p)
return 0;
@@ -1690,15 +1691,16 @@ set_initial_language (void)
}
/* If NAME is a remote name open the file using remote protocol, otherwise
- open it normally. */
+ open it normally. Returns a new reference to the BFD. On error,
+ returns NULL with the BFD error set. */
bfd *
bfd_open_maybe_remote (const char *name)
{
if (remote_filename_p (name))
- return remote_bfd_open (name, gnutarget);
+ return gdb_bfd_ref (remote_bfd_open (name, gnutarget));
else
- return bfd_openr (name, gnutarget);
+ return gdb_bfd_ref (bfd_openr (name, gnutarget));
}
@@ -1717,7 +1719,7 @@ symfile_bfd_open (char *name)
if (remote_filename_p (name))
{
name = xstrdup (name);
- sym_bfd = remote_bfd_open (name, gnutarget);
+ sym_bfd = gdb_bfd_ref (remote_bfd_open (name, gnutarget));
if (!sym_bfd)
{
make_cleanup (xfree, name);
@@ -1727,7 +1729,7 @@ symfile_bfd_open (char *name)
if (!bfd_check_format (sym_bfd, bfd_object))
{
- bfd_close (sym_bfd);
+ gdb_bfd_unref (sym_bfd);
make_cleanup (xfree, name);
error (_("`%s': can't read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
@@ -1762,7 +1764,7 @@ symfile_bfd_open (char *name)
xfree (name);
name = absolute_name;
- sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
+ sym_bfd = gdb_bfd_ref (bfd_fopen (name, gnutarget, FOPEN_RB, desc));
if (!sym_bfd)
{
make_cleanup (xfree, name);
@@ -1773,18 +1775,12 @@ symfile_bfd_open (char *name)
if (!bfd_check_format (sym_bfd, bfd_object))
{
- /* FIXME: should be checking for errors from bfd_close (for one
- thing, on error it does not free all the storage associated
- with the bfd). */
- bfd_close (sym_bfd); /* This also closes desc. */
+ make_cleanup_bfd_close (sym_bfd);
make_cleanup (xfree, name);
error (_("`%s': can't read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
}
- /* bfd_usrdata exists for applications and libbfd must not touch it. */
- gdb_assert (bfd_usrdata (sym_bfd) == NULL);
-
return sym_bfd;
}
@@ -2109,16 +2105,13 @@ generic_load (char *args, int from_tty)
}
/* Open the file for loading. */
- loadfile_bfd = bfd_openr (filename, gnutarget);
+ loadfile_bfd = gdb_bfd_ref (bfd_openr (filename, gnutarget));
if (loadfile_bfd == NULL)
{
perror_with_name (filename);
return;
}
- /* FIXME: should be checking for errors from bfd_close (for one thing,
- on error it does not free all the storage associated with the
- bfd). */
make_cleanup_bfd_close (loadfile_bfd);
if (!bfd_check_format (loadfile_bfd, bfd_object))
@@ -2519,14 +2512,10 @@ reread_symbols (void)
to close the descriptor but BFD lacks a way of closing the
BFD without closing the descriptor. */
obfd_filename = bfd_get_filename (objfile->obfd);
- if (!bfd_close (objfile->obfd))
- error (_("Can't close BFD for %s: %s"), objfile->name,
- bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_unref (objfile->obfd);
objfile->obfd = bfd_open_maybe_remote (obfd_filename);
if (objfile->obfd == NULL)
error (_("Can't open %s to read symbols."), objfile->name);
- else
- objfile->obfd = gdb_bfd_ref (objfile->obfd);
/* bfd_openr sets cacheable to true, which is what we want. */
if (!bfd_check_format (objfile->obfd, bfd_object))
error (_("Can't read symbols from %s: %s."), objfile->name,
diff --git a/gdb/utils.c b/gdb/utils.c
index 5566149..d268999 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -27,6 +27,7 @@
#include "exceptions.h"
#include "gdbthread.h"
#include "fnmatch.h"
+#include "gdb_bfd.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif /* HAVE_SYS_RESOURCE_H */
@@ -198,7 +199,7 @@ make_cleanup_dyn_string_delete (dyn_string_t arg)
static void
do_bfd_close_cleanup (void *arg)
{
- bfd_close (arg);
+ gdb_bfd_unref (arg);
}
struct cleanup *
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index f16588a..6ba92d9 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -752,7 +752,7 @@ windows_make_so (const char *name, LPVOID load_addr)
asection *text = NULL;
CORE_ADDR text_vma;
- abfd = bfd_openr (so->so_name, "pei-i386");
+ abfd = gdb_bfd_ref (bfd_openr (so->so_name, "pei-i386"));
if (!abfd)
return so;
@@ -762,7 +762,7 @@ windows_make_so (const char *name, LPVOID load_addr)
if (!text)
{
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
return so;
}
@@ -773,7 +773,7 @@ windows_make_so (const char *name, LPVOID load_addr)
load_addr + 0x1000);
cygwin_load_end = cygwin_load_start + bfd_section_size (abfd, text);
- bfd_close (abfd);
+ gdb_bfd_unref (abfd);
}
#endif
--
1.7.7.6
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-18 19:32 [PATCH 01/10] change gdb to refcount bfd everywhere Tom Tromey
@ 2012-07-19 14:18 ` Jan Kratochvil
2012-07-19 20:58 ` Tom Tromey
2012-07-20 16:31 ` Tom Tromey
0 siblings, 2 replies; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-19 14:18 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Wed, 18 Jul 2012 21:31:59 +0200, Tom Tromey wrote:
> --- /dev/null
> +++ b/gdb/gdb_bfd.c
> @@ -0,0 +1,90 @@
[...]
> +/* Add reference to ABFD. Returns ABFD. */
> +
> +struct bfd *
> +gdb_bfd_ref (struct bfd *abfd)
[...]
> +/* Unreference and possibly close ABFD. */
> +
> +void
> +gdb_bfd_unref (struct bfd *abfd)
[...]
> --- /dev/null
> +++ b/gdb/gdb_bfd.h
> @@ -0,0 +1,35 @@
[...]
> +/* Acquire a new reference to ABFD. Returns ABFD for convenience.
> + It is fine for ABFD to be NULL; in this case the function does
> + nothing and returns NULL. */
> +
> +struct bfd *gdb_bfd_ref (struct bfd *abfd);
> +
> +/* Release a reference to ABFD. If this is the last reference, ABFD
> + will be freed. If ABFD is NULL, this function does nothing. */
> +
> +void gdb_bfd_unref (struct bfd *abfd);
The comments are present in both and neither is a reference, they are already
out of sync.
> diff --git a/gdb/jit.c b/gdb/jit.c
> index 568d17b..6a1425c 100644
> --- a/gdb/jit.c
> +++ b/gdb/jit.c
> @@ -861,6 +862,7 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
> puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
> return;
> }
> + nbfd = gdb_bfd_ref (nbfd);
I personally disagree about the returned value from gdb_bfd_ref being useful,
it makes the code more magic IMO (plus it is not much compatible with the
narrow GNU Coding Standard code formatting). But when it is already this way
then this gdb_bfd_ref wrapped should have been around
bfd_open_from_target_memory above as gdb_bfd_ref can handle NULL parameter.
> @@ -193,9 +194,9 @@ allocate_objfile (bfd *abfd, int flags)
>
> /* Update the per-objfile information that comes from the bfd, ensuring
> that any data that is reference is saved in the per-objfile data
> - region. */
> + region. Note that we steal a reference to ABFD. */
>
> - objfile->obfd = gdb_bfd_ref (abfd);
> + objfile->obfd = abfd;
Caller could gdb_bfd_unref its reference but YMMV.
> @@ -794,10 +795,10 @@ add_vmap (LdInfo *ldi)
> {
> warning (_("\"%s\": not in executable format: %s."),
> objname, bfd_errmsg (bfd_get_error ()));
> - bfd_close (abfd);
> + gdb_bfd_unref (abfd);
> return NULL;
> }
> - obj = allocate_objfile (vp->bfd, 0);
> + obj = allocate_objfile (gdb_bfd_ref (vp->bfd), 0);
This code is a bit magic due to it. map_vmap is missing gdb_bfd_ref despite
it creates new reference etc.
This all pain would not exist with C++.
> @@ -2519,14 +2512,10 @@ reread_symbols (void)
> to close the descriptor but BFD lacks a way of closing the
> BFD without closing the descriptor. */
> obfd_filename = bfd_get_filename (objfile->obfd);
> - if (!bfd_close (objfile->obfd))
> - error (_("Can't close BFD for %s: %s"), objfile->name,
> - bfd_errmsg (bfd_get_error ()));
> + gdb_bfd_unref (objfile->obfd);
> objfile->obfd = bfd_open_maybe_remote (obfd_filename);
> if (objfile->obfd == NULL)
> error (_("Can't open %s to read symbols."), objfile->name);
> - else
> - objfile->obfd = gdb_bfd_ref (objfile->obfd);
Why isn't gdb_bfd_ref missing here?
> /* bfd_openr sets cacheable to true, which is what we want. */
> if (!bfd_check_format (objfile->obfd, bfd_object))
> error (_("Can't read symbols from %s: %s."), objfile->name,
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-19 14:18 ` Jan Kratochvil
@ 2012-07-19 20:58 ` Tom Tromey
2012-07-20 16:37 ` Tom Tromey
` (2 more replies)
2012-07-20 16:31 ` Tom Tromey
1 sibling, 3 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-19 20:58 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> The comments are present in both and neither is a reference, they
Jan> are already out of sync.
I don't really follow, but I also don't mind changing the comments --
they ought to be clear for everybody, not just me. How about this?
/* Increment the reference count of ABFD. It is fine for ABFD to be
NULL; in this case the function does nothing. */
void gdb_bfd_ref (struct bfd *abfd);
/* Decrement the reference count of ABFD. If this is the last
reference, ABFD will be freed. If ABFD is NULL, this function does
nothing. */
void gdb_bfd_unref (struct bfd *abfd);
Jan> I personally disagree about the returned value from gdb_bfd_ref
Jan> being useful, it makes the code more magic IMO (plus it is not much
Jan> compatible with the narrow GNU Coding Standard code formatting).
I made this change. I'll test the patch and send it tomorrow.
Then I went a bit further and wrote a follow-up patch that wraps the
various BFD-opening functions with gdb equivalents. These initialize
the refcount and stash the filename.
E.g.:
/* A wrapper for bfd_fopen that initializes the gdb-specific reference
count and calls gdb_bfd_stash_filename. */
bfd *gdb_bfd_fopen (const char *, const char *, const char *, int);
I'll send this patch separately tomorrow. I'm curious to know what you
think. I find it cleans up the code quite a bit, I wish I'd done this
from the beginning.
>> @@ -193,9 +194,9 @@ allocate_objfile (bfd *abfd, int flags)
[...]
>> - objfile->obfd = gdb_bfd_ref (abfd);
>> + objfile->obfd = abfd;
Jan> Caller could gdb_bfd_unref its reference but YMMV.
I will look into this tomorrow.
Jan> This all pain would not exist with C++.
Yeah, well...
>> @@ -2519,14 +2512,10 @@ reread_symbols (void)
>> to close the descriptor but BFD lacks a way of closing the
>> BFD without closing the descriptor. */
>> obfd_filename = bfd_get_filename (objfile->obfd);
>> - if (!bfd_close (objfile->obfd))
>> - error (_("Can't close BFD for %s: %s"), objfile->name,
>> - bfd_errmsg (bfd_get_error ()));
>> + gdb_bfd_unref (objfile->obfd);
>> objfile-> obfd = bfd_open_maybe_remote (obfd_filename);
>> if (objfile->obfd == NULL)
>> error (_("Can't open %s to read symbols."), objfile->name);
>> - else
>> - objfile->obfd = gdb_bfd_ref (objfile->obfd);
Jan> Why isn't gdb_bfd_ref missing here?
bfd_open_maybe_remote returns a new reference.
I wonder if I should rename it to gdb_bfd_open_maybe_remote, for
consistency. What do you think? I'm inclined to do it.
Tom
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-19 20:58 ` Tom Tromey
@ 2012-07-20 16:37 ` Tom Tromey
2012-07-23 8:54 ` Jan Kratochvil
2012-07-20 16:37 ` Tom Tromey
2012-07-22 19:00 ` Jan Kratochvil
2 siblings, 1 reply; 30+ messages in thread
From: Tom Tromey @ 2012-07-20 16:37 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>> - objfile->obfd = gdb_bfd_ref (abfd);
>>> + objfile->obfd = abfd;
Jan> Caller could gdb_bfd_unref its reference but YMMV.
Tom> I will look into this tomorrow.
This changes allocate_objfile to acquire a reference to the BFD,
rather than steal a reference. Then it changes all callers, direct or
indirect, to follow.
I'm still undecided as to whether this patch is an improvement. I
think it does fix a couple of obscure latent memory leaks.
In the abstract it seems like it should be an improvement, since it
makes the rules regarding BFD reference counting more local. In
practice, though, this means adding a bunch of cleanups, which tends
to obscure the code.
Regtested on x86-64 Fedora 16.
Let me know what you think.
* coffread.c (coff_symfile_read): Make a cleanup for 'debugfile'
and 'abfd'.
* elfread.c (elf_symfile_read): Make a cleanup for 'debugfile'
and 'abfd'.
* jit.c (jit_bfd_try_read_symtab): Make a cleanup for 'nbfd'.
* machoread.c (macho_add_oso_symfile): Make a cleanup for
'abfd'.
(macho_symfile_read): Make a cleanup for 'dsym_bfd'.
* objfiles.c (allocate_objfile): Acquire a new reference.
* rs6000-nat.c (add_vmap): Don't acquire a BFD reference.
* solib.c (solib_read_symbols): Don't acquire a BFD reference.
* spu-linux-nat.c (spu_symbol_file_add_from_memory): Make
a cleanup for 'nbfd'.
* symfile-mem.c (symbol_file_add_from_memory): Make a cleanup
for 'nbfd'.
* symfile.c (symbol_file_add_with_addrs_or_offsets): Don't
make a cleanup for 'abfd'.
(symbol_file_add): Make a BFD cleanup.
---
gdb/coffread.c | 3 ++-
gdb/elfread.c | 4 +++-
gdb/jit.c | 3 ++-
gdb/machoread.c | 6 ++++++
gdb/objfiles.c | 3 ++-
gdb/rs6000-nat.c | 1 -
gdb/solib.c | 1 -
gdb/spu-linux-nat.c | 9 +++++++--
gdb/symfile-mem.c | 11 +++++------
gdb/symfile.c | 14 ++++++++------
10 files changed, 35 insertions(+), 20 deletions(-)
diff --git a/gdb/coffread.c b/gdb/coffread.c
index b0a8b82..0c7e6d9 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -653,13 +653,14 @@ coff_symfile_read (struct objfile *objfile, int symfile_flags)
char *debugfile;
debugfile = find_separate_debug_file_by_debuglink (objfile);
+ make_cleanup (xfree, debugfile);
if (debugfile)
{
bfd *abfd = symfile_bfd_open (debugfile);
+ make_cleanup_bfd_unref (abfd);
symbol_file_add_separate (abfd, symfile_flags, objfile);
- xfree (debugfile);
}
}
diff --git a/gdb/elfread.c b/gdb/elfread.c
index de61a9a..608a868 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -1445,10 +1445,12 @@ elf_symfile_read (struct objfile *objfile, int symfile_flags)
if (debugfile)
{
+ struct cleanup *cleanup = make_cleanup (xfree, debugfile);
bfd *abfd = symfile_bfd_open (debugfile);
+ make_cleanup_bfd_unref (abfd);
symbol_file_add_separate (abfd, symfile_flags, objfile);
- xfree (debugfile);
+ do_cleanups (cleanup);
}
}
diff --git a/gdb/jit.c b/gdb/jit.c
index 410b94d..cdd9f49 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -896,7 +896,8 @@ JITed symbol file is not an object file, ignoring it.\n"));
++i;
}
- /* This call takes ownership of NBFD. It does not take ownership of SAI. */
+ /* This call does not take ownership of SAI. */
+ make_cleanup_bfd_unref (nbfd);
objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
do_cleanups (old_cleanups);
diff --git a/gdb/machoread.c b/gdb/machoread.c
index 5b9e2ba..0d7578a 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -454,6 +454,7 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
asymbol **symp;
struct bfd_hash_table table;
int nbr_sections;
+ struct cleanup *cleanup;
/* Per section flag to mark which section have been rebased. */
unsigned char *sections_rebased;
@@ -631,11 +632,13 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
/* We need to clear SYMFILE_MAINLINE to avoid interractive question
from symfile.c:symbol_file_add_with_addrs_or_offsets. */
+ cleanup = make_cleanup_bfd_unref (abfd);
symbol_file_add_from_bfd
(abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
| OBJF_READNOW | OBJF_USERLOADED),
main_objfile);
+ do_cleanups (cleanup);
}
/* Read symbols from the vector of oso files. */
@@ -897,6 +900,7 @@ macho_symfile_read (struct objfile *objfile, int symfile_flags)
int ix;
oso_el *oso;
struct bfd_section *asect, *dsect;
+ struct cleanup *cleanup;
if (mach_o_debug_level > 0)
printf_unfiltered (_("dsym file found\n"));
@@ -917,7 +921,9 @@ macho_symfile_read (struct objfile *objfile, int symfile_flags)
}
/* Add the dsym file as a separate file. */
+ cleanup = make_cleanup_bfd_unref (dsym_bfd);
symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
+ do_cleanups (cleanup);
/* Don't try to read dwarf2 from main file or shared libraries. */
return;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 5ff0eb2..411618f 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -194,9 +194,10 @@ allocate_objfile (bfd *abfd, int flags)
/* Update the per-objfile information that comes from the bfd, ensuring
that any data that is reference is saved in the per-objfile data
- region. Note that we steal a reference to ABFD. */
+ region. */
objfile->obfd = abfd;
+ gdb_bfd_ref (abfd);
if (abfd != NULL)
{
/* Look up the gdbarch associated with the BFD. */
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 140012b..8c3f546 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -798,7 +798,6 @@ add_vmap (LdInfo *ldi)
gdb_bfd_unref (abfd);
return NULL;
}
- gdb_bfd_ref (vp->bfd);
obj = allocate_objfile (vp->bfd, 0);
vp->objfile = obj;
diff --git a/gdb/solib.c b/gdb/solib.c
index 9779e10..73773f1 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -610,7 +610,6 @@ solib_read_symbols (struct so_list *so, int flags)
sap = build_section_addr_info_from_section_table (so->sections,
so->sections_end);
- gdb_bfd_ref (so->abfd);
so->objfile = symbol_file_add_from_bfd (so->abfd,
flags, sap, OBJF_SHARED,
NULL);
diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
index a17ff3f..999f1ab 100644
--- a/gdb/spu-linux-nat.c
+++ b/gdb/spu-linux-nat.c
@@ -374,8 +374,13 @@ spu_symbol_file_add_from_memory (int inferior_fd)
/* Open BFD representing SPE executable and read its symbols. */
nbfd = spu_bfd_open (addr);
if (nbfd)
- symbol_file_add_from_bfd (nbfd, SYMFILE_VERBOSE | SYMFILE_MAINLINE,
- NULL, 0, NULL);
+ {
+ struct cleanup *cleanup = make_cleanup_bfd_unref (nbfd);
+
+ symbol_file_add_from_bfd (nbfd, SYMFILE_VERBOSE | SYMFILE_MAINLINE,
+ NULL, 0, NULL);
+ do_cleanups (cleanup);
+ }
}
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index ad87abd..2e53be0 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -111,15 +111,14 @@ symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr, char *name,
xfree (name);
}
+ cleanup = make_cleanup_bfd_unref (nbfd);
+
if (!bfd_check_format (nbfd, bfd_object))
- {
- make_cleanup_bfd_unref (nbfd);
- error (_("Got object file from memory but can't read symbols: %s."),
- bfd_errmsg (bfd_get_error ()));
- }
+ error (_("Got object file from memory but can't read symbols: %s."),
+ bfd_errmsg (bfd_get_error ()));
sai = alloc_section_addr_info (bfd_count_sections (nbfd));
- cleanup = make_cleanup (xfree, sai);
+ make_cleanup (xfree, sai);
i = 0;
for (sec = nbfd->sections; sec != NULL; sec = sec->next)
if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 64c4a3b..e1d5c15 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1037,7 +1037,7 @@ new_symfile_objfile (struct objfile *objfile, int add_flags)
loaded file.
ABFD is a BFD already open on the file, as from symfile_bfd_open.
- This BFD will be closed on error, and is always consumed by this function.
+ A new reference is acquired by this function.
ADD_FLAGS encodes verbosity, whether this is main symbol file or
extra, such as dynamically loaded code, and what to do with breakpoins.
@@ -1061,7 +1061,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
int flags, struct objfile *parent)
{
struct objfile *objfile;
- struct cleanup *my_cleanups;
const char *name = bfd_get_filename (abfd);
const int from_tty = add_flags & SYMFILE_VERBOSE;
const int mainline = add_flags & SYMFILE_MAINLINE;
@@ -1075,8 +1074,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
add_flags &= ~SYMFILE_NO_READ;
}
- my_cleanups = make_cleanup_bfd_unref (abfd);
-
/* Give user a chance to burp if we'd be
interactively wiping out any existing symbols. */
@@ -1087,7 +1084,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
error (_("Not confirmed."));
objfile = allocate_objfile (abfd, flags | (mainline ? OBJF_MAINLINE : 0));
- discard_cleanups (my_cleanups);
if (parent)
add_separate_debug_objfile (objfile, parent);
@@ -1208,8 +1204,14 @@ struct objfile *
symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs,
int flags)
{
- return symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs,
+ bfd *bfd = symfile_bfd_open (name);
+ struct cleanup *cleanup = make_cleanup_bfd_unref (bfd);
+ struct objfile *objf;
+
+ objf = symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs,
flags, NULL);
+ do_cleanups (cleanup);
+ return objf;
}
--
1.7.7.6
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-20 16:37 ` Tom Tromey
@ 2012-07-23 8:54 ` Jan Kratochvil
2012-07-23 9:06 ` Jan Kratochvil
` (2 more replies)
0 siblings, 3 replies; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-23 8:54 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Fri, 20 Jul 2012 18:37:26 +0200, Tom Tromey wrote:
> This changes allocate_objfile to acquire a reference to the BFD,
> rather than steal a reference. Then it changes all callers, direct or
> indirect, to follow.
map_vmap still steals a reference.
> I'm still undecided as to whether this patch is an improvement. I
> think it does fix a couple of obscure latent memory leaks.
I find the code the only way "how it should be" now.
> In practice, though, this means adding a bunch of cleanups, which tends to
> obscure the code.
This should be fixed with the transition to C++.
> Let me know what you think.
I can't speak for each patch individually but all the 3 patches together seem
right to me. Even solib_read_symbols looks as fixed now.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 8:54 ` Jan Kratochvil
@ 2012-07-23 9:06 ` Jan Kratochvil
2012-07-23 15:23 ` Tom Tromey
2012-07-23 14:52 ` Tom Tromey
2012-07-23 18:54 ` Tom Tromey
2 siblings, 1 reply; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-23 9:06 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Mon, 23 Jul 2012 10:53:55 +0200, Jan Kratochvil wrote:
> map_vmap still steals a reference.
And solib-svr4.c enable_break <interp_name> IMO leaks tmp_bfd.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 9:06 ` Jan Kratochvil
@ 2012-07-23 15:23 ` Tom Tromey
2012-07-23 15:25 ` Tom Tromey
2012-07-24 13:17 ` Jan Kratochvil
0 siblings, 2 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 15:23 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> And solib-svr4.c enable_break <interp_name> IMO leaks tmp_bfd.
Thanks for catching this.
Here is the patch.
Tom
2012-07-23 Tom Tromey <tromey@redhat.com>
* solib-svr4.c (enable_break): Update.
* bfd-target.h (target_bfd_reopen): Update documentation.
diff --git a/gdb/bfd-target.h b/gdb/bfd-target.h
index 71001c5..7f4e628 100644
--- a/gdb/bfd-target.h
+++ b/gdb/bfd-target.h
@@ -23,9 +23,9 @@
struct bfd;
struct target_ops;
-/* Given an existing BFD, re-open it as a "struct target_ops". On
- close, it will also close the corresponding BFD (which is like
- freopen and fdopen). */
+/* Given an existing BFD, re-open it as a "struct target_ops". This
+ acquires a new reference to the BFD. This reference will be
+ released when the target is closed. */
struct target_ops *target_bfd_reopen (struct bfd *bfd);
#endif
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 307e483..b636b71 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -1558,9 +1558,11 @@ enable_break (struct svr4_info *info, int from_tty)
goto bkpt_at_symbol;
/* Now convert the TMP_BFD into a target. That way target, as
- well as BFD operations can be used. Note that closing the
- target will also close the underlying bfd. */
+ well as BFD operations can be used. */
tmp_bfd_target = target_bfd_reopen (tmp_bfd);
+ /* target_bfd_reopen acquired its own reference, so we can
+ release ours now. */
+ gdb_bfd_unref (tmp_bfd);
/* On a running target, we can get the dynamic linker's base
address from the shared library table. */
@@ -1670,8 +1672,9 @@ enable_break (struct svr4_info *info, int from_tty)
sym_addr,
tmp_bfd_target);
- /* We're done with both the temporary bfd and target. Remember,
- closing the target closes the underlying bfd. */
+ /* We're done with both the temporary bfd and target. Closing
+ the target closes the underlying bfd, because it holds the
+ only remaining reference. */
target_close (tmp_bfd_target, 0);
if (sym_addr != 0)
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 15:23 ` Tom Tromey
@ 2012-07-23 15:25 ` Tom Tromey
2012-07-24 13:17 ` Jan Kratochvil
1 sibling, 0 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 15:25 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:
Tom> Here is the patch.
Oops, I sent the wrong one.
The right one has the gdb_bfd.h include.
Sorry about that.
Tom
2012-07-23 Tom Tromey <tromey@redhat.com>
* solib-svr4.c (enable_break): Update.
* bfd-target.h (target_bfd_reopen): Update documentation.
Index: bfd-target.h
===================================================================
RCS file: /cvs/src/src/gdb/bfd-target.h,v
retrieving revision 1.10
diff -u -r1.10 bfd-target.h
--- bfd-target.h 4 Jan 2012 08:16:59 -0000 1.10
+++ bfd-target.h 23 Jul 2012 15:24:45 -0000
@@ -23,9 +23,9 @@
struct bfd;
struct target_ops;
-/* Given an existing BFD, re-open it as a "struct target_ops". On
- close, it will also close the corresponding BFD (which is like
- freopen and fdopen). */
+/* Given an existing BFD, re-open it as a "struct target_ops". This
+ acquires a new reference to the BFD. This reference will be
+ released when the target is closed. */
struct target_ops *target_bfd_reopen (struct bfd *bfd);
#endif
Index: solib-svr4.c
===================================================================
RCS file: /cvs/src/src/gdb/solib-svr4.c,v
retrieving revision 1.164
diff -u -r1.164 solib-svr4.c
--- solib-svr4.c 5 Jun 2012 15:44:03 -0000 1.164
+++ solib-svr4.c 23 Jul 2012 15:24:45 -0000
@@ -46,6 +46,7 @@
#include "exec.h"
#include "auxv.h"
#include "exceptions.h"
+#include "gdb_bfd.h"
static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
static int svr4_have_link_map_offsets (void);
@@ -1558,9 +1559,11 @@
goto bkpt_at_symbol;
/* Now convert the TMP_BFD into a target. That way target, as
- well as BFD operations can be used. Note that closing the
- target will also close the underlying bfd. */
+ well as BFD operations can be used. */
tmp_bfd_target = target_bfd_reopen (tmp_bfd);
+ /* target_bfd_reopen acquired its own reference, so we can
+ release ours now. */
+ gdb_bfd_unref (tmp_bfd);
/* On a running target, we can get the dynamic linker's base
address from the shared library table. */
@@ -1670,8 +1673,9 @@
sym_addr,
tmp_bfd_target);
- /* We're done with both the temporary bfd and target. Remember,
- closing the target closes the underlying bfd. */
+ /* We're done with both the temporary bfd and target. Closing
+ the target closes the underlying bfd, because it holds the
+ only remaining reference. */
target_close (tmp_bfd_target, 0);
if (sym_addr != 0)
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 15:23 ` Tom Tromey
2012-07-23 15:25 ` Tom Tromey
@ 2012-07-24 13:17 ` Jan Kratochvil
2012-07-24 19:52 ` Tom Tromey
1 sibling, 1 reply; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-24 13:17 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Mon, 23 Jul 2012 17:22:50 +0200, Tom Tromey wrote:
> >>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
>
> Jan> And solib-svr4.c enable_break <interp_name> IMO leaks tmp_bfd.
>
> Thanks for catching this.
I think there should be 'maint info bfds' now when you even already track bfds
in gdb_bfd_cache, otherwise it may be difficult to track down all such leaks.
(Maybe it was obvious.)
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-24 13:17 ` Jan Kratochvil
@ 2012-07-24 19:52 ` Tom Tromey
2012-07-24 20:27 ` Eli Zaretskii
2012-07-25 11:57 ` Jan Kratochvil
0 siblings, 2 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-24 19:52 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> I think there should be 'maint info bfds' now when you even already
Jan> track bfds in gdb_bfd_cache, otherwise it may be difficult to track
Jan> down all such leaks. (Maybe it was obvious.)
It wasn't obvious to me -- nice idea.
Here's a patch. It needs a doc review.
Tom
* NEWS: Mention maint info bfd.
* gdb_bfd.c (all_bfds): New global.
(gdb_bfd_ref, gdb_bfd_unref): Update all_bfds.
(print_one_bfd, maintenance_info_bfd, _initialize_gdb_bfd):
New functions.
* gdb.texinfo (Maintenance Commands): Document maint info bfd.
---
gdb/NEWS | 5 +++
gdb/doc/gdb.texinfo | 5 +++
gdb/gdb_bfd.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+), 0 deletions(-)
diff --git a/gdb/NEWS b/gdb/NEWS
index 3333810..91415e2 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,11 @@
*** Changes since GDB 7.5
+* New commands (for set/show, see "New options" below)
+
+maint info bfd
+ List the BFDs known to GDB.
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 68ea817..e08838e 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -34536,6 +34536,11 @@ Shared library events.
@end table
+@kindex maint info bfd
+@item maint info bfd
+This prints information about each @code{bfd} object that is known to
+@value{GDBN}.
+
@kindex set displaced-stepping
@kindex show displaced-stepping
@cindex displaced stepping support
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index aa222b9..c0470ba 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -22,6 +22,8 @@
#include "gdb_bfd.h"
#include "gdb_assert.h"
#include "gdb_string.h"
+#include "ui-out.h"
+#include "gdbcmd.h"
#include "hashtab.h"
#ifdef HAVE_ZLIB_H
#include <zlib.h>
@@ -48,6 +50,13 @@ struct gdb_bfd_section_data
void *map_addr;
};
+/* A hash table holding every BFD that gdb knows about. This is not
+ to be confused with 'gdb_bfd_cache', which is used for sharing
+ BFDs; in contrast, this hash is used just to implement
+ "maint info bfd". */
+
+static htab_t all_bfds;
+
/* See gdb_bfd.h. */
void
@@ -224,6 +233,7 @@ void
gdb_bfd_ref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
+ void **slot;
if (abfd == NULL)
return;
@@ -240,6 +250,11 @@ gdb_bfd_ref (struct bfd *abfd)
gdata->refc = 1;
gdata->mtime = bfd_get_mtime (abfd);
bfd_usrdata (abfd) = gdata;
+
+ /* This is the first we've seen it, so add it to the hash table. */
+ slot = htab_find_slot (all_bfds, abfd, INSERT);
+ gdb_assert (slot && !*slot);
+ *slot = abfd;
}
/* See gdb_bfd.h. */
@@ -249,6 +264,7 @@ gdb_bfd_unref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
struct gdb_bfd_cache_search search;
+ void **slot;
if (abfd == NULL)
return;
@@ -277,6 +293,8 @@ gdb_bfd_unref (struct bfd *abfd)
bfd_usrdata (abfd) = NULL; /* Paranoia. */
+ htab_remove_elt (all_bfds, abfd);
+
gdb_bfd_close_or_warn (abfd);
}
@@ -595,3 +613,58 @@ gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
return result;
}
+
+\f
+
+/* A callback for htab_traverse that prints a single BFD. */
+
+static int
+print_one_bfd (void **slot, void *data)
+{
+ bfd *abfd = *slot;
+ struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
+ struct ui_out *uiout = data;
+ struct cleanup *inner;
+
+ inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+ ui_out_field_int (uiout, "refcount", gdata->refc);
+ ui_out_field_string (uiout, "addr", host_address_to_string (abfd));
+ ui_out_field_string (uiout, "filename", bfd_get_filename (abfd));
+ ui_out_text (uiout, "\n");
+ do_cleanups (inner);
+
+ return 1;
+}
+
+/* Implement the 'maint info bfd' command. */
+
+static void
+maintenance_info_bfd (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ struct ui_out *uiout = current_uiout;
+
+ cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds");
+ ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
+ ui_out_table_header (uiout, 10, ui_left, "addr", "Address");
+ ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");
+
+ ui_out_table_body (uiout);
+ htab_traverse (all_bfds, print_one_bfd, uiout);
+
+ do_cleanups (cleanup);
+}
+
+/* -Wmissing-prototypes */
+extern initialize_file_ftype _initialize_gdb_bfd;
+
+void
+_initialize_gdb_bfd (void)
+{
+ all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
+ NULL, xcalloc, xfree);
+
+ add_cmd ("bfd", class_maintenance, maintenance_info_bfd, _("\
+List the BFDs that are currently open."),
+ &maintenanceinfolist);
+}
--
1.7.7.6
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-24 19:52 ` Tom Tromey
@ 2012-07-24 20:27 ` Eli Zaretskii
2012-07-25 14:25 ` Tom Tromey
2012-07-25 11:57 ` Jan Kratochvil
1 sibling, 1 reply; 30+ messages in thread
From: Eli Zaretskii @ 2012-07-24 20:27 UTC (permalink / raw)
To: Tom Tromey; +Cc: jan.kratochvil, gdb-patches
> From: Tom Tromey <tromey@redhat.com>
> Cc: gdb-patches@sourceware.org
> Date: Tue, 24 Jul 2012 13:52:03 -0600
>
> +* New commands (for set/show, see "New options" below)
> +
> +maint info bfd
> + List the BFDs known to GDB.
> +
> *** Changes in GDB 7.5
OK.
> +@kindex maint info bfd
> +@item maint info bfd
> +This prints information about each @code{bfd} object that is known to
> +@value{GDBN}.
This is fine, but I wonder whether a cross-reference to the BFD manual
will make it even better. AFAICS, we mention "BFD" several times in
the manual, but never say anything about it except that it's a library
for reading binary files. Perhaps having an xref here is more
appropriate than in other places, since whoever would like to use this
command needs to know more about BFD than any other reader.
Thanks.
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-24 20:27 ` Eli Zaretskii
@ 2012-07-25 14:25 ` Tom Tromey
2012-07-25 14:29 ` Jan Kratochvil
2012-07-25 15:37 ` Eli Zaretskii
0 siblings, 2 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-25 14:25 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: jan.kratochvil, gdb-patches
>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:
Eli> This is fine, but I wonder whether a cross-reference to the BFD manual
Eli> will make it even better. AFAICS, we mention "BFD" several times in
Eli> the manual, but never say anything about it except that it's a library
Eli> for reading binary files. Perhaps having an xref here is more
Eli> appropriate than in other places, since whoever would like to use this
Eli> command needs to know more about BFD than any other reader.
Jan> All the 'maint info' commands use plural form:
Here is a new patch that addresses both these reviews.
Ok?
Tom
* NEWS: Mention maint info bfds.
* gdb_bfd.c (all_bfds): New global.
(gdb_bfd_ref, gdb_bfd_unref): Update all_bfds.
(print_one_bfd, maintenance_info_bfds, _initialize_gdb_bfd):
New functions.
* gdb.texinfo (Maintenance Commands): Document maint info bfds.
diff --git a/gdb/NEWS b/gdb/NEWS
index 3333810..4da886c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,11 @@
*** Changes since GDB 7.5
+* New commands (for set/show, see "New options" below)
+
+maint info bfds
+ List the BFDs known to GDB.
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 68ea817..facca8f 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -34536,6 +34536,11 @@ Shared library events.
@end table
+@kindex maint info bfds
+@item maint info bfds
+This prints information about each @code{bfd} object that is known to
+@value{GDBN}. @xref{Top, , BFD, bfd, The Binary File Descriptor Library}.
+
@kindex set displaced-stepping
@kindex show displaced-stepping
@cindex displaced stepping support
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index aa222b9..4b88b7f 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -22,6 +22,8 @@
#include "gdb_bfd.h"
#include "gdb_assert.h"
#include "gdb_string.h"
+#include "ui-out.h"
+#include "gdbcmd.h"
#include "hashtab.h"
#ifdef HAVE_ZLIB_H
#include <zlib.h>
@@ -48,6 +50,13 @@ struct gdb_bfd_section_data
void *map_addr;
};
+/* A hash table holding every BFD that gdb knows about. This is not
+ to be confused with 'gdb_bfd_cache', which is used for sharing
+ BFDs; in contrast, this hash is used just to implement
+ "maint info bfd". */
+
+static htab_t all_bfds;
+
/* See gdb_bfd.h. */
void
@@ -224,6 +233,7 @@ void
gdb_bfd_ref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
+ void **slot;
if (abfd == NULL)
return;
@@ -240,6 +250,11 @@ gdb_bfd_ref (struct bfd *abfd)
gdata->refc = 1;
gdata->mtime = bfd_get_mtime (abfd);
bfd_usrdata (abfd) = gdata;
+
+ /* This is the first we've seen it, so add it to the hash table. */
+ slot = htab_find_slot (all_bfds, abfd, INSERT);
+ gdb_assert (slot && !*slot);
+ *slot = abfd;
}
/* See gdb_bfd.h. */
@@ -249,6 +264,7 @@ gdb_bfd_unref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
struct gdb_bfd_cache_search search;
+ void **slot;
if (abfd == NULL)
return;
@@ -277,6 +293,8 @@ gdb_bfd_unref (struct bfd *abfd)
bfd_usrdata (abfd) = NULL; /* Paranoia. */
+ htab_remove_elt (all_bfds, abfd);
+
gdb_bfd_close_or_warn (abfd);
}
@@ -595,3 +613,58 @@ gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
return result;
}
+
+\f
+
+/* A callback for htab_traverse that prints a single BFD. */
+
+static int
+print_one_bfd (void **slot, void *data)
+{
+ bfd *abfd = *slot;
+ struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
+ struct ui_out *uiout = data;
+ struct cleanup *inner;
+
+ inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+ ui_out_field_int (uiout, "refcount", gdata->refc);
+ ui_out_field_string (uiout, "addr", host_address_to_string (abfd));
+ ui_out_field_string (uiout, "filename", bfd_get_filename (abfd));
+ ui_out_text (uiout, "\n");
+ do_cleanups (inner);
+
+ return 1;
+}
+
+/* Implement the 'maint info bfd' command. */
+
+static void
+maintenance_info_bfds (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ struct ui_out *uiout = current_uiout;
+
+ cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds");
+ ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
+ ui_out_table_header (uiout, 10, ui_left, "addr", "Address");
+ ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");
+
+ ui_out_table_body (uiout);
+ htab_traverse (all_bfds, print_one_bfd, uiout);
+
+ do_cleanups (cleanup);
+}
+
+/* -Wmissing-prototypes */
+extern initialize_file_ftype _initialize_gdb_bfd;
+
+void
+_initialize_gdb_bfd (void)
+{
+ all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
+ NULL, xcalloc, xfree);
+
+ add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
+List the BFDs that are currently open."),
+ &maintenanceinfolist);
+}
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-25 14:25 ` Tom Tromey
@ 2012-07-25 14:29 ` Jan Kratochvil
2012-07-25 14:52 ` Tom Tromey
2012-07-25 15:37 ` Eli Zaretskii
1 sibling, 1 reply; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-25 14:29 UTC (permalink / raw)
To: Tom Tromey; +Cc: Eli Zaretskii, gdb-patches
On Wed, 25 Jul 2012 16:24:57 +0200, Tom Tromey wrote:
> Ok?
> + ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
> + ui_out_table_header (uiout, 10, ui_left, "addr", "Address");
> + ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");
When you ask I was not so happy with the constant widths but I am OK with it as
this is only 'maint' command; still at least there could be like from probe.c:
/* What's the size of an address in our architecture? */
size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-25 14:29 ` Jan Kratochvil
@ 2012-07-25 14:52 ` Tom Tromey
2012-07-25 14:55 ` Jan Kratochvil
0 siblings, 1 reply; 30+ messages in thread
From: Tom Tromey @ 2012-07-25 14:52 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: Eli Zaretskii, gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> When you ask I was not so happy with the constant widths but I am
Jan> OK with it as this is only 'maint' command; still at least there
Jan> could be like from probe.c:
Jan> /* What's the size of an address in our architecture? */
Jan> size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
I'll change it to 18.
It can't vary because there may be BFDs from multiple architectures in
the same listing.
Tom
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-25 14:52 ` Tom Tromey
@ 2012-07-25 14:55 ` Jan Kratochvil
2012-07-25 15:44 ` Tom Tromey
0 siblings, 1 reply; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-25 14:55 UTC (permalink / raw)
To: Tom Tromey; +Cc: Eli Zaretskii, gdb-patches
On Wed, 25 Jul 2012 16:52:21 +0200, Tom Tromey wrote:
> I'll change it to 18.
> It can't vary because there may be BFDs from multiple architectures in
> the same listing.
This is why you should do one scan before to find the maximum used width, like
probe.c does. :-) Still I do not think it matters much for 'info maint', 18
should be enough.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-25 14:55 ` Jan Kratochvil
@ 2012-07-25 15:44 ` Tom Tromey
0 siblings, 0 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-25 15:44 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: Eli Zaretskii, gdb-patches
Jan> This is why you should do one scan before to find the maximum used
Jan> width, like probe.c does. :-) Still I do not think it matters much
Jan> for 'info maint', 18 should be enough.
Yeah.
Here is what I am committing.
Tom
implement maint info bfd
* NEWS: Mention maint info bfds.
* gdb_bfd.c (all_bfds): New global.
(gdb_bfd_ref, gdb_bfd_unref): Update all_bfds.
(print_one_bfd, maintenance_info_bfds, _initialize_gdb_bfd):
New functions.
* gdb.texinfo (Maintenance Commands): Document maint info bfds.
diff --git a/gdb/NEWS b/gdb/NEWS
index 3333810..4da886c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,11 @@
*** Changes since GDB 7.5
+* New commands (for set/show, see "New options" below)
+
+maint info bfds
+ List the BFDs known to GDB.
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 68ea817..facca8f 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -34536,6 +34536,11 @@ Shared library events.
@end table
+@kindex maint info bfds
+@item maint info bfds
+This prints information about each @code{bfd} object that is known to
+@value{GDBN}. @xref{Top, , BFD, bfd, The Binary File Descriptor Library}.
+
@kindex set displaced-stepping
@kindex show displaced-stepping
@cindex displaced stepping support
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index aa222b9..fac8776 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -22,6 +22,8 @@
#include "gdb_bfd.h"
#include "gdb_assert.h"
#include "gdb_string.h"
+#include "ui-out.h"
+#include "gdbcmd.h"
#include "hashtab.h"
#ifdef HAVE_ZLIB_H
#include <zlib.h>
@@ -48,6 +50,13 @@ struct gdb_bfd_section_data
void *map_addr;
};
+/* A hash table holding every BFD that gdb knows about. This is not
+ to be confused with 'gdb_bfd_cache', which is used for sharing
+ BFDs; in contrast, this hash is used just to implement
+ "maint info bfd". */
+
+static htab_t all_bfds;
+
/* See gdb_bfd.h. */
void
@@ -224,6 +233,7 @@ void
gdb_bfd_ref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
+ void **slot;
if (abfd == NULL)
return;
@@ -240,6 +250,11 @@ gdb_bfd_ref (struct bfd *abfd)
gdata->refc = 1;
gdata->mtime = bfd_get_mtime (abfd);
bfd_usrdata (abfd) = gdata;
+
+ /* This is the first we've seen it, so add it to the hash table. */
+ slot = htab_find_slot (all_bfds, abfd, INSERT);
+ gdb_assert (slot && !*slot);
+ *slot = abfd;
}
/* See gdb_bfd.h. */
@@ -249,6 +264,7 @@ gdb_bfd_unref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
struct gdb_bfd_cache_search search;
+ void **slot;
if (abfd == NULL)
return;
@@ -277,6 +293,8 @@ gdb_bfd_unref (struct bfd *abfd)
bfd_usrdata (abfd) = NULL; /* Paranoia. */
+ htab_remove_elt (all_bfds, abfd);
+
gdb_bfd_close_or_warn (abfd);
}
@@ -595,3 +613,58 @@ gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
return result;
}
+
+\f
+
+/* A callback for htab_traverse that prints a single BFD. */
+
+static int
+print_one_bfd (void **slot, void *data)
+{
+ bfd *abfd = *slot;
+ struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
+ struct ui_out *uiout = data;
+ struct cleanup *inner;
+
+ inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+ ui_out_field_int (uiout, "refcount", gdata->refc);
+ ui_out_field_string (uiout, "addr", host_address_to_string (abfd));
+ ui_out_field_string (uiout, "filename", bfd_get_filename (abfd));
+ ui_out_text (uiout, "\n");
+ do_cleanups (inner);
+
+ return 1;
+}
+
+/* Implement the 'maint info bfd' command. */
+
+static void
+maintenance_info_bfds (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ struct ui_out *uiout = current_uiout;
+
+ cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds");
+ ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
+ ui_out_table_header (uiout, 18, ui_left, "addr", "Address");
+ ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");
+
+ ui_out_table_body (uiout);
+ htab_traverse (all_bfds, print_one_bfd, uiout);
+
+ do_cleanups (cleanup);
+}
+
+/* -Wmissing-prototypes */
+extern initialize_file_ftype _initialize_gdb_bfd;
+
+void
+_initialize_gdb_bfd (void)
+{
+ all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
+ NULL, xcalloc, xfree);
+
+ add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
+List the BFDs that are currently open."),
+ &maintenanceinfolist);
+}
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-25 14:25 ` Tom Tromey
2012-07-25 14:29 ` Jan Kratochvil
@ 2012-07-25 15:37 ` Eli Zaretskii
1 sibling, 0 replies; 30+ messages in thread
From: Eli Zaretskii @ 2012-07-25 15:37 UTC (permalink / raw)
To: Tom Tromey; +Cc: jan.kratochvil, gdb-patches
> From: Tom Tromey <tromey@redhat.com>
> Cc: jan.kratochvil@redhat.com, gdb-patches@sourceware.org
> Date: Wed, 25 Jul 2012 08:24:57 -0600
>
> Here is a new patch that addresses both these reviews.
>
> Ok?
Fine with me, thanks.
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-24 19:52 ` Tom Tromey
2012-07-24 20:27 ` Eli Zaretskii
@ 2012-07-25 11:57 ` Jan Kratochvil
1 sibling, 0 replies; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-25 11:57 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Tue, 24 Jul 2012 21:52:03 +0200, Tom Tromey wrote:
> +maint info bfd
> + List the BFDs known to GDB.
All the 'maint info' commands use plural form:
(gdb) maintenance info
breakpoints program-spaces psymtabs sections symtabs
For example info source vs. info sources even makes an appropriate difference
in functionality.
I believe the GDB style would be more the plural form for this command.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 8:54 ` Jan Kratochvil
2012-07-23 9:06 ` Jan Kratochvil
@ 2012-07-23 14:52 ` Tom Tromey
2012-07-23 18:54 ` Tom Tromey
2 siblings, 0 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 14:52 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Tom> This changes allocate_objfile to acquire a reference to the BFD,
Tom> rather than steal a reference. Then it changes all callers, direct or
Tom> indirect, to follow.
Jan> map_vmap still steals a reference.
I'll investigate.
One thing I noticed is that bfd_openr_next_archived_file doesn't
document who owns the resulting BFD. Must one close it? Or is it
closed when the parent BFD is closed? I think the latter; or at least,
if not the latter then the caching mechanism in bfd/archive.c is badly
broken. Anyway, whatever bugs exist here are latent ones already in gdb.
Tom> I'm still undecided as to whether this patch is an improvement. I
Tom> think it does fix a couple of obscure latent memory leaks.
Jan> I find the code the only way "how it should be" now.
Ok. I'm going to put those in.
Tom
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 8:54 ` Jan Kratochvil
2012-07-23 9:06 ` Jan Kratochvil
2012-07-23 14:52 ` Tom Tromey
@ 2012-07-23 18:54 ` Tom Tromey
2012-07-23 19:02 ` Jan Kratochvil
2 siblings, 1 reply; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 18:54 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
Tom> This changes allocate_objfile to acquire a reference to the BFD,
Tom> rather than steal a reference. Then it changes all callers, direct or
Tom> indirect, to follow.
Jan> map_vmap still steals a reference.
Here's what I am checking in for this.
I think I have addressed all your comments now.
Thanks for the reviews.
Tom
2012-07-23 Tom Tromey <tromey@redhat.com>
* rs6000-nat.c (add_vmap): Rewrite archive loop. Fix reference
counting.
* exec.c (exec_close_1): Unconditionally release vmap's BFD.
(map_vmap): Acquire a reference to the BFD.
diff --git a/gdb/exec.c b/gdb/exec.c
index 9b95e02..e076609 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -127,16 +127,13 @@ exec_close_1 (int quitting)
vp = nxt;
nxt = vp->nxt;
- /* if there is an objfile associated with this bfd,
- free_objfile() will do proper cleanup of objfile *and* bfd. */
-
if (vp->objfile)
{
free_objfile (vp->objfile);
need_symtab_cleanup = 1;
}
- else if (vp->bfd != exec_bfd)
- gdb_bfd_unref (vp->bfd);
+
+ gdb_bfd_unref (vp->bfd);
xfree (vp);
}
@@ -548,6 +545,7 @@ map_vmap (bfd *abfd, bfd *arch)
memset ((char *) vp, '\0', sizeof (*vp));
vp->nxt = 0;
vp->bfd = abfd;
+ gdb_bfd_ref (abfd);
vp->name = bfd_get_filename (arch ? arch : abfd);
vp->member = arch ? bfd_get_filename (abfd) : "";
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 8c3f546..9b8efd3 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -765,12 +765,16 @@ add_vmap (LdInfo *ldi)
else if (bfd_check_format (abfd, bfd_archive))
{
- last = 0;
- /* FIXME??? am I tossing BFDs? bfd? */
- while ((last = gdb_bfd_openr_next_archived_file (abfd, last)))
+ last = gdb_bfd_openr_next_archived_file (abfd, NULL);
+ while (last != NULL)
{
+ bfd *next;
+
if (strcmp (mem, last->filename) == 0)
break;
+
+ next = gdb_bfd_openr_next_archived_file (abfd, last);
+ gdb_bfd_unref (last);
}
if (!last)
@@ -790,6 +794,9 @@ add_vmap (LdInfo *ldi)
}
vp = map_vmap (last, abfd);
+ /* map_vmap acquired a reference to LAST, so we can release
+ ours. */
+ gdb_bfd_unref (last);
}
else
{
@@ -804,6 +811,11 @@ add_vmap (LdInfo *ldi)
/* Always add symbols for the main objfile. */
if (vp == vmap || auto_solib_add)
vmap_add_symbols (vp);
+
+ /* Anything needing a reference to ABFD has already acquired it, so
+ release our local reference. */
+ gdb_bfd_unref (abfd);
+
return vp;
}
\f
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 18:54 ` Tom Tromey
@ 2012-07-23 19:02 ` Jan Kratochvil
2012-07-23 19:08 ` Tom Tromey
0 siblings, 1 reply; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-23 19:02 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Mon, 23 Jul 2012 20:53:47 +0200, Tom Tromey wrote:
> else if (bfd_check_format (abfd, bfd_archive))
> {
> - last = 0;
> - /* FIXME??? am I tossing BFDs? bfd? */
> - while ((last = gdb_bfd_openr_next_archived_file (abfd, last)))
> + last = gdb_bfd_openr_next_archived_file (abfd, NULL);
> + while (last != NULL)
So you have resolved this question I see.
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 19:02 ` Jan Kratochvil
@ 2012-07-23 19:08 ` Tom Tromey
0 siblings, 0 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 19:08 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> On Mon, 23 Jul 2012 20:53:47 +0200, Tom Tromey wrote:
>> else if (bfd_check_format (abfd, bfd_archive))
>> {
>> - last = 0;
>> - /* FIXME??? am I tossing BFDs? bfd? */
>> - while ((last = gdb_bfd_openr_next_archived_file (abfd, last)))
>> + last = gdb_bfd_openr_next_archived_file (abfd, NULL);
>> + while (last != NULL)
Jan> So you have resolved this question I see.
Not really to my satisfaction.
I just brought it in line with machoread.c, which is more actively
maintained.
I'll try to dig up a definitive answer.
Tom
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-19 20:58 ` Tom Tromey
2012-07-20 16:37 ` Tom Tromey
@ 2012-07-20 16:37 ` Tom Tromey
2012-07-20 18:11 ` Jan Kratochvil
2012-07-22 19:00 ` Jan Kratochvil
2 siblings, 1 reply; 30+ messages in thread
From: Tom Tromey @ 2012-07-20 16:37 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:
Tom> Then I went a bit further and wrote a follow-up patch that wraps the
Tom> various BFD-opening functions with gdb equivalents. These initialize
Tom> the refcount and stash the filename.
I made this change too. It applies on top of the void-returning change.
I think this cleans things up quite a bit, but I'd appreciate your
feedback.
Tom
From b39c77a60199459f788b589adffaed2c506355fa Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey@redhat.com>
Date: Thu, 19 Jul 2012 14:47:25 -0600
Subject: [PATCH 2/3] add wrappers for all bfd opening functions
This introduces gdb-specific wrappers for various BFD-opening
functions. These wrappers ensure that the gdb reference count is
initialized and that gdb_bfd_stash_filename is called.
The one place I did not fully convert is symbol_file_add_from_memory,
because this is the only caller of bfd_elf_bfd_from_remote_memory,
and so it seemed excessive to add a wrapper.
I think this makes the code easier to follow. Comments?
* cli/cli-dump.c (bfd_openr_with_cleanup): Use gdb_bfd_openr.
(bfd_openw_with_cleanup): Use gdb_bfd_openw.
* corelow.c (core_open): Use gdb_bfd_fopen.
* dsrec.c (load_srec): Use gdb_bfd_openr.
* exec.c (exec_file_attach): Use gdb_bfd_fopen.
* gcore.c (gcore_memory_sections): Use gdb_bfd_openw.
* gdb_bfd.c (gdb_bfd_fopen, gdb_bfd_openr, gdb_bfd_openw)
(gdb_bfd_openr_iovec, gdb_bfd_openr_next_archived_file)
(gdb_bfd_fdopenr): New functions.
* gdb_bfd.h (gdb_bfd_fopen, gdb_bfd_openr, gdb_bfd_openw)
(gdb_bfd_openr_iovec, gdb_bfd_openr_next_archived_file)
(gdb_bfd_fdopenr): Declare.
* jit.c (bfd_open_from_target_memory): Use gdb_bfd_openr_iovec.
* m32-rom.c (m32r_load, m32r_upload_command): Use gdb_bfd_openr.
* machoread.c (macho_symfile_read_all_oso): Use gdb_bfd_openr,
gdb_bfd_openr_next_archived_file.
(macho_check_dsym): Use gdb_bfd_openr.
(macho_add_oso_symfile): Don't call gdb_bfd_stash_filename.
* procfs.c (insert_dbx_link_bpt_in_file): Use gdb_bfd_fdopenr.
* remote-m32r-sdi.c (m32r_load): Use gdb_bfd_openr.
* remote-mips.c (mips_load_srec, pmon_load_fast): Use
gdb_bfd_openr.
* remote.c (remote_bfd_open): Use gdb_bfd_openr_iovec.
* rs6000-nat.c (add_vmap): Use gdb_bfd_openr, gdb_bfd_fdopenr,
gdb_bfd_openr_next_archived_file.
* solib-darwin.c (darwin_solib_get_all_image_info_addr_at_init):
Use gdb_bfd_openr.
* solib-pa64.c (pa64_solib_create_inferior_hook): Use
gdb_bfd_openr.
* solib-spu.c (spu_bfd_fopen): Use gdb_bfd_openr_iovec.
* solib.c (solib_bfd_fopen): Use gdb_bfd_fopen.
* spu-linux-nat.c (spu_bfd_open): Use gdb_bfd_openr_iovec.
* symfile.c (bfd_open_maybe_remote): Use gdb_bfd_openr.
(symfile_bfd_open): Use gdb_bfd_fopen.
(generic_load): Use gdb_bfd_openr.
* windows-nat.c (windows_make_so): Use gdb_bfd_openr.
fixlet
---
gdb/cli/cli-dump.c | 6 +--
gdb/corelow.c | 9 +---
gdb/dsrec.c | 3 +-
gdb/exec.c | 9 +---
gdb/gcore.c | 4 +-
gdb/gdb_bfd.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++
gdb/gdb_bfd.h | 45 +++++++++++++++++++
gdb/jit.c | 13 +++---
gdb/m32r-rom.c | 6 +--
gdb/machoread.c | 25 +++--------
gdb/procfs.c | 3 +-
gdb/remote-m32r-sdi.c | 3 +-
gdb/remote-mips.c | 6 +--
gdb/remote.c | 14 +++---
gdb/rs6000-nat.c | 9 +---
gdb/solib-darwin.c | 3 +-
gdb/solib-pa64.c | 3 +-
gdb/solib-spu.c | 10 ++---
gdb/solib.c | 4 +-
gdb/spu-linux-nat.c | 9 ++--
gdb/symfile.c | 16 +------
gdb/windows-nat.c | 3 +-
22 files changed, 211 insertions(+), 106 deletions(-)
diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index 5998b3f..7341f00 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -112,8 +112,7 @@ bfd_openr_with_cleanup (const char *filename, const char *target)
{
bfd *ibfd;
- ibfd = bfd_openr (filename, target);
- gdb_bfd_ref (ibfd);
+ ibfd = gdb_bfd_openr (filename, target);
if (ibfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
@@ -133,8 +132,7 @@ bfd_openw_with_cleanup (const char *filename, const char *target,
if (*mode == 'w') /* Write: create new file */
{
- obfd = bfd_openw (filename, target);
- gdb_bfd_ref (obfd);
+ obfd = gdb_bfd_openw (filename, target);
if (obfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 529e3e2..340b149 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -318,15 +318,12 @@ core_open (char *filename, int from_tty)
if (scratch_chan < 0)
perror_with_name (filename);
- temp_bfd = bfd_fopen (filename, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
- gdb_bfd_ref (temp_bfd);
+ temp_bfd = gdb_bfd_fopen (filename, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan);
if (temp_bfd == NULL)
perror_with_name (filename);
- gdb_bfd_stash_filename (temp_bfd);
-
if (!bfd_check_format (temp_bfd, bfd_core)
&& !gdb_check_format (temp_bfd))
{
diff --git a/gdb/dsrec.c b/gdb/dsrec.c
index 8cca361..f39d0ed 100644
--- a/gdb/dsrec.c
+++ b/gdb/dsrec.c
@@ -61,8 +61,7 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
srec = (char *) alloca (maxrecsize + 1);
- abfd = bfd_openr (file, 0);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (file, 0);
if (!abfd)
{
printf_filtered (_("Unable to open file %s\n"), file);
diff --git a/gdb/exec.c b/gdb/exec.c
index dc9258a..9b95e02 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -233,10 +233,9 @@ exec_file_attach (char *filename, int from_tty)
if (scratch_chan < 0)
perror_with_name (filename);
- exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
- gdb_bfd_ref (exec_bfd);
+ exec_bfd = gdb_bfd_fopen (scratch_pathname, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan);
if (!exec_bfd)
{
@@ -254,8 +253,6 @@ exec_file_attach (char *filename, int from_tty)
gdb_bfd_errmsg (bfd_get_error (), matching));
}
- gdb_bfd_stash_filename (exec_bfd);
-
/* FIXME - This should only be run for RS6000, but the ifdef is a poor
way to accomplish. */
#ifdef DEPRECATED_IBM6000_TARGET
diff --git a/gdb/gcore.c b/gdb/gcore.c
index 1443289..a45e787 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -51,12 +51,10 @@ static int gcore_memory_sections (bfd *);
bfd *
create_gcore_bfd (char *filename)
{
- bfd *obfd = bfd_openw (filename, default_gcore_target ());
+ bfd *obfd = gdb_bfd_openw (filename, default_gcore_target ());
- gdb_bfd_ref (obfd);
if (!obfd)
error (_("Failed to open '%s' for output."), filename);
- gdb_bfd_stash_filename (obfd);
bfd_set_format (obfd, bfd_core);
bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
return obfd;
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index dd6eac3..095d0f3 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -481,3 +481,117 @@ gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
*size = descriptor->size;
return descriptor->data;
}
+
+\f
+
+/* See gdb_bfd.h. */
+
+bfd *
+gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
+ int fd)
+{
+ bfd *result = bfd_fopen (filename, target, mode, fd);
+
+ if (result)
+ {
+ gdb_bfd_stash_filename (result);
+ gdb_bfd_ref (result);
+ }
+
+ return result;
+}
+
+/* See gdb_bfd.h. */
+
+bfd *
+gdb_bfd_openr (const char *filename, const char *target)
+{
+ bfd *result = bfd_openr (filename, target);
+
+ if (result)
+ {
+ gdb_bfd_stash_filename (result);
+ gdb_bfd_ref (result);
+ }
+
+ return result;
+}
+
+/* See gdb_bfd.h. */
+
+bfd *
+gdb_bfd_openw (const char *filename, const char *target)
+{
+ bfd *result = bfd_openw (filename, target);
+
+ if (result)
+ {
+ gdb_bfd_stash_filename (result);
+ gdb_bfd_ref (result);
+ }
+
+ return result;
+}
+
+/* See gdb_bfd.h. */
+
+bfd *
+gdb_bfd_openr_iovec (const char *filename, const char *target,
+ void *(*open_func) (struct bfd *nbfd,
+ void *open_closure),
+ void *open_closure,
+ file_ptr (*pread_func) (struct bfd *nbfd,
+ void *stream,
+ void *buf,
+ file_ptr nbytes,
+ file_ptr offset),
+ int (*close_func) (struct bfd *nbfd,
+ void *stream),
+ int (*stat_func) (struct bfd *abfd,
+ void *stream,
+ struct stat *sb))
+{
+ bfd *result = bfd_openr_iovec (filename, target,
+ open_func, open_closure,
+ pread_func, close_func, stat_func);
+
+ if (result)
+ {
+ gdb_bfd_ref (result);
+ gdb_bfd_stash_filename (result);
+ }
+
+ return result;
+}
+
+/* See gdb_bfd.h. */
+
+bfd *
+gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
+{
+ bfd *result = bfd_openr_next_archived_file (archive, previous);
+
+ if (result)
+ {
+ gdb_bfd_ref (result);
+ /* No need to stash the filename here. */
+ }
+
+ return result;
+}
+
+/* See gdb_bfd.h. */
+
+bfd *
+gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
+{
+ bfd *result = bfd_fdopenr (filename, target, fd);
+
+ if (result)
+ {
+ gdb_bfd_ref (result);
+ gdb_bfd_stash_filename (result);
+ }
+
+ return result;
+}
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index f5b6103..f131ba7 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -58,4 +58,49 @@ void gdb_bfd_unref (struct bfd *abfd);
const gdb_byte *gdb_bfd_map_section (asection *section, bfd_size_type *size);
+\f
+
+/* A wrapper for bfd_fopen that initializes the gdb-specific reference
+ count and calls gdb_bfd_stash_filename. */
+
+bfd *gdb_bfd_fopen (const char *, const char *, const char *, int);
+
+/* A wrapper for bfd_openr that initializes the gdb-specific reference
+ count and calls gdb_bfd_stash_filename. */
+
+bfd *gdb_bfd_openr (const char *, const char *);
+
+/* A wrapper for bfd_openw that initializes the gdb-specific reference
+ count and calls gdb_bfd_stash_filename. */
+
+bfd *gdb_bfd_openw (const char *, const char *);
+
+/* A wrapper for bfd_openr_iovec that initializes the gdb-specific
+ reference count and calls gdb_bfd_stash_filename. */
+
+bfd *gdb_bfd_openr_iovec (const char *filename, const char *target,
+ void *(*open_func) (struct bfd *nbfd,
+ void *open_closure),
+ void *open_closure,
+ file_ptr (*pread_func) (struct bfd *nbfd,
+ void *stream,
+ void *buf,
+ file_ptr nbytes,
+ file_ptr offset),
+ int (*close_func) (struct bfd *nbfd,
+ void *stream),
+ int (*stat_func) (struct bfd *abfd,
+ void *stream,
+ struct stat *sb));
+
+/* A wrapper for bfd_openr_next_archived_file that initializes the
+ gdb-specific reference count and calls gdb_bfd_stash_filename. */
+
+bfd *gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous);
+
+/* A wrapper for bfd_fdopenr that initializes the gdb-specific
+ reference count and calls gdb_bfd_stash_filename. */
+
+bfd *gdb_bfd_fdopenr (const char *filename, const char *target, int fd);
+
#endif /* GDB_BFD_H */
diff --git a/gdb/jit.c b/gdb/jit.c
index 34b7777..410b94d 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -137,12 +137,12 @@ bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
buffer->base = addr;
buffer->size = size;
- return bfd_openr_iovec ("<in-memory>", target,
- mem_bfd_iovec_open,
- buffer,
- mem_bfd_iovec_pread,
- mem_bfd_iovec_close,
- mem_bfd_iovec_stat);
+ return gdb_bfd_openr_iovec ("<in-memory>", target,
+ mem_bfd_iovec_open,
+ buffer,
+ mem_bfd_iovec_pread,
+ mem_bfd_iovec_close,
+ mem_bfd_iovec_stat);
}
/* One reader that has been loaded successfully, and can potentially be used to
@@ -861,7 +861,6 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
return;
}
- gdb_bfd_ref (nbfd);
/* Check the format. NOTE: This initializes important data that GDB uses!
We would segfault later without this line. */
diff --git a/gdb/m32r-rom.c b/gdb/m32r-rom.c
index 5678f13..9a2f5aa 100644
--- a/gdb/m32r-rom.c
+++ b/gdb/m32r-rom.c
@@ -130,8 +130,7 @@ m32r_load (char *filename, int from_tty)
if (filename == NULL || filename[0] == 0)
filename = get_exec_file (1);
- abfd = bfd_openr (filename, 0);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (filename, 0);
if (!abfd)
error (_("Unable to open file %s."), filename);
cleanup = make_cleanup_bfd_unref (abfd);
@@ -530,8 +529,7 @@ m32r_upload_command (char *args, int from_tty)
printf_filtered (" -- Ethernet load complete.\n");
gettimeofday (&end_time, NULL);
- abfd = bfd_openr (args, 0);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (args, 0);
cleanup = make_cleanup_bfd_unref (abfd);
if (abfd != NULL)
{ /* Download is done -- print section statistics. */
diff --git a/gdb/machoread.c b/gdb/machoread.c
index 8ecd4c5..5b9e2ba 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -629,11 +629,6 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
bfd_hash_table_free (&table);
- /* Make sure that the filename has the correct lifetime. The
- current filename comes either from an OSO symbol name or from an
- archive name. Memory for both is not managed by gdb. */
- gdb_bfd_stash_filename (abfd);
-
/* We need to clear SYMFILE_MAINLINE to avoid interractive question
from symfile.c:symbol_file_add_with_addrs_or_offsets. */
symbol_file_add_from_bfd
@@ -689,8 +684,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
}
/* Open the archive and check the format. */
- archive_bfd = bfd_openr (archive_name, gnutarget);
- gdb_bfd_ref (archive_bfd);
+ archive_bfd = gdb_bfd_openr (archive_name, gnutarget);
if (archive_bfd == NULL)
{
warning (_("Could not open OSO archive file \"%s\""),
@@ -707,10 +701,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
continue;
}
- gdb_bfd_stash_filename (archive_bfd);
-
- member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
- gdb_bfd_ref (member_bfd);
+ member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);
if (member_bfd == NULL)
{
@@ -746,9 +737,8 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
}
prev = member_bfd;
- member_bfd = bfd_openr_next_archived_file (archive_bfd,
- member_bfd);
- gdb_bfd_ref (member_bfd);
+ member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd,
+ member_bfd);
/* Free previous member if not referenced by an oso. */
if (ix2 >= last_ix)
@@ -768,8 +758,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
{
bfd *abfd;
- abfd = bfd_openr (oso->name, gnutarget);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (oso->name, gnutarget);
if (!abfd)
warning (_("`%s': can't open to read symbols: %s."), oso->name,
bfd_errmsg (bfd_get_error ()));
@@ -819,14 +808,12 @@ macho_check_dsym (struct objfile *objfile)
warning (_("can't find UUID in %s"), objfile->name);
return NULL;
}
- dsym_bfd = bfd_openr (dsym_filename, gnutarget);
- gdb_bfd_ref (dsym_bfd);
+ dsym_bfd = gdb_bfd_openr (dsym_filename, gnutarget);
if (dsym_bfd == NULL)
{
warning (_("can't open dsym file %s"), dsym_filename);
return NULL;
}
- gdb_bfd_stash_filename (dsym_filename);
if (!bfd_check_format (dsym_bfd, bfd_object))
{
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 3df8254..4409e5b 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3486,8 +3486,7 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
long storage_needed;
CORE_ADDR sym_addr;
- abfd = bfd_fdopenr ("unamed", 0, fd);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_fdopenr ("unamed", 0, fd);
if (abfd == NULL)
{
warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/remote-m32r-sdi.c b/gdb/remote-m32r-sdi.c
index f0c7cba..85268b6 100644
--- a/gdb/remote-m32r-sdi.c
+++ b/gdb/remote-m32r-sdi.c
@@ -1258,8 +1258,7 @@ m32r_load (char *args, int from_tty)
if (!filename)
filename = get_exec_file (1);
- pbfd = bfd_openr (filename, gnutarget);
- gdb_bfd_ref (pbfd);
+ pbfd = gdb_bfd_openr (filename, gnutarget);
if (pbfd == NULL)
{
perror_with_name (filename);
diff --git a/gdb/remote-mips.c b/gdb/remote-mips.c
index 2c8b92e..db4381b 100644
--- a/gdb/remote-mips.c
+++ b/gdb/remote-mips.c
@@ -2789,8 +2789,7 @@ mips_load_srec (char *args)
buffer = alloca (srec_frame * 2 + 256);
- abfd = bfd_openr (args, 0);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (args, 0);
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", args);
@@ -3377,8 +3376,7 @@ pmon_load_fast (char *file)
buffer = (char *) xmalloc (MAXRECSIZE + 1);
binbuf = (unsigned char *) xmalloc (BINCHUNK);
- abfd = bfd_openr (file, 0);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (file, 0);
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", file);
diff --git a/gdb/remote.c b/gdb/remote.c
index 9e620df..6780212 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -9824,14 +9824,12 @@ remote_filename_p (const char *filename)
bfd *
remote_bfd_open (const char *remote_file, const char *target)
{
- bfd *abfd = bfd_openr_iovec (remote_file, target,
- remote_bfd_iovec_open, NULL,
- remote_bfd_iovec_pread,
- remote_bfd_iovec_close,
- remote_bfd_iovec_stat);
-
- if (abfd != NULL)
- gdb_bfd_stash_filename (abfd);
+ bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
+ remote_bfd_iovec_open, NULL,
+ remote_bfd_iovec_pread,
+ remote_bfd_iovec_close,
+ remote_bfd_iovec_stat);
+
return abfd;
}
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 073f167..140012b 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -748,17 +748,15 @@ add_vmap (LdInfo *ldi)
if (fd < 0)
/* Note that this opens it once for every member; a possible
enhancement would be to only open it once for every object. */
- abfd = bfd_openr (filename, gnutarget);
+ abfd = gdb_bfd_openr (filename, gnutarget);
else
- abfd = bfd_fdopenr (filename, gnutarget, fd);
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_fdopenr (filename, gnutarget, fd);
if (!abfd)
{
warning (_("Could not open `%s' as an executable file: %s"),
filename, bfd_errmsg (bfd_get_error ()));
return NULL;
}
- gdb_bfd_stash_filename (abfd);
/* Make sure we have an object file. */
@@ -769,9 +767,8 @@ add_vmap (LdInfo *ldi)
{
last = 0;
/* FIXME??? am I tossing BFDs? bfd? */
- while ((last = bfd_openr_next_archived_file (abfd, last)))
+ while ((last = gdb_bfd_openr_next_archived_file (abfd, last)))
{
- gdb_bfd_ref (last);
if (strcmp (mem, last->filename) == 0)
break;
}
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index b76a966..db8f187 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -371,8 +371,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
cleanup = make_cleanup (null_cleanup, NULL);
/* Create a bfd for the interpreter. */
- dyld_bfd = bfd_openr (interp_name, gnutarget);
- gdb_bfd_ref (dyld_bfd);
+ dyld_bfd = gdb_bfd_openr (interp_name, gnutarget);
if (dyld_bfd)
{
bfd *sub;
diff --git a/gdb/solib-pa64.c b/gdb/solib-pa64.c
index 5e1f730..00ed8a5 100644
--- a/gdb/solib-pa64.c
+++ b/gdb/solib-pa64.c
@@ -362,8 +362,7 @@ manpage for methods to privately map shared library text."));
to find any magic formula to find it for Solaris (appears to
be trivial on GNU/Linux). Therefore, we have to try an alternate
mechanism to find the dynamic linker's base address. */
- tmp_bfd = bfd_openr (buf, gnutarget);
- gdb_bfd_ref (tmp_bfd);
+ tmp_bfd = gdb_bfd_openr (buf, gnutarget);
if (tmp_bfd == NULL)
return;
diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
index c1c49e7..f62d96c 100644
--- a/gdb/solib-spu.c
+++ b/gdb/solib-spu.c
@@ -326,11 +326,10 @@ spu_bfd_fopen (char *name, CORE_ADDR addr)
CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
*open_closure = addr;
- nbfd = bfd_openr_iovec (name, "elf32-spu",
- spu_bfd_iovec_open, open_closure,
- spu_bfd_iovec_pread, spu_bfd_iovec_close,
- spu_bfd_iovec_stat);
- gdb_bfd_ref (nbfd);
+ nbfd = gdb_bfd_openr_iovec (name, "elf32-spu",
+ spu_bfd_iovec_open, open_closure,
+ spu_bfd_iovec_pread, spu_bfd_iovec_close,
+ spu_bfd_iovec_stat);
if (!nbfd)
return NULL;
@@ -340,7 +339,6 @@ spu_bfd_fopen (char *name, CORE_ADDR addr)
return NULL;
}
- gdb_bfd_stash_filename (nbfd);
return nbfd;
}
diff --git a/gdb/solib.c b/gdb/solib.c
index 649cedd..9779e10 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -377,7 +377,7 @@ solib_bfd_fopen (char *pathname, int fd)
}
else
{
- abfd = bfd_fopen (pathname, gnutarget, FOPEN_RB, fd);
+ abfd = gdb_bfd_fopen (pathname, gnutarget, FOPEN_RB, fd);
if (abfd)
bfd_set_cacheable (abfd, 1);
@@ -390,10 +390,8 @@ solib_bfd_fopen (char *pathname, int fd)
pathname, bfd_errmsg (bfd_get_error ()));
}
- gdb_bfd_stash_filename (abfd);
xfree (pathname);
- gdb_bfd_ref (abfd);
return abfd;
}
diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
index ae643e3..a17ff3f 100644
--- a/gdb/spu-linux-nat.c
+++ b/gdb/spu-linux-nat.c
@@ -315,13 +315,12 @@ spu_bfd_open (ULONGEST addr)
ULONGEST *open_closure = xmalloc (sizeof (ULONGEST));
*open_closure = addr;
- nbfd = bfd_openr_iovec ("<in-memory>", "elf32-spu",
- spu_bfd_iovec_open, open_closure,
- spu_bfd_iovec_pread, spu_bfd_iovec_close,
- spu_bfd_iovec_stat);
+ nbfd = gdb_bfd_openr_iovec ("<in-memory>", "elf32-spu",
+ spu_bfd_iovec_open, open_closure,
+ spu_bfd_iovec_pread, spu_bfd_iovec_close,
+ spu_bfd_iovec_stat);
if (!nbfd)
return NULL;
- gdb_bfd_ref (nbfd);
if (!bfd_check_format (nbfd, bfd_object))
{
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 921ed30..64c4a3b 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1702,13 +1702,8 @@ bfd_open_maybe_remote (const char *name)
if (remote_filename_p (name))
result = remote_bfd_open (name, gnutarget);
else
- {
- result = bfd_openr (name, gnutarget);
- if (result != NULL)
- gdb_bfd_stash_filename (result);
- }
+ result = gdb_bfd_openr (name, gnutarget);
- gdb_bfd_ref (result);
return result;
}
@@ -1728,7 +1723,6 @@ symfile_bfd_open (char *name)
if (remote_filename_p (name))
{
sym_bfd = remote_bfd_open (name, gnutarget);
- gdb_bfd_ref (sym_bfd);
if (!sym_bfd)
error (_("`%s': can't open to read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
@@ -1768,8 +1762,7 @@ symfile_bfd_open (char *name)
name = absolute_name;
make_cleanup (xfree, name);
- sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
- gdb_bfd_ref (sym_bfd);
+ sym_bfd = gdb_bfd_fopen (name, gnutarget, FOPEN_RB, desc);
if (!sym_bfd)
{
make_cleanup (xfree, name);
@@ -1785,8 +1778,6 @@ symfile_bfd_open (char *name)
bfd_errmsg (bfd_get_error ()));
}
- gdb_bfd_stash_filename (sym_bfd);
-
return sym_bfd;
}
@@ -2111,8 +2102,7 @@ generic_load (char *args, int from_tty)
}
/* Open the file for loading. */
- loadfile_bfd = bfd_openr (filename, gnutarget);
- gdb_bfd_ref (loadfile_bfd);
+ loadfile_bfd = gdb_bfd_openr (filename, gnutarget);
if (loadfile_bfd == NULL)
{
perror_with_name (filename);
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index b2a1d8b..afef615 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -752,8 +752,7 @@ windows_make_so (const char *name, LPVOID load_addr)
asection *text = NULL;
CORE_ADDR text_vma;
- abfd = bfd_openr (so->so_name, "pei-i386");
- gdb_bfd_ref (abfd);
+ abfd = gdb_bfd_openr (so->so_name, "pei-i386");
if (!abfd)
return so;
--
1.7.7.6
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-20 16:37 ` Tom Tromey
@ 2012-07-20 18:11 ` Jan Kratochvil
0 siblings, 0 replies; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-20 18:11 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Fri, 20 Jul 2012 18:36:35 +0200, Tom Tromey wrote:
> I think this cleans things up quite a bit, but I'd appreciate your
> feedback.
I cannot do a real review now but I like this calling convention FYI.
Even gdb_bfd_stash_filename really fits there nice.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-19 20:58 ` Tom Tromey
2012-07-20 16:37 ` Tom Tromey
2012-07-20 16:37 ` Tom Tromey
@ 2012-07-22 19:00 ` Jan Kratochvil
2012-07-23 14:49 ` Tom Tromey
2 siblings, 1 reply; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-22 19:00 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Thu, 19 Jul 2012 22:58:03 +0200, Tom Tromey wrote:
> >>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
>
> Jan> The comments are present in both and neither is a reference, they
> Jan> are already out of sync.
>
> I don't really follow,
There was some conclusion on gdb-patches to make happy both people wanting
comments in *.c and people wanting comments in *.h and to prevent duplication
of the same comment in both *.c and *.h which leads to stale invalid comments
in one of the files to put the comment only in *.h file and in the *.c file
state:
/* See gdb_bfd.h. */
You even use it at various functions in gdb_bfd.c, just not for gdb_bfd_ref
and gdb_bfd_unref.
> >> @@ -2519,14 +2512,10 @@ reread_symbols (void)
> >> to close the descriptor but BFD lacks a way of closing the
> >> BFD without closing the descriptor. */
> >> obfd_filename = bfd_get_filename (objfile->obfd);
> >> - if (!bfd_close (objfile->obfd))
> >> - error (_("Can't close BFD for %s: %s"), objfile->name,
> >> - bfd_errmsg (bfd_get_error ()));
> >> + gdb_bfd_unref (objfile->obfd);
> >> objfile-> obfd = bfd_open_maybe_remote (obfd_filename);
> >> if (objfile->obfd == NULL)
> >> error (_("Can't open %s to read symbols."), objfile->name);
> >> - else
> >> - objfile->obfd = gdb_bfd_ref (objfile->obfd);
>
> Jan> Why isn't gdb_bfd_ref missing here?
>
> bfd_open_maybe_remote returns a new reference.
Aha, OK.
> I wonder if I should rename it to gdb_bfd_open_maybe_remote, for
> consistency. What do you think? I'm inclined to do it.
Definitely.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-22 19:00 ` Jan Kratochvil
@ 2012-07-23 14:49 ` Tom Tromey
2012-07-23 15:01 ` Tom Tromey
2012-07-23 15:03 ` Tom Tromey
0 siblings, 2 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 14:49 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> There was some conclusion on gdb-patches to make happy both people
Jan> wanting comments in *.c and people wanting comments in *.h and to
Jan> prevent duplication of the same comment in both *.c and *.h which
Jan> leads to stale invalid comments in one of the files to put the
Jan> comment only in *.h file and in the *.c file state: /* See
Jan> gdb_bfd.h. */ You even use it at various functions in gdb_bfd.c,
Jan> just not for gdb_bfd_ref and gdb_bfd_unref.
I'll fix it.
Tom> I wonder if I should rename it to gdb_bfd_open_maybe_remote, for
Tom> consistency. What do you think? I'm inclined to do it.
Jan> Definitely.
I'll do this shortly.
Tom
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 14:49 ` Tom Tromey
@ 2012-07-23 15:01 ` Tom Tromey
2012-07-23 15:03 ` Tom Tromey
1 sibling, 0 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 15:01 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
Jan> There was some conclusion on gdb-patches to make happy both people
Jan> wanting comments in *.c and people wanting comments in *.h and to
Jan> prevent duplication of the same comment in both *.c and *.h which
Jan> leads to stale invalid comments in one of the files to put the
Jan> comment only in *.h file and in the *.c file state: /* See
Jan> gdb_bfd.h. */ You even use it at various functions in gdb_bfd.c,
Jan> just not for gdb_bfd_ref and gdb_bfd_unref.
Tom> I'll fix it.
Here's the patch.
Tom
2012-07-23 Tom Tromey <tromey@redhat.com>
* gdb_bfd.c (gdb_bfd_ref, gdb_bfd_unref): Fix comment.
Index: gdb_bfd.c
===================================================================
RCS file: /cvs/src/src/gdb/gdb_bfd.c,v
retrieving revision 1.6
diff -u -r1.6 gdb_bfd.c
--- gdb_bfd.c 23 Jul 2012 14:57:54 -0000 1.6
+++ gdb_bfd.c 23 Jul 2012 15:00:55 -0000
@@ -218,7 +218,7 @@
return ret;
}
-/* Add reference to ABFD. Returns ABFD. */
+/* See gdb_bfd.h. */
void
gdb_bfd_ref (struct bfd *abfd)
@@ -242,7 +242,7 @@
bfd_usrdata (abfd) = gdata;
}
-/* Unreference and possibly close ABFD. */
+/* See gdb_bfd.h. */
void
gdb_bfd_unref (struct bfd *abfd)
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-23 14:49 ` Tom Tromey
2012-07-23 15:01 ` Tom Tromey
@ 2012-07-23 15:03 ` Tom Tromey
1 sibling, 0 replies; 30+ messages in thread
From: Tom Tromey @ 2012-07-23 15:03 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:
Tom> I wonder if I should rename it to gdb_bfd_open_maybe_remote, for
Tom> consistency. What do you think? I'm inclined to do it.
Jan> Definitely.
Tom> I'll do this shortly.
Here's that patch.
I'm checking it in.
Tom
2012-07-23 Tom Tromey <tromey@redhat.com>
* symfile.c (separate_debug_file_exists): Update.
(gdb_bfd_open_maybe_remote): Rename from bfd_open_maybe_remote.
(reread_symbols): Update.
* elfread.c (build_id_verify): Update.
* symfile.h (gdb_bfd_open_maybe_remote): Rename from
bfd_open_maybe_remote.
Index: elfread.c
===================================================================
RCS file: /cvs/src/src/gdb/elfread.c,v
retrieving revision 1.136
diff -u -r1.136 elfread.c
--- elfread.c 23 Jul 2012 14:58:44 -0000 1.136
+++ elfread.c 23 Jul 2012 15:03:15 -0000
@@ -1109,7 +1109,7 @@
int retval = 0;
/* We expect to be silent on the non-existing files. */
- abfd = bfd_open_maybe_remote (filename);
+ abfd = gdb_bfd_open_maybe_remote (filename);
if (abfd == NULL)
return 0;
Index: symfile.c
===================================================================
RCS file: /cvs/src/src/gdb/symfile.c,v
retrieving revision 1.341
diff -u -r1.341 symfile.c
--- symfile.c 23 Jul 2012 14:58:44 -0000 1.341
+++ symfile.c 23 Jul 2012 15:03:15 -0000
@@ -1353,7 +1353,7 @@
if (filename_cmp (name, parent_objfile->name) == 0)
return 0;
- abfd = bfd_open_maybe_remote (name);
+ abfd = gdb_bfd_open_maybe_remote (name);
if (!abfd)
return 0;
@@ -1697,7 +1697,7 @@
returns NULL with the BFD error set. */
bfd *
-bfd_open_maybe_remote (const char *name)
+gdb_bfd_open_maybe_remote (const char *name)
{
bfd *result;
@@ -2516,7 +2516,7 @@
obfd_filename = bfd_get_filename (objfile->obfd);
/* Open the new BFD before freeing the old one, so that
the filename remains live. */
- objfile->obfd = bfd_open_maybe_remote (obfd_filename);
+ objfile->obfd = gdb_bfd_open_maybe_remote (obfd_filename);
gdb_bfd_unref (obfd);
}
Index: symfile.h
===================================================================
RCS file: /cvs/src/src/gdb/symfile.h,v
retrieving revision 1.112
diff -u -r1.112 symfile.h
--- symfile.h 20 Jul 2012 17:38:04 -0000 1.112
+++ symfile.h 23 Jul 2012 15:03:15 -0000
@@ -550,7 +550,7 @@
extern bfd *symfile_bfd_open (char *);
-extern bfd *bfd_open_maybe_remote (const char *);
+extern bfd *gdb_bfd_open_maybe_remote (const char *);
extern int get_section_index (struct objfile *, char *);
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-19 14:18 ` Jan Kratochvil
2012-07-19 20:58 ` Tom Tromey
@ 2012-07-20 16:31 ` Tom Tromey
2012-07-20 18:10 ` Jan Kratochvil
1 sibling, 1 reply; 30+ messages in thread
From: Tom Tromey @ 2012-07-20 16:31 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> I personally disagree about the returned value from gdb_bfd_ref
Jan> being useful, it makes the code more magic IMO (plus it is not much
Jan> compatible with the narrow GNU Coding Standard code formatting).
I made this change.
Let me know what you think.
Tom
From 7162991da7fd74a7ee4e1e0ac07f64f80aa4edc2 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey@redhat.com>
Date: Thu, 19 Jul 2012 14:22:44 -0600
Subject: [PATCH 1/3] change gdb_bfd_ref to return void
As discussed yesterday, this changes gdb_bfd_ref to return void.
* bfd-target.c (target_bfd_reopen): Update.
* cli/cli-dump.c (bfd_openr_with_cleanup)
(bfd_openw_with_cleanup): Update.
* corelow.c (core_open): Update.
* dsrec.c (load_srec): Update.
* exec.c (exec_file_attach): Update.
* gcore.c (create_gcore_bfd): Update.
* gdb_bfd.c (gdb_bfd_ref): Return void.
(gdb_bfd_open): Update.
* gdb_bfd.h (gdb_bfd_ref): Return void.
Update comments.
* jit.c (jit_bfd_try_read_symtab): Update.
* m32r-rom.c (m32r_load, m32r_upload_command): Update.
* machoread.c (macho_symfile_read_all_oso): Update.
(macho_check_dsym): Update.
* procfs.c (insert_dbx_link_bpt_in_file): Update.
* remote-m32r-sdi.c (m32r_load): Update.
* remote-mips.c (mips_load_srec, pmon_load_fast): Update.
* rs6000-nat.c (add_vmap): Update.
* solib-darwin.c (darwin_solib_get_all_image_info_addr_at_init):
Update.
* solib-pa64.c (pa64_solib_create_inferior_hook): Update.
* solib-spu.c (spu_bfd_open): Update.
* solib.c (solib_bfd_fopen, solib_read_symbols): Update.
* spu-linux-nat.c (spu_bfd_open): Update.
* symfile.c (bfd_open_maybe_remote, symfile_bfd_open)
(generic_load): Update.
* windows-nat.c (windows_make_so): Update.
---
gdb/bfd-target.c | 3 ++-
gdb/cli/cli-dump.c | 6 ++++--
gdb/corelow.c | 7 ++++---
gdb/dsrec.c | 3 ++-
gdb/exec.c | 7 ++++---
gdb/gcore.c | 3 ++-
gdb/gdb_bfd.c | 14 +++++++-------
gdb/gdb_bfd.h | 12 ++++++------
gdb/jit.c | 2 +-
gdb/m32r-rom.c | 6 ++++--
gdb/machoread.c | 19 +++++++++++--------
gdb/procfs.c | 3 ++-
gdb/remote-m32r-sdi.c | 3 ++-
gdb/remote-mips.c | 6 ++++--
gdb/rs6000-nat.c | 14 +++++++++-----
gdb/solib-darwin.c | 6 ++++--
gdb/solib-pa64.c | 3 ++-
gdb/solib-spu.c | 9 +++++----
gdb/solib.c | 6 ++++--
gdb/spu-linux-nat.c | 2 +-
gdb/symfile.c | 20 +++++++++++++-------
gdb/windows-nat.c | 3 ++-
22 files changed, 95 insertions(+), 62 deletions(-)
diff --git a/gdb/bfd-target.c b/gdb/bfd-target.c
index 21f53bb..455d3e6 100644
--- a/gdb/bfd-target.c
+++ b/gdb/bfd-target.c
@@ -84,7 +84,8 @@ target_bfd_reopen (struct bfd *abfd)
struct target_bfd_data *data;
data = XZALLOC (struct target_bfd_data);
- data->bfd = gdb_bfd_ref (abfd);
+ data->bfd = abfd;
+ gdb_bfd_ref (abfd);
build_section_table (abfd, &data->table.sections, &data->table.sections_end);
t = XZALLOC (struct target_ops);
diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index 1244767..5998b3f 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -112,7 +112,8 @@ bfd_openr_with_cleanup (const char *filename, const char *target)
{
bfd *ibfd;
- ibfd = gdb_bfd_ref (bfd_openr (filename, target));
+ ibfd = bfd_openr (filename, target);
+ gdb_bfd_ref (ibfd);
if (ibfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
@@ -132,7 +133,8 @@ bfd_openw_with_cleanup (const char *filename, const char *target,
if (*mode == 'w') /* Write: create new file */
{
- obfd = gdb_bfd_ref (bfd_openw (filename, target));
+ obfd = bfd_openw (filename, target);
+ gdb_bfd_ref (obfd);
if (obfd == NULL)
error (_("Failed to open %s: %s."), filename,
bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 380c0dc..529e3e2 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -318,9 +318,10 @@ core_open (char *filename, int from_tty)
if (scratch_chan < 0)
perror_with_name (filename);
- temp_bfd = gdb_bfd_ref (bfd_fopen (filename, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan));
+ temp_bfd = bfd_fopen (filename, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan);
+ gdb_bfd_ref (temp_bfd);
if (temp_bfd == NULL)
perror_with_name (filename);
diff --git a/gdb/dsrec.c b/gdb/dsrec.c
index 66d4c13..8cca361 100644
--- a/gdb/dsrec.c
+++ b/gdb/dsrec.c
@@ -61,7 +61,8 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
srec = (char *) alloca (maxrecsize + 1);
- abfd = gdb_bfd_ref (bfd_openr (file, 0));
+ abfd = bfd_openr (file, 0);
+ gdb_bfd_ref (abfd);
if (!abfd)
{
printf_filtered (_("Unable to open file %s\n"), file);
diff --git a/gdb/exec.c b/gdb/exec.c
index 540c271..dc9258a 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -233,9 +233,10 @@ exec_file_attach (char *filename, int from_tty)
if (scratch_chan < 0)
perror_with_name (filename);
- exec_bfd = gdb_bfd_ref (bfd_fopen (scratch_pathname, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan));
+ exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
+ write_files ? FOPEN_RUB : FOPEN_RB,
+ scratch_chan);
+ gdb_bfd_ref (exec_bfd);
if (!exec_bfd)
{
diff --git a/gdb/gcore.c b/gdb/gcore.c
index f9a1389..1443289 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -51,8 +51,9 @@ static int gcore_memory_sections (bfd *);
bfd *
create_gcore_bfd (char *filename)
{
- bfd *obfd = gdb_bfd_ref (bfd_openw (filename, default_gcore_target ()));
+ bfd *obfd = bfd_openw (filename, default_gcore_target ());
+ gdb_bfd_ref (obfd);
if (!obfd)
error (_("Failed to open '%s' for output."), filename);
gdb_bfd_stash_filename (obfd);
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 10489ac..dd6eac3 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -158,7 +158,8 @@ gdb_bfd_open (const char *name, const char *target, int fd)
if (abfd != NULL)
{
close (fd);
- return gdb_bfd_ref (abfd);
+ gdb_bfd_ref (abfd);
+ return abfd;
}
abfd = bfd_fopen (name, target, FOPEN_RB, fd);
@@ -170,7 +171,8 @@ gdb_bfd_open (const char *name, const char *target, int fd)
*slot = abfd;
gdb_bfd_stash_filename (abfd);
- return gdb_bfd_ref (abfd);
+ gdb_bfd_ref (abfd);
+ return abfd;
}
/* A helper function that releases any section data attached to the
@@ -218,28 +220,26 @@ gdb_bfd_close_or_warn (struct bfd *abfd)
/* Add reference to ABFD. Returns ABFD. */
-struct bfd *
+void
gdb_bfd_ref (struct bfd *abfd)
{
struct gdb_bfd_data *gdata;
if (abfd == NULL)
- return NULL;
+ return;
gdata = bfd_usrdata (abfd);
if (gdata != NULL)
{
gdata->refc += 1;
- return abfd;
+ return;
}
gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
gdata->refc = 1;
gdata->mtime = bfd_get_mtime (abfd);
bfd_usrdata (abfd) = gdata;
-
- return abfd;
}
/* Unreference and possibly close ABFD. */
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index 1dd82c3..f5b6103 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -35,14 +35,14 @@ void gdb_bfd_stash_filename (struct bfd *abfd);
struct bfd *gdb_bfd_open (const char *name, const char *target, int fd);
-/* Acquire a new reference to ABFD. Returns ABFD for convenience.
- It is fine for ABFD to be NULL; in this case the function does
- nothing and returns NULL. */
+/* Increment the reference count of ABFD. It is fine for ABFD to be
+ NULL; in this case the function does nothing. */
-struct bfd *gdb_bfd_ref (struct bfd *abfd);
+void gdb_bfd_ref (struct bfd *abfd);
-/* Release a reference to ABFD. If this is the last reference, ABFD
- will be freed. If ABFD is NULL, this function does nothing. */
+/* Decrement the reference count of ABFD. If this is the last
+ reference, ABFD will be freed. If ABFD is NULL, this function does
+ nothing. */
void gdb_bfd_unref (struct bfd *abfd);
diff --git a/gdb/jit.c b/gdb/jit.c
index 6478397..34b7777 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -861,7 +861,7 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
return;
}
- nbfd = gdb_bfd_ref (nbfd);
+ gdb_bfd_ref (nbfd);
/* Check the format. NOTE: This initializes important data that GDB uses!
We would segfault later without this line. */
diff --git a/gdb/m32r-rom.c b/gdb/m32r-rom.c
index ffda6f9..5678f13 100644
--- a/gdb/m32r-rom.c
+++ b/gdb/m32r-rom.c
@@ -130,7 +130,8 @@ m32r_load (char *filename, int from_tty)
if (filename == NULL || filename[0] == 0)
filename = get_exec_file (1);
- abfd = gdb_bfd_ref (bfd_openr (filename, 0));
+ abfd = bfd_openr (filename, 0);
+ gdb_bfd_ref (abfd);
if (!abfd)
error (_("Unable to open file %s."), filename);
cleanup = make_cleanup_bfd_unref (abfd);
@@ -529,7 +530,8 @@ m32r_upload_command (char *args, int from_tty)
printf_filtered (" -- Ethernet load complete.\n");
gettimeofday (&end_time, NULL);
- abfd = gdb_bfd_ref (bfd_openr (args, 0));
+ abfd = bfd_openr (args, 0);
+ gdb_bfd_ref (abfd);
cleanup = make_cleanup_bfd_unref (abfd);
if (abfd != NULL)
{ /* Download is done -- print section statistics. */
diff --git a/gdb/machoread.c b/gdb/machoread.c
index 6d309bb..8ecd4c5 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -689,7 +689,8 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
}
/* Open the archive and check the format. */
- archive_bfd = gdb_bfd_ref (bfd_openr (archive_name, gnutarget));
+ archive_bfd = bfd_openr (archive_name, gnutarget);
+ gdb_bfd_ref (archive_bfd);
if (archive_bfd == NULL)
{
warning (_("Could not open OSO archive file \"%s\""),
@@ -708,8 +709,8 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
gdb_bfd_stash_filename (archive_bfd);
- member_bfd = gdb_bfd_ref (bfd_openr_next_archived_file (archive_bfd,
- NULL));
+ member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
+ gdb_bfd_ref (member_bfd);
if (member_bfd == NULL)
{
@@ -745,9 +746,9 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
}
prev = member_bfd;
- member_bfd
- = gdb_bfd_ref (bfd_openr_next_archived_file (archive_bfd,
- member_bfd));
+ member_bfd = bfd_openr_next_archived_file (archive_bfd,
+ member_bfd);
+ gdb_bfd_ref (member_bfd);
/* Free previous member if not referenced by an oso. */
if (ix2 >= last_ix)
@@ -767,7 +768,8 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
{
bfd *abfd;
- abfd = gdb_bfd_ref (bfd_openr (oso->name, gnutarget));
+ abfd = bfd_openr (oso->name, gnutarget);
+ gdb_bfd_ref (abfd);
if (!abfd)
warning (_("`%s': can't open to read symbols: %s."), oso->name,
bfd_errmsg (bfd_get_error ()));
@@ -817,7 +819,8 @@ macho_check_dsym (struct objfile *objfile)
warning (_("can't find UUID in %s"), objfile->name);
return NULL;
}
- dsym_bfd = gdb_bfd_ref (bfd_openr (dsym_filename, gnutarget));
+ dsym_bfd = bfd_openr (dsym_filename, gnutarget);
+ gdb_bfd_ref (dsym_bfd);
if (dsym_bfd == NULL)
{
warning (_("can't open dsym file %s"), dsym_filename);
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 32cb468..3df8254 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3486,7 +3486,8 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
long storage_needed;
CORE_ADDR sym_addr;
- abfd = gdb_bfd_ref (bfd_fdopenr ("unamed", 0, fd));
+ abfd = bfd_fdopenr ("unamed", 0, fd);
+ gdb_bfd_ref (abfd);
if (abfd == NULL)
{
warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/remote-m32r-sdi.c b/gdb/remote-m32r-sdi.c
index b4b7324..f0c7cba 100644
--- a/gdb/remote-m32r-sdi.c
+++ b/gdb/remote-m32r-sdi.c
@@ -1258,7 +1258,8 @@ m32r_load (char *args, int from_tty)
if (!filename)
filename = get_exec_file (1);
- pbfd = gdb_bfd_ref (bfd_openr (filename, gnutarget));
+ pbfd = bfd_openr (filename, gnutarget);
+ gdb_bfd_ref (pbfd);
if (pbfd == NULL)
{
perror_with_name (filename);
diff --git a/gdb/remote-mips.c b/gdb/remote-mips.c
index b46325f..2c8b92e 100644
--- a/gdb/remote-mips.c
+++ b/gdb/remote-mips.c
@@ -2789,7 +2789,8 @@ mips_load_srec (char *args)
buffer = alloca (srec_frame * 2 + 256);
- abfd = gdb_bfd_ref (bfd_openr (args, 0));
+ abfd = bfd_openr (args, 0);
+ gdb_bfd_ref (abfd);
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", args);
@@ -3376,7 +3377,8 @@ pmon_load_fast (char *file)
buffer = (char *) xmalloc (MAXRECSIZE + 1);
binbuf = (unsigned char *) xmalloc (BINCHUNK);
- abfd = gdb_bfd_ref (bfd_openr (file, 0));
+ abfd = bfd_openr (file, 0);
+ gdb_bfd_ref (abfd);
if (!abfd)
{
printf_filtered ("Unable to open file %s\n", file);
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 017e997..073f167 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -751,7 +751,7 @@ add_vmap (LdInfo *ldi)
abfd = bfd_openr (filename, gnutarget);
else
abfd = bfd_fdopenr (filename, gnutarget, fd);
- abfd = gdb_bfd_ref (abfd);
+ gdb_bfd_ref (abfd);
if (!abfd)
{
warning (_("Could not open `%s' as an executable file: %s"),
@@ -769,9 +769,12 @@ add_vmap (LdInfo *ldi)
{
last = 0;
/* FIXME??? am I tossing BFDs? bfd? */
- while ((last = gdb_bfd_ref (bfd_openr_next_archived_file (abfd, last))))
- if (strcmp (mem, last->filename) == 0)
- break;
+ while ((last = bfd_openr_next_archived_file (abfd, last)))
+ {
+ gdb_bfd_ref (last);
+ if (strcmp (mem, last->filename) == 0)
+ break;
+ }
if (!last)
{
@@ -798,7 +801,8 @@ add_vmap (LdInfo *ldi)
gdb_bfd_unref (abfd);
return NULL;
}
- obj = allocate_objfile (gdb_bfd_ref (vp->bfd), 0);
+ gdb_bfd_ref (vp->bfd);
+ obj = allocate_objfile (vp->bfd, 0);
vp->objfile = obj;
/* Always add symbols for the main objfile. */
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 7109ead..b76a966 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -371,7 +371,8 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
cleanup = make_cleanup (null_cleanup, NULL);
/* Create a bfd for the interpreter. */
- dyld_bfd = gdb_bfd_ref (bfd_openr (interp_name, gnutarget));
+ dyld_bfd = bfd_openr (interp_name, gnutarget);
+ gdb_bfd_ref (dyld_bfd);
if (dyld_bfd)
{
bfd *sub;
@@ -381,7 +382,8 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
gdbarch_bfd_arch_info (target_gdbarch));
if (sub)
{
- dyld_bfd = gdb_bfd_ref (sub);
+ dyld_bfd = sub;
+ gdb_bfd_ref (sub);
make_cleanup_bfd_unref (sub);
}
else
diff --git a/gdb/solib-pa64.c b/gdb/solib-pa64.c
index b55b67c..5e1f730 100644
--- a/gdb/solib-pa64.c
+++ b/gdb/solib-pa64.c
@@ -362,7 +362,8 @@ manpage for methods to privately map shared library text."));
to find any magic formula to find it for Solaris (appears to
be trivial on GNU/Linux). Therefore, we have to try an alternate
mechanism to find the dynamic linker's base address. */
- tmp_bfd = gdb_bfd_ref (bfd_openr (buf, gnutarget));
+ tmp_bfd = bfd_openr (buf, gnutarget);
+ gdb_bfd_ref (tmp_bfd);
if (tmp_bfd == NULL)
return;
diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
index 5eeae62..c1c49e7 100644
--- a/gdb/solib-spu.c
+++ b/gdb/solib-spu.c
@@ -326,10 +326,11 @@ spu_bfd_fopen (char *name, CORE_ADDR addr)
CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
*open_closure = addr;
- nbfd = gdb_bfd_ref (bfd_openr_iovec (name, "elf32-spu",
- spu_bfd_iovec_open, open_closure,
- spu_bfd_iovec_pread, spu_bfd_iovec_close,
- spu_bfd_iovec_stat));
+ nbfd = bfd_openr_iovec (name, "elf32-spu",
+ spu_bfd_iovec_open, open_closure,
+ spu_bfd_iovec_pread, spu_bfd_iovec_close,
+ spu_bfd_iovec_stat);
+ gdb_bfd_ref (nbfd);
if (!nbfd)
return NULL;
diff --git a/gdb/solib.c b/gdb/solib.c
index 0fd955d..649cedd 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -393,7 +393,8 @@ solib_bfd_fopen (char *pathname, int fd)
gdb_bfd_stash_filename (abfd);
xfree (pathname);
- return gdb_bfd_ref (abfd);
+ gdb_bfd_ref (abfd);
+ return abfd;
}
/* Find shared library PATHNAME and open a BFD for it. */
@@ -611,7 +612,8 @@ solib_read_symbols (struct so_list *so, int flags)
sap = build_section_addr_info_from_section_table (so->sections,
so->sections_end);
- so->objfile = symbol_file_add_from_bfd (gdb_bfd_ref (so->abfd),
+ gdb_bfd_ref (so->abfd);
+ so->objfile = symbol_file_add_from_bfd (so->abfd,
flags, sap, OBJF_SHARED,
NULL);
so->objfile->addr_low = so->addr_low;
diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
index 12f8211..ae643e3 100644
--- a/gdb/spu-linux-nat.c
+++ b/gdb/spu-linux-nat.c
@@ -321,7 +321,7 @@ spu_bfd_open (ULONGEST addr)
spu_bfd_iovec_stat);
if (!nbfd)
return NULL;
- nbfd = gdb_bfd_ref (nbfd);
+ gdb_bfd_ref (nbfd);
if (!bfd_check_format (nbfd, bfd_object))
{
diff --git a/gdb/symfile.c b/gdb/symfile.c
index c904011..921ed30 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1697,16 +1697,19 @@ set_initial_language (void)
bfd *
bfd_open_maybe_remote (const char *name)
{
+ bfd *result;
+
if (remote_filename_p (name))
- return gdb_bfd_ref (remote_bfd_open (name, gnutarget));
+ result = remote_bfd_open (name, gnutarget);
else
{
- bfd *result = gdb_bfd_ref (bfd_openr (name, gnutarget));
-
+ result = bfd_openr (name, gnutarget);
if (result != NULL)
gdb_bfd_stash_filename (result);
- return result;
}
+
+ gdb_bfd_ref (result);
+ return result;
}
@@ -1724,7 +1727,8 @@ symfile_bfd_open (char *name)
if (remote_filename_p (name))
{
- sym_bfd = gdb_bfd_ref (remote_bfd_open (name, gnutarget));
+ sym_bfd = remote_bfd_open (name, gnutarget);
+ gdb_bfd_ref (sym_bfd);
if (!sym_bfd)
error (_("`%s': can't open to read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
@@ -1764,7 +1768,8 @@ symfile_bfd_open (char *name)
name = absolute_name;
make_cleanup (xfree, name);
- sym_bfd = gdb_bfd_ref (bfd_fopen (name, gnutarget, FOPEN_RB, desc));
+ sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
+ gdb_bfd_ref (sym_bfd);
if (!sym_bfd)
{
make_cleanup (xfree, name);
@@ -2106,7 +2111,8 @@ generic_load (char *args, int from_tty)
}
/* Open the file for loading. */
- loadfile_bfd = gdb_bfd_ref (bfd_openr (filename, gnutarget));
+ loadfile_bfd = bfd_openr (filename, gnutarget);
+ gdb_bfd_ref (loadfile_bfd);
if (loadfile_bfd == NULL)
{
perror_with_name (filename);
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 6ba92d9..b2a1d8b 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -752,7 +752,8 @@ windows_make_so (const char *name, LPVOID load_addr)
asection *text = NULL;
CORE_ADDR text_vma;
- abfd = gdb_bfd_ref (bfd_openr (so->so_name, "pei-i386"));
+ abfd = bfd_openr (so->so_name, "pei-i386");
+ gdb_bfd_ref (abfd);
if (!abfd)
return so;
--
1.7.7.6
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH 01/10] change gdb to refcount bfd everywhere
2012-07-20 16:31 ` Tom Tromey
@ 2012-07-20 18:10 ` Jan Kratochvil
0 siblings, 0 replies; 30+ messages in thread
From: Jan Kratochvil @ 2012-07-20 18:10 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Fri, 20 Jul 2012 18:31:19 +0200, Tom Tromey wrote:
> >>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
>
> Jan> I personally disagree about the returned value from gdb_bfd_ref
> Jan> being useful, it makes the code more magic IMO (plus it is not much
> Jan> compatible with the narrow GNU Coding Standard code formatting).
>
> I made this change.
> Let me know what you think.
I cannot do a real review now but I like this calling convention FYI.
Thanks,
Jan
^ permalink raw reply [flat|nested] 30+ messages in thread
end of thread, other threads:[~2012-07-25 15:44 UTC | newest]
Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-18 19:32 [PATCH 01/10] change gdb to refcount bfd everywhere Tom Tromey
2012-07-19 14:18 ` Jan Kratochvil
2012-07-19 20:58 ` Tom Tromey
2012-07-20 16:37 ` Tom Tromey
2012-07-23 8:54 ` Jan Kratochvil
2012-07-23 9:06 ` Jan Kratochvil
2012-07-23 15:23 ` Tom Tromey
2012-07-23 15:25 ` Tom Tromey
2012-07-24 13:17 ` Jan Kratochvil
2012-07-24 19:52 ` Tom Tromey
2012-07-24 20:27 ` Eli Zaretskii
2012-07-25 14:25 ` Tom Tromey
2012-07-25 14:29 ` Jan Kratochvil
2012-07-25 14:52 ` Tom Tromey
2012-07-25 14:55 ` Jan Kratochvil
2012-07-25 15:44 ` Tom Tromey
2012-07-25 15:37 ` Eli Zaretskii
2012-07-25 11:57 ` Jan Kratochvil
2012-07-23 14:52 ` Tom Tromey
2012-07-23 18:54 ` Tom Tromey
2012-07-23 19:02 ` Jan Kratochvil
2012-07-23 19:08 ` Tom Tromey
2012-07-20 16:37 ` Tom Tromey
2012-07-20 18:11 ` Jan Kratochvil
2012-07-22 19:00 ` Jan Kratochvil
2012-07-23 14:49 ` Tom Tromey
2012-07-23 15:01 ` Tom Tromey
2012-07-23 15:03 ` Tom Tromey
2012-07-20 16:31 ` Tom Tromey
2012-07-20 18:10 ` Jan Kratochvil
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox