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>
29 * thread local storage to store the last error that occured
30 * while reading a field, this variable must be accessed by
31 * bt_ctf_field_error only
33 __thread
int bt_ctf_last_field_error
= 0;
35 struct definition
*bt_ctf_get_top_level_scope(struct bt_ctf_event
*event
,
36 enum bt_ctf_scope scope
)
38 struct definition
*tmp
= NULL
;
41 case BT_TRACE_PACKET_HEADER
:
44 if (event
->stream
->trace_packet_header
)
45 tmp
= &event
->stream
->trace_packet_header
->p
;
47 case BT_STREAM_PACKET_CONTEXT
:
50 if (event
->stream
->stream_packet_context
)
51 tmp
= &event
->stream
->stream_packet_context
->p
;
53 case BT_STREAM_EVENT_HEADER
:
56 if (event
->stream
->stream_event_header
)
57 tmp
= &event
->stream
->stream_event_header
->p
;
59 case BT_STREAM_EVENT_CONTEXT
:
62 if (event
->stream
->stream_event_context
)
63 tmp
= &event
->stream
->stream_event_context
->p
;
65 case BT_EVENT_CONTEXT
:
68 if (event
->event
->event_context
)
69 tmp
= &event
->event
->event_context
->p
;
74 if (event
->event
->event_fields
)
75 tmp
= &event
->event
->event_fields
->p
;
84 struct definition
*bt_ctf_get_field(struct bt_ctf_event
*event
,
85 struct definition
*scope
,
88 struct definition
*def
;
91 def
= lookup_definition(scope
, field
);
92 if (bt_ctf_field_type(def
) == CTF_TYPE_VARIANT
) {
93 struct definition_variant
*variant_definition
;
94 variant_definition
= container_of(def
,
95 struct definition_variant
, p
);
96 return variant_definition
->current_field
;
103 struct definition
*bt_ctf_get_index(struct bt_ctf_event
*event
,
104 struct definition
*field
,
107 struct definition
*ret
= NULL
;
109 if (bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
) {
110 struct definition_array
*array_definition
;
111 array_definition
= container_of(field
,
112 struct definition_array
, p
);
113 ret
= array_index(array_definition
, index
);
114 } else if (bt_ctf_field_type(field
) == CTF_TYPE_SEQUENCE
) {
115 struct definition_sequence
*sequence_definition
;
116 sequence_definition
= container_of(field
,
117 struct definition_sequence
, p
);
118 ret
= sequence_index(sequence_definition
, index
);
123 const char *bt_ctf_event_name(struct bt_ctf_event
*event
)
125 struct ctf_event
*event_class
;
126 struct ctf_stream_class
*stream_class
;
130 stream_class
= event
->stream
->stream_class
;
131 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
132 event
->stream
->event_id
);
133 return g_quark_to_string(event_class
->name
);
136 const char *bt_ctf_field_name(const struct definition
*def
)
139 return g_quark_to_string(def
->name
);
143 enum ctf_type_id
bt_ctf_field_type(struct definition
*def
)
146 return def
->declaration
->id
;
147 return CTF_TYPE_UNKNOWN
;
150 int bt_ctf_get_field_list(struct bt_ctf_event
*event
,
151 struct definition
*scope
,
152 struct definition
const * const **list
,
155 switch (bt_ctf_field_type(scope
)) {
156 case CTF_TYPE_INTEGER
:
158 case CTF_TYPE_STRING
:
161 case CTF_TYPE_STRUCT
:
163 struct definition_struct
*def_struct
;
165 def_struct
= container_of(scope
, struct definition_struct
, p
);
168 if (def_struct
->fields
->pdata
) {
169 *list
= (struct definition
const* const*) def_struct
->fields
->pdata
;
170 *count
= def_struct
->fields
->len
;
176 case CTF_TYPE_UNTAGGED_VARIANT
:
178 case CTF_TYPE_VARIANT
:
180 struct definition_variant
*def_variant
;
182 def_variant
= container_of(scope
, struct definition_variant
, p
);
185 if (def_variant
->fields
->pdata
) {
186 *list
= (struct definition
const* const*) def_variant
->fields
->pdata
;
187 *count
= def_variant
->fields
->len
;
195 struct definition_array
*def_array
;
197 def_array
= container_of(scope
, struct definition_array
, p
);
200 if (def_array
->elems
->pdata
) {
201 *list
= (struct definition
const* const*) def_array
->elems
->pdata
;
202 *count
= def_array
->elems
->len
;
208 case CTF_TYPE_SEQUENCE
:
210 struct definition_sequence
*def_sequence
;
212 def_sequence
= container_of(scope
, struct definition_sequence
, p
);
215 if (def_sequence
->elems
->pdata
) {
216 *list
= (struct definition
const* const*) def_sequence
->elems
->pdata
;
217 *count
= def_sequence
->elems
->len
;
236 uint64_t bt_ctf_get_timestamp(struct bt_ctf_event
*event
)
238 if (event
&& event
->stream
->has_timestamp
)
239 return event
->stream
->timestamp
;
244 static void bt_ctf_field_set_error(int error
)
246 bt_ctf_last_field_error
= error
;
249 int bt_ctf_field_get_error(void)
252 ret
= bt_ctf_last_field_error
;
253 bt_ctf_last_field_error
= 0;
258 uint64_t bt_ctf_get_uint64(struct definition
*field
)
260 unsigned int ret
= 0;
262 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
263 ret
= get_unsigned_int(field
);
265 bt_ctf_field_set_error(-EINVAL
);
270 int64_t bt_ctf_get_int64(struct definition
*field
)
274 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_INTEGER
)
275 ret
= get_signed_int(field
);
277 bt_ctf_field_set_error(-EINVAL
);
283 char *bt_ctf_get_char_array(struct definition
*field
)
287 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_ARRAY
)
288 ret
= get_char_array(field
)->str
;
290 bt_ctf_field_set_error(-EINVAL
);
295 char *bt_ctf_get_string(struct definition
*field
)
299 if (field
&& bt_ctf_field_type(field
) == CTF_TYPE_STRING
)
300 ret
= get_string(field
);
302 bt_ctf_field_set_error(-EINVAL
);