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