lib/ctf-ir/trace.c: validate PH `packet_seq_num` field
[babeltrace.git] / lib / ctf-ir / trace.c
1 /*
2 * trace.c
3 *
4 * Babeltrace CTF IR - Trace
5 *
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "TRACE"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/ctf-ir/trace-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/stream-internal.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/event-class.h>
38 #include <babeltrace/ctf-ir/event-class-internal.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/ctf-writer/clock-internal.h>
41 #include <babeltrace/ctf-ir/field-types-internal.h>
42 #include <babeltrace/ctf-ir/attributes-internal.h>
43 #include <babeltrace/ctf-ir/validation-internal.h>
44 #include <babeltrace/ctf-ir/visitor-internal.h>
45 #include <babeltrace/ctf-ir/utils.h>
46 #include <babeltrace/compiler-internal.h>
47 #include <babeltrace/values.h>
48 #include <babeltrace/values-internal.h>
49 #include <babeltrace/ref.h>
50 #include <babeltrace/types.h>
51 #include <babeltrace/endian-internal.h>
52 #include <inttypes.h>
53 #include <stdint.h>
54 #include <string.h>
55 #include <stdlib.h>
56
57 #define DEFAULT_IDENTIFIER_SIZE 128
58 #define DEFAULT_METADATA_STRING_SIZE 4096
59
60 struct listener_wrapper {
61 bt_ctf_listener_cb listener;
62 void *data;
63 };
64
65 struct bt_ctf_trace_is_static_listener_elem {
66 bt_ctf_trace_is_static_listener func;
67 void *data;
68 };
69
70 static
71 void bt_ctf_trace_destroy(struct bt_object *obj);
72 static
73 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
74
75 static
76 const unsigned int field_type_aliases_alignments[] = {
77 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
78 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
79 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
80 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
81 };
82
83 static
84 const unsigned int field_type_aliases_sizes[] = {
85 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
86 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
87 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
88 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
89 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
90 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
91 };
92
93 struct bt_ctf_trace *bt_ctf_trace_create(void)
94 {
95 struct bt_ctf_trace *trace = NULL;
96
97 trace = g_new0(struct bt_ctf_trace, 1);
98 if (!trace) {
99 BT_LOGE_STR("Failed to allocate one trace.");
100 goto error;
101 }
102
103 BT_LOGD_STR("Creating trace object.");
104 trace->native_byte_order = BT_CTF_BYTE_ORDER_UNSPECIFIED;
105 bt_object_init(trace, bt_ctf_trace_destroy);
106 trace->clocks = g_ptr_array_new_with_free_func(
107 (GDestroyNotify) bt_put);
108 if (!trace->clocks) {
109 BT_LOGE_STR("Failed to allocate one GPtrArray.");
110 goto error;
111 }
112
113 trace->streams = g_ptr_array_new_with_free_func(
114 (GDestroyNotify) bt_object_release);
115 if (!trace->streams) {
116 BT_LOGE_STR("Failed to allocate one GPtrArray.");
117 goto error;
118 }
119
120 trace->stream_classes = g_ptr_array_new_with_free_func(
121 (GDestroyNotify) bt_object_release);
122 if (!trace->stream_classes) {
123 BT_LOGE_STR("Failed to allocate one GPtrArray.");
124 goto error;
125 }
126
127 /* Create the environment array object */
128 trace->environment = bt_ctf_attributes_create();
129 if (!trace->environment) {
130 BT_LOGE_STR("Cannot create empty attributes object.");
131 goto error;
132 }
133
134 trace->listeners = g_ptr_array_new_with_free_func(
135 (GDestroyNotify) g_free);
136 if (!trace->listeners) {
137 BT_LOGE_STR("Failed to allocate one GPtrArray.");
138 goto error;
139 }
140
141 trace->is_static_listeners = g_array_new(FALSE, TRUE,
142 sizeof(struct bt_ctf_trace_is_static_listener_elem));
143 if (!trace->is_static_listeners) {
144 BT_LOGE_STR("Failed to allocate one GArray.");
145 goto error;
146 }
147
148 BT_LOGD("Created trace object: addr=%p", trace);
149 return trace;
150
151 error:
152 BT_PUT(trace);
153 return trace;
154 }
155
156 const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
157 {
158 const char *name = NULL;
159
160 if (!trace) {
161 BT_LOGW_STR("Invalid parameter: trace is NULL.");
162 goto end;
163 }
164
165 if (!trace->name) {
166 goto end;
167 }
168
169 name = trace->name->str;
170 end:
171 return name;
172 }
173
174 int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name)
175 {
176 int ret = 0;
177
178 if (!trace) {
179 BT_LOGW_STR("Invalid parameter: trace is NULL.");
180 ret = -1;
181 goto end;
182 }
183
184 if (!name) {
185 BT_LOGW_STR("Invalid parameter: name is NULL.");
186 ret = -1;
187 goto end;
188 }
189
190 if (trace->frozen) {
191 BT_LOGW("Invalid parameter: trace is frozen: "
192 "addr=%p, name=\"%s\"",
193 trace, bt_ctf_trace_get_name(trace));
194 ret = -1;
195 goto end;
196 }
197
198 trace->name = trace->name ? g_string_assign(trace->name, name) :
199 g_string_new(name);
200 if (!trace->name) {
201 BT_LOGE_STR("Failed to allocate one GString.");
202 ret = -1;
203 goto end;
204 }
205
206 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace, name);
207
208 end:
209 return ret;
210 }
211
212 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace)
213 {
214 return trace && trace->uuid_set ? trace->uuid : NULL;
215 }
216
217 int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid)
218 {
219 int ret = 0;
220
221 if (!trace) {
222 BT_LOGW_STR("Invalid parameter: trace is NULL.");
223 ret = -1;
224 goto end;
225 }
226
227 if (!uuid) {
228 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
229 ret = -1;
230 goto end;
231 }
232
233 if (trace->frozen) {
234 BT_LOGW("Invalid parameter: trace is frozen: "
235 "addr=%p, name=\"%s\"",
236 trace, bt_ctf_trace_get_name(trace));
237 ret = -1;
238 goto end;
239 }
240
241 memcpy(trace->uuid, uuid, BABELTRACE_UUID_LEN);
242 trace->uuid_set = BT_TRUE;
243 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
244 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
245 trace, bt_ctf_trace_get_name(trace),
246 (unsigned int) uuid[0],
247 (unsigned int) uuid[1],
248 (unsigned int) uuid[2],
249 (unsigned int) uuid[3],
250 (unsigned int) uuid[4],
251 (unsigned int) uuid[5],
252 (unsigned int) uuid[6],
253 (unsigned int) uuid[7],
254 (unsigned int) uuid[8],
255 (unsigned int) uuid[9],
256 (unsigned int) uuid[10],
257 (unsigned int) uuid[11],
258 (unsigned int) uuid[12],
259 (unsigned int) uuid[13],
260 (unsigned int) uuid[14],
261 (unsigned int) uuid[15]);
262
263 end:
264 return ret;
265 }
266
267 void bt_ctf_trace_destroy(struct bt_object *obj)
268 {
269 struct bt_ctf_trace *trace;
270
271 trace = container_of(obj, struct bt_ctf_trace, base);
272
273 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
274 trace, bt_ctf_trace_get_name(trace));
275
276 if (trace->environment) {
277 BT_LOGD_STR("Destroying environment attributes.");
278 bt_ctf_attributes_destroy(trace->environment);
279 }
280
281 if (trace->name) {
282 g_string_free(trace->name, TRUE);
283 }
284
285 if (trace->clocks) {
286 BT_LOGD_STR("Putting clock classes.");
287 g_ptr_array_free(trace->clocks, TRUE);
288 }
289
290 if (trace->streams) {
291 BT_LOGD_STR("Destroying streams.");
292 g_ptr_array_free(trace->streams, TRUE);
293 }
294
295 if (trace->stream_classes) {
296 BT_LOGD_STR("Destroying stream classes.");
297 g_ptr_array_free(trace->stream_classes, TRUE);
298 }
299
300 if (trace->listeners) {
301 g_ptr_array_free(trace->listeners, TRUE);
302 }
303
304 if (trace->is_static_listeners) {
305 g_array_free(trace->is_static_listeners, TRUE);
306 }
307
308 BT_LOGD_STR("Putting packet header field type.");
309 bt_put(trace->packet_header_type);
310 g_free(trace);
311 }
312
313 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
314 const char *name, struct bt_value *value)
315 {
316 int ret = 0;
317
318 if (!trace) {
319 BT_LOGW_STR("Invalid parameter: trace is NULL.");
320 ret = -1;
321 goto end;
322 }
323
324 if (!name) {
325 BT_LOGW_STR("Invalid parameter: name is NULL.");
326 ret = -1;
327 goto end;
328 }
329
330 if (!value) {
331 BT_LOGW_STR("Invalid parameter: value is NULL.");
332 ret = -1;
333 goto end;
334 }
335
336 if (bt_ctf_validate_identifier(name)) {
337 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
338 "trace-addr=%p, trace-name=\"%s\", "
339 "env-name=\"%s\"",
340 trace, bt_ctf_trace_get_name(trace), name);
341 ret = -1;
342 goto end;
343 }
344
345 if (!bt_value_is_integer(value) && !bt_value_is_string(value)) {
346 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
347 "trace-addr=%p, trace-name=\"%s\", "
348 "env-name=\"%s\", env-value-type=%s",
349 trace, bt_ctf_trace_get_name(trace), name,
350 bt_value_type_string(bt_value_get_type(value)));
351 ret = -1;
352 goto end;
353 }
354
355 if (trace->is_static) {
356 BT_LOGW("Invalid parameter: trace is static: "
357 "addr=%p, name=\"%s\"",
358 trace, bt_ctf_trace_get_name(trace));
359 ret = -1;
360 goto end;
361 }
362
363 if (trace->frozen) {
364 /*
365 * New environment fields may be added to a frozen trace,
366 * but existing fields may not be changed.
367 *
368 * The object passed is frozen like all other attributes.
369 */
370 struct bt_value *attribute =
371 bt_ctf_attributes_get_field_value_by_name(
372 trace->environment, name);
373
374 if (attribute) {
375 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
376 "trace-addr=%p, trace-name=\"%s\", "
377 "env-name=\"%s\"",
378 trace, bt_ctf_trace_get_name(trace), name);
379 BT_PUT(attribute);
380 ret = -1;
381 goto end;
382 }
383
384 bt_value_freeze(value);
385 }
386
387 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
388 value);
389 if (ret) {
390 BT_LOGE("Cannot set environment field's value: "
391 "trace-addr=%p, trace-name=\"%s\", "
392 "env-name=\"%s\"",
393 trace, bt_ctf_trace_get_name(trace), name);
394 } else {
395 BT_LOGV("Set environment field's value: "
396 "trace-addr=%p, trace-name=\"%s\", "
397 "env-name=\"%s\", value-addr=%p",
398 trace, bt_ctf_trace_get_name(trace), name, value);
399 }
400
401 end:
402 return ret;
403 }
404
405 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
406 const char *name, const char *value)
407 {
408 int ret = 0;
409 struct bt_value *env_value_string_obj = NULL;
410
411 if (!value) {
412 BT_LOGW_STR("Invalid parameter: value is NULL.");
413 ret = -1;
414 goto end;
415 }
416
417 env_value_string_obj = bt_value_string_create_init(value);
418 if (!env_value_string_obj) {
419 BT_LOGE_STR("Cannot create string value object.");
420 ret = -1;
421 goto end;
422 }
423
424 /* bt_ctf_trace_set_environment_field() logs errors */
425 ret = bt_ctf_trace_set_environment_field(trace, name,
426 env_value_string_obj);
427
428 end:
429 bt_put(env_value_string_obj);
430 return ret;
431 }
432
433 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
434 const char *name, int64_t value)
435 {
436 int ret = 0;
437 struct bt_value *env_value_integer_obj = NULL;
438
439 env_value_integer_obj = bt_value_integer_create_init(value);
440 if (!env_value_integer_obj) {
441 BT_LOGE_STR("Cannot create integer value object.");
442 ret = -1;
443 goto end;
444 }
445
446 /* bt_ctf_trace_set_environment_field() logs errors */
447 ret = bt_ctf_trace_set_environment_field(trace, name,
448 env_value_integer_obj);
449
450 end:
451 bt_put(env_value_integer_obj);
452 return ret;
453 }
454
455 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
456 {
457 int64_t ret = 0;
458
459 if (!trace) {
460 BT_LOGW_STR("Invalid parameter: trace is NULL.");
461 ret = (int64_t) -1;
462 goto end;
463 }
464
465 ret = bt_ctf_attributes_get_count(trace->environment);
466 assert(ret >= 0);
467
468 end:
469 return ret;
470 }
471
472 const char *
473 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace,
474 uint64_t index)
475 {
476 const char *ret = NULL;
477
478 if (!trace) {
479 BT_LOGW_STR("Invalid parameter: trace is NULL.");
480 goto end;
481 }
482
483 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
484
485 end:
486 return ret;
487 }
488
489 struct bt_value *bt_ctf_trace_get_environment_field_value_by_index(
490 struct bt_ctf_trace *trace, uint64_t index)
491 {
492 struct bt_value *ret = NULL;
493
494 if (!trace) {
495 BT_LOGW_STR("Invalid parameter: trace is NULL.");
496 goto end;
497 }
498
499 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
500
501 end:
502 return ret;
503 }
504
505 struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
506 struct bt_ctf_trace *trace, const char *name)
507 {
508 struct bt_value *ret = NULL;
509
510 if (!trace) {
511 BT_LOGW_STR("Invalid parameter: trace is NULL.");
512 goto end;
513 }
514
515 if (!name) {
516 BT_LOGW_STR("Invalid parameter: name is NULL.");
517 goto end;
518 }
519
520 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
521 name);
522
523 end:
524 return ret;
525 }
526
527 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
528 struct bt_ctf_clock_class *clock_class)
529 {
530 int ret = 0;
531
532 if (!trace) {
533 BT_LOGW_STR("Invalid parameter: trace is NULL.");
534 ret = -1;
535 goto end;
536 }
537
538 if (trace->is_static) {
539 BT_LOGW("Invalid parameter: trace is static: "
540 "addr=%p, name=\"%s\"",
541 trace, bt_ctf_trace_get_name(trace));
542 ret = -1;
543 goto end;
544 }
545
546 if (!bt_ctf_clock_class_is_valid(clock_class)) {
547 BT_LOGW("Invalid parameter: clock class is invalid: "
548 "trace-addr=%p, trace-name=\"%s\", "
549 "clock-class-addr=%p, clock-class-name=\"%s\"",
550 trace, bt_ctf_trace_get_name(trace),
551 clock_class, bt_ctf_clock_class_get_name(clock_class));
552 ret = -1;
553 goto end;
554 }
555
556 /* Check for duplicate clock classes */
557 if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
558 BT_LOGW("Invalid parameter: clock class already exists in trace: "
559 "trace-addr=%p, trace-name=\"%s\", "
560 "clock-class-addr=%p, clock-class-name=\"%s\"",
561 trace, bt_ctf_trace_get_name(trace),
562 clock_class, bt_ctf_clock_class_get_name(clock_class));
563 ret = -1;
564 goto end;
565 }
566
567 bt_get(clock_class);
568 g_ptr_array_add(trace->clocks, clock_class);
569
570 if (trace->frozen) {
571 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
572 bt_ctf_clock_class_freeze(clock_class);
573 }
574
575 BT_LOGV("Added clock class to trace: "
576 "trace-addr=%p, trace-name=\"%s\", "
577 "clock-class-addr=%p, clock-class-name=\"%s\"",
578 trace, bt_ctf_trace_get_name(trace),
579 clock_class, bt_ctf_clock_class_get_name(clock_class));
580
581 end:
582 return ret;
583 }
584
585 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
586 {
587 int64_t ret = (int64_t) -1;
588
589 if (!trace) {
590 BT_LOGW_STR("Invalid parameter: trace is NULL.");
591 goto end;
592 }
593
594 ret = trace->clocks->len;
595 end:
596 return ret;
597 }
598
599 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
600 struct bt_ctf_trace *trace, uint64_t index)
601 {
602 struct bt_ctf_clock_class *clock_class = NULL;
603
604 if (!trace) {
605 BT_LOGW_STR("Invalid parameter: trace is NULL.");
606 goto end;
607 }
608
609 if (index >= trace->clocks->len) {
610 BT_LOGW("Invalid parameter: index is out of bounds: "
611 "addr=%p, name=\"%s\", "
612 "index=%" PRIu64 ", count=%u",
613 trace, bt_ctf_trace_get_name(trace),
614 index, trace->clocks->len);
615 goto end;
616 }
617
618 clock_class = g_ptr_array_index(trace->clocks, index);
619 bt_get(clock_class);
620 end:
621 return clock_class;
622 }
623
624 static
625 bool packet_header_field_type_is_valid(struct bt_ctf_trace *trace,
626 struct bt_ctf_field_type *packet_header_type)
627 {
628 int ret;
629 bool is_valid = true;
630 struct bt_ctf_field_type *field_type = NULL;
631
632 if (!packet_header_type) {
633 /*
634 * No packet header field type: trace must have only
635 * one stream. At this point the stream class being
636 * added is not part of the trace yet, so we validate
637 * that the trace contains no stream classes yet.
638 */
639 if (trace->stream_classes->len >= 1) {
640 BT_LOGW_STR("Invalid packet header field type: "
641 "packet header field type does not exist but there's more than one stream class in the trace.");
642 goto invalid;
643 }
644
645 /* No packet header field type: valid at this point */
646 goto end;
647 }
648
649 /* Packet header field type, if it exists, must be a structure */
650 if (!bt_ctf_field_type_is_structure(packet_header_type)) {
651 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
652 "ft-addr=%p, ft-id=%s",
653 packet_header_type,
654 bt_ctf_field_type_id_string(packet_header_type->id));
655 goto invalid;
656 }
657
658 /*
659 * If there's a `magic` field, it must be a 32-bit unsigned
660 * integer field type. Also it must be the first field of the
661 * packet header field type.
662 */
663 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
664 packet_header_type, "magic");
665 if (field_type) {
666 const char *field_name;
667
668 if (!bt_ctf_field_type_is_integer(field_type)) {
669 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
670 "magic-ft-addr=%p, magic-ft-id=%s",
671 field_type,
672 bt_ctf_field_type_id_string(field_type->id));
673 goto invalid;
674 }
675
676 if (bt_ctf_field_type_integer_is_signed(field_type)) {
677 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
678 "magic-ft-addr=%p", field_type);
679 goto invalid;
680 }
681
682 if (bt_ctf_field_type_integer_get_size(field_type) != 32) {
683 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
684 "magic-ft-addr=%p, magic-ft-size=%u",
685 field_type,
686 bt_ctf_field_type_integer_get_size(field_type));
687 goto invalid;
688 }
689
690 ret = bt_ctf_field_type_structure_get_field_by_index(
691 packet_header_type, &field_name, NULL, 0);
692 assert(ret == 0);
693
694 if (strcmp(field_name, "magic") != 0) {
695 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
696 "magic-ft-addr=%p, first-field-name=\"%s\"",
697 field_type, field_name);
698 goto invalid;
699 }
700
701 BT_PUT(field_type);
702 }
703
704 /*
705 * If there's a `uuid` field, it must be an array field type of
706 * length 16 with an 8-bit unsigned integer element field type.
707 */
708 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
709 packet_header_type, "uuid");
710 if (field_type) {
711 struct bt_ctf_field_type *elem_ft;
712
713 if (!bt_ctf_field_type_is_array(field_type)) {
714 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
715 "uuid-ft-addr=%p, uuid-ft-id=%s",
716 field_type,
717 bt_ctf_field_type_id_string(field_type->id));
718 goto invalid;
719 }
720
721 if (bt_ctf_field_type_array_get_length(field_type) != 16) {
722 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
723 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
724 field_type,
725 bt_ctf_field_type_array_get_length(field_type));
726 goto invalid;
727 }
728
729 elem_ft = bt_ctf_field_type_array_get_element_type(field_type);
730 assert(elem_ft);
731
732 if (!bt_ctf_field_type_is_integer(elem_ft)) {
733 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
734 "elem-ft-addr=%p, elem-ft-id=%s",
735 elem_ft,
736 bt_ctf_field_type_id_string(elem_ft->id));
737 bt_put(elem_ft);
738 goto invalid;
739 }
740
741 if (bt_ctf_field_type_integer_is_signed(elem_ft)) {
742 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
743 "elem-ft-addr=%p", elem_ft);
744 bt_put(elem_ft);
745 goto invalid;
746 }
747
748 if (bt_ctf_field_type_integer_get_size(elem_ft) != 8) {
749 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
750 "elem-ft-addr=%p, elem-ft-size=%u",
751 elem_ft,
752 bt_ctf_field_type_integer_get_size(elem_ft));
753 bt_put(elem_ft);
754 goto invalid;
755 }
756
757 bt_put(elem_ft);
758 BT_PUT(field_type);
759 }
760
761 /*
762 * The `stream_id` field must exist if there's more than one
763 * stream classes in the trace.
764 */
765 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
766 packet_header_type, "stream_id");
767
768 if (!field_type && trace->stream_classes->len >= 1) {
769 BT_LOGW_STR("Invalid packet header field type: "
770 "`stream_id` field does not exist but there's more than one stream class in the trace.");
771 goto invalid;
772 }
773
774 /*
775 * If there's a `stream_id` field, it must be an unsigned
776 * integer field type.
777 */
778 if (field_type) {
779 if (!bt_ctf_field_type_is_integer(field_type)) {
780 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
781 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
782 field_type,
783 bt_ctf_field_type_id_string(field_type->id));
784 goto invalid;
785 }
786
787 if (bt_ctf_field_type_integer_is_signed(field_type)) {
788 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
789 "stream-id-ft-addr=%p", field_type);
790 goto invalid;
791 }
792
793 BT_PUT(field_type);
794 }
795
796 /*
797 * If there's a `packet_seq_num` field, it must be an unsigned
798 * integer field type.
799 */
800 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
801 packet_header_type, "packet_seq_num");
802 if (field_type) {
803 if (!bt_ctf_field_type_is_integer(field_type)) {
804 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
805 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
806 field_type,
807 bt_ctf_field_type_id_string(field_type->id));
808 goto invalid;
809 }
810
811 if (bt_ctf_field_type_integer_is_signed(field_type)) {
812 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
813 "packet-seq-num-ft-addr=%p", field_type);
814 goto invalid;
815 }
816
817 BT_PUT(field_type);
818 }
819
820 goto end;
821
822 invalid:
823 is_valid = false;
824
825 end:
826 bt_put(field_type);
827 return is_valid;
828 }
829
830 static
831 bool packet_context_field_type_is_valid(struct bt_ctf_trace *trace,
832 struct bt_ctf_stream_class *stream_class,
833 struct bt_ctf_field_type *packet_context_type)
834 {
835 bool is_valid = true;
836 struct bt_ctf_field_type *field_type = NULL;
837
838 if (!packet_context_type) {
839 /* No packet context field type: valid at this point */
840 goto end;
841 }
842
843 /* Packet context field type, if it exists, must be a structure */
844 if (!bt_ctf_field_type_is_structure(packet_context_type)) {
845 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
846 "ft-addr=%p, ft-id=%s",
847 packet_context_type,
848 bt_ctf_field_type_id_string(packet_context_type->id));
849 goto invalid;
850 }
851
852 /*
853 * If there's a `packet_size` field, it must be an unsigned
854 * integer field type.
855 */
856 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
857 packet_context_type, "packet_size");
858 if (field_type) {
859 if (!bt_ctf_field_type_is_integer(field_type)) {
860 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
861 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
862 field_type,
863 bt_ctf_field_type_id_string(field_type->id));
864 goto invalid;
865 }
866
867 if (bt_ctf_field_type_integer_is_signed(field_type)) {
868 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
869 "packet-size-ft-addr=%p", field_type);
870 goto invalid;
871 }
872
873 BT_PUT(field_type);
874 }
875
876 /*
877 * If there's a `content_size` field, it must be an unsigned
878 * integer field type.
879 */
880 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
881 packet_context_type, "content_size");
882 if (field_type) {
883 if (!bt_ctf_field_type_is_integer(field_type)) {
884 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
885 "content-size-ft-addr=%p, content-size-ft-id=%s",
886 field_type,
887 bt_ctf_field_type_id_string(field_type->id));
888 goto invalid;
889 }
890
891 if (bt_ctf_field_type_integer_is_signed(field_type)) {
892 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
893 "content-size-ft-addr=%p", field_type);
894 goto invalid;
895 }
896
897 BT_PUT(field_type);
898 }
899
900 /*
901 * If there's a `events_discarded` field, it must be an unsigned
902 * integer field type.
903 */
904 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
905 packet_context_type, "events_discarded");
906 if (field_type) {
907 if (!bt_ctf_field_type_is_integer(field_type)) {
908 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
909 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
910 field_type,
911 bt_ctf_field_type_id_string(field_type->id));
912 goto invalid;
913 }
914
915 if (bt_ctf_field_type_integer_is_signed(field_type)) {
916 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
917 "events-discarded-ft-addr=%p", field_type);
918 goto invalid;
919 }
920
921 BT_PUT(field_type);
922 }
923
924 /*
925 * If there's a `timestamp_begin` field, it must be an unsigned
926 * integer field type. Also, if the trace is not a CTF writer's
927 * trace, then we cannot automatically set the mapped clock
928 * class of this field, so it must have a mapped clock class.
929 */
930 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
931 packet_context_type, "timestamp_begin");
932 if (field_type) {
933 if (!bt_ctf_field_type_is_integer(field_type)) {
934 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
935 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
936 field_type,
937 bt_ctf_field_type_id_string(field_type->id));
938 goto invalid;
939 }
940
941 if (bt_ctf_field_type_integer_is_signed(field_type)) {
942 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
943 "timestamp-begin-ft-addr=%p", field_type);
944 goto invalid;
945 }
946
947 if (!trace->is_created_by_writer) {
948 struct bt_ctf_clock_class *clock_class =
949 bt_ctf_field_type_integer_get_mapped_clock_class(
950 field_type);
951
952 bt_put(clock_class);
953 if (!clock_class) {
954 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
955 "timestamp-begin-ft-addr=%p", field_type);
956 goto invalid;
957 }
958 }
959
960 BT_PUT(field_type);
961 }
962
963 /*
964 * If there's a `timestamp_end` field, it must be an unsigned
965 * integer field type. Also, if the trace is not a CTF writer's
966 * trace, then we cannot automatically set the mapped clock
967 * class of this field, so it must have a mapped clock class.
968 */
969 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
970 packet_context_type, "timestamp_end");
971 if (field_type) {
972 if (!bt_ctf_field_type_is_integer(field_type)) {
973 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
974 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
975 field_type,
976 bt_ctf_field_type_id_string(field_type->id));
977 goto invalid;
978 }
979
980 if (bt_ctf_field_type_integer_is_signed(field_type)) {
981 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
982 "timestamp-end-ft-addr=%p", field_type);
983 goto invalid;
984 }
985
986 if (!trace->is_created_by_writer) {
987 struct bt_ctf_clock_class *clock_class =
988 bt_ctf_field_type_integer_get_mapped_clock_class(
989 field_type);
990
991 bt_put(clock_class);
992 if (!clock_class) {
993 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
994 "timestamp-end-ft-addr=%p", field_type);
995 goto invalid;
996 }
997 }
998
999 BT_PUT(field_type);
1000 }
1001
1002 goto end;
1003
1004 invalid:
1005 is_valid = false;
1006
1007 end:
1008 bt_put(field_type);
1009 return is_valid;
1010 }
1011
1012 static
1013 bool event_header_field_type_is_valid(struct bt_ctf_trace *trace,
1014 struct bt_ctf_stream_class *stream_class,
1015 struct bt_ctf_field_type *event_header_type)
1016 {
1017 bool is_valid = true;
1018 struct bt_ctf_field_type *field_type = NULL;
1019
1020 /*
1021 * We do not validate that the `timestamp` field exists here
1022 * because CTF does not require this exact name to be mapped to
1023 * a clock class.
1024 */
1025
1026 if (!event_header_type) {
1027 /*
1028 * No event header field type: stream class must have
1029 * only one event class.
1030 */
1031 if (bt_ctf_stream_class_get_event_class_count(stream_class) > 1) {
1032 BT_LOGW_STR("Invalid event header field type: "
1033 "event header field type does not exist but there's more than one event class in the stream class.");
1034 goto invalid;
1035 }
1036
1037 /* No event header field type: valid at this point */
1038 goto end;
1039 }
1040
1041 /* Event header field type, if it exists, must be a structure */
1042 if (!bt_ctf_field_type_is_structure(event_header_type)) {
1043 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1044 "ft-addr=%p, ft-id=%s",
1045 event_header_type,
1046 bt_ctf_field_type_id_string(event_header_type->id));
1047 goto invalid;
1048 }
1049
1050 /*
1051 * If there's an `id` field, it must be an unsigned integer
1052 * field type or an enumeration field type with an unsigned
1053 * integer container field type.
1054 */
1055 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1056 event_header_type, "id");
1057 if (field_type) {
1058 struct bt_ctf_field_type *int_ft;
1059
1060 if (bt_ctf_field_type_is_integer(field_type)) {
1061 int_ft = bt_get(field_type);
1062 } else if (bt_ctf_field_type_is_enumeration(field_type)) {
1063 int_ft = bt_ctf_field_type_enumeration_get_container_type(
1064 field_type);
1065 } else {
1066 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1067 "id-ft-addr=%p, id-ft-id=%s",
1068 field_type,
1069 bt_ctf_field_type_id_string(field_type->id));
1070 goto invalid;
1071 }
1072
1073 assert(int_ft);
1074 if (bt_ctf_field_type_integer_is_signed(int_ft)) {
1075 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1076 "id-ft-addr=%p", int_ft);
1077 goto invalid;
1078 }
1079
1080 bt_put(int_ft);
1081 BT_PUT(field_type);
1082 }
1083
1084 goto end;
1085
1086 invalid:
1087 is_valid = false;
1088
1089 end:
1090 bt_put(field_type);
1091 return is_valid;
1092 }
1093
1094 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
1095 struct bt_ctf_stream_class *stream_class)
1096 {
1097 int ret;
1098 int64_t i;
1099 int64_t stream_id;
1100 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
1101 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
1102 const enum bt_ctf_validation_flag trace_sc_validation_flags =
1103 BT_CTF_VALIDATION_FLAG_TRACE |
1104 BT_CTF_VALIDATION_FLAG_STREAM;
1105 const enum bt_ctf_validation_flag ec_validation_flags =
1106 BT_CTF_VALIDATION_FLAG_EVENT;
1107 struct bt_ctf_field_type *packet_header_type = NULL;
1108 struct bt_ctf_field_type *packet_context_type = NULL;
1109 struct bt_ctf_field_type *event_header_type = NULL;
1110 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
1111 int64_t event_class_count;
1112 struct bt_ctf_trace *current_parent_trace = NULL;
1113
1114 if (!trace) {
1115 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1116 ret = -1;
1117 goto end;
1118 }
1119
1120 if (!stream_class) {
1121 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1122 ret = -1;
1123 goto end;
1124 }
1125
1126 if (trace->is_static) {
1127 BT_LOGW_STR("Invalid parameter: trace is static.");
1128 ret = -1;
1129 goto end;
1130 }
1131
1132 BT_LOGD("Adding stream class to trace: "
1133 "trace-addr=%p, trace-name=\"%s\", "
1134 "stream-class-addr=%p, stream-class-name=\"%s\", "
1135 "stream-class-id=%" PRId64,
1136 trace, bt_ctf_trace_get_name(trace),
1137 stream_class, bt_ctf_stream_class_get_name(stream_class),
1138 bt_ctf_stream_class_get_id(stream_class));
1139
1140 current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
1141 if (current_parent_trace) {
1142 /* Stream class is already associated to a trace, abort. */
1143 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1144 "stream-class-trace-addr=%p, "
1145 "stream-class-trace-name=\"%s\"",
1146 current_parent_trace,
1147 bt_ctf_trace_get_name(current_parent_trace));
1148 ret = -1;
1149 goto end;
1150 }
1151
1152 event_class_count =
1153 bt_ctf_stream_class_get_event_class_count(stream_class);
1154 assert(event_class_count >= 0);
1155
1156 if (stream_class->clock) {
1157 struct bt_ctf_clock_class *stream_clock_class =
1158 stream_class->clock->clock_class;
1159
1160 if (trace->is_created_by_writer) {
1161 /*
1162 * Make sure this clock was also added to the
1163 * trace (potentially through its CTF writer
1164 * owner).
1165 */
1166 size_t i;
1167
1168 for (i = 0; i < trace->clocks->len; i++) {
1169 if (trace->clocks->pdata[i] ==
1170 stream_clock_class) {
1171 /* Found! */
1172 break;
1173 }
1174 }
1175
1176 if (i == trace->clocks->len) {
1177 /* Not found */
1178 BT_LOGW("Stream class's clock's class is not part of the trace: "
1179 "clock-class-addr=%p, clock-class-name=\"%s\"",
1180 stream_clock_class,
1181 bt_ctf_clock_class_get_name(stream_clock_class));
1182 ret = -1;
1183 goto end;
1184 }
1185 } else {
1186 /*
1187 * This trace was NOT created by a CTF writer,
1188 * thus do not allow the stream class to add to
1189 * have a clock at all. Those are two
1190 * independent APIs (non-writer and writer
1191 * APIs), and isolating them simplifies things.
1192 */
1193 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1194 "clock-class-addr=%p, clock-class-name=\"%s\"",
1195 stream_clock_class,
1196 bt_ctf_clock_class_get_name(stream_clock_class));
1197 ret = -1;
1198 goto end;
1199 }
1200 }
1201
1202 /*
1203 * We're about to freeze both the trace and the stream class.
1204 * Also, each event class contained in this stream class are
1205 * already frozen.
1206 *
1207 * This trace, this stream class, and all its event classes
1208 * should be valid at this point.
1209 *
1210 * Validate trace and stream class first, then each event
1211 * class of this stream class can be validated individually.
1212 */
1213 packet_header_type =
1214 bt_ctf_trace_get_packet_header_type(trace);
1215 packet_context_type =
1216 bt_ctf_stream_class_get_packet_context_type(stream_class);
1217 event_header_type =
1218 bt_ctf_stream_class_get_event_header_type(stream_class);
1219 stream_event_ctx_type =
1220 bt_ctf_stream_class_get_event_context_type(stream_class);
1221
1222 BT_LOGD("Validating trace and stream class field types.");
1223 ret = bt_ctf_validate_class_types(trace->environment,
1224 packet_header_type, packet_context_type, event_header_type,
1225 stream_event_ctx_type, NULL, NULL, trace->valid,
1226 stream_class->valid, 1, &trace_sc_validation_output,
1227 trace_sc_validation_flags);
1228 BT_PUT(packet_header_type);
1229 BT_PUT(packet_context_type);
1230 BT_PUT(event_header_type);
1231 BT_PUT(stream_event_ctx_type);
1232
1233 if (ret) {
1234 /*
1235 * This means something went wrong during the validation
1236 * process, not that the objects are invalid.
1237 */
1238 BT_LOGE("Failed to validate trace and stream class field types: "
1239 "ret=%d", ret);
1240 goto end;
1241 }
1242
1243 if ((trace_sc_validation_output.valid_flags &
1244 trace_sc_validation_flags) !=
1245 trace_sc_validation_flags) {
1246 /* Invalid trace/stream class */
1247 BT_LOGW("Invalid trace or stream class field types: "
1248 "valid-flags=0x%x",
1249 trace_sc_validation_output.valid_flags);
1250 ret = -1;
1251 goto end;
1252 }
1253
1254 if (event_class_count > 0) {
1255 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
1256 event_class_count);
1257 if (!ec_validation_outputs) {
1258 BT_LOGE_STR("Failed to allocate one validation output structure.");
1259 ret = -1;
1260 goto end;
1261 }
1262 }
1263
1264 /* Validate each event class individually */
1265 for (i = 0; i < event_class_count; i++) {
1266 struct bt_ctf_event_class *event_class =
1267 bt_ctf_stream_class_get_event_class_by_index(
1268 stream_class, i);
1269 struct bt_ctf_field_type *event_context_type = NULL;
1270 struct bt_ctf_field_type *event_payload_type = NULL;
1271
1272 event_context_type =
1273 bt_ctf_event_class_get_context_type(event_class);
1274 event_payload_type =
1275 bt_ctf_event_class_get_payload_type(event_class);
1276
1277 /*
1278 * It is important to use the field types returned by
1279 * the previous trace and stream class validation here
1280 * because copies could have been made.
1281 */
1282 BT_LOGD("Validating event class's field types: "
1283 "addr=%p, name=\"%s\", id=%" PRId64,
1284 event_class, bt_ctf_event_class_get_name(event_class),
1285 bt_ctf_event_class_get_id(event_class));
1286 ret = bt_ctf_validate_class_types(trace->environment,
1287 trace_sc_validation_output.packet_header_type,
1288 trace_sc_validation_output.packet_context_type,
1289 trace_sc_validation_output.event_header_type,
1290 trace_sc_validation_output.stream_event_ctx_type,
1291 event_context_type, event_payload_type,
1292 1, 1, event_class->valid, &ec_validation_outputs[i],
1293 ec_validation_flags);
1294 BT_PUT(event_context_type);
1295 BT_PUT(event_payload_type);
1296 BT_PUT(event_class);
1297
1298 if (ret) {
1299 BT_LOGE("Failed to validate event class field types: "
1300 "ret=%d", ret);
1301 goto end;
1302 }
1303
1304 if ((ec_validation_outputs[i].valid_flags &
1305 ec_validation_flags) != ec_validation_flags) {
1306 /* Invalid event class */
1307 BT_LOGW("Invalid event class field types: "
1308 "valid-flags=0x%x",
1309 ec_validation_outputs[i].valid_flags);
1310 ret = -1;
1311 goto end;
1312 }
1313 }
1314
1315 stream_id = bt_ctf_stream_class_get_id(stream_class);
1316 if (stream_id < 0) {
1317 stream_id = trace->next_stream_id++;
1318 if (stream_id < 0) {
1319 BT_LOGE_STR("No more stream class IDs available.");
1320 ret = -1;
1321 goto end;
1322 }
1323
1324 /* Try to assign a new stream id */
1325 for (i = 0; i < trace->stream_classes->len; i++) {
1326 if (stream_id == bt_ctf_stream_class_get_id(
1327 trace->stream_classes->pdata[i])) {
1328 /* Duplicate stream id found */
1329 BT_LOGW("Duplicate stream class ID: "
1330 "id=%" PRId64, (int64_t) stream_id);
1331 ret = -1;
1332 goto end;
1333 }
1334 }
1335
1336 if (bt_ctf_stream_class_set_id_no_check(stream_class,
1337 stream_id)) {
1338 /* TODO Should retry with a different stream id */
1339 BT_LOGE("Cannot set stream class's ID: "
1340 "id=%" PRId64, (int64_t) stream_id);
1341 ret = -1;
1342 goto end;
1343 }
1344 }
1345
1346 /*
1347 * At this point all the field types in the validation output
1348 * are valid. Validate the semantics of some scopes according to
1349 * the CTF specification.
1350 */
1351 if (!packet_header_field_type_is_valid(trace,
1352 trace_sc_validation_output.packet_header_type)) {
1353 BT_LOGW_STR("Invalid trace's packet header field type.");
1354 ret = -1;
1355 goto end;
1356 }
1357
1358 if (!packet_context_field_type_is_valid(trace,
1359 stream_class,
1360 trace_sc_validation_output.packet_context_type)) {
1361 BT_LOGW_STR("Invalid stream class's packet context field type.");
1362 ret = -1;
1363 goto end;
1364 }
1365
1366 if (!event_header_field_type_is_valid(trace,
1367 stream_class,
1368 trace_sc_validation_output.event_header_type)) {
1369 BT_LOGW_STR("Invalid steam class's event header field type.");
1370 ret = -1;
1371 goto end;
1372 }
1373
1374 /*
1375 * Now is the time to automatically map specific field types of
1376 * the stream class's packet context and event header field
1377 * types to the stream class's clock's class if they are not
1378 * mapped to a clock class yet. We do it here because we know
1379 * that after this point, everything is frozen so it won't be
1380 * possible for the user to modify the stream class's clock, or
1381 * to map those field types to other clock classes.
1382 */
1383 if (trace->is_created_by_writer) {
1384 if (bt_ctf_stream_class_map_clock_class(stream_class,
1385 trace_sc_validation_output.packet_context_type,
1386 trace_sc_validation_output.event_header_type)) {
1387 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1388 ret = -1;
1389 goto end;
1390 }
1391 }
1392
1393 bt_object_set_parent(stream_class, trace);
1394 g_ptr_array_add(trace->stream_classes, stream_class);
1395
1396 /*
1397 * At this point we know that the function will be successful.
1398 * Therefore we can replace the trace and stream class field
1399 * types with what's in their validation output structure and
1400 * mark them as valid. We can also replace the field types of
1401 * all the event classes of the stream class and mark them as
1402 * valid.
1403 */
1404 bt_ctf_validation_replace_types(trace, stream_class, NULL,
1405 &trace_sc_validation_output, trace_sc_validation_flags);
1406 trace->valid = 1;
1407 stream_class->valid = 1;
1408
1409 /*
1410 * Put what was not moved in bt_ctf_validation_replace_types().
1411 */
1412 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
1413
1414 for (i = 0; i < event_class_count; i++) {
1415 struct bt_ctf_event_class *event_class =
1416 bt_ctf_stream_class_get_event_class_by_index(
1417 stream_class, i);
1418
1419 bt_ctf_validation_replace_types(NULL, NULL, event_class,
1420 &ec_validation_outputs[i], ec_validation_flags);
1421 event_class->valid = 1;
1422 BT_PUT(event_class);
1423
1424 /*
1425 * Put what was not moved in
1426 * bt_ctf_validation_replace_types().
1427 */
1428 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
1429 }
1430
1431 /*
1432 * Freeze the trace and the stream class.
1433 */
1434 bt_ctf_stream_class_freeze(stream_class);
1435 bt_ctf_trace_freeze(trace);
1436
1437 /* Notifiy listeners of the trace's schema modification. */
1438 bt_ctf_stream_class_visit(stream_class,
1439 bt_ctf_trace_object_modification, trace);
1440 BT_LOGD("Added stream class to trace: "
1441 "trace-addr=%p, trace-name=\"%s\", "
1442 "stream-class-addr=%p, stream-class-name=\"%s\", "
1443 "stream-class-id=%" PRId64,
1444 trace, bt_ctf_trace_get_name(trace),
1445 stream_class, bt_ctf_stream_class_get_name(stream_class),
1446 bt_ctf_stream_class_get_id(stream_class));
1447
1448 end:
1449 if (ret) {
1450 bt_object_set_parent(stream_class, NULL);
1451
1452 if (ec_validation_outputs) {
1453 for (i = 0; i < event_class_count; i++) {
1454 bt_ctf_validation_output_put_types(
1455 &ec_validation_outputs[i]);
1456 }
1457 }
1458 }
1459
1460 g_free(ec_validation_outputs);
1461 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
1462 bt_put(current_parent_trace);
1463 assert(!packet_header_type);
1464 assert(!packet_context_type);
1465 assert(!event_header_type);
1466 assert(!stream_event_ctx_type);
1467 return ret;
1468 }
1469
1470 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
1471 {
1472 int64_t ret;
1473
1474 if (!trace) {
1475 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1476 ret = (int64_t) -1;
1477 goto end;
1478 }
1479
1480 ret = (int64_t) trace->streams->len;
1481
1482 end:
1483 return ret;
1484 }
1485
1486 struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index(
1487 struct bt_ctf_trace *trace,
1488 uint64_t index)
1489 {
1490 struct bt_ctf_stream *stream = NULL;
1491
1492 if (!trace) {
1493 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1494 goto end;
1495 }
1496
1497 if (index >= trace->streams->len) {
1498 BT_LOGW("Invalid parameter: index is out of bounds: "
1499 "addr=%p, name=\"%s\", "
1500 "index=%" PRIu64 ", count=%u",
1501 trace, bt_ctf_trace_get_name(trace),
1502 index, trace->streams->len);
1503 goto end;
1504 }
1505
1506 stream = bt_get(g_ptr_array_index(trace->streams, index));
1507
1508 end:
1509 return stream;
1510 }
1511
1512 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
1513 {
1514 int64_t ret;
1515
1516 if (!trace) {
1517 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1518 ret = (int64_t) -1;
1519 goto end;
1520 }
1521
1522 ret = (int64_t) trace->stream_classes->len;
1523 end:
1524 return ret;
1525 }
1526
1527 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index(
1528 struct bt_ctf_trace *trace, uint64_t index)
1529 {
1530 struct bt_ctf_stream_class *stream_class = NULL;
1531
1532 if (!trace) {
1533 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1534 goto end;
1535 }
1536
1537 if (index >= trace->stream_classes->len) {
1538 BT_LOGW("Invalid parameter: index is out of bounds: "
1539 "addr=%p, name=\"%s\", "
1540 "index=%" PRIu64 ", count=%u",
1541 trace, bt_ctf_trace_get_name(trace),
1542 index, trace->stream_classes->len);
1543 goto end;
1544 }
1545
1546 stream_class = g_ptr_array_index(trace->stream_classes, index);
1547 bt_get(stream_class);
1548 end:
1549 return stream_class;
1550 }
1551
1552 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
1553 struct bt_ctf_trace *trace, uint64_t id_param)
1554 {
1555 int i;
1556 struct bt_ctf_stream_class *stream_class = NULL;
1557 int64_t id = (int64_t) id_param;
1558
1559 if (!trace) {
1560 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1561 goto end;
1562 }
1563
1564 if (id < 0) {
1565 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1566 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64,
1567 trace, bt_ctf_trace_get_name(trace), id_param);
1568 goto end;
1569 }
1570
1571 for (i = 0; i < trace->stream_classes->len; i++) {
1572 struct bt_ctf_stream_class *stream_class_candidate;
1573
1574 stream_class_candidate =
1575 g_ptr_array_index(trace->stream_classes, i);
1576
1577 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
1578 (int64_t) id) {
1579 stream_class = stream_class_candidate;
1580 bt_get(stream_class);
1581 goto end;
1582 }
1583 }
1584
1585 end:
1586 return stream_class;
1587 }
1588
1589 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
1590 struct bt_ctf_trace *trace, const char *name)
1591 {
1592 size_t i;
1593 struct bt_ctf_clock_class *clock_class = NULL;
1594
1595 if (!trace) {
1596 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1597 goto end;
1598 }
1599
1600 if (!name) {
1601 BT_LOGW_STR("Invalid parameter: name is NULL.");
1602 goto end;
1603 }
1604
1605 for (i = 0; i < trace->clocks->len; i++) {
1606 struct bt_ctf_clock_class *cur_clk =
1607 g_ptr_array_index(trace->clocks, i);
1608 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
1609
1610 if (!cur_clk_name) {
1611 goto end;
1612 }
1613
1614 if (!strcmp(cur_clk_name, name)) {
1615 clock_class = cur_clk;
1616 bt_get(clock_class);
1617 goto end;
1618 }
1619 }
1620
1621 end:
1622 return clock_class;
1623 }
1624
1625 BT_HIDDEN
1626 bt_bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
1627 struct bt_ctf_clock_class *clock_class)
1628 {
1629 struct search_query query = { .value = clock_class, .found = 0 };
1630
1631 assert(trace);
1632 assert(clock_class);
1633
1634 g_ptr_array_foreach(trace->clocks, value_exists, &query);
1635 return query.found;
1636 }
1637
1638 BT_HIDDEN
1639 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
1640 {
1641 const char *string;
1642
1643 switch (byte_order) {
1644 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
1645 string = "le";
1646 break;
1647 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
1648 string = "be";
1649 break;
1650 case BT_CTF_BYTE_ORDER_NATIVE:
1651 string = "native";
1652 break;
1653 default:
1654 abort();
1655 }
1656
1657 return string;
1658 }
1659
1660 static
1661 int append_trace_metadata(struct bt_ctf_trace *trace,
1662 struct metadata_context *context)
1663 {
1664 unsigned char *uuid = trace->uuid;
1665 int ret = 0;
1666
1667 if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE ||
1668 trace->native_byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
1669 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1670 "set it with bt_ctf_trace_set_native_byte_order(): "
1671 "addr=%p, name=\"%s\"",
1672 trace, bt_ctf_trace_get_name(trace));
1673 ret = -1;
1674 goto end;
1675 }
1676
1677 g_string_append(context->string, "trace {\n");
1678 g_string_append(context->string, "\tmajor = 1;\n");
1679 g_string_append(context->string, "\tminor = 8;\n");
1680 assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
1681 trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
1682 trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
1683
1684 if (trace->uuid_set) {
1685 g_string_append_printf(context->string,
1686 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1687 uuid[0], uuid[1], uuid[2], uuid[3],
1688 uuid[4], uuid[5], uuid[6], uuid[7],
1689 uuid[8], uuid[9], uuid[10], uuid[11],
1690 uuid[12], uuid[13], uuid[14], uuid[15]);
1691 }
1692
1693 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
1694 get_byte_order_string(trace->native_byte_order));
1695
1696 if (trace->packet_header_type) {
1697 g_string_append(context->string, "\tpacket.header := ");
1698 context->current_indentation_level++;
1699 g_string_assign(context->field_name, "");
1700 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1701 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
1702 context);
1703 if (ret) {
1704 goto end;
1705 }
1706 context->current_indentation_level--;
1707 }
1708
1709 g_string_append(context->string, ";\n};\n\n");
1710 end:
1711 return ret;
1712 }
1713
1714 static
1715 void append_env_metadata(struct bt_ctf_trace *trace,
1716 struct metadata_context *context)
1717 {
1718 int64_t i;
1719 int64_t env_size;
1720
1721 env_size = bt_ctf_attributes_get_count(trace->environment);
1722 if (env_size <= 0) {
1723 return;
1724 }
1725
1726 g_string_append(context->string, "env {\n");
1727
1728 for (i = 0; i < env_size; i++) {
1729 struct bt_value *env_field_value_obj = NULL;
1730 const char *entry_name;
1731
1732 entry_name = bt_ctf_attributes_get_field_name(
1733 trace->environment, i);
1734 env_field_value_obj = bt_ctf_attributes_get_field_value(
1735 trace->environment, i);
1736
1737 assert(entry_name);
1738 assert(env_field_value_obj);
1739
1740 switch (bt_value_get_type(env_field_value_obj)) {
1741 case BT_VALUE_TYPE_INTEGER:
1742 {
1743 int ret;
1744 int64_t int_value;
1745
1746 ret = bt_value_integer_get(env_field_value_obj,
1747 &int_value);
1748 assert(ret == 0);
1749 g_string_append_printf(context->string,
1750 "\t%s = %" PRId64 ";\n", entry_name,
1751 int_value);
1752 break;
1753 }
1754 case BT_VALUE_TYPE_STRING:
1755 {
1756 int ret;
1757 const char *str_value;
1758 char *escaped_str = NULL;
1759
1760 ret = bt_value_string_get(env_field_value_obj,
1761 &str_value);
1762 assert(ret == 0);
1763 escaped_str = g_strescape(str_value, NULL);
1764 if (!escaped_str) {
1765 BT_LOGE("Cannot escape string: string=\"%s\"",
1766 str_value);
1767 goto loop_next;
1768 }
1769
1770 g_string_append_printf(context->string,
1771 "\t%s = \"%s\";\n", entry_name, escaped_str);
1772 free(escaped_str);
1773 break;
1774 }
1775 default:
1776 goto loop_next;
1777 }
1778
1779 loop_next:
1780 BT_PUT(env_field_value_obj);
1781 }
1782
1783 g_string_append(context->string, "};\n\n");
1784 }
1785
1786 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
1787 {
1788 char *metadata = NULL;
1789 struct metadata_context *context = NULL;
1790 int err = 0;
1791 size_t i;
1792
1793 if (!trace) {
1794 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1795 goto end;
1796 }
1797
1798 context = g_new0(struct metadata_context, 1);
1799 if (!context) {
1800 BT_LOGE_STR("Failed to allocate one metadata context.");
1801 goto end;
1802 }
1803
1804 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
1805 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
1806 g_string_append(context->string, "/* CTF 1.8 */\n\n");
1807 if (append_trace_metadata(trace, context)) {
1808 /* append_trace_metadata() logs errors */
1809 goto error;
1810 }
1811 append_env_metadata(trace, context);
1812 g_ptr_array_foreach(trace->clocks,
1813 (GFunc)bt_ctf_clock_class_serialize, context);
1814
1815 for (i = 0; i < trace->stream_classes->len; i++) {
1816 /* bt_ctf_stream_class_serialize() logs details */
1817 err = bt_ctf_stream_class_serialize(
1818 trace->stream_classes->pdata[i], context);
1819 if (err) {
1820 /* bt_ctf_stream_class_serialize() logs errors */
1821 goto error;
1822 }
1823 }
1824
1825 metadata = context->string->str;
1826
1827 error:
1828 g_string_free(context->string, err ? TRUE : FALSE);
1829 g_string_free(context->field_name, TRUE);
1830 g_free(context);
1831
1832 end:
1833 return metadata;
1834 }
1835
1836 enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order(
1837 struct bt_ctf_trace *trace)
1838 {
1839 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
1840
1841 if (!trace) {
1842 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1843 goto end;
1844 }
1845
1846 ret = trace->native_byte_order;
1847
1848 end:
1849 return ret;
1850 }
1851
1852 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
1853 enum bt_ctf_byte_order byte_order)
1854 {
1855 int ret = 0;
1856
1857 if (!trace) {
1858 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1859 ret = -1;
1860 goto end;
1861 }
1862
1863 if (trace->frozen) {
1864 BT_LOGW("Invalid parameter: trace is frozen: "
1865 "addr=%p, name=\"%s\"",
1866 trace, bt_ctf_trace_get_name(trace));
1867 ret = -1;
1868 goto end;
1869 }
1870
1871 if (trace->is_created_by_writer &&
1872 byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
1873 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
1874 "addr=%p, name=\"%s\"",
1875 trace, bt_ctf_trace_get_name(trace));
1876 ret = -1;
1877 goto end;
1878 }
1879
1880 if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
1881 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
1882 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
1883 BT_LOGW("Invalid parameter: invalid byte order: "
1884 "addr=%p, name=\"%s\", bo=%s",
1885 trace, bt_ctf_trace_get_name(trace),
1886 bt_ctf_byte_order_string(byte_order));
1887 ret = -1;
1888 goto end;
1889 }
1890
1891 trace->native_byte_order = byte_order;
1892 BT_LOGV("Set trace's native byte order: "
1893 "addr=%p, name=\"%s\", bo=%s",
1894 trace, bt_ctf_trace_get_name(trace),
1895 bt_ctf_byte_order_string(byte_order));
1896
1897 end:
1898 return ret;
1899 }
1900
1901 struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
1902 struct bt_ctf_trace *trace)
1903 {
1904 struct bt_ctf_field_type *field_type = NULL;
1905
1906 if (!trace) {
1907 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1908 goto end;
1909 }
1910
1911 bt_get(trace->packet_header_type);
1912 field_type = trace->packet_header_type;
1913 end:
1914 return field_type;
1915 }
1916
1917 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1918 struct bt_ctf_field_type *packet_header_type)
1919 {
1920 int ret = 0;
1921
1922 if (!trace) {
1923 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1924 ret = -1;
1925 goto end;
1926 }
1927
1928 if (trace->frozen) {
1929 BT_LOGW("Invalid parameter: trace is frozen: "
1930 "addr=%p, name=\"%s\"",
1931 trace, bt_ctf_trace_get_name(trace));
1932 ret = -1;
1933 goto end;
1934 }
1935
1936 /* packet_header_type must be a structure. */
1937 if (packet_header_type &&
1938 !bt_ctf_field_type_is_structure(packet_header_type)) {
1939 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1940 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1941 trace, bt_ctf_trace_get_name(trace),
1942 packet_header_type,
1943 bt_ctf_field_type_id_string(packet_header_type->id));
1944 ret = -1;
1945 goto end;
1946 }
1947
1948 bt_put(trace->packet_header_type);
1949 trace->packet_header_type = bt_get(packet_header_type);
1950 BT_LOGV("Set trace's packet header field type: "
1951 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1952 trace, bt_ctf_trace_get_name(trace), packet_header_type);
1953 end:
1954 return ret;
1955 }
1956
1957 static
1958 int64_t get_stream_class_count(void *element)
1959 {
1960 return bt_ctf_trace_get_stream_class_count(
1961 (struct bt_ctf_trace *) element);
1962 }
1963
1964 static
1965 void *get_stream_class(void *element, int i)
1966 {
1967 return bt_ctf_trace_get_stream_class_by_index(
1968 (struct bt_ctf_trace *) element, i);
1969 }
1970
1971 static
1972 int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
1973 {
1974 return bt_ctf_stream_class_visit(object, visitor, data);
1975 }
1976
1977 int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
1978 bt_ctf_visitor visitor, void *data)
1979 {
1980 int ret;
1981 struct bt_ctf_object obj =
1982 { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
1983
1984 if (!trace) {
1985 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1986 ret = -1;
1987 goto end;
1988 }
1989
1990 if (!visitor) {
1991 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1992 ret = -1;
1993 goto end;
1994 }
1995
1996 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
1997 trace, bt_ctf_trace_get_name(trace));
1998 ret = visitor_helper(&obj, get_stream_class_count,
1999 get_stream_class, visit_stream_class, visitor, data);
2000 end:
2001 return ret;
2002 }
2003
2004 static
2005 int invoke_listener(struct bt_ctf_object *object, void *data)
2006 {
2007 struct listener_wrapper *listener_wrapper = data;
2008
2009 listener_wrapper->listener(object, listener_wrapper->data);
2010 return 0;
2011 }
2012
2013 // TODO: add logging to this function once we use it internally.
2014 int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
2015 bt_ctf_listener_cb listener, void *listener_data)
2016 {
2017 int ret = 0;
2018 struct listener_wrapper *listener_wrapper =
2019 g_new0(struct listener_wrapper, 1);
2020
2021 if (!trace || !listener || !listener_wrapper) {
2022 ret = -1;
2023 goto error;
2024 }
2025
2026 listener_wrapper->listener = listener;
2027 listener_wrapper->data = listener_data;
2028
2029 /* Visit the current schema. */
2030 ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
2031 if (ret) {
2032 goto error;
2033 }
2034
2035 /*
2036 * Add listener to the array of callbacks which will be invoked on
2037 * schema changes.
2038 */
2039 g_ptr_array_add(trace->listeners, listener_wrapper);
2040 return ret;
2041 error:
2042 g_free(listener_wrapper);
2043 return ret;
2044 }
2045
2046 BT_HIDDEN
2047 int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
2048 void *trace_ptr)
2049 {
2050 size_t i;
2051 struct bt_ctf_trace *trace = trace_ptr;
2052
2053 assert(trace);
2054 assert(object);
2055
2056 if (trace->listeners->len == 0) {
2057 goto end;
2058 }
2059
2060 for (i = 0; i < trace->listeners->len; i++) {
2061 struct listener_wrapper *listener =
2062 g_ptr_array_index(trace->listeners, i);
2063
2064 listener->listener(object, listener->data);
2065 }
2066 end:
2067 return 0;
2068 }
2069
2070 BT_HIDDEN
2071 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
2072 {
2073 int ret;
2074 unsigned int alignment, size;
2075 struct bt_ctf_field_type *field_type = NULL;
2076
2077 if (alias >= NR_FIELD_TYPE_ALIAS) {
2078 goto end;
2079 }
2080
2081 alignment = field_type_aliases_alignments[alias];
2082 size = field_type_aliases_sizes[alias];
2083 field_type = bt_ctf_field_type_integer_create(size);
2084 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
2085 if (ret) {
2086 BT_PUT(field_type);
2087 }
2088 end:
2089 return field_type;
2090 }
2091
2092 static
2093 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
2094 {
2095 int i;
2096
2097 if (trace->frozen) {
2098 return;
2099 }
2100
2101 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2102 trace, bt_ctf_trace_get_name(trace));
2103 BT_LOGD_STR("Freezing packet header field type.");
2104 bt_ctf_field_type_freeze(trace->packet_header_type);
2105 BT_LOGD_STR("Freezing environment attributes.");
2106 bt_ctf_attributes_freeze(trace->environment);
2107
2108 if (trace->clocks->len > 0) {
2109 BT_LOGD_STR("Freezing clock classes.");
2110 }
2111
2112 for (i = 0; i < trace->clocks->len; i++) {
2113 struct bt_ctf_clock_class *clock_class =
2114 g_ptr_array_index(trace->clocks, i);
2115
2116 bt_ctf_clock_class_freeze(clock_class);
2117 }
2118
2119 trace->frozen = 1;
2120 }
2121
2122 bt_bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
2123 {
2124 bt_bool is_static = BT_FALSE;
2125
2126 if (!trace) {
2127 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2128 goto end;
2129 }
2130
2131 is_static = trace->is_static;
2132
2133 end:
2134 return is_static;
2135 }
2136
2137 int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace)
2138 {
2139 int ret = 0;
2140 size_t i;
2141
2142 if (!trace) {
2143 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2144 ret = -1;
2145 goto end;
2146 }
2147
2148 trace->is_static = BT_TRUE;
2149 bt_ctf_trace_freeze(trace);
2150 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2151 trace, bt_ctf_trace_get_name(trace));
2152
2153 /* Call all the "trace is static" listeners */
2154 for (i = 0; i < trace->is_static_listeners->len; i++) {
2155 struct bt_ctf_trace_is_static_listener_elem elem =
2156 g_array_index(trace->is_static_listeners,
2157 struct bt_ctf_trace_is_static_listener_elem, i);
2158
2159 if (elem.func) {
2160 elem.func(trace, elem.data);
2161 }
2162 }
2163
2164 end:
2165 return ret;
2166 }
2167
2168 int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace *trace,
2169 bt_ctf_trace_is_static_listener listener, void *data)
2170 {
2171 int i;
2172 struct bt_ctf_trace_is_static_listener_elem new_elem = {
2173 .func = listener,
2174 .data = data,
2175 };
2176
2177 if (!trace) {
2178 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2179 i = -1;
2180 goto end;
2181 }
2182
2183 if (!listener) {
2184 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2185 i = -1;
2186 goto end;
2187 }
2188
2189 if (trace->is_static) {
2190 BT_LOGW("Invalid parameter: trace is already static: "
2191 "addr=%p, name=\"%s\"",
2192 trace, bt_ctf_trace_get_name(trace));
2193 i = -1;
2194 goto end;
2195 }
2196
2197 /* Find the next available spot */
2198 for (i = 0; i < trace->is_static_listeners->len; i++) {
2199 struct bt_ctf_trace_is_static_listener_elem elem =
2200 g_array_index(trace->is_static_listeners,
2201 struct bt_ctf_trace_is_static_listener_elem, i);
2202
2203 if (!elem.func) {
2204 break;
2205 }
2206 }
2207
2208 if (i == trace->is_static_listeners->len) {
2209 g_array_append_val(trace->is_static_listeners, new_elem);
2210 } else {
2211 g_array_insert_val(trace->is_static_listeners, i, new_elem);
2212 }
2213
2214 BT_LOGV("Added \"trace is static\" listener: "
2215 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2216 "data-addr=%p, listener-id=%d",
2217 trace, bt_ctf_trace_get_name(trace), listener, data, i);
2218
2219 end:
2220 return i;
2221 }
2222
2223 int bt_ctf_trace_remove_is_static_listener(
2224 struct bt_ctf_trace *trace, int listener_id)
2225 {
2226 int ret = 0;
2227 struct bt_ctf_trace_is_static_listener_elem *elem;
2228
2229 if (!trace) {
2230 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2231 ret = -1;
2232 goto end;
2233 }
2234
2235 if (listener_id < 0) {
2236 BT_LOGW("Invalid listener ID: must be zero or positive: "
2237 "listener-id=%d", listener_id);
2238 ret = -1;
2239 goto end;
2240 }
2241
2242 if (listener_id >= trace->is_static_listeners->len) {
2243 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2244 "addr=%p, name=\"%s\", listener-id=%d",
2245 trace, bt_ctf_trace_get_name(trace),
2246 listener_id);
2247 ret = -1;
2248 goto end;
2249 }
2250
2251 elem = &g_array_index(trace->is_static_listeners,
2252 struct bt_ctf_trace_is_static_listener_elem,
2253 listener_id);
2254 if (!elem->func) {
2255 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2256 "addr=%p, name=\"%s\", listener-id=%d",
2257 trace, bt_ctf_trace_get_name(trace),
2258 listener_id);
2259 ret = -1;
2260 goto end;
2261 }
2262
2263 elem->func = NULL;
2264 elem->data = NULL;
2265 BT_LOGV("Removed \"trace is static\" listener: "
2266 "trace-addr=%p, trace-name=\"%s\", "
2267 "listener-id=%d", trace, bt_ctf_trace_get_name(trace),
2268 listener_id);
2269
2270 end:
2271 return ret;
2272 }
This page took 0.101957 seconds and 5 git commands to generate.