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"
49 typedef struct symbol *symbolp;
52 typedef struct type *typep;
55 /* An address entry is used to ensure that any given location is only
56 added to the result a single time. It holds an address and the
57 program space from which the address came. */
61 struct program_space *pspace;
65 typedef struct bound_minimal_symbol bound_minimal_symbol_d;
67 DEF_VEC_O (bound_minimal_symbol_d);
69 /* A linespec. Elements of this structure are filled in by a parser
70 (either parse_linespec or some other function). The structure is
71 then converted into SALs by convert_linespec_to_sals. */
75 /* An explicit location describing the SaLs. */
76 struct explicit_location explicit_loc;
78 /* The list of symtabs to search to which to limit the search. May not
79 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
80 filename), FILE_SYMTABS should contain one single NULL member. This
81 will cause the code to use the default symtab. */
82 VEC (symtab_ptr) *file_symtabs;
84 /* A list of matching function symbols and minimal symbols. Both lists
85 may be NULL if no matching symbols were found. */
86 VEC (symbolp) *function_symbols;
87 VEC (bound_minimal_symbol_d) *minimal_symbols;
89 /* A structure of matching label symbols and the corresponding
90 function symbol in which the label was found. Both may be NULL
91 or both must be non-NULL. */
94 VEC (symbolp) *label_symbols;
95 VEC (symbolp) *function_symbols;
98 typedef struct linespec *linespec_p;
100 /* A canonical linespec represented as a symtab-related string.
102 Each entry represents the "SYMTAB:SUFFIX" linespec string.
103 SYMTAB can be converted for example by symtab_to_fullname or
104 symtab_to_filename_for_display as needed. */
106 struct linespec_canonical_name
108 /* Remaining text part of the linespec string. */
111 /* If NULL then SUFFIX is the whole linespec string. */
112 struct symtab *symtab;
115 /* An instance of this is used to keep all state while linespec
116 operates. This instance is passed around as a 'this' pointer to
117 the various implementation methods. */
119 struct linespec_state
121 /* The language in use during linespec processing. */
122 const struct language_defn *language;
124 /* The program space as seen when the module was entered. */
125 struct program_space *program_space;
127 /* If not NULL, the search is restricted to just this program
129 struct program_space *search_pspace;
131 /* The default symtab to use, if no other symtab is specified. */
132 struct symtab *default_symtab;
134 /* The default line to use. */
137 /* The 'funfirstline' value that was passed in to decode_line_1 or
141 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
144 /* The 'canonical' value passed to decode_line_full, or NULL. */
145 struct linespec_result *canonical;
147 /* Canonical strings that mirror the symtabs_and_lines result. */
148 struct linespec_canonical_name *canonical_names;
150 /* This is a set of address_entry objects which is used to prevent
151 duplicate symbols from being entered into the result. */
154 /* Are we building a linespec? */
158 /* This is a helper object that is used when collecting symbols into a
163 /* The linespec object in use. */
164 struct linespec_state *state;
166 /* A list of symtabs to which to restrict matches. */
167 VEC (symtab_ptr) *file_symtabs;
169 /* The result being accumulated. */
172 VEC (symbolp) *symbols;
173 VEC (bound_minimal_symbol_d) *minimal_symbols;
176 /* Possibly add a symbol to the results. */
177 bool add_symbol (symbol *sym);
181 collect_info::add_symbol (symbol *sym)
183 /* In list mode, add all matching symbols, regardless of class.
184 This allows the user to type "list a_global_variable". */
185 if (SYMBOL_CLASS (sym) == LOC_BLOCK || this->state->list_mode)
186 VEC_safe_push (symbolp, this->result.symbols, sym);
188 /* Continue iterating. */
199 /* A colon "separator" */
211 /* EOI (end of input) */
217 typedef enum ls_token_type linespec_token_type;
219 /* List of keywords */
221 static const char * const linespec_keywords[] = { "if", "thread", "task" };
222 #define IF_KEYWORD_INDEX 0
224 /* A token of the linespec lexer */
228 /* The type of the token */
229 linespec_token_type type;
231 /* Data for the token */
234 /* A string, given as a stoken */
235 struct stoken string;
241 typedef struct ls_token linespec_token;
243 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
244 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
246 /* An instance of the linespec parser. */
250 /* Lexer internal data */
253 /* Save head of input stream. */
254 const char *saved_arg;
256 /* Head of the input stream. */
258 #define PARSER_STREAM(P) ((P)->lexer.stream)
260 /* The current token. */
261 linespec_token current;
264 /* Is the entire linespec quote-enclosed? */
265 int is_quote_enclosed;
267 /* The state of the parse. */
268 struct linespec_state state;
269 #define PARSER_STATE(PPTR) (&(PPTR)->state)
271 /* The result of the parse. */
272 struct linespec result;
273 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
275 typedef struct ls_parser linespec_parser;
277 /* A convenience macro for accessing the explicit location result of
279 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
281 /* Prototypes for local functions. */
283 static void iterate_over_file_blocks
284 (struct symtab *symtab, const char *name, domain_enum domain,
285 gdb::function_view<symbol_found_callback_ftype> callback);
287 static void initialize_defaults (struct symtab **default_symtab,
290 CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
292 static struct symtabs_and_lines decode_objc (struct linespec_state *self,
296 static VEC (symtab_ptr) *symtabs_from_filename (const char *,
297 struct program_space *pspace);
299 static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
300 VEC (symbolp) *function_symbols,
301 VEC (symbolp) **label_funcs_ret,
304 static void find_linespec_symbols (struct linespec_state *self,
305 VEC (symtab_ptr) *file_symtabs,
307 VEC (symbolp) **symbols,
308 VEC (bound_minimal_symbol_d) **minsyms);
310 static struct line_offset
311 linespec_parse_variable (struct linespec_state *self,
312 const char *variable);
314 static int symbol_to_sal (struct symtab_and_line *result,
315 int funfirstline, struct symbol *sym);
317 static void add_matching_symbols_to_info (const char *name,
318 struct collect_info *info,
319 struct program_space *pspace);
321 static void add_all_symbol_names_from_pspace (struct collect_info *info,
322 struct program_space *pspace,
323 VEC (const_char_ptr) *names);
325 static VEC (symtab_ptr) *
326 collect_symtabs_from_filename (const char *file,
327 struct program_space *pspace);
329 static void decode_digits_ordinary (struct linespec_state *self,
332 struct symtabs_and_lines *sals,
333 struct linetable_entry **best_entry);
335 static void decode_digits_list_mode (struct linespec_state *self,
337 struct symtabs_and_lines *values,
338 struct symtab_and_line val);
340 static void minsym_found (struct linespec_state *self, struct objfile *objfile,
341 struct minimal_symbol *msymbol,
342 struct symtabs_and_lines *result);
344 static int compare_symbols (const void *a, const void *b);
346 static int compare_msymbols (const void *a, const void *b);
348 /* Permitted quote characters for the parser. This is different from the
349 completer's quote characters to allow backward compatibility with the
351 static const char *const linespec_quote_characters = "\"\'";
353 /* Lexer functions. */
355 /* Lex a number from the input in PARSER. This only supports
358 Return true if input is decimal numbers. Return false if not. */
361 linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
363 tokenp->type = LSTOKEN_NUMBER;
364 LS_TOKEN_STOKEN (*tokenp).length = 0;
365 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
367 /* Keep any sign at the start of the stream. */
368 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
370 ++LS_TOKEN_STOKEN (*tokenp).length;
371 ++(PARSER_STREAM (parser));
374 while (isdigit (*PARSER_STREAM (parser)))
376 ++LS_TOKEN_STOKEN (*tokenp).length;
377 ++(PARSER_STREAM (parser));
380 /* If the next character in the input buffer is not a space, comma,
381 quote, or colon, this input does not represent a number. */
382 if (*PARSER_STREAM (parser) != '\0'
383 && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
384 && *PARSER_STREAM (parser) != ':'
385 && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
387 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
394 /* See linespec.h. */
397 linespec_lexer_lex_keyword (const char *p)
403 for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i)
405 int len = strlen (linespec_keywords[i]);
407 /* If P begins with one of the keywords and the next
408 character is whitespace, we may have found a keyword.
409 It is only a keyword if it is not followed by another
411 if (strncmp (p, linespec_keywords[i], len) == 0
416 /* Special case: "if" ALWAYS stops the lexer, since it
417 is not possible to predict what is going to appear in
418 the condition, which can only be parsed after SaLs have
420 if (i != IF_KEYWORD_INDEX)
423 p = skip_spaces_const (p);
424 for (j = 0; j < ARRAY_SIZE (linespec_keywords); ++j)
426 int nextlen = strlen (linespec_keywords[j]);
428 if (strncmp (p, linespec_keywords[j], nextlen) == 0
429 && isspace (p[nextlen]))
434 return linespec_keywords[i];
442 /* See description in linespec.h. */
445 is_ada_operator (const char *string)
447 const struct ada_opname_map *mapping;
449 for (mapping = ada_opname_table;
450 mapping->encoded != NULL
451 && !startswith (string, mapping->decoded); ++mapping)
454 return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
457 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
458 the location of QUOTE_CHAR, or NULL if not found. */
461 skip_quote_char (const char *string, char quote_char)
463 const char *p, *last;
465 p = last = find_toplevel_char (string, quote_char);
466 while (p && *p != '\0' && *p != ':')
468 p = find_toplevel_char (p, quote_char);
476 /* Make a writable copy of the string given in TOKEN, trimming
477 any trailing whitespace. */
480 copy_token_string (linespec_token token)
484 if (token.type == LSTOKEN_KEYWORD)
485 return xstrdup (LS_TOKEN_KEYWORD (token));
487 str = savestring (LS_TOKEN_STOKEN (token).ptr,
488 LS_TOKEN_STOKEN (token).length);
489 s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
495 /* Does P represent the end of a quote-enclosed linespec? */
498 is_closing_quote_enclosed (const char *p)
500 if (strchr (linespec_quote_characters, *p))
502 p = skip_spaces ((char *) p);
503 return (*p == '\0' || linespec_lexer_lex_keyword (p));
506 /* Find the end of the parameter list that starts with *INPUT.
507 This helper function assists with lexing string segments
508 which might contain valid (non-terminating) commas. */
511 find_parameter_list_end (const char *input)
513 char end_char, start_char;
518 if (start_char == '(')
520 else if (start_char == '<')
529 if (*p == start_char)
531 else if (*p == end_char)
546 /* Lex a string from the input in PARSER. */
548 static linespec_token
549 linespec_lexer_lex_string (linespec_parser *parser)
551 linespec_token token;
552 const char *start = PARSER_STREAM (parser);
554 token.type = LSTOKEN_STRING;
556 /* If the input stream starts with a quote character, skip to the next
557 quote character, regardless of the content. */
558 if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
561 char quote_char = *PARSER_STREAM (parser);
563 /* Special case: Ada operators. */
564 if (PARSER_STATE (parser)->language->la_language == language_ada
565 && quote_char == '\"')
567 int len = is_ada_operator (PARSER_STREAM (parser));
571 /* The input is an Ada operator. Return the quoted string
573 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
574 LS_TOKEN_STOKEN (token).length = len;
575 PARSER_STREAM (parser) += len;
579 /* The input does not represent an Ada operator -- fall through
580 to normal quoted string handling. */
583 /* Skip past the beginning quote. */
584 ++(PARSER_STREAM (parser));
586 /* Mark the start of the string. */
587 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
589 /* Skip to the ending quote. */
590 end = skip_quote_char (PARSER_STREAM (parser), quote_char);
592 /* Error if the input did not terminate properly. */
594 error (_("unmatched quote"));
596 /* Skip over the ending quote and mark the length of the string. */
597 PARSER_STREAM (parser) = (char *) ++end;
598 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
604 /* Otherwise, only identifier characters are permitted.
605 Spaces are the exception. In general, we keep spaces,
606 but only if the next characters in the input do not resolve
607 to one of the keywords.
609 This allows users to forgo quoting CV-qualifiers, template arguments,
610 and similar common language constructs. */
614 if (isspace (*PARSER_STREAM (parser)))
616 p = skip_spaces_const (PARSER_STREAM (parser));
617 /* When we get here we know we've found something followed by
618 a space (we skip over parens and templates below).
619 So if we find a keyword now, we know it is a keyword and not,
620 say, a function name. */
621 if (linespec_lexer_lex_keyword (p) != NULL)
623 LS_TOKEN_STOKEN (token).ptr = start;
624 LS_TOKEN_STOKEN (token).length
625 = PARSER_STREAM (parser) - start;
629 /* Advance past the whitespace. */
630 PARSER_STREAM (parser) = p;
633 /* If the next character is EOI or (single) ':', the
634 string is complete; return the token. */
635 if (*PARSER_STREAM (parser) == 0)
637 LS_TOKEN_STOKEN (token).ptr = start;
638 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
641 else if (PARSER_STREAM (parser)[0] == ':')
643 /* Do not tokenize the C++ scope operator. */
644 if (PARSER_STREAM (parser)[1] == ':')
645 ++(PARSER_STREAM (parser));
647 /* Do not tokenify if the input length so far is one
648 (i.e, a single-letter drive name) and the next character
649 is a directory separator. This allows Windows-style
650 paths to be recognized as filenames without quoting it. */
651 else if ((PARSER_STREAM (parser) - start) != 1
652 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
654 LS_TOKEN_STOKEN (token).ptr = start;
655 LS_TOKEN_STOKEN (token).length
656 = PARSER_STREAM (parser) - start;
660 /* Special case: permit quote-enclosed linespecs. */
661 else if (parser->is_quote_enclosed
662 && strchr (linespec_quote_characters,
663 *PARSER_STREAM (parser))
664 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
666 LS_TOKEN_STOKEN (token).ptr = start;
667 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
670 /* Because commas may terminate a linespec and appear in
671 the middle of valid string input, special cases for
672 '<' and '(' are necessary. */
673 else if (*PARSER_STREAM (parser) == '<'
674 || *PARSER_STREAM (parser) == '(')
678 p = find_parameter_list_end (PARSER_STREAM (parser));
681 PARSER_STREAM (parser) = p;
685 /* Commas are terminators, but not if they are part of an
687 else if (*PARSER_STREAM (parser) == ',')
689 if ((PARSER_STATE (parser)->language->la_language
691 && (PARSER_STREAM (parser) - start) > 8
692 /* strlen ("operator") */)
694 const char *p = strstr (start, "operator");
696 if (p != NULL && is_operator_name (p))
698 /* This is an operator name. Keep going. */
699 ++(PARSER_STREAM (parser));
704 /* Comma terminates the string. */
705 LS_TOKEN_STOKEN (token).ptr = start;
706 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
710 /* Advance the stream. */
711 ++(PARSER_STREAM (parser));
718 /* Lex a single linespec token from PARSER. */
720 static linespec_token
721 linespec_lexer_lex_one (linespec_parser *parser)
725 if (parser->lexer.current.type == LSTOKEN_CONSUMED)
727 /* Skip any whitespace. */
728 PARSER_STREAM (parser) = skip_spaces_const (PARSER_STREAM (parser));
730 /* Check for a keyword, they end the linespec. */
731 keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
734 parser->lexer.current.type = LSTOKEN_KEYWORD;
735 LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
736 /* We do not advance the stream here intentionally:
737 we would like lexing to stop when a keyword is seen.
739 PARSER_STREAM (parser) += strlen (keyword); */
741 return parser->lexer.current;
744 /* Handle other tokens. */
745 switch (*PARSER_STREAM (parser))
748 parser->lexer.current.type = LSTOKEN_EOI;
752 case '0': case '1': case '2': case '3': case '4':
753 case '5': case '6': case '7': case '8': case '9':
754 if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
755 parser->lexer.current = linespec_lexer_lex_string (parser);
759 /* If we have a scope operator, lex the input as a string.
760 Otherwise, return LSTOKEN_COLON. */
761 if (PARSER_STREAM (parser)[1] == ':')
762 parser->lexer.current = linespec_lexer_lex_string (parser);
765 parser->lexer.current.type = LSTOKEN_COLON;
766 ++(PARSER_STREAM (parser));
770 case '\'': case '\"':
771 /* Special case: permit quote-enclosed linespecs. */
772 if (parser->is_quote_enclosed
773 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
775 ++(PARSER_STREAM (parser));
776 parser->lexer.current.type = LSTOKEN_EOI;
779 parser->lexer.current = linespec_lexer_lex_string (parser);
783 parser->lexer.current.type = LSTOKEN_COMMA;
784 LS_TOKEN_STOKEN (parser->lexer.current).ptr
785 = PARSER_STREAM (parser);
786 LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
787 ++(PARSER_STREAM (parser));
791 /* If the input is not a number, it must be a string.
792 [Keywords were already considered above.] */
793 parser->lexer.current = linespec_lexer_lex_string (parser);
798 return parser->lexer.current;
801 /* Consume the current token and return the next token in PARSER's
804 static linespec_token
805 linespec_lexer_consume_token (linespec_parser *parser)
807 parser->lexer.current.type = LSTOKEN_CONSUMED;
808 return linespec_lexer_lex_one (parser);
811 /* Return the next token without consuming the current token. */
813 static linespec_token
814 linespec_lexer_peek_token (linespec_parser *parser)
817 const char *saved_stream = PARSER_STREAM (parser);
818 linespec_token saved_token = parser->lexer.current;
820 next = linespec_lexer_consume_token (parser);
821 PARSER_STREAM (parser) = saved_stream;
822 parser->lexer.current = saved_token;
826 /* Helper functions. */
828 /* Add SAL to SALS. */
831 add_sal_to_sals_basic (struct symtabs_and_lines *sals,
832 struct symtab_and_line *sal)
835 sals->sals = XRESIZEVEC (struct symtab_and_line, sals->sals, sals->nelts);
836 sals->sals[sals->nelts - 1] = *sal;
839 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
840 the new sal, if needed. If not NULL, SYMNAME is the name of the
841 symbol to use when constructing the new canonical name.
843 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
844 canonical name for the SAL. */
847 add_sal_to_sals (struct linespec_state *self,
848 struct symtabs_and_lines *sals,
849 struct symtab_and_line *sal,
850 const char *symname, int literal_canonical)
852 add_sal_to_sals_basic (sals, sal);
856 struct linespec_canonical_name *canonical;
858 self->canonical_names = XRESIZEVEC (struct linespec_canonical_name,
859 self->canonical_names, sals->nelts);
860 canonical = &self->canonical_names[sals->nelts - 1];
861 if (!literal_canonical && sal->symtab)
863 symtab_to_fullname (sal->symtab);
865 /* Note that the filter doesn't have to be a valid linespec
866 input. We only apply the ":LINE" treatment to Ada for
868 if (symname != NULL && sal->line != 0
869 && self->language->la_language == language_ada)
870 canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
871 else if (symname != NULL)
872 canonical->suffix = xstrdup (symname);
874 canonical->suffix = xstrprintf ("%d", sal->line);
875 canonical->symtab = sal->symtab;
880 canonical->suffix = xstrdup (symname);
882 canonical->suffix = xstrdup ("<unknown>");
883 canonical->symtab = NULL;
888 /* A hash function for address_entry. */
891 hash_address_entry (const void *p)
893 const struct address_entry *aep = (const struct address_entry *) p;
896 hash = iterative_hash_object (aep->pspace, 0);
897 return iterative_hash_object (aep->addr, hash);
900 /* An equality function for address_entry. */
903 eq_address_entry (const void *a, const void *b)
905 const struct address_entry *aea = (const struct address_entry *) a;
906 const struct address_entry *aeb = (const struct address_entry *) b;
908 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
911 /* Check whether the address, represented by PSPACE and ADDR, is
912 already in the set. If so, return 0. Otherwise, add it and return
916 maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
918 struct address_entry e, *p;
923 slot = htab_find_slot (set, &e, INSERT);
927 p = XNEW (struct address_entry);
928 memcpy (p, &e, sizeof (struct address_entry));
934 /* A helper that walks over all matching symtabs in all objfiles and
935 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
936 not NULL, then the search is restricted to just that program
937 space. If INCLUDE_INLINE is true then symbols representing
938 inlined instances of functions will be included in the result. */
941 iterate_over_all_matching_symtabs
942 (struct linespec_state *state, const char *name, const domain_enum domain,
943 struct program_space *search_pspace, bool include_inline,
944 gdb::function_view<symbol_found_callback_ftype> callback)
946 struct objfile *objfile;
947 struct program_space *pspace;
949 /* The routine to be used for comparison. */
950 symbol_name_cmp_ftype symbol_name_cmp
951 = (state->language->la_get_symbol_name_cmp != NULL
952 ? state->language->la_get_symbol_name_cmp (name)
957 if (search_pspace != NULL && search_pspace != pspace)
959 if (pspace->executing_startup)
962 set_current_program_space (pspace);
964 ALL_OBJFILES (objfile)
966 struct compunit_symtab *cu;
969 objfile->sf->qf->expand_symtabs_matching
972 [&] (const char *symbol_name)
974 return symbol_name_cmp (symbol_name, name) == 0;
979 ALL_OBJFILE_COMPUNITS (objfile, cu)
981 struct symtab *symtab = COMPUNIT_FILETABS (cu);
983 iterate_over_file_blocks (symtab, name, domain, callback);
990 for (i = FIRST_LOCAL_BLOCK;
991 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
994 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
995 state->language->la_iterate_over_symbols
996 (block, name, domain, [&] (symbol *sym)
998 /* Restrict calls to CALLBACK to symbols
999 representing inline symbols only. */
1000 if (SYMBOL_INLINED (sym))
1001 return callback (sym);
1011 /* Returns the block to be used for symbol searches from
1012 the current location. */
1014 static const struct block *
1015 get_current_search_block (void)
1017 const struct block *block;
1018 enum language save_language;
1020 /* get_selected_block can change the current language when there is
1021 no selected frame yet. */
1022 save_language = current_language->la_language;
1023 block = get_selected_block (0);
1024 set_language (save_language);
1029 /* Iterate over static and global blocks. */
1032 iterate_over_file_blocks
1033 (struct symtab *symtab, const char *name, domain_enum domain,
1034 gdb::function_view<symbol_found_callback_ftype> callback)
1036 struct block *block;
1038 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
1040 block = BLOCK_SUPERBLOCK (block))
1041 LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback);
1044 /* A helper for find_method. This finds all methods in type T which
1045 match NAME. It adds matching symbol names to RESULT_NAMES, and
1046 adds T's direct superclasses to SUPERCLASSES. */
1049 find_methods (struct type *t, const char *name,
1050 VEC (const_char_ptr) **result_names,
1051 VEC (typep) **superclasses)
1054 const char *class_name = type_name_no_tag (t);
1056 /* Ignore this class if it doesn't have a name. This is ugly, but
1057 unless we figure out how to get the physname without the name of
1058 the class, then the loop can't do any good. */
1063 t = check_typedef (t);
1065 /* Loop over each method name. At this level, all overloads of a name
1066 are counted as a single name. There is an inner loop which loops over
1069 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1070 method_counter >= 0;
1073 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1074 char dem_opname[64];
1076 if (startswith (method_name, "__") ||
1077 startswith (method_name, "op") ||
1078 startswith (method_name, "type"))
1080 if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
1081 method_name = dem_opname;
1082 else if (cplus_demangle_opname (method_name, dem_opname, 0))
1083 method_name = dem_opname;
1086 if (strcmp_iw (method_name, name) == 0)
1090 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1096 const char *phys_name;
1098 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1099 if (TYPE_FN_FIELD_STUB (f, field_counter))
1101 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1102 VEC_safe_push (const_char_ptr, *result_names, phys_name);
1108 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1109 VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
1112 /* Find an instance of the character C in the string S that is outside
1113 of all parenthesis pairs, single-quoted strings, and double-quoted
1114 strings. Also, ignore the char within a template name, like a ','
1115 within foo<int, int>. */
1118 find_toplevel_char (const char *s, char c)
1120 int quoted = 0; /* zero if we're not in quotes;
1121 '"' if we're in a double-quoted string;
1122 '\'' if we're in a single-quoted string. */
1123 int depth = 0; /* Number of unclosed parens we've seen. */
1126 for (scan = s; *scan; scan++)
1130 if (*scan == quoted)
1132 else if (*scan == '\\' && *(scan + 1))
1135 else if (*scan == c && ! quoted && depth == 0)
1137 else if (*scan == '"' || *scan == '\'')
1139 else if (*scan == '(' || *scan == '<')
1141 else if ((*scan == ')' || *scan == '>') && depth > 0)
1148 /* The string equivalent of find_toplevel_char. Returns a pointer
1149 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1150 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1153 find_toplevel_string (const char *haystack, const char *needle)
1155 const char *s = haystack;
1159 s = find_toplevel_char (s, *needle);
1163 /* Found first char in HAYSTACK; check rest of string. */
1164 if (startswith (s, needle))
1167 /* Didn't find it; loop over HAYSTACK, looking for the next
1168 instance of the first character of NEEDLE. */
1172 while (s != NULL && *s != '\0');
1174 /* NEEDLE was not found in HAYSTACK. */
1178 /* Convert CANONICAL to its string representation using
1179 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1182 canonical_to_fullform (const struct linespec_canonical_name *canonical)
1184 if (canonical->symtab == NULL)
1185 return xstrdup (canonical->suffix);
1187 return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
1191 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1192 and store the result in SELF->CANONICAL. */
1195 filter_results (struct linespec_state *self,
1196 struct symtabs_and_lines *result,
1197 VEC (const_char_ptr) *filters)
1202 for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
1204 struct linespec_sals lsal;
1207 memset (&lsal, 0, sizeof (lsal));
1209 for (j = 0; j < result->nelts; ++j)
1211 const struct linespec_canonical_name *canonical;
1213 struct cleanup *cleanup;
1215 canonical = &self->canonical_names[j];
1216 fullform = canonical_to_fullform (canonical);
1217 cleanup = make_cleanup (xfree, fullform);
1219 if (strcmp (name, fullform) == 0)
1220 add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);
1222 do_cleanups (cleanup);
1225 if (lsal.sals.nelts > 0)
1227 lsal.canonical = xstrdup (name);
1228 VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
1232 self->canonical->pre_expanded = 0;
1235 /* Store RESULT into SELF->CANONICAL. */
1238 convert_results_to_lsals (struct linespec_state *self,
1239 struct symtabs_and_lines *result)
1241 struct linespec_sals lsal;
1243 lsal.canonical = NULL;
1244 lsal.sals = *result;
1245 VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
1248 /* A structure that contains two string representations of a struct
1249 linespec_canonical_name:
1250 - one where the the symtab's fullname is used;
1251 - one where the filename followed the "set filename-display"
1254 struct decode_line_2_item
1256 /* The form using symtab_to_fullname.
1257 It must be xfree'ed after use. */
1260 /* The form using symtab_to_filename_for_display.
1261 It must be xfree'ed after use. */
1264 /* Field is initialized to zero and it is set to one if the user
1265 requested breakpoint for this entry. */
1266 unsigned int selected : 1;
1269 /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1270 secondarily by FULLFORM. */
1273 decode_line_2_compare_items (const void *ap, const void *bp)
1275 const struct decode_line_2_item *a = (const struct decode_line_2_item *) ap;
1276 const struct decode_line_2_item *b = (const struct decode_line_2_item *) bp;
1279 retval = strcmp (a->displayform, b->displayform);
1283 return strcmp (a->fullform, b->fullform);
1286 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1287 will either return normally, throw an exception on multiple
1288 results, or present a menu to the user. On return, the SALS vector
1289 in SELF->CANONICAL is set up properly. */
1292 decode_line_2 (struct linespec_state *self,
1293 struct symtabs_and_lines *result,
1294 const char *select_mode)
1299 struct cleanup *old_chain;
1300 VEC (const_char_ptr) *filters = NULL;
1301 struct decode_line_2_item *items;
1304 gdb_assert (select_mode != multiple_symbols_all);
1305 gdb_assert (self->canonical != NULL);
1306 gdb_assert (result->nelts >= 1);
1308 old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
1310 /* Prepare ITEMS array. */
1311 items_count = result->nelts;
1312 items = XNEWVEC (struct decode_line_2_item, items_count);
1313 make_cleanup (xfree, items);
1314 for (i = 0; i < items_count; ++i)
1316 const struct linespec_canonical_name *canonical;
1317 struct decode_line_2_item *item;
1319 canonical = &self->canonical_names[i];
1320 gdb_assert (canonical->suffix != NULL);
1323 item->fullform = canonical_to_fullform (canonical);
1324 make_cleanup (xfree, item->fullform);
1326 if (canonical->symtab == NULL)
1327 item->displayform = canonical->suffix;
1330 const char *fn_for_display;
1332 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
1333 item->displayform = xstrprintf ("%s:%s", fn_for_display,
1335 make_cleanup (xfree, item->displayform);
1341 /* Sort the list of method names. */
1342 qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
1344 /* Remove entries with the same FULLFORM. */
1345 if (items_count >= 2)
1347 struct decode_line_2_item *dst, *src;
1350 for (src = &items[1]; src < &items[items_count]; src++)
1351 if (strcmp (src->fullform, dst->fullform) != 0)
1353 items_count = dst + 1 - items;
1356 if (select_mode == multiple_symbols_cancel && items_count > 1)
1357 error (_("canceled because the command is ambiguous\n"
1358 "See set/show multiple-symbol."));
1360 if (select_mode == multiple_symbols_all || items_count == 1)
1362 do_cleanups (old_chain);
1363 convert_results_to_lsals (self, result);
1367 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1368 for (i = 0; i < items_count; i++)
1369 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);
1371 prompt = getenv ("PS2");
1376 args = command_line_input (prompt, 0, "overload-choice");
1378 if (args == 0 || *args == 0)
1379 error_no_arg (_("one or more choice numbers"));
1381 number_or_range_parser parser (args);
1382 while (!parser.finished ())
1384 int num = parser.get_number ();
1387 error (_("canceled"));
1390 /* We intentionally make this result in a single breakpoint,
1391 contrary to what older versions of gdb did. The
1392 rationale is that this lets a user get the
1393 multiple_symbols_all behavior even with the 'ask'
1394 setting; and he can get separate breakpoints by entering
1395 "2-57" at the query. */
1396 do_cleanups (old_chain);
1397 convert_results_to_lsals (self, result);
1402 if (num >= items_count)
1403 printf_unfiltered (_("No choice number %d.\n"), num);
1406 struct decode_line_2_item *item = &items[num];
1408 if (!item->selected)
1410 VEC_safe_push (const_char_ptr, filters, item->fullform);
1415 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1421 filter_results (self, result, filters);
1422 do_cleanups (old_chain);
1427 /* The parser of linespec itself. */
1429 /* Throw an appropriate error when SYMBOL is not found (optionally in
1432 static void ATTRIBUTE_NORETURN
1433 symbol_not_found_error (const char *symbol, const char *filename)
1438 if (!have_full_symbols ()
1439 && !have_partial_symbols ()
1440 && !have_minimal_symbols ())
1441 throw_error (NOT_FOUND_ERROR,
1442 _("No symbol table is loaded. Use the \"file\" command."));
1444 /* If SYMBOL starts with '$', the user attempted to either lookup
1445 a function/variable in his code starting with '$' or an internal
1446 variable of that name. Since we do not know which, be concise and
1447 explain both possibilities. */
1451 throw_error (NOT_FOUND_ERROR,
1452 _("Undefined convenience variable or function \"%s\" "
1453 "not defined in \"%s\"."), symbol, filename);
1455 throw_error (NOT_FOUND_ERROR,
1456 _("Undefined convenience variable or function \"%s\" "
1457 "not defined."), symbol);
1462 throw_error (NOT_FOUND_ERROR,
1463 _("Function \"%s\" not defined in \"%s\"."),
1466 throw_error (NOT_FOUND_ERROR,
1467 _("Function \"%s\" not defined."), symbol);
1471 /* Throw an appropriate error when an unexpected token is encountered
1474 static void ATTRIBUTE_NORETURN
1475 unexpected_linespec_error (linespec_parser *parser)
1477 linespec_token token;
1478 static const char * token_type_strings[]
1479 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1481 /* Get the token that generated the error. */
1482 token = linespec_lexer_lex_one (parser);
1484 /* Finally, throw the error. */
1485 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1486 || token.type == LSTOKEN_KEYWORD)
1490 string = copy_token_string (token);
1491 make_cleanup (xfree, string);
1492 throw_error (GENERIC_ERROR,
1493 _("malformed linespec error: unexpected %s, \"%s\""),
1494 token_type_strings[token.type], string);
1497 throw_error (GENERIC_ERROR,
1498 _("malformed linespec error: unexpected %s"),
1499 token_type_strings[token.type]);
1502 /* Throw an undefined label error. */
1504 static void ATTRIBUTE_NORETURN
1505 undefined_label_error (const char *function, const char *label)
1507 if (function != NULL)
1508 throw_error (NOT_FOUND_ERROR,
1509 _("No label \"%s\" defined in function \"%s\"."),
1512 throw_error (NOT_FOUND_ERROR,
1513 _("No label \"%s\" defined in current function."),
1517 /* Throw a source file not found error. */
1519 static void ATTRIBUTE_NORETURN
1520 source_file_not_found_error (const char *name)
1522 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1525 /* See description in linespec.h. */
1528 linespec_parse_line_offset (const char *string)
1530 const char *start = string;
1531 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1535 line_offset.sign = LINE_OFFSET_PLUS;
1538 else if (*string == '-')
1540 line_offset.sign = LINE_OFFSET_MINUS;
1544 if (*string != '\0' && !isdigit (*string))
1545 error (_("malformed line offset: \"%s\""), start);
1547 /* Right now, we only allow base 10 for offsets. */
1548 line_offset.offset = atoi (string);
1552 /* Parse the basic_spec in PARSER's input. */
1555 linespec_parse_basic (linespec_parser *parser)
1558 linespec_token token;
1559 VEC (symbolp) *symbols, *labels;
1560 VEC (bound_minimal_symbol_d) *minimal_symbols;
1561 struct cleanup *cleanup;
1563 /* Get the next token. */
1564 token = linespec_lexer_lex_one (parser);
1566 /* If it is EOI or KEYWORD, issue an error. */
1567 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1568 unexpected_linespec_error (parser);
1569 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1570 else if (token.type == LSTOKEN_NUMBER)
1572 /* Record the line offset and get the next token. */
1573 name = copy_token_string (token);
1574 cleanup = make_cleanup (xfree, name);
1575 PARSER_EXPLICIT (parser)->line_offset = linespec_parse_line_offset (name);
1576 do_cleanups (cleanup);
1578 /* Get the next token. */
1579 token = linespec_lexer_consume_token (parser);
1581 /* If the next token is a comma, stop parsing and return. */
1582 if (token.type == LSTOKEN_COMMA)
1585 /* If the next token is anything but EOI or KEYWORD, issue
1587 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1588 unexpected_linespec_error (parser);
1591 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1594 /* Next token must be LSTOKEN_STRING. */
1595 if (token.type != LSTOKEN_STRING)
1596 unexpected_linespec_error (parser);
1598 /* The current token will contain the name of a function, method,
1600 name = copy_token_string (token);
1601 cleanup = make_cleanup (xfree, name);
1603 /* Try looking it up as a function/method. */
1604 find_linespec_symbols (PARSER_STATE (parser),
1605 PARSER_RESULT (parser)->file_symtabs, name,
1606 &symbols, &minimal_symbols);
1608 if (symbols != NULL || minimal_symbols != NULL)
1610 PARSER_RESULT (parser)->function_symbols = symbols;
1611 PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
1612 PARSER_EXPLICIT (parser)->function_name = name;
1614 discard_cleanups (cleanup);
1618 /* NAME was not a function or a method. So it must be a label
1619 name or user specified variable like "break foo.c:$zippo". */
1620 labels = find_label_symbols (PARSER_STATE (parser), NULL,
1624 PARSER_RESULT (parser)->labels.label_symbols = labels;
1625 PARSER_RESULT (parser)->labels.function_symbols = symbols;
1626 PARSER_EXPLICIT (parser)->label_name = name;
1628 discard_cleanups (cleanup);
1630 else if (token.type == LSTOKEN_STRING
1631 && *LS_TOKEN_STOKEN (token).ptr == '$')
1633 /* User specified a convenience variable or history value. */
1634 PARSER_EXPLICIT (parser)->line_offset
1635 = linespec_parse_variable (PARSER_STATE (parser), name);
1637 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
1639 /* The user-specified variable was not valid. Do not
1640 throw an error here. parse_linespec will do it for us. */
1641 PARSER_EXPLICIT (parser)->function_name = name;
1642 discard_cleanups (cleanup);
1646 /* The convenience variable/history value parsed correctly.
1647 NAME is no longer needed. */
1648 do_cleanups (cleanup);
1652 /* The name is also not a label. Abort parsing. Do not throw
1653 an error here. parse_linespec will do it for us. */
1655 /* Save a copy of the name we were trying to lookup. */
1656 PARSER_EXPLICIT (parser)->function_name = name;
1657 discard_cleanups (cleanup);
1662 /* Get the next token. */
1663 token = linespec_lexer_consume_token (parser);
1665 if (token.type == LSTOKEN_COLON)
1667 /* User specified a label or a lineno. */
1668 token = linespec_lexer_consume_token (parser);
1670 if (token.type == LSTOKEN_NUMBER)
1672 /* User specified an offset. Record the line offset and
1673 get the next token. */
1674 name = copy_token_string (token);
1675 cleanup = make_cleanup (xfree, name);
1676 PARSER_EXPLICIT (parser)->line_offset
1677 = linespec_parse_line_offset (name);
1678 do_cleanups (cleanup);
1680 /* Ge the next token. */
1681 token = linespec_lexer_consume_token (parser);
1683 else if (token.type == LSTOKEN_STRING)
1685 /* Grab a copy of the label's name and look it up. */
1686 name = copy_token_string (token);
1687 cleanup = make_cleanup (xfree, name);
1688 labels = find_label_symbols (PARSER_STATE (parser),
1689 PARSER_RESULT (parser)->function_symbols,
1694 PARSER_RESULT (parser)->labels.label_symbols = labels;
1695 PARSER_RESULT (parser)->labels.function_symbols = symbols;
1696 PARSER_EXPLICIT (parser)->label_name = name;
1698 discard_cleanups (cleanup);
1702 /* We don't know what it was, but it isn't a label. */
1703 undefined_label_error (PARSER_EXPLICIT (parser)->function_name,
1707 /* Check for a line offset. */
1708 token = linespec_lexer_consume_token (parser);
1709 if (token.type == LSTOKEN_COLON)
1711 /* Get the next token. */
1712 token = linespec_lexer_consume_token (parser);
1714 /* It must be a line offset. */
1715 if (token.type != LSTOKEN_NUMBER)
1716 unexpected_linespec_error (parser);
1718 /* Record the lione offset and get the next token. */
1719 name = copy_token_string (token);
1720 cleanup = make_cleanup (xfree, name);
1722 PARSER_EXPLICIT (parser)->line_offset
1723 = linespec_parse_line_offset (name);
1724 do_cleanups (cleanup);
1726 /* Get the next token. */
1727 token = linespec_lexer_consume_token (parser);
1732 /* Trailing ':' in the input. Issue an error. */
1733 unexpected_linespec_error (parser);
1738 /* Canonicalize the linespec contained in LS. The result is saved into
1739 STATE->canonical. This function handles both linespec and explicit
1743 canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
1745 struct event_location *canon;
1746 struct explicit_location *explicit_loc;
1748 /* If canonicalization was not requested, no need to do anything. */
1749 if (!state->canonical)
1752 /* Save everything as an explicit location. */
1753 canon = state->canonical->location
1754 = new_explicit_location (&ls->explicit_loc).release ();
1755 explicit_loc = get_explicit_location (canon);
1757 if (explicit_loc->label_name != NULL)
1759 state->canonical->special_display = 1;
1761 if (explicit_loc->function_name == NULL)
1765 /* No function was specified, so add the symbol name. */
1766 gdb_assert (ls->labels.function_symbols != NULL
1767 && (VEC_length (symbolp, ls->labels.function_symbols)
1769 s = VEC_index (symbolp, ls->labels.function_symbols, 0);
1770 explicit_loc->function_name = xstrdup (SYMBOL_NATURAL_NAME (s));
1774 /* If this location originally came from a linespec, save a string
1775 representation of it for display and saving to file. */
1776 if (state->is_linespec)
1778 char *linespec = explicit_location_to_linespec (explicit_loc);
1780 set_event_location_string (canon, linespec);
1785 /* Given a line offset in LS, construct the relevant SALs. */
1787 static struct symtabs_and_lines
1788 create_sals_line_offset (struct linespec_state *self,
1791 struct symtabs_and_lines values;
1792 struct symtab_and_line val;
1793 int use_default = 0;
1799 /* This is where we need to make sure we have good defaults.
1800 We must guarantee that this section of code is never executed
1801 when we are called with just a function name, since
1802 set_default_source_symtab_and_line uses
1803 select_source_symtab that calls us with such an argument. */
1805 if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
1806 && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
1808 const char *fullname;
1810 set_current_program_space (self->program_space);
1812 /* Make sure we have at least a default source line. */
1813 set_default_source_symtab_and_line ();
1814 initialize_defaults (&self->default_symtab, &self->default_line);
1815 fullname = symtab_to_fullname (self->default_symtab);
1816 VEC_pop (symtab_ptr, ls->file_symtabs);
1817 VEC_free (symtab_ptr, ls->file_symtabs);
1818 ls->file_symtabs = collect_symtabs_from_filename (fullname,
1819 self->search_pspace);
1823 val.line = ls->explicit_loc.line_offset.offset;
1824 switch (ls->explicit_loc.line_offset.sign)
1826 case LINE_OFFSET_PLUS:
1827 if (ls->explicit_loc.line_offset.offset == 0)
1830 val.line = self->default_line + val.line;
1833 case LINE_OFFSET_MINUS:
1834 if (ls->explicit_loc.line_offset.offset == 0)
1837 val.line = self->default_line - val.line;
1839 val.line = -val.line;
1842 case LINE_OFFSET_NONE:
1843 break; /* No need to adjust val.line. */
1846 if (self->list_mode)
1847 decode_digits_list_mode (self, ls, &values, val);
1850 struct linetable_entry *best_entry = NULL;
1852 const struct block **blocks;
1853 struct cleanup *cleanup;
1854 struct symtabs_and_lines intermediate_results;
1857 intermediate_results.sals = NULL;
1858 intermediate_results.nelts = 0;
1860 decode_digits_ordinary (self, ls, val.line, &intermediate_results,
1862 if (intermediate_results.nelts == 0 && best_entry != NULL)
1863 decode_digits_ordinary (self, ls, best_entry->line,
1864 &intermediate_results, &best_entry);
1866 cleanup = make_cleanup (xfree, intermediate_results.sals);
1868 /* For optimized code, the compiler can scatter one source line
1869 across disjoint ranges of PC values, even when no duplicate
1870 functions or inline functions are involved. For example,
1871 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
1872 function can result in two PC ranges. In this case, we don't
1873 want to set a breakpoint on the first PC of each range. To filter
1874 such cases, we use containing blocks -- for each PC found
1875 above, we see if there are other PCs that are in the same
1876 block. If yes, the other PCs are filtered out. */
1878 filter = XNEWVEC (int, intermediate_results.nelts);
1879 make_cleanup (xfree, filter);
1880 blocks = XNEWVEC (const struct block *, intermediate_results.nelts);
1881 make_cleanup (xfree, blocks);
1883 for (i = 0; i < intermediate_results.nelts; ++i)
1885 set_current_program_space (intermediate_results.sals[i].pspace);
1888 blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
1889 intermediate_results.sals[i].section);
1892 for (i = 0; i < intermediate_results.nelts; ++i)
1894 if (blocks[i] != NULL)
1895 for (j = i + 1; j < intermediate_results.nelts; ++j)
1897 if (blocks[j] == blocks[i])
1905 for (i = 0; i < intermediate_results.nelts; ++i)
1908 struct symbol *sym = (blocks[i]
1909 ? block_containing_function (blocks[i])
1912 if (self->funfirstline)
1913 skip_prologue_sal (&intermediate_results.sals[i]);
1914 /* Make sure the line matches the request, not what was
1916 intermediate_results.sals[i].line = val.line;
1917 add_sal_to_sals (self, &values, &intermediate_results.sals[i],
1918 sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
1921 do_cleanups (cleanup);
1924 if (values.nelts == 0)
1926 if (ls->explicit_loc.source_filename)
1927 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
1928 val.line, ls->explicit_loc.source_filename);
1930 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
1937 /* Convert the given ADDRESS into SaLs. */
1939 static struct symtabs_and_lines
1940 convert_address_location_to_sals (struct linespec_state *self,
1943 struct symtab_and_line sal;
1944 struct symtabs_and_lines sals = {NULL, 0};
1946 sal = find_pc_line (address, 0);
1948 sal.section = find_pc_overlay (address);
1949 sal.explicit_pc = 1;
1950 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
1955 /* Create and return SALs from the linespec LS. */
1957 static struct symtabs_and_lines
1958 convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
1960 struct symtabs_and_lines sals = {NULL, 0};
1962 if (ls->labels.label_symbols != NULL)
1964 /* We have just a bunch of functions/methods or labels. */
1966 struct symtab_and_line sal;
1969 for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
1971 struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));
1973 if (symbol_to_sal (&sal, state->funfirstline, sym)
1974 && maybe_add_address (state->addr_set, pspace, sal.pc))
1975 add_sal_to_sals (state, &sals, &sal,
1976 SYMBOL_NATURAL_NAME (sym), 0);
1979 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
1981 /* We have just a bunch of functions and/or methods. */
1983 struct symtab_and_line sal;
1985 bound_minimal_symbol_d *elem;
1986 struct program_space *pspace;
1988 if (ls->function_symbols != NULL)
1990 /* Sort symbols so that symbols with the same program space are next
1992 qsort (VEC_address (symbolp, ls->function_symbols),
1993 VEC_length (symbolp, ls->function_symbols),
1994 sizeof (symbolp), compare_symbols);
1996 for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
1998 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
1999 set_current_program_space (pspace);
2000 if (symbol_to_sal (&sal, state->funfirstline, sym)
2001 && maybe_add_address (state->addr_set, pspace, sal.pc))
2002 add_sal_to_sals (state, &sals, &sal,
2003 SYMBOL_NATURAL_NAME (sym), 0);
2007 if (ls->minimal_symbols != NULL)
2009 /* Sort minimal symbols by program space, too. */
2010 qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
2011 VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
2012 sizeof (bound_minimal_symbol_d), compare_msymbols);
2015 VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
2019 pspace = elem->objfile->pspace;
2020 set_current_program_space (pspace);
2021 minsym_found (state, elem->objfile, elem->minsym, &sals);
2025 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
2027 /* Only an offset was specified. */
2028 sals = create_sals_line_offset (state, ls);
2030 /* Make sure we have a filename for canonicalization. */
2031 if (ls->explicit_loc.source_filename == NULL)
2033 const char *fullname = symtab_to_fullname (state->default_symtab);
2035 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2036 form so that displaying SOURCE_FILENAME can follow the current
2037 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2038 it has been kept for code simplicity only in absolute form. */
2039 ls->explicit_loc.source_filename = xstrdup (fullname);
2044 /* We haven't found any results... */
2048 canonicalize_linespec (state, ls);
2050 if (sals.nelts > 0 && state->canonical != NULL)
2051 state->canonical->pre_expanded = 1;
2056 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2058 static struct symtabs_and_lines
2059 convert_explicit_location_to_sals (struct linespec_state *self,
2061 const struct explicit_location *explicit_loc)
2063 VEC (symbolp) *symbols, *labels;
2064 VEC (bound_minimal_symbol_d) *minimal_symbols;
2066 if (explicit_loc->source_filename != NULL)
2070 result->file_symtabs
2071 = symtabs_from_filename (explicit_loc->source_filename,
2072 self->search_pspace);
2074 CATCH (except, RETURN_MASK_ERROR)
2076 source_file_not_found_error (explicit_loc->source_filename);
2079 result->explicit_loc.source_filename
2080 = xstrdup (explicit_loc->source_filename);
2084 /* A NULL entry means to use the default symtab. */
2085 VEC_safe_push (symtab_ptr, result->file_symtabs, NULL);
2088 if (explicit_loc->function_name != NULL)
2090 find_linespec_symbols (self, result->file_symtabs,
2091 explicit_loc->function_name, &symbols,
2094 if (symbols == NULL && minimal_symbols == NULL)
2095 symbol_not_found_error (explicit_loc->function_name,
2096 result->explicit_loc.source_filename);
2098 result->explicit_loc.function_name
2099 = xstrdup (explicit_loc->function_name);
2100 result->function_symbols = symbols;
2101 result->minimal_symbols = minimal_symbols;
2104 if (explicit_loc->label_name != NULL)
2107 labels = find_label_symbols (self, result->function_symbols,
2108 &symbols, explicit_loc->label_name);
2111 undefined_label_error (result->explicit_loc.function_name,
2112 explicit_loc->label_name);
2114 result->explicit_loc.label_name = xstrdup (explicit_loc->label_name);
2115 result->labels.label_symbols = labels;
2116 result->labels.function_symbols = symbols;
2119 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
2120 result->explicit_loc.line_offset = explicit_loc->line_offset;
2122 return convert_linespec_to_sals (self, result);
2125 /* Parse a string that specifies a linespec.
2127 The basic grammar of linespecs:
2129 linespec -> var_spec | basic_spec
2130 var_spec -> '$' (STRING | NUMBER)
2132 basic_spec -> file_offset_spec | function_spec | label_spec
2133 file_offset_spec -> opt_file_spec offset_spec
2134 function_spec -> opt_file_spec function_name_spec opt_label_spec
2135 label_spec -> label_name_spec
2137 opt_file_spec -> "" | file_name_spec ':'
2138 opt_label_spec -> "" | ':' label_name_spec
2140 file_name_spec -> STRING
2141 function_name_spec -> STRING
2142 label_name_spec -> STRING
2143 function_name_spec -> STRING
2144 offset_spec -> NUMBER
2148 This may all be followed by several keywords such as "if EXPR",
2151 A comma will terminate parsing.
2153 The function may be an undebuggable function found in minimal symbol table.
2155 If the argument FUNFIRSTLINE is nonzero, we want the first line
2156 of real code inside a function when a function is specified, and it is
2157 not OK to specify a variable or type to get its line number.
2159 DEFAULT_SYMTAB specifies the file to use if none is specified.
2160 It defaults to current_source_symtab.
2161 DEFAULT_LINE specifies the line number to use for relative
2162 line numbers (that start with signs). Defaults to current_source_line.
2163 If CANONICAL is non-NULL, store an array of strings containing the canonical
2164 line specs there if necessary. Currently overloaded member functions and
2165 line numbers or static functions without a filename yield a canonical
2166 line spec. The array and the line spec strings are allocated on the heap,
2167 it is the callers responsibility to free them.
2169 Note that it is possible to return zero for the symtab
2170 if no file is validly specified. Callers must check that.
2171 Also, the line number returned may be invalid. */
2173 /* Parse the linespec in ARG. */
2175 static struct symtabs_and_lines
2176 parse_linespec (linespec_parser *parser, const char *arg)
2178 linespec_token token;
2179 struct symtabs_and_lines values;
2180 struct gdb_exception file_exception = exception_none;
2181 struct cleanup *cleanup;
2183 /* A special case to start. It has become quite popular for
2184 IDEs to work around bugs in the previous parser by quoting
2185 the entire linespec, so we attempt to deal with this nicely. */
2186 parser->is_quote_enclosed = 0;
2187 if (!is_ada_operator (arg)
2188 && strchr (linespec_quote_characters, *arg) != NULL)
2192 end = skip_quote_char (arg + 1, *arg);
2193 if (end != NULL && is_closing_quote_enclosed (end))
2195 /* Here's the special case. Skip ARG past the initial
2198 parser->is_quote_enclosed = 1;
2202 parser->lexer.saved_arg = arg;
2203 parser->lexer.stream = arg;
2205 /* Initialize the default symtab and line offset. */
2206 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2207 &PARSER_STATE (parser)->default_line);
2209 /* Objective-C shortcut. */
2210 values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2211 if (values.sals != NULL)
2214 /* Start parsing. */
2216 /* Get the first token. */
2217 token = linespec_lexer_lex_one (parser);
2219 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2220 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
2224 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2225 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
2227 /* User specified a convenience variable or history value. */
2228 var = copy_token_string (token);
2229 cleanup = make_cleanup (xfree, var);
2230 PARSER_EXPLICIT (parser)->line_offset
2231 = linespec_parse_variable (PARSER_STATE (parser), var);
2232 do_cleanups (cleanup);
2234 /* If a line_offset wasn't found (VAR is the name of a user
2235 variable/function), then skip to normal symbol processing. */
2236 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
2238 /* Consume this token. */
2239 linespec_lexer_consume_token (parser);
2241 goto convert_to_sals;
2244 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
2245 unexpected_linespec_error (parser);
2247 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2248 this token cannot represent a filename. */
2249 token = linespec_lexer_peek_token (parser);
2251 if (token.type == LSTOKEN_COLON)
2253 char *user_filename;
2255 /* Get the current token again and extract the filename. */
2256 token = linespec_lexer_lex_one (parser);
2257 user_filename = copy_token_string (token);
2259 /* Check if the input is a filename. */
2262 PARSER_RESULT (parser)->file_symtabs
2263 = symtabs_from_filename (user_filename,
2264 PARSER_STATE (parser)->search_pspace);
2266 CATCH (ex, RETURN_MASK_ERROR)
2268 file_exception = ex;
2272 if (file_exception.reason >= 0)
2274 /* Symtabs were found for the file. Record the filename. */
2275 PARSER_EXPLICIT (parser)->source_filename = user_filename;
2277 /* Get the next token. */
2278 token = linespec_lexer_consume_token (parser);
2280 /* This is LSTOKEN_COLON; consume it. */
2281 linespec_lexer_consume_token (parser);
2285 /* No symtabs found -- discard user_filename. */
2286 xfree (user_filename);
2288 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2289 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
2292 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2293 else if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2294 && token.type != LSTOKEN_COMMA)
2296 /* TOKEN is the _next_ token, not the one currently in the parser.
2297 Consuming the token will give the correct error message. */
2298 linespec_lexer_consume_token (parser);
2299 unexpected_linespec_error (parser);
2303 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2304 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
2307 /* Parse the rest of the linespec. */
2308 linespec_parse_basic (parser);
2310 if (PARSER_RESULT (parser)->function_symbols == NULL
2311 && PARSER_RESULT (parser)->labels.label_symbols == NULL
2312 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
2313 && PARSER_RESULT (parser)->minimal_symbols == NULL)
2315 /* The linespec didn't parse. Re-throw the file exception if
2317 if (file_exception.reason < 0)
2318 throw_exception (file_exception);
2320 /* Otherwise, the symbol is not found. */
2321 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2322 PARSER_EXPLICIT (parser)->source_filename);
2327 /* Get the last token and record how much of the input was parsed,
2329 token = linespec_lexer_lex_one (parser);
2330 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
2331 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (token).ptr;
2333 /* Convert the data in PARSER_RESULT to SALs. */
2334 values = convert_linespec_to_sals (PARSER_STATE (parser),
2335 PARSER_RESULT (parser));
2341 /* A constructor for linespec_state. */
2344 linespec_state_constructor (struct linespec_state *self,
2345 int flags, const struct language_defn *language,
2346 struct program_space *search_pspace,
2347 struct symtab *default_symtab,
2349 struct linespec_result *canonical)
2351 memset (self, 0, sizeof (*self));
2352 self->language = language;
2353 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2354 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
2355 self->search_pspace = search_pspace;
2356 self->default_symtab = default_symtab;
2357 self->default_line = default_line;
2358 self->canonical = canonical;
2359 self->program_space = current_program_space;
2360 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2361 xfree, xcalloc, xfree);
2362 self->is_linespec = 0;
2365 /* Initialize a new linespec parser. */
2368 linespec_parser_new (linespec_parser *parser,
2369 int flags, const struct language_defn *language,
2370 struct program_space *search_pspace,
2371 struct symtab *default_symtab,
2373 struct linespec_result *canonical)
2375 memset (parser, 0, sizeof (linespec_parser));
2376 parser->lexer.current.type = LSTOKEN_CONSUMED;
2377 memset (PARSER_RESULT (parser), 0, sizeof (struct linespec));
2378 PARSER_EXPLICIT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN;
2379 linespec_state_constructor (PARSER_STATE (parser), flags, language,
2381 default_symtab, default_line, canonical);
2384 /* A destructor for linespec_state. */
2387 linespec_state_destructor (struct linespec_state *self)
2389 htab_delete (self->addr_set);
2392 /* Delete a linespec parser. */
2395 linespec_parser_delete (void *arg)
2397 linespec_parser *parser = (linespec_parser *) arg;
2399 xfree (PARSER_EXPLICIT (parser)->source_filename);
2400 xfree (PARSER_EXPLICIT (parser)->label_name);
2401 xfree (PARSER_EXPLICIT (parser)->function_name);
2403 if (PARSER_RESULT (parser)->file_symtabs != NULL)
2404 VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);
2406 if (PARSER_RESULT (parser)->function_symbols != NULL)
2407 VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);
2409 if (PARSER_RESULT (parser)->minimal_symbols != NULL)
2410 VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);
2412 if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
2413 VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);
2415 if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
2416 VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);
2418 linespec_state_destructor (PARSER_STATE (parser));
2421 /* See description in linespec.h. */
2424 linespec_lex_to_end (char **stringp)
2426 linespec_parser parser;
2427 struct cleanup *cleanup;
2428 linespec_token token;
2431 if (stringp == NULL || *stringp == NULL)
2434 linespec_parser_new (&parser, 0, current_language, NULL, NULL, 0, NULL);
2435 cleanup = make_cleanup (linespec_parser_delete, &parser);
2436 parser.lexer.saved_arg = *stringp;
2437 PARSER_STREAM (&parser) = orig = *stringp;
2441 /* Stop before any comma tokens; we need it to keep it
2442 as the next token in the string. */
2443 token = linespec_lexer_peek_token (&parser);
2444 if (token.type == LSTOKEN_COMMA)
2446 token = linespec_lexer_consume_token (&parser);
2448 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2450 *stringp += PARSER_STREAM (&parser) - orig;
2451 do_cleanups (cleanup);
2454 /* A helper function for decode_line_full and decode_line_1 to
2455 turn LOCATION into symtabs_and_lines. */
2457 static struct symtabs_and_lines
2458 event_location_to_sals (linespec_parser *parser,
2459 const struct event_location *location)
2461 struct symtabs_and_lines result = {NULL, 0};
2463 switch (event_location_type (location))
2465 case LINESPEC_LOCATION:
2467 PARSER_STATE (parser)->is_linespec = 1;
2470 result = parse_linespec (parser, get_linespec_location (location));
2472 CATCH (except, RETURN_MASK_ERROR)
2474 throw_exception (except);
2480 case ADDRESS_LOCATION:
2482 const char *addr_string = get_address_string_location (location);
2483 CORE_ADDR addr = get_address_location (location);
2485 if (addr_string != NULL)
2487 char *expr = xstrdup (addr_string);
2488 const char *const_expr = expr;
2489 struct cleanup *cleanup = make_cleanup (xfree, expr);
2491 addr = linespec_expression_to_pc (&const_expr);
2492 if (PARSER_STATE (parser)->canonical != NULL)
2493 PARSER_STATE (parser)->canonical->location
2494 = copy_event_location (location).release ();
2496 do_cleanups (cleanup);
2499 result = convert_address_location_to_sals (PARSER_STATE (parser),
2504 case EXPLICIT_LOCATION:
2506 const struct explicit_location *explicit_loc;
2508 explicit_loc = get_explicit_location_const (location);
2509 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
2510 PARSER_RESULT (parser),
2515 case PROBE_LOCATION:
2516 /* Probes are handled by their own decoders. */
2517 gdb_assert_not_reached ("attempt to decode probe location");
2521 gdb_assert_not_reached ("unhandled event location type");
2527 /* See linespec.h. */
2530 decode_line_full (const struct event_location *location, int flags,
2531 struct program_space *search_pspace,
2532 struct symtab *default_symtab,
2533 int default_line, struct linespec_result *canonical,
2534 const char *select_mode,
2537 struct symtabs_and_lines result;
2538 struct cleanup *cleanups;
2539 VEC (const_char_ptr) *filters = NULL;
2540 linespec_parser parser;
2541 struct linespec_state *state;
2543 gdb_assert (canonical != NULL);
2544 /* The filter only makes sense for 'all'. */
2545 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
2546 gdb_assert (select_mode == NULL
2547 || select_mode == multiple_symbols_all
2548 || select_mode == multiple_symbols_ask
2549 || select_mode == multiple_symbols_cancel);
2550 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
2552 linespec_parser_new (&parser, flags, current_language,
2553 search_pspace, default_symtab,
2554 default_line, canonical);
2555 cleanups = make_cleanup (linespec_parser_delete, &parser);
2556 save_current_program_space ();
2558 result = event_location_to_sals (&parser, location);
2559 state = PARSER_STATE (&parser);
2561 gdb_assert (result.nelts == 1 || canonical->pre_expanded);
2562 canonical->pre_expanded = 1;
2564 /* Arrange for allocated canonical names to be freed. */
2565 if (result.nelts > 0)
2569 make_cleanup (xfree, state->canonical_names);
2570 for (i = 0; i < result.nelts; ++i)
2572 gdb_assert (state->canonical_names[i].suffix != NULL);
2573 make_cleanup (xfree, state->canonical_names[i].suffix);
2577 if (select_mode == NULL)
2579 if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ())
2580 select_mode = multiple_symbols_all;
2582 select_mode = multiple_symbols_select_mode ();
2585 if (select_mode == multiple_symbols_all)
2589 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
2590 VEC_safe_push (const_char_ptr, filters, filter);
2591 filter_results (state, &result, filters);
2594 convert_results_to_lsals (state, &result);
2597 decode_line_2 (state, &result, select_mode);
2599 do_cleanups (cleanups);
2602 /* See linespec.h. */
2604 struct symtabs_and_lines
2605 decode_line_1 (const struct event_location *location, int flags,
2606 struct program_space *search_pspace,
2607 struct symtab *default_symtab,
2610 struct symtabs_and_lines result;
2611 linespec_parser parser;
2612 struct cleanup *cleanups;
2614 linespec_parser_new (&parser, flags, current_language,
2615 search_pspace, default_symtab,
2616 default_line, NULL);
2617 cleanups = make_cleanup (linespec_parser_delete, &parser);
2618 save_current_program_space ();
2620 result = event_location_to_sals (&parser, location);
2622 do_cleanups (cleanups);
2626 /* See linespec.h. */
2628 struct symtabs_and_lines
2629 decode_line_with_current_source (char *string, int flags)
2631 struct symtabs_and_lines sals;
2632 struct symtab_and_line cursal;
2635 error (_("Empty line specification."));
2637 /* We use whatever is set as the current source line. We do not try
2638 and get a default source symtab+line or it will recursively call us! */
2639 cursal = get_current_source_symtab_and_line ();
2641 event_location_up location = string_to_event_location (&string,
2643 sals = decode_line_1 (location.get (), flags, NULL,
2644 cursal.symtab, cursal.line);
2647 error (_("Junk at end of line specification: %s"), string);
2652 /* See linespec.h. */
2654 struct symtabs_and_lines
2655 decode_line_with_last_displayed (char *string, int flags)
2657 struct symtabs_and_lines sals;
2660 error (_("Empty line specification."));
2662 event_location_up location = string_to_event_location (&string,
2664 if (last_displayed_sal_is_valid ())
2665 sals = decode_line_1 (location.get (), flags, NULL,
2666 get_last_displayed_symtab (),
2667 get_last_displayed_line ());
2669 sals = decode_line_1 (location.get (), flags, NULL,
2670 (struct symtab *) NULL, 0);
2673 error (_("Junk at end of line specification: %s"), string);
2680 /* First, some functions to initialize stuff at the beggining of the
2684 initialize_defaults (struct symtab **default_symtab, int *default_line)
2686 if (*default_symtab == 0)
2688 /* Use whatever we have for the default source line. We don't use
2689 get_current_or_default_symtab_and_line as it can recurse and call
2691 struct symtab_and_line cursal =
2692 get_current_source_symtab_and_line ();
2694 *default_symtab = cursal.symtab;
2695 *default_line = cursal.line;
2701 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2702 advancing EXP_PTR past any parsed text. */
2705 linespec_expression_to_pc (const char **exp_ptr)
2707 if (current_program_space->executing_startup)
2708 /* The error message doesn't really matter, because this case
2709 should only hit during breakpoint reset. */
2710 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
2711 "program space is in startup"));
2714 return value_as_address (parse_to_comma_and_eval (exp_ptr));
2719 /* Here's where we recognise an Objective-C Selector. An Objective C
2720 selector may be implemented by more than one class, therefore it
2721 may represent more than one method/function. This gives us a
2722 situation somewhat analogous to C++ overloading. If there's more
2723 than one method that could represent the selector, then use some of
2724 the existing C++ code to let the user choose one. */
2726 static struct symtabs_and_lines
2727 decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
2729 struct collect_info info;
2730 VEC (const_char_ptr) *symbol_names = NULL;
2731 struct symtabs_and_lines values;
2732 const char *new_argptr;
2733 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
2737 info.file_symtabs = NULL;
2738 VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
2739 make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
2740 info.result.symbols = NULL;
2741 info.result.minimal_symbols = NULL;
2745 new_argptr = find_imps (arg, &symbol_names);
2746 if (VEC_empty (const_char_ptr, symbol_names))
2748 do_cleanups (cleanup);
2752 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
2754 if (!VEC_empty (symbolp, info.result.symbols)
2755 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
2759 saved_arg = (char *) alloca (new_argptr - arg + 1);
2760 memcpy (saved_arg, arg, new_argptr - arg);
2761 saved_arg[new_argptr - arg] = '\0';
2763 ls->explicit_loc.function_name = xstrdup (saved_arg);
2764 ls->function_symbols = info.result.symbols;
2765 ls->minimal_symbols = info.result.minimal_symbols;
2766 values = convert_linespec_to_sals (self, ls);
2768 if (self->canonical)
2772 self->canonical->pre_expanded = 1;
2774 if (ls->explicit_loc.source_filename)
2776 str = xstrprintf ("%s:%s",
2777 ls->explicit_loc.source_filename, saved_arg);
2780 str = xstrdup (saved_arg);
2782 make_cleanup (xfree, str);
2783 self->canonical->location = new_linespec_location (&str).release ();
2787 do_cleanups (cleanup);
2794 /* A function object that serves as symbol_found_callback_ftype
2795 callback for iterate_over_symbols. This is used by
2796 lookup_prefix_sym to collect type symbols. */
2797 class decode_compound_collector
2800 decode_compound_collector ()
2803 m_unique_syms = htab_create_alloc (1, htab_hash_pointer,
2804 htab_eq_pointer, NULL,
2808 ~decode_compound_collector ()
2810 if (m_unique_syms != NULL)
2811 htab_delete (m_unique_syms);
2814 /* Releases ownership of the collected symbols and returns them. */
2815 VEC (symbolp) *release_symbols ()
2817 VEC (symbolp) *res = m_symbols;
2822 /* Callable as a symbol_found_callback_ftype callback. */
2823 bool operator () (symbol *sym);
2826 /* A hash table of all symbols we found. We use this to avoid
2827 adding any symbol more than once. */
2828 htab_t m_unique_syms;
2830 /* The result vector. */
2831 VEC (symbolp) *m_symbols;
2835 decode_compound_collector::operator () (symbol *sym)
2840 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
2841 return true; /* Continue iterating. */
2843 t = SYMBOL_TYPE (sym);
2844 t = check_typedef (t);
2845 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2846 && TYPE_CODE (t) != TYPE_CODE_UNION
2847 && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
2848 return true; /* Continue iterating. */
2850 slot = htab_find_slot (m_unique_syms, sym, INSERT);
2854 VEC_safe_push (symbolp, m_symbols, sym);
2857 return true; /* Continue iterating. */
2862 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
2864 static VEC (symbolp) *
2865 lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
2866 const char *class_name)
2870 decode_compound_collector collector;
2872 for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
2876 iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN,
2877 NULL, false, collector);
2878 iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN,
2879 NULL, false, collector);
2883 /* Program spaces that are executing startup should have
2884 been filtered out earlier. */
2885 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
2886 set_current_program_space (SYMTAB_PSPACE (elt));
2887 iterate_over_file_blocks (elt, class_name, STRUCT_DOMAIN, collector);
2888 iterate_over_file_blocks (elt, class_name, VAR_DOMAIN, collector);
2892 return collector.release_symbols ();
2895 /* A qsort comparison function for symbols. The resulting order does
2896 not actually matter; we just need to be able to sort them so that
2897 symbols with the same program space end up next to each other. */
2900 compare_symbols (const void *a, const void *b)
2902 struct symbol * const *sa = (struct symbol * const*) a;
2903 struct symbol * const *sb = (struct symbol * const*) b;
2906 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa));
2907 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb));
2914 uia = (uintptr_t) *sa;
2915 uib = (uintptr_t) *sb;
2925 /* Like compare_symbols but for minimal symbols. */
2928 compare_msymbols (const void *a, const void *b)
2930 const struct bound_minimal_symbol *sa
2931 = (const struct bound_minimal_symbol *) a;
2932 const struct bound_minimal_symbol *sb
2933 = (const struct bound_minimal_symbol *) b;
2936 uia = (uintptr_t) sa->objfile->pspace;
2937 uib = (uintptr_t) sa->objfile->pspace;
2944 uia = (uintptr_t) sa->minsym;
2945 uib = (uintptr_t) sb->minsym;
2955 /* Look for all the matching instances of each symbol in NAMES. Only
2956 instances from PSPACE are considered; other program spaces are
2957 handled by our caller. If PSPACE is NULL, then all program spaces
2958 are considered. Results are stored into INFO. */
2961 add_all_symbol_names_from_pspace (struct collect_info *info,
2962 struct program_space *pspace,
2963 VEC (const_char_ptr) *names)
2968 for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
2969 add_matching_symbols_to_info (iter, info, pspace);
2973 find_superclass_methods (VEC (typep) *superclasses,
2975 VEC (const_char_ptr) **result_names)
2977 int old_len = VEC_length (const_char_ptr, *result_names);
2978 VEC (typep) *iter_classes;
2979 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
2981 iter_classes = superclasses;
2984 VEC (typep) *new_supers = NULL;
2988 make_cleanup (VEC_cleanup (typep), &new_supers);
2989 for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
2990 find_methods (t, name, result_names, &new_supers);
2992 if (VEC_length (const_char_ptr, *result_names) != old_len
2993 || VEC_empty (typep, new_supers))
2996 iter_classes = new_supers;
2999 do_cleanups (cleanup);
3002 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3003 given by one of the symbols in SYM_CLASSES. Matches are returned
3004 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3007 find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
3008 const char *class_name, const char *method_name,
3009 VEC (symbolp) *sym_classes, VEC (symbolp) **symbols,
3010 VEC (bound_minimal_symbol_d) **minsyms)
3013 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
3015 int last_result_len;
3016 VEC (typep) *superclass_vec;
3017 VEC (const_char_ptr) *result_names;
3018 struct collect_info info;
3020 /* Sort symbols so that symbols with the same program space are next
3022 qsort (VEC_address (symbolp, sym_classes),
3023 VEC_length (symbolp, sym_classes),
3028 info.file_symtabs = file_symtabs;
3029 info.result.symbols = NULL;
3030 info.result.minimal_symbols = NULL;
3032 /* Iterate over all the types, looking for the names of existing
3033 methods matching METHOD_NAME. If we cannot find a direct method in a
3034 given program space, then we consider inherited methods; this is
3035 not ideal (ideal would be to respect C++ hiding rules), but it
3036 seems good enough and is what GDB has historically done. We only
3037 need to collect the names because later we find all symbols with
3038 those names. This loop is written in a somewhat funny way
3039 because we collect data across the program space before deciding
3041 superclass_vec = NULL;
3042 make_cleanup (VEC_cleanup (typep), &superclass_vec);
3043 result_names = NULL;
3044 make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
3045 last_result_len = 0;
3046 for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
3049 struct program_space *pspace;
3051 /* Program spaces that are executing startup should have
3052 been filtered out earlier. */
3053 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
3054 gdb_assert (!pspace->executing_startup);
3055 set_current_program_space (pspace);
3056 t = check_typedef (SYMBOL_TYPE (sym));
3057 find_methods (t, method_name, &result_names, &superclass_vec);
3059 /* Handle all items from a single program space at once; and be
3060 sure not to miss the last batch. */
3061 if (ix == VEC_length (symbolp, sym_classes) - 1
3063 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes,
3066 /* If we did not find a direct implementation anywhere in
3067 this program space, consider superclasses. */
3068 if (VEC_length (const_char_ptr, result_names) == last_result_len)
3069 find_superclass_methods (superclass_vec, method_name,
3072 /* We have a list of candidate symbol names, so now we
3073 iterate over the symbol tables looking for all
3074 matches in this pspace. */
3075 add_all_symbol_names_from_pspace (&info, pspace, result_names);
3077 VEC_truncate (typep, superclass_vec, 0);
3078 last_result_len = VEC_length (const_char_ptr, result_names);
3082 if (!VEC_empty (symbolp, info.result.symbols)
3083 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
3085 *symbols = info.result.symbols;
3086 *minsyms = info.result.minimal_symbols;
3087 do_cleanups (cleanup);
3091 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3092 and other attempts to locate the symbol will be made. */
3093 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
3100 /* This function object is a callback for iterate_over_symtabs, used
3101 when collecting all matching symtabs. */
3103 class symtab_collector
3109 m_symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
3113 ~symtab_collector ()
3115 if (m_symtab_table != NULL)
3116 htab_delete (m_symtab_table);
3119 /* Callable as a symbol_found_callback_ftype callback. */
3120 bool operator () (symtab *sym);
3122 /* Releases ownership of the collected symtabs and returns them. */
3123 VEC (symtab_ptr) *release_symtabs ()
3125 VEC (symtab_ptr) *res = m_symtabs;
3131 /* The result vector of symtabs. */
3132 VEC (symtab_ptr) *m_symtabs;
3134 /* This is used to ensure the symtabs are unique. */
3135 htab_t m_symtab_table;
3139 symtab_collector::operator () (struct symtab *symtab)
3143 slot = htab_find_slot (m_symtab_table, symtab, INSERT);
3147 VEC_safe_push (symtab_ptr, m_symtabs, symtab);
3155 /* Given a file name, return a VEC of all matching symtabs. If
3156 SEARCH_PSPACE is not NULL, the search is restricted to just that
3159 static VEC (symtab_ptr) *
3160 collect_symtabs_from_filename (const char *file,
3161 struct program_space *search_pspace)
3163 symtab_collector collector;
3165 /* Find that file's data. */
3166 if (search_pspace == NULL)
3168 struct program_space *pspace;
3170 ALL_PSPACES (pspace)
3172 if (pspace->executing_startup)
3175 set_current_program_space (pspace);
3176 iterate_over_symtabs (file, collector);
3181 set_current_program_space (search_pspace);
3182 iterate_over_symtabs (file, collector);
3185 return collector.release_symtabs ();
3188 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3189 not NULL, the search is restricted to just that program space. */
3191 static VEC (symtab_ptr) *
3192 symtabs_from_filename (const char *filename,
3193 struct program_space *search_pspace)
3195 VEC (symtab_ptr) *result;
3197 result = collect_symtabs_from_filename (filename, search_pspace);
3199 if (VEC_empty (symtab_ptr, result))
3201 if (!have_full_symbols () && !have_partial_symbols ())
3202 throw_error (NOT_FOUND_ERROR,
3203 _("No symbol table is loaded. "
3204 "Use the \"file\" command."));
3205 source_file_not_found_error (filename);
3211 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3212 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3213 returned in MINSYMS. */
3216 find_function_symbols (struct linespec_state *state,
3217 VEC (symtab_ptr) *file_symtabs, const char *name,
3218 VEC (symbolp) **symbols,
3219 VEC (bound_minimal_symbol_d) **minsyms)
3221 struct collect_info info;
3222 VEC (const_char_ptr) *symbol_names = NULL;
3223 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
3227 info.result.symbols = NULL;
3228 info.result.minimal_symbols = NULL;
3229 info.file_symtabs = file_symtabs;
3231 /* Try NAME as an Objective-C selector. */
3232 find_imps (name, &symbol_names);
3233 if (!VEC_empty (const_char_ptr, symbol_names))
3234 add_all_symbol_names_from_pspace (&info, state->search_pspace,
3237 add_matching_symbols_to_info (name, &info, state->search_pspace);
3239 do_cleanups (cleanup);
3241 if (VEC_empty (symbolp, info.result.symbols))
3243 VEC_free (symbolp, info.result.symbols);
3247 *symbols = info.result.symbols;
3249 if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
3251 VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
3255 *minsyms = info.result.minimal_symbols;
3258 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3259 in SYMBOLS and minimal symbols in MINSYMS. */
3262 find_linespec_symbols (struct linespec_state *state,
3263 VEC (symtab_ptr) *file_symtabs,
3265 VEC (symbolp) **symbols,
3266 VEC (bound_minimal_symbol_d) **minsyms)
3268 demangle_result_storage demangle_storage;
3269 std::string ada_lookup_storage;
3270 const char *lookup_name;
3272 if (state->language->la_language == language_ada)
3274 /* In Ada, the symbol lookups are performed using the encoded
3275 name rather than the demangled name. */
3276 ada_lookup_storage = ada_name_for_lookup (name);
3277 lookup_name = ada_lookup_storage.c_str ();
3281 lookup_name = demangle_for_lookup (name,
3282 state->language->la_language,
3286 std::string canon = cp_canonicalize_string_no_typedefs (lookup_name);
3287 if (!canon.empty ())
3288 lookup_name = canon.c_str ();
3290 /* It's important to not call expand_symtabs_matching unnecessarily
3291 as it can really slow things down (by unnecessarily expanding
3292 potentially 1000s of symtabs, which when debugging some apps can
3293 cost 100s of seconds). Avoid this to some extent by *first* calling
3294 find_function_symbols, and only if that doesn't find anything
3295 *then* call find_method. This handles two important cases:
3296 1) break (anonymous namespace)::foo
3297 2) break class::method where method is in class (and not a baseclass) */
3299 find_function_symbols (state, file_symtabs, lookup_name,
3302 /* If we were unable to locate a symbol of the same name, try dividing
3303 the name into class and method names and searching the class and its
3305 if (VEC_empty (symbolp, *symbols)
3306 && VEC_empty (bound_minimal_symbol_d, *minsyms))
3308 std::string klass, method;
3309 const char *last, *p, *scope_op;
3310 VEC (symbolp) *classes;
3312 /* See if we can find a scope operator and break this symbol
3313 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3315 p = find_toplevel_string (lookup_name, scope_op);
3321 p = find_toplevel_string (p + strlen (scope_op), scope_op);
3324 /* If no scope operator was found, there is nothing more we can do;
3325 we already attempted to lookup the entire name as a symbol
3330 /* LOOKUP_NAME points to the class name.
3331 LAST points to the method name. */
3332 klass = std::string (lookup_name, last - lookup_name);
3334 /* Skip past the scope operator. */
3335 last += strlen (scope_op);
3338 /* Find a list of classes named KLASS. */
3339 classes = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
3340 struct cleanup *old_chain
3341 = make_cleanup (VEC_cleanup (symbolp), &classes);
3343 if (!VEC_empty (symbolp, classes))
3345 /* Now locate a list of suitable methods named METHOD. */
3348 find_method (state, file_symtabs,
3349 klass.c_str (), method.c_str (),
3350 classes, symbols, minsyms);
3353 /* If successful, we're done. If NOT_FOUND_ERROR
3354 was not thrown, rethrow the exception that we did get. */
3355 CATCH (except, RETURN_MASK_ERROR)
3357 if (except.error != NOT_FOUND_ERROR)
3358 throw_exception (except);
3363 do_cleanups (old_chain);
3367 /* Return all labels named NAME in FUNCTION_SYMBOLS. Return the
3368 actual function symbol in which the label was found in LABEL_FUNC_RET. */
3370 static VEC (symbolp) *
3371 find_label_symbols (struct linespec_state *self,
3372 VEC (symbolp) *function_symbols,
3373 VEC (symbolp) **label_funcs_ret, const char *name)
3376 const struct block *block;
3378 struct symbol *fn_sym;
3379 VEC (symbolp) *result = NULL;
3381 if (function_symbols == NULL)
3383 set_current_program_space (self->program_space);
3384 block = get_current_search_block ();
3387 block && !BLOCK_FUNCTION (block);
3388 block = BLOCK_SUPERBLOCK (block))
3392 fn_sym = BLOCK_FUNCTION (block);
3394 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol;
3398 VEC_safe_push (symbolp, result, sym);
3399 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
3405 VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
3407 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
3408 block = SYMBOL_BLOCK_VALUE (fn_sym);
3409 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol;
3413 VEC_safe_push (symbolp, result, sym);
3414 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
3424 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3427 decode_digits_list_mode (struct linespec_state *self,
3429 struct symtabs_and_lines *values,
3430 struct symtab_and_line val)
3435 gdb_assert (self->list_mode);
3437 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
3440 /* The logic above should ensure this. */
3441 gdb_assert (elt != NULL);
3443 set_current_program_space (SYMTAB_PSPACE (elt));
3445 /* Simplistic search just for the list command. */
3446 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
3447 if (val.symtab == NULL)
3449 val.pspace = SYMTAB_PSPACE (elt);
3451 val.explicit_line = 1;
3453 add_sal_to_sals (self, values, &val, NULL, 0);
3457 /* A helper for create_sals_line_offset that iterates over the symtabs,
3458 adding lines to the VEC. */
3461 decode_digits_ordinary (struct linespec_state *self,
3464 struct symtabs_and_lines *sals,
3465 struct linetable_entry **best_entry)
3470 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
3472 std::vector<CORE_ADDR> pcs;
3474 /* The logic above should ensure this. */
3475 gdb_assert (elt != NULL);
3477 set_current_program_space (SYMTAB_PSPACE (elt));
3479 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
3480 for (CORE_ADDR pc : pcs)
3482 struct symtab_and_line sal;
3485 sal.pspace = SYMTAB_PSPACE (elt);
3489 add_sal_to_sals_basic (sals, &sal);
3496 /* Return the line offset represented by VARIABLE. */
3498 static struct line_offset
3499 linespec_parse_variable (struct linespec_state *self, const char *variable)
3503 struct line_offset offset = {0, LINE_OFFSET_NONE};
3505 p = (variable[1] == '$') ? variable + 2 : variable + 1;
3508 while (*p >= '0' && *p <= '9')
3510 if (!*p) /* Reached end of token without hitting non-digit. */
3512 /* We have a value history reference. */
3513 struct value *val_history;
3515 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
3517 = access_value_history ((variable[1] == '$') ? -index : index);
3518 if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
3519 error (_("History values used in line "
3520 "specs must have integer values."));
3521 offset.offset = value_as_long (val_history);
3525 /* Not all digits -- may be user variable/function or a
3526 convenience variable. */
3528 struct internalvar *ivar;
3530 /* Try it as a convenience variable. If it is not a convenience
3531 variable, return and allow normal symbol lookup to occur. */
3532 ivar = lookup_only_internalvar (variable + 1);
3534 /* No internal variable with that name. Mark the offset
3535 as unknown to allow the name to be looked up as a symbol. */
3536 offset.sign = LINE_OFFSET_UNKNOWN;
3539 /* We found a valid variable name. If it is not an integer,
3541 if (!get_internalvar_integer (ivar, &valx))
3542 error (_("Convenience variables used in line "
3543 "specs must have integer values."));
3545 offset.offset = valx;
3553 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
3554 linespec; return the SAL in RESULT. This function should return SALs
3555 matching those from find_function_start_sal, otherwise false
3556 multiple-locations breakpoints could be placed. */
3559 minsym_found (struct linespec_state *self, struct objfile *objfile,
3560 struct minimal_symbol *msymbol,
3561 struct symtabs_and_lines *result)
3563 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3565 struct symtab_and_line sal;
3567 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
3568 (struct obj_section *) 0, 0);
3569 sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
3571 /* The minimal symbol might point to a function descriptor;
3572 resolve it to the actual code address instead. */
3573 pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, ¤t_target);
3575 sal = find_pc_sect_line (pc, NULL, 0);
3577 if (self->funfirstline)
3579 if (sal.symtab != NULL
3580 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
3581 || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
3583 /* If gdbarch_convert_from_func_ptr_addr does not apply then
3584 sal.SECTION, sal.LINE&co. will stay correct from above.
3585 If gdbarch_convert_from_func_ptr_addr applies then
3586 sal.SECTION is cleared from above and sal.LINE&co. will
3587 stay correct from the last find_pc_sect_line above. */
3588 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
3589 sal.pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc,
3591 if (gdbarch_skip_entrypoint_p (gdbarch))
3592 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
3595 skip_prologue_sal (&sal);
3598 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
3599 add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0);
3602 /* A helper struct to pass some data through
3603 iterate_over_minimal_symbols. */
3605 struct collect_minsyms
3607 /* The objfile we're examining. */
3608 struct objfile *objfile;
3610 /* Only search the given symtab, or NULL to search for all symbols. */
3611 struct symtab *symtab;
3613 /* The funfirstline setting from the initial call. */
3616 /* The list_mode setting from the initial call. */
3619 /* The resulting symbols. */
3620 VEC (bound_minimal_symbol_d) *msyms;
3623 /* A helper function to classify a minimal_symbol_type according to
3627 classify_mtype (enum minimal_symbol_type t)
3634 /* Intermediate priority. */
3637 case mst_solib_trampoline:
3638 /* Lowest priority. */
3642 /* Highest priority. */
3647 /* Callback for qsort that sorts symbols by priority. */
3650 compare_msyms (const void *a, const void *b)
3652 const bound_minimal_symbol_d *moa = (const bound_minimal_symbol_d *) a;
3653 const bound_minimal_symbol_d *mob = (const bound_minimal_symbol_d *) b;
3654 enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
3655 enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
3657 return classify_mtype (ta) - classify_mtype (tb);
3660 /* Callback for iterate_over_minimal_symbols that adds the symbol to
3664 add_minsym (struct minimal_symbol *minsym, void *d)
3666 struct collect_minsyms *info = (struct collect_minsyms *) d;
3667 bound_minimal_symbol_d mo;
3670 mo.objfile = info->objfile;
3672 if (info->symtab != NULL)
3675 struct symtab_and_line sal;
3676 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
3678 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym),
3680 sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym);
3682 = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, ¤t_target);
3684 sal = find_pc_sect_line (pc, NULL, 0);
3686 if (info->symtab != sal.symtab)
3690 /* Exclude data symbols when looking for breakpoint locations. */
3691 if (!info->list_mode)
3692 switch (minsym->type)
3694 case mst_slot_got_plt:
3701 /* Make sure this minsym is not a function descriptor
3702 before we decide to discard it. */
3703 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
3704 CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
3705 (gdbarch, BMSYMBOL_VALUE_ADDRESS (mo),
3708 if (addr == BMSYMBOL_VALUE_ADDRESS (mo))
3713 VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
3716 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
3717 is not NULL, the search is restricted to just that program
3720 If SYMTAB is NULL, search all objfiles, otherwise
3721 restrict results to the given SYMTAB. */
3724 search_minsyms_for_name (struct collect_info *info, const char *name,
3725 struct program_space *search_pspace,
3726 struct symtab *symtab)
3728 struct collect_minsyms local;
3729 struct cleanup *cleanup;
3731 memset (&local, 0, sizeof (local));
3732 local.funfirstline = info->state->funfirstline;
3733 local.list_mode = info->state->list_mode;
3734 local.symtab = symtab;
3736 cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);
3740 struct program_space *pspace;
3742 ALL_PSPACES (pspace)
3744 struct objfile *objfile;
3746 if (search_pspace != NULL && search_pspace != pspace)
3748 if (pspace->executing_startup)
3751 set_current_program_space (pspace);
3753 ALL_OBJFILES (objfile)
3755 local.objfile = objfile;
3756 iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
3762 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
3764 set_current_program_space (SYMTAB_PSPACE (symtab));
3765 local.objfile = SYMTAB_OBJFILE(symtab);
3766 iterate_over_minimal_symbols (local.objfile, name, add_minsym,
3771 if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
3775 bound_minimal_symbol_d *item;
3777 qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
3778 VEC_length (bound_minimal_symbol_d, local.msyms),
3779 sizeof (bound_minimal_symbol_d),
3782 /* Now the minsyms are in classification order. So, we walk
3783 over them and process just the minsyms with the same
3784 classification as the very first minsym in the list. */
3785 item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
3786 classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
3789 VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
3792 if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
3795 VEC_safe_push (bound_minimal_symbol_d,
3796 info->result.minimal_symbols, item);
3800 do_cleanups (cleanup);
3803 /* A helper function to add all symbols matching NAME to INFO. If
3804 PSPACE is not NULL, the search is restricted to just that program
3808 add_matching_symbols_to_info (const char *name,
3809 struct collect_info *info,
3810 struct program_space *pspace)
3815 for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
3819 iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN,
3820 pspace, true, [&] (symbol *sym)
3821 { return info->add_symbol (sym); });
3822 search_minsyms_for_name (info, name, pspace, NULL);
3824 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
3826 int prev_len = VEC_length (symbolp, info->result.symbols);
3828 /* Program spaces that are executing startup should have
3829 been filtered out earlier. */
3830 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3831 set_current_program_space (SYMTAB_PSPACE (elt));
3832 iterate_over_file_blocks (elt, name, VAR_DOMAIN, [&] (symbol *sym)
3833 { return info->add_symbol (sym); });
3835 /* If no new symbols were found in this iteration and this symtab
3836 is in assembler, we might actually be looking for a label for
3837 which we don't have debug info. Check for a minimal symbol in
3839 if (prev_len == VEC_length (symbolp, info->result.symbols)
3840 && elt->language == language_asm)
3841 search_minsyms_for_name (info, name, pspace, elt);
3848 /* Now come some functions that are called from multiple places within
3852 symbol_to_sal (struct symtab_and_line *result,
3853 int funfirstline, struct symbol *sym)
3855 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
3857 *result = find_function_start_sal (sym, funfirstline);
3862 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
3865 result->symtab = symbol_symtab (sym);
3866 result->line = SYMBOL_LINE (sym);
3867 result->pc = SYMBOL_VALUE_ADDRESS (sym);
3868 result->pspace = SYMTAB_PSPACE (result->symtab);
3869 result->explicit_pc = 1;
3872 else if (funfirstline)
3876 else if (SYMBOL_LINE (sym) != 0)
3878 /* We know its line number. */
3880 result->symtab = symbol_symtab (sym);
3881 result->line = SYMBOL_LINE (sym);
3882 result->pspace = SYMTAB_PSPACE (result->symtab);
3890 /* See the comment in linespec.h. */
3893 init_linespec_result (struct linespec_result *lr)
3895 memset (lr, 0, sizeof (*lr));
3898 /* See the comment in linespec.h. */
3901 destroy_linespec_result (struct linespec_result *ls)
3904 struct linespec_sals *lsal;
3906 delete_event_location (ls->location);
3907 for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
3909 xfree (lsal->canonical);
3910 xfree (lsal->sals.sals);
3912 VEC_free (linespec_sals, ls->sals);
3915 /* Cleanup function for a linespec_result. */
3918 cleanup_linespec_result (void *a)
3920 destroy_linespec_result ((struct linespec_result *) a);
3923 /* See the comment in linespec.h. */
3926 make_cleanup_destroy_linespec_result (struct linespec_result *ls)
3928 return make_cleanup (cleanup_linespec_result, ls);
3931 /* Return the quote characters permitted by the linespec parser. */
3934 get_gdb_linespec_parser_quote_characters (void)
3936 return linespec_quote_characters;