]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/location.c
Handle custom completion match prefix / LCD
[thirdparty/binutils-gdb.git] / gdb / location.c
CommitLineData
c7c1b3e9 1/* Data structures and API for event locations in GDB.
61baf725 2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
c7c1b3e9
KS
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "defs.h"
20#include "gdb_assert.h"
21#include "location.h"
22#include "symtab.h"
23#include "language.h"
24#include "linespec.h"
25#include "cli/cli-utils.h"
26#include "probe.h"
8090b426 27#include "cp-support.h"
c7c1b3e9
KS
28
29#include <ctype.h>
30#include <string.h>
31
32/* An event location used to set a stop event in the inferior.
33 This structure is an amalgam of the various ways
34 to specify where a stop event should be set. */
35
36struct event_location
37{
38 /* The type of this breakpoint specification. */
39 enum event_location_type type;
ebdad8fc 40#define EL_TYPE(P) (P)->type
c7c1b3e9
KS
41
42 union
43 {
44 /* A generic "this is a string specification" for a location.
45 This representation is used by both "normal" linespecs and
46 probes. */
47 char *addr_string;
ebdad8fc
KS
48#define EL_LINESPEC(P) ((P)->u.addr_string)
49#define EL_PROBE(P) ((P)->u.addr_string)
a06efdd6
KS
50
51 /* An address in the inferior. */
52 CORE_ADDR address;
ebdad8fc 53#define EL_ADDRESS(P) (P)->u.address
00e52e53
KS
54
55 /* An explicit location. */
67994074 56 struct explicit_location explicit_loc;
ebdad8fc 57#define EL_EXPLICIT(P) (&((P)->u.explicit_loc))
c7c1b3e9
KS
58 } u;
59
60 /* Cached string representation of this location. This is used, e.g., to
61 save stop event locations to file. Malloc'd. */
62 char *as_string;
ebdad8fc 63#define EL_STRING(P) ((P)->as_string)
c7c1b3e9
KS
64};
65
66/* See description in location.h. */
67
68enum event_location_type
69event_location_type (const struct event_location *location)
70{
71 return EL_TYPE (location);
72}
73
74/* See description in location.h. */
75
00e52e53 76void
67994074 77initialize_explicit_location (struct explicit_location *explicit_loc)
00e52e53 78{
67994074
KS
79 memset (explicit_loc, 0, sizeof (struct explicit_location));
80 explicit_loc->line_offset.sign = LINE_OFFSET_UNKNOWN;
00e52e53
KS
81}
82
83/* See description in location.h. */
84
ffc2605c 85event_location_up
f2fc3015 86new_linespec_location (const char **linespec)
c7c1b3e9
KS
87{
88 struct event_location *location;
89
90 location = XCNEW (struct event_location);
91 EL_TYPE (location) = LINESPEC_LOCATION;
92 if (*linespec != NULL)
93 {
f2fc3015
TT
94 const char *p;
95 const char *orig = *linespec;
c7c1b3e9
KS
96
97 linespec_lex_to_end (linespec);
98 p = remove_trailing_whitespace (orig, *linespec);
99 if ((p - orig) > 0)
100 EL_LINESPEC (location) = savestring (orig, p - orig);
101 }
ffc2605c 102 return event_location_up (location);
c7c1b3e9
KS
103}
104
105/* See description in location.h. */
106
107const char *
108get_linespec_location (const struct event_location *location)
109{
110 gdb_assert (EL_TYPE (location) == LINESPEC_LOCATION);
111 return EL_LINESPEC (location);
112}
113
114/* See description in location.h. */
115
ffc2605c 116event_location_up
305e13e6
JB
117new_address_location (CORE_ADDR addr, const char *addr_string,
118 int addr_string_len)
a06efdd6
KS
119{
120 struct event_location *location;
121
122 location = XCNEW (struct event_location);
123 EL_TYPE (location) = ADDRESS_LOCATION;
124 EL_ADDRESS (location) = addr;
305e13e6
JB
125 if (addr_string != NULL)
126 EL_STRING (location) = xstrndup (addr_string, addr_string_len);
ffc2605c 127 return event_location_up (location);
a06efdd6
KS
128}
129
130/* See description in location.h. */
131
132CORE_ADDR
133get_address_location (const struct event_location *location)
134{
135 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
136 return EL_ADDRESS (location);
137}
138
139/* See description in location.h. */
140
305e13e6
JB
141const char *
142get_address_string_location (const struct event_location *location)
143{
144 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
145 return EL_STRING (location);
146}
147
148/* See description in location.h. */
149
ffc2605c 150event_location_up
5b56227b
KS
151new_probe_location (const char *probe)
152{
153 struct event_location *location;
154
155 location = XCNEW (struct event_location);
156 EL_TYPE (location) = PROBE_LOCATION;
157 if (probe != NULL)
158 EL_PROBE (location) = xstrdup (probe);
ffc2605c 159 return event_location_up (location);
5b56227b
KS
160}
161
162/* See description in location.h. */
163
164const char *
165get_probe_location (const struct event_location *location)
166{
167 gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
168 return EL_PROBE (location);
169}
170
171/* See description in location.h. */
172
ffc2605c 173event_location_up
67994074 174new_explicit_location (const struct explicit_location *explicit_loc)
00e52e53
KS
175{
176 struct event_location tmp;
177
178 memset (&tmp, 0, sizeof (struct event_location));
179 EL_TYPE (&tmp) = EXPLICIT_LOCATION;
180 initialize_explicit_location (EL_EXPLICIT (&tmp));
67994074 181 if (explicit_loc != NULL)
00e52e53 182 {
67994074 183 if (explicit_loc->source_filename != NULL)
00e52e53
KS
184 {
185 EL_EXPLICIT (&tmp)->source_filename
67994074 186 = explicit_loc->source_filename;
00e52e53
KS
187 }
188
67994074 189 if (explicit_loc->function_name != NULL)
00e52e53 190 EL_EXPLICIT (&tmp)->function_name
67994074 191 = explicit_loc->function_name;
00e52e53 192
67994074
KS
193 if (explicit_loc->label_name != NULL)
194 EL_EXPLICIT (&tmp)->label_name = explicit_loc->label_name;
00e52e53 195
67994074
KS
196 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
197 EL_EXPLICIT (&tmp)->line_offset = explicit_loc->line_offset;
00e52e53
KS
198 }
199
200 return copy_event_location (&tmp);
201}
202
203/* See description in location.h. */
204
205struct explicit_location *
206get_explicit_location (struct event_location *location)
207{
208 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
209 return EL_EXPLICIT (location);
210}
211
212/* See description in location.h. */
213
214const struct explicit_location *
215get_explicit_location_const (const struct event_location *location)
216{
217 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
218 return EL_EXPLICIT (location);
219}
220
221/* This convenience function returns a malloc'd string which
67994074 222 represents the location in EXPLICIT_LOC.
00e52e53
KS
223
224 AS_LINESPEC is non-zero if this string should be a linespec.
225 Otherwise it will be output in explicit form. */
226
227static char *
228explicit_to_string_internal (int as_linespec,
67994074 229 const struct explicit_location *explicit_loc)
00e52e53 230{
00e52e53 231 int need_space = 0;
d7e74731
PA
232 char space = as_linespec ? ':' : ' ';
233 string_file buf;
00e52e53 234
67994074 235 if (explicit_loc->source_filename != NULL)
00e52e53
KS
236 {
237 if (!as_linespec)
d7e74731
PA
238 buf.puts ("-source ");
239 buf.puts (explicit_loc->source_filename);
00e52e53
KS
240 need_space = 1;
241 }
242
67994074 243 if (explicit_loc->function_name != NULL)
00e52e53
KS
244 {
245 if (need_space)
d7e74731 246 buf.putc (space);
00e52e53 247 if (!as_linespec)
d7e74731
PA
248 buf.puts ("-function ");
249 buf.puts (explicit_loc->function_name);
00e52e53
KS
250 need_space = 1;
251 }
252
67994074 253 if (explicit_loc->label_name != NULL)
00e52e53
KS
254 {
255 if (need_space)
d7e74731 256 buf.putc (space);
00e52e53 257 if (!as_linespec)
d7e74731
PA
258 buf.puts ("-label ");
259 buf.puts (explicit_loc->label_name);
00e52e53
KS
260 need_space = 1;
261 }
262
67994074 263 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
00e52e53
KS
264 {
265 if (need_space)
d7e74731 266 buf.putc (space);
00e52e53 267 if (!as_linespec)
d7e74731
PA
268 buf.puts ("-line ");
269 buf.printf ("%s%d",
270 (explicit_loc->line_offset.sign == LINE_OFFSET_NONE ? ""
271 : (explicit_loc->line_offset.sign
272 == LINE_OFFSET_PLUS ? "+" : "-")),
273 explicit_loc->line_offset.offset);
00e52e53
KS
274 }
275
d7e74731 276 return xstrdup (buf.c_str ());
00e52e53
KS
277}
278
279/* See description in location.h. */
280
281char *
67994074 282explicit_location_to_string (const struct explicit_location *explicit_loc)
00e52e53 283{
67994074 284 return explicit_to_string_internal (0, explicit_loc);
00e52e53
KS
285}
286
287/* See description in location.h. */
288
289char *
67994074 290explicit_location_to_linespec (const struct explicit_location *explicit_loc)
00e52e53 291{
67994074 292 return explicit_to_string_internal (1, explicit_loc);
00e52e53
KS
293}
294
295/* See description in location.h. */
296
ffc2605c 297event_location_up
c7c1b3e9
KS
298copy_event_location (const struct event_location *src)
299{
300 struct event_location *dst;
301
302 dst = XCNEW (struct event_location);
303 EL_TYPE (dst) = EL_TYPE (src);
304 if (EL_STRING (src) != NULL)
305 EL_STRING (dst) = xstrdup (EL_STRING (src));
306
307 switch (EL_TYPE (src))
308 {
309 case LINESPEC_LOCATION:
310 if (EL_LINESPEC (src) != NULL)
311 EL_LINESPEC (dst) = xstrdup (EL_LINESPEC (src));
312 break;
313
a06efdd6
KS
314 case ADDRESS_LOCATION:
315 EL_ADDRESS (dst) = EL_ADDRESS (src);
316 break;
317
00e52e53
KS
318 case EXPLICIT_LOCATION:
319 if (EL_EXPLICIT (src)->source_filename != NULL)
320 EL_EXPLICIT (dst)->source_filename
321 = xstrdup (EL_EXPLICIT (src)->source_filename);
322
323 if (EL_EXPLICIT (src)->function_name != NULL)
324 EL_EXPLICIT (dst)->function_name
325 = xstrdup (EL_EXPLICIT (src)->function_name);
326
327 if (EL_EXPLICIT (src)->label_name != NULL)
328 EL_EXPLICIT (dst)->label_name = xstrdup (EL_EXPLICIT (src)->label_name);
329
330 EL_EXPLICIT (dst)->line_offset = EL_EXPLICIT (src)->line_offset;
331 break;
332
333
5b56227b
KS
334 case PROBE_LOCATION:
335 if (EL_PROBE (src) != NULL)
336 EL_PROBE (dst) = xstrdup (EL_PROBE (src));
337 break;
338
c7c1b3e9
KS
339 default:
340 gdb_assert_not_reached ("unknown event location type");
341 }
342
ffc2605c 343 return event_location_up (dst);
c7c1b3e9
KS
344}
345
c7c1b3e9 346void
8e9e35b1 347event_location_deleter::operator() (event_location *location) const
c7c1b3e9
KS
348{
349 if (location != NULL)
350 {
351 xfree (EL_STRING (location));
352
353 switch (EL_TYPE (location))
354 {
355 case LINESPEC_LOCATION:
356 xfree (EL_LINESPEC (location));
357 break;
358
a06efdd6
KS
359 case ADDRESS_LOCATION:
360 /* Nothing to do. */
361 break;
362
00e52e53
KS
363 case EXPLICIT_LOCATION:
364 xfree (EL_EXPLICIT (location)->source_filename);
365 xfree (EL_EXPLICIT (location)->function_name);
366 xfree (EL_EXPLICIT (location)->label_name);
367 break;
368
5b56227b
KS
369 case PROBE_LOCATION:
370 xfree (EL_PROBE (location));
371 break;
372
c7c1b3e9
KS
373 default:
374 gdb_assert_not_reached ("unknown event location type");
375 }
376
377 xfree (location);
378 }
379}
380
381/* See description in location.h. */
382
383const char *
384event_location_to_string (struct event_location *location)
385{
386 if (EL_STRING (location) == NULL)
387 {
388 switch (EL_TYPE (location))
389 {
390 case LINESPEC_LOCATION:
391 if (EL_LINESPEC (location) != NULL)
392 EL_STRING (location) = xstrdup (EL_LINESPEC (location));
393 break;
394
a06efdd6
KS
395 case ADDRESS_LOCATION:
396 EL_STRING (location)
397 = xstrprintf ("*%s",
398 core_addr_to_string (EL_ADDRESS (location)));
399 break;
400
00e52e53
KS
401 case EXPLICIT_LOCATION:
402 EL_STRING (location)
403 = explicit_location_to_string (EL_EXPLICIT (location));
404 break;
405
5b56227b
KS
406 case PROBE_LOCATION:
407 EL_STRING (location) = xstrdup (EL_PROBE (location));
408 break;
409
c7c1b3e9
KS
410 default:
411 gdb_assert_not_reached ("unknown event location type");
412 }
413 }
414
415 return EL_STRING (location);
416}
417
c6756f62
PA
418/* Find an instance of the quote character C in the string S that is
419 outside of all single- and double-quoted strings (i.e., any quoting
420 other than C). */
421
422static const char *
423find_end_quote (const char *s, char end_quote_char)
424{
425 /* zero if we're not in quotes;
426 '"' if we're in a double-quoted string;
427 '\'' if we're in a single-quoted string. */
428 char nested_quote_char = '\0';
429
430 for (const char *scan = s; *scan != '\0'; scan++)
431 {
432 if (nested_quote_char != '\0')
433 {
434 if (*scan == nested_quote_char)
435 nested_quote_char = '\0';
436 else if (scan[0] == '\\' && *(scan + 1) != '\0')
437 scan++;
438 }
439 else if (*scan == end_quote_char && nested_quote_char == '\0')
440 return scan;
441 else if (*scan == '"' || *scan == '\'')
442 nested_quote_char = *scan;
443 }
444
445 return 0;
446}
447
87f0e720
KS
448/* A lexer for explicit locations. This function will advance INP
449 past any strings that it lexes. Returns a malloc'd copy of the
450 lexed string or NULL if no lexing was done. */
451
4b217cc7 452static gdb::unique_xmalloc_ptr<char>
87f0e720 453explicit_location_lex_one (const char **inp,
c6756f62
PA
454 const struct language_defn *language,
455 explicit_completion_info *completion_info)
87f0e720
KS
456{
457 const char *start = *inp;
458
459 if (*start == '\0')
460 return NULL;
461
462 /* If quoted, skip to the ending quote. */
463 if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
464 {
c6756f62
PA
465 if (completion_info != NULL)
466 completion_info->quoted_arg_start = start;
87f0e720 467
c6756f62 468 const char *end = find_end_quote (start + 1, *start);
87f0e720 469
c6756f62
PA
470 if (end == NULL)
471 {
472 if (completion_info == NULL)
87f0e720 473 error (_("Unmatched quote, %s."), start);
c6756f62
PA
474
475 end = start + strlen (start);
476 *inp = end;
4b217cc7 477 return gdb::unique_xmalloc_ptr<char> (savestring (start + 1,
c6756f62 478 *inp - start - 1));
87f0e720 479 }
c6756f62
PA
480
481 if (completion_info != NULL)
482 completion_info->quoted_arg_end = end;
483 *inp = end + 1;
484 return gdb::unique_xmalloc_ptr<char> (savestring (start + 1,
485 *inp - start - 2));
87f0e720
KS
486 }
487
488 /* If the input starts with '-' or '+', the string ends with the next
489 whitespace or comma. */
490 if (*start == '-' || *start == '+')
491 {
492 while (*inp[0] != '\0' && *inp[0] != ',' && !isspace (*inp[0]))
493 ++(*inp);
494 }
495 else
496 {
497 /* Handle numbers first, stopping at the next whitespace or ','. */
498 while (isdigit (*inp[0]))
499 ++(*inp);
500 if (*inp[0] == '\0' || isspace (*inp[0]) || *inp[0] == ',')
4b217cc7
TT
501 return gdb::unique_xmalloc_ptr<char> (savestring (start,
502 *inp - start));
87f0e720
KS
503
504 /* Otherwise stop at the next occurrence of whitespace, '\0',
505 keyword, or ','. */
506 *inp = start;
507 while ((*inp)[0]
508 && (*inp)[0] != ','
509 && !(isspace ((*inp)[0])
510 || linespec_lexer_lex_keyword (&(*inp)[1])))
511 {
512 /* Special case: C++ operator,. */
513 if (language->la_language == language_cplus
8090b426
PA
514 && startswith (*inp, CP_OPERATOR_STR))
515 (*inp) += CP_OPERATOR_LEN;
87f0e720
KS
516 ++(*inp);
517 }
518 }
519
520 if (*inp - start > 0)
4b217cc7 521 return gdb::unique_xmalloc_ptr<char> (savestring (start, *inp - start));
87f0e720
KS
522
523 return NULL;
524}
525
c6756f62
PA
526/* Return true if COMMA points past "operator". START is the start of
527 the line that COMMAND points to, hence when reading backwards, we
528 must not read any character before START. */
529
530static bool
531is_cp_operator (const char *start, const char *comma)
532{
533 if (comma != NULL
534 && (comma - start) >= CP_OPERATOR_LEN)
535 {
536 const char *p = comma;
537
538 while (p > start && isspace (p[-1]))
539 p--;
540 if (p - start >= CP_OPERATOR_LEN)
541 {
542 p -= CP_OPERATOR_LEN;
543 if (strncmp (p, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
544 && (p == start
545 || !(isalnum (p[-1]) || p[-1] == '_')))
546 {
547 return true;
548 }
549 }
550 }
551 return false;
552}
553
554/* When scanning the input string looking for the next explicit
555 location option/delimiter, we jump to the next option by looking
556 for ",", and "-". Such a character can also appear in C++ symbols
557 like "operator," and "operator-". So when we find such a
558 character, we call this function to check if we found such a
559 symbol, meaning we had a false positive for an option string. In
560 that case, we keep looking for the next delimiter, until we find
561 one that is not a false positive, or we reach end of string. FOUND
562 is the character that scanning found (either '-' or ','), and START
563 is the start of the line that FOUND points to, hence when reading
564 backwards, we must not read any character before START. Returns a
565 pointer to the next non-false-positive delimiter character, or NULL
566 if none was found. */
567
568static const char *
569skip_op_false_positives (const char *start, const char *found)
570{
571 while (found != NULL && is_cp_operator (start, found))
572 {
573 if (found[0] == '-' && found[1] == '-')
574 start = found + 2;
575 else
576 start = found + 1;
577 found = find_toplevel_char (start, *found);
578 }
579
580 return found;
581}
582
583/* Assuming both FIRST and NEW_TOK point into the same string, return
584 the pointer that is closer to the start of the string. If FIRST is
585 NULL, returns NEW_TOK. If NEW_TOK is NULL, returns FIRST. */
586
587static const char *
588first_of (const char *first, const char *new_tok)
589{
590 if (first == NULL)
591 return new_tok;
592 else if (new_tok != NULL && new_tok < first)
593 return new_tok;
594 else
595 return first;
596}
597
598/* A lexer for functions in explicit locations. This function will
599 advance INP past a function until the next option, or until end of
600 string. Returns a malloc'd copy of the lexed string or NULL if no
601 lexing was done. */
602
603static gdb::unique_xmalloc_ptr<char>
604explicit_location_lex_one_function (const char **inp,
605 const struct language_defn *language,
606 explicit_completion_info *completion_info)
607{
608 const char *start = *inp;
609
610 if (*start == '\0')
611 return NULL;
612
613 /* If quoted, skip to the ending quote. */
614 if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
615 {
616 char quote_char = *start;
617
618 /* If the input is not an Ada operator, skip to the matching
619 closing quote and return the string. */
620 if (!(language->la_language == language_ada
621 && quote_char == '\"' && is_ada_operator (start)))
622 {
623 if (completion_info != NULL)
624 completion_info->quoted_arg_start = start;
625
626 const char *end = find_toplevel_char (start + 1, quote_char);
627
628 if (end == NULL)
629 {
630 if (completion_info == NULL)
631 error (_("Unmatched quote, %s."), start);
632
633 end = start + strlen (start);
634 *inp = end;
635 char *saved = savestring (start + 1, *inp - start - 1);
636 return gdb::unique_xmalloc_ptr<char> (saved);
637 }
638
639 if (completion_info != NULL)
640 completion_info->quoted_arg_end = end;
641 *inp = end + 1;
642 char *saved = savestring (start + 1, *inp - start - 2);
643 return gdb::unique_xmalloc_ptr<char> (saved);
644 }
645 }
646
647 const char *comma = find_toplevel_char (start, ',');
648
649 /* If we have "-function -myfunction", or perhaps better example,
650 "-function -[BasicClass doIt]" (objc selector), treat
651 "-myfunction" as the function name. I.e., skip the first char if
652 it is an hyphen. Don't skip the first char always, because we
653 may have C++ "operator<", and find_toplevel_char needs to see the
654 'o' in that case. */
655 const char *hyphen
656 = (*start == '-'
657 ? find_toplevel_char (start + 1, '-')
658 : find_toplevel_char (start, '-'));
659
660 /* Check for C++ "operator," and "operator-". */
661 comma = skip_op_false_positives (start, comma);
662 hyphen = skip_op_false_positives (start, hyphen);
663
664 /* Pick the one that appears first. */
665 const char *end = first_of (hyphen, comma);
666
667 /* See if a linespec keyword appears first. */
668 const char *s = start;
669 const char *ws = find_toplevel_char (start, ' ');
670 while (ws != NULL && linespec_lexer_lex_keyword (ws + 1) == NULL)
671 {
672 s = ws + 1;
673 ws = find_toplevel_char (s, ' ');
674 }
675 if (ws != NULL)
676 end = first_of (end, ws + 1);
677
678 /* If we don't have any terminator, then take the whole string. */
679 if (end == NULL)
680 end = start + strlen (start);
681
682 /* Trim whitespace at the end. */
683 while (end > start && end[-1] == ' ')
684 end--;
685
686 *inp = end;
687
688 if (*inp - start > 0)
689 return gdb::unique_xmalloc_ptr<char> (savestring (start, *inp - start));
690
691 return NULL;
692}
693
87f0e720
KS
694/* See description in location.h. */
695
ffc2605c 696event_location_up
87f0e720
KS
697string_to_explicit_location (const char **argp,
698 const struct language_defn *language,
c6756f62 699 explicit_completion_info *completion_info)
87f0e720 700{
ffc2605c 701 event_location_up location;
87f0e720
KS
702
703 /* It is assumed that input beginning with '-' and a non-digit
eeb1af43
KS
704 character is an explicit location. "-p" is reserved, though,
705 for probe locations. */
87f0e720 706 if (argp == NULL
e742d386 707 || *argp == NULL
87f0e720 708 || *argp[0] != '-'
eeb1af43
KS
709 || !isalpha ((*argp)[1])
710 || ((*argp)[0] == '-' && (*argp)[1] == 'p'))
87f0e720
KS
711 return NULL;
712
713 location = new_explicit_location (NULL);
87f0e720
KS
714
715 /* Process option/argument pairs. dprintf_command
716 requires that processing stop on ','. */
717 while ((*argp)[0] != '\0' && (*argp)[0] != ',')
718 {
719 int len;
87f0e720 720 const char *start;
87f0e720 721
c6756f62
PA
722 /* Clear these on each iteration, since they should be filled
723 with info about the last option. */
724 if (completion_info != NULL)
725 {
726 completion_info->quoted_arg_start = NULL;
727 completion_info->quoted_arg_end = NULL;
728 }
729
87f0e720
KS
730 /* If *ARGP starts with a keyword, stop processing
731 options. */
732 if (linespec_lexer_lex_keyword (*argp) != NULL)
733 break;
734
735 /* Mark the start of the string in case we need to rewind. */
736 start = *argp;
737
c6756f62
PA
738 if (completion_info != NULL)
739 completion_info->last_option = start;
740
87f0e720 741 /* Get the option string. */
4b217cc7 742 gdb::unique_xmalloc_ptr<char> opt
c6756f62 743 = explicit_location_lex_one (argp, language, NULL);
87f0e720 744
c6756f62
PA
745 /* Use the length of the option to allow abbreviations. */
746 len = strlen (opt.get ());
87f0e720
KS
747
748 /* Get the argument string. */
f1735a53 749 *argp = skip_spaces (*argp);
87f0e720 750
c6756f62
PA
751 /* All options have a required argument. Checking for this
752 required argument is deferred until later. */
753 gdb::unique_xmalloc_ptr<char> oarg;
754 /* True if we have an argument. This is required because we'll
755 move from OARG before checking whether we have an
756 argument. */
757 bool have_oarg = false;
758
759 /* Convenience to consistently set both OARG/HAVE_OARG from
760 ARG. */
761 auto set_oarg = [&] (gdb::unique_xmalloc_ptr<char> arg)
762 {
763 oarg = std::move (arg);
764 have_oarg = oarg != NULL;
765 };
87f0e720 766
4b217cc7 767 if (strncmp (opt.get (), "-source", len) == 0)
c6756f62
PA
768 {
769 set_oarg (explicit_location_lex_one (argp, language,
770 completion_info));
771 EL_EXPLICIT (location)->source_filename = oarg.release ();
772 }
4b217cc7 773 else if (strncmp (opt.get (), "-function", len) == 0)
c6756f62
PA
774 {
775 set_oarg (explicit_location_lex_one_function (argp, language,
776 completion_info));
777 EL_EXPLICIT (location)->function_name = oarg.release ();
778 }
4b217cc7 779 else if (strncmp (opt.get (), "-line", len) == 0)
87f0e720 780 {
c6756f62 781 set_oarg (explicit_location_lex_one (argp, language, NULL));
f1735a53 782 *argp = skip_spaces (*argp);
4b217cc7 783 if (have_oarg)
c6756f62
PA
784 {
785 EL_EXPLICIT (location)->line_offset
786 = linespec_parse_line_offset (oarg.get ());
787 continue;
788 }
87f0e720 789 }
4b217cc7 790 else if (strncmp (opt.get (), "-label", len) == 0)
c6756f62
PA
791 {
792 set_oarg (explicit_location_lex_one (argp, language, completion_info));
793 EL_EXPLICIT (location)->label_name = oarg.release ();
794 }
87f0e720
KS
795 /* Only emit an "invalid argument" error for options
796 that look like option strings. */
4b217cc7 797 else if (opt.get ()[0] == '-' && !isdigit (opt.get ()[1]))
87f0e720 798 {
c6756f62 799 if (completion_info == NULL)
4b217cc7 800 error (_("invalid explicit location argument, \"%s\""), opt.get ());
87f0e720
KS
801 }
802 else
803 {
804 /* End of the explicit location specification.
805 Stop parsing and return whatever explicit location was
806 parsed. */
807 *argp = start;
87f0e720
KS
808 return location;
809 }
810
f1735a53 811 *argp = skip_spaces (*argp);
c6756f62 812
87f0e720
KS
813 /* It's a little lame to error after the fact, but in this
814 case, it provides a much better user experience to issue
815 the "invalid argument" error before any missing
816 argument error. */
c6756f62 817 if (!have_oarg && completion_info == NULL)
4b217cc7 818 error (_("missing argument for \"%s\""), opt.get ());
87f0e720
KS
819 }
820
821 /* One special error check: If a source filename was given
822 without offset, function, or label, issue an error. */
823 if (EL_EXPLICIT (location)->source_filename != NULL
824 && EL_EXPLICIT (location)->function_name == NULL
825 && EL_EXPLICIT (location)->label_name == NULL
826 && (EL_EXPLICIT (location)->line_offset.sign == LINE_OFFSET_UNKNOWN)
c6756f62 827 && completion_info == NULL)
87f0e720
KS
828 {
829 error (_("Source filename requires function, label, or "
830 "line offset."));
831 }
832
87f0e720
KS
833 return location;
834}
835
c7c1b3e9
KS
836/* See description in location.h. */
837
ffc2605c 838event_location_up
f2fc3015 839string_to_event_location_basic (const char **stringp,
eeb1af43 840 const struct language_defn *language)
c7c1b3e9 841{
ffc2605c 842 event_location_up location;
870f88f7 843 const char *cs;
c7c1b3e9 844
eeb1af43
KS
845 /* Try the input as a probe spec. */
846 cs = *stringp;
935676c9 847 if (cs != NULL && probe_linespec_to_static_ops (&cs) != NULL)
a06efdd6 848 {
eeb1af43
KS
849 location = new_probe_location (*stringp);
850 *stringp += strlen (*stringp);
a06efdd6
KS
851 }
852 else
853 {
eeb1af43
KS
854 /* Try an address location. */
855 if (*stringp != NULL && **stringp == '*')
5b56227b 856 {
87f0e720 857 const char *arg, *orig;
eeb1af43 858 CORE_ADDR addr;
87f0e720 859
87f0e720 860 orig = arg = *stringp;
eeb1af43
KS
861 addr = linespec_expression_to_pc (&arg);
862 location = new_address_location (addr, orig, arg - orig);
863 *stringp += arg - orig;
864 }
865 else
866 {
867 /* Everything else is a linespec. */
868 location = new_linespec_location (stringp);
5b56227b 869 }
a06efdd6
KS
870 }
871
c7c1b3e9 872 return location;
eeb1af43
KS
873}
874
875/* See description in location.h. */
876
ffc2605c 877event_location_up
f2fc3015 878string_to_event_location (const char **stringp,
eeb1af43
KS
879 const struct language_defn *language)
880{
eeb1af43
KS
881 const char *arg, *orig;
882
883 /* Try an explicit location. */
884 orig = arg = *stringp;
c6756f62 885 event_location_up location = string_to_explicit_location (&arg, language, NULL);
eeb1af43
KS
886 if (location != NULL)
887 {
888 /* It was a valid explicit location. Advance STRINGP to
889 the end of input. */
890 *stringp += arg - orig;
891 }
892 else
893 {
894 /* Everything else is a "basic" linespec, address, or probe
895 location. */
896 location = string_to_event_location_basic (stringp, language);
897 }
898
899 return location;
c7c1b3e9
KS
900}
901
902/* See description in location.h. */
903
904int
905event_location_empty_p (const struct event_location *location)
906{
907 switch (EL_TYPE (location))
908 {
909 case LINESPEC_LOCATION:
910 /* Linespecs are never "empty." (NULL is a valid linespec) */
911 return 0;
912
a06efdd6
KS
913 case ADDRESS_LOCATION:
914 return 0;
915
00e52e53
KS
916 case EXPLICIT_LOCATION:
917 return (EL_EXPLICIT (location) == NULL
918 || (EL_EXPLICIT (location)->source_filename == NULL
919 && EL_EXPLICIT (location)->function_name == NULL
920 && EL_EXPLICIT (location)->label_name == NULL
921 && (EL_EXPLICIT (location)->line_offset.sign
922 == LINE_OFFSET_UNKNOWN)));
923
5b56227b
KS
924 case PROBE_LOCATION:
925 return EL_PROBE (location) == NULL;
926
c7c1b3e9
KS
927 default:
928 gdb_assert_not_reached ("unknown event location type");
929 }
930}
931
932/* See description in location.h. */
933
934void
935set_event_location_string (struct event_location *location,
936 const char *string)
937{
938 xfree (EL_STRING (location));
939 EL_STRING (location) = string == NULL ? NULL : xstrdup (string);
940}