1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2024 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/>. */
27 #include "completer.h"
29 #include "cp-support.h"
30 #include "parser-defs.h"
32 #include "objc-lang.h"
37 #include "arch-utils.h"
39 #include "cli/cli-utils.h"
40 #include "filenames.h"
44 #include "gdbsupport/function-view.h"
45 #include "gdbsupport/def-vector.h"
48 #include "gdbsupport/unordered_set.h"
50 /* An enumeration of the various things a user might attempt to
51 complete for a linespec location. */
53 enum class linespec_complete_what
55 /* Nothing, no possible completion. */
58 /* A function/method name. Due to ambiguity between
64 this can also indicate a source filename, iff we haven't seen a
65 separate source filename component, as in "b source.c:function". */
68 /* A label symbol. E.g., break file.c:function:LABEL. */
71 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
74 /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
75 E.g., "break func threa<tab>". */
79 /* A linespec. Elements of this structure are filled in by a parser
80 (either parse_linespec or some other function). The structure is
81 then converted into SALs by convert_linespec_to_sals. */
85 /* An explicit location spec describing the SaLs. */
86 explicit_location_spec explicit_loc
;
88 /* The list of symtabs to search to which to limit the search.
90 If explicit.SOURCE_FILENAME is NULL (no user-specified filename),
91 FILE_SYMTABS should contain one single NULL member. This will cause the
92 code to use the default symtab. */
93 std::vector
<symtab
*> file_symtabs
;
95 /* A list of matching function symbols and minimal symbols. Both lists
96 may be empty if no matching symbols were found. */
97 std::vector
<block_symbol
> function_symbols
;
98 std::vector
<bound_minimal_symbol
> minimal_symbols
;
100 /* A structure of matching label symbols and the corresponding
101 function symbol in which the label was found. Both may be empty
102 or both must be non-empty. */
105 std::vector
<block_symbol
> label_symbols
;
106 std::vector
<block_symbol
> function_symbols
;
110 /* A canonical linespec represented as a symtab-related string.
112 Each entry represents the "SYMTAB:SUFFIX" linespec string.
113 SYMTAB can be converted for example by symtab_to_fullname or
114 symtab_to_filename_for_display as needed. */
116 struct linespec_canonical_name
118 /* Remaining text part of the linespec string. */
121 /* If NULL then SUFFIX is the whole linespec string. */
122 struct symtab
*symtab
;
125 /* An instance of this is used to keep all state while linespec
126 operates. This instance is passed around as a 'this' pointer to
127 the various implementation methods. */
129 struct linespec_state
131 linespec_state (int flags
, const language_defn
*language
,
132 program_space
*pspace
,
133 program_space
*search_pspace
,
134 symtab
*default_symtab
,
136 linespec_result
*canonical
)
137 : language (language
),
138 program_space (pspace
),
139 search_pspace (search_pspace
),
140 default_symtab (default_symtab
),
141 default_line (default_line
),
142 funfirstline ((flags
& DECODE_LINE_FUNFIRSTLINE
) != 0),
143 list_mode ((flags
& DECODE_LINE_LIST_MODE
) != 0),
144 canonical (canonical
)
148 linespec_state (const language_defn
*language
, program_space
*program_space
)
149 : linespec_state (0, language
, program_space
, nullptr, nullptr, 0, nullptr)
153 DISABLE_COPY_AND_ASSIGN (linespec_state
);
155 /* Add ADDR to the address set. Return true if this is a new
157 bool maybe_add_address (program_space
*pspace
, CORE_ADDR addr
)
159 return addr_set
.emplace (pspace
, addr
).second
;
162 /* The language in use during linespec processing. */
163 const struct language_defn
*language
;
165 /* The program space as seen when the module was entered. */
166 struct program_space
*program_space
;
168 /* If not NULL, the search is restricted to just this program
170 struct program_space
*search_pspace
;
172 /* The default symtab to use, if no other symtab is specified. */
173 struct symtab
*default_symtab
;
175 /* The default line to use. */
178 /* The 'funfirstline' value that was passed in to decode_line_1 or
182 /* True if we are running in 'list' mode; see decode_line_list. */
185 /* The 'canonical' value passed to decode_line_full, or NULL. */
186 struct linespec_result
*canonical
;
188 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
189 std::vector
<linespec_canonical_name
> canonical_names
;
191 /* Are we building a linespec? */
192 bool is_linespec
= false;
196 /* An address entry is used to ensure that any given location is
197 only added to the result a single time. It holds an address and
198 the program space from which the address came. */
199 using address_entry
= std::pair
<::program_space
*, CORE_ADDR
>;
201 /* This is a set of address_entry objects which is used to prevent
202 duplicate symbols from being entered into the result. */
203 gdb::unordered_set
<address_entry
> addr_set
;
206 /* This is a helper object that is used when collecting symbols into a
211 /* The linespec object in use. */
212 struct linespec_state
*state
;
214 /* A list of symtabs to which to restrict matches. */
215 const std::vector
<symtab
*> *file_symtabs
;
217 /* The result being accumulated. */
220 std::vector
<block_symbol
> *symbols
;
221 std::vector
<bound_minimal_symbol
> *minimal_symbols
;
224 /* Possibly add a symbol to the results. */
225 virtual bool add_symbol (block_symbol
*bsym
);
229 collect_info::add_symbol (block_symbol
*bsym
)
231 /* In list mode, add all matching symbols, regardless of class.
232 This allows the user to type "list a_global_variable". */
233 if (bsym
->symbol
->aclass () == LOC_BLOCK
|| this->state
->list_mode
)
234 this->result
.symbols
->push_back (*bsym
);
236 /* Continue iterating. */
240 /* Custom collect_info for symbol_searcher. */
242 struct symbol_searcher_collect_info
245 bool add_symbol (block_symbol
*bsym
) override
247 /* Add everything. */
248 this->result
.symbols
->push_back (*bsym
);
250 /* Continue iterating. */
257 enum linespec_token_type
262 /* A colon "separator" */
274 /* EOI (end of input) */
281 /* List of keywords. This is NULL-terminated so that it can be used
282 as enum completer. */
283 const char * const linespec_keywords
[] = { "if", "thread", "task", "inferior", "-force-condition", NULL
};
284 #define IF_KEYWORD_INDEX 0
285 #define FORCE_KEYWORD_INDEX 4
287 /* A token of the linespec lexer */
289 struct linespec_token
291 /* The type of the token */
292 linespec_token_type type
;
294 /* Data for the token */
297 /* A string, given as a stoken */
298 struct stoken string
;
305 /* An instance of the linespec parser. */
307 struct linespec_parser
309 linespec_parser (int flags
, const struct language_defn
*language
,
310 struct program_space
*search_pspace
,
311 struct symtab
*default_symtab
,
313 struct linespec_result
*canonical
);
315 DISABLE_COPY_AND_ASSIGN (linespec_parser
);
317 /* Lexer internal data */
320 /* Save head of input stream. */
321 const char *saved_arg
;
323 /* Head of the input stream. */
326 /* The current token. */
327 linespec_token current
;
330 /* Is the entire linespec quote-enclosed? */
331 bool is_quote_enclosed
= false;
333 /* The state of the parse. */
334 linespec_state state
;
336 /* The result of the parse. */
339 /* What the parser believes the current word point should complete
341 linespec_complete_what complete_what
= linespec_complete_what::NOTHING
;
343 /* The completion word point. The parser advances this as it skips
344 tokens. At some point the input string will end or parsing will
345 fail, and then we attempt completion at the captured completion
346 word point, interpreting the string at completion_word as
348 const char *completion_word
= nullptr;
350 /* If the current token was a quoted string, then this is the
351 quoting character (either " or '). */
352 int completion_quote_char
= 0;
354 /* If the current token was a quoted string, then this points at the
355 end of the quoted string. */
356 const char *completion_quote_end
= nullptr;
358 /* If parsing for completion, then this points at the completion
359 tracker. Otherwise, this is NULL. */
360 struct completion_tracker
*completion_tracker
= nullptr;
363 /* Prototypes for local functions. */
365 static void iterate_over_file_blocks
366 (struct symtab
*symtab
, const lookup_name_info
&name
,
367 domain_search_flags domain
,
368 gdb::function_view
<symbol_found_callback_ftype
> callback
);
370 static void initialize_defaults (struct symtab
**default_symtab
,
373 CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
375 static std::vector
<symtab_and_line
> decode_objc (struct linespec_state
*self
,
379 static std::vector
<symtab
*> symtabs_from_filename
380 (const char *, struct program_space
*pspace
);
382 static std::vector
<block_symbol
> find_label_symbols
383 (struct linespec_state
*self
,
384 const std::vector
<block_symbol
> &function_symbols
,
385 std::vector
<block_symbol
> *label_funcs_ret
,
386 const char *name
, bool completion_mode
= false);
388 static void find_linespec_symbols (struct linespec_state
*self
,
389 const std::vector
<symtab
*> &file_symtabs
,
391 symbol_name_match_type name_match_type
,
392 std::vector
<block_symbol
> *symbols
,
393 std::vector
<bound_minimal_symbol
> *minsyms
);
395 static struct line_offset
396 linespec_parse_variable (struct linespec_state
*self
,
397 const char *variable
);
399 static bool symbol_to_sal (struct symtab_and_line
*result
,
400 bool funfirstline
, struct symbol
*sym
);
402 static void add_matching_symbols_to_info (const char *name
,
403 symbol_name_match_type name_match_type
,
404 domain_search_flags domain_search_flags
,
405 struct collect_info
*info
,
406 struct program_space
*pspace
);
408 static void add_all_symbol_names_from_pspace
409 (struct collect_info
*info
, struct program_space
*pspace
,
410 const std::vector
<const char *> &names
, domain_search_flags domain_search_flags
);
412 static std::vector
<symtab
*>
413 collect_symtabs_from_filename (const char *file
,
414 struct program_space
*pspace
);
416 static std::vector
<symtab_and_line
> decode_digits_ordinary
417 (struct linespec_state
*self
,
420 const linetable_entry
**best_entry
);
422 static std::vector
<symtab_and_line
> decode_digits_list_mode
423 (struct linespec_state
*self
,
425 struct symtab_and_line val
);
427 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
428 struct minimal_symbol
*msymbol
,
429 std::vector
<symtab_and_line
> *result
);
431 static bool compare_symbols (const block_symbol
&a
, const block_symbol
&b
);
433 static bool compare_msymbols (const bound_minimal_symbol
&a
,
434 const bound_minimal_symbol
&b
);
436 /* Permitted quote characters for the parser. This is different from the
437 completer's quote characters to allow backward compatibility with the
439 static const char linespec_quote_characters
[] = "\"\'";
441 /* Lexer functions. */
443 /* Lex a number from the input in PARSER. This only supports
446 Return true if input is decimal numbers. Return false if not. */
449 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
451 tokenp
->type
= LSTOKEN_NUMBER
;
452 tokenp
->data
.string
.length
= 0;
453 tokenp
->data
.string
.ptr
= parser
->lexer
.stream
;
455 /* Keep any sign at the start of the stream. */
456 if (*parser
->lexer
.stream
== '+' || *parser
->lexer
.stream
== '-')
458 ++tokenp
->data
.string
.length
;
459 ++(parser
->lexer
.stream
);
462 while (isdigit (*parser
->lexer
.stream
))
464 ++tokenp
->data
.string
.length
;
465 ++(parser
->lexer
.stream
);
468 /* If the next character in the input buffer is not a space, comma,
469 quote, or colon, this input does not represent a number. */
470 if (*parser
->lexer
.stream
!= '\0'
471 && !isspace (*parser
->lexer
.stream
) && *parser
->lexer
.stream
!= ','
472 && *parser
->lexer
.stream
!= ':'
473 && !strchr (linespec_quote_characters
, *parser
->lexer
.stream
))
475 parser
->lexer
.stream
= tokenp
->data
.string
.ptr
;
482 /* See linespec.h. */
485 linespec_lexer_lex_keyword (const char *p
)
491 for (i
= 0; linespec_keywords
[i
] != NULL
; ++i
)
493 int len
= strlen (linespec_keywords
[i
]);
497 - "thread" or "task" and the next character is
498 whitespace, we may have found a keyword. It is only a
499 keyword if it is not followed by another keyword.
501 - "-force-condition", the next character may be EOF
502 since this keyword does not take any arguments. Otherwise,
503 it should be followed by a keyword.
505 - "if", ALWAYS stop the lexer, since it is not possible to
506 predict what is going to appear in the condition, which can
507 only be parsed after SaLs have been found. */
508 if (strncmp (p
, linespec_keywords
[i
], len
) == 0)
512 if (i
== FORCE_KEYWORD_INDEX
&& p
[len
] == '\0')
513 return linespec_keywords
[i
];
515 if (!isspace (p
[len
]))
518 if (i
== FORCE_KEYWORD_INDEX
)
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
]))
528 return linespec_keywords
[i
];
531 else if (i
!= IF_KEYWORD_INDEX
)
533 /* We matched a "thread" or "task". */
536 for (j
= 0; linespec_keywords
[j
] != NULL
; ++j
)
538 int nextlen
= strlen (linespec_keywords
[j
]);
540 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
541 && isspace (p
[nextlen
]))
546 return linespec_keywords
[i
];
554 /* See description in linespec.h. */
557 is_ada_operator (const char *string
)
559 const struct ada_opname_map
*mapping
;
561 for (mapping
= ada_opname_table
;
562 mapping
->encoded
!= NULL
563 && !startswith (string
, mapping
->decoded
); ++mapping
)
566 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
569 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
570 the location of QUOTE_CHAR, or NULL if not found. */
573 skip_quote_char (const char *string
, char quote_char
)
575 const char *p
, *last
;
577 p
= last
= find_toplevel_char (string
, quote_char
);
578 while (p
&& *p
!= '\0' && *p
!= ':')
580 p
= find_toplevel_char (p
, quote_char
);
588 /* Make a writable copy of the string given in TOKEN, trimming
589 any trailing whitespace. */
591 static gdb::unique_xmalloc_ptr
<char>
592 copy_token_string (linespec_token token
)
596 if (token
.type
== LSTOKEN_KEYWORD
)
597 return make_unique_xstrdup (token
.data
.keyword
);
599 str
= token
.data
.string
.ptr
;
600 s
= remove_trailing_whitespace (str
, str
+ token
.data
.string
.length
);
602 return gdb::unique_xmalloc_ptr
<char> (savestring (str
, s
- str
));
605 /* Does P represent the end of a quote-enclosed linespec? */
608 is_closing_quote_enclosed (const char *p
)
610 if (strchr (linespec_quote_characters
, *p
))
612 p
= skip_spaces ((char *) p
);
613 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
616 /* Find the end of the parameter list that starts with *INPUT.
617 This helper function assists with lexing string segments
618 which might contain valid (non-terminating) commas. */
621 find_parameter_list_end (const char *input
)
623 char end_char
, start_char
;
628 if (start_char
== '(')
630 else if (start_char
== '<')
639 if (*p
== start_char
)
641 else if (*p
== end_char
)
655 /* If the [STRING, STRING_LEN) string ends with what looks like a
656 keyword, return the keyword start offset in STRING. Return -1
660 string_find_incomplete_keyword_at_end (const char * const *keywords
,
661 const char *string
, size_t string_len
)
663 const char *end
= string
+ string_len
;
666 while (p
> string
&& *p
!= ' ')
671 size_t len
= end
- p
;
672 for (size_t i
= 0; keywords
[i
] != NULL
; ++i
)
673 if (strncmp (keywords
[i
], p
, len
) == 0)
680 /* Lex a string from the input in PARSER. */
682 static linespec_token
683 linespec_lexer_lex_string (linespec_parser
*parser
)
685 linespec_token token
;
686 const char *start
= parser
->lexer
.stream
;
688 token
.type
= LSTOKEN_STRING
;
690 /* If the input stream starts with a quote character, skip to the next
691 quote character, regardless of the content. */
692 if (strchr (linespec_quote_characters
, *parser
->lexer
.stream
))
695 char quote_char
= *parser
->lexer
.stream
;
697 /* Special case: Ada operators. */
698 if (parser
->state
.language
->la_language
== language_ada
699 && quote_char
== '\"')
701 int len
= is_ada_operator (parser
->lexer
.stream
);
705 /* The input is an Ada operator. Return the quoted string
707 token
.data
.string
.ptr
= parser
->lexer
.stream
;
708 token
.data
.string
.length
= len
;
709 parser
->lexer
.stream
+= len
;
713 /* The input does not represent an Ada operator -- fall through
714 to normal quoted string handling. */
717 /* Skip past the beginning quote. */
718 ++(parser
->lexer
.stream
);
720 /* Mark the start of the string. */
721 token
.data
.string
.ptr
= parser
->lexer
.stream
;
723 /* Skip to the ending quote. */
724 end
= skip_quote_char (parser
->lexer
.stream
, quote_char
);
726 /* This helps the completer mode decide whether we have a
728 parser
->completion_quote_char
= quote_char
;
729 parser
->completion_quote_end
= end
;
731 /* Error if the input did not terminate properly, unless in
735 if (parser
->completion_tracker
== NULL
)
736 error (_("unmatched quote"));
738 /* In completion mode, we'll try to complete the incomplete
740 token
.type
= LSTOKEN_STRING
;
741 while (*parser
->lexer
.stream
!= '\0')
742 parser
->lexer
.stream
++;
743 token
.data
.string
.length
= parser
->lexer
.stream
- 1 - start
;
747 /* Skip over the ending quote and mark the length of the string. */
748 parser
->lexer
.stream
= (char *) ++end
;
749 token
.data
.string
.length
= parser
->lexer
.stream
- 2 - start
;
756 /* Otherwise, only identifier characters are permitted.
757 Spaces are the exception. In general, we keep spaces,
758 but only if the next characters in the input do not resolve
759 to one of the keywords.
761 This allows users to forgo quoting CV-qualifiers, template arguments,
762 and similar common language constructs. */
766 if (isspace (*parser
->lexer
.stream
))
768 p
= skip_spaces (parser
->lexer
.stream
);
769 /* When we get here we know we've found something followed by
770 a space (we skip over parens and templates below).
771 So if we find a keyword now, we know it is a keyword and not,
772 say, a function name. */
773 if (linespec_lexer_lex_keyword (p
) != NULL
)
775 token
.data
.string
.ptr
= start
;
776 token
.data
.string
.length
777 = parser
->lexer
.stream
- start
;
781 /* Advance past the whitespace. */
782 parser
->lexer
.stream
= p
;
785 /* If the next character is EOI or (single) ':', the
786 string is complete; return the token. */
787 if (*parser
->lexer
.stream
== 0)
789 token
.data
.string
.ptr
= start
;
790 token
.data
.string
.length
= parser
->lexer
.stream
- start
;
793 else if (parser
->lexer
.stream
[0] == ':')
795 /* Do not tokenize the C++ scope operator. */
796 if (parser
->lexer
.stream
[1] == ':')
797 ++(parser
->lexer
.stream
);
799 /* Do not tokenize ABI tags such as "[abi:cxx11]". */
800 else if (parser
->lexer
.stream
- start
> 4
801 && startswith (parser
->lexer
.stream
- 4, "[abi"))
806 /* Do not tokenify if the input length so far is one
807 (i.e, a single-letter drive name) and the next character
808 is a directory separator. This allows Windows-style
809 paths to be recognized as filenames without quoting it. */
810 else if ((parser
->lexer
.stream
- start
) != 1
811 || !IS_DIR_SEPARATOR (parser
->lexer
.stream
[1]))
813 token
.data
.string
.ptr
= start
;
814 token
.data
.string
.length
815 = parser
->lexer
.stream
- start
;
819 /* Special case: permit quote-enclosed linespecs. */
820 else if (parser
->is_quote_enclosed
821 && strchr (linespec_quote_characters
,
822 *parser
->lexer
.stream
)
823 && is_closing_quote_enclosed (parser
->lexer
.stream
))
825 token
.data
.string
.ptr
= start
;
826 token
.data
.string
.length
= parser
->lexer
.stream
- start
;
829 /* Because commas may terminate a linespec and appear in
830 the middle of valid string input, special cases for
831 '<' and '(' are necessary. */
832 else if (*parser
->lexer
.stream
== '<'
833 || *parser
->lexer
.stream
== '(')
835 /* Don't interpret 'operator<' / 'operator<<' as a
836 template parameter list though. */
837 if (*parser
->lexer
.stream
== '<'
838 && (parser
->state
.language
->la_language
840 && (parser
->lexer
.stream
- start
) >= CP_OPERATOR_LEN
)
842 const char *op
= parser
->lexer
.stream
;
844 while (op
> start
&& isspace (op
[-1]))
846 if (op
- start
>= CP_OPERATOR_LEN
)
848 op
-= CP_OPERATOR_LEN
;
849 if (strncmp (op
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0
851 || !(isalnum (op
[-1]) || op
[-1] == '_')))
853 /* This is an operator name. Keep going. */
854 ++(parser
->lexer
.stream
);
855 if (*parser
->lexer
.stream
== '<')
856 ++(parser
->lexer
.stream
);
862 const char *end
= find_parameter_list_end (parser
->lexer
.stream
);
863 parser
->lexer
.stream
= end
;
865 /* Don't loop around to the normal \0 case above because
866 we don't want to misinterpret a potential keyword at
867 the end of the token when the string isn't
868 "()<>"-balanced. This handles "b
869 function(thread<tab>" in completion mode. */
872 token
.data
.string
.ptr
= start
;
873 token
.data
.string
.length
874 = parser
->lexer
.stream
- start
;
880 /* Commas are terminators, but not if they are part of an
882 else if (*parser
->lexer
.stream
== ',')
884 if ((parser
->state
.language
->la_language
886 && (parser
->lexer
.stream
- start
) > CP_OPERATOR_LEN
)
888 const char *op
= strstr (start
, CP_OPERATOR_STR
);
890 if (op
!= NULL
&& is_operator_name (op
))
892 /* This is an operator name. Keep going. */
893 ++(parser
->lexer
.stream
);
898 /* Comma terminates the string. */
899 token
.data
.string
.ptr
= start
;
900 token
.data
.string
.length
= parser
->lexer
.stream
- start
;
904 /* Advance the stream. */
905 gdb_assert (*(parser
->lexer
.stream
) != '\0');
906 ++(parser
->lexer
.stream
);
913 /* Lex a single linespec token from PARSER. */
915 static linespec_token
916 linespec_lexer_lex_one (linespec_parser
*parser
)
920 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
922 /* Skip any whitespace. */
923 parser
->lexer
.stream
= skip_spaces (parser
->lexer
.stream
);
925 /* Check for a keyword, they end the linespec. */
926 keyword
= linespec_lexer_lex_keyword (parser
->lexer
.stream
);
929 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
930 parser
->lexer
.current
.data
.keyword
= keyword
;
931 /* We do not advance the stream here intentionally:
932 we would like lexing to stop when a keyword is seen.
934 parser->lexer.stream += strlen (keyword); */
936 return parser
->lexer
.current
;
939 /* Handle other tokens. */
940 switch (*parser
->lexer
.stream
)
943 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
947 case '0': case '1': case '2': case '3': case '4':
948 case '5': case '6': case '7': case '8': case '9':
949 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
950 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
954 /* If we have a scope operator, lex the input as a string.
955 Otherwise, return LSTOKEN_COLON. */
956 if (parser
->lexer
.stream
[1] == ':')
957 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
960 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
961 ++(parser
->lexer
.stream
);
965 case '\'': case '\"':
966 /* Special case: permit quote-enclosed linespecs. */
967 if (parser
->is_quote_enclosed
968 && is_closing_quote_enclosed (parser
->lexer
.stream
))
970 ++(parser
->lexer
.stream
);
971 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
974 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
978 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
979 parser
->lexer
.current
.data
.string
.ptr
980 = parser
->lexer
.stream
;
981 parser
->lexer
.current
.data
.string
.length
= 1;
982 ++(parser
->lexer
.stream
);
986 /* If the input is not a number, it must be a string.
987 [Keywords were already considered above.] */
988 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
993 return parser
->lexer
.current
;
996 /* Consume the current token and return the next token in PARSER's
997 input stream. Also advance the completion word for completion
1000 static linespec_token
1001 linespec_lexer_consume_token (linespec_parser
*parser
)
1003 gdb_assert (parser
->lexer
.current
.type
!= LSTOKEN_EOI
);
1005 bool advance_word
= (parser
->lexer
.current
.type
!= LSTOKEN_STRING
1006 || *parser
->lexer
.stream
!= '\0');
1008 /* If we're moving past a string to some other token, it must be the
1009 quote was terminated. */
1010 if (parser
->completion_quote_char
)
1012 gdb_assert (parser
->lexer
.current
.type
== LSTOKEN_STRING
);
1014 /* If the string was the last (non-EOI) token, we're past the
1015 quote, but remember that for later. */
1016 if (*parser
->lexer
.stream
!= '\0')
1018 parser
->completion_quote_char
= '\0';
1019 parser
->completion_quote_end
= NULL
;;
1023 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
1024 linespec_lexer_lex_one (parser
);
1026 if (parser
->lexer
.current
.type
== LSTOKEN_STRING
)
1028 /* Advance the completion word past a potential initial
1030 parser
->completion_word
= parser
->lexer
.current
.data
.string
.ptr
;
1032 else if (advance_word
)
1034 /* Advance the completion word past any whitespace. */
1035 parser
->completion_word
= parser
->lexer
.stream
;
1038 return parser
->lexer
.current
;
1041 /* Return the next token without consuming the current token. */
1043 static linespec_token
1044 linespec_lexer_peek_token (linespec_parser
*parser
)
1046 linespec_token next
;
1047 const char *saved_stream
= parser
->lexer
.stream
;
1048 linespec_token saved_token
= parser
->lexer
.current
;
1049 int saved_completion_quote_char
= parser
->completion_quote_char
;
1050 const char *saved_completion_quote_end
= parser
->completion_quote_end
;
1051 const char *saved_completion_word
= parser
->completion_word
;
1053 next
= linespec_lexer_consume_token (parser
);
1054 parser
->lexer
.stream
= saved_stream
;
1055 parser
->lexer
.current
= saved_token
;
1056 parser
->completion_quote_char
= saved_completion_quote_char
;
1057 parser
->completion_quote_end
= saved_completion_quote_end
;
1058 parser
->completion_word
= saved_completion_word
;
1062 /* Helper functions. */
1064 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1065 the new sal, if needed. If not NULL, SYMNAME is the name of the
1066 symbol to use when constructing the new canonical name.
1068 If LITERAL_CANONICAL is true, SYMNAME will be used as the
1069 canonical name for the SAL. */
1072 add_sal_to_sals (struct linespec_state
*self
,
1073 std::vector
<symtab_and_line
> *sals
,
1074 struct symtab_and_line
*sal
,
1075 const char *symname
, bool literal_canonical
)
1077 sals
->push_back (*sal
);
1079 if (self
->canonical
)
1081 linespec_canonical_name
&canonical
1082 = self
->canonical_names
.emplace_back ();
1084 if (!literal_canonical
&& sal
->symtab
)
1086 symtab_to_fullname (sal
->symtab
);
1088 /* Note that the filter doesn't have to be a valid linespec
1089 input. We only apply the ":LINE" treatment to Ada for
1091 if (symname
!= NULL
&& sal
->line
!= 0
1092 && self
->language
->la_language
== language_ada
)
1093 canonical
.suffix
= string_printf ("%s:%d", symname
,
1095 else if (symname
!= NULL
)
1096 canonical
.suffix
= symname
;
1098 canonical
.suffix
= string_printf ("%d", sal
->line
);
1099 canonical
.symtab
= sal
->symtab
;
1103 if (symname
!= NULL
)
1104 canonical
.suffix
= symname
;
1106 canonical
.suffix
= "<unknown>";
1107 canonical
.symtab
= NULL
;
1112 /* A helper that walks over all matching symtabs in all objfiles and
1113 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1114 not NULL, then the search is restricted to just that program
1115 space. If INCLUDE_INLINE is true then symbols representing
1116 inlined instances of functions will be included in the result. */
1119 iterate_over_all_matching_symtabs
1120 (struct linespec_state
*state
,
1121 const lookup_name_info
&lookup_name
,
1122 const domain_search_flags domain
,
1123 struct program_space
*search_pspace
, bool include_inline
,
1124 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1126 for (struct program_space
*pspace
: program_spaces
)
1128 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
1130 if (pspace
->executing_startup
)
1133 set_current_program_space (pspace
);
1135 for (objfile
*objfile
: pspace
->objfiles ())
1137 objfile
->expand_symtabs_matching (NULL
, &lookup_name
, NULL
, NULL
,
1138 (SEARCH_GLOBAL_BLOCK
1139 | SEARCH_STATIC_BLOCK
),
1142 for (compunit_symtab
*cu
: objfile
->compunits ())
1144 struct symtab
*symtab
= cu
->primary_filetab ();
1146 iterate_over_file_blocks (symtab
, lookup_name
, domain
, callback
);
1150 const struct block
*block
;
1152 const blockvector
*bv
= symtab
->compunit ()->blockvector ();
1154 for (i
= FIRST_LOCAL_BLOCK
; i
< bv
->num_blocks (); i
++)
1156 block
= bv
->block (i
);
1157 state
->language
->iterate_over_symbols
1158 (block
, lookup_name
, domain
,
1159 [&] (block_symbol
*bsym
)
1161 /* Restrict calls to CALLBACK to symbols
1162 representing inline symbols only. */
1163 if (bsym
->symbol
->is_inlined ())
1164 return callback (bsym
);
1174 /* Returns the block to be used for symbol searches from
1175 the current location. */
1177 static const struct block
*
1178 get_current_search_block (void)
1180 /* get_selected_block can change the current language when there is
1181 no selected frame yet. */
1182 scoped_restore_current_language save_language
;
1183 return get_selected_block (0);
1186 /* Iterate over static and global blocks. */
1189 iterate_over_file_blocks
1190 (struct symtab
*symtab
, const lookup_name_info
&name
,
1191 domain_search_flags domain
,
1192 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1194 const struct block
*block
;
1196 for (block
= symtab
->compunit ()->blockvector ()->static_block ();
1198 block
= block
->superblock ())
1199 current_language
->iterate_over_symbols (block
, name
, domain
, callback
);
1202 /* A helper for find_method. This finds all methods in type T of
1203 language T_LANG which match NAME. It adds matching symbol names to
1204 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
1207 find_methods (struct type
*t
, enum language t_lang
, const char *name
,
1208 std::vector
<const char *> *result_names
,
1209 std::vector
<struct type
*> *superclasses
)
1212 const char *class_name
= t
->name ();
1214 /* Ignore this class if it doesn't have a name. This is ugly, but
1215 unless we figure out how to get the physname without the name of
1216 the class, then the loop can't do any good. */
1220 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1221 symbol_name_matcher_ftype
*symbol_name_compare
1222 = language_def (t_lang
)->get_symbol_name_matcher (lookup_name
);
1224 t
= check_typedef (t
);
1226 /* Loop over each method name. At this level, all overloads of a name
1227 are counted as a single name. There is an inner loop which loops over
1230 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1231 method_counter
>= 0;
1234 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1236 if (symbol_name_compare (method_name
, lookup_name
, NULL
))
1240 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1246 const char *phys_name
;
1248 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1249 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1251 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1252 result_names
->push_back (phys_name
);
1258 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1259 superclasses
->push_back (TYPE_BASECLASS (t
, ibase
));
1262 /* The string equivalent of find_toplevel_char. Returns a pointer
1263 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1264 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1267 find_toplevel_string (const char *haystack
, const char *needle
)
1269 const char *s
= haystack
;
1273 s
= find_toplevel_char (s
, *needle
);
1277 /* Found first char in HAYSTACK; check rest of string. */
1278 if (startswith (s
, needle
))
1281 /* Didn't find it; loop over HAYSTACK, looking for the next
1282 instance of the first character of NEEDLE. */
1286 while (s
!= NULL
&& *s
!= '\0');
1288 /* NEEDLE was not found in HAYSTACK. */
1292 /* Convert CANONICAL to its string representation using
1293 symtab_to_fullname for SYMTAB. */
1296 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1298 if (canonical
->symtab
== NULL
)
1299 return canonical
->suffix
;
1301 return string_printf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1302 canonical
->suffix
.c_str ());
1305 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1306 and store the result in SELF->CANONICAL. */
1309 filter_results (struct linespec_state
*self
,
1310 std::vector
<symtab_and_line
> *result
,
1311 const std::vector
<const char *> &filters
)
1313 for (const char *name
: filters
)
1317 for (size_t j
= 0; j
< result
->size (); ++j
)
1319 const struct linespec_canonical_name
*canonical
;
1321 canonical
= &self
->canonical_names
[j
];
1322 std::string fullform
= canonical_to_fullform (canonical
);
1324 if (name
== fullform
)
1325 lsal
.sals
.push_back ((*result
)[j
]);
1328 if (!lsal
.sals
.empty ())
1330 lsal
.canonical
= xstrdup (name
);
1331 self
->canonical
->lsals
.push_back (std::move (lsal
));
1335 self
->canonical
->pre_expanded
= 0;
1338 /* Store RESULT into SELF->CANONICAL. */
1341 convert_results_to_lsals (struct linespec_state
*self
,
1342 std::vector
<symtab_and_line
> *result
)
1344 struct linespec_sals lsal
;
1346 lsal
.canonical
= NULL
;
1347 lsal
.sals
= std::move (*result
);
1348 self
->canonical
->lsals
.push_back (std::move (lsal
));
1351 /* A structure that contains two string representations of a struct
1352 linespec_canonical_name:
1353 - one where the symtab's fullname is used;
1354 - one where the filename followed the "set filename-display"
1357 struct decode_line_2_item
1359 decode_line_2_item (std::string
&&fullform_
, std::string
&&displayform_
,
1361 : fullform (std::move (fullform_
)),
1362 displayform (std::move (displayform_
)),
1363 selected (selected_
)
1367 /* Used for sorting. */
1368 bool operator< (const decode_line_2_item
&other
) const
1370 if (displayform
!= other
.displayform
)
1371 return displayform
< other
.displayform
;
1372 return fullform
< other
.fullform
;
1375 /* The form using symtab_to_fullname. */
1376 std::string fullform
;
1378 /* The form using symtab_to_filename_for_display. */
1379 std::string displayform
;
1381 /* True if the user requested breakpoint for this entry. */
1385 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1386 will either return normally, throw an exception on multiple
1387 results, or present a menu to the user. On return, the SALS vector
1388 in SELF->CANONICAL is set up properly. */
1391 decode_line_2 (struct linespec_state
*self
,
1392 std::vector
<symtab_and_line
> *result
,
1393 const char *select_mode
)
1398 std::vector
<const char *> filters
;
1399 std::vector
<struct decode_line_2_item
> items
;
1401 gdb_assert (select_mode
!= multiple_symbols_all
);
1402 gdb_assert (self
->canonical
!= NULL
);
1403 gdb_assert (!result
->empty ());
1405 /* Prepare ITEMS array. */
1406 for (i
= 0; i
< result
->size (); ++i
)
1408 const struct linespec_canonical_name
*canonical
;
1409 std::string displayform
;
1411 canonical
= &self
->canonical_names
[i
];
1413 std::string fullform
= canonical_to_fullform (canonical
);
1415 if (canonical
->symtab
== NULL
)
1416 displayform
= canonical
->suffix
;
1419 const char *fn_for_display
;
1421 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1422 displayform
= string_printf ("%s:%s", fn_for_display
,
1423 canonical
->suffix
.c_str ());
1426 items
.emplace_back (std::move (fullform
), std::move (displayform
),
1430 /* Sort the list of method names. */
1431 std::sort (items
.begin (), items
.end ());
1433 /* Remove entries with the same FULLFORM. */
1434 items
.erase (std::unique (items
.begin (), items
.end (),
1435 [] (const struct decode_line_2_item
&a
,
1436 const struct decode_line_2_item
&b
)
1438 return a
.fullform
== b
.fullform
;
1442 if (select_mode
== multiple_symbols_cancel
&& items
.size () > 1)
1443 error (_("canceled because the command is ambiguous\n"
1444 "See set/show multiple-symbol."));
1446 if (select_mode
== multiple_symbols_all
|| items
.size () == 1)
1448 convert_results_to_lsals (self
, result
);
1452 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1453 for (i
= 0; i
< items
.size (); i
++)
1454 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
.c_str ());
1456 prompt
= getenv ("PS2");
1463 args
= command_line_input (buffer
, prompt
, "overload-choice");
1465 if (args
== 0 || *args
== 0)
1466 error_no_arg (_("one or more choice numbers"));
1468 number_or_range_parser
parser (args
);
1469 while (!parser
.finished ())
1471 int num
= parser
.get_number ();
1474 error (_("canceled"));
1477 /* We intentionally make this result in a single breakpoint,
1478 contrary to what older versions of gdb did. The
1479 rationale is that this lets a user get the
1480 multiple_symbols_all behavior even with the 'ask'
1481 setting; and he can get separate breakpoints by entering
1482 "2-57" at the query. */
1483 convert_results_to_lsals (self
, result
);
1488 if (num
>= items
.size ())
1489 printf_unfiltered (_("No choice number %d.\n"), num
);
1492 struct decode_line_2_item
*item
= &items
[num
];
1494 if (!item
->selected
)
1496 filters
.push_back (item
->fullform
.c_str ());
1497 item
->selected
= true;
1501 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1507 filter_results (self
, result
, filters
);
1512 /* The parser of linespec itself. */
1514 /* Throw an appropriate error when SYMBOL is not found (optionally in
1517 [[noreturn
]] static void
1518 symbol_not_found_error (const char *symbol
, const char *filename
)
1523 if (!have_full_symbols (current_program_space
)
1524 && !have_partial_symbols (current_program_space
)
1525 && !have_minimal_symbols (current_program_space
))
1526 throw_error (NOT_FOUND_ERROR
,
1527 _("No symbol table is loaded. Use the \"file\" command."));
1529 /* If SYMBOL starts with '$', the user attempted to either lookup
1530 a function/variable in his code starting with '$' or an internal
1531 variable of that name. Since we do not know which, be concise and
1532 explain both possibilities. */
1536 throw_error (NOT_FOUND_ERROR
,
1537 _("Undefined convenience variable or function \"%s\" "
1538 "not defined in \"%s\"."), symbol
, filename
);
1540 throw_error (NOT_FOUND_ERROR
,
1541 _("Undefined convenience variable or function \"%s\" "
1542 "not defined."), symbol
);
1547 throw_error (NOT_FOUND_ERROR
,
1548 _("Function \"%s\" not defined in \"%s\"."),
1551 throw_error (NOT_FOUND_ERROR
,
1552 _("Function \"%s\" not defined."), symbol
);
1556 /* Throw an appropriate error when an unexpected token is encountered
1559 [[noreturn
]] static void
1560 unexpected_linespec_error (linespec_parser
*parser
)
1562 linespec_token token
;
1563 static const char * token_type_strings
[]
1564 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1566 /* Get the token that generated the error. */
1567 token
= linespec_lexer_lex_one (parser
);
1569 /* Finally, throw the error. */
1570 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1571 || token
.type
== LSTOKEN_KEYWORD
)
1573 gdb::unique_xmalloc_ptr
<char> string
= copy_token_string (token
);
1574 throw_error (GENERIC_ERROR
,
1575 _("malformed linespec error: unexpected %s, \"%s\""),
1576 token_type_strings
[token
.type
], string
.get ());
1579 throw_error (GENERIC_ERROR
,
1580 _("malformed linespec error: unexpected %s"),
1581 token_type_strings
[token
.type
]);
1584 /* Throw an undefined label error. */
1586 [[noreturn
]] static void
1587 undefined_label_error (const char *function
, const char *label
)
1589 if (function
!= NULL
)
1590 throw_error (NOT_FOUND_ERROR
,
1591 _("No label \"%s\" defined in function \"%s\"."),
1594 throw_error (NOT_FOUND_ERROR
,
1595 _("No label \"%s\" defined in current function."),
1599 /* Throw a source file not found error. */
1601 [[noreturn
]] static void
1602 source_file_not_found_error (const char *name
)
1604 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), name
);
1607 /* Unless at EIO, save the current stream position as completion word
1608 point, and consume the next token. */
1610 static linespec_token
1611 save_stream_and_consume_token (linespec_parser
*parser
)
1613 if (linespec_lexer_peek_token (parser
).type
!= LSTOKEN_EOI
)
1614 parser
->completion_word
= parser
->lexer
.stream
;
1615 return linespec_lexer_consume_token (parser
);
1618 /* See description in linespec.h. */
1621 linespec_parse_line_offset (const char *string
)
1623 const char *start
= string
;
1624 struct line_offset line_offset
;
1628 line_offset
.sign
= LINE_OFFSET_PLUS
;
1631 else if (*string
== '-')
1633 line_offset
.sign
= LINE_OFFSET_MINUS
;
1637 line_offset
.sign
= LINE_OFFSET_NONE
;
1639 if (*string
!= '\0' && !isdigit (*string
))
1640 error (_("malformed line offset: \"%s\""), start
);
1642 /* Right now, we only allow base 10 for offsets. */
1643 line_offset
.offset
= atoi (string
);
1647 /* In completion mode, if the user is still typing the number, there's
1648 no possible completion to offer. But if there's already input past
1649 the number, setup to expect NEXT. */
1652 set_completion_after_number (linespec_parser
*parser
,
1653 linespec_complete_what next
)
1655 if (*parser
->lexer
.stream
== ' ')
1657 parser
->completion_word
= skip_spaces (parser
->lexer
.stream
+ 1);
1658 parser
->complete_what
= next
;
1662 parser
->completion_word
= parser
->lexer
.stream
;
1663 parser
->complete_what
= linespec_complete_what::NOTHING
;
1667 /* Parse the basic_spec in PARSER's input. */
1670 linespec_parse_basic (linespec_parser
*parser
)
1672 gdb::unique_xmalloc_ptr
<char> name
;
1673 linespec_token token
;
1675 /* Get the next token. */
1676 token
= linespec_lexer_lex_one (parser
);
1678 /* If it is EOI or KEYWORD, issue an error. */
1679 if (token
.type
== LSTOKEN_KEYWORD
)
1681 parser
->complete_what
= linespec_complete_what::NOTHING
;
1682 unexpected_linespec_error (parser
);
1684 else if (token
.type
== LSTOKEN_EOI
)
1686 unexpected_linespec_error (parser
);
1688 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1689 else if (token
.type
== LSTOKEN_NUMBER
)
1691 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1693 /* Record the line offset and get the next token. */
1694 name
= copy_token_string (token
);
1695 parser
->result
.explicit_loc
.line_offset
1696 = linespec_parse_line_offset (name
.get ());
1698 /* Get the next token. */
1699 token
= linespec_lexer_consume_token (parser
);
1701 /* If the next token is a comma, stop parsing and return. */
1702 if (token
.type
== LSTOKEN_COMMA
)
1704 parser
->complete_what
= linespec_complete_what::NOTHING
;
1708 /* If the next token is anything but EOI or KEYWORD, issue
1710 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1711 unexpected_linespec_error (parser
);
1714 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1717 /* Next token must be LSTOKEN_STRING. */
1718 if (token
.type
!= LSTOKEN_STRING
)
1720 parser
->complete_what
= linespec_complete_what::NOTHING
;
1721 unexpected_linespec_error (parser
);
1724 /* The current token will contain the name of a function, method,
1726 name
= copy_token_string (token
);
1728 if (parser
->completion_tracker
!= NULL
)
1730 /* If the function name ends with a ":", then this may be an
1731 incomplete "::" scope operator instead of a label separator.
1734 which should expand to:
1737 Do a tentative completion assuming the later. If we find
1738 completions, advance the stream past the colon token and make
1739 it part of the function name/token. */
1741 if (!parser
->completion_quote_char
1742 && strcmp (parser
->lexer
.stream
, ":") == 0)
1744 completion_tracker
tmp_tracker (false);
1745 const char *source_filename
1746 = parser
->result
.explicit_loc
.source_filename
.get ();
1747 symbol_name_match_type match_type
1748 = parser
->result
.explicit_loc
.func_name_match_type
;
1750 linespec_complete_function (tmp_tracker
,
1751 parser
->completion_word
,
1755 if (tmp_tracker
.have_completions ())
1757 parser
->lexer
.stream
++;
1758 token
.data
.string
.length
++;
1760 name
.reset (savestring (parser
->completion_word
,
1761 (parser
->lexer
.stream
1762 - parser
->completion_word
)));
1766 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1770 std::vector
<block_symbol
> symbols
;
1771 std::vector
<bound_minimal_symbol
> minimal_symbols
;
1773 /* Try looking it up as a function/method. */
1774 find_linespec_symbols (&parser
->state
,
1775 parser
->result
.file_symtabs
, name
.get (),
1776 parser
->result
.explicit_loc
.func_name_match_type
,
1777 &symbols
, &minimal_symbols
);
1779 if (!symbols
.empty () || !minimal_symbols
.empty ())
1781 parser
->result
.function_symbols
= std::move (symbols
);
1782 parser
->result
.minimal_symbols
= std::move (minimal_symbols
);
1783 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1787 /* NAME was not a function or a method. So it must be a label
1788 name or user specified variable like "break foo.c:$zippo". */
1789 std::vector
<block_symbol
> labels
1790 = find_label_symbols (&parser
->state
, {}, &symbols
,
1793 if (!labels
.empty ())
1795 parser
->result
.labels
.label_symbols
= std::move (labels
);
1796 parser
->result
.labels
.function_symbols
= std::move (symbols
);
1797 parser
->result
.explicit_loc
.label_name
= std::move (name
);
1799 else if (token
.type
== LSTOKEN_STRING
1800 && *token
.data
.string
.ptr
== '$')
1802 /* User specified a convenience variable or history value. */
1803 parser
->result
.explicit_loc
.line_offset
1804 = linespec_parse_variable (&parser
->state
, name
.get ());
1806 if (parser
->result
.explicit_loc
.line_offset
.sign
1807 == LINE_OFFSET_UNKNOWN
)
1809 /* The user-specified variable was not valid. Do not
1810 throw an error here. parse_linespec will do it for us. */
1811 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1817 /* The name is also not a label. Abort parsing. Do not throw
1818 an error here. parse_linespec will do it for us. */
1820 /* Save a copy of the name we were trying to lookup. */
1821 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1827 int previous_qc
= parser
->completion_quote_char
;
1829 /* Get the next token. */
1830 token
= linespec_lexer_consume_token (parser
);
1832 if (token
.type
== LSTOKEN_EOI
)
1834 if (previous_qc
&& !parser
->completion_quote_char
)
1835 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1837 else if (token
.type
== LSTOKEN_COLON
)
1839 /* User specified a label or a lineno. */
1840 token
= linespec_lexer_consume_token (parser
);
1842 if (token
.type
== LSTOKEN_NUMBER
)
1844 /* User specified an offset. Record the line offset and
1845 get the next token. */
1846 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1848 name
= copy_token_string (token
);
1849 parser
->result
.explicit_loc
.line_offset
1850 = linespec_parse_line_offset (name
.get ());
1852 /* Get the next token. */
1853 token
= linespec_lexer_consume_token (parser
);
1855 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
1857 parser
->complete_what
= linespec_complete_what::LABEL
;
1859 else if (token
.type
== LSTOKEN_STRING
)
1861 parser
->complete_what
= linespec_complete_what::LABEL
;
1863 /* If we have text after the label separated by whitespace
1864 (e.g., "b func():lab i<tab>"), don't consider it part of
1865 the label. In completion mode that should complete to
1866 "if", in normal mode, the 'i' should be treated as
1868 if (parser
->completion_quote_char
== '\0')
1870 const char *ptr
= token
.data
.string
.ptr
;
1871 for (size_t i
= 0; i
< token
.data
.string
.length
; i
++)
1875 token
.data
.string
.length
= i
;
1876 parser
->lexer
.stream
= skip_spaces (ptr
+ i
+ 1);
1882 if (parser
->completion_tracker
!= NULL
)
1884 if (parser
->lexer
.stream
[-1] == ' ')
1886 parser
->completion_word
= parser
->lexer
.stream
;
1887 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1892 std::vector
<block_symbol
> symbols
;
1894 /* Grab a copy of the label's name and look it up. */
1895 name
= copy_token_string (token
);
1896 std::vector
<block_symbol
> labels
1897 = find_label_symbols (&parser
->state
,
1898 parser
->result
.function_symbols
,
1899 &symbols
, name
.get ());
1901 if (!labels
.empty ())
1903 parser
->result
.labels
.label_symbols
= std::move (labels
);
1904 parser
->result
.labels
.function_symbols
= std::move (symbols
);
1905 parser
->result
.explicit_loc
.label_name
= std::move (name
);
1909 /* We don't know what it was, but it isn't a label. */
1910 undefined_label_error
1911 (parser
->result
.explicit_loc
.function_name
.get (),
1917 /* Check for a line offset. */
1918 token
= save_stream_and_consume_token (parser
);
1919 if (token
.type
== LSTOKEN_COLON
)
1921 /* Get the next token. */
1922 token
= linespec_lexer_consume_token (parser
);
1924 /* It must be a line offset. */
1925 if (token
.type
!= LSTOKEN_NUMBER
)
1926 unexpected_linespec_error (parser
);
1928 /* Record the line offset and get the next token. */
1929 name
= copy_token_string (token
);
1931 parser
->result
.explicit_loc
.line_offset
1932 = linespec_parse_line_offset (name
.get ());
1934 /* Get the next token. */
1935 token
= linespec_lexer_consume_token (parser
);
1940 /* Trailing ':' in the input. Issue an error. */
1941 unexpected_linespec_error (parser
);
1946 /* Canonicalize the linespec contained in LS. The result is saved into
1947 STATE->canonical. This function handles both linespec and explicit
1951 canonicalize_linespec (struct linespec_state
*state
, const linespec
*ls
)
1953 /* If canonicalization was not requested, no need to do anything. */
1954 if (!state
->canonical
)
1957 /* Save everything as an explicit location. */
1958 state
->canonical
->locspec
= ls
->explicit_loc
.clone ();
1959 explicit_location_spec
*explicit_loc
1960 = as_explicit_location_spec (state
->canonical
->locspec
.get ());
1962 if (explicit_loc
->label_name
!= NULL
)
1964 state
->canonical
->special_display
= 1;
1966 if (explicit_loc
->function_name
== NULL
)
1968 /* No function was specified, so add the symbol name. */
1969 gdb_assert (ls
->labels
.function_symbols
.size () == 1);
1970 block_symbol s
= ls
->labels
.function_symbols
.front ();
1971 explicit_loc
->function_name
1972 = make_unique_xstrdup (s
.symbol
->natural_name ());
1976 /* If this location originally came from a linespec, save a string
1977 representation of it for display and saving to file. */
1978 if (state
->is_linespec
)
1979 explicit_loc
->set_string (explicit_loc
->to_linespec ());
1982 /* Given a line offset in LS, construct the relevant SALs. */
1984 static std::vector
<symtab_and_line
>
1985 create_sals_line_offset (struct linespec_state
*self
,
1988 bool use_default
= false;
1990 /* This is where we need to make sure we have good defaults.
1991 We must guarantee that this section of code is never executed
1992 when we are called with just a function name, since
1993 set_default_source_symtab_and_line uses
1994 select_source_symtab that calls us with such an argument. */
1996 if (ls
->file_symtabs
.size () == 1
1997 && ls
->file_symtabs
.front () == nullptr)
1999 set_current_program_space (self
->program_space
);
2001 /* Make sure we have at least a default source line. */
2002 set_default_source_symtab_and_line ();
2003 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
2005 = collect_symtabs_from_filename (self
->default_symtab
->filename
,
2006 self
->search_pspace
);
2010 symtab_and_line val
;
2011 val
.line
= ls
->explicit_loc
.line_offset
.offset
;
2012 switch (ls
->explicit_loc
.line_offset
.sign
)
2014 case LINE_OFFSET_PLUS
:
2015 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2018 val
.line
= self
->default_line
+ val
.line
;
2021 case LINE_OFFSET_MINUS
:
2022 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2025 val
.line
= self
->default_line
- val
.line
;
2027 val
.line
= -val
.line
;
2030 case LINE_OFFSET_NONE
:
2031 break; /* No need to adjust val.line. */
2034 std::vector
<symtab_and_line
> values
;
2035 if (self
->list_mode
)
2036 values
= decode_digits_list_mode (self
, ls
, val
);
2039 const linetable_entry
*best_entry
= NULL
;
2042 /* True if the provided line gave an exact match. False if we had to
2043 search for the next following line with code. */
2044 bool was_exact
= true;
2046 std::vector
<symtab_and_line
> intermediate_results
2047 = decode_digits_ordinary (self
, ls
, val
.line
, &best_entry
);
2048 if (intermediate_results
.empty () && best_entry
!= NULL
)
2051 intermediate_results
= decode_digits_ordinary (self
, ls
,
2056 /* For optimized code, the compiler can scatter one source line
2057 across disjoint ranges of PC values, even when no duplicate
2058 functions or inline functions are involved. For example,
2059 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2060 function can result in two PC ranges. In this case, we don't
2061 want to set a breakpoint on the first PC of each range. To filter
2062 such cases, we use containing blocks -- for each PC found
2063 above, we see if there are other PCs that are in the same
2064 block. If yes, the other PCs are filtered out. */
2066 gdb::def_vector
<int> filter (intermediate_results
.size ());
2067 gdb::def_vector
<const block
*> blocks (intermediate_results
.size ());
2069 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2071 set_current_program_space (intermediate_results
[i
].pspace
);
2074 blocks
[i
] = block_for_pc_sect (intermediate_results
[i
].pc
,
2075 intermediate_results
[i
].section
);
2078 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2080 if (blocks
[i
] != NULL
)
2081 for (j
= i
+ 1; j
< intermediate_results
.size (); ++j
)
2083 if (blocks
[j
] == blocks
[i
])
2091 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2094 struct symbol
*sym
= (blocks
[i
]
2095 ? blocks
[i
]->containing_function ()
2097 symtab_and_line
&sal
= intermediate_results
[i
];
2099 /* Don't consider a match if:
2101 - the provided line did not give an exact match (so we
2102 started looking for lines below until we found one with
2103 code associated to it)
2104 - the found location is exactly the start of a function
2105 - the provided line is above the declaration line of the
2108 Consider the following source:
2110 10 } // end of a previous function
2120 The intent of this heuristic is that a breakpoint requested on
2121 line 11 and 12 will not result in a breakpoint on main, but a
2122 breakpoint on line 13 will. A breakpoint requested on the empty
2123 line 16 will also result in a breakpoint in main, at line 17. */
2126 && sym
->aclass () == LOC_BLOCK
2127 && sal
.pc
== sym
->value_block ()->entry_pc ()
2128 && val
.line
< sym
->line ())
2131 if (self
->funfirstline
)
2132 skip_prologue_sal (&sal
);
2135 add_sal_to_sals (self
, &values
, &sal
,
2136 sym
? sym
->natural_name () : nullptr, false);
2140 if (values
.empty ())
2142 if (ls
->explicit_loc
.source_filename
)
2143 throw_error (NOT_FOUND_ERROR
,
2144 _("No compiled code for line %d in file \"%s\"."),
2145 val
.line
, ls
->explicit_loc
.source_filename
.get ());
2147 throw_error (NOT_FOUND_ERROR
,
2148 _("No compiled code for line %d in the current file."),
2155 /* Convert the given ADDRESS into SaLs. */
2157 static std::vector
<symtab_and_line
>
2158 convert_address_location_to_sals (struct linespec_state
*self
,
2161 symtab_and_line sal
= find_pc_line (address
, 0);
2163 sal
.section
= find_pc_overlay (address
);
2164 sal
.explicit_pc
= 1;
2165 sal
.symbol
= find_pc_sect_containing_function (sal
.pc
, sal
.section
);
2167 std::vector
<symtab_and_line
> sals
;
2168 add_sal_to_sals (self
, &sals
, &sal
, core_addr_to_string (address
), true);
2173 /* Create and return SALs from the linespec LS. */
2175 static std::vector
<symtab_and_line
>
2176 convert_linespec_to_sals (struct linespec_state
*state
, linespec
*ls
)
2178 std::vector
<symtab_and_line
> sals
;
2180 if (!ls
->labels
.label_symbols
.empty ())
2182 /* We have just a bunch of functions/methods or labels. */
2183 struct symtab_and_line sal
;
2185 for (const auto &sym
: ls
->labels
.label_symbols
)
2187 struct program_space
*pspace
2188 = sym
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
2190 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2191 && state
->maybe_add_address (pspace
, sal
.pc
))
2192 add_sal_to_sals (state
, &sals
, &sal
,
2193 sym
.symbol
->natural_name (), false);
2196 else if (!ls
->function_symbols
.empty () || !ls
->minimal_symbols
.empty ())
2198 /* We have just a bunch of functions and/or methods. */
2199 if (!ls
->function_symbols
.empty ())
2201 /* Sort symbols so that symbols with the same program space are next
2203 std::sort (ls
->function_symbols
.begin (),
2204 ls
->function_symbols
.end (),
2207 for (const auto &sym
: ls
->function_symbols
)
2209 program_space
*pspace
2210 = sym
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
2211 set_current_program_space (pspace
);
2213 /* Don't skip to the first line of the function if we
2214 had found an ifunc minimal symbol for this function,
2215 because that means that this function is an ifunc
2216 resolver with the same name as the ifunc itself. */
2217 bool found_ifunc
= false;
2219 if (state
->funfirstline
2220 && !ls
->minimal_symbols
.empty ()
2221 && sym
.symbol
->aclass () == LOC_BLOCK
)
2223 const CORE_ADDR addr
2224 = sym
.symbol
->value_block ()->entry_pc ();
2226 for (const auto &elem
: ls
->minimal_symbols
)
2228 if (elem
.minsym
->type () == mst_text_gnu_ifunc
2229 || elem
.minsym
->type () == mst_data_gnu_ifunc
)
2231 CORE_ADDR msym_addr
= elem
.value_address ();
2232 if (elem
.minsym
->type () == mst_data_gnu_ifunc
)
2234 struct gdbarch
*gdbarch
2235 = elem
.objfile
->arch ();
2237 = (gdbarch_convert_from_func_ptr_addr
2240 current_inferior ()->top_target ()));
2243 if (msym_addr
== addr
)
2254 symtab_and_line sal
;
2255 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2256 && state
->maybe_add_address (pspace
, sal
.pc
))
2257 add_sal_to_sals (state
, &sals
, &sal
,
2258 sym
.symbol
->natural_name (), false);
2263 if (!ls
->minimal_symbols
.empty ())
2265 /* Sort minimal symbols by program space, too */
2266 std::sort (ls
->minimal_symbols
.begin (),
2267 ls
->minimal_symbols
.end (),
2270 for (const auto &elem
: ls
->minimal_symbols
)
2272 program_space
*pspace
= elem
.objfile
->pspace ();
2273 set_current_program_space (pspace
);
2274 minsym_found (state
, elem
.objfile
, elem
.minsym
, &sals
);
2278 else if (ls
->explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2280 /* Only an offset was specified. */
2281 sals
= create_sals_line_offset (state
, ls
);
2283 /* Make sure we have a filename for canonicalization. */
2284 if (ls
->explicit_loc
.source_filename
== NULL
)
2286 const char *filename
= state
->default_symtab
->filename
;
2288 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2289 form so that displaying SOURCE_FILENAME can follow the current
2290 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2291 it has been kept for code simplicity only in absolute form. */
2292 ls
->explicit_loc
.source_filename
= make_unique_xstrdup (filename
);
2297 /* We haven't found any results... */
2301 canonicalize_linespec (state
, ls
);
2303 if (!sals
.empty () && state
->canonical
!= NULL
)
2304 state
->canonical
->pre_expanded
= 1;
2309 /* Build RESULT from the explicit location spec components
2310 SOURCE_FILENAME, FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2313 convert_explicit_location_spec_to_linespec
2314 (struct linespec_state
*self
,
2316 const char *source_filename
,
2317 const char *function_name
,
2318 symbol_name_match_type fname_match_type
,
2319 const char *label_name
,
2320 struct line_offset line_offset
)
2322 std::vector
<bound_minimal_symbol
> minimal_symbols
;
2324 result
->explicit_loc
.func_name_match_type
= fname_match_type
;
2326 if (source_filename
!= NULL
)
2330 result
->file_symtabs
2331 = symtabs_from_filename (source_filename
, self
->search_pspace
);
2333 catch (const gdb_exception_error
&except
)
2335 source_file_not_found_error (source_filename
);
2337 result
->explicit_loc
.source_filename
2338 = make_unique_xstrdup (source_filename
);
2342 /* A NULL entry means to use the default symtab. */
2343 result
->file_symtabs
.push_back (nullptr);
2346 if (function_name
!= NULL
)
2348 std::vector
<block_symbol
> symbols
;
2350 find_linespec_symbols (self
, result
->file_symtabs
,
2351 function_name
, fname_match_type
,
2352 &symbols
, &minimal_symbols
);
2354 if (symbols
.empty () && minimal_symbols
.empty ())
2355 symbol_not_found_error (function_name
,
2356 result
->explicit_loc
.source_filename
.get ());
2358 result
->explicit_loc
.function_name
2359 = make_unique_xstrdup (function_name
);
2360 result
->function_symbols
= std::move (symbols
);
2361 result
->minimal_symbols
= std::move (minimal_symbols
);
2364 if (label_name
!= NULL
)
2366 std::vector
<block_symbol
> symbols
;
2367 std::vector
<block_symbol
> labels
2368 = find_label_symbols (self
, result
->function_symbols
,
2369 &symbols
, label_name
);
2371 if (labels
.empty ())
2372 undefined_label_error (result
->explicit_loc
.function_name
.get (),
2375 result
->explicit_loc
.label_name
= make_unique_xstrdup (label_name
);
2376 result
->labels
.label_symbols
= labels
;
2377 result
->labels
.function_symbols
= std::move (symbols
);
2380 if (line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2381 result
->explicit_loc
.line_offset
= line_offset
;
2384 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2386 static std::vector
<symtab_and_line
>
2387 convert_explicit_location_spec_to_sals
2388 (struct linespec_state
*self
,
2390 const explicit_location_spec
*explicit_spec
)
2392 convert_explicit_location_spec_to_linespec (self
, result
,
2393 explicit_spec
->source_filename
.get (),
2394 explicit_spec
->function_name
.get (),
2395 explicit_spec
->func_name_match_type
,
2396 explicit_spec
->label_name
.get (),
2397 explicit_spec
->line_offset
);
2398 return convert_linespec_to_sals (self
, result
);
2401 /* Parse a string that specifies a linespec.
2403 The basic grammar of linespecs:
2405 linespec -> var_spec | basic_spec
2406 var_spec -> '$' (STRING | NUMBER)
2408 basic_spec -> file_offset_spec | function_spec | label_spec
2409 file_offset_spec -> opt_file_spec offset_spec
2410 function_spec -> opt_file_spec function_name_spec opt_label_spec
2411 label_spec -> label_name_spec
2413 opt_file_spec -> "" | file_name_spec ':'
2414 opt_label_spec -> "" | ':' label_name_spec
2416 file_name_spec -> STRING
2417 function_name_spec -> STRING
2418 label_name_spec -> STRING
2419 function_name_spec -> STRING
2420 offset_spec -> NUMBER
2424 This may all be followed by several keywords such as "if EXPR",
2427 A comma will terminate parsing.
2429 The function may be an undebuggable function found in minimal symbol table.
2431 If the argument FUNFIRSTLINE is nonzero, we want the first line
2432 of real code inside a function when a function is specified, and it is
2433 not OK to specify a variable or type to get its line number.
2435 DEFAULT_SYMTAB specifies the file to use if none is specified.
2436 It defaults to current_source_symtab.
2437 DEFAULT_LINE specifies the line number to use for relative
2438 line numbers (that start with signs). Defaults to current_source_line.
2439 If CANONICAL is non-NULL, store an array of strings containing the canonical
2440 line specs there if necessary. Currently overloaded member functions and
2441 line numbers or static functions without a filename yield a canonical
2442 line spec. The array and the line spec strings are allocated on the heap,
2443 it is the callers responsibility to free them.
2445 Note that it is possible to return zero for the symtab
2446 if no file is validly specified. Callers must check that.
2447 Also, the line number returned may be invalid. */
2449 /* Parse the linespec in ARG, which must not be nullptr. MATCH_TYPE
2450 indicates how function names should be matched. */
2452 static std::vector
<symtab_and_line
>
2453 parse_linespec (linespec_parser
*parser
, const char *arg
,
2454 symbol_name_match_type match_type
)
2456 gdb_assert (arg
!= nullptr);
2458 struct gdb_exception file_exception
;
2460 /* A special case to start. It has become quite popular for
2461 IDEs to work around bugs in the previous parser by quoting
2462 the entire linespec, so we attempt to deal with this nicely. */
2463 parser
->is_quote_enclosed
= false;
2464 if (parser
->completion_tracker
== NULL
2465 && !is_ada_operator (arg
)
2467 && strchr (linespec_quote_characters
, *arg
) != NULL
)
2469 const char *end
= skip_quote_char (arg
+ 1, *arg
);
2470 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2472 /* Here's the special case. Skip ARG past the initial
2475 parser
->is_quote_enclosed
= true;
2479 parser
->lexer
.saved_arg
= arg
;
2480 parser
->lexer
.stream
= arg
;
2481 parser
->completion_word
= arg
;
2482 parser
->complete_what
= linespec_complete_what::FUNCTION
;
2483 parser
->result
.explicit_loc
.func_name_match_type
= match_type
;
2485 /* Initialize the default symtab and line offset. */
2486 initialize_defaults (&parser
->state
.default_symtab
,
2487 &parser
->state
.default_line
);
2489 /* Objective-C shortcut. */
2490 if (parser
->completion_tracker
== NULL
)
2492 std::vector
<symtab_and_line
> values
2493 = decode_objc (&parser
->state
, &parser
->result
, arg
);
2494 if (!values
.empty ())
2499 /* "-"/"+" is either an objc selector, or a number. There's
2500 nothing to complete the latter to, so just let the caller
2501 complete on functions, which finds objc selectors, if there's
2503 if ((arg
[0] == '-' || arg
[0] == '+') && arg
[1] == '\0')
2507 /* Start parsing. */
2509 /* Get the first token. */
2510 linespec_token token
= linespec_lexer_consume_token (parser
);
2512 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2513 if (token
.type
== LSTOKEN_STRING
&& *token
.data
.string
.ptr
== '$')
2515 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2516 if (parser
->completion_tracker
== NULL
)
2517 parser
->result
.file_symtabs
.push_back (nullptr);
2519 /* User specified a convenience variable or history value. */
2520 gdb::unique_xmalloc_ptr
<char> var
= copy_token_string (token
);
2521 parser
->result
.explicit_loc
.line_offset
2522 = linespec_parse_variable (&parser
->state
, var
.get ());
2524 /* If a line_offset wasn't found (VAR is the name of a user
2525 variable/function), then skip to normal symbol processing. */
2526 if (parser
->result
.explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2528 /* Consume this token. */
2529 linespec_lexer_consume_token (parser
);
2531 goto convert_to_sals
;
2534 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
2536 /* Let the default linespec_complete_what::FUNCTION kick in. */
2537 unexpected_linespec_error (parser
);
2539 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2541 parser
->complete_what
= linespec_complete_what::NOTHING
;
2542 unexpected_linespec_error (parser
);
2545 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2546 this token cannot represent a filename. */
2547 token
= linespec_lexer_peek_token (parser
);
2549 if (token
.type
== LSTOKEN_COLON
)
2551 /* Get the current token again and extract the filename. */
2552 token
= linespec_lexer_lex_one (parser
);
2553 gdb::unique_xmalloc_ptr
<char> user_filename
= copy_token_string (token
);
2555 /* Check if the input is a filename. */
2558 parser
->result
.file_symtabs
2559 = symtabs_from_filename (user_filename
.get (),
2560 parser
->state
.search_pspace
);
2562 catch (gdb_exception_error
&ex
)
2564 file_exception
= std::move (ex
);
2567 if (file_exception
.reason
>= 0)
2569 /* Symtabs were found for the file. Record the filename. */
2570 parser
->result
.explicit_loc
.source_filename
= std::move (user_filename
);
2572 /* Get the next token. */
2573 token
= linespec_lexer_consume_token (parser
);
2575 /* This is LSTOKEN_COLON; consume it. */
2576 linespec_lexer_consume_token (parser
);
2580 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2581 parser
->result
.file_symtabs
.push_back (nullptr);
2584 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2585 else if (parser
->completion_tracker
== NULL
2586 && (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2587 && token
.type
!= LSTOKEN_COMMA
))
2589 /* TOKEN is the _next_ token, not the one currently in the parser.
2590 Consuming the token will give the correct error message. */
2591 linespec_lexer_consume_token (parser
);
2592 unexpected_linespec_error (parser
);
2596 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2597 parser
->result
.file_symtabs
.push_back (nullptr);
2600 /* Parse the rest of the linespec. */
2601 linespec_parse_basic (parser
);
2603 if (parser
->completion_tracker
== NULL
2604 && parser
->result
.function_symbols
.empty ()
2605 && parser
->result
.labels
.label_symbols
.empty ()
2606 && parser
->result
.explicit_loc
.line_offset
.sign
== LINE_OFFSET_UNKNOWN
2607 && parser
->result
.minimal_symbols
.empty ())
2609 /* The linespec didn't parse. Re-throw the file exception if
2611 if (file_exception
.reason
< 0)
2612 throw_exception (std::move (file_exception
));
2614 /* Otherwise, the symbol is not found. */
2615 symbol_not_found_error
2616 (parser
->result
.explicit_loc
.function_name
.get (),
2617 parser
->result
.explicit_loc
.source_filename
.get ());
2622 /* Get the last token and record how much of the input was parsed,
2624 token
= linespec_lexer_lex_one (parser
);
2625 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2626 unexpected_linespec_error (parser
);
2627 else if (token
.type
== LSTOKEN_KEYWORD
)
2629 /* Setup the completion word past the keyword. Lexing never
2630 advances past a keyword automatically, so skip it
2632 parser
->completion_word
2633 = skip_spaces (skip_to_space (parser
->lexer
.stream
));
2634 parser
->complete_what
= linespec_complete_what::EXPRESSION
;
2637 /* Convert the data in the parser's result to SALs. */
2638 if (parser
->completion_tracker
== NULL
)
2639 return convert_linespec_to_sals (&parser
->state
, &parser
->result
);
2645 /* Initialize a new linespec parser. */
2647 linespec_parser::linespec_parser (int flags
,
2648 const struct language_defn
*language
,
2649 struct program_space
*search_pspace
,
2650 struct symtab
*default_symtab
,
2652 struct linespec_result
*canonical
)
2653 : state (flags
, language
, current_program_space
, search_pspace
,
2654 default_symtab
, default_line
, canonical
)
2656 lexer
.current
.type
= LSTOKEN_CONSUMED
;
2657 result
.explicit_loc
.func_name_match_type
2658 = symbol_name_match_type::WILD
;
2659 result
.explicit_loc
.line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2662 /* See description in linespec.h. */
2665 linespec_lex_to_end (const char **stringp
)
2667 linespec_token token
;
2670 if (stringp
== NULL
|| *stringp
== NULL
)
2673 linespec_parser
parser (0, current_language
, NULL
, NULL
, 0, NULL
);
2674 parser
.lexer
.saved_arg
= *stringp
;
2675 parser
.lexer
.stream
= orig
= *stringp
;
2679 /* Stop before any comma tokens; we need it to keep it
2680 as the next token in the string. */
2681 token
= linespec_lexer_peek_token (&parser
);
2682 if (token
.type
== LSTOKEN_COMMA
)
2684 token
= linespec_lexer_consume_token (&parser
);
2686 while (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
);
2688 *stringp
+= parser
.lexer
.stream
- orig
;
2691 /* See linespec.h. */
2694 linespec_complete_function (completion_tracker
&tracker
,
2695 const char *function
,
2696 symbol_name_match_type func_match_type
,
2697 const char *source_filename
)
2699 complete_symbol_mode mode
= complete_symbol_mode::LINESPEC
;
2701 if (source_filename
!= NULL
)
2703 collect_file_symbol_completion_matches (tracker
, mode
, func_match_type
,
2704 function
, function
, source_filename
);
2708 collect_symbol_completion_matches (tracker
, mode
, func_match_type
,
2709 function
, function
);
2714 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2715 only meaningful if COMPONENT is FUNCTION. */
2718 complete_linespec_component (linespec_parser
*parser
,
2719 completion_tracker
&tracker
,
2721 linespec_complete_what component
,
2722 const char *source_filename
)
2724 if (component
== linespec_complete_what::KEYWORD
)
2726 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
2728 else if (component
== linespec_complete_what::EXPRESSION
)
2731 = advance_to_expression_complete_word_point (tracker
, text
);
2732 complete_expression (tracker
, text
, word
);
2734 else if (component
== linespec_complete_what::FUNCTION
)
2736 completion_list fn_list
;
2738 symbol_name_match_type match_type
2739 = parser
->result
.explicit_loc
.func_name_match_type
;
2740 linespec_complete_function (tracker
, text
, match_type
, source_filename
);
2741 if (source_filename
== NULL
)
2743 /* Haven't seen a source component, like in "b
2744 file.c:function[TAB]". Maybe this wasn't a function, but
2745 a filename instead, like "b file.[TAB]". */
2746 fn_list
= complete_source_filenames (text
);
2749 /* If we only have a single filename completion, append a ':' for
2750 the user, since that's the only thing that can usefully follow
2752 if (fn_list
.size () == 1 && !tracker
.have_completions ())
2754 char *fn
= fn_list
[0].release ();
2756 /* If we also need to append a quote char, it needs to be
2757 appended before the ':'. Append it now, and make ':' the
2758 new "quote" char. */
2759 if (tracker
.quote_char ())
2761 char quote_char_str
[2] = { (char) tracker
.quote_char () };
2763 fn
= reconcat (fn
, fn
, quote_char_str
, (char *) NULL
);
2764 tracker
.set_quote_char (':');
2767 fn
= reconcat (fn
, fn
, ":", (char *) NULL
);
2768 fn_list
[0].reset (fn
);
2770 /* Tell readline to skip appending a space. */
2771 tracker
.set_suppress_append_ws (true);
2773 tracker
.add_completions (std::move (fn_list
));
2777 /* Helper for linespec_complete_label. Find labels that match
2778 LABEL_NAME in the function symbols listed in the PARSER, and add
2779 them to the tracker. */
2782 complete_label (completion_tracker
&tracker
,
2783 linespec_parser
*parser
,
2784 const char *label_name
)
2786 std::vector
<block_symbol
> label_function_symbols
;
2787 std::vector
<block_symbol
> labels
2788 = find_label_symbols (&parser
->state
,
2789 parser
->result
.function_symbols
,
2790 &label_function_symbols
,
2793 for (const auto &label
: labels
)
2795 char *match
= xstrdup (label
.symbol
->search_name ());
2796 tracker
.add_completion (gdb::unique_xmalloc_ptr
<char> (match
));
2800 /* See linespec.h. */
2803 linespec_complete_label (completion_tracker
&tracker
,
2804 const struct language_defn
*language
,
2805 const char *source_filename
,
2806 const char *function_name
,
2807 symbol_name_match_type func_name_match_type
,
2808 const char *label_name
)
2810 linespec_parser
parser (0, language
, NULL
, NULL
, 0, NULL
);
2812 line_offset unknown_offset
;
2816 convert_explicit_location_spec_to_linespec (&parser
.state
,
2820 func_name_match_type
,
2821 NULL
, unknown_offset
);
2823 catch (const gdb_exception_error
&ex
)
2828 complete_label (tracker
, &parser
, label_name
);
2831 /* See description in linespec.h. */
2834 linespec_complete (completion_tracker
&tracker
, const char *text
,
2835 symbol_name_match_type match_type
)
2837 const char *orig
= text
;
2839 linespec_parser
parser (0, current_language
, NULL
, NULL
, 0, NULL
);
2840 parser
.lexer
.saved_arg
= text
;
2841 parser
.result
.explicit_loc
.func_name_match_type
= match_type
;
2842 parser
.lexer
.stream
= text
;
2844 parser
.completion_tracker
= &tracker
;
2845 parser
.state
.is_linespec
= true;
2847 /* Parse as much as possible. parser.completion_word will hold
2848 furthest completion point we managed to parse to. */
2851 parse_linespec (&parser
, text
, match_type
);
2853 catch (const gdb_exception_error
&except
)
2857 if (parser
.completion_quote_char
!= '\0'
2858 && parser
.completion_quote_end
!= NULL
2859 && parser
.completion_quote_end
[1] == '\0')
2861 /* If completing a quoted string with the cursor right at
2862 terminating quote char, complete the completion word without
2863 interpretation, so that readline advances the cursor one
2864 whitespace past the quote, even if there's no match. This
2865 makes these cases behave the same:
2867 before: "b function()"
2868 after: "b function() "
2870 before: "b 'function()'"
2871 after: "b 'function()' "
2873 and trusts the user in this case:
2875 before: "b 'not_loaded_function_yet()'"
2876 after: "b 'not_loaded_function_yet()' "
2878 parser
.complete_what
= linespec_complete_what::NOTHING
;
2879 parser
.completion_quote_char
= '\0';
2881 gdb::unique_xmalloc_ptr
<char> text_copy
2882 (xstrdup (parser
.completion_word
));
2883 tracker
.add_completion (std::move (text_copy
));
2886 tracker
.set_quote_char (parser
.completion_quote_char
);
2888 if (parser
.complete_what
== linespec_complete_what::LABEL
)
2890 parser
.complete_what
= linespec_complete_what::NOTHING
;
2892 const char *func_name
= parser
.result
.explicit_loc
.function_name
.get ();
2894 std::vector
<block_symbol
> function_symbols
;
2895 std::vector
<bound_minimal_symbol
> minimal_symbols
;
2896 find_linespec_symbols (&parser
.state
,
2897 parser
.result
.file_symtabs
,
2898 func_name
, match_type
,
2899 &function_symbols
, &minimal_symbols
);
2901 parser
.result
.function_symbols
= std::move (function_symbols
);
2902 parser
.result
.minimal_symbols
= std::move (minimal_symbols
);
2903 complete_label (tracker
, &parser
, parser
.completion_word
);
2905 else if (parser
.complete_what
== linespec_complete_what::FUNCTION
)
2907 /* While parsing/lexing, we didn't know whether the completion
2908 word completes to a unique function/source name already or
2912 "b function() <tab>"
2913 may need to complete either to:
2914 "b function() const"
2916 "b function() if/thread/task"
2920 may need to complete either to:
2921 "b foo template_fun<T>()"
2922 with "foo" being the template function's return type, or to:
2927 may need to complete either to a source file name:
2929 or this, also a filename, but a unique completion:
2931 or to a function name:
2934 Address that by completing assuming source or function, and
2935 seeing if we find a completion that matches exactly the
2936 completion word. If so, then it must be a function (see note
2937 below) and we advance the completion word to the end of input
2938 and switch to KEYWORD completion mode.
2940 Note: if we find a unique completion for a source filename,
2941 then it won't match the completion word, because the LCD will
2942 contain a trailing ':'. And if we're completing at or after
2943 the ':', then complete_linespec_component won't try to
2944 complete on source filenames. */
2946 const char *word
= parser
.completion_word
;
2948 complete_linespec_component
2950 parser
.completion_word
,
2951 linespec_complete_what::FUNCTION
,
2952 parser
.result
.explicit_loc
.source_filename
.get ());
2954 parser
.complete_what
= linespec_complete_what::NOTHING
;
2956 if (tracker
.quote_char ())
2958 /* The function/file name was not close-quoted, so this
2959 can't be a keyword. Note: complete_linespec_component
2960 may have swapped the original quote char for ':' when we
2961 get here, but that still indicates the same. */
2963 else if (!tracker
.have_completions ())
2966 size_t wordlen
= strlen (parser
.completion_word
);
2969 = string_find_incomplete_keyword_at_end (linespec_keywords
,
2970 parser
.completion_word
,
2975 && parser
.completion_word
[wordlen
- 1] == ' '))
2977 parser
.completion_word
+= key_start
;
2978 parser
.complete_what
= linespec_complete_what::KEYWORD
;
2981 else if (tracker
.completes_to_completion_word (word
))
2983 /* Skip the function and complete on keywords. */
2984 parser
.completion_word
+= strlen (word
);
2985 parser
.complete_what
= linespec_complete_what::KEYWORD
;
2986 tracker
.discard_completions ();
2990 tracker
.advance_custom_word_point_by (parser
.completion_word
- orig
);
2992 complete_linespec_component
2994 parser
.completion_word
,
2995 parser
.complete_what
,
2996 parser
.result
.explicit_loc
.source_filename
.get ());
2998 /* If we're past the "filename:function:label:offset" linespec, and
2999 didn't find any match, then assume the user might want to create
3000 a pending breakpoint anyway and offer the keyword
3002 if (!parser
.completion_quote_char
3003 && (parser
.complete_what
== linespec_complete_what::FUNCTION
3004 || parser
.complete_what
== linespec_complete_what::LABEL
3005 || parser
.complete_what
== linespec_complete_what::NOTHING
)
3006 && !tracker
.have_completions ())
3009 = parser
.completion_word
+ strlen (parser
.completion_word
);
3011 if (end
> orig
&& end
[-1] == ' ')
3013 tracker
.advance_custom_word_point_by (end
- parser
.completion_word
);
3015 complete_linespec_component (&parser
, tracker
, end
,
3016 linespec_complete_what::KEYWORD
,
3022 /* A helper function for decode_line_full and decode_line_1 to
3023 turn LOCSPEC into std::vector<symtab_and_line>. */
3025 static std::vector
<symtab_and_line
>
3026 location_spec_to_sals (linespec_parser
*parser
,
3027 const location_spec
*locspec
)
3029 std::vector
<symtab_and_line
> result
;
3031 switch (locspec
->type ())
3033 case LINESPEC_LOCATION_SPEC
:
3035 const linespec_location_spec
*ls
= as_linespec_location_spec (locspec
);
3036 parser
->state
.is_linespec
= true;
3037 result
= parse_linespec (parser
, ls
->spec_string
.get (),
3042 case ADDRESS_LOCATION_SPEC
:
3044 const address_location_spec
*addr_spec
3045 = as_address_location_spec (locspec
);
3046 const char *addr_string
= addr_spec
->to_string ();
3049 if (addr_string
!= NULL
)
3051 addr
= linespec_expression_to_pc (&addr_string
);
3052 if (parser
->state
.canonical
!= NULL
)
3053 parser
->state
.canonical
->locspec
= locspec
->clone ();
3056 addr
= addr_spec
->address
;
3058 result
= convert_address_location_to_sals (&parser
->state
,
3063 case EXPLICIT_LOCATION_SPEC
:
3065 const explicit_location_spec
*explicit_locspec
3066 = as_explicit_location_spec (locspec
);
3067 result
= convert_explicit_location_spec_to_sals (&parser
->state
,
3073 case PROBE_LOCATION_SPEC
:
3074 /* Probes are handled by their own decoders. */
3075 gdb_assert_not_reached ("attempt to decode probe location");
3079 gdb_assert_not_reached ("unhandled location spec type");
3085 /* See linespec.h. */
3088 decode_line_full (struct location_spec
*locspec
, int flags
,
3089 struct program_space
*search_pspace
,
3090 struct symtab
*default_symtab
,
3091 int default_line
, struct linespec_result
*canonical
,
3092 const char *select_mode
,
3095 std::vector
<const char *> filters
;
3097 gdb_assert (canonical
!= NULL
);
3098 /* The filter only makes sense for 'all'. */
3099 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
3100 gdb_assert (select_mode
== NULL
3101 || select_mode
== multiple_symbols_all
3102 || select_mode
== multiple_symbols_ask
3103 || select_mode
== multiple_symbols_cancel
);
3104 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
3106 linespec_parser
parser (flags
, current_language
,
3107 search_pspace
, default_symtab
,
3108 default_line
, canonical
);
3110 scoped_restore_current_program_space restore_pspace
;
3112 std::vector
<symtab_and_line
> result
= location_spec_to_sals (&parser
,
3114 linespec_state
*state
= &parser
.state
;
3116 if (result
.size () == 0)
3117 throw_error (NOT_SUPPORTED_ERROR
, _("Location %s not available"),
3118 locspec
->to_string ());
3120 gdb_assert (result
.size () == 1 || canonical
->pre_expanded
);
3121 canonical
->pre_expanded
= 1;
3123 if (select_mode
== NULL
)
3125 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
3126 select_mode
= multiple_symbols_all
;
3128 select_mode
= multiple_symbols_select_mode ();
3131 if (select_mode
== multiple_symbols_all
)
3135 filters
.push_back (filter
);
3136 filter_results (state
, &result
, filters
);
3139 convert_results_to_lsals (state
, &result
);
3142 decode_line_2 (state
, &result
, select_mode
);
3145 /* See linespec.h. */
3147 std::vector
<symtab_and_line
>
3148 decode_line_1 (const location_spec
*locspec
, int flags
,
3149 struct program_space
*search_pspace
,
3150 struct symtab
*default_symtab
,
3153 linespec_parser
parser (flags
, current_language
,
3154 search_pspace
, default_symtab
,
3155 default_line
, NULL
);
3157 scoped_restore_current_program_space restore_pspace
;
3159 return location_spec_to_sals (&parser
, locspec
);
3162 /* See linespec.h. */
3164 std::vector
<symtab_and_line
>
3165 decode_line_with_current_source (const char *string
, int flags
)
3168 error (_("Empty line specification."));
3170 /* We use whatever is set as the current source line. We do not try
3171 and get a default source symtab+line or it will recursively call us! */
3172 symtab_and_line cursal
3173 = get_current_source_symtab_and_line (current_program_space
);
3175 location_spec_up locspec
= string_to_location_spec (&string
,
3177 std::vector
<symtab_and_line
> sals
3178 = decode_line_1 (locspec
.get (), flags
, cursal
.pspace
, cursal
.symtab
,
3182 error (_("Junk at end of line specification: %s"), string
);
3187 /* See linespec.h. */
3189 std::vector
<symtab_and_line
>
3190 decode_line_with_last_displayed (const char *string
, int flags
)
3193 error (_("Empty line specification."));
3195 location_spec_up locspec
= string_to_location_spec (&string
,
3197 std::vector
<symtab_and_line
> sals
3198 = (last_displayed_sal_is_valid ()
3199 ? decode_line_1 (locspec
.get (), flags
, NULL
,
3200 get_last_displayed_symtab (),
3201 get_last_displayed_line ())
3202 : decode_line_1 (locspec
.get (), flags
, NULL
, NULL
, 0));
3205 error (_("Junk at end of line specification: %s"), string
);
3212 /* First, some functions to initialize stuff at the beginning of the
3216 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
3218 if (*default_symtab
== 0)
3220 /* Use whatever we have for the default source line. We don't use
3221 get_current_or_default_symtab_and_line as it can recurse and call
3223 symtab_and_line cursal
3224 = get_current_source_symtab_and_line (current_program_space
);
3226 *default_symtab
= cursal
.symtab
;
3227 *default_line
= cursal
.line
;
3233 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3234 advancing EXP_PTR past any parsed text. */
3237 linespec_expression_to_pc (const char **exp_ptr
)
3239 if (current_program_space
->executing_startup
)
3240 /* The error message doesn't really matter, because this case
3241 should only hit during breakpoint reset. */
3242 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
3243 "program space is in startup"));
3246 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
3251 /* Here's where we recognise an Objective-C Selector. An Objective C
3252 selector may be implemented by more than one class, therefore it
3253 may represent more than one method/function. This gives us a
3254 situation somewhat analogous to C++ overloading. If there's more
3255 than one method that could represent the selector, then use some of
3256 the existing C++ code to let the user choose one. */
3258 static std::vector
<symtab_and_line
>
3259 decode_objc (struct linespec_state
*self
, linespec
*ls
, const char *arg
)
3261 struct collect_info info
;
3262 std::vector
<const char *> symbol_names
;
3263 const char *new_argptr
;
3266 std::vector
<symtab
*> symtabs
;
3267 symtabs
.push_back (nullptr);
3269 info
.file_symtabs
= &symtabs
;
3271 std::vector
<block_symbol
> symbols
;
3272 info
.result
.symbols
= &symbols
;
3273 std::vector
<bound_minimal_symbol
> minimal_symbols
;
3274 info
.result
.minimal_symbols
= &minimal_symbols
;
3276 new_argptr
= find_imps (arg
, &symbol_names
);
3277 if (symbol_names
.empty ())
3280 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
,
3281 SEARCH_FUNCTION_DOMAIN
);
3283 std::vector
<symtab_and_line
> values
;
3284 if (!symbols
.empty () || !minimal_symbols
.empty ())
3288 saved_arg
= (char *) alloca (new_argptr
- arg
+ 1);
3289 memcpy (saved_arg
, arg
, new_argptr
- arg
);
3290 saved_arg
[new_argptr
- arg
] = '\0';
3292 ls
->explicit_loc
.function_name
= make_unique_xstrdup (saved_arg
);
3293 ls
->function_symbols
= std::move (symbols
);
3294 ls
->minimal_symbols
= std::move (minimal_symbols
);
3295 values
= convert_linespec_to_sals (self
, ls
);
3297 if (self
->canonical
)
3302 self
->canonical
->pre_expanded
= 1;
3304 if (ls
->explicit_loc
.source_filename
)
3306 holder
= string_printf ("%s:%s",
3307 ls
->explicit_loc
.source_filename
.get (),
3309 str
= holder
.c_str ();
3314 self
->canonical
->locspec
3315 = new_linespec_location_spec (&str
, symbol_name_match_type::FULL
);
3324 /* A function object that serves as symbol_found_callback_ftype
3325 callback for iterate_over_symbols. This is used by
3326 lookup_prefix_sym to collect type symbols. */
3327 class decode_compound_collector
3330 decode_compound_collector () = default;
3332 DISABLE_COPY_AND_ASSIGN (decode_compound_collector
);
3334 /* Return all symbols collected. */
3335 std::vector
<block_symbol
> release_symbols ()
3337 return std::move (m_symbols
);
3340 /* Callable as a symbol_found_callback_ftype callback. */
3341 bool operator () (block_symbol
*bsym
);
3344 /* A hash table of all symbols we found. We use this to avoid
3345 adding any symbol more than once. */
3346 gdb::unordered_set
<const symbol
*> m_unique_syms
;
3348 /* The result vector. */
3349 std::vector
<block_symbol
> m_symbols
;
3353 decode_compound_collector::operator () (block_symbol
*bsym
)
3356 struct symbol
*sym
= bsym
->symbol
;
3358 if (sym
->aclass () != LOC_TYPEDEF
)
3359 return true; /* Continue iterating. */
3362 t
= check_typedef (t
);
3363 if (t
->code () != TYPE_CODE_STRUCT
3364 && t
->code () != TYPE_CODE_UNION
3365 && t
->code () != TYPE_CODE_NAMESPACE
)
3366 return true; /* Continue iterating. */
3368 if (m_unique_syms
.insert (sym
).second
)
3369 m_symbols
.push_back (*bsym
);
3371 return true; /* Continue iterating. */
3376 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3378 static std::vector
<block_symbol
>
3379 lookup_prefix_sym (struct linespec_state
*state
,
3380 const std::vector
<symtab
*> &file_symtabs
,
3381 const char *class_name
)
3383 decode_compound_collector collector
;
3385 lookup_name_info
lookup_name (class_name
, symbol_name_match_type::FULL
);
3387 for (const auto &elt
: file_symtabs
)
3390 iterate_over_all_matching_symtabs (state
, lookup_name
,
3391 SEARCH_STRUCT_DOMAIN
| SEARCH_VFT
,
3392 NULL
, false, collector
);
3395 /* Program spaces that are executing startup should have
3396 been filtered out earlier. */
3397 program_space
*pspace
= elt
->compunit ()->objfile ()->pspace ();
3399 gdb_assert (!pspace
->executing_startup
);
3400 set_current_program_space (pspace
);
3401 iterate_over_file_blocks (elt
, lookup_name
,
3402 SEARCH_STRUCT_DOMAIN
| SEARCH_VFT
,
3407 return collector
.release_symbols ();
3410 /* A std::sort comparison function for symbols. The resulting order does
3411 not actually matter; we just need to be able to sort them so that
3412 symbols with the same program space end up next to each other. */
3415 compare_symbols (const block_symbol
&a
, const block_symbol
&b
)
3419 uia
= (uintptr_t) a
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
3420 uib
= (uintptr_t) b
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
3427 uia
= (uintptr_t) a
.symbol
;
3428 uib
= (uintptr_t) b
.symbol
;
3436 /* Like compare_symbols but for minimal symbols. */
3439 compare_msymbols (const bound_minimal_symbol
&a
, const bound_minimal_symbol
&b
)
3443 uia
= (uintptr_t) a
.objfile
->pspace ();
3444 uib
= (uintptr_t) a
.objfile
->pspace ();
3451 uia
= (uintptr_t) a
.minsym
;
3452 uib
= (uintptr_t) b
.minsym
;
3460 /* Look for all the matching instances of each symbol in NAMES. Only
3461 instances from PSPACE are considered; other program spaces are
3462 handled by our caller. If PSPACE is NULL, then all program spaces
3463 are considered. Results are stored into INFO. */
3466 add_all_symbol_names_from_pspace (struct collect_info
*info
,
3467 struct program_space
*pspace
,
3468 const std::vector
<const char *> &names
,
3469 domain_search_flags domain_search_flags
)
3471 for (const char *iter
: names
)
3472 add_matching_symbols_to_info (iter
,
3473 symbol_name_match_type::FULL
,
3474 domain_search_flags
, info
, pspace
);
3478 find_superclass_methods (std::vector
<struct type
*> &&superclasses
,
3479 const char *name
, enum language name_lang
,
3480 std::vector
<const char *> *result_names
)
3482 size_t old_len
= result_names
->size ();
3486 std::vector
<struct type
*> new_supers
;
3488 for (type
*t
: superclasses
)
3489 find_methods (t
, name_lang
, name
, result_names
, &new_supers
);
3491 if (result_names
->size () != old_len
|| new_supers
.empty ())
3494 superclasses
= std::move (new_supers
);
3498 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3499 given by one of the symbols in SYM_CLASSES. Matches are returned
3500 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3503 find_method (struct linespec_state
*self
,
3504 const std::vector
<symtab
*> &file_symtabs
,
3505 const char *class_name
, const char *method_name
,
3506 std::vector
<block_symbol
> *sym_classes
,
3507 std::vector
<block_symbol
> *symbols
,
3508 std::vector
<bound_minimal_symbol
> *minsyms
)
3510 size_t last_result_len
;
3511 std::vector
<struct type
*> superclass_vec
;
3512 std::vector
<const char *> result_names
;
3513 struct collect_info info
;
3515 /* Sort symbols so that symbols with the same program space are next
3517 std::sort (sym_classes
->begin (), sym_classes
->end (),
3521 info
.file_symtabs
= &file_symtabs
;
3522 info
.result
.symbols
= symbols
;
3523 info
.result
.minimal_symbols
= minsyms
;
3525 /* Iterate over all the types, looking for the names of existing
3526 methods matching METHOD_NAME. If we cannot find a direct method in a
3527 given program space, then we consider inherited methods; this is
3528 not ideal (ideal would be to respect C++ hiding rules), but it
3529 seems good enough and is what GDB has historically done. We only
3530 need to collect the names because later we find all symbols with
3531 those names. This loop is written in a somewhat funny way
3532 because we collect data across the program space before deciding
3534 last_result_len
= 0;
3535 for (const auto &elt
: *sym_classes
)
3538 struct program_space
*pspace
;
3539 struct symbol
*sym
= elt
.symbol
;
3540 unsigned int ix
= &elt
- &*sym_classes
->begin ();
3542 /* Program spaces that are executing startup should have
3543 been filtered out earlier. */
3544 pspace
= sym
->symtab ()->compunit ()->objfile ()->pspace ();
3545 gdb_assert (!pspace
->executing_startup
);
3546 set_current_program_space (pspace
);
3547 t
= check_typedef (sym
->type ());
3548 find_methods (t
, sym
->language (),
3549 method_name
, &result_names
, &superclass_vec
);
3551 /* Handle all items from a single program space at once; and be
3552 sure not to miss the last batch. */
3553 if (ix
== sym_classes
->size () - 1
3555 != (sym_classes
->at (ix
+ 1).symbol
->symtab ()
3556 ->compunit ()->objfile ()->pspace ())))
3558 /* If we did not find a direct implementation anywhere in
3559 this program space, consider superclasses. */
3560 if (result_names
.size () == last_result_len
)
3561 find_superclass_methods (std::move (superclass_vec
), method_name
,
3562 sym
->language (), &result_names
);
3564 /* We have a list of candidate symbol names, so now we
3565 iterate over the symbol tables looking for all
3566 matches in this pspace. */
3567 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
,
3568 SEARCH_FUNCTION_DOMAIN
);
3570 superclass_vec
.clear ();
3571 last_result_len
= result_names
.size ();
3575 if (!symbols
->empty () || !minsyms
->empty ())
3578 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3579 and other attempts to locate the symbol will be made. */
3580 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
3587 /* This function object is a callback for iterate_over_symtabs, used
3588 when collecting all matching symtabs. */
3590 class symtab_collector
3593 symtab_collector () = default;
3595 DISABLE_COPY_AND_ASSIGN (symtab_collector
);
3597 /* Callable as a symbol_found_callback_ftype callback. */
3598 bool operator () (struct symtab
*symtab
)
3600 if (m_symtab_table
.insert (symtab
).second
)
3601 m_symtabs
.push_back (symtab
);
3606 /* Return an rvalue reference to the collected symtabs. */
3607 std::vector
<symtab
*> &&release_symtabs ()
3609 return std::move (m_symtabs
);
3613 /* The result vector of symtabs. */
3614 std::vector
<symtab
*> m_symtabs
;
3616 /* This is used to ensure the symtabs are unique. */
3617 gdb::unordered_set
<const symtab
*> m_symtab_table
;
3622 /* Given a file name, return a list of all matching symtabs. If
3623 SEARCH_PSPACE is not NULL, the search is restricted to just that
3626 static std::vector
<symtab
*>
3627 collect_symtabs_from_filename (const char *file
,
3628 struct program_space
*search_pspace
)
3630 symtab_collector collector
;
3632 /* Find that file's data. */
3633 if (search_pspace
== NULL
)
3635 for (struct program_space
*pspace
: program_spaces
)
3637 if (pspace
->executing_startup
)
3640 iterate_over_symtabs (pspace
, file
, collector
);
3644 iterate_over_symtabs (search_pspace
, file
, collector
);
3646 return collector
.release_symtabs ();
3649 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3650 not NULL, the search is restricted to just that program space. */
3652 static std::vector
<symtab
*>
3653 symtabs_from_filename (const char *filename
,
3654 struct program_space
*search_pspace
)
3656 std::vector
<symtab
*> result
3657 = collect_symtabs_from_filename (filename
, search_pspace
);
3659 if (result
.empty ())
3661 if (!have_full_symbols (current_program_space
)
3662 && !have_partial_symbols (current_program_space
))
3663 throw_error (NOT_FOUND_ERROR
,
3664 _("No symbol table is loaded. "
3665 "Use the \"file\" command."));
3666 source_file_not_found_error (filename
);
3675 symbol_searcher::find_all_symbols (const std::string
&name
,
3676 const struct language_defn
*language
,
3677 domain_search_flags domain_search_flags
,
3678 std::vector
<symtab
*> *search_symtabs
,
3679 struct program_space
*search_pspace
)
3681 symbol_searcher_collect_info info
;
3682 linespec_state
state (language
, current_program_space
);
3684 info
.state
= &state
;
3686 info
.result
.symbols
= &m_symbols
;
3687 info
.result
.minimal_symbols
= &m_minimal_symbols
;
3688 std::vector
<symtab
*> all_symtabs
;
3689 if (search_symtabs
== nullptr)
3691 all_symtabs
.push_back (nullptr);
3692 search_symtabs
= &all_symtabs
;
3694 info
.file_symtabs
= search_symtabs
;
3696 add_matching_symbols_to_info (name
.c_str (), symbol_name_match_type::WILD
,
3697 domain_search_flags
, &info
, search_pspace
);
3700 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3701 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3702 returned in MINSYMS. */
3705 find_function_symbols (struct linespec_state
*state
,
3706 const std::vector
<symtab
*> &file_symtabs
, const char *name
,
3707 symbol_name_match_type name_match_type
,
3708 std::vector
<block_symbol
> *symbols
,
3709 std::vector
<bound_minimal_symbol
> *minsyms
)
3711 struct collect_info info
;
3712 std::vector
<const char *> symbol_names
;
3715 info
.result
.symbols
= symbols
;
3716 info
.result
.minimal_symbols
= minsyms
;
3717 info
.file_symtabs
= &file_symtabs
;
3719 /* Try NAME as an Objective-C selector. */
3720 find_imps (name
, &symbol_names
);
3722 domain_search_flags flags
= SEARCH_FUNCTION_DOMAIN
;
3723 if (state
->list_mode
)
3726 if (!symbol_names
.empty ())
3727 add_all_symbol_names_from_pspace (&info
, state
->search_pspace
,
3728 symbol_names
, flags
);
3730 add_matching_symbols_to_info (name
, name_match_type
, flags
,
3731 &info
, state
->search_pspace
);
3734 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3735 in SYMBOLS and minimal symbols in MINSYMS. */
3738 find_linespec_symbols (struct linespec_state
*state
,
3739 const std::vector
<symtab
*> &file_symtabs
,
3740 const char *lookup_name
,
3741 symbol_name_match_type name_match_type
,
3742 std::vector
<block_symbol
> *symbols
,
3743 std::vector
<bound_minimal_symbol
> *minsyms
)
3745 gdb::unique_xmalloc_ptr
<char> canon
3746 = cp_canonicalize_string_no_typedefs (lookup_name
);
3747 if (canon
!= nullptr)
3748 lookup_name
= canon
.get ();
3750 /* It's important to not call expand_symtabs_matching unnecessarily
3751 as it can really slow things down (by unnecessarily expanding
3752 potentially 1000s of symtabs, which when debugging some apps can
3753 cost 100s of seconds). Avoid this to some extent by *first* calling
3754 find_function_symbols, and only if that doesn't find anything
3755 *then* call find_method. This handles two important cases:
3756 1) break (anonymous namespace)::foo
3757 2) break class::method where method is in class (and not a baseclass) */
3759 find_function_symbols (state
, file_symtabs
, lookup_name
,
3760 name_match_type
, symbols
, minsyms
);
3762 /* If we were unable to locate a symbol of the same name, try dividing
3763 the name into class and method names and searching the class and its
3765 if (symbols
->empty () && minsyms
->empty ())
3767 std::string klass
, method
;
3768 const char *last
, *p
, *scope_op
;
3770 /* See if we can find a scope operator and break this symbol
3771 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3773 p
= find_toplevel_string (lookup_name
, scope_op
);
3779 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
3782 /* If no scope operator was found, there is nothing more we can do;
3783 we already attempted to lookup the entire name as a symbol
3788 /* LOOKUP_NAME points to the class name.
3789 LAST points to the method name. */
3790 klass
= std::string (lookup_name
, last
- lookup_name
);
3792 /* Skip past the scope operator. */
3793 last
+= strlen (scope_op
);
3796 /* Find a list of classes named KLASS. */
3797 std::vector
<block_symbol
> classes
3798 = lookup_prefix_sym (state
, file_symtabs
, klass
.c_str ());
3799 if (!classes
.empty ())
3801 /* Now locate a list of suitable methods named METHOD. */
3804 find_method (state
, file_symtabs
,
3805 klass
.c_str (), method
.c_str (),
3806 &classes
, symbols
, minsyms
);
3809 /* If successful, we're done. If NOT_FOUND_ERROR
3810 was not thrown, rethrow the exception that we did get. */
3811 catch (const gdb_exception_error
&except
)
3813 if (except
.error
!= NOT_FOUND_ERROR
)
3820 /* Helper for find_label_symbols. Find all labels that match name
3821 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3822 Return the actual function symbol in which the label was found in
3823 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3824 interpreted as a label name prefix. Otherwise, only a label named
3825 exactly NAME match. */
3828 find_label_symbols_in_block (const struct block
*block
,
3829 const char *name
, struct symbol
*fn_sym
,
3830 bool completion_mode
,
3831 std::vector
<block_symbol
> *result
,
3832 std::vector
<block_symbol
> *label_funcs_ret
)
3834 if (completion_mode
)
3836 size_t name_len
= strlen (name
);
3838 int (*cmp
) (const char *, const char *, size_t);
3839 cmp
= case_sensitivity
== case_sensitive_on
? strncmp
: strncasecmp
;
3841 for (struct symbol
*sym
: block_iterator_range (block
))
3843 if (sym
->domain () == LABEL_DOMAIN
3844 && cmp (sym
->search_name (), name
, name_len
) == 0)
3846 result
->push_back ({sym
, block
});
3847 label_funcs_ret
->push_back ({fn_sym
, block
});
3853 struct block_symbol label_sym
3854 = lookup_symbol (name
, block
, SEARCH_LABEL_DOMAIN
, 0);
3856 if (label_sym
.symbol
!= NULL
)
3858 result
->push_back (label_sym
);
3859 label_funcs_ret
->push_back ({fn_sym
, block
});
3864 /* Return all labels that match name NAME in FUNCTION_SYMBOLS.
3866 Return the actual function symbol in which the label was found in
3867 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3868 interpreted as a label name prefix. Otherwise, only labels named
3869 exactly NAME match. */
3872 static std::vector
<block_symbol
>
3873 find_label_symbols (struct linespec_state
*self
,
3874 const std::vector
<block_symbol
> &function_symbols
,
3875 std::vector
<block_symbol
> *label_funcs_ret
,
3877 bool completion_mode
)
3879 const struct block
*block
;
3880 struct symbol
*fn_sym
;
3881 std::vector
<block_symbol
> result
;
3883 if (function_symbols
.empty ())
3885 set_current_program_space (self
->program_space
);
3886 block
= get_current_search_block ();
3889 block
&& !block
->function ();
3890 block
= block
->superblock ())
3896 fn_sym
= block
->function ();
3898 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
3899 &result
, label_funcs_ret
);
3903 for (const auto &elt
: function_symbols
)
3905 fn_sym
= elt
.symbol
;
3906 set_current_program_space
3907 (fn_sym
->symtab ()->compunit ()->objfile ()->pspace ());
3908 block
= fn_sym
->value_block ();
3910 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
3911 &result
, label_funcs_ret
);
3920 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3922 static std::vector
<symtab_and_line
>
3923 decode_digits_list_mode (struct linespec_state
*self
,
3925 struct symtab_and_line val
)
3927 gdb_assert (self
->list_mode
);
3929 std::vector
<symtab_and_line
> values
;
3931 for (const auto &elt
: ls
->file_symtabs
)
3933 /* The logic above should ensure this. */
3934 gdb_assert (elt
!= NULL
);
3936 program_space
*pspace
= elt
->compunit ()->objfile ()->pspace ();
3937 set_current_program_space (pspace
);
3939 /* Simplistic search just for the list command. */
3940 val
.symtab
= find_line_symtab (elt
, val
.line
, nullptr);
3941 if (val
.symtab
== NULL
)
3943 val
.pspace
= pspace
;
3945 val
.explicit_line
= true;
3947 add_sal_to_sals (self
, &values
, &val
, NULL
, false);
3953 /* A helper for create_sals_line_offset that iterates over the symtabs
3954 associated with LS and returns a vector of corresponding symtab_and_line
3957 static std::vector
<symtab_and_line
>
3958 decode_digits_ordinary (struct linespec_state
*self
,
3961 const linetable_entry
**best_entry
)
3963 std::vector
<symtab_and_line
> sals
;
3964 for (const auto &elt
: ls
->file_symtabs
)
3966 std::vector
<CORE_ADDR
> pcs
;
3968 /* The logic above should ensure this. */
3969 gdb_assert (elt
!= NULL
);
3971 program_space
*pspace
= elt
->compunit ()->objfile ()->pspace ();
3972 set_current_program_space (pspace
);
3974 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
3975 for (CORE_ADDR pc
: pcs
)
3977 symtab_and_line sal
;
3978 sal
.pspace
= pspace
;
3981 sal
.explicit_line
= true;
3983 sals
.push_back (std::move (sal
));
3992 /* Return the line offset represented by VARIABLE. */
3994 static struct line_offset
3995 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
4001 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
4004 while (*p
>= '0' && *p
<= '9')
4006 if (!*p
) /* Reached end of token without hitting non-digit. */
4008 /* We have a value history reference. */
4009 struct value
*val_history
;
4011 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
4013 = access_value_history ((variable
[1] == '$') ? -index
: index
);
4014 if (val_history
->type ()->code () != TYPE_CODE_INT
)
4015 error (_("History values used in line "
4016 "specs must have integer values."));
4017 offset
.offset
= value_as_long (val_history
);
4018 offset
.sign
= LINE_OFFSET_NONE
;
4022 /* Not all digits -- may be user variable/function or a
4023 convenience variable. */
4025 struct internalvar
*ivar
;
4027 /* Try it as a convenience variable. If it is not a convenience
4028 variable, return and allow normal symbol lookup to occur. */
4029 ivar
= lookup_only_internalvar (variable
+ 1);
4030 /* If there's no internal variable with that name, let the
4031 offset remain as unknown to allow the name to be looked up
4033 if (ivar
!= nullptr)
4035 /* We found a valid variable name. If it is not an integer,
4037 if (!get_internalvar_integer (ivar
, &valx
))
4038 error (_("Convenience variables used in line "
4039 "specs must have integer values."));
4042 offset
.offset
= valx
;
4043 offset
.sign
= LINE_OFFSET_NONE
;
4052 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
4053 linespec; return the SAL in RESULT. This function should return SALs
4054 matching those from find_function_start_sal, otherwise false
4055 multiple-locations breakpoints could be placed. */
4058 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
4059 struct minimal_symbol
*msymbol
,
4060 std::vector
<symtab_and_line
> *result
)
4062 bool want_start_sal
= false;
4064 CORE_ADDR func_addr
;
4065 bool is_function
= msymbol_is_function (objfile
, msymbol
, &func_addr
);
4069 const char *msym_name
= msymbol
->linkage_name ();
4071 if (msymbol
->type () == mst_text_gnu_ifunc
4072 || msymbol
->type () == mst_data_gnu_ifunc
)
4073 want_start_sal
= gnu_ifunc_resolve_name (msym_name
, &func_addr
);
4075 want_start_sal
= true;
4078 symtab_and_line sal
;
4080 if (is_function
&& want_start_sal
)
4081 sal
= find_function_start_sal (func_addr
, NULL
, self
->funfirstline
);
4084 sal
.objfile
= objfile
;
4085 sal
.msymbol
= msymbol
;
4086 /* Store func_addr, not the minsym's address in case this was an
4087 ifunc that hasn't been resolved yet. */
4091 sal
.pc
= msymbol
->value_address (objfile
);
4092 sal
.pspace
= current_program_space
;
4095 sal
.section
= msymbol
->obj_section (objfile
);
4097 if (self
->maybe_add_address (objfile
->pspace (), sal
.pc
))
4098 add_sal_to_sals (self
, result
, &sal
, msymbol
->natural_name (), false);
4101 /* Helper for search_minsyms_for_name that adds the symbol to the
4105 add_minsym (struct minimal_symbol
*minsym
, struct objfile
*objfile
,
4106 struct symtab
*symtab
, bool list_mode
,
4107 std::vector
<bound_minimal_symbol
> *msyms
)
4111 /* We're looking for a label for which we don't have debug
4113 CORE_ADDR func_addr
;
4114 if (msymbol_is_function (objfile
, minsym
, &func_addr
))
4116 symtab_and_line sal
= find_pc_sect_line (func_addr
, NULL
, 0);
4118 if (symtab
!= sal
.symtab
)
4123 /* Exclude data symbols when looking for breakpoint locations. */
4124 if (list_mode
|| msymbol_is_function (objfile
, minsym
))
4125 msyms
->emplace_back (minsym
, objfile
);
4128 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4129 is not NULL, the search is restricted to just that program
4132 If SYMTAB is NULL, search all objfiles, otherwise
4133 restrict results to the given SYMTAB. */
4136 search_minsyms_for_name (struct collect_info
*info
,
4137 const lookup_name_info
&name
,
4138 struct program_space
*search_pspace
,
4139 struct symtab
*symtab
)
4141 std::vector
<bound_minimal_symbol
> minsyms
;
4145 for (struct program_space
*pspace
: program_spaces
)
4147 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
4149 if (pspace
->executing_startup
)
4152 set_current_program_space (pspace
);
4154 for (objfile
*objfile
: pspace
->objfiles ())
4156 iterate_over_minimal_symbols (objfile
, name
,
4157 [&] (struct minimal_symbol
*msym
)
4159 add_minsym (msym
, objfile
, nullptr,
4160 info
->state
->list_mode
,
4169 program_space
*pspace
= symtab
->compunit ()->objfile ()->pspace ();
4171 if (search_pspace
== NULL
|| pspace
== search_pspace
)
4173 set_current_program_space (pspace
);
4174 iterate_over_minimal_symbols
4175 (symtab
->compunit ()->objfile (), name
,
4176 [&] (struct minimal_symbol
*msym
)
4178 add_minsym (msym
, symtab
->compunit ()->objfile (), symtab
,
4179 info
->state
->list_mode
, &minsyms
);
4185 /* Return true if TYPE is a static symbol. */
4186 auto msymbol_type_is_static
= [] (enum minimal_symbol_type type
)
4199 /* Add minsyms to the result set, but filter out trampoline symbols
4200 if we also found extern symbols with the same name. I.e., don't
4201 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4202 'foo' is the symbol that the plt resolves to. */
4203 for (const bound_minimal_symbol
&item
: minsyms
)
4206 if (item
.minsym
->type () == mst_solib_trampoline
)
4208 for (const bound_minimal_symbol
&item2
: minsyms
)
4210 if (&item2
== &item
)
4213 /* Ignore other trampoline symbols. */
4214 if (item2
.minsym
->type () == mst_solib_trampoline
)
4217 /* Trampoline symbols can only jump to exported
4219 if (msymbol_type_is_static (item2
.minsym
->type ()))
4222 if (strcmp (item
.minsym
->linkage_name (),
4223 item2
.minsym
->linkage_name ()) != 0)
4226 /* Found a global minsym with the same name as the
4227 trampoline. Don't create a location for this
4235 info
->result
.minimal_symbols
->push_back (item
);
4239 /* A helper function to add all symbols matching NAME to INFO. If
4240 PSPACE is not NULL, the search is restricted to just that program
4244 add_matching_symbols_to_info (const char *name
,
4245 symbol_name_match_type name_match_type
,
4246 domain_search_flags domain_search_flags
,
4247 struct collect_info
*info
,
4248 struct program_space
*pspace
)
4250 lookup_name_info
lookup_name (name
, name_match_type
);
4252 auto add_symbol
= [&] (block_symbol
*bsym
)
4254 return info
->add_symbol (bsym
);
4257 for (const auto &elt
: *info
->file_symtabs
)
4261 iterate_over_all_matching_symtabs (info
->state
, lookup_name
,
4262 domain_search_flags
,
4265 search_minsyms_for_name (info
, lookup_name
, pspace
, NULL
);
4267 else if (pspace
== NULL
|| pspace
== elt
->compunit ()->objfile ()->pspace ())
4269 int prev_len
= info
->result
.symbols
->size ();
4271 /* Program spaces that are executing startup should have
4272 been filtered out earlier. */
4273 program_space
*elt_pspace
= elt
->compunit ()->objfile ()->pspace ();
4274 gdb_assert (!elt_pspace
->executing_startup
);
4275 set_current_program_space (elt_pspace
);
4276 iterate_over_file_blocks (elt
, lookup_name
, SEARCH_VFT
, add_symbol
);
4278 /* If no new symbols were found in this iteration and this symtab
4279 is in assembler, we might actually be looking for a label for
4280 which we don't have debug info. Check for a minimal symbol in
4282 if (prev_len
== info
->result
.symbols
->size ()
4283 && elt
->language () == language_asm
)
4284 search_minsyms_for_name (info
, lookup_name
, pspace
, elt
);
4291 /* Now come some functions that are called from multiple places within
4295 symbol_to_sal (struct symtab_and_line
*result
,
4296 bool funfirstline
, struct symbol
*sym
)
4298 if (sym
->aclass () == LOC_BLOCK
)
4300 *result
= find_function_start_sal (sym
, funfirstline
);
4305 if (sym
->aclass () == LOC_LABEL
&& sym
->value_address () != 0)
4308 result
->symtab
= sym
->symtab ();
4309 result
->symbol
= sym
;
4310 result
->line
= sym
->line ();
4311 result
->pc
= sym
->value_address ();
4312 result
->pspace
= result
->symtab
->compunit ()->objfile ()->pspace ();
4313 result
->explicit_pc
= 1;
4316 else if (funfirstline
)
4320 else if (sym
->line () != 0)
4322 /* We know its line number. */
4324 result
->symtab
= sym
->symtab ();
4325 result
->symbol
= sym
;
4326 result
->line
= sym
->line ();
4327 result
->pc
= sym
->value_address ();
4328 result
->pspace
= result
->symtab
->compunit ()->objfile ()->pspace ();
4336 linespec_result::~linespec_result ()
4338 for (linespec_sals
&lsal
: lsals
)
4339 xfree (lsal
.canonical
);
4342 /* Return the quote characters permitted by the linespec parser. */
4345 get_gdb_linespec_parser_quote_characters (void)
4347 return linespec_quote_characters
;