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