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
*ctf_event
,
42 enum bt_ctf_scope scope
)
44 struct definition
*tmp
= NULL
;
45 struct ctf_event_definition
*event
= ctf_event
->parent
;
48 case BT_TRACE_PACKET_HEADER
:
51 if (event
->stream
->trace_packet_header
)
52 tmp
= &event
->stream
->trace_packet_header
->p
;
54 case BT_STREAM_PACKET_CONTEXT
:
57 if (event
->stream
->stream_packet_context
)
58 tmp
= &event
->stream
->stream_packet_context
->p
;
60 case BT_STREAM_EVENT_HEADER
:
63 if (event
->stream
->stream_event_header
)
64 tmp
= &event
->stream
->stream_event_header
->p
;
66 case BT_STREAM_EVENT_CONTEXT
:
69 if (event
->stream
->stream_event_context
)
70 tmp
= &event
->stream
->stream_event_context
->p
;
72 case BT_EVENT_CONTEXT
:
73 if (event
->event_context
)
74 tmp
= &event
->event_context
->p
;
77 if (event
->event_fields
)
78 tmp
= &event
->event_fields
->p
;
87 const struct definition
*bt_ctf_get_field(const struct bt_ctf_event
*ctf_event
,
88 const struct definition
*scope
,
91 struct definition
*def
;
92 char *field_underscore
;
95 def
= lookup_definition(scope
, field
);
97 * optionally a field can have an underscore prefix, try
98 * to lookup the field with this prefix if it failed
101 field_underscore
= g_new(char, strlen(field
) + 2);
102 field_underscore
[0] = '_';
103 strcpy(&field_underscore
[1], field
);
104 def
= lookup_definition(scope
, field_underscore
);
105 g_free(field_underscore
);
107 if (bt_ctf_field_type(def
) == CTF_TYPE_VARIANT
) {
108 struct definition_variant
*variant_definition
;
109 variant_definition
= container_of(def
,
110 struct definition_variant
, p
);
111 return variant_definition
->current_field
;
118 const struct definition
*bt_ctf_get_index(const struct bt_ctf_event
*ctf_event
,
119 const struct definition
*field
,
122 struct definition
*ret
= NULL
;
124 if (bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
125 struct definition_array
*array_definition
;
126 array_definition
= container_of(field
,
127 struct definition_array
, p
);
128 ret
= array_index(array_definition
, index
);
129 } else if (bt_ctf_field_type(field
) == CTF_TYPE_SEQUENCE
) {
130 struct definition_sequence
*sequence_definition
;
131 sequence_definition
= container_of(field
,
132 struct definition_sequence
, p
);
133 ret
= sequence_index(sequence_definition
, index
);
138 const char *bt_ctf_event_name(const struct bt_ctf_event
*ctf_event
)
140 struct ctf_event_declaration
*event_class
;
141 struct ctf_stream_declaration
*stream_class
;
142 struct ctf_event_definition
*event
= ctf_event
->parent
;
146 stream_class
= event
->stream
->stream_class
;
147 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
148 event
->stream
->event_id
);
149 return g_quark_to_string(event_class
->name
);
152 const char *bt_ctf_field_name(const struct definition
*def
)
155 return rem_(g_quark_to_string(def
->name
));
159 enum ctf_type_id
bt_ctf_field_type(const struct definition
*def
)
162 return def
->declaration
->id
;
163 return CTF_TYPE_UNKNOWN
;
166 int bt_ctf_get_field_list(const struct bt_ctf_event
*ctf_event
,
167 const struct definition
*scope
,
168 struct definition
const * const **list
,
171 switch (bt_ctf_field_type(scope
)) {
172 case CTF_TYPE_INTEGER
:
174 case CTF_TYPE_STRING
:
177 case CTF_TYPE_STRUCT
:
179 const struct definition_struct
*def_struct
;
181 def_struct
= container_of(scope
, const struct definition_struct
, p
);
184 if (def_struct
->fields
->pdata
) {
185 *list
= (struct definition
const* const*) def_struct
->fields
->pdata
;
186 *count
= def_struct
->fields
->len
;
192 case CTF_TYPE_UNTAGGED_VARIANT
:
194 case CTF_TYPE_VARIANT
:
196 const struct definition_variant
*def_variant
;
198 def_variant
= container_of(scope
, const struct definition_variant
, p
);
201 if (def_variant
->fields
->pdata
) {
202 *list
= (struct definition
const* const*) def_variant
->fields
->pdata
;
203 *count
= def_variant
->fields
->len
;
211 const struct definition_array
*def_array
;
213 def_array
= container_of(scope
, const struct definition_array
, p
);
216 if (def_array
->elems
->pdata
) {
217 *list
= (struct definition
const* const*) def_array
->elems
->pdata
;
218 *count
= def_array
->elems
->len
;
224 case CTF_TYPE_SEQUENCE
:
226 const struct definition_sequence
*def_sequence
;
228 def_sequence
= container_of(scope
, const struct definition_sequence
, p
);
231 if (def_sequence
->elems
->pdata
) {
232 *list
= (struct definition
const* const*) def_sequence
->elems
->pdata
;
233 *count
= def_sequence
->elems
->len
;
252 struct bt_context
*bt_ctf_event_get_context(const struct bt_ctf_event
*ctf_event
)
254 struct bt_context
*ret
= NULL
;
255 struct ctf_file_stream
*cfs
;
256 struct ctf_trace
*trace
;
257 struct ctf_event_definition
*event
= ctf_event
->parent
;
259 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
261 trace
= cfs
->parent
.stream_class
->trace
;
268 int bt_ctf_event_get_handle_id(const struct bt_ctf_event
*ctf_event
)
271 struct ctf_file_stream
*cfs
;
272 struct ctf_trace
*trace
;
273 struct ctf_event_definition
*event
= ctf_event
->parent
;
275 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
277 trace
= cfs
->parent
.stream_class
->trace
;
279 ret
= trace
->handle
->id
;
284 uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event
*ctf_event
)
286 struct ctf_event_definition
*event
= ctf_event
->parent
;
287 if (event
&& event
->stream
->has_timestamp
)
288 return ctf_get_timestamp_raw(event
->stream
,
289 event
->stream
->timestamp
);
294 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event
*ctf_event
)
296 struct ctf_event_definition
*event
= ctf_event
->parent
;
297 if (event
&& event
->stream
->has_timestamp
)
298 return ctf_get_timestamp(event
->stream
,
299 event
->stream
->timestamp
);
304 static void bt_ctf_field_set_error(int error
)
306 bt_ctf_last_field_error
= error
;
309 int bt_ctf_field_get_error(void)
312 ret
= bt_ctf_last_field_error
;
313 bt_ctf_last_field_error
= 0;
318 int bt_ctf_get_int_signedness(const struct definition
*field
)
322 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
323 ret
= get_int_signedness(field
);
326 bt_ctf_field_set_error(-EINVAL
);
332 int bt_ctf_get_int_base(const struct definition
*field
)
336 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
337 ret
= get_int_base(field
);
340 bt_ctf_field_set_error(-EINVAL
);
346 int bt_ctf_get_int_byte_order(const struct definition
*field
)
350 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
351 ret
= get_int_byte_order(field
);
354 bt_ctf_field_set_error(-EINVAL
);
360 enum ctf_string_encoding
bt_ctf_get_encoding(const struct definition
*field
)
362 enum ctf_string_encoding ret
= 0;
367 if (bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
368 ret
= get_int_encoding(field
);
369 else if (bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
370 ret
= get_string_encoding(field
);
378 bt_ctf_field_set_error(-EINVAL
);
382 int bt_ctf_get_array_len(const struct definition
*field
)
386 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
387 ret
= get_array_len(field
);
390 bt_ctf_field_set_error(-EINVAL
);
396 uint64_t bt_ctf_get_uint64(const struct definition
*field
)
398 unsigned int ret
= 0;
400 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
401 ret
= get_unsigned_int(field
);
403 bt_ctf_field_set_error(-EINVAL
);
408 int64_t bt_ctf_get_int64(const struct definition
*field
)
412 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
413 ret
= get_signed_int(field
);
415 bt_ctf_field_set_error(-EINVAL
);
421 char *bt_ctf_get_char_array(const struct definition
*field
)
425 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
426 ret
= get_char_array(field
)->str
;
428 bt_ctf_field_set_error(-EINVAL
);
433 char *bt_ctf_get_string(const struct definition
*field
)
437 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
438 ret
= get_string(field
);
440 bt_ctf_field_set_error(-EINVAL
);