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