1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2016 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"
48 typedef struct symbol
*symbolp
;
51 typedef struct type
*typep
;
54 /* An address entry is used to ensure that any given location is only
55 added to the result a single time. It holds an address and the
56 program space from which the address came. */
60 struct program_space
*pspace
;
64 typedef struct bound_minimal_symbol bound_minimal_symbol_d
;
66 DEF_VEC_O (bound_minimal_symbol_d
);
68 /* A linespec. Elements of this structure are filled in by a parser
69 (either parse_linespec or some other function). The structure is
70 then converted into SALs by convert_linespec_to_sals. */
74 /* An explicit location describing the SaLs. */
75 struct explicit_location explicit_loc
;
77 /* The list of symtabs to search to which to limit the search. May not
78 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
79 filename), FILE_SYMTABS should contain one single NULL member. This
80 will cause the code to use the default symtab. */
81 VEC (symtab_ptr
) *file_symtabs
;
83 /* A list of matching function symbols and minimal symbols. Both lists
84 may be NULL if no matching symbols were found. */
85 VEC (symbolp
) *function_symbols
;
86 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
88 /* A structure of matching label symbols and the corresponding
89 function symbol in which the label was found. Both may be NULL
90 or both must be non-NULL. */
93 VEC (symbolp
) *label_symbols
;
94 VEC (symbolp
) *function_symbols
;
97 typedef struct linespec
*linespec_p
;
99 /* A canonical linespec represented as a symtab-related string.
101 Each entry represents the "SYMTAB:SUFFIX" linespec string.
102 SYMTAB can be converted for example by symtab_to_fullname or
103 symtab_to_filename_for_display as needed. */
105 struct linespec_canonical_name
107 /* Remaining text part of the linespec string. */
110 /* If NULL then SUFFIX is the whole linespec string. */
111 struct symtab
*symtab
;
114 /* An instance of this is used to keep all state while linespec
115 operates. This instance is passed around as a 'this' pointer to
116 the various implementation methods. */
118 struct linespec_state
120 /* The language in use during linespec processing. */
121 const struct language_defn
*language
;
123 /* The program space as seen when the module was entered. */
124 struct program_space
*program_space
;
126 /* If not NULL, the search is restricted to just this program
128 struct program_space
*search_pspace
;
130 /* The default symtab to use, if no other symtab is specified. */
131 struct symtab
*default_symtab
;
133 /* The default line to use. */
136 /* The 'funfirstline' value that was passed in to decode_line_1 or
140 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
143 /* The 'canonical' value passed to decode_line_full, or NULL. */
144 struct linespec_result
*canonical
;
146 /* Canonical strings that mirror the symtabs_and_lines result. */
147 struct linespec_canonical_name
*canonical_names
;
149 /* This is a set of address_entry objects which is used to prevent
150 duplicate symbols from being entered into the result. */
153 /* Are we building a linespec? */
157 /* This is a helper object that is used when collecting symbols into a
162 /* The linespec object in use. */
163 struct linespec_state
*state
;
165 /* A list of symtabs to which to restrict matches. */
166 VEC (symtab_ptr
) *file_symtabs
;
168 /* The result being accumulated. */
171 VEC (symbolp
) *symbols
;
172 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
183 /* A colon "separator" */
195 /* EOI (end of input) */
201 typedef enum ls_token_type linespec_token_type
;
203 /* List of keywords */
205 static const char * const linespec_keywords
[] = { "if", "thread", "task" };
206 #define IF_KEYWORD_INDEX 0
208 /* A token of the linespec lexer */
212 /* The type of the token */
213 linespec_token_type type
;
215 /* Data for the token */
218 /* A string, given as a stoken */
219 struct stoken string
;
225 typedef struct ls_token linespec_token
;
227 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
228 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
230 /* An instance of the linespec parser. */
234 /* Lexer internal data */
237 /* Save head of input stream. */
238 const char *saved_arg
;
240 /* Head of the input stream. */
242 #define PARSER_STREAM(P) ((P)->lexer.stream)
244 /* The current token. */
245 linespec_token current
;
248 /* Is the entire linespec quote-enclosed? */
249 int is_quote_enclosed
;
251 /* The state of the parse. */
252 struct linespec_state state
;
253 #define PARSER_STATE(PPTR) (&(PPTR)->state)
255 /* The result of the parse. */
256 struct linespec result
;
257 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
259 typedef struct ls_parser linespec_parser
;
261 /* A convenience macro for accessing the explicit location result of
263 #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
265 /* Prototypes for local functions. */
267 static void iterate_over_file_blocks (struct symtab
*symtab
,
268 const char *name
, domain_enum domain
,
269 symbol_found_callback_ftype
*callback
,
272 static void initialize_defaults (struct symtab
**default_symtab
,
275 CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
277 static struct symtabs_and_lines
decode_objc (struct linespec_state
*self
,
281 static VEC (symtab_ptr
) *symtabs_from_filename (const char *,
282 struct program_space
*pspace
);
284 static VEC (symbolp
) *find_label_symbols (struct linespec_state
*self
,
285 VEC (symbolp
) *function_symbols
,
286 VEC (symbolp
) **label_funcs_ret
,
289 static void find_linespec_symbols (struct linespec_state
*self
,
290 VEC (symtab_ptr
) *file_symtabs
,
292 VEC (symbolp
) **symbols
,
293 VEC (bound_minimal_symbol_d
) **minsyms
);
295 static struct line_offset
296 linespec_parse_variable (struct linespec_state
*self
,
297 const char *variable
);
299 static int symbol_to_sal (struct symtab_and_line
*result
,
300 int funfirstline
, struct symbol
*sym
);
302 static void add_matching_symbols_to_info (const char *name
,
303 struct collect_info
*info
,
304 struct program_space
*pspace
);
306 static void add_all_symbol_names_from_pspace (struct collect_info
*info
,
307 struct program_space
*pspace
,
308 VEC (const_char_ptr
) *names
);
310 static VEC (symtab_ptr
) *
311 collect_symtabs_from_filename (const char *file
,
312 struct program_space
*pspace
);
314 static void decode_digits_ordinary (struct linespec_state
*self
,
317 struct symtabs_and_lines
*sals
,
318 struct linetable_entry
**best_entry
);
320 static void decode_digits_list_mode (struct linespec_state
*self
,
322 struct symtabs_and_lines
*values
,
323 struct symtab_and_line val
);
325 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
326 struct minimal_symbol
*msymbol
,
327 struct symtabs_and_lines
*result
);
329 static int compare_symbols (const void *a
, const void *b
);
331 static int compare_msymbols (const void *a
, const void *b
);
333 /* Permitted quote characters for the parser. This is different from the
334 completer's quote characters to allow backward compatibility with the
336 static const char *const linespec_quote_characters
= "\"\'";
338 /* Lexer functions. */
340 /* Lex a number from the input in PARSER. This only supports
343 Return true if input is decimal numbers. Return false if not. */
346 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
348 tokenp
->type
= LSTOKEN_NUMBER
;
349 LS_TOKEN_STOKEN (*tokenp
).length
= 0;
350 LS_TOKEN_STOKEN (*tokenp
).ptr
= PARSER_STREAM (parser
);
352 /* Keep any sign at the start of the stream. */
353 if (*PARSER_STREAM (parser
) == '+' || *PARSER_STREAM (parser
) == '-')
355 ++LS_TOKEN_STOKEN (*tokenp
).length
;
356 ++(PARSER_STREAM (parser
));
359 while (isdigit (*PARSER_STREAM (parser
)))
361 ++LS_TOKEN_STOKEN (*tokenp
).length
;
362 ++(PARSER_STREAM (parser
));
365 /* If the next character in the input buffer is not a space, comma,
366 quote, or colon, this input does not represent a number. */
367 if (*PARSER_STREAM (parser
) != '\0'
368 && !isspace (*PARSER_STREAM (parser
)) && *PARSER_STREAM (parser
) != ','
369 && *PARSER_STREAM (parser
) != ':'
370 && !strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
372 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (*tokenp
).ptr
;
379 /* See linespec.h. */
382 linespec_lexer_lex_keyword (const char *p
)
388 for (i
= 0; i
< ARRAY_SIZE (linespec_keywords
); ++i
)
390 int len
= strlen (linespec_keywords
[i
]);
392 /* If P begins with one of the keywords and the next
393 character is whitespace, we may have found a keyword.
394 It is only a keyword if it is not followed by another
396 if (strncmp (p
, linespec_keywords
[i
], len
) == 0
401 /* Special case: "if" ALWAYS stops the lexer, since it
402 is not possible to predict what is going to appear in
403 the condition, which can only be parsed after SaLs have
405 if (i
!= IF_KEYWORD_INDEX
)
408 p
= skip_spaces_const (p
);
409 for (j
= 0; j
< ARRAY_SIZE (linespec_keywords
); ++j
)
411 int nextlen
= strlen (linespec_keywords
[j
]);
413 if (strncmp (p
, linespec_keywords
[j
], nextlen
) == 0
414 && isspace (p
[nextlen
]))
419 return linespec_keywords
[i
];
427 /* See description in linespec.h. */
430 is_ada_operator (const char *string
)
432 const struct ada_opname_map
*mapping
;
434 for (mapping
= ada_opname_table
;
435 mapping
->encoded
!= NULL
436 && !startswith (string
, mapping
->decoded
); ++mapping
)
439 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
442 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
443 the location of QUOTE_CHAR, or NULL if not found. */
446 skip_quote_char (const char *string
, char quote_char
)
448 const char *p
, *last
;
450 p
= last
= find_toplevel_char (string
, quote_char
);
451 while (p
&& *p
!= '\0' && *p
!= ':')
453 p
= find_toplevel_char (p
, quote_char
);
461 /* Make a writable copy of the string given in TOKEN, trimming
462 any trailing whitespace. */
465 copy_token_string (linespec_token token
)
469 if (token
.type
== LSTOKEN_KEYWORD
)
470 return xstrdup (LS_TOKEN_KEYWORD (token
));
472 str
= savestring (LS_TOKEN_STOKEN (token
).ptr
,
473 LS_TOKEN_STOKEN (token
).length
);
474 s
= remove_trailing_whitespace (str
, str
+ LS_TOKEN_STOKEN (token
).length
);
480 /* Does P represent the end of a quote-enclosed linespec? */
483 is_closing_quote_enclosed (const char *p
)
485 if (strchr (linespec_quote_characters
, *p
))
487 p
= skip_spaces ((char *) p
);
488 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
491 /* Find the end of the parameter list that starts with *INPUT.
492 This helper function assists with lexing string segments
493 which might contain valid (non-terminating) commas. */
496 find_parameter_list_end (const char *input
)
498 char end_char
, start_char
;
503 if (start_char
== '(')
505 else if (start_char
== '<')
514 if (*p
== start_char
)
516 else if (*p
== end_char
)
531 /* Lex a string from the input in PARSER. */
533 static linespec_token
534 linespec_lexer_lex_string (linespec_parser
*parser
)
536 linespec_token token
;
537 const char *start
= PARSER_STREAM (parser
);
539 token
.type
= LSTOKEN_STRING
;
541 /* If the input stream starts with a quote character, skip to the next
542 quote character, regardless of the content. */
543 if (strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
546 char quote_char
= *PARSER_STREAM (parser
);
548 /* Special case: Ada operators. */
549 if (PARSER_STATE (parser
)->language
->la_language
== language_ada
550 && quote_char
== '\"')
552 int len
= is_ada_operator (PARSER_STREAM (parser
));
556 /* The input is an Ada operator. Return the quoted string
558 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
559 LS_TOKEN_STOKEN (token
).length
= len
;
560 PARSER_STREAM (parser
) += len
;
564 /* The input does not represent an Ada operator -- fall through
565 to normal quoted string handling. */
568 /* Skip past the beginning quote. */
569 ++(PARSER_STREAM (parser
));
571 /* Mark the start of the string. */
572 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
574 /* Skip to the ending quote. */
575 end
= skip_quote_char (PARSER_STREAM (parser
), quote_char
);
577 /* Error if the input did not terminate properly. */
579 error (_("unmatched quote"));
581 /* Skip over the ending quote and mark the length of the string. */
582 PARSER_STREAM (parser
) = (char *) ++end
;
583 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 2 - start
;
589 /* Otherwise, only identifier characters are permitted.
590 Spaces are the exception. In general, we keep spaces,
591 but only if the next characters in the input do not resolve
592 to one of the keywords.
594 This allows users to forgo quoting CV-qualifiers, template arguments,
595 and similar common language constructs. */
599 if (isspace (*PARSER_STREAM (parser
)))
601 p
= skip_spaces_const (PARSER_STREAM (parser
));
602 /* When we get here we know we've found something followed by
603 a space (we skip over parens and templates below).
604 So if we find a keyword now, we know it is a keyword and not,
605 say, a function name. */
606 if (linespec_lexer_lex_keyword (p
) != NULL
)
608 LS_TOKEN_STOKEN (token
).ptr
= start
;
609 LS_TOKEN_STOKEN (token
).length
610 = PARSER_STREAM (parser
) - start
;
614 /* Advance past the whitespace. */
615 PARSER_STREAM (parser
) = p
;
618 /* If the next character is EOI or (single) ':', the
619 string is complete; return the token. */
620 if (*PARSER_STREAM (parser
) == 0)
622 LS_TOKEN_STOKEN (token
).ptr
= start
;
623 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
626 else if (PARSER_STREAM (parser
)[0] == ':')
628 /* Do not tokenize the C++ scope operator. */
629 if (PARSER_STREAM (parser
)[1] == ':')
630 ++(PARSER_STREAM (parser
));
632 /* Do not tokenify if the input length so far is one
633 (i.e, a single-letter drive name) and the next character
634 is a directory separator. This allows Windows-style
635 paths to be recognized as filenames without quoting it. */
636 else if ((PARSER_STREAM (parser
) - start
) != 1
637 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser
)[1]))
639 LS_TOKEN_STOKEN (token
).ptr
= start
;
640 LS_TOKEN_STOKEN (token
).length
641 = PARSER_STREAM (parser
) - start
;
645 /* Special case: permit quote-enclosed linespecs. */
646 else if (parser
->is_quote_enclosed
647 && strchr (linespec_quote_characters
,
648 *PARSER_STREAM (parser
))
649 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
651 LS_TOKEN_STOKEN (token
).ptr
= start
;
652 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
655 /* Because commas may terminate a linespec and appear in
656 the middle of valid string input, special cases for
657 '<' and '(' are necessary. */
658 else if (*PARSER_STREAM (parser
) == '<'
659 || *PARSER_STREAM (parser
) == '(')
663 p
= find_parameter_list_end (PARSER_STREAM (parser
));
666 PARSER_STREAM (parser
) = p
;
670 /* Commas are terminators, but not if they are part of an
672 else if (*PARSER_STREAM (parser
) == ',')
674 if ((PARSER_STATE (parser
)->language
->la_language
676 && (PARSER_STREAM (parser
) - start
) > 8
677 /* strlen ("operator") */)
679 const char *p
= strstr (start
, "operator");
681 if (p
!= NULL
&& is_operator_name (p
))
683 /* This is an operator name. Keep going. */
684 ++(PARSER_STREAM (parser
));
689 /* Comma terminates the string. */
690 LS_TOKEN_STOKEN (token
).ptr
= start
;
691 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
695 /* Advance the stream. */
696 ++(PARSER_STREAM (parser
));
703 /* Lex a single linespec token from PARSER. */
705 static linespec_token
706 linespec_lexer_lex_one (linespec_parser
*parser
)
710 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
712 /* Skip any whitespace. */
713 PARSER_STREAM (parser
) = skip_spaces_const (PARSER_STREAM (parser
));
715 /* Check for a keyword, they end the linespec. */
716 keyword
= linespec_lexer_lex_keyword (PARSER_STREAM (parser
));
719 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
720 LS_TOKEN_KEYWORD (parser
->lexer
.current
) = keyword
;
721 /* We do not advance the stream here intentionally:
722 we would like lexing to stop when a keyword is seen.
724 PARSER_STREAM (parser) += strlen (keyword); */
726 return parser
->lexer
.current
;
729 /* Handle other tokens. */
730 switch (*PARSER_STREAM (parser
))
733 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
737 case '0': case '1': case '2': case '3': case '4':
738 case '5': case '6': case '7': case '8': case '9':
739 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
740 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
744 /* If we have a scope operator, lex the input as a string.
745 Otherwise, return LSTOKEN_COLON. */
746 if (PARSER_STREAM (parser
)[1] == ':')
747 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
750 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
751 ++(PARSER_STREAM (parser
));
755 case '\'': case '\"':
756 /* Special case: permit quote-enclosed linespecs. */
757 if (parser
->is_quote_enclosed
758 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
760 ++(PARSER_STREAM (parser
));
761 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
764 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
768 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
769 LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
770 = PARSER_STREAM (parser
);
771 LS_TOKEN_STOKEN (parser
->lexer
.current
).length
= 1;
772 ++(PARSER_STREAM (parser
));
776 /* If the input is not a number, it must be a string.
777 [Keywords were already considered above.] */
778 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
783 return parser
->lexer
.current
;
786 /* Consume the current token and return the next token in PARSER's
789 static linespec_token
790 linespec_lexer_consume_token (linespec_parser
*parser
)
792 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
793 return linespec_lexer_lex_one (parser
);
796 /* Return the next token without consuming the current token. */
798 static linespec_token
799 linespec_lexer_peek_token (linespec_parser
*parser
)
802 const char *saved_stream
= PARSER_STREAM (parser
);
803 linespec_token saved_token
= parser
->lexer
.current
;
805 next
= linespec_lexer_consume_token (parser
);
806 PARSER_STREAM (parser
) = saved_stream
;
807 parser
->lexer
.current
= saved_token
;
811 /* Helper functions. */
813 /* Add SAL to SALS. */
816 add_sal_to_sals_basic (struct symtabs_and_lines
*sals
,
817 struct symtab_and_line
*sal
)
820 sals
->sals
= XRESIZEVEC (struct symtab_and_line
, sals
->sals
, sals
->nelts
);
821 sals
->sals
[sals
->nelts
- 1] = *sal
;
824 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
825 the new sal, if needed. If not NULL, SYMNAME is the name of the
826 symbol to use when constructing the new canonical name.
828 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
829 canonical name for the SAL. */
832 add_sal_to_sals (struct linespec_state
*self
,
833 struct symtabs_and_lines
*sals
,
834 struct symtab_and_line
*sal
,
835 const char *symname
, int literal_canonical
)
837 add_sal_to_sals_basic (sals
, sal
);
841 struct linespec_canonical_name
*canonical
;
843 self
->canonical_names
= XRESIZEVEC (struct linespec_canonical_name
,
844 self
->canonical_names
, sals
->nelts
);
845 canonical
= &self
->canonical_names
[sals
->nelts
- 1];
846 if (!literal_canonical
&& sal
->symtab
)
848 symtab_to_fullname (sal
->symtab
);
850 /* Note that the filter doesn't have to be a valid linespec
851 input. We only apply the ":LINE" treatment to Ada for
853 if (symname
!= NULL
&& sal
->line
!= 0
854 && self
->language
->la_language
== language_ada
)
855 canonical
->suffix
= xstrprintf ("%s:%d", symname
, sal
->line
);
856 else if (symname
!= NULL
)
857 canonical
->suffix
= xstrdup (symname
);
859 canonical
->suffix
= xstrprintf ("%d", sal
->line
);
860 canonical
->symtab
= sal
->symtab
;
865 canonical
->suffix
= xstrdup (symname
);
867 canonical
->suffix
= xstrdup ("<unknown>");
868 canonical
->symtab
= NULL
;
873 /* A hash function for address_entry. */
876 hash_address_entry (const void *p
)
878 const struct address_entry
*aep
= (const struct address_entry
*) p
;
881 hash
= iterative_hash_object (aep
->pspace
, 0);
882 return iterative_hash_object (aep
->addr
, hash
);
885 /* An equality function for address_entry. */
888 eq_address_entry (const void *a
, const void *b
)
890 const struct address_entry
*aea
= (const struct address_entry
*) a
;
891 const struct address_entry
*aeb
= (const struct address_entry
*) b
;
893 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
896 /* Check whether the address, represented by PSPACE and ADDR, is
897 already in the set. If so, return 0. Otherwise, add it and return
901 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
903 struct address_entry e
, *p
;
908 slot
= htab_find_slot (set
, &e
, INSERT
);
912 p
= XNEW (struct address_entry
);
913 memcpy (p
, &e
, sizeof (struct address_entry
));
919 /* A callback function and the additional data to call it with. */
921 struct symbol_and_data_callback
923 /* The callback to use. */
924 symbol_found_callback_ftype
*callback
;
926 /* Data to be passed to the callback. */
930 /* A helper for iterate_over_all_matching_symtabs that is used to
931 restrict calls to another callback to symbols representing inline
935 iterate_inline_only (struct symbol
*sym
, void *d
)
937 if (SYMBOL_INLINED (sym
))
939 struct symbol_and_data_callback
*cad
940 = (struct symbol_and_data_callback
*) d
;
942 return cad
->callback (sym
, cad
->data
);
944 return 1; /* Continue iterating. */
947 /* Some data for the expand_symtabs_matching callback. */
949 struct symbol_matcher_data
951 /* The lookup name against which symbol name should be compared. */
952 const char *lookup_name
;
954 /* The routine to be used for comparison. */
955 symbol_name_cmp_ftype symbol_name_cmp
;
958 /* A helper for iterate_over_all_matching_symtabs that is passed as a
959 callback to the expand_symtabs_matching method. */
962 iterate_name_matcher (const char *name
, void *d
)
964 const struct symbol_matcher_data
*data
965 = (const struct symbol_matcher_data
*) d
;
967 if (data
->symbol_name_cmp (name
, data
->lookup_name
) == 0)
968 return 1; /* Expand this symbol's symbol table. */
969 return 0; /* Skip this symbol. */
972 /* A helper that walks over all matching symtabs in all objfiles and
973 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
974 not NULL, then the search is restricted to just that program
975 space. If INCLUDE_INLINE is nonzero then symbols representing
976 inlined instances of functions will be included in the result. */
979 iterate_over_all_matching_symtabs (struct linespec_state
*state
,
981 const domain_enum domain
,
982 symbol_found_callback_ftype
*callback
,
984 struct program_space
*search_pspace
,
987 struct objfile
*objfile
;
988 struct program_space
*pspace
;
989 struct symbol_matcher_data matcher_data
;
991 matcher_data
.lookup_name
= name
;
992 matcher_data
.symbol_name_cmp
=
993 state
->language
->la_get_symbol_name_cmp
!= NULL
994 ? state
->language
->la_get_symbol_name_cmp (name
)
999 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
1001 if (pspace
->executing_startup
)
1004 set_current_program_space (pspace
);
1006 ALL_OBJFILES (objfile
)
1008 struct compunit_symtab
*cu
;
1011 objfile
->sf
->qf
->expand_symtabs_matching (objfile
, NULL
,
1012 iterate_name_matcher
,
1016 ALL_OBJFILE_COMPUNITS (objfile
, cu
)
1018 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1020 iterate_over_file_blocks (symtab
, name
, domain
, callback
, data
);
1024 struct symbol_and_data_callback cad
= { callback
, data
};
1025 struct block
*block
;
1028 for (i
= FIRST_LOCAL_BLOCK
;
1029 i
< BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab
));
1032 block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), i
);
1033 state
->language
->la_iterate_over_symbols
1034 (block
, name
, domain
, iterate_inline_only
, &cad
);
1042 /* Returns the block to be used for symbol searches from
1043 the current location. */
1045 static const struct block
*
1046 get_current_search_block (void)
1048 const struct block
*block
;
1049 enum language save_language
;
1051 /* get_selected_block can change the current language when there is
1052 no selected frame yet. */
1053 save_language
= current_language
->la_language
;
1054 block
= get_selected_block (0);
1055 set_language (save_language
);
1060 /* Iterate over static and global blocks. */
1063 iterate_over_file_blocks (struct symtab
*symtab
,
1064 const char *name
, domain_enum domain
,
1065 symbol_found_callback_ftype
*callback
, void *data
)
1067 struct block
*block
;
1069 for (block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1071 block
= BLOCK_SUPERBLOCK (block
))
1072 LA_ITERATE_OVER_SYMBOLS (block
, name
, domain
, callback
, data
);
1075 /* A helper for find_method. This finds all methods in type T which
1076 match NAME. It adds matching symbol names to RESULT_NAMES, and
1077 adds T's direct superclasses to SUPERCLASSES. */
1080 find_methods (struct type
*t
, const char *name
,
1081 VEC (const_char_ptr
) **result_names
,
1082 VEC (typep
) **superclasses
)
1085 const char *class_name
= type_name_no_tag (t
);
1087 /* Ignore this class if it doesn't have a name. This is ugly, but
1088 unless we figure out how to get the physname without the name of
1089 the class, then the loop can't do any good. */
1094 t
= check_typedef (t
);
1096 /* Loop over each method name. At this level, all overloads of a name
1097 are counted as a single name. There is an inner loop which loops over
1100 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1101 method_counter
>= 0;
1104 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1105 char dem_opname
[64];
1107 if (startswith (method_name
, "__") ||
1108 startswith (method_name
, "op") ||
1109 startswith (method_name
, "type"))
1111 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
1112 method_name
= dem_opname
;
1113 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
1114 method_name
= dem_opname
;
1117 if (strcmp_iw (method_name
, name
) == 0)
1121 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1127 const char *phys_name
;
1129 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1130 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1132 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1133 VEC_safe_push (const_char_ptr
, *result_names
, phys_name
);
1139 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1140 VEC_safe_push (typep
, *superclasses
, TYPE_BASECLASS (t
, ibase
));
1143 /* Find an instance of the character C in the string S that is outside
1144 of all parenthesis pairs, single-quoted strings, and double-quoted
1145 strings. Also, ignore the char within a template name, like a ','
1146 within foo<int, int>. */
1149 find_toplevel_char (const char *s
, char c
)
1151 int quoted
= 0; /* zero if we're not in quotes;
1152 '"' if we're in a double-quoted string;
1153 '\'' if we're in a single-quoted string. */
1154 int depth
= 0; /* Number of unclosed parens we've seen. */
1157 for (scan
= s
; *scan
; scan
++)
1161 if (*scan
== quoted
)
1163 else if (*scan
== '\\' && *(scan
+ 1))
1166 else if (*scan
== c
&& ! quoted
&& depth
== 0)
1168 else if (*scan
== '"' || *scan
== '\'')
1170 else if (*scan
== '(' || *scan
== '<')
1172 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
1179 /* The string equivalent of find_toplevel_char. Returns a pointer
1180 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1181 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1184 find_toplevel_string (const char *haystack
, const char *needle
)
1186 const char *s
= haystack
;
1190 s
= find_toplevel_char (s
, *needle
);
1194 /* Found first char in HAYSTACK; check rest of string. */
1195 if (startswith (s
, needle
))
1198 /* Didn't find it; loop over HAYSTACK, looking for the next
1199 instance of the first character of NEEDLE. */
1203 while (s
!= NULL
&& *s
!= '\0');
1205 /* NEEDLE was not found in HAYSTACK. */
1209 /* Convert CANONICAL to its string representation using
1210 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1213 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1215 if (canonical
->symtab
== NULL
)
1216 return xstrdup (canonical
->suffix
);
1218 return xstrprintf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1222 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1223 and store the result in SELF->CANONICAL. */
1226 filter_results (struct linespec_state
*self
,
1227 struct symtabs_and_lines
*result
,
1228 VEC (const_char_ptr
) *filters
)
1233 for (i
= 0; VEC_iterate (const_char_ptr
, filters
, i
, name
); ++i
)
1235 struct linespec_sals lsal
;
1238 memset (&lsal
, 0, sizeof (lsal
));
1240 for (j
= 0; j
< result
->nelts
; ++j
)
1242 const struct linespec_canonical_name
*canonical
;
1244 struct cleanup
*cleanup
;
1246 canonical
= &self
->canonical_names
[j
];
1247 fullform
= canonical_to_fullform (canonical
);
1248 cleanup
= make_cleanup (xfree
, fullform
);
1250 if (strcmp (name
, fullform
) == 0)
1251 add_sal_to_sals_basic (&lsal
.sals
, &result
->sals
[j
]);
1253 do_cleanups (cleanup
);
1256 if (lsal
.sals
.nelts
> 0)
1258 lsal
.canonical
= xstrdup (name
);
1259 VEC_safe_push (linespec_sals
, self
->canonical
->sals
, &lsal
);
1263 self
->canonical
->pre_expanded
= 0;
1266 /* Store RESULT into SELF->CANONICAL. */
1269 convert_results_to_lsals (struct linespec_state
*self
,
1270 struct symtabs_and_lines
*result
)
1272 struct linespec_sals lsal
;
1274 lsal
.canonical
= NULL
;
1275 lsal
.sals
= *result
;
1276 VEC_safe_push (linespec_sals
, self
->canonical
->sals
, &lsal
);
1279 /* A structure that contains two string representations of a struct
1280 linespec_canonical_name:
1281 - one where the the symtab's fullname is used;
1282 - one where the filename followed the "set filename-display"
1285 struct decode_line_2_item
1287 /* The form using symtab_to_fullname.
1288 It must be xfree'ed after use. */
1291 /* The form using symtab_to_filename_for_display.
1292 It must be xfree'ed after use. */
1295 /* Field is initialized to zero and it is set to one if the user
1296 requested breakpoint for this entry. */
1297 unsigned int selected
: 1;
1300 /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1301 secondarily by FULLFORM. */
1304 decode_line_2_compare_items (const void *ap
, const void *bp
)
1306 const struct decode_line_2_item
*a
= (const struct decode_line_2_item
*) ap
;
1307 const struct decode_line_2_item
*b
= (const struct decode_line_2_item
*) bp
;
1310 retval
= strcmp (a
->displayform
, b
->displayform
);
1314 return strcmp (a
->fullform
, b
->fullform
);
1317 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1318 will either return normally, throw an exception on multiple
1319 results, or present a menu to the user. On return, the SALS vector
1320 in SELF->CANONICAL is set up properly. */
1323 decode_line_2 (struct linespec_state
*self
,
1324 struct symtabs_and_lines
*result
,
1325 const char *select_mode
)
1327 char *args
, *prompt
;
1329 struct cleanup
*old_chain
;
1330 VEC (const_char_ptr
) *filters
= NULL
;
1331 struct get_number_or_range_state state
;
1332 struct decode_line_2_item
*items
;
1335 gdb_assert (select_mode
!= multiple_symbols_all
);
1336 gdb_assert (self
->canonical
!= NULL
);
1337 gdb_assert (result
->nelts
>= 1);
1339 old_chain
= make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
1341 /* Prepare ITEMS array. */
1342 items_count
= result
->nelts
;
1343 items
= XNEWVEC (struct decode_line_2_item
, items_count
);
1344 make_cleanup (xfree
, items
);
1345 for (i
= 0; i
< items_count
; ++i
)
1347 const struct linespec_canonical_name
*canonical
;
1348 struct decode_line_2_item
*item
;
1350 canonical
= &self
->canonical_names
[i
];
1351 gdb_assert (canonical
->suffix
!= NULL
);
1354 item
->fullform
= canonical_to_fullform (canonical
);
1355 make_cleanup (xfree
, item
->fullform
);
1357 if (canonical
->symtab
== NULL
)
1358 item
->displayform
= canonical
->suffix
;
1361 const char *fn_for_display
;
1363 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1364 item
->displayform
= xstrprintf ("%s:%s", fn_for_display
,
1366 make_cleanup (xfree
, item
->displayform
);
1372 /* Sort the list of method names. */
1373 qsort (items
, items_count
, sizeof (*items
), decode_line_2_compare_items
);
1375 /* Remove entries with the same FULLFORM. */
1376 if (items_count
>= 2)
1378 struct decode_line_2_item
*dst
, *src
;
1381 for (src
= &items
[1]; src
< &items
[items_count
]; src
++)
1382 if (strcmp (src
->fullform
, dst
->fullform
) != 0)
1384 items_count
= dst
+ 1 - items
;
1387 if (select_mode
== multiple_symbols_cancel
&& items_count
> 1)
1388 error (_("canceled because the command is ambiguous\n"
1389 "See set/show multiple-symbol."));
1391 if (select_mode
== multiple_symbols_all
|| items_count
== 1)
1393 do_cleanups (old_chain
);
1394 convert_results_to_lsals (self
, result
);
1398 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1399 for (i
= 0; i
< items_count
; i
++)
1400 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
);
1402 prompt
= getenv ("PS2");
1407 args
= command_line_input (prompt
, 0, "overload-choice");
1409 if (args
== 0 || *args
== 0)
1410 error_no_arg (_("one or more choice numbers"));
1412 init_number_or_range (&state
, args
);
1413 while (!state
.finished
)
1417 num
= get_number_or_range (&state
);
1420 error (_("canceled"));
1423 /* We intentionally make this result in a single breakpoint,
1424 contrary to what older versions of gdb did. The
1425 rationale is that this lets a user get the
1426 multiple_symbols_all behavior even with the 'ask'
1427 setting; and he can get separate breakpoints by entering
1428 "2-57" at the query. */
1429 do_cleanups (old_chain
);
1430 convert_results_to_lsals (self
, result
);
1435 if (num
>= items_count
)
1436 printf_unfiltered (_("No choice number %d.\n"), num
);
1439 struct decode_line_2_item
*item
= &items
[num
];
1441 if (!item
->selected
)
1443 VEC_safe_push (const_char_ptr
, filters
, item
->fullform
);
1448 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1454 filter_results (self
, result
, filters
);
1455 do_cleanups (old_chain
);
1460 /* The parser of linespec itself. */
1462 /* Throw an appropriate error when SYMBOL is not found (optionally in
1465 static void ATTRIBUTE_NORETURN
1466 symbol_not_found_error (const char *symbol
, const char *filename
)
1471 if (!have_full_symbols ()
1472 && !have_partial_symbols ()
1473 && !have_minimal_symbols ())
1474 throw_error (NOT_FOUND_ERROR
,
1475 _("No symbol table is loaded. Use the \"file\" command."));
1477 /* If SYMBOL starts with '$', the user attempted to either lookup
1478 a function/variable in his code starting with '$' or an internal
1479 variable of that name. Since we do not know which, be concise and
1480 explain both possibilities. */
1484 throw_error (NOT_FOUND_ERROR
,
1485 _("Undefined convenience variable or function \"%s\" "
1486 "not defined in \"%s\"."), symbol
, filename
);
1488 throw_error (NOT_FOUND_ERROR
,
1489 _("Undefined convenience variable or function \"%s\" "
1490 "not defined."), symbol
);
1495 throw_error (NOT_FOUND_ERROR
,
1496 _("Function \"%s\" not defined in \"%s\"."),
1499 throw_error (NOT_FOUND_ERROR
,
1500 _("Function \"%s\" not defined."), symbol
);
1504 /* Throw an appropriate error when an unexpected token is encountered
1507 static void ATTRIBUTE_NORETURN
1508 unexpected_linespec_error (linespec_parser
*parser
)
1510 linespec_token token
;
1511 static const char * token_type_strings
[]
1512 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1514 /* Get the token that generated the error. */
1515 token
= linespec_lexer_lex_one (parser
);
1517 /* Finally, throw the error. */
1518 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1519 || token
.type
== LSTOKEN_KEYWORD
)
1523 string
= copy_token_string (token
);
1524 make_cleanup (xfree
, string
);
1525 throw_error (GENERIC_ERROR
,
1526 _("malformed linespec error: unexpected %s, \"%s\""),
1527 token_type_strings
[token
.type
], string
);
1530 throw_error (GENERIC_ERROR
,
1531 _("malformed linespec error: unexpected %s"),
1532 token_type_strings
[token
.type
]);
1535 /* Throw an undefined label error. */
1537 static void ATTRIBUTE_NORETURN
1538 undefined_label_error (const char *function
, const char *label
)
1540 if (function
!= NULL
)
1541 throw_error (NOT_FOUND_ERROR
,
1542 _("No label \"%s\" defined in function \"%s\"."),
1545 throw_error (NOT_FOUND_ERROR
,
1546 _("No label \"%s\" defined in current function."),
1550 /* Throw a source file not found error. */
1552 static void ATTRIBUTE_NORETURN
1553 source_file_not_found_error (const char *name
)
1555 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), name
);
1558 /* See description in linespec.h. */
1561 linespec_parse_line_offset (const char *string
)
1563 const char *start
= string
;
1564 struct line_offset line_offset
= {0, LINE_OFFSET_NONE
};
1568 line_offset
.sign
= LINE_OFFSET_PLUS
;
1571 else if (*string
== '-')
1573 line_offset
.sign
= LINE_OFFSET_MINUS
;
1577 if (*string
!= '\0' && !isdigit (*string
))
1578 error (_("malformed line offset: \"%s\""), start
);
1580 /* Right now, we only allow base 10 for offsets. */
1581 line_offset
.offset
= atoi (string
);
1585 /* Parse the basic_spec in PARSER's input. */
1588 linespec_parse_basic (linespec_parser
*parser
)
1591 linespec_token token
;
1592 VEC (symbolp
) *symbols
, *labels
;
1593 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
1594 struct cleanup
*cleanup
;
1596 /* Get the next token. */
1597 token
= linespec_lexer_lex_one (parser
);
1599 /* If it is EOI or KEYWORD, issue an error. */
1600 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1601 unexpected_linespec_error (parser
);
1602 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1603 else if (token
.type
== LSTOKEN_NUMBER
)
1605 /* Record the line offset and get the next token. */
1606 name
= copy_token_string (token
);
1607 cleanup
= make_cleanup (xfree
, name
);
1608 PARSER_EXPLICIT (parser
)->line_offset
= linespec_parse_line_offset (name
);
1609 do_cleanups (cleanup
);
1611 /* Get the next token. */
1612 token
= linespec_lexer_consume_token (parser
);
1614 /* If the next token is a comma, stop parsing and return. */
1615 if (token
.type
== LSTOKEN_COMMA
)
1618 /* If the next token is anything but EOI or KEYWORD, issue
1620 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1621 unexpected_linespec_error (parser
);
1624 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1627 /* Next token must be LSTOKEN_STRING. */
1628 if (token
.type
!= LSTOKEN_STRING
)
1629 unexpected_linespec_error (parser
);
1631 /* The current token will contain the name of a function, method,
1633 name
= copy_token_string (token
);
1634 cleanup
= make_cleanup (xfree
, name
);
1636 /* Try looking it up as a function/method. */
1637 find_linespec_symbols (PARSER_STATE (parser
),
1638 PARSER_RESULT (parser
)->file_symtabs
, name
,
1639 &symbols
, &minimal_symbols
);
1641 if (symbols
!= NULL
|| minimal_symbols
!= NULL
)
1643 PARSER_RESULT (parser
)->function_symbols
= symbols
;
1644 PARSER_RESULT (parser
)->minimal_symbols
= minimal_symbols
;
1645 PARSER_EXPLICIT (parser
)->function_name
= name
;
1647 discard_cleanups (cleanup
);
1651 /* NAME was not a function or a method. So it must be a label
1652 name or user specified variable like "break foo.c:$zippo". */
1653 labels
= find_label_symbols (PARSER_STATE (parser
), NULL
,
1657 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1658 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1659 PARSER_EXPLICIT (parser
)->label_name
= name
;
1661 discard_cleanups (cleanup
);
1663 else if (token
.type
== LSTOKEN_STRING
1664 && *LS_TOKEN_STOKEN (token
).ptr
== '$')
1666 /* User specified a convenience variable or history value. */
1667 PARSER_EXPLICIT (parser
)->line_offset
1668 = linespec_parse_variable (PARSER_STATE (parser
), name
);
1670 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
)
1672 /* The user-specified variable was not valid. Do not
1673 throw an error here. parse_linespec will do it for us. */
1674 PARSER_EXPLICIT (parser
)->function_name
= name
;
1675 discard_cleanups (cleanup
);
1679 /* The convenience variable/history value parsed correctly.
1680 NAME is no longer needed. */
1681 do_cleanups (cleanup
);
1685 /* The name is also not a label. Abort parsing. Do not throw
1686 an error here. parse_linespec will do it for us. */
1688 /* Save a copy of the name we were trying to lookup. */
1689 PARSER_EXPLICIT (parser
)->function_name
= name
;
1690 discard_cleanups (cleanup
);
1695 /* Get the next token. */
1696 token
= linespec_lexer_consume_token (parser
);
1698 if (token
.type
== LSTOKEN_COLON
)
1700 /* User specified a label or a lineno. */
1701 token
= linespec_lexer_consume_token (parser
);
1703 if (token
.type
== LSTOKEN_NUMBER
)
1705 /* User specified an offset. Record the line offset and
1706 get the next token. */
1707 name
= copy_token_string (token
);
1708 cleanup
= make_cleanup (xfree
, name
);
1709 PARSER_EXPLICIT (parser
)->line_offset
1710 = linespec_parse_line_offset (name
);
1711 do_cleanups (cleanup
);
1713 /* Ge the next token. */
1714 token
= linespec_lexer_consume_token (parser
);
1716 else if (token
.type
== LSTOKEN_STRING
)
1718 /* Grab a copy of the label's name and look it up. */
1719 name
= copy_token_string (token
);
1720 cleanup
= make_cleanup (xfree
, name
);
1721 labels
= find_label_symbols (PARSER_STATE (parser
),
1722 PARSER_RESULT (parser
)->function_symbols
,
1727 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1728 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1729 PARSER_EXPLICIT (parser
)->label_name
= name
;
1731 discard_cleanups (cleanup
);
1735 /* We don't know what it was, but it isn't a label. */
1736 undefined_label_error (PARSER_EXPLICIT (parser
)->function_name
,
1740 /* Check for a line offset. */
1741 token
= linespec_lexer_consume_token (parser
);
1742 if (token
.type
== LSTOKEN_COLON
)
1744 /* Get the next token. */
1745 token
= linespec_lexer_consume_token (parser
);
1747 /* It must be a line offset. */
1748 if (token
.type
!= LSTOKEN_NUMBER
)
1749 unexpected_linespec_error (parser
);
1751 /* Record the lione offset and get the next token. */
1752 name
= copy_token_string (token
);
1753 cleanup
= make_cleanup (xfree
, name
);
1755 PARSER_EXPLICIT (parser
)->line_offset
1756 = linespec_parse_line_offset (name
);
1757 do_cleanups (cleanup
);
1759 /* Get the next token. */
1760 token
= linespec_lexer_consume_token (parser
);
1765 /* Trailing ':' in the input. Issue an error. */
1766 unexpected_linespec_error (parser
);
1771 /* Canonicalize the linespec contained in LS. The result is saved into
1772 STATE->canonical. This function handles both linespec and explicit
1776 canonicalize_linespec (struct linespec_state
*state
, const linespec_p ls
)
1778 struct event_location
*canon
;
1779 struct explicit_location
*explicit_loc
;
1781 /* If canonicalization was not requested, no need to do anything. */
1782 if (!state
->canonical
)
1785 /* Save everything as an explicit location. */
1786 canon
= state
->canonical
->location
1787 = new_explicit_location (&ls
->explicit_loc
);
1788 explicit_loc
= get_explicit_location (canon
);
1790 if (explicit_loc
->label_name
!= NULL
)
1792 state
->canonical
->special_display
= 1;
1794 if (explicit_loc
->function_name
== NULL
)
1798 /* No function was specified, so add the symbol name. */
1799 gdb_assert (ls
->labels
.function_symbols
!= NULL
1800 && (VEC_length (symbolp
, ls
->labels
.function_symbols
)
1802 s
= VEC_index (symbolp
, ls
->labels
.function_symbols
, 0);
1803 explicit_loc
->function_name
= xstrdup (SYMBOL_NATURAL_NAME (s
));
1807 /* If this location originally came from a linespec, save a string
1808 representation of it for display and saving to file. */
1809 if (state
->is_linespec
)
1811 char *linespec
= explicit_location_to_linespec (explicit_loc
);
1813 set_event_location_string (canon
, linespec
);
1818 /* Given a line offset in LS, construct the relevant SALs. */
1820 static struct symtabs_and_lines
1821 create_sals_line_offset (struct linespec_state
*self
,
1824 struct symtabs_and_lines values
;
1825 struct symtab_and_line val
;
1826 int use_default
= 0;
1832 /* This is where we need to make sure we have good defaults.
1833 We must guarantee that this section of code is never executed
1834 when we are called with just a function name, since
1835 set_default_source_symtab_and_line uses
1836 select_source_symtab that calls us with such an argument. */
1838 if (VEC_length (symtab_ptr
, ls
->file_symtabs
) == 1
1839 && VEC_index (symtab_ptr
, ls
->file_symtabs
, 0) == NULL
)
1841 const char *fullname
;
1843 set_current_program_space (self
->program_space
);
1845 /* Make sure we have at least a default source line. */
1846 set_default_source_symtab_and_line ();
1847 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
1848 fullname
= symtab_to_fullname (self
->default_symtab
);
1849 VEC_pop (symtab_ptr
, ls
->file_symtabs
);
1850 VEC_free (symtab_ptr
, ls
->file_symtabs
);
1851 ls
->file_symtabs
= collect_symtabs_from_filename (fullname
,
1852 self
->search_pspace
);
1856 val
.line
= ls
->explicit_loc
.line_offset
.offset
;
1857 switch (ls
->explicit_loc
.line_offset
.sign
)
1859 case LINE_OFFSET_PLUS
:
1860 if (ls
->explicit_loc
.line_offset
.offset
== 0)
1863 val
.line
= self
->default_line
+ val
.line
;
1866 case LINE_OFFSET_MINUS
:
1867 if (ls
->explicit_loc
.line_offset
.offset
== 0)
1870 val
.line
= self
->default_line
- val
.line
;
1872 val
.line
= -val
.line
;
1875 case LINE_OFFSET_NONE
:
1876 break; /* No need to adjust val.line. */
1879 if (self
->list_mode
)
1880 decode_digits_list_mode (self
, ls
, &values
, val
);
1883 struct linetable_entry
*best_entry
= NULL
;
1885 const struct block
**blocks
;
1886 struct cleanup
*cleanup
;
1887 struct symtabs_and_lines intermediate_results
;
1890 intermediate_results
.sals
= NULL
;
1891 intermediate_results
.nelts
= 0;
1893 decode_digits_ordinary (self
, ls
, val
.line
, &intermediate_results
,
1895 if (intermediate_results
.nelts
== 0 && best_entry
!= NULL
)
1896 decode_digits_ordinary (self
, ls
, best_entry
->line
,
1897 &intermediate_results
, &best_entry
);
1899 cleanup
= make_cleanup (xfree
, intermediate_results
.sals
);
1901 /* For optimized code, the compiler can scatter one source line
1902 across disjoint ranges of PC values, even when no duplicate
1903 functions or inline functions are involved. For example,
1904 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
1905 function can result in two PC ranges. In this case, we don't
1906 want to set a breakpoint on the first PC of each range. To filter
1907 such cases, we use containing blocks -- for each PC found
1908 above, we see if there are other PCs that are in the same
1909 block. If yes, the other PCs are filtered out. */
1911 filter
= XNEWVEC (int, intermediate_results
.nelts
);
1912 make_cleanup (xfree
, filter
);
1913 blocks
= XNEWVEC (const struct block
*, intermediate_results
.nelts
);
1914 make_cleanup (xfree
, blocks
);
1916 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1918 set_current_program_space (intermediate_results
.sals
[i
].pspace
);
1921 blocks
[i
] = block_for_pc_sect (intermediate_results
.sals
[i
].pc
,
1922 intermediate_results
.sals
[i
].section
);
1925 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1927 if (blocks
[i
] != NULL
)
1928 for (j
= i
+ 1; j
< intermediate_results
.nelts
; ++j
)
1930 if (blocks
[j
] == blocks
[i
])
1938 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1941 struct symbol
*sym
= (blocks
[i
]
1942 ? block_containing_function (blocks
[i
])
1945 if (self
->funfirstline
)
1946 skip_prologue_sal (&intermediate_results
.sals
[i
]);
1947 /* Make sure the line matches the request, not what was
1949 intermediate_results
.sals
[i
].line
= val
.line
;
1950 add_sal_to_sals (self
, &values
, &intermediate_results
.sals
[i
],
1951 sym
? SYMBOL_NATURAL_NAME (sym
) : NULL
, 0);
1954 do_cleanups (cleanup
);
1957 if (values
.nelts
== 0)
1959 if (ls
->explicit_loc
.source_filename
)
1960 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
1961 val
.line
, ls
->explicit_loc
.source_filename
);
1963 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
1970 /* Convert the given ADDRESS into SaLs. */
1972 static struct symtabs_and_lines
1973 convert_address_location_to_sals (struct linespec_state
*self
,
1976 struct symtab_and_line sal
;
1977 struct symtabs_and_lines sals
= {NULL
, 0};
1979 sal
= find_pc_line (address
, 0);
1981 sal
.section
= find_pc_overlay (address
);
1982 sal
.explicit_pc
= 1;
1983 add_sal_to_sals (self
, &sals
, &sal
, core_addr_to_string (address
), 1);
1988 /* Create and return SALs from the linespec LS. */
1990 static struct symtabs_and_lines
1991 convert_linespec_to_sals (struct linespec_state
*state
, linespec_p ls
)
1993 struct symtabs_and_lines sals
= {NULL
, 0};
1995 if (ls
->labels
.label_symbols
!= NULL
)
1997 /* We have just a bunch of functions/methods or labels. */
1999 struct symtab_and_line sal
;
2002 for (i
= 0; VEC_iterate (symbolp
, ls
->labels
.label_symbols
, i
, sym
); ++i
)
2004 struct program_space
*pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2006 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
2007 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2008 add_sal_to_sals (state
, &sals
, &sal
,
2009 SYMBOL_NATURAL_NAME (sym
), 0);
2012 else if (ls
->function_symbols
!= NULL
|| ls
->minimal_symbols
!= NULL
)
2014 /* We have just a bunch of functions and/or methods. */
2016 struct symtab_and_line sal
;
2018 bound_minimal_symbol_d
*elem
;
2019 struct program_space
*pspace
;
2021 if (ls
->function_symbols
!= NULL
)
2023 /* Sort symbols so that symbols with the same program space are next
2025 qsort (VEC_address (symbolp
, ls
->function_symbols
),
2026 VEC_length (symbolp
, ls
->function_symbols
),
2027 sizeof (symbolp
), compare_symbols
);
2029 for (i
= 0; VEC_iterate (symbolp
, ls
->function_symbols
, i
, sym
); ++i
)
2031 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2032 set_current_program_space (pspace
);
2033 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
2034 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2035 add_sal_to_sals (state
, &sals
, &sal
,
2036 SYMBOL_NATURAL_NAME (sym
), 0);
2040 if (ls
->minimal_symbols
!= NULL
)
2042 /* Sort minimal symbols by program space, too. */
2043 qsort (VEC_address (bound_minimal_symbol_d
, ls
->minimal_symbols
),
2044 VEC_length (bound_minimal_symbol_d
, ls
->minimal_symbols
),
2045 sizeof (bound_minimal_symbol_d
), compare_msymbols
);
2048 VEC_iterate (bound_minimal_symbol_d
, ls
->minimal_symbols
,
2052 pspace
= elem
->objfile
->pspace
;
2053 set_current_program_space (pspace
);
2054 minsym_found (state
, elem
->objfile
, elem
->minsym
, &sals
);
2058 else if (ls
->explicit_loc
.line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2060 /* Only an offset was specified. */
2061 sals
= create_sals_line_offset (state
, ls
);
2063 /* Make sure we have a filename for canonicalization. */
2064 if (ls
->explicit_loc
.source_filename
== NULL
)
2066 const char *fullname
= symtab_to_fullname (state
->default_symtab
);
2068 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2069 form so that displaying SOURCE_FILENAME can follow the current
2070 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2071 it has been kept for code simplicity only in absolute form. */
2072 ls
->explicit_loc
.source_filename
= xstrdup (fullname
);
2077 /* We haven't found any results... */
2081 canonicalize_linespec (state
, ls
);
2083 if (sals
.nelts
> 0 && state
->canonical
!= NULL
)
2084 state
->canonical
->pre_expanded
= 1;
2089 /* Convert the explicit location EXPLICIT_LOC into SaLs. */
2091 static struct symtabs_and_lines
2092 convert_explicit_location_to_sals (struct linespec_state
*self
,
2094 const struct explicit_location
*explicit_loc
)
2096 VEC (symbolp
) *symbols
, *labels
;
2097 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
2099 if (explicit_loc
->source_filename
!= NULL
)
2103 result
->file_symtabs
2104 = symtabs_from_filename (explicit_loc
->source_filename
,
2105 self
->search_pspace
);
2107 CATCH (except
, RETURN_MASK_ERROR
)
2109 source_file_not_found_error (explicit_loc
->source_filename
);
2112 result
->explicit_loc
.source_filename
2113 = xstrdup (explicit_loc
->source_filename
);
2117 /* A NULL entry means to use the default symtab. */
2118 VEC_safe_push (symtab_ptr
, result
->file_symtabs
, NULL
);
2121 if (explicit_loc
->function_name
!= NULL
)
2123 find_linespec_symbols (self
, result
->file_symtabs
,
2124 explicit_loc
->function_name
, &symbols
,
2127 if (symbols
== NULL
&& minimal_symbols
== NULL
)
2128 symbol_not_found_error (explicit_loc
->function_name
,
2129 result
->explicit_loc
.source_filename
);
2131 result
->explicit_loc
.function_name
2132 = xstrdup (explicit_loc
->function_name
);
2133 result
->function_symbols
= symbols
;
2134 result
->minimal_symbols
= minimal_symbols
;
2137 if (explicit_loc
->label_name
!= NULL
)
2140 labels
= find_label_symbols (self
, result
->function_symbols
,
2141 &symbols
, explicit_loc
->label_name
);
2144 undefined_label_error (result
->explicit_loc
.function_name
,
2145 explicit_loc
->label_name
);
2147 result
->explicit_loc
.label_name
= xstrdup (explicit_loc
->label_name
);
2148 result
->labels
.label_symbols
= labels
;
2149 result
->labels
.function_symbols
= symbols
;
2152 if (explicit_loc
->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2153 result
->explicit_loc
.line_offset
= explicit_loc
->line_offset
;
2155 return convert_linespec_to_sals (self
, result
);
2158 /* Parse a string that specifies a linespec.
2160 The basic grammar of linespecs:
2162 linespec -> var_spec | basic_spec
2163 var_spec -> '$' (STRING | NUMBER)
2165 basic_spec -> file_offset_spec | function_spec | label_spec
2166 file_offset_spec -> opt_file_spec offset_spec
2167 function_spec -> opt_file_spec function_name_spec opt_label_spec
2168 label_spec -> label_name_spec
2170 opt_file_spec -> "" | file_name_spec ':'
2171 opt_label_spec -> "" | ':' label_name_spec
2173 file_name_spec -> STRING
2174 function_name_spec -> STRING
2175 label_name_spec -> STRING
2176 function_name_spec -> STRING
2177 offset_spec -> NUMBER
2181 This may all be followed by several keywords such as "if EXPR",
2184 A comma will terminate parsing.
2186 The function may be an undebuggable function found in minimal symbol table.
2188 If the argument FUNFIRSTLINE is nonzero, we want the first line
2189 of real code inside a function when a function is specified, and it is
2190 not OK to specify a variable or type to get its line number.
2192 DEFAULT_SYMTAB specifies the file to use if none is specified.
2193 It defaults to current_source_symtab.
2194 DEFAULT_LINE specifies the line number to use for relative
2195 line numbers (that start with signs). Defaults to current_source_line.
2196 If CANONICAL is non-NULL, store an array of strings containing the canonical
2197 line specs there if necessary. Currently overloaded member functions and
2198 line numbers or static functions without a filename yield a canonical
2199 line spec. The array and the line spec strings are allocated on the heap,
2200 it is the callers responsibility to free them.
2202 Note that it is possible to return zero for the symtab
2203 if no file is validly specified. Callers must check that.
2204 Also, the line number returned may be invalid. */
2206 /* Parse the linespec in ARG. */
2208 static struct symtabs_and_lines
2209 parse_linespec (linespec_parser
*parser
, const char *arg
)
2211 linespec_token token
;
2212 struct symtabs_and_lines values
;
2213 struct gdb_exception file_exception
= exception_none
;
2214 struct cleanup
*cleanup
;
2216 /* A special case to start. It has become quite popular for
2217 IDEs to work around bugs in the previous parser by quoting
2218 the entire linespec, so we attempt to deal with this nicely. */
2219 parser
->is_quote_enclosed
= 0;
2220 if (!is_ada_operator (arg
)
2221 && strchr (linespec_quote_characters
, *arg
) != NULL
)
2225 end
= skip_quote_char (arg
+ 1, *arg
);
2226 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2228 /* Here's the special case. Skip ARG past the initial
2231 parser
->is_quote_enclosed
= 1;
2235 parser
->lexer
.saved_arg
= arg
;
2236 parser
->lexer
.stream
= arg
;
2238 /* Initialize the default symtab and line offset. */
2239 initialize_defaults (&PARSER_STATE (parser
)->default_symtab
,
2240 &PARSER_STATE (parser
)->default_line
);
2242 /* Objective-C shortcut. */
2243 values
= decode_objc (PARSER_STATE (parser
), PARSER_RESULT (parser
), arg
);
2244 if (values
.sals
!= NULL
)
2247 /* Start parsing. */
2249 /* Get the first token. */
2250 token
= linespec_lexer_lex_one (parser
);
2252 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2253 if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '$')
2257 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2258 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2260 /* User specified a convenience variable or history value. */
2261 var
= copy_token_string (token
);
2262 cleanup
= make_cleanup (xfree
, var
);
2263 PARSER_EXPLICIT (parser
)->line_offset
2264 = linespec_parse_variable (PARSER_STATE (parser
), var
);
2265 do_cleanups (cleanup
);
2267 /* If a line_offset wasn't found (VAR is the name of a user
2268 variable/function), then skip to normal symbol processing. */
2269 if (PARSER_EXPLICIT (parser
)->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2271 /* Consume this token. */
2272 linespec_lexer_consume_token (parser
);
2274 goto convert_to_sals
;
2277 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2278 unexpected_linespec_error (parser
);
2280 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2281 this token cannot represent a filename. */
2282 token
= linespec_lexer_peek_token (parser
);
2284 if (token
.type
== LSTOKEN_COLON
)
2286 char *user_filename
;
2288 /* Get the current token again and extract the filename. */
2289 token
= linespec_lexer_lex_one (parser
);
2290 user_filename
= copy_token_string (token
);
2292 /* Check if the input is a filename. */
2295 PARSER_RESULT (parser
)->file_symtabs
2296 = symtabs_from_filename (user_filename
,
2297 PARSER_STATE (parser
)->search_pspace
);
2299 CATCH (ex
, RETURN_MASK_ERROR
)
2301 file_exception
= ex
;
2305 if (file_exception
.reason
>= 0)
2307 /* Symtabs were found for the file. Record the filename. */
2308 PARSER_EXPLICIT (parser
)->source_filename
= user_filename
;
2310 /* Get the next token. */
2311 token
= linespec_lexer_consume_token (parser
);
2313 /* This is LSTOKEN_COLON; consume it. */
2314 linespec_lexer_consume_token (parser
);
2318 /* No symtabs found -- discard user_filename. */
2319 xfree (user_filename
);
2321 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2322 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2325 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2326 else if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2327 && token
.type
!= LSTOKEN_COMMA
)
2329 /* TOKEN is the _next_ token, not the one currently in the parser.
2330 Consuming the token will give the correct error message. */
2331 linespec_lexer_consume_token (parser
);
2332 unexpected_linespec_error (parser
);
2336 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2337 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2340 /* Parse the rest of the linespec. */
2341 linespec_parse_basic (parser
);
2343 if (PARSER_RESULT (parser
)->function_symbols
== NULL
2344 && PARSER_RESULT (parser
)->labels
.label_symbols
== NULL
2345 && PARSER_EXPLICIT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
2346 && PARSER_RESULT (parser
)->minimal_symbols
== NULL
)
2348 /* The linespec didn't parse. Re-throw the file exception if
2350 if (file_exception
.reason
< 0)
2351 throw_exception (file_exception
);
2353 /* Otherwise, the symbol is not found. */
2354 symbol_not_found_error (PARSER_EXPLICIT (parser
)->function_name
,
2355 PARSER_EXPLICIT (parser
)->source_filename
);
2360 /* Get the last token and record how much of the input was parsed,
2362 token
= linespec_lexer_lex_one (parser
);
2363 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2364 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (token
).ptr
;
2366 /* Convert the data in PARSER_RESULT to SALs. */
2367 values
= convert_linespec_to_sals (PARSER_STATE (parser
),
2368 PARSER_RESULT (parser
));
2374 /* A constructor for linespec_state. */
2377 linespec_state_constructor (struct linespec_state
*self
,
2378 int flags
, const struct language_defn
*language
,
2379 struct program_space
*search_pspace
,
2380 struct symtab
*default_symtab
,
2382 struct linespec_result
*canonical
)
2384 memset (self
, 0, sizeof (*self
));
2385 self
->language
= language
;
2386 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2387 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2388 self
->search_pspace
= search_pspace
;
2389 self
->default_symtab
= default_symtab
;
2390 self
->default_line
= default_line
;
2391 self
->canonical
= canonical
;
2392 self
->program_space
= current_program_space
;
2393 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2394 xfree
, xcalloc
, xfree
);
2395 self
->is_linespec
= 0;
2398 /* Initialize a new linespec parser. */
2401 linespec_parser_new (linespec_parser
*parser
,
2402 int flags
, const struct language_defn
*language
,
2403 struct program_space
*search_pspace
,
2404 struct symtab
*default_symtab
,
2406 struct linespec_result
*canonical
)
2408 memset (parser
, 0, sizeof (linespec_parser
));
2409 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
2410 memset (PARSER_RESULT (parser
), 0, sizeof (struct linespec
));
2411 PARSER_EXPLICIT (parser
)->line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2412 linespec_state_constructor (PARSER_STATE (parser
), flags
, language
,
2414 default_symtab
, default_line
, canonical
);
2417 /* A destructor for linespec_state. */
2420 linespec_state_destructor (struct linespec_state
*self
)
2422 htab_delete (self
->addr_set
);
2425 /* Delete a linespec parser. */
2428 linespec_parser_delete (void *arg
)
2430 linespec_parser
*parser
= (linespec_parser
*) arg
;
2432 xfree (PARSER_EXPLICIT (parser
)->source_filename
);
2433 xfree (PARSER_EXPLICIT (parser
)->label_name
);
2434 xfree (PARSER_EXPLICIT (parser
)->function_name
);
2436 if (PARSER_RESULT (parser
)->file_symtabs
!= NULL
)
2437 VEC_free (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
);
2439 if (PARSER_RESULT (parser
)->function_symbols
!= NULL
)
2440 VEC_free (symbolp
, PARSER_RESULT (parser
)->function_symbols
);
2442 if (PARSER_RESULT (parser
)->minimal_symbols
!= NULL
)
2443 VEC_free (bound_minimal_symbol_d
, PARSER_RESULT (parser
)->minimal_symbols
);
2445 if (PARSER_RESULT (parser
)->labels
.label_symbols
!= NULL
)
2446 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.label_symbols
);
2448 if (PARSER_RESULT (parser
)->labels
.function_symbols
!= NULL
)
2449 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.function_symbols
);
2451 linespec_state_destructor (PARSER_STATE (parser
));
2454 /* See description in linespec.h. */
2457 linespec_lex_to_end (char **stringp
)
2459 linespec_parser parser
;
2460 struct cleanup
*cleanup
;
2461 linespec_token token
;
2464 if (stringp
== NULL
|| *stringp
== NULL
)
2467 linespec_parser_new (&parser
, 0, current_language
, NULL
, NULL
, 0, NULL
);
2468 cleanup
= make_cleanup (linespec_parser_delete
, &parser
);
2469 parser
.lexer
.saved_arg
= *stringp
;
2470 PARSER_STREAM (&parser
) = orig
= *stringp
;
2474 /* Stop before any comma tokens; we need it to keep it
2475 as the next token in the string. */
2476 token
= linespec_lexer_peek_token (&parser
);
2477 if (token
.type
== LSTOKEN_COMMA
)
2479 token
= linespec_lexer_consume_token (&parser
);
2481 while (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
);
2483 *stringp
+= PARSER_STREAM (&parser
) - orig
;
2484 do_cleanups (cleanup
);
2487 /* A helper function for decode_line_full and decode_line_1 to
2488 turn LOCATION into symtabs_and_lines. */
2490 static struct symtabs_and_lines
2491 event_location_to_sals (linespec_parser
*parser
,
2492 const struct event_location
*location
)
2494 struct symtabs_and_lines result
= {NULL
, 0};
2496 switch (event_location_type (location
))
2498 case LINESPEC_LOCATION
:
2500 PARSER_STATE (parser
)->is_linespec
= 1;
2503 result
= parse_linespec (parser
, get_linespec_location (location
));
2505 CATCH (except
, RETURN_MASK_ERROR
)
2507 throw_exception (except
);
2513 case ADDRESS_LOCATION
:
2515 const char *addr_string
= get_address_string_location (location
);
2516 CORE_ADDR addr
= get_address_location (location
);
2518 if (addr_string
!= NULL
)
2520 char *expr
= xstrdup (addr_string
);
2521 const char *const_expr
= expr
;
2522 struct cleanup
*cleanup
= make_cleanup (xfree
, expr
);
2524 addr
= linespec_expression_to_pc (&const_expr
);
2525 if (PARSER_STATE (parser
)->canonical
!= NULL
)
2526 PARSER_STATE (parser
)->canonical
->location
2527 = copy_event_location (location
);
2529 do_cleanups (cleanup
);
2532 result
= convert_address_location_to_sals (PARSER_STATE (parser
),
2537 case EXPLICIT_LOCATION
:
2539 const struct explicit_location
*explicit_loc
;
2541 explicit_loc
= get_explicit_location_const (location
);
2542 result
= convert_explicit_location_to_sals (PARSER_STATE (parser
),
2543 PARSER_RESULT (parser
),
2548 case PROBE_LOCATION
:
2549 /* Probes are handled by their own decoders. */
2550 gdb_assert_not_reached ("attempt to decode probe location");
2554 gdb_assert_not_reached ("unhandled event location type");
2560 /* See linespec.h. */
2563 decode_line_full (const struct event_location
*location
, int flags
,
2564 struct program_space
*search_pspace
,
2565 struct symtab
*default_symtab
,
2566 int default_line
, struct linespec_result
*canonical
,
2567 const char *select_mode
,
2570 struct symtabs_and_lines result
;
2571 struct cleanup
*cleanups
;
2572 VEC (const_char_ptr
) *filters
= NULL
;
2573 linespec_parser parser
;
2574 struct linespec_state
*state
;
2576 gdb_assert (canonical
!= NULL
);
2577 /* The filter only makes sense for 'all'. */
2578 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
2579 gdb_assert (select_mode
== NULL
2580 || select_mode
== multiple_symbols_all
2581 || select_mode
== multiple_symbols_ask
2582 || select_mode
== multiple_symbols_cancel
);
2583 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
2585 linespec_parser_new (&parser
, flags
, current_language
,
2586 search_pspace
, default_symtab
,
2587 default_line
, canonical
);
2588 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
2589 save_current_program_space ();
2591 result
= event_location_to_sals (&parser
, location
);
2592 state
= PARSER_STATE (&parser
);
2594 gdb_assert (result
.nelts
== 1 || canonical
->pre_expanded
);
2595 canonical
->pre_expanded
= 1;
2597 /* Arrange for allocated canonical names to be freed. */
2598 if (result
.nelts
> 0)
2602 make_cleanup (xfree
, state
->canonical_names
);
2603 for (i
= 0; i
< result
.nelts
; ++i
)
2605 gdb_assert (state
->canonical_names
[i
].suffix
!= NULL
);
2606 make_cleanup (xfree
, state
->canonical_names
[i
].suffix
);
2610 if (select_mode
== NULL
)
2612 if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
2613 select_mode
= multiple_symbols_all
;
2615 select_mode
= multiple_symbols_select_mode ();
2618 if (select_mode
== multiple_symbols_all
)
2622 make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
2623 VEC_safe_push (const_char_ptr
, filters
, filter
);
2624 filter_results (state
, &result
, filters
);
2627 convert_results_to_lsals (state
, &result
);
2630 decode_line_2 (state
, &result
, select_mode
);
2632 do_cleanups (cleanups
);
2635 /* See linespec.h. */
2637 struct symtabs_and_lines
2638 decode_line_1 (const struct event_location
*location
, int flags
,
2639 struct program_space
*search_pspace
,
2640 struct symtab
*default_symtab
,
2643 struct symtabs_and_lines result
;
2644 linespec_parser parser
;
2645 struct cleanup
*cleanups
;
2647 linespec_parser_new (&parser
, flags
, current_language
,
2648 search_pspace
, default_symtab
,
2649 default_line
, NULL
);
2650 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
2651 save_current_program_space ();
2653 result
= event_location_to_sals (&parser
, location
);
2655 do_cleanups (cleanups
);
2659 /* See linespec.h. */
2661 struct symtabs_and_lines
2662 decode_line_with_current_source (char *string
, int flags
)
2664 struct symtabs_and_lines sals
;
2665 struct symtab_and_line cursal
;
2666 struct event_location
*location
;
2667 struct cleanup
*cleanup
;
2670 error (_("Empty line specification."));
2672 /* We use whatever is set as the current source line. We do not try
2673 and get a default source symtab+line or it will recursively call us! */
2674 cursal
= get_current_source_symtab_and_line ();
2676 location
= string_to_event_location (&string
, current_language
);
2677 cleanup
= make_cleanup_delete_event_location (location
);
2678 sals
= decode_line_1 (location
, flags
, NULL
,
2679 cursal
.symtab
, cursal
.line
);
2682 error (_("Junk at end of line specification: %s"), string
);
2684 do_cleanups (cleanup
);
2688 /* See linespec.h. */
2690 struct symtabs_and_lines
2691 decode_line_with_last_displayed (char *string
, int flags
)
2693 struct symtabs_and_lines sals
;
2694 struct event_location
*location
;
2695 struct cleanup
*cleanup
;
2698 error (_("Empty line specification."));
2700 location
= string_to_event_location (&string
, current_language
);
2701 cleanup
= make_cleanup_delete_event_location (location
);
2702 if (last_displayed_sal_is_valid ())
2703 sals
= decode_line_1 (location
, flags
, NULL
,
2704 get_last_displayed_symtab (),
2705 get_last_displayed_line ());
2707 sals
= decode_line_1 (location
, flags
, NULL
, (struct symtab
*) NULL
, 0);
2710 error (_("Junk at end of line specification: %s"), string
);
2712 do_cleanups (cleanup
);
2718 /* First, some functions to initialize stuff at the beggining of the
2722 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
2724 if (*default_symtab
== 0)
2726 /* Use whatever we have for the default source line. We don't use
2727 get_current_or_default_symtab_and_line as it can recurse and call
2729 struct symtab_and_line cursal
=
2730 get_current_source_symtab_and_line ();
2732 *default_symtab
= cursal
.symtab
;
2733 *default_line
= cursal
.line
;
2739 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2740 advancing EXP_PTR past any parsed text. */
2743 linespec_expression_to_pc (const char **exp_ptr
)
2745 if (current_program_space
->executing_startup
)
2746 /* The error message doesn't really matter, because this case
2747 should only hit during breakpoint reset. */
2748 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
2749 "program space is in startup"));
2752 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
2757 /* Here's where we recognise an Objective-C Selector. An Objective C
2758 selector may be implemented by more than one class, therefore it
2759 may represent more than one method/function. This gives us a
2760 situation somewhat analogous to C++ overloading. If there's more
2761 than one method that could represent the selector, then use some of
2762 the existing C++ code to let the user choose one. */
2764 static struct symtabs_and_lines
2765 decode_objc (struct linespec_state
*self
, linespec_p ls
, const char *arg
)
2767 struct collect_info info
;
2768 VEC (const_char_ptr
) *symbol_names
= NULL
;
2769 struct symtabs_and_lines values
;
2770 const char *new_argptr
;
2771 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
2775 info
.file_symtabs
= NULL
;
2776 VEC_safe_push (symtab_ptr
, info
.file_symtabs
, NULL
);
2777 make_cleanup (VEC_cleanup (symtab_ptr
), &info
.file_symtabs
);
2778 info
.result
.symbols
= NULL
;
2779 info
.result
.minimal_symbols
= NULL
;
2783 new_argptr
= find_imps (arg
, &symbol_names
);
2784 if (VEC_empty (const_char_ptr
, symbol_names
))
2786 do_cleanups (cleanup
);
2790 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
);
2792 if (!VEC_empty (symbolp
, info
.result
.symbols
)
2793 || !VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
2797 saved_arg
= (char *) alloca (new_argptr
- arg
+ 1);
2798 memcpy (saved_arg
, arg
, new_argptr
- arg
);
2799 saved_arg
[new_argptr
- arg
] = '\0';
2801 ls
->explicit_loc
.function_name
= xstrdup (saved_arg
);
2802 ls
->function_symbols
= info
.result
.symbols
;
2803 ls
->minimal_symbols
= info
.result
.minimal_symbols
;
2804 values
= convert_linespec_to_sals (self
, ls
);
2806 if (self
->canonical
)
2810 self
->canonical
->pre_expanded
= 1;
2812 if (ls
->explicit_loc
.source_filename
)
2814 str
= xstrprintf ("%s:%s",
2815 ls
->explicit_loc
.source_filename
, saved_arg
);
2818 str
= xstrdup (saved_arg
);
2820 make_cleanup (xfree
, str
);
2821 self
->canonical
->location
= new_linespec_location (&str
);
2825 do_cleanups (cleanup
);
2830 /* An instance of this type is used when collecting prefix symbols for
2833 struct decode_compound_collector
2835 /* The result vector. */
2836 VEC (symbolp
) *symbols
;
2838 /* A hash table of all symbols we found. We use this to avoid
2839 adding any symbol more than once. */
2843 /* A callback for iterate_over_symbols that is used by
2844 lookup_prefix_sym to collect type symbols. */
2847 collect_one_symbol (struct symbol
*sym
, void *d
)
2849 struct decode_compound_collector
*collector
2850 = (struct decode_compound_collector
*) d
;
2854 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
2855 return 1; /* Continue iterating. */
2857 t
= SYMBOL_TYPE (sym
);
2858 t
= check_typedef (t
);
2859 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2860 && TYPE_CODE (t
) != TYPE_CODE_UNION
2861 && TYPE_CODE (t
) != TYPE_CODE_NAMESPACE
)
2862 return 1; /* Continue iterating. */
2864 slot
= htab_find_slot (collector
->unique_syms
, sym
, INSERT
);
2868 VEC_safe_push (symbolp
, collector
->symbols
, sym
);
2871 return 1; /* Continue iterating. */
2874 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
2876 static VEC (symbolp
) *
2877 lookup_prefix_sym (struct linespec_state
*state
, VEC (symtab_ptr
) *file_symtabs
,
2878 const char *class_name
)
2882 struct decode_compound_collector collector
;
2883 struct cleanup
*outer
;
2884 struct cleanup
*cleanup
;
2886 collector
.symbols
= NULL
;
2887 outer
= make_cleanup (VEC_cleanup (symbolp
), &collector
.symbols
);
2889 collector
.unique_syms
= htab_create_alloc (1, htab_hash_pointer
,
2890 htab_eq_pointer
, NULL
,
2892 cleanup
= make_cleanup_htab_delete (collector
.unique_syms
);
2894 for (ix
= 0; VEC_iterate (symtab_ptr
, file_symtabs
, ix
, elt
); ++ix
)
2898 iterate_over_all_matching_symtabs (state
, class_name
, STRUCT_DOMAIN
,
2899 collect_one_symbol
, &collector
,
2901 iterate_over_all_matching_symtabs (state
, class_name
, VAR_DOMAIN
,
2902 collect_one_symbol
, &collector
,
2907 /* Program spaces that are executing startup should have
2908 been filtered out earlier. */
2909 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
2910 set_current_program_space (SYMTAB_PSPACE (elt
));
2911 iterate_over_file_blocks (elt
, class_name
, STRUCT_DOMAIN
,
2912 collect_one_symbol
, &collector
);
2913 iterate_over_file_blocks (elt
, class_name
, VAR_DOMAIN
,
2914 collect_one_symbol
, &collector
);
2918 do_cleanups (cleanup
);
2919 discard_cleanups (outer
);
2920 return collector
.symbols
;
2923 /* A qsort comparison function for symbols. The resulting order does
2924 not actually matter; we just need to be able to sort them so that
2925 symbols with the same program space end up next to each other. */
2928 compare_symbols (const void *a
, const void *b
)
2930 struct symbol
* const *sa
= (struct symbol
* const*) a
;
2931 struct symbol
* const *sb
= (struct symbol
* const*) b
;
2934 uia
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa
));
2935 uib
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb
));
2942 uia
= (uintptr_t) *sa
;
2943 uib
= (uintptr_t) *sb
;
2953 /* Like compare_symbols but for minimal symbols. */
2956 compare_msymbols (const void *a
, const void *b
)
2958 const struct bound_minimal_symbol
*sa
2959 = (const struct bound_minimal_symbol
*) a
;
2960 const struct bound_minimal_symbol
*sb
2961 = (const struct bound_minimal_symbol
*) b
;
2964 uia
= (uintptr_t) sa
->objfile
->pspace
;
2965 uib
= (uintptr_t) sa
->objfile
->pspace
;
2972 uia
= (uintptr_t) sa
->minsym
;
2973 uib
= (uintptr_t) sb
->minsym
;
2983 /* Look for all the matching instances of each symbol in NAMES. Only
2984 instances from PSPACE are considered; other program spaces are
2985 handled by our caller. If PSPACE is NULL, then all program spaces
2986 are considered. Results are stored into INFO. */
2989 add_all_symbol_names_from_pspace (struct collect_info
*info
,
2990 struct program_space
*pspace
,
2991 VEC (const_char_ptr
) *names
)
2996 for (ix
= 0; VEC_iterate (const_char_ptr
, names
, ix
, iter
); ++ix
)
2997 add_matching_symbols_to_info (iter
, info
, pspace
);
3001 find_superclass_methods (VEC (typep
) *superclasses
,
3003 VEC (const_char_ptr
) **result_names
)
3005 int old_len
= VEC_length (const_char_ptr
, *result_names
);
3006 VEC (typep
) *iter_classes
;
3007 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
3009 iter_classes
= superclasses
;
3012 VEC (typep
) *new_supers
= NULL
;
3016 make_cleanup (VEC_cleanup (typep
), &new_supers
);
3017 for (ix
= 0; VEC_iterate (typep
, iter_classes
, ix
, t
); ++ix
)
3018 find_methods (t
, name
, result_names
, &new_supers
);
3020 if (VEC_length (const_char_ptr
, *result_names
) != old_len
3021 || VEC_empty (typep
, new_supers
))
3024 iter_classes
= new_supers
;
3027 do_cleanups (cleanup
);
3030 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3031 given by one of the symbols in SYM_CLASSES. Matches are returned
3032 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
3035 find_method (struct linespec_state
*self
, VEC (symtab_ptr
) *file_symtabs
,
3036 const char *class_name
, const char *method_name
,
3037 VEC (symbolp
) *sym_classes
, VEC (symbolp
) **symbols
,
3038 VEC (bound_minimal_symbol_d
) **minsyms
)
3041 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
3043 int last_result_len
;
3044 VEC (typep
) *superclass_vec
;
3045 VEC (const_char_ptr
) *result_names
;
3046 struct collect_info info
;
3048 /* Sort symbols so that symbols with the same program space are next
3050 qsort (VEC_address (symbolp
, sym_classes
),
3051 VEC_length (symbolp
, sym_classes
),
3056 info
.file_symtabs
= file_symtabs
;
3057 info
.result
.symbols
= NULL
;
3058 info
.result
.minimal_symbols
= NULL
;
3060 /* Iterate over all the types, looking for the names of existing
3061 methods matching METHOD_NAME. If we cannot find a direct method in a
3062 given program space, then we consider inherited methods; this is
3063 not ideal (ideal would be to respect C++ hiding rules), but it
3064 seems good enough and is what GDB has historically done. We only
3065 need to collect the names because later we find all symbols with
3066 those names. This loop is written in a somewhat funny way
3067 because we collect data across the program space before deciding
3069 superclass_vec
= NULL
;
3070 make_cleanup (VEC_cleanup (typep
), &superclass_vec
);
3071 result_names
= NULL
;
3072 make_cleanup (VEC_cleanup (const_char_ptr
), &result_names
);
3073 last_result_len
= 0;
3074 for (ix
= 0; VEC_iterate (symbolp
, sym_classes
, ix
, sym
); ++ix
)
3077 struct program_space
*pspace
;
3079 /* Program spaces that are executing startup should have
3080 been filtered out earlier. */
3081 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
3082 gdb_assert (!pspace
->executing_startup
);
3083 set_current_program_space (pspace
);
3084 t
= check_typedef (SYMBOL_TYPE (sym
));
3085 find_methods (t
, method_name
, &result_names
, &superclass_vec
);
3087 /* Handle all items from a single program space at once; and be
3088 sure not to miss the last batch. */
3089 if (ix
== VEC_length (symbolp
, sym_classes
) - 1
3091 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp
, sym_classes
,
3094 /* If we did not find a direct implementation anywhere in
3095 this program space, consider superclasses. */
3096 if (VEC_length (const_char_ptr
, result_names
) == last_result_len
)
3097 find_superclass_methods (superclass_vec
, method_name
,
3100 /* We have a list of candidate symbol names, so now we
3101 iterate over the symbol tables looking for all
3102 matches in this pspace. */
3103 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
);
3105 VEC_truncate (typep
, superclass_vec
, 0);
3106 last_result_len
= VEC_length (const_char_ptr
, result_names
);
3110 if (!VEC_empty (symbolp
, info
.result
.symbols
)
3111 || !VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
3113 *symbols
= info
.result
.symbols
;
3114 *minsyms
= info
.result
.minimal_symbols
;
3115 do_cleanups (cleanup
);
3119 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3120 and other attempts to locate the symbol will be made. */
3121 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
3126 /* This object is used when collecting all matching symtabs. */
3128 struct symtab_collector
3130 /* The result vector of symtabs. */
3131 VEC (symtab_ptr
) *symtabs
;
3133 /* This is used to ensure the symtabs are unique. */
3134 htab_t symtab_table
;
3137 /* Callback for iterate_over_symtabs. */
3140 add_symtabs_to_list (struct symtab
*symtab
, void *d
)
3142 struct symtab_collector
*data
= (struct symtab_collector
*) d
;
3145 slot
= htab_find_slot (data
->symtab_table
, symtab
, INSERT
);
3149 VEC_safe_push (symtab_ptr
, data
->symtabs
, symtab
);
3155 /* Given a file name, return a VEC of all matching symtabs. If
3156 SEARCH_PSPACE is not NULL, the search is restricted to just that
3159 static VEC (symtab_ptr
) *
3160 collect_symtabs_from_filename (const char *file
,
3161 struct program_space
*search_pspace
)
3163 struct symtab_collector collector
;
3164 struct cleanup
*cleanups
;
3165 struct program_space
*pspace
;
3167 collector
.symtabs
= NULL
;
3168 collector
.symtab_table
= htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
3170 cleanups
= make_cleanup_htab_delete (collector
.symtab_table
);
3172 /* Find that file's data. */
3173 if (search_pspace
== NULL
)
3175 ALL_PSPACES (pspace
)
3177 if (pspace
->executing_startup
)
3180 set_current_program_space (pspace
);
3181 iterate_over_symtabs (file
, add_symtabs_to_list
, &collector
);
3186 set_current_program_space (search_pspace
);
3187 iterate_over_symtabs (file
, add_symtabs_to_list
, &collector
);
3190 do_cleanups (cleanups
);
3191 return collector
.symtabs
;
3194 /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3195 not NULL, the search is restricted to just that program space. */
3197 static VEC (symtab_ptr
) *
3198 symtabs_from_filename (const char *filename
,
3199 struct program_space
*search_pspace
)
3201 VEC (symtab_ptr
) *result
;
3203 result
= collect_symtabs_from_filename (filename
, search_pspace
);
3205 if (VEC_empty (symtab_ptr
, result
))
3207 if (!have_full_symbols () && !have_partial_symbols ())
3208 throw_error (NOT_FOUND_ERROR
,
3209 _("No symbol table is loaded. "
3210 "Use the \"file\" command."));
3211 source_file_not_found_error (filename
);
3217 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3218 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3219 returned in MINSYMS. */
3222 find_function_symbols (struct linespec_state
*state
,
3223 VEC (symtab_ptr
) *file_symtabs
, const char *name
,
3224 VEC (symbolp
) **symbols
,
3225 VEC (bound_minimal_symbol_d
) **minsyms
)
3227 struct collect_info info
;
3228 VEC (const_char_ptr
) *symbol_names
= NULL
;
3229 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
3233 info
.result
.symbols
= NULL
;
3234 info
.result
.minimal_symbols
= NULL
;
3235 info
.file_symtabs
= file_symtabs
;
3237 /* Try NAME as an Objective-C selector. */
3238 find_imps (name
, &symbol_names
);
3239 if (!VEC_empty (const_char_ptr
, symbol_names
))
3240 add_all_symbol_names_from_pspace (&info
, state
->search_pspace
,
3243 add_matching_symbols_to_info (name
, &info
, state
->search_pspace
);
3245 do_cleanups (cleanup
);
3247 if (VEC_empty (symbolp
, info
.result
.symbols
))
3249 VEC_free (symbolp
, info
.result
.symbols
);
3253 *symbols
= info
.result
.symbols
;
3255 if (VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
3257 VEC_free (bound_minimal_symbol_d
, info
.result
.minimal_symbols
);
3261 *minsyms
= info
.result
.minimal_symbols
;
3264 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3265 in SYMBOLS and minimal symbols in MINSYMS. */
3268 find_linespec_symbols (struct linespec_state
*state
,
3269 VEC (symtab_ptr
) *file_symtabs
,
3271 VEC (symbolp
) **symbols
,
3272 VEC (bound_minimal_symbol_d
) **minsyms
)
3274 struct cleanup
*cleanup
;
3276 const char *lookup_name
;
3278 cleanup
= demangle_for_lookup (name
, state
->language
->la_language
,
3280 if (state
->language
->la_language
== language_ada
)
3282 /* In Ada, the symbol lookups are performed using the encoded
3283 name rather than the demangled name. */
3284 lookup_name
= ada_name_for_lookup (name
);
3285 make_cleanup (xfree
, (void *) lookup_name
);
3288 canon
= cp_canonicalize_string_no_typedefs (lookup_name
);
3291 lookup_name
= canon
;
3292 make_cleanup (xfree
, canon
);
3295 /* It's important to not call expand_symtabs_matching unnecessarily
3296 as it can really slow things down (by unnecessarily expanding
3297 potentially 1000s of symtabs, which when debugging some apps can
3298 cost 100s of seconds). Avoid this to some extent by *first* calling
3299 find_function_symbols, and only if that doesn't find anything
3300 *then* call find_method. This handles two important cases:
3301 1) break (anonymous namespace)::foo
3302 2) break class::method where method is in class (and not a baseclass) */
3304 find_function_symbols (state
, file_symtabs
, lookup_name
,
3307 /* If we were unable to locate a symbol of the same name, try dividing
3308 the name into class and method names and searching the class and its
3310 if (VEC_empty (symbolp
, *symbols
)
3311 && VEC_empty (bound_minimal_symbol_d
, *minsyms
))
3313 char *klass
, *method
;
3314 const char *last
, *p
, *scope_op
;
3315 VEC (symbolp
) *classes
;
3317 /* See if we can find a scope operator and break this symbol
3318 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3320 p
= find_toplevel_string (lookup_name
, scope_op
);
3323 /* No C++ scope operator. Try Java. */
3325 p
= find_toplevel_string (lookup_name
, scope_op
);
3332 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
3335 /* If no scope operator was found, there is nothing more we can do;
3336 we already attempted to lookup the entire name as a symbol
3340 do_cleanups (cleanup
);
3344 /* LOOKUP_NAME points to the class name.
3345 LAST points to the method name. */
3346 klass
= XNEWVEC (char, last
- lookup_name
+ 1);
3347 make_cleanup (xfree
, klass
);
3348 strncpy (klass
, lookup_name
, last
- lookup_name
);
3349 klass
[last
- lookup_name
] = '\0';
3351 /* Skip past the scope operator. */
3352 last
+= strlen (scope_op
);
3353 method
= XNEWVEC (char, strlen (last
) + 1);
3354 make_cleanup (xfree
, method
);
3355 strcpy (method
, last
);
3357 /* Find a list of classes named KLASS. */
3358 classes
= lookup_prefix_sym (state
, file_symtabs
, klass
);
3359 make_cleanup (VEC_cleanup (symbolp
), &classes
);
3361 if (!VEC_empty (symbolp
, classes
))
3363 /* Now locate a list of suitable methods named METHOD. */
3366 find_method (state
, file_symtabs
, klass
, method
, classes
,
3370 /* If successful, we're done. If NOT_FOUND_ERROR
3371 was not thrown, rethrow the exception that we did get. */
3372 CATCH (except
, RETURN_MASK_ERROR
)
3374 if (except
.error
!= NOT_FOUND_ERROR
)
3375 throw_exception (except
);
3381 do_cleanups (cleanup
);
3384 /* Return all labels named NAME in FUNCTION_SYMBOLS. Return the
3385 actual function symbol in which the label was found in LABEL_FUNC_RET. */
3387 static VEC (symbolp
) *
3388 find_label_symbols (struct linespec_state
*self
,
3389 VEC (symbolp
) *function_symbols
,
3390 VEC (symbolp
) **label_funcs_ret
, const char *name
)
3393 const struct block
*block
;
3395 struct symbol
*fn_sym
;
3396 VEC (symbolp
) *result
= NULL
;
3398 if (function_symbols
== NULL
)
3400 set_current_program_space (self
->program_space
);
3401 block
= get_current_search_block ();
3404 block
&& !BLOCK_FUNCTION (block
);
3405 block
= BLOCK_SUPERBLOCK (block
))
3409 fn_sym
= BLOCK_FUNCTION (block
);
3411 sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0).symbol
;
3415 VEC_safe_push (symbolp
, result
, sym
);
3416 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
3422 VEC_iterate (symbolp
, function_symbols
, ix
, fn_sym
); ++ix
)
3424 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym
)));
3425 block
= SYMBOL_BLOCK_VALUE (fn_sym
);
3426 sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0).symbol
;
3430 VEC_safe_push (symbolp
, result
, sym
);
3431 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
3441 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3444 decode_digits_list_mode (struct linespec_state
*self
,
3446 struct symtabs_and_lines
*values
,
3447 struct symtab_and_line val
)
3452 gdb_assert (self
->list_mode
);
3454 for (ix
= 0; VEC_iterate (symtab_ptr
, ls
->file_symtabs
, ix
, elt
);
3457 /* The logic above should ensure this. */
3458 gdb_assert (elt
!= NULL
);
3460 set_current_program_space (SYMTAB_PSPACE (elt
));
3462 /* Simplistic search just for the list command. */
3463 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
3464 if (val
.symtab
== NULL
)
3466 val
.pspace
= SYMTAB_PSPACE (elt
);
3468 val
.explicit_line
= 1;
3470 add_sal_to_sals (self
, values
, &val
, NULL
, 0);
3474 /* A helper for create_sals_line_offset that iterates over the symtabs,
3475 adding lines to the VEC. */
3478 decode_digits_ordinary (struct linespec_state
*self
,
3481 struct symtabs_and_lines
*sals
,
3482 struct linetable_entry
**best_entry
)
3487 for (ix
= 0; VEC_iterate (symtab_ptr
, ls
->file_symtabs
, ix
, elt
); ++ix
)
3490 VEC (CORE_ADDR
) *pcs
;
3493 /* The logic above should ensure this. */
3494 gdb_assert (elt
!= NULL
);
3496 set_current_program_space (SYMTAB_PSPACE (elt
));
3498 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
3499 for (i
= 0; VEC_iterate (CORE_ADDR
, pcs
, i
, pc
); ++i
)
3501 struct symtab_and_line sal
;
3504 sal
.pspace
= SYMTAB_PSPACE (elt
);
3508 add_sal_to_sals_basic (sals
, &sal
);
3511 VEC_free (CORE_ADDR
, pcs
);
3517 /* Return the line offset represented by VARIABLE. */
3519 static struct line_offset
3520 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
3524 struct line_offset offset
= {0, LINE_OFFSET_NONE
};
3526 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
3529 while (*p
>= '0' && *p
<= '9')
3531 if (!*p
) /* Reached end of token without hitting non-digit. */
3533 /* We have a value history reference. */
3534 struct value
*val_history
;
3536 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
3538 = access_value_history ((variable
[1] == '$') ? -index
: index
);
3539 if (TYPE_CODE (value_type (val_history
)) != TYPE_CODE_INT
)
3540 error (_("History values used in line "
3541 "specs must have integer values."));
3542 offset
.offset
= value_as_long (val_history
);
3546 /* Not all digits -- may be user variable/function or a
3547 convenience variable. */
3549 struct internalvar
*ivar
;
3551 /* Try it as a convenience variable. If it is not a convenience
3552 variable, return and allow normal symbol lookup to occur. */
3553 ivar
= lookup_only_internalvar (variable
+ 1);
3555 /* No internal variable with that name. Mark the offset
3556 as unknown to allow the name to be looked up as a symbol. */
3557 offset
.sign
= LINE_OFFSET_UNKNOWN
;
3560 /* We found a valid variable name. If it is not an integer,
3562 if (!get_internalvar_integer (ivar
, &valx
))
3563 error (_("Convenience variables used in line "
3564 "specs must have integer values."));
3566 offset
.offset
= valx
;
3574 /* A callback used to possibly add a symbol to the results. */
3577 collect_symbols (struct symbol
*sym
, void *data
)
3579 struct collect_info
*info
= (struct collect_info
*) data
;
3581 /* In list mode, add all matching symbols, regardless of class.
3582 This allows the user to type "list a_global_variable". */
3583 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
|| info
->state
->list_mode
)
3584 VEC_safe_push (symbolp
, info
->result
.symbols
, sym
);
3585 return 1; /* Continue iterating. */
3588 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
3589 linespec; return the SAL in RESULT. This function should return SALs
3590 matching those from find_function_start_sal, otherwise false
3591 multiple-locations breakpoints could be placed. */
3594 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
3595 struct minimal_symbol
*msymbol
,
3596 struct symtabs_and_lines
*result
)
3598 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3600 struct symtab_and_line sal
;
3602 sal
= find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
),
3603 (struct obj_section
*) 0, 0);
3604 sal
.section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
);
3606 /* The minimal symbol might point to a function descriptor;
3607 resolve it to the actual code address instead. */
3608 pc
= gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
3610 sal
= find_pc_sect_line (pc
, NULL
, 0);
3612 if (self
->funfirstline
)
3614 if (sal
.symtab
!= NULL
3615 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal
.symtab
))
3616 || SYMTAB_LANGUAGE (sal
.symtab
) == language_asm
))
3618 /* If gdbarch_convert_from_func_ptr_addr does not apply then
3619 sal.SECTION, sal.LINE&co. will stay correct from above.
3620 If gdbarch_convert_from_func_ptr_addr applies then
3621 sal.SECTION is cleared from above and sal.LINE&co. will
3622 stay correct from the last find_pc_sect_line above. */
3623 sal
.pc
= MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
);
3624 sal
.pc
= gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
,
3626 if (gdbarch_skip_entrypoint_p (gdbarch
))
3627 sal
.pc
= gdbarch_skip_entrypoint (gdbarch
, sal
.pc
);
3630 skip_prologue_sal (&sal
);
3633 if (maybe_add_address (self
->addr_set
, objfile
->pspace
, sal
.pc
))
3634 add_sal_to_sals (self
, result
, &sal
, MSYMBOL_NATURAL_NAME (msymbol
), 0);
3637 /* A helper struct to pass some data through
3638 iterate_over_minimal_symbols. */
3640 struct collect_minsyms
3642 /* The objfile we're examining. */
3643 struct objfile
*objfile
;
3645 /* Only search the given symtab, or NULL to search for all symbols. */
3646 struct symtab
*symtab
;
3648 /* The funfirstline setting from the initial call. */
3651 /* The list_mode setting from the initial call. */
3654 /* The resulting symbols. */
3655 VEC (bound_minimal_symbol_d
) *msyms
;
3658 /* A helper function to classify a minimal_symbol_type according to
3662 classify_mtype (enum minimal_symbol_type t
)
3669 /* Intermediate priority. */
3672 case mst_solib_trampoline
:
3673 /* Lowest priority. */
3677 /* Highest priority. */
3682 /* Callback for qsort that sorts symbols by priority. */
3685 compare_msyms (const void *a
, const void *b
)
3687 const bound_minimal_symbol_d
*moa
= (const bound_minimal_symbol_d
*) a
;
3688 const bound_minimal_symbol_d
*mob
= (const bound_minimal_symbol_d
*) b
;
3689 enum minimal_symbol_type ta
= MSYMBOL_TYPE (moa
->minsym
);
3690 enum minimal_symbol_type tb
= MSYMBOL_TYPE (mob
->minsym
);
3692 return classify_mtype (ta
) - classify_mtype (tb
);
3695 /* Callback for iterate_over_minimal_symbols that adds the symbol to
3699 add_minsym (struct minimal_symbol
*minsym
, void *d
)
3701 struct collect_minsyms
*info
= (struct collect_minsyms
*) d
;
3702 bound_minimal_symbol_d mo
;
3705 mo
.objfile
= info
->objfile
;
3707 if (info
->symtab
!= NULL
)
3710 struct symtab_and_line sal
;
3711 struct gdbarch
*gdbarch
= get_objfile_arch (info
->objfile
);
3713 sal
= find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info
->objfile
, minsym
),
3715 sal
.section
= MSYMBOL_OBJ_SECTION (info
->objfile
, minsym
);
3717 = gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
3719 sal
= find_pc_sect_line (pc
, NULL
, 0);
3721 if (info
->symtab
!= sal
.symtab
)
3725 /* Exclude data symbols when looking for breakpoint locations. */
3726 if (!info
->list_mode
)
3727 switch (minsym
->type
)
3729 case mst_slot_got_plt
:
3736 /* Make sure this minsym is not a function descriptor
3737 before we decide to discard it. */
3738 struct gdbarch
*gdbarch
= get_objfile_arch (info
->objfile
);
3739 CORE_ADDR addr
= gdbarch_convert_from_func_ptr_addr
3740 (gdbarch
, BMSYMBOL_VALUE_ADDRESS (mo
),
3743 if (addr
== BMSYMBOL_VALUE_ADDRESS (mo
))
3748 VEC_safe_push (bound_minimal_symbol_d
, info
->msyms
, &mo
);
3751 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
3752 is not NULL, the search is restricted to just that program
3755 If SYMTAB is NULL, search all objfiles, otherwise
3756 restrict results to the given SYMTAB. */
3759 search_minsyms_for_name (struct collect_info
*info
, const char *name
,
3760 struct program_space
*search_pspace
,
3761 struct symtab
*symtab
)
3763 struct collect_minsyms local
;
3764 struct cleanup
*cleanup
;
3766 memset (&local
, 0, sizeof (local
));
3767 local
.funfirstline
= info
->state
->funfirstline
;
3768 local
.list_mode
= info
->state
->list_mode
;
3769 local
.symtab
= symtab
;
3771 cleanup
= make_cleanup (VEC_cleanup (bound_minimal_symbol_d
), &local
.msyms
);
3775 struct program_space
*pspace
;
3777 ALL_PSPACES (pspace
)
3779 struct objfile
*objfile
;
3781 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
3783 if (pspace
->executing_startup
)
3786 set_current_program_space (pspace
);
3788 ALL_OBJFILES (objfile
)
3790 local
.objfile
= objfile
;
3791 iterate_over_minimal_symbols (objfile
, name
, add_minsym
, &local
);
3797 if (search_pspace
== NULL
|| SYMTAB_PSPACE (symtab
) == search_pspace
)
3799 set_current_program_space (SYMTAB_PSPACE (symtab
));
3800 local
.objfile
= SYMTAB_OBJFILE(symtab
);
3801 iterate_over_minimal_symbols (local
.objfile
, name
, add_minsym
,
3806 if (!VEC_empty (bound_minimal_symbol_d
, local
.msyms
))
3810 bound_minimal_symbol_d
*item
;
3812 qsort (VEC_address (bound_minimal_symbol_d
, local
.msyms
),
3813 VEC_length (bound_minimal_symbol_d
, local
.msyms
),
3814 sizeof (bound_minimal_symbol_d
),
3817 /* Now the minsyms are in classification order. So, we walk
3818 over them and process just the minsyms with the same
3819 classification as the very first minsym in the list. */
3820 item
= VEC_index (bound_minimal_symbol_d
, local
.msyms
, 0);
3821 classification
= classify_mtype (MSYMBOL_TYPE (item
->minsym
));
3824 VEC_iterate (bound_minimal_symbol_d
, local
.msyms
, ix
, item
);
3827 if (classify_mtype (MSYMBOL_TYPE (item
->minsym
)) != classification
)
3830 VEC_safe_push (bound_minimal_symbol_d
,
3831 info
->result
.minimal_symbols
, item
);
3835 do_cleanups (cleanup
);
3838 /* A helper function to add all symbols matching NAME to INFO. If
3839 PSPACE is not NULL, the search is restricted to just that program
3843 add_matching_symbols_to_info (const char *name
,
3844 struct collect_info
*info
,
3845 struct program_space
*pspace
)
3850 for (ix
= 0; VEC_iterate (symtab_ptr
, info
->file_symtabs
, ix
, elt
); ++ix
)
3854 iterate_over_all_matching_symtabs (info
->state
, name
, VAR_DOMAIN
,
3855 collect_symbols
, info
,
3857 search_minsyms_for_name (info
, name
, pspace
, NULL
);
3859 else if (pspace
== NULL
|| pspace
== SYMTAB_PSPACE (elt
))
3861 int prev_len
= VEC_length (symbolp
, info
->result
.symbols
);
3863 /* Program spaces that are executing startup should have
3864 been filtered out earlier. */
3865 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
3866 set_current_program_space (SYMTAB_PSPACE (elt
));
3867 iterate_over_file_blocks (elt
, name
, VAR_DOMAIN
,
3868 collect_symbols
, info
);
3870 /* If no new symbols were found in this iteration and this symtab
3871 is in assembler, we might actually be looking for a label for
3872 which we don't have debug info. Check for a minimal symbol in
3874 if (prev_len
== VEC_length (symbolp
, info
->result
.symbols
)
3875 && elt
->language
== language_asm
)
3876 search_minsyms_for_name (info
, name
, pspace
, elt
);
3883 /* Now come some functions that are called from multiple places within
3887 symbol_to_sal (struct symtab_and_line
*result
,
3888 int funfirstline
, struct symbol
*sym
)
3890 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
3892 *result
= find_function_start_sal (sym
, funfirstline
);
3897 if (SYMBOL_CLASS (sym
) == LOC_LABEL
&& SYMBOL_VALUE_ADDRESS (sym
) != 0)
3900 result
->symtab
= symbol_symtab (sym
);
3901 result
->line
= SYMBOL_LINE (sym
);
3902 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
3903 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
3904 result
->explicit_pc
= 1;
3907 else if (funfirstline
)
3911 else if (SYMBOL_LINE (sym
) != 0)
3913 /* We know its line number. */
3915 result
->symtab
= symbol_symtab (sym
);
3916 result
->line
= SYMBOL_LINE (sym
);
3917 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
3925 /* See the comment in linespec.h. */
3928 init_linespec_result (struct linespec_result
*lr
)
3930 memset (lr
, 0, sizeof (*lr
));
3933 /* See the comment in linespec.h. */
3936 destroy_linespec_result (struct linespec_result
*ls
)
3939 struct linespec_sals
*lsal
;
3941 delete_event_location (ls
->location
);
3942 for (i
= 0; VEC_iterate (linespec_sals
, ls
->sals
, i
, lsal
); ++i
)
3944 xfree (lsal
->canonical
);
3945 xfree (lsal
->sals
.sals
);
3947 VEC_free (linespec_sals
, ls
->sals
);
3950 /* Cleanup function for a linespec_result. */
3953 cleanup_linespec_result (void *a
)
3955 destroy_linespec_result ((struct linespec_result
*) a
);
3958 /* See the comment in linespec.h. */
3961 make_cleanup_destroy_linespec_result (struct linespec_result
*ls
)
3963 return make_cleanup (cleanup_linespec_result
, ls
);
3966 /* Return the quote characters permitted by the linespec parser. */
3969 get_gdb_linespec_parser_quote_characters (void)
3971 return linespec_quote_characters
;