GDB (API)
/home/stan/gdb/src/gdb/linespec.c
Go to the documentation of this file.
00001 /* Parser for linespec for the GNU debugger, GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "frame.h"
00023 #include "command.h"
00024 #include "symfile.h"
00025 #include "objfiles.h"
00026 #include "source.h"
00027 #include "demangle.h"
00028 #include "value.h"
00029 #include "completer.h"
00030 #include "cp-abi.h"
00031 #include "cp-support.h"
00032 #include "parser-defs.h"
00033 #include "block.h"
00034 #include "objc-lang.h"
00035 #include "linespec.h"
00036 #include "exceptions.h"
00037 #include "language.h"
00038 #include "interps.h"
00039 #include "mi/mi-cmds.h"
00040 #include "target.h"
00041 #include "arch-utils.h"
00042 #include <ctype.h>
00043 #include "cli/cli-utils.h"
00044 #include "filenames.h"
00045 #include "ada-lang.h"
00046 #include "stack.h"
00047 
00048 typedef struct symbol *symbolp;
00049 DEF_VEC_P (symbolp);
00050 
00051 typedef struct type *typep;
00052 DEF_VEC_P (typep);
00053 
00054 /* An address entry is used to ensure that any given location is only
00055    added to the result a single time.  It holds an address and the
00056    program space from which the address came.  */
00057 
00058 struct address_entry
00059 {
00060   struct program_space *pspace;
00061   CORE_ADDR addr;
00062 };
00063 
00064 typedef struct bound_minimal_symbol bound_minimal_symbol_d;
00065 
00066 DEF_VEC_O (bound_minimal_symbol_d);
00067 
00068 /* An enumeration of possible signs for a line offset.  */
00069 enum offset_relative_sign
00070 {
00071   /* No sign  */
00072   LINE_OFFSET_NONE,
00073 
00074   /* A plus sign ("+")  */
00075   LINE_OFFSET_PLUS,
00076 
00077   /* A minus sign ("-")  */
00078   LINE_OFFSET_MINUS,
00079 
00080   /* A special "sign" for unspecified offset.  */
00081   LINE_OFFSET_UNKNOWN
00082 };
00083 
00084 /* A line offset in a linespec.  */
00085 
00086 struct line_offset
00087 {
00088   /* Line offset and any specified sign.  */
00089   int offset;
00090   enum offset_relative_sign sign;
00091 };
00092 
00093 /* A linespec.  Elements of this structure are filled in by a parser
00094    (either parse_linespec or some other function).  The structure is
00095    then converted into SALs by convert_linespec_to_sals.  */
00096 
00097 struct linespec
00098 {
00099   /* An expression and the resulting PC.  Specifying an expression
00100      currently precludes the use of other members.  */
00101 
00102   /* The expression entered by the user.  */
00103   const char *expression;
00104 
00105   /* The resulting PC expression derived from evaluating EXPRESSION.  */
00106   CORE_ADDR expr_pc;
00107 
00108   /* Any specified file symtabs.  */
00109 
00110   /* The user-supplied source filename or NULL if none was specified.  */
00111   const char *source_filename;
00112 
00113   /* The list of symtabs to search to which to limit the search.  May not
00114      be NULL.  If SOURCE_FILENAME is NULL (no user-specified filename),
00115      FILE_SYMTABS should contain one single NULL member.  This will
00116      cause the code to use the default symtab.  */
00117   VEC (symtab_ptr) *file_symtabs;
00118 
00119   /* The name of a function or method and any matching symbols.  */
00120 
00121   /* The user-specified function name.  If no function name was
00122      supplied, this may be NULL.  */
00123   const char *function_name;
00124 
00125   /* A list of matching function symbols and minimal symbols.  Both lists
00126      may be NULL if no matching symbols were found.  */
00127   VEC (symbolp) *function_symbols;
00128   VEC (bound_minimal_symbol_d) *minimal_symbols;
00129 
00130   /* The name of a label and matching symbols.  */
00131 
00132   /* The user-specified label name.  */
00133   const char *label_name;
00134 
00135   /* A structure of matching label symbols and the corresponding
00136      function symbol in which the label was found.  Both may be NULL
00137      or both must be non-NULL.  */
00138   struct
00139   {
00140     VEC (symbolp) *label_symbols;
00141     VEC (symbolp) *function_symbols;
00142   } labels;
00143 
00144   /* Line offset.  It may be LINE_OFFSET_UNKNOWN, meaning that no
00145    offset was specified.  */
00146   struct line_offset line_offset;
00147 };
00148 typedef struct linespec *linespec_p;
00149 
00150 /* A canonical linespec represented as a symtab-related string.
00151 
00152    Each entry represents the "SYMTAB:SUFFIX" linespec string.
00153    SYMTAB can be converted for example by symtab_to_fullname or
00154    symtab_to_filename_for_display as needed.  */
00155 
00156 struct linespec_canonical_name
00157 {
00158   /* Remaining text part of the linespec string.  */
00159   char *suffix;
00160 
00161   /* If NULL then SUFFIX is the whole linespec string.  */
00162   struct symtab *symtab;
00163 };
00164 
00165 /* An instance of this is used to keep all state while linespec
00166    operates.  This instance is passed around as a 'this' pointer to
00167    the various implementation methods.  */
00168 
00169 struct linespec_state
00170 {
00171   /* The language in use during linespec processing.  */
00172   const struct language_defn *language;
00173 
00174   /* The program space as seen when the module was entered.  */
00175   struct program_space *program_space;
00176 
00177   /* The default symtab to use, if no other symtab is specified.  */
00178   struct symtab *default_symtab;
00179 
00180   /* The default line to use.  */
00181   int default_line;
00182 
00183   /* The 'funfirstline' value that was passed in to decode_line_1 or
00184      decode_line_full.  */
00185   int funfirstline;
00186 
00187   /* Nonzero if we are running in 'list' mode; see decode_line_list.  */
00188   int list_mode;
00189 
00190   /* The 'canonical' value passed to decode_line_full, or NULL.  */
00191   struct linespec_result *canonical;
00192 
00193   /* Canonical strings that mirror the symtabs_and_lines result.  */
00194   struct linespec_canonical_name *canonical_names;
00195 
00196   /* This is a set of address_entry objects which is used to prevent
00197      duplicate symbols from being entered into the result.  */
00198   htab_t addr_set;
00199 };
00200 
00201 /* This is a helper object that is used when collecting symbols into a
00202    result.  */
00203 
00204 struct collect_info
00205 {
00206   /* The linespec object in use.  */
00207   struct linespec_state *state;
00208 
00209   /* A list of symtabs to which to restrict matches.  */
00210   VEC (symtab_ptr) *file_symtabs;
00211 
00212   /* The result being accumulated.  */
00213   struct
00214   {
00215     VEC (symbolp) *symbols;
00216     VEC (bound_minimal_symbol_d) *minimal_symbols;
00217   } result;
00218 };
00219 
00220 /* Token types  */
00221 
00222 enum ls_token_type
00223 {
00224   /* A keyword  */
00225   LSTOKEN_KEYWORD = 0,
00226 
00227   /* A colon "separator"  */
00228   LSTOKEN_COLON,
00229 
00230   /* A string  */
00231   LSTOKEN_STRING,
00232 
00233   /* A number  */
00234   LSTOKEN_NUMBER,
00235 
00236   /* A comma  */
00237   LSTOKEN_COMMA,
00238 
00239   /* EOI (end of input)  */
00240   LSTOKEN_EOI,
00241 
00242   /* Consumed token  */
00243   LSTOKEN_CONSUMED
00244 };
00245 typedef enum ls_token_type linespec_token_type;
00246 
00247 /* List of keywords  */
00248 
00249 static const char * const linespec_keywords[] = { "if", "thread", "task" };
00250 
00251 /* A token of the linespec lexer  */
00252 
00253 struct ls_token
00254 {
00255   /* The type of the token  */
00256   linespec_token_type type;
00257 
00258   /* Data for the token  */
00259   union
00260   {
00261     /* A string, given as a stoken  */
00262     struct stoken string;
00263 
00264     /* A keyword  */
00265     const char *keyword;
00266   } data;
00267 };
00268 typedef struct ls_token linespec_token;
00269 
00270 #define LS_TOKEN_STOKEN(TOK) (TOK).data.string
00271 #define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
00272 
00273 /* An instance of the linespec parser.  */
00274 
00275 struct ls_parser
00276 {
00277   /* Lexer internal data  */
00278   struct
00279   {
00280     /* Save head of input stream.  */
00281     const char *saved_arg;
00282 
00283     /* Head of the input stream.  */
00284     const char **stream;
00285 #define PARSER_STREAM(P) (*(P)->lexer.stream)
00286 
00287     /* The current token.  */
00288     linespec_token current;
00289   } lexer;
00290 
00291   /* Is the entire linespec quote-enclosed?  */
00292   int is_quote_enclosed;
00293 
00294   /* Is a keyword syntactically valid at this point?
00295      In, e.g., "break thread thread 1", the leading "keyword" must not
00296      be interpreted as such.  */
00297   int keyword_ok;
00298 
00299   /* The state of the parse.  */
00300   struct linespec_state state;
00301 #define PARSER_STATE(PPTR) (&(PPTR)->state)
00302 
00303   /* The result of the parse.  */
00304   struct linespec result;
00305 #define PARSER_RESULT(PPTR) (&(PPTR)->result)
00306 };
00307 typedef struct ls_parser linespec_parser;
00308 
00309 /* Prototypes for local functions.  */
00310 
00311 static void iterate_over_file_blocks (struct symtab *symtab,
00312                                       const char *name, domain_enum domain,
00313                                       symbol_found_callback_ftype *callback,
00314                                       void *data);
00315 
00316 static void initialize_defaults (struct symtab **default_symtab,
00317                                  int *default_line);
00318 
00319 static CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
00320 
00321 static struct symtabs_and_lines decode_objc (struct linespec_state *self,
00322                                              linespec_p ls,
00323                                              const char **argptr);
00324 
00325 static VEC (symtab_ptr) *symtabs_from_filename (const char *);
00326 
00327 static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
00328                                           VEC (symbolp) *function_symbols,
00329                                           VEC (symbolp) **label_funcs_ret,
00330                                           const char *name);
00331 
00332 static void find_linespec_symbols (struct linespec_state *self,
00333                                    VEC (symtab_ptr) *file_symtabs,
00334                                    const char *name,
00335                                    VEC (symbolp) **symbols,
00336                                    VEC (bound_minimal_symbol_d) **minsyms);
00337 
00338 static struct line_offset
00339      linespec_parse_variable (struct linespec_state *self,
00340                               const char *variable);
00341 
00342 static int symbol_to_sal (struct symtab_and_line *result,
00343                           int funfirstline, struct symbol *sym);
00344 
00345 static void add_matching_symbols_to_info (const char *name,
00346                                           struct collect_info *info,
00347                                           struct program_space *pspace);
00348 
00349 static void add_all_symbol_names_from_pspace (struct collect_info *info,
00350                                               struct program_space *pspace,
00351                                               VEC (const_char_ptr) *names);
00352 
00353 static VEC (symtab_ptr) *collect_symtabs_from_filename (const char *file);
00354 
00355 static void decode_digits_ordinary (struct linespec_state *self,
00356                                     linespec_p ls,
00357                                     int line,
00358                                     struct symtabs_and_lines *sals,
00359                                     struct linetable_entry **best_entry);
00360 
00361 static void decode_digits_list_mode (struct linespec_state *self,
00362                                      linespec_p ls,
00363                                      struct symtabs_and_lines *values,
00364                                      struct symtab_and_line val);
00365 
00366 static void minsym_found (struct linespec_state *self, struct objfile *objfile,
00367                           struct minimal_symbol *msymbol,
00368                           struct symtabs_and_lines *result);
00369 
00370 static int compare_symbols (const void *a, const void *b);
00371 
00372 static int compare_msymbols (const void *a, const void *b);
00373 
00374 static const char *find_toplevel_char (const char *s, char c);
00375 
00376 /* Permitted quote characters for the parser.  This is different from the
00377    completer's quote characters to allow backward compatibility with the
00378    previous parser.  */
00379 static const char *const linespec_quote_characters = "\"\'";
00380 
00381 /* Lexer functions.  */
00382 
00383 /* Lex a number from the input in PARSER.  This only supports
00384    decimal numbers.
00385 
00386    Return true if input is decimal numbers.  Return false if not.  */
00387 
00388 static int
00389 linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
00390 {
00391   tokenp->type = LSTOKEN_NUMBER;
00392   LS_TOKEN_STOKEN (*tokenp).length = 0;
00393   LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
00394 
00395   /* Keep any sign at the start of the stream.  */
00396   if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
00397     {
00398       ++LS_TOKEN_STOKEN (*tokenp).length;
00399       ++(PARSER_STREAM (parser));
00400     }
00401 
00402   while (isdigit (*PARSER_STREAM (parser)))
00403     {
00404       ++LS_TOKEN_STOKEN (*tokenp).length;
00405       ++(PARSER_STREAM (parser));
00406     }
00407 
00408   /* If the next character in the input buffer is not a space, comma,
00409      quote, or colon, this input does not represent a number.  */
00410   if (*PARSER_STREAM (parser) != '\0'
00411       && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
00412       && *PARSER_STREAM (parser) != ':'
00413       && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
00414     {
00415       PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
00416       return 0;
00417     }
00418 
00419   return 1;
00420 }
00421 
00422 /* Does P represent one of the keywords?  If so, return
00423    the keyword.  If not, return NULL.  */
00424 
00425 static const char *
00426 linespec_lexer_lex_keyword (const char *p)
00427 {
00428   int i;
00429 
00430   if (p != NULL)
00431     {
00432       for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i)
00433         {
00434           int len = strlen (linespec_keywords[i]);
00435 
00436           /* If P begins with one of the keywords and the next
00437              character is not a valid identifier character,
00438              we have found a keyword.  */
00439           if (strncmp (p, linespec_keywords[i], len) == 0
00440               && !(isalnum (p[len]) || p[len] == '_'))
00441             return linespec_keywords[i];
00442         }
00443     }
00444 
00445   return NULL;
00446 }
00447 
00448 /* Does STRING represent an Ada operator?  If so, return the length
00449    of the decoded operator name.  If not, return 0.  */
00450 
00451 static int
00452 is_ada_operator (const char *string)
00453 {
00454   const struct ada_opname_map *mapping;
00455 
00456   for (mapping = ada_opname_table;
00457        mapping->encoded != NULL
00458          && strncmp (mapping->decoded, string,
00459                      strlen (mapping->decoded)) != 0; ++mapping)
00460     ;
00461 
00462   return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
00463 }
00464 
00465 /* Find QUOTE_CHAR in STRING, accounting for the ':' terminal.  Return
00466    the location of QUOTE_CHAR, or NULL if not found.  */
00467 
00468 static const char *
00469 skip_quote_char (const char *string, char quote_char)
00470 {
00471   const char *p, *last;
00472 
00473   p = last = find_toplevel_char (string, quote_char);
00474   while (p && *p != '\0' && *p != ':')
00475     {
00476       p = find_toplevel_char (p, quote_char);
00477       if (p != NULL)
00478         last = p++;
00479     }
00480 
00481   return last;
00482 }
00483 
00484 /* Make a writable copy of the string given in TOKEN, trimming
00485    any trailing whitespace.  */
00486 
00487 static char *
00488 copy_token_string (linespec_token token)
00489 {
00490   char *str, *s;
00491 
00492   if (token.type == LSTOKEN_KEYWORD)
00493     return xstrdup (LS_TOKEN_KEYWORD (token));
00494 
00495   str = savestring (LS_TOKEN_STOKEN (token).ptr,
00496                     LS_TOKEN_STOKEN (token).length);
00497   s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
00498   *s = '\0';
00499 
00500   return str;
00501 }
00502 
00503 /* Does P represent the end of a quote-enclosed linespec?  */
00504 
00505 static int
00506 is_closing_quote_enclosed (const char *p)
00507 {
00508   if (strchr (linespec_quote_characters, *p))
00509     ++p;
00510   p = skip_spaces ((char *) p);
00511   return (*p == '\0' || linespec_lexer_lex_keyword (p));
00512 }
00513 
00514 /* Find the end of the parameter list that starts with *INPUT.
00515    This helper function assists with lexing string segments
00516    which might contain valid (non-terminating) commas.  */
00517 
00518 static const char *
00519 find_parameter_list_end (const char *input)
00520 {
00521   char end_char, start_char;
00522   int depth;
00523   const char *p;
00524 
00525   start_char = *input;
00526   if (start_char == '(')
00527     end_char = ')';
00528   else if (start_char == '<')
00529     end_char = '>';
00530   else
00531     return NULL;
00532 
00533   p = input;
00534   depth = 0;
00535   while (*p)
00536     {
00537       if (*p == start_char)
00538         ++depth;
00539       else if (*p == end_char)
00540         {
00541           if (--depth == 0)
00542             {
00543               ++p;
00544               break;
00545             }
00546         }
00547       ++p;
00548     }
00549 
00550   return p;
00551 }
00552 
00553 
00554 /* Lex a string from the input in PARSER.  */
00555 
00556 static linespec_token
00557 linespec_lexer_lex_string (linespec_parser *parser)
00558 {
00559   linespec_token token;
00560   const char *start = PARSER_STREAM (parser);
00561 
00562   token.type = LSTOKEN_STRING;
00563 
00564   /* If the input stream starts with a quote character, skip to the next
00565      quote character, regardless of the content.  */
00566   if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
00567     {
00568       const char *end;
00569       char quote_char = *PARSER_STREAM (parser);
00570 
00571       /* Special case: Ada operators.  */
00572       if (PARSER_STATE (parser)->language->la_language == language_ada
00573           && quote_char == '\"')
00574         {
00575           int len = is_ada_operator (PARSER_STREAM (parser));
00576 
00577           if (len != 0)
00578             {
00579               /* The input is an Ada operator.  Return the quoted string
00580                  as-is.  */
00581               LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
00582               LS_TOKEN_STOKEN (token).length = len;
00583               PARSER_STREAM (parser) += len;
00584               return token;
00585             }
00586 
00587           /* The input does not represent an Ada operator -- fall through
00588              to normal quoted string handling.  */
00589         }
00590 
00591       /* Skip past the beginning quote.  */
00592       ++(PARSER_STREAM (parser));
00593 
00594       /* Mark the start of the string.  */
00595       LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
00596 
00597       /* Skip to the ending quote.  */
00598       end = skip_quote_char (PARSER_STREAM (parser), quote_char);
00599 
00600       /* Error if the input did not terminate properly.  */
00601       if (end == NULL)
00602         error (_("unmatched quote"));
00603 
00604       /* Skip over the ending quote and mark the length of the string.  */
00605       PARSER_STREAM (parser) = (char *) ++end;
00606       LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
00607     }
00608   else
00609     {
00610       const char *p;
00611 
00612       /* Otherwise, only identifier characters are permitted.
00613          Spaces are the exception.  In general, we keep spaces,
00614          but only if the next characters in the input do not resolve
00615          to one of the keywords.
00616 
00617          This allows users to forgo quoting CV-qualifiers, template arguments,
00618          and similar common language constructs.  */
00619 
00620       while (1)
00621         {
00622           if (isspace (*PARSER_STREAM (parser)))
00623             {
00624               p = skip_spaces_const (PARSER_STREAM (parser));
00625               /* When we get here we know we've found something followed by
00626                  a space (we skip over parens and templates below).
00627                  So if we find a keyword now, we know it is a keyword and not,
00628                  say, a function name.  */
00629               if (linespec_lexer_lex_keyword (p) != NULL)
00630                 {
00631                   LS_TOKEN_STOKEN (token).ptr = start;
00632                   LS_TOKEN_STOKEN (token).length
00633                     = PARSER_STREAM (parser) - start;
00634                   return token;
00635                 }
00636 
00637               /* Advance past the whitespace.  */
00638               PARSER_STREAM (parser) = p;
00639             }
00640 
00641           /* If the next character is EOI or (single) ':', the
00642              string is complete;  return the token.  */
00643           if (*PARSER_STREAM (parser) == 0)
00644             {
00645               LS_TOKEN_STOKEN (token).ptr = start;
00646               LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
00647               return token;
00648             }
00649           else if (PARSER_STREAM (parser)[0] == ':')
00650             {
00651               /* Do not tokenize the C++ scope operator. */
00652               if (PARSER_STREAM (parser)[1] == ':')
00653                 ++(PARSER_STREAM (parser));
00654 
00655               /* Do not tokenify if the input length so far is one
00656                  (i.e, a single-letter drive name) and the next character
00657                  is a directory separator.  This allows Windows-style
00658                  paths to be recognized as filenames without quoting it.  */
00659               else if ((PARSER_STREAM (parser) - start) != 1
00660                        || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
00661                 {
00662                   LS_TOKEN_STOKEN (token).ptr = start;
00663                   LS_TOKEN_STOKEN (token).length
00664                     = PARSER_STREAM (parser) - start;
00665                   return token;
00666                 }
00667             }
00668           /* Special case: permit quote-enclosed linespecs.  */
00669           else if (parser->is_quote_enclosed
00670                    && strchr (linespec_quote_characters,
00671                               *PARSER_STREAM (parser))
00672                    && is_closing_quote_enclosed (PARSER_STREAM (parser)))
00673             {
00674               LS_TOKEN_STOKEN (token).ptr = start;
00675               LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
00676               return token;
00677             }
00678           /* Because commas may terminate a linespec and appear in
00679              the middle of valid string input, special cases for
00680              '<' and '(' are necessary.  */
00681           else if (*PARSER_STREAM (parser) == '<'
00682                    || *PARSER_STREAM (parser) == '(')
00683             {
00684               const char *p;
00685 
00686               p = find_parameter_list_end (PARSER_STREAM (parser));
00687               if (p != NULL)
00688                 {
00689                   PARSER_STREAM (parser) = p;
00690                   continue;
00691                 }
00692             }
00693           /* Commas are terminators, but not if they are part of an
00694              operator name.  */
00695           else if (*PARSER_STREAM (parser) == ',')
00696             {
00697               if ((PARSER_STATE (parser)->language->la_language
00698                    == language_cplus)
00699                   && (PARSER_STREAM (parser) - start) > 8
00700                   /* strlen ("operator") */)
00701                 {
00702                   char *p = strstr (start, "operator");
00703 
00704                   if (p != NULL && is_operator_name (p))
00705                     {
00706                       /* This is an operator name.  Keep going.  */
00707                       ++(PARSER_STREAM (parser));
00708                       continue;
00709                     }
00710                 }
00711 
00712               /* Comma terminates the string.  */
00713               LS_TOKEN_STOKEN (token).ptr = start;
00714               LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
00715               return token;
00716             }
00717 
00718           /* Advance the stream.  */
00719           ++(PARSER_STREAM (parser));
00720         }
00721     }
00722 
00723   return token;
00724 }
00725 
00726 /* Lex a single linespec token from PARSER.  */
00727 
00728 static linespec_token
00729 linespec_lexer_lex_one (linespec_parser *parser)
00730 {
00731   const char *keyword;
00732 
00733   if (parser->lexer.current.type == LSTOKEN_CONSUMED)
00734     {
00735       /* Skip any whitespace.  */
00736       PARSER_STREAM (parser) = skip_spaces_const (PARSER_STREAM (parser));
00737 
00738       /* Check for a keyword, they end the linespec.  */
00739       keyword = NULL;
00740       if (parser->keyword_ok)
00741         keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
00742       if (keyword != NULL)
00743         {
00744           parser->lexer.current.type = LSTOKEN_KEYWORD;
00745           LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
00746           return parser->lexer.current;
00747         }
00748 
00749       /* Handle other tokens.  */
00750       switch (*PARSER_STREAM (parser))
00751         {
00752         case 0:
00753           parser->lexer.current.type = LSTOKEN_EOI;
00754           break;
00755 
00756         case '+': case '-':
00757         case '0': case '1': case '2': case '3': case '4':
00758         case '5': case '6': case '7': case '8': case '9':
00759            if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
00760              parser->lexer.current = linespec_lexer_lex_string (parser);
00761           break;
00762 
00763         case ':':
00764           /* If we have a scope operator, lex the input as a string.
00765              Otherwise, return LSTOKEN_COLON.  */
00766           if (PARSER_STREAM (parser)[1] == ':')
00767             parser->lexer.current = linespec_lexer_lex_string (parser);
00768           else
00769             {
00770               parser->lexer.current.type = LSTOKEN_COLON;
00771               ++(PARSER_STREAM (parser));
00772             }
00773           break;
00774 
00775         case '\'': case '\"':
00776           /* Special case: permit quote-enclosed linespecs.  */
00777           if (parser->is_quote_enclosed
00778               && is_closing_quote_enclosed (PARSER_STREAM (parser)))
00779             {
00780               ++(PARSER_STREAM (parser));
00781               parser->lexer.current.type = LSTOKEN_EOI;
00782             }
00783           else
00784             parser->lexer.current = linespec_lexer_lex_string (parser);
00785           break;
00786 
00787         case ',':
00788           parser->lexer.current.type = LSTOKEN_COMMA;
00789           LS_TOKEN_STOKEN (parser->lexer.current).ptr
00790             = PARSER_STREAM (parser);
00791           LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
00792           ++(PARSER_STREAM (parser));
00793           break;
00794 
00795         default:
00796           /* If the input is not a number, it must be a string.
00797              [Keywords were already considered above.]  */
00798           parser->lexer.current = linespec_lexer_lex_string (parser);
00799           break;
00800         }
00801     }
00802 
00803   return parser->lexer.current;
00804 }
00805 
00806 /* Consume the current token and return the next token in PARSER's
00807    input stream.  */
00808 
00809 static linespec_token
00810 linespec_lexer_consume_token (linespec_parser *parser)
00811 {
00812   parser->lexer.current.type = LSTOKEN_CONSUMED;
00813   return linespec_lexer_lex_one (parser);
00814 }
00815 
00816 /* Return the next token without consuming the current token.  */
00817 
00818 static linespec_token
00819 linespec_lexer_peek_token (linespec_parser *parser)
00820 {
00821   linespec_token next;
00822   const char *saved_stream = PARSER_STREAM (parser);
00823   linespec_token saved_token = parser->lexer.current;
00824 
00825   next = linespec_lexer_consume_token (parser);
00826   PARSER_STREAM (parser) = saved_stream;
00827   parser->lexer.current = saved_token;
00828   return next;
00829 }
00830 
00831 /* Helper functions.  */
00832 
00833 /* Add SAL to SALS.  */
00834 
00835 static void
00836 add_sal_to_sals_basic (struct symtabs_and_lines *sals,
00837                        struct symtab_and_line *sal)
00838 {
00839   ++sals->nelts;
00840   sals->sals = xrealloc (sals->sals, sals->nelts * sizeof (sals->sals[0]));
00841   sals->sals[sals->nelts - 1] = *sal;
00842 }
00843 
00844 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
00845    the new sal, if needed.  If not NULL, SYMNAME is the name of the
00846    symbol to use when constructing the new canonical name.
00847 
00848    If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
00849    canonical name for the SAL.  */
00850 
00851 static void
00852 add_sal_to_sals (struct linespec_state *self,
00853                  struct symtabs_and_lines *sals,
00854                  struct symtab_and_line *sal,
00855                  const char *symname, int literal_canonical)
00856 {
00857   add_sal_to_sals_basic (sals, sal);
00858 
00859   if (self->canonical)
00860     {
00861       struct linespec_canonical_name *canonical;
00862 
00863       self->canonical_names = xrealloc (self->canonical_names,
00864                                         (sals->nelts
00865                                          * sizeof (*self->canonical_names)));
00866       canonical = &self->canonical_names[sals->nelts - 1];
00867       if (!literal_canonical && sal->symtab)
00868         {
00869           const char *fullname = symtab_to_fullname (sal->symtab);
00870 
00871           /* Note that the filter doesn't have to be a valid linespec
00872              input.  We only apply the ":LINE" treatment to Ada for
00873              the time being.  */
00874           if (symname != NULL && sal->line != 0
00875               && self->language->la_language == language_ada)
00876             canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
00877           else if (symname != NULL)
00878             canonical->suffix = xstrdup (symname);
00879           else
00880             canonical->suffix = xstrprintf ("%d", sal->line);
00881           canonical->symtab = sal->symtab;
00882         }
00883       else
00884         {
00885           if (symname != NULL)
00886             canonical->suffix = xstrdup (symname);
00887           else
00888             canonical->suffix = NULL;
00889           canonical->symtab = NULL;
00890         }
00891     }
00892 }
00893 
00894 /* A hash function for address_entry.  */
00895 
00896 static hashval_t
00897 hash_address_entry (const void *p)
00898 {
00899   const struct address_entry *aep = p;
00900   hashval_t hash;
00901 
00902   hash = iterative_hash_object (aep->pspace, 0);
00903   return iterative_hash_object (aep->addr, hash);
00904 }
00905 
00906 /* An equality function for address_entry.  */
00907 
00908 static int
00909 eq_address_entry (const void *a, const void *b)
00910 {
00911   const struct address_entry *aea = a;
00912   const struct address_entry *aeb = b;
00913 
00914   return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
00915 }
00916 
00917 /* Check whether the address, represented by PSPACE and ADDR, is
00918    already in the set.  If so, return 0.  Otherwise, add it and return
00919    1.  */
00920 
00921 static int
00922 maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
00923 {
00924   struct address_entry e, *p;
00925   void **slot;
00926 
00927   e.pspace = pspace;
00928   e.addr = addr;
00929   slot = htab_find_slot (set, &e, INSERT);
00930   if (*slot)
00931     return 0;
00932 
00933   p = XNEW (struct address_entry);
00934   memcpy (p, &e, sizeof (struct address_entry));
00935   *slot = p;
00936 
00937   return 1;
00938 }
00939 
00940 /* A callback function and the additional data to call it with.  */
00941 
00942 struct symbol_and_data_callback
00943 {
00944   /* The callback to use.  */
00945   symbol_found_callback_ftype *callback;
00946 
00947   /* Data to be passed to the callback.  */
00948   void *data;
00949 };
00950 
00951 /* A helper for iterate_over_all_matching_symtabs that is used to
00952    restrict calls to another callback to symbols representing inline
00953    symbols only.  */
00954 
00955 static int
00956 iterate_inline_only (struct symbol *sym, void *d)
00957 {
00958   if (SYMBOL_INLINED (sym))
00959     {
00960       struct symbol_and_data_callback *cad = d;
00961 
00962       return cad->callback (sym, cad->data);
00963     }
00964   return 1; /* Continue iterating.  */
00965 }
00966 
00967 /* Some data for the expand_symtabs_matching callback.  */
00968 
00969 struct symbol_matcher_data
00970 {
00971   /* The lookup name against which symbol name should be compared.  */
00972   const char *lookup_name;
00973 
00974   /* The routine to be used for comparison.  */
00975   symbol_name_cmp_ftype symbol_name_cmp;
00976 };
00977 
00978 /* A helper for iterate_over_all_matching_symtabs that is passed as a
00979    callback to the expand_symtabs_matching method.  */
00980 
00981 static int
00982 iterate_name_matcher (const char *name, void *d)
00983 {
00984   const struct symbol_matcher_data *data = d;
00985 
00986   if (data->symbol_name_cmp (name, data->lookup_name) == 0)
00987     return 1; /* Expand this symbol's symbol table.  */
00988   return 0; /* Skip this symbol.  */
00989 }
00990 
00991 /* A helper that walks over all matching symtabs in all objfiles and
00992    calls CALLBACK for each symbol matching NAME.  If SEARCH_PSPACE is
00993    not NULL, then the search is restricted to just that program
00994    space.  If INCLUDE_INLINE is nonzero then symbols representing
00995    inlined instances of functions will be included in the result.  */
00996 
00997 static void
00998 iterate_over_all_matching_symtabs (struct linespec_state *state,
00999                                    const char *name,
01000                                    const domain_enum domain,
01001                                    symbol_found_callback_ftype *callback,
01002                                    void *data,
01003                                    struct program_space *search_pspace,
01004                                    int include_inline)
01005 {
01006   struct objfile *objfile;
01007   struct program_space *pspace;
01008   struct symbol_matcher_data matcher_data;
01009 
01010   matcher_data.lookup_name = name;
01011   matcher_data.symbol_name_cmp =
01012     state->language->la_get_symbol_name_cmp != NULL
01013     ? state->language->la_get_symbol_name_cmp (name)
01014     : strcmp_iw;
01015 
01016   ALL_PSPACES (pspace)
01017   {
01018     if (search_pspace != NULL && search_pspace != pspace)
01019       continue;
01020     if (pspace->executing_startup)
01021       continue;
01022 
01023     set_current_program_space (pspace);
01024 
01025     ALL_OBJFILES (objfile)
01026     {
01027       struct symtab *symtab;
01028 
01029       if (objfile->sf)
01030         objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
01031                                                   iterate_name_matcher,
01032                                                   ALL_DOMAIN,
01033                                                   &matcher_data);
01034 
01035       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, symtab)
01036         {
01037           iterate_over_file_blocks (symtab, name, domain, callback, data);
01038 
01039           if (include_inline)
01040             {
01041               struct symbol_and_data_callback cad = { callback, data };
01042               struct block *block;
01043               int i;
01044 
01045               for (i = FIRST_LOCAL_BLOCK;
01046                    i < BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (symtab)); i++)
01047                 {
01048                   block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), i);
01049                   state->language->la_iterate_over_symbols
01050                     (block, name, domain, iterate_inline_only, &cad);
01051                 }
01052             }
01053         }
01054     }
01055   }
01056 }
01057 
01058 /* Returns the block to be used for symbol searches from
01059    the current location.  */
01060 
01061 static struct block *
01062 get_current_search_block (void)
01063 {
01064   struct block *block;
01065   enum language save_language;
01066 
01067   /* get_selected_block can change the current language when there is
01068      no selected frame yet.  */
01069   save_language = current_language->la_language;
01070   block = get_selected_block (0);
01071   set_language (save_language);
01072 
01073   return block;
01074 }
01075 
01076 /* Iterate over static and global blocks.  */
01077 
01078 static void
01079 iterate_over_file_blocks (struct symtab *symtab,
01080                           const char *name, domain_enum domain,
01081                           symbol_found_callback_ftype *callback, void *data)
01082 {
01083   struct block *block;
01084 
01085   for (block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
01086        block != NULL;
01087        block = BLOCK_SUPERBLOCK (block))
01088     LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
01089 }
01090 
01091 /* A helper for find_method.  This finds all methods in type T which
01092    match NAME.  It adds matching symbol names to RESULT_NAMES, and
01093    adds T's direct superclasses to SUPERCLASSES.  */
01094 
01095 static void
01096 find_methods (struct type *t, const char *name,
01097               VEC (const_char_ptr) **result_names,
01098               VEC (typep) **superclasses)
01099 {
01100   int ibase;
01101   const char *class_name = type_name_no_tag (t);
01102 
01103   /* Ignore this class if it doesn't have a name.  This is ugly, but
01104      unless we figure out how to get the physname without the name of
01105      the class, then the loop can't do any good.  */
01106   if (class_name)
01107     {
01108       int method_counter;
01109 
01110       CHECK_TYPEDEF (t);
01111 
01112       /* Loop over each method name.  At this level, all overloads of a name
01113          are counted as a single name.  There is an inner loop which loops over
01114          each overload.  */
01115 
01116       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
01117            method_counter >= 0;
01118            --method_counter)
01119         {
01120           const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
01121           char dem_opname[64];
01122 
01123           if (strncmp (method_name, "__", 2) == 0 ||
01124               strncmp (method_name, "op", 2) == 0 ||
01125               strncmp (method_name, "type", 4) == 0)
01126             {
01127               if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
01128                 method_name = dem_opname;
01129               else if (cplus_demangle_opname (method_name, dem_opname, 0))
01130                 method_name = dem_opname;
01131             }
01132 
01133           if (strcmp_iw (method_name, name) == 0)
01134             {
01135               int field_counter;
01136 
01137               for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
01138                                     - 1);
01139                    field_counter >= 0;
01140                    --field_counter)
01141                 {
01142                   struct fn_field *f;
01143                   const char *phys_name;
01144 
01145                   f = TYPE_FN_FIELDLIST1 (t, method_counter);
01146                   if (TYPE_FN_FIELD_STUB (f, field_counter))
01147                     continue;
01148                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
01149                   VEC_safe_push (const_char_ptr, *result_names, phys_name);
01150                 }
01151             }
01152         }
01153     }
01154 
01155   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
01156     VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
01157 }
01158 
01159 /* Find an instance of the character C in the string S that is outside
01160    of all parenthesis pairs, single-quoted strings, and double-quoted
01161    strings.  Also, ignore the char within a template name, like a ','
01162    within foo<int, int>.  */
01163 
01164 static const char *
01165 find_toplevel_char (const char *s, char c)
01166 {
01167   int quoted = 0;               /* zero if we're not in quotes;
01168                                    '"' if we're in a double-quoted string;
01169                                    '\'' if we're in a single-quoted string.  */
01170   int depth = 0;                /* Number of unclosed parens we've seen.  */
01171   const char *scan;
01172 
01173   for (scan = s; *scan; scan++)
01174     {
01175       if (quoted)
01176         {
01177           if (*scan == quoted)
01178             quoted = 0;
01179           else if (*scan == '\\' && *(scan + 1))
01180             scan++;
01181         }
01182       else if (*scan == c && ! quoted && depth == 0)
01183         return scan;
01184       else if (*scan == '"' || *scan == '\'')
01185         quoted = *scan;
01186       else if (*scan == '(' || *scan == '<')
01187         depth++;
01188       else if ((*scan == ')' || *scan == '>') && depth > 0)
01189         depth--;
01190     }
01191 
01192   return 0;
01193 }
01194 
01195 /* The string equivalent of find_toplevel_char.  Returns a pointer
01196    to the location of NEEDLE in HAYSTACK, ignoring any occurrences
01197    inside "()" and "<>".  Returns NULL if NEEDLE was not found.  */
01198 
01199 static const char *
01200 find_toplevel_string (const char *haystack, const char *needle)
01201 {
01202   const char *s = haystack;
01203 
01204   do
01205     {
01206       s = find_toplevel_char (s, *needle);
01207 
01208       if (s != NULL)
01209         {
01210           /* Found first char in HAYSTACK;  check rest of string.  */
01211           if (strncmp (s, needle, strlen (needle)) == 0)
01212             return s;
01213 
01214           /* Didn't find it; loop over HAYSTACK, looking for the next
01215              instance of the first character of NEEDLE.  */
01216           ++s;
01217         }
01218     }
01219   while (s != NULL && *s != '\0');
01220 
01221   /* NEEDLE was not found in HAYSTACK.  */
01222   return NULL;
01223 }
01224 
01225 /* Convert CANONICAL to its string representation using
01226    symtab_to_fullname for SYMTAB.  The caller must xfree the result.  */
01227 
01228 static char *
01229 canonical_to_fullform (const struct linespec_canonical_name *canonical)
01230 {
01231   if (canonical->symtab == NULL)
01232     return xstrdup (canonical->suffix);
01233   else
01234     return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
01235                        canonical->suffix);
01236 }
01237 
01238 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
01239    and store the result in SELF->CANONICAL.  */
01240 
01241 static void
01242 filter_results (struct linespec_state *self,
01243                 struct symtabs_and_lines *result,
01244                 VEC (const_char_ptr) *filters)
01245 {
01246   int i;
01247   const char *name;
01248 
01249   for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
01250     {
01251       struct linespec_sals lsal;
01252       int j;
01253 
01254       memset (&lsal, 0, sizeof (lsal));
01255 
01256       for (j = 0; j < result->nelts; ++j)
01257         {
01258           const struct linespec_canonical_name *canonical;
01259           char *fullform;
01260           struct cleanup *cleanup;
01261 
01262           canonical = &self->canonical_names[j];
01263           fullform = canonical_to_fullform (canonical);
01264           cleanup = make_cleanup (xfree, fullform);
01265 
01266           if (strcmp (name, fullform) == 0)
01267             add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);
01268 
01269           do_cleanups (cleanup);
01270         }
01271 
01272       if (lsal.sals.nelts > 0)
01273         {
01274           lsal.canonical = xstrdup (name);
01275           VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
01276         }
01277     }
01278 
01279   self->canonical->pre_expanded = 0;
01280 }
01281 
01282 /* Store RESULT into SELF->CANONICAL.  */
01283 
01284 static void
01285 convert_results_to_lsals (struct linespec_state *self,
01286                           struct symtabs_and_lines *result)
01287 {
01288   struct linespec_sals lsal;
01289 
01290   lsal.canonical = NULL;
01291   lsal.sals = *result;
01292   VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
01293 }
01294 
01295 /* A structure that contains two string representations of a struct
01296    linespec_canonical_name:
01297      - one where the the symtab's fullname is used;
01298      - one where the filename followed the "set filename-display"
01299        setting.  */
01300 
01301 struct decode_line_2_item
01302 {
01303   /* The form using symtab_to_fullname.
01304      It must be xfree'ed after use.  */
01305   char *fullform;
01306 
01307   /* The form using symtab_to_filename_for_display.
01308      It must be xfree'ed after use.  */
01309   char *displayform;
01310 
01311   /* Field is initialized to zero and it is set to one if the user
01312      requested breakpoint for this entry.  */
01313   unsigned int selected : 1;
01314 };
01315 
01316 /* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
01317    secondarily by FULLFORM.  */
01318 
01319 static int
01320 decode_line_2_compare_items (const void *ap, const void *bp)
01321 {
01322   const struct decode_line_2_item *a = ap;
01323   const struct decode_line_2_item *b = bp;
01324   int retval;
01325 
01326   retval = strcmp (a->displayform, b->displayform);
01327   if (retval != 0)
01328     return retval;
01329 
01330   return strcmp (a->fullform, b->fullform);
01331 }
01332 
01333 /* Handle multiple results in RESULT depending on SELECT_MODE.  This
01334    will either return normally, throw an exception on multiple
01335    results, or present a menu to the user.  On return, the SALS vector
01336    in SELF->CANONICAL is set up properly.  */
01337 
01338 static void
01339 decode_line_2 (struct linespec_state *self,
01340                struct symtabs_and_lines *result,
01341                const char *select_mode)
01342 {
01343   char *args, *prompt;
01344   int i;
01345   struct cleanup *old_chain;
01346   VEC (const_char_ptr) *filters = NULL;
01347   struct get_number_or_range_state state;
01348   struct decode_line_2_item *items;
01349   int items_count;
01350 
01351   gdb_assert (select_mode != multiple_symbols_all);
01352   gdb_assert (self->canonical != NULL);
01353   gdb_assert (result->nelts >= 1);
01354 
01355   old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
01356 
01357   /* Prepare ITEMS array.  */
01358   items_count = result->nelts;
01359   items = xmalloc (sizeof (*items) * items_count);
01360   make_cleanup (xfree, items);
01361   for (i = 0; i < items_count; ++i)
01362     {
01363       const struct linespec_canonical_name *canonical;
01364       struct decode_line_2_item *item;
01365 
01366       canonical = &self->canonical_names[i];
01367       gdb_assert (canonical->suffix != NULL);
01368       item = &items[i];
01369 
01370       item->fullform = canonical_to_fullform (canonical);
01371       make_cleanup (xfree, item->fullform);
01372 
01373       if (canonical->symtab == NULL)
01374         item->displayform = canonical->suffix;
01375       else
01376         {
01377           const char *fn_for_display;
01378 
01379           fn_for_display = symtab_to_filename_for_display (canonical->symtab);
01380           item->displayform = xstrprintf ("%s:%s", fn_for_display,
01381                                           canonical->suffix);
01382           make_cleanup (xfree, item->displayform);
01383         }
01384 
01385       item->selected = 0;
01386     }
01387 
01388   /* Sort the list of method names.  */
01389   qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
01390 
01391   /* Remove entries with the same FULLFORM.  */
01392   if (items_count >= 2)
01393     {
01394       struct decode_line_2_item *dst, *src;
01395 
01396       dst = items;
01397       for (src = &items[1]; src < &items[items_count]; src++)
01398         if (strcmp (src->fullform, dst->fullform) != 0)
01399           *++dst = *src;
01400       items_count = dst + 1 - items;
01401     }
01402 
01403   if (select_mode == multiple_symbols_cancel && items_count > 1)
01404     error (_("canceled because the command is ambiguous\n"
01405              "See set/show multiple-symbol."));
01406   
01407   if (select_mode == multiple_symbols_all || items_count == 1)
01408     {
01409       do_cleanups (old_chain);
01410       convert_results_to_lsals (self, result);
01411       return;
01412     }
01413 
01414   printf_unfiltered (_("[0] cancel\n[1] all\n"));
01415   for (i = 0; i < items_count; i++)
01416     printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);
01417 
01418   prompt = getenv ("PS2");
01419   if (prompt == NULL)
01420     {
01421       prompt = "> ";
01422     }
01423   args = command_line_input (prompt, 0, "overload-choice");
01424 
01425   if (args == 0 || *args == 0)
01426     error_no_arg (_("one or more choice numbers"));
01427 
01428   init_number_or_range (&state, args);
01429   while (!state.finished)
01430     {
01431       int num;
01432 
01433       num = get_number_or_range (&state);
01434 
01435       if (num == 0)
01436         error (_("canceled"));
01437       else if (num == 1)
01438         {
01439           /* We intentionally make this result in a single breakpoint,
01440              contrary to what older versions of gdb did.  The
01441              rationale is that this lets a user get the
01442              multiple_symbols_all behavior even with the 'ask'
01443              setting; and he can get separate breakpoints by entering
01444              "2-57" at the query.  */
01445           do_cleanups (old_chain);
01446           convert_results_to_lsals (self, result);
01447           return;
01448         }
01449 
01450       num -= 2;
01451       if (num >= items_count)
01452         printf_unfiltered (_("No choice number %d.\n"), num);
01453       else
01454         {
01455           struct decode_line_2_item *item = &items[num];
01456 
01457           if (!item->selected)
01458             {
01459               VEC_safe_push (const_char_ptr, filters, item->fullform);
01460               item->selected = 1;
01461             }
01462           else
01463             {
01464               printf_unfiltered (_("duplicate request for %d ignored.\n"),
01465                                  num + 2);
01466             }
01467         }
01468     }
01469 
01470   filter_results (self, result, filters);
01471   do_cleanups (old_chain);
01472 }
01473 
01474 
01475 
01476 /* The parser of linespec itself.  */
01477 
01478 /* Throw an appropriate error when SYMBOL is not found (optionally in
01479    FILENAME).  */
01480 
01481 static void ATTRIBUTE_NORETURN
01482 symbol_not_found_error (const char *symbol, const char *filename)
01483 {
01484   if (symbol == NULL)
01485     symbol = "";
01486 
01487   if (!have_full_symbols ()
01488       && !have_partial_symbols ()
01489       && !have_minimal_symbols ())
01490     throw_error (NOT_FOUND_ERROR,
01491                  _("No symbol table is loaded.  Use the \"file\" command."));
01492 
01493   /* If SYMBOL starts with '$', the user attempted to either lookup
01494      a function/variable in his code starting with '$' or an internal
01495      variable of that name.  Since we do not know which, be concise and
01496      explain both possibilities.  */
01497   if (*symbol == '$')
01498     {
01499       if (filename)
01500         throw_error (NOT_FOUND_ERROR,
01501                      _("Undefined convenience variable or function \"%s\" "
01502                        "not defined in \"%s\"."), symbol, filename);
01503       else
01504         throw_error (NOT_FOUND_ERROR,
01505                      _("Undefined convenience variable or function \"%s\" "
01506                        "not defined."), symbol);
01507     }
01508   else
01509     {
01510       if (filename)
01511         throw_error (NOT_FOUND_ERROR,
01512                      _("Function \"%s\" not defined in \"%s\"."),
01513                      symbol, filename);
01514       else
01515         throw_error (NOT_FOUND_ERROR,
01516                      _("Function \"%s\" not defined."), symbol);
01517     }
01518 }
01519 
01520 /* Throw an appropriate error when an unexpected token is encountered 
01521    in the input.  */
01522 
01523 static void ATTRIBUTE_NORETURN
01524 unexpected_linespec_error (linespec_parser *parser)
01525 {
01526   linespec_token token;
01527   static const char * token_type_strings[]
01528     = {"keyword", "colon", "string", "number", "comma", "end of input"};
01529 
01530   /* Get the token that generated the error.  */
01531   token = linespec_lexer_lex_one (parser);
01532 
01533   /* Finally, throw the error.  */
01534   if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
01535       || token.type == LSTOKEN_KEYWORD)
01536     {
01537       char *string;
01538       struct cleanup *cleanup;
01539 
01540       string = copy_token_string (token);
01541       cleanup = make_cleanup (xfree, string);
01542       throw_error (GENERIC_ERROR,
01543                    _("malformed linespec error: unexpected %s, \"%s\""),
01544                    token_type_strings[token.type], string);
01545     }
01546   else
01547     throw_error (GENERIC_ERROR,
01548                  _("malformed linespec error: unexpected %s"),
01549                  token_type_strings[token.type]);
01550 }
01551 
01552 /* Parse and return a line offset in STRING.  */
01553 
01554 static struct line_offset
01555 linespec_parse_line_offset (const char *string)
01556 {
01557   struct line_offset line_offset = {0, LINE_OFFSET_NONE};
01558 
01559   if (*string == '+')
01560     {
01561       line_offset.sign = LINE_OFFSET_PLUS;
01562       ++string;
01563     }
01564   else if (*string == '-')
01565     {
01566       line_offset.sign = LINE_OFFSET_MINUS;
01567       ++string;
01568     }
01569 
01570   /* Right now, we only allow base 10 for offsets.  */
01571   line_offset.offset = atoi (string);
01572   return line_offset;
01573 }
01574 
01575 /* Parse the basic_spec in PARSER's input.  */
01576 
01577 static void
01578 linespec_parse_basic (linespec_parser *parser)
01579 {
01580   char *name;
01581   linespec_token token;
01582   VEC (symbolp) *symbols, *labels;
01583   VEC (bound_minimal_symbol_d) *minimal_symbols;
01584   struct cleanup *cleanup;
01585 
01586   /* Get the next token.  */
01587   token = linespec_lexer_lex_one (parser);
01588 
01589   /* If it is EOI or KEYWORD, issue an error.  */
01590   if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
01591     unexpected_linespec_error (parser);
01592   /* If it is a LSTOKEN_NUMBER, we have an offset.  */
01593   else if (token.type == LSTOKEN_NUMBER)
01594     {
01595       /* Record the line offset and get the next token.  */
01596       name = copy_token_string (token);
01597       cleanup = make_cleanup (xfree, name);
01598       PARSER_RESULT (parser)->line_offset = linespec_parse_line_offset (name);
01599       do_cleanups (cleanup);
01600 
01601       /* Get the next token.  */
01602       token = linespec_lexer_consume_token (parser);
01603 
01604       /* If the next token is a comma, stop parsing and return.  */
01605       if (token.type == LSTOKEN_COMMA)
01606         return;
01607 
01608       /* If the next token is anything but EOI or KEYWORD, issue
01609          an error.  */
01610       if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
01611         unexpected_linespec_error (parser);
01612     }
01613 
01614   if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
01615     return;
01616 
01617   /* Next token must be LSTOKEN_STRING.  */
01618   if (token.type != LSTOKEN_STRING)
01619     unexpected_linespec_error (parser);
01620 
01621   /* The current token will contain the name of a function, method,
01622      or label.  */
01623   name  = copy_token_string (token);
01624   cleanup = make_cleanup (xfree, name);
01625 
01626   /* Try looking it up as a function/method.  */
01627   find_linespec_symbols (PARSER_STATE (parser),
01628                          PARSER_RESULT (parser)->file_symtabs, name,
01629                          &symbols, &minimal_symbols);
01630 
01631   if (symbols != NULL || minimal_symbols != NULL)
01632     {
01633       PARSER_RESULT (parser)->function_symbols = symbols;
01634       PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
01635       PARSER_RESULT (parser)->function_name = name;
01636       symbols = NULL;
01637       discard_cleanups (cleanup);
01638     }
01639   else
01640     {
01641       /* NAME was not a function or a method.  So it must be a label
01642          name or user specified variable like "break foo.c:$zippo".  */
01643       labels = find_label_symbols (PARSER_STATE (parser), NULL,
01644                                    &symbols, name);
01645       if (labels != NULL)
01646         {
01647           PARSER_RESULT (parser)->labels.label_symbols = labels;
01648           PARSER_RESULT (parser)->labels.function_symbols = symbols;
01649           PARSER_RESULT (parser)->label_name = name;
01650           symbols = NULL;
01651           discard_cleanups (cleanup);
01652         }
01653       else if (token.type == LSTOKEN_STRING
01654                && *LS_TOKEN_STOKEN (token).ptr == '$')
01655         {
01656           /* User specified a convenience variable or history value.  */
01657           PARSER_RESULT (parser)->line_offset
01658             = linespec_parse_variable (PARSER_STATE (parser), name);
01659 
01660           if (PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
01661             {
01662               /* The user-specified variable was not valid.  Do not
01663                  throw an error here.  parse_linespec will do it for us.  */
01664               PARSER_RESULT (parser)->function_name = name;
01665               discard_cleanups (cleanup);
01666               return;
01667             }
01668         }
01669       else
01670         {
01671           /* The name is also not a label.  Abort parsing.  Do not throw
01672              an error here.  parse_linespec will do it for us.  */
01673 
01674           /* Save a copy of the name we were trying to lookup.  */
01675           PARSER_RESULT (parser)->function_name = name;
01676           discard_cleanups (cleanup);
01677           return;
01678         }
01679     }
01680 
01681   /* Get the next token.  */
01682   token = linespec_lexer_consume_token (parser);
01683 
01684   if (token.type == LSTOKEN_COLON)
01685     {
01686       /* User specified a label or a lineno.  */
01687       token = linespec_lexer_consume_token (parser);
01688 
01689       if (token.type == LSTOKEN_NUMBER)
01690         {
01691           /* User specified an offset.  Record the line offset and
01692              get the next token.  */
01693           name = copy_token_string (token);
01694           cleanup = make_cleanup (xfree, name);
01695           PARSER_RESULT (parser)->line_offset
01696             = linespec_parse_line_offset (name);
01697           do_cleanups (cleanup);
01698 
01699           /* Ge the next token.  */
01700           token = linespec_lexer_consume_token (parser);
01701         }
01702       else if (token.type == LSTOKEN_STRING)
01703         {
01704           /* Grab a copy of the label's name and look it up.  */
01705           name = copy_token_string (token);
01706           cleanup = make_cleanup (xfree, name);
01707           labels = find_label_symbols (PARSER_STATE (parser),
01708                                        PARSER_RESULT (parser)->function_symbols,
01709                                        &symbols, name);
01710 
01711           if (labels != NULL)
01712             {
01713               PARSER_RESULT (parser)->labels.label_symbols = labels;
01714               PARSER_RESULT (parser)->labels.function_symbols = symbols;
01715               PARSER_RESULT (parser)->label_name = name;
01716               symbols = NULL;
01717               discard_cleanups (cleanup);
01718             }
01719           else
01720             {
01721               /* We don't know what it was, but it isn't a label.  */
01722               throw_error (NOT_FOUND_ERROR,
01723                            _("No label \"%s\" defined in function \"%s\"."),
01724                            name, PARSER_RESULT (parser)->function_name);
01725             }
01726 
01727           /* Check for a line offset.  */
01728           token = linespec_lexer_consume_token (parser);
01729           if (token.type == LSTOKEN_COLON)
01730             {
01731               /* Get the next token.  */
01732               token = linespec_lexer_consume_token (parser);
01733 
01734               /* It must be a line offset.  */
01735               if (token.type != LSTOKEN_NUMBER)
01736                 unexpected_linespec_error (parser);
01737 
01738               /* Record the lione offset and get the next token.  */
01739               name = copy_token_string (token);
01740               cleanup = make_cleanup (xfree, name);
01741 
01742               PARSER_RESULT (parser)->line_offset
01743                 = linespec_parse_line_offset (name);
01744               do_cleanups (cleanup);
01745 
01746               /* Get the next token.  */
01747               token = linespec_lexer_consume_token (parser);
01748             }
01749         }
01750       else
01751         {
01752           /* Trailing ':' in the input. Issue an error.  */
01753           unexpected_linespec_error (parser);
01754         }
01755     }
01756 }
01757 
01758 /* Canonicalize the linespec contained in LS.  The result is saved into
01759    STATE->canonical.  */
01760 
01761 static void
01762 canonicalize_linespec (struct linespec_state *state, linespec_p ls)
01763 {
01764   /* If canonicalization was not requested, no need to do anything.  */
01765   if (!state->canonical)
01766     return;
01767 
01768   /* Shortcut expressions, which can only appear by themselves.  */
01769   if (ls->expression != NULL)
01770     state->canonical->addr_string = xstrdup (ls->expression);
01771   else
01772     {
01773       struct ui_file *buf;
01774       int need_colon = 0;
01775 
01776       buf = mem_fileopen ();
01777       if (ls->source_filename)
01778         {
01779           fputs_unfiltered (ls->source_filename, buf);
01780           need_colon = 1;
01781         }
01782 
01783       if (ls->function_name)
01784         {
01785           if (need_colon)
01786             fputc_unfiltered (':', buf);
01787           fputs_unfiltered (ls->function_name, buf);
01788           need_colon = 1;
01789         }
01790 
01791       if (ls->label_name)
01792         {
01793           if (need_colon)
01794             fputc_unfiltered (':', buf);
01795 
01796           if (ls->function_name == NULL)
01797             {
01798               struct symbol *s;
01799 
01800               /* No function was specified, so add the symbol name.  */
01801               gdb_assert (ls->labels.function_symbols != NULL
01802                           && (VEC_length (symbolp, ls->labels.function_symbols)
01803                               == 1));
01804               s = VEC_index (symbolp, ls->labels.function_symbols, 0);
01805               fputs_unfiltered (SYMBOL_NATURAL_NAME (s), buf);
01806               fputc_unfiltered (':', buf);
01807             }
01808 
01809           fputs_unfiltered (ls->label_name, buf);
01810           need_colon = 1;
01811           state->canonical->special_display = 1;
01812         }
01813 
01814       if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
01815         {
01816           if (need_colon)
01817             fputc_unfiltered (':', buf);
01818           fprintf_filtered (buf, "%s%d",
01819                             (ls->line_offset.sign == LINE_OFFSET_NONE ? ""
01820                              : (ls->line_offset.sign
01821                                 == LINE_OFFSET_PLUS ? "+" : "-")),
01822                             ls->line_offset.offset);
01823         }
01824 
01825       state->canonical->addr_string = ui_file_xstrdup (buf, NULL);
01826       ui_file_delete (buf);
01827     }
01828 }
01829 
01830 /* Given a line offset in LS, construct the relevant SALs.  */
01831 
01832 static struct symtabs_and_lines
01833 create_sals_line_offset (struct linespec_state *self,
01834                          linespec_p ls)
01835 {
01836   struct symtabs_and_lines values;
01837   struct symtab_and_line val;
01838   int use_default = 0;
01839 
01840   init_sal (&val);
01841   values.sals = NULL;
01842   values.nelts = 0;
01843 
01844   /* This is where we need to make sure we have good defaults.
01845      We must guarantee that this section of code is never executed
01846      when we are called with just a function name, since
01847      set_default_source_symtab_and_line uses
01848      select_source_symtab that calls us with such an argument.  */
01849 
01850   if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
01851       && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
01852     {
01853       const char *fullname;
01854 
01855       set_current_program_space (self->program_space);
01856 
01857       /* Make sure we have at least a default source line.  */
01858       set_default_source_symtab_and_line ();
01859       initialize_defaults (&self->default_symtab, &self->default_line);
01860       fullname = symtab_to_fullname (self->default_symtab);
01861       VEC_pop (symtab_ptr, ls->file_symtabs);
01862       VEC_free (symtab_ptr, ls->file_symtabs);
01863       ls->file_symtabs = collect_symtabs_from_filename (fullname);
01864       use_default = 1;
01865     }
01866 
01867   val.line = ls->line_offset.offset;
01868   switch (ls->line_offset.sign)
01869     {
01870     case LINE_OFFSET_PLUS:
01871       if (ls->line_offset.offset == 0)
01872         val.line = 5;
01873       if (use_default)
01874         val.line = self->default_line + val.line;
01875       break;
01876 
01877     case LINE_OFFSET_MINUS:
01878       if (ls->line_offset.offset == 0)
01879         val.line = 15;
01880       if (use_default)
01881         val.line = self->default_line - val.line;
01882       else
01883         val.line = -val.line;
01884       break;
01885 
01886     case LINE_OFFSET_NONE:
01887       break;                    /* No need to adjust val.line.  */
01888     }
01889 
01890   if (self->list_mode)
01891     decode_digits_list_mode (self, ls, &values, val);
01892   else
01893     {
01894       struct linetable_entry *best_entry = NULL;
01895       int *filter;
01896       struct block **blocks;
01897       struct cleanup *cleanup;
01898       struct symtabs_and_lines intermediate_results;
01899       int i, j;
01900 
01901       intermediate_results.sals = NULL;
01902       intermediate_results.nelts = 0;
01903 
01904       decode_digits_ordinary (self, ls, val.line, &intermediate_results,
01905                               &best_entry);
01906       if (intermediate_results.nelts == 0 && best_entry != NULL)
01907         decode_digits_ordinary (self, ls, best_entry->line,
01908                                 &intermediate_results, &best_entry);
01909 
01910       cleanup = make_cleanup (xfree, intermediate_results.sals);
01911 
01912       /* For optimized code, the compiler can scatter one source line
01913          across disjoint ranges of PC values, even when no duplicate
01914          functions or inline functions are involved.  For example,
01915          'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
01916          function can result in two PC ranges.  In this case, we don't
01917          want to set a breakpoint on the first PC of each range.  To filter
01918          such cases, we use containing blocks -- for each PC found
01919          above, we see if there are other PCs that are in the same
01920          block.  If yes, the other PCs are filtered out.  */
01921 
01922       filter = XNEWVEC (int, intermediate_results.nelts);
01923       make_cleanup (xfree, filter);
01924       blocks = XNEWVEC (struct block *, intermediate_results.nelts);
01925       make_cleanup (xfree, blocks);
01926 
01927       for (i = 0; i < intermediate_results.nelts; ++i)
01928         {
01929           set_current_program_space (intermediate_results.sals[i].pspace);
01930 
01931           filter[i] = 1;
01932           blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
01933                                          intermediate_results.sals[i].section);
01934         }
01935 
01936       for (i = 0; i < intermediate_results.nelts; ++i)
01937         {
01938           if (blocks[i] != NULL)
01939             for (j = i + 1; j < intermediate_results.nelts; ++j)
01940               {
01941                 if (blocks[j] == blocks[i])
01942                   {
01943                     filter[j] = 0;
01944                     break;
01945                   }
01946               }
01947         }
01948 
01949       for (i = 0; i < intermediate_results.nelts; ++i)
01950         if (filter[i])
01951           {
01952             struct symbol *sym = (blocks[i]
01953                                   ? block_containing_function (blocks[i])
01954                                   : NULL);
01955 
01956             if (self->funfirstline)
01957               skip_prologue_sal (&intermediate_results.sals[i]);
01958             /* Make sure the line matches the request, not what was
01959                found.  */
01960             intermediate_results.sals[i].line = val.line;
01961             add_sal_to_sals (self, &values, &intermediate_results.sals[i],
01962                              sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
01963           }
01964 
01965       do_cleanups (cleanup);
01966     }
01967 
01968   if (values.nelts == 0)
01969     {
01970       if (ls->source_filename)
01971         throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
01972                      val.line, ls->source_filename);
01973       else
01974         throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
01975                      val.line);
01976     }
01977 
01978   return values;
01979 }
01980 
01981 /* Create and return SALs from the linespec LS.  */
01982 
01983 static struct symtabs_and_lines
01984 convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
01985 {
01986   struct symtabs_and_lines sals = {NULL, 0};
01987 
01988   if (ls->expression != NULL)
01989     {
01990       struct symtab_and_line sal;
01991 
01992       /* We have an expression.  No other attribute is allowed.  */
01993       sal = find_pc_line (ls->expr_pc, 0);
01994       sal.pc = ls->expr_pc;
01995       sal.section = find_pc_overlay (ls->expr_pc);
01996       sal.explicit_pc = 1;
01997       add_sal_to_sals (state, &sals, &sal, ls->expression, 1);
01998     }
01999   else if (ls->labels.label_symbols != NULL)
02000     {
02001       /* We have just a bunch of functions/methods or labels.  */
02002       int i;
02003       struct symtab_and_line sal;
02004       struct symbol *sym;
02005 
02006       for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
02007         {
02008           struct program_space *pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
02009 
02010           if (symbol_to_sal (&sal, state->funfirstline, sym)
02011               && maybe_add_address (state->addr_set, pspace, sal.pc))
02012             add_sal_to_sals (state, &sals, &sal,
02013                              SYMBOL_NATURAL_NAME (sym), 0);
02014         }
02015     }
02016   else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
02017     {
02018       /* We have just a bunch of functions and/or methods.  */
02019       int i;
02020       struct symtab_and_line sal;
02021       struct symbol *sym;
02022       bound_minimal_symbol_d *elem;
02023       struct program_space *pspace;
02024 
02025       if (ls->function_symbols != NULL)
02026         {
02027           /* Sort symbols so that symbols with the same program space are next
02028              to each other.  */
02029           qsort (VEC_address (symbolp, ls->function_symbols),
02030                  VEC_length (symbolp, ls->function_symbols),
02031                  sizeof (symbolp), compare_symbols);
02032 
02033           for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
02034             {
02035               pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
02036               set_current_program_space (pspace);
02037               if (symbol_to_sal (&sal, state->funfirstline, sym)
02038                   && maybe_add_address (state->addr_set, pspace, sal.pc))
02039                 add_sal_to_sals (state, &sals, &sal,
02040                                  SYMBOL_NATURAL_NAME (sym), 0);
02041             }
02042         }
02043 
02044       if (ls->minimal_symbols != NULL)
02045         {
02046           /* Sort minimal symbols by program space, too.  */
02047           qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
02048                  VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
02049                  sizeof (bound_minimal_symbol_d), compare_msymbols);
02050 
02051           for (i = 0;
02052                VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
02053                             i, elem);
02054                ++i)
02055             {
02056               pspace = elem->objfile->pspace;
02057               set_current_program_space (pspace);
02058               minsym_found (state, elem->objfile, elem->minsym, &sals);
02059             }
02060         }
02061     }
02062   else if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
02063     {
02064       /* Only an offset was specified.  */
02065         sals = create_sals_line_offset (state, ls);
02066 
02067         /* Make sure we have a filename for canonicalization.  */
02068         if (ls->source_filename == NULL)
02069           {
02070             const char *fullname = symtab_to_fullname (state->default_symtab);
02071 
02072             /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
02073                form so that displaying SOURCE_FILENAME can follow the current
02074                FILENAME_DISPLAY_STRING setting.  But as it is used only rarely
02075                it has been kept for code simplicity only in absolute form.  */
02076             ls->source_filename = xstrdup (fullname);
02077           }
02078     }
02079   else
02080     {
02081       /* We haven't found any results...  */
02082       return sals;
02083     }
02084 
02085   canonicalize_linespec (state, ls);
02086 
02087   if (sals.nelts > 0 && state->canonical != NULL)
02088     state->canonical->pre_expanded = 1;
02089 
02090   return sals;
02091 }
02092 
02093 /* Parse a string that specifies a linespec.
02094    Pass the address of a char * variable; that variable will be
02095    advanced over the characters actually parsed.
02096 
02097    The basic grammar of linespecs:
02098 
02099    linespec -> expr_spec | var_spec | basic_spec
02100    expr_spec -> '*' STRING
02101    var_spec -> '$' (STRING | NUMBER)
02102 
02103    basic_spec -> file_offset_spec | function_spec | label_spec
02104    file_offset_spec -> opt_file_spec offset_spec
02105    function_spec -> opt_file_spec function_name_spec opt_label_spec
02106    label_spec -> label_name_spec
02107 
02108    opt_file_spec -> "" | file_name_spec ':'
02109    opt_label_spec -> "" | ':' label_name_spec
02110 
02111    file_name_spec -> STRING
02112    function_name_spec -> STRING
02113    label_name_spec -> STRING
02114    function_name_spec -> STRING
02115    offset_spec -> NUMBER
02116                -> '+' NUMBER
02117                -> '-' NUMBER
02118 
02119    This may all be followed by several keywords such as "if EXPR",
02120    which we ignore.
02121 
02122    A comma will terminate parsing.
02123 
02124    The function may be an undebuggable function found in minimal symbol table.
02125 
02126    If the argument FUNFIRSTLINE is nonzero, we want the first line
02127    of real code inside a function when a function is specified, and it is
02128    not OK to specify a variable or type to get its line number.
02129 
02130    DEFAULT_SYMTAB specifies the file to use if none is specified.
02131    It defaults to current_source_symtab.
02132    DEFAULT_LINE specifies the line number to use for relative
02133    line numbers (that start with signs).  Defaults to current_source_line.
02134    If CANONICAL is non-NULL, store an array of strings containing the canonical
02135    line specs there if necessary.  Currently overloaded member functions and
02136    line numbers or static functions without a filename yield a canonical
02137    line spec.  The array and the line spec strings are allocated on the heap,
02138    it is the callers responsibility to free them.
02139 
02140    Note that it is possible to return zero for the symtab
02141    if no file is validly specified.  Callers must check that.
02142    Also, the line number returned may be invalid.  */
02143 
02144 /* Parse the linespec in ARGPTR.  */
02145 
02146 static struct symtabs_and_lines
02147 parse_linespec (linespec_parser *parser, const char **argptr)
02148 {
02149   linespec_token token;
02150   struct symtabs_and_lines values;
02151   volatile struct gdb_exception file_exception;
02152   struct cleanup *cleanup;
02153 
02154   /* A special case to start.  It has become quite popular for
02155      IDEs to work around bugs in the previous parser by quoting
02156      the entire linespec, so we attempt to deal with this nicely.  */
02157   parser->is_quote_enclosed = 0;
02158   if (!is_ada_operator (*argptr)
02159       && strchr (linespec_quote_characters, **argptr) != NULL)
02160     {
02161       const char *end;
02162 
02163       end = skip_quote_char (*argptr + 1, **argptr);
02164       if (end != NULL && is_closing_quote_enclosed (end))
02165         {
02166           /* Here's the special case.  Skip ARGPTR past the initial
02167              quote.  */
02168           ++(*argptr);
02169           parser->is_quote_enclosed = 1;
02170         }
02171     }
02172 
02173   /* A keyword at the start cannot be interpreted as such.
02174      Consider "b thread thread 42".  */
02175   parser->keyword_ok = 0;
02176 
02177   parser->lexer.saved_arg = *argptr;
02178   parser->lexer.stream = argptr;
02179   file_exception.reason = 0;
02180 
02181   /* Initialize the default symtab and line offset.  */
02182   initialize_defaults (&PARSER_STATE (parser)->default_symtab,
02183                        &PARSER_STATE (parser)->default_line);
02184 
02185   /* Objective-C shortcut.  */
02186   values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), argptr);
02187   if (values.sals != NULL)
02188     return values;
02189 
02190   /* Start parsing.  */
02191 
02192   /* Get the first token.  */
02193   token = linespec_lexer_lex_one (parser);
02194 
02195   /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER.  */
02196   if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '*')
02197     {
02198       char *expr;
02199       const char *copy;
02200 
02201       /* User specified an expression, *EXPR.  */
02202       copy = expr = copy_token_string (token);
02203       cleanup = make_cleanup (xfree, expr);
02204       PARSER_RESULT (parser)->expr_pc = linespec_expression_to_pc (&copy);
02205       discard_cleanups (cleanup);
02206       PARSER_RESULT (parser)->expression = expr;
02207 
02208       /* This is a little hacky/tricky.  If linespec_expression_to_pc
02209          did not evaluate the entire token, then we must find the
02210          string COPY inside the original token buffer.  */
02211       if (*copy != '\0')
02212         {
02213           PARSER_STREAM (parser) = strstr (parser->lexer.saved_arg, copy);
02214           gdb_assert (PARSER_STREAM (parser) != NULL);
02215         }
02216 
02217       /* Consume the token.  */
02218       linespec_lexer_consume_token (parser);
02219 
02220       goto convert_to_sals;
02221     }
02222   else if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
02223     {
02224       char *var;
02225 
02226       /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
02227       VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
02228 
02229       /* User specified a convenience variable or history value.  */
02230       var = copy_token_string (token);
02231       cleanup = make_cleanup (xfree, var);
02232       PARSER_RESULT (parser)->line_offset
02233         = linespec_parse_variable (PARSER_STATE (parser), var);
02234       do_cleanups (cleanup);
02235 
02236       /* If a line_offset wasn't found (VAR is the name of a user
02237          variable/function), then skip to normal symbol processing.  */
02238       if (PARSER_RESULT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
02239         {
02240           /* Consume this token.  */
02241           linespec_lexer_consume_token (parser);
02242 
02243           goto convert_to_sals;
02244         }
02245     }
02246   else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
02247     unexpected_linespec_error (parser);
02248 
02249   /* Now we can recognize keywords.  */
02250   parser->keyword_ok = 1;
02251 
02252   /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
02253      this token cannot represent a filename.  */
02254   token = linespec_lexer_peek_token (parser);
02255 
02256   if (token.type == LSTOKEN_COLON)
02257     {
02258       char *user_filename;
02259 
02260       /* Get the current token again and extract the filename.  */
02261       token = linespec_lexer_lex_one (parser);
02262       user_filename = copy_token_string (token);
02263 
02264       /* Check if the input is a filename.  */
02265       TRY_CATCH (file_exception, RETURN_MASK_ERROR)
02266         {
02267           PARSER_RESULT (parser)->file_symtabs
02268             = symtabs_from_filename (user_filename);
02269         }
02270 
02271       if (file_exception.reason >= 0)
02272         {
02273           /* Symtabs were found for the file.  Record the filename.  */
02274           PARSER_RESULT (parser)->source_filename = user_filename;
02275 
02276           /* Get the next token.  */
02277           token = linespec_lexer_consume_token (parser);
02278 
02279           /* This is LSTOKEN_COLON; consume it.  */
02280           linespec_lexer_consume_token (parser);
02281         }
02282       else
02283         {
02284           /* No symtabs found -- discard user_filename.  */
02285           xfree (user_filename);
02286 
02287           /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
02288           VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
02289         }
02290     }
02291   /* If the next token is not EOI, KEYWORD, or COMMA, issue an error.  */
02292   else if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
02293            && token.type != LSTOKEN_COMMA)
02294     {
02295       /* TOKEN is the _next_ token, not the one currently in the parser.
02296          Consuming the token will give the correct error message.  */
02297       linespec_lexer_consume_token (parser);
02298       unexpected_linespec_error (parser);
02299     }
02300   else
02301     {
02302       /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
02303       VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
02304     }
02305 
02306   /* Parse the rest of the linespec.  */
02307   linespec_parse_basic (parser);
02308 
02309   if (PARSER_RESULT (parser)->function_symbols == NULL
02310       && PARSER_RESULT (parser)->labels.label_symbols == NULL
02311       && PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
02312       && PARSER_RESULT (parser)->minimal_symbols == NULL)
02313     {
02314       /* The linespec didn't parse.  Re-throw the file exception if
02315          there was one.  */
02316       if (file_exception.reason < 0)
02317         throw_exception (file_exception);
02318 
02319       /* Otherwise, the symbol is not found.  */
02320       symbol_not_found_error (PARSER_RESULT (parser)->function_name,
02321                               PARSER_RESULT (parser)->source_filename);
02322     }
02323 
02324  convert_to_sals:
02325 
02326   /* Get the last token and record how much of the input was parsed,
02327      if necessary.  */
02328   token = linespec_lexer_lex_one (parser);
02329   if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
02330     PARSER_STREAM (parser) = LS_TOKEN_STOKEN (token).ptr;
02331 
02332   /* Convert the data in PARSER_RESULT to SALs.  */
02333   values = convert_linespec_to_sals (PARSER_STATE (parser),
02334                                      PARSER_RESULT (parser));
02335 
02336   return values;
02337 }
02338 
02339 
02340 /* A constructor for linespec_state.  */
02341 
02342 static void
02343 linespec_state_constructor (struct linespec_state *self,
02344                             int flags, const struct language_defn *language,
02345                             struct symtab *default_symtab,
02346                             int default_line,
02347                             struct linespec_result *canonical)
02348 {
02349   memset (self, 0, sizeof (*self));
02350   self->language = language;
02351   self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
02352   self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
02353   self->default_symtab = default_symtab;
02354   self->default_line = default_line;
02355   self->canonical = canonical;
02356   self->program_space = current_program_space;
02357   self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
02358                                       xfree, xcalloc, xfree);
02359 }
02360 
02361 /* Initialize a new linespec parser.  */
02362 
02363 static void
02364 linespec_parser_new (linespec_parser *parser,
02365                      int flags, const struct language_defn *language,
02366                      struct symtab *default_symtab,
02367                      int default_line,
02368                      struct linespec_result *canonical)
02369 {
02370   parser->lexer.current.type = LSTOKEN_CONSUMED;
02371   memset (PARSER_RESULT (parser), 0, sizeof (struct linespec));
02372   PARSER_RESULT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN;
02373   linespec_state_constructor (PARSER_STATE (parser), flags, language,
02374                               default_symtab, default_line, canonical);
02375 }
02376 
02377 /* A destructor for linespec_state.  */
02378 
02379 static void
02380 linespec_state_destructor (struct linespec_state *self)
02381 {
02382   htab_delete (self->addr_set);
02383 }
02384 
02385 /* Delete a linespec parser.  */
02386 
02387 static void
02388 linespec_parser_delete (void *arg)
02389 {
02390   linespec_parser *parser = (linespec_parser *) arg;
02391 
02392   xfree ((char *) PARSER_RESULT (parser)->expression);
02393   xfree ((char *) PARSER_RESULT (parser)->source_filename);
02394   xfree ((char *) PARSER_RESULT (parser)->label_name);
02395   xfree ((char *) PARSER_RESULT (parser)->function_name);
02396 
02397   if (PARSER_RESULT (parser)->file_symtabs != NULL)
02398     VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);
02399 
02400   if (PARSER_RESULT (parser)->function_symbols != NULL)
02401     VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);
02402 
02403   if (PARSER_RESULT (parser)->minimal_symbols != NULL)
02404     VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);
02405 
02406   if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
02407     VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);
02408 
02409   if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
02410     VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);
02411 
02412   linespec_state_destructor (PARSER_STATE (parser));
02413 }
02414 
02415 /* See linespec.h.  */
02416 
02417 void
02418 decode_line_full (char **argptr, int flags,
02419                   struct symtab *default_symtab,
02420                   int default_line, struct linespec_result *canonical,
02421                   const char *select_mode,
02422                   const char *filter)
02423 {
02424   struct symtabs_and_lines result;
02425   struct cleanup *cleanups;
02426   VEC (const_char_ptr) *filters = NULL;
02427   linespec_parser parser;
02428   struct linespec_state *state;
02429   const char *copy, *orig;
02430 
02431   gdb_assert (canonical != NULL);
02432   /* The filter only makes sense for 'all'.  */
02433   gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
02434   gdb_assert (select_mode == NULL
02435               || select_mode == multiple_symbols_all
02436               || select_mode == multiple_symbols_ask
02437               || select_mode == multiple_symbols_cancel);
02438   gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
02439 
02440   linespec_parser_new (&parser, flags, current_language, default_symtab,
02441                        default_line, canonical);
02442   cleanups = make_cleanup (linespec_parser_delete, &parser);
02443   save_current_program_space ();
02444 
02445   orig = copy = *argptr;
02446   result = parse_linespec (&parser, &copy);
02447   *argptr += copy - orig;
02448   state = PARSER_STATE (&parser);
02449 
02450   gdb_assert (result.nelts == 1 || canonical->pre_expanded);
02451   gdb_assert (canonical->addr_string != NULL);
02452   canonical->pre_expanded = 1;
02453 
02454   /* Arrange for allocated canonical names to be freed.  */
02455   if (result.nelts > 0)
02456     {
02457       int i;
02458 
02459       make_cleanup (xfree, state->canonical_names);
02460       for (i = 0; i < result.nelts; ++i)
02461         {
02462           gdb_assert (state->canonical_names[i].suffix != NULL);
02463           make_cleanup (xfree, state->canonical_names[i].suffix);
02464         }
02465     }
02466 
02467   if (select_mode == NULL)
02468     {
02469       if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
02470         select_mode = multiple_symbols_all;
02471       else
02472         select_mode = multiple_symbols_select_mode ();
02473     }
02474 
02475   if (select_mode == multiple_symbols_all)
02476     {
02477       if (filter != NULL)
02478         {
02479           make_cleanup (VEC_cleanup (const_char_ptr), &filters);
02480           VEC_safe_push (const_char_ptr, filters, filter);
02481           filter_results (state, &result, filters);
02482         }
02483       else
02484         convert_results_to_lsals (state, &result);
02485     }
02486   else
02487     decode_line_2 (state, &result, select_mode);
02488 
02489   do_cleanups (cleanups);
02490 }
02491 
02492 /* See linespec.h.  */
02493 
02494 struct symtabs_and_lines
02495 decode_line_1 (char **argptr, int flags,
02496                struct symtab *default_symtab,
02497                int default_line)
02498 {
02499   struct symtabs_and_lines result;
02500   linespec_parser parser;
02501   struct cleanup *cleanups;
02502   const char *copy, *orig;
02503 
02504   linespec_parser_new (&parser, flags, current_language, default_symtab,
02505                        default_line, NULL);
02506   cleanups = make_cleanup (linespec_parser_delete, &parser);
02507   save_current_program_space ();
02508 
02509   orig = copy = *argptr;
02510   result = parse_linespec (&parser, &copy);
02511   *argptr += copy - orig;
02512 
02513   do_cleanups (cleanups);
02514   return result;
02515 }
02516 
02517 /* See linespec.h.  */
02518 
02519 struct symtabs_and_lines
02520 decode_line_with_current_source (char *string, int flags)
02521 {
02522   struct symtabs_and_lines sals;
02523   struct symtab_and_line cursal;
02524 
02525   if (string == 0)
02526     error (_("Empty line specification."));
02527 
02528   /* We use whatever is set as the current source line.  We do not try
02529      and get a default source symtab+line or it will recursively call us!  */
02530   cursal = get_current_source_symtab_and_line ();
02531 
02532   sals = decode_line_1 (&string, flags,
02533                         cursal.symtab, cursal.line);
02534 
02535   if (*string)
02536     error (_("Junk at end of line specification: %s"), string);
02537   return sals;
02538 }
02539 
02540 /* See linespec.h.  */
02541 
02542 struct symtabs_and_lines
02543 decode_line_with_last_displayed (char *string, int flags)
02544 {
02545   struct symtabs_and_lines sals;
02546 
02547   if (string == 0)
02548     error (_("Empty line specification."));
02549 
02550   if (last_displayed_sal_is_valid ())
02551     sals = decode_line_1 (&string, flags,
02552                           get_last_displayed_symtab (),
02553                           get_last_displayed_line ());
02554   else
02555     sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
02556 
02557   if (*string)
02558     error (_("Junk at end of line specification: %s"), string);
02559   return sals;
02560 }
02561 
02562 
02563 
02564 /* First, some functions to initialize stuff at the beggining of the
02565    function.  */
02566 
02567 static void
02568 initialize_defaults (struct symtab **default_symtab, int *default_line)
02569 {
02570   if (*default_symtab == 0)
02571     {
02572       /* Use whatever we have for the default source line.  We don't use
02573          get_current_or_default_symtab_and_line as it can recurse and call
02574          us back!  */
02575       struct symtab_and_line cursal = 
02576         get_current_source_symtab_and_line ();
02577       
02578       *default_symtab = cursal.symtab;
02579       *default_line = cursal.line;
02580     }
02581 }
02582 
02583 
02584 
02585 /* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
02586    advancing EXP_PTR past any parsed text.  */
02587 
02588 static CORE_ADDR
02589 linespec_expression_to_pc (const char **exp_ptr)
02590 {
02591   if (current_program_space->executing_startup)
02592     /* The error message doesn't really matter, because this case
02593        should only hit during breakpoint reset.  */
02594     throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
02595                                     "program space is in startup"));
02596 
02597   (*exp_ptr)++;
02598   return value_as_address (parse_to_comma_and_eval (exp_ptr));
02599 }
02600 
02601 
02602 
02603 /* Here's where we recognise an Objective-C Selector.  An Objective C
02604    selector may be implemented by more than one class, therefore it
02605    may represent more than one method/function.  This gives us a
02606    situation somewhat analogous to C++ overloading.  If there's more
02607    than one method that could represent the selector, then use some of
02608    the existing C++ code to let the user choose one.  */
02609 
02610 static struct symtabs_and_lines
02611 decode_objc (struct linespec_state *self, linespec_p ls, const char **argptr)
02612 {
02613   struct collect_info info;
02614   VEC (const_char_ptr) *symbol_names = NULL;
02615   struct symtabs_and_lines values;
02616   const char *new_argptr;
02617   struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
02618                                           &symbol_names);
02619 
02620   info.state = self;
02621   info.file_symtabs = NULL;
02622   VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
02623   make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
02624   info.result.symbols = NULL;
02625   info.result.minimal_symbols = NULL;
02626   values.nelts = 0;
02627   values.sals = NULL;
02628 
02629   new_argptr = find_imps (*argptr, &symbol_names); 
02630   if (VEC_empty (const_char_ptr, symbol_names))
02631     {
02632       do_cleanups (cleanup);
02633       return values;
02634     }
02635 
02636   add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
02637 
02638   if (!VEC_empty (symbolp, info.result.symbols)
02639       || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
02640     {
02641       char *saved_arg;
02642 
02643       saved_arg = alloca (new_argptr - *argptr + 1);
02644       memcpy (saved_arg, *argptr, new_argptr - *argptr);
02645       saved_arg[new_argptr - *argptr] = '\0';
02646 
02647       ls->function_name = xstrdup (saved_arg);
02648       ls->function_symbols = info.result.symbols;
02649       ls->minimal_symbols = info.result.minimal_symbols;
02650       values = convert_linespec_to_sals (self, ls);
02651 
02652       if (self->canonical)
02653         {
02654           self->canonical->pre_expanded = 1;
02655           if (ls->source_filename)
02656             self->canonical->addr_string
02657               = xstrprintf ("%s:%s", ls->source_filename, saved_arg);
02658           else
02659             self->canonical->addr_string = xstrdup (saved_arg);
02660         }
02661     }
02662 
02663   *argptr = new_argptr;
02664 
02665   do_cleanups (cleanup);
02666 
02667   return values;
02668 }
02669 
02670 /* An instance of this type is used when collecting prefix symbols for
02671    decode_compound.  */
02672 
02673 struct decode_compound_collector
02674 {
02675   /* The result vector.  */
02676   VEC (symbolp) *symbols;
02677 
02678   /* A hash table of all symbols we found.  We use this to avoid
02679      adding any symbol more than once.  */
02680   htab_t unique_syms;
02681 };
02682 
02683 /* A callback for iterate_over_symbols that is used by
02684    lookup_prefix_sym to collect type symbols.  */
02685 
02686 static int
02687 collect_one_symbol (struct symbol *sym, void *d)
02688 {
02689   struct decode_compound_collector *collector = d;
02690   void **slot;
02691   struct type *t;
02692 
02693   if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
02694     return 1; /* Continue iterating.  */
02695 
02696   t = SYMBOL_TYPE (sym);
02697   CHECK_TYPEDEF (t);
02698   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
02699       && TYPE_CODE (t) != TYPE_CODE_UNION
02700       && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
02701     return 1; /* Continue iterating.  */
02702 
02703   slot = htab_find_slot (collector->unique_syms, sym, INSERT);
02704   if (!*slot)
02705     {
02706       *slot = sym;
02707       VEC_safe_push (symbolp, collector->symbols, sym);
02708     }
02709 
02710   return 1; /* Continue iterating.  */
02711 }
02712 
02713 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS.  */
02714 
02715 static VEC (symbolp) *
02716 lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
02717                    const char *class_name)
02718 {
02719   int ix;
02720   struct symtab *elt;
02721   struct decode_compound_collector collector;
02722   struct cleanup *outer;
02723   struct cleanup *cleanup;
02724 
02725   collector.symbols = NULL;
02726   outer = make_cleanup (VEC_cleanup (symbolp), &collector.symbols);
02727 
02728   collector.unique_syms = htab_create_alloc (1, htab_hash_pointer,
02729                                              htab_eq_pointer, NULL,
02730                                              xcalloc, xfree);
02731   cleanup = make_cleanup_htab_delete (collector.unique_syms);
02732 
02733   for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
02734     {
02735       if (elt == NULL)
02736         {
02737           iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN,
02738                                              collect_one_symbol, &collector,
02739                                              NULL, 0);
02740           iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN,
02741                                              collect_one_symbol, &collector,
02742                                              NULL, 0);
02743         }
02744       else
02745         {
02746           /* Program spaces that are executing startup should have
02747              been filtered out earlier.  */
02748           gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
02749           set_current_program_space (SYMTAB_PSPACE (elt));
02750           iterate_over_file_blocks (elt, class_name, STRUCT_DOMAIN,
02751                                     collect_one_symbol, &collector);
02752           iterate_over_file_blocks (elt, class_name, VAR_DOMAIN,
02753                                     collect_one_symbol, &collector);
02754         }
02755     }
02756 
02757   do_cleanups (cleanup);
02758   discard_cleanups (outer);
02759   return collector.symbols;
02760 }
02761 
02762 /* A qsort comparison function for symbols.  The resulting order does
02763    not actually matter; we just need to be able to sort them so that
02764    symbols with the same program space end up next to each other.  */
02765 
02766 static int
02767 compare_symbols (const void *a, const void *b)
02768 {
02769   struct symbol * const *sa = a;
02770   struct symbol * const *sb = b;
02771   uintptr_t uia, uib;
02772 
02773   uia = (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sa));
02774   uib = (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sb));
02775 
02776   if (uia < uib)
02777     return -1;
02778   if (uia > uib)
02779     return 1;
02780 
02781   uia = (uintptr_t) *sa;
02782   uib = (uintptr_t) *sb;
02783 
02784   if (uia < uib)
02785     return -1;
02786   if (uia > uib)
02787     return 1;
02788 
02789   return 0;
02790 }
02791 
02792 /* Like compare_symbols but for minimal symbols.  */
02793 
02794 static int
02795 compare_msymbols (const void *a, const void *b)
02796 {
02797   const struct bound_minimal_symbol *sa = a;
02798   const struct bound_minimal_symbol *sb = b;
02799   uintptr_t uia, uib;
02800 
02801   uia = (uintptr_t) sa->objfile->pspace;
02802   uib = (uintptr_t) sa->objfile->pspace;
02803 
02804   if (uia < uib)
02805     return -1;
02806   if (uia > uib)
02807     return 1;
02808 
02809   uia = (uintptr_t) sa->minsym;
02810   uib = (uintptr_t) sb->minsym;
02811 
02812   if (uia < uib)
02813     return -1;
02814   if (uia > uib)
02815     return 1;
02816 
02817   return 0;
02818 }
02819 
02820 /* Look for all the matching instances of each symbol in NAMES.  Only
02821    instances from PSPACE are considered; other program spaces are
02822    handled by our caller.  If PSPACE is NULL, then all program spaces
02823    are considered.  Results are stored into INFO.  */
02824 
02825 static void
02826 add_all_symbol_names_from_pspace (struct collect_info *info,
02827                                   struct program_space *pspace,
02828                                   VEC (const_char_ptr) *names)
02829 {
02830   int ix;
02831   const char *iter;
02832 
02833   for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
02834     add_matching_symbols_to_info (iter, info, pspace);
02835 }
02836 
02837 static void
02838 find_superclass_methods (VEC (typep) *superclasses,
02839                          const char *name,
02840                          VEC (const_char_ptr) **result_names)
02841 {
02842   int old_len = VEC_length (const_char_ptr, *result_names);
02843   VEC (typep) *iter_classes;
02844   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
02845 
02846   iter_classes = superclasses;
02847   while (1)
02848     {
02849       VEC (typep) *new_supers = NULL;
02850       int ix;
02851       struct type *t;
02852 
02853       make_cleanup (VEC_cleanup (typep), &new_supers);
02854       for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
02855         find_methods (t, name, result_names, &new_supers);
02856 
02857       if (VEC_length (const_char_ptr, *result_names) != old_len
02858           || VEC_empty (typep, new_supers))
02859         break;
02860 
02861       iter_classes = new_supers;
02862     }
02863 
02864   do_cleanups (cleanup);
02865 }
02866 
02867 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
02868    given by one of the symbols in SYM_CLASSES.  Matches are returned
02869    in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols).  */
02870 
02871 static void
02872 find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
02873              const char *class_name, const char *method_name,
02874              VEC (symbolp) *sym_classes, VEC (symbolp) **symbols,
02875              VEC (bound_minimal_symbol_d) **minsyms)
02876 {
02877   struct symbol *sym;
02878   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
02879   int ix;
02880   int last_result_len;
02881   VEC (typep) *superclass_vec;
02882   VEC (const_char_ptr) *result_names;
02883   struct collect_info info;
02884 
02885   /* Sort symbols so that symbols with the same program space are next
02886      to each other.  */
02887   qsort (VEC_address (symbolp, sym_classes),
02888          VEC_length (symbolp, sym_classes),
02889          sizeof (symbolp),
02890          compare_symbols);
02891 
02892   info.state = self;
02893   info.file_symtabs = file_symtabs;
02894   info.result.symbols = NULL;
02895   info.result.minimal_symbols = NULL;
02896 
02897   /* Iterate over all the types, looking for the names of existing
02898      methods matching METHOD_NAME.  If we cannot find a direct method in a
02899      given program space, then we consider inherited methods; this is
02900      not ideal (ideal would be to respect C++ hiding rules), but it
02901      seems good enough and is what GDB has historically done.  We only
02902      need to collect the names because later we find all symbols with
02903      those names.  This loop is written in a somewhat funny way
02904      because we collect data across the program space before deciding
02905      what to do.  */
02906   superclass_vec = NULL;
02907   make_cleanup (VEC_cleanup (typep), &superclass_vec);
02908   result_names = NULL;
02909   make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
02910   last_result_len = 0;
02911   for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
02912     {
02913       struct type *t;
02914       struct program_space *pspace;
02915 
02916       /* Program spaces that are executing startup should have
02917          been filtered out earlier.  */
02918       gdb_assert (!SYMTAB_PSPACE (SYMBOL_SYMTAB (sym))->executing_startup);
02919       pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
02920       set_current_program_space (pspace);
02921       t = check_typedef (SYMBOL_TYPE (sym));
02922       find_methods (t, method_name, &result_names, &superclass_vec);
02923 
02924       /* Handle all items from a single program space at once; and be
02925          sure not to miss the last batch.  */
02926       if (ix == VEC_length (symbolp, sym_classes) - 1
02927           || (pspace
02928               != SYMTAB_PSPACE (SYMBOL_SYMTAB (VEC_index (symbolp, sym_classes,
02929                                                           ix + 1)))))
02930         {
02931           /* If we did not find a direct implementation anywhere in
02932              this program space, consider superclasses.  */
02933           if (VEC_length (const_char_ptr, result_names) == last_result_len)
02934             find_superclass_methods (superclass_vec, method_name,
02935                                      &result_names);
02936 
02937           /* We have a list of candidate symbol names, so now we
02938              iterate over the symbol tables looking for all
02939              matches in this pspace.  */
02940           add_all_symbol_names_from_pspace (&info, pspace, result_names);
02941 
02942           VEC_truncate (typep, superclass_vec, 0);
02943           last_result_len = VEC_length (const_char_ptr, result_names);
02944         }
02945     }
02946 
02947   if (!VEC_empty (symbolp, info.result.symbols)
02948       || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
02949     {
02950       *symbols = info.result.symbols;
02951       *minsyms = info.result.minimal_symbols;
02952       do_cleanups (cleanup);
02953       return;
02954     }
02955 
02956   /* Throw an NOT_FOUND_ERROR.  This will be caught by the caller
02957      and other attempts to locate the symbol will be made.  */
02958   throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
02959 }
02960 
02961 
02962 
02963 /* This object is used when collecting all matching symtabs.  */
02964 
02965 struct symtab_collector
02966 {
02967   /* The result vector of symtabs.  */
02968   VEC (symtab_ptr) *symtabs;
02969 
02970   /* This is used to ensure the symtabs are unique.  */
02971   htab_t symtab_table;
02972 };
02973 
02974 /* Callback for iterate_over_symtabs.  */
02975 
02976 static int
02977 add_symtabs_to_list (struct symtab *symtab, void *d)
02978 {
02979   struct symtab_collector *data = d;
02980   void **slot;
02981 
02982   slot = htab_find_slot (data->symtab_table, symtab, INSERT);
02983   if (!*slot)
02984     {
02985       *slot = symtab;
02986       VEC_safe_push (symtab_ptr, data->symtabs, symtab);
02987     }
02988 
02989   return 0;
02990 }
02991 
02992 /* Given a file name, return a VEC of all matching symtabs.  */
02993 
02994 static VEC (symtab_ptr) *
02995 collect_symtabs_from_filename (const char *file)
02996 {
02997   struct symtab_collector collector;
02998   struct cleanup *cleanups;
02999   struct program_space *pspace;
03000 
03001   collector.symtabs = NULL;
03002   collector.symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
03003                                         NULL);
03004   cleanups = make_cleanup_htab_delete (collector.symtab_table);
03005 
03006   /* Find that file's data.  */
03007   ALL_PSPACES (pspace)
03008   {
03009     if (pspace->executing_startup)
03010       continue;
03011 
03012     set_current_program_space (pspace);
03013     iterate_over_symtabs (file, add_symtabs_to_list, &collector);
03014   }
03015 
03016   do_cleanups (cleanups);
03017   return collector.symtabs;
03018 }
03019 
03020 /* Return all the symtabs associated to the FILENAME.  */
03021 
03022 static VEC (symtab_ptr) *
03023 symtabs_from_filename (const char *filename)
03024 {
03025   VEC (symtab_ptr) *result;
03026   
03027   result = collect_symtabs_from_filename (filename);
03028 
03029   if (VEC_empty (symtab_ptr, result))
03030     {
03031       if (!have_full_symbols () && !have_partial_symbols ())
03032         throw_error (NOT_FOUND_ERROR,
03033                      _("No symbol table is loaded.  "
03034                        "Use the \"file\" command."));
03035       throw_error (NOT_FOUND_ERROR, _("No source file named %s."), filename);
03036     }
03037 
03038   return result;
03039 }
03040 
03041 /* Look up a function symbol named NAME in symtabs FILE_SYMTABS.  Matching
03042    debug symbols are returned in SYMBOLS.  Matching minimal symbols are
03043    returned in MINSYMS.  */
03044 
03045 static void
03046 find_function_symbols (struct linespec_state *state,
03047                        VEC (symtab_ptr) *file_symtabs, const char *name,
03048                        VEC (symbolp) **symbols,
03049                        VEC (bound_minimal_symbol_d) **minsyms)
03050 {
03051   struct collect_info info;
03052   VEC (const_char_ptr) *symbol_names = NULL;
03053   struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
03054                                           &symbol_names);
03055 
03056   info.state = state;
03057   info.result.symbols = NULL;
03058   info.result.minimal_symbols = NULL;
03059   info.file_symtabs = file_symtabs;
03060 
03061   /* Try NAME as an Objective-C selector.  */
03062   find_imps (name, &symbol_names);
03063   if (!VEC_empty (const_char_ptr, symbol_names))
03064     add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
03065   else
03066     add_matching_symbols_to_info (name, &info, NULL);
03067 
03068   do_cleanups (cleanup);
03069 
03070   if (VEC_empty (symbolp, info.result.symbols))
03071     {
03072       VEC_free (symbolp, info.result.symbols);
03073       *symbols = NULL;
03074     }
03075   else
03076     *symbols = info.result.symbols;
03077 
03078   if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
03079     {
03080       VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
03081       *minsyms = NULL;
03082     }
03083   else
03084     *minsyms = info.result.minimal_symbols;
03085 }
03086 
03087 /* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
03088    in SYMBOLS and minimal symbols in MINSYMS.  */
03089 
03090 static void
03091 find_linespec_symbols (struct linespec_state *state,
03092                        VEC (symtab_ptr) *file_symtabs,
03093                        const char *name,
03094                        VEC (symbolp) **symbols,
03095                        VEC (bound_minimal_symbol_d) **minsyms)
03096 {
03097   struct cleanup *cleanup;
03098   char *canon;
03099   const char *lookup_name;
03100   volatile struct gdb_exception except;
03101 
03102   cleanup = demangle_for_lookup (name, state->language->la_language,
03103                                  &lookup_name);
03104   if (state->language->la_language == language_ada)
03105     {
03106       /* In Ada, the symbol lookups are performed using the encoded
03107          name rather than the demangled name.  */
03108       lookup_name = ada_name_for_lookup (name);
03109       make_cleanup (xfree, (void *) lookup_name);
03110     }
03111 
03112   canon = cp_canonicalize_string_no_typedefs (lookup_name);
03113   if (canon != NULL)
03114     {
03115       lookup_name = canon;
03116       make_cleanup (xfree, canon);
03117     }
03118 
03119   /* It's important to not call expand_symtabs_matching unnecessarily
03120      as it can really slow things down (by unnecessarily expanding
03121      potentially 1000s of symtabs, which when debugging some apps can
03122      cost 100s of seconds).  Avoid this to some extent by *first* calling
03123      find_function_symbols, and only if that doesn't find anything
03124      *then* call find_method.  This handles two important cases:
03125      1) break (anonymous namespace)::foo
03126      2) break class::method where method is in class (and not a baseclass)  */
03127 
03128   find_function_symbols (state, file_symtabs, lookup_name,
03129                          symbols, minsyms);
03130 
03131   /* If we were unable to locate a symbol of the same name, try dividing
03132      the name into class and method names and searching the class and its
03133      baseclasses.  */
03134   if (VEC_empty (symbolp, *symbols)
03135       && VEC_empty (bound_minimal_symbol_d, *minsyms))
03136     {
03137       char *klass, *method;
03138       const char *last, *p, *scope_op;
03139       VEC (symbolp) *classes;
03140 
03141       /* See if we can find a scope operator and break this symbol
03142          name into namespaces${SCOPE_OPERATOR}class_name and method_name.  */
03143       scope_op = "::";
03144       p = find_toplevel_string (lookup_name, scope_op);
03145       if (p == NULL)
03146         {
03147           /* No C++ scope operator.  Try Java.  */
03148           scope_op = ".";
03149           p = find_toplevel_string (lookup_name, scope_op);
03150         }
03151 
03152       last = NULL;
03153       while (p != NULL)
03154         {
03155           last = p;
03156           p = find_toplevel_string (p + strlen (scope_op), scope_op);
03157         }
03158 
03159       /* If no scope operator was found, there is nothing more we can do;
03160          we already attempted to lookup the entire name as a symbol
03161          and failed.  */
03162       if (last == NULL)
03163         {
03164           do_cleanups (cleanup);
03165           return;
03166         }
03167 
03168       /* LOOKUP_NAME points to the class name.
03169          LAST points to the method name.  */
03170       klass = xmalloc ((last - lookup_name + 1) * sizeof (char));
03171       make_cleanup (xfree, klass);
03172       strncpy (klass, lookup_name, last - lookup_name);
03173       klass[last - lookup_name] = '\0';
03174 
03175       /* Skip past the scope operator.  */
03176       last += strlen (scope_op);
03177       method = xmalloc ((strlen (last) + 1) * sizeof (char));
03178       make_cleanup (xfree, method);
03179       strcpy (method, last);
03180 
03181       /* Find a list of classes named KLASS.  */
03182       classes = lookup_prefix_sym (state, file_symtabs, klass);
03183       make_cleanup (VEC_cleanup (symbolp), &classes);
03184 
03185       if (!VEC_empty (symbolp, classes))
03186         {
03187           /* Now locate a list of suitable methods named METHOD.  */
03188           TRY_CATCH (except, RETURN_MASK_ERROR)
03189             {
03190               find_method (state, file_symtabs, klass, method, classes,
03191                            symbols, minsyms);
03192             }
03193 
03194           /* If successful, we're done.  If NOT_FOUND_ERROR
03195              was not thrown, rethrow the exception that we did get.  */
03196           if (except.reason < 0 && except.error != NOT_FOUND_ERROR)
03197             throw_exception (except);
03198         }
03199     }
03200 
03201   do_cleanups (cleanup);
03202 }
03203 
03204 /* Return all labels named NAME in FUNCTION_SYMBOLS.  Return the
03205    actual function symbol in which the label was found in LABEL_FUNC_RET.  */
03206 
03207 static VEC (symbolp) *
03208 find_label_symbols (struct linespec_state *self,
03209                     VEC (symbolp) *function_symbols,
03210                     VEC (symbolp) **label_funcs_ret, const char *name)
03211 {
03212   int ix;
03213   struct block *block;
03214   struct symbol *sym;
03215   struct symbol *fn_sym;
03216   VEC (symbolp) *result = NULL;
03217 
03218   if (function_symbols == NULL)
03219     {
03220       set_current_program_space (self->program_space);
03221       block = get_current_search_block ();
03222 
03223       for (;
03224            block && !BLOCK_FUNCTION (block);
03225            block = BLOCK_SUPERBLOCK (block))
03226         ;
03227       if (!block)
03228         return NULL;
03229       fn_sym = BLOCK_FUNCTION (block);
03230 
03231       sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);
03232 
03233       if (sym != NULL)
03234         {
03235           VEC_safe_push (symbolp, result, sym);
03236           VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
03237         }
03238     }
03239   else
03240     {
03241       for (ix = 0;
03242            VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
03243         {
03244           set_current_program_space (SYMTAB_PSPACE (SYMBOL_SYMTAB (fn_sym)));
03245           block = SYMBOL_BLOCK_VALUE (fn_sym);
03246           sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);
03247 
03248           if (sym != NULL)
03249             {
03250               VEC_safe_push (symbolp, result, sym);
03251               VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
03252             }
03253         }
03254     }
03255 
03256   return result;
03257 }
03258 
03259 
03260 
03261 /* A helper for create_sals_line_offset that handles the 'list_mode' case.  */
03262 
03263 static void
03264 decode_digits_list_mode (struct linespec_state *self,
03265                          linespec_p ls,
03266                          struct symtabs_and_lines *values,
03267                          struct symtab_and_line val)
03268 {
03269   int ix;
03270   struct symtab *elt;
03271 
03272   gdb_assert (self->list_mode);
03273 
03274   for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
03275        ++ix)
03276     {
03277       /* The logic above should ensure this.  */
03278       gdb_assert (elt != NULL);
03279 
03280       set_current_program_space (SYMTAB_PSPACE (elt));
03281 
03282       /* Simplistic search just for the list command.  */
03283       val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
03284       if (val.symtab == NULL)
03285         val.symtab = elt;
03286       val.pspace = SYMTAB_PSPACE (elt);
03287       val.pc = 0;
03288       val.explicit_line = 1;
03289 
03290       add_sal_to_sals (self, values, &val, NULL, 0);
03291     }
03292 }
03293 
03294 /* A helper for create_sals_line_offset that iterates over the symtabs,
03295    adding lines to the VEC.  */
03296 
03297 static void
03298 decode_digits_ordinary (struct linespec_state *self,
03299                         linespec_p ls,
03300                         int line,
03301                         struct symtabs_and_lines *sals,
03302                         struct linetable_entry **best_entry)
03303 {
03304   int ix;
03305   struct symtab *elt;
03306 
03307   for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
03308     {
03309       int i;
03310       VEC (CORE_ADDR) *pcs;
03311       CORE_ADDR pc;
03312 
03313       /* The logic above should ensure this.  */
03314       gdb_assert (elt != NULL);
03315 
03316       set_current_program_space (SYMTAB_PSPACE (elt));
03317 
03318       pcs = find_pcs_for_symtab_line (elt, line, best_entry);
03319       for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
03320         {
03321           struct symtab_and_line sal;
03322 
03323           init_sal (&sal);
03324           sal.pspace = SYMTAB_PSPACE (elt);
03325           sal.symtab = elt;
03326           sal.line = line;
03327           sal.pc = pc;
03328           add_sal_to_sals_basic (sals, &sal);
03329         }
03330 
03331       VEC_free (CORE_ADDR, pcs);
03332     }
03333 }
03334 
03335 
03336 
03337 /* Return the line offset represented by VARIABLE.  */
03338 
03339 static struct line_offset
03340 linespec_parse_variable (struct linespec_state *self, const char *variable)
03341 {
03342   int index = 0;
03343   const char *p;
03344   struct line_offset offset = {0, LINE_OFFSET_NONE};
03345 
03346   p = (variable[1] == '$') ? variable + 2 : variable + 1;
03347   if (*p == '$')
03348     ++p;
03349   while (*p >= '0' && *p <= '9')
03350     ++p;
03351   if (!*p)              /* Reached end of token without hitting non-digit.  */
03352     {
03353       /* We have a value history reference.  */
03354       struct value *val_history;
03355 
03356       sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
03357       val_history
03358         = access_value_history ((variable[1] == '$') ? -index : index);
03359       if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
03360         error (_("History values used in line "
03361                  "specs must have integer values."));
03362       offset.offset = value_as_long (val_history);
03363     }
03364   else
03365     {
03366       /* Not all digits -- may be user variable/function or a
03367          convenience variable.  */
03368       LONGEST valx;
03369       struct internalvar *ivar;
03370 
03371       /* Try it as a convenience variable.  If it is not a convenience
03372          variable, return and allow normal symbol lookup to occur.  */
03373       ivar = lookup_only_internalvar (variable + 1);
03374       if (ivar == NULL)
03375         /* No internal variable with that name.  Mark the offset
03376            as unknown to allow the name to be looked up as a symbol.  */
03377         offset.sign = LINE_OFFSET_UNKNOWN;
03378       else
03379         {
03380           /* We found a valid variable name.  If it is not an integer,
03381              throw an error.  */
03382           if (!get_internalvar_integer (ivar, &valx))
03383             error (_("Convenience variables used in line "
03384                      "specs must have integer values."));
03385           else
03386             offset.offset = valx;
03387         }
03388     }
03389 
03390   return offset;
03391 }
03392 
03393 
03394 /* A callback used to possibly add a symbol to the results.  */
03395 
03396 static int
03397 collect_symbols (struct symbol *sym, void *data)
03398 {
03399   struct collect_info *info = data;
03400 
03401   /* In list mode, add all matching symbols, regardless of class.
03402      This allows the user to type "list a_global_variable".  */
03403   if (SYMBOL_CLASS (sym) == LOC_BLOCK || info->state->list_mode)
03404     VEC_safe_push (symbolp, info->result.symbols, sym);
03405   return 1; /* Continue iterating.  */
03406 }
03407 
03408 /* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
03409    linespec; return the SAL in RESULT.  */
03410 
03411 static void
03412 minsym_found (struct linespec_state *self, struct objfile *objfile,
03413               struct minimal_symbol *msymbol,
03414               struct symtabs_and_lines *result)
03415 {
03416   struct gdbarch *gdbarch = get_objfile_arch (objfile);
03417   CORE_ADDR pc;
03418   struct symtab_and_line sal;
03419 
03420   sal = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
03421                            (struct obj_section *) 0, 0);
03422   sal.section = SYMBOL_OBJ_SECTION (objfile, msymbol);
03423 
03424   /* The minimal symbol might point to a function descriptor;
03425      resolve it to the actual code address instead.  */
03426   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
03427   if (pc != sal.pc)
03428     sal = find_pc_sect_line (pc, NULL, 0);
03429 
03430   if (self->funfirstline)
03431     skip_prologue_sal (&sal);
03432 
03433   if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
03434     add_sal_to_sals (self, result, &sal, SYMBOL_NATURAL_NAME (msymbol), 0);
03435 }
03436 
03437 /* A helper struct to pass some data through
03438    iterate_over_minimal_symbols.  */
03439 
03440 struct collect_minsyms
03441 {
03442   /* The objfile we're examining.  */
03443   struct objfile *objfile;
03444 
03445   /* The funfirstline setting from the initial call.  */
03446   int funfirstline;
03447 
03448   /* The list_mode setting from the initial call.  */
03449   int list_mode;
03450 
03451   /* The resulting symbols.  */
03452   VEC (bound_minimal_symbol_d) *msyms;
03453 };
03454 
03455 /* A helper function to classify a minimal_symbol_type according to
03456    priority.  */
03457 
03458 static int
03459 classify_mtype (enum minimal_symbol_type t)
03460 {
03461   switch (t)
03462     {
03463     case mst_file_text:
03464     case mst_file_data:
03465     case mst_file_bss:
03466       /* Intermediate priority.  */
03467       return 1;
03468 
03469     case mst_solib_trampoline:
03470       /* Lowest priority.  */
03471       return 2;
03472 
03473     default:
03474       /* Highest priority.  */
03475       return 0;
03476     }
03477 }
03478 
03479 /* Callback for qsort that sorts symbols by priority.  */
03480 
03481 static int
03482 compare_msyms (const void *a, const void *b)
03483 {
03484   const bound_minimal_symbol_d *moa = a;
03485   const bound_minimal_symbol_d *mob = b;
03486   enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
03487   enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
03488 
03489   return classify_mtype (ta) - classify_mtype (tb);
03490 }
03491 
03492 /* Callback for iterate_over_minimal_symbols that adds the symbol to
03493    the result.  */
03494 
03495 static void
03496 add_minsym (struct minimal_symbol *minsym, void *d)
03497 {
03498   struct collect_minsyms *info = d;
03499   bound_minimal_symbol_d mo;
03500 
03501   /* Exclude data symbols when looking for breakpoint locations.   */
03502   if (!info->list_mode)
03503     switch (minsym->type)
03504       {
03505         case mst_slot_got_plt:
03506         case mst_data:
03507         case mst_bss:
03508         case mst_abs:
03509         case mst_file_data:
03510         case mst_file_bss:
03511           {
03512             /* Make sure this minsym is not a function descriptor
03513                before we decide to discard it.  */
03514             struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
03515             CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
03516                                (gdbarch, SYMBOL_VALUE_ADDRESS (minsym),
03517                                 &current_target);
03518 
03519             if (addr == SYMBOL_VALUE_ADDRESS (minsym))
03520               return;
03521           }
03522       }
03523 
03524   mo.minsym = minsym;
03525   mo.objfile = info->objfile;
03526   VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
03527 }
03528 
03529 /* Search minimal symbols in all objfiles for NAME.  If SEARCH_PSPACE
03530    is not NULL, the search is restricted to just that program
03531    space.  */
03532 
03533 static void
03534 search_minsyms_for_name (struct collect_info *info, const char *name,
03535                          struct program_space *search_pspace)
03536 {
03537   struct objfile *objfile;
03538   struct program_space *pspace;
03539 
03540   ALL_PSPACES (pspace)
03541   {
03542     struct collect_minsyms local;
03543     struct cleanup *cleanup;
03544 
03545     if (search_pspace != NULL && search_pspace != pspace)
03546       continue;
03547     if (pspace->executing_startup)
03548       continue;
03549 
03550     set_current_program_space (pspace);
03551 
03552     memset (&local, 0, sizeof (local));
03553     local.funfirstline = info->state->funfirstline;
03554     local.list_mode = info->state->list_mode;
03555 
03556     cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d),
03557                             &local.msyms);
03558 
03559     ALL_OBJFILES (objfile)
03560     {
03561       local.objfile = objfile;
03562       iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
03563     }
03564 
03565     if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
03566       {
03567         int classification;
03568         int ix;
03569         bound_minimal_symbol_d *item;
03570 
03571         qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
03572                VEC_length (bound_minimal_symbol_d, local.msyms),
03573                sizeof (bound_minimal_symbol_d),
03574                compare_msyms);
03575 
03576         /* Now the minsyms are in classification order.  So, we walk
03577            over them and process just the minsyms with the same
03578            classification as the very first minsym in the list.  */
03579         item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
03580         classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
03581 
03582         for (ix = 0;
03583              VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
03584              ++ix)
03585           {
03586             if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
03587               break;
03588 
03589             VEC_safe_push (bound_minimal_symbol_d,
03590                            info->result.minimal_symbols, item);
03591           }
03592       }
03593 
03594     do_cleanups (cleanup);
03595   }
03596 }
03597 
03598 /* A helper function to add all symbols matching NAME to INFO.  If
03599    PSPACE is not NULL, the search is restricted to just that program
03600    space.  */
03601 
03602 static void
03603 add_matching_symbols_to_info (const char *name,
03604                               struct collect_info *info,
03605                               struct program_space *pspace)
03606 {
03607   int ix;
03608   struct symtab *elt;
03609 
03610   for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
03611     {
03612       if (elt == NULL)
03613         {
03614           iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN,
03615                                              collect_symbols, info,
03616                                              pspace, 1);
03617           search_minsyms_for_name (info, name, pspace);
03618         }
03619       else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
03620         {
03621           /* Program spaces that are executing startup should have
03622              been filtered out earlier.  */
03623           gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
03624           set_current_program_space (SYMTAB_PSPACE (elt));
03625           iterate_over_file_blocks (elt, name, VAR_DOMAIN,
03626                                     collect_symbols, info);
03627         }
03628     }
03629 }
03630 
03631 
03632 
03633 /* Now come some functions that are called from multiple places within
03634    decode_line_1.  */
03635 
03636 static int
03637 symbol_to_sal (struct symtab_and_line *result,
03638                int funfirstline, struct symbol *sym)
03639 {
03640   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
03641     {
03642       *result = find_function_start_sal (sym, funfirstline);
03643       return 1;
03644     }
03645   else
03646     {
03647       if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
03648         {
03649           init_sal (result);
03650           result->symtab = SYMBOL_SYMTAB (sym);
03651           result->line = SYMBOL_LINE (sym);
03652           result->pc = SYMBOL_VALUE_ADDRESS (sym);
03653           result->pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
03654           result->explicit_pc = 1;
03655           return 1;
03656         }
03657       else if (funfirstline)
03658         {
03659           /* Nothing.  */
03660         }
03661       else if (SYMBOL_LINE (sym) != 0)
03662         {
03663           /* We know its line number.  */
03664           init_sal (result);
03665           result->symtab = SYMBOL_SYMTAB (sym);
03666           result->line = SYMBOL_LINE (sym);
03667           result->pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
03668           return 1;
03669         }
03670     }
03671 
03672   return 0;
03673 }
03674 
03675 /* See the comment in linespec.h.  */
03676 
03677 void
03678 init_linespec_result (struct linespec_result *lr)
03679 {
03680   memset (lr, 0, sizeof (*lr));
03681 }
03682 
03683 /* See the comment in linespec.h.  */
03684 
03685 void
03686 destroy_linespec_result (struct linespec_result *ls)
03687 {
03688   int i;
03689   struct linespec_sals *lsal;
03690 
03691   xfree (ls->addr_string);
03692   for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
03693     {
03694       xfree (lsal->canonical);
03695       xfree (lsal->sals.sals);
03696     }
03697   VEC_free (linespec_sals, ls->sals);
03698 }
03699 
03700 /* Cleanup function for a linespec_result.  */
03701 
03702 static void
03703 cleanup_linespec_result (void *a)
03704 {
03705   destroy_linespec_result (a);
03706 }
03707 
03708 /* See the comment in linespec.h.  */
03709 
03710 struct cleanup *
03711 make_cleanup_destroy_linespec_result (struct linespec_result *ls)
03712 {
03713   return make_cleanup (cleanup_linespec_result, ls);
03714 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines