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
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 cplusplus_error (const char *name
, const char *fmt
, ...) ATTR_FORMAT (printf
, 2, 3);
44 static int total_number_of_methods (struct type
*type
);
46 static int find_methods (struct type
*, char *, struct symbol
**);
48 static void build_canonical_line_spec (struct symtab_and_line
*,
51 static char *find_toplevel_char (char *s
, char c
);
53 static struct symtabs_and_lines
decode_line_2 (struct symbol
*[],
57 symtabs_and_lines
symbol_found (int funfirstline
,
62 struct symtab
*sym_symtab
);
65 symtabs_and_lines
minsym_found (int funfirstline
,
66 struct minimal_symbol
*msymbol
);
68 /* Helper functions. */
70 /* Issue a helpful hint on using the command completion feature on
71 single quoted demangled C++ symbols as part of the completion
75 cplusplus_error (const char *name
, const char *fmt
, ...)
77 struct ui_file
*tmp_stream
;
78 tmp_stream
= mem_fileopen ();
79 make_cleanup_ui_file_delete (tmp_stream
);
84 vfprintf_unfiltered (tmp_stream
, fmt
, args
);
90 fprintf_unfiltered (tmp_stream
,
91 ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
92 "(Note leading single quote.)"),
94 error_stream (tmp_stream
);
97 /* Return the number of methods described for TYPE, including the
98 methods from types it derives from. This can't be done in the symbol
99 reader because the type of the baseclass might still be stubbed
100 when the definition of the derived class is parsed. */
103 total_number_of_methods (struct type
*type
)
108 CHECK_TYPEDEF (type
);
109 if (TYPE_CPLUS_SPECIFIC (type
) == NULL
)
111 count
= TYPE_NFN_FIELDS_TOTAL (type
);
113 for (n
= 0; n
< TYPE_N_BASECLASSES (type
); n
++)
114 count
+= total_number_of_methods (TYPE_BASECLASS (type
, n
));
119 /* Recursive helper function for decode_line_1.
120 Look for methods named NAME in type T.
121 Return number of matches.
122 Put matches in SYM_ARR, which should have been allocated with
123 a size of total_number_of_methods (T) * sizeof (struct symbol *).
124 Note that this function is g++ specific. */
127 find_methods (struct type
*t
, char *name
, struct symbol
**sym_arr
)
131 char *class_name
= type_name_no_tag (t
);
133 /* Ignore this class if it doesn't have a name. This is ugly, but
134 unless we figure out how to get the physname without the name of
135 the class, then the loop can't do any good. */
137 && (lookup_symbol (class_name
, (struct block
*) NULL
,
138 STRUCT_NAMESPACE
, (int *) NULL
,
139 (struct symtab
**) NULL
)))
142 int name_len
= strlen (name
);
146 /* Loop over each method name. At this level, all overloads of a name
147 are counted as a single name. There is an inner loop which loops over
150 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
155 char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
158 if (strncmp (method_name
, "__", 2) == 0 ||
159 strncmp (method_name
, "op", 2) == 0 ||
160 strncmp (method_name
, "type", 4) == 0)
162 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
163 method_name
= dem_opname
;
164 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
165 method_name
= dem_opname
;
168 if (strcmp_iw (name
, method_name
) == 0)
169 /* Find all the overloaded methods with that name. */
170 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
177 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
179 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
183 tmp_name
= gdb_mangle_name (t
,
186 phys_name
= alloca (strlen (tmp_name
) + 1);
187 strcpy (phys_name
, tmp_name
);
191 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
193 /* Destructor is handled by caller, dont add it to the list */
194 if (is_destructor_name (phys_name
) != 0)
197 sym_arr
[i1
] = lookup_symbol (phys_name
,
200 (struct symtab
**) NULL
);
205 /* This error message gets printed, but the method
206 still seems to be found
207 fputs_filtered("(Cannot find method ", gdb_stdout);
208 fprintf_symbol_filtered (gdb_stdout, phys_name,
210 DMGL_PARAMS | DMGL_ANSI);
211 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
215 else if (strncmp (class_name
, name
, name_len
) == 0
216 && (class_name
[name_len
] == '\0'
217 || class_name
[name_len
] == '<'))
219 /* For GCC 3.x and stabs, constructors and destructors have names
220 like __base_ctor and __complete_dtor. Check the physname for now
221 if we're looking for a constructor. */
223 = TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
230 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
232 /* GCC 3.x will never produce stabs stub methods, so we don't need
233 to handle this case. */
234 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
236 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
237 if (! is_constructor_name (phys_name
))
240 /* If this method is actually defined, include it in the
242 sym_arr
[i1
] = lookup_symbol (phys_name
,
245 (struct symtab
**) NULL
);
253 /* Only search baseclasses if there is no match yet, since names in
254 derived classes override those in baseclasses.
256 FIXME: The above is not true; it is only true of member functions
257 if they have the same number of arguments (??? - section 13.1 of the
258 ARM says the function members are not in the same scope but doesn't
259 really spell out the rules in a way I understand. In any case, if
260 the number of arguments differ this is a case in which we can overload
261 rather than hiding without any problem, and gcc 2.4.5 does overload
262 rather than hiding in this case). */
265 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
266 i1
+= find_methods (TYPE_BASECLASS (t
, ibase
), name
, sym_arr
+ i1
);
271 /* Helper function for decode_line_1.
272 Build a canonical line spec in CANONICAL if it is non-NULL and if
273 the SAL has a symtab.
274 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
275 If SYMNAME is NULL the line number from SAL is used and the canonical
276 line spec is `filename:linenum'. */
279 build_canonical_line_spec (struct symtab_and_line
*sal
, char *symname
,
282 char **canonical_arr
;
283 char *canonical_name
;
285 struct symtab
*s
= sal
->symtab
;
287 if (s
== (struct symtab
*) NULL
288 || s
->filename
== (char *) NULL
289 || canonical
== (char ***) NULL
)
292 canonical_arr
= (char **) xmalloc (sizeof (char *));
293 *canonical
= canonical_arr
;
295 filename
= s
->filename
;
298 canonical_name
= xmalloc (strlen (filename
) + strlen (symname
) + 2);
299 sprintf (canonical_name
, "%s:%s", filename
, symname
);
303 canonical_name
= xmalloc (strlen (filename
) + 30);
304 sprintf (canonical_name
, "%s:%d", filename
, sal
->line
);
306 canonical_arr
[0] = canonical_name
;
311 /* Find an instance of the character C in the string S that is outside
312 of all parenthesis pairs, single-quoted strings, and double-quoted
313 strings. Also, ignore the char within a template name, like a ','
314 within foo<int, int>. */
317 find_toplevel_char (char *s
, char c
)
319 int quoted
= 0; /* zero if we're not in quotes;
320 '"' if we're in a double-quoted string;
321 '\'' if we're in a single-quoted string. */
322 int depth
= 0; /* number of unclosed parens we've seen */
325 for (scan
= s
; *scan
; scan
++)
331 else if (*scan
== '\\' && *(scan
+ 1))
334 else if (*scan
== c
&& ! quoted
&& depth
== 0)
336 else if (*scan
== '"' || *scan
== '\'')
338 else if (*scan
== '(' || *scan
== '<')
340 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
347 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
348 operate on (ask user if necessary).
349 If CANONICAL is non-NULL return a corresponding array of mangled names
350 as canonical line specs there. */
352 static struct symtabs_and_lines
353 decode_line_2 (struct symbol
*sym_arr
[], int nelts
, int funfirstline
,
356 struct symtabs_and_lines values
, return_values
;
361 struct cleanup
*old_chain
;
362 char **canonical_arr
= (char **) NULL
;
364 values
.sals
= (struct symtab_and_line
*)
365 alloca (nelts
* sizeof (struct symtab_and_line
));
366 return_values
.sals
= (struct symtab_and_line
*)
367 xmalloc (nelts
* sizeof (struct symtab_and_line
));
368 old_chain
= make_cleanup (xfree
, return_values
.sals
);
372 canonical_arr
= (char **) xmalloc (nelts
* sizeof (char *));
373 make_cleanup (xfree
, canonical_arr
);
374 memset (canonical_arr
, 0, nelts
* sizeof (char *));
375 *canonical
= canonical_arr
;
379 printf_unfiltered ("[0] cancel\n[1] all\n");
382 init_sal (&return_values
.sals
[i
]); /* initialize to zeroes */
383 init_sal (&values
.sals
[i
]);
384 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
386 values
.sals
[i
] = find_function_start_sal (sym_arr
[i
], funfirstline
);
387 printf_unfiltered ("[%d] %s at %s:%d\n",
389 SYMBOL_SOURCE_NAME (sym_arr
[i
]),
390 values
.sals
[i
].symtab
->filename
,
391 values
.sals
[i
].line
);
394 printf_unfiltered ("?HERE\n");
398 if ((prompt
= getenv ("PS2")) == NULL
)
402 args
= command_line_input (prompt
, 0, "overload-choice");
404 if (args
== 0 || *args
== 0)
405 error_no_arg ("one or more choice numbers");
413 while (*arg1
>= '0' && *arg1
<= '9')
415 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
416 error ("Arguments must be choice numbers.");
426 for (i
= 0; i
< nelts
; i
++)
428 if (canonical_arr
[i
] == NULL
)
430 symname
= SYMBOL_NAME (sym_arr
[i
]);
431 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
435 memcpy (return_values
.sals
, values
.sals
,
436 (nelts
* sizeof (struct symtab_and_line
)));
437 return_values
.nelts
= nelts
;
438 discard_cleanups (old_chain
);
439 return return_values
;
442 if (num
>= nelts
+ 2)
444 printf_unfiltered ("No choice number %d.\n", num
);
449 if (values
.sals
[num
].pc
)
453 symname
= SYMBOL_NAME (sym_arr
[num
]);
454 make_cleanup (xfree
, symname
);
455 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
457 return_values
.sals
[i
++] = values
.sals
[num
];
458 values
.sals
[num
].pc
= 0;
462 printf_unfiltered ("duplicate request for %d ignored.\n", num
);
467 while (*args
== ' ' || *args
== '\t')
470 return_values
.nelts
= i
;
471 discard_cleanups (old_chain
);
472 return return_values
;
475 /* The parser of linespec itself. */
477 /* Parse a string that specifies a line number.
478 Pass the address of a char * variable; that variable will be
479 advanced over the characters actually parsed.
483 LINENUM -- that line number in current file. PC returned is 0.
484 FILE:LINENUM -- that line in that file. PC returned is 0.
485 FUNCTION -- line number of openbrace of that function.
486 PC returned is the start of the function.
487 VARIABLE -- line number of definition of that variable.
489 FILE:FUNCTION -- likewise, but prefer functions in that file.
490 *EXPR -- line in which address EXPR appears.
492 This may all be followed by an "if EXPR", which we ignore.
494 FUNCTION may be an undebuggable function found in minimal symbol table.
496 If the argument FUNFIRSTLINE is nonzero, we want the first line
497 of real code inside a function when a function is specified, and it is
498 not OK to specify a variable or type to get its line number.
500 DEFAULT_SYMTAB specifies the file to use if none is specified.
501 It defaults to current_source_symtab.
502 DEFAULT_LINE specifies the line number to use for relative
503 line numbers (that start with signs). Defaults to current_source_line.
504 If CANONICAL is non-NULL, store an array of strings containing the canonical
505 line specs there if necessary. Currently overloaded member functions and
506 line numbers or static functions without a filename yield a canonical
507 line spec. The array and the line spec strings are allocated on the heap,
508 it is the callers responsibility to free them.
510 Note that it is possible to return zero for the symtab
511 if no file is validly specified. Callers must check that.
512 Also, the line number returned may be invalid. */
514 /* We allow single quotes in various places. This is a hideous
515 kludge, which exists because the completer can't yet deal with the
516 lack of single quotes. FIXME: write a linespec_completer which we
517 can use as appropriate instead of make_symbol_completion_list. */
519 struct symtabs_and_lines
520 decode_line_1 (char **argptr
, int funfirstline
, struct symtab
*default_symtab
,
521 int default_line
, char ***canonical
)
523 struct symtabs_and_lines values
;
524 struct symtab_and_line val
;
525 register char *p
, *p1
;
526 char *q
, *pp
, *ii
, *p2
;
530 register struct symtab
*s
;
532 register struct symbol
*sym
;
533 /* The symtab that SYM was found in. */
534 struct symtab
*sym_symtab
;
536 register CORE_ADDR pc
;
537 register struct minimal_symbol
*msymbol
;
539 struct symbol
*sym_class
;
542 int is_quote_enclosed
;
546 struct symbol
**sym_arr
;
548 char *saved_arg
= *argptr
;
549 extern char *gdb_completer_quote_characters
;
551 init_sal (&val
); /* initialize to zeroes */
553 /* Defaults have defaults. */
555 if (default_symtab
== 0)
557 /* Use whatever we have for the default source line. We don't use
558 get_current_or_default_symtab_and_line as it can recurse and call
560 struct symtab_and_line cursal
=
561 get_current_source_symtab_and_line ();
563 default_symtab
= cursal
.symtab
;
564 default_line
= cursal
.line
;
567 /* See if arg is *PC */
572 pc
= parse_and_eval_address_1 (argptr
);
574 values
.sals
= (struct symtab_and_line
*)
575 xmalloc (sizeof (struct symtab_and_line
));
578 values
.sals
[0] = find_pc_line (pc
, 0);
579 values
.sals
[0].pc
= pc
;
580 values
.sals
[0].section
= find_pc_overlay (pc
);
585 /* 'has_if' is for the syntax:
586 * (gdb) break foo if (a==b)
588 if ((ii
= strstr (*argptr
, " if ")) != NULL
||
589 (ii
= strstr (*argptr
, "\tif ")) != NULL
||
590 (ii
= strstr (*argptr
, " if\t")) != NULL
||
591 (ii
= strstr (*argptr
, "\tif\t")) != NULL
||
592 (ii
= strstr (*argptr
, " if(")) != NULL
||
593 (ii
= strstr (*argptr
, "\tif( ")) != NULL
)
595 /* Temporarily zap out "if (condition)" to not
596 * confuse the parenthesis-checking code below.
597 * This is undone below. Do not change ii!!
604 /* Set various flags.
605 * 'has_parens' is important for overload checking, where
606 * we allow things like:
607 * (gdb) break c::f(int)
610 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
612 is_quoted
= (**argptr
613 && strchr (get_gdb_completer_quote_characters (),
616 has_parens
= ((pp
= strchr (*argptr
, '(')) != NULL
617 && (pp
= strrchr (pp
, ')')) != NULL
);
619 /* Now that we're safely past the has_parens check,
620 * put back " if (condition)" so outer layers can see it
625 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
626 and we must isolate the first half. Outer layers will call again later
629 Don't count commas that appear in argument lists of overloaded
630 functions, or in quoted strings. It's stupid to go to this much
631 trouble when the rest of the function is such an obvious roach hotel. */
632 ii
= find_toplevel_char (*argptr
, ',');
633 has_comma
= (ii
!= 0);
635 /* Temporarily zap out second half to not
636 * confuse the code below.
637 * This is undone below. Do not change ii!!
644 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
645 /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
646 /* Look for ':', but ignore inside of <> */
652 is_quote_enclosed
= 1;
657 is_quote_enclosed
= 0;
662 char *temp_end
= find_template_name_end (p
);
664 error ("malformed template specification in command");
667 /* Check for the end of the first half of the linespec. End of line,
668 a tab, a double colon or the last single colon, or a space. But
669 if enclosed in double quotes we do not break on enclosed spaces */
673 && ((p
[1] == ':') || (strchr (p
+ 1, ':') == NULL
)))
674 || ((p
[0] == ' ') && !is_quote_enclosed
))
676 if (p
[0] == '.' && strchr (p
, ':') == NULL
) /* Java qualified method. */
678 /* Find the *last* '.', since the others are package qualifiers. */
679 for (p1
= p
; *p1
; p1
++)
687 while (p
[0] == ' ' || p
[0] == '\t')
690 /* if the closing double quote was left at the end, remove it */
691 if (is_quote_enclosed
)
693 char *closing_quote
= strchr (p
- 1, '"');
694 if (closing_quote
&& closing_quote
[1] == '\0')
695 *closing_quote
= '\0';
698 /* Now that we've safely parsed the first half,
699 * put back ',' so outer layers can see it
704 if ((p
[0] == ':' || p
[0] == '.') && !has_parens
)
709 *argptr
= *argptr
+ 1;
710 if (p
[0] == '.' || p
[1] == ':')
712 char *saved_arg2
= *argptr
;
714 /* First check for "global" namespace specification,
715 of the form "::foo". If found, skip over the colons
716 and jump to normal symbol processing */
718 && ((*argptr
== p
) || (p
[-1] == ' ') || (p
[-1] == '\t')))
721 /* We have what looks like a class or namespace
722 scope specification (A::B), possibly with many
723 levels of namespaces or classes (A::B::C::D).
725 Some versions of the HP ANSI C++ compiler (as also possibly
726 other compilers) generate class/function/member names with
727 embedded double-colons if they are inside namespaces. To
728 handle this, we loop a few times, considering larger and
729 larger prefixes of the string as though they were single
730 symbols. So, if the initially supplied string is
731 A::B::C::D::foo, we have to look up "A", then "A::B",
732 then "A::B::C", then "A::B::C::D", and finally
733 "A::B::C::D::foo" as single, monolithic symbols, because
734 A, B, C or D may be namespaces.
736 Note that namespaces can nest only inside other
737 namespaces, and not inside classes. So we need only
738 consider *prefixes* of the string; there is no need to look up
739 "B::C" separately as a symbol in the previous example. */
741 p2
= p
; /* save for restart */
744 /* Extract the class name. */
746 while (p
!= *argptr
&& p
[-1] == ' ')
748 copy
= (char *) alloca (p
- *argptr
+ 1);
749 memcpy (copy
, *argptr
, p
- *argptr
);
750 copy
[p
- *argptr
] = 0;
752 /* Discard the class name from the arg. */
753 p
= p1
+ (p1
[0] == ':' ? 2 : 1);
754 while (*p
== ' ' || *p
== '\t')
758 sym_class
= lookup_symbol (copy
, 0, STRUCT_NAMESPACE
, 0,
759 (struct symtab
**) NULL
);
762 (t
= check_typedef (SYMBOL_TYPE (sym_class
)),
763 (TYPE_CODE (t
) == TYPE_CODE_STRUCT
764 || TYPE_CODE (t
) == TYPE_CODE_UNION
)))
766 /* Arg token is not digits => try it as a function name
767 Find the next token(everything up to end or next blank). */
769 && strchr (get_gdb_completer_quote_characters (),
772 p
= skip_quoted (*argptr
);
773 *argptr
= *argptr
+ 1;
778 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!= ':')
782 q = operator_chars (*argptr, &q1);
786 char *tmp = alloca (q1 - q + 1);
787 memcpy (tmp, q, q1 - q);
789 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
792 cplusplus_error (saved_arg, "no mangling for \"%s\"\n", tmp);
794 copy = (char*) alloca (3 + strlen(opname));
795 sprintf (copy, "__%s", opname);
801 copy
= (char *) alloca (p
- *argptr
+ 1);
802 memcpy (copy
, *argptr
, p
- *argptr
);
803 copy
[p
- *argptr
] = '\0';
805 && copy
[p
- *argptr
- 1]
806 && strchr (get_gdb_completer_quote_characters (),
807 copy
[p
- *argptr
- 1]) != NULL
)
808 copy
[p
- *argptr
- 1] = '\0';
811 /* no line number may be specified */
812 while (*p
== ' ' || *p
== '\t')
817 i1
= 0; /* counter for the symbol array */
818 sym_arr
= (struct symbol
**) alloca (total_number_of_methods (t
)
819 * sizeof (struct symbol
*));
821 if (destructor_name_p (copy
, t
))
823 /* Destructors are a special case. */
824 int m_index
, f_index
;
826 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
828 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, m_index
);
831 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, f_index
),
832 NULL
, VAR_NAMESPACE
, (int *) NULL
,
833 (struct symtab
**) NULL
);
839 i1
= find_methods (t
, copy
, sym_arr
);
842 /* There is exactly one field with that name. */
845 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
847 values
.sals
= (struct symtab_and_line
*)
848 xmalloc (sizeof (struct symtab_and_line
));
850 values
.sals
[0] = find_function_start_sal (sym
,
861 /* There is more than one field with that name
862 (overloaded). Ask the user which one to use. */
863 return decode_line_2 (sym_arr
, i1
, funfirstline
, canonical
);
869 if (is_operator_name (copy
))
871 tmp
= (char *) alloca (strlen (copy
+ 3) + 9);
872 strcpy (tmp
, "operator ");
873 strcat (tmp
, copy
+ 3);
878 cplusplus_error (saved_arg
,
879 "the class `%s' does not have destructor defined\n",
880 SYMBOL_SOURCE_NAME (sym_class
));
882 cplusplus_error (saved_arg
,
883 "the class %s does not have any method named %s\n",
884 SYMBOL_SOURCE_NAME (sym_class
), tmp
);
888 /* Move pointer up to next possible class/namespace token */
889 p
= p2
+ 1; /* restart with old value +1 */
890 /* Move pointer ahead to next double-colon */
891 while (*p
&& (p
[0] != ' ') && (p
[0] != '\t') && (p
[0] != '\''))
895 temp_end
= find_template_name_end (p
);
897 error ("malformed template specification in command");
900 else if ((p
[0] == ':') && (p
[1] == ':'))
901 break; /* found double-colon */
907 break; /* out of the while (1) */
909 p2
= p
; /* save restart for next time around */
910 *argptr
= saved_arg2
; /* restore argptr */
913 /* Last chance attempt -- check entire name as a symbol */
914 /* Use "copy" in preparation for jumping out of this block,
915 to be consistent with usage following the jump target */
916 copy
= (char *) alloca (p
- saved_arg2
+ 1);
917 memcpy (copy
, saved_arg2
, p
- saved_arg2
);
918 /* Note: if is_quoted should be true, we snuff out quote here anyway */
919 copy
[p
- saved_arg2
] = '\000';
920 /* Set argptr to skip over the name */
921 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
922 /* Look up entire name */
923 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
924 s
= (struct symtab
*) 0;
926 return symbol_found (funfirstline
, canonical
, copy
, sym
,
929 /* Couldn't find any interpretation as classes/namespaces, so give up */
930 /* The quotes are important if copy is empty. */
931 cplusplus_error (saved_arg
,
932 "Can't find member of namespace, class, struct, or union named \"%s\"\n",
938 /* Extract the file name. */
940 while (p
!= *argptr
&& p
[-1] == ' ')
942 if ((*p
== '"') && is_quote_enclosed
)
944 copy
= (char *) alloca (p
- *argptr
+ 1);
945 memcpy (copy
, *argptr
, p
- *argptr
);
946 /* It may have the ending quote right after the file name */
947 if (is_quote_enclosed
&& copy
[p
- *argptr
- 1] == '"')
948 copy
[p
- *argptr
- 1] = 0;
950 copy
[p
- *argptr
] = 0;
952 /* Find that file's data. */
953 s
= lookup_symtab (copy
);
956 if (!have_full_symbols () && !have_partial_symbols ())
957 error ("No symbol table is loaded. Use the \"file\" command.");
958 error ("No source file named %s.", copy
);
961 /* Discard the file name from the arg. */
963 while (*p
== ' ' || *p
== '\t')
968 /* No one really seems to know why this was added. It certainly
969 breaks the command line, though, whenever the passed
970 name is of the form ClassName::Method. This bit of code
971 singles out the class name, and if funfirstline is set (for
972 example, you are setting a breakpoint at this function),
973 you get an error. This did not occur with earlier
974 verions, so I am ifdef'ing this out. 3/29/99 */
977 /* Check if what we have till now is a symbol name */
979 /* We may be looking at a template instantiation such
980 as "foo<int>". Check here whether we know about it,
981 instead of falling through to the code below which
982 handles ordinary function names, because that code
983 doesn't like seeing '<' and '>' in a name -- the
984 skip_quoted call doesn't go past them. So see if we
985 can figure it out right now. */
987 copy
= (char *) alloca (p
- *argptr
+ 1);
988 memcpy (copy
, *argptr
, p
- *argptr
);
989 copy
[p
- *argptr
] = '\000';
990 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
993 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
994 return symbol_found (funfirstline
, canonical
, copy
, sym
,
997 /* Otherwise fall out from here and go to file/line spec
1002 /* S is specified file's symtab, or 0 if no file specified.
1003 arg no longer contains the file name. */
1005 /* Check whether arg is all digits (and sign) */
1008 if (*q
== '-' || *q
== '+')
1010 while (*q
>= '0' && *q
<= '9')
1013 if (q
!= *argptr
&& (*q
== 0 || *q
== ' ' || *q
== '\t' || *q
== ','))
1015 /* We found a token consisting of all digits -- at least one digit. */
1022 /* We might need a canonical line spec if no file was specified. */
1023 int need_canonical
= (s
== 0) ? 1 : 0;
1025 /* This is where we need to make sure that we have good defaults.
1026 We must guarantee that this section of code is never executed
1027 when we are called with just a function name, since
1028 set_default_source_symtab_and_line uses
1029 select_source_symtab that calls us with such an argument */
1031 if (s
== 0 && default_symtab
== 0)
1033 struct symtab_and_line cursal
;
1035 /* Make sure we have at least a default source file. */
1036 set_default_source_symtab_and_line ();
1037 cursal
= get_current_source_symtab_and_line ();
1039 default_symtab
= cursal
.symtab
;
1040 default_line
= cursal
.line
;
1043 if (**argptr
== '+')
1044 sign
= plus
, (*argptr
)++;
1045 else if (**argptr
== '-')
1046 sign
= minus
, (*argptr
)++;
1047 val
.line
= atoi (*argptr
);
1054 val
.line
= default_line
+ val
.line
;
1060 val
.line
= default_line
- val
.line
;
1065 break; /* No need to adjust val.line. */
1068 while (*q
== ' ' || *q
== '\t')
1074 /* It is possible that this source file has more than one symtab,
1075 and that the new line number specification has moved us from the
1076 default (in s) to a new one. */
1077 val
.symtab
= find_line_symtab (s
, val
.line
, NULL
, NULL
);
1078 if (val
.symtab
== 0)
1082 values
.sals
= (struct symtab_and_line
*)
1083 xmalloc (sizeof (struct symtab_and_line
));
1084 values
.sals
[0] = val
;
1087 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1091 /* Arg token is not digits => try it as a variable name
1092 Find the next token (everything up to end or next whitespace). */
1094 if (**argptr
== '$') /* May be a convenience variable */
1095 p
= skip_quoted (*argptr
+ (((*argptr
)[1] == '$') ? 2 : 1)); /* One or two $ chars possible */
1098 p
= skip_quoted (*argptr
);
1100 error ("Unmatched single quote.");
1102 else if (has_parens
)
1108 p
= skip_quoted (*argptr
);
1111 copy
= (char *) alloca (p
- *argptr
+ 1);
1112 memcpy (copy
, *argptr
, p
- *argptr
);
1113 copy
[p
- *argptr
] = '\0';
1116 && copy
[0] == copy
[p
- *argptr
- 1]
1117 && strchr (get_gdb_completer_quote_characters (), copy
[0]) != NULL
)
1119 copy
[p
- *argptr
- 1] = '\0';
1122 while (*p
== ' ' || *p
== '\t')
1126 /* If it starts with $: may be a legitimate variable or routine name
1127 (e.g. HP-UX millicode routines such as $$dyncall), or it may
1128 be history value, or it may be a convenience variable */
1134 int need_canonical
= 0;
1136 p
= (copy
[1] == '$') ? copy
+ 2 : copy
+ 1;
1137 while (*p
>= '0' && *p
<= '9')
1139 if (!*p
) /* reached end of token without hitting non-digit */
1141 /* We have a value history reference */
1142 sscanf ((copy
[1] == '$') ? copy
+ 2 : copy
+ 1, "%d", &index
);
1143 valx
= access_value_history ((copy
[1] == '$') ? -index
: index
);
1144 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1145 error ("History values used in line specs must have integer values.");
1149 /* Not all digits -- may be user variable/function or a
1150 convenience variable */
1152 /* Look up entire name as a symbol first */
1153 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
1154 s
= (struct symtab
*) 0;
1156 /* Symbol was found --> jump to normal symbol processing. */
1158 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1161 /* If symbol was not found, look in minimal symbol tables */
1162 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1163 /* Min symbol was found --> jump to minsym processing. */
1165 return minsym_found (funfirstline
, msymbol
);
1167 /* Not a user variable or function -- must be convenience variable */
1168 need_canonical
= (s
== 0) ? 1 : 0;
1169 valx
= value_of_internalvar (lookup_internalvar (copy
+ 1));
1170 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1171 error ("Convenience variables used in line specs must have integer values.");
1174 /* Either history value or convenience value from above, in valx */
1175 val
.symtab
= s
? s
: default_symtab
;
1176 val
.line
= value_as_long (valx
);
1179 values
.sals
= (struct symtab_and_line
*) xmalloc (sizeof val
);
1180 values
.sals
[0] = val
;
1184 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1190 /* Look up that token as a variable.
1191 If file specified, use that file's per-file block to start with. */
1193 sym
= lookup_symbol (copy
,
1194 (s
? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
)
1195 : get_selected_block (0)),
1196 VAR_NAMESPACE
, 0, &sym_symtab
);
1199 return symbol_found (funfirstline
, canonical
, copy
, sym
, s
, sym_symtab
);
1201 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1203 if (msymbol
!= NULL
)
1204 return minsym_found (funfirstline
, msymbol
);
1206 if (!have_full_symbols () &&
1207 !have_partial_symbols () && !have_minimal_symbols ())
1208 error ("No symbol table is loaded. Use the \"file\" command.");
1210 error ("Function \"%s\" not defined.", copy
);
1211 return values
; /* for lint */
1217 /* Now come some functions that are called from multiple places within
1220 /* We've found a symbol SYM to associate with our linespec; build a
1221 corresponding struct symtabs_and_lines. */
1223 static struct symtabs_and_lines
1224 symbol_found (int funfirstline
, char ***canonical
, char *copy
,
1225 struct symbol
*sym
, struct symtab
*s
,
1226 struct symtab
*sym_symtab
)
1228 struct symtabs_and_lines values
;
1230 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1232 /* Arg is the name of a function */
1233 values
.sals
= (struct symtab_and_line
*)
1234 xmalloc (sizeof (struct symtab_and_line
));
1235 values
.sals
[0] = find_function_start_sal (sym
, funfirstline
);
1238 /* Don't use the SYMBOL_LINE; if used at all it points to
1239 the line containing the parameters or thereabouts, not
1240 the first line of code. */
1242 /* We might need a canonical line spec if it is a static
1246 struct blockvector
*bv
= BLOCKVECTOR (sym_symtab
);
1247 struct block
*b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1248 if (lookup_block_symbol (b
, copy
, NULL
, VAR_NAMESPACE
) != NULL
)
1249 build_canonical_line_spec (values
.sals
, copy
, canonical
);
1256 error ("\"%s\" is not a function", copy
);
1257 else if (SYMBOL_LINE (sym
) != 0)
1259 /* We know its line number. */
1260 values
.sals
= (struct symtab_and_line
*)
1261 xmalloc (sizeof (struct symtab_and_line
));
1263 memset (&values
.sals
[0], 0, sizeof (values
.sals
[0]));
1264 values
.sals
[0].symtab
= sym_symtab
;
1265 values
.sals
[0].line
= SYMBOL_LINE (sym
);
1269 /* This can happen if it is compiled with a compiler which doesn't
1270 put out line numbers for variables. */
1271 /* FIXME: Shouldn't we just set .line and .symtab to zero
1272 and return? For example, "info line foo" could print
1274 error ("Line number not known for symbol \"%s\"", copy
);
1278 /* We've found a minimal symbol MSYMBOL to associate with our
1279 linespec; build a corresponding struct symtabs_and_lines. */
1281 static struct symtabs_and_lines
1282 minsym_found (int funfirstline
, struct minimal_symbol
*msymbol
)
1284 struct symtabs_and_lines values
;
1286 values
.sals
= (struct symtab_and_line
*)
1287 xmalloc (sizeof (struct symtab_and_line
));
1288 values
.sals
[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol
),
1289 (struct sec
*) 0, 0);
1290 values
.sals
[0].section
= SYMBOL_BFD_SECTION (msymbol
);
1293 values
.sals
[0].pc
+= FUNCTION_START_OFFSET
;
1294 values
.sals
[0].pc
= SKIP_PROLOGUE (values
.sals
[0].pc
);