]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/symtab.c
701aa5d0007042486f283b8e366265bfdc24288a
[thirdparty/binutils-gdb.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "dwarf2/call-site.h"
21 #include "exceptions.h"
22 #include "symtab.h"
23 #include "event-top.h"
24 #include "gdbtypes.h"
25 #include "gdbcore.h"
26 #include "frame.h"
27 #include "target.h"
28 #include "value.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdbsupport/gdb_regex.h"
32 #include "expression.h"
33 #include "language.h"
34 #include "demangle.h"
35 #include "inferior.h"
36 #include "source.h"
37 #include "filenames.h"
38 #include "objc-lang.h"
39 #include "d-lang.h"
40 #include "ada-lang.h"
41 #include "go-lang.h"
42 #include "p-lang.h"
43 #include "addrmap.h"
44 #include "cli/cli-utils.h"
45 #include "cli/cli-style.h"
46 #include "cli/cli-cmds.h"
47 #include "fnmatch.h"
48 #include "hashtab.h"
49 #include "typeprint.h"
50 #include "exceptions.h"
51
52 #include "gdbsupport/gdb_obstack.h"
53 #include "block.h"
54 #include "dictionary.h"
55
56 #include <sys/types.h>
57 #include <fcntl.h>
58 #include <sys/stat.h>
59 #include "cp-abi.h"
60 #include "cp-support.h"
61 #include "observable.h"
62 #include "macrotab.h"
63 #include "macroscope.h"
64
65 #include "parser-defs.h"
66 #include "completer.h"
67 #include "progspace-and-thread.h"
68 #include <optional>
69 #include "filename-seen-cache.h"
70 #include "arch-utils.h"
71 #include <algorithm>
72 #include <string_view>
73 #include "gdbsupport/pathstuff.h"
74 #include "gdbsupport/common-utils.h"
75 #include <optional>
76 #include "gdbsupport/unordered_set.h"
77
78 /* Forward declarations for local functions. */
79
80 static void rbreak_command (const char *, int);
81
82 static int find_line_common (const linetable *, int, int *, int);
83
84 static struct block_symbol
85 lookup_symbol_aux (const char *name,
86 symbol_name_match_type match_type,
87 const struct block *block,
88 const domain_search_flags domain,
89 enum language language,
90 struct field_of_this_result *);
91
92 static
93 struct block_symbol lookup_local_symbol (const char *name,
94 symbol_name_match_type match_type,
95 const struct block *block,
96 const domain_search_flags domain,
97 const struct language_defn *langdef);
98
99 static struct block_symbol
100 lookup_symbol_in_objfile (struct objfile *objfile,
101 enum block_enum block_index,
102 const char *name,
103 const domain_search_flags domain);
104
105 static void set_main_name (program_space *pspace, const char *name,
106 language lang);
107
108 /* Type of the data stored on the program space. */
109
110 struct main_info
111 {
112 /* Name of "main". */
113
114 std::string name_of_main;
115
116 /* Language of "main". */
117
118 enum language language_of_main = language_unknown;
119 };
120
121 /* Program space key for finding name and language of "main". */
122
123 static const registry<program_space>::key<main_info> main_progspace_key;
124
125 /* The default symbol cache size.
126 There is no extra cpu cost for large N (except when flushing the cache,
127 which is rare). The value here is just a first attempt. A better default
128 value may be higher or lower. A prime number can make up for a bad hash
129 computation, so that's why the number is what it is. */
130 #define DEFAULT_SYMBOL_CACHE_SIZE 1021
131
132 /* The maximum symbol cache size.
133 There's no method to the decision of what value to use here, other than
134 there's no point in allowing a user typo to make gdb consume all memory. */
135 #define MAX_SYMBOL_CACHE_SIZE (1024*1024)
136
137 /* symbol_cache_lookup returns this if a previous lookup failed to find the
138 symbol in any objfile. */
139 #define SYMBOL_LOOKUP_FAILED \
140 ((struct block_symbol) {(struct symbol *) 1, NULL})
141 #define SYMBOL_LOOKUP_FAILED_P(SIB) (SIB.symbol == (struct symbol *) 1)
142
143 /* Recording lookups that don't find the symbol is just as important, if not
144 more so, than recording found symbols. */
145
146 enum symbol_cache_slot_state
147 {
148 SYMBOL_SLOT_UNUSED,
149 SYMBOL_SLOT_NOT_FOUND,
150 SYMBOL_SLOT_FOUND
151 };
152
153 struct symbol_cache_slot
154 {
155 enum symbol_cache_slot_state state;
156
157 /* The objfile that was current when the symbol was looked up.
158 This is only needed for global blocks, but for simplicity's sake
159 we allocate the space for both. If data shows the extra space used
160 for static blocks is a problem, we can split things up then.
161
162 Global blocks need cache lookup to include the objfile context because
163 we need to account for gdbarch_iterate_over_objfiles_in_search_order
164 which can traverse objfiles in, effectively, any order, depending on
165 the current objfile, thus affecting which symbol is found. Normally,
166 only the current objfile is searched first, and then the rest are
167 searched in recorded order; but putting cache lookup inside
168 gdbarch_iterate_over_objfiles_in_search_order would be awkward.
169 Instead we just make the current objfile part of the context of
170 cache lookup. This means we can record the same symbol multiple times,
171 each with a different "current objfile" that was in effect when the
172 lookup was saved in the cache, but cache space is pretty cheap. */
173 const struct objfile *objfile_context;
174
175 /* The domain that was searched for initially. This must exactly
176 match. */
177 domain_search_flags domain;
178
179 union
180 {
181 struct block_symbol found;
182 char *name;
183 } value;
184 };
185
186 /* Clear out SLOT. */
187
188 static void
189 symbol_cache_clear_slot (struct symbol_cache_slot *slot)
190 {
191 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
192 xfree (slot->value.name);
193 slot->state = SYMBOL_SLOT_UNUSED;
194 }
195
196 /* Symbols don't specify global vs static block.
197 So keep them in separate caches. */
198
199 struct block_symbol_cache
200 {
201 unsigned int hits;
202 unsigned int misses;
203 unsigned int collisions;
204
205 /* SYMBOLS is a variable length array of this size.
206 One can imagine that in general one cache (global/static) should be a
207 fraction of the size of the other, but there's no data at the moment
208 on which to decide. */
209 unsigned int size;
210
211 struct symbol_cache_slot symbols[1];
212 };
213
214 /* Clear all slots of BSC and free BSC. */
215
216 static void
217 destroy_block_symbol_cache (struct block_symbol_cache *bsc)
218 {
219 if (bsc != nullptr)
220 {
221 for (unsigned int i = 0; i < bsc->size; i++)
222 symbol_cache_clear_slot (&bsc->symbols[i]);
223 xfree (bsc);
224 }
225 }
226
227 /* The symbol cache.
228
229 Searching for symbols in the static and global blocks over multiple objfiles
230 again and again can be slow, as can searching very big objfiles. This is a
231 simple cache to improve symbol lookup performance, which is critical to
232 overall gdb performance.
233
234 Symbols are hashed on the name, its domain, and block.
235 They are also hashed on their objfile for objfile-specific lookups. */
236
237 struct symbol_cache
238 {
239 symbol_cache () = default;
240
241 ~symbol_cache ()
242 {
243 destroy_block_symbol_cache (global_symbols);
244 destroy_block_symbol_cache (static_symbols);
245 }
246
247 struct block_symbol_cache *global_symbols = nullptr;
248 struct block_symbol_cache *static_symbols = nullptr;
249 };
250
251 /* Program space key for finding its symbol cache. */
252
253 static const registry<program_space>::key<symbol_cache> symbol_cache_key;
254
255 /* When non-zero, print debugging messages related to symtab creation. */
256 unsigned int symtab_create_debug = 0;
257
258 /* When non-zero, print debugging messages related to symbol lookup. */
259 unsigned int symbol_lookup_debug = 0;
260
261 /* The size of the cache is staged here. */
262 static unsigned int new_symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
263
264 /* The current value of the symbol cache size.
265 This is saved so that if the user enters a value too big we can restore
266 the original value from here. */
267 static unsigned int symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
268
269 /* True if a file may be known by two different basenames.
270 This is the uncommon case, and significantly slows down gdb.
271 Default set to "off" to not slow down the common case. */
272 bool basenames_may_differ = false;
273
274 /* Allow the user to configure the debugger behavior with respect
275 to multiple-choice menus when more than one symbol matches during
276 a symbol lookup. */
277
278 const char multiple_symbols_ask[] = "ask";
279 const char multiple_symbols_all[] = "all";
280 const char multiple_symbols_cancel[] = "cancel";
281 static const char *const multiple_symbols_modes[] =
282 {
283 multiple_symbols_ask,
284 multiple_symbols_all,
285 multiple_symbols_cancel,
286 NULL
287 };
288 static const char *multiple_symbols_mode = multiple_symbols_all;
289
290 /* When TRUE, ignore the prologue-end flag in linetable_entry when searching
291 for the SAL past a function prologue. */
292 static bool ignore_prologue_end_flag = false;
293
294 /* Read-only accessor to AUTO_SELECT_MODE. */
295
296 const char *
297 multiple_symbols_select_mode (void)
298 {
299 return multiple_symbols_mode;
300 }
301
302 /* Return the name of a domain_enum. */
303
304 const char *
305 domain_name (domain_enum e)
306 {
307 switch (e)
308 {
309 #define SYM_DOMAIN(X) \
310 case X ## _DOMAIN: return #X "_DOMAIN";
311 #include "sym-domains.def"
312 #undef SYM_DOMAIN
313 default: gdb_assert_not_reached ("bad domain_enum");
314 }
315 }
316
317 /* See symtab.h. */
318
319 std::string
320 domain_name (domain_search_flags flags)
321 {
322 static constexpr domain_search_flags::string_mapping mapping[] = {
323 #define SYM_DOMAIN(X) \
324 MAP_ENUM_FLAG (SEARCH_ ## X ## _DOMAIN),
325 #include "sym-domains.def"
326 #undef SYM_DOMAIN
327 };
328
329 return flags.to_string (mapping);
330 }
331
332 /* See symtab.h. */
333
334 domain_search_flags
335 from_scripting_domain (int val)
336 {
337 if ((val & SCRIPTING_SEARCH_FLAG) == 0)
338 {
339 /* VAL should be one of the domain constants. Verify this and
340 convert it to a search constant. */
341 switch (val)
342 {
343 #define SYM_DOMAIN(X) \
344 case X ## _DOMAIN: break;
345 #include "sym-domains.def"
346 #undef SYM_DOMAIN
347 default:
348 error (_("unrecognized domain constant"));
349 }
350 domain_search_flags result = to_search_flags ((domain_enum) val);
351 if (val == VAR_DOMAIN)
352 {
353 /* This matches the historical practice. */
354 result |= SEARCH_TYPE_DOMAIN | SEARCH_FUNCTION_DOMAIN;
355 }
356 return result;
357 }
358 else
359 {
360 /* VAL is several search constants or'd together. Verify
361 this. */
362 val &= ~SCRIPTING_SEARCH_FLAG;
363 int check = val;
364 #define SYM_DOMAIN(X) \
365 check &= ~ (int) SEARCH_ ## X ## _DOMAIN;
366 #include "sym-domains.def"
367 #undef SYM_DOMAIN
368 if (check != 0)
369 error (_("unrecognized domain constant"));
370 return (domain_search_flag) val;
371 }
372 }
373
374 /* See symtab.h. */
375
376 struct symbol *
377 search_symbol_list (const char *name, int num, struct symbol **syms)
378 {
379 for (int i = 0; i < num; ++i)
380 {
381 if (strcmp (name, syms[i]->natural_name ()) == 0)
382 return syms[i];
383 }
384 return nullptr;
385 }
386
387 /* See symtab.h. */
388
389 CORE_ADDR
390 linetable_entry::pc (const struct objfile *objfile) const
391 {
392 return CORE_ADDR (m_pc) + objfile->text_section_offset ();
393 }
394
395 /* See symtab.h. */
396
397 call_site *
398 compunit_symtab::find_call_site (CORE_ADDR pc) const
399 {
400 if (m_call_site_htab == nullptr)
401 return nullptr;
402
403 CORE_ADDR delta = this->objfile ()->text_section_offset ();
404
405 if (auto it = m_call_site_htab->find (static_cast<unrelocated_addr> (pc - delta));
406 it != m_call_site_htab->end ())
407 return *it;
408
409 /* See if the arch knows another PC we should try. On some
410 platforms, GCC emits a DWARF call site that is offset from the
411 actual return location. */
412 struct gdbarch *arch = objfile ()->arch ();
413 CORE_ADDR new_pc = gdbarch_update_call_site_pc (arch, pc);
414
415 if (pc == new_pc)
416 return nullptr;
417
418 if (auto it = m_call_site_htab->find (static_cast<unrelocated_addr> (new_pc - delta));
419 it != m_call_site_htab->end ())
420 return *it;
421
422 return nullptr;
423 }
424
425 /* See symtab.h. */
426
427 void
428 compunit_symtab::set_blockvector
429 (std::unique_ptr<struct blockvector> blockvector)
430 {
431 gdb_assert (m_blockvector == nullptr);
432 m_blockvector = std::move (blockvector);
433 }
434
435 /* See symtab.h. */
436
437 void
438 compunit_symtab::set_call_site_htab (call_site_htab_t &&call_site_htab)
439 {
440 gdb_assert (m_call_site_htab == nullptr);
441 if (!call_site_htab.empty ())
442 m_call_site_htab
443 = std::make_unique<call_site_htab_t> (std::move (call_site_htab));
444 }
445
446 /* See symtab.h. */
447
448 void
449 compunit_symtab::set_primary_filetab (symtab *primary_filetab)
450 {
451 symtab *prev_filetab = nullptr;
452
453 /* Move PRIMARY_FILETAB to the head of the filetab list. */
454 for (symtab *filetab : this->filetabs ())
455 {
456 if (filetab == primary_filetab)
457 {
458 if (prev_filetab != nullptr)
459 {
460 prev_filetab->next = primary_filetab->next;
461 primary_filetab->next = m_filetabs;
462 m_filetabs = primary_filetab;
463 }
464
465 break;
466 }
467
468 prev_filetab = filetab;
469 }
470
471 gdb_assert (primary_filetab == m_filetabs);
472 }
473
474 /* See symtab.h. */
475
476 struct symtab *
477 compunit_symtab::primary_filetab () const
478 {
479 gdb_assert (m_filetabs != nullptr);
480
481 /* The primary file symtab is the first one in the list. */
482 return m_filetabs;
483 }
484
485 /* See symtab.h. */
486
487 enum language
488 compunit_symtab::language () const
489 {
490 struct symtab *symtab = primary_filetab ();
491
492 /* The language of the compunit symtab is the language of its
493 primary source file. */
494 return symtab->language ();
495 }
496
497 /* See symtab.h. */
498
499 void
500 compunit_symtab::forget_cached_source_info ()
501 {
502 for (symtab *s : filetabs ())
503 s->release_fullname ();
504 }
505
506 /* See symtab.h. */
507
508 struct symbol *
509 compunit_symtab::symbol_at_address (CORE_ADDR addr) const
510 {
511 return blockvector ()->symbol_at_address (addr);
512 }
513
514 /* See symtab.h. */
515
516 compunit_symtab::compunit_symtab (struct objfile *objfile,
517 const char *name_)
518 : m_objfile (objfile),
519 /* The name we record here is only for display/debugging purposes.
520 Just save the basename to avoid path issues (too long for
521 display, relative vs absolute, etc.). */
522 name (obstack_strdup (&objfile->objfile_obstack, lbasename (name_))),
523 m_locations_valid (false),
524 m_epilogue_unwind_valid (false)
525 {
526 symtab_create_debug_printf_v ("created compunit symtab %s for %s",
527 host_address_to_string (this),
528 name);
529 }
530
531 /* See symtab.h. */
532
533 compunit_symtab::~compunit_symtab ()
534 {
535 this->forget_cached_source_info ();
536 }
537
538 /* The relocated address of the minimal symbol, using the section
539 offsets from OBJFILE. */
540
541 CORE_ADDR
542 minimal_symbol::value_address (objfile *objfile) const
543 {
544 if (this->maybe_copied (objfile))
545 return this->get_maybe_copied_address (objfile);
546 else
547 return (CORE_ADDR (this->unrelocated_address ())
548 + objfile->section_offsets[this->section_index ()]);
549 }
550
551 /* See symtab.h. */
552
553 bool
554 minimal_symbol::data_p () const
555 {
556 return m_type == mst_data
557 || m_type == mst_bss
558 || m_type == mst_abs
559 || m_type == mst_file_data
560 || m_type == mst_file_bss;
561 }
562
563 /* See symtab.h. */
564
565 bool
566 minimal_symbol::text_p () const
567 {
568 return m_type == mst_text
569 || m_type == mst_text_gnu_ifunc
570 || m_type == mst_data_gnu_ifunc
571 || m_type == mst_slot_got_plt
572 || m_type == mst_solib_trampoline
573 || m_type == mst_file_text;
574 }
575
576 /* See symtab.h. */
577
578 bool
579 minimal_symbol::maybe_copied (objfile *objfile) const
580 {
581 return (objfile->object_format_has_copy_relocs
582 && (objfile->flags & OBJF_MAINLINE) == 0
583 && (m_type == mst_data || m_type == mst_bss));
584 }
585
586 /* See whether FILENAME matches SEARCH_NAME using the rule that we
587 advertise to the user. (The manual's description of linespecs
588 describes what we advertise). Returns true if they match, false
589 otherwise. */
590
591 bool
592 compare_filenames_for_search (const char *filename, const char *search_name)
593 {
594 int len = strlen (filename);
595 size_t search_len = strlen (search_name);
596
597 if (len < search_len)
598 return false;
599
600 /* The tail of FILENAME must match. */
601 if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
602 return false;
603
604 /* Either the names must completely match, or the character
605 preceding the trailing SEARCH_NAME segment of FILENAME must be a
606 directory separator.
607
608 The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
609 cannot match FILENAME "/path//dir/file.c" - as user has requested
610 absolute path. The sama applies for "c:\file.c" possibly
611 incorrectly hypothetically matching "d:\dir\c:\file.c".
612
613 The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
614 compatible with SEARCH_NAME "file.c". In such case a compiler had
615 to put the "c:file.c" name into debug info. Such compatibility
616 works only on GDB built for DOS host. */
617 return (len == search_len
618 || (!IS_ABSOLUTE_PATH (search_name)
619 && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
620 || (HAS_DRIVE_SPEC (filename)
621 && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
622 }
623
624 /* See symtab.h. */
625
626 void
627 iterate_over_symtabs (program_space *pspace, const char *name,
628 gdb::function_view<bool (symtab *)> callback)
629 {
630 gdb::unique_xmalloc_ptr<char> real_path;
631
632 /* Here we are interested in canonicalizing an absolute path, not
633 absolutizing a relative path. */
634 if (IS_ABSOLUTE_PATH (name))
635 {
636 real_path = gdb_realpath (name);
637 gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
638 }
639
640 for (objfile &objfile : pspace->objfiles ())
641 if (objfile.map_symtabs_matching_filename (name, real_path.get (),
642 callback))
643 return;
644 }
645
646 /* See symtab.h. */
647
648 symtab *
649 lookup_symtab (program_space *pspace, const char *name)
650 {
651 struct symtab *result = NULL;
652
653 iterate_over_symtabs (pspace, name, [&] (symtab *symtab)
654 {
655 result = symtab;
656 return true;
657 });
658
659 return result;
660 }
661
662 \f
663 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
664 full method name, which consist of the class name (from T), the unadorned
665 method name from METHOD_ID, and the signature for the specific overload,
666 specified by SIGNATURE_ID. Note that this function is g++ specific. */
667
668 char *
669 gdb_mangle_name (struct type *type, int method_id, int signature_id)
670 {
671 int mangled_name_len;
672 char *mangled_name;
673 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
674 struct fn_field *method = &f[signature_id];
675 const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
676 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
677 const char *newname = type->name ();
678
679 /* Does the form of physname indicate that it is the full mangled name
680 of a constructor (not just the args)? */
681 int is_full_physname_constructor;
682
683 int is_constructor;
684 int is_destructor = is_destructor_name (physname);
685 /* Need a new type prefix. */
686 const char *const_prefix = method->is_const ? "C" : "";
687 const char *volatile_prefix = method->is_volatile ? "V" : "";
688 char buf[20];
689 int len = (newname == NULL ? 0 : strlen (newname));
690
691 /* Nothing to do if physname already contains a fully mangled v3 abi name
692 or an operator name. */
693 if ((physname[0] == '_' && physname[1] == 'Z')
694 || is_operator_name (field_name))
695 return xstrdup (physname);
696
697 is_full_physname_constructor = is_constructor_name (physname);
698
699 is_constructor = is_full_physname_constructor
700 || (newname && strcmp (field_name, newname) == 0);
701
702 if (!is_destructor)
703 is_destructor = (startswith (physname, "__dt"));
704
705 if (is_destructor || is_full_physname_constructor)
706 {
707 mangled_name = (char *) xmalloc (strlen (physname) + 1);
708 strcpy (mangled_name, physname);
709 return mangled_name;
710 }
711
712 if (len == 0)
713 {
714 xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
715 }
716 else if (physname[0] == 't' || physname[0] == 'Q')
717 {
718 /* The physname for template and qualified methods already includes
719 the class name. */
720 xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
721 newname = NULL;
722 len = 0;
723 }
724 else
725 {
726 xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
727 volatile_prefix, len);
728 }
729 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
730 + strlen (buf) + len + strlen (physname) + 1);
731
732 mangled_name = (char *) xmalloc (mangled_name_len);
733 if (is_constructor)
734 mangled_name[0] = '\0';
735 else
736 strcpy (mangled_name, field_name);
737
738 strcat (mangled_name, buf);
739 /* If the class doesn't have a name, i.e. newname NULL, then we just
740 mangle it using 0 for the length of the class. Thus it gets mangled
741 as something starting with `::' rather than `classname::'. */
742 if (newname != NULL)
743 strcat (mangled_name, newname);
744
745 strcat (mangled_name, physname);
746 return (mangled_name);
747 }
748
749 /* See symtab.h. */
750
751 void
752 general_symbol_info::set_demangled_name (const char *name,
753 struct obstack *obstack)
754 {
755 if (language () == language_ada)
756 {
757 if (name == NULL)
758 {
759 ada_mangled = 0;
760 language_specific.obstack = obstack;
761 }
762 else
763 {
764 ada_mangled = 1;
765 language_specific.demangled_name = name;
766 }
767 }
768 else
769 language_specific.demangled_name = name;
770 }
771
772 \f
773 /* Initialize the language dependent portion of a symbol
774 depending upon the language for the symbol. */
775
776 void
777 general_symbol_info::set_language (enum language language,
778 struct obstack *obstack)
779 {
780 m_language = language;
781 if (language == language_cplus
782 || language == language_d
783 || language == language_go
784 || language == language_objc
785 || language == language_fortran)
786 {
787 set_demangled_name (NULL, obstack);
788 }
789 else if (language == language_ada)
790 {
791 gdb_assert (ada_mangled == 0);
792 language_specific.obstack = obstack;
793 }
794 else
795 {
796 memset (&language_specific, 0, sizeof (language_specific));
797 }
798 }
799
800 /* Functions to initialize a symbol's mangled name. */
801
802 /* Objects of this type are stored in the demangled name hash table. */
803 struct demangled_name_entry
804 {
805 demangled_name_entry (std::string_view mangled_name)
806 : mangled (mangled_name) {}
807
808 std::string_view mangled;
809 enum language language;
810 gdb::unique_xmalloc_ptr<char> demangled;
811 };
812
813 /* Hash function for the demangled name hash. */
814
815 static hashval_t
816 hash_demangled_name_entry (const void *data)
817 {
818 const struct demangled_name_entry *e
819 = (const struct demangled_name_entry *) data;
820
821 return gdb::string_view_hash () (e->mangled);
822 }
823
824 /* Equality function for the demangled name hash. */
825
826 static int
827 eq_demangled_name_entry (const void *a, const void *b)
828 {
829 const struct demangled_name_entry *da
830 = (const struct demangled_name_entry *) a;
831 const struct demangled_name_entry *db
832 = (const struct demangled_name_entry *) b;
833
834 return da->mangled == db->mangled;
835 }
836
837 static void
838 free_demangled_name_entry (void *data)
839 {
840 struct demangled_name_entry *e
841 = (struct demangled_name_entry *) data;
842
843 e->~demangled_name_entry();
844 }
845
846 /* Create the hash table used for demangled names. Each hash entry is
847 a pair of strings; one for the mangled name and one for the demangled
848 name. The entry is hashed via just the mangled name. */
849
850 static void
851 create_demangled_names_hash (struct objfile_per_bfd_storage *per_bfd)
852 {
853 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
854 The hash table code will round this up to the next prime number.
855 Choosing a much larger table size wastes memory, and saves only about
856 1% in symbol reading. However, if the minsym count is already
857 initialized (e.g. because symbol name setting was deferred to
858 a background thread) we can initialize the hashtable with a count
859 based on that, because we will almost certainly have at least that
860 many entries. If we have a nonzero number but less than 256,
861 we still stay with 256 to have some space for psymbols, etc. */
862
863 /* htab will expand the table when it is 3/4th full, so we account for that
864 here. +2 to round up. */
865 int minsym_based_count = (per_bfd->minimal_symbol_count + 2) / 3 * 4;
866 int count = std::max (per_bfd->minimal_symbol_count, minsym_based_count);
867
868 per_bfd->demangled_names_hash.reset (htab_create_alloc
869 (count, hash_demangled_name_entry, eq_demangled_name_entry,
870 free_demangled_name_entry, xcalloc, xfree));
871 }
872
873 /* See symtab.h */
874
875 gdb::unique_xmalloc_ptr<char>
876 symbol_find_demangled_name (struct general_symbol_info *gsymbol,
877 const char *mangled)
878 {
879 gdb::unique_xmalloc_ptr<char> demangled;
880 int i;
881
882 if (gsymbol->language () != language_unknown)
883 {
884 const struct language_defn *lang = language_def (gsymbol->language ());
885
886 lang->sniff_from_mangled_name (mangled, &demangled);
887 return demangled;
888 }
889
890 for (i = language_unknown; i < nr_languages; ++i)
891 {
892 enum language l = (enum language) i;
893 const struct language_defn *lang = language_def (l);
894
895 if (lang->sniff_from_mangled_name (mangled, &demangled))
896 {
897 gsymbol->m_language = l;
898 return demangled;
899 }
900 }
901
902 return NULL;
903 }
904
905 /* Set both the mangled and demangled (if any) names for GSYMBOL based
906 on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
907 objfile's obstack; but if COPY_NAME is 0 and if NAME is
908 NUL-terminated, then this function assumes that NAME is already
909 correctly saved (either permanently or with a lifetime tied to the
910 objfile), and it will not be copied.
911
912 The hash table corresponding to OBJFILE is used, and the memory
913 comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
914 so the pointer can be discarded after calling this function. */
915
916 void
917 general_symbol_info::compute_and_set_names (std::string_view linkage_name,
918 bool copy_name,
919 objfile_per_bfd_storage *per_bfd,
920 std::optional<hashval_t> hash)
921 {
922 struct demangled_name_entry **slot;
923
924 if (language () == language_ada)
925 {
926 /* In Ada, we do the symbol lookups using the mangled name, so
927 we can save some space by not storing the demangled name. */
928 if (!copy_name)
929 m_name = linkage_name.data ();
930 else
931 m_name = obstack_strndup (&per_bfd->storage_obstack,
932 linkage_name.data (),
933 linkage_name.length ());
934 set_demangled_name (NULL, &per_bfd->storage_obstack);
935
936 return;
937 }
938
939 if (per_bfd->demangled_names_hash == NULL)
940 create_demangled_names_hash (per_bfd);
941
942 struct demangled_name_entry entry (linkage_name);
943 if (!hash.has_value ())
944 hash = hash_demangled_name_entry (&entry);
945 slot = ((struct demangled_name_entry **)
946 htab_find_slot_with_hash (per_bfd->demangled_names_hash.get (),
947 &entry, *hash, INSERT));
948
949 /* The const_cast is safe because the only reason it is already
950 initialized is if we purposefully set it from a background
951 thread to avoid doing the work here. However, it is still
952 allocated from the heap and needs to be freed by us, just
953 like if we called symbol_find_demangled_name here. If this is
954 nullptr, we call symbol_find_demangled_name below, but we put
955 this smart pointer here to be sure that we don't leak this name. */
956 gdb::unique_xmalloc_ptr<char> demangled_name
957 (const_cast<char *> (language_specific.demangled_name));
958
959 /* If this name is not in the hash table, add it. */
960 if (*slot == NULL
961 /* A C version of the symbol may have already snuck into the table.
962 This happens to, e.g., main.init (__go_init_main). Cope. */
963 || (language () == language_go && (*slot)->demangled == nullptr))
964 {
965 /* A 0-terminated copy of the linkage name. Callers must set COPY_NAME
966 to true if the string might not be nullterminated. We have to make
967 this copy because demangling needs a nullterminated string. */
968 std::string_view linkage_name_copy;
969 if (copy_name)
970 {
971 char *alloc_name = (char *) alloca (linkage_name.length () + 1);
972 memcpy (alloc_name, linkage_name.data (), linkage_name.length ());
973 alloc_name[linkage_name.length ()] = '\0';
974
975 linkage_name_copy = std::string_view (alloc_name,
976 linkage_name.length ());
977 }
978 else
979 linkage_name_copy = linkage_name;
980
981 if (demangled_name.get () == nullptr)
982 demangled_name
983 = symbol_find_demangled_name (this, linkage_name_copy.data ());
984
985 /* Suppose we have demangled_name==NULL, copy_name==0, and
986 linkage_name_copy==linkage_name. In this case, we already have the
987 mangled name saved, and we don't have a demangled name. So,
988 you might think we could save a little space by not recording
989 this in the hash table at all.
990
991 It turns out that it is actually important to still save such
992 an entry in the hash table, because storing this name gives
993 us better bcache hit rates for partial symbols. */
994 if (!copy_name)
995 {
996 *slot
997 = ((struct demangled_name_entry *)
998 obstack_alloc (&per_bfd->storage_obstack,
999 sizeof (demangled_name_entry)));
1000 new (*slot) demangled_name_entry (linkage_name);
1001 }
1002 else
1003 {
1004 /* If we must copy the mangled name, put it directly after
1005 the struct so we can have a single allocation. */
1006 *slot
1007 = ((struct demangled_name_entry *)
1008 obstack_alloc (&per_bfd->storage_obstack,
1009 sizeof (demangled_name_entry)
1010 + linkage_name.length () + 1));
1011 char *mangled_ptr = reinterpret_cast<char *> (*slot + 1);
1012 memcpy (mangled_ptr, linkage_name.data (), linkage_name.length ());
1013 mangled_ptr [linkage_name.length ()] = '\0';
1014 new (*slot) demangled_name_entry
1015 (std::string_view (mangled_ptr, linkage_name.length ()));
1016 }
1017 (*slot)->demangled = std::move (demangled_name);
1018 (*slot)->language = language ();
1019 }
1020 else if (language () == language_unknown)
1021 m_language = (*slot)->language;
1022
1023 m_name = (*slot)->mangled.data ();
1024 set_demangled_name ((*slot)->demangled.get (), &per_bfd->storage_obstack);
1025 }
1026
1027 /* See symtab.h. */
1028
1029 const char *
1030 general_symbol_info::natural_name () const
1031 {
1032 switch (language ())
1033 {
1034 case language_cplus:
1035 case language_d:
1036 case language_go:
1037 case language_objc:
1038 case language_fortran:
1039 case language_rust:
1040 if (language_specific.demangled_name != nullptr)
1041 return language_specific.demangled_name;
1042 break;
1043 case language_ada:
1044 return ada_decode_symbol (this);
1045 default:
1046 break;
1047 }
1048 return linkage_name ();
1049 }
1050
1051 /* See symtab.h. */
1052
1053 const char *
1054 general_symbol_info::demangled_name () const
1055 {
1056 const char *dem_name = NULL;
1057
1058 switch (language ())
1059 {
1060 case language_cplus:
1061 case language_d:
1062 case language_go:
1063 case language_objc:
1064 case language_fortran:
1065 case language_rust:
1066 dem_name = language_specific.demangled_name;
1067 break;
1068 case language_ada:
1069 dem_name = ada_decode_symbol (this);
1070 break;
1071 default:
1072 break;
1073 }
1074 return dem_name;
1075 }
1076
1077 /* See symtab.h. */
1078
1079 const char *
1080 general_symbol_info::search_name () const
1081 {
1082 if (language () == language_ada)
1083 return linkage_name ();
1084 else
1085 return natural_name ();
1086 }
1087
1088 /* See symtab.h. */
1089
1090 struct obj_section *
1091 general_symbol_info::obj_section (const struct objfile *objfile) const
1092 {
1093 if (section_index () >= 0)
1094 return &objfile->sections_start[section_index ()];
1095 return nullptr;
1096 }
1097
1098 /* See symtab.h. */
1099
1100 bool
1101 symbol_matches_search_name (const struct general_symbol_info *gsymbol,
1102 const lookup_name_info &name)
1103 {
1104 symbol_name_matcher_ftype *name_match
1105 = language_def (gsymbol->language ())->get_symbol_name_matcher (name);
1106 return name_match (gsymbol->search_name (), name, NULL);
1107 }
1108
1109 \f
1110
1111 /* Return true if the two sections are the same, or if they could
1112 plausibly be copies of each other, one in an original object
1113 file and another in a separated debug file. */
1114
1115 bool
1116 matching_obj_sections (struct obj_section *obj_first,
1117 struct obj_section *obj_second)
1118 {
1119 asection *first = obj_first? obj_first->the_bfd_section : NULL;
1120 asection *second = obj_second? obj_second->the_bfd_section : NULL;
1121
1122 /* If they're the same section, then they match. */
1123 if (first == second)
1124 return true;
1125
1126 /* If either is NULL, give up. */
1127 if (first == NULL || second == NULL)
1128 return false;
1129
1130 /* This doesn't apply to absolute symbols. */
1131 if (first->owner == NULL || second->owner == NULL)
1132 return false;
1133
1134 /* If they're in the same object file, they must be different sections. */
1135 if (first->owner == second->owner)
1136 return false;
1137
1138 /* Check whether the two sections are potentially corresponding. They must
1139 have the same size, address, and name. We can't compare section indexes,
1140 which would be more reliable, because some sections may have been
1141 stripped. */
1142 if (bfd_section_size (first) != bfd_section_size (second))
1143 return false;
1144
1145 /* In-memory addresses may start at a different offset, relativize them. */
1146 if (bfd_section_vma (first) - bfd_get_start_address (first->owner)
1147 != bfd_section_vma (second) - bfd_get_start_address (second->owner))
1148 return false;
1149
1150 if (bfd_section_name (first) == NULL
1151 || bfd_section_name (second) == NULL
1152 || strcmp (bfd_section_name (first), bfd_section_name (second)) != 0)
1153 return false;
1154
1155 /* Otherwise check that they are in corresponding objfiles. */
1156
1157 struct objfile *obj = NULL;
1158 for (objfile &objfile : current_program_space->objfiles ())
1159 if (objfile.obfd == first->owner)
1160 {
1161 obj = &objfile;
1162 break;
1163 }
1164 gdb_assert (obj != NULL);
1165
1166 if (obj->separate_debug_objfile != NULL
1167 && obj->separate_debug_objfile->obfd == second->owner)
1168 return true;
1169 if (obj->separate_debug_objfile_backlink != NULL
1170 && obj->separate_debug_objfile_backlink->obfd == second->owner)
1171 return true;
1172
1173 return false;
1174 }
1175 \f
1176 /* Hash function for the symbol cache. */
1177
1178 static unsigned int
1179 hash_symbol_entry (const struct objfile *objfile_context,
1180 const char *name, domain_search_flags domain)
1181 {
1182 unsigned int hash = (uintptr_t) objfile_context;
1183
1184 if (name != NULL)
1185 hash += htab_hash_string (name);
1186
1187 hash += domain * 7;
1188
1189 return hash;
1190 }
1191
1192 /* Equality function for the symbol cache. */
1193
1194 static int
1195 eq_symbol_entry (const struct symbol_cache_slot *slot,
1196 const struct objfile *objfile_context,
1197 const char *name, domain_search_flags domain)
1198 {
1199 const char *slot_name;
1200
1201 if (slot->state == SYMBOL_SLOT_UNUSED)
1202 return 0;
1203
1204 if (slot->objfile_context != objfile_context)
1205 return 0;
1206
1207 domain_search_flags slot_domain = slot->domain;
1208 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1209 slot_name = slot->value.name;
1210 else
1211 slot_name = slot->value.found.symbol->search_name ();
1212
1213 /* NULL names match. */
1214 if (slot_name == NULL && name == NULL)
1215 {
1216 /* But there's no point in calling symbol_matches_domain in the
1217 SYMBOL_SLOT_FOUND case. */
1218 if (slot_domain != domain)
1219 return 0;
1220 }
1221 else if (slot_name != NULL && name != NULL)
1222 {
1223 /* It's important that we use the same comparison that was done
1224 the first time through. If the slot records a found symbol,
1225 then this means using the symbol name comparison function of
1226 the symbol's language with symbol->search_name (). See
1227 dictionary.c.
1228
1229 If the slot records a not-found symbol, then require a precise match.
1230 We could still be lax with whitespace like strcmp_iw though. */
1231
1232 if (slot_domain != domain)
1233 return 0;
1234
1235 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1236 {
1237 if (strcmp (slot_name, name) != 0)
1238 return 0;
1239 }
1240 else
1241 {
1242 struct symbol *sym = slot->value.found.symbol;
1243 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1244
1245 if (!symbol_matches_search_name (sym, lookup_name))
1246 return 0;
1247 }
1248 }
1249 else
1250 {
1251 /* Only one name is NULL. */
1252 return 0;
1253 }
1254
1255 return 1;
1256 }
1257
1258 /* Given a cache of size SIZE, return the size of the struct (with variable
1259 length array) in bytes. */
1260
1261 static size_t
1262 symbol_cache_byte_size (unsigned int size)
1263 {
1264 return (sizeof (struct block_symbol_cache)
1265 + ((size - 1) * sizeof (struct symbol_cache_slot)));
1266 }
1267
1268 /* Resize CACHE. */
1269
1270 static void
1271 resize_symbol_cache (struct symbol_cache *cache, unsigned int new_size)
1272 {
1273 /* If there's no change in size, don't do anything.
1274 All caches have the same size, so we can just compare with the size
1275 of the global symbols cache. */
1276 if ((cache->global_symbols != NULL
1277 && cache->global_symbols->size == new_size)
1278 || (cache->global_symbols == NULL
1279 && new_size == 0))
1280 return;
1281
1282 destroy_block_symbol_cache (cache->global_symbols);
1283 destroy_block_symbol_cache (cache->static_symbols);
1284
1285 if (new_size == 0)
1286 {
1287 cache->global_symbols = NULL;
1288 cache->static_symbols = NULL;
1289 }
1290 else
1291 {
1292 size_t total_size = symbol_cache_byte_size (new_size);
1293
1294 cache->global_symbols
1295 = (struct block_symbol_cache *) xcalloc (1, total_size);
1296 cache->static_symbols
1297 = (struct block_symbol_cache *) xcalloc (1, total_size);
1298 cache->global_symbols->size = new_size;
1299 cache->static_symbols->size = new_size;
1300 }
1301 }
1302
1303 /* Return the symbol cache of PSPACE.
1304 Create one if it doesn't exist yet. */
1305
1306 static struct symbol_cache *
1307 get_symbol_cache (struct program_space *pspace)
1308 {
1309 struct symbol_cache *cache = symbol_cache_key.get (pspace);
1310
1311 if (cache == NULL)
1312 {
1313 cache = symbol_cache_key.emplace (pspace);
1314 resize_symbol_cache (cache, symbol_cache_size);
1315 }
1316
1317 return cache;
1318 }
1319
1320 /* Set the size of the symbol cache in all program spaces. */
1321
1322 static void
1323 set_symbol_cache_size (unsigned int new_size)
1324 {
1325 for (struct program_space *pspace : program_spaces)
1326 {
1327 struct symbol_cache *cache = symbol_cache_key.get (pspace);
1328
1329 /* The pspace could have been created but not have a cache yet. */
1330 if (cache != NULL)
1331 resize_symbol_cache (cache, new_size);
1332 }
1333 }
1334
1335 /* Called when symbol-cache-size is set. */
1336
1337 static void
1338 set_symbol_cache_size_handler (const char *args, int from_tty,
1339 struct cmd_list_element *c)
1340 {
1341 if (new_symbol_cache_size > MAX_SYMBOL_CACHE_SIZE)
1342 {
1343 /* Restore the previous value.
1344 This is the value the "show" command prints. */
1345 new_symbol_cache_size = symbol_cache_size;
1346
1347 error (_("Symbol cache size is too large, max is %u."),
1348 MAX_SYMBOL_CACHE_SIZE);
1349 }
1350 symbol_cache_size = new_symbol_cache_size;
1351
1352 set_symbol_cache_size (symbol_cache_size);
1353 }
1354
1355 /* Lookup symbol NAME,DOMAIN in BLOCK in the symbol cache of PSPACE.
1356 OBJFILE_CONTEXT is the current objfile, which may be NULL.
1357 The result is the symbol if found, SYMBOL_LOOKUP_FAILED if a previous lookup
1358 failed (and thus this one will too), or NULL if the symbol is not present
1359 in the cache.
1360 *BSC_PTR and *SLOT_PTR are set to the cache and slot of the symbol, which
1361 can be used to save the result of a full lookup attempt. */
1362
1363 static struct block_symbol
1364 symbol_cache_lookup (struct symbol_cache *cache,
1365 struct objfile *objfile_context, enum block_enum block,
1366 const char *name, domain_search_flags domain,
1367 struct block_symbol_cache **bsc_ptr,
1368 struct symbol_cache_slot **slot_ptr)
1369 {
1370 struct block_symbol_cache *bsc;
1371 unsigned int hash;
1372 struct symbol_cache_slot *slot;
1373
1374 if (block == GLOBAL_BLOCK)
1375 bsc = cache->global_symbols;
1376 else
1377 bsc = cache->static_symbols;
1378 if (bsc == NULL)
1379 {
1380 *bsc_ptr = NULL;
1381 *slot_ptr = NULL;
1382 return {};
1383 }
1384
1385 hash = hash_symbol_entry (objfile_context, name, domain);
1386 slot = bsc->symbols + hash % bsc->size;
1387
1388 *bsc_ptr = bsc;
1389 *slot_ptr = slot;
1390
1391 if (eq_symbol_entry (slot, objfile_context, name, domain))
1392 {
1393 symbol_lookup_debug_printf ("%s block symbol cache hit%s for %s, %s",
1394 block == GLOBAL_BLOCK ? "Global" : "Static",
1395 slot->state == SYMBOL_SLOT_NOT_FOUND
1396 ? " (not found)" : "", name,
1397 domain_name (domain).c_str ());
1398 ++bsc->hits;
1399 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1400 return SYMBOL_LOOKUP_FAILED;
1401 return slot->value.found;
1402 }
1403
1404 /* Symbol is not present in the cache. */
1405
1406 symbol_lookup_debug_printf ("%s block symbol cache miss for %s, %s",
1407 block == GLOBAL_BLOCK ? "Global" : "Static",
1408 name, domain_name (domain).c_str ());
1409 ++bsc->misses;
1410 return {};
1411 }
1412
1413 /* Mark SYMBOL as found in SLOT.
1414 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1415 if it's not needed to distinguish lookups (STATIC_BLOCK). It is *not*
1416 necessarily the objfile the symbol was found in. */
1417
1418 static void
1419 symbol_cache_mark_found (struct block_symbol_cache *bsc,
1420 struct symbol_cache_slot *slot,
1421 struct objfile *objfile_context,
1422 struct symbol *symbol,
1423 const struct block *block,
1424 domain_search_flags domain)
1425 {
1426 if (bsc == NULL)
1427 return;
1428 if (slot->state != SYMBOL_SLOT_UNUSED)
1429 {
1430 ++bsc->collisions;
1431 symbol_cache_clear_slot (slot);
1432 }
1433 slot->state = SYMBOL_SLOT_FOUND;
1434 slot->objfile_context = objfile_context;
1435 slot->value.found.symbol = symbol;
1436 slot->value.found.block = block;
1437 slot->domain = domain;
1438 }
1439
1440 /* Mark symbol NAME, DOMAIN as not found in SLOT.
1441 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1442 if it's not needed to distinguish lookups (STATIC_BLOCK). */
1443
1444 static void
1445 symbol_cache_mark_not_found (struct block_symbol_cache *bsc,
1446 struct symbol_cache_slot *slot,
1447 struct objfile *objfile_context,
1448 const char *name, domain_search_flags domain)
1449 {
1450 if (bsc == NULL)
1451 return;
1452 if (slot->state != SYMBOL_SLOT_UNUSED)
1453 {
1454 ++bsc->collisions;
1455 symbol_cache_clear_slot (slot);
1456 }
1457 slot->state = SYMBOL_SLOT_NOT_FOUND;
1458 slot->objfile_context = objfile_context;
1459 slot->value.name = xstrdup (name);
1460 slot->domain = domain;
1461 }
1462
1463 /* Flush the symbol cache of PSPACE. */
1464
1465 static void
1466 symbol_cache_flush (struct program_space *pspace)
1467 {
1468 ada_clear_symbol_cache (pspace);
1469 struct symbol_cache *cache = symbol_cache_key.get (pspace);
1470 int pass;
1471
1472 if (cache == NULL)
1473 return;
1474 if (cache->global_symbols == NULL)
1475 {
1476 gdb_assert (symbol_cache_size == 0);
1477 gdb_assert (cache->static_symbols == NULL);
1478 return;
1479 }
1480
1481 /* If the cache is untouched since the last flush, early exit.
1482 This is important for performance during the startup of a program linked
1483 with 100s (or 1000s) of shared libraries. */
1484 if (cache->global_symbols->misses == 0
1485 && cache->static_symbols->misses == 0)
1486 return;
1487
1488 gdb_assert (cache->global_symbols->size == symbol_cache_size);
1489 gdb_assert (cache->static_symbols->size == symbol_cache_size);
1490
1491 for (pass = 0; pass < 2; ++pass)
1492 {
1493 struct block_symbol_cache *bsc
1494 = pass == 0 ? cache->global_symbols : cache->static_symbols;
1495 unsigned int i;
1496
1497 for (i = 0; i < bsc->size; ++i)
1498 symbol_cache_clear_slot (&bsc->symbols[i]);
1499 }
1500
1501 cache->global_symbols->hits = 0;
1502 cache->global_symbols->misses = 0;
1503 cache->global_symbols->collisions = 0;
1504 cache->static_symbols->hits = 0;
1505 cache->static_symbols->misses = 0;
1506 cache->static_symbols->collisions = 0;
1507 }
1508
1509 /* Dump CACHE. */
1510
1511 static void
1512 symbol_cache_dump (const struct symbol_cache *cache)
1513 {
1514 int pass;
1515
1516 if (cache->global_symbols == NULL)
1517 {
1518 gdb_printf (" <disabled>\n");
1519 return;
1520 }
1521
1522 for (pass = 0; pass < 2; ++pass)
1523 {
1524 const struct block_symbol_cache *bsc
1525 = pass == 0 ? cache->global_symbols : cache->static_symbols;
1526 unsigned int i;
1527
1528 if (pass == 0)
1529 gdb_printf ("Global symbols:\n");
1530 else
1531 gdb_printf ("Static symbols:\n");
1532
1533 for (i = 0; i < bsc->size; ++i)
1534 {
1535 const struct symbol_cache_slot *slot = &bsc->symbols[i];
1536
1537 QUIT;
1538
1539 switch (slot->state)
1540 {
1541 case SYMBOL_SLOT_UNUSED:
1542 break;
1543 case SYMBOL_SLOT_NOT_FOUND:
1544 gdb_printf (" [%4u] = %s, %s %s (not found)\n", i,
1545 host_address_to_string (slot->objfile_context),
1546 slot->value.name,
1547 domain_name (slot->domain).c_str ());
1548 break;
1549 case SYMBOL_SLOT_FOUND:
1550 {
1551 struct symbol *found = slot->value.found.symbol;
1552 const struct objfile *context = slot->objfile_context;
1553
1554 gdb_printf (" [%4u] = %s, %s %s\n", i,
1555 host_address_to_string (context),
1556 found->print_name (),
1557 domain_name (found->domain ()));
1558 break;
1559 }
1560 }
1561 }
1562 }
1563 }
1564
1565 /* The "mt print symbol-cache" command. */
1566
1567 static void
1568 maintenance_print_symbol_cache (const char *args, int from_tty)
1569 {
1570 for (struct program_space *pspace : program_spaces)
1571 {
1572 struct symbol_cache *cache;
1573
1574 gdb_printf (_("Symbol cache for pspace %d\n%s:\n"),
1575 pspace->num,
1576 pspace->symfile_object_file != NULL
1577 ? objfile_name (pspace->symfile_object_file)
1578 : "(no object file)");
1579
1580 /* If the cache hasn't been created yet, avoid creating one. */
1581 cache = symbol_cache_key.get (pspace);
1582 if (cache == NULL)
1583 gdb_printf (" <empty>\n");
1584 else
1585 symbol_cache_dump (cache);
1586 }
1587 }
1588
1589 /* The "mt flush-symbol-cache" command. */
1590
1591 static void
1592 maintenance_flush_symbol_cache (const char *args, int from_tty)
1593 {
1594 for (struct program_space *pspace : program_spaces)
1595 {
1596 symbol_cache_flush (pspace);
1597 }
1598 }
1599
1600 /* Print usage statistics of CACHE. */
1601
1602 static void
1603 symbol_cache_stats (struct symbol_cache *cache)
1604 {
1605 int pass;
1606
1607 if (cache->global_symbols == NULL)
1608 {
1609 gdb_printf (" <disabled>\n");
1610 return;
1611 }
1612
1613 for (pass = 0; pass < 2; ++pass)
1614 {
1615 const struct block_symbol_cache *bsc
1616 = pass == 0 ? cache->global_symbols : cache->static_symbols;
1617
1618 QUIT;
1619
1620 if (pass == 0)
1621 gdb_printf ("Global block cache stats:\n");
1622 else
1623 gdb_printf ("Static block cache stats:\n");
1624
1625 gdb_printf (" size: %u\n", bsc->size);
1626 gdb_printf (" hits: %u\n", bsc->hits);
1627 gdb_printf (" misses: %u\n", bsc->misses);
1628 gdb_printf (" collisions: %u\n", bsc->collisions);
1629 }
1630 }
1631
1632 /* The "mt print symbol-cache-statistics" command. */
1633
1634 static void
1635 maintenance_print_symbol_cache_statistics (const char *args, int from_tty)
1636 {
1637 for (struct program_space *pspace : program_spaces)
1638 {
1639 struct symbol_cache *cache;
1640
1641 gdb_printf (_("Symbol cache statistics for pspace %d\n%s:\n"),
1642 pspace->num,
1643 pspace->symfile_object_file != NULL
1644 ? objfile_name (pspace->symfile_object_file)
1645 : "(no object file)");
1646
1647 /* If the cache hasn't been created yet, avoid creating one. */
1648 cache = symbol_cache_key.get (pspace);
1649 if (cache == NULL)
1650 gdb_printf (" empty, no stats available\n");
1651 else
1652 symbol_cache_stats (cache);
1653 }
1654 }
1655
1656 /* This module's 'new_objfile' observer. */
1657
1658 static void
1659 symtab_new_objfile_observer (struct objfile *objfile)
1660 {
1661 symbol_cache_flush (objfile->pspace ());
1662 }
1663
1664 /* This module's 'all_objfiles_removed' observer. */
1665
1666 static void
1667 symtab_all_objfiles_removed (program_space *pspace)
1668 {
1669 symbol_cache_flush (pspace);
1670
1671 /* Forget everything we know about the main function. */
1672 main_progspace_key.clear (pspace);
1673 }
1674
1675 /* This module's 'free_objfile' observer. */
1676
1677 static void
1678 symtab_free_objfile_observer (struct objfile *objfile)
1679 {
1680 symbol_cache_flush (objfile->pspace ());
1681 }
1682 \f
1683 /* See symtab.h. */
1684
1685 void
1686 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
1687 {
1688 gdb_assert (sym != nullptr);
1689 gdb_assert (sym->is_objfile_owned ());
1690 gdb_assert (objfile != nullptr);
1691 gdb_assert (sym->section_index () == -1);
1692
1693 /* Note that if this ends up as -1, fixup_section will handle that
1694 reasonably well. So, it's fine to use the objfile's section
1695 index without doing the check that is done by the wrapper macros
1696 like SECT_OFF_TEXT. */
1697 int fallback;
1698 switch (sym->loc_class ())
1699 {
1700 case LOC_STATIC:
1701 fallback = objfile->sect_index_data;
1702 break;
1703
1704 case LOC_LABEL:
1705 fallback = objfile->sect_index_text;
1706 break;
1707
1708 default:
1709 /* Nothing else will be listed in the minsyms -- no use looking
1710 it up. */
1711 return;
1712 }
1713
1714 CORE_ADDR addr = sym->value_address ();
1715
1716 struct minimal_symbol *msym;
1717
1718 /* First, check whether a minimal symbol with the same name exists
1719 and points to the same address. The address check is required
1720 e.g. on PowerPC64, where the minimal symbol for a function will
1721 point to the function descriptor, while the debug symbol will
1722 point to the actual function code. */
1723 msym = lookup_minimal_symbol_by_pc_name (addr, sym->linkage_name (),
1724 objfile);
1725 if (msym)
1726 sym->set_section_index (msym->section_index ());
1727 else
1728 {
1729 /* Static, function-local variables do appear in the linker
1730 (minimal) symbols, but are frequently given names that won't
1731 be found via lookup_minimal_symbol(). E.g., it has been
1732 observed in frv-uclinux (ELF) executables that a static,
1733 function-local variable named "foo" might appear in the
1734 linker symbols as "foo.6" or "foo.3". Thus, there is no
1735 point in attempting to extend the lookup-by-name mechanism to
1736 handle this case due to the fact that there can be multiple
1737 names.
1738
1739 So, instead, search the section table when lookup by name has
1740 failed. The ``addr'' and ``endaddr'' fields may have already
1741 been relocated. If so, the relocation offset needs to be
1742 subtracted from these values when performing the comparison.
1743 We unconditionally subtract it, because, when no relocation
1744 has been performed, the value will simply be zero.
1745
1746 The address of the symbol whose section we're fixing up HAS
1747 NOT BEEN adjusted (relocated) yet. It can't have been since
1748 the section isn't yet known and knowing the section is
1749 necessary in order to add the correct relocation value. In
1750 other words, we wouldn't even be in this function (attempting
1751 to compute the section) if it were already known.
1752
1753 Note that it is possible to search the minimal symbols
1754 (subtracting the relocation value if necessary) to find the
1755 matching minimal symbol, but this is overkill and much less
1756 efficient. It is not necessary to find the matching minimal
1757 symbol, only its section.
1758
1759 Note that this technique (of doing a section table search)
1760 can fail when unrelocated section addresses overlap. For
1761 this reason, we still attempt a lookup by name prior to doing
1762 a search of the section table. */
1763
1764 for (obj_section &s : objfile->sections ())
1765 {
1766 if ((bfd_section_flags (s.the_bfd_section) & SEC_ALLOC) == 0)
1767 continue;
1768
1769 int idx = &s - objfile->sections_start;
1770 CORE_ADDR offset = objfile->section_offsets[idx];
1771
1772 if (fallback == -1)
1773 fallback = idx;
1774
1775 if (s.addr () - offset <= addr && addr < s.endaddr () - offset)
1776 {
1777 sym->set_section_index (idx);
1778 return;
1779 }
1780 }
1781
1782 /* If we didn't find the section, assume it is in the first
1783 section. If there is no allocated section, then it hardly
1784 matters what we pick, so just pick zero. */
1785 if (fallback == -1)
1786 sym->set_section_index (0);
1787 else
1788 sym->set_section_index (fallback);
1789 }
1790 }
1791
1792 /* See symtab.h. */
1793
1794 demangle_for_lookup_info::demangle_for_lookup_info
1795 (const lookup_name_info &lookup_name, language lang)
1796 {
1797 demangle_result_storage storage;
1798
1799 if (lookup_name.ignore_parameters () && lang == language_cplus)
1800 {
1801 gdb::unique_xmalloc_ptr<char> without_params
1802 = cp_remove_params_if_any (lookup_name.c_str (),
1803 lookup_name.completion_mode ());
1804
1805 if (without_params != NULL)
1806 {
1807 if (lookup_name.match_type () != symbol_name_match_type::SEARCH_NAME)
1808 m_demangled_name = demangle_for_lookup (without_params.get (),
1809 lang, storage);
1810 return;
1811 }
1812 }
1813
1814 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
1815 m_demangled_name = lookup_name.c_str ();
1816 else
1817 m_demangled_name = demangle_for_lookup (lookup_name.c_str (),
1818 lang, storage);
1819 }
1820
1821 /* See symtab.h. */
1822
1823 const lookup_name_info &
1824 lookup_name_info::match_any ()
1825 {
1826 /* Lookup any symbol that "" would complete. I.e., this matches all
1827 symbol names. */
1828 static const lookup_name_info lookup_name ("", symbol_name_match_type::WILD,
1829 true);
1830
1831 return lookup_name;
1832 }
1833
1834 /* See symtab.h. */
1835
1836 unsigned int
1837 lookup_name_info::search_name_hash (language lang) const
1838 {
1839 /* This works around an obscure problem. If currently in Ada mode,
1840 and the name is wrapped in '<...>' (indicating verbatim mode),
1841 force the use of the Ada language here so that the '<' and '>'
1842 will be removed. */
1843 if (current_language->la_language == language_ada && ada ().verbatim_p ())
1844 lang = language_ada;
1845
1846 /* Only compute each language's hash once. */
1847 if (!m_demangled_hashes_p[lang])
1848 {
1849 m_demangled_hashes[lang]
1850 = ::search_name_hash (lang, language_lookup_name (lang));
1851 m_demangled_hashes_p[lang] = true;
1852 }
1853 return m_demangled_hashes[lang];
1854 }
1855
1856 /* Compute the demangled form of NAME as used by the various symbol
1857 lookup functions. The result can either be the input NAME
1858 directly, or a pointer to a buffer owned by the STORAGE object.
1859
1860 For Ada, this function just returns NAME, unmodified.
1861 Normally, Ada symbol lookups are performed using the encoded name
1862 rather than the demangled name, and so it might seem to make sense
1863 for this function to return an encoded version of NAME.
1864 Unfortunately, we cannot do this, because this function is used in
1865 circumstances where it is not appropriate to try to encode NAME.
1866 For instance, when displaying the frame info, we demangle the name
1867 of each parameter, and then perform a symbol lookup inside our
1868 function using that demangled name. In Ada, certain functions
1869 have internally-generated parameters whose name contain uppercase
1870 characters. Encoding those name would result in those uppercase
1871 characters to become lowercase, and thus cause the symbol lookup
1872 to fail. */
1873
1874 const char *
1875 demangle_for_lookup (const char *name, enum language lang,
1876 demangle_result_storage &storage)
1877 {
1878 /* If we are using C++, D, or Go, demangle the name before doing a
1879 lookup, so we can always binary search. */
1880 if (lang == language_cplus)
1881 {
1882 gdb::unique_xmalloc_ptr<char> demangled_name
1883 = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1884 if (demangled_name != NULL)
1885 return storage.set_malloc_ptr (std::move (demangled_name));
1886
1887 /* If we were given a non-mangled name, canonicalize it
1888 according to the language (so far only for C++). */
1889 gdb::unique_xmalloc_ptr<char> canon = cp_canonicalize_string (name);
1890 if (canon != nullptr)
1891 return storage.set_malloc_ptr (std::move (canon));
1892 }
1893 else if (lang == language_d)
1894 {
1895 gdb::unique_xmalloc_ptr<char> demangled_name = d_demangle (name, 0);
1896 if (demangled_name != NULL)
1897 return storage.set_malloc_ptr (std::move (demangled_name));
1898 }
1899 else if (lang == language_go)
1900 {
1901 gdb::unique_xmalloc_ptr<char> demangled_name
1902 = language_def (language_go)->demangle_symbol (name, 0);
1903 if (demangled_name != NULL)
1904 return storage.set_malloc_ptr (std::move (demangled_name));
1905 }
1906
1907 return name;
1908 }
1909
1910 /* See symtab.h. */
1911
1912 unsigned int
1913 search_name_hash (enum language language, const char *search_name)
1914 {
1915 return language_def (language)->search_name_hash (search_name);
1916 }
1917
1918 /* See symtab.h.
1919
1920 This function (or rather its subordinates) have a bunch of loops and
1921 it would seem to be attractive to put in some QUIT's (though I'm not really
1922 sure whether it can run long enough to be really important). But there
1923 are a few calls for which it would appear to be bad news to quit
1924 out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
1925 that there is C++ code below which can error(), but that probably
1926 doesn't affect these calls since they are looking for a known
1927 variable and thus can probably assume it will never hit the C++
1928 code). */
1929
1930 struct block_symbol
1931 lookup_symbol_in_language (const char *name, const struct block *block,
1932 const domain_search_flags domain,
1933 enum language lang,
1934 struct field_of_this_result *is_a_field_of_this)
1935 {
1936 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT;
1937
1938 demangle_result_storage storage;
1939 const char *modified_name = demangle_for_lookup (name, lang, storage);
1940
1941 return lookup_symbol_aux (modified_name,
1942 symbol_name_match_type::FULL,
1943 block, domain, lang,
1944 is_a_field_of_this);
1945 }
1946
1947 /* See symtab.h. */
1948
1949 struct block_symbol
1950 lookup_symbol (const char *name, const struct block *block,
1951 domain_search_flags domain,
1952 struct field_of_this_result *is_a_field_of_this)
1953 {
1954 return lookup_symbol_in_language (name, block, domain,
1955 current_language->la_language,
1956 is_a_field_of_this);
1957 }
1958
1959 /* See symtab.h. */
1960
1961 struct block_symbol
1962 lookup_symbol_search_name (const char *search_name, const struct block *block,
1963 domain_search_flags domain)
1964 {
1965 return lookup_symbol_aux (search_name, symbol_name_match_type::SEARCH_NAME,
1966 block, domain, language_asm, NULL);
1967 }
1968
1969 /* See symtab.h. */
1970
1971 struct block_symbol
1972 lookup_language_this (const struct language_defn *lang,
1973 const struct block *block)
1974 {
1975 if (lang->name_of_this () == NULL || block == NULL)
1976 return {};
1977
1978 symbol_lookup_debug_printf_v ("lookup_language_this (%s, %s (objfile %s))",
1979 lang->name (), host_address_to_string (block),
1980 objfile_debug_name (block->objfile ()));
1981
1982 lookup_name_info this_name (lang->name_of_this (),
1983 symbol_name_match_type::SEARCH_NAME);
1984
1985 while (block)
1986 {
1987 struct symbol *sym;
1988
1989 sym = block_lookup_symbol (block, this_name, SEARCH_VFT);
1990 if (sym != NULL)
1991 {
1992 symbol_lookup_debug_printf_v
1993 ("lookup_language_this (...) = %s (%s, block %s)",
1994 sym->print_name (), host_address_to_string (sym),
1995 host_address_to_string (block));
1996 return (struct block_symbol) {sym, block};
1997 }
1998 if (block->function ())
1999 break;
2000 block = block->superblock ();
2001 }
2002
2003 symbol_lookup_debug_printf_v ("lookup_language_this (...) = NULL");
2004 return {};
2005 }
2006
2007 /* Given TYPE, a structure/union,
2008 return 1 if the component named NAME from the ultimate target
2009 structure/union is defined, otherwise, return 0. */
2010
2011 static int
2012 check_field (struct type *type, const char *name,
2013 struct field_of_this_result *is_a_field_of_this)
2014 {
2015 int i;
2016
2017 /* The type may be a stub. */
2018 type = check_typedef (type);
2019
2020 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
2021 {
2022 const char *t_field_name = type->field (i).name ();
2023
2024 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2025 {
2026 is_a_field_of_this->type = type;
2027 is_a_field_of_this->field = &type->field (i);
2028 return 1;
2029 }
2030 }
2031
2032 /* C++: If it was not found as a data field, then try to return it
2033 as a pointer to a method. */
2034
2035 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2036 {
2037 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
2038 {
2039 is_a_field_of_this->type = type;
2040 is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
2041 return 1;
2042 }
2043 }
2044
2045 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2046 if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
2047 return 1;
2048
2049 return 0;
2050 }
2051
2052 /* Behave like lookup_symbol except that NAME is the natural name
2053 (e.g., demangled name) of the symbol that we're looking for. */
2054
2055 static struct block_symbol
2056 lookup_symbol_aux (const char *name, symbol_name_match_type match_type,
2057 const struct block *block,
2058 const domain_search_flags domain, enum language language,
2059 struct field_of_this_result *is_a_field_of_this)
2060 {
2061 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT;
2062
2063 struct block_symbol result;
2064 const struct language_defn *langdef;
2065
2066 if (symbol_lookup_debug)
2067 {
2068 struct objfile *objfile = (block == nullptr
2069 ? nullptr : block->objfile ());
2070
2071 symbol_lookup_debug_printf
2072 ("demangled symbol name = \"%s\", block @ %s (objfile %s)",
2073 name, host_address_to_string (block),
2074 objfile != NULL ? objfile_debug_name (objfile) : "NULL");
2075 symbol_lookup_debug_printf
2076 ("domain name = \"%s\", language = \"%s\")",
2077 domain_name (domain).c_str (), language_str (language));
2078 }
2079
2080 langdef = language_def (language);
2081
2082 /* Search specified block and its superiors. Don't search
2083 STATIC_BLOCK or GLOBAL_BLOCK. */
2084
2085 result = lookup_local_symbol (name, match_type, block, domain, langdef);
2086 if (result.symbol != NULL)
2087 {
2088 symbol_lookup_debug_printf
2089 ("found symbol @ %s (using lookup_local_symbol)",
2090 host_address_to_string (result.symbol));
2091 return result;
2092 }
2093
2094 /* If requested to do so by the caller and if appropriate for LANGUAGE,
2095 check to see if NAME is a field of `this'. */
2096
2097 /* Don't do this check if we are searching for a struct. It will
2098 not be found by check_field, but will be found by other
2099 means. */
2100 if (is_a_field_of_this != NULL && (domain & SEARCH_STRUCT_DOMAIN) == 0)
2101 {
2102 result = lookup_language_this (langdef, block);
2103
2104 if (result.symbol)
2105 {
2106 struct type *t = result.symbol->type ();
2107
2108 /* I'm not really sure that type of this can ever
2109 be typedefed; just be safe. */
2110 t = check_typedef (t);
2111 if (t->is_pointer_or_reference ())
2112 t = t->target_type ();
2113
2114 if (t->code () != TYPE_CODE_STRUCT
2115 && t->code () != TYPE_CODE_UNION)
2116 error (_("Internal error: `%s' is not an aggregate"),
2117 langdef->name_of_this ());
2118
2119 if (check_field (t, name, is_a_field_of_this))
2120 {
2121 symbol_lookup_debug_printf ("no symbol found");
2122 return {};
2123 }
2124 }
2125 }
2126
2127 /* Now do whatever is appropriate for LANGUAGE to look
2128 up static and global variables. */
2129
2130 result = langdef->lookup_symbol_nonlocal (name, block, domain);
2131 if (result.symbol != NULL)
2132 {
2133 symbol_lookup_debug_printf
2134 ("found symbol @ %s (using language lookup_symbol_nonlocal)",
2135 host_address_to_string (result.symbol));
2136 return result;
2137 }
2138
2139 /* Now search all static file-level symbols. Not strictly correct,
2140 but more useful than an error. */
2141
2142 result = lookup_static_symbol (name, domain);
2143 symbol_lookup_debug_printf
2144 ("found symbol @ %s (using lookup_static_symbol)",
2145 result.symbol != NULL ? host_address_to_string (result.symbol) : "NULL");
2146 return result;
2147 }
2148
2149 /* Check to see if the symbol is defined in BLOCK or its superiors.
2150 Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
2151
2152 static struct block_symbol
2153 lookup_local_symbol (const char *name,
2154 symbol_name_match_type match_type,
2155 const struct block *block,
2156 const domain_search_flags domain,
2157 const struct language_defn *langdef)
2158 {
2159 if (block == nullptr)
2160 return {};
2161
2162 const char *scope = block->scope ();
2163
2164 while (!block->is_global_block () && !block->is_static_block ())
2165 {
2166 struct symbol *sym = lookup_symbol_in_block (name, match_type,
2167 block, domain);
2168 if (sym != NULL)
2169 return (struct block_symbol) {sym, block};
2170
2171 struct symbol *function = block->function ();
2172 if (function != nullptr && function->is_template_function ())
2173 {
2174 struct template_symbol *templ = (struct template_symbol *) function;
2175 sym = search_symbol_list (name,
2176 templ->n_template_arguments,
2177 templ->template_arguments);
2178 if (sym != nullptr)
2179 return (struct block_symbol) {sym, block};
2180 }
2181
2182 struct block_symbol blocksym
2183 = langdef->lookup_symbol_local (scope, name, block, domain);
2184 if (blocksym.symbol != nullptr)
2185 return blocksym;
2186
2187 if (block->inlined_p ())
2188 break;
2189 block = block->superblock ();
2190 }
2191
2192 /* We've reached the end of the function without finding a result. */
2193
2194 return {};
2195 }
2196
2197 /* See symtab.h. */
2198
2199 struct symbol *
2200 lookup_symbol_in_block (const char *name, symbol_name_match_type match_type,
2201 const struct block *block,
2202 const domain_search_flags domain)
2203 {
2204 struct symbol *sym;
2205
2206 if (symbol_lookup_debug)
2207 {
2208 struct objfile *objfile
2209 = block == nullptr ? nullptr : block->objfile ();
2210
2211 symbol_lookup_debug_printf_v
2212 ("lookup_symbol_in_block (%s, %s (objfile %s), %s)",
2213 name, host_address_to_string (block),
2214 objfile != nullptr ? objfile_debug_name (objfile) : "NULL",
2215 domain_name (domain).c_str ());
2216 }
2217
2218 lookup_name_info lookup_name (name, match_type);
2219 sym = block_lookup_symbol (block, lookup_name, domain);
2220 if (sym)
2221 {
2222 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = %s",
2223 host_address_to_string (sym));
2224 return sym;
2225 }
2226
2227 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = NULL");
2228 return NULL;
2229 }
2230
2231 /* See symtab.h. */
2232
2233 struct block_symbol
2234 lookup_global_symbol_from_objfile (struct objfile *main_objfile,
2235 enum block_enum block_index,
2236 const char *name,
2237 const domain_search_flags domain)
2238 {
2239 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2240
2241 for (objfile *objfile : main_objfile->separate_debug_objfiles ())
2242 {
2243 struct block_symbol result
2244 = lookup_symbol_in_objfile (objfile, block_index, name, domain);
2245
2246 if (result.symbol != nullptr)
2247 return result;
2248 }
2249
2250 return {};
2251 }
2252
2253 /* Check to see if the symbol is defined in one of the OBJFILE's
2254 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
2255 depending on whether or not we want to search global symbols or
2256 static symbols. */
2257
2258 static struct block_symbol
2259 lookup_symbol_in_objfile_symtabs (struct objfile *objfile,
2260 enum block_enum block_index, const char *name,
2261 const domain_search_flags domain)
2262 {
2263 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2264
2265 symbol_lookup_debug_printf_v
2266 ("lookup_symbol_in_objfile_symtabs (%s, %s, %s, %s)",
2267 objfile_debug_name (objfile),
2268 block_index == GLOBAL_BLOCK ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2269 name, domain_name (domain).c_str ());
2270
2271 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
2272 best_symbol_tracker accum;
2273 for (compunit_symtab &cust : objfile->compunits ())
2274 {
2275 const struct blockvector *bv;
2276 const struct block *block;
2277
2278 bv = cust.blockvector ();
2279 block = bv->block (block_index);
2280 if (accum.search (&cust, block, lookup_name, domain))
2281 break;
2282 }
2283
2284 if (accum.currently_best.symbol != nullptr)
2285 {
2286 symbol_lookup_debug_printf_v
2287 ("lookup_symbol_in_objfile_symtabs (...) = %s (block %s)",
2288 host_address_to_string (accum.currently_best.symbol),
2289 host_address_to_string (accum.currently_best.block));
2290 return accum.currently_best;
2291 }
2292
2293 symbol_lookup_debug_printf_v
2294 ("lookup_symbol_in_objfile_symtabs (...) = NULL");
2295 return {};
2296 }
2297
2298 /* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
2299 Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
2300 and all associated separate debug objfiles.
2301
2302 Normally we only look in OBJFILE, and not any separate debug objfiles
2303 because the outer loop will cause them to be searched too. This case is
2304 different. Here we're called from search_symbols where it will only
2305 call us for the objfile that contains a matching minsym. */
2306
2307 static struct block_symbol
2308 lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
2309 const char *linkage_name,
2310 domain_search_flags domain)
2311 {
2312 enum language lang = current_language->la_language;
2313 struct objfile *main_objfile;
2314
2315 demangle_result_storage storage;
2316 const char *modified_name = demangle_for_lookup (linkage_name, lang, storage);
2317
2318 if (objfile->separate_debug_objfile_backlink)
2319 main_objfile = objfile->separate_debug_objfile_backlink;
2320 else
2321 main_objfile = objfile;
2322
2323 for (::objfile *cur_objfile : main_objfile->separate_debug_objfiles ())
2324 {
2325 struct block_symbol result;
2326
2327 result = lookup_symbol_in_objfile_symtabs (cur_objfile, GLOBAL_BLOCK,
2328 modified_name, domain);
2329 if (result.symbol == NULL)
2330 result = lookup_symbol_in_objfile_symtabs (cur_objfile, STATIC_BLOCK,
2331 modified_name, domain);
2332 if (result.symbol != NULL)
2333 return result;
2334 }
2335
2336 return {};
2337 }
2338
2339 /* A helper function that throws an exception when a symbol was found
2340 in a psymtab but not in a symtab. */
2341
2342 [[noreturn]] static void
2343 error_in_psymtab_expansion (enum block_enum block_index, const char *name,
2344 struct compunit_symtab *cust)
2345 {
2346 error (_("\
2347 Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
2348 %s may be an inlined function, or may be a template function\n \
2349 (if a template, try specifying an instantiation: %s<type>)."),
2350 block_index == GLOBAL_BLOCK ? "global" : "static",
2351 name,
2352 symtab_to_filename_for_display (cust->primary_filetab ()),
2353 name, name);
2354 }
2355
2356 /* A helper function for various lookup routines that interfaces with
2357 the "quick" symbol table functions. */
2358
2359 static struct block_symbol
2360 lookup_symbol_via_quick_fns (struct objfile *objfile,
2361 enum block_enum block_index, const char *name,
2362 const domain_search_flags domain)
2363 {
2364 symbol_lookup_debug_printf_v
2365 ("lookup_symbol_via_quick_fns (%s, %s, %s, %s)",
2366 objfile_debug_name (objfile),
2367 block_index == GLOBAL_BLOCK ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2368 name, domain_name (domain).c_str ());
2369
2370 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
2371 best_symbol_tracker accum;
2372 auto searcher = [&] (compunit_symtab *symtab)
2373 {
2374 const struct blockvector *bv = symtab->blockvector ();
2375 const struct block *block = bv->block (block_index);
2376 /* If the accumulator finds a best symbol, end the search by
2377 returning false; otherwise keep going by returning true. */
2378 return !accum.search (symtab, block, lookup_name, domain);
2379 };
2380
2381 objfile->search (nullptr, &lookup_name, nullptr, searcher,
2382 block_index == GLOBAL_BLOCK
2383 ? SEARCH_GLOBAL_BLOCK
2384 : SEARCH_STATIC_BLOCK,
2385 domain);
2386 if (accum.best_symtab == nullptr)
2387 {
2388 symbol_lookup_debug_printf_v
2389 ("lookup_symbol_via_quick_fns (...) = NULL");
2390 return {};
2391 }
2392 if (accum.currently_best.symbol == nullptr)
2393 error_in_psymtab_expansion (block_index, name, accum.best_symtab);
2394
2395 symbol_lookup_debug_printf_v
2396 ("lookup_symbol_via_quick_fns (...) = %s (block %s)",
2397 host_address_to_string (accum.currently_best.symbol),
2398 host_address_to_string (accum.currently_best.block));
2399
2400 return accum.currently_best;
2401 }
2402
2403 /* See language.h. */
2404
2405 struct block_symbol
2406 language_defn::lookup_symbol_nonlocal (const char *name,
2407 const struct block *block,
2408 const domain_search_flags domain) const
2409 {
2410 struct block_symbol result;
2411
2412 /* NOTE: dje/2014-10-26: The lookup in all objfiles search could skip
2413 the current objfile. Searching the current objfile first is useful
2414 for both matching user expectations as well as performance. */
2415
2416 result = lookup_symbol_in_static_block (name, block, domain);
2417 if (result.symbol != NULL)
2418 return result;
2419
2420 /* If we didn't find a definition for a builtin type in the static block,
2421 search for it now. This is actually the right thing to do and can be
2422 a massive performance win. E.g., when debugging a program with lots of
2423 shared libraries we could search all of them only to find out the
2424 builtin type isn't defined in any of them. This is common for types
2425 like "void". */
2426 if ((domain & SEARCH_TYPE_DOMAIN) != 0)
2427 {
2428 struct gdbarch *gdbarch;
2429
2430 if (block == NULL)
2431 gdbarch = current_inferior ()->arch ();
2432 else
2433 gdbarch = block->gdbarch ();
2434 result.symbol = language_lookup_primitive_type_as_symbol (this,
2435 gdbarch, name);
2436 result.block = NULL;
2437 if (result.symbol != NULL)
2438 return result;
2439 }
2440
2441 return lookup_global_symbol (name, block, domain);
2442 }
2443
2444 /* See symtab.h. */
2445
2446 struct block_symbol
2447 lookup_symbol_in_static_block (const char *name,
2448 const struct block *block,
2449 const domain_search_flags domain)
2450 {
2451 if (block == nullptr)
2452 return {};
2453
2454 const struct block *static_block = block->static_block ();
2455 struct symbol *sym;
2456
2457 if (static_block == NULL)
2458 return {};
2459
2460 if (symbol_lookup_debug)
2461 {
2462 struct objfile *objfile = (block == nullptr
2463 ? nullptr : block->objfile ());
2464
2465 symbol_lookup_debug_printf
2466 ("lookup_symbol_in_static_block (%s, %s (objfile %s), %s)",
2467 name, host_address_to_string (block),
2468 objfile != nullptr ? objfile_debug_name (objfile) : "NULL",
2469 domain_name (domain).c_str ());
2470 }
2471
2472 sym = lookup_symbol_in_block (name,
2473 symbol_name_match_type::FULL,
2474 static_block, domain);
2475 symbol_lookup_debug_printf ("lookup_symbol_in_static_block (...) = %s",
2476 sym != NULL
2477 ? host_address_to_string (sym) : "NULL");
2478 return (struct block_symbol) {sym, static_block};
2479 }
2480
2481 /* Perform the standard symbol lookup of NAME in OBJFILE:
2482 1) First search expanded symtabs, and if not found
2483 2) Search the "quick" symtabs (partial or .gdb_index).
2484 BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */
2485
2486 static struct block_symbol
2487 lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index,
2488 const char *name, const domain_search_flags domain)
2489 {
2490 struct block_symbol result;
2491
2492 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2493
2494 symbol_lookup_debug_printf ("lookup_symbol_in_objfile (%s, %s, %s, %s)",
2495 objfile_debug_name (objfile),
2496 block_index == GLOBAL_BLOCK
2497 ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2498 name, domain_name (domain).c_str ());
2499
2500 result = lookup_symbol_via_quick_fns (objfile, block_index,
2501 name, domain);
2502 symbol_lookup_debug_printf ("lookup_symbol_in_objfile (...) = %s",
2503 result.symbol != NULL
2504 ? host_address_to_string (result.symbol)
2505 : "NULL");
2506 return result;
2507 }
2508
2509 /* This function contains the common code of lookup_{global,static}_symbol.
2510 OBJFILE is only used if BLOCK_INDEX is GLOBAL_SCOPE, in which case it is
2511 the objfile to start the lookup in. */
2512
2513 static struct block_symbol
2514 lookup_global_or_static_symbol (const char *name,
2515 enum block_enum block_index,
2516 struct objfile *objfile,
2517 const domain_search_flags domain)
2518 {
2519 struct symbol_cache *cache = get_symbol_cache (current_program_space);
2520 struct block_symbol result;
2521 struct block_symbol_cache *bsc;
2522 struct symbol_cache_slot *slot;
2523
2524 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2525 gdb_assert (objfile == nullptr || block_index == GLOBAL_BLOCK);
2526
2527 /* First see if we can find the symbol in the cache.
2528 This works because we use the current objfile to qualify the lookup. */
2529 result = symbol_cache_lookup (cache, objfile, block_index, name, domain,
2530 &bsc, &slot);
2531 if (result.symbol != NULL)
2532 {
2533 if (SYMBOL_LOOKUP_FAILED_P (result))
2534 return {};
2535 return result;
2536 }
2537
2538 /* Do a global search (of global blocks, heh). */
2539 if (result.symbol == NULL)
2540 current_program_space->iterate_over_objfiles_in_search_order
2541 ([&result, block_index, name, domain] (struct objfile *objfile_iter)
2542 {
2543 result = lookup_symbol_in_objfile (objfile_iter, block_index,
2544 name, domain);
2545 return result.symbol != nullptr;
2546 },
2547 objfile);
2548
2549 if (result.symbol != NULL)
2550 symbol_cache_mark_found (bsc, slot, objfile, result.symbol, result.block,
2551 domain);
2552 else
2553 symbol_cache_mark_not_found (bsc, slot, objfile, name, domain);
2554
2555 return result;
2556 }
2557
2558 /* See symtab.h. */
2559
2560 struct block_symbol
2561 lookup_static_symbol (const char *name, const domain_search_flags domain)
2562 {
2563 return lookup_global_or_static_symbol (name, STATIC_BLOCK, nullptr, domain);
2564 }
2565
2566 /* See symtab.h. */
2567
2568 struct block_symbol
2569 lookup_global_symbol (const char *name,
2570 const struct block *block,
2571 const domain_search_flags domain)
2572 {
2573 /* If a block was passed in, we want to search the corresponding
2574 global block first. This yields "more expected" behavior, and is
2575 needed to support 'FILENAME'::VARIABLE lookups. */
2576 const struct block *global_block
2577 = block == nullptr ? nullptr : block->global_block ();
2578 symbol *sym = NULL;
2579 if (global_block != nullptr)
2580 {
2581 sym = lookup_symbol_in_block (name,
2582 symbol_name_match_type::FULL,
2583 global_block, domain);
2584 if (sym != NULL && best_symbol (sym, domain))
2585 return { sym, global_block };
2586 }
2587
2588 struct objfile *objfile = nullptr;
2589 if (block != nullptr)
2590 {
2591 objfile = block->objfile ();
2592 if (objfile->separate_debug_objfile_backlink != nullptr)
2593 objfile = objfile->separate_debug_objfile_backlink;
2594 }
2595
2596 block_symbol bs
2597 = lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
2598 if (better_symbol (sym, bs.symbol, domain) == sym)
2599 return { sym, global_block };
2600 else
2601 return bs;
2602 }
2603
2604 /* See symtab.h. */
2605
2606 bool
2607 symbol::matches (domain_search_flags flags) const
2608 {
2609 /* C++ has a typedef for every tag, and the types are in the struct
2610 domain. */
2611 if (language () == language_cplus && (flags & SEARCH_TYPE_DOMAIN) != 0)
2612 flags |= SEARCH_STRUCT_DOMAIN;
2613
2614 return search_flags_matches (flags, m_domain);
2615 }
2616
2617 /* See symtab.h. */
2618
2619 struct type *
2620 lookup_transparent_type (const char *name, domain_search_flags flags)
2621 {
2622 return current_language->lookup_transparent_type (name, flags);
2623 }
2624
2625 /* A helper for basic_lookup_transparent_type that interfaces with the
2626 "quick" symbol table functions. */
2627
2628 static struct type *
2629 basic_lookup_transparent_type_quick (struct objfile *objfile,
2630 enum block_enum block_index,
2631 domain_search_flags flags,
2632 const lookup_name_info &name)
2633 {
2634 struct compunit_symtab *cust;
2635 const struct blockvector *bv;
2636 const struct block *block;
2637 struct symbol *sym;
2638
2639 cust = objfile->lookup_symbol (block_index, name, flags);
2640 if (cust == NULL)
2641 return NULL;
2642
2643 bv = cust->blockvector ();
2644 block = bv->block (block_index);
2645
2646 sym = block_find_symbol (block, name, flags, nullptr);
2647 if (sym == nullptr)
2648 error_in_psymtab_expansion (block_index, name.c_str (), cust);
2649 gdb_assert (!TYPE_IS_OPAQUE (sym->type ()));
2650 return sym->type ();
2651 }
2652
2653 /* The standard implementation of lookup_transparent_type. This code
2654 was modeled on lookup_symbol -- the parts not relevant to looking
2655 up types were just left out. In particular it's assumed here that
2656 types are available in STRUCT_DOMAIN and only in file-static or
2657 global blocks. */
2658
2659 struct type *
2660 basic_lookup_transparent_type (const char *name, domain_search_flags flags)
2661 {
2662 struct type *t;
2663
2664 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
2665
2666 /* Search all the global symbols. */
2667 for (objfile &objfile : current_program_space->objfiles ())
2668 {
2669 t = basic_lookup_transparent_type_quick (&objfile, GLOBAL_BLOCK,
2670 flags, lookup_name);
2671 if (t)
2672 return t;
2673 }
2674
2675 /* Now search the static file-level symbols. Not strictly correct,
2676 but more useful than an error. */
2677 for (objfile &objfile : current_program_space->objfiles ())
2678 {
2679 t = basic_lookup_transparent_type_quick (&objfile, STATIC_BLOCK,
2680 flags, lookup_name);
2681 if (t)
2682 return t;
2683 }
2684
2685 return (struct type *) 0;
2686 }
2687
2688 /* See symtab.h. */
2689
2690 bool
2691 iterate_over_symbols (const struct block *block,
2692 const lookup_name_info &name,
2693 const domain_search_flags domain,
2694 gdb::function_view<symbol_found_callback_ftype> callback)
2695 {
2696 for (struct symbol *sym : block_iterator_range (block, &name))
2697 {
2698 if (sym->matches (domain))
2699 {
2700 struct block_symbol block_sym = {sym, block};
2701
2702 if (!callback (&block_sym))
2703 return false;
2704 }
2705 }
2706 return true;
2707 }
2708
2709 /* Find the compunit symtab associated with PC and SECTION.
2710 This will read in debug info as necessary. */
2711
2712 struct compunit_symtab *
2713 find_compunit_symtab_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
2714 {
2715 struct compunit_symtab *best_cust = NULL;
2716 CORE_ADDR best_cust_range = 0;
2717
2718 /* If we know that this is not a text address, return failure. This is
2719 necessary because we loop based on the block's high and low code
2720 addresses, which do not include the data ranges, and because
2721 we call find_pc_sect_psymtab which has a similar restriction based
2722 on the partial_symtab's texthigh and textlow. */
2723 bound_minimal_symbol msymbol
2724 = lookup_minimal_symbol_by_pc_section (pc, section);
2725 if (msymbol.minsym && msymbol.minsym->data_p ())
2726 return NULL;
2727
2728 /* Search all symtabs for the one whose file contains our address, and which
2729 is the smallest of all the ones containing the address. This is designed
2730 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
2731 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
2732 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
2733
2734 This happens for native ecoff format, where code from included files
2735 gets its own symtab. The symtab for the included file should have
2736 been read in already via the dependency mechanism.
2737 It might be swifter to create several symtabs with the same name
2738 like xcoff does (I'm not sure).
2739
2740 It also happens for objfiles that have their functions reordered.
2741 For these, the symtab we are looking for is not necessarily read in. */
2742
2743 for (objfile &obj_file : current_program_space->objfiles ())
2744 {
2745 for (compunit_symtab &cust : obj_file.compunits ())
2746 {
2747 const struct blockvector *bv = cust.blockvector ();
2748 const struct block *global_block = bv->global_block ();
2749 CORE_ADDR start = global_block->start ();
2750 CORE_ADDR end = global_block->end ();
2751 bool in_range_p = start <= pc && pc < end;
2752 if (!in_range_p)
2753 continue;
2754
2755 if (bv->map () != nullptr)
2756 {
2757 if (bv->map ()->find (pc) == nullptr)
2758 continue;
2759
2760 return &cust;
2761 }
2762
2763 CORE_ADDR range = end - start;
2764 if (best_cust != nullptr
2765 && range >= best_cust_range)
2766 /* Cust doesn't have a smaller range than best_cust, skip it. */
2767 continue;
2768
2769 /* For an objfile that has its functions reordered,
2770 find_pc_psymtab will find the proper partial symbol table
2771 and we simply return its corresponding symtab. */
2772 /* In order to better support objfiles that contain both
2773 stabs and coff debugging info, we continue on if a psymtab
2774 can't be found. */
2775 struct compunit_symtab *result
2776 = obj_file.find_pc_sect_compunit_symtab (msymbol, pc,
2777 section, 0);
2778 if (result != nullptr)
2779 return result;
2780
2781 if (section != 0)
2782 {
2783 struct symbol *found_sym = nullptr;
2784
2785 for (int b_index = GLOBAL_BLOCK;
2786 b_index <= STATIC_BLOCK && found_sym == nullptr;
2787 ++b_index)
2788 {
2789 const struct block *b = bv->block (b_index);
2790 for (struct symbol *sym : block_iterator_range (b))
2791 {
2792 if (matching_obj_sections (sym->obj_section (&obj_file),
2793 section))
2794 {
2795 found_sym = sym;
2796 break;
2797 }
2798 }
2799 }
2800 if (found_sym == nullptr)
2801 continue; /* No symbol in this symtab matches
2802 section. */
2803 }
2804
2805 /* Cust is best found so far, save it. */
2806 best_cust = &cust;
2807 best_cust_range = range;
2808 }
2809 }
2810
2811 if (best_cust != NULL)
2812 return best_cust;
2813
2814 /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
2815
2816 for (objfile &objf : current_program_space->objfiles ())
2817 {
2818 struct compunit_symtab *result
2819 = objf.find_pc_sect_compunit_symtab (msymbol, pc, section, 1);
2820 if (result != NULL)
2821 return result;
2822 }
2823
2824 return NULL;
2825 }
2826
2827 /* Find the compunit symtab associated with PC.
2828 This will read in debug info as necessary.
2829 Backward compatibility, no section. */
2830
2831 struct compunit_symtab *
2832 find_compunit_symtab_for_pc (CORE_ADDR pc)
2833 {
2834 return find_compunit_symtab_for_pc_sect (pc, find_pc_mapped_section (pc));
2835 }
2836
2837 /* See symtab.h. */
2838
2839 struct symbol *
2840 find_symbol_at_address (CORE_ADDR address)
2841 {
2842 for (objfile &objfile : current_program_space->objfiles ())
2843 {
2844 struct symbol *sym = objfile.find_symbol_by_address (address);
2845 if (sym != nullptr)
2846 return sym;
2847 }
2848
2849 return NULL;
2850 }
2851
2852 \f
2853
2854 /* Find the source file and line number for a given PC value and SECTION.
2855 Return a structure containing a symtab pointer, a line number,
2856 and a pc range for the entire source line.
2857 The value's .pc field is NOT the specified pc.
2858 NOTCURRENT nonzero means, if specified pc is on a line boundary,
2859 use the line that ends there. Otherwise, in that case, the line
2860 that begins there is used. */
2861
2862 /* The big complication here is that a line may start in one file, and end just
2863 before the start of another file. This usually occurs when you #include
2864 code in the middle of a subroutine. To properly find the end of a line's PC
2865 range, we must search all symtabs associated with this compilation unit, and
2866 find the one whose first PC is closer than that of the next line in this
2867 symtab. */
2868
2869 struct symtab_and_line
2870 find_sal_for_pc_sect (CORE_ADDR pc, struct obj_section *section, int notcurrent)
2871 {
2872 /* Info on best line seen so far, and where it starts, and its file. */
2873 const linetable_entry *best = NULL;
2874 CORE_ADDR best_end = 0;
2875 struct symtab *best_symtab = 0;
2876
2877 if (section == nullptr)
2878 {
2879 section = find_pc_overlay (pc);
2880 if (section == nullptr)
2881 section = find_pc_section (pc);
2882 }
2883
2884 /* Store here the first line number
2885 of a file which contains the line at the smallest pc after PC.
2886 If we don't find a line whose range contains PC,
2887 we will use a line one less than this,
2888 with a range from the start of that file to the first line's pc. */
2889 const linetable_entry *alt = NULL;
2890
2891 /* Info on best line seen in this file. */
2892
2893 const linetable_entry *prev;
2894
2895 /* If this pc is not from the current frame,
2896 it is the address of the end of a call instruction.
2897 Quite likely that is the start of the following statement.
2898 But what we want is the statement containing the instruction.
2899 Fudge the pc to make sure we get that. */
2900
2901 /* It's tempting to assume that, if we can't find debugging info for
2902 any function enclosing PC, that we shouldn't search for line
2903 number info, either. However, GAS can emit line number info for
2904 assembly files --- very helpful when debugging hand-written
2905 assembly code. In such a case, we'd have no debug info for the
2906 function, but we would have line info. */
2907
2908 if (notcurrent)
2909 pc -= 1;
2910
2911 /* elz: added this because this function returned the wrong
2912 information if the pc belongs to a stub (import/export)
2913 to call a shlib function. This stub would be anywhere between
2914 two functions in the target, and the line info was erroneously
2915 taken to be the one of the line before the pc. */
2916
2917 /* RT: Further explanation:
2918
2919 * We have stubs (trampolines) inserted between procedures.
2920 *
2921 * Example: "shr1" exists in a shared library, and a "shr1" stub also
2922 * exists in the main image.
2923 *
2924 * In the minimal symbol table, we have a bunch of symbols
2925 * sorted by start address. The stubs are marked as "trampoline",
2926 * the others appear as text. E.g.:
2927 *
2928 * Minimal symbol table for main image
2929 * main: code for main (text symbol)
2930 * shr1: stub (trampoline symbol)
2931 * foo: code for foo (text symbol)
2932 * ...
2933 * Minimal symbol table for "shr1" image:
2934 * ...
2935 * shr1: code for shr1 (text symbol)
2936 * ...
2937 *
2938 * So the code below is trying to detect if we are in the stub
2939 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
2940 * and if found, do the symbolization from the real-code address
2941 * rather than the stub address.
2942 *
2943 * Assumptions being made about the minimal symbol table:
2944 * 1. lookup_minimal_symbol_by_pc_section() will return a trampoline only
2945 * if we're really in the trampoline.s If we're beyond it (say
2946 * we're in "foo" in the above example), it'll have a closer
2947 * symbol (the "foo" text symbol for example) and will not
2948 * return the trampoline.
2949 * 2. lookup_minimal_symbol_text() will find a real text symbol
2950 * corresponding to the trampoline, and whose address will
2951 * be different than the trampoline address. I put in a sanity
2952 * check for the address being the same, to avoid an
2953 * infinite recursion.
2954 */
2955 bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
2956 if (msymbol.minsym != NULL)
2957 if (msymbol.minsym->type () == mst_solib_trampoline)
2958 {
2959 bound_minimal_symbol mfunsym
2960 = lookup_minimal_symbol_text (current_program_space,
2961 msymbol.minsym->linkage_name (),
2962 nullptr);
2963
2964 if (mfunsym.minsym == NULL)
2965 /* I eliminated this warning since it is coming out
2966 * in the following situation:
2967 * gdb shmain // test program with shared libraries
2968 * (gdb) break shr1 // function in shared lib
2969 * Warning: In stub for ...
2970 * In the above situation, the shared lib is not loaded yet,
2971 * so of course we can't find the real func/line info,
2972 * but the "break" still works, and the warning is annoying.
2973 * So I commented out the warning. RT */
2974 /* warning ("In stub for %s; unable to find real function/line info",
2975 msymbol->linkage_name ()); */
2976 ;
2977 /* fall through */
2978 else if (mfunsym.value_address ()
2979 == msymbol.value_address ())
2980 /* Avoid infinite recursion */
2981 /* See above comment about why warning is commented out. */
2982 /* warning ("In stub for %s; unable to find real function/line info",
2983 msymbol->linkage_name ()); */
2984 ;
2985 /* fall through */
2986 else
2987 {
2988 /* Detect an obvious case of infinite recursion. If this
2989 should occur, we'd like to know about it, so error out,
2990 fatally. */
2991 if (mfunsym.value_address () == pc)
2992 internal_error (_("Infinite recursion detected in find_sal_for_pc_sect;"
2993 "please file a bug report"));
2994
2995 return find_sal_for_pc (mfunsym.value_address (), 0);
2996 }
2997 }
2998
2999 symtab_and_line val;
3000 val.pspace = current_program_space;
3001
3002 compunit_symtab *cust = find_compunit_symtab_for_pc_sect (pc, section);
3003 if (cust == NULL)
3004 {
3005 /* If no symbol information, return previous pc. */
3006 if (notcurrent)
3007 pc++;
3008 val.pc = pc;
3009 val.section = section;
3010 return val;
3011 }
3012
3013 const blockvector *bv = cust->blockvector ();
3014 struct objfile *objfile = cust->objfile ();
3015
3016 /* Look at all the symtabs that share this blockvector.
3017 They all have the same apriori range, that we found was right;
3018 but they have different line tables. */
3019
3020 for (symtab *iter_s : cust->filetabs ())
3021 {
3022 /* Find the best line in this symtab. */
3023 const linetable *l = iter_s->linetable ();
3024 if (!l)
3025 continue;
3026
3027 int len = l->nitems;
3028 if (len <= 0)
3029 {
3030 /* I think len can be zero if the symtab lacks line numbers
3031 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
3032 I'm not sure which, and maybe it depends on the symbol
3033 reader). */
3034 continue;
3035 }
3036
3037 prev = NULL;
3038 /* Get first line info. */
3039 const linetable_entry *item = l->item;
3040
3041 /* Is this file's first line closer than the first lines of other files?
3042 If so, record this file, and its first line, as best alternate. */
3043 if (item->pc (objfile) > pc
3044 && (!alt || item->unrelocated_pc () < alt->unrelocated_pc ()))
3045 alt = item;
3046
3047 auto pc_compare = [] (const unrelocated_addr &comp_pc,
3048 const struct linetable_entry & lhs)
3049 {
3050 return comp_pc < lhs.unrelocated_pc ();
3051 };
3052
3053 const linetable_entry *first = item;
3054 const linetable_entry *last = item + len;
3055 item = (std::upper_bound
3056 (first, last,
3057 unrelocated_addr (pc - objfile->text_section_offset ()),
3058 pc_compare));
3059 if (item != first)
3060 {
3061 prev = item - 1; /* Found a matching item. */
3062 /* At this point, prev is a line whose address is <= pc. However, we
3063 don't know if ITEM is pointing to the same statement or not. */
3064 while (item != last && prev->line == item->line && !item->is_stmt)
3065 item++;
3066 }
3067
3068 /* At this point, prev points at the line whose start addr is <= pc, and
3069 item points at the next statement. If we ran off the end of the linetable
3070 (pc >= start of the last line), then prev == item. If pc < start of
3071 the first line, prev will not be set. */
3072
3073 /* Is this file's best line closer than the best in the other files?
3074 If so, record this file, and its best line, as best so far. Don't
3075 save prev if it represents the end of a function (i.e. line number
3076 0) instead of a real line. */
3077
3078 if (prev && prev->line
3079 && (!best || prev->unrelocated_pc () > best->unrelocated_pc ()))
3080 {
3081 best = prev;
3082 best_symtab = iter_s;
3083
3084 /* If during the binary search we land on a non-statement entry,
3085 scan backward through entries at the same address to see if
3086 there is an entry marked as is-statement. In theory this
3087 duplication should have been removed from the line table
3088 during construction, this is just a double check. If the line
3089 table has had the duplication removed then this should be
3090 pretty cheap. */
3091 if (!best->is_stmt)
3092 {
3093 const linetable_entry *tmp = best;
3094 while (tmp > first
3095 && (tmp - 1)->unrelocated_pc () == tmp->unrelocated_pc ()
3096 && (tmp - 1)->line != 0 && !tmp->is_stmt)
3097 --tmp;
3098 if (tmp->is_stmt)
3099 best = tmp;
3100 }
3101
3102 /* Discard BEST_END if it's before the PC of the current BEST. */
3103 if (best_end <= best->pc (objfile))
3104 best_end = 0;
3105 }
3106
3107 /* If another line (denoted by ITEM) is in the linetable and its
3108 PC is after BEST's PC, but before the current BEST_END, then
3109 use ITEM's PC as the new best_end. */
3110 if (best && item < last
3111 && item->unrelocated_pc () > best->unrelocated_pc ()
3112 && (best_end == 0 || best_end > item->pc (objfile)))
3113 best_end = item->pc (objfile);
3114 }
3115
3116 if (!best_symtab)
3117 {
3118 /* If we didn't find any line number info, just return zeros.
3119 We used to return alt->line - 1 here, but that could be
3120 anywhere; if we don't have line number info for this PC,
3121 don't make some up. */
3122 val.pc = pc;
3123 }
3124 else if (best->line == 0)
3125 {
3126 /* If our best fit is in a range of PC's for which no line
3127 number info is available (line number is zero) then we didn't
3128 find any valid line information. */
3129 val.pc = pc;
3130 }
3131 else
3132 {
3133 val.is_stmt = best->is_stmt;
3134 val.symtab = best_symtab;
3135 val.line = best->line;
3136 val.pc = best->pc (objfile);
3137 if (best_end && (!alt || best_end < alt->pc (objfile)))
3138 val.end = best_end;
3139 else if (alt)
3140 val.end = alt->pc (objfile);
3141 else
3142 val.end = bv->global_block ()->end ();
3143 }
3144 val.section = section;
3145 return val;
3146 }
3147
3148 /* Backward compatibility (no section). */
3149
3150 struct symtab_and_line
3151 find_sal_for_pc (CORE_ADDR pc, int notcurrent)
3152 {
3153 struct obj_section *section;
3154
3155 section = find_pc_overlay (pc);
3156 if (!pc_in_unmapped_range (pc, section))
3157 return find_sal_for_pc_sect (pc, section, notcurrent);
3158
3159 /* If the original PC was an unmapped address then we translate this to a
3160 mapped address in order to lookup the sal. However, as the user
3161 passed us an unmapped address it makes more sense to return a result
3162 that has the pc and end fields translated to unmapped addresses. */
3163 pc = overlay_mapped_address (pc, section);
3164 symtab_and_line sal = find_sal_for_pc_sect (pc, section, notcurrent);
3165 sal.pc = overlay_unmapped_address (sal.pc, section);
3166 sal.end = overlay_unmapped_address (sal.end, section);
3167 return sal;
3168 }
3169
3170 /* Compare two symtab_and_line entries. Return true if both have
3171 the same line number and the same symtab pointer. That means we
3172 are dealing with two entries from the same line and from the same
3173 source file.
3174
3175 Return false otherwise. */
3176
3177 static bool
3178 sal_line_symtab_matches_p (const symtab_and_line &sal1,
3179 const symtab_and_line &sal2)
3180 {
3181 return sal1.line == sal2.line && sal1.symtab == sal2.symtab;
3182 }
3183
3184 /* See symtah.h. */
3185
3186 std::optional<CORE_ADDR>
3187 find_line_range_start (CORE_ADDR pc)
3188 {
3189 struct symtab_and_line current_sal = find_sal_for_pc (pc, 0);
3190
3191 if (current_sal.line == 0)
3192 return {};
3193
3194 struct symtab_and_line prev_sal = find_sal_for_pc (current_sal.pc - 1, 0);
3195
3196 /* If the previous entry is for a different line, that means we are already
3197 at the entry with the start PC for this line. */
3198 if (!sal_line_symtab_matches_p (prev_sal, current_sal))
3199 return current_sal.pc;
3200
3201 /* Otherwise, keep looking for entries for the same line but with
3202 smaller PC's. */
3203 bool done = false;
3204 CORE_ADDR prev_pc;
3205 while (!done)
3206 {
3207 prev_pc = prev_sal.pc;
3208
3209 prev_sal = find_sal_for_pc (prev_pc - 1, 0);
3210
3211 /* Did we notice a line change? If so, we are done searching. */
3212 if (!sal_line_symtab_matches_p (prev_sal, current_sal))
3213 done = true;
3214 }
3215
3216 return prev_pc;
3217 }
3218
3219 /* See symtab.h. */
3220
3221 struct symtab *
3222 find_symtab_for_pc (CORE_ADDR pc)
3223 {
3224 struct symtab_and_line sal;
3225
3226 /* This always passes zero for NOTCURRENT to find_sal_for_pc.
3227 There are currently no callers that ever pass non-zero. */
3228 sal = find_sal_for_pc (pc, 0);
3229 return sal.symtab;
3230 }
3231 \f
3232 /* See symtab.h. */
3233
3234 symtab *
3235 find_line_symtab (symtab *sym_tab, int line, int *index)
3236 {
3237 int exact = 0; /* Initialized here to avoid a compiler warning. */
3238
3239 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
3240 so far seen. */
3241
3242 int best_index;
3243 const struct linetable *best_linetable;
3244 struct symtab *best_symtab;
3245
3246 /* First try looking it up in the given symtab. */
3247 best_linetable = sym_tab->linetable ();
3248 best_symtab = sym_tab;
3249 best_index = find_line_common (best_linetable, line, &exact, 0);
3250 if (best_index < 0 || !exact)
3251 {
3252 /* Didn't find an exact match. So we better keep looking for
3253 another symtab with the same name. In the case of xcoff,
3254 multiple csects for one source file (produced by IBM's FORTRAN
3255 compiler) produce multiple symtabs (this is unavoidable
3256 assuming csects can be at arbitrary places in memory and that
3257 the GLOBAL_BLOCK of a symtab has a begin and end address). */
3258
3259 /* BEST is the smallest linenumber > LINE so far seen,
3260 or 0 if none has been seen so far.
3261 BEST_INDEX and BEST_LINETABLE identify the item for it. */
3262 int best;
3263
3264 if (best_index >= 0)
3265 best = best_linetable->item[best_index].line;
3266 else
3267 best = 0;
3268
3269 for (objfile &objfile : current_program_space->objfiles ())
3270 objfile.expand_symtabs_with_fullname (symtab_to_fullname (sym_tab));
3271
3272 for (objfile &objfile : current_program_space->objfiles ())
3273 {
3274 for (compunit_symtab &cu : objfile.compunits ())
3275 {
3276 for (symtab *s : cu.filetabs ())
3277 {
3278 const struct linetable *l;
3279 int ind;
3280
3281 if (FILENAME_CMP (sym_tab->filename (), s->filename ()) != 0)
3282 continue;
3283 if (FILENAME_CMP (symtab_to_fullname (sym_tab),
3284 symtab_to_fullname (s)) != 0)
3285 continue;
3286 l = s->linetable ();
3287 ind = find_line_common (l, line, &exact, 0);
3288 if (ind >= 0)
3289 {
3290 if (exact)
3291 {
3292 best_index = ind;
3293 best_linetable = l;
3294 best_symtab = s;
3295 goto done;
3296 }
3297 if (best == 0 || l->item[ind].line < best)
3298 {
3299 best = l->item[ind].line;
3300 best_index = ind;
3301 best_linetable = l;
3302 best_symtab = s;
3303 }
3304 }
3305 }
3306 }
3307 }
3308 }
3309 done:
3310 if (best_index < 0)
3311 return NULL;
3312
3313 if (index)
3314 *index = best_index;
3315
3316 return best_symtab;
3317 }
3318
3319 /* Given SYMTAB, returns all the PCs function in the symtab that
3320 exactly match LINE. Returns an empty vector if there are no exact
3321 matches, but updates BEST_ITEM in this case. */
3322
3323 std::vector<const linetable_entry *>
3324 find_linetable_entries_for_symtab_line (struct symtab *symtab, int line,
3325 const linetable_entry **best_item)
3326 {
3327 int start = 0;
3328 std::vector<const linetable_entry *> result;
3329
3330 /* First, collect all the PCs that are at this line. */
3331 while (1)
3332 {
3333 int was_exact;
3334 int idx;
3335
3336 idx = find_line_common (symtab->linetable (), line, &was_exact,
3337 start);
3338 if (idx < 0)
3339 break;
3340
3341 if (!was_exact)
3342 {
3343 const linetable_entry *item = &symtab->linetable ()->item[idx];
3344
3345 if (*best_item == NULL
3346 || (item->line < (*best_item)->line && item->is_stmt))
3347 *best_item = item;
3348
3349 break;
3350 }
3351
3352 result.push_back (&symtab->linetable ()->item[idx]);
3353 start = idx + 1;
3354 }
3355
3356 return result;
3357 }
3358
3359 \f
3360 /* Set the PC value for a given source file and line number and return true.
3361 Returns false for invalid line number (and sets the PC to 0).
3362 The source file is specified with a struct symtab. */
3363
3364 bool
3365 find_pc_for_line (struct symtab *symtab, int line, CORE_ADDR *pc)
3366 {
3367 const struct linetable *l;
3368 int ind;
3369
3370 *pc = 0;
3371 if (symtab == 0)
3372 return false;
3373
3374 symtab = find_line_symtab (symtab, line, &ind);
3375 if (symtab != NULL)
3376 {
3377 l = symtab->linetable ();
3378 *pc = l->item[ind].pc (symtab->compunit ()->objfile ());
3379 return true;
3380 }
3381 else
3382 return false;
3383 }
3384
3385 /* Find the range of pc values in a line.
3386 Store the starting pc of the line into *STARTPTR
3387 and the ending pc (start of next line) into *ENDPTR.
3388 Returns true to indicate success.
3389 Returns false if could not find the specified line. */
3390
3391 bool
3392 find_pc_range_for_sal (struct symtab_and_line sal, CORE_ADDR *startptr,
3393 CORE_ADDR *endptr)
3394 {
3395 CORE_ADDR startaddr;
3396 struct symtab_and_line found_sal;
3397
3398 startaddr = sal.pc;
3399 if (startaddr == 0 && !find_pc_for_line (sal.symtab, sal.line, &startaddr))
3400 return false;
3401
3402 /* This whole function is based on address. For example, if line 10 has
3403 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
3404 "info line *0x123" should say the line goes from 0x100 to 0x200
3405 and "info line *0x355" should say the line goes from 0x300 to 0x400.
3406 This also insures that we never give a range like "starts at 0x134
3407 and ends at 0x12c". */
3408
3409 found_sal = find_sal_for_pc_sect (startaddr, sal.section, 0);
3410 if (found_sal.line != sal.line)
3411 {
3412 /* The specified line (sal) has zero bytes. */
3413 *startptr = found_sal.pc;
3414 *endptr = found_sal.pc;
3415 }
3416 else
3417 {
3418 *startptr = found_sal.pc;
3419 *endptr = found_sal.end;
3420 }
3421 return true;
3422 }
3423
3424 /* Given a line table and a line number, return the index into the line
3425 table for the pc of the nearest line whose number is >= the specified one.
3426 Return -1 if none is found. The value is >= 0 if it is an index.
3427 START is the index at which to start searching the line table.
3428
3429 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
3430
3431 static int
3432 find_line_common (const linetable *l, int lineno,
3433 int *exact_match, int start)
3434 {
3435 int i;
3436 int len;
3437
3438 /* BEST is the smallest linenumber > LINENO so far seen,
3439 or 0 if none has been seen so far.
3440 BEST_INDEX identifies the item for it. */
3441
3442 int best_index = -1;
3443 int best = 0;
3444
3445 *exact_match = 0;
3446
3447 if (lineno <= 0)
3448 return -1;
3449 if (l == 0)
3450 return -1;
3451
3452 len = l->nitems;
3453 for (i = start; i < len; i++)
3454 {
3455 const linetable_entry *item = &(l->item[i]);
3456
3457 /* Ignore non-statements. */
3458 if (!item->is_stmt)
3459 continue;
3460
3461 if (item->line == lineno)
3462 {
3463 /* Return the first (lowest address) entry which matches. */
3464 *exact_match = 1;
3465 return i;
3466 }
3467
3468 if (item->line > lineno && (best == 0 || item->line < best))
3469 {
3470 best = item->line;
3471 best_index = i;
3472 }
3473 }
3474
3475 /* If we got here, we didn't get an exact match. */
3476 return best_index;
3477 }
3478
3479 bool
3480 find_line_pc_range_for_pc (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
3481 {
3482 struct symtab_and_line sal;
3483
3484 sal = find_sal_for_pc (pc, 0);
3485 *startptr = sal.pc;
3486 *endptr = sal.end;
3487 return sal.symtab != 0;
3488 }
3489
3490 /* Helper for find_function_start_sal. Does most of the work, except
3491 setting the sal's symbol. */
3492
3493 static symtab_and_line
3494 find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
3495 bool funfirstline)
3496 {
3497 symtab_and_line sal = find_sal_for_pc_sect (func_addr, section, 0);
3498
3499 if (funfirstline && sal.symtab != NULL
3500 && (sal.symtab->compunit ()->locations_valid ()
3501 || sal.symtab->language () == language_asm))
3502 {
3503 struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
3504
3505 sal.pc = func_addr;
3506 if (gdbarch_skip_entrypoint_p (gdbarch))
3507 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
3508 return sal;
3509 }
3510
3511 /* We always should have a line for the function start address.
3512 If we don't, something is odd. Create a plain SAL referring
3513 just the PC and hope that skip_prologue_sal (if requested)
3514 can find a line number for after the prologue. */
3515 if (sal.pc < func_addr)
3516 {
3517 sal = {};
3518 sal.pspace = current_program_space;
3519 sal.pc = func_addr;
3520 sal.section = section;
3521 }
3522
3523 if (funfirstline)
3524 skip_prologue_sal (&sal);
3525
3526 return sal;
3527 }
3528
3529 /* See symtab.h. */
3530
3531 symtab_and_line
3532 find_function_start_sal (CORE_ADDR func_addr, obj_section *section, bool funfirstline)
3533 {
3534 symtab_and_line sal
3535 = find_function_start_sal_1 (func_addr, section, funfirstline);
3536
3537 /* find_function_start_sal_1 does a linetable search, so it finds
3538 the symtab and linenumber, but not a symbol. Fill in the
3539 function symbol too. */
3540 sal.symbol = find_symbol_for_pc_sect_maybe_inline (sal.pc, sal.section);
3541
3542 return sal;
3543 }
3544
3545 /* See symtab.h. */
3546
3547 symtab_and_line
3548 find_function_start_sal (symbol *sym, bool funfirstline)
3549 {
3550 symtab_and_line sal
3551 = find_function_start_sal_1 (sym->value_block ()->entry_pc (),
3552 sym->obj_section (sym->objfile ()),
3553 funfirstline);
3554 sal.symbol = sym;
3555 return sal;
3556 }
3557
3558
3559 /* Given a function start address FUNC_ADDR and SYMTAB, find the first
3560 address for that function that has an entry in SYMTAB's line info
3561 table. If such an entry cannot be found, return FUNC_ADDR
3562 unaltered. */
3563
3564 static CORE_ADDR
3565 skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
3566 {
3567 CORE_ADDR func_start, func_end;
3568 const struct linetable *l;
3569 int i;
3570
3571 /* Give up if this symbol has no lineinfo table. */
3572 l = symtab->linetable ();
3573 if (l == NULL)
3574 return func_addr;
3575
3576 /* Get the range for the function's PC values, or give up if we
3577 cannot, for some reason. */
3578 if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
3579 return func_addr;
3580
3581 struct objfile *objfile = symtab->compunit ()->objfile ();
3582
3583 /* Linetable entries are ordered by PC values, see the commentary in
3584 symtab.h where `struct linetable' is defined. Thus, the first
3585 entry whose PC is in the range [FUNC_START..FUNC_END[ is the
3586 address we are looking for. */
3587 for (i = 0; i < l->nitems; i++)
3588 {
3589 const linetable_entry *item = &(l->item[i]);
3590 CORE_ADDR item_pc = item->pc (objfile);
3591
3592 /* Don't use line numbers of zero, they mark special entries in
3593 the table. See the commentary on symtab.h before the
3594 definition of struct linetable. */
3595 if (item->line > 0 && func_start <= item_pc && item_pc < func_end)
3596 return item_pc;
3597 }
3598
3599 return func_addr;
3600 }
3601
3602 /* Try to locate the address where a breakpoint should be placed past the
3603 prologue of function starting at FUNC_ADDR using the line table.
3604
3605 Return the address associated with the first entry in the line-table for
3606 the function starting at FUNC_ADDR which has prologue_end set to true if
3607 such entry exist, otherwise return an empty optional. */
3608
3609 static std::optional<CORE_ADDR>
3610 skip_prologue_using_linetable (CORE_ADDR func_addr)
3611 {
3612 CORE_ADDR start_pc, end_pc;
3613
3614 if (!find_pc_partial_function (func_addr, nullptr, &start_pc, &end_pc))
3615 return {};
3616
3617 const struct symtab_and_line prologue_sal = find_sal_for_pc (start_pc, 0);
3618 if (prologue_sal.symtab != nullptr
3619 && prologue_sal.symtab->language () != language_asm)
3620 {
3621 const linetable *linetable = prologue_sal.symtab->linetable ();
3622
3623 struct objfile *objfile = prologue_sal.symtab->compunit ()->objfile ();
3624
3625 unrelocated_addr unrel_start
3626 = unrelocated_addr (start_pc - objfile->text_section_offset ());
3627 unrelocated_addr unrel_end
3628 = unrelocated_addr (end_pc - objfile->text_section_offset ());
3629
3630 auto it = std::lower_bound
3631 (linetable->item, linetable->item + linetable->nitems, unrel_start,
3632 [] (const linetable_entry &lte, unrelocated_addr pc)
3633 {
3634 return lte.unrelocated_pc () < pc;
3635 });
3636
3637 for (;
3638 (it < linetable->item + linetable->nitems
3639 && it->unrelocated_pc () < unrel_end);
3640 it++)
3641 if (it->prologue_end)
3642 return {it->pc (objfile)};
3643 }
3644
3645 return {};
3646 }
3647
3648 /* Adjust SAL to the first instruction past the function prologue.
3649 If the PC was explicitly specified, the SAL is not changed.
3650 If the line number was explicitly specified then the SAL can still be
3651 updated, unless the language for SAL is assembler, in which case the SAL
3652 will be left unchanged.
3653 If SAL is already past the prologue, then do nothing. */
3654
3655 void
3656 skip_prologue_sal (struct symtab_and_line *sal)
3657 {
3658 /* Do not change the SAL if PC was specified explicitly. */
3659 if (sal->explicit_pc)
3660 return;
3661
3662 /* In assembly code, if the user asks for a specific line then we should
3663 not adjust the SAL. The user already has instruction level
3664 visibility in this case, so selecting a line other than one requested
3665 is likely to be the wrong choice. */
3666 if (sal->symtab != nullptr
3667 && sal->explicit_line
3668 && sal->symtab->language () == language_asm)
3669 return;
3670
3671 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3672
3673 switch_to_program_space_and_thread (sal->pspace);
3674
3675 symbol *sym = find_symbol_for_pc_sect (sal->pc, sal->section);
3676 objfile *objfile;
3677 CORE_ADDR pc;
3678 obj_section *section;
3679 const char *name;
3680
3681 if (sym != NULL)
3682 {
3683 objfile = sym->objfile ();
3684 pc = sym->value_block ()->entry_pc ();
3685 section = sym->obj_section (objfile);
3686 name = sym->linkage_name ();
3687 }
3688 else
3689 {
3690 bound_minimal_symbol msymbol
3691 = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
3692
3693 if (msymbol.minsym == NULL)
3694 return;
3695
3696 objfile = msymbol.objfile;
3697 pc = msymbol.value_address ();
3698 section = msymbol.minsym->obj_section (objfile);
3699 name = msymbol.minsym->linkage_name ();
3700 }
3701
3702 gdbarch *gdbarch = objfile->arch ();
3703
3704 /* Process the prologue in two passes. In the first pass try to skip the
3705 prologue (SKIP is true) and verify there is a real need for it (indicated
3706 by FORCE_SKIP). If no such reason was found run a second pass where the
3707 prologue is not skipped (SKIP is false). */
3708
3709 int skip = 1;
3710 int force_skip = 1;
3711
3712 /* Be conservative - allow direct PC (without skipping prologue) only if we
3713 have proven the CU (Compilation Unit) supports it. sal->SYMTAB does not
3714 have to be set by the caller so we use SYM instead. */
3715 if (sym != NULL
3716 && sym->symtab ()->compunit ()->locations_valid ())
3717 force_skip = 0;
3718
3719 symtab_and_line start_sal;
3720 CORE_ADDR saved_pc = pc;
3721
3722 do
3723 {
3724 pc = saved_pc;
3725
3726 /* Check if the compiler explicitly indicated where a breakpoint should
3727 be placed to skip the prologue. */
3728 if (!ignore_prologue_end_flag && skip)
3729 {
3730 std::optional<CORE_ADDR> linetable_pc
3731 = skip_prologue_using_linetable (pc);
3732 if (linetable_pc)
3733 {
3734 pc = *linetable_pc;
3735 start_sal = find_sal_for_pc_sect (pc, section, 0);
3736 force_skip = 1;
3737 continue;
3738 }
3739 }
3740
3741 /* If the function is in an unmapped overlay, use its unmapped LMA address,
3742 so that gdbarch_skip_prologue has something unique to work on. */
3743 if (section_is_overlay (section) && !section_is_mapped (section))
3744 pc = overlay_unmapped_address (pc, section);
3745
3746 /* Skip "first line" of function (which is actually its prologue). */
3747 pc += gdbarch_deprecated_function_start_offset (gdbarch);
3748 if (gdbarch_skip_entrypoint_p (gdbarch))
3749 pc = gdbarch_skip_entrypoint (gdbarch, pc);
3750 if (skip)
3751 pc = gdbarch_skip_prologue_noexcept (gdbarch, pc);
3752
3753 /* For overlays, map pc back into its mapped VMA range. */
3754 pc = overlay_mapped_address (pc, section);
3755
3756 /* Calculate line number. */
3757 start_sal = find_sal_for_pc_sect (pc, section, 0);
3758
3759 /* Check if gdbarch_skip_prologue left us in mid-line, and the next
3760 line is still part of the same function. */
3761 if (skip && start_sal.pc != pc
3762 && (sym ? (sym->value_block ()->entry_pc () <= start_sal.end
3763 && start_sal.end < sym->value_block()->end ())
3764 : (lookup_minimal_symbol_by_pc_section (start_sal.end, section).minsym
3765 == lookup_minimal_symbol_by_pc_section (pc, section).minsym)))
3766 {
3767 /* First pc of next line */
3768 pc = start_sal.end;
3769 /* Recalculate the line number (might not be N+1). */
3770 start_sal = find_sal_for_pc_sect (pc, section, 0);
3771 }
3772
3773 /* On targets with executable formats that don't have a concept of
3774 constructors (ELF with .init has, PE doesn't), gcc emits a call
3775 to `__main' in `main' between the prologue and before user
3776 code. */
3777 if (gdbarch_skip_main_prologue_p (gdbarch)
3778 && name && strcmp_iw (name, "main") == 0)
3779 {
3780 pc = gdbarch_skip_main_prologue (gdbarch, pc);
3781 /* Recalculate the line number (might not be N+1). */
3782 start_sal = find_sal_for_pc_sect (pc, section, 0);
3783 force_skip = 1;
3784 }
3785 }
3786 while (!force_skip && skip--);
3787
3788 /* If we still don't have a valid source line, try to find the first
3789 PC in the lineinfo table that belongs to the same function. This
3790 happens with COFF debug info, which does not seem to have an
3791 entry in lineinfo table for the code after the prologue which has
3792 no direct relation to source. For example, this was found to be
3793 the case with the DJGPP target using "gcc -gcoff" when the
3794 compiler inserted code after the prologue to make sure the stack
3795 is aligned. */
3796 if (!force_skip && sym && start_sal.symtab == NULL)
3797 {
3798 pc = skip_prologue_using_lineinfo (pc, sym->symtab ());
3799 /* Recalculate the line number. */
3800 start_sal = find_sal_for_pc_sect (pc, section, 0);
3801 }
3802
3803 /* If we're already past the prologue, leave SAL unchanged. Otherwise
3804 forward SAL to the end of the prologue. */
3805 if (sal->pc >= pc)
3806 return;
3807
3808 sal->pc = pc;
3809 sal->section = section;
3810 sal->symtab = start_sal.symtab;
3811 sal->line = start_sal.line;
3812 sal->end = start_sal.end;
3813
3814 /* Check if we are now inside an inlined function. If we can,
3815 use the call site of the function instead. */
3816 const block *function_block = nullptr;
3817
3818 for (const block *b = block_for_pc_sect (sal->pc, sal->section);
3819 b != nullptr;
3820 b = b->superblock ())
3821 if (b->function () != NULL && b->inlined_p ())
3822 function_block = b;
3823 else if (b->function () != NULL)
3824 break;
3825
3826 if (function_block != NULL
3827 && function_block->function ()->line () != 0)
3828 {
3829 sal->line = function_block->function ()->line ();
3830 sal->symtab = function_block->function ()->symtab ();
3831 }
3832 }
3833
3834 /* Given PC at the function's start address, attempt to find the
3835 prologue end using SAL information. Return zero if the skip fails.
3836
3837 A non-optimized prologue traditionally has one SAL for the function
3838 and a second for the function body. A single line function has
3839 them both pointing at the same line.
3840
3841 An optimized prologue is similar but the prologue may contain
3842 instructions (SALs) from the instruction body. Need to skip those
3843 while not getting into the function body.
3844
3845 The functions end point and an increasing SAL line are used as
3846 indicators of the prologue's endpoint.
3847
3848 This code is based on the function refine_prologue_limit
3849 (found in ia64). */
3850
3851 CORE_ADDR
3852 skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
3853 {
3854 struct symtab_and_line prologue_sal;
3855 CORE_ADDR start_pc;
3856 CORE_ADDR end_pc;
3857 const struct block *bl;
3858
3859 /* Get an initial range for the function. */
3860 find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
3861 start_pc += gdbarch_deprecated_function_start_offset (gdbarch);
3862
3863 prologue_sal = find_sal_for_pc (start_pc, 0);
3864 if (prologue_sal.line != 0)
3865 {
3866 /* For languages other than assembly, treat two consecutive line
3867 entries at the same address as a zero-instruction prologue.
3868 The GNU assembler emits separate line notes for each instruction
3869 in a multi-instruction macro, but compilers generally will not
3870 do this. */
3871 if (prologue_sal.symtab->language () != language_asm)
3872 {
3873 struct objfile *objfile
3874 = prologue_sal.symtab->compunit ()->objfile ();
3875 const linetable *linetable = prologue_sal.symtab->linetable ();
3876 gdb_assert (linetable->nitems > 0);
3877 int idx = 0;
3878
3879 /* Skip any earlier lines, and any end-of-sequence marker
3880 from a previous function. */
3881 while (idx + 1 < linetable->nitems
3882 && (linetable->item[idx].pc (objfile) != prologue_sal.pc
3883 || linetable->item[idx].line == 0))
3884 idx++;
3885
3886 if (idx + 1 < linetable->nitems
3887 && linetable->item[idx+1].line != 0
3888 && linetable->item[idx+1].pc (objfile) == start_pc)
3889 return start_pc;
3890 }
3891
3892 /* If there is only one sal that covers the entire function,
3893 then it is probably a single line function, like
3894 "foo(){}". */
3895 if (prologue_sal.end >= end_pc)
3896 return 0;
3897
3898 while (prologue_sal.end < end_pc)
3899 {
3900 struct symtab_and_line sal;
3901
3902 sal = find_sal_for_pc (prologue_sal.end, 0);
3903 if (sal.line == 0)
3904 break;
3905 /* Assume that a consecutive SAL for the same (or larger)
3906 line mark the prologue -> body transition. */
3907 if (sal.line >= prologue_sal.line)
3908 break;
3909 /* Likewise if we are in a different symtab altogether
3910 (e.g. within a file included via #include).  */
3911 if (sal.symtab != prologue_sal.symtab)
3912 break;
3913
3914 /* The line number is smaller. Check that it's from the
3915 same function, not something inlined. If it's inlined,
3916 then there is no point comparing the line numbers. */
3917 bl = block_for_pc (prologue_sal.end);
3918 while (bl)
3919 {
3920 if (bl->inlined_p ())
3921 break;
3922 if (bl->function ())
3923 {
3924 bl = NULL;
3925 break;
3926 }
3927 bl = bl->superblock ();
3928 }
3929 if (bl != NULL)
3930 break;
3931
3932 /* The case in which compiler's optimizer/scheduler has
3933 moved instructions into the prologue. We look ahead in
3934 the function looking for address ranges whose
3935 corresponding line number is less the first one that we
3936 found for the function. This is more conservative then
3937 refine_prologue_limit which scans a large number of SALs
3938 looking for any in the prologue. */
3939 prologue_sal = sal;
3940 }
3941 }
3942
3943 if (prologue_sal.end < end_pc)
3944 /* Return the end of this line, or zero if we could not find a
3945 line. */
3946 return prologue_sal.end;
3947 else
3948 /* Don't return END_PC, which is past the end of the function. */
3949 return prologue_sal.pc;
3950 }
3951
3952 /* See symtab.h. */
3953
3954 std::optional<CORE_ADDR>
3955 find_epilogue_using_linetable (CORE_ADDR func_addr)
3956 {
3957 CORE_ADDR start_pc, end_pc;
3958
3959 if (!find_pc_partial_function (func_addr, nullptr, &start_pc, &end_pc))
3960 return {};
3961
3962 /* While the standard allows for multiple points marked with epilogue_begin
3963 in the same function, for performance reasons, this function will only
3964 find the last address that sets this flag for a given block.
3965
3966 The lines of a function can be described by several line tables in case
3967 there are different files involved. There's a corner case where a
3968 function epilogue is in a different file than a function start, and using
3969 start_pc as argument to find_sal_for_pc will mean we won't find the
3970 epilogue. Instead, use "end_pc - 1" to maximize our chances of picking
3971 the line table containing an epilogue. */
3972 const struct symtab_and_line sal = find_sal_for_pc (end_pc - 1, 0);
3973 if (sal.symtab != nullptr && sal.symtab->language () != language_asm)
3974 {
3975 struct objfile *objfile = sal.symtab->compunit ()->objfile ();
3976 unrelocated_addr unrel_start
3977 = unrelocated_addr (start_pc - objfile->text_section_offset ());
3978 unrelocated_addr unrel_end
3979 = unrelocated_addr (end_pc - objfile->text_section_offset ());
3980
3981 const linetable *linetable = sal.symtab->linetable ();
3982 if (linetable == nullptr || linetable->nitems == 0)
3983 {
3984 /* Empty line table. */
3985 return {};
3986 }
3987
3988 /* Find the first linetable entry after the current function. Note that
3989 this also may be an end_sequence entry. */
3990 auto it = std::lower_bound
3991 (linetable->item, linetable->item + linetable->nitems, unrel_end,
3992 [] (const linetable_entry &lte, unrelocated_addr pc)
3993 {
3994 return lte.unrelocated_pc () < pc;
3995 });
3996 if (it == linetable->item + linetable->nitems)
3997 {
3998 /* We couldn't find either:
3999 - a linetable entry starting the function after the current
4000 function, or
4001 - an end_sequence entry that terminates the current function
4002 at unrel_end.
4003
4004 This can happen when the linetable doesn't describe the full
4005 extent of the function. This can be triggered with:
4006 - compiler-generated debug info, in the cornercase that the pc
4007 with which we call find_sal_for_pc resides in a different file
4008 than unrel_end, or
4009 - invalid dwarf assembly debug info.
4010 In the former case, there's no point in iterating further, simply
4011 return "not found". In the latter case, there's no current
4012 incentive to attempt to support this, so handle this
4013 conservatively and do the same. */
4014 return {};
4015 }
4016
4017 if (unrel_end < it->unrelocated_pc ())
4018 {
4019 /* We found a line entry that starts past the end of the
4020 function. This can happen if the previous entry straddles
4021 two functions, which shouldn't happen with compiler-generated
4022 debug info. Handle the corner case conservatively. */
4023 return {};
4024 }
4025 gdb_assert (unrel_end == it->unrelocated_pc ());
4026
4027 /* Move to the last linetable entry of the current function. */
4028 if (it == &linetable->item[0])
4029 {
4030 /* Doing it-- would introduce undefined behavior, avoid it by
4031 explicitly handling this case. */
4032 return {};
4033 }
4034 it--;
4035 if (it->unrelocated_pc () < unrel_start)
4036 {
4037 /* Not in the current function. */
4038 return {};
4039 }
4040 gdb_assert (it->unrelocated_pc () < unrel_end);
4041
4042 /* We're at the the last linetable entry of the current function. This
4043 is probably where the epilogue begins, but since the DWARF 5 spec
4044 doesn't guarantee it, we iterate backwards through the current
4045 function until we either find the epilogue beginning, or are sure
4046 that it doesn't exist. */
4047 for (; it >= &linetable->item[0]; it--)
4048 {
4049 if (it->unrelocated_pc () < unrel_start)
4050 {
4051 /* No longer in the current function. */
4052 break;
4053 }
4054
4055 if (it->epilogue_begin)
4056 {
4057 /* Found the beginning of the epilogue. */
4058 return {it->pc (objfile)};
4059 }
4060
4061 if (it == &linetable->item[0])
4062 {
4063 /* No more entries in the current function.
4064 Doing it-- would introduce undefined behavior, avoid it by
4065 explicitly handling this case. */
4066 break;
4067 }
4068 }
4069 }
4070
4071 return {};
4072 }
4073
4074 /* See symtab.h. */
4075
4076 symbol *
4077 find_function_alias_target (bound_minimal_symbol msymbol)
4078 {
4079 CORE_ADDR func_addr;
4080 if (!msymbol_is_function (msymbol.objfile, msymbol.minsym, &func_addr))
4081 return NULL;
4082
4083 symbol *sym = find_symbol_for_pc (func_addr);
4084 if (sym != NULL
4085 && sym->loc_class () == LOC_BLOCK
4086 && sym->value_block ()->entry_pc () == func_addr)
4087 return sym;
4088
4089 return NULL;
4090 }
4091
4092 \f
4093 /* If P is of the form "operator[ \t]+..." where `...' is
4094 some legitimate operator text, return a pointer to the
4095 beginning of the substring of the operator text.
4096 Otherwise, return "". */
4097
4098 static const char *
4099 operator_chars (const char *p, const char **end)
4100 {
4101 *end = "";
4102 if (!startswith (p, CP_OPERATOR_STR))
4103 return *end;
4104 p += CP_OPERATOR_LEN;
4105
4106 /* Don't get faked out by `operator' being part of a longer
4107 identifier. */
4108 if (c_isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
4109 return *end;
4110
4111 /* Allow some whitespace between `operator' and the operator symbol. */
4112 while (*p == ' ' || *p == '\t')
4113 p++;
4114
4115 /* Recognize 'operator TYPENAME'. */
4116
4117 if (c_isalpha (*p) || *p == '_' || *p == '$')
4118 {
4119 const char *q = p + 1;
4120
4121 while (c_isalnum (*q) || *q == '_' || *q == '$')
4122 q++;
4123 *end = q;
4124 return p;
4125 }
4126
4127 while (*p)
4128 switch (*p)
4129 {
4130 case '\\': /* regexp quoting */
4131 if (p[1] == '*')
4132 {
4133 if (p[2] == '=') /* 'operator\*=' */
4134 *end = p + 3;
4135 else /* 'operator\*' */
4136 *end = p + 2;
4137 return p;
4138 }
4139 else if (p[1] == '[')
4140 {
4141 if (p[2] == ']')
4142 error (_("mismatched quoting on brackets, "
4143 "try 'operator\\[\\]'"));
4144 else if (p[2] == '\\' && p[3] == ']')
4145 {
4146 *end = p + 4; /* 'operator\[\]' */
4147 return p;
4148 }
4149 else
4150 error (_("nothing is allowed between '[' and ']'"));
4151 }
4152 else
4153 {
4154 /* Gratuitous quote: skip it and move on. */
4155 p++;
4156 continue;
4157 }
4158 break;
4159 case '!':
4160 case '=':
4161 case '*':
4162 case '/':
4163 case '%':
4164 case '^':
4165 if (p[1] == '=')
4166 *end = p + 2;
4167 else
4168 *end = p + 1;
4169 return p;
4170 case '<':
4171 case '>':
4172 case '+':
4173 case '-':
4174 case '&':
4175 case '|':
4176 if (p[0] == '-' && p[1] == '>')
4177 {
4178 /* Struct pointer member operator 'operator->'. */
4179 if (p[2] == '*')
4180 {
4181 *end = p + 3; /* 'operator->*' */
4182 return p;
4183 }
4184 else if (p[2] == '\\')
4185 {
4186 *end = p + 4; /* Hopefully 'operator->\*' */
4187 return p;
4188 }
4189 else
4190 {
4191 *end = p + 2; /* 'operator->' */
4192 return p;
4193 }
4194 }
4195 if (p[1] == '=' || p[1] == p[0])
4196 *end = p + 2;
4197 else
4198 *end = p + 1;
4199 return p;
4200 case '~':
4201 case ',':
4202 *end = p + 1;
4203 return p;
4204 case '(':
4205 if (p[1] != ')')
4206 error (_("`operator ()' must be specified "
4207 "without whitespace in `()'"));
4208 *end = p + 2;
4209 return p;
4210 case '?':
4211 if (p[1] != ':')
4212 error (_("`operator ?:' must be specified "
4213 "without whitespace in `?:'"));
4214 *end = p + 2;
4215 return p;
4216 case '[':
4217 if (p[1] != ']')
4218 error (_("`operator []' must be specified "
4219 "without whitespace in `[]'"));
4220 *end = p + 2;
4221 return p;
4222 default:
4223 error (_("`operator %s' not supported"), p);
4224 break;
4225 }
4226
4227 *end = "";
4228 return *end;
4229 }
4230 \f
4231
4232 /* See class declaration. */
4233
4234 info_sources_filter::info_sources_filter (match_on match_type,
4235 const char *regexp)
4236 : m_match_type (match_type),
4237 m_regexp (regexp)
4238 {
4239 /* Setup the compiled regular expression M_C_REGEXP based on M_REGEXP. */
4240 if (m_regexp != nullptr && *m_regexp != '\0')
4241 {
4242 gdb_assert (m_regexp != nullptr);
4243
4244 int cflags = REG_NOSUB;
4245 #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
4246 cflags |= REG_ICASE;
4247 #endif
4248 m_c_regexp.emplace (m_regexp, cflags, _("Invalid regexp"));
4249 }
4250 }
4251
4252 /* See class declaration. */
4253
4254 bool
4255 info_sources_filter::matches (const char *fullname) const
4256 {
4257 /* Does it match regexp? */
4258 if (m_c_regexp.has_value ())
4259 {
4260 const char *to_match;
4261 std::string dirname;
4262
4263 switch (m_match_type)
4264 {
4265 case match_on::DIRNAME:
4266 dirname = gdb_ldirname (fullname);
4267 to_match = dirname.c_str ();
4268 break;
4269 case match_on::BASENAME:
4270 to_match = lbasename (fullname);
4271 break;
4272 case match_on::FULLNAME:
4273 to_match = fullname;
4274 break;
4275 default:
4276 gdb_assert_not_reached ("bad m_match_type");
4277 }
4278
4279 if (m_c_regexp->exec (to_match, 0, NULL, 0) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284 }
4285
4286 /* Data structure to maintain the state used for printing the results of
4287 the 'info sources' command. */
4288
4289 struct output_source_filename_data
4290 {
4291 /* Create an object for displaying the results of the 'info sources'
4292 command to UIOUT. FILTER must remain valid and unchanged for the
4293 lifetime of this object as this object retains a reference to FILTER. */
4294 output_source_filename_data (struct ui_out *uiout,
4295 const info_sources_filter &filter)
4296 : m_filter (filter),
4297 m_uiout (uiout)
4298 { /* Nothing. */ }
4299
4300 DISABLE_COPY_AND_ASSIGN (output_source_filename_data);
4301
4302 /* Reset enough state of this object so we can match against a new set of
4303 files. The existing regular expression is retained though. */
4304 void reset_output ()
4305 {
4306 m_first = true;
4307 m_filename_seen_cache.clear ();
4308 }
4309
4310 /* Worker for sources_info, outputs the file name formatted for either
4311 cli or mi (based on the current_uiout). In cli mode displays
4312 FULLNAME with a comma separating this name from any previously
4313 printed name (line breaks are added at the comma). In MI mode
4314 outputs a tuple containing DISP_NAME (the files display name),
4315 FULLNAME, and EXPANDED_P (true when this file is from a fully
4316 expanded symtab, otherwise false). */
4317 void output (const char *disp_name, const char *fullname, bool expanded_p);
4318
4319 /* An overload suitable for use as a callback to
4320 quick_symbol_functions::map_symbol_filenames. */
4321 void operator() (const char *filename, const char *fullname)
4322 {
4323 /* The false here indicates that this file is from an unexpanded
4324 symtab. */
4325 output (filename, fullname, false);
4326 }
4327
4328 /* Return true if at least one filename has been printed (after a call to
4329 output) since either this object was created, or the last call to
4330 reset_output. */
4331 bool printed_filename_p () const
4332 {
4333 return !m_first;
4334 }
4335
4336 private:
4337
4338 /* Flag of whether we're printing the first one. */
4339 bool m_first = true;
4340
4341 /* Cache of what we've seen so far. */
4342 filename_seen_cache m_filename_seen_cache;
4343
4344 /* How source filename should be filtered. */
4345 const info_sources_filter &m_filter;
4346
4347 /* The object to which output is sent. */
4348 struct ui_out *m_uiout;
4349 };
4350
4351 /* See comment in class declaration above. */
4352
4353 void
4354 output_source_filename_data::output (const char *disp_name,
4355 const char *fullname,
4356 bool expanded_p)
4357 {
4358 /* Since a single source file can result in several partial symbol
4359 tables, we need to avoid printing it more than once. Note: if
4360 some of the psymtabs are read in and some are not, it gets
4361 printed both under "Source files for which symbols have been
4362 read" and "Source files for which symbols will be read in on
4363 demand". I consider this a reasonable way to deal with the
4364 situation. I'm not sure whether this can also happen for
4365 symtabs; it doesn't hurt to check. */
4366
4367 /* Was NAME already seen? If so, then don't print it again. */
4368 if (m_filename_seen_cache.seen (fullname))
4369 return;
4370
4371 /* If the filter rejects this file then don't print it. */
4372 if (!m_filter.matches (fullname))
4373 return;
4374
4375 ui_out_emit_tuple ui_emitter (m_uiout, nullptr);
4376
4377 /* Print it and reset *FIRST. */
4378 if (!m_first)
4379 m_uiout->text (", ");
4380 m_first = false;
4381
4382 m_uiout->wrap_hint (0);
4383 if (m_uiout->is_mi_like_p ())
4384 {
4385 m_uiout->field_string ("file", disp_name, file_name_style.style ());
4386 if (fullname != nullptr)
4387 m_uiout->field_string ("fullname", fullname,
4388 file_name_style.style ());
4389 m_uiout->field_string ("debug-fully-read",
4390 (expanded_p ? "true" : "false"));
4391 }
4392 else
4393 {
4394 if (fullname == nullptr)
4395 fullname = disp_name;
4396 m_uiout->field_string ("fullname", fullname,
4397 file_name_style.style ());
4398 }
4399 }
4400
4401 /* For the 'info sources' command, what part of the file names should we be
4402 matching the user supplied regular expression against? */
4403
4404 struct filename_partial_match_opts
4405 {
4406 /* Only match the directory name part. */
4407 bool dirname = false;
4408
4409 /* Only match the basename part. */
4410 bool basename = false;
4411 };
4412
4413 using isrc_flag_option_def
4414 = gdb::option::flag_option_def<filename_partial_match_opts>;
4415
4416 static const gdb::option::option_def info_sources_option_defs[] = {
4417
4418 isrc_flag_option_def {
4419 "dirname",
4420 [] (filename_partial_match_opts *opts) { return &opts->dirname; },
4421 N_("Show only the files having a dirname matching REGEXP."),
4422 },
4423
4424 isrc_flag_option_def {
4425 "basename",
4426 [] (filename_partial_match_opts *opts) { return &opts->basename; },
4427 N_("Show only the files having a basename matching REGEXP."),
4428 },
4429
4430 };
4431
4432 /* Create an option_def_group for the "info sources" options, with
4433 ISRC_OPTS as context. */
4434
4435 static inline gdb::option::option_def_group
4436 make_info_sources_options_def_group (filename_partial_match_opts *isrc_opts)
4437 {
4438 return {{info_sources_option_defs}, isrc_opts};
4439 }
4440
4441 /* Completer for "info sources". */
4442
4443 static void
4444 info_sources_command_completer (cmd_list_element *ignore,
4445 completion_tracker &tracker,
4446 const char *text, const char *word)
4447 {
4448 const auto group = make_info_sources_options_def_group (nullptr);
4449 if (gdb::option::complete_options
4450 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
4451 return;
4452 }
4453
4454 /* See symtab.h. */
4455
4456 void
4457 info_sources_worker (struct ui_out *uiout,
4458 bool group_by_objfile,
4459 const info_sources_filter &filter)
4460 {
4461 output_source_filename_data data (uiout, filter);
4462
4463 ui_out_emit_list results_emitter (uiout, "files");
4464 std::optional<ui_out_emit_tuple> output_tuple;
4465 std::optional<ui_out_emit_list> sources_list;
4466
4467 gdb_assert (group_by_objfile || uiout->is_mi_like_p ());
4468
4469 for (objfile &objfile : current_program_space->objfiles ())
4470 {
4471 if (group_by_objfile)
4472 {
4473 output_tuple.emplace (uiout, nullptr);
4474 uiout->field_string ("filename", objfile_name (&objfile),
4475 file_name_style.style ());
4476 uiout->text (":\n");
4477 bool debug_fully_readin = !objfile.has_unexpanded_symtabs ();
4478 if (uiout->is_mi_like_p ())
4479 {
4480 const char *debug_info_state;
4481 if (objfile.has_symbols ())
4482 {
4483 if (debug_fully_readin)
4484 debug_info_state = "fully-read";
4485 else
4486 debug_info_state = "partially-read";
4487 }
4488 else
4489 debug_info_state = "none";
4490 current_uiout->field_string ("debug-info", debug_info_state);
4491 }
4492 else
4493 {
4494 if (!debug_fully_readin)
4495 uiout->text ("(Full debug information has not yet been read "
4496 "for this file.)\n");
4497 if (!objfile.has_symbols ())
4498 uiout->text ("(Objfile has no debug information.)\n");
4499 uiout->text ("\n");
4500 }
4501 sources_list.emplace (uiout, "sources");
4502 }
4503
4504 for (compunit_symtab &cu : objfile.compunits ())
4505 {
4506 for (symtab *s : cu.filetabs ())
4507 {
4508 const char *file = symtab_to_filename_for_display (s);
4509 const char *fullname = symtab_to_fullname (s);
4510 data.output (file, fullname, true);
4511 }
4512 }
4513
4514 if (group_by_objfile)
4515 {
4516 objfile.map_symbol_filenames (data, true /* need_fullname */);
4517 if (data.printed_filename_p ())
4518 uiout->text ("\n\n");
4519 data.reset_output ();
4520 sources_list.reset ();
4521 output_tuple.reset ();
4522 }
4523 }
4524
4525 if (!group_by_objfile)
4526 {
4527 data.reset_output ();
4528 current_program_space->map_symbol_filenames (data,
4529 true /*need_fullname*/);
4530 }
4531 }
4532
4533 /* Implement the 'info sources' command. */
4534
4535 static void
4536 info_sources_command (const char *args, int from_tty)
4537 {
4538 if (!have_full_symbols (current_program_space)
4539 && !have_partial_symbols (current_program_space))
4540 error (_("No symbol table is loaded. Use the \"file\" command."));
4541
4542 filename_partial_match_opts match_opts;
4543 auto group = make_info_sources_options_def_group (&match_opts);
4544 gdb::option::process_options
4545 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
4546
4547 if (match_opts.dirname && match_opts.basename)
4548 error (_("You cannot give both -basename and -dirname to 'info sources'."));
4549
4550 const char *regex = nullptr;
4551 if (args != NULL && *args != '\000')
4552 regex = args;
4553
4554 if ((match_opts.dirname || match_opts.basename) && regex == nullptr)
4555 error (_("Missing REGEXP for 'info sources'."));
4556
4557 info_sources_filter::match_on match_type;
4558 if (match_opts.dirname)
4559 match_type = info_sources_filter::match_on::DIRNAME;
4560 else if (match_opts.basename)
4561 match_type = info_sources_filter::match_on::BASENAME;
4562 else
4563 match_type = info_sources_filter::match_on::FULLNAME;
4564
4565 info_sources_filter filter (match_type, regex);
4566 info_sources_worker (current_uiout, true, filter);
4567 }
4568
4569 /* Compare FILE against all the entries of FILENAMES. If BASENAMES is
4570 true compare only lbasename of FILENAMES. */
4571
4572 static bool
4573 file_matches (const char *file,
4574 const std::vector<gdb::unique_xmalloc_ptr<char>> &filenames,
4575 bool basenames)
4576 {
4577 if (filenames.empty ())
4578 return true;
4579
4580 for (const auto &name : filenames)
4581 {
4582 const char *lname = (basenames ? lbasename (name.get ()) : name.get ());
4583 if (compare_filenames_for_search (file, lname))
4584 return true;
4585 }
4586
4587 return false;
4588 }
4589
4590 /* Helper function for std::sort on symbol_search objects. Can only sort
4591 symbols, not minimal symbols. */
4592
4593 int
4594 symbol_search::compare_search_syms (const symbol_search &sym_a,
4595 const symbol_search &sym_b)
4596 {
4597 int c;
4598
4599 c = FILENAME_CMP (sym_a.symbol->symtab ()->filename (),
4600 sym_b.symbol->symtab ()->filename ());
4601 if (c != 0)
4602 return c;
4603
4604 if (sym_a.block != sym_b.block)
4605 return sym_a.block - sym_b.block;
4606
4607 c = strcmp (sym_a.symbol->print_name (), sym_b.symbol->print_name ());
4608
4609 if (c != 0)
4610 return c;
4611
4612 /* These two symbols have the same name. It is possible, with types,
4613 that we can see two symbols with the same name, but different types,
4614 consider in C: 'typedef struct foo { ... } foo;' which creates a
4615 'struct foo' type and a 'foo' typedef type. For now this is the only
4616 case we handle. In all other cases, we treat symbols with the same
4617 name as being the same.
4618
4619
4620 First, check the types, if they are the same, then consider these
4621 symbols as the same. */
4622 if (sym_a.symbol->type ()->code () == sym_b.symbol->type ()->code ())
4623 return 0;
4624
4625 /* The types are different, but if neither is a typedef then we still
4626 consider these symbols as the same. */
4627 if (sym_a.symbol->type ()->code () != TYPE_CODE_TYPEDEF
4628 && sym_b.symbol->type ()->code () != TYPE_CODE_TYPEDEF)
4629 return 0;
4630
4631 /* The symbols have different types, and one is a typedef. They cannot
4632 both be typedefs or we'd have taken the "types are the same" exit path
4633 above. If the two types are defined on different lines then order by
4634 line number. As line numbers are unsigned, don't subtract one from
4635 the other in order to avoid underflow. */
4636 if (sym_a.symbol->line () != sym_b.symbol->line ())
4637 return (sym_a.symbol->line () > sym_b.symbol->line () ? 1 : -1);
4638
4639 /* The symbols have different types, and one is a typedef, but both
4640 symbols are defined on the same line. For example:
4641
4642 typedef struct foo { int a; } foo;
4643
4644 In this case we sort the typedef after the non-typedef. This is an
4645 arbitrary decision, but I think looks slightly nicer in the 'info
4646 types' output; first we get the type, then the typedef. */
4647 if (sym_a.symbol->type ()->code () == TYPE_CODE_TYPEDEF)
4648 return 1;
4649 else
4650 return -1;
4651 }
4652
4653 /* Returns true if the type_name of symbol_type of SYM matches TREG.
4654 If SYM has no symbol_type or symbol_name, returns false. */
4655
4656 bool
4657 treg_matches_sym_type_name (const compiled_regex &treg,
4658 const struct symbol *sym)
4659 {
4660 struct type *sym_type;
4661 std::string printed_sym_type_name;
4662
4663 symbol_lookup_debug_printf_v ("treg_matches_sym_type_name, sym %s",
4664 sym->natural_name ());
4665
4666 sym_type = sym->type ();
4667 if (sym_type == NULL)
4668 return false;
4669
4670 {
4671 scoped_switch_to_sym_language_if_auto l (sym);
4672
4673 printed_sym_type_name = type_to_string (sym_type);
4674 }
4675
4676 symbol_lookup_debug_printf_v ("sym_type_name %s",
4677 printed_sym_type_name.c_str ());
4678
4679 if (printed_sym_type_name.empty ())
4680 return false;
4681
4682 return treg.exec (printed_sym_type_name.c_str (), 0, NULL, 0) == 0;
4683 }
4684
4685 /* See symtab.h. */
4686
4687 bool
4688 global_symbol_searcher::is_suitable_msymbol
4689 (const domain_search_flags kind, const minimal_symbol *msymbol)
4690 {
4691 switch (msymbol->type ())
4692 {
4693 case mst_data:
4694 case mst_bss:
4695 case mst_file_data:
4696 case mst_file_bss:
4697 return (kind & SEARCH_VAR_DOMAIN) != 0;
4698 case mst_text:
4699 case mst_file_text:
4700 case mst_solib_trampoline:
4701 case mst_text_gnu_ifunc:
4702 return (kind & SEARCH_FUNCTION_DOMAIN) != 0;
4703 default:
4704 return false;
4705 }
4706 }
4707
4708 /* See symtab.h. */
4709
4710 bool
4711 global_symbol_searcher::expand_symtabs
4712 (objfile *objfile, const std::optional<compiled_regex> &preg) const
4713 {
4714 domain_search_flags kind = m_kind;
4715 bool found_msymbol = false;
4716
4717 auto do_file_match = [&] (const char *filename, bool basenames)
4718 {
4719 return file_matches (filename, m_filenames, basenames);
4720 };
4721 search_symtabs_file_matcher file_matcher = nullptr;
4722 if (!m_filenames.empty ())
4723 file_matcher = do_file_match;
4724
4725 objfile->search
4726 (file_matcher,
4727 &lookup_name_info::match_any (),
4728 [&] (const char *symname)
4729 {
4730 return (!preg.has_value ()
4731 || preg->exec (symname, 0, NULL, 0) == 0);
4732 },
4733 NULL,
4734 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
4735 kind);
4736
4737 /* Here, we search through the minimal symbol tables for functions and
4738 variables that match, and force their symbols to be read. This is in
4739 particular necessary for demangled variable names, which are no longer
4740 put into the partial symbol tables. The symbol will then be found
4741 during the scan of symtabs later.
4742
4743 For functions, find_pc_symtab should succeed if we have debug info for
4744 the function, for variables we have to call
4745 lookup_symbol_in_objfile_from_linkage_name to determine if the
4746 variable has debug info. If the lookup fails, set found_msymbol so
4747 that we will rescan to print any matching symbols without debug info.
4748 We only search the objfile the msymbol came from, we no longer search
4749 all objfiles. In large programs (1000s of shared libs) searching all
4750 objfiles is not worth the pain. */
4751 if (m_filenames.empty ()
4752 && (kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN)) != 0)
4753 {
4754 for (minimal_symbol *msymbol : objfile->msymbols ())
4755 {
4756 QUIT;
4757
4758 if (msymbol->created_by_gdb)
4759 continue;
4760
4761 if (is_suitable_msymbol (kind, msymbol))
4762 {
4763 if (!preg.has_value ()
4764 || preg->exec (msymbol->natural_name (), 0,
4765 NULL, 0) == 0)
4766 {
4767 /* An important side-effect of these lookup functions is
4768 to expand the symbol table if msymbol is found, later
4769 in the process we will add matching symbols or
4770 msymbols to the results list, and that requires that
4771 the symbols tables are expanded. */
4772 if ((kind & SEARCH_FUNCTION_DOMAIN) != 0
4773 ? (find_compunit_symtab_for_pc
4774 (msymbol->value_address (objfile)) == NULL)
4775 : (lookup_symbol_in_objfile_from_linkage_name
4776 (objfile, msymbol->linkage_name (),
4777 SEARCH_VFT)
4778 .symbol == NULL))
4779 found_msymbol = true;
4780 }
4781 }
4782 }
4783 }
4784
4785 return found_msymbol;
4786 }
4787
4788 /* See symtab.h. */
4789
4790 bool
4791 global_symbol_searcher::add_matching_symbols
4792 (objfile *objfile,
4793 const std::optional<compiled_regex> &preg,
4794 const std::optional<compiled_regex> &treg,
4795 std::set<symbol_search> *result_set) const
4796 {
4797 domain_search_flags kind = m_kind;
4798
4799 /* Add matching symbols (if not already present). */
4800 for (compunit_symtab &cust : objfile->compunits ())
4801 {
4802 const struct blockvector *bv = cust.blockvector ();
4803
4804 for (block_enum block : { GLOBAL_BLOCK, STATIC_BLOCK })
4805 {
4806 const struct block *b = bv->block (block);
4807
4808 for (struct symbol *sym : block_iterator_range (b))
4809 {
4810 struct symtab *real_symtab = sym->symtab ();
4811
4812 QUIT;
4813
4814 /* Check first sole REAL_SYMTAB->FILENAME. It does
4815 not need to be a substring of symtab_to_fullname as
4816 it may contain "./" etc. */
4817 if (!(file_matches (real_symtab->filename (), m_filenames, false)
4818 || ((basenames_may_differ
4819 || file_matches (lbasename (real_symtab->filename ()),
4820 m_filenames, true))
4821 && file_matches (symtab_to_fullname (real_symtab),
4822 m_filenames, false))))
4823 continue;
4824
4825 if (!sym->matches (kind))
4826 continue;
4827
4828 if (preg.has_value () && preg->exec (sym->natural_name (), 0,
4829 nullptr, 0) != 0)
4830 continue;
4831
4832 if (((sym->domain () == VAR_DOMAIN
4833 || sym->domain () == FUNCTION_DOMAIN)
4834 && treg.has_value ()
4835 && !treg_matches_sym_type_name (*treg, sym)))
4836 continue;
4837
4838 if ((kind & SEARCH_VAR_DOMAIN) != 0)
4839 {
4840 if (sym->loc_class () == LOC_UNRESOLVED
4841 /* LOC_CONST can be used for more than
4842 just enums, e.g., c++ static const
4843 members. We only want to skip enums
4844 here. */
4845 || (sym->loc_class () == LOC_CONST
4846 && (sym->type ()->code () == TYPE_CODE_ENUM)))
4847 continue;
4848 }
4849 if (sym->domain () == MODULE_DOMAIN && sym->line () == 0)
4850 continue;
4851
4852 if (result_set->size () < m_max_search_results)
4853 {
4854 /* Match, insert if not already in the results. */
4855 symbol_search ss (block, sym);
4856 if (result_set->find (ss) == result_set->end ())
4857 result_set->insert (ss);
4858 }
4859 else
4860 return false;
4861 }
4862 }
4863 }
4864
4865 return true;
4866 }
4867
4868 /* See symtab.h. */
4869
4870 bool
4871 global_symbol_searcher::add_matching_msymbols
4872 (objfile *objfile, const std::optional<compiled_regex> &preg,
4873 std::vector<symbol_search> *results) const
4874 {
4875 domain_search_flags kind = m_kind;
4876
4877 for (minimal_symbol *msymbol : objfile->msymbols ())
4878 {
4879 QUIT;
4880
4881 if (msymbol->created_by_gdb)
4882 continue;
4883
4884 if (is_suitable_msymbol (kind, msymbol))
4885 {
4886 if (!preg.has_value ()
4887 || preg->exec (msymbol->natural_name (), 0,
4888 NULL, 0) == 0)
4889 {
4890 /* For functions we can do a quick check of whether the
4891 symbol might be found via find_pc_symtab. */
4892 if ((kind & SEARCH_FUNCTION_DOMAIN) == 0
4893 || (find_compunit_symtab_for_pc
4894 (msymbol->value_address (objfile)) == NULL))
4895 {
4896 if (lookup_symbol_in_objfile_from_linkage_name
4897 (objfile, msymbol->linkage_name (),
4898 SEARCH_VFT).symbol == NULL)
4899 {
4900 /* Matching msymbol, add it to the results list. */
4901 if (results->size () < m_max_search_results)
4902 results->emplace_back (GLOBAL_BLOCK, msymbol, objfile);
4903 else
4904 return false;
4905 }
4906 }
4907 }
4908 }
4909 }
4910
4911 return true;
4912 }
4913
4914 /* See symtab.h. */
4915
4916 std::vector<symbol_search>
4917 global_symbol_searcher::search () const
4918 {
4919 std::optional<compiled_regex> preg;
4920 std::optional<compiled_regex> treg;
4921
4922 if (m_symbol_name_regexp != NULL)
4923 {
4924 const char *symbol_name_regexp = m_symbol_name_regexp;
4925 std::string symbol_name_regexp_holder;
4926
4927 /* Make sure spacing is right for C++ operators.
4928 This is just a courtesy to make the matching less sensitive
4929 to how many spaces the user leaves between 'operator'
4930 and <TYPENAME> or <OPERATOR>. */
4931 const char *opend;
4932 const char *opname = operator_chars (symbol_name_regexp, &opend);
4933
4934 if (*opname)
4935 {
4936 int fix = -1; /* -1 means ok; otherwise number of
4937 spaces needed. */
4938
4939 if (c_isalpha (*opname) || *opname == '_' || *opname == '$')
4940 {
4941 /* There should 1 space between 'operator' and 'TYPENAME'. */
4942 if (opname[-1] != ' ' || opname[-2] == ' ')
4943 fix = 1;
4944 }
4945 else
4946 {
4947 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
4948 if (opname[-1] == ' ')
4949 fix = 0;
4950 }
4951 /* If wrong number of spaces, fix it. */
4952 if (fix >= 0)
4953 {
4954 symbol_name_regexp_holder
4955 = string_printf ("operator%.*s%s", fix, " ", opname);
4956 symbol_name_regexp = symbol_name_regexp_holder.c_str ();
4957 }
4958 }
4959
4960 int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
4961 ? REG_ICASE : 0);
4962 preg.emplace (symbol_name_regexp, cflags,
4963 _("Invalid regexp"));
4964 }
4965
4966 if (m_symbol_type_regexp != NULL)
4967 {
4968 int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
4969 ? REG_ICASE : 0);
4970 treg.emplace (m_symbol_type_regexp, cflags,
4971 _("Invalid regexp"));
4972 }
4973
4974 bool found_msymbol = false;
4975 std::set<symbol_search> result_set;
4976 for (objfile &objfile : current_program_space->objfiles ())
4977 {
4978 /* Expand symtabs within objfile that possibly contain matching
4979 symbols. */
4980 found_msymbol |= expand_symtabs (&objfile, preg);
4981
4982 /* Find matching symbols within OBJFILE and add them in to the
4983 RESULT_SET set. Use a set here so that we can easily detect
4984 duplicates as we go, and can therefore track how many unique
4985 matches we have found so far. */
4986 if (!add_matching_symbols (&objfile, preg, treg, &result_set))
4987 break;
4988 }
4989
4990 /* Convert the result set into a sorted result list, as std::set is
4991 defined to be sorted then no explicit call to std::sort is needed. */
4992 std::vector<symbol_search> result (result_set.begin (), result_set.end ());
4993
4994 /* If there are no debug symbols, then add matching minsyms. But if the
4995 user wants to see symbols matching a type regexp, then never give a
4996 minimal symbol, as we assume that a minimal symbol does not have a
4997 type. */
4998 if ((found_msymbol
4999 || (m_filenames.empty () && (m_kind & SEARCH_VAR_DOMAIN) != 0))
5000 && !m_exclude_minsyms
5001 && !treg.has_value ())
5002 {
5003 gdb_assert ((m_kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN))
5004 != 0);
5005 for (objfile &objfile : current_program_space->objfiles ())
5006 if (!add_matching_msymbols (&objfile, preg, &result))
5007 break;
5008 }
5009
5010 return result;
5011 }
5012
5013 /* See symtab.h. */
5014
5015 std::string
5016 symbol_to_info_string (struct symbol *sym, int block)
5017 {
5018 std::string str;
5019
5020 gdb_assert (block == GLOBAL_BLOCK || block == STATIC_BLOCK);
5021
5022 if (block == STATIC_BLOCK
5023 && (sym->domain () == VAR_DOMAIN
5024 || sym->domain () == FUNCTION_DOMAIN))
5025 str += "static ";
5026
5027 /* Typedef that is not a C++ class. */
5028 if (sym->domain () == TYPE_DOMAIN)
5029 {
5030 string_file tmp_stream;
5031
5032 /* FIXME: For C (and C++) we end up with a difference in output here
5033 between how a typedef is printed, and non-typedefs are printed.
5034 The TYPEDEF_PRINT code places a ";" at the end in an attempt to
5035 appear C-like, while TYPE_PRINT doesn't.
5036
5037 For the struct printing case below, things are worse, we force
5038 printing of the ";" in this function, which is going to be wrong
5039 for languages that don't require a ";" between statements. */
5040 if (sym->type ()->code () == TYPE_CODE_TYPEDEF)
5041 typedef_print (sym->type (), sym, &tmp_stream);
5042 else
5043 type_print (sym->type (), "", &tmp_stream, -1);
5044 str += tmp_stream.string ();
5045 }
5046 /* variable, func, or typedef-that-is-c++-class. */
5047 else if (sym->domain () == VAR_DOMAIN || sym->domain () == STRUCT_DOMAIN
5048 || sym->domain () == FUNCTION_DOMAIN)
5049 {
5050 string_file tmp_stream;
5051
5052 type_print (sym->type (),
5053 (sym->loc_class () == LOC_TYPEDEF
5054 ? "" : sym->print_name ()),
5055 &tmp_stream, 0);
5056
5057 str += tmp_stream.string ();
5058 str += ";";
5059 }
5060 /* Printing of modules is currently done here, maybe at some future
5061 point we might want a language specific method to print the module
5062 symbol so that we can customise the output more. */
5063 else if (sym->domain () == MODULE_DOMAIN)
5064 str += sym->print_name ();
5065
5066 return str;
5067 }
5068
5069 /* Helper function for symbol info commands, for example 'info
5070 functions', 'info variables', etc. BLOCK is the type of block the
5071 symbols was found in, either GLOBAL_BLOCK or STATIC_BLOCK. SYM is
5072 the symbol we found. If LAST is not NULL, print file and line
5073 number information for the symbol as well. Skip printing the
5074 filename if it matches LAST. */
5075
5076 static void
5077 print_symbol_info (struct symbol *sym, int block, const char *last)
5078 {
5079 scoped_switch_to_sym_language_if_auto l (sym);
5080 struct symtab *s = sym->symtab ();
5081
5082 if (last != NULL)
5083 {
5084 const char *s_filename = symtab_to_filename_for_display (s);
5085
5086 if (filename_cmp (last, s_filename) != 0)
5087 {
5088 gdb_printf (_("\nFile %ps:\n"),
5089 styled_string (file_name_style.style (),
5090 s_filename));
5091 }
5092
5093 if (sym->line () != 0)
5094 gdb_printf ("%d:\t", sym->line ());
5095 else
5096 gdb_puts ("\t");
5097 }
5098
5099 std::string str = symbol_to_info_string (sym, block);
5100 gdb_printf ("%s\n", str.c_str ());
5101 }
5102
5103 /* This help function for symtab_symbol_info() prints information
5104 for non-debugging symbols to gdb_stdout. */
5105
5106 static void
5107 print_msymbol_info (bound_minimal_symbol msymbol)
5108 {
5109 struct gdbarch *gdbarch = msymbol.objfile->arch ();
5110 const char *tmp;
5111
5112 if (gdbarch_addr_bit (gdbarch) <= 32)
5113 tmp = hex_string_custom (msymbol.value_address ()
5114 & (CORE_ADDR) 0xffffffff,
5115 8);
5116 else
5117 tmp = hex_string_custom (msymbol.value_address (),
5118 16);
5119
5120 ui_file_style sym_style = (msymbol.minsym->text_p ()
5121 ? function_name_style.style ()
5122 : ui_file_style ());
5123
5124 gdb_printf (_("%ps %ps\n"),
5125 styled_string (address_style.style (), tmp),
5126 styled_string (sym_style, msymbol.minsym->print_name ()));
5127 }
5128
5129 /* This is the guts of the commands "info functions", "info types", and
5130 "info variables". It calls search_symbols to find all matches and then
5131 print_[m]symbol_info to print out some useful information about the
5132 matches. */
5133
5134 static void
5135 symtab_symbol_info (bool quiet, bool exclude_minsyms,
5136 const char *regexp, domain_enum kind,
5137 const char *t_regexp, int from_tty)
5138 {
5139 const char *last_filename = "";
5140 int first = 1;
5141
5142 if (regexp != nullptr && *regexp == '\0')
5143 regexp = nullptr;
5144
5145 domain_search_flags flags = to_search_flags (kind);
5146 if (kind == TYPE_DOMAIN)
5147 flags |= SEARCH_STRUCT_DOMAIN;
5148
5149 global_symbol_searcher spec (flags, regexp);
5150 spec.set_symbol_type_regexp (t_regexp);
5151 spec.set_exclude_minsyms (exclude_minsyms);
5152 std::vector<symbol_search> symbols = spec.search ();
5153
5154 if (!quiet)
5155 {
5156 const char *classname;
5157 switch (kind)
5158 {
5159 case VAR_DOMAIN:
5160 classname = "variable";
5161 break;
5162 case FUNCTION_DOMAIN:
5163 classname = "function";
5164 break;
5165 case TYPE_DOMAIN:
5166 classname = "type";
5167 break;
5168 case MODULE_DOMAIN:
5169 classname = "module";
5170 break;
5171 default:
5172 gdb_assert_not_reached ("invalid domain enum");
5173 }
5174
5175 if (regexp != NULL)
5176 {
5177 if (t_regexp != NULL)
5178 gdb_printf
5179 (_("All %ss matching regular expression \"%s\""
5180 " with type matching regular expression \"%s\":\n"),
5181 classname, regexp, t_regexp);
5182 else
5183 gdb_printf (_("All %ss matching regular expression \"%s\":\n"),
5184 classname, regexp);
5185 }
5186 else
5187 {
5188 if (t_regexp != NULL)
5189 gdb_printf
5190 (_("All defined %ss"
5191 " with type matching regular expression \"%s\" :\n"),
5192 classname, t_regexp);
5193 else
5194 gdb_printf (_("All defined %ss:\n"), classname);
5195 }
5196 }
5197
5198 for (const symbol_search &p : symbols)
5199 {
5200 QUIT;
5201
5202 if (p.msymbol.minsym != NULL)
5203 {
5204 if (first)
5205 {
5206 if (!quiet)
5207 gdb_printf (_("\nNon-debugging symbols:\n"));
5208 first = 0;
5209 }
5210 print_msymbol_info (p.msymbol);
5211 }
5212 else
5213 {
5214 print_symbol_info (p.symbol, p.block, last_filename);
5215 last_filename
5216 = symtab_to_filename_for_display (p.symbol->symtab ());
5217 }
5218 }
5219 }
5220
5221 /* Structure to hold the values of the options used by the 'info variables'
5222 and 'info functions' commands. These correspond to the -q, -t, and -n
5223 options. */
5224
5225 struct info_vars_funcs_options
5226 {
5227 bool quiet = false;
5228 bool exclude_minsyms = false;
5229 std::string type_regexp;
5230 };
5231
5232 /* The options used by the 'info variables' and 'info functions'
5233 commands. */
5234
5235 static const gdb::option::option_def info_vars_funcs_options_defs[] = {
5236 gdb::option::boolean_option_def<info_vars_funcs_options> {
5237 "q",
5238 [] (info_vars_funcs_options *opt) { return &opt->quiet; },
5239 nullptr, /* show_cmd_cb */
5240 nullptr /* set_doc */
5241 },
5242
5243 gdb::option::boolean_option_def<info_vars_funcs_options> {
5244 "n",
5245 [] (info_vars_funcs_options *opt) { return &opt->exclude_minsyms; },
5246 nullptr, /* show_cmd_cb */
5247 nullptr /* set_doc */
5248 },
5249
5250 gdb::option::string_option_def<info_vars_funcs_options> {
5251 "t",
5252 [] (info_vars_funcs_options *opt) { return &opt->type_regexp; },
5253 nullptr, /* show_cmd_cb */
5254 nullptr /* set_doc */
5255 }
5256 };
5257
5258 /* Returns the option group used by 'info variables' and 'info
5259 functions'. */
5260
5261 static gdb::option::option_def_group
5262 make_info_vars_funcs_options_def_group (info_vars_funcs_options *opts)
5263 {
5264 return {{info_vars_funcs_options_defs}, opts};
5265 }
5266
5267 /* Command completer for 'info variables' and 'info functions'. */
5268
5269 static void
5270 info_vars_funcs_command_completer (struct cmd_list_element *ignore,
5271 completion_tracker &tracker,
5272 const char *text, const char * /* word */)
5273 {
5274 const auto group
5275 = make_info_vars_funcs_options_def_group (nullptr);
5276 if (gdb::option::complete_options
5277 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
5278 return;
5279
5280 const char *word = advance_to_expression_complete_word_point (tracker, text);
5281 symbol_completer (ignore, tracker, text, word);
5282 }
5283
5284 /* Implement the 'info variables' command. */
5285
5286 static void
5287 info_variables_command (const char *args, int from_tty)
5288 {
5289 info_vars_funcs_options opts;
5290 auto grp = make_info_vars_funcs_options_def_group (&opts);
5291 gdb::option::process_options
5292 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5293 if (args != nullptr && *args == '\0')
5294 args = nullptr;
5295
5296 symtab_symbol_info
5297 (opts.quiet, opts.exclude_minsyms, args, VAR_DOMAIN,
5298 opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
5299 from_tty);
5300 }
5301
5302 /* Implement the 'info functions' command. */
5303
5304 static void
5305 info_functions_command (const char *args, int from_tty)
5306 {
5307 info_vars_funcs_options opts;
5308
5309 auto grp = make_info_vars_funcs_options_def_group (&opts);
5310 gdb::option::process_options
5311 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5312 if (args != nullptr && *args == '\0')
5313 args = nullptr;
5314
5315 symtab_symbol_info
5316 (opts.quiet, opts.exclude_minsyms, args, FUNCTION_DOMAIN,
5317 opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
5318 from_tty);
5319 }
5320
5321 /* Holds the -q option for the 'info types' command. */
5322
5323 struct info_types_options
5324 {
5325 bool quiet = false;
5326 };
5327
5328 /* The options used by the 'info types' command. */
5329
5330 static const gdb::option::option_def info_types_options_defs[] = {
5331 gdb::option::boolean_option_def<info_types_options> {
5332 "q",
5333 [] (info_types_options *opt) { return &opt->quiet; },
5334 nullptr, /* show_cmd_cb */
5335 nullptr /* set_doc */
5336 }
5337 };
5338
5339 /* Returns the option group used by 'info types'. */
5340
5341 static gdb::option::option_def_group
5342 make_info_types_options_def_group (info_types_options *opts)
5343 {
5344 return {{info_types_options_defs}, opts};
5345 }
5346
5347 /* Implement the 'info types' command. */
5348
5349 static void
5350 info_types_command (const char *args, int from_tty)
5351 {
5352 info_types_options opts;
5353
5354 auto grp = make_info_types_options_def_group (&opts);
5355 gdb::option::process_options
5356 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5357 if (args != nullptr && *args == '\0')
5358 args = nullptr;
5359 symtab_symbol_info (opts.quiet, false, args, TYPE_DOMAIN, nullptr,
5360 from_tty);
5361 }
5362
5363 /* Command completer for 'info types' command. */
5364
5365 static void
5366 info_types_command_completer (struct cmd_list_element *ignore,
5367 completion_tracker &tracker,
5368 const char *text, const char * /* word */)
5369 {
5370 const auto group
5371 = make_info_types_options_def_group (nullptr);
5372 if (gdb::option::complete_options
5373 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
5374 return;
5375
5376 const char *word = advance_to_expression_complete_word_point (tracker, text);
5377 symbol_completer (ignore, tracker, text, word);
5378 }
5379
5380 /* Implement the 'info modules' command. */
5381
5382 static void
5383 info_modules_command (const char *args, int from_tty)
5384 {
5385 info_types_options opts;
5386
5387 auto grp = make_info_types_options_def_group (&opts);
5388 gdb::option::process_options
5389 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5390 if (args != nullptr && *args == '\0')
5391 args = nullptr;
5392 symtab_symbol_info (opts.quiet, true, args, MODULE_DOMAIN, nullptr,
5393 from_tty);
5394 }
5395
5396 /* Implement the 'info main' command. */
5397
5398 static void
5399 info_main_command (const char *args, int from_tty)
5400 {
5401 gdb_printf ("%s\n", main_name ());
5402 }
5403
5404 static void
5405 rbreak_command (const char *regexp, int from_tty)
5406 {
5407 gdb::unique_xmalloc_ptr<char> file_name;
5408
5409 if (regexp != nullptr)
5410 {
5411 const char *colon = strchr (regexp, ':');
5412
5413 /* Ignore the colon if it is part of a Windows drive. */
5414 if (HAS_DRIVE_SPEC (regexp)
5415 && (regexp[2] == '/' || regexp[2] == '\\'))
5416 colon = strchr (STRIP_DRIVE_SPEC (regexp), ':');
5417
5418 if (colon && *(colon + 1) != ':')
5419 {
5420 int colon_index = colon - regexp;
5421 while (colon_index > 0 && c_isspace (regexp[colon_index - 1]))
5422 --colon_index;
5423
5424 file_name = make_unique_xstrndup (regexp, colon_index);
5425 regexp = skip_spaces (colon + 1);
5426 }
5427 }
5428
5429 global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regexp);
5430 if (file_name != nullptr)
5431 spec.add_filename (std::move (file_name));
5432 std::vector<symbol_search> symbols = spec.search ();
5433
5434 gdb::unordered_set<std::string> seen_names;
5435 scoped_rbreak_breakpoints finalize;
5436 int err_count = 0;
5437
5438 for (const symbol_search &p : symbols)
5439 {
5440 std::string name;
5441 if (p.msymbol.minsym == nullptr)
5442 {
5443 if (file_name != nullptr)
5444 {
5445 struct symtab *symtab = p.symbol->symtab ();
5446 const char *fullname = symtab_to_fullname (symtab);
5447 name = string_printf ("%s:'%s'", fullname,
5448 p.symbol->linkage_name ());
5449 }
5450 else
5451 name = p.symbol->linkage_name ();
5452 }
5453 else
5454 name = p.msymbol.minsym->linkage_name ();
5455
5456 if (!seen_names.insert (name).second)
5457 continue;
5458
5459 try
5460 {
5461 break_command (name.c_str (), from_tty);
5462 }
5463 catch (const gdb_exception_error &ex)
5464 {
5465 exception_print (gdb_stderr, ex);
5466 ++err_count;
5467 continue;
5468 }
5469
5470 if (p.msymbol.minsym == nullptr)
5471 print_symbol_info (p.symbol, p.block, nullptr);
5472 else
5473 gdb_printf ("<function, no debug info> %s;\n", name.c_str ());
5474 }
5475
5476 int first_bp = finalize.first_breakpoint ();
5477 int last_bp = finalize.last_breakpoint ();
5478
5479 if (last_bp == -1)
5480 gdb_printf (_("No breakpoints made.\n"));
5481 else if (first_bp == last_bp)
5482 gdb_printf (_("Successfully created breakpoint %d.\n"), first_bp);
5483 else
5484 gdb_printf (_("Successfully created breakpoints %d-%d.\n"),
5485 first_bp, last_bp);
5486
5487 if (err_count > 0)
5488 gdb_printf (_("%d breakpoints failed due to errors, see above.\n"),
5489 err_count);
5490 }
5491 \f
5492
5493 /* Evaluate if SYMNAME matches LOOKUP_NAME. */
5494
5495 static int
5496 compare_symbol_name (const char *symbol_name, language symbol_language,
5497 const lookup_name_info &lookup_name,
5498 completion_match_result &match_res)
5499 {
5500 const language_defn *lang = language_def (symbol_language);
5501
5502 symbol_name_matcher_ftype *name_match
5503 = lang->get_symbol_name_matcher (lookup_name);
5504
5505 return name_match (symbol_name, lookup_name, &match_res);
5506 }
5507
5508 /* See symtab.h. */
5509
5510 bool
5511 completion_list_add_name (completion_tracker &tracker,
5512 language symbol_language,
5513 const char *symname,
5514 const lookup_name_info &lookup_name,
5515 const char *text, const char *word)
5516 {
5517 completion_match_result &match_res
5518 = tracker.reset_completion_match_result ();
5519
5520 /* Clip symbols that cannot match. */
5521 if (!compare_symbol_name (symname, symbol_language, lookup_name, match_res))
5522 return false;
5523
5524 /* Refresh SYMNAME from the match string. It's potentially
5525 different depending on language. (E.g., on Ada, the match may be
5526 the encoded symbol name wrapped in "<>"). */
5527 symname = match_res.match.match ();
5528 gdb_assert (symname != NULL);
5529
5530 /* We have a match for a completion, so add SYMNAME to the current list
5531 of matches. Note that the name is moved to freshly malloc'd space. */
5532
5533 {
5534 gdb::unique_xmalloc_ptr<char> completion
5535 = make_completion_match_str (symname, text, word);
5536
5537 /* Here we pass the match-for-lcd object to add_completion. Some
5538 languages match the user text against substrings of symbol
5539 names in some cases. E.g., in C++, "b push_ba" completes to
5540 "std::vector::push_back", "std::string::push_back", etc., and
5541 in this case we want the completion lowest common denominator
5542 to be "push_back" instead of "std::". */
5543 tracker.add_completion (std::move (completion),
5544 &match_res.match_for_lcd, text, word);
5545 }
5546
5547 return true;
5548 }
5549
5550 /* completion_list_add_name wrapper for struct symbol. */
5551
5552 static void
5553 completion_list_add_symbol (completion_tracker &tracker,
5554 symbol *sym,
5555 const lookup_name_info &lookup_name,
5556 const char *text, const char *word)
5557 {
5558 if (!completion_list_add_name (tracker, sym->language (),
5559 sym->natural_name (),
5560 lookup_name, text, word))
5561 return;
5562
5563 /* C++ function symbols include the parameters within both the msymbol
5564 name and the symbol name. The problem is that the msymbol name will
5565 describe the parameters in the most basic way, with typedefs stripped
5566 out, while the symbol name will represent the types as they appear in
5567 the program. This means we will see duplicate entries in the
5568 completion tracker. The following converts the symbol name back to
5569 the msymbol name and removes the msymbol name from the completion
5570 tracker. */
5571 if (sym->language () == language_cplus
5572 && sym->loc_class () == LOC_BLOCK)
5573 {
5574 /* The call to canonicalize returns the empty string if the input
5575 string is already in canonical form, thanks to this we don't
5576 remove the symbol we just added above. */
5577 gdb::unique_xmalloc_ptr<char> str
5578 = cp_canonicalize_string_no_typedefs (sym->natural_name ());
5579 if (str != nullptr)
5580 tracker.remove_completion (str.get ());
5581 }
5582 }
5583
5584 /* completion_list_add_name wrapper for struct minimal_symbol. */
5585
5586 static void
5587 completion_list_add_msymbol (completion_tracker &tracker,
5588 minimal_symbol *sym,
5589 const lookup_name_info &lookup_name,
5590 const char *text, const char *word)
5591 {
5592 completion_list_add_name (tracker, sym->language (),
5593 sym->natural_name (),
5594 lookup_name, text, word);
5595 }
5596
5597
5598 /* ObjC: In case we are completing on a selector, look as the msymbol
5599 again and feed all the selectors into the mill. */
5600
5601 static void
5602 completion_list_objc_symbol (completion_tracker &tracker,
5603 struct minimal_symbol *msymbol,
5604 const lookup_name_info &lookup_name,
5605 const char *text, const char *word)
5606 {
5607 static char *tmp = NULL;
5608 static unsigned int tmplen = 0;
5609
5610 const char *method, *category, *selector;
5611 char *tmp2 = NULL;
5612
5613 method = msymbol->natural_name ();
5614
5615 /* Is it a method? */
5616 if ((method[0] != '-') && (method[0] != '+'))
5617 return;
5618
5619 if (text[0] == '[')
5620 /* Complete on shortened method method. */
5621 completion_list_add_name (tracker, language_objc,
5622 method + 1,
5623 lookup_name,
5624 text, word);
5625
5626 while ((strlen (method) + 1) >= tmplen)
5627 {
5628 if (tmplen == 0)
5629 tmplen = 1024;
5630 else
5631 tmplen *= 2;
5632 tmp = (char *) xrealloc (tmp, tmplen);
5633 }
5634 selector = strchr (method, ' ');
5635 if (selector != NULL)
5636 selector++;
5637
5638 category = strchr (method, '(');
5639
5640 if ((category != NULL) && (selector != NULL))
5641 {
5642 memcpy (tmp, method, (category - method));
5643 tmp[category - method] = ' ';
5644 memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
5645 completion_list_add_name (tracker, language_objc, tmp,
5646 lookup_name, text, word);
5647 if (text[0] == '[')
5648 completion_list_add_name (tracker, language_objc, tmp + 1,
5649 lookup_name, text, word);
5650 }
5651
5652 if (selector != NULL)
5653 {
5654 /* Complete on selector only. */
5655 strcpy (tmp, selector);
5656 tmp2 = strchr (tmp, ']');
5657 if (tmp2 != NULL)
5658 *tmp2 = '\0';
5659
5660 completion_list_add_name (tracker, language_objc, tmp,
5661 lookup_name, text, word);
5662 }
5663 }
5664
5665 /* Break the non-quoted text based on the characters which are in
5666 symbols. FIXME: This should probably be language-specific. */
5667
5668 static const char *
5669 language_search_unquoted_string (const char *text, const char *p)
5670 {
5671 for (; p > text; --p)
5672 {
5673 if (c_isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
5674 continue;
5675 else
5676 {
5677 if ((current_language->la_language == language_objc))
5678 {
5679 if (p[-1] == ':') /* Might be part of a method name. */
5680 continue;
5681 else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
5682 p -= 2; /* Beginning of a method name. */
5683 else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
5684 { /* Might be part of a method name. */
5685 const char *t = p;
5686
5687 /* Seeing a ' ' or a '(' is not conclusive evidence
5688 that we are in the middle of a method name. However,
5689 finding "-[" or "+[" should be pretty un-ambiguous.
5690 Unfortunately we have to find it now to decide. */
5691
5692 while (t > text)
5693 if (c_isalnum (t[-1]) || t[-1] == '_' ||
5694 t[-1] == ' ' || t[-1] == ':' ||
5695 t[-1] == '(' || t[-1] == ')')
5696 --t;
5697 else
5698 break;
5699
5700 if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
5701 p = t - 2; /* Method name detected. */
5702 /* Else we leave with p unchanged. */
5703 }
5704 }
5705 break;
5706 }
5707 }
5708 return p;
5709 }
5710
5711 static void
5712 completion_list_add_fields (completion_tracker &tracker,
5713 struct symbol *sym,
5714 const lookup_name_info &lookup_name,
5715 const char *text, const char *word)
5716 {
5717 if (sym->loc_class () == LOC_TYPEDEF)
5718 {
5719 struct type *t = sym->type ();
5720 enum type_code c = t->code ();
5721 int j;
5722
5723 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
5724 for (j = TYPE_N_BASECLASSES (t); j < t->num_fields (); j++)
5725 if (t->field (j).name ())
5726 completion_list_add_name (tracker, sym->language (),
5727 t->field (j).name (),
5728 lookup_name, text, word);
5729 }
5730 }
5731
5732 /* See symtab.h. */
5733
5734 bool
5735 symbol_is_function_or_method (symbol *sym)
5736 {
5737 switch (sym->type ()->code ())
5738 {
5739 case TYPE_CODE_FUNC:
5740 case TYPE_CODE_METHOD:
5741 return true;
5742 default:
5743 return false;
5744 }
5745 }
5746
5747 /* See symtab.h. */
5748
5749 bool
5750 symbol_is_function_or_method (minimal_symbol *msymbol)
5751 {
5752 switch (msymbol->type ())
5753 {
5754 case mst_text:
5755 case mst_text_gnu_ifunc:
5756 case mst_solib_trampoline:
5757 case mst_file_text:
5758 return true;
5759 default:
5760 return false;
5761 }
5762 }
5763
5764 /* See symtab.h. */
5765
5766 bound_minimal_symbol
5767 find_gnu_ifunc (const symbol *sym)
5768 {
5769 if (sym->loc_class () != LOC_BLOCK)
5770 return {};
5771
5772 lookup_name_info lookup_name (sym->search_name (),
5773 symbol_name_match_type::SEARCH_NAME);
5774 struct objfile *objfile = sym->objfile ();
5775
5776 CORE_ADDR address = sym->value_block ()->entry_pc ();
5777 minimal_symbol *ifunc = NULL;
5778
5779 iterate_over_minimal_symbols (objfile, lookup_name,
5780 [&] (minimal_symbol *minsym)
5781 {
5782 if (minsym->type () == mst_text_gnu_ifunc
5783 || minsym->type () == mst_data_gnu_ifunc)
5784 {
5785 CORE_ADDR msym_addr = minsym->value_address (objfile);
5786 if (minsym->type () == mst_data_gnu_ifunc)
5787 {
5788 struct gdbarch *gdbarch = objfile->arch ();
5789 msym_addr = gdbarch_convert_from_func_ptr_addr
5790 (gdbarch, msym_addr, current_inferior ()->top_target ());
5791 }
5792 if (msym_addr == address)
5793 {
5794 ifunc = minsym;
5795 return true;
5796 }
5797 }
5798 return false;
5799 });
5800
5801 if (ifunc != NULL)
5802 return {ifunc, objfile};
5803 return {};
5804 }
5805
5806 /* Add matching symbols from SYMTAB to the current completion list. */
5807
5808 static void
5809 add_symtab_completions (struct compunit_symtab *cust,
5810 completion_tracker &tracker,
5811 complete_symbol_mode mode,
5812 const lookup_name_info &lookup_name,
5813 const char *text, const char *word,
5814 enum type_code code)
5815 {
5816 int i;
5817
5818 if (cust == NULL)
5819 return;
5820
5821 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
5822 {
5823 QUIT;
5824
5825 const struct block *b = cust->blockvector ()->block (i);
5826 for (struct symbol *sym : block_iterator_range (b))
5827 {
5828 if (completion_skip_symbol (mode, sym))
5829 continue;
5830
5831 if (code == TYPE_CODE_UNDEF
5832 || (sym->domain () == STRUCT_DOMAIN
5833 && sym->type ()->code () == code))
5834 completion_list_add_symbol (tracker, sym,
5835 lookup_name,
5836 text, word);
5837 }
5838 }
5839 }
5840
5841 void
5842 default_collect_symbol_completion_matches_break_on
5843 (completion_tracker &tracker, complete_symbol_mode mode,
5844 symbol_name_match_type name_match_type,
5845 const char *text, const char *word,
5846 const char *break_on, enum type_code code)
5847 {
5848 /* Problem: All of the symbols have to be copied because readline
5849 frees them. I'm not going to worry about this; hopefully there
5850 won't be that many. */
5851
5852 const struct block *b;
5853 const struct block *surrounding_static_block, *surrounding_global_block;
5854 /* The symbol we are completing on. Points in same buffer as text. */
5855 const char *sym_text;
5856
5857 /* Now look for the symbol we are supposed to complete on. */
5858 if (mode == complete_symbol_mode::LINESPEC)
5859 sym_text = text;
5860 else
5861 {
5862 const char *p;
5863 char quote_found;
5864 const char *quote_pos = NULL;
5865
5866 /* First see if this is a quoted string. */
5867 quote_found = '\0';
5868 for (p = text; *p != '\0'; ++p)
5869 {
5870 if (quote_found != '\0')
5871 {
5872 if (*p == quote_found)
5873 /* Found close quote. */
5874 quote_found = '\0';
5875 else if (*p == '\\' && p[1] == quote_found)
5876 /* A backslash followed by the quote character
5877 doesn't end the string. */
5878 ++p;
5879 }
5880 else if (*p == '\'' || *p == '"')
5881 {
5882 quote_found = *p;
5883 quote_pos = p;
5884 }
5885 }
5886 if (quote_found == '\'')
5887 /* A string within single quotes can be a symbol, so complete on it. */
5888 sym_text = quote_pos + 1;
5889 else if (quote_found == '"')
5890 /* A double-quoted string is never a symbol, nor does it make sense
5891 to complete it any other way. */
5892 {
5893 return;
5894 }
5895 else
5896 {
5897 /* It is not a quoted string. Break it based on the characters
5898 which are in symbols. */
5899 while (p > text)
5900 {
5901 if (c_isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
5902 || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
5903 --p;
5904 else
5905 break;
5906 }
5907 sym_text = p;
5908 }
5909 }
5910
5911 lookup_name_info lookup_name (sym_text, name_match_type, true);
5912
5913 /* At this point scan through the misc symbol vectors and add each
5914 symbol you find to the list. Eventually we want to ignore
5915 anything that isn't a text symbol (everything else will be
5916 handled by the psymtab code below). */
5917
5918 if (code == TYPE_CODE_UNDEF)
5919 {
5920 for (objfile &objfile : current_program_space->objfiles ())
5921 {
5922 for (minimal_symbol *msymbol : objfile.msymbols ())
5923 {
5924 QUIT;
5925
5926 if (completion_skip_symbol (mode, msymbol))
5927 continue;
5928
5929 completion_list_add_msymbol (tracker, msymbol, lookup_name,
5930 sym_text, word);
5931
5932 completion_list_objc_symbol (tracker, msymbol, lookup_name,
5933 sym_text, word);
5934 }
5935 }
5936 }
5937
5938 /* Add completions for all currently loaded symbol tables. */
5939 for (objfile &objfile : current_program_space->objfiles ())
5940 {
5941 /* Look through the partial symtabs for all symbols which begin by
5942 matching SYM_TEXT. Expand all CUs that you find to the list. */
5943 objfile.search
5944 (nullptr, &lookup_name, nullptr,
5945 [&] (compunit_symtab *symtab)
5946 {
5947 add_symtab_completions (symtab,
5948 tracker, mode, lookup_name,
5949 sym_text, word, code);
5950 return true;
5951 },
5952 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
5953 SEARCH_ALL_DOMAINS);
5954 }
5955
5956 /* Search upwards from currently selected frame (so that we can
5957 complete on local vars). Also catch fields of types defined in
5958 this places which match our text string. Only complete on types
5959 visible from current context. */
5960
5961 b = get_selected_block (0);
5962 surrounding_static_block = b == nullptr ? nullptr : b->static_block ();
5963 surrounding_global_block = b == nullptr ? nullptr : b->global_block ();
5964 if (surrounding_static_block != NULL)
5965 while (b != surrounding_static_block)
5966 {
5967 QUIT;
5968
5969 for (struct symbol *sym : block_iterator_range (b))
5970 {
5971 if (code == TYPE_CODE_UNDEF)
5972 {
5973 completion_list_add_symbol (tracker, sym, lookup_name,
5974 sym_text, word);
5975 completion_list_add_fields (tracker, sym, lookup_name,
5976 sym_text, word);
5977 }
5978 else if (sym->domain () == STRUCT_DOMAIN
5979 && sym->type ()->code () == code)
5980 completion_list_add_symbol (tracker, sym, lookup_name,
5981 sym_text, word);
5982 }
5983
5984 /* Stop when we encounter an enclosing function. Do not stop for
5985 non-inlined functions - the locals of the enclosing function
5986 are in scope for a nested function. */
5987 if (b->function () != NULL && b->inlined_p ())
5988 break;
5989 b = b->superblock ();
5990 }
5991
5992 /* Add fields from the file's types; symbols will be added below. */
5993
5994 if (code == TYPE_CODE_UNDEF)
5995 {
5996 if (surrounding_static_block != NULL)
5997 for (struct symbol *sym : block_iterator_range (surrounding_static_block))
5998 completion_list_add_fields (tracker, sym, lookup_name,
5999 sym_text, word);
6000
6001 if (surrounding_global_block != NULL)
6002 for (struct symbol *sym : block_iterator_range (surrounding_global_block))
6003 completion_list_add_fields (tracker, sym, lookup_name,
6004 sym_text, word);
6005 }
6006
6007 /* Skip macros if we are completing a struct tag -- arguable but
6008 usually what is expected. */
6009 if (current_language->macro_expansion () == macro_expansion_c
6010 && code == TYPE_CODE_UNDEF)
6011 {
6012 /* This adds a macro's name to the current completion list. */
6013 auto add_macro_name = [&] (const char *macro_name,
6014 const macro_definition *,
6015 macro_source_file *,
6016 int)
6017 {
6018 completion_list_add_name (tracker, language_c, macro_name,
6019 lookup_name, sym_text, word);
6020 };
6021
6022 /* Add any macros visible in the default scope. Note that this
6023 may yield the occasional wrong result, because an expression
6024 might be evaluated in a scope other than the default. For
6025 example, if the user types "break file:line if <TAB>", the
6026 resulting expression will be evaluated at "file:line" -- but
6027 at there does not seem to be a way to detect this at
6028 completion time. */
6029 macro_scope scope = default_macro_scope ();
6030 if (scope.is_valid ())
6031 macro_for_each_in_scope (scope.file, scope.line, add_macro_name);
6032
6033 /* User-defined macros are always visible. */
6034 macro_for_each (macro_user_macros, add_macro_name);
6035 }
6036 }
6037
6038 /* Collect all symbols (regardless of class) which begin by matching
6039 TEXT. */
6040
6041 void
6042 collect_symbol_completion_matches (completion_tracker &tracker,
6043 complete_symbol_mode mode,
6044 symbol_name_match_type name_match_type,
6045 const char *text, const char *word)
6046 {
6047 current_language->collect_symbol_completion_matches (tracker, mode,
6048 name_match_type,
6049 text, word,
6050 TYPE_CODE_UNDEF);
6051 }
6052
6053 /* Like collect_symbol_completion_matches, but only collect
6054 STRUCT_DOMAIN symbols whose type code is CODE. */
6055
6056 void
6057 collect_symbol_completion_matches_type (completion_tracker &tracker,
6058 const char *text, const char *word,
6059 enum type_code code)
6060 {
6061 complete_symbol_mode mode = complete_symbol_mode::EXPRESSION;
6062 symbol_name_match_type name_match_type = symbol_name_match_type::EXPRESSION;
6063
6064 gdb_assert (code == TYPE_CODE_UNION
6065 || code == TYPE_CODE_STRUCT
6066 || code == TYPE_CODE_ENUM);
6067 current_language->collect_symbol_completion_matches (tracker, mode,
6068 name_match_type,
6069 text, word, code);
6070 }
6071
6072 /* Like collect_symbol_completion_matches, but collects a list of
6073 symbols defined in all source files named SRCFILE. */
6074
6075 void
6076 collect_file_symbol_completion_matches (completion_tracker &tracker,
6077 complete_symbol_mode mode,
6078 symbol_name_match_type name_match_type,
6079 const char *text, const char *word,
6080 const char *srcfile)
6081 {
6082 /* The symbol we are completing on. Points in same buffer as text. */
6083 const char *sym_text;
6084
6085 /* Now look for the symbol we are supposed to complete on.
6086 FIXME: This should be language-specific. */
6087 if (mode == complete_symbol_mode::LINESPEC)
6088 sym_text = text;
6089 else
6090 {
6091 const char *p;
6092 char quote_found;
6093 const char *quote_pos = NULL;
6094
6095 /* First see if this is a quoted string. */
6096 quote_found = '\0';
6097 for (p = text; *p != '\0'; ++p)
6098 {
6099 if (quote_found != '\0')
6100 {
6101 if (*p == quote_found)
6102 /* Found close quote. */
6103 quote_found = '\0';
6104 else if (*p == '\\' && p[1] == quote_found)
6105 /* A backslash followed by the quote character
6106 doesn't end the string. */
6107 ++p;
6108 }
6109 else if (*p == '\'' || *p == '"')
6110 {
6111 quote_found = *p;
6112 quote_pos = p;
6113 }
6114 }
6115 if (quote_found == '\'')
6116 /* A string within single quotes can be a symbol, so complete on it. */
6117 sym_text = quote_pos + 1;
6118 else if (quote_found == '"')
6119 /* A double-quoted string is never a symbol, nor does it make sense
6120 to complete it any other way. */
6121 {
6122 return;
6123 }
6124 else
6125 {
6126 /* Not a quoted string. */
6127 sym_text = language_search_unquoted_string (text, p);
6128 }
6129 }
6130
6131 lookup_name_info lookup_name (sym_text, name_match_type, true);
6132
6133 /* Go through symtabs for SRCFILE and check the externs and statics
6134 for symbols which match. */
6135 iterate_over_symtabs (current_program_space, srcfile, [&] (symtab *s)
6136 {
6137 add_symtab_completions (s->compunit (),
6138 tracker, mode, lookup_name,
6139 sym_text, word, TYPE_CODE_UNDEF);
6140 return false;
6141 });
6142 }
6143
6144 /* A helper function for make_source_files_completion_list. It adds
6145 another file name to a list of possible completions, growing the
6146 list as necessary. */
6147
6148 static void
6149 add_filename_to_list (const char *fname, const char *text, const char *word,
6150 completion_list *list)
6151 {
6152 list->emplace_back (make_completion_match_str (fname, text, word));
6153 }
6154
6155 static int
6156 not_interesting_fname (const char *fname)
6157 {
6158 static const char *illegal_aliens[] = {
6159 "_globals_", /* inserted by coff_symtab_read */
6160 NULL
6161 };
6162 int i;
6163
6164 for (i = 0; illegal_aliens[i]; i++)
6165 {
6166 if (filename_cmp (fname, illegal_aliens[i]) == 0)
6167 return 1;
6168 }
6169 return 0;
6170 }
6171
6172 /* An object of this type is passed as the callback argument to
6173 map_partial_symbol_filenames. */
6174 struct add_partial_filename_data
6175 {
6176 struct filename_seen_cache *filename_seen_cache;
6177 const char *text;
6178 const char *word;
6179 int text_len;
6180 completion_list *list;
6181
6182 void operator() (const char *filename, const char *fullname);
6183 };
6184
6185 /* A callback for map_partial_symbol_filenames. */
6186
6187 void
6188 add_partial_filename_data::operator() (const char *filename,
6189 const char *fullname)
6190 {
6191 if (not_interesting_fname (filename))
6192 return;
6193 if (!filename_seen_cache->seen (filename)
6194 && filename_ncmp (filename, text, text_len) == 0)
6195 {
6196 /* This file matches for a completion; add it to the
6197 current list of matches. */
6198 add_filename_to_list (filename, text, word, list);
6199 }
6200 else
6201 {
6202 const char *base_name = lbasename (filename);
6203
6204 if (base_name != filename
6205 && !filename_seen_cache->seen (base_name)
6206 && filename_ncmp (base_name, text, text_len) == 0)
6207 add_filename_to_list (base_name, text, word, list);
6208 }
6209 }
6210
6211 /* Return a list of all source files whose names begin with matching
6212 TEXT. The file names are looked up in the symbol tables of this
6213 program. */
6214
6215 completion_list
6216 make_source_files_completion_list (const char *text)
6217 {
6218 size_t text_len = strlen (text);
6219 completion_list list;
6220 const char *base_name;
6221 struct add_partial_filename_data datum;
6222
6223 if (!have_full_symbols (current_program_space)
6224 && !have_partial_symbols (current_program_space))
6225 return list;
6226
6227 filename_seen_cache filenames_seen;
6228
6229 for (objfile &objfile : current_program_space->objfiles ())
6230 {
6231 for (compunit_symtab &cu : objfile.compunits ())
6232 {
6233 for (symtab *s : cu.filetabs ())
6234 {
6235 if (not_interesting_fname (s->filename ()))
6236 continue;
6237 if (!filenames_seen.seen (s->filename ())
6238 && filename_ncmp (s->filename (), text, text_len) == 0)
6239 {
6240 /* This file matches for a completion; add it to the current
6241 list of matches. */
6242 add_filename_to_list (s->filename (), text, text, &list);
6243 }
6244 else
6245 {
6246 /* NOTE: We allow the user to type a base name when the
6247 debug info records leading directories, but not the other
6248 way around. This is what subroutines of breakpoint
6249 command do when they parse file names. */
6250 base_name = lbasename (s->filename ());
6251 if (base_name != s->filename ()
6252 && !filenames_seen.seen (base_name)
6253 && filename_ncmp (base_name, text, text_len) == 0)
6254 add_filename_to_list (base_name, text, text, &list);
6255 }
6256 }
6257 }
6258 }
6259
6260 datum.filename_seen_cache = &filenames_seen;
6261 datum.text = text;
6262 datum.word = text;
6263 datum.text_len = text_len;
6264 datum.list = &list;
6265 current_program_space->map_symbol_filenames (datum, false /*need_fullname*/);
6266
6267 return list;
6268 }
6269 \f
6270 /* Track MAIN */
6271
6272 /* Return the "main_info" object for the current program space. If
6273 the object has not yet been created, create it and fill in some
6274 default values. */
6275
6276 static main_info *
6277 get_main_info (program_space *pspace)
6278 {
6279 main_info *info = main_progspace_key.get (pspace);
6280
6281 if (info == NULL)
6282 {
6283 /* It may seem strange to store the main name in the progspace
6284 and also in whatever objfile happens to see a main name in
6285 its debug info. The reason for this is mainly historical:
6286 gdb returned "main" as the name even if no function named
6287 "main" was defined the program; and this approach lets us
6288 keep compatibility. */
6289 info = main_progspace_key.emplace (pspace);
6290 }
6291
6292 return info;
6293 }
6294
6295 static void
6296 set_main_name (program_space *pspace, const char *name, enum language lang)
6297 {
6298 main_info *info = get_main_info (pspace);
6299
6300 if (!info->name_of_main.empty ())
6301 {
6302 info->name_of_main.clear ();
6303 info->language_of_main = language_unknown;
6304 }
6305 if (name != NULL)
6306 {
6307 info->name_of_main = name;
6308 info->language_of_main = lang;
6309 }
6310 }
6311
6312 /* Deduce the name of the main procedure, and set NAME_OF_MAIN
6313 accordingly. */
6314
6315 static void
6316 find_main_name (void)
6317 {
6318 const char *new_main_name;
6319 program_space *pspace = current_program_space;
6320
6321 /* First check the objfiles to see whether a debuginfo reader has
6322 picked up the appropriate main name. Historically the main name
6323 was found in a more or less random way; this approach instead
6324 relies on the order of objfile creation -- which still isn't
6325 guaranteed to get the correct answer, but is just probably more
6326 accurate. */
6327 for (objfile &objfile : current_program_space->objfiles ())
6328 {
6329 objfile.compute_main_name ();
6330
6331 if (objfile.per_bfd->name_of_main != NULL)
6332 {
6333 set_main_name (pspace,
6334 objfile.per_bfd->name_of_main,
6335 objfile.per_bfd->language_of_main);
6336 return;
6337 }
6338 }
6339
6340 /* Try to see if the main procedure is in Ada. */
6341 /* FIXME: brobecker/2005-03-07: Another way of doing this would
6342 be to add a new method in the language vector, and call this
6343 method for each language until one of them returns a non-empty
6344 name. This would allow us to remove this hard-coded call to
6345 an Ada function. It is not clear that this is a better approach
6346 at this point, because all methods need to be written in a way
6347 such that false positives never be returned. For instance, it is
6348 important that a method does not return a wrong name for the main
6349 procedure if the main procedure is actually written in a different
6350 language. It is easy to guaranty this with Ada, since we use a
6351 special symbol generated only when the main in Ada to find the name
6352 of the main procedure. It is difficult however to see how this can
6353 be guaranteed for languages such as C, for instance. This suggests
6354 that order of call for these methods becomes important, which means
6355 a more complicated approach. */
6356 new_main_name = ada_main_name ();
6357 if (new_main_name != NULL)
6358 {
6359 set_main_name (pspace, new_main_name, language_ada);
6360 return;
6361 }
6362
6363 new_main_name = d_main_name ();
6364 if (new_main_name != NULL)
6365 {
6366 set_main_name (pspace, new_main_name, language_d);
6367 return;
6368 }
6369
6370 new_main_name = go_main_name ();
6371 if (new_main_name != NULL)
6372 {
6373 set_main_name (pspace, new_main_name, language_go);
6374 return;
6375 }
6376
6377 new_main_name = pascal_main_name ();
6378 if (new_main_name != NULL)
6379 {
6380 set_main_name (pspace, new_main_name, language_pascal);
6381 return;
6382 }
6383
6384 /* The languages above didn't identify the name of the main procedure.
6385 Fallback to "main". */
6386
6387 /* Try to find language for main in psymtabs. */
6388 bool symbol_found_p = false;
6389 current_program_space->iterate_over_objfiles_in_search_order
6390 ([&symbol_found_p, pspace] (objfile *obj)
6391 {
6392 language lang
6393 = obj->lookup_global_symbol_language ("main",
6394 SEARCH_FUNCTION_DOMAIN,
6395 &symbol_found_p);
6396 if (symbol_found_p)
6397 {
6398 set_main_name (pspace, "main", lang);
6399 return 1;
6400 }
6401
6402 return 0;
6403 }, nullptr);
6404
6405 if (symbol_found_p)
6406 return;
6407
6408 set_main_name (pspace, "main", language_unknown);
6409 }
6410
6411 /* See symtab.h. */
6412
6413 const char *
6414 main_name ()
6415 {
6416 main_info *info = get_main_info (current_program_space);
6417
6418 if (info->name_of_main.empty ())
6419 find_main_name ();
6420
6421 return info->name_of_main.c_str ();
6422 }
6423
6424 /* Return the language of the main function. If it is not known,
6425 return language_unknown. */
6426
6427 enum language
6428 main_language (void)
6429 {
6430 main_info *info = get_main_info (current_program_space);
6431
6432 if (info->name_of_main.empty ())
6433 find_main_name ();
6434
6435 return info->language_of_main;
6436 }
6437
6438 \f
6439
6440 /* The next index to hand out in response to a registration request. */
6441
6442 static int next_loc_class_value = LOC_FINAL_VALUE;
6443
6444 /* The maximum number of "loc_class" registrations we support. This is
6445 constant for convenience. */
6446 #define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 11)
6447
6448 /* The objects representing the various "loc_class" values. The elements
6449 from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
6450 elements are those registered at gdb initialization time. */
6451
6452 static struct symbol_impl symbol_impl[MAX_SYMBOL_IMPLS];
6453
6454 /* The globally visible pointer. This is separate from 'symbol_impl'
6455 so that it can be const. */
6456
6457 gdb::array_view<const struct symbol_impl> symbol_impls (symbol_impl);
6458
6459 /* Make sure we saved enough room in struct symbol. */
6460
6461 static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_LOC_CLASS_BITS));
6462
6463 /* Register a computed symbol type. LOC_CLASS must be LOC_COMPUTED. OPS
6464 is the ops vector associated with this index. This returns the new
6465 index, which should be used as the loc_class_index field for symbols
6466 of this type. */
6467
6468 int
6469 register_symbol_computed_impl (location_class loc_class,
6470 const struct symbol_computed_ops *ops)
6471 {
6472 int result = next_loc_class_value++;
6473
6474 gdb_assert (loc_class == LOC_COMPUTED);
6475 gdb_assert (result < MAX_SYMBOL_IMPLS);
6476 symbol_impl[result].loc_class = loc_class;
6477 symbol_impl[result].ops_computed = ops;
6478
6479 /* Sanity check OPS. */
6480 gdb_assert (ops != NULL);
6481 gdb_assert (ops->tracepoint_var_ref != NULL);
6482 gdb_assert (ops->describe_location != NULL);
6483 gdb_assert (ops->get_symbol_read_needs != NULL);
6484 gdb_assert (ops->read_variable != NULL);
6485
6486 return result;
6487 }
6488
6489 /* Register a function with frame base type. LOC_CLASS must be LOC_BLOCK.
6490 OPS is the ops vector associated with this index. This returns the
6491 new index, which should be used as the loc_class_index field for symbols
6492 of this type. */
6493
6494 int
6495 register_symbol_block_impl (location_class loc_class,
6496 const struct symbol_block_ops *ops)
6497 {
6498 int result = next_loc_class_value++;
6499
6500 gdb_assert (loc_class == LOC_BLOCK);
6501 gdb_assert (result < MAX_SYMBOL_IMPLS);
6502 symbol_impl[result].loc_class = loc_class;
6503 symbol_impl[result].ops_block = ops;
6504
6505 /* Sanity check OPS. */
6506 gdb_assert (ops != NULL);
6507 gdb_assert (ops->find_frame_base_location != nullptr
6508 || ops->get_block_value != nullptr);
6509
6510 return result;
6511 }
6512
6513 /* Register a register symbol type. LOC_CLASS must be LOC_REGISTER or
6514 LOC_REGPARM_ADDR. OPS is the register ops vector associated with
6515 this index. This returns the new index, which should be used as
6516 the loc_class_index field for symbols of this type. */
6517
6518 int
6519 register_symbol_register_impl (location_class loc_class,
6520 const struct symbol_register_ops *ops)
6521 {
6522 int result = next_loc_class_value++;
6523
6524 gdb_assert (loc_class == LOC_REGISTER || loc_class == LOC_REGPARM_ADDR);
6525 gdb_assert (result < MAX_SYMBOL_IMPLS);
6526 symbol_impl[result].loc_class = loc_class;
6527 symbol_impl[result].ops_register = ops;
6528
6529 return result;
6530 }
6531
6532 /* Initialize elements of 'symbol_impl' for the constants in enum
6533 location_class. */
6534
6535 static void
6536 initialize_ordinary_address_classes (void)
6537 {
6538 for (int i = 0; i < LOC_FINAL_VALUE; ++i)
6539 symbol_impl[i].loc_class = static_cast<location_class> (i);
6540 }
6541
6542 \f
6543
6544 /* See symtab.h. */
6545
6546 struct objfile *
6547 symbol::objfile () const
6548 {
6549 gdb_assert (is_objfile_owned ());
6550 return owner.symtab->compunit ()->objfile ();
6551 }
6552
6553 /* See symtab.h. */
6554
6555 struct gdbarch *
6556 symbol::arch () const
6557 {
6558 if (!is_objfile_owned ())
6559 return owner.arch;
6560 return owner.symtab->compunit ()->objfile ()->arch ();
6561 }
6562
6563 /* See symtab.h. */
6564
6565 struct symtab *
6566 symbol::symtab () const
6567 {
6568 gdb_assert (is_objfile_owned ());
6569 return owner.symtab;
6570 }
6571
6572 /* See symtab.h. */
6573
6574 void
6575 symbol::set_symtab (struct symtab *symtab)
6576 {
6577 gdb_assert (is_objfile_owned ());
6578 owner.symtab = symtab;
6579 }
6580
6581 /* See symtab.h. */
6582
6583 CORE_ADDR
6584 symbol::get_maybe_copied_address () const
6585 {
6586 gdb_assert (this->maybe_copied);
6587 gdb_assert (this->loc_class () == LOC_STATIC);
6588
6589 const char *linkage_name = this->linkage_name ();
6590 bound_minimal_symbol minsym
6591 = lookup_minimal_symbol_linkage (this->objfile ()->pspace (), linkage_name,
6592 false, false);
6593 if (minsym.minsym != nullptr)
6594 return minsym.value_address ();
6595
6596 return this->m_value.address;
6597 }
6598
6599 /* See symtab.h. */
6600
6601 const block *
6602 symbol::value_block () const
6603 {
6604 if (const symbol_block_ops *block_ops = this->block_ops ();
6605 block_ops != nullptr && block_ops->get_block_value != nullptr)
6606 return block_ops->get_block_value (this);
6607
6608 return m_value.block;
6609 }
6610
6611 /* See symtab.h. */
6612
6613 void
6614 symbol::relocate (gdb::array_view<const CORE_ADDR> delta)
6615 {
6616 if ((loc_class () == LOC_LABEL || loc_class () == LOC_STATIC)
6617 && section_index () >= 0)
6618 set_value_address (value_address () + delta[section_index ()]);
6619 }
6620
6621 /* See symtab.h. */
6622
6623 CORE_ADDR
6624 minimal_symbol::get_maybe_copied_address (objfile *objf) const
6625 {
6626 gdb_assert (this->maybe_copied (objf));
6627 gdb_assert ((objf->flags & OBJF_MAINLINE) == 0);
6628
6629 const char *linkage_name = this->linkage_name ();
6630 bound_minimal_symbol found
6631 = lookup_minimal_symbol_linkage (objf->pspace (), linkage_name,
6632 false, true);
6633 if (found.minsym != nullptr)
6634 return found.value_address ();
6635
6636 return (this->m_value.address
6637 + objf->section_offsets[this->section_index ()]);
6638 }
6639
6640 \f
6641
6642 /* Hold the sub-commands of 'info module'. */
6643
6644 static struct cmd_list_element *info_module_cmdlist = NULL;
6645
6646 /* See symtab.h. */
6647
6648 std::vector<module_symbol_search>
6649 search_module_symbols (const char *module_regexp, const char *regexp,
6650 const char *type_regexp, domain_search_flags kind)
6651 {
6652 std::vector<module_symbol_search> results;
6653
6654 /* Search for all modules matching MODULE_REGEXP. */
6655 global_symbol_searcher spec1 (SEARCH_MODULE_DOMAIN, module_regexp);
6656 spec1.set_exclude_minsyms (true);
6657 std::vector<symbol_search> modules = spec1.search ();
6658
6659 /* Now search for all symbols of the required KIND matching the required
6660 regular expressions. We figure out which ones are in which modules
6661 below. */
6662 global_symbol_searcher spec2 (kind, regexp);
6663 spec2.set_symbol_type_regexp (type_regexp);
6664 spec2.set_exclude_minsyms (true);
6665 std::vector<symbol_search> symbols = spec2.search ();
6666
6667 /* Now iterate over all MODULES, checking to see which items from
6668 SYMBOLS are in each module. */
6669 for (const symbol_search &p : modules)
6670 {
6671 QUIT;
6672
6673 /* This is a module. */
6674 gdb_assert (p.symbol != nullptr);
6675
6676 std::string prefix = p.symbol->print_name ();
6677 prefix += "::";
6678
6679 for (const symbol_search &q : symbols)
6680 {
6681 if (q.symbol == nullptr)
6682 continue;
6683
6684 if (strncmp (q.symbol->print_name (), prefix.c_str (),
6685 prefix.size ()) != 0)
6686 continue;
6687
6688 results.push_back ({p, q});
6689 }
6690 }
6691
6692 return results;
6693 }
6694
6695 /* Implement the core of both 'info module functions' and 'info module
6696 variables'. */
6697
6698 static void
6699 info_module_subcommand (bool quiet, const char *module_regexp,
6700 const char *regexp, const char *type_regexp,
6701 domain_search_flags kind)
6702 {
6703 gdb_assert (kind == SEARCH_FUNCTION_DOMAIN || kind == SEARCH_VAR_DOMAIN);
6704
6705 /* Print a header line. Don't build the header line bit by bit as this
6706 prevents internationalisation. */
6707 if (!quiet)
6708 {
6709 if (module_regexp == nullptr)
6710 {
6711 if (type_regexp == nullptr)
6712 {
6713 if (regexp == nullptr)
6714 gdb_printf ((kind == SEARCH_VAR_DOMAIN
6715 ? _("All variables in all modules:")
6716 : _("All functions in all modules:")));
6717 else
6718 gdb_printf
6719 ((kind == SEARCH_VAR_DOMAIN
6720 ? _("All variables matching regular expression"
6721 " \"%s\" in all modules:")
6722 : _("All functions matching regular expression"
6723 " \"%s\" in all modules:")),
6724 regexp);
6725 }
6726 else
6727 {
6728 if (regexp == nullptr)
6729 gdb_printf
6730 ((kind == SEARCH_VAR_DOMAIN
6731 ? _("All variables with type matching regular "
6732 "expression \"%s\" in all modules:")
6733 : _("All functions with type matching regular "
6734 "expression \"%s\" in all modules:")),
6735 type_regexp);
6736 else
6737 gdb_printf
6738 ((kind == SEARCH_VAR_DOMAIN
6739 ? _("All variables matching regular expression "
6740 "\"%s\",\n\twith type matching regular "
6741 "expression \"%s\" in all modules:")
6742 : _("All functions matching regular expression "
6743 "\"%s\",\n\twith type matching regular "
6744 "expression \"%s\" in all modules:")),
6745 regexp, type_regexp);
6746 }
6747 }
6748 else
6749 {
6750 if (type_regexp == nullptr)
6751 {
6752 if (regexp == nullptr)
6753 gdb_printf
6754 ((kind == SEARCH_VAR_DOMAIN
6755 ? _("All variables in all modules matching regular "
6756 "expression \"%s\":")
6757 : _("All functions in all modules matching regular "
6758 "expression \"%s\":")),
6759 module_regexp);
6760 else
6761 gdb_printf
6762 ((kind == SEARCH_VAR_DOMAIN
6763 ? _("All variables matching regular expression "
6764 "\"%s\",\n\tin all modules matching regular "
6765 "expression \"%s\":")
6766 : _("All functions matching regular expression "
6767 "\"%s\",\n\tin all modules matching regular "
6768 "expression \"%s\":")),
6769 regexp, module_regexp);
6770 }
6771 else
6772 {
6773 if (regexp == nullptr)
6774 gdb_printf
6775 ((kind == SEARCH_VAR_DOMAIN
6776 ? _("All variables with type matching regular "
6777 "expression \"%s\"\n\tin all modules matching "
6778 "regular expression \"%s\":")
6779 : _("All functions with type matching regular "
6780 "expression \"%s\"\n\tin all modules matching "
6781 "regular expression \"%s\":")),
6782 type_regexp, module_regexp);
6783 else
6784 gdb_printf
6785 ((kind == SEARCH_VAR_DOMAIN
6786 ? _("All variables matching regular expression "
6787 "\"%s\",\n\twith type matching regular expression "
6788 "\"%s\",\n\tin all modules matching regular "
6789 "expression \"%s\":")
6790 : _("All functions matching regular expression "
6791 "\"%s\",\n\twith type matching regular expression "
6792 "\"%s\",\n\tin all modules matching regular "
6793 "expression \"%s\":")),
6794 regexp, type_regexp, module_regexp);
6795 }
6796 }
6797 gdb_printf ("\n");
6798 }
6799
6800 /* Find all symbols of type KIND matching the given regular expressions
6801 along with the symbols for the modules in which those symbols
6802 reside. */
6803 std::vector<module_symbol_search> module_symbols
6804 = search_module_symbols (module_regexp, regexp, type_regexp, kind);
6805
6806 std::sort (module_symbols.begin (), module_symbols.end (),
6807 [] (const module_symbol_search &a, const module_symbol_search &b)
6808 {
6809 if (a.first < b.first)
6810 return true;
6811 else if (a.first == b.first)
6812 return a.second < b.second;
6813 else
6814 return false;
6815 });
6816
6817 const char *last_filename = "";
6818 const symbol *last_module_symbol = nullptr;
6819 for (const auto &[p, q] : module_symbols)
6820 {
6821 gdb_assert (q.symbol != nullptr);
6822
6823 if (last_module_symbol != p.symbol)
6824 {
6825 gdb_printf ("\n");
6826 gdb_printf (_("Module \"%s\":\n"), p.symbol->print_name ());
6827 last_module_symbol = p.symbol;
6828 last_filename = "";
6829 }
6830
6831 print_symbol_info (q.symbol, q.block, last_filename);
6832 last_filename
6833 = symtab_to_filename_for_display (q.symbol->symtab ());
6834 }
6835 }
6836
6837 /* Hold the option values for the 'info module .....' sub-commands. */
6838
6839 struct info_modules_var_func_options
6840 {
6841 bool quiet = false;
6842 std::string type_regexp;
6843 std::string module_regexp;
6844 };
6845
6846 /* The options used by 'info module variables' and 'info module functions'
6847 commands. */
6848
6849 static const gdb::option::option_def info_modules_var_func_options_defs [] = {
6850 gdb::option::boolean_option_def<info_modules_var_func_options> {
6851 "q",
6852 [] (info_modules_var_func_options *opt) { return &opt->quiet; },
6853 nullptr, /* show_cmd_cb */
6854 nullptr /* set_doc */
6855 },
6856
6857 gdb::option::string_option_def<info_modules_var_func_options> {
6858 "t",
6859 [] (info_modules_var_func_options *opt) { return &opt->type_regexp; },
6860 nullptr, /* show_cmd_cb */
6861 nullptr /* set_doc */
6862 },
6863
6864 gdb::option::string_option_def<info_modules_var_func_options> {
6865 "m",
6866 [] (info_modules_var_func_options *opt) { return &opt->module_regexp; },
6867 nullptr, /* show_cmd_cb */
6868 nullptr /* set_doc */
6869 }
6870 };
6871
6872 /* Return the option group used by the 'info module ...' sub-commands. */
6873
6874 static inline gdb::option::option_def_group
6875 make_info_modules_var_func_options_def_group
6876 (info_modules_var_func_options *opts)
6877 {
6878 return {{info_modules_var_func_options_defs}, opts};
6879 }
6880
6881 /* Implements the 'info module functions' command. */
6882
6883 static void
6884 info_module_functions_command (const char *args, int from_tty)
6885 {
6886 info_modules_var_func_options opts;
6887 auto grp = make_info_modules_var_func_options_def_group (&opts);
6888 gdb::option::process_options
6889 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
6890 if (args != nullptr && *args == '\0')
6891 args = nullptr;
6892
6893 info_module_subcommand
6894 (opts.quiet,
6895 opts.module_regexp.empty () ? nullptr : opts.module_regexp.c_str (), args,
6896 opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
6897 SEARCH_FUNCTION_DOMAIN);
6898 }
6899
6900 /* Implements the 'info module variables' command. */
6901
6902 static void
6903 info_module_variables_command (const char *args, int from_tty)
6904 {
6905 info_modules_var_func_options opts;
6906 auto grp = make_info_modules_var_func_options_def_group (&opts);
6907 gdb::option::process_options
6908 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
6909 if (args != nullptr && *args == '\0')
6910 args = nullptr;
6911
6912 info_module_subcommand
6913 (opts.quiet,
6914 opts.module_regexp.empty () ? nullptr : opts.module_regexp.c_str (), args,
6915 opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
6916 SEARCH_VAR_DOMAIN);
6917 }
6918
6919 /* Command completer for 'info module ...' sub-commands. */
6920
6921 static void
6922 info_module_var_func_command_completer (struct cmd_list_element *ignore,
6923 completion_tracker &tracker,
6924 const char *text,
6925 const char * /* word */)
6926 {
6927
6928 const auto group = make_info_modules_var_func_options_def_group (nullptr);
6929 if (gdb::option::complete_options
6930 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
6931 return;
6932
6933 const char *word = advance_to_expression_complete_word_point (tracker, text);
6934 symbol_completer (ignore, tracker, text, word);
6935 }
6936
6937 \f
6938
6939 INIT_GDB_FILE (symtab)
6940 {
6941 cmd_list_element *c;
6942
6943 initialize_ordinary_address_classes ();
6944
6945 c = add_info ("variables", info_variables_command,
6946 info_print_args_help (_("\
6947 All global and static variable names or those matching REGEXPs.\n\
6948 Usage: info variables [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
6949 Prints the global and static variables.\n"),
6950 _("global and static variables"),
6951 true));
6952 set_cmd_completer_handle_brkchars (c, info_vars_funcs_command_completer);
6953
6954 c = add_info ("functions", info_functions_command,
6955 info_print_args_help (_("\
6956 All function names or those matching REGEXPs.\n\
6957 Usage: info functions [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
6958 Prints the functions.\n"),
6959 _("functions"),
6960 true));
6961 set_cmd_completer_handle_brkchars (c, info_vars_funcs_command_completer);
6962
6963 c = add_info ("types", info_types_command, _("\
6964 All type names, or those matching REGEXP.\n\
6965 Usage: info types [-q] [REGEXP]\n\
6966 Print information about all types matching REGEXP, or all types if no\n\
6967 REGEXP is given. The optional flag -q disables printing of headers."));
6968 set_cmd_completer_handle_brkchars (c, info_types_command_completer);
6969
6970 const auto info_sources_opts
6971 = make_info_sources_options_def_group (nullptr);
6972
6973 static std::string info_sources_help
6974 = gdb::option::build_help (_("\
6975 All source files in the program or those matching REGEXP.\n\
6976 Usage: info sources [OPTION]... [REGEXP]\n\
6977 By default, REGEXP is used to match anywhere in the filename.\n\
6978 \n\
6979 Options:\n\
6980 %OPTIONS%"),
6981 info_sources_opts);
6982
6983 c = add_info ("sources", info_sources_command, info_sources_help.c_str ());
6984 set_cmd_completer_handle_brkchars (c, info_sources_command_completer);
6985
6986 c = add_info ("modules", info_modules_command,
6987 _("All module names, or those matching REGEXP."));
6988 set_cmd_completer_handle_brkchars (c, info_types_command_completer);
6989
6990 add_info ("main", info_main_command,
6991 _("Get main symbol to identify entry point into program."));
6992
6993 add_basic_prefix_cmd ("module", class_info, _("\
6994 Print information about modules."),
6995 &info_module_cmdlist, 0, &infolist);
6996
6997 c = add_cmd ("functions", class_info, info_module_functions_command, _("\
6998 Display functions arranged by modules.\n\
6999 Usage: info module functions [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
7000 Print a summary of all functions within each Fortran module, grouped by\n\
7001 module and file. For each function the line on which the function is\n\
7002 defined is given along with the type signature and name of the function.\n\
7003 \n\
7004 If REGEXP is provided then only functions whose name matches REGEXP are\n\
7005 listed. If MODREGEXP is provided then only functions in modules matching\n\
7006 MODREGEXP are listed. If TYPEREGEXP is given then only functions whose\n\
7007 type signature matches TYPEREGEXP are listed.\n\
7008 \n\
7009 The -q flag suppresses printing some header information."),
7010 &info_module_cmdlist);
7011 set_cmd_completer_handle_brkchars
7012 (c, info_module_var_func_command_completer);
7013
7014 c = add_cmd ("variables", class_info, info_module_variables_command, _("\
7015 Display variables arranged by modules.\n\
7016 Usage: info module variables [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
7017 Print a summary of all variables within each Fortran module, grouped by\n\
7018 module and file. For each variable the line on which the variable is\n\
7019 defined is given along with the type and name of the variable.\n\
7020 \n\
7021 If REGEXP is provided then only variables whose name matches REGEXP are\n\
7022 listed. If MODREGEXP is provided then only variables in modules matching\n\
7023 MODREGEXP are listed. If TYPEREGEXP is given then only variables whose\n\
7024 type matches TYPEREGEXP are listed.\n\
7025 \n\
7026 The -q flag suppresses printing some header information."),
7027 &info_module_cmdlist);
7028 set_cmd_completer_handle_brkchars
7029 (c, info_module_var_func_command_completer);
7030
7031 add_com ("rbreak", class_breakpoint, rbreak_command,
7032 _("Set a breakpoint for all functions matching REGEXP."));
7033
7034 add_setshow_enum_cmd ("multiple-symbols", no_class,
7035 multiple_symbols_modes, &multiple_symbols_mode,
7036 _("\
7037 Set how the debugger handles ambiguities in expressions."), _("\
7038 Show how the debugger handles ambiguities in expressions."), _("\
7039 Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
7040 NULL, NULL, &setlist, &showlist);
7041
7042 add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
7043 &basenames_may_differ, _("\
7044 Set whether a source file may have multiple base names."), _("\
7045 Show whether a source file may have multiple base names."), _("\
7046 (A \"base name\" is the name of a file with the directory part removed.\n\
7047 Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
7048 If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
7049 before comparing them. Canonicalization is an expensive operation,\n\
7050 but it allows the same file be known by more than one base name.\n\
7051 If not set (the default), all source files are assumed to have just\n\
7052 one base name, and gdb will do file name comparisons more efficiently."),
7053 NULL, NULL,
7054 &setlist, &showlist);
7055
7056 add_setshow_zuinteger_cmd ("symtab-create", no_class, &symtab_create_debug,
7057 _("Set debugging of symbol table creation."),
7058 _("Show debugging of symbol table creation."), _("\
7059 When enabled (non-zero), debugging messages are printed when building\n\
7060 symbol tables. A value of 1 (one) normally provides enough information.\n\
7061 A value greater than 1 provides more verbose information."),
7062 NULL,
7063 NULL,
7064 &setdebuglist, &showdebuglist);
7065
7066 add_setshow_zuinteger_cmd ("symbol-lookup", no_class, &symbol_lookup_debug,
7067 _("\
7068 Set debugging of symbol lookup."), _("\
7069 Show debugging of symbol lookup."), _("\
7070 When enabled (non-zero), symbol lookups are logged."),
7071 NULL, NULL,
7072 &setdebuglist, &showdebuglist);
7073
7074 add_setshow_zuinteger_cmd ("symbol-cache-size", no_class,
7075 &new_symbol_cache_size,
7076 _("Set the size of the symbol cache."),
7077 _("Show the size of the symbol cache."), _("\
7078 The size of the symbol cache.\n\
7079 If zero then the symbol cache is disabled."),
7080 set_symbol_cache_size_handler, NULL,
7081 &maintenance_set_cmdlist,
7082 &maintenance_show_cmdlist);
7083
7084 add_setshow_boolean_cmd ("ignore-prologue-end-flag", no_class,
7085 &ignore_prologue_end_flag,
7086 _("Set if the PROLOGUE-END flag is ignored."),
7087 _("Show if the PROLOGUE-END flag is ignored."),
7088 _("\
7089 The PROLOGUE-END flag from the line-table entries is used to place\n\
7090 breakpoints past the prologue of functions. Disabling its use forces\n\
7091 the use of prologue scanners."),
7092 nullptr, nullptr,
7093 &maintenance_set_cmdlist,
7094 &maintenance_show_cmdlist);
7095
7096
7097 add_cmd ("symbol-cache", class_maintenance, maintenance_print_symbol_cache,
7098 _("Dump the symbol cache for each program space."),
7099 &maintenanceprintlist);
7100
7101 add_cmd ("symbol-cache-statistics", class_maintenance,
7102 maintenance_print_symbol_cache_statistics,
7103 _("Print symbol cache statistics for each program space."),
7104 &maintenanceprintlist);
7105
7106 cmd_list_element *maintenance_flush_symbol_cache_cmd
7107 = add_cmd ("symbol-cache", class_maintenance,
7108 maintenance_flush_symbol_cache,
7109 _("Flush the symbol cache for each program space."),
7110 &maintenanceflushlist);
7111 c = add_alias_cmd ("flush-symbol-cache", maintenance_flush_symbol_cache_cmd,
7112 class_maintenance, 0, &maintenancelist);
7113 deprecate_cmd (c, "maintenance flush symbol-cache");
7114
7115 gdb::observers::new_objfile.attach (symtab_new_objfile_observer, "symtab");
7116 gdb::observers::all_objfiles_removed.attach (symtab_all_objfiles_removed,
7117 "symtab");
7118 gdb::observers::free_objfile.attach (symtab_free_objfile_observer, "symtab");
7119 }