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_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
*event
,
42 enum bt_ctf_scope scope
)
44 struct definition
*tmp
= NULL
;
47 case BT_TRACE_PACKET_HEADER
:
50 if (event
->stream
->trace_packet_header
)
51 tmp
= &event
->stream
->trace_packet_header
->p
;
53 case BT_STREAM_PACKET_CONTEXT
:
56 if (event
->stream
->stream_packet_context
)
57 tmp
= &event
->stream
->stream_packet_context
->p
;
59 case BT_STREAM_EVENT_HEADER
:
62 if (event
->stream
->stream_event_header
)
63 tmp
= &event
->stream
->stream_event_header
->p
;
65 case BT_STREAM_EVENT_CONTEXT
:
68 if (event
->stream
->stream_event_context
)
69 tmp
= &event
->stream
->stream_event_context
->p
;
71 case BT_EVENT_CONTEXT
:
74 if (event
->event
->event_context
)
75 tmp
= &event
->event
->event_context
->p
;
80 if (event
->event
->event_fields
)
81 tmp
= &event
->event
->event_fields
->p
;
90 const struct definition
*bt_ctf_get_field(const struct bt_ctf_event
*event
,
91 const struct definition
*scope
,
94 struct definition
*def
;
95 char *field_underscore
;
98 def
= lookup_definition(scope
, field
);
100 * optionally a field can have an underscore prefix, try
101 * to lookup the field with this prefix if it failed
104 field_underscore
= g_new(char, strlen(field
) + 2);
105 field_underscore
[0] = '_';
106 strcpy(&field_underscore
[1], field
);
107 def
= lookup_definition(scope
, field_underscore
);
108 g_free(field_underscore
);
110 if (bt_ctf_field_type(def
) == CTF_TYPE_VARIANT
) {
111 struct definition_variant
*variant_definition
;
112 variant_definition
= container_of(def
,
113 struct definition_variant
, p
);
114 return variant_definition
->current_field
;
121 const struct definition
*bt_ctf_get_index(const struct bt_ctf_event
*event
,
122 const struct definition
*field
,
125 struct definition
*ret
= NULL
;
127 if (bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
128 struct definition_array
*array_definition
;
129 array_definition
= container_of(field
,
130 struct definition_array
, p
);
131 ret
= array_index(array_definition
, index
);
132 } else if (bt_ctf_field_type(field
) == CTF_TYPE_SEQUENCE
) {
133 struct definition_sequence
*sequence_definition
;
134 sequence_definition
= container_of(field
,
135 struct definition_sequence
, p
);
136 ret
= sequence_index(sequence_definition
, index
);
141 const char *bt_ctf_event_name(const struct bt_ctf_event
*event
)
143 struct ctf_event
*event_class
;
144 struct ctf_stream_class
*stream_class
;
148 stream_class
= event
->stream
->stream_class
;
149 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
150 event
->stream
->event_id
);
151 return g_quark_to_string(event_class
->name
);
154 const char *bt_ctf_field_name(const struct definition
*def
)
157 return rem_(g_quark_to_string(def
->name
));
161 enum ctf_type_id
bt_ctf_field_type(const struct definition
*def
)
164 return def
->declaration
->id
;
165 return CTF_TYPE_UNKNOWN
;
168 int bt_ctf_get_field_list(const struct bt_ctf_event
*event
,
169 const struct definition
*scope
,
170 struct definition
const * const **list
,
173 switch (bt_ctf_field_type(scope
)) {
174 case CTF_TYPE_INTEGER
:
176 case CTF_TYPE_STRING
:
179 case CTF_TYPE_STRUCT
:
181 const struct definition_struct
*def_struct
;
183 def_struct
= container_of(scope
, const struct definition_struct
, p
);
186 if (def_struct
->fields
->pdata
) {
187 *list
= (struct definition
const* const*) def_struct
->fields
->pdata
;
188 *count
= def_struct
->fields
->len
;
194 case CTF_TYPE_UNTAGGED_VARIANT
:
196 case CTF_TYPE_VARIANT
:
198 const struct definition_variant
*def_variant
;
200 def_variant
= container_of(scope
, const struct definition_variant
, p
);
203 if (def_variant
->fields
->pdata
) {
204 *list
= (struct definition
const* const*) def_variant
->fields
->pdata
;
205 *count
= def_variant
->fields
->len
;
213 const struct definition_array
*def_array
;
215 def_array
= container_of(scope
, const struct definition_array
, p
);
218 if (def_array
->elems
->pdata
) {
219 *list
= (struct definition
const* const*) def_array
->elems
->pdata
;
220 *count
= def_array
->elems
->len
;
226 case CTF_TYPE_SEQUENCE
:
228 const struct definition_sequence
*def_sequence
;
230 def_sequence
= container_of(scope
, const struct definition_sequence
, p
);
233 if (def_sequence
->elems
->pdata
) {
234 *list
= (struct definition
const* const*) def_sequence
->elems
->pdata
;
235 *count
= def_sequence
->elems
->len
;
254 struct bt_context
*bt_ctf_event_get_context(const struct bt_ctf_event
*event
)
256 struct bt_context
*ret
= NULL
;
257 struct ctf_file_stream
*cfs
;
258 struct ctf_stream
*stream
;
259 struct ctf_stream_class
*stream_class
;
260 struct ctf_trace
*trace
;
262 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
264 stream
= &cfs
->parent
;
265 stream_class
= stream
->stream_class
;
266 trace
= stream_class
->trace
;
274 int bt_ctf_event_get_handle_id(const struct bt_ctf_event
*event
)
277 struct ctf_file_stream
*cfs
;
278 struct ctf_stream
*stream
;
279 struct ctf_stream_class
*stream_class
;
280 struct ctf_trace
*trace
;
282 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
284 stream
= &cfs
->parent
;
285 stream_class
= stream
->stream_class
;
286 trace
= stream_class
->trace
;
289 ret
= trace
->handle
->id
;
294 uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event
*event
)
296 if (event
&& event
->stream
->has_timestamp
)
297 return ctf_get_timestamp_raw(event
->stream
,
298 event
->stream
->timestamp
);
303 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event
*event
)
305 if (event
&& event
->stream
->has_timestamp
)
306 return ctf_get_timestamp(event
->stream
,
307 event
->stream
->timestamp
);
312 static void bt_ctf_field_set_error(int error
)
314 bt_ctf_last_field_error
= error
;
317 int bt_ctf_field_get_error(void)
320 ret
= bt_ctf_last_field_error
;
321 bt_ctf_last_field_error
= 0;
326 int bt_ctf_get_int_signedness(const struct definition
*field
)
330 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
331 ret
= get_int_signedness(field
);
334 bt_ctf_field_set_error(-EINVAL
);
340 int bt_ctf_get_int_base(const struct definition
*field
)
344 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
345 ret
= get_int_base(field
);
348 bt_ctf_field_set_error(-EINVAL
);
354 int bt_ctf_get_int_byte_order(const struct definition
*field
)
358 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
359 ret
= get_int_byte_order(field
);
362 bt_ctf_field_set_error(-EINVAL
);
368 enum ctf_string_encoding
bt_ctf_get_encoding(const struct definition
*field
)
370 enum ctf_string_encoding ret
= 0;
375 if (bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
376 ret
= get_int_encoding(field
);
377 else if (bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
378 ret
= get_string_encoding(field
);
386 bt_ctf_field_set_error(-EINVAL
);
390 int bt_ctf_get_array_len(const struct definition
*field
)
394 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
395 ret
= get_array_len(field
);
398 bt_ctf_field_set_error(-EINVAL
);
404 uint64_t bt_ctf_get_uint64(const struct definition
*field
)
406 unsigned int ret
= 0;
408 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
409 ret
= get_unsigned_int(field
);
411 bt_ctf_field_set_error(-EINVAL
);
416 int64_t bt_ctf_get_int64(const struct definition
*field
)
420 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
421 ret
= get_signed_int(field
);
423 bt_ctf_field_set_error(-EINVAL
);
429 char *bt_ctf_get_char_array(const struct definition
*field
)
433 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
434 ret
= get_char_array(field
)->str
;
436 bt_ctf_field_set_error(-EINVAL
);
441 char *bt_ctf_get_string(const struct definition
*field
)
445 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
446 ret
= get_string(field
);
448 bt_ctf_field_set_error(-EINVAL
);