]> Git Repo - binutils.git/blame - gdb/linespec.c
2003-01-14 David Carlton <[email protected]>
[binutils.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
a04257e6
DC
232 /* Destructor is handled by caller, don't add it to
233 the list. */
015a42b4 234 if (is_destructor_name (phys_name) != 0)
50641945
FN
235 continue;
236
237 sym_arr[i1] = lookup_symbol (phys_name,
238 NULL, VAR_NAMESPACE,
239 (int *) NULL,
240 (struct symtab **) NULL);
241 if (sym_arr[i1])
242 i1++;
243 else
244 {
245 /* This error message gets printed, but the method
246 still seems to be found
247 fputs_filtered("(Cannot find method ", gdb_stdout);
248 fprintf_symbol_filtered (gdb_stdout, phys_name,
249 language_cplus,
250 DMGL_PARAMS | DMGL_ANSI);
251 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
252 */
253 }
254 }
5c717440
DJ
255 else if (strncmp (class_name, name, name_len) == 0
256 && (class_name[name_len] == '\0'
257 || class_name[name_len] == '<'))
003d6d1d 258 {
a04257e6
DC
259 /* For GCC 3.x and stabs, constructors and destructors
260 have names like __base_ctor and __complete_dtor.
261 Check the physname for now if we're looking for a
262 constructor. */
003d6d1d
DJ
263 for (field_counter
264 = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
265 field_counter >= 0;
266 --field_counter)
267 {
268 struct fn_field *f;
269 char *phys_name;
270
271 f = TYPE_FN_FIELDLIST1 (t, method_counter);
272
a04257e6
DC
273 /* GCC 3.x will never produce stabs stub methods, so
274 we don't need to handle this case. */
003d6d1d
DJ
275 if (TYPE_FN_FIELD_STUB (f, field_counter))
276 continue;
277 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
278 if (! is_constructor_name (phys_name))
279 continue;
280
281 /* If this method is actually defined, include it in the
282 list. */
283 sym_arr[i1] = lookup_symbol (phys_name,
284 NULL, VAR_NAMESPACE,
285 (int *) NULL,
286 (struct symtab **) NULL);
287 if (sym_arr[i1])
288 i1++;
289 }
290 }
50641945
FN
291 }
292 }
293
294 /* Only search baseclasses if there is no match yet, since names in
295 derived classes override those in baseclasses.
296
297 FIXME: The above is not true; it is only true of member functions
298 if they have the same number of arguments (??? - section 13.1 of the
299 ARM says the function members are not in the same scope but doesn't
300 really spell out the rules in a way I understand. In any case, if
301 the number of arguments differ this is a case in which we can overload
302 rather than hiding without any problem, and gcc 2.4.5 does overload
303 rather than hiding in this case). */
304
305 if (i1 == 0)
306 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
307 i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
308
309 return i1;
310}
311
312/* Helper function for decode_line_1.
313 Build a canonical line spec in CANONICAL if it is non-NULL and if
314 the SAL has a symtab.
315 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
316 If SYMNAME is NULL the line number from SAL is used and the canonical
317 line spec is `filename:linenum'. */
318
319static void
320build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
321 char ***canonical)
322{
323 char **canonical_arr;
324 char *canonical_name;
325 char *filename;
326 struct symtab *s = sal->symtab;
327
328 if (s == (struct symtab *) NULL
329 || s->filename == (char *) NULL
330 || canonical == (char ***) NULL)
331 return;
332
333 canonical_arr = (char **) xmalloc (sizeof (char *));
334 *canonical = canonical_arr;
335
336 filename = s->filename;
337 if (symname != NULL)
338 {
339 canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
340 sprintf (canonical_name, "%s:%s", filename, symname);
341 }
342 else
343 {
344 canonical_name = xmalloc (strlen (filename) + 30);
345 sprintf (canonical_name, "%s:%d", filename, sal->line);
346 }
347 canonical_arr[0] = canonical_name;
348}
349
350
351
352/* Find an instance of the character C in the string S that is outside
353 of all parenthesis pairs, single-quoted strings, and double-quoted
8120c9d5
EZ
354 strings. Also, ignore the char within a template name, like a ','
355 within foo<int, int>. */
356
50641945
FN
357static char *
358find_toplevel_char (char *s, char c)
359{
360 int quoted = 0; /* zero if we're not in quotes;
361 '"' if we're in a double-quoted string;
362 '\'' if we're in a single-quoted string. */
a04257e6 363 int depth = 0; /* Number of unclosed parens we've seen. */
50641945
FN
364 char *scan;
365
366 for (scan = s; *scan; scan++)
367 {
368 if (quoted)
369 {
370 if (*scan == quoted)
371 quoted = 0;
372 else if (*scan == '\\' && *(scan + 1))
373 scan++;
374 }
375 else if (*scan == c && ! quoted && depth == 0)
376 return scan;
377 else if (*scan == '"' || *scan == '\'')
378 quoted = *scan;
8120c9d5 379 else if (*scan == '(' || *scan == '<')
50641945 380 depth++;
8120c9d5 381 else if ((*scan == ')' || *scan == '>') && depth > 0)
50641945
FN
382 depth--;
383 }
384
385 return 0;
386}
387
388/* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
389 operate on (ask user if necessary).
390 If CANONICAL is non-NULL return a corresponding array of mangled names
391 as canonical line specs there. */
392
393static struct symtabs_and_lines
394decode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
395 char ***canonical)
396{
397 struct symtabs_and_lines values, return_values;
398 char *args, *arg1;
399 int i;
400 char *prompt;
401 char *symname;
402 struct cleanup *old_chain;
403 char **canonical_arr = (char **) NULL;
404
405 values.sals = (struct symtab_and_line *)
406 alloca (nelts * sizeof (struct symtab_and_line));
407 return_values.sals = (struct symtab_and_line *)
408 xmalloc (nelts * sizeof (struct symtab_and_line));
b8c9b27d 409 old_chain = make_cleanup (xfree, return_values.sals);
50641945
FN
410
411 if (canonical)
412 {
413 canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
b8c9b27d 414 make_cleanup (xfree, canonical_arr);
50641945
FN
415 memset (canonical_arr, 0, nelts * sizeof (char *));
416 *canonical = canonical_arr;
417 }
418
419 i = 0;
420 printf_unfiltered ("[0] cancel\n[1] all\n");
421 while (i < nelts)
422 {
a04257e6 423 init_sal (&return_values.sals[i]); /* Initialize to zeroes. */
fe39c653 424 init_sal (&values.sals[i]);
50641945
FN
425 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
426 {
427 values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
428 printf_unfiltered ("[%d] %s at %s:%d\n",
429 (i + 2),
430 SYMBOL_SOURCE_NAME (sym_arr[i]),
431 values.sals[i].symtab->filename,
432 values.sals[i].line);
433 }
434 else
435 printf_unfiltered ("?HERE\n");
436 i++;
437 }
438
439 if ((prompt = getenv ("PS2")) == NULL)
440 {
441 prompt = "> ";
442 }
443 args = command_line_input (prompt, 0, "overload-choice");
444
445 if (args == 0 || *args == 0)
446 error_no_arg ("one or more choice numbers");
447
448 i = 0;
449 while (*args)
450 {
451 int num;
452
453 arg1 = args;
454 while (*arg1 >= '0' && *arg1 <= '9')
455 arg1++;
456 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
457 error ("Arguments must be choice numbers.");
458
459 num = atoi (args);
460
461 if (num == 0)
462 error ("canceled");
463 else if (num == 1)
464 {
465 if (canonical_arr)
466 {
467 for (i = 0; i < nelts; i++)
468 {
469 if (canonical_arr[i] == NULL)
470 {
471 symname = SYMBOL_NAME (sym_arr[i]);
472 canonical_arr[i] = savestring (symname, strlen (symname));
473 }
474 }
475 }
476 memcpy (return_values.sals, values.sals,
477 (nelts * sizeof (struct symtab_and_line)));
478 return_values.nelts = nelts;
479 discard_cleanups (old_chain);
480 return return_values;
481 }
482
483 if (num >= nelts + 2)
484 {
485 printf_unfiltered ("No choice number %d.\n", num);
486 }
487 else
488 {
489 num -= 2;
490 if (values.sals[num].pc)
491 {
492 if (canonical_arr)
493 {
494 symname = SYMBOL_NAME (sym_arr[num]);
b8c9b27d 495 make_cleanup (xfree, symname);
50641945
FN
496 canonical_arr[i] = savestring (symname, strlen (symname));
497 }
498 return_values.sals[i++] = values.sals[num];
499 values.sals[num].pc = 0;
500 }
501 else
502 {
503 printf_unfiltered ("duplicate request for %d ignored.\n", num);
504 }
505 }
506
507 args = arg1;
508 while (*args == ' ' || *args == '\t')
509 args++;
510 }
511 return_values.nelts = i;
512 discard_cleanups (old_chain);
513 return return_values;
514}
515\f
516/* The parser of linespec itself. */
517
518/* Parse a string that specifies a line number.
519 Pass the address of a char * variable; that variable will be
520 advanced over the characters actually parsed.
521
522 The string can be:
523
524 LINENUM -- that line number in current file. PC returned is 0.
525 FILE:LINENUM -- that line in that file. PC returned is 0.
526 FUNCTION -- line number of openbrace of that function.
527 PC returned is the start of the function.
528 VARIABLE -- line number of definition of that variable.
529 PC returned is 0.
530 FILE:FUNCTION -- likewise, but prefer functions in that file.
531 *EXPR -- line in which address EXPR appears.
532
533 This may all be followed by an "if EXPR", which we ignore.
534
535 FUNCTION may be an undebuggable function found in minimal symbol table.
536
537 If the argument FUNFIRSTLINE is nonzero, we want the first line
538 of real code inside a function when a function is specified, and it is
539 not OK to specify a variable or type to get its line number.
540
541 DEFAULT_SYMTAB specifies the file to use if none is specified.
542 It defaults to current_source_symtab.
543 DEFAULT_LINE specifies the line number to use for relative
544 line numbers (that start with signs). Defaults to current_source_line.
545 If CANONICAL is non-NULL, store an array of strings containing the canonical
546 line specs there if necessary. Currently overloaded member functions and
547 line numbers or static functions without a filename yield a canonical
548 line spec. The array and the line spec strings are allocated on the heap,
549 it is the callers responsibility to free them.
550
551 Note that it is possible to return zero for the symtab
552 if no file is validly specified. Callers must check that.
553 Also, the line number returned may be invalid. */
554
555/* We allow single quotes in various places. This is a hideous
556 kludge, which exists because the completer can't yet deal with the
557 lack of single quotes. FIXME: write a linespec_completer which we
558 can use as appropriate instead of make_symbol_completion_list. */
559
560struct symtabs_and_lines
561decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
562 int default_line, char ***canonical)
563{
f3c39e76 564 char *p;
614b3b14 565 char *q;
88d262ca
DC
566 /* If a file name is specified, this is its symtab. */
567 struct symtab *file_symtab = NULL;
50641945 568
50641945 569 char *copy;
636b1a6d
DC
570 /* This is NULL if there are no parens in *ARGPTR, or a pointer to
571 the closing parenthesis if there are parens. */
572 char *paren_pointer;
573 /* This says whether or not something in *ARGPTR is quoted with
574 completer_quotes (i.e. with single quotes). */
50641945 575 int is_quoted;
0960f083 576 /* Is part of *ARGPTR is enclosed in double quotes? */
50641945 577 int is_quote_enclosed;
50641945 578 char *saved_arg = *argptr;
50641945 579
50641945
FN
580 /* Defaults have defaults. */
581
44fe14ab
DC
582 initialize_defaults (&default_symtab, &default_line);
583
a04257e6 584 /* See if arg is *PC. */
50641945
FN
585
586 if (**argptr == '*')
44fe14ab 587 return decode_indirect (argptr);
50641945 588
a04257e6
DC
589 /* Set various flags. 'paren_pointer' is important for overload
590 checking, where we allow things like:
591 (gdb) break c::f(int)
592 */
50641945 593
636b1a6d 594 set_flags (*argptr, &is_quoted, &paren_pointer);
50641945 595
0960f083
DC
596 /* Check to see if it's a multipart linespec (with colons or
597 periods). */
50641945 598
0960f083 599 /* Locate the end of the first half of the linespec. */
50641945 600
0960f083 601 p = locate_first_half (argptr, &is_quote_enclosed);
50641945 602
0960f083 603 /* Does it look like there actually were two parts? */
50641945 604
636b1a6d 605 if ((p[0] == ':' || p[0] == '.') && paren_pointer == NULL)
50641945 606 {
50641945
FN
607 if (is_quoted)
608 *argptr = *argptr + 1;
f3c39e76
DC
609
610 /* Is it a C++ or Java compound data structure? */
611
50641945 612 if (p[0] == '.' || p[1] == ':')
614b3b14
DC
613 return decode_compound (argptr, funfirstline, canonical,
614 saved_arg, p);
50641945 615
f3c39e76
DC
616 /* No, the first part is a filename; set s to be that file's
617 symtab. Also, move argptr past the filename. */
50641945 618
88d262ca 619 file_symtab = symtab_from_filename (argptr, p, is_quote_enclosed);
50641945
FN
620 }
621#if 0
622 /* No one really seems to know why this was added. It certainly
623 breaks the command line, though, whenever the passed
624 name is of the form ClassName::Method. This bit of code
625 singles out the class name, and if funfirstline is set (for
626 example, you are setting a breakpoint at this function),
627 you get an error. This did not occur with earlier
628 verions, so I am ifdef'ing this out. 3/29/99 */
629 else
630 {
631 /* Check if what we have till now is a symbol name */
632
633 /* We may be looking at a template instantiation such
634 as "foo<int>". Check here whether we know about it,
635 instead of falling through to the code below which
636 handles ordinary function names, because that code
637 doesn't like seeing '<' and '>' in a name -- the
638 skip_quoted call doesn't go past them. So see if we
639 can figure it out right now. */
640
641 copy = (char *) alloca (p - *argptr + 1);
642 memcpy (copy, *argptr, p - *argptr);
643 copy[p - *argptr] = '\000';
644 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
645 if (sym)
646 {
50641945 647 *argptr = (*p == '\'') ? p + 1 : p;
413dad4d
DC
648 return symbol_found (funfirstline, canonical, copy, sym,
649 NULL, sym_symtab);
50641945
FN
650 }
651 /* Otherwise fall out from here and go to file/line spec
652 processing, etc. */
653 }
654#endif
655
656 /* S is specified file's symtab, or 0 if no file specified.
657 arg no longer contains the file name. */
658
a04257e6 659 /* Check whether arg is all digits (and sign). */
50641945
FN
660
661 q = *argptr;
662 if (*q == '-' || *q == '+')
663 q++;
664 while (*q >= '0' && *q <= '9')
665 q++;
666
667 if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
84fba31b
DC
668 /* We found a token consisting of all digits -- at least one digit. */
669 return decode_all_digits (argptr, default_symtab, default_line,
88d262ca 670 canonical, file_symtab, q);
50641945
FN
671
672 /* Arg token is not digits => try it as a variable name
673 Find the next token (everything up to end or next whitespace). */
674
a04257e6
DC
675 if (**argptr == '$') /* May be a convenience variable. */
676 /* One or two $ chars possible. */
677 p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));
50641945
FN
678 else if (is_quoted)
679 {
680 p = skip_quoted (*argptr);
681 if (p[-1] != '\'')
682 error ("Unmatched single quote.");
683 }
636b1a6d 684 else if (paren_pointer != NULL)
50641945 685 {
636b1a6d 686 p = paren_pointer + 1;
50641945
FN
687 }
688 else
689 {
690 p = skip_quoted (*argptr);
691 }
692
50641945
FN
693 copy = (char *) alloca (p - *argptr + 1);
694 memcpy (copy, *argptr, p - *argptr);
695 copy[p - *argptr] = '\0';
696 if (p != *argptr
697 && copy[0]
698 && copy[0] == copy[p - *argptr - 1]
c5f0f3d0 699 && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
50641945
FN
700 {
701 copy[p - *argptr - 1] = '\0';
702 copy++;
703 }
704 while (*p == ' ' || *p == '\t')
705 p++;
706 *argptr = p;
707
708 /* If it starts with $: may be a legitimate variable or routine name
709 (e.g. HP-UX millicode routines such as $$dyncall), or it may
a04257e6 710 be history value, or it may be a convenience variable. */
50641945
FN
711
712 if (*copy == '$')
14e91ac5 713 return decode_dollar (copy, funfirstline, default_symtab,
88d262ca 714 canonical, file_symtab);
50641945
FN
715
716 /* Look up that token as a variable.
717 If file specified, use that file's per-file block to start with. */
718
88d262ca 719 return decode_variable (copy, funfirstline, canonical, file_symtab);
413dad4d 720}
50641945 721
44fe14ab
DC
722\f
723
614b3b14
DC
724/* Now, more helper functions for decode_line_1. Some conventions
725 that these functions follow:
726
727 Decode_line_1 typically passes along some of its arguments or local
728 variables to the subfunctions. It passes the variables by
729 reference if they are modified by the subfunction, and by value
730 otherwise.
731
732 Some of the functions have side effects that don't arise from
733 variables that are passed by reference. In particular, if a
734 function is passed ARGPTR as an argument, it modifies what ARGPTR
735 points to; typically, it advances *ARGPTR past whatever substring
736 it has just looked at. (If it doesn't modify *ARGPTR, then the
737 function gets passed *ARGPTR instead, which is then called ARG: see
738 set_flags, for example.) Also, functions that return a struct
739 symtabs_and_lines may modify CANONICAL, as in the description of
740 decode_line_1.
741
742 If a function returns a struct symtabs_and_lines, then that struct
743 will immediately make its way up the call chain to be returned by
744 decode_line_1. In particular, all of the functions decode_XXX
745 calculate the appropriate struct symtabs_and_lines, under the
746 assumption that their argument is of the form XXX. */
44fe14ab
DC
747
748/* First, some functions to initialize stuff at the beggining of the
749 function. */
750
751static void
752initialize_defaults (struct symtab **default_symtab, int *default_line)
753{
754 if (*default_symtab == 0)
755 {
756 /* Use whatever we have for the default source line. We don't use
757 get_current_or_default_symtab_and_line as it can recurse and call
758 us back! */
759 struct symtab_and_line cursal =
760 get_current_source_symtab_and_line ();
761
762 *default_symtab = cursal.symtab;
763 *default_line = cursal.line;
764 }
765}
766
636b1a6d
DC
767static void
768set_flags (char *arg, int *is_quoted, char **paren_pointer)
769{
770 char *ii;
771 int has_if = 0;
772
773 /* 'has_if' is for the syntax:
a04257e6
DC
774 (gdb) break foo if (a==b)
775 */
636b1a6d
DC
776 if ((ii = strstr (arg, " if ")) != NULL ||
777 (ii = strstr (arg, "\tif ")) != NULL ||
778 (ii = strstr (arg, " if\t")) != NULL ||
779 (ii = strstr (arg, "\tif\t")) != NULL ||
780 (ii = strstr (arg, " if(")) != NULL ||
781 (ii = strstr (arg, "\tif( ")) != NULL)
782 has_if = 1;
a04257e6
DC
783 /* Temporarily zap out "if (condition)" to not confuse the
784 parenthesis-checking code below. This is undone below. Do not
785 change ii!! */
636b1a6d
DC
786 if (has_if)
787 {
788 *ii = '\0';
789 }
790
791 *is_quoted = (*arg
792 && strchr (get_gdb_completer_quote_characters (),
793 *arg) != NULL);
794
795 *paren_pointer = strchr (arg, '(');
796 if (*paren_pointer != NULL)
797 *paren_pointer = strrchr (*paren_pointer, ')');
798
a04257e6
DC
799 /* Now that we're safely past the paren_pointer check, put back " if
800 (condition)" so outer layers can see it. */
636b1a6d
DC
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
a04257e6
DC
853 /* Temporarily zap out second half to not confuse the code below.
854 This is undone below. Do not change ii!! */
0960f083
DC
855 if (has_comma)
856 {
857 *ii = '\0';
858 }
859
a04257e6
DC
860 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION. May also be
861 CLASS::MEMBER, or NAMESPACE::NAME. Look for ':', but ignore
862 inside of <>. */
0960f083
DC
863
864 p = *argptr;
865 if (p[0] == '"')
866 {
867 *is_quote_enclosed = 1;
868 (*argptr)++;
869 p++;
870 }
871 else
872 *is_quote_enclosed = 0;
873 for (; *p; p++)
874 {
875 if (p[0] == '<')
876 {
877 char *temp_end = find_template_name_end (p);
878 if (!temp_end)
879 error ("malformed template specification in command");
880 p = temp_end;
881 }
a04257e6
DC
882 /* Check for the end of the first half of the linespec. End of
883 line, a tab, a double colon or the last single colon, or a
884 space. But if enclosed in double quotes we do not break on
885 enclosed spaces. */
0960f083
DC
886 if (!*p
887 || p[0] == '\t'
888 || ((p[0] == ':')
889 && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
890 || ((p[0] == ' ') && !*is_quote_enclosed))
891 break;
a04257e6 892 if (p[0] == '.' && strchr (p, ':') == NULL)
0960f083 893 {
a04257e6
DC
894 /* Java qualified method. Find the *last* '.', since the
895 others are package qualifiers. */
0960f083
DC
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
a04257e6 907 /* If the closing double quote was left at the end, remove it. */
0960f083
DC
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
a04257e6
DC
915 /* Now that we've safely parsed the first half, put back ',' so
916 outer layers can see it. */
0960f083
DC
917 if (has_comma)
918 *ii = ',';
919
920 return p;
921}
922
923\f
924
614b3b14
DC
925/* This handles C++ and Java compound data structures. P should point
926 at the first component separator, i.e. double-colon or period. */
927
928static struct symtabs_and_lines
929decode_compound (char **argptr, int funfirstline, char ***canonical,
930 char *saved_arg, char *p)
931{
932 struct symtabs_and_lines values;
933 char *p1, *p2;
934#if 0
935 char *q, *q1;
936#endif
937 char *saved_arg2 = *argptr;
938 char *temp_end;
939 struct symbol *sym;
940 /* The symtab that SYM was found in. */
941 struct symtab *sym_symtab;
942 char *copy;
943 struct symbol *sym_class;
944 int i1;
945 struct symbol **sym_arr;
946 struct type *t;
947
948 /* First check for "global" namespace specification,
a04257e6
DC
949 of the form "::foo". If found, skip over the colons
950 and jump to normal symbol processing. */
614b3b14
DC
951 if (p[0] == ':'
952 && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
953 saved_arg2 += 2;
954
955 /* We have what looks like a class or namespace
956 scope specification (A::B), possibly with many
957 levels of namespaces or classes (A::B::C::D).
958
959 Some versions of the HP ANSI C++ compiler (as also possibly
960 other compilers) generate class/function/member names with
961 embedded double-colons if they are inside namespaces. To
962 handle this, we loop a few times, considering larger and
963 larger prefixes of the string as though they were single
964 symbols. So, if the initially supplied string is
965 A::B::C::D::foo, we have to look up "A", then "A::B",
966 then "A::B::C", then "A::B::C::D", and finally
967 "A::B::C::D::foo" as single, monolithic symbols, because
968 A, B, C or D may be namespaces.
969
970 Note that namespaces can nest only inside other
971 namespaces, and not inside classes. So we need only
972 consider *prefixes* of the string; there is no need to look up
a04257e6 973 "B::C" separately as a symbol in the previous example. */
614b3b14 974
a04257e6 975 p2 = p; /* Save for restart. */
614b3b14
DC
976 while (1)
977 {
978 /* Extract the class name. */
979 p1 = p;
980 while (p != *argptr && p[-1] == ' ')
981 --p;
982 copy = (char *) alloca (p - *argptr + 1);
983 memcpy (copy, *argptr, p - *argptr);
984 copy[p - *argptr] = 0;
985
986 /* Discard the class name from the arg. */
987 p = p1 + (p1[0] == ':' ? 2 : 1);
988 while (*p == ' ' || *p == '\t')
989 p++;
990 *argptr = p;
991
992 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
993 (struct symtab **) NULL);
994
995 if (sym_class &&
996 (t = check_typedef (SYMBOL_TYPE (sym_class)),
997 (TYPE_CODE (t) == TYPE_CODE_STRUCT
998 || TYPE_CODE (t) == TYPE_CODE_UNION)))
999 {
a04257e6
DC
1000 /* Arg token is not digits => try it as a function name.
1001 Find the next token (everything up to end or next
1002 blank). */
614b3b14
DC
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
a04257e6 1046 /* No line number may be specified. */
614b3b14
DC
1047 while (*p == ' ' || *p == '\t')
1048 p++;
1049 *argptr = p;
1050
1051 sym = 0;
a04257e6 1052 i1 = 0; /* Counter for the symbol array. */
614b3b14
DC
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
a04257e6
DC
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. */
614b3b14
DC
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] == ':'))
a04257e6 1136 break; /* Found double-colon. */
614b3b14
DC
1137 else
1138 p++;
1139 }
1140
1141 if (*p != ':')
a04257e6 1142 break; /* Out of the while (1). */
614b3b14 1143
a04257e6
DC
1144 p2 = p; /* Save restart for next time around. */
1145 *argptr = saved_arg2; /* Restore argptr. */
614b3b14
DC
1146 } /* while (1) */
1147
a04257e6
DC
1148 /* Last chance attempt -- check entire name as a symbol. Use "copy"
1149 in preparation for jumping out of this block, to be consistent
1150 with usage following the jump target. */
614b3b14
DC
1151 copy = (char *) alloca (p - saved_arg2 + 1);
1152 memcpy (copy, saved_arg2, p - saved_arg2);
a04257e6
DC
1153 /* Note: if is_quoted should be true, we snuff out quote here
1154 anyway. */
614b3b14 1155 copy[p - saved_arg2] = '\000';
a04257e6 1156 /* Set argptr to skip over the name. */
614b3b14
DC
1157 *argptr = (*p == '\'') ? p + 1 : p;
1158 /* Look up entire name */
1159 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
1160 if (sym)
1161 return symbol_found (funfirstline, canonical, copy, sym,
1162 NULL, sym_symtab);
1163
a04257e6
DC
1164 /* Couldn't find any interpretation as classes/namespaces, so give
1165 up. The quotes are important if copy is empty. */
614b3b14
DC
1166 cplusplus_error (saved_arg,
1167 "Can't find member of namespace, class, struct, or union named \"%s\"\n",
1168 copy);
1169}
1170
f3c39e76
DC
1171\f
1172
1173/* Return the symtab associated to the filename given by the substring
1174 of *ARGPTR ending at P, and advance ARGPTR past that filename. */
1175
1176static struct symtab *
1177symtab_from_filename (char **argptr, char *p, int is_quote_enclosed)
1178{
1179 char *p1;
1180 char *copy;
94cd26f8 1181 struct symtab *file_symtab;
f3c39e76
DC
1182
1183 p1 = p;
1184 while (p != *argptr && p[-1] == ' ')
1185 --p;
1186 if ((*p == '"') && is_quote_enclosed)
1187 --p;
1188 copy = (char *) alloca (p - *argptr + 1);
1189 memcpy (copy, *argptr, p - *argptr);
a04257e6 1190 /* It may have the ending quote right after the file name. */
f3c39e76
DC
1191 if (is_quote_enclosed && copy[p - *argptr - 1] == '"')
1192 copy[p - *argptr - 1] = 0;
1193 else
1194 copy[p - *argptr] = 0;
1195
1196 /* Find that file's data. */
94cd26f8
DC
1197 file_symtab = lookup_symtab (copy);
1198 if (file_symtab == 0)
f3c39e76
DC
1199 {
1200 if (!have_full_symbols () && !have_partial_symbols ())
1201 error ("No symbol table is loaded. Use the \"file\" command.");
1202 error ("No source file named %s.", copy);
1203 }
1204
1205 /* Discard the file name from the arg. */
1206 p = p1 + 1;
1207 while (*p == ' ' || *p == '\t')
1208 p++;
1209 *argptr = p;
1210
94cd26f8 1211 return file_symtab;
f3c39e76
DC
1212}
1213
84fba31b
DC
1214\f
1215
1216/* This decodes a line where the argument is all digits (possibly
1217 preceded by a sign). Q should point to the end of those digits;
1218 the other arguments are as usual. */
1219
1220static struct symtabs_and_lines
1221decode_all_digits (char **argptr, struct symtab *default_symtab,
1222 int default_line, char ***canonical,
88d262ca 1223 struct symtab *file_symtab, char *q)
84fba31b
DC
1224
1225{
1226 struct symtabs_and_lines values;
1227 struct symtab_and_line val;
1228
1229 enum sign
1230 {
1231 none, plus, minus
1232 }
1233 sign = none;
1234
1235 /* We might need a canonical line spec if no file was specified. */
88d262ca 1236 int need_canonical = (file_symtab == 0) ? 1 : 0;
84fba31b
DC
1237
1238 init_sal (&val);
1239
1240 /* This is where we need to make sure that we have good defaults.
1241 We must guarantee that this section of code is never executed
1242 when we are called with just a function name, since
1243 set_default_source_symtab_and_line uses
a04257e6 1244 select_source_symtab that calls us with such an argument. */
84fba31b 1245
88d262ca 1246 if (file_symtab == 0 && default_symtab == 0)
84fba31b 1247 {
a04257e6 1248 /* Make sure we have at least a default source file. */
84fba31b
DC
1249 set_default_source_symtab_and_line ();
1250 initialize_defaults (&default_symtab, &default_line);
1251 }
1252
1253 if (**argptr == '+')
1254 sign = plus, (*argptr)++;
1255 else if (**argptr == '-')
1256 sign = minus, (*argptr)++;
1257 val.line = atoi (*argptr);
1258 switch (sign)
1259 {
1260 case plus:
1261 if (q == *argptr)
1262 val.line = 5;
88d262ca 1263 if (file_symtab == 0)
84fba31b
DC
1264 val.line = default_line + val.line;
1265 break;
1266 case minus:
1267 if (q == *argptr)
1268 val.line = 15;
88d262ca 1269 if (file_symtab == 0)
84fba31b
DC
1270 val.line = default_line - val.line;
1271 else
1272 val.line = 1;
1273 break;
1274 case none:
1275 break; /* No need to adjust val.line. */
1276 }
1277
1278 while (*q == ' ' || *q == '\t')
1279 q++;
1280 *argptr = q;
88d262ca
DC
1281 if (file_symtab == 0)
1282 file_symtab = default_symtab;
84fba31b
DC
1283
1284 /* It is possible that this source file has more than one symtab,
1285 and that the new line number specification has moved us from the
88d262ca
DC
1286 default (in file_symtab) to a new one. */
1287 val.symtab = find_line_symtab (file_symtab, val.line, NULL, NULL);
84fba31b 1288 if (val.symtab == 0)
88d262ca 1289 val.symtab = file_symtab;
84fba31b
DC
1290
1291 val.pc = 0;
1292 values.sals = (struct symtab_and_line *)
1293 xmalloc (sizeof (struct symtab_and_line));
1294 values.sals[0] = val;
1295 values.nelts = 1;
1296 if (need_canonical)
1297 build_canonical_line_spec (values.sals, NULL, canonical);
1298 return values;
1299}
f3c39e76 1300
614b3b14
DC
1301\f
1302
14e91ac5
DC
1303/* Decode a linespec starting with a dollar sign. */
1304
1305static struct symtabs_and_lines
1306decode_dollar (char *copy, int funfirstline, struct symtab *default_symtab,
88d262ca 1307 char ***canonical, struct symtab *file_symtab)
14e91ac5
DC
1308{
1309 struct value *valx;
1310 int index = 0;
1311 int need_canonical = 0;
1312 struct symtabs_and_lines values;
1313 struct symtab_and_line val;
1314 char *p;
1315 struct symbol *sym;
1316 /* The symtab that SYM was found in. */
1317 struct symtab *sym_symtab;
1318 struct minimal_symbol *msymbol;
1319
1320 p = (copy[1] == '$') ? copy + 2 : copy + 1;
1321 while (*p >= '0' && *p <= '9')
1322 p++;
a04257e6 1323 if (!*p) /* Reached end of token without hitting non-digit. */
14e91ac5 1324 {
a04257e6 1325 /* We have a value history reference. */
14e91ac5
DC
1326 sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1327 valx = access_value_history ((copy[1] == '$') ? -index : index);
1328 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1329 error ("History values used in line specs must have integer values.");
1330 }
1331 else
1332 {
1333 /* Not all digits -- may be user variable/function or a
a04257e6 1334 convenience variable. */
14e91ac5 1335
a04257e6 1336 /* Look up entire name as a symbol first. */
14e91ac5 1337 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
88d262ca 1338 file_symtab = (struct symtab *) 0;
14e91ac5
DC
1339 need_canonical = 1;
1340 /* Symbol was found --> jump to normal symbol processing. */
1341 if (sym)
1342 return symbol_found (funfirstline, canonical, copy, sym,
1343 NULL, sym_symtab);
1344
a04257e6 1345 /* If symbol was not found, look in minimal symbol tables. */
14e91ac5 1346 msymbol = lookup_minimal_symbol (copy, NULL, NULL);
a04257e6 1347 /* Min symbol was found --> jump to minsym processing. */
14e91ac5
DC
1348 if (msymbol)
1349 return minsym_found (funfirstline, msymbol);
1350
a04257e6 1351 /* Not a user variable or function -- must be convenience variable. */
88d262ca 1352 need_canonical = (file_symtab == 0) ? 1 : 0;
14e91ac5
DC
1353 valx = value_of_internalvar (lookup_internalvar (copy + 1));
1354 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1355 error ("Convenience variables used in line specs must have integer values.");
1356 }
1357
1358 init_sal (&val);
1359
a04257e6 1360 /* Either history value or convenience value from above, in valx. */
88d262ca 1361 val.symtab = file_symtab ? file_symtab : default_symtab;
14e91ac5
DC
1362 val.line = value_as_long (valx);
1363 val.pc = 0;
1364
1365 values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1366 values.sals[0] = val;
1367 values.nelts = 1;
1368
1369 if (need_canonical)
1370 build_canonical_line_spec (values.sals, NULL, canonical);
1371
1372 return values;
1373}
1374
bca02a8a
DC
1375\f
1376
88d262ca 1377/* Decode a linespec that's a variable. If FILE_SYMTAB is non-NULL,
bca02a8a
DC
1378 look in that symtab's static variables first. */
1379
1380static struct symtabs_and_lines
1381decode_variable (char *copy, int funfirstline, char ***canonical,
88d262ca 1382 struct symtab *file_symtab)
bca02a8a
DC
1383{
1384 struct symbol *sym;
1385 /* The symtab that SYM was found in. */
1386 struct symtab *sym_symtab;
1387
1388 struct minimal_symbol *msymbol;
1389
1390 sym = lookup_symbol (copy,
88d262ca
DC
1391 (file_symtab
1392 ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab),
1393 STATIC_BLOCK)
bca02a8a
DC
1394 : get_selected_block (0)),
1395 VAR_NAMESPACE, 0, &sym_symtab);
1396
1397 if (sym != NULL)
88d262ca
DC
1398 return symbol_found (funfirstline, canonical, copy, sym,
1399 file_symtab, sym_symtab);
bca02a8a
DC
1400
1401 msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1402
1403 if (msymbol != NULL)
1404 return minsym_found (funfirstline, msymbol);
1405
1406 if (!have_full_symbols () &&
1407 !have_partial_symbols () && !have_minimal_symbols ())
1408 error ("No symbol table is loaded. Use the \"file\" command.");
1409
1410 error ("Function \"%s\" not defined.", copy);
1411}
1412
1413
14e91ac5
DC
1414\f
1415
413dad4d
DC
1416/* Now come some functions that are called from multiple places within
1417 decode_line_1. */
1418
1419/* We've found a symbol SYM to associate with our linespec; build a
1420 corresponding struct symtabs_and_lines. */
1421
1422static struct symtabs_and_lines
1423symbol_found (int funfirstline, char ***canonical, char *copy,
88d262ca 1424 struct symbol *sym, struct symtab *file_symtab,
413dad4d
DC
1425 struct symtab *sym_symtab)
1426{
1427 struct symtabs_and_lines values;
1428
1429 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 1430 {
413dad4d 1431 /* Arg is the name of a function */
50641945
FN
1432 values.sals = (struct symtab_and_line *)
1433 xmalloc (sizeof (struct symtab_and_line));
413dad4d
DC
1434 values.sals[0] = find_function_start_sal (sym, funfirstline);
1435 values.nelts = 1;
1436
1437 /* Don't use the SYMBOL_LINE; if used at all it points to
1438 the line containing the parameters or thereabouts, not
1439 the first line of code. */
1440
1441 /* We might need a canonical line spec if it is a static
1442 function. */
88d262ca 1443 if (file_symtab == 0)
50641945 1444 {
413dad4d
DC
1445 struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1446 struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1447 if (lookup_block_symbol (b, copy, NULL, VAR_NAMESPACE) != NULL)
1448 build_canonical_line_spec (values.sals, copy, canonical);
50641945 1449 }
50641945
FN
1450 return values;
1451 }
413dad4d
DC
1452 else
1453 {
1454 if (funfirstline)
1455 error ("\"%s\" is not a function", copy);
1456 else if (SYMBOL_LINE (sym) != 0)
1457 {
1458 /* We know its line number. */
1459 values.sals = (struct symtab_and_line *)
1460 xmalloc (sizeof (struct symtab_and_line));
1461 values.nelts = 1;
1462 memset (&values.sals[0], 0, sizeof (values.sals[0]));
1463 values.sals[0].symtab = sym_symtab;
1464 values.sals[0].line = SYMBOL_LINE (sym);
1465 return values;
1466 }
1467 else
1468 /* This can happen if it is compiled with a compiler which doesn't
1469 put out line numbers for variables. */
1470 /* FIXME: Shouldn't we just set .line and .symtab to zero
1471 and return? For example, "info line foo" could print
1472 the address. */
1473 error ("Line number not known for symbol \"%s\"", copy);
1474 }
1475}
50641945 1476
413dad4d
DC
1477/* We've found a minimal symbol MSYMBOL to associate with our
1478 linespec; build a corresponding struct symtabs_and_lines. */
50641945 1479
413dad4d
DC
1480static struct symtabs_and_lines
1481minsym_found (int funfirstline, struct minimal_symbol *msymbol)
1482{
1483 struct symtabs_and_lines values;
1484
1485 values.sals = (struct symtab_and_line *)
1486 xmalloc (sizeof (struct symtab_and_line));
1487 values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1488 (struct sec *) 0, 0);
1489 values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1490 if (funfirstline)
1491 {
1492 values.sals[0].pc += FUNCTION_START_OFFSET;
1493 values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1494 }
1495 values.nelts = 1;
1496 return values;
50641945 1497}
This page took 0.654079 seconds and 4 git commands to generate.