API fix : fill the values for timestamp begin and end
[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
c50d2a7a 41const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
9843982d
JD
42 enum bt_ctf_scope scope)
43{
44 struct definition *tmp = NULL;
c50d2a7a 45 struct ctf_event_definition *event = ctf_event->parent;
9843982d
JD
46
47 switch (scope) {
48 case BT_TRACE_PACKET_HEADER:
49 if (!event->stream)
50 goto error;
51 if (event->stream->trace_packet_header)
52 tmp = &event->stream->trace_packet_header->p;
53 break;
54 case BT_STREAM_PACKET_CONTEXT:
55 if (!event->stream)
56 goto error;
57 if (event->stream->stream_packet_context)
58 tmp = &event->stream->stream_packet_context->p;
59 break;
60 case BT_STREAM_EVENT_HEADER:
61 if (!event->stream)
62 goto error;
63 if (event->stream->stream_event_header)
64 tmp = &event->stream->stream_event_header->p;
65 break;
66 case BT_STREAM_EVENT_CONTEXT:
67 if (!event->stream)
68 goto error;
69 if (event->stream->stream_event_context)
70 tmp = &event->stream->stream_event_context->p;
71 break;
72 case BT_EVENT_CONTEXT:
8a4722b0
JD
73 if (event->event_context)
74 tmp = &event->event_context->p;
9843982d
JD
75 break;
76 case BT_EVENT_FIELDS:
8a4722b0
JD
77 if (event->event_fields)
78 tmp = &event->event_fields->p;
9843982d
JD
79 break;
80 }
81 return tmp;
82
83error:
84 return NULL;
85}
86
c50d2a7a 87const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
04ae3991 88 const struct definition *scope,
9843982d
JD
89 const char *field)
90{
91 struct definition *def;
300d317a 92 char *field_underscore;
9843982d
JD
93
94 if (scope) {
95 def = lookup_definition(scope, field);
300d317a
JD
96 /*
97 * optionally a field can have an underscore prefix, try
98 * to lookup the field with this prefix if it failed
99 */
100 if (!def) {
101 field_underscore = g_new(char, strlen(field) + 2);
102 field_underscore[0] = '_';
103 strcpy(&field_underscore[1], field);
104 def = lookup_definition(scope, field_underscore);
105 g_free(field_underscore);
106 }
9843982d
JD
107 if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) {
108 struct definition_variant *variant_definition;
109 variant_definition = container_of(def,
110 struct definition_variant, p);
111 return variant_definition->current_field;
112 }
113 return def;
114 }
115 return NULL;
116}
117
c50d2a7a 118const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
04ae3991 119 const struct definition *field,
9843982d
JD
120 unsigned int index)
121{
122 struct definition *ret = NULL;
123
124 if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
125 struct definition_array *array_definition;
126 array_definition = container_of(field,
127 struct definition_array, p);
128 ret = array_index(array_definition, index);
129 } else if (bt_ctf_field_type(field) == CTF_TYPE_SEQUENCE) {
130 struct definition_sequence *sequence_definition;
131 sequence_definition = container_of(field,
132 struct definition_sequence, p);
133 ret = sequence_index(sequence_definition, index);
134 }
135 return ret;
136}
137
c50d2a7a 138const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
9843982d 139{
4716614a 140 struct ctf_event_declaration *event_class;
f380e105 141 struct ctf_stream_declaration *stream_class;
c50d2a7a 142 struct ctf_event_definition *event = ctf_event->parent;
9843982d
JD
143
144 if (!event)
145 return NULL;
146 stream_class = event->stream->stream_class;
147 event_class = g_ptr_array_index(stream_class->events_by_id,
148 event->stream->event_id);
149 return g_quark_to_string(event_class->name);
150}
151
152const char *bt_ctf_field_name(const struct definition *def)
153{
154 if (def)
300d317a 155 return rem_(g_quark_to_string(def->name));
9843982d
JD
156 return NULL;
157}
158
da320b83 159enum ctf_type_id bt_ctf_field_type(const struct definition *def)
9843982d
JD
160{
161 if (def)
162 return def->declaration->id;
163 return CTF_TYPE_UNKNOWN;
164}
165
c50d2a7a 166int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
04ae3991 167 const struct definition *scope,
9843982d
JD
168 struct definition const * const **list,
169 unsigned int *count)
170{
171 switch (bt_ctf_field_type(scope)) {
172 case CTF_TYPE_INTEGER:
173 case CTF_TYPE_FLOAT:
174 case CTF_TYPE_STRING:
175 case CTF_TYPE_ENUM:
176 goto error;
177 case CTF_TYPE_STRUCT:
178 {
04ae3991 179 const struct definition_struct *def_struct;
9843982d 180
04ae3991 181 def_struct = container_of(scope, const struct definition_struct, p);
9843982d
JD
182 if (!def_struct)
183 goto error;
184 if (def_struct->fields->pdata) {
185 *list = (struct definition const* const*) def_struct->fields->pdata;
186 *count = def_struct->fields->len;
187 goto end;
188 } else {
189 goto error;
190 }
191 }
192 case CTF_TYPE_UNTAGGED_VARIANT:
193 goto error;
194 case CTF_TYPE_VARIANT:
195 {
04ae3991 196 const struct definition_variant *def_variant;
9843982d 197
04ae3991 198 def_variant = container_of(scope, const struct definition_variant, p);
9843982d
JD
199 if (!def_variant)
200 goto error;
201 if (def_variant->fields->pdata) {
202 *list = (struct definition const* const*) def_variant->fields->pdata;
203 *count = def_variant->fields->len;
204 goto end;
205 } else {
206 goto error;
207 }
208 }
209 case CTF_TYPE_ARRAY:
210 {
04ae3991 211 const struct definition_array *def_array;
9843982d 212
04ae3991 213 def_array = container_of(scope, const struct definition_array, p);
9843982d
JD
214 if (!def_array)
215 goto error;
216 if (def_array->elems->pdata) {
217 *list = (struct definition const* const*) def_array->elems->pdata;
218 *count = def_array->elems->len;
219 goto end;
220 } else {
221 goto error;
222 }
223 }
224 case CTF_TYPE_SEQUENCE:
225 {
04ae3991 226 const struct definition_sequence *def_sequence;
9843982d 227
04ae3991 228 def_sequence = container_of(scope, const struct definition_sequence, p);
9843982d
JD
229 if (!def_sequence)
230 goto error;
231 if (def_sequence->elems->pdata) {
232 *list = (struct definition const* const*) def_sequence->elems->pdata;
233 *count = def_sequence->elems->len;
234 goto end;
235 } else {
236 goto error;
237 }
238 }
239 default:
240 break;
241 }
242
243end:
244 return 0;
245
246error:
247 *list = NULL;
248 *count = 0;
249 return -1;
250}
251
c50d2a7a 252struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
98a04903
JD
253{
254 struct bt_context *ret = NULL;
255 struct ctf_file_stream *cfs;
98a04903 256 struct ctf_trace *trace;
c50d2a7a 257 struct ctf_event_definition *event = ctf_event->parent;
98a04903
JD
258
259 cfs = container_of(event->stream, struct ctf_file_stream,
260 parent);
8b8dc96e 261 trace = cfs->parent.stream_class->trace;
98a04903
JD
262 if (trace->ctx)
263 ret = trace->ctx;
264
265 return ret;
266}
267
c50d2a7a 268int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
98a04903
JD
269{
270 int ret = -1;
271 struct ctf_file_stream *cfs;
98a04903 272 struct ctf_trace *trace;
c50d2a7a 273 struct ctf_event_definition *event = ctf_event->parent;
98a04903
JD
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
c50d2a7a 284uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event *ctf_event)
9843982d 285{
c50d2a7a 286 struct ctf_event_definition *event = ctf_event->parent;
9843982d 287 if (event && event->stream->has_timestamp)
c34ea0fa
MD
288 return ctf_get_timestamp_raw(event->stream,
289 event->stream->timestamp);
9843982d 290 else
57f3005e
SJD
291 return -1ULL;
292}
293
c50d2a7a 294uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
57f3005e 295{
c50d2a7a 296 struct ctf_event_definition *event = ctf_event->parent;
c34ea0fa
MD
297 if (event && event->stream->has_timestamp)
298 return ctf_get_timestamp(event->stream,
299 event->stream->timestamp);
300 else
57f3005e 301 return -1ULL;
9843982d
JD
302}
303
304static void bt_ctf_field_set_error(int error)
305{
306 bt_ctf_last_field_error = error;
307}
308
309int bt_ctf_field_get_error(void)
310{
311 int ret;
312 ret = bt_ctf_last_field_error;
313 bt_ctf_last_field_error = 0;
314
315 return ret;
316}
317
8673030f
JD
318int bt_ctf_get_int_signedness(const struct definition *field)
319{
320 int ret;
321
322 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
323 ret = get_int_signedness(field);
324 } else {
325 ret = -1;
326 bt_ctf_field_set_error(-EINVAL);
327 }
328
329 return ret;
330}
331
332int bt_ctf_get_int_base(const struct definition *field)
333{
334 int ret;
335
336 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
337 ret = get_int_base(field);
338 } else {
339 ret = -1;
340 bt_ctf_field_set_error(-EINVAL);
341 }
342
343 return ret;
344}
345
346int bt_ctf_get_int_byte_order(const struct definition *field)
347{
348 int ret;
349
350 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
351 ret = get_int_byte_order(field);
352 } else {
353 ret = -1;
354 bt_ctf_field_set_error(-EINVAL);
355 }
356
357 return ret;
358}
359
360enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
361{
362 enum ctf_string_encoding ret = 0;
363
364 if (!field)
365 goto end;
366
367 if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
368 ret = get_int_encoding(field);
369 else if (bt_ctf_field_type(field) == CTF_TYPE_STRING)
370 ret = get_string_encoding(field);
371 else
372 goto error;
373
374end:
375 return ret;
376
377error:
378 bt_ctf_field_set_error(-EINVAL);
379 return -1;
380}
381
382int bt_ctf_get_array_len(const struct definition *field)
383{
384 int ret;
385
386 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
387 ret = get_array_len(field);
388 } else {
389 ret = -1;
390 bt_ctf_field_set_error(-EINVAL);
391 }
392
393 return ret;
394}
395
da320b83 396uint64_t bt_ctf_get_uint64(const struct definition *field)
9843982d
JD
397{
398 unsigned int ret = 0;
399
400 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
401 ret = get_unsigned_int(field);
402 else
403 bt_ctf_field_set_error(-EINVAL);
404
405 return ret;
406}
407
da320b83 408int64_t bt_ctf_get_int64(const struct definition *field)
9843982d
JD
409{
410 int ret = 0;
411
412 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
413 ret = get_signed_int(field);
414 else
415 bt_ctf_field_set_error(-EINVAL);
416
417 return ret;
418
419}
420
da320b83 421char *bt_ctf_get_char_array(const struct definition *field)
9843982d
JD
422{
423 char *ret = NULL;
424
425 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY)
426 ret = get_char_array(field)->str;
427 else
428 bt_ctf_field_set_error(-EINVAL);
429
430 return ret;
431}
432
da320b83 433char *bt_ctf_get_string(const struct definition *field)
9843982d
JD
434{
435 char *ret = NULL;
436
437 if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING)
438 ret = get_string(field);
439 else
440 bt_ctf_field_set_error(-EINVAL);
441
442 return ret;
443}
e003ab50
JD
444
445int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
446 struct bt_ctf_event_decl const * const **list,
447 unsigned int *count)
448{
449 struct bt_trace_handle *handle;
450 struct trace_descriptor *td;
451 struct ctf_trace *tin;
452
453 if (!ctx)
454 goto error;
455
456 handle = g_hash_table_lookup(ctx->trace_handles,
457 (gpointer) (unsigned long) handle_id);
458 if (!handle)
459 goto error;
460
461 td = handle->td;
462 tin = container_of(td, struct ctf_trace, parent);
463
464 *list = (struct bt_ctf_event_decl const* const*) tin->event_declarations->pdata;
465 *count = tin->event_declarations->len;
466 return 0;
467
468error:
469 return -1;
470}
471
472const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
473{
474 if (!event)
475 return NULL;
476 return g_quark_to_string(event->parent.name);
477}
This page took 0.066505 seconds and 4 git commands to generate.