arc: Add "maintenance print arc" command prefix
[deliverable/binutils-gdb.git] / gdb / location.c
1 /* Data structures and API for event locations in GDB.
2 Copyright (C) 2013-2017 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 "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
28 #include <ctype.h>
29 #include <string.h>
30
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. */
34
35 struct event_location
36 {
37 /* The type of this breakpoint specification. */
38 enum event_location_type type;
39 #define EL_TYPE(P) (P)->type
40
41 union
42 {
43 /* A generic "this is a string specification" for a location.
44 This representation is used by both "normal" linespecs and
45 probes. */
46 char *addr_string;
47 #define EL_LINESPEC(P) ((P)->u.addr_string)
48 #define EL_PROBE(P) ((P)->u.addr_string)
49
50 /* An address in the inferior. */
51 CORE_ADDR address;
52 #define EL_ADDRESS(P) (P)->u.address
53
54 /* An explicit location. */
55 struct explicit_location explicit_loc;
56 #define EL_EXPLICIT(P) (&((P)->u.explicit_loc))
57 } u;
58
59 /* Cached string representation of this location. This is used, e.g., to
60 save stop event locations to file. Malloc'd. */
61 char *as_string;
62 #define EL_STRING(P) ((P)->as_string)
63 };
64
65 /* See description in location.h. */
66
67 enum event_location_type
68 event_location_type (const struct event_location *location)
69 {
70 return EL_TYPE (location);
71 }
72
73 /* See description in location.h. */
74
75 void
76 initialize_explicit_location (struct explicit_location *explicit_loc)
77 {
78 memset (explicit_loc, 0, sizeof (struct explicit_location));
79 explicit_loc->line_offset.sign = LINE_OFFSET_UNKNOWN;
80 }
81
82 /* See description in location.h. */
83
84 struct event_location *
85 new_linespec_location (char **linespec)
86 {
87 struct event_location *location;
88
89 location = XCNEW (struct event_location);
90 EL_TYPE (location) = LINESPEC_LOCATION;
91 if (*linespec != NULL)
92 {
93 char *p;
94 char *orig = *linespec;
95
96 linespec_lex_to_end (linespec);
97 p = remove_trailing_whitespace (orig, *linespec);
98 if ((p - orig) > 0)
99 EL_LINESPEC (location) = savestring (orig, p - orig);
100 }
101 return location;
102 }
103
104 /* See description in location.h. */
105
106 const char *
107 get_linespec_location (const struct event_location *location)
108 {
109 gdb_assert (EL_TYPE (location) == LINESPEC_LOCATION);
110 return EL_LINESPEC (location);
111 }
112
113 /* See description in location.h. */
114
115 struct event_location *
116 new_address_location (CORE_ADDR addr, const char *addr_string,
117 int addr_string_len)
118 {
119 struct event_location *location;
120
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);
126 return location;
127 }
128
129 /* See description in location.h. */
130
131 CORE_ADDR
132 get_address_location (const struct event_location *location)
133 {
134 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
135 return EL_ADDRESS (location);
136 }
137
138 /* See description in location.h. */
139
140 const char *
141 get_address_string_location (const struct event_location *location)
142 {
143 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
144 return EL_STRING (location);
145 }
146
147 /* See description in location.h. */
148
149 struct event_location *
150 new_probe_location (const char *probe)
151 {
152 struct event_location *location;
153
154 location = XCNEW (struct event_location);
155 EL_TYPE (location) = PROBE_LOCATION;
156 if (probe != NULL)
157 EL_PROBE (location) = xstrdup (probe);
158 return location;
159 }
160
161 /* See description in location.h. */
162
163 const char *
164 get_probe_location (const struct event_location *location)
165 {
166 gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
167 return EL_PROBE (location);
168 }
169
170 /* See description in location.h. */
171
172 struct event_location *
173 new_explicit_location (const struct explicit_location *explicit_loc)
174 {
175 struct event_location tmp;
176
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)
181 {
182 if (explicit_loc->source_filename != NULL)
183 {
184 EL_EXPLICIT (&tmp)->source_filename
185 = explicit_loc->source_filename;
186 }
187
188 if (explicit_loc->function_name != NULL)
189 EL_EXPLICIT (&tmp)->function_name
190 = explicit_loc->function_name;
191
192 if (explicit_loc->label_name != NULL)
193 EL_EXPLICIT (&tmp)->label_name = explicit_loc->label_name;
194
195 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
196 EL_EXPLICIT (&tmp)->line_offset = explicit_loc->line_offset;
197 }
198
199 return copy_event_location (&tmp);
200 }
201
202 /* See description in location.h. */
203
204 struct explicit_location *
205 get_explicit_location (struct event_location *location)
206 {
207 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
208 return EL_EXPLICIT (location);
209 }
210
211 /* See description in location.h. */
212
213 const struct explicit_location *
214 get_explicit_location_const (const struct event_location *location)
215 {
216 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
217 return EL_EXPLICIT (location);
218 }
219
220 /* This convenience function returns a malloc'd string which
221 represents the location in EXPLICIT_LOC.
222
223 AS_LINESPEC is non-zero if this string should be a linespec.
224 Otherwise it will be output in explicit form. */
225
226 static char *
227 explicit_to_string_internal (int as_linespec,
228 const struct explicit_location *explicit_loc)
229 {
230 int need_space = 0;
231 char space = as_linespec ? ':' : ' ';
232 string_file buf;
233
234 if (explicit_loc->source_filename != NULL)
235 {
236 if (!as_linespec)
237 buf.puts ("-source ");
238 buf.puts (explicit_loc->source_filename);
239 need_space = 1;
240 }
241
242 if (explicit_loc->function_name != NULL)
243 {
244 if (need_space)
245 buf.putc (space);
246 if (!as_linespec)
247 buf.puts ("-function ");
248 buf.puts (explicit_loc->function_name);
249 need_space = 1;
250 }
251
252 if (explicit_loc->label_name != NULL)
253 {
254 if (need_space)
255 buf.putc (space);
256 if (!as_linespec)
257 buf.puts ("-label ");
258 buf.puts (explicit_loc->label_name);
259 need_space = 1;
260 }
261
262 if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
263 {
264 if (need_space)
265 buf.putc (space);
266 if (!as_linespec)
267 buf.puts ("-line ");
268 buf.printf ("%s%d",
269 (explicit_loc->line_offset.sign == LINE_OFFSET_NONE ? ""
270 : (explicit_loc->line_offset.sign
271 == LINE_OFFSET_PLUS ? "+" : "-")),
272 explicit_loc->line_offset.offset);
273 }
274
275 return xstrdup (buf.c_str ());
276 }
277
278 /* See description in location.h. */
279
280 char *
281 explicit_location_to_string (const struct explicit_location *explicit_loc)
282 {
283 return explicit_to_string_internal (0, explicit_loc);
284 }
285
286 /* See description in location.h. */
287
288 char *
289 explicit_location_to_linespec (const struct explicit_location *explicit_loc)
290 {
291 return explicit_to_string_internal (1, explicit_loc);
292 }
293
294 /* See description in location.h. */
295
296 struct event_location *
297 copy_event_location (const struct event_location *src)
298 {
299 struct event_location *dst;
300
301 dst = XCNEW (struct event_location);
302 EL_TYPE (dst) = EL_TYPE (src);
303 if (EL_STRING (src) != NULL)
304 EL_STRING (dst) = xstrdup (EL_STRING (src));
305
306 switch (EL_TYPE (src))
307 {
308 case LINESPEC_LOCATION:
309 if (EL_LINESPEC (src) != NULL)
310 EL_LINESPEC (dst) = xstrdup (EL_LINESPEC (src));
311 break;
312
313 case ADDRESS_LOCATION:
314 EL_ADDRESS (dst) = EL_ADDRESS (src);
315 break;
316
317 case EXPLICIT_LOCATION:
318 if (EL_EXPLICIT (src)->source_filename != NULL)
319 EL_EXPLICIT (dst)->source_filename
320 = xstrdup (EL_EXPLICIT (src)->source_filename);
321
322 if (EL_EXPLICIT (src)->function_name != NULL)
323 EL_EXPLICIT (dst)->function_name
324 = xstrdup (EL_EXPLICIT (src)->function_name);
325
326 if (EL_EXPLICIT (src)->label_name != NULL)
327 EL_EXPLICIT (dst)->label_name = xstrdup (EL_EXPLICIT (src)->label_name);
328
329 EL_EXPLICIT (dst)->line_offset = EL_EXPLICIT (src)->line_offset;
330 break;
331
332
333 case PROBE_LOCATION:
334 if (EL_PROBE (src) != NULL)
335 EL_PROBE (dst) = xstrdup (EL_PROBE (src));
336 break;
337
338 default:
339 gdb_assert_not_reached ("unknown event location type");
340 }
341
342 return dst;
343 }
344
345 /* A cleanup function for struct event_location. */
346
347 static void
348 delete_event_location_cleanup (void *data)
349 {
350 struct event_location *location = (struct event_location *) data;
351
352 delete_event_location (location);
353 }
354
355 /* See description in location.h. */
356
357 struct cleanup *
358 make_cleanup_delete_event_location (struct event_location *location)
359 {
360 return make_cleanup (delete_event_location_cleanup, location);
361 }
362
363 /* See description in location.h. */
364
365 void
366 delete_event_location (struct event_location *location)
367 {
368 if (location != NULL)
369 {
370 xfree (EL_STRING (location));
371
372 switch (EL_TYPE (location))
373 {
374 case LINESPEC_LOCATION:
375 xfree (EL_LINESPEC (location));
376 break;
377
378 case ADDRESS_LOCATION:
379 /* Nothing to do. */
380 break;
381
382 case EXPLICIT_LOCATION:
383 xfree (EL_EXPLICIT (location)->source_filename);
384 xfree (EL_EXPLICIT (location)->function_name);
385 xfree (EL_EXPLICIT (location)->label_name);
386 break;
387
388 case PROBE_LOCATION:
389 xfree (EL_PROBE (location));
390 break;
391
392 default:
393 gdb_assert_not_reached ("unknown event location type");
394 }
395
396 xfree (location);
397 }
398 }
399
400 /* See description in location.h. */
401
402 const char *
403 event_location_to_string (struct event_location *location)
404 {
405 if (EL_STRING (location) == NULL)
406 {
407 switch (EL_TYPE (location))
408 {
409 case LINESPEC_LOCATION:
410 if (EL_LINESPEC (location) != NULL)
411 EL_STRING (location) = xstrdup (EL_LINESPEC (location));
412 break;
413
414 case ADDRESS_LOCATION:
415 EL_STRING (location)
416 = xstrprintf ("*%s",
417 core_addr_to_string (EL_ADDRESS (location)));
418 break;
419
420 case EXPLICIT_LOCATION:
421 EL_STRING (location)
422 = explicit_location_to_string (EL_EXPLICIT (location));
423 break;
424
425 case PROBE_LOCATION:
426 EL_STRING (location) = xstrdup (EL_PROBE (location));
427 break;
428
429 default:
430 gdb_assert_not_reached ("unknown event location type");
431 }
432 }
433
434 return EL_STRING (location);
435 }
436
437 /* A lexer for explicit locations. This function will advance INP
438 past any strings that it lexes. Returns a malloc'd copy of the
439 lexed string or NULL if no lexing was done. */
440
441 static char *
442 explicit_location_lex_one (const char **inp,
443 const struct language_defn *language)
444 {
445 const char *start = *inp;
446
447 if (*start == '\0')
448 return NULL;
449
450 /* If quoted, skip to the ending quote. */
451 if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
452 {
453 char quote_char = *start;
454
455 /* If the input is not an Ada operator, skip to the matching
456 closing quote and return the string. */
457 if (!(language->la_language == language_ada
458 && quote_char == '\"' && is_ada_operator (start)))
459 {
460 const char *end = find_toplevel_char (start + 1, quote_char);
461
462 if (end == NULL)
463 error (_("Unmatched quote, %s."), start);
464 *inp = end + 1;
465 return savestring (start + 1, *inp - start - 2);
466 }
467 }
468
469 /* If the input starts with '-' or '+', the string ends with the next
470 whitespace or comma. */
471 if (*start == '-' || *start == '+')
472 {
473 while (*inp[0] != '\0' && *inp[0] != ',' && !isspace (*inp[0]))
474 ++(*inp);
475 }
476 else
477 {
478 /* Handle numbers first, stopping at the next whitespace or ','. */
479 while (isdigit (*inp[0]))
480 ++(*inp);
481 if (*inp[0] == '\0' || isspace (*inp[0]) || *inp[0] == ',')
482 return savestring (start, *inp - start);
483
484 /* Otherwise stop at the next occurrence of whitespace, '\0',
485 keyword, or ','. */
486 *inp = start;
487 while ((*inp)[0]
488 && (*inp)[0] != ','
489 && !(isspace ((*inp)[0])
490 || linespec_lexer_lex_keyword (&(*inp)[1])))
491 {
492 /* Special case: C++ operator,. */
493 if (language->la_language == language_cplus
494 && strncmp (*inp, "operator", 8) == 0)
495 (*inp) += 8;
496 ++(*inp);
497 }
498 }
499
500 if (*inp - start > 0)
501 return savestring (start, *inp - start);
502
503 return NULL;
504 }
505
506 /* See description in location.h. */
507
508 struct event_location *
509 string_to_explicit_location (const char **argp,
510 const struct language_defn *language,
511 int dont_throw)
512 {
513 struct cleanup *cleanup;
514 struct event_location *location;
515
516 /* It is assumed that input beginning with '-' and a non-digit
517 character is an explicit location. "-p" is reserved, though,
518 for probe locations. */
519 if (argp == NULL
520 || *argp == NULL
521 || *argp[0] != '-'
522 || !isalpha ((*argp)[1])
523 || ((*argp)[0] == '-' && (*argp)[1] == 'p'))
524 return NULL;
525
526 location = new_explicit_location (NULL);
527 cleanup = make_cleanup_delete_event_location (location);
528
529 /* Process option/argument pairs. dprintf_command
530 requires that processing stop on ','. */
531 while ((*argp)[0] != '\0' && (*argp)[0] != ',')
532 {
533 int len;
534 char *opt, *oarg;
535 const char *start;
536 struct cleanup *opt_cleanup, *oarg_cleanup;
537
538 /* If *ARGP starts with a keyword, stop processing
539 options. */
540 if (linespec_lexer_lex_keyword (*argp) != NULL)
541 break;
542
543 /* Mark the start of the string in case we need to rewind. */
544 start = *argp;
545
546 /* Get the option string. */
547 opt = explicit_location_lex_one (argp, language);
548 opt_cleanup = make_cleanup (xfree, opt);
549
550 *argp = skip_spaces_const (*argp);
551
552 /* Get the argument string. */
553 oarg = explicit_location_lex_one (argp, language);
554 oarg_cleanup = make_cleanup (xfree, oarg);
555 *argp = skip_spaces_const (*argp);
556
557 /* Use the length of the option to allow abbreviations. */
558 len = strlen (opt);
559
560 /* All options have a required argument. Checking for this required
561 argument is deferred until later. */
562 if (strncmp (opt, "-source", len) == 0)
563 EL_EXPLICIT (location)->source_filename = oarg;
564 else if (strncmp (opt, "-function", len) == 0)
565 EL_EXPLICIT (location)->function_name = oarg;
566 else if (strncmp (opt, "-line", len) == 0)
567 {
568 if (oarg != NULL)
569 {
570 EL_EXPLICIT (location)->line_offset
571 = linespec_parse_line_offset (oarg);
572 do_cleanups (oarg_cleanup);
573 do_cleanups (opt_cleanup);
574 continue;
575 }
576 }
577 else if (strncmp (opt, "-label", len) == 0)
578 EL_EXPLICIT (location)->label_name = oarg;
579 /* Only emit an "invalid argument" error for options
580 that look like option strings. */
581 else if (opt[0] == '-' && !isdigit (opt[1]))
582 {
583 if (!dont_throw)
584 error (_("invalid explicit location argument, \"%s\""), opt);
585 }
586 else
587 {
588 /* End of the explicit location specification.
589 Stop parsing and return whatever explicit location was
590 parsed. */
591 *argp = start;
592 discard_cleanups (oarg_cleanup);
593 do_cleanups (opt_cleanup);
594 discard_cleanups (cleanup);
595 return location;
596 }
597
598 /* It's a little lame to error after the fact, but in this
599 case, it provides a much better user experience to issue
600 the "invalid argument" error before any missing
601 argument error. */
602 if (oarg == NULL && !dont_throw)
603 error (_("missing argument for \"%s\""), opt);
604
605 /* The option/argument pair was successfully processed;
606 oarg belongs to the explicit location, and opt should
607 be freed. */
608 discard_cleanups (oarg_cleanup);
609 do_cleanups (opt_cleanup);
610 }
611
612 /* One special error check: If a source filename was given
613 without offset, function, or label, issue an error. */
614 if (EL_EXPLICIT (location)->source_filename != NULL
615 && EL_EXPLICIT (location)->function_name == NULL
616 && EL_EXPLICIT (location)->label_name == NULL
617 && (EL_EXPLICIT (location)->line_offset.sign == LINE_OFFSET_UNKNOWN)
618 && !dont_throw)
619 {
620 error (_("Source filename requires function, label, or "
621 "line offset."));
622 }
623
624 discard_cleanups (cleanup);
625 return location;
626 }
627
628 /* See description in location.h. */
629
630 struct event_location *
631 string_to_event_location_basic (char **stringp,
632 const struct language_defn *language)
633 {
634 struct event_location *location;
635 const char *cs;
636
637 /* Try the input as a probe spec. */
638 cs = *stringp;
639 if (cs != NULL && probe_linespec_to_ops (&cs) != NULL)
640 {
641 location = new_probe_location (*stringp);
642 *stringp += strlen (*stringp);
643 }
644 else
645 {
646 /* Try an address location. */
647 if (*stringp != NULL && **stringp == '*')
648 {
649 const char *arg, *orig;
650 CORE_ADDR addr;
651
652 orig = arg = *stringp;
653 addr = linespec_expression_to_pc (&arg);
654 location = new_address_location (addr, orig, arg - orig);
655 *stringp += arg - orig;
656 }
657 else
658 {
659 /* Everything else is a linespec. */
660 location = new_linespec_location (stringp);
661 }
662 }
663
664 return location;
665 }
666
667 /* See description in location.h. */
668
669 struct event_location *
670 string_to_event_location (char **stringp,
671 const struct language_defn *language)
672 {
673 struct event_location *location;
674 const char *arg, *orig;
675
676 /* Try an explicit location. */
677 orig = arg = *stringp;
678 location = string_to_explicit_location (&arg, language, 0);
679 if (location != NULL)
680 {
681 /* It was a valid explicit location. Advance STRINGP to
682 the end of input. */
683 *stringp += arg - orig;
684 }
685 else
686 {
687 /* Everything else is a "basic" linespec, address, or probe
688 location. */
689 location = string_to_event_location_basic (stringp, language);
690 }
691
692 return location;
693 }
694
695 /* See description in location.h. */
696
697 int
698 event_location_empty_p (const struct event_location *location)
699 {
700 switch (EL_TYPE (location))
701 {
702 case LINESPEC_LOCATION:
703 /* Linespecs are never "empty." (NULL is a valid linespec) */
704 return 0;
705
706 case ADDRESS_LOCATION:
707 return 0;
708
709 case EXPLICIT_LOCATION:
710 return (EL_EXPLICIT (location) == NULL
711 || (EL_EXPLICIT (location)->source_filename == NULL
712 && EL_EXPLICIT (location)->function_name == NULL
713 && EL_EXPLICIT (location)->label_name == NULL
714 && (EL_EXPLICIT (location)->line_offset.sign
715 == LINE_OFFSET_UNKNOWN)));
716
717 case PROBE_LOCATION:
718 return EL_PROBE (location) == NULL;
719
720 default:
721 gdb_assert_not_reached ("unknown event location type");
722 }
723 }
724
725 /* See description in location.h. */
726
727 void
728 set_event_location_string (struct event_location *location,
729 const char *string)
730 {
731 xfree (EL_STRING (location));
732 EL_STRING (location) = string == NULL ? NULL : xstrdup (string);
733 }
This page took 0.043337 seconds and 4 git commands to generate.