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