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