1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
29 #include "completer.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
34 #include "objc-lang.h"
38 #include "mi/mi-cmds.h"
40 #include "arch-utils.h"
42 #include "cli/cli-utils.h"
43 #include "filenames.h"
47 #include "common/function-view.h"
48 #include "common/def-vector.h"
50 /* An enumeration of the various things a user might attempt to
51 complete for a linespec location. */
53 enum class linespec_complete_what
55 /* Nothing, no possible completion. */
58 /* A function/method name. Due to ambiguity between
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". */
68 /* A label symbol. E.g., break file.c:function:LABEL. */
71 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
74 /* A linespec keyword ("if"/"thread"/"task").
75 E.g., "break func threa<tab>". */
79 typedef struct symbol *symbolp;
82 typedef struct type *typep;
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. */
91 struct program_space *pspace;
95 typedef struct bound_minimal_symbol bound_minimal_symbol_d;
97 DEF_VEC_O (bound_minimal_symbol_d);
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. */
105 /* An explicit location describing the SaLs. */
106 struct explicit_location explicit_loc;
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;
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;
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. */
124 VEC (symbolp) *label_symbols;
125 VEC (symbolp) *function_symbols;
128 typedef struct linespec *linespec_p;
130 /* A canonical linespec represented as a symtab-related string.
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. */
136 struct linespec_canonical_name
138 /* Remaining text part of the linespec string. */
141 /* If NULL then SUFFIX is the whole linespec string. */
142 struct symtab *symtab;
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. */
149 struct linespec_state
151 /* The language in use during linespec processing. */
152 const struct language_defn *language;
154 /* The program space as seen when the module was entered. */
155 struct program_space *program_space;
157 /* If not NULL, the search is restricted to just this program
159 struct program_space *search_pspace;
161 /* The default symtab to use, if no other symtab is specified. */
162 struct symtab *default_symtab;
164 /* The default line to use. */
167 /* The 'funfirstline' value that was passed in to decode_line_1 or
171 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
174 /* The 'canonical' value passed to decode_line_full, or NULL. */
175 struct linespec_result *canonical;
177 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
178 struct linespec_canonical_name *canonical_names;
180 /* This is a set of address_entry objects which is used to prevent
181 duplicate symbols from being entered into the result. */
184 /* Are we building a linespec? */
188 /* This is a helper object that is used when collecting symbols into a
193 /* The linespec object in use. */
194 struct linespec_state *state;
196 /* A list of symtabs to which to restrict matches. */
197 VEC (symtab_ptr) *file_symtabs;
199 /* The result being accumulated. */
202 VEC (symbolp) *symbols;
203 VEC (bound_minimal_symbol_d) *minimal_symbols;
206 /* Possibly add a symbol to the results. */
207 bool add_symbol (symbol *sym);
211 collect_info::add_symbol (symbol *sym)
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);
218 /* Continue iterating. */
229 /* A colon "separator" */
241 /* EOI (end of input) */
247 typedef enum ls_token_type linespec_token_type;
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
254 /* A token of the linespec lexer */
258 /* The type of the token */
259 linespec_token_type type;
261 /* Data for the token */
264 /* A string, given as a stoken */
265 struct stoken string;
271 typedef struct ls_token linespec_token;
273 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
274 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
276 /* An instance of the linespec parser. */
280 /* Lexer internal data */
283 /* Save head of input stream. */
284 const char *saved_arg;
286 /* Head of the input stream. */
288 #define PARSER_STREAM(P) ((P)->lexer.stream)
290 /* The current token. */
291 linespec_token current;
294 /* Is the entire linespec quote-enclosed? */
295 int is_quote_enclosed;
297 /* The state of the parse. */
298 struct linespec_state state;
299 #define PARSER_STATE(PPTR) (&(PPTR)->state)
301 /* The result of the parse. */
302 struct linespec result;
303 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
305 /* What the parser believes the current word point should complete
307 linespec_complete_what complete_what;
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
314 const char *completion_word;
316 /* If the current token was a quoted string, then this is the
317 quoting character (either " or '). */
318 int completion_quote_char;
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;
324 /* If parsing for completion, then this points at the completion
325 tracker. Otherwise, this is NULL. */
326 struct completion_tracker *completion_tracker;
328 typedef struct ls_parser linespec_parser;
330 /* A convenience macro for accessing the explicit location result of
332 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
334 /* Prototypes for local functions. */
336 static void iterate_over_file_blocks
337 (struct symtab *symtab, const lookup_name_info &name,
339 gdb::function_view<symbol_found_callback_ftype> callback);
341 static void initialize_defaults (struct symtab **default_symtab,
344 CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
346 static std::vector<symtab_and_line> decode_objc (struct linespec_state *self,
350 static VEC (symtab_ptr) *symtabs_from_filename (const char *,
351 struct program_space *pspace);
353 static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
354 VEC (symbolp) *function_symbols,
355 VEC (symbolp) **label_funcs_ret,
357 bool completion_mode = false);
359 static void find_linespec_symbols (struct linespec_state *self,
360 VEC (symtab_ptr) *file_symtabs,
362 VEC (symbolp) **symbols,
363 VEC (bound_minimal_symbol_d) **minsyms);
365 static struct line_offset
366 linespec_parse_variable (struct linespec_state *self,
367 const char *variable);
369 static int symbol_to_sal (struct symtab_and_line *result,
370 int funfirstline, struct symbol *sym);
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);
377 static void add_all_symbol_names_from_pspace (struct collect_info *info,
378 struct program_space *pspace,
379 VEC (const_char_ptr) *names);
381 static VEC (symtab_ptr) *
382 collect_symtabs_from_filename (const char *file,
383 struct program_space *pspace);
385 static std::vector<symtab_and_line> decode_digits_ordinary
386 (struct linespec_state *self,
389 linetable_entry **best_entry);
391 static std::vector<symtab_and_line> decode_digits_list_mode
392 (struct linespec_state *self,
394 struct symtab_and_line val);
396 static void minsym_found (struct linespec_state *self, struct objfile *objfile,
397 struct minimal_symbol *msymbol,
398 std::vector<symtab_and_line> *result);
400 static int compare_symbols (const void *a, const void *b);
402 static int compare_msymbols (const void *a, const void *b);
404 /* Permitted quote characters for the parser. This is different from the
405 completer's quote characters to allow backward compatibility with the
407 static const char *const linespec_quote_characters = "\"\'";
409 /* Lexer functions. */
411 /* Lex a number from the input in PARSER. This only supports
414 Return true if input is decimal numbers. Return false if not. */
417 linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
419 tokenp->type = LSTOKEN_NUMBER;
420 LS_TOKEN_STOKEN (*tokenp).length = 0;
421 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
423 /* Keep any sign at the start of the stream. */
424 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
426 ++LS_TOKEN_STOKEN (*tokenp).length;
427 ++(PARSER_STREAM (parser));
430 while (isdigit (*PARSER_STREAM (parser)))
432 ++LS_TOKEN_STOKEN (*tokenp).length;
433 ++(PARSER_STREAM (parser));
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)))
443 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
450 /* See linespec.h. */
453 linespec_lexer_lex_keyword (const char *p)
459 for (i = 0; linespec_keywords[i] != NULL; ++i)
461 int len = strlen (linespec_keywords[i]);
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
467 if (strncmp (p, linespec_keywords[i], len) == 0
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
476 if (i != IF_KEYWORD_INDEX)
480 for (j = 0; linespec_keywords[j] != NULL; ++j)
482 int nextlen = strlen (linespec_keywords[j]);
484 if (strncmp (p, linespec_keywords[j], nextlen) == 0
485 && isspace (p[nextlen]))
490 return linespec_keywords[i];
498 /* See description in linespec.h. */
501 is_ada_operator (const char *string)
503 const struct ada_opname_map *mapping;
505 for (mapping = ada_opname_table;
506 mapping->encoded != NULL
507 && !startswith (string, mapping->decoded); ++mapping)
510 return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
513 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
514 the location of QUOTE_CHAR, or NULL if not found. */
517 skip_quote_char (const char *string, char quote_char)
519 const char *p, *last;
521 p = last = find_toplevel_char (string, quote_char);
522 while (p && *p != '\0' && *p != ':')
524 p = find_toplevel_char (p, quote_char);
532 /* Make a writable copy of the string given in TOKEN, trimming
533 any trailing whitespace. */
536 copy_token_string (linespec_token token)
540 if (token.type == LSTOKEN_KEYWORD)
541 return xstrdup (LS_TOKEN_KEYWORD (token));
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);
551 /* Does P represent the end of a quote-enclosed linespec? */
554 is_closing_quote_enclosed (const char *p)
556 if (strchr (linespec_quote_characters, *p))
558 p = skip_spaces ((char *) p);
559 return (*p == '\0' || linespec_lexer_lex_keyword (p));
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. */
567 find_parameter_list_end (const char *input)
569 char end_char, start_char;
574 if (start_char == '(')
576 else if (start_char == '<')
585 if (*p == start_char)
587 else if (*p == end_char)
601 /* If the [STRING, STRING_LEN) string ends with what looks like a
602 keyword, return the keyword start offset in STRING. Return -1
606 string_find_incomplete_keyword_at_end (const char * const *keywords,
607 const char *string, size_t string_len)
609 const char *end = string + string_len;
612 while (p > string && *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)
626 /* Lex a string from the input in PARSER. */
628 static linespec_token
629 linespec_lexer_lex_string (linespec_parser *parser)
631 linespec_token token;
632 const char *start = PARSER_STREAM (parser);
634 token.type = LSTOKEN_STRING;
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)))
641 char quote_char = *PARSER_STREAM (parser);
643 /* Special case: Ada operators. */
644 if (PARSER_STATE (parser)->language->la_language == language_ada
645 && quote_char == '\"')
647 int len = is_ada_operator (PARSER_STREAM (parser));
651 /* The input is an Ada operator. Return the quoted string
653 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
654 LS_TOKEN_STOKEN (token).length = len;
655 PARSER_STREAM (parser) += len;
659 /* The input does not represent an Ada operator -- fall through
660 to normal quoted string handling. */
663 /* Skip past the beginning quote. */
664 ++(PARSER_STREAM (parser));
666 /* Mark the start of the string. */
667 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
669 /* Skip to the ending quote. */
670 end = skip_quote_char (PARSER_STREAM (parser), quote_char);
672 /* This helps the completer mode decide whether we have a
674 parser->completion_quote_char = quote_char;
675 parser->completion_quote_end = end;
677 /* Error if the input did not terminate properly, unless in
681 if (parser->completion_tracker == NULL)
682 error (_("unmatched quote"));
684 /* In completion mode, we'll try to complete the incomplete
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;
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;
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.
707 This allows users to forgo quoting CV-qualifiers, template arguments,
708 and similar common language constructs. */
712 if (isspace (*PARSER_STREAM (parser)))
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)
721 LS_TOKEN_STOKEN (token).ptr = start;
722 LS_TOKEN_STOKEN (token).length
723 = PARSER_STREAM (parser) - start;
727 /* Advance past the whitespace. */
728 PARSER_STREAM (parser) = p;
731 /* If the next character is EOI or (single) ':', the
732 string is complete; return the token. */
733 if (*PARSER_STREAM (parser) == 0)
735 LS_TOKEN_STOKEN (token).ptr = start;
736 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
739 else if (PARSER_STREAM (parser)[0] == ':')
741 /* Do not tokenize the C++ scope operator. */
742 if (PARSER_STREAM (parser)[1] == ':')
743 ++(PARSER_STREAM (parser));
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]))
752 LS_TOKEN_STOKEN (token).ptr = start;
753 LS_TOKEN_STOKEN (token).length
754 = PARSER_STREAM (parser) - start;
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)))
764 LS_TOKEN_STOKEN (token).ptr = start;
765 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
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) == '(')
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
779 && (PARSER_STREAM (parser) - start) >= CP_OPERATOR_LEN)
781 const char *p = PARSER_STREAM (parser);
783 while (p > start && isspace (p[-1]))
785 if (p - start >= CP_OPERATOR_LEN)
787 p -= CP_OPERATOR_LEN;
788 if (strncmp (p, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
790 || !(isalnum (p[-1]) || p[-1] == '_')))
792 /* This is an operator name. Keep going. */
793 ++(PARSER_STREAM (parser));
794 if (*PARSER_STREAM (parser) == '<')
795 ++(PARSER_STREAM (parser));
801 const char *p = find_parameter_list_end (PARSER_STREAM (parser));
802 PARSER_STREAM (parser) = p;
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. */
811 LS_TOKEN_STOKEN (token).ptr = start;
812 LS_TOKEN_STOKEN (token).length
813 = PARSER_STREAM (parser) - start;
819 /* Commas are terminators, but not if they are part of an
821 else if (*PARSER_STREAM (parser) == ',')
823 if ((PARSER_STATE (parser)->language->la_language
825 && (PARSER_STREAM (parser) - start) > CP_OPERATOR_LEN)
827 const char *p = strstr (start, CP_OPERATOR_STR);
829 if (p != NULL && is_operator_name (p))
831 /* This is an operator name. Keep going. */
832 ++(PARSER_STREAM (parser));
837 /* Comma terminates the string. */
838 LS_TOKEN_STOKEN (token).ptr = start;
839 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
843 /* Advance the stream. */
844 ++(PARSER_STREAM (parser));
851 /* Lex a single linespec token from PARSER. */
853 static linespec_token
854 linespec_lexer_lex_one (linespec_parser *parser)
858 if (parser->lexer.current.type == LSTOKEN_CONSUMED)
860 /* Skip any whitespace. */
861 PARSER_STREAM (parser) = skip_spaces (PARSER_STREAM (parser));
863 /* Check for a keyword, they end the linespec. */
864 keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
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.
872 PARSER_STREAM (parser) += strlen (keyword); */
874 return parser->lexer.current;
877 /* Handle other tokens. */
878 switch (*PARSER_STREAM (parser))
881 parser->lexer.current.type = LSTOKEN_EOI;
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);
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);
898 parser->lexer.current.type = LSTOKEN_COLON;
899 ++(PARSER_STREAM (parser));
903 case '\'': case '\"':
904 /* Special case: permit quote-enclosed linespecs. */
905 if (parser->is_quote_enclosed
906 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
908 ++(PARSER_STREAM (parser));
909 parser->lexer.current.type = LSTOKEN_EOI;
912 parser->lexer.current = linespec_lexer_lex_string (parser);
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));
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);
931 return parser->lexer.current;
934 /* Consume the current token and return the next token in PARSER's
935 input stream. Also advance the completion word for completion
938 static linespec_token
939 linespec_lexer_consume_token (linespec_parser *parser)
941 gdb_assert (parser->lexer.current.type != LSTOKEN_EOI);
943 bool advance_word = (parser->lexer.current.type != LSTOKEN_STRING
944 || *PARSER_STREAM (parser) != '\0');
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)
950 gdb_assert (parser->lexer.current.type == LSTOKEN_STRING);
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')
956 parser->completion_quote_char = '\0';
957 parser->completion_quote_end = NULL;;
961 parser->lexer.current.type = LSTOKEN_CONSUMED;
962 linespec_lexer_lex_one (parser);
964 if (parser->lexer.current.type == LSTOKEN_STRING)
966 /* Advance the completion word past a potential initial
968 parser->completion_word = LS_TOKEN_STOKEN (parser->lexer.current).ptr;
970 else if (advance_word)
972 /* Advance the completion word past any whitespace. */
973 parser->completion_word = PARSER_STREAM (parser);
976 return parser->lexer.current;
979 /* Return the next token without consuming the current token. */
981 static linespec_token
982 linespec_lexer_peek_token (linespec_parser *parser)
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;
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;
1000 /* Helper functions. */
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.
1006 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1007 canonical name for the SAL. */
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)
1015 sals->push_back (*sal);
1017 if (self->canonical)
1019 struct linespec_canonical_name *canonical;
1021 self->canonical_names = XRESIZEVEC (struct linespec_canonical_name,
1022 self->canonical_names,
1024 canonical = &self->canonical_names[sals->size () - 1];
1025 if (!literal_canonical && sal->symtab)
1027 symtab_to_fullname (sal->symtab);
1029 /* Note that the filter doesn't have to be a valid linespec
1030 input. We only apply the ":LINE" treatment to Ada for
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);
1038 canonical->suffix = xstrprintf ("%d", sal->line);
1039 canonical->symtab = sal->symtab;
1043 if (symname != NULL)
1044 canonical->suffix = xstrdup (symname);
1046 canonical->suffix = xstrdup ("<unknown>");
1047 canonical->symtab = NULL;
1052 /* A hash function for address_entry. */
1055 hash_address_entry (const void *p)
1057 const struct address_entry *aep = (const struct address_entry *) p;
1060 hash = iterative_hash_object (aep->pspace, 0);
1061 return iterative_hash_object (aep->addr, hash);
1064 /* An equality function for address_entry. */
1067 eq_address_entry (const void *a, const void *b)
1069 const struct address_entry *aea = (const struct address_entry *) a;
1070 const struct address_entry *aeb = (const struct address_entry *) b;
1072 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
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
1080 maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
1082 struct address_entry e, *p;
1087 slot = htab_find_slot (set, &e, INSERT);
1091 p = XNEW (struct address_entry);
1092 memcpy (p, &e, sizeof (struct address_entry));
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. */
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)
1112 struct objfile *objfile;
1113 struct program_space *pspace;
1115 ALL_PSPACES (pspace)
1117 if (search_pspace != NULL && search_pspace != pspace)
1119 if (pspace->executing_startup)
1122 set_current_program_space (pspace);
1124 ALL_OBJFILES (objfile)
1126 struct compunit_symtab *cu;
1129 objfile->sf->qf->expand_symtabs_matching (objfile,
1135 ALL_OBJFILE_COMPUNITS (objfile, cu)
1137 struct symtab *symtab = COMPUNIT_FILETABS (cu);
1139 iterate_over_file_blocks (symtab, lookup_name, name_domain, callback);
1143 struct block *block;
1146 for (i = FIRST_LOCAL_BLOCK;
1147 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
1150 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
1151 state->language->la_iterate_over_symbols
1152 (block, lookup_name, name_domain, [&] (symbol *sym)
1154 /* Restrict calls to CALLBACK to symbols
1155 representing inline symbols only. */
1156 if (SYMBOL_INLINED (sym))
1157 return callback (sym);
1167 /* Returns the block to be used for symbol searches from
1168 the current location. */
1170 static const struct block *
1171 get_current_search_block (void)
1173 const struct block *block;
1174 enum language save_language;
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);
1185 /* Iterate over static and global blocks. */
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)
1192 struct block *block;
1194 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
1196 block = BLOCK_SUPERBLOCK (block))
1197 LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback);
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. */
1205 find_methods (struct type *t, enum language t_lang, const char *name,
1206 VEC (const_char_ptr) **result_names,
1207 VEC (typep) **superclasses)
1210 const char *class_name = type_name_no_tag (t);
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. */
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);
1222 t = check_typedef (t);
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
1228 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1229 method_counter >= 0;
1232 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1233 char dem_opname[64];
1235 if (startswith (method_name, "__") ||
1236 startswith (method_name, "op") ||
1237 startswith (method_name, "type"))
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;
1245 if (symbol_name_compare (method_name, lookup_name, NULL))
1249 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1255 const char *phys_name;
1257 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1258 if (TYPE_FN_FIELD_STUB (f, field_counter))
1260 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1261 VEC_safe_push (const_char_ptr, *result_names, phys_name);
1267 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1268 VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
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<<. */
1277 find_toplevel_char (const char *s, char c)
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. */
1285 for (scan = s; *scan; scan++)
1289 if (*scan == quoted)
1291 else if (*scan == '\\' && *(scan + 1))
1294 else if (*scan == c && ! quoted && depth == 0)
1296 else if (*scan == '"' || *scan == '\'')
1298 else if (*scan == '(' || *scan == '<')
1300 else if ((*scan == ')' || *scan == '>') && depth > 0)
1302 else if (*scan == 'o' && !quoted && depth == 0)
1304 /* Handle C++ operator names. */
1305 if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
1307 scan += CP_OPERATOR_LEN;
1310 while (isspace (*scan))
1321 /* Skip over one less than the appropriate number of
1322 characters: the for loop will skip over the last
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. */
1353 find_toplevel_string (const char *haystack, const char *needle)
1355 const char *s = haystack;
1359 s = find_toplevel_char (s, *needle);
1363 /* Found first char in HAYSTACK; check rest of string. */
1364 if (startswith (s, needle))
1367 /* Didn't find it; loop over HAYSTACK, looking for the next
1368 instance of the first character of NEEDLE. */
1372 while (s != NULL && *s != '\0');
1374 /* NEEDLE was not found in HAYSTACK. */
1378 /* Convert CANONICAL to its string representation using
1379 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1382 canonical_to_fullform (const struct linespec_canonical_name *canonical)
1384 if (canonical->symtab == NULL)
1385 return xstrdup (canonical->suffix);
1387 return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
1391 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1392 and store the result in SELF->CANONICAL. */
1395 filter_results (struct linespec_state *self,
1396 std::vector<symtab_and_line> *result,
1397 VEC (const_char_ptr) *filters)
1402 for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
1406 for (size_t j = 0; j < result->size (); ++j)
1408 const struct linespec_canonical_name *canonical;
1410 struct cleanup *cleanup;
1412 canonical = &self->canonical_names[j];
1413 fullform = canonical_to_fullform (canonical);
1414 cleanup = make_cleanup (xfree, fullform);
1416 if (strcmp (name, fullform) == 0)
1417 lsal.sals.push_back ((*result)[j]);
1419 do_cleanups (cleanup);
1422 if (!lsal.sals.empty ())
1424 lsal.canonical = xstrdup (name);
1425 self->canonical->lsals.push_back (std::move (lsal));
1429 self->canonical->pre_expanded = 0;
1432 /* Store RESULT into SELF->CANONICAL. */
1435 convert_results_to_lsals (struct linespec_state *self,
1436 std::vector<symtab_and_line> *result)
1438 struct linespec_sals lsal;
1440 lsal.canonical = NULL;
1441 lsal.sals = std::move (*result);
1442 self->canonical->lsals.push_back (std::move (lsal));
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"
1451 struct decode_line_2_item
1453 /* The form using symtab_to_fullname.
1454 It must be xfree'ed after use. */
1457 /* The form using symtab_to_filename_for_display.
1458 It must be xfree'ed after use. */
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;
1466 /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1467 secondarily by FULLFORM. */
1470 decode_line_2_compare_items (const void *ap, const void *bp)
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;
1476 retval = strcmp (a->displayform, b->displayform);
1480 return strcmp (a->fullform, b->fullform);
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. */
1489 decode_line_2 (struct linespec_state *self,
1490 std::vector<symtab_and_line> *result,
1491 const char *select_mode)
1496 struct cleanup *old_chain;
1497 VEC (const_char_ptr) *filters = NULL;
1498 struct decode_line_2_item *items;
1501 gdb_assert (select_mode != multiple_symbols_all);
1502 gdb_assert (self->canonical != NULL);
1503 gdb_assert (!result->empty ());
1505 old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
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)
1513 const struct linespec_canonical_name *canonical;
1514 struct decode_line_2_item *item;
1516 canonical = &self->canonical_names[i];
1517 gdb_assert (canonical->suffix != NULL);
1520 item->fullform = canonical_to_fullform (canonical);
1521 make_cleanup (xfree, item->fullform);
1523 if (canonical->symtab == NULL)
1524 item->displayform = canonical->suffix;
1527 const char *fn_for_display;
1529 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
1530 item->displayform = xstrprintf ("%s:%s", fn_for_display,
1532 make_cleanup (xfree, item->displayform);
1538 /* Sort the list of method names. */
1539 qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
1541 /* Remove entries with the same FULLFORM. */
1542 if (items_count >= 2)
1544 struct decode_line_2_item *dst, *src;
1547 for (src = &items[1]; src < &items[items_count]; src++)
1548 if (strcmp (src->fullform, dst->fullform) != 0)
1550 items_count = dst + 1 - items;
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."));
1557 if (select_mode == multiple_symbols_all || items_count == 1)
1559 do_cleanups (old_chain);
1560 convert_results_to_lsals (self, result);
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);
1568 prompt = getenv ("PS2");
1573 args = command_line_input (prompt, 0, "overload-choice");
1575 if (args == 0 || *args == 0)
1576 error_no_arg (_("one or more choice numbers"));
1578 number_or_range_parser parser (args);
1579 while (!parser.finished ())
1581 int num = parser.get_number ();
1584 error (_("canceled"));
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);
1599 if (num >= items_count)
1600 printf_unfiltered (_("No choice number %d.\n"), num);
1603 struct decode_line_2_item *item = &items[num];
1605 if (!item->selected)
1607 VEC_safe_push (const_char_ptr, filters, item->fullform);
1612 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1618 filter_results (self, result, filters);
1619 do_cleanups (old_chain);
1624 /* The parser of linespec itself. */
1626 /* Throw an appropriate error when SYMBOL is not found (optionally in
1629 static void ATTRIBUTE_NORETURN
1630 symbol_not_found_error (const char *symbol, const char *filename)
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."));
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. */
1648 throw_error (NOT_FOUND_ERROR,
1649 _("Undefined convenience variable or function \"%s\" "
1650 "not defined in \"%s\"."), symbol, filename);
1652 throw_error (NOT_FOUND_ERROR,
1653 _("Undefined convenience variable or function \"%s\" "
1654 "not defined."), symbol);
1659 throw_error (NOT_FOUND_ERROR,
1660 _("Function \"%s\" not defined in \"%s\"."),
1663 throw_error (NOT_FOUND_ERROR,
1664 _("Function \"%s\" not defined."), symbol);
1668 /* Throw an appropriate error when an unexpected token is encountered
1671 static void ATTRIBUTE_NORETURN
1672 unexpected_linespec_error (linespec_parser *parser)
1674 linespec_token token;
1675 static const char * token_type_strings[]
1676 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1678 /* Get the token that generated the error. */
1679 token = linespec_lexer_lex_one (parser);
1681 /* Finally, throw the error. */
1682 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1683 || token.type == LSTOKEN_KEYWORD)
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);
1694 throw_error (GENERIC_ERROR,
1695 _("malformed linespec error: unexpected %s"),
1696 token_type_strings[token.type]);
1699 /* Throw an undefined label error. */
1701 static void ATTRIBUTE_NORETURN
1702 undefined_label_error (const char *function, const char *label)
1704 if (function != NULL)
1705 throw_error (NOT_FOUND_ERROR,
1706 _("No label \"%s\" defined in function \"%s\"."),
1709 throw_error (NOT_FOUND_ERROR,
1710 _("No label \"%s\" defined in current function."),
1714 /* Throw a source file not found error. */
1716 static void ATTRIBUTE_NORETURN
1717 source_file_not_found_error (const char *name)
1719 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1722 /* Unless at EIO, save the current stream position as completion word
1723 point, and consume the next token. */
1725 static linespec_token
1726 save_stream_and_consume_token (linespec_parser *parser)
1728 if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
1729 parser->completion_word = PARSER_STREAM (parser);
1730 return linespec_lexer_consume_token (parser);
1733 /* See description in linespec.h. */
1736 linespec_parse_line_offset (const char *string)
1738 const char *start = string;
1739 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1743 line_offset.sign = LINE_OFFSET_PLUS;
1746 else if (*string == '-')
1748 line_offset.sign = LINE_OFFSET_MINUS;
1752 if (*string != '\0' && !isdigit (*string))
1753 error (_("malformed line offset: \"%s\""), start);
1755 /* Right now, we only allow base 10 for offsets. */
1756 line_offset.offset = atoi (string);
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. */
1765 set_completion_after_number (linespec_parser *parser,
1766 linespec_complete_what next)
1768 if (*PARSER_STREAM (parser) == ' ')
1770 parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
1771 parser->complete_what = next;
1775 parser->completion_word = PARSER_STREAM (parser);
1776 parser->complete_what = linespec_complete_what::NOTHING;
1780 /* Parse the basic_spec in PARSER's input. */
1783 linespec_parse_basic (linespec_parser *parser)
1786 linespec_token token;
1787 VEC (symbolp) *symbols, *labels;
1788 VEC (bound_minimal_symbol_d) *minimal_symbols;
1789 struct cleanup *cleanup;
1791 /* Get the next token. */
1792 token = linespec_lexer_lex_one (parser);
1794 /* If it is EOI or KEYWORD, issue an error. */
1795 if (token.type == LSTOKEN_KEYWORD)
1797 parser->complete_what = linespec_complete_what::NOTHING;
1798 unexpected_linespec_error (parser);
1800 else if (token.type == LSTOKEN_EOI)
1802 unexpected_linespec_error (parser);
1804 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1805 else if (token.type == LSTOKEN_NUMBER)
1807 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
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);
1815 /* Get the next token. */
1816 token = linespec_lexer_consume_token (parser);
1818 /* If the next token is a comma, stop parsing and return. */
1819 if (token.type == LSTOKEN_COMMA)
1821 parser->complete_what = linespec_complete_what::NOTHING;
1825 /* If the next token is anything but EOI or KEYWORD, issue
1827 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1828 unexpected_linespec_error (parser);
1831 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1834 /* Next token must be LSTOKEN_STRING. */
1835 if (token.type != LSTOKEN_STRING)
1837 parser->complete_what = linespec_complete_what::NOTHING;
1838 unexpected_linespec_error (parser);
1841 /* The current token will contain the name of a function, method,
1843 name = copy_token_string (token);
1844 cleanup = make_cleanup (free_current_contents, &name);
1846 if (parser->completion_tracker != NULL)
1848 /* If the function name ends with a ":", then this may be an
1849 incomplete "::" scope operator instead of a label separator.
1852 which should expand to:
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. */
1859 if (!parser->completion_quote_char
1860 && strcmp (PARSER_STREAM (parser), ":") == 0)
1862 completion_tracker tmp_tracker;
1863 const char *source_filename
1864 = PARSER_EXPLICIT (parser)->source_filename;
1866 linespec_complete_function (tmp_tracker,
1867 parser->completion_word,
1870 if (tmp_tracker.have_completions ())
1872 PARSER_STREAM (parser)++;
1873 LS_TOKEN_STOKEN (token).length++;
1876 name = savestring (parser->completion_word,
1877 (PARSER_STREAM (parser)
1878 - parser->completion_word));
1882 PARSER_EXPLICIT (parser)->function_name = name;
1883 discard_cleanups (cleanup);
1887 /* XXX Reindent before pushing. */
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);
1894 if (symbols != NULL || minimal_symbols != NULL)
1896 PARSER_RESULT (parser)->function_symbols = symbols;
1897 PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
1898 PARSER_EXPLICIT (parser)->function_name = name;
1900 discard_cleanups (cleanup);
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,
1910 PARSER_RESULT (parser)->labels.label_symbols = labels;
1911 PARSER_RESULT (parser)->labels.function_symbols = symbols;
1912 PARSER_EXPLICIT (parser)->label_name = name;
1914 discard_cleanups (cleanup);
1916 else if (token.type == LSTOKEN_STRING
1917 && *LS_TOKEN_STOKEN (token).ptr == '$')
1919 /* User specified a convenience variable or history value. */
1920 PARSER_EXPLICIT (parser)->line_offset
1921 = linespec_parse_variable (PARSER_STATE (parser), name);
1923 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
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);
1932 /* The convenience variable/history value parsed correctly.
1933 NAME is no longer needed. */
1934 do_cleanups (cleanup);
1938 /* The name is also not a label. Abort parsing. Do not throw
1939 an error here. parse_linespec will do it for us. */
1941 /* Save a copy of the name we were trying to lookup. */
1942 PARSER_EXPLICIT (parser)->function_name = name;
1943 discard_cleanups (cleanup);
1949 int previous_qc = parser->completion_quote_char;
1951 /* Get the next token. */
1952 token = linespec_lexer_consume_token (parser);
1954 if (token.type == LSTOKEN_EOI)
1956 if (previous_qc && !parser->completion_quote_char)
1957 parser->complete_what = linespec_complete_what::KEYWORD;
1959 else if (token.type == LSTOKEN_COLON)
1961 /* User specified a label or a lineno. */
1962 token = linespec_lexer_consume_token (parser);
1964 if (token.type == LSTOKEN_NUMBER)
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);
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);
1976 /* Get the next token. */
1977 token = linespec_lexer_consume_token (parser);
1979 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
1981 parser->complete_what = linespec_complete_what::LABEL;
1983 else if (token.type == LSTOKEN_STRING)
1985 parser->complete_what = linespec_complete_what::LABEL;
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
1992 if (parser->completion_quote_char == '\0')
1994 const char *ptr = LS_TOKEN_STOKEN (token).ptr;
1995 for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
1999 LS_TOKEN_STOKEN (token).length = i;
2000 PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
2006 if (parser->completion_tracker != NULL)
2008 if (PARSER_STREAM (parser)[-1] == ' ')
2010 parser->completion_word = PARSER_STREAM (parser);
2011 parser->complete_what = linespec_complete_what::KEYWORD;
2016 /* XXX Reindent before pushing. */
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,
2027 PARSER_RESULT (parser)->labels.label_symbols = labels;
2028 PARSER_RESULT (parser)->labels.function_symbols = symbols;
2029 PARSER_EXPLICIT (parser)->label_name = name;
2031 discard_cleanups (cleanup);
2035 /* We don't know what it was, but it isn't a label. */
2036 undefined_label_error (PARSER_EXPLICIT (parser)->function_name,
2042 /* Check for a line offset. */
2043 token = save_stream_and_consume_token (parser);
2044 if (token.type == LSTOKEN_COLON)
2046 /* Get the next token. */
2047 token = linespec_lexer_consume_token (parser);
2049 /* It must be a line offset. */
2050 if (token.type != LSTOKEN_NUMBER)
2051 unexpected_linespec_error (parser);
2053 /* Record the line offset and get the next token. */
2054 name = copy_token_string (token);
2055 cleanup = make_cleanup (xfree, name);
2057 PARSER_EXPLICIT (parser)->line_offset
2058 = linespec_parse_line_offset (name);
2059 do_cleanups (cleanup);
2061 /* Get the next token. */
2062 token = linespec_lexer_consume_token (parser);
2067 /* Trailing ':' in the input. Issue an error. */
2068 unexpected_linespec_error (parser);
2073 /* Canonicalize the linespec contained in LS. The result is saved into
2074 STATE->canonical. This function handles both linespec and explicit
2078 canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
2080 struct event_location *canon;
2081 struct explicit_location *explicit_loc;
2083 /* If canonicalization was not requested, no need to do anything. */
2084 if (!state->canonical)
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);
2093 if (explicit_loc->label_name != NULL)
2095 state->canonical->special_display = 1;
2097 if (explicit_loc->function_name == NULL)
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)
2105 s = VEC_index (symbolp, ls->labels.function_symbols, 0);
2106 explicit_loc->function_name = xstrdup (SYMBOL_NATURAL_NAME (s));
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)
2114 char *linespec = explicit_location_to_linespec (explicit_loc);
2116 set_event_location_string (canon, linespec);
2121 /* Given a line offset in LS, construct the relevant SALs. */
2123 static std::vector<symtab_and_line>
2124 create_sals_line_offset (struct linespec_state *self,
2127 int use_default = 0;
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. */
2135 if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
2136 && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
2138 const char *fullname;
2140 set_current_program_space (self->program_space);
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);
2153 symtab_and_line val;
2154 val.line = ls->explicit_loc.line_offset.offset;
2155 switch (ls->explicit_loc.line_offset.sign)
2157 case LINE_OFFSET_PLUS:
2158 if (ls->explicit_loc.line_offset.offset == 0)
2161 val.line = self->default_line + val.line;
2164 case LINE_OFFSET_MINUS:
2165 if (ls->explicit_loc.line_offset.offset == 0)
2168 val.line = self->default_line - val.line;
2170 val.line = -val.line;
2173 case LINE_OFFSET_NONE:
2174 break; /* No need to adjust val.line. */
2177 std::vector<symtab_and_line> values;
2178 if (self->list_mode)
2179 values = decode_digits_list_mode (self, ls, val);
2182 struct linetable_entry *best_entry = NULL;
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,
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. */
2202 gdb::def_vector<int> filter (intermediate_results.size ());
2203 gdb::def_vector<const block *> blocks (intermediate_results.size ());
2205 for (i = 0; i < intermediate_results.size (); ++i)
2207 set_current_program_space (intermediate_results[i].pspace);
2210 blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
2211 intermediate_results[i].section);
2214 for (i = 0; i < intermediate_results.size (); ++i)
2216 if (blocks[i] != NULL)
2217 for (j = i + 1; j < intermediate_results.size (); ++j)
2219 if (blocks[j] == blocks[i])
2227 for (i = 0; i < intermediate_results.size (); ++i)
2230 struct symbol *sym = (blocks[i]
2231 ? block_containing_function (blocks[i])
2234 if (self->funfirstline)
2235 skip_prologue_sal (&intermediate_results[i]);
2236 /* Make sure the line matches the request, not what was
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);
2244 if (values.empty ())
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);
2250 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2257 /* Convert the given ADDRESS into SaLs. */
2259 static std::vector<symtab_and_line>
2260 convert_address_location_to_sals (struct linespec_state *self,
2263 symtab_and_line sal = find_pc_line (address, 0);
2265 sal.section = find_pc_overlay (address);
2266 sal.explicit_pc = 1;
2268 std::vector<symtab_and_line> sals;
2269 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
2274 /* Create and return SALs from the linespec LS. */
2276 static std::vector<symtab_and_line>
2277 convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
2279 std::vector<symtab_and_line> sals;
2281 if (ls->labels.label_symbols != NULL)
2283 /* We have just a bunch of functions/methods or labels. */
2285 struct symtab_and_line sal;
2288 for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
2290 struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));
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);
2298 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2300 /* We have just a bunch of functions and/or methods. */
2302 struct symtab_and_line sal;
2304 bound_minimal_symbol_d *elem;
2305 struct program_space *pspace;
2307 if (ls->function_symbols != NULL)
2309 /* Sort symbols so that symbols with the same program space are next
2311 qsort (VEC_address (symbolp, ls->function_symbols),
2312 VEC_length (symbolp, ls->function_symbols),
2313 sizeof (symbolp), compare_symbols);
2315 for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
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);
2326 if (ls->minimal_symbols != NULL)
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);
2334 VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
2338 pspace = elem->objfile->pspace;
2339 set_current_program_space (pspace);
2340 minsym_found (state, elem->objfile, elem->minsym, &sals);
2344 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
2346 /* Only an offset was specified. */
2347 sals = create_sals_line_offset (state, ls);
2349 /* Make sure we have a filename for canonicalization. */
2350 if (ls->explicit_loc.source_filename == NULL)
2352 const char *fullname = symtab_to_fullname (state->default_symtab);
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);
2363 /* We haven't found any results... */
2367 canonicalize_linespec (state, ls);
2369 if (!sals.empty () && state->canonical != NULL)
2370 state->canonical->pre_expanded = 1;
2375 /* Build RESULT from the explicit location components SOURCE_FILENAME,
2376 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2379 convert_explicit_location_to_linespec (struct linespec_state *self,
2381 const char *source_filename,
2382 const char *function_name,
2383 const char *label_name,
2384 struct line_offset line_offset)
2386 VEC (symbolp) *symbols, *labels;
2387 VEC (bound_minimal_symbol_d) *minimal_symbols;
2389 if (source_filename != NULL)
2393 result->file_symtabs
2394 = symtabs_from_filename (source_filename, self->search_pspace);
2396 CATCH (except, RETURN_MASK_ERROR)
2398 source_file_not_found_error (source_filename);
2401 result->explicit_loc.source_filename = xstrdup (source_filename);
2405 /* A NULL entry means to use the default symtab. */
2406 VEC_safe_push (symtab_ptr, result->file_symtabs, NULL);
2409 if (function_name != NULL)
2411 find_linespec_symbols (self, result->file_symtabs,
2412 function_name, &symbols,
2415 if (symbols == NULL && minimal_symbols == NULL)
2416 symbol_not_found_error (function_name,
2417 result->explicit_loc.source_filename);
2419 result->explicit_loc.function_name = xstrdup (function_name);
2420 result->function_symbols = symbols;
2421 result->minimal_symbols = minimal_symbols;
2424 if (label_name != NULL)
2427 labels = find_label_symbols (self, result->function_symbols,
2428 &symbols, label_name);
2431 undefined_label_error (result->explicit_loc.function_name,
2434 result->explicit_loc.label_name = xstrdup (label_name);
2435 result->labels.label_symbols = labels;
2436 result->labels.function_symbols = symbols;
2439 if (line_offset.sign != LINE_OFFSET_UNKNOWN)
2440 result->explicit_loc.line_offset = line_offset;
2443 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2445 static std::vector<symtab_and_line>
2446 convert_explicit_location_to_sals (struct linespec_state *self,
2448 const struct explicit_location *explicit_loc)
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);
2458 /* Parse a string that specifies a linespec.
2460 The basic grammar of linespecs:
2462 linespec -> var_spec | basic_spec
2463 var_spec -> '$' (STRING | NUMBER)
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
2470 opt_file_spec -> "" | file_name_spec ':'
2471 opt_label_spec -> "" | ':' label_name_spec
2473 file_name_spec -> STRING
2474 function_name_spec -> STRING
2475 label_name_spec -> STRING
2476 function_name_spec -> STRING
2477 offset_spec -> NUMBER
2481 This may all be followed by several keywords such as "if EXPR",
2484 A comma will terminate parsing.
2486 The function may be an undebuggable function found in minimal symbol table.
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.
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.
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. */
2506 /* Parse the linespec in ARG. */
2508 static std::vector<symtab_and_line>
2509 parse_linespec (linespec_parser *parser, const char *arg)
2511 linespec_token token;
2512 struct gdb_exception file_exception = exception_none;
2513 struct cleanup *cleanup;
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)
2525 end = skip_quote_char (arg + 1, *arg);
2526 if (end != NULL && is_closing_quote_enclosed (end))
2528 /* Here's the special case. Skip ARG past the initial
2531 parser->is_quote_enclosed = 1;
2535 parser->lexer.saved_arg = arg;
2536 parser->lexer.stream = arg;
2537 parser->completion_word = arg;
2538 parser->complete_what = linespec_complete_what::FUNCTION;
2540 /* Initialize the default symtab and line offset. */
2541 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2542 &PARSER_STATE (parser)->default_line);
2544 /* Objective-C shortcut. */
2545 if (parser->completion_tracker == NULL)
2547 std::vector<symtab_and_line> values
2548 = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2549 if (!values.empty ())
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
2558 if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
2562 /* Start parsing. */
2564 /* Get the first token. */
2565 token = linespec_lexer_consume_token (parser);
2567 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2568 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
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);
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);
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)
2587 /* Consume this token. */
2588 linespec_lexer_consume_token (parser);
2590 goto convert_to_sals;
2593 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
2595 /* Let the default linespec_complete_what::FUNCTION kick in. */
2596 unexpected_linespec_error (parser);
2598 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
2600 parser->complete_what = linespec_complete_what::NOTHING;
2601 unexpected_linespec_error (parser);
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);
2608 if (token.type == LSTOKEN_COLON)
2610 char *user_filename;
2612 /* Get the current token again and extract the filename. */
2613 token = linespec_lexer_lex_one (parser);
2614 user_filename = copy_token_string (token);
2616 /* Check if the input is a filename. */
2619 PARSER_RESULT (parser)->file_symtabs
2620 = symtabs_from_filename (user_filename,
2621 PARSER_STATE (parser)->search_pspace);
2623 CATCH (ex, RETURN_MASK_ERROR)
2625 file_exception = ex;
2629 if (file_exception.reason >= 0)
2631 /* Symtabs were found for the file. Record the filename. */
2632 PARSER_EXPLICIT (parser)->source_filename = user_filename;
2634 /* Get the next token. */
2635 token = linespec_lexer_consume_token (parser);
2637 /* This is LSTOKEN_COLON; consume it. */
2638 linespec_lexer_consume_token (parser);
2642 /* No symtabs found -- discard user_filename. */
2643 xfree (user_filename);
2645 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2646 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
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))
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);
2661 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2662 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
2665 /* Parse the rest of the linespec. */
2666 linespec_parse_basic (parser);
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)
2674 /* The linespec didn't parse. Re-throw the file exception if
2676 if (file_exception.reason < 0)
2677 throw_exception (file_exception);
2679 /* Otherwise, the symbol is not found. */
2680 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2681 PARSER_EXPLICIT (parser)->source_filename);
2686 /* Get the last token and record how much of the input was parsed,
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)
2693 /* Setup the completion word past the keyword. Lexing never
2694 advances past a keyword automatically, so skip it
2696 parser->completion_word
2697 = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
2698 parser->complete_what = linespec_complete_what::EXPRESSION;
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));
2710 /* A constructor for linespec_state. */
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,
2718 struct linespec_result *canonical)
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;
2734 /* Initialize a new linespec parser. */
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,
2742 struct linespec_result *canonical)
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,
2750 default_symtab, default_line, canonical);
2753 /* A destructor for linespec_state. */
2756 linespec_state_destructor (struct linespec_state *self)
2758 htab_delete (self->addr_set);
2761 /* Delete a linespec parser. */
2764 linespec_parser_delete (void *arg)
2766 linespec_parser *parser = (linespec_parser *) arg;
2768 xfree (PARSER_EXPLICIT (parser)->source_filename);
2769 xfree (PARSER_EXPLICIT (parser)->label_name);
2770 xfree (PARSER_EXPLICIT (parser)->function_name);
2772 if (PARSER_RESULT (parser)->file_symtabs != NULL)
2773 VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);
2775 if (PARSER_RESULT (parser)->function_symbols != NULL)
2776 VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);
2778 if (PARSER_RESULT (parser)->minimal_symbols != NULL)
2779 VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);
2781 if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
2782 VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);
2784 if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
2785 VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);
2787 linespec_state_destructor (PARSER_STATE (parser));
2790 /* See description in linespec.h. */
2793 linespec_lex_to_end (const char **stringp)
2795 linespec_parser parser;
2796 struct cleanup *cleanup;
2797 linespec_token token;
2800 if (stringp == NULL || *stringp == NULL)
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;
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)
2815 token = linespec_lexer_consume_token (&parser);
2817 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2819 *stringp += PARSER_STREAM (&parser) - orig;
2820 do_cleanups (cleanup);
2823 /* See linespec.h. */
2826 linespec_complete_function (completion_tracker &tracker,
2827 const char *function,
2828 const char *source_filename)
2830 complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
2831 symbol_name_match_type func_match_type = symbol_name_match_type::WILD;
2833 if (source_filename != NULL)
2835 collect_file_symbol_completion_matches (tracker, mode, func_match_type,
2836 function, function, source_filename);
2840 collect_symbol_completion_matches (tracker, mode, func_match_type,
2841 function, function);
2846 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2847 only meaningful if COMPONENT is FUNCTION. */
2850 complete_linespec_component (linespec_parser *parser,
2851 completion_tracker &tracker,
2853 linespec_complete_what component,
2854 const char *source_filename)
2856 if (component == linespec_complete_what::KEYWORD)
2858 complete_on_enum (tracker, linespec_keywords, text, text);
2860 else if (component == linespec_complete_what::EXPRESSION)
2863 = advance_to_expression_complete_word_point (tracker, text);
2864 complete_expression (tracker, text, word);
2866 else if (component == linespec_complete_what::FUNCTION)
2868 completion_list fn_list;
2870 linespec_complete_function (tracker, text, source_filename);
2871 if (source_filename == NULL)
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);
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
2882 if (fn_list.size () == 1 && !tracker.have_completions ())
2884 char *fn = fn_list[0].release ();
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 ())
2891 char quote_char_str[2] = { tracker.quote_char () };
2893 fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
2894 tracker.set_quote_char (':');
2897 fn = reconcat (fn, fn, ":", (char *) NULL);
2898 fn_list[0].reset (fn);
2900 /* Tell readline to skip appending a space. */
2901 tracker.set_suppress_append_ws (true);
2903 tracker.add_completions (std::move (fn_list));
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. */
2912 complete_label (completion_tracker &tracker,
2913 linespec_parser *parser,
2914 const char *label_name)
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,
2925 VEC_iterate (symbolp, labels, ix, label); ++ix)
2927 char *match = xstrdup (SYMBOL_SEARCH_NAME (label));
2928 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
2930 VEC_free (symbolp, labels);
2933 /* See linespec.h. */
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)
2942 linespec_parser parser;
2943 struct cleanup *cleanup;
2945 linespec_parser_new (&parser, 0, language, NULL, NULL, 0, NULL);
2946 cleanup = make_cleanup (linespec_parser_delete, &parser);
2948 line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
2952 convert_explicit_location_to_linespec (PARSER_STATE (&parser),
2953 PARSER_RESULT (&parser),
2956 NULL, unknown_offset);
2958 CATCH (ex, RETURN_MASK_ERROR)
2960 do_cleanups (cleanup);
2965 complete_label (tracker, &parser, label_name);
2967 do_cleanups (cleanup);
2970 /* See description in linespec.h. */
2973 linespec_complete (completion_tracker &tracker, const char *text)
2975 linespec_parser parser;
2976 struct cleanup *cleanup;
2977 const char *orig = text;
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;
2984 parser.completion_tracker = &tracker;
2985 PARSER_STATE (&parser)->is_linespec = 1;
2987 /* Parse as much as possible. parser.completion_word will hold
2988 furthest completion point we managed to parse to. */
2991 parse_linespec (&parser, text);
2993 CATCH (except, RETURN_MASK_ERROR)
2998 if (parser.completion_quote_char != '\0'
2999 && parser.completion_quote_end != NULL
3000 && parser.completion_quote_end[1] == '\0')
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:
3008 before: "b function()"
3009 after: "b function() "
3011 before: "b 'function()'"
3012 after: "b 'function()' "
3014 and trusts the user in this case:
3016 before: "b 'not_loaded_function_yet()'"
3017 after: "b 'not_loaded_function_yet()' "
3019 parser.complete_what = linespec_complete_what::NOTHING;
3020 parser.completion_quote_char = '\0';
3022 gdb::unique_xmalloc_ptr<char> text_copy
3023 (xstrdup (parser.completion_word));
3024 tracker.add_completion (std::move (text_copy));
3027 tracker.set_quote_char (parser.completion_quote_char);
3029 if (parser.complete_what == linespec_complete_what::LABEL)
3031 parser.complete_what = linespec_complete_what::NOTHING;
3033 const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
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,
3040 &function_symbols, &minimal_symbols);
3042 PARSER_RESULT (&parser)->function_symbols = function_symbols;
3043 PARSER_RESULT (&parser)->minimal_symbols = minimal_symbols;
3045 complete_label (tracker, &parser, parser.completion_word);
3047 else if (parser.complete_what == linespec_complete_what::FUNCTION)
3049 /* While parsing/lexing, we didn't know whether the completion
3050 word completes to a unique function/source name already or
3054 "b function() <tab>"
3055 may need to complete either to:
3056 "b function() const"
3058 "b function() if/thread/task"
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:
3069 may need to complete either to a source file name:
3071 or this, also a filename, but a unique completion:
3073 or to a function name:
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.
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. */
3088 const char *text = parser.completion_word;
3089 const char *word = parser.completion_word;
3091 complete_linespec_component (&parser, tracker,
3092 parser.completion_word,
3093 linespec_complete_what::FUNCTION,
3094 PARSER_EXPLICIT (&parser)->source_filename);
3096 parser.complete_what = linespec_complete_what::NOTHING;
3098 if (tracker.quote_char ())
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. */
3105 else if (!tracker.have_completions ())
3108 size_t wordlen = strlen (parser.completion_word);
3111 = string_find_incomplete_keyword_at_end (linespec_keywords,
3112 parser.completion_word,
3117 && parser.completion_word[wordlen - 1] == ' '))
3119 parser.completion_word += key_start;
3120 parser.complete_what = linespec_complete_what::KEYWORD;
3123 else if (tracker.completes_to_completion_word (word))
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 ();
3132 tracker.advance_custom_word_point_by (parser.completion_word - orig);
3134 complete_linespec_component (&parser, tracker,
3135 parser.completion_word,
3136 parser.complete_what,
3137 PARSER_EXPLICIT (&parser)->source_filename);
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
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 ())
3150 = parser.completion_word + strlen (parser.completion_word);
3152 if (end > orig && end[-1] == ' ')
3154 tracker.advance_custom_word_point_by (end - parser.completion_word);
3156 complete_linespec_component (&parser, tracker, end,
3157 linespec_complete_what::KEYWORD,
3162 do_cleanups (cleanup);
3165 /* A helper function for decode_line_full and decode_line_1 to
3166 turn LOCATION into std::vector<symtab_and_line>. */
3168 static std::vector<symtab_and_line>
3169 event_location_to_sals (linespec_parser *parser,
3170 const struct event_location *location)
3172 std::vector<symtab_and_line> result;
3174 switch (event_location_type (location))
3176 case LINESPEC_LOCATION:
3178 PARSER_STATE (parser)->is_linespec = 1;
3181 result = parse_linespec (parser, get_linespec_location (location));
3183 CATCH (except, RETURN_MASK_ERROR)
3185 throw_exception (except);
3191 case ADDRESS_LOCATION:
3193 const char *addr_string = get_address_string_location (location);
3194 CORE_ADDR addr = get_address_location (location);
3196 if (addr_string != NULL)
3198 char *expr = xstrdup (addr_string);
3199 const char *const_expr = expr;
3200 struct cleanup *cleanup = make_cleanup (xfree, expr);
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);
3207 do_cleanups (cleanup);
3210 result = convert_address_location_to_sals (PARSER_STATE (parser),
3215 case EXPLICIT_LOCATION:
3217 const struct explicit_location *explicit_loc;
3219 explicit_loc = get_explicit_location_const (location);
3220 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
3221 PARSER_RESULT (parser),
3226 case PROBE_LOCATION:
3227 /* Probes are handled by their own decoders. */
3228 gdb_assert_not_reached ("attempt to decode probe location");
3232 gdb_assert_not_reached ("unhandled event location type");
3238 /* See linespec.h. */
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,
3248 struct cleanup *cleanups;
3249 VEC (const_char_ptr) *filters = NULL;
3250 linespec_parser parser;
3251 struct linespec_state *state;
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);
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);
3267 scoped_restore_current_program_space restore_pspace;
3269 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3271 state = PARSER_STATE (&parser);
3273 gdb_assert (result.size () == 1 || canonical->pre_expanded);
3274 canonical->pre_expanded = 1;
3276 /* Arrange for allocated canonical names to be freed. */
3277 if (!result.empty ())
3281 make_cleanup (xfree, state->canonical_names);
3282 for (i = 0; i < result.size (); ++i)
3284 gdb_assert (state->canonical_names[i].suffix != NULL);
3285 make_cleanup (xfree, state->canonical_names[i].suffix);
3289 if (select_mode == NULL)
3291 if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ())
3292 select_mode = multiple_symbols_all;
3294 select_mode = multiple_symbols_select_mode ();
3297 if (select_mode == multiple_symbols_all)
3301 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
3302 VEC_safe_push (const_char_ptr, filters, filter);
3303 filter_results (state, &result, filters);
3306 convert_results_to_lsals (state, &result);
3309 decode_line_2 (state, &result, select_mode);
3311 do_cleanups (cleanups);
3314 /* See linespec.h. */
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,
3322 linespec_parser parser;
3323 struct cleanup *cleanups;
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);
3330 scoped_restore_current_program_space restore_pspace;
3332 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3335 do_cleanups (cleanups);
3339 /* See linespec.h. */
3341 std::vector<symtab_and_line>
3342 decode_line_with_current_source (const char *string, int flags)
3345 error (_("Empty line specification."));
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 ();
3351 event_location_up location = string_to_event_location (&string,
3353 std::vector<symtab_and_line> sals
3354 = decode_line_1 (location.get (), flags, NULL, cursal.symtab, cursal.line);
3357 error (_("Junk at end of line specification: %s"), string);
3362 /* See linespec.h. */
3364 std::vector<symtab_and_line>
3365 decode_line_with_last_displayed (const char *string, int flags)
3368 error (_("Empty line specification."));
3370 event_location_up location = string_to_event_location (&string,
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));
3381 error (_("Junk at end of line specification: %s"), string);
3388 /* First, some functions to initialize stuff at the beggining of the
3392 initialize_defaults (struct symtab **default_symtab, int *default_line)
3394 if (*default_symtab == 0)
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
3399 struct symtab_and_line cursal =
3400 get_current_source_symtab_and_line ();
3402 *default_symtab = cursal.symtab;
3403 *default_line = cursal.line;
3409 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3410 advancing EXP_PTR past any parsed text. */
3413 linespec_expression_to_pc (const char **exp_ptr)
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"));
3422 return value_as_address (parse_to_comma_and_eval (exp_ptr));
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. */
3434 static std::vector<symtab_and_line>
3435 decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
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),
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;
3450 new_argptr = find_imps (arg, &symbol_names);
3451 if (VEC_empty (const_char_ptr, symbol_names))
3453 do_cleanups (cleanup);
3457 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
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))
3465 saved_arg = (char *) alloca (new_argptr - arg + 1);
3466 memcpy (saved_arg, arg, new_argptr - arg);
3467 saved_arg[new_argptr - arg] = '\0';
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);
3474 if (self->canonical)
3479 self->canonical->pre_expanded = 1;
3481 if (ls->explicit_loc.source_filename)
3483 holder = string_printf ("%s:%s",
3484 ls->explicit_loc.source_filename,
3486 str = holder.c_str ();
3491 self->canonical->location = new_linespec_location (&str);
3495 do_cleanups (cleanup);
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
3508 decode_compound_collector ()
3511 m_unique_syms = htab_create_alloc (1, htab_hash_pointer,
3512 htab_eq_pointer, NULL,
3516 ~decode_compound_collector ()
3518 if (m_unique_syms != NULL)
3519 htab_delete (m_unique_syms);
3522 /* Releases ownership of the collected symbols and returns them. */
3523 VEC (symbolp) *release_symbols ()
3525 VEC (symbolp) *res = m_symbols;
3530 /* Callable as a symbol_found_callback_ftype callback. */
3531 bool operator () (symbol *sym);
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;
3538 /* The result vector. */
3539 VEC (symbolp) *m_symbols;
3543 decode_compound_collector::operator () (symbol *sym)
3548 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
3549 return true; /* Continue iterating. */
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. */
3558 slot = htab_find_slot (m_unique_syms, sym, INSERT);
3562 VEC_safe_push (symbolp, m_symbols, sym);
3565 return true; /* Continue iterating. */
3570 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3572 static VEC (symbolp) *
3573 lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
3574 const char *class_name)
3578 decode_compound_collector collector;
3580 lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
3582 for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
3586 iterate_over_all_matching_symtabs (state, lookup_name,
3587 STRUCT_DOMAIN, NULL, false,
3589 iterate_over_all_matching_symtabs (state, lookup_name,
3590 VAR_DOMAIN, NULL, false,
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);
3604 return collector.release_symbols ();
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. */
3612 compare_symbols (const void *a, const void *b)
3614 struct symbol * const *sa = (struct symbol * const*) a;
3615 struct symbol * const *sb = (struct symbol * const*) b;
3618 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa));
3619 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb));
3626 uia = (uintptr_t) *sa;
3627 uib = (uintptr_t) *sb;
3637 /* Like compare_symbols but for minimal symbols. */
3640 compare_msymbols (const void *a, const void *b)
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;
3648 uia = (uintptr_t) sa->objfile->pspace;
3649 uib = (uintptr_t) sa->objfile->pspace;
3656 uia = (uintptr_t) sa->minsym;
3657 uib = (uintptr_t) sb->minsym;
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. */
3673 add_all_symbol_names_from_pspace (struct collect_info *info,
3674 struct program_space *pspace,
3675 VEC (const_char_ptr) *names)
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,
3687 find_superclass_methods (VEC (typep) *superclasses,
3688 const char *name, enum language name_lang,
3689 VEC (const_char_ptr) **result_names)
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);
3695 iter_classes = superclasses;
3698 VEC (typep) *new_supers = NULL;
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);
3706 if (VEC_length (const_char_ptr, *result_names) != old_len
3707 || VEC_empty (typep, new_supers))
3710 iter_classes = new_supers;
3713 do_cleanups (cleanup);
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). */
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)
3727 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
3729 int last_result_len;
3730 VEC (typep) *superclass_vec;
3731 VEC (const_char_ptr) *result_names;
3732 struct collect_info info;
3734 /* Sort symbols so that symbols with the same program space are next
3736 qsort (VEC_address (symbolp, sym_classes),
3737 VEC_length (symbolp, sym_classes),
3742 info.file_symtabs = file_symtabs;
3743 info.result.symbols = NULL;
3744 info.result.minimal_symbols = NULL;
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
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)
3763 struct program_space *pspace;
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);
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
3778 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes,
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);
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);
3792 VEC_truncate (typep, superclass_vec, 0);
3793 last_result_len = VEC_length (const_char_ptr, result_names);
3797 if (!VEC_empty (symbolp, info.result.symbols)
3798 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
3800 *symbols = info.result.symbols;
3801 *minsyms = info.result.minimal_symbols;
3802 do_cleanups (cleanup);
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"));
3815 /* This function object is a callback for iterate_over_symtabs, used
3816 when collecting all matching symtabs. */
3818 class symtab_collector
3824 m_symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
3828 ~symtab_collector ()
3830 if (m_symtab_table != NULL)
3831 htab_delete (m_symtab_table);
3834 /* Callable as a symbol_found_callback_ftype callback. */
3835 bool operator () (symtab *sym);
3837 /* Releases ownership of the collected symtabs and returns them. */
3838 VEC (symtab_ptr) *release_symtabs ()
3840 VEC (symtab_ptr) *res = m_symtabs;
3846 /* The result vector of symtabs. */
3847 VEC (symtab_ptr) *m_symtabs;
3849 /* This is used to ensure the symtabs are unique. */
3850 htab_t m_symtab_table;
3854 symtab_collector::operator () (struct symtab *symtab)
3858 slot = htab_find_slot (m_symtab_table, symtab, INSERT);
3862 VEC_safe_push (symtab_ptr, m_symtabs, symtab);
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
3874 static VEC (symtab_ptr) *
3875 collect_symtabs_from_filename (const char *file,
3876 struct program_space *search_pspace)
3878 symtab_collector collector;
3880 /* Find that file's data. */
3881 if (search_pspace == NULL)
3883 struct program_space *pspace;
3885 ALL_PSPACES (pspace)
3887 if (pspace->executing_startup)
3890 set_current_program_space (pspace);
3891 iterate_over_symtabs (file, collector);
3896 set_current_program_space (search_pspace);
3897 iterate_over_symtabs (file, collector);
3900 return collector.release_symtabs ();
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. */
3906 static VEC (symtab_ptr) *
3907 symtabs_from_filename (const char *filename,
3908 struct program_space *search_pspace)
3910 VEC (symtab_ptr) *result;
3912 result = collect_symtabs_from_filename (filename, search_pspace);
3914 if (VEC_empty (symtab_ptr, result))
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);
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. */
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)
3936 struct collect_info info;
3937 VEC (const_char_ptr) *symbol_names = NULL;
3938 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
3942 info.result.symbols = NULL;
3943 info.result.minimal_symbols = NULL;
3944 info.file_symtabs = file_symtabs;
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,
3952 add_matching_symbols_to_info (name, symbol_name_match_type::WILD,
3953 &info, state->search_pspace);
3955 do_cleanups (cleanup);
3957 if (VEC_empty (symbolp, info.result.symbols))
3959 VEC_free (symbolp, info.result.symbols);
3963 *symbols = info.result.symbols;
3965 if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
3967 VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
3971 *minsyms = info.result.minimal_symbols;
3974 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3975 in SYMBOLS and minimal symbols in MINSYMS. */
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)
3984 std::string canon = cp_canonicalize_string_no_typedefs (lookup_name);
3985 if (!canon.empty ())
3986 lookup_name = canon.c_str ();
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) */
3997 find_function_symbols (state, file_symtabs, lookup_name,
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
4003 if (VEC_empty (symbolp, *symbols)
4004 && VEC_empty (bound_minimal_symbol_d, *minsyms))
4006 std::string klass, method;
4007 const char *last, *p, *scope_op;
4008 VEC (symbolp) *classes;
4010 /* See if we can find a scope operator and break this symbol
4011 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
4013 p = find_toplevel_string (lookup_name, scope_op);
4019 p = find_toplevel_string (p + strlen (scope_op), scope_op);
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
4028 /* LOOKUP_NAME points to the class name.
4029 LAST points to the method name. */
4030 klass = std::string (lookup_name, last - lookup_name);
4032 /* Skip past the scope operator. */
4033 last += strlen (scope_op);
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);
4041 if (!VEC_empty (symbolp, classes))
4043 /* Now locate a list of suitable methods named METHOD. */
4046 find_method (state, file_symtabs,
4047 klass.c_str (), method.c_str (),
4048 classes, symbols, minsyms);
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)
4055 if (except.error != NOT_FOUND_ERROR)
4056 throw_exception (except);
4061 do_cleanups (old_chain);
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. */
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)
4079 if (completion_mode)
4081 struct block_iterator iter;
4083 size_t name_len = strlen (name);
4085 int (*cmp) (const char *, const char *, size_t);
4086 cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
4088 ALL_BLOCK_SYMBOLS (block, iter, sym)
4090 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
4091 SYMBOL_DOMAIN (sym), LABEL_DOMAIN)
4092 && cmp (SYMBOL_SEARCH_NAME (sym), name, name_len) == 0)
4094 VEC_safe_push (symbolp, *result, sym);
4095 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
4101 struct symbol *sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol;
4105 VEC_safe_push (symbolp, *result, sym);
4106 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
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. */
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)
4124 const struct block *block;
4126 struct symbol *fn_sym;
4127 VEC (symbolp) *result = NULL;
4129 if (function_symbols == NULL)
4131 set_current_program_space (self->program_space);
4132 block = get_current_search_block ();
4135 block && !BLOCK_FUNCTION (block);
4136 block = BLOCK_SUPERBLOCK (block))
4140 fn_sym = BLOCK_FUNCTION (block);
4142 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4143 &result, label_funcs_ret);
4148 VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
4150 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
4151 block = SYMBOL_BLOCK_VALUE (fn_sym);
4153 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4154 &result, label_funcs_ret);
4163 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4165 static std::vector<symtab_and_line>
4166 decode_digits_list_mode (struct linespec_state *self,
4168 struct symtab_and_line val)
4173 gdb_assert (self->list_mode);
4175 std::vector<symtab_and_line> values;
4177 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
4180 /* The logic above should ensure this. */
4181 gdb_assert (elt != NULL);
4183 set_current_program_space (SYMTAB_PSPACE (elt));
4185 /* Simplistic search just for the list command. */
4186 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
4187 if (val.symtab == NULL)
4189 val.pspace = SYMTAB_PSPACE (elt);
4191 val.explicit_line = 1;
4193 add_sal_to_sals (self, &values, &val, NULL, 0);
4199 /* A helper for create_sals_line_offset that iterates over the symtabs,
4200 adding lines to the VEC. */
4202 static std::vector<symtab_and_line>
4203 decode_digits_ordinary (struct linespec_state *self,
4206 struct linetable_entry **best_entry)
4211 std::vector<symtab_and_line> sals;
4212 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
4214 std::vector<CORE_ADDR> pcs;
4216 /* The logic above should ensure this. */
4217 gdb_assert (elt != NULL);
4219 set_current_program_space (SYMTAB_PSPACE (elt));
4221 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
4222 for (CORE_ADDR pc : pcs)
4224 symtab_and_line sal;
4225 sal.pspace = SYMTAB_PSPACE (elt);
4229 sals.push_back (std::move (sal));
4238 /* Return the line offset represented by VARIABLE. */
4240 static struct line_offset
4241 linespec_parse_variable (struct linespec_state *self, const char *variable)
4245 struct line_offset offset = {0, LINE_OFFSET_NONE};
4247 p = (variable[1] == '$') ? variable + 2 : variable + 1;
4250 while (*p >= '0' && *p <= '9')
4252 if (!*p) /* Reached end of token without hitting non-digit. */
4254 /* We have a value history reference. */
4255 struct value *val_history;
4257 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
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);
4267 /* Not all digits -- may be user variable/function or a
4268 convenience variable. */
4270 struct internalvar *ivar;
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);
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;
4281 /* We found a valid variable name. If it is not an integer,
4283 if (!get_internalvar_integer (ivar, &valx))
4284 error (_("Convenience variables used in line "
4285 "specs must have integer values."));
4287 offset.offset = valx;
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. */
4301 minsym_found (struct linespec_state *self, struct objfile *objfile,
4302 struct minimal_symbol *msymbol,
4303 std::vector<symtab_and_line> *result)
4305 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4307 struct symtab_and_line sal;
4309 if (msymbol_is_text (msymbol))
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);
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,
4320 sal = find_pc_sect_line (pc, NULL, 0);
4322 if (self->funfirstline)
4324 if (sal.symtab != NULL
4325 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
4326 || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
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,
4336 if (gdbarch_skip_entrypoint_p (gdbarch))
4337 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
4340 skip_prologue_sal (&sal);
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);
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);
4355 /* A helper struct to pass some data through
4356 iterate_over_minimal_symbols. */
4358 struct collect_minsyms
4360 /* The objfile we're examining. */
4361 struct objfile *objfile;
4363 /* Only search the given symtab, or NULL to search for all symbols. */
4364 struct symtab *symtab;
4366 /* The funfirstline setting from the initial call. */
4369 /* The list_mode setting from the initial call. */
4372 /* The resulting symbols. */
4373 VEC (bound_minimal_symbol_d) *msyms;
4376 /* A helper function to classify a minimal_symbol_type according to
4380 classify_mtype (enum minimal_symbol_type t)
4387 /* Intermediate priority. */
4390 case mst_solib_trampoline:
4391 /* Lowest priority. */
4395 /* Highest priority. */
4400 /* Callback for qsort that sorts symbols by priority. */
4403 compare_msyms (const void *a, const void *b)
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);
4410 return classify_mtype (ta) - classify_mtype (tb);
4413 /* Callback for iterate_over_minimal_symbols that adds the symbol to
4417 add_minsym (struct minimal_symbol *minsym, void *d)
4419 struct collect_minsyms *info = (struct collect_minsyms *) d;
4420 bound_minimal_symbol_d mo;
4423 mo.objfile = info->objfile;
4425 if (info->symtab != NULL)
4428 struct symtab_and_line sal;
4429 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
4431 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym),
4433 sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym);
4435 = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, ¤t_target);
4437 sal = find_pc_sect_line (pc, NULL, 0);
4439 if (info->symtab != sal.symtab)
4443 /* Exclude data symbols when looking for breakpoint locations. */
4444 if (!info->list_mode)
4445 switch (minsym->type)
4447 case mst_slot_got_plt:
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),
4461 if (addr == BMSYMBOL_VALUE_ADDRESS (mo))
4466 VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
4469 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4470 is not NULL, the search is restricted to just that program
4473 If SYMTAB is NULL, search all objfiles, otherwise
4474 restrict results to the given SYMTAB. */
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)
4482 struct collect_minsyms local;
4483 struct cleanup *cleanup;
4485 memset (&local, 0, sizeof (local));
4486 local.funfirstline = info->state->funfirstline;
4487 local.list_mode = info->state->list_mode;
4488 local.symtab = symtab;
4490 cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);
4494 struct program_space *pspace;
4496 ALL_PSPACES (pspace)
4498 struct objfile *objfile;
4500 if (search_pspace != NULL && search_pspace != pspace)
4502 if (pspace->executing_startup)
4505 set_current_program_space (pspace);
4507 ALL_OBJFILES (objfile)
4509 local.objfile = objfile;
4510 iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
4516 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
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);
4524 if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
4528 bound_minimal_symbol_d *item;
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),
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));
4542 VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
4545 if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
4548 VEC_safe_push (bound_minimal_symbol_d,
4549 info->result.minimal_symbols, item);
4553 do_cleanups (cleanup);
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
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)
4569 lookup_name_info lookup_name (name, name_match_type);
4571 for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
4575 iterate_over_all_matching_symtabs (info->state, lookup_name,
4577 pspace, true, [&] (symbol *sym)
4578 { return info->add_symbol (sym); });
4579 search_minsyms_for_name (info, lookup_name, pspace, NULL);
4581 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
4583 int prev_len = VEC_length (symbolp, info->result.symbols);
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,
4591 { return info->add_symbol (sym); });
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
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);
4606 /* Now come some functions that are called from multiple places within
4610 symbol_to_sal (struct symtab_and_line *result,
4611 int funfirstline, struct symbol *sym)
4613 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
4615 *result = find_function_start_sal (sym, funfirstline);
4620 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
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;
4631 else if (funfirstline)
4635 else if (SYMBOL_LINE (sym) != 0)
4637 /* We know its line number. */
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);
4651 linespec_result::~linespec_result ()
4653 for (linespec_sals &lsal : lsals)
4654 xfree (lsal.canonical);
4657 /* Return the quote characters permitted by the linespec parser. */
4660 get_gdb_linespec_parser_quote_characters (void)
4662 return linespec_quote_characters;