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