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