1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "completer.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
34 #include "objc-lang.h"
38 #include "mi/mi-cmds.h"
40 #include "arch-utils.h"
42 #include "cli/cli-utils.h"
43 #include "filenames.h"
47 #include "common/function-view.h"
49 /* An enumeration of the various things a user might attempt to
50 complete for a linespec location. */
52 enum class linespec_complete_what
54 /* Nothing, no possible completion. */
57 /* A function/method name. Due to ambiguity between
63 this can also indicate a source filename, iff we haven't seen a
64 separate source filename component, as in "b source.c:function". */
67 /* A label symbol. E.g., break file.c:function:LABEL. */
70 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
73 /* A linespec keyword ("if"/"thread"/"task").
74 E.g., "break func threa<tab>". */
78 typedef struct symbol
*symbolp
;
81 typedef struct type
*typep
;
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 typedef struct bound_minimal_symbol bound_minimal_symbol_d
;
96 DEF_VEC_O (bound_minimal_symbol_d
);
98 /* A linespec. Elements of this structure are filled in by a parser
99 (either parse_linespec or some other function). The structure is
100 then converted into SALs by convert_linespec_to_sals. */
104 /* An explicit location describing the SaLs. */
105 struct explicit_location explicit_loc
;
107 /* The list of symtabs to search to which to limit the search. May not
108 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
109 filename), FILE_SYMTABS should contain one single NULL member. This
110 will cause the code to use the default symtab. */
111 VEC (symtab_ptr
) *file_symtabs
;
113 /* A list of matching function symbols and minimal symbols. Both lists
114 may be NULL if no matching symbols were found. */
115 VEC (symbolp
) *function_symbols
;
116 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
118 /* A structure of matching label symbols and the corresponding
119 function symbol in which the label was found. Both may be NULL
120 or both must be non-NULL. */
123 VEC (symbolp
) *label_symbols
;
124 VEC (symbolp
) *function_symbols
;
127 typedef struct linespec
*linespec_p
;
129 /* A canonical linespec represented as a symtab-related string.
131 Each entry represents the "SYMTAB:SUFFIX" linespec string.
132 SYMTAB can be converted for example by symtab_to_fullname or
133 symtab_to_filename_for_display as needed. */
135 struct linespec_canonical_name
137 /* Remaining text part of the linespec string. */
140 /* If NULL then SUFFIX is the whole linespec string. */
141 struct symtab
*symtab
;
144 /* An instance of this is used to keep all state while linespec
145 operates. This instance is passed around as a 'this' pointer to
146 the various implementation methods. */
148 struct linespec_state
150 /* The language in use during linespec processing. */
151 const struct language_defn
*language
;
153 /* The program space as seen when the module was entered. */
154 struct program_space
*program_space
;
156 /* If not NULL, the search is restricted to just this program
158 struct program_space
*search_pspace
;
160 /* The default symtab to use, if no other symtab is specified. */
161 struct symtab
*default_symtab
;
163 /* The default line to use. */
166 /* The 'funfirstline' value that was passed in to decode_line_1 or
170 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
173 /* The 'canonical' value passed to decode_line_full, or NULL. */
174 struct linespec_result
*canonical
;
176 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
177 struct linespec_canonical_name
*canonical_names
;
179 /* This is a set of address_entry objects which is used to prevent
180 duplicate symbols from being entered into the result. */
183 /* Are we building a linespec? */
187 /* This is a helper object that is used when collecting symbols into a
192 /* The linespec object in use. */
193 struct linespec_state
*state
;
195 /* A list of symtabs to which to restrict matches. */
196 VEC (symtab_ptr
) *file_symtabs
;
198 /* The result being accumulated. */
201 VEC (symbolp
) *symbols
;
202 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
205 /* Possibly add a symbol to the results. */
206 bool add_symbol (symbol
*sym
);
210 collect_info::add_symbol (symbol
*sym
)
212 /* In list mode, add all matching symbols, regardless of class.
213 This allows the user to type "list a_global_variable". */
214 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
|| this->state
->list_mode
)
215 VEC_safe_push (symbolp
, this->result
.symbols
, sym
);
217 /* Continue iterating. */
228 /* A colon "separator" */
240 /* EOI (end of input) */
246 typedef enum ls_token_type linespec_token_type
;
248 /* List of keywords. This is NULL-terminated so that it can be used
249 as enum completer. */
250 const char * const linespec_keywords
[] = { "if", "thread", "task", NULL
};
251 #define IF_KEYWORD_INDEX 0
253 /* A token of the linespec lexer */
257 /* The type of the token */
258 linespec_token_type type
;
260 /* Data for the token */
263 /* A string, given as a stoken */
264 struct stoken string
;
270 typedef struct ls_token linespec_token
;
272 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
273 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
275 /* An instance of the linespec parser. */
279 /* Lexer internal data */
282 /* Save head of input stream. */
283 const char *saved_arg
;
285 /* Head of the input stream. */
287 #define PARSER_STREAM(P) ((P)->lexer.stream)
289 /* The current token. */
290 linespec_token current
;
293 /* Is the entire linespec quote-enclosed? */
294 int is_quote_enclosed
;
296 /* The state of the parse. */
297 struct linespec_state state
;
298 #define PARSER_STATE(PPTR) (&(PPTR)->state)
300 /* The result of the parse. */
301 struct linespec result
;
302 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
304 /* What the parser believes the current word point should complete
306 linespec_complete_what complete_what
;
308 /* The completion word point. The parser advances this as it skips
309 tokens. At some point the input string will end or parsing will
310 fail, and then we attempt completion at the captured completion
311 word point, interpreting the string at completion_word as
313 const char *completion_word
;
315 /* If the current token was a quoted string, then this is the
316 quoting character (either " or '). */
317 int completion_quote_char
;
319 /* If the current token was a quoted string, then this points at the
320 end of the quoted string. */
321 const char *completion_quote_end
;
323 /* If parsing for completion, then this points at the completion
324 tracker. Otherwise, this is NULL. */
325 struct completion_tracker
*completion_tracker
;
327 typedef struct ls_parser linespec_parser
;
329 /* A convenience macro for accessing the explicit location result of
331 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
333 /* Prototypes for local functions. */
335 static void iterate_over_file_blocks
336 (struct symtab
*symtab
, const char *name
, domain_enum domain
,
337 gdb::function_view
<symbol_found_callback_ftype
> callback
);
339 static void initialize_defaults (struct symtab
**default_symtab
,
342 CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
344 static std::vector
<symtab_and_line
> decode_objc (struct linespec_state
*self
,
348 static VEC (symtab_ptr
) *symtabs_from_filename (const char *,
349 struct program_space
*pspace
);
351 static VEC (symbolp
) *find_label_symbols (struct linespec_state
*self
,
352 VEC (symbolp
) *function_symbols
,
353 VEC (symbolp
) **label_funcs_ret
,
355 bool completion_mode
= false);
357 static void find_linespec_symbols (struct linespec_state
*self
,
358 VEC (symtab_ptr
) *file_symtabs
,
360 VEC (symbolp
) **symbols
,
361 VEC (bound_minimal_symbol_d
) **minsyms
);
363 static struct line_offset
364 linespec_parse_variable (struct linespec_state
*self
,
365 const char *variable
);
367 static int symbol_to_sal (struct symtab_and_line
*result
,
368 int funfirstline
, struct symbol
*sym
);
370 static void add_matching_symbols_to_info (const char *name
,
371 struct collect_info
*info
,
372 struct program_space
*pspace
);
374 static void add_all_symbol_names_from_pspace (struct collect_info
*info
,
375 struct program_space
*pspace
,
376 VEC (const_char_ptr
) *names
);
378 static VEC (symtab_ptr
) *
379 collect_symtabs_from_filename (const char *file
,
380 struct program_space
*pspace
);
382 static std::vector
<symtab_and_line
> decode_digits_ordinary
383 (struct linespec_state
*self
,
386 linetable_entry
**best_entry
);
388 static std::vector
<symtab_and_line
> decode_digits_list_mode
389 (struct linespec_state
*self
,
391 struct symtab_and_line val
);
393 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
394 struct minimal_symbol
*msymbol
,
395 std::vector
<symtab_and_line
> *result
);
397 static int compare_symbols (const void *a
, const void *b
);
399 static int compare_msymbols (const void *a
, const void *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
)
476 p
= skip_spaces_const (p
);
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. */
533 copy_token_string (linespec_token token
)
537 if (token
.type
== LSTOKEN_KEYWORD
)
538 return xstrdup (LS_TOKEN_KEYWORD (token
));
540 str
= savestring (LS_TOKEN_STOKEN (token
).ptr
,
541 LS_TOKEN_STOKEN (token
).length
);
542 s
= remove_trailing_whitespace (str
, str
+ LS_TOKEN_STOKEN (token
).length
);
548 /* Does P represent the end of a quote-enclosed linespec? */
551 is_closing_quote_enclosed (const char *p
)
553 if (strchr (linespec_quote_characters
, *p
))
555 p
= skip_spaces ((char *) p
);
556 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
559 /* Find the end of the parameter list that starts with *INPUT.
560 This helper function assists with lexing string segments
561 which might contain valid (non-terminating) commas. */
564 find_parameter_list_end (const char *input
)
566 char end_char
, start_char
;
571 if (start_char
== '(')
573 else if (start_char
== '<')
582 if (*p
== start_char
)
584 else if (*p
== end_char
)
598 /* If the [STRING, STRING_LEN) string ends with what looks like a
599 keyword, return the keyword start offset in STRING. Return -1
603 string_find_incomplete_keyword_at_end (const char * const *keywords
,
604 const char *string
, size_t string_len
)
606 const char *end
= string
+ string_len
;
609 while (p
> string
&& *p
!= ' ')
614 size_t len
= end
- p
;
615 for (size_t i
= 0; keywords
[i
] != NULL
; ++i
)
616 if (strncmp (keywords
[i
], p
, len
) == 0)
623 /* Lex a string from the input in PARSER. */
625 static linespec_token
626 linespec_lexer_lex_string (linespec_parser
*parser
)
628 linespec_token token
;
629 const char *start
= PARSER_STREAM (parser
);
631 token
.type
= LSTOKEN_STRING
;
633 /* If the input stream starts with a quote character, skip to the next
634 quote character, regardless of the content. */
635 if (strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
638 char quote_char
= *PARSER_STREAM (parser
);
640 /* Special case: Ada operators. */
641 if (PARSER_STATE (parser
)->language
->la_language
== language_ada
642 && quote_char
== '\"')
644 int len
= is_ada_operator (PARSER_STREAM (parser
));
648 /* The input is an Ada operator. Return the quoted string
650 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
651 LS_TOKEN_STOKEN (token
).length
= len
;
652 PARSER_STREAM (parser
) += len
;
656 /* The input does not represent an Ada operator -- fall through
657 to normal quoted string handling. */
660 /* Skip past the beginning quote. */
661 ++(PARSER_STREAM (parser
));
663 /* Mark the start of the string. */
664 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
666 /* Skip to the ending quote. */
667 end
= skip_quote_char (PARSER_STREAM (parser
), quote_char
);
669 /* This helps the completer mode decide whether we have a
671 parser
->completion_quote_char
= quote_char
;
672 parser
->completion_quote_end
= end
;
674 /* Error if the input did not terminate properly, unless in
678 if (parser
->completion_tracker
== NULL
)
679 error (_("unmatched quote"));
681 /* In completion mode, we'll try to complete the incomplete
683 token
.type
= LSTOKEN_STRING
;
684 while (*PARSER_STREAM (parser
) != '\0')
685 PARSER_STREAM (parser
)++;
686 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 1 - start
;
690 /* Skip over the ending quote and mark the length of the string. */
691 PARSER_STREAM (parser
) = (char *) ++end
;
692 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 2 - start
;
699 /* Otherwise, only identifier characters are permitted.
700 Spaces are the exception. In general, we keep spaces,
701 but only if the next characters in the input do not resolve
702 to one of the keywords.
704 This allows users to forgo quoting CV-qualifiers, template arguments,
705 and similar common language constructs. */
709 if (isspace (*PARSER_STREAM (parser
)))
711 p
= skip_spaces_const (PARSER_STREAM (parser
));
712 /* When we get here we know we've found something followed by
713 a space (we skip over parens and templates below).
714 So if we find a keyword now, we know it is a keyword and not,
715 say, a function name. */
716 if (linespec_lexer_lex_keyword (p
) != NULL
)
718 LS_TOKEN_STOKEN (token
).ptr
= start
;
719 LS_TOKEN_STOKEN (token
).length
720 = PARSER_STREAM (parser
) - start
;
724 /* Advance past the whitespace. */
725 PARSER_STREAM (parser
) = p
;
728 /* If the next character is EOI or (single) ':', the
729 string is complete; return the token. */
730 if (*PARSER_STREAM (parser
) == 0)
732 LS_TOKEN_STOKEN (token
).ptr
= start
;
733 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
736 else if (PARSER_STREAM (parser
)[0] == ':')
738 /* Do not tokenize the C++ scope operator. */
739 if (PARSER_STREAM (parser
)[1] == ':')
740 ++(PARSER_STREAM (parser
));
742 /* Do not tokenify if the input length so far is one
743 (i.e, a single-letter drive name) and the next character
744 is a directory separator. This allows Windows-style
745 paths to be recognized as filenames without quoting it. */
746 else if ((PARSER_STREAM (parser
) - start
) != 1
747 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser
)[1]))
749 LS_TOKEN_STOKEN (token
).ptr
= start
;
750 LS_TOKEN_STOKEN (token
).length
751 = PARSER_STREAM (parser
) - start
;
755 /* Special case: permit quote-enclosed linespecs. */
756 else if (parser
->is_quote_enclosed
757 && strchr (linespec_quote_characters
,
758 *PARSER_STREAM (parser
))
759 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
761 LS_TOKEN_STOKEN (token
).ptr
= start
;
762 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
765 /* Because commas may terminate a linespec and appear in
766 the middle of valid string input, special cases for
767 '<' and '(' are necessary. */
768 else if (*PARSER_STREAM (parser
) == '<'
769 || *PARSER_STREAM (parser
) == '(')
771 /* Don't interpret 'operator<' / 'operator<<' as a
772 template parameter list though. */
773 if (*PARSER_STREAM (parser
) == '<'
774 && (PARSER_STATE (parser
)->language
->la_language
776 && (PARSER_STREAM (parser
) - start
) >= CP_OPERATOR_LEN
)
778 const char *p
= PARSER_STREAM (parser
);
780 while (p
> start
&& isspace (p
[-1]))
782 if (p
- start
>= CP_OPERATOR_LEN
)
784 p
-= CP_OPERATOR_LEN
;
785 if (strncmp (p
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0
787 || !(isalnum (p
[-1]) || p
[-1] == '_')))
789 /* This is an operator name. Keep going. */
790 ++(PARSER_STREAM (parser
));
791 if (*PARSER_STREAM (parser
) == '<')
792 ++(PARSER_STREAM (parser
));
798 const char *p
= find_parameter_list_end (PARSER_STREAM (parser
));
799 PARSER_STREAM (parser
) = p
;
801 /* Don't loop around to the normal \0 case above because
802 we don't want to misinterpret a potential keyword at
803 the end of the token when the string isn't
804 "()<>"-balanced. This handles "b
805 function(thread<tab>" in completion mode. */
808 LS_TOKEN_STOKEN (token
).ptr
= start
;
809 LS_TOKEN_STOKEN (token
).length
810 = PARSER_STREAM (parser
) - start
;
816 /* Commas are terminators, but not if they are part of an
818 else if (*PARSER_STREAM (parser
) == ',')
820 if ((PARSER_STATE (parser
)->language
->la_language
822 && (PARSER_STREAM (parser
) - start
) > CP_OPERATOR_LEN
)
824 const char *p
= strstr (start
, CP_OPERATOR_STR
);
826 if (p
!= NULL
&& is_operator_name (p
))
828 /* This is an operator name. Keep going. */
829 ++(PARSER_STREAM (parser
));
834 /* Comma terminates the string. */
835 LS_TOKEN_STOKEN (token
).ptr
= start
;
836 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
840 /* Advance the stream. */
841 ++(PARSER_STREAM (parser
));
848 /* Lex a single linespec token from PARSER. */
850 static linespec_token
851 linespec_lexer_lex_one (linespec_parser
*parser
)
855 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
857 /* Skip any whitespace. */
858 PARSER_STREAM (parser
) = skip_spaces_const (PARSER_STREAM (parser
));
860 /* Check for a keyword, they end the linespec. */
861 keyword
= linespec_lexer_lex_keyword (PARSER_STREAM (parser
));
864 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
865 LS_TOKEN_KEYWORD (parser
->lexer
.current
) = keyword
;
866 /* We do not advance the stream here intentionally:
867 we would like lexing to stop when a keyword is seen.
869 PARSER_STREAM (parser) += strlen (keyword); */
871 return parser
->lexer
.current
;
874 /* Handle other tokens. */
875 switch (*PARSER_STREAM (parser
))
878 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
882 case '0': case '1': case '2': case '3': case '4':
883 case '5': case '6': case '7': case '8': case '9':
884 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
885 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
889 /* If we have a scope operator, lex the input as a string.
890 Otherwise, return LSTOKEN_COLON. */
891 if (PARSER_STREAM (parser
)[1] == ':')
892 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
895 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
896 ++(PARSER_STREAM (parser
));
900 case '\'': case '\"':
901 /* Special case: permit quote-enclosed linespecs. */
902 if (parser
->is_quote_enclosed
903 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
905 ++(PARSER_STREAM (parser
));
906 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
909 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
913 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
914 LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
915 = PARSER_STREAM (parser
);
916 LS_TOKEN_STOKEN (parser
->lexer
.current
).length
= 1;
917 ++(PARSER_STREAM (parser
));
921 /* If the input is not a number, it must be a string.
922 [Keywords were already considered above.] */
923 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
928 return parser
->lexer
.current
;
931 /* Consume the current token and return the next token in PARSER's
932 input stream. Also advance the completion word for completion
935 static linespec_token
936 linespec_lexer_consume_token (linespec_parser
*parser
)
938 gdb_assert (parser
->lexer
.current
.type
!= LSTOKEN_EOI
);
940 bool advance_word
= (parser
->lexer
.current
.type
!= LSTOKEN_STRING
941 || *PARSER_STREAM (parser
) != '\0');
943 /* If we're moving past a string to some other token, it must be the
944 quote was terminated. */
945 if (parser
->completion_quote_char
)
947 gdb_assert (parser
->lexer
.current
.type
== LSTOKEN_STRING
);
949 /* If the string was the last (non-EOI) token, we're past the
950 quote, but remember that for later. */
951 if (*PARSER_STREAM (parser
) != '\0')
953 parser
->completion_quote_char
= '\0';
954 parser
->completion_quote_end
= NULL
;;
958 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
959 linespec_lexer_lex_one (parser
);
961 if (parser
->lexer
.current
.type
== LSTOKEN_STRING
)
963 /* Advance the completion word past a potential initial
965 parser
->completion_word
= LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
;
967 else if (advance_word
)
969 /* Advance the completion word past any whitespace. */
970 parser
->completion_word
= PARSER_STREAM (parser
);
973 return parser
->lexer
.current
;
976 /* Return the next token without consuming the current token. */
978 static linespec_token
979 linespec_lexer_peek_token (linespec_parser
*parser
)
982 const char *saved_stream
= PARSER_STREAM (parser
);
983 linespec_token saved_token
= parser
->lexer
.current
;
984 int saved_completion_quote_char
= parser
->completion_quote_char
;
985 const char *saved_completion_quote_end
= parser
->completion_quote_end
;
986 const char *saved_completion_word
= parser
->completion_word
;
988 next
= linespec_lexer_consume_token (parser
);
989 PARSER_STREAM (parser
) = saved_stream
;
990 parser
->lexer
.current
= saved_token
;
991 parser
->completion_quote_char
= saved_completion_quote_char
;
992 parser
->completion_quote_end
= saved_completion_quote_end
;
993 parser
->completion_word
= saved_completion_word
;
997 /* Helper functions. */
999 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1000 the new sal, if needed. If not NULL, SYMNAME is the name of the
1001 symbol to use when constructing the new canonical name.
1003 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1004 canonical name for the SAL. */
1007 add_sal_to_sals (struct linespec_state
*self
,
1008 std::vector
<symtab_and_line
> *sals
,
1009 struct symtab_and_line
*sal
,
1010 const char *symname
, int literal_canonical
)
1012 sals
->push_back (*sal
);
1014 if (self
->canonical
)
1016 struct linespec_canonical_name
*canonical
;
1018 self
->canonical_names
= XRESIZEVEC (struct linespec_canonical_name
,
1019 self
->canonical_names
,
1021 canonical
= &self
->canonical_names
[sals
->size () - 1];
1022 if (!literal_canonical
&& sal
->symtab
)
1024 symtab_to_fullname (sal
->symtab
);
1026 /* Note that the filter doesn't have to be a valid linespec
1027 input. We only apply the ":LINE" treatment to Ada for
1029 if (symname
!= NULL
&& sal
->line
!= 0
1030 && self
->language
->la_language
== language_ada
)
1031 canonical
->suffix
= xstrprintf ("%s:%d", symname
, sal
->line
);
1032 else if (symname
!= NULL
)
1033 canonical
->suffix
= xstrdup (symname
);
1035 canonical
->suffix
= xstrprintf ("%d", sal
->line
);
1036 canonical
->symtab
= sal
->symtab
;
1040 if (symname
!= NULL
)
1041 canonical
->suffix
= xstrdup (symname
);
1043 canonical
->suffix
= xstrdup ("<unknown>");
1044 canonical
->symtab
= NULL
;
1049 /* A hash function for address_entry. */
1052 hash_address_entry (const void *p
)
1054 const struct address_entry
*aep
= (const struct address_entry
*) p
;
1057 hash
= iterative_hash_object (aep
->pspace
, 0);
1058 return iterative_hash_object (aep
->addr
, hash
);
1061 /* An equality function for address_entry. */
1064 eq_address_entry (const void *a
, const void *b
)
1066 const struct address_entry
*aea
= (const struct address_entry
*) a
;
1067 const struct address_entry
*aeb
= (const struct address_entry
*) b
;
1069 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
1072 /* Check whether the address, represented by PSPACE and ADDR, is
1073 already in the set. If so, return 0. Otherwise, add it and return
1077 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
1079 struct address_entry e
, *p
;
1084 slot
= htab_find_slot (set
, &e
, INSERT
);
1088 p
= XNEW (struct address_entry
);
1089 memcpy (p
, &e
, sizeof (struct address_entry
));
1095 /* A helper that walks over all matching symtabs in all objfiles and
1096 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1097 not NULL, then the search is restricted to just that program
1098 space. If INCLUDE_INLINE is true then symbols representing
1099 inlined instances of functions will be included in the result. */
1102 iterate_over_all_matching_symtabs
1103 (struct linespec_state
*state
, const char *name
, const domain_enum domain
,
1104 struct program_space
*search_pspace
, bool include_inline
,
1105 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1107 struct objfile
*objfile
;
1108 struct program_space
*pspace
;
1110 /* The routine to be used for comparison. */
1111 symbol_name_cmp_ftype symbol_name_cmp
1112 = (state
->language
->la_get_symbol_name_cmp
!= NULL
1113 ? state
->language
->la_get_symbol_name_cmp (name
)
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
1133 [&] (const char *symbol_name
)
1135 return symbol_name_cmp (symbol_name
, name
) == 0;
1140 ALL_OBJFILE_COMPUNITS (objfile
, cu
)
1142 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1144 iterate_over_file_blocks (symtab
, name
, domain
, callback
);
1148 struct block
*block
;
1151 for (i
= FIRST_LOCAL_BLOCK
;
1152 i
< BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab
));
1155 block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), i
);
1156 state
->language
->la_iterate_over_symbols
1157 (block
, name
, domain
, [&] (symbol
*sym
)
1159 /* Restrict calls to CALLBACK to symbols
1160 representing inline symbols only. */
1161 if (SYMBOL_INLINED (sym
))
1162 return callback (sym
);
1172 /* Returns the block to be used for symbol searches from
1173 the current location. */
1175 static const struct block
*
1176 get_current_search_block (void)
1178 const struct block
*block
;
1179 enum language save_language
;
1181 /* get_selected_block can change the current language when there is
1182 no selected frame yet. */
1183 save_language
= current_language
->la_language
;
1184 block
= get_selected_block (0);
1185 set_language (save_language
);
1190 /* Iterate over static and global blocks. */
1193 iterate_over_file_blocks
1194 (struct symtab
*symtab
, const char *name
, domain_enum domain
,
1195 gdb::function_view
<symbol_found_callback_ftype
> callback
)
1197 struct block
*block
;
1199 for (block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1201 block
= BLOCK_SUPERBLOCK (block
))
1202 LA_ITERATE_OVER_SYMBOLS (block
, name
, domain
, callback
);
1205 /* A helper for find_method. This finds all methods in type T which
1206 match NAME. It adds matching symbol names to RESULT_NAMES, and
1207 adds T's direct superclasses to SUPERCLASSES. */
1210 find_methods (struct type
*t
, const char *name
,
1211 VEC (const_char_ptr
) **result_names
,
1212 VEC (typep
) **superclasses
)
1215 const char *class_name
= type_name_no_tag (t
);
1217 /* Ignore this class if it doesn't have a name. This is ugly, but
1218 unless we figure out how to get the physname without the name of
1219 the class, then the loop can't do any good. */
1224 t
= check_typedef (t
);
1226 /* Loop over each method name. At this level, all overloads of a name
1227 are counted as a single name. There is an inner loop which loops over
1230 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1231 method_counter
>= 0;
1234 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1235 char dem_opname
[64];
1237 if (startswith (method_name
, "__") ||
1238 startswith (method_name
, "op") ||
1239 startswith (method_name
, "type"))
1241 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
1242 method_name
= dem_opname
;
1243 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
1244 method_name
= dem_opname
;
1247 if (strcmp_iw (method_name
, name
) == 0)
1251 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1257 const char *phys_name
;
1259 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1260 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1262 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1263 VEC_safe_push (const_char_ptr
, *result_names
, phys_name
);
1269 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1270 VEC_safe_push (typep
, *superclasses
, TYPE_BASECLASS (t
, ibase
));
1273 /* Find an instance of the character C in the string S that is outside
1274 of all parenthesis pairs, single-quoted strings, and double-quoted
1275 strings. Also, ignore the char within a template name, like a ','
1276 within foo<int, int>, while considering C++ operator</operator<<. */
1279 find_toplevel_char (const char *s
, char c
)
1281 int quoted
= 0; /* zero if we're not in quotes;
1282 '"' if we're in a double-quoted string;
1283 '\'' if we're in a single-quoted string. */
1284 int depth
= 0; /* Number of unclosed parens we've seen. */
1287 for (scan
= s
; *scan
; scan
++)
1291 if (*scan
== quoted
)
1293 else if (*scan
== '\\' && *(scan
+ 1))
1296 else if (*scan
== c
&& ! quoted
&& depth
== 0)
1298 else if (*scan
== '"' || *scan
== '\'')
1300 else if (*scan
== '(' || *scan
== '<')
1302 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
1304 else if (*scan
== 'o' && !quoted
&& depth
== 0)
1306 /* Handle C++ operator names. */
1307 if (strncmp (scan
, CP_OPERATOR_STR
, CP_OPERATOR_LEN
) == 0)
1309 scan
+= CP_OPERATOR_LEN
;
1312 while (isspace (*scan
))
1323 /* Skip over one less than the appropriate number of
1324 characters: the for loop will skip over the last
1350 /* The string equivalent of find_toplevel_char. Returns a pointer
1351 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1352 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1355 find_toplevel_string (const char *haystack
, const char *needle
)
1357 const char *s
= haystack
;
1361 s
= find_toplevel_char (s
, *needle
);
1365 /* Found first char in HAYSTACK; check rest of string. */
1366 if (startswith (s
, needle
))
1369 /* Didn't find it; loop over HAYSTACK, looking for the next
1370 instance of the first character of NEEDLE. */
1374 while (s
!= NULL
&& *s
!= '\0');
1376 /* NEEDLE was not found in HAYSTACK. */
1380 /* Convert CANONICAL to its string representation using
1381 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1384 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1386 if (canonical
->symtab
== NULL
)
1387 return xstrdup (canonical
->suffix
);
1389 return xstrprintf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1393 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1394 and store the result in SELF->CANONICAL. */
1397 filter_results (struct linespec_state
*self
,
1398 std::vector
<symtab_and_line
> *result
,
1399 VEC (const_char_ptr
) *filters
)
1404 for (i
= 0; VEC_iterate (const_char_ptr
, filters
, i
, name
); ++i
)
1408 for (size_t j
= 0; j
< result
->size (); ++j
)
1410 const struct linespec_canonical_name
*canonical
;
1412 struct cleanup
*cleanup
;
1414 canonical
= &self
->canonical_names
[j
];
1415 fullform
= canonical_to_fullform (canonical
);
1416 cleanup
= make_cleanup (xfree
, fullform
);
1418 if (strcmp (name
, fullform
) == 0)
1419 lsal
.sals
.push_back ((*result
)[j
]);
1421 do_cleanups (cleanup
);
1424 if (!lsal
.sals
.empty ())
1426 lsal
.canonical
= xstrdup (name
);
1427 self
->canonical
->lsals
.push_back (std::move (lsal
));
1431 self
->canonical
->pre_expanded
= 0;
1434 /* Store RESULT into SELF->CANONICAL. */
1437 convert_results_to_lsals (struct linespec_state
*self
,
1438 std::vector
<symtab_and_line
> *result
)
1440 struct linespec_sals lsal
;
1442 lsal
.canonical
= NULL
;
1443 lsal
.sals
= std::move (*result
);
1444 self
->canonical
->lsals
.push_back (std::move (lsal
));
1447 /* A structure that contains two string representations of a struct
1448 linespec_canonical_name:
1449 - one where the the symtab's fullname is used;
1450 - one where the filename followed the "set filename-display"
1453 struct decode_line_2_item
1455 /* The form using symtab_to_fullname.
1456 It must be xfree'ed after use. */
1459 /* The form using symtab_to_filename_for_display.
1460 It must be xfree'ed after use. */
1463 /* Field is initialized to zero and it is set to one if the user
1464 requested breakpoint for this entry. */
1465 unsigned int selected
: 1;
1468 /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1469 secondarily by FULLFORM. */
1472 decode_line_2_compare_items (const void *ap
, const void *bp
)
1474 const struct decode_line_2_item
*a
= (const struct decode_line_2_item
*) ap
;
1475 const struct decode_line_2_item
*b
= (const struct decode_line_2_item
*) bp
;
1478 retval
= strcmp (a
->displayform
, b
->displayform
);
1482 return strcmp (a
->fullform
, b
->fullform
);
1485 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1486 will either return normally, throw an exception on multiple
1487 results, or present a menu to the user. On return, the SALS vector
1488 in SELF->CANONICAL is set up properly. */
1491 decode_line_2 (struct linespec_state
*self
,
1492 std::vector
<symtab_and_line
> *result
,
1493 const char *select_mode
)
1498 struct cleanup
*old_chain
;
1499 VEC (const_char_ptr
) *filters
= NULL
;
1500 struct decode_line_2_item
*items
;
1503 gdb_assert (select_mode
!= multiple_symbols_all
);
1504 gdb_assert (self
->canonical
!= NULL
);
1505 gdb_assert (!result
->empty ());
1507 old_chain
= make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
1509 /* Prepare ITEMS array. */
1510 items_count
= result
->size ();
1511 items
= XNEWVEC (struct decode_line_2_item
, items_count
);
1512 make_cleanup (xfree
, items
);
1513 for (i
= 0; i
< items_count
; ++i
)
1515 const struct linespec_canonical_name
*canonical
;
1516 struct decode_line_2_item
*item
;
1518 canonical
= &self
->canonical_names
[i
];
1519 gdb_assert (canonical
->suffix
!= NULL
);
1522 item
->fullform
= canonical_to_fullform (canonical
);
1523 make_cleanup (xfree
, item
->fullform
);
1525 if (canonical
->symtab
== NULL
)
1526 item
->displayform
= canonical
->suffix
;
1529 const char *fn_for_display
;
1531 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1532 item
->displayform
= xstrprintf ("%s:%s", fn_for_display
,
1534 make_cleanup (xfree
, item
->displayform
);
1540 /* Sort the list of method names. */
1541 qsort (items
, items_count
, sizeof (*items
), decode_line_2_compare_items
);
1543 /* Remove entries with the same FULLFORM. */
1544 if (items_count
>= 2)
1546 struct decode_line_2_item
*dst
, *src
;
1549 for (src
= &items
[1]; src
< &items
[items_count
]; src
++)
1550 if (strcmp (src
->fullform
, dst
->fullform
) != 0)
1552 items_count
= dst
+ 1 - items
;
1555 if (select_mode
== multiple_symbols_cancel
&& items_count
> 1)
1556 error (_("canceled because the command is ambiguous\n"
1557 "See set/show multiple-symbol."));
1559 if (select_mode
== multiple_symbols_all
|| items_count
== 1)
1561 do_cleanups (old_chain
);
1562 convert_results_to_lsals (self
, result
);
1566 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1567 for (i
= 0; i
< items_count
; i
++)
1568 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
);
1570 prompt
= getenv ("PS2");
1575 args
= command_line_input (prompt
, 0, "overload-choice");
1577 if (args
== 0 || *args
== 0)
1578 error_no_arg (_("one or more choice numbers"));
1580 number_or_range_parser
parser (args
);
1581 while (!parser
.finished ())
1583 int num
= parser
.get_number ();
1586 error (_("canceled"));
1589 /* We intentionally make this result in a single breakpoint,
1590 contrary to what older versions of gdb did. The
1591 rationale is that this lets a user get the
1592 multiple_symbols_all behavior even with the 'ask'
1593 setting; and he can get separate breakpoints by entering
1594 "2-57" at the query. */
1595 do_cleanups (old_chain
);
1596 convert_results_to_lsals (self
, result
);
1601 if (num
>= items_count
)
1602 printf_unfiltered (_("No choice number %d.\n"), num
);
1605 struct decode_line_2_item
*item
= &items
[num
];
1607 if (!item
->selected
)
1609 VEC_safe_push (const_char_ptr
, filters
, item
->fullform
);
1614 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1620 filter_results (self
, result
, filters
);
1621 do_cleanups (old_chain
);
1626 /* The parser of linespec itself. */
1628 /* Throw an appropriate error when SYMBOL is not found (optionally in
1631 static void ATTRIBUTE_NORETURN
1632 symbol_not_found_error (const char *symbol
, const char *filename
)
1637 if (!have_full_symbols ()
1638 && !have_partial_symbols ()
1639 && !have_minimal_symbols ())
1640 throw_error (NOT_FOUND_ERROR
,
1641 _("No symbol table is loaded. Use the \"file\" command."));
1643 /* If SYMBOL starts with '$', the user attempted to either lookup
1644 a function/variable in his code starting with '$' or an internal
1645 variable of that name. Since we do not know which, be concise and
1646 explain both possibilities. */
1650 throw_error (NOT_FOUND_ERROR
,
1651 _("Undefined convenience variable or function \"%s\" "
1652 "not defined in \"%s\"."), symbol
, filename
);
1654 throw_error (NOT_FOUND_ERROR
,
1655 _("Undefined convenience variable or function \"%s\" "
1656 "not defined."), symbol
);
1661 throw_error (NOT_FOUND_ERROR
,
1662 _("Function \"%s\" not defined in \"%s\"."),
1665 throw_error (NOT_FOUND_ERROR
,
1666 _("Function \"%s\" not defined."), symbol
);
1670 /* Throw an appropriate error when an unexpected token is encountered
1673 static void ATTRIBUTE_NORETURN
1674 unexpected_linespec_error (linespec_parser
*parser
)
1676 linespec_token token
;
1677 static const char * token_type_strings
[]
1678 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1680 /* Get the token that generated the error. */
1681 token
= linespec_lexer_lex_one (parser
);
1683 /* Finally, throw the error. */
1684 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1685 || token
.type
== LSTOKEN_KEYWORD
)
1689 string
= copy_token_string (token
);
1690 make_cleanup (xfree
, string
);
1691 throw_error (GENERIC_ERROR
,
1692 _("malformed linespec error: unexpected %s, \"%s\""),
1693 token_type_strings
[token
.type
], string
);
1696 throw_error (GENERIC_ERROR
,
1697 _("malformed linespec error: unexpected %s"),
1698 token_type_strings
[token
.type
]);
1701 /* Throw an undefined label error. */
1703 static void ATTRIBUTE_NORETURN
1704 undefined_label_error (const char *function
, const char *label
)
1706 if (function
!= NULL
)
1707 throw_error (NOT_FOUND_ERROR
,
1708 _("No label \"%s\" defined in function \"%s\"."),
1711 throw_error (NOT_FOUND_ERROR
,
1712 _("No label \"%s\" defined in current function."),
1716 /* Throw a source file not found error. */
1718 static void ATTRIBUTE_NORETURN
1719 source_file_not_found_error (const char *name
)
1721 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), name
);
1724 /* Unless at EIO, save the current stream position as completion word
1725 point, and consume the next token. */
1727 static linespec_token
1728 save_stream_and_consume_token (linespec_parser
*parser
)
1730 if (linespec_lexer_peek_token (parser
).type
!= LSTOKEN_EOI
)
1731 parser
->completion_word
= PARSER_STREAM (parser
);
1732 return linespec_lexer_consume_token (parser
);
1735 /* See description in linespec.h. */
1738 linespec_parse_line_offset (const char *string
)
1740 const char *start
= string
;
1741 struct line_offset line_offset
= {0, LINE_OFFSET_NONE
};
1745 line_offset
.sign
= LINE_OFFSET_PLUS
;
1748 else if (*string
== '-')
1750 line_offset
.sign
= LINE_OFFSET_MINUS
;
1754 if (*string
!= '\0' && !isdigit (*string
))
1755 error (_("malformed line offset: \"%s\""), start
);
1757 /* Right now, we only allow base 10 for offsets. */
1758 line_offset
.offset
= atoi (string
);
1762 /* In completion mode, if the user is still typing the number, there's
1763 no possible completion to offer. But if there's already input past
1764 the number, setup to expect NEXT. */
1767 set_completion_after_number (linespec_parser
*parser
,
1768 linespec_complete_what next
)
1770 if (*PARSER_STREAM (parser
) == ' ')
1772 parser
->completion_word
= skip_spaces_const (PARSER_STREAM (parser
) + 1);
1773 parser
->complete_what
= next
;
1777 parser
->completion_word
= PARSER_STREAM (parser
);
1778 parser
->complete_what
= linespec_complete_what::NOTHING
;
1782 /* Parse the basic_spec in PARSER's input. */
1785 linespec_parse_basic (linespec_parser
*parser
)
1788 linespec_token token
;
1789 VEC (symbolp
) *symbols
, *labels
;
1790 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
1791 struct cleanup
*cleanup
;
1793 /* Get the next token. */
1794 token
= linespec_lexer_lex_one (parser
);
1796 /* If it is EOI or KEYWORD, issue an error. */
1797 if (token
.type
== LSTOKEN_KEYWORD
)
1799 parser
->complete_what
= linespec_complete_what::NOTHING
;
1800 unexpected_linespec_error (parser
);
1802 else if (token
.type
== LSTOKEN_EOI
)
1804 unexpected_linespec_error (parser
);
1806 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1807 else if (token
.type
== LSTOKEN_NUMBER
)
1809 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1811 /* Record the line offset and get the next token. */
1812 name
= copy_token_string (token
);
1813 cleanup
= make_cleanup (xfree
, name
);
1814 PARSER_EXPLICIT (parser
)->line_offset
= linespec_parse_line_offset (name
);
1815 do_cleanups (cleanup
);
1817 /* Get the next token. */
1818 token
= linespec_lexer_consume_token (parser
);
1820 /* If the next token is a comma, stop parsing and return. */
1821 if (token
.type
== LSTOKEN_COMMA
)
1823 parser
->complete_what
= linespec_complete_what::NOTHING
;
1827 /* If the next token is anything but EOI or KEYWORD, issue
1829 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1830 unexpected_linespec_error (parser
);
1833 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1836 /* Next token must be LSTOKEN_STRING. */
1837 if (token
.type
!= LSTOKEN_STRING
)
1839 parser
->complete_what
= linespec_complete_what::NOTHING
;
1840 unexpected_linespec_error (parser
);
1843 /* The current token will contain the name of a function, method,
1845 name
= copy_token_string (token
);
1846 cleanup
= make_cleanup (free_current_contents
, &name
);
1848 if (parser
->completion_tracker
!= NULL
)
1850 /* If the function name ends with a ":", then this may be an
1851 incomplete "::" scope operator instead of a label separator.
1854 which should expand to:
1857 Do a tentative completion assuming the later. If we find
1858 completions, advance the stream past the colon token and make
1859 it part of the function name/token. */
1861 if (!parser
->completion_quote_char
1862 && strcmp (PARSER_STREAM (parser
), ":") == 0)
1864 completion_tracker tmp_tracker
;
1865 const char *source_filename
1866 = PARSER_EXPLICIT (parser
)->source_filename
;
1868 linespec_complete_function (tmp_tracker
,
1869 parser
->completion_word
,
1872 if (tmp_tracker
.have_completions ())
1874 PARSER_STREAM (parser
)++;
1875 LS_TOKEN_STOKEN (token
).length
++;
1878 name
= savestring (parser
->completion_word
,
1879 (PARSER_STREAM (parser
)
1880 - parser
->completion_word
));
1884 PARSER_EXPLICIT (parser
)->function_name
= name
;
1885 discard_cleanups (cleanup
);
1889 /* XXX Reindent before pushing. */
1891 /* Try looking it up as a function/method. */
1892 find_linespec_symbols (PARSER_STATE (parser
),
1893 PARSER_RESULT (parser
)->file_symtabs
, name
,
1894 &symbols
, &minimal_symbols
);
1896 if (symbols
!= NULL
|| minimal_symbols
!= NULL
)
1898 PARSER_RESULT (parser
)->function_symbols
= symbols
;
1899 PARSER_RESULT (parser
)->minimal_symbols
= minimal_symbols
;
1900 PARSER_EXPLICIT (parser
)->function_name
= name
;
1902 discard_cleanups (cleanup
);
1906 /* NAME was not a function or a method. So it must be a label
1907 name or user specified variable like "break foo.c:$zippo". */
1908 labels
= find_label_symbols (PARSER_STATE (parser
), NULL
,
1912 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1913 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1914 PARSER_EXPLICIT (parser
)->label_name
= name
;
1916 discard_cleanups (cleanup
);
1918 else if (token
.type
== LSTOKEN_STRING
1919 && *LS_TOKEN_STOKEN (token
).ptr
== '$')
1921 /* User specified a convenience variable or history value. */
1922 PARSER_EXPLICIT (parser
)->line_offset
1923 = linespec_parse_variable (PARSER_STATE (parser
), name
);
1925 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
)
1927 /* The user-specified variable was not valid. Do not
1928 throw an error here. parse_linespec will do it for us. */
1929 PARSER_EXPLICIT (parser
)->function_name
= name
;
1930 discard_cleanups (cleanup
);
1934 /* The convenience variable/history value parsed correctly.
1935 NAME is no longer needed. */
1936 do_cleanups (cleanup
);
1940 /* The name is also not a label. Abort parsing. Do not throw
1941 an error here. parse_linespec will do it for us. */
1943 /* Save a copy of the name we were trying to lookup. */
1944 PARSER_EXPLICIT (parser
)->function_name
= name
;
1945 discard_cleanups (cleanup
);
1951 int previous_qc
= parser
->completion_quote_char
;
1953 /* Get the next token. */
1954 token
= linespec_lexer_consume_token (parser
);
1956 if (token
.type
== LSTOKEN_EOI
)
1958 if (previous_qc
&& !parser
->completion_quote_char
)
1959 parser
->complete_what
= linespec_complete_what::KEYWORD
;
1961 else if (token
.type
== LSTOKEN_COLON
)
1963 /* User specified a label or a lineno. */
1964 token
= linespec_lexer_consume_token (parser
);
1966 if (token
.type
== LSTOKEN_NUMBER
)
1968 /* User specified an offset. Record the line offset and
1969 get the next token. */
1970 set_completion_after_number (parser
, linespec_complete_what::KEYWORD
);
1972 name
= copy_token_string (token
);
1973 cleanup
= make_cleanup (xfree
, name
);
1974 PARSER_EXPLICIT (parser
)->line_offset
1975 = linespec_parse_line_offset (name
);
1976 do_cleanups (cleanup
);
1978 /* Get the next token. */
1979 token
= linespec_lexer_consume_token (parser
);
1981 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
1983 parser
->complete_what
= linespec_complete_what::LABEL
;
1985 else if (token
.type
== LSTOKEN_STRING
)
1987 parser
->complete_what
= linespec_complete_what::LABEL
;
1989 /* If we have text after the label separated by whitespace
1990 (e.g., "b func():lab i<tab>"), don't consider it part of
1991 the label. In completion mode that should complete to
1992 "if", in normal mode, the 'i' should be treated as
1994 if (parser
->completion_quote_char
== '\0')
1996 const char *ptr
= LS_TOKEN_STOKEN (token
).ptr
;
1997 for (size_t i
= 0; i
< LS_TOKEN_STOKEN (token
).length
; i
++)
2001 LS_TOKEN_STOKEN (token
).length
= i
;
2002 PARSER_STREAM (parser
) = skip_spaces_const (ptr
+ i
+ 1);
2008 if (parser
->completion_tracker
!= NULL
)
2010 if (PARSER_STREAM (parser
)[-1] == ' ')
2012 parser
->completion_word
= PARSER_STREAM (parser
);
2013 parser
->complete_what
= linespec_complete_what::KEYWORD
;
2018 /* XXX Reindent before pushing. */
2020 /* Grab a copy of the label's name and look it up. */
2021 name
= copy_token_string (token
);
2022 cleanup
= make_cleanup (xfree
, name
);
2023 labels
= find_label_symbols (PARSER_STATE (parser
),
2024 PARSER_RESULT (parser
)->function_symbols
,
2029 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
2030 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
2031 PARSER_EXPLICIT (parser
)->label_name
= name
;
2033 discard_cleanups (cleanup
);
2037 /* We don't know what it was, but it isn't a label. */
2038 undefined_label_error (PARSER_EXPLICIT (parser
)->function_name
,
2044 /* Check for a line offset. */
2045 token
= save_stream_and_consume_token (parser
);
2046 if (token
.type
== LSTOKEN_COLON
)
2048 /* Get the next token. */
2049 token
= linespec_lexer_consume_token (parser
);
2051 /* It must be a line offset. */
2052 if (token
.type
!= LSTOKEN_NUMBER
)
2053 unexpected_linespec_error (parser
);
2055 /* Record the line offset and get the next token. */
2056 name
= copy_token_string (token
);
2057 cleanup
= make_cleanup (xfree
, name
);
2059 PARSER_EXPLICIT (parser
)->line_offset
2060 = linespec_parse_line_offset (name
);
2061 do_cleanups (cleanup
);
2063 /* Get the next token. */
2064 token
= linespec_lexer_consume_token (parser
);
2069 /* Trailing ':' in the input. Issue an error. */
2070 unexpected_linespec_error (parser
);
2075 /* Canonicalize the linespec contained in LS. The result is saved into
2076 STATE->canonical. This function handles both linespec and explicit
2080 canonicalize_linespec (struct linespec_state
*state
, const linespec_p ls
)
2082 struct event_location
*canon
;
2083 struct explicit_location
*explicit_loc
;
2085 /* If canonicalization was not requested, no need to do anything. */
2086 if (!state
->canonical
)
2089 /* Save everything as an explicit location. */
2090 state
->canonical
->location
2091 = new_explicit_location (&ls
->explicit_loc
);
2092 canon
= state
->canonical
->location
.get ();
2093 explicit_loc
= get_explicit_location (canon
);
2095 if (explicit_loc
->label_name
!= NULL
)
2097 state
->canonical
->special_display
= 1;
2099 if (explicit_loc
->function_name
== NULL
)
2103 /* No function was specified, so add the symbol name. */
2104 gdb_assert (ls
->labels
.function_symbols
!= NULL
2105 && (VEC_length (symbolp
, ls
->labels
.function_symbols
)
2107 s
= VEC_index (symbolp
, ls
->labels
.function_symbols
, 0);
2108 explicit_loc
->function_name
= xstrdup (SYMBOL_NATURAL_NAME (s
));
2112 /* If this location originally came from a linespec, save a string
2113 representation of it for display and saving to file. */
2114 if (state
->is_linespec
)
2116 char *linespec
= explicit_location_to_linespec (explicit_loc
);
2118 set_event_location_string (canon
, linespec
);
2123 /* Given a line offset in LS, construct the relevant SALs. */
2125 static std::vector
<symtab_and_line
>
2126 create_sals_line_offset (struct linespec_state
*self
,
2129 struct symtab_and_line val
;
2130 int use_default
= 0;
2134 /* This is where we need to make sure we have good defaults.
2135 We must guarantee that this section of code is never executed
2136 when we are called with just a function name, since
2137 set_default_source_symtab_and_line uses
2138 select_source_symtab that calls us with such an argument. */
2140 if (VEC_length (symtab_ptr
, ls
->file_symtabs
) == 1
2141 && VEC_index (symtab_ptr
, ls
->file_symtabs
, 0) == NULL
)
2143 const char *fullname
;
2145 set_current_program_space (self
->program_space
);
2147 /* Make sure we have at least a default source line. */
2148 set_default_source_symtab_and_line ();
2149 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
2150 fullname
= symtab_to_fullname (self
->default_symtab
);
2151 VEC_pop (symtab_ptr
, ls
->file_symtabs
);
2152 VEC_free (symtab_ptr
, ls
->file_symtabs
);
2153 ls
->file_symtabs
= collect_symtabs_from_filename (fullname
,
2154 self
->search_pspace
);
2158 val
.line
= ls
->explicit_loc
.line_offset
.offset
;
2159 switch (ls
->explicit_loc
.line_offset
.sign
)
2161 case LINE_OFFSET_PLUS
:
2162 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2165 val
.line
= self
->default_line
+ val
.line
;
2168 case LINE_OFFSET_MINUS
:
2169 if (ls
->explicit_loc
.line_offset
.offset
== 0)
2172 val
.line
= self
->default_line
- val
.line
;
2174 val
.line
= -val
.line
;
2177 case LINE_OFFSET_NONE
:
2178 break; /* No need to adjust val.line. */
2181 std::vector
<symtab_and_line
> values
;
2182 if (self
->list_mode
)
2183 values
= decode_digits_list_mode (self
, ls
, val
);
2186 struct linetable_entry
*best_entry
= NULL
;
2188 const struct block
**blocks
;
2191 std::vector
<symtab_and_line
> intermediate_results
2192 = decode_digits_ordinary (self
, ls
, val
.line
, &best_entry
);
2193 if (intermediate_results
.empty () && best_entry
!= NULL
)
2194 intermediate_results
= decode_digits_ordinary (self
, ls
,
2198 /* For optimized code, the compiler can scatter one source line
2199 across disjoint ranges of PC values, even when no duplicate
2200 functions or inline functions are involved. For example,
2201 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2202 function can result in two PC ranges. In this case, we don't
2203 want to set a breakpoint on the first PC of each range. To filter
2204 such cases, we use containing blocks -- for each PC found
2205 above, we see if there are other PCs that are in the same
2206 block. If yes, the other PCs are filtered out. */
2208 filter
= XNEWVEC (int, intermediate_results
.size ());
2209 struct cleanup
*cleanup
= make_cleanup (xfree
, filter
);
2210 blocks
= XNEWVEC (const struct block
*, intermediate_results
.size ());
2211 make_cleanup (xfree
, blocks
);
2213 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2215 set_current_program_space (intermediate_results
[i
].pspace
);
2218 blocks
[i
] = block_for_pc_sect (intermediate_results
[i
].pc
,
2219 intermediate_results
[i
].section
);
2222 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2224 if (blocks
[i
] != NULL
)
2225 for (j
= i
+ 1; j
< intermediate_results
.size (); ++j
)
2227 if (blocks
[j
] == blocks
[i
])
2235 for (i
= 0; i
< intermediate_results
.size (); ++i
)
2238 struct symbol
*sym
= (blocks
[i
]
2239 ? block_containing_function (blocks
[i
])
2242 if (self
->funfirstline
)
2243 skip_prologue_sal (&intermediate_results
[i
]);
2244 /* Make sure the line matches the request, not what was
2246 intermediate_results
[i
].line
= val
.line
;
2247 add_sal_to_sals (self
, &values
, &intermediate_results
[i
],
2248 sym
? SYMBOL_NATURAL_NAME (sym
) : NULL
, 0);
2251 do_cleanups (cleanup
);
2254 if (values
.empty ())
2256 if (ls
->explicit_loc
.source_filename
)
2257 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
2258 val
.line
, ls
->explicit_loc
.source_filename
);
2260 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
2267 /* Convert the given ADDRESS into SaLs. */
2269 static std::vector
<symtab_and_line
>
2270 convert_address_location_to_sals (struct linespec_state
*self
,
2273 symtab_and_line sal
= find_pc_line (address
, 0);
2275 sal
.section
= find_pc_overlay (address
);
2276 sal
.explicit_pc
= 1;
2278 std::vector
<symtab_and_line
> sals
;
2279 add_sal_to_sals (self
, &sals
, &sal
, core_addr_to_string (address
), 1);
2284 /* Create and return SALs from the linespec LS. */
2286 static std::vector
<symtab_and_line
>
2287 convert_linespec_to_sals (struct linespec_state
*state
, linespec_p ls
)
2289 std::vector
<symtab_and_line
> sals
;
2291 if (ls
->labels
.label_symbols
!= NULL
)
2293 /* We have just a bunch of functions/methods or labels. */
2295 struct symtab_and_line sal
;
2298 for (i
= 0; VEC_iterate (symbolp
, ls
->labels
.label_symbols
, i
, sym
); ++i
)
2300 struct program_space
*pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2302 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
2303 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2304 add_sal_to_sals (state
, &sals
, &sal
,
2305 SYMBOL_NATURAL_NAME (sym
), 0);
2308 else if (ls
->function_symbols
!= NULL
|| ls
->minimal_symbols
!= NULL
)
2310 /* We have just a bunch of functions and/or methods. */
2312 struct symtab_and_line sal
;
2314 bound_minimal_symbol_d
*elem
;
2315 struct program_space
*pspace
;
2317 if (ls
->function_symbols
!= NULL
)
2319 /* Sort symbols so that symbols with the same program space are next
2321 qsort (VEC_address (symbolp
, ls
->function_symbols
),
2322 VEC_length (symbolp
, ls
->function_symbols
),
2323 sizeof (symbolp
), compare_symbols
);
2325 for (i
= 0; VEC_iterate (symbolp
, ls
->function_symbols
, i
, sym
); ++i
)
2327 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2328 set_current_program_space (pspace
);
2329 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
2330 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2331 add_sal_to_sals (state
, &sals
, &sal
,
2332 SYMBOL_NATURAL_NAME (sym
), 0);
2336 if (ls
->minimal_symbols
!= NULL
)
2338 /* Sort minimal symbols by program space, too. */
2339 qsort (VEC_address (bound_minimal_symbol_d
, ls
->minimal_symbols
),
2340 VEC_length (bound_minimal_symbol_d
, ls
->minimal_symbols
),
2341 sizeof (bound_minimal_symbol_d
), compare_msymbols
);
2344 VEC_iterate (bound_minimal_symbol_d
, ls
->minimal_symbols
,
2348 pspace
= elem
->objfile
->pspace
;
2349 set_current_program_space (pspace
);
2350 minsym_found (state
, elem
->objfile
, elem
->minsym
, &sals
);
2354 else if (ls
->explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2356 /* Only an offset was specified. */
2357 sals
= create_sals_line_offset (state
, ls
);
2359 /* Make sure we have a filename for canonicalization. */
2360 if (ls
->explicit_loc
.source_filename
== NULL
)
2362 const char *fullname
= symtab_to_fullname (state
->default_symtab
);
2364 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2365 form so that displaying SOURCE_FILENAME can follow the current
2366 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2367 it has been kept for code simplicity only in absolute form. */
2368 ls
->explicit_loc
.source_filename
= xstrdup (fullname
);
2373 /* We haven't found any results... */
2377 canonicalize_linespec (state
, ls
);
2379 if (!sals
.empty () && state
->canonical
!= NULL
)
2380 state
->canonical
->pre_expanded
= 1;
2385 /* Build RESULT from the explicit location components SOURCE_FILENAME,
2386 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
2389 convert_explicit_location_to_linespec (struct linespec_state
*self
,
2391 const char *source_filename
,
2392 const char *function_name
,
2393 const char *label_name
,
2394 struct line_offset line_offset
)
2396 VEC (symbolp
) *symbols
, *labels
;
2397 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
2399 if (source_filename
!= NULL
)
2403 result
->file_symtabs
2404 = symtabs_from_filename (source_filename
, self
->search_pspace
);
2406 CATCH (except
, RETURN_MASK_ERROR
)
2408 source_file_not_found_error (source_filename
);
2411 result
->explicit_loc
.source_filename
= xstrdup (source_filename
);
2415 /* A NULL entry means to use the default symtab. */
2416 VEC_safe_push (symtab_ptr
, result
->file_symtabs
, NULL
);
2419 if (function_name
!= NULL
)
2421 find_linespec_symbols (self
, result
->file_symtabs
,
2422 function_name
, &symbols
,
2425 if (symbols
== NULL
&& minimal_symbols
== NULL
)
2426 symbol_not_found_error (function_name
,
2427 result
->explicit_loc
.source_filename
);
2429 result
->explicit_loc
.function_name
= xstrdup (function_name
);
2430 result
->function_symbols
= symbols
;
2431 result
->minimal_symbols
= minimal_symbols
;
2434 if (label_name
!= NULL
)
2437 labels
= find_label_symbols (self
, result
->function_symbols
,
2438 &symbols
, label_name
);
2441 undefined_label_error (result
->explicit_loc
.function_name
,
2444 result
->explicit_loc
.label_name
= xstrdup (label_name
);
2445 result
->labels
.label_symbols
= labels
;
2446 result
->labels
.function_symbols
= symbols
;
2449 if (line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2450 result
->explicit_loc
.line_offset
= line_offset
;
2453 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2455 static std::vector
<symtab_and_line
>
2456 convert_explicit_location_to_sals (struct linespec_state
*self
,
2458 const struct explicit_location
*explicit_loc
)
2460 convert_explicit_location_to_linespec (self
, result
,
2461 explicit_loc
->source_filename
,
2462 explicit_loc
->function_name
,
2463 explicit_loc
->label_name
,
2464 explicit_loc
->line_offset
);
2465 return convert_linespec_to_sals (self
, result
);
2468 /* Parse a string that specifies a linespec.
2470 The basic grammar of linespecs:
2472 linespec -> var_spec | basic_spec
2473 var_spec -> '$' (STRING | NUMBER)
2475 basic_spec -> file_offset_spec | function_spec | label_spec
2476 file_offset_spec -> opt_file_spec offset_spec
2477 function_spec -> opt_file_spec function_name_spec opt_label_spec
2478 label_spec -> label_name_spec
2480 opt_file_spec -> "" | file_name_spec ':'
2481 opt_label_spec -> "" | ':' label_name_spec
2483 file_name_spec -> STRING
2484 function_name_spec -> STRING
2485 label_name_spec -> STRING
2486 function_name_spec -> STRING
2487 offset_spec -> NUMBER
2491 This may all be followed by several keywords such as "if EXPR",
2494 A comma will terminate parsing.
2496 The function may be an undebuggable function found in minimal symbol table.
2498 If the argument FUNFIRSTLINE is nonzero, we want the first line
2499 of real code inside a function when a function is specified, and it is
2500 not OK to specify a variable or type to get its line number.
2502 DEFAULT_SYMTAB specifies the file to use if none is specified.
2503 It defaults to current_source_symtab.
2504 DEFAULT_LINE specifies the line number to use for relative
2505 line numbers (that start with signs). Defaults to current_source_line.
2506 If CANONICAL is non-NULL, store an array of strings containing the canonical
2507 line specs there if necessary. Currently overloaded member functions and
2508 line numbers or static functions without a filename yield a canonical
2509 line spec. The array and the line spec strings are allocated on the heap,
2510 it is the callers responsibility to free them.
2512 Note that it is possible to return zero for the symtab
2513 if no file is validly specified. Callers must check that.
2514 Also, the line number returned may be invalid. */
2516 /* Parse the linespec in ARG. */
2518 static std::vector
<symtab_and_line
>
2519 parse_linespec (linespec_parser
*parser
, const char *arg
)
2521 linespec_token token
;
2522 struct gdb_exception file_exception
= exception_none
;
2523 struct cleanup
*cleanup
;
2525 /* A special case to start. It has become quite popular for
2526 IDEs to work around bugs in the previous parser by quoting
2527 the entire linespec, so we attempt to deal with this nicely. */
2528 parser
->is_quote_enclosed
= 0;
2529 if (parser
->completion_tracker
== NULL
2530 && !is_ada_operator (arg
)
2531 && strchr (linespec_quote_characters
, *arg
) != NULL
)
2535 end
= skip_quote_char (arg
+ 1, *arg
);
2536 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2538 /* Here's the special case. Skip ARG past the initial
2541 parser
->is_quote_enclosed
= 1;
2545 parser
->lexer
.saved_arg
= arg
;
2546 parser
->lexer
.stream
= arg
;
2547 parser
->completion_word
= arg
;
2548 parser
->complete_what
= linespec_complete_what::FUNCTION
;
2550 /* Initialize the default symtab and line offset. */
2551 initialize_defaults (&PARSER_STATE (parser
)->default_symtab
,
2552 &PARSER_STATE (parser
)->default_line
);
2554 /* Objective-C shortcut. */
2555 if (parser
->completion_tracker
== NULL
)
2557 std::vector
<symtab_and_line
> values
2558 = decode_objc (PARSER_STATE (parser
), PARSER_RESULT (parser
), arg
);
2559 if (!values
.empty ())
2564 /* "-"/"+" is either an objc selector, or a number. There's
2565 nothing to complete the latter to, so just let the caller
2566 complete on functions, which finds objc selectors, if there's
2568 if ((arg
[0] == '-' || arg
[0] == '+') && arg
[1] == '\0')
2572 /* Start parsing. */
2574 /* Get the first token. */
2575 token
= linespec_lexer_consume_token (parser
);
2577 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2578 if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '$')
2582 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2583 if (parser
->completion_tracker
== NULL
)
2584 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2586 /* User specified a convenience variable or history value. */
2587 var
= copy_token_string (token
);
2588 cleanup
= make_cleanup (xfree
, var
);
2589 PARSER_EXPLICIT (parser
)->line_offset
2590 = linespec_parse_variable (PARSER_STATE (parser
), var
);
2591 do_cleanups (cleanup
);
2593 /* If a line_offset wasn't found (VAR is the name of a user
2594 variable/function), then skip to normal symbol processing. */
2595 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2597 /* Consume this token. */
2598 linespec_lexer_consume_token (parser
);
2600 goto convert_to_sals
;
2603 else if (token
.type
== LSTOKEN_EOI
&& parser
->completion_tracker
!= NULL
)
2605 /* Let the default linespec_complete_what::FUNCTION kick in. */
2606 unexpected_linespec_error (parser
);
2608 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2610 parser
->complete_what
= linespec_complete_what::NOTHING
;
2611 unexpected_linespec_error (parser
);
2614 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2615 this token cannot represent a filename. */
2616 token
= linespec_lexer_peek_token (parser
);
2618 if (token
.type
== LSTOKEN_COLON
)
2620 char *user_filename
;
2622 /* Get the current token again and extract the filename. */
2623 token
= linespec_lexer_lex_one (parser
);
2624 user_filename
= copy_token_string (token
);
2626 /* Check if the input is a filename. */
2629 PARSER_RESULT (parser
)->file_symtabs
2630 = symtabs_from_filename (user_filename
,
2631 PARSER_STATE (parser
)->search_pspace
);
2633 CATCH (ex
, RETURN_MASK_ERROR
)
2635 file_exception
= ex
;
2639 if (file_exception
.reason
>= 0)
2641 /* Symtabs were found for the file. Record the filename. */
2642 PARSER_EXPLICIT (parser
)->source_filename
= user_filename
;
2644 /* Get the next token. */
2645 token
= linespec_lexer_consume_token (parser
);
2647 /* This is LSTOKEN_COLON; consume it. */
2648 linespec_lexer_consume_token (parser
);
2652 /* No symtabs found -- discard user_filename. */
2653 xfree (user_filename
);
2655 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2656 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2659 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2660 else if (parser
->completion_tracker
== NULL
2661 && (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2662 && token
.type
!= LSTOKEN_COMMA
))
2664 /* TOKEN is the _next_ token, not the one currently in the parser.
2665 Consuming the token will give the correct error message. */
2666 linespec_lexer_consume_token (parser
);
2667 unexpected_linespec_error (parser
);
2671 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2672 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2675 /* Parse the rest of the linespec. */
2676 linespec_parse_basic (parser
);
2678 if (parser
->completion_tracker
== NULL
2679 && PARSER_RESULT (parser
)->function_symbols
== NULL
2680 && PARSER_RESULT (parser
)->labels
.label_symbols
== NULL
2681 && PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
2682 && PARSER_RESULT (parser
)->minimal_symbols
== NULL
)
2684 /* The linespec didn't parse. Re-throw the file exception if
2686 if (file_exception
.reason
< 0)
2687 throw_exception (file_exception
);
2689 /* Otherwise, the symbol is not found. */
2690 symbol_not_found_error (PARSER_EXPLICIT (parser
)->function_name
,
2691 PARSER_EXPLICIT (parser
)->source_filename
);
2696 /* Get the last token and record how much of the input was parsed,
2698 token
= linespec_lexer_lex_one (parser
);
2699 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2700 unexpected_linespec_error (parser
);
2701 else if (token
.type
== LSTOKEN_KEYWORD
)
2703 /* Setup the completion word past the keyword. Lexing never
2704 advances past a keyword automatically, so skip it
2706 parser
->completion_word
2707 = skip_spaces_const (skip_to_space_const (PARSER_STREAM (parser
)));
2708 parser
->complete_what
= linespec_complete_what::EXPRESSION
;
2711 /* Convert the data in PARSER_RESULT to SALs. */
2712 if (parser
->completion_tracker
== NULL
)
2713 return convert_linespec_to_sals (PARSER_STATE (parser
),
2714 PARSER_RESULT (parser
));
2720 /* A constructor for linespec_state. */
2723 linespec_state_constructor (struct linespec_state
*self
,
2724 int flags
, const struct language_defn
*language
,
2725 struct program_space
*search_pspace
,
2726 struct symtab
*default_symtab
,
2728 struct linespec_result
*canonical
)
2730 memset (self
, 0, sizeof (*self
));
2731 self
->language
= language
;
2732 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2733 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2734 self
->search_pspace
= search_pspace
;
2735 self
->default_symtab
= default_symtab
;
2736 self
->default_line
= default_line
;
2737 self
->canonical
= canonical
;
2738 self
->program_space
= current_program_space
;
2739 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2740 xfree
, xcalloc
, xfree
);
2741 self
->is_linespec
= 0;
2744 /* Initialize a new linespec parser. */
2747 linespec_parser_new (linespec_parser
*parser
,
2748 int flags
, const struct language_defn
*language
,
2749 struct program_space
*search_pspace
,
2750 struct symtab
*default_symtab
,
2752 struct linespec_result
*canonical
)
2754 memset (parser
, 0, sizeof (linespec_parser
));
2755 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
2756 memset (PARSER_RESULT (parser
), 0, sizeof (struct linespec
));
2757 PARSER_EXPLICIT (parser
)->line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2758 linespec_state_constructor (PARSER_STATE (parser
), flags
, language
,
2760 default_symtab
, default_line
, canonical
);
2763 /* A destructor for linespec_state. */
2766 linespec_state_destructor (struct linespec_state
*self
)
2768 htab_delete (self
->addr_set
);
2771 /* Delete a linespec parser. */
2774 linespec_parser_delete (void *arg
)
2776 linespec_parser
*parser
= (linespec_parser
*) arg
;
2778 xfree (PARSER_EXPLICIT (parser
)->source_filename
);
2779 xfree (PARSER_EXPLICIT (parser
)->label_name
);
2780 xfree (PARSER_EXPLICIT (parser
)->function_name
);
2782 if (PARSER_RESULT (parser
)->file_symtabs
!= NULL
)
2783 VEC_free (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
);
2785 if (PARSER_RESULT (parser
)->function_symbols
!= NULL
)
2786 VEC_free (symbolp
, PARSER_RESULT (parser
)->function_symbols
);
2788 if (PARSER_RESULT (parser
)->minimal_symbols
!= NULL
)
2789 VEC_free (bound_minimal_symbol_d
, PARSER_RESULT (parser
)->minimal_symbols
);
2791 if (PARSER_RESULT (parser
)->labels
.label_symbols
!= NULL
)
2792 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.label_symbols
);
2794 if (PARSER_RESULT (parser
)->labels
.function_symbols
!= NULL
)
2795 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.function_symbols
);
2797 linespec_state_destructor (PARSER_STATE (parser
));
2800 /* See description in linespec.h. */
2803 linespec_lex_to_end (char **stringp
)
2805 linespec_parser parser
;
2806 struct cleanup
*cleanup
;
2807 linespec_token token
;
2810 if (stringp
== NULL
|| *stringp
== NULL
)
2813 linespec_parser_new (&parser
, 0, current_language
, NULL
, NULL
, 0, NULL
);
2814 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2815 parser
.lexer
.saved_arg
= *stringp
;
2816 PARSER_STREAM (&parser
) = orig
= *stringp
;
2820 /* Stop before any comma tokens; we need it to keep it
2821 as the next token in the string. */
2822 token
= linespec_lexer_peek_token (&parser
);
2823 if (token
.type
== LSTOKEN_COMMA
)
2825 token
= linespec_lexer_consume_token (&parser
);
2827 while (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
);
2829 *stringp
+= PARSER_STREAM (&parser
) - orig
;
2830 do_cleanups (cleanup
);
2833 /* See linespec.h. */
2836 linespec_complete_function (completion_tracker
&tracker
,
2837 const char *function
,
2838 const char *source_filename
)
2840 complete_symbol_mode mode
= complete_symbol_mode::LINESPEC
;
2842 if (source_filename
!= NULL
)
2844 collect_file_symbol_completion_matches (tracker
, mode
,
2849 collect_symbol_completion_matches (tracker
, mode
, function
, function
);
2852 /* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2853 only meaningful if COMPONENT is FUNCTION. */
2856 complete_linespec_component (linespec_parser
*parser
,
2857 completion_tracker
&tracker
,
2859 linespec_complete_what component
,
2860 const char *source_filename
)
2862 if (component
== linespec_complete_what::KEYWORD
)
2864 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
2866 else if (component
== linespec_complete_what::EXPRESSION
)
2869 = advance_to_expression_complete_word_point (tracker
, text
);
2870 complete_expression (tracker
, text
, word
);
2872 else if (component
== linespec_complete_what::FUNCTION
)
2874 completion_list fn_list
;
2876 linespec_complete_function (tracker
, text
, source_filename
);
2877 if (source_filename
== NULL
)
2879 /* Haven't seen a source component, like in "b
2880 file.c:function[TAB]". Maybe this wasn't a function, but
2881 a filename instead, like "b file.[TAB]". */
2882 fn_list
= complete_source_filenames (text
);
2885 /* If we only have a single filename completion, append a ':' for
2886 the user, since that's the only thing that can usefully follow
2888 if (fn_list
.size () == 1 && !tracker
.have_completions ())
2890 char *fn
= fn_list
[0].release ();
2892 /* If we also need to append a quote char, it needs to be
2893 appended before the ':'. Append it now, and make ':' the
2894 new "quote" char. */
2895 if (tracker
.quote_char ())
2897 char quote_char_str
[2] = { tracker
.quote_char () };
2899 fn
= reconcat (fn
, fn
, quote_char_str
, (char *) NULL
);
2900 tracker
.set_quote_char (':');
2903 fn
= reconcat (fn
, fn
, ":", (char *) NULL
);
2904 fn_list
[0].reset (fn
);
2906 /* Tell readline to skip appending a space. */
2907 tracker
.set_suppress_append_ws (true);
2909 tracker
.add_completions (std::move (fn_list
));
2913 /* Helper for linespec_complete_label. Find labels that match
2914 LABEL_NAME in the function symbols listed in the PARSER, and add
2915 them to the tracker. */
2918 complete_label (completion_tracker
&tracker
,
2919 linespec_parser
*parser
,
2920 const char *label_name
)
2922 VEC (symbolp
) *label_function_symbols
= NULL
;
2923 VEC (symbolp
) *labels
2924 = find_label_symbols (PARSER_STATE (parser
),
2925 PARSER_RESULT (parser
)->function_symbols
,
2926 &label_function_symbols
,
2931 VEC_iterate (symbolp
, labels
, ix
, label
); ++ix
)
2933 char *match
= xstrdup (SYMBOL_SEARCH_NAME (label
));
2934 tracker
.add_completion (gdb::unique_xmalloc_ptr
<char> (match
));
2936 VEC_free (symbolp
, labels
);
2939 /* See linespec.h. */
2942 linespec_complete_label (completion_tracker
&tracker
,
2943 const struct language_defn
*language
,
2944 const char *source_filename
,
2945 const char *function_name
,
2946 const char *label_name
)
2948 linespec_parser parser
;
2949 struct cleanup
*cleanup
;
2951 linespec_parser_new (&parser
, 0, language
, NULL
, NULL
, 0, NULL
);
2952 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2954 line_offset unknown_offset
= { 0, LINE_OFFSET_UNKNOWN
};
2958 convert_explicit_location_to_linespec (PARSER_STATE (&parser
),
2959 PARSER_RESULT (&parser
),
2962 NULL
, unknown_offset
);
2964 CATCH (ex
, RETURN_MASK_ERROR
)
2966 do_cleanups (cleanup
);
2971 complete_label (tracker
, &parser
, label_name
);
2973 do_cleanups (cleanup
);
2976 /* See description in linespec.h. */
2979 linespec_complete (completion_tracker
&tracker
, const char *text
)
2981 linespec_parser parser
;
2982 struct cleanup
*cleanup
;
2983 const char *orig
= text
;
2985 linespec_parser_new (&parser
, 0, current_language
, NULL
, NULL
, 0, NULL
);
2986 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2987 parser
.lexer
.saved_arg
= text
;
2988 PARSER_STREAM (&parser
) = text
;
2990 parser
.completion_tracker
= &tracker
;
2991 PARSER_STATE (&parser
)->is_linespec
= 1;
2993 /* Parse as much as possible. parser.completion_word will hold
2994 furthest completion point we managed to parse to. */
2997 parse_linespec (&parser
, text
);
2999 CATCH (except
, RETURN_MASK_ERROR
)
3004 if (parser
.completion_quote_char
!= '\0'
3005 && parser
.completion_quote_end
!= NULL
3006 && parser
.completion_quote_end
[1] == '\0')
3008 /* If completing a quoted string with the cursor right at
3009 terminating quote char, complete the completion word without
3010 interpretation, so that readline advances the cursor one
3011 whitespace past the quote, even if there's no match. This
3012 makes these cases behave the same:
3014 before: "b function()"
3015 after: "b function() "
3017 before: "b 'function()'"
3018 after: "b 'function()' "
3020 and trusts the user in this case:
3022 before: "b 'not_loaded_function_yet()'"
3023 after: "b 'not_loaded_function_yet()' "
3025 parser
.complete_what
= linespec_complete_what::NOTHING
;
3026 parser
.completion_quote_char
= '\0';
3028 gdb::unique_xmalloc_ptr
<char> text_copy
3029 (xstrdup (parser
.completion_word
));
3030 tracker
.add_completion (std::move (text_copy
));
3033 tracker
.set_quote_char (parser
.completion_quote_char
);
3035 if (parser
.complete_what
== linespec_complete_what::LABEL
)
3037 parser
.complete_what
= linespec_complete_what::NOTHING
;
3039 const char *func_name
= PARSER_EXPLICIT (&parser
)->function_name
;
3041 VEC (symbolp
) *function_symbols
;
3042 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
3043 find_linespec_symbols (PARSER_STATE (&parser
),
3044 PARSER_RESULT (&parser
)->file_symtabs
,
3046 &function_symbols
, &minimal_symbols
);
3048 PARSER_RESULT (&parser
)->function_symbols
= function_symbols
;
3049 PARSER_RESULT (&parser
)->minimal_symbols
= minimal_symbols
;
3051 complete_label (tracker
, &parser
, parser
.completion_word
);
3053 else if (parser
.complete_what
== linespec_complete_what::FUNCTION
)
3055 /* While parsing/lexing, we didn't know whether the completion
3056 word completes to a unique function/source name already or
3060 "b function() <tab>"
3061 may need to complete either to:
3062 "b function() const"
3064 "b function() if/thread/task"
3068 may need to complete either to:
3069 "b foo template_fun<T>()"
3070 with "foo" being the template function's return type, or to:
3075 may need to complete either to a source file name:
3077 or this, also a filename, but a unique completion:
3079 or to a function name:
3082 Address that by completing assuming source or function, and
3083 seeing if we find a completion that matches exactly the
3084 completion word. If so, then it must be a function (see note
3085 below) and we advance the completion word to the end of input
3086 and switch to KEYWORD completion mode.
3088 Note: if we find a unique completion for a source filename,
3089 then it won't match the completion word, because the LCD will
3090 contain a trailing ':'. And if we're completing at or after
3091 the ':', then complete_linespec_component won't try to
3092 complete on source filenames. */
3094 const char *text
= parser
.completion_word
;
3095 const char *word
= parser
.completion_word
;
3097 complete_linespec_component (&parser
, tracker
,
3098 parser
.completion_word
,
3099 linespec_complete_what::FUNCTION
,
3100 PARSER_EXPLICIT (&parser
)->source_filename
);
3102 parser
.complete_what
= linespec_complete_what::NOTHING
;
3104 if (tracker
.quote_char ())
3106 /* The function/file name was not close-quoted, so this
3107 can't be a keyword. Note: complete_linespec_component
3108 may have swapped the original quote char for ':' when we
3109 get here, but that still indicates the same. */
3111 else if (!tracker
.have_completions ())
3114 size_t wordlen
= strlen (parser
.completion_word
);
3117 = string_find_incomplete_keyword_at_end (linespec_keywords
,
3118 parser
.completion_word
,
3123 && parser
.completion_word
[wordlen
- 1] == ' '))
3125 parser
.completion_word
+= key_start
;
3126 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3129 else if (tracker
.completes_to_completion_word (word
))
3131 /* Skip the function and complete on keywords. */
3132 parser
.completion_word
+= strlen (word
);
3133 parser
.complete_what
= linespec_complete_what::KEYWORD
;
3134 tracker
.discard_completions ();
3138 tracker
.advance_custom_word_point_by (parser
.completion_word
- orig
);
3140 complete_linespec_component (&parser
, tracker
,
3141 parser
.completion_word
,
3142 parser
.complete_what
,
3143 PARSER_EXPLICIT (&parser
)->source_filename
);
3145 /* If we're past the "filename:function:label:offset" linespec, and
3146 didn't find any match, then assume the user might want to create
3147 a pending breakpoint anyway and offer the keyword
3149 if (!parser
.completion_quote_char
3150 && (parser
.complete_what
== linespec_complete_what::FUNCTION
3151 || parser
.complete_what
== linespec_complete_what::LABEL
3152 || parser
.complete_what
== linespec_complete_what::NOTHING
)
3153 && !tracker
.have_completions ())
3156 = parser
.completion_word
+ strlen (parser
.completion_word
);
3158 if (end
> orig
&& end
[-1] == ' ')
3160 tracker
.advance_custom_word_point_by (end
- parser
.completion_word
);
3162 complete_linespec_component (&parser
, tracker
, end
,
3163 linespec_complete_what::KEYWORD
,
3168 do_cleanups (cleanup
);
3171 /* A helper function for decode_line_full and decode_line_1 to
3172 turn LOCATION into std::vector<symtab_and_line>. */
3174 static std::vector
<symtab_and_line
>
3175 event_location_to_sals (linespec_parser
*parser
,
3176 const struct event_location
*location
)
3178 std::vector
<symtab_and_line
> result
;
3180 switch (event_location_type (location
))
3182 case LINESPEC_LOCATION
:
3184 PARSER_STATE (parser
)->is_linespec
= 1;
3187 result
= parse_linespec (parser
, get_linespec_location (location
));
3189 CATCH (except
, RETURN_MASK_ERROR
)
3191 throw_exception (except
);
3197 case ADDRESS_LOCATION
:
3199 const char *addr_string
= get_address_string_location (location
);
3200 CORE_ADDR addr
= get_address_location (location
);
3202 if (addr_string
!= NULL
)
3204 char *expr
= xstrdup (addr_string
);
3205 const char *const_expr
= expr
;
3206 struct cleanup
*cleanup
= make_cleanup (xfree
, expr
);
3208 addr
= linespec_expression_to_pc (&const_expr
);
3209 if (PARSER_STATE (parser
)->canonical
!= NULL
)
3210 PARSER_STATE (parser
)->canonical
->location
3211 = copy_event_location (location
);
3213 do_cleanups (cleanup
);
3216 result
= convert_address_location_to_sals (PARSER_STATE (parser
),
3221 case EXPLICIT_LOCATION
:
3223 const struct explicit_location
*explicit_loc
;
3225 explicit_loc
= get_explicit_location_const (location
);
3226 result
= convert_explicit_location_to_sals (PARSER_STATE (parser
),
3227 PARSER_RESULT (parser
),
3232 case PROBE_LOCATION
:
3233 /* Probes are handled by their own decoders. */
3234 gdb_assert_not_reached ("attempt to decode probe location");
3238 gdb_assert_not_reached ("unhandled event location type");
3244 /* See linespec.h. */
3247 decode_line_full (const struct event_location
*location
, int flags
,
3248 struct program_space
*search_pspace
,
3249 struct symtab
*default_symtab
,
3250 int default_line
, struct linespec_result
*canonical
,
3251 const char *select_mode
,
3254 struct cleanup
*cleanups
;
3255 VEC (const_char_ptr
) *filters
= NULL
;
3256 linespec_parser parser
;
3257 struct linespec_state
*state
;
3259 gdb_assert (canonical
!= NULL
);
3260 /* The filter only makes sense for 'all'. */
3261 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
3262 gdb_assert (select_mode
== NULL
3263 || select_mode
== multiple_symbols_all
3264 || select_mode
== multiple_symbols_ask
3265 || select_mode
== multiple_symbols_cancel
);
3266 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
3268 linespec_parser_new (&parser
, flags
, current_language
,
3269 search_pspace
, default_symtab
,
3270 default_line
, canonical
);
3271 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
3273 scoped_restore_current_program_space restore_pspace
;
3275 std::vector
<symtab_and_line
> result
= event_location_to_sals (&parser
,
3277 state
= PARSER_STATE (&parser
);
3279 gdb_assert (result
.size () == 1 || canonical
->pre_expanded
);
3280 canonical
->pre_expanded
= 1;
3282 /* Arrange for allocated canonical names to be freed. */
3283 if (!result
.empty ())
3287 make_cleanup (xfree
, state
->canonical_names
);
3288 for (i
= 0; i
< result
.size (); ++i
)
3290 gdb_assert (state
->canonical_names
[i
].suffix
!= NULL
);
3291 make_cleanup (xfree
, state
->canonical_names
[i
].suffix
);
3295 if (select_mode
== NULL
)
3297 if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ())
3298 select_mode
= multiple_symbols_all
;
3300 select_mode
= multiple_symbols_select_mode ();
3303 if (select_mode
== multiple_symbols_all
)
3307 make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
3308 VEC_safe_push (const_char_ptr
, filters
, filter
);
3309 filter_results (state
, &result
, filters
);
3312 convert_results_to_lsals (state
, &result
);
3315 decode_line_2 (state
, &result
, select_mode
);
3317 do_cleanups (cleanups
);
3320 /* See linespec.h. */
3322 std::vector
<symtab_and_line
>
3323 decode_line_1 (const struct event_location
*location
, int flags
,
3324 struct program_space
*search_pspace
,
3325 struct symtab
*default_symtab
,
3328 linespec_parser parser
;
3329 struct cleanup
*cleanups
;
3331 linespec_parser_new (&parser
, flags
, current_language
,
3332 search_pspace
, default_symtab
,
3333 default_line
, NULL
);
3334 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
3336 scoped_restore_current_program_space restore_pspace
;
3338 std::vector
<symtab_and_line
> result
= event_location_to_sals (&parser
,
3341 do_cleanups (cleanups
);
3345 /* See linespec.h. */
3347 std::vector
<symtab_and_line
>
3348 decode_line_with_current_source (char *string
, int flags
)
3351 error (_("Empty line specification."));
3353 /* We use whatever is set as the current source line. We do not try
3354 and get a default source symtab+line or it will recursively call us! */
3355 symtab_and_line cursal
= get_current_source_symtab_and_line ();
3357 event_location_up location
= string_to_event_location (&string
,
3359 std::vector
<symtab_and_line
> sals
3360 = decode_line_1 (location
.get (), flags
, NULL
, cursal
.symtab
, cursal
.line
);
3363 error (_("Junk at end of line specification: %s"), string
);
3368 /* See linespec.h. */
3370 std::vector
<symtab_and_line
>
3371 decode_line_with_last_displayed (char *string
, int flags
)
3374 error (_("Empty line specification."));
3376 event_location_up location
= string_to_event_location (&string
,
3378 std::vector
<symtab_and_line
> sals
3379 = (last_displayed_sal_is_valid ()
3380 ? decode_line_1 (location
.get (), flags
, NULL
,
3381 get_last_displayed_symtab (),
3382 get_last_displayed_line ())
3383 : decode_line_1 (location
.get (), flags
, NULL
,
3384 (struct symtab
*) NULL
, 0));
3387 error (_("Junk at end of line specification: %s"), string
);
3394 /* First, some functions to initialize stuff at the beggining of the
3398 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
3400 if (*default_symtab
== 0)
3402 /* Use whatever we have for the default source line. We don't use
3403 get_current_or_default_symtab_and_line as it can recurse and call
3405 struct symtab_and_line cursal
=
3406 get_current_source_symtab_and_line ();
3408 *default_symtab
= cursal
.symtab
;
3409 *default_line
= cursal
.line
;
3415 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3416 advancing EXP_PTR past any parsed text. */
3419 linespec_expression_to_pc (const char **exp_ptr
)
3421 if (current_program_space
->executing_startup
)
3422 /* The error message doesn't really matter, because this case
3423 should only hit during breakpoint reset. */
3424 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
3425 "program space is in startup"));
3428 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
3433 /* Here's where we recognise an Objective-C Selector. An Objective C
3434 selector may be implemented by more than one class, therefore it
3435 may represent more than one method/function. This gives us a
3436 situation somewhat analogous to C++ overloading. If there's more
3437 than one method that could represent the selector, then use some of
3438 the existing C++ code to let the user choose one. */
3440 static std::vector
<symtab_and_line
>
3441 decode_objc (struct linespec_state
*self
, linespec_p ls
, const char *arg
)
3443 struct collect_info info
;
3444 VEC (const_char_ptr
) *symbol_names
= NULL
;
3445 const char *new_argptr
;
3446 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
3450 info
.file_symtabs
= NULL
;
3451 VEC_safe_push (symtab_ptr
, info
.file_symtabs
, NULL
);
3452 make_cleanup (VEC_cleanup (symtab_ptr
), &info
.file_symtabs
);
3453 info
.result
.symbols
= NULL
;
3454 info
.result
.minimal_symbols
= NULL
;
3456 new_argptr
= find_imps (arg
, &symbol_names
);
3457 if (VEC_empty (const_char_ptr
, symbol_names
))
3459 do_cleanups (cleanup
);
3463 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
);
3465 std::vector
<symtab_and_line
> values
;
3466 if (!VEC_empty (symbolp
, info
.result
.symbols
)
3467 || !VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
3471 saved_arg
= (char *) alloca (new_argptr
- arg
+ 1);
3472 memcpy (saved_arg
, arg
, new_argptr
- arg
);
3473 saved_arg
[new_argptr
- arg
] = '\0';
3475 ls
->explicit_loc
.function_name
= xstrdup (saved_arg
);
3476 ls
->function_symbols
= info
.result
.symbols
;
3477 ls
->minimal_symbols
= info
.result
.minimal_symbols
;
3478 values
= convert_linespec_to_sals (self
, ls
);
3480 if (self
->canonical
)
3484 self
->canonical
->pre_expanded
= 1;
3486 if (ls
->explicit_loc
.source_filename
)
3488 str
= xstrprintf ("%s:%s",
3489 ls
->explicit_loc
.source_filename
, saved_arg
);
3492 str
= xstrdup (saved_arg
);
3494 make_cleanup (xfree
, str
);
3495 self
->canonical
->location
= new_linespec_location (&str
);
3499 do_cleanups (cleanup
);
3506 /* A function object that serves as symbol_found_callback_ftype
3507 callback for iterate_over_symbols. This is used by
3508 lookup_prefix_sym to collect type symbols. */
3509 class decode_compound_collector
3512 decode_compound_collector ()
3515 m_unique_syms
= htab_create_alloc (1, htab_hash_pointer
,
3516 htab_eq_pointer
, NULL
,
3520 ~decode_compound_collector ()
3522 if (m_unique_syms
!= NULL
)
3523 htab_delete (m_unique_syms
);
3526 /* Releases ownership of the collected symbols and returns them. */
3527 VEC (symbolp
) *release_symbols ()
3529 VEC (symbolp
) *res
= m_symbols
;
3534 /* Callable as a symbol_found_callback_ftype callback. */
3535 bool operator () (symbol
*sym
);
3538 /* A hash table of all symbols we found. We use this to avoid
3539 adding any symbol more than once. */
3540 htab_t m_unique_syms
;
3542 /* The result vector. */
3543 VEC (symbolp
) *m_symbols
;
3547 decode_compound_collector::operator () (symbol
*sym
)
3552 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
3553 return true; /* Continue iterating. */
3555 t
= SYMBOL_TYPE (sym
);
3556 t
= check_typedef (t
);
3557 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
3558 && TYPE_CODE (t
) != TYPE_CODE_UNION
3559 && TYPE_CODE (t
) != TYPE_CODE_NAMESPACE
)
3560 return true; /* Continue iterating. */
3562 slot
= htab_find_slot (m_unique_syms
, sym
, INSERT
);
3566 VEC_safe_push (symbolp
, m_symbols
, sym
);
3569 return true; /* Continue iterating. */
3574 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
3576 static VEC (symbolp
) *
3577 lookup_prefix_sym (struct linespec_state
*state
, VEC (symtab_ptr
) *file_symtabs
,
3578 const char *class_name
)
3582 decode_compound_collector collector
;
3584 for (ix
= 0; VEC_iterate (symtab_ptr
, file_symtabs
, ix
, elt
); ++ix
)
3588 iterate_over_all_matching_symtabs (state
, class_name
, STRUCT_DOMAIN
,
3589 NULL
, false, collector
);
3590 iterate_over_all_matching_symtabs (state
, class_name
, VAR_DOMAIN
,
3591 NULL
, false, collector
);
3595 /* Program spaces that are executing startup should have
3596 been filtered out earlier. */
3597 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
3598 set_current_program_space (SYMTAB_PSPACE (elt
));
3599 iterate_over_file_blocks (elt
, class_name
, STRUCT_DOMAIN
, collector
);
3600 iterate_over_file_blocks (elt
, class_name
, VAR_DOMAIN
, collector
);
3604 return collector
.release_symbols ();
3607 /* A qsort comparison function for symbols. The resulting order does
3608 not actually matter; we just need to be able to sort them so that
3609 symbols with the same program space end up next to each other. */
3612 compare_symbols (const void *a
, const void *b
)
3614 struct symbol
* const *sa
= (struct symbol
* const*) a
;
3615 struct symbol
* const *sb
= (struct symbol
* const*) b
;
3618 uia
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa
));
3619 uib
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb
));
3626 uia
= (uintptr_t) *sa
;
3627 uib
= (uintptr_t) *sb
;
3637 /* Like compare_symbols but for minimal symbols. */
3640 compare_msymbols (const void *a
, const void *b
)
3642 const struct bound_minimal_symbol
*sa
3643 = (const struct bound_minimal_symbol
*) a
;
3644 const struct bound_minimal_symbol
*sb
3645 = (const struct bound_minimal_symbol
*) b
;
3648 uia
= (uintptr_t) sa
->objfile
->pspace
;
3649 uib
= (uintptr_t) sa
->objfile
->pspace
;
3656 uia
= (uintptr_t) sa
->minsym
;
3657 uib
= (uintptr_t) sb
->minsym
;
3667 /* Look for all the matching instances of each symbol in NAMES. Only
3668 instances from PSPACE are considered; other program spaces are
3669 handled by our caller. If PSPACE is NULL, then all program spaces
3670 are considered. Results are stored into INFO. */
3673 add_all_symbol_names_from_pspace (struct collect_info
*info
,
3674 struct program_space
*pspace
,
3675 VEC (const_char_ptr
) *names
)
3680 for (ix
= 0; VEC_iterate (const_char_ptr
, names
, ix
, iter
); ++ix
)
3681 add_matching_symbols_to_info (iter
, info
, pspace
);
3685 find_superclass_methods (VEC (typep
) *superclasses
,
3687 VEC (const_char_ptr
) **result_names
)
3689 int old_len
= VEC_length (const_char_ptr
, *result_names
);
3690 VEC (typep
) *iter_classes
;
3691 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
3693 iter_classes
= superclasses
;
3696 VEC (typep
) *new_supers
= NULL
;
3700 make_cleanup (VEC_cleanup (typep
), &new_supers
);
3701 for (ix
= 0; VEC_iterate (typep
, iter_classes
, ix
, t
); ++ix
)
3702 find_methods (t
, name
, result_names
, &new_supers
);
3704 if (VEC_length (const_char_ptr
, *result_names
) != old_len
3705 || VEC_empty (typep
, new_supers
))
3708 iter_classes
= new_supers
;
3711 do_cleanups (cleanup
);
3714 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3715 given by one of the symbols in SYM_CLASSES. Matches are returned
3716 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3719 find_method (struct linespec_state
*self
, VEC (symtab_ptr
) *file_symtabs
,
3720 const char *class_name
, const char *method_name
,
3721 VEC (symbolp
) *sym_classes
, VEC (symbolp
) **symbols
,
3722 VEC (bound_minimal_symbol_d
) **minsyms
)
3725 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
3727 int last_result_len
;
3728 VEC (typep
) *superclass_vec
;
3729 VEC (const_char_ptr
) *result_names
;
3730 struct collect_info info
;
3732 /* Sort symbols so that symbols with the same program space are next
3734 qsort (VEC_address (symbolp
, sym_classes
),
3735 VEC_length (symbolp
, sym_classes
),
3740 info
.file_symtabs
= file_symtabs
;
3741 info
.result
.symbols
= NULL
;
3742 info
.result
.minimal_symbols
= NULL
;
3744 /* Iterate over all the types, looking for the names of existing
3745 methods matching METHOD_NAME. If we cannot find a direct method in a
3746 given program space, then we consider inherited methods; this is
3747 not ideal (ideal would be to respect C++ hiding rules), but it
3748 seems good enough and is what GDB has historically done. We only
3749 need to collect the names because later we find all symbols with
3750 those names. This loop is written in a somewhat funny way
3751 because we collect data across the program space before deciding
3753 superclass_vec
= NULL
;
3754 make_cleanup (VEC_cleanup (typep
), &superclass_vec
);
3755 result_names
= NULL
;
3756 make_cleanup (VEC_cleanup (const_char_ptr
), &result_names
);
3757 last_result_len
= 0;
3758 for (ix
= 0; VEC_iterate (symbolp
, sym_classes
, ix
, sym
); ++ix
)
3761 struct program_space
*pspace
;
3763 /* Program spaces that are executing startup should have
3764 been filtered out earlier. */
3765 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
3766 gdb_assert (!pspace
->executing_startup
);
3767 set_current_program_space (pspace
);
3768 t
= check_typedef (SYMBOL_TYPE (sym
));
3769 find_methods (t
, method_name
, &result_names
, &superclass_vec
);
3771 /* Handle all items from a single program space at once; and be
3772 sure not to miss the last batch. */
3773 if (ix
== VEC_length (symbolp
, sym_classes
) - 1
3775 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp
, sym_classes
,
3778 /* If we did not find a direct implementation anywhere in
3779 this program space, consider superclasses. */
3780 if (VEC_length (const_char_ptr
, result_names
) == last_result_len
)
3781 find_superclass_methods (superclass_vec
, method_name
,
3784 /* We have a list of candidate symbol names, so now we
3785 iterate over the symbol tables looking for all
3786 matches in this pspace. */
3787 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
);
3789 VEC_truncate (typep
, superclass_vec
, 0);
3790 last_result_len
= VEC_length (const_char_ptr
, result_names
);
3794 if (!VEC_empty (symbolp
, info
.result
.symbols
)
3795 || !VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
3797 *symbols
= info
.result
.symbols
;
3798 *minsyms
= info
.result
.minimal_symbols
;
3799 do_cleanups (cleanup
);
3803 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3804 and other attempts to locate the symbol will be made. */
3805 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
3812 /* This function object is a callback for iterate_over_symtabs, used
3813 when collecting all matching symtabs. */
3815 class symtab_collector
3821 m_symtab_table
= htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
3825 ~symtab_collector ()
3827 if (m_symtab_table
!= NULL
)
3828 htab_delete (m_symtab_table
);
3831 /* Callable as a symbol_found_callback_ftype callback. */
3832 bool operator () (symtab
*sym
);
3834 /* Releases ownership of the collected symtabs and returns them. */
3835 VEC (symtab_ptr
) *release_symtabs ()
3837 VEC (symtab_ptr
) *res
= m_symtabs
;
3843 /* The result vector of symtabs. */
3844 VEC (symtab_ptr
) *m_symtabs
;
3846 /* This is used to ensure the symtabs are unique. */
3847 htab_t m_symtab_table
;
3851 symtab_collector::operator () (struct symtab
*symtab
)
3855 slot
= htab_find_slot (m_symtab_table
, symtab
, INSERT
);
3859 VEC_safe_push (symtab_ptr
, m_symtabs
, symtab
);
3867 /* Given a file name, return a VEC of all matching symtabs. If
3868 SEARCH_PSPACE is not NULL, the search is restricted to just that
3871 static VEC (symtab_ptr
) *
3872 collect_symtabs_from_filename (const char *file
,
3873 struct program_space
*search_pspace
)
3875 symtab_collector collector
;
3877 /* Find that file's data. */
3878 if (search_pspace
== NULL
)
3880 struct program_space
*pspace
;
3882 ALL_PSPACES (pspace
)
3884 if (pspace
->executing_startup
)
3887 set_current_program_space (pspace
);
3888 iterate_over_symtabs (file
, collector
);
3893 set_current_program_space (search_pspace
);
3894 iterate_over_symtabs (file
, collector
);
3897 return collector
.release_symtabs ();
3900 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3901 not NULL, the search is restricted to just that program space. */
3903 static VEC (symtab_ptr
) *
3904 symtabs_from_filename (const char *filename
,
3905 struct program_space
*search_pspace
)
3907 VEC (symtab_ptr
) *result
;
3909 result
= collect_symtabs_from_filename (filename
, search_pspace
);
3911 if (VEC_empty (symtab_ptr
, result
))
3913 if (!have_full_symbols () && !have_partial_symbols ())
3914 throw_error (NOT_FOUND_ERROR
,
3915 _("No symbol table is loaded. "
3916 "Use the \"file\" command."));
3917 source_file_not_found_error (filename
);
3923 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3924 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3925 returned in MINSYMS. */
3928 find_function_symbols (struct linespec_state
*state
,
3929 VEC (symtab_ptr
) *file_symtabs
, const char *name
,
3930 VEC (symbolp
) **symbols
,
3931 VEC (bound_minimal_symbol_d
) **minsyms
)
3933 struct collect_info info
;
3934 VEC (const_char_ptr
) *symbol_names
= NULL
;
3935 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
3939 info
.result
.symbols
= NULL
;
3940 info
.result
.minimal_symbols
= NULL
;
3941 info
.file_symtabs
= file_symtabs
;
3943 /* Try NAME as an Objective-C selector. */
3944 find_imps (name
, &symbol_names
);
3945 if (!VEC_empty (const_char_ptr
, symbol_names
))
3946 add_all_symbol_names_from_pspace (&info
, state
->search_pspace
,
3949 add_matching_symbols_to_info (name
, &info
, state
->search_pspace
);
3951 do_cleanups (cleanup
);
3953 if (VEC_empty (symbolp
, info
.result
.symbols
))
3955 VEC_free (symbolp
, info
.result
.symbols
);
3959 *symbols
= info
.result
.symbols
;
3961 if (VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
3963 VEC_free (bound_minimal_symbol_d
, info
.result
.minimal_symbols
);
3967 *minsyms
= info
.result
.minimal_symbols
;
3970 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3971 in SYMBOLS and minimal symbols in MINSYMS. */
3974 find_linespec_symbols (struct linespec_state
*state
,
3975 VEC (symtab_ptr
) *file_symtabs
,
3977 VEC (symbolp
) **symbols
,
3978 VEC (bound_minimal_symbol_d
) **minsyms
)
3980 demangle_result_storage demangle_storage
;
3981 std::string ada_lookup_storage
;
3982 const char *lookup_name
;
3984 if (state
->language
->la_language
== language_ada
)
3986 /* In Ada, the symbol lookups are performed using the encoded
3987 name rather than the demangled name. */
3988 ada_lookup_storage
= ada_name_for_lookup (name
);
3989 lookup_name
= ada_lookup_storage
.c_str ();
3993 lookup_name
= demangle_for_lookup (name
,
3994 state
->language
->la_language
,
3998 std::string canon
= cp_canonicalize_string_no_typedefs (lookup_name
);
3999 if (!canon
.empty ())
4000 lookup_name
= canon
.c_str ();
4002 /* It's important to not call expand_symtabs_matching unnecessarily
4003 as it can really slow things down (by unnecessarily expanding
4004 potentially 1000s of symtabs, which when debugging some apps can
4005 cost 100s of seconds). Avoid this to some extent by *first* calling
4006 find_function_symbols, and only if that doesn't find anything
4007 *then* call find_method. This handles two important cases:
4008 1) break (anonymous namespace)::foo
4009 2) break class::method where method is in class (and not a baseclass) */
4011 find_function_symbols (state
, file_symtabs
, lookup_name
,
4014 /* If we were unable to locate a symbol of the same name, try dividing
4015 the name into class and method names and searching the class and its
4017 if (VEC_empty (symbolp
, *symbols
)
4018 && VEC_empty (bound_minimal_symbol_d
, *minsyms
))
4020 std::string klass
, method
;
4021 const char *last
, *p
, *scope_op
;
4022 VEC (symbolp
) *classes
;
4024 /* See if we can find a scope operator and break this symbol
4025 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
4027 p
= find_toplevel_string (lookup_name
, scope_op
);
4033 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
4036 /* If no scope operator was found, there is nothing more we can do;
4037 we already attempted to lookup the entire name as a symbol
4042 /* LOOKUP_NAME points to the class name.
4043 LAST points to the method name. */
4044 klass
= std::string (lookup_name
, last
- lookup_name
);
4046 /* Skip past the scope operator. */
4047 last
+= strlen (scope_op
);
4050 /* Find a list of classes named KLASS. */
4051 classes
= lookup_prefix_sym (state
, file_symtabs
, klass
.c_str ());
4052 struct cleanup
*old_chain
4053 = make_cleanup (VEC_cleanup (symbolp
), &classes
);
4055 if (!VEC_empty (symbolp
, classes
))
4057 /* Now locate a list of suitable methods named METHOD. */
4060 find_method (state
, file_symtabs
,
4061 klass
.c_str (), method
.c_str (),
4062 classes
, symbols
, minsyms
);
4065 /* If successful, we're done. If NOT_FOUND_ERROR
4066 was not thrown, rethrow the exception that we did get. */
4067 CATCH (except
, RETURN_MASK_ERROR
)
4069 if (except
.error
!= NOT_FOUND_ERROR
)
4070 throw_exception (except
);
4075 do_cleanups (old_chain
);
4079 /* Helper for find_label_symbols. Find all labels that match name
4080 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
4081 Return the actual function symbol in which the label was found in
4082 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4083 interpreted as a label name prefix. Otherwise, only a label named
4084 exactly NAME match. */
4087 find_label_symbols_in_block (const struct block
*block
,
4088 const char *name
, struct symbol
*fn_sym
,
4089 bool completion_mode
,
4090 VEC (symbolp
) **result
,
4091 VEC (symbolp
) **label_funcs_ret
)
4093 if (completion_mode
)
4095 struct block_iterator iter
;
4097 size_t name_len
= strlen (name
);
4099 int (*cmp
) (const char *, const char *, size_t);
4100 cmp
= case_sensitivity
== case_sensitive_on
? strncmp
: strncasecmp
;
4102 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
4104 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
4105 SYMBOL_DOMAIN (sym
), LABEL_DOMAIN
)
4106 && cmp (SYMBOL_SEARCH_NAME (sym
), name
, name_len
) == 0)
4108 VEC_safe_push (symbolp
, *result
, sym
);
4109 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
4115 struct symbol
*sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0).symbol
;
4119 VEC_safe_push (symbolp
, *result
, sym
);
4120 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
4125 /* Return all labels that match name NAME in FUNCTION_SYMBOLS. Return
4126 the actual function symbol in which the label was found in
4127 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4128 interpreted as a label name prefix. Otherwise, only labels named
4129 exactly NAME match. */
4131 static VEC (symbolp
) *
4132 find_label_symbols (struct linespec_state
*self
,
4133 VEC (symbolp
) *function_symbols
,
4134 VEC (symbolp
) **label_funcs_ret
, const char *name
,
4135 bool completion_mode
)
4138 const struct block
*block
;
4140 struct symbol
*fn_sym
;
4141 VEC (symbolp
) *result
= NULL
;
4143 if (function_symbols
== NULL
)
4145 set_current_program_space (self
->program_space
);
4146 block
= get_current_search_block ();
4149 block
&& !BLOCK_FUNCTION (block
);
4150 block
= BLOCK_SUPERBLOCK (block
))
4154 fn_sym
= BLOCK_FUNCTION (block
);
4156 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
4157 &result
, label_funcs_ret
);
4162 VEC_iterate (symbolp
, function_symbols
, ix
, fn_sym
); ++ix
)
4164 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym
)));
4165 block
= SYMBOL_BLOCK_VALUE (fn_sym
);
4167 find_label_symbols_in_block (block
, name
, fn_sym
, completion_mode
,
4168 &result
, label_funcs_ret
);
4177 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4179 static std::vector
<symtab_and_line
>
4180 decode_digits_list_mode (struct linespec_state
*self
,
4182 struct symtab_and_line val
)
4187 gdb_assert (self
->list_mode
);
4189 std::vector
<symtab_and_line
> values
;
4191 for (ix
= 0; VEC_iterate (symtab_ptr
, ls
->file_symtabs
, ix
, elt
);
4194 /* The logic above should ensure this. */
4195 gdb_assert (elt
!= NULL
);
4197 set_current_program_space (SYMTAB_PSPACE (elt
));
4199 /* Simplistic search just for the list command. */
4200 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
4201 if (val
.symtab
== NULL
)
4203 val
.pspace
= SYMTAB_PSPACE (elt
);
4205 val
.explicit_line
= 1;
4207 add_sal_to_sals (self
, &values
, &val
, NULL
, 0);
4213 /* A helper for create_sals_line_offset that iterates over the symtabs,
4214 adding lines to the VEC. */
4216 static std::vector
<symtab_and_line
>
4217 decode_digits_ordinary (struct linespec_state
*self
,
4220 struct linetable_entry
**best_entry
)
4225 std::vector
<symtab_and_line
> sals
;
4226 for (ix
= 0; VEC_iterate (symtab_ptr
, ls
->file_symtabs
, ix
, elt
); ++ix
)
4228 std::vector
<CORE_ADDR
> pcs
;
4230 /* The logic above should ensure this. */
4231 gdb_assert (elt
!= NULL
);
4233 set_current_program_space (SYMTAB_PSPACE (elt
));
4235 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
4236 for (CORE_ADDR pc
: pcs
)
4238 struct symtab_and_line sal
;
4241 sal
.pspace
= SYMTAB_PSPACE (elt
);
4245 sals
.push_back (std::move (sal
));
4254 /* Return the line offset represented by VARIABLE. */
4256 static struct line_offset
4257 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
4261 struct line_offset offset
= {0, LINE_OFFSET_NONE
};
4263 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
4266 while (*p
>= '0' && *p
<= '9')
4268 if (!*p
) /* Reached end of token without hitting non-digit. */
4270 /* We have a value history reference. */
4271 struct value
*val_history
;
4273 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
4275 = access_value_history ((variable
[1] == '$') ? -index
: index
);
4276 if (TYPE_CODE (value_type (val_history
)) != TYPE_CODE_INT
)
4277 error (_("History values used in line "
4278 "specs must have integer values."));
4279 offset
.offset
= value_as_long (val_history
);
4283 /* Not all digits -- may be user variable/function or a
4284 convenience variable. */
4286 struct internalvar
*ivar
;
4288 /* Try it as a convenience variable. If it is not a convenience
4289 variable, return and allow normal symbol lookup to occur. */
4290 ivar
= lookup_only_internalvar (variable
+ 1);
4292 /* No internal variable with that name. Mark the offset
4293 as unknown to allow the name to be looked up as a symbol. */
4294 offset
.sign
= LINE_OFFSET_UNKNOWN
;
4297 /* We found a valid variable name. If it is not an integer,
4299 if (!get_internalvar_integer (ivar
, &valx
))
4300 error (_("Convenience variables used in line "
4301 "specs must have integer values."));
4303 offset
.offset
= valx
;
4311 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
4312 linespec; return the SAL in RESULT. This function should return SALs
4313 matching those from find_function_start_sal, otherwise false
4314 multiple-locations breakpoints could be placed. */
4317 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
4318 struct minimal_symbol
*msymbol
,
4319 std::vector
<symtab_and_line
> *result
)
4321 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
4323 struct symtab_and_line sal
;
4325 sal
= find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
),
4326 (struct obj_section
*) 0, 0);
4327 sal
.section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
);
4329 /* The minimal symbol might point to a function descriptor;
4330 resolve it to the actual code address instead. */
4331 pc
= gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
4333 sal
= find_pc_sect_line (pc
, NULL
, 0);
4335 if (self
->funfirstline
)
4337 if (sal
.symtab
!= NULL
4338 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal
.symtab
))
4339 || SYMTAB_LANGUAGE (sal
.symtab
) == language_asm
))
4341 /* If gdbarch_convert_from_func_ptr_addr does not apply then
4342 sal.SECTION, sal.LINE&co. will stay correct from above.
4343 If gdbarch_convert_from_func_ptr_addr applies then
4344 sal.SECTION is cleared from above and sal.LINE&co. will
4345 stay correct from the last find_pc_sect_line above. */
4346 sal
.pc
= MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
4347 sal
.pc
= gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
,
4349 if (gdbarch_skip_entrypoint_p (gdbarch
))
4350 sal
.pc
= gdbarch_skip_entrypoint (gdbarch
, sal
.pc
);
4353 skip_prologue_sal (&sal
);
4356 if (maybe_add_address (self
->addr_set
, objfile
->pspace
, sal
.pc
))
4357 add_sal_to_sals (self
, result
, &sal
, MSYMBOL_NATURAL_NAME (msymbol
), 0);
4360 /* A helper struct to pass some data through
4361 iterate_over_minimal_symbols. */
4363 struct collect_minsyms
4365 /* The objfile we're examining. */
4366 struct objfile
*objfile
;
4368 /* Only search the given symtab, or NULL to search for all symbols. */
4369 struct symtab
*symtab
;
4371 /* The funfirstline setting from the initial call. */
4374 /* The list_mode setting from the initial call. */
4377 /* The resulting symbols. */
4378 VEC (bound_minimal_symbol_d
) *msyms
;
4381 /* A helper function to classify a minimal_symbol_type according to
4385 classify_mtype (enum minimal_symbol_type t
)
4392 /* Intermediate priority. */
4395 case mst_solib_trampoline
:
4396 /* Lowest priority. */
4400 /* Highest priority. */
4405 /* Callback for qsort that sorts symbols by priority. */
4408 compare_msyms (const void *a
, const void *b
)
4410 const bound_minimal_symbol_d
*moa
= (const bound_minimal_symbol_d
*) a
;
4411 const bound_minimal_symbol_d
*mob
= (const bound_minimal_symbol_d
*) b
;
4412 enum minimal_symbol_type ta
= MSYMBOL_TYPE (moa
->minsym
);
4413 enum minimal_symbol_type tb
= MSYMBOL_TYPE (mob
->minsym
);
4415 return classify_mtype (ta
) - classify_mtype (tb
);
4418 /* Callback for iterate_over_minimal_symbols that adds the symbol to
4422 add_minsym (struct minimal_symbol
*minsym
, void *d
)
4424 struct collect_minsyms
*info
= (struct collect_minsyms
*) d
;
4425 bound_minimal_symbol_d mo
;
4428 mo
.objfile
= info
->objfile
;
4430 if (info
->symtab
!= NULL
)
4433 struct symtab_and_line sal
;
4434 struct gdbarch
*gdbarch
= get_objfile_arch (info
->objfile
);
4436 sal
= find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info
->objfile
, minsym
),
4438 sal
.section
= MSYMBOL_OBJ_SECTION (info
->objfile
, minsym
);
4440 = gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
4442 sal
= find_pc_sect_line (pc
, NULL
, 0);
4444 if (info
->symtab
!= sal
.symtab
)
4448 /* Exclude data symbols when looking for breakpoint locations. */
4449 if (!info
->list_mode
)
4450 switch (minsym
->type
)
4452 case mst_slot_got_plt
:
4459 /* Make sure this minsym is not a function descriptor
4460 before we decide to discard it. */
4461 struct gdbarch
*gdbarch
= get_objfile_arch (info
->objfile
);
4462 CORE_ADDR addr
= gdbarch_convert_from_func_ptr_addr
4463 (gdbarch
, BMSYMBOL_VALUE_ADDRESS (mo
),
4466 if (addr
== BMSYMBOL_VALUE_ADDRESS (mo
))
4471 VEC_safe_push (bound_minimal_symbol_d
, info
->msyms
, &mo
);
4474 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
4475 is not NULL, the search is restricted to just that program
4478 If SYMTAB is NULL, search all objfiles, otherwise
4479 restrict results to the given SYMTAB. */
4482 search_minsyms_for_name (struct collect_info
*info
, const char *name
,
4483 struct program_space
*search_pspace
,
4484 struct symtab
*symtab
)
4486 struct collect_minsyms local
;
4487 struct cleanup
*cleanup
;
4489 memset (&local
, 0, sizeof (local
));
4490 local
.funfirstline
= info
->state
->funfirstline
;
4491 local
.list_mode
= info
->state
->list_mode
;
4492 local
.symtab
= symtab
;
4494 cleanup
= make_cleanup (VEC_cleanup (bound_minimal_symbol_d
), &local
.msyms
);
4498 struct program_space
*pspace
;
4500 ALL_PSPACES (pspace
)
4502 struct objfile
*objfile
;
4504 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
4506 if (pspace
->executing_startup
)
4509 set_current_program_space (pspace
);
4511 ALL_OBJFILES (objfile
)
4513 local
.objfile
= objfile
;
4514 iterate_over_minimal_symbols (objfile
, name
, add_minsym
, &local
);
4520 if (search_pspace
== NULL
|| SYMTAB_PSPACE (symtab
) == search_pspace
)
4522 set_current_program_space (SYMTAB_PSPACE (symtab
));
4523 local
.objfile
= SYMTAB_OBJFILE(symtab
);
4524 iterate_over_minimal_symbols (local
.objfile
, name
, add_minsym
,
4529 if (!VEC_empty (bound_minimal_symbol_d
, local
.msyms
))
4533 bound_minimal_symbol_d
*item
;
4535 qsort (VEC_address (bound_minimal_symbol_d
, local
.msyms
),
4536 VEC_length (bound_minimal_symbol_d
, local
.msyms
),
4537 sizeof (bound_minimal_symbol_d
),
4540 /* Now the minsyms are in classification order. So, we walk
4541 over them and process just the minsyms with the same
4542 classification as the very first minsym in the list. */
4543 item
= VEC_index (bound_minimal_symbol_d
, local
.msyms
, 0);
4544 classification
= classify_mtype (MSYMBOL_TYPE (item
->minsym
));
4547 VEC_iterate (bound_minimal_symbol_d
, local
.msyms
, ix
, item
);
4550 if (classify_mtype (MSYMBOL_TYPE (item
->minsym
)) != classification
)
4553 VEC_safe_push (bound_minimal_symbol_d
,
4554 info
->result
.minimal_symbols
, item
);
4558 do_cleanups (cleanup
);
4561 /* A helper function to add all symbols matching NAME to INFO. If
4562 PSPACE is not NULL, the search is restricted to just that program
4566 add_matching_symbols_to_info (const char *name
,
4567 struct collect_info
*info
,
4568 struct program_space
*pspace
)
4573 for (ix
= 0; VEC_iterate (symtab_ptr
, info
->file_symtabs
, ix
, elt
); ++ix
)
4577 iterate_over_all_matching_symtabs (info
->state
, name
, VAR_DOMAIN
,
4578 pspace
, true, [&] (symbol
*sym
)
4579 { return info
->add_symbol (sym
); });
4580 search_minsyms_for_name (info
, name
, pspace
, NULL
);
4582 else if (pspace
== NULL
|| pspace
== SYMTAB_PSPACE (elt
))
4584 int prev_len
= VEC_length (symbolp
, info
->result
.symbols
);
4586 /* Program spaces that are executing startup should have
4587 been filtered out earlier. */
4588 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
4589 set_current_program_space (SYMTAB_PSPACE (elt
));
4590 iterate_over_file_blocks (elt
, name
, VAR_DOMAIN
, [&] (symbol
*sym
)
4591 { return info
->add_symbol (sym
); });
4593 /* If no new symbols were found in this iteration and this symtab
4594 is in assembler, we might actually be looking for a label for
4595 which we don't have debug info. Check for a minimal symbol in
4597 if (prev_len
== VEC_length (symbolp
, info
->result
.symbols
)
4598 && elt
->language
== language_asm
)
4599 search_minsyms_for_name (info
, name
, pspace
, elt
);
4606 /* Now come some functions that are called from multiple places within
4610 symbol_to_sal (struct symtab_and_line
*result
,
4611 int funfirstline
, struct symbol
*sym
)
4613 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
4615 *result
= find_function_start_sal (sym
, funfirstline
);
4620 if (SYMBOL_CLASS (sym
) == LOC_LABEL
&& SYMBOL_VALUE_ADDRESS (sym
) != 0)
4623 result
->symtab
= symbol_symtab (sym
);
4624 result
->line
= SYMBOL_LINE (sym
);
4625 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
4626 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
4627 result
->explicit_pc
= 1;
4630 else if (funfirstline
)
4634 else if (SYMBOL_LINE (sym
) != 0)
4636 /* We know its line number. */
4638 result
->symtab
= symbol_symtab (sym
);
4639 result
->line
= SYMBOL_LINE (sym
);
4640 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
4648 linespec_result::~linespec_result ()
4650 for (linespec_sals
&lsal
: lsals
)
4651 xfree (lsal
.canonical
);
4654 /* Return the quote characters permitted by the linespec parser. */
4657 get_gdb_linespec_parser_quote_characters (void)
4659 return linespec_quote_characters
;