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