1 /* Parser for linespec for the GNU debugger, GDB.
3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "completer.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
34 #include "objc-lang.h"
38 #include "mi/mi-cmds.h"
40 #include "arch-utils.h"
42 #include "cli/cli-utils.h"
43 #include "filenames.h"
47 typedef struct symbol
*symbolp
;
50 typedef struct type
*typep
;
53 /* An address entry is used to ensure that any given location is only
54 added to the result a single time. It holds an address and the
55 program space from which the address came. */
59 struct program_space
*pspace
;
63 typedef struct bound_minimal_symbol bound_minimal_symbol_d
;
65 DEF_VEC_O (bound_minimal_symbol_d
);
67 /* An enumeration of possible signs for a line offset. */
68 enum offset_relative_sign
73 /* A plus sign ("+") */
76 /* A minus sign ("-") */
79 /* A special "sign" for unspecified offset. */
83 /* A line offset in a linespec. */
87 /* Line offset and any specified sign. */
89 enum offset_relative_sign sign
;
92 /* A linespec. Elements of this structure are filled in by a parser
93 (either parse_linespec or some other function). The structure is
94 then converted into SALs by convert_linespec_to_sals. */
98 /* An expression and the resulting PC. Specifying an expression
99 currently precludes the use of other members. */
101 /* The expression entered by the user. */
102 const char *expression
;
104 /* The resulting PC expression derived from evaluating EXPRESSION. */
107 /* Any specified file symtabs. */
109 /* The user-supplied source filename or NULL if none was specified. */
110 const char *source_filename
;
112 /* The list of symtabs to search to which to limit the search. May not
113 be NULL. If SOURCE_FILENAME is NULL (no user-specified filename),
114 FILE_SYMTABS should contain one single NULL member. This will
115 cause the code to use the default symtab. */
116 VEC (symtab_ptr
) *file_symtabs
;
118 /* The name of a function or method and any matching symbols. */
120 /* The user-specified function name. If no function name was
121 supplied, this may be NULL. */
122 const char *function_name
;
124 /* A list of matching function symbols and minimal symbols. Both lists
125 may be NULL if no matching symbols were found. */
126 VEC (symbolp
) *function_symbols
;
127 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
129 /* The name of a label and matching symbols. */
131 /* The user-specified label name. */
132 const char *label_name
;
134 /* A structure of matching label symbols and the corresponding
135 function symbol in which the label was found. Both may be NULL
136 or both must be non-NULL. */
139 VEC (symbolp
) *label_symbols
;
140 VEC (symbolp
) *function_symbols
;
143 /* Line offset. It may be LINE_OFFSET_UNKNOWN, meaning that no
144 offset was specified. */
145 struct line_offset line_offset
;
147 typedef struct linespec
*linespec_p
;
149 /* A canonical linespec represented as a symtab-related string.
151 Each entry represents the "SYMTAB:SUFFIX" linespec string.
152 SYMTAB can be converted for example by symtab_to_fullname or
153 symtab_to_filename_for_display as needed. */
155 struct linespec_canonical_name
157 /* Remaining text part of the linespec string. */
160 /* If NULL then SUFFIX is the whole linespec string. */
161 struct symtab
*symtab
;
164 /* An instance of this is used to keep all state while linespec
165 operates. This instance is passed around as a 'this' pointer to
166 the various implementation methods. */
168 struct linespec_state
170 /* The language in use during linespec processing. */
171 const struct language_defn
*language
;
173 /* The program space as seen when the module was entered. */
174 struct program_space
*program_space
;
176 /* The default symtab to use, if no other symtab is specified. */
177 struct symtab
*default_symtab
;
179 /* The default line to use. */
182 /* The 'funfirstline' value that was passed in to decode_line_1 or
186 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
189 /* The 'canonical' value passed to decode_line_full, or NULL. */
190 struct linespec_result
*canonical
;
192 /* Canonical strings that mirror the symtabs_and_lines result. */
193 struct linespec_canonical_name
*canonical_names
;
195 /* This is a set of address_entry objects which is used to prevent
196 duplicate symbols from being entered into the result. */
200 /* This is a helper object that is used when collecting symbols into a
205 /* The linespec object in use. */
206 struct linespec_state
*state
;
208 /* A list of symtabs to which to restrict matches. */
209 VEC (symtab_ptr
) *file_symtabs
;
211 /* The result being accumulated. */
214 VEC (symbolp
) *symbols
;
215 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
226 /* A colon "separator" */
238 /* EOI (end of input) */
244 typedef enum ls_token_type linespec_token_type
;
246 /* List of keywords */
248 static const char * const linespec_keywords
[] = { "if", "thread", "task" };
250 /* A token of the linespec lexer */
254 /* The type of the token */
255 linespec_token_type type
;
257 /* Data for the token */
260 /* A string, given as a stoken */
261 struct stoken string
;
267 typedef struct ls_token linespec_token
;
269 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
270 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
272 /* An instance of the linespec parser. */
276 /* Lexer internal data */
279 /* Save head of input stream. */
280 const char *saved_arg
;
282 /* Head of the input stream. */
284 #define PARSER_STREAM(P) (*(P)->lexer.stream)
286 /* The current token. */
287 linespec_token current
;
290 /* Is the entire linespec quote-enclosed? */
291 int is_quote_enclosed
;
293 /* Is a keyword syntactically valid at this point?
294 In, e.g., "break thread thread 1", the leading "keyword" must not
295 be interpreted as such. */
298 /* The state of the parse. */
299 struct linespec_state state
;
300 #define PARSER_STATE(PPTR) (&(PPTR)->state)
302 /* The result of the parse. */
303 struct linespec result
;
304 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
306 typedef struct ls_parser linespec_parser
;
308 /* Prototypes for local functions. */
310 static void iterate_over_file_blocks (struct symtab
*symtab
,
311 const char *name
, domain_enum domain
,
312 symbol_found_callback_ftype
*callback
,
315 static void initialize_defaults (struct symtab
**default_symtab
,
318 static CORE_ADDR
linespec_expression_to_pc (const char **exp_ptr
);
320 static struct symtabs_and_lines
decode_objc (struct linespec_state
*self
,
322 const char **argptr
);
324 static VEC (symtab_ptr
) *symtabs_from_filename (const char *);
326 static VEC (symbolp
) *find_label_symbols (struct linespec_state
*self
,
327 VEC (symbolp
) *function_symbols
,
328 VEC (symbolp
) **label_funcs_ret
,
331 static void find_linespec_symbols (struct linespec_state
*self
,
332 VEC (symtab_ptr
) *file_symtabs
,
334 VEC (symbolp
) **symbols
,
335 VEC (bound_minimal_symbol_d
) **minsyms
);
337 static struct line_offset
338 linespec_parse_variable (struct linespec_state
*self
,
339 const char *variable
);
341 static int symbol_to_sal (struct symtab_and_line
*result
,
342 int funfirstline
, struct symbol
*sym
);
344 static void add_matching_symbols_to_info (const char *name
,
345 struct collect_info
*info
,
346 struct program_space
*pspace
);
348 static void add_all_symbol_names_from_pspace (struct collect_info
*info
,
349 struct program_space
*pspace
,
350 VEC (const_char_ptr
) *names
);
352 static VEC (symtab_ptr
) *collect_symtabs_from_filename (const char *file
);
354 static void decode_digits_ordinary (struct linespec_state
*self
,
357 struct symtabs_and_lines
*sals
,
358 struct linetable_entry
**best_entry
);
360 static void decode_digits_list_mode (struct linespec_state
*self
,
362 struct symtabs_and_lines
*values
,
363 struct symtab_and_line val
);
365 static void minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
366 struct minimal_symbol
*msymbol
,
367 struct symtabs_and_lines
*result
);
369 static int compare_symbols (const void *a
, const void *b
);
371 static int compare_msymbols (const void *a
, const void *b
);
373 static const char *find_toplevel_char (const char *s
, char c
);
375 /* Permitted quote characters for the parser. This is different from the
376 completer's quote characters to allow backward compatibility with the
378 static const char *const linespec_quote_characters
= "\"\'";
380 /* Lexer functions. */
382 /* Lex a number from the input in PARSER. This only supports
385 Return true if input is decimal numbers. Return false if not. */
388 linespec_lexer_lex_number (linespec_parser
*parser
, linespec_token
*tokenp
)
390 tokenp
->type
= LSTOKEN_NUMBER
;
391 LS_TOKEN_STOKEN (*tokenp
).length
= 0;
392 LS_TOKEN_STOKEN (*tokenp
).ptr
= PARSER_STREAM (parser
);
394 /* Keep any sign at the start of the stream. */
395 if (*PARSER_STREAM (parser
) == '+' || *PARSER_STREAM (parser
) == '-')
397 ++LS_TOKEN_STOKEN (*tokenp
).length
;
398 ++(PARSER_STREAM (parser
));
401 while (isdigit (*PARSER_STREAM (parser
)))
403 ++LS_TOKEN_STOKEN (*tokenp
).length
;
404 ++(PARSER_STREAM (parser
));
407 /* If the next character in the input buffer is not a space, comma,
408 quote, or colon, this input does not represent a number. */
409 if (*PARSER_STREAM (parser
) != '\0'
410 && !isspace (*PARSER_STREAM (parser
)) && *PARSER_STREAM (parser
) != ','
411 && *PARSER_STREAM (parser
) != ':'
412 && !strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
414 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (*tokenp
).ptr
;
421 /* Does P represent one of the keywords? If so, return
422 the keyword. If not, return NULL. */
425 linespec_lexer_lex_keyword (const char *p
)
431 for (i
= 0; i
< ARRAY_SIZE (linespec_keywords
); ++i
)
433 int len
= strlen (linespec_keywords
[i
]);
435 /* If P begins with one of the keywords and the next
436 character is not a valid identifier character,
437 we have found a keyword. */
438 if (strncmp (p
, linespec_keywords
[i
], len
) == 0
439 && !(isalnum (p
[len
]) || p
[len
] == '_'))
440 return linespec_keywords
[i
];
447 /* Does STRING represent an Ada operator? If so, return the length
448 of the decoded operator name. If not, return 0. */
451 is_ada_operator (const char *string
)
453 const struct ada_opname_map
*mapping
;
455 for (mapping
= ada_opname_table
;
456 mapping
->encoded
!= NULL
457 && !startswith (string
, mapping
->decoded
); ++mapping
)
460 return mapping
->decoded
== NULL
? 0 : strlen (mapping
->decoded
);
463 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
464 the location of QUOTE_CHAR, or NULL if not found. */
467 skip_quote_char (const char *string
, char quote_char
)
469 const char *p
, *last
;
471 p
= last
= find_toplevel_char (string
, quote_char
);
472 while (p
&& *p
!= '\0' && *p
!= ':')
474 p
= find_toplevel_char (p
, quote_char
);
482 /* Make a writable copy of the string given in TOKEN, trimming
483 any trailing whitespace. */
486 copy_token_string (linespec_token token
)
490 if (token
.type
== LSTOKEN_KEYWORD
)
491 return xstrdup (LS_TOKEN_KEYWORD (token
));
493 str
= savestring (LS_TOKEN_STOKEN (token
).ptr
,
494 LS_TOKEN_STOKEN (token
).length
);
495 s
= remove_trailing_whitespace (str
, str
+ LS_TOKEN_STOKEN (token
).length
);
501 /* Does P represent the end of a quote-enclosed linespec? */
504 is_closing_quote_enclosed (const char *p
)
506 if (strchr (linespec_quote_characters
, *p
))
508 p
= skip_spaces ((char *) p
);
509 return (*p
== '\0' || linespec_lexer_lex_keyword (p
));
512 /* Find the end of the parameter list that starts with *INPUT.
513 This helper function assists with lexing string segments
514 which might contain valid (non-terminating) commas. */
517 find_parameter_list_end (const char *input
)
519 char end_char
, start_char
;
524 if (start_char
== '(')
526 else if (start_char
== '<')
535 if (*p
== start_char
)
537 else if (*p
== end_char
)
552 /* Lex a string from the input in PARSER. */
554 static linespec_token
555 linespec_lexer_lex_string (linespec_parser
*parser
)
557 linespec_token token
;
558 const char *start
= PARSER_STREAM (parser
);
560 token
.type
= LSTOKEN_STRING
;
562 /* If the input stream starts with a quote character, skip to the next
563 quote character, regardless of the content. */
564 if (strchr (linespec_quote_characters
, *PARSER_STREAM (parser
)))
567 char quote_char
= *PARSER_STREAM (parser
);
569 /* Special case: Ada operators. */
570 if (PARSER_STATE (parser
)->language
->la_language
== language_ada
571 && quote_char
== '\"')
573 int len
= is_ada_operator (PARSER_STREAM (parser
));
577 /* The input is an Ada operator. Return the quoted string
579 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
580 LS_TOKEN_STOKEN (token
).length
= len
;
581 PARSER_STREAM (parser
) += len
;
585 /* The input does not represent an Ada operator -- fall through
586 to normal quoted string handling. */
589 /* Skip past the beginning quote. */
590 ++(PARSER_STREAM (parser
));
592 /* Mark the start of the string. */
593 LS_TOKEN_STOKEN (token
).ptr
= PARSER_STREAM (parser
);
595 /* Skip to the ending quote. */
596 end
= skip_quote_char (PARSER_STREAM (parser
), quote_char
);
598 /* Error if the input did not terminate properly. */
600 error (_("unmatched quote"));
602 /* Skip over the ending quote and mark the length of the string. */
603 PARSER_STREAM (parser
) = (char *) ++end
;
604 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - 2 - start
;
610 /* Otherwise, only identifier characters are permitted.
611 Spaces are the exception. In general, we keep spaces,
612 but only if the next characters in the input do not resolve
613 to one of the keywords.
615 This allows users to forgo quoting CV-qualifiers, template arguments,
616 and similar common language constructs. */
620 if (isspace (*PARSER_STREAM (parser
)))
622 p
= skip_spaces_const (PARSER_STREAM (parser
));
623 /* When we get here we know we've found something followed by
624 a space (we skip over parens and templates below).
625 So if we find a keyword now, we know it is a keyword and not,
626 say, a function name. */
627 if (linespec_lexer_lex_keyword (p
) != NULL
)
629 LS_TOKEN_STOKEN (token
).ptr
= start
;
630 LS_TOKEN_STOKEN (token
).length
631 = PARSER_STREAM (parser
) - start
;
635 /* Advance past the whitespace. */
636 PARSER_STREAM (parser
) = p
;
639 /* If the next character is EOI or (single) ':', the
640 string is complete; return the token. */
641 if (*PARSER_STREAM (parser
) == 0)
643 LS_TOKEN_STOKEN (token
).ptr
= start
;
644 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
647 else if (PARSER_STREAM (parser
)[0] == ':')
649 /* Do not tokenize the C++ scope operator. */
650 if (PARSER_STREAM (parser
)[1] == ':')
651 ++(PARSER_STREAM (parser
));
653 /* Do not tokenify if the input length so far is one
654 (i.e, a single-letter drive name) and the next character
655 is a directory separator. This allows Windows-style
656 paths to be recognized as filenames without quoting it. */
657 else if ((PARSER_STREAM (parser
) - start
) != 1
658 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser
)[1]))
660 LS_TOKEN_STOKEN (token
).ptr
= start
;
661 LS_TOKEN_STOKEN (token
).length
662 = PARSER_STREAM (parser
) - start
;
666 /* Special case: permit quote-enclosed linespecs. */
667 else if (parser
->is_quote_enclosed
668 && strchr (linespec_quote_characters
,
669 *PARSER_STREAM (parser
))
670 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
672 LS_TOKEN_STOKEN (token
).ptr
= start
;
673 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
676 /* Because commas may terminate a linespec and appear in
677 the middle of valid string input, special cases for
678 '<' and '(' are necessary. */
679 else if (*PARSER_STREAM (parser
) == '<'
680 || *PARSER_STREAM (parser
) == '(')
684 p
= find_parameter_list_end (PARSER_STREAM (parser
));
687 PARSER_STREAM (parser
) = p
;
691 /* Commas are terminators, but not if they are part of an
693 else if (*PARSER_STREAM (parser
) == ',')
695 if ((PARSER_STATE (parser
)->language
->la_language
697 && (PARSER_STREAM (parser
) - start
) > 8
698 /* strlen ("operator") */)
700 char *p
= strstr (start
, "operator");
702 if (p
!= NULL
&& is_operator_name (p
))
704 /* This is an operator name. Keep going. */
705 ++(PARSER_STREAM (parser
));
710 /* Comma terminates the string. */
711 LS_TOKEN_STOKEN (token
).ptr
= start
;
712 LS_TOKEN_STOKEN (token
).length
= PARSER_STREAM (parser
) - start
;
716 /* Advance the stream. */
717 ++(PARSER_STREAM (parser
));
724 /* Lex a single linespec token from PARSER. */
726 static linespec_token
727 linespec_lexer_lex_one (linespec_parser
*parser
)
731 if (parser
->lexer
.current
.type
== LSTOKEN_CONSUMED
)
733 /* Skip any whitespace. */
734 PARSER_STREAM (parser
) = skip_spaces_const (PARSER_STREAM (parser
));
736 /* Check for a keyword, they end the linespec. */
738 if (parser
->keyword_ok
)
739 keyword
= linespec_lexer_lex_keyword (PARSER_STREAM (parser
));
742 parser
->lexer
.current
.type
= LSTOKEN_KEYWORD
;
743 LS_TOKEN_KEYWORD (parser
->lexer
.current
) = keyword
;
744 return parser
->lexer
.current
;
747 /* Handle other tokens. */
748 switch (*PARSER_STREAM (parser
))
751 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
755 case '0': case '1': case '2': case '3': case '4':
756 case '5': case '6': case '7': case '8': case '9':
757 if (!linespec_lexer_lex_number (parser
, &(parser
->lexer
.current
)))
758 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
762 /* If we have a scope operator, lex the input as a string.
763 Otherwise, return LSTOKEN_COLON. */
764 if (PARSER_STREAM (parser
)[1] == ':')
765 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
768 parser
->lexer
.current
.type
= LSTOKEN_COLON
;
769 ++(PARSER_STREAM (parser
));
773 case '\'': case '\"':
774 /* Special case: permit quote-enclosed linespecs. */
775 if (parser
->is_quote_enclosed
776 && is_closing_quote_enclosed (PARSER_STREAM (parser
)))
778 ++(PARSER_STREAM (parser
));
779 parser
->lexer
.current
.type
= LSTOKEN_EOI
;
782 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
786 parser
->lexer
.current
.type
= LSTOKEN_COMMA
;
787 LS_TOKEN_STOKEN (parser
->lexer
.current
).ptr
788 = PARSER_STREAM (parser
);
789 LS_TOKEN_STOKEN (parser
->lexer
.current
).length
= 1;
790 ++(PARSER_STREAM (parser
));
794 /* If the input is not a number, it must be a string.
795 [Keywords were already considered above.] */
796 parser
->lexer
.current
= linespec_lexer_lex_string (parser
);
801 return parser
->lexer
.current
;
804 /* Consume the current token and return the next token in PARSER's
807 static linespec_token
808 linespec_lexer_consume_token (linespec_parser
*parser
)
810 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
811 return linespec_lexer_lex_one (parser
);
814 /* Return the next token without consuming the current token. */
816 static linespec_token
817 linespec_lexer_peek_token (linespec_parser
*parser
)
820 const char *saved_stream
= PARSER_STREAM (parser
);
821 linespec_token saved_token
= parser
->lexer
.current
;
823 next
= linespec_lexer_consume_token (parser
);
824 PARSER_STREAM (parser
) = saved_stream
;
825 parser
->lexer
.current
= saved_token
;
829 /* Helper functions. */
831 /* Add SAL to SALS. */
834 add_sal_to_sals_basic (struct symtabs_and_lines
*sals
,
835 struct symtab_and_line
*sal
)
838 sals
->sals
= xrealloc (sals
->sals
, sals
->nelts
* sizeof (sals
->sals
[0]));
839 sals
->sals
[sals
->nelts
- 1] = *sal
;
842 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
843 the new sal, if needed. If not NULL, SYMNAME is the name of the
844 symbol to use when constructing the new canonical name.
846 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
847 canonical name for the SAL. */
850 add_sal_to_sals (struct linespec_state
*self
,
851 struct symtabs_and_lines
*sals
,
852 struct symtab_and_line
*sal
,
853 const char *symname
, int literal_canonical
)
855 add_sal_to_sals_basic (sals
, sal
);
859 struct linespec_canonical_name
*canonical
;
861 self
->canonical_names
= xrealloc (self
->canonical_names
,
863 * sizeof (*self
->canonical_names
)));
864 canonical
= &self
->canonical_names
[sals
->nelts
- 1];
865 if (!literal_canonical
&& sal
->symtab
)
867 const char *fullname
= symtab_to_fullname (sal
->symtab
);
869 /* Note that the filter doesn't have to be a valid linespec
870 input. We only apply the ":LINE" treatment to Ada for
872 if (symname
!= NULL
&& sal
->line
!= 0
873 && self
->language
->la_language
== language_ada
)
874 canonical
->suffix
= xstrprintf ("%s:%d", symname
, sal
->line
);
875 else if (symname
!= NULL
)
876 canonical
->suffix
= xstrdup (symname
);
878 canonical
->suffix
= xstrprintf ("%d", sal
->line
);
879 canonical
->symtab
= sal
->symtab
;
884 canonical
->suffix
= xstrdup (symname
);
886 canonical
->suffix
= xstrdup ("<unknown>");
887 canonical
->symtab
= NULL
;
892 /* A hash function for address_entry. */
895 hash_address_entry (const void *p
)
897 const struct address_entry
*aep
= p
;
900 hash
= iterative_hash_object (aep
->pspace
, 0);
901 return iterative_hash_object (aep
->addr
, hash
);
904 /* An equality function for address_entry. */
907 eq_address_entry (const void *a
, const void *b
)
909 const struct address_entry
*aea
= a
;
910 const struct address_entry
*aeb
= b
;
912 return aea
->pspace
== aeb
->pspace
&& aea
->addr
== aeb
->addr
;
915 /* Check whether the address, represented by PSPACE and ADDR, is
916 already in the set. If so, return 0. Otherwise, add it and return
920 maybe_add_address (htab_t set
, struct program_space
*pspace
, CORE_ADDR addr
)
922 struct address_entry e
, *p
;
927 slot
= htab_find_slot (set
, &e
, INSERT
);
931 p
= XNEW (struct address_entry
);
932 memcpy (p
, &e
, sizeof (struct address_entry
));
938 /* A callback function and the additional data to call it with. */
940 struct symbol_and_data_callback
942 /* The callback to use. */
943 symbol_found_callback_ftype
*callback
;
945 /* Data to be passed to the callback. */
949 /* A helper for iterate_over_all_matching_symtabs that is used to
950 restrict calls to another callback to symbols representing inline
954 iterate_inline_only (struct symbol
*sym
, void *d
)
956 if (SYMBOL_INLINED (sym
))
958 struct symbol_and_data_callback
*cad
= d
;
960 return cad
->callback (sym
, cad
->data
);
962 return 1; /* Continue iterating. */
965 /* Some data for the expand_symtabs_matching callback. */
967 struct symbol_matcher_data
969 /* The lookup name against which symbol name should be compared. */
970 const char *lookup_name
;
972 /* The routine to be used for comparison. */
973 symbol_name_cmp_ftype symbol_name_cmp
;
976 /* A helper for iterate_over_all_matching_symtabs that is passed as a
977 callback to the expand_symtabs_matching method. */
980 iterate_name_matcher (const char *name
, void *d
)
982 const struct symbol_matcher_data
*data
= d
;
984 if (data
->symbol_name_cmp (name
, data
->lookup_name
) == 0)
985 return 1; /* Expand this symbol's symbol table. */
986 return 0; /* Skip this symbol. */
989 /* A helper that walks over all matching symtabs in all objfiles and
990 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
991 not NULL, then the search is restricted to just that program
992 space. If INCLUDE_INLINE is nonzero then symbols representing
993 inlined instances of functions will be included in the result. */
996 iterate_over_all_matching_symtabs (struct linespec_state
*state
,
998 const domain_enum domain
,
999 symbol_found_callback_ftype
*callback
,
1001 struct program_space
*search_pspace
,
1004 struct objfile
*objfile
;
1005 struct program_space
*pspace
;
1006 struct symbol_matcher_data matcher_data
;
1008 matcher_data
.lookup_name
= name
;
1009 matcher_data
.symbol_name_cmp
=
1010 state
->language
->la_get_symbol_name_cmp
!= NULL
1011 ? state
->language
->la_get_symbol_name_cmp (name
)
1014 ALL_PSPACES (pspace
)
1016 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
1018 if (pspace
->executing_startup
)
1021 set_current_program_space (pspace
);
1023 ALL_OBJFILES (objfile
)
1025 struct compunit_symtab
*cu
;
1028 objfile
->sf
->qf
->expand_symtabs_matching (objfile
, NULL
,
1029 iterate_name_matcher
,
1033 ALL_OBJFILE_COMPUNITS (objfile
, cu
)
1035 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1037 iterate_over_file_blocks (symtab
, name
, domain
, callback
, data
);
1041 struct symbol_and_data_callback cad
= { callback
, data
};
1042 struct block
*block
;
1045 for (i
= FIRST_LOCAL_BLOCK
;
1046 i
< BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab
));
1049 block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), i
);
1050 state
->language
->la_iterate_over_symbols
1051 (block
, name
, domain
, iterate_inline_only
, &cad
);
1059 /* Returns the block to be used for symbol searches from
1060 the current location. */
1062 static const struct block
*
1063 get_current_search_block (void)
1065 const struct block
*block
;
1066 enum language save_language
;
1068 /* get_selected_block can change the current language when there is
1069 no selected frame yet. */
1070 save_language
= current_language
->la_language
;
1071 block
= get_selected_block (0);
1072 set_language (save_language
);
1077 /* Iterate over static and global blocks. */
1080 iterate_over_file_blocks (struct symtab
*symtab
,
1081 const char *name
, domain_enum domain
,
1082 symbol_found_callback_ftype
*callback
, void *data
)
1084 struct block
*block
;
1086 for (block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1088 block
= BLOCK_SUPERBLOCK (block
))
1089 LA_ITERATE_OVER_SYMBOLS (block
, name
, domain
, callback
, data
);
1092 /* A helper for find_method. This finds all methods in type T which
1093 match NAME. It adds matching symbol names to RESULT_NAMES, and
1094 adds T's direct superclasses to SUPERCLASSES. */
1097 find_methods (struct type
*t
, const char *name
,
1098 VEC (const_char_ptr
) **result_names
,
1099 VEC (typep
) **superclasses
)
1102 const char *class_name
= type_name_no_tag (t
);
1104 /* Ignore this class if it doesn't have a name. This is ugly, but
1105 unless we figure out how to get the physname without the name of
1106 the class, then the loop can't do any good. */
1113 /* Loop over each method name. At this level, all overloads of a name
1114 are counted as a single name. There is an inner loop which loops over
1117 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1118 method_counter
>= 0;
1121 const char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1122 char dem_opname
[64];
1124 if (startswith (method_name
, "__") ||
1125 startswith (method_name
, "op") ||
1126 startswith (method_name
, "type"))
1128 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
1129 method_name
= dem_opname
;
1130 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
1131 method_name
= dem_opname
;
1134 if (strcmp_iw (method_name
, name
) == 0)
1138 for (field_counter
= (TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
)
1144 const char *phys_name
;
1146 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1147 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1149 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1150 VEC_safe_push (const_char_ptr
, *result_names
, phys_name
);
1156 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1157 VEC_safe_push (typep
, *superclasses
, TYPE_BASECLASS (t
, ibase
));
1160 /* Find an instance of the character C in the string S that is outside
1161 of all parenthesis pairs, single-quoted strings, and double-quoted
1162 strings. Also, ignore the char within a template name, like a ','
1163 within foo<int, int>. */
1166 find_toplevel_char (const char *s
, char c
)
1168 int quoted
= 0; /* zero if we're not in quotes;
1169 '"' if we're in a double-quoted string;
1170 '\'' if we're in a single-quoted string. */
1171 int depth
= 0; /* Number of unclosed parens we've seen. */
1174 for (scan
= s
; *scan
; scan
++)
1178 if (*scan
== quoted
)
1180 else if (*scan
== '\\' && *(scan
+ 1))
1183 else if (*scan
== c
&& ! quoted
&& depth
== 0)
1185 else if (*scan
== '"' || *scan
== '\'')
1187 else if (*scan
== '(' || *scan
== '<')
1189 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
1196 /* The string equivalent of find_toplevel_char. Returns a pointer
1197 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1198 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
1201 find_toplevel_string (const char *haystack
, const char *needle
)
1203 const char *s
= haystack
;
1207 s
= find_toplevel_char (s
, *needle
);
1211 /* Found first char in HAYSTACK; check rest of string. */
1212 if (startswith (s
, needle
))
1215 /* Didn't find it; loop over HAYSTACK, looking for the next
1216 instance of the first character of NEEDLE. */
1220 while (s
!= NULL
&& *s
!= '\0');
1222 /* NEEDLE was not found in HAYSTACK. */
1226 /* Convert CANONICAL to its string representation using
1227 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1230 canonical_to_fullform (const struct linespec_canonical_name
*canonical
)
1232 if (canonical
->symtab
== NULL
)
1233 return xstrdup (canonical
->suffix
);
1235 return xstrprintf ("%s:%s", symtab_to_fullname (canonical
->symtab
),
1239 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1240 and store the result in SELF->CANONICAL. */
1243 filter_results (struct linespec_state
*self
,
1244 struct symtabs_and_lines
*result
,
1245 VEC (const_char_ptr
) *filters
)
1250 for (i
= 0; VEC_iterate (const_char_ptr
, filters
, i
, name
); ++i
)
1252 struct linespec_sals lsal
;
1255 memset (&lsal
, 0, sizeof (lsal
));
1257 for (j
= 0; j
< result
->nelts
; ++j
)
1259 const struct linespec_canonical_name
*canonical
;
1261 struct cleanup
*cleanup
;
1263 canonical
= &self
->canonical_names
[j
];
1264 fullform
= canonical_to_fullform (canonical
);
1265 cleanup
= make_cleanup (xfree
, fullform
);
1267 if (strcmp (name
, fullform
) == 0)
1268 add_sal_to_sals_basic (&lsal
.sals
, &result
->sals
[j
]);
1270 do_cleanups (cleanup
);
1273 if (lsal
.sals
.nelts
> 0)
1275 lsal
.canonical
= xstrdup (name
);
1276 VEC_safe_push (linespec_sals
, self
->canonical
->sals
, &lsal
);
1280 self
->canonical
->pre_expanded
= 0;
1283 /* Store RESULT into SELF->CANONICAL. */
1286 convert_results_to_lsals (struct linespec_state
*self
,
1287 struct symtabs_and_lines
*result
)
1289 struct linespec_sals lsal
;
1291 lsal
.canonical
= NULL
;
1292 lsal
.sals
= *result
;
1293 VEC_safe_push (linespec_sals
, self
->canonical
->sals
, &lsal
);
1296 /* A structure that contains two string representations of a struct
1297 linespec_canonical_name:
1298 - one where the the symtab's fullname is used;
1299 - one where the filename followed the "set filename-display"
1302 struct decode_line_2_item
1304 /* The form using symtab_to_fullname.
1305 It must be xfree'ed after use. */
1308 /* The form using symtab_to_filename_for_display.
1309 It must be xfree'ed after use. */
1312 /* Field is initialized to zero and it is set to one if the user
1313 requested breakpoint for this entry. */
1314 unsigned int selected
: 1;
1317 /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1318 secondarily by FULLFORM. */
1321 decode_line_2_compare_items (const void *ap
, const void *bp
)
1323 const struct decode_line_2_item
*a
= ap
;
1324 const struct decode_line_2_item
*b
= bp
;
1327 retval
= strcmp (a
->displayform
, b
->displayform
);
1331 return strcmp (a
->fullform
, b
->fullform
);
1334 /* Handle multiple results in RESULT depending on SELECT_MODE. This
1335 will either return normally, throw an exception on multiple
1336 results, or present a menu to the user. On return, the SALS vector
1337 in SELF->CANONICAL is set up properly. */
1340 decode_line_2 (struct linespec_state
*self
,
1341 struct symtabs_and_lines
*result
,
1342 const char *select_mode
)
1344 char *args
, *prompt
;
1346 struct cleanup
*old_chain
;
1347 VEC (const_char_ptr
) *filters
= NULL
;
1348 struct get_number_or_range_state state
;
1349 struct decode_line_2_item
*items
;
1352 gdb_assert (select_mode
!= multiple_symbols_all
);
1353 gdb_assert (self
->canonical
!= NULL
);
1354 gdb_assert (result
->nelts
>= 1);
1356 old_chain
= make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
1358 /* Prepare ITEMS array. */
1359 items_count
= result
->nelts
;
1360 items
= xmalloc (sizeof (*items
) * items_count
);
1361 make_cleanup (xfree
, items
);
1362 for (i
= 0; i
< items_count
; ++i
)
1364 const struct linespec_canonical_name
*canonical
;
1365 struct decode_line_2_item
*item
;
1367 canonical
= &self
->canonical_names
[i
];
1368 gdb_assert (canonical
->suffix
!= NULL
);
1371 item
->fullform
= canonical_to_fullform (canonical
);
1372 make_cleanup (xfree
, item
->fullform
);
1374 if (canonical
->symtab
== NULL
)
1375 item
->displayform
= canonical
->suffix
;
1378 const char *fn_for_display
;
1380 fn_for_display
= symtab_to_filename_for_display (canonical
->symtab
);
1381 item
->displayform
= xstrprintf ("%s:%s", fn_for_display
,
1383 make_cleanup (xfree
, item
->displayform
);
1389 /* Sort the list of method names. */
1390 qsort (items
, items_count
, sizeof (*items
), decode_line_2_compare_items
);
1392 /* Remove entries with the same FULLFORM. */
1393 if (items_count
>= 2)
1395 struct decode_line_2_item
*dst
, *src
;
1398 for (src
= &items
[1]; src
< &items
[items_count
]; src
++)
1399 if (strcmp (src
->fullform
, dst
->fullform
) != 0)
1401 items_count
= dst
+ 1 - items
;
1404 if (select_mode
== multiple_symbols_cancel
&& items_count
> 1)
1405 error (_("canceled because the command is ambiguous\n"
1406 "See set/show multiple-symbol."));
1408 if (select_mode
== multiple_symbols_all
|| items_count
== 1)
1410 do_cleanups (old_chain
);
1411 convert_results_to_lsals (self
, result
);
1415 printf_unfiltered (_("[0] cancel\n[1] all\n"));
1416 for (i
= 0; i
< items_count
; i
++)
1417 printf_unfiltered ("[%d] %s\n", i
+ 2, items
[i
].displayform
);
1419 prompt
= getenv ("PS2");
1424 args
= command_line_input (prompt
, 0, "overload-choice");
1426 if (args
== 0 || *args
== 0)
1427 error_no_arg (_("one or more choice numbers"));
1429 init_number_or_range (&state
, args
);
1430 while (!state
.finished
)
1434 num
= get_number_or_range (&state
);
1437 error (_("canceled"));
1440 /* We intentionally make this result in a single breakpoint,
1441 contrary to what older versions of gdb did. The
1442 rationale is that this lets a user get the
1443 multiple_symbols_all behavior even with the 'ask'
1444 setting; and he can get separate breakpoints by entering
1445 "2-57" at the query. */
1446 do_cleanups (old_chain
);
1447 convert_results_to_lsals (self
, result
);
1452 if (num
>= items_count
)
1453 printf_unfiltered (_("No choice number %d.\n"), num
);
1456 struct decode_line_2_item
*item
= &items
[num
];
1458 if (!item
->selected
)
1460 VEC_safe_push (const_char_ptr
, filters
, item
->fullform
);
1465 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1471 filter_results (self
, result
, filters
);
1472 do_cleanups (old_chain
);
1477 /* The parser of linespec itself. */
1479 /* Throw an appropriate error when SYMBOL is not found (optionally in
1482 static void ATTRIBUTE_NORETURN
1483 symbol_not_found_error (const char *symbol
, const char *filename
)
1488 if (!have_full_symbols ()
1489 && !have_partial_symbols ()
1490 && !have_minimal_symbols ())
1491 throw_error (NOT_FOUND_ERROR
,
1492 _("No symbol table is loaded. Use the \"file\" command."));
1494 /* If SYMBOL starts with '$', the user attempted to either lookup
1495 a function/variable in his code starting with '$' or an internal
1496 variable of that name. Since we do not know which, be concise and
1497 explain both possibilities. */
1501 throw_error (NOT_FOUND_ERROR
,
1502 _("Undefined convenience variable or function \"%s\" "
1503 "not defined in \"%s\"."), symbol
, filename
);
1505 throw_error (NOT_FOUND_ERROR
,
1506 _("Undefined convenience variable or function \"%s\" "
1507 "not defined."), symbol
);
1512 throw_error (NOT_FOUND_ERROR
,
1513 _("Function \"%s\" not defined in \"%s\"."),
1516 throw_error (NOT_FOUND_ERROR
,
1517 _("Function \"%s\" not defined."), symbol
);
1521 /* Throw an appropriate error when an unexpected token is encountered
1524 static void ATTRIBUTE_NORETURN
1525 unexpected_linespec_error (linespec_parser
*parser
)
1527 linespec_token token
;
1528 static const char * token_type_strings
[]
1529 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1531 /* Get the token that generated the error. */
1532 token
= linespec_lexer_lex_one (parser
);
1534 /* Finally, throw the error. */
1535 if (token
.type
== LSTOKEN_STRING
|| token
.type
== LSTOKEN_NUMBER
1536 || token
.type
== LSTOKEN_KEYWORD
)
1539 struct cleanup
*cleanup
;
1541 string
= copy_token_string (token
);
1542 cleanup
= make_cleanup (xfree
, string
);
1543 throw_error (GENERIC_ERROR
,
1544 _("malformed linespec error: unexpected %s, \"%s\""),
1545 token_type_strings
[token
.type
], string
);
1548 throw_error (GENERIC_ERROR
,
1549 _("malformed linespec error: unexpected %s"),
1550 token_type_strings
[token
.type
]);
1553 /* Parse and return a line offset in STRING. */
1555 static struct line_offset
1556 linespec_parse_line_offset (const char *string
)
1558 struct line_offset line_offset
= {0, LINE_OFFSET_NONE
};
1562 line_offset
.sign
= LINE_OFFSET_PLUS
;
1565 else if (*string
== '-')
1567 line_offset
.sign
= LINE_OFFSET_MINUS
;
1571 /* Right now, we only allow base 10 for offsets. */
1572 line_offset
.offset
= atoi (string
);
1576 /* Parse the basic_spec in PARSER's input. */
1579 linespec_parse_basic (linespec_parser
*parser
)
1582 linespec_token token
;
1583 VEC (symbolp
) *symbols
, *labels
;
1584 VEC (bound_minimal_symbol_d
) *minimal_symbols
;
1585 struct cleanup
*cleanup
;
1587 /* Get the next token. */
1588 token
= linespec_lexer_lex_one (parser
);
1590 /* If it is EOI or KEYWORD, issue an error. */
1591 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1592 unexpected_linespec_error (parser
);
1593 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1594 else if (token
.type
== LSTOKEN_NUMBER
)
1596 /* Record the line offset and get the next token. */
1597 name
= copy_token_string (token
);
1598 cleanup
= make_cleanup (xfree
, name
);
1599 PARSER_RESULT (parser
)->line_offset
= linespec_parse_line_offset (name
);
1600 do_cleanups (cleanup
);
1602 /* Get the next token. */
1603 token
= linespec_lexer_consume_token (parser
);
1605 /* If the next token is a comma, stop parsing and return. */
1606 if (token
.type
== LSTOKEN_COMMA
)
1609 /* If the next token is anything but EOI or KEYWORD, issue
1611 if (token
.type
!= LSTOKEN_KEYWORD
&& token
.type
!= LSTOKEN_EOI
)
1612 unexpected_linespec_error (parser
);
1615 if (token
.type
== LSTOKEN_KEYWORD
|| token
.type
== LSTOKEN_EOI
)
1618 /* Next token must be LSTOKEN_STRING. */
1619 if (token
.type
!= LSTOKEN_STRING
)
1620 unexpected_linespec_error (parser
);
1622 /* The current token will contain the name of a function, method,
1624 name
= copy_token_string (token
);
1625 cleanup
= make_cleanup (xfree
, name
);
1627 /* Try looking it up as a function/method. */
1628 find_linespec_symbols (PARSER_STATE (parser
),
1629 PARSER_RESULT (parser
)->file_symtabs
, name
,
1630 &symbols
, &minimal_symbols
);
1632 if (symbols
!= NULL
|| minimal_symbols
!= NULL
)
1634 PARSER_RESULT (parser
)->function_symbols
= symbols
;
1635 PARSER_RESULT (parser
)->minimal_symbols
= minimal_symbols
;
1636 PARSER_RESULT (parser
)->function_name
= name
;
1638 discard_cleanups (cleanup
);
1642 /* NAME was not a function or a method. So it must be a label
1643 name or user specified variable like "break foo.c:$zippo". */
1644 labels
= find_label_symbols (PARSER_STATE (parser
), NULL
,
1648 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1649 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1650 PARSER_RESULT (parser
)->label_name
= name
;
1652 discard_cleanups (cleanup
);
1654 else if (token
.type
== LSTOKEN_STRING
1655 && *LS_TOKEN_STOKEN (token
).ptr
== '$')
1657 /* User specified a convenience variable or history value. */
1658 PARSER_RESULT (parser
)->line_offset
1659 = linespec_parse_variable (PARSER_STATE (parser
), name
);
1661 if (PARSER_RESULT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
)
1663 /* The user-specified variable was not valid. Do not
1664 throw an error here. parse_linespec will do it for us. */
1665 PARSER_RESULT (parser
)->function_name
= name
;
1666 discard_cleanups (cleanup
);
1670 /* The convenience variable/history value parsed correctly.
1671 NAME is no longer needed. */
1672 do_cleanups (cleanup
);
1676 /* The name is also not a label. Abort parsing. Do not throw
1677 an error here. parse_linespec will do it for us. */
1679 /* Save a copy of the name we were trying to lookup. */
1680 PARSER_RESULT (parser
)->function_name
= name
;
1681 discard_cleanups (cleanup
);
1686 /* Get the next token. */
1687 token
= linespec_lexer_consume_token (parser
);
1689 if (token
.type
== LSTOKEN_COLON
)
1691 /* User specified a label or a lineno. */
1692 token
= linespec_lexer_consume_token (parser
);
1694 if (token
.type
== LSTOKEN_NUMBER
)
1696 /* User specified an offset. Record the line offset and
1697 get the next token. */
1698 name
= copy_token_string (token
);
1699 cleanup
= make_cleanup (xfree
, name
);
1700 PARSER_RESULT (parser
)->line_offset
1701 = linespec_parse_line_offset (name
);
1702 do_cleanups (cleanup
);
1704 /* Ge the next token. */
1705 token
= linespec_lexer_consume_token (parser
);
1707 else if (token
.type
== LSTOKEN_STRING
)
1709 /* Grab a copy of the label's name and look it up. */
1710 name
= copy_token_string (token
);
1711 cleanup
= make_cleanup (xfree
, name
);
1712 labels
= find_label_symbols (PARSER_STATE (parser
),
1713 PARSER_RESULT (parser
)->function_symbols
,
1718 PARSER_RESULT (parser
)->labels
.label_symbols
= labels
;
1719 PARSER_RESULT (parser
)->labels
.function_symbols
= symbols
;
1720 PARSER_RESULT (parser
)->label_name
= name
;
1722 discard_cleanups (cleanup
);
1726 /* We don't know what it was, but it isn't a label. */
1727 throw_error (NOT_FOUND_ERROR
,
1728 _("No label \"%s\" defined in function \"%s\"."),
1729 name
, PARSER_RESULT (parser
)->function_name
);
1732 /* Check for a line offset. */
1733 token
= linespec_lexer_consume_token (parser
);
1734 if (token
.type
== LSTOKEN_COLON
)
1736 /* Get the next token. */
1737 token
= linespec_lexer_consume_token (parser
);
1739 /* It must be a line offset. */
1740 if (token
.type
!= LSTOKEN_NUMBER
)
1741 unexpected_linespec_error (parser
);
1743 /* Record the lione offset and get the next token. */
1744 name
= copy_token_string (token
);
1745 cleanup
= make_cleanup (xfree
, name
);
1747 PARSER_RESULT (parser
)->line_offset
1748 = linespec_parse_line_offset (name
);
1749 do_cleanups (cleanup
);
1751 /* Get the next token. */
1752 token
= linespec_lexer_consume_token (parser
);
1757 /* Trailing ':' in the input. Issue an error. */
1758 unexpected_linespec_error (parser
);
1763 /* Canonicalize the linespec contained in LS. The result is saved into
1764 STATE->canonical. */
1767 canonicalize_linespec (struct linespec_state
*state
, linespec_p ls
)
1769 /* If canonicalization was not requested, no need to do anything. */
1770 if (!state
->canonical
)
1773 /* Shortcut expressions, which can only appear by themselves. */
1774 if (ls
->expression
!= NULL
)
1775 state
->canonical
->addr_string
= xstrdup (ls
->expression
);
1778 struct ui_file
*buf
;
1781 buf
= mem_fileopen ();
1782 if (ls
->source_filename
)
1784 fputs_unfiltered (ls
->source_filename
, buf
);
1788 if (ls
->function_name
)
1791 fputc_unfiltered (':', buf
);
1792 fputs_unfiltered (ls
->function_name
, buf
);
1799 fputc_unfiltered (':', buf
);
1801 if (ls
->function_name
== NULL
)
1805 /* No function was specified, so add the symbol name. */
1806 gdb_assert (ls
->labels
.function_symbols
!= NULL
1807 && (VEC_length (symbolp
, ls
->labels
.function_symbols
)
1809 s
= VEC_index (symbolp
, ls
->labels
.function_symbols
, 0);
1810 fputs_unfiltered (SYMBOL_NATURAL_NAME (s
), buf
);
1811 fputc_unfiltered (':', buf
);
1814 fputs_unfiltered (ls
->label_name
, buf
);
1816 state
->canonical
->special_display
= 1;
1819 if (ls
->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
1822 fputc_unfiltered (':', buf
);
1823 fprintf_filtered (buf
, "%s%d",
1824 (ls
->line_offset
.sign
== LINE_OFFSET_NONE
? ""
1825 : (ls
->line_offset
.sign
1826 == LINE_OFFSET_PLUS
? "+" : "-")),
1827 ls
->line_offset
.offset
);
1830 state
->canonical
->addr_string
= ui_file_xstrdup (buf
, NULL
);
1831 ui_file_delete (buf
);
1835 /* Given a line offset in LS, construct the relevant SALs. */
1837 static struct symtabs_and_lines
1838 create_sals_line_offset (struct linespec_state
*self
,
1841 struct symtabs_and_lines values
;
1842 struct symtab_and_line val
;
1843 int use_default
= 0;
1849 /* This is where we need to make sure we have good defaults.
1850 We must guarantee that this section of code is never executed
1851 when we are called with just a function name, since
1852 set_default_source_symtab_and_line uses
1853 select_source_symtab that calls us with such an argument. */
1855 if (VEC_length (symtab_ptr
, ls
->file_symtabs
) == 1
1856 && VEC_index (symtab_ptr
, ls
->file_symtabs
, 0) == NULL
)
1858 const char *fullname
;
1860 set_current_program_space (self
->program_space
);
1862 /* Make sure we have at least a default source line. */
1863 set_default_source_symtab_and_line ();
1864 initialize_defaults (&self
->default_symtab
, &self
->default_line
);
1865 fullname
= symtab_to_fullname (self
->default_symtab
);
1866 VEC_pop (symtab_ptr
, ls
->file_symtabs
);
1867 VEC_free (symtab_ptr
, ls
->file_symtabs
);
1868 ls
->file_symtabs
= collect_symtabs_from_filename (fullname
);
1872 val
.line
= ls
->line_offset
.offset
;
1873 switch (ls
->line_offset
.sign
)
1875 case LINE_OFFSET_PLUS
:
1876 if (ls
->line_offset
.offset
== 0)
1879 val
.line
= self
->default_line
+ val
.line
;
1882 case LINE_OFFSET_MINUS
:
1883 if (ls
->line_offset
.offset
== 0)
1886 val
.line
= self
->default_line
- val
.line
;
1888 val
.line
= -val
.line
;
1891 case LINE_OFFSET_NONE
:
1892 break; /* No need to adjust val.line. */
1895 if (self
->list_mode
)
1896 decode_digits_list_mode (self
, ls
, &values
, val
);
1899 struct linetable_entry
*best_entry
= NULL
;
1901 const struct block
**blocks
;
1902 struct cleanup
*cleanup
;
1903 struct symtabs_and_lines intermediate_results
;
1906 intermediate_results
.sals
= NULL
;
1907 intermediate_results
.nelts
= 0;
1909 decode_digits_ordinary (self
, ls
, val
.line
, &intermediate_results
,
1911 if (intermediate_results
.nelts
== 0 && best_entry
!= NULL
)
1912 decode_digits_ordinary (self
, ls
, best_entry
->line
,
1913 &intermediate_results
, &best_entry
);
1915 cleanup
= make_cleanup (xfree
, intermediate_results
.sals
);
1917 /* For optimized code, the compiler can scatter one source line
1918 across disjoint ranges of PC values, even when no duplicate
1919 functions or inline functions are involved. For example,
1920 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
1921 function can result in two PC ranges. In this case, we don't
1922 want to set a breakpoint on the first PC of each range. To filter
1923 such cases, we use containing blocks -- for each PC found
1924 above, we see if there are other PCs that are in the same
1925 block. If yes, the other PCs are filtered out. */
1927 filter
= XNEWVEC (int, intermediate_results
.nelts
);
1928 make_cleanup (xfree
, filter
);
1929 blocks
= XNEWVEC (const struct block
*, intermediate_results
.nelts
);
1930 make_cleanup (xfree
, blocks
);
1932 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1934 set_current_program_space (intermediate_results
.sals
[i
].pspace
);
1937 blocks
[i
] = block_for_pc_sect (intermediate_results
.sals
[i
].pc
,
1938 intermediate_results
.sals
[i
].section
);
1941 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1943 if (blocks
[i
] != NULL
)
1944 for (j
= i
+ 1; j
< intermediate_results
.nelts
; ++j
)
1946 if (blocks
[j
] == blocks
[i
])
1954 for (i
= 0; i
< intermediate_results
.nelts
; ++i
)
1957 struct symbol
*sym
= (blocks
[i
]
1958 ? block_containing_function (blocks
[i
])
1961 if (self
->funfirstline
)
1962 skip_prologue_sal (&intermediate_results
.sals
[i
]);
1963 /* Make sure the line matches the request, not what was
1965 intermediate_results
.sals
[i
].line
= val
.line
;
1966 add_sal_to_sals (self
, &values
, &intermediate_results
.sals
[i
],
1967 sym
? SYMBOL_NATURAL_NAME (sym
) : NULL
, 0);
1970 do_cleanups (cleanup
);
1973 if (values
.nelts
== 0)
1975 if (ls
->source_filename
)
1976 throw_error (NOT_FOUND_ERROR
, _("No line %d in file \"%s\"."),
1977 val
.line
, ls
->source_filename
);
1979 throw_error (NOT_FOUND_ERROR
, _("No line %d in the current file."),
1986 /* Create and return SALs from the linespec LS. */
1988 static struct symtabs_and_lines
1989 convert_linespec_to_sals (struct linespec_state
*state
, linespec_p ls
)
1991 struct symtabs_and_lines sals
= {NULL
, 0};
1993 if (ls
->expression
!= NULL
)
1995 struct symtab_and_line sal
;
1997 /* We have an expression. No other attribute is allowed. */
1998 sal
= find_pc_line (ls
->expr_pc
, 0);
1999 sal
.pc
= ls
->expr_pc
;
2000 sal
.section
= find_pc_overlay (ls
->expr_pc
);
2001 sal
.explicit_pc
= 1;
2002 add_sal_to_sals (state
, &sals
, &sal
, ls
->expression
, 1);
2004 else if (ls
->labels
.label_symbols
!= NULL
)
2006 /* We have just a bunch of functions/methods or labels. */
2008 struct symtab_and_line sal
;
2011 for (i
= 0; VEC_iterate (symbolp
, ls
->labels
.label_symbols
, i
, sym
); ++i
)
2013 struct program_space
*pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2015 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
2016 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2017 add_sal_to_sals (state
, &sals
, &sal
,
2018 SYMBOL_NATURAL_NAME (sym
), 0);
2021 else if (ls
->function_symbols
!= NULL
|| ls
->minimal_symbols
!= NULL
)
2023 /* We have just a bunch of functions and/or methods. */
2025 struct symtab_and_line sal
;
2027 bound_minimal_symbol_d
*elem
;
2028 struct program_space
*pspace
;
2030 if (ls
->function_symbols
!= NULL
)
2032 /* Sort symbols so that symbols with the same program space are next
2034 qsort (VEC_address (symbolp
, ls
->function_symbols
),
2035 VEC_length (symbolp
, ls
->function_symbols
),
2036 sizeof (symbolp
), compare_symbols
);
2038 for (i
= 0; VEC_iterate (symbolp
, ls
->function_symbols
, i
, sym
); ++i
)
2040 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2041 set_current_program_space (pspace
);
2042 if (symbol_to_sal (&sal
, state
->funfirstline
, sym
)
2043 && maybe_add_address (state
->addr_set
, pspace
, sal
.pc
))
2044 add_sal_to_sals (state
, &sals
, &sal
,
2045 SYMBOL_NATURAL_NAME (sym
), 0);
2049 if (ls
->minimal_symbols
!= NULL
)
2051 /* Sort minimal symbols by program space, too. */
2052 qsort (VEC_address (bound_minimal_symbol_d
, ls
->minimal_symbols
),
2053 VEC_length (bound_minimal_symbol_d
, ls
->minimal_symbols
),
2054 sizeof (bound_minimal_symbol_d
), compare_msymbols
);
2057 VEC_iterate (bound_minimal_symbol_d
, ls
->minimal_symbols
,
2061 pspace
= elem
->objfile
->pspace
;
2062 set_current_program_space (pspace
);
2063 minsym_found (state
, elem
->objfile
, elem
->minsym
, &sals
);
2067 else if (ls
->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2069 /* Only an offset was specified. */
2070 sals
= create_sals_line_offset (state
, ls
);
2072 /* Make sure we have a filename for canonicalization. */
2073 if (ls
->source_filename
== NULL
)
2075 const char *fullname
= symtab_to_fullname (state
->default_symtab
);
2077 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2078 form so that displaying SOURCE_FILENAME can follow the current
2079 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2080 it has been kept for code simplicity only in absolute form. */
2081 ls
->source_filename
= xstrdup (fullname
);
2086 /* We haven't found any results... */
2090 canonicalize_linespec (state
, ls
);
2092 if (sals
.nelts
> 0 && state
->canonical
!= NULL
)
2093 state
->canonical
->pre_expanded
= 1;
2098 /* Parse a string that specifies a linespec.
2099 Pass the address of a char * variable; that variable will be
2100 advanced over the characters actually parsed.
2102 The basic grammar of linespecs:
2104 linespec -> expr_spec | var_spec | basic_spec
2105 expr_spec -> '*' STRING
2106 var_spec -> '$' (STRING | NUMBER)
2108 basic_spec -> file_offset_spec | function_spec | label_spec
2109 file_offset_spec -> opt_file_spec offset_spec
2110 function_spec -> opt_file_spec function_name_spec opt_label_spec
2111 label_spec -> label_name_spec
2113 opt_file_spec -> "" | file_name_spec ':'
2114 opt_label_spec -> "" | ':' label_name_spec
2116 file_name_spec -> STRING
2117 function_name_spec -> STRING
2118 label_name_spec -> STRING
2119 function_name_spec -> STRING
2120 offset_spec -> NUMBER
2124 This may all be followed by several keywords such as "if EXPR",
2127 A comma will terminate parsing.
2129 The function may be an undebuggable function found in minimal symbol table.
2131 If the argument FUNFIRSTLINE is nonzero, we want the first line
2132 of real code inside a function when a function is specified, and it is
2133 not OK to specify a variable or type to get its line number.
2135 DEFAULT_SYMTAB specifies the file to use if none is specified.
2136 It defaults to current_source_symtab.
2137 DEFAULT_LINE specifies the line number to use for relative
2138 line numbers (that start with signs). Defaults to current_source_line.
2139 If CANONICAL is non-NULL, store an array of strings containing the canonical
2140 line specs there if necessary. Currently overloaded member functions and
2141 line numbers or static functions without a filename yield a canonical
2142 line spec. The array and the line spec strings are allocated on the heap,
2143 it is the callers responsibility to free them.
2145 Note that it is possible to return zero for the symtab
2146 if no file is validly specified. Callers must check that.
2147 Also, the line number returned may be invalid. */
2149 /* Parse the linespec in ARGPTR. */
2151 static struct symtabs_and_lines
2152 parse_linespec (linespec_parser
*parser
, const char **argptr
)
2154 linespec_token token
;
2155 struct symtabs_and_lines values
;
2156 volatile struct gdb_exception file_exception
;
2157 struct cleanup
*cleanup
;
2159 /* A special case to start. It has become quite popular for
2160 IDEs to work around bugs in the previous parser by quoting
2161 the entire linespec, so we attempt to deal with this nicely. */
2162 parser
->is_quote_enclosed
= 0;
2163 if (!is_ada_operator (*argptr
)
2164 && strchr (linespec_quote_characters
, **argptr
) != NULL
)
2168 end
= skip_quote_char (*argptr
+ 1, **argptr
);
2169 if (end
!= NULL
&& is_closing_quote_enclosed (end
))
2171 /* Here's the special case. Skip ARGPTR past the initial
2174 parser
->is_quote_enclosed
= 1;
2178 /* A keyword at the start cannot be interpreted as such.
2179 Consider "b thread thread 42". */
2180 parser
->keyword_ok
= 0;
2182 parser
->lexer
.saved_arg
= *argptr
;
2183 parser
->lexer
.stream
= argptr
;
2184 file_exception
.reason
= 0;
2186 /* Initialize the default symtab and line offset. */
2187 initialize_defaults (&PARSER_STATE (parser
)->default_symtab
,
2188 &PARSER_STATE (parser
)->default_line
);
2190 /* Objective-C shortcut. */
2191 values
= decode_objc (PARSER_STATE (parser
), PARSER_RESULT (parser
), argptr
);
2192 if (values
.sals
!= NULL
)
2195 /* Start parsing. */
2197 /* Get the first token. */
2198 token
= linespec_lexer_lex_one (parser
);
2200 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2201 if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '*')
2206 /* User specified an expression, *EXPR. */
2207 copy
= expr
= copy_token_string (token
);
2208 cleanup
= make_cleanup (xfree
, expr
);
2209 PARSER_RESULT (parser
)->expr_pc
= linespec_expression_to_pc (©
);
2210 discard_cleanups (cleanup
);
2211 PARSER_RESULT (parser
)->expression
= expr
;
2213 /* This is a little hacky/tricky. If linespec_expression_to_pc
2214 did not evaluate the entire token, then we must find the
2215 string COPY inside the original token buffer. */
2218 PARSER_STREAM (parser
) = strstr (parser
->lexer
.saved_arg
, copy
);
2219 gdb_assert (PARSER_STREAM (parser
) != NULL
);
2222 /* Consume the token. */
2223 linespec_lexer_consume_token (parser
);
2225 goto convert_to_sals
;
2227 else if (token
.type
== LSTOKEN_STRING
&& *LS_TOKEN_STOKEN (token
).ptr
== '$')
2231 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2232 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2234 /* User specified a convenience variable or history value. */
2235 var
= copy_token_string (token
);
2236 cleanup
= make_cleanup (xfree
, var
);
2237 PARSER_RESULT (parser
)->line_offset
2238 = linespec_parse_variable (PARSER_STATE (parser
), var
);
2239 do_cleanups (cleanup
);
2241 /* If a line_offset wasn't found (VAR is the name of a user
2242 variable/function), then skip to normal symbol processing. */
2243 if (PARSER_RESULT (parser
)->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
2245 /* Consume this token. */
2246 linespec_lexer_consume_token (parser
);
2248 goto convert_to_sals
;
2251 else if (token
.type
!= LSTOKEN_STRING
&& token
.type
!= LSTOKEN_NUMBER
)
2252 unexpected_linespec_error (parser
);
2254 /* Now we can recognize keywords. */
2255 parser
->keyword_ok
= 1;
2257 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2258 this token cannot represent a filename. */
2259 token
= linespec_lexer_peek_token (parser
);
2261 if (token
.type
== LSTOKEN_COLON
)
2263 char *user_filename
;
2265 /* Get the current token again and extract the filename. */
2266 token
= linespec_lexer_lex_one (parser
);
2267 user_filename
= copy_token_string (token
);
2269 /* Check if the input is a filename. */
2270 TRY_CATCH (file_exception
, RETURN_MASK_ERROR
)
2272 PARSER_RESULT (parser
)->file_symtabs
2273 = symtabs_from_filename (user_filename
);
2276 if (file_exception
.reason
>= 0)
2278 /* Symtabs were found for the file. Record the filename. */
2279 PARSER_RESULT (parser
)->source_filename
= user_filename
;
2281 /* Get the next token. */
2282 token
= linespec_lexer_consume_token (parser
);
2284 /* This is LSTOKEN_COLON; consume it. */
2285 linespec_lexer_consume_token (parser
);
2289 /* No symtabs found -- discard user_filename. */
2290 xfree (user_filename
);
2292 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2293 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2296 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2297 else if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
2298 && token
.type
!= LSTOKEN_COMMA
)
2300 /* TOKEN is the _next_ token, not the one currently in the parser.
2301 Consuming the token will give the correct error message. */
2302 linespec_lexer_consume_token (parser
);
2303 unexpected_linespec_error (parser
);
2307 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2308 VEC_safe_push (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
, NULL
);
2311 /* Parse the rest of the linespec. */
2312 linespec_parse_basic (parser
);
2314 if (PARSER_RESULT (parser
)->function_symbols
== NULL
2315 && PARSER_RESULT (parser
)->labels
.label_symbols
== NULL
2316 && PARSER_RESULT (parser
)->line_offset
.sign
== LINE_OFFSET_UNKNOWN
2317 && PARSER_RESULT (parser
)->minimal_symbols
== NULL
)
2319 /* The linespec didn't parse. Re-throw the file exception if
2321 if (file_exception
.reason
< 0)
2322 throw_exception (file_exception
);
2324 /* Otherwise, the symbol is not found. */
2325 symbol_not_found_error (PARSER_RESULT (parser
)->function_name
,
2326 PARSER_RESULT (parser
)->source_filename
);
2331 /* Get the last token and record how much of the input was parsed,
2333 token
= linespec_lexer_lex_one (parser
);
2334 if (token
.type
!= LSTOKEN_EOI
&& token
.type
!= LSTOKEN_KEYWORD
)
2335 PARSER_STREAM (parser
) = LS_TOKEN_STOKEN (token
).ptr
;
2337 /* Convert the data in PARSER_RESULT to SALs. */
2338 values
= convert_linespec_to_sals (PARSER_STATE (parser
),
2339 PARSER_RESULT (parser
));
2345 /* A constructor for linespec_state. */
2348 linespec_state_constructor (struct linespec_state
*self
,
2349 int flags
, const struct language_defn
*language
,
2350 struct symtab
*default_symtab
,
2352 struct linespec_result
*canonical
)
2354 memset (self
, 0, sizeof (*self
));
2355 self
->language
= language
;
2356 self
->funfirstline
= (flags
& DECODE_LINE_FUNFIRSTLINE
) ? 1 : 0;
2357 self
->list_mode
= (flags
& DECODE_LINE_LIST_MODE
) ? 1 : 0;
2358 self
->default_symtab
= default_symtab
;
2359 self
->default_line
= default_line
;
2360 self
->canonical
= canonical
;
2361 self
->program_space
= current_program_space
;
2362 self
->addr_set
= htab_create_alloc (10, hash_address_entry
, eq_address_entry
,
2363 xfree
, xcalloc
, xfree
);
2366 /* Initialize a new linespec parser. */
2369 linespec_parser_new (linespec_parser
*parser
,
2370 int flags
, const struct language_defn
*language
,
2371 struct symtab
*default_symtab
,
2373 struct linespec_result
*canonical
)
2375 parser
->lexer
.current
.type
= LSTOKEN_CONSUMED
;
2376 memset (PARSER_RESULT (parser
), 0, sizeof (struct linespec
));
2377 PARSER_RESULT (parser
)->line_offset
.sign
= LINE_OFFSET_UNKNOWN
;
2378 linespec_state_constructor (PARSER_STATE (parser
), flags
, language
,
2379 default_symtab
, default_line
, canonical
);
2382 /* A destructor for linespec_state. */
2385 linespec_state_destructor (struct linespec_state
*self
)
2387 htab_delete (self
->addr_set
);
2390 /* Delete a linespec parser. */
2393 linespec_parser_delete (void *arg
)
2395 linespec_parser
*parser
= (linespec_parser
*) arg
;
2397 xfree ((char *) PARSER_RESULT (parser
)->expression
);
2398 xfree ((char *) PARSER_RESULT (parser
)->source_filename
);
2399 xfree ((char *) PARSER_RESULT (parser
)->label_name
);
2400 xfree ((char *) PARSER_RESULT (parser
)->function_name
);
2402 if (PARSER_RESULT (parser
)->file_symtabs
!= NULL
)
2403 VEC_free (symtab_ptr
, PARSER_RESULT (parser
)->file_symtabs
);
2405 if (PARSER_RESULT (parser
)->function_symbols
!= NULL
)
2406 VEC_free (symbolp
, PARSER_RESULT (parser
)->function_symbols
);
2408 if (PARSER_RESULT (parser
)->minimal_symbols
!= NULL
)
2409 VEC_free (bound_minimal_symbol_d
, PARSER_RESULT (parser
)->minimal_symbols
);
2411 if (PARSER_RESULT (parser
)->labels
.label_symbols
!= NULL
)
2412 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.label_symbols
);
2414 if (PARSER_RESULT (parser
)->labels
.function_symbols
!= NULL
)
2415 VEC_free (symbolp
, PARSER_RESULT (parser
)->labels
.function_symbols
);
2417 linespec_state_destructor (PARSER_STATE (parser
));
2420 /* See linespec.h. */
2423 decode_line_full (char **argptr
, int flags
,
2424 struct symtab
*default_symtab
,
2425 int default_line
, struct linespec_result
*canonical
,
2426 const char *select_mode
,
2429 struct symtabs_and_lines result
;
2430 struct cleanup
*cleanups
;
2431 VEC (const_char_ptr
) *filters
= NULL
;
2432 linespec_parser parser
;
2433 struct linespec_state
*state
;
2434 const char *copy
, *orig
;
2436 gdb_assert (canonical
!= NULL
);
2437 /* The filter only makes sense for 'all'. */
2438 gdb_assert (filter
== NULL
|| select_mode
== multiple_symbols_all
);
2439 gdb_assert (select_mode
== NULL
2440 || select_mode
== multiple_symbols_all
2441 || select_mode
== multiple_symbols_ask
2442 || select_mode
== multiple_symbols_cancel
);
2443 gdb_assert ((flags
& DECODE_LINE_LIST_MODE
) == 0);
2445 linespec_parser_new (&parser
, flags
, current_language
, default_symtab
,
2446 default_line
, canonical
);
2447 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
2448 save_current_program_space ();
2450 orig
= copy
= *argptr
;
2451 result
= parse_linespec (&parser
, ©
);
2452 *argptr
+= copy
- orig
;
2453 state
= PARSER_STATE (&parser
);
2455 gdb_assert (result
.nelts
== 1 || canonical
->pre_expanded
);
2456 gdb_assert (canonical
->addr_string
!= NULL
);
2457 canonical
->pre_expanded
= 1;
2459 /* Arrange for allocated canonical names to be freed. */
2460 if (result
.nelts
> 0)
2464 make_cleanup (xfree
, state
->canonical_names
);
2465 for (i
= 0; i
< result
.nelts
; ++i
)
2467 gdb_assert (state
->canonical_names
[i
].suffix
!= NULL
);
2468 make_cleanup (xfree
, state
->canonical_names
[i
].suffix
);
2472 if (select_mode
== NULL
)
2474 if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
2475 select_mode
= multiple_symbols_all
;
2477 select_mode
= multiple_symbols_select_mode ();
2480 if (select_mode
== multiple_symbols_all
)
2484 make_cleanup (VEC_cleanup (const_char_ptr
), &filters
);
2485 VEC_safe_push (const_char_ptr
, filters
, filter
);
2486 filter_results (state
, &result
, filters
);
2489 convert_results_to_lsals (state
, &result
);
2492 decode_line_2 (state
, &result
, select_mode
);
2494 do_cleanups (cleanups
);
2497 /* See linespec.h. */
2499 struct symtabs_and_lines
2500 decode_line_1 (char **argptr
, int flags
,
2501 struct symtab
*default_symtab
,
2504 struct symtabs_and_lines result
;
2505 linespec_parser parser
;
2506 struct cleanup
*cleanups
;
2507 const char *copy
, *orig
;
2509 linespec_parser_new (&parser
, flags
, current_language
, default_symtab
,
2510 default_line
, NULL
);
2511 cleanups
= make_cleanup (linespec_parser_delete
, &parser
);
2512 save_current_program_space ();
2514 orig
= copy
= *argptr
;
2515 result
= parse_linespec (&parser
, ©
);
2516 *argptr
+= copy
- orig
;
2518 do_cleanups (cleanups
);
2522 /* See linespec.h. */
2524 struct symtabs_and_lines
2525 decode_line_with_current_source (char *string
, int flags
)
2527 struct symtabs_and_lines sals
;
2528 struct symtab_and_line cursal
;
2531 error (_("Empty line specification."));
2533 /* We use whatever is set as the current source line. We do not try
2534 and get a default source symtab+line or it will recursively call us! */
2535 cursal
= get_current_source_symtab_and_line ();
2537 sals
= decode_line_1 (&string
, flags
,
2538 cursal
.symtab
, cursal
.line
);
2541 error (_("Junk at end of line specification: %s"), string
);
2545 /* See linespec.h. */
2547 struct symtabs_and_lines
2548 decode_line_with_last_displayed (char *string
, int flags
)
2550 struct symtabs_and_lines sals
;
2553 error (_("Empty line specification."));
2555 if (last_displayed_sal_is_valid ())
2556 sals
= decode_line_1 (&string
, flags
,
2557 get_last_displayed_symtab (),
2558 get_last_displayed_line ());
2560 sals
= decode_line_1 (&string
, flags
, (struct symtab
*) NULL
, 0);
2563 error (_("Junk at end of line specification: %s"), string
);
2569 /* First, some functions to initialize stuff at the beggining of the
2573 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
2575 if (*default_symtab
== 0)
2577 /* Use whatever we have for the default source line. We don't use
2578 get_current_or_default_symtab_and_line as it can recurse and call
2580 struct symtab_and_line cursal
=
2581 get_current_source_symtab_and_line ();
2583 *default_symtab
= cursal
.symtab
;
2584 *default_line
= cursal
.line
;
2590 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2591 advancing EXP_PTR past any parsed text. */
2594 linespec_expression_to_pc (const char **exp_ptr
)
2596 if (current_program_space
->executing_startup
)
2597 /* The error message doesn't really matter, because this case
2598 should only hit during breakpoint reset. */
2599 throw_error (NOT_FOUND_ERROR
, _("cannot evaluate expressions while "
2600 "program space is in startup"));
2603 return value_as_address (parse_to_comma_and_eval (exp_ptr
));
2608 /* Here's where we recognise an Objective-C Selector. An Objective C
2609 selector may be implemented by more than one class, therefore it
2610 may represent more than one method/function. This gives us a
2611 situation somewhat analogous to C++ overloading. If there's more
2612 than one method that could represent the selector, then use some of
2613 the existing C++ code to let the user choose one. */
2615 static struct symtabs_and_lines
2616 decode_objc (struct linespec_state
*self
, linespec_p ls
, const char **argptr
)
2618 struct collect_info info
;
2619 VEC (const_char_ptr
) *symbol_names
= NULL
;
2620 struct symtabs_and_lines values
;
2621 const char *new_argptr
;
2622 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
2626 info
.file_symtabs
= NULL
;
2627 VEC_safe_push (symtab_ptr
, info
.file_symtabs
, NULL
);
2628 make_cleanup (VEC_cleanup (symtab_ptr
), &info
.file_symtabs
);
2629 info
.result
.symbols
= NULL
;
2630 info
.result
.minimal_symbols
= NULL
;
2634 new_argptr
= find_imps (*argptr
, &symbol_names
);
2635 if (VEC_empty (const_char_ptr
, symbol_names
))
2637 do_cleanups (cleanup
);
2641 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
);
2643 if (!VEC_empty (symbolp
, info
.result
.symbols
)
2644 || !VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
2648 saved_arg
= alloca (new_argptr
- *argptr
+ 1);
2649 memcpy (saved_arg
, *argptr
, new_argptr
- *argptr
);
2650 saved_arg
[new_argptr
- *argptr
] = '\0';
2652 ls
->function_name
= xstrdup (saved_arg
);
2653 ls
->function_symbols
= info
.result
.symbols
;
2654 ls
->minimal_symbols
= info
.result
.minimal_symbols
;
2655 values
= convert_linespec_to_sals (self
, ls
);
2657 if (self
->canonical
)
2659 self
->canonical
->pre_expanded
= 1;
2660 if (ls
->source_filename
)
2661 self
->canonical
->addr_string
2662 = xstrprintf ("%s:%s", ls
->source_filename
, saved_arg
);
2664 self
->canonical
->addr_string
= xstrdup (saved_arg
);
2668 *argptr
= new_argptr
;
2670 do_cleanups (cleanup
);
2675 /* An instance of this type is used when collecting prefix symbols for
2678 struct decode_compound_collector
2680 /* The result vector. */
2681 VEC (symbolp
) *symbols
;
2683 /* A hash table of all symbols we found. We use this to avoid
2684 adding any symbol more than once. */
2688 /* A callback for iterate_over_symbols that is used by
2689 lookup_prefix_sym to collect type symbols. */
2692 collect_one_symbol (struct symbol
*sym
, void *d
)
2694 struct decode_compound_collector
*collector
= d
;
2698 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
2699 return 1; /* Continue iterating. */
2701 t
= SYMBOL_TYPE (sym
);
2703 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2704 && TYPE_CODE (t
) != TYPE_CODE_UNION
2705 && TYPE_CODE (t
) != TYPE_CODE_NAMESPACE
)
2706 return 1; /* Continue iterating. */
2708 slot
= htab_find_slot (collector
->unique_syms
, sym
, INSERT
);
2712 VEC_safe_push (symbolp
, collector
->symbols
, sym
);
2715 return 1; /* Continue iterating. */
2718 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
2720 static VEC (symbolp
) *
2721 lookup_prefix_sym (struct linespec_state
*state
, VEC (symtab_ptr
) *file_symtabs
,
2722 const char *class_name
)
2726 struct decode_compound_collector collector
;
2727 struct cleanup
*outer
;
2728 struct cleanup
*cleanup
;
2730 collector
.symbols
= NULL
;
2731 outer
= make_cleanup (VEC_cleanup (symbolp
), &collector
.symbols
);
2733 collector
.unique_syms
= htab_create_alloc (1, htab_hash_pointer
,
2734 htab_eq_pointer
, NULL
,
2736 cleanup
= make_cleanup_htab_delete (collector
.unique_syms
);
2738 for (ix
= 0; VEC_iterate (symtab_ptr
, file_symtabs
, ix
, elt
); ++ix
)
2742 iterate_over_all_matching_symtabs (state
, class_name
, STRUCT_DOMAIN
,
2743 collect_one_symbol
, &collector
,
2745 iterate_over_all_matching_symtabs (state
, class_name
, VAR_DOMAIN
,
2746 collect_one_symbol
, &collector
,
2751 /* Program spaces that are executing startup should have
2752 been filtered out earlier. */
2753 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
2754 set_current_program_space (SYMTAB_PSPACE (elt
));
2755 iterate_over_file_blocks (elt
, class_name
, STRUCT_DOMAIN
,
2756 collect_one_symbol
, &collector
);
2757 iterate_over_file_blocks (elt
, class_name
, VAR_DOMAIN
,
2758 collect_one_symbol
, &collector
);
2762 do_cleanups (cleanup
);
2763 discard_cleanups (outer
);
2764 return collector
.symbols
;
2767 /* A qsort comparison function for symbols. The resulting order does
2768 not actually matter; we just need to be able to sort them so that
2769 symbols with the same program space end up next to each other. */
2772 compare_symbols (const void *a
, const void *b
)
2774 struct symbol
* const *sa
= a
;
2775 struct symbol
* const *sb
= b
;
2778 uia
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa
));
2779 uib
= (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb
));
2786 uia
= (uintptr_t) *sa
;
2787 uib
= (uintptr_t) *sb
;
2797 /* Like compare_symbols but for minimal symbols. */
2800 compare_msymbols (const void *a
, const void *b
)
2802 const struct bound_minimal_symbol
*sa
= a
;
2803 const struct bound_minimal_symbol
*sb
= b
;
2806 uia
= (uintptr_t) sa
->objfile
->pspace
;
2807 uib
= (uintptr_t) sa
->objfile
->pspace
;
2814 uia
= (uintptr_t) sa
->minsym
;
2815 uib
= (uintptr_t) sb
->minsym
;
2825 /* Look for all the matching instances of each symbol in NAMES. Only
2826 instances from PSPACE are considered; other program spaces are
2827 handled by our caller. If PSPACE is NULL, then all program spaces
2828 are considered. Results are stored into INFO. */
2831 add_all_symbol_names_from_pspace (struct collect_info
*info
,
2832 struct program_space
*pspace
,
2833 VEC (const_char_ptr
) *names
)
2838 for (ix
= 0; VEC_iterate (const_char_ptr
, names
, ix
, iter
); ++ix
)
2839 add_matching_symbols_to_info (iter
, info
, pspace
);
2843 find_superclass_methods (VEC (typep
) *superclasses
,
2845 VEC (const_char_ptr
) **result_names
)
2847 int old_len
= VEC_length (const_char_ptr
, *result_names
);
2848 VEC (typep
) *iter_classes
;
2849 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
2851 iter_classes
= superclasses
;
2854 VEC (typep
) *new_supers
= NULL
;
2858 make_cleanup (VEC_cleanup (typep
), &new_supers
);
2859 for (ix
= 0; VEC_iterate (typep
, iter_classes
, ix
, t
); ++ix
)
2860 find_methods (t
, name
, result_names
, &new_supers
);
2862 if (VEC_length (const_char_ptr
, *result_names
) != old_len
2863 || VEC_empty (typep
, new_supers
))
2866 iter_classes
= new_supers
;
2869 do_cleanups (cleanup
);
2872 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
2873 given by one of the symbols in SYM_CLASSES. Matches are returned
2874 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
2877 find_method (struct linespec_state
*self
, VEC (symtab_ptr
) *file_symtabs
,
2878 const char *class_name
, const char *method_name
,
2879 VEC (symbolp
) *sym_classes
, VEC (symbolp
) **symbols
,
2880 VEC (bound_minimal_symbol_d
) **minsyms
)
2883 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
2885 int last_result_len
;
2886 VEC (typep
) *superclass_vec
;
2887 VEC (const_char_ptr
) *result_names
;
2888 struct collect_info info
;
2890 /* Sort symbols so that symbols with the same program space are next
2892 qsort (VEC_address (symbolp
, sym_classes
),
2893 VEC_length (symbolp
, sym_classes
),
2898 info
.file_symtabs
= file_symtabs
;
2899 info
.result
.symbols
= NULL
;
2900 info
.result
.minimal_symbols
= NULL
;
2902 /* Iterate over all the types, looking for the names of existing
2903 methods matching METHOD_NAME. If we cannot find a direct method in a
2904 given program space, then we consider inherited methods; this is
2905 not ideal (ideal would be to respect C++ hiding rules), but it
2906 seems good enough and is what GDB has historically done. We only
2907 need to collect the names because later we find all symbols with
2908 those names. This loop is written in a somewhat funny way
2909 because we collect data across the program space before deciding
2911 superclass_vec
= NULL
;
2912 make_cleanup (VEC_cleanup (typep
), &superclass_vec
);
2913 result_names
= NULL
;
2914 make_cleanup (VEC_cleanup (const_char_ptr
), &result_names
);
2915 last_result_len
= 0;
2916 for (ix
= 0; VEC_iterate (symbolp
, sym_classes
, ix
, sym
); ++ix
)
2919 struct program_space
*pspace
;
2921 /* Program spaces that are executing startup should have
2922 been filtered out earlier. */
2923 pspace
= SYMTAB_PSPACE (symbol_symtab (sym
));
2924 gdb_assert (!pspace
->executing_startup
);
2925 set_current_program_space (pspace
);
2926 t
= check_typedef (SYMBOL_TYPE (sym
));
2927 find_methods (t
, method_name
, &result_names
, &superclass_vec
);
2929 /* Handle all items from a single program space at once; and be
2930 sure not to miss the last batch. */
2931 if (ix
== VEC_length (symbolp
, sym_classes
) - 1
2933 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp
, sym_classes
,
2936 /* If we did not find a direct implementation anywhere in
2937 this program space, consider superclasses. */
2938 if (VEC_length (const_char_ptr
, result_names
) == last_result_len
)
2939 find_superclass_methods (superclass_vec
, method_name
,
2942 /* We have a list of candidate symbol names, so now we
2943 iterate over the symbol tables looking for all
2944 matches in this pspace. */
2945 add_all_symbol_names_from_pspace (&info
, pspace
, result_names
);
2947 VEC_truncate (typep
, superclass_vec
, 0);
2948 last_result_len
= VEC_length (const_char_ptr
, result_names
);
2952 if (!VEC_empty (symbolp
, info
.result
.symbols
)
2953 || !VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
2955 *symbols
= info
.result
.symbols
;
2956 *minsyms
= info
.result
.minimal_symbols
;
2957 do_cleanups (cleanup
);
2961 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
2962 and other attempts to locate the symbol will be made. */
2963 throw_error (NOT_FOUND_ERROR
, _("see caller, this text doesn't matter"));
2968 /* This object is used when collecting all matching symtabs. */
2970 struct symtab_collector
2972 /* The result vector of symtabs. */
2973 VEC (symtab_ptr
) *symtabs
;
2975 /* This is used to ensure the symtabs are unique. */
2976 htab_t symtab_table
;
2979 /* Callback for iterate_over_symtabs. */
2982 add_symtabs_to_list (struct symtab
*symtab
, void *d
)
2984 struct symtab_collector
*data
= d
;
2987 slot
= htab_find_slot (data
->symtab_table
, symtab
, INSERT
);
2991 VEC_safe_push (symtab_ptr
, data
->symtabs
, symtab
);
2997 /* Given a file name, return a VEC of all matching symtabs. */
2999 static VEC (symtab_ptr
) *
3000 collect_symtabs_from_filename (const char *file
)
3002 struct symtab_collector collector
;
3003 struct cleanup
*cleanups
;
3004 struct program_space
*pspace
;
3006 collector
.symtabs
= NULL
;
3007 collector
.symtab_table
= htab_create (1, htab_hash_pointer
, htab_eq_pointer
,
3009 cleanups
= make_cleanup_htab_delete (collector
.symtab_table
);
3011 /* Find that file's data. */
3012 ALL_PSPACES (pspace
)
3014 if (pspace
->executing_startup
)
3017 set_current_program_space (pspace
);
3018 iterate_over_symtabs (file
, add_symtabs_to_list
, &collector
);
3021 do_cleanups (cleanups
);
3022 return collector
.symtabs
;
3025 /* Return all the symtabs associated to the FILENAME. */
3027 static VEC (symtab_ptr
) *
3028 symtabs_from_filename (const char *filename
)
3030 VEC (symtab_ptr
) *result
;
3032 result
= collect_symtabs_from_filename (filename
);
3034 if (VEC_empty (symtab_ptr
, result
))
3036 if (!have_full_symbols () && !have_partial_symbols ())
3037 throw_error (NOT_FOUND_ERROR
,
3038 _("No symbol table is loaded. "
3039 "Use the \"file\" command."));
3040 throw_error (NOT_FOUND_ERROR
, _("No source file named %s."), filename
);
3046 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3047 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3048 returned in MINSYMS. */
3051 find_function_symbols (struct linespec_state
*state
,
3052 VEC (symtab_ptr
) *file_symtabs
, const char *name
,
3053 VEC (symbolp
) **symbols
,
3054 VEC (bound_minimal_symbol_d
) **minsyms
)
3056 struct collect_info info
;
3057 VEC (const_char_ptr
) *symbol_names
= NULL
;
3058 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (const_char_ptr
),
3062 info
.result
.symbols
= NULL
;
3063 info
.result
.minimal_symbols
= NULL
;
3064 info
.file_symtabs
= file_symtabs
;
3066 /* Try NAME as an Objective-C selector. */
3067 find_imps (name
, &symbol_names
);
3068 if (!VEC_empty (const_char_ptr
, symbol_names
))
3069 add_all_symbol_names_from_pspace (&info
, NULL
, symbol_names
);
3071 add_matching_symbols_to_info (name
, &info
, NULL
);
3073 do_cleanups (cleanup
);
3075 if (VEC_empty (symbolp
, info
.result
.symbols
))
3077 VEC_free (symbolp
, info
.result
.symbols
);
3081 *symbols
= info
.result
.symbols
;
3083 if (VEC_empty (bound_minimal_symbol_d
, info
.result
.minimal_symbols
))
3085 VEC_free (bound_minimal_symbol_d
, info
.result
.minimal_symbols
);
3089 *minsyms
= info
.result
.minimal_symbols
;
3092 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3093 in SYMBOLS and minimal symbols in MINSYMS. */
3096 find_linespec_symbols (struct linespec_state
*state
,
3097 VEC (symtab_ptr
) *file_symtabs
,
3099 VEC (symbolp
) **symbols
,
3100 VEC (bound_minimal_symbol_d
) **minsyms
)
3102 struct cleanup
*cleanup
;
3104 const char *lookup_name
;
3105 volatile struct gdb_exception except
;
3107 cleanup
= demangle_for_lookup (name
, state
->language
->la_language
,
3109 if (state
->language
->la_language
== language_ada
)
3111 /* In Ada, the symbol lookups are performed using the encoded
3112 name rather than the demangled name. */
3113 lookup_name
= ada_name_for_lookup (name
);
3114 make_cleanup (xfree
, (void *) lookup_name
);
3117 canon
= cp_canonicalize_string_no_typedefs (lookup_name
);
3120 lookup_name
= canon
;
3121 make_cleanup (xfree
, canon
);
3124 /* It's important to not call expand_symtabs_matching unnecessarily
3125 as it can really slow things down (by unnecessarily expanding
3126 potentially 1000s of symtabs, which when debugging some apps can
3127 cost 100s of seconds). Avoid this to some extent by *first* calling
3128 find_function_symbols, and only if that doesn't find anything
3129 *then* call find_method. This handles two important cases:
3130 1) break (anonymous namespace)::foo
3131 2) break class::method where method is in class (and not a baseclass) */
3133 find_function_symbols (state
, file_symtabs
, lookup_name
,
3136 /* If we were unable to locate a symbol of the same name, try dividing
3137 the name into class and method names and searching the class and its
3139 if (VEC_empty (symbolp
, *symbols
)
3140 && VEC_empty (bound_minimal_symbol_d
, *minsyms
))
3142 char *klass
, *method
;
3143 const char *last
, *p
, *scope_op
;
3144 VEC (symbolp
) *classes
;
3146 /* See if we can find a scope operator and break this symbol
3147 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3149 p
= find_toplevel_string (lookup_name
, scope_op
);
3152 /* No C++ scope operator. Try Java. */
3154 p
= find_toplevel_string (lookup_name
, scope_op
);
3161 p
= find_toplevel_string (p
+ strlen (scope_op
), scope_op
);
3164 /* If no scope operator was found, there is nothing more we can do;
3165 we already attempted to lookup the entire name as a symbol
3169 do_cleanups (cleanup
);
3173 /* LOOKUP_NAME points to the class name.
3174 LAST points to the method name. */
3175 klass
= xmalloc ((last
- lookup_name
+ 1) * sizeof (char));
3176 make_cleanup (xfree
, klass
);
3177 strncpy (klass
, lookup_name
, last
- lookup_name
);
3178 klass
[last
- lookup_name
] = '\0';
3180 /* Skip past the scope operator. */
3181 last
+= strlen (scope_op
);
3182 method
= xmalloc ((strlen (last
) + 1) * sizeof (char));
3183 make_cleanup (xfree
, method
);
3184 strcpy (method
, last
);
3186 /* Find a list of classes named KLASS. */
3187 classes
= lookup_prefix_sym (state
, file_symtabs
, klass
);
3188 make_cleanup (VEC_cleanup (symbolp
), &classes
);
3190 if (!VEC_empty (symbolp
, classes
))
3192 /* Now locate a list of suitable methods named METHOD. */
3193 TRY_CATCH (except
, RETURN_MASK_ERROR
)
3195 find_method (state
, file_symtabs
, klass
, method
, classes
,
3199 /* If successful, we're done. If NOT_FOUND_ERROR
3200 was not thrown, rethrow the exception that we did get. */
3201 if (except
.reason
< 0 && except
.error
!= NOT_FOUND_ERROR
)
3202 throw_exception (except
);
3206 do_cleanups (cleanup
);
3209 /* Return all labels named NAME in FUNCTION_SYMBOLS. Return the
3210 actual function symbol in which the label was found in LABEL_FUNC_RET. */
3212 static VEC (symbolp
) *
3213 find_label_symbols (struct linespec_state
*self
,
3214 VEC (symbolp
) *function_symbols
,
3215 VEC (symbolp
) **label_funcs_ret
, const char *name
)
3218 const struct block
*block
;
3220 struct symbol
*fn_sym
;
3221 VEC (symbolp
) *result
= NULL
;
3223 if (function_symbols
== NULL
)
3225 set_current_program_space (self
->program_space
);
3226 block
= get_current_search_block ();
3229 block
&& !BLOCK_FUNCTION (block
);
3230 block
= BLOCK_SUPERBLOCK (block
))
3234 fn_sym
= BLOCK_FUNCTION (block
);
3236 sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0);
3240 VEC_safe_push (symbolp
, result
, sym
);
3241 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
3247 VEC_iterate (symbolp
, function_symbols
, ix
, fn_sym
); ++ix
)
3249 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym
)));
3250 block
= SYMBOL_BLOCK_VALUE (fn_sym
);
3251 sym
= lookup_symbol (name
, block
, LABEL_DOMAIN
, 0);
3255 VEC_safe_push (symbolp
, result
, sym
);
3256 VEC_safe_push (symbolp
, *label_funcs_ret
, fn_sym
);
3266 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3269 decode_digits_list_mode (struct linespec_state
*self
,
3271 struct symtabs_and_lines
*values
,
3272 struct symtab_and_line val
)
3277 gdb_assert (self
->list_mode
);
3279 for (ix
= 0; VEC_iterate (symtab_ptr
, ls
->file_symtabs
, ix
, elt
);
3282 /* The logic above should ensure this. */
3283 gdb_assert (elt
!= NULL
);
3285 set_current_program_space (SYMTAB_PSPACE (elt
));
3287 /* Simplistic search just for the list command. */
3288 val
.symtab
= find_line_symtab (elt
, val
.line
, NULL
, NULL
);
3289 if (val
.symtab
== NULL
)
3291 val
.pspace
= SYMTAB_PSPACE (elt
);
3293 val
.explicit_line
= 1;
3295 add_sal_to_sals (self
, values
, &val
, NULL
, 0);
3299 /* A helper for create_sals_line_offset that iterates over the symtabs,
3300 adding lines to the VEC. */
3303 decode_digits_ordinary (struct linespec_state
*self
,
3306 struct symtabs_and_lines
*sals
,
3307 struct linetable_entry
**best_entry
)
3312 for (ix
= 0; VEC_iterate (symtab_ptr
, ls
->file_symtabs
, ix
, elt
); ++ix
)
3315 VEC (CORE_ADDR
) *pcs
;
3318 /* The logic above should ensure this. */
3319 gdb_assert (elt
!= NULL
);
3321 set_current_program_space (SYMTAB_PSPACE (elt
));
3323 pcs
= find_pcs_for_symtab_line (elt
, line
, best_entry
);
3324 for (i
= 0; VEC_iterate (CORE_ADDR
, pcs
, i
, pc
); ++i
)
3326 struct symtab_and_line sal
;
3329 sal
.pspace
= SYMTAB_PSPACE (elt
);
3333 add_sal_to_sals_basic (sals
, &sal
);
3336 VEC_free (CORE_ADDR
, pcs
);
3342 /* Return the line offset represented by VARIABLE. */
3344 static struct line_offset
3345 linespec_parse_variable (struct linespec_state
*self
, const char *variable
)
3349 struct line_offset offset
= {0, LINE_OFFSET_NONE
};
3351 p
= (variable
[1] == '$') ? variable
+ 2 : variable
+ 1;
3354 while (*p
>= '0' && *p
<= '9')
3356 if (!*p
) /* Reached end of token without hitting non-digit. */
3358 /* We have a value history reference. */
3359 struct value
*val_history
;
3361 sscanf ((variable
[1] == '$') ? variable
+ 2 : variable
+ 1, "%d", &index
);
3363 = access_value_history ((variable
[1] == '$') ? -index
: index
);
3364 if (TYPE_CODE (value_type (val_history
)) != TYPE_CODE_INT
)
3365 error (_("History values used in line "
3366 "specs must have integer values."));
3367 offset
.offset
= value_as_long (val_history
);
3371 /* Not all digits -- may be user variable/function or a
3372 convenience variable. */
3374 struct internalvar
*ivar
;
3376 /* Try it as a convenience variable. If it is not a convenience
3377 variable, return and allow normal symbol lookup to occur. */
3378 ivar
= lookup_only_internalvar (variable
+ 1);
3380 /* No internal variable with that name. Mark the offset
3381 as unknown to allow the name to be looked up as a symbol. */
3382 offset
.sign
= LINE_OFFSET_UNKNOWN
;
3385 /* We found a valid variable name. If it is not an integer,
3387 if (!get_internalvar_integer (ivar
, &valx
))
3388 error (_("Convenience variables used in line "
3389 "specs must have integer values."));
3391 offset
.offset
= valx
;
3399 /* A callback used to possibly add a symbol to the results. */
3402 collect_symbols (struct symbol
*sym
, void *data
)
3404 struct collect_info
*info
= data
;
3406 /* In list mode, add all matching symbols, regardless of class.
3407 This allows the user to type "list a_global_variable". */
3408 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
|| info
->state
->list_mode
)
3409 VEC_safe_push (symbolp
, info
->result
.symbols
, sym
);
3410 return 1; /* Continue iterating. */
3413 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
3414 linespec; return the SAL in RESULT. */
3417 minsym_found (struct linespec_state
*self
, struct objfile
*objfile
,
3418 struct minimal_symbol
*msymbol
,
3419 struct symtabs_and_lines
*result
)
3421 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3423 struct symtab_and_line sal
;
3425 sal
= find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile
, msymbol
),
3426 (struct obj_section
*) 0, 0);
3427 sal
.section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
);
3429 /* The minimal symbol might point to a function descriptor;
3430 resolve it to the actual code address instead. */
3431 pc
= gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
3433 sal
= find_pc_sect_line (pc
, NULL
, 0);
3435 if (self
->funfirstline
)
3436 skip_prologue_sal (&sal
);
3438 if (maybe_add_address (self
->addr_set
, objfile
->pspace
, sal
.pc
))
3439 add_sal_to_sals (self
, result
, &sal
, MSYMBOL_NATURAL_NAME (msymbol
), 0);
3442 /* A helper struct to pass some data through
3443 iterate_over_minimal_symbols. */
3445 struct collect_minsyms
3447 /* The objfile we're examining. */
3448 struct objfile
*objfile
;
3450 /* Only search the given symtab, or NULL to search for all symbols. */
3451 struct symtab
*symtab
;
3453 /* The funfirstline setting from the initial call. */
3456 /* The list_mode setting from the initial call. */
3459 /* The resulting symbols. */
3460 VEC (bound_minimal_symbol_d
) *msyms
;
3463 /* A helper function to classify a minimal_symbol_type according to
3467 classify_mtype (enum minimal_symbol_type t
)
3474 /* Intermediate priority. */
3477 case mst_solib_trampoline
:
3478 /* Lowest priority. */
3482 /* Highest priority. */
3487 /* Callback for qsort that sorts symbols by priority. */
3490 compare_msyms (const void *a
, const void *b
)
3492 const bound_minimal_symbol_d
*moa
= a
;
3493 const bound_minimal_symbol_d
*mob
= b
;
3494 enum minimal_symbol_type ta
= MSYMBOL_TYPE (moa
->minsym
);
3495 enum minimal_symbol_type tb
= MSYMBOL_TYPE (mob
->minsym
);
3497 return classify_mtype (ta
) - classify_mtype (tb
);
3500 /* Callback for iterate_over_minimal_symbols that adds the symbol to
3504 add_minsym (struct minimal_symbol
*minsym
, void *d
)
3506 struct collect_minsyms
*info
= d
;
3507 bound_minimal_symbol_d mo
;
3510 mo
.objfile
= info
->objfile
;
3512 if (info
->symtab
!= NULL
)
3515 struct symtab_and_line sal
;
3516 struct gdbarch
*gdbarch
= get_objfile_arch (info
->objfile
);
3518 sal
= find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info
->objfile
, minsym
),
3520 sal
.section
= MSYMBOL_OBJ_SECTION (info
->objfile
, minsym
);
3522 = gdbarch_convert_from_func_ptr_addr (gdbarch
, sal
.pc
, ¤t_target
);
3524 sal
= find_pc_sect_line (pc
, NULL
, 0);
3526 if (info
->symtab
!= sal
.symtab
)
3530 /* Exclude data symbols when looking for breakpoint locations. */
3531 if (!info
->list_mode
)
3532 switch (minsym
->type
)
3534 case mst_slot_got_plt
:
3541 /* Make sure this minsym is not a function descriptor
3542 before we decide to discard it. */
3543 struct gdbarch
*gdbarch
= get_objfile_arch (info
->objfile
);
3544 CORE_ADDR addr
= gdbarch_convert_from_func_ptr_addr
3545 (gdbarch
, BMSYMBOL_VALUE_ADDRESS (mo
),
3548 if (addr
== BMSYMBOL_VALUE_ADDRESS (mo
))
3553 VEC_safe_push (bound_minimal_symbol_d
, info
->msyms
, &mo
);
3556 /* Search for minimal symbols called NAME. If SEARCH_PSPACE
3557 is not NULL, the search is restricted to just that program
3560 If SYMTAB is NULL, search all objfiles, otherwise
3561 restrict results to the given SYMTAB. */
3564 search_minsyms_for_name (struct collect_info
*info
, const char *name
,
3565 struct program_space
*search_pspace
,
3566 struct symtab
*symtab
)
3568 struct collect_minsyms local
;
3569 struct cleanup
*cleanup
;
3571 memset (&local
, 0, sizeof (local
));
3572 local
.funfirstline
= info
->state
->funfirstline
;
3573 local
.list_mode
= info
->state
->list_mode
;
3574 local
.symtab
= symtab
;
3576 cleanup
= make_cleanup (VEC_cleanup (bound_minimal_symbol_d
), &local
.msyms
);
3580 struct program_space
*pspace
;
3582 ALL_PSPACES (pspace
)
3584 struct objfile
*objfile
;
3586 if (search_pspace
!= NULL
&& search_pspace
!= pspace
)
3588 if (pspace
->executing_startup
)
3591 set_current_program_space (pspace
);
3593 ALL_OBJFILES (objfile
)
3595 local
.objfile
= objfile
;
3596 iterate_over_minimal_symbols (objfile
, name
, add_minsym
, &local
);
3602 if (search_pspace
== NULL
|| SYMTAB_PSPACE (symtab
) == search_pspace
)
3604 set_current_program_space (SYMTAB_PSPACE (symtab
));
3605 local
.objfile
= SYMTAB_OBJFILE(symtab
);
3606 iterate_over_minimal_symbols (local
.objfile
, name
, add_minsym
,
3611 if (!VEC_empty (bound_minimal_symbol_d
, local
.msyms
))
3615 bound_minimal_symbol_d
*item
;
3617 qsort (VEC_address (bound_minimal_symbol_d
, local
.msyms
),
3618 VEC_length (bound_minimal_symbol_d
, local
.msyms
),
3619 sizeof (bound_minimal_symbol_d
),
3622 /* Now the minsyms are in classification order. So, we walk
3623 over them and process just the minsyms with the same
3624 classification as the very first minsym in the list. */
3625 item
= VEC_index (bound_minimal_symbol_d
, local
.msyms
, 0);
3626 classification
= classify_mtype (MSYMBOL_TYPE (item
->minsym
));
3629 VEC_iterate (bound_minimal_symbol_d
, local
.msyms
, ix
, item
);
3632 if (classify_mtype (MSYMBOL_TYPE (item
->minsym
)) != classification
)
3635 VEC_safe_push (bound_minimal_symbol_d
,
3636 info
->result
.minimal_symbols
, item
);
3640 do_cleanups (cleanup
);
3643 /* A helper function to add all symbols matching NAME to INFO. If
3644 PSPACE is not NULL, the search is restricted to just that program
3648 add_matching_symbols_to_info (const char *name
,
3649 struct collect_info
*info
,
3650 struct program_space
*pspace
)
3655 for (ix
= 0; VEC_iterate (symtab_ptr
, info
->file_symtabs
, ix
, elt
); ++ix
)
3659 iterate_over_all_matching_symtabs (info
->state
, name
, VAR_DOMAIN
,
3660 collect_symbols
, info
,
3662 search_minsyms_for_name (info
, name
, pspace
, NULL
);
3664 else if (pspace
== NULL
|| pspace
== SYMTAB_PSPACE (elt
))
3666 int prev_len
= VEC_length (symbolp
, info
->result
.symbols
);
3668 /* Program spaces that are executing startup should have
3669 been filtered out earlier. */
3670 gdb_assert (!SYMTAB_PSPACE (elt
)->executing_startup
);
3671 set_current_program_space (SYMTAB_PSPACE (elt
));
3672 iterate_over_file_blocks (elt
, name
, VAR_DOMAIN
,
3673 collect_symbols
, info
);
3675 /* If no new symbols were found in this iteration and this symtab
3676 is in assembler, we might actually be looking for a label for
3677 which we don't have debug info. Check for a minimal symbol in
3679 if (prev_len
== VEC_length (symbolp
, info
->result
.symbols
)
3680 && elt
->language
== language_asm
)
3681 search_minsyms_for_name (info
, name
, pspace
, elt
);
3688 /* Now come some functions that are called from multiple places within
3692 symbol_to_sal (struct symtab_and_line
*result
,
3693 int funfirstline
, struct symbol
*sym
)
3695 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
3697 *result
= find_function_start_sal (sym
, funfirstline
);
3702 if (SYMBOL_CLASS (sym
) == LOC_LABEL
&& SYMBOL_VALUE_ADDRESS (sym
) != 0)
3705 result
->symtab
= symbol_symtab (sym
);
3706 result
->line
= SYMBOL_LINE (sym
);
3707 result
->pc
= SYMBOL_VALUE_ADDRESS (sym
);
3708 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
3709 result
->explicit_pc
= 1;
3712 else if (funfirstline
)
3716 else if (SYMBOL_LINE (sym
) != 0)
3718 /* We know its line number. */
3720 result
->symtab
= symbol_symtab (sym
);
3721 result
->line
= SYMBOL_LINE (sym
);
3722 result
->pspace
= SYMTAB_PSPACE (result
->symtab
);
3730 /* See the comment in linespec.h. */
3733 init_linespec_result (struct linespec_result
*lr
)
3735 memset (lr
, 0, sizeof (*lr
));
3738 /* See the comment in linespec.h. */
3741 destroy_linespec_result (struct linespec_result
*ls
)
3744 struct linespec_sals
*lsal
;
3746 xfree (ls
->addr_string
);
3747 for (i
= 0; VEC_iterate (linespec_sals
, ls
->sals
, i
, lsal
); ++i
)
3749 xfree (lsal
->canonical
);
3750 xfree (lsal
->sals
.sals
);
3752 VEC_free (linespec_sals
, ls
->sals
);
3755 /* Cleanup function for a linespec_result. */
3758 cleanup_linespec_result (void *a
)
3760 destroy_linespec_result (a
);
3763 /* See the comment in linespec.h. */
3766 make_cleanup_destroy_linespec_result (struct linespec_result
*ls
)
3768 return make_cleanup (cleanup_linespec_result
, ls
);