1 /* Data structures and API for event locations in GDB.
2 Copyright (C) 2013-2015 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(PTR) (PTR)->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(PTR) ((PTR)->u.addr_string)
48 #define EL_PROBE(PTR) ((PTR)->u.addr_string)
50 /* An address in the inferior. */
52 #define EL_ADDRESS(PTR) (PTR)->u.address
54 /* An explicit location. */
55 struct explicit_location
explicit;
56 #define EL_EXPLICIT(PTR) (&((PTR)->u.explicit))
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(PTR) ((PTR)->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)
78 memset (explicit, 0, sizeof (struct explicit_location
));
79 explicit->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)
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 != NULL
)
170 if (explicit->source_filename
!= NULL
)
172 EL_EXPLICIT (&tmp
)->source_filename
173 = explicit->source_filename
;
176 if (explicit->function_name
!= NULL
)
177 EL_EXPLICIT (&tmp
)->function_name
178 = explicit->function_name
;
180 if (explicit->label_name
!= NULL
)
181 EL_EXPLICIT (&tmp
)->label_name
= explicit->label_name
;
183 if (explicit->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
184 EL_EXPLICIT (&tmp
)->line_offset
= explicit->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.
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)
221 struct cleanup
*cleanup
;
223 space
= as_linespec
? ':' : ' ';
224 buf
= mem_fileopen ();
225 cleanup
= make_cleanup_ui_file_delete (buf
);
227 if (explicit->source_filename
!= NULL
)
230 fputs_unfiltered ("-source ", buf
);
231 fputs_unfiltered (explicit->source_filename
, buf
);
235 if (explicit->function_name
!= NULL
)
238 fputc_unfiltered (space
, buf
);
240 fputs_unfiltered ("-function ", buf
);
241 fputs_unfiltered (explicit->function_name
, buf
);
245 if (explicit->label_name
!= NULL
)
248 fputc_unfiltered (space
, buf
);
250 fputs_unfiltered ("-label ", buf
);
251 fputs_unfiltered (explicit->label_name
, buf
);
255 if (explicit->line_offset
.sign
!= LINE_OFFSET_UNKNOWN
)
258 fputc_unfiltered (space
, buf
);
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
);
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)
278 return explicit_to_string_internal (0, explicit);
281 /* See description in location.h. */
284 explicit_location_to_linespec (const struct explicit_location
*explicit)
286 return explicit_to_string_internal (1, explicit);
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 /* See description in location.h. */
434 struct event_location
*
435 string_to_event_location (char **stringp
,
436 const struct language_defn
*language
)
438 struct event_location
*location
;
440 /* First, check if the string is an address location. */
441 if (*stringp
!= NULL
&& **stringp
== '*')
443 const char *arg
, *orig
;
446 orig
= arg
= *stringp
;
447 addr
= linespec_expression_to_pc (&arg
);
448 location
= new_address_location (addr
);
449 *stringp
+= arg
- orig
;
455 /* Next, try the input as a probe spec. */
457 if (cs
!= NULL
&& probe_linespec_to_ops (&cs
) != NULL
)
459 location
= new_probe_location (*stringp
);
460 *stringp
+= strlen (*stringp
);
464 /* Everything else is a linespec. */
465 location
= new_linespec_location (stringp
);
472 /* See description in location.h. */
475 event_location_empty_p (const struct event_location
*location
)
477 switch (EL_TYPE (location
))
479 case LINESPEC_LOCATION
:
480 /* Linespecs are never "empty." (NULL is a valid linespec) */
483 case ADDRESS_LOCATION
:
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
)));
495 return EL_PROBE (location
) == NULL
;
498 gdb_assert_not_reached ("unknown event location type");
502 /* See description in location.h. */
505 set_event_location_string (struct event_location
*location
,
508 xfree (EL_STRING (location
));
509 EL_STRING (location
) = string
== NULL
? NULL
: xstrdup (string
);