Explicit locations: introduce explicit locations
[deliverable/binutils-gdb.git] / gdb / location.c
1 /* Data structures and API for event locations in GDB.
2 Copyright (C) 2013-2015 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(PTR) (PTR)->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(PTR) ((PTR)->u.addr_string)
48 #define EL_PROBE(PTR) ((PTR)->u.addr_string)
49
50 /* An address in the inferior. */
51 CORE_ADDR address;
52 #define EL_ADDRESS(PTR) (PTR)->u.address
53
54 /* An explicit location. */
55 struct explicit_location explicit;
56 #define EL_EXPLICIT(PTR) (&((PTR)->u.explicit))
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(PTR) ((PTR)->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)
77 {
78 memset (explicit, 0, sizeof (struct explicit_location));
79 explicit->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)
117 {
118 struct event_location *location;
119
120 location = XCNEW (struct event_location);
121 EL_TYPE (location) = ADDRESS_LOCATION;
122 EL_ADDRESS (location) = addr;
123 return location;
124 }
125
126 /* See description in location.h. */
127
128 CORE_ADDR
129 get_address_location (const struct event_location *location)
130 {
131 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
132 return EL_ADDRESS (location);
133 }
134
135 /* See description in location.h. */
136
137 struct event_location *
138 new_probe_location (const char *probe)
139 {
140 struct event_location *location;
141
142 location = XCNEW (struct event_location);
143 EL_TYPE (location) = PROBE_LOCATION;
144 if (probe != NULL)
145 EL_PROBE (location) = xstrdup (probe);
146 return location;
147 }
148
149 /* See description in location.h. */
150
151 const char *
152 get_probe_location (const struct event_location *location)
153 {
154 gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
155 return EL_PROBE (location);
156 }
157
158 /* See description in location.h. */
159
160 struct event_location *
161 new_explicit_location (const struct explicit_location *explicit)
162 {
163 struct event_location tmp;
164
165 memset (&tmp, 0, sizeof (struct event_location));
166 EL_TYPE (&tmp) = EXPLICIT_LOCATION;
167 initialize_explicit_location (EL_EXPLICIT (&tmp));
168 if (explicit != NULL)
169 {
170 if (explicit->source_filename != NULL)
171 {
172 EL_EXPLICIT (&tmp)->source_filename
173 = explicit->source_filename;
174 }
175
176 if (explicit->function_name != NULL)
177 EL_EXPLICIT (&tmp)->function_name
178 = explicit->function_name;
179
180 if (explicit->label_name != NULL)
181 EL_EXPLICIT (&tmp)->label_name = explicit->label_name;
182
183 if (explicit->line_offset.sign != LINE_OFFSET_UNKNOWN)
184 EL_EXPLICIT (&tmp)->line_offset = explicit->line_offset;
185 }
186
187 return copy_event_location (&tmp);
188 }
189
190 /* See description in location.h. */
191
192 struct explicit_location *
193 get_explicit_location (struct event_location *location)
194 {
195 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
196 return EL_EXPLICIT (location);
197 }
198
199 /* See description in location.h. */
200
201 const struct explicit_location *
202 get_explicit_location_const (const struct event_location *location)
203 {
204 gdb_assert (EL_TYPE (location) == EXPLICIT_LOCATION);
205 return EL_EXPLICIT (location);
206 }
207
208 /* This convenience function returns a malloc'd string which
209 represents the location in EXPLICIT.
210
211 AS_LINESPEC is non-zero if this string should be a linespec.
212 Otherwise it will be output in explicit form. */
213
214 static char *
215 explicit_to_string_internal (int as_linespec,
216 const struct explicit_location *explicit)
217 {
218 struct ui_file *buf;
219 char space, *result;
220 int need_space = 0;
221 struct cleanup *cleanup;
222
223 space = as_linespec ? ':' : ' ';
224 buf = mem_fileopen ();
225 cleanup = make_cleanup_ui_file_delete (buf);
226
227 if (explicit->source_filename != NULL)
228 {
229 if (!as_linespec)
230 fputs_unfiltered ("-source ", buf);
231 fputs_unfiltered (explicit->source_filename, buf);
232 need_space = 1;
233 }
234
235 if (explicit->function_name != NULL)
236 {
237 if (need_space)
238 fputc_unfiltered (space, buf);
239 if (!as_linespec)
240 fputs_unfiltered ("-function ", buf);
241 fputs_unfiltered (explicit->function_name, buf);
242 need_space = 1;
243 }
244
245 if (explicit->label_name != NULL)
246 {
247 if (need_space)
248 fputc_unfiltered (space, buf);
249 if (!as_linespec)
250 fputs_unfiltered ("-label ", buf);
251 fputs_unfiltered (explicit->label_name, buf);
252 need_space = 1;
253 }
254
255 if (explicit->line_offset.sign != LINE_OFFSET_UNKNOWN)
256 {
257 if (need_space)
258 fputc_unfiltered (space, buf);
259 if (!as_linespec)
260 fputs_unfiltered ("-line ", buf);
261 fprintf_filtered (buf, "%s%d",
262 (explicit->line_offset.sign == LINE_OFFSET_NONE ? ""
263 : (explicit->line_offset.sign
264 == LINE_OFFSET_PLUS ? "+" : "-")),
265 explicit->line_offset.offset);
266 }
267
268 result = ui_file_xstrdup (buf, NULL);
269 do_cleanups (cleanup);
270 return result;
271 }
272
273 /* See description in location.h. */
274
275 char *
276 explicit_location_to_string (const struct explicit_location *explicit)
277 {
278 return explicit_to_string_internal (0, explicit);
279 }
280
281 /* See description in location.h. */
282
283 char *
284 explicit_location_to_linespec (const struct explicit_location *explicit)
285 {
286 return explicit_to_string_internal (1, explicit);
287 }
288
289 /* See description in location.h. */
290
291 struct event_location *
292 copy_event_location (const struct event_location *src)
293 {
294 struct event_location *dst;
295
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));
300
301 switch (EL_TYPE (src))
302 {
303 case LINESPEC_LOCATION:
304 if (EL_LINESPEC (src) != NULL)
305 EL_LINESPEC (dst) = xstrdup (EL_LINESPEC (src));
306 break;
307
308 case ADDRESS_LOCATION:
309 EL_ADDRESS (dst) = EL_ADDRESS (src);
310 break;
311
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);
316
317 if (EL_EXPLICIT (src)->function_name != NULL)
318 EL_EXPLICIT (dst)->function_name
319 = xstrdup (EL_EXPLICIT (src)->function_name);
320
321 if (EL_EXPLICIT (src)->label_name != NULL)
322 EL_EXPLICIT (dst)->label_name = xstrdup (EL_EXPLICIT (src)->label_name);
323
324 EL_EXPLICIT (dst)->line_offset = EL_EXPLICIT (src)->line_offset;
325 break;
326
327
328 case PROBE_LOCATION:
329 if (EL_PROBE (src) != NULL)
330 EL_PROBE (dst) = xstrdup (EL_PROBE (src));
331 break;
332
333 default:
334 gdb_assert_not_reached ("unknown event location type");
335 }
336
337 return dst;
338 }
339
340 /* A cleanup function for struct event_location. */
341
342 static void
343 delete_event_location_cleanup (void *data)
344 {
345 struct event_location *location = (struct event_location *) data;
346
347 delete_event_location (location);
348 }
349
350 /* See description in location.h. */
351
352 struct cleanup *
353 make_cleanup_delete_event_location (struct event_location *location)
354 {
355 return make_cleanup (delete_event_location_cleanup, location);
356 }
357
358 /* See description in location.h. */
359
360 void
361 delete_event_location (struct event_location *location)
362 {
363 if (location != NULL)
364 {
365 xfree (EL_STRING (location));
366
367 switch (EL_TYPE (location))
368 {
369 case LINESPEC_LOCATION:
370 xfree (EL_LINESPEC (location));
371 break;
372
373 case ADDRESS_LOCATION:
374 /* Nothing to do. */
375 break;
376
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);
381 break;
382
383 case PROBE_LOCATION:
384 xfree (EL_PROBE (location));
385 break;
386
387 default:
388 gdb_assert_not_reached ("unknown event location type");
389 }
390
391 xfree (location);
392 }
393 }
394
395 /* See description in location.h. */
396
397 const char *
398 event_location_to_string (struct event_location *location)
399 {
400 if (EL_STRING (location) == NULL)
401 {
402 switch (EL_TYPE (location))
403 {
404 case LINESPEC_LOCATION:
405 if (EL_LINESPEC (location) != NULL)
406 EL_STRING (location) = xstrdup (EL_LINESPEC (location));
407 break;
408
409 case ADDRESS_LOCATION:
410 EL_STRING (location)
411 = xstrprintf ("*%s",
412 core_addr_to_string (EL_ADDRESS (location)));
413 break;
414
415 case EXPLICIT_LOCATION:
416 EL_STRING (location)
417 = explicit_location_to_string (EL_EXPLICIT (location));
418 break;
419
420 case PROBE_LOCATION:
421 EL_STRING (location) = xstrdup (EL_PROBE (location));
422 break;
423
424 default:
425 gdb_assert_not_reached ("unknown event location type");
426 }
427 }
428
429 return EL_STRING (location);
430 }
431
432 /* See description in location.h. */
433
434 struct event_location *
435 string_to_event_location (char **stringp,
436 const struct language_defn *language)
437 {
438 struct event_location *location;
439
440 /* First, check if the string is an address location. */
441 if (*stringp != NULL && **stringp == '*')
442 {
443 const char *arg, *orig;
444 CORE_ADDR addr;
445
446 orig = arg = *stringp;
447 addr = linespec_expression_to_pc (&arg);
448 location = new_address_location (addr);
449 *stringp += arg - orig;
450 }
451 else
452 {
453 const char *cs;
454
455 /* Next, try the input as a probe spec. */
456 cs = *stringp;
457 if (cs != NULL && probe_linespec_to_ops (&cs) != NULL)
458 {
459 location = new_probe_location (*stringp);
460 *stringp += strlen (*stringp);
461 }
462 else
463 {
464 /* Everything else is a linespec. */
465 location = new_linespec_location (stringp);
466 }
467 }
468
469 return location;
470 }
471
472 /* See description in location.h. */
473
474 int
475 event_location_empty_p (const struct event_location *location)
476 {
477 switch (EL_TYPE (location))
478 {
479 case LINESPEC_LOCATION:
480 /* Linespecs are never "empty." (NULL is a valid linespec) */
481 return 0;
482
483 case ADDRESS_LOCATION:
484 return 0;
485
486 case EXPLICIT_LOCATION:
487 return (EL_EXPLICIT (location) == NULL
488 || (EL_EXPLICIT (location)->source_filename == NULL
489 && EL_EXPLICIT (location)->function_name == NULL
490 && EL_EXPLICIT (location)->label_name == NULL
491 && (EL_EXPLICIT (location)->line_offset.sign
492 == LINE_OFFSET_UNKNOWN)));
493
494 case PROBE_LOCATION:
495 return EL_PROBE (location) == NULL;
496
497 default:
498 gdb_assert_not_reached ("unknown event location type");
499 }
500 }
501
502 /* See description in location.h. */
503
504 void
505 set_event_location_string (struct event_location *location,
506 const char *string)
507 {
508 xfree (EL_STRING (location));
509 EL_STRING (location) = string == NULL ? NULL : xstrdup (string);
510 }
This page took 0.039552 seconds and 4 git commands to generate.