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