Commit | Line | Data |
---|---|---|
50641945 | 1 | /* Parser for linespec for the GNU debugger, GDB. |
05ff989b | 2 | |
61baf725 | 3 | Copyright (C) 1986-2017 Free Software Foundation, Inc. |
50641945 FN |
4 | |
5 | This file is part of GDB. | |
6 | ||
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
50641945 FN |
10 | (at your option) any later version. |
11 | ||
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
50641945 FN |
19 | |
20 | #include "defs.h" | |
21 | #include "symtab.h" | |
c5f0f3d0 FN |
22 | #include "frame.h" |
23 | #include "command.h" | |
50641945 FN |
24 | #include "symfile.h" |
25 | #include "objfiles.h" | |
0378c332 | 26 | #include "source.h" |
50641945 | 27 | #include "demangle.h" |
c5f0f3d0 FN |
28 | #include "value.h" |
29 | #include "completer.h" | |
015a42b4 | 30 | #include "cp-abi.h" |
12907978 | 31 | #include "cp-support.h" |
c38da1af | 32 | #include "parser-defs.h" |
fe898f56 | 33 | #include "block.h" |
d2630e69 | 34 | #include "objc-lang.h" |
b9362cc7 | 35 | #include "linespec.h" |
53c5240f | 36 | #include "language.h" |
dc67126b NR |
37 | #include "interps.h" |
38 | #include "mi/mi-cmds.h" | |
bccdca4a | 39 | #include "target.h" |
94af9270 | 40 | #include "arch-utils.h" |
c00f8484 KS |
41 | #include <ctype.h> |
42 | #include "cli/cli-utils.h" | |
731971ed | 43 | #include "filenames.h" |
f8eba3c6 | 44 | #include "ada-lang.h" |
39cf75f7 | 45 | #include "stack.h" |
f00aae0f | 46 | #include "location.h" |
14bc53a8 | 47 | #include "common/function-view.h" |
f8eba3c6 | 48 | |
f8eba3c6 TT |
49 | typedef struct symbol *symbolp; |
50 | DEF_VEC_P (symbolp); | |
51 | ||
52 | typedef struct type *typep; | |
53 | DEF_VEC_P (typep); | |
54 | ||
55 | /* An address entry is used to ensure that any given location is only | |
56 | added to the result a single time. It holds an address and the | |
57 | program space from which the address came. */ | |
58 | ||
59 | struct address_entry | |
60 | { | |
61 | struct program_space *pspace; | |
62 | CORE_ADDR addr; | |
63 | }; | |
64 | ||
f60e2d5c | 65 | typedef struct bound_minimal_symbol bound_minimal_symbol_d; |
40e084e1 | 66 | |
f60e2d5c | 67 | DEF_VEC_O (bound_minimal_symbol_d); |
40e084e1 | 68 | |
40e084e1 KS |
69 | /* A linespec. Elements of this structure are filled in by a parser |
70 | (either parse_linespec or some other function). The structure is | |
71 | then converted into SALs by convert_linespec_to_sals. */ | |
72 | ||
73 | struct linespec | |
74 | { | |
00e52e53 | 75 | /* An explicit location describing the SaLs. */ |
67994074 | 76 | struct explicit_location explicit_loc; |
40e084e1 KS |
77 | |
78 | /* The list of symtabs to search to which to limit the search. May not | |
00e52e53 KS |
79 | be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified |
80 | filename), FILE_SYMTABS should contain one single NULL member. This | |
81 | will cause the code to use the default symtab. */ | |
ec94af83 | 82 | VEC (symtab_ptr) *file_symtabs; |
40e084e1 | 83 | |
40e084e1 KS |
84 | /* A list of matching function symbols and minimal symbols. Both lists |
85 | may be NULL if no matching symbols were found. */ | |
86 | VEC (symbolp) *function_symbols; | |
f60e2d5c | 87 | VEC (bound_minimal_symbol_d) *minimal_symbols; |
40e084e1 | 88 | |
40e084e1 KS |
89 | /* A structure of matching label symbols and the corresponding |
90 | function symbol in which the label was found. Both may be NULL | |
91 | or both must be non-NULL. */ | |
92 | struct | |
93 | { | |
94 | VEC (symbolp) *label_symbols; | |
95 | VEC (symbolp) *function_symbols; | |
96 | } labels; | |
40e084e1 KS |
97 | }; |
98 | typedef struct linespec *linespec_p; | |
99 | ||
33f448b1 JK |
100 | /* A canonical linespec represented as a symtab-related string. |
101 | ||
102 | Each entry represents the "SYMTAB:SUFFIX" linespec string. | |
103 | SYMTAB can be converted for example by symtab_to_fullname or | |
104 | symtab_to_filename_for_display as needed. */ | |
105 | ||
106 | struct linespec_canonical_name | |
107 | { | |
108 | /* Remaining text part of the linespec string. */ | |
109 | char *suffix; | |
110 | ||
111 | /* If NULL then SUFFIX is the whole linespec string. */ | |
112 | struct symtab *symtab; | |
113 | }; | |
114 | ||
f8eba3c6 TT |
115 | /* An instance of this is used to keep all state while linespec |
116 | operates. This instance is passed around as a 'this' pointer to | |
117 | the various implementation methods. */ | |
118 | ||
119 | struct linespec_state | |
120 | { | |
40e084e1 KS |
121 | /* The language in use during linespec processing. */ |
122 | const struct language_defn *language; | |
123 | ||
f8eba3c6 TT |
124 | /* The program space as seen when the module was entered. */ |
125 | struct program_space *program_space; | |
126 | ||
c2f4122d PA |
127 | /* If not NULL, the search is restricted to just this program |
128 | space. */ | |
129 | struct program_space *search_pspace; | |
130 | ||
f8eba3c6 TT |
131 | /* The default symtab to use, if no other symtab is specified. */ |
132 | struct symtab *default_symtab; | |
133 | ||
134 | /* The default line to use. */ | |
135 | int default_line; | |
136 | ||
f8eba3c6 TT |
137 | /* The 'funfirstline' value that was passed in to decode_line_1 or |
138 | decode_line_full. */ | |
139 | int funfirstline; | |
140 | ||
141 | /* Nonzero if we are running in 'list' mode; see decode_line_list. */ | |
142 | int list_mode; | |
143 | ||
144 | /* The 'canonical' value passed to decode_line_full, or NULL. */ | |
145 | struct linespec_result *canonical; | |
146 | ||
147 | /* Canonical strings that mirror the symtabs_and_lines result. */ | |
33f448b1 | 148 | struct linespec_canonical_name *canonical_names; |
f8eba3c6 TT |
149 | |
150 | /* This is a set of address_entry objects which is used to prevent | |
151 | duplicate symbols from being entered into the result. */ | |
152 | htab_t addr_set; | |
00e52e53 KS |
153 | |
154 | /* Are we building a linespec? */ | |
155 | int is_linespec; | |
f8eba3c6 TT |
156 | }; |
157 | ||
158 | /* This is a helper object that is used when collecting symbols into a | |
159 | result. */ | |
160 | ||
161 | struct collect_info | |
162 | { | |
163 | /* The linespec object in use. */ | |
164 | struct linespec_state *state; | |
165 | ||
40e084e1 | 166 | /* A list of symtabs to which to restrict matches. */ |
ec94af83 | 167 | VEC (symtab_ptr) *file_symtabs; |
40e084e1 | 168 | |
f8eba3c6 | 169 | /* The result being accumulated. */ |
40e084e1 KS |
170 | struct |
171 | { | |
172 | VEC (symbolp) *symbols; | |
f60e2d5c | 173 | VEC (bound_minimal_symbol_d) *minimal_symbols; |
40e084e1 | 174 | } result; |
14bc53a8 PA |
175 | |
176 | /* Possibly add a symbol to the results. */ | |
177 | bool add_symbol (symbol *sym); | |
f8eba3c6 | 178 | }; |
50641945 | 179 | |
14bc53a8 PA |
180 | bool |
181 | collect_info::add_symbol (symbol *sym) | |
182 | { | |
183 | /* In list mode, add all matching symbols, regardless of class. | |
184 | This allows the user to type "list a_global_variable". */ | |
185 | if (SYMBOL_CLASS (sym) == LOC_BLOCK || this->state->list_mode) | |
186 | VEC_safe_push (symbolp, this->result.symbols, sym); | |
187 | ||
188 | /* Continue iterating. */ | |
189 | return true; | |
190 | } | |
191 | ||
40e084e1 | 192 | /* Token types */ |
50641945 | 193 | |
40e084e1 KS |
194 | enum ls_token_type |
195 | { | |
196 | /* A keyword */ | |
197 | LSTOKEN_KEYWORD = 0, | |
44fe14ab | 198 | |
40e084e1 KS |
199 | /* A colon "separator" */ |
200 | LSTOKEN_COLON, | |
44fe14ab | 201 | |
40e084e1 KS |
202 | /* A string */ |
203 | LSTOKEN_STRING, | |
0960f083 | 204 | |
40e084e1 KS |
205 | /* A number */ |
206 | LSTOKEN_NUMBER, | |
207 | ||
208 | /* A comma */ | |
209 | LSTOKEN_COMMA, | |
210 | ||
211 | /* EOI (end of input) */ | |
212 | LSTOKEN_EOI, | |
213 | ||
214 | /* Consumed token */ | |
215 | LSTOKEN_CONSUMED | |
216 | }; | |
217 | typedef enum ls_token_type linespec_token_type; | |
218 | ||
219 | /* List of keywords */ | |
220 | ||
221 | static const char * const linespec_keywords[] = { "if", "thread", "task" }; | |
0578b14e | 222 | #define IF_KEYWORD_INDEX 0 |
40e084e1 KS |
223 | |
224 | /* A token of the linespec lexer */ | |
225 | ||
226 | struct ls_token | |
227 | { | |
228 | /* The type of the token */ | |
229 | linespec_token_type type; | |
230 | ||
231 | /* Data for the token */ | |
232 | union | |
233 | { | |
234 | /* A string, given as a stoken */ | |
235 | struct stoken string; | |
236 | ||
237 | /* A keyword */ | |
238 | const char *keyword; | |
239 | } data; | |
240 | }; | |
241 | typedef struct ls_token linespec_token; | |
242 | ||
243 | #define LS_TOKEN_STOKEN(TOK) (TOK).data.string | |
244 | #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword | |
245 | ||
246 | /* An instance of the linespec parser. */ | |
247 | ||
248 | struct ls_parser | |
249 | { | |
250 | /* Lexer internal data */ | |
251 | struct | |
252 | { | |
253 | /* Save head of input stream. */ | |
d7561cbb | 254 | const char *saved_arg; |
d2630e69 | 255 | |
40e084e1 | 256 | /* Head of the input stream. */ |
f00aae0f KS |
257 | const char *stream; |
258 | #define PARSER_STREAM(P) ((P)->lexer.stream) | |
614b3b14 | 259 | |
40e084e1 KS |
260 | /* The current token. */ |
261 | linespec_token current; | |
262 | } lexer; | |
93d91629 | 263 | |
40e084e1 KS |
264 | /* Is the entire linespec quote-enclosed? */ |
265 | int is_quote_enclosed; | |
266 | ||
267 | /* The state of the parse. */ | |
268 | struct linespec_state state; | |
269 | #define PARSER_STATE(PPTR) (&(PPTR)->state) | |
4224873a | 270 | |
40e084e1 KS |
271 | /* The result of the parse. */ |
272 | struct linespec result; | |
273 | #define PARSER_RESULT(PPTR) (&(PPTR)->result) | |
274 | }; | |
275 | typedef struct ls_parser linespec_parser; | |
50641945 | 276 | |
00e52e53 KS |
277 | /* A convenience macro for accessing the explicit location result of |
278 | the parser. */ | |
67994074 | 279 | #define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc) |
00e52e53 | 280 | |
40e084e1 | 281 | /* Prototypes for local functions. */ |
50641945 | 282 | |
14bc53a8 PA |
283 | static void iterate_over_file_blocks |
284 | (struct symtab *symtab, const char *name, domain_enum domain, | |
285 | gdb::function_view<symbol_found_callback_ftype> callback); | |
4eeaa230 | 286 | |
40e084e1 KS |
287 | static void initialize_defaults (struct symtab **default_symtab, |
288 | int *default_line); | |
50641945 | 289 | |
a06efdd6 | 290 | CORE_ADDR linespec_expression_to_pc (const char **exp_ptr); |
aee8d8ba | 291 | |
40e084e1 KS |
292 | static struct symtabs_and_lines decode_objc (struct linespec_state *self, |
293 | linespec_p ls, | |
f00aae0f | 294 | const char *arg); |
aee8d8ba | 295 | |
c2f4122d PA |
296 | static VEC (symtab_ptr) *symtabs_from_filename (const char *, |
297 | struct program_space *pspace); | |
50641945 | 298 | |
40e084e1 KS |
299 | static VEC (symbolp) *find_label_symbols (struct linespec_state *self, |
300 | VEC (symbolp) *function_symbols, | |
301 | VEC (symbolp) **label_funcs_ret, | |
302 | const char *name); | |
50641945 | 303 | |
b1ae631a | 304 | static void find_linespec_symbols (struct linespec_state *self, |
ec94af83 | 305 | VEC (symtab_ptr) *file_symtabs, |
b1ae631a DE |
306 | const char *name, |
307 | VEC (symbolp) **symbols, | |
f60e2d5c | 308 | VEC (bound_minimal_symbol_d) **minsyms); |
f8eba3c6 | 309 | |
40e084e1 KS |
310 | static struct line_offset |
311 | linespec_parse_variable (struct linespec_state *self, | |
312 | const char *variable); | |
889f28e2 | 313 | |
f8eba3c6 TT |
314 | static int symbol_to_sal (struct symtab_and_line *result, |
315 | int funfirstline, struct symbol *sym); | |
50641945 | 316 | |
f8eba3c6 TT |
317 | static void add_matching_symbols_to_info (const char *name, |
318 | struct collect_info *info, | |
319 | struct program_space *pspace); | |
f3c39e76 | 320 | |
f8eba3c6 TT |
321 | static void add_all_symbol_names_from_pspace (struct collect_info *info, |
322 | struct program_space *pspace, | |
323 | VEC (const_char_ptr) *names); | |
9ef07c8c | 324 | |
c2f4122d PA |
325 | static VEC (symtab_ptr) * |
326 | collect_symtabs_from_filename (const char *file, | |
327 | struct program_space *pspace); | |
84fba31b | 328 | |
40e084e1 KS |
329 | static void decode_digits_ordinary (struct linespec_state *self, |
330 | linespec_p ls, | |
331 | int line, | |
332 | struct symtabs_and_lines *sals, | |
333 | struct linetable_entry **best_entry); | |
14e91ac5 | 334 | |
40e084e1 KS |
335 | static void decode_digits_list_mode (struct linespec_state *self, |
336 | linespec_p ls, | |
337 | struct symtabs_and_lines *values, | |
338 | struct symtab_and_line val); | |
0f5238ed | 339 | |
40e084e1 KS |
340 | static void minsym_found (struct linespec_state *self, struct objfile *objfile, |
341 | struct minimal_symbol *msymbol, | |
342 | struct symtabs_and_lines *result); | |
bca02a8a | 343 | |
40e084e1 | 344 | static int compare_symbols (const void *a, const void *b); |
413dad4d | 345 | |
40e084e1 | 346 | static int compare_msymbols (const void *a, const void *b); |
413dad4d | 347 | |
40e084e1 KS |
348 | /* Permitted quote characters for the parser. This is different from the |
349 | completer's quote characters to allow backward compatibility with the | |
350 | previous parser. */ | |
351 | static const char *const linespec_quote_characters = "\"\'"; | |
f8eba3c6 | 352 | |
40e084e1 KS |
353 | /* Lexer functions. */ |
354 | ||
355 | /* Lex a number from the input in PARSER. This only supports | |
dd3818c8 KS |
356 | decimal numbers. |
357 | ||
d7cbec71 | 358 | Return true if input is decimal numbers. Return false if not. */ |
40e084e1 | 359 | |
d7cbec71 HZ |
360 | static int |
361 | linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp) | |
40e084e1 | 362 | { |
d7cbec71 HZ |
363 | tokenp->type = LSTOKEN_NUMBER; |
364 | LS_TOKEN_STOKEN (*tokenp).length = 0; | |
365 | LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser); | |
40e084e1 KS |
366 | |
367 | /* Keep any sign at the start of the stream. */ | |
368 | if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-') | |
369 | { | |
d7cbec71 | 370 | ++LS_TOKEN_STOKEN (*tokenp).length; |
40e084e1 KS |
371 | ++(PARSER_STREAM (parser)); |
372 | } | |
373 | ||
374 | while (isdigit (*PARSER_STREAM (parser))) | |
375 | { | |
d7cbec71 | 376 | ++LS_TOKEN_STOKEN (*tokenp).length; |
40e084e1 | 377 | ++(PARSER_STREAM (parser)); |
f8eba3c6 | 378 | } |
40e084e1 | 379 | |
dd3818c8 | 380 | /* If the next character in the input buffer is not a space, comma, |
eff9c3e6 | 381 | quote, or colon, this input does not represent a number. */ |
dd3818c8 KS |
382 | if (*PARSER_STREAM (parser) != '\0' |
383 | && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ',' | |
eff9c3e6 KS |
384 | && *PARSER_STREAM (parser) != ':' |
385 | && !strchr (linespec_quote_characters, *PARSER_STREAM (parser))) | |
d7cbec71 HZ |
386 | { |
387 | PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr; | |
388 | return 0; | |
389 | } | |
390 | ||
391 | return 1; | |
f8eba3c6 TT |
392 | } |
393 | ||
32b40af9 | 394 | /* See linespec.h. */ |
f8eba3c6 | 395 | |
0578b14e | 396 | const char * |
40e084e1 | 397 | linespec_lexer_lex_keyword (const char *p) |
f8eba3c6 | 398 | { |
40e084e1 | 399 | int i; |
f8eba3c6 | 400 | |
40e084e1 KS |
401 | if (p != NULL) |
402 | { | |
403 | for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i) | |
404 | { | |
405 | int len = strlen (linespec_keywords[i]); | |
406 | ||
407 | /* If P begins with one of the keywords and the next | |
0578b14e KS |
408 | character is whitespace, we may have found a keyword. |
409 | It is only a keyword if it is not followed by another | |
410 | keyword. */ | |
40e084e1 | 411 | if (strncmp (p, linespec_keywords[i], len) == 0 |
0578b14e KS |
412 | && isspace (p[len])) |
413 | { | |
414 | int j; | |
415 | ||
416 | /* Special case: "if" ALWAYS stops the lexer, since it | |
417 | is not possible to predict what is going to appear in | |
418 | the condition, which can only be parsed after SaLs have | |
419 | been found. */ | |
420 | if (i != IF_KEYWORD_INDEX) | |
421 | { | |
422 | p += len; | |
423 | p = skip_spaces_const (p); | |
424 | for (j = 0; j < ARRAY_SIZE (linespec_keywords); ++j) | |
425 | { | |
426 | int nextlen = strlen (linespec_keywords[j]); | |
427 | ||
428 | if (strncmp (p, linespec_keywords[j], nextlen) == 0 | |
429 | && isspace (p[nextlen])) | |
430 | return NULL; | |
431 | } | |
432 | } | |
433 | ||
434 | return linespec_keywords[i]; | |
435 | } | |
40e084e1 KS |
436 | } |
437 | } | |
438 | ||
439 | return NULL; | |
f8eba3c6 TT |
440 | } |
441 | ||
87f0e720 | 442 | /* See description in linespec.h. */ |
f8eba3c6 | 443 | |
87f0e720 | 444 | int |
40e084e1 | 445 | is_ada_operator (const char *string) |
f8eba3c6 | 446 | { |
40e084e1 | 447 | const struct ada_opname_map *mapping; |
f8eba3c6 | 448 | |
40e084e1 KS |
449 | for (mapping = ada_opname_table; |
450 | mapping->encoded != NULL | |
61012eef | 451 | && !startswith (string, mapping->decoded); ++mapping) |
40e084e1 KS |
452 | ; |
453 | ||
454 | return mapping->decoded == NULL ? 0 : strlen (mapping->decoded); | |
f8eba3c6 TT |
455 | } |
456 | ||
40e084e1 KS |
457 | /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return |
458 | the location of QUOTE_CHAR, or NULL if not found. */ | |
f8eba3c6 | 459 | |
40e084e1 KS |
460 | static const char * |
461 | skip_quote_char (const char *string, char quote_char) | |
f8eba3c6 | 462 | { |
40e084e1 | 463 | const char *p, *last; |
f8eba3c6 | 464 | |
40e084e1 KS |
465 | p = last = find_toplevel_char (string, quote_char); |
466 | while (p && *p != '\0' && *p != ':') | |
467 | { | |
468 | p = find_toplevel_char (p, quote_char); | |
469 | if (p != NULL) | |
470 | last = p++; | |
471 | } | |
f8eba3c6 | 472 | |
40e084e1 | 473 | return last; |
f8eba3c6 | 474 | } |
50641945 | 475 | |
40e084e1 KS |
476 | /* Make a writable copy of the string given in TOKEN, trimming |
477 | any trailing whitespace. */ | |
50641945 | 478 | |
40e084e1 KS |
479 | static char * |
480 | copy_token_string (linespec_token token) | |
50641945 | 481 | { |
40e084e1 | 482 | char *str, *s; |
e0881a8e | 483 | |
40e084e1 KS |
484 | if (token.type == LSTOKEN_KEYWORD) |
485 | return xstrdup (LS_TOKEN_KEYWORD (token)); | |
255e7dbf | 486 | |
40e084e1 KS |
487 | str = savestring (LS_TOKEN_STOKEN (token).ptr, |
488 | LS_TOKEN_STOKEN (token).length); | |
489 | s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length); | |
490 | *s = '\0'; | |
e0881a8e | 491 | |
40e084e1 KS |
492 | return str; |
493 | } | |
255e7dbf | 494 | |
40e084e1 | 495 | /* Does P represent the end of a quote-enclosed linespec? */ |
f3a5f1de | 496 | |
40e084e1 KS |
497 | static int |
498 | is_closing_quote_enclosed (const char *p) | |
499 | { | |
500 | if (strchr (linespec_quote_characters, *p)) | |
501 | ++p; | |
502 | p = skip_spaces ((char *) p); | |
503 | return (*p == '\0' || linespec_lexer_lex_keyword (p)); | |
50641945 FN |
504 | } |
505 | ||
40e084e1 KS |
506 | /* Find the end of the parameter list that starts with *INPUT. |
507 | This helper function assists with lexing string segments | |
508 | which might contain valid (non-terminating) commas. */ | |
481860b3 | 509 | |
d7561cbb KS |
510 | static const char * |
511 | find_parameter_list_end (const char *input) | |
481860b3 | 512 | { |
40e084e1 KS |
513 | char end_char, start_char; |
514 | int depth; | |
d7561cbb | 515 | const char *p; |
481860b3 | 516 | |
40e084e1 KS |
517 | start_char = *input; |
518 | if (start_char == '(') | |
519 | end_char = ')'; | |
520 | else if (start_char == '<') | |
521 | end_char = '>'; | |
522 | else | |
523 | return NULL; | |
481860b3 | 524 | |
40e084e1 KS |
525 | p = input; |
526 | depth = 0; | |
527 | while (*p) | |
481860b3 | 528 | { |
40e084e1 KS |
529 | if (*p == start_char) |
530 | ++depth; | |
531 | else if (*p == end_char) | |
532 | { | |
533 | if (--depth == 0) | |
534 | { | |
535 | ++p; | |
536 | break; | |
537 | } | |
538 | } | |
539 | ++p; | |
481860b3 | 540 | } |
40e084e1 KS |
541 | |
542 | return p; | |
481860b3 GB |
543 | } |
544 | ||
74ccd7f5 | 545 | |
40e084e1 KS |
546 | /* Lex a string from the input in PARSER. */ |
547 | ||
548 | static linespec_token | |
549 | linespec_lexer_lex_string (linespec_parser *parser) | |
74ccd7f5 | 550 | { |
40e084e1 | 551 | linespec_token token; |
d7561cbb | 552 | const char *start = PARSER_STREAM (parser); |
74ccd7f5 | 553 | |
40e084e1 | 554 | token.type = LSTOKEN_STRING; |
74ccd7f5 | 555 | |
40e084e1 KS |
556 | /* If the input stream starts with a quote character, skip to the next |
557 | quote character, regardless of the content. */ | |
558 | if (strchr (linespec_quote_characters, *PARSER_STREAM (parser))) | |
559 | { | |
560 | const char *end; | |
561 | char quote_char = *PARSER_STREAM (parser); | |
50641945 | 562 | |
40e084e1 KS |
563 | /* Special case: Ada operators. */ |
564 | if (PARSER_STATE (parser)->language->la_language == language_ada | |
565 | && quote_char == '\"') | |
566 | { | |
567 | int len = is_ada_operator (PARSER_STREAM (parser)); | |
50641945 | 568 | |
40e084e1 KS |
569 | if (len != 0) |
570 | { | |
571 | /* The input is an Ada operator. Return the quoted string | |
572 | as-is. */ | |
573 | LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser); | |
574 | LS_TOKEN_STOKEN (token).length = len; | |
575 | PARSER_STREAM (parser) += len; | |
576 | return token; | |
577 | } | |
f8eba3c6 | 578 | |
40e084e1 KS |
579 | /* The input does not represent an Ada operator -- fall through |
580 | to normal quoted string handling. */ | |
581 | } | |
f8eba3c6 | 582 | |
40e084e1 KS |
583 | /* Skip past the beginning quote. */ |
584 | ++(PARSER_STREAM (parser)); | |
74ccd7f5 | 585 | |
40e084e1 KS |
586 | /* Mark the start of the string. */ |
587 | LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser); | |
f8eba3c6 | 588 | |
40e084e1 KS |
589 | /* Skip to the ending quote. */ |
590 | end = skip_quote_char (PARSER_STREAM (parser), quote_char); | |
591 | ||
592 | /* Error if the input did not terminate properly. */ | |
593 | if (end == NULL) | |
594 | error (_("unmatched quote")); | |
595 | ||
596 | /* Skip over the ending quote and mark the length of the string. */ | |
597 | PARSER_STREAM (parser) = (char *) ++end; | |
598 | LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start; | |
599 | } | |
600 | else | |
601 | { | |
d7561cbb | 602 | const char *p; |
40e084e1 KS |
603 | |
604 | /* Otherwise, only identifier characters are permitted. | |
605 | Spaces are the exception. In general, we keep spaces, | |
606 | but only if the next characters in the input do not resolve | |
607 | to one of the keywords. | |
608 | ||
609 | This allows users to forgo quoting CV-qualifiers, template arguments, | |
610 | and similar common language constructs. */ | |
611 | ||
612 | while (1) | |
613 | { | |
614 | if (isspace (*PARSER_STREAM (parser))) | |
615 | { | |
d7561cbb | 616 | p = skip_spaces_const (PARSER_STREAM (parser)); |
7c09e5a0 DE |
617 | /* When we get here we know we've found something followed by |
618 | a space (we skip over parens and templates below). | |
619 | So if we find a keyword now, we know it is a keyword and not, | |
620 | say, a function name. */ | |
40e084e1 KS |
621 | if (linespec_lexer_lex_keyword (p) != NULL) |
622 | { | |
623 | LS_TOKEN_STOKEN (token).ptr = start; | |
624 | LS_TOKEN_STOKEN (token).length | |
625 | = PARSER_STREAM (parser) - start; | |
626 | return token; | |
627 | } | |
628 | ||
629 | /* Advance past the whitespace. */ | |
630 | PARSER_STREAM (parser) = p; | |
631 | } | |
632 | ||
633 | /* If the next character is EOI or (single) ':', the | |
634 | string is complete; return the token. */ | |
635 | if (*PARSER_STREAM (parser) == 0) | |
636 | { | |
637 | LS_TOKEN_STOKEN (token).ptr = start; | |
638 | LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start; | |
639 | return token; | |
640 | } | |
641 | else if (PARSER_STREAM (parser)[0] == ':') | |
642 | { | |
643 | /* Do not tokenize the C++ scope operator. */ | |
644 | if (PARSER_STREAM (parser)[1] == ':') | |
645 | ++(PARSER_STREAM (parser)); | |
646 | ||
647 | /* Do not tokenify if the input length so far is one | |
648 | (i.e, a single-letter drive name) and the next character | |
649 | is a directory separator. This allows Windows-style | |
650 | paths to be recognized as filenames without quoting it. */ | |
651 | else if ((PARSER_STREAM (parser) - start) != 1 | |
652 | || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1])) | |
653 | { | |
654 | LS_TOKEN_STOKEN (token).ptr = start; | |
655 | LS_TOKEN_STOKEN (token).length | |
656 | = PARSER_STREAM (parser) - start; | |
657 | return token; | |
658 | } | |
659 | } | |
660 | /* Special case: permit quote-enclosed linespecs. */ | |
661 | else if (parser->is_quote_enclosed | |
662 | && strchr (linespec_quote_characters, | |
663 | *PARSER_STREAM (parser)) | |
664 | && is_closing_quote_enclosed (PARSER_STREAM (parser))) | |
665 | { | |
666 | LS_TOKEN_STOKEN (token).ptr = start; | |
667 | LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start; | |
668 | return token; | |
669 | } | |
670 | /* Because commas may terminate a linespec and appear in | |
671 | the middle of valid string input, special cases for | |
672 | '<' and '(' are necessary. */ | |
673 | else if (*PARSER_STREAM (parser) == '<' | |
674 | || *PARSER_STREAM (parser) == '(') | |
675 | { | |
d7561cbb | 676 | const char *p; |
40e084e1 KS |
677 | |
678 | p = find_parameter_list_end (PARSER_STREAM (parser)); | |
679 | if (p != NULL) | |
680 | { | |
681 | PARSER_STREAM (parser) = p; | |
682 | continue; | |
683 | } | |
684 | } | |
685 | /* Commas are terminators, but not if they are part of an | |
686 | operator name. */ | |
687 | else if (*PARSER_STREAM (parser) == ',') | |
688 | { | |
689 | if ((PARSER_STATE (parser)->language->la_language | |
690 | == language_cplus) | |
691 | && (PARSER_STREAM (parser) - start) > 8 | |
692 | /* strlen ("operator") */) | |
693 | { | |
acaa662f | 694 | const char *p = strstr (start, "operator"); |
40e084e1 KS |
695 | |
696 | if (p != NULL && is_operator_name (p)) | |
697 | { | |
698 | /* This is an operator name. Keep going. */ | |
699 | ++(PARSER_STREAM (parser)); | |
700 | continue; | |
701 | } | |
702 | } | |
703 | ||
704 | /* Comma terminates the string. */ | |
705 | LS_TOKEN_STOKEN (token).ptr = start; | |
706 | LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start; | |
707 | return token; | |
708 | } | |
709 | ||
710 | /* Advance the stream. */ | |
711 | ++(PARSER_STREAM (parser)); | |
712 | } | |
713 | } | |
714 | ||
715 | return token; | |
716 | } | |
717 | ||
718 | /* Lex a single linespec token from PARSER. */ | |
719 | ||
720 | static linespec_token | |
721 | linespec_lexer_lex_one (linespec_parser *parser) | |
722 | { | |
723 | const char *keyword; | |
724 | ||
725 | if (parser->lexer.current.type == LSTOKEN_CONSUMED) | |
726 | { | |
727 | /* Skip any whitespace. */ | |
d7561cbb | 728 | PARSER_STREAM (parser) = skip_spaces_const (PARSER_STREAM (parser)); |
40e084e1 | 729 | |
7c09e5a0 | 730 | /* Check for a keyword, they end the linespec. */ |
0578b14e | 731 | keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser)); |
40e084e1 KS |
732 | if (keyword != NULL) |
733 | { | |
734 | parser->lexer.current.type = LSTOKEN_KEYWORD; | |
735 | LS_TOKEN_KEYWORD (parser->lexer.current) = keyword; | |
0578b14e KS |
736 | /* We do not advance the stream here intentionally: |
737 | we would like lexing to stop when a keyword is seen. | |
738 | ||
739 | PARSER_STREAM (parser) += strlen (keyword); */ | |
740 | ||
40e084e1 KS |
741 | return parser->lexer.current; |
742 | } | |
743 | ||
744 | /* Handle other tokens. */ | |
745 | switch (*PARSER_STREAM (parser)) | |
746 | { | |
747 | case 0: | |
748 | parser->lexer.current.type = LSTOKEN_EOI; | |
749 | break; | |
750 | ||
751 | case '+': case '-': | |
752 | case '0': case '1': case '2': case '3': case '4': | |
753 | case '5': case '6': case '7': case '8': case '9': | |
d7cbec71 HZ |
754 | if (!linespec_lexer_lex_number (parser, &(parser->lexer.current))) |
755 | parser->lexer.current = linespec_lexer_lex_string (parser); | |
40e084e1 KS |
756 | break; |
757 | ||
758 | case ':': | |
759 | /* If we have a scope operator, lex the input as a string. | |
760 | Otherwise, return LSTOKEN_COLON. */ | |
761 | if (PARSER_STREAM (parser)[1] == ':') | |
762 | parser->lexer.current = linespec_lexer_lex_string (parser); | |
763 | else | |
764 | { | |
765 | parser->lexer.current.type = LSTOKEN_COLON; | |
766 | ++(PARSER_STREAM (parser)); | |
767 | } | |
768 | break; | |
769 | ||
770 | case '\'': case '\"': | |
771 | /* Special case: permit quote-enclosed linespecs. */ | |
772 | if (parser->is_quote_enclosed | |
773 | && is_closing_quote_enclosed (PARSER_STREAM (parser))) | |
774 | { | |
775 | ++(PARSER_STREAM (parser)); | |
776 | parser->lexer.current.type = LSTOKEN_EOI; | |
777 | } | |
778 | else | |
779 | parser->lexer.current = linespec_lexer_lex_string (parser); | |
780 | break; | |
781 | ||
782 | case ',': | |
783 | parser->lexer.current.type = LSTOKEN_COMMA; | |
784 | LS_TOKEN_STOKEN (parser->lexer.current).ptr | |
785 | = PARSER_STREAM (parser); | |
786 | LS_TOKEN_STOKEN (parser->lexer.current).length = 1; | |
787 | ++(PARSER_STREAM (parser)); | |
788 | break; | |
789 | ||
790 | default: | |
791 | /* If the input is not a number, it must be a string. | |
792 | [Keywords were already considered above.] */ | |
793 | parser->lexer.current = linespec_lexer_lex_string (parser); | |
794 | break; | |
795 | } | |
796 | } | |
797 | ||
798 | return parser->lexer.current; | |
799 | } | |
800 | ||
801 | /* Consume the current token and return the next token in PARSER's | |
802 | input stream. */ | |
803 | ||
804 | static linespec_token | |
805 | linespec_lexer_consume_token (linespec_parser *parser) | |
806 | { | |
807 | parser->lexer.current.type = LSTOKEN_CONSUMED; | |
808 | return linespec_lexer_lex_one (parser); | |
809 | } | |
810 | ||
811 | /* Return the next token without consuming the current token. */ | |
812 | ||
813 | static linespec_token | |
814 | linespec_lexer_peek_token (linespec_parser *parser) | |
815 | { | |
816 | linespec_token next; | |
d7561cbb | 817 | const char *saved_stream = PARSER_STREAM (parser); |
40e084e1 KS |
818 | linespec_token saved_token = parser->lexer.current; |
819 | ||
820 | next = linespec_lexer_consume_token (parser); | |
821 | PARSER_STREAM (parser) = saved_stream; | |
822 | parser->lexer.current = saved_token; | |
823 | return next; | |
824 | } | |
825 | ||
826 | /* Helper functions. */ | |
827 | ||
828 | /* Add SAL to SALS. */ | |
829 | ||
830 | static void | |
831 | add_sal_to_sals_basic (struct symtabs_and_lines *sals, | |
832 | struct symtab_and_line *sal) | |
833 | { | |
834 | ++sals->nelts; | |
224c3ddb | 835 | sals->sals = XRESIZEVEC (struct symtab_and_line, sals->sals, sals->nelts); |
40e084e1 KS |
836 | sals->sals[sals->nelts - 1] = *sal; |
837 | } | |
838 | ||
839 | /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect | |
840 | the new sal, if needed. If not NULL, SYMNAME is the name of the | |
66f1999b KS |
841 | symbol to use when constructing the new canonical name. |
842 | ||
843 | If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the | |
844 | canonical name for the SAL. */ | |
40e084e1 KS |
845 | |
846 | static void | |
847 | add_sal_to_sals (struct linespec_state *self, | |
848 | struct symtabs_and_lines *sals, | |
849 | struct symtab_and_line *sal, | |
66f1999b | 850 | const char *symname, int literal_canonical) |
40e084e1 KS |
851 | { |
852 | add_sal_to_sals_basic (sals, sal); | |
853 | ||
854 | if (self->canonical) | |
855 | { | |
33f448b1 | 856 | struct linespec_canonical_name *canonical; |
40e084e1 | 857 | |
224c3ddb SM |
858 | self->canonical_names = XRESIZEVEC (struct linespec_canonical_name, |
859 | self->canonical_names, sals->nelts); | |
33f448b1 | 860 | canonical = &self->canonical_names[sals->nelts - 1]; |
4e04028d | 861 | if (!literal_canonical && sal->symtab) |
40e084e1 | 862 | { |
df140a0b TS |
863 | symtab_to_fullname (sal->symtab); |
864 | ||
40e084e1 KS |
865 | /* Note that the filter doesn't have to be a valid linespec |
866 | input. We only apply the ":LINE" treatment to Ada for | |
867 | the time being. */ | |
868 | if (symname != NULL && sal->line != 0 | |
869 | && self->language->la_language == language_ada) | |
33f448b1 | 870 | canonical->suffix = xstrprintf ("%s:%d", symname, sal->line); |
40e084e1 | 871 | else if (symname != NULL) |
33f448b1 | 872 | canonical->suffix = xstrdup (symname); |
40e084e1 | 873 | else |
33f448b1 JK |
874 | canonical->suffix = xstrprintf ("%d", sal->line); |
875 | canonical->symtab = sal->symtab; | |
876 | } | |
877 | else | |
878 | { | |
879 | if (symname != NULL) | |
880 | canonical->suffix = xstrdup (symname); | |
881 | else | |
e617b069 | 882 | canonical->suffix = xstrdup ("<unknown>"); |
33f448b1 | 883 | canonical->symtab = NULL; |
40e084e1 | 884 | } |
40e084e1 KS |
885 | } |
886 | } | |
887 | ||
888 | /* A hash function for address_entry. */ | |
889 | ||
890 | static hashval_t | |
891 | hash_address_entry (const void *p) | |
892 | { | |
9a3c8263 | 893 | const struct address_entry *aep = (const struct address_entry *) p; |
40e084e1 KS |
894 | hashval_t hash; |
895 | ||
896 | hash = iterative_hash_object (aep->pspace, 0); | |
897 | return iterative_hash_object (aep->addr, hash); | |
898 | } | |
899 | ||
900 | /* An equality function for address_entry. */ | |
901 | ||
902 | static int | |
903 | eq_address_entry (const void *a, const void *b) | |
904 | { | |
9a3c8263 SM |
905 | const struct address_entry *aea = (const struct address_entry *) a; |
906 | const struct address_entry *aeb = (const struct address_entry *) b; | |
40e084e1 KS |
907 | |
908 | return aea->pspace == aeb->pspace && aea->addr == aeb->addr; | |
909 | } | |
910 | ||
911 | /* Check whether the address, represented by PSPACE and ADDR, is | |
912 | already in the set. If so, return 0. Otherwise, add it and return | |
913 | 1. */ | |
914 | ||
915 | static int | |
916 | maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr) | |
917 | { | |
918 | struct address_entry e, *p; | |
919 | void **slot; | |
920 | ||
921 | e.pspace = pspace; | |
922 | e.addr = addr; | |
923 | slot = htab_find_slot (set, &e, INSERT); | |
924 | if (*slot) | |
925 | return 0; | |
926 | ||
927 | p = XNEW (struct address_entry); | |
928 | memcpy (p, &e, sizeof (struct address_entry)); | |
929 | *slot = p; | |
930 | ||
931 | return 1; | |
932 | } | |
933 | ||
40e084e1 KS |
934 | /* A helper that walks over all matching symtabs in all objfiles and |
935 | calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is | |
936 | not NULL, then the search is restricted to just that program | |
14bc53a8 | 937 | space. If INCLUDE_INLINE is true then symbols representing |
40e084e1 KS |
938 | inlined instances of functions will be included in the result. */ |
939 | ||
940 | static void | |
14bc53a8 PA |
941 | iterate_over_all_matching_symtabs |
942 | (struct linespec_state *state, const char *name, const domain_enum domain, | |
943 | struct program_space *search_pspace, bool include_inline, | |
944 | gdb::function_view<symbol_found_callback_ftype> callback) | |
40e084e1 KS |
945 | { |
946 | struct objfile *objfile; | |
947 | struct program_space *pspace; | |
40e084e1 | 948 | |
14bc53a8 PA |
949 | /* The routine to be used for comparison. */ |
950 | symbol_name_cmp_ftype symbol_name_cmp | |
951 | = (state->language->la_get_symbol_name_cmp != NULL | |
952 | ? state->language->la_get_symbol_name_cmp (name) | |
953 | : strcmp_iw); | |
40e084e1 KS |
954 | |
955 | ALL_PSPACES (pspace) | |
956 | { | |
957 | if (search_pspace != NULL && search_pspace != pspace) | |
958 | continue; | |
959 | if (pspace->executing_startup) | |
f8eba3c6 TT |
960 | continue; |
961 | ||
962 | set_current_program_space (pspace); | |
50641945 | 963 | |
f8eba3c6 TT |
964 | ALL_OBJFILES (objfile) |
965 | { | |
43f3e411 | 966 | struct compunit_symtab *cu; |
f8eba3c6 TT |
967 | |
968 | if (objfile->sf) | |
14bc53a8 PA |
969 | objfile->sf->qf->expand_symtabs_matching |
970 | (objfile, | |
971 | NULL, | |
972 | [&] (const char *symbol_name) | |
973 | { | |
974 | return symbol_name_cmp (symbol_name, name) == 0; | |
975 | }, | |
976 | NULL, | |
977 | ALL_DOMAIN); | |
f8eba3c6 | 978 | |
43f3e411 | 979 | ALL_OBJFILE_COMPUNITS (objfile, cu) |
f8eba3c6 | 980 | { |
43f3e411 DE |
981 | struct symtab *symtab = COMPUNIT_FILETABS (cu); |
982 | ||
14bc53a8 | 983 | iterate_over_file_blocks (symtab, name, domain, callback); |
481860b3 | 984 | |
d790cf0a DE |
985 | if (include_inline) |
986 | { | |
4eeaa230 | 987 | struct block *block; |
d790cf0a | 988 | int i; |
481860b3 | 989 | |
d790cf0a | 990 | for (i = FIRST_LOCAL_BLOCK; |
439247b6 DE |
991 | i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab)); |
992 | i++) | |
d790cf0a | 993 | { |
439247b6 | 994 | block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i); |
4ae24af0 | 995 | state->language->la_iterate_over_symbols |
14bc53a8 PA |
996 | (block, name, domain, [&] (symbol *sym) |
997 | { | |
998 | /* Restrict calls to CALLBACK to symbols | |
999 | representing inline symbols only. */ | |
1000 | if (SYMBOL_INLINED (sym)) | |
1001 | return callback (sym); | |
1002 | return true; | |
1003 | }); | |
481860b3 | 1004 | } |
f8eba3c6 TT |
1005 | } |
1006 | } | |
1007 | } | |
1008 | } | |
50641945 FN |
1009 | } |
1010 | ||
4eeaa230 DE |
1011 | /* Returns the block to be used for symbol searches from |
1012 | the current location. */ | |
e8eb7bc5 | 1013 | |
3977b71f | 1014 | static const struct block * |
e482a1a7 | 1015 | get_current_search_block (void) |
e8eb7bc5 | 1016 | { |
3977b71f | 1017 | const struct block *block; |
4eeaa230 | 1018 | enum language save_language; |
e8eb7bc5 | 1019 | |
4eeaa230 DE |
1020 | /* get_selected_block can change the current language when there is |
1021 | no selected frame yet. */ | |
1022 | save_language = current_language->la_language; | |
1023 | block = get_selected_block (0); | |
1024 | set_language (save_language); | |
e8eb7bc5 KS |
1025 | |
1026 | return block; | |
1027 | } | |
1028 | ||
4eeaa230 DE |
1029 | /* Iterate over static and global blocks. */ |
1030 | ||
1031 | static void | |
14bc53a8 PA |
1032 | iterate_over_file_blocks |
1033 | (struct symtab *symtab, const char *name, domain_enum domain, | |
1034 | gdb::function_view<symbol_found_callback_ftype> callback) | |
4eeaa230 DE |
1035 | { |
1036 | struct block *block; | |
1037 | ||
439247b6 | 1038 | for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK); |
4eeaa230 DE |
1039 | block != NULL; |
1040 | block = BLOCK_SUPERBLOCK (block)) | |
14bc53a8 | 1041 | LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback); |
4eeaa230 DE |
1042 | } |
1043 | ||
f8eba3c6 | 1044 | /* A helper for find_method. This finds all methods in type T which |
40e084e1 | 1045 | match NAME. It adds matching symbol names to RESULT_NAMES, and |
f8eba3c6 | 1046 | adds T's direct superclasses to SUPERCLASSES. */ |
50641945 | 1047 | |
f8eba3c6 TT |
1048 | static void |
1049 | find_methods (struct type *t, const char *name, | |
1050 | VEC (const_char_ptr) **result_names, | |
1051 | VEC (typep) **superclasses) | |
50641945 | 1052 | { |
50641945 | 1053 | int ibase; |
0d5cff50 | 1054 | const char *class_name = type_name_no_tag (t); |
c00f8484 | 1055 | |
50641945 FN |
1056 | /* Ignore this class if it doesn't have a name. This is ugly, but |
1057 | unless we figure out how to get the physname without the name of | |
1058 | the class, then the loop can't do any good. */ | |
f8eba3c6 | 1059 | if (class_name) |
50641945 FN |
1060 | { |
1061 | int method_counter; | |
1062 | ||
f168693b | 1063 | t = check_typedef (t); |
50641945 FN |
1064 | |
1065 | /* Loop over each method name. At this level, all overloads of a name | |
1066 | are counted as a single name. There is an inner loop which loops over | |
1067 | each overload. */ | |
1068 | ||
1069 | for (method_counter = TYPE_NFN_FIELDS (t) - 1; | |
1070 | method_counter >= 0; | |
1071 | --method_counter) | |
1072 | { | |
0d5cff50 | 1073 | const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter); |
50641945 FN |
1074 | char dem_opname[64]; |
1075 | ||
61012eef GB |
1076 | if (startswith (method_name, "__") || |
1077 | startswith (method_name, "op") || | |
1078 | startswith (method_name, "type")) | |
50641945 FN |
1079 | { |
1080 | if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI)) | |
1081 | method_name = dem_opname; | |
1082 | else if (cplus_demangle_opname (method_name, dem_opname, 0)) | |
1083 | method_name = dem_opname; | |
1084 | } | |
1085 | ||
f8eba3c6 TT |
1086 | if (strcmp_iw (method_name, name) == 0) |
1087 | { | |
1088 | int field_counter; | |
aee8d8ba | 1089 | |
f8eba3c6 TT |
1090 | for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter) |
1091 | - 1); | |
1092 | field_counter >= 0; | |
1093 | --field_counter) | |
1094 | { | |
1095 | struct fn_field *f; | |
1096 | const char *phys_name; | |
1097 | ||
1098 | f = TYPE_FN_FIELDLIST1 (t, method_counter); | |
1099 | if (TYPE_FN_FIELD_STUB (f, field_counter)) | |
1100 | continue; | |
1101 | phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter); | |
1102 | VEC_safe_push (const_char_ptr, *result_names, phys_name); | |
1103 | } | |
1104 | } | |
aee8d8ba DC |
1105 | } |
1106 | } | |
1107 | ||
f8eba3c6 TT |
1108 | for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++) |
1109 | VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase)); | |
50641945 FN |
1110 | } |
1111 | ||
50641945 FN |
1112 | /* Find an instance of the character C in the string S that is outside |
1113 | of all parenthesis pairs, single-quoted strings, and double-quoted | |
8120c9d5 EZ |
1114 | strings. Also, ignore the char within a template name, like a ',' |
1115 | within foo<int, int>. */ | |
1116 | ||
87f0e720 | 1117 | const char * |
40e084e1 | 1118 | find_toplevel_char (const char *s, char c) |
50641945 FN |
1119 | { |
1120 | int quoted = 0; /* zero if we're not in quotes; | |
1121 | '"' if we're in a double-quoted string; | |
1122 | '\'' if we're in a single-quoted string. */ | |
a04257e6 | 1123 | int depth = 0; /* Number of unclosed parens we've seen. */ |
40e084e1 | 1124 | const char *scan; |
50641945 FN |
1125 | |
1126 | for (scan = s; *scan; scan++) | |
1127 | { | |
1128 | if (quoted) | |
1129 | { | |
1130 | if (*scan == quoted) | |
1131 | quoted = 0; | |
1132 | else if (*scan == '\\' && *(scan + 1)) | |
1133 | scan++; | |
1134 | } | |
1135 | else if (*scan == c && ! quoted && depth == 0) | |
1136 | return scan; | |
1137 | else if (*scan == '"' || *scan == '\'') | |
1138 | quoted = *scan; | |
8120c9d5 | 1139 | else if (*scan == '(' || *scan == '<') |
50641945 | 1140 | depth++; |
8120c9d5 | 1141 | else if ((*scan == ')' || *scan == '>') && depth > 0) |
50641945 FN |
1142 | depth--; |
1143 | } | |
1144 | ||
1145 | return 0; | |
1146 | } | |
1147 | ||
40e084e1 KS |
1148 | /* The string equivalent of find_toplevel_char. Returns a pointer |
1149 | to the location of NEEDLE in HAYSTACK, ignoring any occurrences | |
1150 | inside "()" and "<>". Returns NULL if NEEDLE was not found. */ | |
889f28e2 | 1151 | |
40e084e1 KS |
1152 | static const char * |
1153 | find_toplevel_string (const char *haystack, const char *needle) | |
889f28e2 | 1154 | { |
40e084e1 KS |
1155 | const char *s = haystack; |
1156 | ||
1157 | do | |
1158 | { | |
1159 | s = find_toplevel_char (s, *needle); | |
1160 | ||
1161 | if (s != NULL) | |
1162 | { | |
1163 | /* Found first char in HAYSTACK; check rest of string. */ | |
61012eef | 1164 | if (startswith (s, needle)) |
40e084e1 KS |
1165 | return s; |
1166 | ||
1167 | /* Didn't find it; loop over HAYSTACK, looking for the next | |
1168 | instance of the first character of NEEDLE. */ | |
1169 | ++s; | |
1170 | } | |
1171 | } | |
1172 | while (s != NULL && *s != '\0'); | |
1173 | ||
1174 | /* NEEDLE was not found in HAYSTACK. */ | |
1175 | return NULL; | |
889f28e2 AF |
1176 | } |
1177 | ||
33f448b1 JK |
1178 | /* Convert CANONICAL to its string representation using |
1179 | symtab_to_fullname for SYMTAB. The caller must xfree the result. */ | |
1180 | ||
1181 | static char * | |
1182 | canonical_to_fullform (const struct linespec_canonical_name *canonical) | |
1183 | { | |
1184 | if (canonical->symtab == NULL) | |
1185 | return xstrdup (canonical->suffix); | |
1186 | else | |
1187 | return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab), | |
1188 | canonical->suffix); | |
1189 | } | |
1190 | ||
f8eba3c6 TT |
1191 | /* Given FILTERS, a list of canonical names, filter the sals in RESULT |
1192 | and store the result in SELF->CANONICAL. */ | |
50641945 | 1193 | |
f8eba3c6 TT |
1194 | static void |
1195 | filter_results (struct linespec_state *self, | |
1196 | struct symtabs_and_lines *result, | |
1197 | VEC (const_char_ptr) *filters) | |
1198 | { | |
1199 | int i; | |
1200 | const char *name; | |
1201 | ||
1202 | for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i) | |
1203 | { | |
1204 | struct linespec_sals lsal; | |
1205 | int j; | |
1206 | ||
1207 | memset (&lsal, 0, sizeof (lsal)); | |
1208 | ||
1209 | for (j = 0; j < result->nelts; ++j) | |
1210 | { | |
33f448b1 JK |
1211 | const struct linespec_canonical_name *canonical; |
1212 | char *fullform; | |
1213 | struct cleanup *cleanup; | |
1214 | ||
1215 | canonical = &self->canonical_names[j]; | |
1216 | fullform = canonical_to_fullform (canonical); | |
1217 | cleanup = make_cleanup (xfree, fullform); | |
1218 | ||
1219 | if (strcmp (name, fullform) == 0) | |
f8eba3c6 | 1220 | add_sal_to_sals_basic (&lsal.sals, &result->sals[j]); |
33f448b1 JK |
1221 | |
1222 | do_cleanups (cleanup); | |
f8eba3c6 TT |
1223 | } |
1224 | ||
1225 | if (lsal.sals.nelts > 0) | |
1226 | { | |
1227 | lsal.canonical = xstrdup (name); | |
1228 | VEC_safe_push (linespec_sals, self->canonical->sals, &lsal); | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | self->canonical->pre_expanded = 0; | |
1233 | } | |
1234 | ||
1235 | /* Store RESULT into SELF->CANONICAL. */ | |
1236 | ||
1237 | static void | |
1238 | convert_results_to_lsals (struct linespec_state *self, | |
1239 | struct symtabs_and_lines *result) | |
50641945 | 1240 | { |
f8eba3c6 TT |
1241 | struct linespec_sals lsal; |
1242 | ||
1243 | lsal.canonical = NULL; | |
1244 | lsal.sals = *result; | |
1245 | VEC_safe_push (linespec_sals, self->canonical->sals, &lsal); | |
1246 | } | |
1247 | ||
33f448b1 JK |
1248 | /* A structure that contains two string representations of a struct |
1249 | linespec_canonical_name: | |
1250 | - one where the the symtab's fullname is used; | |
1251 | - one where the filename followed the "set filename-display" | |
1252 | setting. */ | |
1253 | ||
1254 | struct decode_line_2_item | |
1255 | { | |
1256 | /* The form using symtab_to_fullname. | |
1257 | It must be xfree'ed after use. */ | |
1258 | char *fullform; | |
1259 | ||
1260 | /* The form using symtab_to_filename_for_display. | |
1261 | It must be xfree'ed after use. */ | |
1262 | char *displayform; | |
1263 | ||
1264 | /* Field is initialized to zero and it is set to one if the user | |
1265 | requested breakpoint for this entry. */ | |
1266 | unsigned int selected : 1; | |
1267 | }; | |
1268 | ||
1269 | /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and | |
1270 | secondarily by FULLFORM. */ | |
1271 | ||
1272 | static int | |
1273 | decode_line_2_compare_items (const void *ap, const void *bp) | |
1274 | { | |
9a3c8263 SM |
1275 | const struct decode_line_2_item *a = (const struct decode_line_2_item *) ap; |
1276 | const struct decode_line_2_item *b = (const struct decode_line_2_item *) bp; | |
33f448b1 JK |
1277 | int retval; |
1278 | ||
1279 | retval = strcmp (a->displayform, b->displayform); | |
1280 | if (retval != 0) | |
1281 | return retval; | |
1282 | ||
1283 | return strcmp (a->fullform, b->fullform); | |
1284 | } | |
1285 | ||
f8eba3c6 TT |
1286 | /* Handle multiple results in RESULT depending on SELECT_MODE. This |
1287 | will either return normally, throw an exception on multiple | |
1288 | results, or present a menu to the user. On return, the SALS vector | |
1289 | in SELF->CANONICAL is set up properly. */ | |
1290 | ||
1291 | static void | |
1292 | decode_line_2 (struct linespec_state *self, | |
1293 | struct symtabs_and_lines *result, | |
1294 | const char *select_mode) | |
1295 | { | |
f8eba3c6 | 1296 | char *args, *prompt; |
50641945 | 1297 | int i; |
50641945 | 1298 | struct cleanup *old_chain; |
33f448b1 | 1299 | VEC (const_char_ptr) *filters = NULL; |
33f448b1 JK |
1300 | struct decode_line_2_item *items; |
1301 | int items_count; | |
50641945 | 1302 | |
f8eba3c6 TT |
1303 | gdb_assert (select_mode != multiple_symbols_all); |
1304 | gdb_assert (self->canonical != NULL); | |
33f448b1 JK |
1305 | gdb_assert (result->nelts >= 1); |
1306 | ||
1307 | old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters); | |
50641945 | 1308 | |
33f448b1 JK |
1309 | /* Prepare ITEMS array. */ |
1310 | items_count = result->nelts; | |
8d749320 | 1311 | items = XNEWVEC (struct decode_line_2_item, items_count); |
33f448b1 JK |
1312 | make_cleanup (xfree, items); |
1313 | for (i = 0; i < items_count; ++i) | |
50641945 | 1314 | { |
33f448b1 JK |
1315 | const struct linespec_canonical_name *canonical; |
1316 | struct decode_line_2_item *item; | |
1317 | ||
1318 | canonical = &self->canonical_names[i]; | |
1319 | gdb_assert (canonical->suffix != NULL); | |
1320 | item = &items[i]; | |
f8eba3c6 | 1321 | |
33f448b1 JK |
1322 | item->fullform = canonical_to_fullform (canonical); |
1323 | make_cleanup (xfree, item->fullform); | |
1324 | ||
1325 | if (canonical->symtab == NULL) | |
1326 | item->displayform = canonical->suffix; | |
1327 | else | |
f8eba3c6 | 1328 | { |
33f448b1 JK |
1329 | const char *fn_for_display; |
1330 | ||
1331 | fn_for_display = symtab_to_filename_for_display (canonical->symtab); | |
1332 | item->displayform = xstrprintf ("%s:%s", fn_for_display, | |
1333 | canonical->suffix); | |
1334 | make_cleanup (xfree, item->displayform); | |
f8eba3c6 TT |
1335 | } |
1336 | ||
33f448b1 | 1337 | item->selected = 0; |
50641945 FN |
1338 | } |
1339 | ||
33f448b1 JK |
1340 | /* Sort the list of method names. */ |
1341 | qsort (items, items_count, sizeof (*items), decode_line_2_compare_items); | |
1342 | ||
1343 | /* Remove entries with the same FULLFORM. */ | |
1344 | if (items_count >= 2) | |
1345 | { | |
1346 | struct decode_line_2_item *dst, *src; | |
1347 | ||
1348 | dst = items; | |
1349 | for (src = &items[1]; src < &items[items_count]; src++) | |
1350 | if (strcmp (src->fullform, dst->fullform) != 0) | |
1351 | *++dst = *src; | |
1352 | items_count = dst + 1 - items; | |
1353 | } | |
1354 | ||
1355 | if (select_mode == multiple_symbols_cancel && items_count > 1) | |
f8eba3c6 TT |
1356 | error (_("canceled because the command is ambiguous\n" |
1357 | "See set/show multiple-symbol.")); | |
1358 | ||
33f448b1 | 1359 | if (select_mode == multiple_symbols_all || items_count == 1) |
50641945 | 1360 | { |
f8eba3c6 TT |
1361 | do_cleanups (old_chain); |
1362 | convert_results_to_lsals (self, result); | |
1363 | return; | |
50641945 FN |
1364 | } |
1365 | ||
f8eba3c6 | 1366 | printf_unfiltered (_("[0] cancel\n[1] all\n")); |
33f448b1 JK |
1367 | for (i = 0; i < items_count; i++) |
1368 | printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform); | |
f8eba3c6 TT |
1369 | |
1370 | prompt = getenv ("PS2"); | |
1371 | if (prompt == NULL) | |
50641945 | 1372 | { |
f8eba3c6 | 1373 | prompt = "> "; |
50641945 | 1374 | } |
f8eba3c6 | 1375 | args = command_line_input (prompt, 0, "overload-choice"); |
50641945 FN |
1376 | |
1377 | if (args == 0 || *args == 0) | |
e2e0b3e5 | 1378 | error_no_arg (_("one or more choice numbers")); |
50641945 | 1379 | |
bfd28288 PA |
1380 | number_or_range_parser parser (args); |
1381 | while (!parser.finished ()) | |
50641945 | 1382 | { |
bfd28288 | 1383 | int num = parser.get_number (); |
50641945 FN |
1384 | |
1385 | if (num == 0) | |
8a3fe4f8 | 1386 | error (_("canceled")); |
50641945 FN |
1387 | else if (num == 1) |
1388 | { | |
f8eba3c6 TT |
1389 | /* We intentionally make this result in a single breakpoint, |
1390 | contrary to what older versions of gdb did. The | |
1391 | rationale is that this lets a user get the | |
1392 | multiple_symbols_all behavior even with the 'ask' | |
1393 | setting; and he can get separate breakpoints by entering | |
1394 | "2-57" at the query. */ | |
1395 | do_cleanups (old_chain); | |
1396 | convert_results_to_lsals (self, result); | |
1397 | return; | |
50641945 FN |
1398 | } |
1399 | ||
f8eba3c6 | 1400 | num -= 2; |
33f448b1 | 1401 | if (num >= items_count) |
f8eba3c6 | 1402 | printf_unfiltered (_("No choice number %d.\n"), num); |
50641945 FN |
1403 | else |
1404 | { | |
33f448b1 | 1405 | struct decode_line_2_item *item = &items[num]; |
f8eba3c6 | 1406 | |
33f448b1 | 1407 | if (!item->selected) |
50641945 | 1408 | { |
33f448b1 JK |
1409 | VEC_safe_push (const_char_ptr, filters, item->fullform); |
1410 | item->selected = 1; | |
50641945 FN |
1411 | } |
1412 | else | |
1413 | { | |
3e43a32a | 1414 | printf_unfiltered (_("duplicate request for %d ignored.\n"), |
f6f99966 | 1415 | num + 2); |
50641945 FN |
1416 | } |
1417 | } | |
50641945 | 1418 | } |
f8eba3c6 TT |
1419 | |
1420 | filter_results (self, result, filters); | |
1421 | do_cleanups (old_chain); | |
50641945 | 1422 | } |
94af9270 | 1423 | |
40e084e1 | 1424 | \f |
3d50dd94 | 1425 | |
40e084e1 KS |
1426 | /* The parser of linespec itself. */ |
1427 | ||
1428 | /* Throw an appropriate error when SYMBOL is not found (optionally in | |
1429 | FILENAME). */ | |
1430 | ||
1431 | static void ATTRIBUTE_NORETURN | |
5d94e27b | 1432 | symbol_not_found_error (const char *symbol, const char *filename) |
3d50dd94 | 1433 | { |
40e084e1 KS |
1434 | if (symbol == NULL) |
1435 | symbol = ""; | |
1436 | ||
1437 | if (!have_full_symbols () | |
1438 | && !have_partial_symbols () | |
1439 | && !have_minimal_symbols ()) | |
1440 | throw_error (NOT_FOUND_ERROR, | |
1441 | _("No symbol table is loaded. Use the \"file\" command.")); | |
1442 | ||
1443 | /* If SYMBOL starts with '$', the user attempted to either lookup | |
1444 | a function/variable in his code starting with '$' or an internal | |
1445 | variable of that name. Since we do not know which, be concise and | |
1446 | explain both possibilities. */ | |
1447 | if (*symbol == '$') | |
1448 | { | |
1449 | if (filename) | |
1450 | throw_error (NOT_FOUND_ERROR, | |
1451 | _("Undefined convenience variable or function \"%s\" " | |
1452 | "not defined in \"%s\"."), symbol, filename); | |
1453 | else | |
1454 | throw_error (NOT_FOUND_ERROR, | |
1455 | _("Undefined convenience variable or function \"%s\" " | |
1456 | "not defined."), symbol); | |
1457 | } | |
1458 | else | |
1459 | { | |
1460 | if (filename) | |
1461 | throw_error (NOT_FOUND_ERROR, | |
1462 | _("Function \"%s\" not defined in \"%s\"."), | |
1463 | symbol, filename); | |
1464 | else | |
1465 | throw_error (NOT_FOUND_ERROR, | |
1466 | _("Function \"%s\" not defined."), symbol); | |
1467 | } | |
3d50dd94 JK |
1468 | } |
1469 | ||
40e084e1 KS |
1470 | /* Throw an appropriate error when an unexpected token is encountered |
1471 | in the input. */ | |
94af9270 | 1472 | |
40e084e1 KS |
1473 | static void ATTRIBUTE_NORETURN |
1474 | unexpected_linespec_error (linespec_parser *parser) | |
94af9270 | 1475 | { |
40e084e1 KS |
1476 | linespec_token token; |
1477 | static const char * token_type_strings[] | |
1478 | = {"keyword", "colon", "string", "number", "comma", "end of input"}; | |
94af9270 | 1479 | |
40e084e1 KS |
1480 | /* Get the token that generated the error. */ |
1481 | token = linespec_lexer_lex_one (parser); | |
94af9270 | 1482 | |
40e084e1 KS |
1483 | /* Finally, throw the error. */ |
1484 | if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER | |
1485 | || token.type == LSTOKEN_KEYWORD) | |
94af9270 | 1486 | { |
40e084e1 | 1487 | char *string; |
40e084e1 KS |
1488 | |
1489 | string = copy_token_string (token); | |
78cc6c2d | 1490 | make_cleanup (xfree, string); |
40e084e1 KS |
1491 | throw_error (GENERIC_ERROR, |
1492 | _("malformed linespec error: unexpected %s, \"%s\""), | |
1493 | token_type_strings[token.type], string); | |
1494 | } | |
1495 | else | |
1496 | throw_error (GENERIC_ERROR, | |
1497 | _("malformed linespec error: unexpected %s"), | |
1498 | token_type_strings[token.type]); | |
1499 | } | |
1500 | ||
00e52e53 KS |
1501 | /* Throw an undefined label error. */ |
1502 | ||
1503 | static void ATTRIBUTE_NORETURN | |
1504 | undefined_label_error (const char *function, const char *label) | |
1505 | { | |
1506 | if (function != NULL) | |
1507 | throw_error (NOT_FOUND_ERROR, | |
1508 | _("No label \"%s\" defined in function \"%s\"."), | |
1509 | label, function); | |
1510 | else | |
1511 | throw_error (NOT_FOUND_ERROR, | |
1512 | _("No label \"%s\" defined in current function."), | |
1513 | label); | |
1514 | } | |
1515 | ||
1516 | /* Throw a source file not found error. */ | |
1517 | ||
1518 | static void ATTRIBUTE_NORETURN | |
1519 | source_file_not_found_error (const char *name) | |
1520 | { | |
1521 | throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name); | |
1522 | } | |
1523 | ||
87f0e720 | 1524 | /* See description in linespec.h. */ |
40e084e1 | 1525 | |
87f0e720 | 1526 | struct line_offset |
09cf2b22 | 1527 | linespec_parse_line_offset (const char *string) |
40e084e1 | 1528 | { |
87f0e720 | 1529 | const char *start = string; |
40e084e1 KS |
1530 | struct line_offset line_offset = {0, LINE_OFFSET_NONE}; |
1531 | ||
1532 | if (*string == '+') | |
1533 | { | |
1534 | line_offset.sign = LINE_OFFSET_PLUS; | |
1535 | ++string; | |
1536 | } | |
1537 | else if (*string == '-') | |
1538 | { | |
1539 | line_offset.sign = LINE_OFFSET_MINUS; | |
1540 | ++string; | |
1541 | } | |
1542 | ||
87f0e720 KS |
1543 | if (*string != '\0' && !isdigit (*string)) |
1544 | error (_("malformed line offset: \"%s\""), start); | |
1545 | ||
40e084e1 KS |
1546 | /* Right now, we only allow base 10 for offsets. */ |
1547 | line_offset.offset = atoi (string); | |
1548 | return line_offset; | |
1549 | } | |
1550 | ||
1551 | /* Parse the basic_spec in PARSER's input. */ | |
1552 | ||
1553 | static void | |
1554 | linespec_parse_basic (linespec_parser *parser) | |
1555 | { | |
1556 | char *name; | |
1557 | linespec_token token; | |
1558 | VEC (symbolp) *symbols, *labels; | |
f60e2d5c | 1559 | VEC (bound_minimal_symbol_d) *minimal_symbols; |
40e084e1 KS |
1560 | struct cleanup *cleanup; |
1561 | ||
1562 | /* Get the next token. */ | |
1563 | token = linespec_lexer_lex_one (parser); | |
1564 | ||
1565 | /* If it is EOI or KEYWORD, issue an error. */ | |
1566 | if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI) | |
1567 | unexpected_linespec_error (parser); | |
1568 | /* If it is a LSTOKEN_NUMBER, we have an offset. */ | |
1569 | else if (token.type == LSTOKEN_NUMBER) | |
1570 | { | |
1571 | /* Record the line offset and get the next token. */ | |
1572 | name = copy_token_string (token); | |
1573 | cleanup = make_cleanup (xfree, name); | |
00e52e53 | 1574 | PARSER_EXPLICIT (parser)->line_offset = linespec_parse_line_offset (name); |
40e084e1 KS |
1575 | do_cleanups (cleanup); |
1576 | ||
1577 | /* Get the next token. */ | |
1578 | token = linespec_lexer_consume_token (parser); | |
1579 | ||
1580 | /* If the next token is a comma, stop parsing and return. */ | |
1581 | if (token.type == LSTOKEN_COMMA) | |
1582 | return; | |
1583 | ||
1584 | /* If the next token is anything but EOI or KEYWORD, issue | |
1585 | an error. */ | |
1586 | if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI) | |
1587 | unexpected_linespec_error (parser); | |
1588 | } | |
1589 | ||
1590 | if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI) | |
1591 | return; | |
1592 | ||
1593 | /* Next token must be LSTOKEN_STRING. */ | |
1594 | if (token.type != LSTOKEN_STRING) | |
1595 | unexpected_linespec_error (parser); | |
1596 | ||
1597 | /* The current token will contain the name of a function, method, | |
1598 | or label. */ | |
1599 | name = copy_token_string (token); | |
1600 | cleanup = make_cleanup (xfree, name); | |
1601 | ||
1602 | /* Try looking it up as a function/method. */ | |
1603 | find_linespec_symbols (PARSER_STATE (parser), | |
1604 | PARSER_RESULT (parser)->file_symtabs, name, | |
1605 | &symbols, &minimal_symbols); | |
1606 | ||
1607 | if (symbols != NULL || minimal_symbols != NULL) | |
1608 | { | |
1609 | PARSER_RESULT (parser)->function_symbols = symbols; | |
1610 | PARSER_RESULT (parser)->minimal_symbols = minimal_symbols; | |
00e52e53 | 1611 | PARSER_EXPLICIT (parser)->function_name = name; |
40e084e1 KS |
1612 | symbols = NULL; |
1613 | discard_cleanups (cleanup); | |
1614 | } | |
1615 | else | |
1616 | { | |
1617 | /* NAME was not a function or a method. So it must be a label | |
b4013987 | 1618 | name or user specified variable like "break foo.c:$zippo". */ |
40e084e1 KS |
1619 | labels = find_label_symbols (PARSER_STATE (parser), NULL, |
1620 | &symbols, name); | |
1621 | if (labels != NULL) | |
94af9270 | 1622 | { |
40e084e1 KS |
1623 | PARSER_RESULT (parser)->labels.label_symbols = labels; |
1624 | PARSER_RESULT (parser)->labels.function_symbols = symbols; | |
00e52e53 | 1625 | PARSER_EXPLICIT (parser)->label_name = name; |
40e084e1 KS |
1626 | symbols = NULL; |
1627 | discard_cleanups (cleanup); | |
1628 | } | |
b4013987 AA |
1629 | else if (token.type == LSTOKEN_STRING |
1630 | && *LS_TOKEN_STOKEN (token).ptr == '$') | |
1631 | { | |
1632 | /* User specified a convenience variable or history value. */ | |
00e52e53 | 1633 | PARSER_EXPLICIT (parser)->line_offset |
b4013987 AA |
1634 | = linespec_parse_variable (PARSER_STATE (parser), name); |
1635 | ||
00e52e53 | 1636 | if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN) |
b4013987 AA |
1637 | { |
1638 | /* The user-specified variable was not valid. Do not | |
1639 | throw an error here. parse_linespec will do it for us. */ | |
00e52e53 | 1640 | PARSER_EXPLICIT (parser)->function_name = name; |
b4013987 AA |
1641 | discard_cleanups (cleanup); |
1642 | return; | |
1643 | } | |
c888a17d KS |
1644 | |
1645 | /* The convenience variable/history value parsed correctly. | |
1646 | NAME is no longer needed. */ | |
1647 | do_cleanups (cleanup); | |
b4013987 | 1648 | } |
40e084e1 KS |
1649 | else |
1650 | { | |
1651 | /* The name is also not a label. Abort parsing. Do not throw | |
1652 | an error here. parse_linespec will do it for us. */ | |
1653 | ||
1654 | /* Save a copy of the name we were trying to lookup. */ | |
00e52e53 | 1655 | PARSER_EXPLICIT (parser)->function_name = name; |
40e084e1 KS |
1656 | discard_cleanups (cleanup); |
1657 | return; | |
1658 | } | |
1659 | } | |
1660 | ||
1661 | /* Get the next token. */ | |
1662 | token = linespec_lexer_consume_token (parser); | |
1663 | ||
1664 | if (token.type == LSTOKEN_COLON) | |
1665 | { | |
1666 | /* User specified a label or a lineno. */ | |
1667 | token = linespec_lexer_consume_token (parser); | |
1668 | ||
1669 | if (token.type == LSTOKEN_NUMBER) | |
1670 | { | |
1671 | /* User specified an offset. Record the line offset and | |
1672 | get the next token. */ | |
1673 | name = copy_token_string (token); | |
1674 | cleanup = make_cleanup (xfree, name); | |
00e52e53 | 1675 | PARSER_EXPLICIT (parser)->line_offset |
40e084e1 KS |
1676 | = linespec_parse_line_offset (name); |
1677 | do_cleanups (cleanup); | |
1678 | ||
1679 | /* Ge the next token. */ | |
1680 | token = linespec_lexer_consume_token (parser); | |
1681 | } | |
1682 | else if (token.type == LSTOKEN_STRING) | |
1683 | { | |
1684 | /* Grab a copy of the label's name and look it up. */ | |
1685 | name = copy_token_string (token); | |
1686 | cleanup = make_cleanup (xfree, name); | |
1687 | labels = find_label_symbols (PARSER_STATE (parser), | |
1688 | PARSER_RESULT (parser)->function_symbols, | |
1689 | &symbols, name); | |
1690 | ||
1691 | if (labels != NULL) | |
94af9270 | 1692 | { |
40e084e1 KS |
1693 | PARSER_RESULT (parser)->labels.label_symbols = labels; |
1694 | PARSER_RESULT (parser)->labels.function_symbols = symbols; | |
00e52e53 | 1695 | PARSER_EXPLICIT (parser)->label_name = name; |
40e084e1 KS |
1696 | symbols = NULL; |
1697 | discard_cleanups (cleanup); | |
1698 | } | |
1699 | else | |
1700 | { | |
1701 | /* We don't know what it was, but it isn't a label. */ | |
00e52e53 KS |
1702 | undefined_label_error (PARSER_EXPLICIT (parser)->function_name, |
1703 | name); | |
40e084e1 KS |
1704 | } |
1705 | ||
1706 | /* Check for a line offset. */ | |
1707 | token = linespec_lexer_consume_token (parser); | |
1708 | if (token.type == LSTOKEN_COLON) | |
1709 | { | |
1710 | /* Get the next token. */ | |
1711 | token = linespec_lexer_consume_token (parser); | |
1712 | ||
1713 | /* It must be a line offset. */ | |
1714 | if (token.type != LSTOKEN_NUMBER) | |
1715 | unexpected_linespec_error (parser); | |
1716 | ||
1717 | /* Record the lione offset and get the next token. */ | |
1718 | name = copy_token_string (token); | |
1719 | cleanup = make_cleanup (xfree, name); | |
1720 | ||
00e52e53 | 1721 | PARSER_EXPLICIT (parser)->line_offset |
40e084e1 KS |
1722 | = linespec_parse_line_offset (name); |
1723 | do_cleanups (cleanup); | |
1724 | ||
1725 | /* Get the next token. */ | |
1726 | token = linespec_lexer_consume_token (parser); | |
94af9270 KS |
1727 | } |
1728 | } | |
40e084e1 KS |
1729 | else |
1730 | { | |
1731 | /* Trailing ':' in the input. Issue an error. */ | |
1732 | unexpected_linespec_error (parser); | |
1733 | } | |
94af9270 | 1734 | } |
40e084e1 | 1735 | } |
94af9270 | 1736 | |
40e084e1 | 1737 | /* Canonicalize the linespec contained in LS. The result is saved into |
00e52e53 KS |
1738 | STATE->canonical. This function handles both linespec and explicit |
1739 | locations. */ | |
40e084e1 KS |
1740 | |
1741 | static void | |
f00aae0f | 1742 | canonicalize_linespec (struct linespec_state *state, const linespec_p ls) |
40e084e1 | 1743 | { |
00e52e53 | 1744 | struct event_location *canon; |
67994074 | 1745 | struct explicit_location *explicit_loc; |
f00aae0f | 1746 | |
40e084e1 KS |
1747 | /* If canonicalization was not requested, no need to do anything. */ |
1748 | if (!state->canonical) | |
1749 | return; | |
1750 | ||
00e52e53 | 1751 | /* Save everything as an explicit location. */ |
67994074 KS |
1752 | canon = state->canonical->location |
1753 | = new_explicit_location (&ls->explicit_loc); | |
1754 | explicit_loc = get_explicit_location (canon); | |
40e084e1 | 1755 | |
67994074 | 1756 | if (explicit_loc->label_name != NULL) |
a06efdd6 | 1757 | { |
00e52e53 | 1758 | state->canonical->special_display = 1; |
40e084e1 | 1759 | |
67994074 | 1760 | if (explicit_loc->function_name == NULL) |
40e084e1 | 1761 | { |
a06efdd6 KS |
1762 | struct symbol *s; |
1763 | ||
1764 | /* No function was specified, so add the symbol name. */ | |
1765 | gdb_assert (ls->labels.function_symbols != NULL | |
1766 | && (VEC_length (symbolp, ls->labels.function_symbols) | |
1767 | == 1)); | |
1768 | s = VEC_index (symbolp, ls->labels.function_symbols, 0); | |
67994074 | 1769 | explicit_loc->function_name = xstrdup (SYMBOL_NATURAL_NAME (s)); |
40e084e1 | 1770 | } |
a06efdd6 | 1771 | } |
40e084e1 | 1772 | |
00e52e53 KS |
1773 | /* If this location originally came from a linespec, save a string |
1774 | representation of it for display and saving to file. */ | |
1775 | if (state->is_linespec) | |
a06efdd6 | 1776 | { |
67994074 | 1777 | char *linespec = explicit_location_to_linespec (explicit_loc); |
a06efdd6 | 1778 | |
00e52e53 KS |
1779 | set_event_location_string (canon, linespec); |
1780 | xfree (linespec); | |
1781 | } | |
94af9270 | 1782 | } |
c00f8484 | 1783 | |
40e084e1 | 1784 | /* Given a line offset in LS, construct the relevant SALs. */ |
c00f8484 | 1785 | |
40e084e1 KS |
1786 | static struct symtabs_and_lines |
1787 | create_sals_line_offset (struct linespec_state *self, | |
1788 | linespec_p ls) | |
c00f8484 | 1789 | { |
40e084e1 KS |
1790 | struct symtabs_and_lines values; |
1791 | struct symtab_and_line val; | |
1792 | int use_default = 0; | |
c00f8484 | 1793 | |
40e084e1 KS |
1794 | init_sal (&val); |
1795 | values.sals = NULL; | |
1796 | values.nelts = 0; | |
1797 | ||
1798 | /* This is where we need to make sure we have good defaults. | |
1799 | We must guarantee that this section of code is never executed | |
2e47c6ca | 1800 | when we are called with just a function name, since |
40e084e1 KS |
1801 | set_default_source_symtab_and_line uses |
1802 | select_source_symtab that calls us with such an argument. */ | |
1803 | ||
ec94af83 DE |
1804 | if (VEC_length (symtab_ptr, ls->file_symtabs) == 1 |
1805 | && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL) | |
3d50dd94 | 1806 | { |
05cba821 JK |
1807 | const char *fullname; |
1808 | ||
40e084e1 | 1809 | set_current_program_space (self->program_space); |
c00f8484 | 1810 | |
40e084e1 KS |
1811 | /* Make sure we have at least a default source line. */ |
1812 | set_default_source_symtab_and_line (); | |
1813 | initialize_defaults (&self->default_symtab, &self->default_line); | |
05cba821 | 1814 | fullname = symtab_to_fullname (self->default_symtab); |
ec94af83 DE |
1815 | VEC_pop (symtab_ptr, ls->file_symtabs); |
1816 | VEC_free (symtab_ptr, ls->file_symtabs); | |
c2f4122d PA |
1817 | ls->file_symtabs = collect_symtabs_from_filename (fullname, |
1818 | self->search_pspace); | |
40e084e1 KS |
1819 | use_default = 1; |
1820 | } | |
c00f8484 | 1821 | |
67994074 KS |
1822 | val.line = ls->explicit_loc.line_offset.offset; |
1823 | switch (ls->explicit_loc.line_offset.sign) | |
40e084e1 KS |
1824 | { |
1825 | case LINE_OFFSET_PLUS: | |
67994074 | 1826 | if (ls->explicit_loc.line_offset.offset == 0) |
40e084e1 KS |
1827 | val.line = 5; |
1828 | if (use_default) | |
1829 | val.line = self->default_line + val.line; | |
1830 | break; | |
1831 | ||
1832 | case LINE_OFFSET_MINUS: | |
67994074 | 1833 | if (ls->explicit_loc.line_offset.offset == 0) |
40e084e1 KS |
1834 | val.line = 15; |
1835 | if (use_default) | |
1836 | val.line = self->default_line - val.line; | |
1837 | else | |
1838 | val.line = -val.line; | |
1839 | break; | |
1840 | ||
1841 | case LINE_OFFSET_NONE: | |
1842 | break; /* No need to adjust val.line. */ | |
1843 | } | |
1844 | ||
1845 | if (self->list_mode) | |
1846 | decode_digits_list_mode (self, ls, &values, val); | |
1847 | else | |
1848 | { | |
1849 | struct linetable_entry *best_entry = NULL; | |
1850 | int *filter; | |
3977b71f | 1851 | const struct block **blocks; |
40e084e1 KS |
1852 | struct cleanup *cleanup; |
1853 | struct symtabs_and_lines intermediate_results; | |
1854 | int i, j; | |
1855 | ||
1856 | intermediate_results.sals = NULL; | |
1857 | intermediate_results.nelts = 0; | |
1858 | ||
1859 | decode_digits_ordinary (self, ls, val.line, &intermediate_results, | |
1860 | &best_entry); | |
1861 | if (intermediate_results.nelts == 0 && best_entry != NULL) | |
1862 | decode_digits_ordinary (self, ls, best_entry->line, | |
1863 | &intermediate_results, &best_entry); | |
1864 | ||
1865 | cleanup = make_cleanup (xfree, intermediate_results.sals); | |
1866 | ||
1867 | /* For optimized code, the compiler can scatter one source line | |
1868 | across disjoint ranges of PC values, even when no duplicate | |
1869 | functions or inline functions are involved. For example, | |
1870 | 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor | |
1871 | function can result in two PC ranges. In this case, we don't | |
1872 | want to set a breakpoint on the first PC of each range. To filter | |
1873 | such cases, we use containing blocks -- for each PC found | |
1874 | above, we see if there are other PCs that are in the same | |
1875 | block. If yes, the other PCs are filtered out. */ | |
1876 | ||
1877 | filter = XNEWVEC (int, intermediate_results.nelts); | |
1878 | make_cleanup (xfree, filter); | |
3977b71f | 1879 | blocks = XNEWVEC (const struct block *, intermediate_results.nelts); |
40e084e1 KS |
1880 | make_cleanup (xfree, blocks); |
1881 | ||
1882 | for (i = 0; i < intermediate_results.nelts; ++i) | |
3d50dd94 | 1883 | { |
40e084e1 | 1884 | set_current_program_space (intermediate_results.sals[i].pspace); |
c00f8484 | 1885 | |
40e084e1 KS |
1886 | filter[i] = 1; |
1887 | blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc, | |
1888 | intermediate_results.sals[i].section); | |
3d50dd94 | 1889 | } |
c00f8484 | 1890 | |
40e084e1 KS |
1891 | for (i = 0; i < intermediate_results.nelts; ++i) |
1892 | { | |
1893 | if (blocks[i] != NULL) | |
1894 | for (j = i + 1; j < intermediate_results.nelts; ++j) | |
1895 | { | |
1896 | if (blocks[j] == blocks[i]) | |
1897 | { | |
1898 | filter[j] = 0; | |
1899 | break; | |
1900 | } | |
1901 | } | |
1902 | } | |
c00f8484 | 1903 | |
40e084e1 KS |
1904 | for (i = 0; i < intermediate_results.nelts; ++i) |
1905 | if (filter[i]) | |
1906 | { | |
1907 | struct symbol *sym = (blocks[i] | |
1908 | ? block_containing_function (blocks[i]) | |
1909 | : NULL); | |
3d50dd94 | 1910 | |
40e084e1 KS |
1911 | if (self->funfirstline) |
1912 | skip_prologue_sal (&intermediate_results.sals[i]); | |
1913 | /* Make sure the line matches the request, not what was | |
1914 | found. */ | |
1915 | intermediate_results.sals[i].line = val.line; | |
1916 | add_sal_to_sals (self, &values, &intermediate_results.sals[i], | |
66f1999b | 1917 | sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0); |
40e084e1 | 1918 | } |
3d50dd94 | 1919 | |
40e084e1 | 1920 | do_cleanups (cleanup); |
f17170e5 | 1921 | } |
c00f8484 | 1922 | |
40e084e1 KS |
1923 | if (values.nelts == 0) |
1924 | { | |
67994074 | 1925 | if (ls->explicit_loc.source_filename) |
40e084e1 | 1926 | throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."), |
67994074 | 1927 | val.line, ls->explicit_loc.source_filename); |
40e084e1 KS |
1928 | else |
1929 | throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."), | |
1930 | val.line); | |
1931 | } | |
3d50dd94 | 1932 | |
40e084e1 | 1933 | return values; |
c00f8484 KS |
1934 | } |
1935 | ||
a06efdd6 KS |
1936 | /* Convert the given ADDRESS into SaLs. */ |
1937 | ||
1938 | static struct symtabs_and_lines | |
1939 | convert_address_location_to_sals (struct linespec_state *self, | |
1940 | CORE_ADDR address) | |
1941 | { | |
1942 | struct symtab_and_line sal; | |
1943 | struct symtabs_and_lines sals = {NULL, 0}; | |
1944 | ||
1945 | sal = find_pc_line (address, 0); | |
1946 | sal.pc = address; | |
1947 | sal.section = find_pc_overlay (address); | |
1948 | sal.explicit_pc = 1; | |
1949 | add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1); | |
1950 | ||
1951 | return sals; | |
1952 | } | |
1953 | ||
40e084e1 KS |
1954 | /* Create and return SALs from the linespec LS. */ |
1955 | ||
1956 | static struct symtabs_and_lines | |
1957 | convert_linespec_to_sals (struct linespec_state *state, linespec_p ls) | |
1958 | { | |
1959 | struct symtabs_and_lines sals = {NULL, 0}; | |
1960 | ||
a06efdd6 | 1961 | if (ls->labels.label_symbols != NULL) |
40e084e1 KS |
1962 | { |
1963 | /* We have just a bunch of functions/methods or labels. */ | |
1964 | int i; | |
1965 | struct symtab_and_line sal; | |
1966 | struct symbol *sym; | |
1967 | ||
1968 | for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i) | |
1969 | { | |
08be3fe3 | 1970 | struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym)); |
fdbb204b TT |
1971 | |
1972 | if (symbol_to_sal (&sal, state->funfirstline, sym) | |
1973 | && maybe_add_address (state->addr_set, pspace, sal.pc)) | |
64b92e45 KS |
1974 | add_sal_to_sals (state, &sals, &sal, |
1975 | SYMBOL_NATURAL_NAME (sym), 0); | |
40e084e1 KS |
1976 | } |
1977 | } | |
1978 | else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL) | |
1979 | { | |
1980 | /* We have just a bunch of functions and/or methods. */ | |
1981 | int i; | |
1982 | struct symtab_and_line sal; | |
1983 | struct symbol *sym; | |
f60e2d5c | 1984 | bound_minimal_symbol_d *elem; |
40e084e1 KS |
1985 | struct program_space *pspace; |
1986 | ||
1987 | if (ls->function_symbols != NULL) | |
1988 | { | |
1989 | /* Sort symbols so that symbols with the same program space are next | |
1990 | to each other. */ | |
1991 | qsort (VEC_address (symbolp, ls->function_symbols), | |
1992 | VEC_length (symbolp, ls->function_symbols), | |
1993 | sizeof (symbolp), compare_symbols); | |
1994 | ||
1995 | for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i) | |
1996 | { | |
08be3fe3 | 1997 | pspace = SYMTAB_PSPACE (symbol_symtab (sym)); |
40e084e1 | 1998 | set_current_program_space (pspace); |
64b92e45 KS |
1999 | if (symbol_to_sal (&sal, state->funfirstline, sym) |
2000 | && maybe_add_address (state->addr_set, pspace, sal.pc)) | |
66f1999b KS |
2001 | add_sal_to_sals (state, &sals, &sal, |
2002 | SYMBOL_NATURAL_NAME (sym), 0); | |
40e084e1 KS |
2003 | } |
2004 | } | |
2005 | ||
2006 | if (ls->minimal_symbols != NULL) | |
2007 | { | |
2008 | /* Sort minimal symbols by program space, too. */ | |
f60e2d5c TT |
2009 | qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols), |
2010 | VEC_length (bound_minimal_symbol_d, ls->minimal_symbols), | |
2011 | sizeof (bound_minimal_symbol_d), compare_msymbols); | |
40e084e1 KS |
2012 | |
2013 | for (i = 0; | |
f60e2d5c TT |
2014 | VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols, |
2015 | i, elem); | |
40e084e1 KS |
2016 | ++i) |
2017 | { | |
001822aa | 2018 | pspace = elem->objfile->pspace; |
40e084e1 KS |
2019 | set_current_program_space (pspace); |
2020 | minsym_found (state, elem->objfile, elem->minsym, &sals); | |
2021 | } | |
2022 | } | |
2023 | } | |
67994074 | 2024 | else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN) |
40e084e1 KS |
2025 | { |
2026 | /* Only an offset was specified. */ | |
2027 | sals = create_sals_line_offset (state, ls); | |
2028 | ||
2029 | /* Make sure we have a filename for canonicalization. */ | |
67994074 | 2030 | if (ls->explicit_loc.source_filename == NULL) |
05cba821 JK |
2031 | { |
2032 | const char *fullname = symtab_to_fullname (state->default_symtab); | |
2033 | ||
e93ba630 JK |
2034 | /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab |
2035 | form so that displaying SOURCE_FILENAME can follow the current | |
2036 | FILENAME_DISPLAY_STRING setting. But as it is used only rarely | |
2037 | it has been kept for code simplicity only in absolute form. */ | |
67994074 | 2038 | ls->explicit_loc.source_filename = xstrdup (fullname); |
05cba821 | 2039 | } |
40e084e1 KS |
2040 | } |
2041 | else | |
2042 | { | |
2043 | /* We haven't found any results... */ | |
2044 | return sals; | |
2045 | } | |
2046 | ||
2047 | canonicalize_linespec (state, ls); | |
2048 | ||
2049 | if (sals.nelts > 0 && state->canonical != NULL) | |
2050 | state->canonical->pre_expanded = 1; | |
2051 | ||
2052 | return sals; | |
2053 | } | |
50641945 | 2054 | |
67994074 | 2055 | /* Convert the explicit location EXPLICIT_LOC into SaLs. */ |
00e52e53 KS |
2056 | |
2057 | static struct symtabs_and_lines | |
2058 | convert_explicit_location_to_sals (struct linespec_state *self, | |
2059 | linespec_p result, | |
67994074 | 2060 | const struct explicit_location *explicit_loc) |
00e52e53 KS |
2061 | { |
2062 | VEC (symbolp) *symbols, *labels; | |
2063 | VEC (bound_minimal_symbol_d) *minimal_symbols; | |
2064 | ||
67994074 | 2065 | if (explicit_loc->source_filename != NULL) |
00e52e53 KS |
2066 | { |
2067 | TRY | |
2068 | { | |
2069 | result->file_symtabs | |
c2f4122d PA |
2070 | = symtabs_from_filename (explicit_loc->source_filename, |
2071 | self->search_pspace); | |
00e52e53 KS |
2072 | } |
2073 | CATCH (except, RETURN_MASK_ERROR) | |
2074 | { | |
67994074 | 2075 | source_file_not_found_error (explicit_loc->source_filename); |
00e52e53 KS |
2076 | } |
2077 | END_CATCH | |
67994074 KS |
2078 | result->explicit_loc.source_filename |
2079 | = xstrdup (explicit_loc->source_filename); | |
00e52e53 KS |
2080 | } |
2081 | else | |
2082 | { | |
2083 | /* A NULL entry means to use the default symtab. */ | |
2084 | VEC_safe_push (symtab_ptr, result->file_symtabs, NULL); | |
2085 | } | |
2086 | ||
67994074 | 2087 | if (explicit_loc->function_name != NULL) |
00e52e53 KS |
2088 | { |
2089 | find_linespec_symbols (self, result->file_symtabs, | |
67994074 | 2090 | explicit_loc->function_name, &symbols, |
00e52e53 KS |
2091 | &minimal_symbols); |
2092 | ||
2093 | if (symbols == NULL && minimal_symbols == NULL) | |
67994074 KS |
2094 | symbol_not_found_error (explicit_loc->function_name, |
2095 | result->explicit_loc.source_filename); | |
00e52e53 | 2096 | |
67994074 KS |
2097 | result->explicit_loc.function_name |
2098 | = xstrdup (explicit_loc->function_name); | |
00e52e53 KS |
2099 | result->function_symbols = symbols; |
2100 | result->minimal_symbols = minimal_symbols; | |
2101 | } | |
2102 | ||
67994074 | 2103 | if (explicit_loc->label_name != NULL) |
00e52e53 KS |
2104 | { |
2105 | symbols = NULL; | |
2106 | labels = find_label_symbols (self, result->function_symbols, | |
67994074 | 2107 | &symbols, explicit_loc->label_name); |
00e52e53 KS |
2108 | |
2109 | if (labels == NULL) | |
67994074 KS |
2110 | undefined_label_error (result->explicit_loc.function_name, |
2111 | explicit_loc->label_name); | |
00e52e53 | 2112 | |
67994074 | 2113 | result->explicit_loc.label_name = xstrdup (explicit_loc->label_name); |
00e52e53 KS |
2114 | result->labels.label_symbols = labels; |
2115 | result->labels.function_symbols = symbols; | |
2116 | } | |
2117 | ||
67994074 KS |
2118 | if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN) |
2119 | result->explicit_loc.line_offset = explicit_loc->line_offset; | |
00e52e53 KS |
2120 | |
2121 | return convert_linespec_to_sals (self, result); | |
2122 | } | |
2123 | ||
40e084e1 | 2124 | /* Parse a string that specifies a linespec. |
50641945 | 2125 | |
40e084e1 | 2126 | The basic grammar of linespecs: |
50641945 | 2127 | |
a06efdd6 | 2128 | linespec -> var_spec | basic_spec |
40e084e1 | 2129 | var_spec -> '$' (STRING | NUMBER) |
50641945 | 2130 | |
40e084e1 KS |
2131 | basic_spec -> file_offset_spec | function_spec | label_spec |
2132 | file_offset_spec -> opt_file_spec offset_spec | |
2133 | function_spec -> opt_file_spec function_name_spec opt_label_spec | |
2134 | label_spec -> label_name_spec | |
50641945 | 2135 | |
40e084e1 KS |
2136 | opt_file_spec -> "" | file_name_spec ':' |
2137 | opt_label_spec -> "" | ':' label_name_spec | |
2138 | ||
2139 | file_name_spec -> STRING | |
2140 | function_name_spec -> STRING | |
2141 | label_name_spec -> STRING | |
2142 | function_name_spec -> STRING | |
2143 | offset_spec -> NUMBER | |
2144 | -> '+' NUMBER | |
2145 | -> '-' NUMBER | |
2146 | ||
2147 | This may all be followed by several keywords such as "if EXPR", | |
2148 | which we ignore. | |
2149 | ||
2150 | A comma will terminate parsing. | |
2151 | ||
2152 | The function may be an undebuggable function found in minimal symbol table. | |
50641945 FN |
2153 | |
2154 | If the argument FUNFIRSTLINE is nonzero, we want the first line | |
2155 | of real code inside a function when a function is specified, and it is | |
2156 | not OK to specify a variable or type to get its line number. | |
2157 | ||
2158 | DEFAULT_SYMTAB specifies the file to use if none is specified. | |
2159 | It defaults to current_source_symtab. | |
2160 | DEFAULT_LINE specifies the line number to use for relative | |
2161 | line numbers (that start with signs). Defaults to current_source_line. | |
2162 | If CANONICAL is non-NULL, store an array of strings containing the canonical | |
1777feb0 | 2163 | line specs there if necessary. Currently overloaded member functions and |
50641945 | 2164 | line numbers or static functions without a filename yield a canonical |
1777feb0 | 2165 | line spec. The array and the line spec strings are allocated on the heap, |
50641945 FN |
2166 | it is the callers responsibility to free them. |
2167 | ||
2168 | Note that it is possible to return zero for the symtab | |
2169 | if no file is validly specified. Callers must check that. | |
58438ac1 | 2170 | Also, the line number returned may be invalid. */ |
50641945 | 2171 | |
f00aae0f | 2172 | /* Parse the linespec in ARG. */ |
50641945 | 2173 | |
ad32032e | 2174 | static struct symtabs_and_lines |
f00aae0f | 2175 | parse_linespec (linespec_parser *parser, const char *arg) |
50641945 | 2176 | { |
40e084e1 KS |
2177 | linespec_token token; |
2178 | struct symtabs_and_lines values; | |
7556d4a4 | 2179 | struct gdb_exception file_exception = exception_none; |
40e084e1 KS |
2180 | struct cleanup *cleanup; |
2181 | ||
2182 | /* A special case to start. It has become quite popular for | |
2183 | IDEs to work around bugs in the previous parser by quoting | |
2184 | the entire linespec, so we attempt to deal with this nicely. */ | |
2185 | parser->is_quote_enclosed = 0; | |
f00aae0f KS |
2186 | if (!is_ada_operator (arg) |
2187 | && strchr (linespec_quote_characters, *arg) != NULL) | |
40e084e1 KS |
2188 | { |
2189 | const char *end; | |
9ef07c8c | 2190 | |
f00aae0f | 2191 | end = skip_quote_char (arg + 1, *arg); |
40e084e1 | 2192 | if (end != NULL && is_closing_quote_enclosed (end)) |
136e1c30 | 2193 | { |
f00aae0f | 2194 | /* Here's the special case. Skip ARG past the initial |
40e084e1 | 2195 | quote. */ |
f00aae0f | 2196 | ++arg; |
40e084e1 | 2197 | parser->is_quote_enclosed = 1; |
136e1c30 DE |
2198 | } |
2199 | } | |
e8eb7bc5 | 2200 | |
f00aae0f KS |
2201 | parser->lexer.saved_arg = arg; |
2202 | parser->lexer.stream = arg; | |
d2630e69 | 2203 | |
40e084e1 KS |
2204 | /* Initialize the default symtab and line offset. */ |
2205 | initialize_defaults (&PARSER_STATE (parser)->default_symtab, | |
2206 | &PARSER_STATE (parser)->default_line); | |
d2630e69 | 2207 | |
40e084e1 | 2208 | /* Objective-C shortcut. */ |
f00aae0f | 2209 | values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg); |
40e084e1 KS |
2210 | if (values.sals != NULL) |
2211 | return values; | |
e0881a8e | 2212 | |
40e084e1 | 2213 | /* Start parsing. */ |
d2630e69 | 2214 | |
40e084e1 KS |
2215 | /* Get the first token. */ |
2216 | token = linespec_lexer_lex_one (parser); | |
50641945 | 2217 | |
40e084e1 | 2218 | /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */ |
a06efdd6 | 2219 | if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$') |
40e084e1 KS |
2220 | { |
2221 | char *var; | |
50641945 | 2222 | |
40e084e1 | 2223 | /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */ |
ec94af83 | 2224 | VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL); |
dcf9f4ab | 2225 | |
40e084e1 KS |
2226 | /* User specified a convenience variable or history value. */ |
2227 | var = copy_token_string (token); | |
2228 | cleanup = make_cleanup (xfree, var); | |
00e52e53 | 2229 | PARSER_EXPLICIT (parser)->line_offset |
40e084e1 | 2230 | = linespec_parse_variable (PARSER_STATE (parser), var); |
cf4ded82 | 2231 | do_cleanups (cleanup); |
f8eba3c6 | 2232 | |
40e084e1 KS |
2233 | /* If a line_offset wasn't found (VAR is the name of a user |
2234 | variable/function), then skip to normal symbol processing. */ | |
00e52e53 | 2235 | if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN) |
40e084e1 | 2236 | { |
40e084e1 KS |
2237 | /* Consume this token. */ |
2238 | linespec_lexer_consume_token (parser); | |
dcf9f4ab | 2239 | |
40e084e1 | 2240 | goto convert_to_sals; |
50641945 | 2241 | } |
40e084e1 KS |
2242 | } |
2243 | else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER) | |
2244 | unexpected_linespec_error (parser); | |
50641945 | 2245 | |
40e084e1 KS |
2246 | /* Shortcut: If the next token is not LSTOKEN_COLON, we know that |
2247 | this token cannot represent a filename. */ | |
2248 | token = linespec_lexer_peek_token (parser); | |
0e0b460e | 2249 | |
40e084e1 | 2250 | if (token.type == LSTOKEN_COLON) |
0e0b460e | 2251 | { |
40e084e1 | 2252 | char *user_filename; |
0e0b460e | 2253 | |
40e084e1 KS |
2254 | /* Get the current token again and extract the filename. */ |
2255 | token = linespec_lexer_lex_one (parser); | |
2256 | user_filename = copy_token_string (token); | |
50641945 | 2257 | |
40e084e1 | 2258 | /* Check if the input is a filename. */ |
492d29ea | 2259 | TRY |
40e084e1 KS |
2260 | { |
2261 | PARSER_RESULT (parser)->file_symtabs | |
c2f4122d PA |
2262 | = symtabs_from_filename (user_filename, |
2263 | PARSER_STATE (parser)->search_pspace); | |
40e084e1 | 2264 | } |
492d29ea | 2265 | CATCH (ex, RETURN_MASK_ERROR) |
7556d4a4 PA |
2266 | { |
2267 | file_exception = ex; | |
2268 | } | |
492d29ea | 2269 | END_CATCH |
50641945 | 2270 | |
40e084e1 KS |
2271 | if (file_exception.reason >= 0) |
2272 | { | |
2273 | /* Symtabs were found for the file. Record the filename. */ | |
00e52e53 | 2274 | PARSER_EXPLICIT (parser)->source_filename = user_filename; |
f8eba3c6 | 2275 | |
40e084e1 KS |
2276 | /* Get the next token. */ |
2277 | token = linespec_lexer_consume_token (parser); | |
50641945 | 2278 | |
40e084e1 KS |
2279 | /* This is LSTOKEN_COLON; consume it. */ |
2280 | linespec_lexer_consume_token (parser); | |
2281 | } | |
2282 | else | |
2283 | { | |
2284 | /* No symtabs found -- discard user_filename. */ | |
2285 | xfree (user_filename); | |
50641945 | 2286 | |
40e084e1 | 2287 | /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */ |
ec94af83 | 2288 | VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL); |
40e084e1 | 2289 | } |
50641945 | 2290 | } |
40e084e1 KS |
2291 | /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */ |
2292 | else if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD | |
2293 | && token.type != LSTOKEN_COMMA) | |
d2630e69 | 2294 | { |
40e084e1 KS |
2295 | /* TOKEN is the _next_ token, not the one currently in the parser. |
2296 | Consuming the token will give the correct error message. */ | |
2297 | linespec_lexer_consume_token (parser); | |
2298 | unexpected_linespec_error (parser); | |
d2630e69 | 2299 | } |
50641945 FN |
2300 | else |
2301 | { | |
40e084e1 | 2302 | /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */ |
ec94af83 | 2303 | VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL); |
50641945 | 2304 | } |
50641945 | 2305 | |
40e084e1 KS |
2306 | /* Parse the rest of the linespec. */ |
2307 | linespec_parse_basic (parser); | |
50641945 | 2308 | |
40e084e1 KS |
2309 | if (PARSER_RESULT (parser)->function_symbols == NULL |
2310 | && PARSER_RESULT (parser)->labels.label_symbols == NULL | |
00e52e53 | 2311 | && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN |
40e084e1 | 2312 | && PARSER_RESULT (parser)->minimal_symbols == NULL) |
f8eba3c6 | 2313 | { |
40e084e1 KS |
2314 | /* The linespec didn't parse. Re-throw the file exception if |
2315 | there was one. */ | |
2316 | if (file_exception.reason < 0) | |
2317 | throw_exception (file_exception); | |
0f5238ed | 2318 | |
40e084e1 | 2319 | /* Otherwise, the symbol is not found. */ |
00e52e53 KS |
2320 | symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name, |
2321 | PARSER_EXPLICIT (parser)->source_filename); | |
0f5238ed TT |
2322 | } |
2323 | ||
40e084e1 | 2324 | convert_to_sals: |
9ef07c8c | 2325 | |
40e084e1 KS |
2326 | /* Get the last token and record how much of the input was parsed, |
2327 | if necessary. */ | |
2328 | token = linespec_lexer_lex_one (parser); | |
2329 | if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD) | |
2330 | PARSER_STREAM (parser) = LS_TOKEN_STOKEN (token).ptr; | |
50641945 | 2331 | |
40e084e1 KS |
2332 | /* Convert the data in PARSER_RESULT to SALs. */ |
2333 | values = convert_linespec_to_sals (PARSER_STATE (parser), | |
2334 | PARSER_RESULT (parser)); | |
f8eba3c6 | 2335 | |
40e084e1 | 2336 | return values; |
413dad4d | 2337 | } |
50641945 | 2338 | |
40e084e1 | 2339 | |
f8eba3c6 | 2340 | /* A constructor for linespec_state. */ |
44fe14ab | 2341 | |
f8eba3c6 TT |
2342 | static void |
2343 | linespec_state_constructor (struct linespec_state *self, | |
40e084e1 | 2344 | int flags, const struct language_defn *language, |
c2f4122d | 2345 | struct program_space *search_pspace, |
f8eba3c6 TT |
2346 | struct symtab *default_symtab, |
2347 | int default_line, | |
2348 | struct linespec_result *canonical) | |
2349 | { | |
2350 | memset (self, 0, sizeof (*self)); | |
40e084e1 | 2351 | self->language = language; |
f8eba3c6 TT |
2352 | self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0; |
2353 | self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0; | |
c2f4122d | 2354 | self->search_pspace = search_pspace; |
f8eba3c6 TT |
2355 | self->default_symtab = default_symtab; |
2356 | self->default_line = default_line; | |
2357 | self->canonical = canonical; | |
2358 | self->program_space = current_program_space; | |
2359 | self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry, | |
2360 | xfree, xcalloc, xfree); | |
00e52e53 | 2361 | self->is_linespec = 0; |
f8eba3c6 | 2362 | } |
44fe14ab | 2363 | |
40e084e1 | 2364 | /* Initialize a new linespec parser. */ |
44fe14ab DC |
2365 | |
2366 | static void | |
40e084e1 KS |
2367 | linespec_parser_new (linespec_parser *parser, |
2368 | int flags, const struct language_defn *language, | |
c2f4122d | 2369 | struct program_space *search_pspace, |
40e084e1 KS |
2370 | struct symtab *default_symtab, |
2371 | int default_line, | |
2372 | struct linespec_result *canonical) | |
44fe14ab | 2373 | { |
f00aae0f | 2374 | memset (parser, 0, sizeof (linespec_parser)); |
40e084e1 KS |
2375 | parser->lexer.current.type = LSTOKEN_CONSUMED; |
2376 | memset (PARSER_RESULT (parser), 0, sizeof (struct linespec)); | |
00e52e53 | 2377 | PARSER_EXPLICIT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN; |
40e084e1 | 2378 | linespec_state_constructor (PARSER_STATE (parser), flags, language, |
c2f4122d | 2379 | search_pspace, |
40e084e1 KS |
2380 | default_symtab, default_line, canonical); |
2381 | } | |
2382 | ||
2383 | /* A destructor for linespec_state. */ | |
44fe14ab | 2384 | |
40e084e1 KS |
2385 | static void |
2386 | linespec_state_destructor (struct linespec_state *self) | |
2387 | { | |
f8eba3c6 TT |
2388 | htab_delete (self->addr_set); |
2389 | } | |
44fe14ab | 2390 | |
40e084e1 KS |
2391 | /* Delete a linespec parser. */ |
2392 | ||
2393 | static void | |
2394 | linespec_parser_delete (void *arg) | |
2395 | { | |
2396 | linespec_parser *parser = (linespec_parser *) arg; | |
2397 | ||
00e52e53 KS |
2398 | xfree (PARSER_EXPLICIT (parser)->source_filename); |
2399 | xfree (PARSER_EXPLICIT (parser)->label_name); | |
2400 | xfree (PARSER_EXPLICIT (parser)->function_name); | |
40e084e1 KS |
2401 | |
2402 | if (PARSER_RESULT (parser)->file_symtabs != NULL) | |
ec94af83 | 2403 | VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs); |
40e084e1 KS |
2404 | |
2405 | if (PARSER_RESULT (parser)->function_symbols != NULL) | |
2406 | VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols); | |
2407 | ||
2408 | if (PARSER_RESULT (parser)->minimal_symbols != NULL) | |
f60e2d5c | 2409 | VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols); |
40e084e1 KS |
2410 | |
2411 | if (PARSER_RESULT (parser)->labels.label_symbols != NULL) | |
2412 | VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols); | |
2413 | ||
2414 | if (PARSER_RESULT (parser)->labels.function_symbols != NULL) | |
2415 | VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols); | |
2416 | ||
2417 | linespec_state_destructor (PARSER_STATE (parser)); | |
2418 | } | |
2419 | ||
c7c1b3e9 KS |
2420 | /* See description in linespec.h. */ |
2421 | ||
2422 | void | |
2423 | linespec_lex_to_end (char **stringp) | |
2424 | { | |
2425 | linespec_parser parser; | |
2426 | struct cleanup *cleanup; | |
2427 | linespec_token token; | |
c7c1b3e9 KS |
2428 | const char *orig; |
2429 | ||
2430 | if (stringp == NULL || *stringp == NULL) | |
2431 | return; | |
2432 | ||
c2f4122d | 2433 | linespec_parser_new (&parser, 0, current_language, NULL, NULL, 0, NULL); |
c7c1b3e9 KS |
2434 | cleanup = make_cleanup (linespec_parser_delete, &parser); |
2435 | parser.lexer.saved_arg = *stringp; | |
2436 | PARSER_STREAM (&parser) = orig = *stringp; | |
2437 | ||
2438 | do | |
2439 | { | |
2440 | /* Stop before any comma tokens; we need it to keep it | |
2441 | as the next token in the string. */ | |
2442 | token = linespec_lexer_peek_token (&parser); | |
2443 | if (token.type == LSTOKEN_COMMA) | |
2444 | break; | |
c7c1b3e9 KS |
2445 | token = linespec_lexer_consume_token (&parser); |
2446 | } | |
2447 | while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD); | |
2448 | ||
2449 | *stringp += PARSER_STREAM (&parser) - orig; | |
2450 | do_cleanups (cleanup); | |
2451 | } | |
2452 | ||
f00aae0f KS |
2453 | /* A helper function for decode_line_full and decode_line_1 to |
2454 | turn LOCATION into symtabs_and_lines. */ | |
2455 | ||
2456 | static struct symtabs_and_lines | |
2457 | event_location_to_sals (linespec_parser *parser, | |
2458 | const struct event_location *location) | |
2459 | { | |
2460 | struct symtabs_and_lines result = {NULL, 0}; | |
2461 | ||
2462 | switch (event_location_type (location)) | |
2463 | { | |
2464 | case LINESPEC_LOCATION: | |
2465 | { | |
00e52e53 | 2466 | PARSER_STATE (parser)->is_linespec = 1; |
f00aae0f KS |
2467 | TRY |
2468 | { | |
2469 | result = parse_linespec (parser, get_linespec_location (location)); | |
2470 | } | |
2471 | CATCH (except, RETURN_MASK_ERROR) | |
2472 | { | |
2473 | throw_exception (except); | |
2474 | } | |
2475 | END_CATCH | |
2476 | } | |
2477 | break; | |
2478 | ||
a06efdd6 | 2479 | case ADDRESS_LOCATION: |
305e13e6 JB |
2480 | { |
2481 | const char *addr_string = get_address_string_location (location); | |
2482 | CORE_ADDR addr = get_address_location (location); | |
2483 | ||
2484 | if (addr_string != NULL) | |
2485 | { | |
2486 | char *expr = xstrdup (addr_string); | |
2487 | const char *const_expr = expr; | |
2488 | struct cleanup *cleanup = make_cleanup (xfree, expr); | |
2489 | ||
2490 | addr = linespec_expression_to_pc (&const_expr); | |
2491 | if (PARSER_STATE (parser)->canonical != NULL) | |
2492 | PARSER_STATE (parser)->canonical->location | |
2493 | = copy_event_location (location); | |
2494 | ||
2495 | do_cleanups (cleanup); | |
2496 | } | |
2497 | ||
2498 | result = convert_address_location_to_sals (PARSER_STATE (parser), | |
2499 | addr); | |
2500 | } | |
a06efdd6 KS |
2501 | break; |
2502 | ||
00e52e53 KS |
2503 | case EXPLICIT_LOCATION: |
2504 | { | |
67994074 | 2505 | const struct explicit_location *explicit_loc; |
00e52e53 | 2506 | |
67994074 | 2507 | explicit_loc = get_explicit_location_const (location); |
00e52e53 KS |
2508 | result = convert_explicit_location_to_sals (PARSER_STATE (parser), |
2509 | PARSER_RESULT (parser), | |
67994074 | 2510 | explicit_loc); |
00e52e53 KS |
2511 | } |
2512 | break; | |
2513 | ||
5b56227b KS |
2514 | case PROBE_LOCATION: |
2515 | /* Probes are handled by their own decoders. */ | |
2516 | gdb_assert_not_reached ("attempt to decode probe location"); | |
2517 | break; | |
2518 | ||
f00aae0f KS |
2519 | default: |
2520 | gdb_assert_not_reached ("unhandled event location type"); | |
2521 | } | |
2522 | ||
2523 | return result; | |
2524 | } | |
2525 | ||
f8eba3c6 | 2526 | /* See linespec.h. */ |
44fe14ab | 2527 | |
f8eba3c6 | 2528 | void |
f00aae0f | 2529 | decode_line_full (const struct event_location *location, int flags, |
c2f4122d | 2530 | struct program_space *search_pspace, |
f8eba3c6 TT |
2531 | struct symtab *default_symtab, |
2532 | int default_line, struct linespec_result *canonical, | |
2533 | const char *select_mode, | |
2534 | const char *filter) | |
44fe14ab | 2535 | { |
f8eba3c6 | 2536 | struct symtabs_and_lines result; |
f8eba3c6 | 2537 | struct cleanup *cleanups; |
f8eba3c6 | 2538 | VEC (const_char_ptr) *filters = NULL; |
40e084e1 KS |
2539 | linespec_parser parser; |
2540 | struct linespec_state *state; | |
f8eba3c6 TT |
2541 | |
2542 | gdb_assert (canonical != NULL); | |
2543 | /* The filter only makes sense for 'all'. */ | |
2544 | gdb_assert (filter == NULL || select_mode == multiple_symbols_all); | |
2545 | gdb_assert (select_mode == NULL | |
2546 | || select_mode == multiple_symbols_all | |
2547 | || select_mode == multiple_symbols_ask | |
2548 | || select_mode == multiple_symbols_cancel); | |
2549 | gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0); | |
2550 | ||
c2f4122d PA |
2551 | linespec_parser_new (&parser, flags, current_language, |
2552 | search_pspace, default_symtab, | |
40e084e1 KS |
2553 | default_line, canonical); |
2554 | cleanups = make_cleanup (linespec_parser_delete, &parser); | |
f8eba3c6 TT |
2555 | save_current_program_space (); |
2556 | ||
f00aae0f | 2557 | result = event_location_to_sals (&parser, location); |
40e084e1 | 2558 | state = PARSER_STATE (&parser); |
f8eba3c6 TT |
2559 | |
2560 | gdb_assert (result.nelts == 1 || canonical->pre_expanded); | |
f8eba3c6 TT |
2561 | canonical->pre_expanded = 1; |
2562 | ||
66f1999b | 2563 | /* Arrange for allocated canonical names to be freed. */ |
f8eba3c6 TT |
2564 | if (result.nelts > 0) |
2565 | { | |
2566 | int i; | |
2567 | ||
40e084e1 | 2568 | make_cleanup (xfree, state->canonical_names); |
f8eba3c6 TT |
2569 | for (i = 0; i < result.nelts; ++i) |
2570 | { | |
33f448b1 JK |
2571 | gdb_assert (state->canonical_names[i].suffix != NULL); |
2572 | make_cleanup (xfree, state->canonical_names[i].suffix); | |
f8eba3c6 TT |
2573 | } |
2574 | } | |
2575 | ||
2576 | if (select_mode == NULL) | |
2577 | { | |
112e8700 | 2578 | if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ()) |
f8eba3c6 TT |
2579 | select_mode = multiple_symbols_all; |
2580 | else | |
2581 | select_mode = multiple_symbols_select_mode (); | |
2582 | } | |
2583 | ||
2584 | if (select_mode == multiple_symbols_all) | |
2585 | { | |
2586 | if (filter != NULL) | |
2587 | { | |
2588 | make_cleanup (VEC_cleanup (const_char_ptr), &filters); | |
2589 | VEC_safe_push (const_char_ptr, filters, filter); | |
40e084e1 | 2590 | filter_results (state, &result, filters); |
f8eba3c6 TT |
2591 | } |
2592 | else | |
40e084e1 | 2593 | convert_results_to_lsals (state, &result); |
f8eba3c6 TT |
2594 | } |
2595 | else | |
40e084e1 | 2596 | decode_line_2 (state, &result, select_mode); |
f8eba3c6 TT |
2597 | |
2598 | do_cleanups (cleanups); | |
2599 | } | |
2600 | ||
39cf75f7 DE |
2601 | /* See linespec.h. */ |
2602 | ||
f8eba3c6 | 2603 | struct symtabs_and_lines |
f00aae0f | 2604 | decode_line_1 (const struct event_location *location, int flags, |
c2f4122d | 2605 | struct program_space *search_pspace, |
f8eba3c6 TT |
2606 | struct symtab *default_symtab, |
2607 | int default_line) | |
2608 | { | |
2609 | struct symtabs_and_lines result; | |
40e084e1 | 2610 | linespec_parser parser; |
f8eba3c6 TT |
2611 | struct cleanup *cleanups; |
2612 | ||
c2f4122d PA |
2613 | linespec_parser_new (&parser, flags, current_language, |
2614 | search_pspace, default_symtab, | |
40e084e1 KS |
2615 | default_line, NULL); |
2616 | cleanups = make_cleanup (linespec_parser_delete, &parser); | |
f8eba3c6 TT |
2617 | save_current_program_space (); |
2618 | ||
f00aae0f | 2619 | result = event_location_to_sals (&parser, location); |
40e084e1 | 2620 | |
f8eba3c6 TT |
2621 | do_cleanups (cleanups); |
2622 | return result; | |
2623 | } | |
2624 | ||
39cf75f7 DE |
2625 | /* See linespec.h. */ |
2626 | ||
2627 | struct symtabs_and_lines | |
2628 | decode_line_with_current_source (char *string, int flags) | |
2629 | { | |
2630 | struct symtabs_and_lines sals; | |
2631 | struct symtab_and_line cursal; | |
f00aae0f KS |
2632 | struct event_location *location; |
2633 | struct cleanup *cleanup; | |
39cf75f7 DE |
2634 | |
2635 | if (string == 0) | |
2636 | error (_("Empty line specification.")); | |
2637 | ||
2638 | /* We use whatever is set as the current source line. We do not try | |
2639 | and get a default source symtab+line or it will recursively call us! */ | |
2640 | cursal = get_current_source_symtab_and_line (); | |
2641 | ||
f00aae0f KS |
2642 | location = string_to_event_location (&string, current_language); |
2643 | cleanup = make_cleanup_delete_event_location (location); | |
c2f4122d | 2644 | sals = decode_line_1 (location, flags, NULL, |
39cf75f7 DE |
2645 | cursal.symtab, cursal.line); |
2646 | ||
2647 | if (*string) | |
2648 | error (_("Junk at end of line specification: %s"), string); | |
f00aae0f KS |
2649 | |
2650 | do_cleanups (cleanup); | |
39cf75f7 DE |
2651 | return sals; |
2652 | } | |
2653 | ||
2654 | /* See linespec.h. */ | |
2655 | ||
2656 | struct symtabs_and_lines | |
2657 | decode_line_with_last_displayed (char *string, int flags) | |
2658 | { | |
2659 | struct symtabs_and_lines sals; | |
f00aae0f KS |
2660 | struct event_location *location; |
2661 | struct cleanup *cleanup; | |
39cf75f7 DE |
2662 | |
2663 | if (string == 0) | |
2664 | error (_("Empty line specification.")); | |
2665 | ||
f00aae0f KS |
2666 | location = string_to_event_location (&string, current_language); |
2667 | cleanup = make_cleanup_delete_event_location (location); | |
39cf75f7 | 2668 | if (last_displayed_sal_is_valid ()) |
c2f4122d | 2669 | sals = decode_line_1 (location, flags, NULL, |
39cf75f7 DE |
2670 | get_last_displayed_symtab (), |
2671 | get_last_displayed_line ()); | |
2672 | else | |
c2f4122d | 2673 | sals = decode_line_1 (location, flags, NULL, (struct symtab *) NULL, 0); |
39cf75f7 DE |
2674 | |
2675 | if (*string) | |
2676 | error (_("Junk at end of line specification: %s"), string); | |
f00aae0f KS |
2677 | |
2678 | do_cleanups (cleanup); | |
39cf75f7 DE |
2679 | return sals; |
2680 | } | |
2681 | ||
f8eba3c6 TT |
2682 | \f |
2683 | ||
2684 | /* First, some functions to initialize stuff at the beggining of the | |
2685 | function. */ | |
2686 | ||
2687 | static void | |
2688 | initialize_defaults (struct symtab **default_symtab, int *default_line) | |
2689 | { | |
2690 | if (*default_symtab == 0) | |
2691 | { | |
2692 | /* Use whatever we have for the default source line. We don't use | |
2693 | get_current_or_default_symtab_and_line as it can recurse and call | |
2694 | us back! */ | |
2695 | struct symtab_and_line cursal = | |
2696 | get_current_source_symtab_and_line (); | |
2697 | ||
2698 | *default_symtab = cursal.symtab; | |
2699 | *default_line = cursal.line; | |
2700 | } | |
2701 | } | |
2702 | ||
2703 | \f | |
2704 | ||
40e084e1 KS |
2705 | /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR, |
2706 | advancing EXP_PTR past any parsed text. */ | |
f8eba3c6 | 2707 | |
a06efdd6 | 2708 | CORE_ADDR |
bbc13ae3 | 2709 | linespec_expression_to_pc (const char **exp_ptr) |
f8eba3c6 | 2710 | { |
f8eba3c6 TT |
2711 | if (current_program_space->executing_startup) |
2712 | /* The error message doesn't really matter, because this case | |
2713 | should only hit during breakpoint reset. */ | |
2714 | throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while " | |
2715 | "program space is in startup")); | |
2716 | ||
40e084e1 KS |
2717 | (*exp_ptr)++; |
2718 | return value_as_address (parse_to_comma_and_eval (exp_ptr)); | |
0960f083 DC |
2719 | } |
2720 | ||
2721 | \f | |
2722 | ||
d2630e69 AF |
2723 | /* Here's where we recognise an Objective-C Selector. An Objective C |
2724 | selector may be implemented by more than one class, therefore it | |
2725 | may represent more than one method/function. This gives us a | |
2726 | situation somewhat analogous to C++ overloading. If there's more | |
2727 | than one method that could represent the selector, then use some of | |
2728 | the existing C++ code to let the user choose one. */ | |
2729 | ||
f8eba3c6 | 2730 | static struct symtabs_and_lines |
f00aae0f | 2731 | decode_objc (struct linespec_state *self, linespec_p ls, const char *arg) |
d2630e69 | 2732 | { |
f8eba3c6 TT |
2733 | struct collect_info info; |
2734 | VEC (const_char_ptr) *symbol_names = NULL; | |
40e084e1 | 2735 | struct symtabs_and_lines values; |
d7561cbb | 2736 | const char *new_argptr; |
f8eba3c6 TT |
2737 | struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr), |
2738 | &symbol_names); | |
2739 | ||
2740 | info.state = self; | |
40e084e1 | 2741 | info.file_symtabs = NULL; |
ec94af83 DE |
2742 | VEC_safe_push (symtab_ptr, info.file_symtabs, NULL); |
2743 | make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs); | |
40e084e1 KS |
2744 | info.result.symbols = NULL; |
2745 | info.result.minimal_symbols = NULL; | |
2746 | values.nelts = 0; | |
2747 | values.sals = NULL; | |
f8eba3c6 | 2748 | |
f00aae0f | 2749 | new_argptr = find_imps (arg, &symbol_names); |
f8eba3c6 TT |
2750 | if (VEC_empty (const_char_ptr, symbol_names)) |
2751 | { | |
2752 | do_cleanups (cleanup); | |
40e084e1 | 2753 | return values; |
f8eba3c6 | 2754 | } |
d2630e69 | 2755 | |
f8eba3c6 | 2756 | add_all_symbol_names_from_pspace (&info, NULL, symbol_names); |
d2630e69 | 2757 | |
40e084e1 | 2758 | if (!VEC_empty (symbolp, info.result.symbols) |
f60e2d5c | 2759 | || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols)) |
d2630e69 | 2760 | { |
f8eba3c6 | 2761 | char *saved_arg; |
d2630e69 | 2762 | |
224c3ddb | 2763 | saved_arg = (char *) alloca (new_argptr - arg + 1); |
f00aae0f KS |
2764 | memcpy (saved_arg, arg, new_argptr - arg); |
2765 | saved_arg[new_argptr - arg] = '\0'; | |
d2630e69 | 2766 | |
67994074 | 2767 | ls->explicit_loc.function_name = xstrdup (saved_arg); |
40e084e1 KS |
2768 | ls->function_symbols = info.result.symbols; |
2769 | ls->minimal_symbols = info.result.minimal_symbols; | |
2770 | values = convert_linespec_to_sals (self, ls); | |
2771 | ||
f8eba3c6 | 2772 | if (self->canonical) |
d2630e69 | 2773 | { |
f00aae0f KS |
2774 | char *str; |
2775 | ||
f8eba3c6 | 2776 | self->canonical->pre_expanded = 1; |
f00aae0f | 2777 | |
67994074 | 2778 | if (ls->explicit_loc.source_filename) |
f00aae0f KS |
2779 | { |
2780 | str = xstrprintf ("%s:%s", | |
67994074 | 2781 | ls->explicit_loc.source_filename, saved_arg); |
f00aae0f | 2782 | } |
f8eba3c6 | 2783 | else |
f00aae0f KS |
2784 | str = xstrdup (saved_arg); |
2785 | ||
2786 | make_cleanup (xfree, str); | |
2787 | self->canonical->location = new_linespec_location (&str); | |
d2630e69 | 2788 | } |
d2630e69 AF |
2789 | } |
2790 | ||
f8eba3c6 | 2791 | do_cleanups (cleanup); |
c00f8484 | 2792 | |
40e084e1 | 2793 | return values; |
f8eba3c6 | 2794 | } |
c00f8484 | 2795 | |
ffdbe864 YQ |
2796 | namespace { |
2797 | ||
14bc53a8 PA |
2798 | /* A function object that serves as symbol_found_callback_ftype |
2799 | callback for iterate_over_symbols. This is used by | |
2800 | lookup_prefix_sym to collect type symbols. */ | |
2801 | class decode_compound_collector | |
f8eba3c6 | 2802 | { |
14bc53a8 | 2803 | public: |
fc4007c9 | 2804 | decode_compound_collector () |
14bc53a8 | 2805 | : m_symbols (NULL) |
fc4007c9 | 2806 | { |
14bc53a8 PA |
2807 | m_unique_syms = htab_create_alloc (1, htab_hash_pointer, |
2808 | htab_eq_pointer, NULL, | |
2809 | xcalloc, xfree); | |
fc4007c9 TT |
2810 | } |
2811 | ||
2812 | ~decode_compound_collector () | |
2813 | { | |
14bc53a8 PA |
2814 | if (m_unique_syms != NULL) |
2815 | htab_delete (m_unique_syms); | |
fc4007c9 | 2816 | } |
3a93a0c2 | 2817 | |
14bc53a8 PA |
2818 | /* Releases ownership of the collected symbols and returns them. */ |
2819 | VEC (symbolp) *release_symbols () | |
2820 | { | |
2821 | VEC (symbolp) *res = m_symbols; | |
2822 | m_symbols = NULL; | |
2823 | return res; | |
2824 | } | |
c00f8484 | 2825 | |
14bc53a8 PA |
2826 | /* Callable as a symbol_found_callback_ftype callback. */ |
2827 | bool operator () (symbol *sym); | |
2828 | ||
2829 | private: | |
2830 | /* A hash table of all symbols we found. We use this to avoid | |
2831 | adding any symbol more than once. */ | |
2832 | htab_t m_unique_syms; | |
2833 | ||
2834 | /* The result vector. */ | |
2835 | VEC (symbolp) *m_symbols; | |
2836 | }; | |
2837 | ||
2838 | bool | |
2839 | decode_compound_collector::operator () (symbol *sym) | |
f8eba3c6 | 2840 | { |
f8eba3c6 TT |
2841 | void **slot; |
2842 | struct type *t; | |
614b3b14 | 2843 | |
f8eba3c6 | 2844 | if (SYMBOL_CLASS (sym) != LOC_TYPEDEF) |
14bc53a8 | 2845 | return true; /* Continue iterating. */ |
f8eba3c6 TT |
2846 | |
2847 | t = SYMBOL_TYPE (sym); | |
f168693b | 2848 | t = check_typedef (t); |
f8eba3c6 TT |
2849 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT |
2850 | && TYPE_CODE (t) != TYPE_CODE_UNION | |
2851 | && TYPE_CODE (t) != TYPE_CODE_NAMESPACE) | |
14bc53a8 | 2852 | return true; /* Continue iterating. */ |
614b3b14 | 2853 | |
14bc53a8 | 2854 | slot = htab_find_slot (m_unique_syms, sym, INSERT); |
f8eba3c6 TT |
2855 | if (!*slot) |
2856 | { | |
2857 | *slot = sym; | |
14bc53a8 | 2858 | VEC_safe_push (symbolp, m_symbols, sym); |
f8eba3c6 TT |
2859 | } |
2860 | ||
14bc53a8 | 2861 | return true; /* Continue iterating. */ |
f8eba3c6 | 2862 | } |
93d91629 | 2863 | |
ffdbe864 YQ |
2864 | } // namespace |
2865 | ||
40e084e1 | 2866 | /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */ |
93d91629 | 2867 | |
f8eba3c6 | 2868 | static VEC (symbolp) * |
ec94af83 | 2869 | lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs, |
40e084e1 | 2870 | const char *class_name) |
93d91629 | 2871 | { |
f8eba3c6 TT |
2872 | int ix; |
2873 | struct symtab *elt; | |
14bc53a8 | 2874 | decode_compound_collector collector; |
e0881a8e | 2875 | |
ec94af83 | 2876 | for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix) |
f8eba3c6 TT |
2877 | { |
2878 | if (elt == NULL) | |
2879 | { | |
40e084e1 | 2880 | iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN, |
14bc53a8 | 2881 | NULL, false, collector); |
40e084e1 | 2882 | iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN, |
14bc53a8 | 2883 | NULL, false, collector); |
f8eba3c6 TT |
2884 | } |
2885 | else | |
2886 | { | |
f8eba3c6 TT |
2887 | /* Program spaces that are executing startup should have |
2888 | been filtered out earlier. */ | |
2889 | gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup); | |
2890 | set_current_program_space (SYMTAB_PSPACE (elt)); | |
14bc53a8 PA |
2891 | iterate_over_file_blocks (elt, class_name, STRUCT_DOMAIN, collector); |
2892 | iterate_over_file_blocks (elt, class_name, VAR_DOMAIN, collector); | |
1e5a1abc KS |
2893 | } |
2894 | } | |
2895 | ||
14bc53a8 | 2896 | return collector.release_symbols (); |
93d91629 DC |
2897 | } |
2898 | ||
40e084e1 KS |
2899 | /* A qsort comparison function for symbols. The resulting order does |
2900 | not actually matter; we just need to be able to sort them so that | |
2901 | symbols with the same program space end up next to each other. */ | |
2902 | ||
2903 | static int | |
2904 | compare_symbols (const void *a, const void *b) | |
2905 | { | |
9a3c8263 SM |
2906 | struct symbol * const *sa = (struct symbol * const*) a; |
2907 | struct symbol * const *sb = (struct symbol * const*) b; | |
40e084e1 KS |
2908 | uintptr_t uia, uib; |
2909 | ||
08be3fe3 DE |
2910 | uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa)); |
2911 | uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb)); | |
40e084e1 KS |
2912 | |
2913 | if (uia < uib) | |
2914 | return -1; | |
2915 | if (uia > uib) | |
2916 | return 1; | |
2917 | ||
2918 | uia = (uintptr_t) *sa; | |
2919 | uib = (uintptr_t) *sb; | |
2920 | ||
2921 | if (uia < uib) | |
2922 | return -1; | |
2923 | if (uia > uib) | |
2924 | return 1; | |
2925 | ||
2926 | return 0; | |
2927 | } | |
2928 | ||
2929 | /* Like compare_symbols but for minimal symbols. */ | |
4224873a | 2930 | |
f8eba3c6 | 2931 | static int |
40e084e1 | 2932 | compare_msymbols (const void *a, const void *b) |
4224873a | 2933 | { |
9a3c8263 SM |
2934 | const struct bound_minimal_symbol *sa |
2935 | = (const struct bound_minimal_symbol *) a; | |
2936 | const struct bound_minimal_symbol *sb | |
2937 | = (const struct bound_minimal_symbol *) b; | |
f8eba3c6 TT |
2938 | uintptr_t uia, uib; |
2939 | ||
001822aa TT |
2940 | uia = (uintptr_t) sa->objfile->pspace; |
2941 | uib = (uintptr_t) sa->objfile->pspace; | |
f8eba3c6 TT |
2942 | |
2943 | if (uia < uib) | |
2944 | return -1; | |
2945 | if (uia > uib) | |
2946 | return 1; | |
2947 | ||
001822aa TT |
2948 | uia = (uintptr_t) sa->minsym; |
2949 | uib = (uintptr_t) sb->minsym; | |
f8eba3c6 TT |
2950 | |
2951 | if (uia < uib) | |
2952 | return -1; | |
2953 | if (uia > uib) | |
2954 | return 1; | |
2955 | ||
2956 | return 0; | |
2957 | } | |
2958 | ||
2959 | /* Look for all the matching instances of each symbol in NAMES. Only | |
2960 | instances from PSPACE are considered; other program spaces are | |
2961 | handled by our caller. If PSPACE is NULL, then all program spaces | |
2962 | are considered. Results are stored into INFO. */ | |
2963 | ||
2964 | static void | |
2965 | add_all_symbol_names_from_pspace (struct collect_info *info, | |
2966 | struct program_space *pspace, | |
2967 | VEC (const_char_ptr) *names) | |
2968 | { | |
2969 | int ix; | |
2970 | const char *iter; | |
2971 | ||
2972 | for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix) | |
2973 | add_matching_symbols_to_info (iter, info, pspace); | |
2974 | } | |
2975 | ||
2976 | static void | |
2977 | find_superclass_methods (VEC (typep) *superclasses, | |
2978 | const char *name, | |
2979 | VEC (const_char_ptr) **result_names) | |
2980 | { | |
2981 | int old_len = VEC_length (const_char_ptr, *result_names); | |
2982 | VEC (typep) *iter_classes; | |
2983 | struct cleanup *cleanup = make_cleanup (null_cleanup, NULL); | |
2984 | ||
2985 | iter_classes = superclasses; | |
2986 | while (1) | |
2987 | { | |
2988 | VEC (typep) *new_supers = NULL; | |
2989 | int ix; | |
2990 | struct type *t; | |
2991 | ||
2992 | make_cleanup (VEC_cleanup (typep), &new_supers); | |
2993 | for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix) | |
2994 | find_methods (t, name, result_names, &new_supers); | |
2995 | ||
2996 | if (VEC_length (const_char_ptr, *result_names) != old_len | |
2997 | || VEC_empty (typep, new_supers)) | |
2998 | break; | |
4224873a | 2999 | |
f8eba3c6 TT |
3000 | iter_classes = new_supers; |
3001 | } | |
4224873a | 3002 | |
f8eba3c6 TT |
3003 | do_cleanups (cleanup); |
3004 | } | |
3005 | ||
40e084e1 KS |
3006 | /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is |
3007 | given by one of the symbols in SYM_CLASSES. Matches are returned | |
3008 | in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */ | |
f8eba3c6 | 3009 | |
40e084e1 | 3010 | static void |
ec94af83 | 3011 | find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs, |
40e084e1 KS |
3012 | const char *class_name, const char *method_name, |
3013 | VEC (symbolp) *sym_classes, VEC (symbolp) **symbols, | |
f60e2d5c | 3014 | VEC (bound_minimal_symbol_d) **minsyms) |
f8eba3c6 | 3015 | { |
f8eba3c6 TT |
3016 | struct symbol *sym; |
3017 | struct cleanup *cleanup = make_cleanup (null_cleanup, NULL); | |
3018 | int ix; | |
3019 | int last_result_len; | |
3020 | VEC (typep) *superclass_vec; | |
3021 | VEC (const_char_ptr) *result_names; | |
3022 | struct collect_info info; | |
4224873a | 3023 | |
f8eba3c6 TT |
3024 | /* Sort symbols so that symbols with the same program space are next |
3025 | to each other. */ | |
3026 | qsort (VEC_address (symbolp, sym_classes), | |
3027 | VEC_length (symbolp, sym_classes), | |
3028 | sizeof (symbolp), | |
3029 | compare_symbols); | |
3030 | ||
3031 | info.state = self; | |
40e084e1 KS |
3032 | info.file_symtabs = file_symtabs; |
3033 | info.result.symbols = NULL; | |
3034 | info.result.minimal_symbols = NULL; | |
f8eba3c6 TT |
3035 | |
3036 | /* Iterate over all the types, looking for the names of existing | |
40e084e1 | 3037 | methods matching METHOD_NAME. If we cannot find a direct method in a |
f8eba3c6 TT |
3038 | given program space, then we consider inherited methods; this is |
3039 | not ideal (ideal would be to respect C++ hiding rules), but it | |
3040 | seems good enough and is what GDB has historically done. We only | |
3041 | need to collect the names because later we find all symbols with | |
3042 | those names. This loop is written in a somewhat funny way | |
3043 | because we collect data across the program space before deciding | |
3044 | what to do. */ | |
3045 | superclass_vec = NULL; | |
3046 | make_cleanup (VEC_cleanup (typep), &superclass_vec); | |
3047 | result_names = NULL; | |
3048 | make_cleanup (VEC_cleanup (const_char_ptr), &result_names); | |
3049 | last_result_len = 0; | |
3050 | for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix) | |
3051 | { | |
3052 | struct type *t; | |
3053 | struct program_space *pspace; | |
3054 | ||
3055 | /* Program spaces that are executing startup should have | |
3056 | been filtered out earlier. */ | |
08be3fe3 DE |
3057 | pspace = SYMTAB_PSPACE (symbol_symtab (sym)); |
3058 | gdb_assert (!pspace->executing_startup); | |
f8eba3c6 TT |
3059 | set_current_program_space (pspace); |
3060 | t = check_typedef (SYMBOL_TYPE (sym)); | |
40e084e1 | 3061 | find_methods (t, method_name, &result_names, &superclass_vec); |
f8eba3c6 TT |
3062 | |
3063 | /* Handle all items from a single program space at once; and be | |
3064 | sure not to miss the last batch. */ | |
3065 | if (ix == VEC_length (symbolp, sym_classes) - 1 | |
3066 | || (pspace | |
08be3fe3 | 3067 | != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes, |
f8eba3c6 | 3068 | ix + 1))))) |
4224873a | 3069 | { |
f8eba3c6 TT |
3070 | /* If we did not find a direct implementation anywhere in |
3071 | this program space, consider superclasses. */ | |
3072 | if (VEC_length (const_char_ptr, result_names) == last_result_len) | |
40e084e1 KS |
3073 | find_superclass_methods (superclass_vec, method_name, |
3074 | &result_names); | |
f8eba3c6 TT |
3075 | |
3076 | /* We have a list of candidate symbol names, so now we | |
3077 | iterate over the symbol tables looking for all | |
3078 | matches in this pspace. */ | |
3079 | add_all_symbol_names_from_pspace (&info, pspace, result_names); | |
3080 | ||
3081 | VEC_truncate (typep, superclass_vec, 0); | |
3082 | last_result_len = VEC_length (const_char_ptr, result_names); | |
4224873a | 3083 | } |
4224873a | 3084 | } |
f8eba3c6 | 3085 | |
40e084e1 | 3086 | if (!VEC_empty (symbolp, info.result.symbols) |
f60e2d5c | 3087 | || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols)) |
4224873a | 3088 | { |
40e084e1 KS |
3089 | *symbols = info.result.symbols; |
3090 | *minsyms = info.result.minimal_symbols; | |
f8eba3c6 | 3091 | do_cleanups (cleanup); |
40e084e1 | 3092 | return; |
4224873a | 3093 | } |
f8eba3c6 | 3094 | |
40e084e1 KS |
3095 | /* Throw an NOT_FOUND_ERROR. This will be caught by the caller |
3096 | and other attempts to locate the symbol will be made. */ | |
3097 | throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter")); | |
f8eba3c6 TT |
3098 | } |
3099 | ||
3100 | \f | |
3101 | ||
ffdbe864 YQ |
3102 | namespace { |
3103 | ||
14bc53a8 PA |
3104 | /* This function object is a callback for iterate_over_symtabs, used |
3105 | when collecting all matching symtabs. */ | |
f8eba3c6 | 3106 | |
14bc53a8 | 3107 | class symtab_collector |
f8eba3c6 | 3108 | { |
14bc53a8 | 3109 | public: |
fc4007c9 | 3110 | symtab_collector () |
fc4007c9 | 3111 | { |
14bc53a8 PA |
3112 | m_symtabs = NULL; |
3113 | m_symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer, | |
3114 | NULL); | |
fc4007c9 TT |
3115 | } |
3116 | ||
3117 | ~symtab_collector () | |
3118 | { | |
14bc53a8 PA |
3119 | if (m_symtab_table != NULL) |
3120 | htab_delete (m_symtab_table); | |
fc4007c9 | 3121 | } |
f8eba3c6 | 3122 | |
14bc53a8 PA |
3123 | /* Callable as a symbol_found_callback_ftype callback. */ |
3124 | bool operator () (symtab *sym); | |
f8eba3c6 | 3125 | |
14bc53a8 PA |
3126 | /* Releases ownership of the collected symtabs and returns them. */ |
3127 | VEC (symtab_ptr) *release_symtabs () | |
3128 | { | |
3129 | VEC (symtab_ptr) *res = m_symtabs; | |
3130 | m_symtabs = NULL; | |
3131 | return res; | |
3132 | } | |
3133 | ||
3134 | private: | |
3135 | /* The result vector of symtabs. */ | |
3136 | VEC (symtab_ptr) *m_symtabs; | |
3137 | ||
3138 | /* This is used to ensure the symtabs are unique. */ | |
3139 | htab_t m_symtab_table; | |
3140 | }; | |
3141 | ||
3142 | bool | |
3143 | symtab_collector::operator () (struct symtab *symtab) | |
f8eba3c6 | 3144 | { |
f8eba3c6 TT |
3145 | void **slot; |
3146 | ||
14bc53a8 | 3147 | slot = htab_find_slot (m_symtab_table, symtab, INSERT); |
f8eba3c6 | 3148 | if (!*slot) |
4224873a | 3149 | { |
f8eba3c6 | 3150 | *slot = symtab; |
14bc53a8 | 3151 | VEC_safe_push (symtab_ptr, m_symtabs, symtab); |
4224873a | 3152 | } |
f8eba3c6 | 3153 | |
14bc53a8 | 3154 | return false; |
4224873a DC |
3155 | } |
3156 | ||
ffdbe864 YQ |
3157 | } // namespace |
3158 | ||
c2f4122d PA |
3159 | /* Given a file name, return a VEC of all matching symtabs. If |
3160 | SEARCH_PSPACE is not NULL, the search is restricted to just that | |
3161 | program space. */ | |
f8eba3c6 | 3162 | |
ec94af83 | 3163 | static VEC (symtab_ptr) * |
c2f4122d PA |
3164 | collect_symtabs_from_filename (const char *file, |
3165 | struct program_space *search_pspace) | |
f8eba3c6 | 3166 | { |
14bc53a8 | 3167 | symtab_collector collector; |
f8eba3c6 TT |
3168 | |
3169 | /* Find that file's data. */ | |
c2f4122d PA |
3170 | if (search_pspace == NULL) |
3171 | { | |
14bc53a8 PA |
3172 | struct program_space *pspace; |
3173 | ||
c2f4122d PA |
3174 | ALL_PSPACES (pspace) |
3175 | { | |
3176 | if (pspace->executing_startup) | |
3177 | continue; | |
f8eba3c6 | 3178 | |
c2f4122d | 3179 | set_current_program_space (pspace); |
14bc53a8 | 3180 | iterate_over_symtabs (file, collector); |
c2f4122d PA |
3181 | } |
3182 | } | |
3183 | else | |
3184 | { | |
3185 | set_current_program_space (search_pspace); | |
14bc53a8 | 3186 | iterate_over_symtabs (file, collector); |
c2f4122d | 3187 | } |
f3c39e76 | 3188 | |
14bc53a8 | 3189 | return collector.release_symtabs (); |
f8eba3c6 TT |
3190 | } |
3191 | ||
c2f4122d PA |
3192 | /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is |
3193 | not NULL, the search is restricted to just that program space. */ | |
f8eba3c6 | 3194 | |
ec94af83 | 3195 | static VEC (symtab_ptr) * |
c2f4122d PA |
3196 | symtabs_from_filename (const char *filename, |
3197 | struct program_space *search_pspace) | |
40e084e1 | 3198 | { |
ec94af83 | 3199 | VEC (symtab_ptr) *result; |
40e084e1 | 3200 | |
c2f4122d | 3201 | result = collect_symtabs_from_filename (filename, search_pspace); |
f8eba3c6 | 3202 | |
ec94af83 | 3203 | if (VEC_empty (symtab_ptr, result)) |
f8eba3c6 | 3204 | { |
40e084e1 KS |
3205 | if (!have_full_symbols () && !have_partial_symbols ()) |
3206 | throw_error (NOT_FOUND_ERROR, | |
3207 | _("No symbol table is loaded. " | |
3208 | "Use the \"file\" command.")); | |
00e52e53 | 3209 | source_file_not_found_error (filename); |
f8eba3c6 TT |
3210 | } |
3211 | ||
40e084e1 | 3212 | return result; |
84fba31b | 3213 | } |
f3c39e76 | 3214 | |
40e084e1 KS |
3215 | /* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching |
3216 | debug symbols are returned in SYMBOLS. Matching minimal symbols are | |
3217 | returned in MINSYMS. */ | |
14e91ac5 | 3218 | |
40e084e1 KS |
3219 | static void |
3220 | find_function_symbols (struct linespec_state *state, | |
ec94af83 | 3221 | VEC (symtab_ptr) *file_symtabs, const char *name, |
40e084e1 | 3222 | VEC (symbolp) **symbols, |
f60e2d5c | 3223 | VEC (bound_minimal_symbol_d) **minsyms) |
14e91ac5 | 3224 | { |
40e084e1 KS |
3225 | struct collect_info info; |
3226 | VEC (const_char_ptr) *symbol_names = NULL; | |
3227 | struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr), | |
3228 | &symbol_names); | |
14e91ac5 | 3229 | |
40e084e1 KS |
3230 | info.state = state; |
3231 | info.result.symbols = NULL; | |
3232 | info.result.minimal_symbols = NULL; | |
3233 | info.file_symtabs = file_symtabs; | |
e0881a8e | 3234 | |
40e084e1 | 3235 | /* Try NAME as an Objective-C selector. */ |
d7561cbb | 3236 | find_imps (name, &symbol_names); |
40e084e1 | 3237 | if (!VEC_empty (const_char_ptr, symbol_names)) |
c2f4122d PA |
3238 | add_all_symbol_names_from_pspace (&info, state->search_pspace, |
3239 | symbol_names); | |
40e084e1 | 3240 | else |
c2f4122d | 3241 | add_matching_symbols_to_info (name, &info, state->search_pspace); |
40e084e1 KS |
3242 | |
3243 | do_cleanups (cleanup); | |
3244 | ||
3245 | if (VEC_empty (symbolp, info.result.symbols)) | |
3246 | { | |
3247 | VEC_free (symbolp, info.result.symbols); | |
3248 | *symbols = NULL; | |
14e91ac5 DC |
3249 | } |
3250 | else | |
40e084e1 KS |
3251 | *symbols = info.result.symbols; |
3252 | ||
f60e2d5c | 3253 | if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols)) |
14e91ac5 | 3254 | { |
f60e2d5c | 3255 | VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols); |
40e084e1 KS |
3256 | *minsyms = NULL; |
3257 | } | |
3258 | else | |
3259 | *minsyms = info.result.minimal_symbols; | |
3260 | } | |
3261 | ||
3262 | /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols | |
3263 | in SYMBOLS and minimal symbols in MINSYMS. */ | |
14e91ac5 | 3264 | |
b1ae631a | 3265 | static void |
40e084e1 | 3266 | find_linespec_symbols (struct linespec_state *state, |
ec94af83 | 3267 | VEC (symtab_ptr) *file_symtabs, |
40e084e1 KS |
3268 | const char *name, |
3269 | VEC (symbolp) **symbols, | |
f60e2d5c | 3270 | VEC (bound_minimal_symbol_d) **minsyms) |
40e084e1 | 3271 | { |
2f408ecb PA |
3272 | demangle_result_storage demangle_storage; |
3273 | std::string ada_lookup_storage; | |
cc81e1c6 | 3274 | const char *lookup_name; |
f8eba3c6 | 3275 | |
40e084e1 KS |
3276 | if (state->language->la_language == language_ada) |
3277 | { | |
3278 | /* In Ada, the symbol lookups are performed using the encoded | |
3279 | name rather than the demangled name. */ | |
2f408ecb PA |
3280 | ada_lookup_storage = ada_name_for_lookup (name); |
3281 | lookup_name = ada_lookup_storage.c_str (); | |
40e084e1 | 3282 | } |
2f408ecb | 3283 | else |
40e084e1 | 3284 | { |
2f408ecb PA |
3285 | lookup_name = demangle_for_lookup (name, |
3286 | state->language->la_language, | |
3287 | demangle_storage); | |
40e084e1 | 3288 | } |
f8eba3c6 | 3289 | |
2f408ecb PA |
3290 | std::string canon = cp_canonicalize_string_no_typedefs (lookup_name); |
3291 | if (!canon.empty ()) | |
3292 | lookup_name = canon.c_str (); | |
3293 | ||
cc81e1c6 DE |
3294 | /* It's important to not call expand_symtabs_matching unnecessarily |
3295 | as it can really slow things down (by unnecessarily expanding | |
3296 | potentially 1000s of symtabs, which when debugging some apps can | |
3297 | cost 100s of seconds). Avoid this to some extent by *first* calling | |
3298 | find_function_symbols, and only if that doesn't find anything | |
3299 | *then* call find_method. This handles two important cases: | |
3300 | 1) break (anonymous namespace)::foo | |
3301 | 2) break class::method where method is in class (and not a baseclass) */ | |
14e91ac5 | 3302 | |
cc81e1c6 DE |
3303 | find_function_symbols (state, file_symtabs, lookup_name, |
3304 | symbols, minsyms); | |
14e91ac5 | 3305 | |
cc81e1c6 DE |
3306 | /* If we were unable to locate a symbol of the same name, try dividing |
3307 | the name into class and method names and searching the class and its | |
3308 | baseclasses. */ | |
3309 | if (VEC_empty (symbolp, *symbols) | |
f60e2d5c | 3310 | && VEC_empty (bound_minimal_symbol_d, *minsyms)) |
40e084e1 | 3311 | { |
2f408ecb | 3312 | std::string klass, method; |
cc81e1c6 DE |
3313 | const char *last, *p, *scope_op; |
3314 | VEC (symbolp) *classes; | |
14e91ac5 | 3315 | |
cc81e1c6 DE |
3316 | /* See if we can find a scope operator and break this symbol |
3317 | name into namespaces${SCOPE_OPERATOR}class_name and method_name. */ | |
3318 | scope_op = "::"; | |
3319 | p = find_toplevel_string (lookup_name, scope_op); | |
14e91ac5 | 3320 | |
cc81e1c6 DE |
3321 | last = NULL; |
3322 | while (p != NULL) | |
f8eba3c6 | 3323 | { |
cc81e1c6 DE |
3324 | last = p; |
3325 | p = find_toplevel_string (p + strlen (scope_op), scope_op); | |
f8eba3c6 | 3326 | } |
14e91ac5 | 3327 | |
cc81e1c6 DE |
3328 | /* If no scope operator was found, there is nothing more we can do; |
3329 | we already attempted to lookup the entire name as a symbol | |
3330 | and failed. */ | |
3331 | if (last == NULL) | |
2f408ecb | 3332 | return; |
cc81e1c6 DE |
3333 | |
3334 | /* LOOKUP_NAME points to the class name. | |
3335 | LAST points to the method name. */ | |
2f408ecb | 3336 | klass = std::string (lookup_name, last - lookup_name); |
cc81e1c6 DE |
3337 | |
3338 | /* Skip past the scope operator. */ | |
3339 | last += strlen (scope_op); | |
2f408ecb | 3340 | method = last; |
cc81e1c6 DE |
3341 | |
3342 | /* Find a list of classes named KLASS. */ | |
2f408ecb PA |
3343 | classes = lookup_prefix_sym (state, file_symtabs, klass.c_str ()); |
3344 | struct cleanup *old_chain | |
3345 | = make_cleanup (VEC_cleanup (symbolp), &classes); | |
cc81e1c6 DE |
3346 | |
3347 | if (!VEC_empty (symbolp, classes)) | |
3348 | { | |
3349 | /* Now locate a list of suitable methods named METHOD. */ | |
492d29ea | 3350 | TRY |
cc81e1c6 | 3351 | { |
2f408ecb PA |
3352 | find_method (state, file_symtabs, |
3353 | klass.c_str (), method.c_str (), | |
3354 | classes, symbols, minsyms); | |
cc81e1c6 DE |
3355 | } |
3356 | ||
3357 | /* If successful, we're done. If NOT_FOUND_ERROR | |
3358 | was not thrown, rethrow the exception that we did get. */ | |
492d29ea | 3359 | CATCH (except, RETURN_MASK_ERROR) |
7556d4a4 PA |
3360 | { |
3361 | if (except.error != NOT_FOUND_ERROR) | |
3362 | throw_exception (except); | |
3363 | } | |
492d29ea | 3364 | END_CATCH |
cc81e1c6 | 3365 | } |
14e91ac5 | 3366 | |
2f408ecb PA |
3367 | do_cleanups (old_chain); |
3368 | } | |
14e91ac5 DC |
3369 | } |
3370 | ||
40e084e1 KS |
3371 | /* Return all labels named NAME in FUNCTION_SYMBOLS. Return the |
3372 | actual function symbol in which the label was found in LABEL_FUNC_RET. */ | |
0f5238ed | 3373 | |
40e084e1 KS |
3374 | static VEC (symbolp) * |
3375 | find_label_symbols (struct linespec_state *self, | |
3376 | VEC (symbolp) *function_symbols, | |
3377 | VEC (symbolp) **label_funcs_ret, const char *name) | |
0f5238ed | 3378 | { |
f8eba3c6 | 3379 | int ix; |
3977b71f | 3380 | const struct block *block; |
40e084e1 KS |
3381 | struct symbol *sym; |
3382 | struct symbol *fn_sym; | |
3383 | VEC (symbolp) *result = NULL; | |
9ef07c8c | 3384 | |
f8eba3c6 | 3385 | if (function_symbols == NULL) |
9ef07c8c | 3386 | { |
f8eba3c6 | 3387 | set_current_program_space (self->program_space); |
4eeaa230 | 3388 | block = get_current_search_block (); |
f8eba3c6 | 3389 | |
9ef07c8c TT |
3390 | for (; |
3391 | block && !BLOCK_FUNCTION (block); | |
3392 | block = BLOCK_SUPERBLOCK (block)) | |
3393 | ; | |
3394 | if (!block) | |
40e084e1 | 3395 | return NULL; |
f8eba3c6 TT |
3396 | fn_sym = BLOCK_FUNCTION (block); |
3397 | ||
d12307c1 | 3398 | sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol; |
f8eba3c6 | 3399 | |
40e084e1 KS |
3400 | if (sym != NULL) |
3401 | { | |
3402 | VEC_safe_push (symbolp, result, sym); | |
3403 | VEC_safe_push (symbolp, *label_funcs_ret, fn_sym); | |
3404 | } | |
3405 | } | |
3406 | else | |
3407 | { | |
3408 | for (ix = 0; | |
3409 | VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix) | |
f8eba3c6 | 3410 | { |
08be3fe3 | 3411 | set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym))); |
40e084e1 | 3412 | block = SYMBOL_BLOCK_VALUE (fn_sym); |
d12307c1 | 3413 | sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol; |
40e084e1 KS |
3414 | |
3415 | if (sym != NULL) | |
3416 | { | |
3417 | VEC_safe_push (symbolp, result, sym); | |
3418 | VEC_safe_push (symbolp, *label_funcs_ret, fn_sym); | |
3419 | } | |
f8eba3c6 | 3420 | } |
40e084e1 | 3421 | } |
f8eba3c6 | 3422 | |
40e084e1 KS |
3423 | return result; |
3424 | } | |
f8eba3c6 | 3425 | |
40e084e1 KS |
3426 | \f |
3427 | ||
3428 | /* A helper for create_sals_line_offset that handles the 'list_mode' case. */ | |
3429 | ||
3430 | static void | |
3431 | decode_digits_list_mode (struct linespec_state *self, | |
3432 | linespec_p ls, | |
3433 | struct symtabs_and_lines *values, | |
3434 | struct symtab_and_line val) | |
3435 | { | |
3436 | int ix; | |
3437 | struct symtab *elt; | |
3438 | ||
3439 | gdb_assert (self->list_mode); | |
3440 | ||
ec94af83 | 3441 | for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); |
40e084e1 KS |
3442 | ++ix) |
3443 | { | |
3444 | /* The logic above should ensure this. */ | |
3445 | gdb_assert (elt != NULL); | |
3446 | ||
3447 | set_current_program_space (SYMTAB_PSPACE (elt)); | |
3448 | ||
3449 | /* Simplistic search just for the list command. */ | |
3450 | val.symtab = find_line_symtab (elt, val.line, NULL, NULL); | |
3451 | if (val.symtab == NULL) | |
3452 | val.symtab = elt; | |
3453 | val.pspace = SYMTAB_PSPACE (elt); | |
3454 | val.pc = 0; | |
3455 | val.explicit_line = 1; | |
3456 | ||
66f1999b | 3457 | add_sal_to_sals (self, values, &val, NULL, 0); |
f8eba3c6 | 3458 | } |
40e084e1 | 3459 | } |
f8eba3c6 | 3460 | |
40e084e1 KS |
3461 | /* A helper for create_sals_line_offset that iterates over the symtabs, |
3462 | adding lines to the VEC. */ | |
3463 | ||
3464 | static void | |
3465 | decode_digits_ordinary (struct linespec_state *self, | |
3466 | linespec_p ls, | |
3467 | int line, | |
3468 | struct symtabs_and_lines *sals, | |
3469 | struct linetable_entry **best_entry) | |
3470 | { | |
3471 | int ix; | |
3472 | struct symtab *elt; | |
f8eba3c6 | 3473 | |
ec94af83 | 3474 | for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix) |
f8eba3c6 | 3475 | { |
40e084e1 KS |
3476 | int i; |
3477 | VEC (CORE_ADDR) *pcs; | |
3478 | CORE_ADDR pc; | |
3479 | ||
3480 | /* The logic above should ensure this. */ | |
3481 | gdb_assert (elt != NULL); | |
f8eba3c6 | 3482 | |
40e084e1 | 3483 | set_current_program_space (SYMTAB_PSPACE (elt)); |
f8eba3c6 | 3484 | |
40e084e1 KS |
3485 | pcs = find_pcs_for_symtab_line (elt, line, best_entry); |
3486 | for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i) | |
f8eba3c6 TT |
3487 | { |
3488 | struct symtab_and_line sal; | |
40e084e1 KS |
3489 | |
3490 | init_sal (&sal); | |
3491 | sal.pspace = SYMTAB_PSPACE (elt); | |
3492 | sal.symtab = elt; | |
3493 | sal.line = line; | |
3494 | sal.pc = pc; | |
3495 | add_sal_to_sals_basic (sals, &sal); | |
f8eba3c6 | 3496 | } |
40e084e1 KS |
3497 | |
3498 | VEC_free (CORE_ADDR, pcs); | |
f8eba3c6 | 3499 | } |
40e084e1 KS |
3500 | } |
3501 | ||
3502 | \f | |
3503 | ||
3504 | /* Return the line offset represented by VARIABLE. */ | |
3505 | ||
3506 | static struct line_offset | |
3507 | linespec_parse_variable (struct linespec_state *self, const char *variable) | |
3508 | { | |
3509 | int index = 0; | |
3510 | const char *p; | |
3511 | struct line_offset offset = {0, LINE_OFFSET_NONE}; | |
f8eba3c6 | 3512 | |
40e084e1 KS |
3513 | p = (variable[1] == '$') ? variable + 2 : variable + 1; |
3514 | if (*p == '$') | |
3515 | ++p; | |
3516 | while (*p >= '0' && *p <= '9') | |
3517 | ++p; | |
3518 | if (!*p) /* Reached end of token without hitting non-digit. */ | |
f8eba3c6 | 3519 | { |
40e084e1 KS |
3520 | /* We have a value history reference. */ |
3521 | struct value *val_history; | |
f8eba3c6 | 3522 | |
40e084e1 KS |
3523 | sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index); |
3524 | val_history | |
3525 | = access_value_history ((variable[1] == '$') ? -index : index); | |
3526 | if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT) | |
3527 | error (_("History values used in line " | |
3528 | "specs must have integer values.")); | |
3529 | offset.offset = value_as_long (val_history); | |
3530 | } | |
3531 | else | |
3532 | { | |
3533 | /* Not all digits -- may be user variable/function or a | |
3534 | convenience variable. */ | |
3535 | LONGEST valx; | |
3536 | struct internalvar *ivar; | |
3537 | ||
3538 | /* Try it as a convenience variable. If it is not a convenience | |
3539 | variable, return and allow normal symbol lookup to occur. */ | |
3540 | ivar = lookup_only_internalvar (variable + 1); | |
3541 | if (ivar == NULL) | |
3542 | /* No internal variable with that name. Mark the offset | |
3543 | as unknown to allow the name to be looked up as a symbol. */ | |
3544 | offset.sign = LINE_OFFSET_UNKNOWN; | |
3545 | else | |
3546 | { | |
3547 | /* We found a valid variable name. If it is not an integer, | |
3548 | throw an error. */ | |
3549 | if (!get_internalvar_integer (ivar, &valx)) | |
3550 | error (_("Convenience variables used in line " | |
3551 | "specs must have integer values.")); | |
3552 | else | |
3553 | offset.offset = valx; | |
3554 | } | |
f8eba3c6 TT |
3555 | } |
3556 | ||
40e084e1 | 3557 | return offset; |
f8eba3c6 | 3558 | } |
40e084e1 | 3559 | \f |
f8eba3c6 | 3560 | |
40e084e1 | 3561 | /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our |
6e22494e JK |
3562 | linespec; return the SAL in RESULT. This function should return SALs |
3563 | matching those from find_function_start_sal, otherwise false | |
3564 | multiple-locations breakpoints could be placed. */ | |
f8eba3c6 TT |
3565 | |
3566 | static void | |
3567 | minsym_found (struct linespec_state *self, struct objfile *objfile, | |
3568 | struct minimal_symbol *msymbol, | |
3569 | struct symtabs_and_lines *result) | |
3570 | { | |
3571 | struct gdbarch *gdbarch = get_objfile_arch (objfile); | |
3572 | CORE_ADDR pc; | |
3573 | struct symtab_and_line sal; | |
3574 | ||
77e371c0 | 3575 | sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile, msymbol), |
f8eba3c6 | 3576 | (struct obj_section *) 0, 0); |
efd66ac6 | 3577 | sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol); |
f8eba3c6 TT |
3578 | |
3579 | /* The minimal symbol might point to a function descriptor; | |
3580 | resolve it to the actual code address instead. */ | |
3581 | pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, ¤t_target); | |
3582 | if (pc != sal.pc) | |
3583 | sal = find_pc_sect_line (pc, NULL, 0); | |
3584 | ||
3585 | if (self->funfirstline) | |
6e22494e JK |
3586 | { |
3587 | if (sal.symtab != NULL | |
3588 | && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab)) | |
3589 | || SYMTAB_LANGUAGE (sal.symtab) == language_asm)) | |
3590 | { | |
3591 | /* If gdbarch_convert_from_func_ptr_addr does not apply then | |
3592 | sal.SECTION, sal.LINE&co. will stay correct from above. | |
3593 | If gdbarch_convert_from_func_ptr_addr applies then | |
3594 | sal.SECTION is cleared from above and sal.LINE&co. will | |
3595 | stay correct from the last find_pc_sect_line above. */ | |
3596 | sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol); | |
3597 | sal.pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, | |
3598 | ¤t_target); | |
141c5cc4 JK |
3599 | if (gdbarch_skip_entrypoint_p (gdbarch)) |
3600 | sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc); | |
6e22494e JK |
3601 | } |
3602 | else | |
3603 | skip_prologue_sal (&sal); | |
3604 | } | |
f8eba3c6 | 3605 | |
07fea4b4 | 3606 | if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc)) |
efd66ac6 | 3607 | add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0); |
f8eba3c6 TT |
3608 | } |
3609 | ||
39b856a4 TT |
3610 | /* A helper struct to pass some data through |
3611 | iterate_over_minimal_symbols. */ | |
3612 | ||
3613 | struct collect_minsyms | |
3614 | { | |
3615 | /* The objfile we're examining. */ | |
3616 | struct objfile *objfile; | |
3617 | ||
87186c6a MMN |
3618 | /* Only search the given symtab, or NULL to search for all symbols. */ |
3619 | struct symtab *symtab; | |
3620 | ||
39b856a4 TT |
3621 | /* The funfirstline setting from the initial call. */ |
3622 | int funfirstline; | |
3623 | ||
095bcf5e JB |
3624 | /* The list_mode setting from the initial call. */ |
3625 | int list_mode; | |
3626 | ||
39b856a4 | 3627 | /* The resulting symbols. */ |
f60e2d5c | 3628 | VEC (bound_minimal_symbol_d) *msyms; |
39b856a4 TT |
3629 | }; |
3630 | ||
3631 | /* A helper function to classify a minimal_symbol_type according to | |
3632 | priority. */ | |
3633 | ||
3634 | static int | |
3635 | classify_mtype (enum minimal_symbol_type t) | |
3636 | { | |
3637 | switch (t) | |
f8eba3c6 | 3638 | { |
39b856a4 TT |
3639 | case mst_file_text: |
3640 | case mst_file_data: | |
3641 | case mst_file_bss: | |
3642 | /* Intermediate priority. */ | |
3643 | return 1; | |
3644 | ||
3645 | case mst_solib_trampoline: | |
3646 | /* Lowest priority. */ | |
3647 | return 2; | |
3648 | ||
3649 | default: | |
3650 | /* Highest priority. */ | |
3651 | return 0; | |
f8eba3c6 | 3652 | } |
39b856a4 TT |
3653 | } |
3654 | ||
3655 | /* Callback for qsort that sorts symbols by priority. */ | |
3656 | ||
3657 | static int | |
3658 | compare_msyms (const void *a, const void *b) | |
3659 | { | |
9a3c8263 SM |
3660 | const bound_minimal_symbol_d *moa = (const bound_minimal_symbol_d *) a; |
3661 | const bound_minimal_symbol_d *mob = (const bound_minimal_symbol_d *) b; | |
39b856a4 TT |
3662 | enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym); |
3663 | enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym); | |
3664 | ||
3665 | return classify_mtype (ta) - classify_mtype (tb); | |
3666 | } | |
3667 | ||
3668 | /* Callback for iterate_over_minimal_symbols that adds the symbol to | |
3669 | the result. */ | |
3670 | ||
3671 | static void | |
3672 | add_minsym (struct minimal_symbol *minsym, void *d) | |
3673 | { | |
9a3c8263 | 3674 | struct collect_minsyms *info = (struct collect_minsyms *) d; |
f60e2d5c | 3675 | bound_minimal_symbol_d mo; |
39b856a4 | 3676 | |
77e371c0 TT |
3677 | mo.minsym = minsym; |
3678 | mo.objfile = info->objfile; | |
3679 | ||
87186c6a MMN |
3680 | if (info->symtab != NULL) |
3681 | { | |
3682 | CORE_ADDR pc; | |
3683 | struct symtab_and_line sal; | |
3684 | struct gdbarch *gdbarch = get_objfile_arch (info->objfile); | |
3685 | ||
3686 | sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym), | |
3687 | NULL, 0); | |
3688 | sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym); | |
3689 | pc | |
3690 | = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, ¤t_target); | |
3691 | if (pc != sal.pc) | |
3692 | sal = find_pc_sect_line (pc, NULL, 0); | |
3693 | ||
3694 | if (info->symtab != sal.symtab) | |
3695 | return; | |
3696 | } | |
3697 | ||
095bcf5e JB |
3698 | /* Exclude data symbols when looking for breakpoint locations. */ |
3699 | if (!info->list_mode) | |
3700 | switch (minsym->type) | |
3701 | { | |
3702 | case mst_slot_got_plt: | |
3703 | case mst_data: | |
3704 | case mst_bss: | |
3705 | case mst_abs: | |
3706 | case mst_file_data: | |
3707 | case mst_file_bss: | |
1a2da5ee JB |
3708 | { |
3709 | /* Make sure this minsym is not a function descriptor | |
3710 | before we decide to discard it. */ | |
df6d5441 | 3711 | struct gdbarch *gdbarch = get_objfile_arch (info->objfile); |
1a2da5ee | 3712 | CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr |
77e371c0 | 3713 | (gdbarch, BMSYMBOL_VALUE_ADDRESS (mo), |
1a2da5ee JB |
3714 | ¤t_target); |
3715 | ||
77e371c0 | 3716 | if (addr == BMSYMBOL_VALUE_ADDRESS (mo)) |
1a2da5ee JB |
3717 | return; |
3718 | } | |
095bcf5e JB |
3719 | } |
3720 | ||
f60e2d5c | 3721 | VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo); |
f8eba3c6 TT |
3722 | } |
3723 | ||
87186c6a | 3724 | /* Search for minimal symbols called NAME. If SEARCH_PSPACE |
f8eba3c6 | 3725 | is not NULL, the search is restricted to just that program |
87186c6a MMN |
3726 | space. |
3727 | ||
3728 | If SYMTAB is NULL, search all objfiles, otherwise | |
3729 | restrict results to the given SYMTAB. */ | |
f8eba3c6 TT |
3730 | |
3731 | static void | |
3732 | search_minsyms_for_name (struct collect_info *info, const char *name, | |
87186c6a MMN |
3733 | struct program_space *search_pspace, |
3734 | struct symtab *symtab) | |
f8eba3c6 | 3735 | { |
87186c6a MMN |
3736 | struct collect_minsyms local; |
3737 | struct cleanup *cleanup; | |
f8eba3c6 | 3738 | |
87186c6a MMN |
3739 | memset (&local, 0, sizeof (local)); |
3740 | local.funfirstline = info->state->funfirstline; | |
3741 | local.list_mode = info->state->list_mode; | |
3742 | local.symtab = symtab; | |
39b856a4 | 3743 | |
87186c6a | 3744 | cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms); |
f8eba3c6 | 3745 | |
87186c6a MMN |
3746 | if (symtab == NULL) |
3747 | { | |
3748 | struct program_space *pspace; | |
f8eba3c6 | 3749 | |
87186c6a MMN |
3750 | ALL_PSPACES (pspace) |
3751 | { | |
3752 | struct objfile *objfile; | |
39b856a4 | 3753 | |
87186c6a MMN |
3754 | if (search_pspace != NULL && search_pspace != pspace) |
3755 | continue; | |
3756 | if (pspace->executing_startup) | |
3757 | continue; | |
39b856a4 | 3758 | |
87186c6a MMN |
3759 | set_current_program_space (pspace); |
3760 | ||
3761 | ALL_OBJFILES (objfile) | |
3762 | { | |
3763 | local.objfile = objfile; | |
3764 | iterate_over_minimal_symbols (objfile, name, add_minsym, &local); | |
3765 | } | |
3766 | } | |
3767 | } | |
3768 | else | |
f8eba3c6 | 3769 | { |
87186c6a MMN |
3770 | if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace) |
3771 | { | |
3772 | set_current_program_space (SYMTAB_PSPACE (symtab)); | |
3773 | local.objfile = SYMTAB_OBJFILE(symtab); | |
3774 | iterate_over_minimal_symbols (local.objfile, name, add_minsym, | |
3775 | &local); | |
3776 | } | |
9ef07c8c | 3777 | } |
39b856a4 | 3778 | |
f60e2d5c | 3779 | if (!VEC_empty (bound_minimal_symbol_d, local.msyms)) |
39b856a4 TT |
3780 | { |
3781 | int classification; | |
3782 | int ix; | |
f60e2d5c | 3783 | bound_minimal_symbol_d *item; |
39b856a4 | 3784 | |
f60e2d5c TT |
3785 | qsort (VEC_address (bound_minimal_symbol_d, local.msyms), |
3786 | VEC_length (bound_minimal_symbol_d, local.msyms), | |
3787 | sizeof (bound_minimal_symbol_d), | |
39b856a4 TT |
3788 | compare_msyms); |
3789 | ||
3790 | /* Now the minsyms are in classification order. So, we walk | |
3791 | over them and process just the minsyms with the same | |
3792 | classification as the very first minsym in the list. */ | |
f60e2d5c | 3793 | item = VEC_index (bound_minimal_symbol_d, local.msyms, 0); |
39b856a4 TT |
3794 | classification = classify_mtype (MSYMBOL_TYPE (item->minsym)); |
3795 | ||
3796 | for (ix = 0; | |
f60e2d5c | 3797 | VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item); |
39b856a4 TT |
3798 | ++ix) |
3799 | { | |
3800 | if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification) | |
3801 | break; | |
3802 | ||
f60e2d5c | 3803 | VEC_safe_push (bound_minimal_symbol_d, |
40e084e1 | 3804 | info->result.minimal_symbols, item); |
39b856a4 TT |
3805 | } |
3806 | } | |
3807 | ||
3808 | do_cleanups (cleanup); | |
f8eba3c6 TT |
3809 | } |
3810 | ||
3811 | /* A helper function to add all symbols matching NAME to INFO. If | |
3812 | PSPACE is not NULL, the search is restricted to just that program | |
3813 | space. */ | |
0f5238ed | 3814 | |
f8eba3c6 TT |
3815 | static void |
3816 | add_matching_symbols_to_info (const char *name, | |
3817 | struct collect_info *info, | |
3818 | struct program_space *pspace) | |
3819 | { | |
3820 | int ix; | |
3821 | struct symtab *elt; | |
0f5238ed | 3822 | |
ec94af83 | 3823 | for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix) |
f8eba3c6 | 3824 | { |
f8eba3c6 TT |
3825 | if (elt == NULL) |
3826 | { | |
40e084e1 | 3827 | iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN, |
14bc53a8 PA |
3828 | pspace, true, [&] (symbol *sym) |
3829 | { return info->add_symbol (sym); }); | |
87186c6a | 3830 | search_minsyms_for_name (info, name, pspace, NULL); |
f8eba3c6 TT |
3831 | } |
3832 | else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt)) | |
3833 | { | |
87186c6a MMN |
3834 | int prev_len = VEC_length (symbolp, info->result.symbols); |
3835 | ||
f8eba3c6 TT |
3836 | /* Program spaces that are executing startup should have |
3837 | been filtered out earlier. */ | |
3838 | gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup); | |
3839 | set_current_program_space (SYMTAB_PSPACE (elt)); | |
14bc53a8 PA |
3840 | iterate_over_file_blocks (elt, name, VAR_DOMAIN, [&] (symbol *sym) |
3841 | { return info->add_symbol (sym); }); | |
87186c6a MMN |
3842 | |
3843 | /* If no new symbols were found in this iteration and this symtab | |
3844 | is in assembler, we might actually be looking for a label for | |
3845 | which we don't have debug info. Check for a minimal symbol in | |
3846 | this case. */ | |
3847 | if (prev_len == VEC_length (symbolp, info->result.symbols) | |
3848 | && elt->language == language_asm) | |
3849 | search_minsyms_for_name (info, name, pspace, elt); | |
f8eba3c6 TT |
3850 | } |
3851 | } | |
0f5238ed TT |
3852 | } |
3853 | ||
14e91ac5 DC |
3854 | \f |
3855 | ||
413dad4d DC |
3856 | /* Now come some functions that are called from multiple places within |
3857 | decode_line_1. */ | |
3858 | ||
f8eba3c6 TT |
3859 | static int |
3860 | symbol_to_sal (struct symtab_and_line *result, | |
3861 | int funfirstline, struct symbol *sym) | |
413dad4d | 3862 | { |
413dad4d | 3863 | if (SYMBOL_CLASS (sym) == LOC_BLOCK) |
50641945 | 3864 | { |
f8eba3c6 TT |
3865 | *result = find_function_start_sal (sym, funfirstline); |
3866 | return 1; | |
50641945 | 3867 | } |
413dad4d DC |
3868 | else |
3869 | { | |
62853458 | 3870 | if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0) |
413dad4d | 3871 | { |
f8eba3c6 | 3872 | init_sal (result); |
08be3fe3 | 3873 | result->symtab = symbol_symtab (sym); |
f8eba3c6 TT |
3874 | result->line = SYMBOL_LINE (sym); |
3875 | result->pc = SYMBOL_VALUE_ADDRESS (sym); | |
08be3fe3 | 3876 | result->pspace = SYMTAB_PSPACE (result->symtab); |
f8eba3c6 TT |
3877 | result->explicit_pc = 1; |
3878 | return 1; | |
413dad4d | 3879 | } |
62853458 | 3880 | else if (funfirstline) |
dcf9f4ab | 3881 | { |
f8eba3c6 | 3882 | /* Nothing. */ |
dcf9f4ab | 3883 | } |
62853458 TT |
3884 | else if (SYMBOL_LINE (sym) != 0) |
3885 | { | |
3886 | /* We know its line number. */ | |
f8eba3c6 | 3887 | init_sal (result); |
08be3fe3 | 3888 | result->symtab = symbol_symtab (sym); |
f8eba3c6 | 3889 | result->line = SYMBOL_LINE (sym); |
08be3fe3 | 3890 | result->pspace = SYMTAB_PSPACE (result->symtab); |
f8eba3c6 | 3891 | return 1; |
62853458 | 3892 | } |
413dad4d | 3893 | } |
f8eba3c6 TT |
3894 | |
3895 | return 0; | |
413dad4d | 3896 | } |
50641945 | 3897 | |
f8eba3c6 | 3898 | /* See the comment in linespec.h. */ |
50641945 | 3899 | |
f8eba3c6 TT |
3900 | void |
3901 | init_linespec_result (struct linespec_result *lr) | |
413dad4d | 3902 | { |
f8eba3c6 TT |
3903 | memset (lr, 0, sizeof (*lr)); |
3904 | } | |
413dad4d | 3905 | |
f8eba3c6 | 3906 | /* See the comment in linespec.h. */ |
bccdca4a | 3907 | |
f8eba3c6 TT |
3908 | void |
3909 | destroy_linespec_result (struct linespec_result *ls) | |
3910 | { | |
3911 | int i; | |
3912 | struct linespec_sals *lsal; | |
bccdca4a | 3913 | |
f00aae0f | 3914 | delete_event_location (ls->location); |
f8eba3c6 TT |
3915 | for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i) |
3916 | { | |
3917 | xfree (lsal->canonical); | |
3918 | xfree (lsal->sals.sals); | |
3919 | } | |
3920 | VEC_free (linespec_sals, ls->sals); | |
3921 | } | |
e48883f7 | 3922 | |
f8eba3c6 TT |
3923 | /* Cleanup function for a linespec_result. */ |
3924 | ||
3925 | static void | |
3926 | cleanup_linespec_result (void *a) | |
3927 | { | |
9a3c8263 | 3928 | destroy_linespec_result ((struct linespec_result *) a); |
50641945 | 3929 | } |
7efd8fc2 | 3930 | |
f8eba3c6 TT |
3931 | /* See the comment in linespec.h. */ |
3932 | ||
3933 | struct cleanup * | |
3934 | make_cleanup_destroy_linespec_result (struct linespec_result *ls) | |
7efd8fc2 | 3935 | { |
f8eba3c6 | 3936 | return make_cleanup (cleanup_linespec_result, ls); |
7efd8fc2 | 3937 | } |
87f0e720 KS |
3938 | |
3939 | /* Return the quote characters permitted by the linespec parser. */ | |
3940 | ||
3941 | const char * | |
3942 | get_gdb_linespec_parser_quote_characters (void) | |
3943 | { | |
3944 | return linespec_quote_characters; | |
3945 | } |