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>
33 * thread local storage to store the last error that occured
34 * while reading a field, this variable must be accessed by
35 * bt_ctf_field_error only
37 __thread
int bt_ctf_last_field_error
= 0;
39 struct bt_ctf_iter
*bt_ctf_iter_create(struct bt_context
*ctx
,
40 struct bt_iter_pos
*begin_pos
,
41 struct bt_iter_pos
*end_pos
)
43 struct bt_ctf_iter
*iter
;
46 iter
= g_new0(struct bt_ctf_iter
, 1);
47 ret
= bt_iter_init(&iter
->parent
, ctx
, begin_pos
, end_pos
);
52 iter
->callbacks
= g_array_new(0, 1, sizeof(struct bt_stream_callbacks
));
53 iter
->recalculate_dep_graph
= 0;
54 iter
->main_callbacks
.callback
= NULL
;
55 iter
->dep_gc
= g_ptr_array_new();
59 void bt_ctf_iter_destroy(struct bt_ctf_iter
*iter
)
61 struct bt_stream_callbacks
*bt_stream_cb
;
62 struct bt_callback_chain
*bt_chain
;
65 /* free all events callbacks */
66 if (iter
->main_callbacks
.callback
)
67 g_array_free(iter
->main_callbacks
.callback
, TRUE
);
69 /* free per-event callbacks */
70 for (i
= 0; i
< iter
->callbacks
->len
; i
++) {
71 bt_stream_cb
= &g_array_index(iter
->callbacks
,
72 struct bt_stream_callbacks
, i
);
73 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
75 for (j
= 0; j
< bt_stream_cb
->per_id_callbacks
->len
; j
++) {
76 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
77 struct bt_callback_chain
, j
);
78 if (bt_chain
->callback
) {
79 g_array_free(bt_chain
->callback
, TRUE
);
82 g_array_free(bt_stream_cb
->per_id_callbacks
, TRUE
);
85 bt_iter_fini(&iter
->parent
);
89 struct bt_iter
*bt_ctf_get_iter(struct bt_ctf_iter
*iter
)
94 struct bt_ctf_event
*bt_ctf_iter_read_event(struct bt_ctf_iter
*iter
)
96 struct ctf_file_stream
*file_stream
;
97 struct bt_ctf_event
*ret
= &iter
->current_ctf_event
;
99 file_stream
= heap_maximum(iter
->parent
.stream_heap
);
101 /* end of file for all streams */
104 ret
->stream
= &file_stream
->parent
;
105 ret
->event
= g_ptr_array_index(ret
->stream
->events_by_id
,
106 ret
->stream
->event_id
);
108 if (ret
->stream
->stream_id
> iter
->callbacks
->len
)
111 process_callbacks(iter
, ret
->stream
);
119 struct definition
*bt_ctf_get_top_level_scope(struct bt_ctf_event
*event
,
120 enum bt_ctf_scope scope
)
122 struct definition
*tmp
= NULL
;
125 case BT_TRACE_PACKET_HEADER
:
128 if (event
->stream
->trace_packet_header
)
129 tmp
= &event
->stream
->trace_packet_header
->p
;
131 case BT_STREAM_PACKET_CONTEXT
:
134 if (event
->stream
->stream_packet_context
)
135 tmp
= &event
->stream
->stream_packet_context
->p
;
137 case BT_STREAM_EVENT_HEADER
:
140 if (event
->stream
->stream_event_header
)
141 tmp
= &event
->stream
->stream_event_header
->p
;
143 case BT_STREAM_EVENT_CONTEXT
:
146 if (event
->stream
->stream_event_context
)
147 tmp
= &event
->stream
->stream_event_context
->p
;
149 case BT_EVENT_CONTEXT
:
152 if (event
->event
->event_context
)
153 tmp
= &event
->event
->event_context
->p
;
155 case BT_EVENT_FIELDS
:
158 if (event
->event
->event_fields
)
159 tmp
= &event
->event
->event_fields
->p
;
168 struct definition
*bt_ctf_get_field(struct bt_ctf_event
*event
,
169 struct definition
*scope
,
172 struct definition
*def
;
175 def
= lookup_definition(scope
, field
);
176 if (bt_ctf_field_type(def
) == CTF_TYPE_VARIANT
) {
177 struct definition_variant
*variant_definition
;
178 variant_definition
= container_of(def
,
179 struct definition_variant
, p
);
180 return variant_definition
->current_field
;
187 struct definition
*bt_ctf_get_index(struct bt_ctf_event
*event
,
188 struct definition
*field
,
191 struct definition
*ret
= NULL
;
193 if (bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
194 struct definition_array
*array_definition
;
195 array_definition
= container_of(field
,
196 struct definition_array
, p
);
197 ret
= array_index(array_definition
, index
);
198 } else if (bt_ctf_field_type(field
) == CTF_TYPE_SEQUENCE
) {
199 struct definition_sequence
*sequence_definition
;
200 sequence_definition
= container_of(field
,
201 struct definition_sequence
, p
);
202 ret
= sequence_index(sequence_definition
, index
);
207 const char *bt_ctf_event_name(struct bt_ctf_event
*event
)
209 struct ctf_event
*event_class
;
210 struct ctf_stream_class
*stream_class
;
214 stream_class
= event
->stream
->stream_class
;
215 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
216 event
->stream
->event_id
);
217 return g_quark_to_string(event_class
->name
);
220 const char *bt_ctf_field_name(const struct definition
*def
)
223 return g_quark_to_string(def
->name
);
227 enum ctf_type_id
bt_ctf_field_type(struct definition
*def
)
230 return def
->declaration
->id
;
231 return CTF_TYPE_UNKNOWN
;
234 int bt_ctf_get_field_list(struct bt_ctf_event
*event
,
235 struct definition
*scope
,
236 struct definition
const * const **list
,
239 switch (bt_ctf_field_type(scope
)) {
240 case CTF_TYPE_INTEGER
:
242 case CTF_TYPE_STRING
:
245 case CTF_TYPE_STRUCT
:
247 struct definition_struct
*def_struct
;
249 def_struct
= container_of(scope
, struct definition_struct
, p
);
252 if (def_struct
->fields
->pdata
) {
253 *list
= (struct definition
const* const*) def_struct
->fields
->pdata
;
254 *count
= def_struct
->fields
->len
;
260 case CTF_TYPE_UNTAGGED_VARIANT
:
262 case CTF_TYPE_VARIANT
:
264 struct definition_variant
*def_variant
;
266 def_variant
= container_of(scope
, struct definition_variant
, p
);
269 if (def_variant
->fields
->pdata
) {
270 *list
= (struct definition
const* const*) def_variant
->fields
->pdata
;
271 *count
= def_variant
->fields
->len
;
279 struct definition_array
*def_array
;
281 def_array
= container_of(scope
, struct definition_array
, p
);
284 if (def_array
->elems
->pdata
) {
285 *list
= (struct definition
const* const*) def_array
->elems
->pdata
;
286 *count
= def_array
->elems
->len
;
292 case CTF_TYPE_SEQUENCE
:
294 struct definition_sequence
*def_sequence
;
296 def_sequence
= container_of(scope
, struct definition_sequence
, p
);
299 if (def_sequence
->elems
->pdata
) {
300 *list
= (struct definition
const* const*) def_sequence
->elems
->pdata
;
301 *count
= def_sequence
->elems
->len
;
320 uint64_t bt_ctf_get_timestamp(struct bt_ctf_event
*event
)
322 if (event
&& event
->stream
->has_timestamp
)
323 return event
->stream
->timestamp
;
328 static void bt_ctf_field_set_error(int error
)
330 bt_ctf_last_field_error
= error
;
333 int bt_ctf_field_get_error(void)
336 ret
= bt_ctf_last_field_error
;
337 bt_ctf_last_field_error
= 0;
342 uint64_t bt_ctf_get_uint64(struct definition
*field
)
344 unsigned int ret
= 0;
346 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
347 ret
= get_unsigned_int(field
);
349 bt_ctf_field_set_error(-EINVAL
);
354 int64_t bt_ctf_get_int64(struct definition
*field
)
358 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
359 ret
= get_signed_int(field
);
361 bt_ctf_field_set_error(-EINVAL
);
367 char *bt_ctf_get_char_array(struct definition
*field
)
371 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
372 ret
= get_char_array(field
)->str
;
374 bt_ctf_field_set_error(-EINVAL
);
379 char *bt_ctf_get_string(struct definition
*field
)
383 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
384 ret
= get_string(field
);
386 bt_ctf_field_set_error(-EINVAL
);