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