Fix API: add const qualifiers, privatize struct bt_ctf_event
[babeltrace.git] / formats / ctf / events.c
1 /*
2 * ctf/events.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
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>
30 #include <glib.h>
31
32 #include "events-private.h"
33
34 /*
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
38 */
39 __thread int bt_ctf_last_field_error = 0;
40
41 const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *event,
42 enum bt_ctf_scope scope)
43 {
44 struct definition *tmp = NULL;
45
46 switch (scope) {
47 case BT_TRACE_PACKET_HEADER:
48 if (!event->stream)
49 goto error;
50 if (event->stream->trace_packet_header)
51 tmp = &event->stream->trace_packet_header->p;
52 break;
53 case BT_STREAM_PACKET_CONTEXT:
54 if (!event->stream)
55 goto error;
56 if (event->stream->stream_packet_context)
57 tmp = &event->stream->stream_packet_context->p;
58 break;
59 case BT_STREAM_EVENT_HEADER:
60 if (!event->stream)
61 goto error;
62 if (event->stream->stream_event_header)
63 tmp = &event->stream->stream_event_header->p;
64 break;
65 case BT_STREAM_EVENT_CONTEXT:
66 if (!event->stream)
67 goto error;
68 if (event->stream->stream_event_context)
69 tmp = &event->stream->stream_event_context->p;
70 break;
71 case BT_EVENT_CONTEXT:
72 if (!event->event)
73 goto error;
74 if (event->event->event_context)
75 tmp = &event->event->event_context->p;
76 break;
77 case BT_EVENT_FIELDS:
78 if (!event->event)
79 goto error;
80 if (event->event->event_fields)
81 tmp = &event->event->event_fields->p;
82 break;
83 }
84 return tmp;
85
86 error:
87 return NULL;
88 }
89
90 const struct definition *bt_ctf_get_field(const struct bt_ctf_event *event,
91 const struct definition *scope,
92 const char *field)
93 {
94 struct definition *def;
95 char *field_underscore;
96
97 if (scope) {
98 def = lookup_definition(scope, field);
99 /*
100 * optionally a field can have an underscore prefix, try
101 * to lookup the field with this prefix if it failed
102 */
103 if (!def) {
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);
109 }
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;
115 }
116 return def;
117 }
118 return NULL;
119 }
120
121 const struct definition *bt_ctf_get_index(const struct bt_ctf_event *event,
122 const struct definition *field,
123 unsigned int index)
124 {
125 struct definition *ret = NULL;
126
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);
137 }
138 return ret;
139 }
140
141 const char *bt_ctf_event_name(const struct bt_ctf_event *event)
142 {
143 struct ctf_event *event_class;
144 struct ctf_stream_class *stream_class;
145
146 if (!event)
147 return NULL;
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);
152 }
153
154 const char *bt_ctf_field_name(const struct definition *def)
155 {
156 if (def)
157 return rem_(g_quark_to_string(def->name));
158 return NULL;
159 }
160
161 enum ctf_type_id bt_ctf_field_type(const struct definition *def)
162 {
163 if (def)
164 return def->declaration->id;
165 return CTF_TYPE_UNKNOWN;
166 }
167
168 int bt_ctf_get_field_list(const struct bt_ctf_event *event,
169 const struct definition *scope,
170 struct definition const * const **list,
171 unsigned int *count)
172 {
173 switch (bt_ctf_field_type(scope)) {
174 case CTF_TYPE_INTEGER:
175 case CTF_TYPE_FLOAT:
176 case CTF_TYPE_STRING:
177 case CTF_TYPE_ENUM:
178 goto error;
179 case CTF_TYPE_STRUCT:
180 {
181 const struct definition_struct *def_struct;
182
183 def_struct = container_of(scope, const struct definition_struct, p);
184 if (!def_struct)
185 goto error;
186 if (def_struct->fields->pdata) {
187 *list = (struct definition const* const*) def_struct->fields->pdata;
188 *count = def_struct->fields->len;
189 goto end;
190 } else {
191 goto error;
192 }
193 }
194 case CTF_TYPE_UNTAGGED_VARIANT:
195 goto error;
196 case CTF_TYPE_VARIANT:
197 {
198 const struct definition_variant *def_variant;
199
200 def_variant = container_of(scope, const struct definition_variant, p);
201 if (!def_variant)
202 goto error;
203 if (def_variant->fields->pdata) {
204 *list = (struct definition const* const*) def_variant->fields->pdata;
205 *count = def_variant->fields->len;
206 goto end;
207 } else {
208 goto error;
209 }
210 }
211 case CTF_TYPE_ARRAY:
212 {
213 const struct definition_array *def_array;
214
215 def_array = container_of(scope, const struct definition_array, p);
216 if (!def_array)
217 goto error;
218 if (def_array->elems->pdata) {
219 *list = (struct definition const* const*) def_array->elems->pdata;
220 *count = def_array->elems->len;
221 goto end;
222 } else {
223 goto error;
224 }
225 }
226 case CTF_TYPE_SEQUENCE:
227 {
228 const struct definition_sequence *def_sequence;
229
230 def_sequence = container_of(scope, const struct definition_sequence, p);
231 if (!def_sequence)
232 goto error;
233 if (def_sequence->elems->pdata) {
234 *list = (struct definition const* const*) def_sequence->elems->pdata;
235 *count = def_sequence->elems->len;
236 goto end;
237 } else {
238 goto error;
239 }
240 }
241 default:
242 break;
243 }
244
245 end:
246 return 0;
247
248 error:
249 *list = NULL;
250 *count = 0;
251 return -1;
252 }
253
254 uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event *event)
255 {
256 if (event && event->stream->has_timestamp)
257 return ctf_get_timestamp_raw(event->stream,
258 event->stream->timestamp);
259 else
260 return -1ULL;
261 }
262
263 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *event)
264 {
265 if (event && event->stream->has_timestamp)
266 return ctf_get_timestamp(event->stream,
267 event->stream->timestamp);
268 else
269 return -1ULL;
270 }
271
272 static void bt_ctf_field_set_error(int error)
273 {
274 bt_ctf_last_field_error = error;
275 }
276
277 int bt_ctf_field_get_error(void)
278 {
279 int ret;
280 ret = bt_ctf_last_field_error;
281 bt_ctf_last_field_error = 0;
282
283 return ret;
284 }
285
286 int bt_ctf_get_int_signedness(const struct definition *field)
287 {
288 int ret;
289
290 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
291 ret = get_int_signedness(field);
292 } else {
293 ret = -1;
294 bt_ctf_field_set_error(-EINVAL);
295 }
296
297 return ret;
298 }
299
300 int bt_ctf_get_int_base(const struct definition *field)
301 {
302 int ret;
303
304 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
305 ret = get_int_base(field);
306 } else {
307 ret = -1;
308 bt_ctf_field_set_error(-EINVAL);
309 }
310
311 return ret;
312 }
313
314 int bt_ctf_get_int_byte_order(const struct definition *field)
315 {
316 int ret;
317
318 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
319 ret = get_int_byte_order(field);
320 } else {
321 ret = -1;
322 bt_ctf_field_set_error(-EINVAL);
323 }
324
325 return ret;
326 }
327
328 enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
329 {
330 enum ctf_string_encoding ret = 0;
331
332 if (!field)
333 goto end;
334
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);
339 else
340 goto error;
341
342 end:
343 return ret;
344
345 error:
346 bt_ctf_field_set_error(-EINVAL);
347 return -1;
348 }
349
350 int bt_ctf_get_array_len(const struct definition *field)
351 {
352 int ret;
353
354 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
355 ret = get_array_len(field);
356 } else {
357 ret = -1;
358 bt_ctf_field_set_error(-EINVAL);
359 }
360
361 return ret;
362 }
363
364 uint64_t bt_ctf_get_uint64(const struct definition *field)
365 {
366 unsigned int ret = 0;
367
368 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
369 ret = get_unsigned_int(field);
370 else
371 bt_ctf_field_set_error(-EINVAL);
372
373 return ret;
374 }
375
376 int64_t bt_ctf_get_int64(const struct definition *field)
377 {
378 int ret = 0;
379
380 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
381 ret = get_signed_int(field);
382 else
383 bt_ctf_field_set_error(-EINVAL);
384
385 return ret;
386
387 }
388
389 char *bt_ctf_get_char_array(const struct definition *field)
390 {
391 char *ret = NULL;
392
393 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY)
394 ret = get_char_array(field)->str;
395 else
396 bt_ctf_field_set_error(-EINVAL);
397
398 return ret;
399 }
400
401 char *bt_ctf_get_string(const struct definition *field)
402 {
403 char *ret = NULL;
404
405 if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING)
406 ret = get_string(field);
407 else
408 bt_ctf_field_set_error(-EINVAL);
409
410 return ret;
411 }
This page took 0.036465 seconds and 4 git commands to generate.