Don't include sys/personality.h in linux-low.cc anymore
[deliverable/binutils-gdb.git] / gdb / symfile-debug.c
CommitLineData
8fb8eb5c
DE
1/* Debug logging for the symbol file functions for the GNU debugger, GDB.
2
3666a048 3 Copyright (C) 2013-2021 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"
8fb8eb5c
DE
37
38/* We need to save a pointer to the real symbol functions.
39 Plus, the debug versions are malloc'd because we have to NULL out the
40 ones that are NULL in the real copy. */
41
42struct debug_sym_fns_data
43{
8c42777c
TT
44 const struct sym_fns *real_sf = nullptr;
45 struct sym_fns debug_sf {};
8fb8eb5c
DE
46};
47
48/* We need to record a pointer to the real set of functions for each
49 objfile. */
8c42777c
TT
50static const struct objfile_key<debug_sym_fns_data>
51 symfile_debug_objfile_data_key;
8fb8eb5c 52
491144b5
CB
53/* If true all calls to the symfile functions are logged. */
54static bool debug_symfile = false;
8fb8eb5c
DE
55
56/* Return non-zero if symfile debug logging is installed. */
57
58static int
59symfile_debug_installed (struct objfile *objfile)
60{
61 return (objfile->sf != NULL
8c42777c 62 && symfile_debug_objfile_data_key.get (objfile) != NULL);
8fb8eb5c
DE
63}
64
8fb8eb5c
DE
65/* Utility return the name to print for SYMTAB. */
66
67static const char *
68debug_symtab_name (struct symtab *symtab)
69{
70 return symtab_to_filename_for_display (symtab);
71}
72\f
8fb8eb5c 73
4d080b46
TT
74/* See objfiles.h. */
75
76bool
77objfile::has_partial_symbols ()
8fb8eb5c 78{
4d080b46 79 bool retval = false;
8fb8eb5c 80
4d080b46
TT
81 /* If we have not read psymbols, but we have a function capable of reading
82 them, then that is an indication that they are in fact available. Without
83 this function the symbols may have been already read in but they also may
84 not be present in this objfile. */
e1114590
TT
85 for (const auto &iter : qf)
86 {
87 if ((flags & OBJF_PSYMTABS_READ) == 0
88 && iter->can_lazily_read_symbols ())
89 retval = true;
90 else
91 retval = iter->has_symbols (this);
92 if (retval)
93 break;
94 }
8fb8eb5c 95
4d080b46
TT
96 if (debug_symfile)
97 fprintf_filtered (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
98 objfile_debug_name (this), retval);
8fb8eb5c
DE
99
100 return retval;
101}
102
4d080b46
TT
103struct symtab *
104objfile::find_last_source_symtab ()
8fb8eb5c 105{
4d080b46 106 struct symtab *retval = nullptr;
8fb8eb5c 107
4d080b46
TT
108 if (debug_symfile)
109 fprintf_filtered (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
110 objfile_debug_name (this));
8fb8eb5c 111
e1114590
TT
112 for (const auto &iter : qf)
113 {
114 retval = iter->find_last_source_symtab (this);
115 if (retval != nullptr)
116 break;
117 }
8fb8eb5c 118
4d080b46
TT
119 if (debug_symfile)
120 fprintf_filtered (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
121 retval ? debug_symtab_name (retval) : "NULL");
8fb8eb5c
DE
122
123 return retval;
124}
125
4d080b46
TT
126void
127objfile::forget_cached_source_info ()
8fb8eb5c 128{
4d080b46
TT
129 if (debug_symfile)
130 fprintf_filtered (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
131 objfile_debug_name (this));
8fb8eb5c 132
e1114590
TT
133 for (const auto &iter : qf)
134 iter->forget_cached_source_info (this);
8fb8eb5c
DE
135}
136
4d080b46
TT
137bool
138objfile::map_symtabs_matching_filename
139 (const char *name, const char *real_path,
14bc53a8 140 gdb::function_view<bool (symtab *)> callback)
8fb8eb5c 141{
4d080b46
TT
142 if (debug_symfile)
143 fprintf_filtered (gdb_stdlog,
144 "qf->map_symtabs_matching_filename (%s, \"%s\", "
145 "\"%s\", %s)\n",
146 objfile_debug_name (this), name,
147 real_path ? real_path : NULL,
148 host_address_to_string (&callback));
8fb8eb5c 149
536a40f3
TT
150 bool retval = true;
151 const char *name_basename = lbasename (name);
152
153 auto match_one_filename = [&] (const char *filename, bool basenames)
154 {
155 if (compare_filenames_for_search (filename, name))
156 return true;
157 if (basenames && FILENAME_CMP (name_basename, filename) == 0)
158 return true;
159 if (real_path != nullptr && IS_ABSOLUTE_PATH (filename)
160 && IS_ABSOLUTE_PATH (real_path))
161 return filename_cmp (filename, real_path) == 0;
162 return false;
163 };
164
165 compunit_symtab *last_made = this->compunit_symtabs;
166
167 auto on_expansion = [&] (compunit_symtab *symtab)
168 {
169 /* The callback to iterate_over_some_symtabs returns false to keep
170 going and true to continue, so we have to invert the result
171 here, for expand_symtabs_matching. */
172 bool result = !iterate_over_some_symtabs (name, real_path,
173 this->compunit_symtabs,
174 last_made,
175 callback);
176 last_made = this->compunit_symtabs;
177 return result;
178 };
179
e1114590
TT
180 for (const auto &iter : qf)
181 {
536a40f3
TT
182 if (!iter->expand_symtabs_matching (this,
183 match_one_filename,
184 nullptr,
185 nullptr,
186 on_expansion,
187 (SEARCH_GLOBAL_BLOCK
188 | SEARCH_STATIC_BLOCK),
189 UNDEF_DOMAIN,
190 ALL_DOMAIN))
191 {
192 retval = false;
193 break;
194 }
e1114590 195 }
8fb8eb5c 196
4d080b46
TT
197 if (debug_symfile)
198 fprintf_filtered (gdb_stdlog,
199 "qf->map_symtabs_matching_filename (...) = %d\n",
200 retval);
8fb8eb5c 201
536a40f3
TT
202 /* We must re-invert the return value here to match the caller's
203 expectations. */
204 return !retval;
8fb8eb5c
DE
205}
206
4d080b46
TT
207struct compunit_symtab *
208objfile::lookup_symbol (block_enum kind, const char *name, domain_enum domain)
8fb8eb5c 209{
4d080b46 210 struct compunit_symtab *retval = nullptr;
8fb8eb5c 211
4d080b46
TT
212 if (debug_symfile)
213 fprintf_filtered (gdb_stdlog,
214 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
215 objfile_debug_name (this), kind, name,
216 domain_name (domain));
8fb8eb5c 217
84d865e3
TT
218 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
219
220 auto search_one_symtab = [&] (compunit_symtab *stab)
221 {
222 struct symbol *sym, *with_opaque = NULL;
223 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
224 const struct block *block = BLOCKVECTOR_BLOCK (bv, kind);
225
226 sym = block_find_symbol (block, name, domain,
227 block_find_non_opaque_type_preferred,
228 &with_opaque);
229
230 /* Some caution must be observed with overloaded functions
231 and methods, since the index will not contain any overload
232 information (but NAME might contain it). */
233
234 if (sym != NULL
235 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
236 {
237 retval = stab;
238 /* Found it. */
239 return false;
240 }
241 if (with_opaque != NULL
242 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
243 retval = stab;
244
245 /* Keep looking through other psymtabs. */
246 return true;
247 };
248
e1114590
TT
249 for (const auto &iter : qf)
250 {
84d865e3
TT
251 if (!iter->expand_symtabs_matching (this,
252 nullptr,
253 &lookup_name,
254 nullptr,
255 search_one_symtab,
256 kind == GLOBAL_BLOCK
257 ? SEARCH_GLOBAL_BLOCK
258 : SEARCH_STATIC_BLOCK,
259 domain,
260 ALL_DOMAIN))
e1114590
TT
261 break;
262 }
8fb8eb5c 263
4d080b46
TT
264 if (debug_symfile)
265 fprintf_filtered (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
266 retval
267 ? debug_symtab_name (compunit_primary_filetab (retval))
268 : "NULL");
8fb8eb5c
DE
269
270 return retval;
271}
272
4d080b46 273void
4829711b 274objfile::print_stats (bool print_bcache)
8fb8eb5c 275{
4d080b46 276 if (debug_symfile)
4829711b
TT
277 fprintf_filtered (gdb_stdlog, "qf->print_stats (%s, %d)\n",
278 objfile_debug_name (this), print_bcache);
8fb8eb5c 279
e1114590
TT
280 for (const auto &iter : qf)
281 iter->print_stats (this, print_bcache);
8fb8eb5c
DE
282}
283
4d080b46
TT
284void
285objfile::dump ()
8fb8eb5c 286{
4d080b46
TT
287 if (debug_symfile)
288 fprintf_filtered (gdb_stdlog, "qf->dump (%s)\n",
289 objfile_debug_name (this));
8fb8eb5c 290
e1114590
TT
291 for (const auto &iter : qf)
292 iter->dump (this);
8fb8eb5c
DE
293}
294
4d080b46
TT
295void
296objfile::expand_symtabs_for_function (const char *func_name)
8fb8eb5c 297{
4d080b46
TT
298 if (debug_symfile)
299 fprintf_filtered (gdb_stdlog,
300 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
301 objfile_debug_name (this), func_name);
8fb8eb5c 302
7089bd88
TT
303 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
304 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
305
e1114590 306 for (const auto &iter : qf)
7089bd88
TT
307 iter->expand_symtabs_matching (this,
308 nullptr,
309 &lookup_name,
310 nullptr,
311 nullptr,
312 (SEARCH_GLOBAL_BLOCK
313 | SEARCH_STATIC_BLOCK),
314 VAR_DOMAIN,
315 ALL_DOMAIN);
8fb8eb5c
DE
316}
317
4d080b46
TT
318void
319objfile::expand_all_symtabs ()
8fb8eb5c 320{
4d080b46
TT
321 if (debug_symfile)
322 fprintf_filtered (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
323 objfile_debug_name (this));
8fb8eb5c 324
e1114590
TT
325 for (const auto &iter : qf)
326 iter->expand_all_symtabs (this);
8fb8eb5c
DE
327}
328
4d080b46
TT
329void
330objfile::expand_symtabs_with_fullname (const char *fullname)
8fb8eb5c 331{
4d080b46
TT
332 if (debug_symfile)
333 fprintf_filtered (gdb_stdlog,
334 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
335 objfile_debug_name (this), fullname);
8fb8eb5c 336
90160b57
TT
337 const char *basename = lbasename (fullname);
338 auto file_matcher = [&] (const char *filename, bool basenames)
339 {
340 return filename_cmp (basenames ? basename : fullname, filename) == 0;
341 };
342
e1114590 343 for (const auto &iter : qf)
90160b57
TT
344 iter->expand_symtabs_matching (this,
345 file_matcher,
346 nullptr,
347 nullptr,
348 nullptr,
349 (SEARCH_GLOBAL_BLOCK
350 | SEARCH_STATIC_BLOCK),
351 UNDEF_DOMAIN,
352 ALL_DOMAIN);
8fb8eb5c
DE
353}
354
4d080b46 355void
0b7b2c2a 356objfile::expand_matching_symbols
4d080b46 357 (const lookup_name_info &name, domain_enum domain,
199b4314 358 int global,
199b4314 359 symbol_compare_ftype *ordered_compare)
8fb8eb5c 360{
4d080b46
TT
361 if (debug_symfile)
362 fprintf_filtered (gdb_stdlog,
0b7b2c2a 363 "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
4d080b46
TT
364 objfile_debug_name (this),
365 domain_name (domain), global,
366 host_address_to_string (ordered_compare));
8fb8eb5c 367
e1114590 368 for (const auto &iter : qf)
0b7b2c2a
TT
369 iter->expand_matching_symbols (this, name, domain, global,
370 ordered_compare);
8fb8eb5c
DE
371}
372
df35e626 373bool
4d080b46
TT
374objfile::expand_symtabs_matching
375 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 376 const lookup_name_info *lookup_name,
14bc53a8
PA
377 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
378 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 379 block_search_flags search_flags,
3bfa51a7 380 domain_enum domain,
14bc53a8 381 enum search_domain kind)
8fb8eb5c 382{
4d080b46
TT
383 if (debug_symfile)
384 fprintf_filtered (gdb_stdlog,
385 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
386 objfile_debug_name (this),
387 host_address_to_string (&file_matcher),
388 host_address_to_string (&symbol_matcher),
389 host_address_to_string (&expansion_notify),
390 search_domain_name (kind));
391
e1114590 392 for (const auto &iter : qf)
df35e626
TT
393 if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
394 symbol_matcher, expansion_notify,
3bfa51a7 395 search_flags, domain, kind))
df35e626
TT
396 return false;
397 return true;
8fb8eb5c
DE
398}
399
4d080b46
TT
400struct compunit_symtab *
401objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol,
43f3e411
DE
402 CORE_ADDR pc,
403 struct obj_section *section,
404 int warn_if_readin)
8fb8eb5c 405{
4d080b46
TT
406 struct compunit_symtab *retval = nullptr;
407
408 if (debug_symfile)
409 fprintf_filtered (gdb_stdlog,
410 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
411 objfile_debug_name (this),
412 host_address_to_string (msymbol.minsym),
413 hex_string (pc),
414 host_address_to_string (section),
415 warn_if_readin);
416
e1114590
TT
417 for (const auto &iter : qf)
418 {
419 retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
420 warn_if_readin);
421 if (retval != nullptr)
422 break;
423 }
4d080b46
TT
424
425 if (debug_symfile)
426 fprintf_filtered (gdb_stdlog,
427 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
428 retval
429 ? debug_symtab_name (compunit_primary_filetab (retval))
430 : "NULL");
8fb8eb5c
DE
431
432 return retval;
433}
434
4d080b46 435void
f4655dee
TT
436objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
437 bool need_fullname)
8fb8eb5c 438{
4d080b46
TT
439 if (debug_symfile)
440 fprintf_filtered (gdb_stdlog,
f4655dee 441 "qf->map_symbol_filenames (%s, ..., %d)\n",
4d080b46 442 objfile_debug_name (this),
4d080b46 443 need_fullname);
8fb8eb5c 444
e1114590 445 for (const auto &iter : qf)
f4655dee 446 iter->map_symbol_filenames (this, fun, need_fullname);
8fb8eb5c
DE
447}
448
4d080b46
TT
449struct compunit_symtab *
450objfile::find_compunit_symtab_by_address (CORE_ADDR address)
71a3c369 451{
4d080b46
TT
452 if (debug_symfile)
453 fprintf_filtered (gdb_stdlog,
454 "qf->find_compunit_symtab_by_address (%s, %s)\n",
455 objfile_debug_name (this),
456 hex_string (address));
71a3c369
TT
457
458 struct compunit_symtab *result = NULL;
e1114590
TT
459 for (const auto &iter : qf)
460 {
461 result = iter->find_compunit_symtab_by_address (this, address);
462 if (result != nullptr)
463 break;
464 }
71a3c369 465
4d080b46
TT
466 if (debug_symfile)
467 fprintf_filtered (gdb_stdlog,
468 "qf->find_compunit_symtab_by_address (...) = %s\n",
469 result
470 ? debug_symtab_name (compunit_primary_filetab (result))
471 : "NULL");
472
473 return result;
474}
475
476enum language
477objfile::lookup_global_symbol_language (const char *name,
478 domain_enum domain,
479 bool *symbol_found_p)
480{
481 enum language result = language_unknown;
e1114590 482 *symbol_found_p = false;
4d080b46 483
e1114590
TT
484 for (const auto &iter : qf)
485 {
486 result = iter->lookup_global_symbol_language (this, name, domain,
487 symbol_found_p);
488 if (*symbol_found_p)
489 break;
490 }
71a3c369
TT
491
492 return result;
493}
494
d1eef86d
TT
495void
496objfile::require_partial_symbols (bool verbose)
497{
498 if ((flags & OBJF_PSYMTABS_READ) == 0)
499 {
500 flags |= OBJF_PSYMTABS_READ;
501
e1114590
TT
502 bool printed = false;
503 for (const auto &iter : qf)
d1eef86d 504 {
e1114590
TT
505 if (iter->can_lazily_read_symbols ())
506 {
507 if (verbose && !printed)
508 {
509 printf_filtered (_("Reading symbols from %s...\n"),
510 objfile_name (this));
511 printed = true;
512 }
513 iter->read_partial_symbols (this);
514 }
d1eef86d 515 }
e1114590
TT
516 if (printed && !objfile_has_symbols (this))
517 printf_filtered (_("(No debugging symbols found in %s)\n"),
518 objfile_name (this));
d1eef86d
TT
519 }
520}
521
8fb8eb5c
DE
522\f
523/* Debugging version of struct sym_probe_fns. */
524
814cf43a 525static const std::vector<std::unique_ptr<probe>> &
8fb8eb5c
DE
526debug_sym_get_probes (struct objfile *objfile)
527{
19ba03f4 528 const struct debug_sym_fns_data *debug_data
8c42777c 529 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c 530
814cf43a 531 const std::vector<std::unique_ptr<probe>> &retval
aaa63a31 532 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
8fb8eb5c
DE
533
534 fprintf_filtered (gdb_stdlog,
535 "probes->sym_get_probes (%s) = %s\n",
cc485e62 536 objfile_debug_name (objfile),
aaa63a31 537 host_address_to_string (retval.data ()));
8fb8eb5c
DE
538
539 return retval;
540}
541
8fb8eb5c
DE
542static const struct sym_probe_fns debug_sym_probe_fns =
543{
544 debug_sym_get_probes,
8fb8eb5c
DE
545};
546\f
547/* Debugging version of struct sym_fns. */
548
549static void
550debug_sym_new_init (struct objfile *objfile)
551{
19ba03f4 552 const struct debug_sym_fns_data *debug_data
8c42777c 553 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
554
555 fprintf_filtered (gdb_stdlog, "sf->sym_new_init (%s)\n",
cc485e62 556 objfile_debug_name (objfile));
8fb8eb5c
DE
557
558 debug_data->real_sf->sym_new_init (objfile);
559}
560
561static void
562debug_sym_init (struct objfile *objfile)
563{
19ba03f4 564 const struct debug_sym_fns_data *debug_data
8c42777c 565 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
566
567 fprintf_filtered (gdb_stdlog, "sf->sym_init (%s)\n",
cc485e62 568 objfile_debug_name (objfile));
8fb8eb5c
DE
569
570 debug_data->real_sf->sym_init (objfile);
571}
572
573static void
b15cc25c 574debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
8fb8eb5c 575{
19ba03f4 576 const struct debug_sym_fns_data *debug_data
8c42777c 577 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
578
579 fprintf_filtered (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
b15cc25c 580 objfile_debug_name (objfile), (unsigned) symfile_flags);
8fb8eb5c
DE
581
582 debug_data->real_sf->sym_read (objfile, symfile_flags);
583}
584
8fb8eb5c
DE
585static void
586debug_sym_finish (struct objfile *objfile)
587{
19ba03f4 588 const struct debug_sym_fns_data *debug_data
8c42777c 589 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
590
591 fprintf_filtered (gdb_stdlog, "sf->sym_finish (%s)\n",
cc485e62 592 objfile_debug_name (objfile));
8fb8eb5c
DE
593
594 debug_data->real_sf->sym_finish (objfile);
595}
596
597static void
598debug_sym_offsets (struct objfile *objfile,
37e136b1 599 const section_addr_info &info)
8fb8eb5c 600{
19ba03f4 601 const struct debug_sym_fns_data *debug_data
8c42777c 602 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
603
604 fprintf_filtered (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
cc485e62 605 objfile_debug_name (objfile),
37e136b1 606 host_address_to_string (&info));
8fb8eb5c
DE
607
608 debug_data->real_sf->sym_offsets (objfile, info);
609}
610
62982abd 611static symfile_segment_data_up
8fb8eb5c
DE
612debug_sym_segments (bfd *abfd)
613{
614 /* This API function is annoying, it doesn't take a "this" pointer.
615 Fortunately it is only used in one place where we (re-)lookup the
616 sym_fns table to use. Thus we will never be called. */
617 gdb_assert_not_reached ("debug_sym_segments called");
618}
619
620static void
621debug_sym_read_linetable (struct objfile *objfile)
622{
19ba03f4 623 const struct debug_sym_fns_data *debug_data
8c42777c 624 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
625
626 fprintf_filtered (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
cc485e62 627 objfile_debug_name (objfile));
8fb8eb5c
DE
628
629 debug_data->real_sf->sym_read_linetable (objfile);
630}
631
632static bfd_byte *
633debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
634{
19ba03f4 635 const struct debug_sym_fns_data *debug_data
8c42777c 636 = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
637 bfd_byte *retval;
638
639 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
640
641 fprintf_filtered (gdb_stdlog,
642 "sf->sym_relocate (%s, %s, %s) = %s\n",
cc485e62 643 objfile_debug_name (objfile),
8fb8eb5c
DE
644 host_address_to_string (sectp),
645 host_address_to_string (buf),
646 host_address_to_string (retval));
647
648 return retval;
649}
650
651/* Template of debugging version of struct sym_fns.
652 A copy is made, with sym_flavour updated, and a pointer to the real table
653 installed in real_sf, and then a pointer to the copy is installed in the
654 objfile. */
655
656static const struct sym_fns debug_sym_fns =
657{
658 debug_sym_new_init,
659 debug_sym_init,
660 debug_sym_read,
8fb8eb5c
DE
661 debug_sym_finish,
662 debug_sym_offsets,
663 debug_sym_segments,
664 debug_sym_read_linetable,
665 debug_sym_relocate,
666 &debug_sym_probe_fns,
8fb8eb5c
DE
667};
668\f
8fb8eb5c
DE
669/* Install the debugging versions of the symfile functions for OBJFILE.
670 Do not call this if the debug versions are already installed. */
671
672static void
673install_symfile_debug_logging (struct objfile *objfile)
674{
675 const struct sym_fns *real_sf;
676 struct debug_sym_fns_data *debug_data;
677
678 /* The debug versions should not already be installed. */
679 gdb_assert (!symfile_debug_installed (objfile));
680
681 real_sf = objfile->sf;
682
683 /* Alas we have to preserve NULL entries in REAL_SF. */
8c42777c 684 debug_data = new struct debug_sym_fns_data;
8fb8eb5c
DE
685
686#define COPY_SF_PTR(from, to, name, func) \
687 do { \
688 if ((from)->name) \
689 (to)->debug_sf.name = func; \
690 } while (0)
691
692 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
693 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
694 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
8fb8eb5c
DE
695 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
696 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
697 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
698 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
699 debug_sym_read_linetable);
700 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
701 if (real_sf->sym_probe_fns)
702 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
8fb8eb5c
DE
703
704#undef COPY_SF_PTR
705
706 debug_data->real_sf = real_sf;
8c42777c 707 symfile_debug_objfile_data_key.set (objfile, debug_data);
8fb8eb5c
DE
708 objfile->sf = &debug_data->debug_sf;
709}
710
711/* Uninstall the debugging versions of the symfile functions for OBJFILE.
712 Do not call this if the debug versions are not installed. */
713
714static void
715uninstall_symfile_debug_logging (struct objfile *objfile)
716{
717 struct debug_sym_fns_data *debug_data;
718
719 /* The debug versions should be currently installed. */
720 gdb_assert (symfile_debug_installed (objfile));
721
8c42777c 722 debug_data = symfile_debug_objfile_data_key.get (objfile);
8fb8eb5c
DE
723
724 objfile->sf = debug_data->real_sf;
8c42777c 725 symfile_debug_objfile_data_key.clear (objfile);
8fb8eb5c
DE
726}
727
728/* Call this function to set OBJFILE->SF.
729 Do not set OBJFILE->SF directly. */
730
731void
732objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
733{
734 if (symfile_debug_installed (objfile))
735 {
736 gdb_assert (debug_symfile);
737 /* Remove the current one, and reinstall a new one later. */
738 uninstall_symfile_debug_logging (objfile);
739 }
740
741 /* Assume debug logging is disabled. */
742 objfile->sf = sf;
743
744 /* Turn debug logging on if enabled. */
745 if (debug_symfile)
746 install_symfile_debug_logging (objfile);
747}
748
749static void
eb4c3f4a 750set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
8fb8eb5c 751{
94c93c35 752 for (struct program_space *pspace : program_spaces)
2030c079 753 for (objfile *objfile : pspace->objfiles ())
99d89cde
TT
754 {
755 if (debug_symfile)
756 {
757 if (!symfile_debug_installed (objfile))
758 install_symfile_debug_logging (objfile);
759 }
760 else
761 {
762 if (symfile_debug_installed (objfile))
763 uninstall_symfile_debug_logging (objfile);
764 }
765 }
8fb8eb5c
DE
766}
767
768static void
769show_debug_symfile (struct ui_file *file, int from_tty,
770 struct cmd_list_element *c, const char *value)
771{
772 fprintf_filtered (file, _("Symfile debugging is %s.\n"), value);
773}
774
6c265988 775void _initialize_symfile_debug ();
8fb8eb5c 776void
6c265988 777_initialize_symfile_debug ()
8fb8eb5c 778{
8fb8eb5c
DE
779 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
780Set debugging of the symfile functions."), _("\
781Show debugging of the symfile functions."), _("\
782When enabled, all calls to the symfile functions are logged."),
783 set_debug_symfile, show_debug_symfile,
784 &setdebuglist, &showdebuglist);
785
786 /* Note: We don't need a new-objfile observer because debug logging
787 will be installed when objfile init'n calls objfile_set_sym_fns. */
788}
This page took 0.964342 seconds and 4 git commands to generate.