2012-07-25 Hui Zhu <hui_zhu@mentor.com>
[deliverable/binutils-gdb.git] / gdb / linespec.c
1 /* Parser for linespec for the GNU debugger, GDB.
2
3 Copyright (C) 1986-2005, 2007-2012 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "frame.h"
23 #include "command.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "source.h"
27 #include "demangle.h"
28 #include "value.h"
29 #include "completer.h"
30 #include "cp-abi.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
33 #include "block.h"
34 #include "objc-lang.h"
35 #include "linespec.h"
36 #include "exceptions.h"
37 #include "language.h"
38 #include "interps.h"
39 #include "mi/mi-cmds.h"
40 #include "target.h"
41 #include "arch-utils.h"
42 #include <ctype.h>
43 #include "cli/cli-utils.h"
44 #include "filenames.h"
45 #include "ada-lang.h"
46 #include "stack.h"
47
48 typedef struct symtab *symtab_p;
49 DEF_VEC_P (symtab_p);
50
51 typedef struct symbol *symbolp;
52 DEF_VEC_P (symbolp);
53
54 typedef struct type *typep;
55 DEF_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
61 struct address_entry
62 {
63 struct program_space *pspace;
64 CORE_ADDR addr;
65 };
66
67 /* A helper struct which just holds a minimal symbol and the object
68 file from which it came. */
69
70 typedef struct minsym_and_objfile
71 {
72 struct minimal_symbol *minsym;
73 struct objfile *objfile;
74 } minsym_and_objfile_d;
75
76 DEF_VEC_O (minsym_and_objfile_d);
77
78 /* An enumeration of possible signs for a line offset. */
79 enum 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
96 struct 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
107 struct 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. */
113 const char *expression;
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. */
121 const char *source_filename;
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. */
133 const char *function_name;
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. */
143 const char *label_name;
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 };
158 typedef struct linespec *linespec_p;
159
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
164 struct linespec_state
165 {
166 /* The language in use during linespec processing. */
167 const struct language_defn *language;
168
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
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
199 struct collect_info
200 {
201 /* The linespec object in use. */
202 struct linespec_state *state;
203
204 /* A list of symtabs to which to restrict matches. */
205 VEC (symtab_p) *file_symtabs;
206
207 /* The result being accumulated. */
208 struct
209 {
210 VEC (symbolp) *symbols;
211 VEC (minsym_and_objfile_d) *minimal_symbols;
212 } result;
213 };
214
215 /* Token types */
216
217 enum ls_token_type
218 {
219 /* A keyword */
220 LSTOKEN_KEYWORD = 0,
221
222 /* A colon "separator" */
223 LSTOKEN_COLON,
224
225 /* A string */
226 LSTOKEN_STRING,
227
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 };
240 typedef enum ls_token_type linespec_token_type;
241
242 /* List of keywords */
243
244 static const char * const linespec_keywords[] = { "if", "thread", "task" };
245
246 /* A token of the linespec lexer */
247
248 struct 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 };
263 typedef 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
270 struct ls_parser
271 {
272 /* Lexer internal data */
273 struct
274 {
275 /* Save head of input stream. */
276 char *saved_arg;
277
278 /* Head of the input stream. */
279 char **stream;
280 #define PARSER_STREAM(P) (*(P)->lexer.stream)
281
282 /* The current token. */
283 linespec_token current;
284 } lexer;
285
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)
292
293 /* The result of the parse. */
294 struct linespec result;
295 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
296 };
297 typedef struct ls_parser linespec_parser;
298
299 /* Prototypes for local functions. */
300
301 static void initialize_defaults (struct symtab **default_symtab,
302 int *default_line);
303
304 static CORE_ADDR linespec_expression_to_pc (char **exp_ptr);
305
306 static struct symtabs_and_lines decode_objc (struct linespec_state *self,
307 linespec_p ls,
308 char **argptr);
309
310 static VEC (symtab_p) *symtabs_from_filename (const char *);
311
312 static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
313 VEC (symbolp) *function_symbols,
314 VEC (symbolp) **label_funcs_ret,
315 const char *name);
316
317 void 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);
322
323 static struct line_offset
324 linespec_parse_variable (struct linespec_state *self,
325 const char *variable);
326
327 static int symbol_to_sal (struct symtab_and_line *result,
328 int funfirstline, struct symbol *sym);
329
330 static void add_matching_symbols_to_info (const char *name,
331 struct collect_info *info,
332 struct program_space *pspace);
333
334 static void add_all_symbol_names_from_pspace (struct collect_info *info,
335 struct program_space *pspace,
336 VEC (const_char_ptr) *names);
337
338 static VEC (symtab_p) *collect_symtabs_from_filename (const char *file);
339
340 static 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);
345
346 static 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);
350
351 static void minsym_found (struct linespec_state *self, struct objfile *objfile,
352 struct minimal_symbol *msymbol,
353 struct symtabs_and_lines *result);
354
355 static int compare_symbols (const void *a, const void *b);
356
357 static int compare_msymbols (const void *a, const void *b);
358
359 static const char *find_toplevel_char (const char *s, char c);
360
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. */
364 static const char *const linespec_quote_characters = "\"\'";
365
366 /* Lexer functions. */
367
368 /* Lex a number from the input in PARSER. This only supports
369 decimal numbers.\
370 Return true if input is decimal numbers. Return false if not. */
371
372 static int
373 linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
374 {
375 tokenp->type = LSTOKEN_NUMBER;
376 LS_TOKEN_STOKEN (*tokenp).length = 0;
377 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
378
379 /* Keep any sign at the start of the stream. */
380 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
381 {
382 ++LS_TOKEN_STOKEN (*tokenp).length;
383 ++(PARSER_STREAM (parser));
384 }
385
386 while (isdigit (*PARSER_STREAM (parser)))
387 {
388 ++LS_TOKEN_STOKEN (*tokenp).length;
389 ++(PARSER_STREAM (parser));
390 }
391
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;
399 }
400
401 /* Does P represent one of the keywords? If so, return
402 the keyword. If not, return NULL. */
403
404 static const char *
405 linespec_lexer_lex_keyword (const char *p)
406 {
407 int i;
408
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;
425 }
426
427 /* Does STRING represent an Ada operator? If so, return the length
428 of the decoded operator name. If not, return 0. */
429
430 static int
431 is_ada_operator (const char *string)
432 {
433 const struct ada_opname_map *mapping;
434
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);
442 }
443
444 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
445 the location of QUOTE_CHAR, or NULL if not found. */
446
447 static const char *
448 skip_quote_char (const char *string, char quote_char)
449 {
450 const char *p, *last;
451
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 }
459
460 return last;
461 }
462
463 /* Make a writable copy of the string given in TOKEN, trimming
464 any trailing whitespace. */
465
466 static char *
467 copy_token_string (linespec_token token)
468 {
469 char *str, *s;
470
471 if (token.type == LSTOKEN_KEYWORD)
472 return xstrdup (LS_TOKEN_KEYWORD (token));
473
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';
478
479 return str;
480 }
481
482 /* Does P represent the end of a quote-enclosed linespec? */
483
484 static int
485 is_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));
491 }
492
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. */
496
497 static char *
498 find_parameter_list_end (char *input)
499 {
500 char end_char, start_char;
501 int depth;
502 char *p;
503
504 start_char = *input;
505 if (start_char == '(')
506 end_char = ')';
507 else if (start_char == '<')
508 end_char = '>';
509 else
510 return NULL;
511
512 p = input;
513 depth = 0;
514 while (*p)
515 {
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;
527 }
528
529 return p;
530 }
531
532
533 /* Lex a string from the input in PARSER. */
534
535 static linespec_token
536 linespec_lexer_lex_string (linespec_parser *parser)
537 {
538 linespec_token token;
539 char *start = PARSER_STREAM (parser);
540
541 token.type = LSTOKEN_STRING;
542
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);
549
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));
555
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 }
565
566 /* The input does not represent an Ada operator -- fall through
567 to normal quoted string handling. */
568 }
569
570 /* Skip past the beginning quote. */
571 ++(PARSER_STREAM (parser));
572
573 /* Mark the start of the string. */
574 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
575
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
703 static linespec_token
704 linespec_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':
732 if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
733 parser->lexer.current = linespec_lexer_lex_string (parser);
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
782 static linespec_token
783 linespec_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
791 static linespec_token
792 linespec_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
808 static void
809 add_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
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. */
823
824 static void
825 add_sal_to_sals (struct linespec_state *self,
826 struct symtabs_and_lines *sals,
827 struct symtab_and_line *sal,
828 const char *symname, int literal_canonical)
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 *));
838 if (!literal_canonical && sal->symtab && sal->symtab->filename)
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 }
854 else if (symname != NULL)
855 canonical_name = xstrdup (symname);
856
857 self->canonical_names[sals->nelts - 1] = canonical_name;
858 }
859 }
860
861 /* A hash function for address_entry. */
862
863 static hashval_t
864 hash_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
875 static int
876 eq_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
888 static int
889 maybe_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
909 struct 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
922 static int
923 iterate_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
936 struct 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
948 static int
949 iterate_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
964 static void
965 iterate_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)
988 continue;
989
990 set_current_program_space (pspace);
991
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,
1000 &matcher_data);
1001
1002 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, symtab)
1003 {
1004 struct block *block;
1005
1006 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1007 LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
1008
1009 if (include_inline)
1010 {
1011 struct symbol_and_data_callback cad = { callback, data };
1012 int i;
1013
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);
1020 }
1021 }
1022 }
1023 }
1024 }
1025 }
1026
1027 /* Returns the block to be used for symbol searches for the given SYMTAB,
1028 which may be NULL. */
1029
1030 static struct block *
1031 get_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
1051 /* A helper for find_method. This finds all methods in type T which
1052 match NAME. It adds matching symbol names to RESULT_NAMES, and
1053 adds T's direct superclasses to SUPERCLASSES. */
1054
1055 static void
1056 find_methods (struct type *t, const char *name,
1057 VEC (const_char_ptr) **result_names,
1058 VEC (typep) **superclasses)
1059 {
1060 int i1 = 0;
1061 int ibase;
1062 const char *class_name = type_name_no_tag (t);
1063
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. */
1067 if (class_name)
1068 {
1069 int method_counter;
1070 int name_len = strlen (name);
1071
1072 CHECK_TYPEDEF (t);
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 {
1082 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
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
1095 if (strcmp_iw (method_name, name) == 0)
1096 {
1097 int field_counter;
1098
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 }
1114 }
1115 }
1116
1117 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1118 VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
1119 }
1120
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
1123 strings. Also, ignore the char within a template name, like a ','
1124 within foo<int, int>. */
1125
1126 static const char *
1127 find_toplevel_char (const char *s, char c)
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. */
1132 int depth = 0; /* Number of unclosed parens we've seen. */
1133 const char *scan;
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;
1148 else if (*scan == '(' || *scan == '<')
1149 depth++;
1150 else if ((*scan == ')' || *scan == '>') && depth > 0)
1151 depth--;
1152 }
1153
1154 return 0;
1155 }
1156
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. */
1160
1161 static const char *
1162 find_toplevel_string (const char *haystack, const char *needle)
1163 {
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;
1185 }
1186
1187 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
1188 and store the result in SELF->CANONICAL. */
1189
1190 static void
1191 filter_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
1223 static void
1224 convert_results_to_lsals (struct linespec_state *self,
1225 struct symtabs_and_lines *result)
1226 {
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
1239 static void
1240 decode_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;
1246 int i;
1247 struct cleanup *old_chain;
1248 VEC (const_char_ptr) *item_names = NULL, *filters = NULL;
1249 struct get_number_or_range_state state;
1250
1251 gdb_assert (select_mode != multiple_symbols_all);
1252 gdb_assert (self->canonical != NULL);
1253
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)
1257 {
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]);
1273 }
1274
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)
1282 {
1283 do_cleanups (old_chain);
1284 convert_results_to_lsals (self, result);
1285 return;
1286 }
1287
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
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)
1299 {
1300 prompt = "> ";
1301 }
1302 args = command_line_input (prompt, 0, "overload-choice");
1303
1304 if (args == 0 || *args == 0)
1305 error_no_arg (_("one or more choice numbers"));
1306
1307 init_number_or_range (&state, args);
1308 while (!state.finished)
1309 {
1310 int num;
1311
1312 num = get_number_or_range (&state);
1313
1314 if (num == 0)
1315 error (_("canceled"));
1316 else if (num == 1)
1317 {
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;
1327 }
1328
1329 num -= 2;
1330 if (num >= VEC_length (const_char_ptr, item_names))
1331 printf_unfiltered (_("No choice number %d.\n"), num);
1332 else
1333 {
1334 const char *elt = VEC_index (const_char_ptr, item_names, num);
1335
1336 if (elt != NULL)
1337 {
1338 VEC_safe_push (const_char_ptr, filters, elt);
1339 VEC_replace (const_char_ptr, item_names, num, NULL);
1340 }
1341 else
1342 {
1343 printf_unfiltered (_("duplicate request for %d ignored.\n"),
1344 num);
1345 }
1346 }
1347 }
1348
1349 filter_results (self, result, filters);
1350 do_cleanups (old_chain);
1351 }
1352
1353 \f
1354
1355 /* The parser of linespec itself. */
1356
1357 /* Throw an appropriate error when SYMBOL is not found (optionally in
1358 FILENAME). */
1359
1360 static void ATTRIBUTE_NORETURN
1361 symbol_not_found_error (const char *symbol, const char *filename)
1362 {
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 }
1397 }
1398
1399 /* Throw an appropriate error when an unexpected token is encountered
1400 in the input. */
1401
1402 static void ATTRIBUTE_NORETURN
1403 unexpected_linespec_error (linespec_parser *parser)
1404 {
1405 linespec_token token;
1406 static const char * token_type_strings[]
1407 = {"keyword", "colon", "string", "number", "comma", "end of input"};
1408
1409 /* Get the token that generated the error. */
1410 token = linespec_lexer_lex_one (parser);
1411
1412 /* Finally, throw the error. */
1413 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1414 || token.type == LSTOKEN_KEYWORD)
1415 {
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
1433 static struct line_offset
1434 linespec_parse_line_offset (const char *string)
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
1456 static void
1457 linespec_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)
1525 {
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)
1575 {
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);
1611 }
1612 }
1613 else
1614 {
1615 /* Trailing ':' in the input. Issue an error. */
1616 unexpected_linespec_error (parser);
1617 }
1618 }
1619 }
1620
1621 /* Canonicalize the linespec contained in LS. The result is saved into
1622 STATE->canonical. */
1623
1624 static void
1625 canonicalize_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 }
1691 }
1692
1693 /* Given a line offset in LS, construct the relevant SALs. */
1694
1695 static struct symtabs_and_lines
1696 create_sals_line_offset (struct linespec_state *self,
1697 linespec_p ls)
1698 {
1699 struct symtabs_and_lines values;
1700 struct symtab_and_line val;
1701 int use_default = 0;
1702
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)
1715 {
1716 set_current_program_space (self->program_space);
1717
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 }
1727
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)
1789 {
1790 set_current_program_space (intermediate_results.sals[i].pspace);
1791
1792 filter[i] = 1;
1793 blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
1794 intermediate_results.sals[i].section);
1795 }
1796
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 }
1809
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);
1816
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],
1823 sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
1824 }
1825
1826 do_cleanups (cleanup);
1827 }
1828
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 }
1838
1839 return values;
1840 }
1841
1842 /* Create and return SALs from the linespec LS. */
1843
1844 static struct symtabs_and_lines
1845 convert_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 {
1851 struct symtab_and_line sal;
1852
1853 /* We have an expression. No other attribute is allowed. */
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);
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 {
1869 if (symbol_to_sal (&sal, state->funfirstline, sym))
1870 add_sal_to_sals (state, &sals, &sal,
1871 SYMBOL_NATURAL_NAME (sym), 0);
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);
1895 if (symbol_to_sal (&sal, state->funfirstline, sym)
1896 && maybe_add_address (state->addr_set, pspace, sal.pc))
1897 add_sal_to_sals (state, &sals, &sal,
1898 SYMBOL_NATURAL_NAME (sym), 0);
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 {
1913 pspace = elem->objfile->pspace;
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 }
1941
1942 /* Parse a string that specifies a linespec.
1943 Pass the address of a char * variable; that variable will be
1944 advanced over the characters actually parsed.
1945
1946 The basic grammar of linespecs:
1947
1948 linespec -> expr_spec | var_spec | basic_spec
1949 expr_spec -> '*' STRING
1950 var_spec -> '$' (STRING | NUMBER)
1951
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
1956
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.
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
1984 line specs there if necessary. Currently overloaded member functions and
1985 line numbers or static functions without a filename yield a canonical
1986 line spec. The array and the line spec strings are allocated on the heap,
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.
1991 Also, the line number returned may be invalid. */
1992
1993 /* Parse the linespec in ARGPTR. */
1994
1995 static struct symtabs_and_lines
1996 parse_linespec (linespec_parser *parser, char **argptr)
1997 {
1998 linespec_token token;
1999 struct symtabs_and_lines values;
2000 volatile struct gdb_exception file_exception;
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;
2011
2012 end = skip_quote_char (*argptr + 1, **argptr);
2013 if (end != NULL && is_closing_quote_enclosed (end))
2014 {
2015 /* Here's the special case. Skip ARGPTR past the initial
2016 quote. */
2017 ++(*argptr);
2018 parser->is_quote_enclosed = 1;
2019 }
2020 }
2021
2022 parser->lexer.saved_arg = *argptr;
2023 parser->lexer.stream = argptr;
2024 file_exception.reason = 0;
2025
2026 /* Initialize the default symtab and line offset. */
2027 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2028 &PARSER_STATE (parser)->default_line);
2029
2030 /* Objective-C shortcut. */
2031 values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), argptr);
2032 if (values.sals != NULL)
2033 return values;
2034
2035 /* Start parsing. */
2036
2037 /* Get the first token. */
2038 token = linespec_lexer_lex_one (parser);
2039
2040 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2041 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '*')
2042 {
2043 char *expr, *copy;
2044
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;
2051
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 }
2060
2061 /* Consume the token. */
2062 linespec_lexer_consume_token (parser);
2063
2064 goto convert_to_sals;
2065 }
2066 else if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
2067 {
2068 char *var;
2069
2070 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2071 VEC_safe_push (symtab_p, PARSER_RESULT (parser)->file_symtabs, NULL);
2072
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);
2078
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);
2084
2085 /* Consume this token. */
2086 linespec_lexer_consume_token (parser);
2087
2088 goto convert_to_sals;
2089 }
2090
2091 do_cleanups (cleanup);
2092 }
2093 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
2094 unexpected_linespec_error (parser);
2095
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);
2099
2100 if (token.type == LSTOKEN_COLON)
2101 {
2102 char *user_filename;
2103
2104 /* Get the current token again and extract the filename. */
2105 token = linespec_lexer_lex_one (parser);
2106 user_filename = copy_token_string (token);
2107
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 }
2114
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;
2119
2120 /* Get the next token. */
2121 token = linespec_lexer_consume_token (parser);
2122
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);
2130
2131 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2132 VEC_safe_push (symtab_p, PARSER_RESULT (parser)->file_symtabs, NULL);
2133 }
2134 }
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)
2138 {
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);
2143 }
2144 else
2145 {
2146 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2147 VEC_safe_push (symtab_p, PARSER_RESULT (parser)->file_symtabs, NULL);
2148 }
2149
2150 /* Parse the rest of the linespec. */
2151 linespec_parse_basic (parser);
2152
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)
2157 {
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);
2162
2163 /* Otherwise, the symbol is not found. */
2164 symbol_not_found_error (PARSER_RESULT (parser)->function_name,
2165 PARSER_RESULT (parser)->source_filename);
2166 }
2167
2168 convert_to_sals:
2169
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;
2175
2176 /* Convert the data in PARSER_RESULT to SALs. */
2177 values = convert_linespec_to_sals (PARSER_STATE (parser),
2178 PARSER_RESULT (parser));
2179
2180 return values;
2181 }
2182
2183
2184 /* A constructor for linespec_state. */
2185
2186 static void
2187 linespec_state_constructor (struct linespec_state *self,
2188 int flags, const struct language_defn *language,
2189 struct symtab *default_symtab,
2190 int default_line,
2191 struct linespec_result *canonical)
2192 {
2193 memset (self, 0, sizeof (*self));
2194 self->language = language;
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 }
2204
2205 /* Initialize a new linespec parser. */
2206
2207 static void
2208 linespec_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)
2213 {
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. */
2222
2223 static void
2224 linespec_state_destructor (struct linespec_state *self)
2225 {
2226 htab_delete (self->addr_set);
2227 }
2228
2229 /* Delete a linespec parser. */
2230
2231 static void
2232 linespec_parser_delete (void *arg)
2233 {
2234 linespec_parser *parser = (linespec_parser *) arg;
2235
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);
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
2259 /* See linespec.h. */
2260
2261 void
2262 decode_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)
2267 {
2268 struct symtabs_and_lines result;
2269 struct cleanup *cleanups;
2270 char *arg_start = *argptr;
2271 VEC (const_char_ptr) *filters = NULL;
2272 linespec_parser parser;
2273 struct linespec_state *state;
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
2284 linespec_parser_new (&parser, flags, current_language, default_symtab,
2285 default_line, canonical);
2286 cleanups = make_cleanup (linespec_parser_delete, &parser);
2287 save_current_program_space ();
2288
2289 result = parse_linespec (&parser, argptr);
2290 state = PARSER_STATE (&parser);
2291
2292 gdb_assert (result.nelts == 1 || canonical->pre_expanded);
2293 gdb_assert (canonical->addr_string != NULL);
2294 canonical->pre_expanded = 1;
2295
2296 /* Arrange for allocated canonical names to be freed. */
2297 if (result.nelts > 0)
2298 {
2299 int i;
2300
2301 make_cleanup (xfree, state->canonical_names);
2302 for (i = 0; i < result.nelts; ++i)
2303 {
2304 gdb_assert (state->canonical_names[i] != NULL);
2305 make_cleanup (xfree, state->canonical_names[i]);
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);
2323 filter_results (state, &result, filters);
2324 }
2325 else
2326 convert_results_to_lsals (state, &result);
2327 }
2328 else
2329 decode_line_2 (state, &result, select_mode);
2330
2331 do_cleanups (cleanups);
2332 }
2333
2334 /* See linespec.h. */
2335
2336 struct symtabs_and_lines
2337 decode_line_1 (char **argptr, int flags,
2338 struct symtab *default_symtab,
2339 int default_line)
2340 {
2341 struct symtabs_and_lines result;
2342 linespec_parser parser;
2343 struct cleanup *cleanups;
2344
2345 linespec_parser_new (&parser, flags, current_language, default_symtab,
2346 default_line, NULL);
2347 cleanups = make_cleanup (linespec_parser_delete, &parser);
2348 save_current_program_space ();
2349
2350 result = parse_linespec (&parser, argptr);
2351
2352 do_cleanups (cleanups);
2353 return result;
2354 }
2355
2356 /* See linespec.h. */
2357
2358 struct symtabs_and_lines
2359 decode_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
2381 struct symtabs_and_lines
2382 decode_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
2401 \f
2402
2403 /* First, some functions to initialize stuff at the beggining of the
2404 function. */
2405
2406 static void
2407 initialize_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
2424 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2425 advancing EXP_PTR past any parsed text. */
2426
2427 static CORE_ADDR
2428 linespec_expression_to_pc (char **exp_ptr)
2429 {
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
2436 (*exp_ptr)++;
2437 return value_as_address (parse_to_comma_and_eval (exp_ptr));
2438 }
2439
2440 \f
2441
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
2449 static struct symtabs_and_lines
2450 decode_objc (struct linespec_state *self, linespec_p ls, char **argptr)
2451 {
2452 struct collect_info info;
2453 VEC (const_char_ptr) *symbol_names = NULL;
2454 struct symtabs_and_lines values;
2455 char *new_argptr;
2456 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
2457 &symbol_names);
2458
2459 info.state = self;
2460 info.file_symtabs = NULL;
2461 VEC_safe_push (symtab_p, info.file_symtabs, NULL);
2462 make_cleanup (VEC_cleanup (symtab_p), &info.file_symtabs);
2463 info.result.symbols = NULL;
2464 info.result.minimal_symbols = NULL;
2465 values.nelts = 0;
2466 values.sals = NULL;
2467
2468 new_argptr = find_imps (*argptr, &symbol_names);
2469 if (VEC_empty (const_char_ptr, symbol_names))
2470 {
2471 do_cleanups (cleanup);
2472 return values;
2473 }
2474
2475 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
2476
2477 if (!VEC_empty (symbolp, info.result.symbols)
2478 || !VEC_empty (minsym_and_objfile_d, info.result.minimal_symbols))
2479 {
2480 char *saved_arg;
2481
2482 saved_arg = alloca (new_argptr - *argptr + 1);
2483 memcpy (saved_arg, *argptr, new_argptr - *argptr);
2484 saved_arg[new_argptr - *argptr] = '\0';
2485
2486 ls->function_name = xstrdup (saved_arg);
2487 ls->function_symbols = info.result.symbols;
2488 ls->minimal_symbols = info.result.minimal_symbols;
2489 values = convert_linespec_to_sals (self, ls);
2490
2491 if (self->canonical)
2492 {
2493 self->canonical->pre_expanded = 1;
2494 if (ls->source_filename)
2495 self->canonical->addr_string
2496 = xstrprintf ("%s:%s", ls->source_filename, saved_arg);
2497 else
2498 self->canonical->addr_string = xstrdup (saved_arg);
2499 }
2500 }
2501
2502 *argptr = new_argptr;
2503
2504 do_cleanups (cleanup);
2505
2506 return values;
2507 }
2508
2509 /* An instance of this type is used when collecting prefix symbols for
2510 decode_compound. */
2511
2512 struct decode_compound_collector
2513 {
2514 /* The result vector. */
2515 VEC (symbolp) *symbols;
2516
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 };
2521
2522 /* A callback for iterate_over_symbols that is used by
2523 lookup_prefix_sym to collect type symbols. */
2524
2525 static int
2526 collect_one_symbol (struct symbol *sym, void *d)
2527 {
2528 struct decode_compound_collector *collector = d;
2529 void **slot;
2530 struct type *t;
2531
2532 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
2533 return 1; /* Continue iterating. */
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)
2540 return 1; /* Continue iterating. */
2541
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
2549 return 1; /* Continue iterating. */
2550 }
2551
2552 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
2553
2554 static VEC (symbolp) *
2555 lookup_prefix_sym (struct linespec_state *state, VEC (symtab_p) *file_symtabs,
2556 const char *class_name)
2557 {
2558 int ix;
2559 struct symtab *elt;
2560 struct decode_compound_collector collector;
2561 struct cleanup *outer;
2562 struct cleanup *cleanup;
2563
2564 collector.symbols = NULL;
2565 outer = make_cleanup (VEC_cleanup (symbolp), &collector.symbols);
2566
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);
2571
2572 for (ix = 0; VEC_iterate (symtab_p, file_symtabs, ix, elt); ++ix)
2573 {
2574 if (elt == NULL)
2575 {
2576 iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN,
2577 collect_one_symbol, &collector,
2578 NULL, 0);
2579 iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN,
2580 collect_one_symbol, &collector,
2581 NULL, 0);
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);
2592 LA_ITERATE_OVER_SYMBOLS (search_block, class_name, STRUCT_DOMAIN,
2593 collect_one_symbol, &collector);
2594 LA_ITERATE_OVER_SYMBOLS (search_block, class_name, VAR_DOMAIN,
2595 collect_one_symbol, &collector);
2596 }
2597 }
2598
2599 do_cleanups (cleanup);
2600 discard_cleanups (outer);
2601 return collector.symbols;
2602 }
2603
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
2608 static int
2609 compare_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. */
2635
2636 static int
2637 compare_msymbols (const void *a, const void *b)
2638 {
2639 const struct minsym_and_objfile *sa = a;
2640 const struct minsym_and_objfile *sb = b;
2641 uintptr_t uia, uib;
2642
2643 uia = (uintptr_t) sa->objfile->pspace;
2644 uib = (uintptr_t) sa->objfile->pspace;
2645
2646 if (uia < uib)
2647 return -1;
2648 if (uia > uib)
2649 return 1;
2650
2651 uia = (uintptr_t) sa->minsym;
2652 uib = (uintptr_t) sb->minsym;
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
2667 static void
2668 add_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
2679 static void
2680 find_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;
2702
2703 iter_classes = new_supers;
2704 }
2705
2706 do_cleanups (cleanup);
2707 }
2708
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). */
2712
2713 static void
2714 find_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)
2718 {
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;
2726
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;
2735 info.file_symtabs = file_symtabs;
2736 info.result.symbols = NULL;
2737 info.result.minimal_symbols = NULL;
2738
2739 /* Iterate over all the types, looking for the names of existing
2740 methods matching METHOD_NAME. If we cannot find a direct method in a
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));
2764 find_methods (t, method_name, &result_names, &superclass_vec);
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)))))
2772 {
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)
2776 find_superclass_methods (superclass_vec, method_name,
2777 &result_names);
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);
2786 }
2787 }
2788
2789 if (!VEC_empty (symbolp, info.result.symbols)
2790 || !VEC_empty (minsym_and_objfile_d, info.result.minimal_symbols))
2791 {
2792 *symbols = info.result.symbols;
2793 *minsyms = info.result.minimal_symbols;
2794 do_cleanups (cleanup);
2795 return;
2796 }
2797
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"));
2801 }
2802
2803 \f
2804
2805 /* This object is used when collecting all matching symtabs. */
2806
2807 struct 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
2818 static int
2819 add_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)
2826 {
2827 *slot = symtab;
2828 VEC_safe_push (symtab_p, data->symtabs, symtab);
2829 }
2830
2831 return 0;
2832 }
2833
2834 /* Given a file name, return a VEC of all matching symtabs. */
2835
2836 static VEC (symtab_p) *
2837 collect_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 }
2857
2858 do_cleanups (cleanups);
2859 return collector.symtabs;
2860 }
2861
2862 /* Return all the symtabs associated to the FILENAME. */
2863
2864 static VEC (symtab_p) *
2865 symtabs_from_filename (const char *filename)
2866 {
2867 VEC (symtab_p) *result;
2868
2869 result = collect_symtabs_from_filename (filename);
2870
2871 if (VEC_empty (symtab_p, result))
2872 {
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);
2878 }
2879
2880 return result;
2881 }
2882
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. */
2886
2887 static void
2888 find_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)
2892 {
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);
2897
2898 info.state = state;
2899 info.result.symbols = NULL;
2900 info.result.minimal_symbols = NULL;
2901 info.file_symtabs = file_symtabs;
2902
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;
2916 }
2917 else
2918 *symbols = info.result.symbols;
2919
2920 if (VEC_empty (minsym_and_objfile_d, info.result.minimal_symbols))
2921 {
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. */
2931
2932 void
2933 find_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;
2944
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 }
2954
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 }
2961
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 }
2972
2973 last = NULL;
2974 while (p != NULL)
2975 {
2976 last = p;
2977 p = find_toplevel_string (p + strlen (scope_op), scope_op);
2978 }
2979
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;
2987 }
2988
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';
2995
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);
3001
3002 /* Find a list of classes named KLASS. */
3003 classes = lookup_prefix_sym (state, file_symtabs, klass);
3004 make_cleanup (VEC_cleanup (symbolp), &classes);
3005 if (!VEC_empty (symbolp, classes))
3006 {
3007 /* Now locate a list of suitable methods named METHOD. */
3008 TRY_CATCH (except, RETURN_MASK_ERROR)
3009 {
3010 find_method (state, file_symtabs, klass, method, classes,
3011 symbols, minsyms);
3012 }
3013
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);
3025 }
3026
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);
3031 }
3032
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. */
3035
3036 static VEC (symbolp) *
3037 find_label_symbols (struct linespec_state *self,
3038 VEC (symbolp) *function_symbols,
3039 VEC (symbolp) **label_funcs_ret, const char *name)
3040 {
3041 int ix;
3042 struct block *block;
3043 struct symbol *sym;
3044 struct symbol *fn_sym;
3045 VEC (symbolp) *result = NULL;
3046
3047 if (function_symbols == NULL)
3048 {
3049 set_current_program_space (self->program_space);
3050 block = get_search_block (NULL);
3051
3052 for (;
3053 block && !BLOCK_FUNCTION (block);
3054 block = BLOCK_SUPERBLOCK (block))
3055 ;
3056 if (!block)
3057 return NULL;
3058 fn_sym = BLOCK_FUNCTION (block);
3059
3060 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);
3061
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)
3072 {
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 }
3082 }
3083 }
3084
3085 return result;
3086 }
3087
3088 \f
3089
3090 /* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3091
3092 static void
3093 decode_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
3119 add_sal_to_sals (self, values, &val, NULL, 0);
3120 }
3121 }
3122
3123 /* A helper for create_sals_line_offset that iterates over the symtabs,
3124 adding lines to the VEC. */
3125
3126 static void
3127 decode_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;
3135
3136 for (ix = 0; VEC_iterate (symtab_p, ls->file_symtabs, ix, elt); ++ix)
3137 {
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);
3144
3145 set_current_program_space (SYMTAB_PSPACE (elt));
3146
3147 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
3148 for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
3149 {
3150 struct symtab_and_line sal;
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);
3158 }
3159
3160 VEC_free (CORE_ADDR, pcs);
3161 }
3162 }
3163
3164 \f
3165
3166 /* Return the line offset represented by VARIABLE. */
3167
3168 static struct line_offset
3169 linespec_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};
3174
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. */
3181 {
3182 /* We have a value history reference. */
3183 struct value *val_history;
3184
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 }
3217 }
3218
3219 return offset;
3220 }
3221 \f
3222
3223 /* A callback used to possibly add a symbol to the results. */
3224
3225 static int
3226 collect_symbols (struct symbol *sym, void *data)
3227 {
3228 struct collect_info *info = data;
3229
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);
3234 return 1; /* Continue iterating. */
3235 }
3236
3237 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
3238 linespec; return the SAL in RESULT. */
3239
3240 static void
3241 minsym_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
3262 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
3263 add_sal_to_sals (self, result, &sal, SYMBOL_NATURAL_NAME (msymbol), 0);
3264 }
3265
3266 /* A helper struct to pass some data through
3267 iterate_over_minimal_symbols. */
3268
3269 struct 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
3277 /* The list_mode setting from the initial call. */
3278 int list_mode;
3279
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
3287 static int
3288 classify_mtype (enum minimal_symbol_type t)
3289 {
3290 switch (t)
3291 {
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;
3305 }
3306 }
3307
3308 /* Callback for qsort that sorts symbols by priority. */
3309
3310 static int
3311 compare_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
3324 static void
3325 add_minsym (struct minimal_symbol *minsym, void *d)
3326 {
3327 struct collect_minsyms *info = d;
3328 minsym_and_objfile_d mo;
3329
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:
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 }
3351 }
3352
3353 mo.minsym = minsym;
3354 mo.objfile = info->objfile;
3355 VEC_safe_push (minsym_and_objfile_d, info->msyms, &mo);
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
3362 static void
3363 search_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 {
3371 struct collect_minsyms local;
3372 struct cleanup *cleanup;
3373
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
3381 memset (&local, 0, sizeof (local));
3382 local.funfirstline = info->state->funfirstline;
3383 local.list_mode = info->state->list_mode;
3384
3385 cleanup = make_cleanup (VEC_cleanup (minsym_and_objfile_d),
3386 &local.msyms);
3387
3388 ALL_OBJFILES (objfile)
3389 {
3390 local.objfile = objfile;
3391 iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
3392 }
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
3418 VEC_safe_push (minsym_and_objfile_d,
3419 info->result.minimal_symbols, item);
3420 }
3421 }
3422
3423 do_cleanups (cleanup);
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. */
3430
3431 static void
3432 add_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;
3438
3439 for (ix = 0; VEC_iterate (symtab_p, info->file_symtabs, ix, elt); ++ix)
3440 {
3441 if (elt == NULL)
3442 {
3443 iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN,
3444 collect_symbols, info,
3445 pspace, 1);
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 }
3459 }
3460
3461 \f
3462
3463 /* Now come some functions that are called from multiple places within
3464 decode_line_1. */
3465
3466 static int
3467 symbol_to_sal (struct symtab_and_line *result,
3468 int funfirstline, struct symbol *sym)
3469 {
3470 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
3471 {
3472 *result = find_function_start_sal (sym, funfirstline);
3473 return 1;
3474 }
3475 else
3476 {
3477 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
3478 {
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;
3486 }
3487 else if (funfirstline)
3488 {
3489 /* Nothing. */
3490 }
3491 else if (SYMBOL_LINE (sym) != 0)
3492 {
3493 /* We know its line number. */
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;
3499 }
3500 }
3501
3502 return 0;
3503 }
3504
3505 /* See the comment in linespec.h. */
3506
3507 void
3508 init_linespec_result (struct linespec_result *lr)
3509 {
3510 memset (lr, 0, sizeof (*lr));
3511 }
3512
3513 /* See the comment in linespec.h. */
3514
3515 void
3516 destroy_linespec_result (struct linespec_result *ls)
3517 {
3518 int i;
3519 struct linespec_sals *lsal;
3520
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 }
3529
3530 /* Cleanup function for a linespec_result. */
3531
3532 static void
3533 cleanup_linespec_result (void *a)
3534 {
3535 destroy_linespec_result (a);
3536 }
3537
3538 /* See the comment in linespec.h. */
3539
3540 struct cleanup *
3541 make_cleanup_destroy_linespec_result (struct linespec_result *ls)
3542 {
3543 return make_cleanup (cleanup_linespec_result, ls);
3544 }
This page took 0.148061 seconds and 5 git commands to generate.