This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
centralize "cannot close foo bfd" warnings
- From: Pedro Alves <pedro at codesourcery dot com>
- To: gdb-patches at sourceware dot org
- Date: Wed, 14 Apr 2010 18:33:09 +0100
- Subject: centralize "cannot close foo bfd" warnings
I was looking a patch I posted a couple of days ago,
<http://sourceware.org/ml/gdb-patches/2010-04/msg00343.html>
... and noticing I had done something rather ugly,
+ /* ref/unref so that we don't have to inline the warning
+ within gdb_bfd_unref. */
+ gdb_bfd_ref (abfd);
+ gdb_bfd_unref (abfd);
That's an ugly contortion to avoid writting yet another:
warning (_("cannot close \"%s\": %s"), ...
warning.
I decided to centralize the warning instead, like in the patch below.
Applied.
--
Pedro Alves
2010-04-14 Pedro Alves <pedro@codesourcery.com>
gdb/
* objfiles.h (gdb_bfd_close_or_warn): Declare.
* objfiles.c (gdb_bfd_close_or_warn): New.
* corelow.c: Include objfiles.h
(core_close): Use gdb_bfd_close_or_warn.
* elfread.c (build_id_verify): Ditto.
* exec.c (exec_close, exec_close_1): Ditto.
---
gdb/corelow.c | 5 ++---
gdb/elfread.c | 4 +---
gdb/exec.c | 8 ++------
gdb/objfiles.c | 21 ++++++++++++++++++---
gdb/objfiles.h | 1 +
5 files changed, 24 insertions(+), 15 deletions(-)
Index: src/gdb/corelow.c
===================================================================
--- src.orig/gdb/corelow.c 2010-04-14 15:16:15.000000000 +0100
+++ src/gdb/corelow.c 2010-04-14 17:50:17.000000000 +0100
@@ -46,6 +46,7 @@
#include "solib.h"
#include "filenames.h"
#include "progspace.h"
+#include "objfiles.h"
#ifndef O_LARGEFILE
@@ -221,9 +222,7 @@ core_close (int quitting)
core_has_fake_pid = 0;
name = bfd_get_filename (core_bfd);
- if (!bfd_close (core_bfd))
- warning (_("cannot close \"%s\": %s"),
- name, bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_close_or_warn (core_bfd);
xfree (name);
core_bfd = NULL;
}
Index: src/gdb/elfread.c
===================================================================
--- src.orig/gdb/elfread.c 2010-04-14 15:16:15.000000000 +0100
+++ src/gdb/elfread.c 2010-04-14 15:29:24.000000000 +0100
@@ -618,9 +618,7 @@ build_id_verify (const char *filename, s
else
retval = 1;
- if (!bfd_close (abfd))
- warning (_("cannot close \"%s\": %s"), filename,
- bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_close_or_warn (abfd);
xfree (found);
Index: src/gdb/exec.c
===================================================================
--- src.orig/gdb/exec.c 2010-04-14 15:16:15.000000000 +0100
+++ src/gdb/exec.c 2010-04-14 15:30:16.000000000 +0100
@@ -101,9 +101,7 @@ exec_close (void)
bfd *abfd = exec_bfd;
char *name = bfd_get_filename (abfd);
- if (!bfd_close (abfd))
- warning (_("cannot close \"%s\": %s"),
- name, bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_close_or_warn (abfd);
xfree (name);
/* Removing target sections may close the exec_ops target.
@@ -141,9 +139,7 @@ exec_close_1 (int quitting)
}
else if (vp->bfd != exec_bfd)
/* FIXME-leak: We should be freeing vp->name too, I think. */
- if (!bfd_close (vp->bfd))
- warning (_("cannot close \"%s\": %s"),
- vp->name, bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_close_or_warn (vp->bfd);
xfree (vp);
}
Index: src/gdb/objfiles.c
===================================================================
--- src.orig/gdb/objfiles.c 2010-04-14 15:16:15.000000000 +0100
+++ src/gdb/objfiles.c 2010-04-14 15:28:19.000000000 +0100
@@ -1471,6 +1471,23 @@ 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)
@@ -1519,9 +1536,7 @@ gdb_bfd_unref (struct bfd *abfd)
bfd_usrdata (abfd) = NULL; /* Paranoia. */
name = bfd_get_filename (abfd);
- if (!bfd_close (abfd))
- warning (_("cannot close \"%s\": %s"),
- name, bfd_errmsg (bfd_get_error ()));
+ gdb_bfd_close_or_warn (abfd);
xfree (name);
}
Index: src/gdb/objfiles.h
===================================================================
--- src.orig/gdb/objfiles.h 2010-04-14 15:16:19.000000000 +0100
+++ src/gdb/objfiles.h 2010-04-14 15:29:04.000000000 +0100
@@ -533,6 +533,7 @@ extern void *objfile_data (struct objfil
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);
/* Traverse all object files in the current program space.