gdb: remove current_top_target function
[binutils-gdb.git] / gdb / linespec.c
1 /* Parser for linespec for the GNU debugger, GDB.
2
3 Copyright (C) 1986-2021 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 "frame.h"
23 #include "command.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "source.h"
27 #include "demangle.h"
28 #include "value.h"
29 #include "completer.h"
30 #include "cp-abi.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
33 #include "block.h"
34 #include "objc-lang.h"
35 #include "linespec.h"
36 #include "language.h"
37 #include "interps.h"
38 #include "mi/mi-cmds.h"
39 #include "target.h"
40 #include "arch-utils.h"
41 #include <ctype.h>
42 #include "cli/cli-utils.h"
43 #include "filenames.h"
44 #include "ada-lang.h"
45 #include "stack.h"
46 #include "location.h"
47 #include "gdbsupport/function-view.h"
48 #include "gdbsupport/def-vector.h"
49 #include <algorithm>
50 #include "inferior.h"
51
52 /* An enumeration of the various things a user might attempt to
53 complete for a linespec location. */
54
55 enum class linespec_complete_what
56 {
57 /* Nothing, no possible completion. */
58 NOTHING,
59
60 /* A function/method name. Due to ambiguity between
61
62 (gdb) b source[TAB]
63 source_file.c
64 source_function
65
66 this can also indicate a source filename, iff we haven't seen a
67 separate source filename component, as in "b source.c:function". */
68 FUNCTION,
69
70 /* A label symbol. E.g., break file.c:function:LABEL. */
71 LABEL,
72
73 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
74 EXPRESSION,
75
76 /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
77 E.g., "break func threa<tab>". */
78 KEYWORD,
79 };
80
81 /* An address entry is used to ensure that any given location is only
82 added to the result a single time. It holds an address and the
83 program space from which the address came. */
84
85 struct address_entry
86 {
87 struct program_space *pspace;
88 CORE_ADDR addr;
89 };
90
91 /* A linespec. Elements of this structure are filled in by a parser
92 (either parse_linespec or some other function). The structure is
93 then converted into SALs by convert_linespec_to_sals. */
94
95 struct linespec
96 {
97 /* An explicit location describing the SaLs. */
98 struct explicit_location explicit_loc;
99
100 /* The list of symtabs to search to which to limit the search. May not
101 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
102 filename), FILE_SYMTABS should contain one single NULL member. This
103 will cause the code to use the default symtab. */
104 std::vector<symtab *> *file_symtabs;
105
106 /* A list of matching function symbols and minimal symbols. Both lists
107 may be NULL (or empty) if no matching symbols were found. */
108 std::vector<block_symbol> *function_symbols;
109 std::vector<bound_minimal_symbol> *minimal_symbols;
110
111 /* A structure of matching label symbols and the corresponding
112 function symbol in which the label was found. Both may be NULL
113 or both must be non-NULL. */
114 struct
115 {
116 std::vector<block_symbol> *label_symbols;
117 std::vector<block_symbol> *function_symbols;
118 } labels;
119 };
120 typedef struct linespec *linespec_p;
121
122 /* A canonical linespec represented as a symtab-related string.
123
124 Each entry represents the "SYMTAB:SUFFIX" linespec string.
125 SYMTAB can be converted for example by symtab_to_fullname or
126 symtab_to_filename_for_display as needed. */
127
128 struct linespec_canonical_name
129 {
130 /* Remaining text part of the linespec string. */
131 char *suffix;
132
133 /* If NULL then SUFFIX is the whole linespec string. */
134 struct symtab *symtab;
135 };
136
137 /* An instance of this is used to keep all state while linespec
138 operates. This instance is passed around as a 'this' pointer to
139 the various implementation methods. */
140
141 struct linespec_state
142 {
143 /* The language in use during linespec processing. */
144 const struct language_defn *language;
145
146 /* The program space as seen when the module was entered. */
147 struct program_space *program_space;
148
149 /* If not NULL, the search is restricted to just this program
150 space. */
151 struct program_space *search_pspace;
152
153 /* The default symtab to use, if no other symtab is specified. */
154 struct symtab *default_symtab;
155
156 /* The default line to use. */
157 int default_line;
158
159 /* The 'funfirstline' value that was passed in to decode_line_1 or
160 decode_line_full. */
161 int funfirstline;
162
163 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
164 int list_mode;
165
166 /* The 'canonical' value passed to decode_line_full, or NULL. */
167 struct linespec_result *canonical;
168
169 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
170 struct linespec_canonical_name *canonical_names;
171
172 /* This is a set of address_entry objects which is used to prevent
173 duplicate symbols from being entered into the result. */
174 htab_t addr_set;
175
176 /* Are we building a linespec? */
177 int is_linespec;
178 };
179
180 /* This is a helper object that is used when collecting symbols into a
181 result. */
182
183 struct collect_info
184 {
185 /* The linespec object in use. */
186 struct linespec_state *state;
187
188 /* A list of symtabs to which to restrict matches. */
189 std::vector<symtab *> *file_symtabs;
190
191 /* The result being accumulated. */
192 struct
193 {
194 std::vector<block_symbol> *symbols;
195 std::vector<bound_minimal_symbol> *minimal_symbols;
196 } result;
197
198 /* Possibly add a symbol to the results. */
199 virtual bool add_symbol (block_symbol *bsym);
200 };
201
202 bool
203 collect_info::add_symbol (block_symbol *bsym)
204 {
205 /* In list mode, add all matching symbols, regardless of class.
206 This allows the user to type "list a_global_variable". */
207 if (SYMBOL_CLASS (bsym->symbol) == LOC_BLOCK || this->state->list_mode)
208 this->result.symbols->push_back (*bsym);
209
210 /* Continue iterating. */
211 return true;
212 }
213
214 /* Custom collect_info for symbol_searcher. */
215
216 struct symbol_searcher_collect_info
217 : collect_info
218 {
219 bool add_symbol (block_symbol *bsym) override
220 {
221 /* Add everything. */
222 this->result.symbols->push_back (*bsym);
223
224 /* Continue iterating. */
225 return true;
226 }
227 };
228
229 /* Token types */
230
231 enum ls_token_type
232 {
233 /* A keyword */
234 LSTOKEN_KEYWORD = 0,
235
236 /* A colon "separator" */
237 LSTOKEN_COLON,
238
239 /* A string */
240 LSTOKEN_STRING,
241
242 /* A number */
243 LSTOKEN_NUMBER,
244
245 /* A comma */
246 LSTOKEN_COMMA,
247
248 /* EOI (end of input) */
249 LSTOKEN_EOI,
250
251 /* Consumed token */
252 LSTOKEN_CONSUMED
253 };
254 typedef enum ls_token_type linespec_token_type;
255
256 /* List of keywords. This is NULL-terminated so that it can be used
257 as enum completer. */
258 const char * const linespec_keywords[] = { "if", "thread", "task", "-force-condition", NULL };
259 #define IF_KEYWORD_INDEX 0
260 #define FORCE_KEYWORD_INDEX 3
261
262 /* A token of the linespec lexer */
263
264 struct linespec_token
265 {
266 /* The type of the token */
267 linespec_token_type type;
268
269 /* Data for the token */
270 union
271 {
272 /* A string, given as a stoken */
273 struct stoken string;
274
275 /* A keyword */
276 const char *keyword;
277 } data;
278 };
279
280 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
281 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
282
283 /* An instance of the linespec parser. */
284
285 struct linespec_parser
286 {
287 linespec_parser (int flags, const struct language_defn *language,
288 struct program_space *search_pspace,
289 struct symtab *default_symtab,
290 int default_line,
291 struct linespec_result *canonical);
292
293 ~linespec_parser ();
294
295 DISABLE_COPY_AND_ASSIGN (linespec_parser);
296
297 /* Lexer internal data */
298 struct
299 {
300 /* Save head of input stream. */
301 const char *saved_arg;
302
303 /* Head of the input stream. */
304 const char *stream;
305 #define PARSER_STREAM(P) ((P)->lexer.stream)
306
307 /* The current token. */
308 linespec_token current;
309 } lexer {};
310
311 /* Is the entire linespec quote-enclosed? */
312 int is_quote_enclosed = 0;
313
314 /* The state of the parse. */
315 struct linespec_state state {};
316 #define PARSER_STATE(PPTR) (&(PPTR)->state)
317
318 /* The result of the parse. */
319 struct linespec result {};
320 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
321
322 /* What the parser believes the current word point should complete
323 to. */
324 linespec_complete_what complete_what = linespec_complete_what::NOTHING;
325
326 /* The completion word point. The parser advances this as it skips
327 tokens. At some point the input string will end or parsing will
328 fail, and then we attempt completion at the captured completion
329 word point, interpreting the string at completion_word as
330 COMPLETE_WHAT. */
331 const char *completion_word = nullptr;
332
333 /* If the current token was a quoted string, then this is the
334 quoting character (either " or '). */
335 int completion_quote_char = 0;
336
337 /* If the current token was a quoted string, then this points at the
338 end of the quoted string. */
339 const char *completion_quote_end = nullptr;
340
341 /* If parsing for completion, then this points at the completion
342 tracker. Otherwise, this is NULL. */
343 struct completion_tracker *completion_tracker = nullptr;
344 };
345
346 /* A convenience macro for accessing the explicit location result of
347 the parser. */
348 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
349
350 /* Prototypes for local functions. */
351
352 static void iterate_over_file_blocks
353 (struct symtab *symtab, const lookup_name_info &name,
354 domain_enum domain,
355 gdb::function_view<symbol_found_callback_ftype> callback);
356
357 static void initialize_defaults (struct symtab **default_symtab,
358 int *default_line);
359
360 CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
361
362 static std::vector<symtab_and_line> decode_objc (struct linespec_state *self,
363 linespec_p ls,
364 const char *arg);
365
366 static std::vector<symtab *> symtabs_from_filename
367 (const char *, struct program_space *pspace);
368
369 static std::vector<block_symbol> *find_label_symbols
370 (struct linespec_state *self, std::vector<block_symbol> *function_symbols,
371 std::vector<block_symbol> *label_funcs_ret, const char *name,
372 bool completion_mode = false);
373
374 static void find_linespec_symbols (struct linespec_state *self,
375 std::vector<symtab *> *file_symtabs,
376 const char *name,
377 symbol_name_match_type name_match_type,
378 std::vector<block_symbol> *symbols,
379 std::vector<bound_minimal_symbol> *minsyms);
380
381 static struct line_offset
382 linespec_parse_variable (struct linespec_state *self,
383 const char *variable);
384
385 static int symbol_to_sal (struct symtab_and_line *result,
386 int funfirstline, struct symbol *sym);
387
388 static void add_matching_symbols_to_info (const char *name,
389 symbol_name_match_type name_match_type,
390 enum search_domain search_domain,
391 struct collect_info *info,
392 struct program_space *pspace);
393
394 static void add_all_symbol_names_from_pspace
395 (struct collect_info *info, struct program_space *pspace,
396 const std::vector<const char *> &names, enum search_domain search_domain);
397
398 static std::vector<symtab *>
399 collect_symtabs_from_filename (const char *file,
400 struct program_space *pspace);
401
402 static std::vector<symtab_and_line> decode_digits_ordinary
403 (struct linespec_state *self,
404 linespec_p ls,
405 int line,
406 linetable_entry **best_entry);
407
408 static std::vector<symtab_and_line> decode_digits_list_mode
409 (struct linespec_state *self,
410 linespec_p ls,
411 struct symtab_and_line val);
412
413 static void minsym_found (struct linespec_state *self, struct objfile *objfile,
414 struct minimal_symbol *msymbol,
415 std::vector<symtab_and_line> *result);
416
417 static bool compare_symbols (const block_symbol &a, const block_symbol &b);
418
419 static bool compare_msymbols (const bound_minimal_symbol &a,
420 const bound_minimal_symbol &b);
421
422 /* Permitted quote characters for the parser. This is different from the
423 completer's quote characters to allow backward compatibility with the
424 previous parser. */
425 static const char linespec_quote_characters[] = "\"\'";
426
427 /* Lexer functions. */
428
429 /* Lex a number from the input in PARSER. This only supports
430 decimal numbers.
431
432 Return true if input is decimal numbers. Return false if not. */
433
434 static int
435 linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
436 {
437 tokenp->type = LSTOKEN_NUMBER;
438 LS_TOKEN_STOKEN (*tokenp).length = 0;
439 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
440
441 /* Keep any sign at the start of the stream. */
442 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
443 {
444 ++LS_TOKEN_STOKEN (*tokenp).length;
445 ++(PARSER_STREAM (parser));
446 }
447
448 while (isdigit (*PARSER_STREAM (parser)))
449 {
450 ++LS_TOKEN_STOKEN (*tokenp).length;
451 ++(PARSER_STREAM (parser));
452 }
453
454 /* If the next character in the input buffer is not a space, comma,
455 quote, or colon, this input does not represent a number. */
456 if (*PARSER_STREAM (parser) != '\0'
457 && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
458 && *PARSER_STREAM (parser) != ':'
459 && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
460 {
461 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
462 return 0;
463 }
464
465 return 1;
466 }
467
468 /* See linespec.h. */
469
470 const char *
471 linespec_lexer_lex_keyword (const char *p)
472 {
473 int i;
474
475 if (p != NULL)
476 {
477 for (i = 0; linespec_keywords[i] != NULL; ++i)
478 {
479 int len = strlen (linespec_keywords[i]);
480
481 /* If P begins with
482
483 - "thread" or "task" and the next character is
484 whitespace, we may have found a keyword. It is only a
485 keyword if it is not followed by another keyword.
486
487 - "-force-condition", the next character may be EOF
488 since this keyword does not take any arguments. Otherwise,
489 it should be followed by a keyword.
490
491 - "if", ALWAYS stop the lexer, since it is not possible to
492 predict what is going to appear in the condition, which can
493 only be parsed after SaLs have been found. */
494 if (strncmp (p, linespec_keywords[i], len) == 0)
495 {
496 int j;
497
498 if (i == FORCE_KEYWORD_INDEX && p[len] == '\0')
499 return linespec_keywords[i];
500
501 if (!isspace (p[len]))
502 continue;
503
504 if (i == FORCE_KEYWORD_INDEX)
505 {
506 p += len;
507 p = skip_spaces (p);
508 for (j = 0; linespec_keywords[j] != NULL; ++j)
509 {
510 int nextlen = strlen (linespec_keywords[j]);
511
512 if (strncmp (p, linespec_keywords[j], nextlen) == 0
513 && isspace (p[nextlen]))
514 return linespec_keywords[i];
515 }
516 }
517 else if (i != IF_KEYWORD_INDEX)
518 {
519 /* We matched a "thread" or "task". */
520 p += len;
521 p = skip_spaces (p);
522 for (j = 0; linespec_keywords[j] != NULL; ++j)
523 {
524 int nextlen = strlen (linespec_keywords[j]);
525
526 if (strncmp (p, linespec_keywords[j], nextlen) == 0
527 && isspace (p[nextlen]))
528 return NULL;
529 }
530 }
531
532 return linespec_keywords[i];
533 }
534 }
535 }
536
537 return NULL;
538 }
539
540 /* See description in linespec.h. */
541
542 int
543 is_ada_operator (const char *string)
544 {
545 const struct ada_opname_map *mapping;
546
547 for (mapping = ada_opname_table;
548 mapping->encoded != NULL
549 && !startswith (string, mapping->decoded); ++mapping)
550 ;
551
552 return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
553 }
554
555 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
556 the location of QUOTE_CHAR, or NULL if not found. */
557
558 static const char *
559 skip_quote_char (const char *string, char quote_char)
560 {
561 const char *p, *last;
562
563 p = last = find_toplevel_char (string, quote_char);
564 while (p && *p != '\0' && *p != ':')
565 {
566 p = find_toplevel_char (p, quote_char);
567 if (p != NULL)
568 last = p++;
569 }
570
571 return last;
572 }
573
574 /* Make a writable copy of the string given in TOKEN, trimming
575 any trailing whitespace. */
576
577 static gdb::unique_xmalloc_ptr<char>
578 copy_token_string (linespec_token token)
579 {
580 const char *str, *s;
581
582 if (token.type == LSTOKEN_KEYWORD)
583 return make_unique_xstrdup (LS_TOKEN_KEYWORD (token));
584
585 str = LS_TOKEN_STOKEN (token).ptr;
586 s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
587
588 return gdb::unique_xmalloc_ptr<char> (savestring (str, s - str));
589 }
590
591 /* Does P represent the end of a quote-enclosed linespec? */
592
593 static int
594 is_closing_quote_enclosed (const char *p)
595 {
596 if (strchr (linespec_quote_characters, *p))
597 ++p;
598 p = skip_spaces ((char *) p);
599 return (*p == '\0' || linespec_lexer_lex_keyword (p));
600 }
601
602 /* Find the end of the parameter list that starts with *INPUT.
603 This helper function assists with lexing string segments
604 which might contain valid (non-terminating) commas. */
605
606 static const char *
607 find_parameter_list_end (const char *input)
608 {
609 char end_char, start_char;
610 int depth;
611 const char *p;
612
613 start_char = *input;
614 if (start_char == '(')
615 end_char = ')';
616 else if (start_char == '<')
617 end_char = '>';
618 else
619 return NULL;
620
621 p = input;
622 depth = 0;
623 while (*p)
624 {
625 if (*p == start_char)
626 ++depth;
627 else if (*p == end_char)
628 {
629 if (--depth == 0)
630 {
631 ++p;
632 break;
633 }
634 }
635 ++p;
636 }
637
638 return p;
639 }
640
641 /* If the [STRING, STRING_LEN) string ends with what looks like a
642 keyword, return the keyword start offset in STRING. Return -1
643 otherwise. */
644
645 static size_t
646 string_find_incomplete_keyword_at_end (const char * const *keywords,
647 const char *string, size_t string_len)
648 {
649 const char *end = string + string_len;
650 const char *p = end;
651
652 while (p > string && *p != ' ')
653 --p;
654 if (p > string)
655 {
656 p++;
657 size_t len = end - p;
658 for (size_t i = 0; keywords[i] != NULL; ++i)
659 if (strncmp (keywords[i], p, len) == 0)
660 return p - string;
661 }
662
663 return -1;
664 }
665
666 /* Lex a string from the input in PARSER. */
667
668 static linespec_token
669 linespec_lexer_lex_string (linespec_parser *parser)
670 {
671 linespec_token token;
672 const char *start = PARSER_STREAM (parser);
673
674 token.type = LSTOKEN_STRING;
675
676 /* If the input stream starts with a quote character, skip to the next
677 quote character, regardless of the content. */
678 if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
679 {
680 const char *end;
681 char quote_char = *PARSER_STREAM (parser);
682
683 /* Special case: Ada operators. */
684 if (PARSER_STATE (parser)->language->la_language == language_ada
685 && quote_char == '\"')
686 {
687 int len = is_ada_operator (PARSER_STREAM (parser));
688
689 if (len != 0)
690 {
691 /* The input is an Ada operator. Return the quoted string
692 as-is. */
693 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
694 LS_TOKEN_STOKEN (token).length = len;
695 PARSER_STREAM (parser) += len;
696 return token;
697 }
698
699 /* The input does not represent an Ada operator -- fall through
700 to normal quoted string handling. */
701 }
702
703 /* Skip past the beginning quote. */
704 ++(PARSER_STREAM (parser));
705
706 /* Mark the start of the string. */
707 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
708
709 /* Skip to the ending quote. */
710 end = skip_quote_char (PARSER_STREAM (parser), quote_char);
711
712 /* This helps the completer mode decide whether we have a
713 complete string. */
714 parser->completion_quote_char = quote_char;
715 parser->completion_quote_end = end;
716
717 /* Error if the input did not terminate properly, unless in
718 completion mode. */
719 if (end == NULL)
720 {
721 if (parser->completion_tracker == NULL)
722 error (_("unmatched quote"));
723
724 /* In completion mode, we'll try to complete the incomplete
725 token. */
726 token.type = LSTOKEN_STRING;
727 while (*PARSER_STREAM (parser) != '\0')
728 PARSER_STREAM (parser)++;
729 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 1 - start;
730 }
731 else
732 {
733 /* Skip over the ending quote and mark the length of the string. */
734 PARSER_STREAM (parser) = (char *) ++end;
735 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
736 }
737 }
738 else
739 {
740 const char *p;
741
742 /* Otherwise, only identifier characters are permitted.
743 Spaces are the exception. In general, we keep spaces,
744 but only if the next characters in the input do not resolve
745 to one of the keywords.
746
747 This allows users to forgo quoting CV-qualifiers, template arguments,
748 and similar common language constructs. */
749
750 while (1)
751 {
752 if (isspace (*PARSER_STREAM (parser)))
753 {
754 p = skip_spaces (PARSER_STREAM (parser));
755 /* When we get here we know we've found something followed by
756 a space (we skip over parens and templates below).
757 So if we find a keyword now, we know it is a keyword and not,
758 say, a function name. */
759 if (linespec_lexer_lex_keyword (p) != NULL)
760 {
761 LS_TOKEN_STOKEN (token).ptr = start;
762 LS_TOKEN_STOKEN (token).length
763 = PARSER_STREAM (parser) - start;
764 return token;
765 }
766
767 /* Advance past the whitespace. */
768 PARSER_STREAM (parser) = p;
769 }
770
771 /* If the next character is EOI or (single) ':', the
772 string is complete; return the token. */
773 if (*PARSER_STREAM (parser) == 0)
774 {
775 LS_TOKEN_STOKEN (token).ptr = start;
776 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
777 return token;
778 }
779 else if (PARSER_STREAM (parser)[0] == ':')
780 {
781 /* Do not tokenize the C++ scope operator. */
782 if (PARSER_STREAM (parser)[1] == ':')
783 ++(PARSER_STREAM (parser));
784
785 /* Do not tokenize ABI tags such as "[abi:cxx11]". */
786 else if (PARSER_STREAM (parser) - start > 4
787 && startswith (PARSER_STREAM (parser) - 4, "[abi"))
788 {
789 /* Nothing. */
790 }
791
792 /* Do not tokenify if the input length so far is one
793 (i.e, a single-letter drive name) and the next character
794 is a directory separator. This allows Windows-style
795 paths to be recognized as filenames without quoting it. */
796 else if ((PARSER_STREAM (parser) - start) != 1
797 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
798 {
799 LS_TOKEN_STOKEN (token).ptr = start;
800 LS_TOKEN_STOKEN (token).length
801 = PARSER_STREAM (parser) - start;
802 return token;
803 }
804 }
805 /* Special case: permit quote-enclosed linespecs. */
806 else if (parser->is_quote_enclosed
807 && strchr (linespec_quote_characters,
808 *PARSER_STREAM (parser))
809 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
810 {
811 LS_TOKEN_STOKEN (token).ptr = start;
812 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
813 return token;
814 }
815 /* Because commas may terminate a linespec and appear in
816 the middle of valid string input, special cases for
817 '<' and '(' are necessary. */
818 else if (*PARSER_STREAM (parser) == '<'
819 || *PARSER_STREAM (parser) == '(')
820 {
821 /* Don't interpret 'operator<' / 'operator<<' as a
822 template parameter list though. */
823 if (*PARSER_STREAM (parser) == '<'
824 && (PARSER_STATE (parser)->language->la_language
825 == language_cplus)
826 && (PARSER_STREAM (parser) - start) >= CP_OPERATOR_LEN)
827 {
828 const char *op = PARSER_STREAM (parser);
829
830 while (op > start && isspace (op[-1]))
831 op--;
832 if (op - start >= CP_OPERATOR_LEN)
833 {
834 op -= CP_OPERATOR_LEN;
835 if (strncmp (op, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
836 && (op == start
837 || !(isalnum (op[-1]) || op[-1] == '_')))
838 {
839 /* This is an operator name. Keep going. */
840 ++(PARSER_STREAM (parser));
841 if (*PARSER_STREAM (parser) == '<')
842 ++(PARSER_STREAM (parser));
843 continue;
844 }
845 }
846 }
847
848 const char *end = find_parameter_list_end (PARSER_STREAM (parser));
849 PARSER_STREAM (parser) = end;
850
851 /* Don't loop around to the normal \0 case above because
852 we don't want to misinterpret a potential keyword at
853 the end of the token when the string isn't
854 "()<>"-balanced. This handles "b
855 function(thread<tab>" in completion mode. */
856 if (*end == '\0')
857 {
858 LS_TOKEN_STOKEN (token).ptr = start;
859 LS_TOKEN_STOKEN (token).length
860 = PARSER_STREAM (parser) - start;
861 return token;
862 }
863 else
864 continue;
865 }
866 /* Commas are terminators, but not if they are part of an
867 operator name. */
868 else if (*PARSER_STREAM (parser) == ',')
869 {
870 if ((PARSER_STATE (parser)->language->la_language
871 == language_cplus)
872 && (PARSER_STREAM (parser) - start) > CP_OPERATOR_LEN)
873 {
874 const char *op = strstr (start, CP_OPERATOR_STR);
875
876 if (op != NULL && is_operator_name (op))
877 {
878 /* This is an operator name. Keep going. */
879 ++(PARSER_STREAM (parser));
880 continue;
881 }
882 }
883
884 /* Comma terminates the string. */
885 LS_TOKEN_STOKEN (token).ptr = start;
886 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
887 return token;
888 }
889
890 /* Advance the stream. */
891 gdb_assert (*(PARSER_STREAM (parser)) != '\0');
892 ++(PARSER_STREAM (parser));
893 }
894 }
895
896 return token;
897 }
898
899 /* Lex a single linespec token from PARSER. */
900
901 static linespec_token
902 linespec_lexer_lex_one (linespec_parser *parser)
903 {
904 const char *keyword;
905
906 if (parser->lexer.current.type == LSTOKEN_CONSUMED)
907 {
908 /* Skip any whitespace. */
909 PARSER_STREAM (parser) = skip_spaces (PARSER_STREAM (parser));
910
911 /* Check for a keyword, they end the linespec. */
912 keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
913 if (keyword != NULL)
914 {
915 parser->lexer.current.type = LSTOKEN_KEYWORD;
916 LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
917 /* We do not advance the stream here intentionally:
918 we would like lexing to stop when a keyword is seen.
919
920 PARSER_STREAM (parser) += strlen (keyword); */
921
922 return parser->lexer.current;
923 }
924
925 /* Handle other tokens. */
926 switch (*PARSER_STREAM (parser))
927 {
928 case 0:
929 parser->lexer.current.type = LSTOKEN_EOI;
930 break;
931
932 case '+': case '-':
933 case '0': case '1': case '2': case '3': case '4':
934 case '5': case '6': case '7': case '8': case '9':
935 if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
936 parser->lexer.current = linespec_lexer_lex_string (parser);
937 break;
938
939 case ':':
940 /* If we have a scope operator, lex the input as a string.
941 Otherwise, return LSTOKEN_COLON. */
942 if (PARSER_STREAM (parser)[1] == ':')
943 parser->lexer.current = linespec_lexer_lex_string (parser);
944 else
945 {
946 parser->lexer.current.type = LSTOKEN_COLON;
947 ++(PARSER_STREAM (parser));
948 }
949 break;
950
951 case '\'': case '\"':
952 /* Special case: permit quote-enclosed linespecs. */
953 if (parser->is_quote_enclosed
954 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
955 {
956 ++(PARSER_STREAM (parser));
957 parser->lexer.current.type = LSTOKEN_EOI;
958 }
959 else
960 parser->lexer.current = linespec_lexer_lex_string (parser);
961 break;
962
963 case ',':
964 parser->lexer.current.type = LSTOKEN_COMMA;
965 LS_TOKEN_STOKEN (parser->lexer.current).ptr
966 = PARSER_STREAM (parser);
967 LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
968 ++(PARSER_STREAM (parser));
969 break;
970
971 default:
972 /* If the input is not a number, it must be a string.
973 [Keywords were already considered above.] */
974 parser->lexer.current = linespec_lexer_lex_string (parser);
975 break;
976 }
977 }
978
979 return parser->lexer.current;
980 }
981
982 /* Consume the current token and return the next token in PARSER's
983 input stream. Also advance the completion word for completion
984 mode. */
985
986 static linespec_token
987 linespec_lexer_consume_token (linespec_parser *parser)
988 {
989 gdb_assert (parser->lexer.current.type != LSTOKEN_EOI);
990
991 bool advance_word = (parser->lexer.current.type != LSTOKEN_STRING
992 || *PARSER_STREAM (parser) != '\0');
993
994 /* If we're moving past a string to some other token, it must be the
995 quote was terminated. */
996 if (parser->completion_quote_char)
997 {
998 gdb_assert (parser->lexer.current.type == LSTOKEN_STRING);
999
1000 /* If the string was the last (non-EOI) token, we're past the
1001 quote, but remember that for later. */
1002 if (*PARSER_STREAM (parser) != '\0')
1003 {
1004 parser->completion_quote_char = '\0';
1005 parser->completion_quote_end = NULL;;
1006 }
1007 }
1008
1009 parser->lexer.current.type = LSTOKEN_CONSUMED;
1010 linespec_lexer_lex_one (parser);
1011
1012 if (parser->lexer.current.type == LSTOKEN_STRING)
1013 {
1014 /* Advance the completion word past a potential initial
1015 quote-char. */
1016 parser->completion_word = LS_TOKEN_STOKEN (parser->lexer.current).ptr;
1017 }
1018 else if (advance_word)
1019 {
1020 /* Advance the completion word past any whitespace. */
1021 parser->completion_word = PARSER_STREAM (parser);
1022 }
1023
1024 return parser->lexer.current;
1025 }
1026
1027 /* Return the next token without consuming the current token. */
1028
1029 static linespec_token
1030 linespec_lexer_peek_token (linespec_parser *parser)
1031 {
1032 linespec_token next;
1033 const char *saved_stream = PARSER_STREAM (parser);
1034 linespec_token saved_token = parser->lexer.current;
1035 int saved_completion_quote_char = parser->completion_quote_char;
1036 const char *saved_completion_quote_end = parser->completion_quote_end;
1037 const char *saved_completion_word = parser->completion_word;
1038
1039 next = linespec_lexer_consume_token (parser);
1040 PARSER_STREAM (parser) = saved_stream;
1041 parser->lexer.current = saved_token;
1042 parser->completion_quote_char = saved_completion_quote_char;
1043 parser->completion_quote_end = saved_completion_quote_end;
1044 parser->completion_word = saved_completion_word;
1045 return next;
1046 }
1047
1048 /* Helper functions. */
1049
1050 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1051 the new sal, if needed. If not NULL, SYMNAME is the name of the
1052 symbol to use when constructing the new canonical name.
1053
1054 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1055 canonical name for the SAL. */
1056
1057 static void
1058 add_sal_to_sals (struct linespec_state *self,
1059 std::vector<symtab_and_line> *sals,
1060 struct symtab_and_line *sal,
1061 const char *symname, int literal_canonical)
1062 {
1063 sals->push_back (*sal);
1064
1065 if (self->canonical)
1066 {
1067 struct linespec_canonical_name *canonical;
1068
1069 self->canonical_names = XRESIZEVEC (struct linespec_canonical_name,
1070 self->canonical_names,
1071 sals->size ());
1072 canonical = &self->canonical_names[sals->size () - 1];
1073 if (!literal_canonical && sal->symtab)
1074 {
1075 symtab_to_fullname (sal->symtab);
1076
1077 /* Note that the filter doesn't have to be a valid linespec
1078 input. We only apply the ":LINE" treatment to Ada for
1079 the time being. */
1080 if (symname != NULL && sal->line != 0
1081 && self->language->la_language == language_ada)
1082 canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
1083 else if (symname != NULL)
1084 canonical->suffix = xstrdup (symname);
1085 else
1086 canonical->suffix = xstrprintf ("%d", sal->line);
1087 canonical->symtab = sal->symtab;
1088 }
1089 else
1090 {
1091 if (symname != NULL)
1092 canonical->suffix = xstrdup (symname);
1093 else
1094 canonical->suffix = xstrdup ("<unknown>");
1095 canonical->symtab = NULL;
1096 }
1097 }
1098 }
1099
1100 /* A hash function for address_entry. */
1101
1102 static hashval_t
1103 hash_address_entry (const void *p)
1104 {
1105 const struct address_entry *aep = (const struct address_entry *) p;
1106 hashval_t hash;
1107
1108 hash = iterative_hash_object (aep->pspace, 0);
1109 return iterative_hash_object (aep->addr, hash);
1110 }
1111
1112 /* An equality function for address_entry. */
1113
1114 static int
1115 eq_address_entry (const void *a, const void *b)
1116 {
1117 const struct address_entry *aea = (const struct address_entry *) a;
1118 const struct address_entry *aeb = (const struct address_entry *) b;
1119
1120 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
1121 }
1122
1123 /* Check whether the address, represented by PSPACE and ADDR, is
1124 already in the set. If so, return 0. Otherwise, add it and return
1125 1. */
1126
1127 static int
1128 maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
1129 {
1130 struct address_entry e, *p;
1131 void **slot;
1132
1133 e.pspace = pspace;
1134 e.addr = addr;
1135 slot = htab_find_slot (set, &e, INSERT);
1136 if (*slot)
1137 return 0;
1138
1139 p = XNEW (struct address_entry);
1140 memcpy (p, &e, sizeof (struct address_entry));
1141 *slot = p;
1142
1143 return 1;
1144 }
1145
1146 /* A helper that walks over all matching symtabs in all objfiles and
1147 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1148 not NULL, then the search is restricted to just that program
1149 space. If INCLUDE_INLINE is true then symbols representing
1150 inlined instances of functions will be included in the result. */
1151
1152 static void
1153 iterate_over_all_matching_symtabs
1154 (struct linespec_state *state,
1155 const lookup_name_info &lookup_name,
1156 const domain_enum name_domain,
1157 enum search_domain search_domain,
1158 struct program_space *search_pspace, bool include_inline,
1159 gdb::function_view<symbol_found_callback_ftype> callback)
1160 {
1161 for (struct program_space *pspace : program_spaces)
1162 {
1163 if (search_pspace != NULL && search_pspace != pspace)
1164 continue;
1165 if (pspace->executing_startup)
1166 continue;
1167
1168 set_current_program_space (pspace);
1169
1170 for (objfile *objfile : current_program_space->objfiles ())
1171 {
1172 objfile->expand_symtabs_matching (NULL, &lookup_name, NULL, NULL,
1173 search_domain);
1174
1175 for (compunit_symtab *cu : objfile->compunits ())
1176 {
1177 struct symtab *symtab = COMPUNIT_FILETABS (cu);
1178
1179 iterate_over_file_blocks (symtab, lookup_name, name_domain,
1180 callback);
1181
1182 if (include_inline)
1183 {
1184 const struct block *block;
1185 int i;
1186
1187 for (i = FIRST_LOCAL_BLOCK;
1188 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
1189 i++)
1190 {
1191 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
1192 state->language->iterate_over_symbols
1193 (block, lookup_name, name_domain,
1194 [&] (block_symbol *bsym)
1195 {
1196 /* Restrict calls to CALLBACK to symbols
1197 representing inline symbols only. */
1198 if (SYMBOL_INLINED (bsym->symbol))
1199 return callback (bsym);
1200 return true;
1201 });
1202 }
1203 }
1204 }
1205 }
1206 }
1207 }
1208
1209 /* Returns the block to be used for symbol searches from
1210 the current location. */
1211
1212 static const struct block *
1213 get_current_search_block (void)
1214 {
1215 /* get_selected_block can change the current language when there is
1216 no selected frame yet. */
1217 scoped_restore_current_language save_language;
1218 return get_selected_block (0);
1219 }
1220
1221 /* Iterate over static and global blocks. */
1222
1223 static void
1224 iterate_over_file_blocks
1225 (struct symtab *symtab, const lookup_name_info &name,
1226 domain_enum domain, gdb::function_view<symbol_found_callback_ftype> callback)
1227 {
1228 const struct block *block;
1229
1230 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
1231 block != NULL;
1232 block = BLOCK_SUPERBLOCK (block))
1233 current_language->iterate_over_symbols (block, name, domain, callback);
1234 }
1235
1236 /* A helper for find_method. This finds all methods in type T of
1237 language T_LANG which match NAME. It adds matching symbol names to
1238 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
1239
1240 static void
1241 find_methods (struct type *t, enum language t_lang, const char *name,
1242 std::vector<const char *> *result_names,
1243 std::vector<struct type *> *superclasses)
1244 {
1245 int ibase;
1246 const char *class_name = t->name ();
1247
1248 /* Ignore this class if it doesn't have a name. This is ugly, but
1249 unless we figure out how to get the physname without the name of
1250 the class, then the loop can't do any good. */
1251 if (class_name)
1252 {
1253 int method_counter;
1254 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1255 symbol_name_matcher_ftype *symbol_name_compare
1256 = language_def (t_lang)->get_symbol_name_matcher (lookup_name);
1257
1258 t = check_typedef (t);
1259
1260 /* Loop over each method name. At this level, all overloads of a name
1261 are counted as a single name. There is an inner loop which loops over
1262 each overload. */
1263
1264 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1265 method_counter >= 0;
1266 --method_counter)
1267 {
1268 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1269
1270 if (symbol_name_compare (method_name, lookup_name, NULL))
1271 {
1272 int field_counter;
1273
1274 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1275 - 1);
1276 field_counter >= 0;
1277 --field_counter)
1278 {
1279 struct fn_field *f;
1280 const char *phys_name;
1281
1282 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1283 if (TYPE_FN_FIELD_STUB (f, field_counter))
1284 continue;
1285 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1286 result_names->push_back (phys_name);
1287 }
1288 }
1289 }
1290 }
1291
1292 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1293 superclasses->push_back (TYPE_BASECLASS (t, ibase));
1294 }
1295
1296 /* Find an instance of the character C in the string S that is outside
1297 of all parenthesis pairs, single-quoted strings, and double-quoted
1298 strings. Also, ignore the char within a template name, like a ','
1299 within foo<int, int>, while considering C++ operator</operator<<. */
1300
1301 const char *
1302 find_toplevel_char (const char *s, char c)
1303 {
1304 int quoted = 0; /* zero if we're not in quotes;
1305 '"' if we're in a double-quoted string;
1306 '\'' if we're in a single-quoted string. */
1307 int depth = 0; /* Number of unclosed parens we've seen. */
1308 const char *scan;
1309
1310 for (scan = s; *scan; scan++)
1311 {
1312 if (quoted)
1313 {
1314 if (*scan == quoted)
1315 quoted = 0;
1316 else if (*scan == '\\' && *(scan + 1))
1317 scan++;
1318 }
1319 else if (*scan == c && ! quoted && depth == 0)
1320 return scan;
1321 else if (*scan == '"' || *scan == '\'')
1322 quoted = *scan;
1323 else if (*scan == '(' || *scan == '<')
1324 depth++;
1325 else if ((*scan == ')' || *scan == '>') && depth > 0)
1326 depth--;
1327 else if (*scan == 'o' && !quoted && depth == 0)
1328 {
1329 /* Handle C++ operator names. */
1330 if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
1331 {
1332 scan += CP_OPERATOR_LEN;
1333 if (*scan == c)
1334 return scan;
1335 while (isspace (*scan))
1336 {
1337 ++scan;
1338 if (*scan == c)
1339 return scan;
1340 }
1341 if (*scan == '\0')
1342 break;
1343
1344 switch (*scan)
1345 {
1346 /* Skip over one less than the appropriate number of
1347 characters: the for loop will skip over the last
1348 one. */
1349 case '<':
1350 if (scan[1] == '<')
1351 {
1352 scan++;
1353 if (*scan == c)
1354 return scan;
1355 }
1356 break;
1357 case '>':
1358 if (scan[1] == '>')
1359 {
1360 scan++;
1361 if (*scan == c)
1362 return scan;
1363 }
1364 break;
1365 }
1366 }
1367 }
1368 }
1369
1370 return 0;
1371 }
1372
1373 /* The string equivalent of find_toplevel_char. Returns a pointer
1374 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1375 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1376
1377 static const char *
1378 find_toplevel_string (const char *haystack, const char *needle)
1379 {
1380 const char *s = haystack;
1381
1382 do
1383 {
1384 s = find_toplevel_char (s, *needle);
1385
1386 if (s != NULL)
1387 {
1388 /* Found first char in HAYSTACK; check rest of string. */
1389 if (startswith (s, needle))
1390 return s;
1391
1392 /* Didn't find it; loop over HAYSTACK, looking for the next
1393 instance of the first character of NEEDLE. */
1394 ++s;
1395 }
1396 }
1397 while (s != NULL && *s != '\0');
1398
1399 /* NEEDLE was not found in HAYSTACK. */
1400 return NULL;
1401 }
1402
1403 /* Convert CANONICAL to its string representation using
1404 symtab_to_fullname for SYMTAB. */
1405
1406 static std::string
1407 canonical_to_fullform (const struct linespec_canonical_name *canonical)
1408 {
1409 if (canonical->symtab == NULL)
1410 return canonical->suffix;
1411 else
1412 return string_printf ("%s:%s", symtab_to_fullname (canonical->symtab),
1413 canonical->suffix);
1414 }
1415
1416 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1417 and store the result in SELF->CANONICAL. */
1418
1419 static void
1420 filter_results (struct linespec_state *self,
1421 std::vector<symtab_and_line> *result,
1422 const std::vector<const char *> &filters)
1423 {
1424 for (const char *name : filters)
1425 {
1426 linespec_sals lsal;
1427
1428 for (size_t j = 0; j < result->size (); ++j)
1429 {
1430 const struct linespec_canonical_name *canonical;
1431
1432 canonical = &self->canonical_names[j];
1433 std::string fullform = canonical_to_fullform (canonical);
1434
1435 if (name == fullform)
1436 lsal.sals.push_back ((*result)[j]);
1437 }
1438
1439 if (!lsal.sals.empty ())
1440 {
1441 lsal.canonical = xstrdup (name);
1442 self->canonical->lsals.push_back (std::move (lsal));
1443 }
1444 }
1445
1446 self->canonical->pre_expanded = 0;
1447 }
1448
1449 /* Store RESULT into SELF->CANONICAL. */
1450
1451 static void
1452 convert_results_to_lsals (struct linespec_state *self,
1453 std::vector<symtab_and_line> *result)
1454 {
1455 struct linespec_sals lsal;
1456
1457 lsal.canonical = NULL;
1458 lsal.sals = std::move (*result);
1459 self->canonical->lsals.push_back (std::move (lsal));
1460 }
1461
1462 /* A structure that contains two string representations of a struct
1463 linespec_canonical_name:
1464 - one where the symtab's fullname is used;
1465 - one where the filename followed the "set filename-display"
1466 setting. */
1467
1468 struct decode_line_2_item
1469 {
1470 decode_line_2_item (std::string &&fullform_, std::string &&displayform_,
1471 bool selected_)
1472 : fullform (std::move (fullform_)),
1473 displayform (std::move (displayform_)),
1474 selected (selected_)
1475 {
1476 }
1477
1478 /* The form using symtab_to_fullname. */
1479 std::string fullform;
1480
1481 /* The form using symtab_to_filename_for_display. */
1482 std::string displayform;
1483
1484 /* Field is initialized to zero and it is set to one if the user
1485 requested breakpoint for this entry. */
1486 unsigned int selected : 1;
1487 };
1488
1489 /* Helper for std::sort to sort decode_line_2_item entries by
1490 DISPLAYFORM and secondarily by FULLFORM. */
1491
1492 static bool
1493 decode_line_2_compare_items (const decode_line_2_item &a,
1494 const decode_line_2_item &b)
1495 {
1496 if (a.displayform != b.displayform)
1497 return a.displayform < b.displayform;
1498 return a.fullform < b.fullform;
1499 }
1500
1501 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1502 will either return normally, throw an exception on multiple
1503 results, or present a menu to the user. On return, the SALS vector
1504 in SELF->CANONICAL is set up properly. */
1505
1506 static void
1507 decode_line_2 (struct linespec_state *self,
1508 std::vector<symtab_and_line> *result,
1509 const char *select_mode)
1510 {
1511 const char *args;
1512 const char *prompt;
1513 int i;
1514 std::vector<const char *> filters;
1515 std::vector<struct decode_line_2_item> items;
1516
1517 gdb_assert (select_mode != multiple_symbols_all);
1518 gdb_assert (self->canonical != NULL);
1519 gdb_assert (!result->empty ());
1520
1521 /* Prepare ITEMS array. */
1522 for (i = 0; i < result->size (); ++i)
1523 {
1524 const struct linespec_canonical_name *canonical;
1525 std::string displayform;
1526
1527 canonical = &self->canonical_names[i];
1528 gdb_assert (canonical->suffix != NULL);
1529
1530 std::string fullform = canonical_to_fullform (canonical);
1531
1532 if (canonical->symtab == NULL)
1533 displayform = canonical->suffix;
1534 else
1535 {
1536 const char *fn_for_display;
1537
1538 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
1539 displayform = string_printf ("%s:%s", fn_for_display,
1540 canonical->suffix);
1541 }
1542
1543 items.emplace_back (std::move (fullform), std::move (displayform),
1544 false);
1545 }
1546
1547 /* Sort the list of method names. */
1548 std::sort (items.begin (), items.end (), decode_line_2_compare_items);
1549
1550 /* Remove entries with the same FULLFORM. */
1551 items.erase (std::unique (items.begin (), items.end (),
1552 [] (const struct decode_line_2_item &a,
1553 const struct decode_line_2_item &b)
1554 {
1555 return a.fullform == b.fullform;
1556 }),
1557 items.end ());
1558
1559 if (select_mode == multiple_symbols_cancel && items.size () > 1)
1560 error (_("canceled because the command is ambiguous\n"
1561 "See set/show multiple-symbol."));
1562
1563 if (select_mode == multiple_symbols_all || items.size () == 1)
1564 {
1565 convert_results_to_lsals (self, result);
1566 return;
1567 }
1568
1569 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1570 for (i = 0; i < items.size (); i++)
1571 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform.c_str ());
1572
1573 prompt = getenv ("PS2");
1574 if (prompt == NULL)
1575 {
1576 prompt = "> ";
1577 }
1578 args = command_line_input (prompt, "overload-choice");
1579
1580 if (args == 0 || *args == 0)
1581 error_no_arg (_("one or more choice numbers"));
1582
1583 number_or_range_parser parser (args);
1584 while (!parser.finished ())
1585 {
1586 int num = parser.get_number ();
1587
1588 if (num == 0)
1589 error (_("canceled"));
1590 else if (num == 1)
1591 {
1592 /* We intentionally make this result in a single breakpoint,
1593 contrary to what older versions of gdb did. The
1594 rationale is that this lets a user get the
1595 multiple_symbols_all behavior even with the 'ask'
1596 setting; and he can get separate breakpoints by entering
1597 "2-57" at the query. */
1598 convert_results_to_lsals (self, result);
1599 return;
1600 }
1601
1602 num -= 2;
1603 if (num >= items.size ())
1604 printf_unfiltered (_("No choice number %d.\n"), num);
1605 else
1606 {
1607 struct decode_line_2_item *item = &items[num];
1608
1609 if (!item->selected)
1610 {
1611 filters.push_back (item->fullform.c_str ());
1612 item->selected = 1;
1613 }
1614 else
1615 {
1616 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1617 num + 2);
1618 }
1619 }
1620 }
1621
1622 filter_results (self, result, filters);
1623 }
1624
1625 \f
1626
1627 /* The parser of linespec itself. */
1628
1629 /* Throw an appropriate error when SYMBOL is not found (optionally in
1630 FILENAME). */
1631
1632 static void ATTRIBUTE_NORETURN
1633 symbol_not_found_error (const char *symbol, const char *filename)
1634 {
1635 if (symbol == NULL)
1636 symbol = "";
1637
1638 if (!have_full_symbols ()
1639 && !have_partial_symbols ()
1640 && !have_minimal_symbols ())
1641 throw_error (NOT_FOUND_ERROR,
1642 _("No symbol table is loaded. Use the \"file\" command."));
1643
1644 /* If SYMBOL starts with '$', the user attempted to either lookup
1645 a function/variable in his code starting with '$' or an internal
1646 variable of that name. Since we do not know which, be concise and
1647 explain both possibilities. */
1648 if (*symbol == '$')
1649 {
1650 if (filename)
1651 throw_error (NOT_FOUND_ERROR,
1652 _("Undefined convenience variable or function \"%s\" "
1653 "not defined in \"%s\"."), symbol, filename);
1654 else
1655 throw_error (NOT_FOUND_ERROR,
1656 _("Undefined convenience variable or function \"%s\" "
1657 "not defined."), symbol);
1658 }
1659 else
1660 {
1661 if (filename)
1662 throw_error (NOT_FOUND_ERROR,
1663 _("Function \"%s\" not defined in \"%s\"."),
1664 symbol, filename);
1665 else
1666 throw_error (NOT_FOUND_ERROR,
1667 _("Function \"%s\" not defined."), symbol);
1668 }
1669 }
1670
1671 /* Throw an appropriate error when an unexpected token is encountered
1672 in the input. */
1673
1674 static void ATTRIBUTE_NORETURN
1675 unexpected_linespec_error (linespec_parser *parser)
1676 {
1677 linespec_token token;
1678 static const char * token_type_strings[]
1679 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1680
1681 /* Get the token that generated the error. */
1682 token = linespec_lexer_lex_one (parser);
1683
1684 /* Finally, throw the error. */
1685 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1686 || token.type == LSTOKEN_KEYWORD)
1687 {
1688 gdb::unique_xmalloc_ptr<char> string = copy_token_string (token);
1689 throw_error (GENERIC_ERROR,
1690 _("malformed linespec error: unexpected %s, \"%s\""),
1691 token_type_strings[token.type], string.get ());
1692 }
1693 else
1694 throw_error (GENERIC_ERROR,
1695 _("malformed linespec error: unexpected %s"),
1696 token_type_strings[token.type]);
1697 }
1698
1699 /* Throw an undefined label error. */
1700
1701 static void ATTRIBUTE_NORETURN
1702 undefined_label_error (const char *function, const char *label)
1703 {
1704 if (function != NULL)
1705 throw_error (NOT_FOUND_ERROR,
1706 _("No label \"%s\" defined in function \"%s\"."),
1707 label, function);
1708 else
1709 throw_error (NOT_FOUND_ERROR,
1710 _("No label \"%s\" defined in current function."),
1711 label);
1712 }
1713
1714 /* Throw a source file not found error. */
1715
1716 static void ATTRIBUTE_NORETURN
1717 source_file_not_found_error (const char *name)
1718 {
1719 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1720 }
1721
1722 /* Unless at EIO, save the current stream position as completion word
1723 point, and consume the next token. */
1724
1725 static linespec_token
1726 save_stream_and_consume_token (linespec_parser *parser)
1727 {
1728 if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
1729 parser->completion_word = PARSER_STREAM (parser);
1730 return linespec_lexer_consume_token (parser);
1731 }
1732
1733 /* See description in linespec.h. */
1734
1735 struct line_offset
1736 linespec_parse_line_offset (const char *string)
1737 {
1738 const char *start = string;
1739 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1740
1741 if (*string == '+')
1742 {
1743 line_offset.sign = LINE_OFFSET_PLUS;
1744 ++string;
1745 }
1746 else if (*string == '-')
1747 {
1748 line_offset.sign = LINE_OFFSET_MINUS;
1749 ++string;
1750 }
1751
1752 if (*string != '\0' && !isdigit (*string))
1753 error (_("malformed line offset: \"%s\""), start);
1754
1755 /* Right now, we only allow base 10 for offsets. */
1756 line_offset.offset = atoi (string);
1757 return line_offset;
1758 }
1759
1760 /* In completion mode, if the user is still typing the number, there's
1761 no possible completion to offer. But if there's already input past
1762 the number, setup to expect NEXT. */
1763
1764 static void
1765 set_completion_after_number (linespec_parser *parser,
1766 linespec_complete_what next)
1767 {
1768 if (*PARSER_STREAM (parser) == ' ')
1769 {
1770 parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
1771 parser->complete_what = next;
1772 }
1773 else
1774 {
1775 parser->completion_word = PARSER_STREAM (parser);
1776 parser->complete_what = linespec_complete_what::NOTHING;
1777 }
1778 }
1779
1780 /* Parse the basic_spec in PARSER's input. */
1781
1782 static void
1783 linespec_parse_basic (linespec_parser *parser)
1784 {
1785 gdb::unique_xmalloc_ptr<char> name;
1786 linespec_token token;
1787 std::vector<block_symbol> symbols;
1788 std::vector<block_symbol> *labels;
1789 std::vector<bound_minimal_symbol> minimal_symbols;
1790
1791 /* Get the next token. */
1792 token = linespec_lexer_lex_one (parser);
1793
1794 /* If it is EOI or KEYWORD, issue an error. */
1795 if (token.type == LSTOKEN_KEYWORD)
1796 {
1797 parser->complete_what = linespec_complete_what::NOTHING;
1798 unexpected_linespec_error (parser);
1799 }
1800 else if (token.type == LSTOKEN_EOI)
1801 {
1802 unexpected_linespec_error (parser);
1803 }
1804 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1805 else if (token.type == LSTOKEN_NUMBER)
1806 {
1807 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1808
1809 /* Record the line offset and get the next token. */
1810 name = copy_token_string (token);
1811 PARSER_EXPLICIT (parser)->line_offset
1812 = linespec_parse_line_offset (name.get ());
1813
1814 /* Get the next token. */
1815 token = linespec_lexer_consume_token (parser);
1816
1817 /* If the next token is a comma, stop parsing and return. */
1818 if (token.type == LSTOKEN_COMMA)
1819 {
1820 parser->complete_what = linespec_complete_what::NOTHING;
1821 return;
1822 }
1823
1824 /* If the next token is anything but EOI or KEYWORD, issue
1825 an error. */
1826 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1827 unexpected_linespec_error (parser);
1828 }
1829
1830 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1831 return;
1832
1833 /* Next token must be LSTOKEN_STRING. */
1834 if (token.type != LSTOKEN_STRING)
1835 {
1836 parser->complete_what = linespec_complete_what::NOTHING;
1837 unexpected_linespec_error (parser);
1838 }
1839
1840 /* The current token will contain the name of a function, method,
1841 or label. */
1842 name = copy_token_string (token);
1843
1844 if (parser->completion_tracker != NULL)
1845 {
1846 /* If the function name ends with a ":", then this may be an
1847 incomplete "::" scope operator instead of a label separator.
1848 E.g.,
1849 "b klass:<tab>"
1850 which should expand to:
1851 "b klass::method()"
1852
1853 Do a tentative completion assuming the later. If we find
1854 completions, advance the stream past the colon token and make
1855 it part of the function name/token. */
1856
1857 if (!parser->completion_quote_char
1858 && strcmp (PARSER_STREAM (parser), ":") == 0)
1859 {
1860 completion_tracker tmp_tracker;
1861 const char *source_filename
1862 = PARSER_EXPLICIT (parser)->source_filename;
1863 symbol_name_match_type match_type
1864 = PARSER_EXPLICIT (parser)->func_name_match_type;
1865
1866 linespec_complete_function (tmp_tracker,
1867 parser->completion_word,
1868 match_type,
1869 source_filename);
1870
1871 if (tmp_tracker.have_completions ())
1872 {
1873 PARSER_STREAM (parser)++;
1874 LS_TOKEN_STOKEN (token).length++;
1875
1876 name.reset (savestring (parser->completion_word,
1877 (PARSER_STREAM (parser)
1878 - parser->completion_word)));
1879 }
1880 }
1881
1882 PARSER_EXPLICIT (parser)->function_name = name.release ();
1883 }
1884 else
1885 {
1886 /* Try looking it up as a function/method. */
1887 find_linespec_symbols (PARSER_STATE (parser),
1888 PARSER_RESULT (parser)->file_symtabs, name.get (),
1889 PARSER_EXPLICIT (parser)->func_name_match_type,
1890 &symbols, &minimal_symbols);
1891
1892 if (!symbols.empty () || !minimal_symbols.empty ())
1893 {
1894 PARSER_RESULT (parser)->function_symbols
1895 = new std::vector<block_symbol> (std::move (symbols));
1896 PARSER_RESULT (parser)->minimal_symbols
1897 = new std::vector<bound_minimal_symbol>
1898 (std::move (minimal_symbols));
1899 PARSER_EXPLICIT (parser)->function_name = name.release ();
1900 }
1901 else
1902 {
1903 /* NAME was not a function or a method. So it must be a label
1904 name or user specified variable like "break foo.c:$zippo". */
1905 labels = find_label_symbols (PARSER_STATE (parser), NULL,
1906 &symbols, name.get ());
1907 if (labels != NULL)
1908 {
1909 PARSER_RESULT (parser)->labels.label_symbols = labels;
1910 PARSER_RESULT (parser)->labels.function_symbols
1911 = new std::vector<block_symbol> (std::move (symbols));
1912 PARSER_EXPLICIT (parser)->label_name = name.release ();
1913 }
1914 else if (token.type == LSTOKEN_STRING
1915 && *LS_TOKEN_STOKEN (token).ptr == '$')
1916 {
1917 /* User specified a convenience variable or history value. */
1918 PARSER_EXPLICIT (parser)->line_offset
1919 = linespec_parse_variable (PARSER_STATE (parser), name.get ());
1920
1921 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
1922 {
1923 /* The user-specified variable was not valid. Do not
1924 throw an error here. parse_linespec will do it for us. */
1925 PARSER_EXPLICIT (parser)->function_name = name.release ();
1926 return;
1927 }
1928 }
1929 else
1930 {
1931 /* The name is also not a label. Abort parsing. Do not throw
1932 an error here. parse_linespec will do it for us. */
1933
1934 /* Save a copy of the name we were trying to lookup. */
1935 PARSER_EXPLICIT (parser)->function_name = name.release ();
1936 return;
1937 }
1938 }
1939 }
1940
1941 int previous_qc = parser->completion_quote_char;
1942
1943 /* Get the next token. */
1944 token = linespec_lexer_consume_token (parser);
1945
1946 if (token.type == LSTOKEN_EOI)
1947 {
1948 if (previous_qc && !parser->completion_quote_char)
1949 parser->complete_what = linespec_complete_what::KEYWORD;
1950 }
1951 else if (token.type == LSTOKEN_COLON)
1952 {
1953 /* User specified a label or a lineno. */
1954 token = linespec_lexer_consume_token (parser);
1955
1956 if (token.type == LSTOKEN_NUMBER)
1957 {
1958 /* User specified an offset. Record the line offset and
1959 get the next token. */
1960 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1961
1962 name = copy_token_string (token);
1963 PARSER_EXPLICIT (parser)->line_offset
1964 = linespec_parse_line_offset (name.get ());
1965
1966 /* Get the next token. */
1967 token = linespec_lexer_consume_token (parser);
1968 }
1969 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
1970 {
1971 parser->complete_what = linespec_complete_what::LABEL;
1972 }
1973 else if (token.type == LSTOKEN_STRING)
1974 {
1975 parser->complete_what = linespec_complete_what::LABEL;
1976
1977 /* If we have text after the label separated by whitespace
1978 (e.g., "b func():lab i<tab>"), don't consider it part of
1979 the label. In completion mode that should complete to
1980 "if", in normal mode, the 'i' should be treated as
1981 garbage. */
1982 if (parser->completion_quote_char == '\0')
1983 {
1984 const char *ptr = LS_TOKEN_STOKEN (token).ptr;
1985 for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
1986 {
1987 if (ptr[i] == ' ')
1988 {
1989 LS_TOKEN_STOKEN (token).length = i;
1990 PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
1991 break;
1992 }
1993 }
1994 }
1995
1996 if (parser->completion_tracker != NULL)
1997 {
1998 if (PARSER_STREAM (parser)[-1] == ' ')
1999 {
2000 parser->completion_word = PARSER_STREAM (parser);
2001 parser->complete_what = linespec_complete_what::KEYWORD;
2002 }
2003 }
2004 else
2005 {
2006 /* Grab a copy of the label's name and look it up. */
2007 name = copy_token_string (token);
2008 labels
2009 = find_label_symbols (PARSER_STATE (parser),
2010 PARSER_RESULT (parser)->function_symbols,
2011 &symbols, name.get ());
2012
2013 if (labels != NULL)
2014 {
2015 PARSER_RESULT (parser)->labels.label_symbols = labels;
2016 PARSER_RESULT (parser)->labels.function_symbols
2017 = new std::vector<block_symbol> (std::move (symbols));
2018 PARSER_EXPLICIT (parser)->label_name = name.release ();
2019 }
2020 else
2021 {
2022 /* We don't know what it was, but it isn't a label. */
2023 undefined_label_error
2024 (PARSER_EXPLICIT (parser)->function_name, name.get ());
2025 }
2026
2027 }
2028
2029 /* Check for a line offset. */
2030 token = save_stream_and_consume_token (parser);
2031 if (token.type == LSTOKEN_COLON)
2032 {
2033 /* Get the next token. */
2034 token = linespec_lexer_consume_token (parser);
2035
2036 /* It must be a line offset. */
2037 if (token.type != LSTOKEN_NUMBER)
2038 unexpected_linespec_error (parser);
2039
2040 /* Record the line offset and get the next token. */
2041 name = copy_token_string (token);
2042
2043 PARSER_EXPLICIT (parser)->line_offset
2044 = linespec_parse_line_offset (name.get ());
2045
2046 /* Get the next token. */
2047 token = linespec_lexer_consume_token (parser);
2048 }
2049 }
2050 else
2051 {
2052 /* Trailing ':' in the input. Issue an error. */
2053 unexpected_linespec_error (parser);
2054 }
2055 }
2056 }
2057
2058 /* Canonicalize the linespec contained in LS. The result is saved into
2059 STATE->canonical. This function handles both linespec and explicit
2060 locations. */
2061
2062 static void
2063 canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
2064 {
2065 struct event_location *canon;
2066 struct explicit_location *explicit_loc;
2067
2068 /* If canonicalization was not requested, no need to do anything. */
2069 if (!state->canonical)
2070 return;
2071
2072 /* Save everything as an explicit location. */
2073 state->canonical->location
2074 = new_explicit_location (&ls->explicit_loc);
2075 canon = state->canonical->location.get ();
2076 explicit_loc = get_explicit_location (canon);
2077
2078 if (explicit_loc->label_name != NULL)
2079 {
2080 state->canonical->special_display = 1;
2081
2082 if (explicit_loc->function_name == NULL)
2083 {
2084 /* No function was specified, so add the symbol name. */
2085 gdb_assert (!ls->labels.function_symbols->empty ()
2086 && (ls->labels.function_symbols->size () == 1));
2087 block_symbol s = ls->labels.function_symbols->front ();
2088 explicit_loc->function_name = xstrdup (s.symbol->natural_name ());
2089 }
2090 }
2091
2092 /* If this location originally came from a linespec, save a string
2093 representation of it for display and saving to file. */
2094 if (state->is_linespec)
2095 {
2096 char *linespec = explicit_location_to_linespec (explicit_loc);
2097
2098 set_event_location_string (canon, linespec);
2099 xfree (linespec);
2100 }
2101 }
2102
2103 /* Given a line offset in LS, construct the relevant SALs. */
2104
2105 static std::vector<symtab_and_line>
2106 create_sals_line_offset (struct linespec_state *self,
2107 linespec_p ls)
2108 {
2109 int use_default = 0;
2110
2111 /* This is where we need to make sure we have good defaults.
2112 We must guarantee that this section of code is never executed
2113 when we are called with just a function name, since
2114 set_default_source_symtab_and_line uses
2115 select_source_symtab that calls us with such an argument. */
2116
2117 if (ls->file_symtabs->size () == 1
2118 && ls->file_symtabs->front () == nullptr)
2119 {
2120 set_current_program_space (self->program_space);
2121
2122 /* Make sure we have at least a default source line. */
2123 set_default_source_symtab_and_line ();
2124 initialize_defaults (&self->default_symtab, &self->default_line);
2125 *ls->file_symtabs
2126 = collect_symtabs_from_filename (self->default_symtab->filename,
2127 self->search_pspace);
2128 use_default = 1;
2129 }
2130
2131 symtab_and_line val;
2132 val.line = ls->explicit_loc.line_offset.offset;
2133 switch (ls->explicit_loc.line_offset.sign)
2134 {
2135 case LINE_OFFSET_PLUS:
2136 if (ls->explicit_loc.line_offset.offset == 0)
2137 val.line = 5;
2138 if (use_default)
2139 val.line = self->default_line + val.line;
2140 break;
2141
2142 case LINE_OFFSET_MINUS:
2143 if (ls->explicit_loc.line_offset.offset == 0)
2144 val.line = 15;
2145 if (use_default)
2146 val.line = self->default_line - val.line;
2147 else
2148 val.line = -val.line;
2149 break;
2150
2151 case LINE_OFFSET_NONE:
2152 break; /* No need to adjust val.line. */
2153 }
2154
2155 std::vector<symtab_and_line> values;
2156 if (self->list_mode)
2157 values = decode_digits_list_mode (self, ls, val);
2158 else
2159 {
2160 struct linetable_entry *best_entry = NULL;
2161 int i, j;
2162
2163 std::vector<symtab_and_line> intermediate_results
2164 = decode_digits_ordinary (self, ls, val.line, &best_entry);
2165 if (intermediate_results.empty () && best_entry != NULL)
2166 intermediate_results = decode_digits_ordinary (self, ls,
2167 best_entry->line,
2168 &best_entry);
2169
2170 /* For optimized code, the compiler can scatter one source line
2171 across disjoint ranges of PC values, even when no duplicate
2172 functions or inline functions are involved. For example,
2173 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2174 function can result in two PC ranges. In this case, we don't
2175 want to set a breakpoint on the first PC of each range. To filter
2176 such cases, we use containing blocks -- for each PC found
2177 above, we see if there are other PCs that are in the same
2178 block. If yes, the other PCs are filtered out. */
2179
2180 gdb::def_vector<int> filter (intermediate_results.size ());
2181 gdb::def_vector<const block *> blocks (intermediate_results.size ());
2182
2183 for (i = 0; i < intermediate_results.size (); ++i)
2184 {
2185 set_current_program_space (intermediate_results[i].pspace);
2186
2187 filter[i] = 1;
2188 blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
2189 intermediate_results[i].section);
2190 }
2191
2192 for (i = 0; i < intermediate_results.size (); ++i)
2193 {
2194 if (blocks[i] != NULL)
2195 for (j = i + 1; j < intermediate_results.size (); ++j)
2196 {
2197 if (blocks[j] == blocks[i])
2198 {
2199 filter[j] = 0;
2200 break;
2201 }
2202 }
2203 }
2204
2205 for (i = 0; i < intermediate_results.size (); ++i)
2206 if (filter[i])
2207 {
2208 struct symbol *sym = (blocks[i]
2209 ? block_containing_function (blocks[i])
2210 : NULL);
2211
2212 if (self->funfirstline)
2213 skip_prologue_sal (&intermediate_results[i]);
2214 intermediate_results[i].symbol = sym;
2215 add_sal_to_sals (self, &values, &intermediate_results[i],
2216 sym ? sym->natural_name () : NULL, 0);
2217 }
2218 }
2219
2220 if (values.empty ())
2221 {
2222 if (ls->explicit_loc.source_filename)
2223 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
2224 val.line, ls->explicit_loc.source_filename);
2225 else
2226 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2227 val.line);
2228 }
2229
2230 return values;
2231 }
2232
2233 /* Convert the given ADDRESS into SaLs. */
2234
2235 static std::vector<symtab_and_line>
2236 convert_address_location_to_sals (struct linespec_state *self,
2237 CORE_ADDR address)
2238 {
2239 symtab_and_line sal = find_pc_line (address, 0);
2240 sal.pc = address;
2241 sal.section = find_pc_overlay (address);
2242 sal.explicit_pc = 1;
2243 sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
2244
2245 std::vector<symtab_and_line> sals;
2246 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
2247
2248 return sals;
2249 }
2250
2251 /* Create and return SALs from the linespec LS. */
2252
2253 static std::vector<symtab_and_line>
2254 convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
2255 {
2256 std::vector<symtab_and_line> sals;
2257
2258 if (ls->labels.label_symbols != NULL)
2259 {
2260 /* We have just a bunch of functions/methods or labels. */
2261 struct symtab_and_line sal;
2262
2263 for (const auto &sym : *ls->labels.label_symbols)
2264 {
2265 struct program_space *pspace
2266 = SYMTAB_PSPACE (symbol_symtab (sym.symbol));
2267
2268 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
2269 && maybe_add_address (state->addr_set, pspace, sal.pc))
2270 add_sal_to_sals (state, &sals, &sal,
2271 sym.symbol->natural_name (), 0);
2272 }
2273 }
2274 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2275 {
2276 /* We have just a bunch of functions and/or methods. */
2277 if (ls->function_symbols != NULL)
2278 {
2279 /* Sort symbols so that symbols with the same program space are next
2280 to each other. */
2281 std::sort (ls->function_symbols->begin (),
2282 ls->function_symbols->end (),
2283 compare_symbols);
2284
2285 for (const auto &sym : *ls->function_symbols)
2286 {
2287 program_space *pspace
2288 = SYMTAB_PSPACE (symbol_symtab (sym.symbol));
2289 set_current_program_space (pspace);
2290
2291 /* Don't skip to the first line of the function if we
2292 had found an ifunc minimal symbol for this function,
2293 because that means that this function is an ifunc
2294 resolver with the same name as the ifunc itself. */
2295 bool found_ifunc = false;
2296
2297 if (state->funfirstline
2298 && ls->minimal_symbols != NULL
2299 && SYMBOL_CLASS (sym.symbol) == LOC_BLOCK)
2300 {
2301 const CORE_ADDR addr
2302 = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym.symbol));
2303
2304 for (const auto &elem : *ls->minimal_symbols)
2305 {
2306 if (MSYMBOL_TYPE (elem.minsym) == mst_text_gnu_ifunc
2307 || MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
2308 {
2309 CORE_ADDR msym_addr = BMSYMBOL_VALUE_ADDRESS (elem);
2310 if (MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
2311 {
2312 struct gdbarch *gdbarch
2313 = elem.objfile->arch ();
2314 msym_addr
2315 = (gdbarch_convert_from_func_ptr_addr
2316 (gdbarch,
2317 msym_addr,
2318 current_inferior ()->top_target ()));
2319 }
2320
2321 if (msym_addr == addr)
2322 {
2323 found_ifunc = true;
2324 break;
2325 }
2326 }
2327 }
2328 }
2329
2330 if (!found_ifunc)
2331 {
2332 symtab_and_line sal;
2333 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
2334 && maybe_add_address (state->addr_set, pspace, sal.pc))
2335 add_sal_to_sals (state, &sals, &sal,
2336 sym.symbol->natural_name (), 0);
2337 }
2338 }
2339 }
2340
2341 if (ls->minimal_symbols != NULL)
2342 {
2343 /* Sort minimal symbols by program space, too */
2344 std::sort (ls->minimal_symbols->begin (),
2345 ls->minimal_symbols->end (),
2346 compare_msymbols);
2347
2348 for (const auto &elem : *ls->minimal_symbols)
2349 {
2350 program_space *pspace = elem.objfile->pspace;
2351 set_current_program_space (pspace);
2352 minsym_found (state, elem.objfile, elem.minsym, &sals);
2353 }
2354 }
2355 }
2356 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
2357 {
2358 /* Only an offset was specified. */
2359 sals = create_sals_line_offset (state, ls);
2360
2361 /* Make sure we have a filename for canonicalization. */
2362 if (ls->explicit_loc.source_filename == NULL)
2363 {
2364 const char *fullname = symtab_to_fullname (state->default_symtab);
2365
2366 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2367 form so that displaying SOURCE_FILENAME can follow the current
2368 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2369 it has been kept for code simplicity only in absolute form. */
2370 ls->explicit_loc.source_filename = xstrdup (fullname);
2371 }
2372 }
2373 else
2374 {
2375 /* We haven't found any results... */
2376 return sals;
2377 }
2378
2379 canonicalize_linespec (state, ls);
2380
2381 if (!sals.empty () && state->canonical != NULL)
2382 state->canonical->pre_expanded = 1;
2383
2384 return sals;
2385 }
2386
2387 /* Build RESULT from the explicit location components SOURCE_FILENAME,
2388 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2389
2390 static void
2391 convert_explicit_location_to_linespec (struct linespec_state *self,
2392 linespec_p result,
2393 const char *source_filename,
2394 const char *function_name,
2395 symbol_name_match_type fname_match_type,
2396 const char *label_name,
2397 struct line_offset line_offset)
2398 {
2399 std::vector<block_symbol> symbols;
2400 std::vector<block_symbol> *labels;
2401 std::vector<bound_minimal_symbol> minimal_symbols;
2402
2403 result->explicit_loc.func_name_match_type = fname_match_type;
2404
2405 if (source_filename != NULL)
2406 {
2407 try
2408 {
2409 *result->file_symtabs
2410 = symtabs_from_filename (source_filename, self->search_pspace);
2411 }
2412 catch (const gdb_exception_error &except)
2413 {
2414 source_file_not_found_error (source_filename);
2415 }
2416 result->explicit_loc.source_filename = xstrdup (source_filename);
2417 }
2418 else
2419 {
2420 /* A NULL entry means to use the default symtab. */
2421 result->file_symtabs->push_back (nullptr);
2422 }
2423
2424 if (function_name != NULL)
2425 {
2426 find_linespec_symbols (self, result->file_symtabs,
2427 function_name, fname_match_type,
2428 &symbols, &minimal_symbols);
2429
2430 if (symbols.empty () && minimal_symbols.empty ())
2431 symbol_not_found_error (function_name,
2432 result->explicit_loc.source_filename);
2433
2434 result->explicit_loc.function_name = xstrdup (function_name);
2435 result->function_symbols
2436 = new std::vector<block_symbol> (std::move (symbols));
2437 result->minimal_symbols
2438 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
2439 }
2440
2441 if (label_name != NULL)
2442 {
2443 labels = find_label_symbols (self, result->function_symbols,
2444 &symbols, label_name);
2445
2446 if (labels == NULL)
2447 undefined_label_error (result->explicit_loc.function_name,
2448 label_name);
2449
2450 result->explicit_loc.label_name = xstrdup (label_name);
2451 result->labels.label_symbols = labels;
2452 result->labels.function_symbols
2453 = new std::vector<block_symbol> (std::move (symbols));
2454 }
2455
2456 if (line_offset.sign != LINE_OFFSET_UNKNOWN)
2457 result->explicit_loc.line_offset = line_offset;
2458 }
2459
2460 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2461
2462 static std::vector<symtab_and_line>
2463 convert_explicit_location_to_sals (struct linespec_state *self,
2464 linespec_p result,
2465 const struct explicit_location *explicit_loc)
2466 {
2467 convert_explicit_location_to_linespec (self, result,
2468 explicit_loc->source_filename,
2469 explicit_loc->function_name,
2470 explicit_loc->func_name_match_type,
2471 explicit_loc->label_name,
2472 explicit_loc->line_offset);
2473 return convert_linespec_to_sals (self, result);
2474 }
2475
2476 /* Parse a string that specifies a linespec.
2477
2478 The basic grammar of linespecs:
2479
2480 linespec -> var_spec | basic_spec
2481 var_spec -> '$' (STRING | NUMBER)
2482
2483 basic_spec -> file_offset_spec | function_spec | label_spec
2484 file_offset_spec -> opt_file_spec offset_spec
2485 function_spec -> opt_file_spec function_name_spec opt_label_spec
2486 label_spec -> label_name_spec
2487
2488 opt_file_spec -> "" | file_name_spec ':'
2489 opt_label_spec -> "" | ':' label_name_spec
2490
2491 file_name_spec -> STRING
2492 function_name_spec -> STRING
2493 label_name_spec -> STRING
2494 function_name_spec -> STRING
2495 offset_spec -> NUMBER
2496 -> '+' NUMBER
2497 -> '-' NUMBER
2498
2499 This may all be followed by several keywords such as "if EXPR",
2500 which we ignore.
2501
2502 A comma will terminate parsing.
2503
2504 The function may be an undebuggable function found in minimal symbol table.
2505
2506 If the argument FUNFIRSTLINE is nonzero, we want the first line
2507 of real code inside a function when a function is specified, and it is
2508 not OK to specify a variable or type to get its line number.
2509
2510 DEFAULT_SYMTAB specifies the file to use if none is specified.
2511 It defaults to current_source_symtab.
2512 DEFAULT_LINE specifies the line number to use for relative
2513 line numbers (that start with signs). Defaults to current_source_line.
2514 If CANONICAL is non-NULL, store an array of strings containing the canonical
2515 line specs there if necessary. Currently overloaded member functions and
2516 line numbers or static functions without a filename yield a canonical
2517 line spec. The array and the line spec strings are allocated on the heap,
2518 it is the callers responsibility to free them.
2519
2520 Note that it is possible to return zero for the symtab
2521 if no file is validly specified. Callers must check that.
2522 Also, the line number returned may be invalid. */
2523
2524 /* Parse the linespec in ARG. MATCH_TYPE indicates how function names
2525 should be matched. */
2526
2527 static std::vector<symtab_and_line>
2528 parse_linespec (linespec_parser *parser, const char *arg,
2529 symbol_name_match_type match_type)
2530 {
2531 linespec_token token;
2532 struct gdb_exception file_exception;
2533
2534 /* A special case to start. It has become quite popular for
2535 IDEs to work around bugs in the previous parser by quoting
2536 the entire linespec, so we attempt to deal with this nicely. */
2537 parser->is_quote_enclosed = 0;
2538 if (parser->completion_tracker == NULL
2539 && !is_ada_operator (arg)
2540 && strchr (linespec_quote_characters, *arg) != NULL)
2541 {
2542 const char *end;
2543
2544 end = skip_quote_char (arg + 1, *arg);
2545 if (end != NULL && is_closing_quote_enclosed (end))
2546 {
2547 /* Here's the special case. Skip ARG past the initial
2548 quote. */
2549 ++arg;
2550 parser->is_quote_enclosed = 1;
2551 }
2552 }
2553
2554 parser->lexer.saved_arg = arg;
2555 parser->lexer.stream = arg;
2556 parser->completion_word = arg;
2557 parser->complete_what = linespec_complete_what::FUNCTION;
2558 PARSER_EXPLICIT (parser)->func_name_match_type = match_type;
2559
2560 /* Initialize the default symtab and line offset. */
2561 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2562 &PARSER_STATE (parser)->default_line);
2563
2564 /* Objective-C shortcut. */
2565 if (parser->completion_tracker == NULL)
2566 {
2567 std::vector<symtab_and_line> values
2568 = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2569 if (!values.empty ())
2570 return values;
2571 }
2572 else
2573 {
2574 /* "-"/"+" is either an objc selector, or a number. There's
2575 nothing to complete the latter to, so just let the caller
2576 complete on functions, which finds objc selectors, if there's
2577 any. */
2578 if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
2579 return {};
2580 }
2581
2582 /* Start parsing. */
2583
2584 /* Get the first token. */
2585 token = linespec_lexer_consume_token (parser);
2586
2587 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2588 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
2589 {
2590 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2591 if (parser->completion_tracker == NULL)
2592 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
2593
2594 /* User specified a convenience variable or history value. */
2595 gdb::unique_xmalloc_ptr<char> var = copy_token_string (token);
2596 PARSER_EXPLICIT (parser)->line_offset
2597 = linespec_parse_variable (PARSER_STATE (parser), var.get ());
2598
2599 /* If a line_offset wasn't found (VAR is the name of a user
2600 variable/function), then skip to normal symbol processing. */
2601 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
2602 {
2603 /* Consume this token. */
2604 linespec_lexer_consume_token (parser);
2605
2606 goto convert_to_sals;
2607 }
2608 }
2609 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
2610 {
2611 /* Let the default linespec_complete_what::FUNCTION kick in. */
2612 unexpected_linespec_error (parser);
2613 }
2614 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
2615 {
2616 parser->complete_what = linespec_complete_what::NOTHING;
2617 unexpected_linespec_error (parser);
2618 }
2619
2620 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2621 this token cannot represent a filename. */
2622 token = linespec_lexer_peek_token (parser);
2623
2624 if (token.type == LSTOKEN_COLON)
2625 {
2626 /* Get the current token again and extract the filename. */
2627 token = linespec_lexer_lex_one (parser);
2628 gdb::unique_xmalloc_ptr<char> user_filename = copy_token_string (token);
2629
2630 /* Check if the input is a filename. */
2631 try
2632 {
2633 *PARSER_RESULT (parser)->file_symtabs
2634 = symtabs_from_filename (user_filename.get (),
2635 PARSER_STATE (parser)->search_pspace);
2636 }
2637 catch (gdb_exception_error &ex)
2638 {
2639 file_exception = std::move (ex);
2640 }
2641
2642 if (file_exception.reason >= 0)
2643 {
2644 /* Symtabs were found for the file. Record the filename. */
2645 PARSER_EXPLICIT (parser)->source_filename = user_filename.release ();
2646
2647 /* Get the next token. */
2648 token = linespec_lexer_consume_token (parser);
2649
2650 /* This is LSTOKEN_COLON; consume it. */
2651 linespec_lexer_consume_token (parser);
2652 }
2653 else
2654 {
2655 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2656 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
2657 }
2658 }
2659 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2660 else if (parser->completion_tracker == NULL
2661 && (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2662 && token.type != LSTOKEN_COMMA))
2663 {
2664 /* TOKEN is the _next_ token, not the one currently in the parser.
2665 Consuming the token will give the correct error message. */
2666 linespec_lexer_consume_token (parser);
2667 unexpected_linespec_error (parser);
2668 }
2669 else
2670 {
2671 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2672 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
2673 }
2674
2675 /* Parse the rest of the linespec. */
2676 linespec_parse_basic (parser);
2677
2678 if (parser->completion_tracker == NULL
2679 && PARSER_RESULT (parser)->function_symbols == NULL
2680 && PARSER_RESULT (parser)->labels.label_symbols == NULL
2681 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
2682 && PARSER_RESULT (parser)->minimal_symbols == NULL)
2683 {
2684 /* The linespec didn't parse. Re-throw the file exception if
2685 there was one. */
2686 if (file_exception.reason < 0)
2687 throw_exception (std::move (file_exception));
2688
2689 /* Otherwise, the symbol is not found. */
2690 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2691 PARSER_EXPLICIT (parser)->source_filename);
2692 }
2693
2694 convert_to_sals:
2695
2696 /* Get the last token and record how much of the input was parsed,
2697 if necessary. */
2698 token = linespec_lexer_lex_one (parser);
2699 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
2700 unexpected_linespec_error (parser);
2701 else if (token.type == LSTOKEN_KEYWORD)
2702 {
2703 /* Setup the completion word past the keyword. Lexing never
2704 advances past a keyword automatically, so skip it
2705 manually. */
2706 parser->completion_word
2707 = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
2708 parser->complete_what = linespec_complete_what::EXPRESSION;
2709 }
2710
2711 /* Convert the data in PARSER_RESULT to SALs. */
2712 if (parser->completion_tracker == NULL)
2713 return convert_linespec_to_sals (PARSER_STATE (parser),
2714 PARSER_RESULT (parser));
2715
2716 return {};
2717 }
2718
2719
2720 /* A constructor for linespec_state. */
2721
2722 static void
2723 linespec_state_constructor (struct linespec_state *self,
2724 int flags, const struct language_defn *language,
2725 struct program_space *search_pspace,
2726 struct symtab *default_symtab,
2727 int default_line,
2728 struct linespec_result *canonical)
2729 {
2730 memset (self, 0, sizeof (*self));
2731 self->language = language;
2732 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2733 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
2734 self->search_pspace = search_pspace;
2735 self->default_symtab = default_symtab;
2736 self->default_line = default_line;
2737 self->canonical = canonical;
2738 self->program_space = current_program_space;
2739 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2740 xfree, xcalloc, xfree);
2741 self->is_linespec = 0;
2742 }
2743
2744 /* Initialize a new linespec parser. */
2745
2746 linespec_parser::linespec_parser (int flags,
2747 const struct language_defn *language,
2748 struct program_space *search_pspace,
2749 struct symtab *default_symtab,
2750 int default_line,
2751 struct linespec_result *canonical)
2752 {
2753 lexer.current.type = LSTOKEN_CONSUMED;
2754 PARSER_RESULT (this)->file_symtabs = new std::vector<symtab *> ();
2755 PARSER_EXPLICIT (this)->func_name_match_type
2756 = symbol_name_match_type::WILD;
2757 PARSER_EXPLICIT (this)->line_offset.sign = LINE_OFFSET_UNKNOWN;
2758 linespec_state_constructor (PARSER_STATE (this), flags, language,
2759 search_pspace,
2760 default_symtab, default_line, canonical);
2761 }
2762
2763 /* A destructor for linespec_state. */
2764
2765 static void
2766 linespec_state_destructor (struct linespec_state *self)
2767 {
2768 htab_delete (self->addr_set);
2769 xfree (self->canonical_names);
2770 }
2771
2772 /* Delete a linespec parser. */
2773
2774 linespec_parser::~linespec_parser ()
2775 {
2776 xfree (PARSER_EXPLICIT (this)->source_filename);
2777 xfree (PARSER_EXPLICIT (this)->label_name);
2778 xfree (PARSER_EXPLICIT (this)->function_name);
2779
2780 delete PARSER_RESULT (this)->file_symtabs;
2781 delete PARSER_RESULT (this)->function_symbols;
2782 delete PARSER_RESULT (this)->minimal_symbols;
2783 delete PARSER_RESULT (this)->labels.label_symbols;
2784 delete PARSER_RESULT (this)->labels.function_symbols;
2785
2786 linespec_state_destructor (PARSER_STATE (this));
2787 }
2788
2789 /* See description in linespec.h. */
2790
2791 void
2792 linespec_lex_to_end (const char **stringp)
2793 {
2794 linespec_token token;
2795 const char *orig;
2796
2797 if (stringp == NULL || *stringp == NULL)
2798 return;
2799
2800 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
2801 parser.lexer.saved_arg = *stringp;
2802 PARSER_STREAM (&parser) = orig = *stringp;
2803
2804 do
2805 {
2806 /* Stop before any comma tokens; we need it to keep it
2807 as the next token in the string. */
2808 token = linespec_lexer_peek_token (&parser);
2809 if (token.type == LSTOKEN_COMMA)
2810 break;
2811 token = linespec_lexer_consume_token (&parser);
2812 }
2813 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2814
2815 *stringp += PARSER_STREAM (&parser) - orig;
2816 }
2817
2818 /* See linespec.h. */
2819
2820 void
2821 linespec_complete_function (completion_tracker &tracker,
2822 const char *function,
2823 symbol_name_match_type func_match_type,
2824 const char *source_filename)
2825 {
2826 complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
2827
2828 if (source_filename != NULL)
2829 {
2830 collect_file_symbol_completion_matches (tracker, mode, func_match_type,
2831 function, function, source_filename);
2832 }
2833 else
2834 {
2835 collect_symbol_completion_matches (tracker, mode, func_match_type,
2836 function, function);
2837
2838 }
2839 }
2840
2841 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2842 only meaningful if COMPONENT is FUNCTION. */
2843
2844 static void
2845 complete_linespec_component (linespec_parser *parser,
2846 completion_tracker &tracker,
2847 const char *text,
2848 linespec_complete_what component,
2849 const char *source_filename)
2850 {
2851 if (component == linespec_complete_what::KEYWORD)
2852 {
2853 complete_on_enum (tracker, linespec_keywords, text, text);
2854 }
2855 else if (component == linespec_complete_what::EXPRESSION)
2856 {
2857 const char *word
2858 = advance_to_expression_complete_word_point (tracker, text);
2859 complete_expression (tracker, text, word);
2860 }
2861 else if (component == linespec_complete_what::FUNCTION)
2862 {
2863 completion_list fn_list;
2864
2865 symbol_name_match_type match_type
2866 = PARSER_EXPLICIT (parser)->func_name_match_type;
2867 linespec_complete_function (tracker, text, match_type, source_filename);
2868 if (source_filename == NULL)
2869 {
2870 /* Haven't seen a source component, like in "b
2871 file.c:function[TAB]". Maybe this wasn't a function, but
2872 a filename instead, like "b file.[TAB]". */
2873 fn_list = complete_source_filenames (text);
2874 }
2875
2876 /* If we only have a single filename completion, append a ':' for
2877 the user, since that's the only thing that can usefully follow
2878 the filename. */
2879 if (fn_list.size () == 1 && !tracker.have_completions ())
2880 {
2881 char *fn = fn_list[0].release ();
2882
2883 /* If we also need to append a quote char, it needs to be
2884 appended before the ':'. Append it now, and make ':' the
2885 new "quote" char. */
2886 if (tracker.quote_char ())
2887 {
2888 char quote_char_str[2] = { (char) tracker.quote_char () };
2889
2890 fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
2891 tracker.set_quote_char (':');
2892 }
2893 else
2894 fn = reconcat (fn, fn, ":", (char *) NULL);
2895 fn_list[0].reset (fn);
2896
2897 /* Tell readline to skip appending a space. */
2898 tracker.set_suppress_append_ws (true);
2899 }
2900 tracker.add_completions (std::move (fn_list));
2901 }
2902 }
2903
2904 /* Helper for linespec_complete_label. Find labels that match
2905 LABEL_NAME in the function symbols listed in the PARSER, and add
2906 them to the tracker. */
2907
2908 static void
2909 complete_label (completion_tracker &tracker,
2910 linespec_parser *parser,
2911 const char *label_name)
2912 {
2913 std::vector<block_symbol> label_function_symbols;
2914 std::vector<block_symbol> *labels
2915 = find_label_symbols (PARSER_STATE (parser),
2916 PARSER_RESULT (parser)->function_symbols,
2917 &label_function_symbols,
2918 label_name, true);
2919
2920 if (labels != nullptr)
2921 {
2922 for (const auto &label : *labels)
2923 {
2924 char *match = xstrdup (label.symbol->search_name ());
2925 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
2926 }
2927 delete labels;
2928 }
2929 }
2930
2931 /* See linespec.h. */
2932
2933 void
2934 linespec_complete_label (completion_tracker &tracker,
2935 const struct language_defn *language,
2936 const char *source_filename,
2937 const char *function_name,
2938 symbol_name_match_type func_name_match_type,
2939 const char *label_name)
2940 {
2941 linespec_parser parser (0, language, NULL, NULL, 0, NULL);
2942
2943 line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
2944
2945 try
2946 {
2947 convert_explicit_location_to_linespec (PARSER_STATE (&parser),
2948 PARSER_RESULT (&parser),
2949 source_filename,
2950 function_name,
2951 func_name_match_type,
2952 NULL, unknown_offset);
2953 }
2954 catch (const gdb_exception_error &ex)
2955 {
2956 return;
2957 }
2958
2959 complete_label (tracker, &parser, label_name);
2960 }
2961
2962 /* See description in linespec.h. */
2963
2964 void
2965 linespec_complete (completion_tracker &tracker, const char *text,
2966 symbol_name_match_type match_type)
2967 {
2968 const char *orig = text;
2969
2970 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
2971 parser.lexer.saved_arg = text;
2972 PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
2973 PARSER_STREAM (&parser) = text;
2974
2975 parser.completion_tracker = &tracker;
2976 PARSER_STATE (&parser)->is_linespec = 1;
2977
2978 /* Parse as much as possible. parser.completion_word will hold
2979 furthest completion point we managed to parse to. */
2980 try
2981 {
2982 parse_linespec (&parser, text, match_type);
2983 }
2984 catch (const gdb_exception_error &except)
2985 {
2986 }
2987
2988 if (parser.completion_quote_char != '\0'
2989 && parser.completion_quote_end != NULL
2990 && parser.completion_quote_end[1] == '\0')
2991 {
2992 /* If completing a quoted string with the cursor right at
2993 terminating quote char, complete the completion word without
2994 interpretation, so that readline advances the cursor one
2995 whitespace past the quote, even if there's no match. This
2996 makes these cases behave the same:
2997
2998 before: "b function()"
2999 after: "b function() "
3000
3001 before: "b 'function()'"
3002 after: "b 'function()' "
3003
3004 and trusts the user in this case:
3005
3006 before: "b 'not_loaded_function_yet()'"
3007 after: "b 'not_loaded_function_yet()' "
3008 */
3009 parser.complete_what = linespec_complete_what::NOTHING;
3010 parser.completion_quote_char = '\0';
3011
3012 gdb::unique_xmalloc_ptr<char> text_copy
3013 (xstrdup (parser.completion_word));
3014 tracker.add_completion (std::move (text_copy));
3015 }
3016
3017 tracker.set_quote_char (parser.completion_quote_char);
3018
3019 if (parser.complete_what == linespec_complete_what::LABEL)
3020 {
3021 parser.complete_what = linespec_complete_what::NOTHING;
3022
3023 const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
3024
3025 std::vector<block_symbol> function_symbols;
3026 std::vector<bound_minimal_symbol> minimal_symbols;
3027 find_linespec_symbols (PARSER_STATE (&parser),
3028 PARSER_RESULT (&parser)->file_symtabs,
3029 func_name, match_type,
3030 &function_symbols, &minimal_symbols);
3031
3032 PARSER_RESULT (&parser)->function_symbols
3033 = new std::vector<block_symbol> (std::move (function_symbols));
3034 PARSER_RESULT (&parser)->minimal_symbols
3035 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
3036
3037 complete_label (tracker, &parser, parser.completion_word);
3038 }
3039 else if (parser.complete_what == linespec_complete_what::FUNCTION)
3040 {
3041 /* While parsing/lexing, we didn't know whether the completion
3042 word completes to a unique function/source name already or
3043 not.
3044
3045 E.g.:
3046 "b function() <tab>"
3047 may need to complete either to:
3048 "b function() const"
3049 or to:
3050 "b function() if/thread/task"
3051
3052 Or, this:
3053 "b foo t"
3054 may need to complete either to:
3055 "b foo template_fun<T>()"
3056 with "foo" being the template function's return type, or to:
3057 "b foo thread/task"
3058
3059 Or, this:
3060 "b file<TAB>"
3061 may need to complete either to a source file name:
3062 "b file.c"
3063 or this, also a filename, but a unique completion:
3064 "b file.c:"
3065 or to a function name:
3066 "b file_function"
3067
3068 Address that by completing assuming source or function, and
3069 seeing if we find a completion that matches exactly the
3070 completion word. If so, then it must be a function (see note
3071 below) and we advance the completion word to the end of input
3072 and switch to KEYWORD completion mode.
3073
3074 Note: if we find a unique completion for a source filename,
3075 then it won't match the completion word, because the LCD will
3076 contain a trailing ':'. And if we're completing at or after
3077 the ':', then complete_linespec_component won't try to
3078 complete on source filenames. */
3079
3080 const char *word = parser.completion_word;
3081
3082 complete_linespec_component (&parser, tracker,
3083 parser.completion_word,
3084 linespec_complete_what::FUNCTION,
3085 PARSER_EXPLICIT (&parser)->source_filename);
3086
3087 parser.complete_what = linespec_complete_what::NOTHING;
3088
3089 if (tracker.quote_char ())
3090 {
3091 /* The function/file name was not close-quoted, so this
3092 can't be a keyword. Note: complete_linespec_component
3093 may have swapped the original quote char for ':' when we
3094 get here, but that still indicates the same. */
3095 }
3096 else if (!tracker.have_completions ())
3097 {
3098 size_t key_start;
3099 size_t wordlen = strlen (parser.completion_word);
3100
3101 key_start
3102 = string_find_incomplete_keyword_at_end (linespec_keywords,
3103 parser.completion_word,
3104 wordlen);
3105
3106 if (key_start != -1
3107 || (wordlen > 0
3108 && parser.completion_word[wordlen - 1] == ' '))
3109 {
3110 parser.completion_word += key_start;
3111 parser.complete_what = linespec_complete_what::KEYWORD;
3112 }
3113 }
3114 else if (tracker.completes_to_completion_word (word))
3115 {
3116 /* Skip the function and complete on keywords. */
3117 parser.completion_word += strlen (word);
3118 parser.complete_what = linespec_complete_what::KEYWORD;
3119 tracker.discard_completions ();
3120 }
3121 }
3122
3123 tracker.advance_custom_word_point_by (parser.completion_word - orig);
3124
3125 complete_linespec_component (&parser, tracker,
3126 parser.completion_word,
3127 parser.complete_what,
3128 PARSER_EXPLICIT (&parser)->source_filename);
3129
3130 /* If we're past the "filename:function:label:offset" linespec, and
3131 didn't find any match, then assume the user might want to create
3132 a pending breakpoint anyway and offer the keyword
3133 completions. */
3134 if (!parser.completion_quote_char
3135 && (parser.complete_what == linespec_complete_what::FUNCTION
3136 || parser.complete_what == linespec_complete_what::LABEL
3137 || parser.complete_what == linespec_complete_what::NOTHING)
3138 && !tracker.have_completions ())
3139 {
3140 const char *end
3141 = parser.completion_word + strlen (parser.completion_word);
3142
3143 if (end > orig && end[-1] == ' ')
3144 {
3145 tracker.advance_custom_word_point_by (end - parser.completion_word);
3146
3147 complete_linespec_component (&parser, tracker, end,
3148 linespec_complete_what::KEYWORD,
3149 NULL);
3150 }
3151 }
3152 }
3153
3154 /* A helper function for decode_line_full and decode_line_1 to
3155 turn LOCATION into std::vector<symtab_and_line>. */
3156
3157 static std::vector<symtab_and_line>
3158 event_location_to_sals (linespec_parser *parser,
3159 const struct event_location *location)
3160 {
3161 std::vector<symtab_and_line> result;
3162
3163 switch (event_location_type (location))
3164 {
3165 case LINESPEC_LOCATION:
3166 {
3167 PARSER_STATE (parser)->is_linespec = 1;
3168 try
3169 {
3170 const linespec_location *ls = get_linespec_location (location);
3171 result = parse_linespec (parser,
3172 ls->spec_string, ls->match_type);
3173 }
3174 catch (const gdb_exception_error &except)
3175 {
3176 throw;
3177 }
3178 }
3179 break;
3180
3181 case ADDRESS_LOCATION:
3182 {
3183 const char *addr_string = get_address_string_location (location);
3184 CORE_ADDR addr = get_address_location (location);
3185
3186 if (addr_string != NULL)
3187 {
3188 addr = linespec_expression_to_pc (&addr_string);
3189 if (PARSER_STATE (parser)->canonical != NULL)
3190 PARSER_STATE (parser)->canonical->location
3191 = copy_event_location (location);
3192 }
3193
3194 result = convert_address_location_to_sals (PARSER_STATE (parser),
3195 addr);
3196 }
3197 break;
3198
3199 case EXPLICIT_LOCATION:
3200 {
3201 const struct explicit_location *explicit_loc;
3202
3203 explicit_loc = get_explicit_location_const (location);
3204 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
3205 PARSER_RESULT (parser),
3206 explicit_loc);
3207 }
3208 break;
3209
3210 case PROBE_LOCATION:
3211 /* Probes are handled by their own decoders. */
3212 gdb_assert_not_reached ("attempt to decode probe location");
3213 break;
3214
3215 default:
3216 gdb_assert_not_reached ("unhandled event location type");
3217 }
3218
3219 return result;
3220 }
3221
3222 /* See linespec.h. */
3223
3224 void
3225 decode_line_full (struct event_location *location, int flags,
3226 struct program_space *search_pspace,
3227 struct symtab *default_symtab,
3228 int default_line, struct linespec_result *canonical,
3229 const char *select_mode,
3230 const char *filter)
3231 {
3232 std::vector<const char *> filters;
3233 struct linespec_state *state;
3234
3235 gdb_assert (canonical != NULL);
3236 /* The filter only makes sense for 'all'. */
3237 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
3238 gdb_assert (select_mode == NULL
3239 || select_mode == multiple_symbols_all
3240 || select_mode == multiple_symbols_ask
3241 || select_mode == multiple_symbols_cancel);
3242 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
3243
3244 linespec_parser parser (flags, current_language,
3245 search_pspace, default_symtab,
3246 default_line, canonical);
3247
3248 scoped_restore_current_program_space restore_pspace;
3249
3250 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3251 location);
3252 state = PARSER_STATE (&parser);
3253
3254 if (result.size () == 0)
3255 throw_error (NOT_SUPPORTED_ERROR, _("Location %s not available"),
3256 event_location_to_string (location));
3257
3258 gdb_assert (result.size () == 1 || canonical->pre_expanded);
3259 canonical->pre_expanded = 1;
3260
3261 /* Arrange for allocated canonical names to be freed. */
3262 std::vector<gdb::unique_xmalloc_ptr<char>> hold_names;
3263 for (int i = 0; i < result.size (); ++i)
3264 {
3265 gdb_assert (state->canonical_names[i].suffix != NULL);
3266 hold_names.emplace_back (state->canonical_names[i].suffix);
3267 }
3268
3269 if (select_mode == NULL)
3270 {
3271 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
3272 select_mode = multiple_symbols_all;
3273 else
3274 select_mode = multiple_symbols_select_mode ();
3275 }
3276
3277 if (select_mode == multiple_symbols_all)
3278 {
3279 if (filter != NULL)
3280 {
3281 filters.push_back (filter);
3282 filter_results (state, &result, filters);
3283 }
3284 else
3285 convert_results_to_lsals (state, &result);
3286 }
3287 else
3288 decode_line_2 (state, &result, select_mode);
3289 }
3290
3291 /* See linespec.h. */
3292
3293 std::vector<symtab_and_line>
3294 decode_line_1 (const struct event_location *location, int flags,
3295 struct program_space *search_pspace,
3296 struct symtab *default_symtab,
3297 int default_line)
3298 {
3299 linespec_parser parser (flags, current_language,
3300 search_pspace, default_symtab,
3301 default_line, NULL);
3302
3303 scoped_restore_current_program_space restore_pspace;
3304
3305 return event_location_to_sals (&parser, location);
3306 }
3307
3308 /* See linespec.h. */
3309
3310 std::vector<symtab_and_line>
3311 decode_line_with_current_source (const char *string, int flags)
3312 {
3313 if (string == 0)
3314 error (_("Empty line specification."));
3315
3316 /* We use whatever is set as the current source line. We do not try
3317 and get a default source symtab+line or it will recursively call us! */
3318 symtab_and_line cursal = get_current_source_symtab_and_line ();
3319
3320 event_location_up location = string_to_event_location (&string,
3321 current_language);
3322 std::vector<symtab_and_line> sals
3323 = decode_line_1 (location.get (), flags, NULL, cursal.symtab, cursal.line);
3324
3325 if (*string)
3326 error (_("Junk at end of line specification: %s"), string);
3327
3328 return sals;
3329 }
3330
3331 /* See linespec.h. */
3332
3333 std::vector<symtab_and_line>
3334 decode_line_with_last_displayed (const char *string, int flags)
3335 {
3336 if (string == 0)
3337 error (_("Empty line specification."));
3338
3339 event_location_up location = string_to_event_location (&string,
3340 current_language);
3341 std::vector<symtab_and_line> sals
3342 = (last_displayed_sal_is_valid ()
3343 ? decode_line_1 (location.get (), flags, NULL,
3344 get_last_displayed_symtab (),
3345 get_last_displayed_line ())
3346 : decode_line_1 (location.get (), flags, NULL, NULL, 0));
3347
3348 if (*string)
3349 error (_("Junk at end of line specification: %s"), string);
3350
3351 return sals;
3352 }
3353
3354 \f
3355
3356 /* First, some functions to initialize stuff at the beginning of the
3357 function. */
3358
3359 static void
3360 initialize_defaults (struct symtab **default_symtab, int *default_line)
3361 {
3362 if (*default_symtab == 0)
3363 {
3364 /* Use whatever we have for the default source line. We don't use
3365 get_current_or_default_symtab_and_line as it can recurse and call
3366 us back! */
3367 struct symtab_and_line cursal =
3368 get_current_source_symtab_and_line ();
3369
3370 *default_symtab = cursal.symtab;
3371 *default_line = cursal.line;
3372 }
3373 }
3374
3375 \f
3376
3377 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3378 advancing EXP_PTR past any parsed text. */
3379
3380 CORE_ADDR
3381 linespec_expression_to_pc (const char **exp_ptr)
3382 {
3383 if (current_program_space->executing_startup)
3384 /* The error message doesn't really matter, because this case
3385 should only hit during breakpoint reset. */
3386 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
3387 "program space is in startup"));
3388
3389 (*exp_ptr)++;
3390 return value_as_address (parse_to_comma_and_eval (exp_ptr));
3391 }
3392
3393 \f
3394
3395 /* Here's where we recognise an Objective-C Selector. An Objective C
3396 selector may be implemented by more than one class, therefore it
3397 may represent more than one method/function. This gives us a
3398 situation somewhat analogous to C++ overloading. If there's more
3399 than one method that could represent the selector, then use some of
3400 the existing C++ code to let the user choose one. */
3401
3402 static std::vector<symtab_and_line>
3403 decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
3404 {
3405 struct collect_info info;
3406 std::vector<const char *> symbol_names;
3407 const char *new_argptr;
3408
3409 info.state = self;
3410 std::vector<symtab *> symtabs;
3411 symtabs.push_back (nullptr);
3412
3413 info.file_symtabs = &symtabs;
3414
3415 std::vector<block_symbol> symbols;
3416 info.result.symbols = &symbols;
3417 std::vector<bound_minimal_symbol> minimal_symbols;
3418 info.result.minimal_symbols = &minimal_symbols;
3419
3420 new_argptr = find_imps (arg, &symbol_names);
3421 if (symbol_names.empty ())
3422 return {};
3423
3424 add_all_symbol_names_from_pspace (&info, NULL, symbol_names,
3425 FUNCTIONS_DOMAIN);
3426
3427 std::vector<symtab_and_line> values;
3428 if (!symbols.empty () || !minimal_symbols.empty ())
3429 {
3430 char *saved_arg;
3431
3432 saved_arg = (char *) alloca (new_argptr - arg + 1);
3433 memcpy (saved_arg, arg, new_argptr - arg);
3434 saved_arg[new_argptr - arg] = '\0';
3435
3436 ls->explicit_loc.function_name = xstrdup (saved_arg);
3437 ls->function_symbols
3438 = new std::vector<block_symbol> (std::move (symbols));
3439 ls->minimal_symbols
3440 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
3441 values = convert_linespec_to_sals (self, ls);
3442
3443 if (self->canonical)
3444 {
3445 std::string holder;
3446 const char *str;
3447
3448 self->canonical->pre_expanded = 1;
3449
3450 if (ls->explicit_loc.source_filename)
3451 {
3452 holder = string_printf ("%s:%s",
3453 ls->explicit_loc.source_filename,
3454 saved_arg);
3455 str = holder.c_str ();
3456 }
3457 else
3458 str = saved_arg;
3459
3460 self->canonical->location
3461 = new_linespec_location (&str, symbol_name_match_type::FULL);
3462 }
3463 }
3464
3465 return values;
3466 }
3467
3468 namespace {
3469
3470 /* A function object that serves as symbol_found_callback_ftype
3471 callback for iterate_over_symbols. This is used by
3472 lookup_prefix_sym to collect type symbols. */
3473 class decode_compound_collector
3474 {
3475 public:
3476 decode_compound_collector ()
3477 : m_unique_syms (htab_create_alloc (1, htab_hash_pointer,
3478 htab_eq_pointer, NULL,
3479 xcalloc, xfree))
3480 {
3481 }
3482
3483 /* Return all symbols collected. */
3484 std::vector<block_symbol> release_symbols ()
3485 {
3486 return std::move (m_symbols);
3487 }
3488
3489 /* Callable as a symbol_found_callback_ftype callback. */
3490 bool operator () (block_symbol *bsym);
3491
3492 private:
3493 /* A hash table of all symbols we found. We use this to avoid
3494 adding any symbol more than once. */
3495 htab_up m_unique_syms;
3496
3497 /* The result vector. */
3498 std::vector<block_symbol> m_symbols;
3499 };
3500
3501 bool
3502 decode_compound_collector::operator () (block_symbol *bsym)
3503 {
3504 void **slot;
3505 struct type *t;
3506 struct symbol *sym = bsym->symbol;
3507
3508 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
3509 return true; /* Continue iterating. */
3510
3511 t = SYMBOL_TYPE (sym);
3512 t = check_typedef (t);
3513 if (t->code () != TYPE_CODE_STRUCT
3514 && t->code () != TYPE_CODE_UNION
3515 && t->code () != TYPE_CODE_NAMESPACE)
3516 return true; /* Continue iterating. */
3517
3518 slot = htab_find_slot (m_unique_syms.get (), sym, INSERT);
3519 if (!*slot)
3520 {
3521 *slot = sym;
3522 m_symbols.push_back (*bsym);
3523 }
3524
3525 return true; /* Continue iterating. */
3526 }
3527
3528 } // namespace
3529
3530 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3531
3532 static std::vector<block_symbol>
3533 lookup_prefix_sym (struct linespec_state *state,
3534 std::vector<symtab *> *file_symtabs,
3535 const char *class_name)
3536 {
3537 decode_compound_collector collector;
3538
3539 lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
3540
3541 for (const auto &elt : *file_symtabs)
3542 {
3543 if (elt == nullptr)
3544 {
3545 iterate_over_all_matching_symtabs (state, lookup_name,
3546 STRUCT_DOMAIN, ALL_DOMAIN,
3547 NULL, false, collector);
3548 iterate_over_all_matching_symtabs (state, lookup_name,
3549 VAR_DOMAIN, ALL_DOMAIN,
3550 NULL, false, collector);
3551 }
3552 else
3553 {
3554 /* Program spaces that are executing startup should have
3555 been filtered out earlier. */
3556 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3557 set_current_program_space (SYMTAB_PSPACE (elt));
3558 iterate_over_file_blocks (elt, lookup_name, STRUCT_DOMAIN, collector);
3559 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN, collector);
3560 }
3561 }
3562
3563 return collector.release_symbols ();
3564 }
3565
3566 /* A std::sort comparison function for symbols. The resulting order does
3567 not actually matter; we just need to be able to sort them so that
3568 symbols with the same program space end up next to each other. */
3569
3570 static bool
3571 compare_symbols (const block_symbol &a, const block_symbol &b)
3572 {
3573 uintptr_t uia, uib;
3574
3575 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (a.symbol));
3576 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (b.symbol));
3577
3578 if (uia < uib)
3579 return true;
3580 if (uia > uib)
3581 return false;
3582
3583 uia = (uintptr_t) a.symbol;
3584 uib = (uintptr_t) b.symbol;
3585
3586 if (uia < uib)
3587 return true;
3588
3589 return false;
3590 }
3591
3592 /* Like compare_symbols but for minimal symbols. */
3593
3594 static bool
3595 compare_msymbols (const bound_minimal_symbol &a, const bound_minimal_symbol &b)
3596 {
3597 uintptr_t uia, uib;
3598
3599 uia = (uintptr_t) a.objfile->pspace;
3600 uib = (uintptr_t) a.objfile->pspace;
3601
3602 if (uia < uib)
3603 return true;
3604 if (uia > uib)
3605 return false;
3606
3607 uia = (uintptr_t) a.minsym;
3608 uib = (uintptr_t) b.minsym;
3609
3610 if (uia < uib)
3611 return true;
3612
3613 return false;
3614 }
3615
3616 /* Look for all the matching instances of each symbol in NAMES. Only
3617 instances from PSPACE are considered; other program spaces are
3618 handled by our caller. If PSPACE is NULL, then all program spaces
3619 are considered. Results are stored into INFO. */
3620
3621 static void
3622 add_all_symbol_names_from_pspace (struct collect_info *info,
3623 struct program_space *pspace,
3624 const std::vector<const char *> &names,
3625 enum search_domain search_domain)
3626 {
3627 for (const char *iter : names)
3628 add_matching_symbols_to_info (iter,
3629 symbol_name_match_type::FULL,
3630 search_domain, info, pspace);
3631 }
3632
3633 static void
3634 find_superclass_methods (std::vector<struct type *> &&superclasses,
3635 const char *name, enum language name_lang,
3636 std::vector<const char *> *result_names)
3637 {
3638 size_t old_len = result_names->size ();
3639
3640 while (1)
3641 {
3642 std::vector<struct type *> new_supers;
3643
3644 for (type *t : superclasses)
3645 find_methods (t, name_lang, name, result_names, &new_supers);
3646
3647 if (result_names->size () != old_len || new_supers.empty ())
3648 break;
3649
3650 superclasses = std::move (new_supers);
3651 }
3652 }
3653
3654 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3655 given by one of the symbols in SYM_CLASSES. Matches are returned
3656 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3657
3658 static void
3659 find_method (struct linespec_state *self, std::vector<symtab *> *file_symtabs,
3660 const char *class_name, const char *method_name,
3661 std::vector<block_symbol> *sym_classes,
3662 std::vector<block_symbol> *symbols,
3663 std::vector<bound_minimal_symbol> *minsyms)
3664 {
3665 size_t last_result_len;
3666 std::vector<struct type *> superclass_vec;
3667 std::vector<const char *> result_names;
3668 struct collect_info info;
3669
3670 /* Sort symbols so that symbols with the same program space are next
3671 to each other. */
3672 std::sort (sym_classes->begin (), sym_classes->end (),
3673 compare_symbols);
3674
3675 info.state = self;
3676 info.file_symtabs = file_symtabs;
3677 info.result.symbols = symbols;
3678 info.result.minimal_symbols = minsyms;
3679
3680 /* Iterate over all the types, looking for the names of existing
3681 methods matching METHOD_NAME. If we cannot find a direct method in a
3682 given program space, then we consider inherited methods; this is
3683 not ideal (ideal would be to respect C++ hiding rules), but it
3684 seems good enough and is what GDB has historically done. We only
3685 need to collect the names because later we find all symbols with
3686 those names. This loop is written in a somewhat funny way
3687 because we collect data across the program space before deciding
3688 what to do. */
3689 last_result_len = 0;
3690 for (const auto &elt : *sym_classes)
3691 {
3692 struct type *t;
3693 struct program_space *pspace;
3694 struct symbol *sym = elt.symbol;
3695 unsigned int ix = &elt - &*sym_classes->begin ();
3696
3697 /* Program spaces that are executing startup should have
3698 been filtered out earlier. */
3699 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
3700 gdb_assert (!pspace->executing_startup);
3701 set_current_program_space (pspace);
3702 t = check_typedef (SYMBOL_TYPE (sym));
3703 find_methods (t, sym->language (),
3704 method_name, &result_names, &superclass_vec);
3705
3706 /* Handle all items from a single program space at once; and be
3707 sure not to miss the last batch. */
3708 if (ix == sym_classes->size () - 1
3709 || (pspace
3710 != SYMTAB_PSPACE (symbol_symtab (sym_classes->at (ix + 1).symbol))))
3711 {
3712 /* If we did not find a direct implementation anywhere in
3713 this program space, consider superclasses. */
3714 if (result_names.size () == last_result_len)
3715 find_superclass_methods (std::move (superclass_vec), method_name,
3716 sym->language (), &result_names);
3717
3718 /* We have a list of candidate symbol names, so now we
3719 iterate over the symbol tables looking for all
3720 matches in this pspace. */
3721 add_all_symbol_names_from_pspace (&info, pspace, result_names,
3722 FUNCTIONS_DOMAIN);
3723
3724 superclass_vec.clear ();
3725 last_result_len = result_names.size ();
3726 }
3727 }
3728
3729 if (!symbols->empty () || !minsyms->empty ())
3730 return;
3731
3732 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3733 and other attempts to locate the symbol will be made. */
3734 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
3735 }
3736
3737 \f
3738
3739 namespace {
3740
3741 /* This function object is a callback for iterate_over_symtabs, used
3742 when collecting all matching symtabs. */
3743
3744 class symtab_collector
3745 {
3746 public:
3747 symtab_collector ()
3748 : m_symtab_table (htab_create (1, htab_hash_pointer, htab_eq_pointer,
3749 NULL))
3750 {
3751 }
3752
3753 /* Callable as a symbol_found_callback_ftype callback. */
3754 bool operator () (symtab *sym);
3755
3756 /* Return an rvalue reference to the collected symtabs. */
3757 std::vector<symtab *> &&release_symtabs ()
3758 {
3759 return std::move (m_symtabs);
3760 }
3761
3762 private:
3763 /* The result vector of symtabs. */
3764 std::vector<symtab *> m_symtabs;
3765
3766 /* This is used to ensure the symtabs are unique. */
3767 htab_up m_symtab_table;
3768 };
3769
3770 bool
3771 symtab_collector::operator () (struct symtab *symtab)
3772 {
3773 void **slot;
3774
3775 slot = htab_find_slot (m_symtab_table.get (), symtab, INSERT);
3776 if (!*slot)
3777 {
3778 *slot = symtab;
3779 m_symtabs.push_back (symtab);
3780 }
3781
3782 return false;
3783 }
3784
3785 } // namespace
3786
3787 /* Given a file name, return a list of all matching symtabs. If
3788 SEARCH_PSPACE is not NULL, the search is restricted to just that
3789 program space. */
3790
3791 static std::vector<symtab *>
3792 collect_symtabs_from_filename (const char *file,
3793 struct program_space *search_pspace)
3794 {
3795 symtab_collector collector;
3796
3797 /* Find that file's data. */
3798 if (search_pspace == NULL)
3799 {
3800 for (struct program_space *pspace : program_spaces)
3801 {
3802 if (pspace->executing_startup)
3803 continue;
3804
3805 set_current_program_space (pspace);
3806 iterate_over_symtabs (file, collector);
3807 }
3808 }
3809 else
3810 {
3811 set_current_program_space (search_pspace);
3812 iterate_over_symtabs (file, collector);
3813 }
3814
3815 return collector.release_symtabs ();
3816 }
3817
3818 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3819 not NULL, the search is restricted to just that program space. */
3820
3821 static std::vector<symtab *>
3822 symtabs_from_filename (const char *filename,
3823 struct program_space *search_pspace)
3824 {
3825 std::vector<symtab *> result
3826 = collect_symtabs_from_filename (filename, search_pspace);
3827
3828 if (result.empty ())
3829 {
3830 if (!have_full_symbols () && !have_partial_symbols ())
3831 throw_error (NOT_FOUND_ERROR,
3832 _("No symbol table is loaded. "
3833 "Use the \"file\" command."));
3834 source_file_not_found_error (filename);
3835 }
3836
3837 return result;
3838 }
3839
3840 /* See symtab.h. */
3841
3842 void
3843 symbol_searcher::find_all_symbols (const std::string &name,
3844 const struct language_defn *language,
3845 enum search_domain search_domain,
3846 std::vector<symtab *> *search_symtabs,
3847 struct program_space *search_pspace)
3848 {
3849 symbol_searcher_collect_info info;
3850 struct linespec_state state;
3851
3852 memset (&state, 0, sizeof (state));
3853 state.language = language;
3854 info.state = &state;
3855
3856 info.result.symbols = &m_symbols;
3857 info.result.minimal_symbols = &m_minimal_symbols;
3858 std::vector<symtab *> all_symtabs;
3859 if (search_symtabs == nullptr)
3860 {
3861 all_symtabs.push_back (nullptr);
3862 search_symtabs = &all_symtabs;
3863 }
3864 info.file_symtabs = search_symtabs;
3865
3866 add_matching_symbols_to_info (name.c_str (), symbol_name_match_type::WILD,
3867 search_domain, &info, search_pspace);
3868 }
3869
3870 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3871 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3872 returned in MINSYMS. */
3873
3874 static void
3875 find_function_symbols (struct linespec_state *state,
3876 std::vector<symtab *> *file_symtabs, const char *name,
3877 symbol_name_match_type name_match_type,
3878 std::vector<block_symbol> *symbols,
3879 std::vector<bound_minimal_symbol> *minsyms)
3880 {
3881 struct collect_info info;
3882 std::vector<const char *> symbol_names;
3883
3884 info.state = state;
3885 info.result.symbols = symbols;
3886 info.result.minimal_symbols = minsyms;
3887 info.file_symtabs = file_symtabs;
3888
3889 /* Try NAME as an Objective-C selector. */
3890 find_imps (name, &symbol_names);
3891 if (!symbol_names.empty ())
3892 add_all_symbol_names_from_pspace (&info, state->search_pspace,
3893 symbol_names, FUNCTIONS_DOMAIN);
3894 else
3895 add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
3896 &info, state->search_pspace);
3897 }
3898
3899 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3900 in SYMBOLS and minimal symbols in MINSYMS. */
3901
3902 static void
3903 find_linespec_symbols (struct linespec_state *state,
3904 std::vector<symtab *> *file_symtabs,
3905 const char *lookup_name,
3906 symbol_name_match_type name_match_type,
3907 std::vector <block_symbol> *symbols,
3908 std::vector<bound_minimal_symbol> *minsyms)
3909 {
3910 gdb::unique_xmalloc_ptr<char> canon
3911 = cp_canonicalize_string_no_typedefs (lookup_name);
3912 if (canon != nullptr)
3913 lookup_name = canon.get ();
3914
3915 /* It's important to not call expand_symtabs_matching unnecessarily
3916 as it can really slow things down (by unnecessarily expanding
3917 potentially 1000s of symtabs, which when debugging some apps can
3918 cost 100s of seconds). Avoid this to some extent by *first* calling
3919 find_function_symbols, and only if that doesn't find anything
3920 *then* call find_method. This handles two important cases:
3921 1) break (anonymous namespace)::foo
3922 2) break class::method where method is in class (and not a baseclass) */
3923
3924 find_function_symbols (state, file_symtabs, lookup_name,
3925 name_match_type, symbols, minsyms);
3926
3927 /* If we were unable to locate a symbol of the same name, try dividing
3928 the name into class and method names and searching the class and its
3929 baseclasses. */
3930 if (symbols->empty () && minsyms->empty ())
3931 {
3932 std::string klass, method;
3933 const char *last, *p, *scope_op;
3934
3935 /* See if we can find a scope operator and break this symbol
3936 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3937 scope_op = "::";
3938 p = find_toplevel_string (lookup_name, scope_op);
3939
3940 last = NULL;
3941 while (p != NULL)
3942 {
3943 last = p;
3944 p = find_toplevel_string (p + strlen (scope_op), scope_op);
3945 }
3946
3947 /* If no scope operator was found, there is nothing more we can do;
3948 we already attempted to lookup the entire name as a symbol
3949 and failed. */
3950 if (last == NULL)
3951 return;
3952
3953 /* LOOKUP_NAME points to the class name.
3954 LAST points to the method name. */
3955 klass = std::string (lookup_name, last - lookup_name);
3956
3957 /* Skip past the scope operator. */
3958 last += strlen (scope_op);
3959 method = last;
3960
3961 /* Find a list of classes named KLASS. */
3962 std::vector<block_symbol> classes
3963 = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
3964 if (!classes.empty ())
3965 {
3966 /* Now locate a list of suitable methods named METHOD. */
3967 try
3968 {
3969 find_method (state, file_symtabs,
3970 klass.c_str (), method.c_str (),
3971 &classes, symbols, minsyms);
3972 }
3973
3974 /* If successful, we're done. If NOT_FOUND_ERROR
3975 was not thrown, rethrow the exception that we did get. */
3976 catch (const gdb_exception_error &except)
3977 {
3978 if (except.error != NOT_FOUND_ERROR)
3979 throw;
3980 }
3981 }
3982 }
3983 }
3984
3985 /* Helper for find_label_symbols. Find all labels that match name
3986 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3987 Return the actual function symbol in which the label was found in
3988 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3989 interpreted as a label name prefix. Otherwise, only a label named
3990 exactly NAME match. */
3991
3992 static void
3993 find_label_symbols_in_block (const struct block *block,
3994 const char *name, struct symbol *fn_sym,
3995 bool completion_mode,
3996 std::vector<block_symbol> *result,
3997 std::vector<block_symbol> *label_funcs_ret)
3998 {
3999 if (completion_mode)
4000 {
4001 struct block_iterator iter;
4002 struct symbol *sym;
4003 size_t name_len = strlen (name);
4004
4005 int (*cmp) (const char *, const char *, size_t);
4006 cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
4007
4008 ALL_BLOCK_SYMBOLS (block, iter, sym)
4009 {
4010 if (symbol_matches_domain (sym->language (),
4011 SYMBOL_DOMAIN (sym), LABEL_DOMAIN)
4012 && cmp (sym->search_name (), name, name_len) == 0)
4013 {
4014 result->push_back ({sym, block});
4015 label_funcs_ret->push_back ({fn_sym, block});
4016 }
4017 }
4018 }
4019 else
4020 {
4021 struct block_symbol label_sym
4022 = lookup_symbol (name, block, LABEL_DOMAIN, 0);
4023
4024 if (label_sym.symbol != NULL)
4025 {
4026 result->push_back (label_sym);
4027 label_funcs_ret->push_back ({fn_sym, block});
4028 }
4029 }
4030 }
4031
4032 /* Return all labels that match name NAME in FUNCTION_SYMBOLS or NULL
4033 if no matches were found.
4034
4035 Return the actual function symbol in which the label was found in
4036 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4037 interpreted as a label name prefix. Otherwise, only labels named
4038 exactly NAME match. */
4039
4040
4041 static std::vector<block_symbol> *
4042 find_label_symbols (struct linespec_state *self,
4043 std::vector<block_symbol> *function_symbols,
4044 std::vector<block_symbol> *label_funcs_ret,
4045 const char *name,
4046 bool completion_mode)
4047 {
4048 const struct block *block;
4049 struct symbol *fn_sym;
4050 std::vector<block_symbol> result;
4051
4052 if (function_symbols == NULL)
4053 {
4054 set_current_program_space (self->program_space);
4055 block = get_current_search_block ();
4056
4057 for (;
4058 block && !BLOCK_FUNCTION (block);
4059 block = BLOCK_SUPERBLOCK (block))
4060 ;
4061 if (!block)
4062 return NULL;
4063 fn_sym = BLOCK_FUNCTION (block);
4064
4065 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4066 &result, label_funcs_ret);
4067 }
4068 else
4069 {
4070 for (const auto &elt : *function_symbols)
4071 {
4072 fn_sym = elt.symbol;
4073 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
4074 block = SYMBOL_BLOCK_VALUE (fn_sym);
4075
4076 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4077 &result, label_funcs_ret);
4078 }
4079 }
4080
4081 if (!result.empty ())
4082 return new std::vector<block_symbol> (std::move (result));
4083 return nullptr;
4084 }
4085
4086 \f
4087
4088 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4089
4090 static std::vector<symtab_and_line>
4091 decode_digits_list_mode (struct linespec_state *self,
4092 linespec_p ls,
4093 struct symtab_and_line val)
4094 {
4095 gdb_assert (self->list_mode);
4096
4097 std::vector<symtab_and_line> values;
4098
4099 for (const auto &elt : *ls->file_symtabs)
4100 {
4101 /* The logic above should ensure this. */
4102 gdb_assert (elt != NULL);
4103
4104 set_current_program_space (SYMTAB_PSPACE (elt));
4105
4106 /* Simplistic search just for the list command. */
4107 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
4108 if (val.symtab == NULL)
4109 val.symtab = elt;
4110 val.pspace = SYMTAB_PSPACE (elt);
4111 val.pc = 0;
4112 val.explicit_line = true;
4113
4114 add_sal_to_sals (self, &values, &val, NULL, 0);
4115 }
4116
4117 return values;
4118 }
4119
4120 /* A helper for create_sals_line_offset that iterates over the symtabs
4121 associated with LS and returns a vector of corresponding symtab_and_line
4122 structures. */
4123
4124 static std::vector<symtab_and_line>
4125 decode_digits_ordinary (struct linespec_state *self,
4126 linespec_p ls,
4127 int line,
4128 struct linetable_entry **best_entry)
4129 {
4130 std::vector<symtab_and_line> sals;
4131 for (const auto &elt : *ls->file_symtabs)
4132 {
4133 std::vector<CORE_ADDR> pcs;
4134
4135 /* The logic above should ensure this. */
4136 gdb_assert (elt != NULL);
4137
4138 set_current_program_space (SYMTAB_PSPACE (elt));
4139
4140 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
4141 for (CORE_ADDR pc : pcs)
4142 {
4143 symtab_and_line sal;
4144 sal.pspace = SYMTAB_PSPACE (elt);
4145 sal.symtab = elt;
4146 sal.line = line;
4147 sal.explicit_line = true;
4148 sal.pc = pc;
4149 sals.push_back (std::move (sal));
4150 }
4151 }
4152
4153 return sals;
4154 }
4155
4156 \f
4157
4158 /* Return the line offset represented by VARIABLE. */
4159
4160 static struct line_offset
4161 linespec_parse_variable (struct linespec_state *self, const char *variable)
4162 {
4163 int index = 0;
4164 const char *p;
4165 struct line_offset offset = {0, LINE_OFFSET_NONE};
4166
4167 p = (variable[1] == '$') ? variable + 2 : variable + 1;
4168 if (*p == '$')
4169 ++p;
4170 while (*p >= '0' && *p <= '9')
4171 ++p;
4172 if (!*p) /* Reached end of token without hitting non-digit. */
4173 {
4174 /* We have a value history reference. */
4175 struct value *val_history;
4176
4177 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
4178 val_history
4179 = access_value_history ((variable[1] == '$') ? -index : index);
4180 if (value_type (val_history)->code () != TYPE_CODE_INT)
4181 error (_("History values used in line "
4182 "specs must have integer values."));
4183 offset.offset = value_as_long (val_history);
4184 }
4185 else
4186 {
4187 /* Not all digits -- may be user variable/function or a
4188 convenience variable. */
4189 LONGEST valx;
4190 struct internalvar *ivar;
4191
4192 /* Try it as a convenience variable. If it is not a convenience
4193 variable, return and allow normal symbol lookup to occur. */
4194 ivar = lookup_only_internalvar (variable + 1);
4195 if (ivar == NULL)
4196 /* No internal variable with that name. Mark the offset
4197 as unknown to allow the name to be looked up as a symbol. */
4198 offset.sign = LINE_OFFSET_UNKNOWN;
4199 else
4200 {
4201 /* We found a valid variable name. If it is not an integer,
4202 throw an error. */
4203 if (!get_internalvar_integer (ivar, &valx))
4204 error (_("Convenience variables used in line "
4205 "specs must have integer values."));
4206 else
4207 offset.offset = valx;
4208 }
4209 }
4210
4211 return offset;
4212 }
4213 \f
4214
4215 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
4216 linespec; return the SAL in RESULT. This function should return SALs
4217 matching those from find_function_start_sal, otherwise false
4218 multiple-locations breakpoints could be placed. */
4219
4220 static void
4221 minsym_found (struct linespec_state *self, struct objfile *objfile,
4222 struct minimal_symbol *msymbol,
4223 std::vector<symtab_and_line> *result)
4224 {
4225 bool want_start_sal;
4226
4227 CORE_ADDR func_addr;
4228 bool is_function = msymbol_is_function (objfile, msymbol, &func_addr);
4229
4230 if (is_function)
4231 {
4232 const char *msym_name = msymbol->linkage_name ();
4233
4234 if (MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
4235 || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
4236 want_start_sal = gnu_ifunc_resolve_name (msym_name, &func_addr);
4237 else
4238 want_start_sal = true;
4239 }
4240
4241 symtab_and_line sal;
4242
4243 if (is_function && want_start_sal)
4244 sal = find_function_start_sal (func_addr, NULL, self->funfirstline);
4245 else
4246 {
4247 sal.objfile = objfile;
4248 sal.msymbol = msymbol;
4249 /* Store func_addr, not the minsym's address in case this was an
4250 ifunc that hasn't been resolved yet. */
4251 if (is_function)
4252 sal.pc = func_addr;
4253 else
4254 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
4255 sal.pspace = current_program_space;
4256 }
4257
4258 sal.section = msymbol->obj_section (objfile);
4259
4260 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
4261 add_sal_to_sals (self, result, &sal, msymbol->natural_name (), 0);
4262 }
4263
4264 /* Helper for search_minsyms_for_name that adds the symbol to the
4265 result. */
4266
4267 static void
4268 add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
4269 struct symtab *symtab, int list_mode,
4270 std::vector<struct bound_minimal_symbol> *msyms)
4271 {
4272 if (symtab != NULL)
4273 {
4274 /* We're looking for a label for which we don't have debug
4275 info. */
4276 CORE_ADDR func_addr;
4277 if (msymbol_is_function (objfile, minsym, &func_addr))
4278 {
4279 symtab_and_line sal = find_pc_sect_line (func_addr, NULL, 0);
4280
4281 if (symtab != sal.symtab)
4282 return;
4283 }
4284 }
4285
4286 /* Exclude data symbols when looking for breakpoint locations. */
4287 if (!list_mode && !msymbol_is_function (objfile, minsym))
4288 return;
4289
4290 struct bound_minimal_symbol mo = {minsym, objfile};
4291 msyms->push_back (mo);
4292 return;
4293 }
4294
4295 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4296 is not NULL, the search is restricted to just that program
4297 space.
4298
4299 If SYMTAB is NULL, search all objfiles, otherwise
4300 restrict results to the given SYMTAB. */
4301
4302 static void
4303 search_minsyms_for_name (struct collect_info *info,
4304 const lookup_name_info &name,
4305 struct program_space *search_pspace,
4306 struct symtab *symtab)
4307 {
4308 std::vector<struct bound_minimal_symbol> minsyms;
4309
4310 if (symtab == NULL)
4311 {
4312 for (struct program_space *pspace : program_spaces)
4313 {
4314 if (search_pspace != NULL && search_pspace != pspace)
4315 continue;
4316 if (pspace->executing_startup)
4317 continue;
4318
4319 set_current_program_space (pspace);
4320
4321 for (objfile *objfile : current_program_space->objfiles ())
4322 {
4323 iterate_over_minimal_symbols (objfile, name,
4324 [&] (struct minimal_symbol *msym)
4325 {
4326 add_minsym (msym, objfile, nullptr,
4327 info->state->list_mode,
4328 &minsyms);
4329 return false;
4330 });
4331 }
4332 }
4333 }
4334 else
4335 {
4336 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
4337 {
4338 set_current_program_space (SYMTAB_PSPACE (symtab));
4339 iterate_over_minimal_symbols
4340 (SYMTAB_OBJFILE (symtab), name,
4341 [&] (struct minimal_symbol *msym)
4342 {
4343 add_minsym (msym, SYMTAB_OBJFILE (symtab), symtab,
4344 info->state->list_mode, &minsyms);
4345 return false;
4346 });
4347 }
4348 }
4349
4350 /* Return true if TYPE is a static symbol. */
4351 auto msymbol_type_is_static = [] (enum minimal_symbol_type type)
4352 {
4353 switch (type)
4354 {
4355 case mst_file_text:
4356 case mst_file_data:
4357 case mst_file_bss:
4358 return true;
4359 default:
4360 return false;
4361 }
4362 };
4363
4364 /* Add minsyms to the result set, but filter out trampoline symbols
4365 if we also found extern symbols with the same name. I.e., don't
4366 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4367 'foo' is the symbol that the plt resolves to. */
4368 for (const bound_minimal_symbol &item : minsyms)
4369 {
4370 bool skip = false;
4371 if (MSYMBOL_TYPE (item.minsym) == mst_solib_trampoline)
4372 {
4373 for (const bound_minimal_symbol &item2 : minsyms)
4374 {
4375 if (&item2 == &item)
4376 continue;
4377
4378 /* Trampoline symbols can only jump to exported
4379 symbols. */
4380 if (msymbol_type_is_static (MSYMBOL_TYPE (item2.minsym)))
4381 continue;
4382
4383 if (strcmp (item.minsym->linkage_name (),
4384 item2.minsym->linkage_name ()) != 0)
4385 continue;
4386
4387 /* Found a global minsym with the same name as the
4388 trampoline. Don't create a location for this
4389 trampoline. */
4390 skip = true;
4391 break;
4392 }
4393 }
4394
4395 if (!skip)
4396 info->result.minimal_symbols->push_back (item);
4397 }
4398 }
4399
4400 /* A helper function to add all symbols matching NAME to INFO. If
4401 PSPACE is not NULL, the search is restricted to just that program
4402 space. */
4403
4404 static void
4405 add_matching_symbols_to_info (const char *name,
4406 symbol_name_match_type name_match_type,
4407 enum search_domain search_domain,
4408 struct collect_info *info,
4409 struct program_space *pspace)
4410 {
4411 lookup_name_info lookup_name (name, name_match_type);
4412
4413 for (const auto &elt : *info->file_symtabs)
4414 {
4415 if (elt == nullptr)
4416 {
4417 iterate_over_all_matching_symtabs (info->state, lookup_name,
4418 VAR_DOMAIN, search_domain,
4419 pspace, true,
4420 [&] (block_symbol *bsym)
4421 { return info->add_symbol (bsym); });
4422 search_minsyms_for_name (info, lookup_name, pspace, NULL);
4423 }
4424 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
4425 {
4426 int prev_len = info->result.symbols->size ();
4427
4428 /* Program spaces that are executing startup should have
4429 been filtered out earlier. */
4430 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
4431 set_current_program_space (SYMTAB_PSPACE (elt));
4432 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN,
4433 [&] (block_symbol *bsym)
4434 { return info->add_symbol (bsym); });
4435
4436 /* If no new symbols were found in this iteration and this symtab
4437 is in assembler, we might actually be looking for a label for
4438 which we don't have debug info. Check for a minimal symbol in
4439 this case. */
4440 if (prev_len == info->result.symbols->size ()
4441 && elt->language == language_asm)
4442 search_minsyms_for_name (info, lookup_name, pspace, elt);
4443 }
4444 }
4445 }
4446
4447 \f
4448
4449 /* Now come some functions that are called from multiple places within
4450 decode_line_1. */
4451
4452 static int
4453 symbol_to_sal (struct symtab_and_line *result,
4454 int funfirstline, struct symbol *sym)
4455 {
4456 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
4457 {
4458 *result = find_function_start_sal (sym, funfirstline);
4459 return 1;
4460 }
4461 else
4462 {
4463 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
4464 {
4465 *result = {};
4466 result->symtab = symbol_symtab (sym);
4467 result->symbol = sym;
4468 result->line = SYMBOL_LINE (sym);
4469 result->pc = SYMBOL_VALUE_ADDRESS (sym);
4470 result->pspace = SYMTAB_PSPACE (result->symtab);
4471 result->explicit_pc = 1;
4472 return 1;
4473 }
4474 else if (funfirstline)
4475 {
4476 /* Nothing. */
4477 }
4478 else if (SYMBOL_LINE (sym) != 0)
4479 {
4480 /* We know its line number. */
4481 *result = {};
4482 result->symtab = symbol_symtab (sym);
4483 result->symbol = sym;
4484 result->line = SYMBOL_LINE (sym);
4485 result->pc = SYMBOL_VALUE_ADDRESS (sym);
4486 result->pspace = SYMTAB_PSPACE (result->symtab);
4487 return 1;
4488 }
4489 }
4490
4491 return 0;
4492 }
4493
4494 linespec_result::~linespec_result ()
4495 {
4496 for (linespec_sals &lsal : lsals)
4497 xfree (lsal.canonical);
4498 }
4499
4500 /* Return the quote characters permitted by the linespec parser. */
4501
4502 const char *
4503 get_gdb_linespec_parser_quote_characters (void)
4504 {
4505 return linespec_quote_characters;
4506 }