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