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