]>
Commit | Line | Data |
---|---|---|
8fb8eb5c DE |
1 | /* Debug logging for the symbol file functions for the GNU debugger, GDB. |
2 | ||
d01e8234 | 3 | Copyright (C) 2013-2025 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 | ||
5b9707eb | 28 | #include "cli/cli-cmds.h" |
8fb8eb5c | 29 | #include "objfiles.h" |
76727919 | 30 | #include "observable.h" |
8fb8eb5c DE |
31 | #include "source.h" |
32 | #include "symtab.h" | |
33 | #include "symfile.h" | |
84d865e3 | 34 | #include "block.h" |
536a40f3 | 35 | #include "filenames.h" |
27807da5 AB |
36 | #include "build-id.h" |
37 | #include "debuginfod-support.h" | |
8fb8eb5c DE |
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 | { | |
8c42777c TT |
45 | const struct sym_fns *real_sf = nullptr; |
46 | struct sym_fns debug_sf {}; | |
8fb8eb5c DE |
47 | }; |
48 | ||
49 | /* We need to record a pointer to the real set of functions for each | |
50 | objfile. */ | |
08b8a139 | 51 | static const registry<objfile>::key<debug_sym_fns_data> |
8c42777c | 52 | symfile_debug_objfile_data_key; |
8fb8eb5c | 53 | |
491144b5 CB |
54 | /* If true all calls to the symfile functions are logged. */ |
55 | static bool debug_symfile = false; | |
8fb8eb5c DE |
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 | |
8c42777c | 63 | && symfile_debug_objfile_data_key.get (objfile) != NULL); |
8fb8eb5c DE |
64 | } |
65 | ||
8fb8eb5c DE |
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 | |
8fb8eb5c | 74 | |
4d080b46 TT |
75 | /* See objfiles.h. */ |
76 | ||
77 | bool | |
78 | objfile::has_partial_symbols () | |
8fb8eb5c | 79 | { |
4d080b46 | 80 | bool retval = false; |
8fb8eb5c | 81 | |
4d080b46 TT |
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. */ | |
e1114590 TT |
86 | for (const auto &iter : qf) |
87 | { | |
245703b3 | 88 | retval = iter->has_symbols (this); |
e1114590 TT |
89 | if (retval) |
90 | break; | |
91 | } | |
8fb8eb5c | 92 | |
4d080b46 | 93 | if (debug_symfile) |
6cb06a8c TT |
94 | gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n", |
95 | objfile_debug_name (this), retval); | |
8fb8eb5c DE |
96 | |
97 | return retval; | |
98 | } | |
99 | ||
fc4d5ebf AB |
100 | /* See objfiles.h. */ |
101 | bool | |
102 | objfile::has_unexpanded_symtabs () | |
103 | { | |
104 | if (debug_symfile) | |
6cb06a8c TT |
105 | gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n", |
106 | objfile_debug_name (this)); | |
fc4d5ebf AB |
107 | |
108 | bool result = false; | |
245703b3 | 109 | for (const auto &iter : qf) |
fc4d5ebf AB |
110 | { |
111 | if (iter->has_unexpanded_symtabs (this)) | |
112 | { | |
113 | result = true; | |
114 | break; | |
115 | } | |
116 | } | |
117 | ||
118 | if (debug_symfile) | |
6cb06a8c TT |
119 | gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n", |
120 | objfile_debug_name (this), (result ? 1 : 0)); | |
fc4d5ebf AB |
121 | |
122 | return result; | |
123 | } | |
124 | ||
4d080b46 TT |
125 | struct symtab * |
126 | objfile::find_last_source_symtab () | |
8fb8eb5c | 127 | { |
4d080b46 | 128 | struct symtab *retval = nullptr; |
8fb8eb5c | 129 | |
4d080b46 | 130 | if (debug_symfile) |
6cb06a8c TT |
131 | gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n", |
132 | objfile_debug_name (this)); | |
8fb8eb5c | 133 | |
245703b3 | 134 | for (const auto &iter : qf) |
e1114590 TT |
135 | { |
136 | retval = iter->find_last_source_symtab (this); | |
137 | if (retval != nullptr) | |
138 | break; | |
139 | } | |
8fb8eb5c | 140 | |
4d080b46 | 141 | if (debug_symfile) |
6cb06a8c TT |
142 | gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n", |
143 | retval ? debug_symtab_name (retval) : "NULL"); | |
8fb8eb5c DE |
144 | |
145 | return retval; | |
146 | } | |
147 | ||
4d080b46 TT |
148 | void |
149 | objfile::forget_cached_source_info () | |
8fb8eb5c | 150 | { |
4d080b46 | 151 | if (debug_symfile) |
6cb06a8c TT |
152 | gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n", |
153 | objfile_debug_name (this)); | |
8fb8eb5c | 154 | |
21f6be77 | 155 | for (compunit_symtab *cu : compunits ()) |
f59be2ed | 156 | cu->forget_cached_source_info (); |
21f6be77 | 157 | |
245703b3 | 158 | for (const auto &iter : qf) |
e1114590 | 159 | iter->forget_cached_source_info (this); |
8fb8eb5c DE |
160 | } |
161 | ||
4d080b46 TT |
162 | bool |
163 | objfile::map_symtabs_matching_filename | |
164 | (const char *name, const char *real_path, | |
14bc53a8 | 165 | gdb::function_view<bool (symtab *)> callback) |
8fb8eb5c | 166 | { |
4d080b46 | 167 | if (debug_symfile) |
6cb06a8c TT |
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)); | |
8fb8eb5c | 174 | |
536a40f3 TT |
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 | ||
245703b3 | 205 | for (const auto &iter : qf) |
e1114590 | 206 | { |
536a40f3 TT |
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), | |
f214edce | 214 | SEARCH_ALL_DOMAINS)) |
536a40f3 TT |
215 | { |
216 | retval = false; | |
217 | break; | |
218 | } | |
e1114590 | 219 | } |
8fb8eb5c | 220 | |
4d080b46 | 221 | if (debug_symfile) |
6cb06a8c TT |
222 | gdb_printf (gdb_stdlog, |
223 | "qf->map_symtabs_matching_filename (...) = %d\n", | |
224 | retval); | |
8fb8eb5c | 225 | |
536a40f3 TT |
226 | /* We must re-invert the return value here to match the caller's |
227 | expectations. */ | |
228 | return !retval; | |
8fb8eb5c DE |
229 | } |
230 | ||
4d080b46 | 231 | struct compunit_symtab * |
e70d6457 | 232 | objfile::lookup_symbol (block_enum kind, const lookup_name_info &name, |
ccf41c24 | 233 | domain_search_flags domain) |
8fb8eb5c | 234 | { |
4d080b46 | 235 | struct compunit_symtab *retval = nullptr; |
8fb8eb5c | 236 | |
4d080b46 | 237 | if (debug_symfile) |
6cb06a8c TT |
238 | gdb_printf (gdb_stdlog, |
239 | "qf->lookup_symbol (%s, %d, \"%s\", %s)\n", | |
e70d6457 | 240 | objfile_debug_name (this), kind, name.c_str (), |
ccf41c24 | 241 | domain_name (domain).c_str ()); |
8fb8eb5c | 242 | |
84d865e3 TT |
243 | auto search_one_symtab = [&] (compunit_symtab *stab) |
244 | { | |
245 | struct symbol *sym, *with_opaque = NULL; | |
af39c5c8 | 246 | const struct blockvector *bv = stab->blockvector (); |
63d609de | 247 | const struct block *block = bv->block (kind); |
84d865e3 | 248 | |
e70d6457 | 249 | sym = block_find_symbol (block, name, domain, &with_opaque); |
84d865e3 TT |
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 | ||
b7a92724 | 255 | if (sym != nullptr) |
84d865e3 TT |
256 | { |
257 | retval = stab; | |
258 | /* Found it. */ | |
259 | return false; | |
260 | } | |
b7a92724 | 261 | if (with_opaque != nullptr) |
84d865e3 TT |
262 | retval = stab; |
263 | ||
264 | /* Keep looking through other psymtabs. */ | |
265 | return true; | |
266 | }; | |
267 | ||
245703b3 | 268 | for (const auto &iter : qf) |
e1114590 | 269 | { |
84d865e3 TT |
270 | if (!iter->expand_symtabs_matching (this, |
271 | nullptr, | |
e70d6457 | 272 | &name, |
84d865e3 TT |
273 | nullptr, |
274 | search_one_symtab, | |
275 | kind == GLOBAL_BLOCK | |
276 | ? SEARCH_GLOBAL_BLOCK | |
277 | : SEARCH_STATIC_BLOCK, | |
ccf41c24 | 278 | domain)) |
e1114590 TT |
279 | break; |
280 | } | |
8fb8eb5c | 281 | |
4d080b46 | 282 | if (debug_symfile) |
6cb06a8c TT |
283 | gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n", |
284 | retval | |
285 | ? debug_symtab_name (retval->primary_filetab ()) | |
286 | : "NULL"); | |
8fb8eb5c DE |
287 | |
288 | return retval; | |
289 | } | |
290 | ||
4d080b46 | 291 | void |
4829711b | 292 | objfile::print_stats (bool print_bcache) |
8fb8eb5c | 293 | { |
4d080b46 | 294 | if (debug_symfile) |
6cb06a8c TT |
295 | gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n", |
296 | objfile_debug_name (this), print_bcache); | |
8fb8eb5c | 297 | |
245703b3 | 298 | for (const auto &iter : qf) |
e1114590 | 299 | iter->print_stats (this, print_bcache); |
8fb8eb5c DE |
300 | } |
301 | ||
4d080b46 TT |
302 | void |
303 | objfile::dump () | |
8fb8eb5c | 304 | { |
4d080b46 | 305 | if (debug_symfile) |
6cb06a8c TT |
306 | gdb_printf (gdb_stdlog, "qf->dump (%s)\n", |
307 | objfile_debug_name (this)); | |
8fb8eb5c | 308 | |
e1114590 TT |
309 | for (const auto &iter : qf) |
310 | iter->dump (this); | |
8fb8eb5c DE |
311 | } |
312 | ||
4d080b46 TT |
313 | void |
314 | objfile::expand_symtabs_for_function (const char *func_name) | |
8fb8eb5c | 315 | { |
4d080b46 | 316 | if (debug_symfile) |
6cb06a8c TT |
317 | gdb_printf (gdb_stdlog, |
318 | "qf->expand_symtabs_for_function (%s, \"%s\")\n", | |
319 | objfile_debug_name (this), func_name); | |
8fb8eb5c | 320 | |
7089bd88 TT |
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 | ||
245703b3 | 324 | for (const auto &iter : qf) |
7089bd88 TT |
325 | iter->expand_symtabs_matching (this, |
326 | nullptr, | |
327 | &lookup_name, | |
328 | nullptr, | |
329 | nullptr, | |
330 | (SEARCH_GLOBAL_BLOCK | |
331 | | SEARCH_STATIC_BLOCK), | |
8068710e | 332 | SEARCH_FUNCTION_DOMAIN); |
8fb8eb5c DE |
333 | } |
334 | ||
4d080b46 TT |
335 | void |
336 | objfile::expand_all_symtabs () | |
8fb8eb5c | 337 | { |
4d080b46 | 338 | if (debug_symfile) |
6cb06a8c TT |
339 | gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n", |
340 | objfile_debug_name (this)); | |
8fb8eb5c | 341 | |
245703b3 | 342 | for (const auto &iter : qf) |
e1114590 | 343 | iter->expand_all_symtabs (this); |
8fb8eb5c DE |
344 | } |
345 | ||
4d080b46 TT |
346 | void |
347 | objfile::expand_symtabs_with_fullname (const char *fullname) | |
8fb8eb5c | 348 | { |
4d080b46 | 349 | if (debug_symfile) |
6cb06a8c TT |
350 | gdb_printf (gdb_stdlog, |
351 | "qf->expand_symtabs_with_fullname (%s, \"%s\")\n", | |
352 | objfile_debug_name (this), fullname); | |
8fb8eb5c | 353 | |
90160b57 TT |
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 | ||
245703b3 | 360 | for (const auto &iter : qf) |
90160b57 TT |
361 | iter->expand_symtabs_matching (this, |
362 | file_matcher, | |
363 | nullptr, | |
364 | nullptr, | |
365 | nullptr, | |
366 | (SEARCH_GLOBAL_BLOCK | |
367 | | SEARCH_STATIC_BLOCK), | |
f214edce | 368 | SEARCH_ALL_DOMAINS); |
8fb8eb5c DE |
369 | } |
370 | ||
df35e626 | 371 | bool |
4d080b46 | 372 | objfile::expand_symtabs_matching |
00aa53da | 373 | (expand_symtabs_file_matcher file_matcher, |
c1a66c06 | 374 | const lookup_name_info *lookup_name, |
00aa53da SM |
375 | expand_symtabs_symbol_matcher symbol_matcher, |
376 | expand_symtabs_expansion_listener expansion_notify, | |
03a8ea51 | 377 | block_search_flags search_flags, |
00105aa1 | 378 | domain_search_flags domain, |
00aa53da | 379 | expand_symtabs_lang_matcher lang_matcher) |
8fb8eb5c | 380 | { |
4f348ca8 TT |
381 | /* This invariant is documented in quick-functions.h. */ |
382 | gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr); | |
383 | ||
4d080b46 | 384 | if (debug_symfile) |
6cb06a8c TT |
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), | |
6c015214 | 391 | domain_name (domain).c_str ()); |
4d080b46 | 392 | |
245703b3 | 393 | for (const auto &iter : qf) |
df35e626 TT |
394 | if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name, |
395 | symbol_matcher, expansion_notify, | |
00105aa1 TV |
396 | search_flags, domain, |
397 | lang_matcher)) | |
df35e626 TT |
398 | return false; |
399 | return true; | |
8fb8eb5c DE |
400 | } |
401 | ||
4d080b46 | 402 | struct compunit_symtab * |
03b40f6f | 403 | objfile::find_pc_sect_compunit_symtab (bound_minimal_symbol msymbol, |
43f3e411 DE |
404 | CORE_ADDR pc, |
405 | struct obj_section *section, | |
406 | int warn_if_readin) | |
8fb8eb5c | 407 | { |
4d080b46 TT |
408 | struct compunit_symtab *retval = nullptr; |
409 | ||
410 | if (debug_symfile) | |
6cb06a8c TT |
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); | |
4d080b46 | 418 | |
245703b3 | 419 | for (const auto &iter : qf) |
e1114590 TT |
420 | { |
421 | retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section, | |
422 | warn_if_readin); | |
423 | if (retval != nullptr) | |
424 | break; | |
425 | } | |
4d080b46 TT |
426 | |
427 | if (debug_symfile) | |
6cb06a8c TT |
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"); | |
8fb8eb5c DE |
433 | |
434 | return retval; | |
435 | } | |
436 | ||
4d080b46 | 437 | void |
00aa53da | 438 | objfile::map_symbol_filenames (symbol_filename_listener fun, bool need_fullname) |
8fb8eb5c | 439 | { |
4d080b46 | 440 | if (debug_symfile) |
6cb06a8c TT |
441 | gdb_printf (gdb_stdlog, |
442 | "qf->map_symbol_filenames (%s, ..., %d)\n", | |
443 | objfile_debug_name (this), | |
444 | need_fullname); | |
8fb8eb5c | 445 | |
245703b3 | 446 | for (const auto &iter : qf) |
f4655dee | 447 | iter->map_symbol_filenames (this, fun, need_fullname); |
8fb8eb5c DE |
448 | } |
449 | ||
4ea870ef TT |
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 | ||
245703b3 | 458 | for (const auto &iter : qf) |
4ea870ef TT |
459 | iter->compute_main_name (this); |
460 | } | |
461 | ||
4d080b46 TT |
462 | struct compunit_symtab * |
463 | objfile::find_compunit_symtab_by_address (CORE_ADDR address) | |
71a3c369 | 464 | { |
4d080b46 | 465 | if (debug_symfile) |
6cb06a8c TT |
466 | gdb_printf (gdb_stdlog, |
467 | "qf->find_compunit_symtab_by_address (%s, %s)\n", | |
468 | objfile_debug_name (this), | |
469 | hex_string (address)); | |
71a3c369 TT |
470 | |
471 | struct compunit_symtab *result = NULL; | |
245703b3 | 472 | for (const auto &iter : qf) |
e1114590 TT |
473 | { |
474 | result = iter->find_compunit_symtab_by_address (this, address); | |
475 | if (result != nullptr) | |
476 | break; | |
477 | } | |
71a3c369 | 478 | |
4d080b46 | 479 | if (debug_symfile) |
6cb06a8c TT |
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"); | |
4d080b46 TT |
485 | |
486 | return result; | |
487 | } | |
488 | ||
489 | enum language | |
490 | objfile::lookup_global_symbol_language (const char *name, | |
6c015214 | 491 | domain_search_flags domain, |
4d080b46 TT |
492 | bool *symbol_found_p) |
493 | { | |
494 | enum language result = language_unknown; | |
e1114590 | 495 | *symbol_found_p = false; |
4d080b46 | 496 | |
245703b3 | 497 | for (const auto &iter : qf) |
e1114590 TT |
498 | { |
499 | result = iter->lookup_global_symbol_language (this, name, domain, | |
500 | symbol_found_p); | |
501 | if (*symbol_found_p) | |
502 | break; | |
503 | } | |
71a3c369 TT |
504 | |
505 | return result; | |
506 | } | |
507 | ||
6234ba17 AB |
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 | ||
27807da5 AB |
574 | /* See objfiles.h. */ |
575 | ||
576 | bool | |
577 | objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags) | |
578 | { | |
661d98a3 AB |
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 | ||
629bcc68 | 631 | ext_lang_missing_file_result ext_result |
661d98a3 AB |
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 ()); | |
27807da5 | 638 | |
661d98a3 AB |
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 | } | |
27807da5 | 653 | |
661d98a3 AB |
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 (); | |
27807da5 | 658 | } |
6234ba17 | 659 | |
661d98a3 | 660 | return has_dwarf2; |
27807da5 AB |
661 | } |
662 | ||
8fb8eb5c DE |
663 | \f |
664 | /* Debugging version of struct sym_probe_fns. */ | |
665 | ||
814cf43a | 666 | static const std::vector<std::unique_ptr<probe>> & |
8fb8eb5c DE |
667 | debug_sym_get_probes (struct objfile *objfile) |
668 | { | |
19ba03f4 | 669 | const struct debug_sym_fns_data *debug_data |
8c42777c | 670 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 671 | |
814cf43a | 672 | const std::vector<std::unique_ptr<probe>> &retval |
aaa63a31 | 673 | = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile); |
8fb8eb5c | 674 | |
6cb06a8c TT |
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 ())); | |
8fb8eb5c DE |
679 | |
680 | return retval; | |
681 | } | |
682 | ||
8fb8eb5c DE |
683 | static const struct sym_probe_fns debug_sym_probe_fns = |
684 | { | |
685 | debug_sym_get_probes, | |
8fb8eb5c DE |
686 | }; |
687 | \f | |
688 | /* Debugging version of struct sym_fns. */ | |
689 | ||
690 | static void | |
691 | debug_sym_new_init (struct objfile *objfile) | |
692 | { | |
19ba03f4 | 693 | const struct debug_sym_fns_data *debug_data |
8c42777c | 694 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 695 | |
6cb06a8c TT |
696 | gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n", |
697 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
698 | |
699 | debug_data->real_sf->sym_new_init (objfile); | |
700 | } | |
701 | ||
702 | static void | |
703 | debug_sym_init (struct objfile *objfile) | |
704 | { | |
19ba03f4 | 705 | const struct debug_sym_fns_data *debug_data |
8c42777c | 706 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 707 | |
6cb06a8c TT |
708 | gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n", |
709 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
710 | |
711 | debug_data->real_sf->sym_init (objfile); | |
712 | } | |
713 | ||
714 | static void | |
b15cc25c | 715 | debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags) |
8fb8eb5c | 716 | { |
19ba03f4 | 717 | const struct debug_sym_fns_data *debug_data |
8c42777c | 718 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 719 | |
6cb06a8c TT |
720 | gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n", |
721 | objfile_debug_name (objfile), (unsigned) symfile_flags); | |
8fb8eb5c DE |
722 | |
723 | debug_data->real_sf->sym_read (objfile, symfile_flags); | |
724 | } | |
725 | ||
8fb8eb5c DE |
726 | static void |
727 | debug_sym_finish (struct objfile *objfile) | |
728 | { | |
19ba03f4 | 729 | const struct debug_sym_fns_data *debug_data |
8c42777c | 730 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 731 | |
6cb06a8c TT |
732 | gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n", |
733 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
734 | |
735 | debug_data->real_sf->sym_finish (objfile); | |
736 | } | |
737 | ||
738 | static void | |
739 | debug_sym_offsets (struct objfile *objfile, | |
37e136b1 | 740 | const section_addr_info &info) |
8fb8eb5c | 741 | { |
19ba03f4 | 742 | const struct debug_sym_fns_data *debug_data |
8c42777c | 743 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 744 | |
6cb06a8c TT |
745 | gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n", |
746 | objfile_debug_name (objfile), | |
747 | host_address_to_string (&info)); | |
8fb8eb5c DE |
748 | |
749 | debug_data->real_sf->sym_offsets (objfile, info); | |
750 | } | |
751 | ||
62982abd | 752 | static symfile_segment_data_up |
8fb8eb5c DE |
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 | { | |
19ba03f4 | 764 | const struct debug_sym_fns_data *debug_data |
8c42777c | 765 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c | 766 | |
6cb06a8c TT |
767 | gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n", |
768 | objfile_debug_name (objfile)); | |
8fb8eb5c DE |
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 | { | |
19ba03f4 | 776 | const struct debug_sym_fns_data *debug_data |
8c42777c | 777 | = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c DE |
778 | bfd_byte *retval; |
779 | ||
780 | retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf); | |
781 | ||
6cb06a8c TT |
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)); | |
8fb8eb5c DE |
788 | |
789 | return retval; | |
790 | } | |
791 | ||
8fb8eb5c DE |
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. */ | |
8c42777c | 807 | debug_data = new struct debug_sym_fns_data; |
8fb8eb5c DE |
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); | |
8fb8eb5c DE |
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; | |
8fb8eb5c DE |
826 | |
827 | #undef COPY_SF_PTR | |
828 | ||
829 | debug_data->real_sf = real_sf; | |
8c42777c | 830 | symfile_debug_objfile_data_key.set (objfile, debug_data); |
8fb8eb5c DE |
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 | ||
8c42777c | 845 | debug_data = symfile_debug_objfile_data_key.get (objfile); |
8fb8eb5c DE |
846 | |
847 | objfile->sf = debug_data->real_sf; | |
8c42777c | 848 | symfile_debug_objfile_data_key.clear (objfile); |
8fb8eb5c DE |
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 | |
eb4c3f4a | 873 | set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c) |
8fb8eb5c | 874 | { |
94c93c35 | 875 | for (struct program_space *pspace : program_spaces) |
2030c079 | 876 | for (objfile *objfile : pspace->objfiles ()) |
99d89cde TT |
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 | } | |
8fb8eb5c DE |
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 | { | |
6cb06a8c | 895 | gdb_printf (file, _("Symfile debugging is %s.\n"), value); |
8fb8eb5c DE |
896 | } |
897 | ||
5fe70629 | 898 | INIT_GDB_FILE (symfile_debug) |
8fb8eb5c | 899 | { |
8fb8eb5c DE |
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 | } |