Visibility hidden by default
[babeltrace.git] / src / ctf-writer / trace.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_LOG_TAG "CTF-WRITER/TRACE"
9 #include "logging.h"
10
11 #include <inttypes.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <babeltrace2-ctf-writer/event.h>
18 #include <babeltrace2-ctf-writer/object.h>
19 #include <babeltrace2-ctf-writer/utils.h>
20 #include <babeltrace2/types.h>
21
22 #include "common/assert.h"
23 #include "compat/compiler.h"
24 #include "compat/endian.h"
25
26 #include "attributes.h"
27 #include "clock-class.h"
28 #include "clock.h"
29 #include "event-class.h"
30 #include "event.h"
31 #include "field-types.h"
32 #include "field-wrapper.h"
33 #include "functor.h"
34 #include "stream-class.h"
35 #include "stream.h"
36 #include "trace.h"
37 #include "utils.h"
38 #include "validation.h"
39 #include "values.h"
40 #include "visitor.h"
41 #include "writer.h"
42
43 #define DEFAULT_IDENTIFIER_SIZE 128
44 #define DEFAULT_METADATA_STRING_SIZE 4096
45
46 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace,
47 bt_ctf_object_release_func release_func)
48 {
49 int ret = 0;
50
51 BT_LOGD_STR("Initializing common trace object.");
52 trace->native_byte_order = BT_CTF_BYTE_ORDER_UNSPECIFIED;
53 bt_ctf_object_init_shared_with_parent(&trace->base, release_func);
54 trace->clock_classes = g_ptr_array_new_with_free_func(
55 (GDestroyNotify) bt_ctf_object_put_ref);
56 if (!trace->clock_classes) {
57 BT_LOGE_STR("Failed to allocate one GPtrArray.");
58 goto error;
59 }
60
61 trace->streams = g_ptr_array_new_with_free_func(
62 (GDestroyNotify) bt_ctf_object_try_spec_release);
63 if (!trace->streams) {
64 BT_LOGE_STR("Failed to allocate one GPtrArray.");
65 goto error;
66 }
67
68 trace->stream_classes = g_ptr_array_new_with_free_func(
69 (GDestroyNotify) bt_ctf_object_try_spec_release);
70 if (!trace->stream_classes) {
71 BT_LOGE_STR("Failed to allocate one GPtrArray.");
72 goto error;
73 }
74
75 /* Create the environment array object */
76 trace->environment = bt_ctf_attributes_create();
77 if (!trace->environment) {
78 BT_LOGE_STR("Cannot create empty attributes object.");
79 goto error;
80 }
81
82 BT_LOGD("Initialized common trace object: addr=%p", trace);
83 goto end;
84
85 error:
86 ret = -1;
87
88 end:
89 return ret;
90 }
91
92 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace)
93 {
94 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
95 trace, bt_ctf_trace_common_get_name(trace));
96
97 if (trace->environment) {
98 BT_LOGD_STR("Destroying environment attributes.");
99 bt_ctf_attributes_destroy(trace->environment);
100 }
101
102 if (trace->name) {
103 g_string_free(trace->name, TRUE);
104 }
105
106 if (trace->clock_classes) {
107 BT_LOGD_STR("Putting clock classes.");
108 g_ptr_array_free(trace->clock_classes, TRUE);
109 }
110
111 if (trace->streams) {
112 BT_LOGD_STR("Destroying streams.");
113 g_ptr_array_free(trace->streams, TRUE);
114 }
115
116 if (trace->stream_classes) {
117 BT_LOGD_STR("Destroying stream classes.");
118 g_ptr_array_free(trace->stream_classes, TRUE);
119 }
120
121 BT_LOGD_STR("Putting packet header field type.");
122 bt_ctf_object_put_ref(trace->packet_header_field_type);
123 }
124
125 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name)
126 {
127 int ret = 0;
128
129 if (!trace) {
130 BT_LOGW_STR("Invalid parameter: trace is NULL.");
131 ret = -1;
132 goto end;
133 }
134
135 if (!name) {
136 BT_LOGW_STR("Invalid parameter: name is NULL.");
137 ret = -1;
138 goto end;
139 }
140
141 if (trace->frozen) {
142 BT_LOGW("Invalid parameter: trace is frozen: "
143 "addr=%p, name=\"%s\"",
144 trace, bt_ctf_trace_common_get_name(trace));
145 ret = -1;
146 goto end;
147 }
148
149 trace->name = trace->name ? g_string_assign(trace->name, name) :
150 g_string_new(name);
151 if (!trace->name) {
152 BT_LOGE_STR("Failed to allocate one GString.");
153 ret = -1;
154 goto end;
155 }
156
157 BT_LOGT("Set trace's name: addr=%p, name=\"%s\"", trace, name);
158
159 end:
160 return ret;
161 }
162
163 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace,
164 const uint8_t *uuid)
165 {
166 int ret = 0;
167
168 if (!trace) {
169 BT_LOGW_STR("Invalid parameter: trace is NULL.");
170 ret = -1;
171 goto end;
172 }
173
174 if (!uuid) {
175 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
176 ret = -1;
177 goto end;
178 }
179
180 if (trace->frozen) {
181 BT_LOGW("Invalid parameter: trace is frozen: "
182 "addr=%p, name=\"%s\"",
183 trace, bt_ctf_trace_common_get_name(trace));
184 ret = -1;
185 goto end;
186 }
187
188 bt_uuid_copy(trace->uuid, uuid);
189 trace->uuid_set = BT_CTF_TRUE;
190 BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", "
191 "uuid=\"" BT_UUID_FMT "\"",
192 trace, bt_ctf_trace_common_get_name(trace),
193 BT_UUID_FMT_VALUES(uuid));
194
195 end:
196 return ret;
197 }
198
199 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace,
200 const char *name, struct bt_ctf_private_value *value)
201 {
202 int ret = 0;
203
204 if (!trace) {
205 BT_LOGW_STR("Invalid parameter: trace is NULL.");
206 ret = -1;
207 goto end;
208 }
209
210 if (!name) {
211 BT_LOGW_STR("Invalid parameter: name is NULL.");
212 ret = -1;
213 goto end;
214 }
215
216 if (!value) {
217 BT_LOGW_STR("Invalid parameter: value is NULL.");
218 ret = -1;
219 goto end;
220 }
221
222 if (!bt_ctf_identifier_is_valid(name)) {
223 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
224 "trace-addr=%p, trace-name=\"%s\", "
225 "env-name=\"%s\"",
226 trace, bt_ctf_trace_common_get_name(trace), name);
227 ret = -1;
228 goto end;
229 }
230
231 if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value)) &&
232 !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value))) {
233 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
234 "trace-addr=%p, trace-name=\"%s\", "
235 "env-name=\"%s\", env-value-type=%s",
236 trace, bt_ctf_trace_common_get_name(trace), name,
237 bt_ctf_value_type_string(
238 bt_ctf_value_get_type(
239 bt_ctf_private_value_as_value(value))));
240 ret = -1;
241 goto end;
242 }
243
244 if (trace->frozen) {
245 /*
246 * New environment fields may be added to a frozen trace,
247 * but existing fields may not be changed.
248 *
249 * The object passed is frozen like all other attributes.
250 */
251 struct bt_ctf_private_value *attribute =
252 bt_ctf_attributes_borrow_field_value_by_name(
253 trace->environment, name);
254
255 if (attribute) {
256 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
257 "trace-addr=%p, trace-name=\"%s\", "
258 "env-name=\"%s\"",
259 trace, bt_ctf_trace_common_get_name(trace), name);
260 ret = -1;
261 goto end;
262 }
263
264 bt_ctf_value_freeze(bt_ctf_private_value_as_value(value));
265 }
266
267 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
268 value);
269 if (ret) {
270 BT_LOGE("Cannot set environment field's value: "
271 "trace-addr=%p, trace-name=\"%s\", "
272 "env-name=\"%s\"",
273 trace, bt_ctf_trace_common_get_name(trace), name);
274 } else {
275 BT_LOGT("Set environment field's value: "
276 "trace-addr=%p, trace-name=\"%s\", "
277 "env-name=\"%s\", value-addr=%p",
278 trace, bt_ctf_trace_common_get_name(trace), name, value);
279 }
280
281 end:
282 return ret;
283 }
284
285 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace,
286 const char *name, const char *value)
287 {
288 int ret = 0;
289 struct bt_ctf_private_value *env_value_string_obj = NULL;
290
291 if (!value) {
292 BT_LOGW_STR("Invalid parameter: value is NULL.");
293 ret = -1;
294 goto end;
295 }
296
297 env_value_string_obj = bt_ctf_private_value_string_create_init(value);
298 if (!env_value_string_obj) {
299 BT_LOGE_STR("Cannot create string value object.");
300 ret = -1;
301 goto end;
302 }
303
304 /* bt_ctf_trace_common_set_environment_field() logs errors */
305 ret = bt_ctf_trace_common_set_environment_field(trace, name,
306 env_value_string_obj);
307
308 end:
309 bt_ctf_object_put_ref(env_value_string_obj);
310 return ret;
311 }
312
313 int bt_ctf_trace_common_set_environment_field_integer(
314 struct bt_ctf_trace_common *trace, const char *name, int64_t value)
315 {
316 int ret = 0;
317 struct bt_ctf_private_value *env_value_integer_obj = NULL;
318
319 env_value_integer_obj = bt_ctf_private_value_integer_create_init(value);
320 if (!env_value_integer_obj) {
321 BT_LOGE_STR("Cannot create integer value object.");
322 ret = -1;
323 goto end;
324 }
325
326 /* bt_ctf_trace_common_set_environment_field() logs errors */
327 ret = bt_ctf_trace_common_set_environment_field(trace, name,
328 env_value_integer_obj);
329
330 end:
331 bt_ctf_object_put_ref(env_value_integer_obj);
332 return ret;
333 }
334
335 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace,
336 struct bt_ctf_clock_class *clock_class)
337 {
338 int ret = 0;
339
340 if (!trace) {
341 BT_LOGW_STR("Invalid parameter: trace is NULL.");
342 ret = -1;
343 goto end;
344 }
345
346 if (!bt_ctf_clock_class_is_valid(clock_class)) {
347 BT_LOGW("Invalid parameter: clock class is invalid: "
348 "trace-addr=%p, trace-name=\"%s\", "
349 "clock-class-addr=%p, clock-class-name=\"%s\"",
350 trace, bt_ctf_trace_common_get_name(trace),
351 clock_class, bt_ctf_clock_class_get_name(clock_class));
352 ret = -1;
353 goto end;
354 }
355
356 /* Check for duplicate clock classes */
357 if (bt_ctf_trace_common_has_clock_class(trace, clock_class)) {
358 BT_LOGW("Invalid parameter: clock class already exists in trace: "
359 "trace-addr=%p, trace-name=\"%s\", "
360 "clock-class-addr=%p, clock-class-name=\"%s\"",
361 trace, bt_ctf_trace_common_get_name(trace),
362 clock_class, bt_ctf_clock_class_get_name(clock_class));
363 ret = -1;
364 goto end;
365 }
366
367 bt_ctf_object_get_ref(clock_class);
368 g_ptr_array_add(trace->clock_classes, clock_class);
369
370 if (trace->frozen) {
371 BT_LOGT_STR("Freezing added clock class because trace is frozen.");
372 bt_ctf_clock_class_freeze(clock_class);
373 }
374
375 BT_LOGT("Added clock class to trace: "
376 "trace-addr=%p, trace-name=\"%s\", "
377 "clock-class-addr=%p, clock-class-name=\"%s\"",
378 trace, bt_ctf_trace_common_get_name(trace),
379 clock_class, bt_ctf_clock_class_get_name(clock_class));
380
381 end:
382 return ret;
383 }
384
385 static
386 bool packet_header_field_type_is_valid(struct bt_ctf_trace_common *trace,
387 struct bt_ctf_field_type_common *packet_header_type)
388 {
389 int ret;
390 bool is_valid = true;
391 struct bt_ctf_field_type_common *field_type = NULL;
392
393 if (!packet_header_type) {
394 /*
395 * No packet header field type: trace must have only
396 * one stream. At this point the stream class being
397 * added is not part of the trace yet, so we validate
398 * that the trace contains no stream classes yet.
399 */
400 if (trace->stream_classes->len >= 1) {
401 BT_LOGW_STR("Invalid packet header field type: "
402 "packet header field type does not exist but there's more than one stream class in the trace.");
403 goto invalid;
404 }
405
406 /* No packet header field type: valid at this point */
407 goto end;
408 }
409
410 /* Packet header field type, if it exists, must be a structure */
411 if (packet_header_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
412 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
413 "ft-addr=%p, ft-id=%s",
414 packet_header_type,
415 bt_ctf_field_type_id_string(packet_header_type->id));
416 goto invalid;
417 }
418
419 /*
420 * If there's a `magic` field, it must be a 32-bit unsigned
421 * integer field type. Also it must be the first field of the
422 * packet header field type.
423 */
424 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
425 packet_header_type, "magic");
426 if (field_type) {
427 const char *field_name;
428
429 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
430 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
431 "magic-ft-addr=%p, magic-ft-id=%s",
432 field_type,
433 bt_ctf_field_type_id_string(field_type->id));
434 goto invalid;
435 }
436
437 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
438 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
439 "magic-ft-addr=%p", field_type);
440 goto invalid;
441 }
442
443 if (bt_ctf_field_type_common_integer_get_size(field_type) != 32) {
444 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
445 "magic-ft-addr=%p, magic-ft-size=%u",
446 field_type,
447 bt_ctf_field_type_common_integer_get_size(field_type));
448 goto invalid;
449 }
450
451 ret = bt_ctf_field_type_common_structure_borrow_field_by_index(
452 packet_header_type, &field_name, NULL, 0);
453 BT_ASSERT_DBG(ret == 0);
454
455 if (strcmp(field_name, "magic") != 0) {
456 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
457 "magic-ft-addr=%p, first-field-name=\"%s\"",
458 field_type, field_name);
459 goto invalid;
460 }
461 }
462
463 /*
464 * If there's a `uuid` field, it must be an array field type of
465 * length 16 with an 8-bit unsigned integer element field type.
466 */
467 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
468 packet_header_type, "uuid");
469 if (field_type) {
470 struct bt_ctf_field_type_common *elem_ft;
471
472 if (field_type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) {
473 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
474 "uuid-ft-addr=%p, uuid-ft-id=%s",
475 field_type,
476 bt_ctf_field_type_id_string(field_type->id));
477 goto invalid;
478 }
479
480 if (bt_ctf_field_type_common_array_get_length(field_type) != 16) {
481 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
482 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
483 field_type,
484 bt_ctf_field_type_common_array_get_length(field_type));
485 goto invalid;
486 }
487
488 elem_ft = bt_ctf_field_type_common_array_borrow_element_field_type(field_type);
489 BT_ASSERT_DBG(elem_ft);
490
491 if (elem_ft->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
492 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
493 "elem-ft-addr=%p, elem-ft-id=%s",
494 elem_ft,
495 bt_ctf_field_type_id_string(elem_ft->id));
496 goto invalid;
497 }
498
499 if (bt_ctf_field_type_common_integer_is_signed(elem_ft)) {
500 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
501 "elem-ft-addr=%p", elem_ft);
502 goto invalid;
503 }
504
505 if (bt_ctf_field_type_common_integer_get_size(elem_ft) != 8) {
506 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
507 "elem-ft-addr=%p, elem-ft-size=%u",
508 elem_ft,
509 bt_ctf_field_type_common_integer_get_size(elem_ft));
510 goto invalid;
511 }
512 }
513
514 /*
515 * The `stream_id` field must exist if there's more than one
516 * stream classes in the trace.
517 */
518 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
519 packet_header_type, "stream_id");
520
521 if (!field_type && trace->stream_classes->len >= 1) {
522 BT_LOGW_STR("Invalid packet header field type: "
523 "`stream_id` field does not exist but there's more than one stream class in the trace.");
524 goto invalid;
525 }
526
527 /*
528 * If there's a `stream_id` field, it must be an unsigned
529 * integer field type.
530 */
531 if (field_type) {
532 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
533 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
534 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
535 field_type,
536 bt_ctf_field_type_id_string(field_type->id));
537 goto invalid;
538 }
539
540 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
541 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
542 "stream-id-ft-addr=%p", field_type);
543 goto invalid;
544 }
545 }
546
547 /*
548 * If there's a `packet_seq_num` field, it must be an unsigned
549 * integer field type.
550 */
551 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
552 packet_header_type, "packet_seq_num");
553 if (field_type) {
554 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
555 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
556 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
557 field_type,
558 bt_ctf_field_type_id_string(field_type->id));
559 goto invalid;
560 }
561
562 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
563 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
564 "packet-seq-num-ft-addr=%p", field_type);
565 goto invalid;
566 }
567 }
568
569 goto end;
570
571 invalid:
572 is_valid = false;
573
574 end:
575 return is_valid;
576 }
577
578 static
579 bool packet_context_field_type_is_valid(struct bt_ctf_trace_common *trace,
580 struct bt_ctf_stream_class_common *stream_class,
581 struct bt_ctf_field_type_common *packet_context_type,
582 bool check_ts_begin_end_mapped)
583 {
584 bool is_valid = true;
585 struct bt_ctf_field_type_common *field_type = NULL;
586
587 if (!packet_context_type) {
588 /* No packet context field type: valid at this point */
589 goto end;
590 }
591
592 /* Packet context field type, if it exists, must be a structure */
593 if (packet_context_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
594 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
595 "ft-addr=%p, ft-id=%s",
596 packet_context_type,
597 bt_ctf_field_type_id_string(packet_context_type->id));
598 goto invalid;
599 }
600
601 /*
602 * If there's a `packet_size` field, it must be an unsigned
603 * integer field type.
604 */
605 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
606 packet_context_type, "packet_size");
607 if (field_type) {
608 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
609 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
610 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
611 field_type,
612 bt_ctf_field_type_id_string(field_type->id));
613 goto invalid;
614 }
615
616 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
617 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
618 "packet-size-ft-addr=%p", field_type);
619 goto invalid;
620 }
621 }
622
623 /*
624 * If there's a `content_size` field, it must be an unsigned
625 * integer field type.
626 */
627 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
628 packet_context_type, "content_size");
629 if (field_type) {
630 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
631 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
632 "content-size-ft-addr=%p, content-size-ft-id=%s",
633 field_type,
634 bt_ctf_field_type_id_string(field_type->id));
635 goto invalid;
636 }
637
638 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
639 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
640 "content-size-ft-addr=%p", field_type);
641 goto invalid;
642 }
643 }
644
645 /*
646 * If there's a `events_discarded` field, it must be an unsigned
647 * integer field type.
648 */
649 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
650 packet_context_type, "events_discarded");
651 if (field_type) {
652 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
653 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
654 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
655 field_type,
656 bt_ctf_field_type_id_string(field_type->id));
657 goto invalid;
658 }
659
660 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
661 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
662 "events-discarded-ft-addr=%p", field_type);
663 goto invalid;
664 }
665 }
666
667 /*
668 * If there's a `timestamp_begin` field, it must be an unsigned
669 * integer field type. Also, if the trace is not a CTF writer's
670 * trace, then we cannot automatically set the mapped clock
671 * class of this field, so it must have a mapped clock class.
672 */
673 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
674 packet_context_type, "timestamp_begin");
675 if (field_type) {
676 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
677 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
678 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
679 field_type,
680 bt_ctf_field_type_id_string(field_type->id));
681 goto invalid;
682 }
683
684 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
685 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
686 "timestamp-begin-ft-addr=%p", field_type);
687 goto invalid;
688 }
689
690 if (check_ts_begin_end_mapped) {
691 struct bt_ctf_clock_class *clock_class =
692 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
693 field_type);
694
695 if (!clock_class) {
696 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
697 "timestamp-begin-ft-addr=%p", field_type);
698 goto invalid;
699 }
700 }
701 }
702
703 /*
704 * If there's a `timestamp_end` field, it must be an unsigned
705 * integer field type. Also, if the trace is not a CTF writer's
706 * trace, then we cannot automatically set the mapped clock
707 * class of this field, so it must have a mapped clock class.
708 */
709 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
710 packet_context_type, "timestamp_end");
711 if (field_type) {
712 if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
713 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
714 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
715 field_type,
716 bt_ctf_field_type_id_string(field_type->id));
717 goto invalid;
718 }
719
720 if (bt_ctf_field_type_common_integer_is_signed(field_type)) {
721 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
722 "timestamp-end-ft-addr=%p", field_type);
723 goto invalid;
724 }
725
726 if (check_ts_begin_end_mapped) {
727 struct bt_ctf_clock_class *clock_class =
728 bt_ctf_field_type_common_integer_borrow_mapped_clock_class(
729 field_type);
730
731 if (!clock_class) {
732 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
733 "timestamp-end-ft-addr=%p", field_type);
734 goto invalid;
735 }
736 }
737 }
738
739 goto end;
740
741 invalid:
742 is_valid = false;
743
744 end:
745 return is_valid;
746 }
747
748 static
749 bool event_header_field_type_is_valid(struct bt_ctf_trace_common *trace,
750 struct bt_ctf_stream_class_common *stream_class,
751 struct bt_ctf_field_type_common *event_header_type)
752 {
753 bool is_valid = true;
754 struct bt_ctf_field_type_common *field_type = NULL;
755
756 /*
757 * We do not validate that the `timestamp` field exists here
758 * because CTF does not require this exact name to be mapped to
759 * a clock class.
760 */
761
762 if (!event_header_type) {
763 /*
764 * No event header field type: stream class must have
765 * only one event class.
766 */
767 if (bt_ctf_stream_class_common_get_event_class_count(stream_class) > 1) {
768 BT_LOGW_STR("Invalid event header field type: "
769 "event header field type does not exist but there's more than one event class in the stream class.");
770 goto invalid;
771 }
772
773 /* No event header field type: valid at this point */
774 goto end;
775 }
776
777 /* Event header field type, if it exists, must be a structure */
778 if (event_header_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
779 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
780 "ft-addr=%p, ft-id=%s",
781 event_header_type,
782 bt_ctf_field_type_id_string(event_header_type->id));
783 goto invalid;
784 }
785
786 /*
787 * If there's an `id` field, it must be an unsigned integer
788 * field type or an enumeration field type with an unsigned
789 * integer container field type.
790 */
791 field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name(
792 event_header_type, "id");
793 if (field_type) {
794 struct bt_ctf_field_type_common *int_ft;
795
796 if (field_type->id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
797 int_ft = field_type;
798 } else if (field_type->id == BT_CTF_FIELD_TYPE_ID_ENUM) {
799 int_ft = bt_ctf_field_type_common_enumeration_borrow_container_field_type(
800 field_type);
801 } else {
802 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
803 "id-ft-addr=%p, id-ft-id=%s",
804 field_type,
805 bt_ctf_field_type_id_string(field_type->id));
806 goto invalid;
807 }
808
809 BT_ASSERT_DBG(int_ft);
810 if (bt_ctf_field_type_common_integer_is_signed(int_ft)) {
811 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
812 "id-ft-addr=%p", int_ft);
813 goto invalid;
814 }
815 }
816
817 goto end;
818
819 invalid:
820 is_valid = false;
821
822 end:
823 return is_valid;
824 }
825
826 static
827 int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common *trace)
828 {
829 int ret = 0;
830
831 if (trace->packet_header_field_type) {
832 struct bt_ctf_clock_class *clock_class = NULL;
833
834 ret = bt_ctf_field_type_common_validate_single_clock_class(
835 trace->packet_header_field_type,
836 &clock_class);
837 bt_ctf_object_put_ref(clock_class);
838 if (ret || clock_class) {
839 BT_LOGW("Trace's packet header field type cannot "
840 "contain a field type which is mapped to "
841 "a clock class: "
842 "trace-addr=%p, trace-name=\"%s\", "
843 "clock-class-name=\"%s\"",
844 trace, bt_ctf_trace_common_get_name(trace),
845 clock_class ?
846 bt_ctf_clock_class_get_name(clock_class) :
847 NULL);
848 ret = -1;
849 }
850 }
851
852 return ret;
853 }
854
855 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common *trace,
856 struct bt_ctf_stream_class_common *stream_class,
857 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func,
858 struct bt_ctf_clock_class *init_expected_clock_class,
859 int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class,
860 struct bt_ctf_field_type_common *packet_context_field_type,
861 struct bt_ctf_field_type_common *event_header_field_type),
862 bool check_ts_begin_end_mapped)
863 {
864 int ret;
865 int64_t i;
866 int64_t stream_id;
867 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
868 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
869 const enum bt_ctf_validation_flag trace_sc_validation_flags =
870 BT_CTF_VALIDATION_FLAG_TRACE |
871 BT_CTF_VALIDATION_FLAG_STREAM;
872 const enum bt_ctf_validation_flag ec_validation_flags =
873 BT_CTF_VALIDATION_FLAG_EVENT;
874 struct bt_ctf_field_type_common *packet_header_type = NULL;
875 struct bt_ctf_field_type_common *packet_context_type = NULL;
876 struct bt_ctf_field_type_common *event_header_type = NULL;
877 struct bt_ctf_field_type_common *stream_event_ctx_type = NULL;
878 int64_t event_class_count = 0;
879 struct bt_ctf_trace_common *current_parent_trace = NULL;
880 struct bt_ctf_clock_class *expected_clock_class =
881 bt_ctf_object_get_ref(init_expected_clock_class);
882
883 BT_ASSERT_DBG(copy_field_type_func);
884
885 if (!trace) {
886 BT_LOGW_STR("Invalid parameter: trace is NULL.");
887 ret = -1;
888 goto end;
889 }
890
891 if (!stream_class) {
892 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
893 ret = -1;
894 goto end;
895 }
896
897 BT_LOGD("Adding stream class to trace: "
898 "trace-addr=%p, trace-name=\"%s\", "
899 "stream-class-addr=%p, stream-class-name=\"%s\", "
900 "stream-class-id=%" PRId64,
901 trace, bt_ctf_trace_common_get_name(trace),
902 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
903 bt_ctf_stream_class_common_get_id(stream_class));
904
905 current_parent_trace = bt_ctf_stream_class_common_borrow_trace(stream_class);
906 if (current_parent_trace) {
907 /* Stream class is already associated to a trace, abort. */
908 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
909 "stream-class-trace-addr=%p, "
910 "stream-class-trace-name=\"%s\"",
911 current_parent_trace,
912 bt_ctf_trace_common_get_name(current_parent_trace));
913 ret = -1;
914 goto end;
915 }
916
917 event_class_count =
918 bt_ctf_stream_class_common_get_event_class_count(stream_class);
919 BT_ASSERT_DBG(event_class_count >= 0);
920
921 if (!stream_class->frozen) {
922 /*
923 * Stream class is not frozen yet. Validate that the
924 * stream class contains at most a single clock class
925 * because the previous
926 * bt_ctf_stream_class_common_add_event_class() calls did
927 * not make this validation since the stream class's
928 * direct field types (packet context, event header,
929 * event context) could change afterwards. This stream
930 * class is about to be frozen and those field types
931 * won't be changed if this function succeeds.
932 *
933 * At this point we're also sure that the stream class's
934 * clock, if any, has the same class as the stream
935 * class's expected clock class, if any. This is why, if
936 * bt_ctf_stream_class_common_validate_single_clock_class()
937 * succeeds below, the call to
938 * bt_ctf_stream_class_map_clock_class() at the end of this
939 * function is safe because it maps to the same, single
940 * clock class.
941 */
942 ret = bt_ctf_stream_class_common_validate_single_clock_class(
943 stream_class, &expected_clock_class);
944 if (ret) {
945 BT_LOGW("Invalid parameter: stream class or one of its "
946 "event classes contains a field type which is "
947 "not recursively mapped to the expected "
948 "clock class: "
949 "stream-class-addr=%p, "
950 "stream-class-id=%" PRId64 ", "
951 "stream-class-name=\"%s\", "
952 "expected-clock-class-addr=%p, "
953 "expected-clock-class-name=\"%s\"",
954 stream_class, bt_ctf_stream_class_common_get_id(stream_class),
955 bt_ctf_stream_class_common_get_name(stream_class),
956 expected_clock_class,
957 expected_clock_class ?
958 bt_ctf_clock_class_get_name(expected_clock_class) :
959 NULL);
960 goto end;
961 }
962 }
963
964 ret = check_packet_header_type_has_no_clock_class(trace);
965 if (ret) {
966 /* check_packet_header_type_has_no_clock_class() logs errors */
967 goto end;
968 }
969
970 /*
971 * We're about to freeze both the trace and the stream class.
972 * Also, each event class contained in this stream class are
973 * already frozen.
974 *
975 * This trace, this stream class, and all its event classes
976 * should be valid at this point.
977 *
978 * Validate trace and stream class first, then each event
979 * class of this stream class can be validated individually.
980 */
981 packet_header_type =
982 bt_ctf_trace_common_borrow_packet_header_field_type(trace);
983 packet_context_type =
984 bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class);
985 event_header_type =
986 bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class);
987 stream_event_ctx_type =
988 bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class);
989
990 BT_LOGD("Validating trace and stream class field types.");
991 ret = bt_ctf_validate_class_types(trace->environment,
992 packet_header_type, packet_context_type, event_header_type,
993 stream_event_ctx_type, NULL, NULL, trace->valid,
994 stream_class->valid, 1, &trace_sc_validation_output,
995 trace_sc_validation_flags, copy_field_type_func);
996
997 if (ret) {
998 /*
999 * This means something went wrong during the validation
1000 * process, not that the objects are invalid.
1001 */
1002 BT_LOGE("Failed to validate trace and stream class field types: "
1003 "ret=%d", ret);
1004 goto end;
1005 }
1006
1007 if ((trace_sc_validation_output.valid_flags &
1008 trace_sc_validation_flags) !=
1009 trace_sc_validation_flags) {
1010 /* Invalid trace/stream class */
1011 BT_LOGW("Invalid trace or stream class field types: "
1012 "valid-flags=0x%x",
1013 trace_sc_validation_output.valid_flags);
1014 ret = -1;
1015 goto end;
1016 }
1017
1018 if (event_class_count > 0) {
1019 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
1020 event_class_count);
1021 if (!ec_validation_outputs) {
1022 BT_LOGE_STR("Failed to allocate one validation output structure.");
1023 ret = -1;
1024 goto end;
1025 }
1026 }
1027
1028 /* Validate each event class individually */
1029 for (i = 0; i < event_class_count; i++) {
1030 struct bt_ctf_event_class_common *event_class =
1031 bt_ctf_stream_class_common_borrow_event_class_by_index(
1032 stream_class, i);
1033 struct bt_ctf_field_type_common *event_context_type = NULL;
1034 struct bt_ctf_field_type_common *event_payload_type = NULL;
1035
1036 event_context_type =
1037 bt_ctf_event_class_common_borrow_context_field_type(
1038 event_class);
1039 event_payload_type =
1040 bt_ctf_event_class_common_borrow_payload_field_type(
1041 event_class);
1042
1043 /*
1044 * It is important to use the field types returned by
1045 * the previous trace and stream class validation here
1046 * because copies could have been made.
1047 */
1048 BT_LOGD("Validating event class's field types: "
1049 "addr=%p, name=\"%s\", id=%" PRId64,
1050 event_class, bt_ctf_event_class_common_get_name(event_class),
1051 bt_ctf_event_class_common_get_id(event_class));
1052 ret = bt_ctf_validate_class_types(trace->environment,
1053 trace_sc_validation_output.packet_header_type,
1054 trace_sc_validation_output.packet_context_type,
1055 trace_sc_validation_output.event_header_type,
1056 trace_sc_validation_output.stream_event_ctx_type,
1057 event_context_type, event_payload_type,
1058 1, 1, event_class->valid, &ec_validation_outputs[i],
1059 ec_validation_flags, copy_field_type_func);
1060
1061 if (ret) {
1062 BT_LOGE("Failed to validate event class field types: "
1063 "ret=%d", ret);
1064 goto end;
1065 }
1066
1067 if ((ec_validation_outputs[i].valid_flags &
1068 ec_validation_flags) != ec_validation_flags) {
1069 /* Invalid event class */
1070 BT_LOGW("Invalid event class field types: "
1071 "valid-flags=0x%x",
1072 ec_validation_outputs[i].valid_flags);
1073 ret = -1;
1074 goto end;
1075 }
1076 }
1077
1078 stream_id = bt_ctf_stream_class_common_get_id(stream_class);
1079 if (stream_id < 0) {
1080 stream_id = trace->next_stream_id++;
1081 if (stream_id < 0) {
1082 BT_LOGE_STR("No more stream class IDs available.");
1083 ret = -1;
1084 goto end;
1085 }
1086
1087 /* Try to assign a new stream id */
1088 for (i = 0; i < trace->stream_classes->len; i++) {
1089 if (stream_id == bt_ctf_stream_class_common_get_id(
1090 trace->stream_classes->pdata[i])) {
1091 /* Duplicate stream id found */
1092 BT_LOGW("Duplicate stream class ID: "
1093 "id=%" PRId64, (int64_t) stream_id);
1094 ret = -1;
1095 goto end;
1096 }
1097 }
1098
1099 if (bt_ctf_stream_class_common_set_id_no_check(stream_class,
1100 stream_id)) {
1101 /* TODO Should retry with a different stream id */
1102 BT_LOGE("Cannot set stream class's ID: "
1103 "id=%" PRId64, (int64_t) stream_id);
1104 ret = -1;
1105 goto end;
1106 }
1107 }
1108
1109 /*
1110 * At this point all the field types in the validation output
1111 * are valid. Validate the semantics of some scopes according to
1112 * the CTF specification.
1113 */
1114 if (!packet_header_field_type_is_valid(trace,
1115 trace_sc_validation_output.packet_header_type)) {
1116 BT_LOGW_STR("Invalid trace's packet header field type.");
1117 ret = -1;
1118 goto end;
1119 }
1120
1121 if (!packet_context_field_type_is_valid(trace,
1122 stream_class,
1123 trace_sc_validation_output.packet_context_type,
1124 check_ts_begin_end_mapped)) {
1125 BT_LOGW_STR("Invalid stream class's packet context field type.");
1126 ret = -1;
1127 goto end;
1128 }
1129
1130 if (!event_header_field_type_is_valid(trace,
1131 stream_class,
1132 trace_sc_validation_output.event_header_type)) {
1133 BT_LOGW_STR("Invalid steam class's event header field type.");
1134 ret = -1;
1135 goto end;
1136 }
1137
1138 /*
1139 * Now is the time to automatically map specific field types of
1140 * the stream class's packet context and event header field
1141 * types to the stream class's clock's class if they are not
1142 * mapped to a clock class yet. We do it here because we know
1143 * that after this point, everything is frozen so it won't be
1144 * possible for the user to modify the stream class's clock, or
1145 * to map those field types to other clock classes.
1146 */
1147 if (map_clock_classes_func) {
1148 if (map_clock_classes_func(stream_class,
1149 trace_sc_validation_output.packet_context_type,
1150 trace_sc_validation_output.event_header_type)) {
1151 /* map_clock_classes_func() logs errors */
1152 ret = -1;
1153 goto end;
1154 }
1155 }
1156
1157 bt_ctf_object_set_parent(&stream_class->base, &trace->base);
1158 g_ptr_array_add(trace->stream_classes, stream_class);
1159
1160 /*
1161 * At this point we know that the function will be successful.
1162 * Therefore we can replace the trace and stream class field
1163 * types with what's in their validation output structure and
1164 * mark them as valid. We can also replace the field types of
1165 * all the event classes of the stream class and mark them as
1166 * valid.
1167 */
1168 bt_ctf_validation_replace_types(trace, stream_class, NULL,
1169 &trace_sc_validation_output, trace_sc_validation_flags);
1170 trace->valid = 1;
1171 stream_class->valid = 1;
1172
1173 /*
1174 * Put what was not moved in bt_ctf_validation_replace_types().
1175 */
1176 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
1177
1178 for (i = 0; i < event_class_count; i++) {
1179 struct bt_ctf_event_class_common *event_class =
1180 bt_ctf_stream_class_common_borrow_event_class_by_index(
1181 stream_class, i);
1182
1183 bt_ctf_validation_replace_types(NULL, NULL, event_class,
1184 &ec_validation_outputs[i], ec_validation_flags);
1185 event_class->valid = 1;
1186
1187 /*
1188 * Put what was not moved in
1189 * bt_ctf_validation_replace_types().
1190 */
1191 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
1192 }
1193
1194 /*
1195 * Freeze the trace and the stream class.
1196 */
1197 bt_ctf_stream_class_common_freeze(stream_class);
1198 bt_ctf_trace_common_freeze(trace);
1199
1200 /*
1201 * It is safe to set the stream class's unique clock class
1202 * now because the stream class is frozen.
1203 */
1204 if (expected_clock_class) {
1205 BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class);
1206 }
1207
1208 BT_LOGD("Added stream class to trace: "
1209 "trace-addr=%p, trace-name=\"%s\", "
1210 "stream-class-addr=%p, stream-class-name=\"%s\", "
1211 "stream-class-id=%" PRId64,
1212 trace, bt_ctf_trace_common_get_name(trace),
1213 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
1214 bt_ctf_stream_class_common_get_id(stream_class));
1215
1216 end:
1217 if (ret) {
1218 if (stream_class) {
1219 bt_ctf_object_set_parent(&stream_class->base, NULL);
1220 }
1221
1222 if (ec_validation_outputs) {
1223 for (i = 0; i < event_class_count; i++) {
1224 bt_ctf_validation_output_put_types(
1225 &ec_validation_outputs[i]);
1226 }
1227 }
1228 }
1229
1230 g_free(ec_validation_outputs);
1231 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
1232 bt_ctf_object_put_ref(expected_clock_class);
1233 return ret;
1234 }
1235
1236 bt_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace,
1237 struct bt_ctf_clock_class *clock_class)
1238 {
1239 struct bt_ctf_search_query query = { .value = clock_class, .found = 0 };
1240
1241 BT_ASSERT_DBG(trace);
1242 BT_ASSERT_DBG(clock_class);
1243
1244 g_ptr_array_foreach(trace->clock_classes, value_exists, &query);
1245 return query.found;
1246 }
1247
1248 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common *trace,
1249 enum bt_ctf_byte_order byte_order, bool allow_unspecified)
1250 {
1251 int ret = 0;
1252
1253 if (!trace) {
1254 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1255 ret = -1;
1256 goto end;
1257 }
1258
1259 if (trace->frozen) {
1260 BT_LOGW("Invalid parameter: trace is frozen: "
1261 "addr=%p, name=\"%s\"",
1262 trace, bt_ctf_trace_common_get_name(trace));
1263 ret = -1;
1264 goto end;
1265 }
1266
1267 if (byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED && !allow_unspecified) {
1268 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
1269 "addr=%p, name=\"%s\"",
1270 trace, bt_ctf_trace_common_get_name(trace));
1271 ret = -1;
1272 goto end;
1273 }
1274
1275 if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
1276 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
1277 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
1278 BT_LOGW("Invalid parameter: invalid byte order: "
1279 "addr=%p, name=\"%s\", bo=%s",
1280 trace, bt_ctf_trace_common_get_name(trace),
1281 bt_ctf_byte_order_string(byte_order));
1282 ret = -1;
1283 goto end;
1284 }
1285
1286 trace->native_byte_order = byte_order;
1287 BT_LOGT("Set trace's native byte order: "
1288 "addr=%p, name=\"%s\", bo=%s",
1289 trace, bt_ctf_trace_common_get_name(trace),
1290 bt_ctf_byte_order_string(byte_order));
1291
1292 end:
1293 return ret;
1294 }
1295
1296 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common *trace,
1297 struct bt_ctf_field_type_common *packet_header_type)
1298 {
1299 int ret = 0;
1300
1301 if (!trace) {
1302 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1303 ret = -1;
1304 goto end;
1305 }
1306
1307 if (trace->frozen) {
1308 BT_LOGW("Invalid parameter: trace is frozen: "
1309 "addr=%p, name=\"%s\"",
1310 trace, bt_ctf_trace_common_get_name(trace));
1311 ret = -1;
1312 goto end;
1313 }
1314
1315 /* packet_header_type must be a structure. */
1316 if (packet_header_type &&
1317 packet_header_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
1318 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1319 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1320 trace, bt_ctf_trace_common_get_name(trace),
1321 packet_header_type,
1322 bt_ctf_field_type_id_string(packet_header_type->id));
1323 ret = -1;
1324 goto end;
1325 }
1326
1327 bt_ctf_object_put_ref(trace->packet_header_field_type);
1328 trace->packet_header_field_type = bt_ctf_object_get_ref(packet_header_type);
1329 BT_LOGT("Set trace's packet header field type: "
1330 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1331 trace, bt_ctf_trace_common_get_name(trace), packet_header_type);
1332 end:
1333 return ret;
1334 }
1335
1336 static
1337 int64_t get_stream_class_count(void *element)
1338 {
1339 return bt_ctf_trace_get_stream_class_count(
1340 (struct bt_ctf_trace *) element);
1341 }
1342
1343 static
1344 void *get_stream_class(void *element, int i)
1345 {
1346 return bt_ctf_trace_get_stream_class_by_index(
1347 (struct bt_ctf_trace *) element, i);
1348 }
1349
1350 static
1351 int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
1352 {
1353 return bt_ctf_stream_class_visit(object, visitor, data);
1354 }
1355
1356 int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
1357 bt_ctf_visitor visitor, void *data)
1358 {
1359 int ret;
1360 struct bt_ctf_visitor_object obj = {
1361 .object = trace,
1362 .type = BT_CTF_VISITOR_OBJECT_TYPE_TRACE
1363 };
1364
1365 if (!trace) {
1366 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1367 ret = -1;
1368 goto end;
1369 }
1370
1371 if (!visitor) {
1372 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
1373 ret = -1;
1374 goto end;
1375 }
1376
1377 BT_LOGT("Visiting trace: addr=%p, name=\"%s\"",
1378 trace, bt_ctf_trace_get_name(trace));
1379 ret = bt_ctf_visitor_helper(&obj, get_stream_class_count,
1380 get_stream_class, visit_stream_class, visitor, data);
1381 end:
1382 return ret;
1383 }
1384
1385 static
1386 void bt_ctf_trace_destroy(struct bt_ctf_object *obj)
1387 {
1388 struct bt_ctf_trace *trace = (void *) obj;
1389
1390 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
1391 trace, bt_ctf_trace_get_name(trace));
1392 bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace));
1393 g_free(trace);
1394 }
1395
1396 struct bt_ctf_trace *bt_ctf_trace_create(void)
1397 {
1398 struct bt_ctf_trace *trace = NULL;
1399 int ret;
1400
1401 BT_LOGD_STR("Creating CTF writer trace object.");
1402 trace = g_new0(struct bt_ctf_trace, 1);
1403 if (!trace) {
1404 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
1405 goto error;
1406 }
1407
1408 ret = bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace),
1409 bt_ctf_trace_destroy);
1410 if (ret) {
1411 /* bt_ctf_trace_common_initialize() logs errors */
1412 goto error;
1413 }
1414
1415 BT_LOGD("Created CTF writer trace object: addr=%p", trace);
1416 return trace;
1417
1418 error:
1419 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace);
1420 return trace;
1421 }
1422
1423 BT_EXPORT
1424 const uint8_t *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace)
1425 {
1426 return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace));
1427 }
1428
1429 BT_EXPORT
1430 int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace,
1431 const uint8_t *uuid)
1432 {
1433 return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace), uuid);
1434 }
1435
1436 BT_EXPORT
1437 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
1438 const char *name, const char *value)
1439 {
1440 return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace),
1441 name, value);
1442 }
1443
1444 BT_EXPORT
1445 int bt_ctf_trace_set_environment_field_integer(
1446 struct bt_ctf_trace *trace, const char *name, int64_t value)
1447 {
1448 return bt_ctf_trace_common_set_environment_field_integer(
1449 BT_CTF_TO_COMMON(trace), name, value);
1450 }
1451
1452 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
1453 {
1454 return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace));
1455 }
1456
1457 const char *
1458 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace,
1459 uint64_t index)
1460 {
1461 return bt_ctf_trace_common_get_environment_field_name_by_index(
1462 BT_CTF_TO_COMMON(trace), index);
1463 }
1464
1465 struct bt_ctf_value *bt_ctf_trace_get_environment_field_value_by_index(
1466 struct bt_ctf_trace *trace, uint64_t index)
1467 {
1468 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index(
1469 BT_CTF_TO_COMMON(trace), index));
1470 }
1471
1472 struct bt_ctf_value *bt_ctf_trace_get_environment_field_value_by_name(
1473 struct bt_ctf_trace *trace, const char *name)
1474 {
1475 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name(
1476 BT_CTF_TO_COMMON(trace), name));
1477 }
1478
1479 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
1480 struct bt_ctf_clock_class *clock_class)
1481 {
1482 return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace),
1483 (void *) clock_class);
1484 }
1485
1486 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
1487 {
1488 return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace));
1489 }
1490
1491 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
1492 struct bt_ctf_trace *trace, uint64_t index)
1493 {
1494 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index(
1495 BT_CTF_TO_COMMON(trace), index));
1496 }
1497
1498 static
1499 int map_clock_classes_func(struct bt_ctf_stream_class_common *stream_class,
1500 struct bt_ctf_field_type_common *packet_context_type,
1501 struct bt_ctf_field_type_common *event_header_type)
1502 {
1503 int ret = bt_ctf_stream_class_map_clock_class(
1504 BT_CTF_FROM_COMMON(stream_class),
1505 BT_CTF_FROM_COMMON(packet_context_type),
1506 BT_CTF_FROM_COMMON(event_header_type));
1507
1508 if (ret) {
1509 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
1510 }
1511
1512 return ret;
1513 }
1514
1515 BT_EXPORT
1516 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
1517 struct bt_ctf_stream_class *stream_class)
1518 {
1519 int ret = 0;
1520 struct bt_ctf_clock_class *expected_clock_class = NULL;
1521
1522 if (!trace) {
1523 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1524 ret = -1;
1525 goto end;
1526 }
1527
1528 if (!stream_class) {
1529 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1530 ret = -1;
1531 goto end;
1532 }
1533
1534 if (stream_class->clock) {
1535 struct bt_ctf_clock_class *stream_clock_class =
1536 stream_class->clock->clock_class;
1537
1538 /*
1539 * Make sure this clock was also added to the
1540 * trace (potentially through its CTF writer
1541 * owner).
1542 */
1543 size_t i;
1544
1545 for (i = 0; i < trace->common.clock_classes->len; i++) {
1546 if (trace->common.clock_classes->pdata[i] ==
1547 stream_clock_class) {
1548 /* Found! */
1549 break;
1550 }
1551 }
1552
1553 if (i == trace->common.clock_classes->len) {
1554 /* Not found */
1555 BT_LOGW("Stream class's clock's class is not part of the trace: "
1556 "clock-class-addr=%p, clock-class-name=\"%s\"",
1557 stream_clock_class,
1558 bt_ctf_clock_class_get_name(stream_clock_class));
1559 ret = -1;
1560 goto end;
1561 }
1562
1563 if (stream_class->common.clock_class &&
1564 stream_class->common.clock_class !=
1565 stream_class->clock->clock_class) {
1566 /*
1567 * Stream class already has an expected clock
1568 * class, but it does not match its clock's
1569 * class.
1570 */
1571 BT_LOGW("Invalid parameter: stream class's clock's "
1572 "class does not match stream class's "
1573 "expected clock class: "
1574 "stream-class-addr=%p, "
1575 "stream-class-id=%" PRId64 ", "
1576 "stream-class-name=\"%s\", "
1577 "expected-clock-class-addr=%p, "
1578 "expected-clock-class-name=\"%s\"",
1579 stream_class,
1580 bt_ctf_stream_class_get_id(stream_class),
1581 bt_ctf_stream_class_get_name(stream_class),
1582 stream_class->common.clock_class,
1583 bt_ctf_clock_class_get_name(stream_class->common.clock_class));
1584 } else if (!stream_class->common.clock_class) {
1585 /*
1586 * Set expected clock class to stream class's
1587 * clock's class.
1588 */
1589 expected_clock_class = stream_class->clock->clock_class;
1590 }
1591 }
1592
1593
1594 ret = bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace),
1595 BT_CTF_TO_COMMON(stream_class),
1596 (bt_ctf_validation_flag_copy_field_type_func) bt_ctf_field_type_copy,
1597 expected_clock_class, map_clock_classes_func,
1598 false);
1599
1600 end:
1601 return ret;
1602 }
1603
1604 BT_EXPORT
1605 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
1606 {
1607 return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace));
1608 }
1609
1610 BT_EXPORT
1611 struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index(
1612 struct bt_ctf_trace *trace, uint64_t index)
1613 {
1614 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index(
1615 BT_CTF_TO_COMMON(trace), index));
1616 }
1617
1618 BT_EXPORT
1619 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
1620 {
1621 return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace));
1622 }
1623
1624 BT_EXPORT
1625 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index(
1626 struct bt_ctf_trace *trace, uint64_t index)
1627 {
1628 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index(
1629 BT_CTF_TO_COMMON(trace), index));
1630 }
1631
1632 BT_EXPORT
1633 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
1634 struct bt_ctf_trace *trace, uint64_t id)
1635 {
1636 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id(
1637 BT_CTF_TO_COMMON(trace), id));
1638 }
1639
1640 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
1641 struct bt_ctf_trace *trace, const char *name)
1642 {
1643 return bt_ctf_object_get_ref(
1644 bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace),
1645 name));
1646 }
1647
1648 static
1649 int append_trace_metadata(struct bt_ctf_trace *trace,
1650 struct metadata_context *context)
1651 {
1652 uint8_t *uuid = trace->common.uuid;
1653 int ret = 0;
1654
1655 if (trace->common.native_byte_order == BT_CTF_BYTE_ORDER_NATIVE ||
1656 trace->common.native_byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
1657 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
1658 "set it with bt_ctf_trace_set_native_byte_order(): "
1659 "addr=%p, name=\"%s\"",
1660 trace, bt_ctf_trace_get_name(trace));
1661 ret = -1;
1662 goto end;
1663 }
1664
1665 g_string_append(context->string, "trace {\n");
1666 g_string_append(context->string, "\tmajor = 1;\n");
1667 g_string_append(context->string, "\tminor = 8;\n");
1668 BT_ASSERT_DBG(trace->common.native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
1669 trace->common.native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
1670 trace->common.native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
1671
1672 if (trace->common.uuid_set) {
1673 g_string_append_printf(context->string,
1674 "\tuuid = \"" BT_UUID_FMT "\";\n",
1675 BT_UUID_FMT_VALUES(uuid));
1676 }
1677
1678 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
1679 bt_ctf_get_byte_order_string(trace->common.native_byte_order));
1680
1681 if (trace->common.packet_header_field_type) {
1682 g_string_append(context->string, "\tpacket.header := ");
1683 context->current_indentation_level++;
1684 g_string_assign(context->field_name, "");
1685 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
1686 ret = bt_ctf_field_type_serialize_recursive(
1687 (void *) trace->common.packet_header_field_type,
1688 context);
1689 if (ret) {
1690 goto end;
1691 }
1692 context->current_indentation_level--;
1693 }
1694
1695 g_string_append(context->string, ";\n};\n\n");
1696 end:
1697 return ret;
1698 }
1699
1700 static
1701 void append_env_metadata(struct bt_ctf_trace *trace,
1702 struct metadata_context *context)
1703 {
1704 int64_t i;
1705 int64_t env_size;
1706
1707 env_size = bt_ctf_attributes_get_count(trace->common.environment);
1708 if (env_size <= 0) {
1709 return;
1710 }
1711
1712 g_string_append(context->string, "env {\n");
1713
1714 for (i = 0; i < env_size; i++) {
1715 struct bt_ctf_private_value *env_field_value_obj = NULL;
1716 const char *entry_name;
1717
1718 entry_name = bt_ctf_attributes_get_field_name(
1719 trace->common.environment, i);
1720 env_field_value_obj = bt_ctf_attributes_borrow_field_value(
1721 trace->common.environment, i);
1722
1723 BT_ASSERT_DBG(entry_name);
1724 BT_ASSERT_DBG(env_field_value_obj);
1725
1726 switch (bt_ctf_value_get_type(
1727 bt_ctf_private_value_as_value(env_field_value_obj))) {
1728 case BT_CTF_VALUE_TYPE_INTEGER:
1729 {
1730 int64_t int_value;
1731
1732 int_value = bt_ctf_value_integer_get(
1733 bt_ctf_private_value_as_value(
1734 env_field_value_obj));
1735 g_string_append_printf(context->string,
1736 "\t%s = %" PRId64 ";\n", entry_name,
1737 int_value);
1738 break;
1739 }
1740 case BT_CTF_VALUE_TYPE_STRING:
1741 {
1742 const char *str_value;
1743 char *escaped_str = NULL;
1744
1745 str_value = bt_ctf_value_string_get(
1746 bt_ctf_private_value_as_value(
1747 env_field_value_obj));
1748 escaped_str = g_strescape(str_value, NULL);
1749 if (!escaped_str) {
1750 BT_LOGE("Cannot escape string: string=\"%s\"",
1751 str_value);
1752 continue;
1753 }
1754
1755 g_string_append_printf(context->string,
1756 "\t%s = \"%s\";\n", entry_name, escaped_str);
1757 free(escaped_str);
1758 break;
1759 }
1760 default:
1761 continue;
1762 }
1763 }
1764
1765 g_string_append(context->string, "};\n\n");
1766 }
1767
1768 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
1769 {
1770 char *metadata = NULL;
1771 struct metadata_context *context = NULL;
1772 int err = 0;
1773 size_t i;
1774
1775 if (!trace) {
1776 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1777 goto end;
1778 }
1779
1780 context = g_new0(struct metadata_context, 1);
1781 if (!context) {
1782 BT_LOGE_STR("Failed to allocate one metadata context.");
1783 goto end;
1784 }
1785
1786 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
1787 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
1788 g_string_append(context->string, "/* CTF 1.8 */\n\n");
1789 if (append_trace_metadata(trace, context)) {
1790 /* append_trace_metadata() logs errors */
1791 goto error;
1792 }
1793 append_env_metadata(trace, context);
1794 g_ptr_array_foreach(trace->common.clock_classes,
1795 (GFunc) bt_ctf_clock_class_serialize, context);
1796
1797 for (i = 0; i < trace->common.stream_classes->len; i++) {
1798 /* bt_ctf_stream_class_serialize() logs details */
1799 err = bt_ctf_stream_class_serialize(
1800 trace->common.stream_classes->pdata[i], context);
1801 if (err) {
1802 /* bt_ctf_stream_class_serialize() logs errors */
1803 goto error;
1804 }
1805 }
1806
1807 metadata = context->string->str;
1808
1809 error:
1810 g_string_free(context->string, err ? TRUE : FALSE);
1811 g_string_free(context->field_name, TRUE);
1812 g_free(context);
1813
1814 end:
1815 return metadata;
1816 }
1817
1818 BT_EXPORT
1819 enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order(
1820 struct bt_ctf_trace *trace)
1821 {
1822 return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace));
1823 }
1824
1825 BT_EXPORT
1826 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
1827 enum bt_ctf_byte_order byte_order)
1828 {
1829 return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace),
1830 (int) byte_order, false);
1831 }
1832
1833 BT_EXPORT
1834 struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_field_type(
1835 struct bt_ctf_trace *trace)
1836 {
1837 return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type(
1838 BT_CTF_TO_COMMON(trace)));
1839 }
1840
1841 BT_EXPORT
1842 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace *trace,
1843 struct bt_ctf_field_type *packet_header_type)
1844 {
1845 return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace),
1846 (void *) packet_header_type);
1847 }
1848
1849 BT_EXPORT
1850 const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
1851 {
1852 return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace));
1853 }
This page took 0.097615 seconds and 4 git commands to generate.