Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / linespec.c
CommitLineData
50641945 1/* Parser for linespec for the GNU debugger, GDB.
05ff989b 2
b811d2c2 3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
50641945
FN
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
50641945
FN
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
50641945
FN
19
20#include "defs.h"
21#include "symtab.h"
c5f0f3d0
FN
22#include "frame.h"
23#include "command.h"
50641945
FN
24#include "symfile.h"
25#include "objfiles.h"
0378c332 26#include "source.h"
50641945 27#include "demangle.h"
c5f0f3d0
FN
28#include "value.h"
29#include "completer.h"
015a42b4 30#include "cp-abi.h"
12907978 31#include "cp-support.h"
c38da1af 32#include "parser-defs.h"
fe898f56 33#include "block.h"
d2630e69 34#include "objc-lang.h"
b9362cc7 35#include "linespec.h"
53c5240f 36#include "language.h"
dc67126b
NR
37#include "interps.h"
38#include "mi/mi-cmds.h"
bccdca4a 39#include "target.h"
94af9270 40#include "arch-utils.h"
c00f8484
KS
41#include <ctype.h>
42#include "cli/cli-utils.h"
731971ed 43#include "filenames.h"
f8eba3c6 44#include "ada-lang.h"
39cf75f7 45#include "stack.h"
f00aae0f 46#include "location.h"
268a13a5
TT
47#include "gdbsupport/function-view.h"
48#include "gdbsupport/def-vector.h"
41c1efc6 49#include <algorithm>
f8eba3c6 50
c45ec17c
PA
51/* An enumeration of the various things a user might attempt to
52 complete for a linespec location. */
53
54enum class linespec_complete_what
55{
56 /* Nothing, no possible completion. */
57 NOTHING,
58
59 /* A function/method name. Due to ambiguity between
60
61 (gdb) b source[TAB]
62 source_file.c
63 source_function
64
65 this can also indicate a source filename, iff we haven't seen a
66 separate source filename component, as in "b source.c:function". */
67 FUNCTION,
68
69 /* A label symbol. E.g., break file.c:function:LABEL. */
70 LABEL,
71
72 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
73 EXPRESSION,
74
733d554a 75 /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
c45ec17c
PA
76 E.g., "break func threa<tab>". */
77 KEYWORD,
78};
79
f8eba3c6
TT
80/* An address entry is used to ensure that any given location is only
81 added to the result a single time. It holds an address and the
82 program space from which the address came. */
83
84struct address_entry
85{
86 struct program_space *pspace;
87 CORE_ADDR addr;
88};
89
40e084e1
KS
90/* A linespec. Elements of this structure are filled in by a parser
91 (either parse_linespec or some other function). The structure is
92 then converted into SALs by convert_linespec_to_sals. */
93
94struct linespec
95{
00e52e53 96 /* An explicit location describing the SaLs. */
67994074 97 struct explicit_location explicit_loc;
40e084e1
KS
98
99 /* The list of symtabs to search to which to limit the search. May not
00e52e53
KS
100 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
101 filename), FILE_SYMTABS should contain one single NULL member. This
102 will cause the code to use the default symtab. */
2a908241 103 std::vector<symtab *> *file_symtabs;
40e084e1 104
40e084e1 105 /* A list of matching function symbols and minimal symbols. Both lists
7243d011 106 may be NULL (or empty) if no matching symbols were found. */
7e41c8db 107 std::vector<block_symbol> *function_symbols;
c2a031c5 108 std::vector<bound_minimal_symbol> *minimal_symbols;
40e084e1 109
40e084e1
KS
110 /* A structure of matching label symbols and the corresponding
111 function symbol in which the label was found. Both may be NULL
112 or both must be non-NULL. */
113 struct
114 {
7e41c8db
KS
115 std::vector<block_symbol> *label_symbols;
116 std::vector<block_symbol> *function_symbols;
40e084e1 117 } labels;
40e084e1
KS
118};
119typedef struct linespec *linespec_p;
120
33f448b1
JK
121/* A canonical linespec represented as a symtab-related string.
122
123 Each entry represents the "SYMTAB:SUFFIX" linespec string.
124 SYMTAB can be converted for example by symtab_to_fullname or
125 symtab_to_filename_for_display as needed. */
126
127struct linespec_canonical_name
128{
129 /* Remaining text part of the linespec string. */
130 char *suffix;
131
132 /* If NULL then SUFFIX is the whole linespec string. */
133 struct symtab *symtab;
134};
135
f8eba3c6
TT
136/* An instance of this is used to keep all state while linespec
137 operates. This instance is passed around as a 'this' pointer to
138 the various implementation methods. */
139
140struct linespec_state
141{
40e084e1
KS
142 /* The language in use during linespec processing. */
143 const struct language_defn *language;
144
f8eba3c6
TT
145 /* The program space as seen when the module was entered. */
146 struct program_space *program_space;
147
c2f4122d
PA
148 /* If not NULL, the search is restricted to just this program
149 space. */
150 struct program_space *search_pspace;
151
f8eba3c6
TT
152 /* The default symtab to use, if no other symtab is specified. */
153 struct symtab *default_symtab;
154
155 /* The default line to use. */
156 int default_line;
157
f8eba3c6
TT
158 /* The 'funfirstline' value that was passed in to decode_line_1 or
159 decode_line_full. */
160 int funfirstline;
161
162 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
163 int list_mode;
164
165 /* The 'canonical' value passed to decode_line_full, or NULL. */
166 struct linespec_result *canonical;
167
6c5b2ebe 168 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
33f448b1 169 struct linespec_canonical_name *canonical_names;
f8eba3c6
TT
170
171 /* This is a set of address_entry objects which is used to prevent
172 duplicate symbols from being entered into the result. */
173 htab_t addr_set;
00e52e53
KS
174
175 /* Are we building a linespec? */
176 int is_linespec;
f8eba3c6
TT
177};
178
179/* This is a helper object that is used when collecting symbols into a
180 result. */
181
182struct collect_info
183{
184 /* The linespec object in use. */
185 struct linespec_state *state;
186
40e084e1 187 /* A list of symtabs to which to restrict matches. */
2a908241 188 std::vector<symtab *> *file_symtabs;
40e084e1 189
f8eba3c6 190 /* The result being accumulated. */
40e084e1
KS
191 struct
192 {
7e41c8db 193 std::vector<block_symbol> *symbols;
c2a031c5 194 std::vector<bound_minimal_symbol> *minimal_symbols;
40e084e1 195 } result;
14bc53a8
PA
196
197 /* Possibly add a symbol to the results. */
fcaad03c 198 virtual bool add_symbol (block_symbol *bsym);
f8eba3c6 199};
50641945 200
14bc53a8 201bool
7e41c8db 202collect_info::add_symbol (block_symbol *bsym)
14bc53a8
PA
203{
204 /* In list mode, add all matching symbols, regardless of class.
205 This allows the user to type "list a_global_variable". */
7e41c8db
KS
206 if (SYMBOL_CLASS (bsym->symbol) == LOC_BLOCK || this->state->list_mode)
207 this->result.symbols->push_back (*bsym);
14bc53a8
PA
208
209 /* Continue iterating. */
210 return true;
211}
212
fcaad03c
KS
213/* Custom collect_info for symbol_searcher. */
214
215struct symbol_searcher_collect_info
216 : collect_info
217{
218 bool add_symbol (block_symbol *bsym) override
219 {
220 /* Add everything. */
221 this->result.symbols->push_back (*bsym);
222
223 /* Continue iterating. */
224 return true;
225 }
226};
227
40e084e1 228/* Token types */
50641945 229
40e084e1
KS
230enum ls_token_type
231{
232 /* A keyword */
233 LSTOKEN_KEYWORD = 0,
44fe14ab 234
40e084e1
KS
235 /* A colon "separator" */
236 LSTOKEN_COLON,
44fe14ab 237
40e084e1
KS
238 /* A string */
239 LSTOKEN_STRING,
0960f083 240
40e084e1
KS
241 /* A number */
242 LSTOKEN_NUMBER,
243
244 /* A comma */
245 LSTOKEN_COMMA,
246
247 /* EOI (end of input) */
248 LSTOKEN_EOI,
249
250 /* Consumed token */
251 LSTOKEN_CONSUMED
252};
253typedef enum ls_token_type linespec_token_type;
254
c6756f62
PA
255/* List of keywords. This is NULL-terminated so that it can be used
256 as enum completer. */
733d554a 257const char * const linespec_keywords[] = { "if", "thread", "task", "-force-condition", NULL };
0578b14e 258#define IF_KEYWORD_INDEX 0
733d554a 259#define FORCE_KEYWORD_INDEX 3
40e084e1
KS
260
261/* A token of the linespec lexer */
262
f99b5177 263struct linespec_token
40e084e1
KS
264{
265 /* The type of the token */
266 linespec_token_type type;
267
268 /* Data for the token */
269 union
270 {
271 /* A string, given as a stoken */
272 struct stoken string;
273
274 /* A keyword */
275 const char *keyword;
276 } data;
277};
40e084e1
KS
278
279#define LS_TOKEN_STOKEN(TOK) (TOK).data.string
280#define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
281
282/* An instance of the linespec parser. */
283
3a6ae42d 284struct linespec_parser
40e084e1 285{
3a6ae42d
TT
286 linespec_parser (int flags, const struct language_defn *language,
287 struct program_space *search_pspace,
288 struct symtab *default_symtab,
289 int default_line,
290 struct linespec_result *canonical);
291
292 ~linespec_parser ();
293
294 DISABLE_COPY_AND_ASSIGN (linespec_parser);
295
40e084e1
KS
296 /* Lexer internal data */
297 struct
298 {
299 /* Save head of input stream. */
d7561cbb 300 const char *saved_arg;
d2630e69 301
40e084e1 302 /* Head of the input stream. */
f00aae0f
KS
303 const char *stream;
304#define PARSER_STREAM(P) ((P)->lexer.stream)
614b3b14 305
40e084e1
KS
306 /* The current token. */
307 linespec_token current;
3a6ae42d 308 } lexer {};
93d91629 309
40e084e1 310 /* Is the entire linespec quote-enclosed? */
3a6ae42d 311 int is_quote_enclosed = 0;
40e084e1
KS
312
313 /* The state of the parse. */
3a6ae42d 314 struct linespec_state state {};
40e084e1 315#define PARSER_STATE(PPTR) (&(PPTR)->state)
4224873a 316
40e084e1 317 /* The result of the parse. */
3a6ae42d 318 struct linespec result {};
40e084e1 319#define PARSER_RESULT(PPTR) (&(PPTR)->result)
c45ec17c
PA
320
321 /* What the parser believes the current word point should complete
322 to. */
3a6ae42d 323 linespec_complete_what complete_what = linespec_complete_what::NOTHING;
c45ec17c
PA
324
325 /* The completion word point. The parser advances this as it skips
326 tokens. At some point the input string will end or parsing will
327 fail, and then we attempt completion at the captured completion
328 word point, interpreting the string at completion_word as
329 COMPLETE_WHAT. */
3a6ae42d 330 const char *completion_word = nullptr;
c45ec17c
PA
331
332 /* If the current token was a quoted string, then this is the
333 quoting character (either " or '). */
3a6ae42d 334 int completion_quote_char = 0;
c45ec17c
PA
335
336 /* If the current token was a quoted string, then this points at the
337 end of the quoted string. */
3a6ae42d 338 const char *completion_quote_end = nullptr;
c45ec17c
PA
339
340 /* If parsing for completion, then this points at the completion
341 tracker. Otherwise, this is NULL. */
3a6ae42d 342 struct completion_tracker *completion_tracker = nullptr;
40e084e1 343};
50641945 344
00e52e53
KS
345/* A convenience macro for accessing the explicit location result of
346 the parser. */
67994074 347#define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
00e52e53 348
40e084e1 349/* Prototypes for local functions. */
50641945 350
14bc53a8 351static void iterate_over_file_blocks
b5ec771e
PA
352 (struct symtab *symtab, const lookup_name_info &name,
353 domain_enum domain,
14bc53a8 354 gdb::function_view<symbol_found_callback_ftype> callback);
4eeaa230 355
40e084e1
KS
356static void initialize_defaults (struct symtab **default_symtab,
357 int *default_line);
50641945 358
a06efdd6 359CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
aee8d8ba 360
6c5b2ebe
PA
361static std::vector<symtab_and_line> decode_objc (struct linespec_state *self,
362 linespec_p ls,
363 const char *arg);
aee8d8ba 364
4717cec4 365static std::vector<symtab *> symtabs_from_filename
2a908241 366 (const char *, struct program_space *pspace);
50641945 367
7e41c8db
KS
368static std::vector<block_symbol> *find_label_symbols
369 (struct linespec_state *self, std::vector<block_symbol> *function_symbols,
370 std::vector<block_symbol> *label_funcs_ret, const char *name,
7243d011 371 bool completion_mode = false);
50641945 372
b1ae631a 373static void find_linespec_symbols (struct linespec_state *self,
2a908241 374 std::vector<symtab *> *file_symtabs,
b1ae631a 375 const char *name,
a20714ff 376 symbol_name_match_type name_match_type,
7e41c8db 377 std::vector<block_symbol> *symbols,
c2a031c5 378 std::vector<bound_minimal_symbol> *minsyms);
f8eba3c6 379
40e084e1
KS
380static struct line_offset
381 linespec_parse_variable (struct linespec_state *self,
382 const char *variable);
889f28e2 383
f8eba3c6
TT
384static int symbol_to_sal (struct symtab_and_line *result,
385 int funfirstline, struct symbol *sym);
50641945 386
f8eba3c6 387static void add_matching_symbols_to_info (const char *name,
b5ec771e 388 symbol_name_match_type name_match_type,
56d87ef7 389 enum search_domain search_domain,
f8eba3c6
TT
390 struct collect_info *info,
391 struct program_space *pspace);
f3c39e76 392
9b2f8581
TT
393static void add_all_symbol_names_from_pspace
394 (struct collect_info *info, struct program_space *pspace,
395 const std::vector<const char *> &names, enum search_domain search_domain);
9ef07c8c 396
4717cec4 397static std::vector<symtab *>
c2f4122d
PA
398 collect_symtabs_from_filename (const char *file,
399 struct program_space *pspace);
84fba31b 400
6c5b2ebe
PA
401static std::vector<symtab_and_line> decode_digits_ordinary
402 (struct linespec_state *self,
403 linespec_p ls,
404 int line,
405 linetable_entry **best_entry);
14e91ac5 406
6c5b2ebe
PA
407static std::vector<symtab_and_line> decode_digits_list_mode
408 (struct linespec_state *self,
409 linespec_p ls,
410 struct symtab_and_line val);
0f5238ed 411
40e084e1
KS
412static void minsym_found (struct linespec_state *self, struct objfile *objfile,
413 struct minimal_symbol *msymbol,
6c5b2ebe 414 std::vector<symtab_and_line> *result);
bca02a8a 415
7e41c8db 416static bool compare_symbols (const block_symbol &a, const block_symbol &b);
413dad4d 417
c2a031c5
KS
418static bool compare_msymbols (const bound_minimal_symbol &a,
419 const bound_minimal_symbol &b);
413dad4d 420
40e084e1
KS
421/* Permitted quote characters for the parser. This is different from the
422 completer's quote characters to allow backward compatibility with the
423 previous parser. */
db92ac45 424static const char linespec_quote_characters[] = "\"\'";
f8eba3c6 425
40e084e1
KS
426/* Lexer functions. */
427
428/* Lex a number from the input in PARSER. This only supports
dd3818c8
KS
429 decimal numbers.
430
d7cbec71 431 Return true if input is decimal numbers. Return false if not. */
40e084e1 432
d7cbec71
HZ
433static int
434linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
40e084e1 435{
d7cbec71
HZ
436 tokenp->type = LSTOKEN_NUMBER;
437 LS_TOKEN_STOKEN (*tokenp).length = 0;
438 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
40e084e1
KS
439
440 /* Keep any sign at the start of the stream. */
441 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
442 {
d7cbec71 443 ++LS_TOKEN_STOKEN (*tokenp).length;
40e084e1
KS
444 ++(PARSER_STREAM (parser));
445 }
446
447 while (isdigit (*PARSER_STREAM (parser)))
448 {
d7cbec71 449 ++LS_TOKEN_STOKEN (*tokenp).length;
40e084e1 450 ++(PARSER_STREAM (parser));
f8eba3c6 451 }
40e084e1 452
dd3818c8 453 /* If the next character in the input buffer is not a space, comma,
eff9c3e6 454 quote, or colon, this input does not represent a number. */
dd3818c8
KS
455 if (*PARSER_STREAM (parser) != '\0'
456 && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
eff9c3e6
KS
457 && *PARSER_STREAM (parser) != ':'
458 && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
d7cbec71
HZ
459 {
460 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
461 return 0;
462 }
463
464 return 1;
f8eba3c6
TT
465}
466
32b40af9 467/* See linespec.h. */
f8eba3c6 468
0578b14e 469const char *
40e084e1 470linespec_lexer_lex_keyword (const char *p)
f8eba3c6 471{
40e084e1 472 int i;
f8eba3c6 473
40e084e1
KS
474 if (p != NULL)
475 {
c6756f62 476 for (i = 0; linespec_keywords[i] != NULL; ++i)
40e084e1
KS
477 {
478 int len = strlen (linespec_keywords[i]);
479
5759831a
TBA
480 /* If P begins with
481
482 - "thread" or "task" and the next character is
483 whitespace, we may have found a keyword. It is only a
484 keyword if it is not followed by another keyword.
485
486 - "-force-condition", the next character may be EOF
487 since this keyword does not take any arguments. Otherwise,
488 it should be followed by a keyword.
489
490 - "if", ALWAYS stop the lexer, since it is not possible to
491 predict what is going to appear in the condition, which can
492 only be parsed after SaLs have been found. */
493 if (strncmp (p, linespec_keywords[i], len) == 0)
0578b14e
KS
494 {
495 int j;
496
5759831a
TBA
497 if (i == FORCE_KEYWORD_INDEX && p[len] == '\0')
498 return linespec_keywords[i];
499
500 if (!isspace (p[len]))
501 continue;
502
733d554a
TBA
503 if (i == FORCE_KEYWORD_INDEX)
504 {
505 p += len;
506 p = skip_spaces (p);
5759831a
TBA
507 for (j = 0; linespec_keywords[j] != NULL; ++j)
508 {
509 int nextlen = strlen (linespec_keywords[j]);
733d554a 510
5759831a
TBA
511 if (strncmp (p, linespec_keywords[j], nextlen) == 0
512 && isspace (p[nextlen]))
513 return linespec_keywords[i];
514 }
515 }
733d554a 516 else if (i != IF_KEYWORD_INDEX)
0578b14e 517 {
5759831a 518 /* We matched a "thread" or "task". */
0578b14e 519 p += len;
f1735a53 520 p = skip_spaces (p);
c6756f62 521 for (j = 0; linespec_keywords[j] != NULL; ++j)
0578b14e
KS
522 {
523 int nextlen = strlen (linespec_keywords[j]);
524
525 if (strncmp (p, linespec_keywords[j], nextlen) == 0
526 && isspace (p[nextlen]))
527 return NULL;
528 }
529 }
530
531 return linespec_keywords[i];
532 }
40e084e1
KS
533 }
534 }
535
536 return NULL;
f8eba3c6
TT
537}
538
87f0e720 539/* See description in linespec.h. */
f8eba3c6 540
87f0e720 541int
40e084e1 542is_ada_operator (const char *string)
f8eba3c6 543{
40e084e1 544 const struct ada_opname_map *mapping;
f8eba3c6 545
40e084e1
KS
546 for (mapping = ada_opname_table;
547 mapping->encoded != NULL
61012eef 548 && !startswith (string, mapping->decoded); ++mapping)
40e084e1
KS
549 ;
550
551 return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
f8eba3c6
TT
552}
553
40e084e1
KS
554/* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
555 the location of QUOTE_CHAR, or NULL if not found. */
f8eba3c6 556
40e084e1
KS
557static const char *
558skip_quote_char (const char *string, char quote_char)
f8eba3c6 559{
40e084e1 560 const char *p, *last;
f8eba3c6 561
40e084e1
KS
562 p = last = find_toplevel_char (string, quote_char);
563 while (p && *p != '\0' && *p != ':')
564 {
565 p = find_toplevel_char (p, quote_char);
566 if (p != NULL)
567 last = p++;
568 }
f8eba3c6 569
40e084e1 570 return last;
f8eba3c6 571}
50641945 572
40e084e1
KS
573/* Make a writable copy of the string given in TOKEN, trimming
574 any trailing whitespace. */
50641945 575
a5b5adf5 576static gdb::unique_xmalloc_ptr<char>
40e084e1 577copy_token_string (linespec_token token)
50641945 578{
a5b5adf5 579 const char *str, *s;
e0881a8e 580
40e084e1 581 if (token.type == LSTOKEN_KEYWORD)
b02f78f9 582 return make_unique_xstrdup (LS_TOKEN_KEYWORD (token));
255e7dbf 583
a5b5adf5 584 str = LS_TOKEN_STOKEN (token).ptr;
40e084e1 585 s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
e0881a8e 586
a5b5adf5 587 return gdb::unique_xmalloc_ptr<char> (savestring (str, s - str));
40e084e1 588}
255e7dbf 589
40e084e1 590/* Does P represent the end of a quote-enclosed linespec? */
f3a5f1de 591
40e084e1
KS
592static int
593is_closing_quote_enclosed (const char *p)
594{
595 if (strchr (linespec_quote_characters, *p))
596 ++p;
597 p = skip_spaces ((char *) p);
598 return (*p == '\0' || linespec_lexer_lex_keyword (p));
50641945
FN
599}
600
40e084e1
KS
601/* Find the end of the parameter list that starts with *INPUT.
602 This helper function assists with lexing string segments
603 which might contain valid (non-terminating) commas. */
481860b3 604
d7561cbb
KS
605static const char *
606find_parameter_list_end (const char *input)
481860b3 607{
40e084e1
KS
608 char end_char, start_char;
609 int depth;
d7561cbb 610 const char *p;
481860b3 611
40e084e1
KS
612 start_char = *input;
613 if (start_char == '(')
614 end_char = ')';
615 else if (start_char == '<')
616 end_char = '>';
617 else
618 return NULL;
481860b3 619
40e084e1
KS
620 p = input;
621 depth = 0;
622 while (*p)
481860b3 623 {
40e084e1
KS
624 if (*p == start_char)
625 ++depth;
626 else if (*p == end_char)
627 {
628 if (--depth == 0)
629 {
630 ++p;
631 break;
632 }
633 }
634 ++p;
481860b3 635 }
40e084e1
KS
636
637 return p;
481860b3
GB
638}
639
c45ec17c
PA
640/* If the [STRING, STRING_LEN) string ends with what looks like a
641 keyword, return the keyword start offset in STRING. Return -1
642 otherwise. */
643
644static size_t
645string_find_incomplete_keyword_at_end (const char * const *keywords,
646 const char *string, size_t string_len)
647{
648 const char *end = string + string_len;
649 const char *p = end;
650
651 while (p > string && *p != ' ')
652 --p;
653 if (p > string)
654 {
655 p++;
656 size_t len = end - p;
657 for (size_t i = 0; keywords[i] != NULL; ++i)
658 if (strncmp (keywords[i], p, len) == 0)
659 return p - string;
660 }
661
662 return -1;
663}
74ccd7f5 664
40e084e1
KS
665/* Lex a string from the input in PARSER. */
666
667static linespec_token
668linespec_lexer_lex_string (linespec_parser *parser)
74ccd7f5 669{
40e084e1 670 linespec_token token;
d7561cbb 671 const char *start = PARSER_STREAM (parser);
74ccd7f5 672
40e084e1 673 token.type = LSTOKEN_STRING;
74ccd7f5 674
40e084e1
KS
675 /* If the input stream starts with a quote character, skip to the next
676 quote character, regardless of the content. */
677 if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
678 {
679 const char *end;
680 char quote_char = *PARSER_STREAM (parser);
50641945 681
40e084e1
KS
682 /* Special case: Ada operators. */
683 if (PARSER_STATE (parser)->language->la_language == language_ada
684 && quote_char == '\"')
685 {
686 int len = is_ada_operator (PARSER_STREAM (parser));
50641945 687
40e084e1
KS
688 if (len != 0)
689 {
690 /* The input is an Ada operator. Return the quoted string
691 as-is. */
692 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
693 LS_TOKEN_STOKEN (token).length = len;
694 PARSER_STREAM (parser) += len;
695 return token;
696 }
f8eba3c6 697
40e084e1
KS
698 /* The input does not represent an Ada operator -- fall through
699 to normal quoted string handling. */
700 }
f8eba3c6 701
40e084e1
KS
702 /* Skip past the beginning quote. */
703 ++(PARSER_STREAM (parser));
74ccd7f5 704
40e084e1
KS
705 /* Mark the start of the string. */
706 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
f8eba3c6 707
40e084e1
KS
708 /* Skip to the ending quote. */
709 end = skip_quote_char (PARSER_STREAM (parser), quote_char);
710
c45ec17c
PA
711 /* This helps the completer mode decide whether we have a
712 complete string. */
713 parser->completion_quote_char = quote_char;
714 parser->completion_quote_end = end;
40e084e1 715
c45ec17c
PA
716 /* Error if the input did not terminate properly, unless in
717 completion mode. */
718 if (end == NULL)
719 {
720 if (parser->completion_tracker == NULL)
721 error (_("unmatched quote"));
722
723 /* In completion mode, we'll try to complete the incomplete
724 token. */
725 token.type = LSTOKEN_STRING;
726 while (*PARSER_STREAM (parser) != '\0')
727 PARSER_STREAM (parser)++;
728 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 1 - start;
729 }
730 else
731 {
732 /* Skip over the ending quote and mark the length of the string. */
733 PARSER_STREAM (parser) = (char *) ++end;
734 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
735 }
40e084e1
KS
736 }
737 else
738 {
d7561cbb 739 const char *p;
40e084e1
KS
740
741 /* Otherwise, only identifier characters are permitted.
742 Spaces are the exception. In general, we keep spaces,
743 but only if the next characters in the input do not resolve
744 to one of the keywords.
745
746 This allows users to forgo quoting CV-qualifiers, template arguments,
747 and similar common language constructs. */
748
749 while (1)
750 {
751 if (isspace (*PARSER_STREAM (parser)))
752 {
f1735a53 753 p = skip_spaces (PARSER_STREAM (parser));
7c09e5a0
DE
754 /* When we get here we know we've found something followed by
755 a space (we skip over parens and templates below).
756 So if we find a keyword now, we know it is a keyword and not,
757 say, a function name. */
40e084e1
KS
758 if (linespec_lexer_lex_keyword (p) != NULL)
759 {
760 LS_TOKEN_STOKEN (token).ptr = start;
761 LS_TOKEN_STOKEN (token).length
762 = PARSER_STREAM (parser) - start;
763 return token;
764 }
765
766 /* Advance past the whitespace. */
767 PARSER_STREAM (parser) = p;
768 }
769
770 /* If the next character is EOI or (single) ':', the
771 string is complete; return the token. */
772 if (*PARSER_STREAM (parser) == 0)
773 {
774 LS_TOKEN_STOKEN (token).ptr = start;
775 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
776 return token;
777 }
778 else if (PARSER_STREAM (parser)[0] == ':')
779 {
780 /* Do not tokenize the C++ scope operator. */
781 if (PARSER_STREAM (parser)[1] == ':')
782 ++(PARSER_STREAM (parser));
783
bd69330d
PA
784 /* Do not tokenize ABI tags such as "[abi:cxx11]". */
785 else if (PARSER_STREAM (parser) - start > 4
786 && startswith (PARSER_STREAM (parser) - 4, "[abi"))
f19e22e9
TV
787 {
788 /* Nothing. */
789 }
bd69330d 790
40e084e1
KS
791 /* Do not tokenify if the input length so far is one
792 (i.e, a single-letter drive name) and the next character
793 is a directory separator. This allows Windows-style
794 paths to be recognized as filenames without quoting it. */
795 else if ((PARSER_STREAM (parser) - start) != 1
796 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
797 {
798 LS_TOKEN_STOKEN (token).ptr = start;
799 LS_TOKEN_STOKEN (token).length
800 = PARSER_STREAM (parser) - start;
801 return token;
802 }
803 }
804 /* Special case: permit quote-enclosed linespecs. */
805 else if (parser->is_quote_enclosed
806 && strchr (linespec_quote_characters,
807 *PARSER_STREAM (parser))
808 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
809 {
810 LS_TOKEN_STOKEN (token).ptr = start;
811 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
812 return token;
813 }
814 /* Because commas may terminate a linespec and appear in
815 the middle of valid string input, special cases for
816 '<' and '(' are necessary. */
817 else if (*PARSER_STREAM (parser) == '<'
818 || *PARSER_STREAM (parser) == '(')
819 {
be966d42
PA
820 /* Don't interpret 'operator<' / 'operator<<' as a
821 template parameter list though. */
822 if (*PARSER_STREAM (parser) == '<'
823 && (PARSER_STATE (parser)->language->la_language
824 == language_cplus)
825 && (PARSER_STREAM (parser) - start) >= CP_OPERATOR_LEN)
826 {
b926417a 827 const char *op = PARSER_STREAM (parser);
be966d42 828
b926417a
TT
829 while (op > start && isspace (op[-1]))
830 op--;
831 if (op - start >= CP_OPERATOR_LEN)
be966d42 832 {
b926417a
TT
833 op -= CP_OPERATOR_LEN;
834 if (strncmp (op, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
835 && (op == start
836 || !(isalnum (op[-1]) || op[-1] == '_')))
be966d42
PA
837 {
838 /* This is an operator name. Keep going. */
839 ++(PARSER_STREAM (parser));
840 if (*PARSER_STREAM (parser) == '<')
841 ++(PARSER_STREAM (parser));
842 continue;
843 }
844 }
845 }
846
b926417a
TT
847 const char *end = find_parameter_list_end (PARSER_STREAM (parser));
848 PARSER_STREAM (parser) = end;
40e084e1 849
be966d42
PA
850 /* Don't loop around to the normal \0 case above because
851 we don't want to misinterpret a potential keyword at
852 the end of the token when the string isn't
853 "()<>"-balanced. This handles "b
854 function(thread<tab>" in completion mode. */
b926417a 855 if (*end == '\0')
40e084e1 856 {
be966d42
PA
857 LS_TOKEN_STOKEN (token).ptr = start;
858 LS_TOKEN_STOKEN (token).length
859 = PARSER_STREAM (parser) - start;
860 return token;
40e084e1 861 }
be966d42
PA
862 else
863 continue;
40e084e1
KS
864 }
865 /* Commas are terminators, but not if they are part of an
866 operator name. */
867 else if (*PARSER_STREAM (parser) == ',')
868 {
869 if ((PARSER_STATE (parser)->language->la_language
870 == language_cplus)
8090b426 871 && (PARSER_STREAM (parser) - start) > CP_OPERATOR_LEN)
40e084e1 872 {
b926417a 873 const char *op = strstr (start, CP_OPERATOR_STR);
40e084e1 874
b926417a 875 if (op != NULL && is_operator_name (op))
40e084e1
KS
876 {
877 /* This is an operator name. Keep going. */
878 ++(PARSER_STREAM (parser));
879 continue;
880 }
881 }
882
883 /* Comma terminates the string. */
884 LS_TOKEN_STOKEN (token).ptr = start;
885 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
886 return token;
887 }
888
889 /* Advance the stream. */
f19e22e9 890 gdb_assert (*(PARSER_STREAM (parser)) != '\0');
40e084e1
KS
891 ++(PARSER_STREAM (parser));
892 }
893 }
894
895 return token;
896}
897
898/* Lex a single linespec token from PARSER. */
899
900static linespec_token
901linespec_lexer_lex_one (linespec_parser *parser)
902{
903 const char *keyword;
904
905 if (parser->lexer.current.type == LSTOKEN_CONSUMED)
906 {
907 /* Skip any whitespace. */
f1735a53 908 PARSER_STREAM (parser) = skip_spaces (PARSER_STREAM (parser));
40e084e1 909
7c09e5a0 910 /* Check for a keyword, they end the linespec. */
0578b14e 911 keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
40e084e1
KS
912 if (keyword != NULL)
913 {
914 parser->lexer.current.type = LSTOKEN_KEYWORD;
915 LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
0578b14e
KS
916 /* We do not advance the stream here intentionally:
917 we would like lexing to stop when a keyword is seen.
918
919 PARSER_STREAM (parser) += strlen (keyword); */
920
40e084e1
KS
921 return parser->lexer.current;
922 }
923
924 /* Handle other tokens. */
925 switch (*PARSER_STREAM (parser))
926 {
927 case 0:
928 parser->lexer.current.type = LSTOKEN_EOI;
929 break;
930
931 case '+': case '-':
932 case '0': case '1': case '2': case '3': case '4':
dda83cd7
SM
933 case '5': case '6': case '7': case '8': case '9':
934 if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
d7cbec71 935 parser->lexer.current = linespec_lexer_lex_string (parser);
dda83cd7 936 break;
40e084e1
KS
937
938 case ':':
939 /* If we have a scope operator, lex the input as a string.
940 Otherwise, return LSTOKEN_COLON. */
941 if (PARSER_STREAM (parser)[1] == ':')
942 parser->lexer.current = linespec_lexer_lex_string (parser);
943 else
944 {
945 parser->lexer.current.type = LSTOKEN_COLON;
946 ++(PARSER_STREAM (parser));
947 }
948 break;
949
950 case '\'': case '\"':
951 /* Special case: permit quote-enclosed linespecs. */
952 if (parser->is_quote_enclosed
953 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
954 {
955 ++(PARSER_STREAM (parser));
956 parser->lexer.current.type = LSTOKEN_EOI;
957 }
958 else
959 parser->lexer.current = linespec_lexer_lex_string (parser);
960 break;
961
962 case ',':
963 parser->lexer.current.type = LSTOKEN_COMMA;
964 LS_TOKEN_STOKEN (parser->lexer.current).ptr
965 = PARSER_STREAM (parser);
966 LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
967 ++(PARSER_STREAM (parser));
968 break;
969
970 default:
971 /* If the input is not a number, it must be a string.
972 [Keywords were already considered above.] */
973 parser->lexer.current = linespec_lexer_lex_string (parser);
974 break;
975 }
976 }
977
978 return parser->lexer.current;
979}
980
981/* Consume the current token and return the next token in PARSER's
c45ec17c
PA
982 input stream. Also advance the completion word for completion
983 mode. */
40e084e1
KS
984
985static linespec_token
986linespec_lexer_consume_token (linespec_parser *parser)
987{
c45ec17c
PA
988 gdb_assert (parser->lexer.current.type != LSTOKEN_EOI);
989
990 bool advance_word = (parser->lexer.current.type != LSTOKEN_STRING
991 || *PARSER_STREAM (parser) != '\0');
992
993 /* If we're moving past a string to some other token, it must be the
994 quote was terminated. */
995 if (parser->completion_quote_char)
996 {
997 gdb_assert (parser->lexer.current.type == LSTOKEN_STRING);
998
999 /* If the string was the last (non-EOI) token, we're past the
1000 quote, but remember that for later. */
1001 if (*PARSER_STREAM (parser) != '\0')
1002 {
1003 parser->completion_quote_char = '\0';
1004 parser->completion_quote_end = NULL;;
1005 }
1006 }
1007
40e084e1 1008 parser->lexer.current.type = LSTOKEN_CONSUMED;
c45ec17c
PA
1009 linespec_lexer_lex_one (parser);
1010
1011 if (parser->lexer.current.type == LSTOKEN_STRING)
1012 {
1013 /* Advance the completion word past a potential initial
1014 quote-char. */
1015 parser->completion_word = LS_TOKEN_STOKEN (parser->lexer.current).ptr;
1016 }
1017 else if (advance_word)
1018 {
1019 /* Advance the completion word past any whitespace. */
1020 parser->completion_word = PARSER_STREAM (parser);
1021 }
1022
1023 return parser->lexer.current;
40e084e1
KS
1024}
1025
1026/* Return the next token without consuming the current token. */
1027
1028static linespec_token
1029linespec_lexer_peek_token (linespec_parser *parser)
1030{
1031 linespec_token next;
d7561cbb 1032 const char *saved_stream = PARSER_STREAM (parser);
40e084e1 1033 linespec_token saved_token = parser->lexer.current;
c45ec17c
PA
1034 int saved_completion_quote_char = parser->completion_quote_char;
1035 const char *saved_completion_quote_end = parser->completion_quote_end;
1036 const char *saved_completion_word = parser->completion_word;
40e084e1
KS
1037
1038 next = linespec_lexer_consume_token (parser);
1039 PARSER_STREAM (parser) = saved_stream;
1040 parser->lexer.current = saved_token;
c45ec17c
PA
1041 parser->completion_quote_char = saved_completion_quote_char;
1042 parser->completion_quote_end = saved_completion_quote_end;
1043 parser->completion_word = saved_completion_word;
40e084e1
KS
1044 return next;
1045}
1046
1047/* Helper functions. */
1048
40e084e1
KS
1049/* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1050 the new sal, if needed. If not NULL, SYMNAME is the name of the
66f1999b
KS
1051 symbol to use when constructing the new canonical name.
1052
1053 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1054 canonical name for the SAL. */
40e084e1
KS
1055
1056static void
1057add_sal_to_sals (struct linespec_state *self,
6c5b2ebe 1058 std::vector<symtab_and_line> *sals,
40e084e1 1059 struct symtab_and_line *sal,
66f1999b 1060 const char *symname, int literal_canonical)
40e084e1 1061{
6c5b2ebe 1062 sals->push_back (*sal);
40e084e1
KS
1063
1064 if (self->canonical)
1065 {
33f448b1 1066 struct linespec_canonical_name *canonical;
40e084e1 1067
224c3ddb 1068 self->canonical_names = XRESIZEVEC (struct linespec_canonical_name,
6c5b2ebe
PA
1069 self->canonical_names,
1070 sals->size ());
1071 canonical = &self->canonical_names[sals->size () - 1];
4e04028d 1072 if (!literal_canonical && sal->symtab)
40e084e1 1073 {
df140a0b
TS
1074 symtab_to_fullname (sal->symtab);
1075
40e084e1
KS
1076 /* Note that the filter doesn't have to be a valid linespec
1077 input. We only apply the ":LINE" treatment to Ada for
1078 the time being. */
1079 if (symname != NULL && sal->line != 0
1080 && self->language->la_language == language_ada)
33f448b1 1081 canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
40e084e1 1082 else if (symname != NULL)
33f448b1 1083 canonical->suffix = xstrdup (symname);
40e084e1 1084 else
33f448b1
JK
1085 canonical->suffix = xstrprintf ("%d", sal->line);
1086 canonical->symtab = sal->symtab;
1087 }
1088 else
1089 {
1090 if (symname != NULL)
1091 canonical->suffix = xstrdup (symname);
1092 else
e617b069 1093 canonical->suffix = xstrdup ("<unknown>");
33f448b1 1094 canonical->symtab = NULL;
40e084e1 1095 }
40e084e1
KS
1096 }
1097}
1098
1099/* A hash function for address_entry. */
1100
1101static hashval_t
1102hash_address_entry (const void *p)
1103{
9a3c8263 1104 const struct address_entry *aep = (const struct address_entry *) p;
40e084e1
KS
1105 hashval_t hash;
1106
1107 hash = iterative_hash_object (aep->pspace, 0);
1108 return iterative_hash_object (aep->addr, hash);
1109}
1110
1111/* An equality function for address_entry. */
1112
1113static int
1114eq_address_entry (const void *a, const void *b)
1115{
9a3c8263
SM
1116 const struct address_entry *aea = (const struct address_entry *) a;
1117 const struct address_entry *aeb = (const struct address_entry *) b;
40e084e1
KS
1118
1119 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
1120}
1121
1122/* Check whether the address, represented by PSPACE and ADDR, is
1123 already in the set. If so, return 0. Otherwise, add it and return
1124 1. */
1125
1126static int
1127maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
1128{
1129 struct address_entry e, *p;
1130 void **slot;
1131
1132 e.pspace = pspace;
1133 e.addr = addr;
1134 slot = htab_find_slot (set, &e, INSERT);
1135 if (*slot)
1136 return 0;
1137
1138 p = XNEW (struct address_entry);
1139 memcpy (p, &e, sizeof (struct address_entry));
1140 *slot = p;
1141
1142 return 1;
1143}
1144
40e084e1
KS
1145/* A helper that walks over all matching symtabs in all objfiles and
1146 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1147 not NULL, then the search is restricted to just that program
14bc53a8 1148 space. If INCLUDE_INLINE is true then symbols representing
40e084e1
KS
1149 inlined instances of functions will be included in the result. */
1150
1151static void
14bc53a8 1152iterate_over_all_matching_symtabs
b5ec771e
PA
1153 (struct linespec_state *state,
1154 const lookup_name_info &lookup_name,
1155 const domain_enum name_domain,
56d87ef7 1156 enum search_domain search_domain,
14bc53a8
PA
1157 struct program_space *search_pspace, bool include_inline,
1158 gdb::function_view<symbol_found_callback_ftype> callback)
40e084e1 1159{
94c93c35
TT
1160 for (struct program_space *pspace : program_spaces)
1161 {
1162 if (search_pspace != NULL && search_pspace != pspace)
1163 continue;
1164 if (pspace->executing_startup)
1165 continue;
f8eba3c6 1166
94c93c35 1167 set_current_program_space (pspace);
50641945 1168
94c93c35
TT
1169 for (objfile *objfile : current_program_space->objfiles ())
1170 {
1171 if (objfile->sf)
1172 objfile->sf->qf->expand_symtabs_matching (objfile,
1173 NULL,
1174 &lookup_name,
1175 NULL, NULL,
1176 search_domain);
1177
1178 for (compunit_symtab *cu : objfile->compunits ())
1179 {
1180 struct symtab *symtab = COMPUNIT_FILETABS (cu);
43f3e411 1181
94c93c35
TT
1182 iterate_over_file_blocks (symtab, lookup_name, name_domain,
1183 callback);
481860b3 1184
94c93c35
TT
1185 if (include_inline)
1186 {
1187 const struct block *block;
1188 int i;
481860b3 1189
94c93c35
TT
1190 for (i = FIRST_LOCAL_BLOCK;
1191 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
1192 i++)
1193 {
1194 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
4009ee92 1195 state->language->iterate_over_symbols
94c93c35
TT
1196 (block, lookup_name, name_domain,
1197 [&] (block_symbol *bsym)
1198 {
1199 /* Restrict calls to CALLBACK to symbols
1200 representing inline symbols only. */
1201 if (SYMBOL_INLINED (bsym->symbol))
1202 return callback (bsym);
1203 return true;
1204 });
1205 }
1206 }
1207 }
1208 }
1209 }
50641945
FN
1210}
1211
4eeaa230
DE
1212/* Returns the block to be used for symbol searches from
1213 the current location. */
e8eb7bc5 1214
3977b71f 1215static const struct block *
e482a1a7 1216get_current_search_block (void)
e8eb7bc5 1217{
4eeaa230
DE
1218 /* get_selected_block can change the current language when there is
1219 no selected frame yet. */
9bb9b2f9
TT
1220 scoped_restore_current_language save_language;
1221 return get_selected_block (0);
e8eb7bc5
KS
1222}
1223
4eeaa230
DE
1224/* Iterate over static and global blocks. */
1225
1226static void
14bc53a8 1227iterate_over_file_blocks
b5ec771e
PA
1228 (struct symtab *symtab, const lookup_name_info &name,
1229 domain_enum domain, gdb::function_view<symbol_found_callback_ftype> callback)
4eeaa230 1230{
582942f4 1231 const struct block *block;
4eeaa230 1232
439247b6 1233 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
4eeaa230
DE
1234 block != NULL;
1235 block = BLOCK_SUPERBLOCK (block))
4b2f86ef 1236 current_language->iterate_over_symbols (block, name, domain, callback);
4eeaa230
DE
1237}
1238
b5ec771e
PA
1239/* A helper for find_method. This finds all methods in type T of
1240 language T_LANG which match NAME. It adds matching symbol names to
1241 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
50641945 1242
f8eba3c6 1243static void
b5ec771e 1244find_methods (struct type *t, enum language t_lang, const char *name,
9b2f8581 1245 std::vector<const char *> *result_names,
8e8d776e 1246 std::vector<struct type *> *superclasses)
50641945 1247{
50641945 1248 int ibase;
7d93a1e0 1249 const char *class_name = t->name ();
c00f8484 1250
50641945
FN
1251 /* Ignore this class if it doesn't have a name. This is ugly, but
1252 unless we figure out how to get the physname without the name of
1253 the class, then the loop can't do any good. */
f8eba3c6 1254 if (class_name)
50641945
FN
1255 {
1256 int method_counter;
b5ec771e
PA
1257 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1258 symbol_name_matcher_ftype *symbol_name_compare
c9debfb9 1259 = language_def (t_lang)->get_symbol_name_matcher (lookup_name);
50641945 1260
f168693b 1261 t = check_typedef (t);
50641945
FN
1262
1263 /* Loop over each method name. At this level, all overloads of a name
dda83cd7
SM
1264 are counted as a single name. There is an inner loop which loops over
1265 each overload. */
50641945
FN
1266
1267 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1268 method_counter >= 0;
1269 --method_counter)
1270 {
0d5cff50 1271 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
50641945 1272
b5ec771e 1273 if (symbol_name_compare (method_name, lookup_name, NULL))
f8eba3c6
TT
1274 {
1275 int field_counter;
aee8d8ba 1276
f8eba3c6
TT
1277 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1278 - 1);
1279 field_counter >= 0;
1280 --field_counter)
1281 {
1282 struct fn_field *f;
1283 const char *phys_name;
1284
1285 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1286 if (TYPE_FN_FIELD_STUB (f, field_counter))
1287 continue;
1288 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
9b2f8581 1289 result_names->push_back (phys_name);
f8eba3c6
TT
1290 }
1291 }
aee8d8ba
DC
1292 }
1293 }
1294
f8eba3c6 1295 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
8e8d776e 1296 superclasses->push_back (TYPE_BASECLASS (t, ibase));
50641945
FN
1297}
1298
50641945
FN
1299/* Find an instance of the character C in the string S that is outside
1300 of all parenthesis pairs, single-quoted strings, and double-quoted
8120c9d5 1301 strings. Also, ignore the char within a template name, like a ','
be966d42 1302 within foo<int, int>, while considering C++ operator</operator<<. */
8120c9d5 1303
87f0e720 1304const char *
40e084e1 1305find_toplevel_char (const char *s, char c)
50641945
FN
1306{
1307 int quoted = 0; /* zero if we're not in quotes;
1308 '"' if we're in a double-quoted string;
1309 '\'' if we're in a single-quoted string. */
a04257e6 1310 int depth = 0; /* Number of unclosed parens we've seen. */
40e084e1 1311 const char *scan;
50641945
FN
1312
1313 for (scan = s; *scan; scan++)
1314 {
1315 if (quoted)
1316 {
1317 if (*scan == quoted)
1318 quoted = 0;
1319 else if (*scan == '\\' && *(scan + 1))
1320 scan++;
1321 }
1322 else if (*scan == c && ! quoted && depth == 0)
1323 return scan;
1324 else if (*scan == '"' || *scan == '\'')
1325 quoted = *scan;
8120c9d5 1326 else if (*scan == '(' || *scan == '<')
50641945 1327 depth++;
8120c9d5 1328 else if ((*scan == ')' || *scan == '>') && depth > 0)
50641945 1329 depth--;
be966d42
PA
1330 else if (*scan == 'o' && !quoted && depth == 0)
1331 {
1332 /* Handle C++ operator names. */
1333 if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
1334 {
1335 scan += CP_OPERATOR_LEN;
1336 if (*scan == c)
1337 return scan;
1338 while (isspace (*scan))
1339 {
1340 ++scan;
1341 if (*scan == c)
1342 return scan;
1343 }
1344 if (*scan == '\0')
1345 break;
1346
1347 switch (*scan)
1348 {
1349 /* Skip over one less than the appropriate number of
1350 characters: the for loop will skip over the last
1351 one. */
1352 case '<':
1353 if (scan[1] == '<')
1354 {
1355 scan++;
1356 if (*scan == c)
1357 return scan;
1358 }
1359 break;
1360 case '>':
1361 if (scan[1] == '>')
1362 {
1363 scan++;
1364 if (*scan == c)
1365 return scan;
1366 }
1367 break;
1368 }
1369 }
1370 }
50641945
FN
1371 }
1372
1373 return 0;
1374}
1375
40e084e1
KS
1376/* The string equivalent of find_toplevel_char. Returns a pointer
1377 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1378 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
889f28e2 1379
40e084e1
KS
1380static const char *
1381find_toplevel_string (const char *haystack, const char *needle)
889f28e2 1382{
40e084e1
KS
1383 const char *s = haystack;
1384
1385 do
1386 {
1387 s = find_toplevel_char (s, *needle);
1388
1389 if (s != NULL)
1390 {
1391 /* Found first char in HAYSTACK; check rest of string. */
61012eef 1392 if (startswith (s, needle))
40e084e1
KS
1393 return s;
1394
1395 /* Didn't find it; loop over HAYSTACK, looking for the next
1396 instance of the first character of NEEDLE. */
1397 ++s;
1398 }
1399 }
1400 while (s != NULL && *s != '\0');
1401
1402 /* NEEDLE was not found in HAYSTACK. */
1403 return NULL;
889f28e2
AF
1404}
1405
33f448b1 1406/* Convert CANONICAL to its string representation using
53a0f8a2 1407 symtab_to_fullname for SYMTAB. */
33f448b1 1408
53a0f8a2 1409static std::string
33f448b1
JK
1410canonical_to_fullform (const struct linespec_canonical_name *canonical)
1411{
1412 if (canonical->symtab == NULL)
53a0f8a2 1413 return canonical->suffix;
33f448b1 1414 else
53a0f8a2
TT
1415 return string_printf ("%s:%s", symtab_to_fullname (canonical->symtab),
1416 canonical->suffix);
33f448b1
JK
1417}
1418
f8eba3c6
TT
1419/* Given FILTERS, a list of canonical names, filter the sals in RESULT
1420 and store the result in SELF->CANONICAL. */
50641945 1421
f8eba3c6
TT
1422static void
1423filter_results (struct linespec_state *self,
6c5b2ebe 1424 std::vector<symtab_and_line> *result,
f73c6ece 1425 const std::vector<const char *> &filters)
f8eba3c6 1426{
f73c6ece 1427 for (const char *name : filters)
f8eba3c6 1428 {
6c5b2ebe 1429 linespec_sals lsal;
f8eba3c6 1430
6c5b2ebe 1431 for (size_t j = 0; j < result->size (); ++j)
f8eba3c6 1432 {
33f448b1 1433 const struct linespec_canonical_name *canonical;
33f448b1
JK
1434
1435 canonical = &self->canonical_names[j];
53a0f8a2 1436 std::string fullform = canonical_to_fullform (canonical);
33f448b1 1437
53a0f8a2 1438 if (name == fullform)
6c5b2ebe 1439 lsal.sals.push_back ((*result)[j]);
f8eba3c6
TT
1440 }
1441
6c5b2ebe 1442 if (!lsal.sals.empty ())
f8eba3c6
TT
1443 {
1444 lsal.canonical = xstrdup (name);
6c5b2ebe 1445 self->canonical->lsals.push_back (std::move (lsal));
f8eba3c6
TT
1446 }
1447 }
1448
1449 self->canonical->pre_expanded = 0;
1450}
1451
1452/* Store RESULT into SELF->CANONICAL. */
1453
1454static void
1455convert_results_to_lsals (struct linespec_state *self,
6c5b2ebe 1456 std::vector<symtab_and_line> *result)
50641945 1457{
f8eba3c6
TT
1458 struct linespec_sals lsal;
1459
1460 lsal.canonical = NULL;
6c5b2ebe
PA
1461 lsal.sals = std::move (*result);
1462 self->canonical->lsals.push_back (std::move (lsal));
f8eba3c6
TT
1463}
1464
33f448b1
JK
1465/* A structure that contains two string representations of a struct
1466 linespec_canonical_name:
6471e7d2 1467 - one where the symtab's fullname is used;
33f448b1
JK
1468 - one where the filename followed the "set filename-display"
1469 setting. */
1470
1471struct decode_line_2_item
1472{
53a0f8a2
TT
1473 decode_line_2_item (std::string &&fullform_, std::string &&displayform_,
1474 bool selected_)
1475 : fullform (std::move (fullform_)),
1476 displayform (std::move (displayform_)),
1477 selected (selected_)
1478 {
1479 }
33f448b1 1480
53a0f8a2
TT
1481 /* The form using symtab_to_fullname. */
1482 std::string fullform;
1483
1484 /* The form using symtab_to_filename_for_display. */
1485 std::string displayform;
33f448b1
JK
1486
1487 /* Field is initialized to zero and it is set to one if the user
1488 requested breakpoint for this entry. */
1489 unsigned int selected : 1;
1490};
1491
53a0f8a2
TT
1492/* Helper for std::sort to sort decode_line_2_item entries by
1493 DISPLAYFORM and secondarily by FULLFORM. */
33f448b1 1494
53a0f8a2
TT
1495static bool
1496decode_line_2_compare_items (const decode_line_2_item &a,
1497 const decode_line_2_item &b)
33f448b1 1498{
53a0f8a2
TT
1499 if (a.displayform != b.displayform)
1500 return a.displayform < b.displayform;
1501 return a.fullform < b.fullform;
33f448b1
JK
1502}
1503
f8eba3c6
TT
1504/* Handle multiple results in RESULT depending on SELECT_MODE. This
1505 will either return normally, throw an exception on multiple
1506 results, or present a menu to the user. On return, the SALS vector
1507 in SELF->CANONICAL is set up properly. */
1508
1509static void
1510decode_line_2 (struct linespec_state *self,
6c5b2ebe 1511 std::vector<symtab_and_line> *result,
f8eba3c6
TT
1512 const char *select_mode)
1513{
992a7040 1514 const char *args;
a121b7c1 1515 const char *prompt;
50641945 1516 int i;
f73c6ece 1517 std::vector<const char *> filters;
53a0f8a2 1518 std::vector<struct decode_line_2_item> items;
50641945 1519
f8eba3c6
TT
1520 gdb_assert (select_mode != multiple_symbols_all);
1521 gdb_assert (self->canonical != NULL);
6c5b2ebe 1522 gdb_assert (!result->empty ());
33f448b1 1523
33f448b1 1524 /* Prepare ITEMS array. */
53a0f8a2 1525 for (i = 0; i < result->size (); ++i)
50641945 1526 {
33f448b1 1527 const struct linespec_canonical_name *canonical;
53a0f8a2
TT
1528 std::string displayform;
1529
33f448b1
JK
1530 canonical = &self->canonical_names[i];
1531 gdb_assert (canonical->suffix != NULL);
f8eba3c6 1532
53a0f8a2 1533 std::string fullform = canonical_to_fullform (canonical);
33f448b1
JK
1534
1535 if (canonical->symtab == NULL)
53a0f8a2 1536 displayform = canonical->suffix;
33f448b1 1537 else
f8eba3c6 1538 {
33f448b1
JK
1539 const char *fn_for_display;
1540
1541 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
53a0f8a2
TT
1542 displayform = string_printf ("%s:%s", fn_for_display,
1543 canonical->suffix);
f8eba3c6
TT
1544 }
1545
53a0f8a2
TT
1546 items.emplace_back (std::move (fullform), std::move (displayform),
1547 false);
50641945
FN
1548 }
1549
33f448b1 1550 /* Sort the list of method names. */
53a0f8a2 1551 std::sort (items.begin (), items.end (), decode_line_2_compare_items);
33f448b1
JK
1552
1553 /* Remove entries with the same FULLFORM. */
53a0f8a2
TT
1554 items.erase (std::unique (items.begin (), items.end (),
1555 [] (const struct decode_line_2_item &a,
1556 const struct decode_line_2_item &b)
1557 {
1558 return a.fullform == b.fullform;
1559 }),
1560 items.end ());
1561
1562 if (select_mode == multiple_symbols_cancel && items.size () > 1)
f8eba3c6
TT
1563 error (_("canceled because the command is ambiguous\n"
1564 "See set/show multiple-symbol."));
1565
53a0f8a2 1566 if (select_mode == multiple_symbols_all || items.size () == 1)
50641945 1567 {
f8eba3c6
TT
1568 convert_results_to_lsals (self, result);
1569 return;
50641945
FN
1570 }
1571
f8eba3c6 1572 printf_unfiltered (_("[0] cancel\n[1] all\n"));
53a0f8a2
TT
1573 for (i = 0; i < items.size (); i++)
1574 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform.c_str ());
f8eba3c6
TT
1575
1576 prompt = getenv ("PS2");
1577 if (prompt == NULL)
50641945 1578 {
f8eba3c6 1579 prompt = "> ";
50641945 1580 }
89fbedf3 1581 args = command_line_input (prompt, "overload-choice");
50641945
FN
1582
1583 if (args == 0 || *args == 0)
e2e0b3e5 1584 error_no_arg (_("one or more choice numbers"));
50641945 1585
bfd28288
PA
1586 number_or_range_parser parser (args);
1587 while (!parser.finished ())
50641945 1588 {
bfd28288 1589 int num = parser.get_number ();
50641945
FN
1590
1591 if (num == 0)
8a3fe4f8 1592 error (_("canceled"));
50641945
FN
1593 else if (num == 1)
1594 {
f8eba3c6
TT
1595 /* We intentionally make this result in a single breakpoint,
1596 contrary to what older versions of gdb did. The
1597 rationale is that this lets a user get the
1598 multiple_symbols_all behavior even with the 'ask'
1599 setting; and he can get separate breakpoints by entering
1600 "2-57" at the query. */
f8eba3c6
TT
1601 convert_results_to_lsals (self, result);
1602 return;
50641945
FN
1603 }
1604
f8eba3c6 1605 num -= 2;
53a0f8a2 1606 if (num >= items.size ())
f8eba3c6 1607 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
1608 else
1609 {
33f448b1 1610 struct decode_line_2_item *item = &items[num];
f8eba3c6 1611
33f448b1 1612 if (!item->selected)
50641945 1613 {
f73c6ece 1614 filters.push_back (item->fullform.c_str ());
33f448b1 1615 item->selected = 1;
50641945
FN
1616 }
1617 else
1618 {
3e43a32a 1619 printf_unfiltered (_("duplicate request for %d ignored.\n"),
f6f99966 1620 num + 2);
50641945
FN
1621 }
1622 }
50641945 1623 }
f8eba3c6
TT
1624
1625 filter_results (self, result, filters);
50641945 1626}
94af9270 1627
40e084e1 1628\f
3d50dd94 1629
40e084e1
KS
1630/* The parser of linespec itself. */
1631
1632/* Throw an appropriate error when SYMBOL is not found (optionally in
1633 FILENAME). */
1634
1635static void ATTRIBUTE_NORETURN
5d94e27b 1636symbol_not_found_error (const char *symbol, const char *filename)
3d50dd94 1637{
40e084e1
KS
1638 if (symbol == NULL)
1639 symbol = "";
1640
1641 if (!have_full_symbols ()
1642 && !have_partial_symbols ()
1643 && !have_minimal_symbols ())
1644 throw_error (NOT_FOUND_ERROR,
1645 _("No symbol table is loaded. Use the \"file\" command."));
1646
1647 /* If SYMBOL starts with '$', the user attempted to either lookup
1648 a function/variable in his code starting with '$' or an internal
1649 variable of that name. Since we do not know which, be concise and
1650 explain both possibilities. */
1651 if (*symbol == '$')
1652 {
1653 if (filename)
1654 throw_error (NOT_FOUND_ERROR,
1655 _("Undefined convenience variable or function \"%s\" "
1656 "not defined in \"%s\"."), symbol, filename);
1657 else
1658 throw_error (NOT_FOUND_ERROR,
1659 _("Undefined convenience variable or function \"%s\" "
1660 "not defined."), symbol);
1661 }
1662 else
1663 {
1664 if (filename)
1665 throw_error (NOT_FOUND_ERROR,
1666 _("Function \"%s\" not defined in \"%s\"."),
1667 symbol, filename);
1668 else
1669 throw_error (NOT_FOUND_ERROR,
1670 _("Function \"%s\" not defined."), symbol);
1671 }
3d50dd94
JK
1672}
1673
40e084e1
KS
1674/* Throw an appropriate error when an unexpected token is encountered
1675 in the input. */
94af9270 1676
40e084e1
KS
1677static void ATTRIBUTE_NORETURN
1678unexpected_linespec_error (linespec_parser *parser)
94af9270 1679{
40e084e1
KS
1680 linespec_token token;
1681 static const char * token_type_strings[]
1682 = {"keyword", "colon", "string", "number", "comma", "end of input"};
94af9270 1683
40e084e1
KS
1684 /* Get the token that generated the error. */
1685 token = linespec_lexer_lex_one (parser);
94af9270 1686
40e084e1
KS
1687 /* Finally, throw the error. */
1688 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1689 || token.type == LSTOKEN_KEYWORD)
94af9270 1690 {
a5b5adf5 1691 gdb::unique_xmalloc_ptr<char> string = copy_token_string (token);
40e084e1
KS
1692 throw_error (GENERIC_ERROR,
1693 _("malformed linespec error: unexpected %s, \"%s\""),
a5b5adf5 1694 token_type_strings[token.type], string.get ());
40e084e1
KS
1695 }
1696 else
1697 throw_error (GENERIC_ERROR,
1698 _("malformed linespec error: unexpected %s"),
1699 token_type_strings[token.type]);
1700}
1701
00e52e53
KS
1702/* Throw an undefined label error. */
1703
1704static void ATTRIBUTE_NORETURN
1705undefined_label_error (const char *function, const char *label)
1706{
1707 if (function != NULL)
1708 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
1709 _("No label \"%s\" defined in function \"%s\"."),
1710 label, function);
00e52e53
KS
1711 else
1712 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
1713 _("No label \"%s\" defined in current function."),
1714 label);
00e52e53
KS
1715}
1716
1717/* Throw a source file not found error. */
1718
1719static void ATTRIBUTE_NORETURN
1720source_file_not_found_error (const char *name)
1721{
1722 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1723}
1724
c45ec17c
PA
1725/* Unless at EIO, save the current stream position as completion word
1726 point, and consume the next token. */
1727
1728static linespec_token
1729save_stream_and_consume_token (linespec_parser *parser)
1730{
1731 if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
1732 parser->completion_word = PARSER_STREAM (parser);
1733 return linespec_lexer_consume_token (parser);
1734}
1735
87f0e720 1736/* See description in linespec.h. */
40e084e1 1737
87f0e720 1738struct line_offset
09cf2b22 1739linespec_parse_line_offset (const char *string)
40e084e1 1740{
87f0e720 1741 const char *start = string;
40e084e1
KS
1742 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1743
1744 if (*string == '+')
1745 {
1746 line_offset.sign = LINE_OFFSET_PLUS;
1747 ++string;
1748 }
1749 else if (*string == '-')
1750 {
1751 line_offset.sign = LINE_OFFSET_MINUS;
1752 ++string;
1753 }
1754
87f0e720
KS
1755 if (*string != '\0' && !isdigit (*string))
1756 error (_("malformed line offset: \"%s\""), start);
1757
40e084e1
KS
1758 /* Right now, we only allow base 10 for offsets. */
1759 line_offset.offset = atoi (string);
1760 return line_offset;
1761}
1762
c45ec17c
PA
1763/* In completion mode, if the user is still typing the number, there's
1764 no possible completion to offer. But if there's already input past
1765 the number, setup to expect NEXT. */
1766
1767static void
1768set_completion_after_number (linespec_parser *parser,
1769 linespec_complete_what next)
1770{
1771 if (*PARSER_STREAM (parser) == ' ')
1772 {
f1735a53 1773 parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
c45ec17c
PA
1774 parser->complete_what = next;
1775 }
1776 else
1777 {
1778 parser->completion_word = PARSER_STREAM (parser);
1779 parser->complete_what = linespec_complete_what::NOTHING;
1780 }
1781}
1782
40e084e1
KS
1783/* Parse the basic_spec in PARSER's input. */
1784
1785static void
1786linespec_parse_basic (linespec_parser *parser)
1787{
a5b5adf5 1788 gdb::unique_xmalloc_ptr<char> name;
40e084e1 1789 linespec_token token;
7e41c8db
KS
1790 std::vector<block_symbol> symbols;
1791 std::vector<block_symbol> *labels;
c2a031c5 1792 std::vector<bound_minimal_symbol> minimal_symbols;
40e084e1
KS
1793
1794 /* Get the next token. */
1795 token = linespec_lexer_lex_one (parser);
1796
1797 /* If it is EOI or KEYWORD, issue an error. */
c45ec17c
PA
1798 if (token.type == LSTOKEN_KEYWORD)
1799 {
1800 parser->complete_what = linespec_complete_what::NOTHING;
1801 unexpected_linespec_error (parser);
1802 }
1803 else if (token.type == LSTOKEN_EOI)
1804 {
1805 unexpected_linespec_error (parser);
1806 }
40e084e1
KS
1807 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1808 else if (token.type == LSTOKEN_NUMBER)
1809 {
c45ec17c
PA
1810 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1811
40e084e1
KS
1812 /* Record the line offset and get the next token. */
1813 name = copy_token_string (token);
a5b5adf5
TT
1814 PARSER_EXPLICIT (parser)->line_offset
1815 = linespec_parse_line_offset (name.get ());
40e084e1
KS
1816
1817 /* Get the next token. */
1818 token = linespec_lexer_consume_token (parser);
1819
1820 /* If the next token is a comma, stop parsing and return. */
1821 if (token.type == LSTOKEN_COMMA)
c45ec17c
PA
1822 {
1823 parser->complete_what = linespec_complete_what::NOTHING;
1824 return;
1825 }
40e084e1
KS
1826
1827 /* If the next token is anything but EOI or KEYWORD, issue
1828 an error. */
1829 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1830 unexpected_linespec_error (parser);
1831 }
1832
1833 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1834 return;
1835
1836 /* Next token must be LSTOKEN_STRING. */
1837 if (token.type != LSTOKEN_STRING)
c45ec17c
PA
1838 {
1839 parser->complete_what = linespec_complete_what::NOTHING;
1840 unexpected_linespec_error (parser);
1841 }
40e084e1
KS
1842
1843 /* The current token will contain the name of a function, method,
1844 or label. */
c45ec17c 1845 name = copy_token_string (token);
c45ec17c
PA
1846
1847 if (parser->completion_tracker != NULL)
1848 {
1849 /* If the function name ends with a ":", then this may be an
1850 incomplete "::" scope operator instead of a label separator.
1851 E.g.,
1852 "b klass:<tab>"
1853 which should expand to:
1854 "b klass::method()"
1855
1856 Do a tentative completion assuming the later. If we find
1857 completions, advance the stream past the colon token and make
1858 it part of the function name/token. */
1859
1860 if (!parser->completion_quote_char
1861 && strcmp (PARSER_STREAM (parser), ":") == 0)
1862 {
1863 completion_tracker tmp_tracker;
1864 const char *source_filename
1865 = PARSER_EXPLICIT (parser)->source_filename;
a20714ff
PA
1866 symbol_name_match_type match_type
1867 = PARSER_EXPLICIT (parser)->func_name_match_type;
c45ec17c
PA
1868
1869 linespec_complete_function (tmp_tracker,
1870 parser->completion_word,
a20714ff 1871 match_type,
c45ec17c
PA
1872 source_filename);
1873
1874 if (tmp_tracker.have_completions ())
1875 {
1876 PARSER_STREAM (parser)++;
1877 LS_TOKEN_STOKEN (token).length++;
1878
a5b5adf5
TT
1879 name.reset (savestring (parser->completion_word,
1880 (PARSER_STREAM (parser)
1881 - parser->completion_word)));
c45ec17c
PA
1882 }
1883 }
1884
a5b5adf5 1885 PARSER_EXPLICIT (parser)->function_name = name.release ();
c45ec17c
PA
1886 }
1887 else
1888 {
6a307fc5
TT
1889 /* Try looking it up as a function/method. */
1890 find_linespec_symbols (PARSER_STATE (parser),
a5b5adf5 1891 PARSER_RESULT (parser)->file_symtabs, name.get (),
6a307fc5
TT
1892 PARSER_EXPLICIT (parser)->func_name_match_type,
1893 &symbols, &minimal_symbols);
40e084e1 1894
c2a031c5 1895 if (!symbols.empty () || !minimal_symbols.empty ())
94af9270 1896 {
7243d011 1897 PARSER_RESULT (parser)->function_symbols
7e41c8db 1898 = new std::vector<block_symbol> (std::move (symbols));
c2a031c5
KS
1899 PARSER_RESULT (parser)->minimal_symbols
1900 = new std::vector<bound_minimal_symbol>
dda83cd7 1901 (std::move (minimal_symbols));
a5b5adf5 1902 PARSER_EXPLICIT (parser)->function_name = name.release ();
40e084e1 1903 }
6a307fc5 1904 else
b4013987 1905 {
6a307fc5
TT
1906 /* NAME was not a function or a method. So it must be a label
1907 name or user specified variable like "break foo.c:$zippo". */
1908 labels = find_label_symbols (PARSER_STATE (parser), NULL,
a5b5adf5 1909 &symbols, name.get ());
6a307fc5
TT
1910 if (labels != NULL)
1911 {
1912 PARSER_RESULT (parser)->labels.label_symbols = labels;
7243d011 1913 PARSER_RESULT (parser)->labels.function_symbols
7e41c8db 1914 = new std::vector<block_symbol> (std::move (symbols));
a5b5adf5 1915 PARSER_EXPLICIT (parser)->label_name = name.release ();
6a307fc5
TT
1916 }
1917 else if (token.type == LSTOKEN_STRING
1918 && *LS_TOKEN_STOKEN (token).ptr == '$')
1919 {
1920 /* User specified a convenience variable or history value. */
1921 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1922 = linespec_parse_variable (PARSER_STATE (parser), name.get ());
b4013987 1923
6a307fc5
TT
1924 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
1925 {
1926 /* The user-specified variable was not valid. Do not
1927 throw an error here. parse_linespec will do it for us. */
a5b5adf5 1928 PARSER_EXPLICIT (parser)->function_name = name.release ();
6a307fc5
TT
1929 return;
1930 }
6a307fc5
TT
1931 }
1932 else
b4013987 1933 {
6a307fc5
TT
1934 /* The name is also not a label. Abort parsing. Do not throw
1935 an error here. parse_linespec will do it for us. */
1936
1937 /* Save a copy of the name we were trying to lookup. */
a5b5adf5 1938 PARSER_EXPLICIT (parser)->function_name = name.release ();
b4013987
AA
1939 return;
1940 }
1941 }
c45ec17c
PA
1942 }
1943
1944 int previous_qc = parser->completion_quote_char;
40e084e1
KS
1945
1946 /* Get the next token. */
1947 token = linespec_lexer_consume_token (parser);
1948
c45ec17c
PA
1949 if (token.type == LSTOKEN_EOI)
1950 {
1951 if (previous_qc && !parser->completion_quote_char)
1952 parser->complete_what = linespec_complete_what::KEYWORD;
1953 }
1954 else if (token.type == LSTOKEN_COLON)
40e084e1
KS
1955 {
1956 /* User specified a label or a lineno. */
1957 token = linespec_lexer_consume_token (parser);
1958
1959 if (token.type == LSTOKEN_NUMBER)
1960 {
1961 /* User specified an offset. Record the line offset and
1962 get the next token. */
c45ec17c
PA
1963 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1964
40e084e1 1965 name = copy_token_string (token);
00e52e53 1966 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1967 = linespec_parse_line_offset (name.get ());
40e084e1 1968
c45ec17c 1969 /* Get the next token. */
40e084e1
KS
1970 token = linespec_lexer_consume_token (parser);
1971 }
c45ec17c
PA
1972 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
1973 {
1974 parser->complete_what = linespec_complete_what::LABEL;
1975 }
40e084e1
KS
1976 else if (token.type == LSTOKEN_STRING)
1977 {
c45ec17c
PA
1978 parser->complete_what = linespec_complete_what::LABEL;
1979
1980 /* If we have text after the label separated by whitespace
1981 (e.g., "b func():lab i<tab>"), don't consider it part of
1982 the label. In completion mode that should complete to
1983 "if", in normal mode, the 'i' should be treated as
1984 garbage. */
1985 if (parser->completion_quote_char == '\0')
1986 {
1987 const char *ptr = LS_TOKEN_STOKEN (token).ptr;
1988 for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
1989 {
1990 if (ptr[i] == ' ')
1991 {
1992 LS_TOKEN_STOKEN (token).length = i;
f1735a53 1993 PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
c45ec17c
PA
1994 break;
1995 }
1996 }
1997 }
1998
1999 if (parser->completion_tracker != NULL)
2000 {
2001 if (PARSER_STREAM (parser)[-1] == ' ')
2002 {
2003 parser->completion_word = PARSER_STREAM (parser);
2004 parser->complete_what = linespec_complete_what::KEYWORD;
2005 }
2006 }
2007 else
2008 {
6a307fc5
TT
2009 /* Grab a copy of the label's name and look it up. */
2010 name = copy_token_string (token);
6a307fc5
TT
2011 labels
2012 = find_label_symbols (PARSER_STATE (parser),
2013 PARSER_RESULT (parser)->function_symbols,
a5b5adf5 2014 &symbols, name.get ());
40e084e1 2015
6a307fc5
TT
2016 if (labels != NULL)
2017 {
2018 PARSER_RESULT (parser)->labels.label_symbols = labels;
7243d011 2019 PARSER_RESULT (parser)->labels.function_symbols
7e41c8db 2020 = new std::vector<block_symbol> (std::move (symbols));
a5b5adf5 2021 PARSER_EXPLICIT (parser)->label_name = name.release ();
6a307fc5
TT
2022 }
2023 else
2024 {
2025 /* We don't know what it was, but it isn't a label. */
2026 undefined_label_error
a5b5adf5 2027 (PARSER_EXPLICIT (parser)->function_name, name.get ());
6a307fc5 2028 }
40e084e1 2029
c45ec17c
PA
2030 }
2031
40e084e1 2032 /* Check for a line offset. */
c45ec17c 2033 token = save_stream_and_consume_token (parser);
40e084e1
KS
2034 if (token.type == LSTOKEN_COLON)
2035 {
2036 /* Get the next token. */
2037 token = linespec_lexer_consume_token (parser);
2038
2039 /* It must be a line offset. */
2040 if (token.type != LSTOKEN_NUMBER)
2041 unexpected_linespec_error (parser);
2042
c6756f62 2043 /* Record the line offset and get the next token. */
40e084e1 2044 name = copy_token_string (token);
40e084e1 2045
00e52e53 2046 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 2047 = linespec_parse_line_offset (name.get ());
40e084e1
KS
2048
2049 /* Get the next token. */
2050 token = linespec_lexer_consume_token (parser);
94af9270
KS
2051 }
2052 }
40e084e1
KS
2053 else
2054 {
2055 /* Trailing ':' in the input. Issue an error. */
2056 unexpected_linespec_error (parser);
2057 }
94af9270 2058 }
40e084e1 2059}
94af9270 2060
40e084e1 2061/* Canonicalize the linespec contained in LS. The result is saved into
00e52e53
KS
2062 STATE->canonical. This function handles both linespec and explicit
2063 locations. */
40e084e1
KS
2064
2065static void
f00aae0f 2066canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
40e084e1 2067{
00e52e53 2068 struct event_location *canon;
67994074 2069 struct explicit_location *explicit_loc;
f00aae0f 2070
40e084e1
KS
2071 /* If canonicalization was not requested, no need to do anything. */
2072 if (!state->canonical)
2073 return;
2074
00e52e53 2075 /* Save everything as an explicit location. */
8e9e35b1
TT
2076 state->canonical->location
2077 = new_explicit_location (&ls->explicit_loc);
2078 canon = state->canonical->location.get ();
67994074 2079 explicit_loc = get_explicit_location (canon);
40e084e1 2080
67994074 2081 if (explicit_loc->label_name != NULL)
a06efdd6 2082 {
00e52e53 2083 state->canonical->special_display = 1;
40e084e1 2084
67994074 2085 if (explicit_loc->function_name == NULL)
40e084e1 2086 {
a06efdd6 2087 /* No function was specified, so add the symbol name. */
7243d011
KS
2088 gdb_assert (!ls->labels.function_symbols->empty ()
2089 && (ls->labels.function_symbols->size () == 1));
7e41c8db 2090 block_symbol s = ls->labels.function_symbols->front ();
987012b8 2091 explicit_loc->function_name = xstrdup (s.symbol->natural_name ());
40e084e1 2092 }
a06efdd6 2093 }
40e084e1 2094
00e52e53
KS
2095 /* If this location originally came from a linespec, save a string
2096 representation of it for display and saving to file. */
2097 if (state->is_linespec)
a06efdd6 2098 {
67994074 2099 char *linespec = explicit_location_to_linespec (explicit_loc);
a06efdd6 2100
00e52e53
KS
2101 set_event_location_string (canon, linespec);
2102 xfree (linespec);
2103 }
94af9270 2104}
c00f8484 2105
40e084e1 2106/* Given a line offset in LS, construct the relevant SALs. */
c00f8484 2107
6c5b2ebe 2108static std::vector<symtab_and_line>
40e084e1
KS
2109create_sals_line_offset (struct linespec_state *self,
2110 linespec_p ls)
c00f8484 2111{
40e084e1 2112 int use_default = 0;
c00f8484 2113
40e084e1
KS
2114 /* This is where we need to make sure we have good defaults.
2115 We must guarantee that this section of code is never executed
2e47c6ca 2116 when we are called with just a function name, since
40e084e1
KS
2117 set_default_source_symtab_and_line uses
2118 select_source_symtab that calls us with such an argument. */
2119
2a908241
KS
2120 if (ls->file_symtabs->size () == 1
2121 && ls->file_symtabs->front () == nullptr)
3d50dd94 2122 {
40e084e1 2123 set_current_program_space (self->program_space);
c00f8484 2124
40e084e1
KS
2125 /* Make sure we have at least a default source line. */
2126 set_default_source_symtab_and_line ();
2127 initialize_defaults (&self->default_symtab, &self->default_line);
4717cec4 2128 *ls->file_symtabs
bb995d00
KS
2129 = collect_symtabs_from_filename (self->default_symtab->filename,
2130 self->search_pspace);
40e084e1
KS
2131 use_default = 1;
2132 }
c00f8484 2133
51abb421 2134 symtab_and_line val;
67994074
KS
2135 val.line = ls->explicit_loc.line_offset.offset;
2136 switch (ls->explicit_loc.line_offset.sign)
40e084e1
KS
2137 {
2138 case LINE_OFFSET_PLUS:
67994074 2139 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2140 val.line = 5;
2141 if (use_default)
2142 val.line = self->default_line + val.line;
2143 break;
2144
2145 case LINE_OFFSET_MINUS:
67994074 2146 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2147 val.line = 15;
2148 if (use_default)
2149 val.line = self->default_line - val.line;
2150 else
2151 val.line = -val.line;
2152 break;
2153
2154 case LINE_OFFSET_NONE:
2155 break; /* No need to adjust val.line. */
2156 }
2157
6c5b2ebe 2158 std::vector<symtab_and_line> values;
40e084e1 2159 if (self->list_mode)
6c5b2ebe 2160 values = decode_digits_list_mode (self, ls, val);
40e084e1
KS
2161 else
2162 {
2163 struct linetable_entry *best_entry = NULL;
40e084e1
KS
2164 int i, j;
2165
6c5b2ebe
PA
2166 std::vector<symtab_and_line> intermediate_results
2167 = decode_digits_ordinary (self, ls, val.line, &best_entry);
2168 if (intermediate_results.empty () && best_entry != NULL)
2169 intermediate_results = decode_digits_ordinary (self, ls,
2170 best_entry->line,
2171 &best_entry);
40e084e1
KS
2172
2173 /* For optimized code, the compiler can scatter one source line
2174 across disjoint ranges of PC values, even when no duplicate
2175 functions or inline functions are involved. For example,
2176 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2177 function can result in two PC ranges. In this case, we don't
2178 want to set a breakpoint on the first PC of each range. To filter
2179 such cases, we use containing blocks -- for each PC found
2180 above, we see if there are other PCs that are in the same
2181 block. If yes, the other PCs are filtered out. */
2182
0fc21fd8
TT
2183 gdb::def_vector<int> filter (intermediate_results.size ());
2184 gdb::def_vector<const block *> blocks (intermediate_results.size ());
40e084e1 2185
6c5b2ebe 2186 for (i = 0; i < intermediate_results.size (); ++i)
3d50dd94 2187 {
6c5b2ebe 2188 set_current_program_space (intermediate_results[i].pspace);
c00f8484 2189
40e084e1 2190 filter[i] = 1;
6c5b2ebe
PA
2191 blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
2192 intermediate_results[i].section);
3d50dd94 2193 }
c00f8484 2194
6c5b2ebe 2195 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2196 {
2197 if (blocks[i] != NULL)
6c5b2ebe 2198 for (j = i + 1; j < intermediate_results.size (); ++j)
40e084e1
KS
2199 {
2200 if (blocks[j] == blocks[i])
2201 {
2202 filter[j] = 0;
2203 break;
2204 }
2205 }
2206 }
c00f8484 2207
6c5b2ebe 2208 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2209 if (filter[i])
2210 {
2211 struct symbol *sym = (blocks[i]
2212 ? block_containing_function (blocks[i])
2213 : NULL);
3d50dd94 2214
40e084e1 2215 if (self->funfirstline)
6c5b2ebe 2216 skip_prologue_sal (&intermediate_results[i]);
cd2bb709 2217 intermediate_results[i].symbol = sym;
6c5b2ebe 2218 add_sal_to_sals (self, &values, &intermediate_results[i],
987012b8 2219 sym ? sym->natural_name () : NULL, 0);
40e084e1 2220 }
f17170e5 2221 }
c00f8484 2222
6c5b2ebe 2223 if (values.empty ())
40e084e1 2224 {
67994074 2225 if (ls->explicit_loc.source_filename)
40e084e1 2226 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
67994074 2227 val.line, ls->explicit_loc.source_filename);
40e084e1
KS
2228 else
2229 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2230 val.line);
2231 }
3d50dd94 2232
40e084e1 2233 return values;
c00f8484
KS
2234}
2235
a06efdd6
KS
2236/* Convert the given ADDRESS into SaLs. */
2237
6c5b2ebe 2238static std::vector<symtab_and_line>
a06efdd6
KS
2239convert_address_location_to_sals (struct linespec_state *self,
2240 CORE_ADDR address)
2241{
6c5b2ebe 2242 symtab_and_line sal = find_pc_line (address, 0);
a06efdd6
KS
2243 sal.pc = address;
2244 sal.section = find_pc_overlay (address);
2245 sal.explicit_pc = 1;
cd2bb709 2246 sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
6c5b2ebe
PA
2247
2248 std::vector<symtab_and_line> sals;
a06efdd6
KS
2249 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
2250
2251 return sals;
2252}
2253
40e084e1
KS
2254/* Create and return SALs from the linespec LS. */
2255
6c5b2ebe 2256static std::vector<symtab_and_line>
40e084e1
KS
2257convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
2258{
6c5b2ebe 2259 std::vector<symtab_and_line> sals;
40e084e1 2260
a06efdd6 2261 if (ls->labels.label_symbols != NULL)
40e084e1
KS
2262 {
2263 /* We have just a bunch of functions/methods or labels. */
7e41c8db
KS
2264 struct symtab_and_line sal;
2265
3553eadc 2266 for (const auto &sym : *ls->labels.label_symbols)
40e084e1 2267 {
7e41c8db
KS
2268 struct program_space *pspace
2269 = SYMTAB_PSPACE (symbol_symtab (sym.symbol));
fdbb204b 2270
7e41c8db 2271 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
fdbb204b 2272 && maybe_add_address (state->addr_set, pspace, sal.pc))
64b92e45 2273 add_sal_to_sals (state, &sals, &sal,
987012b8 2274 sym.symbol->natural_name (), 0);
40e084e1
KS
2275 }
2276 }
2277 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2278 {
2279 /* We have just a bunch of functions and/or methods. */
40e084e1
KS
2280 if (ls->function_symbols != NULL)
2281 {
2282 /* Sort symbols so that symbols with the same program space are next
2283 to each other. */
7243d011
KS
2284 std::sort (ls->function_symbols->begin (),
2285 ls->function_symbols->end (),
4dedf84d 2286 compare_symbols);
40e084e1 2287
7243d011 2288 for (const auto &sym : *ls->function_symbols)
40e084e1 2289 {
7e41c8db
KS
2290 program_space *pspace
2291 = SYMTAB_PSPACE (symbol_symtab (sym.symbol));
40e084e1 2292 set_current_program_space (pspace);
76af0f26
PA
2293
2294 /* Don't skip to the first line of the function if we
2295 had found an ifunc minimal symbol for this function,
2296 because that means that this function is an ifunc
2297 resolver with the same name as the ifunc itself. */
2298 bool found_ifunc = false;
2299
2300 if (state->funfirstline
2301 && ls->minimal_symbols != NULL
7e41c8db 2302 && SYMBOL_CLASS (sym.symbol) == LOC_BLOCK)
76af0f26
PA
2303 {
2304 const CORE_ADDR addr
7e41c8db 2305 = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym.symbol));
76af0f26 2306
c2a031c5 2307 for (const auto &elem : *ls->minimal_symbols)
76af0f26 2308 {
c2a031c5
KS
2309 if (MSYMBOL_TYPE (elem.minsym) == mst_text_gnu_ifunc
2310 || MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
76af0f26 2311 {
c2a031c5
KS
2312 CORE_ADDR msym_addr = BMSYMBOL_VALUE_ADDRESS (elem);
2313 if (MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
f50776aa
PA
2314 {
2315 struct gdbarch *gdbarch
08feed99 2316 = elem.objfile->arch ();
f50776aa
PA
2317 msym_addr
2318 = (gdbarch_convert_from_func_ptr_addr
2319 (gdbarch,
2320 msym_addr,
8b88a78e 2321 current_top_target ()));
f50776aa
PA
2322 }
2323
2324 if (msym_addr == addr)
2325 {
2326 found_ifunc = true;
2327 break;
2328 }
76af0f26
PA
2329 }
2330 }
2331 }
2332
2333 if (!found_ifunc)
2334 {
2335 symtab_and_line sal;
7e41c8db 2336 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
76af0f26
PA
2337 && maybe_add_address (state->addr_set, pspace, sal.pc))
2338 add_sal_to_sals (state, &sals, &sal,
987012b8 2339 sym.symbol->natural_name (), 0);
76af0f26 2340 }
40e084e1
KS
2341 }
2342 }
2343
2344 if (ls->minimal_symbols != NULL)
2345 {
76af0f26 2346 /* Sort minimal symbols by program space, too */
c2a031c5
KS
2347 std::sort (ls->minimal_symbols->begin (),
2348 ls->minimal_symbols->end (),
2349 compare_msymbols);
40e084e1 2350
c2a031c5 2351 for (const auto &elem : *ls->minimal_symbols)
40e084e1 2352 {
c2a031c5 2353 program_space *pspace = elem.objfile->pspace;
40e084e1 2354 set_current_program_space (pspace);
c2a031c5 2355 minsym_found (state, elem.objfile, elem.minsym, &sals);
40e084e1
KS
2356 }
2357 }
2358 }
67994074 2359 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1
KS
2360 {
2361 /* Only an offset was specified. */
2362 sals = create_sals_line_offset (state, ls);
2363
2364 /* Make sure we have a filename for canonicalization. */
67994074 2365 if (ls->explicit_loc.source_filename == NULL)
05cba821
JK
2366 {
2367 const char *fullname = symtab_to_fullname (state->default_symtab);
2368
e93ba630
JK
2369 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2370 form so that displaying SOURCE_FILENAME can follow the current
2371 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2372 it has been kept for code simplicity only in absolute form. */
67994074 2373 ls->explicit_loc.source_filename = xstrdup (fullname);
05cba821 2374 }
40e084e1
KS
2375 }
2376 else
2377 {
2378 /* We haven't found any results... */
2379 return sals;
2380 }
2381
2382 canonicalize_linespec (state, ls);
2383
6c5b2ebe 2384 if (!sals.empty () && state->canonical != NULL)
40e084e1
KS
2385 state->canonical->pre_expanded = 1;
2386
2387 return sals;
2388}
50641945 2389
a2459270
PA
2390/* Build RESULT from the explicit location components SOURCE_FILENAME,
2391 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
00e52e53 2392
a2459270
PA
2393static void
2394convert_explicit_location_to_linespec (struct linespec_state *self,
2395 linespec_p result,
2396 const char *source_filename,
2397 const char *function_name,
a20714ff 2398 symbol_name_match_type fname_match_type,
a2459270
PA
2399 const char *label_name,
2400 struct line_offset line_offset)
00e52e53 2401{
7e41c8db
KS
2402 std::vector<block_symbol> symbols;
2403 std::vector<block_symbol> *labels;
c2a031c5 2404 std::vector<bound_minimal_symbol> minimal_symbols;
00e52e53 2405
a20714ff
PA
2406 result->explicit_loc.func_name_match_type = fname_match_type;
2407
a2459270 2408 if (source_filename != NULL)
00e52e53 2409 {
a70b8144 2410 try
00e52e53 2411 {
4717cec4
SM
2412 *result->file_symtabs
2413 = symtabs_from_filename (source_filename, self->search_pspace);
00e52e53 2414 }
230d2906 2415 catch (const gdb_exception_error &except)
00e52e53 2416 {
a2459270 2417 source_file_not_found_error (source_filename);
00e52e53 2418 }
a2459270 2419 result->explicit_loc.source_filename = xstrdup (source_filename);
00e52e53
KS
2420 }
2421 else
2422 {
2423 /* A NULL entry means to use the default symtab. */
2a908241 2424 result->file_symtabs->push_back (nullptr);
00e52e53
KS
2425 }
2426
a2459270 2427 if (function_name != NULL)
00e52e53
KS
2428 {
2429 find_linespec_symbols (self, result->file_symtabs,
a20714ff
PA
2430 function_name, fname_match_type,
2431 &symbols, &minimal_symbols);
00e52e53 2432
c2a031c5 2433 if (symbols.empty () && minimal_symbols.empty ())
a2459270 2434 symbol_not_found_error (function_name,
67994074 2435 result->explicit_loc.source_filename);
00e52e53 2436
a2459270 2437 result->explicit_loc.function_name = xstrdup (function_name);
7243d011 2438 result->function_symbols
7e41c8db 2439 = new std::vector<block_symbol> (std::move (symbols));
c2a031c5
KS
2440 result->minimal_symbols
2441 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
00e52e53
KS
2442 }
2443
a2459270 2444 if (label_name != NULL)
00e52e53 2445 {
00e52e53 2446 labels = find_label_symbols (self, result->function_symbols,
a2459270 2447 &symbols, label_name);
00e52e53
KS
2448
2449 if (labels == NULL)
67994074 2450 undefined_label_error (result->explicit_loc.function_name,
a2459270 2451 label_name);
00e52e53 2452
a2459270 2453 result->explicit_loc.label_name = xstrdup (label_name);
00e52e53 2454 result->labels.label_symbols = labels;
7243d011 2455 result->labels.function_symbols
7e41c8db 2456 = new std::vector<block_symbol> (std::move (symbols));
00e52e53
KS
2457 }
2458
a2459270
PA
2459 if (line_offset.sign != LINE_OFFSET_UNKNOWN)
2460 result->explicit_loc.line_offset = line_offset;
2461}
00e52e53 2462
a2459270
PA
2463/* Convert the explicit location EXPLICIT_LOC into SaLs. */
2464
6c5b2ebe 2465static std::vector<symtab_and_line>
a2459270
PA
2466convert_explicit_location_to_sals (struct linespec_state *self,
2467 linespec_p result,
2468 const struct explicit_location *explicit_loc)
2469{
2470 convert_explicit_location_to_linespec (self, result,
2471 explicit_loc->source_filename,
2472 explicit_loc->function_name,
a20714ff 2473 explicit_loc->func_name_match_type,
a2459270
PA
2474 explicit_loc->label_name,
2475 explicit_loc->line_offset);
2476 return convert_linespec_to_sals (self, result);
00e52e53
KS
2477}
2478
40e084e1 2479/* Parse a string that specifies a linespec.
50641945 2480
40e084e1 2481 The basic grammar of linespecs:
50641945 2482
a06efdd6 2483 linespec -> var_spec | basic_spec
40e084e1 2484 var_spec -> '$' (STRING | NUMBER)
50641945 2485
40e084e1
KS
2486 basic_spec -> file_offset_spec | function_spec | label_spec
2487 file_offset_spec -> opt_file_spec offset_spec
2488 function_spec -> opt_file_spec function_name_spec opt_label_spec
2489 label_spec -> label_name_spec
50641945 2490
40e084e1
KS
2491 opt_file_spec -> "" | file_name_spec ':'
2492 opt_label_spec -> "" | ':' label_name_spec
2493
2494 file_name_spec -> STRING
2495 function_name_spec -> STRING
2496 label_name_spec -> STRING
2497 function_name_spec -> STRING
2498 offset_spec -> NUMBER
dda83cd7 2499 -> '+' NUMBER
40e084e1
KS
2500 -> '-' NUMBER
2501
2502 This may all be followed by several keywords such as "if EXPR",
2503 which we ignore.
2504
2505 A comma will terminate parsing.
2506
2507 The function may be an undebuggable function found in minimal symbol table.
50641945
FN
2508
2509 If the argument FUNFIRSTLINE is nonzero, we want the first line
2510 of real code inside a function when a function is specified, and it is
2511 not OK to specify a variable or type to get its line number.
2512
2513 DEFAULT_SYMTAB specifies the file to use if none is specified.
2514 It defaults to current_source_symtab.
2515 DEFAULT_LINE specifies the line number to use for relative
2516 line numbers (that start with signs). Defaults to current_source_line.
2517 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 2518 line specs there if necessary. Currently overloaded member functions and
50641945 2519 line numbers or static functions without a filename yield a canonical
1777feb0 2520 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
2521 it is the callers responsibility to free them.
2522
2523 Note that it is possible to return zero for the symtab
2524 if no file is validly specified. Callers must check that.
58438ac1 2525 Also, the line number returned may be invalid. */
50641945 2526
a20714ff
PA
2527/* Parse the linespec in ARG. MATCH_TYPE indicates how function names
2528 should be matched. */
50641945 2529
6c5b2ebe 2530static std::vector<symtab_and_line>
a20714ff
PA
2531parse_linespec (linespec_parser *parser, const char *arg,
2532 symbol_name_match_type match_type)
50641945 2533{
40e084e1 2534 linespec_token token;
cc06b668 2535 struct gdb_exception file_exception;
40e084e1
KS
2536
2537 /* A special case to start. It has become quite popular for
2538 IDEs to work around bugs in the previous parser by quoting
2539 the entire linespec, so we attempt to deal with this nicely. */
2540 parser->is_quote_enclosed = 0;
c45ec17c
PA
2541 if (parser->completion_tracker == NULL
2542 && !is_ada_operator (arg)
f00aae0f 2543 && strchr (linespec_quote_characters, *arg) != NULL)
40e084e1
KS
2544 {
2545 const char *end;
9ef07c8c 2546
f00aae0f 2547 end = skip_quote_char (arg + 1, *arg);
40e084e1 2548 if (end != NULL && is_closing_quote_enclosed (end))
136e1c30 2549 {
f00aae0f 2550 /* Here's the special case. Skip ARG past the initial
40e084e1 2551 quote. */
f00aae0f 2552 ++arg;
40e084e1 2553 parser->is_quote_enclosed = 1;
136e1c30
DE
2554 }
2555 }
e8eb7bc5 2556
f00aae0f
KS
2557 parser->lexer.saved_arg = arg;
2558 parser->lexer.stream = arg;
c45ec17c
PA
2559 parser->completion_word = arg;
2560 parser->complete_what = linespec_complete_what::FUNCTION;
a20714ff 2561 PARSER_EXPLICIT (parser)->func_name_match_type = match_type;
d2630e69 2562
40e084e1
KS
2563 /* Initialize the default symtab and line offset. */
2564 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2565 &PARSER_STATE (parser)->default_line);
d2630e69 2566
40e084e1 2567 /* Objective-C shortcut. */
c45ec17c
PA
2568 if (parser->completion_tracker == NULL)
2569 {
6c5b2ebe
PA
2570 std::vector<symtab_and_line> values
2571 = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2572 if (!values.empty ())
c45ec17c
PA
2573 return values;
2574 }
2575 else
2576 {
2577 /* "-"/"+" is either an objc selector, or a number. There's
2578 nothing to complete the latter to, so just let the caller
2579 complete on functions, which finds objc selectors, if there's
2580 any. */
2581 if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
2582 return {};
2583 }
e0881a8e 2584
40e084e1 2585 /* Start parsing. */
d2630e69 2586
40e084e1 2587 /* Get the first token. */
c45ec17c 2588 token = linespec_lexer_consume_token (parser);
50641945 2589
40e084e1 2590 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
a06efdd6 2591 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
40e084e1 2592 {
40e084e1 2593 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
c45ec17c 2594 if (parser->completion_tracker == NULL)
2a908241 2595 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
dcf9f4ab 2596
40e084e1 2597 /* User specified a convenience variable or history value. */
a5b5adf5 2598 gdb::unique_xmalloc_ptr<char> var = copy_token_string (token);
00e52e53 2599 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 2600 = linespec_parse_variable (PARSER_STATE (parser), var.get ());
f8eba3c6 2601
40e084e1
KS
2602 /* If a line_offset wasn't found (VAR is the name of a user
2603 variable/function), then skip to normal symbol processing. */
00e52e53 2604 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1 2605 {
40e084e1
KS
2606 /* Consume this token. */
2607 linespec_lexer_consume_token (parser);
dcf9f4ab 2608
40e084e1 2609 goto convert_to_sals;
50641945 2610 }
40e084e1 2611 }
c45ec17c
PA
2612 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
2613 {
2614 /* Let the default linespec_complete_what::FUNCTION kick in. */
2615 unexpected_linespec_error (parser);
2616 }
40e084e1 2617 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
c45ec17c
PA
2618 {
2619 parser->complete_what = linespec_complete_what::NOTHING;
2620 unexpected_linespec_error (parser);
2621 }
50641945 2622
40e084e1
KS
2623 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2624 this token cannot represent a filename. */
2625 token = linespec_lexer_peek_token (parser);
0e0b460e 2626
40e084e1 2627 if (token.type == LSTOKEN_COLON)
0e0b460e 2628 {
40e084e1
KS
2629 /* Get the current token again and extract the filename. */
2630 token = linespec_lexer_lex_one (parser);
a5b5adf5 2631 gdb::unique_xmalloc_ptr<char> user_filename = copy_token_string (token);
50641945 2632
40e084e1 2633 /* Check if the input is a filename. */
a70b8144 2634 try
40e084e1 2635 {
4717cec4 2636 *PARSER_RESULT (parser)->file_symtabs
a5b5adf5 2637 = symtabs_from_filename (user_filename.get (),
c2f4122d 2638 PARSER_STATE (parser)->search_pspace);
40e084e1 2639 }
94aeb44b 2640 catch (gdb_exception_error &ex)
7556d4a4 2641 {
94aeb44b 2642 file_exception = std::move (ex);
7556d4a4 2643 }
50641945 2644
40e084e1
KS
2645 if (file_exception.reason >= 0)
2646 {
2647 /* Symtabs were found for the file. Record the filename. */
a5b5adf5 2648 PARSER_EXPLICIT (parser)->source_filename = user_filename.release ();
f8eba3c6 2649
40e084e1
KS
2650 /* Get the next token. */
2651 token = linespec_lexer_consume_token (parser);
50641945 2652
40e084e1
KS
2653 /* This is LSTOKEN_COLON; consume it. */
2654 linespec_lexer_consume_token (parser);
2655 }
2656 else
2657 {
40e084e1 2658 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2a908241 2659 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
40e084e1 2660 }
50641945 2661 }
40e084e1 2662 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
c45ec17c
PA
2663 else if (parser->completion_tracker == NULL
2664 && (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2665 && token.type != LSTOKEN_COMMA))
d2630e69 2666 {
40e084e1
KS
2667 /* TOKEN is the _next_ token, not the one currently in the parser.
2668 Consuming the token will give the correct error message. */
2669 linespec_lexer_consume_token (parser);
2670 unexpected_linespec_error (parser);
d2630e69 2671 }
50641945
FN
2672 else
2673 {
40e084e1 2674 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2a908241 2675 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
50641945 2676 }
50641945 2677
40e084e1
KS
2678 /* Parse the rest of the linespec. */
2679 linespec_parse_basic (parser);
50641945 2680
c45ec17c
PA
2681 if (parser->completion_tracker == NULL
2682 && PARSER_RESULT (parser)->function_symbols == NULL
40e084e1 2683 && PARSER_RESULT (parser)->labels.label_symbols == NULL
00e52e53 2684 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
40e084e1 2685 && PARSER_RESULT (parser)->minimal_symbols == NULL)
f8eba3c6 2686 {
40e084e1
KS
2687 /* The linespec didn't parse. Re-throw the file exception if
2688 there was one. */
2689 if (file_exception.reason < 0)
94aeb44b 2690 throw_exception (std::move (file_exception));
0f5238ed 2691
40e084e1 2692 /* Otherwise, the symbol is not found. */
00e52e53
KS
2693 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2694 PARSER_EXPLICIT (parser)->source_filename);
0f5238ed
TT
2695 }
2696
40e084e1 2697 convert_to_sals:
9ef07c8c 2698
40e084e1
KS
2699 /* Get the last token and record how much of the input was parsed,
2700 if necessary. */
2701 token = linespec_lexer_lex_one (parser);
2702 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
c45ec17c
PA
2703 unexpected_linespec_error (parser);
2704 else if (token.type == LSTOKEN_KEYWORD)
2705 {
2706 /* Setup the completion word past the keyword. Lexing never
2707 advances past a keyword automatically, so skip it
2708 manually. */
2709 parser->completion_word
f1735a53 2710 = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
c45ec17c
PA
2711 parser->complete_what = linespec_complete_what::EXPRESSION;
2712 }
50641945 2713
40e084e1 2714 /* Convert the data in PARSER_RESULT to SALs. */
c45ec17c 2715 if (parser->completion_tracker == NULL)
6c5b2ebe
PA
2716 return convert_linespec_to_sals (PARSER_STATE (parser),
2717 PARSER_RESULT (parser));
f8eba3c6 2718
6c5b2ebe 2719 return {};
413dad4d 2720}
50641945 2721
40e084e1 2722
f8eba3c6 2723/* A constructor for linespec_state. */
44fe14ab 2724
f8eba3c6
TT
2725static void
2726linespec_state_constructor (struct linespec_state *self,
40e084e1 2727 int flags, const struct language_defn *language,
c2f4122d 2728 struct program_space *search_pspace,
f8eba3c6
TT
2729 struct symtab *default_symtab,
2730 int default_line,
2731 struct linespec_result *canonical)
2732{
2733 memset (self, 0, sizeof (*self));
40e084e1 2734 self->language = language;
f8eba3c6
TT
2735 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2736 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
c2f4122d 2737 self->search_pspace = search_pspace;
f8eba3c6
TT
2738 self->default_symtab = default_symtab;
2739 self->default_line = default_line;
2740 self->canonical = canonical;
2741 self->program_space = current_program_space;
2742 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2743 xfree, xcalloc, xfree);
00e52e53 2744 self->is_linespec = 0;
f8eba3c6 2745}
44fe14ab 2746
40e084e1 2747/* Initialize a new linespec parser. */
44fe14ab 2748
3a6ae42d
TT
2749linespec_parser::linespec_parser (int flags,
2750 const struct language_defn *language,
2751 struct program_space *search_pspace,
2752 struct symtab *default_symtab,
2753 int default_line,
2754 struct linespec_result *canonical)
2755{
2756 lexer.current.type = LSTOKEN_CONSUMED;
2757 PARSER_RESULT (this)->file_symtabs = new std::vector<symtab *> ();
2758 PARSER_EXPLICIT (this)->func_name_match_type
a20714ff 2759 = symbol_name_match_type::WILD;
3a6ae42d
TT
2760 PARSER_EXPLICIT (this)->line_offset.sign = LINE_OFFSET_UNKNOWN;
2761 linespec_state_constructor (PARSER_STATE (this), flags, language,
c2f4122d 2762 search_pspace,
40e084e1
KS
2763 default_symtab, default_line, canonical);
2764}
2765
2766/* A destructor for linespec_state. */
44fe14ab 2767
40e084e1
KS
2768static void
2769linespec_state_destructor (struct linespec_state *self)
2770{
f8eba3c6 2771 htab_delete (self->addr_set);
1055a3b4 2772 xfree (self->canonical_names);
f8eba3c6 2773}
44fe14ab 2774
40e084e1
KS
2775/* Delete a linespec parser. */
2776
3a6ae42d 2777linespec_parser::~linespec_parser ()
40e084e1 2778{
3a6ae42d
TT
2779 xfree (PARSER_EXPLICIT (this)->source_filename);
2780 xfree (PARSER_EXPLICIT (this)->label_name);
2781 xfree (PARSER_EXPLICIT (this)->function_name);
40e084e1 2782
3a6ae42d
TT
2783 delete PARSER_RESULT (this)->file_symtabs;
2784 delete PARSER_RESULT (this)->function_symbols;
2785 delete PARSER_RESULT (this)->minimal_symbols;
2786 delete PARSER_RESULT (this)->labels.label_symbols;
2787 delete PARSER_RESULT (this)->labels.function_symbols;
40e084e1 2788
3a6ae42d 2789 linespec_state_destructor (PARSER_STATE (this));
40e084e1
KS
2790}
2791
c7c1b3e9
KS
2792/* See description in linespec.h. */
2793
2794void
f2fc3015 2795linespec_lex_to_end (const char **stringp)
c7c1b3e9 2796{
c7c1b3e9 2797 linespec_token token;
c7c1b3e9
KS
2798 const char *orig;
2799
2800 if (stringp == NULL || *stringp == NULL)
2801 return;
2802
3a6ae42d 2803 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
c7c1b3e9
KS
2804 parser.lexer.saved_arg = *stringp;
2805 PARSER_STREAM (&parser) = orig = *stringp;
2806
2807 do
2808 {
2809 /* Stop before any comma tokens; we need it to keep it
2810 as the next token in the string. */
2811 token = linespec_lexer_peek_token (&parser);
2812 if (token.type == LSTOKEN_COMMA)
2813 break;
c7c1b3e9
KS
2814 token = linespec_lexer_consume_token (&parser);
2815 }
2816 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2817
2818 *stringp += PARSER_STREAM (&parser) - orig;
c7c1b3e9
KS
2819}
2820
c6756f62
PA
2821/* See linespec.h. */
2822
2823void
2824linespec_complete_function (completion_tracker &tracker,
2825 const char *function,
a20714ff 2826 symbol_name_match_type func_match_type,
c6756f62
PA
2827 const char *source_filename)
2828{
2829 complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
2830
2831 if (source_filename != NULL)
2832 {
b5ec771e
PA
2833 collect_file_symbol_completion_matches (tracker, mode, func_match_type,
2834 function, function, source_filename);
c6756f62
PA
2835 }
2836 else
b5ec771e
PA
2837 {
2838 collect_symbol_completion_matches (tracker, mode, func_match_type,
2839 function, function);
2840
2841 }
c6756f62
PA
2842}
2843
c45ec17c
PA
2844/* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2845 only meaningful if COMPONENT is FUNCTION. */
2846
2847static void
2848complete_linespec_component (linespec_parser *parser,
2849 completion_tracker &tracker,
2850 const char *text,
2851 linespec_complete_what component,
2852 const char *source_filename)
2853{
2854 if (component == linespec_complete_what::KEYWORD)
2855 {
2856 complete_on_enum (tracker, linespec_keywords, text, text);
2857 }
2858 else if (component == linespec_complete_what::EXPRESSION)
2859 {
2860 const char *word
2861 = advance_to_expression_complete_word_point (tracker, text);
2862 complete_expression (tracker, text, word);
2863 }
2864 else if (component == linespec_complete_what::FUNCTION)
2865 {
2866 completion_list fn_list;
2867
a20714ff
PA
2868 symbol_name_match_type match_type
2869 = PARSER_EXPLICIT (parser)->func_name_match_type;
2870 linespec_complete_function (tracker, text, match_type, source_filename);
c45ec17c
PA
2871 if (source_filename == NULL)
2872 {
2873 /* Haven't seen a source component, like in "b
2874 file.c:function[TAB]". Maybe this wasn't a function, but
2875 a filename instead, like "b file.[TAB]". */
2876 fn_list = complete_source_filenames (text);
2877 }
2878
2879 /* If we only have a single filename completion, append a ':' for
2880 the user, since that's the only thing that can usefully follow
2881 the filename. */
2882 if (fn_list.size () == 1 && !tracker.have_completions ())
2883 {
2884 char *fn = fn_list[0].release ();
2885
2886 /* If we also need to append a quote char, it needs to be
2887 appended before the ':'. Append it now, and make ':' the
2888 new "quote" char. */
2889 if (tracker.quote_char ())
2890 {
896a7aa6 2891 char quote_char_str[2] = { (char) tracker.quote_char () };
c45ec17c
PA
2892
2893 fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
2894 tracker.set_quote_char (':');
2895 }
2896 else
2897 fn = reconcat (fn, fn, ":", (char *) NULL);
2898 fn_list[0].reset (fn);
2899
2900 /* Tell readline to skip appending a space. */
2901 tracker.set_suppress_append_ws (true);
2902 }
2903 tracker.add_completions (std::move (fn_list));
2904 }
2905}
2906
a2459270
PA
2907/* Helper for linespec_complete_label. Find labels that match
2908 LABEL_NAME in the function symbols listed in the PARSER, and add
2909 them to the tracker. */
2910
2911static void
2912complete_label (completion_tracker &tracker,
2913 linespec_parser *parser,
2914 const char *label_name)
2915{
7e41c8db
KS
2916 std::vector<block_symbol> label_function_symbols;
2917 std::vector<block_symbol> *labels
a2459270
PA
2918 = find_label_symbols (PARSER_STATE (parser),
2919 PARSER_RESULT (parser)->function_symbols,
2920 &label_function_symbols,
2921 label_name, true);
2922
3553eadc 2923 if (labels != nullptr)
a2459270 2924 {
3553eadc
KS
2925 for (const auto &label : *labels)
2926 {
987012b8 2927 char *match = xstrdup (label.symbol->search_name ());
3553eadc
KS
2928 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
2929 }
2930 delete labels;
a2459270 2931 }
a2459270
PA
2932}
2933
2934/* See linespec.h. */
2935
2936void
2937linespec_complete_label (completion_tracker &tracker,
2938 const struct language_defn *language,
2939 const char *source_filename,
2940 const char *function_name,
a20714ff 2941 symbol_name_match_type func_name_match_type,
a2459270
PA
2942 const char *label_name)
2943{
3a6ae42d 2944 linespec_parser parser (0, language, NULL, NULL, 0, NULL);
a2459270
PA
2945
2946 line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
2947
a70b8144 2948 try
a2459270
PA
2949 {
2950 convert_explicit_location_to_linespec (PARSER_STATE (&parser),
2951 PARSER_RESULT (&parser),
2952 source_filename,
2953 function_name,
a20714ff 2954 func_name_match_type,
a2459270
PA
2955 NULL, unknown_offset);
2956 }
230d2906 2957 catch (const gdb_exception_error &ex)
a2459270 2958 {
a2459270
PA
2959 return;
2960 }
a2459270
PA
2961
2962 complete_label (tracker, &parser, label_name);
a2459270
PA
2963}
2964
c45ec17c
PA
2965/* See description in linespec.h. */
2966
2967void
a20714ff
PA
2968linespec_complete (completion_tracker &tracker, const char *text,
2969 symbol_name_match_type match_type)
c45ec17c 2970{
c45ec17c
PA
2971 const char *orig = text;
2972
3a6ae42d 2973 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
c45ec17c 2974 parser.lexer.saved_arg = text;
a20714ff 2975 PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
c45ec17c
PA
2976 PARSER_STREAM (&parser) = text;
2977
2978 parser.completion_tracker = &tracker;
2979 PARSER_STATE (&parser)->is_linespec = 1;
2980
2981 /* Parse as much as possible. parser.completion_word will hold
2982 furthest completion point we managed to parse to. */
a70b8144 2983 try
c45ec17c 2984 {
a20714ff 2985 parse_linespec (&parser, text, match_type);
c45ec17c 2986 }
230d2906 2987 catch (const gdb_exception_error &except)
c45ec17c
PA
2988 {
2989 }
c45ec17c
PA
2990
2991 if (parser.completion_quote_char != '\0'
2992 && parser.completion_quote_end != NULL
2993 && parser.completion_quote_end[1] == '\0')
2994 {
2995 /* If completing a quoted string with the cursor right at
2996 terminating quote char, complete the completion word without
2997 interpretation, so that readline advances the cursor one
2998 whitespace past the quote, even if there's no match. This
2999 makes these cases behave the same:
3000
3001 before: "b function()"
3002 after: "b function() "
3003
3004 before: "b 'function()'"
3005 after: "b 'function()' "
3006
3007 and trusts the user in this case:
3008
3009 before: "b 'not_loaded_function_yet()'"
3010 after: "b 'not_loaded_function_yet()' "
3011 */
3012 parser.complete_what = linespec_complete_what::NOTHING;
3013 parser.completion_quote_char = '\0';
3014
3015 gdb::unique_xmalloc_ptr<char> text_copy
3016 (xstrdup (parser.completion_word));
3017 tracker.add_completion (std::move (text_copy));
3018 }
3019
3020 tracker.set_quote_char (parser.completion_quote_char);
3021
3022 if (parser.complete_what == linespec_complete_what::LABEL)
3023 {
3024 parser.complete_what = linespec_complete_what::NOTHING;
3025
3026 const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
3027
7e41c8db 3028 std::vector<block_symbol> function_symbols;
c2a031c5 3029 std::vector<bound_minimal_symbol> minimal_symbols;
c45ec17c
PA
3030 find_linespec_symbols (PARSER_STATE (&parser),
3031 PARSER_RESULT (&parser)->file_symtabs,
a20714ff 3032 func_name, match_type,
c45ec17c
PA
3033 &function_symbols, &minimal_symbols);
3034
7243d011 3035 PARSER_RESULT (&parser)->function_symbols
7e41c8db 3036 = new std::vector<block_symbol> (std::move (function_symbols));
c2a031c5
KS
3037 PARSER_RESULT (&parser)->minimal_symbols
3038 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
c45ec17c
PA
3039
3040 complete_label (tracker, &parser, parser.completion_word);
3041 }
3042 else if (parser.complete_what == linespec_complete_what::FUNCTION)
3043 {
3044 /* While parsing/lexing, we didn't know whether the completion
3045 word completes to a unique function/source name already or
3046 not.
3047
3048 E.g.:
3049 "b function() <tab>"
3050 may need to complete either to:
3051 "b function() const"
3052 or to:
3053 "b function() if/thread/task"
3054
3055 Or, this:
3056 "b foo t"
3057 may need to complete either to:
3058 "b foo template_fun<T>()"
3059 with "foo" being the template function's return type, or to:
3060 "b foo thread/task"
3061
3062 Or, this:
3063 "b file<TAB>"
3064 may need to complete either to a source file name:
3065 "b file.c"
3066 or this, also a filename, but a unique completion:
3067 "b file.c:"
3068 or to a function name:
3069 "b file_function"
3070
3071 Address that by completing assuming source or function, and
3072 seeing if we find a completion that matches exactly the
3073 completion word. If so, then it must be a function (see note
3074 below) and we advance the completion word to the end of input
3075 and switch to KEYWORD completion mode.
3076
3077 Note: if we find a unique completion for a source filename,
3078 then it won't match the completion word, because the LCD will
3079 contain a trailing ':'. And if we're completing at or after
3080 the ':', then complete_linespec_component won't try to
3081 complete on source filenames. */
3082
c45ec17c
PA
3083 const char *word = parser.completion_word;
3084
3085 complete_linespec_component (&parser, tracker,
3086 parser.completion_word,
3087 linespec_complete_what::FUNCTION,
3088 PARSER_EXPLICIT (&parser)->source_filename);
3089
3090 parser.complete_what = linespec_complete_what::NOTHING;
3091
3092 if (tracker.quote_char ())
3093 {
3094 /* The function/file name was not close-quoted, so this
3095 can't be a keyword. Note: complete_linespec_component
3096 may have swapped the original quote char for ':' when we
3097 get here, but that still indicates the same. */
3098 }
3099 else if (!tracker.have_completions ())
3100 {
3101 size_t key_start;
3102 size_t wordlen = strlen (parser.completion_word);
3103
3104 key_start
3105 = string_find_incomplete_keyword_at_end (linespec_keywords,
3106 parser.completion_word,
3107 wordlen);
3108
3109 if (key_start != -1
3110 || (wordlen > 0
3111 && parser.completion_word[wordlen - 1] == ' '))
3112 {
3113 parser.completion_word += key_start;
3114 parser.complete_what = linespec_complete_what::KEYWORD;
3115 }
3116 }
3117 else if (tracker.completes_to_completion_word (word))
3118 {
3119 /* Skip the function and complete on keywords. */
3120 parser.completion_word += strlen (word);
3121 parser.complete_what = linespec_complete_what::KEYWORD;
3122 tracker.discard_completions ();
3123 }
3124 }
3125
3126 tracker.advance_custom_word_point_by (parser.completion_word - orig);
3127
3128 complete_linespec_component (&parser, tracker,
3129 parser.completion_word,
3130 parser.complete_what,
3131 PARSER_EXPLICIT (&parser)->source_filename);
3132
3133 /* If we're past the "filename:function:label:offset" linespec, and
3134 didn't find any match, then assume the user might want to create
3135 a pending breakpoint anyway and offer the keyword
3136 completions. */
3137 if (!parser.completion_quote_char
3138 && (parser.complete_what == linespec_complete_what::FUNCTION
3139 || parser.complete_what == linespec_complete_what::LABEL
3140 || parser.complete_what == linespec_complete_what::NOTHING)
3141 && !tracker.have_completions ())
3142 {
3143 const char *end
3144 = parser.completion_word + strlen (parser.completion_word);
3145
3146 if (end > orig && end[-1] == ' ')
3147 {
3148 tracker.advance_custom_word_point_by (end - parser.completion_word);
3149
3150 complete_linespec_component (&parser, tracker, end,
3151 linespec_complete_what::KEYWORD,
3152 NULL);
3153 }
3154 }
c45ec17c
PA
3155}
3156
f00aae0f 3157/* A helper function for decode_line_full and decode_line_1 to
6c5b2ebe 3158 turn LOCATION into std::vector<symtab_and_line>. */
f00aae0f 3159
6c5b2ebe 3160static std::vector<symtab_and_line>
f00aae0f
KS
3161event_location_to_sals (linespec_parser *parser,
3162 const struct event_location *location)
3163{
6c5b2ebe 3164 std::vector<symtab_and_line> result;
f00aae0f
KS
3165
3166 switch (event_location_type (location))
3167 {
3168 case LINESPEC_LOCATION:
3169 {
00e52e53 3170 PARSER_STATE (parser)->is_linespec = 1;
a70b8144 3171 try
f00aae0f 3172 {
a20714ff
PA
3173 const linespec_location *ls = get_linespec_location (location);
3174 result = parse_linespec (parser,
3175 ls->spec_string, ls->match_type);
f00aae0f 3176 }
230d2906 3177 catch (const gdb_exception_error &except)
f00aae0f 3178 {
eedc3f4f 3179 throw;
f00aae0f 3180 }
f00aae0f
KS
3181 }
3182 break;
3183
a06efdd6 3184 case ADDRESS_LOCATION:
305e13e6
JB
3185 {
3186 const char *addr_string = get_address_string_location (location);
3187 CORE_ADDR addr = get_address_location (location);
3188
3189 if (addr_string != NULL)
3190 {
9be2c17a 3191 addr = linespec_expression_to_pc (&addr_string);
305e13e6
JB
3192 if (PARSER_STATE (parser)->canonical != NULL)
3193 PARSER_STATE (parser)->canonical->location
8e9e35b1 3194 = copy_event_location (location);
305e13e6
JB
3195 }
3196
3197 result = convert_address_location_to_sals (PARSER_STATE (parser),
3198 addr);
3199 }
a06efdd6
KS
3200 break;
3201
00e52e53
KS
3202 case EXPLICIT_LOCATION:
3203 {
67994074 3204 const struct explicit_location *explicit_loc;
00e52e53 3205
67994074 3206 explicit_loc = get_explicit_location_const (location);
00e52e53
KS
3207 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
3208 PARSER_RESULT (parser),
67994074 3209 explicit_loc);
00e52e53
KS
3210 }
3211 break;
3212
5b56227b
KS
3213 case PROBE_LOCATION:
3214 /* Probes are handled by their own decoders. */
3215 gdb_assert_not_reached ("attempt to decode probe location");
3216 break;
3217
f00aae0f
KS
3218 default:
3219 gdb_assert_not_reached ("unhandled event location type");
3220 }
3221
3222 return result;
3223}
3224
f8eba3c6 3225/* See linespec.h. */
44fe14ab 3226
f8eba3c6 3227void
626d2320 3228decode_line_full (struct event_location *location, int flags,
c2f4122d 3229 struct program_space *search_pspace,
f8eba3c6
TT
3230 struct symtab *default_symtab,
3231 int default_line, struct linespec_result *canonical,
3232 const char *select_mode,
3233 const char *filter)
44fe14ab 3234{
f73c6ece 3235 std::vector<const char *> filters;
40e084e1 3236 struct linespec_state *state;
f8eba3c6
TT
3237
3238 gdb_assert (canonical != NULL);
3239 /* The filter only makes sense for 'all'. */
3240 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
3241 gdb_assert (select_mode == NULL
3242 || select_mode == multiple_symbols_all
3243 || select_mode == multiple_symbols_ask
3244 || select_mode == multiple_symbols_cancel);
3245 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
3246
3a6ae42d
TT
3247 linespec_parser parser (flags, current_language,
3248 search_pspace, default_symtab,
3249 default_line, canonical);
5ed8105e
PA
3250
3251 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3252
6c5b2ebe
PA
3253 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3254 location);
40e084e1 3255 state = PARSER_STATE (&parser);
f8eba3c6 3256
626d2320
TV
3257 if (result.size () == 0)
3258 throw_error (NOT_SUPPORTED_ERROR, _("Location %s not available"),
3259 event_location_to_string (location));
3260
6c5b2ebe 3261 gdb_assert (result.size () == 1 || canonical->pre_expanded);
f8eba3c6
TT
3262 canonical->pre_expanded = 1;
3263
66f1999b 3264 /* Arrange for allocated canonical names to be freed. */
3a6ae42d
TT
3265 std::vector<gdb::unique_xmalloc_ptr<char>> hold_names;
3266 for (int i = 0; i < result.size (); ++i)
f8eba3c6 3267 {
3a6ae42d
TT
3268 gdb_assert (state->canonical_names[i].suffix != NULL);
3269 hold_names.emplace_back (state->canonical_names[i].suffix);
f8eba3c6
TT
3270 }
3271
3272 if (select_mode == NULL)
3273 {
29f94340 3274 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
f8eba3c6
TT
3275 select_mode = multiple_symbols_all;
3276 else
3277 select_mode = multiple_symbols_select_mode ();
3278 }
3279
3280 if (select_mode == multiple_symbols_all)
3281 {
3282 if (filter != NULL)
3283 {
f73c6ece 3284 filters.push_back (filter);
40e084e1 3285 filter_results (state, &result, filters);
f8eba3c6
TT
3286 }
3287 else
40e084e1 3288 convert_results_to_lsals (state, &result);
f8eba3c6
TT
3289 }
3290 else
40e084e1 3291 decode_line_2 (state, &result, select_mode);
f8eba3c6
TT
3292}
3293
39cf75f7
DE
3294/* See linespec.h. */
3295
6c5b2ebe 3296std::vector<symtab_and_line>
f00aae0f 3297decode_line_1 (const struct event_location *location, int flags,
c2f4122d 3298 struct program_space *search_pspace,
f8eba3c6
TT
3299 struct symtab *default_symtab,
3300 int default_line)
3301{
3a6ae42d
TT
3302 linespec_parser parser (flags, current_language,
3303 search_pspace, default_symtab,
3304 default_line, NULL);
5ed8105e
PA
3305
3306 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3307
3a6ae42d 3308 return event_location_to_sals (&parser, location);
f8eba3c6
TT
3309}
3310
39cf75f7
DE
3311/* See linespec.h. */
3312
6c5b2ebe 3313std::vector<symtab_and_line>
f2fc3015 3314decode_line_with_current_source (const char *string, int flags)
39cf75f7 3315{
39cf75f7
DE
3316 if (string == 0)
3317 error (_("Empty line specification."));
3318
3319 /* We use whatever is set as the current source line. We do not try
3320 and get a default source symtab+line or it will recursively call us! */
6c5b2ebe 3321 symtab_and_line cursal = get_current_source_symtab_and_line ();
39cf75f7 3322
ffc2605c
TT
3323 event_location_up location = string_to_event_location (&string,
3324 current_language);
6c5b2ebe
PA
3325 std::vector<symtab_and_line> sals
3326 = decode_line_1 (location.get (), flags, NULL, cursal.symtab, cursal.line);
39cf75f7
DE
3327
3328 if (*string)
3329 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3330
39cf75f7
DE
3331 return sals;
3332}
3333
3334/* See linespec.h. */
3335
6c5b2ebe 3336std::vector<symtab_and_line>
f2fc3015 3337decode_line_with_last_displayed (const char *string, int flags)
39cf75f7 3338{
39cf75f7
DE
3339 if (string == 0)
3340 error (_("Empty line specification."));
3341
ffc2605c
TT
3342 event_location_up location = string_to_event_location (&string,
3343 current_language);
6c5b2ebe
PA
3344 std::vector<symtab_and_line> sals
3345 = (last_displayed_sal_is_valid ()
3346 ? decode_line_1 (location.get (), flags, NULL,
3347 get_last_displayed_symtab (),
3348 get_last_displayed_line ())
cafb3438 3349 : decode_line_1 (location.get (), flags, NULL, NULL, 0));
39cf75f7
DE
3350
3351 if (*string)
3352 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3353
39cf75f7
DE
3354 return sals;
3355}
3356
f8eba3c6
TT
3357\f
3358
85102364 3359/* First, some functions to initialize stuff at the beginning of the
f8eba3c6
TT
3360 function. */
3361
3362static void
3363initialize_defaults (struct symtab **default_symtab, int *default_line)
3364{
3365 if (*default_symtab == 0)
3366 {
3367 /* Use whatever we have for the default source line. We don't use
dda83cd7 3368 get_current_or_default_symtab_and_line as it can recurse and call
f8eba3c6
TT
3369 us back! */
3370 struct symtab_and_line cursal =
3371 get_current_source_symtab_and_line ();
3372
3373 *default_symtab = cursal.symtab;
3374 *default_line = cursal.line;
3375 }
3376}
3377
3378\f
3379
40e084e1
KS
3380/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3381 advancing EXP_PTR past any parsed text. */
f8eba3c6 3382
a06efdd6 3383CORE_ADDR
bbc13ae3 3384linespec_expression_to_pc (const char **exp_ptr)
f8eba3c6 3385{
f8eba3c6
TT
3386 if (current_program_space->executing_startup)
3387 /* The error message doesn't really matter, because this case
3388 should only hit during breakpoint reset. */
3389 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
3390 "program space is in startup"));
3391
40e084e1
KS
3392 (*exp_ptr)++;
3393 return value_as_address (parse_to_comma_and_eval (exp_ptr));
0960f083
DC
3394}
3395
3396\f
3397
d2630e69
AF
3398/* Here's where we recognise an Objective-C Selector. An Objective C
3399 selector may be implemented by more than one class, therefore it
3400 may represent more than one method/function. This gives us a
3401 situation somewhat analogous to C++ overloading. If there's more
3402 than one method that could represent the selector, then use some of
3403 the existing C++ code to let the user choose one. */
3404
6c5b2ebe 3405static std::vector<symtab_and_line>
f00aae0f 3406decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
d2630e69 3407{
f8eba3c6 3408 struct collect_info info;
9b2f8581 3409 std::vector<const char *> symbol_names;
d7561cbb 3410 const char *new_argptr;
f8eba3c6
TT
3411
3412 info.state = self;
2a908241
KS
3413 std::vector<symtab *> symtabs;
3414 symtabs.push_back (nullptr);
3415
3416 info.file_symtabs = &symtabs;
7243d011 3417
7e41c8db 3418 std::vector<block_symbol> symbols;
7243d011 3419 info.result.symbols = &symbols;
c2a031c5
KS
3420 std::vector<bound_minimal_symbol> minimal_symbols;
3421 info.result.minimal_symbols = &minimal_symbols;
f8eba3c6 3422
f00aae0f 3423 new_argptr = find_imps (arg, &symbol_names);
9b2f8581 3424 if (symbol_names.empty ())
2a908241 3425 return {};
d2630e69 3426
56d87ef7
PA
3427 add_all_symbol_names_from_pspace (&info, NULL, symbol_names,
3428 FUNCTIONS_DOMAIN);
d2630e69 3429
6c5b2ebe 3430 std::vector<symtab_and_line> values;
c2a031c5 3431 if (!symbols.empty () || !minimal_symbols.empty ())
d2630e69 3432 {
f8eba3c6 3433 char *saved_arg;
d2630e69 3434
224c3ddb 3435 saved_arg = (char *) alloca (new_argptr - arg + 1);
f00aae0f
KS
3436 memcpy (saved_arg, arg, new_argptr - arg);
3437 saved_arg[new_argptr - arg] = '\0';
d2630e69 3438
67994074 3439 ls->explicit_loc.function_name = xstrdup (saved_arg);
7e41c8db
KS
3440 ls->function_symbols
3441 = new std::vector<block_symbol> (std::move (symbols));
c2a031c5
KS
3442 ls->minimal_symbols
3443 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
40e084e1
KS
3444 values = convert_linespec_to_sals (self, ls);
3445
f8eba3c6 3446 if (self->canonical)
d2630e69 3447 {
f2fc3015
TT
3448 std::string holder;
3449 const char *str;
f00aae0f 3450
f8eba3c6 3451 self->canonical->pre_expanded = 1;
f00aae0f 3452
67994074 3453 if (ls->explicit_loc.source_filename)
f00aae0f 3454 {
f2fc3015
TT
3455 holder = string_printf ("%s:%s",
3456 ls->explicit_loc.source_filename,
3457 saved_arg);
3458 str = holder.c_str ();
f00aae0f 3459 }
f8eba3c6 3460 else
f2fc3015 3461 str = saved_arg;
f00aae0f 3462
a20714ff
PA
3463 self->canonical->location
3464 = new_linespec_location (&str, symbol_name_match_type::FULL);
d2630e69 3465 }
d2630e69
AF
3466 }
3467
40e084e1 3468 return values;
f8eba3c6 3469}
c00f8484 3470
ffdbe864
YQ
3471namespace {
3472
14bc53a8
PA
3473/* A function object that serves as symbol_found_callback_ftype
3474 callback for iterate_over_symbols. This is used by
3475 lookup_prefix_sym to collect type symbols. */
3476class decode_compound_collector
f8eba3c6 3477{
14bc53a8 3478public:
fc4007c9 3479 decode_compound_collector ()
7a8a5d47
TT
3480 : m_unique_syms (htab_create_alloc (1, htab_hash_pointer,
3481 htab_eq_pointer, NULL,
3482 xcalloc, xfree))
fc4007c9 3483 {
fc4007c9 3484 }
3a93a0c2 3485
4dedf84d 3486 /* Return all symbols collected. */
7e41c8db 3487 std::vector<block_symbol> release_symbols ()
14bc53a8 3488 {
4dedf84d 3489 return std::move (m_symbols);
14bc53a8 3490 }
c00f8484 3491
14bc53a8 3492 /* Callable as a symbol_found_callback_ftype callback. */
7e41c8db 3493 bool operator () (block_symbol *bsym);
14bc53a8
PA
3494
3495private:
3496 /* A hash table of all symbols we found. We use this to avoid
3497 adding any symbol more than once. */
7a8a5d47 3498 htab_up m_unique_syms;
14bc53a8
PA
3499
3500 /* The result vector. */
7e41c8db 3501 std::vector<block_symbol> m_symbols;
14bc53a8
PA
3502};
3503
3504bool
7e41c8db 3505decode_compound_collector::operator () (block_symbol *bsym)
f8eba3c6 3506{
f8eba3c6
TT
3507 void **slot;
3508 struct type *t;
7e41c8db 3509 struct symbol *sym = bsym->symbol;
614b3b14 3510
f8eba3c6 3511 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
14bc53a8 3512 return true; /* Continue iterating. */
f8eba3c6
TT
3513
3514 t = SYMBOL_TYPE (sym);
f168693b 3515 t = check_typedef (t);
78134374
SM
3516 if (t->code () != TYPE_CODE_STRUCT
3517 && t->code () != TYPE_CODE_UNION
3518 && t->code () != TYPE_CODE_NAMESPACE)
14bc53a8 3519 return true; /* Continue iterating. */
614b3b14 3520
7a8a5d47 3521 slot = htab_find_slot (m_unique_syms.get (), sym, INSERT);
f8eba3c6
TT
3522 if (!*slot)
3523 {
3524 *slot = sym;
7e41c8db 3525 m_symbols.push_back (*bsym);
f8eba3c6
TT
3526 }
3527
14bc53a8 3528 return true; /* Continue iterating. */
f8eba3c6 3529}
93d91629 3530
ffdbe864
YQ
3531} // namespace
3532
40e084e1 3533/* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
93d91629 3534
7e41c8db 3535static std::vector<block_symbol>
2a908241
KS
3536lookup_prefix_sym (struct linespec_state *state,
3537 std::vector<symtab *> *file_symtabs,
40e084e1 3538 const char *class_name)
93d91629 3539{
14bc53a8 3540 decode_compound_collector collector;
e0881a8e 3541
b5ec771e
PA
3542 lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
3543
2a908241 3544 for (const auto &elt : *file_symtabs)
f8eba3c6 3545 {
2a908241 3546 if (elt == nullptr)
f8eba3c6 3547 {
b5ec771e 3548 iterate_over_all_matching_symtabs (state, lookup_name,
56d87ef7
PA
3549 STRUCT_DOMAIN, ALL_DOMAIN,
3550 NULL, false, collector);
b5ec771e 3551 iterate_over_all_matching_symtabs (state, lookup_name,
56d87ef7
PA
3552 VAR_DOMAIN, ALL_DOMAIN,
3553 NULL, false, collector);
f8eba3c6
TT
3554 }
3555 else
3556 {
f8eba3c6
TT
3557 /* Program spaces that are executing startup should have
3558 been filtered out earlier. */
3559 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3560 set_current_program_space (SYMTAB_PSPACE (elt));
b5ec771e
PA
3561 iterate_over_file_blocks (elt, lookup_name, STRUCT_DOMAIN, collector);
3562 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN, collector);
1e5a1abc
KS
3563 }
3564 }
3565
14bc53a8 3566 return collector.release_symbols ();
93d91629
DC
3567}
3568
7243d011
KS
3569/* A std::sort comparison function for symbols. The resulting order does
3570 not actually matter; we just need to be able to sort them so that
3571 symbols with the same program space end up next to each other. */
3572
3573static bool
7e41c8db 3574compare_symbols (const block_symbol &a, const block_symbol &b)
7243d011
KS
3575{
3576 uintptr_t uia, uib;
3577
7e41c8db
KS
3578 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (a.symbol));
3579 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (b.symbol));
7243d011
KS
3580
3581 if (uia < uib)
3582 return true;
3583 if (uia > uib)
3584 return false;
3585
7e41c8db
KS
3586 uia = (uintptr_t) a.symbol;
3587 uib = (uintptr_t) b.symbol;
7243d011
KS
3588
3589 if (uia < uib)
3590 return true;
3591
3592 return false;
3593}
3594
40e084e1 3595/* Like compare_symbols but for minimal symbols. */
4224873a 3596
c2a031c5
KS
3597static bool
3598compare_msymbols (const bound_minimal_symbol &a, const bound_minimal_symbol &b)
4224873a 3599{
f8eba3c6
TT
3600 uintptr_t uia, uib;
3601
c2a031c5
KS
3602 uia = (uintptr_t) a.objfile->pspace;
3603 uib = (uintptr_t) a.objfile->pspace;
f8eba3c6
TT
3604
3605 if (uia < uib)
c2a031c5 3606 return true;
f8eba3c6 3607 if (uia > uib)
c2a031c5 3608 return false;
f8eba3c6 3609
c2a031c5
KS
3610 uia = (uintptr_t) a.minsym;
3611 uib = (uintptr_t) b.minsym;
f8eba3c6
TT
3612
3613 if (uia < uib)
c2a031c5 3614 return true;
f8eba3c6 3615
c2a031c5 3616 return false;
f8eba3c6
TT
3617}
3618
3619/* Look for all the matching instances of each symbol in NAMES. Only
3620 instances from PSPACE are considered; other program spaces are
3621 handled by our caller. If PSPACE is NULL, then all program spaces
3622 are considered. Results are stored into INFO. */
3623
3624static void
3625add_all_symbol_names_from_pspace (struct collect_info *info,
3626 struct program_space *pspace,
9b2f8581 3627 const std::vector<const char *> &names,
56d87ef7 3628 enum search_domain search_domain)
f8eba3c6 3629{
9b2f8581 3630 for (const char *iter : names)
b5ec771e
PA
3631 add_matching_symbols_to_info (iter,
3632 symbol_name_match_type::FULL,
56d87ef7 3633 search_domain, info, pspace);
f8eba3c6
TT
3634}
3635
3636static void
8e8d776e 3637find_superclass_methods (std::vector<struct type *> &&superclasses,
b5ec771e 3638 const char *name, enum language name_lang,
9b2f8581 3639 std::vector<const char *> *result_names)
f8eba3c6 3640{
9b2f8581 3641 size_t old_len = result_names->size ();
f8eba3c6 3642
f8eba3c6
TT
3643 while (1)
3644 {
8e8d776e 3645 std::vector<struct type *> new_supers;
f8eba3c6 3646
52941706 3647 for (type *t : superclasses)
b5ec771e 3648 find_methods (t, name_lang, name, result_names, &new_supers);
f8eba3c6 3649
8e8d776e 3650 if (result_names->size () != old_len || new_supers.empty ())
f8eba3c6 3651 break;
4224873a 3652
8e8d776e 3653 superclasses = std::move (new_supers);
f8eba3c6 3654 }
f8eba3c6
TT
3655}
3656
40e084e1
KS
3657/* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3658 given by one of the symbols in SYM_CLASSES. Matches are returned
3659 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
f8eba3c6 3660
40e084e1 3661static void
2a908241 3662find_method (struct linespec_state *self, std::vector<symtab *> *file_symtabs,
40e084e1 3663 const char *class_name, const char *method_name,
7e41c8db
KS
3664 std::vector<block_symbol> *sym_classes,
3665 std::vector<block_symbol> *symbols,
c2a031c5 3666 std::vector<bound_minimal_symbol> *minsyms)
f8eba3c6 3667{
9b2f8581 3668 size_t last_result_len;
8e8d776e 3669 std::vector<struct type *> superclass_vec;
9b2f8581 3670 std::vector<const char *> result_names;
f8eba3c6 3671 struct collect_info info;
4224873a 3672
f8eba3c6
TT
3673 /* Sort symbols so that symbols with the same program space are next
3674 to each other. */
4dedf84d
KS
3675 std::sort (sym_classes->begin (), sym_classes->end (),
3676 compare_symbols);
f8eba3c6
TT
3677
3678 info.state = self;
40e084e1 3679 info.file_symtabs = file_symtabs;
7243d011 3680 info.result.symbols = symbols;
c2a031c5 3681 info.result.minimal_symbols = minsyms;
f8eba3c6
TT
3682
3683 /* Iterate over all the types, looking for the names of existing
40e084e1 3684 methods matching METHOD_NAME. If we cannot find a direct method in a
f8eba3c6
TT
3685 given program space, then we consider inherited methods; this is
3686 not ideal (ideal would be to respect C++ hiding rules), but it
3687 seems good enough and is what GDB has historically done. We only
3688 need to collect the names because later we find all symbols with
3689 those names. This loop is written in a somewhat funny way
3690 because we collect data across the program space before deciding
3691 what to do. */
f8eba3c6 3692 last_result_len = 0;
7e41c8db 3693 for (const auto &elt : *sym_classes)
f8eba3c6
TT
3694 {
3695 struct type *t;
3696 struct program_space *pspace;
7e41c8db 3697 struct symbol *sym = elt.symbol;
ea90f227 3698 unsigned int ix = &elt - &*sym_classes->begin ();
f8eba3c6
TT
3699
3700 /* Program spaces that are executing startup should have
3701 been filtered out earlier. */
08be3fe3
DE
3702 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
3703 gdb_assert (!pspace->executing_startup);
f8eba3c6
TT
3704 set_current_program_space (pspace);
3705 t = check_typedef (SYMBOL_TYPE (sym));
c1b5c1eb 3706 find_methods (t, sym->language (),
b5ec771e 3707 method_name, &result_names, &superclass_vec);
f8eba3c6
TT
3708
3709 /* Handle all items from a single program space at once; and be
3710 sure not to miss the last batch. */
4dedf84d 3711 if (ix == sym_classes->size () - 1
f8eba3c6 3712 || (pspace
7e41c8db 3713 != SYMTAB_PSPACE (symbol_symtab (sym_classes->at (ix + 1).symbol))))
4224873a 3714 {
f8eba3c6
TT
3715 /* If we did not find a direct implementation anywhere in
3716 this program space, consider superclasses. */
9b2f8581 3717 if (result_names.size () == last_result_len)
8e8d776e 3718 find_superclass_methods (std::move (superclass_vec), method_name,
c1b5c1eb 3719 sym->language (), &result_names);
f8eba3c6
TT
3720
3721 /* We have a list of candidate symbol names, so now we
3722 iterate over the symbol tables looking for all
3723 matches in this pspace. */
56d87ef7
PA
3724 add_all_symbol_names_from_pspace (&info, pspace, result_names,
3725 FUNCTIONS_DOMAIN);
f8eba3c6 3726
8e8d776e 3727 superclass_vec.clear ();
9b2f8581 3728 last_result_len = result_names.size ();
4224873a 3729 }
4224873a 3730 }
f8eba3c6 3731
c2a031c5
KS
3732 if (!symbols->empty () || !minsyms->empty ())
3733 return;
f8eba3c6 3734
40e084e1
KS
3735 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3736 and other attempts to locate the symbol will be made. */
3737 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
f8eba3c6
TT
3738}
3739
3740\f
3741
ffdbe864
YQ
3742namespace {
3743
14bc53a8
PA
3744/* This function object is a callback for iterate_over_symtabs, used
3745 when collecting all matching symtabs. */
f8eba3c6 3746
14bc53a8 3747class symtab_collector
f8eba3c6 3748{
14bc53a8 3749public:
fc4007c9 3750 symtab_collector ()
7a8a5d47
TT
3751 : m_symtab_table (htab_create (1, htab_hash_pointer, htab_eq_pointer,
3752 NULL))
fc4007c9 3753 {
fc4007c9 3754 }
f8eba3c6 3755
14bc53a8
PA
3756 /* Callable as a symbol_found_callback_ftype callback. */
3757 bool operator () (symtab *sym);
f8eba3c6 3758
4717cec4
SM
3759 /* Return an rvalue reference to the collected symtabs. */
3760 std::vector<symtab *> &&release_symtabs ()
14bc53a8 3761 {
2a908241 3762 return std::move (m_symtabs);
14bc53a8
PA
3763 }
3764
3765private:
3766 /* The result vector of symtabs. */
4717cec4 3767 std::vector<symtab *> m_symtabs;
14bc53a8
PA
3768
3769 /* This is used to ensure the symtabs are unique. */
7a8a5d47 3770 htab_up m_symtab_table;
14bc53a8
PA
3771};
3772
3773bool
3774symtab_collector::operator () (struct symtab *symtab)
f8eba3c6 3775{
f8eba3c6
TT
3776 void **slot;
3777
7a8a5d47 3778 slot = htab_find_slot (m_symtab_table.get (), symtab, INSERT);
f8eba3c6 3779 if (!*slot)
4224873a 3780 {
f8eba3c6 3781 *slot = symtab;
4717cec4 3782 m_symtabs.push_back (symtab);
4224873a 3783 }
f8eba3c6 3784
14bc53a8 3785 return false;
4224873a
DC
3786}
3787
ffdbe864
YQ
3788} // namespace
3789
2a908241 3790/* Given a file name, return a list of all matching symtabs. If
c2f4122d
PA
3791 SEARCH_PSPACE is not NULL, the search is restricted to just that
3792 program space. */
f8eba3c6 3793
4717cec4 3794static std::vector<symtab *>
c2f4122d
PA
3795collect_symtabs_from_filename (const char *file,
3796 struct program_space *search_pspace)
f8eba3c6 3797{
14bc53a8 3798 symtab_collector collector;
f8eba3c6
TT
3799
3800 /* Find that file's data. */
c2f4122d
PA
3801 if (search_pspace == NULL)
3802 {
94c93c35 3803 for (struct program_space *pspace : program_spaces)
dda83cd7 3804 {
c2f4122d
PA
3805 if (pspace->executing_startup)
3806 continue;
f8eba3c6 3807
c2f4122d 3808 set_current_program_space (pspace);
14bc53a8 3809 iterate_over_symtabs (file, collector);
c2f4122d
PA
3810 }
3811 }
3812 else
3813 {
3814 set_current_program_space (search_pspace);
14bc53a8 3815 iterate_over_symtabs (file, collector);
c2f4122d 3816 }
f3c39e76 3817
14bc53a8 3818 return collector.release_symtabs ();
f8eba3c6
TT
3819}
3820
c2f4122d
PA
3821/* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3822 not NULL, the search is restricted to just that program space. */
f8eba3c6 3823
4717cec4 3824static std::vector<symtab *>
c2f4122d
PA
3825symtabs_from_filename (const char *filename,
3826 struct program_space *search_pspace)
40e084e1 3827{
4717cec4 3828 std::vector<symtab *> result
2a908241 3829 = collect_symtabs_from_filename (filename, search_pspace);
f8eba3c6 3830
4717cec4 3831 if (result.empty ())
f8eba3c6 3832 {
40e084e1
KS
3833 if (!have_full_symbols () && !have_partial_symbols ())
3834 throw_error (NOT_FOUND_ERROR,
3835 _("No symbol table is loaded. "
3836 "Use the \"file\" command."));
00e52e53 3837 source_file_not_found_error (filename);
f8eba3c6
TT
3838 }
3839
40e084e1 3840 return result;
84fba31b 3841}
f3c39e76 3842
fcaad03c
KS
3843/* See symtab.h. */
3844
3845void
3846symbol_searcher::find_all_symbols (const std::string &name,
3847 const struct language_defn *language,
3848 enum search_domain search_domain,
3849 std::vector<symtab *> *search_symtabs,
3850 struct program_space *search_pspace)
3851{
3852 symbol_searcher_collect_info info;
3853 struct linespec_state state;
3854
3855 memset (&state, 0, sizeof (state));
3856 state.language = language;
3857 info.state = &state;
3858
3859 info.result.symbols = &m_symbols;
3860 info.result.minimal_symbols = &m_minimal_symbols;
3861 std::vector<symtab *> all_symtabs;
3862 if (search_symtabs == nullptr)
3863 {
3864 all_symtabs.push_back (nullptr);
3865 search_symtabs = &all_symtabs;
3866 }
3867 info.file_symtabs = search_symtabs;
3868
3869 add_matching_symbols_to_info (name.c_str (), symbol_name_match_type::WILD,
3870 search_domain, &info, search_pspace);
3871}
3872
40e084e1
KS
3873/* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3874 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3875 returned in MINSYMS. */
14e91ac5 3876
40e084e1
KS
3877static void
3878find_function_symbols (struct linespec_state *state,
2a908241 3879 std::vector<symtab *> *file_symtabs, const char *name,
a20714ff 3880 symbol_name_match_type name_match_type,
7e41c8db 3881 std::vector<block_symbol> *symbols,
c2a031c5 3882 std::vector<bound_minimal_symbol> *minsyms)
14e91ac5 3883{
40e084e1 3884 struct collect_info info;
9b2f8581 3885 std::vector<const char *> symbol_names;
14e91ac5 3886
40e084e1 3887 info.state = state;
7243d011 3888 info.result.symbols = symbols;
c2a031c5 3889 info.result.minimal_symbols = minsyms;
40e084e1 3890 info.file_symtabs = file_symtabs;
e0881a8e 3891
40e084e1 3892 /* Try NAME as an Objective-C selector. */
d7561cbb 3893 find_imps (name, &symbol_names);
9b2f8581 3894 if (!symbol_names.empty ())
c2f4122d 3895 add_all_symbol_names_from_pspace (&info, state->search_pspace,
56d87ef7 3896 symbol_names, FUNCTIONS_DOMAIN);
40e084e1 3897 else
a20714ff 3898 add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
b5ec771e 3899 &info, state->search_pspace);
40e084e1
KS
3900}
3901
3902/* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3903 in SYMBOLS and minimal symbols in MINSYMS. */
14e91ac5 3904
b1ae631a 3905static void
40e084e1 3906find_linespec_symbols (struct linespec_state *state,
2a908241 3907 std::vector<symtab *> *file_symtabs,
b5ec771e 3908 const char *lookup_name,
a20714ff 3909 symbol_name_match_type name_match_type,
7e41c8db 3910 std::vector <block_symbol> *symbols,
c2a031c5 3911 std::vector<bound_minimal_symbol> *minsyms)
40e084e1 3912{
596dc4ad
TT
3913 gdb::unique_xmalloc_ptr<char> canon
3914 = cp_canonicalize_string_no_typedefs (lookup_name);
3915 if (canon != nullptr)
3916 lookup_name = canon.get ();
2f408ecb 3917
cc81e1c6
DE
3918 /* It's important to not call expand_symtabs_matching unnecessarily
3919 as it can really slow things down (by unnecessarily expanding
3920 potentially 1000s of symtabs, which when debugging some apps can
3921 cost 100s of seconds). Avoid this to some extent by *first* calling
3922 find_function_symbols, and only if that doesn't find anything
3923 *then* call find_method. This handles two important cases:
3924 1) break (anonymous namespace)::foo
3925 2) break class::method where method is in class (and not a baseclass) */
14e91ac5 3926
cc81e1c6 3927 find_function_symbols (state, file_symtabs, lookup_name,
7243d011 3928 name_match_type, symbols, minsyms);
14e91ac5 3929
cc81e1c6
DE
3930 /* If we were unable to locate a symbol of the same name, try dividing
3931 the name into class and method names and searching the class and its
3932 baseclasses. */
c2a031c5 3933 if (symbols->empty () && minsyms->empty ())
40e084e1 3934 {
2f408ecb 3935 std::string klass, method;
cc81e1c6 3936 const char *last, *p, *scope_op;
14e91ac5 3937
cc81e1c6
DE
3938 /* See if we can find a scope operator and break this symbol
3939 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3940 scope_op = "::";
3941 p = find_toplevel_string (lookup_name, scope_op);
14e91ac5 3942
cc81e1c6
DE
3943 last = NULL;
3944 while (p != NULL)
f8eba3c6 3945 {
cc81e1c6
DE
3946 last = p;
3947 p = find_toplevel_string (p + strlen (scope_op), scope_op);
f8eba3c6 3948 }
14e91ac5 3949
cc81e1c6
DE
3950 /* If no scope operator was found, there is nothing more we can do;
3951 we already attempted to lookup the entire name as a symbol
3952 and failed. */
3953 if (last == NULL)
2f408ecb 3954 return;
cc81e1c6
DE
3955
3956 /* LOOKUP_NAME points to the class name.
3957 LAST points to the method name. */
2f408ecb 3958 klass = std::string (lookup_name, last - lookup_name);
cc81e1c6
DE
3959
3960 /* Skip past the scope operator. */
3961 last += strlen (scope_op);
2f408ecb 3962 method = last;
cc81e1c6
DE
3963
3964 /* Find a list of classes named KLASS. */
7e41c8db 3965 std::vector<block_symbol> classes
4dedf84d
KS
3966 = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
3967 if (!classes.empty ())
cc81e1c6
DE
3968 {
3969 /* Now locate a list of suitable methods named METHOD. */
a70b8144 3970 try
cc81e1c6 3971 {
2f408ecb
PA
3972 find_method (state, file_symtabs,
3973 klass.c_str (), method.c_str (),
4dedf84d 3974 &classes, symbols, minsyms);
cc81e1c6
DE
3975 }
3976
3977 /* If successful, we're done. If NOT_FOUND_ERROR
3978 was not thrown, rethrow the exception that we did get. */
230d2906 3979 catch (const gdb_exception_error &except)
7556d4a4
PA
3980 {
3981 if (except.error != NOT_FOUND_ERROR)
eedc3f4f 3982 throw;
7556d4a4 3983 }
cc81e1c6 3984 }
2f408ecb 3985 }
14e91ac5
DC
3986}
3987
a2459270
PA
3988/* Helper for find_label_symbols. Find all labels that match name
3989 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3990 Return the actual function symbol in which the label was found in
3991 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3992 interpreted as a label name prefix. Otherwise, only a label named
3993 exactly NAME match. */
3994
3995static void
3996find_label_symbols_in_block (const struct block *block,
3997 const char *name, struct symbol *fn_sym,
3998 bool completion_mode,
7e41c8db
KS
3999 std::vector<block_symbol> *result,
4000 std::vector<block_symbol> *label_funcs_ret)
a2459270
PA
4001{
4002 if (completion_mode)
4003 {
4004 struct block_iterator iter;
4005 struct symbol *sym;
4006 size_t name_len = strlen (name);
4007
4008 int (*cmp) (const char *, const char *, size_t);
4009 cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
4010
4011 ALL_BLOCK_SYMBOLS (block, iter, sym)
4012 {
c1b5c1eb 4013 if (symbol_matches_domain (sym->language (),
a2459270 4014 SYMBOL_DOMAIN (sym), LABEL_DOMAIN)
987012b8 4015 && cmp (sym->search_name (), name, name_len) == 0)
a2459270 4016 {
7e41c8db
KS
4017 result->push_back ({sym, block});
4018 label_funcs_ret->push_back ({fn_sym, block});
a2459270
PA
4019 }
4020 }
4021 }
4022 else
4023 {
7e41c8db
KS
4024 struct block_symbol label_sym
4025 = lookup_symbol (name, block, LABEL_DOMAIN, 0);
a2459270 4026
7e41c8db 4027 if (label_sym.symbol != NULL)
a2459270 4028 {
7e41c8db
KS
4029 result->push_back (label_sym);
4030 label_funcs_ret->push_back ({fn_sym, block});
a2459270
PA
4031 }
4032 }
4033}
4034
3553eadc
KS
4035/* Return all labels that match name NAME in FUNCTION_SYMBOLS or NULL
4036 if no matches were found.
4037
4038 Return the actual function symbol in which the label was found in
a2459270
PA
4039 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4040 interpreted as a label name prefix. Otherwise, only labels named
4041 exactly NAME match. */
0f5238ed 4042
7e41c8db
KS
4043
4044static std::vector<block_symbol> *
40e084e1 4045find_label_symbols (struct linespec_state *self,
7e41c8db
KS
4046 std::vector<block_symbol> *function_symbols,
4047 std::vector<block_symbol> *label_funcs_ret,
4048 const char *name,
a2459270 4049 bool completion_mode)
0f5238ed 4050{
3977b71f 4051 const struct block *block;
40e084e1 4052 struct symbol *fn_sym;
7e41c8db 4053 std::vector<block_symbol> result;
9ef07c8c 4054
f8eba3c6 4055 if (function_symbols == NULL)
9ef07c8c 4056 {
f8eba3c6 4057 set_current_program_space (self->program_space);
4eeaa230 4058 block = get_current_search_block ();
f8eba3c6 4059
9ef07c8c
TT
4060 for (;
4061 block && !BLOCK_FUNCTION (block);
4062 block = BLOCK_SUPERBLOCK (block))
4063 ;
4064 if (!block)
40e084e1 4065 return NULL;
f8eba3c6
TT
4066 fn_sym = BLOCK_FUNCTION (block);
4067
a2459270
PA
4068 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4069 &result, label_funcs_ret);
40e084e1
KS
4070 }
4071 else
4072 {
7243d011 4073 for (const auto &elt : *function_symbols)
f8eba3c6 4074 {
7e41c8db
KS
4075 fn_sym = elt.symbol;
4076 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
4077 block = SYMBOL_BLOCK_VALUE (fn_sym);
40e084e1 4078
7e41c8db 4079 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
a2459270 4080 &result, label_funcs_ret);
f8eba3c6 4081 }
40e084e1 4082 }
f8eba3c6 4083
3553eadc 4084 if (!result.empty ())
7e41c8db 4085 return new std::vector<block_symbol> (std::move (result));
3553eadc 4086 return nullptr;
40e084e1 4087}
f8eba3c6 4088
40e084e1
KS
4089\f
4090
4091/* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4092
6c5b2ebe 4093static std::vector<symtab_and_line>
40e084e1
KS
4094decode_digits_list_mode (struct linespec_state *self,
4095 linespec_p ls,
40e084e1
KS
4096 struct symtab_and_line val)
4097{
40e084e1
KS
4098 gdb_assert (self->list_mode);
4099
6c5b2ebe
PA
4100 std::vector<symtab_and_line> values;
4101
2a908241 4102 for (const auto &elt : *ls->file_symtabs)
40e084e1
KS
4103 {
4104 /* The logic above should ensure this. */
4105 gdb_assert (elt != NULL);
4106
4107 set_current_program_space (SYMTAB_PSPACE (elt));
4108
4109 /* Simplistic search just for the list command. */
4110 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
4111 if (val.symtab == NULL)
4112 val.symtab = elt;
4113 val.pspace = SYMTAB_PSPACE (elt);
4114 val.pc = 0;
5b0e2db4 4115 val.explicit_line = true;
40e084e1 4116
6c5b2ebe 4117 add_sal_to_sals (self, &values, &val, NULL, 0);
f8eba3c6 4118 }
6c5b2ebe
PA
4119
4120 return values;
40e084e1 4121}
f8eba3c6 4122
791b7405
AB
4123/* A helper for create_sals_line_offset that iterates over the symtabs
4124 associated with LS and returns a vector of corresponding symtab_and_line
4125 structures. */
40e084e1 4126
6c5b2ebe 4127static std::vector<symtab_and_line>
40e084e1
KS
4128decode_digits_ordinary (struct linespec_state *self,
4129 linespec_p ls,
4130 int line,
40e084e1
KS
4131 struct linetable_entry **best_entry)
4132{
6c5b2ebe 4133 std::vector<symtab_and_line> sals;
2a908241 4134 for (const auto &elt : *ls->file_symtabs)
f8eba3c6 4135 {
67d89901 4136 std::vector<CORE_ADDR> pcs;
40e084e1
KS
4137
4138 /* The logic above should ensure this. */
4139 gdb_assert (elt != NULL);
f8eba3c6 4140
40e084e1 4141 set_current_program_space (SYMTAB_PSPACE (elt));
f8eba3c6 4142
40e084e1 4143 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
67d89901 4144 for (CORE_ADDR pc : pcs)
f8eba3c6 4145 {
51abb421 4146 symtab_and_line sal;
40e084e1
KS
4147 sal.pspace = SYMTAB_PSPACE (elt);
4148 sal.symtab = elt;
4149 sal.line = line;
5b0e2db4 4150 sal.explicit_line = true;
40e084e1 4151 sal.pc = pc;
6c5b2ebe 4152 sals.push_back (std::move (sal));
f8eba3c6
TT
4153 }
4154 }
6c5b2ebe
PA
4155
4156 return sals;
40e084e1
KS
4157}
4158
4159\f
4160
4161/* Return the line offset represented by VARIABLE. */
4162
4163static struct line_offset
4164linespec_parse_variable (struct linespec_state *self, const char *variable)
4165{
4166 int index = 0;
4167 const char *p;
4168 struct line_offset offset = {0, LINE_OFFSET_NONE};
f8eba3c6 4169
40e084e1
KS
4170 p = (variable[1] == '$') ? variable + 2 : variable + 1;
4171 if (*p == '$')
4172 ++p;
4173 while (*p >= '0' && *p <= '9')
4174 ++p;
4175 if (!*p) /* Reached end of token without hitting non-digit. */
f8eba3c6 4176 {
40e084e1
KS
4177 /* We have a value history reference. */
4178 struct value *val_history;
f8eba3c6 4179
40e084e1
KS
4180 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
4181 val_history
4182 = access_value_history ((variable[1] == '$') ? -index : index);
78134374 4183 if (value_type (val_history)->code () != TYPE_CODE_INT)
40e084e1
KS
4184 error (_("History values used in line "
4185 "specs must have integer values."));
4186 offset.offset = value_as_long (val_history);
4187 }
4188 else
4189 {
4190 /* Not all digits -- may be user variable/function or a
4191 convenience variable. */
4192 LONGEST valx;
4193 struct internalvar *ivar;
4194
4195 /* Try it as a convenience variable. If it is not a convenience
4196 variable, return and allow normal symbol lookup to occur. */
4197 ivar = lookup_only_internalvar (variable + 1);
4198 if (ivar == NULL)
4199 /* No internal variable with that name. Mark the offset
4200 as unknown to allow the name to be looked up as a symbol. */
4201 offset.sign = LINE_OFFSET_UNKNOWN;
4202 else
4203 {
4204 /* We found a valid variable name. If it is not an integer,
4205 throw an error. */
4206 if (!get_internalvar_integer (ivar, &valx))
4207 error (_("Convenience variables used in line "
4208 "specs must have integer values."));
4209 else
4210 offset.offset = valx;
4211 }
f8eba3c6
TT
4212 }
4213
40e084e1 4214 return offset;
f8eba3c6 4215}
40e084e1 4216\f
f8eba3c6 4217
40e084e1 4218/* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
6e22494e
JK
4219 linespec; return the SAL in RESULT. This function should return SALs
4220 matching those from find_function_start_sal, otherwise false
4221 multiple-locations breakpoints could be placed. */
f8eba3c6
TT
4222
4223static void
4224minsym_found (struct linespec_state *self, struct objfile *objfile,
4225 struct minimal_symbol *msymbol,
6c5b2ebe 4226 std::vector<symtab_and_line> *result)
f8eba3c6 4227{
3467ec66 4228 bool want_start_sal;
f8eba3c6 4229
4024cf2b 4230 CORE_ADDR func_addr;
3467ec66
PA
4231 bool is_function = msymbol_is_function (objfile, msymbol, &func_addr);
4232
4233 if (is_function)
4234 {
c9d95fa3 4235 const char *msym_name = msymbol->linkage_name ();
3467ec66 4236
f50776aa
PA
4237 if (MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
4238 || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
3467ec66
PA
4239 want_start_sal = gnu_ifunc_resolve_name (msym_name, &func_addr);
4240 else
4241 want_start_sal = true;
4242 }
4243
4244 symtab_and_line sal;
4245
4246 if (is_function && want_start_sal)
42ddae10 4247 sal = find_function_start_sal (func_addr, NULL, self->funfirstline);
e5f25bc5
PA
4248 else
4249 {
4250 sal.objfile = objfile;
3467ec66
PA
4251 sal.msymbol = msymbol;
4252 /* Store func_addr, not the minsym's address in case this was an
4253 ifunc that hasn't been resolved yet. */
4254 if (is_function)
4255 sal.pc = func_addr;
4256 else
4257 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
e5f25bc5 4258 sal.pspace = current_program_space;
6e22494e 4259 }
f8eba3c6 4260
6b05c8bd
YQ
4261 sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
4262
07fea4b4 4263 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
c9d95fa3 4264 add_sal_to_sals (self, result, &sal, msymbol->natural_name (), 0);
f8eba3c6
TT
4265}
4266
41c1efc6
TT
4267/* Helper for search_minsyms_for_name that adds the symbol to the
4268 result. */
39b856a4
TT
4269
4270static void
41c1efc6
TT
4271add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
4272 struct symtab *symtab, int list_mode,
4273 std::vector<struct bound_minimal_symbol> *msyms)
39b856a4 4274{
41c1efc6 4275 if (symtab != NULL)
87186c6a 4276 {
4024cf2b
PA
4277 /* We're looking for a label for which we don't have debug
4278 info. */
4279 CORE_ADDR func_addr;
41c1efc6 4280 if (msymbol_is_function (objfile, minsym, &func_addr))
4024cf2b
PA
4281 {
4282 symtab_and_line sal = find_pc_sect_line (func_addr, NULL, 0);
87186c6a 4283
41c1efc6 4284 if (symtab != sal.symtab)
4024cf2b
PA
4285 return;
4286 }
87186c6a
MMN
4287 }
4288
4024cf2b 4289 /* Exclude data symbols when looking for breakpoint locations. */
41c1efc6 4290 if (!list_mode && !msymbol_is_function (objfile, minsym))
4024cf2b 4291 return;
095bcf5e 4292
41c1efc6
TT
4293 struct bound_minimal_symbol mo = {minsym, objfile};
4294 msyms->push_back (mo);
ca31ab1d 4295 return;
f8eba3c6
TT
4296}
4297
87186c6a 4298/* Search for minimal symbols called NAME. If SEARCH_PSPACE
f8eba3c6 4299 is not NULL, the search is restricted to just that program
87186c6a
MMN
4300 space.
4301
4302 If SYMTAB is NULL, search all objfiles, otherwise
4303 restrict results to the given SYMTAB. */
f8eba3c6
TT
4304
4305static void
b5ec771e
PA
4306search_minsyms_for_name (struct collect_info *info,
4307 const lookup_name_info &name,
87186c6a
MMN
4308 struct program_space *search_pspace,
4309 struct symtab *symtab)
f8eba3c6 4310{
41c1efc6 4311 std::vector<struct bound_minimal_symbol> minsyms;
f8eba3c6 4312
87186c6a
MMN
4313 if (symtab == NULL)
4314 {
94c93c35
TT
4315 for (struct program_space *pspace : program_spaces)
4316 {
4317 if (search_pspace != NULL && search_pspace != pspace)
4318 continue;
4319 if (pspace->executing_startup)
4320 continue;
39b856a4 4321
94c93c35 4322 set_current_program_space (pspace);
87186c6a 4323
94c93c35
TT
4324 for (objfile *objfile : current_program_space->objfiles ())
4325 {
4326 iterate_over_minimal_symbols (objfile, name,
4327 [&] (struct minimal_symbol *msym)
4328 {
4329 add_minsym (msym, objfile, nullptr,
4330 info->state->list_mode,
4331 &minsyms);
4332 return false;
4333 });
4334 }
4335 }
87186c6a
MMN
4336 }
4337 else
f8eba3c6 4338 {
87186c6a
MMN
4339 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
4340 {
4341 set_current_program_space (SYMTAB_PSPACE (symtab));
41c1efc6
TT
4342 iterate_over_minimal_symbols
4343 (SYMTAB_OBJFILE (symtab), name,
4344 [&] (struct minimal_symbol *msym)
4345 {
4346 add_minsym (msym, SYMTAB_OBJFILE (symtab), symtab,
4347 info->state->list_mode, &minsyms);
ca31ab1d 4348 return false;
41c1efc6 4349 });
87186c6a 4350 }
9ef07c8c 4351 }
39b856a4 4352
77f2120b
PA
4353 /* Return true if TYPE is a static symbol. */
4354 auto msymbol_type_is_static = [] (enum minimal_symbol_type type)
41c1efc6 4355 {
77f2120b
PA
4356 switch (type)
4357 {
4358 case mst_file_text:
4359 case mst_file_data:
4360 case mst_file_bss:
4361 return true;
4362 default:
4363 return false;
4364 }
4365 };
39b856a4 4366
77f2120b
PA
4367 /* Add minsyms to the result set, but filter out trampoline symbols
4368 if we also found extern symbols with the same name. I.e., don't
4369 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4370 'foo' is the symbol that the plt resolves to. */
4371 for (const bound_minimal_symbol &item : minsyms)
4372 {
4373 bool skip = false;
4374 if (MSYMBOL_TYPE (item.minsym) == mst_solib_trampoline)
4375 {
4376 for (const bound_minimal_symbol &item2 : minsyms)
4377 {
4378 if (&item2 == &item)
4379 continue;
39b856a4 4380
77f2120b
PA
4381 /* Trampoline symbols can only jump to exported
4382 symbols. */
4383 if (msymbol_type_is_static (MSYMBOL_TYPE (item2.minsym)))
4384 continue;
41c1efc6 4385
77f2120b
PA
4386 if (strcmp (item.minsym->linkage_name (),
4387 item2.minsym->linkage_name ()) != 0)
4388 continue;
41c1efc6 4389
77f2120b
PA
4390 /* Found a global minsym with the same name as the
4391 trampoline. Don't create a location for this
4392 trampoline. */
4393 skip = true;
4394 break;
4395 }
41c1efc6 4396 }
77f2120b
PA
4397
4398 if (!skip)
4399 info->result.minimal_symbols->push_back (item);
41c1efc6 4400 }
f8eba3c6
TT
4401}
4402
4403/* A helper function to add all symbols matching NAME to INFO. If
4404 PSPACE is not NULL, the search is restricted to just that program
4405 space. */
0f5238ed 4406
f8eba3c6
TT
4407static void
4408add_matching_symbols_to_info (const char *name,
b5ec771e 4409 symbol_name_match_type name_match_type,
56d87ef7 4410 enum search_domain search_domain,
f8eba3c6
TT
4411 struct collect_info *info,
4412 struct program_space *pspace)
4413{
b5ec771e
PA
4414 lookup_name_info lookup_name (name, name_match_type);
4415
2a908241 4416 for (const auto &elt : *info->file_symtabs)
f8eba3c6 4417 {
2a908241 4418 if (elt == nullptr)
f8eba3c6 4419 {
b5ec771e 4420 iterate_over_all_matching_symtabs (info->state, lookup_name,
56d87ef7 4421 VAR_DOMAIN, search_domain,
7e41c8db
KS
4422 pspace, true,
4423 [&] (block_symbol *bsym)
4424 { return info->add_symbol (bsym); });
b5ec771e 4425 search_minsyms_for_name (info, lookup_name, pspace, NULL);
f8eba3c6
TT
4426 }
4427 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
4428 {
7243d011 4429 int prev_len = info->result.symbols->size ();
87186c6a 4430
f8eba3c6
TT
4431 /* Program spaces that are executing startup should have
4432 been filtered out earlier. */
4433 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
4434 set_current_program_space (SYMTAB_PSPACE (elt));
b5ec771e 4435 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN,
7e41c8db
KS
4436 [&] (block_symbol *bsym)
4437 { return info->add_symbol (bsym); });
87186c6a
MMN
4438
4439 /* If no new symbols were found in this iteration and this symtab
4440 is in assembler, we might actually be looking for a label for
4441 which we don't have debug info. Check for a minimal symbol in
4442 this case. */
7243d011 4443 if (prev_len == info->result.symbols->size ()
87186c6a 4444 && elt->language == language_asm)
b5ec771e 4445 search_minsyms_for_name (info, lookup_name, pspace, elt);
f8eba3c6
TT
4446 }
4447 }
0f5238ed
TT
4448}
4449
14e91ac5
DC
4450\f
4451
413dad4d
DC
4452/* Now come some functions that are called from multiple places within
4453 decode_line_1. */
4454
f8eba3c6
TT
4455static int
4456symbol_to_sal (struct symtab_and_line *result,
4457 int funfirstline, struct symbol *sym)
413dad4d 4458{
413dad4d 4459 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 4460 {
f8eba3c6
TT
4461 *result = find_function_start_sal (sym, funfirstline);
4462 return 1;
50641945 4463 }
413dad4d
DC
4464 else
4465 {
62853458 4466 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
413dad4d 4467 {
51abb421 4468 *result = {};
08be3fe3 4469 result->symtab = symbol_symtab (sym);
06871ae8 4470 result->symbol = sym;
f8eba3c6
TT
4471 result->line = SYMBOL_LINE (sym);
4472 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 4473 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6
TT
4474 result->explicit_pc = 1;
4475 return 1;
413dad4d 4476 }
62853458 4477 else if (funfirstline)
dcf9f4ab 4478 {
f8eba3c6 4479 /* Nothing. */
dcf9f4ab 4480 }
62853458
TT
4481 else if (SYMBOL_LINE (sym) != 0)
4482 {
4483 /* We know its line number. */
51abb421 4484 *result = {};
08be3fe3 4485 result->symtab = symbol_symtab (sym);
06871ae8 4486 result->symbol = sym;
f8eba3c6 4487 result->line = SYMBOL_LINE (sym);
e5f25bc5 4488 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 4489 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6 4490 return 1;
62853458 4491 }
413dad4d 4492 }
f8eba3c6
TT
4493
4494 return 0;
413dad4d 4495}
50641945 4496
16e802b9 4497linespec_result::~linespec_result ()
f8eba3c6 4498{
6c5b2ebe
PA
4499 for (linespec_sals &lsal : lsals)
4500 xfree (lsal.canonical);
7efd8fc2 4501}
87f0e720
KS
4502
4503/* Return the quote characters permitted by the linespec parser. */
4504
4505const char *
4506get_gdb_linespec_parser_quote_characters (void)
4507{
4508 return linespec_quote_characters;
4509}
This page took 2.086576 seconds and 4 git commands to generate.