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)
118 struct event_location *location;
120 location = XCNEW (struct event_location);
121 EL_TYPE (location) = ADDRESS_LOCATION;
122 EL_ADDRESS (location) = addr;
126 /* See description in location.h. */
129 get_address_location (const struct event_location *location)
131 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
132 return EL_ADDRESS (location);
135 /* See description in location.h. */
137 struct event_location *
138 new_probe_location (const char *probe)
140 struct event_location *location;
142 location = XCNEW (struct event_location);
143 EL_TYPE (location) = PROBE_LOCATION;
145 EL_PROBE (location) = xstrdup (probe);
149 /* See description in location.h. */
152 get_probe_location (const struct event_location *location)
154 gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
155 return EL_PROBE (location);
158 /* See description in location.h. */
160 struct event_location *
161 new_explicit_location (const struct explicit_location *explicit_loc)
163 struct event_location tmp;
165 memset (&tmp, 0, sizeof (struct event_location));
166 EL_TYPE (&tmp) = EXPLICIT_LOCATION;
167 initialize_explicit_location (EL_EXPLICIT (&tmp));
168 if (explicit_loc != NULL)
170 if (explicit_loc->source_filename != NULL)
172 EL_EXPLICIT (&tmp)->source_filename
173 = explicit_loc->source_filename;
176 if (explicit_loc->function_name != NULL)
177 EL_EXPLICIT (&tmp)->function_name
178 = explicit_loc->function_name;
180 if (explicit_loc->label_name != NULL)
181 EL_EXPLICIT (&tmp)->label_name = explicit_loc->label_name;
183 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
184 EL_EXPLICIT (&tmp)->line_offset = explicit_loc->line_offset;
187 return copy_event_location (&tmp);
190 /* See description in location.h. */
192 struct explicit_location *
193 get_explicit_location (struct event_location *location)
195 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
196 return EL_EXPLICIT (location);
199 /* See description in location.h. */
201 const struct explicit_location *
202 get_explicit_location_const (const struct event_location *location)
204 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
205 return EL_EXPLICIT (location);
208 /* This convenience function returns a malloc'd string which
209 represents the location in EXPLICIT_LOC.
211 AS_LINESPEC is non-zero if this string should be a linespec.
212 Otherwise it will be output in explicit form. */
215 explicit_to_string_internal (int as_linespec,
216 const struct explicit_location *explicit_loc)
221 struct cleanup *cleanup;
223 space = as_linespec ? ':' : ' ';
224 buf = mem_fileopen ();
225 cleanup = make_cleanup_ui_file_delete (buf);
227 if (explicit_loc->source_filename != NULL)
230 fputs_unfiltered ("-source ", buf);
231 fputs_unfiltered (explicit_loc->source_filename, buf);
235 if (explicit_loc->function_name != NULL)
238 fputc_unfiltered (space, buf);
240 fputs_unfiltered ("-function ", buf);
241 fputs_unfiltered (explicit_loc->function_name, buf);
245 if (explicit_loc->label_name != NULL)
248 fputc_unfiltered (space, buf);
250 fputs_unfiltered ("-label ", buf);
251 fputs_unfiltered (explicit_loc->label_name, buf);
255 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
258 fputc_unfiltered (space, buf);
260 fputs_unfiltered ("-line ", buf);
261 fprintf_filtered (buf, "%s%d",
262 (explicit_loc->line_offset.sign == LINE_OFFSET_NONE ? ""
263 : (explicit_loc->line_offset.sign
264 == LINE_OFFSET_PLUS ? "+" : "-")),
265 explicit_loc->line_offset.offset);
268 result = ui_file_xstrdup (buf, NULL);
269 do_cleanups (cleanup);
273 /* See description in location.h. */
276 explicit_location_to_string (const struct explicit_location *explicit_loc)
278 return explicit_to_string_internal (0, explicit_loc);
281 /* See description in location.h. */
284 explicit_location_to_linespec (const struct explicit_location *explicit_loc)
286 return explicit_to_string_internal (1, explicit_loc);
289 /* See description in location.h. */
291 struct event_location *
292 copy_event_location (const struct event_location *src)
294 struct event_location *dst;
296 dst = XCNEW (struct event_location);
297 EL_TYPE (dst) = EL_TYPE (src);
298 if (EL_STRING (src) != NULL)
299 EL_STRING (dst) = xstrdup (EL_STRING (src));
301 switch (EL_TYPE (src))
303 case LINESPEC_LOCATION:
304 if (EL_LINESPEC (src) != NULL)
305 EL_LINESPEC (dst) = xstrdup (EL_LINESPEC (src));
308 case ADDRESS_LOCATION:
309 EL_ADDRESS (dst) = EL_ADDRESS (src);
312 case EXPLICIT_LOCATION:
313 if (EL_EXPLICIT (src)->source_filename != NULL)
314 EL_EXPLICIT (dst)->source_filename
315 = xstrdup (EL_EXPLICIT (src)->source_filename);
317 if (EL_EXPLICIT (src)->function_name != NULL)
318 EL_EXPLICIT (dst)->function_name
319 = xstrdup (EL_EXPLICIT (src)->function_name);
321 if (EL_EXPLICIT (src)->label_name != NULL)
322 EL_EXPLICIT (dst)->label_name = xstrdup (EL_EXPLICIT (src)->label_name);
324 EL_EXPLICIT (dst)->line_offset = EL_EXPLICIT (src)->line_offset;
329 if (EL_PROBE (src) != NULL)
330 EL_PROBE (dst) = xstrdup (EL_PROBE (src));
334 gdb_assert_not_reached ("unknown event location type");
340 /* A cleanup function for struct event_location. */
343 delete_event_location_cleanup (void *data)
345 struct event_location *location = (struct event_location *) data;
347 delete_event_location (location);
350 /* See description in location.h. */
353 make_cleanup_delete_event_location (struct event_location *location)
355 return make_cleanup (delete_event_location_cleanup, location);
358 /* See description in location.h. */
361 delete_event_location (struct event_location *location)
363 if (location != NULL)
365 xfree (EL_STRING (location));
367 switch (EL_TYPE (location))
369 case LINESPEC_LOCATION:
370 xfree (EL_LINESPEC (location));
373 case ADDRESS_LOCATION:
377 case EXPLICIT_LOCATION:
378 xfree (EL_EXPLICIT (location)->source_filename);
379 xfree (EL_EXPLICIT (location)->function_name);
380 xfree (EL_EXPLICIT (location)->label_name);
384 xfree (EL_PROBE (location));
388 gdb_assert_not_reached ("unknown event location type");
395 /* See description in location.h. */
398 event_location_to_string (struct event_location *location)
400 if (EL_STRING (location) == NULL)
402 switch (EL_TYPE (location))
404 case LINESPEC_LOCATION:
405 if (EL_LINESPEC (location) != NULL)
406 EL_STRING (location) = xstrdup (EL_LINESPEC (location));
409 case ADDRESS_LOCATION:
412 core_addr_to_string (EL_ADDRESS (location)));
415 case EXPLICIT_LOCATION:
417 = explicit_location_to_string (EL_EXPLICIT (location));
421 EL_STRING (location) = xstrdup (EL_PROBE (location));
425 gdb_assert_not_reached ("unknown event location type");
429 return EL_STRING (location);
432 /* A lexer for explicit locations. This function will advance INP
433 past any strings that it lexes. Returns a malloc'd copy of the
434 lexed string or NULL if no lexing was done. */
437 explicit_location_lex_one (const char **inp,
438 const struct language_defn *language)
440 const char *start = *inp;
445 /* If quoted, skip to the ending quote. */
446 if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
448 char quote_char = *start;
450 /* If the input is not an Ada operator, skip to the matching
451 closing quote and return the string. */
452 if (!(language->la_language == language_ada
453 && quote_char == '\"' && is_ada_operator (start)))
455 const char *end = find_toplevel_char (start + 1, quote_char);
458 error (_("Unmatched quote, %s."), start);
460 return savestring (start + 1, *inp - start - 2);
464 /* If the input starts with '-' or '+', the string ends with the next
465 whitespace or comma. */
466 if (*start == '-' || *start == '+')
468 while (*inp[0] != '\0' && *inp[0] != ',' && !isspace (*inp[0]))
473 /* Handle numbers first, stopping at the next whitespace or ','. */
474 while (isdigit (*inp[0]))
476 if (*inp[0] == '\0' || isspace (*inp[0]) || *inp[0] == ',')
477 return savestring (start, *inp - start);
479 /* Otherwise stop at the next occurrence of whitespace, '\0',
484 && !(isspace ((*inp)[0])
485 || linespec_lexer_lex_keyword (&(*inp)[1])))
487 /* Special case: C++ operator,. */
488 if (language->la_language == language_cplus
489 && strncmp (*inp, "operator", 8)
496 if (*inp - start > 0)
497 return savestring (start, *inp - start);
502 /* See description in location.h. */
504 struct event_location *
505 string_to_explicit_location (const char **argp,
506 const struct language_defn *language,
509 struct cleanup *cleanup;
510 struct event_location *location;
512 /* It is assumed that input beginning with '-' and a non-digit
513 character is an explicit location. */
517 || !isalpha ((*argp)[1]))
520 location = new_explicit_location (NULL);
521 cleanup = make_cleanup_delete_event_location (location);
523 /* Process option/argument pairs. dprintf_command
524 requires that processing stop on ','. */
525 while ((*argp)[0] != '\0' && (*argp)[0] != ',')
530 struct cleanup *opt_cleanup, *oarg_cleanup;
532 /* If *ARGP starts with a keyword, stop processing
534 if (linespec_lexer_lex_keyword (*argp) != NULL)
537 /* Mark the start of the string in case we need to rewind. */
540 /* Get the option string. */
541 opt = explicit_location_lex_one (argp, language);
542 opt_cleanup = make_cleanup (xfree, opt);
544 *argp = skip_spaces_const (*argp);
546 /* Get the argument string. */
547 oarg = explicit_location_lex_one (argp, language);
548 oarg_cleanup = make_cleanup (xfree, oarg);
549 *argp = skip_spaces_const (*argp);
551 /* Use the length of the option to allow abbreviations. */
554 /* All options have a required argument. Checking for this required
555 argument is deferred until later. */
556 if (strncmp (opt, "-source", len) == 0)
557 EL_EXPLICIT (location)->source_filename = oarg;
558 else if (strncmp (opt, "-function", len) == 0)
559 EL_EXPLICIT (location)->function_name = oarg;
560 else if (strncmp (opt, "-line", len) == 0)
564 EL_EXPLICIT (location)->line_offset
565 = linespec_parse_line_offset (oarg);
566 do_cleanups (oarg_cleanup);
567 do_cleanups (opt_cleanup);
571 else if (strncmp (opt, "-label", len) == 0)
572 EL_EXPLICIT (location)->label_name = oarg;
573 /* Only emit an "invalid argument" error for options
574 that look like option strings. */
575 else if (opt[0] == '-' && !isdigit (opt[1]))
578 error (_("invalid explicit location argument, \"%s\""), opt);
582 /* End of the explicit location specification.
583 Stop parsing and return whatever explicit location was
586 discard_cleanups (oarg_cleanup);
587 do_cleanups (opt_cleanup);
588 discard_cleanups (cleanup);
592 /* It's a little lame to error after the fact, but in this
593 case, it provides a much better user experience to issue
594 the "invalid argument" error before any missing
596 if (oarg == NULL && !dont_throw)
597 error (_("missing argument for \"%s\""), opt);
599 /* The option/argument pair was successfully processed;
600 oarg belongs to the explicit location, and opt should
602 discard_cleanups (oarg_cleanup);
603 do_cleanups (opt_cleanup);
606 /* One special error check: If a source filename was given
607 without offset, function, or label, issue an error. */
608 if (EL_EXPLICIT (location)->source_filename != NULL
609 && EL_EXPLICIT (location)->function_name == NULL
610 && EL_EXPLICIT (location)->label_name == NULL
611 && (EL_EXPLICIT (location)->line_offset.sign == LINE_OFFSET_UNKNOWN)
614 error (_("Source filename requires function, label, or "
618 discard_cleanups (cleanup);
622 /* See description in location.h. */
624 struct event_location *
625 string_to_event_location (char **stringp,
626 const struct language_defn *language)
628 struct event_location *location;
630 /* First, check if the string is an address location. */
631 if (*stringp != NULL && **stringp == '*')
633 const char *arg, *orig;
636 orig = arg = *stringp;
637 addr = linespec_expression_to_pc (&arg);
638 location = new_address_location (addr);
639 *stringp += arg - orig;
645 /* Next, try the input as a probe spec. */
647 if (cs != NULL && probe_linespec_to_ops (&cs) != NULL)
649 location = new_probe_location (*stringp);
650 *stringp += strlen (*stringp);
654 const char *arg, *orig;
656 /* Next, try an explicit location. */
657 orig = arg = *stringp;
658 location = string_to_explicit_location (&arg, language, 0);
659 if (location != NULL)
661 /* It was a valid explicit location. Advance STRINGP to
663 *stringp += arg - orig;
667 /* Everything else is a linespec. */
668 location = new_linespec_location (stringp);
676 /* See description in location.h. */
679 event_location_empty_p (const struct event_location *location)
681 switch (EL_TYPE (location))
683 case LINESPEC_LOCATION:
684 /* Linespecs are never "empty." (NULL is a valid linespec) */
687 case ADDRESS_LOCATION:
690 case EXPLICIT_LOCATION:
691 return (EL_EXPLICIT (location) == NULL
692 || (EL_EXPLICIT (location)->source_filename == NULL
693 && EL_EXPLICIT (location)->function_name == NULL
694 && EL_EXPLICIT (location)->label_name == NULL
695 && (EL_EXPLICIT (location)->line_offset.sign
696 == LINE_OFFSET_UNKNOWN)));
699 return EL_PROBE (location) == NULL;
702 gdb_assert_not_reached ("unknown event location type");
706 /* See description in location.h. */
709 set_event_location_string (struct event_location *location,
712 xfree (EL_STRING (location));
713 EL_STRING (location) = string == NULL ? NULL : xstrdup (string);