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/>. */
28 #include "completer.h"
30 #include "cp-support.h"
31 #include "parser-defs.h"
33 #include "objc-lang.h"
37 #include "mi/mi-cmds.h"
39 #include "arch-utils.h"
41 #include "cli/cli-utils.h"
42 #include "filenames.h"
46 #include "gdbsupport/function-view.h"
47 #include "gdbsupport/def-vector.h"
51 /* An enumeration of the various things a user might attempt to
52 complete for a linespec location. */
54 enum class linespec_complete_what
56 /* Nothing, no possible completion. */
59 /* A function/method name. Due to ambiguity between
65 this can also indicate a source filename, iff we haven't seen a
66 separate source filename component, as in "b source.c:function". */
69 /* A label symbol. E.g., break file.c:function:LABEL. */
72 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
75 /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
76 E.g., "break func threa<tab>". */
80 /* An address entry is used to ensure that any given location is only
81 added to the result a single time. It holds an address and the
82 program space from which the address came. */
86 struct program_space
*pspace
;
90 /* A linespec. Elements of this structure are filled in by a parser
91 (either parse_linespec or some other function). The structure is
92 then converted into SALs by convert_linespec_to_sals. */
96 /* An explicit location spec describing the SaLs. */
97 explicit_location_spec explicit_loc
;
99 /* The list of symtabs to search to which to limit the search.
101 If explicit.SOURCE_FILENAME is NULL (no user-specified filename),
102 FILE_SYMTABS should contain one single NULL member. This will cause the
103 code to use the default symtab. */
104 std::vector
<symtab
*> file_symtabs
;
106 /* A list of matching function symbols and minimal symbols. Both lists
107 may be empty if no matching symbols were found. */
108 std::vector
<block_symbol
> function_symbols
;
109 std::vector
<bound_minimal_symbol
> minimal_symbols
;
111 /* A structure of matching label symbols and the corresponding
112 function symbol in which the label was found. Both may be empty
113 or both must be non-empty. */
116 std::vector
<block_symbol
> label_symbols
;
117 std::vector
<block_symbol
> function_symbols
;
121 /* A canonical linespec represented as a symtab-related string.
123 Each entry represents the "SYMTAB:SUFFIX" linespec string.
124 SYMTAB can be converted for example by symtab_to_fullname or
125 symtab_to_filename_for_display as needed. */
127 struct linespec_canonical_name
129 /* Remaining text part of the linespec string. */
132 /* If NULL then SUFFIX is the whole linespec string. */
133 struct symtab
*symtab
;
136 /* An instance of this is used to keep all state while linespec
137 operates. This instance is passed around as a 'this' pointer to
138 the various implementation methods. */
140 struct linespec_state
142 /* The language in use during linespec processing. */
143 const struct language_defn
*language
;
145 /* The program space as seen when the module was entered. */
146 struct program_space
*program_space
;
148 /* If not NULL, the search is restricted to just this program
150 struct program_space
*search_pspace
;
152 /* The default symtab to use, if no other symtab is specified. */
153 struct symtab
*default_symtab
;
155 /* The default line to use. */
158 /* The 'funfirstline' value that was passed in to decode_line_1 or
162 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
165 /* The 'canonical' value passed to decode_line_full, or NULL. */
166 struct linespec_result
*canonical
;
168 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
169 struct linespec_canonical_name
*canonical_names
;
171 /* This is a set of address_entry objects which is used to prevent
172 duplicate symbols from being entered into the result. */
175 /* Are we building a linespec? */
179 /* This is a helper object that is used when collecting symbols into a
184 /* The linespec object in use. */
185 struct linespec_state
*state
;
187 /* A list of symtabs to which to restrict matches. */
188 const std::vector
<symtab
*> *file_symtabs
;
190 /* The result being accumulated. */
193 std::vector
<block_symbol
> *symbols
;
194 std::vector
<bound_minimal_symbol
> *minimal_symbols
;
197 /* Possibly add a symbol to the results. */
198 virtual bool add_symbol (block_symbol
*bsym
);
202 collect_info::add_symbol (block_symbol
*bsym
)
204 /* In list mode, add all matching symbols, regardless of class.
205 This allows the user to type "list a_global_variable". */
206 if (bsym
->symbol
->aclass () == LOC_BLOCK
|| this->state
->list_mode
)
207 this->result
.symbols
->push_back (*bsym
);
209 /* Continue iterating. */
213 /* Custom collect_info for symbol_searcher. */
215 struct symbol_searcher_collect_info
218 bool add_symbol (block_symbol
*bsym
) override
220 /* Add everything. */
221 this->result
.symbols
->push_back (*bsym
);
223 /* Continue iterating. */
230 enum linespec_token_type
235 /* A colon "separator" */
247 /* EOI (end of input) */
254 /* List of keywords. This is NULL-terminated so that it can be used
255 as enum completer. */
256 const char * const linespec_keywords
[] = { "if", "thread", "task", "inferior", "-force-condition", NULL
};
257 #define IF_KEYWORD_INDEX 0
258 #define FORCE_KEYWORD_INDEX 4
260 /* A token of the linespec lexer */
262 struct linespec_token
264 /* The type of the token */
265 linespec_token_type type
;
267 /* Data for the token */
270 /* A string, given as a stoken */
271 struct stoken string
;
278 /* An instance of the linespec parser. */
280 struct linespec_parser
282 linespec_parser (int flags
, const struct language_defn
*language
,
283 struct program_space
*search_pspace
,
284 struct symtab
*default_symtab
,
286 struct linespec_result
*canonical
);
290 DISABLE_COPY_AND_ASSIGN (linespec_parser
);
292 /* Lexer internal data */
295 /* Save head of input stream. */
296 const char *saved_arg
;
298 /* Head of the input stream. */
301 /* The current token. */
302 linespec_token current
;
305 /* Is the entire linespec quote-enclosed? */
306 int is_quote_enclosed
= 0;
308 /* The state of the parse. */
309 struct linespec_state state
{};
311 /* The result of the parse. */
314 /* What the parser believes the current word point should complete
316 linespec_complete_what complete_what
= linespec_complete_what::NOTHING
;
318 /* The completion word point. The parser advances this as it skips
319 tokens. At some point the input string will end or parsing will
320 fail, and then we attempt completion at the captured completion
321 word point, interpreting the string at completion_word as
323 const char *completion_word
= nullptr;
325 /* If the current token was a quoted string, then this is the
326 quoting character (either " or '). */
327 int completion_quote_char
= 0;
329 /* If the current token was a quoted string, then this points at the
330 end of the quoted string. */
331 const char *completion_quote_end
= nullptr;
333 /* If parsing for completion, then this points at the completion
334 tracker. Otherwise, this is NULL. */
335 struct completion_tracker
*completion_tracker
= nullptr;
338 /* Prototypes for local functions. */
340 static void iterate_over_file_blocks
341 (struct symtab
*symtab
, const lookup_name_info
&name
,
342 domain_search_flags domain
,
343 gdb::function_view
<symbol_found_callback_ftype
> callback
);
345 static void initialize_defaults (struct symtab
**default_symtab
,
348 CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
350 static std::vector
<symtab_and_line
> decode_objc (struct linespec_state
*self
,
354 static std::vector
<symtab
*> symtabs_from_filename
355 (const char *, struct program_space
*pspace
);
357 static std::vector
<block_symbol
> find_label_symbols
358 (struct linespec_state
*self
,
359 const std::vector
<block_symbol
> &function_symbols
,
360 std::vector
<block_symbol
> *label_funcs_ret
,
361 const char *name
, bool completion_mode
= false);
363 static void find_linespec_symbols (struct linespec_state
*self
,
364 const std::vector
<symtab
*> &file_symtabs
,
366 symbol_name_match_type name_match_type
,
367 std::vector
<block_symbol
> *symbols
,
368 std::vector
<bound_minimal_symbol
> *minsyms
);
370 static struct line_offset
371 linespec_parse_variable (struct linespec_state
*self
,
372 const char *variable
);
374 static int symbol_to_sal (struct symtab_and_line
*result
,
375 int funfirstline
, struct symbol
*sym
);
377 static void add_matching_symbols_to_info (const char *name
,
378 symbol_name_match_type name_match_type
,
379 domain_search_flags domain_search_flags
,
380 struct collect_info
*info
,
381 struct program_space
*pspace
);
383 static void add_all_symbol_names_from_pspace
384 (struct collect_info
*info
, struct program_space
*pspace
,
385 const std::vector
<const char *> &names
, domain_search_flags domain_search_flags
);
387 static std::vector
<symtab
*>
388 collect_symtabs_from_filename (const char *file
,
389 struct program_space
*pspace
);
391 static std::vector
<symtab_and_line
> decode_digits_ordinary
392 (struct linespec_state
*self
,
395 const linetable_entry
**best_entry
);
397 static std::vector
<symtab_and_line
> decode_digits_list_mode
398 (struct linespec_state
*self
,
400 struct symtab_and_line val
);
402 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
403 struct minimal_symbol
*msymbol
,
404 std::vector
<symtab_and_line
> *result
);
406 static bool compare_symbols (const block_symbol
&a
, const block_symbol
&b
);
408 static bool compare_msymbols (const bound_minimal_symbol
&a
,
409 const bound_minimal_symbol
&b
);
411 /* Permitted quote characters for the parser. This is different from the
412 completer's quote characters to allow backward compatibility with the
414 static const char linespec_quote_characters
[] = "\"\'";
416 /* Lexer functions. */
418 /* Lex a number from the input in PARSER. This only supports
421 Return true if input is decimal numbers. Return false if not. */
424 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
426 tokenp
->type
= LSTOKEN_NUMBER
;
427 tokenp
->data
.string
.length
= 0;
428 tokenp
->data
.string
.ptr
= parser
->lexer
.stream
;
430 /* Keep any sign at the start of the stream. */
431 if (*parser
->lexer
.stream
== '+' || *parser
->lexer
.stream
== '-')
433 ++tokenp
->data
.string
.length
;
434 ++(parser
->lexer
.stream
);
437 while (isdigit (*parser
->lexer
.stream
))
439 ++tokenp
->data
.string
.length
;
440 ++(parser
->lexer
.stream
);
443 /* If the next character in the input buffer is not a space, comma,
444 quote, or colon, this input does not represent a number. */
445 if (*parser
->lexer
.stream
!= '\0'
446 && !isspace (*parser
->lexer
.stream
) && *parser
->lexer
.stream
!= ','
447 && *parser
->lexer
.stream
!= ':'
448 && !strchr (linespec_quote_characters
, *parser
->lexer
.stream
))
450 parser
->lexer
.stream
= tokenp
->data
.string
.ptr
;
457 /* See linespec.h. */
460 linespec_lexer_lex_keyword (const char *p
)
466 for (i
= 0; linespec_keywords
[i
] != NULL
; ++i
)
468 int len
= strlen (linespec_keywords
[i
]);
472 - "thread" or "task" and the next character is
473 whitespace, we may have found a keyword. It is only a
474 keyword if it is not followed by another keyword.
476 - "-force-condition", the next character may be EOF
477 since this keyword does not take any arguments. Otherwise,
478 it should be followed by a keyword.
480 - "if", ALWAYS stop the lexer, since it is not possible to
481 predict what is going to appear in the condition, which can
482 only be parsed after SaLs have been found. */
483 if (strncmp (p
, linespec_keywords
[i
], len
) == 0)
487 if (i
== FORCE_KEYWORD_INDEX
&& p
[len
] == '\0')
488 return linespec_keywords
[i
];
490 if (!isspace (p
[len
]))
493 if (i
== FORCE_KEYWORD_INDEX
)
497 for (j
= 0; linespec_keywords
[j
] != NULL
; ++j
)
499 int nextlen
= strlen (linespec_keywords
[j
]);
501 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
502 && isspace (p
[nextlen
]))
503 return linespec_keywords
[i
];
506 else if (i
!= IF_KEYWORD_INDEX
)
508 /* We matched a "thread" or "task". */
511 for (j
= 0; linespec_keywords
[j
] != NULL
; ++j
)
513 int nextlen
= strlen (linespec_keywords
[j
]);
515 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
516 && isspace (p
[nextlen
]))
521 return linespec_keywords
[i
];
529 /* See description in linespec.h. */
532 is_ada_operator (const char *string
)
534 const struct ada_opname_map
*mapping
;
536 for (mapping
= ada_opname_table
;
537 mapping
->encoded
!= NULL
538 && !startswith (string
, mapping
->decoded
); ++mapping
)
541 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
544 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
545 the location of QUOTE_CHAR, or NULL if not found. */
548 skip_quote_char (const char *string
, char quote_char
)
550 const char *p
, *last
;
552 p
= last
= find_toplevel_char (string
, quote_char
);
553 while (p
&& *p
!= '\0' && *p
!= ':')
555 p
= find_toplevel_char (p
, quote_char
);
563 /* Make a writable copy of the string given in TOKEN, trimming
564 any trailing whitespace. */
566 static gdb::unique_xmalloc_ptr
<char>
567 copy_token_string (linespec_token token
)
571 if (token
.type
== LSTOKEN_KEYWORD
)
572 return make_unique_xstrdup (token
.data
.keyword
);
574 str
= token
.data
.string
.ptr
;
575 s
= remove_trailing_whitespace (str
, str
+ token
.data
.string
.length
);
577 return gdb::unique_xmalloc_ptr
<char> (savestring (str
, s
- str
));
580 /* Does P represent the end of a quote-enclosed linespec? */
583 is_closing_quote_enclosed (const char *p
)
585 if (strchr (linespec_quote_characters
, *p
))
587 p
= skip_spaces ((char *) p
);
588 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
591 /* Find the end of the parameter list that starts with *INPUT.
592 This helper function assists with lexing string segments
593 which might contain valid (non-terminating) commas. */
596 find_parameter_list_end (const char *input
)
598 char end_char
, start_char
;
603 if (start_char
== '(')
605 else if (start_char
== '<')
614 if (*p
== start_char
)
616 else if (*p
== end_char
)
630 /* If the [STRING, STRING_LEN) string ends with what looks like a
631 keyword, return the keyword start offset in STRING. Return -1
635 string_find_incomplete_keyword_at_end (const char * const *keywords
,
636 const char *string
, size_t string_len
)
638 const char *end
= string
+ string_len
;
641 while (p
> string
&& *p
!= ' ')
646 size_t len
= end
- p
;
647 for (size_t i
= 0; keywords
[i
] != NULL
; ++i
)
648 if (strncmp (keywords
[i
], p
, len
) == 0)
655 /* Lex a string from the input in PARSER. */
657 static linespec_token
658 linespec_lexer_lex_string (linespec_parser
*parser
)
660 linespec_token token
;
661 const char *start
= parser
->lexer
.stream
;
663 token
.type
= LSTOKEN_STRING
;
665 /* If the input stream starts with a quote character, skip to the next
666 quote character, regardless of the content. */
667 if (strchr (linespec_quote_characters
, *parser
->lexer
.stream
))
670 char quote_char
= *parser
->lexer
.stream
;
672 /* Special case: Ada operators. */
673 if (parser
->state
.language
->la_language
== language_ada
674 && quote_char
== '\"')
676 int len
= is_ada_operator (parser
->lexer
.stream
);
680 /* The input is an Ada operator. Return the quoted string
682 token
.data
.string
.ptr
= parser
->lexer
.stream
;
683 token
.data
.string
.length
= len
;
684 parser
->lexer
.stream
+= len
;
688 /* The input does not represent an Ada operator -- fall through
689 to normal quoted string handling. */
692 /* Skip past the beginning quote. */
693 ++(parser
->lexer
.stream
);
695 /* Mark the start of the string. */
696 token
.data
.string
.ptr
= parser
->lexer
.stream
;
698 /* Skip to the ending quote. */
699 end
= skip_quote_char (parser
->lexer
.stream
, quote_char
);
701 /* This helps the completer mode decide whether we have a
703 parser
->completion_quote_char
= quote_char
;
704 parser
->completion_quote_end
= end
;
706 /* Error if the input did not terminate properly, unless in
710 if (parser
->completion_tracker
== NULL
)
711 error (_("unmatched quote"));
713 /* In completion mode, we'll try to complete the incomplete
715 token
.type
= LSTOKEN_STRING
;
716 while (*parser
->lexer
.stream
!= '\0')
717 parser
->lexer
.stream
++;
718 token
.data
.string
.length
= parser
->lexer
.stream
- 1 - start
;
722 /* Skip over the ending quote and mark the length of the string. */
723 parser
->lexer
.stream
= (char *) ++end
;
724 token
.data
.string
.length
= parser
->lexer
.stream
- 2 - start
;
731 /* Otherwise, only identifier characters are permitted.
732 Spaces are the exception. In general, we keep spaces,
733 but only if the next characters in the input do not resolve
734 to one of the keywords.
736 This allows users to forgo quoting CV-qualifiers, template arguments,
737 and similar common language constructs. */
741 if (isspace (*parser
->lexer
.stream
))
743 p
= skip_spaces (parser
->lexer
.stream
);
744 /* When we get here we know we've found something followed by
745 a space (we skip over parens and templates below).
746 So if we find a keyword now, we know it is a keyword and not,
747 say, a function name. */
748 if (linespec_lexer_lex_keyword (p
) != NULL
)
750 token
.data
.string
.ptr
= start
;
751 token
.data
.string
.length
752 = parser
->lexer
.stream
- start
;
756 /* Advance past the whitespace. */
757 parser
->lexer
.stream
= p
;
760 /* If the next character is EOI or (single) ':', the
761 string is complete; return the token. */
762 if (*parser
->lexer
.stream
== 0)
764 token
.data
.string
.ptr
= start
;
765 token
.data
.string
.length
= parser
->lexer
.stream
- start
;
768 else if (parser
->lexer
.stream
[0] == ':')
770 /* Do not tokenize the C++ scope operator. */
771 if (parser
->lexer
.stream
[1] == ':')
772 ++(parser
->lexer
.stream
);
774 /* Do not tokenize ABI tags such as "[abi:cxx11]". */
775 else if (parser
->lexer
.stream
- start
> 4
776 && startswith (parser
->lexer
.stream
- 4, "[abi"))
781 /* Do not tokenify if the input length so far is one
782 (i.e, a single-letter drive name) and the next character
783 is a directory separator. This allows Windows-style
784 paths to be recognized as filenames without quoting it. */
785 else if ((parser
->lexer
.stream
- start
) != 1
786 || !IS_DIR_SEPARATOR (parser
->lexer
.stream
[1]))
788 token
.data
.string
.ptr
= start
;
789 token
.data
.string
.length
790 = parser
->lexer
.stream
- start
;
794 /* Special case: permit quote-enclosed linespecs. */
795 else if (parser
->is_quote_enclosed
796 && strchr (linespec_quote_characters
,
797 *parser
->lexer
.stream
)
798 && is_closing_quote_enclosed (parser
->lexer
.stream
))
800 token
.data
.string
.ptr
= start
;
801 token
.data
.string
.length
= parser
->lexer
.stream
- start
;
804 /* Because commas may terminate a linespec and appear in
805 the middle of valid string input, special cases for
806 '<' and '(' are necessary. */
807 else if (*parser
->lexer
.stream
== '<'
808 || *parser
->lexer
.stream
== '(')
810 /* Don't interpret 'operator<' / 'operator<<' as a
811 template parameter list though. */
812 if (*parser
->lexer
.stream
== '<'
813 && (parser
->state
.language
->la_language
815 && (parser
->lexer
.stream
- start
) >= CP_OPERATOR_LEN
)
817 const char *op
= parser
->lexer
.stream
;
819 while (op
> start
&& isspace (op
[-1]))
821 if (op
- start
>= CP_OPERATOR_LEN
)
823 op
-= CP_OPERATOR_LEN
;
824 if (strncmp (op
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0
826 || !(isalnum (op
[-1]) || op
[-1] == '_')))
828 /* This is an operator name. Keep going. */
829 ++(parser
->lexer
.stream
);
830 if (*parser
->lexer
.stream
== '<')
831 ++(parser
->lexer
.stream
);
837 const char *end
= find_parameter_list_end (parser
->lexer
.stream
);
838 parser
->lexer
.stream
= end
;
840 /* Don't loop around to the normal \0 case above because
841 we don't want to misinterpret a potential keyword at
842 the end of the token when the string isn't
843 "()<>"-balanced. This handles "b
844 function(thread<tab>" in completion mode. */
847 token
.data
.string
.ptr
= start
;
848 token
.data
.string
.length
849 = parser
->lexer
.stream
- start
;
855 /* Commas are terminators, but not if they are part of an
857 else if (*parser
->lexer
.stream
== ',')
859 if ((parser
->state
.language
->la_language
861 && (parser
->lexer
.stream
- start
) > CP_OPERATOR_LEN
)
863 const char *op
= strstr (start
, CP_OPERATOR_STR
);
865 if (op
!= NULL
&& is_operator_name (op
))
867 /* This is an operator name. Keep going. */
868 ++(parser
->lexer
.stream
);
873 /* Comma terminates the string. */
874 token
.data
.string
.ptr
= start
;
875 token
.data
.string
.length
= parser
->lexer
.stream
- start
;
879 /* Advance the stream. */
880 gdb_assert (*(parser
->lexer
.stream
) != '\0');
881 ++(parser
->lexer
.stream
);
888 /* Lex a single linespec token from PARSER. */
890 static linespec_token
891 linespec_lexer_lex_one (linespec_parser
*parser
)
895 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
897 /* Skip any whitespace. */
898 parser
->lexer
.stream
= skip_spaces (parser
->lexer
.stream
);
900 /* Check for a keyword, they end the linespec. */
901 keyword
= linespec_lexer_lex_keyword (parser
->lexer
.stream
);
904 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
905 parser
->lexer
.current
.data
.keyword
= keyword
;
906 /* We do not advance the stream here intentionally:
907 we would like lexing to stop when a keyword is seen.
909 parser->lexer.stream += strlen (keyword); */
911 return parser
->lexer
.current
;
914 /* Handle other tokens. */
915 switch (*parser
->lexer
.stream
)
918 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
922 case '0': case '1': case '2': case '3': case '4':
923 case '5': case '6': case '7': case '8': case '9':
924 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
925 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
929 /* If we have a scope operator, lex the input as a string.
930 Otherwise, return LSTOKEN_COLON. */
931 if (parser
->lexer
.stream
[1] == ':')
932 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
935 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
936 ++(parser
->lexer
.stream
);
940 case '\'': case '\"':
941 /* Special case: permit quote-enclosed linespecs. */
942 if (parser
->is_quote_enclosed
943 && is_closing_quote_enclosed (parser
->lexer
.stream
))
945 ++(parser
->lexer
.stream
);
946 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
949 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
953 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
954 parser
->lexer
.current
.data
.string
.ptr
955 = parser
->lexer
.stream
;
956 parser
->lexer
.current
.data
.string
.length
= 1;
957 ++(parser
->lexer
.stream
);
961 /* If the input is not a number, it must be a string.
962 [Keywords were already considered above.] */
963 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
968 return parser
->lexer
.current
;
971 /* Consume the current token and return the next token in PARSER's
972 input stream. Also advance the completion word for completion
975 static linespec_token
976 linespec_lexer_consume_token (linespec_parser
*parser
)
978 gdb_assert (parser
->lexer
.current
.type
!= LSTOKEN_EOI
);
980 bool advance_word
= (parser
->lexer
.current
.type
!= LSTOKEN_STRING
981 || *parser
->lexer
.stream
!= '\0');
983 /* If we're moving past a string to some other token, it must be the
984 quote was terminated. */
985 if (parser
->completion_quote_char
)
987 gdb_assert (parser
->lexer
.current
.type
== LSTOKEN_STRING
);
989 /* If the string was the last (non-EOI) token, we're past the
990 quote, but remember that for later. */
991 if (*parser
->lexer
.stream
!= '\0')
993 parser
->completion_quote_char
= '\0';
994 parser
->completion_quote_end
= NULL
;;
998 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
999 linespec_lexer_lex_one (parser
);
1001 if (parser
->lexer
.current
.type
== LSTOKEN_STRING
)
1003 /* Advance the completion word past a potential initial
1005 parser
->completion_word
= parser
->lexer
.current
.data
.string
.ptr
;
1007 else if (advance_word
)
1009 /* Advance the completion word past any whitespace. */
1010 parser
->completion_word
= parser
->lexer
.stream
;
1013 return parser
->lexer
.current
;
1016 /* Return the next token without consuming the current token. */
1018 static linespec_token
1019 linespec_lexer_peek_token (linespec_parser
*parser
)
1021 linespec_token next
;
1022 const char *saved_stream
= parser
->lexer
.stream
;
1023 linespec_token saved_token
= parser
->lexer
.current
;
1024 int saved_completion_quote_char
= parser
->completion_quote_char
;
1025 const char *saved_completion_quote_end
= parser
->completion_quote_end
;
1026 const char *saved_completion_word
= parser
->completion_word
;
1028 next
= linespec_lexer_consume_token (parser
);
1029 parser
->lexer
.stream
= saved_stream
;
1030 parser
->lexer
.current
= saved_token
;
1031 parser
->completion_quote_char
= saved_completion_quote_char
;
1032 parser
->completion_quote_end
= saved_completion_quote_end
;
1033 parser
->completion_word
= saved_completion_word
;
1037 /* Helper functions. */
1039 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1040 the new sal, if needed. If not NULL, SYMNAME is the name of the
1041 symbol to use when constructing the new canonical name.
1043 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1044 canonical name for the SAL. */
1047 add_sal_to_sals (struct linespec_state
*self
,
1048 std::vector
<symtab_and_line
> *sals
,
1049 struct symtab_and_line
*sal
,
1050 const char *symname
, int literal_canonical
)
1052 sals
->push_back (*sal
);
1054 if (self
->canonical
)
1056 struct linespec_canonical_name
*canonical
;
1058 self
->canonical_names
= XRESIZEVEC (struct linespec_canonical_name
,
1059 self
->canonical_names
,
1061 canonical
= &self
->canonical_names
[sals
->size () - 1];
1062 if (!literal_canonical
&& sal
->symtab
)
1064 symtab_to_fullname (sal
->symtab
);
1066 /* Note that the filter doesn't have to be a valid linespec
1067 input. We only apply the ":LINE" treatment to Ada for
1069 if (symname
!= NULL
&& sal
->line
!= 0
1070 && self
->language
->la_language
== language_ada
)
1071 canonical
->suffix
= xstrprintf ("%s:%d", symname
,
1072 sal
->line
).release ();
1073 else if (symname
!= NULL
)
1074 canonical
->suffix
= xstrdup (symname
);
1076 canonical
->suffix
= xstrprintf ("%d", sal
->line
).release ();
1077 canonical
->symtab
= sal
->symtab
;
1081 if (symname
!= NULL
)
1082 canonical
->suffix
= xstrdup (symname
);
1084 canonical
->suffix
= xstrdup ("<unknown>");
1085 canonical
->symtab
= NULL
;
1090 /* A hash function for address_entry. */
1093 hash_address_entry (const void *p
)
1095 const struct address_entry
*aep
= (const struct address_entry
*) p
;
1098 hash
= iterative_hash_object (aep
->pspace
, 0);
1099 return iterative_hash_object (aep
->addr
, hash
);
1102 /* An equality function for address_entry. */
1105 eq_address_entry (const void *a
, const void *b
)
1107 const struct address_entry
*aea
= (const struct address_entry
*) a
;
1108 const struct address_entry
*aeb
= (const struct address_entry
*) b
;
1110 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
1113 /* Check whether the address, represented by PSPACE and ADDR, is
1114 already in the set. If so, return 0. Otherwise, add it and return
1118 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
1120 struct address_entry e
, *p
;
1125 slot
= htab_find_slot (set
, &e
, INSERT
);
1129 p
= XNEW (struct address_entry
);
1130 memcpy (p
, &e
, sizeof (struct address_entry
));
1136 /* A helper that walks over all matching symtabs in all objfiles and
1137 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1138 not NULL, then the search is restricted to just that program
1139 space. If INCLUDE_INLINE is true then symbols representing
1140 inlined instances of functions will be included in the result. */
1143 iterate_over_all_matching_symtabs
1144 (struct linespec_state
*state
,
1145 const lookup_name_info
&lookup_name
,
1146 const domain_search_flags domain
,
1147 struct program_space
*search_pspace
, bool include_inline
,
1148 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1150 for (struct program_space
*pspace
: program_spaces
)
1152 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
1154 if (pspace
->executing_startup
)
1157 set_current_program_space (pspace
);
1159 for (objfile
*objfile
: pspace
->objfiles ())
1161 objfile
->expand_symtabs_matching (NULL
, &lookup_name
, NULL
, NULL
,
1162 (SEARCH_GLOBAL_BLOCK
1163 | SEARCH_STATIC_BLOCK
),
1166 for (compunit_symtab
*cu
: objfile
->compunits ())
1168 struct symtab
*symtab
= cu
->primary_filetab ();
1170 iterate_over_file_blocks (symtab
, lookup_name
, domain
, callback
);
1174 const struct block
*block
;
1176 const blockvector
*bv
= symtab
->compunit ()->blockvector ();
1178 for (i
= FIRST_LOCAL_BLOCK
; i
< bv
->num_blocks (); i
++)
1180 block
= bv
->block (i
);
1181 state
->language
->iterate_over_symbols
1182 (block
, lookup_name
, domain
,
1183 [&] (block_symbol
*bsym
)
1185 /* Restrict calls to CALLBACK to symbols
1186 representing inline symbols only. */
1187 if (bsym
->symbol
->is_inlined ())
1188 return callback (bsym
);
1198 /* Returns the block to be used for symbol searches from
1199 the current location. */
1201 static const struct block
*
1202 get_current_search_block (void)
1204 /* get_selected_block can change the current language when there is
1205 no selected frame yet. */
1206 scoped_restore_current_language save_language
;
1207 return get_selected_block (0);
1210 /* Iterate over static and global blocks. */
1213 iterate_over_file_blocks
1214 (struct symtab
*symtab
, const lookup_name_info
&name
,
1215 domain_search_flags domain
,
1216 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1218 const struct block
*block
;
1220 for (block
= symtab
->compunit ()->blockvector ()->static_block ();
1222 block
= block
->superblock ())
1223 current_language
->iterate_over_symbols (block
, name
, domain
, callback
);
1226 /* A helper for find_method. This finds all methods in type T of
1227 language T_LANG which match NAME. It adds matching symbol names to
1228 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
1231 find_methods (struct type
*t
, enum language t_lang
, const char *name
,
1232 std::vector
<const char *> *result_names
,
1233 std::vector
<struct type
*> *superclasses
)
1236 const char *class_name
= t
->name ();
1238 /* Ignore this class if it doesn't have a name. This is ugly, but
1239 unless we figure out how to get the physname without the name of
1240 the class, then the loop can't do any good. */
1244 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1245 symbol_name_matcher_ftype
*symbol_name_compare
1246 = language_def (t_lang
)->get_symbol_name_matcher (lookup_name
);
1248 t
= check_typedef (t
);
1250 /* Loop over each method name. At this level, all overloads of a name
1251 are counted as a single name. There is an inner loop which loops over
1254 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1255 method_counter
>= 0;
1258 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1260 if (symbol_name_compare (method_name
, lookup_name
, NULL
))
1264 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1270 const char *phys_name
;
1272 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1273 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1275 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1276 result_names
->push_back (phys_name
);
1282 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1283 superclasses
->push_back (TYPE_BASECLASS (t
, ibase
));
1286 /* The string equivalent of find_toplevel_char. Returns a pointer
1287 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1288 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1291 find_toplevel_string (const char *haystack
, const char *needle
)
1293 const char *s
= haystack
;
1297 s
= find_toplevel_char (s
, *needle
);
1301 /* Found first char in HAYSTACK; check rest of string. */
1302 if (startswith (s
, needle
))
1305 /* Didn't find it; loop over HAYSTACK, looking for the next
1306 instance of the first character of NEEDLE. */
1310 while (s
!= NULL
&& *s
!= '\0');
1312 /* NEEDLE was not found in HAYSTACK. */
1316 /* Convert CANONICAL to its string representation using
1317 symtab_to_fullname for SYMTAB. */
1320 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1322 if (canonical
->symtab
== NULL
)
1323 return canonical
->suffix
;
1325 return string_printf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1329 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1330 and store the result in SELF->CANONICAL. */
1333 filter_results (struct linespec_state
*self
,
1334 std::vector
<symtab_and_line
> *result
,
1335 const std::vector
<const char *> &filters
)
1337 for (const char *name
: filters
)
1341 for (size_t j
= 0; j
< result
->size (); ++j
)
1343 const struct linespec_canonical_name
*canonical
;
1345 canonical
= &self
->canonical_names
[j
];
1346 std::string fullform
= canonical_to_fullform (canonical
);
1348 if (name
== fullform
)
1349 lsal
.sals
.push_back ((*result
)[j
]);
1352 if (!lsal
.sals
.empty ())
1354 lsal
.canonical
= xstrdup (name
);
1355 self
->canonical
->lsals
.push_back (std::move (lsal
));
1359 self
->canonical
->pre_expanded
= 0;
1362 /* Store RESULT into SELF->CANONICAL. */
1365 convert_results_to_lsals (struct linespec_state
*self
,
1366 std::vector
<symtab_and_line
> *result
)
1368 struct linespec_sals lsal
;
1370 lsal
.canonical
= NULL
;
1371 lsal
.sals
= std::move (*result
);
1372 self
->canonical
->lsals
.push_back (std::move (lsal
));
1375 /* A structure that contains two string representations of a struct
1376 linespec_canonical_name:
1377 - one where the symtab's fullname is used;
1378 - one where the filename followed the "set filename-display"
1381 struct decode_line_2_item
1383 decode_line_2_item (std::string
&&fullform_
, std::string
&&displayform_
,
1385 : fullform (std::move (fullform_
)),
1386 displayform (std::move (displayform_
)),
1387 selected (selected_
)
1391 /* The form using symtab_to_fullname. */
1392 std::string fullform
;
1394 /* The form using symtab_to_filename_for_display. */
1395 std::string displayform
;
1397 /* Field is initialized to zero and it is set to one if the user
1398 requested breakpoint for this entry. */
1399 unsigned int selected
: 1;
1402 /* Helper for std::sort to sort decode_line_2_item entries by
1403 DISPLAYFORM and secondarily by FULLFORM. */
1406 decode_line_2_compare_items (const decode_line_2_item
&a
,
1407 const decode_line_2_item
&b
)
1409 if (a
.displayform
!= b
.displayform
)
1410 return a
.displayform
< b
.displayform
;
1411 return a
.fullform
< b
.fullform
;
1414 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1415 will either return normally, throw an exception on multiple
1416 results, or present a menu to the user. On return, the SALS vector
1417 in SELF->CANONICAL is set up properly. */
1420 decode_line_2 (struct linespec_state
*self
,
1421 std::vector
<symtab_and_line
> *result
,
1422 const char *select_mode
)
1427 std::vector
<const char *> filters
;
1428 std::vector
<struct decode_line_2_item
> items
;
1430 gdb_assert (select_mode
!= multiple_symbols_all
);
1431 gdb_assert (self
->canonical
!= NULL
);
1432 gdb_assert (!result
->empty ());
1434 /* Prepare ITEMS array. */
1435 for (i
= 0; i
< result
->size (); ++i
)
1437 const struct linespec_canonical_name
*canonical
;
1438 std::string displayform
;
1440 canonical
= &self
->canonical_names
[i
];
1441 gdb_assert (canonical
->suffix
!= NULL
);
1443 std::string fullform
= canonical_to_fullform (canonical
);
1445 if (canonical
->symtab
== NULL
)
1446 displayform
= canonical
->suffix
;
1449 const char *fn_for_display
;
1451 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1452 displayform
= string_printf ("%s:%s", fn_for_display
,
1456 items
.emplace_back (std::move (fullform
), std::move (displayform
),
1460 /* Sort the list of method names. */
1461 std::sort (items
.begin (), items
.end (), decode_line_2_compare_items
);
1463 /* Remove entries with the same FULLFORM. */
1464 items
.erase (std::unique (items
.begin (), items
.end (),
1465 [] (const struct decode_line_2_item
&a
,
1466 const struct decode_line_2_item
&b
)
1468 return a
.fullform
== b
.fullform
;
1472 if (select_mode
== multiple_symbols_cancel
&& items
.size () > 1)
1473 error (_("canceled because the command is ambiguous\n"
1474 "See set/show multiple-symbol."));
1476 if (select_mode
== multiple_symbols_all
|| items
.size () == 1)
1478 convert_results_to_lsals (self
, result
);
1482 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1483 for (i
= 0; i
< items
.size (); i
++)
1484 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
.c_str ());
1486 prompt
= getenv ("PS2");
1493 args
= command_line_input (buffer
, prompt
, "overload-choice");
1495 if (args
== 0 || *args
== 0)
1496 error_no_arg (_("one or more choice numbers"));
1498 number_or_range_parser
parser (args
);
1499 while (!parser
.finished ())
1501 int num
= parser
.get_number ();
1504 error (_("canceled"));
1507 /* We intentionally make this result in a single breakpoint,
1508 contrary to what older versions of gdb did. The
1509 rationale is that this lets a user get the
1510 multiple_symbols_all behavior even with the 'ask'
1511 setting; and he can get separate breakpoints by entering
1512 "2-57" at the query. */
1513 convert_results_to_lsals (self
, result
);
1518 if (num
>= items
.size ())
1519 printf_unfiltered (_("No choice number %d.\n"), num
);
1522 struct decode_line_2_item
*item
= &items
[num
];
1524 if (!item
->selected
)
1526 filters
.push_back (item
->fullform
.c_str ());
1531 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1537 filter_results (self
, result
, filters
);
1542 /* The parser of linespec itself. */
1544 /* Throw an appropriate error when SYMBOL is not found (optionally in
1547 [[noreturn
]] static void
1548 symbol_not_found_error (const char *symbol
, const char *filename
)
1553 if (!have_full_symbols (current_program_space
)
1554 && !have_partial_symbols (current_program_space
)
1555 && !have_minimal_symbols (current_program_space
))
1556 throw_error (NOT_FOUND_ERROR
,
1557 _("No symbol table is loaded. Use the \"file\" command."));
1559 /* If SYMBOL starts with '$', the user attempted to either lookup
1560 a function/variable in his code starting with '$' or an internal
1561 variable of that name. Since we do not know which, be concise and
1562 explain both possibilities. */
1566 throw_error (NOT_FOUND_ERROR
,
1567 _("Undefined convenience variable or function \"%s\" "
1568 "not defined in \"%s\"."), symbol
, filename
);
1570 throw_error (NOT_FOUND_ERROR
,
1571 _("Undefined convenience variable or function \"%s\" "
1572 "not defined."), symbol
);
1577 throw_error (NOT_FOUND_ERROR
,
1578 _("Function \"%s\" not defined in \"%s\"."),
1581 throw_error (NOT_FOUND_ERROR
,
1582 _("Function \"%s\" not defined."), symbol
);
1586 /* Throw an appropriate error when an unexpected token is encountered
1589 [[noreturn
]] static void
1590 unexpected_linespec_error (linespec_parser
*parser
)
1592 linespec_token token
;
1593 static const char * token_type_strings
[]
1594 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1596 /* Get the token that generated the error. */
1597 token
= linespec_lexer_lex_one (parser
);
1599 /* Finally, throw the error. */
1600 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1601 || token
.type
== LSTOKEN_KEYWORD
)
1603 gdb::unique_xmalloc_ptr
<char> string
= copy_token_string (token
);
1604 throw_error (GENERIC_ERROR
,
1605 _("malformed linespec error: unexpected %s, \"%s\""),
1606 token_type_strings
[token
.type
], string
.get ());
1609 throw_error (GENERIC_ERROR
,
1610 _("malformed linespec error: unexpected %s"),
1611 token_type_strings
[token
.type
]);
1614 /* Throw an undefined label error. */
1616 [[noreturn
]] static void
1617 undefined_label_error (const char *function
, const char *label
)
1619 if (function
!= NULL
)
1620 throw_error (NOT_FOUND_ERROR
,
1621 _("No label \"%s\" defined in function \"%s\"."),
1624 throw_error (NOT_FOUND_ERROR
,
1625 _("No label \"%s\" defined in current function."),
1629 /* Throw a source file not found error. */
1631 [[noreturn
]] static void
1632 source_file_not_found_error (const char *name
)
1634 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), name
);
1637 /* Unless at EIO, save the current stream position as completion word
1638 point, and consume the next token. */
1640 static linespec_token
1641 save_stream_and_consume_token (linespec_parser
*parser
)
1643 if (linespec_lexer_peek_token (parser
).type
!= LSTOKEN_EOI
)
1644 parser
->completion_word
= parser
->lexer
.stream
;
1645 return linespec_lexer_consume_token (parser
);
1648 /* See description in linespec.h. */
1651 linespec_parse_line_offset (const char *string
)
1653 const char *start
= string
;
1654 struct line_offset line_offset
;
1658 line_offset
.sign
= LINE_OFFSET_PLUS
;
1661 else if (*string
== '-')
1663 line_offset
.sign
= LINE_OFFSET_MINUS
;
1667 line_offset
.sign
= LINE_OFFSET_NONE
;
1669 if (*string
!= '\0' && !isdigit (*string
))
1670 error (_("malformed line offset: \"%s\""), start
);
1672 /* Right now, we only allow base 10 for offsets. */
1673 line_offset
.offset
= atoi (string
);
1677 /* In completion mode, if the user is still typing the number, there's
1678 no possible completion to offer. But if there's already input past
1679 the number, setup to expect NEXT. */
1682 set_completion_after_number (linespec_parser
*parser
,
1683 linespec_complete_what next
)
1685 if (*parser
->lexer
.stream
== ' ')
1687 parser
->completion_word
= skip_spaces (parser
->lexer
.stream
+ 1);
1688 parser
->complete_what
= next
;
1692 parser
->completion_word
= parser
->lexer
.stream
;
1693 parser
->complete_what
= linespec_complete_what::NOTHING
;
1697 /* Parse the basic_spec in PARSER's input. */
1700 linespec_parse_basic (linespec_parser
*parser
)
1702 gdb::unique_xmalloc_ptr
<char> name
;
1703 linespec_token token
;
1705 /* Get the next token. */
1706 token
= linespec_lexer_lex_one (parser
);
1708 /* If it is EOI or KEYWORD, issue an error. */
1709 if (token
.type
== LSTOKEN_KEYWORD
)
1711 parser
->complete_what
= linespec_complete_what::NOTHING
;
1712 unexpected_linespec_error (parser
);
1714 else if (token
.type
== LSTOKEN_EOI
)
1716 unexpected_linespec_error (parser
);
1718 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1719 else if (token
.type
== LSTOKEN_NUMBER
)
1721 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1723 /* Record the line offset and get the next token. */
1724 name
= copy_token_string (token
);
1725 parser
->result
.explicit_loc
.line_offset
1726 = linespec_parse_line_offset (name
.get ());
1728 /* Get the next token. */
1729 token
= linespec_lexer_consume_token (parser
);
1731 /* If the next token is a comma, stop parsing and return. */
1732 if (token
.type
== LSTOKEN_COMMA
)
1734 parser
->complete_what
= linespec_complete_what::NOTHING
;
1738 /* If the next token is anything but EOI or KEYWORD, issue
1740 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1741 unexpected_linespec_error (parser
);
1744 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1747 /* Next token must be LSTOKEN_STRING. */
1748 if (token
.type
!= LSTOKEN_STRING
)
1750 parser
->complete_what
= linespec_complete_what::NOTHING
;
1751 unexpected_linespec_error (parser
);
1754 /* The current token will contain the name of a function, method,
1756 name
= copy_token_string (token
);
1758 if (parser
->completion_tracker
!= NULL
)
1760 /* If the function name ends with a ":", then this may be an
1761 incomplete "::" scope operator instead of a label separator.
1764 which should expand to:
1767 Do a tentative completion assuming the later. If we find
1768 completions, advance the stream past the colon token and make
1769 it part of the function name/token. */
1771 if (!parser
->completion_quote_char
1772 && strcmp (parser
->lexer
.stream
, ":") == 0)
1774 completion_tracker
tmp_tracker (false);
1775 const char *source_filename
1776 = parser
->result
.explicit_loc
.source_filename
.get ();
1777 symbol_name_match_type match_type
1778 = parser
->result
.explicit_loc
.func_name_match_type
;
1780 linespec_complete_function (tmp_tracker
,
1781 parser
->completion_word
,
1785 if (tmp_tracker
.have_completions ())
1787 parser
->lexer
.stream
++;
1788 token
.data
.string
.length
++;
1790 name
.reset (savestring (parser
->completion_word
,
1791 (parser
->lexer
.stream
1792 - parser
->completion_word
)));
1796 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1800 std::vector
<block_symbol
> symbols
;
1801 std::vector
<bound_minimal_symbol
> minimal_symbols
;
1803 /* Try looking it up as a function/method. */
1804 find_linespec_symbols (&parser
->state
,
1805 parser
->result
.file_symtabs
, name
.get (),
1806 parser
->result
.explicit_loc
.func_name_match_type
,
1807 &symbols
, &minimal_symbols
);
1809 if (!symbols
.empty () || !minimal_symbols
.empty ())
1811 parser
->result
.function_symbols
= std::move (symbols
);
1812 parser
->result
.minimal_symbols
= std::move (minimal_symbols
);
1813 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1817 /* NAME was not a function or a method. So it must be a label
1818 name or user specified variable like "break foo.c:$zippo". */
1819 std::vector
<block_symbol
> labels
1820 = find_label_symbols (&parser
->state
, {}, &symbols
,
1823 if (!labels
.empty ())
1825 parser
->result
.labels
.label_symbols
= std::move (labels
);
1826 parser
->result
.labels
.function_symbols
= std::move (symbols
);
1827 parser
->result
.explicit_loc
.label_name
= std::move (name
);
1829 else if (token
.type
== LSTOKEN_STRING
1830 && *token
.data
.string
.ptr
== '$')
1832 /* User specified a convenience variable or history value. */
1833 parser
->result
.explicit_loc
.line_offset
1834 = linespec_parse_variable (&parser
->state
, name
.get ());
1836 if (parser
->result
.explicit_loc
.line_offset
.sign
1837 == LINE_OFFSET_UNKNOWN
)
1839 /* The user-specified variable was not valid. Do not
1840 throw an error here. parse_linespec will do it for us. */
1841 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1847 /* The name is also not a label. Abort parsing. Do not throw
1848 an error here. parse_linespec will do it for us. */
1850 /* Save a copy of the name we were trying to lookup. */
1851 parser
->result
.explicit_loc
.function_name
= std::move (name
);
1857 int previous_qc
= parser
->completion_quote_char
;
1859 /* Get the next token. */
1860 token
= linespec_lexer_consume_token (parser
);
1862 if (token
.type
== LSTOKEN_EOI
)
1864 if (previous_qc
&& !parser
->completion_quote_char
)
1865 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1867 else if (token
.type
== LSTOKEN_COLON
)
1869 /* User specified a label or a lineno. */
1870 token
= linespec_lexer_consume_token (parser
);
1872 if (token
.type
== LSTOKEN_NUMBER
)
1874 /* User specified an offset. Record the line offset and
1875 get the next token. */
1876 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1878 name
= copy_token_string (token
);
1879 parser
->result
.explicit_loc
.line_offset
1880 = linespec_parse_line_offset (name
.get ());
1882 /* Get the next token. */
1883 token
= linespec_lexer_consume_token (parser
);
1885 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
1887 parser
->complete_what
= linespec_complete_what::LABEL
;
1889 else if (token
.type
== LSTOKEN_STRING
)
1891 parser
->complete_what
= linespec_complete_what::LABEL
;
1893 /* If we have text after the label separated by whitespace
1894 (e.g., "b func():lab i<tab>"), don't consider it part of
1895 the label. In completion mode that should complete to
1896 "if", in normal mode, the 'i' should be treated as
1898 if (parser
->completion_quote_char
== '\0')
1900 const char *ptr
= token
.data
.string
.ptr
;
1901 for (size_t i
= 0; i
< token
.data
.string
.length
; i
++)
1905 token
.data
.string
.length
= i
;
1906 parser
->lexer
.stream
= skip_spaces (ptr
+ i
+ 1);
1912 if (parser
->completion_tracker
!= NULL
)
1914 if (parser
->lexer
.stream
[-1] == ' ')
1916 parser
->completion_word
= parser
->lexer
.stream
;
1917 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1922 std::vector
<block_symbol
> symbols
;
1924 /* Grab a copy of the label's name and look it up. */
1925 name
= copy_token_string (token
);
1926 std::vector
<block_symbol
> labels
1927 = find_label_symbols (&parser
->state
,
1928 parser
->result
.function_symbols
,
1929 &symbols
, name
.get ());
1931 if (!labels
.empty ())
1933 parser
->result
.labels
.label_symbols
= std::move (labels
);
1934 parser
->result
.labels
.function_symbols
= std::move (symbols
);
1935 parser
->result
.explicit_loc
.label_name
= std::move (name
);
1939 /* We don't know what it was, but it isn't a label. */
1940 undefined_label_error
1941 (parser
->result
.explicit_loc
.function_name
.get (),
1947 /* Check for a line offset. */
1948 token
= save_stream_and_consume_token (parser
);
1949 if (token
.type
== LSTOKEN_COLON
)
1951 /* Get the next token. */
1952 token
= linespec_lexer_consume_token (parser
);
1954 /* It must be a line offset. */
1955 if (token
.type
!= LSTOKEN_NUMBER
)
1956 unexpected_linespec_error (parser
);
1958 /* Record the line offset and get the next token. */
1959 name
= copy_token_string (token
);
1961 parser
->result
.explicit_loc
.line_offset
1962 = linespec_parse_line_offset (name
.get ());
1964 /* Get the next token. */
1965 token
= linespec_lexer_consume_token (parser
);
1970 /* Trailing ':' in the input. Issue an error. */
1971 unexpected_linespec_error (parser
);
1976 /* Canonicalize the linespec contained in LS. The result is saved into
1977 STATE->canonical. This function handles both linespec and explicit
1981 canonicalize_linespec (struct linespec_state
*state
, const linespec
*ls
)
1983 /* If canonicalization was not requested, no need to do anything. */
1984 if (!state
->canonical
)
1987 /* Save everything as an explicit location. */
1988 state
->canonical
->locspec
= ls
->explicit_loc
.clone ();
1989 explicit_location_spec
*explicit_loc
1990 = as_explicit_location_spec (state
->canonical
->locspec
.get ());
1992 if (explicit_loc
->label_name
!= NULL
)
1994 state
->canonical
->special_display
= 1;
1996 if (explicit_loc
->function_name
== NULL
)
1998 /* No function was specified, so add the symbol name. */
1999 gdb_assert (ls
->labels
.function_symbols
.size () == 1);
2000 block_symbol s
= ls
->labels
.function_symbols
.front ();
2001 explicit_loc
->function_name
2002 = make_unique_xstrdup (s
.symbol
->natural_name ());
2006 /* If this location originally came from a linespec, save a string
2007 representation of it for display and saving to file. */
2008 if (state
->is_linespec
)
2009 explicit_loc
->set_string (explicit_loc
->to_linespec ());
2012 /* Given a line offset in LS, construct the relevant SALs. */
2014 static std::vector
<symtab_and_line
>
2015 create_sals_line_offset (struct linespec_state
*self
,
2018 int use_default
= 0;
2020 /* This is where we need to make sure we have good defaults.
2021 We must guarantee that this section of code is never executed
2022 when we are called with just a function name, since
2023 set_default_source_symtab_and_line uses
2024 select_source_symtab that calls us with such an argument. */
2026 if (ls
->file_symtabs
.size () == 1
2027 && ls
->file_symtabs
.front () == nullptr)
2029 set_current_program_space (self
->program_space
);
2031 /* Make sure we have at least a default source line. */
2032 set_default_source_symtab_and_line ();
2033 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
2035 = collect_symtabs_from_filename (self
->default_symtab
->filename
,
2036 self
->search_pspace
);
2040 symtab_and_line val
;
2041 val
.line
= ls
->explicit_loc
.line_offset
.offset
;
2042 switch (ls
->explicit_loc
.line_offset
.sign
)
2044 case LINE_OFFSET_PLUS
:
2045 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2048 val
.line
= self
->default_line
+ val
.line
;
2051 case LINE_OFFSET_MINUS
:
2052 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2055 val
.line
= self
->default_line
- val
.line
;
2057 val
.line
= -val
.line
;
2060 case LINE_OFFSET_NONE
:
2061 break; /* No need to adjust val.line. */
2064 std::vector
<symtab_and_line
> values
;
2065 if (self
->list_mode
)
2066 values
= decode_digits_list_mode (self
, ls
, val
);
2069 const linetable_entry
*best_entry
= NULL
;
2072 std::vector
<symtab_and_line
> intermediate_results
2073 = decode_digits_ordinary (self
, ls
, val
.line
, &best_entry
);
2074 if (intermediate_results
.empty () && best_entry
!= NULL
)
2075 intermediate_results
= decode_digits_ordinary (self
, ls
,
2079 /* For optimized code, the compiler can scatter one source line
2080 across disjoint ranges of PC values, even when no duplicate
2081 functions or inline functions are involved. For example,
2082 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2083 function can result in two PC ranges. In this case, we don't
2084 want to set a breakpoint on the first PC of each range. To filter
2085 such cases, we use containing blocks -- for each PC found
2086 above, we see if there are other PCs that are in the same
2087 block. If yes, the other PCs are filtered out. */
2089 gdb::def_vector
<int> filter (intermediate_results
.size ());
2090 gdb::def_vector
<const block
*> blocks (intermediate_results
.size ());
2092 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2094 set_current_program_space (intermediate_results
[i
].pspace
);
2097 blocks
[i
] = block_for_pc_sect (intermediate_results
[i
].pc
,
2098 intermediate_results
[i
].section
);
2101 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2103 if (blocks
[i
] != NULL
)
2104 for (j
= i
+ 1; j
< intermediate_results
.size (); ++j
)
2106 if (blocks
[j
] == blocks
[i
])
2114 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2117 struct symbol
*sym
= (blocks
[i
]
2118 ? blocks
[i
]->containing_function ()
2121 if (self
->funfirstline
)
2122 skip_prologue_sal (&intermediate_results
[i
]);
2123 intermediate_results
[i
].symbol
= sym
;
2124 add_sal_to_sals (self
, &values
, &intermediate_results
[i
],
2125 sym
? sym
->natural_name () : NULL
, 0);
2129 if (values
.empty ())
2131 if (ls
->explicit_loc
.source_filename
)
2132 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
2133 val
.line
, ls
->explicit_loc
.source_filename
.get ());
2135 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
2142 /* Convert the given ADDRESS into SaLs. */
2144 static std::vector
<symtab_and_line
>
2145 convert_address_location_to_sals (struct linespec_state
*self
,
2148 symtab_and_line sal
= find_pc_line (address
, 0);
2150 sal
.section
= find_pc_overlay (address
);
2151 sal
.explicit_pc
= 1;
2152 sal
.symbol
= find_pc_sect_containing_function (sal
.pc
, sal
.section
);
2154 std::vector
<symtab_and_line
> sals
;
2155 add_sal_to_sals (self
, &sals
, &sal
, core_addr_to_string (address
), 1);
2160 /* Create and return SALs from the linespec LS. */
2162 static std::vector
<symtab_and_line
>
2163 convert_linespec_to_sals (struct linespec_state
*state
, linespec
*ls
)
2165 std::vector
<symtab_and_line
> sals
;
2167 if (!ls
->labels
.label_symbols
.empty ())
2169 /* We have just a bunch of functions/methods or labels. */
2170 struct symtab_and_line sal
;
2172 for (const auto &sym
: ls
->labels
.label_symbols
)
2174 struct program_space
*pspace
2175 = sym
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
2177 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2178 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2179 add_sal_to_sals (state
, &sals
, &sal
,
2180 sym
.symbol
->natural_name (), 0);
2183 else if (!ls
->function_symbols
.empty () || !ls
->minimal_symbols
.empty ())
2185 /* We have just a bunch of functions and/or methods. */
2186 if (!ls
->function_symbols
.empty ())
2188 /* Sort symbols so that symbols with the same program space are next
2190 std::sort (ls
->function_symbols
.begin (),
2191 ls
->function_symbols
.end (),
2194 for (const auto &sym
: ls
->function_symbols
)
2196 program_space
*pspace
2197 = sym
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
2198 set_current_program_space (pspace
);
2200 /* Don't skip to the first line of the function if we
2201 had found an ifunc minimal symbol for this function,
2202 because that means that this function is an ifunc
2203 resolver with the same name as the ifunc itself. */
2204 bool found_ifunc
= false;
2206 if (state
->funfirstline
2207 && !ls
->minimal_symbols
.empty ()
2208 && sym
.symbol
->aclass () == LOC_BLOCK
)
2210 const CORE_ADDR addr
2211 = sym
.symbol
->value_block ()->entry_pc ();
2213 for (const auto &elem
: ls
->minimal_symbols
)
2215 if (elem
.minsym
->type () == mst_text_gnu_ifunc
2216 || elem
.minsym
->type () == mst_data_gnu_ifunc
)
2218 CORE_ADDR msym_addr
= elem
.value_address ();
2219 if (elem
.minsym
->type () == mst_data_gnu_ifunc
)
2221 struct gdbarch
*gdbarch
2222 = elem
.objfile
->arch ();
2224 = (gdbarch_convert_from_func_ptr_addr
2227 current_inferior ()->top_target ()));
2230 if (msym_addr
== addr
)
2241 symtab_and_line sal
;
2242 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2243 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2244 add_sal_to_sals (state
, &sals
, &sal
,
2245 sym
.symbol
->natural_name (), 0);
2250 if (!ls
->minimal_symbols
.empty ())
2252 /* Sort minimal symbols by program space, too */
2253 std::sort (ls
->minimal_symbols
.begin (),
2254 ls
->minimal_symbols
.end (),
2257 for (const auto &elem
: ls
->minimal_symbols
)
2259 program_space
*pspace
= elem
.objfile
->pspace ();
2260 set_current_program_space (pspace
);
2261 minsym_found (state
, elem
.objfile
, elem
.minsym
, &sals
);
2265 else if (ls
->explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2267 /* Only an offset was specified. */
2268 sals
= create_sals_line_offset (state
, ls
);
2270 /* Make sure we have a filename for canonicalization. */
2271 if (ls
->explicit_loc
.source_filename
== NULL
)
2273 const char *filename
= state
->default_symtab
->filename
;
2275 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2276 form so that displaying SOURCE_FILENAME can follow the current
2277 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2278 it has been kept for code simplicity only in absolute form. */
2279 ls
->explicit_loc
.source_filename
= make_unique_xstrdup (filename
);
2284 /* We haven't found any results... */
2288 canonicalize_linespec (state
, ls
);
2290 if (!sals
.empty () && state
->canonical
!= NULL
)
2291 state
->canonical
->pre_expanded
= 1;
2296 /* Build RESULT from the explicit location spec components
2297 SOURCE_FILENAME, FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2300 convert_explicit_location_spec_to_linespec
2301 (struct linespec_state
*self
,
2303 const char *source_filename
,
2304 const char *function_name
,
2305 symbol_name_match_type fname_match_type
,
2306 const char *label_name
,
2307 struct line_offset line_offset
)
2309 std::vector
<bound_minimal_symbol
> minimal_symbols
;
2311 result
->explicit_loc
.func_name_match_type
= fname_match_type
;
2313 if (source_filename
!= NULL
)
2317 result
->file_symtabs
2318 = symtabs_from_filename (source_filename
, self
->search_pspace
);
2320 catch (const gdb_exception_error
&except
)
2322 source_file_not_found_error (source_filename
);
2324 result
->explicit_loc
.source_filename
2325 = make_unique_xstrdup (source_filename
);
2329 /* A NULL entry means to use the default symtab. */
2330 result
->file_symtabs
.push_back (nullptr);
2333 if (function_name
!= NULL
)
2335 std::vector
<block_symbol
> symbols
;
2337 find_linespec_symbols (self
, result
->file_symtabs
,
2338 function_name
, fname_match_type
,
2339 &symbols
, &minimal_symbols
);
2341 if (symbols
.empty () && minimal_symbols
.empty ())
2342 symbol_not_found_error (function_name
,
2343 result
->explicit_loc
.source_filename
.get ());
2345 result
->explicit_loc
.function_name
2346 = make_unique_xstrdup (function_name
);
2347 result
->function_symbols
= std::move (symbols
);
2348 result
->minimal_symbols
= std::move (minimal_symbols
);
2351 if (label_name
!= NULL
)
2353 std::vector
<block_symbol
> symbols
;
2354 std::vector
<block_symbol
> labels
2355 = find_label_symbols (self
, result
->function_symbols
,
2356 &symbols
, label_name
);
2358 if (labels
.empty ())
2359 undefined_label_error (result
->explicit_loc
.function_name
.get (),
2362 result
->explicit_loc
.label_name
= make_unique_xstrdup (label_name
);
2363 result
->labels
.label_symbols
= labels
;
2364 result
->labels
.function_symbols
= std::move (symbols
);
2367 if (line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2368 result
->explicit_loc
.line_offset
= line_offset
;
2371 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2373 static std::vector
<symtab_and_line
>
2374 convert_explicit_location_spec_to_sals
2375 (struct linespec_state
*self
,
2377 const explicit_location_spec
*explicit_spec
)
2379 convert_explicit_location_spec_to_linespec (self
, result
,
2380 explicit_spec
->source_filename
.get (),
2381 explicit_spec
->function_name
.get (),
2382 explicit_spec
->func_name_match_type
,
2383 explicit_spec
->label_name
.get (),
2384 explicit_spec
->line_offset
);
2385 return convert_linespec_to_sals (self
, result
);
2388 /* Parse a string that specifies a linespec.
2390 The basic grammar of linespecs:
2392 linespec -> var_spec | basic_spec
2393 var_spec -> '$' (STRING | NUMBER)
2395 basic_spec -> file_offset_spec | function_spec | label_spec
2396 file_offset_spec -> opt_file_spec offset_spec
2397 function_spec -> opt_file_spec function_name_spec opt_label_spec
2398 label_spec -> label_name_spec
2400 opt_file_spec -> "" | file_name_spec ':'
2401 opt_label_spec -> "" | ':' label_name_spec
2403 file_name_spec -> STRING
2404 function_name_spec -> STRING
2405 label_name_spec -> STRING
2406 function_name_spec -> STRING
2407 offset_spec -> NUMBER
2411 This may all be followed by several keywords such as "if EXPR",
2414 A comma will terminate parsing.
2416 The function may be an undebuggable function found in minimal symbol table.
2418 If the argument FUNFIRSTLINE is nonzero, we want the first line
2419 of real code inside a function when a function is specified, and it is
2420 not OK to specify a variable or type to get its line number.
2422 DEFAULT_SYMTAB specifies the file to use if none is specified.
2423 It defaults to current_source_symtab.
2424 DEFAULT_LINE specifies the line number to use for relative
2425 line numbers (that start with signs). Defaults to current_source_line.
2426 If CANONICAL is non-NULL, store an array of strings containing the canonical
2427 line specs there if necessary. Currently overloaded member functions and
2428 line numbers or static functions without a filename yield a canonical
2429 line spec. The array and the line spec strings are allocated on the heap,
2430 it is the callers responsibility to free them.
2432 Note that it is possible to return zero for the symtab
2433 if no file is validly specified. Callers must check that.
2434 Also, the line number returned may be invalid. */
2436 /* Parse the linespec in ARG, which must not be nullptr. MATCH_TYPE
2437 indicates how function names should be matched. */
2439 static std::vector
<symtab_and_line
>
2440 parse_linespec (linespec_parser
*parser
, const char *arg
,
2441 symbol_name_match_type match_type
)
2443 gdb_assert (arg
!= nullptr);
2445 struct gdb_exception file_exception
;
2447 /* A special case to start. It has become quite popular for
2448 IDEs to work around bugs in the previous parser by quoting
2449 the entire linespec, so we attempt to deal with this nicely. */
2450 parser
->is_quote_enclosed
= 0;
2451 if (parser
->completion_tracker
== NULL
2452 && !is_ada_operator (arg
)
2454 && strchr (linespec_quote_characters
, *arg
) != NULL
)
2456 const char *end
= skip_quote_char (arg
+ 1, *arg
);
2457 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2459 /* Here's the special case. Skip ARG past the initial
2462 parser
->is_quote_enclosed
= 1;
2466 parser
->lexer
.saved_arg
= arg
;
2467 parser
->lexer
.stream
= arg
;
2468 parser
->completion_word
= arg
;
2469 parser
->complete_what
= linespec_complete_what::FUNCTION
;
2470 parser
->result
.explicit_loc
.func_name_match_type
= match_type
;
2472 /* Initialize the default symtab and line offset. */
2473 initialize_defaults (&parser
->state
.default_symtab
,
2474 &parser
->state
.default_line
);
2476 /* Objective-C shortcut. */
2477 if (parser
->completion_tracker
== NULL
)
2479 std::vector
<symtab_and_line
> values
2480 = decode_objc (&parser
->state
, &parser
->result
, arg
);
2481 if (!values
.empty ())
2486 /* "-"/"+" is either an objc selector, or a number. There's
2487 nothing to complete the latter to, so just let the caller
2488 complete on functions, which finds objc selectors, if there's
2490 if ((arg
[0] == '-' || arg
[0] == '+') && arg
[1] == '\0')
2494 /* Start parsing. */
2496 /* Get the first token. */
2497 linespec_token token
= linespec_lexer_consume_token (parser
);
2499 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2500 if (token
.type
== LSTOKEN_STRING
&& *token
.data
.string
.ptr
== '$')
2502 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2503 if (parser
->completion_tracker
== NULL
)
2504 parser
->result
.file_symtabs
.push_back (nullptr);
2506 /* User specified a convenience variable or history value. */
2507 gdb::unique_xmalloc_ptr
<char> var
= copy_token_string (token
);
2508 parser
->result
.explicit_loc
.line_offset
2509 = linespec_parse_variable (&parser
->state
, var
.get ());
2511 /* If a line_offset wasn't found (VAR is the name of a user
2512 variable/function), then skip to normal symbol processing. */
2513 if (parser
->result
.explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2515 /* Consume this token. */
2516 linespec_lexer_consume_token (parser
);
2518 goto convert_to_sals
;
2521 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
2523 /* Let the default linespec_complete_what::FUNCTION kick in. */
2524 unexpected_linespec_error (parser
);
2526 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2528 parser
->complete_what
= linespec_complete_what::NOTHING
;
2529 unexpected_linespec_error (parser
);
2532 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2533 this token cannot represent a filename. */
2534 token
= linespec_lexer_peek_token (parser
);
2536 if (token
.type
== LSTOKEN_COLON
)
2538 /* Get the current token again and extract the filename. */
2539 token
= linespec_lexer_lex_one (parser
);
2540 gdb::unique_xmalloc_ptr
<char> user_filename
= copy_token_string (token
);
2542 /* Check if the input is a filename. */
2545 parser
->result
.file_symtabs
2546 = symtabs_from_filename (user_filename
.get (),
2547 parser
->state
.search_pspace
);
2549 catch (gdb_exception_error
&ex
)
2551 file_exception
= std::move (ex
);
2554 if (file_exception
.reason
>= 0)
2556 /* Symtabs were found for the file. Record the filename. */
2557 parser
->result
.explicit_loc
.source_filename
= std::move (user_filename
);
2559 /* Get the next token. */
2560 token
= linespec_lexer_consume_token (parser
);
2562 /* This is LSTOKEN_COLON; consume it. */
2563 linespec_lexer_consume_token (parser
);
2567 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2568 parser
->result
.file_symtabs
.push_back (nullptr);
2571 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2572 else if (parser
->completion_tracker
== NULL
2573 && (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2574 && token
.type
!= LSTOKEN_COMMA
))
2576 /* TOKEN is the _next_ token, not the one currently in the parser.
2577 Consuming the token will give the correct error message. */
2578 linespec_lexer_consume_token (parser
);
2579 unexpected_linespec_error (parser
);
2583 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2584 parser
->result
.file_symtabs
.push_back (nullptr);
2587 /* Parse the rest of the linespec. */
2588 linespec_parse_basic (parser
);
2590 if (parser
->completion_tracker
== NULL
2591 && parser
->result
.function_symbols
.empty ()
2592 && parser
->result
.labels
.label_symbols
.empty ()
2593 && parser
->result
.explicit_loc
.line_offset
.sign
== LINE_OFFSET_UNKNOWN
2594 && parser
->result
.minimal_symbols
.empty ())
2596 /* The linespec didn't parse. Re-throw the file exception if
2598 if (file_exception
.reason
< 0)
2599 throw_exception (std::move (file_exception
));
2601 /* Otherwise, the symbol is not found. */
2602 symbol_not_found_error
2603 (parser
->result
.explicit_loc
.function_name
.get (),
2604 parser
->result
.explicit_loc
.source_filename
.get ());
2609 /* Get the last token and record how much of the input was parsed,
2611 token
= linespec_lexer_lex_one (parser
);
2612 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2613 unexpected_linespec_error (parser
);
2614 else if (token
.type
== LSTOKEN_KEYWORD
)
2616 /* Setup the completion word past the keyword. Lexing never
2617 advances past a keyword automatically, so skip it
2619 parser
->completion_word
2620 = skip_spaces (skip_to_space (parser
->lexer
.stream
));
2621 parser
->complete_what
= linespec_complete_what::EXPRESSION
;
2624 /* Convert the data in the parser's result to SALs. */
2625 if (parser
->completion_tracker
== NULL
)
2626 return convert_linespec_to_sals (&parser
->state
, &parser
->result
);
2632 /* A constructor for linespec_state. */
2635 linespec_state_constructor (struct linespec_state
*self
,
2636 int flags
, const struct language_defn
*language
,
2637 struct program_space
*search_pspace
,
2638 struct symtab
*default_symtab
,
2640 struct linespec_result
*canonical
)
2642 memset (self
, 0, sizeof (*self
));
2643 self
->language
= language
;
2644 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2645 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2646 self
->search_pspace
= search_pspace
;
2647 self
->default_symtab
= default_symtab
;
2648 self
->default_line
= default_line
;
2649 self
->canonical
= canonical
;
2650 self
->program_space
= current_program_space
;
2651 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2652 xfree
, xcalloc
, xfree
);
2653 self
->is_linespec
= 0;
2656 /* Initialize a new linespec parser. */
2658 linespec_parser::linespec_parser (int flags
,
2659 const struct language_defn
*language
,
2660 struct program_space
*search_pspace
,
2661 struct symtab
*default_symtab
,
2663 struct linespec_result
*canonical
)
2665 lexer
.current
.type
= LSTOKEN_CONSUMED
;
2666 result
.explicit_loc
.func_name_match_type
2667 = symbol_name_match_type::WILD
;
2668 result
.explicit_loc
.line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2669 linespec_state_constructor (&state
, flags
, language
,
2671 default_symtab
, default_line
, canonical
);
2674 /* A destructor for linespec_state. */
2677 linespec_state_destructor (struct linespec_state
*self
)
2679 htab_delete (self
->addr_set
);
2680 xfree (self
->canonical_names
);
2683 /* Delete a linespec parser. */
2685 linespec_parser::~linespec_parser ()
2687 linespec_state_destructor (&state
);
2690 /* See description in linespec.h. */
2693 linespec_lex_to_end (const char **stringp
)
2695 linespec_token token
;
2698 if (stringp
== NULL
|| *stringp
== NULL
)
2701 linespec_parser
parser (0, current_language
, NULL
, NULL
, 0, NULL
);
2702 parser
.lexer
.saved_arg
= *stringp
;
2703 parser
.lexer
.stream
= orig
= *stringp
;
2707 /* Stop before any comma tokens; we need it to keep it
2708 as the next token in the string. */
2709 token
= linespec_lexer_peek_token (&parser
);
2710 if (token
.type
== LSTOKEN_COMMA
)
2712 token
= linespec_lexer_consume_token (&parser
);
2714 while (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
);
2716 *stringp
+= parser
.lexer
.stream
- orig
;
2719 /* See linespec.h. */
2722 linespec_complete_function (completion_tracker
&tracker
,
2723 const char *function
,
2724 symbol_name_match_type func_match_type
,
2725 const char *source_filename
)
2727 complete_symbol_mode mode
= complete_symbol_mode::LINESPEC
;
2729 if (source_filename
!= NULL
)
2731 collect_file_symbol_completion_matches (tracker
, mode
, func_match_type
,
2732 function
, function
, source_filename
);
2736 collect_symbol_completion_matches (tracker
, mode
, func_match_type
,
2737 function
, function
);
2742 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2743 only meaningful if COMPONENT is FUNCTION. */
2746 complete_linespec_component (linespec_parser
*parser
,
2747 completion_tracker
&tracker
,
2749 linespec_complete_what component
,
2750 const char *source_filename
)
2752 if (component
== linespec_complete_what::KEYWORD
)
2754 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
2756 else if (component
== linespec_complete_what::EXPRESSION
)
2759 = advance_to_expression_complete_word_point (tracker
, text
);
2760 complete_expression (tracker
, text
, word
);
2762 else if (component
== linespec_complete_what::FUNCTION
)
2764 completion_list fn_list
;
2766 symbol_name_match_type match_type
2767 = parser
->result
.explicit_loc
.func_name_match_type
;
2768 linespec_complete_function (tracker
, text
, match_type
, source_filename
);
2769 if (source_filename
== NULL
)
2771 /* Haven't seen a source component, like in "b
2772 file.c:function[TAB]". Maybe this wasn't a function, but
2773 a filename instead, like "b file.[TAB]". */
2774 fn_list
= complete_source_filenames (text
);
2777 /* If we only have a single filename completion, append a ':' for
2778 the user, since that's the only thing that can usefully follow
2780 if (fn_list
.size () == 1 && !tracker
.have_completions ())
2782 char *fn
= fn_list
[0].release ();
2784 /* If we also need to append a quote char, it needs to be
2785 appended before the ':'. Append it now, and make ':' the
2786 new "quote" char. */
2787 if (tracker
.quote_char ())
2789 char quote_char_str
[2] = { (char) tracker
.quote_char () };
2791 fn
= reconcat (fn
, fn
, quote_char_str
, (char *) NULL
);
2792 tracker
.set_quote_char (':');
2795 fn
= reconcat (fn
, fn
, ":", (char *) NULL
);
2796 fn_list
[0].reset (fn
);
2798 /* Tell readline to skip appending a space. */
2799 tracker
.set_suppress_append_ws (true);
2801 tracker
.add_completions (std::move (fn_list
));
2805 /* Helper for linespec_complete_label. Find labels that match
2806 LABEL_NAME in the function symbols listed in the PARSER, and add
2807 them to the tracker. */
2810 complete_label (completion_tracker
&tracker
,
2811 linespec_parser
*parser
,
2812 const char *label_name
)
2814 std::vector
<block_symbol
> label_function_symbols
;
2815 std::vector
<block_symbol
> labels
2816 = find_label_symbols (&parser
->state
,
2817 parser
->result
.function_symbols
,
2818 &label_function_symbols
,
2821 for (const auto &label
: labels
)
2823 char *match
= xstrdup (label
.symbol
->search_name ());
2824 tracker
.add_completion (gdb::unique_xmalloc_ptr
<char> (match
));
2828 /* See linespec.h. */
2831 linespec_complete_label (completion_tracker
&tracker
,
2832 const struct language_defn
*language
,
2833 const char *source_filename
,
2834 const char *function_name
,
2835 symbol_name_match_type func_name_match_type
,
2836 const char *label_name
)
2838 linespec_parser
parser (0, language
, NULL
, NULL
, 0, NULL
);
2840 line_offset unknown_offset
;
2844 convert_explicit_location_spec_to_linespec (&parser
.state
,
2848 func_name_match_type
,
2849 NULL
, unknown_offset
);
2851 catch (const gdb_exception_error
&ex
)
2856 complete_label (tracker
, &parser
, label_name
);
2859 /* See description in linespec.h. */
2862 linespec_complete (completion_tracker
&tracker
, const char *text
,
2863 symbol_name_match_type match_type
)
2865 const char *orig
= text
;
2867 linespec_parser
parser (0, current_language
, NULL
, NULL
, 0, NULL
);
2868 parser
.lexer
.saved_arg
= text
;
2869 parser
.result
.explicit_loc
.func_name_match_type
= match_type
;
2870 parser
.lexer
.stream
= text
;
2872 parser
.completion_tracker
= &tracker
;
2873 parser
.state
.is_linespec
= 1;
2875 /* Parse as much as possible. parser.completion_word will hold
2876 furthest completion point we managed to parse to. */
2879 parse_linespec (&parser
, text
, match_type
);
2881 catch (const gdb_exception_error
&except
)
2885 if (parser
.completion_quote_char
!= '\0'
2886 && parser
.completion_quote_end
!= NULL
2887 && parser
.completion_quote_end
[1] == '\0')
2889 /* If completing a quoted string with the cursor right at
2890 terminating quote char, complete the completion word without
2891 interpretation, so that readline advances the cursor one
2892 whitespace past the quote, even if there's no match. This
2893 makes these cases behave the same:
2895 before: "b function()"
2896 after: "b function() "
2898 before: "b 'function()'"
2899 after: "b 'function()' "
2901 and trusts the user in this case:
2903 before: "b 'not_loaded_function_yet()'"
2904 after: "b 'not_loaded_function_yet()' "
2906 parser
.complete_what
= linespec_complete_what::NOTHING
;
2907 parser
.completion_quote_char
= '\0';
2909 gdb::unique_xmalloc_ptr
<char> text_copy
2910 (xstrdup (parser
.completion_word
));
2911 tracker
.add_completion (std::move (text_copy
));
2914 tracker
.set_quote_char (parser
.completion_quote_char
);
2916 if (parser
.complete_what
== linespec_complete_what::LABEL
)
2918 parser
.complete_what
= linespec_complete_what::NOTHING
;
2920 const char *func_name
= parser
.result
.explicit_loc
.function_name
.get ();
2922 std::vector
<block_symbol
> function_symbols
;
2923 std::vector
<bound_minimal_symbol
> minimal_symbols
;
2924 find_linespec_symbols (&parser
.state
,
2925 parser
.result
.file_symtabs
,
2926 func_name
, match_type
,
2927 &function_symbols
, &minimal_symbols
);
2929 parser
.result
.function_symbols
= std::move (function_symbols
);
2930 parser
.result
.minimal_symbols
= std::move (minimal_symbols
);
2931 complete_label (tracker
, &parser
, parser
.completion_word
);
2933 else if (parser
.complete_what
== linespec_complete_what::FUNCTION
)
2935 /* While parsing/lexing, we didn't know whether the completion
2936 word completes to a unique function/source name already or
2940 "b function() <tab>"
2941 may need to complete either to:
2942 "b function() const"
2944 "b function() if/thread/task"
2948 may need to complete either to:
2949 "b foo template_fun<T>()"
2950 with "foo" being the template function's return type, or to:
2955 may need to complete either to a source file name:
2957 or this, also a filename, but a unique completion:
2959 or to a function name:
2962 Address that by completing assuming source or function, and
2963 seeing if we find a completion that matches exactly the
2964 completion word. If so, then it must be a function (see note
2965 below) and we advance the completion word to the end of input
2966 and switch to KEYWORD completion mode.
2968 Note: if we find a unique completion for a source filename,
2969 then it won't match the completion word, because the LCD will
2970 contain a trailing ':'. And if we're completing at or after
2971 the ':', then complete_linespec_component won't try to
2972 complete on source filenames. */
2974 const char *word
= parser
.completion_word
;
2976 complete_linespec_component
2978 parser
.completion_word
,
2979 linespec_complete_what::FUNCTION
,
2980 parser
.result
.explicit_loc
.source_filename
.get ());
2982 parser
.complete_what
= linespec_complete_what::NOTHING
;
2984 if (tracker
.quote_char ())
2986 /* The function/file name was not close-quoted, so this
2987 can't be a keyword. Note: complete_linespec_component
2988 may have swapped the original quote char for ':' when we
2989 get here, but that still indicates the same. */
2991 else if (!tracker
.have_completions ())
2994 size_t wordlen
= strlen (parser
.completion_word
);
2997 = string_find_incomplete_keyword_at_end (linespec_keywords
,
2998 parser
.completion_word
,
3003 && parser
.completion_word
[wordlen
- 1] == ' '))
3005 parser
.completion_word
+= key_start
;
3006 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3009 else if (tracker
.completes_to_completion_word (word
))
3011 /* Skip the function and complete on keywords. */
3012 parser
.completion_word
+= strlen (word
);
3013 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3014 tracker
.discard_completions ();
3018 tracker
.advance_custom_word_point_by (parser
.completion_word
- orig
);
3020 complete_linespec_component
3022 parser
.completion_word
,
3023 parser
.complete_what
,
3024 parser
.result
.explicit_loc
.source_filename
.get ());
3026 /* If we're past the "filename:function:label:offset" linespec, and
3027 didn't find any match, then assume the user might want to create
3028 a pending breakpoint anyway and offer the keyword
3030 if (!parser
.completion_quote_char
3031 && (parser
.complete_what
== linespec_complete_what::FUNCTION
3032 || parser
.complete_what
== linespec_complete_what::LABEL
3033 || parser
.complete_what
== linespec_complete_what::NOTHING
)
3034 && !tracker
.have_completions ())
3037 = parser
.completion_word
+ strlen (parser
.completion_word
);
3039 if (end
> orig
&& end
[-1] == ' ')
3041 tracker
.advance_custom_word_point_by (end
- parser
.completion_word
);
3043 complete_linespec_component (&parser
, tracker
, end
,
3044 linespec_complete_what::KEYWORD
,
3050 /* A helper function for decode_line_full and decode_line_1 to
3051 turn LOCSPEC into std::vector<symtab_and_line>. */
3053 static std::vector
<symtab_and_line
>
3054 location_spec_to_sals (linespec_parser
*parser
,
3055 const location_spec
*locspec
)
3057 std::vector
<symtab_and_line
> result
;
3059 switch (locspec
->type ())
3061 case LINESPEC_LOCATION_SPEC
:
3063 const linespec_location_spec
*ls
= as_linespec_location_spec (locspec
);
3064 parser
->state
.is_linespec
= 1;
3065 result
= parse_linespec (parser
, ls
->spec_string
.get (),
3070 case ADDRESS_LOCATION_SPEC
:
3072 const address_location_spec
*addr_spec
3073 = as_address_location_spec (locspec
);
3074 const char *addr_string
= addr_spec
->to_string ();
3077 if (addr_string
!= NULL
)
3079 addr
= linespec_expression_to_pc (&addr_string
);
3080 if (parser
->state
.canonical
!= NULL
)
3081 parser
->state
.canonical
->locspec
= locspec
->clone ();
3084 addr
= addr_spec
->address
;
3086 result
= convert_address_location_to_sals (&parser
->state
,
3091 case EXPLICIT_LOCATION_SPEC
:
3093 const explicit_location_spec
*explicit_locspec
3094 = as_explicit_location_spec (locspec
);
3095 result
= convert_explicit_location_spec_to_sals (&parser
->state
,
3101 case PROBE_LOCATION_SPEC
:
3102 /* Probes are handled by their own decoders. */
3103 gdb_assert_not_reached ("attempt to decode probe location");
3107 gdb_assert_not_reached ("unhandled location spec type");
3113 /* See linespec.h. */
3116 decode_line_full (struct location_spec
*locspec
, int flags
,
3117 struct program_space
*search_pspace
,
3118 struct symtab
*default_symtab
,
3119 int default_line
, struct linespec_result
*canonical
,
3120 const char *select_mode
,
3123 std::vector
<const char *> filters
;
3125 gdb_assert (canonical
!= NULL
);
3126 /* The filter only makes sense for 'all'. */
3127 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
3128 gdb_assert (select_mode
== NULL
3129 || select_mode
== multiple_symbols_all
3130 || select_mode
== multiple_symbols_ask
3131 || select_mode
== multiple_symbols_cancel
);
3132 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
3134 linespec_parser
parser (flags
, current_language
,
3135 search_pspace
, default_symtab
,
3136 default_line
, canonical
);
3138 scoped_restore_current_program_space restore_pspace
;
3140 std::vector
<symtab_and_line
> result
= location_spec_to_sals (&parser
,
3142 linespec_state
*state
= &parser
.state
;
3144 if (result
.size () == 0)
3145 throw_error (NOT_SUPPORTED_ERROR
, _("Location %s not available"),
3146 locspec
->to_string ());
3148 gdb_assert (result
.size () == 1 || canonical
->pre_expanded
);
3149 canonical
->pre_expanded
= 1;
3151 /* Arrange for allocated canonical names to be freed. */
3152 std::vector
<gdb::unique_xmalloc_ptr
<char>> hold_names
;
3153 for (int i
= 0; i
< result
.size (); ++i
)
3155 gdb_assert (state
->canonical_names
[i
].suffix
!= NULL
);
3156 hold_names
.emplace_back (state
->canonical_names
[i
].suffix
);
3159 if (select_mode
== NULL
)
3161 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
3162 select_mode
= multiple_symbols_all
;
3164 select_mode
= multiple_symbols_select_mode ();
3167 if (select_mode
== multiple_symbols_all
)
3171 filters
.push_back (filter
);
3172 filter_results (state
, &result
, filters
);
3175 convert_results_to_lsals (state
, &result
);
3178 decode_line_2 (state
, &result
, select_mode
);
3181 /* See linespec.h. */
3183 std::vector
<symtab_and_line
>
3184 decode_line_1 (const location_spec
*locspec
, int flags
,
3185 struct program_space
*search_pspace
,
3186 struct symtab
*default_symtab
,
3189 linespec_parser
parser (flags
, current_language
,
3190 search_pspace
, default_symtab
,
3191 default_line
, NULL
);
3193 scoped_restore_current_program_space restore_pspace
;
3195 return location_spec_to_sals (&parser
, locspec
);
3198 /* See linespec.h. */
3200 std::vector
<symtab_and_line
>
3201 decode_line_with_current_source (const char *string
, int flags
)
3204 error (_("Empty line specification."));
3206 /* We use whatever is set as the current source line. We do not try
3207 and get a default source symtab+line or it will recursively call us! */
3208 symtab_and_line cursal
3209 = get_current_source_symtab_and_line (current_program_space
);
3211 location_spec_up locspec
= string_to_location_spec (&string
,
3213 std::vector
<symtab_and_line
> sals
3214 = decode_line_1 (locspec
.get (), flags
, cursal
.pspace
, cursal
.symtab
,
3218 error (_("Junk at end of line specification: %s"), string
);
3223 /* See linespec.h. */
3225 std::vector
<symtab_and_line
>
3226 decode_line_with_last_displayed (const char *string
, int flags
)
3229 error (_("Empty line specification."));
3231 location_spec_up locspec
= string_to_location_spec (&string
,
3233 std::vector
<symtab_and_line
> sals
3234 = (last_displayed_sal_is_valid ()
3235 ? decode_line_1 (locspec
.get (), flags
, NULL
,
3236 get_last_displayed_symtab (),
3237 get_last_displayed_line ())
3238 : decode_line_1 (locspec
.get (), flags
, NULL
, NULL
, 0));
3241 error (_("Junk at end of line specification: %s"), string
);
3248 /* First, some functions to initialize stuff at the beginning of the
3252 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
3254 if (*default_symtab
== 0)
3256 /* Use whatever we have for the default source line. We don't use
3257 get_current_or_default_symtab_and_line as it can recurse and call
3259 symtab_and_line cursal
3260 = get_current_source_symtab_and_line (current_program_space
);
3262 *default_symtab
= cursal
.symtab
;
3263 *default_line
= cursal
.line
;
3269 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3270 advancing EXP_PTR past any parsed text. */
3273 linespec_expression_to_pc (const char **exp_ptr
)
3275 if (current_program_space
->executing_startup
)
3276 /* The error message doesn't really matter, because this case
3277 should only hit during breakpoint reset. */
3278 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
3279 "program space is in startup"));
3282 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
3287 /* Here's where we recognise an Objective-C Selector. An Objective C
3288 selector may be implemented by more than one class, therefore it
3289 may represent more than one method/function. This gives us a
3290 situation somewhat analogous to C++ overloading. If there's more
3291 than one method that could represent the selector, then use some of
3292 the existing C++ code to let the user choose one. */
3294 static std::vector
<symtab_and_line
>
3295 decode_objc (struct linespec_state
*self
, linespec
*ls
, const char *arg
)
3297 struct collect_info info
;
3298 std::vector
<const char *> symbol_names
;
3299 const char *new_argptr
;
3302 std::vector
<symtab
*> symtabs
;
3303 symtabs
.push_back (nullptr);
3305 info
.file_symtabs
= &symtabs
;
3307 std::vector
<block_symbol
> symbols
;
3308 info
.result
.symbols
= &symbols
;
3309 std::vector
<bound_minimal_symbol
> minimal_symbols
;
3310 info
.result
.minimal_symbols
= &minimal_symbols
;
3312 new_argptr
= find_imps (arg
, &symbol_names
);
3313 if (symbol_names
.empty ())
3316 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
,
3317 SEARCH_FUNCTION_DOMAIN
);
3319 std::vector
<symtab_and_line
> values
;
3320 if (!symbols
.empty () || !minimal_symbols
.empty ())
3324 saved_arg
= (char *) alloca (new_argptr
- arg
+ 1);
3325 memcpy (saved_arg
, arg
, new_argptr
- arg
);
3326 saved_arg
[new_argptr
- arg
] = '\0';
3328 ls
->explicit_loc
.function_name
= make_unique_xstrdup (saved_arg
);
3329 ls
->function_symbols
= std::move (symbols
);
3330 ls
->minimal_symbols
= std::move (minimal_symbols
);
3331 values
= convert_linespec_to_sals (self
, ls
);
3333 if (self
->canonical
)
3338 self
->canonical
->pre_expanded
= 1;
3340 if (ls
->explicit_loc
.source_filename
)
3342 holder
= string_printf ("%s:%s",
3343 ls
->explicit_loc
.source_filename
.get (),
3345 str
= holder
.c_str ();
3350 self
->canonical
->locspec
3351 = new_linespec_location_spec (&str
, symbol_name_match_type::FULL
);
3360 /* A function object that serves as symbol_found_callback_ftype
3361 callback for iterate_over_symbols. This is used by
3362 lookup_prefix_sym to collect type symbols. */
3363 class decode_compound_collector
3366 decode_compound_collector ()
3367 : m_unique_syms (htab_create_alloc (1, htab_hash_pointer
,
3368 htab_eq_pointer
, NULL
,
3373 /* Return all symbols collected. */
3374 std::vector
<block_symbol
> release_symbols ()
3376 return std::move (m_symbols
);
3379 /* Callable as a symbol_found_callback_ftype callback. */
3380 bool operator () (block_symbol
*bsym
);
3383 /* A hash table of all symbols we found. We use this to avoid
3384 adding any symbol more than once. */
3385 htab_up m_unique_syms
;
3387 /* The result vector. */
3388 std::vector
<block_symbol
> m_symbols
;
3392 decode_compound_collector::operator () (block_symbol
*bsym
)
3396 struct symbol
*sym
= bsym
->symbol
;
3398 if (sym
->aclass () != LOC_TYPEDEF
)
3399 return true; /* Continue iterating. */
3402 t
= check_typedef (t
);
3403 if (t
->code () != TYPE_CODE_STRUCT
3404 && t
->code () != TYPE_CODE_UNION
3405 && t
->code () != TYPE_CODE_NAMESPACE
)
3406 return true; /* Continue iterating. */
3408 slot
= htab_find_slot (m_unique_syms
.get (), sym
, INSERT
);
3412 m_symbols
.push_back (*bsym
);
3415 return true; /* Continue iterating. */
3420 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3422 static std::vector
<block_symbol
>
3423 lookup_prefix_sym (struct linespec_state
*state
,
3424 const std::vector
<symtab
*> &file_symtabs
,
3425 const char *class_name
)
3427 decode_compound_collector collector
;
3429 lookup_name_info
lookup_name (class_name
, symbol_name_match_type::FULL
);
3431 for (const auto &elt
: file_symtabs
)
3434 iterate_over_all_matching_symtabs (state
, lookup_name
,
3435 SEARCH_STRUCT_DOMAIN
| SEARCH_VFT
,
3436 NULL
, false, collector
);
3439 /* Program spaces that are executing startup should have
3440 been filtered out earlier. */
3441 program_space
*pspace
= elt
->compunit ()->objfile ()->pspace ();
3443 gdb_assert (!pspace
->executing_startup
);
3444 set_current_program_space (pspace
);
3445 iterate_over_file_blocks (elt
, lookup_name
,
3446 SEARCH_STRUCT_DOMAIN
| SEARCH_VFT
,
3451 return collector
.release_symbols ();
3454 /* A std::sort comparison function for symbols. The resulting order does
3455 not actually matter; we just need to be able to sort them so that
3456 symbols with the same program space end up next to each other. */
3459 compare_symbols (const block_symbol
&a
, const block_symbol
&b
)
3463 uia
= (uintptr_t) a
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
3464 uib
= (uintptr_t) b
.symbol
->symtab ()->compunit ()->objfile ()->pspace ();
3471 uia
= (uintptr_t) a
.symbol
;
3472 uib
= (uintptr_t) b
.symbol
;
3480 /* Like compare_symbols but for minimal symbols. */
3483 compare_msymbols (const bound_minimal_symbol
&a
, const bound_minimal_symbol
&b
)
3487 uia
= (uintptr_t) a
.objfile
->pspace ();
3488 uib
= (uintptr_t) a
.objfile
->pspace ();
3495 uia
= (uintptr_t) a
.minsym
;
3496 uib
= (uintptr_t) b
.minsym
;
3504 /* Look for all the matching instances of each symbol in NAMES. Only
3505 instances from PSPACE are considered; other program spaces are
3506 handled by our caller. If PSPACE is NULL, then all program spaces
3507 are considered. Results are stored into INFO. */
3510 add_all_symbol_names_from_pspace (struct collect_info
*info
,
3511 struct program_space
*pspace
,
3512 const std::vector
<const char *> &names
,
3513 domain_search_flags domain_search_flags
)
3515 for (const char *iter
: names
)
3516 add_matching_symbols_to_info (iter
,
3517 symbol_name_match_type::FULL
,
3518 domain_search_flags
, info
, pspace
);
3522 find_superclass_methods (std::vector
<struct type
*> &&superclasses
,
3523 const char *name
, enum language name_lang
,
3524 std::vector
<const char *> *result_names
)
3526 size_t old_len
= result_names
->size ();
3530 std::vector
<struct type
*> new_supers
;
3532 for (type
*t
: superclasses
)
3533 find_methods (t
, name_lang
, name
, result_names
, &new_supers
);
3535 if (result_names
->size () != old_len
|| new_supers
.empty ())
3538 superclasses
= std::move (new_supers
);
3542 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3543 given by one of the symbols in SYM_CLASSES. Matches are returned
3544 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3547 find_method (struct linespec_state
*self
,
3548 const std::vector
<symtab
*> &file_symtabs
,
3549 const char *class_name
, const char *method_name
,
3550 std::vector
<block_symbol
> *sym_classes
,
3551 std::vector
<block_symbol
> *symbols
,
3552 std::vector
<bound_minimal_symbol
> *minsyms
)
3554 size_t last_result_len
;
3555 std::vector
<struct type
*> superclass_vec
;
3556 std::vector
<const char *> result_names
;
3557 struct collect_info info
;
3559 /* Sort symbols so that symbols with the same program space are next
3561 std::sort (sym_classes
->begin (), sym_classes
->end (),
3565 info
.file_symtabs
= &file_symtabs
;
3566 info
.result
.symbols
= symbols
;
3567 info
.result
.minimal_symbols
= minsyms
;
3569 /* Iterate over all the types, looking for the names of existing
3570 methods matching METHOD_NAME. If we cannot find a direct method in a
3571 given program space, then we consider inherited methods; this is
3572 not ideal (ideal would be to respect C++ hiding rules), but it
3573 seems good enough and is what GDB has historically done. We only
3574 need to collect the names because later we find all symbols with
3575 those names. This loop is written in a somewhat funny way
3576 because we collect data across the program space before deciding
3578 last_result_len
= 0;
3579 for (const auto &elt
: *sym_classes
)
3582 struct program_space
*pspace
;
3583 struct symbol
*sym
= elt
.symbol
;
3584 unsigned int ix
= &elt
- &*sym_classes
->begin ();
3586 /* Program spaces that are executing startup should have
3587 been filtered out earlier. */
3588 pspace
= sym
->symtab ()->compunit ()->objfile ()->pspace ();
3589 gdb_assert (!pspace
->executing_startup
);
3590 set_current_program_space (pspace
);
3591 t
= check_typedef (sym
->type ());
3592 find_methods (t
, sym
->language (),
3593 method_name
, &result_names
, &superclass_vec
);
3595 /* Handle all items from a single program space at once; and be
3596 sure not to miss the last batch. */
3597 if (ix
== sym_classes
->size () - 1
3599 != (sym_classes
->at (ix
+ 1).symbol
->symtab ()
3600 ->compunit ()->objfile ()->pspace ())))
3602 /* If we did not find a direct implementation anywhere in
3603 this program space, consider superclasses. */
3604 if (result_names
.size () == last_result_len
)
3605 find_superclass_methods (std::move (superclass_vec
), method_name
,
3606 sym
->language (), &result_names
);
3608 /* We have a list of candidate symbol names, so now we
3609 iterate over the symbol tables looking for all
3610 matches in this pspace. */
3611 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
,
3612 SEARCH_FUNCTION_DOMAIN
);
3614 superclass_vec
.clear ();
3615 last_result_len
= result_names
.size ();
3619 if (!symbols
->empty () || !minsyms
->empty ())
3622 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3623 and other attempts to locate the symbol will be made. */
3624 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
3631 /* This function object is a callback for iterate_over_symtabs, used
3632 when collecting all matching symtabs. */
3634 class symtab_collector
3638 : m_symtab_table (htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
3643 /* Callable as a symbol_found_callback_ftype callback. */
3644 bool operator () (symtab
*sym
);
3646 /* Return an rvalue reference to the collected symtabs. */
3647 std::vector
<symtab
*> &&release_symtabs ()
3649 return std::move (m_symtabs
);
3653 /* The result vector of symtabs. */
3654 std::vector
<symtab
*> m_symtabs
;
3656 /* This is used to ensure the symtabs are unique. */
3657 htab_up m_symtab_table
;
3661 symtab_collector::operator () (struct symtab
*symtab
)
3665 slot
= htab_find_slot (m_symtab_table
.get (), symtab
, INSERT
);
3669 m_symtabs
.push_back (symtab
);
3677 /* Given a file name, return a list of all matching symtabs. If
3678 SEARCH_PSPACE is not NULL, the search is restricted to just that
3681 static std::vector
<symtab
*>
3682 collect_symtabs_from_filename (const char *file
,
3683 struct program_space
*search_pspace
)
3685 symtab_collector collector
;
3687 /* Find that file's data. */
3688 if (search_pspace
== NULL
)
3690 for (struct program_space
*pspace
: program_spaces
)
3692 if (pspace
->executing_startup
)
3695 set_current_program_space (pspace
);
3696 iterate_over_symtabs (file
, collector
);
3701 set_current_program_space (search_pspace
);
3702 iterate_over_symtabs (file
, collector
);
3705 return collector
.release_symtabs ();
3708 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3709 not NULL, the search is restricted to just that program space. */
3711 static std::vector
<symtab
*>
3712 symtabs_from_filename (const char *filename
,
3713 struct program_space
*search_pspace
)
3715 std::vector
<symtab
*> result
3716 = collect_symtabs_from_filename (filename
, search_pspace
);
3718 if (result
.empty ())
3720 if (!have_full_symbols (current_program_space
)
3721 && !have_partial_symbols (current_program_space
))
3722 throw_error (NOT_FOUND_ERROR
,
3723 _("No symbol table is loaded. "
3724 "Use the \"file\" command."));
3725 source_file_not_found_error (filename
);
3734 symbol_searcher::find_all_symbols (const std::string
&name
,
3735 const struct language_defn
*language
,
3736 domain_search_flags domain_search_flags
,
3737 std::vector
<symtab
*> *search_symtabs
,
3738 struct program_space
*search_pspace
)
3740 symbol_searcher_collect_info info
;
3741 struct linespec_state state
;
3743 memset (&state
, 0, sizeof (state
));
3744 state
.language
= language
;
3745 info
.state
= &state
;
3747 info
.result
.symbols
= &m_symbols
;
3748 info
.result
.minimal_symbols
= &m_minimal_symbols
;
3749 std::vector
<symtab
*> all_symtabs
;
3750 if (search_symtabs
== nullptr)
3752 all_symtabs
.push_back (nullptr);
3753 search_symtabs
= &all_symtabs
;
3755 info
.file_symtabs
= search_symtabs
;
3757 add_matching_symbols_to_info (name
.c_str (), symbol_name_match_type::WILD
,
3758 domain_search_flags
, &info
, search_pspace
);
3761 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3762 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3763 returned in MINSYMS. */
3766 find_function_symbols (struct linespec_state
*state
,
3767 const std::vector
<symtab
*> &file_symtabs
, const char *name
,
3768 symbol_name_match_type name_match_type
,
3769 std::vector
<block_symbol
> *symbols
,
3770 std::vector
<bound_minimal_symbol
> *minsyms
)
3772 struct collect_info info
;
3773 std::vector
<const char *> symbol_names
;
3776 info
.result
.symbols
= symbols
;
3777 info
.result
.minimal_symbols
= minsyms
;
3778 info
.file_symtabs
= &file_symtabs
;
3780 /* Try NAME as an Objective-C selector. */
3781 find_imps (name
, &symbol_names
);
3783 domain_search_flags flags
= SEARCH_FUNCTION_DOMAIN
;
3784 if (state
->list_mode
)
3787 if (!symbol_names
.empty ())
3788 add_all_symbol_names_from_pspace (&info
, state
->search_pspace
,
3789 symbol_names
, flags
);
3791 add_matching_symbols_to_info (name
, name_match_type
, flags
,
3792 &info
, state
->search_pspace
);
3795 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3796 in SYMBOLS and minimal symbols in MINSYMS. */
3799 find_linespec_symbols (struct linespec_state
*state
,
3800 const std::vector
<symtab
*> &file_symtabs
,
3801 const char *lookup_name
,
3802 symbol_name_match_type name_match_type
,
3803 std::vector
<block_symbol
> *symbols
,
3804 std::vector
<bound_minimal_symbol
> *minsyms
)
3806 gdb::unique_xmalloc_ptr
<char> canon
3807 = cp_canonicalize_string_no_typedefs (lookup_name
);
3808 if (canon
!= nullptr)
3809 lookup_name
= canon
.get ();
3811 /* It's important to not call expand_symtabs_matching unnecessarily
3812 as it can really slow things down (by unnecessarily expanding
3813 potentially 1000s of symtabs, which when debugging some apps can
3814 cost 100s of seconds). Avoid this to some extent by *first* calling
3815 find_function_symbols, and only if that doesn't find anything
3816 *then* call find_method. This handles two important cases:
3817 1) break (anonymous namespace)::foo
3818 2) break class::method where method is in class (and not a baseclass) */
3820 find_function_symbols (state
, file_symtabs
, lookup_name
,
3821 name_match_type
, symbols
, minsyms
);
3823 /* If we were unable to locate a symbol of the same name, try dividing
3824 the name into class and method names and searching the class and its
3826 if (symbols
->empty () && minsyms
->empty ())
3828 std::string klass
, method
;
3829 const char *last
, *p
, *scope_op
;
3831 /* See if we can find a scope operator and break this symbol
3832 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3834 p
= find_toplevel_string (lookup_name
, scope_op
);
3840 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
3843 /* If no scope operator was found, there is nothing more we can do;
3844 we already attempted to lookup the entire name as a symbol
3849 /* LOOKUP_NAME points to the class name.
3850 LAST points to the method name. */
3851 klass
= std::string (lookup_name
, last
- lookup_name
);
3853 /* Skip past the scope operator. */
3854 last
+= strlen (scope_op
);
3857 /* Find a list of classes named KLASS. */
3858 std::vector
<block_symbol
> classes
3859 = lookup_prefix_sym (state
, file_symtabs
, klass
.c_str ());
3860 if (!classes
.empty ())
3862 /* Now locate a list of suitable methods named METHOD. */
3865 find_method (state
, file_symtabs
,
3866 klass
.c_str (), method
.c_str (),
3867 &classes
, symbols
, minsyms
);
3870 /* If successful, we're done. If NOT_FOUND_ERROR
3871 was not thrown, rethrow the exception that we did get. */
3872 catch (const gdb_exception_error
&except
)
3874 if (except
.error
!= NOT_FOUND_ERROR
)
3881 /* Helper for find_label_symbols. Find all labels that match name
3882 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3883 Return the actual function symbol in which the label was found in
3884 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3885 interpreted as a label name prefix. Otherwise, only a label named
3886 exactly NAME match. */
3889 find_label_symbols_in_block (const struct block
*block
,
3890 const char *name
, struct symbol
*fn_sym
,
3891 bool completion_mode
,
3892 std::vector
<block_symbol
> *result
,
3893 std::vector
<block_symbol
> *label_funcs_ret
)
3895 if (completion_mode
)
3897 size_t name_len
= strlen (name
);
3899 int (*cmp
) (const char *, const char *, size_t);
3900 cmp
= case_sensitivity
== case_sensitive_on
? strncmp
: strncasecmp
;
3902 for (struct symbol
*sym
: block_iterator_range (block
))
3904 if (sym
->domain () == LABEL_DOMAIN
3905 && cmp (sym
->search_name (), name
, name_len
) == 0)
3907 result
->push_back ({sym
, block
});
3908 label_funcs_ret
->push_back ({fn_sym
, block
});
3914 struct block_symbol label_sym
3915 = lookup_symbol (name
, block
, SEARCH_LABEL_DOMAIN
, 0);
3917 if (label_sym
.symbol
!= NULL
)
3919 result
->push_back (label_sym
);
3920 label_funcs_ret
->push_back ({fn_sym
, block
});
3925 /* Return all labels that match name NAME in FUNCTION_SYMBOLS.
3927 Return the actual function symbol in which the label was found in
3928 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3929 interpreted as a label name prefix. Otherwise, only labels named
3930 exactly NAME match. */
3933 static std::vector
<block_symbol
>
3934 find_label_symbols (struct linespec_state
*self
,
3935 const std::vector
<block_symbol
> &function_symbols
,
3936 std::vector
<block_symbol
> *label_funcs_ret
,
3938 bool completion_mode
)
3940 const struct block
*block
;
3941 struct symbol
*fn_sym
;
3942 std::vector
<block_symbol
> result
;
3944 if (function_symbols
.empty ())
3946 set_current_program_space (self
->program_space
);
3947 block
= get_current_search_block ();
3950 block
&& !block
->function ();
3951 block
= block
->superblock ())
3957 fn_sym
= block
->function ();
3959 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
3960 &result
, label_funcs_ret
);
3964 for (const auto &elt
: function_symbols
)
3966 fn_sym
= elt
.symbol
;
3967 set_current_program_space
3968 (fn_sym
->symtab ()->compunit ()->objfile ()->pspace ());
3969 block
= fn_sym
->value_block ();
3971 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
3972 &result
, label_funcs_ret
);
3981 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3983 static std::vector
<symtab_and_line
>
3984 decode_digits_list_mode (struct linespec_state
*self
,
3986 struct symtab_and_line val
)
3988 gdb_assert (self
->list_mode
);
3990 std::vector
<symtab_and_line
> values
;
3992 for (const auto &elt
: ls
->file_symtabs
)
3994 /* The logic above should ensure this. */
3995 gdb_assert (elt
!= NULL
);
3997 program_space
*pspace
= elt
->compunit ()->objfile ()->pspace ();
3998 set_current_program_space (pspace
);
4000 /* Simplistic search just for the list command. */
4001 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
4002 if (val
.symtab
== NULL
)
4004 val
.pspace
= pspace
;
4006 val
.explicit_line
= true;
4008 add_sal_to_sals (self
, &values
, &val
, NULL
, 0);
4014 /* A helper for create_sals_line_offset that iterates over the symtabs
4015 associated with LS and returns a vector of corresponding symtab_and_line
4018 static std::vector
<symtab_and_line
>
4019 decode_digits_ordinary (struct linespec_state
*self
,
4022 const linetable_entry
**best_entry
)
4024 std::vector
<symtab_and_line
> sals
;
4025 for (const auto &elt
: ls
->file_symtabs
)
4027 std::vector
<CORE_ADDR
> pcs
;
4029 /* The logic above should ensure this. */
4030 gdb_assert (elt
!= NULL
);
4032 program_space
*pspace
= elt
->compunit ()->objfile ()->pspace ();
4033 set_current_program_space (pspace
);
4035 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
4036 for (CORE_ADDR pc
: pcs
)
4038 symtab_and_line sal
;
4039 sal
.pspace
= pspace
;
4042 sal
.explicit_line
= true;
4044 sals
.push_back (std::move (sal
));
4053 /* Return the line offset represented by VARIABLE. */
4055 static struct line_offset
4056 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
4062 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
4065 while (*p
>= '0' && *p
<= '9')
4067 if (!*p
) /* Reached end of token without hitting non-digit. */
4069 /* We have a value history reference. */
4070 struct value
*val_history
;
4072 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
4074 = access_value_history ((variable
[1] == '$') ? -index
: index
);
4075 if (val_history
->type ()->code () != TYPE_CODE_INT
)
4076 error (_("History values used in line "
4077 "specs must have integer values."));
4078 offset
.offset
= value_as_long (val_history
);
4079 offset
.sign
= LINE_OFFSET_NONE
;
4083 /* Not all digits -- may be user variable/function or a
4084 convenience variable. */
4086 struct internalvar
*ivar
;
4088 /* Try it as a convenience variable. If it is not a convenience
4089 variable, return and allow normal symbol lookup to occur. */
4090 ivar
= lookup_only_internalvar (variable
+ 1);
4091 /* If there's no internal variable with that name, let the
4092 offset remain as unknown to allow the name to be looked up
4094 if (ivar
!= nullptr)
4096 /* We found a valid variable name. If it is not an integer,
4098 if (!get_internalvar_integer (ivar
, &valx
))
4099 error (_("Convenience variables used in line "
4100 "specs must have integer values."));
4103 offset
.offset
= valx
;
4104 offset
.sign
= LINE_OFFSET_NONE
;
4113 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
4114 linespec; return the SAL in RESULT. This function should return SALs
4115 matching those from find_function_start_sal, otherwise false
4116 multiple-locations breakpoints could be placed. */
4119 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
4120 struct minimal_symbol
*msymbol
,
4121 std::vector
<symtab_and_line
> *result
)
4123 bool want_start_sal
= false;
4125 CORE_ADDR func_addr
;
4126 bool is_function
= msymbol_is_function (objfile
, msymbol
, &func_addr
);
4130 const char *msym_name
= msymbol
->linkage_name ();
4132 if (msymbol
->type () == mst_text_gnu_ifunc
4133 || msymbol
->type () == mst_data_gnu_ifunc
)
4134 want_start_sal
= gnu_ifunc_resolve_name (msym_name
, &func_addr
);
4136 want_start_sal
= true;
4139 symtab_and_line sal
;
4141 if (is_function
&& want_start_sal
)
4142 sal
= find_function_start_sal (func_addr
, NULL
, self
->funfirstline
);
4145 sal
.objfile
= objfile
;
4146 sal
.msymbol
= msymbol
;
4147 /* Store func_addr, not the minsym's address in case this was an
4148 ifunc that hasn't been resolved yet. */
4152 sal
.pc
= msymbol
->value_address (objfile
);
4153 sal
.pspace
= current_program_space
;
4156 sal
.section
= msymbol
->obj_section (objfile
);
4158 if (maybe_add_address (self
->addr_set
, objfile
->pspace (), sal
.pc
))
4159 add_sal_to_sals (self
, result
, &sal
, msymbol
->natural_name (), 0);
4162 /* Helper for search_minsyms_for_name that adds the symbol to the
4166 add_minsym (struct minimal_symbol
*minsym
, struct objfile
*objfile
,
4167 struct symtab
*symtab
, int list_mode
,
4168 std::vector
<struct bound_minimal_symbol
> *msyms
)
4172 /* We're looking for a label for which we don't have debug
4174 CORE_ADDR func_addr
;
4175 if (msymbol_is_function (objfile
, minsym
, &func_addr
))
4177 symtab_and_line sal
= find_pc_sect_line (func_addr
, NULL
, 0);
4179 if (symtab
!= sal
.symtab
)
4184 /* Exclude data symbols when looking for breakpoint locations. */
4185 if (!list_mode
&& !msymbol_is_function (objfile
, minsym
))
4188 msyms
->emplace_back (minsym
, objfile
);
4192 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4193 is not NULL, the search is restricted to just that program
4196 If SYMTAB is NULL, search all objfiles, otherwise
4197 restrict results to the given SYMTAB. */
4200 search_minsyms_for_name (struct collect_info
*info
,
4201 const lookup_name_info
&name
,
4202 struct program_space
*search_pspace
,
4203 struct symtab
*symtab
)
4205 std::vector
<struct bound_minimal_symbol
> minsyms
;
4209 for (struct program_space
*pspace
: program_spaces
)
4211 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
4213 if (pspace
->executing_startup
)
4216 set_current_program_space (pspace
);
4218 for (objfile
*objfile
: pspace
->objfiles ())
4220 iterate_over_minimal_symbols (objfile
, name
,
4221 [&] (struct minimal_symbol
*msym
)
4223 add_minsym (msym
, objfile
, nullptr,
4224 info
->state
->list_mode
,
4233 program_space
*pspace
= symtab
->compunit ()->objfile ()->pspace ();
4235 if (search_pspace
== NULL
|| pspace
== search_pspace
)
4237 set_current_program_space (pspace
);
4238 iterate_over_minimal_symbols
4239 (symtab
->compunit ()->objfile (), name
,
4240 [&] (struct minimal_symbol
*msym
)
4242 add_minsym (msym
, symtab
->compunit ()->objfile (), symtab
,
4243 info
->state
->list_mode
, &minsyms
);
4249 /* Return true if TYPE is a static symbol. */
4250 auto msymbol_type_is_static
= [] (enum minimal_symbol_type type
)
4263 /* Add minsyms to the result set, but filter out trampoline symbols
4264 if we also found extern symbols with the same name. I.e., don't
4265 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4266 'foo' is the symbol that the plt resolves to. */
4267 for (const bound_minimal_symbol
&item
: minsyms
)
4270 if (item
.minsym
->type () == mst_solib_trampoline
)
4272 for (const bound_minimal_symbol
&item2
: minsyms
)
4274 if (&item2
== &item
)
4277 /* Ignore other trampoline symbols. */
4278 if (item2
.minsym
->type () == mst_solib_trampoline
)
4281 /* Trampoline symbols can only jump to exported
4283 if (msymbol_type_is_static (item2
.minsym
->type ()))
4286 if (strcmp (item
.minsym
->linkage_name (),
4287 item2
.minsym
->linkage_name ()) != 0)
4290 /* Found a global minsym with the same name as the
4291 trampoline. Don't create a location for this
4299 info
->result
.minimal_symbols
->push_back (item
);
4303 /* A helper function to add all symbols matching NAME to INFO. If
4304 PSPACE is not NULL, the search is restricted to just that program
4308 add_matching_symbols_to_info (const char *name
,
4309 symbol_name_match_type name_match_type
,
4310 domain_search_flags domain_search_flags
,
4311 struct collect_info
*info
,
4312 struct program_space
*pspace
)
4314 lookup_name_info
lookup_name (name
, name_match_type
);
4316 for (const auto &elt
: *info
->file_symtabs
)
4320 iterate_over_all_matching_symtabs (info
->state
, lookup_name
,
4321 domain_search_flags
,
4323 [&] (block_symbol
*bsym
)
4324 { return info
->add_symbol (bsym
); });
4325 search_minsyms_for_name (info
, lookup_name
, pspace
, NULL
);
4327 else if (pspace
== NULL
|| pspace
== elt
->compunit ()->objfile ()->pspace ())
4329 int prev_len
= info
->result
.symbols
->size ();
4331 /* Program spaces that are executing startup should have
4332 been filtered out earlier. */
4333 program_space
*elt_pspace
= elt
->compunit ()->objfile ()->pspace ();
4334 gdb_assert (!elt_pspace
->executing_startup
);
4335 set_current_program_space (elt_pspace
);
4336 iterate_over_file_blocks (elt
, lookup_name
, SEARCH_VFT
,
4337 [&] (block_symbol
*bsym
)
4338 { return info
->add_symbol (bsym
); });
4340 /* If no new symbols were found in this iteration and this symtab
4341 is in assembler, we might actually be looking for a label for
4342 which we don't have debug info. Check for a minimal symbol in
4344 if (prev_len
== info
->result
.symbols
->size ()
4345 && elt
->language () == language_asm
)
4346 search_minsyms_for_name (info
, lookup_name
, pspace
, elt
);
4353 /* Now come some functions that are called from multiple places within
4357 symbol_to_sal (struct symtab_and_line
*result
,
4358 int funfirstline
, struct symbol
*sym
)
4360 if (sym
->aclass () == LOC_BLOCK
)
4362 *result
= find_function_start_sal (sym
, funfirstline
);
4367 if (sym
->aclass () == LOC_LABEL
&& sym
->value_address () != 0)
4370 result
->symtab
= sym
->symtab ();
4371 result
->symbol
= sym
;
4372 result
->line
= sym
->line ();
4373 result
->pc
= sym
->value_address ();
4374 result
->pspace
= result
->symtab
->compunit ()->objfile ()->pspace ();
4375 result
->explicit_pc
= 1;
4378 else if (funfirstline
)
4382 else if (sym
->line () != 0)
4384 /* We know its line number. */
4386 result
->symtab
= sym
->symtab ();
4387 result
->symbol
= sym
;
4388 result
->line
= sym
->line ();
4389 result
->pc
= sym
->value_address ();
4390 result
->pspace
= result
->symtab
->compunit ()->objfile ()->pspace ();
4398 linespec_result::~linespec_result ()
4400 for (linespec_sals
&lsal
: lsals
)
4401 xfree (lsal
.canonical
);
4404 /* Return the quote characters permitted by the linespec parser. */
4407 get_gdb_linespec_parser_quote_characters (void)
4409 return linespec_quote_characters
;