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