Get rid of clock-raw and use real clock
[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
03798a93 284uint64_t bt_ctf_get_real_timestamp(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)
03798a93 288 return event->stream->real_timestamp;
9843982d 289 else
57f3005e
SJD
290 return -1ULL;
291}
292
03798a93 293uint64_t bt_ctf_get_cycles_timestamp(const struct bt_ctf_event *ctf_event)
57f3005e 294{
c50d2a7a 295 struct ctf_event_definition *event = ctf_event->parent;
c34ea0fa 296 if (event && event->stream->has_timestamp)
03798a93 297 return event->stream->cycles_timestamp;
c34ea0fa 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
fef0e521
MD
358ssize_t bt_ctf_get_int_len(const struct definition *field)
359{
360 ssize_t ret;
361
362 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
363 ret = (ssize_t) get_int_len(field);
364 } else {
365 ret = -1;
366 bt_ctf_field_set_error(-EINVAL);
367 }
368
369 return ret;
370}
371
8673030f
JD
372enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
373{
374 enum ctf_string_encoding ret = 0;
375
376 if (!field)
377 goto end;
378
379 if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
380 ret = get_int_encoding(field);
381 else if (bt_ctf_field_type(field) == CTF_TYPE_STRING)
382 ret = get_string_encoding(field);
383 else
384 goto error;
385
386end:
387 return ret;
388
389error:
390 bt_ctf_field_set_error(-EINVAL);
391 return -1;
392}
393
394int bt_ctf_get_array_len(const struct definition *field)
395{
396 int ret;
397
398 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
399 ret = get_array_len(field);
400 } else {
401 ret = -1;
402 bt_ctf_field_set_error(-EINVAL);
403 }
404
405 return ret;
406}
407
da320b83 408uint64_t bt_ctf_get_uint64(const struct definition *field)
9843982d
JD
409{
410 unsigned int ret = 0;
411
412 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
413 ret = get_unsigned_int(field);
414 else
415 bt_ctf_field_set_error(-EINVAL);
416
417 return ret;
418}
419
da320b83 420int64_t bt_ctf_get_int64(const struct definition *field)
9843982d
JD
421{
422 int ret = 0;
423
424 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
425 ret = get_signed_int(field);
426 else
427 bt_ctf_field_set_error(-EINVAL);
428
429 return ret;
430
431}
432
da320b83 433char *bt_ctf_get_char_array(const struct definition *field)
9843982d
JD
434{
435 char *ret = NULL;
436
437 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY)
438 ret = get_char_array(field)->str;
439 else
440 bt_ctf_field_set_error(-EINVAL);
441
442 return ret;
443}
444
da320b83 445char *bt_ctf_get_string(const struct definition *field)
9843982d
JD
446{
447 char *ret = NULL;
448
449 if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING)
450 ret = get_string(field);
451 else
452 bt_ctf_field_set_error(-EINVAL);
453
454 return ret;
455}
e003ab50
JD
456
457int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
64c2c249 458 struct bt_ctf_event_decl * const **list,
e003ab50
JD
459 unsigned int *count)
460{
461 struct bt_trace_handle *handle;
462 struct trace_descriptor *td;
463 struct ctf_trace *tin;
464
465 if (!ctx)
466 goto error;
467
468 handle = g_hash_table_lookup(ctx->trace_handles,
469 (gpointer) (unsigned long) handle_id);
470 if (!handle)
471 goto error;
472
473 td = handle->td;
474 tin = container_of(td, struct ctf_trace, parent);
475
64c2c249 476 *list = (struct bt_ctf_event_decl * const*) tin->event_declarations->pdata;
e003ab50
JD
477 *count = tin->event_declarations->len;
478 return 0;
479
480error:
481 return -1;
482}
483
484const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
485{
486 if (!event)
487 return NULL;
488 return g_quark_to_string(event->parent.name);
489}
64c2c249
JD
490
491int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl,
492 enum bt_ctf_scope scope,
493 struct bt_ctf_field_decl const * const **list,
494 unsigned int *count)
495{
496 int i;
497 GArray *fields = NULL;
498 gpointer *ret_list = NULL;
499 GPtrArray *fields_array = NULL;
500 int ret = 0;
501 *count = 0;
502
503 switch (scope) {
504 case BT_EVENT_CONTEXT:
505 if (event_decl->context_decl) {
506 ret_list = event_decl->context_decl->pdata;
507 *count = event_decl->context_decl->len;
508 goto end;
509 }
510 event_decl->context_decl = g_ptr_array_new();
511 if (!event_decl->parent.context_decl) {
512 ret = -1;
513 goto end;
514 }
515 fields = event_decl->parent.context_decl->fields;
516 fields_array = event_decl->context_decl;
517 break;
518 case BT_EVENT_FIELDS:
519 if (event_decl->fields_decl) {
520 ret_list = event_decl->fields_decl->pdata;
521 *count = event_decl->fields_decl->len;
522 goto end;
523 }
524 event_decl->fields_decl = g_ptr_array_new();
525 if (!event_decl->parent.fields_decl) {
526 ret = -1;
527 goto end;
528 }
529 fields = event_decl->parent.fields_decl->fields;
530 fields_array = event_decl->fields_decl;
531 break;
532 case BT_STREAM_PACKET_CONTEXT:
533 if (event_decl->packet_context_decl) {
534 ret_list = event_decl->packet_context_decl->pdata;
535 *count = event_decl->packet_context_decl->len;
536 goto end;
537 }
538 event_decl->packet_context_decl = g_ptr_array_new();
539 if (!event_decl->parent.stream->packet_context_decl) {
540 ret = -1;
541 goto end;
542 }
543 fields = event_decl->parent.stream->packet_context_decl->fields;
544 fields_array = event_decl->packet_context_decl;
545 break;
546 case BT_STREAM_EVENT_CONTEXT:
547 if (event_decl->event_context_decl) {
548 ret_list = event_decl->event_context_decl->pdata;
549 *count = event_decl->event_context_decl->len;
550 goto end;
551 }
552 event_decl->event_context_decl = g_ptr_array_new();
553 if (!event_decl->parent.stream->event_context_decl) {
554 ret = -1;
555 goto end;
556 }
557 fields = event_decl->parent.stream->event_context_decl->fields;
558 fields_array = event_decl->event_context_decl;
559 break;
560 case BT_STREAM_EVENT_HEADER:
561 if (event_decl->event_header_decl) {
562 ret_list = event_decl->event_header_decl->pdata;
563 *count = event_decl->event_header_decl->len;
564 goto end;
565 }
566 event_decl->event_header_decl = g_ptr_array_new();
567 if (!event_decl->parent.stream->event_header_decl) {
568 ret = -1;
569 goto end;
570 }
571 fields = event_decl->parent.stream->event_header_decl->fields;
572 fields_array = event_decl->event_header_decl;
573 break;
574 case BT_TRACE_PACKET_HEADER:
575 if (event_decl->packet_header_decl) {
576 ret_list = event_decl->packet_header_decl->pdata;
577 *count = event_decl->packet_header_decl->len;
578 goto end;
579 }
580 event_decl->packet_header_decl = g_ptr_array_new();
581 if (!event_decl->parent.stream->trace->packet_header_decl) {
582 ret = -1;
583 goto end;
584 }
585 fields = event_decl->parent.stream->trace->packet_header_decl->fields;
586 fields_array = event_decl->packet_header_decl;
587 break;
588 }
589
590 for (i = 0; i < fields->len; i++) {
591 g_ptr_array_add(fields_array,
592 &g_array_index(fields,
593 struct declaration_field, i));
594 }
595 ret_list = fields_array->pdata;
596 *count = fields->len;
597
598end:
599 *list = (struct bt_ctf_field_decl const* const*) ret_list;
600
601 return ret;
602}
603
604const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
605{
606 if (field)
607 return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
608 return NULL;
609}
This page took 0.047574 seconds and 4 git commands to generate.