1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2021 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 "gdbsupport/function-view.h"
48 #include "gdbsupport/def-vector.h"
52 /* An enumeration of the various things a user might attempt to
53 complete for a linespec location. */
55 enum class linespec_complete_what
57 /* Nothing, no possible completion. */
60 /* A function/method name. Due to ambiguity between
66 this can also indicate a source filename, iff we haven't seen a
67 separate source filename component, as in "b source.c:function". */
70 /* A label symbol. E.g., break file.c:function:LABEL. */
73 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
76 /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
77 E.g., "break func threa<tab>". */
81 /* An address entry is used to ensure that any given location is only
82 added to the result a single time. It holds an address and the
83 program space from which the address came. */
87 struct program_space
*pspace
;
91 /* A linespec. Elements of this structure are filled in by a parser
92 (either parse_linespec or some other function). The structure is
93 then converted into SALs by convert_linespec_to_sals. */
97 /* An explicit location describing the SaLs. */
98 struct explicit_location explicit_loc
;
100 /* The list of symtabs to search to which to limit the search. May not
101 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
102 filename), FILE_SYMTABS should contain one single NULL member. This
103 will cause the code to use the default symtab. */
104 std::vector
<symtab
*> *file_symtabs
;
106 /* A list of matching function symbols and minimal symbols. Both lists
107 may be NULL (or empty) if no matching symbols were found. */
108 std::vector
<block_symbol
> *function_symbols
;
109 std::vector
<bound_minimal_symbol
> *minimal_symbols
;
111 /* A structure of matching label symbols and the corresponding
112 function symbol in which the label was found. Both may be NULL
113 or both must be non-NULL. */
116 std::vector
<block_symbol
> *label_symbols
;
117 std::vector
<block_symbol
> *function_symbols
;
120 typedef struct linespec
*linespec_p
;
122 /* A canonical linespec represented as a symtab-related string.
124 Each entry represents the "SYMTAB:SUFFIX" linespec string.
125 SYMTAB can be converted for example by symtab_to_fullname or
126 symtab_to_filename_for_display as needed. */
128 struct linespec_canonical_name
130 /* Remaining text part of the linespec string. */
133 /* If NULL then SUFFIX is the whole linespec string. */
134 struct symtab
*symtab
;
137 /* An instance of this is used to keep all state while linespec
138 operates. This instance is passed around as a 'this' pointer to
139 the various implementation methods. */
141 struct linespec_state
143 /* The language in use during linespec processing. */
144 const struct language_defn
*language
;
146 /* The program space as seen when the module was entered. */
147 struct program_space
*program_space
;
149 /* If not NULL, the search is restricted to just this program
151 struct program_space
*search_pspace
;
153 /* The default symtab to use, if no other symtab is specified. */
154 struct symtab
*default_symtab
;
156 /* The default line to use. */
159 /* The 'funfirstline' value that was passed in to decode_line_1 or
163 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
166 /* The 'canonical' value passed to decode_line_full, or NULL. */
167 struct linespec_result
*canonical
;
169 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
170 struct linespec_canonical_name
*canonical_names
;
172 /* This is a set of address_entry objects which is used to prevent
173 duplicate symbols from being entered into the result. */
176 /* Are we building a linespec? */
180 /* This is a helper object that is used when collecting symbols into a
185 /* The linespec object in use. */
186 struct linespec_state
*state
;
188 /* A list of symtabs to which to restrict matches. */
189 std::vector
<symtab
*> *file_symtabs
;
191 /* The result being accumulated. */
194 std::vector
<block_symbol
> *symbols
;
195 std::vector
<bound_minimal_symbol
> *minimal_symbols
;
198 /* Possibly add a symbol to the results. */
199 virtual bool add_symbol (block_symbol
*bsym
);
203 collect_info::add_symbol (block_symbol
*bsym
)
205 /* In list mode, add all matching symbols, regardless of class.
206 This allows the user to type "list a_global_variable". */
207 if (SYMBOL_CLASS (bsym
->symbol
) == LOC_BLOCK
|| this->state
->list_mode
)
208 this->result
.symbols
->push_back (*bsym
);
210 /* Continue iterating. */
214 /* Custom collect_info for symbol_searcher. */
216 struct symbol_searcher_collect_info
219 bool add_symbol (block_symbol
*bsym
) override
221 /* Add everything. */
222 this->result
.symbols
->push_back (*bsym
);
224 /* Continue iterating. */
236 /* A colon "separator" */
248 /* EOI (end of input) */
254 typedef enum ls_token_type linespec_token_type
;
256 /* List of keywords. This is NULL-terminated so that it can be used
257 as enum completer. */
258 const char * const linespec_keywords
[] = { "if", "thread", "task", "-force-condition", NULL
};
259 #define IF_KEYWORD_INDEX 0
260 #define FORCE_KEYWORD_INDEX 3
262 /* A token of the linespec lexer */
264 struct linespec_token
266 /* The type of the token */
267 linespec_token_type type
;
269 /* Data for the token */
272 /* A string, given as a stoken */
273 struct stoken string
;
280 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
281 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
283 /* An instance of the linespec parser. */
285 struct linespec_parser
287 linespec_parser (int flags
, const struct language_defn
*language
,
288 struct program_space
*search_pspace
,
289 struct symtab
*default_symtab
,
291 struct linespec_result
*canonical
);
295 DISABLE_COPY_AND_ASSIGN (linespec_parser
);
297 /* Lexer internal data */
300 /* Save head of input stream. */
301 const char *saved_arg
;
303 /* Head of the input stream. */
305 #define PARSER_STREAM(P) ((P)->lexer.stream)
307 /* The current token. */
308 linespec_token current
;
311 /* Is the entire linespec quote-enclosed? */
312 int is_quote_enclosed
= 0;
314 /* The state of the parse. */
315 struct linespec_state state
{};
316 #define PARSER_STATE(PPTR) (&(PPTR)->state)
318 /* The result of the parse. */
319 struct linespec result
{};
320 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
322 /* What the parser believes the current word point should complete
324 linespec_complete_what complete_what
= linespec_complete_what::NOTHING
;
326 /* The completion word point. The parser advances this as it skips
327 tokens. At some point the input string will end or parsing will
328 fail, and then we attempt completion at the captured completion
329 word point, interpreting the string at completion_word as
331 const char *completion_word
= nullptr;
333 /* If the current token was a quoted string, then this is the
334 quoting character (either " or '). */
335 int completion_quote_char
= 0;
337 /* If the current token was a quoted string, then this points at the
338 end of the quoted string. */
339 const char *completion_quote_end
= nullptr;
341 /* If parsing for completion, then this points at the completion
342 tracker. Otherwise, this is NULL. */
343 struct completion_tracker
*completion_tracker
= nullptr;
346 /* A convenience macro for accessing the explicit location result of
348 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
350 /* Prototypes for local functions. */
352 static void iterate_over_file_blocks
353 (struct symtab
*symtab
, const lookup_name_info
&name
,
355 gdb::function_view
<symbol_found_callback_ftype
> callback
);
357 static void initialize_defaults (struct symtab
**default_symtab
,
360 CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
362 static std::vector
<symtab_and_line
> decode_objc (struct linespec_state
*self
,
366 static std::vector
<symtab
*> symtabs_from_filename
367 (const char *, struct program_space
*pspace
);
369 static std::vector
<block_symbol
> *find_label_symbols
370 (struct linespec_state
*self
, std::vector
<block_symbol
> *function_symbols
,
371 std::vector
<block_symbol
> *label_funcs_ret
, const char *name
,
372 bool completion_mode
= false);
374 static void find_linespec_symbols (struct linespec_state
*self
,
375 std::vector
<symtab
*> *file_symtabs
,
377 symbol_name_match_type name_match_type
,
378 std::vector
<block_symbol
> *symbols
,
379 std::vector
<bound_minimal_symbol
> *minsyms
);
381 static struct line_offset
382 linespec_parse_variable (struct linespec_state
*self
,
383 const char *variable
);
385 static int symbol_to_sal (struct symtab_and_line
*result
,
386 int funfirstline
, struct symbol
*sym
);
388 static void add_matching_symbols_to_info (const char *name
,
389 symbol_name_match_type name_match_type
,
390 enum search_domain search_domain
,
391 struct collect_info
*info
,
392 struct program_space
*pspace
);
394 static void add_all_symbol_names_from_pspace
395 (struct collect_info
*info
, struct program_space
*pspace
,
396 const std::vector
<const char *> &names
, enum search_domain search_domain
);
398 static std::vector
<symtab
*>
399 collect_symtabs_from_filename (const char *file
,
400 struct program_space
*pspace
);
402 static std::vector
<symtab_and_line
> decode_digits_ordinary
403 (struct linespec_state
*self
,
406 linetable_entry
**best_entry
);
408 static std::vector
<symtab_and_line
> decode_digits_list_mode
409 (struct linespec_state
*self
,
411 struct symtab_and_line val
);
413 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
414 struct minimal_symbol
*msymbol
,
415 std::vector
<symtab_and_line
> *result
);
417 static bool compare_symbols (const block_symbol
&a
, const block_symbol
&b
);
419 static bool compare_msymbols (const bound_minimal_symbol
&a
,
420 const bound_minimal_symbol
&b
);
422 /* Permitted quote characters for the parser. This is different from the
423 completer's quote characters to allow backward compatibility with the
425 static const char linespec_quote_characters
[] = "\"\'";
427 /* Lexer functions. */
429 /* Lex a number from the input in PARSER. This only supports
432 Return true if input is decimal numbers. Return false if not. */
435 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
437 tokenp
->type
= LSTOKEN_NUMBER
;
438 LS_TOKEN_STOKEN (*tokenp
).length
= 0;
439 LS_TOKEN_STOKEN (*tokenp
).ptr
= PARSER_STREAM (parser
);
441 /* Keep any sign at the start of the stream. */
442 if (*PARSER_STREAM (parser
) == '+' || *PARSER_STREAM (parser
) == '-')
444 ++LS_TOKEN_STOKEN (*tokenp
).length
;
445 ++(PARSER_STREAM (parser
));
448 while (isdigit (*PARSER_STREAM (parser
)))
450 ++LS_TOKEN_STOKEN (*tokenp
).length
;
451 ++(PARSER_STREAM (parser
));
454 /* If the next character in the input buffer is not a space, comma,
455 quote, or colon, this input does not represent a number. */
456 if (*PARSER_STREAM (parser
) != '\0'
457 && !isspace (*PARSER_STREAM (parser
)) && *PARSER_STREAM (parser
) != ','
458 && *PARSER_STREAM (parser
) != ':'
459 && !strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
461 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (*tokenp
).ptr
;
468 /* See linespec.h. */
471 linespec_lexer_lex_keyword (const char *p
)
477 for (i
= 0; linespec_keywords
[i
] != NULL
; ++i
)
479 int len
= strlen (linespec_keywords
[i
]);
483 - "thread" or "task" and the next character is
484 whitespace, we may have found a keyword. It is only a
485 keyword if it is not followed by another keyword.
487 - "-force-condition", the next character may be EOF
488 since this keyword does not take any arguments. Otherwise,
489 it should be followed by a keyword.
491 - "if", ALWAYS stop the lexer, since it is not possible to
492 predict what is going to appear in the condition, which can
493 only be parsed after SaLs have been found. */
494 if (strncmp (p
, linespec_keywords
[i
], len
) == 0)
498 if (i
== FORCE_KEYWORD_INDEX
&& p
[len
] == '\0')
499 return linespec_keywords
[i
];
501 if (!isspace (p
[len
]))
504 if (i
== FORCE_KEYWORD_INDEX
)
508 for (j
= 0; linespec_keywords
[j
] != NULL
; ++j
)
510 int nextlen
= strlen (linespec_keywords
[j
]);
512 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
513 && isspace (p
[nextlen
]))
514 return linespec_keywords
[i
];
517 else if (i
!= IF_KEYWORD_INDEX
)
519 /* We matched a "thread" or "task". */
522 for (j
= 0; linespec_keywords
[j
] != NULL
; ++j
)
524 int nextlen
= strlen (linespec_keywords
[j
]);
526 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
527 && isspace (p
[nextlen
]))
532 return linespec_keywords
[i
];
540 /* See description in linespec.h. */
543 is_ada_operator (const char *string
)
545 const struct ada_opname_map
*mapping
;
547 for (mapping
= ada_opname_table
;
548 mapping
->encoded
!= NULL
549 && !startswith (string
, mapping
->decoded
); ++mapping
)
552 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
555 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
556 the location of QUOTE_CHAR, or NULL if not found. */
559 skip_quote_char (const char *string
, char quote_char
)
561 const char *p
, *last
;
563 p
= last
= find_toplevel_char (string
, quote_char
);
564 while (p
&& *p
!= '\0' && *p
!= ':')
566 p
= find_toplevel_char (p
, quote_char
);
574 /* Make a writable copy of the string given in TOKEN, trimming
575 any trailing whitespace. */
577 static gdb::unique_xmalloc_ptr
<char>
578 copy_token_string (linespec_token token
)
582 if (token
.type
== LSTOKEN_KEYWORD
)
583 return make_unique_xstrdup (LS_TOKEN_KEYWORD (token
));
585 str
= LS_TOKEN_STOKEN (token
).ptr
;
586 s
= remove_trailing_whitespace (str
, str
+ LS_TOKEN_STOKEN (token
).length
);
588 return gdb::unique_xmalloc_ptr
<char> (savestring (str
, s
- str
));
591 /* Does P represent the end of a quote-enclosed linespec? */
594 is_closing_quote_enclosed (const char *p
)
596 if (strchr (linespec_quote_characters
, *p
))
598 p
= skip_spaces ((char *) p
);
599 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
602 /* Find the end of the parameter list that starts with *INPUT.
603 This helper function assists with lexing string segments
604 which might contain valid (non-terminating) commas. */
607 find_parameter_list_end (const char *input
)
609 char end_char
, start_char
;
614 if (start_char
== '(')
616 else if (start_char
== '<')
625 if (*p
== start_char
)
627 else if (*p
== end_char
)
641 /* If the [STRING, STRING_LEN) string ends with what looks like a
642 keyword, return the keyword start offset in STRING. Return -1
646 string_find_incomplete_keyword_at_end (const char * const *keywords
,
647 const char *string
, size_t string_len
)
649 const char *end
= string
+ string_len
;
652 while (p
> string
&& *p
!= ' ')
657 size_t len
= end
- p
;
658 for (size_t i
= 0; keywords
[i
] != NULL
; ++i
)
659 if (strncmp (keywords
[i
], p
, len
) == 0)
666 /* Lex a string from the input in PARSER. */
668 static linespec_token
669 linespec_lexer_lex_string (linespec_parser
*parser
)
671 linespec_token token
;
672 const char *start
= PARSER_STREAM (parser
);
674 token
.type
= LSTOKEN_STRING
;
676 /* If the input stream starts with a quote character, skip to the next
677 quote character, regardless of the content. */
678 if (strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
681 char quote_char
= *PARSER_STREAM (parser
);
683 /* Special case: Ada operators. */
684 if (PARSER_STATE (parser
)->language
->la_language
== language_ada
685 && quote_char
== '\"')
687 int len
= is_ada_operator (PARSER_STREAM (parser
));
691 /* The input is an Ada operator. Return the quoted string
693 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
694 LS_TOKEN_STOKEN (token
).length
= len
;
695 PARSER_STREAM (parser
) += len
;
699 /* The input does not represent an Ada operator -- fall through
700 to normal quoted string handling. */
703 /* Skip past the beginning quote. */
704 ++(PARSER_STREAM (parser
));
706 /* Mark the start of the string. */
707 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
709 /* Skip to the ending quote. */
710 end
= skip_quote_char (PARSER_STREAM (parser
), quote_char
);
712 /* This helps the completer mode decide whether we have a
714 parser
->completion_quote_char
= quote_char
;
715 parser
->completion_quote_end
= end
;
717 /* Error if the input did not terminate properly, unless in
721 if (parser
->completion_tracker
== NULL
)
722 error (_("unmatched quote"));
724 /* In completion mode, we'll try to complete the incomplete
726 token
.type
= LSTOKEN_STRING
;
727 while (*PARSER_STREAM (parser
) != '\0')
728 PARSER_STREAM (parser
)++;
729 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 1 - start
;
733 /* Skip over the ending quote and mark the length of the string. */
734 PARSER_STREAM (parser
) = (char *) ++end
;
735 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 2 - start
;
742 /* Otherwise, only identifier characters are permitted.
743 Spaces are the exception. In general, we keep spaces,
744 but only if the next characters in the input do not resolve
745 to one of the keywords.
747 This allows users to forgo quoting CV-qualifiers, template arguments,
748 and similar common language constructs. */
752 if (isspace (*PARSER_STREAM (parser
)))
754 p
= skip_spaces (PARSER_STREAM (parser
));
755 /* When we get here we know we've found something followed by
756 a space (we skip over parens and templates below).
757 So if we find a keyword now, we know it is a keyword and not,
758 say, a function name. */
759 if (linespec_lexer_lex_keyword (p
) != NULL
)
761 LS_TOKEN_STOKEN (token
).ptr
= start
;
762 LS_TOKEN_STOKEN (token
).length
763 = PARSER_STREAM (parser
) - start
;
767 /* Advance past the whitespace. */
768 PARSER_STREAM (parser
) = p
;
771 /* If the next character is EOI or (single) ':', the
772 string is complete; return the token. */
773 if (*PARSER_STREAM (parser
) == 0)
775 LS_TOKEN_STOKEN (token
).ptr
= start
;
776 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
779 else if (PARSER_STREAM (parser
)[0] == ':')
781 /* Do not tokenize the C++ scope operator. */
782 if (PARSER_STREAM (parser
)[1] == ':')
783 ++(PARSER_STREAM (parser
));
785 /* Do not tokenize ABI tags such as "[abi:cxx11]". */
786 else if (PARSER_STREAM (parser
) - start
> 4
787 && startswith (PARSER_STREAM (parser
) - 4, "[abi"))
792 /* Do not tokenify if the input length so far is one
793 (i.e, a single-letter drive name) and the next character
794 is a directory separator. This allows Windows-style
795 paths to be recognized as filenames without quoting it. */
796 else if ((PARSER_STREAM (parser
) - start
) != 1
797 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser
)[1]))
799 LS_TOKEN_STOKEN (token
).ptr
= start
;
800 LS_TOKEN_STOKEN (token
).length
801 = PARSER_STREAM (parser
) - start
;
805 /* Special case: permit quote-enclosed linespecs. */
806 else if (parser
->is_quote_enclosed
807 && strchr (linespec_quote_characters
,
808 *PARSER_STREAM (parser
))
809 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
811 LS_TOKEN_STOKEN (token
).ptr
= start
;
812 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
815 /* Because commas may terminate a linespec and appear in
816 the middle of valid string input, special cases for
817 '<' and '(' are necessary. */
818 else if (*PARSER_STREAM (parser
) == '<'
819 || *PARSER_STREAM (parser
) == '(')
821 /* Don't interpret 'operator<' / 'operator<<' as a
822 template parameter list though. */
823 if (*PARSER_STREAM (parser
) == '<'
824 && (PARSER_STATE (parser
)->language
->la_language
826 && (PARSER_STREAM (parser
) - start
) >= CP_OPERATOR_LEN
)
828 const char *op
= PARSER_STREAM (parser
);
830 while (op
> start
&& isspace (op
[-1]))
832 if (op
- start
>= CP_OPERATOR_LEN
)
834 op
-= CP_OPERATOR_LEN
;
835 if (strncmp (op
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0
837 || !(isalnum (op
[-1]) || op
[-1] == '_')))
839 /* This is an operator name. Keep going. */
840 ++(PARSER_STREAM (parser
));
841 if (*PARSER_STREAM (parser
) == '<')
842 ++(PARSER_STREAM (parser
));
848 const char *end
= find_parameter_list_end (PARSER_STREAM (parser
));
849 PARSER_STREAM (parser
) = end
;
851 /* Don't loop around to the normal \0 case above because
852 we don't want to misinterpret a potential keyword at
853 the end of the token when the string isn't
854 "()<>"-balanced. This handles "b
855 function(thread<tab>" in completion mode. */
858 LS_TOKEN_STOKEN (token
).ptr
= start
;
859 LS_TOKEN_STOKEN (token
).length
860 = PARSER_STREAM (parser
) - start
;
866 /* Commas are terminators, but not if they are part of an
868 else if (*PARSER_STREAM (parser
) == ',')
870 if ((PARSER_STATE (parser
)->language
->la_language
872 && (PARSER_STREAM (parser
) - start
) > CP_OPERATOR_LEN
)
874 const char *op
= strstr (start
, CP_OPERATOR_STR
);
876 if (op
!= NULL
&& is_operator_name (op
))
878 /* This is an operator name. Keep going. */
879 ++(PARSER_STREAM (parser
));
884 /* Comma terminates the string. */
885 LS_TOKEN_STOKEN (token
).ptr
= start
;
886 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
890 /* Advance the stream. */
891 gdb_assert (*(PARSER_STREAM (parser
)) != '\0');
892 ++(PARSER_STREAM (parser
));
899 /* Lex a single linespec token from PARSER. */
901 static linespec_token
902 linespec_lexer_lex_one (linespec_parser
*parser
)
906 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
908 /* Skip any whitespace. */
909 PARSER_STREAM (parser
) = skip_spaces (PARSER_STREAM (parser
));
911 /* Check for a keyword, they end the linespec. */
912 keyword
= linespec_lexer_lex_keyword (PARSER_STREAM (parser
));
915 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
916 LS_TOKEN_KEYWORD (parser
->lexer
.current
) = keyword
;
917 /* We do not advance the stream here intentionally:
918 we would like lexing to stop when a keyword is seen.
920 PARSER_STREAM (parser) += strlen (keyword); */
922 return parser
->lexer
.current
;
925 /* Handle other tokens. */
926 switch (*PARSER_STREAM (parser
))
929 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
933 case '0': case '1': case '2': case '3': case '4':
934 case '5': case '6': case '7': case '8': case '9':
935 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
936 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
940 /* If we have a scope operator, lex the input as a string.
941 Otherwise, return LSTOKEN_COLON. */
942 if (PARSER_STREAM (parser
)[1] == ':')
943 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
946 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
947 ++(PARSER_STREAM (parser
));
951 case '\'': case '\"':
952 /* Special case: permit quote-enclosed linespecs. */
953 if (parser
->is_quote_enclosed
954 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
956 ++(PARSER_STREAM (parser
));
957 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
960 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
964 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
965 LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
966 = PARSER_STREAM (parser
);
967 LS_TOKEN_STOKEN (parser
->lexer
.current
).length
= 1;
968 ++(PARSER_STREAM (parser
));
972 /* If the input is not a number, it must be a string.
973 [Keywords were already considered above.] */
974 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
979 return parser
->lexer
.current
;
982 /* Consume the current token and return the next token in PARSER's
983 input stream. Also advance the completion word for completion
986 static linespec_token
987 linespec_lexer_consume_token (linespec_parser
*parser
)
989 gdb_assert (parser
->lexer
.current
.type
!= LSTOKEN_EOI
);
991 bool advance_word
= (parser
->lexer
.current
.type
!= LSTOKEN_STRING
992 || *PARSER_STREAM (parser
) != '\0');
994 /* If we're moving past a string to some other token, it must be the
995 quote was terminated. */
996 if (parser
->completion_quote_char
)
998 gdb_assert (parser
->lexer
.current
.type
== LSTOKEN_STRING
);
1000 /* If the string was the last (non-EOI) token, we're past the
1001 quote, but remember that for later. */
1002 if (*PARSER_STREAM (parser
) != '\0')
1004 parser
->completion_quote_char
= '\0';
1005 parser
->completion_quote_end
= NULL
;;
1009 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
1010 linespec_lexer_lex_one (parser
);
1012 if (parser
->lexer
.current
.type
== LSTOKEN_STRING
)
1014 /* Advance the completion word past a potential initial
1016 parser
->completion_word
= LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
;
1018 else if (advance_word
)
1020 /* Advance the completion word past any whitespace. */
1021 parser
->completion_word
= PARSER_STREAM (parser
);
1024 return parser
->lexer
.current
;
1027 /* Return the next token without consuming the current token. */
1029 static linespec_token
1030 linespec_lexer_peek_token (linespec_parser
*parser
)
1032 linespec_token next
;
1033 const char *saved_stream
= PARSER_STREAM (parser
);
1034 linespec_token saved_token
= parser
->lexer
.current
;
1035 int saved_completion_quote_char
= parser
->completion_quote_char
;
1036 const char *saved_completion_quote_end
= parser
->completion_quote_end
;
1037 const char *saved_completion_word
= parser
->completion_word
;
1039 next
= linespec_lexer_consume_token (parser
);
1040 PARSER_STREAM (parser
) = saved_stream
;
1041 parser
->lexer
.current
= saved_token
;
1042 parser
->completion_quote_char
= saved_completion_quote_char
;
1043 parser
->completion_quote_end
= saved_completion_quote_end
;
1044 parser
->completion_word
= saved_completion_word
;
1048 /* Helper functions. */
1050 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1051 the new sal, if needed. If not NULL, SYMNAME is the name of the
1052 symbol to use when constructing the new canonical name.
1054 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1055 canonical name for the SAL. */
1058 add_sal_to_sals (struct linespec_state
*self
,
1059 std::vector
<symtab_and_line
> *sals
,
1060 struct symtab_and_line
*sal
,
1061 const char *symname
, int literal_canonical
)
1063 sals
->push_back (*sal
);
1065 if (self
->canonical
)
1067 struct linespec_canonical_name
*canonical
;
1069 self
->canonical_names
= XRESIZEVEC (struct linespec_canonical_name
,
1070 self
->canonical_names
,
1072 canonical
= &self
->canonical_names
[sals
->size () - 1];
1073 if (!literal_canonical
&& sal
->symtab
)
1075 symtab_to_fullname (sal
->symtab
);
1077 /* Note that the filter doesn't have to be a valid linespec
1078 input. We only apply the ":LINE" treatment to Ada for
1080 if (symname
!= NULL
&& sal
->line
!= 0
1081 && self
->language
->la_language
== language_ada
)
1082 canonical
->suffix
= xstrprintf ("%s:%d", symname
, sal
->line
);
1083 else if (symname
!= NULL
)
1084 canonical
->suffix
= xstrdup (symname
);
1086 canonical
->suffix
= xstrprintf ("%d", sal
->line
);
1087 canonical
->symtab
= sal
->symtab
;
1091 if (symname
!= NULL
)
1092 canonical
->suffix
= xstrdup (symname
);
1094 canonical
->suffix
= xstrdup ("<unknown>");
1095 canonical
->symtab
= NULL
;
1100 /* A hash function for address_entry. */
1103 hash_address_entry (const void *p
)
1105 const struct address_entry
*aep
= (const struct address_entry
*) p
;
1108 hash
= iterative_hash_object (aep
->pspace
, 0);
1109 return iterative_hash_object (aep
->addr
, hash
);
1112 /* An equality function for address_entry. */
1115 eq_address_entry (const void *a
, const void *b
)
1117 const struct address_entry
*aea
= (const struct address_entry
*) a
;
1118 const struct address_entry
*aeb
= (const struct address_entry
*) b
;
1120 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
1123 /* Check whether the address, represented by PSPACE and ADDR, is
1124 already in the set. If so, return 0. Otherwise, add it and return
1128 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
1130 struct address_entry e
, *p
;
1135 slot
= htab_find_slot (set
, &e
, INSERT
);
1139 p
= XNEW (struct address_entry
);
1140 memcpy (p
, &e
, sizeof (struct address_entry
));
1146 /* A helper that walks over all matching symtabs in all objfiles and
1147 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1148 not NULL, then the search is restricted to just that program
1149 space. If INCLUDE_INLINE is true then symbols representing
1150 inlined instances of functions will be included in the result. */
1153 iterate_over_all_matching_symtabs
1154 (struct linespec_state
*state
,
1155 const lookup_name_info
&lookup_name
,
1156 const domain_enum name_domain
,
1157 enum search_domain search_domain
,
1158 struct program_space
*search_pspace
, bool include_inline
,
1159 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1161 for (struct program_space
*pspace
: program_spaces
)
1163 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
1165 if (pspace
->executing_startup
)
1168 set_current_program_space (pspace
);
1170 for (objfile
*objfile
: current_program_space
->objfiles ())
1172 objfile
->expand_symtabs_matching (NULL
, &lookup_name
, NULL
, NULL
,
1175 for (compunit_symtab
*cu
: objfile
->compunits ())
1177 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1179 iterate_over_file_blocks (symtab
, lookup_name
, name_domain
,
1184 const struct block
*block
;
1187 for (i
= FIRST_LOCAL_BLOCK
;
1188 i
< BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab
));
1191 block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), i
);
1192 state
->language
->iterate_over_symbols
1193 (block
, lookup_name
, name_domain
,
1194 [&] (block_symbol
*bsym
)
1196 /* Restrict calls to CALLBACK to symbols
1197 representing inline symbols only. */
1198 if (SYMBOL_INLINED (bsym
->symbol
))
1199 return callback (bsym
);
1209 /* Returns the block to be used for symbol searches from
1210 the current location. */
1212 static const struct block
*
1213 get_current_search_block (void)
1215 /* get_selected_block can change the current language when there is
1216 no selected frame yet. */
1217 scoped_restore_current_language save_language
;
1218 return get_selected_block (0);
1221 /* Iterate over static and global blocks. */
1224 iterate_over_file_blocks
1225 (struct symtab
*symtab
, const lookup_name_info
&name
,
1226 domain_enum domain
, gdb::function_view
<symbol_found_callback_ftype
> callback
)
1228 const struct block
*block
;
1230 for (block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1232 block
= BLOCK_SUPERBLOCK (block
))
1233 current_language
->iterate_over_symbols (block
, name
, domain
, callback
);
1236 /* A helper for find_method. This finds all methods in type T of
1237 language T_LANG which match NAME. It adds matching symbol names to
1238 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
1241 find_methods (struct type
*t
, enum language t_lang
, const char *name
,
1242 std::vector
<const char *> *result_names
,
1243 std::vector
<struct type
*> *superclasses
)
1246 const char *class_name
= t
->name ();
1248 /* Ignore this class if it doesn't have a name. This is ugly, but
1249 unless we figure out how to get the physname without the name of
1250 the class, then the loop can't do any good. */
1254 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1255 symbol_name_matcher_ftype
*symbol_name_compare
1256 = language_def (t_lang
)->get_symbol_name_matcher (lookup_name
);
1258 t
= check_typedef (t
);
1260 /* Loop over each method name. At this level, all overloads of a name
1261 are counted as a single name. There is an inner loop which loops over
1264 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1265 method_counter
>= 0;
1268 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1270 if (symbol_name_compare (method_name
, lookup_name
, NULL
))
1274 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1280 const char *phys_name
;
1282 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1283 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1285 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1286 result_names
->push_back (phys_name
);
1292 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1293 superclasses
->push_back (TYPE_BASECLASS (t
, ibase
));
1296 /* Find an instance of the character C in the string S that is outside
1297 of all parenthesis pairs, single-quoted strings, and double-quoted
1298 strings. Also, ignore the char within a template name, like a ','
1299 within foo<int, int>, while considering C++ operator</operator<<. */
1302 find_toplevel_char (const char *s
, char c
)
1304 int quoted
= 0; /* zero if we're not in quotes;
1305 '"' if we're in a double-quoted string;
1306 '\'' if we're in a single-quoted string. */
1307 int depth
= 0; /* Number of unclosed parens we've seen. */
1310 for (scan
= s
; *scan
; scan
++)
1314 if (*scan
== quoted
)
1316 else if (*scan
== '\\' && *(scan
+ 1))
1319 else if (*scan
== c
&& ! quoted
&& depth
== 0)
1321 else if (*scan
== '"' || *scan
== '\'')
1323 else if (*scan
== '(' || *scan
== '<')
1325 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
1327 else if (*scan
== 'o' && !quoted
&& depth
== 0)
1329 /* Handle C++ operator names. */
1330 if (strncmp (scan
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0)
1332 scan
+= CP_OPERATOR_LEN
;
1335 while (isspace (*scan
))
1346 /* Skip over one less than the appropriate number of
1347 characters: the for loop will skip over the last
1373 /* The string equivalent of find_toplevel_char. Returns a pointer
1374 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1375 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1378 find_toplevel_string (const char *haystack
, const char *needle
)
1380 const char *s
= haystack
;
1384 s
= find_toplevel_char (s
, *needle
);
1388 /* Found first char in HAYSTACK; check rest of string. */
1389 if (startswith (s
, needle
))
1392 /* Didn't find it; loop over HAYSTACK, looking for the next
1393 instance of the first character of NEEDLE. */
1397 while (s
!= NULL
&& *s
!= '\0');
1399 /* NEEDLE was not found in HAYSTACK. */
1403 /* Convert CANONICAL to its string representation using
1404 symtab_to_fullname for SYMTAB. */
1407 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1409 if (canonical
->symtab
== NULL
)
1410 return canonical
->suffix
;
1412 return string_printf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1416 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1417 and store the result in SELF->CANONICAL. */
1420 filter_results (struct linespec_state
*self
,
1421 std::vector
<symtab_and_line
> *result
,
1422 const std::vector
<const char *> &filters
)
1424 for (const char *name
: filters
)
1428 for (size_t j
= 0; j
< result
->size (); ++j
)
1430 const struct linespec_canonical_name
*canonical
;
1432 canonical
= &self
->canonical_names
[j
];
1433 std::string fullform
= canonical_to_fullform (canonical
);
1435 if (name
== fullform
)
1436 lsal
.sals
.push_back ((*result
)[j
]);
1439 if (!lsal
.sals
.empty ())
1441 lsal
.canonical
= xstrdup (name
);
1442 self
->canonical
->lsals
.push_back (std::move (lsal
));
1446 self
->canonical
->pre_expanded
= 0;
1449 /* Store RESULT into SELF->CANONICAL. */
1452 convert_results_to_lsals (struct linespec_state
*self
,
1453 std::vector
<symtab_and_line
> *result
)
1455 struct linespec_sals lsal
;
1457 lsal
.canonical
= NULL
;
1458 lsal
.sals
= std::move (*result
);
1459 self
->canonical
->lsals
.push_back (std::move (lsal
));
1462 /* A structure that contains two string representations of a struct
1463 linespec_canonical_name:
1464 - one where the symtab's fullname is used;
1465 - one where the filename followed the "set filename-display"
1468 struct decode_line_2_item
1470 decode_line_2_item (std::string
&&fullform_
, std::string
&&displayform_
,
1472 : fullform (std::move (fullform_
)),
1473 displayform (std::move (displayform_
)),
1474 selected (selected_
)
1478 /* The form using symtab_to_fullname. */
1479 std::string fullform
;
1481 /* The form using symtab_to_filename_for_display. */
1482 std::string displayform
;
1484 /* Field is initialized to zero and it is set to one if the user
1485 requested breakpoint for this entry. */
1486 unsigned int selected
: 1;
1489 /* Helper for std::sort to sort decode_line_2_item entries by
1490 DISPLAYFORM and secondarily by FULLFORM. */
1493 decode_line_2_compare_items (const decode_line_2_item
&a
,
1494 const decode_line_2_item
&b
)
1496 if (a
.displayform
!= b
.displayform
)
1497 return a
.displayform
< b
.displayform
;
1498 return a
.fullform
< b
.fullform
;
1501 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1502 will either return normally, throw an exception on multiple
1503 results, or present a menu to the user. On return, the SALS vector
1504 in SELF->CANONICAL is set up properly. */
1507 decode_line_2 (struct linespec_state
*self
,
1508 std::vector
<symtab_and_line
> *result
,
1509 const char *select_mode
)
1514 std::vector
<const char *> filters
;
1515 std::vector
<struct decode_line_2_item
> items
;
1517 gdb_assert (select_mode
!= multiple_symbols_all
);
1518 gdb_assert (self
->canonical
!= NULL
);
1519 gdb_assert (!result
->empty ());
1521 /* Prepare ITEMS array. */
1522 for (i
= 0; i
< result
->size (); ++i
)
1524 const struct linespec_canonical_name
*canonical
;
1525 std::string displayform
;
1527 canonical
= &self
->canonical_names
[i
];
1528 gdb_assert (canonical
->suffix
!= NULL
);
1530 std::string fullform
= canonical_to_fullform (canonical
);
1532 if (canonical
->symtab
== NULL
)
1533 displayform
= canonical
->suffix
;
1536 const char *fn_for_display
;
1538 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1539 displayform
= string_printf ("%s:%s", fn_for_display
,
1543 items
.emplace_back (std::move (fullform
), std::move (displayform
),
1547 /* Sort the list of method names. */
1548 std::sort (items
.begin (), items
.end (), decode_line_2_compare_items
);
1550 /* Remove entries with the same FULLFORM. */
1551 items
.erase (std::unique (items
.begin (), items
.end (),
1552 [] (const struct decode_line_2_item
&a
,
1553 const struct decode_line_2_item
&b
)
1555 return a
.fullform
== b
.fullform
;
1559 if (select_mode
== multiple_symbols_cancel
&& items
.size () > 1)
1560 error (_("canceled because the command is ambiguous\n"
1561 "See set/show multiple-symbol."));
1563 if (select_mode
== multiple_symbols_all
|| items
.size () == 1)
1565 convert_results_to_lsals (self
, result
);
1569 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1570 for (i
= 0; i
< items
.size (); i
++)
1571 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
.c_str ());
1573 prompt
= getenv ("PS2");
1578 args
= command_line_input (prompt
, "overload-choice");
1580 if (args
== 0 || *args
== 0)
1581 error_no_arg (_("one or more choice numbers"));
1583 number_or_range_parser
parser (args
);
1584 while (!parser
.finished ())
1586 int num
= parser
.get_number ();
1589 error (_("canceled"));
1592 /* We intentionally make this result in a single breakpoint,
1593 contrary to what older versions of gdb did. The
1594 rationale is that this lets a user get the
1595 multiple_symbols_all behavior even with the 'ask'
1596 setting; and he can get separate breakpoints by entering
1597 "2-57" at the query. */
1598 convert_results_to_lsals (self
, result
);
1603 if (num
>= items
.size ())
1604 printf_unfiltered (_("No choice number %d.\n"), num
);
1607 struct decode_line_2_item
*item
= &items
[num
];
1609 if (!item
->selected
)
1611 filters
.push_back (item
->fullform
.c_str ());
1616 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1622 filter_results (self
, result
, filters
);
1627 /* The parser of linespec itself. */
1629 /* Throw an appropriate error when SYMBOL is not found (optionally in
1632 static void ATTRIBUTE_NORETURN
1633 symbol_not_found_error (const char *symbol
, const char *filename
)
1638 if (!have_full_symbols ()
1639 && !have_partial_symbols ()
1640 && !have_minimal_symbols ())
1641 throw_error (NOT_FOUND_ERROR
,
1642 _("No symbol table is loaded. Use the \"file\" command."));
1644 /* If SYMBOL starts with '$', the user attempted to either lookup
1645 a function/variable in his code starting with '$' or an internal
1646 variable of that name. Since we do not know which, be concise and
1647 explain both possibilities. */
1651 throw_error (NOT_FOUND_ERROR
,
1652 _("Undefined convenience variable or function \"%s\" "
1653 "not defined in \"%s\"."), symbol
, filename
);
1655 throw_error (NOT_FOUND_ERROR
,
1656 _("Undefined convenience variable or function \"%s\" "
1657 "not defined."), symbol
);
1662 throw_error (NOT_FOUND_ERROR
,
1663 _("Function \"%s\" not defined in \"%s\"."),
1666 throw_error (NOT_FOUND_ERROR
,
1667 _("Function \"%s\" not defined."), symbol
);
1671 /* Throw an appropriate error when an unexpected token is encountered
1674 static void ATTRIBUTE_NORETURN
1675 unexpected_linespec_error (linespec_parser
*parser
)
1677 linespec_token token
;
1678 static const char * token_type_strings
[]
1679 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1681 /* Get the token that generated the error. */
1682 token
= linespec_lexer_lex_one (parser
);
1684 /* Finally, throw the error. */
1685 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1686 || token
.type
== LSTOKEN_KEYWORD
)
1688 gdb::unique_xmalloc_ptr
<char> string
= copy_token_string (token
);
1689 throw_error (GENERIC_ERROR
,
1690 _("malformed linespec error: unexpected %s, \"%s\""),
1691 token_type_strings
[token
.type
], string
.get ());
1694 throw_error (GENERIC_ERROR
,
1695 _("malformed linespec error: unexpected %s"),
1696 token_type_strings
[token
.type
]);
1699 /* Throw an undefined label error. */
1701 static void ATTRIBUTE_NORETURN
1702 undefined_label_error (const char *function
, const char *label
)
1704 if (function
!= NULL
)
1705 throw_error (NOT_FOUND_ERROR
,
1706 _("No label \"%s\" defined in function \"%s\"."),
1709 throw_error (NOT_FOUND_ERROR
,
1710 _("No label \"%s\" defined in current function."),
1714 /* Throw a source file not found error. */
1716 static void ATTRIBUTE_NORETURN
1717 source_file_not_found_error (const char *name
)
1719 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), name
);
1722 /* Unless at EIO, save the current stream position as completion word
1723 point, and consume the next token. */
1725 static linespec_token
1726 save_stream_and_consume_token (linespec_parser
*parser
)
1728 if (linespec_lexer_peek_token (parser
).type
!= LSTOKEN_EOI
)
1729 parser
->completion_word
= PARSER_STREAM (parser
);
1730 return linespec_lexer_consume_token (parser
);
1733 /* See description in linespec.h. */
1736 linespec_parse_line_offset (const char *string
)
1738 const char *start
= string
;
1739 struct line_offset line_offset
= {0, LINE_OFFSET_NONE
};
1743 line_offset
.sign
= LINE_OFFSET_PLUS
;
1746 else if (*string
== '-')
1748 line_offset
.sign
= LINE_OFFSET_MINUS
;
1752 if (*string
!= '\0' && !isdigit (*string
))
1753 error (_("malformed line offset: \"%s\""), start
);
1755 /* Right now, we only allow base 10 for offsets. */
1756 line_offset
.offset
= atoi (string
);
1760 /* In completion mode, if the user is still typing the number, there's
1761 no possible completion to offer. But if there's already input past
1762 the number, setup to expect NEXT. */
1765 set_completion_after_number (linespec_parser
*parser
,
1766 linespec_complete_what next
)
1768 if (*PARSER_STREAM (parser
) == ' ')
1770 parser
->completion_word
= skip_spaces (PARSER_STREAM (parser
) + 1);
1771 parser
->complete_what
= next
;
1775 parser
->completion_word
= PARSER_STREAM (parser
);
1776 parser
->complete_what
= linespec_complete_what::NOTHING
;
1780 /* Parse the basic_spec in PARSER's input. */
1783 linespec_parse_basic (linespec_parser
*parser
)
1785 gdb::unique_xmalloc_ptr
<char> name
;
1786 linespec_token token
;
1787 std::vector
<block_symbol
> symbols
;
1788 std::vector
<block_symbol
> *labels
;
1789 std::vector
<bound_minimal_symbol
> minimal_symbols
;
1791 /* Get the next token. */
1792 token
= linespec_lexer_lex_one (parser
);
1794 /* If it is EOI or KEYWORD, issue an error. */
1795 if (token
.type
== LSTOKEN_KEYWORD
)
1797 parser
->complete_what
= linespec_complete_what::NOTHING
;
1798 unexpected_linespec_error (parser
);
1800 else if (token
.type
== LSTOKEN_EOI
)
1802 unexpected_linespec_error (parser
);
1804 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1805 else if (token
.type
== LSTOKEN_NUMBER
)
1807 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1809 /* Record the line offset and get the next token. */
1810 name
= copy_token_string (token
);
1811 PARSER_EXPLICIT (parser
)->line_offset
1812 = linespec_parse_line_offset (name
.get ());
1814 /* Get the next token. */
1815 token
= linespec_lexer_consume_token (parser
);
1817 /* If the next token is a comma, stop parsing and return. */
1818 if (token
.type
== LSTOKEN_COMMA
)
1820 parser
->complete_what
= linespec_complete_what::NOTHING
;
1824 /* If the next token is anything but EOI or KEYWORD, issue
1826 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1827 unexpected_linespec_error (parser
);
1830 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1833 /* Next token must be LSTOKEN_STRING. */
1834 if (token
.type
!= LSTOKEN_STRING
)
1836 parser
->complete_what
= linespec_complete_what::NOTHING
;
1837 unexpected_linespec_error (parser
);
1840 /* The current token will contain the name of a function, method,
1842 name
= copy_token_string (token
);
1844 if (parser
->completion_tracker
!= NULL
)
1846 /* If the function name ends with a ":", then this may be an
1847 incomplete "::" scope operator instead of a label separator.
1850 which should expand to:
1853 Do a tentative completion assuming the later. If we find
1854 completions, advance the stream past the colon token and make
1855 it part of the function name/token. */
1857 if (!parser
->completion_quote_char
1858 && strcmp (PARSER_STREAM (parser
), ":") == 0)
1860 completion_tracker tmp_tracker
;
1861 const char *source_filename
1862 = PARSER_EXPLICIT (parser
)->source_filename
;
1863 symbol_name_match_type match_type
1864 = PARSER_EXPLICIT (parser
)->func_name_match_type
;
1866 linespec_complete_function (tmp_tracker
,
1867 parser
->completion_word
,
1871 if (tmp_tracker
.have_completions ())
1873 PARSER_STREAM (parser
)++;
1874 LS_TOKEN_STOKEN (token
).length
++;
1876 name
.reset (savestring (parser
->completion_word
,
1877 (PARSER_STREAM (parser
)
1878 - parser
->completion_word
)));
1882 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1886 /* Try looking it up as a function/method. */
1887 find_linespec_symbols (PARSER_STATE (parser
),
1888 PARSER_RESULT (parser
)->file_symtabs
, name
.get (),
1889 PARSER_EXPLICIT (parser
)->func_name_match_type
,
1890 &symbols
, &minimal_symbols
);
1892 if (!symbols
.empty () || !minimal_symbols
.empty ())
1894 PARSER_RESULT (parser
)->function_symbols
1895 = new std::vector
<block_symbol
> (std::move (symbols
));
1896 PARSER_RESULT (parser
)->minimal_symbols
1897 = new std::vector
<bound_minimal_symbol
>
1898 (std::move (minimal_symbols
));
1899 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1903 /* NAME was not a function or a method. So it must be a label
1904 name or user specified variable like "break foo.c:$zippo". */
1905 labels
= find_label_symbols (PARSER_STATE (parser
), NULL
,
1906 &symbols
, name
.get ());
1909 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1910 PARSER_RESULT (parser
)->labels
.function_symbols
1911 = new std::vector
<block_symbol
> (std::move (symbols
));
1912 PARSER_EXPLICIT (parser
)->label_name
= name
.release ();
1914 else if (token
.type
== LSTOKEN_STRING
1915 && *LS_TOKEN_STOKEN (token
).ptr
== '$')
1917 /* User specified a convenience variable or history value. */
1918 PARSER_EXPLICIT (parser
)->line_offset
1919 = linespec_parse_variable (PARSER_STATE (parser
), name
.get ());
1921 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
)
1923 /* The user-specified variable was not valid. Do not
1924 throw an error here. parse_linespec will do it for us. */
1925 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1931 /* The name is also not a label. Abort parsing. Do not throw
1932 an error here. parse_linespec will do it for us. */
1934 /* Save a copy of the name we were trying to lookup. */
1935 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1941 int previous_qc
= parser
->completion_quote_char
;
1943 /* Get the next token. */
1944 token
= linespec_lexer_consume_token (parser
);
1946 if (token
.type
== LSTOKEN_EOI
)
1948 if (previous_qc
&& !parser
->completion_quote_char
)
1949 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1951 else if (token
.type
== LSTOKEN_COLON
)
1953 /* User specified a label or a lineno. */
1954 token
= linespec_lexer_consume_token (parser
);
1956 if (token
.type
== LSTOKEN_NUMBER
)
1958 /* User specified an offset. Record the line offset and
1959 get the next token. */
1960 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1962 name
= copy_token_string (token
);
1963 PARSER_EXPLICIT (parser
)->line_offset
1964 = linespec_parse_line_offset (name
.get ());
1966 /* Get the next token. */
1967 token
= linespec_lexer_consume_token (parser
);
1969 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
1971 parser
->complete_what
= linespec_complete_what::LABEL
;
1973 else if (token
.type
== LSTOKEN_STRING
)
1975 parser
->complete_what
= linespec_complete_what::LABEL
;
1977 /* If we have text after the label separated by whitespace
1978 (e.g., "b func():lab i<tab>"), don't consider it part of
1979 the label. In completion mode that should complete to
1980 "if", in normal mode, the 'i' should be treated as
1982 if (parser
->completion_quote_char
== '\0')
1984 const char *ptr
= LS_TOKEN_STOKEN (token
).ptr
;
1985 for (size_t i
= 0; i
< LS_TOKEN_STOKEN (token
).length
; i
++)
1989 LS_TOKEN_STOKEN (token
).length
= i
;
1990 PARSER_STREAM (parser
) = skip_spaces (ptr
+ i
+ 1);
1996 if (parser
->completion_tracker
!= NULL
)
1998 if (PARSER_STREAM (parser
)[-1] == ' ')
2000 parser
->completion_word
= PARSER_STREAM (parser
);
2001 parser
->complete_what
= linespec_complete_what::KEYWORD
;
2006 /* Grab a copy of the label's name and look it up. */
2007 name
= copy_token_string (token
);
2009 = find_label_symbols (PARSER_STATE (parser
),
2010 PARSER_RESULT (parser
)->function_symbols
,
2011 &symbols
, name
.get ());
2015 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
2016 PARSER_RESULT (parser
)->labels
.function_symbols
2017 = new std::vector
<block_symbol
> (std::move (symbols
));
2018 PARSER_EXPLICIT (parser
)->label_name
= name
.release ();
2022 /* We don't know what it was, but it isn't a label. */
2023 undefined_label_error
2024 (PARSER_EXPLICIT (parser
)->function_name
, name
.get ());
2029 /* Check for a line offset. */
2030 token
= save_stream_and_consume_token (parser
);
2031 if (token
.type
== LSTOKEN_COLON
)
2033 /* Get the next token. */
2034 token
= linespec_lexer_consume_token (parser
);
2036 /* It must be a line offset. */
2037 if (token
.type
!= LSTOKEN_NUMBER
)
2038 unexpected_linespec_error (parser
);
2040 /* Record the line offset and get the next token. */
2041 name
= copy_token_string (token
);
2043 PARSER_EXPLICIT (parser
)->line_offset
2044 = linespec_parse_line_offset (name
.get ());
2046 /* Get the next token. */
2047 token
= linespec_lexer_consume_token (parser
);
2052 /* Trailing ':' in the input. Issue an error. */
2053 unexpected_linespec_error (parser
);
2058 /* Canonicalize the linespec contained in LS. The result is saved into
2059 STATE->canonical. This function handles both linespec and explicit
2063 canonicalize_linespec (struct linespec_state
*state
, const linespec_p ls
)
2065 struct event_location
*canon
;
2066 struct explicit_location
*explicit_loc
;
2068 /* If canonicalization was not requested, no need to do anything. */
2069 if (!state
->canonical
)
2072 /* Save everything as an explicit location. */
2073 state
->canonical
->location
2074 = new_explicit_location (&ls
->explicit_loc
);
2075 canon
= state
->canonical
->location
.get ();
2076 explicit_loc
= get_explicit_location (canon
);
2078 if (explicit_loc
->label_name
!= NULL
)
2080 state
->canonical
->special_display
= 1;
2082 if (explicit_loc
->function_name
== NULL
)
2084 /* No function was specified, so add the symbol name. */
2085 gdb_assert (!ls
->labels
.function_symbols
->empty ()
2086 && (ls
->labels
.function_symbols
->size () == 1));
2087 block_symbol s
= ls
->labels
.function_symbols
->front ();
2088 explicit_loc
->function_name
= xstrdup (s
.symbol
->natural_name ());
2092 /* If this location originally came from a linespec, save a string
2093 representation of it for display and saving to file. */
2094 if (state
->is_linespec
)
2096 char *linespec
= explicit_location_to_linespec (explicit_loc
);
2098 set_event_location_string (canon
, linespec
);
2103 /* Given a line offset in LS, construct the relevant SALs. */
2105 static std::vector
<symtab_and_line
>
2106 create_sals_line_offset (struct linespec_state
*self
,
2109 int use_default
= 0;
2111 /* This is where we need to make sure we have good defaults.
2112 We must guarantee that this section of code is never executed
2113 when we are called with just a function name, since
2114 set_default_source_symtab_and_line uses
2115 select_source_symtab that calls us with such an argument. */
2117 if (ls
->file_symtabs
->size () == 1
2118 && ls
->file_symtabs
->front () == nullptr)
2120 set_current_program_space (self
->program_space
);
2122 /* Make sure we have at least a default source line. */
2123 set_default_source_symtab_and_line ();
2124 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
2126 = collect_symtabs_from_filename (self
->default_symtab
->filename
,
2127 self
->search_pspace
);
2131 symtab_and_line val
;
2132 val
.line
= ls
->explicit_loc
.line_offset
.offset
;
2133 switch (ls
->explicit_loc
.line_offset
.sign
)
2135 case LINE_OFFSET_PLUS
:
2136 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2139 val
.line
= self
->default_line
+ val
.line
;
2142 case LINE_OFFSET_MINUS
:
2143 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2146 val
.line
= self
->default_line
- val
.line
;
2148 val
.line
= -val
.line
;
2151 case LINE_OFFSET_NONE
:
2152 break; /* No need to adjust val.line. */
2155 std::vector
<symtab_and_line
> values
;
2156 if (self
->list_mode
)
2157 values
= decode_digits_list_mode (self
, ls
, val
);
2160 struct linetable_entry
*best_entry
= NULL
;
2163 std::vector
<symtab_and_line
> intermediate_results
2164 = decode_digits_ordinary (self
, ls
, val
.line
, &best_entry
);
2165 if (intermediate_results
.empty () && best_entry
!= NULL
)
2166 intermediate_results
= decode_digits_ordinary (self
, ls
,
2170 /* For optimized code, the compiler can scatter one source line
2171 across disjoint ranges of PC values, even when no duplicate
2172 functions or inline functions are involved. For example,
2173 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2174 function can result in two PC ranges. In this case, we don't
2175 want to set a breakpoint on the first PC of each range. To filter
2176 such cases, we use containing blocks -- for each PC found
2177 above, we see if there are other PCs that are in the same
2178 block. If yes, the other PCs are filtered out. */
2180 gdb::def_vector
<int> filter (intermediate_results
.size ());
2181 gdb::def_vector
<const block
*> blocks (intermediate_results
.size ());
2183 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2185 set_current_program_space (intermediate_results
[i
].pspace
);
2188 blocks
[i
] = block_for_pc_sect (intermediate_results
[i
].pc
,
2189 intermediate_results
[i
].section
);
2192 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2194 if (blocks
[i
] != NULL
)
2195 for (j
= i
+ 1; j
< intermediate_results
.size (); ++j
)
2197 if (blocks
[j
] == blocks
[i
])
2205 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2208 struct symbol
*sym
= (blocks
[i
]
2209 ? block_containing_function (blocks
[i
])
2212 if (self
->funfirstline
)
2213 skip_prologue_sal (&intermediate_results
[i
]);
2214 intermediate_results
[i
].symbol
= sym
;
2215 add_sal_to_sals (self
, &values
, &intermediate_results
[i
],
2216 sym
? sym
->natural_name () : NULL
, 0);
2220 if (values
.empty ())
2222 if (ls
->explicit_loc
.source_filename
)
2223 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
2224 val
.line
, ls
->explicit_loc
.source_filename
);
2226 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
2233 /* Convert the given ADDRESS into SaLs. */
2235 static std::vector
<symtab_and_line
>
2236 convert_address_location_to_sals (struct linespec_state
*self
,
2239 symtab_and_line sal
= find_pc_line (address
, 0);
2241 sal
.section
= find_pc_overlay (address
);
2242 sal
.explicit_pc
= 1;
2243 sal
.symbol
= find_pc_sect_containing_function (sal
.pc
, sal
.section
);
2245 std::vector
<symtab_and_line
> sals
;
2246 add_sal_to_sals (self
, &sals
, &sal
, core_addr_to_string (address
), 1);
2251 /* Create and return SALs from the linespec LS. */
2253 static std::vector
<symtab_and_line
>
2254 convert_linespec_to_sals (struct linespec_state
*state
, linespec_p ls
)
2256 std::vector
<symtab_and_line
> sals
;
2258 if (ls
->labels
.label_symbols
!= NULL
)
2260 /* We have just a bunch of functions/methods or labels. */
2261 struct symtab_and_line sal
;
2263 for (const auto &sym
: *ls
->labels
.label_symbols
)
2265 struct program_space
*pspace
2266 = SYMTAB_PSPACE (symbol_symtab (sym
.symbol
));
2268 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2269 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2270 add_sal_to_sals (state
, &sals
, &sal
,
2271 sym
.symbol
->natural_name (), 0);
2274 else if (ls
->function_symbols
!= NULL
|| ls
->minimal_symbols
!= NULL
)
2276 /* We have just a bunch of functions and/or methods. */
2277 if (ls
->function_symbols
!= NULL
)
2279 /* Sort symbols so that symbols with the same program space are next
2281 std::sort (ls
->function_symbols
->begin (),
2282 ls
->function_symbols
->end (),
2285 for (const auto &sym
: *ls
->function_symbols
)
2287 program_space
*pspace
2288 = SYMTAB_PSPACE (symbol_symtab (sym
.symbol
));
2289 set_current_program_space (pspace
);
2291 /* Don't skip to the first line of the function if we
2292 had found an ifunc minimal symbol for this function,
2293 because that means that this function is an ifunc
2294 resolver with the same name as the ifunc itself. */
2295 bool found_ifunc
= false;
2297 if (state
->funfirstline
2298 && ls
->minimal_symbols
!= NULL
2299 && SYMBOL_CLASS (sym
.symbol
) == LOC_BLOCK
)
2301 const CORE_ADDR addr
2302 = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
.symbol
));
2304 for (const auto &elem
: *ls
->minimal_symbols
)
2306 if (MSYMBOL_TYPE (elem
.minsym
) == mst_text_gnu_ifunc
2307 || MSYMBOL_TYPE (elem
.minsym
) == mst_data_gnu_ifunc
)
2309 CORE_ADDR msym_addr
= BMSYMBOL_VALUE_ADDRESS (elem
);
2310 if (MSYMBOL_TYPE (elem
.minsym
) == mst_data_gnu_ifunc
)
2312 struct gdbarch
*gdbarch
2313 = elem
.objfile
->arch ();
2315 = (gdbarch_convert_from_func_ptr_addr
2318 current_inferior ()->top_target ()));
2321 if (msym_addr
== addr
)
2332 symtab_and_line sal
;
2333 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2334 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2335 add_sal_to_sals (state
, &sals
, &sal
,
2336 sym
.symbol
->natural_name (), 0);
2341 if (ls
->minimal_symbols
!= NULL
)
2343 /* Sort minimal symbols by program space, too */
2344 std::sort (ls
->minimal_symbols
->begin (),
2345 ls
->minimal_symbols
->end (),
2348 for (const auto &elem
: *ls
->minimal_symbols
)
2350 program_space
*pspace
= elem
.objfile
->pspace
;
2351 set_current_program_space (pspace
);
2352 minsym_found (state
, elem
.objfile
, elem
.minsym
, &sals
);
2356 else if (ls
->explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2358 /* Only an offset was specified. */
2359 sals
= create_sals_line_offset (state
, ls
);
2361 /* Make sure we have a filename for canonicalization. */
2362 if (ls
->explicit_loc
.source_filename
== NULL
)
2364 const char *fullname
= symtab_to_fullname (state
->default_symtab
);
2366 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2367 form so that displaying SOURCE_FILENAME can follow the current
2368 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2369 it has been kept for code simplicity only in absolute form. */
2370 ls
->explicit_loc
.source_filename
= xstrdup (fullname
);
2375 /* We haven't found any results... */
2379 canonicalize_linespec (state
, ls
);
2381 if (!sals
.empty () && state
->canonical
!= NULL
)
2382 state
->canonical
->pre_expanded
= 1;
2387 /* Build RESULT from the explicit location components SOURCE_FILENAME,
2388 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2391 convert_explicit_location_to_linespec (struct linespec_state
*self
,
2393 const char *source_filename
,
2394 const char *function_name
,
2395 symbol_name_match_type fname_match_type
,
2396 const char *label_name
,
2397 struct line_offset line_offset
)
2399 std::vector
<block_symbol
> symbols
;
2400 std::vector
<block_symbol
> *labels
;
2401 std::vector
<bound_minimal_symbol
> minimal_symbols
;
2403 result
->explicit_loc
.func_name_match_type
= fname_match_type
;
2405 if (source_filename
!= NULL
)
2409 *result
->file_symtabs
2410 = symtabs_from_filename (source_filename
, self
->search_pspace
);
2412 catch (const gdb_exception_error
&except
)
2414 source_file_not_found_error (source_filename
);
2416 result
->explicit_loc
.source_filename
= xstrdup (source_filename
);
2420 /* A NULL entry means to use the default symtab. */
2421 result
->file_symtabs
->push_back (nullptr);
2424 if (function_name
!= NULL
)
2426 find_linespec_symbols (self
, result
->file_symtabs
,
2427 function_name
, fname_match_type
,
2428 &symbols
, &minimal_symbols
);
2430 if (symbols
.empty () && minimal_symbols
.empty ())
2431 symbol_not_found_error (function_name
,
2432 result
->explicit_loc
.source_filename
);
2434 result
->explicit_loc
.function_name
= xstrdup (function_name
);
2435 result
->function_symbols
2436 = new std::vector
<block_symbol
> (std::move (symbols
));
2437 result
->minimal_symbols
2438 = new std::vector
<bound_minimal_symbol
> (std::move (minimal_symbols
));
2441 if (label_name
!= NULL
)
2443 labels
= find_label_symbols (self
, result
->function_symbols
,
2444 &symbols
, label_name
);
2447 undefined_label_error (result
->explicit_loc
.function_name
,
2450 result
->explicit_loc
.label_name
= xstrdup (label_name
);
2451 result
->labels
.label_symbols
= labels
;
2452 result
->labels
.function_symbols
2453 = new std::vector
<block_symbol
> (std::move (symbols
));
2456 if (line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2457 result
->explicit_loc
.line_offset
= line_offset
;
2460 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2462 static std::vector
<symtab_and_line
>
2463 convert_explicit_location_to_sals (struct linespec_state
*self
,
2465 const struct explicit_location
*explicit_loc
)
2467 convert_explicit_location_to_linespec (self
, result
,
2468 explicit_loc
->source_filename
,
2469 explicit_loc
->function_name
,
2470 explicit_loc
->func_name_match_type
,
2471 explicit_loc
->label_name
,
2472 explicit_loc
->line_offset
);
2473 return convert_linespec_to_sals (self
, result
);
2476 /* Parse a string that specifies a linespec.
2478 The basic grammar of linespecs:
2480 linespec -> var_spec | basic_spec
2481 var_spec -> '$' (STRING | NUMBER)
2483 basic_spec -> file_offset_spec | function_spec | label_spec
2484 file_offset_spec -> opt_file_spec offset_spec
2485 function_spec -> opt_file_spec function_name_spec opt_label_spec
2486 label_spec -> label_name_spec
2488 opt_file_spec -> "" | file_name_spec ':'
2489 opt_label_spec -> "" | ':' label_name_spec
2491 file_name_spec -> STRING
2492 function_name_spec -> STRING
2493 label_name_spec -> STRING
2494 function_name_spec -> STRING
2495 offset_spec -> NUMBER
2499 This may all be followed by several keywords such as "if EXPR",
2502 A comma will terminate parsing.
2504 The function may be an undebuggable function found in minimal symbol table.
2506 If the argument FUNFIRSTLINE is nonzero, we want the first line
2507 of real code inside a function when a function is specified, and it is
2508 not OK to specify a variable or type to get its line number.
2510 DEFAULT_SYMTAB specifies the file to use if none is specified.
2511 It defaults to current_source_symtab.
2512 DEFAULT_LINE specifies the line number to use for relative
2513 line numbers (that start with signs). Defaults to current_source_line.
2514 If CANONICAL is non-NULL, store an array of strings containing the canonical
2515 line specs there if necessary. Currently overloaded member functions and
2516 line numbers or static functions without a filename yield a canonical
2517 line spec. The array and the line spec strings are allocated on the heap,
2518 it is the callers responsibility to free them.
2520 Note that it is possible to return zero for the symtab
2521 if no file is validly specified. Callers must check that.
2522 Also, the line number returned may be invalid. */
2524 /* Parse the linespec in ARG. MATCH_TYPE indicates how function names
2525 should be matched. */
2527 static std::vector
<symtab_and_line
>
2528 parse_linespec (linespec_parser
*parser
, const char *arg
,
2529 symbol_name_match_type match_type
)
2531 linespec_token token
;
2532 struct gdb_exception file_exception
;
2534 /* A special case to start. It has become quite popular for
2535 IDEs to work around bugs in the previous parser by quoting
2536 the entire linespec, so we attempt to deal with this nicely. */
2537 parser
->is_quote_enclosed
= 0;
2538 if (parser
->completion_tracker
== NULL
2539 && !is_ada_operator (arg
)
2540 && strchr (linespec_quote_characters
, *arg
) != NULL
)
2544 end
= skip_quote_char (arg
+ 1, *arg
);
2545 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2547 /* Here's the special case. Skip ARG past the initial
2550 parser
->is_quote_enclosed
= 1;
2554 parser
->lexer
.saved_arg
= arg
;
2555 parser
->lexer
.stream
= arg
;
2556 parser
->completion_word
= arg
;
2557 parser
->complete_what
= linespec_complete_what::FUNCTION
;
2558 PARSER_EXPLICIT (parser
)->func_name_match_type
= match_type
;
2560 /* Initialize the default symtab and line offset. */
2561 initialize_defaults (&PARSER_STATE (parser
)->default_symtab
,
2562 &PARSER_STATE (parser
)->default_line
);
2564 /* Objective-C shortcut. */
2565 if (parser
->completion_tracker
== NULL
)
2567 std::vector
<symtab_and_line
> values
2568 = decode_objc (PARSER_STATE (parser
), PARSER_RESULT (parser
), arg
);
2569 if (!values
.empty ())
2574 /* "-"/"+" is either an objc selector, or a number. There's
2575 nothing to complete the latter to, so just let the caller
2576 complete on functions, which finds objc selectors, if there's
2578 if ((arg
[0] == '-' || arg
[0] == '+') && arg
[1] == '\0')
2582 /* Start parsing. */
2584 /* Get the first token. */
2585 token
= linespec_lexer_consume_token (parser
);
2587 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2588 if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '$')
2590 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2591 if (parser
->completion_tracker
== NULL
)
2592 PARSER_RESULT (parser
)->file_symtabs
->push_back (nullptr);
2594 /* User specified a convenience variable or history value. */
2595 gdb::unique_xmalloc_ptr
<char> var
= copy_token_string (token
);
2596 PARSER_EXPLICIT (parser
)->line_offset
2597 = linespec_parse_variable (PARSER_STATE (parser
), var
.get ());
2599 /* If a line_offset wasn't found (VAR is the name of a user
2600 variable/function), then skip to normal symbol processing. */
2601 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2603 /* Consume this token. */
2604 linespec_lexer_consume_token (parser
);
2606 goto convert_to_sals
;
2609 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
2611 /* Let the default linespec_complete_what::FUNCTION kick in. */
2612 unexpected_linespec_error (parser
);
2614 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2616 parser
->complete_what
= linespec_complete_what::NOTHING
;
2617 unexpected_linespec_error (parser
);
2620 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2621 this token cannot represent a filename. */
2622 token
= linespec_lexer_peek_token (parser
);
2624 if (token
.type
== LSTOKEN_COLON
)
2626 /* Get the current token again and extract the filename. */
2627 token
= linespec_lexer_lex_one (parser
);
2628 gdb::unique_xmalloc_ptr
<char> user_filename
= copy_token_string (token
);
2630 /* Check if the input is a filename. */
2633 *PARSER_RESULT (parser
)->file_symtabs
2634 = symtabs_from_filename (user_filename
.get (),
2635 PARSER_STATE (parser
)->search_pspace
);
2637 catch (gdb_exception_error
&ex
)
2639 file_exception
= std::move (ex
);
2642 if (file_exception
.reason
>= 0)
2644 /* Symtabs were found for the file. Record the filename. */
2645 PARSER_EXPLICIT (parser
)->source_filename
= user_filename
.release ();
2647 /* Get the next token. */
2648 token
= linespec_lexer_consume_token (parser
);
2650 /* This is LSTOKEN_COLON; consume it. */
2651 linespec_lexer_consume_token (parser
);
2655 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2656 PARSER_RESULT (parser
)->file_symtabs
->push_back (nullptr);
2659 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2660 else if (parser
->completion_tracker
== NULL
2661 && (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2662 && token
.type
!= LSTOKEN_COMMA
))
2664 /* TOKEN is the _next_ token, not the one currently in the parser.
2665 Consuming the token will give the correct error message. */
2666 linespec_lexer_consume_token (parser
);
2667 unexpected_linespec_error (parser
);
2671 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2672 PARSER_RESULT (parser
)->file_symtabs
->push_back (nullptr);
2675 /* Parse the rest of the linespec. */
2676 linespec_parse_basic (parser
);
2678 if (parser
->completion_tracker
== NULL
2679 && PARSER_RESULT (parser
)->function_symbols
== NULL
2680 && PARSER_RESULT (parser
)->labels
.label_symbols
== NULL
2681 && PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
2682 && PARSER_RESULT (parser
)->minimal_symbols
== NULL
)
2684 /* The linespec didn't parse. Re-throw the file exception if
2686 if (file_exception
.reason
< 0)
2687 throw_exception (std::move (file_exception
));
2689 /* Otherwise, the symbol is not found. */
2690 symbol_not_found_error (PARSER_EXPLICIT (parser
)->function_name
,
2691 PARSER_EXPLICIT (parser
)->source_filename
);
2696 /* Get the last token and record how much of the input was parsed,
2698 token
= linespec_lexer_lex_one (parser
);
2699 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2700 unexpected_linespec_error (parser
);
2701 else if (token
.type
== LSTOKEN_KEYWORD
)
2703 /* Setup the completion word past the keyword. Lexing never
2704 advances past a keyword automatically, so skip it
2706 parser
->completion_word
2707 = skip_spaces (skip_to_space (PARSER_STREAM (parser
)));
2708 parser
->complete_what
= linespec_complete_what::EXPRESSION
;
2711 /* Convert the data in PARSER_RESULT to SALs. */
2712 if (parser
->completion_tracker
== NULL
)
2713 return convert_linespec_to_sals (PARSER_STATE (parser
),
2714 PARSER_RESULT (parser
));
2720 /* A constructor for linespec_state. */
2723 linespec_state_constructor (struct linespec_state
*self
,
2724 int flags
, const struct language_defn
*language
,
2725 struct program_space
*search_pspace
,
2726 struct symtab
*default_symtab
,
2728 struct linespec_result
*canonical
)
2730 memset (self
, 0, sizeof (*self
));
2731 self
->language
= language
;
2732 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2733 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2734 self
->search_pspace
= search_pspace
;
2735 self
->default_symtab
= default_symtab
;
2736 self
->default_line
= default_line
;
2737 self
->canonical
= canonical
;
2738 self
->program_space
= current_program_space
;
2739 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2740 xfree
, xcalloc
, xfree
);
2741 self
->is_linespec
= 0;
2744 /* Initialize a new linespec parser. */
2746 linespec_parser::linespec_parser (int flags
,
2747 const struct language_defn
*language
,
2748 struct program_space
*search_pspace
,
2749 struct symtab
*default_symtab
,
2751 struct linespec_result
*canonical
)
2753 lexer
.current
.type
= LSTOKEN_CONSUMED
;
2754 PARSER_RESULT (this)->file_symtabs
= new std::vector
<symtab
*> ();
2755 PARSER_EXPLICIT (this)->func_name_match_type
2756 = symbol_name_match_type::WILD
;
2757 PARSER_EXPLICIT (this)->line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2758 linespec_state_constructor (PARSER_STATE (this), flags
, language
,
2760 default_symtab
, default_line
, canonical
);
2763 /* A destructor for linespec_state. */
2766 linespec_state_destructor (struct linespec_state
*self
)
2768 htab_delete (self
->addr_set
);
2769 xfree (self
->canonical_names
);
2772 /* Delete a linespec parser. */
2774 linespec_parser::~linespec_parser ()
2776 xfree (PARSER_EXPLICIT (this)->source_filename
);
2777 xfree (PARSER_EXPLICIT (this)->label_name
);
2778 xfree (PARSER_EXPLICIT (this)->function_name
);
2780 delete PARSER_RESULT (this)->file_symtabs
;
2781 delete PARSER_RESULT (this)->function_symbols
;
2782 delete PARSER_RESULT (this)->minimal_symbols
;
2783 delete PARSER_RESULT (this)->labels
.label_symbols
;
2784 delete PARSER_RESULT (this)->labels
.function_symbols
;
2786 linespec_state_destructor (PARSER_STATE (this));
2789 /* See description in linespec.h. */
2792 linespec_lex_to_end (const char **stringp
)
2794 linespec_token token
;
2797 if (stringp
== NULL
|| *stringp
== NULL
)
2800 linespec_parser
parser (0, current_language
, NULL
, NULL
, 0, NULL
);
2801 parser
.lexer
.saved_arg
= *stringp
;
2802 PARSER_STREAM (&parser
) = orig
= *stringp
;
2806 /* Stop before any comma tokens; we need it to keep it
2807 as the next token in the string. */
2808 token
= linespec_lexer_peek_token (&parser
);
2809 if (token
.type
== LSTOKEN_COMMA
)
2811 token
= linespec_lexer_consume_token (&parser
);
2813 while (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
);
2815 *stringp
+= PARSER_STREAM (&parser
) - orig
;
2818 /* See linespec.h. */
2821 linespec_complete_function (completion_tracker
&tracker
,
2822 const char *function
,
2823 symbol_name_match_type func_match_type
,
2824 const char *source_filename
)
2826 complete_symbol_mode mode
= complete_symbol_mode::LINESPEC
;
2828 if (source_filename
!= NULL
)
2830 collect_file_symbol_completion_matches (tracker
, mode
, func_match_type
,
2831 function
, function
, source_filename
);
2835 collect_symbol_completion_matches (tracker
, mode
, func_match_type
,
2836 function
, function
);
2841 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2842 only meaningful if COMPONENT is FUNCTION. */
2845 complete_linespec_component (linespec_parser
*parser
,
2846 completion_tracker
&tracker
,
2848 linespec_complete_what component
,
2849 const char *source_filename
)
2851 if (component
== linespec_complete_what::KEYWORD
)
2853 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
2855 else if (component
== linespec_complete_what::EXPRESSION
)
2858 = advance_to_expression_complete_word_point (tracker
, text
);
2859 complete_expression (tracker
, text
, word
);
2861 else if (component
== linespec_complete_what::FUNCTION
)
2863 completion_list fn_list
;
2865 symbol_name_match_type match_type
2866 = PARSER_EXPLICIT (parser
)->func_name_match_type
;
2867 linespec_complete_function (tracker
, text
, match_type
, source_filename
);
2868 if (source_filename
== NULL
)
2870 /* Haven't seen a source component, like in "b
2871 file.c:function[TAB]". Maybe this wasn't a function, but
2872 a filename instead, like "b file.[TAB]". */
2873 fn_list
= complete_source_filenames (text
);
2876 /* If we only have a single filename completion, append a ':' for
2877 the user, since that's the only thing that can usefully follow
2879 if (fn_list
.size () == 1 && !tracker
.have_completions ())
2881 char *fn
= fn_list
[0].release ();
2883 /* If we also need to append a quote char, it needs to be
2884 appended before the ':'. Append it now, and make ':' the
2885 new "quote" char. */
2886 if (tracker
.quote_char ())
2888 char quote_char_str
[2] = { (char) tracker
.quote_char () };
2890 fn
= reconcat (fn
, fn
, quote_char_str
, (char *) NULL
);
2891 tracker
.set_quote_char (':');
2894 fn
= reconcat (fn
, fn
, ":", (char *) NULL
);
2895 fn_list
[0].reset (fn
);
2897 /* Tell readline to skip appending a space. */
2898 tracker
.set_suppress_append_ws (true);
2900 tracker
.add_completions (std::move (fn_list
));
2904 /* Helper for linespec_complete_label. Find labels that match
2905 LABEL_NAME in the function symbols listed in the PARSER, and add
2906 them to the tracker. */
2909 complete_label (completion_tracker
&tracker
,
2910 linespec_parser
*parser
,
2911 const char *label_name
)
2913 std::vector
<block_symbol
> label_function_symbols
;
2914 std::vector
<block_symbol
> *labels
2915 = find_label_symbols (PARSER_STATE (parser
),
2916 PARSER_RESULT (parser
)->function_symbols
,
2917 &label_function_symbols
,
2920 if (labels
!= nullptr)
2922 for (const auto &label
: *labels
)
2924 char *match
= xstrdup (label
.symbol
->search_name ());
2925 tracker
.add_completion (gdb::unique_xmalloc_ptr
<char> (match
));
2931 /* See linespec.h. */
2934 linespec_complete_label (completion_tracker
&tracker
,
2935 const struct language_defn
*language
,
2936 const char *source_filename
,
2937 const char *function_name
,
2938 symbol_name_match_type func_name_match_type
,
2939 const char *label_name
)
2941 linespec_parser
parser (0, language
, NULL
, NULL
, 0, NULL
);
2943 line_offset unknown_offset
= { 0, LINE_OFFSET_UNKNOWN
};
2947 convert_explicit_location_to_linespec (PARSER_STATE (&parser
),
2948 PARSER_RESULT (&parser
),
2951 func_name_match_type
,
2952 NULL
, unknown_offset
);
2954 catch (const gdb_exception_error
&ex
)
2959 complete_label (tracker
, &parser
, label_name
);
2962 /* See description in linespec.h. */
2965 linespec_complete (completion_tracker
&tracker
, const char *text
,
2966 symbol_name_match_type match_type
)
2968 const char *orig
= text
;
2970 linespec_parser
parser (0, current_language
, NULL
, NULL
, 0, NULL
);
2971 parser
.lexer
.saved_arg
= text
;
2972 PARSER_EXPLICIT (&parser
)->func_name_match_type
= match_type
;
2973 PARSER_STREAM (&parser
) = text
;
2975 parser
.completion_tracker
= &tracker
;
2976 PARSER_STATE (&parser
)->is_linespec
= 1;
2978 /* Parse as much as possible. parser.completion_word will hold
2979 furthest completion point we managed to parse to. */
2982 parse_linespec (&parser
, text
, match_type
);
2984 catch (const gdb_exception_error
&except
)
2988 if (parser
.completion_quote_char
!= '\0'
2989 && parser
.completion_quote_end
!= NULL
2990 && parser
.completion_quote_end
[1] == '\0')
2992 /* If completing a quoted string with the cursor right at
2993 terminating quote char, complete the completion word without
2994 interpretation, so that readline advances the cursor one
2995 whitespace past the quote, even if there's no match. This
2996 makes these cases behave the same:
2998 before: "b function()"
2999 after: "b function() "
3001 before: "b 'function()'"
3002 after: "b 'function()' "
3004 and trusts the user in this case:
3006 before: "b 'not_loaded_function_yet()'"
3007 after: "b 'not_loaded_function_yet()' "
3009 parser
.complete_what
= linespec_complete_what::NOTHING
;
3010 parser
.completion_quote_char
= '\0';
3012 gdb::unique_xmalloc_ptr
<char> text_copy
3013 (xstrdup (parser
.completion_word
));
3014 tracker
.add_completion (std::move (text_copy
));
3017 tracker
.set_quote_char (parser
.completion_quote_char
);
3019 if (parser
.complete_what
== linespec_complete_what::LABEL
)
3021 parser
.complete_what
= linespec_complete_what::NOTHING
;
3023 const char *func_name
= PARSER_EXPLICIT (&parser
)->function_name
;
3025 std::vector
<block_symbol
> function_symbols
;
3026 std::vector
<bound_minimal_symbol
> minimal_symbols
;
3027 find_linespec_symbols (PARSER_STATE (&parser
),
3028 PARSER_RESULT (&parser
)->file_symtabs
,
3029 func_name
, match_type
,
3030 &function_symbols
, &minimal_symbols
);
3032 PARSER_RESULT (&parser
)->function_symbols
3033 = new std::vector
<block_symbol
> (std::move (function_symbols
));
3034 PARSER_RESULT (&parser
)->minimal_symbols
3035 = new std::vector
<bound_minimal_symbol
> (std::move (minimal_symbols
));
3037 complete_label (tracker
, &parser
, parser
.completion_word
);
3039 else if (parser
.complete_what
== linespec_complete_what::FUNCTION
)
3041 /* While parsing/lexing, we didn't know whether the completion
3042 word completes to a unique function/source name already or
3046 "b function() <tab>"
3047 may need to complete either to:
3048 "b function() const"
3050 "b function() if/thread/task"
3054 may need to complete either to:
3055 "b foo template_fun<T>()"
3056 with "foo" being the template function's return type, or to:
3061 may need to complete either to a source file name:
3063 or this, also a filename, but a unique completion:
3065 or to a function name:
3068 Address that by completing assuming source or function, and
3069 seeing if we find a completion that matches exactly the
3070 completion word. If so, then it must be a function (see note
3071 below) and we advance the completion word to the end of input
3072 and switch to KEYWORD completion mode.
3074 Note: if we find a unique completion for a source filename,
3075 then it won't match the completion word, because the LCD will
3076 contain a trailing ':'. And if we're completing at or after
3077 the ':', then complete_linespec_component won't try to
3078 complete on source filenames. */
3080 const char *word
= parser
.completion_word
;
3082 complete_linespec_component (&parser
, tracker
,
3083 parser
.completion_word
,
3084 linespec_complete_what::FUNCTION
,
3085 PARSER_EXPLICIT (&parser
)->source_filename
);
3087 parser
.complete_what
= linespec_complete_what::NOTHING
;
3089 if (tracker
.quote_char ())
3091 /* The function/file name was not close-quoted, so this
3092 can't be a keyword. Note: complete_linespec_component
3093 may have swapped the original quote char for ':' when we
3094 get here, but that still indicates the same. */
3096 else if (!tracker
.have_completions ())
3099 size_t wordlen
= strlen (parser
.completion_word
);
3102 = string_find_incomplete_keyword_at_end (linespec_keywords
,
3103 parser
.completion_word
,
3108 && parser
.completion_word
[wordlen
- 1] == ' '))
3110 parser
.completion_word
+= key_start
;
3111 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3114 else if (tracker
.completes_to_completion_word (word
))
3116 /* Skip the function and complete on keywords. */
3117 parser
.completion_word
+= strlen (word
);
3118 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3119 tracker
.discard_completions ();
3123 tracker
.advance_custom_word_point_by (parser
.completion_word
- orig
);
3125 complete_linespec_component (&parser
, tracker
,
3126 parser
.completion_word
,
3127 parser
.complete_what
,
3128 PARSER_EXPLICIT (&parser
)->source_filename
);
3130 /* If we're past the "filename:function:label:offset" linespec, and
3131 didn't find any match, then assume the user might want to create
3132 a pending breakpoint anyway and offer the keyword
3134 if (!parser
.completion_quote_char
3135 && (parser
.complete_what
== linespec_complete_what::FUNCTION
3136 || parser
.complete_what
== linespec_complete_what::LABEL
3137 || parser
.complete_what
== linespec_complete_what::NOTHING
)
3138 && !tracker
.have_completions ())
3141 = parser
.completion_word
+ strlen (parser
.completion_word
);
3143 if (end
> orig
&& end
[-1] == ' ')
3145 tracker
.advance_custom_word_point_by (end
- parser
.completion_word
);
3147 complete_linespec_component (&parser
, tracker
, end
,
3148 linespec_complete_what::KEYWORD
,
3154 /* A helper function for decode_line_full and decode_line_1 to
3155 turn LOCATION into std::vector<symtab_and_line>. */
3157 static std::vector
<symtab_and_line
>
3158 event_location_to_sals (linespec_parser
*parser
,
3159 const struct event_location
*location
)
3161 std::vector
<symtab_and_line
> result
;
3163 switch (event_location_type (location
))
3165 case LINESPEC_LOCATION
:
3167 PARSER_STATE (parser
)->is_linespec
= 1;
3170 const linespec_location
*ls
= get_linespec_location (location
);
3171 result
= parse_linespec (parser
,
3172 ls
->spec_string
, ls
->match_type
);
3174 catch (const gdb_exception_error
&except
)
3181 case ADDRESS_LOCATION
:
3183 const char *addr_string
= get_address_string_location (location
);
3184 CORE_ADDR addr
= get_address_location (location
);
3186 if (addr_string
!= NULL
)
3188 addr
= linespec_expression_to_pc (&addr_string
);
3189 if (PARSER_STATE (parser
)->canonical
!= NULL
)
3190 PARSER_STATE (parser
)->canonical
->location
3191 = copy_event_location (location
);
3194 result
= convert_address_location_to_sals (PARSER_STATE (parser
),
3199 case EXPLICIT_LOCATION
:
3201 const struct explicit_location
*explicit_loc
;
3203 explicit_loc
= get_explicit_location_const (location
);
3204 result
= convert_explicit_location_to_sals (PARSER_STATE (parser
),
3205 PARSER_RESULT (parser
),
3210 case PROBE_LOCATION
:
3211 /* Probes are handled by their own decoders. */
3212 gdb_assert_not_reached ("attempt to decode probe location");
3216 gdb_assert_not_reached ("unhandled event location type");
3222 /* See linespec.h. */
3225 decode_line_full (struct event_location
*location
, int flags
,
3226 struct program_space
*search_pspace
,
3227 struct symtab
*default_symtab
,
3228 int default_line
, struct linespec_result
*canonical
,
3229 const char *select_mode
,
3232 std::vector
<const char *> filters
;
3233 struct linespec_state
*state
;
3235 gdb_assert (canonical
!= NULL
);
3236 /* The filter only makes sense for 'all'. */
3237 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
3238 gdb_assert (select_mode
== NULL
3239 || select_mode
== multiple_symbols_all
3240 || select_mode
== multiple_symbols_ask
3241 || select_mode
== multiple_symbols_cancel
);
3242 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
3244 linespec_parser
parser (flags
, current_language
,
3245 search_pspace
, default_symtab
,
3246 default_line
, canonical
);
3248 scoped_restore_current_program_space restore_pspace
;
3250 std::vector
<symtab_and_line
> result
= event_location_to_sals (&parser
,
3252 state
= PARSER_STATE (&parser
);
3254 if (result
.size () == 0)
3255 throw_error (NOT_SUPPORTED_ERROR
, _("Location %s not available"),
3256 event_location_to_string (location
));
3258 gdb_assert (result
.size () == 1 || canonical
->pre_expanded
);
3259 canonical
->pre_expanded
= 1;
3261 /* Arrange for allocated canonical names to be freed. */
3262 std::vector
<gdb::unique_xmalloc_ptr
<char>> hold_names
;
3263 for (int i
= 0; i
< result
.size (); ++i
)
3265 gdb_assert (state
->canonical_names
[i
].suffix
!= NULL
);
3266 hold_names
.emplace_back (state
->canonical_names
[i
].suffix
);
3269 if (select_mode
== NULL
)
3271 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
3272 select_mode
= multiple_symbols_all
;
3274 select_mode
= multiple_symbols_select_mode ();
3277 if (select_mode
== multiple_symbols_all
)
3281 filters
.push_back (filter
);
3282 filter_results (state
, &result
, filters
);
3285 convert_results_to_lsals (state
, &result
);
3288 decode_line_2 (state
, &result
, select_mode
);
3291 /* See linespec.h. */
3293 std::vector
<symtab_and_line
>
3294 decode_line_1 (const struct event_location
*location
, int flags
,
3295 struct program_space
*search_pspace
,
3296 struct symtab
*default_symtab
,
3299 linespec_parser
parser (flags
, current_language
,
3300 search_pspace
, default_symtab
,
3301 default_line
, NULL
);
3303 scoped_restore_current_program_space restore_pspace
;
3305 return event_location_to_sals (&parser
, location
);
3308 /* See linespec.h. */
3310 std::vector
<symtab_and_line
>
3311 decode_line_with_current_source (const char *string
, int flags
)
3314 error (_("Empty line specification."));
3316 /* We use whatever is set as the current source line. We do not try
3317 and get a default source symtab+line or it will recursively call us! */
3318 symtab_and_line cursal
= get_current_source_symtab_and_line ();
3320 event_location_up location
= string_to_event_location (&string
,
3322 std::vector
<symtab_and_line
> sals
3323 = decode_line_1 (location
.get (), flags
, NULL
, cursal
.symtab
, cursal
.line
);
3326 error (_("Junk at end of line specification: %s"), string
);
3331 /* See linespec.h. */
3333 std::vector
<symtab_and_line
>
3334 decode_line_with_last_displayed (const char *string
, int flags
)
3337 error (_("Empty line specification."));
3339 event_location_up location
= string_to_event_location (&string
,
3341 std::vector
<symtab_and_line
> sals
3342 = (last_displayed_sal_is_valid ()
3343 ? decode_line_1 (location
.get (), flags
, NULL
,
3344 get_last_displayed_symtab (),
3345 get_last_displayed_line ())
3346 : decode_line_1 (location
.get (), flags
, NULL
, NULL
, 0));
3349 error (_("Junk at end of line specification: %s"), string
);
3356 /* First, some functions to initialize stuff at the beginning of the
3360 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
3362 if (*default_symtab
== 0)
3364 /* Use whatever we have for the default source line. We don't use
3365 get_current_or_default_symtab_and_line as it can recurse and call
3367 struct symtab_and_line cursal
=
3368 get_current_source_symtab_and_line ();
3370 *default_symtab
= cursal
.symtab
;
3371 *default_line
= cursal
.line
;
3377 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3378 advancing EXP_PTR past any parsed text. */
3381 linespec_expression_to_pc (const char **exp_ptr
)
3383 if (current_program_space
->executing_startup
)
3384 /* The error message doesn't really matter, because this case
3385 should only hit during breakpoint reset. */
3386 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
3387 "program space is in startup"));
3390 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
3395 /* Here's where we recognise an Objective-C Selector. An Objective C
3396 selector may be implemented by more than one class, therefore it
3397 may represent more than one method/function. This gives us a
3398 situation somewhat analogous to C++ overloading. If there's more
3399 than one method that could represent the selector, then use some of
3400 the existing C++ code to let the user choose one. */
3402 static std::vector
<symtab_and_line
>
3403 decode_objc (struct linespec_state
*self
, linespec_p ls
, const char *arg
)
3405 struct collect_info info
;
3406 std::vector
<const char *> symbol_names
;
3407 const char *new_argptr
;
3410 std::vector
<symtab
*> symtabs
;
3411 symtabs
.push_back (nullptr);
3413 info
.file_symtabs
= &symtabs
;
3415 std::vector
<block_symbol
> symbols
;
3416 info
.result
.symbols
= &symbols
;
3417 std::vector
<bound_minimal_symbol
> minimal_symbols
;
3418 info
.result
.minimal_symbols
= &minimal_symbols
;
3420 new_argptr
= find_imps (arg
, &symbol_names
);
3421 if (symbol_names
.empty ())
3424 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
,
3427 std::vector
<symtab_and_line
> values
;
3428 if (!symbols
.empty () || !minimal_symbols
.empty ())
3432 saved_arg
= (char *) alloca (new_argptr
- arg
+ 1);
3433 memcpy (saved_arg
, arg
, new_argptr
- arg
);
3434 saved_arg
[new_argptr
- arg
] = '\0';
3436 ls
->explicit_loc
.function_name
= xstrdup (saved_arg
);
3437 ls
->function_symbols
3438 = new std::vector
<block_symbol
> (std::move (symbols
));
3440 = new std::vector
<bound_minimal_symbol
> (std::move (minimal_symbols
));
3441 values
= convert_linespec_to_sals (self
, ls
);
3443 if (self
->canonical
)
3448 self
->canonical
->pre_expanded
= 1;
3450 if (ls
->explicit_loc
.source_filename
)
3452 holder
= string_printf ("%s:%s",
3453 ls
->explicit_loc
.source_filename
,
3455 str
= holder
.c_str ();
3460 self
->canonical
->location
3461 = new_linespec_location (&str
, symbol_name_match_type::FULL
);
3470 /* A function object that serves as symbol_found_callback_ftype
3471 callback for iterate_over_symbols. This is used by
3472 lookup_prefix_sym to collect type symbols. */
3473 class decode_compound_collector
3476 decode_compound_collector ()
3477 : m_unique_syms (htab_create_alloc (1, htab_hash_pointer
,
3478 htab_eq_pointer
, NULL
,
3483 /* Return all symbols collected. */
3484 std::vector
<block_symbol
> release_symbols ()
3486 return std::move (m_symbols
);
3489 /* Callable as a symbol_found_callback_ftype callback. */
3490 bool operator () (block_symbol
*bsym
);
3493 /* A hash table of all symbols we found. We use this to avoid
3494 adding any symbol more than once. */
3495 htab_up m_unique_syms
;
3497 /* The result vector. */
3498 std::vector
<block_symbol
> m_symbols
;
3502 decode_compound_collector::operator () (block_symbol
*bsym
)
3506 struct symbol
*sym
= bsym
->symbol
;
3508 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
3509 return true; /* Continue iterating. */
3511 t
= SYMBOL_TYPE (sym
);
3512 t
= check_typedef (t
);
3513 if (t
->code () != TYPE_CODE_STRUCT
3514 && t
->code () != TYPE_CODE_UNION
3515 && t
->code () != TYPE_CODE_NAMESPACE
)
3516 return true; /* Continue iterating. */
3518 slot
= htab_find_slot (m_unique_syms
.get (), sym
, INSERT
);
3522 m_symbols
.push_back (*bsym
);
3525 return true; /* Continue iterating. */
3530 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3532 static std::vector
<block_symbol
>
3533 lookup_prefix_sym (struct linespec_state
*state
,
3534 std::vector
<symtab
*> *file_symtabs
,
3535 const char *class_name
)
3537 decode_compound_collector collector
;
3539 lookup_name_info
lookup_name (class_name
, symbol_name_match_type::FULL
);
3541 for (const auto &elt
: *file_symtabs
)
3545 iterate_over_all_matching_symtabs (state
, lookup_name
,
3546 STRUCT_DOMAIN
, ALL_DOMAIN
,
3547 NULL
, false, collector
);
3548 iterate_over_all_matching_symtabs (state
, lookup_name
,
3549 VAR_DOMAIN
, ALL_DOMAIN
,
3550 NULL
, false, collector
);
3554 /* Program spaces that are executing startup should have
3555 been filtered out earlier. */
3556 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
3557 set_current_program_space (SYMTAB_PSPACE (elt
));
3558 iterate_over_file_blocks (elt
, lookup_name
, STRUCT_DOMAIN
, collector
);
3559 iterate_over_file_blocks (elt
, lookup_name
, VAR_DOMAIN
, collector
);
3563 return collector
.release_symbols ();
3566 /* A std::sort comparison function for symbols. The resulting order does
3567 not actually matter; we just need to be able to sort them so that
3568 symbols with the same program space end up next to each other. */
3571 compare_symbols (const block_symbol
&a
, const block_symbol
&b
)
3575 uia
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (a
.symbol
));
3576 uib
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (b
.symbol
));
3583 uia
= (uintptr_t) a
.symbol
;
3584 uib
= (uintptr_t) b
.symbol
;
3592 /* Like compare_symbols but for minimal symbols. */
3595 compare_msymbols (const bound_minimal_symbol
&a
, const bound_minimal_symbol
&b
)
3599 uia
= (uintptr_t) a
.objfile
->pspace
;
3600 uib
= (uintptr_t) a
.objfile
->pspace
;
3607 uia
= (uintptr_t) a
.minsym
;
3608 uib
= (uintptr_t) b
.minsym
;
3616 /* Look for all the matching instances of each symbol in NAMES. Only
3617 instances from PSPACE are considered; other program spaces are
3618 handled by our caller. If PSPACE is NULL, then all program spaces
3619 are considered. Results are stored into INFO. */
3622 add_all_symbol_names_from_pspace (struct collect_info
*info
,
3623 struct program_space
*pspace
,
3624 const std::vector
<const char *> &names
,
3625 enum search_domain search_domain
)
3627 for (const char *iter
: names
)
3628 add_matching_symbols_to_info (iter
,
3629 symbol_name_match_type::FULL
,
3630 search_domain
, info
, pspace
);
3634 find_superclass_methods (std::vector
<struct type
*> &&superclasses
,
3635 const char *name
, enum language name_lang
,
3636 std::vector
<const char *> *result_names
)
3638 size_t old_len
= result_names
->size ();
3642 std::vector
<struct type
*> new_supers
;
3644 for (type
*t
: superclasses
)
3645 find_methods (t
, name_lang
, name
, result_names
, &new_supers
);
3647 if (result_names
->size () != old_len
|| new_supers
.empty ())
3650 superclasses
= std::move (new_supers
);
3654 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3655 given by one of the symbols in SYM_CLASSES. Matches are returned
3656 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3659 find_method (struct linespec_state
*self
, std::vector
<symtab
*> *file_symtabs
,
3660 const char *class_name
, const char *method_name
,
3661 std::vector
<block_symbol
> *sym_classes
,
3662 std::vector
<block_symbol
> *symbols
,
3663 std::vector
<bound_minimal_symbol
> *minsyms
)
3665 size_t last_result_len
;
3666 std::vector
<struct type
*> superclass_vec
;
3667 std::vector
<const char *> result_names
;
3668 struct collect_info info
;
3670 /* Sort symbols so that symbols with the same program space are next
3672 std::sort (sym_classes
->begin (), sym_classes
->end (),
3676 info
.file_symtabs
= file_symtabs
;
3677 info
.result
.symbols
= symbols
;
3678 info
.result
.minimal_symbols
= minsyms
;
3680 /* Iterate over all the types, looking for the names of existing
3681 methods matching METHOD_NAME. If we cannot find a direct method in a
3682 given program space, then we consider inherited methods; this is
3683 not ideal (ideal would be to respect C++ hiding rules), but it
3684 seems good enough and is what GDB has historically done. We only
3685 need to collect the names because later we find all symbols with
3686 those names. This loop is written in a somewhat funny way
3687 because we collect data across the program space before deciding
3689 last_result_len
= 0;
3690 for (const auto &elt
: *sym_classes
)
3693 struct program_space
*pspace
;
3694 struct symbol
*sym
= elt
.symbol
;
3695 unsigned int ix
= &elt
- &*sym_classes
->begin ();
3697 /* Program spaces that are executing startup should have
3698 been filtered out earlier. */
3699 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
3700 gdb_assert (!pspace
->executing_startup
);
3701 set_current_program_space (pspace
);
3702 t
= check_typedef (SYMBOL_TYPE (sym
));
3703 find_methods (t
, sym
->language (),
3704 method_name
, &result_names
, &superclass_vec
);
3706 /* Handle all items from a single program space at once; and be
3707 sure not to miss the last batch. */
3708 if (ix
== sym_classes
->size () - 1
3710 != SYMTAB_PSPACE (symbol_symtab (sym_classes
->at (ix
+ 1).symbol
))))
3712 /* If we did not find a direct implementation anywhere in
3713 this program space, consider superclasses. */
3714 if (result_names
.size () == last_result_len
)
3715 find_superclass_methods (std::move (superclass_vec
), method_name
,
3716 sym
->language (), &result_names
);
3718 /* We have a list of candidate symbol names, so now we
3719 iterate over the symbol tables looking for all
3720 matches in this pspace. */
3721 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
,
3724 superclass_vec
.clear ();
3725 last_result_len
= result_names
.size ();
3729 if (!symbols
->empty () || !minsyms
->empty ())
3732 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3733 and other attempts to locate the symbol will be made. */
3734 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
3741 /* This function object is a callback for iterate_over_symtabs, used
3742 when collecting all matching symtabs. */
3744 class symtab_collector
3748 : m_symtab_table (htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
3753 /* Callable as a symbol_found_callback_ftype callback. */
3754 bool operator () (symtab
*sym
);
3756 /* Return an rvalue reference to the collected symtabs. */
3757 std::vector
<symtab
*> &&release_symtabs ()
3759 return std::move (m_symtabs
);
3763 /* The result vector of symtabs. */
3764 std::vector
<symtab
*> m_symtabs
;
3766 /* This is used to ensure the symtabs are unique. */
3767 htab_up m_symtab_table
;
3771 symtab_collector::operator () (struct symtab
*symtab
)
3775 slot
= htab_find_slot (m_symtab_table
.get (), symtab
, INSERT
);
3779 m_symtabs
.push_back (symtab
);
3787 /* Given a file name, return a list of all matching symtabs. If
3788 SEARCH_PSPACE is not NULL, the search is restricted to just that
3791 static std::vector
<symtab
*>
3792 collect_symtabs_from_filename (const char *file
,
3793 struct program_space
*search_pspace
)
3795 symtab_collector collector
;
3797 /* Find that file's data. */
3798 if (search_pspace
== NULL
)
3800 for (struct program_space
*pspace
: program_spaces
)
3802 if (pspace
->executing_startup
)
3805 set_current_program_space (pspace
);
3806 iterate_over_symtabs (file
, collector
);
3811 set_current_program_space (search_pspace
);
3812 iterate_over_symtabs (file
, collector
);
3815 return collector
.release_symtabs ();
3818 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3819 not NULL, the search is restricted to just that program space. */
3821 static std::vector
<symtab
*>
3822 symtabs_from_filename (const char *filename
,
3823 struct program_space
*search_pspace
)
3825 std::vector
<symtab
*> result
3826 = collect_symtabs_from_filename (filename
, search_pspace
);
3828 if (result
.empty ())
3830 if (!have_full_symbols () && !have_partial_symbols ())
3831 throw_error (NOT_FOUND_ERROR
,
3832 _("No symbol table is loaded. "
3833 "Use the \"file\" command."));
3834 source_file_not_found_error (filename
);
3843 symbol_searcher::find_all_symbols (const std::string
&name
,
3844 const struct language_defn
*language
,
3845 enum search_domain search_domain
,
3846 std::vector
<symtab
*> *search_symtabs
,
3847 struct program_space
*search_pspace
)
3849 symbol_searcher_collect_info info
;
3850 struct linespec_state state
;
3852 memset (&state
, 0, sizeof (state
));
3853 state
.language
= language
;
3854 info
.state
= &state
;
3856 info
.result
.symbols
= &m_symbols
;
3857 info
.result
.minimal_symbols
= &m_minimal_symbols
;
3858 std::vector
<symtab
*> all_symtabs
;
3859 if (search_symtabs
== nullptr)
3861 all_symtabs
.push_back (nullptr);
3862 search_symtabs
= &all_symtabs
;
3864 info
.file_symtabs
= search_symtabs
;
3866 add_matching_symbols_to_info (name
.c_str (), symbol_name_match_type::WILD
,
3867 search_domain
, &info
, search_pspace
);
3870 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3871 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3872 returned in MINSYMS. */
3875 find_function_symbols (struct linespec_state
*state
,
3876 std::vector
<symtab
*> *file_symtabs
, const char *name
,
3877 symbol_name_match_type name_match_type
,
3878 std::vector
<block_symbol
> *symbols
,
3879 std::vector
<bound_minimal_symbol
> *minsyms
)
3881 struct collect_info info
;
3882 std::vector
<const char *> symbol_names
;
3885 info
.result
.symbols
= symbols
;
3886 info
.result
.minimal_symbols
= minsyms
;
3887 info
.file_symtabs
= file_symtabs
;
3889 /* Try NAME as an Objective-C selector. */
3890 find_imps (name
, &symbol_names
);
3891 if (!symbol_names
.empty ())
3892 add_all_symbol_names_from_pspace (&info
, state
->search_pspace
,
3893 symbol_names
, FUNCTIONS_DOMAIN
);
3895 add_matching_symbols_to_info (name
, name_match_type
, FUNCTIONS_DOMAIN
,
3896 &info
, state
->search_pspace
);
3899 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3900 in SYMBOLS and minimal symbols in MINSYMS. */
3903 find_linespec_symbols (struct linespec_state
*state
,
3904 std::vector
<symtab
*> *file_symtabs
,
3905 const char *lookup_name
,
3906 symbol_name_match_type name_match_type
,
3907 std::vector
<block_symbol
> *symbols
,
3908 std::vector
<bound_minimal_symbol
> *minsyms
)
3910 gdb::unique_xmalloc_ptr
<char> canon
3911 = cp_canonicalize_string_no_typedefs (lookup_name
);
3912 if (canon
!= nullptr)
3913 lookup_name
= canon
.get ();
3915 /* It's important to not call expand_symtabs_matching unnecessarily
3916 as it can really slow things down (by unnecessarily expanding
3917 potentially 1000s of symtabs, which when debugging some apps can
3918 cost 100s of seconds). Avoid this to some extent by *first* calling
3919 find_function_symbols, and only if that doesn't find anything
3920 *then* call find_method. This handles two important cases:
3921 1) break (anonymous namespace)::foo
3922 2) break class::method where method is in class (and not a baseclass) */
3924 find_function_symbols (state
, file_symtabs
, lookup_name
,
3925 name_match_type
, symbols
, minsyms
);
3927 /* If we were unable to locate a symbol of the same name, try dividing
3928 the name into class and method names and searching the class and its
3930 if (symbols
->empty () && minsyms
->empty ())
3932 std::string klass
, method
;
3933 const char *last
, *p
, *scope_op
;
3935 /* See if we can find a scope operator and break this symbol
3936 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3938 p
= find_toplevel_string (lookup_name
, scope_op
);
3944 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
3947 /* If no scope operator was found, there is nothing more we can do;
3948 we already attempted to lookup the entire name as a symbol
3953 /* LOOKUP_NAME points to the class name.
3954 LAST points to the method name. */
3955 klass
= std::string (lookup_name
, last
- lookup_name
);
3957 /* Skip past the scope operator. */
3958 last
+= strlen (scope_op
);
3961 /* Find a list of classes named KLASS. */
3962 std::vector
<block_symbol
> classes
3963 = lookup_prefix_sym (state
, file_symtabs
, klass
.c_str ());
3964 if (!classes
.empty ())
3966 /* Now locate a list of suitable methods named METHOD. */
3969 find_method (state
, file_symtabs
,
3970 klass
.c_str (), method
.c_str (),
3971 &classes
, symbols
, minsyms
);
3974 /* If successful, we're done. If NOT_FOUND_ERROR
3975 was not thrown, rethrow the exception that we did get. */
3976 catch (const gdb_exception_error
&except
)
3978 if (except
.error
!= NOT_FOUND_ERROR
)
3985 /* Helper for find_label_symbols. Find all labels that match name
3986 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3987 Return the actual function symbol in which the label was found in
3988 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3989 interpreted as a label name prefix. Otherwise, only a label named
3990 exactly NAME match. */
3993 find_label_symbols_in_block (const struct block
*block
,
3994 const char *name
, struct symbol
*fn_sym
,
3995 bool completion_mode
,
3996 std::vector
<block_symbol
> *result
,
3997 std::vector
<block_symbol
> *label_funcs_ret
)
3999 if (completion_mode
)
4001 struct block_iterator iter
;
4003 size_t name_len
= strlen (name
);
4005 int (*cmp
) (const char *, const char *, size_t);
4006 cmp
= case_sensitivity
== case_sensitive_on
? strncmp
: strncasecmp
;
4008 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
4010 if (symbol_matches_domain (sym
->language (),
4011 SYMBOL_DOMAIN (sym
), LABEL_DOMAIN
)
4012 && cmp (sym
->search_name (), name
, name_len
) == 0)
4014 result
->push_back ({sym
, block
});
4015 label_funcs_ret
->push_back ({fn_sym
, block
});
4021 struct block_symbol label_sym
4022 = lookup_symbol (name
, block
, LABEL_DOMAIN
, 0);
4024 if (label_sym
.symbol
!= NULL
)
4026 result
->push_back (label_sym
);
4027 label_funcs_ret
->push_back ({fn_sym
, block
});
4032 /* Return all labels that match name NAME in FUNCTION_SYMBOLS or NULL
4033 if no matches were found.
4035 Return the actual function symbol in which the label was found in
4036 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4037 interpreted as a label name prefix. Otherwise, only labels named
4038 exactly NAME match. */
4041 static std::vector
<block_symbol
> *
4042 find_label_symbols (struct linespec_state
*self
,
4043 std::vector
<block_symbol
> *function_symbols
,
4044 std::vector
<block_symbol
> *label_funcs_ret
,
4046 bool completion_mode
)
4048 const struct block
*block
;
4049 struct symbol
*fn_sym
;
4050 std::vector
<block_symbol
> result
;
4052 if (function_symbols
== NULL
)
4054 set_current_program_space (self
->program_space
);
4055 block
= get_current_search_block ();
4058 block
&& !BLOCK_FUNCTION (block
);
4059 block
= BLOCK_SUPERBLOCK (block
))
4063 fn_sym
= BLOCK_FUNCTION (block
);
4065 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
4066 &result
, label_funcs_ret
);
4070 for (const auto &elt
: *function_symbols
)
4072 fn_sym
= elt
.symbol
;
4073 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym
)));
4074 block
= SYMBOL_BLOCK_VALUE (fn_sym
);
4076 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
4077 &result
, label_funcs_ret
);
4081 if (!result
.empty ())
4082 return new std::vector
<block_symbol
> (std::move (result
));
4088 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4090 static std::vector
<symtab_and_line
>
4091 decode_digits_list_mode (struct linespec_state
*self
,
4093 struct symtab_and_line val
)
4095 gdb_assert (self
->list_mode
);
4097 std::vector
<symtab_and_line
> values
;
4099 for (const auto &elt
: *ls
->file_symtabs
)
4101 /* The logic above should ensure this. */
4102 gdb_assert (elt
!= NULL
);
4104 set_current_program_space (SYMTAB_PSPACE (elt
));
4106 /* Simplistic search just for the list command. */
4107 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
4108 if (val
.symtab
== NULL
)
4110 val
.pspace
= SYMTAB_PSPACE (elt
);
4112 val
.explicit_line
= true;
4114 add_sal_to_sals (self
, &values
, &val
, NULL
, 0);
4120 /* A helper for create_sals_line_offset that iterates over the symtabs
4121 associated with LS and returns a vector of corresponding symtab_and_line
4124 static std::vector
<symtab_and_line
>
4125 decode_digits_ordinary (struct linespec_state
*self
,
4128 struct linetable_entry
**best_entry
)
4130 std::vector
<symtab_and_line
> sals
;
4131 for (const auto &elt
: *ls
->file_symtabs
)
4133 std::vector
<CORE_ADDR
> pcs
;
4135 /* The logic above should ensure this. */
4136 gdb_assert (elt
!= NULL
);
4138 set_current_program_space (SYMTAB_PSPACE (elt
));
4140 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
4141 for (CORE_ADDR pc
: pcs
)
4143 symtab_and_line sal
;
4144 sal
.pspace
= SYMTAB_PSPACE (elt
);
4147 sal
.explicit_line
= true;
4149 sals
.push_back (std::move (sal
));
4158 /* Return the line offset represented by VARIABLE. */
4160 static struct line_offset
4161 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
4165 struct line_offset offset
= {0, LINE_OFFSET_NONE
};
4167 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
4170 while (*p
>= '0' && *p
<= '9')
4172 if (!*p
) /* Reached end of token without hitting non-digit. */
4174 /* We have a value history reference. */
4175 struct value
*val_history
;
4177 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
4179 = access_value_history ((variable
[1] == '$') ? -index
: index
);
4180 if (value_type (val_history
)->code () != TYPE_CODE_INT
)
4181 error (_("History values used in line "
4182 "specs must have integer values."));
4183 offset
.offset
= value_as_long (val_history
);
4187 /* Not all digits -- may be user variable/function or a
4188 convenience variable. */
4190 struct internalvar
*ivar
;
4192 /* Try it as a convenience variable. If it is not a convenience
4193 variable, return and allow normal symbol lookup to occur. */
4194 ivar
= lookup_only_internalvar (variable
+ 1);
4196 /* No internal variable with that name. Mark the offset
4197 as unknown to allow the name to be looked up as a symbol. */
4198 offset
.sign
= LINE_OFFSET_UNKNOWN
;
4201 /* We found a valid variable name. If it is not an integer,
4203 if (!get_internalvar_integer (ivar
, &valx
))
4204 error (_("Convenience variables used in line "
4205 "specs must have integer values."));
4207 offset
.offset
= valx
;
4215 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
4216 linespec; return the SAL in RESULT. This function should return SALs
4217 matching those from find_function_start_sal, otherwise false
4218 multiple-locations breakpoints could be placed. */
4221 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
4222 struct minimal_symbol
*msymbol
,
4223 std::vector
<symtab_and_line
> *result
)
4225 bool want_start_sal
;
4227 CORE_ADDR func_addr
;
4228 bool is_function
= msymbol_is_function (objfile
, msymbol
, &func_addr
);
4232 const char *msym_name
= msymbol
->linkage_name ();
4234 if (MSYMBOL_TYPE (msymbol
) == mst_text_gnu_ifunc
4235 || MSYMBOL_TYPE (msymbol
) == mst_data_gnu_ifunc
)
4236 want_start_sal
= gnu_ifunc_resolve_name (msym_name
, &func_addr
);
4238 want_start_sal
= true;
4241 symtab_and_line sal
;
4243 if (is_function
&& want_start_sal
)
4244 sal
= find_function_start_sal (func_addr
, NULL
, self
->funfirstline
);
4247 sal
.objfile
= objfile
;
4248 sal
.msymbol
= msymbol
;
4249 /* Store func_addr, not the minsym's address in case this was an
4250 ifunc that hasn't been resolved yet. */
4254 sal
.pc
= MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
4255 sal
.pspace
= current_program_space
;
4258 sal
.section
= msymbol
->obj_section (objfile
);
4260 if (maybe_add_address (self
->addr_set
, objfile
->pspace
, sal
.pc
))
4261 add_sal_to_sals (self
, result
, &sal
, msymbol
->natural_name (), 0);
4264 /* Helper for search_minsyms_for_name that adds the symbol to the
4268 add_minsym (struct minimal_symbol
*minsym
, struct objfile
*objfile
,
4269 struct symtab
*symtab
, int list_mode
,
4270 std::vector
<struct bound_minimal_symbol
> *msyms
)
4274 /* We're looking for a label for which we don't have debug
4276 CORE_ADDR func_addr
;
4277 if (msymbol_is_function (objfile
, minsym
, &func_addr
))
4279 symtab_and_line sal
= find_pc_sect_line (func_addr
, NULL
, 0);
4281 if (symtab
!= sal
.symtab
)
4286 /* Exclude data symbols when looking for breakpoint locations. */
4287 if (!list_mode
&& !msymbol_is_function (objfile
, minsym
))
4290 struct bound_minimal_symbol mo
= {minsym
, objfile
};
4291 msyms
->push_back (mo
);
4295 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4296 is not NULL, the search is restricted to just that program
4299 If SYMTAB is NULL, search all objfiles, otherwise
4300 restrict results to the given SYMTAB. */
4303 search_minsyms_for_name (struct collect_info
*info
,
4304 const lookup_name_info
&name
,
4305 struct program_space
*search_pspace
,
4306 struct symtab
*symtab
)
4308 std::vector
<struct bound_minimal_symbol
> minsyms
;
4312 for (struct program_space
*pspace
: program_spaces
)
4314 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
4316 if (pspace
->executing_startup
)
4319 set_current_program_space (pspace
);
4321 for (objfile
*objfile
: current_program_space
->objfiles ())
4323 iterate_over_minimal_symbols (objfile
, name
,
4324 [&] (struct minimal_symbol
*msym
)
4326 add_minsym (msym
, objfile
, nullptr,
4327 info
->state
->list_mode
,
4336 if (search_pspace
== NULL
|| SYMTAB_PSPACE (symtab
) == search_pspace
)
4338 set_current_program_space (SYMTAB_PSPACE (symtab
));
4339 iterate_over_minimal_symbols
4340 (SYMTAB_OBJFILE (symtab
), name
,
4341 [&] (struct minimal_symbol
*msym
)
4343 add_minsym (msym
, SYMTAB_OBJFILE (symtab
), symtab
,
4344 info
->state
->list_mode
, &minsyms
);
4350 /* Return true if TYPE is a static symbol. */
4351 auto msymbol_type_is_static
= [] (enum minimal_symbol_type type
)
4364 /* Add minsyms to the result set, but filter out trampoline symbols
4365 if we also found extern symbols with the same name. I.e., don't
4366 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4367 'foo' is the symbol that the plt resolves to. */
4368 for (const bound_minimal_symbol
&item
: minsyms
)
4371 if (MSYMBOL_TYPE (item
.minsym
) == mst_solib_trampoline
)
4373 for (const bound_minimal_symbol
&item2
: minsyms
)
4375 if (&item2
== &item
)
4378 /* Trampoline symbols can only jump to exported
4380 if (msymbol_type_is_static (MSYMBOL_TYPE (item2
.minsym
)))
4383 if (strcmp (item
.minsym
->linkage_name (),
4384 item2
.minsym
->linkage_name ()) != 0)
4387 /* Found a global minsym with the same name as the
4388 trampoline. Don't create a location for this
4396 info
->result
.minimal_symbols
->push_back (item
);
4400 /* A helper function to add all symbols matching NAME to INFO. If
4401 PSPACE is not NULL, the search is restricted to just that program
4405 add_matching_symbols_to_info (const char *name
,
4406 symbol_name_match_type name_match_type
,
4407 enum search_domain search_domain
,
4408 struct collect_info
*info
,
4409 struct program_space
*pspace
)
4411 lookup_name_info
lookup_name (name
, name_match_type
);
4413 for (const auto &elt
: *info
->file_symtabs
)
4417 iterate_over_all_matching_symtabs (info
->state
, lookup_name
,
4418 VAR_DOMAIN
, search_domain
,
4420 [&] (block_symbol
*bsym
)
4421 { return info
->add_symbol (bsym
); });
4422 search_minsyms_for_name (info
, lookup_name
, pspace
, NULL
);
4424 else if (pspace
== NULL
|| pspace
== SYMTAB_PSPACE (elt
))
4426 int prev_len
= info
->result
.symbols
->size ();
4428 /* Program spaces that are executing startup should have
4429 been filtered out earlier. */
4430 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
4431 set_current_program_space (SYMTAB_PSPACE (elt
));
4432 iterate_over_file_blocks (elt
, lookup_name
, VAR_DOMAIN
,
4433 [&] (block_symbol
*bsym
)
4434 { return info
->add_symbol (bsym
); });
4436 /* If no new symbols were found in this iteration and this symtab
4437 is in assembler, we might actually be looking for a label for
4438 which we don't have debug info. Check for a minimal symbol in
4440 if (prev_len
== info
->result
.symbols
->size ()
4441 && elt
->language
== language_asm
)
4442 search_minsyms_for_name (info
, lookup_name
, pspace
, elt
);
4449 /* Now come some functions that are called from multiple places within
4453 symbol_to_sal (struct symtab_and_line
*result
,
4454 int funfirstline
, struct symbol
*sym
)
4456 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
4458 *result
= find_function_start_sal (sym
, funfirstline
);
4463 if (SYMBOL_CLASS (sym
) == LOC_LABEL
&& SYMBOL_VALUE_ADDRESS (sym
) != 0)
4466 result
->symtab
= symbol_symtab (sym
);
4467 result
->symbol
= sym
;
4468 result
->line
= SYMBOL_LINE (sym
);
4469 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
4470 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
4471 result
->explicit_pc
= 1;
4474 else if (funfirstline
)
4478 else if (SYMBOL_LINE (sym
) != 0)
4480 /* We know its line number. */
4482 result
->symtab
= symbol_symtab (sym
);
4483 result
->symbol
= sym
;
4484 result
->line
= SYMBOL_LINE (sym
);
4485 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
4486 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
4494 linespec_result::~linespec_result ()
4496 for (linespec_sals
&lsal
: lsals
)
4497 xfree (lsal
.canonical
);
4500 /* Return the quote characters permitted by the linespec parser. */
4503 get_gdb_linespec_parser_quote_characters (void)
4505 return linespec_quote_characters
;