xref: /openbsd-src/gnu/usr.bin/binutils/gdb/linespec.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
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, 2004
4    Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7 
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.
12 
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.
17 
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.  */
22 
23 #include "defs.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "command.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "source.h"
30 #include "demangle.h"
31 #include "value.h"
32 #include "completer.h"
33 #include "cp-abi.h"
34 #include "parser-defs.h"
35 #include "block.h"
36 #include "objc-lang.h"
37 #include "linespec.h"
38 
39 /* We share this one with symtab.c, but it is not exported widely. */
40 
41 extern char *operator_chars (char *, char **);
42 
43 /* Prototypes for local functions */
44 
45 static void initialize_defaults (struct symtab **default_symtab,
46 				 int *default_line);
47 
48 static void set_flags (char *arg, int *is_quoted, char **paren_pointer);
49 
50 static struct symtabs_and_lines decode_indirect (char **argptr);
51 
52 static char *locate_first_half (char **argptr, int *is_quote_enclosed);
53 
54 static struct symtabs_and_lines decode_objc (char **argptr,
55 					     int funfirstline,
56 					     struct symtab *file_symtab,
57 					     char ***canonical,
58 					     char *saved_arg);
59 
60 static struct symtabs_and_lines decode_compound (char **argptr,
61 						 int funfirstline,
62 						 char ***canonical,
63 						 char *saved_arg,
64 						 char *p);
65 
66 static struct symbol *lookup_prefix_sym (char **argptr, char *p);
67 
68 static struct symtabs_and_lines find_method (int funfirstline,
69 					     char ***canonical,
70 					     char *saved_arg,
71 					     char *copy,
72 					     struct type *t,
73 					     struct symbol *sym_class);
74 
75 static int collect_methods (char *copy, struct type *t,
76 			    struct symbol **sym_arr);
77 
78 static NORETURN void cplusplus_error (const char *name,
79 				      const char *fmt, ...)
80      ATTR_NORETURN ATTR_FORMAT (printf, 2, 3);
81 
82 static int total_number_of_methods (struct type *type);
83 
84 static int find_methods (struct type *, char *, struct symbol **);
85 
86 static int add_matching_methods (int method_counter, struct type *t,
87 				 struct symbol **sym_arr);
88 
89 static int add_constructors (int method_counter, struct type *t,
90 			     struct symbol **sym_arr);
91 
92 static void build_canonical_line_spec (struct symtab_and_line *,
93 				       char *, char ***);
94 
95 static char *find_toplevel_char (char *s, char c);
96 
97 static int is_objc_method_format (const char *s);
98 
99 static struct symtabs_and_lines decode_line_2 (struct symbol *[],
100 					       int, int, char ***);
101 
102 static struct symtab *symtab_from_filename (char **argptr,
103 					    char *p, int is_quote_enclosed,
104 					    int *not_found_ptr);
105 
106 static struct
107 symtabs_and_lines decode_all_digits (char **argptr,
108 				     struct symtab *default_symtab,
109 				     int default_line,
110 				     char ***canonical,
111 				     struct symtab *file_symtab,
112 				     char *q);
113 
114 static struct symtabs_and_lines decode_dollar (char *copy,
115 					       int funfirstline,
116 					       struct symtab *default_symtab,
117 					       char ***canonical,
118 					       struct symtab *file_symtab);
119 
120 static struct symtabs_and_lines decode_variable (char *copy,
121 						 int funfirstline,
122 						 char ***canonical,
123 						 struct symtab *file_symtab,
124 						 int *not_found_ptr);
125 
126 static struct
127 symtabs_and_lines symbol_found (int funfirstline,
128 				char ***canonical,
129 				char *copy,
130 				struct symbol *sym,
131 				struct symtab *file_symtab,
132 				struct symtab *sym_symtab);
133 
134 static struct
135 symtabs_and_lines minsym_found (int funfirstline,
136 				struct minimal_symbol *msymbol);
137 
138 /* Helper functions. */
139 
140 /* Issue a helpful hint on using the command completion feature on
141    single quoted demangled C++ symbols as part of the completion
142    error.  */
143 
144 static NORETURN void
cplusplus_error(const char * name,const char * fmt,...)145 cplusplus_error (const char *name, const char *fmt, ...)
146 {
147   struct ui_file *tmp_stream;
148   tmp_stream = mem_fileopen ();
149   make_cleanup_ui_file_delete (tmp_stream);
150 
151   {
152     va_list args;
153     va_start (args, fmt);
154     vfprintf_unfiltered (tmp_stream, fmt, args);
155     va_end (args);
156   }
157 
158   while (*name == '\'')
159     name++;
160   fprintf_unfiltered (tmp_stream,
161 		      ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
162 		       "(Note leading single quote.)"),
163 		      name, name);
164   error_stream (tmp_stream);
165 }
166 
167 /* Return the number of methods described for TYPE, including the
168    methods from types it derives from. This can't be done in the symbol
169    reader because the type of the baseclass might still be stubbed
170    when the definition of the derived class is parsed.  */
171 
172 static int
total_number_of_methods(struct type * type)173 total_number_of_methods (struct type *type)
174 {
175   int n;
176   int count;
177 
178   CHECK_TYPEDEF (type);
179   if (TYPE_CPLUS_SPECIFIC (type) == NULL)
180     return 0;
181   count = TYPE_NFN_FIELDS_TOTAL (type);
182 
183   for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
184     count += total_number_of_methods (TYPE_BASECLASS (type, n));
185 
186   return count;
187 }
188 
189 /* Recursive helper function for decode_line_1.
190    Look for methods named NAME in type T.
191    Return number of matches.
192    Put matches in SYM_ARR, which should have been allocated with
193    a size of total_number_of_methods (T) * sizeof (struct symbol *).
194    Note that this function is g++ specific.  */
195 
196 static int
find_methods(struct type * t,char * name,struct symbol ** sym_arr)197 find_methods (struct type *t, char *name, struct symbol **sym_arr)
198 {
199   int i1 = 0;
200   int ibase;
201   char *class_name = type_name_no_tag (t);
202 
203   /* Ignore this class if it doesn't have a name.  This is ugly, but
204      unless we figure out how to get the physname without the name of
205      the class, then the loop can't do any good.  */
206   if (class_name
207       && (lookup_symbol (class_name, (struct block *) NULL,
208 			 STRUCT_DOMAIN, (int *) NULL,
209 			 (struct symtab **) NULL)))
210     {
211       int method_counter;
212       int name_len = strlen (name);
213 
214       CHECK_TYPEDEF (t);
215 
216       /* Loop over each method name.  At this level, all overloads of a name
217          are counted as a single name.  There is an inner loop which loops over
218          each overload.  */
219 
220       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
221 	   method_counter >= 0;
222 	   --method_counter)
223 	{
224 	  char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
225 	  char dem_opname[64];
226 
227 	  if (strncmp (method_name, "__", 2) == 0 ||
228 	      strncmp (method_name, "op", 2) == 0 ||
229 	      strncmp (method_name, "type", 4) == 0)
230 	    {
231 	      if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
232 		method_name = dem_opname;
233 	      else if (cplus_demangle_opname (method_name, dem_opname, 0))
234 		method_name = dem_opname;
235 	    }
236 
237 	  if (strcmp_iw (name, method_name) == 0)
238 	    /* Find all the overloaded methods with that name.  */
239 	    i1 += add_matching_methods (method_counter, t,
240 					sym_arr + i1);
241 	  else if (strncmp (class_name, name, name_len) == 0
242 		   && (class_name[name_len] == '\0'
243 		       || class_name[name_len] == '<'))
244 	    i1 += add_constructors (method_counter, t,
245 				    sym_arr + i1);
246 	}
247     }
248 
249   /* Only search baseclasses if there is no match yet, since names in
250      derived classes override those in baseclasses.
251 
252      FIXME: The above is not true; it is only true of member functions
253      if they have the same number of arguments (??? - section 13.1 of the
254      ARM says the function members are not in the same scope but doesn't
255      really spell out the rules in a way I understand.  In any case, if
256      the number of arguments differ this is a case in which we can overload
257      rather than hiding without any problem, and gcc 2.4.5 does overload
258      rather than hiding in this case).  */
259 
260   if (i1 == 0)
261     for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
262       i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
263 
264   return i1;
265 }
266 
267 /* Add the symbols associated to methods of the class whose type is T
268    and whose name matches the method indexed by METHOD_COUNTER in the
269    array SYM_ARR.  Return the number of methods added.  */
270 
271 static int
add_matching_methods(int method_counter,struct type * t,struct symbol ** sym_arr)272 add_matching_methods (int method_counter, struct type *t,
273 		      struct symbol **sym_arr)
274 {
275   int field_counter;
276   int i1 = 0;
277 
278   for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
279        field_counter >= 0;
280        --field_counter)
281     {
282       struct fn_field *f;
283       char *phys_name;
284 
285       f = TYPE_FN_FIELDLIST1 (t, method_counter);
286 
287       if (TYPE_FN_FIELD_STUB (f, field_counter))
288 	{
289 	  char *tmp_name;
290 
291 	  tmp_name = gdb_mangle_name (t,
292 				      method_counter,
293 				      field_counter);
294 	  phys_name = alloca (strlen (tmp_name) + 1);
295 	  strcpy (phys_name, tmp_name);
296 	  xfree (tmp_name);
297 	}
298       else
299 	phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
300 
301       /* Destructor is handled by caller, don't add it to
302 	 the list.  */
303       if (is_destructor_name (phys_name) != 0)
304 	continue;
305 
306       sym_arr[i1] = lookup_symbol (phys_name,
307 				   NULL, VAR_DOMAIN,
308 				   (int *) NULL,
309 				   (struct symtab **) NULL);
310       if (sym_arr[i1])
311 	i1++;
312       else
313 	{
314 	  /* This error message gets printed, but the method
315 	     still seems to be found
316 	     fputs_filtered("(Cannot find method ", gdb_stdout);
317 	     fprintf_symbol_filtered (gdb_stdout, phys_name,
318 	     language_cplus,
319 	     DMGL_PARAMS | DMGL_ANSI);
320 	     fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
321 	  */
322 	}
323     }
324 
325   return i1;
326 }
327 
328 /* Add the symbols associated to constructors of the class whose type
329    is CLASS_TYPE and which are indexed by by METHOD_COUNTER to the
330    array SYM_ARR.  Return the number of methods added.  */
331 
332 static int
add_constructors(int method_counter,struct type * t,struct symbol ** sym_arr)333 add_constructors (int method_counter, struct type *t,
334 		  struct symbol **sym_arr)
335 {
336   int field_counter;
337   int i1 = 0;
338 
339   /* For GCC 3.x and stabs, constructors and destructors
340      have names like __base_ctor and __complete_dtor.
341      Check the physname for now if we're looking for a
342      constructor.  */
343   for (field_counter
344 	 = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
345        field_counter >= 0;
346        --field_counter)
347     {
348       struct fn_field *f;
349       char *phys_name;
350 
351       f = TYPE_FN_FIELDLIST1 (t, method_counter);
352 
353       /* GCC 3.x will never produce stabs stub methods, so
354 	 we don't need to handle this case.  */
355       if (TYPE_FN_FIELD_STUB (f, field_counter))
356 	continue;
357       phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
358       if (! is_constructor_name (phys_name))
359 	continue;
360 
361       /* If this method is actually defined, include it in the
362 	 list.  */
363       sym_arr[i1] = lookup_symbol (phys_name,
364 				   NULL, VAR_DOMAIN,
365 				   (int *) NULL,
366 				   (struct symtab **) NULL);
367       if (sym_arr[i1])
368 	i1++;
369     }
370 
371   return i1;
372 }
373 
374 /* Helper function for decode_line_1.
375    Build a canonical line spec in CANONICAL if it is non-NULL and if
376    the SAL has a symtab.
377    If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
378    If SYMNAME is NULL the line number from SAL is used and the canonical
379    line spec is `filename:linenum'.  */
380 
381 static void
build_canonical_line_spec(struct symtab_and_line * sal,char * symname,char *** canonical)382 build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
383 			   char ***canonical)
384 {
385   char **canonical_arr;
386   char *canonical_name;
387   char *filename;
388   struct symtab *s = sal->symtab;
389 
390   if (s == (struct symtab *) NULL
391       || s->filename == (char *) NULL
392       || canonical == (char ***) NULL)
393     return;
394 
395   canonical_arr = (char **) xmalloc (sizeof (char *));
396   *canonical = canonical_arr;
397 
398   filename = s->filename;
399   if (symname != NULL)
400     {
401       canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
402       sprintf (canonical_name, "%s:%s", filename, symname);
403     }
404   else
405     {
406       canonical_name = xmalloc (strlen (filename) + 30);
407       sprintf (canonical_name, "%s:%d", filename, sal->line);
408     }
409   canonical_arr[0] = canonical_name;
410 }
411 
412 
413 
414 /* Find an instance of the character C in the string S that is outside
415    of all parenthesis pairs, single-quoted strings, and double-quoted
416    strings.  Also, ignore the char within a template name, like a ','
417    within foo<int, int>.  */
418 
419 static char *
find_toplevel_char(char * s,char c)420 find_toplevel_char (char *s, char c)
421 {
422   int quoted = 0;		/* zero if we're not in quotes;
423 				   '"' if we're in a double-quoted string;
424 				   '\'' if we're in a single-quoted string.  */
425   int depth = 0;		/* Number of unclosed parens we've seen.  */
426   char *scan;
427 
428   for (scan = s; *scan; scan++)
429     {
430       if (quoted)
431 	{
432 	  if (*scan == quoted)
433 	    quoted = 0;
434 	  else if (*scan == '\\' && *(scan + 1))
435 	    scan++;
436 	}
437       else if (*scan == c && ! quoted && depth == 0)
438 	return scan;
439       else if (*scan == '"' || *scan == '\'')
440 	quoted = *scan;
441       else if (*scan == '(' || *scan == '<')
442 	depth++;
443       else if ((*scan == ')' || *scan == '>') && depth > 0)
444 	depth--;
445     }
446 
447   return 0;
448 }
449 
450 /* Determines if the gives string corresponds to an Objective-C method
451    representation, such as -[Foo bar:] or +[Foo bar]. Objective-C symbols
452    are allowed to have spaces and parentheses in them.  */
453 
454 static int
is_objc_method_format(const char * s)455 is_objc_method_format (const char *s)
456 {
457   if (s == NULL || *s == '\0')
458     return 0;
459   /* Handle arguments with the format FILENAME:SYMBOL.  */
460   if ((s[0] == ':') && (strchr ("+-", s[1]) != NULL)
461       && (s[2] == '[') && strchr(s, ']'))
462     return 1;
463   /* Handle arguments that are just SYMBOL.  */
464   else if ((strchr ("+-", s[0]) != NULL) && (s[1] == '[') && strchr(s, ']'))
465     return 1;
466   return 0;
467 }
468 
469 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
470    operate on (ask user if necessary).
471    If CANONICAL is non-NULL return a corresponding array of mangled names
472    as canonical line specs there.  */
473 
474 static struct symtabs_and_lines
decode_line_2(struct symbol * sym_arr[],int nelts,int funfirstline,char *** canonical)475 decode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
476 	       char ***canonical)
477 {
478   struct symtabs_and_lines values, return_values;
479   char *args, *arg1;
480   int i;
481   char *prompt;
482   char *symname;
483   struct cleanup *old_chain;
484   char **canonical_arr = (char **) NULL;
485 
486   values.sals = (struct symtab_and_line *)
487     alloca (nelts * sizeof (struct symtab_and_line));
488   return_values.sals = (struct symtab_and_line *)
489     xmalloc (nelts * sizeof (struct symtab_and_line));
490   old_chain = make_cleanup (xfree, return_values.sals);
491 
492   if (canonical)
493     {
494       canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
495       make_cleanup (xfree, canonical_arr);
496       memset (canonical_arr, 0, nelts * sizeof (char *));
497       *canonical = canonical_arr;
498     }
499 
500   i = 0;
501   printf_unfiltered ("[0] cancel\n[1] all\n");
502   while (i < nelts)
503     {
504       init_sal (&return_values.sals[i]);	/* Initialize to zeroes.  */
505       init_sal (&values.sals[i]);
506       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
507 	{
508 	  values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
509 	  if (values.sals[i].symtab)
510 	    printf_unfiltered ("[%d] %s at %s:%d\n",
511 			       (i + 2),
512 			       SYMBOL_PRINT_NAME (sym_arr[i]),
513 			       values.sals[i].symtab->filename,
514 			       values.sals[i].line);
515 	  else
516 	    printf_unfiltered ("[%d] %s at ?FILE:%d [No symtab? Probably broken debug info...]\n",
517 			       (i + 2),
518 			       SYMBOL_PRINT_NAME (sym_arr[i]),
519 			       values.sals[i].line);
520 
521 	}
522       else
523 	printf_unfiltered ("?HERE\n");
524       i++;
525     }
526 
527   prompt = getenv ("PS2");
528   if (prompt == NULL)
529     {
530       prompt = "> ";
531     }
532   args = command_line_input (prompt, 0, "overload-choice");
533 
534   if (args == 0 || *args == 0)
535     error_no_arg ("one or more choice numbers");
536 
537   i = 0;
538   while (*args)
539     {
540       int num;
541 
542       arg1 = args;
543       while (*arg1 >= '0' && *arg1 <= '9')
544 	arg1++;
545       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
546 	error ("Arguments must be choice numbers.");
547 
548       num = atoi (args);
549 
550       if (num == 0)
551 	error ("canceled");
552       else if (num == 1)
553 	{
554 	  if (canonical_arr)
555 	    {
556 	      for (i = 0; i < nelts; i++)
557 		{
558 		  if (canonical_arr[i] == NULL)
559 		    {
560 		      symname = DEPRECATED_SYMBOL_NAME (sym_arr[i]);
561 		      canonical_arr[i] = savestring (symname, strlen (symname));
562 		    }
563 		}
564 	    }
565 	  memcpy (return_values.sals, values.sals,
566 		  (nelts * sizeof (struct symtab_and_line)));
567 	  return_values.nelts = nelts;
568 	  discard_cleanups (old_chain);
569 	  return return_values;
570 	}
571 
572       if (num >= nelts + 2)
573 	{
574 	  printf_unfiltered ("No choice number %d.\n", num);
575 	}
576       else
577 	{
578 	  num -= 2;
579 	  if (values.sals[num].pc)
580 	    {
581 	      if (canonical_arr)
582 		{
583 		  symname = DEPRECATED_SYMBOL_NAME (sym_arr[num]);
584 		  make_cleanup (xfree, symname);
585 		  canonical_arr[i] = savestring (symname, strlen (symname));
586 		}
587 	      return_values.sals[i++] = values.sals[num];
588 	      values.sals[num].pc = 0;
589 	    }
590 	  else
591 	    {
592 	      printf_unfiltered ("duplicate request for %d ignored.\n", num);
593 	    }
594 	}
595 
596       args = arg1;
597       while (*args == ' ' || *args == '\t')
598 	args++;
599     }
600   return_values.nelts = i;
601   discard_cleanups (old_chain);
602   return return_values;
603 }
604 
605 /* The parser of linespec itself. */
606 
607 /* Parse a string that specifies a line number.
608    Pass the address of a char * variable; that variable will be
609    advanced over the characters actually parsed.
610 
611    The string can be:
612 
613    LINENUM -- that line number in current file.  PC returned is 0.
614    FILE:LINENUM -- that line in that file.  PC returned is 0.
615    FUNCTION -- line number of openbrace of that function.
616    PC returned is the start of the function.
617    VARIABLE -- line number of definition of that variable.
618    PC returned is 0.
619    FILE:FUNCTION -- likewise, but prefer functions in that file.
620    *EXPR -- line in which address EXPR appears.
621 
622    This may all be followed by an "if EXPR", which we ignore.
623 
624    FUNCTION may be an undebuggable function found in minimal symbol table.
625 
626    If the argument FUNFIRSTLINE is nonzero, we want the first line
627    of real code inside a function when a function is specified, and it is
628    not OK to specify a variable or type to get its line number.
629 
630    DEFAULT_SYMTAB specifies the file to use if none is specified.
631    It defaults to current_source_symtab.
632    DEFAULT_LINE specifies the line number to use for relative
633    line numbers (that start with signs).  Defaults to current_source_line.
634    If CANONICAL is non-NULL, store an array of strings containing the canonical
635    line specs there if necessary. Currently overloaded member functions and
636    line numbers or static functions without a filename yield a canonical
637    line spec. The array and the line spec strings are allocated on the heap,
638    it is the callers responsibility to free them.
639 
640    Note that it is possible to return zero for the symtab
641    if no file is validly specified.  Callers must check that.
642    Also, the line number returned may be invalid.
643 
644    If NOT_FOUND_PTR is not null, store a boolean true/false value at the location, based
645    on whether or not failure occurs due to an unknown function or file.  In the case
646    where failure does occur due to an unknown function or file, do not issue an error
647    message.  */
648 
649 /* We allow single quotes in various places.  This is a hideous
650    kludge, which exists because the completer can't yet deal with the
651    lack of single quotes.  FIXME: write a linespec_completer which we
652    can use as appropriate instead of make_symbol_completion_list.  */
653 
654 struct symtabs_and_lines
decode_line_1(char ** argptr,int funfirstline,struct symtab * default_symtab,int default_line,char *** canonical,int * not_found_ptr)655 decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
656 	       int default_line, char ***canonical, int *not_found_ptr)
657 {
658   char *p;
659   char *q;
660   /* If a file name is specified, this is its symtab.  */
661   struct symtab *file_symtab = NULL;
662 
663   char *copy;
664   /* This is NULL if there are no parens in *ARGPTR, or a pointer to
665      the closing parenthesis if there are parens.  */
666   char *paren_pointer;
667   /* This says whether or not something in *ARGPTR is quoted with
668      completer_quotes (i.e. with single quotes).  */
669   int is_quoted;
670   /* Is part of *ARGPTR is enclosed in double quotes?  */
671   int is_quote_enclosed;
672   int is_objc_method = 0;
673   char *saved_arg = *argptr;
674 
675   if (not_found_ptr)
676     *not_found_ptr = 0;
677 
678   /* Defaults have defaults.  */
679 
680   initialize_defaults (&default_symtab, &default_line);
681 
682   /* See if arg is *PC.  */
683 
684   if (**argptr == '*')
685     return decode_indirect (argptr);
686 
687   /* Set various flags.  'paren_pointer' is important for overload
688      checking, where we allow things like:
689         (gdb) break c::f(int)
690   */
691 
692   set_flags (*argptr, &is_quoted, &paren_pointer);
693 
694   /* Check to see if it's a multipart linespec (with colons or
695      periods).  */
696 
697   /* Locate the end of the first half of the linespec.
698      After the call, for instance, if the argptr string is "foo.c:123"
699      p will point at "123".  If there is only one part, like "foo", p
700      will point to "". If this is a C++ name, like "A::B::foo", p will
701      point to "::B::foo". Argptr is not changed by this call.  */
702 
703   p = locate_first_half (argptr, &is_quote_enclosed);
704 
705   /* Check if this is an Objective-C method (anything that starts with
706      a '+' or '-' and a '[').  */
707   if (is_objc_method_format (p))
708     {
709       is_objc_method = 1;
710       paren_pointer  = NULL; /* Just a category name.  Ignore it.  */
711     }
712 
713   /* Check if the symbol could be an Objective-C selector.  */
714 
715   {
716     struct symtabs_and_lines values;
717     values = decode_objc (argptr, funfirstline, NULL,
718 			  canonical, saved_arg);
719     if (values.sals != NULL)
720       return values;
721   }
722 
723   /* Does it look like there actually were two parts?  */
724 
725   if ((p[0] == ':' || p[0] == '.') && paren_pointer == NULL)
726     {
727       if (is_quoted)
728 	*argptr = *argptr + 1;
729 
730       /* Is it a C++ or Java compound data structure?
731 	 The check on p[1] == ':' is capturing the case of "::",
732 	 since p[0]==':' was checked above.
733 	 Note that the call to decode_compound does everything
734 	 for us, including the lookup on the symbol table, so we
735 	 can return now. */
736 
737       if (p[0] == '.' || p[1] == ':')
738 	return decode_compound (argptr, funfirstline, canonical,
739 				saved_arg, p);
740 
741       /* No, the first part is a filename; set s to be that file's
742 	 symtab.  Also, move argptr past the filename.  */
743 
744       file_symtab = symtab_from_filename (argptr, p, is_quote_enclosed,
745 		      			  not_found_ptr);
746     }
747 #if 0
748   /* No one really seems to know why this was added. It certainly
749      breaks the command line, though, whenever the passed
750      name is of the form ClassName::Method. This bit of code
751      singles out the class name, and if funfirstline is set (for
752      example, you are setting a breakpoint at this function),
753      you get an error. This did not occur with earlier
754      verions, so I am ifdef'ing this out. 3/29/99 */
755   else
756     {
757       /* Check if what we have till now is a symbol name */
758 
759       /* We may be looking at a template instantiation such
760          as "foo<int>".  Check here whether we know about it,
761          instead of falling through to the code below which
762          handles ordinary function names, because that code
763          doesn't like seeing '<' and '>' in a name -- the
764          skip_quoted call doesn't go past them.  So see if we
765          can figure it out right now. */
766 
767       copy = (char *) alloca (p - *argptr + 1);
768       memcpy (copy, *argptr, p - *argptr);
769       copy[p - *argptr] = '\000';
770       sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
771       if (sym)
772 	{
773 	  *argptr = (*p == '\'') ? p + 1 : p;
774 	  return symbol_found (funfirstline, canonical, copy, sym,
775 			       NULL, sym_symtab);
776 	}
777       /* Otherwise fall out from here and go to file/line spec
778          processing, etc. */
779     }
780 #endif
781 
782   /* S is specified file's symtab, or 0 if no file specified.
783      arg no longer contains the file name.  */
784 
785   /* Check whether arg is all digits (and sign).  */
786 
787   q = *argptr;
788   if (*q == '-' || *q == '+')
789     q++;
790   while (*q >= '0' && *q <= '9')
791     q++;
792 
793   if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
794     /* We found a token consisting of all digits -- at least one digit.  */
795     return decode_all_digits (argptr, default_symtab, default_line,
796 			      canonical, file_symtab, q);
797 
798   /* Arg token is not digits => try it as a variable name
799      Find the next token (everything up to end or next whitespace).  */
800 
801   if (**argptr == '$')		/* May be a convenience variable.  */
802     /* One or two $ chars possible.  */
803     p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));
804   else if (is_quoted)
805     {
806       p = skip_quoted (*argptr);
807       if (p[-1] != '\'')
808 	error ("Unmatched single quote.");
809     }
810   else if (is_objc_method)
811     {
812       /* allow word separators in method names for Obj-C */
813       p = skip_quoted_chars (*argptr, NULL, "");
814     }
815   else if (paren_pointer != NULL)
816     {
817       p = paren_pointer + 1;
818     }
819   else
820     {
821       p = skip_quoted (*argptr);
822     }
823 
824   copy = (char *) alloca (p - *argptr + 1);
825   memcpy (copy, *argptr, p - *argptr);
826   copy[p - *argptr] = '\0';
827   if (p != *argptr
828       && copy[0]
829       && copy[0] == copy[p - *argptr - 1]
830       && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
831     {
832       copy[p - *argptr - 1] = '\0';
833       copy++;
834     }
835   while (*p == ' ' || *p == '\t')
836     p++;
837   *argptr = p;
838 
839   /* If it starts with $: may be a legitimate variable or routine name
840      (e.g. HP-UX millicode routines such as $$dyncall), or it may
841      be history value, or it may be a convenience variable.  */
842 
843   if (*copy == '$')
844     return decode_dollar (copy, funfirstline, default_symtab,
845 			  canonical, file_symtab);
846 
847   /* Look up that token as a variable.
848      If file specified, use that file's per-file block to start with.  */
849 
850   return decode_variable (copy, funfirstline, canonical,
851 			  file_symtab, not_found_ptr);
852 }
853 
854 
855 
856 /* Now, more helper functions for decode_line_1.  Some conventions
857    that these functions follow:
858 
859    Decode_line_1 typically passes along some of its arguments or local
860    variables to the subfunctions.  It passes the variables by
861    reference if they are modified by the subfunction, and by value
862    otherwise.
863 
864    Some of the functions have side effects that don't arise from
865    variables that are passed by reference.  In particular, if a
866    function is passed ARGPTR as an argument, it modifies what ARGPTR
867    points to; typically, it advances *ARGPTR past whatever substring
868    it has just looked at.  (If it doesn't modify *ARGPTR, then the
869    function gets passed *ARGPTR instead, which is then called ARG: see
870    set_flags, for example.)  Also, functions that return a struct
871    symtabs_and_lines may modify CANONICAL, as in the description of
872    decode_line_1.
873 
874    If a function returns a struct symtabs_and_lines, then that struct
875    will immediately make its way up the call chain to be returned by
876    decode_line_1.  In particular, all of the functions decode_XXX
877    calculate the appropriate struct symtabs_and_lines, under the
878    assumption that their argument is of the form XXX.  */
879 
880 /* First, some functions to initialize stuff at the beggining of the
881    function.  */
882 
883 static void
initialize_defaults(struct symtab ** default_symtab,int * default_line)884 initialize_defaults (struct symtab **default_symtab, int *default_line)
885 {
886   if (*default_symtab == 0)
887     {
888       /* Use whatever we have for the default source line.  We don't use
889          get_current_or_default_symtab_and_line as it can recurse and call
890 	 us back! */
891       struct symtab_and_line cursal =
892 	get_current_source_symtab_and_line ();
893 
894       *default_symtab = cursal.symtab;
895       *default_line = cursal.line;
896     }
897 }
898 
899 static void
set_flags(char * arg,int * is_quoted,char ** paren_pointer)900 set_flags (char *arg, int *is_quoted, char **paren_pointer)
901 {
902   char *ii;
903   int has_if = 0;
904 
905   /* 'has_if' is for the syntax:
906         (gdb) break foo if (a==b)
907   */
908   if ((ii = strstr (arg, " if ")) != NULL ||
909       (ii = strstr (arg, "\tif ")) != NULL ||
910       (ii = strstr (arg, " if\t")) != NULL ||
911       (ii = strstr (arg, "\tif\t")) != NULL ||
912       (ii = strstr (arg, " if(")) != NULL ||
913       (ii = strstr (arg, "\tif( ")) != NULL)
914     has_if = 1;
915   /* Temporarily zap out "if (condition)" to not confuse the
916      parenthesis-checking code below.  This is undone below. Do not
917      change ii!!  */
918   if (has_if)
919     {
920       *ii = '\0';
921     }
922 
923   *is_quoted = (*arg
924 		&& strchr (get_gdb_completer_quote_characters (),
925 			   *arg) != NULL);
926 
927   *paren_pointer = strchr (arg, '(');
928   if (*paren_pointer != NULL)
929     *paren_pointer = strrchr (*paren_pointer, ')');
930 
931   /* Now that we're safely past the paren_pointer check, put back " if
932      (condition)" so outer layers can see it.  */
933   if (has_if)
934     *ii = ' ';
935 }
936 
937 
938 
939 /* Decode arg of the form *PC.  */
940 
941 static struct symtabs_and_lines
decode_indirect(char ** argptr)942 decode_indirect (char **argptr)
943 {
944   struct symtabs_and_lines values;
945   CORE_ADDR pc;
946 
947   (*argptr)++;
948   pc = parse_and_eval_address_1 (argptr);
949 
950   values.sals = (struct symtab_and_line *)
951     xmalloc (sizeof (struct symtab_and_line));
952 
953   values.nelts = 1;
954   values.sals[0] = find_pc_line (pc, 0);
955   values.sals[0].pc = pc;
956   values.sals[0].section = find_pc_overlay (pc);
957 
958   return values;
959 }
960 
961 
962 
963 /* Locate the first half of the linespec, ending in a colon, period,
964    or whitespace.  (More or less.)  Also, check to see if *ARGPTR is
965    enclosed in double quotes; if so, set is_quote_enclosed, advance
966    ARGPTR past that and zero out the trailing double quote.
967    If ARGPTR is just a simple name like "main", p will point to ""
968    at the end.  */
969 
970 static char *
locate_first_half(char ** argptr,int * is_quote_enclosed)971 locate_first_half (char **argptr, int *is_quote_enclosed)
972 {
973   char *ii;
974   char *p, *p1;
975   int has_comma;
976 
977   /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
978      and we must isolate the first half.  Outer layers will call again later
979      for the second half.
980 
981      Don't count commas that appear in argument lists of overloaded
982      functions, or in quoted strings.  It's stupid to go to this much
983      trouble when the rest of the function is such an obvious roach hotel.  */
984   ii = find_toplevel_char (*argptr, ',');
985   has_comma = (ii != 0);
986 
987   /* Temporarily zap out second half to not confuse the code below.
988      This is undone below. Do not change ii!!  */
989   if (has_comma)
990     {
991       *ii = '\0';
992     }
993 
994   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION.  May also be
995      CLASS::MEMBER, or NAMESPACE::NAME.  Look for ':', but ignore
996      inside of <>.  */
997 
998   p = *argptr;
999   if (p[0] == '"')
1000     {
1001       *is_quote_enclosed = 1;
1002       (*argptr)++;
1003       p++;
1004     }
1005   else
1006     *is_quote_enclosed = 0;
1007   for (; *p; p++)
1008     {
1009       if (p[0] == '<')
1010 	{
1011 	  char *temp_end = find_template_name_end (p);
1012 	  if (!temp_end)
1013 	    error ("malformed template specification in command");
1014 	  p = temp_end;
1015 	}
1016       /* Check for a colon and a plus or minus and a [ (which
1017          indicates an Objective-C method) */
1018       if (is_objc_method_format (p))
1019 	{
1020 	  break;
1021 	}
1022       /* Check for the end of the first half of the linespec.  End of
1023          line, a tab, a double colon or the last single colon, or a
1024          space.  But if enclosed in double quotes we do not break on
1025          enclosed spaces.  */
1026       if (!*p
1027 	  || p[0] == '\t'
1028 	  || ((p[0] == ':')
1029 	      && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
1030 	  || ((p[0] == ' ') && !*is_quote_enclosed))
1031 	break;
1032       if (p[0] == '.' && strchr (p, ':') == NULL)
1033 	{
1034 	  /* Java qualified method.  Find the *last* '.', since the
1035 	     others are package qualifiers.  */
1036 	  for (p1 = p; *p1; p1++)
1037 	    {
1038 	      if (*p1 == '.')
1039 		p = p1;
1040 	    }
1041 	  break;
1042 	}
1043     }
1044   while (p[0] == ' ' || p[0] == '\t')
1045     p++;
1046 
1047   /* If the closing double quote was left at the end, remove it.  */
1048   if (*is_quote_enclosed)
1049     {
1050       char *closing_quote = strchr (p - 1, '"');
1051       if (closing_quote && closing_quote[1] == '\0')
1052 	*closing_quote = '\0';
1053     }
1054 
1055   /* Now that we've safely parsed the first half, put back ',' so
1056      outer layers can see it.  */
1057   if (has_comma)
1058     *ii = ',';
1059 
1060   return p;
1061 }
1062 
1063 
1064 
1065 /* Here's where we recognise an Objective-C Selector.  An Objective C
1066    selector may be implemented by more than one class, therefore it
1067    may represent more than one method/function.  This gives us a
1068    situation somewhat analogous to C++ overloading.  If there's more
1069    than one method that could represent the selector, then use some of
1070    the existing C++ code to let the user choose one.  */
1071 
1072 struct symtabs_and_lines
decode_objc(char ** argptr,int funfirstline,struct symtab * file_symtab,char *** canonical,char * saved_arg)1073 decode_objc (char **argptr, int funfirstline, struct symtab *file_symtab,
1074 	     char ***canonical, char *saved_arg)
1075 {
1076   struct symtabs_and_lines values;
1077   struct symbol **sym_arr = NULL;
1078   struct symbol *sym = NULL;
1079   char *copy = NULL;
1080   struct block *block = NULL;
1081   int i1 = 0;
1082   int i2 = 0;
1083 
1084   values.sals = NULL;
1085   values.nelts = 0;
1086 
1087   if (file_symtab != NULL)
1088     block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab), STATIC_BLOCK);
1089   else
1090     block = get_selected_block (0);
1091 
1092   copy = find_imps (file_symtab, block, *argptr, NULL, &i1, &i2);
1093 
1094   if (i1 > 0)
1095     {
1096       sym_arr = (struct symbol **) alloca ((i1 + 1) * sizeof (struct symbol *));
1097       sym_arr[i1] = 0;
1098 
1099       copy = find_imps (file_symtab, block, *argptr, sym_arr, &i1, &i2);
1100       *argptr = copy;
1101     }
1102 
1103   /* i1 now represents the TOTAL number of matches found.
1104      i2 represents how many HIGH-LEVEL (struct symbol) matches,
1105      which will come first in the sym_arr array.  Any low-level
1106      (minimal_symbol) matches will follow those.  */
1107 
1108   if (i1 == 1)
1109     {
1110       if (i2 > 0)
1111 	{
1112 	  /* Already a struct symbol.  */
1113 	  sym = sym_arr[0];
1114 	}
1115       else
1116 	{
1117 	  sym = find_pc_function (SYMBOL_VALUE_ADDRESS (sym_arr[0]));
1118 	  if ((sym != NULL) && strcmp (SYMBOL_LINKAGE_NAME (sym_arr[0]), SYMBOL_LINKAGE_NAME (sym)) != 0)
1119 	    {
1120 	      warning ("debugging symbol \"%s\" does not match selector; ignoring", SYMBOL_LINKAGE_NAME (sym));
1121 	      sym = NULL;
1122 	    }
1123 	}
1124 
1125       values.sals = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
1126       values.nelts = 1;
1127 
1128       if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1129 	{
1130 	  /* Canonicalize this, so it remains resolved for dylib loads.  */
1131 	  values.sals[0] = find_function_start_sal (sym, funfirstline);
1132 	  build_canonical_line_spec (values.sals, SYMBOL_NATURAL_NAME (sym), canonical);
1133 	}
1134       else
1135 	{
1136 	  /* The only match was a non-debuggable symbol.  */
1137 	  values.sals[0].symtab = 0;
1138 	  values.sals[0].line = 0;
1139 	  values.sals[0].end = 0;
1140 	  values.sals[0].pc = SYMBOL_VALUE_ADDRESS (sym_arr[0]);
1141 	}
1142       return values;
1143     }
1144 
1145   if (i1 > 1)
1146     {
1147       /* More than one match. The user must choose one or more.  */
1148       return decode_line_2 (sym_arr, i2, funfirstline, canonical);
1149     }
1150 
1151   return values;
1152 }
1153 
1154 /* This handles C++ and Java compound data structures.  P should point
1155    at the first component separator, i.e. double-colon or period.  As
1156    an example, on entrance to this function we could have ARGPTR
1157    pointing to "AAA::inA::fun" and P pointing to "::inA::fun".  */
1158 
1159 static struct symtabs_and_lines
decode_compound(char ** argptr,int funfirstline,char *** canonical,char * saved_arg,char * p)1160 decode_compound (char **argptr, int funfirstline, char ***canonical,
1161 		 char *saved_arg, char *p)
1162 {
1163   struct symtabs_and_lines values;
1164   char *p2;
1165   char *saved_arg2 = *argptr;
1166   char *temp_end;
1167   struct symbol *sym;
1168   /* The symtab that SYM was found in.  */
1169   struct symtab *sym_symtab;
1170   char *copy;
1171   struct symbol *sym_class;
1172   struct symbol **sym_arr;
1173   struct type *t;
1174 
1175   /* First check for "global" namespace specification, of the form
1176      "::foo".  If found, skip over the colons and jump to normal
1177      symbol processing.  I.e. the whole line specification starts with
1178      "::" (note the condition that *argptr == p). */
1179   if (p[0] == ':'
1180       && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
1181     saved_arg2 += 2;
1182 
1183   /* Given our example "AAA::inA::fun", we have two cases to consider:
1184 
1185      1) AAA::inA is the name of a class.  In that case, presumably it
1186         has a method called "fun"; we then look up that method using
1187         find_method.
1188 
1189      2) AAA::inA isn't the name of a class.  In that case, either the
1190         user made a typo or AAA::inA is the name of a namespace.
1191         Either way, we just look up AAA::inA::fun with lookup_symbol.
1192 
1193      Thus, our first task is to find everything before the last set of
1194      double-colons and figure out if it's the name of a class.  So we
1195      first loop through all of the double-colons.  */
1196 
1197   p2 = p;		/* Save for restart.  */
1198 
1199   /* This is very messy. Following the example above we have now the
1200      following pointers:
1201      p -> "::inA::fun"
1202      argptr -> "AAA::inA::fun
1203      saved_arg -> "AAA::inA::fun
1204      saved_arg2 -> "AAA::inA::fun
1205      p2 -> "::inA::fun". */
1206 
1207   /* In the loop below, with these strings, we'll make 2 passes, each
1208      is marked in comments.*/
1209 
1210   while (1)
1211     {
1212       /* Move pointer up to next possible class/namespace token.  */
1213 
1214       p = p2 + 1;	/* Restart with old value +1.  */
1215 
1216       /* PASS1: at this point p2->"::inA::fun", so p->":inA::fun",
1217 	 i.e. if there is a double-colon, p will now point to the
1218 	 second colon. */
1219       /* PASS2: p2->"::fun", p->":fun" */
1220 
1221       /* Move pointer ahead to next double-colon.  */
1222       while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
1223 	{
1224 	  if (p[0] == '<')
1225 	    {
1226 	      temp_end = find_template_name_end (p);
1227 	      if (!temp_end)
1228 		error ("malformed template specification in command");
1229 	      p = temp_end;
1230 	    }
1231 	  /* Note that, since, at the start of this loop, p would be
1232 	     pointing to the second colon in a double-colon, we only
1233 	     satisfy the condition below if there is another
1234 	     double-colon to the right (after). I.e. there is another
1235 	     component that can be a class or a namespace. I.e, if at
1236 	     the beginning of this loop (PASS1), we had
1237 	     p->":inA::fun", we'll trigger this when p has been
1238 	     advanced to point to "::fun".  */
1239 	  /* PASS2: we will not trigger this. */
1240 	  else if ((p[0] == ':') && (p[1] == ':'))
1241 	    break;	/* Found double-colon.  */
1242 	  else
1243 	    /* PASS2: We'll keep getting here, until p->"", at which point
1244 	       we exit this loop.  */
1245 	    p++;
1246 	}
1247 
1248       if (*p != ':')
1249 	break;		/* Out of the while (1).  This would happen
1250 			   for instance if we have looked up
1251 			   unsuccessfully all the components of the
1252 			   string, and p->""(PASS2)  */
1253 
1254       /* We get here if p points to ' ', '\t', '\'', "::" or ""(i.e
1255 	 string ended). */
1256       /* Save restart for next time around.  */
1257       p2 = p;
1258       /* Restore argptr as it was on entry to this function.  */
1259       *argptr = saved_arg2;
1260       /* PASS1: at this point p->"::fun" argptr->"AAA::inA::fun",
1261 	 p2->"::fun".  */
1262 
1263       /* All ready for next pass through the loop.  */
1264     }			/* while (1) */
1265 
1266 
1267   /* Start of lookup in the symbol tables. */
1268 
1269   /* Lookup in the symbol table the substring between argptr and
1270      p. Note, this call changes the value of argptr.  */
1271   /* Before the call, argptr->"AAA::inA::fun",
1272      p->"", p2->"::fun".  After the call: argptr->"fun", p, p2
1273      unchanged.  */
1274   sym_class = lookup_prefix_sym (argptr, p2);
1275 
1276   /* If sym_class has been found, and if "AAA::inA" is a class, then
1277      we're in case 1 above.  So we look up "fun" as a method of that
1278      class.  */
1279   if (sym_class &&
1280       (t = check_typedef (SYMBOL_TYPE (sym_class)),
1281        (TYPE_CODE (t) == TYPE_CODE_STRUCT
1282 	|| TYPE_CODE (t) == TYPE_CODE_UNION)))
1283     {
1284       /* Arg token is not digits => try it as a function name.
1285 	 Find the next token (everything up to end or next
1286 	 blank).  */
1287       if (**argptr
1288 	  && strchr (get_gdb_completer_quote_characters (),
1289 		     **argptr) != NULL)
1290 	{
1291 	  p = skip_quoted (*argptr);
1292 	  *argptr = *argptr + 1;
1293 	}
1294       else
1295 	{
1296 	  /* At this point argptr->"fun".  */
1297 	  p = *argptr;
1298 	  while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
1299 	    p++;
1300 	  /* At this point p->"".  String ended.  */
1301 	}
1302 
1303       /* Allocate our own copy of the substring between argptr and
1304 	 p. */
1305       copy = (char *) alloca (p - *argptr + 1);
1306       memcpy (copy, *argptr, p - *argptr);
1307       copy[p - *argptr] = '\0';
1308       if (p != *argptr
1309 	  && copy[p - *argptr - 1]
1310 	  && strchr (get_gdb_completer_quote_characters (),
1311 		     copy[p - *argptr - 1]) != NULL)
1312 	copy[p - *argptr - 1] = '\0';
1313 
1314       /* At this point copy->"fun", p->"" */
1315 
1316       /* No line number may be specified.  */
1317       while (*p == ' ' || *p == '\t')
1318 	p++;
1319       *argptr = p;
1320       /* At this point arptr->"".  */
1321 
1322       /* Look for copy as a method of sym_class. */
1323       /* At this point copy->"fun", sym_class is "AAA:inA",
1324 	 saved_arg->"AAA::inA::fun".  This concludes the scanning of
1325 	 the string for possible components matches.  If we find it
1326 	 here, we return. If not, and we are at the and of the string,
1327 	 we'll lookup the whole string in the symbol tables.  */
1328 
1329       return find_method (funfirstline, canonical, saved_arg,
1330 			  copy, t, sym_class);
1331 
1332     } /* End if symbol found */
1333 
1334 
1335   /* We couldn't find a class, so we're in case 2 above.  We check the
1336      entire name as a symbol instead.  */
1337 
1338   copy = (char *) alloca (p - saved_arg2 + 1);
1339   memcpy (copy, saved_arg2, p - saved_arg2);
1340   /* Note: if is_quoted should be true, we snuff out quote here
1341      anyway.  */
1342   copy[p - saved_arg2] = '\000';
1343   /* Set argptr to skip over the name.  */
1344   *argptr = (*p == '\'') ? p + 1 : p;
1345 
1346   /* Look up entire name */
1347   sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
1348   if (sym)
1349     return symbol_found (funfirstline, canonical, copy, sym,
1350 			 NULL, sym_symtab);
1351 
1352   /* Couldn't find any interpretation as classes/namespaces, so give
1353      up.  The quotes are important if copy is empty.  */
1354   cplusplus_error (saved_arg,
1355 		   "Can't find member of namespace, class, struct, or union named \"%s\"\n",
1356 		   copy);
1357 }
1358 
1359 /* Next come some helper functions for decode_compound.  */
1360 
1361 /* Return the symbol corresponding to the substring of *ARGPTR ending
1362    at P, allowing whitespace.  Also, advance *ARGPTR past the symbol
1363    name in question, the compound object separator ("::" or "."), and
1364    whitespace.  Note that *ARGPTR is changed whether or not the
1365    lookup_symbol call finds anything (i.e we return NULL).  As an
1366    example, say ARGPTR is "AAA::inA::fun" and P is "::inA::fun".  */
1367 
1368 static struct symbol *
lookup_prefix_sym(char ** argptr,char * p)1369 lookup_prefix_sym (char **argptr, char *p)
1370 {
1371   char *p1;
1372   char *copy;
1373 
1374   /* Extract the class name.  */
1375   p1 = p;
1376   while (p != *argptr && p[-1] == ' ')
1377     --p;
1378   copy = (char *) alloca (p - *argptr + 1);
1379   memcpy (copy, *argptr, p - *argptr);
1380   copy[p - *argptr] = 0;
1381 
1382   /* Discard the class name from the argptr.  */
1383   p = p1 + (p1[0] == ':' ? 2 : 1);
1384   while (*p == ' ' || *p == '\t')
1385     p++;
1386   *argptr = p;
1387 
1388   /* At this point p1->"::inA::fun", p->"inA::fun" copy->"AAA",
1389      argptr->"inA::fun" */
1390 
1391   return lookup_symbol (copy, 0, STRUCT_DOMAIN, 0,
1392 			(struct symtab **) NULL);
1393 }
1394 
1395 /* This finds the method COPY in the class whose type is T and whose
1396    symbol is SYM_CLASS.  */
1397 
1398 static struct symtabs_and_lines
find_method(int funfirstline,char *** canonical,char * saved_arg,char * copy,struct type * t,struct symbol * sym_class)1399 find_method (int funfirstline, char ***canonical, char *saved_arg,
1400 	     char *copy, struct type *t, struct symbol *sym_class)
1401 {
1402   struct symtabs_and_lines values;
1403   struct symbol *sym = 0;
1404   int i1;	/*  Counter for the symbol array.  */
1405   struct symbol **sym_arr =  alloca (total_number_of_methods (t)
1406 				     * sizeof (struct symbol *));
1407 
1408   /* Find all methods with a matching name, and put them in
1409      sym_arr.  */
1410 
1411   i1 = collect_methods (copy, t, sym_arr);
1412 
1413   if (i1 == 1)
1414     {
1415       /* There is exactly one field with that name.  */
1416       sym = sym_arr[0];
1417 
1418       if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1419 	{
1420 	  values.sals = (struct symtab_and_line *)
1421 	    xmalloc (sizeof (struct symtab_and_line));
1422 	  values.nelts = 1;
1423 	  values.sals[0] = find_function_start_sal (sym,
1424 						    funfirstline);
1425 	}
1426       else
1427 	{
1428 	  values.nelts = 0;
1429 	}
1430       return values;
1431     }
1432   if (i1 > 0)
1433     {
1434       /* There is more than one field with that name
1435 	 (overloaded).  Ask the user which one to use.  */
1436       return decode_line_2 (sym_arr, i1, funfirstline, canonical);
1437     }
1438   else
1439     {
1440       char *tmp;
1441 
1442       if (is_operator_name (copy))
1443 	{
1444 	  tmp = (char *) alloca (strlen (copy + 3) + 9);
1445 	  strcpy (tmp, "operator ");
1446 	  strcat (tmp, copy + 3);
1447 	}
1448       else
1449 	tmp = copy;
1450       if (tmp[0] == '~')
1451 	cplusplus_error (saved_arg,
1452 			 "the class `%s' does not have destructor defined\n",
1453 			 SYMBOL_PRINT_NAME (sym_class));
1454       else
1455 	cplusplus_error (saved_arg,
1456 			 "the class %s does not have any method named %s\n",
1457 			 SYMBOL_PRINT_NAME (sym_class), tmp);
1458     }
1459 }
1460 
1461 /* Find all methods named COPY in the class whose type is T, and put
1462    them in SYM_ARR.  Return the number of methods found.  */
1463 
1464 static int
collect_methods(char * copy,struct type * t,struct symbol ** sym_arr)1465 collect_methods (char *copy, struct type *t,
1466 		 struct symbol **sym_arr)
1467 {
1468   int i1 = 0;	/*  Counter for the symbol array.  */
1469 
1470   if (destructor_name_p (copy, t))
1471     {
1472       /* Destructors are a special case.  */
1473       int m_index, f_index;
1474 
1475       if (get_destructor_fn_field (t, &m_index, &f_index))
1476 	{
1477 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
1478 
1479 	  sym_arr[i1] =
1480 	    lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
1481 			   NULL, VAR_DOMAIN, (int *) NULL,
1482 			   (struct symtab **) NULL);
1483 	  if (sym_arr[i1])
1484 	    i1++;
1485 	}
1486     }
1487   else
1488     i1 = find_methods (t, copy, sym_arr);
1489 
1490   return i1;
1491 }
1492 
1493 
1494 
1495 /* Return the symtab associated to the filename given by the substring
1496    of *ARGPTR ending at P, and advance ARGPTR past that filename.  If
1497    NOT_FOUND_PTR is not null and the source file is not found, store
1498    boolean true at the location pointed to and do not issue an
1499    error message.  */
1500 
1501 static struct symtab *
symtab_from_filename(char ** argptr,char * p,int is_quote_enclosed,int * not_found_ptr)1502 symtab_from_filename (char **argptr, char *p, int is_quote_enclosed,
1503 		      int *not_found_ptr)
1504 {
1505   char *p1;
1506   char *copy;
1507   struct symtab *file_symtab;
1508 
1509   p1 = p;
1510   while (p != *argptr && p[-1] == ' ')
1511     --p;
1512   if ((*p == '"') && is_quote_enclosed)
1513     --p;
1514   copy = (char *) alloca (p - *argptr + 1);
1515   memcpy (copy, *argptr, p - *argptr);
1516   /* It may have the ending quote right after the file name.  */
1517   if (is_quote_enclosed && copy[p - *argptr - 1] == '"')
1518     copy[p - *argptr - 1] = 0;
1519   else
1520     copy[p - *argptr] = 0;
1521 
1522   /* Find that file's data.  */
1523   file_symtab = lookup_symtab (copy);
1524   if (file_symtab == 0)
1525     {
1526       if (!have_full_symbols () && !have_partial_symbols ())
1527 	error ("No symbol table is loaded.  Use the \"file\" command.");
1528       if (not_found_ptr)
1529 	{
1530 	  *not_found_ptr = 1;
1531 	  /* The caller has indicated that it wishes quiet notification of any
1532 	     error where the function or file is not found.  A call to
1533 	     error_silent causes an error to occur, but it does not issue
1534 	     the supplied message.  The message can be manually output by
1535 	     the caller, if desired.  This is used, for example, when
1536 	     attempting to set breakpoints for functions in shared libraries
1537 	     that have not yet been loaded.  */
1538 	  error_silent ("No source file named %s.", copy);
1539 	}
1540       error ("No source file named %s.", copy);
1541     }
1542 
1543   /* Discard the file name from the arg.  */
1544   p = p1 + 1;
1545   while (*p == ' ' || *p == '\t')
1546     p++;
1547   *argptr = p;
1548 
1549   return file_symtab;
1550 }
1551 
1552 
1553 
1554 /* This decodes a line where the argument is all digits (possibly
1555    preceded by a sign).  Q should point to the end of those digits;
1556    the other arguments are as usual.  */
1557 
1558 static struct symtabs_and_lines
decode_all_digits(char ** argptr,struct symtab * default_symtab,int default_line,char *** canonical,struct symtab * file_symtab,char * q)1559 decode_all_digits (char **argptr, struct symtab *default_symtab,
1560 		   int default_line, char ***canonical,
1561 		   struct symtab *file_symtab, char *q)
1562 
1563 {
1564   struct symtabs_and_lines values;
1565   struct symtab_and_line val;
1566 
1567   enum sign
1568     {
1569       none, plus, minus
1570     }
1571   sign = none;
1572 
1573   /* We might need a canonical line spec if no file was specified.  */
1574   int need_canonical = (file_symtab == 0) ? 1 : 0;
1575 
1576   init_sal (&val);
1577 
1578   /* This is where we need to make sure that we have good defaults.
1579      We must guarantee that this section of code is never executed
1580      when we are called with just a function name, since
1581      set_default_source_symtab_and_line uses
1582      select_source_symtab that calls us with such an argument.  */
1583 
1584   if (file_symtab == 0 && default_symtab == 0)
1585     {
1586       /* Make sure we have at least a default source file.  */
1587       set_default_source_symtab_and_line ();
1588       initialize_defaults (&default_symtab, &default_line);
1589     }
1590 
1591   if (**argptr == '+')
1592     sign = plus, (*argptr)++;
1593   else if (**argptr == '-')
1594     sign = minus, (*argptr)++;
1595   val.line = atoi (*argptr);
1596   switch (sign)
1597     {
1598     case plus:
1599       if (q == *argptr)
1600 	val.line = 5;
1601       if (file_symtab == 0)
1602 	val.line = default_line + val.line;
1603       break;
1604     case minus:
1605       if (q == *argptr)
1606 	val.line = 15;
1607       if (file_symtab == 0)
1608 	val.line = default_line - val.line;
1609       else
1610 	val.line = 1;
1611       break;
1612     case none:
1613       break;		/* No need to adjust val.line.  */
1614     }
1615 
1616   while (*q == ' ' || *q == '\t')
1617     q++;
1618   *argptr = q;
1619   if (file_symtab == 0)
1620     file_symtab = default_symtab;
1621 
1622   /* It is possible that this source file has more than one symtab,
1623      and that the new line number specification has moved us from the
1624      default (in file_symtab) to a new one.  */
1625   val.symtab = find_line_symtab (file_symtab, val.line, NULL, NULL);
1626   if (val.symtab == 0)
1627     val.symtab = file_symtab;
1628 
1629   val.pc = 0;
1630   values.sals = (struct symtab_and_line *)
1631     xmalloc (sizeof (struct symtab_and_line));
1632   values.sals[0] = val;
1633   values.nelts = 1;
1634   if (need_canonical)
1635     build_canonical_line_spec (values.sals, NULL, canonical);
1636   return values;
1637 }
1638 
1639 
1640 
1641 /* Decode a linespec starting with a dollar sign.  */
1642 
1643 static struct symtabs_and_lines
decode_dollar(char * copy,int funfirstline,struct symtab * default_symtab,char *** canonical,struct symtab * file_symtab)1644 decode_dollar (char *copy, int funfirstline, struct symtab *default_symtab,
1645 	       char ***canonical, struct symtab *file_symtab)
1646 {
1647   struct value *valx;
1648   int index = 0;
1649   int need_canonical = 0;
1650   struct symtabs_and_lines values;
1651   struct symtab_and_line val;
1652   char *p;
1653   struct symbol *sym;
1654   /* The symtab that SYM was found in.  */
1655   struct symtab *sym_symtab;
1656   struct minimal_symbol *msymbol;
1657 
1658   p = (copy[1] == '$') ? copy + 2 : copy + 1;
1659   while (*p >= '0' && *p <= '9')
1660     p++;
1661   if (!*p)		/* Reached end of token without hitting non-digit.  */
1662     {
1663       /* We have a value history reference.  */
1664       sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1665       valx = access_value_history ((copy[1] == '$') ? -index : index);
1666       if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1667 	error ("History values used in line specs must have integer values.");
1668     }
1669   else
1670     {
1671       /* Not all digits -- may be user variable/function or a
1672 	 convenience variable.  */
1673 
1674       /* Look up entire name as a symbol first.  */
1675       sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
1676       file_symtab = (struct symtab *) 0;
1677       need_canonical = 1;
1678       /* Symbol was found --> jump to normal symbol processing.  */
1679       if (sym)
1680 	return symbol_found (funfirstline, canonical, copy, sym,
1681 			     NULL, sym_symtab);
1682 
1683       /* If symbol was not found, look in minimal symbol tables.  */
1684       msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1685       /* Min symbol was found --> jump to minsym processing.  */
1686       if (msymbol)
1687 	return minsym_found (funfirstline, msymbol);
1688 
1689       /* Not a user variable or function -- must be convenience variable.  */
1690       need_canonical = (file_symtab == 0) ? 1 : 0;
1691       valx = value_of_internalvar (lookup_internalvar (copy + 1));
1692       if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1693 	error ("Convenience variables used in line specs must have integer values.");
1694     }
1695 
1696   init_sal (&val);
1697 
1698   /* Either history value or convenience value from above, in valx.  */
1699   val.symtab = file_symtab ? file_symtab : default_symtab;
1700   val.line = value_as_long (valx);
1701   val.pc = 0;
1702 
1703   values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1704   values.sals[0] = val;
1705   values.nelts = 1;
1706 
1707   if (need_canonical)
1708     build_canonical_line_spec (values.sals, NULL, canonical);
1709 
1710   return values;
1711 }
1712 
1713 
1714 
1715 /* Decode a linespec that's a variable.  If FILE_SYMTAB is non-NULL,
1716    look in that symtab's static variables first.  If NOT_FOUND_PTR is not NULL and
1717    the function cannot be found, store boolean true in the location pointed to
1718    and do not issue an error message.  */
1719 
1720 static struct symtabs_and_lines
decode_variable(char * copy,int funfirstline,char *** canonical,struct symtab * file_symtab,int * not_found_ptr)1721 decode_variable (char *copy, int funfirstline, char ***canonical,
1722 		 struct symtab *file_symtab, int *not_found_ptr)
1723 {
1724   struct symbol *sym;
1725   /* The symtab that SYM was found in.  */
1726   struct symtab *sym_symtab;
1727 
1728   struct minimal_symbol *msymbol;
1729 
1730   sym = lookup_symbol (copy,
1731 		       (file_symtab
1732 			? BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab),
1733 					     STATIC_BLOCK)
1734 			: get_selected_block (0)),
1735 		       VAR_DOMAIN, 0, &sym_symtab);
1736 
1737   if (sym != NULL)
1738     return symbol_found (funfirstline, canonical, copy, sym,
1739 			 file_symtab, sym_symtab);
1740 
1741   msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1742 
1743   if (msymbol != NULL)
1744     return minsym_found (funfirstline, msymbol);
1745 
1746   if (!have_full_symbols () &&
1747       !have_partial_symbols () && !have_minimal_symbols ())
1748     error ("No symbol table is loaded.  Use the \"file\" command.");
1749 
1750   if (not_found_ptr)
1751     {
1752       *not_found_ptr = 1;
1753       /* The caller has indicated that it wishes quiet notification of any
1754 	 error where the function or file is not found.  A call to
1755 	 error_silent causes an error to occur, but it does not issue
1756 	 the supplied message.  The message can be manually output by
1757 	 the caller, if desired.  This is used, for example, when
1758 	 attempting to set breakpoints for functions in shared libraries
1759 	 that have not yet been loaded.  */
1760       error_silent ("Function \"%s\" not defined.", copy);
1761     }
1762 
1763   error ("Function \"%s\" not defined.", copy);
1764 }
1765 
1766 
1767 
1768 
1769 /* Now come some functions that are called from multiple places within
1770    decode_line_1.  */
1771 
1772 /* We've found a symbol SYM to associate with our linespec; build a
1773    corresponding struct symtabs_and_lines.  */
1774 
1775 static struct symtabs_and_lines
symbol_found(int funfirstline,char *** canonical,char * copy,struct symbol * sym,struct symtab * file_symtab,struct symtab * sym_symtab)1776 symbol_found (int funfirstline, char ***canonical, char *copy,
1777 	      struct symbol *sym, struct symtab *file_symtab,
1778 	      struct symtab *sym_symtab)
1779 {
1780   struct symtabs_and_lines values;
1781 
1782   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1783     {
1784       /* Arg is the name of a function */
1785       values.sals = (struct symtab_and_line *)
1786 	xmalloc (sizeof (struct symtab_and_line));
1787       values.sals[0] = find_function_start_sal (sym, funfirstline);
1788       values.nelts = 1;
1789 
1790       /* Don't use the SYMBOL_LINE; if used at all it points to
1791 	 the line containing the parameters or thereabouts, not
1792 	 the first line of code.  */
1793 
1794       /* We might need a canonical line spec if it is a static
1795 	 function.  */
1796       if (file_symtab == 0)
1797 	{
1798 	  struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1799 	  struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1800 	  if (lookup_block_symbol (b, copy, NULL, VAR_DOMAIN) != NULL)
1801 	    build_canonical_line_spec (values.sals, copy, canonical);
1802 	}
1803       return values;
1804     }
1805   else
1806     {
1807       if (funfirstline)
1808 	error ("\"%s\" is not a function", copy);
1809       else if (SYMBOL_LINE (sym) != 0)
1810 	{
1811 	  /* We know its line number.  */
1812 	  values.sals = (struct symtab_and_line *)
1813 	    xmalloc (sizeof (struct symtab_and_line));
1814 	  values.nelts = 1;
1815 	  memset (&values.sals[0], 0, sizeof (values.sals[0]));
1816 	  values.sals[0].symtab = sym_symtab;
1817 	  values.sals[0].line = SYMBOL_LINE (sym);
1818 	  return values;
1819 	}
1820       else
1821 	/* This can happen if it is compiled with a compiler which doesn't
1822 	   put out line numbers for variables.  */
1823 	/* FIXME: Shouldn't we just set .line and .symtab to zero
1824 	   and return?  For example, "info line foo" could print
1825 	   the address.  */
1826 	error ("Line number not known for symbol \"%s\"", copy);
1827     }
1828 }
1829 
1830 /* We've found a minimal symbol MSYMBOL to associate with our
1831    linespec; build a corresponding struct symtabs_and_lines.  */
1832 
1833 static struct symtabs_and_lines
minsym_found(int funfirstline,struct minimal_symbol * msymbol)1834 minsym_found (int funfirstline, struct minimal_symbol *msymbol)
1835 {
1836   struct symtabs_and_lines values;
1837 
1838   values.sals = (struct symtab_and_line *)
1839     xmalloc (sizeof (struct symtab_and_line));
1840   values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1841 				      (struct bfd_section *) 0, 0);
1842   values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1843   if (funfirstline)
1844     {
1845       values.sals[0].pc += DEPRECATED_FUNCTION_START_OFFSET;
1846       values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1847     }
1848   values.nelts = 1;
1849   return values;
1850 }
1851