1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "completer.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
34 #include "objc-lang.h"
38 #include "mi/mi-cmds.h"
40 #include "arch-utils.h"
42 #include "cli/cli-utils.h"
43 #include "filenames.h"
47 #include "common/function-view.h"
48 #include "common/def-vector.h"
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").
76 E.g., "break func threa<tab>". */
80 /* Typedef for unique_ptrs of vectors of symtabs. */
82 typedef std::unique_ptr
<std::vector
<symtab
*>> symtab_vector_up
;
84 /* An address entry is used to ensure that any given location is only
85 added to the result a single time. It holds an address and the
86 program space from which the address came. */
90 struct program_space
*pspace
;
94 /* A linespec. Elements of this structure are filled in by a parser
95 (either parse_linespec or some other function). The structure is
96 then converted into SALs by convert_linespec_to_sals. */
100 /* An explicit location describing the SaLs. */
101 struct explicit_location explicit_loc
;
103 /* The list of symtabs to search to which to limit the search. May not
104 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
105 filename), FILE_SYMTABS should contain one single NULL member. This
106 will cause the code to use the default symtab. */
107 std::vector
<symtab
*> *file_symtabs
;
109 /* A list of matching function symbols and minimal symbols. Both lists
110 may be NULL (or empty) if no matching symbols were found. */
111 std::vector
<block_symbol
> *function_symbols
;
112 std::vector
<bound_minimal_symbol
> *minimal_symbols
;
114 /* A structure of matching label symbols and the corresponding
115 function symbol in which the label was found. Both may be NULL
116 or both must be non-NULL. */
119 std::vector
<block_symbol
> *label_symbols
;
120 std::vector
<block_symbol
> *function_symbols
;
123 typedef struct linespec
*linespec_p
;
125 /* A canonical linespec represented as a symtab-related string.
127 Each entry represents the "SYMTAB:SUFFIX" linespec string.
128 SYMTAB can be converted for example by symtab_to_fullname or
129 symtab_to_filename_for_display as needed. */
131 struct linespec_canonical_name
133 /* Remaining text part of the linespec string. */
136 /* If NULL then SUFFIX is the whole linespec string. */
137 struct symtab
*symtab
;
140 /* An instance of this is used to keep all state while linespec
141 operates. This instance is passed around as a 'this' pointer to
142 the various implementation methods. */
144 struct linespec_state
146 /* The language in use during linespec processing. */
147 const struct language_defn
*language
;
149 /* The program space as seen when the module was entered. */
150 struct program_space
*program_space
;
152 /* If not NULL, the search is restricted to just this program
154 struct program_space
*search_pspace
;
156 /* The default symtab to use, if no other symtab is specified. */
157 struct symtab
*default_symtab
;
159 /* The default line to use. */
162 /* The 'funfirstline' value that was passed in to decode_line_1 or
166 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
169 /* The 'canonical' value passed to decode_line_full, or NULL. */
170 struct linespec_result
*canonical
;
172 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
173 struct linespec_canonical_name
*canonical_names
;
175 /* This is a set of address_entry objects which is used to prevent
176 duplicate symbols from being entered into the result. */
179 /* Are we building a linespec? */
183 /* This is a helper object that is used when collecting symbols into a
188 /* The linespec object in use. */
189 struct linespec_state
*state
;
191 /* A list of symtabs to which to restrict matches. */
192 std::vector
<symtab
*> *file_symtabs
;
194 /* The result being accumulated. */
197 std::vector
<block_symbol
> *symbols
;
198 std::vector
<bound_minimal_symbol
> *minimal_symbols
;
201 /* Possibly add a symbol to the results. */
202 bool add_symbol (block_symbol
*bsym
);
206 collect_info::add_symbol (block_symbol
*bsym
)
208 /* In list mode, add all matching symbols, regardless of class.
209 This allows the user to type "list a_global_variable". */
210 if (SYMBOL_CLASS (bsym
->symbol
) == LOC_BLOCK
|| this->state
->list_mode
)
211 this->result
.symbols
->push_back (*bsym
);
213 /* Continue iterating. */
224 /* A colon "separator" */
236 /* EOI (end of input) */
242 typedef enum ls_token_type linespec_token_type
;
244 /* List of keywords. This is NULL-terminated so that it can be used
245 as enum completer. */
246 const char * const linespec_keywords
[] = { "if", "thread", "task", NULL
};
247 #define IF_KEYWORD_INDEX 0
249 /* A token of the linespec lexer */
253 /* The type of the token */
254 linespec_token_type type
;
256 /* Data for the token */
259 /* A string, given as a stoken */
260 struct stoken string
;
266 typedef struct ls_token linespec_token
;
268 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
269 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
271 /* An instance of the linespec parser. */
275 /* Lexer internal data */
278 /* Save head of input stream. */
279 const char *saved_arg
;
281 /* Head of the input stream. */
283 #define PARSER_STREAM(P) ((P)->lexer.stream)
285 /* The current token. */
286 linespec_token current
;
289 /* Is the entire linespec quote-enclosed? */
290 int is_quote_enclosed
;
292 /* The state of the parse. */
293 struct linespec_state state
;
294 #define PARSER_STATE(PPTR) (&(PPTR)->state)
296 /* The result of the parse. */
297 struct linespec result
;
298 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
300 /* What the parser believes the current word point should complete
302 linespec_complete_what complete_what
;
304 /* The completion word point. The parser advances this as it skips
305 tokens. At some point the input string will end or parsing will
306 fail, and then we attempt completion at the captured completion
307 word point, interpreting the string at completion_word as
309 const char *completion_word
;
311 /* If the current token was a quoted string, then this is the
312 quoting character (either " or '). */
313 int completion_quote_char
;
315 /* If the current token was a quoted string, then this points at the
316 end of the quoted string. */
317 const char *completion_quote_end
;
319 /* If parsing for completion, then this points at the completion
320 tracker. Otherwise, this is NULL. */
321 struct completion_tracker
*completion_tracker
;
323 typedef struct ls_parser linespec_parser
;
325 /* A convenience macro for accessing the explicit location result of
327 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
329 /* Prototypes for local functions. */
331 static void iterate_over_file_blocks
332 (struct symtab
*symtab
, const lookup_name_info
&name
,
334 gdb::function_view
<symbol_found_callback_ftype
> callback
);
336 static void initialize_defaults (struct symtab
**default_symtab
,
339 CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
341 static std::vector
<symtab_and_line
> decode_objc (struct linespec_state
*self
,
345 static symtab_vector_up symtabs_from_filename
346 (const char *, struct program_space
*pspace
);
348 static std::vector
<block_symbol
> *find_label_symbols
349 (struct linespec_state
*self
, std::vector
<block_symbol
> *function_symbols
,
350 std::vector
<block_symbol
> *label_funcs_ret
, const char *name
,
351 bool completion_mode
= false);
353 static void find_linespec_symbols (struct linespec_state
*self
,
354 std::vector
<symtab
*> *file_symtabs
,
356 symbol_name_match_type name_match_type
,
357 std::vector
<block_symbol
> *symbols
,
358 std::vector
<bound_minimal_symbol
> *minsyms
);
360 static struct line_offset
361 linespec_parse_variable (struct linespec_state
*self
,
362 const char *variable
);
364 static int symbol_to_sal (struct symtab_and_line
*result
,
365 int funfirstline
, struct symbol
*sym
);
367 static void add_matching_symbols_to_info (const char *name
,
368 symbol_name_match_type name_match_type
,
369 enum search_domain search_domain
,
370 struct collect_info
*info
,
371 struct program_space
*pspace
);
373 static void add_all_symbol_names_from_pspace
374 (struct collect_info
*info
, struct program_space
*pspace
,
375 const std::vector
<const char *> &names
, enum search_domain search_domain
);
377 static symtab_vector_up
378 collect_symtabs_from_filename (const char *file
,
379 struct program_space
*pspace
);
381 static std::vector
<symtab_and_line
> decode_digits_ordinary
382 (struct linespec_state
*self
,
385 linetable_entry
**best_entry
);
387 static std::vector
<symtab_and_line
> decode_digits_list_mode
388 (struct linespec_state
*self
,
390 struct symtab_and_line val
);
392 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
393 struct minimal_symbol
*msymbol
,
394 std::vector
<symtab_and_line
> *result
);
396 static bool compare_symbols (const block_symbol
&a
, const block_symbol
&b
);
398 static bool compare_msymbols (const bound_minimal_symbol
&a
,
399 const bound_minimal_symbol
&b
);
401 /* Permitted quote characters for the parser. This is different from the
402 completer's quote characters to allow backward compatibility with the
404 static const char *const linespec_quote_characters
= "\"\'";
406 /* Lexer functions. */
408 /* Lex a number from the input in PARSER. This only supports
411 Return true if input is decimal numbers. Return false if not. */
414 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
416 tokenp
->type
= LSTOKEN_NUMBER
;
417 LS_TOKEN_STOKEN (*tokenp
).length
= 0;
418 LS_TOKEN_STOKEN (*tokenp
).ptr
= PARSER_STREAM (parser
);
420 /* Keep any sign at the start of the stream. */
421 if (*PARSER_STREAM (parser
) == '+' || *PARSER_STREAM (parser
) == '-')
423 ++LS_TOKEN_STOKEN (*tokenp
).length
;
424 ++(PARSER_STREAM (parser
));
427 while (isdigit (*PARSER_STREAM (parser
)))
429 ++LS_TOKEN_STOKEN (*tokenp
).length
;
430 ++(PARSER_STREAM (parser
));
433 /* If the next character in the input buffer is not a space, comma,
434 quote, or colon, this input does not represent a number. */
435 if (*PARSER_STREAM (parser
) != '\0'
436 && !isspace (*PARSER_STREAM (parser
)) && *PARSER_STREAM (parser
) != ','
437 && *PARSER_STREAM (parser
) != ':'
438 && !strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
440 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (*tokenp
).ptr
;
447 /* See linespec.h. */
450 linespec_lexer_lex_keyword (const char *p
)
456 for (i
= 0; linespec_keywords
[i
] != NULL
; ++i
)
458 int len
= strlen (linespec_keywords
[i
]);
460 /* If P begins with one of the keywords and the next
461 character is whitespace, we may have found a keyword.
462 It is only a keyword if it is not followed by another
464 if (strncmp (p
, linespec_keywords
[i
], len
) == 0
469 /* Special case: "if" ALWAYS stops the lexer, since it
470 is not possible to predict what is going to appear in
471 the condition, which can only be parsed after SaLs have
473 if (i
!= IF_KEYWORD_INDEX
)
477 for (j
= 0; linespec_keywords
[j
] != NULL
; ++j
)
479 int nextlen
= strlen (linespec_keywords
[j
]);
481 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
482 && isspace (p
[nextlen
]))
487 return linespec_keywords
[i
];
495 /* See description in linespec.h. */
498 is_ada_operator (const char *string
)
500 const struct ada_opname_map
*mapping
;
502 for (mapping
= ada_opname_table
;
503 mapping
->encoded
!= NULL
504 && !startswith (string
, mapping
->decoded
); ++mapping
)
507 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
510 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
511 the location of QUOTE_CHAR, or NULL if not found. */
514 skip_quote_char (const char *string
, char quote_char
)
516 const char *p
, *last
;
518 p
= last
= find_toplevel_char (string
, quote_char
);
519 while (p
&& *p
!= '\0' && *p
!= ':')
521 p
= find_toplevel_char (p
, quote_char
);
529 /* Make a writable copy of the string given in TOKEN, trimming
530 any trailing whitespace. */
532 static gdb::unique_xmalloc_ptr
<char>
533 copy_token_string (linespec_token token
)
537 if (token
.type
== LSTOKEN_KEYWORD
)
538 return gdb::unique_xmalloc_ptr
<char> (xstrdup (LS_TOKEN_KEYWORD (token
)));
540 str
= LS_TOKEN_STOKEN (token
).ptr
;
541 s
= remove_trailing_whitespace (str
, str
+ LS_TOKEN_STOKEN (token
).length
);
543 return gdb::unique_xmalloc_ptr
<char> (savestring (str
, s
- str
));
546 /* Does P represent the end of a quote-enclosed linespec? */
549 is_closing_quote_enclosed (const char *p
)
551 if (strchr (linespec_quote_characters
, *p
))
553 p
= skip_spaces ((char *) p
);
554 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
557 /* Find the end of the parameter list that starts with *INPUT.
558 This helper function assists with lexing string segments
559 which might contain valid (non-terminating) commas. */
562 find_parameter_list_end (const char *input
)
564 char end_char
, start_char
;
569 if (start_char
== '(')
571 else if (start_char
== '<')
580 if (*p
== start_char
)
582 else if (*p
== end_char
)
596 /* If the [STRING, STRING_LEN) string ends with what looks like a
597 keyword, return the keyword start offset in STRING. Return -1
601 string_find_incomplete_keyword_at_end (const char * const *keywords
,
602 const char *string
, size_t string_len
)
604 const char *end
= string
+ string_len
;
607 while (p
> string
&& *p
!= ' ')
612 size_t len
= end
- p
;
613 for (size_t i
= 0; keywords
[i
] != NULL
; ++i
)
614 if (strncmp (keywords
[i
], p
, len
) == 0)
621 /* Lex a string from the input in PARSER. */
623 static linespec_token
624 linespec_lexer_lex_string (linespec_parser
*parser
)
626 linespec_token token
;
627 const char *start
= PARSER_STREAM (parser
);
629 token
.type
= LSTOKEN_STRING
;
631 /* If the input stream starts with a quote character, skip to the next
632 quote character, regardless of the content. */
633 if (strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
636 char quote_char
= *PARSER_STREAM (parser
);
638 /* Special case: Ada operators. */
639 if (PARSER_STATE (parser
)->language
->la_language
== language_ada
640 && quote_char
== '\"')
642 int len
= is_ada_operator (PARSER_STREAM (parser
));
646 /* The input is an Ada operator. Return the quoted string
648 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
649 LS_TOKEN_STOKEN (token
).length
= len
;
650 PARSER_STREAM (parser
) += len
;
654 /* The input does not represent an Ada operator -- fall through
655 to normal quoted string handling. */
658 /* Skip past the beginning quote. */
659 ++(PARSER_STREAM (parser
));
661 /* Mark the start of the string. */
662 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
664 /* Skip to the ending quote. */
665 end
= skip_quote_char (PARSER_STREAM (parser
), quote_char
);
667 /* This helps the completer mode decide whether we have a
669 parser
->completion_quote_char
= quote_char
;
670 parser
->completion_quote_end
= end
;
672 /* Error if the input did not terminate properly, unless in
676 if (parser
->completion_tracker
== NULL
)
677 error (_("unmatched quote"));
679 /* In completion mode, we'll try to complete the incomplete
681 token
.type
= LSTOKEN_STRING
;
682 while (*PARSER_STREAM (parser
) != '\0')
683 PARSER_STREAM (parser
)++;
684 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 1 - start
;
688 /* Skip over the ending quote and mark the length of the string. */
689 PARSER_STREAM (parser
) = (char *) ++end
;
690 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 2 - start
;
697 /* Otherwise, only identifier characters are permitted.
698 Spaces are the exception. In general, we keep spaces,
699 but only if the next characters in the input do not resolve
700 to one of the keywords.
702 This allows users to forgo quoting CV-qualifiers, template arguments,
703 and similar common language constructs. */
707 if (isspace (*PARSER_STREAM (parser
)))
709 p
= skip_spaces (PARSER_STREAM (parser
));
710 /* When we get here we know we've found something followed by
711 a space (we skip over parens and templates below).
712 So if we find a keyword now, we know it is a keyword and not,
713 say, a function name. */
714 if (linespec_lexer_lex_keyword (p
) != NULL
)
716 LS_TOKEN_STOKEN (token
).ptr
= start
;
717 LS_TOKEN_STOKEN (token
).length
718 = PARSER_STREAM (parser
) - start
;
722 /* Advance past the whitespace. */
723 PARSER_STREAM (parser
) = p
;
726 /* If the next character is EOI or (single) ':', the
727 string is complete; return the token. */
728 if (*PARSER_STREAM (parser
) == 0)
730 LS_TOKEN_STOKEN (token
).ptr
= start
;
731 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
734 else if (PARSER_STREAM (parser
)[0] == ':')
736 /* Do not tokenize the C++ scope operator. */
737 if (PARSER_STREAM (parser
)[1] == ':')
738 ++(PARSER_STREAM (parser
));
740 /* Do not tokenize ABI tags such as "[abi:cxx11]". */
741 else if (PARSER_STREAM (parser
) - start
> 4
742 && startswith (PARSER_STREAM (parser
) - 4, "[abi"))
743 ++(PARSER_STREAM (parser
));
745 /* Do not tokenify if the input length so far is one
746 (i.e, a single-letter drive name) and the next character
747 is a directory separator. This allows Windows-style
748 paths to be recognized as filenames without quoting it. */
749 else if ((PARSER_STREAM (parser
) - start
) != 1
750 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser
)[1]))
752 LS_TOKEN_STOKEN (token
).ptr
= start
;
753 LS_TOKEN_STOKEN (token
).length
754 = PARSER_STREAM (parser
) - start
;
758 /* Special case: permit quote-enclosed linespecs. */
759 else if (parser
->is_quote_enclosed
760 && strchr (linespec_quote_characters
,
761 *PARSER_STREAM (parser
))
762 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
764 LS_TOKEN_STOKEN (token
).ptr
= start
;
765 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
768 /* Because commas may terminate a linespec and appear in
769 the middle of valid string input, special cases for
770 '<' and '(' are necessary. */
771 else if (*PARSER_STREAM (parser
) == '<'
772 || *PARSER_STREAM (parser
) == '(')
774 /* Don't interpret 'operator<' / 'operator<<' as a
775 template parameter list though. */
776 if (*PARSER_STREAM (parser
) == '<'
777 && (PARSER_STATE (parser
)->language
->la_language
779 && (PARSER_STREAM (parser
) - start
) >= CP_OPERATOR_LEN
)
781 const char *p
= PARSER_STREAM (parser
);
783 while (p
> start
&& isspace (p
[-1]))
785 if (p
- start
>= CP_OPERATOR_LEN
)
787 p
-= CP_OPERATOR_LEN
;
788 if (strncmp (p
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0
790 || !(isalnum (p
[-1]) || p
[-1] == '_')))
792 /* This is an operator name. Keep going. */
793 ++(PARSER_STREAM (parser
));
794 if (*PARSER_STREAM (parser
) == '<')
795 ++(PARSER_STREAM (parser
));
801 const char *p
= find_parameter_list_end (PARSER_STREAM (parser
));
802 PARSER_STREAM (parser
) = p
;
804 /* Don't loop around to the normal \0 case above because
805 we don't want to misinterpret a potential keyword at
806 the end of the token when the string isn't
807 "()<>"-balanced. This handles "b
808 function(thread<tab>" in completion mode. */
811 LS_TOKEN_STOKEN (token
).ptr
= start
;
812 LS_TOKEN_STOKEN (token
).length
813 = PARSER_STREAM (parser
) - start
;
819 /* Commas are terminators, but not if they are part of an
821 else if (*PARSER_STREAM (parser
) == ',')
823 if ((PARSER_STATE (parser
)->language
->la_language
825 && (PARSER_STREAM (parser
) - start
) > CP_OPERATOR_LEN
)
827 const char *p
= strstr (start
, CP_OPERATOR_STR
);
829 if (p
!= NULL
&& is_operator_name (p
))
831 /* This is an operator name. Keep going. */
832 ++(PARSER_STREAM (parser
));
837 /* Comma terminates the string. */
838 LS_TOKEN_STOKEN (token
).ptr
= start
;
839 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
843 /* Advance the stream. */
844 ++(PARSER_STREAM (parser
));
851 /* Lex a single linespec token from PARSER. */
853 static linespec_token
854 linespec_lexer_lex_one (linespec_parser
*parser
)
858 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
860 /* Skip any whitespace. */
861 PARSER_STREAM (parser
) = skip_spaces (PARSER_STREAM (parser
));
863 /* Check for a keyword, they end the linespec. */
864 keyword
= linespec_lexer_lex_keyword (PARSER_STREAM (parser
));
867 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
868 LS_TOKEN_KEYWORD (parser
->lexer
.current
) = keyword
;
869 /* We do not advance the stream here intentionally:
870 we would like lexing to stop when a keyword is seen.
872 PARSER_STREAM (parser) += strlen (keyword); */
874 return parser
->lexer
.current
;
877 /* Handle other tokens. */
878 switch (*PARSER_STREAM (parser
))
881 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
885 case '0': case '1': case '2': case '3': case '4':
886 case '5': case '6': case '7': case '8': case '9':
887 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
888 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
892 /* If we have a scope operator, lex the input as a string.
893 Otherwise, return LSTOKEN_COLON. */
894 if (PARSER_STREAM (parser
)[1] == ':')
895 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
898 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
899 ++(PARSER_STREAM (parser
));
903 case '\'': case '\"':
904 /* Special case: permit quote-enclosed linespecs. */
905 if (parser
->is_quote_enclosed
906 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
908 ++(PARSER_STREAM (parser
));
909 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
912 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
916 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
917 LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
918 = PARSER_STREAM (parser
);
919 LS_TOKEN_STOKEN (parser
->lexer
.current
).length
= 1;
920 ++(PARSER_STREAM (parser
));
924 /* If the input is not a number, it must be a string.
925 [Keywords were already considered above.] */
926 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
931 return parser
->lexer
.current
;
934 /* Consume the current token and return the next token in PARSER's
935 input stream. Also advance the completion word for completion
938 static linespec_token
939 linespec_lexer_consume_token (linespec_parser
*parser
)
941 gdb_assert (parser
->lexer
.current
.type
!= LSTOKEN_EOI
);
943 bool advance_word
= (parser
->lexer
.current
.type
!= LSTOKEN_STRING
944 || *PARSER_STREAM (parser
) != '\0');
946 /* If we're moving past a string to some other token, it must be the
947 quote was terminated. */
948 if (parser
->completion_quote_char
)
950 gdb_assert (parser
->lexer
.current
.type
== LSTOKEN_STRING
);
952 /* If the string was the last (non-EOI) token, we're past the
953 quote, but remember that for later. */
954 if (*PARSER_STREAM (parser
) != '\0')
956 parser
->completion_quote_char
= '\0';
957 parser
->completion_quote_end
= NULL
;;
961 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
962 linespec_lexer_lex_one (parser
);
964 if (parser
->lexer
.current
.type
== LSTOKEN_STRING
)
966 /* Advance the completion word past a potential initial
968 parser
->completion_word
= LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
;
970 else if (advance_word
)
972 /* Advance the completion word past any whitespace. */
973 parser
->completion_word
= PARSER_STREAM (parser
);
976 return parser
->lexer
.current
;
979 /* Return the next token without consuming the current token. */
981 static linespec_token
982 linespec_lexer_peek_token (linespec_parser
*parser
)
985 const char *saved_stream
= PARSER_STREAM (parser
);
986 linespec_token saved_token
= parser
->lexer
.current
;
987 int saved_completion_quote_char
= parser
->completion_quote_char
;
988 const char *saved_completion_quote_end
= parser
->completion_quote_end
;
989 const char *saved_completion_word
= parser
->completion_word
;
991 next
= linespec_lexer_consume_token (parser
);
992 PARSER_STREAM (parser
) = saved_stream
;
993 parser
->lexer
.current
= saved_token
;
994 parser
->completion_quote_char
= saved_completion_quote_char
;
995 parser
->completion_quote_end
= saved_completion_quote_end
;
996 parser
->completion_word
= saved_completion_word
;
1000 /* Helper functions. */
1002 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1003 the new sal, if needed. If not NULL, SYMNAME is the name of the
1004 symbol to use when constructing the new canonical name.
1006 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1007 canonical name for the SAL. */
1010 add_sal_to_sals (struct linespec_state
*self
,
1011 std::vector
<symtab_and_line
> *sals
,
1012 struct symtab_and_line
*sal
,
1013 const char *symname
, int literal_canonical
)
1015 sals
->push_back (*sal
);
1017 if (self
->canonical
)
1019 struct linespec_canonical_name
*canonical
;
1021 self
->canonical_names
= XRESIZEVEC (struct linespec_canonical_name
,
1022 self
->canonical_names
,
1024 canonical
= &self
->canonical_names
[sals
->size () - 1];
1025 if (!literal_canonical
&& sal
->symtab
)
1027 symtab_to_fullname (sal
->symtab
);
1029 /* Note that the filter doesn't have to be a valid linespec
1030 input. We only apply the ":LINE" treatment to Ada for
1032 if (symname
!= NULL
&& sal
->line
!= 0
1033 && self
->language
->la_language
== language_ada
)
1034 canonical
->suffix
= xstrprintf ("%s:%d", symname
, sal
->line
);
1035 else if (symname
!= NULL
)
1036 canonical
->suffix
= xstrdup (symname
);
1038 canonical
->suffix
= xstrprintf ("%d", sal
->line
);
1039 canonical
->symtab
= sal
->symtab
;
1043 if (symname
!= NULL
)
1044 canonical
->suffix
= xstrdup (symname
);
1046 canonical
->suffix
= xstrdup ("<unknown>");
1047 canonical
->symtab
= NULL
;
1052 /* A hash function for address_entry. */
1055 hash_address_entry (const void *p
)
1057 const struct address_entry
*aep
= (const struct address_entry
*) p
;
1060 hash
= iterative_hash_object (aep
->pspace
, 0);
1061 return iterative_hash_object (aep
->addr
, hash
);
1064 /* An equality function for address_entry. */
1067 eq_address_entry (const void *a
, const void *b
)
1069 const struct address_entry
*aea
= (const struct address_entry
*) a
;
1070 const struct address_entry
*aeb
= (const struct address_entry
*) b
;
1072 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
1075 /* Check whether the address, represented by PSPACE and ADDR, is
1076 already in the set. If so, return 0. Otherwise, add it and return
1080 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
1082 struct address_entry e
, *p
;
1087 slot
= htab_find_slot (set
, &e
, INSERT
);
1091 p
= XNEW (struct address_entry
);
1092 memcpy (p
, &e
, sizeof (struct address_entry
));
1098 /* A helper that walks over all matching symtabs in all objfiles and
1099 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1100 not NULL, then the search is restricted to just that program
1101 space. If INCLUDE_INLINE is true then symbols representing
1102 inlined instances of functions will be included in the result. */
1105 iterate_over_all_matching_symtabs
1106 (struct linespec_state
*state
,
1107 const lookup_name_info
&lookup_name
,
1108 const domain_enum name_domain
,
1109 enum search_domain search_domain
,
1110 struct program_space
*search_pspace
, bool include_inline
,
1111 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1113 struct objfile
*objfile
;
1114 struct program_space
*pspace
;
1116 ALL_PSPACES (pspace
)
1118 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
1120 if (pspace
->executing_startup
)
1123 set_current_program_space (pspace
);
1125 ALL_OBJFILES (objfile
)
1127 struct compunit_symtab
*cu
;
1130 objfile
->sf
->qf
->expand_symtabs_matching (objfile
,
1136 ALL_OBJFILE_COMPUNITS (objfile
, cu
)
1138 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1140 iterate_over_file_blocks (symtab
, lookup_name
, name_domain
, callback
);
1144 struct block
*block
;
1147 for (i
= FIRST_LOCAL_BLOCK
;
1148 i
< BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab
));
1151 block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), i
);
1152 state
->language
->la_iterate_over_symbols
1153 (block
, lookup_name
, name_domain
, [&] (block_symbol
*bsym
)
1155 /* Restrict calls to CALLBACK to symbols
1156 representing inline symbols only. */
1157 if (SYMBOL_INLINED (bsym
->symbol
))
1158 return callback (bsym
);
1168 /* Returns the block to be used for symbol searches from
1169 the current location. */
1171 static const struct block
*
1172 get_current_search_block (void)
1174 const struct block
*block
;
1175 enum language save_language
;
1177 /* get_selected_block can change the current language when there is
1178 no selected frame yet. */
1179 save_language
= current_language
->la_language
;
1180 block
= get_selected_block (0);
1181 set_language (save_language
);
1186 /* Iterate over static and global blocks. */
1189 iterate_over_file_blocks
1190 (struct symtab
*symtab
, const lookup_name_info
&name
,
1191 domain_enum domain
, gdb::function_view
<symbol_found_callback_ftype
> callback
)
1193 struct block
*block
;
1195 for (block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1197 block
= BLOCK_SUPERBLOCK (block
))
1198 LA_ITERATE_OVER_SYMBOLS (block
, name
, domain
, callback
);
1201 /* A helper for find_method. This finds all methods in type T of
1202 language T_LANG which match NAME. It adds matching symbol names to
1203 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
1206 find_methods (struct type
*t
, enum language t_lang
, const char *name
,
1207 std::vector
<const char *> *result_names
,
1208 std::vector
<struct type
*> *superclasses
)
1211 const char *class_name
= TYPE_NAME (t
);
1213 /* Ignore this class if it doesn't have a name. This is ugly, but
1214 unless we figure out how to get the physname without the name of
1215 the class, then the loop can't do any good. */
1219 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1220 symbol_name_matcher_ftype
*symbol_name_compare
1221 = get_symbol_name_matcher (language_def (t_lang
), lookup_name
);
1223 t
= check_typedef (t
);
1225 /* Loop over each method name. At this level, all overloads of a name
1226 are counted as a single name. There is an inner loop which loops over
1229 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1230 method_counter
>= 0;
1233 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1234 char dem_opname
[64];
1236 if (startswith (method_name
, "__") ||
1237 startswith (method_name
, "op") ||
1238 startswith (method_name
, "type"))
1240 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
1241 method_name
= dem_opname
;
1242 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
1243 method_name
= dem_opname
;
1246 if (symbol_name_compare (method_name
, lookup_name
, NULL
))
1250 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1256 const char *phys_name
;
1258 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1259 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1261 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1262 result_names
->push_back (phys_name
);
1268 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1269 superclasses
->push_back (TYPE_BASECLASS (t
, ibase
));
1272 /* Find an instance of the character C in the string S that is outside
1273 of all parenthesis pairs, single-quoted strings, and double-quoted
1274 strings. Also, ignore the char within a template name, like a ','
1275 within foo<int, int>, while considering C++ operator</operator<<. */
1278 find_toplevel_char (const char *s
, char c
)
1280 int quoted
= 0; /* zero if we're not in quotes;
1281 '"' if we're in a double-quoted string;
1282 '\'' if we're in a single-quoted string. */
1283 int depth
= 0; /* Number of unclosed parens we've seen. */
1286 for (scan
= s
; *scan
; scan
++)
1290 if (*scan
== quoted
)
1292 else if (*scan
== '\\' && *(scan
+ 1))
1295 else if (*scan
== c
&& ! quoted
&& depth
== 0)
1297 else if (*scan
== '"' || *scan
== '\'')
1299 else if (*scan
== '(' || *scan
== '<')
1301 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
1303 else if (*scan
== 'o' && !quoted
&& depth
== 0)
1305 /* Handle C++ operator names. */
1306 if (strncmp (scan
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0)
1308 scan
+= CP_OPERATOR_LEN
;
1311 while (isspace (*scan
))
1322 /* Skip over one less than the appropriate number of
1323 characters: the for loop will skip over the last
1349 /* The string equivalent of find_toplevel_char. Returns a pointer
1350 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1351 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1354 find_toplevel_string (const char *haystack
, const char *needle
)
1356 const char *s
= haystack
;
1360 s
= find_toplevel_char (s
, *needle
);
1364 /* Found first char in HAYSTACK; check rest of string. */
1365 if (startswith (s
, needle
))
1368 /* Didn't find it; loop over HAYSTACK, looking for the next
1369 instance of the first character of NEEDLE. */
1373 while (s
!= NULL
&& *s
!= '\0');
1375 /* NEEDLE was not found in HAYSTACK. */
1379 /* Convert CANONICAL to its string representation using
1380 symtab_to_fullname for SYMTAB. */
1383 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1385 if (canonical
->symtab
== NULL
)
1386 return canonical
->suffix
;
1388 return string_printf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1392 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1393 and store the result in SELF->CANONICAL. */
1396 filter_results (struct linespec_state
*self
,
1397 std::vector
<symtab_and_line
> *result
,
1398 const std::vector
<const char *> &filters
)
1400 for (const char *name
: filters
)
1404 for (size_t j
= 0; j
< result
->size (); ++j
)
1406 const struct linespec_canonical_name
*canonical
;
1408 canonical
= &self
->canonical_names
[j
];
1409 std::string fullform
= canonical_to_fullform (canonical
);
1411 if (name
== fullform
)
1412 lsal
.sals
.push_back ((*result
)[j
]);
1415 if (!lsal
.sals
.empty ())
1417 lsal
.canonical
= xstrdup (name
);
1418 self
->canonical
->lsals
.push_back (std::move (lsal
));
1422 self
->canonical
->pre_expanded
= 0;
1425 /* Store RESULT into SELF->CANONICAL. */
1428 convert_results_to_lsals (struct linespec_state
*self
,
1429 std::vector
<symtab_and_line
> *result
)
1431 struct linespec_sals lsal
;
1433 lsal
.canonical
= NULL
;
1434 lsal
.sals
= std::move (*result
);
1435 self
->canonical
->lsals
.push_back (std::move (lsal
));
1438 /* A structure that contains two string representations of a struct
1439 linespec_canonical_name:
1440 - one where the the symtab's fullname is used;
1441 - one where the filename followed the "set filename-display"
1444 struct decode_line_2_item
1446 decode_line_2_item (std::string
&&fullform_
, std::string
&&displayform_
,
1448 : fullform (std::move (fullform_
)),
1449 displayform (std::move (displayform_
)),
1450 selected (selected_
)
1454 /* The form using symtab_to_fullname. */
1455 std::string fullform
;
1457 /* The form using symtab_to_filename_for_display. */
1458 std::string displayform
;
1460 /* Field is initialized to zero and it is set to one if the user
1461 requested breakpoint for this entry. */
1462 unsigned int selected
: 1;
1465 /* Helper for std::sort to sort decode_line_2_item entries by
1466 DISPLAYFORM and secondarily by FULLFORM. */
1469 decode_line_2_compare_items (const decode_line_2_item
&a
,
1470 const decode_line_2_item
&b
)
1472 if (a
.displayform
!= b
.displayform
)
1473 return a
.displayform
< b
.displayform
;
1474 return a
.fullform
< b
.fullform
;
1477 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1478 will either return normally, throw an exception on multiple
1479 results, or present a menu to the user. On return, the SALS vector
1480 in SELF->CANONICAL is set up properly. */
1483 decode_line_2 (struct linespec_state
*self
,
1484 std::vector
<symtab_and_line
> *result
,
1485 const char *select_mode
)
1490 std::vector
<const char *> filters
;
1491 std::vector
<struct decode_line_2_item
> items
;
1493 gdb_assert (select_mode
!= multiple_symbols_all
);
1494 gdb_assert (self
->canonical
!= NULL
);
1495 gdb_assert (!result
->empty ());
1497 /* Prepare ITEMS array. */
1498 for (i
= 0; i
< result
->size (); ++i
)
1500 const struct linespec_canonical_name
*canonical
;
1501 std::string displayform
;
1503 canonical
= &self
->canonical_names
[i
];
1504 gdb_assert (canonical
->suffix
!= NULL
);
1506 std::string fullform
= canonical_to_fullform (canonical
);
1508 if (canonical
->symtab
== NULL
)
1509 displayform
= canonical
->suffix
;
1512 const char *fn_for_display
;
1514 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1515 displayform
= string_printf ("%s:%s", fn_for_display
,
1519 items
.emplace_back (std::move (fullform
), std::move (displayform
),
1523 /* Sort the list of method names. */
1524 std::sort (items
.begin (), items
.end (), decode_line_2_compare_items
);
1526 /* Remove entries with the same FULLFORM. */
1527 items
.erase (std::unique (items
.begin (), items
.end (),
1528 [] (const struct decode_line_2_item
&a
,
1529 const struct decode_line_2_item
&b
)
1531 return a
.fullform
== b
.fullform
;
1535 if (select_mode
== multiple_symbols_cancel
&& items
.size () > 1)
1536 error (_("canceled because the command is ambiguous\n"
1537 "See set/show multiple-symbol."));
1539 if (select_mode
== multiple_symbols_all
|| items
.size () == 1)
1541 convert_results_to_lsals (self
, result
);
1545 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1546 for (i
= 0; i
< items
.size (); i
++)
1547 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
.c_str ());
1549 prompt
= getenv ("PS2");
1554 args
= command_line_input (prompt
, "overload-choice");
1556 if (args
== 0 || *args
== 0)
1557 error_no_arg (_("one or more choice numbers"));
1559 number_or_range_parser
parser (args
);
1560 while (!parser
.finished ())
1562 int num
= parser
.get_number ();
1565 error (_("canceled"));
1568 /* We intentionally make this result in a single breakpoint,
1569 contrary to what older versions of gdb did. The
1570 rationale is that this lets a user get the
1571 multiple_symbols_all behavior even with the 'ask'
1572 setting; and he can get separate breakpoints by entering
1573 "2-57" at the query. */
1574 convert_results_to_lsals (self
, result
);
1579 if (num
>= items
.size ())
1580 printf_unfiltered (_("No choice number %d.\n"), num
);
1583 struct decode_line_2_item
*item
= &items
[num
];
1585 if (!item
->selected
)
1587 filters
.push_back (item
->fullform
.c_str ());
1592 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1598 filter_results (self
, result
, filters
);
1603 /* The parser of linespec itself. */
1605 /* Throw an appropriate error when SYMBOL is not found (optionally in
1608 static void ATTRIBUTE_NORETURN
1609 symbol_not_found_error (const char *symbol
, const char *filename
)
1614 if (!have_full_symbols ()
1615 && !have_partial_symbols ()
1616 && !have_minimal_symbols ())
1617 throw_error (NOT_FOUND_ERROR
,
1618 _("No symbol table is loaded. Use the \"file\" command."));
1620 /* If SYMBOL starts with '$', the user attempted to either lookup
1621 a function/variable in his code starting with '$' or an internal
1622 variable of that name. Since we do not know which, be concise and
1623 explain both possibilities. */
1627 throw_error (NOT_FOUND_ERROR
,
1628 _("Undefined convenience variable or function \"%s\" "
1629 "not defined in \"%s\"."), symbol
, filename
);
1631 throw_error (NOT_FOUND_ERROR
,
1632 _("Undefined convenience variable or function \"%s\" "
1633 "not defined."), symbol
);
1638 throw_error (NOT_FOUND_ERROR
,
1639 _("Function \"%s\" not defined in \"%s\"."),
1642 throw_error (NOT_FOUND_ERROR
,
1643 _("Function \"%s\" not defined."), symbol
);
1647 /* Throw an appropriate error when an unexpected token is encountered
1650 static void ATTRIBUTE_NORETURN
1651 unexpected_linespec_error (linespec_parser
*parser
)
1653 linespec_token token
;
1654 static const char * token_type_strings
[]
1655 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1657 /* Get the token that generated the error. */
1658 token
= linespec_lexer_lex_one (parser
);
1660 /* Finally, throw the error. */
1661 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1662 || token
.type
== LSTOKEN_KEYWORD
)
1664 gdb::unique_xmalloc_ptr
<char> string
= copy_token_string (token
);
1665 throw_error (GENERIC_ERROR
,
1666 _("malformed linespec error: unexpected %s, \"%s\""),
1667 token_type_strings
[token
.type
], string
.get ());
1670 throw_error (GENERIC_ERROR
,
1671 _("malformed linespec error: unexpected %s"),
1672 token_type_strings
[token
.type
]);
1675 /* Throw an undefined label error. */
1677 static void ATTRIBUTE_NORETURN
1678 undefined_label_error (const char *function
, const char *label
)
1680 if (function
!= NULL
)
1681 throw_error (NOT_FOUND_ERROR
,
1682 _("No label \"%s\" defined in function \"%s\"."),
1685 throw_error (NOT_FOUND_ERROR
,
1686 _("No label \"%s\" defined in current function."),
1690 /* Throw a source file not found error. */
1692 static void ATTRIBUTE_NORETURN
1693 source_file_not_found_error (const char *name
)
1695 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), name
);
1698 /* Unless at EIO, save the current stream position as completion word
1699 point, and consume the next token. */
1701 static linespec_token
1702 save_stream_and_consume_token (linespec_parser
*parser
)
1704 if (linespec_lexer_peek_token (parser
).type
!= LSTOKEN_EOI
)
1705 parser
->completion_word
= PARSER_STREAM (parser
);
1706 return linespec_lexer_consume_token (parser
);
1709 /* See description in linespec.h. */
1712 linespec_parse_line_offset (const char *string
)
1714 const char *start
= string
;
1715 struct line_offset line_offset
= {0, LINE_OFFSET_NONE
};
1719 line_offset
.sign
= LINE_OFFSET_PLUS
;
1722 else if (*string
== '-')
1724 line_offset
.sign
= LINE_OFFSET_MINUS
;
1728 if (*string
!= '\0' && !isdigit (*string
))
1729 error (_("malformed line offset: \"%s\""), start
);
1731 /* Right now, we only allow base 10 for offsets. */
1732 line_offset
.offset
= atoi (string
);
1736 /* In completion mode, if the user is still typing the number, there's
1737 no possible completion to offer. But if there's already input past
1738 the number, setup to expect NEXT. */
1741 set_completion_after_number (linespec_parser
*parser
,
1742 linespec_complete_what next
)
1744 if (*PARSER_STREAM (parser
) == ' ')
1746 parser
->completion_word
= skip_spaces (PARSER_STREAM (parser
) + 1);
1747 parser
->complete_what
= next
;
1751 parser
->completion_word
= PARSER_STREAM (parser
);
1752 parser
->complete_what
= linespec_complete_what::NOTHING
;
1756 /* Parse the basic_spec in PARSER's input. */
1759 linespec_parse_basic (linespec_parser
*parser
)
1761 gdb::unique_xmalloc_ptr
<char> name
;
1762 linespec_token token
;
1763 std::vector
<block_symbol
> symbols
;
1764 std::vector
<block_symbol
> *labels
;
1765 std::vector
<bound_minimal_symbol
> minimal_symbols
;
1767 /* Get the next token. */
1768 token
= linespec_lexer_lex_one (parser
);
1770 /* If it is EOI or KEYWORD, issue an error. */
1771 if (token
.type
== LSTOKEN_KEYWORD
)
1773 parser
->complete_what
= linespec_complete_what::NOTHING
;
1774 unexpected_linespec_error (parser
);
1776 else if (token
.type
== LSTOKEN_EOI
)
1778 unexpected_linespec_error (parser
);
1780 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1781 else if (token
.type
== LSTOKEN_NUMBER
)
1783 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1785 /* Record the line offset and get the next token. */
1786 name
= copy_token_string (token
);
1787 PARSER_EXPLICIT (parser
)->line_offset
1788 = linespec_parse_line_offset (name
.get ());
1790 /* Get the next token. */
1791 token
= linespec_lexer_consume_token (parser
);
1793 /* If the next token is a comma, stop parsing and return. */
1794 if (token
.type
== LSTOKEN_COMMA
)
1796 parser
->complete_what
= linespec_complete_what::NOTHING
;
1800 /* If the next token is anything but EOI or KEYWORD, issue
1802 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1803 unexpected_linespec_error (parser
);
1806 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1809 /* Next token must be LSTOKEN_STRING. */
1810 if (token
.type
!= LSTOKEN_STRING
)
1812 parser
->complete_what
= linespec_complete_what::NOTHING
;
1813 unexpected_linespec_error (parser
);
1816 /* The current token will contain the name of a function, method,
1818 name
= copy_token_string (token
);
1820 if (parser
->completion_tracker
!= NULL
)
1822 /* If the function name ends with a ":", then this may be an
1823 incomplete "::" scope operator instead of a label separator.
1826 which should expand to:
1829 Do a tentative completion assuming the later. If we find
1830 completions, advance the stream past the colon token and make
1831 it part of the function name/token. */
1833 if (!parser
->completion_quote_char
1834 && strcmp (PARSER_STREAM (parser
), ":") == 0)
1836 completion_tracker tmp_tracker
;
1837 const char *source_filename
1838 = PARSER_EXPLICIT (parser
)->source_filename
;
1839 symbol_name_match_type match_type
1840 = PARSER_EXPLICIT (parser
)->func_name_match_type
;
1842 linespec_complete_function (tmp_tracker
,
1843 parser
->completion_word
,
1847 if (tmp_tracker
.have_completions ())
1849 PARSER_STREAM (parser
)++;
1850 LS_TOKEN_STOKEN (token
).length
++;
1852 name
.reset (savestring (parser
->completion_word
,
1853 (PARSER_STREAM (parser
)
1854 - parser
->completion_word
)));
1858 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1862 /* Try looking it up as a function/method. */
1863 find_linespec_symbols (PARSER_STATE (parser
),
1864 PARSER_RESULT (parser
)->file_symtabs
, name
.get (),
1865 PARSER_EXPLICIT (parser
)->func_name_match_type
,
1866 &symbols
, &minimal_symbols
);
1868 if (!symbols
.empty () || !minimal_symbols
.empty ())
1870 PARSER_RESULT (parser
)->function_symbols
1871 = new std::vector
<block_symbol
> (std::move (symbols
));
1872 PARSER_RESULT (parser
)->minimal_symbols
1873 = new std::vector
<bound_minimal_symbol
>
1874 (std::move (minimal_symbols
));
1875 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1879 /* NAME was not a function or a method. So it must be a label
1880 name or user specified variable like "break foo.c:$zippo". */
1881 labels
= find_label_symbols (PARSER_STATE (parser
), NULL
,
1882 &symbols
, name
.get ());
1885 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1886 PARSER_RESULT (parser
)->labels
.function_symbols
1887 = new std::vector
<block_symbol
> (std::move (symbols
));
1888 PARSER_EXPLICIT (parser
)->label_name
= name
.release ();
1890 else if (token
.type
== LSTOKEN_STRING
1891 && *LS_TOKEN_STOKEN (token
).ptr
== '$')
1893 /* User specified a convenience variable or history value. */
1894 PARSER_EXPLICIT (parser
)->line_offset
1895 = linespec_parse_variable (PARSER_STATE (parser
), name
.get ());
1897 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
)
1899 /* The user-specified variable was not valid. Do not
1900 throw an error here. parse_linespec will do it for us. */
1901 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1907 /* The name is also not a label. Abort parsing. Do not throw
1908 an error here. parse_linespec will do it for us. */
1910 /* Save a copy of the name we were trying to lookup. */
1911 PARSER_EXPLICIT (parser
)->function_name
= name
.release ();
1917 int previous_qc
= parser
->completion_quote_char
;
1919 /* Get the next token. */
1920 token
= linespec_lexer_consume_token (parser
);
1922 if (token
.type
== LSTOKEN_EOI
)
1924 if (previous_qc
&& !parser
->completion_quote_char
)
1925 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1927 else if (token
.type
== LSTOKEN_COLON
)
1929 /* User specified a label or a lineno. */
1930 token
= linespec_lexer_consume_token (parser
);
1932 if (token
.type
== LSTOKEN_NUMBER
)
1934 /* User specified an offset. Record the line offset and
1935 get the next token. */
1936 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1938 name
= copy_token_string (token
);
1939 PARSER_EXPLICIT (parser
)->line_offset
1940 = linespec_parse_line_offset (name
.get ());
1942 /* Get the next token. */
1943 token
= linespec_lexer_consume_token (parser
);
1945 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
1947 parser
->complete_what
= linespec_complete_what::LABEL
;
1949 else if (token
.type
== LSTOKEN_STRING
)
1951 parser
->complete_what
= linespec_complete_what::LABEL
;
1953 /* If we have text after the label separated by whitespace
1954 (e.g., "b func():lab i<tab>"), don't consider it part of
1955 the label. In completion mode that should complete to
1956 "if", in normal mode, the 'i' should be treated as
1958 if (parser
->completion_quote_char
== '\0')
1960 const char *ptr
= LS_TOKEN_STOKEN (token
).ptr
;
1961 for (size_t i
= 0; i
< LS_TOKEN_STOKEN (token
).length
; i
++)
1965 LS_TOKEN_STOKEN (token
).length
= i
;
1966 PARSER_STREAM (parser
) = skip_spaces (ptr
+ i
+ 1);
1972 if (parser
->completion_tracker
!= NULL
)
1974 if (PARSER_STREAM (parser
)[-1] == ' ')
1976 parser
->completion_word
= PARSER_STREAM (parser
);
1977 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1982 /* Grab a copy of the label's name and look it up. */
1983 name
= copy_token_string (token
);
1985 = find_label_symbols (PARSER_STATE (parser
),
1986 PARSER_RESULT (parser
)->function_symbols
,
1987 &symbols
, name
.get ());
1991 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1992 PARSER_RESULT (parser
)->labels
.function_symbols
1993 = new std::vector
<block_symbol
> (std::move (symbols
));
1994 PARSER_EXPLICIT (parser
)->label_name
= name
.release ();
1998 /* We don't know what it was, but it isn't a label. */
1999 undefined_label_error
2000 (PARSER_EXPLICIT (parser
)->function_name
, name
.get ());
2005 /* Check for a line offset. */
2006 token
= save_stream_and_consume_token (parser
);
2007 if (token
.type
== LSTOKEN_COLON
)
2009 /* Get the next token. */
2010 token
= linespec_lexer_consume_token (parser
);
2012 /* It must be a line offset. */
2013 if (token
.type
!= LSTOKEN_NUMBER
)
2014 unexpected_linespec_error (parser
);
2016 /* Record the line offset and get the next token. */
2017 name
= copy_token_string (token
);
2019 PARSER_EXPLICIT (parser
)->line_offset
2020 = linespec_parse_line_offset (name
.get ());
2022 /* Get the next token. */
2023 token
= linespec_lexer_consume_token (parser
);
2028 /* Trailing ':' in the input. Issue an error. */
2029 unexpected_linespec_error (parser
);
2034 /* Canonicalize the linespec contained in LS. The result is saved into
2035 STATE->canonical. This function handles both linespec and explicit
2039 canonicalize_linespec (struct linespec_state
*state
, const linespec_p ls
)
2041 struct event_location
*canon
;
2042 struct explicit_location
*explicit_loc
;
2044 /* If canonicalization was not requested, no need to do anything. */
2045 if (!state
->canonical
)
2048 /* Save everything as an explicit location. */
2049 state
->canonical
->location
2050 = new_explicit_location (&ls
->explicit_loc
);
2051 canon
= state
->canonical
->location
.get ();
2052 explicit_loc
= get_explicit_location (canon
);
2054 if (explicit_loc
->label_name
!= NULL
)
2056 state
->canonical
->special_display
= 1;
2058 if (explicit_loc
->function_name
== NULL
)
2060 /* No function was specified, so add the symbol name. */
2061 gdb_assert (!ls
->labels
.function_symbols
->empty ()
2062 && (ls
->labels
.function_symbols
->size () == 1));
2063 block_symbol s
= ls
->labels
.function_symbols
->front ();
2064 explicit_loc
->function_name
2065 = xstrdup (SYMBOL_NATURAL_NAME (s
.symbol
));
2069 /* If this location originally came from a linespec, save a string
2070 representation of it for display and saving to file. */
2071 if (state
->is_linespec
)
2073 char *linespec
= explicit_location_to_linespec (explicit_loc
);
2075 set_event_location_string (canon
, linespec
);
2080 /* Given a line offset in LS, construct the relevant SALs. */
2082 static std::vector
<symtab_and_line
>
2083 create_sals_line_offset (struct linespec_state
*self
,
2086 int use_default
= 0;
2088 /* This is where we need to make sure we have good defaults.
2089 We must guarantee that this section of code is never executed
2090 when we are called with just a function name, since
2091 set_default_source_symtab_and_line uses
2092 select_source_symtab that calls us with such an argument. */
2094 if (ls
->file_symtabs
->size () == 1
2095 && ls
->file_symtabs
->front () == nullptr)
2097 const char *fullname
;
2099 set_current_program_space (self
->program_space
);
2101 /* Make sure we have at least a default source line. */
2102 set_default_source_symtab_and_line ();
2103 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
2104 fullname
= symtab_to_fullname (self
->default_symtab
);
2105 symtab_vector_up r
=
2106 collect_symtabs_from_filename (fullname
, self
->search_pspace
);
2107 ls
->file_symtabs
= r
.release ();
2111 symtab_and_line val
;
2112 val
.line
= ls
->explicit_loc
.line_offset
.offset
;
2113 switch (ls
->explicit_loc
.line_offset
.sign
)
2115 case LINE_OFFSET_PLUS
:
2116 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2119 val
.line
= self
->default_line
+ val
.line
;
2122 case LINE_OFFSET_MINUS
:
2123 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2126 val
.line
= self
->default_line
- val
.line
;
2128 val
.line
= -val
.line
;
2131 case LINE_OFFSET_NONE
:
2132 break; /* No need to adjust val.line. */
2135 std::vector
<symtab_and_line
> values
;
2136 if (self
->list_mode
)
2137 values
= decode_digits_list_mode (self
, ls
, val
);
2140 struct linetable_entry
*best_entry
= NULL
;
2143 std::vector
<symtab_and_line
> intermediate_results
2144 = decode_digits_ordinary (self
, ls
, val
.line
, &best_entry
);
2145 if (intermediate_results
.empty () && best_entry
!= NULL
)
2146 intermediate_results
= decode_digits_ordinary (self
, ls
,
2150 /* For optimized code, the compiler can scatter one source line
2151 across disjoint ranges of PC values, even when no duplicate
2152 functions or inline functions are involved. For example,
2153 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2154 function can result in two PC ranges. In this case, we don't
2155 want to set a breakpoint on the first PC of each range. To filter
2156 such cases, we use containing blocks -- for each PC found
2157 above, we see if there are other PCs that are in the same
2158 block. If yes, the other PCs are filtered out. */
2160 gdb::def_vector
<int> filter (intermediate_results
.size ());
2161 gdb::def_vector
<const block
*> blocks (intermediate_results
.size ());
2163 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2165 set_current_program_space (intermediate_results
[i
].pspace
);
2168 blocks
[i
] = block_for_pc_sect (intermediate_results
[i
].pc
,
2169 intermediate_results
[i
].section
);
2172 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2174 if (blocks
[i
] != NULL
)
2175 for (j
= i
+ 1; j
< intermediate_results
.size (); ++j
)
2177 if (blocks
[j
] == blocks
[i
])
2185 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2188 struct symbol
*sym
= (blocks
[i
]
2189 ? block_containing_function (blocks
[i
])
2192 if (self
->funfirstline
)
2193 skip_prologue_sal (&intermediate_results
[i
]);
2194 intermediate_results
[i
].symbol
= sym
;
2195 add_sal_to_sals (self
, &values
, &intermediate_results
[i
],
2196 sym
? SYMBOL_NATURAL_NAME (sym
) : NULL
, 0);
2200 if (values
.empty ())
2202 if (ls
->explicit_loc
.source_filename
)
2203 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
2204 val
.line
, ls
->explicit_loc
.source_filename
);
2206 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
2213 /* Convert the given ADDRESS into SaLs. */
2215 static std::vector
<symtab_and_line
>
2216 convert_address_location_to_sals (struct linespec_state
*self
,
2219 symtab_and_line sal
= find_pc_line (address
, 0);
2221 sal
.section
= find_pc_overlay (address
);
2222 sal
.explicit_pc
= 1;
2223 sal
.symbol
= find_pc_sect_containing_function (sal
.pc
, sal
.section
);
2225 std::vector
<symtab_and_line
> sals
;
2226 add_sal_to_sals (self
, &sals
, &sal
, core_addr_to_string (address
), 1);
2231 /* Create and return SALs from the linespec LS. */
2233 static std::vector
<symtab_and_line
>
2234 convert_linespec_to_sals (struct linespec_state
*state
, linespec_p ls
)
2236 std::vector
<symtab_and_line
> sals
;
2238 if (ls
->labels
.label_symbols
!= NULL
)
2240 /* We have just a bunch of functions/methods or labels. */
2241 struct symtab_and_line sal
;
2243 for (const auto &sym
: *ls
->labels
.label_symbols
)
2245 struct program_space
*pspace
2246 = SYMTAB_PSPACE (symbol_symtab (sym
.symbol
));
2248 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2249 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2250 add_sal_to_sals (state
, &sals
, &sal
,
2251 SYMBOL_NATURAL_NAME (sym
.symbol
), 0);
2254 else if (ls
->function_symbols
!= NULL
|| ls
->minimal_symbols
!= NULL
)
2256 /* We have just a bunch of functions and/or methods. */
2257 if (ls
->function_symbols
!= NULL
)
2259 /* Sort symbols so that symbols with the same program space are next
2261 std::sort (ls
->function_symbols
->begin (),
2262 ls
->function_symbols
->end (),
2265 for (const auto &sym
: *ls
->function_symbols
)
2267 program_space
*pspace
2268 = SYMTAB_PSPACE (symbol_symtab (sym
.symbol
));
2269 set_current_program_space (pspace
);
2271 /* Don't skip to the first line of the function if we
2272 had found an ifunc minimal symbol for this function,
2273 because that means that this function is an ifunc
2274 resolver with the same name as the ifunc itself. */
2275 bool found_ifunc
= false;
2277 if (state
->funfirstline
2278 && ls
->minimal_symbols
!= NULL
2279 && SYMBOL_CLASS (sym
.symbol
) == LOC_BLOCK
)
2281 const CORE_ADDR addr
2282 = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
.symbol
));
2284 for (const auto &elem
: *ls
->minimal_symbols
)
2286 if (MSYMBOL_TYPE (elem
.minsym
) == mst_text_gnu_ifunc
2287 || MSYMBOL_TYPE (elem
.minsym
) == mst_data_gnu_ifunc
)
2289 CORE_ADDR msym_addr
= BMSYMBOL_VALUE_ADDRESS (elem
);
2290 if (MSYMBOL_TYPE (elem
.minsym
) == mst_data_gnu_ifunc
)
2292 struct gdbarch
*gdbarch
2293 = get_objfile_arch (elem
.objfile
);
2295 = (gdbarch_convert_from_func_ptr_addr
2298 current_top_target ()));
2301 if (msym_addr
== addr
)
2312 symtab_and_line sal
;
2313 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
.symbol
)
2314 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2315 add_sal_to_sals (state
, &sals
, &sal
,
2316 SYMBOL_NATURAL_NAME (sym
.symbol
), 0);
2321 if (ls
->minimal_symbols
!= NULL
)
2323 /* Sort minimal symbols by program space, too */
2324 std::sort (ls
->minimal_symbols
->begin (),
2325 ls
->minimal_symbols
->end (),
2328 for (const auto &elem
: *ls
->minimal_symbols
)
2330 program_space
*pspace
= elem
.objfile
->pspace
;
2331 set_current_program_space (pspace
);
2332 minsym_found (state
, elem
.objfile
, elem
.minsym
, &sals
);
2336 else if (ls
->explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2338 /* Only an offset was specified. */
2339 sals
= create_sals_line_offset (state
, ls
);
2341 /* Make sure we have a filename for canonicalization. */
2342 if (ls
->explicit_loc
.source_filename
== NULL
)
2344 const char *fullname
= symtab_to_fullname (state
->default_symtab
);
2346 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2347 form so that displaying SOURCE_FILENAME can follow the current
2348 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2349 it has been kept for code simplicity only in absolute form. */
2350 ls
->explicit_loc
.source_filename
= xstrdup (fullname
);
2355 /* We haven't found any results... */
2359 canonicalize_linespec (state
, ls
);
2361 if (!sals
.empty () && state
->canonical
!= NULL
)
2362 state
->canonical
->pre_expanded
= 1;
2367 /* Build RESULT from the explicit location components SOURCE_FILENAME,
2368 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2371 convert_explicit_location_to_linespec (struct linespec_state
*self
,
2373 const char *source_filename
,
2374 const char *function_name
,
2375 symbol_name_match_type fname_match_type
,
2376 const char *label_name
,
2377 struct line_offset line_offset
)
2379 std::vector
<block_symbol
> symbols
;
2380 std::vector
<block_symbol
> *labels
;
2381 std::vector
<bound_minimal_symbol
> minimal_symbols
;
2383 result
->explicit_loc
.func_name_match_type
= fname_match_type
;
2385 if (source_filename
!= NULL
)
2389 result
->file_symtabs
2390 = symtabs_from_filename (source_filename
,
2391 self
->search_pspace
).release ();
2393 CATCH (except
, RETURN_MASK_ERROR
)
2395 source_file_not_found_error (source_filename
);
2398 result
->explicit_loc
.source_filename
= xstrdup (source_filename
);
2402 /* A NULL entry means to use the default symtab. */
2403 result
->file_symtabs
->push_back (nullptr);
2406 if (function_name
!= NULL
)
2408 find_linespec_symbols (self
, result
->file_symtabs
,
2409 function_name
, fname_match_type
,
2410 &symbols
, &minimal_symbols
);
2412 if (symbols
.empty () && minimal_symbols
.empty ())
2413 symbol_not_found_error (function_name
,
2414 result
->explicit_loc
.source_filename
);
2416 result
->explicit_loc
.function_name
= xstrdup (function_name
);
2417 result
->function_symbols
2418 = new std::vector
<block_symbol
> (std::move (symbols
));
2419 result
->minimal_symbols
2420 = new std::vector
<bound_minimal_symbol
> (std::move (minimal_symbols
));
2423 if (label_name
!= NULL
)
2425 labels
= find_label_symbols (self
, result
->function_symbols
,
2426 &symbols
, label_name
);
2429 undefined_label_error (result
->explicit_loc
.function_name
,
2432 result
->explicit_loc
.label_name
= xstrdup (label_name
);
2433 result
->labels
.label_symbols
= labels
;
2434 result
->labels
.function_symbols
2435 = new std::vector
<block_symbol
> (std::move (symbols
));
2438 if (line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2439 result
->explicit_loc
.line_offset
= line_offset
;
2442 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2444 static std::vector
<symtab_and_line
>
2445 convert_explicit_location_to_sals (struct linespec_state
*self
,
2447 const struct explicit_location
*explicit_loc
)
2449 convert_explicit_location_to_linespec (self
, result
,
2450 explicit_loc
->source_filename
,
2451 explicit_loc
->function_name
,
2452 explicit_loc
->func_name_match_type
,
2453 explicit_loc
->label_name
,
2454 explicit_loc
->line_offset
);
2455 return convert_linespec_to_sals (self
, result
);
2458 /* Parse a string that specifies a linespec.
2460 The basic grammar of linespecs:
2462 linespec -> var_spec | basic_spec
2463 var_spec -> '$' (STRING | NUMBER)
2465 basic_spec -> file_offset_spec | function_spec | label_spec
2466 file_offset_spec -> opt_file_spec offset_spec
2467 function_spec -> opt_file_spec function_name_spec opt_label_spec
2468 label_spec -> label_name_spec
2470 opt_file_spec -> "" | file_name_spec ':'
2471 opt_label_spec -> "" | ':' label_name_spec
2473 file_name_spec -> STRING
2474 function_name_spec -> STRING
2475 label_name_spec -> STRING
2476 function_name_spec -> STRING
2477 offset_spec -> NUMBER
2481 This may all be followed by several keywords such as "if EXPR",
2484 A comma will terminate parsing.
2486 The function may be an undebuggable function found in minimal symbol table.
2488 If the argument FUNFIRSTLINE is nonzero, we want the first line
2489 of real code inside a function when a function is specified, and it is
2490 not OK to specify a variable or type to get its line number.
2492 DEFAULT_SYMTAB specifies the file to use if none is specified.
2493 It defaults to current_source_symtab.
2494 DEFAULT_LINE specifies the line number to use for relative
2495 line numbers (that start with signs). Defaults to current_source_line.
2496 If CANONICAL is non-NULL, store an array of strings containing the canonical
2497 line specs there if necessary. Currently overloaded member functions and
2498 line numbers or static functions without a filename yield a canonical
2499 line spec. The array and the line spec strings are allocated on the heap,
2500 it is the callers responsibility to free them.
2502 Note that it is possible to return zero for the symtab
2503 if no file is validly specified. Callers must check that.
2504 Also, the line number returned may be invalid. */
2506 /* Parse the linespec in ARG. MATCH_TYPE indicates how function names
2507 should be matched. */
2509 static std::vector
<symtab_and_line
>
2510 parse_linespec (linespec_parser
*parser
, const char *arg
,
2511 symbol_name_match_type match_type
)
2513 linespec_token token
;
2514 struct gdb_exception file_exception
= exception_none
;
2516 /* A special case to start. It has become quite popular for
2517 IDEs to work around bugs in the previous parser by quoting
2518 the entire linespec, so we attempt to deal with this nicely. */
2519 parser
->is_quote_enclosed
= 0;
2520 if (parser
->completion_tracker
== NULL
2521 && !is_ada_operator (arg
)
2522 && strchr (linespec_quote_characters
, *arg
) != NULL
)
2526 end
= skip_quote_char (arg
+ 1, *arg
);
2527 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2529 /* Here's the special case. Skip ARG past the initial
2532 parser
->is_quote_enclosed
= 1;
2536 parser
->lexer
.saved_arg
= arg
;
2537 parser
->lexer
.stream
= arg
;
2538 parser
->completion_word
= arg
;
2539 parser
->complete_what
= linespec_complete_what::FUNCTION
;
2540 PARSER_EXPLICIT (parser
)->func_name_match_type
= match_type
;
2542 /* Initialize the default symtab and line offset. */
2543 initialize_defaults (&PARSER_STATE (parser
)->default_symtab
,
2544 &PARSER_STATE (parser
)->default_line
);
2546 /* Objective-C shortcut. */
2547 if (parser
->completion_tracker
== NULL
)
2549 std::vector
<symtab_and_line
> values
2550 = decode_objc (PARSER_STATE (parser
), PARSER_RESULT (parser
), arg
);
2551 if (!values
.empty ())
2556 /* "-"/"+" is either an objc selector, or a number. There's
2557 nothing to complete the latter to, so just let the caller
2558 complete on functions, which finds objc selectors, if there's
2560 if ((arg
[0] == '-' || arg
[0] == '+') && arg
[1] == '\0')
2564 /* Start parsing. */
2566 /* Get the first token. */
2567 token
= linespec_lexer_consume_token (parser
);
2569 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2570 if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '$')
2572 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2573 if (parser
->completion_tracker
== NULL
)
2574 PARSER_RESULT (parser
)->file_symtabs
->push_back (nullptr);
2576 /* User specified a convenience variable or history value. */
2577 gdb::unique_xmalloc_ptr
<char> var
= copy_token_string (token
);
2578 PARSER_EXPLICIT (parser
)->line_offset
2579 = linespec_parse_variable (PARSER_STATE (parser
), var
.get ());
2581 /* If a line_offset wasn't found (VAR is the name of a user
2582 variable/function), then skip to normal symbol processing. */
2583 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2585 /* Consume this token. */
2586 linespec_lexer_consume_token (parser
);
2588 goto convert_to_sals
;
2591 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
2593 /* Let the default linespec_complete_what::FUNCTION kick in. */
2594 unexpected_linespec_error (parser
);
2596 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2598 parser
->complete_what
= linespec_complete_what::NOTHING
;
2599 unexpected_linespec_error (parser
);
2602 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2603 this token cannot represent a filename. */
2604 token
= linespec_lexer_peek_token (parser
);
2606 if (token
.type
== LSTOKEN_COLON
)
2608 /* Get the current token again and extract the filename. */
2609 token
= linespec_lexer_lex_one (parser
);
2610 gdb::unique_xmalloc_ptr
<char> user_filename
= copy_token_string (token
);
2612 /* Check if the input is a filename. */
2616 = symtabs_from_filename (user_filename
.get (),
2617 PARSER_STATE (parser
)->search_pspace
);
2618 PARSER_RESULT (parser
)->file_symtabs
= r
.release ();
2620 CATCH (ex
, RETURN_MASK_ERROR
)
2622 file_exception
= ex
;
2626 if (file_exception
.reason
>= 0)
2628 /* Symtabs were found for the file. Record the filename. */
2629 PARSER_EXPLICIT (parser
)->source_filename
= user_filename
.release ();
2631 /* Get the next token. */
2632 token
= linespec_lexer_consume_token (parser
);
2634 /* This is LSTOKEN_COLON; consume it. */
2635 linespec_lexer_consume_token (parser
);
2639 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2640 PARSER_RESULT (parser
)->file_symtabs
->push_back (nullptr);
2643 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2644 else if (parser
->completion_tracker
== NULL
2645 && (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2646 && token
.type
!= LSTOKEN_COMMA
))
2648 /* TOKEN is the _next_ token, not the one currently in the parser.
2649 Consuming the token will give the correct error message. */
2650 linespec_lexer_consume_token (parser
);
2651 unexpected_linespec_error (parser
);
2655 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2656 PARSER_RESULT (parser
)->file_symtabs
->push_back (nullptr);
2659 /* Parse the rest of the linespec. */
2660 linespec_parse_basic (parser
);
2662 if (parser
->completion_tracker
== NULL
2663 && PARSER_RESULT (parser
)->function_symbols
== NULL
2664 && PARSER_RESULT (parser
)->labels
.label_symbols
== NULL
2665 && PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
2666 && PARSER_RESULT (parser
)->minimal_symbols
== NULL
)
2668 /* The linespec didn't parse. Re-throw the file exception if
2670 if (file_exception
.reason
< 0)
2671 throw_exception (file_exception
);
2673 /* Otherwise, the symbol is not found. */
2674 symbol_not_found_error (PARSER_EXPLICIT (parser
)->function_name
,
2675 PARSER_EXPLICIT (parser
)->source_filename
);
2680 /* Get the last token and record how much of the input was parsed,
2682 token
= linespec_lexer_lex_one (parser
);
2683 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2684 unexpected_linespec_error (parser
);
2685 else if (token
.type
== LSTOKEN_KEYWORD
)
2687 /* Setup the completion word past the keyword. Lexing never
2688 advances past a keyword automatically, so skip it
2690 parser
->completion_word
2691 = skip_spaces (skip_to_space (PARSER_STREAM (parser
)));
2692 parser
->complete_what
= linespec_complete_what::EXPRESSION
;
2695 /* Convert the data in PARSER_RESULT to SALs. */
2696 if (parser
->completion_tracker
== NULL
)
2697 return convert_linespec_to_sals (PARSER_STATE (parser
),
2698 PARSER_RESULT (parser
));
2704 /* A constructor for linespec_state. */
2707 linespec_state_constructor (struct linespec_state
*self
,
2708 int flags
, const struct language_defn
*language
,
2709 struct program_space
*search_pspace
,
2710 struct symtab
*default_symtab
,
2712 struct linespec_result
*canonical
)
2714 memset (self
, 0, sizeof (*self
));
2715 self
->language
= language
;
2716 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2717 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2718 self
->search_pspace
= search_pspace
;
2719 self
->default_symtab
= default_symtab
;
2720 self
->default_line
= default_line
;
2721 self
->canonical
= canonical
;
2722 self
->program_space
= current_program_space
;
2723 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2724 xfree
, xcalloc
, xfree
);
2725 self
->is_linespec
= 0;
2728 /* Initialize a new linespec parser. */
2731 linespec_parser_new (linespec_parser
*parser
,
2732 int flags
, const struct language_defn
*language
,
2733 struct program_space
*search_pspace
,
2734 struct symtab
*default_symtab
,
2736 struct linespec_result
*canonical
)
2738 memset (parser
, 0, sizeof (linespec_parser
));
2739 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
2740 memset (PARSER_RESULT (parser
), 0, sizeof (struct linespec
));
2741 PARSER_RESULT (parser
)->file_symtabs
= new std::vector
<symtab
*> ();
2742 PARSER_EXPLICIT (parser
)->func_name_match_type
2743 = symbol_name_match_type::WILD
;
2744 PARSER_EXPLICIT (parser
)->line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2745 linespec_state_constructor (PARSER_STATE (parser
), flags
, language
,
2747 default_symtab
, default_line
, canonical
);
2750 /* A destructor for linespec_state. */
2753 linespec_state_destructor (struct linespec_state
*self
)
2755 htab_delete (self
->addr_set
);
2758 /* Delete a linespec parser. */
2761 linespec_parser_delete (void *arg
)
2763 linespec_parser
*parser
= (linespec_parser
*) arg
;
2765 xfree (PARSER_EXPLICIT (parser
)->source_filename
);
2766 xfree (PARSER_EXPLICIT (parser
)->label_name
);
2767 xfree (PARSER_EXPLICIT (parser
)->function_name
);
2769 delete PARSER_RESULT (parser
)->file_symtabs
;
2770 delete PARSER_RESULT (parser
)->function_symbols
;
2771 delete PARSER_RESULT (parser
)->minimal_symbols
;
2772 delete PARSER_RESULT (parser
)->labels
.label_symbols
;
2773 delete PARSER_RESULT (parser
)->labels
.function_symbols
;
2775 linespec_state_destructor (PARSER_STATE (parser
));
2778 /* See description in linespec.h. */
2781 linespec_lex_to_end (const char **stringp
)
2783 linespec_parser parser
;
2784 struct cleanup
*cleanup
;
2785 linespec_token token
;
2788 if (stringp
== NULL
|| *stringp
== NULL
)
2791 linespec_parser_new (&parser
, 0, current_language
, NULL
, NULL
, 0, NULL
);
2792 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2793 parser
.lexer
.saved_arg
= *stringp
;
2794 PARSER_STREAM (&parser
) = orig
= *stringp
;
2798 /* Stop before any comma tokens; we need it to keep it
2799 as the next token in the string. */
2800 token
= linespec_lexer_peek_token (&parser
);
2801 if (token
.type
== LSTOKEN_COMMA
)
2803 token
= linespec_lexer_consume_token (&parser
);
2805 while (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
);
2807 *stringp
+= PARSER_STREAM (&parser
) - orig
;
2808 do_cleanups (cleanup
);
2811 /* See linespec.h. */
2814 linespec_complete_function (completion_tracker
&tracker
,
2815 const char *function
,
2816 symbol_name_match_type func_match_type
,
2817 const char *source_filename
)
2819 complete_symbol_mode mode
= complete_symbol_mode::LINESPEC
;
2821 if (source_filename
!= NULL
)
2823 collect_file_symbol_completion_matches (tracker
, mode
, func_match_type
,
2824 function
, function
, source_filename
);
2828 collect_symbol_completion_matches (tracker
, mode
, func_match_type
,
2829 function
, function
);
2834 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2835 only meaningful if COMPONENT is FUNCTION. */
2838 complete_linespec_component (linespec_parser
*parser
,
2839 completion_tracker
&tracker
,
2841 linespec_complete_what component
,
2842 const char *source_filename
)
2844 if (component
== linespec_complete_what::KEYWORD
)
2846 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
2848 else if (component
== linespec_complete_what::EXPRESSION
)
2851 = advance_to_expression_complete_word_point (tracker
, text
);
2852 complete_expression (tracker
, text
, word
);
2854 else if (component
== linespec_complete_what::FUNCTION
)
2856 completion_list fn_list
;
2858 symbol_name_match_type match_type
2859 = PARSER_EXPLICIT (parser
)->func_name_match_type
;
2860 linespec_complete_function (tracker
, text
, match_type
, source_filename
);
2861 if (source_filename
== NULL
)
2863 /* Haven't seen a source component, like in "b
2864 file.c:function[TAB]". Maybe this wasn't a function, but
2865 a filename instead, like "b file.[TAB]". */
2866 fn_list
= complete_source_filenames (text
);
2869 /* If we only have a single filename completion, append a ':' for
2870 the user, since that's the only thing that can usefully follow
2872 if (fn_list
.size () == 1 && !tracker
.have_completions ())
2874 char *fn
= fn_list
[0].release ();
2876 /* If we also need to append a quote char, it needs to be
2877 appended before the ':'. Append it now, and make ':' the
2878 new "quote" char. */
2879 if (tracker
.quote_char ())
2881 char quote_char_str
[2] = { (char) tracker
.quote_char () };
2883 fn
= reconcat (fn
, fn
, quote_char_str
, (char *) NULL
);
2884 tracker
.set_quote_char (':');
2887 fn
= reconcat (fn
, fn
, ":", (char *) NULL
);
2888 fn_list
[0].reset (fn
);
2890 /* Tell readline to skip appending a space. */
2891 tracker
.set_suppress_append_ws (true);
2893 tracker
.add_completions (std::move (fn_list
));
2897 /* Helper for linespec_complete_label. Find labels that match
2898 LABEL_NAME in the function symbols listed in the PARSER, and add
2899 them to the tracker. */
2902 complete_label (completion_tracker
&tracker
,
2903 linespec_parser
*parser
,
2904 const char *label_name
)
2906 std::vector
<block_symbol
> label_function_symbols
;
2907 std::vector
<block_symbol
> *labels
2908 = find_label_symbols (PARSER_STATE (parser
),
2909 PARSER_RESULT (parser
)->function_symbols
,
2910 &label_function_symbols
,
2913 if (labels
!= nullptr)
2915 for (const auto &label
: *labels
)
2917 char *match
= xstrdup (SYMBOL_SEARCH_NAME (label
.symbol
));
2918 tracker
.add_completion (gdb::unique_xmalloc_ptr
<char> (match
));
2924 /* See linespec.h. */
2927 linespec_complete_label (completion_tracker
&tracker
,
2928 const struct language_defn
*language
,
2929 const char *source_filename
,
2930 const char *function_name
,
2931 symbol_name_match_type func_name_match_type
,
2932 const char *label_name
)
2934 linespec_parser parser
;
2935 struct cleanup
*cleanup
;
2937 linespec_parser_new (&parser
, 0, language
, NULL
, NULL
, 0, NULL
);
2938 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2940 line_offset unknown_offset
= { 0, LINE_OFFSET_UNKNOWN
};
2944 convert_explicit_location_to_linespec (PARSER_STATE (&parser
),
2945 PARSER_RESULT (&parser
),
2948 func_name_match_type
,
2949 NULL
, unknown_offset
);
2951 CATCH (ex
, RETURN_MASK_ERROR
)
2953 do_cleanups (cleanup
);
2958 complete_label (tracker
, &parser
, label_name
);
2960 do_cleanups (cleanup
);
2963 /* See description in linespec.h. */
2966 linespec_complete (completion_tracker
&tracker
, const char *text
,
2967 symbol_name_match_type match_type
)
2969 linespec_parser parser
;
2970 struct cleanup
*cleanup
;
2971 const char *orig
= text
;
2973 linespec_parser_new (&parser
, 0, current_language
, NULL
, NULL
, 0, NULL
);
2974 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2975 parser
.lexer
.saved_arg
= text
;
2976 PARSER_EXPLICIT (&parser
)->func_name_match_type
= match_type
;
2977 PARSER_STREAM (&parser
) = text
;
2979 parser
.completion_tracker
= &tracker
;
2980 PARSER_STATE (&parser
)->is_linespec
= 1;
2982 /* Parse as much as possible. parser.completion_word will hold
2983 furthest completion point we managed to parse to. */
2986 parse_linespec (&parser
, text
, match_type
);
2988 CATCH (except
, RETURN_MASK_ERROR
)
2993 if (parser
.completion_quote_char
!= '\0'
2994 && parser
.completion_quote_end
!= NULL
2995 && parser
.completion_quote_end
[1] == '\0')
2997 /* If completing a quoted string with the cursor right at
2998 terminating quote char, complete the completion word without
2999 interpretation, so that readline advances the cursor one
3000 whitespace past the quote, even if there's no match. This
3001 makes these cases behave the same:
3003 before: "b function()"
3004 after: "b function() "
3006 before: "b 'function()'"
3007 after: "b 'function()' "
3009 and trusts the user in this case:
3011 before: "b 'not_loaded_function_yet()'"
3012 after: "b 'not_loaded_function_yet()' "
3014 parser
.complete_what
= linespec_complete_what::NOTHING
;
3015 parser
.completion_quote_char
= '\0';
3017 gdb::unique_xmalloc_ptr
<char> text_copy
3018 (xstrdup (parser
.completion_word
));
3019 tracker
.add_completion (std::move (text_copy
));
3022 tracker
.set_quote_char (parser
.completion_quote_char
);
3024 if (parser
.complete_what
== linespec_complete_what::LABEL
)
3026 parser
.complete_what
= linespec_complete_what::NOTHING
;
3028 const char *func_name
= PARSER_EXPLICIT (&parser
)->function_name
;
3030 std::vector
<block_symbol
> function_symbols
;
3031 std::vector
<bound_minimal_symbol
> minimal_symbols
;
3032 find_linespec_symbols (PARSER_STATE (&parser
),
3033 PARSER_RESULT (&parser
)->file_symtabs
,
3034 func_name
, match_type
,
3035 &function_symbols
, &minimal_symbols
);
3037 PARSER_RESULT (&parser
)->function_symbols
3038 = new std::vector
<block_symbol
> (std::move (function_symbols
));
3039 PARSER_RESULT (&parser
)->minimal_symbols
3040 = new std::vector
<bound_minimal_symbol
> (std::move (minimal_symbols
));
3042 complete_label (tracker
, &parser
, parser
.completion_word
);
3044 else if (parser
.complete_what
== linespec_complete_what::FUNCTION
)
3046 /* While parsing/lexing, we didn't know whether the completion
3047 word completes to a unique function/source name already or
3051 "b function() <tab>"
3052 may need to complete either to:
3053 "b function() const"
3055 "b function() if/thread/task"
3059 may need to complete either to:
3060 "b foo template_fun<T>()"
3061 with "foo" being the template function's return type, or to:
3066 may need to complete either to a source file name:
3068 or this, also a filename, but a unique completion:
3070 or to a function name:
3073 Address that by completing assuming source or function, and
3074 seeing if we find a completion that matches exactly the
3075 completion word. If so, then it must be a function (see note
3076 below) and we advance the completion word to the end of input
3077 and switch to KEYWORD completion mode.
3079 Note: if we find a unique completion for a source filename,
3080 then it won't match the completion word, because the LCD will
3081 contain a trailing ':'. And if we're completing at or after
3082 the ':', then complete_linespec_component won't try to
3083 complete on source filenames. */
3085 const char *word
= parser
.completion_word
;
3087 complete_linespec_component (&parser
, tracker
,
3088 parser
.completion_word
,
3089 linespec_complete_what::FUNCTION
,
3090 PARSER_EXPLICIT (&parser
)->source_filename
);
3092 parser
.complete_what
= linespec_complete_what::NOTHING
;
3094 if (tracker
.quote_char ())
3096 /* The function/file name was not close-quoted, so this
3097 can't be a keyword. Note: complete_linespec_component
3098 may have swapped the original quote char for ':' when we
3099 get here, but that still indicates the same. */
3101 else if (!tracker
.have_completions ())
3104 size_t wordlen
= strlen (parser
.completion_word
);
3107 = string_find_incomplete_keyword_at_end (linespec_keywords
,
3108 parser
.completion_word
,
3113 && parser
.completion_word
[wordlen
- 1] == ' '))
3115 parser
.completion_word
+= key_start
;
3116 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3119 else if (tracker
.completes_to_completion_word (word
))
3121 /* Skip the function and complete on keywords. */
3122 parser
.completion_word
+= strlen (word
);
3123 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3124 tracker
.discard_completions ();
3128 tracker
.advance_custom_word_point_by (parser
.completion_word
- orig
);
3130 complete_linespec_component (&parser
, tracker
,
3131 parser
.completion_word
,
3132 parser
.complete_what
,
3133 PARSER_EXPLICIT (&parser
)->source_filename
);
3135 /* If we're past the "filename:function:label:offset" linespec, and
3136 didn't find any match, then assume the user might want to create
3137 a pending breakpoint anyway and offer the keyword
3139 if (!parser
.completion_quote_char
3140 && (parser
.complete_what
== linespec_complete_what::FUNCTION
3141 || parser
.complete_what
== linespec_complete_what::LABEL
3142 || parser
.complete_what
== linespec_complete_what::NOTHING
)
3143 && !tracker
.have_completions ())
3146 = parser
.completion_word
+ strlen (parser
.completion_word
);
3148 if (end
> orig
&& end
[-1] == ' ')
3150 tracker
.advance_custom_word_point_by (end
- parser
.completion_word
);
3152 complete_linespec_component (&parser
, tracker
, end
,
3153 linespec_complete_what::KEYWORD
,
3158 do_cleanups (cleanup
);
3161 /* A helper function for decode_line_full and decode_line_1 to
3162 turn LOCATION into std::vector<symtab_and_line>. */
3164 static std::vector
<symtab_and_line
>
3165 event_location_to_sals (linespec_parser
*parser
,
3166 const struct event_location
*location
)
3168 std::vector
<symtab_and_line
> result
;
3170 switch (event_location_type (location
))
3172 case LINESPEC_LOCATION
:
3174 PARSER_STATE (parser
)->is_linespec
= 1;
3177 const linespec_location
*ls
= get_linespec_location (location
);
3178 result
= parse_linespec (parser
,
3179 ls
->spec_string
, ls
->match_type
);
3181 CATCH (except
, RETURN_MASK_ERROR
)
3183 throw_exception (except
);
3189 case ADDRESS_LOCATION
:
3191 const char *addr_string
= get_address_string_location (location
);
3192 CORE_ADDR addr
= get_address_location (location
);
3194 if (addr_string
!= NULL
)
3196 addr
= linespec_expression_to_pc (&addr_string
);
3197 if (PARSER_STATE (parser
)->canonical
!= NULL
)
3198 PARSER_STATE (parser
)->canonical
->location
3199 = copy_event_location (location
);
3202 result
= convert_address_location_to_sals (PARSER_STATE (parser
),
3207 case EXPLICIT_LOCATION
:
3209 const struct explicit_location
*explicit_loc
;
3211 explicit_loc
= get_explicit_location_const (location
);
3212 result
= convert_explicit_location_to_sals (PARSER_STATE (parser
),
3213 PARSER_RESULT (parser
),
3218 case PROBE_LOCATION
:
3219 /* Probes are handled by their own decoders. */
3220 gdb_assert_not_reached ("attempt to decode probe location");
3224 gdb_assert_not_reached ("unhandled event location type");
3230 /* See linespec.h. */
3233 decode_line_full (const struct event_location
*location
, int flags
,
3234 struct program_space
*search_pspace
,
3235 struct symtab
*default_symtab
,
3236 int default_line
, struct linespec_result
*canonical
,
3237 const char *select_mode
,
3240 struct cleanup
*cleanups
;
3241 std::vector
<const char *> filters
;
3242 linespec_parser parser
;
3243 struct linespec_state
*state
;
3245 gdb_assert (canonical
!= NULL
);
3246 /* The filter only makes sense for 'all'. */
3247 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
3248 gdb_assert (select_mode
== NULL
3249 || select_mode
== multiple_symbols_all
3250 || select_mode
== multiple_symbols_ask
3251 || select_mode
== multiple_symbols_cancel
);
3252 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
3254 linespec_parser_new (&parser
, flags
, current_language
,
3255 search_pspace
, default_symtab
,
3256 default_line
, canonical
);
3257 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
3259 scoped_restore_current_program_space restore_pspace
;
3261 std::vector
<symtab_and_line
> result
= event_location_to_sals (&parser
,
3263 state
= PARSER_STATE (&parser
);
3265 gdb_assert (result
.size () == 1 || canonical
->pre_expanded
);
3266 canonical
->pre_expanded
= 1;
3268 /* Arrange for allocated canonical names to be freed. */
3269 if (!result
.empty ())
3273 make_cleanup (xfree
, state
->canonical_names
);
3274 for (i
= 0; i
< result
.size (); ++i
)
3276 gdb_assert (state
->canonical_names
[i
].suffix
!= NULL
);
3277 make_cleanup (xfree
, state
->canonical_names
[i
].suffix
);
3281 if (select_mode
== NULL
)
3283 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
3284 select_mode
= multiple_symbols_all
;
3286 select_mode
= multiple_symbols_select_mode ();
3289 if (select_mode
== multiple_symbols_all
)
3293 filters
.push_back (filter
);
3294 filter_results (state
, &result
, filters
);
3297 convert_results_to_lsals (state
, &result
);
3300 decode_line_2 (state
, &result
, select_mode
);
3302 do_cleanups (cleanups
);
3305 /* See linespec.h. */
3307 std::vector
<symtab_and_line
>
3308 decode_line_1 (const struct event_location
*location
, int flags
,
3309 struct program_space
*search_pspace
,
3310 struct symtab
*default_symtab
,
3313 linespec_parser parser
;
3314 struct cleanup
*cleanups
;
3316 linespec_parser_new (&parser
, flags
, current_language
,
3317 search_pspace
, default_symtab
,
3318 default_line
, NULL
);
3319 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
3321 scoped_restore_current_program_space restore_pspace
;
3323 std::vector
<symtab_and_line
> result
= event_location_to_sals (&parser
,
3326 do_cleanups (cleanups
);
3330 /* See linespec.h. */
3332 std::vector
<symtab_and_line
>
3333 decode_line_with_current_source (const char *string
, int flags
)
3336 error (_("Empty line specification."));
3338 /* We use whatever is set as the current source line. We do not try
3339 and get a default source symtab+line or it will recursively call us! */
3340 symtab_and_line cursal
= get_current_source_symtab_and_line ();
3342 event_location_up location
= string_to_event_location (&string
,
3344 std::vector
<symtab_and_line
> sals
3345 = decode_line_1 (location
.get (), flags
, NULL
, cursal
.symtab
, cursal
.line
);
3348 error (_("Junk at end of line specification: %s"), string
);
3353 /* See linespec.h. */
3355 std::vector
<symtab_and_line
>
3356 decode_line_with_last_displayed (const char *string
, int flags
)
3359 error (_("Empty line specification."));
3361 event_location_up location
= string_to_event_location (&string
,
3363 std::vector
<symtab_and_line
> sals
3364 = (last_displayed_sal_is_valid ()
3365 ? decode_line_1 (location
.get (), flags
, NULL
,
3366 get_last_displayed_symtab (),
3367 get_last_displayed_line ())
3368 : decode_line_1 (location
.get (), flags
, NULL
,
3369 (struct symtab
*) NULL
, 0));
3372 error (_("Junk at end of line specification: %s"), string
);
3379 /* First, some functions to initialize stuff at the beggining of the
3383 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
3385 if (*default_symtab
== 0)
3387 /* Use whatever we have for the default source line. We don't use
3388 get_current_or_default_symtab_and_line as it can recurse and call
3390 struct symtab_and_line cursal
=
3391 get_current_source_symtab_and_line ();
3393 *default_symtab
= cursal
.symtab
;
3394 *default_line
= cursal
.line
;
3400 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3401 advancing EXP_PTR past any parsed text. */
3404 linespec_expression_to_pc (const char **exp_ptr
)
3406 if (current_program_space
->executing_startup
)
3407 /* The error message doesn't really matter, because this case
3408 should only hit during breakpoint reset. */
3409 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
3410 "program space is in startup"));
3413 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
3418 /* Here's where we recognise an Objective-C Selector. An Objective C
3419 selector may be implemented by more than one class, therefore it
3420 may represent more than one method/function. This gives us a
3421 situation somewhat analogous to C++ overloading. If there's more
3422 than one method that could represent the selector, then use some of
3423 the existing C++ code to let the user choose one. */
3425 static std::vector
<symtab_and_line
>
3426 decode_objc (struct linespec_state
*self
, linespec_p ls
, const char *arg
)
3428 struct collect_info info
;
3429 std::vector
<const char *> symbol_names
;
3430 const char *new_argptr
;
3433 std::vector
<symtab
*> symtabs
;
3434 symtabs
.push_back (nullptr);
3436 info
.file_symtabs
= &symtabs
;
3438 std::vector
<block_symbol
> symbols
;
3439 info
.result
.symbols
= &symbols
;
3440 std::vector
<bound_minimal_symbol
> minimal_symbols
;
3441 info
.result
.minimal_symbols
= &minimal_symbols
;
3443 new_argptr
= find_imps (arg
, &symbol_names
);
3444 if (symbol_names
.empty ())
3447 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
,
3450 std::vector
<symtab_and_line
> values
;
3451 if (!symbols
.empty () || !minimal_symbols
.empty ())
3455 saved_arg
= (char *) alloca (new_argptr
- arg
+ 1);
3456 memcpy (saved_arg
, arg
, new_argptr
- arg
);
3457 saved_arg
[new_argptr
- arg
] = '\0';
3459 ls
->explicit_loc
.function_name
= xstrdup (saved_arg
);
3460 ls
->function_symbols
3461 = new std::vector
<block_symbol
> (std::move (symbols
));
3463 = new std::vector
<bound_minimal_symbol
> (std::move (minimal_symbols
));
3464 values
= convert_linespec_to_sals (self
, ls
);
3466 if (self
->canonical
)
3471 self
->canonical
->pre_expanded
= 1;
3473 if (ls
->explicit_loc
.source_filename
)
3475 holder
= string_printf ("%s:%s",
3476 ls
->explicit_loc
.source_filename
,
3478 str
= holder
.c_str ();
3483 self
->canonical
->location
3484 = new_linespec_location (&str
, symbol_name_match_type::FULL
);
3493 /* A function object that serves as symbol_found_callback_ftype
3494 callback for iterate_over_symbols. This is used by
3495 lookup_prefix_sym to collect type symbols. */
3496 class decode_compound_collector
3499 decode_compound_collector ()
3501 m_unique_syms
= htab_create_alloc (1, htab_hash_pointer
,
3502 htab_eq_pointer
, NULL
,
3506 ~decode_compound_collector ()
3508 if (m_unique_syms
!= NULL
)
3509 htab_delete (m_unique_syms
);
3512 /* Return all symbols collected. */
3513 std::vector
<block_symbol
> release_symbols ()
3515 return std::move (m_symbols
);
3518 /* Callable as a symbol_found_callback_ftype callback. */
3519 bool operator () (block_symbol
*bsym
);
3522 /* A hash table of all symbols we found. We use this to avoid
3523 adding any symbol more than once. */
3524 htab_t m_unique_syms
;
3526 /* The result vector. */
3527 std::vector
<block_symbol
> m_symbols
;
3531 decode_compound_collector::operator () (block_symbol
*bsym
)
3535 struct symbol
*sym
= bsym
->symbol
;
3537 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
3538 return true; /* Continue iterating. */
3540 t
= SYMBOL_TYPE (sym
);
3541 t
= check_typedef (t
);
3542 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
3543 && TYPE_CODE (t
) != TYPE_CODE_UNION
3544 && TYPE_CODE (t
) != TYPE_CODE_NAMESPACE
)
3545 return true; /* Continue iterating. */
3547 slot
= htab_find_slot (m_unique_syms
, sym
, INSERT
);
3551 m_symbols
.push_back (*bsym
);
3554 return true; /* Continue iterating. */
3559 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3561 static std::vector
<block_symbol
>
3562 lookup_prefix_sym (struct linespec_state
*state
,
3563 std::vector
<symtab
*> *file_symtabs
,
3564 const char *class_name
)
3566 decode_compound_collector collector
;
3568 lookup_name_info
lookup_name (class_name
, symbol_name_match_type::FULL
);
3570 for (const auto &elt
: *file_symtabs
)
3574 iterate_over_all_matching_symtabs (state
, lookup_name
,
3575 STRUCT_DOMAIN
, ALL_DOMAIN
,
3576 NULL
, false, collector
);
3577 iterate_over_all_matching_symtabs (state
, lookup_name
,
3578 VAR_DOMAIN
, ALL_DOMAIN
,
3579 NULL
, false, collector
);
3583 /* Program spaces that are executing startup should have
3584 been filtered out earlier. */
3585 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
3586 set_current_program_space (SYMTAB_PSPACE (elt
));
3587 iterate_over_file_blocks (elt
, lookup_name
, STRUCT_DOMAIN
, collector
);
3588 iterate_over_file_blocks (elt
, lookup_name
, VAR_DOMAIN
, collector
);
3592 return collector
.release_symbols ();
3595 /* A std::sort comparison function for symbols. The resulting order does
3596 not actually matter; we just need to be able to sort them so that
3597 symbols with the same program space end up next to each other. */
3600 compare_symbols (const block_symbol
&a
, const block_symbol
&b
)
3604 uia
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (a
.symbol
));
3605 uib
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (b
.symbol
));
3612 uia
= (uintptr_t) a
.symbol
;
3613 uib
= (uintptr_t) b
.symbol
;
3621 /* Like compare_symbols but for minimal symbols. */
3624 compare_msymbols (const bound_minimal_symbol
&a
, const bound_minimal_symbol
&b
)
3628 uia
= (uintptr_t) a
.objfile
->pspace
;
3629 uib
= (uintptr_t) a
.objfile
->pspace
;
3636 uia
= (uintptr_t) a
.minsym
;
3637 uib
= (uintptr_t) b
.minsym
;
3645 /* Look for all the matching instances of each symbol in NAMES. Only
3646 instances from PSPACE are considered; other program spaces are
3647 handled by our caller. If PSPACE is NULL, then all program spaces
3648 are considered. Results are stored into INFO. */
3651 add_all_symbol_names_from_pspace (struct collect_info
*info
,
3652 struct program_space
*pspace
,
3653 const std::vector
<const char *> &names
,
3654 enum search_domain search_domain
)
3656 for (const char *iter
: names
)
3657 add_matching_symbols_to_info (iter
,
3658 symbol_name_match_type::FULL
,
3659 search_domain
, info
, pspace
);
3663 find_superclass_methods (std::vector
<struct type
*> &&superclasses
,
3664 const char *name
, enum language name_lang
,
3665 std::vector
<const char *> *result_names
)
3667 size_t old_len
= result_names
->size ();
3671 std::vector
<struct type
*> new_supers
;
3673 for (type
*t
: superclasses
)
3674 find_methods (t
, name_lang
, name
, result_names
, &new_supers
);
3676 if (result_names
->size () != old_len
|| new_supers
.empty ())
3679 superclasses
= std::move (new_supers
);
3683 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3684 given by one of the symbols in SYM_CLASSES. Matches are returned
3685 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3688 find_method (struct linespec_state
*self
, std::vector
<symtab
*> *file_symtabs
,
3689 const char *class_name
, const char *method_name
,
3690 std::vector
<block_symbol
> *sym_classes
,
3691 std::vector
<block_symbol
> *symbols
,
3692 std::vector
<bound_minimal_symbol
> *minsyms
)
3694 size_t last_result_len
;
3695 std::vector
<struct type
*> superclass_vec
;
3696 std::vector
<const char *> result_names
;
3697 struct collect_info info
;
3699 /* Sort symbols so that symbols with the same program space are next
3701 std::sort (sym_classes
->begin (), sym_classes
->end (),
3705 info
.file_symtabs
= file_symtabs
;
3706 info
.result
.symbols
= symbols
;
3707 info
.result
.minimal_symbols
= minsyms
;
3709 /* Iterate over all the types, looking for the names of existing
3710 methods matching METHOD_NAME. If we cannot find a direct method in a
3711 given program space, then we consider inherited methods; this is
3712 not ideal (ideal would be to respect C++ hiding rules), but it
3713 seems good enough and is what GDB has historically done. We only
3714 need to collect the names because later we find all symbols with
3715 those names. This loop is written in a somewhat funny way
3716 because we collect data across the program space before deciding
3718 last_result_len
= 0;
3719 unsigned int ix
= 0;
3720 for (const auto &elt
: *sym_classes
)
3723 struct program_space
*pspace
;
3724 struct symbol
*sym
= elt
.symbol
;
3726 /* Program spaces that are executing startup should have
3727 been filtered out earlier. */
3728 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
3729 gdb_assert (!pspace
->executing_startup
);
3730 set_current_program_space (pspace
);
3731 t
= check_typedef (SYMBOL_TYPE (sym
));
3732 find_methods (t
, SYMBOL_LANGUAGE (sym
),
3733 method_name
, &result_names
, &superclass_vec
);
3735 /* Handle all items from a single program space at once; and be
3736 sure not to miss the last batch. */
3737 if (ix
== sym_classes
->size () - 1
3739 != SYMTAB_PSPACE (symbol_symtab (sym_classes
->at (ix
+ 1).symbol
))))
3741 /* If we did not find a direct implementation anywhere in
3742 this program space, consider superclasses. */
3743 if (result_names
.size () == last_result_len
)
3744 find_superclass_methods (std::move (superclass_vec
), method_name
,
3745 SYMBOL_LANGUAGE (sym
), &result_names
);
3747 /* We have a list of candidate symbol names, so now we
3748 iterate over the symbol tables looking for all
3749 matches in this pspace. */
3750 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
,
3753 superclass_vec
.clear ();
3754 last_result_len
= result_names
.size ();
3759 if (!symbols
->empty () || !minsyms
->empty ())
3762 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3763 and other attempts to locate the symbol will be made. */
3764 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
3771 /* This function object is a callback for iterate_over_symtabs, used
3772 when collecting all matching symtabs. */
3774 class symtab_collector
3778 : m_symtabs (new std::vector
<symtab
*> ())
3780 m_symtab_table
= htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
3784 ~symtab_collector ()
3786 if (m_symtab_table
!= NULL
)
3787 htab_delete (m_symtab_table
);
3790 /* Callable as a symbol_found_callback_ftype callback. */
3791 bool operator () (symtab
*sym
);
3793 /* Releases ownership of the collected symtabs and returns them. */
3794 symtab_vector_up
release_symtabs ()
3796 return std::move (m_symtabs
);
3800 /* The result vector of symtabs. */
3801 symtab_vector_up m_symtabs
;
3803 /* This is used to ensure the symtabs are unique. */
3804 htab_t m_symtab_table
;
3808 symtab_collector::operator () (struct symtab
*symtab
)
3812 slot
= htab_find_slot (m_symtab_table
, symtab
, INSERT
);
3816 m_symtabs
->push_back (symtab
);
3824 /* Given a file name, return a list of all matching symtabs. If
3825 SEARCH_PSPACE is not NULL, the search is restricted to just that
3828 static symtab_vector_up
3829 collect_symtabs_from_filename (const char *file
,
3830 struct program_space
*search_pspace
)
3832 symtab_collector collector
;
3834 /* Find that file's data. */
3835 if (search_pspace
== NULL
)
3837 struct program_space
*pspace
;
3839 ALL_PSPACES (pspace
)
3841 if (pspace
->executing_startup
)
3844 set_current_program_space (pspace
);
3845 iterate_over_symtabs (file
, collector
);
3850 set_current_program_space (search_pspace
);
3851 iterate_over_symtabs (file
, collector
);
3854 return collector
.release_symtabs ();
3857 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3858 not NULL, the search is restricted to just that program space. */
3860 static symtab_vector_up
3861 symtabs_from_filename (const char *filename
,
3862 struct program_space
*search_pspace
)
3864 symtab_vector_up result
3865 = collect_symtabs_from_filename (filename
, search_pspace
);
3867 if (result
->empty ())
3869 if (!have_full_symbols () && !have_partial_symbols ())
3870 throw_error (NOT_FOUND_ERROR
,
3871 _("No symbol table is loaded. "
3872 "Use the \"file\" command."));
3873 source_file_not_found_error (filename
);
3879 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3880 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3881 returned in MINSYMS. */
3884 find_function_symbols (struct linespec_state
*state
,
3885 std::vector
<symtab
*> *file_symtabs
, const char *name
,
3886 symbol_name_match_type name_match_type
,
3887 std::vector
<block_symbol
> *symbols
,
3888 std::vector
<bound_minimal_symbol
> *minsyms
)
3890 struct collect_info info
;
3891 std::vector
<const char *> symbol_names
;
3894 info
.result
.symbols
= symbols
;
3895 info
.result
.minimal_symbols
= minsyms
;
3896 info
.file_symtabs
= file_symtabs
;
3898 /* Try NAME as an Objective-C selector. */
3899 find_imps (name
, &symbol_names
);
3900 if (!symbol_names
.empty ())
3901 add_all_symbol_names_from_pspace (&info
, state
->search_pspace
,
3902 symbol_names
, FUNCTIONS_DOMAIN
);
3904 add_matching_symbols_to_info (name
, name_match_type
, FUNCTIONS_DOMAIN
,
3905 &info
, state
->search_pspace
);
3908 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3909 in SYMBOLS and minimal symbols in MINSYMS. */
3912 find_linespec_symbols (struct linespec_state
*state
,
3913 std::vector
<symtab
*> *file_symtabs
,
3914 const char *lookup_name
,
3915 symbol_name_match_type name_match_type
,
3916 std::vector
<block_symbol
> *symbols
,
3917 std::vector
<bound_minimal_symbol
> *minsyms
)
3919 std::string canon
= cp_canonicalize_string_no_typedefs (lookup_name
);
3920 if (!canon
.empty ())
3921 lookup_name
= canon
.c_str ();
3923 /* It's important to not call expand_symtabs_matching unnecessarily
3924 as it can really slow things down (by unnecessarily expanding
3925 potentially 1000s of symtabs, which when debugging some apps can
3926 cost 100s of seconds). Avoid this to some extent by *first* calling
3927 find_function_symbols, and only if that doesn't find anything
3928 *then* call find_method. This handles two important cases:
3929 1) break (anonymous namespace)::foo
3930 2) break class::method where method is in class (and not a baseclass) */
3932 find_function_symbols (state
, file_symtabs
, lookup_name
,
3933 name_match_type
, symbols
, minsyms
);
3935 /* If we were unable to locate a symbol of the same name, try dividing
3936 the name into class and method names and searching the class and its
3938 if (symbols
->empty () && minsyms
->empty ())
3940 std::string klass
, method
;
3941 const char *last
, *p
, *scope_op
;
3943 /* See if we can find a scope operator and break this symbol
3944 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3946 p
= find_toplevel_string (lookup_name
, scope_op
);
3952 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
3955 /* If no scope operator was found, there is nothing more we can do;
3956 we already attempted to lookup the entire name as a symbol
3961 /* LOOKUP_NAME points to the class name.
3962 LAST points to the method name. */
3963 klass
= std::string (lookup_name
, last
- lookup_name
);
3965 /* Skip past the scope operator. */
3966 last
+= strlen (scope_op
);
3969 /* Find a list of classes named KLASS. */
3970 std::vector
<block_symbol
> classes
3971 = lookup_prefix_sym (state
, file_symtabs
, klass
.c_str ());
3972 if (!classes
.empty ())
3974 /* Now locate a list of suitable methods named METHOD. */
3977 find_method (state
, file_symtabs
,
3978 klass
.c_str (), method
.c_str (),
3979 &classes
, symbols
, minsyms
);
3982 /* If successful, we're done. If NOT_FOUND_ERROR
3983 was not thrown, rethrow the exception that we did get. */
3984 CATCH (except
, RETURN_MASK_ERROR
)
3986 if (except
.error
!= NOT_FOUND_ERROR
)
3987 throw_exception (except
);
3994 /* Helper for find_label_symbols. Find all labels that match name
3995 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3996 Return the actual function symbol in which the label was found in
3997 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3998 interpreted as a label name prefix. Otherwise, only a label named
3999 exactly NAME match. */
4002 find_label_symbols_in_block (const struct block
*block
,
4003 const char *name
, struct symbol
*fn_sym
,
4004 bool completion_mode
,
4005 std::vector
<block_symbol
> *result
,
4006 std::vector
<block_symbol
> *label_funcs_ret
)
4008 if (completion_mode
)
4010 struct block_iterator iter
;
4012 size_t name_len
= strlen (name
);
4014 int (*cmp
) (const char *, const char *, size_t);
4015 cmp
= case_sensitivity
== case_sensitive_on
? strncmp
: strncasecmp
;
4017 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
4019 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
4020 SYMBOL_DOMAIN (sym
), LABEL_DOMAIN
)
4021 && cmp (SYMBOL_SEARCH_NAME (sym
), name
, name_len
) == 0)
4023 result
->push_back ({sym
, block
});
4024 label_funcs_ret
->push_back ({fn_sym
, block
});
4030 struct block_symbol label_sym
4031 = lookup_symbol (name
, block
, LABEL_DOMAIN
, 0);
4033 if (label_sym
.symbol
!= NULL
)
4035 result
->push_back (label_sym
);
4036 label_funcs_ret
->push_back ({fn_sym
, block
});
4041 /* Return all labels that match name NAME in FUNCTION_SYMBOLS or NULL
4042 if no matches were found.
4044 Return the actual function symbol in which the label was found in
4045 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4046 interpreted as a label name prefix. Otherwise, only labels named
4047 exactly NAME match. */
4050 static std::vector
<block_symbol
> *
4051 find_label_symbols (struct linespec_state
*self
,
4052 std::vector
<block_symbol
> *function_symbols
,
4053 std::vector
<block_symbol
> *label_funcs_ret
,
4055 bool completion_mode
)
4057 const struct block
*block
;
4058 struct symbol
*fn_sym
;
4059 std::vector
<block_symbol
> result
;
4061 if (function_symbols
== NULL
)
4063 set_current_program_space (self
->program_space
);
4064 block
= get_current_search_block ();
4067 block
&& !BLOCK_FUNCTION (block
);
4068 block
= BLOCK_SUPERBLOCK (block
))
4072 fn_sym
= BLOCK_FUNCTION (block
);
4074 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
4075 &result
, label_funcs_ret
);
4079 for (const auto &elt
: *function_symbols
)
4081 fn_sym
= elt
.symbol
;
4082 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym
)));
4083 block
= SYMBOL_BLOCK_VALUE (fn_sym
);
4085 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
4086 &result
, label_funcs_ret
);
4090 if (!result
.empty ())
4091 return new std::vector
<block_symbol
> (std::move (result
));
4097 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4099 static std::vector
<symtab_and_line
>
4100 decode_digits_list_mode (struct linespec_state
*self
,
4102 struct symtab_and_line val
)
4104 gdb_assert (self
->list_mode
);
4106 std::vector
<symtab_and_line
> values
;
4108 for (const auto &elt
: *ls
->file_symtabs
)
4110 /* The logic above should ensure this. */
4111 gdb_assert (elt
!= NULL
);
4113 set_current_program_space (SYMTAB_PSPACE (elt
));
4115 /* Simplistic search just for the list command. */
4116 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
4117 if (val
.symtab
== NULL
)
4119 val
.pspace
= SYMTAB_PSPACE (elt
);
4121 val
.explicit_line
= 1;
4123 add_sal_to_sals (self
, &values
, &val
, NULL
, 0);
4129 /* A helper for create_sals_line_offset that iterates over the symtabs,
4130 adding lines to the VEC. */
4132 static std::vector
<symtab_and_line
>
4133 decode_digits_ordinary (struct linespec_state
*self
,
4136 struct linetable_entry
**best_entry
)
4138 std::vector
<symtab_and_line
> sals
;
4139 for (const auto &elt
: *ls
->file_symtabs
)
4141 std::vector
<CORE_ADDR
> pcs
;
4143 /* The logic above should ensure this. */
4144 gdb_assert (elt
!= NULL
);
4146 set_current_program_space (SYMTAB_PSPACE (elt
));
4148 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
4149 for (CORE_ADDR pc
: pcs
)
4151 symtab_and_line sal
;
4152 sal
.pspace
= SYMTAB_PSPACE (elt
);
4156 sals
.push_back (std::move (sal
));
4165 /* Return the line offset represented by VARIABLE. */
4167 static struct line_offset
4168 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
4172 struct line_offset offset
= {0, LINE_OFFSET_NONE
};
4174 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
4177 while (*p
>= '0' && *p
<= '9')
4179 if (!*p
) /* Reached end of token without hitting non-digit. */
4181 /* We have a value history reference. */
4182 struct value
*val_history
;
4184 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
4186 = access_value_history ((variable
[1] == '$') ? -index
: index
);
4187 if (TYPE_CODE (value_type (val_history
)) != TYPE_CODE_INT
)
4188 error (_("History values used in line "
4189 "specs must have integer values."));
4190 offset
.offset
= value_as_long (val_history
);
4194 /* Not all digits -- may be user variable/function or a
4195 convenience variable. */
4197 struct internalvar
*ivar
;
4199 /* Try it as a convenience variable. If it is not a convenience
4200 variable, return and allow normal symbol lookup to occur. */
4201 ivar
= lookup_only_internalvar (variable
+ 1);
4203 /* No internal variable with that name. Mark the offset
4204 as unknown to allow the name to be looked up as a symbol. */
4205 offset
.sign
= LINE_OFFSET_UNKNOWN
;
4208 /* We found a valid variable name. If it is not an integer,
4210 if (!get_internalvar_integer (ivar
, &valx
))
4211 error (_("Convenience variables used in line "
4212 "specs must have integer values."));
4214 offset
.offset
= valx
;
4222 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
4223 linespec; return the SAL in RESULT. This function should return SALs
4224 matching those from find_function_start_sal, otherwise false
4225 multiple-locations breakpoints could be placed. */
4228 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
4229 struct minimal_symbol
*msymbol
,
4230 std::vector
<symtab_and_line
> *result
)
4232 bool want_start_sal
;
4234 CORE_ADDR func_addr
;
4235 bool is_function
= msymbol_is_function (objfile
, msymbol
, &func_addr
);
4239 const char *msym_name
= MSYMBOL_LINKAGE_NAME (msymbol
);
4241 if (MSYMBOL_TYPE (msymbol
) == mst_text_gnu_ifunc
4242 || MSYMBOL_TYPE (msymbol
) == mst_data_gnu_ifunc
)
4243 want_start_sal
= gnu_ifunc_resolve_name (msym_name
, &func_addr
);
4245 want_start_sal
= true;
4248 symtab_and_line sal
;
4250 if (is_function
&& want_start_sal
)
4251 sal
= find_function_start_sal (func_addr
, NULL
, self
->funfirstline
);
4254 sal
.objfile
= objfile
;
4255 sal
.msymbol
= msymbol
;
4256 /* Store func_addr, not the minsym's address in case this was an
4257 ifunc that hasn't been resolved yet. */
4261 sal
.pc
= MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
4262 sal
.pspace
= current_program_space
;
4265 sal
.section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
);
4267 if (maybe_add_address (self
->addr_set
, objfile
->pspace
, sal
.pc
))
4268 add_sal_to_sals (self
, result
, &sal
, MSYMBOL_NATURAL_NAME (msymbol
), 0);
4271 /* A helper function to classify a minimal_symbol_type according to
4275 classify_mtype (enum minimal_symbol_type t
)
4282 /* Intermediate priority. */
4285 case mst_solib_trampoline
:
4286 /* Lowest priority. */
4290 /* Highest priority. */
4295 /* Callback for std::sort that sorts symbols by priority. */
4298 compare_msyms (const bound_minimal_symbol
&a
, const bound_minimal_symbol
&b
)
4300 enum minimal_symbol_type ta
= MSYMBOL_TYPE (a
.minsym
);
4301 enum minimal_symbol_type tb
= MSYMBOL_TYPE (b
.minsym
);
4303 return classify_mtype (ta
) < classify_mtype (tb
);
4306 /* Helper for search_minsyms_for_name that adds the symbol to the
4310 add_minsym (struct minimal_symbol
*minsym
, struct objfile
*objfile
,
4311 struct symtab
*symtab
, int list_mode
,
4312 std::vector
<struct bound_minimal_symbol
> *msyms
)
4316 /* We're looking for a label for which we don't have debug
4318 CORE_ADDR func_addr
;
4319 if (msymbol_is_function (objfile
, minsym
, &func_addr
))
4321 symtab_and_line sal
= find_pc_sect_line (func_addr
, NULL
, 0);
4323 if (symtab
!= sal
.symtab
)
4328 /* Exclude data symbols when looking for breakpoint locations. */
4329 if (!list_mode
&& !msymbol_is_function (objfile
, minsym
))
4332 struct bound_minimal_symbol mo
= {minsym
, objfile
};
4333 msyms
->push_back (mo
);
4337 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4338 is not NULL, the search is restricted to just that program
4341 If SYMTAB is NULL, search all objfiles, otherwise
4342 restrict results to the given SYMTAB. */
4345 search_minsyms_for_name (struct collect_info
*info
,
4346 const lookup_name_info
&name
,
4347 struct program_space
*search_pspace
,
4348 struct symtab
*symtab
)
4350 std::vector
<struct bound_minimal_symbol
> minsyms
;
4354 struct program_space
*pspace
;
4356 ALL_PSPACES (pspace
)
4358 struct objfile
*objfile
;
4360 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
4362 if (pspace
->executing_startup
)
4365 set_current_program_space (pspace
);
4367 ALL_OBJFILES (objfile
)
4369 iterate_over_minimal_symbols (objfile
, name
,
4370 [&] (struct minimal_symbol
*msym
)
4372 add_minsym (msym
, objfile
, nullptr,
4373 info
->state
->list_mode
,
4382 if (search_pspace
== NULL
|| SYMTAB_PSPACE (symtab
) == search_pspace
)
4384 set_current_program_space (SYMTAB_PSPACE (symtab
));
4385 iterate_over_minimal_symbols
4386 (SYMTAB_OBJFILE (symtab
), name
,
4387 [&] (struct minimal_symbol
*msym
)
4389 add_minsym (msym
, SYMTAB_OBJFILE (symtab
), symtab
,
4390 info
->state
->list_mode
, &minsyms
);
4396 if (!minsyms
.empty ())
4400 std::sort (minsyms
.begin (), minsyms
.end (), compare_msyms
);
4402 /* Now the minsyms are in classification order. So, we walk
4403 over them and process just the minsyms with the same
4404 classification as the very first minsym in the list. */
4405 classification
= classify_mtype (MSYMBOL_TYPE (minsyms
[0].minsym
));
4407 for (const bound_minimal_symbol
&item
: minsyms
)
4409 if (classify_mtype (MSYMBOL_TYPE (item
.minsym
)) != classification
)
4412 info
->result
.minimal_symbols
->push_back (item
);
4417 /* A helper function to add all symbols matching NAME to INFO. If
4418 PSPACE is not NULL, the search is restricted to just that program
4422 add_matching_symbols_to_info (const char *name
,
4423 symbol_name_match_type name_match_type
,
4424 enum search_domain search_domain
,
4425 struct collect_info
*info
,
4426 struct program_space
*pspace
)
4428 lookup_name_info
lookup_name (name
, name_match_type
);
4430 for (const auto &elt
: *info
->file_symtabs
)
4434 iterate_over_all_matching_symtabs (info
->state
, lookup_name
,
4435 VAR_DOMAIN
, search_domain
,
4437 [&] (block_symbol
*bsym
)
4438 { return info
->add_symbol (bsym
); });
4439 search_minsyms_for_name (info
, lookup_name
, pspace
, NULL
);
4441 else if (pspace
== NULL
|| pspace
== SYMTAB_PSPACE (elt
))
4443 int prev_len
= info
->result
.symbols
->size ();
4445 /* Program spaces that are executing startup should have
4446 been filtered out earlier. */
4447 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
4448 set_current_program_space (SYMTAB_PSPACE (elt
));
4449 iterate_over_file_blocks (elt
, lookup_name
, VAR_DOMAIN
,
4450 [&] (block_symbol
*bsym
)
4451 { return info
->add_symbol (bsym
); });
4453 /* If no new symbols were found in this iteration and this symtab
4454 is in assembler, we might actually be looking for a label for
4455 which we don't have debug info. Check for a minimal symbol in
4457 if (prev_len
== info
->result
.symbols
->size ()
4458 && elt
->language
== language_asm
)
4459 search_minsyms_for_name (info
, lookup_name
, pspace
, elt
);
4466 /* Now come some functions that are called from multiple places within
4470 symbol_to_sal (struct symtab_and_line
*result
,
4471 int funfirstline
, struct symbol
*sym
)
4473 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
4475 *result
= find_function_start_sal (sym
, funfirstline
);
4480 if (SYMBOL_CLASS (sym
) == LOC_LABEL
&& SYMBOL_VALUE_ADDRESS (sym
) != 0)
4483 result
->symtab
= symbol_symtab (sym
);
4484 result
->symbol
= sym
;
4485 result
->line
= SYMBOL_LINE (sym
);
4486 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
4487 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
4488 result
->explicit_pc
= 1;
4491 else if (funfirstline
)
4495 else if (SYMBOL_LINE (sym
) != 0)
4497 /* We know its line number. */
4499 result
->symtab
= symbol_symtab (sym
);
4500 result
->symbol
= sym
;
4501 result
->line
= SYMBOL_LINE (sym
);
4502 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
4503 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
4511 linespec_result::~linespec_result ()
4513 for (linespec_sals
&lsal
: lsals
)
4514 xfree (lsal
.canonical
);
4517 /* Return the quote characters permitted by the linespec parser. */
4520 get_gdb_linespec_parser_quote_characters (void)
4522 return linespec_quote_characters
;