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
3666a048 3 Copyright (C) 1986-2021 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>
328d42d8 50#include "inferior.h"
f8eba3c6 51
c45ec17c
PA
52/* An enumeration of the various things a user might attempt to
53 complete for a linespec location. */
54
55enum class linespec_complete_what
56{
57 /* Nothing, no possible completion. */
58 NOTHING,
59
60 /* A function/method name. Due to ambiguity between
61
62 (gdb) b source[TAB]
63 source_file.c
64 source_function
65
66 this can also indicate a source filename, iff we haven't seen a
67 separate source filename component, as in "b source.c:function". */
68 FUNCTION,
69
70 /* A label symbol. E.g., break file.c:function:LABEL. */
71 LABEL,
72
73 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
74 EXPRESSION,
75
733d554a 76 /* A linespec keyword ("if"/"thread"/"task"/"-force-condition").
c45ec17c
PA
77 E.g., "break func threa<tab>". */
78 KEYWORD,
79};
80
f8eba3c6
TT
81/* An address entry is used to ensure that any given location is only
82 added to the result a single time. It holds an address and the
83 program space from which the address came. */
84
85struct address_entry
86{
87 struct program_space *pspace;
88 CORE_ADDR addr;
89};
90
40e084e1
KS
91/* A linespec. Elements of this structure are filled in by a parser
92 (either parse_linespec or some other function). The structure is
93 then converted into SALs by convert_linespec_to_sals. */
94
95struct linespec
96{
00e52e53 97 /* An explicit location describing the SaLs. */
67994074 98 struct explicit_location explicit_loc;
40e084e1
KS
99
100 /* The list of symtabs to search to which to limit the search. May not
00e52e53
KS
101 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
102 filename), FILE_SYMTABS should contain one single NULL member. This
103 will cause the code to use the default symtab. */
2a908241 104 std::vector<symtab *> *file_symtabs;
40e084e1 105
40e084e1 106 /* A list of matching function symbols and minimal symbols. Both lists
7243d011 107 may be NULL (or empty) if no matching symbols were found. */
7e41c8db 108 std::vector<block_symbol> *function_symbols;
c2a031c5 109 std::vector<bound_minimal_symbol> *minimal_symbols;
40e084e1 110
40e084e1
KS
111 /* A structure of matching label symbols and the corresponding
112 function symbol in which the label was found. Both may be NULL
113 or both must be non-NULL. */
114 struct
115 {
7e41c8db
KS
116 std::vector<block_symbol> *label_symbols;
117 std::vector<block_symbol> *function_symbols;
40e084e1 118 } labels;
40e084e1 119};
40e084e1 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 361static std::vector<symtab_and_line> decode_objc (struct linespec_state *self,
1487a14e 362 linespec *ls,
6c5b2ebe 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,
1487a14e 403 linespec *ls,
6c5b2ebe
PA
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,
1487a14e 409 linespec *ls,
6c5b2ebe 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 {
4d080b46 1171 objfile->expand_symtabs_matching (NULL, &lookup_name, NULL, NULL,
03a8ea51
TT
1172 (SEARCH_GLOBAL_BLOCK
1173 | SEARCH_STATIC_BLOCK),
3bfa51a7 1174 UNDEF_DOMAIN,
4d080b46 1175 search_domain);
94c93c35
TT
1176
1177 for (compunit_symtab *cu : objfile->compunits ())
1178 {
1179 struct symtab *symtab = COMPUNIT_FILETABS (cu);
43f3e411 1180
94c93c35
TT
1181 iterate_over_file_blocks (symtab, lookup_name, name_domain,
1182 callback);
481860b3 1183
94c93c35
TT
1184 if (include_inline)
1185 {
1186 const struct block *block;
1187 int i;
481860b3 1188
94c93c35
TT
1189 for (i = FIRST_LOCAL_BLOCK;
1190 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
1191 i++)
1192 {
1193 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
4009ee92 1194 state->language->iterate_over_symbols
94c93c35
TT
1195 (block, lookup_name, name_domain,
1196 [&] (block_symbol *bsym)
1197 {
1198 /* Restrict calls to CALLBACK to symbols
1199 representing inline symbols only. */
1200 if (SYMBOL_INLINED (bsym->symbol))
1201 return callback (bsym);
1202 return true;
1203 });
1204 }
1205 }
1206 }
1207 }
1208 }
50641945
FN
1209}
1210
4eeaa230
DE
1211/* Returns the block to be used for symbol searches from
1212 the current location. */
e8eb7bc5 1213
3977b71f 1214static const struct block *
e482a1a7 1215get_current_search_block (void)
e8eb7bc5 1216{
4eeaa230
DE
1217 /* get_selected_block can change the current language when there is
1218 no selected frame yet. */
9bb9b2f9
TT
1219 scoped_restore_current_language save_language;
1220 return get_selected_block (0);
e8eb7bc5
KS
1221}
1222
4eeaa230
DE
1223/* Iterate over static and global blocks. */
1224
1225static void
14bc53a8 1226iterate_over_file_blocks
b5ec771e
PA
1227 (struct symtab *symtab, const lookup_name_info &name,
1228 domain_enum domain, gdb::function_view<symbol_found_callback_ftype> callback)
4eeaa230 1229{
582942f4 1230 const struct block *block;
4eeaa230 1231
439247b6 1232 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
4eeaa230
DE
1233 block != NULL;
1234 block = BLOCK_SUPERBLOCK (block))
4b2f86ef 1235 current_language->iterate_over_symbols (block, name, domain, callback);
4eeaa230
DE
1236}
1237
b5ec771e
PA
1238/* A helper for find_method. This finds all methods in type T of
1239 language T_LANG which match NAME. It adds matching symbol names to
1240 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
50641945 1241
f8eba3c6 1242static void
b5ec771e 1243find_methods (struct type *t, enum language t_lang, const char *name,
9b2f8581 1244 std::vector<const char *> *result_names,
8e8d776e 1245 std::vector<struct type *> *superclasses)
50641945 1246{
50641945 1247 int ibase;
7d93a1e0 1248 const char *class_name = t->name ();
c00f8484 1249
50641945
FN
1250 /* Ignore this class if it doesn't have a name. This is ugly, but
1251 unless we figure out how to get the physname without the name of
1252 the class, then the loop can't do any good. */
f8eba3c6 1253 if (class_name)
50641945
FN
1254 {
1255 int method_counter;
b5ec771e
PA
1256 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1257 symbol_name_matcher_ftype *symbol_name_compare
c9debfb9 1258 = language_def (t_lang)->get_symbol_name_matcher (lookup_name);
50641945 1259
f168693b 1260 t = check_typedef (t);
50641945
FN
1261
1262 /* Loop over each method name. At this level, all overloads of a name
dda83cd7
SM
1263 are counted as a single name. There is an inner loop which loops over
1264 each overload. */
50641945
FN
1265
1266 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1267 method_counter >= 0;
1268 --method_counter)
1269 {
0d5cff50 1270 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
50641945 1271
b5ec771e 1272 if (symbol_name_compare (method_name, lookup_name, NULL))
f8eba3c6
TT
1273 {
1274 int field_counter;
aee8d8ba 1275
f8eba3c6
TT
1276 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1277 - 1);
1278 field_counter >= 0;
1279 --field_counter)
1280 {
1281 struct fn_field *f;
1282 const char *phys_name;
1283
1284 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1285 if (TYPE_FN_FIELD_STUB (f, field_counter))
1286 continue;
1287 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
9b2f8581 1288 result_names->push_back (phys_name);
f8eba3c6
TT
1289 }
1290 }
aee8d8ba
DC
1291 }
1292 }
1293
f8eba3c6 1294 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
8e8d776e 1295 superclasses->push_back (TYPE_BASECLASS (t, ibase));
50641945
FN
1296}
1297
50641945
FN
1298/* Find an instance of the character C in the string S that is outside
1299 of all parenthesis pairs, single-quoted strings, and double-quoted
8120c9d5 1300 strings. Also, ignore the char within a template name, like a ','
be966d42 1301 within foo<int, int>, while considering C++ operator</operator<<. */
8120c9d5 1302
87f0e720 1303const char *
40e084e1 1304find_toplevel_char (const char *s, char c)
50641945
FN
1305{
1306 int quoted = 0; /* zero if we're not in quotes;
1307 '"' if we're in a double-quoted string;
1308 '\'' if we're in a single-quoted string. */
a04257e6 1309 int depth = 0; /* Number of unclosed parens we've seen. */
40e084e1 1310 const char *scan;
50641945
FN
1311
1312 for (scan = s; *scan; scan++)
1313 {
1314 if (quoted)
1315 {
1316 if (*scan == quoted)
1317 quoted = 0;
1318 else if (*scan == '\\' && *(scan + 1))
1319 scan++;
1320 }
1321 else if (*scan == c && ! quoted && depth == 0)
1322 return scan;
1323 else if (*scan == '"' || *scan == '\'')
1324 quoted = *scan;
8120c9d5 1325 else if (*scan == '(' || *scan == '<')
50641945 1326 depth++;
8120c9d5 1327 else if ((*scan == ')' || *scan == '>') && depth > 0)
50641945 1328 depth--;
be966d42
PA
1329 else if (*scan == 'o' && !quoted && depth == 0)
1330 {
1331 /* Handle C++ operator names. */
1332 if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
1333 {
1334 scan += CP_OPERATOR_LEN;
1335 if (*scan == c)
1336 return scan;
1337 while (isspace (*scan))
1338 {
1339 ++scan;
1340 if (*scan == c)
1341 return scan;
1342 }
1343 if (*scan == '\0')
1344 break;
1345
1346 switch (*scan)
1347 {
1348 /* Skip over one less than the appropriate number of
1349 characters: the for loop will skip over the last
1350 one. */
1351 case '<':
1352 if (scan[1] == '<')
1353 {
1354 scan++;
1355 if (*scan == c)
1356 return scan;
1357 }
1358 break;
1359 case '>':
1360 if (scan[1] == '>')
1361 {
1362 scan++;
1363 if (*scan == c)
1364 return scan;
1365 }
1366 break;
1367 }
1368 }
1369 }
50641945
FN
1370 }
1371
1372 return 0;
1373}
1374
40e084e1
KS
1375/* The string equivalent of find_toplevel_char. Returns a pointer
1376 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1377 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
889f28e2 1378
40e084e1
KS
1379static const char *
1380find_toplevel_string (const char *haystack, const char *needle)
889f28e2 1381{
40e084e1
KS
1382 const char *s = haystack;
1383
1384 do
1385 {
1386 s = find_toplevel_char (s, *needle);
1387
1388 if (s != NULL)
1389 {
1390 /* Found first char in HAYSTACK; check rest of string. */
61012eef 1391 if (startswith (s, needle))
40e084e1
KS
1392 return s;
1393
1394 /* Didn't find it; loop over HAYSTACK, looking for the next
1395 instance of the first character of NEEDLE. */
1396 ++s;
1397 }
1398 }
1399 while (s != NULL && *s != '\0');
1400
1401 /* NEEDLE was not found in HAYSTACK. */
1402 return NULL;
889f28e2
AF
1403}
1404
33f448b1 1405/* Convert CANONICAL to its string representation using
53a0f8a2 1406 symtab_to_fullname for SYMTAB. */
33f448b1 1407
53a0f8a2 1408static std::string
33f448b1
JK
1409canonical_to_fullform (const struct linespec_canonical_name *canonical)
1410{
1411 if (canonical->symtab == NULL)
53a0f8a2 1412 return canonical->suffix;
33f448b1 1413 else
53a0f8a2
TT
1414 return string_printf ("%s:%s", symtab_to_fullname (canonical->symtab),
1415 canonical->suffix);
33f448b1
JK
1416}
1417
f8eba3c6
TT
1418/* Given FILTERS, a list of canonical names, filter the sals in RESULT
1419 and store the result in SELF->CANONICAL. */
50641945 1420
f8eba3c6
TT
1421static void
1422filter_results (struct linespec_state *self,
6c5b2ebe 1423 std::vector<symtab_and_line> *result,
f73c6ece 1424 const std::vector<const char *> &filters)
f8eba3c6 1425{
f73c6ece 1426 for (const char *name : filters)
f8eba3c6 1427 {
6c5b2ebe 1428 linespec_sals lsal;
f8eba3c6 1429
6c5b2ebe 1430 for (size_t j = 0; j < result->size (); ++j)
f8eba3c6 1431 {
33f448b1 1432 const struct linespec_canonical_name *canonical;
33f448b1
JK
1433
1434 canonical = &self->canonical_names[j];
53a0f8a2 1435 std::string fullform = canonical_to_fullform (canonical);
33f448b1 1436
53a0f8a2 1437 if (name == fullform)
6c5b2ebe 1438 lsal.sals.push_back ((*result)[j]);
f8eba3c6
TT
1439 }
1440
6c5b2ebe 1441 if (!lsal.sals.empty ())
f8eba3c6
TT
1442 {
1443 lsal.canonical = xstrdup (name);
6c5b2ebe 1444 self->canonical->lsals.push_back (std::move (lsal));
f8eba3c6
TT
1445 }
1446 }
1447
1448 self->canonical->pre_expanded = 0;
1449}
1450
1451/* Store RESULT into SELF->CANONICAL. */
1452
1453static void
1454convert_results_to_lsals (struct linespec_state *self,
6c5b2ebe 1455 std::vector<symtab_and_line> *result)
50641945 1456{
f8eba3c6
TT
1457 struct linespec_sals lsal;
1458
1459 lsal.canonical = NULL;
6c5b2ebe
PA
1460 lsal.sals = std::move (*result);
1461 self->canonical->lsals.push_back (std::move (lsal));
f8eba3c6
TT
1462}
1463
33f448b1
JK
1464/* A structure that contains two string representations of a struct
1465 linespec_canonical_name:
6471e7d2 1466 - one where the symtab's fullname is used;
33f448b1
JK
1467 - one where the filename followed the "set filename-display"
1468 setting. */
1469
1470struct decode_line_2_item
1471{
53a0f8a2
TT
1472 decode_line_2_item (std::string &&fullform_, std::string &&displayform_,
1473 bool selected_)
1474 : fullform (std::move (fullform_)),
1475 displayform (std::move (displayform_)),
1476 selected (selected_)
1477 {
1478 }
33f448b1 1479
53a0f8a2
TT
1480 /* The form using symtab_to_fullname. */
1481 std::string fullform;
1482
1483 /* The form using symtab_to_filename_for_display. */
1484 std::string displayform;
33f448b1
JK
1485
1486 /* Field is initialized to zero and it is set to one if the user
1487 requested breakpoint for this entry. */
1488 unsigned int selected : 1;
1489};
1490
53a0f8a2
TT
1491/* Helper for std::sort to sort decode_line_2_item entries by
1492 DISPLAYFORM and secondarily by FULLFORM. */
33f448b1 1493
53a0f8a2
TT
1494static bool
1495decode_line_2_compare_items (const decode_line_2_item &a,
1496 const decode_line_2_item &b)
33f448b1 1497{
53a0f8a2
TT
1498 if (a.displayform != b.displayform)
1499 return a.displayform < b.displayform;
1500 return a.fullform < b.fullform;
33f448b1
JK
1501}
1502
f8eba3c6
TT
1503/* Handle multiple results in RESULT depending on SELECT_MODE. This
1504 will either return normally, throw an exception on multiple
1505 results, or present a menu to the user. On return, the SALS vector
1506 in SELF->CANONICAL is set up properly. */
1507
1508static void
1509decode_line_2 (struct linespec_state *self,
6c5b2ebe 1510 std::vector<symtab_and_line> *result,
f8eba3c6
TT
1511 const char *select_mode)
1512{
992a7040 1513 const char *args;
a121b7c1 1514 const char *prompt;
50641945 1515 int i;
f73c6ece 1516 std::vector<const char *> filters;
53a0f8a2 1517 std::vector<struct decode_line_2_item> items;
50641945 1518
f8eba3c6
TT
1519 gdb_assert (select_mode != multiple_symbols_all);
1520 gdb_assert (self->canonical != NULL);
6c5b2ebe 1521 gdb_assert (!result->empty ());
33f448b1 1522
33f448b1 1523 /* Prepare ITEMS array. */
53a0f8a2 1524 for (i = 0; i < result->size (); ++i)
50641945 1525 {
33f448b1 1526 const struct linespec_canonical_name *canonical;
53a0f8a2
TT
1527 std::string displayform;
1528
33f448b1
JK
1529 canonical = &self->canonical_names[i];
1530 gdb_assert (canonical->suffix != NULL);
f8eba3c6 1531
53a0f8a2 1532 std::string fullform = canonical_to_fullform (canonical);
33f448b1
JK
1533
1534 if (canonical->symtab == NULL)
53a0f8a2 1535 displayform = canonical->suffix;
33f448b1 1536 else
f8eba3c6 1537 {
33f448b1
JK
1538 const char *fn_for_display;
1539
1540 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
53a0f8a2
TT
1541 displayform = string_printf ("%s:%s", fn_for_display,
1542 canonical->suffix);
f8eba3c6
TT
1543 }
1544
53a0f8a2
TT
1545 items.emplace_back (std::move (fullform), std::move (displayform),
1546 false);
50641945
FN
1547 }
1548
33f448b1 1549 /* Sort the list of method names. */
53a0f8a2 1550 std::sort (items.begin (), items.end (), decode_line_2_compare_items);
33f448b1
JK
1551
1552 /* Remove entries with the same FULLFORM. */
53a0f8a2
TT
1553 items.erase (std::unique (items.begin (), items.end (),
1554 [] (const struct decode_line_2_item &a,
1555 const struct decode_line_2_item &b)
1556 {
1557 return a.fullform == b.fullform;
1558 }),
1559 items.end ());
1560
1561 if (select_mode == multiple_symbols_cancel && items.size () > 1)
f8eba3c6
TT
1562 error (_("canceled because the command is ambiguous\n"
1563 "See set/show multiple-symbol."));
1564
53a0f8a2 1565 if (select_mode == multiple_symbols_all || items.size () == 1)
50641945 1566 {
f8eba3c6
TT
1567 convert_results_to_lsals (self, result);
1568 return;
50641945
FN
1569 }
1570
f8eba3c6 1571 printf_unfiltered (_("[0] cancel\n[1] all\n"));
53a0f8a2
TT
1572 for (i = 0; i < items.size (); i++)
1573 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform.c_str ());
f8eba3c6
TT
1574
1575 prompt = getenv ("PS2");
1576 if (prompt == NULL)
50641945 1577 {
f8eba3c6 1578 prompt = "> ";
50641945 1579 }
89fbedf3 1580 args = command_line_input (prompt, "overload-choice");
50641945
FN
1581
1582 if (args == 0 || *args == 0)
e2e0b3e5 1583 error_no_arg (_("one or more choice numbers"));
50641945 1584
bfd28288
PA
1585 number_or_range_parser parser (args);
1586 while (!parser.finished ())
50641945 1587 {
bfd28288 1588 int num = parser.get_number ();
50641945
FN
1589
1590 if (num == 0)
8a3fe4f8 1591 error (_("canceled"));
50641945
FN
1592 else if (num == 1)
1593 {
f8eba3c6
TT
1594 /* We intentionally make this result in a single breakpoint,
1595 contrary to what older versions of gdb did. The
1596 rationale is that this lets a user get the
1597 multiple_symbols_all behavior even with the 'ask'
1598 setting; and he can get separate breakpoints by entering
1599 "2-57" at the query. */
f8eba3c6
TT
1600 convert_results_to_lsals (self, result);
1601 return;
50641945
FN
1602 }
1603
f8eba3c6 1604 num -= 2;
53a0f8a2 1605 if (num >= items.size ())
f8eba3c6 1606 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
1607 else
1608 {
33f448b1 1609 struct decode_line_2_item *item = &items[num];
f8eba3c6 1610
33f448b1 1611 if (!item->selected)
50641945 1612 {
f73c6ece 1613 filters.push_back (item->fullform.c_str ());
33f448b1 1614 item->selected = 1;
50641945
FN
1615 }
1616 else
1617 {
3e43a32a 1618 printf_unfiltered (_("duplicate request for %d ignored.\n"),
f6f99966 1619 num + 2);
50641945
FN
1620 }
1621 }
50641945 1622 }
f8eba3c6
TT
1623
1624 filter_results (self, result, filters);
50641945 1625}
94af9270 1626
40e084e1 1627\f
3d50dd94 1628
40e084e1
KS
1629/* The parser of linespec itself. */
1630
1631/* Throw an appropriate error when SYMBOL is not found (optionally in
1632 FILENAME). */
1633
1634static void ATTRIBUTE_NORETURN
5d94e27b 1635symbol_not_found_error (const char *symbol, const char *filename)
3d50dd94 1636{
40e084e1
KS
1637 if (symbol == NULL)
1638 symbol = "";
1639
1640 if (!have_full_symbols ()
1641 && !have_partial_symbols ()
1642 && !have_minimal_symbols ())
1643 throw_error (NOT_FOUND_ERROR,
1644 _("No symbol table is loaded. Use the \"file\" command."));
1645
1646 /* If SYMBOL starts with '$', the user attempted to either lookup
1647 a function/variable in his code starting with '$' or an internal
1648 variable of that name. Since we do not know which, be concise and
1649 explain both possibilities. */
1650 if (*symbol == '$')
1651 {
1652 if (filename)
1653 throw_error (NOT_FOUND_ERROR,
1654 _("Undefined convenience variable or function \"%s\" "
1655 "not defined in \"%s\"."), symbol, filename);
1656 else
1657 throw_error (NOT_FOUND_ERROR,
1658 _("Undefined convenience variable or function \"%s\" "
1659 "not defined."), symbol);
1660 }
1661 else
1662 {
1663 if (filename)
1664 throw_error (NOT_FOUND_ERROR,
1665 _("Function \"%s\" not defined in \"%s\"."),
1666 symbol, filename);
1667 else
1668 throw_error (NOT_FOUND_ERROR,
1669 _("Function \"%s\" not defined."), symbol);
1670 }
3d50dd94
JK
1671}
1672
40e084e1
KS
1673/* Throw an appropriate error when an unexpected token is encountered
1674 in the input. */
94af9270 1675
40e084e1
KS
1676static void ATTRIBUTE_NORETURN
1677unexpected_linespec_error (linespec_parser *parser)
94af9270 1678{
40e084e1
KS
1679 linespec_token token;
1680 static const char * token_type_strings[]
1681 = {"keyword", "colon", "string", "number", "comma", "end of input"};
94af9270 1682
40e084e1
KS
1683 /* Get the token that generated the error. */
1684 token = linespec_lexer_lex_one (parser);
94af9270 1685
40e084e1
KS
1686 /* Finally, throw the error. */
1687 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1688 || token.type == LSTOKEN_KEYWORD)
94af9270 1689 {
a5b5adf5 1690 gdb::unique_xmalloc_ptr<char> string = copy_token_string (token);
40e084e1
KS
1691 throw_error (GENERIC_ERROR,
1692 _("malformed linespec error: unexpected %s, \"%s\""),
a5b5adf5 1693 token_type_strings[token.type], string.get ());
40e084e1
KS
1694 }
1695 else
1696 throw_error (GENERIC_ERROR,
1697 _("malformed linespec error: unexpected %s"),
1698 token_type_strings[token.type]);
1699}
1700
00e52e53
KS
1701/* Throw an undefined label error. */
1702
1703static void ATTRIBUTE_NORETURN
1704undefined_label_error (const char *function, const char *label)
1705{
1706 if (function != NULL)
1707 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
1708 _("No label \"%s\" defined in function \"%s\"."),
1709 label, function);
00e52e53
KS
1710 else
1711 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
1712 _("No label \"%s\" defined in current function."),
1713 label);
00e52e53
KS
1714}
1715
1716/* Throw a source file not found error. */
1717
1718static void ATTRIBUTE_NORETURN
1719source_file_not_found_error (const char *name)
1720{
1721 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1722}
1723
c45ec17c
PA
1724/* Unless at EIO, save the current stream position as completion word
1725 point, and consume the next token. */
1726
1727static linespec_token
1728save_stream_and_consume_token (linespec_parser *parser)
1729{
1730 if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
1731 parser->completion_word = PARSER_STREAM (parser);
1732 return linespec_lexer_consume_token (parser);
1733}
1734
87f0e720 1735/* See description in linespec.h. */
40e084e1 1736
87f0e720 1737struct line_offset
09cf2b22 1738linespec_parse_line_offset (const char *string)
40e084e1 1739{
87f0e720 1740 const char *start = string;
40e084e1
KS
1741 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1742
1743 if (*string == '+')
1744 {
1745 line_offset.sign = LINE_OFFSET_PLUS;
1746 ++string;
1747 }
1748 else if (*string == '-')
1749 {
1750 line_offset.sign = LINE_OFFSET_MINUS;
1751 ++string;
1752 }
1753
87f0e720
KS
1754 if (*string != '\0' && !isdigit (*string))
1755 error (_("malformed line offset: \"%s\""), start);
1756
40e084e1
KS
1757 /* Right now, we only allow base 10 for offsets. */
1758 line_offset.offset = atoi (string);
1759 return line_offset;
1760}
1761
c45ec17c
PA
1762/* In completion mode, if the user is still typing the number, there's
1763 no possible completion to offer. But if there's already input past
1764 the number, setup to expect NEXT. */
1765
1766static void
1767set_completion_after_number (linespec_parser *parser,
1768 linespec_complete_what next)
1769{
1770 if (*PARSER_STREAM (parser) == ' ')
1771 {
f1735a53 1772 parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
c45ec17c
PA
1773 parser->complete_what = next;
1774 }
1775 else
1776 {
1777 parser->completion_word = PARSER_STREAM (parser);
1778 parser->complete_what = linespec_complete_what::NOTHING;
1779 }
1780}
1781
40e084e1
KS
1782/* Parse the basic_spec in PARSER's input. */
1783
1784static void
1785linespec_parse_basic (linespec_parser *parser)
1786{
a5b5adf5 1787 gdb::unique_xmalloc_ptr<char> name;
40e084e1 1788 linespec_token token;
7e41c8db
KS
1789 std::vector<block_symbol> symbols;
1790 std::vector<block_symbol> *labels;
c2a031c5 1791 std::vector<bound_minimal_symbol> minimal_symbols;
40e084e1
KS
1792
1793 /* Get the next token. */
1794 token = linespec_lexer_lex_one (parser);
1795
1796 /* If it is EOI or KEYWORD, issue an error. */
c45ec17c
PA
1797 if (token.type == LSTOKEN_KEYWORD)
1798 {
1799 parser->complete_what = linespec_complete_what::NOTHING;
1800 unexpected_linespec_error (parser);
1801 }
1802 else if (token.type == LSTOKEN_EOI)
1803 {
1804 unexpected_linespec_error (parser);
1805 }
40e084e1
KS
1806 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1807 else if (token.type == LSTOKEN_NUMBER)
1808 {
c45ec17c
PA
1809 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1810
40e084e1
KS
1811 /* Record the line offset and get the next token. */
1812 name = copy_token_string (token);
a5b5adf5
TT
1813 PARSER_EXPLICIT (parser)->line_offset
1814 = linespec_parse_line_offset (name.get ());
40e084e1
KS
1815
1816 /* Get the next token. */
1817 token = linespec_lexer_consume_token (parser);
1818
1819 /* If the next token is a comma, stop parsing and return. */
1820 if (token.type == LSTOKEN_COMMA)
c45ec17c
PA
1821 {
1822 parser->complete_what = linespec_complete_what::NOTHING;
1823 return;
1824 }
40e084e1
KS
1825
1826 /* If the next token is anything but EOI or KEYWORD, issue
1827 an error. */
1828 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1829 unexpected_linespec_error (parser);
1830 }
1831
1832 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1833 return;
1834
1835 /* Next token must be LSTOKEN_STRING. */
1836 if (token.type != LSTOKEN_STRING)
c45ec17c
PA
1837 {
1838 parser->complete_what = linespec_complete_what::NOTHING;
1839 unexpected_linespec_error (parser);
1840 }
40e084e1
KS
1841
1842 /* The current token will contain the name of a function, method,
1843 or label. */
c45ec17c 1844 name = copy_token_string (token);
c45ec17c
PA
1845
1846 if (parser->completion_tracker != NULL)
1847 {
1848 /* If the function name ends with a ":", then this may be an
1849 incomplete "::" scope operator instead of a label separator.
1850 E.g.,
1851 "b klass:<tab>"
1852 which should expand to:
1853 "b klass::method()"
1854
1855 Do a tentative completion assuming the later. If we find
1856 completions, advance the stream past the colon token and make
1857 it part of the function name/token. */
1858
1859 if (!parser->completion_quote_char
1860 && strcmp (PARSER_STREAM (parser), ":") == 0)
1861 {
1862 completion_tracker tmp_tracker;
1863 const char *source_filename
1864 = PARSER_EXPLICIT (parser)->source_filename;
a20714ff
PA
1865 symbol_name_match_type match_type
1866 = PARSER_EXPLICIT (parser)->func_name_match_type;
c45ec17c
PA
1867
1868 linespec_complete_function (tmp_tracker,
1869 parser->completion_word,
a20714ff 1870 match_type,
c45ec17c
PA
1871 source_filename);
1872
1873 if (tmp_tracker.have_completions ())
1874 {
1875 PARSER_STREAM (parser)++;
1876 LS_TOKEN_STOKEN (token).length++;
1877
a5b5adf5
TT
1878 name.reset (savestring (parser->completion_word,
1879 (PARSER_STREAM (parser)
1880 - parser->completion_word)));
c45ec17c
PA
1881 }
1882 }
1883
a5b5adf5 1884 PARSER_EXPLICIT (parser)->function_name = name.release ();
c45ec17c
PA
1885 }
1886 else
1887 {
6a307fc5
TT
1888 /* Try looking it up as a function/method. */
1889 find_linespec_symbols (PARSER_STATE (parser),
a5b5adf5 1890 PARSER_RESULT (parser)->file_symtabs, name.get (),
6a307fc5
TT
1891 PARSER_EXPLICIT (parser)->func_name_match_type,
1892 &symbols, &minimal_symbols);
40e084e1 1893
c2a031c5 1894 if (!symbols.empty () || !minimal_symbols.empty ())
94af9270 1895 {
7243d011 1896 PARSER_RESULT (parser)->function_symbols
7e41c8db 1897 = new std::vector<block_symbol> (std::move (symbols));
c2a031c5
KS
1898 PARSER_RESULT (parser)->minimal_symbols
1899 = new std::vector<bound_minimal_symbol>
dda83cd7 1900 (std::move (minimal_symbols));
a5b5adf5 1901 PARSER_EXPLICIT (parser)->function_name = name.release ();
40e084e1 1902 }
6a307fc5 1903 else
b4013987 1904 {
6a307fc5
TT
1905 /* NAME was not a function or a method. So it must be a label
1906 name or user specified variable like "break foo.c:$zippo". */
1907 labels = find_label_symbols (PARSER_STATE (parser), NULL,
a5b5adf5 1908 &symbols, name.get ());
6a307fc5
TT
1909 if (labels != NULL)
1910 {
1911 PARSER_RESULT (parser)->labels.label_symbols = labels;
7243d011 1912 PARSER_RESULT (parser)->labels.function_symbols
7e41c8db 1913 = new std::vector<block_symbol> (std::move (symbols));
a5b5adf5 1914 PARSER_EXPLICIT (parser)->label_name = name.release ();
6a307fc5
TT
1915 }
1916 else if (token.type == LSTOKEN_STRING
1917 && *LS_TOKEN_STOKEN (token).ptr == '$')
1918 {
1919 /* User specified a convenience variable or history value. */
1920 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1921 = linespec_parse_variable (PARSER_STATE (parser), name.get ());
b4013987 1922
6a307fc5
TT
1923 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
1924 {
1925 /* The user-specified variable was not valid. Do not
1926 throw an error here. parse_linespec will do it for us. */
a5b5adf5 1927 PARSER_EXPLICIT (parser)->function_name = name.release ();
6a307fc5
TT
1928 return;
1929 }
6a307fc5
TT
1930 }
1931 else
b4013987 1932 {
6a307fc5
TT
1933 /* The name is also not a label. Abort parsing. Do not throw
1934 an error here. parse_linespec will do it for us. */
1935
1936 /* Save a copy of the name we were trying to lookup. */
a5b5adf5 1937 PARSER_EXPLICIT (parser)->function_name = name.release ();
b4013987
AA
1938 return;
1939 }
1940 }
c45ec17c
PA
1941 }
1942
1943 int previous_qc = parser->completion_quote_char;
40e084e1
KS
1944
1945 /* Get the next token. */
1946 token = linespec_lexer_consume_token (parser);
1947
c45ec17c
PA
1948 if (token.type == LSTOKEN_EOI)
1949 {
1950 if (previous_qc && !parser->completion_quote_char)
1951 parser->complete_what = linespec_complete_what::KEYWORD;
1952 }
1953 else if (token.type == LSTOKEN_COLON)
40e084e1
KS
1954 {
1955 /* User specified a label or a lineno. */
1956 token = linespec_lexer_consume_token (parser);
1957
1958 if (token.type == LSTOKEN_NUMBER)
1959 {
1960 /* User specified an offset. Record the line offset and
1961 get the next token. */
c45ec17c
PA
1962 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1963
40e084e1 1964 name = copy_token_string (token);
00e52e53 1965 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1966 = linespec_parse_line_offset (name.get ());
40e084e1 1967
c45ec17c 1968 /* Get the next token. */
40e084e1
KS
1969 token = linespec_lexer_consume_token (parser);
1970 }
c45ec17c
PA
1971 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
1972 {
1973 parser->complete_what = linespec_complete_what::LABEL;
1974 }
40e084e1
KS
1975 else if (token.type == LSTOKEN_STRING)
1976 {
c45ec17c
PA
1977 parser->complete_what = linespec_complete_what::LABEL;
1978
1979 /* If we have text after the label separated by whitespace
1980 (e.g., "b func():lab i<tab>"), don't consider it part of
1981 the label. In completion mode that should complete to
1982 "if", in normal mode, the 'i' should be treated as
1983 garbage. */
1984 if (parser->completion_quote_char == '\0')
1985 {
1986 const char *ptr = LS_TOKEN_STOKEN (token).ptr;
1987 for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
1988 {
1989 if (ptr[i] == ' ')
1990 {
1991 LS_TOKEN_STOKEN (token).length = i;
f1735a53 1992 PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
c45ec17c
PA
1993 break;
1994 }
1995 }
1996 }
1997
1998 if (parser->completion_tracker != NULL)
1999 {
2000 if (PARSER_STREAM (parser)[-1] == ' ')
2001 {
2002 parser->completion_word = PARSER_STREAM (parser);
2003 parser->complete_what = linespec_complete_what::KEYWORD;
2004 }
2005 }
2006 else
2007 {
6a307fc5
TT
2008 /* Grab a copy of the label's name and look it up. */
2009 name = copy_token_string (token);
6a307fc5
TT
2010 labels
2011 = find_label_symbols (PARSER_STATE (parser),
2012 PARSER_RESULT (parser)->function_symbols,
a5b5adf5 2013 &symbols, name.get ());
40e084e1 2014
6a307fc5
TT
2015 if (labels != NULL)
2016 {
2017 PARSER_RESULT (parser)->labels.label_symbols = labels;
7243d011 2018 PARSER_RESULT (parser)->labels.function_symbols
7e41c8db 2019 = new std::vector<block_symbol> (std::move (symbols));
a5b5adf5 2020 PARSER_EXPLICIT (parser)->label_name = name.release ();
6a307fc5
TT
2021 }
2022 else
2023 {
2024 /* We don't know what it was, but it isn't a label. */
2025 undefined_label_error
a5b5adf5 2026 (PARSER_EXPLICIT (parser)->function_name, name.get ());
6a307fc5 2027 }
40e084e1 2028
c45ec17c
PA
2029 }
2030
40e084e1 2031 /* Check for a line offset. */
c45ec17c 2032 token = save_stream_and_consume_token (parser);
40e084e1
KS
2033 if (token.type == LSTOKEN_COLON)
2034 {
2035 /* Get the next token. */
2036 token = linespec_lexer_consume_token (parser);
2037
2038 /* It must be a line offset. */
2039 if (token.type != LSTOKEN_NUMBER)
2040 unexpected_linespec_error (parser);
2041
c6756f62 2042 /* Record the line offset and get the next token. */
40e084e1 2043 name = copy_token_string (token);
40e084e1 2044
00e52e53 2045 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 2046 = linespec_parse_line_offset (name.get ());
40e084e1
KS
2047
2048 /* Get the next token. */
2049 token = linespec_lexer_consume_token (parser);
94af9270
KS
2050 }
2051 }
40e084e1
KS
2052 else
2053 {
2054 /* Trailing ':' in the input. Issue an error. */
2055 unexpected_linespec_error (parser);
2056 }
94af9270 2057 }
40e084e1 2058}
94af9270 2059
40e084e1 2060/* Canonicalize the linespec contained in LS. The result is saved into
00e52e53
KS
2061 STATE->canonical. This function handles both linespec and explicit
2062 locations. */
40e084e1
KS
2063
2064static void
1487a14e 2065canonicalize_linespec (struct linespec_state *state, const linespec *ls)
40e084e1 2066{
00e52e53 2067 struct event_location *canon;
67994074 2068 struct explicit_location *explicit_loc;
f00aae0f 2069
40e084e1
KS
2070 /* If canonicalization was not requested, no need to do anything. */
2071 if (!state->canonical)
2072 return;
2073
00e52e53 2074 /* Save everything as an explicit location. */
8e9e35b1
TT
2075 state->canonical->location
2076 = new_explicit_location (&ls->explicit_loc);
2077 canon = state->canonical->location.get ();
67994074 2078 explicit_loc = get_explicit_location (canon);
40e084e1 2079
67994074 2080 if (explicit_loc->label_name != NULL)
a06efdd6 2081 {
00e52e53 2082 state->canonical->special_display = 1;
40e084e1 2083
67994074 2084 if (explicit_loc->function_name == NULL)
40e084e1 2085 {
a06efdd6 2086 /* No function was specified, so add the symbol name. */
7243d011
KS
2087 gdb_assert (!ls->labels.function_symbols->empty ()
2088 && (ls->labels.function_symbols->size () == 1));
7e41c8db 2089 block_symbol s = ls->labels.function_symbols->front ();
987012b8 2090 explicit_loc->function_name = xstrdup (s.symbol->natural_name ());
40e084e1 2091 }
a06efdd6 2092 }
40e084e1 2093
00e52e53
KS
2094 /* If this location originally came from a linespec, save a string
2095 representation of it for display and saving to file. */
2096 if (state->is_linespec)
a06efdd6 2097 {
67994074 2098 char *linespec = explicit_location_to_linespec (explicit_loc);
a06efdd6 2099
00e52e53
KS
2100 set_event_location_string (canon, linespec);
2101 xfree (linespec);
2102 }
94af9270 2103}
c00f8484 2104
40e084e1 2105/* Given a line offset in LS, construct the relevant SALs. */
c00f8484 2106
6c5b2ebe 2107static std::vector<symtab_and_line>
40e084e1 2108create_sals_line_offset (struct linespec_state *self,
1487a14e 2109 linespec *ls)
c00f8484 2110{
40e084e1 2111 int use_default = 0;
c00f8484 2112
40e084e1
KS
2113 /* This is where we need to make sure we have good defaults.
2114 We must guarantee that this section of code is never executed
2e47c6ca 2115 when we are called with just a function name, since
40e084e1
KS
2116 set_default_source_symtab_and_line uses
2117 select_source_symtab that calls us with such an argument. */
2118
2a908241
KS
2119 if (ls->file_symtabs->size () == 1
2120 && ls->file_symtabs->front () == nullptr)
3d50dd94 2121 {
40e084e1 2122 set_current_program_space (self->program_space);
c00f8484 2123
40e084e1
KS
2124 /* Make sure we have at least a default source line. */
2125 set_default_source_symtab_and_line ();
2126 initialize_defaults (&self->default_symtab, &self->default_line);
4717cec4 2127 *ls->file_symtabs
bb995d00
KS
2128 = collect_symtabs_from_filename (self->default_symtab->filename,
2129 self->search_pspace);
40e084e1
KS
2130 use_default = 1;
2131 }
c00f8484 2132
51abb421 2133 symtab_and_line val;
67994074
KS
2134 val.line = ls->explicit_loc.line_offset.offset;
2135 switch (ls->explicit_loc.line_offset.sign)
40e084e1
KS
2136 {
2137 case LINE_OFFSET_PLUS:
67994074 2138 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2139 val.line = 5;
2140 if (use_default)
2141 val.line = self->default_line + val.line;
2142 break;
2143
2144 case LINE_OFFSET_MINUS:
67994074 2145 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2146 val.line = 15;
2147 if (use_default)
2148 val.line = self->default_line - val.line;
2149 else
2150 val.line = -val.line;
2151 break;
2152
2153 case LINE_OFFSET_NONE:
2154 break; /* No need to adjust val.line. */
2155 }
2156
6c5b2ebe 2157 std::vector<symtab_and_line> values;
40e084e1 2158 if (self->list_mode)
6c5b2ebe 2159 values = decode_digits_list_mode (self, ls, val);
40e084e1
KS
2160 else
2161 {
2162 struct linetable_entry *best_entry = NULL;
40e084e1
KS
2163 int i, j;
2164
6c5b2ebe
PA
2165 std::vector<symtab_and_line> intermediate_results
2166 = decode_digits_ordinary (self, ls, val.line, &best_entry);
2167 if (intermediate_results.empty () && best_entry != NULL)
2168 intermediate_results = decode_digits_ordinary (self, ls,
2169 best_entry->line,
2170 &best_entry);
40e084e1
KS
2171
2172 /* For optimized code, the compiler can scatter one source line
2173 across disjoint ranges of PC values, even when no duplicate
2174 functions or inline functions are involved. For example,
2175 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2176 function can result in two PC ranges. In this case, we don't
2177 want to set a breakpoint on the first PC of each range. To filter
2178 such cases, we use containing blocks -- for each PC found
2179 above, we see if there are other PCs that are in the same
2180 block. If yes, the other PCs are filtered out. */
2181
0fc21fd8
TT
2182 gdb::def_vector<int> filter (intermediate_results.size ());
2183 gdb::def_vector<const block *> blocks (intermediate_results.size ());
40e084e1 2184
6c5b2ebe 2185 for (i = 0; i < intermediate_results.size (); ++i)
3d50dd94 2186 {
6c5b2ebe 2187 set_current_program_space (intermediate_results[i].pspace);
c00f8484 2188
40e084e1 2189 filter[i] = 1;
6c5b2ebe
PA
2190 blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
2191 intermediate_results[i].section);
3d50dd94 2192 }
c00f8484 2193
6c5b2ebe 2194 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2195 {
2196 if (blocks[i] != NULL)
6c5b2ebe 2197 for (j = i + 1; j < intermediate_results.size (); ++j)
40e084e1
KS
2198 {
2199 if (blocks[j] == blocks[i])
2200 {
2201 filter[j] = 0;
2202 break;
2203 }
2204 }
2205 }
c00f8484 2206
6c5b2ebe 2207 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2208 if (filter[i])
2209 {
2210 struct symbol *sym = (blocks[i]
2211 ? block_containing_function (blocks[i])
2212 : NULL);
3d50dd94 2213
40e084e1 2214 if (self->funfirstline)
6c5b2ebe 2215 skip_prologue_sal (&intermediate_results[i]);
cd2bb709 2216 intermediate_results[i].symbol = sym;
6c5b2ebe 2217 add_sal_to_sals (self, &values, &intermediate_results[i],
987012b8 2218 sym ? sym->natural_name () : NULL, 0);
40e084e1 2219 }
f17170e5 2220 }
c00f8484 2221
6c5b2ebe 2222 if (values.empty ())
40e084e1 2223 {
67994074 2224 if (ls->explicit_loc.source_filename)
40e084e1 2225 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
67994074 2226 val.line, ls->explicit_loc.source_filename);
40e084e1
KS
2227 else
2228 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2229 val.line);
2230 }
3d50dd94 2231
40e084e1 2232 return values;
c00f8484
KS
2233}
2234
a06efdd6
KS
2235/* Convert the given ADDRESS into SaLs. */
2236
6c5b2ebe 2237static std::vector<symtab_and_line>
a06efdd6
KS
2238convert_address_location_to_sals (struct linespec_state *self,
2239 CORE_ADDR address)
2240{
6c5b2ebe 2241 symtab_and_line sal = find_pc_line (address, 0);
a06efdd6
KS
2242 sal.pc = address;
2243 sal.section = find_pc_overlay (address);
2244 sal.explicit_pc = 1;
cd2bb709 2245 sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
6c5b2ebe
PA
2246
2247 std::vector<symtab_and_line> sals;
a06efdd6
KS
2248 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
2249
2250 return sals;
2251}
2252
40e084e1
KS
2253/* Create and return SALs from the linespec LS. */
2254
6c5b2ebe 2255static std::vector<symtab_and_line>
1487a14e 2256convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
40e084e1 2257{
6c5b2ebe 2258 std::vector<symtab_and_line> sals;
40e084e1 2259
a06efdd6 2260 if (ls->labels.label_symbols != NULL)
40e084e1
KS
2261 {
2262 /* We have just a bunch of functions/methods or labels. */
7e41c8db
KS
2263 struct symtab_and_line sal;
2264
3553eadc 2265 for (const auto &sym : *ls->labels.label_symbols)
40e084e1 2266 {
7e41c8db
KS
2267 struct program_space *pspace
2268 = SYMTAB_PSPACE (symbol_symtab (sym.symbol));
fdbb204b 2269
7e41c8db 2270 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
fdbb204b 2271 && maybe_add_address (state->addr_set, pspace, sal.pc))
64b92e45 2272 add_sal_to_sals (state, &sals, &sal,
987012b8 2273 sym.symbol->natural_name (), 0);
40e084e1
KS
2274 }
2275 }
2276 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2277 {
2278 /* We have just a bunch of functions and/or methods. */
40e084e1
KS
2279 if (ls->function_symbols != NULL)
2280 {
2281 /* Sort symbols so that symbols with the same program space are next
2282 to each other. */
7243d011
KS
2283 std::sort (ls->function_symbols->begin (),
2284 ls->function_symbols->end (),
4dedf84d 2285 compare_symbols);
40e084e1 2286
7243d011 2287 for (const auto &sym : *ls->function_symbols)
40e084e1 2288 {
7e41c8db
KS
2289 program_space *pspace
2290 = SYMTAB_PSPACE (symbol_symtab (sym.symbol));
40e084e1 2291 set_current_program_space (pspace);
76af0f26
PA
2292
2293 /* Don't skip to the first line of the function if we
2294 had found an ifunc minimal symbol for this function,
2295 because that means that this function is an ifunc
2296 resolver with the same name as the ifunc itself. */
2297 bool found_ifunc = false;
2298
2299 if (state->funfirstline
2300 && ls->minimal_symbols != NULL
7e41c8db 2301 && SYMBOL_CLASS (sym.symbol) == LOC_BLOCK)
76af0f26
PA
2302 {
2303 const CORE_ADDR addr
7e41c8db 2304 = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym.symbol));
76af0f26 2305
c2a031c5 2306 for (const auto &elem : *ls->minimal_symbols)
76af0f26 2307 {
c2a031c5
KS
2308 if (MSYMBOL_TYPE (elem.minsym) == mst_text_gnu_ifunc
2309 || MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
76af0f26 2310 {
c2a031c5
KS
2311 CORE_ADDR msym_addr = BMSYMBOL_VALUE_ADDRESS (elem);
2312 if (MSYMBOL_TYPE (elem.minsym) == mst_data_gnu_ifunc)
f50776aa
PA
2313 {
2314 struct gdbarch *gdbarch
08feed99 2315 = elem.objfile->arch ();
f50776aa
PA
2316 msym_addr
2317 = (gdbarch_convert_from_func_ptr_addr
2318 (gdbarch,
2319 msym_addr,
328d42d8 2320 current_inferior ()->top_target ()));
f50776aa
PA
2321 }
2322
2323 if (msym_addr == addr)
2324 {
2325 found_ifunc = true;
2326 break;
2327 }
76af0f26
PA
2328 }
2329 }
2330 }
2331
2332 if (!found_ifunc)
2333 {
2334 symtab_and_line sal;
7e41c8db 2335 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
76af0f26
PA
2336 && maybe_add_address (state->addr_set, pspace, sal.pc))
2337 add_sal_to_sals (state, &sals, &sal,
987012b8 2338 sym.symbol->natural_name (), 0);
76af0f26 2339 }
40e084e1
KS
2340 }
2341 }
2342
2343 if (ls->minimal_symbols != NULL)
2344 {
76af0f26 2345 /* Sort minimal symbols by program space, too */
c2a031c5
KS
2346 std::sort (ls->minimal_symbols->begin (),
2347 ls->minimal_symbols->end (),
2348 compare_msymbols);
40e084e1 2349
c2a031c5 2350 for (const auto &elem : *ls->minimal_symbols)
40e084e1 2351 {
c2a031c5 2352 program_space *pspace = elem.objfile->pspace;
40e084e1 2353 set_current_program_space (pspace);
c2a031c5 2354 minsym_found (state, elem.objfile, elem.minsym, &sals);
40e084e1
KS
2355 }
2356 }
2357 }
67994074 2358 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1
KS
2359 {
2360 /* Only an offset was specified. */
2361 sals = create_sals_line_offset (state, ls);
2362
2363 /* Make sure we have a filename for canonicalization. */
67994074 2364 if (ls->explicit_loc.source_filename == NULL)
05cba821
JK
2365 {
2366 const char *fullname = symtab_to_fullname (state->default_symtab);
2367
e93ba630
JK
2368 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2369 form so that displaying SOURCE_FILENAME can follow the current
2370 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2371 it has been kept for code simplicity only in absolute form. */
67994074 2372 ls->explicit_loc.source_filename = xstrdup (fullname);
05cba821 2373 }
40e084e1
KS
2374 }
2375 else
2376 {
2377 /* We haven't found any results... */
2378 return sals;
2379 }
2380
2381 canonicalize_linespec (state, ls);
2382
6c5b2ebe 2383 if (!sals.empty () && state->canonical != NULL)
40e084e1
KS
2384 state->canonical->pre_expanded = 1;
2385
2386 return sals;
2387}
50641945 2388
a2459270
PA
2389/* Build RESULT from the explicit location components SOURCE_FILENAME,
2390 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
00e52e53 2391
a2459270
PA
2392static void
2393convert_explicit_location_to_linespec (struct linespec_state *self,
1487a14e 2394 linespec *result,
a2459270
PA
2395 const char *source_filename,
2396 const char *function_name,
a20714ff 2397 symbol_name_match_type fname_match_type,
a2459270
PA
2398 const char *label_name,
2399 struct line_offset line_offset)
00e52e53 2400{
7e41c8db
KS
2401 std::vector<block_symbol> symbols;
2402 std::vector<block_symbol> *labels;
c2a031c5 2403 std::vector<bound_minimal_symbol> minimal_symbols;
00e52e53 2404
a20714ff
PA
2405 result->explicit_loc.func_name_match_type = fname_match_type;
2406
a2459270 2407 if (source_filename != NULL)
00e52e53 2408 {
a70b8144 2409 try
00e52e53 2410 {
4717cec4
SM
2411 *result->file_symtabs
2412 = symtabs_from_filename (source_filename, self->search_pspace);
00e52e53 2413 }
230d2906 2414 catch (const gdb_exception_error &except)
00e52e53 2415 {
a2459270 2416 source_file_not_found_error (source_filename);
00e52e53 2417 }
a2459270 2418 result->explicit_loc.source_filename = xstrdup (source_filename);
00e52e53
KS
2419 }
2420 else
2421 {
2422 /* A NULL entry means to use the default symtab. */
2a908241 2423 result->file_symtabs->push_back (nullptr);
00e52e53
KS
2424 }
2425
a2459270 2426 if (function_name != NULL)
00e52e53
KS
2427 {
2428 find_linespec_symbols (self, result->file_symtabs,
a20714ff
PA
2429 function_name, fname_match_type,
2430 &symbols, &minimal_symbols);
00e52e53 2431
c2a031c5 2432 if (symbols.empty () && minimal_symbols.empty ())
a2459270 2433 symbol_not_found_error (function_name,
67994074 2434 result->explicit_loc.source_filename);
00e52e53 2435
a2459270 2436 result->explicit_loc.function_name = xstrdup (function_name);
7243d011 2437 result->function_symbols
7e41c8db 2438 = new std::vector<block_symbol> (std::move (symbols));
c2a031c5
KS
2439 result->minimal_symbols
2440 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
00e52e53
KS
2441 }
2442
a2459270 2443 if (label_name != NULL)
00e52e53 2444 {
00e52e53 2445 labels = find_label_symbols (self, result->function_symbols,
a2459270 2446 &symbols, label_name);
00e52e53
KS
2447
2448 if (labels == NULL)
67994074 2449 undefined_label_error (result->explicit_loc.function_name,
a2459270 2450 label_name);
00e52e53 2451
a2459270 2452 result->explicit_loc.label_name = xstrdup (label_name);
00e52e53 2453 result->labels.label_symbols = labels;
7243d011 2454 result->labels.function_symbols
7e41c8db 2455 = new std::vector<block_symbol> (std::move (symbols));
00e52e53
KS
2456 }
2457
a2459270
PA
2458 if (line_offset.sign != LINE_OFFSET_UNKNOWN)
2459 result->explicit_loc.line_offset = line_offset;
2460}
00e52e53 2461
a2459270
PA
2462/* Convert the explicit location EXPLICIT_LOC into SaLs. */
2463
6c5b2ebe 2464static std::vector<symtab_and_line>
a2459270 2465convert_explicit_location_to_sals (struct linespec_state *self,
1487a14e 2466 linespec *result,
a2459270
PA
2467 const struct explicit_location *explicit_loc)
2468{
2469 convert_explicit_location_to_linespec (self, result,
2470 explicit_loc->source_filename,
2471 explicit_loc->function_name,
a20714ff 2472 explicit_loc->func_name_match_type,
a2459270
PA
2473 explicit_loc->label_name,
2474 explicit_loc->line_offset);
2475 return convert_linespec_to_sals (self, result);
00e52e53
KS
2476}
2477
40e084e1 2478/* Parse a string that specifies a linespec.
50641945 2479
40e084e1 2480 The basic grammar of linespecs:
50641945 2481
a06efdd6 2482 linespec -> var_spec | basic_spec
40e084e1 2483 var_spec -> '$' (STRING | NUMBER)
50641945 2484
40e084e1
KS
2485 basic_spec -> file_offset_spec | function_spec | label_spec
2486 file_offset_spec -> opt_file_spec offset_spec
2487 function_spec -> opt_file_spec function_name_spec opt_label_spec
2488 label_spec -> label_name_spec
50641945 2489
40e084e1
KS
2490 opt_file_spec -> "" | file_name_spec ':'
2491 opt_label_spec -> "" | ':' label_name_spec
2492
2493 file_name_spec -> STRING
2494 function_name_spec -> STRING
2495 label_name_spec -> STRING
2496 function_name_spec -> STRING
2497 offset_spec -> NUMBER
dda83cd7 2498 -> '+' NUMBER
40e084e1
KS
2499 -> '-' NUMBER
2500
2501 This may all be followed by several keywords such as "if EXPR",
2502 which we ignore.
2503
2504 A comma will terminate parsing.
2505
2506 The function may be an undebuggable function found in minimal symbol table.
50641945
FN
2507
2508 If the argument FUNFIRSTLINE is nonzero, we want the first line
2509 of real code inside a function when a function is specified, and it is
2510 not OK to specify a variable or type to get its line number.
2511
2512 DEFAULT_SYMTAB specifies the file to use if none is specified.
2513 It defaults to current_source_symtab.
2514 DEFAULT_LINE specifies the line number to use for relative
2515 line numbers (that start with signs). Defaults to current_source_line.
2516 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 2517 line specs there if necessary. Currently overloaded member functions and
50641945 2518 line numbers or static functions without a filename yield a canonical
1777feb0 2519 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
2520 it is the callers responsibility to free them.
2521
2522 Note that it is possible to return zero for the symtab
2523 if no file is validly specified. Callers must check that.
58438ac1 2524 Also, the line number returned may be invalid. */
50641945 2525
a20714ff
PA
2526/* Parse the linespec in ARG. MATCH_TYPE indicates how function names
2527 should be matched. */
50641945 2528
6c5b2ebe 2529static std::vector<symtab_and_line>
a20714ff
PA
2530parse_linespec (linespec_parser *parser, const char *arg,
2531 symbol_name_match_type match_type)
50641945 2532{
40e084e1 2533 linespec_token token;
cc06b668 2534 struct gdb_exception file_exception;
40e084e1
KS
2535
2536 /* A special case to start. It has become quite popular for
2537 IDEs to work around bugs in the previous parser by quoting
2538 the entire linespec, so we attempt to deal with this nicely. */
2539 parser->is_quote_enclosed = 0;
c45ec17c
PA
2540 if (parser->completion_tracker == NULL
2541 && !is_ada_operator (arg)
f00aae0f 2542 && strchr (linespec_quote_characters, *arg) != NULL)
40e084e1
KS
2543 {
2544 const char *end;
9ef07c8c 2545
f00aae0f 2546 end = skip_quote_char (arg + 1, *arg);
40e084e1 2547 if (end != NULL && is_closing_quote_enclosed (end))
136e1c30 2548 {
f00aae0f 2549 /* Here's the special case. Skip ARG past the initial
40e084e1 2550 quote. */
f00aae0f 2551 ++arg;
40e084e1 2552 parser->is_quote_enclosed = 1;
136e1c30
DE
2553 }
2554 }
e8eb7bc5 2555
f00aae0f
KS
2556 parser->lexer.saved_arg = arg;
2557 parser->lexer.stream = arg;
c45ec17c
PA
2558 parser->completion_word = arg;
2559 parser->complete_what = linespec_complete_what::FUNCTION;
a20714ff 2560 PARSER_EXPLICIT (parser)->func_name_match_type = match_type;
d2630e69 2561
40e084e1
KS
2562 /* Initialize the default symtab and line offset. */
2563 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2564 &PARSER_STATE (parser)->default_line);
d2630e69 2565
40e084e1 2566 /* Objective-C shortcut. */
c45ec17c
PA
2567 if (parser->completion_tracker == NULL)
2568 {
6c5b2ebe
PA
2569 std::vector<symtab_and_line> values
2570 = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2571 if (!values.empty ())
c45ec17c
PA
2572 return values;
2573 }
2574 else
2575 {
2576 /* "-"/"+" is either an objc selector, or a number. There's
2577 nothing to complete the latter to, so just let the caller
2578 complete on functions, which finds objc selectors, if there's
2579 any. */
2580 if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
2581 return {};
2582 }
e0881a8e 2583
40e084e1 2584 /* Start parsing. */
d2630e69 2585
40e084e1 2586 /* Get the first token. */
c45ec17c 2587 token = linespec_lexer_consume_token (parser);
50641945 2588
40e084e1 2589 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
a06efdd6 2590 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
40e084e1 2591 {
40e084e1 2592 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
c45ec17c 2593 if (parser->completion_tracker == NULL)
2a908241 2594 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
dcf9f4ab 2595
40e084e1 2596 /* User specified a convenience variable or history value. */
a5b5adf5 2597 gdb::unique_xmalloc_ptr<char> var = copy_token_string (token);
00e52e53 2598 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 2599 = linespec_parse_variable (PARSER_STATE (parser), var.get ());
f8eba3c6 2600
40e084e1
KS
2601 /* If a line_offset wasn't found (VAR is the name of a user
2602 variable/function), then skip to normal symbol processing. */
00e52e53 2603 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1 2604 {
40e084e1
KS
2605 /* Consume this token. */
2606 linespec_lexer_consume_token (parser);
dcf9f4ab 2607
40e084e1 2608 goto convert_to_sals;
50641945 2609 }
40e084e1 2610 }
c45ec17c
PA
2611 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
2612 {
2613 /* Let the default linespec_complete_what::FUNCTION kick in. */
2614 unexpected_linespec_error (parser);
2615 }
40e084e1 2616 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
c45ec17c
PA
2617 {
2618 parser->complete_what = linespec_complete_what::NOTHING;
2619 unexpected_linespec_error (parser);
2620 }
50641945 2621
40e084e1
KS
2622 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2623 this token cannot represent a filename. */
2624 token = linespec_lexer_peek_token (parser);
0e0b460e 2625
40e084e1 2626 if (token.type == LSTOKEN_COLON)
0e0b460e 2627 {
40e084e1
KS
2628 /* Get the current token again and extract the filename. */
2629 token = linespec_lexer_lex_one (parser);
a5b5adf5 2630 gdb::unique_xmalloc_ptr<char> user_filename = copy_token_string (token);
50641945 2631
40e084e1 2632 /* Check if the input is a filename. */
a70b8144 2633 try
40e084e1 2634 {
4717cec4 2635 *PARSER_RESULT (parser)->file_symtabs
a5b5adf5 2636 = symtabs_from_filename (user_filename.get (),
c2f4122d 2637 PARSER_STATE (parser)->search_pspace);
40e084e1 2638 }
94aeb44b 2639 catch (gdb_exception_error &ex)
7556d4a4 2640 {
94aeb44b 2641 file_exception = std::move (ex);
7556d4a4 2642 }
50641945 2643
40e084e1
KS
2644 if (file_exception.reason >= 0)
2645 {
2646 /* Symtabs were found for the file. Record the filename. */
a5b5adf5 2647 PARSER_EXPLICIT (parser)->source_filename = user_filename.release ();
f8eba3c6 2648
40e084e1
KS
2649 /* Get the next token. */
2650 token = linespec_lexer_consume_token (parser);
50641945 2651
40e084e1
KS
2652 /* This is LSTOKEN_COLON; consume it. */
2653 linespec_lexer_consume_token (parser);
2654 }
2655 else
2656 {
40e084e1 2657 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2a908241 2658 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
40e084e1 2659 }
50641945 2660 }
40e084e1 2661 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
c45ec17c
PA
2662 else if (parser->completion_tracker == NULL
2663 && (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2664 && token.type != LSTOKEN_COMMA))
d2630e69 2665 {
40e084e1
KS
2666 /* TOKEN is the _next_ token, not the one currently in the parser.
2667 Consuming the token will give the correct error message. */
2668 linespec_lexer_consume_token (parser);
2669 unexpected_linespec_error (parser);
d2630e69 2670 }
50641945
FN
2671 else
2672 {
40e084e1 2673 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
2a908241 2674 PARSER_RESULT (parser)->file_symtabs->push_back (nullptr);
50641945 2675 }
50641945 2676
40e084e1
KS
2677 /* Parse the rest of the linespec. */
2678 linespec_parse_basic (parser);
50641945 2679
c45ec17c
PA
2680 if (parser->completion_tracker == NULL
2681 && PARSER_RESULT (parser)->function_symbols == NULL
40e084e1 2682 && PARSER_RESULT (parser)->labels.label_symbols == NULL
00e52e53 2683 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
40e084e1 2684 && PARSER_RESULT (parser)->minimal_symbols == NULL)
f8eba3c6 2685 {
40e084e1
KS
2686 /* The linespec didn't parse. Re-throw the file exception if
2687 there was one. */
2688 if (file_exception.reason < 0)
94aeb44b 2689 throw_exception (std::move (file_exception));
0f5238ed 2690
40e084e1 2691 /* Otherwise, the symbol is not found. */
00e52e53
KS
2692 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2693 PARSER_EXPLICIT (parser)->source_filename);
0f5238ed
TT
2694 }
2695
40e084e1 2696 convert_to_sals:
9ef07c8c 2697
40e084e1
KS
2698 /* Get the last token and record how much of the input was parsed,
2699 if necessary. */
2700 token = linespec_lexer_lex_one (parser);
2701 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
c45ec17c
PA
2702 unexpected_linespec_error (parser);
2703 else if (token.type == LSTOKEN_KEYWORD)
2704 {
2705 /* Setup the completion word past the keyword. Lexing never
2706 advances past a keyword automatically, so skip it
2707 manually. */
2708 parser->completion_word
f1735a53 2709 = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
c45ec17c
PA
2710 parser->complete_what = linespec_complete_what::EXPRESSION;
2711 }
50641945 2712
40e084e1 2713 /* Convert the data in PARSER_RESULT to SALs. */
c45ec17c 2714 if (parser->completion_tracker == NULL)
6c5b2ebe
PA
2715 return convert_linespec_to_sals (PARSER_STATE (parser),
2716 PARSER_RESULT (parser));
f8eba3c6 2717
6c5b2ebe 2718 return {};
413dad4d 2719}
50641945 2720
40e084e1 2721
f8eba3c6 2722/* A constructor for linespec_state. */
44fe14ab 2723
f8eba3c6
TT
2724static void
2725linespec_state_constructor (struct linespec_state *self,
40e084e1 2726 int flags, const struct language_defn *language,
c2f4122d 2727 struct program_space *search_pspace,
f8eba3c6
TT
2728 struct symtab *default_symtab,
2729 int default_line,
2730 struct linespec_result *canonical)
2731{
2732 memset (self, 0, sizeof (*self));
40e084e1 2733 self->language = language;
f8eba3c6
TT
2734 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2735 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
c2f4122d 2736 self->search_pspace = search_pspace;
f8eba3c6
TT
2737 self->default_symtab = default_symtab;
2738 self->default_line = default_line;
2739 self->canonical = canonical;
2740 self->program_space = current_program_space;
2741 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2742 xfree, xcalloc, xfree);
00e52e53 2743 self->is_linespec = 0;
f8eba3c6 2744}
44fe14ab 2745
40e084e1 2746/* Initialize a new linespec parser. */
44fe14ab 2747
3a6ae42d
TT
2748linespec_parser::linespec_parser (int flags,
2749 const struct language_defn *language,
2750 struct program_space *search_pspace,
2751 struct symtab *default_symtab,
2752 int default_line,
2753 struct linespec_result *canonical)
2754{
2755 lexer.current.type = LSTOKEN_CONSUMED;
2756 PARSER_RESULT (this)->file_symtabs = new std::vector<symtab *> ();
2757 PARSER_EXPLICIT (this)->func_name_match_type
a20714ff 2758 = symbol_name_match_type::WILD;
3a6ae42d
TT
2759 PARSER_EXPLICIT (this)->line_offset.sign = LINE_OFFSET_UNKNOWN;
2760 linespec_state_constructor (PARSER_STATE (this), flags, language,
c2f4122d 2761 search_pspace,
40e084e1
KS
2762 default_symtab, default_line, canonical);
2763}
2764
2765/* A destructor for linespec_state. */
44fe14ab 2766
40e084e1
KS
2767static void
2768linespec_state_destructor (struct linespec_state *self)
2769{
f8eba3c6 2770 htab_delete (self->addr_set);
1055a3b4 2771 xfree (self->canonical_names);
f8eba3c6 2772}
44fe14ab 2773
40e084e1
KS
2774/* Delete a linespec parser. */
2775
3a6ae42d 2776linespec_parser::~linespec_parser ()
40e084e1 2777{
3a6ae42d
TT
2778 xfree (PARSER_EXPLICIT (this)->source_filename);
2779 xfree (PARSER_EXPLICIT (this)->label_name);
2780 xfree (PARSER_EXPLICIT (this)->function_name);
40e084e1 2781
3a6ae42d
TT
2782 delete PARSER_RESULT (this)->file_symtabs;
2783 delete PARSER_RESULT (this)->function_symbols;
2784 delete PARSER_RESULT (this)->minimal_symbols;
2785 delete PARSER_RESULT (this)->labels.label_symbols;
2786 delete PARSER_RESULT (this)->labels.function_symbols;
40e084e1 2787
3a6ae42d 2788 linespec_state_destructor (PARSER_STATE (this));
40e084e1
KS
2789}
2790
c7c1b3e9
KS
2791/* See description in linespec.h. */
2792
2793void
f2fc3015 2794linespec_lex_to_end (const char **stringp)
c7c1b3e9 2795{
c7c1b3e9 2796 linespec_token token;
c7c1b3e9
KS
2797 const char *orig;
2798
2799 if (stringp == NULL || *stringp == NULL)
2800 return;
2801
3a6ae42d 2802 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
c7c1b3e9
KS
2803 parser.lexer.saved_arg = *stringp;
2804 PARSER_STREAM (&parser) = orig = *stringp;
2805
2806 do
2807 {
2808 /* Stop before any comma tokens; we need it to keep it
2809 as the next token in the string. */
2810 token = linespec_lexer_peek_token (&parser);
2811 if (token.type == LSTOKEN_COMMA)
2812 break;
c7c1b3e9
KS
2813 token = linespec_lexer_consume_token (&parser);
2814 }
2815 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2816
2817 *stringp += PARSER_STREAM (&parser) - orig;
c7c1b3e9
KS
2818}
2819
c6756f62
PA
2820/* See linespec.h. */
2821
2822void
2823linespec_complete_function (completion_tracker &tracker,
2824 const char *function,
a20714ff 2825 symbol_name_match_type func_match_type,
c6756f62
PA
2826 const char *source_filename)
2827{
2828 complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
2829
2830 if (source_filename != NULL)
2831 {
b5ec771e
PA
2832 collect_file_symbol_completion_matches (tracker, mode, func_match_type,
2833 function, function, source_filename);
c6756f62
PA
2834 }
2835 else
b5ec771e
PA
2836 {
2837 collect_symbol_completion_matches (tracker, mode, func_match_type,
2838 function, function);
2839
2840 }
c6756f62
PA
2841}
2842
c45ec17c
PA
2843/* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2844 only meaningful if COMPONENT is FUNCTION. */
2845
2846static void
2847complete_linespec_component (linespec_parser *parser,
2848 completion_tracker &tracker,
2849 const char *text,
2850 linespec_complete_what component,
2851 const char *source_filename)
2852{
2853 if (component == linespec_complete_what::KEYWORD)
2854 {
2855 complete_on_enum (tracker, linespec_keywords, text, text);
2856 }
2857 else if (component == linespec_complete_what::EXPRESSION)
2858 {
2859 const char *word
2860 = advance_to_expression_complete_word_point (tracker, text);
2861 complete_expression (tracker, text, word);
2862 }
2863 else if (component == linespec_complete_what::FUNCTION)
2864 {
2865 completion_list fn_list;
2866
a20714ff
PA
2867 symbol_name_match_type match_type
2868 = PARSER_EXPLICIT (parser)->func_name_match_type;
2869 linespec_complete_function (tracker, text, match_type, source_filename);
c45ec17c
PA
2870 if (source_filename == NULL)
2871 {
2872 /* Haven't seen a source component, like in "b
2873 file.c:function[TAB]". Maybe this wasn't a function, but
2874 a filename instead, like "b file.[TAB]". */
2875 fn_list = complete_source_filenames (text);
2876 }
2877
2878 /* If we only have a single filename completion, append a ':' for
2879 the user, since that's the only thing that can usefully follow
2880 the filename. */
2881 if (fn_list.size () == 1 && !tracker.have_completions ())
2882 {
2883 char *fn = fn_list[0].release ();
2884
2885 /* If we also need to append a quote char, it needs to be
2886 appended before the ':'. Append it now, and make ':' the
2887 new "quote" char. */
2888 if (tracker.quote_char ())
2889 {
896a7aa6 2890 char quote_char_str[2] = { (char) tracker.quote_char () };
c45ec17c
PA
2891
2892 fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
2893 tracker.set_quote_char (':');
2894 }
2895 else
2896 fn = reconcat (fn, fn, ":", (char *) NULL);
2897 fn_list[0].reset (fn);
2898
2899 /* Tell readline to skip appending a space. */
2900 tracker.set_suppress_append_ws (true);
2901 }
2902 tracker.add_completions (std::move (fn_list));
2903 }
2904}
2905
a2459270
PA
2906/* Helper for linespec_complete_label. Find labels that match
2907 LABEL_NAME in the function symbols listed in the PARSER, and add
2908 them to the tracker. */
2909
2910static void
2911complete_label (completion_tracker &tracker,
2912 linespec_parser *parser,
2913 const char *label_name)
2914{
7e41c8db
KS
2915 std::vector<block_symbol> label_function_symbols;
2916 std::vector<block_symbol> *labels
a2459270
PA
2917 = find_label_symbols (PARSER_STATE (parser),
2918 PARSER_RESULT (parser)->function_symbols,
2919 &label_function_symbols,
2920 label_name, true);
2921
3553eadc 2922 if (labels != nullptr)
a2459270 2923 {
3553eadc
KS
2924 for (const auto &label : *labels)
2925 {
987012b8 2926 char *match = xstrdup (label.symbol->search_name ());
3553eadc
KS
2927 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
2928 }
2929 delete labels;
a2459270 2930 }
a2459270
PA
2931}
2932
2933/* See linespec.h. */
2934
2935void
2936linespec_complete_label (completion_tracker &tracker,
2937 const struct language_defn *language,
2938 const char *source_filename,
2939 const char *function_name,
a20714ff 2940 symbol_name_match_type func_name_match_type,
a2459270
PA
2941 const char *label_name)
2942{
3a6ae42d 2943 linespec_parser parser (0, language, NULL, NULL, 0, NULL);
a2459270
PA
2944
2945 line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
2946
a70b8144 2947 try
a2459270
PA
2948 {
2949 convert_explicit_location_to_linespec (PARSER_STATE (&parser),
2950 PARSER_RESULT (&parser),
2951 source_filename,
2952 function_name,
a20714ff 2953 func_name_match_type,
a2459270
PA
2954 NULL, unknown_offset);
2955 }
230d2906 2956 catch (const gdb_exception_error &ex)
a2459270 2957 {
a2459270
PA
2958 return;
2959 }
a2459270
PA
2960
2961 complete_label (tracker, &parser, label_name);
a2459270
PA
2962}
2963
c45ec17c
PA
2964/* See description in linespec.h. */
2965
2966void
a20714ff
PA
2967linespec_complete (completion_tracker &tracker, const char *text,
2968 symbol_name_match_type match_type)
c45ec17c 2969{
c45ec17c
PA
2970 const char *orig = text;
2971
3a6ae42d 2972 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
c45ec17c 2973 parser.lexer.saved_arg = text;
a20714ff 2974 PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
c45ec17c
PA
2975 PARSER_STREAM (&parser) = text;
2976
2977 parser.completion_tracker = &tracker;
2978 PARSER_STATE (&parser)->is_linespec = 1;
2979
2980 /* Parse as much as possible. parser.completion_word will hold
2981 furthest completion point we managed to parse to. */
a70b8144 2982 try
c45ec17c 2983 {
a20714ff 2984 parse_linespec (&parser, text, match_type);
c45ec17c 2985 }
230d2906 2986 catch (const gdb_exception_error &except)
c45ec17c
PA
2987 {
2988 }
c45ec17c
PA
2989
2990 if (parser.completion_quote_char != '\0'
2991 && parser.completion_quote_end != NULL
2992 && parser.completion_quote_end[1] == '\0')
2993 {
2994 /* If completing a quoted string with the cursor right at
2995 terminating quote char, complete the completion word without
2996 interpretation, so that readline advances the cursor one
2997 whitespace past the quote, even if there's no match. This
2998 makes these cases behave the same:
2999
3000 before: "b function()"
3001 after: "b function() "
3002
3003 before: "b 'function()'"
3004 after: "b 'function()' "
3005
3006 and trusts the user in this case:
3007
3008 before: "b 'not_loaded_function_yet()'"
3009 after: "b 'not_loaded_function_yet()' "
3010 */
3011 parser.complete_what = linespec_complete_what::NOTHING;
3012 parser.completion_quote_char = '\0';
3013
3014 gdb::unique_xmalloc_ptr<char> text_copy
3015 (xstrdup (parser.completion_word));
3016 tracker.add_completion (std::move (text_copy));
3017 }
3018
3019 tracker.set_quote_char (parser.completion_quote_char);
3020
3021 if (parser.complete_what == linespec_complete_what::LABEL)
3022 {
3023 parser.complete_what = linespec_complete_what::NOTHING;
3024
3025 const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
3026
7e41c8db 3027 std::vector<block_symbol> function_symbols;
c2a031c5 3028 std::vector<bound_minimal_symbol> minimal_symbols;
c45ec17c
PA
3029 find_linespec_symbols (PARSER_STATE (&parser),
3030 PARSER_RESULT (&parser)->file_symtabs,
a20714ff 3031 func_name, match_type,
c45ec17c
PA
3032 &function_symbols, &minimal_symbols);
3033
7243d011 3034 PARSER_RESULT (&parser)->function_symbols
7e41c8db 3035 = new std::vector<block_symbol> (std::move (function_symbols));
c2a031c5
KS
3036 PARSER_RESULT (&parser)->minimal_symbols
3037 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
c45ec17c
PA
3038
3039 complete_label (tracker, &parser, parser.completion_word);
3040 }
3041 else if (parser.complete_what == linespec_complete_what::FUNCTION)
3042 {
3043 /* While parsing/lexing, we didn't know whether the completion
3044 word completes to a unique function/source name already or
3045 not.
3046
3047 E.g.:
3048 "b function() <tab>"
3049 may need to complete either to:
3050 "b function() const"
3051 or to:
3052 "b function() if/thread/task"
3053
3054 Or, this:
3055 "b foo t"
3056 may need to complete either to:
3057 "b foo template_fun<T>()"
3058 with "foo" being the template function's return type, or to:
3059 "b foo thread/task"
3060
3061 Or, this:
3062 "b file<TAB>"
3063 may need to complete either to a source file name:
3064 "b file.c"
3065 or this, also a filename, but a unique completion:
3066 "b file.c:"
3067 or to a function name:
3068 "b file_function"
3069
3070 Address that by completing assuming source or function, and
3071 seeing if we find a completion that matches exactly the
3072 completion word. If so, then it must be a function (see note
3073 below) and we advance the completion word to the end of input
3074 and switch to KEYWORD completion mode.
3075
3076 Note: if we find a unique completion for a source filename,
3077 then it won't match the completion word, because the LCD will
3078 contain a trailing ':'. And if we're completing at or after
3079 the ':', then complete_linespec_component won't try to
3080 complete on source filenames. */
3081
c45ec17c
PA
3082 const char *word = parser.completion_word;
3083
3084 complete_linespec_component (&parser, tracker,
3085 parser.completion_word,
3086 linespec_complete_what::FUNCTION,
3087 PARSER_EXPLICIT (&parser)->source_filename);
3088
3089 parser.complete_what = linespec_complete_what::NOTHING;
3090
3091 if (tracker.quote_char ())
3092 {
3093 /* The function/file name was not close-quoted, so this
3094 can't be a keyword. Note: complete_linespec_component
3095 may have swapped the original quote char for ':' when we
3096 get here, but that still indicates the same. */
3097 }
3098 else if (!tracker.have_completions ())
3099 {
3100 size_t key_start;
3101 size_t wordlen = strlen (parser.completion_word);
3102
3103 key_start
3104 = string_find_incomplete_keyword_at_end (linespec_keywords,
3105 parser.completion_word,
3106 wordlen);
3107
3108 if (key_start != -1
3109 || (wordlen > 0
3110 && parser.completion_word[wordlen - 1] == ' '))
3111 {
3112 parser.completion_word += key_start;
3113 parser.complete_what = linespec_complete_what::KEYWORD;
3114 }
3115 }
3116 else if (tracker.completes_to_completion_word (word))
3117 {
3118 /* Skip the function and complete on keywords. */
3119 parser.completion_word += strlen (word);
3120 parser.complete_what = linespec_complete_what::KEYWORD;
3121 tracker.discard_completions ();
3122 }
3123 }
3124
3125 tracker.advance_custom_word_point_by (parser.completion_word - orig);
3126
3127 complete_linespec_component (&parser, tracker,
3128 parser.completion_word,
3129 parser.complete_what,
3130 PARSER_EXPLICIT (&parser)->source_filename);
3131
3132 /* If we're past the "filename:function:label:offset" linespec, and
3133 didn't find any match, then assume the user might want to create
3134 a pending breakpoint anyway and offer the keyword
3135 completions. */
3136 if (!parser.completion_quote_char
3137 && (parser.complete_what == linespec_complete_what::FUNCTION
3138 || parser.complete_what == linespec_complete_what::LABEL
3139 || parser.complete_what == linespec_complete_what::NOTHING)
3140 && !tracker.have_completions ())
3141 {
3142 const char *end
3143 = parser.completion_word + strlen (parser.completion_word);
3144
3145 if (end > orig && end[-1] == ' ')
3146 {
3147 tracker.advance_custom_word_point_by (end - parser.completion_word);
3148
3149 complete_linespec_component (&parser, tracker, end,
3150 linespec_complete_what::KEYWORD,
3151 NULL);
3152 }
3153 }
c45ec17c
PA
3154}
3155
f00aae0f 3156/* A helper function for decode_line_full and decode_line_1 to
6c5b2ebe 3157 turn LOCATION into std::vector<symtab_and_line>. */
f00aae0f 3158
6c5b2ebe 3159static std::vector<symtab_and_line>
f00aae0f
KS
3160event_location_to_sals (linespec_parser *parser,
3161 const struct event_location *location)
3162{
6c5b2ebe 3163 std::vector<symtab_and_line> result;
f00aae0f
KS
3164
3165 switch (event_location_type (location))
3166 {
3167 case LINESPEC_LOCATION:
3168 {
00e52e53 3169 PARSER_STATE (parser)->is_linespec = 1;
a70b8144 3170 try
f00aae0f 3171 {
a20714ff
PA
3172 const linespec_location *ls = get_linespec_location (location);
3173 result = parse_linespec (parser,
3174 ls->spec_string, ls->match_type);
f00aae0f 3175 }
230d2906 3176 catch (const gdb_exception_error &except)
f00aae0f 3177 {
eedc3f4f 3178 throw;
f00aae0f 3179 }
f00aae0f
KS
3180 }
3181 break;
3182
a06efdd6 3183 case ADDRESS_LOCATION:
305e13e6
JB
3184 {
3185 const char *addr_string = get_address_string_location (location);
3186 CORE_ADDR addr = get_address_location (location);
3187
3188 if (addr_string != NULL)
3189 {
9be2c17a 3190 addr = linespec_expression_to_pc (&addr_string);
305e13e6
JB
3191 if (PARSER_STATE (parser)->canonical != NULL)
3192 PARSER_STATE (parser)->canonical->location
8e9e35b1 3193 = copy_event_location (location);
305e13e6
JB
3194 }
3195
3196 result = convert_address_location_to_sals (PARSER_STATE (parser),
3197 addr);
3198 }
a06efdd6
KS
3199 break;
3200
00e52e53
KS
3201 case EXPLICIT_LOCATION:
3202 {
67994074 3203 const struct explicit_location *explicit_loc;
00e52e53 3204
67994074 3205 explicit_loc = get_explicit_location_const (location);
00e52e53
KS
3206 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
3207 PARSER_RESULT (parser),
67994074 3208 explicit_loc);
00e52e53
KS
3209 }
3210 break;
3211
5b56227b
KS
3212 case PROBE_LOCATION:
3213 /* Probes are handled by their own decoders. */
3214 gdb_assert_not_reached ("attempt to decode probe location");
3215 break;
3216
f00aae0f
KS
3217 default:
3218 gdb_assert_not_reached ("unhandled event location type");
3219 }
3220
3221 return result;
3222}
3223
f8eba3c6 3224/* See linespec.h. */
44fe14ab 3225
f8eba3c6 3226void
626d2320 3227decode_line_full (struct event_location *location, int flags,
c2f4122d 3228 struct program_space *search_pspace,
f8eba3c6
TT
3229 struct symtab *default_symtab,
3230 int default_line, struct linespec_result *canonical,
3231 const char *select_mode,
3232 const char *filter)
44fe14ab 3233{
f73c6ece 3234 std::vector<const char *> filters;
40e084e1 3235 struct linespec_state *state;
f8eba3c6
TT
3236
3237 gdb_assert (canonical != NULL);
3238 /* The filter only makes sense for 'all'. */
3239 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
3240 gdb_assert (select_mode == NULL
3241 || select_mode == multiple_symbols_all
3242 || select_mode == multiple_symbols_ask
3243 || select_mode == multiple_symbols_cancel);
3244 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
3245
3a6ae42d
TT
3246 linespec_parser parser (flags, current_language,
3247 search_pspace, default_symtab,
3248 default_line, canonical);
5ed8105e
PA
3249
3250 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3251
6c5b2ebe
PA
3252 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3253 location);
40e084e1 3254 state = PARSER_STATE (&parser);
f8eba3c6 3255
626d2320
TV
3256 if (result.size () == 0)
3257 throw_error (NOT_SUPPORTED_ERROR, _("Location %s not available"),
3258 event_location_to_string (location));
3259
6c5b2ebe 3260 gdb_assert (result.size () == 1 || canonical->pre_expanded);
f8eba3c6
TT
3261 canonical->pre_expanded = 1;
3262
66f1999b 3263 /* Arrange for allocated canonical names to be freed. */
3a6ae42d
TT
3264 std::vector<gdb::unique_xmalloc_ptr<char>> hold_names;
3265 for (int i = 0; i < result.size (); ++i)
f8eba3c6 3266 {
3a6ae42d
TT
3267 gdb_assert (state->canonical_names[i].suffix != NULL);
3268 hold_names.emplace_back (state->canonical_names[i].suffix);
f8eba3c6
TT
3269 }
3270
3271 if (select_mode == NULL)
3272 {
29f94340 3273 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
f8eba3c6
TT
3274 select_mode = multiple_symbols_all;
3275 else
3276 select_mode = multiple_symbols_select_mode ();
3277 }
3278
3279 if (select_mode == multiple_symbols_all)
3280 {
3281 if (filter != NULL)
3282 {
f73c6ece 3283 filters.push_back (filter);
40e084e1 3284 filter_results (state, &result, filters);
f8eba3c6
TT
3285 }
3286 else
40e084e1 3287 convert_results_to_lsals (state, &result);
f8eba3c6
TT
3288 }
3289 else
40e084e1 3290 decode_line_2 (state, &result, select_mode);
f8eba3c6
TT
3291}
3292
39cf75f7
DE
3293/* See linespec.h. */
3294
6c5b2ebe 3295std::vector<symtab_and_line>
f00aae0f 3296decode_line_1 (const struct event_location *location, int flags,
c2f4122d 3297 struct program_space *search_pspace,
f8eba3c6
TT
3298 struct symtab *default_symtab,
3299 int default_line)
3300{
3a6ae42d
TT
3301 linespec_parser parser (flags, current_language,
3302 search_pspace, default_symtab,
3303 default_line, NULL);
5ed8105e
PA
3304
3305 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3306
3a6ae42d 3307 return event_location_to_sals (&parser, location);
f8eba3c6
TT
3308}
3309
39cf75f7
DE
3310/* See linespec.h. */
3311
6c5b2ebe 3312std::vector<symtab_and_line>
f2fc3015 3313decode_line_with_current_source (const char *string, int flags)
39cf75f7 3314{
39cf75f7
DE
3315 if (string == 0)
3316 error (_("Empty line specification."));
3317
3318 /* We use whatever is set as the current source line. We do not try
3319 and get a default source symtab+line or it will recursively call us! */
6c5b2ebe 3320 symtab_and_line cursal = get_current_source_symtab_and_line ();
39cf75f7 3321
ffc2605c
TT
3322 event_location_up location = string_to_event_location (&string,
3323 current_language);
6c5b2ebe
PA
3324 std::vector<symtab_and_line> sals
3325 = decode_line_1 (location.get (), flags, NULL, cursal.symtab, cursal.line);
39cf75f7
DE
3326
3327 if (*string)
3328 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3329
39cf75f7
DE
3330 return sals;
3331}
3332
3333/* See linespec.h. */
3334
6c5b2ebe 3335std::vector<symtab_and_line>
f2fc3015 3336decode_line_with_last_displayed (const char *string, int flags)
39cf75f7 3337{
39cf75f7
DE
3338 if (string == 0)
3339 error (_("Empty line specification."));
3340
ffc2605c
TT
3341 event_location_up location = string_to_event_location (&string,
3342 current_language);
6c5b2ebe
PA
3343 std::vector<symtab_and_line> sals
3344 = (last_displayed_sal_is_valid ()
3345 ? decode_line_1 (location.get (), flags, NULL,
3346 get_last_displayed_symtab (),
3347 get_last_displayed_line ())
cafb3438 3348 : decode_line_1 (location.get (), flags, NULL, NULL, 0));
39cf75f7
DE
3349
3350 if (*string)
3351 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3352
39cf75f7
DE
3353 return sals;
3354}
3355
f8eba3c6
TT
3356\f
3357
85102364 3358/* First, some functions to initialize stuff at the beginning of the
f8eba3c6
TT
3359 function. */
3360
3361static void
3362initialize_defaults (struct symtab **default_symtab, int *default_line)
3363{
3364 if (*default_symtab == 0)
3365 {
3366 /* Use whatever we have for the default source line. We don't use
dda83cd7 3367 get_current_or_default_symtab_and_line as it can recurse and call
f8eba3c6
TT
3368 us back! */
3369 struct symtab_and_line cursal =
3370 get_current_source_symtab_and_line ();
3371
3372 *default_symtab = cursal.symtab;
3373 *default_line = cursal.line;
3374 }
3375}
3376
3377\f
3378
40e084e1
KS
3379/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3380 advancing EXP_PTR past any parsed text. */
f8eba3c6 3381
a06efdd6 3382CORE_ADDR
bbc13ae3 3383linespec_expression_to_pc (const char **exp_ptr)
f8eba3c6 3384{
f8eba3c6
TT
3385 if (current_program_space->executing_startup)
3386 /* The error message doesn't really matter, because this case
3387 should only hit during breakpoint reset. */
3388 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
3389 "program space is in startup"));
3390
40e084e1
KS
3391 (*exp_ptr)++;
3392 return value_as_address (parse_to_comma_and_eval (exp_ptr));
0960f083
DC
3393}
3394
3395\f
3396
d2630e69
AF
3397/* Here's where we recognise an Objective-C Selector. An Objective C
3398 selector may be implemented by more than one class, therefore it
3399 may represent more than one method/function. This gives us a
3400 situation somewhat analogous to C++ overloading. If there's more
3401 than one method that could represent the selector, then use some of
3402 the existing C++ code to let the user choose one. */
3403
6c5b2ebe 3404static std::vector<symtab_and_line>
1487a14e 3405decode_objc (struct linespec_state *self, linespec *ls, const char *arg)
d2630e69 3406{
f8eba3c6 3407 struct collect_info info;
9b2f8581 3408 std::vector<const char *> symbol_names;
d7561cbb 3409 const char *new_argptr;
f8eba3c6
TT
3410
3411 info.state = self;
2a908241
KS
3412 std::vector<symtab *> symtabs;
3413 symtabs.push_back (nullptr);
3414
3415 info.file_symtabs = &symtabs;
7243d011 3416
7e41c8db 3417 std::vector<block_symbol> symbols;
7243d011 3418 info.result.symbols = &symbols;
c2a031c5
KS
3419 std::vector<bound_minimal_symbol> minimal_symbols;
3420 info.result.minimal_symbols = &minimal_symbols;
f8eba3c6 3421
f00aae0f 3422 new_argptr = find_imps (arg, &symbol_names);
9b2f8581 3423 if (symbol_names.empty ())
2a908241 3424 return {};
d2630e69 3425
56d87ef7
PA
3426 add_all_symbol_names_from_pspace (&info, NULL, symbol_names,
3427 FUNCTIONS_DOMAIN);
d2630e69 3428
6c5b2ebe 3429 std::vector<symtab_and_line> values;
c2a031c5 3430 if (!symbols.empty () || !minimal_symbols.empty ())
d2630e69 3431 {
f8eba3c6 3432 char *saved_arg;
d2630e69 3433
224c3ddb 3434 saved_arg = (char *) alloca (new_argptr - arg + 1);
f00aae0f
KS
3435 memcpy (saved_arg, arg, new_argptr - arg);
3436 saved_arg[new_argptr - arg] = '\0';
d2630e69 3437
67994074 3438 ls->explicit_loc.function_name = xstrdup (saved_arg);
7e41c8db
KS
3439 ls->function_symbols
3440 = new std::vector<block_symbol> (std::move (symbols));
c2a031c5
KS
3441 ls->minimal_symbols
3442 = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
40e084e1
KS
3443 values = convert_linespec_to_sals (self, ls);
3444
f8eba3c6 3445 if (self->canonical)
d2630e69 3446 {
f2fc3015
TT
3447 std::string holder;
3448 const char *str;
f00aae0f 3449
f8eba3c6 3450 self->canonical->pre_expanded = 1;
f00aae0f 3451
67994074 3452 if (ls->explicit_loc.source_filename)
f00aae0f 3453 {
f2fc3015
TT
3454 holder = string_printf ("%s:%s",
3455 ls->explicit_loc.source_filename,
3456 saved_arg);
3457 str = holder.c_str ();
f00aae0f 3458 }
f8eba3c6 3459 else
f2fc3015 3460 str = saved_arg;
f00aae0f 3461
a20714ff
PA
3462 self->canonical->location
3463 = new_linespec_location (&str, symbol_name_match_type::FULL);
d2630e69 3464 }
d2630e69
AF
3465 }
3466
40e084e1 3467 return values;
f8eba3c6 3468}
c00f8484 3469
ffdbe864
YQ
3470namespace {
3471
14bc53a8
PA
3472/* A function object that serves as symbol_found_callback_ftype
3473 callback for iterate_over_symbols. This is used by
3474 lookup_prefix_sym to collect type symbols. */
3475class decode_compound_collector
f8eba3c6 3476{
14bc53a8 3477public:
fc4007c9 3478 decode_compound_collector ()
7a8a5d47
TT
3479 : m_unique_syms (htab_create_alloc (1, htab_hash_pointer,
3480 htab_eq_pointer, NULL,
3481 xcalloc, xfree))
fc4007c9 3482 {
fc4007c9 3483 }
3a93a0c2 3484
4dedf84d 3485 /* Return all symbols collected. */
7e41c8db 3486 std::vector<block_symbol> release_symbols ()
14bc53a8 3487 {
4dedf84d 3488 return std::move (m_symbols);
14bc53a8 3489 }
c00f8484 3490
14bc53a8 3491 /* Callable as a symbol_found_callback_ftype callback. */
7e41c8db 3492 bool operator () (block_symbol *bsym);
14bc53a8
PA
3493
3494private:
3495 /* A hash table of all symbols we found. We use this to avoid
3496 adding any symbol more than once. */
7a8a5d47 3497 htab_up m_unique_syms;
14bc53a8
PA
3498
3499 /* The result vector. */
7e41c8db 3500 std::vector<block_symbol> m_symbols;
14bc53a8
PA
3501};
3502
3503bool
7e41c8db 3504decode_compound_collector::operator () (block_symbol *bsym)
f8eba3c6 3505{
f8eba3c6
TT
3506 void **slot;
3507 struct type *t;
7e41c8db 3508 struct symbol *sym = bsym->symbol;
614b3b14 3509
f8eba3c6 3510 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
14bc53a8 3511 return true; /* Continue iterating. */
f8eba3c6
TT
3512
3513 t = SYMBOL_TYPE (sym);
f168693b 3514 t = check_typedef (t);
78134374
SM
3515 if (t->code () != TYPE_CODE_STRUCT
3516 && t->code () != TYPE_CODE_UNION
3517 && t->code () != TYPE_CODE_NAMESPACE)
14bc53a8 3518 return true; /* Continue iterating. */
614b3b14 3519
7a8a5d47 3520 slot = htab_find_slot (m_unique_syms.get (), sym, INSERT);
f8eba3c6
TT
3521 if (!*slot)
3522 {
3523 *slot = sym;
7e41c8db 3524 m_symbols.push_back (*bsym);
f8eba3c6
TT
3525 }
3526
14bc53a8 3527 return true; /* Continue iterating. */
f8eba3c6 3528}
93d91629 3529
ffdbe864
YQ
3530} // namespace
3531
40e084e1 3532/* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
93d91629 3533
7e41c8db 3534static std::vector<block_symbol>
2a908241
KS
3535lookup_prefix_sym (struct linespec_state *state,
3536 std::vector<symtab *> *file_symtabs,
40e084e1 3537 const char *class_name)
93d91629 3538{
14bc53a8 3539 decode_compound_collector collector;
e0881a8e 3540
b5ec771e
PA
3541 lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
3542
2a908241 3543 for (const auto &elt : *file_symtabs)
f8eba3c6 3544 {
2a908241 3545 if (elt == nullptr)
f8eba3c6 3546 {
b5ec771e 3547 iterate_over_all_matching_symtabs (state, lookup_name,
56d87ef7
PA
3548 STRUCT_DOMAIN, ALL_DOMAIN,
3549 NULL, false, collector);
b5ec771e 3550 iterate_over_all_matching_symtabs (state, lookup_name,
56d87ef7
PA
3551 VAR_DOMAIN, ALL_DOMAIN,
3552 NULL, false, collector);
f8eba3c6
TT
3553 }
3554 else
3555 {
f8eba3c6
TT
3556 /* Program spaces that are executing startup should have
3557 been filtered out earlier. */
3558 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3559 set_current_program_space (SYMTAB_PSPACE (elt));
b5ec771e
PA
3560 iterate_over_file_blocks (elt, lookup_name, STRUCT_DOMAIN, collector);
3561 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN, collector);
1e5a1abc
KS
3562 }
3563 }
3564
14bc53a8 3565 return collector.release_symbols ();
93d91629
DC
3566}
3567
7243d011
KS
3568/* A std::sort comparison function for symbols. The resulting order does
3569 not actually matter; we just need to be able to sort them so that
3570 symbols with the same program space end up next to each other. */
3571
3572static bool
7e41c8db 3573compare_symbols (const block_symbol &a, const block_symbol &b)
7243d011
KS
3574{
3575 uintptr_t uia, uib;
3576
7e41c8db
KS
3577 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (a.symbol));
3578 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (b.symbol));
7243d011
KS
3579
3580 if (uia < uib)
3581 return true;
3582 if (uia > uib)
3583 return false;
3584
7e41c8db
KS
3585 uia = (uintptr_t) a.symbol;
3586 uib = (uintptr_t) b.symbol;
7243d011
KS
3587
3588 if (uia < uib)
3589 return true;
3590
3591 return false;
3592}
3593
40e084e1 3594/* Like compare_symbols but for minimal symbols. */
4224873a 3595
c2a031c5
KS
3596static bool
3597compare_msymbols (const bound_minimal_symbol &a, const bound_minimal_symbol &b)
4224873a 3598{
f8eba3c6
TT
3599 uintptr_t uia, uib;
3600
c2a031c5
KS
3601 uia = (uintptr_t) a.objfile->pspace;
3602 uib = (uintptr_t) a.objfile->pspace;
f8eba3c6
TT
3603
3604 if (uia < uib)
c2a031c5 3605 return true;
f8eba3c6 3606 if (uia > uib)
c2a031c5 3607 return false;
f8eba3c6 3608
c2a031c5
KS
3609 uia = (uintptr_t) a.minsym;
3610 uib = (uintptr_t) b.minsym;
f8eba3c6
TT
3611
3612 if (uia < uib)
c2a031c5 3613 return true;
f8eba3c6 3614
c2a031c5 3615 return false;
f8eba3c6
TT
3616}
3617
3618/* Look for all the matching instances of each symbol in NAMES. Only
3619 instances from PSPACE are considered; other program spaces are
3620 handled by our caller. If PSPACE is NULL, then all program spaces
3621 are considered. Results are stored into INFO. */
3622
3623static void
3624add_all_symbol_names_from_pspace (struct collect_info *info,
3625 struct program_space *pspace,
9b2f8581 3626 const std::vector<const char *> &names,
56d87ef7 3627 enum search_domain search_domain)
f8eba3c6 3628{
9b2f8581 3629 for (const char *iter : names)
b5ec771e
PA
3630 add_matching_symbols_to_info (iter,
3631 symbol_name_match_type::FULL,
56d87ef7 3632 search_domain, info, pspace);
f8eba3c6
TT
3633}
3634
3635static void
8e8d776e 3636find_superclass_methods (std::vector<struct type *> &&superclasses,
b5ec771e 3637 const char *name, enum language name_lang,
9b2f8581 3638 std::vector<const char *> *result_names)
f8eba3c6 3639{
9b2f8581 3640 size_t old_len = result_names->size ();
f8eba3c6 3641
f8eba3c6
TT
3642 while (1)
3643 {
8e8d776e 3644 std::vector<struct type *> new_supers;
f8eba3c6 3645
52941706 3646 for (type *t : superclasses)
b5ec771e 3647 find_methods (t, name_lang, name, result_names, &new_supers);
f8eba3c6 3648
8e8d776e 3649 if (result_names->size () != old_len || new_supers.empty ())
f8eba3c6 3650 break;
4224873a 3651
8e8d776e 3652 superclasses = std::move (new_supers);
f8eba3c6 3653 }
f8eba3c6
TT
3654}
3655
40e084e1
KS
3656/* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3657 given by one of the symbols in SYM_CLASSES. Matches are returned
3658 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
f8eba3c6 3659
40e084e1 3660static void
2a908241 3661find_method (struct linespec_state *self, std::vector<symtab *> *file_symtabs,
40e084e1 3662 const char *class_name, const char *method_name,
7e41c8db
KS
3663 std::vector<block_symbol> *sym_classes,
3664 std::vector<block_symbol> *symbols,
c2a031c5 3665 std::vector<bound_minimal_symbol> *minsyms)
f8eba3c6 3666{
9b2f8581 3667 size_t last_result_len;
8e8d776e 3668 std::vector<struct type *> superclass_vec;
9b2f8581 3669 std::vector<const char *> result_names;
f8eba3c6 3670 struct collect_info info;
4224873a 3671
f8eba3c6
TT
3672 /* Sort symbols so that symbols with the same program space are next
3673 to each other. */
4dedf84d
KS
3674 std::sort (sym_classes->begin (), sym_classes->end (),
3675 compare_symbols);
f8eba3c6
TT
3676
3677 info.state = self;
40e084e1 3678 info.file_symtabs = file_symtabs;
7243d011 3679 info.result.symbols = symbols;
c2a031c5 3680 info.result.minimal_symbols = minsyms;
f8eba3c6
TT
3681
3682 /* Iterate over all the types, looking for the names of existing
40e084e1 3683 methods matching METHOD_NAME. If we cannot find a direct method in a
f8eba3c6
TT
3684 given program space, then we consider inherited methods; this is
3685 not ideal (ideal would be to respect C++ hiding rules), but it
3686 seems good enough and is what GDB has historically done. We only
3687 need to collect the names because later we find all symbols with
3688 those names. This loop is written in a somewhat funny way
3689 because we collect data across the program space before deciding
3690 what to do. */
f8eba3c6 3691 last_result_len = 0;
7e41c8db 3692 for (const auto &elt : *sym_classes)
f8eba3c6
TT
3693 {
3694 struct type *t;
3695 struct program_space *pspace;
7e41c8db 3696 struct symbol *sym = elt.symbol;
ea90f227 3697 unsigned int ix = &elt - &*sym_classes->begin ();
f8eba3c6
TT
3698
3699 /* Program spaces that are executing startup should have
3700 been filtered out earlier. */
08be3fe3
DE
3701 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
3702 gdb_assert (!pspace->executing_startup);
f8eba3c6
TT
3703 set_current_program_space (pspace);
3704 t = check_typedef (SYMBOL_TYPE (sym));
c1b5c1eb 3705 find_methods (t, sym->language (),
b5ec771e 3706 method_name, &result_names, &superclass_vec);
f8eba3c6
TT
3707
3708 /* Handle all items from a single program space at once; and be
3709 sure not to miss the last batch. */
4dedf84d 3710 if (ix == sym_classes->size () - 1
f8eba3c6 3711 || (pspace
7e41c8db 3712 != SYMTAB_PSPACE (symbol_symtab (sym_classes->at (ix + 1).symbol))))
4224873a 3713 {
f8eba3c6
TT
3714 /* If we did not find a direct implementation anywhere in
3715 this program space, consider superclasses. */
9b2f8581 3716 if (result_names.size () == last_result_len)
8e8d776e 3717 find_superclass_methods (std::move (superclass_vec), method_name,
c1b5c1eb 3718 sym->language (), &result_names);
f8eba3c6
TT
3719
3720 /* We have a list of candidate symbol names, so now we
3721 iterate over the symbol tables looking for all
3722 matches in this pspace. */
56d87ef7
PA
3723 add_all_symbol_names_from_pspace (&info, pspace, result_names,
3724 FUNCTIONS_DOMAIN);
f8eba3c6 3725
8e8d776e 3726 superclass_vec.clear ();
9b2f8581 3727 last_result_len = result_names.size ();
4224873a 3728 }
4224873a 3729 }
f8eba3c6 3730
c2a031c5
KS
3731 if (!symbols->empty () || !minsyms->empty ())
3732 return;
f8eba3c6 3733
40e084e1
KS
3734 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3735 and other attempts to locate the symbol will be made. */
3736 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
f8eba3c6
TT
3737}
3738
3739\f
3740
ffdbe864
YQ
3741namespace {
3742
14bc53a8
PA
3743/* This function object is a callback for iterate_over_symtabs, used
3744 when collecting all matching symtabs. */
f8eba3c6 3745
14bc53a8 3746class symtab_collector
f8eba3c6 3747{
14bc53a8 3748public:
fc4007c9 3749 symtab_collector ()
7a8a5d47
TT
3750 : m_symtab_table (htab_create (1, htab_hash_pointer, htab_eq_pointer,
3751 NULL))
fc4007c9 3752 {
fc4007c9 3753 }
f8eba3c6 3754
14bc53a8
PA
3755 /* Callable as a symbol_found_callback_ftype callback. */
3756 bool operator () (symtab *sym);
f8eba3c6 3757
4717cec4
SM
3758 /* Return an rvalue reference to the collected symtabs. */
3759 std::vector<symtab *> &&release_symtabs ()
14bc53a8 3760 {
2a908241 3761 return std::move (m_symtabs);
14bc53a8
PA
3762 }
3763
3764private:
3765 /* The result vector of symtabs. */
4717cec4 3766 std::vector<symtab *> m_symtabs;
14bc53a8
PA
3767
3768 /* This is used to ensure the symtabs are unique. */
7a8a5d47 3769 htab_up m_symtab_table;
14bc53a8
PA
3770};
3771
3772bool
3773symtab_collector::operator () (struct symtab *symtab)
f8eba3c6 3774{
f8eba3c6
TT
3775 void **slot;
3776
7a8a5d47 3777 slot = htab_find_slot (m_symtab_table.get (), symtab, INSERT);
f8eba3c6 3778 if (!*slot)
4224873a 3779 {
f8eba3c6 3780 *slot = symtab;
4717cec4 3781 m_symtabs.push_back (symtab);
4224873a 3782 }
f8eba3c6 3783
14bc53a8 3784 return false;
4224873a
DC
3785}
3786
ffdbe864
YQ
3787} // namespace
3788
2a908241 3789/* Given a file name, return a list of all matching symtabs. If
c2f4122d
PA
3790 SEARCH_PSPACE is not NULL, the search is restricted to just that
3791 program space. */
f8eba3c6 3792
4717cec4 3793static std::vector<symtab *>
c2f4122d
PA
3794collect_symtabs_from_filename (const char *file,
3795 struct program_space *search_pspace)
f8eba3c6 3796{
14bc53a8 3797 symtab_collector collector;
f8eba3c6
TT
3798
3799 /* Find that file's data. */
c2f4122d
PA
3800 if (search_pspace == NULL)
3801 {
94c93c35 3802 for (struct program_space *pspace : program_spaces)
dda83cd7 3803 {
c2f4122d
PA
3804 if (pspace->executing_startup)
3805 continue;
f8eba3c6 3806
c2f4122d 3807 set_current_program_space (pspace);
14bc53a8 3808 iterate_over_symtabs (file, collector);
c2f4122d
PA
3809 }
3810 }
3811 else
3812 {
3813 set_current_program_space (search_pspace);
14bc53a8 3814 iterate_over_symtabs (file, collector);
c2f4122d 3815 }
f3c39e76 3816
14bc53a8 3817 return collector.release_symtabs ();
f8eba3c6
TT
3818}
3819
c2f4122d
PA
3820/* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3821 not NULL, the search is restricted to just that program space. */
f8eba3c6 3822
4717cec4 3823static std::vector<symtab *>
c2f4122d
PA
3824symtabs_from_filename (const char *filename,
3825 struct program_space *search_pspace)
40e084e1 3826{
4717cec4 3827 std::vector<symtab *> result
2a908241 3828 = collect_symtabs_from_filename (filename, search_pspace);
f8eba3c6 3829
4717cec4 3830 if (result.empty ())
f8eba3c6 3831 {
40e084e1
KS
3832 if (!have_full_symbols () && !have_partial_symbols ())
3833 throw_error (NOT_FOUND_ERROR,
3834 _("No symbol table is loaded. "
3835 "Use the \"file\" command."));
00e52e53 3836 source_file_not_found_error (filename);
f8eba3c6
TT
3837 }
3838
40e084e1 3839 return result;
84fba31b 3840}
f3c39e76 3841
fcaad03c
KS
3842/* See symtab.h. */
3843
3844void
3845symbol_searcher::find_all_symbols (const std::string &name,
3846 const struct language_defn *language,
3847 enum search_domain search_domain,
3848 std::vector<symtab *> *search_symtabs,
3849 struct program_space *search_pspace)
3850{
3851 symbol_searcher_collect_info info;
3852 struct linespec_state state;
3853
3854 memset (&state, 0, sizeof (state));
3855 state.language = language;
3856 info.state = &state;
3857
3858 info.result.symbols = &m_symbols;
3859 info.result.minimal_symbols = &m_minimal_symbols;
3860 std::vector<symtab *> all_symtabs;
3861 if (search_symtabs == nullptr)
3862 {
3863 all_symtabs.push_back (nullptr);
3864 search_symtabs = &all_symtabs;
3865 }
3866 info.file_symtabs = search_symtabs;
3867
3868 add_matching_symbols_to_info (name.c_str (), symbol_name_match_type::WILD,
3869 search_domain, &info, search_pspace);
3870}
3871
40e084e1
KS
3872/* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3873 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3874 returned in MINSYMS. */
14e91ac5 3875
40e084e1
KS
3876static void
3877find_function_symbols (struct linespec_state *state,
2a908241 3878 std::vector<symtab *> *file_symtabs, const char *name,
a20714ff 3879 symbol_name_match_type name_match_type,
7e41c8db 3880 std::vector<block_symbol> *symbols,
c2a031c5 3881 std::vector<bound_minimal_symbol> *minsyms)
14e91ac5 3882{
40e084e1 3883 struct collect_info info;
9b2f8581 3884 std::vector<const char *> symbol_names;
14e91ac5 3885
40e084e1 3886 info.state = state;
7243d011 3887 info.result.symbols = symbols;
c2a031c5 3888 info.result.minimal_symbols = minsyms;
40e084e1 3889 info.file_symtabs = file_symtabs;
e0881a8e 3890
40e084e1 3891 /* Try NAME as an Objective-C selector. */
d7561cbb 3892 find_imps (name, &symbol_names);
9b2f8581 3893 if (!symbol_names.empty ())
c2f4122d 3894 add_all_symbol_names_from_pspace (&info, state->search_pspace,
56d87ef7 3895 symbol_names, FUNCTIONS_DOMAIN);
40e084e1 3896 else
a20714ff 3897 add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
b5ec771e 3898 &info, state->search_pspace);
40e084e1
KS
3899}
3900
3901/* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3902 in SYMBOLS and minimal symbols in MINSYMS. */
14e91ac5 3903
b1ae631a 3904static void
40e084e1 3905find_linespec_symbols (struct linespec_state *state,
2a908241 3906 std::vector<symtab *> *file_symtabs,
b5ec771e 3907 const char *lookup_name,
a20714ff 3908 symbol_name_match_type name_match_type,
7e41c8db 3909 std::vector <block_symbol> *symbols,
c2a031c5 3910 std::vector<bound_minimal_symbol> *minsyms)
40e084e1 3911{
596dc4ad
TT
3912 gdb::unique_xmalloc_ptr<char> canon
3913 = cp_canonicalize_string_no_typedefs (lookup_name);
3914 if (canon != nullptr)
3915 lookup_name = canon.get ();
2f408ecb 3916
cc81e1c6
DE
3917 /* It's important to not call expand_symtabs_matching unnecessarily
3918 as it can really slow things down (by unnecessarily expanding
3919 potentially 1000s of symtabs, which when debugging some apps can
3920 cost 100s of seconds). Avoid this to some extent by *first* calling
3921 find_function_symbols, and only if that doesn't find anything
3922 *then* call find_method. This handles two important cases:
3923 1) break (anonymous namespace)::foo
3924 2) break class::method where method is in class (and not a baseclass) */
14e91ac5 3925
cc81e1c6 3926 find_function_symbols (state, file_symtabs, lookup_name,
7243d011 3927 name_match_type, symbols, minsyms);
14e91ac5 3928
cc81e1c6
DE
3929 /* If we were unable to locate a symbol of the same name, try dividing
3930 the name into class and method names and searching the class and its
3931 baseclasses. */
c2a031c5 3932 if (symbols->empty () && minsyms->empty ())
40e084e1 3933 {
2f408ecb 3934 std::string klass, method;
cc81e1c6 3935 const char *last, *p, *scope_op;
14e91ac5 3936
cc81e1c6
DE
3937 /* See if we can find a scope operator and break this symbol
3938 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3939 scope_op = "::";
3940 p = find_toplevel_string (lookup_name, scope_op);
14e91ac5 3941
cc81e1c6
DE
3942 last = NULL;
3943 while (p != NULL)
f8eba3c6 3944 {
cc81e1c6
DE
3945 last = p;
3946 p = find_toplevel_string (p + strlen (scope_op), scope_op);
f8eba3c6 3947 }
14e91ac5 3948
cc81e1c6
DE
3949 /* If no scope operator was found, there is nothing more we can do;
3950 we already attempted to lookup the entire name as a symbol
3951 and failed. */
3952 if (last == NULL)
2f408ecb 3953 return;
cc81e1c6
DE
3954
3955 /* LOOKUP_NAME points to the class name.
3956 LAST points to the method name. */
2f408ecb 3957 klass = std::string (lookup_name, last - lookup_name);
cc81e1c6
DE
3958
3959 /* Skip past the scope operator. */
3960 last += strlen (scope_op);
2f408ecb 3961 method = last;
cc81e1c6
DE
3962
3963 /* Find a list of classes named KLASS. */
7e41c8db 3964 std::vector<block_symbol> classes
4dedf84d
KS
3965 = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
3966 if (!classes.empty ())
cc81e1c6
DE
3967 {
3968 /* Now locate a list of suitable methods named METHOD. */
a70b8144 3969 try
cc81e1c6 3970 {
2f408ecb
PA
3971 find_method (state, file_symtabs,
3972 klass.c_str (), method.c_str (),
4dedf84d 3973 &classes, symbols, minsyms);
cc81e1c6
DE
3974 }
3975
3976 /* If successful, we're done. If NOT_FOUND_ERROR
3977 was not thrown, rethrow the exception that we did get. */
230d2906 3978 catch (const gdb_exception_error &except)
7556d4a4
PA
3979 {
3980 if (except.error != NOT_FOUND_ERROR)
eedc3f4f 3981 throw;
7556d4a4 3982 }
cc81e1c6 3983 }
2f408ecb 3984 }
14e91ac5
DC
3985}
3986
a2459270
PA
3987/* Helper for find_label_symbols. Find all labels that match name
3988 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3989 Return the actual function symbol in which the label was found in
3990 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3991 interpreted as a label name prefix. Otherwise, only a label named
3992 exactly NAME match. */
3993
3994static void
3995find_label_symbols_in_block (const struct block *block,
3996 const char *name, struct symbol *fn_sym,
3997 bool completion_mode,
7e41c8db
KS
3998 std::vector<block_symbol> *result,
3999 std::vector<block_symbol> *label_funcs_ret)
a2459270
PA
4000{
4001 if (completion_mode)
4002 {
4003 struct block_iterator iter;
4004 struct symbol *sym;
4005 size_t name_len = strlen (name);
4006
4007 int (*cmp) (const char *, const char *, size_t);
4008 cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
4009
4010 ALL_BLOCK_SYMBOLS (block, iter, sym)
4011 {
c1b5c1eb 4012 if (symbol_matches_domain (sym->language (),
a2459270 4013 SYMBOL_DOMAIN (sym), LABEL_DOMAIN)
987012b8 4014 && cmp (sym->search_name (), name, name_len) == 0)
a2459270 4015 {
7e41c8db
KS
4016 result->push_back ({sym, block});
4017 label_funcs_ret->push_back ({fn_sym, block});
a2459270
PA
4018 }
4019 }
4020 }
4021 else
4022 {
7e41c8db
KS
4023 struct block_symbol label_sym
4024 = lookup_symbol (name, block, LABEL_DOMAIN, 0);
a2459270 4025
7e41c8db 4026 if (label_sym.symbol != NULL)
a2459270 4027 {
7e41c8db
KS
4028 result->push_back (label_sym);
4029 label_funcs_ret->push_back ({fn_sym, block});
a2459270
PA
4030 }
4031 }
4032}
4033
3553eadc
KS
4034/* Return all labels that match name NAME in FUNCTION_SYMBOLS or NULL
4035 if no matches were found.
4036
4037 Return the actual function symbol in which the label was found in
a2459270
PA
4038 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4039 interpreted as a label name prefix. Otherwise, only labels named
4040 exactly NAME match. */
0f5238ed 4041
7e41c8db
KS
4042
4043static std::vector<block_symbol> *
40e084e1 4044find_label_symbols (struct linespec_state *self,
7e41c8db
KS
4045 std::vector<block_symbol> *function_symbols,
4046 std::vector<block_symbol> *label_funcs_ret,
4047 const char *name,
a2459270 4048 bool completion_mode)
0f5238ed 4049{
3977b71f 4050 const struct block *block;
40e084e1 4051 struct symbol *fn_sym;
7e41c8db 4052 std::vector<block_symbol> result;
9ef07c8c 4053
f8eba3c6 4054 if (function_symbols == NULL)
9ef07c8c 4055 {
f8eba3c6 4056 set_current_program_space (self->program_space);
4eeaa230 4057 block = get_current_search_block ();
f8eba3c6 4058
9ef07c8c
TT
4059 for (;
4060 block && !BLOCK_FUNCTION (block);
4061 block = BLOCK_SUPERBLOCK (block))
4062 ;
4063 if (!block)
40e084e1 4064 return NULL;
f8eba3c6
TT
4065 fn_sym = BLOCK_FUNCTION (block);
4066
a2459270
PA
4067 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4068 &result, label_funcs_ret);
40e084e1
KS
4069 }
4070 else
4071 {
7243d011 4072 for (const auto &elt : *function_symbols)
f8eba3c6 4073 {
7e41c8db
KS
4074 fn_sym = elt.symbol;
4075 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
4076 block = SYMBOL_BLOCK_VALUE (fn_sym);
40e084e1 4077
7e41c8db 4078 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
a2459270 4079 &result, label_funcs_ret);
f8eba3c6 4080 }
40e084e1 4081 }
f8eba3c6 4082
3553eadc 4083 if (!result.empty ())
7e41c8db 4084 return new std::vector<block_symbol> (std::move (result));
3553eadc 4085 return nullptr;
40e084e1 4086}
f8eba3c6 4087
40e084e1
KS
4088\f
4089
4090/* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4091
6c5b2ebe 4092static std::vector<symtab_and_line>
40e084e1 4093decode_digits_list_mode (struct linespec_state *self,
1487a14e 4094 linespec *ls,
40e084e1
KS
4095 struct symtab_and_line val)
4096{
40e084e1
KS
4097 gdb_assert (self->list_mode);
4098
6c5b2ebe
PA
4099 std::vector<symtab_and_line> values;
4100
2a908241 4101 for (const auto &elt : *ls->file_symtabs)
40e084e1
KS
4102 {
4103 /* The logic above should ensure this. */
4104 gdb_assert (elt != NULL);
4105
4106 set_current_program_space (SYMTAB_PSPACE (elt));
4107
4108 /* Simplistic search just for the list command. */
4109 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
4110 if (val.symtab == NULL)
4111 val.symtab = elt;
4112 val.pspace = SYMTAB_PSPACE (elt);
4113 val.pc = 0;
5b0e2db4 4114 val.explicit_line = true;
40e084e1 4115
6c5b2ebe 4116 add_sal_to_sals (self, &values, &val, NULL, 0);
f8eba3c6 4117 }
6c5b2ebe
PA
4118
4119 return values;
40e084e1 4120}
f8eba3c6 4121
791b7405
AB
4122/* A helper for create_sals_line_offset that iterates over the symtabs
4123 associated with LS and returns a vector of corresponding symtab_and_line
4124 structures. */
40e084e1 4125
6c5b2ebe 4126static std::vector<symtab_and_line>
40e084e1 4127decode_digits_ordinary (struct linespec_state *self,
1487a14e 4128 linespec *ls,
40e084e1 4129 int line,
40e084e1
KS
4130 struct linetable_entry **best_entry)
4131{
6c5b2ebe 4132 std::vector<symtab_and_line> sals;
2a908241 4133 for (const auto &elt : *ls->file_symtabs)
f8eba3c6 4134 {
67d89901 4135 std::vector<CORE_ADDR> pcs;
40e084e1
KS
4136
4137 /* The logic above should ensure this. */
4138 gdb_assert (elt != NULL);
f8eba3c6 4139
40e084e1 4140 set_current_program_space (SYMTAB_PSPACE (elt));
f8eba3c6 4141
40e084e1 4142 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
67d89901 4143 for (CORE_ADDR pc : pcs)
f8eba3c6 4144 {
51abb421 4145 symtab_and_line sal;
40e084e1
KS
4146 sal.pspace = SYMTAB_PSPACE (elt);
4147 sal.symtab = elt;
4148 sal.line = line;
5b0e2db4 4149 sal.explicit_line = true;
40e084e1 4150 sal.pc = pc;
6c5b2ebe 4151 sals.push_back (std::move (sal));
f8eba3c6
TT
4152 }
4153 }
6c5b2ebe
PA
4154
4155 return sals;
40e084e1
KS
4156}
4157
4158\f
4159
4160/* Return the line offset represented by VARIABLE. */
4161
4162static struct line_offset
4163linespec_parse_variable (struct linespec_state *self, const char *variable)
4164{
4165 int index = 0;
4166 const char *p;
4167 struct line_offset offset = {0, LINE_OFFSET_NONE};
f8eba3c6 4168
40e084e1
KS
4169 p = (variable[1] == '$') ? variable + 2 : variable + 1;
4170 if (*p == '$')
4171 ++p;
4172 while (*p >= '0' && *p <= '9')
4173 ++p;
4174 if (!*p) /* Reached end of token without hitting non-digit. */
f8eba3c6 4175 {
40e084e1
KS
4176 /* We have a value history reference. */
4177 struct value *val_history;
f8eba3c6 4178
40e084e1
KS
4179 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
4180 val_history
4181 = access_value_history ((variable[1] == '$') ? -index : index);
78134374 4182 if (value_type (val_history)->code () != TYPE_CODE_INT)
40e084e1
KS
4183 error (_("History values used in line "
4184 "specs must have integer values."));
4185 offset.offset = value_as_long (val_history);
4186 }
4187 else
4188 {
4189 /* Not all digits -- may be user variable/function or a
4190 convenience variable. */
4191 LONGEST valx;
4192 struct internalvar *ivar;
4193
4194 /* Try it as a convenience variable. If it is not a convenience
4195 variable, return and allow normal symbol lookup to occur. */
4196 ivar = lookup_only_internalvar (variable + 1);
4197 if (ivar == NULL)
4198 /* No internal variable with that name. Mark the offset
4199 as unknown to allow the name to be looked up as a symbol. */
4200 offset.sign = LINE_OFFSET_UNKNOWN;
4201 else
4202 {
4203 /* We found a valid variable name. If it is not an integer,
4204 throw an error. */
4205 if (!get_internalvar_integer (ivar, &valx))
4206 error (_("Convenience variables used in line "
4207 "specs must have integer values."));
4208 else
4209 offset.offset = valx;
4210 }
f8eba3c6
TT
4211 }
4212
40e084e1 4213 return offset;
f8eba3c6 4214}
40e084e1 4215\f
f8eba3c6 4216
40e084e1 4217/* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
6e22494e
JK
4218 linespec; return the SAL in RESULT. This function should return SALs
4219 matching those from find_function_start_sal, otherwise false
4220 multiple-locations breakpoints could be placed. */
f8eba3c6
TT
4221
4222static void
4223minsym_found (struct linespec_state *self, struct objfile *objfile,
4224 struct minimal_symbol *msymbol,
6c5b2ebe 4225 std::vector<symtab_and_line> *result)
f8eba3c6 4226{
3467ec66 4227 bool want_start_sal;
f8eba3c6 4228
4024cf2b 4229 CORE_ADDR func_addr;
3467ec66
PA
4230 bool is_function = msymbol_is_function (objfile, msymbol, &func_addr);
4231
4232 if (is_function)
4233 {
c9d95fa3 4234 const char *msym_name = msymbol->linkage_name ();
3467ec66 4235
f50776aa
PA
4236 if (MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
4237 || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
3467ec66
PA
4238 want_start_sal = gnu_ifunc_resolve_name (msym_name, &func_addr);
4239 else
4240 want_start_sal = true;
4241 }
4242
4243 symtab_and_line sal;
4244
4245 if (is_function && want_start_sal)
42ddae10 4246 sal = find_function_start_sal (func_addr, NULL, self->funfirstline);
e5f25bc5
PA
4247 else
4248 {
4249 sal.objfile = objfile;
3467ec66
PA
4250 sal.msymbol = msymbol;
4251 /* Store func_addr, not the minsym's address in case this was an
4252 ifunc that hasn't been resolved yet. */
4253 if (is_function)
4254 sal.pc = func_addr;
4255 else
4256 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
e5f25bc5 4257 sal.pspace = current_program_space;
6e22494e 4258 }
f8eba3c6 4259
ebbc3a7d 4260 sal.section = msymbol->obj_section (objfile);
6b05c8bd 4261
07fea4b4 4262 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
c9d95fa3 4263 add_sal_to_sals (self, result, &sal, msymbol->natural_name (), 0);
f8eba3c6
TT
4264}
4265
41c1efc6
TT
4266/* Helper for search_minsyms_for_name that adds the symbol to the
4267 result. */
39b856a4
TT
4268
4269static void
41c1efc6
TT
4270add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
4271 struct symtab *symtab, int list_mode,
4272 std::vector<struct bound_minimal_symbol> *msyms)
39b856a4 4273{
41c1efc6 4274 if (symtab != NULL)
87186c6a 4275 {
4024cf2b
PA
4276 /* We're looking for a label for which we don't have debug
4277 info. */
4278 CORE_ADDR func_addr;
41c1efc6 4279 if (msymbol_is_function (objfile, minsym, &func_addr))
4024cf2b
PA
4280 {
4281 symtab_and_line sal = find_pc_sect_line (func_addr, NULL, 0);
87186c6a 4282
41c1efc6 4283 if (symtab != sal.symtab)
4024cf2b
PA
4284 return;
4285 }
87186c6a
MMN
4286 }
4287
4024cf2b 4288 /* Exclude data symbols when looking for breakpoint locations. */
41c1efc6 4289 if (!list_mode && !msymbol_is_function (objfile, minsym))
4024cf2b 4290 return;
095bcf5e 4291
41c1efc6
TT
4292 struct bound_minimal_symbol mo = {minsym, objfile};
4293 msyms->push_back (mo);
ca31ab1d 4294 return;
f8eba3c6
TT
4295}
4296
87186c6a 4297/* Search for minimal symbols called NAME. If SEARCH_PSPACE
f8eba3c6 4298 is not NULL, the search is restricted to just that program
87186c6a
MMN
4299 space.
4300
4301 If SYMTAB is NULL, search all objfiles, otherwise
4302 restrict results to the given SYMTAB. */
f8eba3c6
TT
4303
4304static void
b5ec771e
PA
4305search_minsyms_for_name (struct collect_info *info,
4306 const lookup_name_info &name,
87186c6a
MMN
4307 struct program_space *search_pspace,
4308 struct symtab *symtab)
f8eba3c6 4309{
41c1efc6 4310 std::vector<struct bound_minimal_symbol> minsyms;
f8eba3c6 4311
87186c6a
MMN
4312 if (symtab == NULL)
4313 {
94c93c35
TT
4314 for (struct program_space *pspace : program_spaces)
4315 {
4316 if (search_pspace != NULL && search_pspace != pspace)
4317 continue;
4318 if (pspace->executing_startup)
4319 continue;
39b856a4 4320
94c93c35 4321 set_current_program_space (pspace);
87186c6a 4322
94c93c35
TT
4323 for (objfile *objfile : current_program_space->objfiles ())
4324 {
4325 iterate_over_minimal_symbols (objfile, name,
4326 [&] (struct minimal_symbol *msym)
4327 {
4328 add_minsym (msym, objfile, nullptr,
4329 info->state->list_mode,
4330 &minsyms);
4331 return false;
4332 });
4333 }
4334 }
87186c6a
MMN
4335 }
4336 else
f8eba3c6 4337 {
87186c6a
MMN
4338 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
4339 {
4340 set_current_program_space (SYMTAB_PSPACE (symtab));
41c1efc6
TT
4341 iterate_over_minimal_symbols
4342 (SYMTAB_OBJFILE (symtab), name,
4343 [&] (struct minimal_symbol *msym)
4344 {
4345 add_minsym (msym, SYMTAB_OBJFILE (symtab), symtab,
4346 info->state->list_mode, &minsyms);
ca31ab1d 4347 return false;
41c1efc6 4348 });
87186c6a 4349 }
9ef07c8c 4350 }
39b856a4 4351
77f2120b
PA
4352 /* Return true if TYPE is a static symbol. */
4353 auto msymbol_type_is_static = [] (enum minimal_symbol_type type)
41c1efc6 4354 {
77f2120b
PA
4355 switch (type)
4356 {
4357 case mst_file_text:
4358 case mst_file_data:
4359 case mst_file_bss:
4360 return true;
4361 default:
4362 return false;
4363 }
4364 };
39b856a4 4365
77f2120b
PA
4366 /* Add minsyms to the result set, but filter out trampoline symbols
4367 if we also found extern symbols with the same name. I.e., don't
4368 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4369 'foo' is the symbol that the plt resolves to. */
4370 for (const bound_minimal_symbol &item : minsyms)
4371 {
4372 bool skip = false;
4373 if (MSYMBOL_TYPE (item.minsym) == mst_solib_trampoline)
4374 {
4375 for (const bound_minimal_symbol &item2 : minsyms)
4376 {
4377 if (&item2 == &item)
4378 continue;
39b856a4 4379
77f2120b
PA
4380 /* Trampoline symbols can only jump to exported
4381 symbols. */
4382 if (msymbol_type_is_static (MSYMBOL_TYPE (item2.minsym)))
4383 continue;
41c1efc6 4384
77f2120b
PA
4385 if (strcmp (item.minsym->linkage_name (),
4386 item2.minsym->linkage_name ()) != 0)
4387 continue;
41c1efc6 4388
77f2120b
PA
4389 /* Found a global minsym with the same name as the
4390 trampoline. Don't create a location for this
4391 trampoline. */
4392 skip = true;
4393 break;
4394 }
41c1efc6 4395 }
77f2120b
PA
4396
4397 if (!skip)
4398 info->result.minimal_symbols->push_back (item);
41c1efc6 4399 }
f8eba3c6
TT
4400}
4401
4402/* A helper function to add all symbols matching NAME to INFO. If
4403 PSPACE is not NULL, the search is restricted to just that program
4404 space. */
0f5238ed 4405
f8eba3c6
TT
4406static void
4407add_matching_symbols_to_info (const char *name,
b5ec771e 4408 symbol_name_match_type name_match_type,
56d87ef7 4409 enum search_domain search_domain,
f8eba3c6
TT
4410 struct collect_info *info,
4411 struct program_space *pspace)
4412{
b5ec771e
PA
4413 lookup_name_info lookup_name (name, name_match_type);
4414
2a908241 4415 for (const auto &elt : *info->file_symtabs)
f8eba3c6 4416 {
2a908241 4417 if (elt == nullptr)
f8eba3c6 4418 {
b5ec771e 4419 iterate_over_all_matching_symtabs (info->state, lookup_name,
56d87ef7 4420 VAR_DOMAIN, search_domain,
7e41c8db
KS
4421 pspace, true,
4422 [&] (block_symbol *bsym)
4423 { return info->add_symbol (bsym); });
b5ec771e 4424 search_minsyms_for_name (info, lookup_name, pspace, NULL);
f8eba3c6
TT
4425 }
4426 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
4427 {
7243d011 4428 int prev_len = info->result.symbols->size ();
87186c6a 4429
f8eba3c6
TT
4430 /* Program spaces that are executing startup should have
4431 been filtered out earlier. */
4432 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
4433 set_current_program_space (SYMTAB_PSPACE (elt));
b5ec771e 4434 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN,
7e41c8db
KS
4435 [&] (block_symbol *bsym)
4436 { return info->add_symbol (bsym); });
87186c6a
MMN
4437
4438 /* If no new symbols were found in this iteration and this symtab
4439 is in assembler, we might actually be looking for a label for
4440 which we don't have debug info. Check for a minimal symbol in
4441 this case. */
7243d011 4442 if (prev_len == info->result.symbols->size ()
87186c6a 4443 && elt->language == language_asm)
b5ec771e 4444 search_minsyms_for_name (info, lookup_name, pspace, elt);
f8eba3c6
TT
4445 }
4446 }
0f5238ed
TT
4447}
4448
14e91ac5
DC
4449\f
4450
413dad4d
DC
4451/* Now come some functions that are called from multiple places within
4452 decode_line_1. */
4453
f8eba3c6
TT
4454static int
4455symbol_to_sal (struct symtab_and_line *result,
4456 int funfirstline, struct symbol *sym)
413dad4d 4457{
413dad4d 4458 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 4459 {
f8eba3c6
TT
4460 *result = find_function_start_sal (sym, funfirstline);
4461 return 1;
50641945 4462 }
413dad4d
DC
4463 else
4464 {
62853458 4465 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
413dad4d 4466 {
51abb421 4467 *result = {};
08be3fe3 4468 result->symtab = symbol_symtab (sym);
06871ae8 4469 result->symbol = sym;
f8eba3c6
TT
4470 result->line = SYMBOL_LINE (sym);
4471 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 4472 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6
TT
4473 result->explicit_pc = 1;
4474 return 1;
413dad4d 4475 }
62853458 4476 else if (funfirstline)
dcf9f4ab 4477 {
f8eba3c6 4478 /* Nothing. */
dcf9f4ab 4479 }
62853458
TT
4480 else if (SYMBOL_LINE (sym) != 0)
4481 {
4482 /* We know its line number. */
51abb421 4483 *result = {};
08be3fe3 4484 result->symtab = symbol_symtab (sym);
06871ae8 4485 result->symbol = sym;
f8eba3c6 4486 result->line = SYMBOL_LINE (sym);
e5f25bc5 4487 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 4488 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6 4489 return 1;
62853458 4490 }
413dad4d 4491 }
f8eba3c6
TT
4492
4493 return 0;
413dad4d 4494}
50641945 4495
16e802b9 4496linespec_result::~linespec_result ()
f8eba3c6 4497{
6c5b2ebe
PA
4498 for (linespec_sals &lsal : lsals)
4499 xfree (lsal.canonical);
7efd8fc2 4500}
87f0e720
KS
4501
4502/* Return the quote characters permitted by the linespec parser. */
4503
4504const char *
4505get_gdb_linespec_parser_quote_characters (void)
4506{
4507 return linespec_quote_characters;
4508}
This page took 2.314449 seconds and 4 git commands to generate.