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.
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>
32 #include "events-private.h"
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
39 __thread
int bt_ctf_last_field_error
= 0;
41 const struct definition
*bt_ctf_get_top_level_scope(const struct bt_ctf_event
*ctf_event
,
42 enum bt_ctf_scope scope
)
44 struct definition
*tmp
= NULL
;
45 struct ctf_event_definition
*event
;
50 event
= ctf_event
->parent
;
52 case BT_TRACE_PACKET_HEADER
:
55 if (event
->stream
->trace_packet_header
)
56 tmp
= &event
->stream
->trace_packet_header
->p
;
58 case BT_STREAM_PACKET_CONTEXT
:
61 if (event
->stream
->stream_packet_context
)
62 tmp
= &event
->stream
->stream_packet_context
->p
;
64 case BT_STREAM_EVENT_HEADER
:
67 if (event
->stream
->stream_event_header
)
68 tmp
= &event
->stream
->stream_event_header
->p
;
70 case BT_STREAM_EVENT_CONTEXT
:
73 if (event
->stream
->stream_event_context
)
74 tmp
= &event
->stream
->stream_event_context
->p
;
76 case BT_EVENT_CONTEXT
:
77 if (event
->event_context
)
78 tmp
= &event
->event_context
->p
;
81 if (event
->event_fields
)
82 tmp
= &event
->event_fields
->p
;
91 const struct definition
*bt_ctf_get_field(const struct bt_ctf_event
*ctf_event
,
92 const struct definition
*scope
,
95 struct definition
*def
;
96 char *field_underscore
;
98 if (!ctf_event
|| !scope
|| !field
)
101 def
= lookup_definition(scope
, field
);
103 * optionally a field can have an underscore prefix, try
104 * to lookup the field with this prefix if it failed
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
);
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
;
122 const struct definition
*bt_ctf_get_index(const struct bt_ctf_event
*ctf_event
,
123 const struct definition
*field
,
126 struct definition
*ret
= NULL
;
128 if (!ctf_event
|| !field
)
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
);
145 const char *bt_ctf_event_name(const struct bt_ctf_event
*ctf_event
)
147 struct ctf_event_declaration
*event_class
;
148 struct ctf_stream_declaration
*stream_class
;
149 struct ctf_event_definition
*event
;
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
);
161 const char *bt_ctf_field_name(const struct definition
*def
)
166 return rem_(g_quark_to_string(def
->name
));
169 enum ctf_type_id
bt_ctf_field_type(const struct definition
*def
)
172 return CTF_TYPE_UNKNOWN
;
174 return def
->declaration
->id
;
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
,
182 if (!ctf_event
|| !scope
|| !list
|| !count
)
185 switch (bt_ctf_field_type(scope
)) {
186 case CTF_TYPE_INTEGER
:
188 case CTF_TYPE_STRING
:
191 case CTF_TYPE_STRUCT
:
193 const struct definition_struct
*def_struct
;
195 def_struct
= container_of(scope
, const struct definition_struct
, p
);
198 if (def_struct
->fields
->pdata
) {
199 *list
= (struct definition
const* const*) def_struct
->fields
->pdata
;
200 *count
= def_struct
->fields
->len
;
207 case CTF_TYPE_UNTAGGED_VARIANT
:
209 case CTF_TYPE_VARIANT
:
211 const struct definition_variant
*def_variant
;
213 def_variant
= container_of(scope
, const struct definition_variant
, p
);
216 if (def_variant
->fields
->pdata
) {
217 *list
= (struct definition
const* const*) def_variant
->fields
->pdata
;
218 *count
= def_variant
->fields
->len
;
227 const struct definition_array
*def_array
;
229 def_array
= container_of(scope
, const struct definition_array
, p
);
232 if (def_array
->elems
->pdata
) {
233 *list
= (struct definition
const* const*) def_array
->elems
->pdata
;
234 *count
= def_array
->elems
->len
;
241 case CTF_TYPE_SEQUENCE
:
243 const struct definition_sequence
*def_sequence
;
245 def_sequence
= container_of(scope
, const struct definition_sequence
, p
);
248 if (def_sequence
->elems
->pdata
) {
249 *list
= (struct definition
const* const*) def_sequence
->elems
->pdata
;
250 *count
= def_sequence
->elems
->len
;
270 struct bt_context
*bt_ctf_event_get_context(const struct bt_ctf_event
*ctf_event
)
272 struct bt_context
*ret
= NULL
;
273 struct ctf_file_stream
*cfs
;
274 struct ctf_trace
*trace
;
275 struct ctf_event_definition
*event
;
280 event
= ctf_event
->parent
;
281 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
283 trace
= cfs
->parent
.stream_class
->trace
;
290 int bt_ctf_event_get_handle_id(const struct bt_ctf_event
*ctf_event
)
293 struct ctf_file_stream
*cfs
;
294 struct ctf_trace
*trace
;
295 struct ctf_event_definition
*event
;
300 event
= ctf_event
->parent
;
301 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
303 trace
= cfs
->parent
.stream_class
->trace
;
305 ret
= trace
->handle
->id
;
310 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event
*ctf_event
)
312 struct ctf_event_definition
*event
;
317 event
= ctf_event
->parent
;
318 if (event
&& event
->stream
->has_timestamp
)
319 return event
->stream
->real_timestamp
;
324 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event
*ctf_event
)
326 struct ctf_event_definition
*event
;
331 event
= ctf_event
->parent
;
332 if (event
&& event
->stream
->has_timestamp
)
333 return event
->stream
->cycles_timestamp
;
338 static void bt_ctf_field_set_error(int error
)
340 bt_ctf_last_field_error
= error
;
343 int bt_ctf_field_get_error(void)
346 ret
= bt_ctf_last_field_error
;
347 bt_ctf_last_field_error
= 0;
352 int bt_ctf_get_int_signedness(const struct definition
*field
)
356 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
357 ret
= get_int_signedness(field
);
360 bt_ctf_field_set_error(-EINVAL
);
366 int bt_ctf_get_int_base(const struct definition
*field
)
370 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
371 ret
= get_int_base(field
);
374 bt_ctf_field_set_error(-EINVAL
);
380 int bt_ctf_get_int_byte_order(const struct definition
*field
)
384 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
385 ret
= get_int_byte_order(field
);
388 bt_ctf_field_set_error(-EINVAL
);
394 ssize_t
bt_ctf_get_int_len(const struct definition
*field
)
398 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
399 ret
= (ssize_t
) get_int_len(field
);
402 bt_ctf_field_set_error(-EINVAL
);
408 enum ctf_string_encoding
bt_ctf_get_encoding(const struct definition
*field
)
410 enum ctf_string_encoding ret
= 0;
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
);
424 bt_ctf_field_set_error(-EINVAL
);
428 int bt_ctf_get_array_len(const struct definition
*field
)
432 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
433 ret
= get_array_len(field
);
436 bt_ctf_field_set_error(-EINVAL
);
442 uint64_t bt_ctf_get_uint64(const struct definition
*field
)
446 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
447 ret
= get_unsigned_int(field
);
449 bt_ctf_field_set_error(-EINVAL
);
454 int64_t bt_ctf_get_int64(const struct definition
*field
)
458 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
459 ret
= get_signed_int(field
);
461 bt_ctf_field_set_error(-EINVAL
);
467 char *bt_ctf_get_char_array(const struct definition
*field
)
471 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
472 ret
= get_char_array(field
)->str
;
474 bt_ctf_field_set_error(-EINVAL
);
479 char *bt_ctf_get_string(const struct definition
*field
)
483 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
484 ret
= get_string(field
);
486 bt_ctf_field_set_error(-EINVAL
);
491 int bt_ctf_get_event_decl_list(int handle_id
, struct bt_context
*ctx
,
492 struct bt_ctf_event_decl
* const **list
,
495 struct bt_trace_handle
*handle
;
496 struct trace_descriptor
*td
;
497 struct ctf_trace
*tin
;
499 if (!ctx
|| !list
|| !count
)
502 handle
= g_hash_table_lookup(ctx
->trace_handles
,
503 (gpointer
) (unsigned long) handle_id
);
508 tin
= container_of(td
, struct ctf_trace
, parent
);
510 *list
= (struct bt_ctf_event_decl
* const*) tin
->event_declarations
->pdata
;
511 *count
= tin
->event_declarations
->len
;
518 const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl
*event
)
523 return g_quark_to_string(event
->parent
.name
);
526 int 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
,
532 GArray
*fields
= NULL
;
533 gpointer
*ret_list
= NULL
;
534 GPtrArray
*fields_array
= NULL
;
538 if (!event_decl
|| !list
|| !count
)
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
;
548 event_decl
->context_decl
= g_ptr_array_new();
549 if (!event_decl
->parent
.context_decl
) {
553 fields
= event_decl
->parent
.context_decl
->fields
;
554 fields_array
= event_decl
->context_decl
;
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
;
562 event_decl
->fields_decl
= g_ptr_array_new();
563 if (!event_decl
->parent
.fields_decl
) {
567 fields
= event_decl
->parent
.fields_decl
->fields
;
568 fields_array
= event_decl
->fields_decl
;
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
;
576 event_decl
->packet_context_decl
= g_ptr_array_new();
577 if (!event_decl
->parent
.stream
->packet_context_decl
) {
581 fields
= event_decl
->parent
.stream
->packet_context_decl
->fields
;
582 fields_array
= event_decl
->packet_context_decl
;
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
;
590 event_decl
->event_context_decl
= g_ptr_array_new();
591 if (!event_decl
->parent
.stream
->event_context_decl
) {
595 fields
= event_decl
->parent
.stream
->event_context_decl
->fields
;
596 fields_array
= event_decl
->event_context_decl
;
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
;
604 event_decl
->event_header_decl
= g_ptr_array_new();
605 if (!event_decl
->parent
.stream
->event_header_decl
) {
609 fields
= event_decl
->parent
.stream
->event_header_decl
->fields
;
610 fields_array
= event_decl
->event_header_decl
;
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
;
618 event_decl
->packet_header_decl
= g_ptr_array_new();
619 if (!event_decl
->parent
.stream
->trace
->packet_header_decl
) {
623 fields
= event_decl
->parent
.stream
->trace
->packet_header_decl
->fields
;
624 fields_array
= event_decl
->packet_header_decl
;
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
));
633 ret_list
= fields_array
->pdata
;
634 *count
= fields
->len
;
637 *list
= (struct bt_ctf_field_decl
const* const*) ret_list
;
642 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl
*field
)
647 return rem_(g_quark_to_string(((struct declaration_field
*) field
)->name
));