API Fix : missing list fields of event decl
[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 *ctf_event,
42 enum bt_ctf_scope scope)
43 {
44 struct definition *tmp = NULL;
45 struct ctf_event_definition *event = ctf_event->parent;
46
47 switch (scope) {
48 case BT_TRACE_PACKET_HEADER:
49 if (!event->stream)
50 goto error;
51 if (event->stream->trace_packet_header)
52 tmp = &event->stream->trace_packet_header->p;
53 break;
54 case BT_STREAM_PACKET_CONTEXT:
55 if (!event->stream)
56 goto error;
57 if (event->stream->stream_packet_context)
58 tmp = &event->stream->stream_packet_context->p;
59 break;
60 case BT_STREAM_EVENT_HEADER:
61 if (!event->stream)
62 goto error;
63 if (event->stream->stream_event_header)
64 tmp = &event->stream->stream_event_header->p;
65 break;
66 case BT_STREAM_EVENT_CONTEXT:
67 if (!event->stream)
68 goto error;
69 if (event->stream->stream_event_context)
70 tmp = &event->stream->stream_event_context->p;
71 break;
72 case BT_EVENT_CONTEXT:
73 if (event->event_context)
74 tmp = &event->event_context->p;
75 break;
76 case BT_EVENT_FIELDS:
77 if (event->event_fields)
78 tmp = &event->event_fields->p;
79 break;
80 }
81 return tmp;
82
83 error:
84 return NULL;
85 }
86
87 const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
88 const struct definition *scope,
89 const char *field)
90 {
91 struct definition *def;
92 char *field_underscore;
93
94 if (scope) {
95 def = lookup_definition(scope, field);
96 /*
97 * optionally a field can have an underscore prefix, try
98 * to lookup the field with this prefix if it failed
99 */
100 if (!def) {
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);
106 }
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;
112 }
113 return def;
114 }
115 return NULL;
116 }
117
118 const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
119 const struct definition *field,
120 unsigned int index)
121 {
122 struct definition *ret = NULL;
123
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);
134 }
135 return ret;
136 }
137
138 const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
139 {
140 struct ctf_event_declaration *event_class;
141 struct ctf_stream_declaration *stream_class;
142 struct ctf_event_definition *event = ctf_event->parent;
143
144 if (!event)
145 return NULL;
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);
150 }
151
152 const char *bt_ctf_field_name(const struct definition *def)
153 {
154 if (def)
155 return rem_(g_quark_to_string(def->name));
156 return NULL;
157 }
158
159 enum ctf_type_id bt_ctf_field_type(const struct definition *def)
160 {
161 if (def)
162 return def->declaration->id;
163 return CTF_TYPE_UNKNOWN;
164 }
165
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,
169 unsigned int *count)
170 {
171 switch (bt_ctf_field_type(scope)) {
172 case CTF_TYPE_INTEGER:
173 case CTF_TYPE_FLOAT:
174 case CTF_TYPE_STRING:
175 case CTF_TYPE_ENUM:
176 goto error;
177 case CTF_TYPE_STRUCT:
178 {
179 const struct definition_struct *def_struct;
180
181 def_struct = container_of(scope, const struct definition_struct, p);
182 if (!def_struct)
183 goto error;
184 if (def_struct->fields->pdata) {
185 *list = (struct definition const* const*) def_struct->fields->pdata;
186 *count = def_struct->fields->len;
187 goto end;
188 } else {
189 goto error;
190 }
191 }
192 case CTF_TYPE_UNTAGGED_VARIANT:
193 goto error;
194 case CTF_TYPE_VARIANT:
195 {
196 const struct definition_variant *def_variant;
197
198 def_variant = container_of(scope, const struct definition_variant, p);
199 if (!def_variant)
200 goto error;
201 if (def_variant->fields->pdata) {
202 *list = (struct definition const* const*) def_variant->fields->pdata;
203 *count = def_variant->fields->len;
204 goto end;
205 } else {
206 goto error;
207 }
208 }
209 case CTF_TYPE_ARRAY:
210 {
211 const struct definition_array *def_array;
212
213 def_array = container_of(scope, const struct definition_array, p);
214 if (!def_array)
215 goto error;
216 if (def_array->elems->pdata) {
217 *list = (struct definition const* const*) def_array->elems->pdata;
218 *count = def_array->elems->len;
219 goto end;
220 } else {
221 goto error;
222 }
223 }
224 case CTF_TYPE_SEQUENCE:
225 {
226 const struct definition_sequence *def_sequence;
227
228 def_sequence = container_of(scope, const struct definition_sequence, p);
229 if (!def_sequence)
230 goto error;
231 if (def_sequence->elems->pdata) {
232 *list = (struct definition const* const*) def_sequence->elems->pdata;
233 *count = def_sequence->elems->len;
234 goto end;
235 } else {
236 goto error;
237 }
238 }
239 default:
240 break;
241 }
242
243 end:
244 return 0;
245
246 error:
247 *list = NULL;
248 *count = 0;
249 return -1;
250 }
251
252 struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
253 {
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;
258
259 cfs = container_of(event->stream, struct ctf_file_stream,
260 parent);
261 trace = cfs->parent.stream_class->trace;
262 if (trace->ctx)
263 ret = trace->ctx;
264
265 return ret;
266 }
267
268 int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
269 {
270 int ret = -1;
271 struct ctf_file_stream *cfs;
272 struct ctf_trace *trace;
273 struct ctf_event_definition *event = ctf_event->parent;
274
275 cfs = container_of(event->stream, struct ctf_file_stream,
276 parent);
277 trace = cfs->parent.stream_class->trace;
278 if (trace->handle)
279 ret = trace->handle->id;
280
281 return ret;
282 }
283
284 uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event *ctf_event)
285 {
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);
290 else
291 return -1ULL;
292 }
293
294 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
295 {
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);
300 else
301 return -1ULL;
302 }
303
304 static void bt_ctf_field_set_error(int error)
305 {
306 bt_ctf_last_field_error = error;
307 }
308
309 int bt_ctf_field_get_error(void)
310 {
311 int ret;
312 ret = bt_ctf_last_field_error;
313 bt_ctf_last_field_error = 0;
314
315 return ret;
316 }
317
318 int bt_ctf_get_int_signedness(const struct definition *field)
319 {
320 int ret;
321
322 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
323 ret = get_int_signedness(field);
324 } else {
325 ret = -1;
326 bt_ctf_field_set_error(-EINVAL);
327 }
328
329 return ret;
330 }
331
332 int bt_ctf_get_int_base(const struct definition *field)
333 {
334 int ret;
335
336 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
337 ret = get_int_base(field);
338 } else {
339 ret = -1;
340 bt_ctf_field_set_error(-EINVAL);
341 }
342
343 return ret;
344 }
345
346 int bt_ctf_get_int_byte_order(const struct definition *field)
347 {
348 int ret;
349
350 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) {
351 ret = get_int_byte_order(field);
352 } else {
353 ret = -1;
354 bt_ctf_field_set_error(-EINVAL);
355 }
356
357 return ret;
358 }
359
360 enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field)
361 {
362 enum ctf_string_encoding ret = 0;
363
364 if (!field)
365 goto end;
366
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);
371 else
372 goto error;
373
374 end:
375 return ret;
376
377 error:
378 bt_ctf_field_set_error(-EINVAL);
379 return -1;
380 }
381
382 int bt_ctf_get_array_len(const struct definition *field)
383 {
384 int ret;
385
386 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) {
387 ret = get_array_len(field);
388 } else {
389 ret = -1;
390 bt_ctf_field_set_error(-EINVAL);
391 }
392
393 return ret;
394 }
395
396 uint64_t bt_ctf_get_uint64(const struct definition *field)
397 {
398 unsigned int ret = 0;
399
400 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
401 ret = get_unsigned_int(field);
402 else
403 bt_ctf_field_set_error(-EINVAL);
404
405 return ret;
406 }
407
408 int64_t bt_ctf_get_int64(const struct definition *field)
409 {
410 int ret = 0;
411
412 if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER)
413 ret = get_signed_int(field);
414 else
415 bt_ctf_field_set_error(-EINVAL);
416
417 return ret;
418
419 }
420
421 char *bt_ctf_get_char_array(const struct definition *field)
422 {
423 char *ret = NULL;
424
425 if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY)
426 ret = get_char_array(field)->str;
427 else
428 bt_ctf_field_set_error(-EINVAL);
429
430 return ret;
431 }
432
433 char *bt_ctf_get_string(const struct definition *field)
434 {
435 char *ret = NULL;
436
437 if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING)
438 ret = get_string(field);
439 else
440 bt_ctf_field_set_error(-EINVAL);
441
442 return ret;
443 }
444
445 int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
446 struct bt_ctf_event_decl * const **list,
447 unsigned int *count)
448 {
449 struct bt_trace_handle *handle;
450 struct trace_descriptor *td;
451 struct ctf_trace *tin;
452
453 if (!ctx)
454 goto error;
455
456 handle = g_hash_table_lookup(ctx->trace_handles,
457 (gpointer) (unsigned long) handle_id);
458 if (!handle)
459 goto error;
460
461 td = handle->td;
462 tin = container_of(td, struct ctf_trace, parent);
463
464 *list = (struct bt_ctf_event_decl * const*) tin->event_declarations->pdata;
465 *count = tin->event_declarations->len;
466 return 0;
467
468 error:
469 return -1;
470 }
471
472 const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
473 {
474 if (!event)
475 return NULL;
476 return g_quark_to_string(event->parent.name);
477 }
478
479 int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl,
480 enum bt_ctf_scope scope,
481 struct bt_ctf_field_decl const * const **list,
482 unsigned int *count)
483 {
484 int i;
485 GArray *fields = NULL;
486 gpointer *ret_list = NULL;
487 GPtrArray *fields_array = NULL;
488 int ret = 0;
489 *count = 0;
490
491 switch (scope) {
492 case BT_EVENT_CONTEXT:
493 if (event_decl->context_decl) {
494 ret_list = event_decl->context_decl->pdata;
495 *count = event_decl->context_decl->len;
496 goto end;
497 }
498 event_decl->context_decl = g_ptr_array_new();
499 if (!event_decl->parent.context_decl) {
500 ret = -1;
501 goto end;
502 }
503 fields = event_decl->parent.context_decl->fields;
504 fields_array = event_decl->context_decl;
505 break;
506 case BT_EVENT_FIELDS:
507 if (event_decl->fields_decl) {
508 ret_list = event_decl->fields_decl->pdata;
509 *count = event_decl->fields_decl->len;
510 goto end;
511 }
512 event_decl->fields_decl = g_ptr_array_new();
513 if (!event_decl->parent.fields_decl) {
514 ret = -1;
515 goto end;
516 }
517 fields = event_decl->parent.fields_decl->fields;
518 fields_array = event_decl->fields_decl;
519 break;
520 case BT_STREAM_PACKET_CONTEXT:
521 if (event_decl->packet_context_decl) {
522 ret_list = event_decl->packet_context_decl->pdata;
523 *count = event_decl->packet_context_decl->len;
524 goto end;
525 }
526 event_decl->packet_context_decl = g_ptr_array_new();
527 if (!event_decl->parent.stream->packet_context_decl) {
528 ret = -1;
529 goto end;
530 }
531 fields = event_decl->parent.stream->packet_context_decl->fields;
532 fields_array = event_decl->packet_context_decl;
533 break;
534 case BT_STREAM_EVENT_CONTEXT:
535 if (event_decl->event_context_decl) {
536 ret_list = event_decl->event_context_decl->pdata;
537 *count = event_decl->event_context_decl->len;
538 goto end;
539 }
540 event_decl->event_context_decl = g_ptr_array_new();
541 if (!event_decl->parent.stream->event_context_decl) {
542 ret = -1;
543 goto end;
544 }
545 fields = event_decl->parent.stream->event_context_decl->fields;
546 fields_array = event_decl->event_context_decl;
547 break;
548 case BT_STREAM_EVENT_HEADER:
549 if (event_decl->event_header_decl) {
550 ret_list = event_decl->event_header_decl->pdata;
551 *count = event_decl->event_header_decl->len;
552 goto end;
553 }
554 event_decl->event_header_decl = g_ptr_array_new();
555 if (!event_decl->parent.stream->event_header_decl) {
556 ret = -1;
557 goto end;
558 }
559 fields = event_decl->parent.stream->event_header_decl->fields;
560 fields_array = event_decl->event_header_decl;
561 break;
562 case BT_TRACE_PACKET_HEADER:
563 if (event_decl->packet_header_decl) {
564 ret_list = event_decl->packet_header_decl->pdata;
565 *count = event_decl->packet_header_decl->len;
566 goto end;
567 }
568 event_decl->packet_header_decl = g_ptr_array_new();
569 if (!event_decl->parent.stream->trace->packet_header_decl) {
570 ret = -1;
571 goto end;
572 }
573 fields = event_decl->parent.stream->trace->packet_header_decl->fields;
574 fields_array = event_decl->packet_header_decl;
575 break;
576 }
577
578 for (i = 0; i < fields->len; i++) {
579 g_ptr_array_add(fields_array,
580 &g_array_index(fields,
581 struct declaration_field, i));
582 }
583 ret_list = fields_array->pdata;
584 *count = fields->len;
585
586 end:
587 *list = (struct bt_ctf_field_decl const* const*) ret_list;
588
589 return ret;
590 }
591
592 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
593 {
594 if (field)
595 return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
596 return NULL;
597 }
This page took 0.064751 seconds and 4 git commands to generate.