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