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