]> Git Repo - binutils.git/blob - gdb/linespec.c
* gdbint.texinfo (User Interface): ISO C rather than ISO-C.
[binutils.git] / gdb / linespec.c
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.
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 "demangle.h"
30 #include "value.h"
31 #include "completer.h"
32 #include "cp-abi.h"
33
34 /* Prototype for one function in parser-defs.h,
35    instead of including that entire file. */
36
37 extern char *find_template_name_end (char *);
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 cplusplus_error (const char *name, const char *fmt, ...) ATTR_FORMAT (printf, 2, 3);
46
47 static int total_number_of_methods (struct type *type);
48
49 static int find_methods (struct type *, char *, struct symbol **);
50
51 static void build_canonical_line_spec (struct symtab_and_line *,
52                                        char *, char ***);
53
54 static char *find_toplevel_char (char *s, char c);
55
56 static struct symtabs_and_lines decode_line_2 (struct symbol *[],
57                                                int, int, char ***);
58
59 /* Helper functions. */
60
61 /* Issue a helpful hint on using the command completion feature on
62    single quoted demangled C++ symbols as part of the completion
63    error.  */
64
65 static void
66 cplusplus_error (const char *name, const char *fmt, ...)
67 {
68   struct ui_file *tmp_stream;
69   tmp_stream = mem_fileopen ();
70   make_cleanup_ui_file_delete (tmp_stream);
71
72   {
73     va_list args;
74     va_start (args, fmt);
75     vfprintf_unfiltered (tmp_stream, fmt, args);
76     va_end (args);
77   }
78
79   while (*name == '\'')
80     name++;
81   fprintf_unfiltered (tmp_stream,
82                       ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
83                        "(Note leading single quote.)"),
84                       name, name);
85   error_stream (tmp_stream);
86 }
87
88 /* Return the number of methods described for TYPE, including the
89    methods from types it derives from. This can't be done in the symbol
90    reader because the type of the baseclass might still be stubbed
91    when the definition of the derived class is parsed.  */
92
93 static int
94 total_number_of_methods (struct type *type)
95 {
96   int n;
97   int count;
98
99   CHECK_TYPEDEF (type);
100   if (TYPE_CPLUS_SPECIFIC (type) == NULL)
101     return 0;
102   count = TYPE_NFN_FIELDS_TOTAL (type);
103
104   for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
105     count += total_number_of_methods (TYPE_BASECLASS (type, n));
106
107   return count;
108 }
109
110 /* Recursive helper function for decode_line_1.
111    Look for methods named NAME in type T.
112    Return number of matches.
113    Put matches in SYM_ARR, which should have been allocated with
114    a size of total_number_of_methods (T) * sizeof (struct symbol *).
115    Note that this function is g++ specific.  */
116
117 static int
118 find_methods (struct type *t, char *name, struct symbol **sym_arr)
119 {
120   int i1 = 0;
121   int ibase;
122   char *class_name = type_name_no_tag (t);
123
124   /* Ignore this class if it doesn't have a name.  This is ugly, but
125      unless we figure out how to get the physname without the name of
126      the class, then the loop can't do any good.  */
127   if (class_name
128       && (lookup_symbol (class_name, (struct block *) NULL,
129                          STRUCT_NAMESPACE, (int *) NULL,
130                          (struct symtab **) NULL)))
131     {
132       int method_counter;
133       int name_len = strlen (name);
134
135       CHECK_TYPEDEF (t);
136
137       /* Loop over each method name.  At this level, all overloads of a name
138          are counted as a single name.  There is an inner loop which loops over
139          each overload.  */
140
141       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
142            method_counter >= 0;
143            --method_counter)
144         {
145           int field_counter;
146           char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
147           char dem_opname[64];
148
149           if (strncmp (method_name, "__", 2) == 0 ||
150               strncmp (method_name, "op", 2) == 0 ||
151               strncmp (method_name, "type", 4) == 0)
152             {
153               if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
154                 method_name = dem_opname;
155               else if (cplus_demangle_opname (method_name, dem_opname, 0))
156                 method_name = dem_opname;
157             }
158
159           if (strcmp_iw (name, method_name) == 0)
160             /* Find all the overloaded methods with that name.  */
161             for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
162                  field_counter >= 0;
163                  --field_counter)
164               {
165                 struct fn_field *f;
166                 char *phys_name;
167
168                 f = TYPE_FN_FIELDLIST1 (t, method_counter);
169
170                 if (TYPE_FN_FIELD_STUB (f, field_counter))
171                   {
172                     char *tmp_name;
173
174                     tmp_name = gdb_mangle_name (t,
175                                                 method_counter,
176                                                 field_counter);
177                     phys_name = alloca (strlen (tmp_name) + 1);
178                     strcpy (phys_name, tmp_name);
179                     xfree (tmp_name);
180                   }
181                 else
182                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
183                 
184                 /* Destructor is handled by caller, dont add it to the list */
185                 if (is_destructor_name (phys_name) != 0)
186                   continue;
187
188                 sym_arr[i1] = lookup_symbol (phys_name,
189                                              NULL, VAR_NAMESPACE,
190                                              (int *) NULL,
191                                              (struct symtab **) NULL);
192                 if (sym_arr[i1])
193                   i1++;
194                 else
195                   {
196                     /* This error message gets printed, but the method
197                        still seems to be found
198                        fputs_filtered("(Cannot find method ", gdb_stdout);
199                        fprintf_symbol_filtered (gdb_stdout, phys_name,
200                        language_cplus,
201                        DMGL_PARAMS | DMGL_ANSI);
202                        fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
203                      */
204                   }
205               }
206           else if (strncmp (class_name, name, name_len) == 0
207                    && (class_name[name_len] == '\0'
208                        || class_name[name_len] == '<'))
209             {
210               /* For GCC 3.x and stabs, constructors and destructors have names
211                  like __base_ctor and __complete_dtor.  Check the physname for now
212                  if we're looking for a constructor.  */
213               for (field_counter
214                      = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
215                    field_counter >= 0;
216                    --field_counter)
217                 {
218                   struct fn_field *f;
219                   char *phys_name;
220                   
221                   f = TYPE_FN_FIELDLIST1 (t, method_counter);
222
223                   /* GCC 3.x will never produce stabs stub methods, so we don't need
224                      to handle this case.  */
225                   if (TYPE_FN_FIELD_STUB (f, field_counter))
226                     continue;
227                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
228                   if (! is_constructor_name (phys_name))
229                     continue;
230
231                   /* If this method is actually defined, include it in the
232                      list.  */
233                   sym_arr[i1] = lookup_symbol (phys_name,
234                                                NULL, VAR_NAMESPACE,
235                                                (int *) NULL,
236                                                (struct symtab **) NULL);
237                   if (sym_arr[i1])
238                     i1++;
239                 }
240             }
241         }
242     }
243
244   /* Only search baseclasses if there is no match yet, since names in
245      derived classes override those in baseclasses.
246
247      FIXME: The above is not true; it is only true of member functions
248      if they have the same number of arguments (??? - section 13.1 of the
249      ARM says the function members are not in the same scope but doesn't
250      really spell out the rules in a way I understand.  In any case, if
251      the number of arguments differ this is a case in which we can overload
252      rather than hiding without any problem, and gcc 2.4.5 does overload
253      rather than hiding in this case).  */
254
255   if (i1 == 0)
256     for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
257       i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
258
259   return i1;
260 }
261
262 /* Helper function for decode_line_1.
263    Build a canonical line spec in CANONICAL if it is non-NULL and if
264    the SAL has a symtab.
265    If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
266    If SYMNAME is NULL the line number from SAL is used and the canonical
267    line spec is `filename:linenum'.  */
268
269 static void
270 build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
271                            char ***canonical)
272 {
273   char **canonical_arr;
274   char *canonical_name;
275   char *filename;
276   struct symtab *s = sal->symtab;
277
278   if (s == (struct symtab *) NULL
279       || s->filename == (char *) NULL
280       || canonical == (char ***) NULL)
281     return;
282
283   canonical_arr = (char **) xmalloc (sizeof (char *));
284   *canonical = canonical_arr;
285
286   filename = s->filename;
287   if (symname != NULL)
288     {
289       canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
290       sprintf (canonical_name, "%s:%s", filename, symname);
291     }
292   else
293     {
294       canonical_name = xmalloc (strlen (filename) + 30);
295       sprintf (canonical_name, "%s:%d", filename, sal->line);
296     }
297   canonical_arr[0] = canonical_name;
298 }
299
300
301
302 /* Find an instance of the character C in the string S that is outside
303    of all parenthesis pairs, single-quoted strings, and double-quoted
304    strings.  Also, ignore the char within a template name, like a ','
305    within foo<int, int>.  */
306
307 static char *
308 find_toplevel_char (char *s, char c)
309 {
310   int quoted = 0;               /* zero if we're not in quotes;
311                                    '"' if we're in a double-quoted string;
312                                    '\'' if we're in a single-quoted string.  */
313   int depth = 0;                /* number of unclosed parens we've seen */
314   char *scan;
315
316   for (scan = s; *scan; scan++)
317     {
318       if (quoted)
319         {
320           if (*scan == quoted)
321             quoted = 0;
322           else if (*scan == '\\' && *(scan + 1))
323             scan++;
324         }
325       else if (*scan == c && ! quoted && depth == 0)
326         return scan;
327       else if (*scan == '"' || *scan == '\'')
328         quoted = *scan;
329       else if (*scan == '(' || *scan == '<')
330         depth++;
331       else if ((*scan == ')' || *scan == '>') && depth > 0)
332         depth--;
333     }
334
335   return 0;
336 }
337
338 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
339    operate on (ask user if necessary).
340    If CANONICAL is non-NULL return a corresponding array of mangled names
341    as canonical line specs there.  */
342
343 static struct symtabs_and_lines
344 decode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
345                char ***canonical)
346 {
347   struct symtabs_and_lines values, return_values;
348   char *args, *arg1;
349   int i;
350   char *prompt;
351   char *symname;
352   struct cleanup *old_chain;
353   char **canonical_arr = (char **) NULL;
354
355   values.sals = (struct symtab_and_line *)
356     alloca (nelts * sizeof (struct symtab_and_line));
357   return_values.sals = (struct symtab_and_line *)
358     xmalloc (nelts * sizeof (struct symtab_and_line));
359   old_chain = make_cleanup (xfree, return_values.sals);
360
361   if (canonical)
362     {
363       canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
364       make_cleanup (xfree, canonical_arr);
365       memset (canonical_arr, 0, nelts * sizeof (char *));
366       *canonical = canonical_arr;
367     }
368
369   i = 0;
370   printf_unfiltered ("[0] cancel\n[1] all\n");
371   while (i < nelts)
372     {
373       INIT_SAL (&return_values.sals[i]);        /* initialize to zeroes */
374       INIT_SAL (&values.sals[i]);
375       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
376         {
377           values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
378           printf_unfiltered ("[%d] %s at %s:%d\n",
379                              (i + 2),
380                              SYMBOL_SOURCE_NAME (sym_arr[i]),
381                              values.sals[i].symtab->filename,
382                              values.sals[i].line);
383         }
384       else
385         printf_unfiltered ("?HERE\n");
386       i++;
387     }
388
389   if ((prompt = getenv ("PS2")) == NULL)
390     {
391       prompt = "> ";
392     }
393   args = command_line_input (prompt, 0, "overload-choice");
394
395   if (args == 0 || *args == 0)
396     error_no_arg ("one or more choice numbers");
397
398   i = 0;
399   while (*args)
400     {
401       int num;
402
403       arg1 = args;
404       while (*arg1 >= '0' && *arg1 <= '9')
405         arg1++;
406       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
407         error ("Arguments must be choice numbers.");
408
409       num = atoi (args);
410
411       if (num == 0)
412         error ("canceled");
413       else if (num == 1)
414         {
415           if (canonical_arr)
416             {
417               for (i = 0; i < nelts; i++)
418                 {
419                   if (canonical_arr[i] == NULL)
420                     {
421                       symname = SYMBOL_NAME (sym_arr[i]);
422                       canonical_arr[i] = savestring (symname, strlen (symname));
423                     }
424                 }
425             }
426           memcpy (return_values.sals, values.sals,
427                   (nelts * sizeof (struct symtab_and_line)));
428           return_values.nelts = nelts;
429           discard_cleanups (old_chain);
430           return return_values;
431         }
432
433       if (num >= nelts + 2)
434         {
435           printf_unfiltered ("No choice number %d.\n", num);
436         }
437       else
438         {
439           num -= 2;
440           if (values.sals[num].pc)
441             {
442               if (canonical_arr)
443                 {
444                   symname = SYMBOL_NAME (sym_arr[num]);
445                   make_cleanup (xfree, symname);
446                   canonical_arr[i] = savestring (symname, strlen (symname));
447                 }
448               return_values.sals[i++] = values.sals[num];
449               values.sals[num].pc = 0;
450             }
451           else
452             {
453               printf_unfiltered ("duplicate request for %d ignored.\n", num);
454             }
455         }
456
457       args = arg1;
458       while (*args == ' ' || *args == '\t')
459         args++;
460     }
461   return_values.nelts = i;
462   discard_cleanups (old_chain);
463   return return_values;
464 }
465 \f
466 /* The parser of linespec itself. */
467
468 /* Parse a string that specifies a line number.
469    Pass the address of a char * variable; that variable will be
470    advanced over the characters actually parsed.
471
472    The string can be:
473
474    LINENUM -- that line number in current file.  PC returned is 0.
475    FILE:LINENUM -- that line in that file.  PC returned is 0.
476    FUNCTION -- line number of openbrace of that function.
477    PC returned is the start of the function.
478    VARIABLE -- line number of definition of that variable.
479    PC returned is 0.
480    FILE:FUNCTION -- likewise, but prefer functions in that file.
481    *EXPR -- line in which address EXPR appears.
482
483    This may all be followed by an "if EXPR", which we ignore.
484
485    FUNCTION may be an undebuggable function found in minimal symbol table.
486
487    If the argument FUNFIRSTLINE is nonzero, we want the first line
488    of real code inside a function when a function is specified, and it is
489    not OK to specify a variable or type to get its line number.
490
491    DEFAULT_SYMTAB specifies the file to use if none is specified.
492    It defaults to current_source_symtab.
493    DEFAULT_LINE specifies the line number to use for relative
494    line numbers (that start with signs).  Defaults to current_source_line.
495    If CANONICAL is non-NULL, store an array of strings containing the canonical
496    line specs there if necessary. Currently overloaded member functions and
497    line numbers or static functions without a filename yield a canonical
498    line spec. The array and the line spec strings are allocated on the heap,
499    it is the callers responsibility to free them.
500
501    Note that it is possible to return zero for the symtab
502    if no file is validly specified.  Callers must check that.
503    Also, the line number returned may be invalid.  */
504
505 /* We allow single quotes in various places.  This is a hideous
506    kludge, which exists because the completer can't yet deal with the
507    lack of single quotes.  FIXME: write a linespec_completer which we
508    can use as appropriate instead of make_symbol_completion_list.  */
509
510 struct symtabs_and_lines
511 decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
512                int default_line, char ***canonical)
513 {
514   struct symtabs_and_lines values;
515   struct symtab_and_line val;
516   register char *p, *p1;
517   char *q, *pp, *ii, *p2;
518 #if 0
519   char *q1;
520 #endif
521   register struct symtab *s;
522
523   register struct symbol *sym;
524   /* The symtab that SYM was found in.  */
525   struct symtab *sym_symtab;
526
527   register CORE_ADDR pc;
528   register struct minimal_symbol *msymbol;
529   char *copy;
530   struct symbol *sym_class;
531   int i1;
532   int is_quoted;
533   int is_quote_enclosed;
534   int has_parens;
535   int has_if = 0;
536   int has_comma = 0;
537   struct symbol **sym_arr;
538   struct type *t;
539   char *saved_arg = *argptr;
540   extern char *gdb_completer_quote_characters;
541
542   INIT_SAL (&val);              /* initialize to zeroes */
543
544   /* Defaults have defaults.  */
545
546   if (default_symtab == 0)
547     {
548       default_symtab = current_source_symtab;
549       default_line = current_source_line;
550     }
551
552   /* See if arg is *PC */
553
554   if (**argptr == '*')
555     {
556       (*argptr)++;
557       pc = parse_and_eval_address_1 (argptr);
558
559       values.sals = (struct symtab_and_line *)
560         xmalloc (sizeof (struct symtab_and_line));
561
562       values.nelts = 1;
563       values.sals[0] = find_pc_line (pc, 0);
564       values.sals[0].pc = pc;
565       values.sals[0].section = find_pc_overlay (pc);
566
567       return values;
568     }
569
570   /* 'has_if' is for the syntax:
571    *     (gdb) break foo if (a==b)
572    */
573   if ((ii = strstr (*argptr, " if ")) != NULL ||
574       (ii = strstr (*argptr, "\tif ")) != NULL ||
575       (ii = strstr (*argptr, " if\t")) != NULL ||
576       (ii = strstr (*argptr, "\tif\t")) != NULL ||
577       (ii = strstr (*argptr, " if(")) != NULL ||
578       (ii = strstr (*argptr, "\tif( ")) != NULL)
579     has_if = 1;
580   /* Temporarily zap out "if (condition)" to not
581    * confuse the parenthesis-checking code below.
582    * This is undone below. Do not change ii!!
583    */
584   if (has_if)
585     {
586       *ii = '\0';
587     }
588
589   /* Set various flags.
590    * 'has_parens' is important for overload checking, where
591    * we allow things like: 
592    *     (gdb) break c::f(int)
593    */
594
595   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
596
597   is_quoted = (**argptr
598                && strchr (get_gdb_completer_quote_characters (),
599                           **argptr) != NULL);
600
601   has_parens = ((pp = strchr (*argptr, '(')) != NULL
602                 && (pp = strrchr (pp, ')')) != NULL);
603
604   /* Now that we're safely past the has_parens check,
605    * put back " if (condition)" so outer layers can see it 
606    */
607   if (has_if)
608     *ii = ' ';
609
610   /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
611      and we must isolate the first half.  Outer layers will call again later
612      for the second half.
613
614      Don't count commas that appear in argument lists of overloaded
615      functions, or in quoted strings.  It's stupid to go to this much
616      trouble when the rest of the function is such an obvious roach hotel.  */
617   ii = find_toplevel_char (*argptr, ',');
618   has_comma = (ii != 0);
619
620   /* Temporarily zap out second half to not
621    * confuse the code below.
622    * This is undone below. Do not change ii!!
623    */
624   if (has_comma)
625     {
626       *ii = '\0';
627     }
628
629   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
630   /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
631   /* Look for ':', but ignore inside of <> */
632
633   s = NULL;
634   p = *argptr;
635   if (p[0] == '"')
636     {
637       is_quote_enclosed = 1;
638       (*argptr)++;
639       p++;
640     }
641   else
642     is_quote_enclosed = 0;
643   for (; *p; p++)
644     {
645       if (p[0] == '<')
646         {
647           char *temp_end = find_template_name_end (p);
648           if (!temp_end)
649             error ("malformed template specification in command");
650           p = temp_end;
651         }
652       /* Check for the end of the first half of the linespec.  End of line,
653          a tab, a double colon or the last single colon, or a space.  But
654          if enclosed in double quotes we do not break on enclosed spaces */
655       if (!*p
656           || p[0] == '\t'
657           || ((p[0] == ':')
658               && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
659           || ((p[0] == ' ') && !is_quote_enclosed))
660         break;
661       if (p[0] == '.' && strchr (p, ':') == NULL)       /* Java qualified method. */
662         {
663           /* Find the *last* '.', since the others are package qualifiers. */
664           for (p1 = p; *p1; p1++)
665             {
666               if (*p1 == '.')
667                 p = p1;
668             }
669           break;
670         }
671     }
672   while (p[0] == ' ' || p[0] == '\t')
673     p++;
674
675   /* if the closing double quote was left at the end, remove it */
676   if (is_quote_enclosed)
677     {
678       char *closing_quote = strchr (p - 1, '"');
679       if (closing_quote && closing_quote[1] == '\0')
680         *closing_quote = '\0';
681     }
682
683   /* Now that we've safely parsed the first half,
684    * put back ',' so outer layers can see it 
685    */
686   if (has_comma)
687     *ii = ',';
688
689   if ((p[0] == ':' || p[0] == '.') && !has_parens)
690     {
691       /*  C++ */
692       /*  ... or Java */
693       if (is_quoted)
694         *argptr = *argptr + 1;
695       if (p[0] == '.' || p[1] == ':')
696         {
697           char *saved_arg2 = *argptr;
698           char *temp_end;
699           /* First check for "global" namespace specification,
700              of the form "::foo". If found, skip over the colons
701              and jump to normal symbol processing */
702           if (p[0] == ':' 
703               && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
704             saved_arg2 += 2;
705
706           /* We have what looks like a class or namespace
707              scope specification (A::B), possibly with many
708              levels of namespaces or classes (A::B::C::D).
709
710              Some versions of the HP ANSI C++ compiler (as also possibly
711              other compilers) generate class/function/member names with
712              embedded double-colons if they are inside namespaces. To
713              handle this, we loop a few times, considering larger and
714              larger prefixes of the string as though they were single
715              symbols.  So, if the initially supplied string is
716              A::B::C::D::foo, we have to look up "A", then "A::B",
717              then "A::B::C", then "A::B::C::D", and finally
718              "A::B::C::D::foo" as single, monolithic symbols, because
719              A, B, C or D may be namespaces.
720
721              Note that namespaces can nest only inside other
722              namespaces, and not inside classes.  So we need only
723              consider *prefixes* of the string; there is no need to look up
724              "B::C" separately as a symbol in the previous example. */
725
726           p2 = p;               /* save for restart */
727           while (1)
728             {
729               /* Extract the class name.  */
730               p1 = p;
731               while (p != *argptr && p[-1] == ' ')
732                 --p;
733               copy = (char *) alloca (p - *argptr + 1);
734               memcpy (copy, *argptr, p - *argptr);
735               copy[p - *argptr] = 0;
736
737               /* Discard the class name from the arg.  */
738               p = p1 + (p1[0] == ':' ? 2 : 1);
739               while (*p == ' ' || *p == '\t')
740                 p++;
741               *argptr = p;
742
743               sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
744                                          (struct symtab **) NULL);
745
746               if (sym_class &&
747                   (t = check_typedef (SYMBOL_TYPE (sym_class)),
748                    (TYPE_CODE (t) == TYPE_CODE_STRUCT
749                     || TYPE_CODE (t) == TYPE_CODE_UNION)))
750                 {
751                   /* Arg token is not digits => try it as a function name
752                      Find the next token(everything up to end or next blank). */
753                   if (**argptr
754                       && strchr (get_gdb_completer_quote_characters (),
755                                  **argptr) != NULL)
756                     {
757                       p = skip_quoted (*argptr);
758                       *argptr = *argptr + 1;
759                     }
760                   else
761                     {
762                       p = *argptr;
763                       while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
764                         p++;
765                     }
766 /*
767    q = operator_chars (*argptr, &q1);
768    if (q1 - q)
769    {
770    char *opname;
771    char *tmp = alloca (q1 - q + 1);
772    memcpy (tmp, q, q1 - q);
773    tmp[q1 - q] = '\0';
774    opname = cplus_mangle_opname (tmp, DMGL_ANSI);
775    if (opname == NULL)
776    {
777    cplusplus_error (saved_arg, "no mangling for \"%s\"\n", tmp);
778    }
779    copy = (char*) alloca (3 + strlen(opname));
780    sprintf (copy, "__%s", opname);
781    p = q1;
782    }
783    else
784  */
785                   {
786                     copy = (char *) alloca (p - *argptr + 1);
787                     memcpy (copy, *argptr, p - *argptr);
788                     copy[p - *argptr] = '\0';
789                     if (p != *argptr
790                         && copy[p - *argptr - 1]
791                         && strchr (get_gdb_completer_quote_characters (),
792                                    copy[p - *argptr - 1]) != NULL)
793                       copy[p - *argptr - 1] = '\0';
794                   }
795
796                   /* no line number may be specified */
797                   while (*p == ' ' || *p == '\t')
798                     p++;
799                   *argptr = p;
800
801                   sym = 0;
802                   i1 = 0;       /*  counter for the symbol array */
803                   sym_arr = (struct symbol **) alloca (total_number_of_methods (t)
804                                                 * sizeof (struct symbol *));
805
806                   if (destructor_name_p (copy, t))
807                     {
808                       /* Destructors are a special case.  */
809                       int m_index, f_index;
810
811                       if (get_destructor_fn_field (t, &m_index, &f_index))
812                         {
813                           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
814
815                           sym_arr[i1] =
816                             lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
817                                            NULL, VAR_NAMESPACE, (int *) NULL,
818                                            (struct symtab **) NULL);
819                           if (sym_arr[i1])
820                             i1++;
821                         }
822                     }
823                   else
824                     i1 = find_methods (t, copy, sym_arr);
825                   if (i1 == 1)
826                     {
827                       /* There is exactly one field with that name.  */
828                       sym = sym_arr[0];
829
830                       if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
831                         {
832                           values.sals = (struct symtab_and_line *)
833                             xmalloc (sizeof (struct symtab_and_line));
834                           values.nelts = 1;
835                           values.sals[0] = find_function_start_sal (sym,
836                                                               funfirstline);
837                         }
838                       else
839                         {
840                           values.nelts = 0;
841                         }
842                       return values;
843                     }
844                   if (i1 > 0)
845                     {
846                       /* There is more than one field with that name
847                          (overloaded).  Ask the user which one to use.  */
848                       return decode_line_2 (sym_arr, i1, funfirstline, canonical);
849                     }
850                   else
851                     {
852                       char *tmp;
853
854                       if (is_operator_name (copy))
855                         {
856                           tmp = (char *) alloca (strlen (copy + 3) + 9);
857                           strcpy (tmp, "operator ");
858                           strcat (tmp, copy + 3);
859                         }
860                       else
861                         tmp = copy;
862                       if (tmp[0] == '~')
863                         cplusplus_error (saved_arg,
864                                          "the class `%s' does not have destructor defined\n",
865                                          SYMBOL_SOURCE_NAME (sym_class));
866                       else
867                         cplusplus_error (saved_arg,
868                                          "the class %s does not have any method named %s\n",
869                                          SYMBOL_SOURCE_NAME (sym_class), tmp);
870                     }
871                 }
872
873               /* Move pointer up to next possible class/namespace token */
874               p = p2 + 1;       /* restart with old value +1 */
875               /* Move pointer ahead to next double-colon */
876               while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
877                 {
878                   if (p[0] == '<')
879                     {
880                       temp_end = find_template_name_end (p);
881                       if (!temp_end)
882                         error ("malformed template specification in command");
883                       p = temp_end;
884                     }
885                   else if ((p[0] == ':') && (p[1] == ':'))
886                     break;      /* found double-colon */
887                   else
888                     p++;
889                 }
890
891               if (*p != ':')
892                 break;          /* out of the while (1) */
893
894               p2 = p;           /* save restart for next time around */
895               *argptr = saved_arg2;     /* restore argptr */
896             }                   /* while (1) */
897
898           /* Last chance attempt -- check entire name as a symbol */
899           /* Use "copy" in preparation for jumping out of this block,
900              to be consistent with usage following the jump target */
901           copy = (char *) alloca (p - saved_arg2 + 1);
902           memcpy (copy, saved_arg2, p - saved_arg2);
903           /* Note: if is_quoted should be true, we snuff out quote here anyway */
904           copy[p - saved_arg2] = '\000';
905           /* Set argptr to skip over the name */
906           *argptr = (*p == '\'') ? p + 1 : p;
907           /* Look up entire name */
908           sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
909           s = (struct symtab *) 0;
910           /* Prepare to jump: restore the " if (condition)" so outer layers see it */
911           /* Symbol was found --> jump to normal symbol processing.
912              Code following "symbol_found" expects "copy" to have the
913              symbol name, "sym" to have the symbol pointer, "s" to be
914              a specified file's symtab, and sym_symtab to be the symbol's
915              symtab. */
916           /* By jumping there we avoid falling through the FILE:LINE and
917              FILE:FUNC processing stuff below */
918           if (sym)
919             goto symbol_found;
920
921           /* Couldn't find any interpretation as classes/namespaces, so give up */
922           /* The quotes are important if copy is empty.  */
923           cplusplus_error (saved_arg,
924                            "Can't find member of namespace, class, struct, or union named \"%s\"\n",
925                            copy);
926         }
927       /*  end of C++  */
928
929
930       /* Extract the file name.  */
931       p1 = p;
932       while (p != *argptr && p[-1] == ' ')
933         --p;
934       if ((*p == '"') && is_quote_enclosed)
935         --p;
936       copy = (char *) alloca (p - *argptr + 1);
937       memcpy (copy, *argptr, p - *argptr);
938       /* It may have the ending quote right after the file name */
939       if (is_quote_enclosed && copy[p - *argptr - 1] == '"')
940         copy[p - *argptr - 1] = 0;
941       else
942         copy[p - *argptr] = 0;
943
944       /* Find that file's data.  */
945       s = lookup_symtab (copy);
946       if (s == 0)
947         {
948           if (!have_full_symbols () && !have_partial_symbols ())
949             error ("No symbol table is loaded.  Use the \"file\" command.");
950           error ("No source file named %s.", copy);
951         }
952
953       /* Discard the file name from the arg.  */
954       p = p1 + 1;
955       while (*p == ' ' || *p == '\t')
956         p++;
957       *argptr = p;
958     }
959 #if 0
960   /* No one really seems to know why this was added. It certainly
961      breaks the command line, though, whenever the passed
962      name is of the form ClassName::Method. This bit of code
963      singles out the class name, and if funfirstline is set (for
964      example, you are setting a breakpoint at this function),
965      you get an error. This did not occur with earlier
966      verions, so I am ifdef'ing this out. 3/29/99 */
967   else
968     {
969       /* Check if what we have till now is a symbol name */
970
971       /* We may be looking at a template instantiation such
972          as "foo<int>".  Check here whether we know about it,
973          instead of falling through to the code below which
974          handles ordinary function names, because that code
975          doesn't like seeing '<' and '>' in a name -- the
976          skip_quoted call doesn't go past them.  So see if we
977          can figure it out right now. */
978
979       copy = (char *) alloca (p - *argptr + 1);
980       memcpy (copy, *argptr, p - *argptr);
981       copy[p - *argptr] = '\000';
982       sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
983       if (sym)
984         {
985           /* Yes, we have a symbol; jump to symbol processing */
986           /* Code after symbol_found expects S, SYM_SYMTAB, SYM, 
987              and COPY to be set correctly */
988           *argptr = (*p == '\'') ? p + 1 : p;
989           s = (struct symtab *) 0;
990           goto symbol_found;
991         }
992       /* Otherwise fall out from here and go to file/line spec
993          processing, etc. */
994     }
995 #endif
996
997   /* S is specified file's symtab, or 0 if no file specified.
998      arg no longer contains the file name.  */
999
1000   /* Check whether arg is all digits (and sign) */
1001
1002   q = *argptr;
1003   if (*q == '-' || *q == '+')
1004     q++;
1005   while (*q >= '0' && *q <= '9')
1006     q++;
1007
1008   if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
1009     {
1010       /* We found a token consisting of all digits -- at least one digit.  */
1011       enum sign
1012         {
1013           none, plus, minus
1014         }
1015       sign = none;
1016
1017       /* We might need a canonical line spec if no file was specified.  */
1018       int need_canonical = (s == 0) ? 1 : 0;
1019
1020       /* This is where we need to make sure that we have good defaults.
1021          We must guarantee that this section of code is never executed
1022          when we are called with just a function name, since
1023          select_source_symtab calls us with such an argument  */
1024
1025       if (s == 0 && default_symtab == 0)
1026         {
1027           select_source_symtab (0);
1028           default_symtab = current_source_symtab;
1029           default_line = current_source_line;
1030         }
1031
1032       if (**argptr == '+')
1033         sign = plus, (*argptr)++;
1034       else if (**argptr == '-')
1035         sign = minus, (*argptr)++;
1036       val.line = atoi (*argptr);
1037       switch (sign)
1038         {
1039         case plus:
1040           if (q == *argptr)
1041             val.line = 5;
1042           if (s == 0)
1043             val.line = default_line + val.line;
1044           break;
1045         case minus:
1046           if (q == *argptr)
1047             val.line = 15;
1048           if (s == 0)
1049             val.line = default_line - val.line;
1050           else
1051             val.line = 1;
1052           break;
1053         case none:
1054           break;                /* No need to adjust val.line.  */
1055         }
1056
1057       while (*q == ' ' || *q == '\t')
1058         q++;
1059       *argptr = q;
1060       if (s == 0)
1061         s = default_symtab;
1062
1063       /* It is possible that this source file has more than one symtab, 
1064          and that the new line number specification has moved us from the
1065          default (in s) to a new one.  */
1066       val.symtab = find_line_symtab (s, val.line, NULL, NULL);
1067       if (val.symtab == 0)
1068         val.symtab = s;
1069
1070       val.pc = 0;
1071       values.sals = (struct symtab_and_line *)
1072         xmalloc (sizeof (struct symtab_and_line));
1073       values.sals[0] = val;
1074       values.nelts = 1;
1075       if (need_canonical)
1076         build_canonical_line_spec (values.sals, NULL, canonical);
1077       return values;
1078     }
1079
1080   /* Arg token is not digits => try it as a variable name
1081      Find the next token (everything up to end or next whitespace).  */
1082
1083   if (**argptr == '$')          /* May be a convenience variable */
1084     p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));        /* One or two $ chars possible */
1085   else if (is_quoted)
1086     {
1087       p = skip_quoted (*argptr);
1088       if (p[-1] != '\'')
1089         error ("Unmatched single quote.");
1090     }
1091   else if (has_parens)
1092     {
1093       p = pp + 1;
1094     }
1095   else
1096     {
1097       p = skip_quoted (*argptr);
1098     }
1099
1100   copy = (char *) alloca (p - *argptr + 1);
1101   memcpy (copy, *argptr, p - *argptr);
1102   copy[p - *argptr] = '\0';
1103   if (p != *argptr
1104       && copy[0]
1105       && copy[0] == copy[p - *argptr - 1]
1106       && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
1107     {
1108       copy[p - *argptr - 1] = '\0';
1109       copy++;
1110     }
1111   while (*p == ' ' || *p == '\t')
1112     p++;
1113   *argptr = p;
1114
1115   /* If it starts with $: may be a legitimate variable or routine name
1116      (e.g. HP-UX millicode routines such as $$dyncall), or it may
1117      be history value, or it may be a convenience variable */
1118
1119   if (*copy == '$')
1120     {
1121       struct value *valx;
1122       int index = 0;
1123       int need_canonical = 0;
1124
1125       p = (copy[1] == '$') ? copy + 2 : copy + 1;
1126       while (*p >= '0' && *p <= '9')
1127         p++;
1128       if (!*p)                  /* reached end of token without hitting non-digit */
1129         {
1130           /* We have a value history reference */
1131           sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1132           valx = access_value_history ((copy[1] == '$') ? -index : index);
1133           if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1134             error ("History values used in line specs must have integer values.");
1135         }
1136       else
1137         {
1138           /* Not all digits -- may be user variable/function or a
1139              convenience variable */
1140
1141           /* Look up entire name as a symbol first */
1142           sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
1143           s = (struct symtab *) 0;
1144           need_canonical = 1;
1145           /* Symbol was found --> jump to normal symbol processing.
1146              Code following "symbol_found" expects "copy" to have the
1147              symbol name, "sym" to have the symbol pointer, "s" to be
1148              a specified file's symtab, and sym_symtab to be the symbol's
1149              symtab. */
1150           if (sym)
1151             goto symbol_found;
1152
1153           /* If symbol was not found, look in minimal symbol tables */
1154           msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1155           /* Min symbol was found --> jump to minsym processing. */
1156           if (msymbol)
1157             goto minimal_symbol_found;
1158
1159           /* Not a user variable or function -- must be convenience variable */
1160           need_canonical = (s == 0) ? 1 : 0;
1161           valx = value_of_internalvar (lookup_internalvar (copy + 1));
1162           if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1163             error ("Convenience variables used in line specs must have integer values.");
1164         }
1165
1166       /* Either history value or convenience value from above, in valx */
1167       val.symtab = s ? s : default_symtab;
1168       val.line = value_as_long (valx);
1169       val.pc = 0;
1170
1171       values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1172       values.sals[0] = val;
1173       values.nelts = 1;
1174
1175       if (need_canonical)
1176         build_canonical_line_spec (values.sals, NULL, canonical);
1177
1178       return values;
1179     }
1180
1181
1182   /* Look up that token as a variable.
1183      If file specified, use that file's per-file block to start with.  */
1184
1185   sym = lookup_symbol (copy,
1186                        (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
1187                         : get_selected_block (0)),
1188                        VAR_NAMESPACE, 0, &sym_symtab);
1189
1190 symbol_found:                   /* We also jump here from inside the C++ class/namespace 
1191                                    code on finding a symbol of the form "A::B::C" */
1192
1193   if (sym != NULL)
1194     {
1195       if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1196         {
1197           /* Arg is the name of a function */
1198           values.sals = (struct symtab_and_line *)
1199             xmalloc (sizeof (struct symtab_and_line));
1200           values.sals[0] = find_function_start_sal (sym, funfirstline);
1201           values.nelts = 1;
1202
1203           /* Don't use the SYMBOL_LINE; if used at all it points to
1204              the line containing the parameters or thereabouts, not
1205              the first line of code.  */
1206
1207           /* We might need a canonical line spec if it is a static
1208              function.  */
1209           if (s == 0)
1210             {
1211               struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1212               struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1213               if (lookup_block_symbol (b, copy, NULL, VAR_NAMESPACE) != NULL)
1214                 build_canonical_line_spec (values.sals, copy, canonical);
1215             }
1216           return values;
1217         }
1218       else
1219         {
1220           if (funfirstline)
1221             error ("\"%s\" is not a function", copy);
1222           else if (SYMBOL_LINE (sym) != 0)
1223             {
1224               /* We know its line number.  */
1225               values.sals = (struct symtab_and_line *)
1226                 xmalloc (sizeof (struct symtab_and_line));
1227               values.nelts = 1;
1228               memset (&values.sals[0], 0, sizeof (values.sals[0]));
1229               values.sals[0].symtab = sym_symtab;
1230               values.sals[0].line = SYMBOL_LINE (sym);
1231               return values;
1232             }
1233           else
1234             /* This can happen if it is compiled with a compiler which doesn't
1235                put out line numbers for variables.  */
1236             /* FIXME: Shouldn't we just set .line and .symtab to zero
1237                and return?  For example, "info line foo" could print
1238                the address.  */
1239             error ("Line number not known for symbol \"%s\"", copy);
1240         }
1241     }
1242
1243   msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1244
1245 minimal_symbol_found:           /* We also jump here from the case for variables
1246                                    that begin with '$' */
1247
1248   if (msymbol != NULL)
1249     {
1250       values.sals = (struct symtab_and_line *)
1251         xmalloc (sizeof (struct symtab_and_line));
1252       values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1253                                           (struct sec *) 0, 0);
1254       values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1255       if (funfirstline)
1256         {
1257           values.sals[0].pc += FUNCTION_START_OFFSET;
1258           values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1259         }
1260       values.nelts = 1;
1261       return values;
1262     }
1263
1264   if (!have_full_symbols () &&
1265       !have_partial_symbols () && !have_minimal_symbols ())
1266     error ("No symbol table is loaded.  Use the \"file\" command.");
1267
1268   error ("Function \"%s\" not defined.", copy);
1269   return values;                /* for lint */
1270 }
This page took 0.103102 seconds and 4 git commands to generate.