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