Explicit locations: introduce probe 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 } u;
54
55 /* Cached string representation of this location. This is used, e.g., to
56 save stop event locations to file. Malloc'd. */
57 char *as_string;
58 #define EL_STRING(PTR) ((PTR)->as_string)
59 };
60
61 /* See description in location.h. */
62
63 enum event_location_type
64 event_location_type (const struct event_location *location)
65 {
66 return EL_TYPE (location);
67 }
68
69 /* See description in location.h. */
70
71 struct event_location *
72 new_linespec_location (char **linespec)
73 {
74 struct event_location *location;
75
76 location = XCNEW (struct event_location);
77 EL_TYPE (location) = LINESPEC_LOCATION;
78 if (*linespec != NULL)
79 {
80 char *p;
81 char *orig = *linespec;
82
83 linespec_lex_to_end (linespec);
84 p = remove_trailing_whitespace (orig, *linespec);
85 if ((p - orig) > 0)
86 EL_LINESPEC (location) = savestring (orig, p - orig);
87 }
88 return location;
89 }
90
91 /* See description in location.h. */
92
93 const char *
94 get_linespec_location (const struct event_location *location)
95 {
96 gdb_assert (EL_TYPE (location) == LINESPEC_LOCATION);
97 return EL_LINESPEC (location);
98 }
99
100 /* See description in location.h. */
101
102 struct event_location *
103 new_address_location (CORE_ADDR addr)
104 {
105 struct event_location *location;
106
107 location = XCNEW (struct event_location);
108 EL_TYPE (location) = ADDRESS_LOCATION;
109 EL_ADDRESS (location) = addr;
110 return location;
111 }
112
113 /* See description in location.h. */
114
115 CORE_ADDR
116 get_address_location (const struct event_location *location)
117 {
118 gdb_assert (EL_TYPE (location) == ADDRESS_LOCATION);
119 return EL_ADDRESS (location);
120 }
121
122 /* See description in location.h. */
123
124 struct event_location *
125 new_probe_location (const char *probe)
126 {
127 struct event_location *location;
128
129 location = XCNEW (struct event_location);
130 EL_TYPE (location) = PROBE_LOCATION;
131 if (probe != NULL)
132 EL_PROBE (location) = xstrdup (probe);
133 return location;
134 }
135
136 /* See description in location.h. */
137
138 const char *
139 get_probe_location (const struct event_location *location)
140 {
141 gdb_assert (EL_TYPE (location) == PROBE_LOCATION);
142 return EL_PROBE (location);
143 }
144
145 /* See description in location.h. */
146
147 struct event_location *
148 copy_event_location (const struct event_location *src)
149 {
150 struct event_location *dst;
151
152 dst = XCNEW (struct event_location);
153 EL_TYPE (dst) = EL_TYPE (src);
154 if (EL_STRING (src) != NULL)
155 EL_STRING (dst) = xstrdup (EL_STRING (src));
156
157 switch (EL_TYPE (src))
158 {
159 case LINESPEC_LOCATION:
160 if (EL_LINESPEC (src) != NULL)
161 EL_LINESPEC (dst) = xstrdup (EL_LINESPEC (src));
162 break;
163
164 case ADDRESS_LOCATION:
165 EL_ADDRESS (dst) = EL_ADDRESS (src);
166 break;
167
168 case PROBE_LOCATION:
169 if (EL_PROBE (src) != NULL)
170 EL_PROBE (dst) = xstrdup (EL_PROBE (src));
171 break;
172
173 default:
174 gdb_assert_not_reached ("unknown event location type");
175 }
176
177 return dst;
178 }
179
180 /* A cleanup function for struct event_location. */
181
182 static void
183 delete_event_location_cleanup (void *data)
184 {
185 struct event_location *location = (struct event_location *) data;
186
187 delete_event_location (location);
188 }
189
190 /* See description in location.h. */
191
192 struct cleanup *
193 make_cleanup_delete_event_location (struct event_location *location)
194 {
195 return make_cleanup (delete_event_location_cleanup, location);
196 }
197
198 /* See description in location.h. */
199
200 void
201 delete_event_location (struct event_location *location)
202 {
203 if (location != NULL)
204 {
205 xfree (EL_STRING (location));
206
207 switch (EL_TYPE (location))
208 {
209 case LINESPEC_LOCATION:
210 xfree (EL_LINESPEC (location));
211 break;
212
213 case ADDRESS_LOCATION:
214 /* Nothing to do. */
215 break;
216
217 case PROBE_LOCATION:
218 xfree (EL_PROBE (location));
219 break;
220
221 default:
222 gdb_assert_not_reached ("unknown event location type");
223 }
224
225 xfree (location);
226 }
227 }
228
229 /* See description in location.h. */
230
231 const char *
232 event_location_to_string (struct event_location *location)
233 {
234 if (EL_STRING (location) == NULL)
235 {
236 switch (EL_TYPE (location))
237 {
238 case LINESPEC_LOCATION:
239 if (EL_LINESPEC (location) != NULL)
240 EL_STRING (location) = xstrdup (EL_LINESPEC (location));
241 break;
242
243 case ADDRESS_LOCATION:
244 EL_STRING (location)
245 = xstrprintf ("*%s",
246 core_addr_to_string (EL_ADDRESS (location)));
247 break;
248
249 case PROBE_LOCATION:
250 EL_STRING (location) = xstrdup (EL_PROBE (location));
251 break;
252
253 default:
254 gdb_assert_not_reached ("unknown event location type");
255 }
256 }
257
258 return EL_STRING (location);
259 }
260
261 /* See description in location.h. */
262
263 struct event_location *
264 string_to_event_location (char **stringp,
265 const struct language_defn *language)
266 {
267 struct event_location *location;
268
269 /* First, check if the string is an address location. */
270 if (*stringp != NULL && **stringp == '*')
271 {
272 const char *arg, *orig;
273 CORE_ADDR addr;
274
275 orig = arg = *stringp;
276 addr = linespec_expression_to_pc (&arg);
277 location = new_address_location (addr);
278 *stringp += arg - orig;
279 }
280 else
281 {
282 const char *cs;
283
284 /* Next, try the input as a probe spec. */
285 cs = *stringp;
286 if (cs != NULL && probe_linespec_to_ops (&cs) != NULL)
287 {
288 location = new_probe_location (*stringp);
289 *stringp += strlen (*stringp);
290 }
291 else
292 {
293 /* Everything else is a linespec. */
294 location = new_linespec_location (stringp);
295 }
296 }
297
298 return location;
299 }
300
301 /* See description in location.h. */
302
303 int
304 event_location_empty_p (const struct event_location *location)
305 {
306 switch (EL_TYPE (location))
307 {
308 case LINESPEC_LOCATION:
309 /* Linespecs are never "empty." (NULL is a valid linespec) */
310 return 0;
311
312 case ADDRESS_LOCATION:
313 return 0;
314
315 case PROBE_LOCATION:
316 return EL_PROBE (location) == NULL;
317
318 default:
319 gdb_assert_not_reached ("unknown event location type");
320 }
321 }
322
323 /* See description in location.h. */
324
325 void
326 set_event_location_string (struct event_location *location,
327 const char *string)
328 {
329 xfree (EL_STRING (location));
330 EL_STRING (location) = string == NULL ? NULL : xstrdup (string);
331 }
This page took 0.059721 seconds and 4 git commands to generate.