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 uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event
*event
)
256 if (event
&& event
->stream
->has_timestamp
)
257 return ctf_get_timestamp_raw(event
->stream
,
258 event
->stream
->timestamp
);
263 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event
*event
)
265 if (event
&& event
->stream
->has_timestamp
)
266 return ctf_get_timestamp(event
->stream
,
267 event
->stream
->timestamp
);
272 static void bt_ctf_field_set_error(int error
)
274 bt_ctf_last_field_error
= error
;
277 int bt_ctf_field_get_error(void)
280 ret
= bt_ctf_last_field_error
;
281 bt_ctf_last_field_error
= 0;
286 int bt_ctf_get_int_signedness(const struct definition
*field
)
290 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
291 ret
= get_int_signedness(field
);
294 bt_ctf_field_set_error(-EINVAL
);
300 int bt_ctf_get_int_base(const struct definition
*field
)
304 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
305 ret
= get_int_base(field
);
308 bt_ctf_field_set_error(-EINVAL
);
314 int bt_ctf_get_int_byte_order(const struct definition
*field
)
318 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
319 ret
= get_int_byte_order(field
);
322 bt_ctf_field_set_error(-EINVAL
);
328 enum ctf_string_encoding
bt_ctf_get_encoding(const struct definition
*field
)
330 enum ctf_string_encoding ret
= 0;
335 if (bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
336 ret
= get_int_encoding(field
);
337 else if (bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
338 ret
= get_string_encoding(field
);
346 bt_ctf_field_set_error(-EINVAL
);
350 int bt_ctf_get_array_len(const struct definition
*field
)
354 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
355 ret
= get_array_len(field
);
358 bt_ctf_field_set_error(-EINVAL
);
364 uint64_t bt_ctf_get_uint64(const struct definition
*field
)
366 unsigned int ret
= 0;
368 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
369 ret
= get_unsigned_int(field
);
371 bt_ctf_field_set_error(-EINVAL
);
376 int64_t bt_ctf_get_int64(const struct definition
*field
)
380 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
381 ret
= get_signed_int(field
);
383 bt_ctf_field_set_error(-EINVAL
);
389 char *bt_ctf_get_char_array(const struct definition
*field
)
393 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
394 ret
= get_char_array(field
)->str
;
396 bt_ctf_field_set_error(-EINVAL
);
401 char *bt_ctf_get_string(const struct definition
*field
)
405 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
406 ret
= get_string(field
);
408 bt_ctf_field_set_error(-EINVAL
);