Introduce gdb::function_view
[deliverable/binutils-gdb.git] / gdb / linespec.c
CommitLineData
50641945 1/* Parser for linespec for the GNU debugger, GDB.
05ff989b 2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
50641945
FN
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
50641945
FN
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
50641945
FN
19
20#include "defs.h"
21#include "symtab.h"
c5f0f3d0
FN
22#include "frame.h"
23#include "command.h"
50641945
FN
24#include "symfile.h"
25#include "objfiles.h"
0378c332 26#include "source.h"
50641945 27#include "demangle.h"
c5f0f3d0
FN
28#include "value.h"
29#include "completer.h"
015a42b4 30#include "cp-abi.h"
12907978 31#include "cp-support.h"
c38da1af 32#include "parser-defs.h"
fe898f56 33#include "block.h"
d2630e69 34#include "objc-lang.h"
b9362cc7 35#include "linespec.h"
53c5240f 36#include "language.h"
dc67126b
NR
37#include "interps.h"
38#include "mi/mi-cmds.h"
bccdca4a 39#include "target.h"
94af9270 40#include "arch-utils.h"
c00f8484
KS
41#include <ctype.h>
42#include "cli/cli-utils.h"
731971ed 43#include "filenames.h"
f8eba3c6 44#include "ada-lang.h"
39cf75f7 45#include "stack.h"
f00aae0f 46#include "location.h"
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;
33f448b1
JK
1331 struct decode_line_2_item *items;
1332 int items_count;
50641945 1333
f8eba3c6
TT
1334 gdb_assert (select_mode != multiple_symbols_all);
1335 gdb_assert (self->canonical != NULL);
33f448b1
JK
1336 gdb_assert (result->nelts >= 1);
1337
1338 old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
50641945 1339
33f448b1
JK
1340 /* Prepare ITEMS array. */
1341 items_count = result->nelts;
8d749320 1342 items = XNEWVEC (struct decode_line_2_item, items_count);
33f448b1
JK
1343 make_cleanup (xfree, items);
1344 for (i = 0; i < items_count; ++i)
50641945 1345 {
33f448b1
JK
1346 const struct linespec_canonical_name *canonical;
1347 struct decode_line_2_item *item;
1348
1349 canonical = &self->canonical_names[i];
1350 gdb_assert (canonical->suffix != NULL);
1351 item = &items[i];
f8eba3c6 1352
33f448b1
JK
1353 item->fullform = canonical_to_fullform (canonical);
1354 make_cleanup (xfree, item->fullform);
1355
1356 if (canonical->symtab == NULL)
1357 item->displayform = canonical->suffix;
1358 else
f8eba3c6 1359 {
33f448b1
JK
1360 const char *fn_for_display;
1361
1362 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
1363 item->displayform = xstrprintf ("%s:%s", fn_for_display,
1364 canonical->suffix);
1365 make_cleanup (xfree, item->displayform);
f8eba3c6
TT
1366 }
1367
33f448b1 1368 item->selected = 0;
50641945
FN
1369 }
1370
33f448b1
JK
1371 /* Sort the list of method names. */
1372 qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
1373
1374 /* Remove entries with the same FULLFORM. */
1375 if (items_count >= 2)
1376 {
1377 struct decode_line_2_item *dst, *src;
1378
1379 dst = items;
1380 for (src = &items[1]; src < &items[items_count]; src++)
1381 if (strcmp (src->fullform, dst->fullform) != 0)
1382 *++dst = *src;
1383 items_count = dst + 1 - items;
1384 }
1385
1386 if (select_mode == multiple_symbols_cancel && items_count > 1)
f8eba3c6
TT
1387 error (_("canceled because the command is ambiguous\n"
1388 "See set/show multiple-symbol."));
1389
33f448b1 1390 if (select_mode == multiple_symbols_all || items_count == 1)
50641945 1391 {
f8eba3c6
TT
1392 do_cleanups (old_chain);
1393 convert_results_to_lsals (self, result);
1394 return;
50641945
FN
1395 }
1396
f8eba3c6 1397 printf_unfiltered (_("[0] cancel\n[1] all\n"));
33f448b1
JK
1398 for (i = 0; i < items_count; i++)
1399 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);
f8eba3c6
TT
1400
1401 prompt = getenv ("PS2");
1402 if (prompt == NULL)
50641945 1403 {
f8eba3c6 1404 prompt = "> ";
50641945 1405 }
f8eba3c6 1406 args = command_line_input (prompt, 0, "overload-choice");
50641945
FN
1407
1408 if (args == 0 || *args == 0)
e2e0b3e5 1409 error_no_arg (_("one or more choice numbers"));
50641945 1410
bfd28288
PA
1411 number_or_range_parser parser (args);
1412 while (!parser.finished ())
50641945 1413 {
bfd28288 1414 int num = parser.get_number ();
50641945
FN
1415
1416 if (num == 0)
8a3fe4f8 1417 error (_("canceled"));
50641945
FN
1418 else if (num == 1)
1419 {
f8eba3c6
TT
1420 /* We intentionally make this result in a single breakpoint,
1421 contrary to what older versions of gdb did. The
1422 rationale is that this lets a user get the
1423 multiple_symbols_all behavior even with the 'ask'
1424 setting; and he can get separate breakpoints by entering
1425 "2-57" at the query. */
1426 do_cleanups (old_chain);
1427 convert_results_to_lsals (self, result);
1428 return;
50641945
FN
1429 }
1430
f8eba3c6 1431 num -= 2;
33f448b1 1432 if (num >= items_count)
f8eba3c6 1433 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
1434 else
1435 {
33f448b1 1436 struct decode_line_2_item *item = &items[num];
f8eba3c6 1437
33f448b1 1438 if (!item->selected)
50641945 1439 {
33f448b1
JK
1440 VEC_safe_push (const_char_ptr, filters, item->fullform);
1441 item->selected = 1;
50641945
FN
1442 }
1443 else
1444 {
3e43a32a 1445 printf_unfiltered (_("duplicate request for %d ignored.\n"),
f6f99966 1446 num + 2);
50641945
FN
1447 }
1448 }
50641945 1449 }
f8eba3c6
TT
1450
1451 filter_results (self, result, filters);
1452 do_cleanups (old_chain);
50641945 1453}
94af9270 1454
40e084e1 1455\f
3d50dd94 1456
40e084e1
KS
1457/* The parser of linespec itself. */
1458
1459/* Throw an appropriate error when SYMBOL is not found (optionally in
1460 FILENAME). */
1461
1462static void ATTRIBUTE_NORETURN
5d94e27b 1463symbol_not_found_error (const char *symbol, const char *filename)
3d50dd94 1464{
40e084e1
KS
1465 if (symbol == NULL)
1466 symbol = "";
1467
1468 if (!have_full_symbols ()
1469 && !have_partial_symbols ()
1470 && !have_minimal_symbols ())
1471 throw_error (NOT_FOUND_ERROR,
1472 _("No symbol table is loaded. Use the \"file\" command."));
1473
1474 /* If SYMBOL starts with '$', the user attempted to either lookup
1475 a function/variable in his code starting with '$' or an internal
1476 variable of that name. Since we do not know which, be concise and
1477 explain both possibilities. */
1478 if (*symbol == '$')
1479 {
1480 if (filename)
1481 throw_error (NOT_FOUND_ERROR,
1482 _("Undefined convenience variable or function \"%s\" "
1483 "not defined in \"%s\"."), symbol, filename);
1484 else
1485 throw_error (NOT_FOUND_ERROR,
1486 _("Undefined convenience variable or function \"%s\" "
1487 "not defined."), symbol);
1488 }
1489 else
1490 {
1491 if (filename)
1492 throw_error (NOT_FOUND_ERROR,
1493 _("Function \"%s\" not defined in \"%s\"."),
1494 symbol, filename);
1495 else
1496 throw_error (NOT_FOUND_ERROR,
1497 _("Function \"%s\" not defined."), symbol);
1498 }
3d50dd94
JK
1499}
1500
40e084e1
KS
1501/* Throw an appropriate error when an unexpected token is encountered
1502 in the input. */
94af9270 1503
40e084e1
KS
1504static void ATTRIBUTE_NORETURN
1505unexpected_linespec_error (linespec_parser *parser)
94af9270 1506{
40e084e1
KS
1507 linespec_token token;
1508 static const char * token_type_strings[]
1509 = {"keyword", "colon", "string", "number", "comma", "end of input"};
94af9270 1510
40e084e1
KS
1511 /* Get the token that generated the error. */
1512 token = linespec_lexer_lex_one (parser);
94af9270 1513
40e084e1
KS
1514 /* Finally, throw the error. */
1515 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1516 || token.type == LSTOKEN_KEYWORD)
94af9270 1517 {
40e084e1 1518 char *string;
40e084e1
KS
1519
1520 string = copy_token_string (token);
78cc6c2d 1521 make_cleanup (xfree, string);
40e084e1
KS
1522 throw_error (GENERIC_ERROR,
1523 _("malformed linespec error: unexpected %s, \"%s\""),
1524 token_type_strings[token.type], string);
1525 }
1526 else
1527 throw_error (GENERIC_ERROR,
1528 _("malformed linespec error: unexpected %s"),
1529 token_type_strings[token.type]);
1530}
1531
00e52e53
KS
1532/* Throw an undefined label error. */
1533
1534static void ATTRIBUTE_NORETURN
1535undefined_label_error (const char *function, const char *label)
1536{
1537 if (function != NULL)
1538 throw_error (NOT_FOUND_ERROR,
1539 _("No label \"%s\" defined in function \"%s\"."),
1540 label, function);
1541 else
1542 throw_error (NOT_FOUND_ERROR,
1543 _("No label \"%s\" defined in current function."),
1544 label);
1545}
1546
1547/* Throw a source file not found error. */
1548
1549static void ATTRIBUTE_NORETURN
1550source_file_not_found_error (const char *name)
1551{
1552 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1553}
1554
87f0e720 1555/* See description in linespec.h. */
40e084e1 1556
87f0e720 1557struct line_offset
09cf2b22 1558linespec_parse_line_offset (const char *string)
40e084e1 1559{
87f0e720 1560 const char *start = string;
40e084e1
KS
1561 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1562
1563 if (*string == '+')
1564 {
1565 line_offset.sign = LINE_OFFSET_PLUS;
1566 ++string;
1567 }
1568 else if (*string == '-')
1569 {
1570 line_offset.sign = LINE_OFFSET_MINUS;
1571 ++string;
1572 }
1573
87f0e720
KS
1574 if (*string != '\0' && !isdigit (*string))
1575 error (_("malformed line offset: \"%s\""), start);
1576
40e084e1
KS
1577 /* Right now, we only allow base 10 for offsets. */
1578 line_offset.offset = atoi (string);
1579 return line_offset;
1580}
1581
1582/* Parse the basic_spec in PARSER's input. */
1583
1584static void
1585linespec_parse_basic (linespec_parser *parser)
1586{
1587 char *name;
1588 linespec_token token;
1589 VEC (symbolp) *symbols, *labels;
f60e2d5c 1590 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1
KS
1591 struct cleanup *cleanup;
1592
1593 /* Get the next token. */
1594 token = linespec_lexer_lex_one (parser);
1595
1596 /* If it is EOI or KEYWORD, issue an error. */
1597 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1598 unexpected_linespec_error (parser);
1599 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1600 else if (token.type == LSTOKEN_NUMBER)
1601 {
1602 /* Record the line offset and get the next token. */
1603 name = copy_token_string (token);
1604 cleanup = make_cleanup (xfree, name);
00e52e53 1605 PARSER_EXPLICIT (parser)->line_offset = linespec_parse_line_offset (name);
40e084e1
KS
1606 do_cleanups (cleanup);
1607
1608 /* Get the next token. */
1609 token = linespec_lexer_consume_token (parser);
1610
1611 /* If the next token is a comma, stop parsing and return. */
1612 if (token.type == LSTOKEN_COMMA)
1613 return;
1614
1615 /* If the next token is anything but EOI or KEYWORD, issue
1616 an error. */
1617 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1618 unexpected_linespec_error (parser);
1619 }
1620
1621 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1622 return;
1623
1624 /* Next token must be LSTOKEN_STRING. */
1625 if (token.type != LSTOKEN_STRING)
1626 unexpected_linespec_error (parser);
1627
1628 /* The current token will contain the name of a function, method,
1629 or label. */
1630 name = copy_token_string (token);
1631 cleanup = make_cleanup (xfree, name);
1632
1633 /* Try looking it up as a function/method. */
1634 find_linespec_symbols (PARSER_STATE (parser),
1635 PARSER_RESULT (parser)->file_symtabs, name,
1636 &symbols, &minimal_symbols);
1637
1638 if (symbols != NULL || minimal_symbols != NULL)
1639 {
1640 PARSER_RESULT (parser)->function_symbols = symbols;
1641 PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
00e52e53 1642 PARSER_EXPLICIT (parser)->function_name = name;
40e084e1
KS
1643 symbols = NULL;
1644 discard_cleanups (cleanup);
1645 }
1646 else
1647 {
1648 /* NAME was not a function or a method. So it must be a label
b4013987 1649 name or user specified variable like "break foo.c:$zippo". */
40e084e1
KS
1650 labels = find_label_symbols (PARSER_STATE (parser), NULL,
1651 &symbols, name);
1652 if (labels != NULL)
94af9270 1653 {
40e084e1
KS
1654 PARSER_RESULT (parser)->labels.label_symbols = labels;
1655 PARSER_RESULT (parser)->labels.function_symbols = symbols;
00e52e53 1656 PARSER_EXPLICIT (parser)->label_name = name;
40e084e1
KS
1657 symbols = NULL;
1658 discard_cleanups (cleanup);
1659 }
b4013987
AA
1660 else if (token.type == LSTOKEN_STRING
1661 && *LS_TOKEN_STOKEN (token).ptr == '$')
1662 {
1663 /* User specified a convenience variable or history value. */
00e52e53 1664 PARSER_EXPLICIT (parser)->line_offset
b4013987
AA
1665 = linespec_parse_variable (PARSER_STATE (parser), name);
1666
00e52e53 1667 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
b4013987
AA
1668 {
1669 /* The user-specified variable was not valid. Do not
1670 throw an error here. parse_linespec will do it for us. */
00e52e53 1671 PARSER_EXPLICIT (parser)->function_name = name;
b4013987
AA
1672 discard_cleanups (cleanup);
1673 return;
1674 }
c888a17d
KS
1675
1676 /* The convenience variable/history value parsed correctly.
1677 NAME is no longer needed. */
1678 do_cleanups (cleanup);
b4013987 1679 }
40e084e1
KS
1680 else
1681 {
1682 /* The name is also not a label. Abort parsing. Do not throw
1683 an error here. parse_linespec will do it for us. */
1684
1685 /* Save a copy of the name we were trying to lookup. */
00e52e53 1686 PARSER_EXPLICIT (parser)->function_name = name;
40e084e1
KS
1687 discard_cleanups (cleanup);
1688 return;
1689 }
1690 }
1691
1692 /* Get the next token. */
1693 token = linespec_lexer_consume_token (parser);
1694
1695 if (token.type == LSTOKEN_COLON)
1696 {
1697 /* User specified a label or a lineno. */
1698 token = linespec_lexer_consume_token (parser);
1699
1700 if (token.type == LSTOKEN_NUMBER)
1701 {
1702 /* User specified an offset. Record the line offset and
1703 get the next token. */
1704 name = copy_token_string (token);
1705 cleanup = make_cleanup (xfree, name);
00e52e53 1706 PARSER_EXPLICIT (parser)->line_offset
40e084e1
KS
1707 = linespec_parse_line_offset (name);
1708 do_cleanups (cleanup);
1709
1710 /* Ge the next token. */
1711 token = linespec_lexer_consume_token (parser);
1712 }
1713 else if (token.type == LSTOKEN_STRING)
1714 {
1715 /* Grab a copy of the label's name and look it up. */
1716 name = copy_token_string (token);
1717 cleanup = make_cleanup (xfree, name);
1718 labels = find_label_symbols (PARSER_STATE (parser),
1719 PARSER_RESULT (parser)->function_symbols,
1720 &symbols, name);
1721
1722 if (labels != NULL)
94af9270 1723 {
40e084e1
KS
1724 PARSER_RESULT (parser)->labels.label_symbols = labels;
1725 PARSER_RESULT (parser)->labels.function_symbols = symbols;
00e52e53 1726 PARSER_EXPLICIT (parser)->label_name = name;
40e084e1
KS
1727 symbols = NULL;
1728 discard_cleanups (cleanup);
1729 }
1730 else
1731 {
1732 /* We don't know what it was, but it isn't a label. */
00e52e53
KS
1733 undefined_label_error (PARSER_EXPLICIT (parser)->function_name,
1734 name);
40e084e1
KS
1735 }
1736
1737 /* Check for a line offset. */
1738 token = linespec_lexer_consume_token (parser);
1739 if (token.type == LSTOKEN_COLON)
1740 {
1741 /* Get the next token. */
1742 token = linespec_lexer_consume_token (parser);
1743
1744 /* It must be a line offset. */
1745 if (token.type != LSTOKEN_NUMBER)
1746 unexpected_linespec_error (parser);
1747
1748 /* Record the lione offset and get the next token. */
1749 name = copy_token_string (token);
1750 cleanup = make_cleanup (xfree, name);
1751
00e52e53 1752 PARSER_EXPLICIT (parser)->line_offset
40e084e1
KS
1753 = linespec_parse_line_offset (name);
1754 do_cleanups (cleanup);
1755
1756 /* Get the next token. */
1757 token = linespec_lexer_consume_token (parser);
94af9270
KS
1758 }
1759 }
40e084e1
KS
1760 else
1761 {
1762 /* Trailing ':' in the input. Issue an error. */
1763 unexpected_linespec_error (parser);
1764 }
94af9270 1765 }
40e084e1 1766}
94af9270 1767
40e084e1 1768/* Canonicalize the linespec contained in LS. The result is saved into
00e52e53
KS
1769 STATE->canonical. This function handles both linespec and explicit
1770 locations. */
40e084e1
KS
1771
1772static void
f00aae0f 1773canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
40e084e1 1774{
00e52e53 1775 struct event_location *canon;
67994074 1776 struct explicit_location *explicit_loc;
f00aae0f 1777
40e084e1
KS
1778 /* If canonicalization was not requested, no need to do anything. */
1779 if (!state->canonical)
1780 return;
1781
00e52e53 1782 /* Save everything as an explicit location. */
67994074
KS
1783 canon = state->canonical->location
1784 = new_explicit_location (&ls->explicit_loc);
1785 explicit_loc = get_explicit_location (canon);
40e084e1 1786
67994074 1787 if (explicit_loc->label_name != NULL)
a06efdd6 1788 {
00e52e53 1789 state->canonical->special_display = 1;
40e084e1 1790
67994074 1791 if (explicit_loc->function_name == NULL)
40e084e1 1792 {
a06efdd6
KS
1793 struct symbol *s;
1794
1795 /* No function was specified, so add the symbol name. */
1796 gdb_assert (ls->labels.function_symbols != NULL
1797 && (VEC_length (symbolp, ls->labels.function_symbols)
1798 == 1));
1799 s = VEC_index (symbolp, ls->labels.function_symbols, 0);
67994074 1800 explicit_loc->function_name = xstrdup (SYMBOL_NATURAL_NAME (s));
40e084e1 1801 }
a06efdd6 1802 }
40e084e1 1803
00e52e53
KS
1804 /* If this location originally came from a linespec, save a string
1805 representation of it for display and saving to file. */
1806 if (state->is_linespec)
a06efdd6 1807 {
67994074 1808 char *linespec = explicit_location_to_linespec (explicit_loc);
a06efdd6 1809
00e52e53
KS
1810 set_event_location_string (canon, linespec);
1811 xfree (linespec);
1812 }
94af9270 1813}
c00f8484 1814
40e084e1 1815/* Given a line offset in LS, construct the relevant SALs. */
c00f8484 1816
40e084e1
KS
1817static struct symtabs_and_lines
1818create_sals_line_offset (struct linespec_state *self,
1819 linespec_p ls)
c00f8484 1820{
40e084e1
KS
1821 struct symtabs_and_lines values;
1822 struct symtab_and_line val;
1823 int use_default = 0;
c00f8484 1824
40e084e1
KS
1825 init_sal (&val);
1826 values.sals = NULL;
1827 values.nelts = 0;
1828
1829 /* This is where we need to make sure we have good defaults.
1830 We must guarantee that this section of code is never executed
2e47c6ca 1831 when we are called with just a function name, since
40e084e1
KS
1832 set_default_source_symtab_and_line uses
1833 select_source_symtab that calls us with such an argument. */
1834
ec94af83
DE
1835 if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
1836 && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
3d50dd94 1837 {
05cba821
JK
1838 const char *fullname;
1839
40e084e1 1840 set_current_program_space (self->program_space);
c00f8484 1841
40e084e1
KS
1842 /* Make sure we have at least a default source line. */
1843 set_default_source_symtab_and_line ();
1844 initialize_defaults (&self->default_symtab, &self->default_line);
05cba821 1845 fullname = symtab_to_fullname (self->default_symtab);
ec94af83
DE
1846 VEC_pop (symtab_ptr, ls->file_symtabs);
1847 VEC_free (symtab_ptr, ls->file_symtabs);
c2f4122d
PA
1848 ls->file_symtabs = collect_symtabs_from_filename (fullname,
1849 self->search_pspace);
40e084e1
KS
1850 use_default = 1;
1851 }
c00f8484 1852
67994074
KS
1853 val.line = ls->explicit_loc.line_offset.offset;
1854 switch (ls->explicit_loc.line_offset.sign)
40e084e1
KS
1855 {
1856 case LINE_OFFSET_PLUS:
67994074 1857 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
1858 val.line = 5;
1859 if (use_default)
1860 val.line = self->default_line + val.line;
1861 break;
1862
1863 case LINE_OFFSET_MINUS:
67994074 1864 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
1865 val.line = 15;
1866 if (use_default)
1867 val.line = self->default_line - val.line;
1868 else
1869 val.line = -val.line;
1870 break;
1871
1872 case LINE_OFFSET_NONE:
1873 break; /* No need to adjust val.line. */
1874 }
1875
1876 if (self->list_mode)
1877 decode_digits_list_mode (self, ls, &values, val);
1878 else
1879 {
1880 struct linetable_entry *best_entry = NULL;
1881 int *filter;
3977b71f 1882 const struct block **blocks;
40e084e1
KS
1883 struct cleanup *cleanup;
1884 struct symtabs_and_lines intermediate_results;
1885 int i, j;
1886
1887 intermediate_results.sals = NULL;
1888 intermediate_results.nelts = 0;
1889
1890 decode_digits_ordinary (self, ls, val.line, &intermediate_results,
1891 &best_entry);
1892 if (intermediate_results.nelts == 0 && best_entry != NULL)
1893 decode_digits_ordinary (self, ls, best_entry->line,
1894 &intermediate_results, &best_entry);
1895
1896 cleanup = make_cleanup (xfree, intermediate_results.sals);
1897
1898 /* For optimized code, the compiler can scatter one source line
1899 across disjoint ranges of PC values, even when no duplicate
1900 functions or inline functions are involved. For example,
1901 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
1902 function can result in two PC ranges. In this case, we don't
1903 want to set a breakpoint on the first PC of each range. To filter
1904 such cases, we use containing blocks -- for each PC found
1905 above, we see if there are other PCs that are in the same
1906 block. If yes, the other PCs are filtered out. */
1907
1908 filter = XNEWVEC (int, intermediate_results.nelts);
1909 make_cleanup (xfree, filter);
3977b71f 1910 blocks = XNEWVEC (const struct block *, intermediate_results.nelts);
40e084e1
KS
1911 make_cleanup (xfree, blocks);
1912
1913 for (i = 0; i < intermediate_results.nelts; ++i)
3d50dd94 1914 {
40e084e1 1915 set_current_program_space (intermediate_results.sals[i].pspace);
c00f8484 1916
40e084e1
KS
1917 filter[i] = 1;
1918 blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
1919 intermediate_results.sals[i].section);
3d50dd94 1920 }
c00f8484 1921
40e084e1
KS
1922 for (i = 0; i < intermediate_results.nelts; ++i)
1923 {
1924 if (blocks[i] != NULL)
1925 for (j = i + 1; j < intermediate_results.nelts; ++j)
1926 {
1927 if (blocks[j] == blocks[i])
1928 {
1929 filter[j] = 0;
1930 break;
1931 }
1932 }
1933 }
c00f8484 1934
40e084e1
KS
1935 for (i = 0; i < intermediate_results.nelts; ++i)
1936 if (filter[i])
1937 {
1938 struct symbol *sym = (blocks[i]
1939 ? block_containing_function (blocks[i])
1940 : NULL);
3d50dd94 1941
40e084e1
KS
1942 if (self->funfirstline)
1943 skip_prologue_sal (&intermediate_results.sals[i]);
1944 /* Make sure the line matches the request, not what was
1945 found. */
1946 intermediate_results.sals[i].line = val.line;
1947 add_sal_to_sals (self, &values, &intermediate_results.sals[i],
66f1999b 1948 sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
40e084e1 1949 }
3d50dd94 1950
40e084e1 1951 do_cleanups (cleanup);
f17170e5 1952 }
c00f8484 1953
40e084e1
KS
1954 if (values.nelts == 0)
1955 {
67994074 1956 if (ls->explicit_loc.source_filename)
40e084e1 1957 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
67994074 1958 val.line, ls->explicit_loc.source_filename);
40e084e1
KS
1959 else
1960 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
1961 val.line);
1962 }
3d50dd94 1963
40e084e1 1964 return values;
c00f8484
KS
1965}
1966
a06efdd6
KS
1967/* Convert the given ADDRESS into SaLs. */
1968
1969static struct symtabs_and_lines
1970convert_address_location_to_sals (struct linespec_state *self,
1971 CORE_ADDR address)
1972{
1973 struct symtab_and_line sal;
1974 struct symtabs_and_lines sals = {NULL, 0};
1975
1976 sal = find_pc_line (address, 0);
1977 sal.pc = address;
1978 sal.section = find_pc_overlay (address);
1979 sal.explicit_pc = 1;
1980 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
1981
1982 return sals;
1983}
1984
40e084e1
KS
1985/* Create and return SALs from the linespec LS. */
1986
1987static struct symtabs_and_lines
1988convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
1989{
1990 struct symtabs_and_lines sals = {NULL, 0};
1991
a06efdd6 1992 if (ls->labels.label_symbols != NULL)
40e084e1
KS
1993 {
1994 /* We have just a bunch of functions/methods or labels. */
1995 int i;
1996 struct symtab_and_line sal;
1997 struct symbol *sym;
1998
1999 for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
2000 {
08be3fe3 2001 struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));
fdbb204b
TT
2002
2003 if (symbol_to_sal (&sal, state->funfirstline, sym)
2004 && maybe_add_address (state->addr_set, pspace, sal.pc))
64b92e45
KS
2005 add_sal_to_sals (state, &sals, &sal,
2006 SYMBOL_NATURAL_NAME (sym), 0);
40e084e1
KS
2007 }
2008 }
2009 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2010 {
2011 /* We have just a bunch of functions and/or methods. */
2012 int i;
2013 struct symtab_and_line sal;
2014 struct symbol *sym;
f60e2d5c 2015 bound_minimal_symbol_d *elem;
40e084e1
KS
2016 struct program_space *pspace;
2017
2018 if (ls->function_symbols != NULL)
2019 {
2020 /* Sort symbols so that symbols with the same program space are next
2021 to each other. */
2022 qsort (VEC_address (symbolp, ls->function_symbols),
2023 VEC_length (symbolp, ls->function_symbols),
2024 sizeof (symbolp), compare_symbols);
2025
2026 for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
2027 {
08be3fe3 2028 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
40e084e1 2029 set_current_program_space (pspace);
64b92e45
KS
2030 if (symbol_to_sal (&sal, state->funfirstline, sym)
2031 && maybe_add_address (state->addr_set, pspace, sal.pc))
66f1999b
KS
2032 add_sal_to_sals (state, &sals, &sal,
2033 SYMBOL_NATURAL_NAME (sym), 0);
40e084e1
KS
2034 }
2035 }
2036
2037 if (ls->minimal_symbols != NULL)
2038 {
2039 /* Sort minimal symbols by program space, too. */
f60e2d5c
TT
2040 qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
2041 VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
2042 sizeof (bound_minimal_symbol_d), compare_msymbols);
40e084e1
KS
2043
2044 for (i = 0;
f60e2d5c
TT
2045 VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
2046 i, elem);
40e084e1
KS
2047 ++i)
2048 {
001822aa 2049 pspace = elem->objfile->pspace;
40e084e1
KS
2050 set_current_program_space (pspace);
2051 minsym_found (state, elem->objfile, elem->minsym, &sals);
2052 }
2053 }
2054 }
67994074 2055 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1
KS
2056 {
2057 /* Only an offset was specified. */
2058 sals = create_sals_line_offset (state, ls);
2059
2060 /* Make sure we have a filename for canonicalization. */
67994074 2061 if (ls->explicit_loc.source_filename == NULL)
05cba821
JK
2062 {
2063 const char *fullname = symtab_to_fullname (state->default_symtab);
2064
e93ba630
JK
2065 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2066 form so that displaying SOURCE_FILENAME can follow the current
2067 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2068 it has been kept for code simplicity only in absolute form. */
67994074 2069 ls->explicit_loc.source_filename = xstrdup (fullname);
05cba821 2070 }
40e084e1
KS
2071 }
2072 else
2073 {
2074 /* We haven't found any results... */
2075 return sals;
2076 }
2077
2078 canonicalize_linespec (state, ls);
2079
2080 if (sals.nelts > 0 && state->canonical != NULL)
2081 state->canonical->pre_expanded = 1;
2082
2083 return sals;
2084}
50641945 2085
67994074 2086/* Convert the explicit location EXPLICIT_LOC into SaLs. */
00e52e53
KS
2087
2088static struct symtabs_and_lines
2089convert_explicit_location_to_sals (struct linespec_state *self,
2090 linespec_p result,
67994074 2091 const struct explicit_location *explicit_loc)
00e52e53
KS
2092{
2093 VEC (symbolp) *symbols, *labels;
2094 VEC (bound_minimal_symbol_d) *minimal_symbols;
2095
67994074 2096 if (explicit_loc->source_filename != NULL)
00e52e53
KS
2097 {
2098 TRY
2099 {
2100 result->file_symtabs
c2f4122d
PA
2101 = symtabs_from_filename (explicit_loc->source_filename,
2102 self->search_pspace);
00e52e53
KS
2103 }
2104 CATCH (except, RETURN_MASK_ERROR)
2105 {
67994074 2106 source_file_not_found_error (explicit_loc->source_filename);
00e52e53
KS
2107 }
2108 END_CATCH
67994074
KS
2109 result->explicit_loc.source_filename
2110 = xstrdup (explicit_loc->source_filename);
00e52e53
KS
2111 }
2112 else
2113 {
2114 /* A NULL entry means to use the default symtab. */
2115 VEC_safe_push (symtab_ptr, result->file_symtabs, NULL);
2116 }
2117
67994074 2118 if (explicit_loc->function_name != NULL)
00e52e53
KS
2119 {
2120 find_linespec_symbols (self, result->file_symtabs,
67994074 2121 explicit_loc->function_name, &symbols,
00e52e53
KS
2122 &minimal_symbols);
2123
2124 if (symbols == NULL && minimal_symbols == NULL)
67994074
KS
2125 symbol_not_found_error (explicit_loc->function_name,
2126 result->explicit_loc.source_filename);
00e52e53 2127
67994074
KS
2128 result->explicit_loc.function_name
2129 = xstrdup (explicit_loc->function_name);
00e52e53
KS
2130 result->function_symbols = symbols;
2131 result->minimal_symbols = minimal_symbols;
2132 }
2133
67994074 2134 if (explicit_loc->label_name != NULL)
00e52e53
KS
2135 {
2136 symbols = NULL;
2137 labels = find_label_symbols (self, result->function_symbols,
67994074 2138 &symbols, explicit_loc->label_name);
00e52e53
KS
2139
2140 if (labels == NULL)
67994074
KS
2141 undefined_label_error (result->explicit_loc.function_name,
2142 explicit_loc->label_name);
00e52e53 2143
67994074 2144 result->explicit_loc.label_name = xstrdup (explicit_loc->label_name);
00e52e53
KS
2145 result->labels.label_symbols = labels;
2146 result->labels.function_symbols = symbols;
2147 }
2148
67994074
KS
2149 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
2150 result->explicit_loc.line_offset = explicit_loc->line_offset;
00e52e53
KS
2151
2152 return convert_linespec_to_sals (self, result);
2153}
2154
40e084e1 2155/* Parse a string that specifies a linespec.
50641945 2156
40e084e1 2157 The basic grammar of linespecs:
50641945 2158
a06efdd6 2159 linespec -> var_spec | basic_spec
40e084e1 2160 var_spec -> '$' (STRING | NUMBER)
50641945 2161
40e084e1
KS
2162 basic_spec -> file_offset_spec | function_spec | label_spec
2163 file_offset_spec -> opt_file_spec offset_spec
2164 function_spec -> opt_file_spec function_name_spec opt_label_spec
2165 label_spec -> label_name_spec
50641945 2166
40e084e1
KS
2167 opt_file_spec -> "" | file_name_spec ':'
2168 opt_label_spec -> "" | ':' label_name_spec
2169
2170 file_name_spec -> STRING
2171 function_name_spec -> STRING
2172 label_name_spec -> STRING
2173 function_name_spec -> STRING
2174 offset_spec -> NUMBER
2175 -> '+' NUMBER
2176 -> '-' NUMBER
2177
2178 This may all be followed by several keywords such as "if EXPR",
2179 which we ignore.
2180
2181 A comma will terminate parsing.
2182
2183 The function may be an undebuggable function found in minimal symbol table.
50641945
FN
2184
2185 If the argument FUNFIRSTLINE is nonzero, we want the first line
2186 of real code inside a function when a function is specified, and it is
2187 not OK to specify a variable or type to get its line number.
2188
2189 DEFAULT_SYMTAB specifies the file to use if none is specified.
2190 It defaults to current_source_symtab.
2191 DEFAULT_LINE specifies the line number to use for relative
2192 line numbers (that start with signs). Defaults to current_source_line.
2193 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 2194 line specs there if necessary. Currently overloaded member functions and
50641945 2195 line numbers or static functions without a filename yield a canonical
1777feb0 2196 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
2197 it is the callers responsibility to free them.
2198
2199 Note that it is possible to return zero for the symtab
2200 if no file is validly specified. Callers must check that.
58438ac1 2201 Also, the line number returned may be invalid. */
50641945 2202
f00aae0f 2203/* Parse the linespec in ARG. */
50641945 2204
ad32032e 2205static struct symtabs_and_lines
f00aae0f 2206parse_linespec (linespec_parser *parser, const char *arg)
50641945 2207{
40e084e1
KS
2208 linespec_token token;
2209 struct symtabs_and_lines values;
7556d4a4 2210 struct gdb_exception file_exception = exception_none;
40e084e1
KS
2211 struct cleanup *cleanup;
2212
2213 /* A special case to start. It has become quite popular for
2214 IDEs to work around bugs in the previous parser by quoting
2215 the entire linespec, so we attempt to deal with this nicely. */
2216 parser->is_quote_enclosed = 0;
f00aae0f
KS
2217 if (!is_ada_operator (arg)
2218 && strchr (linespec_quote_characters, *arg) != NULL)
40e084e1
KS
2219 {
2220 const char *end;
9ef07c8c 2221
f00aae0f 2222 end = skip_quote_char (arg + 1, *arg);
40e084e1 2223 if (end != NULL && is_closing_quote_enclosed (end))
136e1c30 2224 {
f00aae0f 2225 /* Here's the special case. Skip ARG past the initial
40e084e1 2226 quote. */
f00aae0f 2227 ++arg;
40e084e1 2228 parser->is_quote_enclosed = 1;
136e1c30
DE
2229 }
2230 }
e8eb7bc5 2231
f00aae0f
KS
2232 parser->lexer.saved_arg = arg;
2233 parser->lexer.stream = arg;
d2630e69 2234
40e084e1
KS
2235 /* Initialize the default symtab and line offset. */
2236 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2237 &PARSER_STATE (parser)->default_line);
d2630e69 2238
40e084e1 2239 /* Objective-C shortcut. */
f00aae0f 2240 values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
40e084e1
KS
2241 if (values.sals != NULL)
2242 return values;
e0881a8e 2243
40e084e1 2244 /* Start parsing. */
d2630e69 2245
40e084e1
KS
2246 /* Get the first token. */
2247 token = linespec_lexer_lex_one (parser);
50641945 2248
40e084e1 2249 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
a06efdd6 2250 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
40e084e1
KS
2251 {
2252 char *var;
50641945 2253
40e084e1 2254 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2255 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
dcf9f4ab 2256
40e084e1
KS
2257 /* User specified a convenience variable or history value. */
2258 var = copy_token_string (token);
2259 cleanup = make_cleanup (xfree, var);
00e52e53 2260 PARSER_EXPLICIT (parser)->line_offset
40e084e1 2261 = linespec_parse_variable (PARSER_STATE (parser), var);
cf4ded82 2262 do_cleanups (cleanup);
f8eba3c6 2263
40e084e1
KS
2264 /* If a line_offset wasn't found (VAR is the name of a user
2265 variable/function), then skip to normal symbol processing. */
00e52e53 2266 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1 2267 {
40e084e1
KS
2268 /* Consume this token. */
2269 linespec_lexer_consume_token (parser);
dcf9f4ab 2270
40e084e1 2271 goto convert_to_sals;
50641945 2272 }
40e084e1
KS
2273 }
2274 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
2275 unexpected_linespec_error (parser);
50641945 2276
40e084e1
KS
2277 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2278 this token cannot represent a filename. */
2279 token = linespec_lexer_peek_token (parser);
0e0b460e 2280
40e084e1 2281 if (token.type == LSTOKEN_COLON)
0e0b460e 2282 {
40e084e1 2283 char *user_filename;
0e0b460e 2284
40e084e1
KS
2285 /* Get the current token again and extract the filename. */
2286 token = linespec_lexer_lex_one (parser);
2287 user_filename = copy_token_string (token);
50641945 2288
40e084e1 2289 /* Check if the input is a filename. */
492d29ea 2290 TRY
40e084e1
KS
2291 {
2292 PARSER_RESULT (parser)->file_symtabs
c2f4122d
PA
2293 = symtabs_from_filename (user_filename,
2294 PARSER_STATE (parser)->search_pspace);
40e084e1 2295 }
492d29ea 2296 CATCH (ex, RETURN_MASK_ERROR)
7556d4a4
PA
2297 {
2298 file_exception = ex;
2299 }
492d29ea 2300 END_CATCH
50641945 2301
40e084e1
KS
2302 if (file_exception.reason >= 0)
2303 {
2304 /* Symtabs were found for the file. Record the filename. */
00e52e53 2305 PARSER_EXPLICIT (parser)->source_filename = user_filename;
f8eba3c6 2306
40e084e1
KS
2307 /* Get the next token. */
2308 token = linespec_lexer_consume_token (parser);
50641945 2309
40e084e1
KS
2310 /* This is LSTOKEN_COLON; consume it. */
2311 linespec_lexer_consume_token (parser);
2312 }
2313 else
2314 {
2315 /* No symtabs found -- discard user_filename. */
2316 xfree (user_filename);
50641945 2317
40e084e1 2318 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2319 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
40e084e1 2320 }
50641945 2321 }
40e084e1
KS
2322 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2323 else if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2324 && token.type != LSTOKEN_COMMA)
d2630e69 2325 {
40e084e1
KS
2326 /* TOKEN is the _next_ token, not the one currently in the parser.
2327 Consuming the token will give the correct error message. */
2328 linespec_lexer_consume_token (parser);
2329 unexpected_linespec_error (parser);
d2630e69 2330 }
50641945
FN
2331 else
2332 {
40e084e1 2333 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2334 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
50641945 2335 }
50641945 2336
40e084e1
KS
2337 /* Parse the rest of the linespec. */
2338 linespec_parse_basic (parser);
50641945 2339
40e084e1
KS
2340 if (PARSER_RESULT (parser)->function_symbols == NULL
2341 && PARSER_RESULT (parser)->labels.label_symbols == NULL
00e52e53 2342 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
40e084e1 2343 && PARSER_RESULT (parser)->minimal_symbols == NULL)
f8eba3c6 2344 {
40e084e1
KS
2345 /* The linespec didn't parse. Re-throw the file exception if
2346 there was one. */
2347 if (file_exception.reason < 0)
2348 throw_exception (file_exception);
0f5238ed 2349
40e084e1 2350 /* Otherwise, the symbol is not found. */
00e52e53
KS
2351 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2352 PARSER_EXPLICIT (parser)->source_filename);
0f5238ed
TT
2353 }
2354
40e084e1 2355 convert_to_sals:
9ef07c8c 2356
40e084e1
KS
2357 /* Get the last token and record how much of the input was parsed,
2358 if necessary. */
2359 token = linespec_lexer_lex_one (parser);
2360 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
2361 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (token).ptr;
50641945 2362
40e084e1
KS
2363 /* Convert the data in PARSER_RESULT to SALs. */
2364 values = convert_linespec_to_sals (PARSER_STATE (parser),
2365 PARSER_RESULT (parser));
f8eba3c6 2366
40e084e1 2367 return values;
413dad4d 2368}
50641945 2369
40e084e1 2370
f8eba3c6 2371/* A constructor for linespec_state. */
44fe14ab 2372
f8eba3c6
TT
2373static void
2374linespec_state_constructor (struct linespec_state *self,
40e084e1 2375 int flags, const struct language_defn *language,
c2f4122d 2376 struct program_space *search_pspace,
f8eba3c6
TT
2377 struct symtab *default_symtab,
2378 int default_line,
2379 struct linespec_result *canonical)
2380{
2381 memset (self, 0, sizeof (*self));
40e084e1 2382 self->language = language;
f8eba3c6
TT
2383 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2384 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
c2f4122d 2385 self->search_pspace = search_pspace;
f8eba3c6
TT
2386 self->default_symtab = default_symtab;
2387 self->default_line = default_line;
2388 self->canonical = canonical;
2389 self->program_space = current_program_space;
2390 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2391 xfree, xcalloc, xfree);
00e52e53 2392 self->is_linespec = 0;
f8eba3c6 2393}
44fe14ab 2394
40e084e1 2395/* Initialize a new linespec parser. */
44fe14ab
DC
2396
2397static void
40e084e1
KS
2398linespec_parser_new (linespec_parser *parser,
2399 int flags, const struct language_defn *language,
c2f4122d 2400 struct program_space *search_pspace,
40e084e1
KS
2401 struct symtab *default_symtab,
2402 int default_line,
2403 struct linespec_result *canonical)
44fe14ab 2404{
f00aae0f 2405 memset (parser, 0, sizeof (linespec_parser));
40e084e1
KS
2406 parser->lexer.current.type = LSTOKEN_CONSUMED;
2407 memset (PARSER_RESULT (parser), 0, sizeof (struct linespec));
00e52e53 2408 PARSER_EXPLICIT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN;
40e084e1 2409 linespec_state_constructor (PARSER_STATE (parser), flags, language,
c2f4122d 2410 search_pspace,
40e084e1
KS
2411 default_symtab, default_line, canonical);
2412}
2413
2414/* A destructor for linespec_state. */
44fe14ab 2415
40e084e1
KS
2416static void
2417linespec_state_destructor (struct linespec_state *self)
2418{
f8eba3c6
TT
2419 htab_delete (self->addr_set);
2420}
44fe14ab 2421
40e084e1
KS
2422/* Delete a linespec parser. */
2423
2424static void
2425linespec_parser_delete (void *arg)
2426{
2427 linespec_parser *parser = (linespec_parser *) arg;
2428
00e52e53
KS
2429 xfree (PARSER_EXPLICIT (parser)->source_filename);
2430 xfree (PARSER_EXPLICIT (parser)->label_name);
2431 xfree (PARSER_EXPLICIT (parser)->function_name);
40e084e1
KS
2432
2433 if (PARSER_RESULT (parser)->file_symtabs != NULL)
ec94af83 2434 VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);
40e084e1
KS
2435
2436 if (PARSER_RESULT (parser)->function_symbols != NULL)
2437 VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);
2438
2439 if (PARSER_RESULT (parser)->minimal_symbols != NULL)
f60e2d5c 2440 VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);
40e084e1
KS
2441
2442 if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
2443 VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);
2444
2445 if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
2446 VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);
2447
2448 linespec_state_destructor (PARSER_STATE (parser));
2449}
2450
c7c1b3e9
KS
2451/* See description in linespec.h. */
2452
2453void
2454linespec_lex_to_end (char **stringp)
2455{
2456 linespec_parser parser;
2457 struct cleanup *cleanup;
2458 linespec_token token;
c7c1b3e9
KS
2459 const char *orig;
2460
2461 if (stringp == NULL || *stringp == NULL)
2462 return;
2463
c2f4122d 2464 linespec_parser_new (&parser, 0, current_language, NULL, NULL, 0, NULL);
c7c1b3e9
KS
2465 cleanup = make_cleanup (linespec_parser_delete, &parser);
2466 parser.lexer.saved_arg = *stringp;
2467 PARSER_STREAM (&parser) = orig = *stringp;
2468
2469 do
2470 {
2471 /* Stop before any comma tokens; we need it to keep it
2472 as the next token in the string. */
2473 token = linespec_lexer_peek_token (&parser);
2474 if (token.type == LSTOKEN_COMMA)
2475 break;
c7c1b3e9
KS
2476 token = linespec_lexer_consume_token (&parser);
2477 }
2478 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2479
2480 *stringp += PARSER_STREAM (&parser) - orig;
2481 do_cleanups (cleanup);
2482}
2483
f00aae0f
KS
2484/* A helper function for decode_line_full and decode_line_1 to
2485 turn LOCATION into symtabs_and_lines. */
2486
2487static struct symtabs_and_lines
2488event_location_to_sals (linespec_parser *parser,
2489 const struct event_location *location)
2490{
2491 struct symtabs_and_lines result = {NULL, 0};
2492
2493 switch (event_location_type (location))
2494 {
2495 case LINESPEC_LOCATION:
2496 {
00e52e53 2497 PARSER_STATE (parser)->is_linespec = 1;
f00aae0f
KS
2498 TRY
2499 {
2500 result = parse_linespec (parser, get_linespec_location (location));
2501 }
2502 CATCH (except, RETURN_MASK_ERROR)
2503 {
2504 throw_exception (except);
2505 }
2506 END_CATCH
2507 }
2508 break;
2509
a06efdd6 2510 case ADDRESS_LOCATION:
305e13e6
JB
2511 {
2512 const char *addr_string = get_address_string_location (location);
2513 CORE_ADDR addr = get_address_location (location);
2514
2515 if (addr_string != NULL)
2516 {
2517 char *expr = xstrdup (addr_string);
2518 const char *const_expr = expr;
2519 struct cleanup *cleanup = make_cleanup (xfree, expr);
2520
2521 addr = linespec_expression_to_pc (&const_expr);
2522 if (PARSER_STATE (parser)->canonical != NULL)
2523 PARSER_STATE (parser)->canonical->location
2524 = copy_event_location (location);
2525
2526 do_cleanups (cleanup);
2527 }
2528
2529 result = convert_address_location_to_sals (PARSER_STATE (parser),
2530 addr);
2531 }
a06efdd6
KS
2532 break;
2533
00e52e53
KS
2534 case EXPLICIT_LOCATION:
2535 {
67994074 2536 const struct explicit_location *explicit_loc;
00e52e53 2537
67994074 2538 explicit_loc = get_explicit_location_const (location);
00e52e53
KS
2539 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
2540 PARSER_RESULT (parser),
67994074 2541 explicit_loc);
00e52e53
KS
2542 }
2543 break;
2544
5b56227b
KS
2545 case PROBE_LOCATION:
2546 /* Probes are handled by their own decoders. */
2547 gdb_assert_not_reached ("attempt to decode probe location");
2548 break;
2549
f00aae0f
KS
2550 default:
2551 gdb_assert_not_reached ("unhandled event location type");
2552 }
2553
2554 return result;
2555}
2556
f8eba3c6 2557/* See linespec.h. */
44fe14ab 2558
f8eba3c6 2559void
f00aae0f 2560decode_line_full (const struct event_location *location, int flags,
c2f4122d 2561 struct program_space *search_pspace,
f8eba3c6
TT
2562 struct symtab *default_symtab,
2563 int default_line, struct linespec_result *canonical,
2564 const char *select_mode,
2565 const char *filter)
44fe14ab 2566{
f8eba3c6 2567 struct symtabs_and_lines result;
f8eba3c6 2568 struct cleanup *cleanups;
f8eba3c6 2569 VEC (const_char_ptr) *filters = NULL;
40e084e1
KS
2570 linespec_parser parser;
2571 struct linespec_state *state;
f8eba3c6
TT
2572
2573 gdb_assert (canonical != NULL);
2574 /* The filter only makes sense for 'all'. */
2575 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
2576 gdb_assert (select_mode == NULL
2577 || select_mode == multiple_symbols_all
2578 || select_mode == multiple_symbols_ask
2579 || select_mode == multiple_symbols_cancel);
2580 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
2581
c2f4122d
PA
2582 linespec_parser_new (&parser, flags, current_language,
2583 search_pspace, default_symtab,
40e084e1
KS
2584 default_line, canonical);
2585 cleanups = make_cleanup (linespec_parser_delete, &parser);
f8eba3c6
TT
2586 save_current_program_space ();
2587
f00aae0f 2588 result = event_location_to_sals (&parser, location);
40e084e1 2589 state = PARSER_STATE (&parser);
f8eba3c6
TT
2590
2591 gdb_assert (result.nelts == 1 || canonical->pre_expanded);
f8eba3c6
TT
2592 canonical->pre_expanded = 1;
2593
66f1999b 2594 /* Arrange for allocated canonical names to be freed. */
f8eba3c6
TT
2595 if (result.nelts > 0)
2596 {
2597 int i;
2598
40e084e1 2599 make_cleanup (xfree, state->canonical_names);
f8eba3c6
TT
2600 for (i = 0; i < result.nelts; ++i)
2601 {
33f448b1
JK
2602 gdb_assert (state->canonical_names[i].suffix != NULL);
2603 make_cleanup (xfree, state->canonical_names[i].suffix);
f8eba3c6
TT
2604 }
2605 }
2606
2607 if (select_mode == NULL)
2608 {
112e8700 2609 if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ())
f8eba3c6
TT
2610 select_mode = multiple_symbols_all;
2611 else
2612 select_mode = multiple_symbols_select_mode ();
2613 }
2614
2615 if (select_mode == multiple_symbols_all)
2616 {
2617 if (filter != NULL)
2618 {
2619 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
2620 VEC_safe_push (const_char_ptr, filters, filter);
40e084e1 2621 filter_results (state, &result, filters);
f8eba3c6
TT
2622 }
2623 else
40e084e1 2624 convert_results_to_lsals (state, &result);
f8eba3c6
TT
2625 }
2626 else
40e084e1 2627 decode_line_2 (state, &result, select_mode);
f8eba3c6
TT
2628
2629 do_cleanups (cleanups);
2630}
2631
39cf75f7
DE
2632/* See linespec.h. */
2633
f8eba3c6 2634struct symtabs_and_lines
f00aae0f 2635decode_line_1 (const struct event_location *location, int flags,
c2f4122d 2636 struct program_space *search_pspace,
f8eba3c6
TT
2637 struct symtab *default_symtab,
2638 int default_line)
2639{
2640 struct symtabs_and_lines result;
40e084e1 2641 linespec_parser parser;
f8eba3c6
TT
2642 struct cleanup *cleanups;
2643
c2f4122d
PA
2644 linespec_parser_new (&parser, flags, current_language,
2645 search_pspace, default_symtab,
40e084e1
KS
2646 default_line, NULL);
2647 cleanups = make_cleanup (linespec_parser_delete, &parser);
f8eba3c6
TT
2648 save_current_program_space ();
2649
f00aae0f 2650 result = event_location_to_sals (&parser, location);
40e084e1 2651
f8eba3c6
TT
2652 do_cleanups (cleanups);
2653 return result;
2654}
2655
39cf75f7
DE
2656/* See linespec.h. */
2657
2658struct symtabs_and_lines
2659decode_line_with_current_source (char *string, int flags)
2660{
2661 struct symtabs_and_lines sals;
2662 struct symtab_and_line cursal;
f00aae0f
KS
2663 struct event_location *location;
2664 struct cleanup *cleanup;
39cf75f7
DE
2665
2666 if (string == 0)
2667 error (_("Empty line specification."));
2668
2669 /* We use whatever is set as the current source line. We do not try
2670 and get a default source symtab+line or it will recursively call us! */
2671 cursal = get_current_source_symtab_and_line ();
2672
f00aae0f
KS
2673 location = string_to_event_location (&string, current_language);
2674 cleanup = make_cleanup_delete_event_location (location);
c2f4122d 2675 sals = decode_line_1 (location, flags, NULL,
39cf75f7
DE
2676 cursal.symtab, cursal.line);
2677
2678 if (*string)
2679 error (_("Junk at end of line specification: %s"), string);
f00aae0f
KS
2680
2681 do_cleanups (cleanup);
39cf75f7
DE
2682 return sals;
2683}
2684
2685/* See linespec.h. */
2686
2687struct symtabs_and_lines
2688decode_line_with_last_displayed (char *string, int flags)
2689{
2690 struct symtabs_and_lines sals;
f00aae0f
KS
2691 struct event_location *location;
2692 struct cleanup *cleanup;
39cf75f7
DE
2693
2694 if (string == 0)
2695 error (_("Empty line specification."));
2696
f00aae0f
KS
2697 location = string_to_event_location (&string, current_language);
2698 cleanup = make_cleanup_delete_event_location (location);
39cf75f7 2699 if (last_displayed_sal_is_valid ())
c2f4122d 2700 sals = decode_line_1 (location, flags, NULL,
39cf75f7
DE
2701 get_last_displayed_symtab (),
2702 get_last_displayed_line ());
2703 else
c2f4122d 2704 sals = decode_line_1 (location, flags, NULL, (struct symtab *) NULL, 0);
39cf75f7
DE
2705
2706 if (*string)
2707 error (_("Junk at end of line specification: %s"), string);
f00aae0f
KS
2708
2709 do_cleanups (cleanup);
39cf75f7
DE
2710 return sals;
2711}
2712
f8eba3c6
TT
2713\f
2714
2715/* First, some functions to initialize stuff at the beggining of the
2716 function. */
2717
2718static void
2719initialize_defaults (struct symtab **default_symtab, int *default_line)
2720{
2721 if (*default_symtab == 0)
2722 {
2723 /* Use whatever we have for the default source line. We don't use
2724 get_current_or_default_symtab_and_line as it can recurse and call
2725 us back! */
2726 struct symtab_and_line cursal =
2727 get_current_source_symtab_and_line ();
2728
2729 *default_symtab = cursal.symtab;
2730 *default_line = cursal.line;
2731 }
2732}
2733
2734\f
2735
40e084e1
KS
2736/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2737 advancing EXP_PTR past any parsed text. */
f8eba3c6 2738
a06efdd6 2739CORE_ADDR
bbc13ae3 2740linespec_expression_to_pc (const char **exp_ptr)
f8eba3c6 2741{
f8eba3c6
TT
2742 if (current_program_space->executing_startup)
2743 /* The error message doesn't really matter, because this case
2744 should only hit during breakpoint reset. */
2745 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
2746 "program space is in startup"));
2747
40e084e1
KS
2748 (*exp_ptr)++;
2749 return value_as_address (parse_to_comma_and_eval (exp_ptr));
0960f083
DC
2750}
2751
2752\f
2753
d2630e69
AF
2754/* Here's where we recognise an Objective-C Selector. An Objective C
2755 selector may be implemented by more than one class, therefore it
2756 may represent more than one method/function. This gives us a
2757 situation somewhat analogous to C++ overloading. If there's more
2758 than one method that could represent the selector, then use some of
2759 the existing C++ code to let the user choose one. */
2760
f8eba3c6 2761static struct symtabs_and_lines
f00aae0f 2762decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
d2630e69 2763{
f8eba3c6
TT
2764 struct collect_info info;
2765 VEC (const_char_ptr) *symbol_names = NULL;
40e084e1 2766 struct symtabs_and_lines values;
d7561cbb 2767 const char *new_argptr;
f8eba3c6
TT
2768 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
2769 &symbol_names);
2770
2771 info.state = self;
40e084e1 2772 info.file_symtabs = NULL;
ec94af83
DE
2773 VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
2774 make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
40e084e1
KS
2775 info.result.symbols = NULL;
2776 info.result.minimal_symbols = NULL;
2777 values.nelts = 0;
2778 values.sals = NULL;
f8eba3c6 2779
f00aae0f 2780 new_argptr = find_imps (arg, &symbol_names);
f8eba3c6
TT
2781 if (VEC_empty (const_char_ptr, symbol_names))
2782 {
2783 do_cleanups (cleanup);
40e084e1 2784 return values;
f8eba3c6 2785 }
d2630e69 2786
f8eba3c6 2787 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
d2630e69 2788
40e084e1 2789 if (!VEC_empty (symbolp, info.result.symbols)
f60e2d5c 2790 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
d2630e69 2791 {
f8eba3c6 2792 char *saved_arg;
d2630e69 2793
224c3ddb 2794 saved_arg = (char *) alloca (new_argptr - arg + 1);
f00aae0f
KS
2795 memcpy (saved_arg, arg, new_argptr - arg);
2796 saved_arg[new_argptr - arg] = '\0';
d2630e69 2797
67994074 2798 ls->explicit_loc.function_name = xstrdup (saved_arg);
40e084e1
KS
2799 ls->function_symbols = info.result.symbols;
2800 ls->minimal_symbols = info.result.minimal_symbols;
2801 values = convert_linespec_to_sals (self, ls);
2802
f8eba3c6 2803 if (self->canonical)
d2630e69 2804 {
f00aae0f
KS
2805 char *str;
2806
f8eba3c6 2807 self->canonical->pre_expanded = 1;
f00aae0f 2808
67994074 2809 if (ls->explicit_loc.source_filename)
f00aae0f
KS
2810 {
2811 str = xstrprintf ("%s:%s",
67994074 2812 ls->explicit_loc.source_filename, saved_arg);
f00aae0f 2813 }
f8eba3c6 2814 else
f00aae0f
KS
2815 str = xstrdup (saved_arg);
2816
2817 make_cleanup (xfree, str);
2818 self->canonical->location = new_linespec_location (&str);
d2630e69 2819 }
d2630e69
AF
2820 }
2821
f8eba3c6 2822 do_cleanups (cleanup);
c00f8484 2823
40e084e1 2824 return values;
f8eba3c6 2825}
c00f8484 2826
f8eba3c6
TT
2827/* An instance of this type is used when collecting prefix symbols for
2828 decode_compound. */
17763fd9 2829
f8eba3c6
TT
2830struct decode_compound_collector
2831{
2832 /* The result vector. */
2833 VEC (symbolp) *symbols;
3a93a0c2 2834
f8eba3c6
TT
2835 /* A hash table of all symbols we found. We use this to avoid
2836 adding any symbol more than once. */
2837 htab_t unique_syms;
fc4007c9
TT
2838
2839 decode_compound_collector ()
2840 : symbols (NULL),
2841 unique_syms (NULL)
2842 {
2843 }
2844
2845 ~decode_compound_collector ()
2846 {
2847 if (unique_syms != NULL)
2848 htab_delete (unique_syms);
2849 }
f8eba3c6 2850};
3a93a0c2 2851
f8eba3c6
TT
2852/* A callback for iterate_over_symbols that is used by
2853 lookup_prefix_sym to collect type symbols. */
c00f8484 2854
f8eba3c6
TT
2855static int
2856collect_one_symbol (struct symbol *sym, void *d)
2857{
9a3c8263
SM
2858 struct decode_compound_collector *collector
2859 = (struct decode_compound_collector *) d;
f8eba3c6
TT
2860 void **slot;
2861 struct type *t;
614b3b14 2862
f8eba3c6 2863 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
8e704927 2864 return 1; /* Continue iterating. */
f8eba3c6
TT
2865
2866 t = SYMBOL_TYPE (sym);
f168693b 2867 t = check_typedef (t);
f8eba3c6
TT
2868 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2869 && TYPE_CODE (t) != TYPE_CODE_UNION
2870 && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
8e704927 2871 return 1; /* Continue iterating. */
614b3b14 2872
f8eba3c6
TT
2873 slot = htab_find_slot (collector->unique_syms, sym, INSERT);
2874 if (!*slot)
2875 {
2876 *slot = sym;
2877 VEC_safe_push (symbolp, collector->symbols, sym);
2878 }
2879
8e704927 2880 return 1; /* Continue iterating. */
f8eba3c6 2881}
93d91629 2882
40e084e1 2883/* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
93d91629 2884
f8eba3c6 2885static VEC (symbolp) *
ec94af83 2886lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
40e084e1 2887 const char *class_name)
93d91629 2888{
f8eba3c6
TT
2889 int ix;
2890 struct symtab *elt;
2891 struct decode_compound_collector collector;
2892 struct cleanup *outer;
2893 struct cleanup *cleanup;
17763fd9 2894
f8eba3c6 2895 collector.symbols = NULL;
40e084e1 2896 outer = make_cleanup (VEC_cleanup (symbolp), &collector.symbols);
e0881a8e 2897
f8eba3c6
TT
2898 collector.unique_syms = htab_create_alloc (1, htab_hash_pointer,
2899 htab_eq_pointer, NULL,
2900 xcalloc, xfree);
e0881a8e 2901
ec94af83 2902 for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
f8eba3c6
TT
2903 {
2904 if (elt == NULL)
2905 {
40e084e1 2906 iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN,
f8eba3c6 2907 collect_one_symbol, &collector,
481860b3 2908 NULL, 0);
40e084e1 2909 iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN,
f8eba3c6 2910 collect_one_symbol, &collector,
481860b3 2911 NULL, 0);
f8eba3c6
TT
2912 }
2913 else
2914 {
f8eba3c6
TT
2915 /* Program spaces that are executing startup should have
2916 been filtered out earlier. */
2917 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
2918 set_current_program_space (SYMTAB_PSPACE (elt));
4eeaa230
DE
2919 iterate_over_file_blocks (elt, class_name, STRUCT_DOMAIN,
2920 collect_one_symbol, &collector);
2921 iterate_over_file_blocks (elt, class_name, VAR_DOMAIN,
2922 collect_one_symbol, &collector);
1e5a1abc
KS
2923 }
2924 }
2925
f8eba3c6
TT
2926 discard_cleanups (outer);
2927 return collector.symbols;
93d91629
DC
2928}
2929
40e084e1
KS
2930/* A qsort comparison function for symbols. The resulting order does
2931 not actually matter; we just need to be able to sort them so that
2932 symbols with the same program space end up next to each other. */
2933
2934static int
2935compare_symbols (const void *a, const void *b)
2936{
9a3c8263
SM
2937 struct symbol * const *sa = (struct symbol * const*) a;
2938 struct symbol * const *sb = (struct symbol * const*) b;
40e084e1
KS
2939 uintptr_t uia, uib;
2940
08be3fe3
DE
2941 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa));
2942 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb));
40e084e1
KS
2943
2944 if (uia < uib)
2945 return -1;
2946 if (uia > uib)
2947 return 1;
2948
2949 uia = (uintptr_t) *sa;
2950 uib = (uintptr_t) *sb;
2951
2952 if (uia < uib)
2953 return -1;
2954 if (uia > uib)
2955 return 1;
2956
2957 return 0;
2958}
2959
2960/* Like compare_symbols but for minimal symbols. */
4224873a 2961
f8eba3c6 2962static int
40e084e1 2963compare_msymbols (const void *a, const void *b)
4224873a 2964{
9a3c8263
SM
2965 const struct bound_minimal_symbol *sa
2966 = (const struct bound_minimal_symbol *) a;
2967 const struct bound_minimal_symbol *sb
2968 = (const struct bound_minimal_symbol *) b;
f8eba3c6
TT
2969 uintptr_t uia, uib;
2970
001822aa
TT
2971 uia = (uintptr_t) sa->objfile->pspace;
2972 uib = (uintptr_t) sa->objfile->pspace;
f8eba3c6
TT
2973
2974 if (uia < uib)
2975 return -1;
2976 if (uia > uib)
2977 return 1;
2978
001822aa
TT
2979 uia = (uintptr_t) sa->minsym;
2980 uib = (uintptr_t) sb->minsym;
f8eba3c6
TT
2981
2982 if (uia < uib)
2983 return -1;
2984 if (uia > uib)
2985 return 1;
2986
2987 return 0;
2988}
2989
2990/* Look for all the matching instances of each symbol in NAMES. Only
2991 instances from PSPACE are considered; other program spaces are
2992 handled by our caller. If PSPACE is NULL, then all program spaces
2993 are considered. Results are stored into INFO. */
2994
2995static void
2996add_all_symbol_names_from_pspace (struct collect_info *info,
2997 struct program_space *pspace,
2998 VEC (const_char_ptr) *names)
2999{
3000 int ix;
3001 const char *iter;
3002
3003 for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
3004 add_matching_symbols_to_info (iter, info, pspace);
3005}
3006
3007static void
3008find_superclass_methods (VEC (typep) *superclasses,
3009 const char *name,
3010 VEC (const_char_ptr) **result_names)
3011{
3012 int old_len = VEC_length (const_char_ptr, *result_names);
3013 VEC (typep) *iter_classes;
3014 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
3015
3016 iter_classes = superclasses;
3017 while (1)
3018 {
3019 VEC (typep) *new_supers = NULL;
3020 int ix;
3021 struct type *t;
3022
3023 make_cleanup (VEC_cleanup (typep), &new_supers);
3024 for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
3025 find_methods (t, name, result_names, &new_supers);
3026
3027 if (VEC_length (const_char_ptr, *result_names) != old_len
3028 || VEC_empty (typep, new_supers))
3029 break;
4224873a 3030
f8eba3c6
TT
3031 iter_classes = new_supers;
3032 }
4224873a 3033
f8eba3c6
TT
3034 do_cleanups (cleanup);
3035}
3036
40e084e1
KS
3037/* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3038 given by one of the symbols in SYM_CLASSES. Matches are returned
3039 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
f8eba3c6 3040
40e084e1 3041static void
ec94af83 3042find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
40e084e1
KS
3043 const char *class_name, const char *method_name,
3044 VEC (symbolp) *sym_classes, VEC (symbolp) **symbols,
f60e2d5c 3045 VEC (bound_minimal_symbol_d) **minsyms)
f8eba3c6 3046{
f8eba3c6
TT
3047 struct symbol *sym;
3048 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
3049 int ix;
3050 int last_result_len;
3051 VEC (typep) *superclass_vec;
3052 VEC (const_char_ptr) *result_names;
3053 struct collect_info info;
4224873a 3054
f8eba3c6
TT
3055 /* Sort symbols so that symbols with the same program space are next
3056 to each other. */
3057 qsort (VEC_address (symbolp, sym_classes),
3058 VEC_length (symbolp, sym_classes),
3059 sizeof (symbolp),
3060 compare_symbols);
3061
3062 info.state = self;
40e084e1
KS
3063 info.file_symtabs = file_symtabs;
3064 info.result.symbols = NULL;
3065 info.result.minimal_symbols = NULL;
f8eba3c6
TT
3066
3067 /* Iterate over all the types, looking for the names of existing
40e084e1 3068 methods matching METHOD_NAME. If we cannot find a direct method in a
f8eba3c6
TT
3069 given program space, then we consider inherited methods; this is
3070 not ideal (ideal would be to respect C++ hiding rules), but it
3071 seems good enough and is what GDB has historically done. We only
3072 need to collect the names because later we find all symbols with
3073 those names. This loop is written in a somewhat funny way
3074 because we collect data across the program space before deciding
3075 what to do. */
3076 superclass_vec = NULL;
3077 make_cleanup (VEC_cleanup (typep), &superclass_vec);
3078 result_names = NULL;
3079 make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
3080 last_result_len = 0;
3081 for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
3082 {
3083 struct type *t;
3084 struct program_space *pspace;
3085
3086 /* Program spaces that are executing startup should have
3087 been filtered out earlier. */
08be3fe3
DE
3088 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
3089 gdb_assert (!pspace->executing_startup);
f8eba3c6
TT
3090 set_current_program_space (pspace);
3091 t = check_typedef (SYMBOL_TYPE (sym));
40e084e1 3092 find_methods (t, method_name, &result_names, &superclass_vec);
f8eba3c6
TT
3093
3094 /* Handle all items from a single program space at once; and be
3095 sure not to miss the last batch. */
3096 if (ix == VEC_length (symbolp, sym_classes) - 1
3097 || (pspace
08be3fe3 3098 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes,
f8eba3c6 3099 ix + 1)))))
4224873a 3100 {
f8eba3c6
TT
3101 /* If we did not find a direct implementation anywhere in
3102 this program space, consider superclasses. */
3103 if (VEC_length (const_char_ptr, result_names) == last_result_len)
40e084e1
KS
3104 find_superclass_methods (superclass_vec, method_name,
3105 &result_names);
f8eba3c6
TT
3106
3107 /* We have a list of candidate symbol names, so now we
3108 iterate over the symbol tables looking for all
3109 matches in this pspace. */
3110 add_all_symbol_names_from_pspace (&info, pspace, result_names);
3111
3112 VEC_truncate (typep, superclass_vec, 0);
3113 last_result_len = VEC_length (const_char_ptr, result_names);
4224873a 3114 }
4224873a 3115 }
f8eba3c6 3116
40e084e1 3117 if (!VEC_empty (symbolp, info.result.symbols)
f60e2d5c 3118 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
4224873a 3119 {
40e084e1
KS
3120 *symbols = info.result.symbols;
3121 *minsyms = info.result.minimal_symbols;
f8eba3c6 3122 do_cleanups (cleanup);
40e084e1 3123 return;
4224873a 3124 }
f8eba3c6 3125
40e084e1
KS
3126 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3127 and other attempts to locate the symbol will be made. */
3128 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
f8eba3c6
TT
3129}
3130
3131\f
3132
3133/* This object is used when collecting all matching symtabs. */
3134
3135struct symtab_collector
3136{
3137 /* The result vector of symtabs. */
ec94af83 3138 VEC (symtab_ptr) *symtabs;
f8eba3c6
TT
3139
3140 /* This is used to ensure the symtabs are unique. */
3141 htab_t symtab_table;
fc4007c9
TT
3142
3143 symtab_collector ()
3144 : symtabs (NULL),
3145 symtab_table (NULL)
3146 {
3147 }
3148
3149 ~symtab_collector ()
3150 {
3151 if (symtab_table != NULL)
3152 htab_delete (symtab_table);
3153 }
f8eba3c6
TT
3154};
3155
3156/* Callback for iterate_over_symtabs. */
3157
3158static int
3159add_symtabs_to_list (struct symtab *symtab, void *d)
3160{
9a3c8263 3161 struct symtab_collector *data = (struct symtab_collector *) d;
f8eba3c6
TT
3162 void **slot;
3163
3164 slot = htab_find_slot (data->symtab_table, symtab, INSERT);
3165 if (!*slot)
4224873a 3166 {
f8eba3c6 3167 *slot = symtab;
ec94af83 3168 VEC_safe_push (symtab_ptr, data->symtabs, symtab);
4224873a 3169 }
f8eba3c6
TT
3170
3171 return 0;
4224873a
DC
3172}
3173
c2f4122d
PA
3174/* Given a file name, return a VEC of all matching symtabs. If
3175 SEARCH_PSPACE is not NULL, the search is restricted to just that
3176 program space. */
f8eba3c6 3177
ec94af83 3178static VEC (symtab_ptr) *
c2f4122d
PA
3179collect_symtabs_from_filename (const char *file,
3180 struct program_space *search_pspace)
f8eba3c6
TT
3181{
3182 struct symtab_collector collector;
3183 struct cleanup *cleanups;
3184 struct program_space *pspace;
3185
3186 collector.symtabs = NULL;
3187 collector.symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
3188 NULL);
f8eba3c6
TT
3189
3190 /* Find that file's data. */
c2f4122d
PA
3191 if (search_pspace == NULL)
3192 {
3193 ALL_PSPACES (pspace)
3194 {
3195 if (pspace->executing_startup)
3196 continue;
f8eba3c6 3197
c2f4122d
PA
3198 set_current_program_space (pspace);
3199 iterate_over_symtabs (file, add_symtabs_to_list, &collector);
3200 }
3201 }
3202 else
3203 {
3204 set_current_program_space (search_pspace);
3205 iterate_over_symtabs (file, add_symtabs_to_list, &collector);
3206 }
f3c39e76 3207
f8eba3c6
TT
3208 return collector.symtabs;
3209}
3210
c2f4122d
PA
3211/* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3212 not NULL, the search is restricted to just that program space. */
f8eba3c6 3213
ec94af83 3214static VEC (symtab_ptr) *
c2f4122d
PA
3215symtabs_from_filename (const char *filename,
3216 struct program_space *search_pspace)
40e084e1 3217{
ec94af83 3218 VEC (symtab_ptr) *result;
40e084e1 3219
c2f4122d 3220 result = collect_symtabs_from_filename (filename, search_pspace);
f8eba3c6 3221
ec94af83 3222 if (VEC_empty (symtab_ptr, result))
f8eba3c6 3223 {
40e084e1
KS
3224 if (!have_full_symbols () && !have_partial_symbols ())
3225 throw_error (NOT_FOUND_ERROR,
3226 _("No symbol table is loaded. "
3227 "Use the \"file\" command."));
00e52e53 3228 source_file_not_found_error (filename);
f8eba3c6
TT
3229 }
3230
40e084e1 3231 return result;
84fba31b 3232}
f3c39e76 3233
40e084e1
KS
3234/* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3235 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3236 returned in MINSYMS. */
14e91ac5 3237
40e084e1
KS
3238static void
3239find_function_symbols (struct linespec_state *state,
ec94af83 3240 VEC (symtab_ptr) *file_symtabs, const char *name,
40e084e1 3241 VEC (symbolp) **symbols,
f60e2d5c 3242 VEC (bound_minimal_symbol_d) **minsyms)
14e91ac5 3243{
40e084e1
KS
3244 struct collect_info info;
3245 VEC (const_char_ptr) *symbol_names = NULL;
3246 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
3247 &symbol_names);
14e91ac5 3248
40e084e1
KS
3249 info.state = state;
3250 info.result.symbols = NULL;
3251 info.result.minimal_symbols = NULL;
3252 info.file_symtabs = file_symtabs;
e0881a8e 3253
40e084e1 3254 /* Try NAME as an Objective-C selector. */
d7561cbb 3255 find_imps (name, &symbol_names);
40e084e1 3256 if (!VEC_empty (const_char_ptr, symbol_names))
c2f4122d
PA
3257 add_all_symbol_names_from_pspace (&info, state->search_pspace,
3258 symbol_names);
40e084e1 3259 else
c2f4122d 3260 add_matching_symbols_to_info (name, &info, state->search_pspace);
40e084e1
KS
3261
3262 do_cleanups (cleanup);
3263
3264 if (VEC_empty (symbolp, info.result.symbols))
3265 {
3266 VEC_free (symbolp, info.result.symbols);
3267 *symbols = NULL;
14e91ac5
DC
3268 }
3269 else
40e084e1
KS
3270 *symbols = info.result.symbols;
3271
f60e2d5c 3272 if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
14e91ac5 3273 {
f60e2d5c 3274 VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
40e084e1
KS
3275 *minsyms = NULL;
3276 }
3277 else
3278 *minsyms = info.result.minimal_symbols;
3279}
3280
3281/* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3282 in SYMBOLS and minimal symbols in MINSYMS. */
14e91ac5 3283
b1ae631a 3284static void
40e084e1 3285find_linespec_symbols (struct linespec_state *state,
ec94af83 3286 VEC (symtab_ptr) *file_symtabs,
40e084e1
KS
3287 const char *name,
3288 VEC (symbolp) **symbols,
f60e2d5c 3289 VEC (bound_minimal_symbol_d) **minsyms)
40e084e1 3290{
2f408ecb
PA
3291 demangle_result_storage demangle_storage;
3292 std::string ada_lookup_storage;
cc81e1c6 3293 const char *lookup_name;
f8eba3c6 3294
40e084e1
KS
3295 if (state->language->la_language == language_ada)
3296 {
3297 /* In Ada, the symbol lookups are performed using the encoded
3298 name rather than the demangled name. */
2f408ecb
PA
3299 ada_lookup_storage = ada_name_for_lookup (name);
3300 lookup_name = ada_lookup_storage.c_str ();
40e084e1 3301 }
2f408ecb 3302 else
40e084e1 3303 {
2f408ecb
PA
3304 lookup_name = demangle_for_lookup (name,
3305 state->language->la_language,
3306 demangle_storage);
40e084e1 3307 }
f8eba3c6 3308
2f408ecb
PA
3309 std::string canon = cp_canonicalize_string_no_typedefs (lookup_name);
3310 if (!canon.empty ())
3311 lookup_name = canon.c_str ();
3312
cc81e1c6
DE
3313 /* It's important to not call expand_symtabs_matching unnecessarily
3314 as it can really slow things down (by unnecessarily expanding
3315 potentially 1000s of symtabs, which when debugging some apps can
3316 cost 100s of seconds). Avoid this to some extent by *first* calling
3317 find_function_symbols, and only if that doesn't find anything
3318 *then* call find_method. This handles two important cases:
3319 1) break (anonymous namespace)::foo
3320 2) break class::method where method is in class (and not a baseclass) */
14e91ac5 3321
cc81e1c6
DE
3322 find_function_symbols (state, file_symtabs, lookup_name,
3323 symbols, minsyms);
14e91ac5 3324
cc81e1c6
DE
3325 /* If we were unable to locate a symbol of the same name, try dividing
3326 the name into class and method names and searching the class and its
3327 baseclasses. */
3328 if (VEC_empty (symbolp, *symbols)
f60e2d5c 3329 && VEC_empty (bound_minimal_symbol_d, *minsyms))
40e084e1 3330 {
2f408ecb 3331 std::string klass, method;
cc81e1c6
DE
3332 const char *last, *p, *scope_op;
3333 VEC (symbolp) *classes;
14e91ac5 3334
cc81e1c6
DE
3335 /* See if we can find a scope operator and break this symbol
3336 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3337 scope_op = "::";
3338 p = find_toplevel_string (lookup_name, scope_op);
14e91ac5 3339
cc81e1c6
DE
3340 last = NULL;
3341 while (p != NULL)
f8eba3c6 3342 {
cc81e1c6
DE
3343 last = p;
3344 p = find_toplevel_string (p + strlen (scope_op), scope_op);
f8eba3c6 3345 }
14e91ac5 3346
cc81e1c6
DE
3347 /* If no scope operator was found, there is nothing more we can do;
3348 we already attempted to lookup the entire name as a symbol
3349 and failed. */
3350 if (last == NULL)
2f408ecb 3351 return;
cc81e1c6
DE
3352
3353 /* LOOKUP_NAME points to the class name.
3354 LAST points to the method name. */
2f408ecb 3355 klass = std::string (lookup_name, last - lookup_name);
cc81e1c6
DE
3356
3357 /* Skip past the scope operator. */
3358 last += strlen (scope_op);
2f408ecb 3359 method = last;
cc81e1c6
DE
3360
3361 /* Find a list of classes named KLASS. */
2f408ecb
PA
3362 classes = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
3363 struct cleanup *old_chain
3364 = make_cleanup (VEC_cleanup (symbolp), &classes);
cc81e1c6
DE
3365
3366 if (!VEC_empty (symbolp, classes))
3367 {
3368 /* Now locate a list of suitable methods named METHOD. */
492d29ea 3369 TRY
cc81e1c6 3370 {
2f408ecb
PA
3371 find_method (state, file_symtabs,
3372 klass.c_str (), method.c_str (),
3373 classes, symbols, minsyms);
cc81e1c6
DE
3374 }
3375
3376 /* If successful, we're done. If NOT_FOUND_ERROR
3377 was not thrown, rethrow the exception that we did get. */
492d29ea 3378 CATCH (except, RETURN_MASK_ERROR)
7556d4a4
PA
3379 {
3380 if (except.error != NOT_FOUND_ERROR)
3381 throw_exception (except);
3382 }
492d29ea 3383 END_CATCH
cc81e1c6 3384 }
14e91ac5 3385
2f408ecb
PA
3386 do_cleanups (old_chain);
3387 }
14e91ac5
DC
3388}
3389
40e084e1
KS
3390/* Return all labels named NAME in FUNCTION_SYMBOLS. Return the
3391 actual function symbol in which the label was found in LABEL_FUNC_RET. */
0f5238ed 3392
40e084e1
KS
3393static VEC (symbolp) *
3394find_label_symbols (struct linespec_state *self,
3395 VEC (symbolp) *function_symbols,
3396 VEC (symbolp) **label_funcs_ret, const char *name)
0f5238ed 3397{
f8eba3c6 3398 int ix;
3977b71f 3399 const struct block *block;
40e084e1
KS
3400 struct symbol *sym;
3401 struct symbol *fn_sym;
3402 VEC (symbolp) *result = NULL;
9ef07c8c 3403
f8eba3c6 3404 if (function_symbols == NULL)
9ef07c8c 3405 {
f8eba3c6 3406 set_current_program_space (self->program_space);
4eeaa230 3407 block = get_current_search_block ();
f8eba3c6 3408
9ef07c8c
TT
3409 for (;
3410 block && !BLOCK_FUNCTION (block);
3411 block = BLOCK_SUPERBLOCK (block))
3412 ;
3413 if (!block)
40e084e1 3414 return NULL;
f8eba3c6
TT
3415 fn_sym = BLOCK_FUNCTION (block);
3416
d12307c1 3417 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol;
f8eba3c6 3418
40e084e1
KS
3419 if (sym != NULL)
3420 {
3421 VEC_safe_push (symbolp, result, sym);
3422 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
3423 }
3424 }
3425 else
3426 {
3427 for (ix = 0;
3428 VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
f8eba3c6 3429 {
08be3fe3 3430 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
40e084e1 3431 block = SYMBOL_BLOCK_VALUE (fn_sym);
d12307c1 3432 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol;
40e084e1
KS
3433
3434 if (sym != NULL)
3435 {
3436 VEC_safe_push (symbolp, result, sym);
3437 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
3438 }
f8eba3c6 3439 }
40e084e1 3440 }
f8eba3c6 3441
40e084e1
KS
3442 return result;
3443}
f8eba3c6 3444
40e084e1
KS
3445\f
3446
3447/* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3448
3449static void
3450decode_digits_list_mode (struct linespec_state *self,
3451 linespec_p ls,
3452 struct symtabs_and_lines *values,
3453 struct symtab_and_line val)
3454{
3455 int ix;
3456 struct symtab *elt;
3457
3458 gdb_assert (self->list_mode);
3459
ec94af83 3460 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
40e084e1
KS
3461 ++ix)
3462 {
3463 /* The logic above should ensure this. */
3464 gdb_assert (elt != NULL);
3465
3466 set_current_program_space (SYMTAB_PSPACE (elt));
3467
3468 /* Simplistic search just for the list command. */
3469 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
3470 if (val.symtab == NULL)
3471 val.symtab = elt;
3472 val.pspace = SYMTAB_PSPACE (elt);
3473 val.pc = 0;
3474 val.explicit_line = 1;
3475
66f1999b 3476 add_sal_to_sals (self, values, &val, NULL, 0);
f8eba3c6 3477 }
40e084e1 3478}
f8eba3c6 3479
40e084e1
KS
3480/* A helper for create_sals_line_offset that iterates over the symtabs,
3481 adding lines to the VEC. */
3482
3483static void
3484decode_digits_ordinary (struct linespec_state *self,
3485 linespec_p ls,
3486 int line,
3487 struct symtabs_and_lines *sals,
3488 struct linetable_entry **best_entry)
3489{
3490 int ix;
3491 struct symtab *elt;
f8eba3c6 3492
ec94af83 3493 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
f8eba3c6 3494 {
40e084e1
KS
3495 int i;
3496 VEC (CORE_ADDR) *pcs;
3497 CORE_ADDR pc;
3498
3499 /* The logic above should ensure this. */
3500 gdb_assert (elt != NULL);
f8eba3c6 3501
40e084e1 3502 set_current_program_space (SYMTAB_PSPACE (elt));
f8eba3c6 3503
40e084e1
KS
3504 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
3505 for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
f8eba3c6
TT
3506 {
3507 struct symtab_and_line sal;
40e084e1
KS
3508
3509 init_sal (&sal);
3510 sal.pspace = SYMTAB_PSPACE (elt);
3511 sal.symtab = elt;
3512 sal.line = line;
3513 sal.pc = pc;
3514 add_sal_to_sals_basic (sals, &sal);
f8eba3c6 3515 }
40e084e1
KS
3516
3517 VEC_free (CORE_ADDR, pcs);
f8eba3c6 3518 }
40e084e1
KS
3519}
3520
3521\f
3522
3523/* Return the line offset represented by VARIABLE. */
3524
3525static struct line_offset
3526linespec_parse_variable (struct linespec_state *self, const char *variable)
3527{
3528 int index = 0;
3529 const char *p;
3530 struct line_offset offset = {0, LINE_OFFSET_NONE};
f8eba3c6 3531
40e084e1
KS
3532 p = (variable[1] == '$') ? variable + 2 : variable + 1;
3533 if (*p == '$')
3534 ++p;
3535 while (*p >= '0' && *p <= '9')
3536 ++p;
3537 if (!*p) /* Reached end of token without hitting non-digit. */
f8eba3c6 3538 {
40e084e1
KS
3539 /* We have a value history reference. */
3540 struct value *val_history;
f8eba3c6 3541
40e084e1
KS
3542 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
3543 val_history
3544 = access_value_history ((variable[1] == '$') ? -index : index);
3545 if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
3546 error (_("History values used in line "
3547 "specs must have integer values."));
3548 offset.offset = value_as_long (val_history);
3549 }
3550 else
3551 {
3552 /* Not all digits -- may be user variable/function or a
3553 convenience variable. */
3554 LONGEST valx;
3555 struct internalvar *ivar;
3556
3557 /* Try it as a convenience variable. If it is not a convenience
3558 variable, return and allow normal symbol lookup to occur. */
3559 ivar = lookup_only_internalvar (variable + 1);
3560 if (ivar == NULL)
3561 /* No internal variable with that name. Mark the offset
3562 as unknown to allow the name to be looked up as a symbol. */
3563 offset.sign = LINE_OFFSET_UNKNOWN;
3564 else
3565 {
3566 /* We found a valid variable name. If it is not an integer,
3567 throw an error. */
3568 if (!get_internalvar_integer (ivar, &valx))
3569 error (_("Convenience variables used in line "
3570 "specs must have integer values."));
3571 else
3572 offset.offset = valx;
3573 }
f8eba3c6
TT
3574 }
3575
40e084e1 3576 return offset;
f8eba3c6 3577}
40e084e1 3578\f
f8eba3c6
TT
3579
3580/* A callback used to possibly add a symbol to the results. */
3581
3582static int
3583collect_symbols (struct symbol *sym, void *data)
3584{
9a3c8263 3585 struct collect_info *info = (struct collect_info *) data;
f8eba3c6 3586
40e084e1
KS
3587 /* In list mode, add all matching symbols, regardless of class.
3588 This allows the user to type "list a_global_variable". */
3589 if (SYMBOL_CLASS (sym) == LOC_BLOCK || info->state->list_mode)
3590 VEC_safe_push (symbolp, info->result.symbols, sym);
8e704927 3591 return 1; /* Continue iterating. */
f8eba3c6
TT
3592}
3593
40e084e1 3594/* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
6e22494e
JK
3595 linespec; return the SAL in RESULT. This function should return SALs
3596 matching those from find_function_start_sal, otherwise false
3597 multiple-locations breakpoints could be placed. */
f8eba3c6
TT
3598
3599static void
3600minsym_found (struct linespec_state *self, struct objfile *objfile,
3601 struct minimal_symbol *msymbol,
3602 struct symtabs_and_lines *result)
3603{
3604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3605 CORE_ADDR pc;
3606 struct symtab_and_line sal;
3607
77e371c0 3608 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
f8eba3c6 3609 (struct obj_section *) 0, 0);
efd66ac6 3610 sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
f8eba3c6
TT
3611
3612 /* The minimal symbol might point to a function descriptor;
3613 resolve it to the actual code address instead. */
3614 pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
3615 if (pc != sal.pc)
3616 sal = find_pc_sect_line (pc, NULL, 0);
3617
3618 if (self->funfirstline)
6e22494e
JK
3619 {
3620 if (sal.symtab != NULL
3621 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
3622 || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
3623 {
3624 /* If gdbarch_convert_from_func_ptr_addr does not apply then
3625 sal.SECTION, sal.LINE&co. will stay correct from above.
3626 If gdbarch_convert_from_func_ptr_addr applies then
3627 sal.SECTION is cleared from above and sal.LINE&co. will
3628 stay correct from the last find_pc_sect_line above. */
3629 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
3630 sal.pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc,
3631 &current_target);
141c5cc4
JK
3632 if (gdbarch_skip_entrypoint_p (gdbarch))
3633 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
6e22494e
JK
3634 }
3635 else
3636 skip_prologue_sal (&sal);
3637 }
f8eba3c6 3638
07fea4b4 3639 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
efd66ac6 3640 add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0);
f8eba3c6
TT
3641}
3642
39b856a4
TT
3643/* A helper struct to pass some data through
3644 iterate_over_minimal_symbols. */
3645
3646struct collect_minsyms
3647{
3648 /* The objfile we're examining. */
3649 struct objfile *objfile;
3650
87186c6a
MMN
3651 /* Only search the given symtab, or NULL to search for all symbols. */
3652 struct symtab *symtab;
3653
39b856a4
TT
3654 /* The funfirstline setting from the initial call. */
3655 int funfirstline;
3656
095bcf5e
JB
3657 /* The list_mode setting from the initial call. */
3658 int list_mode;
3659
39b856a4 3660 /* The resulting symbols. */
f60e2d5c 3661 VEC (bound_minimal_symbol_d) *msyms;
39b856a4
TT
3662};
3663
3664/* A helper function to classify a minimal_symbol_type according to
3665 priority. */
3666
3667static int
3668classify_mtype (enum minimal_symbol_type t)
3669{
3670 switch (t)
f8eba3c6 3671 {
39b856a4
TT
3672 case mst_file_text:
3673 case mst_file_data:
3674 case mst_file_bss:
3675 /* Intermediate priority. */
3676 return 1;
3677
3678 case mst_solib_trampoline:
3679 /* Lowest priority. */
3680 return 2;
3681
3682 default:
3683 /* Highest priority. */
3684 return 0;
f8eba3c6 3685 }
39b856a4
TT
3686}
3687
3688/* Callback for qsort that sorts symbols by priority. */
3689
3690static int
3691compare_msyms (const void *a, const void *b)
3692{
9a3c8263
SM
3693 const bound_minimal_symbol_d *moa = (const bound_minimal_symbol_d *) a;
3694 const bound_minimal_symbol_d *mob = (const bound_minimal_symbol_d *) b;
39b856a4
TT
3695 enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
3696 enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
3697
3698 return classify_mtype (ta) - classify_mtype (tb);
3699}
3700
3701/* Callback for iterate_over_minimal_symbols that adds the symbol to
3702 the result. */
3703
3704static void
3705add_minsym (struct minimal_symbol *minsym, void *d)
3706{
9a3c8263 3707 struct collect_minsyms *info = (struct collect_minsyms *) d;
f60e2d5c 3708 bound_minimal_symbol_d mo;
39b856a4 3709
77e371c0
TT
3710 mo.minsym = minsym;
3711 mo.objfile = info->objfile;
3712
87186c6a
MMN
3713 if (info->symtab != NULL)
3714 {
3715 CORE_ADDR pc;
3716 struct symtab_and_line sal;
3717 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
3718
3719 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym),
3720 NULL, 0);
3721 sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym);
3722 pc
3723 = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
3724 if (pc != sal.pc)
3725 sal = find_pc_sect_line (pc, NULL, 0);
3726
3727 if (info->symtab != sal.symtab)
3728 return;
3729 }
3730
095bcf5e
JB
3731 /* Exclude data symbols when looking for breakpoint locations. */
3732 if (!info->list_mode)
3733 switch (minsym->type)
3734 {
3735 case mst_slot_got_plt:
3736 case mst_data:
3737 case mst_bss:
3738 case mst_abs:
3739 case mst_file_data:
3740 case mst_file_bss:
1a2da5ee
JB
3741 {
3742 /* Make sure this minsym is not a function descriptor
3743 before we decide to discard it. */
df6d5441 3744 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
1a2da5ee 3745 CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
77e371c0 3746 (gdbarch, BMSYMBOL_VALUE_ADDRESS (mo),
1a2da5ee
JB
3747 &current_target);
3748
77e371c0 3749 if (addr == BMSYMBOL_VALUE_ADDRESS (mo))
1a2da5ee
JB
3750 return;
3751 }
095bcf5e
JB
3752 }
3753
f60e2d5c 3754 VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
f8eba3c6
TT
3755}
3756
87186c6a 3757/* Search for minimal symbols called NAME. If SEARCH_PSPACE
f8eba3c6 3758 is not NULL, the search is restricted to just that program
87186c6a
MMN
3759 space.
3760
3761 If SYMTAB is NULL, search all objfiles, otherwise
3762 restrict results to the given SYMTAB. */
f8eba3c6
TT
3763
3764static void
3765search_minsyms_for_name (struct collect_info *info, const char *name,
87186c6a
MMN
3766 struct program_space *search_pspace,
3767 struct symtab *symtab)
f8eba3c6 3768{
87186c6a
MMN
3769 struct collect_minsyms local;
3770 struct cleanup *cleanup;
f8eba3c6 3771
87186c6a
MMN
3772 memset (&local, 0, sizeof (local));
3773 local.funfirstline = info->state->funfirstline;
3774 local.list_mode = info->state->list_mode;
3775 local.symtab = symtab;
39b856a4 3776
87186c6a 3777 cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);
f8eba3c6 3778
87186c6a
MMN
3779 if (symtab == NULL)
3780 {
3781 struct program_space *pspace;
f8eba3c6 3782
87186c6a
MMN
3783 ALL_PSPACES (pspace)
3784 {
3785 struct objfile *objfile;
39b856a4 3786
87186c6a
MMN
3787 if (search_pspace != NULL && search_pspace != pspace)
3788 continue;
3789 if (pspace->executing_startup)
3790 continue;
39b856a4 3791
87186c6a
MMN
3792 set_current_program_space (pspace);
3793
3794 ALL_OBJFILES (objfile)
3795 {
3796 local.objfile = objfile;
3797 iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
3798 }
3799 }
3800 }
3801 else
f8eba3c6 3802 {
87186c6a
MMN
3803 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
3804 {
3805 set_current_program_space (SYMTAB_PSPACE (symtab));
3806 local.objfile = SYMTAB_OBJFILE(symtab);
3807 iterate_over_minimal_symbols (local.objfile, name, add_minsym,
3808 &local);
3809 }
9ef07c8c 3810 }
39b856a4 3811
f60e2d5c 3812 if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
39b856a4
TT
3813 {
3814 int classification;
3815 int ix;
f60e2d5c 3816 bound_minimal_symbol_d *item;
39b856a4 3817
f60e2d5c
TT
3818 qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
3819 VEC_length (bound_minimal_symbol_d, local.msyms),
3820 sizeof (bound_minimal_symbol_d),
39b856a4
TT
3821 compare_msyms);
3822
3823 /* Now the minsyms are in classification order. So, we walk
3824 over them and process just the minsyms with the same
3825 classification as the very first minsym in the list. */
f60e2d5c 3826 item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
39b856a4
TT
3827 classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
3828
3829 for (ix = 0;
f60e2d5c 3830 VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
39b856a4
TT
3831 ++ix)
3832 {
3833 if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
3834 break;
3835
f60e2d5c 3836 VEC_safe_push (bound_minimal_symbol_d,
40e084e1 3837 info->result.minimal_symbols, item);
39b856a4
TT
3838 }
3839 }
3840
3841 do_cleanups (cleanup);
f8eba3c6
TT
3842}
3843
3844/* A helper function to add all symbols matching NAME to INFO. If
3845 PSPACE is not NULL, the search is restricted to just that program
3846 space. */
0f5238ed 3847
f8eba3c6
TT
3848static void
3849add_matching_symbols_to_info (const char *name,
3850 struct collect_info *info,
3851 struct program_space *pspace)
3852{
3853 int ix;
3854 struct symtab *elt;
0f5238ed 3855
ec94af83 3856 for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
f8eba3c6 3857 {
f8eba3c6
TT
3858 if (elt == NULL)
3859 {
40e084e1 3860 iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN,
f8eba3c6 3861 collect_symbols, info,
481860b3 3862 pspace, 1);
87186c6a 3863 search_minsyms_for_name (info, name, pspace, NULL);
f8eba3c6
TT
3864 }
3865 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
3866 {
87186c6a
MMN
3867 int prev_len = VEC_length (symbolp, info->result.symbols);
3868
f8eba3c6
TT
3869 /* Program spaces that are executing startup should have
3870 been filtered out earlier. */
3871 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3872 set_current_program_space (SYMTAB_PSPACE (elt));
4eeaa230
DE
3873 iterate_over_file_blocks (elt, name, VAR_DOMAIN,
3874 collect_symbols, info);
87186c6a
MMN
3875
3876 /* If no new symbols were found in this iteration and this symtab
3877 is in assembler, we might actually be looking for a label for
3878 which we don't have debug info. Check for a minimal symbol in
3879 this case. */
3880 if (prev_len == VEC_length (symbolp, info->result.symbols)
3881 && elt->language == language_asm)
3882 search_minsyms_for_name (info, name, pspace, elt);
f8eba3c6
TT
3883 }
3884 }
0f5238ed
TT
3885}
3886
14e91ac5
DC
3887\f
3888
413dad4d
DC
3889/* Now come some functions that are called from multiple places within
3890 decode_line_1. */
3891
f8eba3c6
TT
3892static int
3893symbol_to_sal (struct symtab_and_line *result,
3894 int funfirstline, struct symbol *sym)
413dad4d 3895{
413dad4d 3896 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 3897 {
f8eba3c6
TT
3898 *result = find_function_start_sal (sym, funfirstline);
3899 return 1;
50641945 3900 }
413dad4d
DC
3901 else
3902 {
62853458 3903 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
413dad4d 3904 {
f8eba3c6 3905 init_sal (result);
08be3fe3 3906 result->symtab = symbol_symtab (sym);
f8eba3c6
TT
3907 result->line = SYMBOL_LINE (sym);
3908 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 3909 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6
TT
3910 result->explicit_pc = 1;
3911 return 1;
413dad4d 3912 }
62853458 3913 else if (funfirstline)
dcf9f4ab 3914 {
f8eba3c6 3915 /* Nothing. */
dcf9f4ab 3916 }
62853458
TT
3917 else if (SYMBOL_LINE (sym) != 0)
3918 {
3919 /* We know its line number. */
f8eba3c6 3920 init_sal (result);
08be3fe3 3921 result->symtab = symbol_symtab (sym);
f8eba3c6 3922 result->line = SYMBOL_LINE (sym);
08be3fe3 3923 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6 3924 return 1;
62853458 3925 }
413dad4d 3926 }
f8eba3c6
TT
3927
3928 return 0;
413dad4d 3929}
50641945 3930
f8eba3c6 3931/* See the comment in linespec.h. */
50641945 3932
f8eba3c6
TT
3933void
3934init_linespec_result (struct linespec_result *lr)
413dad4d 3935{
f8eba3c6
TT
3936 memset (lr, 0, sizeof (*lr));
3937}
413dad4d 3938
f8eba3c6 3939/* See the comment in linespec.h. */
bccdca4a 3940
f8eba3c6
TT
3941void
3942destroy_linespec_result (struct linespec_result *ls)
3943{
3944 int i;
3945 struct linespec_sals *lsal;
bccdca4a 3946
f00aae0f 3947 delete_event_location (ls->location);
f8eba3c6
TT
3948 for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
3949 {
3950 xfree (lsal->canonical);
3951 xfree (lsal->sals.sals);
3952 }
3953 VEC_free (linespec_sals, ls->sals);
3954}
e48883f7 3955
f8eba3c6
TT
3956/* Cleanup function for a linespec_result. */
3957
3958static void
3959cleanup_linespec_result (void *a)
3960{
9a3c8263 3961 destroy_linespec_result ((struct linespec_result *) a);
50641945 3962}
7efd8fc2 3963
f8eba3c6
TT
3964/* See the comment in linespec.h. */
3965
3966struct cleanup *
3967make_cleanup_destroy_linespec_result (struct linespec_result *ls)
7efd8fc2 3968{
f8eba3c6 3969 return make_cleanup (cleanup_linespec_result, ls);
7efd8fc2 3970}
87f0e720
KS
3971
3972/* Return the quote characters permitted by the linespec parser. */
3973
3974const char *
3975get_gdb_linespec_parser_quote_characters (void)
3976{
3977 return linespec_quote_characters;
3978}
This page took 1.930132 seconds and 4 git commands to generate.