Cleanup: update ifdef wrapper name
[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
b330165c 37 * bt_ctf_field_get_error only
9843982d
JD
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;
7f89ddce 45 struct ctf_event_definition *event;
9843982d 46
7f89ddce
MD
47 if (!ctf_event)
48 return NULL;
49
50 event = ctf_event->parent;
9843982d
JD
51 switch (scope) {
52 case BT_TRACE_PACKET_HEADER:
53 if (!event->stream)
54 goto error;
55 if (event->stream->trace_packet_header)
56 tmp = &event->stream->trace_packet_header->p;
57 break;
58 case BT_STREAM_PACKET_CONTEXT:
59 if (!event->stream)
60 goto error;
61 if (event->stream->stream_packet_context)
62 tmp = &event->stream->stream_packet_context->p;
63 break;
64 case BT_STREAM_EVENT_HEADER:
65 if (!event->stream)
66 goto error;
67 if (event->stream->stream_event_header)
68 tmp = &event->stream->stream_event_header->p;
69 break;
70 case BT_STREAM_EVENT_CONTEXT:
71 if (!event->stream)
72 goto error;
73 if (event->stream->stream_event_context)
74 tmp = &event->stream->stream_event_context->p;
75 break;
76 case BT_EVENT_CONTEXT:
8a4722b0
JD
77 if (event->event_context)
78 tmp = &event->event_context->p;
9843982d
JD
79 break;
80 case BT_EVENT_FIELDS:
8a4722b0
JD
81 if (event->event_fields)
82 tmp = &event->event_fields->p;
9843982d
JD
83 break;
84 }
85 return tmp;
86
87error:
88 return NULL;
89}
90
c50d2a7a 91const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
04ae3991 92 const struct definition *scope,
9843982d
JD
93 const char *field)
94{
95 struct definition *def;
300d317a 96 char *field_underscore;
9843982d 97
7f89ddce
MD
98 if (!ctf_event || !scope || !field)
99 return NULL;
100
101 def = lookup_definition(scope, field);
102 /*
103 * optionally a field can have an underscore prefix, try
104 * to lookup the field with this prefix if it failed
105 */
106 if (!def) {
107 field_underscore = g_new(char, strlen(field) + 2);
108 field_underscore[0] = '_';
109 strcpy(&field_underscore[1], field);
110 def = lookup_definition(scope, field_underscore);
111 g_free(field_underscore);
9843982d 112 }
7f89ddce
MD
113 if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) {
114 struct definition_variant *variant_definition;
115 variant_definition = container_of(def,
116 struct definition_variant, p);
117 return variant_definition->current_field;
118 }
119 return def;
9843982d
JD
120}
121
c50d2a7a 122const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
04ae3991 123 const struct definition *field,
9843982d
JD
124 unsigned int index)
125{
126 struct definition *ret = NULL;
127
7f89ddce
MD
128 if (!ctf_event || !field)
129 return NULL;
130
9843982d
JD
131 if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
132 struct definition_array *array_definition;
133 array_definition = container_of(field,
134 struct definition_array, p);
135 ret = array_index(array_definition, index);
136 } else if (bt_ctf_field_type(field) == CTF_TYPE_SEQUENCE) {
137 struct definition_sequence *sequence_definition;
138 sequence_definition = container_of(field,
139 struct definition_sequence, p);
140 ret = sequence_index(sequence_definition, index);
141 }
142 return ret;
143}
144
c50d2a7a 145const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
9843982d 146{
4716614a 147 struct ctf_event_declaration *event_class;
f380e105 148 struct ctf_stream_declaration *stream_class;
7f89ddce 149 struct ctf_event_definition *event;
9843982d 150
7f89ddce 151 if (!ctf_event)
9843982d 152 return NULL;
7f89ddce
MD
153
154 event = ctf_event->parent;
9843982d
JD
155 stream_class = event->stream->stream_class;
156 event_class = g_ptr_array_index(stream_class->events_by_id,
157 event->stream->event_id);
158 return g_quark_to_string(event_class->name);
159}
160
161const char *bt_ctf_field_name(const struct definition *def)
162{
7f89ddce
MD
163 if (!def)
164 return NULL;
165
166 return rem_(g_quark_to_string(def->name));
9843982d
JD
167}
168
da320b83 169enum ctf_type_id bt_ctf_field_type(const struct definition *def)
9843982d 170{
7f89ddce
MD
171 if (!def)
172 return CTF_TYPE_UNKNOWN;
173
174 return def->declaration->id;
9843982d
JD
175}
176
c50d2a7a 177int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
04ae3991 178 const struct definition *scope,
9843982d
JD
179 struct definition const * const **list,
180 unsigned int *count)
181{
7f89ddce
MD
182 if (!ctf_event || !scope || !list || !count)
183 return -EINVAL;
184
9843982d
JD
185 switch (bt_ctf_field_type(scope)) {
186 case CTF_TYPE_INTEGER:
187 case CTF_TYPE_FLOAT:
188 case CTF_TYPE_STRING:
189 case CTF_TYPE_ENUM:
190 goto error;
191 case CTF_TYPE_STRUCT:
192 {
04ae3991 193 const struct definition_struct *def_struct;
9843982d 194
04ae3991 195 def_struct = container_of(scope, const struct definition_struct, p);
9843982d
JD
196 if (!def_struct)
197 goto error;
198 if (def_struct->fields->pdata) {
199 *list = (struct definition const* const*) def_struct->fields->pdata;
200 *count = def_struct->fields->len;
201 goto end;
202 } else {
203 goto error;
204 }
7f89ddce 205 break;
9843982d
JD
206 }
207 case CTF_TYPE_UNTAGGED_VARIANT:
208 goto error;
209 case CTF_TYPE_VARIANT:
210 {
04ae3991 211 const struct definition_variant *def_variant;
9843982d 212
04ae3991 213 def_variant = container_of(scope, const struct definition_variant, p);
9843982d
JD
214 if (!def_variant)
215 goto error;
216 if (def_variant->fields->pdata) {
217 *list = (struct definition const* const*) def_variant->fields->pdata;
218 *count = def_variant->fields->len;
219 goto end;
220 } else {
221 goto error;
222 }
7f89ddce 223 break;
9843982d
JD
224 }
225 case CTF_TYPE_ARRAY:
226 {
04ae3991 227 const struct definition_array *def_array;
9843982d 228
04ae3991 229 def_array = container_of(scope, const struct definition_array, p);
9843982d
JD
230 if (!def_array)
231 goto error;
232 if (def_array->elems->pdata) {
233 *list = (struct definition const* const*) def_array->elems->pdata;
234 *count = def_array->elems->len;
235 goto end;
236 } else {
237 goto error;
238 }
7f89ddce 239 break;
9843982d
JD
240 }
241 case CTF_TYPE_SEQUENCE:
242 {
04ae3991 243 const struct definition_sequence *def_sequence;
9843982d 244
04ae3991 245 def_sequence = container_of(scope, const struct definition_sequence, p);
9843982d
JD
246 if (!def_sequence)
247 goto error;
248 if (def_sequence->elems->pdata) {
249 *list = (struct definition const* const*) def_sequence->elems->pdata;
250 *count = def_sequence->elems->len;
251 goto end;
252 } else {
253 goto error;
254 }
7f89ddce 255 break;
9843982d
JD
256 }
257 default:
258 break;
259 }
260
261end:
262 return 0;
263
264error:
265 *list = NULL;
266 *count = 0;
267 return -1;
268}
269
c50d2a7a 270struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
98a04903
JD
271{
272 struct bt_context *ret = NULL;
273 struct ctf_file_stream *cfs;
98a04903 274 struct ctf_trace *trace;
7f89ddce 275 struct ctf_event_definition *event;
98a04903 276
7f89ddce
MD
277 if (!ctf_event)
278 return NULL;
279
280 event = ctf_event->parent;
98a04903
JD
281 cfs = container_of(event->stream, struct ctf_file_stream,
282 parent);
8b8dc96e 283 trace = cfs->parent.stream_class->trace;
98a04903
JD
284 if (trace->ctx)
285 ret = trace->ctx;
286
287 return ret;
288}
289
c50d2a7a 290int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
98a04903
JD
291{
292 int ret = -1;
293 struct ctf_file_stream *cfs;
98a04903 294 struct ctf_trace *trace;
7f89ddce
MD
295 struct ctf_event_definition *event;
296
297 if (!ctf_event)
298 return -EINVAL;
98a04903 299
7f89ddce 300 event = ctf_event->parent;
98a04903
JD
301 cfs = container_of(event->stream, struct ctf_file_stream,
302 parent);
8b8dc96e 303 trace = cfs->parent.stream_class->trace;
98a04903
JD
304 if (trace->handle)
305 ret = trace->handle->id;
306
307 return ret;
308}
309
d40ee8ec 310uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
9843982d 311{
7f89ddce
MD
312 struct ctf_event_definition *event;
313
314 if (!ctf_event)
315 return -1ULL;
316
317 event = ctf_event->parent;
9843982d 318 if (event && event->stream->has_timestamp)
03798a93 319 return event->stream->real_timestamp;
9843982d 320 else
57f3005e
SJD
321 return -1ULL;
322}
323
d40ee8ec 324uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
57f3005e 325{
7f89ddce
MD
326 struct ctf_event_definition *event;
327
328 if (!ctf_event)
329 return -1ULL;
330
331 event = ctf_event->parent;
c34ea0fa 332 if (event && event->stream->has_timestamp)
03798a93 333 return event->stream->cycles_timestamp;
c34ea0fa 334 else
57f3005e 335 return -1ULL;
9843982d
JD
336}
337
338static void bt_ctf_field_set_error(int error)
339{
340 bt_ctf_last_field_error = error;
341}
342
343int bt_ctf_field_get_error(void)
344{
345 int ret;
346 ret = bt_ctf_last_field_error;
347 bt_ctf_last_field_error = 0;
348
349 return ret;
350}
351
8673030f
JD
352int bt_ctf_get_int_signedness(const struct definition *field)
353{
354 int ret;
355
356 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
357 ret = get_int_signedness(field);
358 } else {
7f89ddce 359 ret = -EINVAL;
8673030f
JD
360 bt_ctf_field_set_error(-EINVAL);
361 }
362
363 return ret;
364}
365
366int bt_ctf_get_int_base(const struct definition *field)
367{
368 int ret;
369
370 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
371 ret = get_int_base(field);
372 } else {
7f89ddce 373 ret = -EINVAL;
8673030f
JD
374 bt_ctf_field_set_error(-EINVAL);
375 }
376
377 return ret;
378}
379
380int bt_ctf_get_int_byte_order(const struct definition *field)
381{
382 int ret;
383
384 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
385 ret = get_int_byte_order(field);
386 } else {
7f89ddce 387 ret = -EINVAL;
8673030f
JD
388 bt_ctf_field_set_error(-EINVAL);
389 }
390
391 return ret;
392}
393
fef0e521
MD
394ssize_t bt_ctf_get_int_len(const struct definition *field)
395{
396 ssize_t ret;
397
398 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
399 ret = (ssize_t) get_int_len(field);
400 } else {
7f89ddce 401 ret = -EINVAL;
fef0e521
MD
402 bt_ctf_field_set_error(-EINVAL);
403 }
404
405 return ret;
406}
407
8673030f
JD
408enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
409{
410 enum ctf_string_encoding ret = 0;
411
412 if (!field)
7f89ddce 413 goto error;
8673030f
JD
414
415 if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
416 ret = get_int_encoding(field);
417 else if (bt_ctf_field_type(field) == CTF_TYPE_STRING)
418 ret = get_string_encoding(field);
419 else
420 goto error;
8673030f
JD
421 return ret;
422
423error:
424 bt_ctf_field_set_error(-EINVAL);
425 return -1;
426}
427
428int bt_ctf_get_array_len(const struct definition *field)
429{
430 int ret;
431
432 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
433 ret = get_array_len(field);
434 } else {
435 ret = -1;
436 bt_ctf_field_set_error(-EINVAL);
437 }
438
439 return ret;
440}
441
da320b83 442uint64_t bt_ctf_get_uint64(const struct definition *field)
9843982d 443{
63dd3817 444 uint64_t ret = 0;
9843982d
JD
445
446 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
447 ret = get_unsigned_int(field);
448 else
449 bt_ctf_field_set_error(-EINVAL);
450
451 return ret;
452}
453
da320b83 454int64_t bt_ctf_get_int64(const struct definition *field)
9843982d 455{
63dd3817 456 int64_t ret = 0;
9843982d
JD
457
458 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
459 ret = get_signed_int(field);
460 else
461 bt_ctf_field_set_error(-EINVAL);
462
463 return ret;
464
465}
466
da320b83 467char *bt_ctf_get_char_array(const struct definition *field)
9843982d
JD
468{
469 char *ret = NULL;
470
471 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY)
472 ret = get_char_array(field)->str;
473 else
474 bt_ctf_field_set_error(-EINVAL);
475
476 return ret;
477}
478
da320b83 479char *bt_ctf_get_string(const struct definition *field)
9843982d
JD
480{
481 char *ret = NULL;
482
483 if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING)
484 ret = get_string(field);
485 else
486 bt_ctf_field_set_error(-EINVAL);
487
488 return ret;
489}
e003ab50
JD
490
491int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
64c2c249 492 struct bt_ctf_event_decl * const **list,
e003ab50
JD
493 unsigned int *count)
494{
495 struct bt_trace_handle *handle;
496 struct trace_descriptor *td;
497 struct ctf_trace *tin;
498
7f89ddce 499 if (!ctx || !list || !count)
e003ab50
JD
500 goto error;
501
502 handle = g_hash_table_lookup(ctx->trace_handles,
503 (gpointer) (unsigned long) handle_id);
504 if (!handle)
505 goto error;
506
507 td = handle->td;
508 tin = container_of(td, struct ctf_trace, parent);
509
64c2c249 510 *list = (struct bt_ctf_event_decl * const*) tin->event_declarations->pdata;
e003ab50
JD
511 *count = tin->event_declarations->len;
512 return 0;
513
514error:
515 return -1;
516}
517
518const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
519{
520 if (!event)
521 return NULL;
7f89ddce 522
e003ab50
JD
523 return g_quark_to_string(event->parent.name);
524}
64c2c249
JD
525
526int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl,
527 enum bt_ctf_scope scope,
528 struct bt_ctf_field_decl const * const **list,
529 unsigned int *count)
530{
531 int i;
532 GArray *fields = NULL;
533 gpointer *ret_list = NULL;
534 GPtrArray *fields_array = NULL;
535 int ret = 0;
536 *count = 0;
537
7f89ddce
MD
538 if (!event_decl || !list || !count)
539 return -EINVAL;
540
64c2c249
JD
541 switch (scope) {
542 case BT_EVENT_CONTEXT:
543 if (event_decl->context_decl) {
544 ret_list = event_decl->context_decl->pdata;
545 *count = event_decl->context_decl->len;
546 goto end;
547 }
548 event_decl->context_decl = g_ptr_array_new();
549 if (!event_decl->parent.context_decl) {
550 ret = -1;
551 goto end;
552 }
553 fields = event_decl->parent.context_decl->fields;
554 fields_array = event_decl->context_decl;
555 break;
556 case BT_EVENT_FIELDS:
557 if (event_decl->fields_decl) {
558 ret_list = event_decl->fields_decl->pdata;
559 *count = event_decl->fields_decl->len;
560 goto end;
561 }
562 event_decl->fields_decl = g_ptr_array_new();
563 if (!event_decl->parent.fields_decl) {
564 ret = -1;
565 goto end;
566 }
567 fields = event_decl->parent.fields_decl->fields;
568 fields_array = event_decl->fields_decl;
569 break;
570 case BT_STREAM_PACKET_CONTEXT:
571 if (event_decl->packet_context_decl) {
572 ret_list = event_decl->packet_context_decl->pdata;
573 *count = event_decl->packet_context_decl->len;
574 goto end;
575 }
576 event_decl->packet_context_decl = g_ptr_array_new();
577 if (!event_decl->parent.stream->packet_context_decl) {
578 ret = -1;
579 goto end;
580 }
581 fields = event_decl->parent.stream->packet_context_decl->fields;
582 fields_array = event_decl->packet_context_decl;
583 break;
584 case BT_STREAM_EVENT_CONTEXT:
585 if (event_decl->event_context_decl) {
586 ret_list = event_decl->event_context_decl->pdata;
587 *count = event_decl->event_context_decl->len;
588 goto end;
589 }
590 event_decl->event_context_decl = g_ptr_array_new();
591 if (!event_decl->parent.stream->event_context_decl) {
592 ret = -1;
593 goto end;
594 }
595 fields = event_decl->parent.stream->event_context_decl->fields;
596 fields_array = event_decl->event_context_decl;
597 break;
598 case BT_STREAM_EVENT_HEADER:
599 if (event_decl->event_header_decl) {
600 ret_list = event_decl->event_header_decl->pdata;
601 *count = event_decl->event_header_decl->len;
602 goto end;
603 }
604 event_decl->event_header_decl = g_ptr_array_new();
605 if (!event_decl->parent.stream->event_header_decl) {
606 ret = -1;
607 goto end;
608 }
609 fields = event_decl->parent.stream->event_header_decl->fields;
610 fields_array = event_decl->event_header_decl;
611 break;
612 case BT_TRACE_PACKET_HEADER:
613 if (event_decl->packet_header_decl) {
614 ret_list = event_decl->packet_header_decl->pdata;
615 *count = event_decl->packet_header_decl->len;
616 goto end;
617 }
618 event_decl->packet_header_decl = g_ptr_array_new();
619 if (!event_decl->parent.stream->trace->packet_header_decl) {
620 ret = -1;
621 goto end;
622 }
623 fields = event_decl->parent.stream->trace->packet_header_decl->fields;
624 fields_array = event_decl->packet_header_decl;
625 break;
626 }
627
628 for (i = 0; i < fields->len; i++) {
629 g_ptr_array_add(fields_array,
630 &g_array_index(fields,
631 struct declaration_field, i));
632 }
633 ret_list = fields_array->pdata;
634 *count = fields->len;
635
636end:
637 *list = (struct bt_ctf_field_decl const* const*) ret_list;
638
639 return ret;
640}
641
642const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
643{
7f89ddce
MD
644 if (!field)
645 return NULL;
646
647 return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
64c2c249 648}
This page took 0.05449 seconds and 4 git commands to generate.