GDB (xrefs)
/tmp/gdb-8.1/gdb/build-id.c
Go to the documentation of this file.
1 /* build-id-related functions.
2 
3  Copyright (C) 1991-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
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.
11 
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.
16 
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/>. */
19 
20 #include "defs.h"
21 #include "bfd.h"
22 #include "gdb_bfd.h"
23 #include "build-id.h"
24 #include "gdb_vecs.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "filenames.h"
28 #include "gdbcore.h"
29 
30 /* See build-id.h. */
31 
32 const struct bfd_build_id *
33 build_id_bfd_get (bfd *abfd)
34 {
35  if (!bfd_check_format (abfd, bfd_object))
36  return NULL;
37 
38  if (abfd->build_id != NULL)
39  return abfd->build_id;
40 
41  /* No build-id */
42  return NULL;
43 }
44 
45 /* See build-id.h. */
46 
47 int
48 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
49 {
50  const struct bfd_build_id *found;
51  int retval = 0;
52 
53  found = build_id_bfd_get (abfd);
54 
55  if (found == NULL)
56  warning (_("File \"%s\" has no build-id, file skipped"),
57  bfd_get_filename (abfd));
58  else if (found->size != check_len
59  || memcmp (found->data, check, found->size) != 0)
60  warning (_("File \"%s\" has a different build-id, file skipped"),
61  bfd_get_filename (abfd));
62  else
63  retval = 1;
64 
65  return retval;
66 }
67 
68 /* See build-id.h. */
69 
71 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
72 {
73  char *link, *debugdir;
74  VEC (char_ptr) *debugdir_vec;
75  struct cleanup *back_to;
76  int ix;
77  gdb_bfd_ref_ptr abfd;
78  int alloc_len;
79 
80  /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
81  alloc_len = (strlen (debug_file_directory)
82  + (sizeof "/.build-id/" - 1) + 1
83  + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
84  link = (char *) alloca (alloc_len);
85 
86  /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
87  cause "/.build-id/..." lookups. */
88 
89  debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
90  back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
91 
92  for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
93  {
94  size_t debugdir_len = strlen (debugdir);
95  const gdb_byte *data = build_id;
96  size_t size = build_id_len;
97  char *s;
98  char *filename = NULL;
99  struct cleanup *inner;
100 
101  memcpy (link, debugdir, debugdir_len);
102  s = &link[debugdir_len];
103  s += sprintf (s, "/.build-id/");
104  if (size > 0)
105  {
106  size--;
107  s += sprintf (s, "%02x", (unsigned) *data++);
108  }
109  if (size > 0)
110  *s++ = '/';
111  while (size-- > 0)
112  s += sprintf (s, "%02x", (unsigned) *data++);
113  strcpy (s, ".debug");
114 
116  printf_unfiltered (_(" Trying %s\n"), link);
117 
118  /* lrealpath() is expensive even for the usually non-existent files. */
119  if (access (link, F_OK) == 0)
120  filename = lrealpath (link);
121 
122  if (filename == NULL)
123  continue;
124 
125  /* We expect to be silent on the non-existing files. */
126  inner = make_cleanup (xfree, filename);
127  abfd = gdb_bfd_open (filename, gnutarget, -1);
128  do_cleanups (inner);
129 
130  if (abfd == NULL)
131  continue;
132 
133  if (build_id_verify (abfd.get(), build_id_len, build_id))
134  break;
135 
136  abfd.release ();
137  }
138 
139  do_cleanups (back_to);
140  return abfd;
141 }
142 
143 /* See build-id.h. */
144 
145 char *
147 {
148  const struct bfd_build_id *build_id;
149 
150  build_id = build_id_bfd_get (objfile->obfd);
151  if (build_id != NULL)
152  {
154  printf_unfiltered (_("\nLooking for separate debug info (build-id) for "
155  "%s\n"), objfile_name (objfile));
156 
157  gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
158  build_id->data));
159  /* Prevent looping on a stripped .debug file. */
160  if (abfd != NULL
161  && filename_cmp (bfd_get_filename (abfd.get ()),
162  objfile_name (objfile)) == 0)
163  warning (_("\"%s\": separate debug info file has no debug info"),
164  bfd_get_filename (abfd.get ()));
165  else if (abfd != NULL)
166  return xstrdup (bfd_get_filename (abfd.get ()));
167  }
168  return NULL;
169 }
bfd * obfd
Definition: objfiles.h:342
char * find_separate_debug_file_by_buildid(struct objfile *objfile)
Definition: build-id.c:146
void xfree(void *)
void warning(const char *fmt,...)
Definition: errors.c:26
#define VEC(T)
Definition: vec.h:414
#define _(String)
Definition: gdb_locale.h:35
char * char_ptr
Definition: gdb_vecs.h:25
char * debug_file_directory
Definition: symfile.c:1414
#define VEC_iterate(T, V, I, P)
Definition: vec.h:181
gdb_bfd_ref_ptr gdb_bfd_open(const char *name, const char *target, int fd)
Definition: gdb_bfd.c:383
int build_id_verify(bfd *abfd, size_t check_len, const bfd_byte *check)
Definition: build-id.c:48
char * gnutarget
Definition: corefile.c:442
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:116
void printf_unfiltered(const char *format,...)
Definition: utils.c:2056
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1557
int separate_debug_file_debug
Definition: symfile.c:1333
bfd_byte gdb_byte
Definition: common-types.h:38
const struct bfd_build_id * build_id_bfd_get(bfd *abfd)
Definition: build-id.c:33
static void check(BOOL ok, const char *file, int line)
Definition: windows-nat.c:323
T * get() const
Definition: gdb_ref_ptr.h:130
gdb_bfd_ref_ptr build_id_to_debug_bfd(size_t build_id_len, const bfd_byte *build_id)
Definition: build-id.c:71
struct cleanup * make_cleanup_free_char_ptr_vec(VEC(char_ptr) *char_ptr_vec)
Definition: utils.c:3192
size_t size
Definition: go32-nat.c:242
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:174