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