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