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