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