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"
37 /* We share this one with symtab.c, but it is not exported widely. */
39 extern char *operator_chars (char *, char **);
41 /* Prototypes for local functions */
43 static void initialize_defaults (struct symtab
**default_symtab
,
46 static void set_flags (char *arg
, int *is_quoted
, char **paren_pointer
);
48 static struct symtabs_and_lines
decode_indirect (char **argptr
);
50 static char *locate_first_half (char **argptr
, int *is_quote_enclosed
);
52 static struct symtabs_and_lines
decode_compound (char **argptr
,
58 static struct symbol
*lookup_prefix_sym (char **argptr
, char *p
);
60 static struct symtabs_and_lines
find_method (int funfirstline
,
65 struct symbol
*sym_class
);
67 static int collect_methods (char *copy
, struct type
*t
,
68 struct symbol
**sym_arr
);
70 static NORETURN
void cplusplus_error (const char *name
,
72 ATTR_NORETURN
ATTR_FORMAT (printf
, 2, 3);
74 static int total_number_of_methods (struct type
*type
);
76 static int find_methods (struct type
*, char *, struct symbol
**);
78 static void build_canonical_line_spec (struct symtab_and_line
*,
81 static char *find_toplevel_char (char *s
, char c
);
83 static struct symtabs_and_lines
decode_line_2 (struct symbol
*[],
86 static struct symtab
*symtab_from_filename (char **argptr
,
87 char *p
, int is_quote_enclosed
);
90 symtabs_and_lines
decode_all_digits (char **argptr
,
91 struct symtab
*default_symtab
,
94 struct symtab
*file_symtab
,
97 static struct symtabs_and_lines
decode_dollar (char *copy
,
99 struct symtab
*default_symtab
,
101 struct symtab
*file_symtab
);
103 static struct symtabs_and_lines
decode_variable (char *copy
,
106 struct symtab
*file_symtab
);
109 symtabs_and_lines
symbol_found (int funfirstline
,
113 struct symtab
*file_symtab
,
114 struct symtab
*sym_symtab
);
117 symtabs_and_lines
minsym_found (int funfirstline
,
118 struct minimal_symbol
*msymbol
);
120 /* Helper functions. */
122 /* Issue a helpful hint on using the command completion feature on
123 single quoted demangled C++ symbols as part of the completion
127 cplusplus_error (const char *name
, const char *fmt
, ...)
129 struct ui_file
*tmp_stream
;
130 tmp_stream
= mem_fileopen ();
131 make_cleanup_ui_file_delete (tmp_stream
);
135 va_start (args
, fmt
);
136 vfprintf_unfiltered (tmp_stream
, fmt
, args
);
140 while (*name
== '\'')
142 fprintf_unfiltered (tmp_stream
,
143 ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
144 "(Note leading single quote.)"),
146 error_stream (tmp_stream
);
149 /* Return the number of methods described for TYPE, including the
150 methods from types it derives from. This can't be done in the symbol
151 reader because the type of the baseclass might still be stubbed
152 when the definition of the derived class is parsed. */
155 total_number_of_methods (struct type
*type
)
160 CHECK_TYPEDEF (type
);
161 if (TYPE_CPLUS_SPECIFIC (type
) == NULL
)
163 count
= TYPE_NFN_FIELDS_TOTAL (type
);
165 for (n
= 0; n
< TYPE_N_BASECLASSES (type
); n
++)
166 count
+= total_number_of_methods (TYPE_BASECLASS (type
, n
));
171 /* Recursive helper function for decode_line_1.
172 Look for methods named NAME in type T.
173 Return number of matches.
174 Put matches in SYM_ARR, which should have been allocated with
175 a size of total_number_of_methods (T) * sizeof (struct symbol *).
176 Note that this function is g++ specific. */
179 find_methods (struct type
*t
, char *name
, struct symbol
**sym_arr
)
183 char *class_name
= type_name_no_tag (t
);
185 /* Ignore this class if it doesn't have a name. This is ugly, but
186 unless we figure out how to get the physname without the name of
187 the class, then the loop can't do any good. */
189 && (lookup_symbol (class_name
, (struct block
*) NULL
,
190 STRUCT_NAMESPACE
, (int *) NULL
,
191 (struct symtab
**) NULL
)))
194 int name_len
= strlen (name
);
198 /* Loop over each method name. At this level, all overloads of a name
199 are counted as a single name. There is an inner loop which loops over
202 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
207 char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
210 if (strncmp (method_name
, "__", 2) == 0 ||
211 strncmp (method_name
, "op", 2) == 0 ||
212 strncmp (method_name
, "type", 4) == 0)
214 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
215 method_name
= dem_opname
;
216 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
217 method_name
= dem_opname
;
220 if (strcmp_iw (name
, method_name
) == 0)
221 /* Find all the overloaded methods with that name. */
222 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
229 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
231 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
235 tmp_name
= gdb_mangle_name (t
,
238 phys_name
= alloca (strlen (tmp_name
) + 1);
239 strcpy (phys_name
, tmp_name
);
243 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
245 /* Destructor is handled by caller, don't add it to
247 if (is_destructor_name (phys_name
) != 0)
250 sym_arr
[i1
] = lookup_symbol (phys_name
,
253 (struct symtab
**) NULL
);
258 /* This error message gets printed, but the method
259 still seems to be found
260 fputs_filtered("(Cannot find method ", gdb_stdout);
261 fprintf_symbol_filtered (gdb_stdout, phys_name,
263 DMGL_PARAMS | DMGL_ANSI);
264 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
268 else if (strncmp (class_name
, name
, name_len
) == 0
269 && (class_name
[name_len
] == '\0'
270 || class_name
[name_len
] == '<'))
272 /* For GCC 3.x and stabs, constructors and destructors
273 have names like __base_ctor and __complete_dtor.
274 Check the physname for now if we're looking for a
277 = TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
284 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
286 /* GCC 3.x will never produce stabs stub methods, so
287 we don't need to handle this case. */
288 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
290 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
291 if (! is_constructor_name (phys_name
))
294 /* If this method is actually defined, include it in the
296 sym_arr
[i1
] = lookup_symbol (phys_name
,
299 (struct symtab
**) NULL
);
307 /* Only search baseclasses if there is no match yet, since names in
308 derived classes override those in baseclasses.
310 FIXME: The above is not true; it is only true of member functions
311 if they have the same number of arguments (??? - section 13.1 of the
312 ARM says the function members are not in the same scope but doesn't
313 really spell out the rules in a way I understand. In any case, if
314 the number of arguments differ this is a case in which we can overload
315 rather than hiding without any problem, and gcc 2.4.5 does overload
316 rather than hiding in this case). */
319 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
320 i1
+= find_methods (TYPE_BASECLASS (t
, ibase
), name
, sym_arr
+ i1
);
325 /* Helper function for decode_line_1.
326 Build a canonical line spec in CANONICAL if it is non-NULL and if
327 the SAL has a symtab.
328 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
329 If SYMNAME is NULL the line number from SAL is used and the canonical
330 line spec is `filename:linenum'. */
333 build_canonical_line_spec (struct symtab_and_line
*sal
, char *symname
,
336 char **canonical_arr
;
337 char *canonical_name
;
339 struct symtab
*s
= sal
->symtab
;
341 if (s
== (struct symtab
*) NULL
342 || s
->filename
== (char *) NULL
343 || canonical
== (char ***) NULL
)
346 canonical_arr
= (char **) xmalloc (sizeof (char *));
347 *canonical
= canonical_arr
;
349 filename
= s
->filename
;
352 canonical_name
= xmalloc (strlen (filename
) + strlen (symname
) + 2);
353 sprintf (canonical_name
, "%s:%s", filename
, symname
);
357 canonical_name
= xmalloc (strlen (filename
) + 30);
358 sprintf (canonical_name
, "%s:%d", filename
, sal
->line
);
360 canonical_arr
[0] = canonical_name
;
365 /* Find an instance of the character C in the string S that is outside
366 of all parenthesis pairs, single-quoted strings, and double-quoted
367 strings. Also, ignore the char within a template name, like a ','
368 within foo<int, int>. */
371 find_toplevel_char (char *s
, char c
)
373 int quoted
= 0; /* zero if we're not in quotes;
374 '"' if we're in a double-quoted string;
375 '\'' if we're in a single-quoted string. */
376 int depth
= 0; /* Number of unclosed parens we've seen. */
379 for (scan
= s
; *scan
; scan
++)
385 else if (*scan
== '\\' && *(scan
+ 1))
388 else if (*scan
== c
&& ! quoted
&& depth
== 0)
390 else if (*scan
== '"' || *scan
== '\'')
392 else if (*scan
== '(' || *scan
== '<')
394 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
401 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
402 operate on (ask user if necessary).
403 If CANONICAL is non-NULL return a corresponding array of mangled names
404 as canonical line specs there. */
406 static struct symtabs_and_lines
407 decode_line_2 (struct symbol
*sym_arr
[], int nelts
, int funfirstline
,
410 struct symtabs_and_lines values
, return_values
;
415 struct cleanup
*old_chain
;
416 char **canonical_arr
= (char **) NULL
;
418 values
.sals
= (struct symtab_and_line
*)
419 alloca (nelts
* sizeof (struct symtab_and_line
));
420 return_values
.sals
= (struct symtab_and_line
*)
421 xmalloc (nelts
* sizeof (struct symtab_and_line
));
422 old_chain
= make_cleanup (xfree
, return_values
.sals
);
426 canonical_arr
= (char **) xmalloc (nelts
* sizeof (char *));
427 make_cleanup (xfree
, canonical_arr
);
428 memset (canonical_arr
, 0, nelts
* sizeof (char *));
429 *canonical
= canonical_arr
;
433 printf_unfiltered ("[0] cancel\n[1] all\n");
436 init_sal (&return_values
.sals
[i
]); /* Initialize to zeroes. */
437 init_sal (&values
.sals
[i
]);
438 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
440 values
.sals
[i
] = find_function_start_sal (sym_arr
[i
], funfirstline
);
441 printf_unfiltered ("[%d] %s at %s:%d\n",
443 SYMBOL_PRINT_NAME (sym_arr
[i
]),
444 values
.sals
[i
].symtab
->filename
,
445 values
.sals
[i
].line
);
448 printf_unfiltered ("?HERE\n");
452 prompt
= getenv ("PS2");
457 args
= command_line_input (prompt
, 0, "overload-choice");
459 if (args
== 0 || *args
== 0)
460 error_no_arg ("one or more choice numbers");
468 while (*arg1
>= '0' && *arg1
<= '9')
470 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
471 error ("Arguments must be choice numbers.");
481 for (i
= 0; i
< nelts
; i
++)
483 if (canonical_arr
[i
] == NULL
)
485 symname
= DEPRECATED_SYMBOL_NAME (sym_arr
[i
]);
486 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
490 memcpy (return_values
.sals
, values
.sals
,
491 (nelts
* sizeof (struct symtab_and_line
)));
492 return_values
.nelts
= nelts
;
493 discard_cleanups (old_chain
);
494 return return_values
;
497 if (num
>= nelts
+ 2)
499 printf_unfiltered ("No choice number %d.\n", num
);
504 if (values
.sals
[num
].pc
)
508 symname
= DEPRECATED_SYMBOL_NAME (sym_arr
[num
]);
509 make_cleanup (xfree
, symname
);
510 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
512 return_values
.sals
[i
++] = values
.sals
[num
];
513 values
.sals
[num
].pc
= 0;
517 printf_unfiltered ("duplicate request for %d ignored.\n", num
);
522 while (*args
== ' ' || *args
== '\t')
525 return_values
.nelts
= i
;
526 discard_cleanups (old_chain
);
527 return return_values
;
530 /* The parser of linespec itself. */
532 /* Parse a string that specifies a line number.
533 Pass the address of a char * variable; that variable will be
534 advanced over the characters actually parsed.
538 LINENUM -- that line number in current file. PC returned is 0.
539 FILE:LINENUM -- that line in that file. PC returned is 0.
540 FUNCTION -- line number of openbrace of that function.
541 PC returned is the start of the function.
542 VARIABLE -- line number of definition of that variable.
544 FILE:FUNCTION -- likewise, but prefer functions in that file.
545 *EXPR -- line in which address EXPR appears.
547 This may all be followed by an "if EXPR", which we ignore.
549 FUNCTION may be an undebuggable function found in minimal symbol table.
551 If the argument FUNFIRSTLINE is nonzero, we want the first line
552 of real code inside a function when a function is specified, and it is
553 not OK to specify a variable or type to get its line number.
555 DEFAULT_SYMTAB specifies the file to use if none is specified.
556 It defaults to current_source_symtab.
557 DEFAULT_LINE specifies the line number to use for relative
558 line numbers (that start with signs). Defaults to current_source_line.
559 If CANONICAL is non-NULL, store an array of strings containing the canonical
560 line specs there if necessary. Currently overloaded member functions and
561 line numbers or static functions without a filename yield a canonical
562 line spec. The array and the line spec strings are allocated on the heap,
563 it is the callers responsibility to free them.
565 Note that it is possible to return zero for the symtab
566 if no file is validly specified. Callers must check that.
567 Also, the line number returned may be invalid. */
569 /* We allow single quotes in various places. This is a hideous
570 kludge, which exists because the completer can't yet deal with the
571 lack of single quotes. FIXME: write a linespec_completer which we
572 can use as appropriate instead of make_symbol_completion_list. */
574 struct symtabs_and_lines
575 decode_line_1 (char **argptr
, int funfirstline
, struct symtab
*default_symtab
,
576 int default_line
, char ***canonical
)
580 /* If a file name is specified, this is its symtab. */
581 struct symtab
*file_symtab
= NULL
;
584 /* This is NULL if there are no parens in *ARGPTR, or a pointer to
585 the closing parenthesis if there are parens. */
587 /* This says whether or not something in *ARGPTR is quoted with
588 completer_quotes (i.e. with single quotes). */
590 /* Is part of *ARGPTR is enclosed in double quotes? */
591 int is_quote_enclosed
;
592 char *saved_arg
= *argptr
;
594 /* Defaults have defaults. */
596 initialize_defaults (&default_symtab
, &default_line
);
598 /* See if arg is *PC. */
601 return decode_indirect (argptr
);
603 /* Set various flags. 'paren_pointer' is important for overload
604 checking, where we allow things like:
605 (gdb) break c::f(int)
608 set_flags (*argptr
, &is_quoted
, &paren_pointer
);
610 /* Check to see if it's a multipart linespec (with colons or
613 /* Locate the end of the first half of the linespec. */
615 p
= locate_first_half (argptr
, &is_quote_enclosed
);
617 /* Does it look like there actually were two parts? */
619 if ((p
[0] == ':' || p
[0] == '.') && paren_pointer
== NULL
)
622 *argptr
= *argptr
+ 1;
624 /* Is it a C++ or Java compound data structure? */
626 if (p
[0] == '.' || p
[1] == ':')
627 return decode_compound (argptr
, funfirstline
, canonical
,
630 /* No, the first part is a filename; set s to be that file's
631 symtab. Also, move argptr past the filename. */
633 file_symtab
= symtab_from_filename (argptr
, p
, is_quote_enclosed
);
636 /* No one really seems to know why this was added. It certainly
637 breaks the command line, though, whenever the passed
638 name is of the form ClassName::Method. This bit of code
639 singles out the class name, and if funfirstline is set (for
640 example, you are setting a breakpoint at this function),
641 you get an error. This did not occur with earlier
642 verions, so I am ifdef'ing this out. 3/29/99 */
645 /* Check if what we have till now is a symbol name */
647 /* We may be looking at a template instantiation such
648 as "foo<int>". Check here whether we know about it,
649 instead of falling through to the code below which
650 handles ordinary function names, because that code
651 doesn't like seeing '<' and '>' in a name -- the
652 skip_quoted call doesn't go past them. So see if we
653 can figure it out right now. */
655 copy
= (char *) alloca (p
- *argptr
+ 1);
656 memcpy (copy
, *argptr
, p
- *argptr
);
657 copy
[p
- *argptr
] = '\000';
658 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
661 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
662 return symbol_found (funfirstline
, canonical
, copy
, sym
,
665 /* Otherwise fall out from here and go to file/line spec
670 /* S is specified file's symtab, or 0 if no file specified.
671 arg no longer contains the file name. */
673 /* Check whether arg is all digits (and sign). */
676 if (*q
== '-' || *q
== '+')
678 while (*q
>= '0' && *q
<= '9')
681 if (q
!= *argptr
&& (*q
== 0 || *q
== ' ' || *q
== '\t' || *q
== ','))
682 /* We found a token consisting of all digits -- at least one digit. */
683 return decode_all_digits (argptr
, default_symtab
, default_line
,
684 canonical
, file_symtab
, q
);
686 /* Arg token is not digits => try it as a variable name
687 Find the next token (everything up to end or next whitespace). */
689 if (**argptr
== '$') /* May be a convenience variable. */
690 /* One or two $ chars possible. */
691 p
= skip_quoted (*argptr
+ (((*argptr
)[1] == '$') ? 2 : 1));
694 p
= skip_quoted (*argptr
);
696 error ("Unmatched single quote.");
698 else if (paren_pointer
!= NULL
)
700 p
= paren_pointer
+ 1;
704 p
= skip_quoted (*argptr
);
707 copy
= (char *) alloca (p
- *argptr
+ 1);
708 memcpy (copy
, *argptr
, p
- *argptr
);
709 copy
[p
- *argptr
] = '\0';
712 && copy
[0] == copy
[p
- *argptr
- 1]
713 && strchr (get_gdb_completer_quote_characters (), copy
[0]) != NULL
)
715 copy
[p
- *argptr
- 1] = '\0';
718 while (*p
== ' ' || *p
== '\t')
722 /* If it starts with $: may be a legitimate variable or routine name
723 (e.g. HP-UX millicode routines such as $$dyncall), or it may
724 be history value, or it may be a convenience variable. */
727 return decode_dollar (copy
, funfirstline
, default_symtab
,
728 canonical
, file_symtab
);
730 /* Look up that token as a variable.
731 If file specified, use that file's per-file block to start with. */
733 return decode_variable (copy
, funfirstline
, canonical
, file_symtab
);
738 /* Now, more helper functions for decode_line_1. Some conventions
739 that these functions follow:
741 Decode_line_1 typically passes along some of its arguments or local
742 variables to the subfunctions. It passes the variables by
743 reference if they are modified by the subfunction, and by value
746 Some of the functions have side effects that don't arise from
747 variables that are passed by reference. In particular, if a
748 function is passed ARGPTR as an argument, it modifies what ARGPTR
749 points to; typically, it advances *ARGPTR past whatever substring
750 it has just looked at. (If it doesn't modify *ARGPTR, then the
751 function gets passed *ARGPTR instead, which is then called ARG: see
752 set_flags, for example.) Also, functions that return a struct
753 symtabs_and_lines may modify CANONICAL, as in the description of
756 If a function returns a struct symtabs_and_lines, then that struct
757 will immediately make its way up the call chain to be returned by
758 decode_line_1. In particular, all of the functions decode_XXX
759 calculate the appropriate struct symtabs_and_lines, under the
760 assumption that their argument is of the form XXX. */
762 /* First, some functions to initialize stuff at the beggining of the
766 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
768 if (*default_symtab
== 0)
770 /* Use whatever we have for the default source line. We don't use
771 get_current_or_default_symtab_and_line as it can recurse and call
773 struct symtab_and_line cursal
=
774 get_current_source_symtab_and_line ();
776 *default_symtab
= cursal
.symtab
;
777 *default_line
= cursal
.line
;
782 set_flags (char *arg
, int *is_quoted
, char **paren_pointer
)
787 /* 'has_if' is for the syntax:
788 (gdb) break foo if (a==b)
790 if ((ii
= strstr (arg
, " if ")) != NULL
||
791 (ii
= strstr (arg
, "\tif ")) != NULL
||
792 (ii
= strstr (arg
, " if\t")) != NULL
||
793 (ii
= strstr (arg
, "\tif\t")) != NULL
||
794 (ii
= strstr (arg
, " if(")) != NULL
||
795 (ii
= strstr (arg
, "\tif( ")) != NULL
)
797 /* Temporarily zap out "if (condition)" to not confuse the
798 parenthesis-checking code below. This is undone below. Do not
806 && strchr (get_gdb_completer_quote_characters (),
809 *paren_pointer
= strchr (arg
, '(');
810 if (*paren_pointer
!= NULL
)
811 *paren_pointer
= strrchr (*paren_pointer
, ')');
813 /* Now that we're safely past the paren_pointer check, put back " if
814 (condition)" so outer layers can see it. */
821 /* Decode arg of the form *PC. */
823 static struct symtabs_and_lines
824 decode_indirect (char **argptr
)
826 struct symtabs_and_lines values
;
830 pc
= parse_and_eval_address_1 (argptr
);
832 values
.sals
= (struct symtab_and_line
*)
833 xmalloc (sizeof (struct symtab_and_line
));
836 values
.sals
[0] = find_pc_line (pc
, 0);
837 values
.sals
[0].pc
= pc
;
838 values
.sals
[0].section
= find_pc_overlay (pc
);
845 /* Locate the first half of the linespec, ending in a colon, period,
846 or whitespace. (More or less.) Also, check to see if *ARGPTR is
847 enclosed in double quotes; if so, set is_quote_enclosed, advance
848 ARGPTR past that and zero out the trailing double quote. */
851 locate_first_half (char **argptr
, int *is_quote_enclosed
)
857 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
858 and we must isolate the first half. Outer layers will call again later
861 Don't count commas that appear in argument lists of overloaded
862 functions, or in quoted strings. It's stupid to go to this much
863 trouble when the rest of the function is such an obvious roach hotel. */
864 ii
= find_toplevel_char (*argptr
, ',');
865 has_comma
= (ii
!= 0);
867 /* Temporarily zap out second half to not confuse the code below.
868 This is undone below. Do not change ii!! */
874 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION. May also be
875 CLASS::MEMBER, or NAMESPACE::NAME. Look for ':', but ignore
881 *is_quote_enclosed
= 1;
886 *is_quote_enclosed
= 0;
891 char *temp_end
= find_template_name_end (p
);
893 error ("malformed template specification in command");
896 /* Check for the end of the first half of the linespec. End of
897 line, a tab, a double colon or the last single colon, or a
898 space. But if enclosed in double quotes we do not break on
903 && ((p
[1] == ':') || (strchr (p
+ 1, ':') == NULL
)))
904 || ((p
[0] == ' ') && !*is_quote_enclosed
))
906 if (p
[0] == '.' && strchr (p
, ':') == NULL
)
908 /* Java qualified method. Find the *last* '.', since the
909 others are package qualifiers. */
910 for (p1
= p
; *p1
; p1
++)
918 while (p
[0] == ' ' || p
[0] == '\t')
921 /* If the closing double quote was left at the end, remove it. */
922 if (*is_quote_enclosed
)
924 char *closing_quote
= strchr (p
- 1, '"');
925 if (closing_quote
&& closing_quote
[1] == '\0')
926 *closing_quote
= '\0';
929 /* Now that we've safely parsed the first half, put back ',' so
930 outer layers can see it. */
939 /* This handles C++ and Java compound data structures. P should point
940 at the first component separator, i.e. double-colon or period. */
942 static struct symtabs_and_lines
943 decode_compound (char **argptr
, int funfirstline
, char ***canonical
,
944 char *saved_arg
, char *p
)
946 struct symtabs_and_lines values
;
951 char *saved_arg2
= *argptr
;
954 /* The symtab that SYM was found in. */
955 struct symtab
*sym_symtab
;
957 struct symbol
*sym_class
;
958 struct symbol
**sym_arr
;
961 /* First check for "global" namespace specification,
962 of the form "::foo". If found, skip over the colons
963 and jump to normal symbol processing. */
965 && ((*argptr
== p
) || (p
[-1] == ' ') || (p
[-1] == '\t')))
968 /* We have what looks like a class or namespace
969 scope specification (A::B), possibly with many
970 levels of namespaces or classes (A::B::C::D).
972 Some versions of the HP ANSI C++ compiler (as also possibly
973 other compilers) generate class/function/member names with
974 embedded double-colons if they are inside namespaces. To
975 handle this, we loop a few times, considering larger and
976 larger prefixes of the string as though they were single
977 symbols. So, if the initially supplied string is
978 A::B::C::D::foo, we have to look up "A", then "A::B",
979 then "A::B::C", then "A::B::C::D", and finally
980 "A::B::C::D::foo" as single, monolithic symbols, because
981 A, B, C or D may be namespaces.
983 Note that namespaces can nest only inside other
984 namespaces, and not inside classes. So we need only
985 consider *prefixes* of the string; there is no need to look up
986 "B::C" separately as a symbol in the previous example. */
988 p2
= p
; /* Save for restart. */
991 sym_class
= lookup_prefix_sym (argptr
, p
);
994 (t
= check_typedef (SYMBOL_TYPE (sym_class
)),
995 (TYPE_CODE (t
) == TYPE_CODE_STRUCT
996 || TYPE_CODE (t
) == TYPE_CODE_UNION
)))
998 /* Arg token is not digits => try it as a function name.
999 Find the next token (everything up to end or next
1002 && strchr (get_gdb_completer_quote_characters (),
1005 p
= skip_quoted (*argptr
);
1006 *argptr
= *argptr
+ 1;
1011 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!= ':')
1015 q = operator_chars (*argptr, &q1);
1019 char *tmp = alloca (q1 - q + 1);
1020 memcpy (tmp, q, q1 - q);
1022 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
1025 cplusplus_error (saved_arg, "no mangling for \"%s\"\n", tmp);
1027 copy = (char*) alloca (3 + strlen(opname));
1028 sprintf (copy, "__%s", opname);
1034 copy
= (char *) alloca (p
- *argptr
+ 1);
1035 memcpy (copy
, *argptr
, p
- *argptr
);
1036 copy
[p
- *argptr
] = '\0';
1038 && copy
[p
- *argptr
- 1]
1039 && strchr (get_gdb_completer_quote_characters (),
1040 copy
[p
- *argptr
- 1]) != NULL
)
1041 copy
[p
- *argptr
- 1] = '\0';
1044 /* No line number may be specified. */
1045 while (*p
== ' ' || *p
== '\t')
1049 return find_method (funfirstline
, canonical
, saved_arg
,
1050 copy
, t
, sym_class
);
1053 /* Move pointer up to next possible class/namespace token. */
1054 p
= p2
+ 1; /* Restart with old value +1. */
1055 /* Move pointer ahead to next double-colon. */
1056 while (*p
&& (p
[0] != ' ') && (p
[0] != '\t') && (p
[0] != '\''))
1060 temp_end
= find_template_name_end (p
);
1062 error ("malformed template specification in command");
1065 else if ((p
[0] == ':') && (p
[1] == ':'))
1066 break; /* Found double-colon. */
1072 break; /* Out of the while (1). */
1074 p2
= p
; /* Save restart for next time around. */
1075 *argptr
= saved_arg2
; /* Restore argptr. */
1078 /* Last chance attempt -- check entire name as a symbol. Use "copy"
1079 in preparation for jumping out of this block, to be consistent
1080 with usage following the jump target. */
1081 copy
= (char *) alloca (p
- saved_arg2
+ 1);
1082 memcpy (copy
, saved_arg2
, p
- saved_arg2
);
1083 /* Note: if is_quoted should be true, we snuff out quote here
1085 copy
[p
- saved_arg2
] = '\000';
1086 /* Set argptr to skip over the name. */
1087 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
1088 /* Look up entire name */
1089 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
1091 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1094 /* Couldn't find any interpretation as classes/namespaces, so give
1095 up. The quotes are important if copy is empty. */
1096 cplusplus_error (saved_arg
,
1097 "Can't find member of namespace, class, struct, or union named \"%s\"\n",
1101 /* Next come some helper functions for decode_compound. */
1103 /* Return the symbol corresponding to the substring of *ARGPTR ending
1104 at P, allowing whitespace. Also, advance *ARGPTR past the symbol
1105 name in question, the compound object separator ("::" or "."), and
1108 static struct symbol
*
1109 lookup_prefix_sym (char **argptr
, char *p
)
1114 /* Extract the class name. */
1116 while (p
!= *argptr
&& p
[-1] == ' ')
1118 copy
= (char *) alloca (p
- *argptr
+ 1);
1119 memcpy (copy
, *argptr
, p
- *argptr
);
1120 copy
[p
- *argptr
] = 0;
1122 /* Discard the class name from the arg. */
1123 p
= p1
+ (p1
[0] == ':' ? 2 : 1);
1124 while (*p
== ' ' || *p
== '\t')
1128 return lookup_symbol (copy
, 0, STRUCT_NAMESPACE
, 0,
1129 (struct symtab
**) NULL
);
1132 /* This finds the method COPY in the class whose type is T and whose
1133 symbol is SYM_CLASS. */
1135 static struct symtabs_and_lines
1136 find_method (int funfirstline
, char ***canonical
, char *saved_arg
,
1137 char *copy
, struct type
*t
, struct symbol
*sym_class
)
1139 struct symtabs_and_lines values
;
1140 struct symbol
*sym
= 0;
1141 int i1
; /* Counter for the symbol array. */
1142 struct symbol
**sym_arr
= alloca (total_number_of_methods (t
)
1143 * sizeof (struct symbol
*));
1145 /* Find all methods with a matching name, and put them in
1148 i1
= collect_methods (copy
, t
, sym_arr
);
1152 /* There is exactly one field with that name. */
1155 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1157 values
.sals
= (struct symtab_and_line
*)
1158 xmalloc (sizeof (struct symtab_and_line
));
1160 values
.sals
[0] = find_function_start_sal (sym
,
1171 /* There is more than one field with that name
1172 (overloaded). Ask the user which one to use. */
1173 return decode_line_2 (sym_arr
, i1
, funfirstline
, canonical
);
1179 if (is_operator_name (copy
))
1181 tmp
= (char *) alloca (strlen (copy
+ 3) + 9);
1182 strcpy (tmp
, "operator ");
1183 strcat (tmp
, copy
+ 3);
1188 cplusplus_error (saved_arg
,
1189 "the class `%s' does not have destructor defined\n",
1190 SYMBOL_PRINT_NAME (sym_class
));
1192 cplusplus_error (saved_arg
,
1193 "the class %s does not have any method named %s\n",
1194 SYMBOL_PRINT_NAME (sym_class
), tmp
);
1198 /* Find all methods named COPY in the class whose type is T, and put
1199 them in SYM_ARR. Return the number of methods found. */
1202 collect_methods (char *copy
, struct type
*t
,
1203 struct symbol
**sym_arr
)
1205 int i1
= 0; /* Counter for the symbol array. */
1207 if (destructor_name_p (copy
, t
))
1209 /* Destructors are a special case. */
1210 int m_index
, f_index
;
1212 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
1214 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, m_index
);
1217 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, f_index
),
1218 NULL
, VAR_NAMESPACE
, (int *) NULL
,
1219 (struct symtab
**) NULL
);
1225 i1
= find_methods (t
, copy
, sym_arr
);
1232 /* Return the symtab associated to the filename given by the substring
1233 of *ARGPTR ending at P, and advance ARGPTR past that filename. */
1235 static struct symtab
*
1236 symtab_from_filename (char **argptr
, char *p
, int is_quote_enclosed
)
1240 struct symtab
*file_symtab
;
1243 while (p
!= *argptr
&& p
[-1] == ' ')
1245 if ((*p
== '"') && is_quote_enclosed
)
1247 copy
= (char *) alloca (p
- *argptr
+ 1);
1248 memcpy (copy
, *argptr
, p
- *argptr
);
1249 /* It may have the ending quote right after the file name. */
1250 if (is_quote_enclosed
&& copy
[p
- *argptr
- 1] == '"')
1251 copy
[p
- *argptr
- 1] = 0;
1253 copy
[p
- *argptr
] = 0;
1255 /* Find that file's data. */
1256 file_symtab
= lookup_symtab (copy
);
1257 if (file_symtab
== 0)
1259 if (!have_full_symbols () && !have_partial_symbols ())
1260 error ("No symbol table is loaded. Use the \"file\" command.");
1261 error ("No source file named %s.", copy
);
1264 /* Discard the file name from the arg. */
1266 while (*p
== ' ' || *p
== '\t')
1275 /* This decodes a line where the argument is all digits (possibly
1276 preceded by a sign). Q should point to the end of those digits;
1277 the other arguments are as usual. */
1279 static struct symtabs_and_lines
1280 decode_all_digits (char **argptr
, struct symtab
*default_symtab
,
1281 int default_line
, char ***canonical
,
1282 struct symtab
*file_symtab
, char *q
)
1285 struct symtabs_and_lines values
;
1286 struct symtab_and_line val
;
1294 /* We might need a canonical line spec if no file was specified. */
1295 int need_canonical
= (file_symtab
== 0) ? 1 : 0;
1299 /* This is where we need to make sure that we have good defaults.
1300 We must guarantee that this section of code is never executed
1301 when we are called with just a function name, since
1302 set_default_source_symtab_and_line uses
1303 select_source_symtab that calls us with such an argument. */
1305 if (file_symtab
== 0 && default_symtab
== 0)
1307 /* Make sure we have at least a default source file. */
1308 set_default_source_symtab_and_line ();
1309 initialize_defaults (&default_symtab
, &default_line
);
1312 if (**argptr
== '+')
1313 sign
= plus
, (*argptr
)++;
1314 else if (**argptr
== '-')
1315 sign
= minus
, (*argptr
)++;
1316 val
.line
= atoi (*argptr
);
1322 if (file_symtab
== 0)
1323 val
.line
= default_line
+ val
.line
;
1328 if (file_symtab
== 0)
1329 val
.line
= default_line
- val
.line
;
1334 break; /* No need to adjust val.line. */
1337 while (*q
== ' ' || *q
== '\t')
1340 if (file_symtab
== 0)
1341 file_symtab
= default_symtab
;
1343 /* It is possible that this source file has more than one symtab,
1344 and that the new line number specification has moved us from the
1345 default (in file_symtab) to a new one. */
1346 val
.symtab
= find_line_symtab (file_symtab
, val
.line
, NULL
, NULL
);
1347 if (val
.symtab
== 0)
1348 val
.symtab
= file_symtab
;
1351 values
.sals
= (struct symtab_and_line
*)
1352 xmalloc (sizeof (struct symtab_and_line
));
1353 values
.sals
[0] = val
;
1356 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1362 /* Decode a linespec starting with a dollar sign. */
1364 static struct symtabs_and_lines
1365 decode_dollar (char *copy
, int funfirstline
, struct symtab
*default_symtab
,
1366 char ***canonical
, struct symtab
*file_symtab
)
1370 int need_canonical
= 0;
1371 struct symtabs_and_lines values
;
1372 struct symtab_and_line val
;
1375 /* The symtab that SYM was found in. */
1376 struct symtab
*sym_symtab
;
1377 struct minimal_symbol
*msymbol
;
1379 p
= (copy
[1] == '$') ? copy
+ 2 : copy
+ 1;
1380 while (*p
>= '0' && *p
<= '9')
1382 if (!*p
) /* Reached end of token without hitting non-digit. */
1384 /* We have a value history reference. */
1385 sscanf ((copy
[1] == '$') ? copy
+ 2 : copy
+ 1, "%d", &index
);
1386 valx
= access_value_history ((copy
[1] == '$') ? -index
: index
);
1387 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1388 error ("History values used in line specs must have integer values.");
1392 /* Not all digits -- may be user variable/function or a
1393 convenience variable. */
1395 /* Look up entire name as a symbol first. */
1396 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
1397 file_symtab
= (struct symtab
*) 0;
1399 /* Symbol was found --> jump to normal symbol processing. */
1401 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1404 /* If symbol was not found, look in minimal symbol tables. */
1405 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1406 /* Min symbol was found --> jump to minsym processing. */
1408 return minsym_found (funfirstline
, msymbol
);
1410 /* Not a user variable or function -- must be convenience variable. */
1411 need_canonical
= (file_symtab
== 0) ? 1 : 0;
1412 valx
= value_of_internalvar (lookup_internalvar (copy
+ 1));
1413 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1414 error ("Convenience variables used in line specs must have integer values.");
1419 /* Either history value or convenience value from above, in valx. */
1420 val
.symtab
= file_symtab
? file_symtab
: default_symtab
;
1421 val
.line
= value_as_long (valx
);
1424 values
.sals
= (struct symtab_and_line
*) xmalloc (sizeof val
);
1425 values
.sals
[0] = val
;
1429 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1436 /* Decode a linespec that's a variable. If FILE_SYMTAB is non-NULL,
1437 look in that symtab's static variables first. */
1439 static struct symtabs_and_lines
1440 decode_variable (char *copy
, int funfirstline
, char ***canonical
,
1441 struct symtab
*file_symtab
)
1444 /* The symtab that SYM was found in. */
1445 struct symtab
*sym_symtab
;
1447 struct minimal_symbol
*msymbol
;
1449 sym
= lookup_symbol (copy
,
1451 ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab
),
1453 : get_selected_block (0)),
1454 VAR_NAMESPACE
, 0, &sym_symtab
);
1457 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1458 file_symtab
, sym_symtab
);
1460 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1462 if (msymbol
!= NULL
)
1463 return minsym_found (funfirstline
, msymbol
);
1465 if (!have_full_symbols () &&
1466 !have_partial_symbols () && !have_minimal_symbols ())
1467 error ("No symbol table is loaded. Use the \"file\" command.");
1469 error ("Function \"%s\" not defined.", copy
);
1475 /* Now come some functions that are called from multiple places within
1478 /* We've found a symbol SYM to associate with our linespec; build a
1479 corresponding struct symtabs_and_lines. */
1481 static struct symtabs_and_lines
1482 symbol_found (int funfirstline
, char ***canonical
, char *copy
,
1483 struct symbol
*sym
, struct symtab
*file_symtab
,
1484 struct symtab
*sym_symtab
)
1486 struct symtabs_and_lines values
;
1488 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1490 /* Arg is the name of a function */
1491 values
.sals
= (struct symtab_and_line
*)
1492 xmalloc (sizeof (struct symtab_and_line
));
1493 values
.sals
[0] = find_function_start_sal (sym
, funfirstline
);
1496 /* Don't use the SYMBOL_LINE; if used at all it points to
1497 the line containing the parameters or thereabouts, not
1498 the first line of code. */
1500 /* We might need a canonical line spec if it is a static
1502 if (file_symtab
== 0)
1504 struct blockvector
*bv
= BLOCKVECTOR (sym_symtab
);
1505 struct block
*b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1506 if (lookup_block_symbol (b
, copy
, NULL
, VAR_NAMESPACE
) != NULL
)
1507 build_canonical_line_spec (values
.sals
, copy
, canonical
);
1514 error ("\"%s\" is not a function", copy
);
1515 else if (SYMBOL_LINE (sym
) != 0)
1517 /* We know its line number. */
1518 values
.sals
= (struct symtab_and_line
*)
1519 xmalloc (sizeof (struct symtab_and_line
));
1521 memset (&values
.sals
[0], 0, sizeof (values
.sals
[0]));
1522 values
.sals
[0].symtab
= sym_symtab
;
1523 values
.sals
[0].line
= SYMBOL_LINE (sym
);
1527 /* This can happen if it is compiled with a compiler which doesn't
1528 put out line numbers for variables. */
1529 /* FIXME: Shouldn't we just set .line and .symtab to zero
1530 and return? For example, "info line foo" could print
1532 error ("Line number not known for symbol \"%s\"", copy
);
1536 /* We've found a minimal symbol MSYMBOL to associate with our
1537 linespec; build a corresponding struct symtabs_and_lines. */
1539 static struct symtabs_and_lines
1540 minsym_found (int funfirstline
, struct minimal_symbol
*msymbol
)
1542 struct symtabs_and_lines values
;
1544 values
.sals
= (struct symtab_and_line
*)
1545 xmalloc (sizeof (struct symtab_and_line
));
1546 values
.sals
[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol
),
1547 (struct sec
*) 0, 0);
1548 values
.sals
[0].section
= SYMBOL_BFD_SECTION (msymbol
);
1551 values
.sals
[0].pc
+= FUNCTION_START_OFFSET
;
1552 values
.sals
[0].pc
= SKIP_PROLOGUE (values
.sals
[0].pc
);
This page took 0.060551 seconds and 4 git commands to generate.