b837b128393baf87905c81b28fc444cb2f434c42
[babeltrace.git] / formats / ctf / events.c
1 /*
2 * ctf/events.c
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>
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>
30 #include <glib.h>
31
32 #include "events-private.h"
33
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_get_error only
38 */
39 __thread int bt_ctf_last_field_error = 0;
40
41 const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
42 enum bt_ctf_scope scope)
43 {
44 const struct definition *tmp = NULL;
45 const struct ctf_event_definition *event;
46
47 if (!ctf_event)
48 return NULL;
49
50 event = ctf_event->parent;
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:
77 if (event->event_context)
78 tmp = &event->event_context->p;
79 break;
80 case BT_EVENT_FIELDS:
81 if (event->event_fields)
82 tmp = &event->event_fields->p;
83 break;
84 }
85 return tmp;
86
87 error:
88 return NULL;
89 }
90
91 const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
92 const struct definition *scope,
93 const char *field)
94 {
95 const struct definition *def;
96 char *field_underscore;
97
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);
112 }
113 if (bt_ctf_field_type(bt_ctf_get_decl_from_def(def)) == CTF_TYPE_VARIANT) {
114 const struct definition_variant *variant_definition;
115 variant_definition = container_of(def,
116 const struct definition_variant, p);
117 return variant_definition->current_field;
118 }
119 return def;
120 }
121
122 const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
123 const struct definition *field,
124 unsigned int index)
125 {
126 struct definition *ret = NULL;
127
128 if (!ctf_event || !field)
129 return NULL;
130
131 if (bt_ctf_field_type(bt_ctf_get_decl_from_def(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(bt_ctf_get_decl_from_def(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
145 const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
146 {
147 const struct ctf_event_declaration *event_class;
148 const struct ctf_stream_declaration *stream_class;
149 const struct ctf_event_definition *event;
150
151 if (!ctf_event)
152 return NULL;
153
154 event = ctf_event->parent;
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
161 const char *bt_ctf_field_name(const struct definition *def)
162 {
163 if (!def)
164 return NULL;
165
166 return rem_(g_quark_to_string(def->name));
167 }
168
169 enum ctf_type_id bt_ctf_field_type(const struct declaration *decl)
170 {
171 if (!decl)
172 return CTF_TYPE_UNKNOWN;
173
174 return decl->id;
175 }
176
177 int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
178 const struct definition *scope,
179 struct definition const * const **list,
180 unsigned int *count)
181 {
182 if (!ctf_event || !scope || !list || !count)
183 return -EINVAL;
184
185 switch (bt_ctf_field_type(bt_ctf_get_decl_from_def(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 {
193 const struct definition_struct *def_struct;
194
195 def_struct = container_of(scope, const struct definition_struct, p);
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 }
205 break;
206 }
207 case CTF_TYPE_UNTAGGED_VARIANT:
208 goto error;
209 case CTF_TYPE_VARIANT:
210 {
211 const struct definition_variant *def_variant;
212
213 def_variant = container_of(scope, const struct definition_variant, p);
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 }
223 break;
224 }
225 case CTF_TYPE_ARRAY:
226 {
227 const struct definition_array *def_array;
228
229 def_array = container_of(scope, const struct definition_array, p);
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 }
239 break;
240 }
241 case CTF_TYPE_SEQUENCE:
242 {
243 const struct definition_sequence *def_sequence;
244
245 def_sequence = container_of(scope, const struct definition_sequence, p);
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 }
255 break;
256 }
257 default:
258 break;
259 }
260
261 end:
262 return 0;
263
264 error:
265 *list = NULL;
266 *count = 0;
267 return -1;
268 }
269
270 struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
271 {
272 struct bt_context *ret = NULL;
273 const struct ctf_file_stream *cfs;
274 const struct ctf_trace *trace;
275 const struct ctf_event_definition *event;
276
277 if (!ctf_event)
278 return NULL;
279
280 event = ctf_event->parent;
281 cfs = container_of(event->stream, const struct ctf_file_stream,
282 parent);
283 trace = cfs->parent.stream_class->trace;
284 if (trace->ctx)
285 ret = trace->ctx;
286
287 return ret;
288 }
289
290 int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
291 {
292 int ret = -1;
293 const struct ctf_file_stream *cfs;
294 const struct ctf_trace *trace;
295 const struct ctf_event_definition *event;
296
297 if (!ctf_event)
298 return -EINVAL;
299
300 event = ctf_event->parent;
301 cfs = container_of(event->stream, const struct ctf_file_stream,
302 parent);
303 trace = cfs->parent.stream_class->trace;
304 if (trace->handle)
305 ret = trace->handle->id;
306
307 return ret;
308 }
309
310 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
311 {
312 const struct ctf_event_definition *event;
313
314 if (!ctf_event)
315 return -1ULL;
316
317 event = ctf_event->parent;
318 if (event && event->stream->has_timestamp)
319 return event->stream->real_timestamp;
320 else
321 return -1ULL;
322 }
323
324 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
325 {
326 const struct ctf_event_definition *event;
327
328 if (!ctf_event)
329 return -1ULL;
330
331 event = ctf_event->parent;
332 if (event && event->stream->has_timestamp)
333 return event->stream->cycles_timestamp;
334 else
335 return -1ULL;
336 }
337
338 static void bt_ctf_field_set_error(int error)
339 {
340 bt_ctf_last_field_error = error;
341 }
342
343 int 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
352 static const struct declaration_integer *
353 get_declaration_integer(const struct declaration *decl)
354 {
355 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_INTEGER)
356 return NULL;
357 return container_of(decl, const struct declaration_integer, p);
358 }
359
360 static const struct declaration_string *
361 get_declaration_string(const struct declaration *decl)
362 {
363 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_STRING)
364 return NULL;
365 return container_of(decl, const struct declaration_string, p);
366 }
367
368 static const struct declaration_array *
369 get_declaration_array(const struct declaration *decl)
370 {
371 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_ARRAY)
372 return NULL;
373 return container_of(decl, const struct declaration_array, p);
374 }
375
376 static const struct declaration_sequence *
377 get_declaration_sequence(const struct declaration *decl)
378 {
379 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_SEQUENCE)
380 return NULL;
381 return container_of(decl, const struct declaration_sequence, p);
382 }
383
384 int bt_ctf_get_int_signedness(const struct declaration *decl)
385 {
386 const struct declaration_integer *integer;
387
388 integer = get_declaration_integer(decl);
389 if (!integer) {
390 bt_ctf_field_set_error(-EINVAL);
391 return -EINVAL;
392 }
393 return integer->signedness;
394 }
395
396 int bt_ctf_get_int_base(const struct declaration *decl)
397 {
398 const struct declaration_integer *integer;
399
400 integer = get_declaration_integer(decl);
401 if (!integer) {
402 bt_ctf_field_set_error(-EINVAL);
403 return -EINVAL;
404 }
405 return integer->base;
406 }
407
408 int bt_ctf_get_int_byte_order(const struct declaration *decl)
409 {
410 const struct declaration_integer *integer;
411
412 integer = get_declaration_integer(decl);
413 if (!integer) {
414 bt_ctf_field_set_error(-EINVAL);
415 return -EINVAL;
416 }
417 return integer->byte_order;
418 }
419
420 ssize_t bt_ctf_get_int_len(const struct declaration *decl)
421 {
422 const struct declaration_integer *integer;
423
424 integer = get_declaration_integer(decl);
425 if (!integer) {
426 bt_ctf_field_set_error(-EINVAL);
427 return -EINVAL;
428 }
429 return (ssize_t) integer->len;
430 }
431
432 const struct definition *bt_ctf_get_enum_int(const struct definition *field)
433 {
434 const struct definition_enum *def_enum;
435
436 if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
437 bt_ctf_field_set_error(-EINVAL);
438 return NULL;
439 }
440 def_enum = container_of(field, const struct definition_enum, p);
441 return &def_enum->integer->p;
442 }
443
444 const char *bt_ctf_get_enum_str(const struct definition *field)
445 {
446 const struct definition_enum *def_enum;
447 const struct declaration_enum *decl_enum;
448 GArray *array;
449 const char *ret;
450
451 if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
452 bt_ctf_field_set_error(-EINVAL);
453 return NULL;
454 }
455 def_enum = container_of(field, const struct definition_enum, p);
456 decl_enum = def_enum->declaration;
457 if (get_int_signedness(&def_enum->integer->p)) {
458 array = enum_int_to_quark_set(decl_enum,
459 get_signed_int(&def_enum->integer->p));
460 } else {
461 array = enum_uint_to_quark_set(decl_enum,
462 get_unsigned_int(&def_enum->integer->p));
463 }
464 if (!array) {
465 bt_ctf_field_set_error(-ENOENT);
466 return NULL;
467 }
468
469 if (array->len == 0) {
470 g_array_unref(array);
471 bt_ctf_field_set_error(-ENOENT);
472 return NULL;
473 }
474 /* Return first string. Arbitrary choice. */
475 ret = g_quark_to_string(g_array_index(array, GQuark, 0));
476 g_array_unref(array);
477 return ret;
478 }
479
480 enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl)
481 {
482 enum ctf_string_encoding ret = 0;
483 enum ctf_type_id type;
484 const struct declaration_integer *integer;
485 const struct declaration_string *string;
486 const struct declaration_array *array;
487 const struct declaration_sequence *sequence;
488
489 if (!decl)
490 goto error;
491
492 type = bt_ctf_field_type(decl);
493
494 switch (type) {
495 case CTF_TYPE_ARRAY:
496 array = get_declaration_array(decl);
497 if (!array)
498 goto error;
499 integer = get_declaration_integer(array->elem);
500 if (!integer)
501 goto error;
502 ret = integer->encoding;
503 break;
504 case CTF_TYPE_SEQUENCE:
505 sequence = get_declaration_sequence(decl);
506 if (!sequence)
507 goto error;
508 integer = get_declaration_integer(sequence->elem);
509 if (!integer)
510 goto error;
511 ret = integer->encoding;
512 break;
513 case CTF_TYPE_STRING:
514 string = get_declaration_string(decl);
515 if (!string)
516 goto error;
517 ret = string->encoding;
518 break;
519 case CTF_TYPE_INTEGER:
520 integer = get_declaration_integer(decl);
521 if (!integer)
522 goto error;
523 ret = integer->encoding;
524 break;
525 default:
526 goto error;
527 }
528 return ret;
529
530 error:
531 bt_ctf_field_set_error(-EINVAL);
532 return -1;
533 }
534
535 int bt_ctf_get_array_len(const struct declaration *decl)
536 {
537 const struct declaration_array *array;
538
539 array = get_declaration_array(decl);
540 if (!array)
541 goto error;
542 return array->len;
543
544 error:
545 bt_ctf_field_set_error(-EINVAL);
546 return -1;
547 }
548
549 uint64_t bt_ctf_get_uint64(const struct definition *field)
550 {
551 uint64_t ret = 0;
552
553 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER)
554 ret = get_unsigned_int(field);
555 else
556 bt_ctf_field_set_error(-EINVAL);
557
558 return ret;
559 }
560
561 int64_t bt_ctf_get_int64(const struct definition *field)
562 {
563 int64_t ret = 0;
564
565 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER)
566 ret = get_signed_int(field);
567 else
568 bt_ctf_field_set_error(-EINVAL);
569
570 return ret;
571 }
572
573 char *bt_ctf_get_char_array(const struct definition *field)
574 {
575 char *ret = NULL;
576 GString *char_array;
577
578 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_ARRAY) {
579 char_array = get_char_array(field);
580 if (char_array) {
581 ret = char_array->str;
582 goto end;
583 }
584 }
585 bt_ctf_field_set_error(-EINVAL);
586
587 end:
588 return ret;
589 }
590
591 char *bt_ctf_get_string(const struct definition *field)
592 {
593 char *ret = NULL;
594
595 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_STRING)
596 ret = get_string(field);
597 else
598 bt_ctf_field_set_error(-EINVAL);
599
600 return ret;
601 }
602
603 int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
604 struct bt_ctf_event_decl * const **list,
605 unsigned int *count)
606 {
607 struct bt_trace_handle *handle;
608 struct trace_descriptor *td;
609 struct ctf_trace *tin;
610
611 if (!ctx || !list || !count)
612 goto error;
613
614 handle = g_hash_table_lookup(ctx->trace_handles,
615 (gpointer) (unsigned long) handle_id);
616 if (!handle)
617 goto error;
618
619 td = handle->td;
620 tin = container_of(td, struct ctf_trace, parent);
621
622 *list = (struct bt_ctf_event_decl * const*) tin->event_declarations->pdata;
623 *count = tin->event_declarations->len;
624 return 0;
625
626 error:
627 return -1;
628 }
629
630 const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
631 {
632 if (!event)
633 return NULL;
634
635 return g_quark_to_string(event->parent.name);
636 }
637
638 int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl,
639 enum bt_ctf_scope scope,
640 struct bt_ctf_field_decl const * const **list,
641 unsigned int *count)
642 {
643 int i;
644 GArray *fields = NULL;
645 gpointer *ret_list = NULL;
646 GPtrArray *fields_array = NULL;
647 int ret = 0;
648 *count = 0;
649
650 if (!event_decl || !list || !count)
651 return -EINVAL;
652
653 switch (scope) {
654 case BT_EVENT_CONTEXT:
655 if (event_decl->context_decl) {
656 ret_list = event_decl->context_decl->pdata;
657 *count = event_decl->context_decl->len;
658 goto end;
659 }
660 event_decl->context_decl = g_ptr_array_new();
661 if (!event_decl->parent.context_decl) {
662 ret = -1;
663 goto end;
664 }
665 fields = event_decl->parent.context_decl->fields;
666 fields_array = event_decl->context_decl;
667 break;
668 case BT_EVENT_FIELDS:
669 if (event_decl->fields_decl) {
670 ret_list = event_decl->fields_decl->pdata;
671 *count = event_decl->fields_decl->len;
672 goto end;
673 }
674 event_decl->fields_decl = g_ptr_array_new();
675 if (!event_decl->parent.fields_decl) {
676 ret = -1;
677 goto end;
678 }
679 fields = event_decl->parent.fields_decl->fields;
680 fields_array = event_decl->fields_decl;
681 break;
682 case BT_STREAM_PACKET_CONTEXT:
683 if (event_decl->packet_context_decl) {
684 ret_list = event_decl->packet_context_decl->pdata;
685 *count = event_decl->packet_context_decl->len;
686 goto end;
687 }
688 event_decl->packet_context_decl = g_ptr_array_new();
689 if (!event_decl->parent.stream->packet_context_decl) {
690 ret = -1;
691 goto end;
692 }
693 fields = event_decl->parent.stream->packet_context_decl->fields;
694 fields_array = event_decl->packet_context_decl;
695 break;
696 case BT_STREAM_EVENT_CONTEXT:
697 if (event_decl->event_context_decl) {
698 ret_list = event_decl->event_context_decl->pdata;
699 *count = event_decl->event_context_decl->len;
700 goto end;
701 }
702 event_decl->event_context_decl = g_ptr_array_new();
703 if (!event_decl->parent.stream->event_context_decl) {
704 ret = -1;
705 goto end;
706 }
707 fields = event_decl->parent.stream->event_context_decl->fields;
708 fields_array = event_decl->event_context_decl;
709 break;
710 case BT_STREAM_EVENT_HEADER:
711 if (event_decl->event_header_decl) {
712 ret_list = event_decl->event_header_decl->pdata;
713 *count = event_decl->event_header_decl->len;
714 goto end;
715 }
716 event_decl->event_header_decl = g_ptr_array_new();
717 if (!event_decl->parent.stream->event_header_decl) {
718 ret = -1;
719 goto end;
720 }
721 fields = event_decl->parent.stream->event_header_decl->fields;
722 fields_array = event_decl->event_header_decl;
723 break;
724 case BT_TRACE_PACKET_HEADER:
725 if (event_decl->packet_header_decl) {
726 ret_list = event_decl->packet_header_decl->pdata;
727 *count = event_decl->packet_header_decl->len;
728 goto end;
729 }
730 event_decl->packet_header_decl = g_ptr_array_new();
731 if (!event_decl->parent.stream->trace->packet_header_decl) {
732 ret = -1;
733 goto end;
734 }
735 fields = event_decl->parent.stream->trace->packet_header_decl->fields;
736 fields_array = event_decl->packet_header_decl;
737 break;
738 }
739
740 for (i = 0; i < fields->len; i++) {
741 g_ptr_array_add(fields_array,
742 &g_array_index(fields,
743 struct declaration_field, i));
744 }
745 ret_list = fields_array->pdata;
746 *count = fields->len;
747
748 end:
749 *list = (struct bt_ctf_field_decl const* const*) ret_list;
750
751 return ret;
752 }
753
754 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
755 {
756 if (!field)
757 return NULL;
758
759 return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
760 }
761
762 const struct declaration *bt_ctf_get_decl_from_def(const struct definition *def)
763 {
764 if (def)
765 return def->declaration;
766
767 return NULL;
768 }
769
770 const struct declaration *bt_ctf_get_decl_from_field_decl(
771 const struct bt_ctf_field_decl *field)
772 {
773 if (field)
774 return ((struct declaration_field *) field)->declaration;
775
776 return NULL;
777 }
This page took 0.070012 seconds and 3 git commands to generate.