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