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