]>
Commit | Line | Data |
---|---|---|
8fb8eb5c DE |
1 | /* Debug logging for the symbol file functions for the GNU debugger, GDB. |
2 | ||
1d506c26 | 3 | Copyright (C) 2013-2024 Free Software Foundation, Inc. |
8fb8eb5c DE |
4 | |
5 | Contributed by Cygnus Support, using pieces from other GDB modules. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | /* Note: Be careful with functions that can throw errors. | |
23 | We want to see a logging message regardless of whether an error was thrown. | |
24 | This typically means printing a message before calling the real function | |
25 | and then if the function returns a result printing a message after it | |
26 | returns. */ | |
27 | ||
28 | #include "defs.h" | |
29 | #include "gdbcmd.h" | |
30 | #include "objfiles.h" | |
76727919 | 31 | #include "observable.h" |
8fb8eb5c DE |
32 | #include "source.h" |
33 | #include "symtab.h" | |
34 | #include "symfile.h" | |
84d865e3 | 35 | #include "block.h" |
536a40f3 | 36 | #include "filenames.h" |
7d9931cc | 37 | #include "cli/cli-style.h" |
27807da5 AB |
38 | #include "build-id.h" |
39 | #include "debuginfod-support.h" | |
8fb8eb5c DE |
40 | |
41 | /* We need to save a pointer to the real symbol functions. | |
42 | Plus, the debug versions are malloc'd because we have to NULL out the | |
43 | ones that are NULL in the real copy. */ | |
44 | ||
45 | struct debug_sym_fns_data | |
46 | { | |
8c42777c TT |
47 | const struct sym_fns *real_sf = nullptr; |
48 | struct sym_fns debug_sf {}; | |
8fb8eb5c DE |
49 | }; |
50 | ||
51 | /* We need to record a pointer to the real set of functions for each | |
52 | objfile. */ | |
08b8a139 | 53 | static const registry<objfile>::key<debug_sym_fns_data> |
8c42777c | 54 | symfile_debug_objfile_data_key; |
8fb8eb5c | 55 | |
491144b5 CB |
56 | /* If true all calls to the symfile functions are logged. */ |
57 | static bool debug_symfile = false; | |
8fb8eb5c DE |
58 | |
59 | /* Return non-zero if symfile debug logging is installed. */ | |
60 | ||
61 | static int | |
62 | symfile_debug_installed (struct objfile *objfile) | |
63 | { | |
64 | return (objfile->sf != NULL | |
8c42777c | 65 | && symfile_debug_objfile_data_key.get (objfile) != NULL); |
8fb8eb5c DE |
66 | } |
67 | ||
8fb8eb5c DE |
68 | /* Utility return the name to print for SYMTAB. */ |
69 | ||
70 | static const char * | |
71 | debug_symtab_name (struct symtab *symtab) | |
72 | { | |
73 | return symtab_to_filename_for_display (symtab); | |
74 | } | |
75 | \f | |
8fb8eb5c | 76 | |
4d080b46 TT |
77 | /* See objfiles.h. */ |
78 | ||
79 | bool | |
80 | objfile::has_partial_symbols () | |
8fb8eb5c | 81 | { |
4d080b46 | 82 | bool retval = false; |
8fb8eb5c | 83 | |
4d080b46 TT |
84 | /* If we have not read psymbols, but we have a function capable of reading |
85 | them, then that is an indication that they are in fact available. Without | |
86 | this function the symbols may have been already read in but they also may | |
87 | not be present in this objfile. */ | |
e1114590 TT |
88 | for (const auto &iter : qf) |
89 | { | |
245703b3 | 90 | retval = iter->has_symbols (this); |
e1114590 TT |
91 | if (retval) |
92 | break; | |
93 | } | |
8fb8eb5c | 94 | |
4d080b46 | 95 | if (debug_symfile) |
6cb06a8c TT |
96 | gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n", |
97 | objfile_debug_name (this), retval); | |
8fb8eb5c DE |
98 | |
99 | return retval; | |
100 | } | |
101 | ||
fc4d5ebf AB |
102 | /* See objfiles.h. */ |
103 | bool | |
104 | objfile::has_unexpanded_symtabs () | |
105 | { | |
106 | if (debug_symfile) | |
6cb06a8c TT |
107 | gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n", |
108 | objfile_debug_name (this)); | |
fc4d5ebf AB |
109 | |
110 | bool result = false; | |
245703b3 | 111 | for (const auto &iter : qf) |
fc4d5ebf AB |
112 | { |
113 | if (iter->has_unexpanded_symtabs (this)) | |
114 | { | |
115 | result = true; | |
116 | break; | |
117 | } | |
118 | } | |
119 | ||
120 | if (debug_symfile) | |
6cb06a8c TT |
121 | gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n", |
122 | objfile_debug_name (this), (result ? 1 : 0)); | |
fc4d5ebf AB |
123 | |
124 | return result; | |
125 | } | |
126 | ||
4d080b46 TT |
127 | struct symtab * |
128 | objfile::find_last_source_symtab () | |
8fb8eb5c | 129 | { |
4d080b46 | 130 | struct symtab *retval = nullptr; |
8fb8eb5c | 131 | |
4d080b46 | 132 | if (debug_symfile) |
6cb06a8c TT |
133 | gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n", |
134 | objfile_debug_name (this)); | |
8fb8eb5c | 135 | |
245703b3 | 136 | for (const auto &iter : qf) |
e1114590 TT |
137 | { |
138 | retval = iter->find_last_source_symtab (this); | |
139 | if (retval != nullptr) | |
140 | break; | |
141 | } | |
8fb8eb5c | 142 | |
4d080b46 | 143 | if (debug_symfile) |
6cb06a8c TT |
144 | gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n", |
145 | retval ? debug_symtab_name (retval) : "NULL"); | |
8fb8eb5c DE |
146 | |
147 | return retval; | |
148 | } | |
149 | ||
4d080b46 TT |
150 | void |
151 | objfile::forget_cached_source_info () | |
8fb8eb5c | 152 | { |
4d080b46 | 153 | if (debug_symfile) |
6cb06a8c TT |
154 | gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n", |
155 | objfile_debug_name (this)); | |
8fb8eb5c | 156 | |
21f6be77 TT |
157 | for (compunit_symtab *cu : compunits ()) |
158 | { | |
159 | for (symtab *s : cu->filetabs ()) | |
160 | { | |
161 | if (s->fullname != NULL) | |
162 | { | |
163 | xfree (s->fullname); | |
164 | s->fullname = NULL; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
245703b3 | 169 | for (const auto &iter : qf) |
e1114590 | 170 | iter->forget_cached_source_info (this); |
8fb8eb5c DE |
171 | } |
172 | ||
4d080b46 TT |
173 | bool |
174 | objfile::map_symtabs_matching_filename | |
175 | (const char *name, const char *real_path, | |
14bc53a8 | 176 | gdb::function_view<bool (symtab *)> callback) |
8fb8eb5c | 177 | { |
4d080b46 | 178 | if (debug_symfile) |
6cb06a8c TT |
179 | gdb_printf (gdb_stdlog, |
180 | "qf->map_symtabs_matching_filename (%s, \"%s\", " | |
181 | "\"%s\", %s)\n", | |
182 | objfile_debug_name (this), name, | |
183 | real_path ? real_path : NULL, | |
184 | host_address_to_string (&callback)); | |
8fb8eb5c | 185 | |
536a40f3 TT |
186 | bool retval = true; |
187 | const char *name_basename = lbasename (name); | |
188 | ||
189 | auto match_one_filename = [&] (const char *filename, bool basenames) | |
190 | { | |
191 | if (compare_filenames_for_search (filename, name)) | |
192 | return true; | |
193 | if (basenames && FILENAME_CMP (name_basename, filename) == 0) | |
194 | return true; | |
195 | if (real_path != nullptr && IS_ABSOLUTE_PATH (filename) | |
196 | && IS_ABSOLUTE_PATH (real_path)) | |
197 | return filename_cmp (filename, real_path) == 0; | |
198 | return false; | |
199 | }; | |
200 | ||
201 | compunit_symtab *last_made = this->compunit_symtabs; | |
202 | ||
203 | auto on_expansion = [&] (compunit_symtab *symtab) | |
204 | { | |
205 | /* The callback to iterate_over_some_symtabs returns false to keep | |
206 | going and true to continue, so we have to invert the result | |
207 | here, for expand_symtabs_matching. */ | |
208 | bool result = !iterate_over_some_symtabs (name, real_path, | |
209 | this->compunit_symtabs, | |
210 | last_made, | |
211 | callback); | |
212 | last_made = this->compunit_symtabs; | |
213 | return result; | |
214 | }; | |
215 | ||
245703b3 | 216 | for (const auto &iter : qf) |
e1114590 | 217 | { |
536a40f3 TT |
218 | if (!iter->expand_symtabs_matching (this, |
219 | match_one_filename, | |
220 | nullptr, | |
221 | nullptr, | |
222 | on_expansion, | |
223 | (SEARCH_GLOBAL_BLOCK | |
224 | | SEARCH_STATIC_BLOCK), | |
225 | UNDEF_DOMAIN, | |
226 | ALL_DOMAIN)) | |
227 | { | |
228 | retval = false; | |
229 | break; | |
230 | } | |
e1114590 | 231 | } |
8fb8eb5c | 232 | |
4d080b46 | 233 | if (debug_symfile) |
6cb06a8c TT |
234 | gdb_printf (gdb_stdlog, |
235 | "qf->map_symtabs_matching_filename (...) = %d\n", | |
236 | retval); | |
8fb8eb5c | 237 | |
536a40f3 TT |
238 | /* We must re-invert the return value here to match the caller's |
239 | expectations. */ | |
240 | return !retval; | |
8fb8eb5c DE |
241 | } |
242 | ||
4d080b46 TT |
243 | struct compunit_symtab * |
244 | objfile::lookup_symbol (block_enum kind, const char *name, domain_enum domain) | |
8fb8eb5c | 245 | { |
4d080b46 | 246 | struct compunit_symtab *retval = nullptr; |
8fb8eb5c | 247 | |
4d080b46 | 248 | if (debug_symfile) |
6cb06a8c TT |
249 | gdb_printf (gdb_stdlog, |
250 | "qf->lookup_symbol (%s, %d, \"%s\", %s)\n", | |
251 | objfile_debug_name (this), kind, name, | |
252 | domain_name (domain)); | |
8fb8eb5c | 253 | |
84d865e3 TT |
254 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
255 | ||
256 | auto search_one_symtab = [&] (compunit_symtab *stab) | |
257 | { | |
258 | struct symbol *sym, *with_opaque = NULL; | |
af39c5c8 | 259 | const struct blockvector *bv = stab->blockvector (); |
63d609de | 260 | const struct block *block = bv->block (kind); |
84d865e3 | 261 | |
b7a92724 | 262 | sym = block_find_symbol (block, lookup_name, domain, &with_opaque); |
84d865e3 TT |
263 | |
264 | /* Some caution must be observed with overloaded functions | |
265 | and methods, since the index will not contain any overload | |
266 | information (but NAME might contain it). */ | |
267 | ||
b7a92724 | 268 | if (sym != nullptr) |
84d865e3 TT |
269 | { |
270 | retval = stab; | |
271 | /* Found it. */ | |
272 | return false; | |
273 | } | |
b7a92724 | 274 | if (with_opaque != nullptr) |
84d865e3 TT |
275 | retval = stab; |
276 | ||
277 | /* Keep looking through other psymtabs. */ | |
278 | return true; | |
279 | }; | |
280 | ||
245703b3 | 281 | for (const auto &iter : qf) |
e1114590 | 282 | { |
84d865e3 TT |
283 | if (!iter->expand_symtabs_matching (this, |
284 | nullptr, | |
285 | &lookup_name, | |
286 | nullptr, | |
287 | search_one_symtab, | |
288 | kind == GLOBAL_BLOCK | |
289 | ? SEARCH_GLOBAL_BLOCK | |
290 | : SEARCH_STATIC_BLOCK, | |
291 | domain, | |
292 | ALL_DOMAIN)) | |
e1114590 TT |
293 | break; |
294 | } | |
8fb8eb5c | 295 | |
4d080b46 | 296 | if (debug_symfile) |
6cb06a8c TT |
297 | gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n", |
298 | retval | |
299 | ? debug_symtab_name (retval->primary_filetab ()) | |
300 | : "NULL"); | |
8fb8eb5c DE |
301 | |
302 | return retval; | |
303 | } | |
304 | ||
4d080b46 | 305 | void |
4829711b | 306 | objfile::print_stats (bool print_bcache) |
8fb8eb5c | 307 | { |
4d080b46 | 308 | if (debug_symfile) |
6cb06a8c TT |
309 | gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n", |
310 | objfile_debug_name (this), print_bcache); | |
8fb8eb5c | 311 | |
245703b3 | 312 | for (const auto &iter : qf) |
e1114590 | 313 | iter->print_stats (this, print_bcache); |
8fb8eb5c DE |
314 | } |
315 | ||
4d080b46 TT |
316 | void |
317 | objfile::dump () | |
8fb8eb5c | 318 | { |
4d080b46 | 319 | if (debug_symfile) |
6cb06a8c TT |
320 | gdb_printf (gdb_stdlog, "qf->dump (%s)\n", |
321 | objfile_debug_name (this)); | |
8fb8eb5c | 322 | |
e1114590 TT |
323 | for (const auto &iter : qf) |
324 | iter->dump (this); | |
8fb8eb5c DE |
325 | } |
326 | ||
4d080b46 TT |
327 | void |
328 | objfile::expand_symtabs_for_function (const char *func_name) | |
8fb8eb5c | 329 | { |
4d080b46 | 330 | if (debug_symfile) |
6cb06a8c TT |
331 | gdb_printf (gdb_stdlog, |
332 | "qf->expand_symtabs_for_function (%s, \"%s\")\n", | |
333 | objfile_debug_name (this), func_name); | |
8fb8eb5c | 334 | |
7089bd88 TT |
335 | lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL); |
336 | lookup_name_info lookup_name = base_lookup.make_ignore_params (); | |
337 | ||
245703b3 | 338 | for (const auto &iter : qf) |
7089bd88 TT |
339 | iter->expand_symtabs_matching (this, |
340 | nullptr, | |
341 | &lookup_name, | |
342 | nullptr, | |
343 | nullptr, | |
344 | (SEARCH_GLOBAL_BLOCK | |
345 | | SEARCH_STATIC_BLOCK), | |
346 | VAR_DOMAIN, | |
347 | ALL_DOMAIN); | |
8fb8eb5c DE |
348 | } |
349 | ||
4d080b46 TT |
350 | void |
351 | objfile::expand_all_symtabs () | |
8fb8eb5c | 352 | { |
4d080b46 | 353 | if (debug_symfile) |
6cb06a8c TT |
354 | gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n", |
355 | objfile_debug_name (this)); | |
8fb8eb5c | 356 | |
245703b3 | 357 | for (const auto &iter : qf) |
e1114590 | 358 | iter->expand_all_symtabs (this); |
8fb8eb5c DE |
359 | } |
360 | ||
4d080b46 TT |
361 | void |
362 | objfile::expand_symtabs_with_fullname (const char *fullname) | |
8fb8eb5c | 363 | { |
4d080b46 | 364 | if (debug_symfile) |
6cb06a8c TT |
365 | gdb_printf (gdb_stdlog, |
366 | "qf->expand_symtabs_with_fullname (%s, \"%s\")\n", | |
367 | objfile_debug_name (this), fullname); | |
8fb8eb5c | 368 | |
90160b57 TT |
369 | const char *basename = lbasename (fullname); |
370 | auto file_matcher = [&] (const char *filename, bool basenames) | |
371 | { | |
372 | return filename_cmp (basenames ? basename : fullname, filename) == 0; | |
373 | }; | |
374 | ||
245703b3 | 375 | for (const auto &iter : qf) |
90160b57 TT |
376 | iter->expand_symtabs_matching (this, |
377 | file_matcher, | |
378 | nullptr, | |
379 | nullptr, | |
380 | nullptr, | |
381 | (SEARCH_GLOBAL_BLOCK | |
382 | | SEARCH_STATIC_BLOCK), | |
383 | UNDEF_DOMAIN, | |
384 | ALL_DOMAIN); | |
8fb8eb5c DE |
385 | } |
386 | ||
df35e626 | 387 | bool |
4d080b46 TT |
388 | objfile::expand_symtabs_matching |
389 | (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher, | |
c1a66c06 | 390 | const lookup_name_info *lookup_name, |
14bc53a8 PA |
391 | gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher, |
392 | gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify, | |
03a8ea51 | 393 | block_search_flags search_flags, |
3bfa51a7 | 394 | domain_enum domain, |
14bc53a8 | 395 | enum search_domain kind) |
8fb8eb5c | 396 | { |
4f348ca8 TT |
397 | /* This invariant is documented in quick-functions.h. */ |
398 | gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr); | |
399 | ||
4d080b46 | 400 | if (debug_symfile) |
6cb06a8c TT |
401 | gdb_printf (gdb_stdlog, |
402 | "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n", | |
403 | objfile_debug_name (this), | |
404 | host_address_to_string (&file_matcher), | |
405 | host_address_to_string (&symbol_matcher), | |
406 | host_address_to_string (&expansion_notify), | |
407 | search_domain_name (kind)); | |
4d080b46 | 408 | |
245703b3 | 409 | for (const auto &iter : qf) |
df35e626 TT |
410 | if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name, |
411 | symbol_matcher, expansion_notify, | |
3bfa51a7 | 412 | search_flags, domain, kind)) |
df35e626 TT |
413 | return false; |
414 | return true; | |
8fb8eb5c DE |
415 | } |
416 | ||
4d080b46 TT |
417 | struct compunit_symtab * |
418 | objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol, | |
43f3e411 DE |
419 | CORE_ADDR pc, |
420 | struct obj_section *section, | |
421 | int warn_if_readin) | |
8fb8eb5c | 422 | { |
4d080b46 TT |
423 | struct compunit_symtab *retval = nullptr; |
424 | ||
425 | if (debug_symfile) | |
6cb06a8c TT |
426 | gdb_printf (gdb_stdlog, |
427 | "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n", | |
428 | objfile_debug_name (this), | |
429 | host_address_to_string (msymbol.minsym), | |
430 | hex_string (pc), | |
431 | host_address_to_string (section), | |
432 | warn_if_readin); | |
4d080b46 | 433 | |
245703b3 | 434 | for (const auto &iter : qf) |
e1114590 TT |
435 | { |
436 | retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section, | |
437 | warn_if_readin); | |
438 | if (retval != nullptr) | |
439 | break; | |
440 | } | |
4d080b46 TT |
441 | |
442 | if (debug_symfile) | |
6cb06a8c TT |
443 | gdb_printf (gdb_stdlog, |
444 | "qf->find_pc_sect_compunit_symtab (...) = %s\n", | |
445 | retval | |
446 | ? debug_symtab_name (retval->primary_filetab ()) | |
447 | : "NULL"); | |
8fb8eb5c DE |
448 | |
449 | return retval; | |
450 | } | |
451 | ||
4d080b46 | 452 | void |
f4655dee TT |
453 | objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun, |
454 | bool need_fullname) | |
8fb8eb5c | 455 | { |
4d080b46 | 456 | if (debug_symfile) |
6cb06a8c TT |
457 | gdb_printf (gdb_stdlog, |
458 | "qf->map_symbol_filenames (%s, ..., %d)\n", | |
459 | objfile_debug_name (this), | |
460 | need_fullname); | |
8fb8eb5c | 461 | |
245703b3 | 462 | for (const auto &iter : qf) |
f4655dee | 463 | iter->map_symbol_filenames (this, fun, need_fullname); |
8fb8eb5c DE |
464 | } |
465 | ||
4ea870ef TT |
466 | void |
467 | objfile::compute_main_name () | |
468 | { | |
469 | if (debug_symfile) | |
470 | gdb_printf (gdb_stdlog, | |
471 | "qf->compute_main_name (%s)\n", | |
472 | objfile_debug_name (this)); | |
473 | ||
245703b3 | 474 | for (const auto &iter : qf) |
4ea870ef TT |
475 | iter->compute_main_name (this); |
476 | } | |
477 | ||
4d080b46 TT |
478 | struct compunit_symtab * |
479 | objfile::find_compunit_symtab_by_address (CORE_ADDR address) | |
71a3c369 | 480 | { |
4d080b46 | 481 | if (debug_symfile) |
6cb06a8c TT |
482 | gdb_printf (gdb_stdlog, |
483 | "qf->find_compunit_symtab_by_address (%s, %s)\n", | |
484 | objfile_debug_name (this), | |
485 | hex_string (address)); | |
71a3c369 TT |
486 | |
487 | struct compunit_symtab *result = NULL; | |
245703b3 | 488 | for (const auto &iter : qf) |
e1114590 TT |
489 | { |
490 | result = iter->find_compunit_symtab_by_address (this, address); | |
491 | if (result != nullptr) | |
492 | break; | |
493 | } | |
71a3c369 | 494 | |
4d080b46 | 495 | if (debug_symfile) |
6cb06a8c TT |
496 | gdb_printf (gdb_stdlog, |
497 | "qf->find_compunit_symtab_by_address (...) = %s\n", | |
498 | result | |
499 | ? debug_symtab_name (result->primary_filetab ()) | |
500 | : "NULL"); | |
4d080b46 TT |
501 | |
502 | return result; | |
503 | } | |
504 | ||
505 | enum language | |
506 | objfile::lookup_global_symbol_language (const char *name, | |
507 | domain_enum domain, | |
508 | bool *symbol_found_p) | |
509 | { | |
510 | enum language result = language_unknown; | |
e1114590 | 511 | *symbol_found_p = false; |
4d080b46 | 512 | |
245703b3 | 513 | for (const auto &iter : qf) |
e1114590 TT |
514 | { |
515 | result = iter->lookup_global_symbol_language (this, name, domain, | |
516 | symbol_found_p); | |
517 | if (*symbol_found_p) | |
518 | break; | |
519 | } | |
71a3c369 TT |
520 | |
521 | return result; | |
522 | } | |
523 | ||
6234ba17 AB |
524 | /* Call LOOKUP_FUNC to find the filename of a file containing the separate |
525 | debug information matching OBJFILE. If LOOKUP_FUNC does return a | |
526 | filename then open this file and return a std::pair containing the | |
527 | gdb_bfd_ref_ptr of the open file and the filename returned by | |
528 | LOOKUP_FUNC, otherwise this function returns an empty pair; the first | |
529 | item will be nullptr, and the second will be an empty string. | |
530 | ||
531 | Any warnings generated by this function, or by calling LOOKUP_FUNC are | |
532 | placed into WARNINGS, these warnings are only displayed to the user if | |
533 | GDB is unable to find the separate debug information via any route. */ | |
534 | static std::pair<gdb_bfd_ref_ptr, std::string> | |
535 | simple_find_and_open_separate_symbol_file | |
536 | (struct objfile *objfile, | |
537 | std::string (*lookup_func) (struct objfile *, deferred_warnings *), | |
538 | deferred_warnings *warnings) | |
539 | { | |
540 | std::string filename = lookup_func (objfile, warnings); | |
541 | ||
542 | if (!filename.empty ()) | |
543 | { | |
544 | gdb_bfd_ref_ptr symfile_bfd | |
545 | = symfile_bfd_open_no_error (filename.c_str ()); | |
546 | if (symfile_bfd != nullptr) | |
547 | return { symfile_bfd, filename }; | |
548 | } | |
549 | ||
550 | return {}; | |
551 | } | |
552 | ||
553 | /* Lookup separate debug information for OBJFILE via debuginfod. If | |
554 | successful the debug information will be have been downloaded into the | |
555 | debuginfod cache and this function will return a std::pair containing a | |
556 | gdb_bfd_ref_ptr of the open debug information file and the filename for | |
557 | the file within the debuginfod cache. If no debug information could be | |
558 | found then this function returns an empty pair; the first item will be | |
559 | nullptr, and the second will be an empty string. */ | |
560 | ||
561 | static std::pair<gdb_bfd_ref_ptr, std::string> | |
562 | debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile) | |
563 | { | |
564 | const struct bfd_build_id *build_id | |
565 | = build_id_bfd_get (objfile->obfd.get ()); | |
566 | const char *filename = bfd_get_filename (objfile->obfd.get ()); | |
567 | ||
568 | if (build_id != nullptr) | |
569 | { | |
570 | gdb::unique_xmalloc_ptr<char> symfile_path; | |
571 | scoped_fd fd (debuginfod_debuginfo_query (build_id->data, build_id->size, | |
572 | filename, &symfile_path)); | |
573 | ||
574 | if (fd.get () >= 0) | |
575 | { | |
576 | /* File successfully retrieved from server. */ | |
577 | gdb_bfd_ref_ptr debug_bfd | |
578 | (symfile_bfd_open_no_error (symfile_path.get ())); | |
579 | ||
580 | if (debug_bfd != nullptr | |
581 | && build_id_verify (debug_bfd.get (), | |
582 | build_id->size, build_id->data)) | |
583 | return { debug_bfd, std::string (symfile_path.get ()) }; | |
584 | } | |
585 | } | |
586 | ||
587 | return {}; | |
588 | } | |
589 | ||
27807da5 AB |
590 | /* See objfiles.h. */ |
591 | ||
592 | bool | |
593 | objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags) | |
594 | { | |
661d98a3 AB |
595 | bool has_dwarf2 = false; |
596 | ||
597 | /* Usually we only make a single pass when looking for separate debug | |
598 | information. However, it is possible for an extension language hook | |
599 | to request that GDB make a second pass, in which case max_attempts | |
600 | will be updated, and the loop restarted. */ | |
601 | for (unsigned attempt = 0, max_attempts = 1; | |
602 | attempt < max_attempts && !has_dwarf2; | |
603 | ++attempt) | |
604 | { | |
605 | gdb_assert (max_attempts <= 2); | |
606 | ||
607 | deferred_warnings warnings; | |
608 | gdb_bfd_ref_ptr debug_bfd; | |
609 | std::string filename; | |
610 | ||
611 | std::tie (debug_bfd, filename) | |
612 | = simple_find_and_open_separate_symbol_file | |
613 | (this, find_separate_debug_file_by_buildid, &warnings); | |
614 | ||
615 | if (debug_bfd == nullptr) | |
616 | std::tie (debug_bfd, filename) | |
617 | = simple_find_and_open_separate_symbol_file | |
618 | (this, find_separate_debug_file_by_debuglink, &warnings); | |
619 | ||
620 | /* Only try debuginfod on the first attempt. Sure, we could imagine | |
621 | an extension that somehow adds the required debug info to the | |
622 | debuginfod server but, at least for now, we don't support this | |
623 | scenario. Better for the extension to return new debug info | |
624 | directly to GDB. Plus, going to the debuginfod server might be | |
625 | slow, so that's a good argument for only doing this once. */ | |
626 | if (debug_bfd == nullptr && attempt == 0) | |
627 | std::tie (debug_bfd, filename) | |
628 | = debuginfod_find_and_open_separate_symbol_file (this); | |
629 | ||
630 | if (debug_bfd != nullptr) | |
631 | { | |
632 | /* We found a separate debug info symbol file. If this is our | |
633 | first attempt then setting HAS_DWARF2 will cause us to break | |
634 | from the attempt loop. */ | |
635 | symbol_file_add_separate (debug_bfd, filename.c_str (), | |
636 | symfile_flags, this); | |
637 | has_dwarf2 = true; | |
638 | } | |
639 | else if (attempt == 0) | |
640 | { | |
641 | /* Failed to find a separate debug info symbol file. Call out to | |
642 | the extension languages. The user might have registered an | |
643 | extension that can find the debug info for us, or maybe give | |
644 | the user a system specific message that guides them to finding | |
645 | the missing debug info. */ | |
646 | ||
647 | ext_lang_missing_debuginfo_result ext_result | |
648 | = ext_lang_handle_missing_debuginfo (this); | |
649 | if (!ext_result.filename ().empty ()) | |
650 | { | |
651 | /* Extension found a suitable debug file for us. */ | |
652 | debug_bfd | |
653 | = symfile_bfd_open_no_error (ext_result.filename ().c_str ()); | |
27807da5 | 654 | |
661d98a3 AB |
655 | if (debug_bfd != nullptr) |
656 | { | |
657 | symbol_file_add_separate (debug_bfd, | |
658 | ext_result.filename ().c_str (), | |
659 | symfile_flags, this); | |
660 | has_dwarf2 = true; | |
661 | } | |
662 | } | |
663 | else if (ext_result.try_again ()) | |
664 | { | |
665 | max_attempts = 2; | |
666 | continue; | |
667 | } | |
668 | } | |
27807da5 | 669 | |
661d98a3 AB |
670 | /* If we still have not got a separate debug symbol file, then |
671 | emit any warnings we've collected so far. */ | |
672 | if (!has_dwarf2) | |
673 | warnings.emit (); | |
27807da5 | 674 | } |
6234ba17 | 675 | |
661d98a3 | 676 | return has_dwarf2; |
27807da5 AB |
677 | } |
678 | ||
8fb8eb5c DE |
679 | \f |
680 | /* Debugging version of struct sym_probe_fns. */ | |
681 | ||
814cf43a | 682 | static const std::vector<std::unique_ptr<probe>> & |
8fb8eb5c DE |
683 | debug_sym_get_probes (struct objfile *objfile) |
684 | { | |
19ba03f4 | 685 | const struct debug_sym_fns_data *debug_data |
8c42777c | 686 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 687 | |
814cf43a | 688 | const std::vector<std::unique_ptr<probe>> &retval |
aaa63a31 | 689 | = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile); |
8fb8eb5c | 690 | |
6cb06a8c TT |
691 | gdb_printf (gdb_stdlog, |
692 | "probes->sym_get_probes (%s) = %s\n", | |
693 | objfile_debug_name (objfile), | |
694 | host_address_to_string (retval.data ())); | |
8fb8eb5c DE |
695 | |
696 | return retval; | |
697 | } | |
698 | ||
8fb8eb5c DE |
699 | static const struct sym_probe_fns debug_sym_probe_fns = |
700 | { | |
701 | debug_sym_get_probes, | |
8fb8eb5c DE |
702 | }; |
703 | \f | |
704 | /* Debugging version of struct sym_fns. */ | |
705 | ||
706 | static void | |
707 | debug_sym_new_init (struct objfile *objfile) | |
708 | { | |
19ba03f4 | 709 | const struct debug_sym_fns_data *debug_data |
8c42777c | 710 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 711 | |
6cb06a8c TT |
712 | gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n", |
713 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
714 | |
715 | debug_data->real_sf->sym_new_init (objfile); | |
716 | } | |
717 | ||
718 | static void | |
719 | debug_sym_init (struct objfile *objfile) | |
720 | { | |
19ba03f4 | 721 | const struct debug_sym_fns_data *debug_data |
8c42777c | 722 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 723 | |
6cb06a8c TT |
724 | gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n", |
725 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
726 | |
727 | debug_data->real_sf->sym_init (objfile); | |
728 | } | |
729 | ||
730 | static void | |
b15cc25c | 731 | debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags) |
8fb8eb5c | 732 | { |
19ba03f4 | 733 | const struct debug_sym_fns_data *debug_data |
8c42777c | 734 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 735 | |
6cb06a8c TT |
736 | gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n", |
737 | objfile_debug_name (objfile), (unsigned) symfile_flags); | |
8fb8eb5c DE |
738 | |
739 | debug_data->real_sf->sym_read (objfile, symfile_flags); | |
740 | } | |
741 | ||
8fb8eb5c DE |
742 | static void |
743 | debug_sym_finish (struct objfile *objfile) | |
744 | { | |
19ba03f4 | 745 | const struct debug_sym_fns_data *debug_data |
8c42777c | 746 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 747 | |
6cb06a8c TT |
748 | gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n", |
749 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
750 | |
751 | debug_data->real_sf->sym_finish (objfile); | |
752 | } | |
753 | ||
754 | static void | |
755 | debug_sym_offsets (struct objfile *objfile, | |
37e136b1 | 756 | const section_addr_info &info) |
8fb8eb5c | 757 | { |
19ba03f4 | 758 | const struct debug_sym_fns_data *debug_data |
8c42777c | 759 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 760 | |
6cb06a8c TT |
761 | gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n", |
762 | objfile_debug_name (objfile), | |
763 | host_address_to_string (&info)); | |
8fb8eb5c DE |
764 | |
765 | debug_data->real_sf->sym_offsets (objfile, info); | |
766 | } | |
767 | ||
62982abd | 768 | static symfile_segment_data_up |
8fb8eb5c DE |
769 | debug_sym_segments (bfd *abfd) |
770 | { | |
771 | /* This API function is annoying, it doesn't take a "this" pointer. | |
772 | Fortunately it is only used in one place where we (re-)lookup the | |
773 | sym_fns table to use. Thus we will never be called. */ | |
774 | gdb_assert_not_reached ("debug_sym_segments called"); | |
775 | } | |
776 | ||
777 | static void | |
778 | debug_sym_read_linetable (struct objfile *objfile) | |
779 | { | |
19ba03f4 | 780 | const struct debug_sym_fns_data *debug_data |
8c42777c | 781 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 782 | |
6cb06a8c TT |
783 | gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n", |
784 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
785 | |
786 | debug_data->real_sf->sym_read_linetable (objfile); | |
787 | } | |
788 | ||
789 | static bfd_byte * | |
790 | debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf) | |
791 | { | |
19ba03f4 | 792 | const struct debug_sym_fns_data *debug_data |
8c42777c | 793 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c DE |
794 | bfd_byte *retval; |
795 | ||
796 | retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf); | |
797 | ||
6cb06a8c TT |
798 | gdb_printf (gdb_stdlog, |
799 | "sf->sym_relocate (%s, %s, %s) = %s\n", | |
800 | objfile_debug_name (objfile), | |
801 | host_address_to_string (sectp), | |
802 | host_address_to_string (buf), | |
803 | host_address_to_string (retval)); | |
8fb8eb5c DE |
804 | |
805 | return retval; | |
806 | } | |
807 | ||
808 | /* Template of debugging version of struct sym_fns. | |
809 | A copy is made, with sym_flavour updated, and a pointer to the real table | |
810 | installed in real_sf, and then a pointer to the copy is installed in the | |
811 | objfile. */ | |
812 | ||
813 | static const struct sym_fns debug_sym_fns = | |
814 | { | |
815 | debug_sym_new_init, | |
816 | debug_sym_init, | |
817 | debug_sym_read, | |
8fb8eb5c DE |
818 | debug_sym_finish, |
819 | debug_sym_offsets, | |
820 | debug_sym_segments, | |
821 | debug_sym_read_linetable, | |
822 | debug_sym_relocate, | |
823 | &debug_sym_probe_fns, | |
8fb8eb5c DE |
824 | }; |
825 | \f | |
8fb8eb5c DE |
826 | /* Install the debugging versions of the symfile functions for OBJFILE. |
827 | Do not call this if the debug versions are already installed. */ | |
828 | ||
829 | static void | |
830 | install_symfile_debug_logging (struct objfile *objfile) | |
831 | { | |
832 | const struct sym_fns *real_sf; | |
833 | struct debug_sym_fns_data *debug_data; | |
834 | ||
835 | /* The debug versions should not already be installed. */ | |
836 | gdb_assert (!symfile_debug_installed (objfile)); | |
837 | ||
838 | real_sf = objfile->sf; | |
839 | ||
840 | /* Alas we have to preserve NULL entries in REAL_SF. */ | |
8c42777c | 841 | debug_data = new struct debug_sym_fns_data; |
8fb8eb5c DE |
842 | |
843 | #define COPY_SF_PTR(from, to, name, func) \ | |
844 | do { \ | |
845 | if ((from)->name) \ | |
846 | (to)->debug_sf.name = func; \ | |
847 | } while (0) | |
848 | ||
849 | COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init); | |
850 | COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init); | |
851 | COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read); | |
8fb8eb5c DE |
852 | COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish); |
853 | COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets); | |
854 | COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments); | |
855 | COPY_SF_PTR (real_sf, debug_data, sym_read_linetable, | |
856 | debug_sym_read_linetable); | |
857 | COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate); | |
858 | if (real_sf->sym_probe_fns) | |
859 | debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns; | |
8fb8eb5c DE |
860 | |
861 | #undef COPY_SF_PTR | |
862 | ||
863 | debug_data->real_sf = real_sf; | |
8c42777c | 864 | symfile_debug_objfile_data_key.set (objfile, debug_data); |
8fb8eb5c DE |
865 | objfile->sf = &debug_data->debug_sf; |
866 | } | |
867 | ||
868 | /* Uninstall the debugging versions of the symfile functions for OBJFILE. | |
869 | Do not call this if the debug versions are not installed. */ | |
870 | ||
871 | static void | |
872 | uninstall_symfile_debug_logging (struct objfile *objfile) | |
873 | { | |
874 | struct debug_sym_fns_data *debug_data; | |
875 | ||
876 | /* The debug versions should be currently installed. */ | |
877 | gdb_assert (symfile_debug_installed (objfile)); | |
878 | ||
8c42777c | 879 | debug_data = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c DE |
880 | |
881 | objfile->sf = debug_data->real_sf; | |
8c42777c | 882 | symfile_debug_objfile_data_key.clear (objfile); |
8fb8eb5c DE |
883 | } |
884 | ||
885 | /* Call this function to set OBJFILE->SF. | |
886 | Do not set OBJFILE->SF directly. */ | |
887 | ||
888 | void | |
889 | objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf) | |
890 | { | |
891 | if (symfile_debug_installed (objfile)) | |
892 | { | |
893 | gdb_assert (debug_symfile); | |
894 | /* Remove the current one, and reinstall a new one later. */ | |
895 | uninstall_symfile_debug_logging (objfile); | |
896 | } | |
897 | ||
898 | /* Assume debug logging is disabled. */ | |
899 | objfile->sf = sf; | |
900 | ||
901 | /* Turn debug logging on if enabled. */ | |
902 | if (debug_symfile) | |
903 | install_symfile_debug_logging (objfile); | |
904 | } | |
905 | ||
906 | static void | |
eb4c3f4a | 907 | set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c) |
8fb8eb5c | 908 | { |
94c93c35 | 909 | for (struct program_space *pspace : program_spaces) |
2030c079 | 910 | for (objfile *objfile : pspace->objfiles ()) |
99d89cde TT |
911 | { |
912 | if (debug_symfile) | |
913 | { | |
914 | if (!symfile_debug_installed (objfile)) | |
915 | install_symfile_debug_logging (objfile); | |
916 | } | |
917 | else | |
918 | { | |
919 | if (symfile_debug_installed (objfile)) | |
920 | uninstall_symfile_debug_logging (objfile); | |
921 | } | |
922 | } | |
8fb8eb5c DE |
923 | } |
924 | ||
925 | static void | |
926 | show_debug_symfile (struct ui_file *file, int from_tty, | |
927 | struct cmd_list_element *c, const char *value) | |
928 | { | |
6cb06a8c | 929 | gdb_printf (file, _("Symfile debugging is %s.\n"), value); |
8fb8eb5c DE |
930 | } |
931 | ||
6c265988 | 932 | void _initialize_symfile_debug (); |
8fb8eb5c | 933 | void |
6c265988 | 934 | _initialize_symfile_debug () |
8fb8eb5c | 935 | { |
8fb8eb5c DE |
936 | add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\ |
937 | Set debugging of the symfile functions."), _("\ | |
938 | Show debugging of the symfile functions."), _("\ | |
939 | When enabled, all calls to the symfile functions are logged."), | |
940 | set_debug_symfile, show_debug_symfile, | |
941 | &setdebuglist, &showdebuglist); | |
942 | ||
943 | /* Note: We don't need a new-objfile observer because debug logging | |
944 | will be installed when objfile init'n calls objfile_set_sym_fns. */ | |
945 | } |