1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2005, 2007-2012 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"
36 #include "exceptions.h"
39 #include "mi/mi-cmds.h"
41 #include "arch-utils.h"
43 #include "cli/cli-utils.h"
44 #include "filenames.h"
48 typedef struct symtab
*symtab_p
;
51 typedef struct symbol
*symbolp
;
54 typedef struct type
*typep
;
57 /* An address entry is used to ensure that any given location is only
58 added to the result a single time. It holds an address and the
59 program space from which the address came. */
63 struct program_space
*pspace
;
67 /* A helper struct which just holds a minimal symbol and the object
68 file from which it came. */
70 typedef struct minsym_and_objfile
72 struct minimal_symbol
*minsym
;
73 struct objfile
*objfile
;
74 } minsym_and_objfile_d
;
76 DEF_VEC_O (minsym_and_objfile_d
);
78 /* An enumeration of possible signs for a line offset. */
79 enum offset_relative_sign
84 /* A plus sign ("+") */
87 /* A minus sign ("-") */
90 /* A special "sign" for unspecified offset. */
94 /* A line offset in a linespec. */
98 /* Line offset and any specified sign. */
100 enum offset_relative_sign sign
;
103 /* A linespec. Elements of this structure are filled in by a parser
104 (either parse_linespec or some other function). The structure is
105 then converted into SALs by convert_linespec_to_sals. */
109 /* An expression and the resulting PC. Specifying an expression
110 currently precludes the use of other members. */
112 /* The expression entered by the user. */
113 const char *expression
;
115 /* The resulting PC expression derived from evaluating EXPRESSION. */
118 /* Any specified file symtabs. */
120 /* The user-supplied source filename or NULL if none was specified. */
121 const char *source_filename
;
123 /* The list of symtabs to search to which to limit the search. May not
124 be NULL. If SOURCE_FILENAME is NULL (no user-specified filename),
125 FILE_SYMTABS should contain one single NULL member. This will
126 cause the code to use the default symtab. */
127 VEC (symtab_p
) *file_symtabs
;
129 /* The name of a function or method and any matching symbols. */
131 /* The user-specified function name. If no function name was
132 supplied, this may be NULL. */
133 const char *function_name
;
135 /* A list of matching function symbols and minimal symbols. Both lists
136 may be NULL if no matching symbols were found. */
137 VEC (symbolp
) *function_symbols
;
138 VEC (minsym_and_objfile_d
) *minimal_symbols
;
140 /* The name of a label and matching symbols. */
142 /* The user-specified label name. */
143 const char *label_name
;
145 /* A structure of matching label symbols and the corresponding
146 function symbol in which the label was found. Both may be NULL
147 or both must be non-NULL. */
150 VEC (symbolp
) *label_symbols
;
151 VEC (symbolp
) *function_symbols
;
154 /* Line offset. It may be LINE_OFFSET_UNKNOWN, meaning that no
155 offset was specified. */
156 struct line_offset line_offset
;
158 typedef struct linespec
*linespec_p
;
160 /* An instance of this is used to keep all state while linespec
161 operates. This instance is passed around as a 'this' pointer to
162 the various implementation methods. */
164 struct linespec_state
166 /* The language in use during linespec processing. */
167 const struct language_defn
*language
;
169 /* The program space as seen when the module was entered. */
170 struct program_space
*program_space
;
172 /* The default symtab to use, if no other symtab is specified. */
173 struct symtab
*default_symtab
;
175 /* The default line to use. */
178 /* The 'funfirstline' value that was passed in to decode_line_1 or
182 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
185 /* The 'canonical' value passed to decode_line_full, or NULL. */
186 struct linespec_result
*canonical
;
188 /* Canonical strings that mirror the symtabs_and_lines result. */
189 char **canonical_names
;
191 /* This is a set of address_entry objects which is used to prevent
192 duplicate symbols from being entered into the result. */
196 /* This is a helper object that is used when collecting symbols into a
201 /* The linespec object in use. */
202 struct linespec_state
*state
;
204 /* A list of symtabs to which to restrict matches. */
205 VEC (symtab_p
) *file_symtabs
;
207 /* The result being accumulated. */
210 VEC (symbolp
) *symbols
;
211 VEC (minsym_and_objfile_d
) *minimal_symbols
;
222 /* A colon "separator" */
234 /* EOI (end of input) */
240 typedef enum ls_token_type linespec_token_type
;
242 /* List of keywords */
244 static const char * const linespec_keywords
[] = { "if", "thread", "task" };
246 /* A token of the linespec lexer */
250 /* The type of the token */
251 linespec_token_type type
;
253 /* Data for the token */
256 /* A string, given as a stoken */
257 struct stoken string
;
263 typedef struct ls_token linespec_token
;
265 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
266 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
268 /* An instance of the linespec parser. */
272 /* Lexer internal data */
275 /* Save head of input stream. */
278 /* Head of the input stream. */
280 #define PARSER_STREAM(P) (*(P)->lexer.stream)
282 /* The current token. */
283 linespec_token current
;
286 /* Is the entire linespec quote-enclosed? */
287 int is_quote_enclosed
;
289 /* The state of the parse. */
290 struct linespec_state state
;
291 #define PARSER_STATE(PPTR) (&(PPTR)->state)
293 /* The result of the parse. */
294 struct linespec result
;
295 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
297 typedef struct ls_parser linespec_parser
;
299 /* Prototypes for local functions. */
301 static void initialize_defaults (struct symtab
**default_symtab
,
304 static CORE_ADDR
linespec_expression_to_pc (char **exp_ptr
);
306 static struct symtabs_and_lines
decode_objc (struct linespec_state
*self
,
310 static VEC (symtab_p
) *symtabs_from_filename (const char *);
312 static VEC (symbolp
) *find_label_symbols (struct linespec_state
*self
,
313 VEC (symbolp
) *function_symbols
,
314 VEC (symbolp
) **label_funcs_ret
,
317 void find_linespec_symbols (struct linespec_state
*self
,
318 VEC (symtab_p
) *file_symtabs
,
320 VEC (symbolp
) **symbols
,
321 VEC (minsym_and_objfile_d
) **minsyms
);
323 static struct line_offset
324 linespec_parse_variable (struct linespec_state
*self
,
325 const char *variable
);
327 static int symbol_to_sal (struct symtab_and_line
*result
,
328 int funfirstline
, struct symbol
*sym
);
330 static void add_matching_symbols_to_info (const char *name
,
331 struct collect_info
*info
,
332 struct program_space
*pspace
);
334 static void add_all_symbol_names_from_pspace (struct collect_info
*info
,
335 struct program_space
*pspace
,
336 VEC (const_char_ptr
) *names
);
338 static VEC (symtab_p
) *collect_symtabs_from_filename (const char *file
);
340 static void decode_digits_ordinary (struct linespec_state
*self
,
343 struct symtabs_and_lines
*sals
,
344 struct linetable_entry
**best_entry
);
346 static void decode_digits_list_mode (struct linespec_state
*self
,
348 struct symtabs_and_lines
*values
,
349 struct symtab_and_line val
);
351 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
352 struct minimal_symbol
*msymbol
,
353 struct symtabs_and_lines
*result
);
355 static int compare_symbols (const void *a
, const void *b
);
357 static int compare_msymbols (const void *a
, const void *b
);
359 static const char *find_toplevel_char (const char *s
, char c
);
361 /* Permitted quote characters for the parser. This is different from the
362 completer's quote characters to allow backward compatibility with the
364 static const char *const linespec_quote_characters
= "\"\'";
366 /* Lexer functions. */
368 /* Lex a number from the input in PARSER. This only supports
370 Return true if input is decimal numbers. Return false if not. */
373 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
375 tokenp
->type
= LSTOKEN_NUMBER
;
376 LS_TOKEN_STOKEN (*tokenp
).length
= 0;
377 LS_TOKEN_STOKEN (*tokenp
).ptr
= PARSER_STREAM (parser
);
379 /* Keep any sign at the start of the stream. */
380 if (*PARSER_STREAM (parser
) == '+' || *PARSER_STREAM (parser
) == '-')
382 ++LS_TOKEN_STOKEN (*tokenp
).length
;
383 ++(PARSER_STREAM (parser
));
386 while (isdigit (*PARSER_STREAM (parser
)))
388 ++LS_TOKEN_STOKEN (*tokenp
).length
;
389 ++(PARSER_STREAM (parser
));
392 if (*PARSER_STREAM (parser
) != '\0' && !isspace(*PARSER_STREAM (parser
)))
394 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (*tokenp
).ptr
;
401 /* Does P represent one of the keywords? If so, return
402 the keyword. If not, return NULL. */
405 linespec_lexer_lex_keyword (const char *p
)
411 for (i
= 0; i
< ARRAY_SIZE (linespec_keywords
); ++i
)
413 int len
= strlen (linespec_keywords
[i
]);
415 /* If P begins with one of the keywords and the next
416 character is not a valid identifier character,
417 we have found a keyword. */
418 if (strncmp (p
, linespec_keywords
[i
], len
) == 0
419 && !(isalnum (p
[len
]) || p
[len
] == '_'))
420 return linespec_keywords
[i
];
427 /* Does STRING represent an Ada operator? If so, return the length
428 of the decoded operator name. If not, return 0. */
431 is_ada_operator (const char *string
)
433 const struct ada_opname_map
*mapping
;
435 for (mapping
= ada_opname_table
;
436 mapping
->encoded
!= NULL
437 && strncmp (mapping
->decoded
, string
,
438 strlen (mapping
->decoded
)) != 0; ++mapping
)
441 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
444 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
445 the location of QUOTE_CHAR, or NULL if not found. */
448 skip_quote_char (const char *string
, char quote_char
)
450 const char *p
, *last
;
452 p
= last
= find_toplevel_char (string
, quote_char
);
453 while (p
&& *p
!= '\0' && *p
!= ':')
455 p
= find_toplevel_char (p
, quote_char
);
463 /* Make a writable copy of the string given in TOKEN, trimming
464 any trailing whitespace. */
467 copy_token_string (linespec_token token
)
471 if (token
.type
== LSTOKEN_KEYWORD
)
472 return xstrdup (LS_TOKEN_KEYWORD (token
));
474 str
= savestring (LS_TOKEN_STOKEN (token
).ptr
,
475 LS_TOKEN_STOKEN (token
).length
);
476 s
= remove_trailing_whitespace (str
, str
+ LS_TOKEN_STOKEN (token
).length
);
482 /* Does P represent the end of a quote-enclosed linespec? */
485 is_closing_quote_enclosed (const char *p
)
487 if (strchr (linespec_quote_characters
, *p
))
489 p
= skip_spaces ((char *) p
);
490 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
493 /* Find the end of the parameter list that starts with *INPUT.
494 This helper function assists with lexing string segments
495 which might contain valid (non-terminating) commas. */
498 find_parameter_list_end (char *input
)
500 char end_char
, start_char
;
505 if (start_char
== '(')
507 else if (start_char
== '<')
516 if (*p
== start_char
)
518 else if (*p
== end_char
)
533 /* Lex a string from the input in PARSER. */
535 static linespec_token
536 linespec_lexer_lex_string (linespec_parser
*parser
)
538 linespec_token token
;
539 char *start
= PARSER_STREAM (parser
);
541 token
.type
= LSTOKEN_STRING
;
543 /* If the input stream starts with a quote character, skip to the next
544 quote character, regardless of the content. */
545 if (strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
548 char quote_char
= *PARSER_STREAM (parser
);
550 /* Special case: Ada operators. */
551 if (PARSER_STATE (parser
)->language
->la_language
== language_ada
552 && quote_char
== '\"')
554 int len
= is_ada_operator (PARSER_STREAM (parser
));
558 /* The input is an Ada operator. Return the quoted string
560 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
561 LS_TOKEN_STOKEN (token
).length
= len
;
562 PARSER_STREAM (parser
) += len
;
566 /* The input does not represent an Ada operator -- fall through
567 to normal quoted string handling. */
570 /* Skip past the beginning quote. */
571 ++(PARSER_STREAM (parser
));
573 /* Mark the start of the string. */
574 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
576 /* Skip to the ending quote. */
577 end
= skip_quote_char (PARSER_STREAM (parser
), quote_char
);
579 /* Error if the input did not terminate properly. */
581 error (_("unmatched quote"));
583 /* Skip over the ending quote and mark the length of the string. */
584 PARSER_STREAM (parser
) = (char *) ++end
;
585 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 2 - start
;
591 /* Otherwise, only identifier characters are permitted.
592 Spaces are the exception. In general, we keep spaces,
593 but only if the next characters in the input do not resolve
594 to one of the keywords.
596 This allows users to forgo quoting CV-qualifiers, template arguments,
597 and similar common language constructs. */
601 if (isspace (*PARSER_STREAM (parser
)))
603 p
= skip_spaces (PARSER_STREAM (parser
));
604 if (linespec_lexer_lex_keyword (p
) != NULL
)
606 LS_TOKEN_STOKEN (token
).ptr
= start
;
607 LS_TOKEN_STOKEN (token
).length
608 = PARSER_STREAM (parser
) - start
;
612 /* Advance past the whitespace. */
613 PARSER_STREAM (parser
) = p
;
616 /* If the next character is EOI or (single) ':', the
617 string is complete; return the token. */
618 if (*PARSER_STREAM (parser
) == 0)
620 LS_TOKEN_STOKEN (token
).ptr
= start
;
621 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
624 else if (PARSER_STREAM (parser
)[0] == ':')
626 /* Do not tokenize the C++ scope operator. */
627 if (PARSER_STREAM (parser
)[1] == ':')
628 ++(PARSER_STREAM (parser
));
630 /* Do not tokenify if the input length so far is one
631 (i.e, a single-letter drive name) and the next character
632 is a directory separator. This allows Windows-style
633 paths to be recognized as filenames without quoting it. */
634 else if ((PARSER_STREAM (parser
) - start
) != 1
635 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser
)[1]))
637 LS_TOKEN_STOKEN (token
).ptr
= start
;
638 LS_TOKEN_STOKEN (token
).length
639 = PARSER_STREAM (parser
) - start
;
643 /* Special case: permit quote-enclosed linespecs. */
644 else if (parser
->is_quote_enclosed
645 && strchr (linespec_quote_characters
,
646 *PARSER_STREAM (parser
))
647 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
649 LS_TOKEN_STOKEN (token
).ptr
= start
;
650 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
653 /* Because commas may terminate a linespec and appear in
654 the middle of valid string input, special cases for
655 '<' and '(' are necessary. */
656 else if (*PARSER_STREAM (parser
) == '<'
657 || *PARSER_STREAM (parser
) == '(')
661 p
= find_parameter_list_end (PARSER_STREAM (parser
));
664 PARSER_STREAM (parser
) = p
;
668 /* Commas are terminators, but not if they are part of an
670 else if (*PARSER_STREAM (parser
) == ',')
672 if ((PARSER_STATE (parser
)->language
->la_language
674 && (PARSER_STREAM (parser
) - start
) > 8
675 /* strlen ("operator") */)
677 char *p
= strstr (start
, "operator");
679 if (p
!= NULL
&& is_operator_name (p
))
681 /* This is an operator name. Keep going. */
682 ++(PARSER_STREAM (parser
));
687 /* Comma terminates the string. */
688 LS_TOKEN_STOKEN (token
).ptr
= start
;
689 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
693 /* Advance the stream. */
694 ++(PARSER_STREAM (parser
));
701 /* Lex a single linespec token from PARSER. */
703 static linespec_token
704 linespec_lexer_lex_one (linespec_parser
*parser
)
708 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
710 /* Skip any whitespace. */
711 PARSER_STREAM (parser
) = skip_spaces (PARSER_STREAM (parser
));
713 /* Check for a keyword. */
714 keyword
= linespec_lexer_lex_keyword (PARSER_STREAM (parser
));
717 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
718 LS_TOKEN_KEYWORD (parser
->lexer
.current
) = keyword
;
719 return parser
->lexer
.current
;
722 /* Handle other tokens. */
723 switch (*PARSER_STREAM (parser
))
726 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
730 case '0': case '1': case '2': case '3': case '4':
731 case '5': case '6': case '7': case '8': case '9':
732 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
733 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
737 /* If we have a scope operator, lex the input as a string.
738 Otherwise, return LSTOKEN_COLON. */
739 if (PARSER_STREAM (parser
)[1] == ':')
740 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
743 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
744 ++(PARSER_STREAM (parser
));
748 case '\'': case '\"':
749 /* Special case: permit quote-enclosed linespecs. */
750 if (parser
->is_quote_enclosed
751 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
753 ++(PARSER_STREAM (parser
));
754 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
757 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
761 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
762 LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
763 = PARSER_STREAM (parser
);
764 LS_TOKEN_STOKEN (parser
->lexer
.current
).length
= 1;
765 ++(PARSER_STREAM (parser
));
769 /* If the input is not a number, it must be a string.
770 [Keywords were already considered above.] */
771 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
776 return parser
->lexer
.current
;
779 /* Consume the current token and return the next token in PARSER's
782 static linespec_token
783 linespec_lexer_consume_token (linespec_parser
*parser
)
785 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
786 return linespec_lexer_lex_one (parser
);
789 /* Return the next token without consuming the current token. */
791 static linespec_token
792 linespec_lexer_peek_token (linespec_parser
*parser
)
795 char *saved_stream
= PARSER_STREAM (parser
);
796 linespec_token saved_token
= parser
->lexer
.current
;
798 next
= linespec_lexer_consume_token (parser
);
799 PARSER_STREAM (parser
) = saved_stream
;
800 parser
->lexer
.current
= saved_token
;
804 /* Helper functions. */
806 /* Add SAL to SALS. */
809 add_sal_to_sals_basic (struct symtabs_and_lines
*sals
,
810 struct symtab_and_line
*sal
)
813 sals
->sals
= xrealloc (sals
->sals
, sals
->nelts
* sizeof (sals
->sals
[0]));
814 sals
->sals
[sals
->nelts
- 1] = *sal
;
817 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
818 the new sal, if needed. If not NULL, SYMNAME is the name of the
819 symbol to use when constructing the new canonical name.
821 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
822 canonical name for the SAL. */
825 add_sal_to_sals (struct linespec_state
*self
,
826 struct symtabs_and_lines
*sals
,
827 struct symtab_and_line
*sal
,
828 const char *symname
, int literal_canonical
)
830 add_sal_to_sals_basic (sals
, sal
);
834 char *canonical_name
= NULL
;
836 self
->canonical_names
= xrealloc (self
->canonical_names
,
837 sals
->nelts
* sizeof (char *));
838 if (!literal_canonical
&& sal
->symtab
&& sal
->symtab
->filename
)
840 char *filename
= sal
->symtab
->filename
;
842 /* Note that the filter doesn't have to be a valid linespec
843 input. We only apply the ":LINE" treatment to Ada for
845 if (symname
!= NULL
&& sal
->line
!= 0
846 && self
->language
->la_language
== language_ada
)
847 canonical_name
= xstrprintf ("%s:%s:%d", filename
, symname
,
849 else if (symname
!= NULL
)
850 canonical_name
= xstrprintf ("%s:%s", filename
, symname
);
852 canonical_name
= xstrprintf ("%s:%d", filename
, sal
->line
);
854 else if (symname
!= NULL
)
855 canonical_name
= xstrdup (symname
);
857 self
->canonical_names
[sals
->nelts
- 1] = canonical_name
;
861 /* A hash function for address_entry. */
864 hash_address_entry (const void *p
)
866 const struct address_entry
*aep
= p
;
869 hash
= iterative_hash_object (aep
->pspace
, 0);
870 return iterative_hash_object (aep
->addr
, hash
);
873 /* An equality function for address_entry. */
876 eq_address_entry (const void *a
, const void *b
)
878 const struct address_entry
*aea
= a
;
879 const struct address_entry
*aeb
= b
;
881 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
884 /* Check whether the address, represented by PSPACE and ADDR, is
885 already in the set. If so, return 0. Otherwise, add it and return
889 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
891 struct address_entry e
, *p
;
896 slot
= htab_find_slot (set
, &e
, INSERT
);
900 p
= XNEW (struct address_entry
);
901 memcpy (p
, &e
, sizeof (struct address_entry
));
907 /* A callback function and the additional data to call it with. */
909 struct symbol_and_data_callback
911 /* The callback to use. */
912 symbol_found_callback_ftype
*callback
;
914 /* Data to be passed to the callback. */
918 /* A helper for iterate_over_all_matching_symtabs that is used to
919 restrict calls to another callback to symbols representing inline
923 iterate_inline_only (struct symbol
*sym
, void *d
)
925 if (SYMBOL_INLINED (sym
))
927 struct symbol_and_data_callback
*cad
= d
;
929 return cad
->callback (sym
, cad
->data
);
931 return 1; /* Continue iterating. */
934 /* Some data for the expand_symtabs_matching callback. */
936 struct symbol_matcher_data
938 /* The lookup name against which symbol name should be compared. */
939 const char *lookup_name
;
941 /* The routine to be used for comparison. */
942 symbol_name_cmp_ftype symbol_name_cmp
;
945 /* A helper for iterate_over_all_matching_symtabs that is passed as a
946 callback to the expand_symtabs_matching method. */
949 iterate_name_matcher (const char *name
, void *d
)
951 const struct symbol_matcher_data
*data
= d
;
953 if (data
->symbol_name_cmp (name
, data
->lookup_name
) == 0)
954 return 1; /* Expand this symbol's symbol table. */
955 return 0; /* Skip this symbol. */
958 /* A helper that walks over all matching symtabs in all objfiles and
959 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
960 not NULL, then the search is restricted to just that program
961 space. If INCLUDE_INLINE is nonzero then symbols representing
962 inlined instances of functions will be included in the result. */
965 iterate_over_all_matching_symtabs (struct linespec_state
*state
,
967 const domain_enum domain
,
968 symbol_found_callback_ftype
*callback
,
970 struct program_space
*search_pspace
,
973 struct objfile
*objfile
;
974 struct program_space
*pspace
;
975 struct symbol_matcher_data matcher_data
;
977 matcher_data
.lookup_name
= name
;
978 matcher_data
.symbol_name_cmp
=
979 state
->language
->la_get_symbol_name_cmp
!= NULL
980 ? state
->language
->la_get_symbol_name_cmp (name
)
985 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
987 if (pspace
->executing_startup
)
990 set_current_program_space (pspace
);
992 ALL_OBJFILES (objfile
)
994 struct symtab
*symtab
;
997 objfile
->sf
->qf
->expand_symtabs_matching (objfile
, NULL
,
998 iterate_name_matcher
,
1002 ALL_OBJFILE_PRIMARY_SYMTABS (objfile
, symtab
)
1004 struct block
*block
;
1006 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1007 LA_ITERATE_OVER_SYMBOLS (block
, name
, domain
, callback
, data
);
1011 struct symbol_and_data_callback cad
= { callback
, data
};
1014 for (i
= FIRST_LOCAL_BLOCK
;
1015 i
< BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (symtab
)); i
++)
1017 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), i
);
1018 LA_ITERATE_OVER_SYMBOLS (block
, name
, domain
,
1019 iterate_inline_only
, &cad
);
1027 /* Returns the block to be used for symbol searches for the given SYMTAB,
1028 which may be NULL. */
1030 static struct block
*
1031 get_search_block (struct symtab
*symtab
)
1033 struct block
*block
;
1036 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1039 enum language save_language
;
1041 /* get_selected_block can change the current language when there is
1042 no selected frame yet. */
1043 save_language
= current_language
->la_language
;
1044 block
= get_selected_block (0);
1045 set_language (save_language
);
1051 /* A helper for find_method. This finds all methods in type T which
1052 match NAME. It adds matching symbol names to RESULT_NAMES, and
1053 adds T's direct superclasses to SUPERCLASSES. */
1056 find_methods (struct type
*t
, const char *name
,
1057 VEC (const_char_ptr
) **result_names
,
1058 VEC (typep
) **superclasses
)
1062 const char *class_name
= type_name_no_tag (t
);
1064 /* Ignore this class if it doesn't have a name. This is ugly, but
1065 unless we figure out how to get the physname without the name of
1066 the class, then the loop can't do any good. */
1070 int name_len
= strlen (name
);
1074 /* Loop over each method name. At this level, all overloads of a name
1075 are counted as a single name. There is an inner loop which loops over
1078 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1079 method_counter
>= 0;
1082 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1083 char dem_opname
[64];
1085 if (strncmp (method_name
, "__", 2) == 0 ||
1086 strncmp (method_name
, "op", 2) == 0 ||
1087 strncmp (method_name
, "type", 4) == 0)
1089 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
1090 method_name
= dem_opname
;
1091 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
1092 method_name
= dem_opname
;
1095 if (strcmp_iw (method_name
, name
) == 0)
1099 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1105 const char *phys_name
;
1107 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1108 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1110 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1111 VEC_safe_push (const_char_ptr
, *result_names
, phys_name
);
1117 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1118 VEC_safe_push (typep
, *superclasses
, TYPE_BASECLASS (t
, ibase
));
1121 /* Find an instance of the character C in the string S that is outside
1122 of all parenthesis pairs, single-quoted strings, and double-quoted
1123 strings. Also, ignore the char within a template name, like a ','
1124 within foo<int, int>. */
1127 find_toplevel_char (const char *s
, char c
)
1129 int quoted
= 0; /* zero if we're not in quotes;
1130 '"' if we're in a double-quoted string;
1131 '\'' if we're in a single-quoted string. */
1132 int depth
= 0; /* Number of unclosed parens we've seen. */
1135 for (scan
= s
; *scan
; scan
++)
1139 if (*scan
== quoted
)
1141 else if (*scan
== '\\' && *(scan
+ 1))
1144 else if (*scan
== c
&& ! quoted
&& depth
== 0)
1146 else if (*scan
== '"' || *scan
== '\'')
1148 else if (*scan
== '(' || *scan
== '<')
1150 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
1157 /* The string equivalent of find_toplevel_char. Returns a pointer
1158 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1159 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1162 find_toplevel_string (const char *haystack
, const char *needle
)
1164 const char *s
= haystack
;
1168 s
= find_toplevel_char (s
, *needle
);
1172 /* Found first char in HAYSTACK; check rest of string. */
1173 if (strncmp (s
, needle
, strlen (needle
)) == 0)
1176 /* Didn't find it; loop over HAYSTACK, looking for the next
1177 instance of the first character of NEEDLE. */
1181 while (s
!= NULL
&& *s
!= '\0');
1183 /* NEEDLE was not found in HAYSTACK. */
1187 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1188 and store the result in SELF->CANONICAL. */
1191 filter_results (struct linespec_state
*self
,
1192 struct symtabs_and_lines
*result
,
1193 VEC (const_char_ptr
) *filters
)
1198 for (i
= 0; VEC_iterate (const_char_ptr
, filters
, i
, name
); ++i
)
1200 struct linespec_sals lsal
;
1203 memset (&lsal
, 0, sizeof (lsal
));
1205 for (j
= 0; j
< result
->nelts
; ++j
)
1207 if (strcmp (name
, self
->canonical_names
[j
]) == 0)
1208 add_sal_to_sals_basic (&lsal
.sals
, &result
->sals
[j
]);
1211 if (lsal
.sals
.nelts
> 0)
1213 lsal
.canonical
= xstrdup (name
);
1214 VEC_safe_push (linespec_sals
, self
->canonical
->sals
, &lsal
);
1218 self
->canonical
->pre_expanded
= 0;
1221 /* Store RESULT into SELF->CANONICAL. */
1224 convert_results_to_lsals (struct linespec_state
*self
,
1225 struct symtabs_and_lines
*result
)
1227 struct linespec_sals lsal
;
1229 lsal
.canonical
= NULL
;
1230 lsal
.sals
= *result
;
1231 VEC_safe_push (linespec_sals
, self
->canonical
->sals
, &lsal
);
1234 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1235 will either return normally, throw an exception on multiple
1236 results, or present a menu to the user. On return, the SALS vector
1237 in SELF->CANONICAL is set up properly. */
1240 decode_line_2 (struct linespec_state
*self
,
1241 struct symtabs_and_lines
*result
,
1242 const char *select_mode
)
1245 char *args
, *prompt
;
1247 struct cleanup
*old_chain
;
1248 VEC (const_char_ptr
) *item_names
= NULL
, *filters
= NULL
;
1249 struct get_number_or_range_state state
;
1251 gdb_assert (select_mode
!= multiple_symbols_all
);
1252 gdb_assert (self
->canonical
!= NULL
);
1254 old_chain
= make_cleanup (VEC_cleanup (const_char_ptr
), &item_names
);
1255 make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
1256 for (i
= 0; i
< result
->nelts
; ++i
)
1261 gdb_assert (self
->canonical_names
[i
] != NULL
);
1262 for (j
= 0; VEC_iterate (const_char_ptr
, item_names
, j
, iter
); ++j
)
1264 if (strcmp (iter
, self
->canonical_names
[i
]) == 0)
1272 VEC_safe_push (const_char_ptr
, item_names
, self
->canonical_names
[i
]);
1275 if (select_mode
== multiple_symbols_cancel
1276 && VEC_length (const_char_ptr
, item_names
) > 1)
1277 error (_("canceled because the command is ambiguous\n"
1278 "See set/show multiple-symbol."));
1280 if (select_mode
== multiple_symbols_all
1281 || VEC_length (const_char_ptr
, item_names
) == 1)
1283 do_cleanups (old_chain
);
1284 convert_results_to_lsals (self
, result
);
1288 /* Sort the list of method names alphabetically. */
1289 qsort (VEC_address (const_char_ptr
, item_names
),
1290 VEC_length (const_char_ptr
, item_names
),
1291 sizeof (const_char_ptr
), compare_strings
);
1293 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1294 for (i
= 0; VEC_iterate (const_char_ptr
, item_names
, i
, iter
); ++i
)
1295 printf_unfiltered ("[%d] %s\n", i
+ 2, iter
);
1297 prompt
= getenv ("PS2");
1302 args
= command_line_input (prompt
, 0, "overload-choice");
1304 if (args
== 0 || *args
== 0)
1305 error_no_arg (_("one or more choice numbers"));
1307 init_number_or_range (&state
, args
);
1308 while (!state
.finished
)
1312 num
= get_number_or_range (&state
);
1315 error (_("canceled"));
1318 /* We intentionally make this result in a single breakpoint,
1319 contrary to what older versions of gdb did. The
1320 rationale is that this lets a user get the
1321 multiple_symbols_all behavior even with the 'ask'
1322 setting; and he can get separate breakpoints by entering
1323 "2-57" at the query. */
1324 do_cleanups (old_chain
);
1325 convert_results_to_lsals (self
, result
);
1330 if (num
>= VEC_length (const_char_ptr
, item_names
))
1331 printf_unfiltered (_("No choice number %d.\n"), num
);
1334 const char *elt
= VEC_index (const_char_ptr
, item_names
, num
);
1338 VEC_safe_push (const_char_ptr
, filters
, elt
);
1339 VEC_replace (const_char_ptr
, item_names
, num
, NULL
);
1343 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1349 filter_results (self
, result
, filters
);
1350 do_cleanups (old_chain
);
1355 /* The parser of linespec itself. */
1357 /* Throw an appropriate error when SYMBOL is not found (optionally in
1360 static void ATTRIBUTE_NORETURN
1361 symbol_not_found_error (const char *symbol
, const char *filename
)
1366 if (!have_full_symbols ()
1367 && !have_partial_symbols ()
1368 && !have_minimal_symbols ())
1369 throw_error (NOT_FOUND_ERROR
,
1370 _("No symbol table is loaded. Use the \"file\" command."));
1372 /* If SYMBOL starts with '$', the user attempted to either lookup
1373 a function/variable in his code starting with '$' or an internal
1374 variable of that name. Since we do not know which, be concise and
1375 explain both possibilities. */
1379 throw_error (NOT_FOUND_ERROR
,
1380 _("Undefined convenience variable or function \"%s\" "
1381 "not defined in \"%s\"."), symbol
, filename
);
1383 throw_error (NOT_FOUND_ERROR
,
1384 _("Undefined convenience variable or function \"%s\" "
1385 "not defined."), symbol
);
1390 throw_error (NOT_FOUND_ERROR
,
1391 _("Function \"%s\" not defined in \"%s\"."),
1394 throw_error (NOT_FOUND_ERROR
,
1395 _("Function \"%s\" not defined."), symbol
);
1399 /* Throw an appropriate error when an unexpected token is encountered
1402 static void ATTRIBUTE_NORETURN
1403 unexpected_linespec_error (linespec_parser
*parser
)
1405 linespec_token token
;
1406 static const char * token_type_strings
[]
1407 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1409 /* Get the token that generated the error. */
1410 token
= linespec_lexer_lex_one (parser
);
1412 /* Finally, throw the error. */
1413 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1414 || token
.type
== LSTOKEN_KEYWORD
)
1417 struct cleanup
*cleanup
;
1419 string
= copy_token_string (token
);
1420 cleanup
= make_cleanup (xfree
, string
);
1421 throw_error (GENERIC_ERROR
,
1422 _("malformed linespec error: unexpected %s, \"%s\""),
1423 token_type_strings
[token
.type
], string
);
1426 throw_error (GENERIC_ERROR
,
1427 _("malformed linespec error: unexpected %s"),
1428 token_type_strings
[token
.type
]);
1431 /* Parse and return a line offset in STRING. */
1433 static struct line_offset
1434 linespec_parse_line_offset (const char *string
)
1436 struct line_offset line_offset
= {0, LINE_OFFSET_NONE
};
1440 line_offset
.sign
= LINE_OFFSET_PLUS
;
1443 else if (*string
== '-')
1445 line_offset
.sign
= LINE_OFFSET_MINUS
;
1449 /* Right now, we only allow base 10 for offsets. */
1450 line_offset
.offset
= atoi (string
);
1454 /* Parse the basic_spec in PARSER's input. */
1457 linespec_parse_basic (linespec_parser
*parser
)
1460 linespec_token token
;
1461 VEC (symbolp
) *symbols
, *labels
;
1462 VEC (minsym_and_objfile_d
) *minimal_symbols
;
1463 struct cleanup
*cleanup
;
1465 /* Get the next token. */
1466 token
= linespec_lexer_lex_one (parser
);
1468 /* If it is EOI or KEYWORD, issue an error. */
1469 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1470 unexpected_linespec_error (parser
);
1471 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1472 else if (token
.type
== LSTOKEN_NUMBER
)
1474 /* Record the line offset and get the next token. */
1475 name
= copy_token_string (token
);
1476 cleanup
= make_cleanup (xfree
, name
);
1477 PARSER_RESULT (parser
)->line_offset
= linespec_parse_line_offset (name
);
1478 do_cleanups (cleanup
);
1480 /* Get the next token. */
1481 token
= linespec_lexer_consume_token (parser
);
1483 /* If the next token is a comma, stop parsing and return. */
1484 if (token
.type
== LSTOKEN_COMMA
)
1487 /* If the next token is anything but EOI or KEYWORD, issue
1489 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1490 unexpected_linespec_error (parser
);
1493 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1496 /* Next token must be LSTOKEN_STRING. */
1497 if (token
.type
!= LSTOKEN_STRING
)
1498 unexpected_linespec_error (parser
);
1500 /* The current token will contain the name of a function, method,
1502 name
= copy_token_string (token
);
1503 cleanup
= make_cleanup (xfree
, name
);
1505 /* Try looking it up as a function/method. */
1506 find_linespec_symbols (PARSER_STATE (parser
),
1507 PARSER_RESULT (parser
)->file_symtabs
, name
,
1508 &symbols
, &minimal_symbols
);
1510 if (symbols
!= NULL
|| minimal_symbols
!= NULL
)
1512 PARSER_RESULT (parser
)->function_symbols
= symbols
;
1513 PARSER_RESULT (parser
)->minimal_symbols
= minimal_symbols
;
1514 PARSER_RESULT (parser
)->function_name
= name
;
1516 discard_cleanups (cleanup
);
1520 /* NAME was not a function or a method. So it must be a label
1522 labels
= find_label_symbols (PARSER_STATE (parser
), NULL
,
1526 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1527 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1528 PARSER_RESULT (parser
)->label_name
= name
;
1530 discard_cleanups (cleanup
);
1534 /* The name is also not a label. Abort parsing. Do not throw
1535 an error here. parse_linespec will do it for us. */
1537 /* Save a copy of the name we were trying to lookup. */
1538 PARSER_RESULT (parser
)->function_name
= name
;
1539 discard_cleanups (cleanup
);
1544 /* Get the next token. */
1545 token
= linespec_lexer_consume_token (parser
);
1547 if (token
.type
== LSTOKEN_COLON
)
1549 /* User specified a label or a lineno. */
1550 token
= linespec_lexer_consume_token (parser
);
1552 if (token
.type
== LSTOKEN_NUMBER
)
1554 /* User specified an offset. Record the line offset and
1555 get the next token. */
1556 name
= copy_token_string (token
);
1557 cleanup
= make_cleanup (xfree
, name
);
1558 PARSER_RESULT (parser
)->line_offset
1559 = linespec_parse_line_offset (name
);
1560 do_cleanups (cleanup
);
1562 /* Ge the next token. */
1563 token
= linespec_lexer_consume_token (parser
);
1565 else if (token
.type
== LSTOKEN_STRING
)
1567 /* Grab a copy of the label's name and look it up. */
1568 name
= copy_token_string (token
);
1569 cleanup
= make_cleanup (xfree
, name
);
1570 labels
= find_label_symbols (PARSER_STATE (parser
),
1571 PARSER_RESULT (parser
)->function_symbols
,
1576 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1577 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1578 PARSER_RESULT (parser
)->label_name
= name
;
1580 discard_cleanups (cleanup
);
1584 /* We don't know what it was, but it isn't a label. */
1585 throw_error (NOT_FOUND_ERROR
,
1586 _("No label \"%s\" defined in function \"%s\"."),
1587 name
, PARSER_RESULT (parser
)->function_name
);
1590 /* Check for a line offset. */
1591 token
= linespec_lexer_consume_token (parser
);
1592 if (token
.type
== LSTOKEN_COLON
)
1594 /* Get the next token. */
1595 token
= linespec_lexer_consume_token (parser
);
1597 /* It must be a line offset. */
1598 if (token
.type
!= LSTOKEN_NUMBER
)
1599 unexpected_linespec_error (parser
);
1601 /* Record the lione offset and get the next token. */
1602 name
= copy_token_string (token
);
1603 cleanup
= make_cleanup (xfree
, name
);
1605 PARSER_RESULT (parser
)->line_offset
1606 = linespec_parse_line_offset (name
);
1607 do_cleanups (cleanup
);
1609 /* Get the next token. */
1610 token
= linespec_lexer_consume_token (parser
);
1615 /* Trailing ':' in the input. Issue an error. */
1616 unexpected_linespec_error (parser
);
1621 /* Canonicalize the linespec contained in LS. The result is saved into
1622 STATE->canonical. */
1625 canonicalize_linespec (struct linespec_state
*state
, linespec_p ls
)
1627 /* If canonicalization was not requested, no need to do anything. */
1628 if (!state
->canonical
)
1631 /* Shortcut expressions, which can only appear by themselves. */
1632 if (ls
->expression
!= NULL
)
1633 state
->canonical
->addr_string
= xstrdup (ls
->expression
);
1636 struct ui_file
*buf
;
1639 buf
= mem_fileopen ();
1640 if (ls
->source_filename
)
1642 fputs_unfiltered (ls
->source_filename
, buf
);
1646 if (ls
->function_name
)
1649 fputc_unfiltered (':', buf
);
1650 fputs_unfiltered (ls
->function_name
, buf
);
1657 fputc_unfiltered (':', buf
);
1659 if (ls
->function_name
== NULL
)
1663 /* No function was specified, so add the symbol name. */
1664 gdb_assert (ls
->labels
.function_symbols
!= NULL
1665 && (VEC_length (symbolp
, ls
->labels
.function_symbols
)
1667 s
= VEC_index (symbolp
, ls
->labels
.function_symbols
, 0);
1668 fputs_unfiltered (SYMBOL_NATURAL_NAME (s
), buf
);
1669 fputc_unfiltered (':', buf
);
1672 fputs_unfiltered (ls
->label_name
, buf
);
1674 state
->canonical
->special_display
= 1;
1677 if (ls
->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
1680 fputc_unfiltered (':', buf
);
1681 fprintf_filtered (buf
, "%s%d",
1682 (ls
->line_offset
.sign
== LINE_OFFSET_NONE
? ""
1683 : (ls
->line_offset
.sign
1684 == LINE_OFFSET_PLUS
? "+" : "-")),
1685 ls
->line_offset
.offset
);
1688 state
->canonical
->addr_string
= ui_file_xstrdup (buf
, NULL
);
1689 ui_file_delete (buf
);
1693 /* Given a line offset in LS, construct the relevant SALs. */
1695 static struct symtabs_and_lines
1696 create_sals_line_offset (struct linespec_state
*self
,
1699 struct symtabs_and_lines values
;
1700 struct symtab_and_line val
;
1701 int use_default
= 0;
1707 /* This is where we need to make sure we have good defaults.
1708 We must guarantee that this section of code is never executed
1709 when we are called with just a function anme, since
1710 set_default_source_symtab_and_line uses
1711 select_source_symtab that calls us with such an argument. */
1713 if (VEC_length (symtab_p
, ls
->file_symtabs
) == 1
1714 && VEC_index (symtab_p
, ls
->file_symtabs
, 0) == NULL
)
1716 set_current_program_space (self
->program_space
);
1718 /* Make sure we have at least a default source line. */
1719 set_default_source_symtab_and_line ();
1720 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
1721 VEC_pop (symtab_p
, ls
->file_symtabs
);
1722 VEC_free (symtab_p
, ls
->file_symtabs
);
1724 = collect_symtabs_from_filename (self
->default_symtab
->filename
);
1728 val
.line
= ls
->line_offset
.offset
;
1729 switch (ls
->line_offset
.sign
)
1731 case LINE_OFFSET_PLUS
:
1732 if (ls
->line_offset
.offset
== 0)
1735 val
.line
= self
->default_line
+ val
.line
;
1738 case LINE_OFFSET_MINUS
:
1739 if (ls
->line_offset
.offset
== 0)
1742 val
.line
= self
->default_line
- val
.line
;
1744 val
.line
= -val
.line
;
1747 case LINE_OFFSET_NONE
:
1748 break; /* No need to adjust val.line. */
1751 if (self
->list_mode
)
1752 decode_digits_list_mode (self
, ls
, &values
, val
);
1755 struct linetable_entry
*best_entry
= NULL
;
1757 struct block
**blocks
;
1758 struct cleanup
*cleanup
;
1759 struct symtabs_and_lines intermediate_results
;
1762 intermediate_results
.sals
= NULL
;
1763 intermediate_results
.nelts
= 0;
1765 decode_digits_ordinary (self
, ls
, val
.line
, &intermediate_results
,
1767 if (intermediate_results
.nelts
== 0 && best_entry
!= NULL
)
1768 decode_digits_ordinary (self
, ls
, best_entry
->line
,
1769 &intermediate_results
, &best_entry
);
1771 cleanup
= make_cleanup (xfree
, intermediate_results
.sals
);
1773 /* For optimized code, the compiler can scatter one source line
1774 across disjoint ranges of PC values, even when no duplicate
1775 functions or inline functions are involved. For example,
1776 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
1777 function can result in two PC ranges. In this case, we don't
1778 want to set a breakpoint on the first PC of each range. To filter
1779 such cases, we use containing blocks -- for each PC found
1780 above, we see if there are other PCs that are in the same
1781 block. If yes, the other PCs are filtered out. */
1783 filter
= XNEWVEC (int, intermediate_results
.nelts
);
1784 make_cleanup (xfree
, filter
);
1785 blocks
= XNEWVEC (struct block
*, intermediate_results
.nelts
);
1786 make_cleanup (xfree
, blocks
);
1788 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1790 set_current_program_space (intermediate_results
.sals
[i
].pspace
);
1793 blocks
[i
] = block_for_pc_sect (intermediate_results
.sals
[i
].pc
,
1794 intermediate_results
.sals
[i
].section
);
1797 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1799 if (blocks
[i
] != NULL
)
1800 for (j
= i
+ 1; j
< intermediate_results
.nelts
; ++j
)
1802 if (blocks
[j
] == blocks
[i
])
1810 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1813 struct symbol
*sym
= (blocks
[i
]
1814 ? block_containing_function (blocks
[i
])
1817 if (self
->funfirstline
)
1818 skip_prologue_sal (&intermediate_results
.sals
[i
]);
1819 /* Make sure the line matches the request, not what was
1821 intermediate_results
.sals
[i
].line
= val
.line
;
1822 add_sal_to_sals (self
, &values
, &intermediate_results
.sals
[i
],
1823 sym
? SYMBOL_NATURAL_NAME (sym
) : NULL
, 0);
1826 do_cleanups (cleanup
);
1829 if (values
.nelts
== 0)
1831 if (ls
->source_filename
)
1832 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
1833 val
.line
, ls
->source_filename
);
1835 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
1842 /* Create and return SALs from the linespec LS. */
1844 static struct symtabs_and_lines
1845 convert_linespec_to_sals (struct linespec_state
*state
, linespec_p ls
)
1847 struct symtabs_and_lines sals
= {NULL
, 0};
1849 if (ls
->expression
!= NULL
)
1851 struct symtab_and_line sal
;
1853 /* We have an expression. No other attribute is allowed. */
1854 sal
= find_pc_line (ls
->expr_pc
, 0);
1855 sal
.pc
= ls
->expr_pc
;
1856 sal
.section
= find_pc_overlay (ls
->expr_pc
);
1857 sal
.explicit_pc
= 1;
1858 add_sal_to_sals (state
, &sals
, &sal
, ls
->expression
, 1);
1860 else if (ls
->labels
.label_symbols
!= NULL
)
1862 /* We have just a bunch of functions/methods or labels. */
1864 struct symtab_and_line sal
;
1867 for (i
= 0; VEC_iterate (symbolp
, ls
->labels
.label_symbols
, i
, sym
); ++i
)
1869 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
))
1870 add_sal_to_sals (state
, &sals
, &sal
,
1871 SYMBOL_NATURAL_NAME (sym
), 0);
1874 else if (ls
->function_symbols
!= NULL
|| ls
->minimal_symbols
!= NULL
)
1876 /* We have just a bunch of functions and/or methods. */
1878 struct symtab_and_line sal
;
1880 minsym_and_objfile_d
*elem
;
1881 struct program_space
*pspace
;
1883 if (ls
->function_symbols
!= NULL
)
1885 /* Sort symbols so that symbols with the same program space are next
1887 qsort (VEC_address (symbolp
, ls
->function_symbols
),
1888 VEC_length (symbolp
, ls
->function_symbols
),
1889 sizeof (symbolp
), compare_symbols
);
1891 for (i
= 0; VEC_iterate (symbolp
, ls
->function_symbols
, i
, sym
); ++i
)
1893 pspace
= SYMTAB_PSPACE (SYMBOL_SYMTAB (sym
));
1894 set_current_program_space (pspace
);
1895 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
1896 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
1897 add_sal_to_sals (state
, &sals
, &sal
,
1898 SYMBOL_NATURAL_NAME (sym
), 0);
1902 if (ls
->minimal_symbols
!= NULL
)
1904 /* Sort minimal symbols by program space, too. */
1905 qsort (VEC_address (minsym_and_objfile_d
, ls
->minimal_symbols
),
1906 VEC_length (minsym_and_objfile_d
, ls
->minimal_symbols
),
1907 sizeof (minsym_and_objfile_d
), compare_msymbols
);
1910 VEC_iterate (minsym_and_objfile_d
, ls
->minimal_symbols
, i
, elem
);
1913 pspace
= elem
->objfile
->pspace
;
1914 set_current_program_space (pspace
);
1915 minsym_found (state
, elem
->objfile
, elem
->minsym
, &sals
);
1919 else if (ls
->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
1921 /* Only an offset was specified. */
1922 sals
= create_sals_line_offset (state
, ls
);
1924 /* Make sure we have a filename for canonicalization. */
1925 if (ls
->source_filename
== NULL
)
1926 ls
->source_filename
= xstrdup (state
->default_symtab
->filename
);
1930 /* We haven't found any results... */
1934 canonicalize_linespec (state
, ls
);
1936 if (sals
.nelts
> 0 && state
->canonical
!= NULL
)
1937 state
->canonical
->pre_expanded
= 1;
1942 /* Parse a string that specifies a linespec.
1943 Pass the address of a char * variable; that variable will be
1944 advanced over the characters actually parsed.
1946 The basic grammar of linespecs:
1948 linespec -> expr_spec | var_spec | basic_spec
1949 expr_spec -> '*' STRING
1950 var_spec -> '$' (STRING | NUMBER)
1952 basic_spec -> file_offset_spec | function_spec | label_spec
1953 file_offset_spec -> opt_file_spec offset_spec
1954 function_spec -> opt_file_spec function_name_spec opt_label_spec
1955 label_spec -> label_name_spec
1957 opt_file_spec -> "" | file_name_spec ':'
1958 opt_label_spec -> "" | ':' label_name_spec
1960 file_name_spec -> STRING
1961 function_name_spec -> STRING
1962 label_name_spec -> STRING
1963 function_name_spec -> STRING
1964 offset_spec -> NUMBER
1968 This may all be followed by several keywords such as "if EXPR",
1971 A comma will terminate parsing.
1973 The function may be an undebuggable function found in minimal symbol table.
1975 If the argument FUNFIRSTLINE is nonzero, we want the first line
1976 of real code inside a function when a function is specified, and it is
1977 not OK to specify a variable or type to get its line number.
1979 DEFAULT_SYMTAB specifies the file to use if none is specified.
1980 It defaults to current_source_symtab.
1981 DEFAULT_LINE specifies the line number to use for relative
1982 line numbers (that start with signs). Defaults to current_source_line.
1983 If CANONICAL is non-NULL, store an array of strings containing the canonical
1984 line specs there if necessary. Currently overloaded member functions and
1985 line numbers or static functions without a filename yield a canonical
1986 line spec. The array and the line spec strings are allocated on the heap,
1987 it is the callers responsibility to free them.
1989 Note that it is possible to return zero for the symtab
1990 if no file is validly specified. Callers must check that.
1991 Also, the line number returned may be invalid. */
1993 /* Parse the linespec in ARGPTR. */
1995 static struct symtabs_and_lines
1996 parse_linespec (linespec_parser
*parser
, char **argptr
)
1998 linespec_token token
;
1999 struct symtabs_and_lines values
;
2000 volatile struct gdb_exception file_exception
;
2001 struct cleanup
*cleanup
;
2003 /* A special case to start. It has become quite popular for
2004 IDEs to work around bugs in the previous parser by quoting
2005 the entire linespec, so we attempt to deal with this nicely. */
2006 parser
->is_quote_enclosed
= 0;
2007 if (!is_ada_operator (*argptr
)
2008 && strchr (linespec_quote_characters
, **argptr
) != NULL
)
2012 end
= skip_quote_char (*argptr
+ 1, **argptr
);
2013 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2015 /* Here's the special case. Skip ARGPTR past the initial
2018 parser
->is_quote_enclosed
= 1;
2022 parser
->lexer
.saved_arg
= *argptr
;
2023 parser
->lexer
.stream
= argptr
;
2024 file_exception
.reason
= 0;
2026 /* Initialize the default symtab and line offset. */
2027 initialize_defaults (&PARSER_STATE (parser
)->default_symtab
,
2028 &PARSER_STATE (parser
)->default_line
);
2030 /* Objective-C shortcut. */
2031 values
= decode_objc (PARSER_STATE (parser
), PARSER_RESULT (parser
), argptr
);
2032 if (values
.sals
!= NULL
)
2035 /* Start parsing. */
2037 /* Get the first token. */
2038 token
= linespec_lexer_lex_one (parser
);
2040 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2041 if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '*')
2045 /* User specified an expression, *EXPR. */
2046 copy
= expr
= copy_token_string (token
);
2047 cleanup
= make_cleanup (xfree
, expr
);
2048 PARSER_RESULT (parser
)->expr_pc
= linespec_expression_to_pc (©
);
2049 discard_cleanups (cleanup
);
2050 PARSER_RESULT (parser
)->expression
= expr
;
2052 /* This is a little hacky/tricky. If linespec_expression_to_pc
2053 did not evaluate the entire token, then we must find the
2054 string COPY inside the original token buffer. */
2057 PARSER_STREAM (parser
) = strstr (parser
->lexer
.saved_arg
, copy
);
2058 gdb_assert (PARSER_STREAM (parser
) != NULL
);
2061 /* Consume the token. */
2062 linespec_lexer_consume_token (parser
);
2064 goto convert_to_sals
;
2066 else if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '$')
2070 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2071 VEC_safe_push (symtab_p
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2073 /* User specified a convenience variable or history value. */
2074 var
= copy_token_string (token
);
2075 cleanup
= make_cleanup (xfree
, var
);
2076 PARSER_RESULT (parser
)->line_offset
2077 = linespec_parse_variable (PARSER_STATE (parser
), var
);
2079 /* If a line_offset wasn't found (VAR is the name of a user
2080 variable/function), then skip to normal symbol processing. */
2081 if (PARSER_RESULT (parser
)->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2083 discard_cleanups (cleanup
);
2085 /* Consume this token. */
2086 linespec_lexer_consume_token (parser
);
2088 goto convert_to_sals
;
2091 do_cleanups (cleanup
);
2093 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2094 unexpected_linespec_error (parser
);
2096 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2097 this token cannot represent a filename. */
2098 token
= linespec_lexer_peek_token (parser
);
2100 if (token
.type
== LSTOKEN_COLON
)
2102 char *user_filename
;
2104 /* Get the current token again and extract the filename. */
2105 token
= linespec_lexer_lex_one (parser
);
2106 user_filename
= copy_token_string (token
);
2108 /* Check if the input is a filename. */
2109 TRY_CATCH (file_exception
, RETURN_MASK_ERROR
)
2111 PARSER_RESULT (parser
)->file_symtabs
2112 = symtabs_from_filename (user_filename
);
2115 if (file_exception
.reason
>= 0)
2117 /* Symtabs were found for the file. Record the filename. */
2118 PARSER_RESULT (parser
)->source_filename
= user_filename
;
2120 /* Get the next token. */
2121 token
= linespec_lexer_consume_token (parser
);
2123 /* This is LSTOKEN_COLON; consume it. */
2124 linespec_lexer_consume_token (parser
);
2128 /* No symtabs found -- discard user_filename. */
2129 xfree (user_filename
);
2131 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2132 VEC_safe_push (symtab_p
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2135 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2136 else if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2137 && token
.type
!= LSTOKEN_COMMA
)
2139 /* TOKEN is the _next_ token, not the one currently in the parser.
2140 Consuming the token will give the correct error message. */
2141 linespec_lexer_consume_token (parser
);
2142 unexpected_linespec_error (parser
);
2146 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2147 VEC_safe_push (symtab_p
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2150 /* Parse the rest of the linespec. */
2151 linespec_parse_basic (parser
);
2153 if (PARSER_RESULT (parser
)->function_symbols
== NULL
2154 && PARSER_RESULT (parser
)->labels
.label_symbols
== NULL
2155 && PARSER_RESULT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
2156 && PARSER_RESULT (parser
)->minimal_symbols
== NULL
)
2158 /* The linespec didn't parse. Re-throw the file exception if
2160 if (file_exception
.reason
< 0)
2161 throw_exception (file_exception
);
2163 /* Otherwise, the symbol is not found. */
2164 symbol_not_found_error (PARSER_RESULT (parser
)->function_name
,
2165 PARSER_RESULT (parser
)->source_filename
);
2170 /* Get the last token and record how much of the input was parsed,
2172 token
= linespec_lexer_lex_one (parser
);
2173 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2174 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (token
).ptr
;
2176 /* Convert the data in PARSER_RESULT to SALs. */
2177 values
= convert_linespec_to_sals (PARSER_STATE (parser
),
2178 PARSER_RESULT (parser
));
2184 /* A constructor for linespec_state. */
2187 linespec_state_constructor (struct linespec_state
*self
,
2188 int flags
, const struct language_defn
*language
,
2189 struct symtab
*default_symtab
,
2191 struct linespec_result
*canonical
)
2193 memset (self
, 0, sizeof (*self
));
2194 self
->language
= language
;
2195 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2196 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2197 self
->default_symtab
= default_symtab
;
2198 self
->default_line
= default_line
;
2199 self
->canonical
= canonical
;
2200 self
->program_space
= current_program_space
;
2201 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2202 xfree
, xcalloc
, xfree
);
2205 /* Initialize a new linespec parser. */
2208 linespec_parser_new (linespec_parser
*parser
,
2209 int flags
, const struct language_defn
*language
,
2210 struct symtab
*default_symtab
,
2212 struct linespec_result
*canonical
)
2214 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
2215 memset (PARSER_RESULT (parser
), 0, sizeof (struct linespec
));
2216 PARSER_RESULT (parser
)->line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2217 linespec_state_constructor (PARSER_STATE (parser
), flags
, language
,
2218 default_symtab
, default_line
, canonical
);
2221 /* A destructor for linespec_state. */
2224 linespec_state_destructor (struct linespec_state
*self
)
2226 htab_delete (self
->addr_set
);
2229 /* Delete a linespec parser. */
2232 linespec_parser_delete (void *arg
)
2234 linespec_parser
*parser
= (linespec_parser
*) arg
;
2236 xfree ((char *) PARSER_RESULT (parser
)->expression
);
2237 xfree ((char *) PARSER_RESULT (parser
)->source_filename
);
2238 xfree ((char *) PARSER_RESULT (parser
)->label_name
);
2239 xfree ((char *) PARSER_RESULT (parser
)->function_name
);
2241 if (PARSER_RESULT (parser
)->file_symtabs
!= NULL
)
2242 VEC_free (symtab_p
, PARSER_RESULT (parser
)->file_symtabs
);
2244 if (PARSER_RESULT (parser
)->function_symbols
!= NULL
)
2245 VEC_free (symbolp
, PARSER_RESULT (parser
)->function_symbols
);
2247 if (PARSER_RESULT (parser
)->minimal_symbols
!= NULL
)
2248 VEC_free (minsym_and_objfile_d
, PARSER_RESULT (parser
)->minimal_symbols
);
2250 if (PARSER_RESULT (parser
)->labels
.label_symbols
!= NULL
)
2251 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.label_symbols
);
2253 if (PARSER_RESULT (parser
)->labels
.function_symbols
!= NULL
)
2254 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.function_symbols
);
2256 linespec_state_destructor (PARSER_STATE (parser
));
2259 /* See linespec.h. */
2262 decode_line_full (char **argptr
, int flags
,
2263 struct symtab
*default_symtab
,
2264 int default_line
, struct linespec_result
*canonical
,
2265 const char *select_mode
,
2268 struct symtabs_and_lines result
;
2269 struct cleanup
*cleanups
;
2270 char *arg_start
= *argptr
;
2271 VEC (const_char_ptr
) *filters
= NULL
;
2272 linespec_parser parser
;
2273 struct linespec_state
*state
;
2275 gdb_assert (canonical
!= NULL
);
2276 /* The filter only makes sense for 'all'. */
2277 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
2278 gdb_assert (select_mode
== NULL
2279 || select_mode
== multiple_symbols_all
2280 || select_mode
== multiple_symbols_ask
2281 || select_mode
== multiple_symbols_cancel
);
2282 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
2284 linespec_parser_new (&parser
, flags
, current_language
, default_symtab
,
2285 default_line
, canonical
);
2286 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
2287 save_current_program_space ();
2289 result
= parse_linespec (&parser
, argptr
);
2290 state
= PARSER_STATE (&parser
);
2292 gdb_assert (result
.nelts
== 1 || canonical
->pre_expanded
);
2293 gdb_assert (canonical
->addr_string
!= NULL
);
2294 canonical
->pre_expanded
= 1;
2296 /* Arrange for allocated canonical names to be freed. */
2297 if (result
.nelts
> 0)
2301 make_cleanup (xfree
, state
->canonical_names
);
2302 for (i
= 0; i
< result
.nelts
; ++i
)
2304 gdb_assert (state
->canonical_names
[i
] != NULL
);
2305 make_cleanup (xfree
, state
->canonical_names
[i
]);
2309 if (select_mode
== NULL
)
2311 if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
2312 select_mode
= multiple_symbols_all
;
2314 select_mode
= multiple_symbols_select_mode ();
2317 if (select_mode
== multiple_symbols_all
)
2321 make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
2322 VEC_safe_push (const_char_ptr
, filters
, filter
);
2323 filter_results (state
, &result
, filters
);
2326 convert_results_to_lsals (state
, &result
);
2329 decode_line_2 (state
, &result
, select_mode
);
2331 do_cleanups (cleanups
);
2334 /* See linespec.h. */
2336 struct symtabs_and_lines
2337 decode_line_1 (char **argptr
, int flags
,
2338 struct symtab
*default_symtab
,
2341 struct symtabs_and_lines result
;
2342 linespec_parser parser
;
2343 struct cleanup
*cleanups
;
2345 linespec_parser_new (&parser
, flags
, current_language
, default_symtab
,
2346 default_line
, NULL
);
2347 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
2348 save_current_program_space ();
2350 result
= parse_linespec (&parser
, argptr
);
2352 do_cleanups (cleanups
);
2356 /* See linespec.h. */
2358 struct symtabs_and_lines
2359 decode_line_with_current_source (char *string
, int flags
)
2361 struct symtabs_and_lines sals
;
2362 struct symtab_and_line cursal
;
2365 error (_("Empty line specification."));
2367 /* We use whatever is set as the current source line. We do not try
2368 and get a default source symtab+line or it will recursively call us! */
2369 cursal
= get_current_source_symtab_and_line ();
2371 sals
= decode_line_1 (&string
, flags
,
2372 cursal
.symtab
, cursal
.line
);
2375 error (_("Junk at end of line specification: %s"), string
);
2379 /* See linespec.h. */
2381 struct symtabs_and_lines
2382 decode_line_with_last_displayed (char *string
, int flags
)
2384 struct symtabs_and_lines sals
;
2387 error (_("Empty line specification."));
2389 if (last_displayed_sal_is_valid ())
2390 sals
= decode_line_1 (&string
, flags
,
2391 get_last_displayed_symtab (),
2392 get_last_displayed_line ());
2394 sals
= decode_line_1 (&string
, flags
, (struct symtab
*) NULL
, 0);
2397 error (_("Junk at end of line specification: %s"), string
);
2403 /* First, some functions to initialize stuff at the beggining of the
2407 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
2409 if (*default_symtab
== 0)
2411 /* Use whatever we have for the default source line. We don't use
2412 get_current_or_default_symtab_and_line as it can recurse and call
2414 struct symtab_and_line cursal
=
2415 get_current_source_symtab_and_line ();
2417 *default_symtab
= cursal
.symtab
;
2418 *default_line
= cursal
.line
;
2424 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2425 advancing EXP_PTR past any parsed text. */
2428 linespec_expression_to_pc (char **exp_ptr
)
2430 if (current_program_space
->executing_startup
)
2431 /* The error message doesn't really matter, because this case
2432 should only hit during breakpoint reset. */
2433 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
2434 "program space is in startup"));
2437 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
2442 /* Here's where we recognise an Objective-C Selector. An Objective C
2443 selector may be implemented by more than one class, therefore it
2444 may represent more than one method/function. This gives us a
2445 situation somewhat analogous to C++ overloading. If there's more
2446 than one method that could represent the selector, then use some of
2447 the existing C++ code to let the user choose one. */
2449 static struct symtabs_and_lines
2450 decode_objc (struct linespec_state
*self
, linespec_p ls
, char **argptr
)
2452 struct collect_info info
;
2453 VEC (const_char_ptr
) *symbol_names
= NULL
;
2454 struct symtabs_and_lines values
;
2456 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
2460 info
.file_symtabs
= NULL
;
2461 VEC_safe_push (symtab_p
, info
.file_symtabs
, NULL
);
2462 make_cleanup (VEC_cleanup (symtab_p
), &info
.file_symtabs
);
2463 info
.result
.symbols
= NULL
;
2464 info
.result
.minimal_symbols
= NULL
;
2468 new_argptr
= find_imps (*argptr
, &symbol_names
);
2469 if (VEC_empty (const_char_ptr
, symbol_names
))
2471 do_cleanups (cleanup
);
2475 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
);
2477 if (!VEC_empty (symbolp
, info
.result
.symbols
)
2478 || !VEC_empty (minsym_and_objfile_d
, info
.result
.minimal_symbols
))
2482 saved_arg
= alloca (new_argptr
- *argptr
+ 1);
2483 memcpy (saved_arg
, *argptr
, new_argptr
- *argptr
);
2484 saved_arg
[new_argptr
- *argptr
] = '\0';
2486 ls
->function_name
= xstrdup (saved_arg
);
2487 ls
->function_symbols
= info
.result
.symbols
;
2488 ls
->minimal_symbols
= info
.result
.minimal_symbols
;
2489 values
= convert_linespec_to_sals (self
, ls
);
2491 if (self
->canonical
)
2493 self
->canonical
->pre_expanded
= 1;
2494 if (ls
->source_filename
)
2495 self
->canonical
->addr_string
2496 = xstrprintf ("%s:%s", ls
->source_filename
, saved_arg
);
2498 self
->canonical
->addr_string
= xstrdup (saved_arg
);
2502 *argptr
= new_argptr
;
2504 do_cleanups (cleanup
);
2509 /* An instance of this type is used when collecting prefix symbols for
2512 struct decode_compound_collector
2514 /* The result vector. */
2515 VEC (symbolp
) *symbols
;
2517 /* A hash table of all symbols we found. We use this to avoid
2518 adding any symbol more than once. */
2522 /* A callback for iterate_over_symbols that is used by
2523 lookup_prefix_sym to collect type symbols. */
2526 collect_one_symbol (struct symbol
*sym
, void *d
)
2528 struct decode_compound_collector
*collector
= d
;
2532 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
2533 return 1; /* Continue iterating. */
2535 t
= SYMBOL_TYPE (sym
);
2537 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2538 && TYPE_CODE (t
) != TYPE_CODE_UNION
2539 && TYPE_CODE (t
) != TYPE_CODE_NAMESPACE
)
2540 return 1; /* Continue iterating. */
2542 slot
= htab_find_slot (collector
->unique_syms
, sym
, INSERT
);
2546 VEC_safe_push (symbolp
, collector
->symbols
, sym
);
2549 return 1; /* Continue iterating. */
2552 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
2554 static VEC (symbolp
) *
2555 lookup_prefix_sym (struct linespec_state
*state
, VEC (symtab_p
) *file_symtabs
,
2556 const char *class_name
)
2560 struct decode_compound_collector collector
;
2561 struct cleanup
*outer
;
2562 struct cleanup
*cleanup
;
2564 collector
.symbols
= NULL
;
2565 outer
= make_cleanup (VEC_cleanup (symbolp
), &collector
.symbols
);
2567 collector
.unique_syms
= htab_create_alloc (1, htab_hash_pointer
,
2568 htab_eq_pointer
, NULL
,
2570 cleanup
= make_cleanup_htab_delete (collector
.unique_syms
);
2572 for (ix
= 0; VEC_iterate (symtab_p
, file_symtabs
, ix
, elt
); ++ix
)
2576 iterate_over_all_matching_symtabs (state
, class_name
, STRUCT_DOMAIN
,
2577 collect_one_symbol
, &collector
,
2579 iterate_over_all_matching_symtabs (state
, class_name
, VAR_DOMAIN
,
2580 collect_one_symbol
, &collector
,
2585 struct block
*search_block
;
2587 /* Program spaces that are executing startup should have
2588 been filtered out earlier. */
2589 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
2590 set_current_program_space (SYMTAB_PSPACE (elt
));
2591 search_block
= get_search_block (elt
);
2592 LA_ITERATE_OVER_SYMBOLS (search_block
, class_name
, STRUCT_DOMAIN
,
2593 collect_one_symbol
, &collector
);
2594 LA_ITERATE_OVER_SYMBOLS (search_block
, class_name
, VAR_DOMAIN
,
2595 collect_one_symbol
, &collector
);
2599 do_cleanups (cleanup
);
2600 discard_cleanups (outer
);
2601 return collector
.symbols
;
2604 /* A qsort comparison function for symbols. The resulting order does
2605 not actually matter; we just need to be able to sort them so that
2606 symbols with the same program space end up next to each other. */
2609 compare_symbols (const void *a
, const void *b
)
2611 struct symbol
* const *sa
= a
;
2612 struct symbol
* const *sb
= b
;
2615 uia
= (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sa
));
2616 uib
= (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sb
));
2623 uia
= (uintptr_t) *sa
;
2624 uib
= (uintptr_t) *sb
;
2634 /* Like compare_symbols but for minimal symbols. */
2637 compare_msymbols (const void *a
, const void *b
)
2639 const struct minsym_and_objfile
*sa
= a
;
2640 const struct minsym_and_objfile
*sb
= b
;
2643 uia
= (uintptr_t) sa
->objfile
->pspace
;
2644 uib
= (uintptr_t) sa
->objfile
->pspace
;
2651 uia
= (uintptr_t) sa
->minsym
;
2652 uib
= (uintptr_t) sb
->minsym
;
2662 /* Look for all the matching instances of each symbol in NAMES. Only
2663 instances from PSPACE are considered; other program spaces are
2664 handled by our caller. If PSPACE is NULL, then all program spaces
2665 are considered. Results are stored into INFO. */
2668 add_all_symbol_names_from_pspace (struct collect_info
*info
,
2669 struct program_space
*pspace
,
2670 VEC (const_char_ptr
) *names
)
2675 for (ix
= 0; VEC_iterate (const_char_ptr
, names
, ix
, iter
); ++ix
)
2676 add_matching_symbols_to_info (iter
, info
, pspace
);
2680 find_superclass_methods (VEC (typep
) *superclasses
,
2682 VEC (const_char_ptr
) **result_names
)
2684 int old_len
= VEC_length (const_char_ptr
, *result_names
);
2685 VEC (typep
) *iter_classes
;
2686 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
2688 iter_classes
= superclasses
;
2691 VEC (typep
) *new_supers
= NULL
;
2695 make_cleanup (VEC_cleanup (typep
), &new_supers
);
2696 for (ix
= 0; VEC_iterate (typep
, iter_classes
, ix
, t
); ++ix
)
2697 find_methods (t
, name
, result_names
, &new_supers
);
2699 if (VEC_length (const_char_ptr
, *result_names
) != old_len
2700 || VEC_empty (typep
, new_supers
))
2703 iter_classes
= new_supers
;
2706 do_cleanups (cleanup
);
2709 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
2710 given by one of the symbols in SYM_CLASSES. Matches are returned
2711 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
2714 find_method (struct linespec_state
*self
, VEC (symtab_p
) *file_symtabs
,
2715 const char *class_name
, const char *method_name
,
2716 VEC (symbolp
) *sym_classes
, VEC (symbolp
) **symbols
,
2717 VEC (minsym_and_objfile_d
) **minsyms
)
2720 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
2722 int last_result_len
;
2723 VEC (typep
) *superclass_vec
;
2724 VEC (const_char_ptr
) *result_names
;
2725 struct collect_info info
;
2727 /* Sort symbols so that symbols with the same program space are next
2729 qsort (VEC_address (symbolp
, sym_classes
),
2730 VEC_length (symbolp
, sym_classes
),
2735 info
.file_symtabs
= file_symtabs
;
2736 info
.result
.symbols
= NULL
;
2737 info
.result
.minimal_symbols
= NULL
;
2739 /* Iterate over all the types, looking for the names of existing
2740 methods matching METHOD_NAME. If we cannot find a direct method in a
2741 given program space, then we consider inherited methods; this is
2742 not ideal (ideal would be to respect C++ hiding rules), but it
2743 seems good enough and is what GDB has historically done. We only
2744 need to collect the names because later we find all symbols with
2745 those names. This loop is written in a somewhat funny way
2746 because we collect data across the program space before deciding
2748 superclass_vec
= NULL
;
2749 make_cleanup (VEC_cleanup (typep
), &superclass_vec
);
2750 result_names
= NULL
;
2751 make_cleanup (VEC_cleanup (const_char_ptr
), &result_names
);
2752 last_result_len
= 0;
2753 for (ix
= 0; VEC_iterate (symbolp
, sym_classes
, ix
, sym
); ++ix
)
2756 struct program_space
*pspace
;
2758 /* Program spaces that are executing startup should have
2759 been filtered out earlier. */
2760 gdb_assert (!SYMTAB_PSPACE (SYMBOL_SYMTAB (sym
))->executing_startup
);
2761 pspace
= SYMTAB_PSPACE (SYMBOL_SYMTAB (sym
));
2762 set_current_program_space (pspace
);
2763 t
= check_typedef (SYMBOL_TYPE (sym
));
2764 find_methods (t
, method_name
, &result_names
, &superclass_vec
);
2766 /* Handle all items from a single program space at once; and be
2767 sure not to miss the last batch. */
2768 if (ix
== VEC_length (symbolp
, sym_classes
) - 1
2770 != SYMTAB_PSPACE (SYMBOL_SYMTAB (VEC_index (symbolp
, sym_classes
,
2773 /* If we did not find a direct implementation anywhere in
2774 this program space, consider superclasses. */
2775 if (VEC_length (const_char_ptr
, result_names
) == last_result_len
)
2776 find_superclass_methods (superclass_vec
, method_name
,
2779 /* We have a list of candidate symbol names, so now we
2780 iterate over the symbol tables looking for all
2781 matches in this pspace. */
2782 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
);
2784 VEC_truncate (typep
, superclass_vec
, 0);
2785 last_result_len
= VEC_length (const_char_ptr
, result_names
);
2789 if (!VEC_empty (symbolp
, info
.result
.symbols
)
2790 || !VEC_empty (minsym_and_objfile_d
, info
.result
.minimal_symbols
))
2792 *symbols
= info
.result
.symbols
;
2793 *minsyms
= info
.result
.minimal_symbols
;
2794 do_cleanups (cleanup
);
2798 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
2799 and other attempts to locate the symbol will be made. */
2800 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
2805 /* This object is used when collecting all matching symtabs. */
2807 struct symtab_collector
2809 /* The result vector of symtabs. */
2810 VEC (symtab_p
) *symtabs
;
2812 /* This is used to ensure the symtabs are unique. */
2813 htab_t symtab_table
;
2816 /* Callback for iterate_over_symtabs. */
2819 add_symtabs_to_list (struct symtab
*symtab
, void *d
)
2821 struct symtab_collector
*data
= d
;
2824 slot
= htab_find_slot (data
->symtab_table
, symtab
, INSERT
);
2828 VEC_safe_push (symtab_p
, data
->symtabs
, symtab
);
2834 /* Given a file name, return a VEC of all matching symtabs. */
2836 static VEC (symtab_p
) *
2837 collect_symtabs_from_filename (const char *file
)
2839 struct symtab_collector collector
;
2840 struct cleanup
*cleanups
;
2841 struct program_space
*pspace
;
2843 collector
.symtabs
= NULL
;
2844 collector
.symtab_table
= htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
2846 cleanups
= make_cleanup_htab_delete (collector
.symtab_table
);
2848 /* Find that file's data. */
2849 ALL_PSPACES (pspace
)
2851 if (pspace
->executing_startup
)
2854 set_current_program_space (pspace
);
2855 iterate_over_symtabs (file
, add_symtabs_to_list
, &collector
);
2858 do_cleanups (cleanups
);
2859 return collector
.symtabs
;
2862 /* Return all the symtabs associated to the FILENAME. */
2864 static VEC (symtab_p
) *
2865 symtabs_from_filename (const char *filename
)
2867 VEC (symtab_p
) *result
;
2869 result
= collect_symtabs_from_filename (filename
);
2871 if (VEC_empty (symtab_p
, result
))
2873 if (!have_full_symbols () && !have_partial_symbols ())
2874 throw_error (NOT_FOUND_ERROR
,
2875 _("No symbol table is loaded. "
2876 "Use the \"file\" command."));
2877 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), filename
);
2883 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
2884 debug symbols are returned in SYMBOLS. Matching minimal symbols are
2885 returned in MINSYMS. */
2888 find_function_symbols (struct linespec_state
*state
,
2889 VEC (symtab_p
) *file_symtabs
, const char *name
,
2890 VEC (symbolp
) **symbols
,
2891 VEC (minsym_and_objfile_d
) **minsyms
)
2893 struct collect_info info
;
2894 VEC (const_char_ptr
) *symbol_names
= NULL
;
2895 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
2899 info
.result
.symbols
= NULL
;
2900 info
.result
.minimal_symbols
= NULL
;
2901 info
.file_symtabs
= file_symtabs
;
2903 /* Try NAME as an Objective-C selector. */
2904 find_imps ((char *) name
, &symbol_names
);
2905 if (!VEC_empty (const_char_ptr
, symbol_names
))
2906 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
);
2908 add_matching_symbols_to_info (name
, &info
, NULL
);
2910 do_cleanups (cleanup
);
2912 if (VEC_empty (symbolp
, info
.result
.symbols
))
2914 VEC_free (symbolp
, info
.result
.symbols
);
2918 *symbols
= info
.result
.symbols
;
2920 if (VEC_empty (minsym_and_objfile_d
, info
.result
.minimal_symbols
))
2922 VEC_free (minsym_and_objfile_d
, info
.result
.minimal_symbols
);
2926 *minsyms
= info
.result
.minimal_symbols
;
2929 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
2930 in SYMBOLS and minimal symbols in MINSYMS. */
2933 find_linespec_symbols (struct linespec_state
*state
,
2934 VEC (symtab_p
) *file_symtabs
,
2936 VEC (symbolp
) **symbols
,
2937 VEC (minsym_and_objfile_d
) **minsyms
)
2939 char *klass
, *method
, *canon
;
2940 const char *lookup_name
, *last
, *p
, *scope_op
;
2941 struct cleanup
*cleanup
;
2942 VEC (symbolp
) *classes
;
2943 volatile struct gdb_exception except
;
2945 cleanup
= demangle_for_lookup (name
, state
->language
->la_language
,
2947 if (state
->language
->la_language
== language_ada
)
2949 /* In Ada, the symbol lookups are performed using the encoded
2950 name rather than the demangled name. */
2951 lookup_name
= ada_name_for_lookup (name
);
2952 make_cleanup (xfree
, (void *) lookup_name
);
2955 canon
= cp_canonicalize_string_no_typedefs (lookup_name
);
2958 lookup_name
= canon
;
2959 cleanup
= make_cleanup (xfree
, canon
);
2962 /* See if we can find a scope operator and break this symbol
2963 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
2965 p
= find_toplevel_string (lookup_name
, scope_op
);
2968 /* No C++ scope operator. Try Java. */
2970 p
= find_toplevel_string (lookup_name
, scope_op
);
2977 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
2980 /* If no scope operator was found, lookup the name as a symbol. */
2983 find_function_symbols (state
, file_symtabs
, lookup_name
,
2985 do_cleanups (cleanup
);
2989 /* NAME points to the class name.
2990 LAST points to the method name. */
2991 klass
= xmalloc ((last
- lookup_name
+ 1) * sizeof (char));
2992 make_cleanup (xfree
, klass
);
2993 strncpy (klass
, lookup_name
, last
- lookup_name
);
2994 klass
[last
- lookup_name
] = '\0';
2996 /* Skip past the scope operator. */
2997 last
+= strlen (scope_op
);
2998 method
= xmalloc ((strlen (last
) + 1) * sizeof (char));
2999 make_cleanup (xfree
, method
);
3000 strcpy (method
, last
);
3002 /* Find a list of classes named KLASS. */
3003 classes
= lookup_prefix_sym (state
, file_symtabs
, klass
);
3004 make_cleanup (VEC_cleanup (symbolp
), &classes
);
3005 if (!VEC_empty (symbolp
, classes
))
3007 /* Now locate a list of suitable methods named METHOD. */
3008 TRY_CATCH (except
, RETURN_MASK_ERROR
)
3010 find_method (state
, file_symtabs
, klass
, method
, classes
,
3014 /* If successful, we're done. If NOT_FOUND_ERROR
3015 was not thrown, rethrow the exception that we did get.
3016 Otherwise, fall back to looking up the entire name as a symbol.
3017 This can happen with namespace::function. */
3018 if (except
.reason
>= 0)
3020 do_cleanups (cleanup
);
3023 else if (except
.error
!= NOT_FOUND_ERROR
)
3024 throw_exception (except
);
3027 /* We couldn't find a class, so we check the entire name as a symbol
3029 find_function_symbols (state
, file_symtabs
, lookup_name
, symbols
, minsyms
);
3030 do_cleanups (cleanup
);
3033 /* Return all labels named NAME in FUNCTION_SYMBOLS. Return the
3034 actual function symbol in which the label was found in LABEL_FUNC_RET. */
3036 static VEC (symbolp
) *
3037 find_label_symbols (struct linespec_state
*self
,
3038 VEC (symbolp
) *function_symbols
,
3039 VEC (symbolp
) **label_funcs_ret
, const char *name
)
3042 struct block
*block
;
3044 struct symbol
*fn_sym
;
3045 VEC (symbolp
) *result
= NULL
;
3047 if (function_symbols
== NULL
)
3049 set_current_program_space (self
->program_space
);
3050 block
= get_search_block (NULL
);
3053 block
&& !BLOCK_FUNCTION (block
);
3054 block
= BLOCK_SUPERBLOCK (block
))
3058 fn_sym
= BLOCK_FUNCTION (block
);
3060 sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0);
3064 VEC_safe_push (symbolp
, result
, sym
);
3065 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
3071 VEC_iterate (symbolp
, function_symbols
, ix
, fn_sym
); ++ix
)
3073 set_current_program_space (SYMTAB_PSPACE (SYMBOL_SYMTAB (fn_sym
)));
3074 block
= SYMBOL_BLOCK_VALUE (fn_sym
);
3075 sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0);
3079 VEC_safe_push (symbolp
, result
, sym
);
3080 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
3090 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3093 decode_digits_list_mode (struct linespec_state
*self
,
3095 struct symtabs_and_lines
*values
,
3096 struct symtab_and_line val
)
3101 gdb_assert (self
->list_mode
);
3103 for (ix
= 0; VEC_iterate (symtab_p
, ls
->file_symtabs
, ix
, elt
);
3106 /* The logic above should ensure this. */
3107 gdb_assert (elt
!= NULL
);
3109 set_current_program_space (SYMTAB_PSPACE (elt
));
3111 /* Simplistic search just for the list command. */
3112 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
3113 if (val
.symtab
== NULL
)
3115 val
.pspace
= SYMTAB_PSPACE (elt
);
3117 val
.explicit_line
= 1;
3119 add_sal_to_sals (self
, values
, &val
, NULL
, 0);
3123 /* A helper for create_sals_line_offset that iterates over the symtabs,
3124 adding lines to the VEC. */
3127 decode_digits_ordinary (struct linespec_state
*self
,
3130 struct symtabs_and_lines
*sals
,
3131 struct linetable_entry
**best_entry
)
3136 for (ix
= 0; VEC_iterate (symtab_p
, ls
->file_symtabs
, ix
, elt
); ++ix
)
3139 VEC (CORE_ADDR
) *pcs
;
3142 /* The logic above should ensure this. */
3143 gdb_assert (elt
!= NULL
);
3145 set_current_program_space (SYMTAB_PSPACE (elt
));
3147 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
3148 for (i
= 0; VEC_iterate (CORE_ADDR
, pcs
, i
, pc
); ++i
)
3150 struct symtab_and_line sal
;
3153 sal
.pspace
= SYMTAB_PSPACE (elt
);
3157 add_sal_to_sals_basic (sals
, &sal
);
3160 VEC_free (CORE_ADDR
, pcs
);
3166 /* Return the line offset represented by VARIABLE. */
3168 static struct line_offset
3169 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
3173 struct line_offset offset
= {0, LINE_OFFSET_NONE
};
3175 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
3178 while (*p
>= '0' && *p
<= '9')
3180 if (!*p
) /* Reached end of token without hitting non-digit. */
3182 /* We have a value history reference. */
3183 struct value
*val_history
;
3185 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
3187 = access_value_history ((variable
[1] == '$') ? -index
: index
);
3188 if (TYPE_CODE (value_type (val_history
)) != TYPE_CODE_INT
)
3189 error (_("History values used in line "
3190 "specs must have integer values."));
3191 offset
.offset
= value_as_long (val_history
);
3195 /* Not all digits -- may be user variable/function or a
3196 convenience variable. */
3198 struct internalvar
*ivar
;
3200 /* Try it as a convenience variable. If it is not a convenience
3201 variable, return and allow normal symbol lookup to occur. */
3202 ivar
= lookup_only_internalvar (variable
+ 1);
3204 /* No internal variable with that name. Mark the offset
3205 as unknown to allow the name to be looked up as a symbol. */
3206 offset
.sign
= LINE_OFFSET_UNKNOWN
;
3209 /* We found a valid variable name. If it is not an integer,
3211 if (!get_internalvar_integer (ivar
, &valx
))
3212 error (_("Convenience variables used in line "
3213 "specs must have integer values."));
3215 offset
.offset
= valx
;
3223 /* A callback used to possibly add a symbol to the results. */
3226 collect_symbols (struct symbol
*sym
, void *data
)
3228 struct collect_info
*info
= data
;
3230 /* In list mode, add all matching symbols, regardless of class.
3231 This allows the user to type "list a_global_variable". */
3232 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
|| info
->state
->list_mode
)
3233 VEC_safe_push (symbolp
, info
->result
.symbols
, sym
);
3234 return 1; /* Continue iterating. */
3237 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
3238 linespec; return the SAL in RESULT. */
3241 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
3242 struct minimal_symbol
*msymbol
,
3243 struct symtabs_and_lines
*result
)
3245 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3247 struct symtab_and_line sal
;
3249 sal
= find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol
),
3250 (struct obj_section
*) 0, 0);
3251 sal
.section
= SYMBOL_OBJ_SECTION (msymbol
);
3253 /* The minimal symbol might point to a function descriptor;
3254 resolve it to the actual code address instead. */
3255 pc
= gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
3257 sal
= find_pc_sect_line (pc
, NULL
, 0);
3259 if (self
->funfirstline
)
3260 skip_prologue_sal (&sal
);
3262 if (maybe_add_address (self
->addr_set
, objfile
->pspace
, sal
.pc
))
3263 add_sal_to_sals (self
, result
, &sal
, SYMBOL_NATURAL_NAME (msymbol
), 0);
3266 /* A helper struct to pass some data through
3267 iterate_over_minimal_symbols. */
3269 struct collect_minsyms
3271 /* The objfile we're examining. */
3272 struct objfile
*objfile
;
3274 /* The funfirstline setting from the initial call. */
3277 /* The list_mode setting from the initial call. */
3280 /* The resulting symbols. */
3281 VEC (minsym_and_objfile_d
) *msyms
;
3284 /* A helper function to classify a minimal_symbol_type according to
3288 classify_mtype (enum minimal_symbol_type t
)
3295 /* Intermediate priority. */
3298 case mst_solib_trampoline
:
3299 /* Lowest priority. */
3303 /* Highest priority. */
3308 /* Callback for qsort that sorts symbols by priority. */
3311 compare_msyms (const void *a
, const void *b
)
3313 const minsym_and_objfile_d
*moa
= a
;
3314 const minsym_and_objfile_d
*mob
= b
;
3315 enum minimal_symbol_type ta
= MSYMBOL_TYPE (moa
->minsym
);
3316 enum minimal_symbol_type tb
= MSYMBOL_TYPE (mob
->minsym
);
3318 return classify_mtype (ta
) - classify_mtype (tb
);
3321 /* Callback for iterate_over_minimal_symbols that adds the symbol to
3325 add_minsym (struct minimal_symbol
*minsym
, void *d
)
3327 struct collect_minsyms
*info
= d
;
3328 minsym_and_objfile_d mo
;
3330 /* Exclude data symbols when looking for breakpoint locations. */
3331 if (!info
->list_mode
)
3332 switch (minsym
->type
)
3334 case mst_slot_got_plt
:
3341 /* Make sure this minsym is not a function descriptor
3342 before we decide to discard it. */
3343 struct gdbarch
*gdbarch
= info
->objfile
->gdbarch
;
3344 CORE_ADDR addr
= gdbarch_convert_from_func_ptr_addr
3345 (gdbarch
, SYMBOL_VALUE_ADDRESS (minsym
),
3348 if (addr
== SYMBOL_VALUE_ADDRESS (minsym
))
3354 mo
.objfile
= info
->objfile
;
3355 VEC_safe_push (minsym_and_objfile_d
, info
->msyms
, &mo
);
3358 /* Search minimal symbols in all objfiles for NAME. If SEARCH_PSPACE
3359 is not NULL, the search is restricted to just that program
3363 search_minsyms_for_name (struct collect_info
*info
, const char *name
,
3364 struct program_space
*search_pspace
)
3366 struct objfile
*objfile
;
3367 struct program_space
*pspace
;
3369 ALL_PSPACES (pspace
)
3371 struct collect_minsyms local
;
3372 struct cleanup
*cleanup
;
3374 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
3376 if (pspace
->executing_startup
)
3379 set_current_program_space (pspace
);
3381 memset (&local
, 0, sizeof (local
));
3382 local
.funfirstline
= info
->state
->funfirstline
;
3383 local
.list_mode
= info
->state
->list_mode
;
3385 cleanup
= make_cleanup (VEC_cleanup (minsym_and_objfile_d
),
3388 ALL_OBJFILES (objfile
)
3390 local
.objfile
= objfile
;
3391 iterate_over_minimal_symbols (objfile
, name
, add_minsym
, &local
);
3394 if (!VEC_empty (minsym_and_objfile_d
, local
.msyms
))
3398 minsym_and_objfile_d
*item
;
3400 qsort (VEC_address (minsym_and_objfile_d
, local
.msyms
),
3401 VEC_length (minsym_and_objfile_d
, local
.msyms
),
3402 sizeof (minsym_and_objfile_d
),
3405 /* Now the minsyms are in classification order. So, we walk
3406 over them and process just the minsyms with the same
3407 classification as the very first minsym in the list. */
3408 item
= VEC_index (minsym_and_objfile_d
, local
.msyms
, 0);
3409 classification
= classify_mtype (MSYMBOL_TYPE (item
->minsym
));
3412 VEC_iterate (minsym_and_objfile_d
, local
.msyms
, ix
, item
);
3415 if (classify_mtype (MSYMBOL_TYPE (item
->minsym
)) != classification
)
3418 VEC_safe_push (minsym_and_objfile_d
,
3419 info
->result
.minimal_symbols
, item
);
3423 do_cleanups (cleanup
);
3427 /* A helper function to add all symbols matching NAME to INFO. If
3428 PSPACE is not NULL, the search is restricted to just that program
3432 add_matching_symbols_to_info (const char *name
,
3433 struct collect_info
*info
,
3434 struct program_space
*pspace
)
3439 for (ix
= 0; VEC_iterate (symtab_p
, info
->file_symtabs
, ix
, elt
); ++ix
)
3443 iterate_over_all_matching_symtabs (info
->state
, name
, VAR_DOMAIN
,
3444 collect_symbols
, info
,
3446 search_minsyms_for_name (info
, name
, pspace
);
3448 else if (pspace
== NULL
|| pspace
== SYMTAB_PSPACE (elt
))
3450 /* Program spaces that are executing startup should have
3451 been filtered out earlier. */
3452 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
3453 set_current_program_space (SYMTAB_PSPACE (elt
));
3454 LA_ITERATE_OVER_SYMBOLS (get_search_block (elt
), name
,
3455 VAR_DOMAIN
, collect_symbols
,
3463 /* Now come some functions that are called from multiple places within
3467 symbol_to_sal (struct symtab_and_line
*result
,
3468 int funfirstline
, struct symbol
*sym
)
3470 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
3472 *result
= find_function_start_sal (sym
, funfirstline
);
3477 if (SYMBOL_CLASS (sym
) == LOC_LABEL
&& SYMBOL_VALUE_ADDRESS (sym
) != 0)
3480 result
->symtab
= SYMBOL_SYMTAB (sym
);
3481 result
->line
= SYMBOL_LINE (sym
);
3482 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
3483 result
->pspace
= SYMTAB_PSPACE (SYMBOL_SYMTAB (sym
));
3484 result
->explicit_pc
= 1;
3487 else if (funfirstline
)
3491 else if (SYMBOL_LINE (sym
) != 0)
3493 /* We know its line number. */
3495 result
->symtab
= SYMBOL_SYMTAB (sym
);
3496 result
->line
= SYMBOL_LINE (sym
);
3497 result
->pspace
= SYMTAB_PSPACE (SYMBOL_SYMTAB (sym
));
3505 /* See the comment in linespec.h. */
3508 init_linespec_result (struct linespec_result
*lr
)
3510 memset (lr
, 0, sizeof (*lr
));
3513 /* See the comment in linespec.h. */
3516 destroy_linespec_result (struct linespec_result
*ls
)
3519 struct linespec_sals
*lsal
;
3521 xfree (ls
->addr_string
);
3522 for (i
= 0; VEC_iterate (linespec_sals
, ls
->sals
, i
, lsal
); ++i
)
3524 xfree (lsal
->canonical
);
3525 xfree (lsal
->sals
.sals
);
3527 VEC_free (linespec_sals
, ls
->sals
);
3530 /* Cleanup function for a linespec_result. */
3533 cleanup_linespec_result (void *a
)
3535 destroy_linespec_result (a
);
3538 /* See the comment in linespec.h. */
3541 make_cleanup_destroy_linespec_result (struct linespec_result
*ls
)
3543 return make_cleanup (cleanup_linespec_result
, ls
);