the "ambiguous linespec" series
[deliverable/binutils-gdb.git] / gdb / linespec.c
CommitLineData
50641945 1/* Parser for linespec for the GNU debugger, GDB.
05ff989b 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
0fb0cc75 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
7b6bb8da 5 2009, 2010, 2011 Free Software Foundation, Inc.
50641945
FN
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
50641945
FN
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
50641945
FN
21
22#include "defs.h"
23#include "symtab.h"
c5f0f3d0
FN
24#include "frame.h"
25#include "command.h"
50641945
FN
26#include "symfile.h"
27#include "objfiles.h"
0378c332 28#include "source.h"
50641945 29#include "demangle.h"
c5f0f3d0
FN
30#include "value.h"
31#include "completer.h"
015a42b4 32#include "cp-abi.h"
12907978 33#include "cp-support.h"
c38da1af 34#include "parser-defs.h"
fe898f56 35#include "block.h"
d2630e69 36#include "objc-lang.h"
b9362cc7 37#include "linespec.h"
05ff989b 38#include "exceptions.h"
53c5240f 39#include "language.h"
dc67126b
NR
40#include "interps.h"
41#include "mi/mi-cmds.h"
bccdca4a 42#include "target.h"
94af9270 43#include "arch-utils.h"
c00f8484
KS
44#include <ctype.h>
45#include "cli/cli-utils.h"
731971ed 46#include "filenames.h"
f8eba3c6
TT
47#include "ada-lang.h"
48
49typedef struct symtab *symtab_p;
50DEF_VEC_P (symtab_p);
51
52typedef struct symbol *symbolp;
53DEF_VEC_P (symbolp);
54
55typedef struct type *typep;
56DEF_VEC_P (typep);
57
58/* An address entry is used to ensure that any given location is only
59 added to the result a single time. It holds an address and the
60 program space from which the address came. */
61
62struct address_entry
63{
64 struct program_space *pspace;
65 CORE_ADDR addr;
66};
67
68/* An instance of this is used to keep all state while linespec
69 operates. This instance is passed around as a 'this' pointer to
70 the various implementation methods. */
71
72struct linespec_state
73{
74 /* The program space as seen when the module was entered. */
75 struct program_space *program_space;
76
77 /* The default symtab to use, if no other symtab is specified. */
78 struct symtab *default_symtab;
79
80 /* The default line to use. */
81 int default_line;
82
83 /* If the linespec started with "FILE:", this holds all the matching
84 symtabs. Otherwise, it will hold a single NULL entry, meaning
85 that the default symtab should be used. */
86 VEC (symtab_p) *file_symtabs;
87
88 /* If the linespec started with "FILE:", this holds an xmalloc'd
89 copy of "FILE". */
90 char *user_filename;
91
92 /* If the linespec is "FUNCTION:LABEL", this holds an xmalloc'd copy
93 of "FUNCTION". */
94 char *user_function;
95
96 /* The 'funfirstline' value that was passed in to decode_line_1 or
97 decode_line_full. */
98 int funfirstline;
99
100 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
101 int list_mode;
102
103 /* The 'canonical' value passed to decode_line_full, or NULL. */
104 struct linespec_result *canonical;
105
106 /* Canonical strings that mirror the symtabs_and_lines result. */
107 char **canonical_names;
108
109 /* This is a set of address_entry objects which is used to prevent
110 duplicate symbols from being entered into the result. */
111 htab_t addr_set;
112};
113
114/* This is a helper object that is used when collecting symbols into a
115 result. */
116
117struct collect_info
118{
119 /* The linespec object in use. */
120 struct linespec_state *state;
121
122 /* The result being accumulated. */
123 struct symtabs_and_lines result;
124
125 /* The current objfile; used only by the minimal symbol code. */
126 struct objfile *objfile;
127};
50641945 128
1777feb0 129/* Prototypes for local functions. */
50641945 130
44fe14ab
DC
131static void initialize_defaults (struct symtab **default_symtab,
132 int *default_line);
133
f8eba3c6
TT
134static struct symtabs_and_lines decode_indirect (struct linespec_state *self,
135 char **argptr);
44fe14ab 136
0960f083
DC
137static char *locate_first_half (char **argptr, int *is_quote_enclosed);
138
f8eba3c6
TT
139static struct symtabs_and_lines decode_objc (struct linespec_state *self,
140 char **argptr);
d2630e69 141
f8eba3c6
TT
142static struct symtabs_and_lines decode_compound (struct linespec_state *self,
143 char **argptr,
614b3b14 144 char *saved_arg,
58438ac1 145 char *p);
614b3b14 146
f8eba3c6
TT
147static VEC (symbolp) *lookup_prefix_sym (char **argptr, char *p,
148 VEC (symtab_p) *,
149 char **);
93d91629 150
f8eba3c6 151static struct symtabs_and_lines find_method (struct linespec_state *self,
4224873a
DC
152 char *saved_arg,
153 char *copy,
f8eba3c6
TT
154 const char *class_name,
155 VEC (symbolp) *sym_classes);
4224873a 156
c25c4a8b
JK
157static void cplusplus_error (const char *name, const char *fmt, ...)
158 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
50641945 159
f8eba3c6 160static char *find_toplevel_char (char *s, char c);
50641945 161
f8eba3c6 162static int is_objc_method_format (const char *s);
50641945 163
f8eba3c6
TT
164static VEC (symtab_p) *symtabs_from_filename (char **argptr,
165 char *p, int is_quote_enclosed,
166 char **user_filename);
aee8d8ba 167
f8eba3c6
TT
168static VEC (symbolp) *find_function_symbols (char **argptr, char *p,
169 int is_quote_enclosed,
170 char **user_function);
aee8d8ba 171
f8eba3c6
TT
172static struct symtabs_and_lines decode_all_digits (struct linespec_state *self,
173 char **argptr,
174 char *q);
50641945 175
f8eba3c6
TT
176static struct symtabs_and_lines decode_dollar (struct linespec_state *self,
177 char *copy);
50641945 178
f8eba3c6
TT
179static int decode_label (struct linespec_state *self,
180 VEC (symbolp) *function_symbols,
181 char *copy,
182 struct symtabs_and_lines *result);
183
184static struct symtabs_and_lines decode_variable (struct linespec_state *self,
185 char *copy);
889f28e2 186
f8eba3c6
TT
187static int symbol_to_sal (struct symtab_and_line *result,
188 int funfirstline, struct symbol *sym);
50641945 189
f8eba3c6
TT
190static void add_matching_symbols_to_info (const char *name,
191 struct collect_info *info,
192 struct program_space *pspace);
f3c39e76 193
f8eba3c6
TT
194static void add_all_symbol_names_from_pspace (struct collect_info *info,
195 struct program_space *pspace,
196 VEC (const_char_ptr) *names);
9ef07c8c 197
f8eba3c6 198/* Helper functions. */
84fba31b 199
f8eba3c6 200/* Add SAL to SALS. */
14e91ac5 201
f8eba3c6
TT
202static void
203add_sal_to_sals_basic (struct symtabs_and_lines *sals,
204 struct symtab_and_line *sal)
205{
206 ++sals->nelts;
207 sals->sals = xrealloc (sals->sals, sals->nelts * sizeof (sals->sals[0]));
208 sals->sals[sals->nelts - 1] = *sal;
209}
0f5238ed 210
f8eba3c6
TT
211/* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
212 the new sal, if needed. If not NULL, SYMNAME is the name of the
213 symbol to use when constructing the new canonical name. */
bca02a8a 214
f8eba3c6
TT
215static void
216add_sal_to_sals (struct linespec_state *self,
217 struct symtabs_and_lines *sals,
218 struct symtab_and_line *sal,
219 const char *symname)
220{
221 add_sal_to_sals_basic (sals, sal);
413dad4d 222
f8eba3c6
TT
223 if (self->canonical)
224 {
225 char *canonical_name = NULL;
413dad4d 226
f8eba3c6
TT
227 self->canonical_names = xrealloc (self->canonical_names,
228 sals->nelts * sizeof (char *));
229 if (sal->symtab && sal->symtab->filename)
230 {
231 char *filename = sal->symtab->filename;
232
233 /* Note that the filter doesn't have to be a valid linespec
234 input. We only apply the ":LINE" treatment to Ada for
235 the time being. */
236 if (symname != NULL && sal->line != 0
237 && current_language->la_language == language_ada)
238 canonical_name = xstrprintf ("%s:%s:%d", filename, symname,
239 sal->line);
240 else if (symname != NULL)
241 canonical_name = xstrprintf ("%s:%s", filename, symname);
242 else
243 canonical_name = xstrprintf ("%s:%d", filename, sal->line);
244 }
245
246 self->canonical_names[sals->nelts - 1] = canonical_name;
247 }
248}
249
250/* A hash function for address_entry. */
251
252static hashval_t
253hash_address_entry (const void *p)
254{
255 const struct address_entry *aep = p;
256
257 return iterative_hash_object (*aep, 0);
258}
259
260/* An equality function for address_entry. */
261
262static int
263eq_address_entry (const void *a, const void *b)
264{
265 const struct address_entry *aea = a;
266 const struct address_entry *aeb = b;
267
268 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
269}
270
271/* Check whether the address, represented by PSPACE and ADDR, is
272 already in the set. If so, return 0. Otherwise, add it and return
273 1. */
274
275static int
276maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
277{
278 struct address_entry e, *p;
279 void **slot;
280
281 e.pspace = pspace;
282 e.addr = addr;
283 slot = htab_find_slot (set, &e, INSERT);
284 if (*slot)
285 return 0;
286
287 p = XNEW (struct address_entry);
288 memcpy (p, &e, sizeof (struct address_entry));
289 *slot = p;
290
291 return 1;
292}
50641945 293
255e7dbf
AC
294/* Issue a helpful hint on using the command completion feature on
295 single quoted demangled C++ symbols as part of the completion
296 error. */
50641945 297
c25c4a8b 298static void
255e7dbf 299cplusplus_error (const char *name, const char *fmt, ...)
50641945 300{
255e7dbf 301 struct ui_file *tmp_stream;
f3a5f1de 302 char *message;
e0881a8e 303
255e7dbf
AC
304 tmp_stream = mem_fileopen ();
305 make_cleanup_ui_file_delete (tmp_stream);
306
307 {
308 va_list args;
e0881a8e 309
255e7dbf
AC
310 va_start (args, fmt);
311 vfprintf_unfiltered (tmp_stream, fmt, args);
312 va_end (args);
313 }
314
50641945
FN
315 while (*name == '\'')
316 name++;
255e7dbf
AC
317 fprintf_unfiltered (tmp_stream,
318 ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
319 "(Note leading single quote.)"),
320 name, name);
f3a5f1de 321
759ef836
PA
322 message = ui_file_xstrdup (tmp_stream, NULL);
323 make_cleanup (xfree, message);
324 throw_error (NOT_FOUND_ERROR, "%s", message);
50641945
FN
325}
326
f8eba3c6
TT
327/* A helper for iterate_over_all_matching_symtabs that is passed as a
328 callback to the expand_symtabs_matching method. */
50641945
FN
329
330static int
f8eba3c6
TT
331iterate_name_matcher (const struct language_defn *language,
332 const char *name, void *d)
50641945 333{
f8eba3c6 334 const char **dname = d;
50641945 335
f8eba3c6
TT
336 if (language->la_symbol_name_compare (name, *dname) == 0)
337 return 1;
338 return 0;
339}
340
341/* A helper that walks over all matching symtabs in all objfiles and
342 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
343 not NULL, then the search is restricted to just that program
344 space. */
345
346static void
347iterate_over_all_matching_symtabs (const char *name,
348 const domain_enum domain,
349 int (*callback) (struct symbol *, void *),
350 void *data,
351 struct program_space *search_pspace)
352{
353 struct objfile *objfile;
354 struct program_space *pspace;
355
356 ALL_PSPACES (pspace)
357 {
358 if (search_pspace != NULL && search_pspace != pspace)
359 continue;
360 if (pspace->executing_startup)
361 continue;
362
363 set_current_program_space (pspace);
50641945 364
f8eba3c6
TT
365 ALL_OBJFILES (objfile)
366 {
367 struct symtab *symtab;
368
369 if (objfile->sf)
370 objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
371 iterate_name_matcher,
372 ALL_DOMAIN,
373 &name);
374
375 ALL_OBJFILE_SYMTABS (objfile, symtab)
376 {
377 if (symtab->primary)
378 {
379 struct block *block;
50641945 380
f8eba3c6
TT
381 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
382 LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
383 }
384 }
385 }
386 }
50641945
FN
387}
388
e8eb7bc5
KS
389/* Returns the block to be used for symbol searches for the given SYMTAB,
390 which may be NULL. */
391
392static struct block *
393get_search_block (struct symtab *symtab)
394{
395 struct block *block;
396
397 if (symtab != NULL)
398 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
399 else
400 {
401 enum language save_language;
402
403 /* get_selected_block can change the current language when there is
404 no selected frame yet. */
405 save_language = current_language->la_language;
406 block = get_selected_block (0);
407 set_language (save_language);
408 }
409
410 return block;
411}
412
f8eba3c6
TT
413/* A helper for find_method. This finds all methods in type T which
414 match NAME. It adds resulting symbol names to RESULT_NAMES, and
415 adds T's direct superclasses to SUPERCLASSES. */
50641945 416
f8eba3c6
TT
417static void
418find_methods (struct type *t, const char *name,
419 VEC (const_char_ptr) **result_names,
420 VEC (typep) **superclasses)
50641945
FN
421{
422 int i1 = 0;
423 int ibase;
50641945 424 char *class_name = type_name_no_tag (t);
c00f8484
KS
425 char *canon;
426
50641945
FN
427 /* Ignore this class if it doesn't have a name. This is ugly, but
428 unless we figure out how to get the physname without the name of
429 the class, then the loop can't do any good. */
f8eba3c6 430 if (class_name)
50641945
FN
431 {
432 int method_counter;
5c717440 433 int name_len = strlen (name);
50641945 434
8bd1f2c6 435 CHECK_TYPEDEF (t);
50641945
FN
436
437 /* Loop over each method name. At this level, all overloads of a name
438 are counted as a single name. There is an inner loop which loops over
439 each overload. */
440
441 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
442 method_counter >= 0;
443 --method_counter)
444 {
50641945
FN
445 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
446 char dem_opname[64];
447
448 if (strncmp (method_name, "__", 2) == 0 ||
449 strncmp (method_name, "op", 2) == 0 ||
450 strncmp (method_name, "type", 4) == 0)
451 {
452 if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
453 method_name = dem_opname;
454 else if (cplus_demangle_opname (method_name, dem_opname, 0))
455 method_name = dem_opname;
456 }
457
f8eba3c6
TT
458 if (strcmp_iw (method_name, name) == 0)
459 {
460 int field_counter;
aee8d8ba 461
f8eba3c6
TT
462 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
463 - 1);
464 field_counter >= 0;
465 --field_counter)
466 {
467 struct fn_field *f;
468 const char *phys_name;
469
470 f = TYPE_FN_FIELDLIST1 (t, method_counter);
471 if (TYPE_FN_FIELD_STUB (f, field_counter))
472 continue;
473 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
474 VEC_safe_push (const_char_ptr, *result_names, phys_name);
475 }
476 }
aee8d8ba
DC
477 }
478 }
479
f8eba3c6
TT
480 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
481 VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
50641945
FN
482}
483
50641945
FN
484/* Find an instance of the character C in the string S that is outside
485 of all parenthesis pairs, single-quoted strings, and double-quoted
8120c9d5
EZ
486 strings. Also, ignore the char within a template name, like a ','
487 within foo<int, int>. */
488
50641945
FN
489static char *
490find_toplevel_char (char *s, char c)
491{
492 int quoted = 0; /* zero if we're not in quotes;
493 '"' if we're in a double-quoted string;
494 '\'' if we're in a single-quoted string. */
a04257e6 495 int depth = 0; /* Number of unclosed parens we've seen. */
50641945
FN
496 char *scan;
497
498 for (scan = s; *scan; scan++)
499 {
500 if (quoted)
501 {
502 if (*scan == quoted)
503 quoted = 0;
504 else if (*scan == '\\' && *(scan + 1))
505 scan++;
506 }
507 else if (*scan == c && ! quoted && depth == 0)
508 return scan;
509 else if (*scan == '"' || *scan == '\'')
510 quoted = *scan;
8120c9d5 511 else if (*scan == '(' || *scan == '<')
50641945 512 depth++;
8120c9d5 513 else if ((*scan == ')' || *scan == '>') && depth > 0)
50641945
FN
514 depth--;
515 }
516
517 return 0;
518}
519
889f28e2 520/* Determines if the gives string corresponds to an Objective-C method
1777feb0 521 representation, such as -[Foo bar:] or +[Foo bar]. Objective-C symbols
889f28e2
AF
522 are allowed to have spaces and parentheses in them. */
523
524static int
525is_objc_method_format (const char *s)
526{
527 if (s == NULL || *s == '\0')
528 return 0;
529 /* Handle arguments with the format FILENAME:SYMBOL. */
530 if ((s[0] == ':') && (strchr ("+-", s[1]) != NULL)
531 && (s[2] == '[') && strchr(s, ']'))
532 return 1;
533 /* Handle arguments that are just SYMBOL. */
534 else if ((strchr ("+-", s[0]) != NULL) && (s[1] == '[') && strchr(s, ']'))
535 return 1;
536 return 0;
537}
538
f8eba3c6
TT
539/* Given FILTERS, a list of canonical names, filter the sals in RESULT
540 and store the result in SELF->CANONICAL. */
50641945 541
f8eba3c6
TT
542static void
543filter_results (struct linespec_state *self,
544 struct symtabs_and_lines *result,
545 VEC (const_char_ptr) *filters)
546{
547 int i;
548 const char *name;
549
550 for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
551 {
552 struct linespec_sals lsal;
553 int j;
554
555 memset (&lsal, 0, sizeof (lsal));
556
557 for (j = 0; j < result->nelts; ++j)
558 {
559 if (strcmp (name, self->canonical_names[j]) == 0)
560 add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);
561 }
562
563 if (lsal.sals.nelts > 0)
564 {
565 lsal.canonical = xstrdup (name);
566 VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
567 }
568 }
569
570 self->canonical->pre_expanded = 0;
571}
572
573/* Store RESULT into SELF->CANONICAL. */
574
575static void
576convert_results_to_lsals (struct linespec_state *self,
577 struct symtabs_and_lines *result)
50641945 578{
f8eba3c6
TT
579 struct linespec_sals lsal;
580
581 lsal.canonical = NULL;
582 lsal.sals = *result;
583 VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
584}
585
586/* Handle multiple results in RESULT depending on SELECT_MODE. This
587 will either return normally, throw an exception on multiple
588 results, or present a menu to the user. On return, the SALS vector
589 in SELF->CANONICAL is set up properly. */
590
591static void
592decode_line_2 (struct linespec_state *self,
593 struct symtabs_and_lines *result,
594 const char *select_mode)
595{
596 const char *iter;
597 char *args, *prompt;
50641945 598 int i;
50641945 599 struct cleanup *old_chain;
f8eba3c6
TT
600 VEC (const_char_ptr) *item_names = NULL, *filters = NULL;
601 struct get_number_or_range_state state;
50641945 602
f8eba3c6
TT
603 gdb_assert (select_mode != multiple_symbols_all);
604 gdb_assert (self->canonical != NULL);
50641945 605
f8eba3c6
TT
606 old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &item_names);
607 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
608 for (i = 0; i < result->nelts; ++i)
50641945 609 {
f8eba3c6
TT
610 int j, found = 0;
611 const char *iter;
612
613 gdb_assert (self->canonical_names[i] != NULL);
614 for (j = 0; VEC_iterate (const_char_ptr, item_names, j, iter); ++j)
615 {
616 if (strcmp (iter, self->canonical_names[i]) == 0)
617 {
618 found = 1;
619 break;
620 }
621 }
622
623 if (!found)
624 VEC_safe_push (const_char_ptr, item_names, self->canonical_names[i]);
50641945
FN
625 }
626
f8eba3c6
TT
627 if (select_mode == multiple_symbols_cancel
628 && VEC_length (const_char_ptr, item_names) > 1)
629 error (_("canceled because the command is ambiguous\n"
630 "See set/show multiple-symbol."));
631
632 if (select_mode == multiple_symbols_all
633 || VEC_length (const_char_ptr, item_names) == 1)
50641945 634 {
f8eba3c6
TT
635 do_cleanups (old_chain);
636 convert_results_to_lsals (self, result);
637 return;
50641945
FN
638 }
639
f8eba3c6
TT
640 printf_unfiltered (_("[0] cancel\n[1] all\n"));
641 for (i = 0; VEC_iterate (const_char_ptr, item_names, i, iter); ++i)
642 printf_unfiltered ("[%d] %s\n", i + 2, iter);
643
644 prompt = getenv ("PS2");
645 if (prompt == NULL)
50641945 646 {
f8eba3c6 647 prompt = "> ";
50641945 648 }
f8eba3c6 649 args = command_line_input (prompt, 0, "overload-choice");
50641945
FN
650
651 if (args == 0 || *args == 0)
e2e0b3e5 652 error_no_arg (_("one or more choice numbers"));
50641945 653
f8eba3c6
TT
654 init_number_or_range (&state, args);
655 while (!state.finished)
50641945
FN
656 {
657 int num;
658
f8eba3c6 659 num = get_number_or_range (&state);
50641945
FN
660
661 if (num == 0)
8a3fe4f8 662 error (_("canceled"));
50641945
FN
663 else if (num == 1)
664 {
f8eba3c6
TT
665 /* We intentionally make this result in a single breakpoint,
666 contrary to what older versions of gdb did. The
667 rationale is that this lets a user get the
668 multiple_symbols_all behavior even with the 'ask'
669 setting; and he can get separate breakpoints by entering
670 "2-57" at the query. */
671 do_cleanups (old_chain);
672 convert_results_to_lsals (self, result);
673 return;
50641945
FN
674 }
675
f8eba3c6
TT
676 num -= 2;
677 if (num >= VEC_length (const_char_ptr, item_names))
678 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
679 else
680 {
f8eba3c6
TT
681 const char *elt = VEC_index (const_char_ptr, item_names, num);
682
683 if (elt != NULL)
50641945 684 {
f8eba3c6
TT
685 VEC_safe_push (const_char_ptr, filters, elt);
686 VEC_replace (const_char_ptr, item_names, num, NULL);
50641945
FN
687 }
688 else
689 {
3e43a32a
MS
690 printf_unfiltered (_("duplicate request for %d ignored.\n"),
691 num);
50641945
FN
692 }
693 }
50641945 694 }
f8eba3c6
TT
695
696 filter_results (self, result, filters);
697 do_cleanups (old_chain);
50641945 698}
94af9270 699
3d50dd94
JK
700/* Valid delimiters for linespec keywords "if", "thread" or "task". */
701
702static int
703is_linespec_boundary (char c)
704{
705 return c == ' ' || c == '\t' || c == '\0' || c == ',';
706}
707
94af9270
KS
708/* A helper function for decode_line_1 and friends which skips P
709 past any method overload information at the beginning of P, e.g.,
710 "(const struct foo *)".
711
712 This function assumes that P has already been validated to contain
713 overload information, and it will assert if *P != '('. */
714static char *
715find_method_overload_end (char *p)
716{
717 int depth = 0;
718
719 gdb_assert (*p == '(');
720
721 while (*p)
722 {
723 if (*p == '(')
724 ++depth;
725 else if (*p == ')')
726 {
727 if (--depth == 0)
728 {
729 ++p;
730 break;
731 }
732 }
733 ++p;
734 }
735
736 return p;
737}
c00f8484 738
c00f8484 739/* Keep important information used when looking up a name. This includes
3d50dd94
JK
740 template parameters, overload information, and important keywords, including
741 the possible Java trailing type. */
c00f8484
KS
742
743static char *
3d50dd94 744keep_name_info (char *p, int on_boundary)
c00f8484 745{
3d50dd94
JK
746 const char *quotes = get_gdb_completer_quote_characters ();
747 char *saved_p = p;
748 int nest = 0;
c00f8484 749
3d50dd94
JK
750 while (*p)
751 {
752 if (strchr (quotes, *p))
753 break;
c00f8484 754
3d50dd94
JK
755 if (*p == ',' && !nest)
756 break;
c00f8484 757
3d50dd94
JK
758 if (on_boundary && !nest)
759 {
760 const char *const words[] = { "if", "thread", "task" };
761 int wordi;
c00f8484 762
3d50dd94
JK
763 for (wordi = 0; wordi < ARRAY_SIZE (words); wordi++)
764 if (strncmp (p, words[wordi], strlen (words[wordi])) == 0
765 && is_linespec_boundary (p[strlen (words[wordi])]))
766 break;
767 if (wordi < ARRAY_SIZE (words))
768 break;
769 }
c00f8484 770
3d50dd94
JK
771 if (*p == '(' || *p == '<' || *p == '[')
772 nest++;
773 else if ((*p == ')' || *p == '>' || *p == ']') && nest > 0)
774 nest--;
c00f8484 775
3d50dd94
JK
776 p++;
777
778 /* The ',' check could fail on "operator ,". */
779 p += cp_validate_operator (p);
780
781 on_boundary = is_linespec_boundary (p[-1]);
f17170e5 782 }
c00f8484 783
3d50dd94
JK
784 while (p > saved_p && is_linespec_boundary (p[-1]))
785 p--;
786
787 return p;
c00f8484
KS
788}
789
50641945 790\f
1777feb0 791/* The parser of linespec itself. */
50641945
FN
792
793/* Parse a string that specifies a line number.
794 Pass the address of a char * variable; that variable will be
795 advanced over the characters actually parsed.
796
797 The string can be:
798
799 LINENUM -- that line number in current file. PC returned is 0.
800 FILE:LINENUM -- that line in that file. PC returned is 0.
801 FUNCTION -- line number of openbrace of that function.
802 PC returned is the start of the function.
0f5238ed 803 LABEL -- a label in the current scope
50641945
FN
804 VARIABLE -- line number of definition of that variable.
805 PC returned is 0.
806 FILE:FUNCTION -- likewise, but prefer functions in that file.
807 *EXPR -- line in which address EXPR appears.
808
809 This may all be followed by an "if EXPR", which we ignore.
810
811 FUNCTION may be an undebuggable function found in minimal symbol table.
812
813 If the argument FUNFIRSTLINE is nonzero, we want the first line
814 of real code inside a function when a function is specified, and it is
815 not OK to specify a variable or type to get its line number.
816
817 DEFAULT_SYMTAB specifies the file to use if none is specified.
818 It defaults to current_source_symtab.
819 DEFAULT_LINE specifies the line number to use for relative
820 line numbers (that start with signs). Defaults to current_source_line.
821 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 822 line specs there if necessary. Currently overloaded member functions and
50641945 823 line numbers or static functions without a filename yield a canonical
1777feb0 824 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
825 it is the callers responsibility to free them.
826
827 Note that it is possible to return zero for the symtab
828 if no file is validly specified. Callers must check that.
58438ac1 829 Also, the line number returned may be invalid. */
50641945
FN
830
831/* We allow single quotes in various places. This is a hideous
832 kludge, which exists because the completer can't yet deal with the
833 lack of single quotes. FIXME: write a linespec_completer which we
834 can use as appropriate instead of make_symbol_completion_list. */
835
836struct symtabs_and_lines
f8eba3c6 837decode_line_internal (struct linespec_state *self, char **argptr)
50641945 838{
f3c39e76 839 char *p;
614b3b14 840 char *q;
50641945 841
50641945 842 char *copy;
636b1a6d
DC
843 /* This says whether or not something in *ARGPTR is quoted with
844 completer_quotes (i.e. with single quotes). */
e325fb69 845 int is_quoted;
e5dd4106 846 /* Is *ARGPTR enclosed in double quotes? */
50641945 847 int is_quote_enclosed;
d2630e69 848 int is_objc_method = 0;
50641945 849 char *saved_arg = *argptr;
791dfb64
DJ
850 /* If IS_QUOTED, the end of the quoted bit. */
851 char *end_quote = NULL;
e8eb7bc5
KS
852 /* Is *ARGPTR enclosed in single quotes? */
853 int is_squote_enclosed = 0;
0e0b460e
KS
854 /* The "first half" of the linespec. */
855 char *first_half;
50641945 856
f8eba3c6
TT
857 /* If we are parsing `function:label', this holds the symbols
858 matching the function name. */
859 VEC (symbolp) *function_symbols = NULL;
860 /* If FUNCTION_SYMBOLS is not NULL, then this is the exception that
9ef07c8c
TT
861 was thrown when trying to parse a filename. */
862 volatile struct gdb_exception file_exception;
863
f8eba3c6
TT
864 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
865
50641945
FN
866 /* Defaults have defaults. */
867
f8eba3c6 868 initialize_defaults (&self->default_symtab, &self->default_line);
44fe14ab 869
a04257e6 870 /* See if arg is *PC. */
50641945 871
e325fb69 872 if (**argptr == '*')
f8eba3c6
TT
873 {
874 do_cleanups (cleanup);
875 return decode_indirect (self, argptr);
876 }
5f01dbc0 877
e325fb69
MS
878 is_quoted = (strchr (get_gdb_completer_quote_characters (),
879 **argptr) != NULL);
50641945 880
791dfb64 881 if (is_quoted)
e8eb7bc5
KS
882 {
883 end_quote = skip_quoted (*argptr);
884 if (*end_quote == '\0')
885 is_squote_enclosed = 1;
886 }
50641945 887
0960f083
DC
888 /* Check to see if it's a multipart linespec (with colons or
889 periods). */
50641945 890
17763fd9
EZ
891 /* Locate the end of the first half of the linespec.
892 After the call, for instance, if the argptr string is "foo.c:123"
893 p will point at "123". If there is only one part, like "foo", p
1777feb0
MS
894 will point to "". If this is a C++ name, like "A::B::foo", p will
895 point to "::B::foo". Argptr is not changed by this call. */
50641945 896
0e0b460e 897 first_half = p = locate_first_half (argptr, &is_quote_enclosed);
50641945 898
e8eb7bc5
KS
899 /* First things first: if ARGPTR starts with a filename, get its
900 symtab and strip the filename from ARGPTR. */
901 TRY_CATCH (file_exception, RETURN_MASK_ERROR)
902 {
f8eba3c6
TT
903 self->file_symtabs = symtabs_from_filename (argptr, p, is_quote_enclosed,
904 &self->user_filename);
905 }
906
907 if (VEC_empty (symtab_p, self->file_symtabs))
908 {
909 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
910 VEC_safe_push (symtab_p, self->file_symtabs, NULL);
e8eb7bc5
KS
911 }
912
913 if (file_exception.reason >= 0)
914 {
915 /* Check for single quotes on the non-filename part. */
916 is_quoted = (**argptr
917 && strchr (get_gdb_completer_quote_characters (),
918 **argptr) != NULL);
919 if (is_quoted)
920 end_quote = skip_quoted (*argptr);
921
922 /* Locate the next "half" of the linespec. */
923 first_half = p = locate_first_half (argptr, &is_quote_enclosed);
924 }
925
d2630e69
AF
926 /* Check if this is an Objective-C method (anything that starts with
927 a '+' or '-' and a '['). */
889f28e2 928 if (is_objc_method_format (p))
94af9270 929 is_objc_method = 1;
d2630e69
AF
930
931 /* Check if the symbol could be an Objective-C selector. */
932
933 {
934 struct symtabs_and_lines values;
e0881a8e 935
f8eba3c6 936 values = decode_objc (self, argptr);
d2630e69 937 if (values.sals != NULL)
f8eba3c6
TT
938 {
939 do_cleanups (cleanup);
940 return values;
941 }
d2630e69
AF
942 }
943
0960f083 944 /* Does it look like there actually were two parts? */
50641945 945
791dfb64 946 if (p[0] == ':' || p[0] == '.')
50641945 947 {
17763fd9
EZ
948 /* Is it a C++ or Java compound data structure?
949 The check on p[1] == ':' is capturing the case of "::",
1777feb0 950 since p[0]==':' was checked above.
17763fd9
EZ
951 Note that the call to decode_compound does everything
952 for us, including the lookup on the symbol table, so we
1777feb0 953 can return now. */
17763fd9 954
50641945 955 if (p[0] == '.' || p[1] == ':')
791dfb64 956 {
94af9270 957 struct symtabs_and_lines values;
dcf9f4ab
JK
958 volatile struct gdb_exception ex;
959 char *saved_argptr = *argptr;
94af9270
KS
960
961 if (is_quote_enclosed)
962 ++saved_arg;
dcf9f4ab 963
2f741504
JK
964 /* Initialize it just to avoid a GCC false warning. */
965 memset (&values, 0, sizeof (values));
966
dcf9f4ab
JK
967 TRY_CATCH (ex, RETURN_MASK_ERROR)
968 {
f8eba3c6 969 values = decode_compound (self, argptr, saved_arg, p);
dcf9f4ab 970 }
e8eb7bc5 971 if ((is_quoted || is_squote_enclosed) && **argptr == '\'')
94af9270 972 *argptr = *argptr + 1;
50641945 973
dcf9f4ab 974 if (ex.reason >= 0)
f8eba3c6
TT
975 {
976 do_cleanups (cleanup);
977 return values;
978 }
50641945 979
dcf9f4ab
JK
980 if (ex.error != NOT_FOUND_ERROR)
981 throw_exception (ex);
982
983 *argptr = saved_argptr;
984 }
985 else
50641945 986 {
dcf9f4ab
JK
987 /* If there was an exception looking up a specified filename earlier,
988 then check whether we were really given `function:label'. */
989 if (file_exception.reason < 0)
990 {
f8eba3c6
TT
991 function_symbols = find_function_symbols (argptr, p,
992 is_quote_enclosed,
993 &self->user_function);
994
dcf9f4ab
JK
995 /* If we did not find a function, re-throw the original
996 exception. */
f8eba3c6 997 if (!function_symbols)
dcf9f4ab 998 throw_exception (file_exception);
f8eba3c6
TT
999
1000 make_cleanup (VEC_cleanup (symbolp), &function_symbols);
dcf9f4ab
JK
1001 }
1002
1003 /* Check for single quotes on the non-filename part. */
1004 if (!is_quoted)
1005 {
1006 is_quoted = (**argptr
1007 && strchr (get_gdb_completer_quote_characters (),
1008 **argptr) != NULL);
1009 if (is_quoted)
1010 end_quote = skip_quoted (*argptr);
1011 }
50641945 1012 }
50641945 1013 }
50641945 1014
f8eba3c6
TT
1015 /* self->file_symtabs holds the specified file symtabs, or 0 if no file
1016 specified.
1017 If we are parsing `function:symbol', then FUNCTION_SYMBOLS holds the
1018 functions before the `:'.
50641945
FN
1019 arg no longer contains the file name. */
1020
0e0b460e
KS
1021 /* If the filename was quoted, we must re-check the quotation. */
1022
1023 if (end_quote == first_half && *end_quote!= '\0')
1024 {
1025 is_quoted = (**argptr
1026 && strchr (get_gdb_completer_quote_characters (),
1027 **argptr) != NULL);
1028 if (is_quoted)
1029 end_quote = skip_quoted (*argptr);
1030 }
1031
a04257e6 1032 /* Check whether arg is all digits (and sign). */
50641945
FN
1033
1034 q = *argptr;
1035 if (*q == '-' || *q == '+')
1036 q++;
1037 while (*q >= '0' && *q <= '9')
1038 q++;
1039
9ef07c8c 1040 if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ',')
f8eba3c6
TT
1041 && function_symbols == NULL)
1042 {
1043 struct symtabs_and_lines values;
1044
1045 /* We found a token consisting of all digits -- at least one digit. */
1046 values = decode_all_digits (self, argptr, q);
1047 do_cleanups (cleanup);
1048 return values;
1049 }
50641945
FN
1050
1051 /* Arg token is not digits => try it as a variable name
1052 Find the next token (everything up to end or next whitespace). */
1053
a04257e6
DC
1054 if (**argptr == '$') /* May be a convenience variable. */
1055 /* One or two $ chars possible. */
1056 p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));
e8eb7bc5 1057 else if (is_quoted || is_squote_enclosed)
50641945 1058 {
791dfb64 1059 p = end_quote;
50641945 1060 if (p[-1] != '\'')
8a3fe4f8 1061 error (_("Unmatched single quote."));
50641945 1062 }
d2630e69
AF
1063 else if (is_objc_method)
1064 {
1777feb0 1065 /* allow word separators in method names for Obj-C. */
d2630e69
AF
1066 p = skip_quoted_chars (*argptr, NULL, "");
1067 }
50641945
FN
1068 else
1069 {
1070 p = skip_quoted (*argptr);
1071 }
1072
c00f8484 1073 /* Keep any important naming information. */
3d50dd94 1074 p = keep_name_info (p, p == saved_arg || is_linespec_boundary (p[-1]));
94af9270 1075
50641945
FN
1076 copy = (char *) alloca (p - *argptr + 1);
1077 memcpy (copy, *argptr, p - *argptr);
1078 copy[p - *argptr] = '\0';
1079 if (p != *argptr
1080 && copy[0]
1081 && copy[0] == copy[p - *argptr - 1]
c5f0f3d0 1082 && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
50641945
FN
1083 {
1084 copy[p - *argptr - 1] = '\0';
1085 copy++;
1086 }
e8eb7bc5 1087 else if (is_quoted || is_squote_enclosed)
791dfb64 1088 copy[p - *argptr - 1] = '\0';
f8eba3c6
TT
1089
1090 *argptr = skip_spaces (p);
50641945
FN
1091
1092 /* If it starts with $: may be a legitimate variable or routine name
1093 (e.g. HP-UX millicode routines such as $$dyncall), or it may
a04257e6 1094 be history value, or it may be a convenience variable. */
50641945 1095
f8eba3c6
TT
1096 if (*copy == '$' && function_symbols == NULL)
1097 {
1098 struct symtabs_and_lines values;
1099
1100 values = decode_dollar (self, copy);
1101 do_cleanups (cleanup);
1102 return values;
1103 }
50641945 1104
0f5238ed
TT
1105 /* Try the token as a label, but only if no file was specified,
1106 because we can only really find labels in the current scope. */
1107
f8eba3c6
TT
1108 if (VEC_length (symtab_p, self->file_symtabs) == 1
1109 && VEC_index (symtab_p, self->file_symtabs, 0) == NULL)
0f5238ed
TT
1110 {
1111 struct symtabs_and_lines label_result;
f8eba3c6
TT
1112 if (decode_label (self, function_symbols, copy, &label_result))
1113 {
1114 do_cleanups (cleanup);
1115 return label_result;
1116 }
0f5238ed
TT
1117 }
1118
f8eba3c6 1119 if (function_symbols)
58438ac1 1120 throw_exception (file_exception);
9ef07c8c 1121
50641945
FN
1122 /* Look up that token as a variable.
1123 If file specified, use that file's per-file block to start with. */
1124
f8eba3c6
TT
1125 {
1126 struct symtabs_and_lines values;
1127
1128 values = decode_variable (self, copy);
1129 do_cleanups (cleanup);
1130 return values;
1131 }
413dad4d 1132}
50641945 1133
f8eba3c6 1134/* A constructor for linespec_state. */
44fe14ab 1135
f8eba3c6
TT
1136static void
1137linespec_state_constructor (struct linespec_state *self,
1138 int flags,
1139 struct symtab *default_symtab,
1140 int default_line,
1141 struct linespec_result *canonical)
1142{
1143 memset (self, 0, sizeof (*self));
1144 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
1145 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
1146 self->default_symtab = default_symtab;
1147 self->default_line = default_line;
1148 self->canonical = canonical;
1149 self->program_space = current_program_space;
1150 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
1151 xfree, xcalloc, xfree);
1152}
44fe14ab 1153
f8eba3c6 1154/* A destructor for linespec_state. */
44fe14ab
DC
1155
1156static void
f8eba3c6 1157linespec_state_destructor (void *arg)
44fe14ab 1158{
f8eba3c6 1159 struct linespec_state *self = arg;
44fe14ab 1160
f8eba3c6
TT
1161 xfree (self->user_filename);
1162 xfree (self->user_function);
1163 VEC_free (symtab_p, self->file_symtabs);
1164 htab_delete (self->addr_set);
1165}
44fe14ab 1166
f8eba3c6 1167/* See linespec.h. */
44fe14ab 1168
f8eba3c6
TT
1169void
1170decode_line_full (char **argptr, int flags,
1171 struct symtab *default_symtab,
1172 int default_line, struct linespec_result *canonical,
1173 const char *select_mode,
1174 const char *filter)
44fe14ab 1175{
f8eba3c6
TT
1176 struct symtabs_and_lines result;
1177 struct linespec_state state;
1178 struct cleanup *cleanups;
1179 char *arg_start = *argptr;
1180 VEC (const_char_ptr) *filters = NULL;
1181
1182 gdb_assert (canonical != NULL);
1183 /* The filter only makes sense for 'all'. */
1184 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
1185 gdb_assert (select_mode == NULL
1186 || select_mode == multiple_symbols_all
1187 || select_mode == multiple_symbols_ask
1188 || select_mode == multiple_symbols_cancel);
1189 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
1190
1191 linespec_state_constructor (&state, flags,
1192 default_symtab, default_line, canonical);
1193 cleanups = make_cleanup (linespec_state_destructor, &state);
1194 save_current_program_space ();
1195
1196 result = decode_line_internal (&state, argptr);
1197
1198 gdb_assert (result.nelts == 1 || canonical->pre_expanded);
1199 gdb_assert (canonical->addr_string != NULL);
1200 canonical->pre_expanded = 1;
1201
1202 /* Fill in the missing canonical names. */
1203 if (result.nelts > 0)
1204 {
1205 int i;
1206
1207 if (state.canonical_names == NULL)
1208 state.canonical_names = xcalloc (result.nelts, sizeof (char *));
1209 make_cleanup (xfree, state.canonical_names);
1210 for (i = 0; i < result.nelts; ++i)
1211 {
1212 if (state.canonical_names[i] == NULL)
1213 state.canonical_names[i] = savestring (arg_start,
1214 *argptr - arg_start);
1215 make_cleanup (xfree, state.canonical_names[i]);
1216 }
1217 }
1218
1219 if (select_mode == NULL)
1220 {
1221 if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
1222 select_mode = multiple_symbols_all;
1223 else
1224 select_mode = multiple_symbols_select_mode ();
1225 }
1226
1227 if (select_mode == multiple_symbols_all)
1228 {
1229 if (filter != NULL)
1230 {
1231 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
1232 VEC_safe_push (const_char_ptr, filters, filter);
1233 filter_results (&state, &result, filters);
1234 }
1235 else
1236 convert_results_to_lsals (&state, &result);
1237 }
1238 else
1239 decode_line_2 (&state, &result, select_mode);
1240
1241 do_cleanups (cleanups);
1242}
1243
1244struct symtabs_and_lines
1245decode_line_1 (char **argptr, int flags,
1246 struct symtab *default_symtab,
1247 int default_line)
1248{
1249 struct symtabs_and_lines result;
1250 struct linespec_state state;
1251 struct cleanup *cleanups;
1252
1253 linespec_state_constructor (&state, flags,
1254 default_symtab, default_line, NULL);
1255 cleanups = make_cleanup (linespec_state_destructor, &state);
1256 save_current_program_space ();
1257
1258 result = decode_line_internal (&state, argptr);
1259 do_cleanups (cleanups);
1260 return result;
1261}
1262
1263\f
1264
1265/* First, some functions to initialize stuff at the beggining of the
1266 function. */
1267
1268static void
1269initialize_defaults (struct symtab **default_symtab, int *default_line)
1270{
1271 if (*default_symtab == 0)
1272 {
1273 /* Use whatever we have for the default source line. We don't use
1274 get_current_or_default_symtab_and_line as it can recurse and call
1275 us back! */
1276 struct symtab_and_line cursal =
1277 get_current_source_symtab_and_line ();
1278
1279 *default_symtab = cursal.symtab;
1280 *default_line = cursal.line;
1281 }
1282}
1283
1284\f
1285
1286/* Decode arg of the form *PC. */
1287
1288static struct symtabs_and_lines
1289decode_indirect (struct linespec_state *self, char **argptr)
1290{
1291 struct symtabs_and_lines values;
1292 CORE_ADDR pc;
1293 char *initial = *argptr;
1294
1295 if (current_program_space->executing_startup)
1296 /* The error message doesn't really matter, because this case
1297 should only hit during breakpoint reset. */
1298 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
1299 "program space is in startup"));
1300
44fe14ab 1301 (*argptr)++;
63ffa6ee 1302 pc = value_as_address (parse_to_comma_and_eval (argptr));
44fe14ab
DC
1303
1304 values.sals = (struct symtab_and_line *)
1305 xmalloc (sizeof (struct symtab_and_line));
1306
1307 values.nelts = 1;
1308 values.sals[0] = find_pc_line (pc, 0);
1309 values.sals[0].pc = pc;
1310 values.sals[0].section = find_pc_overlay (pc);
ed0616c6 1311 values.sals[0].explicit_pc = 1;
44fe14ab 1312
f8eba3c6
TT
1313 if (self->canonical)
1314 self->canonical->addr_string = savestring (initial, *argptr - initial);
1315
44fe14ab
DC
1316 return values;
1317}
413dad4d
DC
1318
1319\f
1320
0960f083
DC
1321/* Locate the first half of the linespec, ending in a colon, period,
1322 or whitespace. (More or less.) Also, check to see if *ARGPTR is
1323 enclosed in double quotes; if so, set is_quote_enclosed, advance
17763fd9
EZ
1324 ARGPTR past that and zero out the trailing double quote.
1325 If ARGPTR is just a simple name like "main", p will point to ""
1326 at the end. */
0960f083
DC
1327
1328static char *
1329locate_first_half (char **argptr, int *is_quote_enclosed)
1330{
1331 char *ii;
1332 char *p, *p1;
1333 int has_comma;
1334
1335 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
1336 and we must isolate the first half. Outer layers will call again later
1337 for the second half.
1338
1339 Don't count commas that appear in argument lists of overloaded
1340 functions, or in quoted strings. It's stupid to go to this much
1341 trouble when the rest of the function is such an obvious roach hotel. */
1342 ii = find_toplevel_char (*argptr, ',');
1343 has_comma = (ii != 0);
1344
a04257e6 1345 /* Temporarily zap out second half to not confuse the code below.
1777feb0 1346 This is undone below. Do not change ii!! */
0960f083
DC
1347 if (has_comma)
1348 {
1349 *ii = '\0';
1350 }
1351
a04257e6
DC
1352 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION. May also be
1353 CLASS::MEMBER, or NAMESPACE::NAME. Look for ':', but ignore
1354 inside of <>. */
0960f083
DC
1355
1356 p = *argptr;
1357 if (p[0] == '"')
1358 {
1359 *is_quote_enclosed = 1;
1360 (*argptr)++;
1361 p++;
1362 }
1363 else
0e0b460e
KS
1364 {
1365 *is_quote_enclosed = 0;
1366 if (strchr (get_gdb_completer_quote_characters (), *p))
1367 {
1368 ++(*argptr);
1369 ++p;
1370 }
1371 }
731971ed
KS
1372
1373
1374 /* Check for a drive letter in the filename. This is done on all hosts
1375 to capture cross-compilation environments. On Unixen, directory
1376 separators are illegal in filenames, so if the user enters "e:/foo.c",
1377 he is referring to a directory named "e:" and a source file named
1378 "foo.c", and we still want to keep these two pieces together. */
1379 if (isalpha (p[0]) && p[1] == ':' && IS_DIR_SEPARATOR (p[2]))
1380 p += 3;
1381
0960f083
DC
1382 for (; *p; p++)
1383 {
1384 if (p[0] == '<')
1385 {
1386 char *temp_end = find_template_name_end (p);
e0881a8e 1387
0960f083 1388 if (!temp_end)
8a3fe4f8 1389 error (_("malformed template specification in command"));
0960f083
DC
1390 p = temp_end;
1391 }
c00f8484
KS
1392
1393 if (p[0] == '(')
1394 p = find_method_overload_end (p);
1395
d2630e69 1396 /* Check for a colon and a plus or minus and a [ (which
1777feb0 1397 indicates an Objective-C method). */
889f28e2 1398 if (is_objc_method_format (p))
d2630e69
AF
1399 {
1400 break;
1401 }
a04257e6 1402 /* Check for the end of the first half of the linespec. End of
e8eb7bc5
KS
1403 line, a tab, a colon or a space. But if enclosed in double
1404 quotes we do not break on enclosed spaces. */
0960f083
DC
1405 if (!*p
1406 || p[0] == '\t'
e8eb7bc5 1407 || (p[0] == ':')
0960f083
DC
1408 || ((p[0] == ' ') && !*is_quote_enclosed))
1409 break;
a04257e6 1410 if (p[0] == '.' && strchr (p, ':') == NULL)
0960f083 1411 {
a04257e6 1412 /* Java qualified method. Find the *last* '.', since the
94af9270
KS
1413 others are package qualifiers. Stop at any open parenthesis
1414 which might provide overload information. */
1415 for (p1 = p; *p1 && *p1 != '('; p1++)
0960f083
DC
1416 {
1417 if (*p1 == '.')
1418 p = p1;
1419 }
1420 break;
1421 }
1422 }
f8eba3c6 1423 p = skip_spaces (p);
0960f083 1424
a04257e6 1425 /* If the closing double quote was left at the end, remove it. */
0960f083
DC
1426 if (*is_quote_enclosed)
1427 {
1428 char *closing_quote = strchr (p - 1, '"');
e0881a8e 1429
0960f083
DC
1430 if (closing_quote && closing_quote[1] == '\0')
1431 *closing_quote = '\0';
1432 }
1433
a04257e6
DC
1434 /* Now that we've safely parsed the first half, put back ',' so
1435 outer layers can see it. */
0960f083
DC
1436 if (has_comma)
1437 *ii = ',';
1438
1439 return p;
1440}
1441
1442\f
1443
d2630e69
AF
1444/* Here's where we recognise an Objective-C Selector. An Objective C
1445 selector may be implemented by more than one class, therefore it
1446 may represent more than one method/function. This gives us a
1447 situation somewhat analogous to C++ overloading. If there's more
1448 than one method that could represent the selector, then use some of
1449 the existing C++ code to let the user choose one. */
1450
f8eba3c6
TT
1451static struct symtabs_and_lines
1452decode_objc (struct linespec_state *self, char **argptr)
d2630e69 1453{
f8eba3c6
TT
1454 struct collect_info info;
1455 VEC (const_char_ptr) *symbol_names = NULL;
1456 char *new_argptr;
1457 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
1458 &symbol_names);
1459
1460 info.state = self;
1461 info.result.sals = NULL;
1462 info.result.nelts = 0;
1463 info.objfile = NULL;
1464
1465 new_argptr = find_imps (*argptr, &symbol_names);
1466 if (VEC_empty (const_char_ptr, symbol_names))
1467 {
1468 do_cleanups (cleanup);
1469 return info.result;
1470 }
d2630e69 1471
f8eba3c6 1472 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
d2630e69 1473
f8eba3c6 1474 if (info.result.nelts > 0)
d2630e69 1475 {
f8eba3c6 1476 char *saved_arg;
d2630e69 1477
f8eba3c6
TT
1478 saved_arg = alloca (new_argptr - *argptr + 1);
1479 memcpy (saved_arg, *argptr, new_argptr - *argptr);
1480 saved_arg[new_argptr - *argptr] = '\0';
d2630e69 1481
f8eba3c6 1482 if (self->canonical)
d2630e69 1483 {
f8eba3c6
TT
1484 self->canonical->pre_expanded = 1;
1485 if (self->user_filename)
1486 self->canonical->addr_string
1487 = xstrprintf ("%s:%s", self->user_filename, saved_arg);
1488 else
1489 self->canonical->addr_string = xstrdup (saved_arg);
d2630e69 1490 }
d2630e69
AF
1491 }
1492
f8eba3c6 1493 *argptr = new_argptr;
d2630e69 1494
f8eba3c6
TT
1495 do_cleanups (cleanup);
1496 return info.result;
d2630e69
AF
1497}
1498
614b3b14 1499/* This handles C++ and Java compound data structures. P should point
17763fd9
EZ
1500 at the first component separator, i.e. double-colon or period. As
1501 an example, on entrance to this function we could have ARGPTR
1502 pointing to "AAA::inA::fun" and P pointing to "::inA::fun". */
614b3b14
DC
1503
1504static struct symtabs_and_lines
f8eba3c6
TT
1505decode_compound (struct linespec_state *self,
1506 char **argptr, char *the_real_saved_arg, char *p)
614b3b14
DC
1507{
1508 struct symtabs_and_lines values;
f8eba3c6 1509 char *p2;
614b3b14
DC
1510 char *saved_arg2 = *argptr;
1511 char *temp_end;
1512 struct symbol *sym;
614b3b14 1513 char *copy;
f8eba3c6
TT
1514 VEC (symbolp) *sym_classes;
1515 char *saved_arg, *class_name;
1516 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
c00f8484
KS
1517
1518 /* If the user specified any completer quote characters in the input,
1519 strip them. They are superfluous. */
1520 saved_arg = alloca (strlen (the_real_saved_arg) + 1);
1521 {
1522 char *dst = saved_arg;
1523 char *src = the_real_saved_arg;
1524 char *quotes = get_gdb_completer_quote_characters ();
1525 while (*src != '\0')
1526 {
1527 if (strchr (quotes, *src) == NULL)
1528 *dst++ = *src;
1529 ++src;
1530 }
1531 *dst = '\0';
1532 }
614b3b14 1533
17763fd9
EZ
1534 /* First check for "global" namespace specification, of the form
1535 "::foo". If found, skip over the colons and jump to normal
1536 symbol processing. I.e. the whole line specification starts with
1777feb0 1537 "::" (note the condition that *argptr == p). */
614b3b14
DC
1538 if (p[0] == ':'
1539 && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
1540 saved_arg2 += 2;
1541
87b3ede8
DC
1542 /* Given our example "AAA::inA::fun", we have two cases to consider:
1543
1544 1) AAA::inA is the name of a class. In that case, presumably it
1545 has a method called "fun"; we then look up that method using
1546 find_method.
1547
1548 2) AAA::inA isn't the name of a class. In that case, either the
c00f8484
KS
1549 user made a typo, AAA::inA is the name of a namespace, or it is
1550 the name of a minimal symbol.
f8eba3c6 1551 In this case we just delegate to decode_variable.
87b3ede8
DC
1552
1553 Thus, our first task is to find everything before the last set of
1554 double-colons and figure out if it's the name of a class. So we
1555 first loop through all of the double-colons. */
614b3b14 1556
a04257e6 1557 p2 = p; /* Save for restart. */
17763fd9 1558
1777feb0 1559 /* This is very messy. Following the example above we have now the
17763fd9
EZ
1560 following pointers:
1561 p -> "::inA::fun"
1562 argptr -> "AAA::inA::fun
1563 saved_arg -> "AAA::inA::fun
1564 saved_arg2 -> "AAA::inA::fun
1777feb0 1565 p2 -> "::inA::fun". */
17763fd9
EZ
1566
1567 /* In the loop below, with these strings, we'll make 2 passes, each
1777feb0 1568 is marked in comments. */
17763fd9 1569
614b3b14
DC
1570 while (1)
1571 {
c00f8484
KS
1572 static char *break_characters = " \t(";
1573
a04257e6 1574 /* Move pointer up to next possible class/namespace token. */
17763fd9 1575
a04257e6 1576 p = p2 + 1; /* Restart with old value +1. */
17763fd9
EZ
1577
1578 /* PASS1: at this point p2->"::inA::fun", so p->":inA::fun",
1579 i.e. if there is a double-colon, p will now point to the
1777feb0 1580 second colon. */
87b3ede8 1581 /* PASS2: p2->"::fun", p->":fun" */
17763fd9 1582
a04257e6 1583 /* Move pointer ahead to next double-colon. */
c00f8484
KS
1584 while (*p
1585 && strchr (break_characters, *p) == NULL
1586 && strchr (get_gdb_completer_quote_characters (), *p) == NULL)
614b3b14 1587 {
12907978
KS
1588 if (current_language->la_language == language_cplus)
1589 p += cp_validate_operator (p);
1590
614b3b14
DC
1591 if (p[0] == '<')
1592 {
1593 temp_end = find_template_name_end (p);
1594 if (!temp_end)
8a3fe4f8 1595 error (_("malformed template specification in command"));
614b3b14
DC
1596 p = temp_end;
1597 }
17763fd9
EZ
1598 /* Note that, since, at the start of this loop, p would be
1599 pointing to the second colon in a double-colon, we only
1600 satisfy the condition below if there is another
1777feb0
MS
1601 double-colon to the right (after). I.e. there is another
1602 component that can be a class or a namespace. I.e, if at
17763fd9
EZ
1603 the beginning of this loop (PASS1), we had
1604 p->":inA::fun", we'll trigger this when p has been
1605 advanced to point to "::fun". */
1777feb0 1606 /* PASS2: we will not trigger this. */
614b3b14 1607 else if ((p[0] == ':') && (p[1] == ':'))
a04257e6 1608 break; /* Found double-colon. */
614b3b14 1609 else
c00f8484
KS
1610 {
1611 /* PASS2: We'll keep getting here, until P points to one of the
1612 break characters, at which point we exit this loop. */
2b1dbab0
KS
1613 if (*p)
1614 {
1615 if (p[1] == '('
1616 && strncmp (&p[1], CP_ANONYMOUS_NAMESPACE_STR,
1617 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
1618 p += CP_ANONYMOUS_NAMESPACE_LEN;
1619 else if (strchr (break_characters, *p) == NULL)
1620 ++p;
1621 }
c00f8484 1622 }
614b3b14
DC
1623 }
1624
1625 if (*p != ':')
17763fd9
EZ
1626 break; /* Out of the while (1). This would happen
1627 for instance if we have looked up
1628 unsuccessfully all the components of the
1777feb0 1629 string, and p->""(PASS2). */
17763fd9 1630
c00f8484 1631 /* We get here if p points to one of the break characters or "" (i.e.,
1777feb0 1632 string ended). */
17763fd9
EZ
1633 /* Save restart for next time around. */
1634 p2 = p;
1635 /* Restore argptr as it was on entry to this function. */
1636 *argptr = saved_arg2;
87b3ede8
DC
1637 /* PASS1: at this point p->"::fun" argptr->"AAA::inA::fun",
1638 p2->"::fun". */
17763fd9
EZ
1639
1640 /* All ready for next pass through the loop. */
614b3b14
DC
1641 } /* while (1) */
1642
87b3ede8 1643
1777feb0 1644 /* Start of lookup in the symbol tables. */
87b3ede8
DC
1645
1646 /* Lookup in the symbol table the substring between argptr and
1777feb0 1647 p. Note, this call changes the value of argptr. */
87b3ede8
DC
1648 /* Before the call, argptr->"AAA::inA::fun",
1649 p->"", p2->"::fun". After the call: argptr->"fun", p, p2
1650 unchanged. */
f8eba3c6
TT
1651 sym_classes = lookup_prefix_sym (argptr, p2, self->file_symtabs,
1652 &class_name);
1653 make_cleanup (VEC_cleanup (symbolp), &sym_classes);
1654 make_cleanup (xfree, class_name);
1655
1656 /* If a class has been found, then we're in case 1 above. So we
1657 look up "fun" as a method of those classes. */
1658 if (!VEC_empty (symbolp, sym_classes))
87b3ede8
DC
1659 {
1660 /* Arg token is not digits => try it as a function name.
1661 Find the next token (everything up to end or next
1662 blank). */
1663 if (**argptr
1664 && strchr (get_gdb_completer_quote_characters (),
1665 **argptr) != NULL)
1666 {
1667 p = skip_quoted (*argptr);
1668 *argptr = *argptr + 1;
1669 }
1670 else
1671 {
1672 /* At this point argptr->"fun". */
94af9270 1673 char *a;
e0881a8e 1674
87b3ede8 1675 p = *argptr;
94af9270
KS
1676 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':'
1677 && *p != '(')
87b3ede8
DC
1678 p++;
1679 /* At this point p->"". String ended. */
12907978
KS
1680 /* Nope, C++ operators could have spaces in them
1681 ("foo::operator <" or "foo::operator delete []").
1682 I apologize, this is a bit hacky... */
1683 if (current_language->la_language == language_cplus
1684 && *p == ' ' && p - 8 - *argptr + 1 > 0)
1685 {
1686 /* The above loop has already swallowed "operator". */
1687 p += cp_validate_operator (p - 8) - 8;
1688 }
94af9270 1689
c00f8484 1690 /* Keep any important naming information. */
3d50dd94 1691 p = keep_name_info (p, 1);
87b3ede8
DC
1692 }
1693
1694 /* Allocate our own copy of the substring between argptr and
1777feb0 1695 p. */
87b3ede8
DC
1696 copy = (char *) alloca (p - *argptr + 1);
1697 memcpy (copy, *argptr, p - *argptr);
1698 copy[p - *argptr] = '\0';
1699 if (p != *argptr
1700 && copy[p - *argptr - 1]
1701 && strchr (get_gdb_completer_quote_characters (),
1702 copy[p - *argptr - 1]) != NULL)
1703 copy[p - *argptr - 1] = '\0';
1704
1777feb0 1705 /* At this point copy->"fun", p->"". */
87b3ede8
DC
1706
1707 /* No line number may be specified. */
f8eba3c6 1708 *argptr = skip_spaces (p);
87b3ede8
DC
1709 /* At this point arptr->"". */
1710
1777feb0 1711 /* Look for copy as a method of sym_class. */
87b3ede8
DC
1712 /* At this point copy->"fun", sym_class is "AAA:inA",
1713 saved_arg->"AAA::inA::fun". This concludes the scanning of
1714 the string for possible components matches. If we find it
1777feb0 1715 here, we return. If not, and we are at the and of the string,
87b3ede8
DC
1716 we'll lookup the whole string in the symbol tables. */
1717
f8eba3c6
TT
1718 values = find_method (self, saved_arg, copy, class_name, sym_classes);
1719
1720 do_cleanups (cleanup);
1721 return values;
1777feb0 1722 } /* End if symbol found. */
87b3ede8
DC
1723
1724
1725 /* We couldn't find a class, so we're in case 2 above. We check the
f8eba3c6
TT
1726 entire name as a symbol instead. The simplest way to do this is
1727 to just throw an exception and let our caller fall through to
1728 decode_variable. */
87b3ede8 1729
f8eba3c6
TT
1730 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
1731}
c00f8484 1732
f8eba3c6
TT
1733/* An instance of this type is used when collecting prefix symbols for
1734 decode_compound. */
17763fd9 1735
f8eba3c6
TT
1736struct decode_compound_collector
1737{
1738 /* The result vector. */
1739 VEC (symbolp) *symbols;
3a93a0c2 1740
f8eba3c6
TT
1741 /* A hash table of all symbols we found. We use this to avoid
1742 adding any symbol more than once. */
1743 htab_t unique_syms;
1744};
3a93a0c2 1745
f8eba3c6
TT
1746/* A callback for iterate_over_symbols that is used by
1747 lookup_prefix_sym to collect type symbols. */
c00f8484 1748
f8eba3c6
TT
1749static int
1750collect_one_symbol (struct symbol *sym, void *d)
1751{
1752 struct decode_compound_collector *collector = d;
1753 void **slot;
1754 struct type *t;
614b3b14 1755
f8eba3c6
TT
1756 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1757 return 1;
1758
1759 t = SYMBOL_TYPE (sym);
1760 CHECK_TYPEDEF (t);
1761 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1762 && TYPE_CODE (t) != TYPE_CODE_UNION
1763 && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
1764 return 1;
614b3b14 1765
f8eba3c6
TT
1766 slot = htab_find_slot (collector->unique_syms, sym, INSERT);
1767 if (!*slot)
1768 {
1769 *slot = sym;
1770 VEC_safe_push (symbolp, collector->symbols, sym);
1771 }
1772
1773 return 1;
1774}
93d91629
DC
1775
1776/* Return the symbol corresponding to the substring of *ARGPTR ending
1777 at P, allowing whitespace. Also, advance *ARGPTR past the symbol
1778 name in question, the compound object separator ("::" or "."), and
17763fd9 1779 whitespace. Note that *ARGPTR is changed whether or not the
f8eba3c6 1780 this call finds anything (i.e we return NULL). As an
17763fd9 1781 example, say ARGPTR is "AAA::inA::fun" and P is "::inA::fun". */
93d91629 1782
f8eba3c6
TT
1783static VEC (symbolp) *
1784lookup_prefix_sym (char **argptr, char *p, VEC (symtab_p) *file_symtabs,
1785 char **class_name)
93d91629
DC
1786{
1787 char *p1;
1788 char *copy;
f8eba3c6
TT
1789 int ix;
1790 struct symtab *elt;
1791 struct decode_compound_collector collector;
1792 struct cleanup *outer;
1793 struct cleanup *cleanup;
1794 struct block *search_block;
93d91629
DC
1795
1796 /* Extract the class name. */
1797 p1 = p;
1798 while (p != *argptr && p[-1] == ' ')
1799 --p;
f8eba3c6 1800 copy = (char *) xmalloc (p - *argptr + 1);
93d91629
DC
1801 memcpy (copy, *argptr, p - *argptr);
1802 copy[p - *argptr] = 0;
f8eba3c6
TT
1803 *class_name = copy;
1804 outer = make_cleanup (xfree, copy);
93d91629 1805
17763fd9 1806 /* Discard the class name from the argptr. */
93d91629 1807 p = p1 + (p1[0] == ':' ? 2 : 1);
f8eba3c6 1808 p = skip_spaces (p);
93d91629
DC
1809 *argptr = p;
1810
17763fd9 1811 /* At this point p1->"::inA::fun", p->"inA::fun" copy->"AAA",
1777feb0 1812 argptr->"inA::fun". */
17763fd9 1813
f8eba3c6
TT
1814 collector.symbols = NULL;
1815 make_cleanup (VEC_cleanup (symbolp), &collector.symbols);
e0881a8e 1816
f8eba3c6
TT
1817 collector.unique_syms = htab_create_alloc (1, htab_hash_pointer,
1818 htab_eq_pointer, NULL,
1819 xcalloc, xfree);
1820 cleanup = make_cleanup_htab_delete (collector.unique_syms);
e0881a8e 1821
f8eba3c6
TT
1822 for (ix = 0; VEC_iterate (symtab_p, file_symtabs, ix, elt); ++ix)
1823 {
1824 if (elt == NULL)
1825 {
1826 iterate_over_all_matching_symtabs (copy, STRUCT_DOMAIN,
1827 collect_one_symbol, &collector,
1828 NULL);
1829 iterate_over_all_matching_symtabs (copy, VAR_DOMAIN,
1830 collect_one_symbol, &collector,
1831 NULL);
1832 }
1833 else
1834 {
1835 struct block *search_block;
1836
1837 /* Program spaces that are executing startup should have
1838 been filtered out earlier. */
1839 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
1840 set_current_program_space (SYMTAB_PSPACE (elt));
1841 search_block = get_search_block (elt);
1842 LA_ITERATE_OVER_SYMBOLS (search_block, copy, STRUCT_DOMAIN,
1843 collect_one_symbol, &collector);
1844 LA_ITERATE_OVER_SYMBOLS (search_block, copy, VAR_DOMAIN,
1845 collect_one_symbol, &collector);
1e5a1abc
KS
1846 }
1847 }
1848
f8eba3c6
TT
1849 do_cleanups (cleanup);
1850 discard_cleanups (outer);
1851 return collector.symbols;
93d91629
DC
1852}
1853
f8eba3c6
TT
1854/* A qsort comparison function for symbols. The resulting order does
1855 not actually matter; we just need to be able to sort them so that
1856 symbols with the same program space end up next to each other. */
4224873a 1857
f8eba3c6
TT
1858static int
1859compare_symbols (const void *a, const void *b)
4224873a 1860{
f8eba3c6
TT
1861 struct symbol * const *sa = a;
1862 struct symbol * const *sb = b;
1863 uintptr_t uia, uib;
1864
1865 uia = (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sa));
1866 uib = (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sb));
1867
1868 if (uia < uib)
1869 return -1;
1870 if (uia > uib)
1871 return 1;
1872
1873 uia = (uintptr_t) *sa;
1874 uib = (uintptr_t) *sb;
1875
1876 if (uia < uib)
1877 return -1;
1878 if (uia > uib)
1879 return 1;
1880
1881 return 0;
1882}
1883
1884/* Look for all the matching instances of each symbol in NAMES. Only
1885 instances from PSPACE are considered; other program spaces are
1886 handled by our caller. If PSPACE is NULL, then all program spaces
1887 are considered. Results are stored into INFO. */
1888
1889static void
1890add_all_symbol_names_from_pspace (struct collect_info *info,
1891 struct program_space *pspace,
1892 VEC (const_char_ptr) *names)
1893{
1894 int ix;
1895 const char *iter;
1896
1897 for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
1898 add_matching_symbols_to_info (iter, info, pspace);
1899}
1900
1901static void
1902find_superclass_methods (VEC (typep) *superclasses,
1903 const char *name,
1904 VEC (const_char_ptr) **result_names)
1905{
1906 int old_len = VEC_length (const_char_ptr, *result_names);
1907 VEC (typep) *iter_classes;
1908 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
1909
1910 iter_classes = superclasses;
1911 while (1)
1912 {
1913 VEC (typep) *new_supers = NULL;
1914 int ix;
1915 struct type *t;
1916
1917 make_cleanup (VEC_cleanup (typep), &new_supers);
1918 for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
1919 find_methods (t, name, result_names, &new_supers);
1920
1921 if (VEC_length (const_char_ptr, *result_names) != old_len
1922 || VEC_empty (typep, new_supers))
1923 break;
4224873a 1924
f8eba3c6
TT
1925 iter_classes = new_supers;
1926 }
4224873a 1927
f8eba3c6
TT
1928 do_cleanups (cleanup);
1929}
1930
1931/* This finds the method COPY in the class whose type is given by one
1932 of the symbols in SYM_CLASSES. */
1933
1934static struct symtabs_and_lines
1935find_method (struct linespec_state *self, char *saved_arg,
1936 char *copy, const char *class_name, VEC (symbolp) *sym_classes)
1937{
1938 char *canon;
1939 struct symbol *sym;
1940 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
1941 int ix;
1942 int last_result_len;
1943 VEC (typep) *superclass_vec;
1944 VEC (const_char_ptr) *result_names;
1945 struct collect_info info;
1946 char *name_iter;
4224873a 1947
f8eba3c6
TT
1948 /* NAME is typed by the user: it needs to be canonicalized before
1949 searching the symbol tables. */
1950 canon = cp_canonicalize_string_no_typedefs (copy);
1951 if (canon != NULL)
4224873a 1952 {
f8eba3c6
TT
1953 copy = canon;
1954 make_cleanup (xfree, copy);
1955 }
4224873a 1956
f8eba3c6
TT
1957 /* Sort symbols so that symbols with the same program space are next
1958 to each other. */
1959 qsort (VEC_address (symbolp, sym_classes),
1960 VEC_length (symbolp, sym_classes),
1961 sizeof (symbolp),
1962 compare_symbols);
1963
1964 info.state = self;
1965 info.result.sals = NULL;
1966 info.result.nelts = 0;
1967 info.objfile = NULL;
1968
1969 /* Iterate over all the types, looking for the names of existing
1970 methods matching COPY. If we cannot find a direct method in a
1971 given program space, then we consider inherited methods; this is
1972 not ideal (ideal would be to respect C++ hiding rules), but it
1973 seems good enough and is what GDB has historically done. We only
1974 need to collect the names because later we find all symbols with
1975 those names. This loop is written in a somewhat funny way
1976 because we collect data across the program space before deciding
1977 what to do. */
1978 superclass_vec = NULL;
1979 make_cleanup (VEC_cleanup (typep), &superclass_vec);
1980 result_names = NULL;
1981 make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
1982 last_result_len = 0;
1983 for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
1984 {
1985 struct type *t;
1986 struct program_space *pspace;
1987
1988 /* Program spaces that are executing startup should have
1989 been filtered out earlier. */
1990 gdb_assert (!SYMTAB_PSPACE (SYMBOL_SYMTAB (sym))->executing_startup);
1991 pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
1992 set_current_program_space (pspace);
1993 t = check_typedef (SYMBOL_TYPE (sym));
1994 find_methods (t, copy, &result_names, &superclass_vec);
1995
1996 /* Handle all items from a single program space at once; and be
1997 sure not to miss the last batch. */
1998 if (ix == VEC_length (symbolp, sym_classes) - 1
1999 || (pspace
2000 != SYMTAB_PSPACE (SYMBOL_SYMTAB (VEC_index (symbolp, sym_classes,
2001 ix + 1)))))
4224873a 2002 {
f8eba3c6
TT
2003 /* If we did not find a direct implementation anywhere in
2004 this program space, consider superclasses. */
2005 if (VEC_length (const_char_ptr, result_names) == last_result_len)
2006 find_superclass_methods (superclass_vec, copy, &result_names);
2007
2008 /* We have a list of candidate symbol names, so now we
2009 iterate over the symbol tables looking for all
2010 matches in this pspace. */
2011 add_all_symbol_names_from_pspace (&info, pspace, result_names);
2012
2013 VEC_truncate (typep, superclass_vec, 0);
2014 last_result_len = VEC_length (const_char_ptr, result_names);
4224873a 2015 }
4224873a 2016 }
f8eba3c6
TT
2017
2018 if (info.result.nelts > 0)
4224873a 2019 {
f8eba3c6 2020 if (self->canonical)
94af9270 2021 {
f8eba3c6
TT
2022 self->canonical->pre_expanded = 1;
2023 if (self->user_filename)
2024 self->canonical->addr_string
2025 = xstrprintf ("%s:%s", self->user_filename, saved_arg);
2026 else
2027 self->canonical->addr_string = xstrdup (saved_arg);
94af9270
KS
2028 }
2029
f8eba3c6
TT
2030 do_cleanups (cleanup);
2031
2032 return info.result;
4224873a 2033 }
f8eba3c6
TT
2034
2035 if (copy[0] == '~')
2036 cplusplus_error (saved_arg,
2037 "the class `%s' does not have destructor defined\n",
2038 class_name);
4224873a 2039 else
f8eba3c6
TT
2040 cplusplus_error (saved_arg,
2041 "the class %s does not have any method named %s\n",
2042 class_name, copy);
2043}
2044
2045\f
2046
2047/* This object is used when collecting all matching symtabs. */
2048
2049struct symtab_collector
2050{
2051 /* The result vector of symtabs. */
2052 VEC (symtab_p) *symtabs;
2053
2054 /* This is used to ensure the symtabs are unique. */
2055 htab_t symtab_table;
2056};
2057
2058/* Callback for iterate_over_symtabs. */
2059
2060static int
2061add_symtabs_to_list (struct symtab *symtab, void *d)
2062{
2063 struct symtab_collector *data = d;
2064 void **slot;
2065
2066 slot = htab_find_slot (data->symtab_table, symtab, INSERT);
2067 if (!*slot)
4224873a 2068 {
f8eba3c6
TT
2069 *slot = symtab;
2070 VEC_safe_push (symtab_p, data->symtabs, symtab);
4224873a 2071 }
f8eba3c6
TT
2072
2073 return 0;
4224873a
DC
2074}
2075
f8eba3c6
TT
2076/* Given a file name, return a VEC of all matching symtabs. */
2077
2078static VEC (symtab_p) *
2079collect_symtabs_from_filename (const char *file)
2080{
2081 struct symtab_collector collector;
2082 struct cleanup *cleanups;
2083 struct program_space *pspace;
2084
2085 collector.symtabs = NULL;
2086 collector.symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
2087 NULL);
2088 cleanups = make_cleanup_htab_delete (collector.symtab_table);
2089
2090 /* Find that file's data. */
2091 ALL_PSPACES (pspace)
2092 {
2093 if (pspace->executing_startup)
2094 continue;
2095
2096 set_current_program_space (pspace);
2097 iterate_over_symtabs (file, add_symtabs_to_list, &collector);
2098 }
f3c39e76 2099
f8eba3c6
TT
2100 do_cleanups (cleanups);
2101 return collector.symtabs;
2102}
2103
2104/* Return all the symtabs associated to the filename given by the
2105 substring of *ARGPTR ending at P, and advance ARGPTR past that
2106 filename. */
f3c39e76 2107
f8eba3c6
TT
2108static VEC (symtab_p) *
2109symtabs_from_filename (char **argptr, char *p, int is_quote_enclosed,
2110 char **user_filename)
f3c39e76
DC
2111{
2112 char *p1;
2113 char *copy;
f8eba3c6
TT
2114 struct cleanup *outer;
2115 VEC (symtab_p) *result;
f3c39e76
DC
2116
2117 p1 = p;
2118 while (p != *argptr && p[-1] == ' ')
2119 --p;
2120 if ((*p == '"') && is_quote_enclosed)
2121 --p;
f8eba3c6
TT
2122 copy = xmalloc (p - *argptr + 1);
2123 outer = make_cleanup (xfree, copy);
f3c39e76 2124 memcpy (copy, *argptr, p - *argptr);
a04257e6 2125 /* It may have the ending quote right after the file name. */
0e0b460e
KS
2126 if ((is_quote_enclosed && copy[p - *argptr - 1] == '"')
2127 || copy[p - *argptr - 1] == '\'')
f3c39e76
DC
2128 copy[p - *argptr - 1] = 0;
2129 else
2130 copy[p - *argptr] = 0;
2131
f8eba3c6
TT
2132 result = collect_symtabs_from_filename (copy);
2133
2134 if (VEC_empty (symtab_p, result))
f3c39e76 2135 {
b96e2927
PA
2136 if (!have_full_symbols () && !have_partial_symbols ())
2137 throw_error (NOT_FOUND_ERROR,
3e43a32a
MS
2138 _("No symbol table is loaded. "
2139 "Use the \"file\" command."));
109c3e39 2140 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), copy);
f3c39e76
DC
2141 }
2142
2143 /* Discard the file name from the arg. */
edb2aadf 2144 if (*p1 == '\0')
f8eba3c6
TT
2145 *argptr = p1;
2146 else
2147 *argptr = skip_spaces (p1 + 1);
2148
2149 discard_cleanups (outer);
2150 *user_filename = copy;
2151 return result;
2152}
2153
2154/* A callback used by iterate_over_all_matching_symtabs that collects
2155 symbols for find_function_symbols. */
2156
2157static int
2158collect_function_symbols (struct symbol *sym, void *arg)
2159{
2160 VEC (symbolp) **syms = arg;
f3c39e76 2161
f8eba3c6
TT
2162 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
2163 VEC_safe_push (symbolp, *syms, sym);
2164
2165 return 1;
f3c39e76
DC
2166}
2167
9ef07c8c
TT
2168/* Look up a function symbol in *ARGPTR. If found, advance *ARGPTR
2169 and return the symbol. If not found, return NULL. */
2170
f8eba3c6
TT
2171static VEC (symbolp) *
2172find_function_symbols (char **argptr, char *p, int is_quote_enclosed,
2173 char **user_function)
9ef07c8c
TT
2174{
2175 char *p1;
2176 char *copy;
f8eba3c6 2177 VEC (symbolp) *result = NULL;
9ef07c8c
TT
2178
2179 p1 = p;
2180 while (p != *argptr && p[-1] == ' ')
2181 --p;
2182 if ((*p == '"') && is_quote_enclosed)
2183 --p;
f8eba3c6
TT
2184 copy = (char *) xmalloc (p - *argptr + 1);
2185 *user_function = copy;
9ef07c8c
TT
2186 memcpy (copy, *argptr, p - *argptr);
2187 /* It may have the ending quote right after the file name. */
2188 if ((is_quote_enclosed && copy[p - *argptr - 1] == '"')
2189 || copy[p - *argptr - 1] == '\'')
2190 copy[p - *argptr - 1] = 0;
2191 else
2192 copy[p - *argptr] = 0;
2193
f8eba3c6
TT
2194 iterate_over_all_matching_symtabs (copy, VAR_DOMAIN,
2195 collect_function_symbols, &result, NULL);
9ef07c8c 2196
f8eba3c6
TT
2197 if (VEC_empty (symbolp, result))
2198 VEC_free (symbolp, result);
2199 else
2200 {
2201 /* Discard the file name from the arg. */
2202 *argptr = skip_spaces (p1 + 1);
2203 }
9ef07c8c 2204
f8eba3c6 2205 return result;
9ef07c8c
TT
2206}
2207
84fba31b
DC
2208\f
2209
f8eba3c6
TT
2210/* A helper for decode_all_digits that handles the 'list_mode' case. */
2211
2212static void
2213decode_digits_list_mode (struct linespec_state *self,
2214 struct symtabs_and_lines *values,
2215 struct symtab_and_line val)
2216{
2217 int ix;
2218 struct symtab *elt;
2219
2220 gdb_assert (self->list_mode);
2221
2222 for (ix = 0; VEC_iterate (symtab_p, self->file_symtabs, ix, elt); ++ix)
2223 {
2224 /* The logic above should ensure this. */
2225 gdb_assert (elt != NULL);
2226
2227 set_current_program_space (SYMTAB_PSPACE (elt));
2228
2229 /* Simplistic search just for the list command. */
2230 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
2231 if (val.symtab == NULL)
2232 val.symtab = elt;
2233 val.pspace = SYMTAB_PSPACE (elt);
2234 val.pc = 0;
2235 val.explicit_line = 1;
2236
2237 add_sal_to_sals (self, values, &val, NULL);
2238 }
2239}
2240
2241/* A helper for decode_all_digits that iterates over the symtabs,
2242 adding lines to the VEC. */
2243
2244static void
2245decode_digits_ordinary (struct linespec_state *self,
2246 int line,
2247 struct symtabs_and_lines *sals,
2248 struct linetable_entry **best_entry)
2249{
2250 int ix;
2251 struct symtab *elt;
2252
2253 for (ix = 0; VEC_iterate (symtab_p, self->file_symtabs, ix, elt); ++ix)
2254 {
2255 int i;
2256 VEC (CORE_ADDR) *pcs;
2257 CORE_ADDR pc;
2258
2259 /* The logic above should ensure this. */
2260 gdb_assert (elt != NULL);
2261
2262 set_current_program_space (SYMTAB_PSPACE (elt));
2263
2264 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
2265 for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
2266 {
2267 struct symtab_and_line sal;
2268
2269 init_sal (&sal);
2270 sal.pspace = SYMTAB_PSPACE (elt);
2271 sal.symtab = elt;
2272 sal.line = line;
2273 sal.pc = pc;
2274 add_sal_to_sals_basic (sals, &sal);
2275 }
2276
2277 VEC_free (CORE_ADDR, pcs);
2278 }
2279}
2280
84fba31b
DC
2281/* This decodes a line where the argument is all digits (possibly
2282 preceded by a sign). Q should point to the end of those digits;
2283 the other arguments are as usual. */
2284
2285static struct symtabs_and_lines
f8eba3c6
TT
2286decode_all_digits (struct linespec_state *self,
2287 char **argptr,
2288 char *q)
84fba31b
DC
2289{
2290 struct symtabs_and_lines values;
2291 struct symtab_and_line val;
f8eba3c6
TT
2292 int use_default = 0;
2293 char *saved_arg = *argptr;
84fba31b
DC
2294
2295 enum sign
2296 {
2297 none, plus, minus
2298 }
2299 sign = none;
2300
84fba31b 2301 init_sal (&val);
f8eba3c6
TT
2302 values.sals = NULL;
2303 values.nelts = 0;
6c95b8df 2304
84fba31b
DC
2305 /* This is where we need to make sure that we have good defaults.
2306 We must guarantee that this section of code is never executed
2307 when we are called with just a function name, since
2308 set_default_source_symtab_and_line uses
a04257e6 2309 select_source_symtab that calls us with such an argument. */
84fba31b 2310
f8eba3c6
TT
2311 if (VEC_length (symtab_p, self->file_symtabs) == 1
2312 && VEC_index (symtab_p, self->file_symtabs, 0) == NULL)
84fba31b 2313 {
f8eba3c6
TT
2314 set_current_program_space (self->program_space);
2315
a04257e6 2316 /* Make sure we have at least a default source file. */
84fba31b 2317 set_default_source_symtab_and_line ();
f8eba3c6
TT
2318 initialize_defaults (&self->default_symtab, &self->default_line);
2319 VEC_pop (symtab_p, self->file_symtabs);
2320 VEC_free (symtab_p, self->file_symtabs);
2321 self->file_symtabs
2322 = collect_symtabs_from_filename (self->default_symtab->filename);
2323 use_default = 1;
84fba31b
DC
2324 }
2325
2326 if (**argptr == '+')
2327 sign = plus, (*argptr)++;
2328 else if (**argptr == '-')
2329 sign = minus, (*argptr)++;
2330 val.line = atoi (*argptr);
2331 switch (sign)
2332 {
2333 case plus:
2334 if (q == *argptr)
2335 val.line = 5;
f8eba3c6
TT
2336 if (use_default)
2337 val.line = self->default_line + val.line;
84fba31b
DC
2338 break;
2339 case minus:
2340 if (q == *argptr)
2341 val.line = 15;
f8eba3c6
TT
2342 if (use_default)
2343 val.line = self->default_line - val.line;
84fba31b
DC
2344 else
2345 val.line = 1;
2346 break;
2347 case none:
2348 break; /* No need to adjust val.line. */
2349 }
2350
f8eba3c6
TT
2351 *argptr = skip_spaces (q);
2352
2353 if (self->list_mode)
2354 decode_digits_list_mode (self, &values, val);
2355 else
2356 {
2357 struct linetable_entry *best_entry = NULL;
2358 int *filter;
2359 struct block **blocks;
2360 struct cleanup *cleanup;
2361 struct symtabs_and_lines intermediate_results;
2362 int i, j;
2363
2364 intermediate_results.sals = NULL;
2365 intermediate_results.nelts = 0;
2366
2367 decode_digits_ordinary (self, val.line, &intermediate_results,
2368 &best_entry);
2369 if (intermediate_results.nelts == 0 && best_entry != NULL)
2370 decode_digits_ordinary (self, best_entry->line, &intermediate_results,
2371 &best_entry);
2372
2373 cleanup = make_cleanup (xfree, intermediate_results.sals);
2374
2375 /* For optimized code, compiler can scatter one source line
2376 accross disjoint ranges of PC values, even when no duplicate
2377 functions or inline functions are involved. For example,
2378 'for (;;)' inside non-template non-inline non-ctor-or-dtor
2379 function can result in two PC ranges. In this case, we don't
2380 want to set breakpoint on first PC of each range. To filter
2381 such cases, we use containing blocks -- for each PC found
2382 above we see if there are other PCs that are in the same
2383 block. If yes, the other PCs are filtered out. */
2384
2385 filter = xmalloc (intermediate_results.nelts * sizeof (int));
2386 make_cleanup (xfree, filter);
2387 blocks = xmalloc (intermediate_results.nelts * sizeof (struct block *));
2388 make_cleanup (xfree, blocks);
2389
2390 for (i = 0; i < intermediate_results.nelts; ++i)
2391 {
2392 set_current_program_space (intermediate_results.sals[i].pspace);
2393
2394 filter[i] = 1;
2395 blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
2396 intermediate_results.sals[i].section);
2397 }
2398
2399 for (i = 0; i < intermediate_results.nelts; ++i)
2400 {
2401 if (blocks[i] != NULL)
2402 for (j = i + 1; j < intermediate_results.nelts; ++j)
2403 {
2404 if (blocks[j] == blocks[i])
2405 {
2406 filter[j] = 0;
2407 break;
2408 }
2409 }
2410 }
2411
2412 for (i = 0; i < intermediate_results.nelts; ++i)
2413 if (filter[i])
2414 {
2415 struct symbol *sym = (blocks[i]
2416 ? block_containing_function (blocks[i])
2417 : NULL);
2418
2419 if (self->funfirstline)
2420 skip_prologue_sal (&intermediate_results.sals[i]);
2421 /* Make sure the line matches the request, not what was
2422 found. */
2423 intermediate_results.sals[i].line = val.line;
2424 add_sal_to_sals (self, &values, &intermediate_results.sals[i],
2425 sym ? SYMBOL_NATURAL_NAME (sym) : NULL);
2426 }
2427
2428 do_cleanups (cleanup);
2429 }
2430
2431 if (values.nelts == 0)
2432 {
2433 if (self->user_filename)
2434 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
2435 val.line, self->user_filename);
2436 else
2437 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2438 val.line);
2439 }
2440
2441 if (self->canonical)
2442 {
2443 char *copy = savestring (saved_arg, q - saved_arg);
2444
2445 self->canonical->pre_expanded = 1;
2446 gdb_assert (self->user_filename || use_default);
2447 self->canonical->addr_string
2448 = xstrprintf ("%s:%s", (self->user_filename
2449 ? self->user_filename
2450 : self->default_symtab->filename),
2451 copy);
2452 xfree (copy);
2453 }
2454
84fba31b
DC
2455 return values;
2456}
f3c39e76 2457
614b3b14
DC
2458\f
2459
14e91ac5
DC
2460/* Decode a linespec starting with a dollar sign. */
2461
2462static struct symtabs_and_lines
f8eba3c6 2463decode_dollar (struct linespec_state *self, char *copy)
14e91ac5 2464{
4fa62494 2465 LONGEST valx;
14e91ac5 2466 int index = 0;
14e91ac5
DC
2467 struct symtabs_and_lines values;
2468 struct symtab_and_line val;
2469 char *p;
2470 struct symbol *sym;
14e91ac5 2471 struct minimal_symbol *msymbol;
f8eba3c6
TT
2472 int ix;
2473 struct symtab *elt;
14e91ac5
DC
2474
2475 p = (copy[1] == '$') ? copy + 2 : copy + 1;
2476 while (*p >= '0' && *p <= '9')
2477 p++;
a04257e6 2478 if (!*p) /* Reached end of token without hitting non-digit. */
14e91ac5 2479 {
a04257e6 2480 /* We have a value history reference. */
4fa62494 2481 struct value *val_history;
e0881a8e 2482
14e91ac5 2483 sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
4fa62494
UW
2484 val_history = access_value_history ((copy[1] == '$') ? -index : index);
2485 if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
3e43a32a
MS
2486 error (_("History values used in line "
2487 "specs must have integer values."));
4fa62494 2488 valx = value_as_long (val_history);
14e91ac5
DC
2489 }
2490 else
2491 {
2492 /* Not all digits -- may be user variable/function or a
a04257e6 2493 convenience variable. */
14e91ac5 2494
f8eba3c6
TT
2495 volatile struct gdb_exception exc;
2496
2497 TRY_CATCH (exc, RETURN_MASK_ERROR)
2498 {
2499 values = decode_variable (self, copy);
2500 }
2501
2502 if (exc.reason == 0)
2503 return values;
14e91ac5 2504
f8eba3c6
TT
2505 if (exc.error != NOT_FOUND_ERROR)
2506 throw_exception (exc);
14e91ac5 2507
a04257e6 2508 /* Not a user variable or function -- must be convenience variable. */
4fa62494 2509 if (!get_internalvar_integer (lookup_internalvar (copy + 1), &valx))
3e43a32a
MS
2510 error (_("Convenience variables used in line "
2511 "specs must have integer values."));
14e91ac5
DC
2512 }
2513
2514 init_sal (&val);
2515
f8eba3c6
TT
2516 values.sals = NULL;
2517 values.nelts = 0;
14e91ac5 2518
f8eba3c6
TT
2519 for (ix = 0; VEC_iterate (symtab_p, self->file_symtabs, ix, elt); ++ix)
2520 {
2521 if (elt == NULL)
2522 {
2523 elt = self->default_symtab;
2524 set_current_program_space (self->program_space);
2525 }
2526 else
2527 set_current_program_space (SYMTAB_PSPACE (elt));
2528
2529 /* Either history value or convenience value from above, in valx. */
2530 val.symtab = elt;
2531 val.line = valx;
2532 val.pc = 0;
2533 val.pspace = elt ? SYMTAB_PSPACE (elt) : current_program_space;
2534
2535 add_sal_to_sals (self, &values, &val, NULL);
2536 }
14e91ac5 2537
f8eba3c6
TT
2538 if (self->canonical)
2539 {
2540 self->canonical->pre_expanded = 1;
2541 if (self->user_filename)
2542 self->canonical->addr_string = xstrprintf ("%s:%s",
2543 self->user_filename, copy);
2544 else
2545 self->canonical->addr_string = xstrdup (copy);
2546 }
14e91ac5
DC
2547
2548 return values;
2549}
2550
bca02a8a
DC
2551\f
2552
0f5238ed
TT
2553/* A helper for decode_line_1 that tries to find a label. The label
2554 is searched for in the current block.
f8eba3c6 2555 FUNCTION_SYMBOLS is a list of the enclosing functions; or NULL if none
9ef07c8c 2556 specified.
0f5238ed
TT
2557 COPY is the name of the label to find.
2558 CANONICAL is the same as the "canonical" argument to decode_line_1.
2559 RESULT is a pointer to a symtabs_and_lines structure which will be
2560 filled in on success.
2561 This function returns 1 if a label was found, 0 otherwise. */
2562
2563static int
f8eba3c6
TT
2564decode_label (struct linespec_state *self,
2565 VEC (symbolp) *function_symbols, char *copy,
7efd8fc2 2566 struct symtabs_and_lines *result)
0f5238ed 2567{
f8eba3c6
TT
2568 struct symbol *fn_sym;
2569 int ix;
9ef07c8c 2570
f8eba3c6 2571 if (function_symbols == NULL)
9ef07c8c 2572 {
f8eba3c6
TT
2573 struct block *block;
2574 struct symbol *sym;
2575 struct symtab_and_line sal;
2576 struct symtabs_and_lines values;
2577
2578 values.nelts = 0;
2579 values.sals = NULL;
2580
2581 set_current_program_space (self->program_space);
2582 block = get_search_block (NULL);
2583
9ef07c8c
TT
2584 for (;
2585 block && !BLOCK_FUNCTION (block);
2586 block = BLOCK_SUPERBLOCK (block))
2587 ;
2588 if (!block)
2589 return 0;
f8eba3c6
TT
2590 fn_sym = BLOCK_FUNCTION (block);
2591
2592 sym = lookup_symbol (copy, block, LABEL_DOMAIN, 0);
2593
2594 if (sym == NULL)
2595 return 0;
2596
2597 symbol_to_sal (&sal, self->funfirstline, sym);
2598 add_sal_to_sals (self, &values, &sal,
2599 SYMBOL_NATURAL_NAME (fn_sym));
2600
2601 if (self->canonical)
2602 {
2603 self->canonical->special_display = 1;
2604 self->canonical->addr_string
2605 = xstrprintf ("%s:%s", SYMBOL_NATURAL_NAME (fn_sym),
2606 copy);
2607 }
2608
2609 *result = values;
2610
2611 return 1;
2612 }
2613
2614 result->sals = NULL;
2615 result->nelts = 0;
2616
2617 for (ix = 0; VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
2618 {
2619 struct block *block;
2620 struct symbol *sym;
2621
2622 set_current_program_space (SYMTAB_PSPACE (SYMBOL_SYMTAB (fn_sym)));
2623 block = SYMBOL_BLOCK_VALUE (fn_sym);
2624 sym = lookup_symbol (copy, block, LABEL_DOMAIN, 0);
2625
2626 if (sym != NULL)
2627 {
2628 struct symtab_and_line sal;
2629 char *symname;
2630
2631 symbol_to_sal (&sal, self->funfirstline, sym);
2632 symname = xstrprintf ("%s:%s",
2633 SYMBOL_NATURAL_NAME (fn_sym),
2634 SYMBOL_NATURAL_NAME (sym));
2635 add_sal_to_sals (self, result, &sal, symname);
2636 xfree (symname);
2637 }
2638 }
2639
2640 if (self->canonical && result->nelts > 0)
2641 {
2642 self->canonical->pre_expanded = 1;
2643 self->canonical->special_display = 1;
2644
2645 gdb_assert (self->user_function);
2646 self->canonical->addr_string
2647 = xstrprintf ("%s:%s", self->user_function, copy);
2648 }
2649
2650 return result->nelts > 0;
2651}
2652
2653/* A callback used to possibly add a symbol to the results. */
2654
2655static int
2656collect_symbols (struct symbol *sym, void *data)
2657{
2658 struct collect_info *info = data;
2659 struct symtab_and_line sal;
2660
2661 if ((SYMBOL_CLASS (sym) == LOC_STATIC
2662 && !info->state->funfirstline
2663 && !maybe_add_address (info->state->addr_set,
2664 SYMTAB_PSPACE (SYMBOL_SYMTAB (sym)),
2665 SYMBOL_VALUE_ADDRESS (sym)))
2666 || (SYMBOL_CLASS (sym) == LOC_BLOCK
2667 && !maybe_add_address (info->state->addr_set,
2668 SYMTAB_PSPACE (SYMBOL_SYMTAB (sym)),
2669 BLOCK_START (SYMBOL_BLOCK_VALUE (sym)))))
2670 {
2671 /* Nothing. */
2672 }
2673 else if (symbol_to_sal (&sal, info->state->funfirstline, sym))
2674 add_sal_to_sals (info->state, &info->result, &sal,
2675 SYMBOL_NATURAL_NAME (sym));
2676
2677 return 1;
2678}
2679
2680/* We've found a minimal symbol MSYMBOL to associate with our
2681 linespec; add it to the result symtabs_and_lines. */
2682
2683static void
2684minsym_found (struct linespec_state *self, struct objfile *objfile,
2685 struct minimal_symbol *msymbol,
2686 struct symtabs_and_lines *result)
2687{
2688 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2689 CORE_ADDR pc;
2690 struct symtab_and_line sal;
2691
2692 sal = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
2693 (struct obj_section *) 0, 0);
2694 sal.section = SYMBOL_OBJ_SECTION (msymbol);
2695
2696 /* The minimal symbol might point to a function descriptor;
2697 resolve it to the actual code address instead. */
2698 pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
2699 if (pc != sal.pc)
2700 sal = find_pc_sect_line (pc, NULL, 0);
2701
2702 if (self->funfirstline)
2703 skip_prologue_sal (&sal);
2704
2705 add_sal_to_sals (self, result, &sal, SYMBOL_NATURAL_NAME (msymbol));
2706}
2707
2708/* Callback for iterate_over_minimal_symbols that may add the symbol
2709 to the result. */
2710
2711static void
2712check_minsym (struct minimal_symbol *minsym, void *d)
2713{
2714 struct collect_info *info = d;
2715
2716 if (MSYMBOL_TYPE (minsym) == mst_unknown
2717 || MSYMBOL_TYPE (minsym) == mst_slot_got_plt
2718 || MSYMBOL_TYPE (minsym) == mst_solib_trampoline)
2719 {
2720 /* Reject some odd ones. */
2721 }
2722 else if (info->state->funfirstline
2723 && MSYMBOL_TYPE (minsym) != mst_text
2724 && MSYMBOL_TYPE (minsym) != mst_text_gnu_ifunc
2725 && MSYMBOL_TYPE (minsym) != mst_file_text)
2726 {
2727 /* When FUNFIRSTLINE, only allow text symbols. */
2728 }
2729 else if (maybe_add_address (info->state->addr_set, info->objfile->pspace,
2730 SYMBOL_VALUE_ADDRESS (minsym)))
2731 minsym_found (info->state, info->objfile, minsym, &info->result);
2732}
2733
2734/* Search minimal symbols in all objfiles for NAME. If SEARCH_PSPACE
2735 is not NULL, the search is restricted to just that program
2736 space. */
2737
2738static void
2739search_minsyms_for_name (struct collect_info *info, const char *name,
2740 struct program_space *search_pspace)
2741{
2742 struct objfile *objfile;
2743 struct program_space *pspace;
2744
2745 ALL_PSPACES (pspace)
2746 {
2747 if (search_pspace != NULL && search_pspace != pspace)
2748 continue;
2749 if (pspace->executing_startup)
2750 continue;
2751
2752 set_current_program_space (pspace);
2753
2754 ALL_OBJFILES (objfile)
2755 {
2756 info->objfile = objfile;
2757 iterate_over_minimal_symbols (objfile, name, check_minsym, info);
9ef07c8c 2758 }
f8eba3c6
TT
2759 }
2760}
2761
2762/* A helper function to add all symbols matching NAME to INFO. If
2763 PSPACE is not NULL, the search is restricted to just that program
2764 space. */
0f5238ed 2765
f8eba3c6
TT
2766static void
2767add_matching_symbols_to_info (const char *name,
2768 struct collect_info *info,
2769 struct program_space *pspace)
2770{
2771 int ix;
2772 struct symtab *elt;
0f5238ed 2773
f8eba3c6
TT
2774 for (ix = 0; VEC_iterate (symtab_p, info->state->file_symtabs, ix, elt); ++ix)
2775 {
2776 struct symbol *sym;
0f5238ed 2777
f8eba3c6
TT
2778 if (elt == NULL)
2779 {
2780 iterate_over_all_matching_symtabs (name, VAR_DOMAIN,
2781 collect_symbols, info,
2782 pspace);
2783 search_minsyms_for_name (info, name, pspace);
2784 }
2785 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
2786 {
2787 /* Program spaces that are executing startup should have
2788 been filtered out earlier. */
2789 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
2790 set_current_program_space (SYMTAB_PSPACE (elt));
2791 LA_ITERATE_OVER_SYMBOLS (get_search_block (elt), name,
2792 VAR_DOMAIN, collect_symbols,
2793 info);
2794 }
2795 }
0f5238ed
TT
2796}
2797
88d262ca 2798/* Decode a linespec that's a variable. If FILE_SYMTAB is non-NULL,
58438ac1 2799 look in that symtab's static variables first. */
bca02a8a
DC
2800
2801static struct symtabs_and_lines
f8eba3c6 2802decode_variable (struct linespec_state *self, char *copy)
bca02a8a 2803{
f8eba3c6
TT
2804 struct collect_info info;
2805 const char *lookup_name;
2806 char *canon;
3a93a0c2 2807 struct cleanup *cleanup;
bca02a8a 2808
f8eba3c6
TT
2809 info.state = self;
2810 info.result.sals = NULL;
2811 info.result.nelts = 0;
2812 info.objfile = NULL;
2813
2814 cleanup = demangle_for_lookup (copy, current_language->la_language,
2815 &lookup_name);
2816 if (current_language->la_language == language_ada)
3a93a0c2 2817 {
f8eba3c6
TT
2818 /* In Ada, the symbol lookups are performed using the encoded
2819 name rather than the demangled name. */
2820 lookup_name = ada_name_for_lookup (copy);
2821 make_cleanup (xfree, (void *) lookup_name);
3a93a0c2
KS
2822 }
2823
f8eba3c6
TT
2824 canon = cp_canonicalize_string_no_typedefs (lookup_name);
2825 if (canon != NULL)
3a93a0c2 2826 {
f8eba3c6
TT
2827 make_cleanup (xfree, canon);
2828 lookup_name = canon;
3a93a0c2 2829 }
bca02a8a 2830
f8eba3c6 2831 add_matching_symbols_to_info (lookup_name, &info, NULL);
bca02a8a 2832
f8eba3c6
TT
2833 if (info.result.nelts > 0)
2834 {
2835 if (self->canonical)
2836 {
2837 self->canonical->pre_expanded = 1;
2838 if (self->user_filename)
2839 self->canonical->addr_string
2840 = xstrprintf ("%s:%s", self->user_filename, copy);
2841 else
2842 self->canonical->addr_string = xstrdup (copy);
2843 }
2844 return info.result;
2845 }
bca02a8a 2846
b96e2927
PA
2847 if (!have_full_symbols ()
2848 && !have_partial_symbols ()
2849 && !have_minimal_symbols ())
2850 throw_error (NOT_FOUND_ERROR,
2851 _("No symbol table is loaded. Use the \"file\" command."));
f8eba3c6
TT
2852 if (self->user_filename)
2853 throw_error (NOT_FOUND_ERROR, _("Function \"%s\" not defined in \"%s\"."),
2854 copy, self->user_filename);
2855 else
2856 throw_error (NOT_FOUND_ERROR, _("Function \"%s\" not defined."), copy);
bca02a8a
DC
2857}
2858
2859
14e91ac5
DC
2860\f
2861
413dad4d
DC
2862/* Now come some functions that are called from multiple places within
2863 decode_line_1. */
2864
f8eba3c6
TT
2865static int
2866symbol_to_sal (struct symtab_and_line *result,
2867 int funfirstline, struct symbol *sym)
413dad4d 2868{
413dad4d 2869 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 2870 {
f8eba3c6
TT
2871 *result = find_function_start_sal (sym, funfirstline);
2872 return 1;
50641945 2873 }
413dad4d
DC
2874 else
2875 {
62853458 2876 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
413dad4d 2877 {
f8eba3c6
TT
2878 init_sal (result);
2879 result->symtab = SYMBOL_SYMTAB (sym);
2880 result->line = SYMBOL_LINE (sym);
2881 result->pc = SYMBOL_VALUE_ADDRESS (sym);
2882 result->pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
2883 result->explicit_pc = 1;
2884 return 1;
413dad4d 2885 }
62853458 2886 else if (funfirstline)
dcf9f4ab 2887 {
f8eba3c6 2888 /* Nothing. */
dcf9f4ab 2889 }
62853458
TT
2890 else if (SYMBOL_LINE (sym) != 0)
2891 {
2892 /* We know its line number. */
f8eba3c6
TT
2893 init_sal (result);
2894 result->symtab = SYMBOL_SYMTAB (sym);
2895 result->line = SYMBOL_LINE (sym);
2896 result->pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
2897 return 1;
62853458 2898 }
413dad4d 2899 }
f8eba3c6
TT
2900
2901 return 0;
413dad4d 2902}
50641945 2903
f8eba3c6 2904/* See the comment in linespec.h. */
50641945 2905
f8eba3c6
TT
2906void
2907init_linespec_result (struct linespec_result *lr)
413dad4d 2908{
f8eba3c6
TT
2909 memset (lr, 0, sizeof (*lr));
2910}
413dad4d 2911
f8eba3c6 2912/* See the comment in linespec.h. */
bccdca4a 2913
f8eba3c6
TT
2914void
2915destroy_linespec_result (struct linespec_result *ls)
2916{
2917 int i;
2918 struct linespec_sals *lsal;
bccdca4a 2919
f8eba3c6
TT
2920 xfree (ls->addr_string);
2921 for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
2922 {
2923 xfree (lsal->canonical);
2924 xfree (lsal->sals.sals);
2925 }
2926 VEC_free (linespec_sals, ls->sals);
2927}
e48883f7 2928
f8eba3c6
TT
2929/* Cleanup function for a linespec_result. */
2930
2931static void
2932cleanup_linespec_result (void *a)
2933{
2934 destroy_linespec_result (a);
50641945 2935}
7efd8fc2 2936
f8eba3c6
TT
2937/* See the comment in linespec.h. */
2938
2939struct cleanup *
2940make_cleanup_destroy_linespec_result (struct linespec_result *ls)
7efd8fc2 2941{
f8eba3c6 2942 return make_cleanup (cleanup_linespec_result, ls);
7efd8fc2 2943}
This page took 1.06322 seconds and 4 git commands to generate.