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