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