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