1 /* Data structures and API for event locations in GDB.
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
20 #include "gdb_assert.h"
25 #include "cli/cli-utils.h"
31 /* An event location used to set a stop event in the inferior.
32 This structure is an amalgam of the various ways
33 to specify where a stop event should be set. */
37 /* The type of this breakpoint specification. */
38 enum event_location_type type;
39 #define EL_TYPE(P) (P)->type
43 /* A generic "this is a string specification" for a location.
44 This representation is used by both "normal" linespecs and
47 #define EL_LINESPEC(P) ((P)->u.addr_string)
48 #define EL_PROBE(P) ((P)->u.addr_string)
50 /* An address in the inferior. */
52 #define EL_ADDRESS(P) (P)->u.address
54 /* An explicit location. */
55 struct explicit_location explicit_loc;
56 #define EL_EXPLICIT(P) (&((P)->u.explicit_loc))
59 /* Cached string representation of this location. This is used, e.g., to
60 save stop event locations to file. Malloc'd. */
62 #define EL_STRING(P) ((P)->as_string)
65 /* See description in location.h. */
67 enum event_location_type
68 event_location_type (const struct event_location *location)
70 return EL_TYPE (location);
73 /* See description in location.h. */
76 initialize_explicit_location (struct explicit_location *explicit_loc)
78 memset (explicit_loc, 0, sizeof (struct explicit_location));
79 explicit_loc->line_offset.sign = LINE_OFFSET_UNKNOWN;
82 /* See description in location.h. */
84 struct event_location *
85 new_linespec_location (char **linespec)
87 struct event_location *location;
89 location = XCNEW (struct event_location);
90 EL_TYPE (location) = LINESPEC_LOCATION;
91 if (*linespec != NULL)
94 char *orig = *linespec;
96 linespec_lex_to_end (linespec);
97 p = remove_trailing_whitespace (orig, *linespec);
99 EL_LINESPEC (location) = savestring (orig, p - orig);
104 /* See description in location.h. */
107 get_linespec_location (const struct event_location *location)
109 gdb_assert (EL_TYPE (location) == LINESPEC_LOCATION);
110 return EL_LINESPEC (location);
113 /* See description in location.h. */
115 struct event_location *
116 new_address_location (CORE_ADDR addr, const char *addr_string,
119 struct event_location *location;
121 location = XCNEW (struct event_location);
122 EL_TYPE (location) = ADDRESS_LOCATION;
123 EL_ADDRESS (location) = addr;
124 if (addr_string != NULL)
125 EL_STRING (location) = xstrndup (addr_string, addr_string_len);
129 /* See description in location.h. */
132 get_address_location (const struct event_location *location)
134 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
135 return EL_ADDRESS (location);
138 /* See description in location.h. */
141 get_address_string_location (const struct event_location *location)
143 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
144 return EL_STRING (location);
147 /* See description in location.h. */
149 struct event_location *
150 new_probe_location (const char *probe)
152 struct event_location *location;
154 location = XCNEW (struct event_location);
155 EL_TYPE (location) = PROBE_LOCATION;
157 EL_PROBE (location) = xstrdup (probe);
161 /* See description in location.h. */
164 get_probe_location (const struct event_location *location)
166 gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
167 return EL_PROBE (location);
170 /* See description in location.h. */
172 struct event_location *
173 new_explicit_location (const struct explicit_location *explicit_loc)
175 struct event_location tmp;
177 memset (&tmp, 0, sizeof (struct event_location));
178 EL_TYPE (&tmp) = EXPLICIT_LOCATION;
179 initialize_explicit_location (EL_EXPLICIT (&tmp));
180 if (explicit_loc != NULL)
182 if (explicit_loc->source_filename != NULL)
184 EL_EXPLICIT (&tmp)->source_filename
185 = explicit_loc->source_filename;
188 if (explicit_loc->function_name != NULL)
189 EL_EXPLICIT (&tmp)->function_name
190 = explicit_loc->function_name;
192 if (explicit_loc->label_name != NULL)
193 EL_EXPLICIT (&tmp)->label_name = explicit_loc->label_name;
195 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
196 EL_EXPLICIT (&tmp)->line_offset = explicit_loc->line_offset;
199 return copy_event_location (&tmp);
202 /* See description in location.h. */
204 struct explicit_location *
205 get_explicit_location (struct event_location *location)
207 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
208 return EL_EXPLICIT (location);
211 /* See description in location.h. */
213 const struct explicit_location *
214 get_explicit_location_const (const struct event_location *location)
216 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
217 return EL_EXPLICIT (location);
220 /* This convenience function returns a malloc'd string which
221 represents the location in EXPLICIT_LOC.
223 AS_LINESPEC is non-zero if this string should be a linespec.
224 Otherwise it will be output in explicit form. */
227 explicit_to_string_internal (int as_linespec,
228 const struct explicit_location *explicit_loc)
233 struct cleanup *cleanup;
235 space = as_linespec ? ':' : ' ';
236 buf = mem_fileopen ();
237 cleanup = make_cleanup_ui_file_delete (buf);
239 if (explicit_loc->source_filename != NULL)
242 fputs_unfiltered ("-source ", buf);
243 fputs_unfiltered (explicit_loc->source_filename, buf);
247 if (explicit_loc->function_name != NULL)
250 fputc_unfiltered (space, buf);
252 fputs_unfiltered ("-function ", buf);
253 fputs_unfiltered (explicit_loc->function_name, buf);
257 if (explicit_loc->label_name != NULL)
260 fputc_unfiltered (space, buf);
262 fputs_unfiltered ("-label ", buf);
263 fputs_unfiltered (explicit_loc->label_name, buf);
267 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
270 fputc_unfiltered (space, buf);
272 fputs_unfiltered ("-line ", buf);
273 fprintf_filtered (buf, "%s%d",
274 (explicit_loc->line_offset.sign == LINE_OFFSET_NONE ? ""
275 : (explicit_loc->line_offset.sign
276 == LINE_OFFSET_PLUS ? "+" : "-")),
277 explicit_loc->line_offset.offset);
280 result = ui_file_xstrdup (buf, NULL);
281 do_cleanups (cleanup);
285 /* See description in location.h. */
288 explicit_location_to_string (const struct explicit_location *explicit_loc)
290 return explicit_to_string_internal (0, explicit_loc);
293 /* See description in location.h. */
296 explicit_location_to_linespec (const struct explicit_location *explicit_loc)
298 return explicit_to_string_internal (1, explicit_loc);
301 /* See description in location.h. */
303 struct event_location *
304 copy_event_location (const struct event_location *src)
306 struct event_location *dst;
308 dst = XCNEW (struct event_location);
309 EL_TYPE (dst) = EL_TYPE (src);
310 if (EL_STRING (src) != NULL)
311 EL_STRING (dst) = xstrdup (EL_STRING (src));
313 switch (EL_TYPE (src))
315 case LINESPEC_LOCATION:
316 if (EL_LINESPEC (src) != NULL)
317 EL_LINESPEC (dst) = xstrdup (EL_LINESPEC (src));
320 case ADDRESS_LOCATION:
321 EL_ADDRESS (dst) = EL_ADDRESS (src);
324 case EXPLICIT_LOCATION:
325 if (EL_EXPLICIT (src)->source_filename != NULL)
326 EL_EXPLICIT (dst)->source_filename
327 = xstrdup (EL_EXPLICIT (src)->source_filename);
329 if (EL_EXPLICIT (src)->function_name != NULL)
330 EL_EXPLICIT (dst)->function_name
331 = xstrdup (EL_EXPLICIT (src)->function_name);
333 if (EL_EXPLICIT (src)->label_name != NULL)
334 EL_EXPLICIT (dst)->label_name = xstrdup (EL_EXPLICIT (src)->label_name);
336 EL_EXPLICIT (dst)->line_offset = EL_EXPLICIT (src)->line_offset;
341 if (EL_PROBE (src) != NULL)
342 EL_PROBE (dst) = xstrdup (EL_PROBE (src));
346 gdb_assert_not_reached ("unknown event location type");
352 /* A cleanup function for struct event_location. */
355 delete_event_location_cleanup (void *data)
357 struct event_location *location = (struct event_location *) data;
359 delete_event_location (location);
362 /* See description in location.h. */
365 make_cleanup_delete_event_location (struct event_location *location)
367 return make_cleanup (delete_event_location_cleanup, location);
370 /* See description in location.h. */
373 delete_event_location (struct event_location *location)
375 if (location != NULL)
377 xfree (EL_STRING (location));
379 switch (EL_TYPE (location))
381 case LINESPEC_LOCATION:
382 xfree (EL_LINESPEC (location));
385 case ADDRESS_LOCATION:
389 case EXPLICIT_LOCATION:
390 xfree (EL_EXPLICIT (location)->source_filename);
391 xfree (EL_EXPLICIT (location)->function_name);
392 xfree (EL_EXPLICIT (location)->label_name);
396 xfree (EL_PROBE (location));
400 gdb_assert_not_reached ("unknown event location type");
407 /* See description in location.h. */
410 event_location_to_string (struct event_location *location)
412 if (EL_STRING (location) == NULL)
414 switch (EL_TYPE (location))
416 case LINESPEC_LOCATION:
417 if (EL_LINESPEC (location) != NULL)
418 EL_STRING (location) = xstrdup (EL_LINESPEC (location));
421 case ADDRESS_LOCATION:
424 core_addr_to_string (EL_ADDRESS (location)));
427 case EXPLICIT_LOCATION:
429 = explicit_location_to_string (EL_EXPLICIT (location));
433 EL_STRING (location) = xstrdup (EL_PROBE (location));
437 gdb_assert_not_reached ("unknown event location type");
441 return EL_STRING (location);
444 /* A lexer for explicit locations. This function will advance INP
445 past any strings that it lexes. Returns a malloc'd copy of the
446 lexed string or NULL if no lexing was done. */
449 explicit_location_lex_one (const char **inp,
450 const struct language_defn *language)
452 const char *start = *inp;
457 /* If quoted, skip to the ending quote. */
458 if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
460 char quote_char = *start;
462 /* If the input is not an Ada operator, skip to the matching
463 closing quote and return the string. */
464 if (!(language->la_language == language_ada
465 && quote_char == '\"' && is_ada_operator (start)))
467 const char *end = find_toplevel_char (start + 1, quote_char);
470 error (_("Unmatched quote, %s."), start);
472 return savestring (start + 1, *inp - start - 2);
476 /* If the input starts with '-' or '+', the string ends with the next
477 whitespace or comma. */
478 if (*start == '-' || *start == '+')
480 while (*inp[0] != '\0' && *inp[0] != ',' && !isspace (*inp[0]))
485 /* Handle numbers first, stopping at the next whitespace or ','. */
486 while (isdigit (*inp[0]))
488 if (*inp[0] == '\0' || isspace (*inp[0]) || *inp[0] == ',')
489 return savestring (start, *inp - start);
491 /* Otherwise stop at the next occurrence of whitespace, '\0',
496 && !(isspace ((*inp)[0])
497 || linespec_lexer_lex_keyword (&(*inp)[1])))
499 /* Special case: C++ operator,. */
500 if (language->la_language == language_cplus
501 && strncmp (*inp, "operator", 8) == 0)
507 if (*inp - start > 0)
508 return savestring (start, *inp - start);
513 /* See description in location.h. */
515 struct event_location *
516 string_to_explicit_location (const char **argp,
517 const struct language_defn *language,
520 struct cleanup *cleanup;
521 struct event_location *location;
523 /* It is assumed that input beginning with '-' and a non-digit
524 character is an explicit location. "-p" is reserved, though,
525 for probe locations. */
529 || !isalpha ((*argp)[1])
530 || ((*argp)[0] == '-' && (*argp)[1] == 'p'))
533 location = new_explicit_location (NULL);
534 cleanup = make_cleanup_delete_event_location (location);
536 /* Process option/argument pairs. dprintf_command
537 requires that processing stop on ','. */
538 while ((*argp)[0] != '\0' && (*argp)[0] != ',')
543 struct cleanup *opt_cleanup, *oarg_cleanup;
545 /* If *ARGP starts with a keyword, stop processing
547 if (linespec_lexer_lex_keyword (*argp) != NULL)
550 /* Mark the start of the string in case we need to rewind. */
553 /* Get the option string. */
554 opt = explicit_location_lex_one (argp, language);
555 opt_cleanup = make_cleanup (xfree, opt);
557 *argp = skip_spaces_const (*argp);
559 /* Get the argument string. */
560 oarg = explicit_location_lex_one (argp, language);
561 oarg_cleanup = make_cleanup (xfree, oarg);
562 *argp = skip_spaces_const (*argp);
564 /* Use the length of the option to allow abbreviations. */
567 /* All options have a required argument. Checking for this required
568 argument is deferred until later. */
569 if (strncmp (opt, "-source", len) == 0)
570 EL_EXPLICIT (location)->source_filename = oarg;
571 else if (strncmp (opt, "-function", len) == 0)
572 EL_EXPLICIT (location)->function_name = oarg;
573 else if (strncmp (opt, "-line", len) == 0)
577 EL_EXPLICIT (location)->line_offset
578 = linespec_parse_line_offset (oarg);
579 do_cleanups (oarg_cleanup);
580 do_cleanups (opt_cleanup);
584 else if (strncmp (opt, "-label", len) == 0)
585 EL_EXPLICIT (location)->label_name = oarg;
586 /* Only emit an "invalid argument" error for options
587 that look like option strings. */
588 else if (opt[0] == '-' && !isdigit (opt[1]))
591 error (_("invalid explicit location argument, \"%s\""), opt);
595 /* End of the explicit location specification.
596 Stop parsing and return whatever explicit location was
599 discard_cleanups (oarg_cleanup);
600 do_cleanups (opt_cleanup);
601 discard_cleanups (cleanup);
605 /* It's a little lame to error after the fact, but in this
606 case, it provides a much better user experience to issue
607 the "invalid argument" error before any missing
609 if (oarg == NULL && !dont_throw)
610 error (_("missing argument for \"%s\""), opt);
612 /* The option/argument pair was successfully processed;
613 oarg belongs to the explicit location, and opt should
615 discard_cleanups (oarg_cleanup);
616 do_cleanups (opt_cleanup);
619 /* One special error check: If a source filename was given
620 without offset, function, or label, issue an error. */
621 if (EL_EXPLICIT (location)->source_filename != NULL
622 && EL_EXPLICIT (location)->function_name == NULL
623 && EL_EXPLICIT (location)->label_name == NULL
624 && (EL_EXPLICIT (location)->line_offset.sign == LINE_OFFSET_UNKNOWN)
627 error (_("Source filename requires function, label, or "
631 discard_cleanups (cleanup);
635 /* See description in location.h. */
637 struct event_location *
638 string_to_event_location_basic (char **stringp,
639 const struct language_defn *language)
641 struct event_location *location;
644 /* Try the input as a probe spec. */
646 if (cs != NULL && probe_linespec_to_ops (&cs) != NULL)
648 location = new_probe_location (*stringp);
649 *stringp += strlen (*stringp);
653 /* Try an address location. */
654 if (*stringp != NULL && **stringp == '*')
656 const char *arg, *orig;
659 orig = arg = *stringp;
660 addr = linespec_expression_to_pc (&arg);
661 location = new_address_location (addr, orig, arg - orig);
662 *stringp += arg - orig;
666 /* Everything else is a linespec. */
667 location = new_linespec_location (stringp);
674 /* See description in location.h. */
676 struct event_location *
677 string_to_event_location (char **stringp,
678 const struct language_defn *language)
680 struct event_location *location;
681 const char *arg, *orig;
683 /* Try an explicit location. */
684 orig = arg = *stringp;
685 location = string_to_explicit_location (&arg, language, 0);
686 if (location != NULL)
688 /* It was a valid explicit location. Advance STRINGP to
690 *stringp += arg - orig;
694 /* Everything else is a "basic" linespec, address, or probe
696 location = string_to_event_location_basic (stringp, language);
702 /* See description in location.h. */
705 event_location_empty_p (const struct event_location *location)
707 switch (EL_TYPE (location))
709 case LINESPEC_LOCATION:
710 /* Linespecs are never "empty." (NULL is a valid linespec) */
713 case ADDRESS_LOCATION:
716 case EXPLICIT_LOCATION:
717 return (EL_EXPLICIT (location) == NULL
718 || (EL_EXPLICIT (location)->source_filename == NULL
719 && EL_EXPLICIT (location)->function_name == NULL
720 && EL_EXPLICIT (location)->label_name == NULL
721 && (EL_EXPLICIT (location)->line_offset.sign
722 == LINE_OFFSET_UNKNOWN)));
725 return EL_PROBE (location) == NULL;
728 gdb_assert_not_reached ("unknown event location type");
732 /* See description in location.h. */
735 set_event_location_string (struct event_location *location,
738 xfree (EL_STRING (location));
739 EL_STRING (location) = string == NULL ? NULL : xstrdup (string);