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