6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/babeltrace.h>
31 #include <babeltrace/format.h>
32 #include <babeltrace/ctf/events.h>
33 #include <babeltrace/ctf-ir/metadata.h>
34 #include <babeltrace/prio_heap.h>
35 #include <babeltrace/iterator-internal.h>
36 #include <babeltrace/ctf/events-internal.h>
37 #include <babeltrace/ctf/metadata.h>
40 #include "events-private.h"
43 * thread local storage to store the last error that occured
44 * while reading a field, this variable must be accessed by
45 * bt_ctf_field_get_error only
47 __thread
int bt_ctf_last_field_error
= 0;
49 const struct bt_definition
*bt_ctf_get_top_level_scope(const struct bt_ctf_event
*ctf_event
,
50 enum bt_ctf_scope scope
)
52 const struct bt_definition
*tmp
= NULL
;
53 const struct ctf_event_definition
*event
;
58 event
= ctf_event
->parent
;
60 case BT_TRACE_PACKET_HEADER
:
63 if (event
->stream
->trace_packet_header
)
64 tmp
= &event
->stream
->trace_packet_header
->p
;
66 case BT_STREAM_PACKET_CONTEXT
:
69 if (event
->stream
->stream_packet_context
)
70 tmp
= &event
->stream
->stream_packet_context
->p
;
72 case BT_STREAM_EVENT_HEADER
:
75 if (event
->stream
->stream_event_header
)
76 tmp
= &event
->stream
->stream_event_header
->p
;
78 case BT_STREAM_EVENT_CONTEXT
:
81 if (event
->stream
->stream_event_context
)
82 tmp
= &event
->stream
->stream_event_context
->p
;
84 case BT_EVENT_CONTEXT
:
85 if (event
->event_context
)
86 tmp
= &event
->event_context
->p
;
89 if (event
->event_fields
)
90 tmp
= &event
->event_fields
->p
;
99 const struct bt_definition
*bt_ctf_get_field(const struct bt_ctf_event
*ctf_event
,
100 const struct bt_definition
*scope
,
103 const struct bt_definition
*def
;
104 char *field_underscore
;
106 if (!ctf_event
|| !scope
|| !field
)
109 def
= bt_lookup_definition(scope
, field
);
111 * optionally a field can have an underscore prefix, try
112 * to lookup the field with this prefix if it failed
115 field_underscore
= g_new(char, strlen(field
) + 2);
116 field_underscore
[0] = '_';
117 strcpy(&field_underscore
[1], field
);
118 def
= bt_lookup_definition(scope
, field_underscore
);
119 g_free(field_underscore
);
121 if (bt_ctf_field_type(bt_ctf_get_decl_from_def(def
)) == CTF_TYPE_VARIANT
) {
122 const struct definition_variant
*variant_definition
;
123 variant_definition
= container_of(def
,
124 const struct definition_variant
, p
);
125 return variant_definition
->current_field
;
130 const struct bt_definition
*bt_ctf_get_index(const struct bt_ctf_event
*ctf_event
,
131 const struct bt_definition
*field
,
134 struct bt_definition
*ret
= NULL
;
136 if (!ctf_event
|| !field
)
139 if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_ARRAY
) {
140 struct definition_array
*array_definition
;
141 array_definition
= container_of(field
,
142 struct definition_array
, p
);
143 ret
= bt_array_index(array_definition
, index
);
144 } else if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
145 struct definition_sequence
*sequence_definition
;
146 sequence_definition
= container_of(field
,
147 struct definition_sequence
, p
);
148 ret
= bt_sequence_index(sequence_definition
, index
);
153 const char *bt_ctf_event_name(const struct bt_ctf_event
*ctf_event
)
155 const struct ctf_event_declaration
*event_class
;
156 const struct ctf_stream_declaration
*stream_class
;
157 const struct ctf_event_definition
*event
;
162 event
= ctf_event
->parent
;
163 stream_class
= event
->stream
->stream_class
;
164 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
165 event
->stream
->event_id
);
166 return g_quark_to_string(event_class
->name
);
169 const char *bt_ctf_field_name(const struct bt_definition
*def
)
171 if (!def
|| !def
->name
)
174 return rem_(g_quark_to_string(def
->name
));
177 enum ctf_type_id
bt_ctf_field_type(const struct bt_declaration
*decl
)
180 return CTF_TYPE_UNKNOWN
;
185 int bt_ctf_get_field_list(const struct bt_ctf_event
*ctf_event
,
186 const struct bt_definition
*scope
,
187 struct bt_definition
const * const **list
,
190 if (!ctf_event
|| !scope
|| !list
|| !count
)
193 switch (bt_ctf_field_type(bt_ctf_get_decl_from_def(scope
))) {
194 case CTF_TYPE_INTEGER
:
196 case CTF_TYPE_STRING
:
199 case CTF_TYPE_STRUCT
:
201 const struct definition_struct
*def_struct
;
203 def_struct
= container_of(scope
, const struct definition_struct
, p
);
206 if (def_struct
->fields
->pdata
) {
207 *list
= (struct bt_definition
const* const*) def_struct
->fields
->pdata
;
208 *count
= def_struct
->fields
->len
;
215 case CTF_TYPE_UNTAGGED_VARIANT
:
217 case CTF_TYPE_VARIANT
:
219 const struct definition_variant
*def_variant
;
221 def_variant
= container_of(scope
, const struct definition_variant
, p
);
224 if (def_variant
->fields
->pdata
) {
225 *list
= (struct bt_definition
const* const*) def_variant
->fields
->pdata
;
226 *count
= def_variant
->fields
->len
;
235 const struct definition_array
*def_array
;
237 def_array
= container_of(scope
, const struct definition_array
, p
);
240 if (def_array
->elems
->pdata
) {
241 *list
= (struct bt_definition
const* const*) def_array
->elems
->pdata
;
242 *count
= def_array
->elems
->len
;
249 case CTF_TYPE_SEQUENCE
:
251 const struct definition_sequence
*def_sequence
;
253 def_sequence
= container_of(scope
, const struct definition_sequence
, p
);
256 if (def_sequence
->elems
->pdata
) {
257 *list
= (struct bt_definition
const* const*) def_sequence
->elems
->pdata
;
258 *count
= def_sequence
->elems
->len
;
278 struct bt_context
*bt_ctf_event_get_context(const struct bt_ctf_event
*ctf_event
)
280 struct bt_context
*ret
= NULL
;
281 const struct ctf_file_stream
*cfs
;
282 const struct ctf_trace
*trace
;
283 const struct ctf_event_definition
*event
;
288 event
= ctf_event
->parent
;
289 cfs
= container_of(event
->stream
, const struct ctf_file_stream
,
291 trace
= cfs
->parent
.stream_class
->trace
;
292 if (trace
->parent
.ctx
)
293 ret
= trace
->parent
.ctx
;
298 int bt_ctf_event_get_handle_id(const struct bt_ctf_event
*ctf_event
)
301 const struct ctf_file_stream
*cfs
;
302 const struct ctf_trace
*trace
;
303 const struct ctf_event_definition
*event
;
308 event
= ctf_event
->parent
;
309 cfs
= container_of(event
->stream
, const struct ctf_file_stream
,
311 trace
= cfs
->parent
.stream_class
->trace
;
312 if (trace
->parent
.handle
)
313 ret
= trace
->parent
.handle
->id
;
318 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event
*ctf_event
)
320 const struct ctf_event_definition
*event
;
325 event
= ctf_event
->parent
;
326 if (event
&& event
->stream
->has_timestamp
)
327 return event
->stream
->real_timestamp
;
332 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event
*ctf_event
)
334 const struct ctf_event_definition
*event
;
339 event
= ctf_event
->parent
;
340 if (event
&& event
->stream
->has_timestamp
)
341 return event
->stream
->cycles_timestamp
;
346 static void bt_ctf_field_set_error(int error
)
348 bt_ctf_last_field_error
= error
;
351 int bt_ctf_field_get_error(void)
354 ret
= bt_ctf_last_field_error
;
355 bt_ctf_last_field_error
= 0;
360 static const struct declaration_integer
*
361 get_declaration_integer(const struct bt_declaration
*decl
)
363 if (!decl
|| bt_ctf_field_type(decl
) != CTF_TYPE_INTEGER
)
365 return container_of(decl
, const struct declaration_integer
, p
);
368 static const struct declaration_string
*
369 get_declaration_string(const struct bt_declaration
*decl
)
371 if (!decl
|| bt_ctf_field_type(decl
) != CTF_TYPE_STRING
)
373 return container_of(decl
, const struct declaration_string
, p
);
376 static const struct declaration_array
*
377 get_declaration_array(const struct bt_declaration
*decl
)
379 if (!decl
|| bt_ctf_field_type(decl
) != CTF_TYPE_ARRAY
)
381 return container_of(decl
, const struct declaration_array
, p
);
384 static const struct declaration_sequence
*
385 get_declaration_sequence(const struct bt_declaration
*decl
)
387 if (!decl
|| bt_ctf_field_type(decl
) != CTF_TYPE_SEQUENCE
)
389 return container_of(decl
, const struct declaration_sequence
, p
);
392 int bt_ctf_get_int_signedness(const struct bt_declaration
*decl
)
394 const struct declaration_integer
*integer
;
396 integer
= get_declaration_integer(decl
);
398 bt_ctf_field_set_error(-EINVAL
);
401 return integer
->signedness
;
404 int bt_ctf_get_int_base(const struct bt_declaration
*decl
)
406 const struct declaration_integer
*integer
;
408 integer
= get_declaration_integer(decl
);
410 bt_ctf_field_set_error(-EINVAL
);
413 return integer
->base
;
416 int bt_ctf_get_int_byte_order(const struct bt_declaration
*decl
)
418 const struct declaration_integer
*integer
;
420 integer
= get_declaration_integer(decl
);
422 bt_ctf_field_set_error(-EINVAL
);
425 return integer
->byte_order
;
428 ssize_t
bt_ctf_get_int_len(const struct bt_declaration
*decl
)
430 const struct declaration_integer
*integer
;
432 integer
= get_declaration_integer(decl
);
434 bt_ctf_field_set_error(-EINVAL
);
437 return (ssize_t
) integer
->len
;
440 const struct bt_definition
*bt_ctf_get_enum_int(const struct bt_definition
*field
)
442 const struct definition_enum
*def_enum
;
444 if (!field
|| bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) != CTF_TYPE_ENUM
) {
445 bt_ctf_field_set_error(-EINVAL
);
448 def_enum
= container_of(field
, const struct definition_enum
, p
);
449 return &def_enum
->integer
->p
;
452 const char *bt_ctf_get_enum_str(const struct bt_definition
*field
)
454 const struct definition_enum
*def_enum
;
455 const struct declaration_enum
*decl_enum
;
459 if (!field
|| bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) != CTF_TYPE_ENUM
) {
460 bt_ctf_field_set_error(-EINVAL
);
463 def_enum
= container_of(field
, const struct definition_enum
, p
);
464 decl_enum
= def_enum
->declaration
;
465 if (bt_get_int_signedness(&def_enum
->integer
->p
)) {
466 array
= bt_enum_int_to_quark_set(decl_enum
,
467 bt_get_signed_int(&def_enum
->integer
->p
));
469 array
= bt_enum_uint_to_quark_set(decl_enum
,
470 bt_get_unsigned_int(&def_enum
->integer
->p
));
473 bt_ctf_field_set_error(-ENOENT
);
477 if (array
->len
== 0) {
478 g_array_unref(array
);
479 bt_ctf_field_set_error(-ENOENT
);
482 /* Return first string. Arbitrary choice. */
483 ret
= g_quark_to_string(g_array_index(array
, GQuark
, 0));
484 g_array_unref(array
);
488 enum ctf_string_encoding
bt_ctf_get_encoding(const struct bt_declaration
*decl
)
490 enum ctf_string_encoding ret
= 0;
491 enum ctf_type_id type
;
492 const struct declaration_integer
*integer
;
493 const struct declaration_string
*string
;
494 const struct declaration_array
*array
;
495 const struct declaration_sequence
*sequence
;
500 type
= bt_ctf_field_type(decl
);
504 array
= get_declaration_array(decl
);
507 integer
= get_declaration_integer(array
->elem
);
510 ret
= integer
->encoding
;
512 case CTF_TYPE_SEQUENCE
:
513 sequence
= get_declaration_sequence(decl
);
516 integer
= get_declaration_integer(sequence
->elem
);
519 ret
= integer
->encoding
;
521 case CTF_TYPE_STRING
:
522 string
= get_declaration_string(decl
);
525 ret
= string
->encoding
;
527 case CTF_TYPE_INTEGER
:
528 integer
= get_declaration_integer(decl
);
531 ret
= integer
->encoding
;
539 bt_ctf_field_set_error(-EINVAL
);
543 int bt_ctf_get_array_len(const struct bt_declaration
*decl
)
545 const struct declaration_array
*array
;
547 array
= get_declaration_array(decl
);
553 bt_ctf_field_set_error(-EINVAL
);
557 uint64_t bt_ctf_get_uint64(const struct bt_definition
*field
)
561 if (field
&& bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_INTEGER
)
562 ret
= bt_get_unsigned_int(field
);
564 bt_ctf_field_set_error(-EINVAL
);
569 int64_t bt_ctf_get_int64(const struct bt_definition
*field
)
573 if (field
&& bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_INTEGER
)
574 ret
= bt_get_signed_int(field
);
576 bt_ctf_field_set_error(-EINVAL
);
581 char *bt_ctf_get_char_array(const struct bt_definition
*field
)
586 if (field
&& bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_ARRAY
) {
587 char_array
= bt_get_char_array(field
);
589 ret
= char_array
->str
;
593 bt_ctf_field_set_error(-EINVAL
);
599 char *bt_ctf_get_string(const struct bt_definition
*field
)
603 if (field
&& bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_STRING
)
604 ret
= bt_get_string(field
);
606 bt_ctf_field_set_error(-EINVAL
);
611 double bt_ctf_get_float(const struct bt_definition
*field
)
615 if (field
&& bt_ctf_field_type(bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_FLOAT
) {
616 ret
= bt_get_float(field
);
618 bt_ctf_field_set_error(-EINVAL
);
624 const struct bt_definition
*bt_ctf_get_variant(const struct bt_definition
*field
)
626 const struct bt_definition
*ret
= NULL
;
628 if (field
&& bt_ctf_field_type(
629 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_VARIANT
) {
630 struct definition_variant
*variant
= container_of(field
,
631 struct definition_variant
, p
);
633 ret
= bt_variant_get_current_field(variant
);
635 bt_ctf_field_set_error(-EINVAL
);
641 uint64_t bt_ctf_get_struct_field_count(const struct bt_definition
*field
)
644 const struct bt_declaration
*declaration
=
645 bt_ctf_get_decl_from_def(field
);
647 if (field
&& bt_ctf_field_type(declaration
) == CTF_TYPE_STRUCT
) {
648 const struct declaration_struct
*struct_declaration
=
649 container_of(declaration
, struct declaration_struct
, p
);
651 ret
= bt_struct_declaration_len(struct_declaration
);
653 bt_ctf_field_set_error(-EINVAL
);
659 const struct bt_definition
*bt_ctf_get_struct_field_index(
660 const struct bt_definition
*field
, uint64_t i
)
662 const struct bt_definition
*ret
= NULL
;
664 if (field
&& bt_ctf_field_type(
665 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_STRUCT
&&
666 i
< bt_ctf_get_struct_field_count(field
)) {
667 const struct definition_struct
*structure
= container_of(
668 field
, struct definition_struct
, p
);
670 ret
= bt_struct_definition_get_field_from_index(structure
, i
);
674 bt_ctf_field_set_error(-EINVAL
);
680 int bt_ctf_get_event_decl_list(int handle_id
, struct bt_context
*ctx
,
681 struct bt_ctf_event_decl
* const **list
,
684 struct bt_trace_handle
*handle
;
685 struct bt_trace_descriptor
*td
;
686 struct ctf_trace
*tin
;
688 if (!ctx
|| !list
|| !count
)
691 handle
= g_hash_table_lookup(ctx
->trace_handles
,
692 (gpointer
) (unsigned long) handle_id
);
697 tin
= container_of(td
, struct ctf_trace
, parent
);
699 *list
= (struct bt_ctf_event_decl
* const*) tin
->event_declarations
->pdata
;
700 *count
= tin
->event_declarations
->len
;
707 const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl
*event
)
712 return g_quark_to_string(event
->parent
.name
);
715 int bt_ctf_get_decl_fields(struct bt_ctf_event_decl
*event_decl
,
716 enum bt_ctf_scope scope
,
717 struct bt_ctf_field_decl
const * const **list
,
721 GArray
*fields
= NULL
;
722 gpointer
*ret_list
= NULL
;
723 GPtrArray
*fields_array
= NULL
;
726 if (!event_decl
|| !list
|| !count
)
731 case BT_EVENT_CONTEXT
:
732 if (event_decl
->context_decl
) {
733 ret_list
= event_decl
->context_decl
->pdata
;
734 *count
= event_decl
->context_decl
->len
;
737 event_decl
->context_decl
= g_ptr_array_new();
738 if (!event_decl
->parent
.context_decl
) {
742 fields
= event_decl
->parent
.context_decl
->fields
;
743 fields_array
= event_decl
->context_decl
;
745 case BT_EVENT_FIELDS
:
746 if (event_decl
->fields_decl
) {
747 ret_list
= event_decl
->fields_decl
->pdata
;
748 *count
= event_decl
->fields_decl
->len
;
751 event_decl
->fields_decl
= g_ptr_array_new();
752 if (!event_decl
->parent
.fields_decl
) {
756 fields
= event_decl
->parent
.fields_decl
->fields
;
757 fields_array
= event_decl
->fields_decl
;
759 case BT_STREAM_PACKET_CONTEXT
:
760 if (event_decl
->packet_context_decl
) {
761 ret_list
= event_decl
->packet_context_decl
->pdata
;
762 *count
= event_decl
->packet_context_decl
->len
;
765 event_decl
->packet_context_decl
= g_ptr_array_new();
766 if (!event_decl
->parent
.stream
->packet_context_decl
) {
770 fields
= event_decl
->parent
.stream
->packet_context_decl
->fields
;
771 fields_array
= event_decl
->packet_context_decl
;
773 case BT_STREAM_EVENT_CONTEXT
:
774 if (event_decl
->event_context_decl
) {
775 ret_list
= event_decl
->event_context_decl
->pdata
;
776 *count
= event_decl
->event_context_decl
->len
;
779 event_decl
->event_context_decl
= g_ptr_array_new();
780 if (!event_decl
->parent
.stream
->event_context_decl
) {
784 fields
= event_decl
->parent
.stream
->event_context_decl
->fields
;
785 fields_array
= event_decl
->event_context_decl
;
787 case BT_STREAM_EVENT_HEADER
:
788 if (event_decl
->event_header_decl
) {
789 ret_list
= event_decl
->event_header_decl
->pdata
;
790 *count
= event_decl
->event_header_decl
->len
;
793 event_decl
->event_header_decl
= g_ptr_array_new();
794 if (!event_decl
->parent
.stream
->event_header_decl
) {
798 fields
= event_decl
->parent
.stream
->event_header_decl
->fields
;
799 fields_array
= event_decl
->event_header_decl
;
801 case BT_TRACE_PACKET_HEADER
:
802 if (event_decl
->packet_header_decl
) {
803 ret_list
= event_decl
->packet_header_decl
->pdata
;
804 *count
= event_decl
->packet_header_decl
->len
;
807 event_decl
->packet_header_decl
= g_ptr_array_new();
808 if (!event_decl
->parent
.stream
->trace
->packet_header_decl
) {
812 fields
= event_decl
->parent
.stream
->trace
->packet_header_decl
->fields
;
813 fields_array
= event_decl
->packet_header_decl
;
817 for (i
= 0; i
< fields
->len
; i
++) {
818 g_ptr_array_add(fields_array
,
819 &g_array_index(fields
,
820 struct declaration_field
, i
));
822 ret_list
= fields_array
->pdata
;
823 *count
= fields
->len
;
826 *list
= (struct bt_ctf_field_decl
const* const*) ret_list
;
831 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl
*field
)
836 return rem_(g_quark_to_string(((struct declaration_field
*) field
)->name
));
839 const struct bt_declaration
*bt_ctf_get_decl_from_def(const struct bt_definition
*def
)
842 return def
->declaration
;
847 const struct bt_declaration
*bt_ctf_get_decl_from_field_decl(
848 const struct bt_ctf_field_decl
*field
)
851 return ((struct declaration_field
*) field
)->declaration
;