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 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 void cplusplus_error (const char *name
, const char *fmt
, ...) ATTR_FORMAT (printf
, 2, 3);
53 static int total_number_of_methods (struct type
*type
);
55 static int find_methods (struct type
*, char *, struct symbol
**);
57 static void build_canonical_line_spec (struct symtab_and_line
*,
60 static char *find_toplevel_char (char *s
, char c
);
62 static struct symtabs_and_lines
decode_line_2 (struct symbol
*[],
66 symtabs_and_lines
symbol_found (int funfirstline
,
71 struct symtab
*sym_symtab
);
74 symtabs_and_lines
minsym_found (int funfirstline
,
75 struct minimal_symbol
*msymbol
);
77 /* Helper functions. */
79 /* Issue a helpful hint on using the command completion feature on
80 single quoted demangled C++ symbols as part of the completion
84 cplusplus_error (const char *name
, const char *fmt
, ...)
86 struct ui_file
*tmp_stream
;
87 tmp_stream
= mem_fileopen ();
88 make_cleanup_ui_file_delete (tmp_stream
);
93 vfprintf_unfiltered (tmp_stream
, fmt
, args
);
99 fprintf_unfiltered (tmp_stream
,
100 ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
101 "(Note leading single quote.)"),
103 error_stream (tmp_stream
);
106 /* Return the number of methods described for TYPE, including the
107 methods from types it derives from. This can't be done in the symbol
108 reader because the type of the baseclass might still be stubbed
109 when the definition of the derived class is parsed. */
112 total_number_of_methods (struct type
*type
)
117 CHECK_TYPEDEF (type
);
118 if (TYPE_CPLUS_SPECIFIC (type
) == NULL
)
120 count
= TYPE_NFN_FIELDS_TOTAL (type
);
122 for (n
= 0; n
< TYPE_N_BASECLASSES (type
); n
++)
123 count
+= total_number_of_methods (TYPE_BASECLASS (type
, n
));
128 /* Recursive helper function for decode_line_1.
129 Look for methods named NAME in type T.
130 Return number of matches.
131 Put matches in SYM_ARR, which should have been allocated with
132 a size of total_number_of_methods (T) * sizeof (struct symbol *).
133 Note that this function is g++ specific. */
136 find_methods (struct type
*t
, char *name
, struct symbol
**sym_arr
)
140 char *class_name
= type_name_no_tag (t
);
142 /* Ignore this class if it doesn't have a name. This is ugly, but
143 unless we figure out how to get the physname without the name of
144 the class, then the loop can't do any good. */
146 && (lookup_symbol (class_name
, (struct block
*) NULL
,
147 STRUCT_NAMESPACE
, (int *) NULL
,
148 (struct symtab
**) NULL
)))
151 int name_len
= strlen (name
);
155 /* Loop over each method name. At this level, all overloads of a name
156 are counted as a single name. There is an inner loop which loops over
159 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
164 char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
167 if (strncmp (method_name
, "__", 2) == 0 ||
168 strncmp (method_name
, "op", 2) == 0 ||
169 strncmp (method_name
, "type", 4) == 0)
171 if (cplus_demangle_opname (method_name
, dem_opname
, DMGL_ANSI
))
172 method_name
= dem_opname
;
173 else if (cplus_demangle_opname (method_name
, dem_opname
, 0))
174 method_name
= dem_opname
;
177 if (strcmp_iw (name
, method_name
) == 0)
178 /* Find all the overloaded methods with that name. */
179 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
186 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
188 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
192 tmp_name
= gdb_mangle_name (t
,
195 phys_name
= alloca (strlen (tmp_name
) + 1);
196 strcpy (phys_name
, tmp_name
);
200 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
202 /* Destructor is handled by caller, dont add it to the list */
203 if (is_destructor_name (phys_name
) != 0)
206 sym_arr
[i1
] = lookup_symbol (phys_name
,
209 (struct symtab
**) NULL
);
214 /* This error message gets printed, but the method
215 still seems to be found
216 fputs_filtered("(Cannot find method ", gdb_stdout);
217 fprintf_symbol_filtered (gdb_stdout, phys_name,
219 DMGL_PARAMS | DMGL_ANSI);
220 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
224 else if (strncmp (class_name
, name
, name_len
) == 0
225 && (class_name
[name_len
] == '\0'
226 || class_name
[name_len
] == '<'))
228 /* For GCC 3.x and stabs, constructors and destructors have names
229 like __base_ctor and __complete_dtor. Check the physname for now
230 if we're looking for a constructor. */
232 = TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
239 f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
241 /* GCC 3.x will never produce stabs stub methods, so we don't need
242 to handle this case. */
243 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
245 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
246 if (! is_constructor_name (phys_name
))
249 /* If this method is actually defined, include it in the
251 sym_arr
[i1
] = lookup_symbol (phys_name
,
254 (struct symtab
**) NULL
);
262 /* Only search baseclasses if there is no match yet, since names in
263 derived classes override those in baseclasses.
265 FIXME: The above is not true; it is only true of member functions
266 if they have the same number of arguments (??? - section 13.1 of the
267 ARM says the function members are not in the same scope but doesn't
268 really spell out the rules in a way I understand. In any case, if
269 the number of arguments differ this is a case in which we can overload
270 rather than hiding without any problem, and gcc 2.4.5 does overload
271 rather than hiding in this case). */
274 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
275 i1
+= find_methods (TYPE_BASECLASS (t
, ibase
), name
, sym_arr
+ i1
);
280 /* Helper function for decode_line_1.
281 Build a canonical line spec in CANONICAL if it is non-NULL and if
282 the SAL has a symtab.
283 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
284 If SYMNAME is NULL the line number from SAL is used and the canonical
285 line spec is `filename:linenum'. */
288 build_canonical_line_spec (struct symtab_and_line
*sal
, char *symname
,
291 char **canonical_arr
;
292 char *canonical_name
;
294 struct symtab
*s
= sal
->symtab
;
296 if (s
== (struct symtab
*) NULL
297 || s
->filename
== (char *) NULL
298 || canonical
== (char ***) NULL
)
301 canonical_arr
= (char **) xmalloc (sizeof (char *));
302 *canonical
= canonical_arr
;
304 filename
= s
->filename
;
307 canonical_name
= xmalloc (strlen (filename
) + strlen (symname
) + 2);
308 sprintf (canonical_name
, "%s:%s", filename
, symname
);
312 canonical_name
= xmalloc (strlen (filename
) + 30);
313 sprintf (canonical_name
, "%s:%d", filename
, sal
->line
);
315 canonical_arr
[0] = canonical_name
;
320 /* Find an instance of the character C in the string S that is outside
321 of all parenthesis pairs, single-quoted strings, and double-quoted
322 strings. Also, ignore the char within a template name, like a ','
323 within foo<int, int>. */
326 find_toplevel_char (char *s
, char c
)
328 int quoted
= 0; /* zero if we're not in quotes;
329 '"' if we're in a double-quoted string;
330 '\'' if we're in a single-quoted string. */
331 int depth
= 0; /* number of unclosed parens we've seen */
334 for (scan
= s
; *scan
; scan
++)
340 else if (*scan
== '\\' && *(scan
+ 1))
343 else if (*scan
== c
&& ! quoted
&& depth
== 0)
345 else if (*scan
== '"' || *scan
== '\'')
347 else if (*scan
== '(' || *scan
== '<')
349 else if ((*scan
== ')' || *scan
== '>') && depth
> 0)
356 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
357 operate on (ask user if necessary).
358 If CANONICAL is non-NULL return a corresponding array of mangled names
359 as canonical line specs there. */
361 static struct symtabs_and_lines
362 decode_line_2 (struct symbol
*sym_arr
[], int nelts
, int funfirstline
,
365 struct symtabs_and_lines values
, return_values
;
370 struct cleanup
*old_chain
;
371 char **canonical_arr
= (char **) NULL
;
373 values
.sals
= (struct symtab_and_line
*)
374 alloca (nelts
* sizeof (struct symtab_and_line
));
375 return_values
.sals
= (struct symtab_and_line
*)
376 xmalloc (nelts
* sizeof (struct symtab_and_line
));
377 old_chain
= make_cleanup (xfree
, return_values
.sals
);
381 canonical_arr
= (char **) xmalloc (nelts
* sizeof (char *));
382 make_cleanup (xfree
, canonical_arr
);
383 memset (canonical_arr
, 0, nelts
* sizeof (char *));
384 *canonical
= canonical_arr
;
388 printf_unfiltered ("[0] cancel\n[1] all\n");
391 init_sal (&return_values
.sals
[i
]); /* initialize to zeroes */
392 init_sal (&values
.sals
[i
]);
393 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
395 values
.sals
[i
] = find_function_start_sal (sym_arr
[i
], funfirstline
);
396 printf_unfiltered ("[%d] %s at %s:%d\n",
398 SYMBOL_SOURCE_NAME (sym_arr
[i
]),
399 values
.sals
[i
].symtab
->filename
,
400 values
.sals
[i
].line
);
403 printf_unfiltered ("?HERE\n");
407 if ((prompt
= getenv ("PS2")) == NULL
)
411 args
= command_line_input (prompt
, 0, "overload-choice");
413 if (args
== 0 || *args
== 0)
414 error_no_arg ("one or more choice numbers");
422 while (*arg1
>= '0' && *arg1
<= '9')
424 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
425 error ("Arguments must be choice numbers.");
435 for (i
= 0; i
< nelts
; i
++)
437 if (canonical_arr
[i
] == NULL
)
439 symname
= SYMBOL_NAME (sym_arr
[i
]);
440 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
444 memcpy (return_values
.sals
, values
.sals
,
445 (nelts
* sizeof (struct symtab_and_line
)));
446 return_values
.nelts
= nelts
;
447 discard_cleanups (old_chain
);
448 return return_values
;
451 if (num
>= nelts
+ 2)
453 printf_unfiltered ("No choice number %d.\n", num
);
458 if (values
.sals
[num
].pc
)
462 symname
= SYMBOL_NAME (sym_arr
[num
]);
463 make_cleanup (xfree
, symname
);
464 canonical_arr
[i
] = savestring (symname
, strlen (symname
));
466 return_values
.sals
[i
++] = values
.sals
[num
];
467 values
.sals
[num
].pc
= 0;
471 printf_unfiltered ("duplicate request for %d ignored.\n", num
);
476 while (*args
== ' ' || *args
== '\t')
479 return_values
.nelts
= i
;
480 discard_cleanups (old_chain
);
481 return return_values
;
484 /* The parser of linespec itself. */
486 /* Parse a string that specifies a line number.
487 Pass the address of a char * variable; that variable will be
488 advanced over the characters actually parsed.
492 LINENUM -- that line number in current file. PC returned is 0.
493 FILE:LINENUM -- that line in that file. PC returned is 0.
494 FUNCTION -- line number of openbrace of that function.
495 PC returned is the start of the function.
496 VARIABLE -- line number of definition of that variable.
498 FILE:FUNCTION -- likewise, but prefer functions in that file.
499 *EXPR -- line in which address EXPR appears.
501 This may all be followed by an "if EXPR", which we ignore.
503 FUNCTION may be an undebuggable function found in minimal symbol table.
505 If the argument FUNFIRSTLINE is nonzero, we want the first line
506 of real code inside a function when a function is specified, and it is
507 not OK to specify a variable or type to get its line number.
509 DEFAULT_SYMTAB specifies the file to use if none is specified.
510 It defaults to current_source_symtab.
511 DEFAULT_LINE specifies the line number to use for relative
512 line numbers (that start with signs). Defaults to current_source_line.
513 If CANONICAL is non-NULL, store an array of strings containing the canonical
514 line specs there if necessary. Currently overloaded member functions and
515 line numbers or static functions without a filename yield a canonical
516 line spec. The array and the line spec strings are allocated on the heap,
517 it is the callers responsibility to free them.
519 Note that it is possible to return zero for the symtab
520 if no file is validly specified. Callers must check that.
521 Also, the line number returned may be invalid. */
523 /* We allow single quotes in various places. This is a hideous
524 kludge, which exists because the completer can't yet deal with the
525 lack of single quotes. FIXME: write a linespec_completer which we
526 can use as appropriate instead of make_symbol_completion_list. */
528 struct symtabs_and_lines
529 decode_line_1 (char **argptr
, int funfirstline
, struct symtab
*default_symtab
,
530 int default_line
, char ***canonical
)
532 struct symtabs_and_lines values
;
533 struct symtab_and_line val
;
534 register char *p
, *p1
;
539 register struct symtab
*s
= NULL
;
541 register struct symbol
*sym
;
542 /* The symtab that SYM was found in. */
543 struct symtab
*sym_symtab
;
545 register struct minimal_symbol
*msymbol
;
547 struct symbol
*sym_class
;
549 /* This is NULL if there are no parens in *ARGPTR, or a pointer to
550 the closing parenthesis if there are parens. */
552 /* This says whether or not something in *ARGPTR is quoted with
553 completer_quotes (i.e. with single quotes). */
555 /* Is part of *ARGPTR is enclosed in double quotes? */
556 int is_quote_enclosed
;
557 struct symbol
**sym_arr
;
559 char *saved_arg
= *argptr
;
560 extern char *gdb_completer_quote_characters
;
562 init_sal (&val
); /* initialize to zeroes */
564 /* Defaults have defaults. */
566 initialize_defaults (&default_symtab
, &default_line
);
568 /* See if arg is *PC */
571 return decode_indirect (argptr
);
573 /* Set various flags.
574 * 'paren_pointer' is important for overload checking, where
575 * we allow things like:
576 * (gdb) break c::f(int)
579 set_flags (*argptr
, &is_quoted
, &paren_pointer
);
581 /* Check to see if it's a multipart linespec (with colons or
584 /* Locate the end of the first half of the linespec. */
586 p
= locate_first_half (argptr
, &is_quote_enclosed
);
588 /* Does it look like there actually were two parts? */
590 if ((p
[0] == ':' || p
[0] == '.') && paren_pointer
== NULL
)
595 *argptr
= *argptr
+ 1;
596 if (p
[0] == '.' || p
[1] == ':')
598 char *saved_arg2
= *argptr
;
600 /* First check for "global" namespace specification,
601 of the form "::foo". If found, skip over the colons
602 and jump to normal symbol processing */
604 && ((*argptr
== p
) || (p
[-1] == ' ') || (p
[-1] == '\t')))
607 /* We have what looks like a class or namespace
608 scope specification (A::B), possibly with many
609 levels of namespaces or classes (A::B::C::D).
611 Some versions of the HP ANSI C++ compiler (as also possibly
612 other compilers) generate class/function/member names with
613 embedded double-colons if they are inside namespaces. To
614 handle this, we loop a few times, considering larger and
615 larger prefixes of the string as though they were single
616 symbols. So, if the initially supplied string is
617 A::B::C::D::foo, we have to look up "A", then "A::B",
618 then "A::B::C", then "A::B::C::D", and finally
619 "A::B::C::D::foo" as single, monolithic symbols, because
620 A, B, C or D may be namespaces.
622 Note that namespaces can nest only inside other
623 namespaces, and not inside classes. So we need only
624 consider *prefixes* of the string; there is no need to look up
625 "B::C" separately as a symbol in the previous example. */
627 p2
= p
; /* save for restart */
630 /* Extract the class name. */
632 while (p
!= *argptr
&& p
[-1] == ' ')
634 copy
= (char *) alloca (p
- *argptr
+ 1);
635 memcpy (copy
, *argptr
, p
- *argptr
);
636 copy
[p
- *argptr
] = 0;
638 /* Discard the class name from the arg. */
639 p
= p1
+ (p1
[0] == ':' ? 2 : 1);
640 while (*p
== ' ' || *p
== '\t')
644 sym_class
= lookup_symbol (copy
, 0, STRUCT_NAMESPACE
, 0,
645 (struct symtab
**) NULL
);
648 (t
= check_typedef (SYMBOL_TYPE (sym_class
)),
649 (TYPE_CODE (t
) == TYPE_CODE_STRUCT
650 || TYPE_CODE (t
) == TYPE_CODE_UNION
)))
652 /* Arg token is not digits => try it as a function name
653 Find the next token(everything up to end or next blank). */
655 && strchr (get_gdb_completer_quote_characters (),
658 p
= skip_quoted (*argptr
);
659 *argptr
= *argptr
+ 1;
664 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!= ':')
668 q = operator_chars (*argptr, &q1);
672 char *tmp = alloca (q1 - q + 1);
673 memcpy (tmp, q, q1 - q);
675 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
678 cplusplus_error (saved_arg, "no mangling for \"%s\"\n", tmp);
680 copy = (char*) alloca (3 + strlen(opname));
681 sprintf (copy, "__%s", opname);
687 copy
= (char *) alloca (p
- *argptr
+ 1);
688 memcpy (copy
, *argptr
, p
- *argptr
);
689 copy
[p
- *argptr
] = '\0';
691 && copy
[p
- *argptr
- 1]
692 && strchr (get_gdb_completer_quote_characters (),
693 copy
[p
- *argptr
- 1]) != NULL
)
694 copy
[p
- *argptr
- 1] = '\0';
697 /* no line number may be specified */
698 while (*p
== ' ' || *p
== '\t')
703 i1
= 0; /* counter for the symbol array */
704 sym_arr
= (struct symbol
**) alloca (total_number_of_methods (t
)
705 * sizeof (struct symbol
*));
707 if (destructor_name_p (copy
, t
))
709 /* Destructors are a special case. */
710 int m_index
, f_index
;
712 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
714 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, m_index
);
717 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, f_index
),
718 NULL
, VAR_NAMESPACE
, (int *) NULL
,
719 (struct symtab
**) NULL
);
725 i1
= find_methods (t
, copy
, sym_arr
);
728 /* There is exactly one field with that name. */
731 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
733 values
.sals
= (struct symtab_and_line
*)
734 xmalloc (sizeof (struct symtab_and_line
));
736 values
.sals
[0] = find_function_start_sal (sym
,
747 /* There is more than one field with that name
748 (overloaded). Ask the user which one to use. */
749 return decode_line_2 (sym_arr
, i1
, funfirstline
, canonical
);
755 if (is_operator_name (copy
))
757 tmp
= (char *) alloca (strlen (copy
+ 3) + 9);
758 strcpy (tmp
, "operator ");
759 strcat (tmp
, copy
+ 3);
764 cplusplus_error (saved_arg
,
765 "the class `%s' does not have destructor defined\n",
766 SYMBOL_SOURCE_NAME (sym_class
));
768 cplusplus_error (saved_arg
,
769 "the class %s does not have any method named %s\n",
770 SYMBOL_SOURCE_NAME (sym_class
), tmp
);
774 /* Move pointer up to next possible class/namespace token */
775 p
= p2
+ 1; /* restart with old value +1 */
776 /* Move pointer ahead to next double-colon */
777 while (*p
&& (p
[0] != ' ') && (p
[0] != '\t') && (p
[0] != '\''))
781 temp_end
= find_template_name_end (p
);
783 error ("malformed template specification in command");
786 else if ((p
[0] == ':') && (p
[1] == ':'))
787 break; /* found double-colon */
793 break; /* out of the while (1) */
795 p2
= p
; /* save restart for next time around */
796 *argptr
= saved_arg2
; /* restore argptr */
799 /* Last chance attempt -- check entire name as a symbol */
800 /* Use "copy" in preparation for jumping out of this block,
801 to be consistent with usage following the jump target */
802 copy
= (char *) alloca (p
- saved_arg2
+ 1);
803 memcpy (copy
, saved_arg2
, p
- saved_arg2
);
804 /* Note: if is_quoted should be true, we snuff out quote here anyway */
805 copy
[p
- saved_arg2
] = '\000';
806 /* Set argptr to skip over the name */
807 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
808 /* Look up entire name */
809 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
810 s
= (struct symtab
*) 0;
812 return symbol_found (funfirstline
, canonical
, copy
, sym
,
815 /* Couldn't find any interpretation as classes/namespaces, so give up */
816 /* The quotes are important if copy is empty. */
817 cplusplus_error (saved_arg
,
818 "Can't find member of namespace, class, struct, or union named \"%s\"\n",
824 /* Extract the file name. */
826 while (p
!= *argptr
&& p
[-1] == ' ')
828 if ((*p
== '"') && is_quote_enclosed
)
830 copy
= (char *) alloca (p
- *argptr
+ 1);
831 memcpy (copy
, *argptr
, p
- *argptr
);
832 /* It may have the ending quote right after the file name */
833 if (is_quote_enclosed
&& copy
[p
- *argptr
- 1] == '"')
834 copy
[p
- *argptr
- 1] = 0;
836 copy
[p
- *argptr
] = 0;
838 /* Find that file's data. */
839 s
= lookup_symtab (copy
);
842 if (!have_full_symbols () && !have_partial_symbols ())
843 error ("No symbol table is loaded. Use the \"file\" command.");
844 error ("No source file named %s.", copy
);
847 /* Discard the file name from the arg. */
849 while (*p
== ' ' || *p
== '\t')
854 /* No one really seems to know why this was added. It certainly
855 breaks the command line, though, whenever the passed
856 name is of the form ClassName::Method. This bit of code
857 singles out the class name, and if funfirstline is set (for
858 example, you are setting a breakpoint at this function),
859 you get an error. This did not occur with earlier
860 verions, so I am ifdef'ing this out. 3/29/99 */
863 /* Check if what we have till now is a symbol name */
865 /* We may be looking at a template instantiation such
866 as "foo<int>". Check here whether we know about it,
867 instead of falling through to the code below which
868 handles ordinary function names, because that code
869 doesn't like seeing '<' and '>' in a name -- the
870 skip_quoted call doesn't go past them. So see if we
871 can figure it out right now. */
873 copy
= (char *) alloca (p
- *argptr
+ 1);
874 memcpy (copy
, *argptr
, p
- *argptr
);
875 copy
[p
- *argptr
] = '\000';
876 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
879 *argptr
= (*p
== '\'') ? p
+ 1 : p
;
880 return symbol_found (funfirstline
, canonical
, copy
, sym
,
883 /* Otherwise fall out from here and go to file/line spec
888 /* S is specified file's symtab, or 0 if no file specified.
889 arg no longer contains the file name. */
891 /* Check whether arg is all digits (and sign) */
894 if (*q
== '-' || *q
== '+')
896 while (*q
>= '0' && *q
<= '9')
899 if (q
!= *argptr
&& (*q
== 0 || *q
== ' ' || *q
== '\t' || *q
== ','))
901 /* We found a token consisting of all digits -- at least one digit. */
908 /* We might need a canonical line spec if no file was specified. */
909 int need_canonical
= (s
== 0) ? 1 : 0;
911 /* This is where we need to make sure that we have good defaults.
912 We must guarantee that this section of code is never executed
913 when we are called with just a function name, since
914 set_default_source_symtab_and_line uses
915 select_source_symtab that calls us with such an argument */
917 if (s
== 0 && default_symtab
== 0)
919 /* Make sure we have at least a default source file. */
920 set_default_source_symtab_and_line ();
921 initialize_defaults (&default_symtab
, &default_line
);
925 sign
= plus
, (*argptr
)++;
926 else if (**argptr
== '-')
927 sign
= minus
, (*argptr
)++;
928 val
.line
= atoi (*argptr
);
935 val
.line
= default_line
+ val
.line
;
941 val
.line
= default_line
- val
.line
;
946 break; /* No need to adjust val.line. */
949 while (*q
== ' ' || *q
== '\t')
955 /* It is possible that this source file has more than one symtab,
956 and that the new line number specification has moved us from the
957 default (in s) to a new one. */
958 val
.symtab
= find_line_symtab (s
, val
.line
, NULL
, NULL
);
963 values
.sals
= (struct symtab_and_line
*)
964 xmalloc (sizeof (struct symtab_and_line
));
965 values
.sals
[0] = val
;
968 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
972 /* Arg token is not digits => try it as a variable name
973 Find the next token (everything up to end or next whitespace). */
975 if (**argptr
== '$') /* May be a convenience variable */
976 p
= skip_quoted (*argptr
+ (((*argptr
)[1] == '$') ? 2 : 1)); /* One or two $ chars possible */
979 p
= skip_quoted (*argptr
);
981 error ("Unmatched single quote.");
983 else if (paren_pointer
!= NULL
)
985 p
= paren_pointer
+ 1;
989 p
= skip_quoted (*argptr
);
992 copy
= (char *) alloca (p
- *argptr
+ 1);
993 memcpy (copy
, *argptr
, p
- *argptr
);
994 copy
[p
- *argptr
] = '\0';
997 && copy
[0] == copy
[p
- *argptr
- 1]
998 && strchr (get_gdb_completer_quote_characters (), copy
[0]) != NULL
)
1000 copy
[p
- *argptr
- 1] = '\0';
1003 while (*p
== ' ' || *p
== '\t')
1007 /* If it starts with $: may be a legitimate variable or routine name
1008 (e.g. HP-UX millicode routines such as $$dyncall), or it may
1009 be history value, or it may be a convenience variable */
1015 int need_canonical
= 0;
1017 p
= (copy
[1] == '$') ? copy
+ 2 : copy
+ 1;
1018 while (*p
>= '0' && *p
<= '9')
1020 if (!*p
) /* reached end of token without hitting non-digit */
1022 /* We have a value history reference */
1023 sscanf ((copy
[1] == '$') ? copy
+ 2 : copy
+ 1, "%d", &index
);
1024 valx
= access_value_history ((copy
[1] == '$') ? -index
: index
);
1025 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1026 error ("History values used in line specs must have integer values.");
1030 /* Not all digits -- may be user variable/function or a
1031 convenience variable */
1033 /* Look up entire name as a symbol first */
1034 sym
= lookup_symbol (copy
, 0, VAR_NAMESPACE
, 0, &sym_symtab
);
1035 s
= (struct symtab
*) 0;
1037 /* Symbol was found --> jump to normal symbol processing. */
1039 return symbol_found (funfirstline
, canonical
, copy
, sym
,
1042 /* If symbol was not found, look in minimal symbol tables */
1043 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1044 /* Min symbol was found --> jump to minsym processing. */
1046 return minsym_found (funfirstline
, msymbol
);
1048 /* Not a user variable or function -- must be convenience variable */
1049 need_canonical
= (s
== 0) ? 1 : 0;
1050 valx
= value_of_internalvar (lookup_internalvar (copy
+ 1));
1051 if (TYPE_CODE (VALUE_TYPE (valx
)) != TYPE_CODE_INT
)
1052 error ("Convenience variables used in line specs must have integer values.");
1055 /* Either history value or convenience value from above, in valx */
1056 val
.symtab
= s
? s
: default_symtab
;
1057 val
.line
= value_as_long (valx
);
1060 values
.sals
= (struct symtab_and_line
*) xmalloc (sizeof val
);
1061 values
.sals
[0] = val
;
1065 build_canonical_line_spec (values
.sals
, NULL
, canonical
);
1071 /* Look up that token as a variable.
1072 If file specified, use that file's per-file block to start with. */
1074 sym
= lookup_symbol (copy
,
1075 (s
? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
)
1076 : get_selected_block (0)),
1077 VAR_NAMESPACE
, 0, &sym_symtab
);
1080 return symbol_found (funfirstline
, canonical
, copy
, sym
, s
, sym_symtab
);
1082 msymbol
= lookup_minimal_symbol (copy
, NULL
, NULL
);
1084 if (msymbol
!= NULL
)
1085 return minsym_found (funfirstline
, msymbol
);
1087 if (!have_full_symbols () &&
1088 !have_partial_symbols () && !have_minimal_symbols ())
1089 error ("No symbol table is loaded. Use the \"file\" command.");
1091 error ("Function \"%s\" not defined.", copy
);
1092 return values
; /* for lint */
1097 /* Now, still more helper functions. */
1099 /* NOTE: carlton/2002-11-07: Some of these have non-obvious side
1100 effects. In particular, if a function is passed ARGPTR as an
1101 argument, it modifies what ARGPTR points to. (Typically, it
1102 advances *ARGPTR past whatever substring it has just looked
1105 /* First, some functions to initialize stuff at the beggining of the
1109 initialize_defaults (struct symtab
**default_symtab
, int *default_line
)
1111 if (*default_symtab
== 0)
1113 /* Use whatever we have for the default source line. We don't use
1114 get_current_or_default_symtab_and_line as it can recurse and call
1116 struct symtab_and_line cursal
=
1117 get_current_source_symtab_and_line ();
1119 *default_symtab
= cursal
.symtab
;
1120 *default_line
= cursal
.line
;
1125 set_flags (char *arg
, int *is_quoted
, char **paren_pointer
)
1130 /* 'has_if' is for the syntax:
1131 * (gdb) break foo if (a==b)
1133 if ((ii
= strstr (arg
, " if ")) != NULL
||
1134 (ii
= strstr (arg
, "\tif ")) != NULL
||
1135 (ii
= strstr (arg
, " if\t")) != NULL
||
1136 (ii
= strstr (arg
, "\tif\t")) != NULL
||
1137 (ii
= strstr (arg
, " if(")) != NULL
||
1138 (ii
= strstr (arg
, "\tif( ")) != NULL
)
1140 /* Temporarily zap out "if (condition)" to not
1141 * confuse the parenthesis-checking code below.
1142 * This is undone below. Do not change ii!!
1150 && strchr (get_gdb_completer_quote_characters (),
1153 *paren_pointer
= strchr (arg
, '(');
1154 if (*paren_pointer
!= NULL
)
1155 *paren_pointer
= strrchr (*paren_pointer
, ')');
1157 /* Now that we're safely past the paren_pointer check,
1158 * put back " if (condition)" so outer layers can see it
1166 /* Decode arg of the form *PC. */
1168 static struct symtabs_and_lines
1169 decode_indirect (char **argptr
)
1171 struct symtabs_and_lines values
;
1175 pc
= parse_and_eval_address_1 (argptr
);
1177 values
.sals
= (struct symtab_and_line
*)
1178 xmalloc (sizeof (struct symtab_and_line
));
1181 values
.sals
[0] = find_pc_line (pc
, 0);
1182 values
.sals
[0].pc
= pc
;
1183 values
.sals
[0].section
= find_pc_overlay (pc
);
1190 /* Locate the first half of the linespec, ending in a colon, period,
1191 or whitespace. (More or less.) Also, check to see if *ARGPTR is
1192 enclosed in double quotes; if so, set is_quote_enclosed, advance
1193 ARGPTR past that and zero out the trailing double quote. */
1196 locate_first_half (char **argptr
, int *is_quote_enclosed
)
1202 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
1203 and we must isolate the first half. Outer layers will call again later
1204 for the second half.
1206 Don't count commas that appear in argument lists of overloaded
1207 functions, or in quoted strings. It's stupid to go to this much
1208 trouble when the rest of the function is such an obvious roach hotel. */
1209 ii
= find_toplevel_char (*argptr
, ',');
1210 has_comma
= (ii
!= 0);
1212 /* Temporarily zap out second half to not
1213 * confuse the code below.
1214 * This is undone below. Do not change ii!!
1221 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1222 /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
1223 /* Look for ':', but ignore inside of <> */
1228 *is_quote_enclosed
= 1;
1233 *is_quote_enclosed
= 0;
1238 char *temp_end
= find_template_name_end (p
);
1240 error ("malformed template specification in command");
1243 /* Check for the end of the first half of the linespec. End of line,
1244 a tab, a double colon or the last single colon, or a space. But
1245 if enclosed in double quotes we do not break on enclosed spaces */
1249 && ((p
[1] == ':') || (strchr (p
+ 1, ':') == NULL
)))
1250 || ((p
[0] == ' ') && !*is_quote_enclosed
))
1252 if (p
[0] == '.' && strchr (p
, ':') == NULL
) /* Java qualified method. */
1254 /* Find the *last* '.', since the others are package qualifiers. */
1255 for (p1
= p
; *p1
; p1
++)
1263 while (p
[0] == ' ' || p
[0] == '\t')
1266 /* if the closing double quote was left at the end, remove it */
1267 if (*is_quote_enclosed
)
1269 char *closing_quote
= strchr (p
- 1, '"');
1270 if (closing_quote
&& closing_quote
[1] == '\0')
1271 *closing_quote
= '\0';
1274 /* Now that we've safely parsed the first half,
1275 * put back ',' so outer layers can see it
1285 /* Now come some functions that are called from multiple places within
1288 /* We've found a symbol SYM to associate with our linespec; build a
1289 corresponding struct symtabs_and_lines. */
1291 static struct symtabs_and_lines
1292 symbol_found (int funfirstline
, char ***canonical
, char *copy
,
1293 struct symbol
*sym
, struct symtab
*s
,
1294 struct symtab
*sym_symtab
)
1296 struct symtabs_and_lines values
;
1298 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1300 /* Arg is the name of a function */
1301 values
.sals
= (struct symtab_and_line
*)
1302 xmalloc (sizeof (struct symtab_and_line
));
1303 values
.sals
[0] = find_function_start_sal (sym
, funfirstline
);
1306 /* Don't use the SYMBOL_LINE; if used at all it points to
1307 the line containing the parameters or thereabouts, not
1308 the first line of code. */
1310 /* We might need a canonical line spec if it is a static
1314 struct blockvector
*bv
= BLOCKVECTOR (sym_symtab
);
1315 struct block
*b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1316 if (lookup_block_symbol (b
, copy
, NULL
, VAR_NAMESPACE
) != NULL
)
1317 build_canonical_line_spec (values
.sals
, copy
, canonical
);
1324 error ("\"%s\" is not a function", copy
);
1325 else if (SYMBOL_LINE (sym
) != 0)
1327 /* We know its line number. */
1328 values
.sals
= (struct symtab_and_line
*)
1329 xmalloc (sizeof (struct symtab_and_line
));
1331 memset (&values
.sals
[0], 0, sizeof (values
.sals
[0]));
1332 values
.sals
[0].symtab
= sym_symtab
;
1333 values
.sals
[0].line
= SYMBOL_LINE (sym
);
1337 /* This can happen if it is compiled with a compiler which doesn't
1338 put out line numbers for variables. */
1339 /* FIXME: Shouldn't we just set .line and .symtab to zero
1340 and return? For example, "info line foo" could print
1342 error ("Line number not known for symbol \"%s\"", copy
);
1346 /* We've found a minimal symbol MSYMBOL to associate with our
1347 linespec; build a corresponding struct symtabs_and_lines. */
1349 static struct symtabs_and_lines
1350 minsym_found (int funfirstline
, struct minimal_symbol
*msymbol
)
1352 struct symtabs_and_lines values
;
1354 values
.sals
= (struct symtab_and_line
*)
1355 xmalloc (sizeof (struct symtab_and_line
));
1356 values
.sals
[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol
),
1357 (struct sec
*) 0, 0);
1358 values
.sals
[0].section
= SYMBOL_BFD_SECTION (msymbol
);
1361 values
.sals
[0].pc
+= FUNCTION_START_OFFSET
;
1362 values
.sals
[0].pc
= SKIP_PROLOGUE (values
.sals
[0].pc
);