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 ctf_event_definition
*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
:
72 if (event
->event_context
)
73 tmp
= &event
->event_context
->p
;
76 if (event
->event_fields
)
77 tmp
= &event
->event_fields
->p
;
86 const struct definition
*bt_ctf_get_field(const struct ctf_event_definition
*event
,
87 const struct definition
*scope
,
90 struct definition
*def
;
91 char *field_underscore
;
94 def
= lookup_definition(scope
, field
);
96 * optionally a field can have an underscore prefix, try
97 * to lookup the field with this prefix if it failed
100 field_underscore
= g_new(char, strlen(field
) + 2);
101 field_underscore
[0] = '_';
102 strcpy(&field_underscore
[1], field
);
103 def
= lookup_definition(scope
, field_underscore
);
104 g_free(field_underscore
);
106 if (bt_ctf_field_type(def
) == CTF_TYPE_VARIANT
) {
107 struct definition_variant
*variant_definition
;
108 variant_definition
= container_of(def
,
109 struct definition_variant
, p
);
110 return variant_definition
->current_field
;
117 const struct definition
*bt_ctf_get_index(const struct ctf_event_definition
*event
,
118 const struct definition
*field
,
121 struct definition
*ret
= NULL
;
123 if (bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
124 struct definition_array
*array_definition
;
125 array_definition
= container_of(field
,
126 struct definition_array
, p
);
127 ret
= array_index(array_definition
, index
);
128 } else if (bt_ctf_field_type(field
) == CTF_TYPE_SEQUENCE
) {
129 struct definition_sequence
*sequence_definition
;
130 sequence_definition
= container_of(field
,
131 struct definition_sequence
, p
);
132 ret
= sequence_index(sequence_definition
, index
);
137 const char *bt_ctf_event_name(const struct ctf_event_definition
*event
)
139 struct ctf_event_declaration
*event_class
;
140 struct ctf_stream_declaration
*stream_class
;
144 stream_class
= event
->stream
->stream_class
;
145 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
146 event
->stream
->event_id
);
147 return g_quark_to_string(event_class
->name
);
150 const char *bt_ctf_field_name(const struct definition
*def
)
153 return rem_(g_quark_to_string(def
->name
));
157 enum ctf_type_id
bt_ctf_field_type(const struct definition
*def
)
160 return def
->declaration
->id
;
161 return CTF_TYPE_UNKNOWN
;
164 int bt_ctf_get_field_list(const struct ctf_event_definition
*event
,
165 const struct definition
*scope
,
166 struct definition
const * const **list
,
169 switch (bt_ctf_field_type(scope
)) {
170 case CTF_TYPE_INTEGER
:
172 case CTF_TYPE_STRING
:
175 case CTF_TYPE_STRUCT
:
177 const struct definition_struct
*def_struct
;
179 def_struct
= container_of(scope
, const struct definition_struct
, p
);
182 if (def_struct
->fields
->pdata
) {
183 *list
= (struct definition
const* const*) def_struct
->fields
->pdata
;
184 *count
= def_struct
->fields
->len
;
190 case CTF_TYPE_UNTAGGED_VARIANT
:
192 case CTF_TYPE_VARIANT
:
194 const struct definition_variant
*def_variant
;
196 def_variant
= container_of(scope
, const struct definition_variant
, p
);
199 if (def_variant
->fields
->pdata
) {
200 *list
= (struct definition
const* const*) def_variant
->fields
->pdata
;
201 *count
= def_variant
->fields
->len
;
209 const struct definition_array
*def_array
;
211 def_array
= container_of(scope
, const struct definition_array
, p
);
214 if (def_array
->elems
->pdata
) {
215 *list
= (struct definition
const* const*) def_array
->elems
->pdata
;
216 *count
= def_array
->elems
->len
;
222 case CTF_TYPE_SEQUENCE
:
224 const struct definition_sequence
*def_sequence
;
226 def_sequence
= container_of(scope
, const struct definition_sequence
, p
);
229 if (def_sequence
->elems
->pdata
) {
230 *list
= (struct definition
const* const*) def_sequence
->elems
->pdata
;
231 *count
= def_sequence
->elems
->len
;
250 struct bt_context
*bt_ctf_event_get_context(const struct ctf_event_definition
*event
)
252 struct bt_context
*ret
= NULL
;
253 struct ctf_file_stream
*cfs
;
254 struct ctf_trace
*trace
;
256 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
258 trace
= cfs
->parent
.stream_class
->trace
;
265 int bt_ctf_event_get_handle_id(const struct ctf_event_definition
*event
)
268 struct ctf_file_stream
*cfs
;
269 struct ctf_trace
*trace
;
271 cfs
= container_of(event
->stream
, struct ctf_file_stream
,
273 trace
= cfs
->parent
.stream_class
->trace
;
275 ret
= trace
->handle
->id
;
280 uint64_t bt_ctf_get_timestamp_raw(const struct ctf_event_definition
*event
)
282 if (event
&& event
->stream
->has_timestamp
)
283 return ctf_get_timestamp_raw(event
->stream
,
284 event
->stream
->timestamp
);
289 uint64_t bt_ctf_get_timestamp(const struct ctf_event_definition
*event
)
291 if (event
&& event
->stream
->has_timestamp
)
292 return ctf_get_timestamp(event
->stream
,
293 event
->stream
->timestamp
);
298 static void bt_ctf_field_set_error(int error
)
300 bt_ctf_last_field_error
= error
;
303 int bt_ctf_field_get_error(void)
306 ret
= bt_ctf_last_field_error
;
307 bt_ctf_last_field_error
= 0;
312 int bt_ctf_get_int_signedness(const struct definition
*field
)
316 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
317 ret
= get_int_signedness(field
);
320 bt_ctf_field_set_error(-EINVAL
);
326 int bt_ctf_get_int_base(const struct definition
*field
)
330 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
331 ret
= get_int_base(field
);
334 bt_ctf_field_set_error(-EINVAL
);
340 int bt_ctf_get_int_byte_order(const struct definition
*field
)
344 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
) {
345 ret
= get_int_byte_order(field
);
348 bt_ctf_field_set_error(-EINVAL
);
354 enum ctf_string_encoding
bt_ctf_get_encoding(const struct definition
*field
)
356 enum ctf_string_encoding ret
= 0;
361 if (bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
362 ret
= get_int_encoding(field
);
363 else if (bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
364 ret
= get_string_encoding(field
);
372 bt_ctf_field_set_error(-EINVAL
);
376 int bt_ctf_get_array_len(const struct definition
*field
)
380 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
381 ret
= get_array_len(field
);
384 bt_ctf_field_set_error(-EINVAL
);
390 uint64_t bt_ctf_get_uint64(const struct definition
*field
)
392 unsigned int ret
= 0;
394 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
395 ret
= get_unsigned_int(field
);
397 bt_ctf_field_set_error(-EINVAL
);
402 int64_t bt_ctf_get_int64(const struct definition
*field
)
406 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
407 ret
= get_signed_int(field
);
409 bt_ctf_field_set_error(-EINVAL
);
415 char *bt_ctf_get_char_array(const struct definition
*field
)
419 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
420 ret
= get_char_array(field
)->str
;
422 bt_ctf_field_set_error(-EINVAL
);
427 char *bt_ctf_get_string(const struct definition
*field
)
431 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
432 ret
= get_string(field
);
434 bt_ctf_field_set_error(-EINVAL
);