This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[patch 2/3] Use gdb_realpath in gdb_bfd_open
- From: Jan Kratochvil <jan dot kratochvil at redhat dot com>
- To: gdb-patches at sourceware dot org
- Cc: Doug Evans <dje at google dot com>
- Date: Wed, 28 Aug 2013 18:05:47 +0200
- Subject: [patch 2/3] Use gdb_realpath in gdb_bfd_open
- Authentication-results: sourceware.org; auth=none
Hi,
it seems logical to always call gdb_realpath() in gdb_bfd_open(), IIRC also
suggested by Doug. Currently all the callers already pass in
gdb_realpath()-canonicalized filenames but [patch 3/3] will change that.
This patch has no user visible effect (except for lower GDB performance).
Jan
gdb/
2013-08-28 Jan Kratochvil <jan.kratochvil@redhat.com>
Use gdb_realpath in gdb_bfd_open.
* gdb_bfd.c (struct gdb_bfd_data): Add field canonical_filename.
(struct gdb_bfd_cache_search): Rename field filename to
canonical_filename.
(hash_bfd): New variable gdata. Use gdata->canonical_filename.
(eq_bfd): Use gdata->canonical_filename and
gdb_bfd_cache_search::canonical_filename.
(gdb_bfd_open): New variables canonical_name, canonical_name_copy and
gdata. Initialize canonical_name with gdb_realpath and use it.
Call xfree for it when needed. Store it also to
gdata->canonical_filename.
(gdb_bfd_ref): Set gdata->canonical_filename.
(gdb_bfd_unref): Use gdata->canonical_filename. Rename the use of
struct gdb_bfd_cache_search.
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -92,6 +92,11 @@ struct gdb_bfd_data
BFD. Otherwise, this is NULL. */
bfd *archive_bfd;
+ /* Filename used for lookups in gdb_bfd_cache.
+ It is only optionally canonical (by gdb_realpath), it may be also
+ just bfd_get_filename (abfd). */
+ const char *canonical_filename;
+
/* The registry. */
REGISTRY_FIELDS;
};
@@ -112,7 +117,7 @@ static htab_t gdb_bfd_cache;
struct gdb_bfd_cache_search
{
/* The filename. */
- const char *filename;
+ const char *canonical_filename;
/* The mtime. */
time_t mtime;
};
@@ -123,9 +128,10 @@ static hashval_t
hash_bfd (const void *b)
{
const bfd *abfd = b;
+ struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
/* It is simplest to just hash the filename. */
- return htab_hash_string (bfd_get_filename (abfd));
+ return htab_hash_string (gdata->canonical_filename);
}
/* An equality function for BFDs. Note that this expects the caller
@@ -139,7 +145,7 @@ eq_bfd (const void *a, const void *b)
struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
return (gdata->mtime == s->mtime
- && strcmp (bfd_get_filename (abfd), s->filename) == 0);
+ && strcmp (gdata->canonical_filename, s->canonical_filename) == 0);
}
/* See gdb_bfd.h. */
@@ -152,6 +158,8 @@ gdb_bfd_open (const char *name, const char *target, int fd)
bfd *abfd;
struct gdb_bfd_cache_search search;
struct stat st;
+ char *canonical_name, *canonical_name_copy;
+ struct gdb_bfd_data *gdata;
if (gdb_bfd_cache == NULL)
gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
@@ -167,7 +175,9 @@ gdb_bfd_open (const char *name, const char *target, int fd)
}
}
- search.filename = name;
+ canonical_name = gdb_realpath (name);
+
+ search.canonical_filename = canonical_name;
if (fstat (fd, &st) < 0)
{
/* Weird situation here. */
@@ -177,13 +187,14 @@ gdb_bfd_open (const char *name, const char *target, int fd)
search.mtime = st.st_mtime;
/* Note that this must compute the same result as hash_bfd. */
- hash = htab_hash_string (name);
+ hash = htab_hash_string (search.canonical_filename);
/* Note that we cannot use htab_find_slot_with_hash here, because
opening the BFD may fail; and this would violate hashtab
invariants. */
abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
if (abfd != NULL)
{
+ xfree (canonical_name);
close (fd);
gdb_bfd_ref (abfd);
return abfd;
@@ -191,7 +202,10 @@ gdb_bfd_open (const char *name, const char *target, int fd)
abfd = bfd_fopen (name, target, FOPEN_RB, fd);
if (abfd == NULL)
- return NULL;
+ {
+ xfree (canonical_name);
+ return NULL;
+ }
slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
gdb_assert (!*slot);
@@ -199,6 +213,14 @@ gdb_bfd_open (const char *name, const char *target, int fd)
gdb_bfd_stash_filename (abfd);
gdb_bfd_ref (abfd);
+
+ /* Store CANONICAL_NAME into gdata->canonical_filename. */
+ gdata = bfd_usrdata (abfd);
+ canonical_name_copy = bfd_alloc (abfd, strlen (canonical_name) + 1);
+ strcpy (canonical_name_copy, canonical_name);
+ xfree (canonical_name);
+ gdata->canonical_filename = canonical_name_copy;
+
return abfd;
}
@@ -271,6 +293,7 @@ gdb_bfd_ref (struct bfd *abfd)
gdata->refc = 1;
gdata->mtime = bfd_get_mtime (abfd);
gdata->archive_bfd = NULL;
+ gdata->canonical_filename = bfd_get_filename (abfd);
bfd_usrdata (abfd) = gdata;
bfd_alloc_data (abfd);
@@ -301,11 +324,11 @@ gdb_bfd_unref (struct bfd *abfd)
return;
archive_bfd = gdata->archive_bfd;
- search.filename = bfd_get_filename (abfd);
+ search.canonical_filename = gdata->canonical_filename;
- if (gdb_bfd_cache && search.filename)
+ if (gdb_bfd_cache && search.canonical_filename)
{
- hashval_t hash = htab_hash_string (search.filename);
+ hashval_t hash = htab_hash_string (search.canonical_filename);
void **slot;
search.mtime = gdata->mtime;