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