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