gdb/linespec.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Parser for linespec for the GNU debugger, GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "frame.h"
  17. #include "command.h"
  18. #include "symfile.h"
  19. #include "objfiles.h"
  20. #include "source.h"
  21. #include "demangle.h"
  22. #include "value.h"
  23. #include "completer.h"
  24. #include "cp-abi.h"
  25. #include "cp-support.h"
  26. #include "parser-defs.h"
  27. #include "block.h"
  28. #include "objc-lang.h"
  29. #include "linespec.h"
  30. #include "language.h"
  31. #include "interps.h"
  32. #include "mi/mi-cmds.h"
  33. #include "target.h"
  34. #include "arch-utils.h"
  35. #include <ctype.h>
  36. #include "cli/cli-utils.h"
  37. #include "filenames.h"
  38. #include "ada-lang.h"
  39. #include "stack.h"

  40. typedef struct symbol *symbolp;
  41. DEF_VEC_P (symbolp);

  42. typedef struct type *typep;
  43. DEF_VEC_P (typep);

  44. /* An address entry is used to ensure that any given location is only
  45.    added to the result a single time.  It holds an address and the
  46.    program space from which the address came.  */

  47. struct address_entry
  48. {
  49.   struct program_space *pspace;
  50.   CORE_ADDR addr;
  51. };

  52. typedef struct bound_minimal_symbol bound_minimal_symbol_d;

  53. DEF_VEC_O (bound_minimal_symbol_d);

  54. /* An enumeration of possible signs for a line offset.  */
  55. enum offset_relative_sign
  56. {
  57.   /* No sign  */
  58.   LINE_OFFSET_NONE,

  59.   /* A plus sign ("+")  */
  60.   LINE_OFFSET_PLUS,

  61.   /* A minus sign ("-")  */
  62.   LINE_OFFSET_MINUS,

  63.   /* A special "sign" for unspecified offset.  */
  64.   LINE_OFFSET_UNKNOWN
  65. };

  66. /* A line offset in a linespec.  */

  67. struct line_offset
  68. {
  69.   /* Line offset and any specified sign.  */
  70.   int offset;
  71.   enum offset_relative_sign sign;
  72. };

  73. /* A linespec.  Elements of this structure are filled in by a parser
  74.    (either parse_linespec or some other function).  The structure is
  75.    then converted into SALs by convert_linespec_to_sals.  */

  76. struct linespec
  77. {
  78.   /* An expression and the resulting PC.  Specifying an expression
  79.      currently precludes the use of other members.  */

  80.   /* The expression entered by the user.  */
  81.   const char *expression;

  82.   /* The resulting PC expression derived from evaluating EXPRESSION.  */
  83.   CORE_ADDR expr_pc;

  84.   /* Any specified file symtabs.  */

  85.   /* The user-supplied source filename or NULL if none was specified.  */
  86.   const char *source_filename;

  87.   /* The list of symtabs to search to which to limit the search.  May not
  88.      be NULL.  If SOURCE_FILENAME is NULL (no user-specified filename),
  89.      FILE_SYMTABS should contain one single NULL member.  This will
  90.      cause the code to use the default symtab.  */
  91.   VEC (symtab_ptr) *file_symtabs;

  92.   /* The name of a function or method and any matching symbols.  */

  93.   /* The user-specified function name.  If no function name was
  94.      supplied, this may be NULL.  */
  95.   const char *function_name;

  96.   /* A list of matching function symbols and minimal symbols.  Both lists
  97.      may be NULL if no matching symbols were found.  */
  98.   VEC (symbolp) *function_symbols;
  99.   VEC (bound_minimal_symbol_d) *minimal_symbols;

  100.   /* The name of a label and matching symbols.  */

  101.   /* The user-specified label name.  */
  102.   const char *label_name;

  103.   /* A structure of matching label symbols and the corresponding
  104.      function symbol in which the label was found.  Both may be NULL
  105.      or both must be non-NULL.  */
  106.   struct
  107.   {
  108.     VEC (symbolp) *label_symbols;
  109.     VEC (symbolp) *function_symbols;
  110.   } labels;

  111.   /* Line offset.  It may be LINE_OFFSET_UNKNOWN, meaning that no
  112.    offset was specified.  */
  113.   struct line_offset line_offset;
  114. };
  115. typedef struct linespec *linespec_p;

  116. /* A canonical linespec represented as a symtab-related string.

  117.    Each entry represents the "SYMTAB:SUFFIX" linespec string.
  118.    SYMTAB can be converted for example by symtab_to_fullname or
  119.    symtab_to_filename_for_display as needed.  */

  120. struct linespec_canonical_name
  121. {
  122.   /* Remaining text part of the linespec string.  */
  123.   char *suffix;

  124.   /* If NULL then SUFFIX is the whole linespec string.  */
  125.   struct symtab *symtab;
  126. };

  127. /* An instance of this is used to keep all state while linespec
  128.    operates.  This instance is passed around as a 'this' pointer to
  129.    the various implementation methods.  */

  130. struct linespec_state
  131. {
  132.   /* The language in use during linespec processing.  */
  133.   const struct language_defn *language;

  134.   /* The program space as seen when the module was entered.  */
  135.   struct program_space *program_space;

  136.   /* The default symtab to use, if no other symtab is specified.  */
  137.   struct symtab *default_symtab;

  138.   /* The default line to use.  */
  139.   int default_line;

  140.   /* The 'funfirstline' value that was passed in to decode_line_1 or
  141.      decode_line_full.  */
  142.   int funfirstline;

  143.   /* Nonzero if we are running in 'list' mode; see decode_line_list.  */
  144.   int list_mode;

  145.   /* The 'canonical' value passed to decode_line_full, or NULL.  */
  146.   struct linespec_result *canonical;

  147.   /* Canonical strings that mirror the symtabs_and_lines result.  */
  148.   struct linespec_canonical_name *canonical_names;

  149.   /* This is a set of address_entry objects which is used to prevent
  150.      duplicate symbols from being entered into the result.  */
  151.   htab_t addr_set;
  152. };

  153. /* This is a helper object that is used when collecting symbols into a
  154.    result.  */

  155. struct collect_info
  156. {
  157.   /* The linespec object in use.  */
  158.   struct linespec_state *state;

  159.   /* A list of symtabs to which to restrict matches.  */
  160.   VEC (symtab_ptr) *file_symtabs;

  161.   /* The result being accumulated.  */
  162.   struct
  163.   {
  164.     VEC (symbolp) *symbols;
  165.     VEC (bound_minimal_symbol_d) *minimal_symbols;
  166.   } result;
  167. };

  168. /* Token types  */

  169. enum ls_token_type
  170. {
  171.   /* A keyword  */
  172.   LSTOKEN_KEYWORD = 0,

  173.   /* A colon "separator"  */
  174.   LSTOKEN_COLON,

  175.   /* A string  */
  176.   LSTOKEN_STRING,

  177.   /* A number  */
  178.   LSTOKEN_NUMBER,

  179.   /* A comma  */
  180.   LSTOKEN_COMMA,

  181.   /* EOI (end of input)  */
  182.   LSTOKEN_EOI,

  183.   /* Consumed token  */
  184.   LSTOKEN_CONSUMED
  185. };
  186. typedef enum ls_token_type linespec_token_type;

  187. /* List of keywords  */

  188. static const char * const linespec_keywords[] = { "if", "thread", "task" };

  189. /* A token of the linespec lexer  */

  190. struct ls_token
  191. {
  192.   /* The type of the token  */
  193.   linespec_token_type type;

  194.   /* Data for the token  */
  195.   union
  196.   {
  197.     /* A string, given as a stoken  */
  198.     struct stoken string;

  199.     /* A keyword  */
  200.     const char *keyword;
  201.   } data;
  202. };
  203. typedef struct ls_token linespec_token;

  204. #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
  205. #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword

  206. /* An instance of the linespec parser.  */

  207. struct ls_parser
  208. {
  209.   /* Lexer internal data  */
  210.   struct
  211.   {
  212.     /* Save head of input stream.  */
  213.     const char *saved_arg;

  214.     /* Head of the input stream.  */
  215.     const char **stream;
  216. #define PARSER_STREAM(P) (*(P)->lexer.stream)

  217.     /* The current token.  */
  218.     linespec_token current;
  219.   } lexer;

  220.   /* Is the entire linespec quote-enclosed?  */
  221.   int is_quote_enclosed;

  222.   /* Is a keyword syntactically valid at this point?
  223.      In, e.g., "break thread thread 1", the leading "keyword" must not
  224.      be interpreted as such.  */
  225.   int keyword_ok;

  226.   /* The state of the parse.  */
  227.   struct linespec_state state;
  228. #define PARSER_STATE(PPTR) (&(PPTR)->state)

  229.   /* The result of the parse.  */
  230.   struct linespec result;
  231. #define PARSER_RESULT(PPTR) (&(PPTR)->result)
  232. };
  233. typedef struct ls_parser linespec_parser;

  234. /* Prototypes for local functions.  */

  235. static void iterate_over_file_blocks (struct symtab *symtab,
  236.                                       const char *name, domain_enum domain,
  237.                                       symbol_found_callback_ftype *callback,
  238.                                       void *data);

  239. static void initialize_defaults (struct symtab **default_symtab,
  240.                                  int *default_line);

  241. static CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);

  242. static struct symtabs_and_lines decode_objc (struct linespec_state *self,
  243.                                              linespec_p ls,
  244.                                              const char **argptr);

  245. static VEC (symtab_ptr) *symtabs_from_filename (const char *);

  246. static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
  247.                                           VEC (symbolp) *function_symbols,
  248.                                           VEC (symbolp) **label_funcs_ret,
  249.                                           const char *name);

  250. static void find_linespec_symbols (struct linespec_state *self,
  251.                                    VEC (symtab_ptr) *file_symtabs,
  252.                                    const char *name,
  253.                                    VEC (symbolp) **symbols,
  254.                                    VEC (bound_minimal_symbol_d) **minsyms);

  255. static struct line_offset
  256.      linespec_parse_variable (struct linespec_state *self,
  257.                               const char *variable);

  258. static int symbol_to_sal (struct symtab_and_line *result,
  259.                           int funfirstline, struct symbol *sym);

  260. static void add_matching_symbols_to_info (const char *name,
  261.                                           struct collect_info *info,
  262.                                           struct program_space *pspace);

  263. static void add_all_symbol_names_from_pspace (struct collect_info *info,
  264.                                               struct program_space *pspace,
  265.                                               VEC (const_char_ptr) *names);

  266. static VEC (symtab_ptr) *collect_symtabs_from_filename (const char *file);

  267. static void decode_digits_ordinary (struct linespec_state *self,
  268.                                     linespec_p ls,
  269.                                     int line,
  270.                                     struct symtabs_and_lines *sals,
  271.                                     struct linetable_entry **best_entry);

  272. static void decode_digits_list_mode (struct linespec_state *self,
  273.                                      linespec_p ls,
  274.                                      struct symtabs_and_lines *values,
  275.                                      struct symtab_and_line val);

  276. static void minsym_found (struct linespec_state *self, struct objfile *objfile,
  277.                           struct minimal_symbol *msymbol,
  278.                           struct symtabs_and_lines *result);

  279. static int compare_symbols (const void *a, const void *b);

  280. static int compare_msymbols (const void *a, const void *b);

  281. static const char *find_toplevel_char (const char *s, char c);

  282. /* Permitted quote characters for the parser.  This is different from the
  283.    completer's quote characters to allow backward compatibility with the
  284.    previous parser.  */
  285. static const char *const linespec_quote_characters = "\"\'";

  286. /* Lexer functions.  */

  287. /* Lex a number from the input in PARSER.  This only supports
  288.    decimal numbers.

  289.    Return true if input is decimal numbers.  Return false if not.  */

  290. static int
  291. linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
  292. {
  293.   tokenp->type = LSTOKEN_NUMBER;
  294.   LS_TOKEN_STOKEN (*tokenp).length = 0;
  295.   LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);

  296.   /* Keep any sign at the start of the stream.  */
  297.   if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
  298.     {
  299.       ++LS_TOKEN_STOKEN (*tokenp).length;
  300.       ++(PARSER_STREAM (parser));
  301.     }

  302.   while (isdigit (*PARSER_STREAM (parser)))
  303.     {
  304.       ++LS_TOKEN_STOKEN (*tokenp).length;
  305.       ++(PARSER_STREAM (parser));
  306.     }

  307.   /* If the next character in the input buffer is not a space, comma,
  308.      quote, or colon, this input does not represent a number.  */
  309.   if (*PARSER_STREAM (parser) != '\0'
  310.       && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
  311.       && *PARSER_STREAM (parser) != ':'
  312.       && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
  313.     {
  314.       PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
  315.       return 0;
  316.     }

  317.   return 1;
  318. }

  319. /* Does P represent one of the keywords?  If so, return
  320.    the keyword.  If not, return NULL.  */

  321. static const char *
  322. linespec_lexer_lex_keyword (const char *p)
  323. {
  324.   int i;

  325.   if (p != NULL)
  326.     {
  327.       for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i)
  328.         {
  329.           int len = strlen (linespec_keywords[i]);

  330.           /* If P begins with one of the keywords and the next
  331.              character is not a valid identifier character,
  332.              we have found a keyword.  */
  333.           if (strncmp (p, linespec_keywords[i], len) == 0
  334.               && !(isalnum (p[len]) || p[len] == '_'))
  335.             return linespec_keywords[i];
  336.         }
  337.     }

  338.   return NULL;
  339. }

  340. /* Does STRING represent an Ada operator?  If so, return the length
  341.    of the decoded operator name.  If not, return 0.  */

  342. static int
  343. is_ada_operator (const char *string)
  344. {
  345.   const struct ada_opname_map *mapping;

  346.   for (mapping = ada_opname_table;
  347.        mapping->encoded != NULL
  348.          && strncmp (mapping->decoded, string,
  349.                      strlen (mapping->decoded)) != 0; ++mapping)
  350.     ;

  351.   return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
  352. }

  353. /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal.  Return
  354.    the location of QUOTE_CHAR, or NULL if not found.  */

  355. static const char *
  356. skip_quote_char (const char *string, char quote_char)
  357. {
  358.   const char *p, *last;

  359.   p = last = find_toplevel_char (string, quote_char);
  360.   while (p && *p != '\0' && *p != ':')
  361.     {
  362.       p = find_toplevel_char (p, quote_char);
  363.       if (p != NULL)
  364.         last = p++;
  365.     }

  366.   return last;
  367. }

  368. /* Make a writable copy of the string given in TOKEN, trimming
  369.    any trailing whitespace.  */

  370. static char *
  371. copy_token_string (linespec_token token)
  372. {
  373.   char *str, *s;

  374.   if (token.type == LSTOKEN_KEYWORD)
  375.     return xstrdup (LS_TOKEN_KEYWORD (token));

  376.   str = savestring (LS_TOKEN_STOKEN (token).ptr,
  377.                     LS_TOKEN_STOKEN (token).length);
  378.   s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
  379.   *s = '\0';

  380.   return str;
  381. }

  382. /* Does P represent the end of a quote-enclosed linespec?  */

  383. static int
  384. is_closing_quote_enclosed (const char *p)
  385. {
  386.   if (strchr (linespec_quote_characters, *p))
  387.     ++p;
  388.   p = skip_spaces ((char *) p);
  389.   return (*p == '\0' || linespec_lexer_lex_keyword (p));
  390. }

  391. /* Find the end of the parameter list that starts with *INPUT.
  392.    This helper function assists with lexing string segments
  393.    which might contain valid (non-terminating) commas.  */

  394. static const char *
  395. find_parameter_list_end (const char *input)
  396. {
  397.   char end_char, start_char;
  398.   int depth;
  399.   const char *p;

  400.   start_char = *input;
  401.   if (start_char == '(')
  402.     end_char = ')';
  403.   else if (start_char == '<')
  404.     end_char = '>';
  405.   else
  406.     return NULL;

  407.   p = input;
  408.   depth = 0;
  409.   while (*p)
  410.     {
  411.       if (*p == start_char)
  412.         ++depth;
  413.       else if (*p == end_char)
  414.         {
  415.           if (--depth == 0)
  416.             {
  417.               ++p;
  418.               break;
  419.             }
  420.         }
  421.       ++p;
  422.     }

  423.   return p;
  424. }


  425. /* Lex a string from the input in PARSER.  */

  426. static linespec_token
  427. linespec_lexer_lex_string (linespec_parser *parser)
  428. {
  429.   linespec_token token;
  430.   const char *start = PARSER_STREAM (parser);

  431.   token.type = LSTOKEN_STRING;

  432.   /* If the input stream starts with a quote character, skip to the next
  433.      quote character, regardless of the content.  */
  434.   if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
  435.     {
  436.       const char *end;
  437.       char quote_char = *PARSER_STREAM (parser);

  438.       /* Special case: Ada operators.  */
  439.       if (PARSER_STATE (parser)->language->la_language == language_ada
  440.           && quote_char == '\"')
  441.         {
  442.           int len = is_ada_operator (PARSER_STREAM (parser));

  443.           if (len != 0)
  444.             {
  445.               /* The input is an Ada operator.  Return the quoted string
  446.                  as-is.  */
  447.               LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
  448.               LS_TOKEN_STOKEN (token).length = len;
  449.               PARSER_STREAM (parser) += len;
  450.               return token;
  451.             }

  452.           /* The input does not represent an Ada operator -- fall through
  453.              to normal quoted string handling.  */
  454.         }

  455.       /* Skip past the beginning quote.  */
  456.       ++(PARSER_STREAM (parser));

  457.       /* Mark the start of the string.  */
  458.       LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);

  459.       /* Skip to the ending quote.  */
  460.       end = skip_quote_char (PARSER_STREAM (parser), quote_char);

  461.       /* Error if the input did not terminate properly.  */
  462.       if (end == NULL)
  463.         error (_("unmatched quote"));

  464.       /* Skip over the ending quote and mark the length of the string.  */
  465.       PARSER_STREAM (parser) = (char *) ++end;
  466.       LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
  467.     }
  468.   else
  469.     {
  470.       const char *p;

  471.       /* Otherwise, only identifier characters are permitted.
  472.          Spaces are the exception.  In general, we keep spaces,
  473.          but only if the next characters in the input do not resolve
  474.          to one of the keywords.

  475.          This allows users to forgo quoting CV-qualifiers, template arguments,
  476.          and similar common language constructs.  */

  477.       while (1)
  478.         {
  479.           if (isspace (*PARSER_STREAM (parser)))
  480.             {
  481.               p = skip_spaces_const (PARSER_STREAM (parser));
  482.               /* When we get here we know we've found something followed by
  483.                  a space (we skip over parens and templates below).
  484.                  So if we find a keyword now, we know it is a keyword and not,
  485.                  say, a function name.  */
  486.               if (linespec_lexer_lex_keyword (p) != NULL)
  487.                 {
  488.                   LS_TOKEN_STOKEN (token).ptr = start;
  489.                   LS_TOKEN_STOKEN (token).length
  490.                     = PARSER_STREAM (parser) - start;
  491.                   return token;
  492.                 }

  493.               /* Advance past the whitespace.  */
  494.               PARSER_STREAM (parser) = p;
  495.             }

  496.           /* If the next character is EOI or (single) ':', the
  497.              string is complete;  return the token.  */
  498.           if (*PARSER_STREAM (parser) == 0)
  499.             {
  500.               LS_TOKEN_STOKEN (token).ptr = start;
  501.               LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
  502.               return token;
  503.             }
  504.           else if (PARSER_STREAM (parser)[0] == ':')
  505.             {
  506.               /* Do not tokenize the C++ scope operator. */
  507.               if (PARSER_STREAM (parser)[1] == ':')
  508.                 ++(PARSER_STREAM (parser));

  509.               /* Do not tokenify if the input length so far is one
  510.                  (i.e, a single-letter drive name) and the next character
  511.                  is a directory separator.  This allows Windows-style
  512.                  paths to be recognized as filenames without quoting it.  */
  513.               else if ((PARSER_STREAM (parser) - start) != 1
  514.                        || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
  515.                 {
  516.                   LS_TOKEN_STOKEN (token).ptr = start;
  517.                   LS_TOKEN_STOKEN (token).length
  518.                     = PARSER_STREAM (parser) - start;
  519.                   return token;
  520.                 }
  521.             }
  522.           /* Special case: permit quote-enclosed linespecs.  */
  523.           else if (parser->is_quote_enclosed
  524.                    && strchr (linespec_quote_characters,
  525.                               *PARSER_STREAM (parser))
  526.                    && is_closing_quote_enclosed (PARSER_STREAM (parser)))
  527.             {
  528.               LS_TOKEN_STOKEN (token).ptr = start;
  529.               LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
  530.               return token;
  531.             }
  532.           /* Because commas may terminate a linespec and appear in
  533.              the middle of valid string input, special cases for
  534.              '<' and '(' are necessary.  */
  535.           else if (*PARSER_STREAM (parser) == '<'
  536.                    || *PARSER_STREAM (parser) == '(')
  537.             {
  538.               const char *p;

  539.               p = find_parameter_list_end (PARSER_STREAM (parser));
  540.               if (p != NULL)
  541.                 {
  542.                   PARSER_STREAM (parser) = p;
  543.                   continue;
  544.                 }
  545.             }
  546.           /* Commas are terminators, but not if they are part of an
  547.              operator name.  */
  548.           else if (*PARSER_STREAM (parser) == ',')
  549.             {
  550.               if ((PARSER_STATE (parser)->language->la_language
  551.                    == language_cplus)
  552.                   && (PARSER_STREAM (parser) - start) > 8
  553.                   /* strlen ("operator") */)
  554.                 {
  555.                   char *p = strstr (start, "operator");

  556.                   if (p != NULL && is_operator_name (p))
  557.                     {
  558.                       /* This is an operator name.  Keep going.  */
  559.                       ++(PARSER_STREAM (parser));
  560.                       continue;
  561.                     }
  562.                 }

  563.               /* Comma terminates the string.  */
  564.               LS_TOKEN_STOKEN (token).ptr = start;
  565.               LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
  566.               return token;
  567.             }

  568.           /* Advance the stream.  */
  569.           ++(PARSER_STREAM (parser));
  570.         }
  571.     }

  572.   return token;
  573. }

  574. /* Lex a single linespec token from PARSER.  */

  575. static linespec_token
  576. linespec_lexer_lex_one (linespec_parser *parser)
  577. {
  578.   const char *keyword;

  579.   if (parser->lexer.current.type == LSTOKEN_CONSUMED)
  580.     {
  581.       /* Skip any whitespace.  */
  582.       PARSER_STREAM (parser) = skip_spaces_const (PARSER_STREAM (parser));

  583.       /* Check for a keyword, they end the linespec.  */
  584.       keyword = NULL;
  585.       if (parser->keyword_ok)
  586.         keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
  587.       if (keyword != NULL)
  588.         {
  589.           parser->lexer.current.type = LSTOKEN_KEYWORD;
  590.           LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
  591.           return parser->lexer.current;
  592.         }

  593.       /* Handle other tokens.  */
  594.       switch (*PARSER_STREAM (parser))
  595.         {
  596.         case 0:
  597.           parser->lexer.current.type = LSTOKEN_EOI;
  598.           break;

  599.         case '+': case '-':
  600.         case '0': case '1': case '2': case '3': case '4':
  601.         case '5': case '6': case '7': case '8': case '9':
  602.            if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
  603.              parser->lexer.current = linespec_lexer_lex_string (parser);
  604.           break;

  605.         case ':':
  606.           /* If we have a scope operator, lex the input as a string.
  607.              Otherwise, return LSTOKEN_COLON.  */
  608.           if (PARSER_STREAM (parser)[1] == ':')
  609.             parser->lexer.current = linespec_lexer_lex_string (parser);
  610.           else
  611.             {
  612.               parser->lexer.current.type = LSTOKEN_COLON;
  613.               ++(PARSER_STREAM (parser));
  614.             }
  615.           break;

  616.         case '\'': case '\"':
  617.           /* Special case: permit quote-enclosed linespecs.  */
  618.           if (parser->is_quote_enclosed
  619.               && is_closing_quote_enclosed (PARSER_STREAM (parser)))
  620.             {
  621.               ++(PARSER_STREAM (parser));
  622.               parser->lexer.current.type = LSTOKEN_EOI;
  623.             }
  624.           else
  625.             parser->lexer.current = linespec_lexer_lex_string (parser);
  626.           break;

  627.         case ',':
  628.           parser->lexer.current.type = LSTOKEN_COMMA;
  629.           LS_TOKEN_STOKEN (parser->lexer.current).ptr
  630.             = PARSER_STREAM (parser);
  631.           LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
  632.           ++(PARSER_STREAM (parser));
  633.           break;

  634.         default:
  635.           /* If the input is not a number, it must be a string.
  636.              [Keywords were already considered above.]  */
  637.           parser->lexer.current = linespec_lexer_lex_string (parser);
  638.           break;
  639.         }
  640.     }

  641.   return parser->lexer.current;
  642. }

  643. /* Consume the current token and return the next token in PARSER's
  644.    input stream.  */

  645. static linespec_token
  646. linespec_lexer_consume_token (linespec_parser *parser)
  647. {
  648.   parser->lexer.current.type = LSTOKEN_CONSUMED;
  649.   return linespec_lexer_lex_one (parser);
  650. }

  651. /* Return the next token without consuming the current token.  */

  652. static linespec_token
  653. linespec_lexer_peek_token (linespec_parser *parser)
  654. {
  655.   linespec_token next;
  656.   const char *saved_stream = PARSER_STREAM (parser);
  657.   linespec_token saved_token = parser->lexer.current;

  658.   next = linespec_lexer_consume_token (parser);
  659.   PARSER_STREAM (parser) = saved_stream;
  660.   parser->lexer.current = saved_token;
  661.   return next;
  662. }

  663. /* Helper functions.  */

  664. /* Add SAL to SALS.  */

  665. static void
  666. add_sal_to_sals_basic (struct symtabs_and_lines *sals,
  667.                        struct symtab_and_line *sal)
  668. {
  669.   ++sals->nelts;
  670.   sals->sals = xrealloc (sals->sals, sals->nelts * sizeof (sals->sals[0]));
  671.   sals->sals[sals->nelts - 1] = *sal;
  672. }

  673. /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
  674.    the new sal, if needed.  If not NULL, SYMNAME is the name of the
  675.    symbol to use when constructing the new canonical name.

  676.    If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
  677.    canonical name for the SAL.  */

  678. static void
  679. add_sal_to_sals (struct linespec_state *self,
  680.                  struct symtabs_and_lines *sals,
  681.                  struct symtab_and_line *sal,
  682.                  const char *symname, int literal_canonical)
  683. {
  684.   add_sal_to_sals_basic (sals, sal);

  685.   if (self->canonical)
  686.     {
  687.       struct linespec_canonical_name *canonical;

  688.       self->canonical_names = xrealloc (self->canonical_names,
  689.                                         (sals->nelts
  690.                                          * sizeof (*self->canonical_names)));
  691.       canonical = &self->canonical_names[sals->nelts - 1];
  692.       if (!literal_canonical && sal->symtab)
  693.         {
  694.           const char *fullname = symtab_to_fullname (sal->symtab);

  695.           /* Note that the filter doesn't have to be a valid linespec
  696.              input.  We only apply the ":LINE" treatment to Ada for
  697.              the time being.  */
  698.           if (symname != NULL && sal->line != 0
  699.               && self->language->la_language == language_ada)
  700.             canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
  701.           else if (symname != NULL)
  702.             canonical->suffix = xstrdup (symname);
  703.           else
  704.             canonical->suffix = xstrprintf ("%d", sal->line);
  705.           canonical->symtab = sal->symtab;
  706.         }
  707.       else
  708.         {
  709.           if (symname != NULL)
  710.             canonical->suffix = xstrdup (symname);
  711.           else
  712.             canonical->suffix = xstrdup ("<unknown>");
  713.           canonical->symtab = NULL;
  714.         }
  715.     }
  716. }

  717. /* A hash function for address_entry.  */

  718. static hashval_t
  719. hash_address_entry (const void *p)
  720. {
  721.   const struct address_entry *aep = p;
  722.   hashval_t hash;

  723.   hash = iterative_hash_object (aep->pspace, 0);
  724.   return iterative_hash_object (aep->addr, hash);
  725. }

  726. /* An equality function for address_entry.  */

  727. static int
  728. eq_address_entry (const void *a, const void *b)
  729. {
  730.   const struct address_entry *aea = a;
  731.   const struct address_entry *aeb = b;

  732.   return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
  733. }

  734. /* Check whether the address, represented by PSPACE and ADDR, is
  735.    already in the set.  If so, return 0.  Otherwise, add it and return
  736.    1.  */

  737. static int
  738. maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
  739. {
  740.   struct address_entry e, *p;
  741.   void **slot;

  742.   e.pspace = pspace;
  743.   e.addr = addr;
  744.   slot = htab_find_slot (set, &e, INSERT);
  745.   if (*slot)
  746.     return 0;

  747.   p = XNEW (struct address_entry);
  748.   memcpy (p, &e, sizeof (struct address_entry));
  749.   *slot = p;

  750.   return 1;
  751. }

  752. /* A callback function and the additional data to call it with.  */

  753. struct symbol_and_data_callback
  754. {
  755.   /* The callback to use.  */
  756.   symbol_found_callback_ftype *callback;

  757.   /* Data to be passed to the callback.  */
  758.   void *data;
  759. };

  760. /* A helper for iterate_over_all_matching_symtabs that is used to
  761.    restrict calls to another callback to symbols representing inline
  762.    symbols only.  */

  763. static int
  764. iterate_inline_only (struct symbol *sym, void *d)
  765. {
  766.   if (SYMBOL_INLINED (sym))
  767.     {
  768.       struct symbol_and_data_callback *cad = d;

  769.       return cad->callback (sym, cad->data);
  770.     }
  771.   return 1; /* Continue iterating.  */
  772. }

  773. /* Some data for the expand_symtabs_matching callback.  */

  774. struct symbol_matcher_data
  775. {
  776.   /* The lookup name against which symbol name should be compared.  */
  777.   const char *lookup_name;

  778.   /* The routine to be used for comparison.  */
  779.   symbol_name_cmp_ftype symbol_name_cmp;
  780. };

  781. /* A helper for iterate_over_all_matching_symtabs that is passed as a
  782.    callback to the expand_symtabs_matching method.  */

  783. static int
  784. iterate_name_matcher (const char *name, void *d)
  785. {
  786.   const struct symbol_matcher_data *data = d;

  787.   if (data->symbol_name_cmp (name, data->lookup_name) == 0)
  788.     return 1; /* Expand this symbol's symbol table.  */
  789.   return 0; /* Skip this symbol.  */
  790. }

  791. /* A helper that walks over all matching symtabs in all objfiles and
  792.    calls CALLBACK for each symbol matching NAME.  If SEARCH_PSPACE is
  793.    not NULL, then the search is restricted to just that program
  794.    space.  If INCLUDE_INLINE is nonzero then symbols representing
  795.    inlined instances of functions will be included in the result.  */

  796. static void
  797. iterate_over_all_matching_symtabs (struct linespec_state *state,
  798.                                    const char *name,
  799.                                    const domain_enum domain,
  800.                                    symbol_found_callback_ftype *callback,
  801.                                    void *data,
  802.                                    struct program_space *search_pspace,
  803.                                    int include_inline)
  804. {
  805.   struct objfile *objfile;
  806.   struct program_space *pspace;
  807.   struct symbol_matcher_data matcher_data;

  808.   matcher_data.lookup_name = name;
  809.   matcher_data.symbol_name_cmp =
  810.     state->language->la_get_symbol_name_cmp != NULL
  811.     ? state->language->la_get_symbol_name_cmp (name)
  812.     : strcmp_iw;

  813.   ALL_PSPACES (pspace)
  814.   {
  815.     if (search_pspace != NULL && search_pspace != pspace)
  816.       continue;
  817.     if (pspace->executing_startup)
  818.       continue;

  819.     set_current_program_space (pspace);

  820.     ALL_OBJFILES (objfile)
  821.     {
  822.       struct compunit_symtab *cu;

  823.       if (objfile->sf)
  824.         objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
  825.                                                   iterate_name_matcher,
  826.                                                   ALL_DOMAIN,
  827.                                                   &matcher_data);

  828.       ALL_OBJFILE_COMPUNITS (objfile, cu)
  829.         {
  830.           struct symtab *symtab = COMPUNIT_FILETABS (cu);

  831.           iterate_over_file_blocks (symtab, name, domain, callback, data);

  832.           if (include_inline)
  833.             {
  834.               struct symbol_and_data_callback cad = { callback, data };
  835.               struct block *block;
  836.               int i;

  837.               for (i = FIRST_LOCAL_BLOCK;
  838.                    i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
  839.                    i++)
  840.                 {
  841.                   block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
  842.                   state->language->la_iterate_over_symbols
  843.                     (block, name, domain, iterate_inline_only, &cad);
  844.                 }
  845.             }
  846.         }
  847.     }
  848.   }
  849. }

  850. /* Returns the block to be used for symbol searches from
  851.    the current location.  */

  852. static const struct block *
  853. get_current_search_block (void)
  854. {
  855.   const struct block *block;
  856.   enum language save_language;

  857.   /* get_selected_block can change the current language when there is
  858.      no selected frame yet.  */
  859.   save_language = current_language->la_language;
  860.   block = get_selected_block (0);
  861.   set_language (save_language);

  862.   return block;
  863. }

  864. /* Iterate over static and global blocks.  */

  865. static void
  866. iterate_over_file_blocks (struct symtab *symtab,
  867.                           const char *name, domain_enum domain,
  868.                           symbol_found_callback_ftype *callback, void *data)
  869. {
  870.   struct block *block;

  871.   for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
  872.        block != NULL;
  873.        block = BLOCK_SUPERBLOCK (block))
  874.     LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
  875. }

  876. /* A helper for find_method.  This finds all methods in type T which
  877.    match NAME.  It adds matching symbol names to RESULT_NAMES, and
  878.    adds T's direct superclasses to SUPERCLASSES.  */

  879. static void
  880. find_methods (struct type *t, const char *name,
  881.               VEC (const_char_ptr) **result_names,
  882.               VEC (typep) **superclasses)
  883. {
  884.   int ibase;
  885.   const char *class_name = type_name_no_tag (t);

  886.   /* Ignore this class if it doesn't have a name.  This is ugly, but
  887.      unless we figure out how to get the physname without the name of
  888.      the class, then the loop can't do any good.  */
  889.   if (class_name)
  890.     {
  891.       int method_counter;

  892.       CHECK_TYPEDEF (t);

  893.       /* Loop over each method name.  At this level, all overloads of a name
  894.          are counted as a single name.  There is an inner loop which loops over
  895.          each overload.  */

  896.       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
  897.            method_counter >= 0;
  898.            --method_counter)
  899.         {
  900.           const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
  901.           char dem_opname[64];

  902.           if (strncmp (method_name, "__", 2) == 0 ||
  903.               strncmp (method_name, "op", 2) == 0 ||
  904.               strncmp (method_name, "type", 4) == 0)
  905.             {
  906.               if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
  907.                 method_name = dem_opname;
  908.               else if (cplus_demangle_opname (method_name, dem_opname, 0))
  909.                 method_name = dem_opname;
  910.             }

  911.           if (strcmp_iw (method_name, name) == 0)
  912.             {
  913.               int field_counter;

  914.               for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
  915.                                     - 1);
  916.                    field_counter >= 0;
  917.                    --field_counter)
  918.                 {
  919.                   struct fn_field *f;
  920.                   const char *phys_name;

  921.                   f = TYPE_FN_FIELDLIST1 (t, method_counter);
  922.                   if (TYPE_FN_FIELD_STUB (f, field_counter))
  923.                     continue;
  924.                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
  925.                   VEC_safe_push (const_char_ptr, *result_names, phys_name);
  926.                 }
  927.             }
  928.         }
  929.     }

  930.   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
  931.     VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
  932. }

  933. /* Find an instance of the character C in the string S that is outside
  934.    of all parenthesis pairs, single-quoted strings, and double-quoted
  935.    strings.  Also, ignore the char within a template name, like a ','
  936.    within foo<int, int>.  */

  937. static const char *
  938. find_toplevel_char (const char *s, char c)
  939. {
  940.   int quoted = 0;                /* zero if we're not in quotes;
  941.                                    '"' if we're in a double-quoted string;
  942.                                    '\'' if we're in a single-quoted string.  */
  943.   int depth = 0;                /* Number of unclosed parens we've seen.  */
  944.   const char *scan;

  945.   for (scan = s; *scan; scan++)
  946.     {
  947.       if (quoted)
  948.         {
  949.           if (*scan == quoted)
  950.             quoted = 0;
  951.           else if (*scan == '\\' && *(scan + 1))
  952.             scan++;
  953.         }
  954.       else if (*scan == c && ! quoted && depth == 0)
  955.         return scan;
  956.       else if (*scan == '"' || *scan == '\'')
  957.         quoted = *scan;
  958.       else if (*scan == '(' || *scan == '<')
  959.         depth++;
  960.       else if ((*scan == ')' || *scan == '>') && depth > 0)
  961.         depth--;
  962.     }

  963.   return 0;
  964. }

  965. /* The string equivalent of find_toplevel_char.  Returns a pointer
  966.    to the location of NEEDLE in HAYSTACK, ignoring any occurrences
  967.    inside "()" and "<>".  Returns NULL if NEEDLE was not found.  */

  968. static const char *
  969. find_toplevel_string (const char *haystack, const char *needle)
  970. {
  971.   const char *s = haystack;

  972.   do
  973.     {
  974.       s = find_toplevel_char (s, *needle);

  975.       if (s != NULL)
  976.         {
  977.           /* Found first char in HAYSTACK;  check rest of string.  */
  978.           if (strncmp (s, needle, strlen (needle)) == 0)
  979.             return s;

  980.           /* Didn't find it; loop over HAYSTACK, looking for the next
  981.              instance of the first character of NEEDLE.  */
  982.           ++s;
  983.         }
  984.     }
  985.   while (s != NULL && *s != '\0');

  986.   /* NEEDLE was not found in HAYSTACK.  */
  987.   return NULL;
  988. }

  989. /* Convert CANONICAL to its string representation using
  990.    symtab_to_fullname for SYMTAB.  The caller must xfree the result.  */

  991. static char *
  992. canonical_to_fullform (const struct linespec_canonical_name *canonical)
  993. {
  994.   if (canonical->symtab == NULL)
  995.     return xstrdup (canonical->suffix);
  996.   else
  997.     return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
  998.                        canonical->suffix);
  999. }

  1000. /* Given FILTERS, a list of canonical names, filter the sals in RESULT
  1001.    and store the result in SELF->CANONICAL.  */

  1002. static void
  1003. filter_results (struct linespec_state *self,
  1004.                 struct symtabs_and_lines *result,
  1005.                 VEC (const_char_ptr) *filters)
  1006. {
  1007.   int i;
  1008.   const char *name;

  1009.   for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
  1010.     {
  1011.       struct linespec_sals lsal;
  1012.       int j;

  1013.       memset (&lsal, 0, sizeof (lsal));

  1014.       for (j = 0; j < result->nelts; ++j)
  1015.         {
  1016.           const struct linespec_canonical_name *canonical;
  1017.           char *fullform;
  1018.           struct cleanup *cleanup;

  1019.           canonical = &self->canonical_names[j];
  1020.           fullform = canonical_to_fullform (canonical);
  1021.           cleanup = make_cleanup (xfree, fullform);

  1022.           if (strcmp (name, fullform) == 0)
  1023.             add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);

  1024.           do_cleanups (cleanup);
  1025.         }

  1026.       if (lsal.sals.nelts > 0)
  1027.         {
  1028.           lsal.canonical = xstrdup (name);
  1029.           VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
  1030.         }
  1031.     }

  1032.   self->canonical->pre_expanded = 0;
  1033. }

  1034. /* Store RESULT into SELF->CANONICAL.  */

  1035. static void
  1036. convert_results_to_lsals (struct linespec_state *self,
  1037.                           struct symtabs_and_lines *result)
  1038. {
  1039.   struct linespec_sals lsal;

  1040.   lsal.canonical = NULL;
  1041.   lsal.sals = *result;
  1042.   VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
  1043. }

  1044. /* A structure that contains two string representations of a struct
  1045.    linespec_canonical_name:
  1046.      - one where the the symtab's fullname is used;
  1047.      - one where the filename followed the "set filename-display"
  1048.        setting.  */

  1049. struct decode_line_2_item
  1050. {
  1051.   /* The form using symtab_to_fullname.
  1052.      It must be xfree'ed after use.  */
  1053.   char *fullform;

  1054.   /* The form using symtab_to_filename_for_display.
  1055.      It must be xfree'ed after use.  */
  1056.   char *displayform;

  1057.   /* Field is initialized to zero and it is set to one if the user
  1058.      requested breakpoint for this entry.  */
  1059.   unsigned int selected : 1;
  1060. };

  1061. /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
  1062.    secondarily by FULLFORM.  */

  1063. static int
  1064. decode_line_2_compare_items (const void *ap, const void *bp)
  1065. {
  1066.   const struct decode_line_2_item *a = ap;
  1067.   const struct decode_line_2_item *b = bp;
  1068.   int retval;

  1069.   retval = strcmp (a->displayform, b->displayform);
  1070.   if (retval != 0)
  1071.     return retval;

  1072.   return strcmp (a->fullform, b->fullform);
  1073. }

  1074. /* Handle multiple results in RESULT depending on SELECT_MODE.  This
  1075.    will either return normally, throw an exception on multiple
  1076.    results, or present a menu to the user.  On return, the SALS vector
  1077.    in SELF->CANONICAL is set up properly.  */

  1078. static void
  1079. decode_line_2 (struct linespec_state *self,
  1080.                struct symtabs_and_lines *result,
  1081.                const char *select_mode)
  1082. {
  1083.   char *args, *prompt;
  1084.   int i;
  1085.   struct cleanup *old_chain;
  1086.   VEC (const_char_ptr) *filters = NULL;
  1087.   struct get_number_or_range_state state;
  1088.   struct decode_line_2_item *items;
  1089.   int items_count;

  1090.   gdb_assert (select_mode != multiple_symbols_all);
  1091.   gdb_assert (self->canonical != NULL);
  1092.   gdb_assert (result->nelts >= 1);

  1093.   old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);

  1094.   /* Prepare ITEMS array.  */
  1095.   items_count = result->nelts;
  1096.   items = xmalloc (sizeof (*items) * items_count);
  1097.   make_cleanup (xfree, items);
  1098.   for (i = 0; i < items_count; ++i)
  1099.     {
  1100.       const struct linespec_canonical_name *canonical;
  1101.       struct decode_line_2_item *item;

  1102.       canonical = &self->canonical_names[i];
  1103.       gdb_assert (canonical->suffix != NULL);
  1104.       item = &items[i];

  1105.       item->fullform = canonical_to_fullform (canonical);
  1106.       make_cleanup (xfree, item->fullform);

  1107.       if (canonical->symtab == NULL)
  1108.         item->displayform = canonical->suffix;
  1109.       else
  1110.         {
  1111.           const char *fn_for_display;

  1112.           fn_for_display = symtab_to_filename_for_display (canonical->symtab);
  1113.           item->displayform = xstrprintf ("%s:%s", fn_for_display,
  1114.                                           canonical->suffix);
  1115.           make_cleanup (xfree, item->displayform);
  1116.         }

  1117.       item->selected = 0;
  1118.     }

  1119.   /* Sort the list of method names.  */
  1120.   qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);

  1121.   /* Remove entries with the same FULLFORM.  */
  1122.   if (items_count >= 2)
  1123.     {
  1124.       struct decode_line_2_item *dst, *src;

  1125.       dst = items;
  1126.       for (src = &items[1]; src < &items[items_count]; src++)
  1127.         if (strcmp (src->fullform, dst->fullform) != 0)
  1128.           *++dst = *src;
  1129.       items_count = dst + 1 - items;
  1130.     }

  1131.   if (select_mode == multiple_symbols_cancel && items_count > 1)
  1132.     error (_("canceled because the command is ambiguous\n"
  1133.              "See set/show multiple-symbol."));

  1134.   if (select_mode == multiple_symbols_all || items_count == 1)
  1135.     {
  1136.       do_cleanups (old_chain);
  1137.       convert_results_to_lsals (self, result);
  1138.       return;
  1139.     }

  1140.   printf_unfiltered (_("[0] cancel\n[1] all\n"));
  1141.   for (i = 0; i < items_count; i++)
  1142.     printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);

  1143.   prompt = getenv ("PS2");
  1144.   if (prompt == NULL)
  1145.     {
  1146.       prompt = "> ";
  1147.     }
  1148.   args = command_line_input (prompt, 0, "overload-choice");

  1149.   if (args == 0 || *args == 0)
  1150.     error_no_arg (_("one or more choice numbers"));

  1151.   init_number_or_range (&state, args);
  1152.   while (!state.finished)
  1153.     {
  1154.       int num;

  1155.       num = get_number_or_range (&state);

  1156.       if (num == 0)
  1157.         error (_("canceled"));
  1158.       else if (num == 1)
  1159.         {
  1160.           /* We intentionally make this result in a single breakpoint,
  1161.              contrary to what older versions of gdb did.  The
  1162.              rationale is that this lets a user get the
  1163.              multiple_symbols_all behavior even with the 'ask'
  1164.              setting; and he can get separate breakpoints by entering
  1165.              "2-57" at the query.  */
  1166.           do_cleanups (old_chain);
  1167.           convert_results_to_lsals (self, result);
  1168.           return;
  1169.         }

  1170.       num -= 2;
  1171.       if (num >= items_count)
  1172.         printf_unfiltered (_("No choice number %d.\n"), num);
  1173.       else
  1174.         {
  1175.           struct decode_line_2_item *item = &items[num];

  1176.           if (!item->selected)
  1177.             {
  1178.               VEC_safe_push (const_char_ptr, filters, item->fullform);
  1179.               item->selected = 1;
  1180.             }
  1181.           else
  1182.             {
  1183.               printf_unfiltered (_("duplicate request for %d ignored.\n"),
  1184.                                  num + 2);
  1185.             }
  1186.         }
  1187.     }

  1188.   filter_results (self, result, filters);
  1189.   do_cleanups (old_chain);
  1190. }



  1191. /* The parser of linespec itself.  */

  1192. /* Throw an appropriate error when SYMBOL is not found (optionally in
  1193.    FILENAME).  */

  1194. static void ATTRIBUTE_NORETURN
  1195. symbol_not_found_error (const char *symbol, const char *filename)
  1196. {
  1197.   if (symbol == NULL)
  1198.     symbol = "";

  1199.   if (!have_full_symbols ()
  1200.       && !have_partial_symbols ()
  1201.       && !have_minimal_symbols ())
  1202.     throw_error (NOT_FOUND_ERROR,
  1203.                  _("No symbol table is loaded.  Use the \"file\" command."));

  1204.   /* If SYMBOL starts with '$', the user attempted to either lookup
  1205.      a function/variable in his code starting with '$' or an internal
  1206.      variable of that name.  Since we do not know which, be concise and
  1207.      explain both possibilities.  */
  1208.   if (*symbol == '$')
  1209.     {
  1210.       if (filename)
  1211.         throw_error (NOT_FOUND_ERROR,
  1212.                      _("Undefined convenience variable or function \"%s\" "
  1213.                        "not defined in \"%s\"."), symbol, filename);
  1214.       else
  1215.         throw_error (NOT_FOUND_ERROR,
  1216.                      _("Undefined convenience variable or function \"%s\" "
  1217.                        "not defined."), symbol);
  1218.     }
  1219.   else
  1220.     {
  1221.       if (filename)
  1222.         throw_error (NOT_FOUND_ERROR,
  1223.                      _("Function \"%s\" not defined in \"%s\"."),
  1224.                      symbol, filename);
  1225.       else
  1226.         throw_error (NOT_FOUND_ERROR,
  1227.                      _("Function \"%s\" not defined."), symbol);
  1228.     }
  1229. }

  1230. /* Throw an appropriate error when an unexpected token is encountered
  1231.    in the input.  */

  1232. static void ATTRIBUTE_NORETURN
  1233. unexpected_linespec_error (linespec_parser *parser)
  1234. {
  1235.   linespec_token token;
  1236.   static const char * token_type_strings[]
  1237.     = {"keyword", "colon", "string", "number", "comma", "end of input"};

  1238.   /* Get the token that generated the error.  */
  1239.   token = linespec_lexer_lex_one (parser);

  1240.   /* Finally, throw the error.  */
  1241.   if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
  1242.       || token.type == LSTOKEN_KEYWORD)
  1243.     {
  1244.       char *string;
  1245.       struct cleanup *cleanup;

  1246.       string = copy_token_string (token);
  1247.       cleanup = make_cleanup (xfree, string);
  1248.       throw_error (GENERIC_ERROR,
  1249.                    _("malformed linespec error: unexpected %s, \"%s\""),
  1250.                    token_type_strings[token.type], string);
  1251.     }
  1252.   else
  1253.     throw_error (GENERIC_ERROR,
  1254.                  _("malformed linespec error: unexpected %s"),
  1255.                  token_type_strings[token.type]);
  1256. }

  1257. /* Parse and return a line offset in STRING.  */

  1258. static struct line_offset
  1259. linespec_parse_line_offset (const char *string)
  1260. {
  1261.   struct line_offset line_offset = {0, LINE_OFFSET_NONE};

  1262.   if (*string == '+')
  1263.     {
  1264.       line_offset.sign = LINE_OFFSET_PLUS;
  1265.       ++string;
  1266.     }
  1267.   else if (*string == '-')
  1268.     {
  1269.       line_offset.sign = LINE_OFFSET_MINUS;
  1270.       ++string;
  1271.     }

  1272.   /* Right now, we only allow base 10 for offsets.  */
  1273.   line_offset.offset = atoi (string);
  1274.   return line_offset;
  1275. }

  1276. /* Parse the basic_spec in PARSER's input.  */

  1277. static void
  1278. linespec_parse_basic (linespec_parser *parser)
  1279. {
  1280.   char *name;
  1281.   linespec_token token;
  1282.   VEC (symbolp) *symbols, *labels;
  1283.   VEC (bound_minimal_symbol_d) *minimal_symbols;
  1284.   struct cleanup *cleanup;

  1285.   /* Get the next token.  */
  1286.   token = linespec_lexer_lex_one (parser);

  1287.   /* If it is EOI or KEYWORD, issue an error.  */
  1288.   if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
  1289.     unexpected_linespec_error (parser);
  1290.   /* If it is a LSTOKEN_NUMBER, we have an offset.  */
  1291.   else if (token.type == LSTOKEN_NUMBER)
  1292.     {
  1293.       /* Record the line offset and get the next token.  */
  1294.       name = copy_token_string (token);
  1295.       cleanup = make_cleanup (xfree, name);
  1296.       PARSER_RESULT (parser)->line_offset = linespec_parse_line_offset (name);
  1297.       do_cleanups (cleanup);

  1298.       /* Get the next token.  */
  1299.       token = linespec_lexer_consume_token (parser);

  1300.       /* If the next token is a comma, stop parsing and return.  */
  1301.       if (token.type == LSTOKEN_COMMA)
  1302.         return;

  1303.       /* If the next token is anything but EOI or KEYWORD, issue
  1304.          an error.  */
  1305.       if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
  1306.         unexpected_linespec_error (parser);
  1307.     }

  1308.   if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
  1309.     return;

  1310.   /* Next token must be LSTOKEN_STRING.  */
  1311.   if (token.type != LSTOKEN_STRING)
  1312.     unexpected_linespec_error (parser);

  1313.   /* The current token will contain the name of a function, method,
  1314.      or label.  */
  1315.   name  = copy_token_string (token);
  1316.   cleanup = make_cleanup (xfree, name);

  1317.   /* Try looking it up as a function/method.  */
  1318.   find_linespec_symbols (PARSER_STATE (parser),
  1319.                          PARSER_RESULT (parser)->file_symtabs, name,
  1320.                          &symbols, &minimal_symbols);

  1321.   if (symbols != NULL || minimal_symbols != NULL)
  1322.     {
  1323.       PARSER_RESULT (parser)->function_symbols = symbols;
  1324.       PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
  1325.       PARSER_RESULT (parser)->function_name = name;
  1326.       symbols = NULL;
  1327.       discard_cleanups (cleanup);
  1328.     }
  1329.   else
  1330.     {
  1331.       /* NAME was not a function or a method.  So it must be a label
  1332.          name or user specified variable like "break foo.c:$zippo".  */
  1333.       labels = find_label_symbols (PARSER_STATE (parser), NULL,
  1334.                                    &symbols, name);
  1335.       if (labels != NULL)
  1336.         {
  1337.           PARSER_RESULT (parser)->labels.label_symbols = labels;
  1338.           PARSER_RESULT (parser)->labels.function_symbols = symbols;
  1339.           PARSER_RESULT (parser)->label_name = name;
  1340.           symbols = NULL;
  1341.           discard_cleanups (cleanup);
  1342.         }
  1343.       else if (token.type == LSTOKEN_STRING
  1344.                && *LS_TOKEN_STOKEN (token).ptr == '$')
  1345.         {
  1346.           /* User specified a convenience variable or history value.  */
  1347.           PARSER_RESULT (parser)->line_offset
  1348.             = linespec_parse_variable (PARSER_STATE (parser), name);

  1349.           if (PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
  1350.             {
  1351.               /* The user-specified variable was not valid.  Do not
  1352.                  throw an error here.  parse_linespec will do it for us.  */
  1353.               PARSER_RESULT (parser)->function_name = name;
  1354.               discard_cleanups (cleanup);
  1355.               return;
  1356.             }

  1357.           /* The convenience variable/history value parsed correctly.
  1358.              NAME is no longer needed.  */
  1359.           do_cleanups (cleanup);
  1360.         }
  1361.       else
  1362.         {
  1363.           /* The name is also not a label.  Abort parsing.  Do not throw
  1364.              an error here.  parse_linespec will do it for us.  */

  1365.           /* Save a copy of the name we were trying to lookup.  */
  1366.           PARSER_RESULT (parser)->function_name = name;
  1367.           discard_cleanups (cleanup);
  1368.           return;
  1369.         }
  1370.     }

  1371.   /* Get the next token.  */
  1372.   token = linespec_lexer_consume_token (parser);

  1373.   if (token.type == LSTOKEN_COLON)
  1374.     {
  1375.       /* User specified a label or a lineno.  */
  1376.       token = linespec_lexer_consume_token (parser);

  1377.       if (token.type == LSTOKEN_NUMBER)
  1378.         {
  1379.           /* User specified an offset.  Record the line offset and
  1380.              get the next token.  */
  1381.           name = copy_token_string (token);
  1382.           cleanup = make_cleanup (xfree, name);
  1383.           PARSER_RESULT (parser)->line_offset
  1384.             = linespec_parse_line_offset (name);
  1385.           do_cleanups (cleanup);

  1386.           /* Ge the next token.  */
  1387.           token = linespec_lexer_consume_token (parser);
  1388.         }
  1389.       else if (token.type == LSTOKEN_STRING)
  1390.         {
  1391.           /* Grab a copy of the label's name and look it up.  */
  1392.           name = copy_token_string (token);
  1393.           cleanup = make_cleanup (xfree, name);
  1394.           labels = find_label_symbols (PARSER_STATE (parser),
  1395.                                        PARSER_RESULT (parser)->function_symbols,
  1396.                                        &symbols, name);

  1397.           if (labels != NULL)
  1398.             {
  1399.               PARSER_RESULT (parser)->labels.label_symbols = labels;
  1400.               PARSER_RESULT (parser)->labels.function_symbols = symbols;
  1401.               PARSER_RESULT (parser)->label_name = name;
  1402.               symbols = NULL;
  1403.               discard_cleanups (cleanup);
  1404.             }
  1405.           else
  1406.             {
  1407.               /* We don't know what it was, but it isn't a label.  */
  1408.               throw_error (NOT_FOUND_ERROR,
  1409.                            _("No label \"%s\" defined in function \"%s\"."),
  1410.                            name, PARSER_RESULT (parser)->function_name);
  1411.             }

  1412.           /* Check for a line offset.  */
  1413.           token = linespec_lexer_consume_token (parser);
  1414.           if (token.type == LSTOKEN_COLON)
  1415.             {
  1416.               /* Get the next token.  */
  1417.               token = linespec_lexer_consume_token (parser);

  1418.               /* It must be a line offset.  */
  1419.               if (token.type != LSTOKEN_NUMBER)
  1420.                 unexpected_linespec_error (parser);

  1421.               /* Record the lione offset and get the next token.  */
  1422.               name = copy_token_string (token);
  1423.               cleanup = make_cleanup (xfree, name);

  1424.               PARSER_RESULT (parser)->line_offset
  1425.                 = linespec_parse_line_offset (name);
  1426.               do_cleanups (cleanup);

  1427.               /* Get the next token.  */
  1428.               token = linespec_lexer_consume_token (parser);
  1429.             }
  1430.         }
  1431.       else
  1432.         {
  1433.           /* Trailing ':' in the input. Issue an error.  */
  1434.           unexpected_linespec_error (parser);
  1435.         }
  1436.     }
  1437. }

  1438. /* Canonicalize the linespec contained in LS.  The result is saved into
  1439.    STATE->canonical.  */

  1440. static void
  1441. canonicalize_linespec (struct linespec_state *state, linespec_p ls)
  1442. {
  1443.   /* If canonicalization was not requested, no need to do anything.  */
  1444.   if (!state->canonical)
  1445.     return;

  1446.   /* Shortcut expressions, which can only appear by themselves.  */
  1447.   if (ls->expression != NULL)
  1448.     state->canonical->addr_string = xstrdup (ls->expression);
  1449.   else
  1450.     {
  1451.       struct ui_file *buf;
  1452.       int need_colon = 0;

  1453.       buf = mem_fileopen ();
  1454.       if (ls->source_filename)
  1455.         {
  1456.           fputs_unfiltered (ls->source_filename, buf);
  1457.           need_colon = 1;
  1458.         }

  1459.       if (ls->function_name)
  1460.         {
  1461.           if (need_colon)
  1462.             fputc_unfiltered (':', buf);
  1463.           fputs_unfiltered (ls->function_name, buf);
  1464.           need_colon = 1;
  1465.         }

  1466.       if (ls->label_name)
  1467.         {
  1468.           if (need_colon)
  1469.             fputc_unfiltered (':', buf);

  1470.           if (ls->function_name == NULL)
  1471.             {
  1472.               struct symbol *s;

  1473.               /* No function was specified, so add the symbol name.  */
  1474.               gdb_assert (ls->labels.function_symbols != NULL
  1475.                           && (VEC_length (symbolp, ls->labels.function_symbols)
  1476.                               == 1));
  1477.               s = VEC_index (symbolp, ls->labels.function_symbols, 0);
  1478.               fputs_unfiltered (SYMBOL_NATURAL_NAME (s), buf);
  1479.               fputc_unfiltered (':', buf);
  1480.             }

  1481.           fputs_unfiltered (ls->label_name, buf);
  1482.           need_colon = 1;
  1483.           state->canonical->special_display = 1;
  1484.         }

  1485.       if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
  1486.         {
  1487.           if (need_colon)
  1488.             fputc_unfiltered (':', buf);
  1489.           fprintf_filtered (buf, "%s%d",
  1490.                             (ls->line_offset.sign == LINE_OFFSET_NONE ? ""
  1491.                              : (ls->line_offset.sign
  1492.                                 == LINE_OFFSET_PLUS ? "+" : "-")),
  1493.                             ls->line_offset.offset);
  1494.         }

  1495.       state->canonical->addr_string = ui_file_xstrdup (buf, NULL);
  1496.       ui_file_delete (buf);
  1497.     }
  1498. }

  1499. /* Given a line offset in LS, construct the relevant SALs.  */

  1500. static struct symtabs_and_lines
  1501. create_sals_line_offset (struct linespec_state *self,
  1502.                          linespec_p ls)
  1503. {
  1504.   struct symtabs_and_lines values;
  1505.   struct symtab_and_line val;
  1506.   int use_default = 0;

  1507.   init_sal (&val);
  1508.   values.sals = NULL;
  1509.   values.nelts = 0;

  1510.   /* This is where we need to make sure we have good defaults.
  1511.      We must guarantee that this section of code is never executed
  1512.      when we are called with just a function name, since
  1513.      set_default_source_symtab_and_line uses
  1514.      select_source_symtab that calls us with such an argument.  */

  1515.   if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
  1516.       && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
  1517.     {
  1518.       const char *fullname;

  1519.       set_current_program_space (self->program_space);

  1520.       /* Make sure we have at least a default source line.  */
  1521.       set_default_source_symtab_and_line ();
  1522.       initialize_defaults (&self->default_symtab, &self->default_line);
  1523.       fullname = symtab_to_fullname (self->default_symtab);
  1524.       VEC_pop (symtab_ptr, ls->file_symtabs);
  1525.       VEC_free (symtab_ptr, ls->file_symtabs);
  1526.       ls->file_symtabs = collect_symtabs_from_filename (fullname);
  1527.       use_default = 1;
  1528.     }

  1529.   val.line = ls->line_offset.offset;
  1530.   switch (ls->line_offset.sign)
  1531.     {
  1532.     case LINE_OFFSET_PLUS:
  1533.       if (ls->line_offset.offset == 0)
  1534.         val.line = 5;
  1535.       if (use_default)
  1536.         val.line = self->default_line + val.line;
  1537.       break;

  1538.     case LINE_OFFSET_MINUS:
  1539.       if (ls->line_offset.offset == 0)
  1540.         val.line = 15;
  1541.       if (use_default)
  1542.         val.line = self->default_line - val.line;
  1543.       else
  1544.         val.line = -val.line;
  1545.       break;

  1546.     case LINE_OFFSET_NONE:
  1547.       break;                        /* No need to adjust val.line.  */
  1548.     }

  1549.   if (self->list_mode)
  1550.     decode_digits_list_mode (self, ls, &values, val);
  1551.   else
  1552.     {
  1553.       struct linetable_entry *best_entry = NULL;
  1554.       int *filter;
  1555.       const struct block **blocks;
  1556.       struct cleanup *cleanup;
  1557.       struct symtabs_and_lines intermediate_results;
  1558.       int i, j;

  1559.       intermediate_results.sals = NULL;
  1560.       intermediate_results.nelts = 0;

  1561.       decode_digits_ordinary (self, ls, val.line, &intermediate_results,
  1562.                               &best_entry);
  1563.       if (intermediate_results.nelts == 0 && best_entry != NULL)
  1564.         decode_digits_ordinary (self, ls, best_entry->line,
  1565.                                 &intermediate_results, &best_entry);

  1566.       cleanup = make_cleanup (xfree, intermediate_results.sals);

  1567.       /* For optimized code, the compiler can scatter one source line
  1568.          across disjoint ranges of PC values, even when no duplicate
  1569.          functions or inline functions are involved.  For example,
  1570.          'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
  1571.          function can result in two PC ranges.  In this case, we don't
  1572.          want to set a breakpoint on the first PC of each range.  To filter
  1573.          such cases, we use containing blocks -- for each PC found
  1574.          above, we see if there are other PCs that are in the same
  1575.          block.  If yes, the other PCs are filtered out.  */

  1576.       filter = XNEWVEC (int, intermediate_results.nelts);
  1577.       make_cleanup (xfree, filter);
  1578.       blocks = XNEWVEC (const struct block *, intermediate_results.nelts);
  1579.       make_cleanup (xfree, blocks);

  1580.       for (i = 0; i < intermediate_results.nelts; ++i)
  1581.         {
  1582.           set_current_program_space (intermediate_results.sals[i].pspace);

  1583.           filter[i] = 1;
  1584.           blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
  1585.                                          intermediate_results.sals[i].section);
  1586.         }

  1587.       for (i = 0; i < intermediate_results.nelts; ++i)
  1588.         {
  1589.           if (blocks[i] != NULL)
  1590.             for (j = i + 1; j < intermediate_results.nelts; ++j)
  1591.               {
  1592.                 if (blocks[j] == blocks[i])
  1593.                   {
  1594.                     filter[j] = 0;
  1595.                     break;
  1596.                   }
  1597.               }
  1598.         }

  1599.       for (i = 0; i < intermediate_results.nelts; ++i)
  1600.         if (filter[i])
  1601.           {
  1602.             struct symbol *sym = (blocks[i]
  1603.                                   ? block_containing_function (blocks[i])
  1604.                                   : NULL);

  1605.             if (self->funfirstline)
  1606.               skip_prologue_sal (&intermediate_results.sals[i]);
  1607.             /* Make sure the line matches the request, not what was
  1608.                found.  */
  1609.             intermediate_results.sals[i].line = val.line;
  1610.             add_sal_to_sals (self, &values, &intermediate_results.sals[i],
  1611.                              sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
  1612.           }

  1613.       do_cleanups (cleanup);
  1614.     }

  1615.   if (values.nelts == 0)
  1616.     {
  1617.       if (ls->source_filename)
  1618.         throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
  1619.                      val.line, ls->source_filename);
  1620.       else
  1621.         throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
  1622.                      val.line);
  1623.     }

  1624.   return values;
  1625. }

  1626. /* Create and return SALs from the linespec LS.  */

  1627. static struct symtabs_and_lines
  1628. convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
  1629. {
  1630.   struct symtabs_and_lines sals = {NULL, 0};

  1631.   if (ls->expression != NULL)
  1632.     {
  1633.       struct symtab_and_line sal;

  1634.       /* We have an expression.  No other attribute is allowed.  */
  1635.       sal = find_pc_line (ls->expr_pc, 0);
  1636.       sal.pc = ls->expr_pc;
  1637.       sal.section = find_pc_overlay (ls->expr_pc);
  1638.       sal.explicit_pc = 1;
  1639.       add_sal_to_sals (state, &sals, &sal, ls->expression, 1);
  1640.     }
  1641.   else if (ls->labels.label_symbols != NULL)
  1642.     {
  1643.       /* We have just a bunch of functions/methods or labels.  */
  1644.       int i;
  1645.       struct symtab_and_line sal;
  1646.       struct symbol *sym;

  1647.       for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
  1648.         {
  1649.           struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));

  1650.           if (symbol_to_sal (&sal, state->funfirstline, sym)
  1651.               && maybe_add_address (state->addr_set, pspace, sal.pc))
  1652.             add_sal_to_sals (state, &sals, &sal,
  1653.                              SYMBOL_NATURAL_NAME (sym), 0);
  1654.         }
  1655.     }
  1656.   else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
  1657.     {
  1658.       /* We have just a bunch of functions and/or methods.  */
  1659.       int i;
  1660.       struct symtab_and_line sal;
  1661.       struct symbol *sym;
  1662.       bound_minimal_symbol_d *elem;
  1663.       struct program_space *pspace;

  1664.       if (ls->function_symbols != NULL)
  1665.         {
  1666.           /* Sort symbols so that symbols with the same program space are next
  1667.              to each other.  */
  1668.           qsort (VEC_address (symbolp, ls->function_symbols),
  1669.                  VEC_length (symbolp, ls->function_symbols),
  1670.                  sizeof (symbolp), compare_symbols);

  1671.           for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
  1672.             {
  1673.               pspace = SYMTAB_PSPACE (symbol_symtab (sym));
  1674.               set_current_program_space (pspace);
  1675.               if (symbol_to_sal (&sal, state->funfirstline, sym)
  1676.                   && maybe_add_address (state->addr_set, pspace, sal.pc))
  1677.                 add_sal_to_sals (state, &sals, &sal,
  1678.                                  SYMBOL_NATURAL_NAME (sym), 0);
  1679.             }
  1680.         }

  1681.       if (ls->minimal_symbols != NULL)
  1682.         {
  1683.           /* Sort minimal symbols by program space, too.  */
  1684.           qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
  1685.                  VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
  1686.                  sizeof (bound_minimal_symbol_d), compare_msymbols);

  1687.           for (i = 0;
  1688.                VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
  1689.                             i, elem);
  1690.                ++i)
  1691.             {
  1692.               pspace = elem->objfile->pspace;
  1693.               set_current_program_space (pspace);
  1694.               minsym_found (state, elem->objfile, elem->minsym, &sals);
  1695.             }
  1696.         }
  1697.     }
  1698.   else if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
  1699.     {
  1700.       /* Only an offset was specified.  */
  1701.         sals = create_sals_line_offset (state, ls);

  1702.         /* Make sure we have a filename for canonicalization.  */
  1703.         if (ls->source_filename == NULL)
  1704.           {
  1705.             const char *fullname = symtab_to_fullname (state->default_symtab);

  1706.             /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
  1707.                form so that displaying SOURCE_FILENAME can follow the current
  1708.                FILENAME_DISPLAY_STRING setting.  But as it is used only rarely
  1709.                it has been kept for code simplicity only in absolute form.  */
  1710.             ls->source_filename = xstrdup (fullname);
  1711.           }
  1712.     }
  1713.   else
  1714.     {
  1715.       /* We haven't found any results...  */
  1716.       return sals;
  1717.     }

  1718.   canonicalize_linespec (state, ls);

  1719.   if (sals.nelts > 0 && state->canonical != NULL)
  1720.     state->canonical->pre_expanded = 1;

  1721.   return sals;
  1722. }

  1723. /* Parse a string that specifies a linespec.
  1724.    Pass the address of a char * variable; that variable will be
  1725.    advanced over the characters actually parsed.

  1726.    The basic grammar of linespecs:

  1727.    linespec -> expr_spec | var_spec | basic_spec
  1728.    expr_spec -> '*' STRING
  1729.    var_spec -> '$' (STRING | NUMBER)

  1730.    basic_spec -> file_offset_spec | function_spec | label_spec
  1731.    file_offset_spec -> opt_file_spec offset_spec
  1732.    function_spec -> opt_file_spec function_name_spec opt_label_spec
  1733.    label_spec -> label_name_spec

  1734.    opt_file_spec -> "" | file_name_spec ':'
  1735.    opt_label_spec -> "" | ':' label_name_spec

  1736.    file_name_spec -> STRING
  1737.    function_name_spec -> STRING
  1738.    label_name_spec -> STRING
  1739.    function_name_spec -> STRING
  1740.    offset_spec -> NUMBER
  1741.                -> '+' NUMBER
  1742.                -> '-' NUMBER

  1743.    This may all be followed by several keywords such as "if EXPR",
  1744.    which we ignore.

  1745.    A comma will terminate parsing.

  1746.    The function may be an undebuggable function found in minimal symbol table.

  1747.    If the argument FUNFIRSTLINE is nonzero, we want the first line
  1748.    of real code inside a function when a function is specified, and it is
  1749.    not OK to specify a variable or type to get its line number.

  1750.    DEFAULT_SYMTAB specifies the file to use if none is specified.
  1751.    It defaults to current_source_symtab.
  1752.    DEFAULT_LINE specifies the line number to use for relative
  1753.    line numbers (that start with signs).  Defaults to current_source_line.
  1754.    If CANONICAL is non-NULL, store an array of strings containing the canonical
  1755.    line specs there if necessary.  Currently overloaded member functions and
  1756.    line numbers or static functions without a filename yield a canonical
  1757.    line spec.  The array and the line spec strings are allocated on the heap,
  1758.    it is the callers responsibility to free them.

  1759.    Note that it is possible to return zero for the symtab
  1760.    if no file is validly specified.  Callers must check that.
  1761.    Also, the line number returned may be invalid.  */

  1762. /* Parse the linespec in ARGPTR.  */

  1763. static struct symtabs_and_lines
  1764. parse_linespec (linespec_parser *parser, const char **argptr)
  1765. {
  1766.   linespec_token token;
  1767.   struct symtabs_and_lines values;
  1768.   volatile struct gdb_exception file_exception;
  1769.   struct cleanup *cleanup;

  1770.   /* A special case to start.  It has become quite popular for
  1771.      IDEs to work around bugs in the previous parser by quoting
  1772.      the entire linespec, so we attempt to deal with this nicely.  */
  1773.   parser->is_quote_enclosed = 0;
  1774.   if (!is_ada_operator (*argptr)
  1775.       && strchr (linespec_quote_characters, **argptr) != NULL)
  1776.     {
  1777.       const char *end;

  1778.       end = skip_quote_char (*argptr + 1, **argptr);
  1779.       if (end != NULL && is_closing_quote_enclosed (end))
  1780.         {
  1781.           /* Here's the special case.  Skip ARGPTR past the initial
  1782.              quote.  */
  1783.           ++(*argptr);
  1784.           parser->is_quote_enclosed = 1;
  1785.         }
  1786.     }

  1787.   /* A keyword at the start cannot be interpreted as such.
  1788.      Consider "b thread thread 42".  */
  1789.   parser->keyword_ok = 0;

  1790.   parser->lexer.saved_arg = *argptr;
  1791.   parser->lexer.stream = argptr;
  1792.   file_exception.reason = 0;

  1793.   /* Initialize the default symtab and line offset.  */
  1794.   initialize_defaults (&PARSER_STATE (parser)->default_symtab,
  1795.                        &PARSER_STATE (parser)->default_line);

  1796.   /* Objective-C shortcut.  */
  1797.   values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), argptr);
  1798.   if (values.sals != NULL)
  1799.     return values;

  1800.   /* Start parsing.  */

  1801.   /* Get the first token.  */
  1802.   token = linespec_lexer_lex_one (parser);

  1803.   /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER.  */
  1804.   if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '*')
  1805.     {
  1806.       char *expr;
  1807.       const char *copy;

  1808.       /* User specified an expression, *EXPR.  */
  1809.       copy = expr = copy_token_string (token);
  1810.       cleanup = make_cleanup (xfree, expr);
  1811.       PARSER_RESULT (parser)->expr_pc = linespec_expression_to_pc (&copy);
  1812.       discard_cleanups (cleanup);
  1813.       PARSER_RESULT (parser)->expression = expr;

  1814.       /* This is a little hacky/tricky.  If linespec_expression_to_pc
  1815.          did not evaluate the entire token, then we must find the
  1816.          string COPY inside the original token buffer.  */
  1817.       if (*copy != '\0')
  1818.         {
  1819.           PARSER_STREAM (parser) = strstr (parser->lexer.saved_arg, copy);
  1820.           gdb_assert (PARSER_STREAM (parser) != NULL);
  1821.         }

  1822.       /* Consume the token.  */
  1823.       linespec_lexer_consume_token (parser);

  1824.       goto convert_to_sals;
  1825.     }
  1826.   else if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
  1827.     {
  1828.       char *var;

  1829.       /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
  1830.       VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);

  1831.       /* User specified a convenience variable or history value.  */
  1832.       var = copy_token_string (token);
  1833.       cleanup = make_cleanup (xfree, var);
  1834.       PARSER_RESULT (parser)->line_offset
  1835.         = linespec_parse_variable (PARSER_STATE (parser), var);
  1836.       do_cleanups (cleanup);

  1837.       /* If a line_offset wasn't found (VAR is the name of a user
  1838.          variable/function), then skip to normal symbol processing.  */
  1839.       if (PARSER_RESULT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
  1840.         {
  1841.           /* Consume this token.  */
  1842.           linespec_lexer_consume_token (parser);

  1843.           goto convert_to_sals;
  1844.         }
  1845.     }
  1846.   else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
  1847.     unexpected_linespec_error (parser);

  1848.   /* Now we can recognize keywords.  */
  1849.   parser->keyword_ok = 1;

  1850.   /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
  1851.      this token cannot represent a filename.  */
  1852.   token = linespec_lexer_peek_token (parser);

  1853.   if (token.type == LSTOKEN_COLON)
  1854.     {
  1855.       char *user_filename;

  1856.       /* Get the current token again and extract the filename.  */
  1857.       token = linespec_lexer_lex_one (parser);
  1858.       user_filename = copy_token_string (token);

  1859.       /* Check if the input is a filename.  */
  1860.       TRY_CATCH (file_exception, RETURN_MASK_ERROR)
  1861.         {
  1862.           PARSER_RESULT (parser)->file_symtabs
  1863.             = symtabs_from_filename (user_filename);
  1864.         }

  1865.       if (file_exception.reason >= 0)
  1866.         {
  1867.           /* Symtabs were found for the file.  Record the filename.  */
  1868.           PARSER_RESULT (parser)->source_filename = user_filename;

  1869.           /* Get the next token.  */
  1870.           token = linespec_lexer_consume_token (parser);

  1871.           /* This is LSTOKEN_COLON; consume it.  */
  1872.           linespec_lexer_consume_token (parser);
  1873.         }
  1874.       else
  1875.         {
  1876.           /* No symtabs found -- discard user_filename.  */
  1877.           xfree (user_filename);

  1878.           /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
  1879.           VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
  1880.         }
  1881.     }
  1882.   /* If the next token is not EOI, KEYWORD, or COMMA, issue an error.  */
  1883.   else if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
  1884.            && token.type != LSTOKEN_COMMA)
  1885.     {
  1886.       /* TOKEN is the _next_ token, not the one currently in the parser.
  1887.          Consuming the token will give the correct error message.  */
  1888.       linespec_lexer_consume_token (parser);
  1889.       unexpected_linespec_error (parser);
  1890.     }
  1891.   else
  1892.     {
  1893.       /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
  1894.       VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
  1895.     }

  1896.   /* Parse the rest of the linespec.  */
  1897.   linespec_parse_basic (parser);

  1898.   if (PARSER_RESULT (parser)->function_symbols == NULL
  1899.       && PARSER_RESULT (parser)->labels.label_symbols == NULL
  1900.       && PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
  1901.       && PARSER_RESULT (parser)->minimal_symbols == NULL)
  1902.     {
  1903.       /* The linespec didn't parse.  Re-throw the file exception if
  1904.          there was one.  */
  1905.       if (file_exception.reason < 0)
  1906.         throw_exception (file_exception);

  1907.       /* Otherwise, the symbol is not found.  */
  1908.       symbol_not_found_error (PARSER_RESULT (parser)->function_name,
  1909.                               PARSER_RESULT (parser)->source_filename);
  1910.     }

  1911. convert_to_sals:

  1912.   /* Get the last token and record how much of the input was parsed,
  1913.      if necessary.  */
  1914.   token = linespec_lexer_lex_one (parser);
  1915.   if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
  1916.     PARSER_STREAM (parser) = LS_TOKEN_STOKEN (token).ptr;

  1917.   /* Convert the data in PARSER_RESULT to SALs.  */
  1918.   values = convert_linespec_to_sals (PARSER_STATE (parser),
  1919.                                      PARSER_RESULT (parser));

  1920.   return values;
  1921. }


  1922. /* A constructor for linespec_state.  */

  1923. static void
  1924. linespec_state_constructor (struct linespec_state *self,
  1925.                             int flags, const struct language_defn *language,
  1926.                             struct symtab *default_symtab,
  1927.                             int default_line,
  1928.                             struct linespec_result *canonical)
  1929. {
  1930.   memset (self, 0, sizeof (*self));
  1931.   self->language = language;
  1932.   self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
  1933.   self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
  1934.   self->default_symtab = default_symtab;
  1935.   self->default_line = default_line;
  1936.   self->canonical = canonical;
  1937.   self->program_space = current_program_space;
  1938.   self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
  1939.                                       xfree, xcalloc, xfree);
  1940. }

  1941. /* Initialize a new linespec parser.  */

  1942. static void
  1943. linespec_parser_new (linespec_parser *parser,
  1944.                      int flags, const struct language_defn *language,
  1945.                      struct symtab *default_symtab,
  1946.                      int default_line,
  1947.                      struct linespec_result *canonical)
  1948. {
  1949.   parser->lexer.current.type = LSTOKEN_CONSUMED;
  1950.   memset (PARSER_RESULT (parser), 0, sizeof (struct linespec));
  1951.   PARSER_RESULT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN;
  1952.   linespec_state_constructor (PARSER_STATE (parser), flags, language,
  1953.                               default_symtab, default_line, canonical);
  1954. }

  1955. /* A destructor for linespec_state.  */

  1956. static void
  1957. linespec_state_destructor (struct linespec_state *self)
  1958. {
  1959.   htab_delete (self->addr_set);
  1960. }

  1961. /* Delete a linespec parser.  */

  1962. static void
  1963. linespec_parser_delete (void *arg)
  1964. {
  1965.   linespec_parser *parser = (linespec_parser *) arg;

  1966.   xfree ((char *) PARSER_RESULT (parser)->expression);
  1967.   xfree ((char *) PARSER_RESULT (parser)->source_filename);
  1968.   xfree ((char *) PARSER_RESULT (parser)->label_name);
  1969.   xfree ((char *) PARSER_RESULT (parser)->function_name);

  1970.   if (PARSER_RESULT (parser)->file_symtabs != NULL)
  1971.     VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);

  1972.   if (PARSER_RESULT (parser)->function_symbols != NULL)
  1973.     VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);

  1974.   if (PARSER_RESULT (parser)->minimal_symbols != NULL)
  1975.     VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);

  1976.   if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
  1977.     VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);

  1978.   if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
  1979.     VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);

  1980.   linespec_state_destructor (PARSER_STATE (parser));
  1981. }

  1982. /* See linespec.h.  */

  1983. void
  1984. decode_line_full (char **argptr, int flags,
  1985.                   struct symtab *default_symtab,
  1986.                   int default_line, struct linespec_result *canonical,
  1987.                   const char *select_mode,
  1988.                   const char *filter)
  1989. {
  1990.   struct symtabs_and_lines result;
  1991.   struct cleanup *cleanups;
  1992.   VEC (const_char_ptr) *filters = NULL;
  1993.   linespec_parser parser;
  1994.   struct linespec_state *state;
  1995.   const char *copy, *orig;

  1996.   gdb_assert (canonical != NULL);
  1997.   /* The filter only makes sense for 'all'.  */
  1998.   gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
  1999.   gdb_assert (select_mode == NULL
  2000.               || select_mode == multiple_symbols_all
  2001.               || select_mode == multiple_symbols_ask
  2002.               || select_mode == multiple_symbols_cancel);
  2003.   gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);

  2004.   linespec_parser_new (&parser, flags, current_language, default_symtab,
  2005.                        default_line, canonical);
  2006.   cleanups = make_cleanup (linespec_parser_delete, &parser);
  2007.   save_current_program_space ();

  2008.   orig = copy = *argptr;
  2009.   result = parse_linespec (&parser, &copy);
  2010.   *argptr += copy - orig;
  2011.   state = PARSER_STATE (&parser);

  2012.   gdb_assert (result.nelts == 1 || canonical->pre_expanded);
  2013.   gdb_assert (canonical->addr_string != NULL);
  2014.   canonical->pre_expanded = 1;

  2015.   /* Arrange for allocated canonical names to be freed.  */
  2016.   if (result.nelts > 0)
  2017.     {
  2018.       int i;

  2019.       make_cleanup (xfree, state->canonical_names);
  2020.       for (i = 0; i < result.nelts; ++i)
  2021.         {
  2022.           gdb_assert (state->canonical_names[i].suffix != NULL);
  2023.           make_cleanup (xfree, state->canonical_names[i].suffix);
  2024.         }
  2025.     }

  2026.   if (select_mode == NULL)
  2027.     {
  2028.       if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
  2029.         select_mode = multiple_symbols_all;
  2030.       else
  2031.         select_mode = multiple_symbols_select_mode ();
  2032.     }

  2033.   if (select_mode == multiple_symbols_all)
  2034.     {
  2035.       if (filter != NULL)
  2036.         {
  2037.           make_cleanup (VEC_cleanup (const_char_ptr), &filters);
  2038.           VEC_safe_push (const_char_ptr, filters, filter);
  2039.           filter_results (state, &result, filters);
  2040.         }
  2041.       else
  2042.         convert_results_to_lsals (state, &result);
  2043.     }
  2044.   else
  2045.     decode_line_2 (state, &result, select_mode);

  2046.   do_cleanups (cleanups);
  2047. }

  2048. /* See linespec.h.  */

  2049. struct symtabs_and_lines
  2050. decode_line_1 (char **argptr, int flags,
  2051.                struct symtab *default_symtab,
  2052.                int default_line)
  2053. {
  2054.   struct symtabs_and_lines result;
  2055.   linespec_parser parser;
  2056.   struct cleanup *cleanups;
  2057.   const char *copy, *orig;

  2058.   linespec_parser_new (&parser, flags, current_language, default_symtab,
  2059.                        default_line, NULL);
  2060.   cleanups = make_cleanup (linespec_parser_delete, &parser);
  2061.   save_current_program_space ();

  2062.   orig = copy = *argptr;
  2063.   result = parse_linespec (&parser, &copy);
  2064.   *argptr += copy - orig;

  2065.   do_cleanups (cleanups);
  2066.   return result;
  2067. }

  2068. /* See linespec.h.  */

  2069. struct symtabs_and_lines
  2070. decode_line_with_current_source (char *string, int flags)
  2071. {
  2072.   struct symtabs_and_lines sals;
  2073.   struct symtab_and_line cursal;

  2074.   if (string == 0)
  2075.     error (_("Empty line specification."));

  2076.   /* We use whatever is set as the current source line.  We do not try
  2077.      and get a default source symtab+line or it will recursively call us!  */
  2078.   cursal = get_current_source_symtab_and_line ();

  2079.   sals = decode_line_1 (&string, flags,
  2080.                         cursal.symtab, cursal.line);

  2081.   if (*string)
  2082.     error (_("Junk at end of line specification: %s"), string);
  2083.   return sals;
  2084. }

  2085. /* See linespec.h.  */

  2086. struct symtabs_and_lines
  2087. decode_line_with_last_displayed (char *string, int flags)
  2088. {
  2089.   struct symtabs_and_lines sals;

  2090.   if (string == 0)
  2091.     error (_("Empty line specification."));

  2092.   if (last_displayed_sal_is_valid ())
  2093.     sals = decode_line_1 (&string, flags,
  2094.                           get_last_displayed_symtab (),
  2095.                           get_last_displayed_line ());
  2096.   else
  2097.     sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);

  2098.   if (*string)
  2099.     error (_("Junk at end of line specification: %s"), string);
  2100.   return sals;
  2101. }



  2102. /* First, some functions to initialize stuff at the beggining of the
  2103.    function.  */

  2104. static void
  2105. initialize_defaults (struct symtab **default_symtab, int *default_line)
  2106. {
  2107.   if (*default_symtab == 0)
  2108.     {
  2109.       /* Use whatever we have for the default source line.  We don't use
  2110.          get_current_or_default_symtab_and_line as it can recurse and call
  2111.          us back!  */
  2112.       struct symtab_and_line cursal =
  2113.         get_current_source_symtab_and_line ();

  2114.       *default_symtab = cursal.symtab;
  2115.       *default_line = cursal.line;
  2116.     }
  2117. }



  2118. /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
  2119.    advancing EXP_PTR past any parsed text.  */

  2120. static CORE_ADDR
  2121. linespec_expression_to_pc (const char **exp_ptr)
  2122. {
  2123.   if (current_program_space->executing_startup)
  2124.     /* The error message doesn't really matter, because this case
  2125.        should only hit during breakpoint reset.  */
  2126.     throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
  2127.                                     "program space is in startup"));

  2128.   (*exp_ptr)++;
  2129.   return value_as_address (parse_to_comma_and_eval (exp_ptr));
  2130. }



  2131. /* Here's where we recognise an Objective-C Selector.  An Objective C
  2132.    selector may be implemented by more than one class, therefore it
  2133.    may represent more than one method/function.  This gives us a
  2134.    situation somewhat analogous to C++ overloading.  If there's more
  2135.    than one method that could represent the selector, then use some of
  2136.    the existing C++ code to let the user choose one.  */

  2137. static struct symtabs_and_lines
  2138. decode_objc (struct linespec_state *self, linespec_p ls, const char **argptr)
  2139. {
  2140.   struct collect_info info;
  2141.   VEC (const_char_ptr) *symbol_names = NULL;
  2142.   struct symtabs_and_lines values;
  2143.   const char *new_argptr;
  2144.   struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
  2145.                                           &symbol_names);

  2146.   info.state = self;
  2147.   info.file_symtabs = NULL;
  2148.   VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
  2149.   make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
  2150.   info.result.symbols = NULL;
  2151.   info.result.minimal_symbols = NULL;
  2152.   values.nelts = 0;
  2153.   values.sals = NULL;

  2154.   new_argptr = find_imps (*argptr, &symbol_names);
  2155.   if (VEC_empty (const_char_ptr, symbol_names))
  2156.     {
  2157.       do_cleanups (cleanup);
  2158.       return values;
  2159.     }

  2160.   add_all_symbol_names_from_pspace (&info, NULL, symbol_names);

  2161.   if (!VEC_empty (symbolp, info.result.symbols)
  2162.       || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
  2163.     {
  2164.       char *saved_arg;

  2165.       saved_arg = alloca (new_argptr - *argptr + 1);
  2166.       memcpy (saved_arg, *argptr, new_argptr - *argptr);
  2167.       saved_arg[new_argptr - *argptr] = '\0';

  2168.       ls->function_name = xstrdup (saved_arg);
  2169.       ls->function_symbols = info.result.symbols;
  2170.       ls->minimal_symbols = info.result.minimal_symbols;
  2171.       values = convert_linespec_to_sals (self, ls);

  2172.       if (self->canonical)
  2173.         {
  2174.           self->canonical->pre_expanded = 1;
  2175.           if (ls->source_filename)
  2176.             self->canonical->addr_string
  2177.               = xstrprintf ("%s:%s", ls->source_filename, saved_arg);
  2178.           else
  2179.             self->canonical->addr_string = xstrdup (saved_arg);
  2180.         }
  2181.     }

  2182.   *argptr = new_argptr;

  2183.   do_cleanups (cleanup);

  2184.   return values;
  2185. }

  2186. /* An instance of this type is used when collecting prefix symbols for
  2187.    decode_compound.  */

  2188. struct decode_compound_collector
  2189. {
  2190.   /* The result vector.  */
  2191.   VEC (symbolp) *symbols;

  2192.   /* A hash table of all symbols we found.  We use this to avoid
  2193.      adding any symbol more than once.  */
  2194.   htab_t unique_syms;
  2195. };

  2196. /* A callback for iterate_over_symbols that is used by
  2197.    lookup_prefix_sym to collect type symbols.  */

  2198. static int
  2199. collect_one_symbol (struct symbol *sym, void *d)
  2200. {
  2201.   struct decode_compound_collector *collector = d;
  2202.   void **slot;
  2203.   struct type *t;

  2204.   if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  2205.     return 1; /* Continue iterating.  */

  2206.   t = SYMBOL_TYPE (sym);
  2207.   CHECK_TYPEDEF (t);
  2208.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  2209.       && TYPE_CODE (t) != TYPE_CODE_UNION
  2210.       && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
  2211.     return 1; /* Continue iterating.  */

  2212.   slot = htab_find_slot (collector->unique_syms, sym, INSERT);
  2213.   if (!*slot)
  2214.     {
  2215.       *slot = sym;
  2216.       VEC_safe_push (symbolp, collector->symbols, sym);
  2217.     }

  2218.   return 1; /* Continue iterating.  */
  2219. }

  2220. /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS.  */

  2221. static VEC (symbolp) *
  2222. lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
  2223.                    const char *class_name)
  2224. {
  2225.   int ix;
  2226.   struct symtab *elt;
  2227.   struct decode_compound_collector collector;
  2228.   struct cleanup *outer;
  2229.   struct cleanup *cleanup;

  2230.   collector.symbols = NULL;
  2231.   outer = make_cleanup (VEC_cleanup (symbolp), &collector.symbols);

  2232.   collector.unique_syms = htab_create_alloc (1, htab_hash_pointer,
  2233.                                              htab_eq_pointer, NULL,
  2234.                                              xcalloc, xfree);
  2235.   cleanup = make_cleanup_htab_delete (collector.unique_syms);

  2236.   for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
  2237.     {
  2238.       if (elt == NULL)
  2239.         {
  2240.           iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN,
  2241.                                              collect_one_symbol, &collector,
  2242.                                              NULL, 0);
  2243.           iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN,
  2244.                                              collect_one_symbol, &collector,
  2245.                                              NULL, 0);
  2246.         }
  2247.       else
  2248.         {
  2249.           /* Program spaces that are executing startup should have
  2250.              been filtered out earlier.  */
  2251.           gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
  2252.           set_current_program_space (SYMTAB_PSPACE (elt));
  2253.           iterate_over_file_blocks (elt, class_name, STRUCT_DOMAIN,
  2254.                                     collect_one_symbol, &collector);
  2255.           iterate_over_file_blocks (elt, class_name, VAR_DOMAIN,
  2256.                                     collect_one_symbol, &collector);
  2257.         }
  2258.     }

  2259.   do_cleanups (cleanup);
  2260.   discard_cleanups (outer);
  2261.   return collector.symbols;
  2262. }

  2263. /* A qsort comparison function for symbols.  The resulting order does
  2264.    not actually matter; we just need to be able to sort them so that
  2265.    symbols with the same program space end up next to each other.  */

  2266. static int
  2267. compare_symbols (const void *a, const void *b)
  2268. {
  2269.   struct symbol * const *sa = a;
  2270.   struct symbol * const *sb = b;
  2271.   uintptr_t uia, uib;

  2272.   uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa));
  2273.   uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb));

  2274.   if (uia < uib)
  2275.     return -1;
  2276.   if (uia > uib)
  2277.     return 1;

  2278.   uia = (uintptr_t) *sa;
  2279.   uib = (uintptr_t) *sb;

  2280.   if (uia < uib)
  2281.     return -1;
  2282.   if (uia > uib)
  2283.     return 1;

  2284.   return 0;
  2285. }

  2286. /* Like compare_symbols but for minimal symbols.  */

  2287. static int
  2288. compare_msymbols (const void *a, const void *b)
  2289. {
  2290.   const struct bound_minimal_symbol *sa = a;
  2291.   const struct bound_minimal_symbol *sb = b;
  2292.   uintptr_t uia, uib;

  2293.   uia = (uintptr_t) sa->objfile->pspace;
  2294.   uib = (uintptr_t) sa->objfile->pspace;

  2295.   if (uia < uib)
  2296.     return -1;
  2297.   if (uia > uib)
  2298.     return 1;

  2299.   uia = (uintptr_t) sa->minsym;
  2300.   uib = (uintptr_t) sb->minsym;

  2301.   if (uia < uib)
  2302.     return -1;
  2303.   if (uia > uib)
  2304.     return 1;

  2305.   return 0;
  2306. }

  2307. /* Look for all the matching instances of each symbol in NAMES.  Only
  2308.    instances from PSPACE are considered; other program spaces are
  2309.    handled by our caller.  If PSPACE is NULL, then all program spaces
  2310.    are considered.  Results are stored into INFO.  */

  2311. static void
  2312. add_all_symbol_names_from_pspace (struct collect_info *info,
  2313.                                   struct program_space *pspace,
  2314.                                   VEC (const_char_ptr) *names)
  2315. {
  2316.   int ix;
  2317.   const char *iter;

  2318.   for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
  2319.     add_matching_symbols_to_info (iter, info, pspace);
  2320. }

  2321. static void
  2322. find_superclass_methods (VEC (typep) *superclasses,
  2323.                          const char *name,
  2324.                          VEC (const_char_ptr) **result_names)
  2325. {
  2326.   int old_len = VEC_length (const_char_ptr, *result_names);
  2327.   VEC (typep) *iter_classes;
  2328.   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);

  2329.   iter_classes = superclasses;
  2330.   while (1)
  2331.     {
  2332.       VEC (typep) *new_supers = NULL;
  2333.       int ix;
  2334.       struct type *t;

  2335.       make_cleanup (VEC_cleanup (typep), &new_supers);
  2336.       for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
  2337.         find_methods (t, name, result_names, &new_supers);

  2338.       if (VEC_length (const_char_ptr, *result_names) != old_len
  2339.           || VEC_empty (typep, new_supers))
  2340.         break;

  2341.       iter_classes = new_supers;
  2342.     }

  2343.   do_cleanups (cleanup);
  2344. }

  2345. /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
  2346.    given by one of the symbols in SYM_CLASSES.  Matches are returned
  2347.    in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols).  */

  2348. static void
  2349. find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
  2350.              const char *class_name, const char *method_name,
  2351.              VEC (symbolp) *sym_classes, VEC (symbolp) **symbols,
  2352.              VEC (bound_minimal_symbol_d) **minsyms)
  2353. {
  2354.   struct symbol *sym;
  2355.   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
  2356.   int ix;
  2357.   int last_result_len;
  2358.   VEC (typep) *superclass_vec;
  2359.   VEC (const_char_ptr) *result_names;
  2360.   struct collect_info info;

  2361.   /* Sort symbols so that symbols with the same program space are next
  2362.      to each other.  */
  2363.   qsort (VEC_address (symbolp, sym_classes),
  2364.          VEC_length (symbolp, sym_classes),
  2365.          sizeof (symbolp),
  2366.          compare_symbols);

  2367.   info.state = self;
  2368.   info.file_symtabs = file_symtabs;
  2369.   info.result.symbols = NULL;
  2370.   info.result.minimal_symbols = NULL;

  2371.   /* Iterate over all the types, looking for the names of existing
  2372.      methods matching METHOD_NAME.  If we cannot find a direct method in a
  2373.      given program space, then we consider inherited methods; this is
  2374.      not ideal (ideal would be to respect C++ hiding rules), but it
  2375.      seems good enough and is what GDB has historically done.  We only
  2376.      need to collect the names because later we find all symbols with
  2377.      those names.  This loop is written in a somewhat funny way
  2378.      because we collect data across the program space before deciding
  2379.      what to do.  */
  2380.   superclass_vec = NULL;
  2381.   make_cleanup (VEC_cleanup (typep), &superclass_vec);
  2382.   result_names = NULL;
  2383.   make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
  2384.   last_result_len = 0;
  2385.   for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
  2386.     {
  2387.       struct type *t;
  2388.       struct program_space *pspace;

  2389.       /* Program spaces that are executing startup should have
  2390.          been filtered out earlier.  */
  2391.       pspace = SYMTAB_PSPACE (symbol_symtab (sym));
  2392.       gdb_assert (!pspace->executing_startup);
  2393.       set_current_program_space (pspace);
  2394.       t = check_typedef (SYMBOL_TYPE (sym));
  2395.       find_methods (t, method_name, &result_names, &superclass_vec);

  2396.       /* Handle all items from a single program space at once; and be
  2397.          sure not to miss the last batch.  */
  2398.       if (ix == VEC_length (symbolp, sym_classes) - 1
  2399.           || (pspace
  2400.               != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes,
  2401.                                                           ix + 1)))))
  2402.         {
  2403.           /* If we did not find a direct implementation anywhere in
  2404.              this program space, consider superclasses.  */
  2405.           if (VEC_length (const_char_ptr, result_names) == last_result_len)
  2406.             find_superclass_methods (superclass_vec, method_name,
  2407.                                      &result_names);

  2408.           /* We have a list of candidate symbol names, so now we
  2409.              iterate over the symbol tables looking for all
  2410.              matches in this pspace.  */
  2411.           add_all_symbol_names_from_pspace (&info, pspace, result_names);

  2412.           VEC_truncate (typep, superclass_vec, 0);
  2413.           last_result_len = VEC_length (const_char_ptr, result_names);
  2414.         }
  2415.     }

  2416.   if (!VEC_empty (symbolp, info.result.symbols)
  2417.       || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
  2418.     {
  2419.       *symbols = info.result.symbols;
  2420.       *minsyms = info.result.minimal_symbols;
  2421.       do_cleanups (cleanup);
  2422.       return;
  2423.     }

  2424.   /* Throw an NOT_FOUND_ERROR.  This will be caught by the caller
  2425.      and other attempts to locate the symbol will be made.  */
  2426.   throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
  2427. }



  2428. /* This object is used when collecting all matching symtabs.  */

  2429. struct symtab_collector
  2430. {
  2431.   /* The result vector of symtabs.  */
  2432.   VEC (symtab_ptr) *symtabs;

  2433.   /* This is used to ensure the symtabs are unique.  */
  2434.   htab_t symtab_table;
  2435. };

  2436. /* Callback for iterate_over_symtabs.  */

  2437. static int
  2438. add_symtabs_to_list (struct symtab *symtab, void *d)
  2439. {
  2440.   struct symtab_collector *data = d;
  2441.   void **slot;

  2442.   slot = htab_find_slot (data->symtab_table, symtab, INSERT);
  2443.   if (!*slot)
  2444.     {
  2445.       *slot = symtab;
  2446.       VEC_safe_push (symtab_ptr, data->symtabs, symtab);
  2447.     }

  2448.   return 0;
  2449. }

  2450. /* Given a file name, return a VEC of all matching symtabs.  */

  2451. static VEC (symtab_ptr) *
  2452. collect_symtabs_from_filename (const char *file)
  2453. {
  2454.   struct symtab_collector collector;
  2455.   struct cleanup *cleanups;
  2456.   struct program_space *pspace;

  2457.   collector.symtabs = NULL;
  2458.   collector.symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
  2459.                                         NULL);
  2460.   cleanups = make_cleanup_htab_delete (collector.symtab_table);

  2461.   /* Find that file's data.  */
  2462.   ALL_PSPACES (pspace)
  2463.   {
  2464.     if (pspace->executing_startup)
  2465.       continue;

  2466.     set_current_program_space (pspace);
  2467.     iterate_over_symtabs (file, add_symtabs_to_list, &collector);
  2468.   }

  2469.   do_cleanups (cleanups);
  2470.   return collector.symtabs;
  2471. }

  2472. /* Return all the symtabs associated to the FILENAME.  */

  2473. static VEC (symtab_ptr) *
  2474. symtabs_from_filename (const char *filename)
  2475. {
  2476.   VEC (symtab_ptr) *result;

  2477.   result = collect_symtabs_from_filename (filename);

  2478.   if (VEC_empty (symtab_ptr, result))
  2479.     {
  2480.       if (!have_full_symbols () && !have_partial_symbols ())
  2481.         throw_error (NOT_FOUND_ERROR,
  2482.                      _("No symbol table is loaded.  "
  2483.                        "Use the \"file\" command."));
  2484.       throw_error (NOT_FOUND_ERROR, _("No source file named %s."), filename);
  2485.     }

  2486.   return result;
  2487. }

  2488. /* Look up a function symbol named NAME in symtabs FILE_SYMTABS.  Matching
  2489.    debug symbols are returned in SYMBOLS.  Matching minimal symbols are
  2490.    returned in MINSYMS.  */

  2491. static void
  2492. find_function_symbols (struct linespec_state *state,
  2493.                        VEC (symtab_ptr) *file_symtabs, const char *name,
  2494.                        VEC (symbolp) **symbols,
  2495.                        VEC (bound_minimal_symbol_d) **minsyms)
  2496. {
  2497.   struct collect_info info;
  2498.   VEC (const_char_ptr) *symbol_names = NULL;
  2499.   struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
  2500.                                           &symbol_names);

  2501.   info.state = state;
  2502.   info.result.symbols = NULL;
  2503.   info.result.minimal_symbols = NULL;
  2504.   info.file_symtabs = file_symtabs;

  2505.   /* Try NAME as an Objective-C selector.  */
  2506.   find_imps (name, &symbol_names);
  2507.   if (!VEC_empty (const_char_ptr, symbol_names))
  2508.     add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
  2509.   else
  2510.     add_matching_symbols_to_info (name, &info, NULL);

  2511.   do_cleanups (cleanup);

  2512.   if (VEC_empty (symbolp, info.result.symbols))
  2513.     {
  2514.       VEC_free (symbolp, info.result.symbols);
  2515.       *symbols = NULL;
  2516.     }
  2517.   else
  2518.     *symbols = info.result.symbols;

  2519.   if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
  2520.     {
  2521.       VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
  2522.       *minsyms = NULL;
  2523.     }
  2524.   else
  2525.     *minsyms = info.result.minimal_symbols;
  2526. }

  2527. /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
  2528.    in SYMBOLS and minimal symbols in MINSYMS.  */

  2529. static void
  2530. find_linespec_symbols (struct linespec_state *state,
  2531.                        VEC (symtab_ptr) *file_symtabs,
  2532.                        const char *name,
  2533.                        VEC (symbolp) **symbols,
  2534.                        VEC (bound_minimal_symbol_d) **minsyms)
  2535. {
  2536.   struct cleanup *cleanup;
  2537.   char *canon;
  2538.   const char *lookup_name;
  2539.   volatile struct gdb_exception except;

  2540.   cleanup = demangle_for_lookup (name, state->language->la_language,
  2541.                                  &lookup_name);
  2542.   if (state->language->la_language == language_ada)
  2543.     {
  2544.       /* In Ada, the symbol lookups are performed using the encoded
  2545.          name rather than the demangled name.  */
  2546.       lookup_name = ada_name_for_lookup (name);
  2547.       make_cleanup (xfree, (void *) lookup_name);
  2548.     }

  2549.   canon = cp_canonicalize_string_no_typedefs (lookup_name);
  2550.   if (canon != NULL)
  2551.     {
  2552.       lookup_name = canon;
  2553.       make_cleanup (xfree, canon);
  2554.     }

  2555.   /* It's important to not call expand_symtabs_matching unnecessarily
  2556.      as it can really slow things down (by unnecessarily expanding
  2557.      potentially 1000s of symtabs, which when debugging some apps can
  2558.      cost 100s of seconds).  Avoid this to some extent by *first* calling
  2559.      find_function_symbols, and only if that doesn't find anything
  2560.      *then* call find_method.  This handles two important cases:
  2561.      1) break (anonymous namespace)::foo
  2562.      2) break class::method where method is in class (and not a baseclass)  */

  2563.   find_function_symbols (state, file_symtabs, lookup_name,
  2564.                          symbols, minsyms);

  2565.   /* If we were unable to locate a symbol of the same name, try dividing
  2566.      the name into class and method names and searching the class and its
  2567.      baseclasses.  */
  2568.   if (VEC_empty (symbolp, *symbols)
  2569.       && VEC_empty (bound_minimal_symbol_d, *minsyms))
  2570.     {
  2571.       char *klass, *method;
  2572.       const char *last, *p, *scope_op;
  2573.       VEC (symbolp) *classes;

  2574.       /* See if we can find a scope operator and break this symbol
  2575.          name into namespaces${SCOPE_OPERATOR}class_name and method_name.  */
  2576.       scope_op = "::";
  2577.       p = find_toplevel_string (lookup_name, scope_op);
  2578.       if (p == NULL)
  2579.         {
  2580.           /* No C++ scope operator.  Try Java.  */
  2581.           scope_op = ".";
  2582.           p = find_toplevel_string (lookup_name, scope_op);
  2583.         }

  2584.       last = NULL;
  2585.       while (p != NULL)
  2586.         {
  2587.           last = p;
  2588.           p = find_toplevel_string (p + strlen (scope_op), scope_op);
  2589.         }

  2590.       /* If no scope operator was found, there is nothing more we can do;
  2591.          we already attempted to lookup the entire name as a symbol
  2592.          and failed.  */
  2593.       if (last == NULL)
  2594.         {
  2595.           do_cleanups (cleanup);
  2596.           return;
  2597.         }

  2598.       /* LOOKUP_NAME points to the class name.
  2599.          LAST points to the method name.  */
  2600.       klass = xmalloc ((last - lookup_name + 1) * sizeof (char));
  2601.       make_cleanup (xfree, klass);
  2602.       strncpy (klass, lookup_name, last - lookup_name);
  2603.       klass[last - lookup_name] = '\0';

  2604.       /* Skip past the scope operator.  */
  2605.       last += strlen (scope_op);
  2606.       method = xmalloc ((strlen (last) + 1) * sizeof (char));
  2607.       make_cleanup (xfree, method);
  2608.       strcpy (method, last);

  2609.       /* Find a list of classes named KLASS.  */
  2610.       classes = lookup_prefix_sym (state, file_symtabs, klass);
  2611.       make_cleanup (VEC_cleanup (symbolp), &classes);

  2612.       if (!VEC_empty (symbolp, classes))
  2613.         {
  2614.           /* Now locate a list of suitable methods named METHOD.  */
  2615.           TRY_CATCH (except, RETURN_MASK_ERROR)
  2616.             {
  2617.               find_method (state, file_symtabs, klass, method, classes,
  2618.                            symbols, minsyms);
  2619.             }

  2620.           /* If successful, we're done.  If NOT_FOUND_ERROR
  2621.              was not thrown, rethrow the exception that we did get.  */
  2622.           if (except.reason < 0 && except.error != NOT_FOUND_ERROR)
  2623.             throw_exception (except);
  2624.         }
  2625.     }

  2626.   do_cleanups (cleanup);
  2627. }

  2628. /* Return all labels named NAME in FUNCTION_SYMBOLS.  Return the
  2629.    actual function symbol in which the label was found in LABEL_FUNC_RET.  */

  2630. static VEC (symbolp) *
  2631. find_label_symbols (struct linespec_state *self,
  2632.                     VEC (symbolp) *function_symbols,
  2633.                     VEC (symbolp) **label_funcs_ret, const char *name)
  2634. {
  2635.   int ix;
  2636.   const struct block *block;
  2637.   struct symbol *sym;
  2638.   struct symbol *fn_sym;
  2639.   VEC (symbolp) *result = NULL;

  2640.   if (function_symbols == NULL)
  2641.     {
  2642.       set_current_program_space (self->program_space);
  2643.       block = get_current_search_block ();

  2644.       for (;
  2645.            block && !BLOCK_FUNCTION (block);
  2646.            block = BLOCK_SUPERBLOCK (block))
  2647.         ;
  2648.       if (!block)
  2649.         return NULL;
  2650.       fn_sym = BLOCK_FUNCTION (block);

  2651.       sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);

  2652.       if (sym != NULL)
  2653.         {
  2654.           VEC_safe_push (symbolp, result, sym);
  2655.           VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
  2656.         }
  2657.     }
  2658.   else
  2659.     {
  2660.       for (ix = 0;
  2661.            VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
  2662.         {
  2663.           set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
  2664.           block = SYMBOL_BLOCK_VALUE (fn_sym);
  2665.           sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);

  2666.           if (sym != NULL)
  2667.             {
  2668.               VEC_safe_push (symbolp, result, sym);
  2669.               VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
  2670.             }
  2671.         }
  2672.     }

  2673.   return result;
  2674. }



  2675. /* A helper for create_sals_line_offset that handles the 'list_mode' case.  */

  2676. static void
  2677. decode_digits_list_mode (struct linespec_state *self,
  2678.                          linespec_p ls,
  2679.                          struct symtabs_and_lines *values,
  2680.                          struct symtab_and_line val)
  2681. {
  2682.   int ix;
  2683.   struct symtab *elt;

  2684.   gdb_assert (self->list_mode);

  2685.   for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
  2686.        ++ix)
  2687.     {
  2688.       /* The logic above should ensure this.  */
  2689.       gdb_assert (elt != NULL);

  2690.       set_current_program_space (SYMTAB_PSPACE (elt));

  2691.       /* Simplistic search just for the list command.  */
  2692.       val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
  2693.       if (val.symtab == NULL)
  2694.         val.symtab = elt;
  2695.       val.pspace = SYMTAB_PSPACE (elt);
  2696.       val.pc = 0;
  2697.       val.explicit_line = 1;

  2698.       add_sal_to_sals (self, values, &val, NULL, 0);
  2699.     }
  2700. }

  2701. /* A helper for create_sals_line_offset that iterates over the symtabs,
  2702.    adding lines to the VEC.  */

  2703. static void
  2704. decode_digits_ordinary (struct linespec_state *self,
  2705.                         linespec_p ls,
  2706.                         int line,
  2707.                         struct symtabs_and_lines *sals,
  2708.                         struct linetable_entry **best_entry)
  2709. {
  2710.   int ix;
  2711.   struct symtab *elt;

  2712.   for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
  2713.     {
  2714.       int i;
  2715.       VEC (CORE_ADDR) *pcs;
  2716.       CORE_ADDR pc;

  2717.       /* The logic above should ensure this.  */
  2718.       gdb_assert (elt != NULL);

  2719.       set_current_program_space (SYMTAB_PSPACE (elt));

  2720.       pcs = find_pcs_for_symtab_line (elt, line, best_entry);
  2721.       for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
  2722.         {
  2723.           struct symtab_and_line sal;

  2724.           init_sal (&sal);
  2725.           sal.pspace = SYMTAB_PSPACE (elt);
  2726.           sal.symtab = elt;
  2727.           sal.line = line;
  2728.           sal.pc = pc;
  2729.           add_sal_to_sals_basic (sals, &sal);
  2730.         }

  2731.       VEC_free (CORE_ADDR, pcs);
  2732.     }
  2733. }



  2734. /* Return the line offset represented by VARIABLE.  */

  2735. static struct line_offset
  2736. linespec_parse_variable (struct linespec_state *self, const char *variable)
  2737. {
  2738.   int index = 0;
  2739.   const char *p;
  2740.   struct line_offset offset = {0, LINE_OFFSET_NONE};

  2741.   p = (variable[1] == '$') ? variable + 2 : variable + 1;
  2742.   if (*p == '$')
  2743.     ++p;
  2744.   while (*p >= '0' && *p <= '9')
  2745.     ++p;
  2746.   if (!*p)                /* Reached end of token without hitting non-digit.  */
  2747.     {
  2748.       /* We have a value history reference.  */
  2749.       struct value *val_history;

  2750.       sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
  2751.       val_history
  2752.         = access_value_history ((variable[1] == '$') ? -index : index);
  2753.       if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
  2754.         error (_("History values used in line "
  2755.                  "specs must have integer values."));
  2756.       offset.offset = value_as_long (val_history);
  2757.     }
  2758.   else
  2759.     {
  2760.       /* Not all digits -- may be user variable/function or a
  2761.          convenience variable.  */
  2762.       LONGEST valx;
  2763.       struct internalvar *ivar;

  2764.       /* Try it as a convenience variable.  If it is not a convenience
  2765.          variable, return and allow normal symbol lookup to occur.  */
  2766.       ivar = lookup_only_internalvar (variable + 1);
  2767.       if (ivar == NULL)
  2768.         /* No internal variable with that name.  Mark the offset
  2769.            as unknown to allow the name to be looked up as a symbol.  */
  2770.         offset.sign = LINE_OFFSET_UNKNOWN;
  2771.       else
  2772.         {
  2773.           /* We found a valid variable name.  If it is not an integer,
  2774.              throw an error.  */
  2775.           if (!get_internalvar_integer (ivar, &valx))
  2776.             error (_("Convenience variables used in line "
  2777.                      "specs must have integer values."));
  2778.           else
  2779.             offset.offset = valx;
  2780.         }
  2781.     }

  2782.   return offset;
  2783. }


  2784. /* A callback used to possibly add a symbol to the results.  */

  2785. static int
  2786. collect_symbols (struct symbol *sym, void *data)
  2787. {
  2788.   struct collect_info *info = data;

  2789.   /* In list mode, add all matching symbols, regardless of class.
  2790.      This allows the user to type "list a_global_variable".  */
  2791.   if (SYMBOL_CLASS (sym) == LOC_BLOCK || info->state->list_mode)
  2792.     VEC_safe_push (symbolp, info->result.symbols, sym);
  2793.   return 1; /* Continue iterating.  */
  2794. }

  2795. /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
  2796.    linespec; return the SAL in RESULT.  */

  2797. static void
  2798. minsym_found (struct linespec_state *self, struct objfile *objfile,
  2799.               struct minimal_symbol *msymbol,
  2800.               struct symtabs_and_lines *result)
  2801. {
  2802.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  2803.   CORE_ADDR pc;
  2804.   struct symtab_and_line sal;

  2805.   sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
  2806.                            (struct obj_section *) 0, 0);
  2807.   sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);

  2808.   /* The minimal symbol might point to a function descriptor;
  2809.      resolve it to the actual code address instead.  */
  2810.   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
  2811.   if (pc != sal.pc)
  2812.     sal = find_pc_sect_line (pc, NULL, 0);

  2813.   if (self->funfirstline)
  2814.     skip_prologue_sal (&sal);

  2815.   if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
  2816.     add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0);
  2817. }

  2818. /* A helper struct to pass some data through
  2819.    iterate_over_minimal_symbols.  */

  2820. struct collect_minsyms
  2821. {
  2822.   /* The objfile we're examining.  */
  2823.   struct objfile *objfile;

  2824.   /* Only search the given symtab, or NULL to search for all symbols.  */
  2825.   struct symtab *symtab;

  2826.   /* The funfirstline setting from the initial call.  */
  2827.   int funfirstline;

  2828.   /* The list_mode setting from the initial call.  */
  2829.   int list_mode;

  2830.   /* The resulting symbols.  */
  2831.   VEC (bound_minimal_symbol_d) *msyms;
  2832. };

  2833. /* A helper function to classify a minimal_symbol_type according to
  2834.    priority.  */

  2835. static int
  2836. classify_mtype (enum minimal_symbol_type t)
  2837. {
  2838.   switch (t)
  2839.     {
  2840.     case mst_file_text:
  2841.     case mst_file_data:
  2842.     case mst_file_bss:
  2843.       /* Intermediate priority.  */
  2844.       return 1;

  2845.     case mst_solib_trampoline:
  2846.       /* Lowest priority.  */
  2847.       return 2;

  2848.     default:
  2849.       /* Highest priority.  */
  2850.       return 0;
  2851.     }
  2852. }

  2853. /* Callback for qsort that sorts symbols by priority.  */

  2854. static int
  2855. compare_msyms (const void *a, const void *b)
  2856. {
  2857.   const bound_minimal_symbol_d *moa = a;
  2858.   const bound_minimal_symbol_d *mob = b;
  2859.   enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
  2860.   enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);

  2861.   return classify_mtype (ta) - classify_mtype (tb);
  2862. }

  2863. /* Callback for iterate_over_minimal_symbols that adds the symbol to
  2864.    the result.  */

  2865. static void
  2866. add_minsym (struct minimal_symbol *minsym, void *d)
  2867. {
  2868.   struct collect_minsyms *info = d;
  2869.   bound_minimal_symbol_d mo;

  2870.   mo.minsym = minsym;
  2871.   mo.objfile = info->objfile;

  2872.   if (info->symtab != NULL)
  2873.     {
  2874.       CORE_ADDR pc;
  2875.       struct symtab_and_line sal;
  2876.       struct gdbarch *gdbarch = get_objfile_arch (info->objfile);

  2877.       sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym),
  2878.                                NULL, 0);
  2879.       sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym);
  2880.       pc
  2881.         = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
  2882.       if (pc != sal.pc)
  2883.         sal = find_pc_sect_line (pc, NULL, 0);

  2884.       if (info->symtab != sal.symtab)
  2885.         return;
  2886.     }

  2887.   /* Exclude data symbols when looking for breakpoint locations.   */
  2888.   if (!info->list_mode)
  2889.     switch (minsym->type)
  2890.       {
  2891.         case mst_slot_got_plt:
  2892.         case mst_data:
  2893.         case mst_bss:
  2894.         case mst_abs:
  2895.         case mst_file_data:
  2896.         case mst_file_bss:
  2897.           {
  2898.             /* Make sure this minsym is not a function descriptor
  2899.                before we decide to discard it.  */
  2900.             struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
  2901.             CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
  2902.                                (gdbarch, BMSYMBOL_VALUE_ADDRESS (mo),
  2903.                                 &current_target);

  2904.             if (addr == BMSYMBOL_VALUE_ADDRESS (mo))
  2905.               return;
  2906.           }
  2907.       }

  2908.   VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
  2909. }

  2910. /* Search for minimal symbols called NAME.  If SEARCH_PSPACE
  2911.    is not NULL, the search is restricted to just that program
  2912.    space.

  2913.    If SYMTAB is NULL, search all objfiles, otherwise
  2914.    restrict results to the given SYMTAB.  */

  2915. static void
  2916. search_minsyms_for_name (struct collect_info *info, const char *name,
  2917.                          struct program_space *search_pspace,
  2918.                          struct symtab *symtab)
  2919. {
  2920.   struct collect_minsyms local;
  2921.   struct cleanup *cleanup;

  2922.   memset (&local, 0, sizeof (local));
  2923.   local.funfirstline = info->state->funfirstline;
  2924.   local.list_mode = info->state->list_mode;
  2925.   local.symtab = symtab;

  2926.   cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);

  2927.   if (symtab == NULL)
  2928.     {
  2929.       struct program_space *pspace;

  2930.       ALL_PSPACES (pspace)
  2931.       {
  2932.         struct objfile *objfile;

  2933.         if (search_pspace != NULL && search_pspace != pspace)
  2934.           continue;
  2935.         if (pspace->executing_startup)
  2936.           continue;

  2937.         set_current_program_space (pspace);

  2938.         ALL_OBJFILES (objfile)
  2939.         {
  2940.           local.objfile = objfile;
  2941.           iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
  2942.         }
  2943.       }
  2944.     }
  2945.   else
  2946.     {
  2947.       if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
  2948.         {
  2949.           set_current_program_space (SYMTAB_PSPACE (symtab));
  2950.           local.objfile = SYMTAB_OBJFILE(symtab);
  2951.           iterate_over_minimal_symbols (local.objfile, name, add_minsym,
  2952.                                         &local);
  2953.         }
  2954.     }

  2955.     if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
  2956.       {
  2957.         int classification;
  2958.         int ix;
  2959.         bound_minimal_symbol_d *item;

  2960.         qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
  2961.                VEC_length (bound_minimal_symbol_d, local.msyms),
  2962.                sizeof (bound_minimal_symbol_d),
  2963.                compare_msyms);

  2964.         /* Now the minsyms are in classification order.  So, we walk
  2965.            over them and process just the minsyms with the same
  2966.            classification as the very first minsym in the list.  */
  2967.         item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
  2968.         classification = classify_mtype (MSYMBOL_TYPE (item->minsym));

  2969.         for (ix = 0;
  2970.              VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
  2971.              ++ix)
  2972.           {
  2973.             if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
  2974.               break;

  2975.             VEC_safe_push (bound_minimal_symbol_d,
  2976.                            info->result.minimal_symbols, item);
  2977.           }
  2978.       }

  2979.     do_cleanups (cleanup);
  2980. }

  2981. /* A helper function to add all symbols matching NAME to INFO.  If
  2982.    PSPACE is not NULL, the search is restricted to just that program
  2983.    space.  */

  2984. static void
  2985. add_matching_symbols_to_info (const char *name,
  2986.                               struct collect_info *info,
  2987.                               struct program_space *pspace)
  2988. {
  2989.   int ix;
  2990.   struct symtab *elt;

  2991.   for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
  2992.     {
  2993.       if (elt == NULL)
  2994.         {
  2995.           iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN,
  2996.                                              collect_symbols, info,
  2997.                                              pspace, 1);
  2998.           search_minsyms_for_name (info, name, pspace, NULL);
  2999.         }
  3000.       else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
  3001.         {
  3002.           int prev_len = VEC_length (symbolp, info->result.symbols);

  3003.           /* Program spaces that are executing startup should have
  3004.              been filtered out earlier.  */
  3005.           gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
  3006.           set_current_program_space (SYMTAB_PSPACE (elt));
  3007.           iterate_over_file_blocks (elt, name, VAR_DOMAIN,
  3008.                                     collect_symbols, info);

  3009.           /* If no new symbols were found in this iteration and this symtab
  3010.              is in assembler, we might actually be looking for a label for
  3011.              which we don't have debug info.  Check for a minimal symbol in
  3012.              this case.  */
  3013.           if (prev_len == VEC_length (symbolp, info->result.symbols)
  3014.               && elt->language == language_asm)
  3015.             search_minsyms_for_name (info, name, pspace, elt);
  3016.         }
  3017.     }
  3018. }



  3019. /* Now come some functions that are called from multiple places within
  3020.    decode_line_1.  */

  3021. static int
  3022. symbol_to_sal (struct symtab_and_line *result,
  3023.                int funfirstline, struct symbol *sym)
  3024. {
  3025.   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
  3026.     {
  3027.       *result = find_function_start_sal (sym, funfirstline);
  3028.       return 1;
  3029.     }
  3030.   else
  3031.     {
  3032.       if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
  3033.         {
  3034.           init_sal (result);
  3035.           result->symtab = symbol_symtab (sym);
  3036.           result->line = SYMBOL_LINE (sym);
  3037.           result->pc = SYMBOL_VALUE_ADDRESS (sym);
  3038.           result->pspace = SYMTAB_PSPACE (result->symtab);
  3039.           result->explicit_pc = 1;
  3040.           return 1;
  3041.         }
  3042.       else if (funfirstline)
  3043.         {
  3044.           /* Nothing.  */
  3045.         }
  3046.       else if (SYMBOL_LINE (sym) != 0)
  3047.         {
  3048.           /* We know its line number.  */
  3049.           init_sal (result);
  3050.           result->symtab = symbol_symtab (sym);
  3051.           result->line = SYMBOL_LINE (sym);
  3052.           result->pspace = SYMTAB_PSPACE (result->symtab);
  3053.           return 1;
  3054.         }
  3055.     }

  3056.   return 0;
  3057. }

  3058. /* See the comment in linespec.h.  */

  3059. void
  3060. init_linespec_result (struct linespec_result *lr)
  3061. {
  3062.   memset (lr, 0, sizeof (*lr));
  3063. }

  3064. /* See the comment in linespec.h.  */

  3065. void
  3066. destroy_linespec_result (struct linespec_result *ls)
  3067. {
  3068.   int i;
  3069.   struct linespec_sals *lsal;

  3070.   xfree (ls->addr_string);
  3071.   for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
  3072.     {
  3073.       xfree (lsal->canonical);
  3074.       xfree (lsal->sals.sals);
  3075.     }
  3076.   VEC_free (linespec_sals, ls->sals);
  3077. }

  3078. /* Cleanup function for a linespec_result.  */

  3079. static void
  3080. cleanup_linespec_result (void *a)
  3081. {
  3082.   destroy_linespec_result (a);
  3083. }

  3084. /* See the comment in linespec.h.  */

  3085. struct cleanup *
  3086. make_cleanup_destroy_linespec_result (struct linespec_result *ls)
  3087. {
  3088.   return make_cleanup (cleanup_linespec_result, ls);
  3089. }