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