ctf: decoding: accommodate lttng-crash timestamp quirk
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta.h
CommitLineData
44c440bc
PP
1#ifndef _CTF_META_H
2#define _CTF_META_H
3
4/*
5 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 */
17
3fadfbc0 18#include <babeltrace2/babeltrace.h>
578e048b 19#include "common/common.h"
6162e6b7 20#include "common/uuid.h"
578e048b 21#include "common/assert.h"
44c440bc
PP
22#include <glib.h>
23#include <stdint.h>
24#include <string.h>
25
864cad70
PP
26enum ctf_field_class_type {
27 CTF_FIELD_CLASS_TYPE_INT,
28 CTF_FIELD_CLASS_TYPE_ENUM,
29 CTF_FIELD_CLASS_TYPE_FLOAT,
30 CTF_FIELD_CLASS_TYPE_STRING,
31 CTF_FIELD_CLASS_TYPE_STRUCT,
32 CTF_FIELD_CLASS_TYPE_ARRAY,
33 CTF_FIELD_CLASS_TYPE_SEQUENCE,
34 CTF_FIELD_CLASS_TYPE_VARIANT,
44c440bc
PP
35};
36
5cd6d0e5
PP
37enum ctf_field_class_meaning {
38 CTF_FIELD_CLASS_MEANING_NONE,
39 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME,
40 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME,
41 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID,
42 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID,
43 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID,
44 CTF_FIELD_CLASS_MEANING_MAGIC,
45 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT,
46 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT,
47 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE,
48 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE,
49 CTF_FIELD_CLASS_MEANING_UUID,
44c440bc
PP
50};
51
52enum ctf_byte_order {
e2bd7d40 53 CTF_BYTE_ORDER_UNKNOWN,
44c440bc
PP
54 CTF_BYTE_ORDER_DEFAULT,
55 CTF_BYTE_ORDER_LITTLE,
56 CTF_BYTE_ORDER_BIG,
57};
58
59enum ctf_encoding {
60 CTF_ENCODING_NONE,
61 CTF_ENCODING_UTF8,
62};
63
83ebb7f1 64enum ctf_scope {
d7fd2938
JR
65 CTF_SCOPE_PACKET_UNKNOWN = -1,
66 CTF_SCOPE_PACKET_HEADER = 0,
83ebb7f1
PP
67 CTF_SCOPE_PACKET_CONTEXT,
68 CTF_SCOPE_EVENT_HEADER,
69 CTF_SCOPE_EVENT_COMMON_CONTEXT,
70 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT,
71 CTF_SCOPE_EVENT_PAYLOAD,
72};
73
0f2d58c9
PP
74struct ctf_clock_class {
75 GString *name;
76 GString *description;
77 uint64_t frequency;
78 uint64_t precision;
79 int64_t offset_seconds;
80 uint64_t offset_cycles;
6162e6b7 81 bt_uuid_t uuid;
0f2d58c9
PP
82 bool has_uuid;
83 bool is_absolute;
84
85 /* Weak, set during translation */
86 bt_clock_class *ir_cc;
87};
88
5cd6d0e5 89struct ctf_field_class {
864cad70 90 enum ctf_field_class_type type;
44c440bc
PP
91 unsigned int alignment;
92 bool is_compound;
93 bool in_ir;
94
95 /* Weak, set during translation. NULL if `in_ir` is false below. */
b19ff26f 96 bt_field_class *ir_fc;
44c440bc
PP
97};
98
5cd6d0e5
PP
99struct ctf_field_class_bit_array {
100 struct ctf_field_class base;
44c440bc
PP
101 enum ctf_byte_order byte_order;
102 unsigned int size;
103};
104
5cd6d0e5
PP
105struct ctf_field_class_int {
106 struct ctf_field_class_bit_array base;
107 enum ctf_field_class_meaning meaning;
44c440bc 108 bool is_signed;
4cdfc5e8 109 bt_field_class_integer_preferred_display_base disp_base;
44c440bc
PP
110 enum ctf_encoding encoding;
111 int64_t storing_index;
112
0f2d58c9
PP
113 /* Weak */
114 struct ctf_clock_class *mapped_clock_class;
44c440bc
PP
115};
116
117struct ctf_range {
118 union {
119 uint64_t u;
120 int64_t i;
121 } lower;
122
123 union {
124 uint64_t u;
125 int64_t i;
126 } upper;
127};
128
5cd6d0e5 129struct ctf_field_class_enum_mapping {
44c440bc 130 GString *label;
45c51519
PP
131
132 /* Array of `struct ctf_range` */
133 GArray *ranges;
44c440bc
PP
134};
135
5cd6d0e5
PP
136struct ctf_field_class_enum {
137 struct ctf_field_class_int base;
44c440bc 138
5cd6d0e5 139 /* Array of `struct ctf_field_class_enum_mapping` */
44c440bc
PP
140 GArray *mappings;
141};
142
5cd6d0e5
PP
143struct ctf_field_class_float {
144 struct ctf_field_class_bit_array base;
44c440bc
PP
145};
146
5cd6d0e5
PP
147struct ctf_field_class_string {
148 struct ctf_field_class base;
44c440bc
PP
149 enum ctf_encoding encoding;
150};
151
5cd6d0e5 152struct ctf_named_field_class {
45c51519
PP
153 /* Original name which can include a leading `_` */
154 GString *orig_name;
155
156 /* Name as translated to trace IR (leading `_` removed) */
44c440bc
PP
157 GString *name;
158
159 /* Owned by this */
5cd6d0e5 160 struct ctf_field_class *fc;
44c440bc
PP
161};
162
5cd6d0e5
PP
163struct ctf_field_class_struct {
164 struct ctf_field_class base;
44c440bc 165
5cd6d0e5 166 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
167 GArray *members;
168};
169
170struct ctf_field_path {
83ebb7f1 171 enum ctf_scope root;
44c440bc
PP
172
173 /* Array of `int64_t` */
174 GArray *path;
175};
176
5cd6d0e5 177struct ctf_field_class_variant_range {
44c440bc
PP
178 struct ctf_range range;
179 uint64_t option_index;
180};
181
5cd6d0e5
PP
182struct ctf_field_class_variant {
183 struct ctf_field_class base;
44c440bc
PP
184 GString *tag_ref;
185 struct ctf_field_path tag_path;
186 uint64_t stored_tag_index;
187
5cd6d0e5 188 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
189 GArray *options;
190
5cd6d0e5 191 /* Array of `struct ctf_field_class_variant_range` */
44c440bc
PP
192 GArray *ranges;
193
194 /* Weak */
5cd6d0e5 195 struct ctf_field_class_enum *tag_fc;
44c440bc
PP
196};
197
5cd6d0e5
PP
198struct ctf_field_class_array_base {
199 struct ctf_field_class base;
200 struct ctf_field_class *elem_fc;
44c440bc
PP
201 bool is_text;
202};
203
5cd6d0e5
PP
204struct ctf_field_class_array {
205 struct ctf_field_class_array_base base;
206 enum ctf_field_class_meaning meaning;
44c440bc
PP
207 uint64_t length;
208};
209
5cd6d0e5
PP
210struct ctf_field_class_sequence {
211 struct ctf_field_class_array_base base;
44c440bc
PP
212 GString *length_ref;
213 struct ctf_field_path length_path;
214 uint64_t stored_length_index;
215
216 /* Weak */
5cd6d0e5 217 struct ctf_field_class_int *length_fc;
44c440bc
PP
218};
219
220struct ctf_event_class {
221 GString *name;
222 uint64_t id;
223 GString *emf_uri;
4cdfc5e8 224 bt_event_class_log_level log_level;
44c440bc 225 bool is_translated;
6da709aa 226 bool is_log_level_set;
44c440bc
PP
227
228 /* Owned by this */
5cd6d0e5 229 struct ctf_field_class *spec_context_fc;
44c440bc
PP
230
231 /* Owned by this */
5cd6d0e5 232 struct ctf_field_class *payload_fc;
44c440bc
PP
233
234 /* Weak, set during translation */
b19ff26f 235 bt_event_class *ir_ec;
44c440bc
PP
236};
237
238struct ctf_stream_class {
239 uint64_t id;
240 bool is_translated;
afd45274
PP
241 bool packets_have_ts_begin;
242 bool packets_have_ts_end;
243 bool has_discarded_events;
244 bool has_discarded_packets;
245 bool discarded_events_have_default_cs;
246 bool discarded_packets_have_default_cs;
44c440bc
PP
247
248 /* Owned by this */
5cd6d0e5 249 struct ctf_field_class *packet_context_fc;
44c440bc
PP
250
251 /* Owned by this */
5cd6d0e5 252 struct ctf_field_class *event_header_fc;
44c440bc
PP
253
254 /* Owned by this */
5cd6d0e5 255 struct ctf_field_class *event_common_context_fc;
44c440bc
PP
256
257 /* Array of `struct ctf_event_class *`, owned by this */
258 GPtrArray *event_classes;
259
260 /*
261 * Hash table mapping event class IDs to `struct ctf_event_class *`,
262 * weak.
263 */
264 GHashTable *event_classes_by_id;
265
0f2d58c9
PP
266 /* Weak */
267 struct ctf_clock_class *default_clock_class;
44c440bc
PP
268
269 /* Weak, set during translation */
b19ff26f 270 bt_stream_class *ir_sc;
44c440bc
PP
271};
272
273enum ctf_trace_class_env_entry_type {
274 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
275 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
276};
277
278struct ctf_trace_class_env_entry {
279 enum ctf_trace_class_env_entry_type type;
280 GString *name;
281
282 struct {
283 int64_t i;
284 GString *str;
285 } value;
286};
287
288struct ctf_trace_class {
44c440bc
PP
289 unsigned int major;
290 unsigned int minor;
6162e6b7 291 bt_uuid_t uuid;
44c440bc
PP
292 bool is_uuid_set;
293 enum ctf_byte_order default_byte_order;
294
295 /* Owned by this */
5cd6d0e5 296 struct ctf_field_class *packet_header_fc;
44c440bc
PP
297
298 uint64_t stored_value_count;
299
0f2d58c9 300 /* Array of `struct ctf_clock_class *` (owned by this) */
44c440bc
PP
301 GPtrArray *clock_classes;
302
303 /* Array of `struct ctf_stream_class *` */
304 GPtrArray *stream_classes;
305
306 /* Array of `struct ctf_trace_class_env_entry` */
307 GArray *env_entries;
308
309 bool is_translated;
310
311 /* Weak, set during translation */
b19ff26f 312 bt_trace_class *ir_tc;
5c2e8153
FD
313
314 struct {
315 bool lttng_crash;
316 } quirks;
44c440bc
PP
317};
318
319static inline
5cd6d0e5 320void ctf_field_class_destroy(struct ctf_field_class *fc);
44c440bc
PP
321
322static inline
864cad70
PP
323void _ctf_field_class_init(struct ctf_field_class *fc,
324 enum ctf_field_class_type type, unsigned int alignment)
44c440bc 325{
5cd6d0e5 326 BT_ASSERT(fc);
864cad70 327 fc->type = type;
5cd6d0e5
PP
328 fc->alignment = alignment;
329 fc->in_ir = false;
44c440bc
PP
330}
331
332static inline
5cd6d0e5 333void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
864cad70 334 enum ctf_field_class_type type)
44c440bc 335{
864cad70 336 _ctf_field_class_init((void *) fc, type, 1);
44c440bc
PP
337}
338
339static inline
5cd6d0e5 340void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
864cad70 341 enum ctf_field_class_type type)
44c440bc 342{
864cad70 343 _ctf_field_class_bit_array_init((void *) fc, type);
5cd6d0e5
PP
344 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
345 fc->storing_index = -1;
44c440bc
PP
346}
347
348static inline
349void ctf_field_path_init(struct ctf_field_path *field_path)
350{
351 BT_ASSERT(field_path);
352 field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t));
353 BT_ASSERT(field_path->path);
354}
355
356static inline
357void ctf_field_path_fini(struct ctf_field_path *field_path)
358{
359 BT_ASSERT(field_path);
360
361 if (field_path->path) {
362 g_array_free(field_path->path, TRUE);
363 }
364}
365
366static inline
5cd6d0e5 367void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
44c440bc 368{
5cd6d0e5
PP
369 BT_ASSERT(named_fc);
370 named_fc->name = g_string_new(NULL);
371 BT_ASSERT(named_fc->name);
45c51519
PP
372 named_fc->orig_name = g_string_new(NULL);
373 BT_ASSERT(named_fc->orig_name);
44c440bc
PP
374}
375
376static inline
5cd6d0e5 377void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
44c440bc 378{
5cd6d0e5 379 BT_ASSERT(named_fc);
44c440bc 380
5cd6d0e5
PP
381 if (named_fc->name) {
382 g_string_free(named_fc->name, TRUE);
44c440bc
PP
383 }
384
45c51519
PP
385 if (named_fc->orig_name) {
386 g_string_free(named_fc->orig_name, TRUE);
387 }
388
5cd6d0e5 389 ctf_field_class_destroy(named_fc->fc);
44c440bc
PP
390}
391
392static inline
5cd6d0e5
PP
393void _ctf_field_class_enum_mapping_init(
394 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
395{
396 BT_ASSERT(mapping);
397 mapping->label = g_string_new(NULL);
398 BT_ASSERT(mapping->label);
45c51519
PP
399 mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range));
400 BT_ASSERT(mapping->ranges);
44c440bc
PP
401}
402
403static inline
5cd6d0e5
PP
404void _ctf_field_class_enum_mapping_fini(
405 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
406{
407 BT_ASSERT(mapping);
408
409 if (mapping->label) {
410 g_string_free(mapping->label, TRUE);
411 }
45c51519
PP
412
413 if (mapping->ranges) {
414 g_array_free(mapping->ranges, TRUE);
415 }
44c440bc
PP
416}
417
418static inline
5cd6d0e5 419struct ctf_field_class_int *ctf_field_class_int_create(void)
44c440bc 420{
5cd6d0e5 421 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
44c440bc 422
5cd6d0e5 423 BT_ASSERT(fc);
864cad70 424 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
5cd6d0e5 425 return fc;
44c440bc
PP
426}
427
428static inline
5cd6d0e5 429struct ctf_field_class_float *ctf_field_class_float_create(void)
44c440bc 430{
5cd6d0e5
PP
431 struct ctf_field_class_float *fc =
432 g_new0(struct ctf_field_class_float, 1);
44c440bc 433
5cd6d0e5 434 BT_ASSERT(fc);
864cad70 435 _ctf_field_class_bit_array_init((void *) fc, CTF_FIELD_CLASS_TYPE_FLOAT);
5cd6d0e5 436 return fc;
44c440bc
PP
437}
438
439static inline
5cd6d0e5 440struct ctf_field_class_string *ctf_field_class_string_create(void)
44c440bc 441{
5cd6d0e5
PP
442 struct ctf_field_class_string *fc =
443 g_new0(struct ctf_field_class_string, 1);
44c440bc 444
5cd6d0e5 445 BT_ASSERT(fc);
864cad70 446 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRING, 8);
5cd6d0e5 447 return fc;
44c440bc
PP
448}
449
450static inline
5cd6d0e5 451struct ctf_field_class_enum *ctf_field_class_enum_create(void)
44c440bc 452{
5cd6d0e5 453 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
44c440bc 454
5cd6d0e5 455 BT_ASSERT(fc);
864cad70 456 _ctf_field_class_int_init((void *) fc, CTF_FIELD_CLASS_TYPE_ENUM);
5cd6d0e5
PP
457 fc->mappings = g_array_new(FALSE, TRUE,
458 sizeof(struct ctf_field_class_enum_mapping));
459 BT_ASSERT(fc->mappings);
460 return fc;
44c440bc
PP
461}
462
463static inline
5cd6d0e5 464struct ctf_field_class_struct *ctf_field_class_struct_create(void)
44c440bc 465{
5cd6d0e5
PP
466 struct ctf_field_class_struct *fc =
467 g_new0(struct ctf_field_class_struct, 1);
44c440bc 468
5cd6d0e5 469 BT_ASSERT(fc);
864cad70 470 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
5cd6d0e5
PP
471 fc->members = g_array_new(FALSE, TRUE,
472 sizeof(struct ctf_named_field_class));
473 BT_ASSERT(fc->members);
474 fc->base.is_compound = true;
475 return fc;
44c440bc
PP
476}
477
478static inline
5cd6d0e5 479struct ctf_field_class_variant *ctf_field_class_variant_create(void)
44c440bc 480{
5cd6d0e5
PP
481 struct ctf_field_class_variant *fc =
482 g_new0(struct ctf_field_class_variant, 1);
44c440bc 483
5cd6d0e5 484 BT_ASSERT(fc);
864cad70 485 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
5cd6d0e5
PP
486 fc->options = g_array_new(FALSE, TRUE,
487 sizeof(struct ctf_named_field_class));
488 BT_ASSERT(fc->options);
489 fc->ranges = g_array_new(FALSE, TRUE,
490 sizeof(struct ctf_field_class_variant_range));
491 BT_ASSERT(fc->ranges);
492 fc->tag_ref = g_string_new(NULL);
493 BT_ASSERT(fc->tag_ref);
494 ctf_field_path_init(&fc->tag_path);
495 fc->base.is_compound = true;
496 return fc;
44c440bc
PP
497}
498
499static inline
5cd6d0e5 500struct ctf_field_class_array *ctf_field_class_array_create(void)
44c440bc 501{
5cd6d0e5
PP
502 struct ctf_field_class_array *fc =
503 g_new0(struct ctf_field_class_array, 1);
44c440bc 504
5cd6d0e5 505 BT_ASSERT(fc);
864cad70 506 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
5cd6d0e5
PP
507 fc->base.base.is_compound = true;
508 return fc;
44c440bc
PP
509}
510
511static inline
5cd6d0e5 512struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
44c440bc 513{
5cd6d0e5
PP
514 struct ctf_field_class_sequence *fc =
515 g_new0(struct ctf_field_class_sequence, 1);
44c440bc 516
5cd6d0e5 517 BT_ASSERT(fc);
864cad70 518 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
5cd6d0e5
PP
519 fc->length_ref = g_string_new(NULL);
520 BT_ASSERT(fc->length_ref);
521 ctf_field_path_init(&fc->length_path);
522 fc->base.base.is_compound = true;
523 return fc;
44c440bc
PP
524}
525
526static inline
5cd6d0e5 527void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
44c440bc 528{
5cd6d0e5 529 BT_ASSERT(fc);
5cd6d0e5 530 g_free(fc);
44c440bc
PP
531}
532
533static inline
5cd6d0e5 534void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
44c440bc 535{
5cd6d0e5 536 BT_ASSERT(fc);
44c440bc 537
5cd6d0e5 538 if (fc->mappings) {
44c440bc
PP
539 uint64_t i;
540
5cd6d0e5
PP
541 for (i = 0; i < fc->mappings->len; i++) {
542 struct ctf_field_class_enum_mapping *mapping =
543 &g_array_index(fc->mappings,
544 struct ctf_field_class_enum_mapping, i);
44c440bc 545
5cd6d0e5 546 _ctf_field_class_enum_mapping_fini(mapping);
44c440bc
PP
547 }
548
5cd6d0e5 549 g_array_free(fc->mappings, TRUE);
44c440bc
PP
550 }
551
5cd6d0e5 552 g_free(fc);
44c440bc
PP
553}
554
555static inline
5cd6d0e5 556void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
44c440bc 557{
5cd6d0e5
PP
558 BT_ASSERT(fc);
559 g_free(fc);
44c440bc
PP
560}
561
562static inline
5cd6d0e5 563void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
44c440bc 564{
5cd6d0e5
PP
565 BT_ASSERT(fc);
566 g_free(fc);
44c440bc
PP
567}
568
569static inline
5cd6d0e5 570void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
44c440bc 571{
5cd6d0e5 572 BT_ASSERT(fc);
44c440bc 573
5cd6d0e5 574 if (fc->members) {
44c440bc
PP
575 uint64_t i;
576
5cd6d0e5
PP
577 for (i = 0; i < fc->members->len; i++) {
578 struct ctf_named_field_class *named_fc =
579 &g_array_index(fc->members,
580 struct ctf_named_field_class, i);
44c440bc 581
5cd6d0e5 582 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
583 }
584
5cd6d0e5 585 g_array_free(fc->members, TRUE);
44c440bc
PP
586 }
587
5cd6d0e5 588 g_free(fc);
44c440bc
PP
589}
590
591static inline
5cd6d0e5 592void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
44c440bc 593{
5cd6d0e5
PP
594 BT_ASSERT(fc);
595 ctf_field_class_destroy(fc->elem_fc);
44c440bc
PP
596}
597
598static inline
5cd6d0e5 599void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
44c440bc 600{
5cd6d0e5
PP
601 BT_ASSERT(fc);
602 _ctf_field_class_array_base_fini((void *) fc);
603 g_free(fc);
44c440bc
PP
604}
605
606static inline
5cd6d0e5 607void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
44c440bc 608{
5cd6d0e5
PP
609 BT_ASSERT(fc);
610 _ctf_field_class_array_base_fini((void *) fc);
44c440bc 611
5cd6d0e5
PP
612 if (fc->length_ref) {
613 g_string_free(fc->length_ref, TRUE);
44c440bc
PP
614 }
615
5cd6d0e5
PP
616 ctf_field_path_fini(&fc->length_path);
617 g_free(fc);
44c440bc
PP
618}
619
620static inline
5cd6d0e5 621void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
44c440bc 622{
5cd6d0e5 623 BT_ASSERT(fc);
44c440bc 624
5cd6d0e5 625 if (fc->options) {
44c440bc
PP
626 uint64_t i;
627
5cd6d0e5
PP
628 for (i = 0; i < fc->options->len; i++) {
629 struct ctf_named_field_class *named_fc =
630 &g_array_index(fc->options,
631 struct ctf_named_field_class, i);
44c440bc 632
5cd6d0e5 633 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
634 }
635
5cd6d0e5 636 g_array_free(fc->options, TRUE);
44c440bc
PP
637 }
638
5cd6d0e5
PP
639 if (fc->ranges) {
640 g_array_free(fc->ranges, TRUE);
44c440bc
PP
641 }
642
5cd6d0e5
PP
643 if (fc->tag_ref) {
644 g_string_free(fc->tag_ref, TRUE);
44c440bc
PP
645 }
646
5cd6d0e5
PP
647 ctf_field_path_fini(&fc->tag_path);
648 g_free(fc);
44c440bc
PP
649}
650
651static inline
5cd6d0e5 652void ctf_field_class_destroy(struct ctf_field_class *fc)
44c440bc 653{
5cd6d0e5 654 if (!fc) {
44c440bc
PP
655 return;
656 }
657
864cad70
PP
658 switch (fc->type) {
659 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 660 _ctf_field_class_int_destroy((void *) fc);
44c440bc 661 break;
864cad70 662 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 663 _ctf_field_class_enum_destroy((void *) fc);
44c440bc 664 break;
864cad70 665 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 666 _ctf_field_class_float_destroy((void *) fc);
44c440bc 667 break;
864cad70 668 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 669 _ctf_field_class_string_destroy((void *) fc);
44c440bc 670 break;
864cad70 671 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 672 _ctf_field_class_struct_destroy((void *) fc);
44c440bc 673 break;
864cad70 674 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 675 _ctf_field_class_array_destroy((void *) fc);
44c440bc 676 break;
864cad70 677 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 678 _ctf_field_class_sequence_destroy((void *) fc);
44c440bc 679 break;
864cad70 680 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 681 _ctf_field_class_variant_destroy((void *) fc);
44c440bc
PP
682 break;
683 default:
684 abort();
685 }
686}
687
688static inline
45c51519
PP
689struct ctf_range *ctf_field_class_enum_mapping_borrow_range_by_index(
690 struct ctf_field_class_enum_mapping *mapping, uint64_t index)
44c440bc 691{
45c51519
PP
692 BT_ASSERT(mapping);
693 BT_ASSERT(index < mapping->ranges->len);
694 return &g_array_index(mapping->ranges, struct ctf_range, index);
44c440bc
PP
695}
696
697static inline
5cd6d0e5
PP
698struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_index(
699 struct ctf_field_class_enum *fc, uint64_t index)
44c440bc 700{
5cd6d0e5
PP
701 BT_ASSERT(fc);
702 BT_ASSERT(index < fc->mappings->len);
703 return &g_array_index(fc->mappings, struct ctf_field_class_enum_mapping,
44c440bc
PP
704 index);
705}
706
45c51519
PP
707static inline
708struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_label(
709 struct ctf_field_class_enum *fc, const char *label)
710{
711 struct ctf_field_class_enum_mapping *ret_mapping = NULL;
712 uint64_t i;
713
714 BT_ASSERT(fc);
715 BT_ASSERT(label);
716
717 for (i = 0; i < fc->mappings->len; i++) {
718 struct ctf_field_class_enum_mapping *mapping =
719 ctf_field_class_enum_borrow_mapping_by_index(fc, i);
720
721 if (strcmp(mapping->label->str, label) == 0) {
722 ret_mapping = mapping;
723 goto end;
724 }
725 }
726
727end:
728 return ret_mapping;
729}
730
731static inline
732void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc,
733 const char *label, uint64_t u_lower, uint64_t u_upper)
734{
735 struct ctf_field_class_enum_mapping *mapping = NULL;
736 struct ctf_range range = {
737 .lower.u = u_lower,
738 .upper.u = u_upper,
739 };
740 uint64_t i;
741
742 BT_ASSERT(fc);
743 BT_ASSERT(label);
744
745 for (i = 0; i < fc->mappings->len; i++) {
746 mapping = ctf_field_class_enum_borrow_mapping_by_index(
747 fc, i);
748
749 if (strcmp(mapping->label->str, label) == 0) {
750 break;
751 }
752 }
753
754 if (i == fc->mappings->len) {
755 mapping = NULL;
756 }
757
758 if (!mapping) {
759 g_array_set_size(fc->mappings, fc->mappings->len + 1);
760 mapping = ctf_field_class_enum_borrow_mapping_by_index(
761 fc, fc->mappings->len - 1);
762 _ctf_field_class_enum_mapping_init(mapping);
763 g_string_assign(mapping->label, label);
764 }
765
766 g_array_append_val(mapping->ranges, range);
767}
768
44c440bc 769static inline
5cd6d0e5
PP
770struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_index(
771 struct ctf_field_class_struct *fc, uint64_t index)
44c440bc 772{
5cd6d0e5
PP
773 BT_ASSERT(fc);
774 BT_ASSERT(index < fc->members->len);
775 return &g_array_index(fc->members, struct ctf_named_field_class,
44c440bc
PP
776 index);
777}
778
779static inline
5cd6d0e5
PP
780struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_name(
781 struct ctf_field_class_struct *fc, const char *name)
44c440bc
PP
782{
783 uint64_t i;
5cd6d0e5 784 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 785
5cd6d0e5 786 BT_ASSERT(fc);
44c440bc
PP
787 BT_ASSERT(name);
788
5cd6d0e5
PP
789 for (i = 0; i < fc->members->len; i++) {
790 struct ctf_named_field_class *named_fc =
791 ctf_field_class_struct_borrow_member_by_index(fc, i);
44c440bc 792
5cd6d0e5
PP
793 if (strcmp(name, named_fc->name->str) == 0) {
794 ret_named_fc = named_fc;
44c440bc
PP
795 goto end;
796 }
797 }
798
799end:
5cd6d0e5 800 return ret_named_fc;
44c440bc
PP
801}
802
803static inline
5cd6d0e5
PP
804struct ctf_field_class *ctf_field_class_struct_borrow_member_field_class_by_name(
805 struct ctf_field_class_struct *struct_fc, const char *name)
44c440bc 806{
5cd6d0e5
PP
807 struct ctf_named_field_class *named_fc = NULL;
808 struct ctf_field_class *fc = NULL;
44c440bc 809
5cd6d0e5 810 if (!struct_fc) {
44c440bc
PP
811 goto end;
812 }
813
5cd6d0e5
PP
814 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
815 if (!named_fc) {
44c440bc
PP
816 goto end;
817 }
818
5cd6d0e5 819 fc = named_fc->fc;
44c440bc
PP
820
821end:
5cd6d0e5 822 return fc;
44c440bc
PP
823}
824
825static inline
5cd6d0e5
PP
826struct ctf_field_class_int *
827ctf_field_class_struct_borrow_member_int_field_class_by_name(
828 struct ctf_field_class_struct *struct_fc, const char *name)
44c440bc 829{
5cd6d0e5 830 struct ctf_field_class_int *int_fc = NULL;
44c440bc 831
5cd6d0e5
PP
832 int_fc = (void *)
833 ctf_field_class_struct_borrow_member_field_class_by_name(
834 struct_fc, name);
835 if (!int_fc) {
44c440bc
PP
836 goto end;
837 }
838
864cad70
PP
839 if (int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_INT &&
840 int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_ENUM) {
5cd6d0e5 841 int_fc = NULL;
44c440bc
PP
842 goto end;
843 }
844
845end:
5cd6d0e5 846 return int_fc;
44c440bc
PP
847}
848
45c51519
PP
849static inline
850void _ctf_named_field_class_unescape_orig_name(
851 struct ctf_named_field_class *named_fc)
852{
853 const char *name = named_fc->orig_name->str;
854
855 if (name[0] == '_') {
856 name++;
857 }
858
859 g_string_assign(named_fc->name, name);
860}
44c440bc
PP
861
862static inline
5cd6d0e5 863void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
45c51519 864 const char *orig_name, struct ctf_field_class *member_fc)
44c440bc 865{
5cd6d0e5 866 struct ctf_named_field_class *named_fc;
44c440bc 867
5cd6d0e5 868 BT_ASSERT(fc);
45c51519 869 BT_ASSERT(orig_name);
5cd6d0e5 870 g_array_set_size(fc->members, fc->members->len + 1);
44c440bc 871
5cd6d0e5
PP
872 named_fc = &g_array_index(fc->members, struct ctf_named_field_class,
873 fc->members->len - 1);
874 _ctf_named_field_class_init(named_fc);
45c51519
PP
875 g_string_assign(named_fc->orig_name, orig_name);
876 _ctf_named_field_class_unescape_orig_name(named_fc);
5cd6d0e5 877 named_fc->fc = member_fc;
44c440bc 878
5cd6d0e5
PP
879 if (member_fc->alignment > fc->base.alignment) {
880 fc->base.alignment = member_fc->alignment;
44c440bc
PP
881 }
882}
883
884static inline
5cd6d0e5
PP
885struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_index(
886 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 887{
5cd6d0e5
PP
888 BT_ASSERT(fc);
889 BT_ASSERT(index < fc->options->len);
890 return &g_array_index(fc->options, struct ctf_named_field_class,
44c440bc
PP
891 index);
892}
893
894static inline
5cd6d0e5
PP
895struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_name(
896 struct ctf_field_class_variant *fc, const char *name)
44c440bc
PP
897{
898 uint64_t i;
5cd6d0e5 899 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 900
5cd6d0e5 901 BT_ASSERT(fc);
44c440bc
PP
902 BT_ASSERT(name);
903
5cd6d0e5
PP
904 for (i = 0; i < fc->options->len; i++) {
905 struct ctf_named_field_class *named_fc =
906 ctf_field_class_variant_borrow_option_by_index(fc, i);
44c440bc 907
5cd6d0e5
PP
908 if (strcmp(name, named_fc->name->str) == 0) {
909 ret_named_fc = named_fc;
44c440bc
PP
910 goto end;
911 }
912 }
913
914end:
5cd6d0e5 915 return ret_named_fc;
44c440bc
PP
916}
917
918static inline
5cd6d0e5
PP
919struct ctf_field_class_variant_range *
920ctf_field_class_variant_borrow_range_by_index(
921 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 922{
5cd6d0e5
PP
923 BT_ASSERT(fc);
924 BT_ASSERT(index < fc->ranges->len);
925 return &g_array_index(fc->ranges, struct ctf_field_class_variant_range,
44c440bc
PP
926 index);
927}
928
929static inline
5cd6d0e5 930void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
45c51519 931 const char *orig_name, struct ctf_field_class *option_fc)
44c440bc 932{
5cd6d0e5 933 struct ctf_named_field_class *named_fc;
44c440bc 934
5cd6d0e5 935 BT_ASSERT(fc);
45c51519 936 BT_ASSERT(orig_name);
5cd6d0e5 937 g_array_set_size(fc->options, fc->options->len + 1);
44c440bc 938
5cd6d0e5
PP
939 named_fc = &g_array_index(fc->options, struct ctf_named_field_class,
940 fc->options->len - 1);
941 _ctf_named_field_class_init(named_fc);
45c51519
PP
942 g_string_assign(named_fc->orig_name, orig_name);
943 _ctf_named_field_class_unescape_orig_name(named_fc);
5cd6d0e5 944 named_fc->fc = option_fc;
44c440bc
PP
945}
946
947static inline
5cd6d0e5
PP
948void ctf_field_class_variant_set_tag_field_class(
949 struct ctf_field_class_variant *fc,
950 struct ctf_field_class_enum *tag_fc)
44c440bc
PP
951{
952 uint64_t option_i;
953
5cd6d0e5
PP
954 BT_ASSERT(fc);
955 BT_ASSERT(tag_fc);
956 fc->tag_fc = tag_fc;
44c440bc 957
5cd6d0e5 958 for (option_i = 0; option_i < fc->options->len; option_i++) {
45c51519 959 uint64_t range_i;
5cd6d0e5
PP
960 struct ctf_named_field_class *named_fc =
961 ctf_field_class_variant_borrow_option_by_index(
962 fc, option_i);
45c51519 963 struct ctf_field_class_enum_mapping *mapping;
44c440bc 964
45c51519
PP
965 mapping = ctf_field_class_enum_borrow_mapping_by_label(
966 tag_fc, named_fc->orig_name->str);
967 if (!mapping) {
968 continue;
969 }
44c440bc 970
45c51519
PP
971 for (range_i = 0; range_i < mapping->ranges->len;
972 range_i++) {
973 struct ctf_range *range =
974 ctf_field_class_enum_mapping_borrow_range_by_index(
975 mapping, range_i);
976 struct ctf_field_class_variant_range var_range;
44c440bc 977
45c51519
PP
978 var_range.range = *range;
979 var_range.option_index = option_i;
980 g_array_append_val(fc->ranges, var_range);
44c440bc
PP
981 }
982 }
983}
984
985static inline
5cd6d0e5
PP
986struct ctf_field_class *ctf_field_class_compound_borrow_field_class_by_index(
987 struct ctf_field_class *comp_fc, uint64_t index)
44c440bc 988{
5cd6d0e5 989 struct ctf_field_class *fc = NULL;
44c440bc 990
864cad70
PP
991 switch (comp_fc->type) {
992 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 993 {
5cd6d0e5
PP
994 struct ctf_named_field_class *named_fc =
995 ctf_field_class_struct_borrow_member_by_index(
996 (void *) comp_fc, index);
44c440bc 997
5cd6d0e5
PP
998 BT_ASSERT(named_fc);
999 fc = named_fc->fc;
44c440bc
PP
1000 break;
1001 }
864cad70 1002 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1003 {
5cd6d0e5
PP
1004 struct ctf_named_field_class *named_fc =
1005 ctf_field_class_variant_borrow_option_by_index(
1006 (void *) comp_fc, index);
44c440bc 1007
5cd6d0e5
PP
1008 BT_ASSERT(named_fc);
1009 fc = named_fc->fc;
44c440bc
PP
1010 break;
1011 }
864cad70
PP
1012 case CTF_FIELD_CLASS_TYPE_ARRAY:
1013 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc 1014 {
5cd6d0e5 1015 struct ctf_field_class_array_base *array_fc = (void *) comp_fc;
44c440bc 1016
5cd6d0e5 1017 fc = array_fc->elem_fc;
44c440bc
PP
1018 break;
1019 }
1020 default:
1021 break;
1022 }
1023
5cd6d0e5 1024 return fc;
44c440bc
PP
1025}
1026
1027static inline
5cd6d0e5 1028uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
44c440bc
PP
1029{
1030 uint64_t field_count;
1031
864cad70
PP
1032 switch (fc->type) {
1033 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 1034 {
5cd6d0e5 1035 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 1036
5cd6d0e5 1037 field_count = struct_fc->members->len;
44c440bc
PP
1038 break;
1039 }
864cad70 1040 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1041 {
5cd6d0e5 1042 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 1043
5cd6d0e5 1044 field_count = var_fc->options->len;
44c440bc
PP
1045 break;
1046 }
864cad70
PP
1047 case CTF_FIELD_CLASS_TYPE_ARRAY:
1048 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc
PP
1049 /*
1050 * Array and sequence types always contain a single
1051 * member (the element type).
1052 */
1053 field_count = 1;
1054 break;
1055 default:
1056 abort();
1057 }
1058
1059 return field_count;
1060}
1061
1062static inline
45c51519
PP
1063int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1064 struct ctf_field_class *fc, const char *orig_name)
44c440bc
PP
1065{
1066 int64_t ret_index = -1;
1067 uint64_t i;
1068
864cad70
PP
1069 switch (fc->type) {
1070 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 1071 {
5cd6d0e5 1072 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 1073
5cd6d0e5
PP
1074 for (i = 0; i < struct_fc->members->len; i++) {
1075 struct ctf_named_field_class *named_fc =
1076 ctf_field_class_struct_borrow_member_by_index(
1077 struct_fc, i);
44c440bc 1078
45c51519 1079 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
44c440bc
PP
1080 ret_index = (int64_t) i;
1081 goto end;
1082 }
1083 }
1084
1085 break;
1086 }
864cad70 1087 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1088 {
5cd6d0e5 1089 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 1090
5cd6d0e5
PP
1091 for (i = 0; i < var_fc->options->len; i++) {
1092 struct ctf_named_field_class *named_fc =
1093 ctf_field_class_variant_borrow_option_by_index(
1094 var_fc, i);
44c440bc 1095
45c51519 1096 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
44c440bc
PP
1097 ret_index = (int64_t) i;
1098 goto end;
1099 }
1100 }
1101
1102 break;
1103 }
1104 default:
1105 break;
1106 }
1107
1108end:
1109 return ret_index;
1110}
1111
1112static inline
1113void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index)
1114{
1115 BT_ASSERT(fp);
1116 g_array_append_val(fp->path, index);
1117}
1118
1119static inline
1120int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path *fp,
1121 uint64_t index)
1122{
1123 BT_ASSERT(fp);
1124 BT_ASSERT(index < fp->path->len);
1125 return g_array_index(fp->path, int64_t, index);
1126}
1127
1128static inline
1129void ctf_field_path_clear(struct ctf_field_path *fp)
1130{
1131 BT_ASSERT(fp);
1132 g_array_set_size(fp->path, 0);
1133}
1134
83ebb7f1
PP
1135static inline
1136const char *ctf_scope_string(enum ctf_scope scope)
1137{
1138 switch (scope) {
1139 case CTF_SCOPE_PACKET_HEADER:
8a432889 1140 return "PACKET_HEADER";
83ebb7f1 1141 case CTF_SCOPE_PACKET_CONTEXT:
8a432889 1142 return "PACKET_CONTEXT";
83ebb7f1 1143 case CTF_SCOPE_EVENT_HEADER:
8a432889 1144 return "EVENT_HEADER";
83ebb7f1 1145 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
8a432889 1146 return "EVENT_COMMON_CONTEXT";
83ebb7f1 1147 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
8a432889 1148 return "EVENT_SPECIFIC_CONTEXT";
83ebb7f1 1149 case CTF_SCOPE_EVENT_PAYLOAD:
8a432889 1150 return "EVENT_PAYLOAD";
83ebb7f1
PP
1151 default:
1152 abort();
1153 }
1154}
1155
44c440bc
PP
1156static inline
1157GString *ctf_field_path_string(struct ctf_field_path *path)
1158{
1159 GString *str = g_string_new(NULL);
1160 uint64_t i;
1161
1162 BT_ASSERT(path);
1163
1164 if (!str) {
1165 goto end;
1166 }
1167
83ebb7f1 1168 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
44c440bc
PP
1169
1170 for (i = 0; i < path->path->len; i++) {
1171 g_string_append_printf(str, ", %" PRId64,
1172 ctf_field_path_borrow_index_by_index(path, i));
1173 }
1174
1175 g_string_append(str, "]");
1176
1177end:
1178 return str;
1179}
1180
1181static inline
5cd6d0e5 1182struct ctf_field_class *ctf_field_path_borrow_field_class(
44c440bc
PP
1183 struct ctf_field_path *field_path,
1184 struct ctf_trace_class *tc,
1185 struct ctf_stream_class *sc,
1186 struct ctf_event_class *ec)
1187{
1188 uint64_t i;
5cd6d0e5 1189 struct ctf_field_class *fc;
44c440bc
PP
1190
1191 switch (field_path->root) {
83ebb7f1 1192 case CTF_SCOPE_PACKET_HEADER:
5cd6d0e5 1193 fc = tc->packet_header_fc;
44c440bc 1194 break;
83ebb7f1 1195 case CTF_SCOPE_PACKET_CONTEXT:
5cd6d0e5 1196 fc = sc->packet_context_fc;
44c440bc 1197 break;
83ebb7f1 1198 case CTF_SCOPE_EVENT_HEADER:
5cd6d0e5 1199 fc = sc->event_header_fc;
44c440bc 1200 break;
83ebb7f1 1201 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
5cd6d0e5 1202 fc = sc->event_common_context_fc;
44c440bc 1203 break;
83ebb7f1 1204 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
5cd6d0e5 1205 fc = ec->spec_context_fc;
44c440bc 1206 break;
83ebb7f1 1207 case CTF_SCOPE_EVENT_PAYLOAD:
5cd6d0e5 1208 fc = ec->payload_fc;
44c440bc
PP
1209 break;
1210 default:
1211 abort();
1212 }
1213
5cd6d0e5 1214 BT_ASSERT(fc);
44c440bc
PP
1215
1216 for (i = 0; i < field_path->path->len; i++) {
1217 int64_t child_index =
1218 ctf_field_path_borrow_index_by_index(field_path, i);
5cd6d0e5
PP
1219 struct ctf_field_class *child_fc =
1220 ctf_field_class_compound_borrow_field_class_by_index(
1221 fc, child_index);
1222 BT_ASSERT(child_fc);
1223 fc = child_fc;
44c440bc
PP
1224 }
1225
5cd6d0e5
PP
1226 BT_ASSERT(fc);
1227 return fc;
44c440bc
PP
1228}
1229
1230static inline
5cd6d0e5 1231struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
44c440bc
PP
1232
1233static inline
5cd6d0e5
PP
1234void ctf_field_class_bit_array_copy_content(
1235 struct ctf_field_class_bit_array *dst_fc,
1236 struct ctf_field_class_bit_array *src_fc)
44c440bc 1237{
5cd6d0e5
PP
1238 BT_ASSERT(dst_fc);
1239 BT_ASSERT(src_fc);
1240 dst_fc->byte_order = src_fc->byte_order;
1241 dst_fc->size = src_fc->size;
44c440bc
PP
1242}
1243
1244static inline
5cd6d0e5
PP
1245void ctf_field_class_int_copy_content(
1246 struct ctf_field_class_int *dst_fc,
1247 struct ctf_field_class_int *src_fc)
44c440bc 1248{
5cd6d0e5
PP
1249 ctf_field_class_bit_array_copy_content((void *) dst_fc, (void *) src_fc);
1250 dst_fc->meaning = src_fc->meaning;
1251 dst_fc->is_signed = src_fc->is_signed;
1252 dst_fc->disp_base = src_fc->disp_base;
1253 dst_fc->encoding = src_fc->encoding;
398454ed 1254 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
5cd6d0e5 1255 dst_fc->storing_index = src_fc->storing_index;
44c440bc
PP
1256}
1257
1258static inline
5cd6d0e5
PP
1259struct ctf_field_class_int *_ctf_field_class_int_copy(
1260 struct ctf_field_class_int *fc)
44c440bc 1261{
5cd6d0e5 1262 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
44c440bc 1263
5cd6d0e5
PP
1264 BT_ASSERT(copy_fc);
1265 ctf_field_class_int_copy_content(copy_fc, fc);
1266 return copy_fc;
44c440bc
PP
1267}
1268
1269static inline
5cd6d0e5
PP
1270struct ctf_field_class_enum *_ctf_field_class_enum_copy(
1271 struct ctf_field_class_enum *fc)
44c440bc 1272{
5cd6d0e5 1273 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
44c440bc
PP
1274 uint64_t i;
1275
5cd6d0e5
PP
1276 BT_ASSERT(copy_fc);
1277 ctf_field_class_int_copy_content((void *) copy_fc, (void *) fc);
44c440bc 1278
5cd6d0e5 1279 for (i = 0; i < fc->mappings->len; i++) {
45c51519
PP
1280 uint64_t range_i;
1281
5cd6d0e5
PP
1282 struct ctf_field_class_enum_mapping *mapping =
1283 &g_array_index(fc->mappings,
1284 struct ctf_field_class_enum_mapping, i);
44c440bc 1285
45c51519
PP
1286 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1287 struct ctf_range *range =
1288 &g_array_index(mapping->ranges,
1289 struct ctf_range, range_i);
1290
1291 ctf_field_class_enum_map_range(copy_fc,
1292 mapping->label->str, range->lower.u,
1293 range->upper.u);
1294 }
44c440bc
PP
1295 }
1296
5cd6d0e5 1297 return copy_fc;
44c440bc
PP
1298}
1299
1300static inline
5cd6d0e5
PP
1301struct ctf_field_class_float *_ctf_field_class_float_copy(
1302 struct ctf_field_class_float *fc)
44c440bc 1303{
5cd6d0e5 1304 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
44c440bc 1305
5cd6d0e5
PP
1306 BT_ASSERT(copy_fc);
1307 ctf_field_class_bit_array_copy_content((void *) copy_fc, (void *) fc);
1308 return copy_fc;
44c440bc
PP
1309}
1310
1311static inline
5cd6d0e5
PP
1312struct ctf_field_class_string *_ctf_field_class_string_copy(
1313 struct ctf_field_class_string *fc)
44c440bc 1314{
5cd6d0e5 1315 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
44c440bc 1316
5cd6d0e5
PP
1317 BT_ASSERT(copy_fc);
1318 return copy_fc;
44c440bc
PP
1319}
1320
1321static inline
5cd6d0e5
PP
1322struct ctf_field_class_struct *_ctf_field_class_struct_copy(
1323 struct ctf_field_class_struct *fc)
44c440bc 1324{
5cd6d0e5 1325 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
44c440bc
PP
1326 uint64_t i;
1327
5cd6d0e5 1328 BT_ASSERT(copy_fc);
44c440bc 1329
5cd6d0e5
PP
1330 for (i = 0; i < fc->members->len; i++) {
1331 struct ctf_named_field_class *named_fc =
1332 &g_array_index(fc->members,
1333 struct ctf_named_field_class, i);
44c440bc 1334
5cd6d0e5
PP
1335 ctf_field_class_struct_append_member(copy_fc,
1336 named_fc->name->str,
1337 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1338 }
1339
5cd6d0e5 1340 return copy_fc;
44c440bc
PP
1341}
1342
1343static inline
1344void ctf_field_path_copy_content(struct ctf_field_path *dst_fp,
1345 struct ctf_field_path *src_fp)
1346{
1347 uint64_t i;
1348
1349 BT_ASSERT(dst_fp);
1350 BT_ASSERT(src_fp);
1351 dst_fp->root = src_fp->root;
1352 ctf_field_path_clear(dst_fp);
1353
1354 for (i = 0; i < src_fp->path->len; i++) {
1355 int64_t index = ctf_field_path_borrow_index_by_index(
1356 src_fp, i);
1357
1358 ctf_field_path_append_index(dst_fp, index);
1359 }
1360}
1361
1362static inline
5cd6d0e5
PP
1363struct ctf_field_class_variant *_ctf_field_class_variant_copy(
1364 struct ctf_field_class_variant *fc)
44c440bc 1365{
5cd6d0e5
PP
1366 struct ctf_field_class_variant *copy_fc =
1367 ctf_field_class_variant_create();
44c440bc
PP
1368 uint64_t i;
1369
5cd6d0e5 1370 BT_ASSERT(copy_fc);
44c440bc 1371
5cd6d0e5
PP
1372 for (i = 0; i < fc->options->len; i++) {
1373 struct ctf_named_field_class *named_fc =
1374 &g_array_index(fc->options,
1375 struct ctf_named_field_class, i);
44c440bc 1376
5cd6d0e5
PP
1377 ctf_field_class_variant_append_option(copy_fc,
1378 named_fc->name->str,
1379 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1380 }
1381
5cd6d0e5
PP
1382 for (i = 0; i < fc->ranges->len; i++) {
1383 struct ctf_field_class_variant_range *range =
1384 &g_array_index(fc->ranges,
1385 struct ctf_field_class_variant_range, i);
44c440bc 1386
5cd6d0e5 1387 g_array_append_val(copy_fc->ranges, *range);
44c440bc
PP
1388 }
1389
5cd6d0e5
PP
1390 ctf_field_path_copy_content(&copy_fc->tag_path, &fc->tag_path);
1391 g_string_assign(copy_fc->tag_ref, fc->tag_ref->str);
1392 copy_fc->stored_tag_index = fc->stored_tag_index;
1393 return copy_fc;
44c440bc
PP
1394}
1395
1396static inline
5cd6d0e5
PP
1397void ctf_field_class_array_base_copy_content(
1398 struct ctf_field_class_array_base *dst_fc,
1399 struct ctf_field_class_array_base *src_fc)
44c440bc 1400{
5cd6d0e5
PP
1401 BT_ASSERT(dst_fc);
1402 BT_ASSERT(src_fc);
1403 dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc);
1404 dst_fc->is_text = src_fc->is_text;
44c440bc
PP
1405}
1406
1407static inline
5cd6d0e5
PP
1408struct ctf_field_class_array *_ctf_field_class_array_copy(
1409 struct ctf_field_class_array *fc)
44c440bc 1410{
5cd6d0e5 1411 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
44c440bc 1412
5cd6d0e5
PP
1413 BT_ASSERT(copy_fc);
1414 ctf_field_class_array_base_copy_content((void *) copy_fc, (void *) fc);
1415 copy_fc->length = fc->length;
1416 return copy_fc;
44c440bc
PP
1417}
1418
1419static inline
5cd6d0e5
PP
1420struct ctf_field_class_sequence *_ctf_field_class_sequence_copy(
1421 struct ctf_field_class_sequence *fc)
44c440bc 1422{
5cd6d0e5
PP
1423 struct ctf_field_class_sequence *copy_fc =
1424 ctf_field_class_sequence_create();
44c440bc 1425
5cd6d0e5
PP
1426 BT_ASSERT(copy_fc);
1427 ctf_field_class_array_base_copy_content((void *) copy_fc, (void *) fc);
1428 ctf_field_path_copy_content(&copy_fc->length_path, &fc->length_path);
1429 g_string_assign(copy_fc->length_ref, fc->length_ref->str);
1430 copy_fc->stored_length_index = fc->stored_length_index;
1431 return copy_fc;
44c440bc
PP
1432}
1433
1434static inline
5cd6d0e5 1435struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
44c440bc 1436{
5cd6d0e5 1437 struct ctf_field_class *copy_fc = NULL;
44c440bc 1438
5cd6d0e5 1439 if (!fc) {
44c440bc
PP
1440 goto end;
1441 }
1442
1443 /*
1444 * Translation should not have happened yet.
1445 */
5cd6d0e5 1446 BT_ASSERT(!fc->ir_fc);
44c440bc 1447
864cad70
PP
1448 switch (fc->type) {
1449 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 1450 copy_fc = (void *) _ctf_field_class_int_copy((void *) fc);
44c440bc 1451 break;
864cad70 1452 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 1453 copy_fc = (void *) _ctf_field_class_enum_copy((void *) fc);
44c440bc 1454 break;
864cad70 1455 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 1456 copy_fc = (void *) _ctf_field_class_float_copy((void *) fc);
44c440bc 1457 break;
864cad70 1458 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 1459 copy_fc = (void *) _ctf_field_class_string_copy((void *) fc);
44c440bc 1460 break;
864cad70 1461 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 1462 copy_fc = (void *) _ctf_field_class_struct_copy((void *) fc);
44c440bc 1463 break;
864cad70 1464 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 1465 copy_fc = (void *) _ctf_field_class_array_copy((void *) fc);
44c440bc 1466 break;
864cad70 1467 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 1468 copy_fc = (void *) _ctf_field_class_sequence_copy((void *) fc);
44c440bc 1469 break;
864cad70 1470 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 1471 copy_fc = (void *) _ctf_field_class_variant_copy((void *) fc);
44c440bc
PP
1472 break;
1473 default:
1474 abort();
1475 }
1476
864cad70 1477 copy_fc->type = fc->type;
5cd6d0e5
PP
1478 copy_fc->alignment = fc->alignment;
1479 copy_fc->in_ir = fc->in_ir;
44c440bc
PP
1480
1481end:
5cd6d0e5 1482 return copy_fc;
44c440bc
PP
1483}
1484
1485static inline
1486struct ctf_event_class *ctf_event_class_create(void)
1487{
1488 struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1);
1489
1490 BT_ASSERT(ec);
1491 ec->name = g_string_new(NULL);
1492 BT_ASSERT(ec->name);
1493 ec->emf_uri = g_string_new(NULL);
1494 BT_ASSERT(ec->emf_uri);
6da709aa 1495 ec->is_log_level_set = false;
44c440bc
PP
1496 return ec;
1497}
1498
6da709aa
JR
1499static inline
1500void ctf_event_class_set_log_level(struct ctf_event_class *ec,
1501 enum bt_event_class_log_level log_level)
1502{
1503 BT_ASSERT(ec);
1504 ec->log_level = log_level;
1505 ec->is_log_level_set = true;
1506}
1507
44c440bc
PP
1508static inline
1509void ctf_event_class_destroy(struct ctf_event_class *ec)
1510{
1511 if (!ec) {
1512 return;
1513 }
1514
1515 if (ec->name) {
1516 g_string_free(ec->name, TRUE);
1517 }
1518
1519 if (ec->emf_uri) {
1520 g_string_free(ec->emf_uri, TRUE);
1521 }
1522
5cd6d0e5
PP
1523 ctf_field_class_destroy(ec->spec_context_fc);
1524 ctf_field_class_destroy(ec->payload_fc);
44c440bc
PP
1525 g_free(ec);
1526}
1527
1528static inline
1529struct ctf_stream_class *ctf_stream_class_create(void)
1530{
1531 struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1);
1532
1533 BT_ASSERT(sc);
1534 sc->event_classes = g_ptr_array_new_with_free_func(
1535 (GDestroyNotify) ctf_event_class_destroy);
1536 BT_ASSERT(sc->event_classes);
1537 sc->event_classes_by_id = g_hash_table_new(g_direct_hash,
1538 g_direct_equal);
1539 BT_ASSERT(sc->event_classes_by_id);
1540 return sc;
1541}
1542
1543static inline
1544void ctf_stream_class_destroy(struct ctf_stream_class *sc)
1545{
1546 if (!sc) {
1547 return;
1548 }
1549
1550 if (sc->event_classes) {
1551 g_ptr_array_free(sc->event_classes, TRUE);
1552 }
1553
1554 if (sc->event_classes_by_id) {
1555 g_hash_table_destroy(sc->event_classes_by_id);
1556 }
1557
5cd6d0e5
PP
1558 ctf_field_class_destroy(sc->packet_context_fc);
1559 ctf_field_class_destroy(sc->event_header_fc);
1560 ctf_field_class_destroy(sc->event_common_context_fc);
44c440bc
PP
1561 g_free(sc);
1562}
1563
1564static inline
1565void ctf_stream_class_append_event_class(struct ctf_stream_class *sc,
1566 struct ctf_event_class *ec)
1567{
1568 g_ptr_array_add(sc->event_classes, ec);
1569 g_hash_table_insert(sc->event_classes_by_id,
1570 GUINT_TO_POINTER((guint) ec->id), ec);
1571}
1572
1573static inline
1574struct ctf_event_class *ctf_stream_class_borrow_event_class_by_id(
864cad70 1575 struct ctf_stream_class *sc, uint64_t type)
44c440bc
PP
1576{
1577 BT_ASSERT(sc);
1578 return g_hash_table_lookup(sc->event_classes_by_id,
864cad70 1579 GUINT_TO_POINTER((guint) type));
44c440bc
PP
1580}
1581
1582static inline
1583void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry)
1584{
1585 BT_ASSERT(entry);
1586 entry->name = g_string_new(NULL);
1587 BT_ASSERT(entry->name);
1588 entry->value.str = g_string_new(NULL);
1589 BT_ASSERT(entry->value.str);
1590}
1591
1592static inline
1593void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry)
1594{
1595 BT_ASSERT(entry);
1596
1597 if (entry->name) {
1598 g_string_free(entry->name, TRUE);
1599 }
1600
1601 if (entry->value.str) {
1602 g_string_free(entry->value.str, TRUE);
1603 }
1604}
1605
0f2d58c9
PP
1606static inline
1607struct ctf_clock_class *ctf_clock_class_create(void)
1608{
1609 struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1);
1610
1611 BT_ASSERT(cc);
1612 cc->name = g_string_new(NULL);
1613 BT_ASSERT(cc->name);
1614 cc->description = g_string_new(NULL);
1615 BT_ASSERT(cc->description);
1616 return cc;
1617}
1618
1619static inline
1620void ctf_clock_class_destroy(struct ctf_clock_class *cc)
1621{
1622 if (!cc) {
1623 return;
1624 }
1625
1626 if (cc->name) {
1627 g_string_free(cc->name, TRUE);
1628 }
1629
1630 if (cc->description) {
1631 g_string_free(cc->description, TRUE);
1632 }
1633
1634 bt_clock_class_put_ref(cc->ir_cc);
1635 g_free(cc);
1636}
1637
44c440bc
PP
1638static inline
1639struct ctf_trace_class *ctf_trace_class_create(void)
1640{
1641 struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1);
1642
1643 BT_ASSERT(tc);
e2bd7d40 1644 tc->default_byte_order = CTF_BYTE_ORDER_UNKNOWN;
44c440bc 1645 tc->clock_classes = g_ptr_array_new_with_free_func(
0f2d58c9 1646 (GDestroyNotify) ctf_clock_class_destroy);
44c440bc
PP
1647 BT_ASSERT(tc->clock_classes);
1648 tc->stream_classes = g_ptr_array_new_with_free_func(
1649 (GDestroyNotify) ctf_stream_class_destroy);
1650 BT_ASSERT(tc->stream_classes);
1651 tc->env_entries = g_array_new(FALSE, TRUE,
1652 sizeof(struct ctf_trace_class_env_entry));
1653 return tc;
1654}
1655
1656static inline
1657void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1658{
1659 if (!tc) {
1660 return;
1661 }
1662
5cd6d0e5 1663 ctf_field_class_destroy(tc->packet_header_fc);
44c440bc
PP
1664
1665 if (tc->clock_classes) {
1666 g_ptr_array_free(tc->clock_classes, TRUE);
1667 }
1668
1669 if (tc->stream_classes) {
1670 g_ptr_array_free(tc->stream_classes, TRUE);
1671 }
1672
1673 if (tc->env_entries) {
1674 uint64_t i;
1675
1676 for (i = 0; i < tc->env_entries->len; i++) {
1677 struct ctf_trace_class_env_entry *entry =
1678 &g_array_index(tc->env_entries,
1679 struct ctf_trace_class_env_entry, i);
1680
1681 _ctf_trace_class_env_entry_fini(entry);
1682 }
1683
1684 g_array_free(tc->env_entries, TRUE);
1685 }
1686
1687 g_free(tc);
1688}
1689
1690static inline
1691void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc,
1692 const char *name, enum ctf_trace_class_env_entry_type type,
1693 const char *str_value, int64_t i_value)
1694{
1695 struct ctf_trace_class_env_entry *entry;
1696
1697 BT_ASSERT(tc);
1698 BT_ASSERT(name);
1699 g_array_set_size(tc->env_entries, tc->env_entries->len + 1);
1700
1701 entry = &g_array_index(tc->env_entries,
1702 struct ctf_trace_class_env_entry, tc->env_entries->len - 1);
1703 entry->type = type;
1704 _ctf_trace_class_env_entry_init(entry);
1705 g_string_assign(entry->name, name);
1706
1707 if (str_value) {
1708 g_string_assign(entry->value.str, str_value);
1709 }
1710
1711 entry->value.i = i_value;
1712}
1713
1714static inline
1715struct ctf_stream_class *ctf_trace_class_borrow_stream_class_by_id(
1716 struct ctf_trace_class *tc, uint64_t id)
1717{
1718 uint64_t i;
1719 struct ctf_stream_class *ret_sc = NULL;
1720
1721 BT_ASSERT(tc);
1722
1723 for (i = 0; i < tc->stream_classes->len; i++) {
1724 struct ctf_stream_class *sc = tc->stream_classes->pdata[i];
1725
1726 if (sc->id == id) {
1727 ret_sc = sc;
1728 goto end;
1729 }
1730 }
1731
1732end:
1733 return ret_sc;
1734}
1735
1736static inline
0f2d58c9 1737struct ctf_clock_class *ctf_trace_class_borrow_clock_class_by_name(
44c440bc
PP
1738 struct ctf_trace_class *tc, const char *name)
1739{
1740 uint64_t i;
0f2d58c9 1741 struct ctf_clock_class *ret_cc = NULL;
44c440bc
PP
1742
1743 BT_ASSERT(tc);
1744 BT_ASSERT(name);
1745
1746 for (i = 0; i < tc->clock_classes->len; i++) {
0f2d58c9 1747 struct ctf_clock_class *cc = tc->clock_classes->pdata[i];
44c440bc 1748
0f2d58c9
PP
1749 BT_ASSERT(cc->name);
1750 if (strcmp(cc->name->str, name) == 0) {
44c440bc
PP
1751 ret_cc = cc;
1752 goto end;
1753 }
1754 }
1755
1756end:
1757 return ret_cc;
1758}
1759
1760static inline
1761struct ctf_trace_class_env_entry *ctf_trace_class_borrow_env_entry_by_index(
1762 struct ctf_trace_class *tc, uint64_t index)
1763{
1764 BT_ASSERT(tc);
1765 BT_ASSERT(index < tc->env_entries->len);
1766 return &g_array_index(tc->env_entries, struct ctf_trace_class_env_entry,
1767 index);
1768}
1769
1770static inline
1771struct ctf_trace_class_env_entry *ctf_trace_class_borrow_env_entry_by_name(
1772 struct ctf_trace_class *tc, const char *name)
1773{
1774 struct ctf_trace_class_env_entry *ret_entry = NULL;
1775 uint64_t i;
1776
1777 BT_ASSERT(tc);
1778 BT_ASSERT(name);
1779
1780 for (i = 0; i < tc->env_entries->len; i++) {
1781 struct ctf_trace_class_env_entry *env_entry =
1782 ctf_trace_class_borrow_env_entry_by_index(tc, i);
1783
1784 if (strcmp(env_entry->name->str, name) == 0) {
1785 ret_entry = env_entry;
1786 goto end;
1787 }
1788 }
1789
1790end:
1791 return ret_entry;
1792}
1793
1794#endif /* _CTF_META_H */
This page took 0.126541 seconds and 4 git commands to generate.