+ }
+
+ return parser->lexer.current;
+}
+
+/* Consume the current token and return the next token in PARSER's
+ input stream. */
+
+static linespec_token
+linespec_lexer_consume_token (linespec_parser *parser)
+{
+ parser->lexer.current.type = LSTOKEN_CONSUMED;
+ return linespec_lexer_lex_one (parser);
+}
+
+/* Return the next token without consuming the current token. */
+
+static linespec_token
+linespec_lexer_peek_token (linespec_parser *parser)
+{
+ linespec_token next;
+ const char *saved_stream = PARSER_STREAM (parser);
+ linespec_token saved_token = parser->lexer.current;
+
+ next = linespec_lexer_consume_token (parser);
+ PARSER_STREAM (parser) = saved_stream;
+ parser->lexer.current = saved_token;
+ return next;
+}
+
+/* Helper functions. */
+
+/* Add SAL to SALS. */
+
+static void
+add_sal_to_sals_basic (struct symtabs_and_lines *sals,
+ struct symtab_and_line *sal)
+{
+ ++sals->nelts;
+ sals->sals = xrealloc (sals->sals, sals->nelts * sizeof (sals->sals[0]));
+ sals->sals[sals->nelts - 1] = *sal;
+}
+
+/* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
+ the new sal, if needed. If not NULL, SYMNAME is the name of the
+ symbol to use when constructing the new canonical name.
+
+ If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
+ canonical name for the SAL. */
+
+static void
+add_sal_to_sals (struct linespec_state *self,
+ struct symtabs_and_lines *sals,
+ struct symtab_and_line *sal,
+ const char *symname, int literal_canonical)
+{
+ add_sal_to_sals_basic (sals, sal);
+
+ if (self->canonical)
+ {
+ struct linespec_canonical_name *canonical;
+
+ self->canonical_names = xrealloc (self->canonical_names,
+ (sals->nelts
+ * sizeof (*self->canonical_names)));
+ canonical = &self->canonical_names[sals->nelts - 1];
+ if (!literal_canonical && sal->symtab)
+ {
+ const char *fullname = symtab_to_fullname (sal->symtab);
+
+ /* Note that the filter doesn't have to be a valid linespec
+ input. We only apply the ":LINE" treatment to Ada for
+ the time being. */
+ if (symname != NULL && sal->line != 0
+ && self->language->la_language == language_ada)
+ canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
+ else if (symname != NULL)
+ canonical->suffix = xstrdup (symname);
+ else
+ canonical->suffix = xstrprintf ("%d", sal->line);
+ canonical->symtab = sal->symtab;
+ }
+ else
+ {
+ if (symname != NULL)
+ canonical->suffix = xstrdup (symname);
+ else
+ canonical->suffix = xstrdup ("<unknown>");
+ canonical->symtab = NULL;
+ }
+ }
+}
+
+/* A hash function for address_entry. */
+
+static hashval_t
+hash_address_entry (const void *p)
+{
+ const struct address_entry *aep = p;
+ hashval_t hash;
+
+ hash = iterative_hash_object (aep->pspace, 0);
+ return iterative_hash_object (aep->addr, hash);
+}
+
+/* An equality function for address_entry. */
+
+static int
+eq_address_entry (const void *a, const void *b)
+{
+ const struct address_entry *aea = a;
+ const struct address_entry *aeb = b;
+
+ return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
+}
+
+/* Check whether the address, represented by PSPACE and ADDR, is
+ already in the set. If so, return 0. Otherwise, add it and return
+ 1. */
+
+static int
+maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
+{
+ struct address_entry e, *p;
+ void **slot;
+
+ e.pspace = pspace;
+ e.addr = addr;
+ slot = htab_find_slot (set, &e, INSERT);
+ if (*slot)
+ return 0;
+
+ p = XNEW (struct address_entry);
+ memcpy (p, &e, sizeof (struct address_entry));
+ *slot = p;
+
+ return 1;
+}
+
+/* A callback function and the additional data to call it with. */
+
+struct symbol_and_data_callback
+{
+ /* The callback to use. */
+ symbol_found_callback_ftype *callback;
+
+ /* Data to be passed to the callback. */
+ void *data;
+};
+
+/* A helper for iterate_over_all_matching_symtabs that is used to
+ restrict calls to another callback to symbols representing inline
+ symbols only. */
+
+static int
+iterate_inline_only (struct symbol *sym, void *d)
+{
+ if (SYMBOL_INLINED (sym))
+ {
+ struct symbol_and_data_callback *cad = d;
+
+ return cad->callback (sym, cad->data);
+ }
+ return 1; /* Continue iterating. */
+}
+
+/* Some data for the expand_symtabs_matching callback. */
+
+struct symbol_matcher_data
+{
+ /* The lookup name against which symbol name should be compared. */
+ const char *lookup_name;
+
+ /* The routine to be used for comparison. */
+ symbol_name_cmp_ftype symbol_name_cmp;
+};
+
+/* A helper for iterate_over_all_matching_symtabs that is passed as a
+ callback to the expand_symtabs_matching method. */
+
+static int
+iterate_name_matcher (const char *name, void *d)
+{
+ const struct symbol_matcher_data *data = d;
+
+ if (data->symbol_name_cmp (name, data->lookup_name) == 0)
+ return 1; /* Expand this symbol's symbol table. */
+ return 0; /* Skip this symbol. */
+}
+
+/* A helper that walks over all matching symtabs in all objfiles and
+ calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
+ not NULL, then the search is restricted to just that program
+ space. If INCLUDE_INLINE is nonzero then symbols representing
+ inlined instances of functions will be included in the result. */
+
+static void
+iterate_over_all_matching_symtabs (struct linespec_state *state,
+ const char *name,
+ const domain_enum domain,
+ symbol_found_callback_ftype *callback,
+ void *data,
+ struct program_space *search_pspace,
+ int include_inline)
+{
+ struct objfile *objfile;
+ struct program_space *pspace;
+ struct symbol_matcher_data matcher_data;
+
+ matcher_data.lookup_name = name;
+ matcher_data.symbol_name_cmp =
+ state->language->la_get_symbol_name_cmp != NULL
+ ? state->language->la_get_symbol_name_cmp (name)
+ : strcmp_iw;
+
+ ALL_PSPACES (pspace)
+ {
+ if (search_pspace != NULL && search_pspace != pspace)
+ continue;
+ if (pspace->executing_startup)
+ continue;
+
+ set_current_program_space (pspace);
+
+ ALL_OBJFILES (objfile)
+ {
+ struct symtab *symtab;
+
+ if (objfile->sf)
+ objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
+ iterate_name_matcher,
+ ALL_DOMAIN,
+ &matcher_data);
+
+ ALL_OBJFILE_PRIMARY_SYMTABS (objfile, symtab)
+ {
+ iterate_over_file_blocks (symtab, name, domain, callback, data);
+
+ if (include_inline)
+ {
+ struct symbol_and_data_callback cad = { callback, data };
+ struct block *block;
+ int i;
+
+ for (i = FIRST_LOCAL_BLOCK;
+ i < BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (symtab)); i++)
+ {
+ block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), i);
+ state->language->la_iterate_over_symbols
+ (block, name, domain, iterate_inline_only, &cad);
+ }
+ }
+ }
+ }
+ }
+}
+
+/* Returns the block to be used for symbol searches from
+ the current location. */
+
+static const struct block *
+get_current_search_block (void)
+{
+ const struct block *block;
+ enum language save_language;
+
+ /* get_selected_block can change the current language when there is
+ no selected frame yet. */
+ save_language = current_language->la_language;
+ block = get_selected_block (0);
+ set_language (save_language);
+
+ return block;
+}
+
+/* Iterate over static and global blocks. */
+
+static void
+iterate_over_file_blocks (struct symtab *symtab,
+ const char *name, domain_enum domain,
+ symbol_found_callback_ftype *callback, void *data)
+{
+ struct block *block;
+
+ for (block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
+ block != NULL;
+ block = BLOCK_SUPERBLOCK (block))
+ LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
+}
+
+/* A helper for find_method. This finds all methods in type T which
+ match NAME. It adds matching symbol names to RESULT_NAMES, and
+ adds T's direct superclasses to SUPERCLASSES. */
+
+static void
+find_methods (struct type *t, const char *name,
+ VEC (const_char_ptr) **result_names,
+ VEC (typep) **superclasses)
+{
+ int ibase;
+ const char *class_name = type_name_no_tag (t);
+
+ /* Ignore this class if it doesn't have a name. This is ugly, but
+ unless we figure out how to get the physname without the name of
+ the class, then the loop can't do any good. */
+ if (class_name)
+ {
+ int method_counter;
+
+ CHECK_TYPEDEF (t);
+
+ /* Loop over each method name. At this level, all overloads of a name
+ are counted as a single name. There is an inner loop which loops over
+ each overload. */
+
+ for (method_counter = TYPE_NFN_FIELDS (t) - 1;
+ method_counter >= 0;
+ --method_counter)
+ {
+ const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
+ char dem_opname[64];
+
+ if (strncmp (method_name, "__", 2) == 0 ||
+ strncmp (method_name, "op", 2) == 0 ||
+ strncmp (method_name, "type", 4) == 0)
+ {
+ if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
+ method_name = dem_opname;
+ else if (cplus_demangle_opname (method_name, dem_opname, 0))
+ method_name = dem_opname;
+ }
+
+ if (strcmp_iw (method_name, name) == 0)
+ {
+ int field_counter;
+
+ for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
+ - 1);
+ field_counter >= 0;
+ --field_counter)
+ {
+ struct fn_field *f;
+ const char *phys_name;
+
+ f = TYPE_FN_FIELDLIST1 (t, method_counter);
+ if (TYPE_FN_FIELD_STUB (f, field_counter))
+ continue;
+ phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
+ VEC_safe_push (const_char_ptr, *result_names, phys_name);
+ }
+ }
+ }
+ }
+
+ for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
+ VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
+}
+
+/* Find an instance of the character C in the string S that is outside
+ of all parenthesis pairs, single-quoted strings, and double-quoted
+ strings. Also, ignore the char within a template name, like a ','
+ within foo<int, int>. */
+
+static const char *
+find_toplevel_char (const char *s, char c)
+{
+ int quoted = 0; /* zero if we're not in quotes;
+ '"' if we're in a double-quoted string;
+ '\'' if we're in a single-quoted string. */
+ int depth = 0; /* Number of unclosed parens we've seen. */
+ const char *scan;
+
+ for (scan = s; *scan; scan++)
+ {
+ if (quoted)
+ {
+ if (*scan == quoted)
+ quoted = 0;
+ else if (*scan == '\\' && *(scan + 1))
+ scan++;
+ }
+ else if (*scan == c && ! quoted && depth == 0)
+ return scan;
+ else if (*scan == '"' || *scan == '\'')
+ quoted = *scan;
+ else if (*scan == '(' || *scan == '<')
+ depth++;
+ else if ((*scan == ')' || *scan == '>') && depth > 0)
+ depth--;
+ }
+
+ return 0;
+}
+
+/* The string equivalent of find_toplevel_char. Returns a pointer
+ to the location of NEEDLE in HAYSTACK, ignoring any occurrences
+ inside "()" and "<>". Returns NULL if NEEDLE was not found. */
+
+static const char *
+find_toplevel_string (const char *haystack, const char *needle)
+{
+ const char *s = haystack;
+
+ do
+ {
+ s = find_toplevel_char (s, *needle);
+
+ if (s != NULL)
+ {
+ /* Found first char in HAYSTACK; check rest of string. */
+ if (strncmp (s, needle, strlen (needle)) == 0)
+ return s;
+
+ /* Didn't find it; loop over HAYSTACK, looking for the next
+ instance of the first character of NEEDLE. */
+ ++s;
+ }
+ }
+ while (s != NULL && *s != '\0');
+
+ /* NEEDLE was not found in HAYSTACK. */
+ return NULL;
+}
+
+/* Convert CANONICAL to its string representation using
+ symtab_to_fullname for SYMTAB. The caller must xfree the result. */
+
+static char *
+canonical_to_fullform (const struct linespec_canonical_name *canonical)
+{
+ if (canonical->symtab == NULL)
+ return xstrdup (canonical->suffix);
+ else
+ return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
+ canonical->suffix);
+}
+
+/* Given FILTERS, a list of canonical names, filter the sals in RESULT
+ and store the result in SELF->CANONICAL. */
+
+static void
+filter_results (struct linespec_state *self,
+ struct symtabs_and_lines *result,
+ VEC (const_char_ptr) *filters)
+{
+ int i;
+ const char *name;
+
+ for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
+ {
+ struct linespec_sals lsal;
+ int j;
+
+ memset (&lsal, 0, sizeof (lsal));
+
+ for (j = 0; j < result->nelts; ++j)
+ {
+ const struct linespec_canonical_name *canonical;
+ char *fullform;
+ struct cleanup *cleanup;
+
+ canonical = &self->canonical_names[j];
+ fullform = canonical_to_fullform (canonical);
+ cleanup = make_cleanup (xfree, fullform);
+
+ if (strcmp (name, fullform) == 0)
+ add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);
+
+ do_cleanups (cleanup);
+ }
+
+ if (lsal.sals.nelts > 0)
+ {
+ lsal.canonical = xstrdup (name);
+ VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
+ }
+ }
+
+ self->canonical->pre_expanded = 0;
+}
+
+/* Store RESULT into SELF->CANONICAL. */
+
+static void
+convert_results_to_lsals (struct linespec_state *self,
+ struct symtabs_and_lines *result)
+{
+ struct linespec_sals lsal;
+
+ lsal.canonical = NULL;
+ lsal.sals = *result;
+ VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
+}
+
+/* A structure that contains two string representations of a struct
+ linespec_canonical_name:
+ - one where the the symtab's fullname is used;
+ - one where the filename followed the "set filename-display"
+ setting. */
+
+struct decode_line_2_item
+{
+ /* The form using symtab_to_fullname.
+ It must be xfree'ed after use. */
+ char *fullform;
+
+ /* The form using symtab_to_filename_for_display.
+ It must be xfree'ed after use. */
+ char *displayform;
+
+ /* Field is initialized to zero and it is set to one if the user
+ requested breakpoint for this entry. */
+ unsigned int selected : 1;
+};
+
+/* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
+ secondarily by FULLFORM. */
+
+static int
+decode_line_2_compare_items (const void *ap, const void *bp)
+{
+ const struct decode_line_2_item *a = ap;
+ const struct decode_line_2_item *b = bp;
+ int retval;
+
+ retval = strcmp (a->displayform, b->displayform);
+ if (retval != 0)
+ return retval;
+
+ return strcmp (a->fullform, b->fullform);
+}
+
+/* Handle multiple results in RESULT depending on SELECT_MODE. This
+ will either return normally, throw an exception on multiple
+ results, or present a menu to the user. On return, the SALS vector
+ in SELF->CANONICAL is set up properly. */
+
+static void
+decode_line_2 (struct linespec_state *self,
+ struct symtabs_and_lines *result,
+ const char *select_mode)
+{
+ char *args, *prompt;
+ int i;
+ struct cleanup *old_chain;
+ VEC (const_char_ptr) *filters = NULL;
+ struct get_number_or_range_state state;
+ struct decode_line_2_item *items;
+ int items_count;
+
+ gdb_assert (select_mode != multiple_symbols_all);
+ gdb_assert (self->canonical != NULL);
+ gdb_assert (result->nelts >= 1);
+
+ old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
+
+ /* Prepare ITEMS array. */
+ items_count = result->nelts;
+ items = xmalloc (sizeof (*items) * items_count);
+ make_cleanup (xfree, items);
+ for (i = 0; i < items_count; ++i)
+ {
+ const struct linespec_canonical_name *canonical;
+ struct decode_line_2_item *item;
+
+ canonical = &self->canonical_names[i];
+ gdb_assert (canonical->suffix != NULL);
+ item = &items[i];
+
+ item->fullform = canonical_to_fullform (canonical);
+ make_cleanup (xfree, item->fullform);
+
+ if (canonical->symtab == NULL)
+ item->displayform = canonical->suffix;
+ else
+ {
+ const char *fn_for_display;
+
+ fn_for_display = symtab_to_filename_for_display (canonical->symtab);
+ item->displayform = xstrprintf ("%s:%s", fn_for_display,
+ canonical->suffix);
+ make_cleanup (xfree, item->displayform);
+ }
+
+ item->selected = 0;
+ }
+
+ /* Sort the list of method names. */
+ qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
+
+ /* Remove entries with the same FULLFORM. */
+ if (items_count >= 2)
+ {
+ struct decode_line_2_item *dst, *src;
+
+ dst = items;
+ for (src = &items[1]; src < &items[items_count]; src++)
+ if (strcmp (src->fullform, dst->fullform) != 0)
+ *++dst = *src;
+ items_count = dst + 1 - items;
+ }
+
+ if (select_mode == multiple_symbols_cancel && items_count > 1)
+ error (_("canceled because the command is ambiguous\n"
+ "See set/show multiple-symbol."));
+
+ if (select_mode == multiple_symbols_all || items_count == 1)
+ {
+ do_cleanups (old_chain);
+ convert_results_to_lsals (self, result);
+ return;
+ }
+
+ printf_unfiltered (_("[0] cancel\n[1] all\n"));
+ for (i = 0; i < items_count; i++)
+ printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);
+
+ prompt = getenv ("PS2");
+ if (prompt == NULL)
+ {
+ prompt = "> ";
+ }
+ args = command_line_input (prompt, 0, "overload-choice");
+
+ if (args == 0 || *args == 0)
+ error_no_arg (_("one or more choice numbers"));
+
+ init_number_or_range (&state, args);
+ while (!state.finished)
+ {
+ int num;
+
+ num = get_number_or_range (&state);
+
+ if (num == 0)
+ error (_("canceled"));
+ else if (num == 1)
+ {
+ /* We intentionally make this result in a single breakpoint,
+ contrary to what older versions of gdb did. The
+ rationale is that this lets a user get the
+ multiple_symbols_all behavior even with the 'ask'
+ setting; and he can get separate breakpoints by entering
+ "2-57" at the query. */
+ do_cleanups (old_chain);
+ convert_results_to_lsals (self, result);
+ return;
+ }
+
+ num -= 2;
+ if (num >= items_count)
+ printf_unfiltered (_("No choice number %d.\n"), num);
+ else
+ {
+ struct decode_line_2_item *item = &items[num];
+
+ if (!item->selected)
+ {
+ VEC_safe_push (const_char_ptr, filters, item->fullform);
+ item->selected = 1;
+ }
+ else
+ {
+ printf_unfiltered (_("duplicate request for %d ignored.\n"),
+ num + 2);
+ }
+ }
+ }
+
+ filter_results (self, result, filters);
+ do_cleanups (old_chain);
+}
+
+\f
+
+/* The parser of linespec itself. */
+
+/* Throw an appropriate error when SYMBOL is not found (optionally in
+ FILENAME). */
+
+static void ATTRIBUTE_NORETURN
+symbol_not_found_error (const char *symbol, const char *filename)
+{
+ if (symbol == NULL)
+ symbol = "";
+
+ if (!have_full_symbols ()
+ && !have_partial_symbols ()
+ && !have_minimal_symbols ())
+ throw_error (NOT_FOUND_ERROR,
+ _("No symbol table is loaded. Use the \"file\" command."));
+
+ /* If SYMBOL starts with '$', the user attempted to either lookup
+ a function/variable in his code starting with '$' or an internal
+ variable of that name. Since we do not know which, be concise and
+ explain both possibilities. */
+ if (*symbol == '$')
+ {
+ if (filename)
+ throw_error (NOT_FOUND_ERROR,
+ _("Undefined convenience variable or function \"%s\" "
+ "not defined in \"%s\"."), symbol, filename);
+ else
+ throw_error (NOT_FOUND_ERROR,
+ _("Undefined convenience variable or function \"%s\" "
+ "not defined."), symbol);
+ }
+ else
+ {
+ if (filename)
+ throw_error (NOT_FOUND_ERROR,
+ _("Function \"%s\" not defined in \"%s\"."),
+ symbol, filename);
+ else
+ throw_error (NOT_FOUND_ERROR,
+ _("Function \"%s\" not defined."), symbol);
+ }
+}
+
+/* Throw an appropriate error when an unexpected token is encountered
+ in the input. */
+
+static void ATTRIBUTE_NORETURN
+unexpected_linespec_error (linespec_parser *parser)
+{
+ linespec_token token;
+ static const char * token_type_strings[]
+ = {"keyword", "colon", "string", "number", "comma", "end of input"};
+
+ /* Get the token that generated the error. */
+ token = linespec_lexer_lex_one (parser);
+
+ /* Finally, throw the error. */
+ if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
+ || token.type == LSTOKEN_KEYWORD)
+ {
+ char *string;
+ struct cleanup *cleanup;
+
+ string = copy_token_string (token);
+ cleanup = make_cleanup (xfree, string);
+ throw_error (GENERIC_ERROR,
+ _("malformed linespec error: unexpected %s, \"%s\""),
+ token_type_strings[token.type], string);
+ }
+ else
+ throw_error (GENERIC_ERROR,
+ _("malformed linespec error: unexpected %s"),
+ token_type_strings[token.type]);
+}
+
+/* Parse and return a line offset in STRING. */
+
+static struct line_offset
+linespec_parse_line_offset (const char *string)
+{
+ struct line_offset line_offset = {0, LINE_OFFSET_NONE};
+
+ if (*string == '+')
+ {
+ line_offset.sign = LINE_OFFSET_PLUS;
+ ++string;
+ }
+ else if (*string == '-')
+ {
+ line_offset.sign = LINE_OFFSET_MINUS;
+ ++string;
+ }
+
+ /* Right now, we only allow base 10 for offsets. */
+ line_offset.offset = atoi (string);
+ return line_offset;
+}
+
+/* Parse the basic_spec in PARSER's input. */
+
+static void
+linespec_parse_basic (linespec_parser *parser)
+{
+ char *name;
+ linespec_token token;
+ VEC (symbolp) *symbols, *labels;
+ VEC (bound_minimal_symbol_d) *minimal_symbols;
+ struct cleanup *cleanup;
+
+ /* Get the next token. */
+ token = linespec_lexer_lex_one (parser);
+
+ /* If it is EOI or KEYWORD, issue an error. */
+ if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
+ unexpected_linespec_error (parser);
+ /* If it is a LSTOKEN_NUMBER, we have an offset. */
+ else if (token.type == LSTOKEN_NUMBER)
+ {
+ /* Record the line offset and get the next token. */
+ name = copy_token_string (token);
+ cleanup = make_cleanup (xfree, name);
+ PARSER_RESULT (parser)->line_offset = linespec_parse_line_offset (name);
+ do_cleanups (cleanup);
+
+ /* Get the next token. */
+ token = linespec_lexer_consume_token (parser);
+
+ /* If the next token is a comma, stop parsing and return. */
+ if (token.type == LSTOKEN_COMMA)
+ return;
+
+ /* If the next token is anything but EOI or KEYWORD, issue
+ an error. */
+ if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
+ unexpected_linespec_error (parser);
+ }
+
+ if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
+ return;
+
+ /* Next token must be LSTOKEN_STRING. */
+ if (token.type != LSTOKEN_STRING)
+ unexpected_linespec_error (parser);
+
+ /* The current token will contain the name of a function, method,
+ or label. */
+ name = copy_token_string (token);
+ cleanup = make_cleanup (xfree, name);
+
+ /* Try looking it up as a function/method. */
+ find_linespec_symbols (PARSER_STATE (parser),
+ PARSER_RESULT (parser)->file_symtabs, name,
+ &symbols, &minimal_symbols);
+
+ if (symbols != NULL || minimal_symbols != NULL)
+ {
+ PARSER_RESULT (parser)->function_symbols = symbols;
+ PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
+ PARSER_RESULT (parser)->function_name = name;
+ symbols = NULL;
+ discard_cleanups (cleanup);
+ }
+ else
+ {
+ /* NAME was not a function or a method. So it must be a label
+ name or user specified variable like "break foo.c:$zippo". */
+ labels = find_label_symbols (PARSER_STATE (parser), NULL,
+ &symbols, name);
+ if (labels != NULL)
+ {
+ PARSER_RESULT (parser)->labels.label_symbols = labels;
+ PARSER_RESULT (parser)->labels.function_symbols = symbols;
+ PARSER_RESULT (parser)->label_name = name;
+ symbols = NULL;
+ discard_cleanups (cleanup);
+ }
+ else if (token.type == LSTOKEN_STRING
+ && *LS_TOKEN_STOKEN (token).ptr == '$')
+ {
+ /* User specified a convenience variable or history value. */
+ PARSER_RESULT (parser)->line_offset
+ = linespec_parse_variable (PARSER_STATE (parser), name);
+
+ if (PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
+ {
+ /* The user-specified variable was not valid. Do not
+ throw an error here. parse_linespec will do it for us. */
+ PARSER_RESULT (parser)->function_name = name;
+ discard_cleanups (cleanup);
+ return;
+ }
+
+ /* The convenience variable/history value parsed correctly.
+ NAME is no longer needed. */
+ do_cleanups (cleanup);
+ }
+ else
+ {
+ /* The name is also not a label. Abort parsing. Do not throw
+ an error here. parse_linespec will do it for us. */
+
+ /* Save a copy of the name we were trying to lookup. */
+ PARSER_RESULT (parser)->function_name = name;
+ discard_cleanups (cleanup);
+ return;
+ }
+ }
+
+ /* Get the next token. */
+ token = linespec_lexer_consume_token (parser);
+
+ if (token.type == LSTOKEN_COLON)
+ {
+ /* User specified a label or a lineno. */
+ token = linespec_lexer_consume_token (parser);
+
+ if (token.type == LSTOKEN_NUMBER)
+ {
+ /* User specified an offset. Record the line offset and
+ get the next token. */
+ name = copy_token_string (token);
+ cleanup = make_cleanup (xfree, name);
+ PARSER_RESULT (parser)->line_offset
+ = linespec_parse_line_offset (name);
+ do_cleanups (cleanup);
+
+ /* Ge the next token. */
+ token = linespec_lexer_consume_token (parser);
+ }
+ else if (token.type == LSTOKEN_STRING)
+ {
+ /* Grab a copy of the label's name and look it up. */
+ name = copy_token_string (token);
+ cleanup = make_cleanup (xfree, name);
+ labels = find_label_symbols (PARSER_STATE (parser),
+ PARSER_RESULT (parser)->function_symbols,
+ &symbols, name);
+
+ if (labels != NULL)
+ {
+ PARSER_RESULT (parser)->labels.label_symbols = labels;
+ PARSER_RESULT (parser)->labels.function_symbols = symbols;
+ PARSER_RESULT (parser)->label_name = name;
+ symbols = NULL;
+ discard_cleanups (cleanup);
+ }
+ else
+ {
+ /* We don't know what it was, but it isn't a label. */
+ throw_error (NOT_FOUND_ERROR,
+ _("No label \"%s\" defined in function \"%s\"."),
+ name, PARSER_RESULT (parser)->function_name);
+ }
+
+ /* Check for a line offset. */
+ token = linespec_lexer_consume_token (parser);
+ if (token.type == LSTOKEN_COLON)
+ {
+ /* Get the next token. */
+ token = linespec_lexer_consume_token (parser);
+
+ /* It must be a line offset. */
+ if (token.type != LSTOKEN_NUMBER)
+ unexpected_linespec_error (parser);
+
+ /* Record the lione offset and get the next token. */
+ name = copy_token_string (token);
+ cleanup = make_cleanup (xfree, name);
+
+ PARSER_RESULT (parser)->line_offset
+ = linespec_parse_line_offset (name);
+ do_cleanups (cleanup);
+
+ /* Get the next token. */
+ token = linespec_lexer_consume_token (parser);
+ }
+ }
+ else
+ {
+ /* Trailing ':' in the input. Issue an error. */
+ unexpected_linespec_error (parser);
+ }
+ }
+}
+
+/* Canonicalize the linespec contained in LS. The result is saved into
+ STATE->canonical. */
+
+static void
+canonicalize_linespec (struct linespec_state *state, linespec_p ls)
+{
+ /* If canonicalization was not requested, no need to do anything. */
+ if (!state->canonical)
+ return;
+
+ /* Shortcut expressions, which can only appear by themselves. */
+ if (ls->expression != NULL)
+ state->canonical->addr_string = xstrdup (ls->expression);
+ else
+ {
+ struct ui_file *buf;
+ int need_colon = 0;
+
+ buf = mem_fileopen ();
+ if (ls->source_filename)
+ {
+ fputs_unfiltered (ls->source_filename, buf);
+ need_colon = 1;
+ }
+
+ if (ls->function_name)
+ {
+ if (need_colon)
+ fputc_unfiltered (':', buf);
+ fputs_unfiltered (ls->function_name, buf);
+ need_colon = 1;
+ }
+
+ if (ls->label_name)
+ {
+ if (need_colon)
+ fputc_unfiltered (':', buf);
+
+ if (ls->function_name == NULL)
+ {
+ struct symbol *s;
+
+ /* No function was specified, so add the symbol name. */
+ gdb_assert (ls->labels.function_symbols != NULL
+ && (VEC_length (symbolp, ls->labels.function_symbols)
+ == 1));
+ s = VEC_index (symbolp, ls->labels.function_symbols, 0);
+ fputs_unfiltered (SYMBOL_NATURAL_NAME (s), buf);
+ fputc_unfiltered (':', buf);
+ }
+
+ fputs_unfiltered (ls->label_name, buf);
+ need_colon = 1;
+ state->canonical->special_display = 1;
+ }
+
+ if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
+ {
+ if (need_colon)
+ fputc_unfiltered (':', buf);
+ fprintf_filtered (buf, "%s%d",
+ (ls->line_offset.sign == LINE_OFFSET_NONE ? ""
+ : (ls->line_offset.sign
+ == LINE_OFFSET_PLUS ? "+" : "-")),
+ ls->line_offset.offset);
+ }
+
+ state->canonical->addr_string = ui_file_xstrdup (buf, NULL);
+ ui_file_delete (buf);
+ }
+}
+
+/* Given a line offset in LS, construct the relevant SALs. */
+
+static struct symtabs_and_lines
+create_sals_line_offset (struct linespec_state *self,
+ linespec_p ls)
+{
+ struct symtabs_and_lines values;
+ struct symtab_and_line val;
+ int use_default = 0;
+
+ init_sal (&val);
+ values.sals = NULL;
+ values.nelts = 0;
+
+ /* This is where we need to make sure we have good defaults.
+ We must guarantee that this section of code is never executed
+ when we are called with just a function name, since
+ set_default_source_symtab_and_line uses
+ select_source_symtab that calls us with such an argument. */
+
+ if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
+ && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
+ {
+ const char *fullname;
+
+ set_current_program_space (self->program_space);
+
+ /* Make sure we have at least a default source line. */
+ set_default_source_symtab_and_line ();
+ initialize_defaults (&self->default_symtab, &self->default_line);
+ fullname = symtab_to_fullname (self->default_symtab);
+ VEC_pop (symtab_ptr, ls->file_symtabs);
+ VEC_free (symtab_ptr, ls->file_symtabs);
+ ls->file_symtabs = collect_symtabs_from_filename (fullname);
+ use_default = 1;
+ }
+
+ val.line = ls->line_offset.offset;
+ switch (ls->line_offset.sign)
+ {
+ case LINE_OFFSET_PLUS:
+ if (ls->line_offset.offset == 0)
+ val.line = 5;
+ if (use_default)
+ val.line = self->default_line + val.line;
+ break;
+
+ case LINE_OFFSET_MINUS:
+ if (ls->line_offset.offset == 0)
+ val.line = 15;
+ if (use_default)
+ val.line = self->default_line - val.line;
+ else
+ val.line = -val.line;
+ break;
+
+ case LINE_OFFSET_NONE:
+ break; /* No need to adjust val.line. */
+ }
+
+ if (self->list_mode)
+ decode_digits_list_mode (self, ls, &values, val);
+ else
+ {
+ struct linetable_entry *best_entry = NULL;
+ int *filter;
+ const struct block **blocks;
+ struct cleanup *cleanup;
+ struct symtabs_and_lines intermediate_results;
+ int i, j;
+
+ intermediate_results.sals = NULL;
+ intermediate_results.nelts = 0;
+
+ decode_digits_ordinary (self, ls, val.line, &intermediate_results,
+ &best_entry);
+ if (intermediate_results.nelts == 0 && best_entry != NULL)
+ decode_digits_ordinary (self, ls, best_entry->line,
+ &intermediate_results, &best_entry);
+
+ cleanup = make_cleanup (xfree, intermediate_results.sals);
+
+ /* For optimized code, the compiler can scatter one source line
+ across disjoint ranges of PC values, even when no duplicate
+ functions or inline functions are involved. For example,
+ 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
+ function can result in two PC ranges. In this case, we don't
+ want to set a breakpoint on the first PC of each range. To filter
+ such cases, we use containing blocks -- for each PC found
+ above, we see if there are other PCs that are in the same
+ block. If yes, the other PCs are filtered out. */
+
+ filter = XNEWVEC (int, intermediate_results.nelts);
+ make_cleanup (xfree, filter);
+ blocks = XNEWVEC (const struct block *, intermediate_results.nelts);
+ make_cleanup (xfree, blocks);
+
+ for (i = 0; i < intermediate_results.nelts; ++i)
+ {
+ set_current_program_space (intermediate_results.sals[i].pspace);
+
+ filter[i] = 1;
+ blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
+ intermediate_results.sals[i].section);
+ }
+
+ for (i = 0; i < intermediate_results.nelts; ++i)
+ {
+ if (blocks[i] != NULL)
+ for (j = i + 1; j < intermediate_results.nelts; ++j)
+ {
+ if (blocks[j] == blocks[i])
+ {
+ filter[j] = 0;
+ break;
+ }
+ }
+ }
+
+ for (i = 0; i < intermediate_results.nelts; ++i)
+ if (filter[i])
+ {
+ struct symbol *sym = (blocks[i]
+ ? block_containing_function (blocks[i])
+ : NULL);
+
+ if (self->funfirstline)
+ skip_prologue_sal (&intermediate_results.sals[i]);
+ /* Make sure the line matches the request, not what was
+ found. */
+ intermediate_results.sals[i].line = val.line;
+ add_sal_to_sals (self, &values, &intermediate_results.sals[i],
+ sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
+ }
+
+ do_cleanups (cleanup);
+ }
+
+ if (values.nelts == 0)
+ {
+ if (ls->source_filename)
+ throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
+ val.line, ls->source_filename);
+ else
+ throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
+ val.line);
+ }
+
+ return values;
+}
+
+/* Create and return SALs from the linespec LS. */
+
+static struct symtabs_and_lines
+convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
+{
+ struct symtabs_and_lines sals = {NULL, 0};