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