Add missing permission notice in each source file
[babeltrace.git] / formats / ctf / events.c
CommitLineData
9843982d 1/*
5c5facc7 2 * ctf/events.c
9843982d
JD
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.
c462e188
MD
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
9843982d
JD
28 */
29
30#include <babeltrace/babeltrace.h>
31#include <babeltrace/format.h>
32#include <babeltrace/ctf/events.h>
33#include <babeltrace/ctf-ir/metadata.h>
e4195791
MD
34#include <babeltrace/prio_heap.h>
35#include <babeltrace/iterator-internal.h>
36#include <babeltrace/ctf/events-internal.h>
37#include <babeltrace/ctf/metadata.h>
9843982d
JD
38#include <glib.h>
39
c34ea0fa
MD
40#include "events-private.h"
41
9843982d
JD
42/*
43 * thread local storage to store the last error that occured
44 * while reading a field, this variable must be accessed by
b330165c 45 * bt_ctf_field_get_error only
9843982d
JD
46 */
47__thread int bt_ctf_last_field_error = 0;
48
c50d2a7a 49const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
9843982d
JD
50 enum bt_ctf_scope scope)
51{
554cf424
MD
52 const struct definition *tmp = NULL;
53 const struct ctf_event_definition *event;
9843982d 54
7f89ddce
MD
55 if (!ctf_event)
56 return NULL;
57
58 event = ctf_event->parent;
9843982d
JD
59 switch (scope) {
60 case BT_TRACE_PACKET_HEADER:
61 if (!event->stream)
62 goto error;
63 if (event->stream->trace_packet_header)
64 tmp = &event->stream->trace_packet_header->p;
65 break;
66 case BT_STREAM_PACKET_CONTEXT:
67 if (!event->stream)
68 goto error;
69 if (event->stream->stream_packet_context)
70 tmp = &event->stream->stream_packet_context->p;
71 break;
72 case BT_STREAM_EVENT_HEADER:
73 if (!event->stream)
74 goto error;
75 if (event->stream->stream_event_header)
76 tmp = &event->stream->stream_event_header->p;
77 break;
78 case BT_STREAM_EVENT_CONTEXT:
79 if (!event->stream)
80 goto error;
81 if (event->stream->stream_event_context)
82 tmp = &event->stream->stream_event_context->p;
83 break;
84 case BT_EVENT_CONTEXT:
8a4722b0
JD
85 if (event->event_context)
86 tmp = &event->event_context->p;
9843982d
JD
87 break;
88 case BT_EVENT_FIELDS:
8a4722b0
JD
89 if (event->event_fields)
90 tmp = &event->event_fields->p;
9843982d
JD
91 break;
92 }
93 return tmp;
94
95error:
96 return NULL;
97}
98
c50d2a7a 99const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
04ae3991 100 const struct definition *scope,
9843982d
JD
101 const char *field)
102{
554cf424 103 const struct definition *def;
300d317a 104 char *field_underscore;
9843982d 105
7f89ddce
MD
106 if (!ctf_event || !scope || !field)
107 return NULL;
108
109 def = lookup_definition(scope, field);
110 /*
111 * optionally a field can have an underscore prefix, try
112 * to lookup the field with this prefix if it failed
113 */
114 if (!def) {
115 field_underscore = g_new(char, strlen(field) + 2);
116 field_underscore[0] = '_';
117 strcpy(&field_underscore[1], field);
118 def = lookup_definition(scope, field_underscore);
119 g_free(field_underscore);
9843982d 120 }
b14d90be 121 if (bt_ctf_field_type(bt_ctf_get_decl_from_def(def)) == CTF_TYPE_VARIANT) {
554cf424 122 const struct definition_variant *variant_definition;
7f89ddce 123 variant_definition = container_of(def,
554cf424 124 const struct definition_variant, p);
7f89ddce
MD
125 return variant_definition->current_field;
126 }
127 return def;
9843982d
JD
128}
129
c50d2a7a 130const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
04ae3991 131 const struct definition *field,
9843982d
JD
132 unsigned int index)
133{
134 struct definition *ret = NULL;
135
7f89ddce
MD
136 if (!ctf_event || !field)
137 return NULL;
138
b14d90be 139 if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_ARRAY) {
9843982d
JD
140 struct definition_array *array_definition;
141 array_definition = container_of(field,
142 struct definition_array, p);
143 ret = array_index(array_definition, index);
b14d90be 144 } else if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) {
9843982d
JD
145 struct definition_sequence *sequence_definition;
146 sequence_definition = container_of(field,
147 struct definition_sequence, p);
148 ret = sequence_index(sequence_definition, index);
149 }
150 return ret;
151}
152
c50d2a7a 153const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event)
9843982d 154{
554cf424
MD
155 const struct ctf_event_declaration *event_class;
156 const struct ctf_stream_declaration *stream_class;
157 const struct ctf_event_definition *event;
9843982d 158
7f89ddce 159 if (!ctf_event)
9843982d 160 return NULL;
7f89ddce
MD
161
162 event = ctf_event->parent;
9843982d
JD
163 stream_class = event->stream->stream_class;
164 event_class = g_ptr_array_index(stream_class->events_by_id,
165 event->stream->event_id);
166 return g_quark_to_string(event_class->name);
167}
168
169const char *bt_ctf_field_name(const struct definition *def)
170{
7f89ddce
MD
171 if (!def)
172 return NULL;
173
174 return rem_(g_quark_to_string(def->name));
9843982d
JD
175}
176
2bdfa4cf 177enum ctf_type_id bt_ctf_field_type(const struct declaration *decl)
9843982d 178{
2bdfa4cf 179 if (!decl)
7f89ddce
MD
180 return CTF_TYPE_UNKNOWN;
181
2bdfa4cf 182 return decl->id;
9843982d
JD
183}
184
c50d2a7a 185int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event,
04ae3991 186 const struct definition *scope,
9843982d
JD
187 struct definition const * const **list,
188 unsigned int *count)
189{
7f89ddce
MD
190 if (!ctf_event || !scope || !list || !count)
191 return -EINVAL;
192
b14d90be 193 switch (bt_ctf_field_type(bt_ctf_get_decl_from_def(scope))) {
9843982d
JD
194 case CTF_TYPE_INTEGER:
195 case CTF_TYPE_FLOAT:
196 case CTF_TYPE_STRING:
197 case CTF_TYPE_ENUM:
198 goto error;
199 case CTF_TYPE_STRUCT:
200 {
04ae3991 201 const struct definition_struct *def_struct;
9843982d 202
04ae3991 203 def_struct = container_of(scope, const struct definition_struct, p);
9843982d
JD
204 if (!def_struct)
205 goto error;
206 if (def_struct->fields->pdata) {
207 *list = (struct definition const* const*) def_struct->fields->pdata;
208 *count = def_struct->fields->len;
209 goto end;
210 } else {
211 goto error;
212 }
7f89ddce 213 break;
9843982d
JD
214 }
215 case CTF_TYPE_UNTAGGED_VARIANT:
216 goto error;
217 case CTF_TYPE_VARIANT:
218 {
04ae3991 219 const struct definition_variant *def_variant;
9843982d 220
04ae3991 221 def_variant = container_of(scope, const struct definition_variant, p);
9843982d
JD
222 if (!def_variant)
223 goto error;
224 if (def_variant->fields->pdata) {
225 *list = (struct definition const* const*) def_variant->fields->pdata;
226 *count = def_variant->fields->len;
227 goto end;
228 } else {
229 goto error;
230 }
7f89ddce 231 break;
9843982d
JD
232 }
233 case CTF_TYPE_ARRAY:
234 {
04ae3991 235 const struct definition_array *def_array;
9843982d 236
04ae3991 237 def_array = container_of(scope, const struct definition_array, p);
9843982d
JD
238 if (!def_array)
239 goto error;
240 if (def_array->elems->pdata) {
241 *list = (struct definition const* const*) def_array->elems->pdata;
242 *count = def_array->elems->len;
243 goto end;
244 } else {
245 goto error;
246 }
7f89ddce 247 break;
9843982d
JD
248 }
249 case CTF_TYPE_SEQUENCE:
250 {
04ae3991 251 const struct definition_sequence *def_sequence;
9843982d 252
04ae3991 253 def_sequence = container_of(scope, const struct definition_sequence, p);
9843982d
JD
254 if (!def_sequence)
255 goto error;
256 if (def_sequence->elems->pdata) {
257 *list = (struct definition const* const*) def_sequence->elems->pdata;
258 *count = def_sequence->elems->len;
259 goto end;
260 } else {
261 goto error;
262 }
7f89ddce 263 break;
9843982d
JD
264 }
265 default:
266 break;
267 }
268
269end:
270 return 0;
271
272error:
273 *list = NULL;
274 *count = 0;
275 return -1;
276}
277
c50d2a7a 278struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event)
98a04903
JD
279{
280 struct bt_context *ret = NULL;
554cf424
MD
281 const struct ctf_file_stream *cfs;
282 const struct ctf_trace *trace;
283 const struct ctf_event_definition *event;
98a04903 284
7f89ddce
MD
285 if (!ctf_event)
286 return NULL;
287
288 event = ctf_event->parent;
554cf424 289 cfs = container_of(event->stream, const struct ctf_file_stream,
98a04903 290 parent);
8b8dc96e 291 trace = cfs->parent.stream_class->trace;
98a04903
JD
292 if (trace->ctx)
293 ret = trace->ctx;
294
295 return ret;
296}
297
c50d2a7a 298int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event)
98a04903
JD
299{
300 int ret = -1;
554cf424
MD
301 const struct ctf_file_stream *cfs;
302 const struct ctf_trace *trace;
303 const struct ctf_event_definition *event;
7f89ddce
MD
304
305 if (!ctf_event)
306 return -EINVAL;
98a04903 307
7f89ddce 308 event = ctf_event->parent;
554cf424 309 cfs = container_of(event->stream, const struct ctf_file_stream,
98a04903 310 parent);
8b8dc96e 311 trace = cfs->parent.stream_class->trace;
98a04903
JD
312 if (trace->handle)
313 ret = trace->handle->id;
314
315 return ret;
316}
317
d40ee8ec 318uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event)
9843982d 319{
554cf424 320 const struct ctf_event_definition *event;
7f89ddce
MD
321
322 if (!ctf_event)
323 return -1ULL;
324
325 event = ctf_event->parent;
9843982d 326 if (event && event->stream->has_timestamp)
03798a93 327 return event->stream->real_timestamp;
9843982d 328 else
57f3005e
SJD
329 return -1ULL;
330}
331
d40ee8ec 332uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event)
57f3005e 333{
554cf424 334 const struct ctf_event_definition *event;
7f89ddce
MD
335
336 if (!ctf_event)
337 return -1ULL;
338
339 event = ctf_event->parent;
c34ea0fa 340 if (event && event->stream->has_timestamp)
03798a93 341 return event->stream->cycles_timestamp;
c34ea0fa 342 else
57f3005e 343 return -1ULL;
9843982d
JD
344}
345
346static void bt_ctf_field_set_error(int error)
347{
348 bt_ctf_last_field_error = error;
349}
350
351int bt_ctf_field_get_error(void)
352{
353 int ret;
354 ret = bt_ctf_last_field_error;
355 bt_ctf_last_field_error = 0;
356
357 return ret;
358}
359
554cf424
MD
360static const struct declaration_integer *
361get_declaration_integer(const struct declaration *decl)
2bdfa4cf 362{
554cf424
MD
363 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_INTEGER)
364 return NULL;
365 return container_of(decl, const struct declaration_integer, p);
2bdfa4cf
JD
366}
367
554cf424
MD
368static const struct declaration_string *
369get_declaration_string(const struct declaration *decl)
2bdfa4cf 370{
554cf424
MD
371 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_STRING)
372 return NULL;
373 return container_of(decl, const struct declaration_string, p);
2bdfa4cf
JD
374}
375
554cf424
MD
376static const struct declaration_array *
377get_declaration_array(const struct declaration *decl)
2bdfa4cf 378{
554cf424
MD
379 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_ARRAY)
380 return NULL;
381 return container_of(decl, const struct declaration_array, p);
2bdfa4cf
JD
382}
383
c22b9327
JD
384static const struct declaration_sequence *
385get_declaration_sequence(const struct declaration *decl)
386{
387 if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_SEQUENCE)
388 return NULL;
389 return container_of(decl, const struct declaration_sequence, p);
390}
391
2bdfa4cf 392int bt_ctf_get_int_signedness(const struct declaration *decl)
8673030f 393{
554cf424 394 const struct declaration_integer *integer;
8673030f 395
2bdfa4cf 396 integer = get_declaration_integer(decl);
554cf424 397 if (!integer) {
8673030f 398 bt_ctf_field_set_error(-EINVAL);
554cf424 399 return -EINVAL;
8673030f 400 }
554cf424 401 return integer->signedness;
8673030f
JD
402}
403
2bdfa4cf 404int bt_ctf_get_int_base(const struct declaration *decl)
8673030f 405{
554cf424 406 const struct declaration_integer *integer;
8673030f 407
2bdfa4cf 408 integer = get_declaration_integer(decl);
554cf424 409 if (!integer) {
8673030f 410 bt_ctf_field_set_error(-EINVAL);
554cf424 411 return -EINVAL;
8673030f 412 }
554cf424 413 return integer->base;
8673030f
JD
414}
415
2bdfa4cf 416int bt_ctf_get_int_byte_order(const struct declaration *decl)
8673030f 417{
554cf424 418 const struct declaration_integer *integer;
8673030f 419
2bdfa4cf 420 integer = get_declaration_integer(decl);
554cf424 421 if (!integer) {
8673030f 422 bt_ctf_field_set_error(-EINVAL);
554cf424 423 return -EINVAL;
8673030f 424 }
554cf424 425 return integer->byte_order;
8673030f
JD
426}
427
2bdfa4cf 428ssize_t bt_ctf_get_int_len(const struct declaration *decl)
fef0e521 429{
554cf424 430 const struct declaration_integer *integer;
fef0e521 431
2bdfa4cf 432 integer = get_declaration_integer(decl);
554cf424 433 if (!integer) {
fef0e521 434 bt_ctf_field_set_error(-EINVAL);
554cf424 435 return -EINVAL;
fef0e521 436 }
554cf424 437 return (ssize_t) integer->len;
fef0e521
MD
438}
439
4d25c350
MD
440const struct definition *bt_ctf_get_enum_int(const struct definition *field)
441{
554cf424 442 const struct definition_enum *def_enum;
4d25c350 443
b14d90be 444 if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
4d25c350
MD
445 bt_ctf_field_set_error(-EINVAL);
446 return NULL;
447 }
554cf424 448 def_enum = container_of(field, const struct definition_enum, p);
4d25c350
MD
449 return &def_enum->integer->p;
450}
451
452const char *bt_ctf_get_enum_str(const struct definition *field)
453{
554cf424
MD
454 const struct definition_enum *def_enum;
455 const struct declaration_enum *decl_enum;
4d25c350
MD
456 GArray *array;
457 const char *ret;
458
b14d90be 459 if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) {
4d25c350
MD
460 bt_ctf_field_set_error(-EINVAL);
461 return NULL;
462 }
554cf424 463 def_enum = container_of(field, const struct definition_enum, p);
4d25c350
MD
464 decl_enum = def_enum->declaration;
465 if (get_int_signedness(&def_enum->integer->p)) {
466 array = enum_int_to_quark_set(decl_enum,
467 get_signed_int(&def_enum->integer->p));
468 } else {
469 array = enum_uint_to_quark_set(decl_enum,
470 get_unsigned_int(&def_enum->integer->p));
471 }
472 if (!array) {
473 bt_ctf_field_set_error(-ENOENT);
474 return NULL;
475 }
476
477 if (array->len == 0) {
478 g_array_unref(array);
479 bt_ctf_field_set_error(-ENOENT);
480 return NULL;
481 }
482 /* Return first string. Arbitrary choice. */
483 ret = g_quark_to_string(g_array_index(array, GQuark, 0));
484 g_array_unref(array);
485 return ret;
486}
487
2bdfa4cf 488enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl)
8673030f
JD
489{
490 enum ctf_string_encoding ret = 0;
c22b9327 491 enum ctf_type_id type;
554cf424
MD
492 const struct declaration_integer *integer;
493 const struct declaration_string *string;
c22b9327
JD
494 const struct declaration_array *array;
495 const struct declaration_sequence *sequence;
8673030f 496
2bdfa4cf 497 if (!decl)
7f89ddce 498 goto error;
8673030f 499
c22b9327
JD
500 type = bt_ctf_field_type(decl);
501
502 switch (type) {
503 case CTF_TYPE_ARRAY:
504 array = get_declaration_array(decl);
505 if (!array)
2bdfa4cf 506 goto error;
c22b9327
JD
507 integer = get_declaration_integer(array->elem);
508 if (!integer)
509 goto error;
510 ret = integer->encoding;
511 break;
512 case CTF_TYPE_SEQUENCE:
513 sequence = get_declaration_sequence(decl);
514 if (!sequence)
515 goto error;
516 integer = get_declaration_integer(sequence->elem);
517 if (!integer)
518 goto error;
519 ret = integer->encoding;
520 break;
521 case CTF_TYPE_STRING:
2bdfa4cf 522 string = get_declaration_string(decl);
c22b9327 523 if (!string)
2bdfa4cf 524 goto error;
c22b9327
JD
525 ret = string->encoding;
526 break;
527 case CTF_TYPE_INTEGER:
528 integer = get_declaration_integer(decl);
529 if (!integer)
530 goto error;
531 ret = integer->encoding;
532 break;
533 default:
8673030f 534 goto error;
2bdfa4cf 535 }
8673030f
JD
536 return ret;
537
538error:
539 bt_ctf_field_set_error(-EINVAL);
540 return -1;
541}
542
2bdfa4cf 543int bt_ctf_get_array_len(const struct declaration *decl)
8673030f 544{
554cf424 545 const struct declaration_array *array;
8673030f 546
554cf424
MD
547 array = get_declaration_array(decl);
548 if (!array)
2bdfa4cf 549 goto error;
554cf424 550 return array->len;
2bdfa4cf
JD
551
552error:
553 bt_ctf_field_set_error(-EINVAL);
554 return -1;
8673030f
JD
555}
556
da320b83 557uint64_t bt_ctf_get_uint64(const struct definition *field)
9843982d 558{
63dd3817 559 uint64_t ret = 0;
9843982d 560
b14d90be 561 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER)
9843982d
JD
562 ret = get_unsigned_int(field);
563 else
564 bt_ctf_field_set_error(-EINVAL);
565
566 return ret;
567}
568
da320b83 569int64_t bt_ctf_get_int64(const struct definition *field)
9843982d 570{
63dd3817 571 int64_t ret = 0;
9843982d 572
b14d90be 573 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER)
9843982d
JD
574 ret = get_signed_int(field);
575 else
576 bt_ctf_field_set_error(-EINVAL);
577
578 return ret;
9843982d
JD
579}
580
da320b83 581char *bt_ctf_get_char_array(const struct definition *field)
9843982d
JD
582{
583 char *ret = NULL;
885885bf 584 GString *char_array;
9843982d 585
b14d90be 586 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_ARRAY) {
885885bf
JD
587 char_array = get_char_array(field);
588 if (char_array) {
589 ret = char_array->str;
590 goto end;
591 }
592 }
593 bt_ctf_field_set_error(-EINVAL);
9843982d 594
885885bf 595end:
9843982d
JD
596 return ret;
597}
598
da320b83 599char *bt_ctf_get_string(const struct definition *field)
9843982d
JD
600{
601 char *ret = NULL;
602
b14d90be 603 if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_STRING)
9843982d
JD
604 ret = get_string(field);
605 else
606 bt_ctf_field_set_error(-EINVAL);
607
608 return ret;
609}
e003ab50
JD
610
611int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx,
64c2c249 612 struct bt_ctf_event_decl * const **list,
e003ab50
JD
613 unsigned int *count)
614{
615 struct bt_trace_handle *handle;
616 struct trace_descriptor *td;
617 struct ctf_trace *tin;
618
7f89ddce 619 if (!ctx || !list || !count)
e003ab50
JD
620 goto error;
621
622 handle = g_hash_table_lookup(ctx->trace_handles,
623 (gpointer) (unsigned long) handle_id);
624 if (!handle)
625 goto error;
626
627 td = handle->td;
628 tin = container_of(td, struct ctf_trace, parent);
629
64c2c249 630 *list = (struct bt_ctf_event_decl * const*) tin->event_declarations->pdata;
e003ab50
JD
631 *count = tin->event_declarations->len;
632 return 0;
633
634error:
635 return -1;
636}
637
638const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event)
639{
640 if (!event)
641 return NULL;
7f89ddce 642
e003ab50
JD
643 return g_quark_to_string(event->parent.name);
644}
64c2c249
JD
645
646int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl,
647 enum bt_ctf_scope scope,
648 struct bt_ctf_field_decl const * const **list,
649 unsigned int *count)
650{
651 int i;
652 GArray *fields = NULL;
653 gpointer *ret_list = NULL;
654 GPtrArray *fields_array = NULL;
655 int ret = 0;
656 *count = 0;
657
7f89ddce
MD
658 if (!event_decl || !list || !count)
659 return -EINVAL;
660
64c2c249
JD
661 switch (scope) {
662 case BT_EVENT_CONTEXT:
663 if (event_decl->context_decl) {
664 ret_list = event_decl->context_decl->pdata;
665 *count = event_decl->context_decl->len;
666 goto end;
667 }
668 event_decl->context_decl = g_ptr_array_new();
669 if (!event_decl->parent.context_decl) {
670 ret = -1;
671 goto end;
672 }
673 fields = event_decl->parent.context_decl->fields;
674 fields_array = event_decl->context_decl;
675 break;
676 case BT_EVENT_FIELDS:
677 if (event_decl->fields_decl) {
678 ret_list = event_decl->fields_decl->pdata;
679 *count = event_decl->fields_decl->len;
680 goto end;
681 }
682 event_decl->fields_decl = g_ptr_array_new();
683 if (!event_decl->parent.fields_decl) {
684 ret = -1;
685 goto end;
686 }
687 fields = event_decl->parent.fields_decl->fields;
688 fields_array = event_decl->fields_decl;
689 break;
690 case BT_STREAM_PACKET_CONTEXT:
691 if (event_decl->packet_context_decl) {
692 ret_list = event_decl->packet_context_decl->pdata;
693 *count = event_decl->packet_context_decl->len;
694 goto end;
695 }
696 event_decl->packet_context_decl = g_ptr_array_new();
697 if (!event_decl->parent.stream->packet_context_decl) {
698 ret = -1;
699 goto end;
700 }
701 fields = event_decl->parent.stream->packet_context_decl->fields;
702 fields_array = event_decl->packet_context_decl;
703 break;
704 case BT_STREAM_EVENT_CONTEXT:
705 if (event_decl->event_context_decl) {
706 ret_list = event_decl->event_context_decl->pdata;
707 *count = event_decl->event_context_decl->len;
708 goto end;
709 }
710 event_decl->event_context_decl = g_ptr_array_new();
711 if (!event_decl->parent.stream->event_context_decl) {
712 ret = -1;
713 goto end;
714 }
715 fields = event_decl->parent.stream->event_context_decl->fields;
716 fields_array = event_decl->event_context_decl;
717 break;
718 case BT_STREAM_EVENT_HEADER:
719 if (event_decl->event_header_decl) {
720 ret_list = event_decl->event_header_decl->pdata;
721 *count = event_decl->event_header_decl->len;
722 goto end;
723 }
724 event_decl->event_header_decl = g_ptr_array_new();
725 if (!event_decl->parent.stream->event_header_decl) {
726 ret = -1;
727 goto end;
728 }
729 fields = event_decl->parent.stream->event_header_decl->fields;
730 fields_array = event_decl->event_header_decl;
731 break;
732 case BT_TRACE_PACKET_HEADER:
733 if (event_decl->packet_header_decl) {
734 ret_list = event_decl->packet_header_decl->pdata;
735 *count = event_decl->packet_header_decl->len;
736 goto end;
737 }
738 event_decl->packet_header_decl = g_ptr_array_new();
739 if (!event_decl->parent.stream->trace->packet_header_decl) {
740 ret = -1;
741 goto end;
742 }
743 fields = event_decl->parent.stream->trace->packet_header_decl->fields;
744 fields_array = event_decl->packet_header_decl;
745 break;
746 }
747
748 for (i = 0; i < fields->len; i++) {
749 g_ptr_array_add(fields_array,
750 &g_array_index(fields,
751 struct declaration_field, i));
752 }
753 ret_list = fields_array->pdata;
754 *count = fields->len;
755
756end:
757 *list = (struct bt_ctf_field_decl const* const*) ret_list;
758
759 return ret;
760}
761
762const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field)
763{
7f89ddce
MD
764 if (!field)
765 return NULL;
766
767 return rem_(g_quark_to_string(((struct declaration_field *) field)->name));
64c2c249 768}
2bdfa4cf 769
b14d90be 770const struct declaration *bt_ctf_get_decl_from_def(const struct definition *def)
2bdfa4cf
JD
771{
772 if (def)
773 return def->declaration;
774
775 return NULL;
776}
b14d90be
JD
777
778const struct declaration *bt_ctf_get_decl_from_field_decl(
779 const struct bt_ctf_field_decl *field)
780{
781 if (field)
782 return ((struct declaration_field *) field)->declaration;
783
784 return NULL;
785}
This page took 0.114771 seconds and 4 git commands to generate.