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