add ctf_event_definition pointer to ctf_stream_definition
[babeltrace.git] / formats / ctf / events.c
CommitLineData
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 41const 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
86error:
87 return NULL;
88}
89
04ae3991
MD
90const 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
121const 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 141const char *bt_ctf_event_name(const struct bt_ctf_event *event)
9843982d 142{
4716614a 143 struct ctf_event_declaration *event_class;
f380e105 144 struct ctf_stream_declaration *stream_class;
9843982d
JD
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
154const 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 161enum 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
168int 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
245end:
246 return 0;
247
248error:
249 *list = NULL;
250 *count = 0;
251 return -1;
252}
253
98a04903
JD
254struct 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;
98a04903
JD
258 struct ctf_trace *trace;
259
260 cfs = container_of(event->stream, struct ctf_file_stream,
261 parent);
8b8dc96e 262 trace = cfs->parent.stream_class->trace;
98a04903
JD
263 if (trace->ctx)
264 ret = trace->ctx;
265
266 return ret;
267}
268
269int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event)
270{
271 int ret = -1;
272 struct ctf_file_stream *cfs;
98a04903
JD
273 struct ctf_trace *trace;
274
275 cfs = container_of(event->stream, struct ctf_file_stream,
276 parent);
8b8dc96e 277 trace = cfs->parent.stream_class->trace;
98a04903
JD
278 if (trace->handle)
279 ret = trace->handle->id;
280
281 return ret;
282}
283
04ae3991 284uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event *event)
9843982d
JD
285{
286 if (event && event->stream->has_timestamp)
c34ea0fa
MD
287 return ctf_get_timestamp_raw(event->stream,
288 event->stream->timestamp);
9843982d 289 else
57f3005e
SJD
290 return -1ULL;
291}
292
04ae3991 293uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *event)
57f3005e 294{
c34ea0fa
MD
295 if (event && event->stream->has_timestamp)
296 return ctf_get_timestamp(event->stream,
297 event->stream->timestamp);
298 else
57f3005e 299 return -1ULL;
9843982d
JD
300}
301
302static void bt_ctf_field_set_error(int error)
303{
304 bt_ctf_last_field_error = error;
305}
306
307int bt_ctf_field_get_error(void)
308{
309 int ret;
310 ret = bt_ctf_last_field_error;
311 bt_ctf_last_field_error = 0;
312
313 return ret;
314}
315
8673030f
JD
316int bt_ctf_get_int_signedness(const struct definition *field)
317{
318 int ret;
319
320 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
321 ret = get_int_signedness(field);
322 } else {
323 ret = -1;
324 bt_ctf_field_set_error(-EINVAL);
325 }
326
327 return ret;
328}
329
330int bt_ctf_get_int_base(const struct definition *field)
331{
332 int ret;
333
334 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
335 ret = get_int_base(field);
336 } else {
337 ret = -1;
338 bt_ctf_field_set_error(-EINVAL);
339 }
340
341 return ret;
342}
343
344int bt_ctf_get_int_byte_order(const struct definition *field)
345{
346 int ret;
347
348 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
349 ret = get_int_byte_order(field);
350 } else {
351 ret = -1;
352 bt_ctf_field_set_error(-EINVAL);
353 }
354
355 return ret;
356}
357
358enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
359{
360 enum ctf_string_encoding ret = 0;
361
362 if (!field)
363 goto end;
364
365 if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
366 ret = get_int_encoding(field);
367 else if (bt_ctf_field_type(field) == CTF_TYPE_STRING)
368 ret = get_string_encoding(field);
369 else
370 goto error;
371
372end:
373 return ret;
374
375error:
376 bt_ctf_field_set_error(-EINVAL);
377 return -1;
378}
379
380int bt_ctf_get_array_len(const struct definition *field)
381{
382 int ret;
383
384 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
385 ret = get_array_len(field);
386 } else {
387 ret = -1;
388 bt_ctf_field_set_error(-EINVAL);
389 }
390
391 return ret;
392}
393
da320b83 394uint64_t bt_ctf_get_uint64(const struct definition *field)
9843982d
JD
395{
396 unsigned int ret = 0;
397
398 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
399 ret = get_unsigned_int(field);
400 else
401 bt_ctf_field_set_error(-EINVAL);
402
403 return ret;
404}
405
da320b83 406int64_t bt_ctf_get_int64(const struct definition *field)
9843982d
JD
407{
408 int ret = 0;
409
410 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
411 ret = get_signed_int(field);
412 else
413 bt_ctf_field_set_error(-EINVAL);
414
415 return ret;
416
417}
418
da320b83 419char *bt_ctf_get_char_array(const struct definition *field)
9843982d
JD
420{
421 char *ret = NULL;
422
423 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY)
424 ret = get_char_array(field)->str;
425 else
426 bt_ctf_field_set_error(-EINVAL);
427
428 return ret;
429}
430
da320b83 431char *bt_ctf_get_string(const struct definition *field)
9843982d
JD
432{
433 char *ret = NULL;
434
435 if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING)
436 ret = get_string(field);
437 else
438 bt_ctf_field_set_error(-EINVAL);
439
440 return ret;
441}
This page took 0.042046 seconds and 4 git commands to generate.