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