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