kernel - cleanup vfs_cache debugging
[dragonfly.git] / contrib / gdb-7 / gdb / gdb_bfd.c
blobd4540b9e45ebfbf8493b5e8026c7b82af0dcdac1
1 /* Definitions for BFD wrappers used by GDB.
3 Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "gdb_bfd.h"
22 #include "gdb_assert.h"
23 #include "gdb_string.h"
24 #include "ui-out.h"
25 #include "gdbcmd.h"
26 #include "hashtab.h"
27 #ifdef HAVE_ZLIB_H
28 #include <zlib.h>
29 #endif
30 #ifdef HAVE_MMAP
31 #include <sys/mman.h>
32 #ifndef MAP_FAILED
33 #define MAP_FAILED ((void *) -1)
34 #endif
35 #endif
37 /* An object of this type is stored in the section's user data when
38 mapping a section. */
40 struct gdb_bfd_section_data
42 /* Size of the data. */
43 bfd_size_type size;
44 /* If the data was mmapped, this is the length of the map. */
45 bfd_size_type map_len;
46 /* The data. If NULL, the section data has not been read. */
47 void *data;
48 /* If the data was mmapped, this is the map address. */
49 void *map_addr;
52 /* A hash table holding every BFD that gdb knows about. This is not
53 to be confused with 'gdb_bfd_cache', which is used for sharing
54 BFDs; in contrast, this hash is used just to implement
55 "maint info bfd". */
57 static htab_t all_bfds;
59 /* See gdb_bfd.h. */
61 void
62 gdb_bfd_stash_filename (struct bfd *abfd)
64 char *name = bfd_get_filename (abfd);
65 char *data;
67 data = bfd_alloc (abfd, strlen (name) + 1);
68 strcpy (data, name);
70 /* Unwarranted chumminess with BFD. */
71 abfd->filename = data;
74 /* An object of this type is stored in each BFD's user data. */
76 struct gdb_bfd_data
78 /* The reference count. */
79 int refc;
81 /* The mtime of the BFD at the point the cache entry was made. */
82 time_t mtime;
84 /* If the BFD comes from an archive, this points to the archive's
85 BFD. Otherwise, this is NULL. */
86 bfd *archive_bfd;
88 /* The registry. */
89 REGISTRY_FIELDS;
92 #define GDB_BFD_DATA_ACCESSOR(ABFD) \
93 ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
95 DEFINE_REGISTRY (bfd, GDB_BFD_DATA_ACCESSOR)
97 /* A hash table storing all the BFDs maintained in the cache. */
99 static htab_t gdb_bfd_cache;
101 /* The type of an object being looked up in gdb_bfd_cache. We use
102 htab's capability of storing one kind of object (BFD in this case)
103 and using a different sort of object for searching. */
105 struct gdb_bfd_cache_search
107 /* The filename. */
108 const char *filename;
109 /* The mtime. */
110 time_t mtime;
113 /* A hash function for BFDs. */
115 static hashval_t
116 hash_bfd (const void *b)
118 const bfd *abfd = b;
120 /* It is simplest to just hash the filename. */
121 return htab_hash_string (bfd_get_filename (abfd));
124 /* An equality function for BFDs. Note that this expects the caller
125 to search using struct gdb_bfd_cache_search only, not BFDs. */
127 static int
128 eq_bfd (const void *a, const void *b)
130 const bfd *abfd = a;
131 const struct gdb_bfd_cache_search *s = b;
132 struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
134 return (gdata->mtime == s->mtime
135 && strcmp (bfd_get_filename (abfd), s->filename) == 0);
138 /* See gdb_bfd.h. */
140 struct bfd *
141 gdb_bfd_open (const char *name, const char *target, int fd)
143 hashval_t hash;
144 void **slot;
145 bfd *abfd;
146 struct gdb_bfd_cache_search search;
147 struct stat st;
149 if (gdb_bfd_cache == NULL)
150 gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
151 xcalloc, xfree);
153 if (fd == -1)
155 fd = open (name, O_RDONLY | O_BINARY);
156 if (fd == -1)
158 bfd_set_error (bfd_error_system_call);
159 return NULL;
163 search.filename = name;
164 if (fstat (fd, &st) < 0)
166 /* Weird situation here. */
167 search.mtime = 0;
169 else
170 search.mtime = st.st_mtime;
172 /* Note that this must compute the same result as hash_bfd. */
173 hash = htab_hash_string (name);
174 /* Note that we cannot use htab_find_slot_with_hash here, because
175 opening the BFD may fail; and this would violate hashtab
176 invariants. */
177 abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
178 if (abfd != NULL)
180 close (fd);
181 gdb_bfd_ref (abfd);
182 return abfd;
185 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
186 if (abfd == NULL)
187 return NULL;
189 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
190 gdb_assert (!*slot);
191 *slot = abfd;
193 gdb_bfd_stash_filename (abfd);
194 gdb_bfd_ref (abfd);
195 return abfd;
198 /* A helper function that releases any section data attached to the
199 BFD. */
201 static void
202 free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore)
204 struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp);
206 if (sect != NULL && sect->data != NULL)
208 #ifdef HAVE_MMAP
209 if (sect->map_addr != NULL)
211 int res;
213 res = munmap (sect->map_addr, sect->map_len);
214 gdb_assert (res == 0);
216 else
217 #endif
218 xfree (sect->data);
222 /* Close ABFD, and warn if that fails. */
224 static int
225 gdb_bfd_close_or_warn (struct bfd *abfd)
227 int ret;
228 char *name = bfd_get_filename (abfd);
230 bfd_map_over_sections (abfd, free_one_bfd_section, NULL);
232 ret = bfd_close (abfd);
234 if (!ret)
235 warning (_("cannot close \"%s\": %s"),
236 name, bfd_errmsg (bfd_get_error ()));
238 return ret;
241 /* See gdb_bfd.h. */
243 void
244 gdb_bfd_ref (struct bfd *abfd)
246 struct gdb_bfd_data *gdata;
247 void **slot;
249 if (abfd == NULL)
250 return;
252 gdata = bfd_usrdata (abfd);
254 if (gdata != NULL)
256 gdata->refc += 1;
257 return;
260 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
261 abfd->flags |= BFD_DECOMPRESS;
263 gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
264 gdata->refc = 1;
265 gdata->mtime = bfd_get_mtime (abfd);
266 gdata->archive_bfd = NULL;
267 bfd_usrdata (abfd) = gdata;
269 bfd_alloc_data (abfd);
271 /* This is the first we've seen it, so add it to the hash table. */
272 slot = htab_find_slot (all_bfds, abfd, INSERT);
273 gdb_assert (slot && !*slot);
274 *slot = abfd;
277 /* See gdb_bfd.h. */
279 void
280 gdb_bfd_unref (struct bfd *abfd)
282 struct gdb_bfd_data *gdata;
283 struct gdb_bfd_cache_search search;
284 bfd *archive_bfd;
286 if (abfd == NULL)
287 return;
289 gdata = bfd_usrdata (abfd);
290 gdb_assert (gdata->refc >= 1);
292 gdata->refc -= 1;
293 if (gdata->refc > 0)
294 return;
296 archive_bfd = gdata->archive_bfd;
297 search.filename = bfd_get_filename (abfd);
299 if (gdb_bfd_cache && search.filename)
301 hashval_t hash = htab_hash_string (search.filename);
302 void **slot;
304 search.mtime = gdata->mtime;
305 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
306 NO_INSERT);
308 if (slot && *slot)
309 htab_clear_slot (gdb_bfd_cache, slot);
312 bfd_free_data (abfd);
313 bfd_usrdata (abfd) = NULL; /* Paranoia. */
315 htab_remove_elt (all_bfds, abfd);
317 gdb_bfd_close_or_warn (abfd);
319 gdb_bfd_unref (archive_bfd);
322 /* A helper function that returns the section data descriptor
323 associated with SECTION. If no such descriptor exists, a new one
324 is allocated and cleared. */
326 static struct gdb_bfd_section_data *
327 get_section_descriptor (asection *section)
329 struct gdb_bfd_section_data *result;
331 result = bfd_get_section_userdata (section->owner, section);
333 if (result == NULL)
335 result = bfd_zalloc (section->owner, sizeof (*result));
336 bfd_set_section_userdata (section->owner, section, result);
339 return result;
342 /* See gdb_bfd.h. */
344 const gdb_byte *
345 gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
347 bfd *abfd;
348 struct gdb_bfd_section_data *descriptor;
349 bfd_byte *data;
351 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
352 gdb_assert (size != NULL);
354 abfd = sectp->owner;
356 descriptor = get_section_descriptor (sectp);
358 /* If the data was already read for this BFD, just reuse it. */
359 if (descriptor->data != NULL)
360 goto done;
362 #ifdef HAVE_MMAP
363 if (!bfd_is_section_compressed (abfd, sectp))
365 /* The page size, used when mmapping. */
366 static int pagesize;
368 if (pagesize == 0)
369 pagesize = getpagesize ();
371 /* Only try to mmap sections which are large enough: we don't want
372 to waste space due to fragmentation. */
374 if (bfd_get_section_size (sectp) > 4 * pagesize)
376 descriptor->size = bfd_get_section_size (sectp);
377 descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
378 MAP_PRIVATE, sectp->filepos,
379 &descriptor->map_addr,
380 &descriptor->map_len);
382 if ((caddr_t)descriptor->data != MAP_FAILED)
384 #if HAVE_POSIX_MADVISE
385 posix_madvise (descriptor->map_addr, descriptor->map_len,
386 POSIX_MADV_WILLNEED);
387 #endif
388 goto done;
391 /* On failure, clear out the section data and try again. */
392 memset (descriptor, 0, sizeof (*descriptor));
395 #endif /* HAVE_MMAP */
397 /* Handle compressed sections, or ordinary uncompressed sections in
398 the no-mmap case. */
400 descriptor->size = bfd_get_section_size (sectp);
401 descriptor->data = NULL;
403 data = NULL;
404 if (!bfd_get_full_section_contents (abfd, sectp, &data))
405 error (_("Can't read data for section '%s' in file '%s'"),
406 bfd_get_section_name (abfd, sectp),
407 bfd_get_filename (abfd));
408 descriptor->data = data;
410 done:
411 gdb_assert (descriptor->data != NULL);
412 *size = descriptor->size;
413 return descriptor->data;
418 /* See gdb_bfd.h. */
420 bfd *
421 gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
422 int fd)
424 bfd *result = bfd_fopen (filename, target, mode, fd);
426 if (result)
428 gdb_bfd_stash_filename (result);
429 gdb_bfd_ref (result);
432 return result;
435 /* See gdb_bfd.h. */
437 bfd *
438 gdb_bfd_openr (const char *filename, const char *target)
440 bfd *result = bfd_openr (filename, target);
442 if (result)
444 gdb_bfd_stash_filename (result);
445 gdb_bfd_ref (result);
448 return result;
451 /* See gdb_bfd.h. */
453 bfd *
454 gdb_bfd_openw (const char *filename, const char *target)
456 bfd *result = bfd_openw (filename, target);
458 if (result)
460 gdb_bfd_stash_filename (result);
461 gdb_bfd_ref (result);
464 return result;
467 /* See gdb_bfd.h. */
469 bfd *
470 gdb_bfd_openr_iovec (const char *filename, const char *target,
471 void *(*open_func) (struct bfd *nbfd,
472 void *open_closure),
473 void *open_closure,
474 file_ptr (*pread_func) (struct bfd *nbfd,
475 void *stream,
476 void *buf,
477 file_ptr nbytes,
478 file_ptr offset),
479 int (*close_func) (struct bfd *nbfd,
480 void *stream),
481 int (*stat_func) (struct bfd *abfd,
482 void *stream,
483 struct stat *sb))
485 bfd *result = bfd_openr_iovec (filename, target,
486 open_func, open_closure,
487 pread_func, close_func, stat_func);
489 if (result)
491 gdb_bfd_ref (result);
492 gdb_bfd_stash_filename (result);
495 return result;
498 /* See gdb_bfd.h. */
500 void
501 gdb_bfd_mark_parent (bfd *child, bfd *parent)
503 struct gdb_bfd_data *gdata;
505 gdb_bfd_ref (child);
506 /* No need to stash the filename here, because we also keep a
507 reference on the parent archive. */
509 gdata = bfd_usrdata (child);
510 if (gdata->archive_bfd == NULL)
512 gdata->archive_bfd = parent;
513 gdb_bfd_ref (parent);
515 else
516 gdb_assert (gdata->archive_bfd == parent);
519 /* See gdb_bfd.h. */
521 bfd *
522 gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
524 bfd *result = bfd_openr_next_archived_file (archive, previous);
526 if (result)
527 gdb_bfd_mark_parent (result, archive);
529 return result;
532 /* See gdb_bfd.h. */
534 bfd *
535 gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
537 bfd *result = bfd_fdopenr (filename, target, fd);
539 if (result)
541 gdb_bfd_ref (result);
542 gdb_bfd_stash_filename (result);
545 return result;
550 /* A callback for htab_traverse that prints a single BFD. */
552 static int
553 print_one_bfd (void **slot, void *data)
555 bfd *abfd = *slot;
556 struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
557 struct ui_out *uiout = data;
558 struct cleanup *inner;
560 inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
561 ui_out_field_int (uiout, "refcount", gdata->refc);
562 ui_out_field_string (uiout, "addr", host_address_to_string (abfd));
563 ui_out_field_string (uiout, "filename", bfd_get_filename (abfd));
564 ui_out_text (uiout, "\n");
565 do_cleanups (inner);
567 return 1;
570 /* Implement the 'maint info bfd' command. */
572 static void
573 maintenance_info_bfds (char *arg, int from_tty)
575 struct cleanup *cleanup;
576 struct ui_out *uiout = current_uiout;
578 cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds");
579 ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
580 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");
581 ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");
583 ui_out_table_body (uiout);
584 htab_traverse (all_bfds, print_one_bfd, uiout);
586 do_cleanups (cleanup);
589 /* -Wmissing-prototypes */
590 extern initialize_file_ftype _initialize_gdb_bfd;
592 void
593 _initialize_gdb_bfd (void)
595 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
596 NULL, xcalloc, xfree);
598 add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
599 List the BFDs that are currently open."),
600 &maintenanceinfolist);