1 /* Parser for linespec for the GNU debugger, GDB.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
32 #include "completer.h"
34 #include "parser-defs.h"
36 /* We share this one with symtab.c, but it is not exported widely. */
38 extern char *operator_chars (char *, char **);
40 /* Prototypes for local functions */
42 static void initialize_defaults (struct symtab
**default_symtab
,
45 static void set_flags (char *arg
, int *is_quoted
, char **paren_pointer
);
47 static struct symtabs_and_lines
decode_indirect (char **argptr
);
49 static char *locate_first_half (char **argptr
, int *is_quote_enclosed
);
51 static struct symtabs_and_lines
decode_compound (char **argptr
,
57 static NORETURN
void cplusplus_error (const char *name
,
59 ATTR_NORETURN
ATTR_FORMAT (printf
, 2, 3);
61 static int total_number_of_methods (struct type
*type
);
63 static int find_methods (struct type
*, char *, struct symbol
**);
65 static void build_canonical_line_spec (struct symtab_and_line
*,
68 static char *find_toplevel_char (char *s
, char c
);
70 static struct symtabs_and_lines
decode_line_2 (struct symbol
*[],
73 static struct symtab
*symtab_from_filename (char **argptr
,
74 char *p
, int is_quote_enclosed
);
77 symtabs_and_lines
decode_all_digits (char **argptr
,
78 struct symtab
*default_symtab
,
84 static struct symtabs_and_lines
decode_dollar (char *copy
,
86 struct symtab
*default_symtab
,
90 static struct symtabs_and_lines
decode_variable (char *copy
,
96 symtabs_and_lines
symbol_found (int funfirstline
,
101 struct symtab
*sym_symtab
);
104 symtabs_and_lines
minsym_found (int funfirstline
,
105 struct minimal_symbol
*msymbol
);
107 /* Helper functions. */
109 /* Issue a helpful hint on using the command completion feature on
110 single quoted demangled C++ symbols as part of the completion
114 cplusplus_error (const char *name
, const char *fmt
, ...)
116 struct ui_file
*tmp_stream
;
117 tmp_stream
= mem_fileopen ();
118 make_cleanup_ui_file_delete (tmp_stream
);
122 va_start (args
, fmt
);
123 vfprintf_unfiltered (tmp_stream
, fmt
, args
);
127 while (*name
== '\'')
129 fprintf_unfiltered (tmp_stream
,
130 ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
131 "(Note leading single quote.)"),
133 error_stream (tmp_stream
);
136 /* Return the number of methods described for TYPE, including the
137 methods from types it derives from. This can't be done in the symbol
138 reader because the type of the baseclass might still be stubbed
139 when the definition of the derived class is parsed. */
142 total_number_of_methods (struct type
*type
)
147 CHECK_TYPEDEF (type
);
148 if (TYPE_CPLUS_SPECIFIC (type
) == NULL
)
150 count
= TYPE_NFN_FIELDS_TOTAL (type
);
152 for (n
= 0; n
< TYPE_N_BASECLASSES (type
); n
++)
153 count
+= total_number_of_methods (TYPE_BASECLASS (type
, n
));
158 /* Recursive helper function for decode_line_1.
159 Look for methods named NAME in type T.
160 Return number of matches.
161 Put matches in SYM_ARR, which should have been allocated with
162 a size of total_number_of_methods (T) * sizeof (struct symbol *).
163 Note that this function is g++ specific. */
166 find_methods (struct type
*t
, char *name
, struct symbol
**sym_arr
)
170 char *class_name
= type_name_no_tag (t
);
172 /* Ignore this class if it doesn't have a name. This is ugly, but
173 unless we figure out how to get the physname without the name of
174 the class, then the loop can't do any good. */
176 && (lookup_symbol (class_name
, (struct block
*) NULL
,
177 STRUCT_NAMESPACE
, (int *) NULL
,
178 (struct symtab
**) NULL
)))
181 int name_len
= strlen (name
);
185 /* Loop over each method name. At this level, all overloads of a name
186 are counted as a single name. There is an inner loop which loops over
189 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
194 char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
197 if (strncmp (method_name
, "__", 2) == 0 ||
198 strncmp (method_name
, "op", 2) == 0 ||
199 strncmp (method_name
, "type", 4) == 0)
201 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
202 method_name
= dem_opname
;
203 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
204 method_name
= dem_opname
;
207 if (strcmp_iw (name
, method_name
) == 0)
208 /* Find all the overloaded methods with that name. */
209 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
216 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
218 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
222 tmp_name
= gdb_mangle_name (t
,
225 phys_name
= alloca (strlen (tmp_name
) + 1);
226 strcpy (phys_name
, tmp_name
);
230 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
232 /* Destructor is handled by caller, dont add it to the list */
233 if (is_destructor_name (phys_name
) != 0)
236 sym_arr
[i1
] = lookup_symbol (phys_name
,
239 (struct symtab
**) NULL
);
244 /* This error message gets printed, but the method
245 still seems to be found
246 fputs_filtered("(Cannot find method ", gdb_stdout);
247 fprintf_symbol_filtered (gdb_stdout, phys_name,
249 DMGL_PARAMS | DMGL_ANSI);
250 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
254 else if (strncmp (class_name
, name
, name_len
) == 0
255 && (class_name
[name_len
] == '\0'
256 || class_name
[name_len
] == '<'))
258 /* For GCC 3.x and stabs, constructors and destructors have names
259 like __base_ctor and __complete_dtor. Check the physname for now
260 if we're looking for a constructor. */
262 = TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
269 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
271 /* GCC 3.x will never produce stabs stub methods, so we don't need
272 to handle this case. */
273 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
275 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
276 if (! is_constructor_name (phys_name
))
279 /* If this method is actually defined, include it in the
281 sym_arr
[i1
] = lookup_symbol (phys_name
,
284 (struct symtab
**) NULL
);
292 /* Only search baseclasses if there is no match yet, since names in
293 derived classes override those in baseclasses.
295 FIXME: The above is not true; it is only true of member functions
296 if they have the same number of arguments (??? - section 13.1 of the
297 ARM says the function members are not in the same scope but doesn't
298 really spell out the rules in a way I understand. In any case, if
299 the number of arguments differ this is a case in which we can overload
300 rather than hiding without any problem, and gcc 2.4.5 does overload
301 rather than hiding in this case). */
304 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
305 i1
+= find_methods (TYPE_BASECLASS (t
, ibase
), name
, sym_arr
+ i1
);
310 /* Helper function for decode_line_1.
311 Build a canonical line spec in CANONICAL if it is non-NULL and if
312 the SAL has a symtab.
313 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
314 If SYMNAME is NULL the line number from SAL is used and the canonical
315 line spec is `filename:linenum'. */
318 build_canonical_line_spec (struct symtab_and_line
*sal
, char *symname
,
321 char **canonical_arr
;
322 char *canonical_name
;
324 struct symtab
*s
= sal
->symtab
;
326 if (s
== (struct symtab
*) NULL
327 || s
->filename
== (char *) NULL
328 || canonical
== (char ***) NULL
)
331 canonical_arr
= (char **) xmalloc (sizeof (char *));
332 *canonical
= canonical_arr
;
334 filename
= s
->filename
;
337 canonical_name
= xmalloc (strlen (filename
) + strlen (symname
) + 2);
338 sprintf (canonical_name
, "%s:%s", filename
, symname
);
342 canonical_name
= xmalloc (strlen (filename
) + 30);
343 sprintf (canonical_name
, "%s:%d", filename
, sal
->line
);
345 canonical_arr
[0] = canonical_name
;
350 /* Find an instance of the character C in the string S that is outside
351 of all parenthesis pairs, single-quoted strings, and double-quoted
352 strings. Also, ignore the char within a template name, like a ','
353 within foo<int, int>. */
356 find_toplevel_char (char *s
, char c
)
358 int quoted
= 0; /* zero if we're not in quotes;
359 '"' if we're in a double-quoted string;
360 '\'' if we're in a single-quoted string. */
361 int depth
= 0; /* number of unclosed parens we've seen */
364 for (scan
= s
; *scan
; scan
++)
370 else if (*scan
== '\\' && *(scan
+ 1))
373 else if (*scan
== c
&& ! quoted
&& depth
== 0)
375 else if (*scan
== '"' || *scan
== '\'')
377 else if (*scan
== '(' || *scan
== '<')
379 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
386 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
387 operate on (ask user if necessary).
388 If CANONICAL is non-NULL return a corresponding array of mangled names
389 as canonical line specs there. */
391 static struct symtabs_and_lines
392 decode_line_2 (struct symbol
*sym_arr
[], int nelts
, int funfirstline
,
395 struct symtabs_and_lines values
, return_values
;
400 struct cleanup
*old_chain
;
401 char **canonical_arr
= (char **) NULL
;
403 values
.sals
= (struct symtab_and_line
*)
404 alloca (nelts
* sizeof (struct symtab_and_line
));
405 return_values
.sals
= (struct symtab_and_line
*)
406 xmalloc (nelts
* sizeof (struct symtab_and_line
));
407 old_chain
= make_cleanup (xfree
, return_values
.sals
);
411 canonical_arr
= (char **) xmalloc (nelts
* sizeof (char *));
412 make_cleanup (xfree
, canonical_arr
);
413 memset (canonical_arr
, 0, nelts
* sizeof (char *));
414 *canonical
= canonical_arr
;
418 printf_unfiltered ("[0] cancel\n[1] all\n");
421 init_sal (&return_values
.sals
[i
]); /* initialize to zeroes */
422 init_sal (&values
.sals
[i
]);
423 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
425 values
.sals
[i
] = find_function_start_sal (sym_arr
[i
], funfirstline
);
426 printf_unfiltered ("[%d] %s at %s:%d\n",
428 SYMBOL_SOURCE_NAME (sym_arr
[i
]),
429 values
.sals
[i
].symtab
->filename
,
430 values
.sals
[i
].line
);
433 printf_unfiltered ("?HERE\n");
437 if ((prompt
= getenv ("PS2")) == NULL
)
441 args
= command_line_input (prompt
, 0, "overload-choice");
443 if (args
== 0 || *args
== 0)
444 error_no_arg ("one or more choice numbers");
452 while (*arg1
>= '0' && *arg1
<= '9')
454 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
455 error ("Arguments must be choice numbers.");
465 for (i
= 0; i
< nelts
; i
++)
467 if (canonical_arr
[i
] == NULL
)
469 symname
= SYMBOL_NAME (sym_arr
[i
]);
470 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
474 memcpy (return_values
.sals
, values
.sals
,
475 (nelts
* sizeof (struct symtab_and_line
)));
476 return_values
.nelts
= nelts
;
477 discard_cleanups (old_chain
);
478 return return_values
;
481 if (num
>= nelts
+ 2)
483 printf_unfiltered ("No choice number %d.\n", num
);
488 if (values
.sals
[num
].pc
)
492 symname
= SYMBOL_NAME (sym_arr
[num
]);
493 make_cleanup (xfree
, symname
);
494 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
496 return_values
.sals
[i
++] = values
.sals
[num
];
497 values
.sals
[num
].pc
= 0;
501 printf_unfiltered ("duplicate request for %d ignored.\n", num
);
506 while (*args
== ' ' || *args
== '\t')
509 return_values
.nelts
= i
;
510 discard_cleanups (old_chain
);
511 return return_values
;
514 /* The parser of linespec itself. */
516 /* Parse a string that specifies a line number.
517 Pass the address of a char * variable; that variable will be
518 advanced over the characters actually parsed.
522 LINENUM -- that line number in current file. PC returned is 0.
523 FILE:LINENUM -- that line in that file. PC returned is 0.
524 FUNCTION -- line number of openbrace of that function.
525 PC returned is the start of the function.
526 VARIABLE -- line number of definition of that variable.
528 FILE:FUNCTION -- likewise, but prefer functions in that file.
529 *EXPR -- line in which address EXPR appears.
531 This may all be followed by an "if EXPR", which we ignore.
533 FUNCTION may be an undebuggable function found in minimal symbol table.
535 If the argument FUNFIRSTLINE is nonzero, we want the first line
536 of real code inside a function when a function is specified, and it is
537 not OK to specify a variable or type to get its line number.
539 DEFAULT_SYMTAB specifies the file to use if none is specified.
540 It defaults to current_source_symtab.
541 DEFAULT_LINE specifies the line number to use for relative
542 line numbers (that start with signs). Defaults to current_source_line.
543 If CANONICAL is non-NULL, store an array of strings containing the canonical
544 line specs there if necessary. Currently overloaded member functions and
545 line numbers or static functions without a filename yield a canonical
546 line spec. The array and the line spec strings are allocated on the heap,
547 it is the callers responsibility to free them.
549 Note that it is possible to return zero for the symtab
550 if no file is validly specified. Callers must check that.
551 Also, the line number returned may be invalid. */
553 /* We allow single quotes in various places. This is a hideous
554 kludge, which exists because the completer can't yet deal with the
555 lack of single quotes. FIXME: write a linespec_completer which we
556 can use as appropriate instead of make_symbol_completion_list. */
558 struct symtabs_and_lines
559 decode_line_1 (char **argptr
, int funfirstline
, struct symtab
*default_symtab
,
560 int default_line
, char ***canonical
)
564 struct symtab
*s
= NULL
;
567 /* This is NULL if there are no parens in *ARGPTR, or a pointer to
568 the closing parenthesis if there are parens. */
570 /* This says whether or not something in *ARGPTR is quoted with
571 completer_quotes (i.e. with single quotes). */
573 /* Is part of *ARGPTR is enclosed in double quotes? */
574 int is_quote_enclosed
;
575 char *saved_arg
= *argptr
;
577 /* Defaults have defaults. */
579 initialize_defaults (&default_symtab
, &default_line
);
581 /* See if arg is *PC */
584 return decode_indirect (argptr
);
586 /* Set various flags.
587 * 'paren_pointer' is important for overload checking, where
588 * we allow things like:
589 * (gdb) break c::f(int)
592 set_flags (*argptr
, &is_quoted
, &paren_pointer
);
594 /* Check to see if it's a multipart linespec (with colons or
597 /* Locate the end of the first half of the linespec. */
599 p
= locate_first_half (argptr
, &is_quote_enclosed
);
601 /* Does it look like there actually were two parts? */
603 if ((p
[0] == ':' || p
[0] == '.') && paren_pointer
== NULL
)
606 *argptr
= *argptr
+ 1;
608 /* Is it a C++ or Java compound data structure? */
610 if (p
[0] == '.' || p
[1] == ':')
611 return decode_compound (argptr
, funfirstline
, canonical
,
614 /* No, the first part is a filename; set s to be that file's
615 symtab. Also, move argptr past the filename. */
617 s
= symtab_from_filename (argptr
, p
, is_quote_enclosed
);
620 /* No one really seems to know why this was added. It certainly
621 breaks the command line, though, whenever the passed
622 name is of the form ClassName::Method. This bit of code
623 singles out the class name, and if funfirstline is set (for
624 example, you are setting a breakpoint at this function),
625 you get an error. This did not occur with earlier
626 verions, so I am ifdef'ing this out. 3/29/99 */
629 /* Check if what we have till now is a symbol name */
631 /* We may be looking at a template instantiation such
632 as "foo<int>". Check here whether we know about it,
633 instead of falling through to the code below which
634 handles ordinary function names, because that code
635 doesn't like seeing '<' and '>' in a name -- the
636 skip_quoted call doesn't go past them. So see if we
637 can figure it out right now. */
639 copy
= (char *) alloca (p
- *argptr
+ 1);
640 memcpy (copy
, *argptr
, p
- *argptr
);
641 copy
[p
- *argptr
] = '\000';
642 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
645 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
646 return symbol_found (funfirstline
, canonical
, copy
, sym
,
649 /* Otherwise fall out from here and go to file/line spec
654 /* S is specified file's symtab, or 0 if no file specified.
655 arg no longer contains the file name. */
657 /* Check whether arg is all digits (and sign) */
660 if (*q
== '-' || *q
== '+')
662 while (*q
>= '0' && *q
<= '9')
665 if (q
!= *argptr
&& (*q
== 0 || *q
== ' ' || *q
== '\t' || *q
== ','))
666 /* We found a token consisting of all digits -- at least one digit. */
667 return decode_all_digits (argptr
, default_symtab
, default_line
,
670 /* Arg token is not digits => try it as a variable name
671 Find the next token (everything up to end or next whitespace). */
673 if (**argptr
== '$') /* May be a convenience variable */
674 p
= skip_quoted (*argptr
+ (((*argptr
)[1] == '$') ? 2 : 1)); /* One or two $ chars possible */
677 p
= skip_quoted (*argptr
);
679 error ("Unmatched single quote.");
681 else if (paren_pointer
!= NULL
)
683 p
= paren_pointer
+ 1;
687 p
= skip_quoted (*argptr
);
690 copy
= (char *) alloca (p
- *argptr
+ 1);
691 memcpy (copy
, *argptr
, p
- *argptr
);
692 copy
[p
- *argptr
] = '\0';
695 && copy
[0] == copy
[p
- *argptr
- 1]
696 && strchr (get_gdb_completer_quote_characters (), copy
[0]) != NULL
)
698 copy
[p
- *argptr
- 1] = '\0';
701 while (*p
== ' ' || *p
== '\t')
705 /* If it starts with $: may be a legitimate variable or routine name
706 (e.g. HP-UX millicode routines such as $$dyncall), or it may
707 be history value, or it may be a convenience variable */
710 return decode_dollar (copy
, funfirstline
, default_symtab
,
713 /* Look up that token as a variable.
714 If file specified, use that file's per-file block to start with. */
716 return decode_variable (copy
, funfirstline
, canonical
, s
);
721 /* Now, more helper functions for decode_line_1. Some conventions
722 that these functions follow:
724 Decode_line_1 typically passes along some of its arguments or local
725 variables to the subfunctions. It passes the variables by
726 reference if they are modified by the subfunction, and by value
729 Some of the functions have side effects that don't arise from
730 variables that are passed by reference. In particular, if a
731 function is passed ARGPTR as an argument, it modifies what ARGPTR
732 points to; typically, it advances *ARGPTR past whatever substring
733 it has just looked at. (If it doesn't modify *ARGPTR, then the
734 function gets passed *ARGPTR instead, which is then called ARG: see
735 set_flags, for example.) Also, functions that return a struct
736 symtabs_and_lines may modify CANONICAL, as in the description of
739 If a function returns a struct symtabs_and_lines, then that struct
740 will immediately make its way up the call chain to be returned by
741 decode_line_1. In particular, all of the functions decode_XXX
742 calculate the appropriate struct symtabs_and_lines, under the
743 assumption that their argument is of the form XXX. */
745 /* First, some functions to initialize stuff at the beggining of the
749 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
751 if (*default_symtab
== 0)
753 /* Use whatever we have for the default source line. We don't use
754 get_current_or_default_symtab_and_line as it can recurse and call
756 struct symtab_and_line cursal
=
757 get_current_source_symtab_and_line ();
759 *default_symtab
= cursal
.symtab
;
760 *default_line
= cursal
.line
;
765 set_flags (char *arg
, int *is_quoted
, char **paren_pointer
)
770 /* 'has_if' is for the syntax:
771 * (gdb) break foo if (a==b)
773 if ((ii
= strstr (arg
, " if ")) != NULL
||
774 (ii
= strstr (arg
, "\tif ")) != NULL
||
775 (ii
= strstr (arg
, " if\t")) != NULL
||
776 (ii
= strstr (arg
, "\tif\t")) != NULL
||
777 (ii
= strstr (arg
, " if(")) != NULL
||
778 (ii
= strstr (arg
, "\tif( ")) != NULL
)
780 /* Temporarily zap out "if (condition)" to not
781 * confuse the parenthesis-checking code below.
782 * This is undone below. Do not change ii!!
790 && strchr (get_gdb_completer_quote_characters (),
793 *paren_pointer
= strchr (arg
, '(');
794 if (*paren_pointer
!= NULL
)
795 *paren_pointer
= strrchr (*paren_pointer
, ')');
797 /* Now that we're safely past the paren_pointer check,
798 * put back " if (condition)" so outer layers can see it
806 /* Decode arg of the form *PC. */
808 static struct symtabs_and_lines
809 decode_indirect (char **argptr
)
811 struct symtabs_and_lines values
;
815 pc
= parse_and_eval_address_1 (argptr
);
817 values
.sals
= (struct symtab_and_line
*)
818 xmalloc (sizeof (struct symtab_and_line
));
821 values
.sals
[0] = find_pc_line (pc
, 0);
822 values
.sals
[0].pc
= pc
;
823 values
.sals
[0].section
= find_pc_overlay (pc
);
830 /* Locate the first half of the linespec, ending in a colon, period,
831 or whitespace. (More or less.) Also, check to see if *ARGPTR is
832 enclosed in double quotes; if so, set is_quote_enclosed, advance
833 ARGPTR past that and zero out the trailing double quote. */
836 locate_first_half (char **argptr
, int *is_quote_enclosed
)
842 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
843 and we must isolate the first half. Outer layers will call again later
846 Don't count commas that appear in argument lists of overloaded
847 functions, or in quoted strings. It's stupid to go to this much
848 trouble when the rest of the function is such an obvious roach hotel. */
849 ii
= find_toplevel_char (*argptr
, ',');
850 has_comma
= (ii
!= 0);
852 /* Temporarily zap out second half to not
853 * confuse the code below.
854 * This is undone below. Do not change ii!!
861 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
862 /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
863 /* Look for ':', but ignore inside of <> */
868 *is_quote_enclosed
= 1;
873 *is_quote_enclosed
= 0;
878 char *temp_end
= find_template_name_end (p
);
880 error ("malformed template specification in command");
883 /* Check for the end of the first half of the linespec. End of line,
884 a tab, a double colon or the last single colon, or a space. But
885 if enclosed in double quotes we do not break on enclosed spaces */
889 && ((p
[1] == ':') || (strchr (p
+ 1, ':') == NULL
)))
890 || ((p
[0] == ' ') && !*is_quote_enclosed
))
892 if (p
[0] == '.' && strchr (p
, ':') == NULL
) /* Java qualified method. */
894 /* Find the *last* '.', since the others are package qualifiers. */
895 for (p1
= p
; *p1
; p1
++)
903 while (p
[0] == ' ' || p
[0] == '\t')
906 /* if the closing double quote was left at the end, remove it */
907 if (*is_quote_enclosed
)
909 char *closing_quote
= strchr (p
- 1, '"');
910 if (closing_quote
&& closing_quote
[1] == '\0')
911 *closing_quote
= '\0';
914 /* Now that we've safely parsed the first half,
915 * put back ',' so outer layers can see it
925 /* This handles C++ and Java compound data structures. P should point
926 at the first component separator, i.e. double-colon or period. */
928 static struct symtabs_and_lines
929 decode_compound (char **argptr
, int funfirstline
, char ***canonical
,
930 char *saved_arg
, char *p
)
932 struct symtabs_and_lines values
;
937 char *saved_arg2
= *argptr
;
940 /* The symtab that SYM was found in. */
941 struct symtab
*sym_symtab
;
943 struct symbol
*sym_class
;
945 struct symbol
**sym_arr
;
948 /* First check for "global" namespace specification,
949 of the form "::foo". If found, skip over the colons
950 and jump to normal symbol processing */
952 && ((*argptr
== p
) || (p
[-1] == ' ') || (p
[-1] == '\t')))
955 /* We have what looks like a class or namespace
956 scope specification (A::B), possibly with many
957 levels of namespaces or classes (A::B::C::D).
959 Some versions of the HP ANSI C++ compiler (as also possibly
960 other compilers) generate class/function/member names with
961 embedded double-colons if they are inside namespaces. To
962 handle this, we loop a few times, considering larger and
963 larger prefixes of the string as though they were single
964 symbols. So, if the initially supplied string is
965 A::B::C::D::foo, we have to look up "A", then "A::B",
966 then "A::B::C", then "A::B::C::D", and finally
967 "A::B::C::D::foo" as single, monolithic symbols, because
968 A, B, C or D may be namespaces.
970 Note that namespaces can nest only inside other
971 namespaces, and not inside classes. So we need only
972 consider *prefixes* of the string; there is no need to look up
973 "B::C" separately as a symbol in the previous example. */
975 p2
= p
; /* save for restart */
978 /* Extract the class name. */
980 while (p
!= *argptr
&& p
[-1] == ' ')
982 copy
= (char *) alloca (p
- *argptr
+ 1);
983 memcpy (copy
, *argptr
, p
- *argptr
);
984 copy
[p
- *argptr
] = 0;
986 /* Discard the class name from the arg. */
987 p
= p1
+ (p1
[0] == ':' ? 2 : 1);
988 while (*p
== ' ' || *p
== '\t')
992 sym_class
= lookup_symbol (copy
, 0, STRUCT_NAMESPACE
, 0,
993 (struct symtab
**) NULL
);
996 (t
= check_typedef (SYMBOL_TYPE (sym_class
)),
997 (TYPE_CODE (t
) == TYPE_CODE_STRUCT
998 || TYPE_CODE (t
) == TYPE_CODE_UNION
)))
1000 /* Arg token is not digits => try it as a function name
1001 Find the next token(everything up to end or next blank). */
1003 && strchr (get_gdb_completer_quote_characters (),
1006 p
= skip_quoted (*argptr
);
1007 *argptr
= *argptr
+ 1;
1012 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!= ':')
1016 q = operator_chars (*argptr, &q1);
1020 char *tmp = alloca (q1 - q + 1);
1021 memcpy (tmp, q, q1 - q);
1023 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
1026 cplusplus_error (saved_arg, "no mangling for \"%s\"\n", tmp);
1028 copy = (char*) alloca (3 + strlen(opname));
1029 sprintf (copy, "__%s", opname);
1035 copy
= (char *) alloca (p
- *argptr
+ 1);
1036 memcpy (copy
, *argptr
, p
- *argptr
);
1037 copy
[p
- *argptr
] = '\0';
1039 && copy
[p
- *argptr
- 1]
1040 && strchr (get_gdb_completer_quote_characters (),
1041 copy
[p
- *argptr
- 1]) != NULL
)
1042 copy
[p
- *argptr
- 1] = '\0';
1045 /* no line number may be specified */
1046 while (*p
== ' ' || *p
== '\t')
1051 i1
= 0; /* counter for the symbol array */
1052 sym_arr
= (struct symbol
**) alloca (total_number_of_methods (t
)
1053 * sizeof (struct symbol
*));
1055 if (destructor_name_p (copy
, t
))
1057 /* Destructors are a special case. */
1058 int m_index
, f_index
;
1060 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
1062 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, m_index
);
1065 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, f_index
),
1066 NULL
, VAR_NAMESPACE
, (int *) NULL
,
1067 (struct symtab
**) NULL
);
1073 i1
= find_methods (t
, copy
, sym_arr
);
1076 /* There is exactly one field with that name. */
1079 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1081 values
.sals
= (struct symtab_and_line
*)
1082 xmalloc (sizeof (struct symtab_and_line
));
1084 values
.sals
[0] = find_function_start_sal (sym
,
1095 /* There is more than one field with that name
1096 (overloaded). Ask the user which one to use. */
1097 return decode_line_2 (sym_arr
, i1
, funfirstline
, canonical
);
1103 if (is_operator_name (copy
))
1105 tmp
= (char *) alloca (strlen (copy
+ 3) + 9);
1106 strcpy (tmp
, "operator ");
1107 strcat (tmp
, copy
+ 3);
1112 cplusplus_error (saved_arg
,
1113 "the class `%s' does not have destructor defined\n",
1114 SYMBOL_SOURCE_NAME (sym_class
));
1116 cplusplus_error (saved_arg
,
1117 "the class %s does not have any method named %s\n",
1118 SYMBOL_SOURCE_NAME (sym_class
), tmp
);
1122 /* Move pointer up to next possible class/namespace token */
1123 p
= p2
+ 1; /* restart with old value +1 */
1124 /* Move pointer ahead to next double-colon */
1125 while (*p
&& (p
[0] != ' ') && (p
[0] != '\t') && (p
[0] != '\''))
1129 temp_end
= find_template_name_end (p
);
1131 error ("malformed template specification in command");
1134 else if ((p
[0] == ':') && (p
[1] == ':'))
1135 break; /* found double-colon */
1141 break; /* out of the while (1) */
1143 p2
= p
; /* save restart for next time around */
1144 *argptr
= saved_arg2
; /* restore argptr */
1147 /* Last chance attempt -- check entire name as a symbol */
1148 /* Use "copy" in preparation for jumping out of this block,
1149 to be consistent with usage following the jump target */
1150 copy
= (char *) alloca (p
- saved_arg2
+ 1);
1151 memcpy (copy
, saved_arg2
, p
- saved_arg2
);
1152 /* Note: if is_quoted should be true, we snuff out quote here anyway */
1153 copy
[p
- saved_arg2
] = '\000';
1154 /* Set argptr to skip over the name */
1155 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
1156 /* Look up entire name */
1157 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
1159 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1162 /* Couldn't find any interpretation as classes/namespaces, so give up */
1163 /* The quotes are important if copy is empty. */
1164 cplusplus_error (saved_arg
,
1165 "Can't find member of namespace, class, struct, or union named \"%s\"\n",
1171 /* Return the symtab associated to the filename given by the substring
1172 of *ARGPTR ending at P, and advance ARGPTR past that filename. */
1174 static struct symtab
*
1175 symtab_from_filename (char **argptr
, char *p
, int is_quote_enclosed
)
1182 while (p
!= *argptr
&& p
[-1] == ' ')
1184 if ((*p
== '"') && is_quote_enclosed
)
1186 copy
= (char *) alloca (p
- *argptr
+ 1);
1187 memcpy (copy
, *argptr
, p
- *argptr
);
1188 /* It may have the ending quote right after the file name */
1189 if (is_quote_enclosed
&& copy
[p
- *argptr
- 1] == '"')
1190 copy
[p
- *argptr
- 1] = 0;
1192 copy
[p
- *argptr
] = 0;
1194 /* Find that file's data. */
1195 s
= lookup_symtab (copy
);
1198 if (!have_full_symbols () && !have_partial_symbols ())
1199 error ("No symbol table is loaded. Use the \"file\" command.");
1200 error ("No source file named %s.", copy
);
1203 /* Discard the file name from the arg. */
1205 while (*p
== ' ' || *p
== '\t')
1214 /* This decodes a line where the argument is all digits (possibly
1215 preceded by a sign). Q should point to the end of those digits;
1216 the other arguments are as usual. */
1218 static struct symtabs_and_lines
1219 decode_all_digits (char **argptr
, struct symtab
*default_symtab
,
1220 int default_line
, char ***canonical
,
1221 struct symtab
*s
, char *q
)
1224 struct symtabs_and_lines values
;
1225 struct symtab_and_line val
;
1233 /* We might need a canonical line spec if no file was specified. */
1234 int need_canonical
= (s
== 0) ? 1 : 0;
1238 /* This is where we need to make sure that we have good defaults.
1239 We must guarantee that this section of code is never executed
1240 when we are called with just a function name, since
1241 set_default_source_symtab_and_line uses
1242 select_source_symtab that calls us with such an argument */
1244 if (s
== 0 && default_symtab
== 0)
1246 /* Make sure we have at least a default source file. */
1247 set_default_source_symtab_and_line ();
1248 initialize_defaults (&default_symtab
, &default_line
);
1251 if (**argptr
== '+')
1252 sign
= plus
, (*argptr
)++;
1253 else if (**argptr
== '-')
1254 sign
= minus
, (*argptr
)++;
1255 val
.line
= atoi (*argptr
);
1262 val
.line
= default_line
+ val
.line
;
1268 val
.line
= default_line
- val
.line
;
1273 break; /* No need to adjust val.line. */
1276 while (*q
== ' ' || *q
== '\t')
1282 /* It is possible that this source file has more than one symtab,
1283 and that the new line number specification has moved us from the
1284 default (in s) to a new one. */
1285 val
.symtab
= find_line_symtab (s
, val
.line
, NULL
, NULL
);
1286 if (val
.symtab
== 0)
1290 values
.sals
= (struct symtab_and_line
*)
1291 xmalloc (sizeof (struct symtab_and_line
));
1292 values
.sals
[0] = val
;
1295 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1301 /* Decode a linespec starting with a dollar sign. */
1303 static struct symtabs_and_lines
1304 decode_dollar (char *copy
, int funfirstline
, struct symtab
*default_symtab
,
1305 char ***canonical
, struct symtab
*s
)
1309 int need_canonical
= 0;
1310 struct symtabs_and_lines values
;
1311 struct symtab_and_line val
;
1314 /* The symtab that SYM was found in. */
1315 struct symtab
*sym_symtab
;
1316 struct minimal_symbol
*msymbol
;
1318 p
= (copy
[1] == '$') ? copy
+ 2 : copy
+ 1;
1319 while (*p
>= '0' && *p
<= '9')
1321 if (!*p
) /* reached end of token without hitting non-digit */
1323 /* We have a value history reference */
1324 sscanf ((copy
[1] == '$') ? copy
+ 2 : copy
+ 1, "%d", &index
);
1325 valx
= access_value_history ((copy
[1] == '$') ? -index
: index
);
1326 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1327 error ("History values used in line specs must have integer values.");
1331 /* Not all digits -- may be user variable/function or a
1332 convenience variable */
1334 /* Look up entire name as a symbol first */
1335 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
1336 s
= (struct symtab
*) 0;
1338 /* Symbol was found --> jump to normal symbol processing. */
1340 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1343 /* If symbol was not found, look in minimal symbol tables */
1344 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1345 /* Min symbol was found --> jump to minsym processing. */
1347 return minsym_found (funfirstline
, msymbol
);
1349 /* Not a user variable or function -- must be convenience variable */
1350 need_canonical
= (s
== 0) ? 1 : 0;
1351 valx
= value_of_internalvar (lookup_internalvar (copy
+ 1));
1352 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1353 error ("Convenience variables used in line specs must have integer values.");
1358 /* Either history value or convenience value from above, in valx */
1359 val
.symtab
= s
? s
: default_symtab
;
1360 val
.line
= value_as_long (valx
);
1363 values
.sals
= (struct symtab_and_line
*) xmalloc (sizeof val
);
1364 values
.sals
[0] = val
;
1368 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1375 /* Decode a linespec that's a variable. If S is non-NULL,
1376 look in that symtab's static variables first. */
1378 static struct symtabs_and_lines
1379 decode_variable (char *copy
, int funfirstline
, char ***canonical
,
1383 /* The symtab that SYM was found in. */
1384 struct symtab
*sym_symtab
;
1386 struct minimal_symbol
*msymbol
;
1388 sym
= lookup_symbol (copy
,
1389 (s
? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
)
1390 : get_selected_block (0)),
1391 VAR_NAMESPACE
, 0, &sym_symtab
);
1394 return symbol_found (funfirstline
, canonical
, copy
, sym
, s
, sym_symtab
);
1396 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1398 if (msymbol
!= NULL
)
1399 return minsym_found (funfirstline
, msymbol
);
1401 if (!have_full_symbols () &&
1402 !have_partial_symbols () && !have_minimal_symbols ())
1403 error ("No symbol table is loaded. Use the \"file\" command.");
1405 error ("Function \"%s\" not defined.", copy
);
1411 /* Now come some functions that are called from multiple places within
1414 /* We've found a symbol SYM to associate with our linespec; build a
1415 corresponding struct symtabs_and_lines. */
1417 static struct symtabs_and_lines
1418 symbol_found (int funfirstline
, char ***canonical
, char *copy
,
1419 struct symbol
*sym
, struct symtab
*s
,
1420 struct symtab
*sym_symtab
)
1422 struct symtabs_and_lines values
;
1424 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1426 /* Arg is the name of a function */
1427 values
.sals
= (struct symtab_and_line
*)
1428 xmalloc (sizeof (struct symtab_and_line
));
1429 values
.sals
[0] = find_function_start_sal (sym
, funfirstline
);
1432 /* Don't use the SYMBOL_LINE; if used at all it points to
1433 the line containing the parameters or thereabouts, not
1434 the first line of code. */
1436 /* We might need a canonical line spec if it is a static
1440 struct blockvector
*bv
= BLOCKVECTOR (sym_symtab
);
1441 struct block
*b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1442 if (lookup_block_symbol (b
, copy
, NULL
, VAR_NAMESPACE
) != NULL
)
1443 build_canonical_line_spec (values
.sals
, copy
, canonical
);
1450 error ("\"%s\" is not a function", copy
);
1451 else if (SYMBOL_LINE (sym
) != 0)
1453 /* We know its line number. */
1454 values
.sals
= (struct symtab_and_line
*)
1455 xmalloc (sizeof (struct symtab_and_line
));
1457 memset (&values
.sals
[0], 0, sizeof (values
.sals
[0]));
1458 values
.sals
[0].symtab
= sym_symtab
;
1459 values
.sals
[0].line
= SYMBOL_LINE (sym
);
1463 /* This can happen if it is compiled with a compiler which doesn't
1464 put out line numbers for variables. */
1465 /* FIXME: Shouldn't we just set .line and .symtab to zero
1466 and return? For example, "info line foo" could print
1468 error ("Line number not known for symbol \"%s\"", copy
);
1472 /* We've found a minimal symbol MSYMBOL to associate with our
1473 linespec; build a corresponding struct symtabs_and_lines. */
1475 static struct symtabs_and_lines
1476 minsym_found (int funfirstline
, struct minimal_symbol
*msymbol
)
1478 struct symtabs_and_lines values
;
1480 values
.sals
= (struct symtab_and_line
*)
1481 xmalloc (sizeof (struct symtab_and_line
));
1482 values
.sals
[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol
),
1483 (struct sec
*) 0, 0);
1484 values
.sals
[0].section
= SYMBOL_BFD_SECTION (msymbol
);
1487 values
.sals
[0].pc
+= FUNCTION_START_OFFSET
;
1488 values
.sals
[0].pc
= SKIP_PROLOGUE (values
.sals
[0].pc
);