Commit | Line | Data |
---|---|---|
9843982d | 1 | /* |
5c5facc7 | 2 | * ctf/events.c |
9843982d JD |
3 | * |
4 | * Babeltrace Library | |
5 | * | |
6 | * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation | |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
9 | * Julien Desfossez <julien.desfossez@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | */ | |
21 | ||
22 | #include <babeltrace/babeltrace.h> | |
23 | #include <babeltrace/format.h> | |
24 | #include <babeltrace/ctf/events.h> | |
25 | #include <babeltrace/ctf-ir/metadata.h> | |
e4195791 MD |
26 | #include <babeltrace/prio_heap.h> |
27 | #include <babeltrace/iterator-internal.h> | |
28 | #include <babeltrace/ctf/events-internal.h> | |
29 | #include <babeltrace/ctf/metadata.h> | |
9843982d JD |
30 | #include <glib.h> |
31 | ||
c34ea0fa MD |
32 | #include "events-private.h" |
33 | ||
9843982d JD |
34 | /* |
35 | * thread local storage to store the last error that occured | |
36 | * while reading a field, this variable must be accessed by | |
37 | * bt_ctf_field_error only | |
38 | */ | |
39 | __thread int bt_ctf_last_field_error = 0; | |
40 | ||
04ae3991 | 41 | const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *event, |
9843982d JD |
42 | enum bt_ctf_scope scope) |
43 | { | |
44 | struct definition *tmp = NULL; | |
45 | ||
46 | switch (scope) { | |
47 | case BT_TRACE_PACKET_HEADER: | |
48 | if (!event->stream) | |
49 | goto error; | |
50 | if (event->stream->trace_packet_header) | |
51 | tmp = &event->stream->trace_packet_header->p; | |
52 | break; | |
53 | case BT_STREAM_PACKET_CONTEXT: | |
54 | if (!event->stream) | |
55 | goto error; | |
56 | if (event->stream->stream_packet_context) | |
57 | tmp = &event->stream->stream_packet_context->p; | |
58 | break; | |
59 | case BT_STREAM_EVENT_HEADER: | |
60 | if (!event->stream) | |
61 | goto error; | |
62 | if (event->stream->stream_event_header) | |
63 | tmp = &event->stream->stream_event_header->p; | |
64 | break; | |
65 | case BT_STREAM_EVENT_CONTEXT: | |
66 | if (!event->stream) | |
67 | goto error; | |
68 | if (event->stream->stream_event_context) | |
69 | tmp = &event->stream->stream_event_context->p; | |
70 | break; | |
71 | case BT_EVENT_CONTEXT: | |
72 | if (!event->event) | |
73 | goto error; | |
74 | if (event->event->event_context) | |
75 | tmp = &event->event->event_context->p; | |
76 | break; | |
77 | case BT_EVENT_FIELDS: | |
78 | if (!event->event) | |
79 | goto error; | |
80 | if (event->event->event_fields) | |
81 | tmp = &event->event->event_fields->p; | |
82 | break; | |
83 | } | |
84 | return tmp; | |
85 | ||
86 | error: | |
87 | return NULL; | |
88 | } | |
89 | ||
04ae3991 MD |
90 | const struct definition *bt_ctf_get_field(const struct bt_ctf_event *event, |
91 | const struct definition *scope, | |
9843982d JD |
92 | const char *field) |
93 | { | |
94 | struct definition *def; | |
300d317a | 95 | char *field_underscore; |
9843982d JD |
96 | |
97 | if (scope) { | |
98 | def = lookup_definition(scope, field); | |
300d317a JD |
99 | /* |
100 | * optionally a field can have an underscore prefix, try | |
101 | * to lookup the field with this prefix if it failed | |
102 | */ | |
103 | if (!def) { | |
104 | field_underscore = g_new(char, strlen(field) + 2); | |
105 | field_underscore[0] = '_'; | |
106 | strcpy(&field_underscore[1], field); | |
107 | def = lookup_definition(scope, field_underscore); | |
108 | g_free(field_underscore); | |
109 | } | |
9843982d JD |
110 | if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) { |
111 | struct definition_variant *variant_definition; | |
112 | variant_definition = container_of(def, | |
113 | struct definition_variant, p); | |
114 | return variant_definition->current_field; | |
115 | } | |
116 | return def; | |
117 | } | |
118 | return NULL; | |
119 | } | |
120 | ||
04ae3991 MD |
121 | const struct definition *bt_ctf_get_index(const struct bt_ctf_event *event, |
122 | const struct definition *field, | |
9843982d JD |
123 | unsigned int index) |
124 | { | |
125 | struct definition *ret = NULL; | |
126 | ||
127 | if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) { | |
128 | struct definition_array *array_definition; | |
129 | array_definition = container_of(field, | |
130 | struct definition_array, p); | |
131 | ret = array_index(array_definition, index); | |
132 | } else if (bt_ctf_field_type(field) == CTF_TYPE_SEQUENCE) { | |
133 | struct definition_sequence *sequence_definition; | |
134 | sequence_definition = container_of(field, | |
135 | struct definition_sequence, p); | |
136 | ret = sequence_index(sequence_definition, index); | |
137 | } | |
138 | return ret; | |
139 | } | |
140 | ||
04ae3991 | 141 | const char *bt_ctf_event_name(const struct bt_ctf_event *event) |
9843982d JD |
142 | { |
143 | struct ctf_event *event_class; | |
144 | struct ctf_stream_class *stream_class; | |
145 | ||
146 | if (!event) | |
147 | return NULL; | |
148 | stream_class = event->stream->stream_class; | |
149 | event_class = g_ptr_array_index(stream_class->events_by_id, | |
150 | event->stream->event_id); | |
151 | return g_quark_to_string(event_class->name); | |
152 | } | |
153 | ||
154 | const char *bt_ctf_field_name(const struct definition *def) | |
155 | { | |
156 | if (def) | |
300d317a | 157 | return rem_(g_quark_to_string(def->name)); |
9843982d JD |
158 | return NULL; |
159 | } | |
160 | ||
da320b83 | 161 | enum ctf_type_id bt_ctf_field_type(const struct definition *def) |
9843982d JD |
162 | { |
163 | if (def) | |
164 | return def->declaration->id; | |
165 | return CTF_TYPE_UNKNOWN; | |
166 | } | |
167 | ||
04ae3991 MD |
168 | int bt_ctf_get_field_list(const struct bt_ctf_event *event, |
169 | const struct definition *scope, | |
9843982d JD |
170 | struct definition const * const **list, |
171 | unsigned int *count) | |
172 | { | |
173 | switch (bt_ctf_field_type(scope)) { | |
174 | case CTF_TYPE_INTEGER: | |
175 | case CTF_TYPE_FLOAT: | |
176 | case CTF_TYPE_STRING: | |
177 | case CTF_TYPE_ENUM: | |
178 | goto error; | |
179 | case CTF_TYPE_STRUCT: | |
180 | { | |
04ae3991 | 181 | const struct definition_struct *def_struct; |
9843982d | 182 | |
04ae3991 | 183 | def_struct = container_of(scope, const struct definition_struct, p); |
9843982d JD |
184 | if (!def_struct) |
185 | goto error; | |
186 | if (def_struct->fields->pdata) { | |
187 | *list = (struct definition const* const*) def_struct->fields->pdata; | |
188 | *count = def_struct->fields->len; | |
189 | goto end; | |
190 | } else { | |
191 | goto error; | |
192 | } | |
193 | } | |
194 | case CTF_TYPE_UNTAGGED_VARIANT: | |
195 | goto error; | |
196 | case CTF_TYPE_VARIANT: | |
197 | { | |
04ae3991 | 198 | const struct definition_variant *def_variant; |
9843982d | 199 | |
04ae3991 | 200 | def_variant = container_of(scope, const struct definition_variant, p); |
9843982d JD |
201 | if (!def_variant) |
202 | goto error; | |
203 | if (def_variant->fields->pdata) { | |
204 | *list = (struct definition const* const*) def_variant->fields->pdata; | |
205 | *count = def_variant->fields->len; | |
206 | goto end; | |
207 | } else { | |
208 | goto error; | |
209 | } | |
210 | } | |
211 | case CTF_TYPE_ARRAY: | |
212 | { | |
04ae3991 | 213 | const struct definition_array *def_array; |
9843982d | 214 | |
04ae3991 | 215 | def_array = container_of(scope, const struct definition_array, p); |
9843982d JD |
216 | if (!def_array) |
217 | goto error; | |
218 | if (def_array->elems->pdata) { | |
219 | *list = (struct definition const* const*) def_array->elems->pdata; | |
220 | *count = def_array->elems->len; | |
221 | goto end; | |
222 | } else { | |
223 | goto error; | |
224 | } | |
225 | } | |
226 | case CTF_TYPE_SEQUENCE: | |
227 | { | |
04ae3991 | 228 | const struct definition_sequence *def_sequence; |
9843982d | 229 | |
04ae3991 | 230 | def_sequence = container_of(scope, const struct definition_sequence, p); |
9843982d JD |
231 | if (!def_sequence) |
232 | goto error; | |
233 | if (def_sequence->elems->pdata) { | |
234 | *list = (struct definition const* const*) def_sequence->elems->pdata; | |
235 | *count = def_sequence->elems->len; | |
236 | goto end; | |
237 | } else { | |
238 | goto error; | |
239 | } | |
240 | } | |
241 | default: | |
242 | break; | |
243 | } | |
244 | ||
245 | end: | |
246 | return 0; | |
247 | ||
248 | error: | |
249 | *list = NULL; | |
250 | *count = 0; | |
251 | return -1; | |
252 | } | |
253 | ||
98a04903 JD |
254 | struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event) |
255 | { | |
256 | struct bt_context *ret = NULL; | |
257 | struct ctf_file_stream *cfs; | |
258 | struct ctf_stream *stream; | |
259 | struct ctf_stream_class *stream_class; | |
260 | struct ctf_trace *trace; | |
261 | ||
262 | cfs = container_of(event->stream, struct ctf_file_stream, | |
263 | parent); | |
264 | stream = &cfs->parent; | |
265 | stream_class = stream->stream_class; | |
266 | trace = stream_class->trace; | |
267 | ||
268 | if (trace->ctx) | |
269 | ret = trace->ctx; | |
270 | ||
271 | return ret; | |
272 | } | |
273 | ||
274 | int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event) | |
275 | { | |
276 | int ret = -1; | |
277 | struct ctf_file_stream *cfs; | |
278 | struct ctf_stream *stream; | |
279 | struct ctf_stream_class *stream_class; | |
280 | struct ctf_trace *trace; | |
281 | ||
282 | cfs = container_of(event->stream, struct ctf_file_stream, | |
283 | parent); | |
284 | stream = &cfs->parent; | |
285 | stream_class = stream->stream_class; | |
286 | trace = stream_class->trace; | |
287 | ||
288 | if (trace->handle) | |
289 | ret = trace->handle->id; | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
04ae3991 | 294 | uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event *event) |
9843982d JD |
295 | { |
296 | if (event && event->stream->has_timestamp) | |
c34ea0fa MD |
297 | return ctf_get_timestamp_raw(event->stream, |
298 | event->stream->timestamp); | |
9843982d | 299 | else |
57f3005e SJD |
300 | return -1ULL; |
301 | } | |
302 | ||
04ae3991 | 303 | uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *event) |
57f3005e | 304 | { |
c34ea0fa MD |
305 | if (event && event->stream->has_timestamp) |
306 | return ctf_get_timestamp(event->stream, | |
307 | event->stream->timestamp); | |
308 | else | |
57f3005e | 309 | return -1ULL; |
9843982d JD |
310 | } |
311 | ||
312 | static void bt_ctf_field_set_error(int error) | |
313 | { | |
314 | bt_ctf_last_field_error = error; | |
315 | } | |
316 | ||
317 | int bt_ctf_field_get_error(void) | |
318 | { | |
319 | int ret; | |
320 | ret = bt_ctf_last_field_error; | |
321 | bt_ctf_last_field_error = 0; | |
322 | ||
323 | return ret; | |
324 | } | |
325 | ||
8673030f JD |
326 | int bt_ctf_get_int_signedness(const struct definition *field) |
327 | { | |
328 | int ret; | |
329 | ||
330 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { | |
331 | ret = get_int_signedness(field); | |
332 | } else { | |
333 | ret = -1; | |
334 | bt_ctf_field_set_error(-EINVAL); | |
335 | } | |
336 | ||
337 | return ret; | |
338 | } | |
339 | ||
340 | int bt_ctf_get_int_base(const struct definition *field) | |
341 | { | |
342 | int ret; | |
343 | ||
344 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { | |
345 | ret = get_int_base(field); | |
346 | } else { | |
347 | ret = -1; | |
348 | bt_ctf_field_set_error(-EINVAL); | |
349 | } | |
350 | ||
351 | return ret; | |
352 | } | |
353 | ||
354 | int bt_ctf_get_int_byte_order(const struct definition *field) | |
355 | { | |
356 | int ret; | |
357 | ||
358 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { | |
359 | ret = get_int_byte_order(field); | |
360 | } else { | |
361 | ret = -1; | |
362 | bt_ctf_field_set_error(-EINVAL); | |
363 | } | |
364 | ||
365 | return ret; | |
366 | } | |
367 | ||
368 | enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field) | |
369 | { | |
370 | enum ctf_string_encoding ret = 0; | |
371 | ||
372 | if (!field) | |
373 | goto end; | |
374 | ||
375 | if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER) | |
376 | ret = get_int_encoding(field); | |
377 | else if (bt_ctf_field_type(field) == CTF_TYPE_STRING) | |
378 | ret = get_string_encoding(field); | |
379 | else | |
380 | goto error; | |
381 | ||
382 | end: | |
383 | return ret; | |
384 | ||
385 | error: | |
386 | bt_ctf_field_set_error(-EINVAL); | |
387 | return -1; | |
388 | } | |
389 | ||
390 | int bt_ctf_get_array_len(const struct definition *field) | |
391 | { | |
392 | int ret; | |
393 | ||
394 | if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) { | |
395 | ret = get_array_len(field); | |
396 | } else { | |
397 | ret = -1; | |
398 | bt_ctf_field_set_error(-EINVAL); | |
399 | } | |
400 | ||
401 | return ret; | |
402 | } | |
403 | ||
da320b83 | 404 | uint64_t bt_ctf_get_uint64(const struct definition *field) |
9843982d JD |
405 | { |
406 | unsigned int ret = 0; | |
407 | ||
408 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) | |
409 | ret = get_unsigned_int(field); | |
410 | else | |
411 | bt_ctf_field_set_error(-EINVAL); | |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
da320b83 | 416 | int64_t bt_ctf_get_int64(const struct definition *field) |
9843982d JD |
417 | { |
418 | int ret = 0; | |
419 | ||
420 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) | |
421 | ret = get_signed_int(field); | |
422 | else | |
423 | bt_ctf_field_set_error(-EINVAL); | |
424 | ||
425 | return ret; | |
426 | ||
427 | } | |
428 | ||
da320b83 | 429 | char *bt_ctf_get_char_array(const struct definition *field) |
9843982d JD |
430 | { |
431 | char *ret = NULL; | |
432 | ||
433 | if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) | |
434 | ret = get_char_array(field)->str; | |
435 | else | |
436 | bt_ctf_field_set_error(-EINVAL); | |
437 | ||
438 | return ret; | |
439 | } | |
440 | ||
da320b83 | 441 | char *bt_ctf_get_string(const struct definition *field) |
9843982d JD |
442 | { |
443 | char *ret = NULL; | |
444 | ||
445 | if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING) | |
446 | ret = get_string(field); | |
447 | else | |
448 | bt_ctf_field_set_error(-EINVAL); | |
449 | ||
450 | return ret; | |
451 | } |