Logging: standardize logging tags
[babeltrace.git] / src / ctf-writer / stream.c
1 /*
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "CTF-WRITER/STREAM"
25 #include "logging.h"
26
27 #include <inttypes.h>
28 #include <stdint.h>
29 #include <unistd.h>
30
31 #include <babeltrace2/ctf-writer/field-types.h>
32 #include <babeltrace2/ctf-writer/object.h>
33 #include <babeltrace2/ctf-writer/stream-class.h>
34 #include <babeltrace2/ctf-writer/stream.h>
35 #include <babeltrace2/ctf-writer/trace.h>
36
37 #include "common/align.h"
38 #include "common/assert.h"
39 #include "compat/compiler.h"
40 #include "ctfser/ctfser.h"
41
42 #include "assert-pre.h"
43 #include "event-class.h"
44 #include "event.h"
45 #include "fields.h"
46 #include "stream-class.h"
47 #include "stream.h"
48 #include "trace.h"
49 #include "writer.h"
50
51 BT_HIDDEN
52 void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common *stream)
53 {
54 BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"",
55 stream, bt_ctf_stream_common_get_name(stream));
56
57 if (stream->name) {
58 g_string_free(stream->name, TRUE);
59 }
60 }
61
62 BT_HIDDEN
63 int bt_ctf_stream_common_initialize(
64 struct bt_ctf_stream_common *stream,
65 struct bt_ctf_stream_class_common *stream_class, const char *name,
66 uint64_t id, bt_ctf_object_release_func release_func)
67 {
68 int ret = 0;
69 struct bt_ctf_trace_common *trace = NULL;
70
71 bt_ctf_object_init_shared_with_parent(&stream->base, release_func);
72
73 if (!stream_class) {
74 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
75 goto error;
76 }
77
78 BT_LOGD("Initializing common stream object: stream-class-addr=%p, "
79 "stream-class-name=\"%s\", stream-name=\"%s\", "
80 "stream-id=%" PRIu64,
81 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
82 name, id);
83 trace = bt_ctf_stream_class_common_borrow_trace(stream_class);
84 if (!trace) {
85 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
86 "stream-class-addr=%p, stream-class-name=\"%s\", "
87 "stream-name=\"%s\"",
88 stream_class,
89 bt_ctf_stream_class_common_get_name(stream_class), name);
90 goto error;
91 }
92
93 if (id != -1ULL) {
94 /*
95 * Validate that the given ID is unique amongst all the
96 * existing trace's streams created from the same stream
97 * class.
98 */
99 size_t i;
100
101 for (i = 0; i < trace->streams->len; i++) {
102 struct bt_ctf_stream_common *trace_stream =
103 g_ptr_array_index(trace->streams, i);
104
105 if (trace_stream->stream_class != (void *) stream_class) {
106 continue;
107 }
108
109 if (trace_stream->id == id) {
110 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
111 goto error;
112 }
113 }
114 }
115
116 /*
117 * Acquire reference to parent since stream will become publicly
118 * reachable; it needs its parent to remain valid.
119 */
120 bt_ctf_object_set_parent(&stream->base, &trace->base);
121 stream->stream_class = stream_class;
122 stream->id = (int64_t) id;
123
124 if (name) {
125 stream->name = g_string_new(name);
126 if (!stream->name) {
127 BT_LOGE_STR("Failed to allocate a GString.");
128 goto error;
129 }
130 }
131
132 BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace);
133
134 /* Add this stream to the trace's streams */
135 BT_LOGD("Created common stream object: addr=%p", stream);
136 goto end;
137
138 error:
139 ret = -1;
140
141 end:
142 return ret;
143 }
144
145 static
146 void bt_ctf_stream_destroy(struct bt_ctf_object *obj);
147
148 static
149 int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
150
151 static
152 int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
153 {
154 int ret = 0;
155 struct bt_ctf_field_type *field_type = NULL;
156
157 if (!field) {
158 BT_LOGW_STR("Invalid parameter: field is NULL.");
159 ret = -1;
160 goto end;
161 }
162
163 field_type = bt_ctf_field_get_type(field);
164 BT_ASSERT(field_type);
165
166 if (bt_ctf_field_type_get_type_id(field_type) !=
167 BT_CTF_FIELD_TYPE_ID_INTEGER) {
168 /* Not an integer and the value is unset, error. */
169 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
170 "field-addr=%p, ft-addr=%p, ft-id=%s",
171 field, field_type,
172 bt_ctf_field_type_id_string((int)
173 bt_ctf_field_type_get_type_id(field_type)));
174 ret = -1;
175 goto end;
176 }
177
178 if (bt_ctf_field_type_integer_is_signed(field_type)) {
179 ret = bt_ctf_field_integer_signed_set_value(field, (int64_t) value);
180 if (ret) {
181 /* Value is out of range, error. */
182 BT_LOGW("Cannot set signed integer field's value: "
183 "addr=%p, value=%" PRId64,
184 field, (int64_t) value);
185 goto end;
186 }
187 } else {
188 ret = bt_ctf_field_integer_unsigned_set_value(field, value);
189 if (ret) {
190 /* Value is out of range, error. */
191 BT_LOGW("Cannot set unsigned integer field's value: "
192 "addr=%p, value=%" PRIu64,
193 field, value);
194 goto end;
195 }
196 }
197 end:
198 bt_ctf_object_put_ref(field_type);
199 return ret;
200 }
201
202 static
203 int set_packet_header_magic(struct bt_ctf_stream *stream)
204 {
205 int ret = 0;
206 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field_by_name(
207 stream->packet_header, "magic");
208 const uint32_t magic_value = 0xc1fc1fc1;
209
210 BT_ASSERT(stream);
211
212 if (!magic_field) {
213 /* No magic field found. Not an error, skip. */
214 BT_LOGV("No field named `magic` in packet header: skipping: "
215 "stream-addr=%p, stream-name=\"%s\"",
216 stream, bt_ctf_stream_get_name(stream));
217 goto end;
218 }
219
220 ret = bt_ctf_field_integer_unsigned_set_value(magic_field,
221 (uint64_t) magic_value);
222
223 if (ret) {
224 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
225 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
226 stream, bt_ctf_stream_get_name(stream),
227 magic_field, (uint64_t) magic_value);
228 } else {
229 BT_LOGV("Set packet header field's `magic` field's value: "
230 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
231 stream, bt_ctf_stream_get_name(stream),
232 magic_field, (uint64_t) magic_value);
233 }
234 end:
235 bt_ctf_object_put_ref(magic_field);
236 return ret;
237 }
238
239 static
240 int set_packet_header_uuid(struct bt_ctf_stream *stream)
241 {
242 int ret = 0;
243 int64_t i;
244 struct bt_ctf_trace *trace = NULL;
245 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field_by_name(
246 stream->packet_header, "uuid");
247
248 BT_ASSERT(stream);
249
250 if (!uuid_field) {
251 /* No uuid field found. Not an error, skip. */
252 BT_LOGV("No field named `uuid` in packet header: skipping: "
253 "stream-addr=%p, stream-name=\"%s\"",
254 stream, bt_ctf_stream_get_name(stream));
255 goto end;
256 }
257
258 trace = (struct bt_ctf_trace *)
259 bt_ctf_object_get_parent(&stream->common.base);
260
261 for (i = 0; i < 16; i++) {
262 struct bt_ctf_field *uuid_element =
263 bt_ctf_field_array_get_field(uuid_field, i);
264
265 ret = bt_ctf_field_integer_unsigned_set_value(
266 uuid_element, (uint64_t) trace->common.uuid[i]);
267 bt_ctf_object_put_ref(uuid_element);
268 if (ret) {
269 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
270 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
271 "value=%" PRIu64 ", index=%" PRId64,
272 stream, bt_ctf_stream_get_name(stream),
273 uuid_element, (uint64_t) trace->common.uuid[i], i);
274 goto end;
275 }
276 }
277
278 BT_LOGV("Set packet header field's `uuid` field's value: "
279 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
280 stream, bt_ctf_stream_get_name(stream), uuid_field);
281
282 end:
283 bt_ctf_object_put_ref(uuid_field);
284 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace);
285 return ret;
286 }
287 static
288 int set_packet_header_stream_id(struct bt_ctf_stream *stream)
289 {
290 int ret = 0;
291 uint32_t stream_id;
292 struct bt_ctf_field *stream_id_field =
293 bt_ctf_field_structure_get_field_by_name(
294 stream->packet_header, "stream_id");
295
296 if (!stream_id_field) {
297 /* No stream_id field found. Not an error, skip. */
298 BT_LOGV("No field named `stream_id` in packet header: skipping: "
299 "stream-addr=%p, stream-name=\"%s\"",
300 stream, bt_ctf_stream_get_name(stream));
301 goto end;
302 }
303
304 stream_id = stream->common.stream_class->id;
305 ret = bt_ctf_field_integer_unsigned_set_value(stream_id_field,
306 (uint64_t) stream_id);
307 if (ret) {
308 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
309 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
310 stream, bt_ctf_stream_get_name(stream),
311 stream_id_field, (uint64_t) stream_id);
312 } else {
313 BT_LOGV("Set packet header field's `stream_id` field's value: "
314 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
315 stream, bt_ctf_stream_get_name(stream),
316 stream_id_field, (uint64_t) stream_id);
317 }
318
319 end:
320 bt_ctf_object_put_ref(stream_id_field);
321 return ret;
322 }
323
324 static
325 int auto_populate_packet_header(struct bt_ctf_stream *stream)
326 {
327 int ret = 0;
328
329 if (!stream->packet_header) {
330 goto end;
331 }
332
333 ret = set_packet_header_magic(stream);
334 if (ret) {
335 BT_LOGW("Cannot set packet header's magic number field: "
336 "stream-addr=%p, stream-name=\"%s\"",
337 stream, bt_ctf_stream_get_name(stream));
338 goto end;
339 }
340
341 ret = set_packet_header_uuid(stream);
342 if (ret) {
343 BT_LOGW("Cannot set packet header's UUID field: "
344 "stream-addr=%p, stream-name=\"%s\"",
345 stream, bt_ctf_stream_get_name(stream));
346 goto end;
347 }
348
349 ret = set_packet_header_stream_id(stream);
350 if (ret) {
351 BT_LOGW("Cannot set packet header's stream class ID field: "
352 "stream-addr=%p, stream-name=\"%s\"",
353 stream, bt_ctf_stream_get_name(stream));
354 goto end;
355 }
356
357 BT_LOGV("Automatically populated stream's packet header's known fields: "
358 "stream-addr=%p, stream-name=\"%s\"",
359 stream, bt_ctf_stream_get_name(stream));
360
361 end:
362 return ret;
363 }
364
365 static
366 int set_packet_context_packet_size(struct bt_ctf_stream *stream,
367 uint64_t packet_size_bits)
368 {
369 int ret = 0;
370 struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name(
371 stream->packet_context, "packet_size");
372
373 ret = bt_ctf_field_integer_unsigned_set_value(field, packet_size_bits);
374 if (ret) {
375 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
376 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
377 stream, bt_ctf_stream_get_name(stream),
378 field, packet_size_bits);
379 } else {
380 BT_LOGV("Set packet context field's `packet_size` field's value: "
381 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
382 stream, bt_ctf_stream_get_name(stream),
383 field, packet_size_bits);
384 }
385
386 bt_ctf_object_put_ref(field);
387 return ret;
388 }
389
390 static
391 int set_packet_context_content_size(struct bt_ctf_stream *stream,
392 uint64_t content_size_bits)
393 {
394 int ret = 0;
395 struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name(
396 stream->packet_context, "content_size");
397
398 BT_ASSERT(stream);
399
400 if (!field) {
401 /* No content size field found. Not an error, skip. */
402 BT_LOGV("No field named `content_size` in packet context: skipping: "
403 "stream-addr=%p, stream-name=\"%s\"",
404 stream, bt_ctf_stream_get_name(stream));
405 goto end;
406 }
407
408 ret = bt_ctf_field_integer_unsigned_set_value(field, content_size_bits);
409 if (ret) {
410 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
411 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
412 stream, bt_ctf_stream_get_name(stream),
413 field, content_size_bits);
414 } else {
415 BT_LOGV("Set packet context field's `content_size` field's value: "
416 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
417 stream, bt_ctf_stream_get_name(stream),
418 field, content_size_bits);
419 }
420
421 end:
422 bt_ctf_object_put_ref(field);
423 return ret;
424 }
425
426 static
427 int set_packet_context_events_discarded(struct bt_ctf_stream *stream)
428 {
429 int ret = 0;
430 struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name(
431 stream->packet_context, "events_discarded");
432
433 BT_ASSERT(stream);
434
435 if (!field) {
436 /* No discarded events count field found. Not an error, skip. */
437 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
438 "stream-addr=%p, stream-name=\"%s\"",
439 stream, bt_ctf_stream_get_name(stream));
440 goto end;
441 }
442
443 /*
444 * If the field is set by the user, make sure that the value is
445 * greater than or equal to the stream's current count of
446 * discarded events. We do not allow wrapping here. If it's
447 * valid, update the stream's current count.
448 */
449 if (bt_ctf_field_is_set_recursive(field)) {
450 uint64_t user_val;
451
452 ret = bt_ctf_field_integer_unsigned_get_value(field,
453 &user_val);
454 if (ret) {
455 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
456 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
457 stream, bt_ctf_stream_get_name(stream), field);
458 goto end;
459 }
460
461 if (user_val < stream->discarded_events) {
462 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
463 "value is lesser than the stream's current discarded events count: "
464 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
465 "value=%" PRIu64 ", "
466 "stream-discarded-events-count=%" PRIu64,
467 stream, bt_ctf_stream_get_name(stream), field,
468 user_val, stream->discarded_events);
469 goto end;
470 }
471
472 stream->discarded_events = user_val;
473 } else {
474 ret = bt_ctf_field_integer_unsigned_set_value(field,
475 stream->discarded_events);
476 if (ret) {
477 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
478 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
479 stream, bt_ctf_stream_get_name(stream),
480 field, stream->discarded_events);
481 } else {
482 BT_LOGV("Set packet context field's `events_discarded` field's value: "
483 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
484 stream, bt_ctf_stream_get_name(stream),
485 field, stream->discarded_events);
486 }
487 }
488
489 end:
490 bt_ctf_object_put_ref(field);
491 return ret;
492 }
493
494 static
495 void update_clock_value(uint64_t *val, uint64_t new_val,
496 unsigned int new_val_size)
497 {
498 const uint64_t pow2 = 1ULL << new_val_size;
499 const uint64_t mask = pow2 - 1;
500 uint64_t val_masked;
501
502 #ifdef BT_LOG_ENABLED_VERBOSE
503 uint64_t old_val = *val;
504 #endif
505
506 if (new_val_size == 64) {
507 *val = new_val;
508 goto end;
509 }
510
511 val_masked = *val & mask;
512
513 if (new_val < val_masked) {
514 /* Wrapped once */
515 new_val |= pow2;
516 }
517
518 *val &= ~mask;
519 *val |= new_val;
520
521 end:
522 BT_LOGV("Updated clock value: old-val=%" PRIu64 ", new-val=%" PRIu64,
523 old_val, *val);
524 return;
525 }
526
527 static
528 int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
529 {
530 int ret = 0;
531 struct bt_ctf_field_common *field_common = (void *) field;
532
533 if (!field) {
534 goto end;
535 }
536
537 switch (bt_ctf_field_get_type_id(field)) {
538 case BT_CTF_FIELD_TYPE_ID_INTEGER:
539 {
540 struct bt_ctf_clock_class *cc =
541 bt_ctf_field_type_integer_get_mapped_clock_class(
542 (void *) field_common->type);
543 int val_size;
544 uint64_t uval;
545
546 if (!cc) {
547 goto end;
548 }
549
550 bt_ctf_object_put_ref(cc);
551 val_size = bt_ctf_field_type_integer_get_size(
552 (void *) field_common->type);
553 BT_ASSERT(val_size >= 1);
554
555 if (bt_ctf_field_type_integer_is_signed(
556 (void *) field_common->type)) {
557 int64_t ival;
558
559 ret = bt_ctf_field_integer_signed_get_value(field, &ival);
560 uval = (uint64_t) ival;
561 } else {
562 ret = bt_ctf_field_integer_unsigned_get_value(field, &uval);
563 }
564
565 if (ret) {
566 /* Not set */
567 goto end;
568 }
569
570 update_clock_value(val, uval, val_size);
571 break;
572 }
573 case BT_CTF_FIELD_TYPE_ID_ENUM:
574 {
575 struct bt_ctf_field *int_field =
576 bt_ctf_field_enumeration_get_container(field);
577
578 BT_ASSERT(int_field);
579 ret = visit_field_update_clock_value(int_field, val);
580 bt_ctf_object_put_ref(int_field);
581 break;
582 }
583 case BT_CTF_FIELD_TYPE_ID_ARRAY:
584 {
585 uint64_t i;
586 int64_t len = bt_ctf_field_type_array_get_length(
587 (void *) field_common->type);
588
589 BT_ASSERT(len >= 0);
590
591 for (i = 0; i < len; i++) {
592 struct bt_ctf_field *elem_field =
593 bt_ctf_field_array_get_field(field, i);
594
595 BT_ASSERT(elem_field);
596 ret = visit_field_update_clock_value(elem_field, val);
597 bt_ctf_object_put_ref(elem_field);
598 if (ret) {
599 goto end;
600 }
601 }
602 break;
603 }
604 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
605 {
606 uint64_t i;
607 int64_t len = bt_ctf_field_common_sequence_get_length(
608 (void *) field);
609
610 if (len < 0) {
611 ret = -1;
612 goto end;
613 }
614
615 for (i = 0; i < len; i++) {
616 struct bt_ctf_field *elem_field =
617 bt_ctf_field_sequence_get_field(field, i);
618
619 BT_ASSERT(elem_field);
620 ret = visit_field_update_clock_value(elem_field, val);
621 bt_ctf_object_put_ref(elem_field);
622 if (ret) {
623 goto end;
624 }
625 }
626 break;
627 }
628 case BT_CTF_FIELD_TYPE_ID_STRUCT:
629 {
630 uint64_t i;
631 int64_t len = bt_ctf_field_type_structure_get_field_count(
632 (void *) field_common->type);
633
634 BT_ASSERT(len >= 0);
635
636 for (i = 0; i < len; i++) {
637 struct bt_ctf_field *member_field =
638 bt_ctf_field_structure_get_field_by_index(field, i);
639
640 BT_ASSERT(member_field);
641 ret = visit_field_update_clock_value(member_field, val);
642 bt_ctf_object_put_ref(member_field);
643 if (ret) {
644 goto end;
645 }
646 }
647 break;
648 }
649 case BT_CTF_FIELD_TYPE_ID_VARIANT:
650 {
651 struct bt_ctf_field *cur_field =
652 bt_ctf_field_variant_get_current_field(field);
653
654 if (!cur_field) {
655 ret = -1;
656 goto end;
657 }
658
659 ret = visit_field_update_clock_value(cur_field, val);
660 bt_ctf_object_put_ref(cur_field);
661 break;
662 }
663 default:
664 break;
665 }
666
667 end:
668 return ret;
669 }
670
671 int visit_event_update_clock_value(struct bt_ctf_event *event, uint64_t *val)
672 {
673 int ret = 0;
674 struct bt_ctf_field *field;
675
676 field = bt_ctf_event_get_header(event);
677 ret = visit_field_update_clock_value(field, val);
678 bt_ctf_object_put_ref(field);
679 if (ret) {
680 BT_LOGW_STR("Cannot automatically update clock value in "
681 "event's header.");
682 goto end;
683 }
684
685 field = bt_ctf_event_get_stream_event_context(event);
686 ret = visit_field_update_clock_value(field, val);
687 bt_ctf_object_put_ref(field);
688 if (ret) {
689 BT_LOGW_STR("Cannot automatically update clock value in "
690 "event's stream event context.");
691 goto end;
692 }
693
694 field = bt_ctf_event_get_context(event);
695 ret = visit_field_update_clock_value(field, val);
696 bt_ctf_object_put_ref(field);
697 if (ret) {
698 BT_LOGW_STR("Cannot automatically update clock value in "
699 "event's context.");
700 goto end;
701 }
702
703 field = bt_ctf_event_get_payload_field(event);
704 ret = visit_field_update_clock_value(field, val);
705 bt_ctf_object_put_ref(field);
706 if (ret) {
707 BT_LOGW_STR("Cannot automatically update clock value in "
708 "event's payload.");
709 goto end;
710 }
711
712 end:
713 return ret;
714 }
715
716 static
717 int set_packet_context_timestamps(struct bt_ctf_stream *stream)
718 {
719 int ret = 0;
720 uint64_t val;
721 uint64_t cur_clock_value;
722 uint64_t init_clock_value = 0;
723 struct bt_ctf_field *ts_begin_field = bt_ctf_field_structure_get_field_by_name(
724 stream->packet_context, "timestamp_begin");
725 struct bt_ctf_field *ts_end_field = bt_ctf_field_structure_get_field_by_name(
726 stream->packet_context, "timestamp_end");
727 struct bt_ctf_field_common *packet_context =
728 (void *) stream->packet_context;
729 uint64_t i;
730 int64_t len;
731
732 if (ts_begin_field && bt_ctf_field_is_set_recursive(ts_begin_field)) {
733 /* Use provided `timestamp_begin` value as starting value */
734 ret = bt_ctf_field_integer_unsigned_get_value(ts_begin_field, &val);
735 BT_ASSERT(ret == 0);
736 init_clock_value = val;
737 } else if (stream->last_ts_end != -1ULL) {
738 /* Use last packet's ending timestamp as starting value */
739 init_clock_value = stream->last_ts_end;
740 }
741
742 cur_clock_value = init_clock_value;
743
744 if (stream->last_ts_end != -1ULL &&
745 cur_clock_value < stream->last_ts_end) {
746 BT_LOGW("Packet's initial timestamp is less than previous "
747 "packet's final timestamp: "
748 "stream-addr=%p, stream-name=\"%s\", "
749 "cur-packet-ts-begin=%" PRIu64 ", "
750 "prev-packet-ts-end=%" PRIu64,
751 stream, bt_ctf_stream_get_name(stream),
752 cur_clock_value, stream->last_ts_end);
753 ret = -1;
754 goto end;
755 }
756
757 /*
758 * Visit all the packet context fields, followed by all the
759 * fields of all the events, in order, updating our current
760 * clock value as we visit.
761 *
762 * While visiting the packet context fields, do not consider
763 * `timestamp_begin` and `timestamp_end` because this function's
764 * purpose is to set them anyway. Also do not consider
765 * `packet_size`, `content_size`, `events_discarded`, and
766 * `packet_seq_num` if they are not set because those are
767 * autopopulating fields.
768 */
769 len = bt_ctf_field_type_structure_get_field_count(
770 (void *) packet_context->type);
771 BT_ASSERT(len >= 0);
772
773 for (i = 0; i < len; i++) {
774 const char *member_name;
775 struct bt_ctf_field *member_field;
776
777 ret = bt_ctf_field_type_structure_get_field_by_index(
778 (void *) packet_context->type, &member_name, NULL, i);
779 BT_ASSERT(ret == 0);
780
781 if (strcmp(member_name, "timestamp_begin") == 0 ||
782 strcmp(member_name, "timestamp_end") == 0) {
783 continue;
784 }
785
786 member_field = bt_ctf_field_structure_get_field_by_index(
787 stream->packet_context, i);
788 BT_ASSERT(member_field);
789
790 if (strcmp(member_name, "packet_size") == 0 &&
791 !bt_ctf_field_is_set_recursive(member_field)) {
792 bt_ctf_object_put_ref(member_field);
793 continue;
794 }
795
796 if (strcmp(member_name, "content_size") == 0 &&
797 !bt_ctf_field_is_set_recursive(member_field)) {
798 bt_ctf_object_put_ref(member_field);
799 continue;
800 }
801
802 if (strcmp(member_name, "events_discarded") == 0 &&
803 !bt_ctf_field_is_set_recursive(member_field)) {
804 bt_ctf_object_put_ref(member_field);
805 continue;
806 }
807
808 if (strcmp(member_name, "packet_seq_num") == 0 &&
809 !bt_ctf_field_is_set_recursive(member_field)) {
810 bt_ctf_object_put_ref(member_field);
811 continue;
812 }
813
814 ret = visit_field_update_clock_value(member_field,
815 &cur_clock_value);
816 bt_ctf_object_put_ref(member_field);
817 if (ret) {
818 BT_LOGW("Cannot automatically update clock value "
819 "in stream's packet context: "
820 "stream-addr=%p, stream-name=\"%s\", "
821 "field-name=\"%s\"",
822 stream, bt_ctf_stream_get_name(stream),
823 member_name);
824 goto end;
825 }
826 }
827
828 for (i = 0; i < stream->events->len; i++) {
829 struct bt_ctf_event *event = g_ptr_array_index(stream->events, i);
830
831 BT_ASSERT(event);
832 ret = visit_event_update_clock_value(event, &cur_clock_value);
833 if (ret) {
834 BT_LOGW("Cannot automatically update clock value "
835 "in stream's packet context: "
836 "stream-addr=%p, stream-name=\"%s\", "
837 "index=%" PRIu64 ", event-addr=%p, "
838 "event-class-id=%" PRId64 ", "
839 "event-class-name=\"%s\"",
840 stream, bt_ctf_stream_get_name(stream),
841 i, event,
842 bt_ctf_event_class_common_get_id(event->common.class),
843 bt_ctf_event_class_common_get_name(event->common.class));
844 goto end;
845 }
846 }
847
848 /*
849 * Everything is visited, thus the current clock value
850 * corresponds to the ending timestamp. Validate this value
851 * against the provided value of `timestamp_end`, if any,
852 * otherwise set it.
853 */
854 if (ts_end_field && bt_ctf_field_is_set_recursive(ts_end_field)) {
855 ret = bt_ctf_field_integer_unsigned_get_value(ts_end_field, &val);
856 BT_ASSERT(ret == 0);
857
858 if (val < cur_clock_value) {
859 BT_LOGW("Packet's final timestamp is less than "
860 "computed packet's final timestamp: "
861 "stream-addr=%p, stream-name=\"%s\", "
862 "cur-packet-ts-end=%" PRIu64 ", "
863 "computed-packet-ts-end=%" PRIu64,
864 stream, bt_ctf_stream_get_name(stream),
865 val, cur_clock_value);
866 ret = -1;
867 goto end;
868 }
869
870 stream->last_ts_end = val;
871 }
872
873 if (ts_end_field && !bt_ctf_field_is_set_recursive(ts_end_field)) {
874 ret = set_integer_field_value(ts_end_field, cur_clock_value);
875 BT_ASSERT(ret == 0);
876 stream->last_ts_end = cur_clock_value;
877 }
878
879 if (!ts_end_field) {
880 stream->last_ts_end = cur_clock_value;
881 }
882
883 /* Set `timestamp_begin` field to initial clock value */
884 if (ts_begin_field && !bt_ctf_field_is_set_recursive(ts_begin_field)) {
885 ret = set_integer_field_value(ts_begin_field, init_clock_value);
886 BT_ASSERT(ret == 0);
887 }
888
889 end:
890 bt_ctf_object_put_ref(ts_begin_field);
891 bt_ctf_object_put_ref(ts_end_field);
892 return ret;
893 }
894
895 static
896 int auto_populate_packet_context(struct bt_ctf_stream *stream, bool set_ts,
897 uint64_t packet_size_bits, uint64_t content_size_bits)
898 {
899 int ret = 0;
900
901 if (!stream->packet_context) {
902 goto end;
903 }
904
905 ret = set_packet_context_packet_size(stream, packet_size_bits);
906 if (ret) {
907 BT_LOGW("Cannot set packet context's packet size field: "
908 "stream-addr=%p, stream-name=\"%s\"",
909 stream, bt_ctf_stream_get_name(stream));
910 goto end;
911 }
912
913 ret = set_packet_context_content_size(stream, content_size_bits);
914 if (ret) {
915 BT_LOGW("Cannot set packet context's content size field: "
916 "stream-addr=%p, stream-name=\"%s\"",
917 stream, bt_ctf_stream_get_name(stream));
918 goto end;
919 }
920
921 if (set_ts) {
922 ret = set_packet_context_timestamps(stream);
923 if (ret) {
924 BT_LOGW("Cannot set packet context's timestamp fields: "
925 "stream-addr=%p, stream-name=\"%s\"",
926 stream, bt_ctf_stream_get_name(stream));
927 goto end;
928 }
929 }
930
931 ret = set_packet_context_events_discarded(stream);
932 if (ret) {
933 BT_LOGW("Cannot set packet context's discarded events count field: "
934 "stream-addr=%p, stream-name=\"%s\"",
935 stream, bt_ctf_stream_get_name(stream));
936 goto end;
937 }
938
939 BT_LOGV("Automatically populated stream's packet context's known fields: "
940 "stream-addr=%p, stream-name=\"%s\"",
941 stream, bt_ctf_stream_get_name(stream));
942
943 end:
944 return ret;
945 }
946
947 static
948 void release_event(struct bt_ctf_event *event)
949 {
950 if (bt_ctf_object_get_ref_count(&event->common.base)) {
951 /*
952 * The event is being orphaned, but it must guarantee the
953 * existence of its event class for the duration of its
954 * lifetime.
955 */
956 bt_ctf_object_get_ref(event->common.class);
957 BT_CTF_OBJECT_PUT_REF_AND_RESET(event->common.base.parent);
958 } else {
959 bt_ctf_object_try_spec_release(&event->common.base);
960 }
961 }
962
963 static
964 int create_stream_file(struct bt_ctf_writer *writer,
965 struct bt_ctf_stream *stream)
966 {
967 int ret = 0;
968 GString *filename = g_string_new(NULL);
969 int64_t stream_class_id;
970 char *file_path = NULL;
971
972 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
973 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
974 writer, stream, bt_ctf_stream_get_name(stream),
975 stream->common.stream_class,
976 stream->common.stream_class->name->str);
977
978 if (stream->common.name && stream->common.name->len > 0) {
979 /* Use stream name's base name as prefix */
980 gchar *basename = g_path_get_basename(stream->common.name->str);
981
982 BT_ASSERT(basename);
983
984 if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) {
985 g_string_assign(filename, "stream");
986 } else {
987 g_string_assign(filename, basename);
988 }
989
990 g_free(basename);
991 goto append_ids;
992 }
993
994 if (stream->common.stream_class->name &&
995 stream->common.stream_class->name->len > 0) {
996 /* Use stream class name's base name as prefix */
997 gchar *basename =
998 g_path_get_basename(
999 stream->common.stream_class->name->str);
1000
1001 BT_ASSERT(basename);
1002
1003 if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) {
1004 g_string_assign(filename, "stream");
1005 } else {
1006 g_string_assign(filename, basename);
1007 }
1008
1009 g_free(basename);
1010 goto append_ids;
1011 }
1012
1013 /* Default to using `stream-` as prefix */
1014 g_string_assign(filename, "stream");
1015
1016 append_ids:
1017 stream_class_id = bt_ctf_stream_class_common_get_id(stream->common.stream_class);
1018 BT_ASSERT(stream_class_id >= 0);
1019 BT_ASSERT(stream->common.id >= 0);
1020 g_string_append_printf(filename, "-%" PRId64 "-%" PRId64,
1021 stream_class_id, stream->common.id);
1022
1023 file_path = g_build_filename(writer->path->str, filename->str, NULL);
1024 if (file_path == NULL) {
1025 ret = -1;
1026 goto end;
1027 }
1028
1029 ret = bt_ctfser_init(&stream->ctfser, file_path);
1030 g_free(file_path);
1031 if (ret) {
1032 /* bt_ctfser_init() logs errors */
1033 goto end;
1034 }
1035
1036 BT_LOGD("Created stream file for writing: "
1037 "stream-addr=%p, stream-name=\"%s\", "
1038 "filename=\"%s\"", stream, bt_ctf_stream_get_name(stream),
1039 filename->str);
1040
1041 end:
1042 g_string_free(filename, TRUE);
1043 return ret;
1044 }
1045
1046 BT_HIDDEN
1047 struct bt_ctf_stream *bt_ctf_stream_create_with_id(
1048 struct bt_ctf_stream_class *stream_class,
1049 const char *name, uint64_t id)
1050 {
1051 int ret;
1052 int fd;
1053 struct bt_ctf_stream *stream = NULL;
1054 struct bt_ctf_trace *trace = NULL;
1055 struct bt_ctf_writer *writer = NULL;
1056
1057 BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, "
1058 "stream-class-name=\"%s\", stream-name=\"%s\", "
1059 "stream-id=%" PRIu64,
1060 stream_class, bt_ctf_stream_class_get_name(stream_class),
1061 name, id);
1062 stream = g_new0(struct bt_ctf_stream, 1);
1063 if (!stream) {
1064 BT_LOGE_STR("Failed to allocate one stream.");
1065 goto error;
1066 }
1067
1068 if (id == -1ULL) {
1069 id = stream_class->next_stream_id;
1070 }
1071
1072 ret = bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream),
1073 BT_CTF_TO_COMMON(stream_class), name, id, bt_ctf_stream_destroy);
1074 if (ret) {
1075 /* bt_ctf_stream_common_initialize() logs errors */
1076 goto error;
1077 }
1078
1079 trace = BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1080 BT_CTF_TO_COMMON(stream_class)));
1081 if (!trace) {
1082 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1083 "stream-class-addr=%p, stream-class-name=\"%s\", "
1084 "stream-name=\"%s\"",
1085 stream_class, bt_ctf_stream_class_get_name(stream_class),
1086 name);
1087 goto error;
1088 }
1089
1090 writer = (struct bt_ctf_writer *)
1091 bt_ctf_object_get_parent(&trace->common.base);
1092 stream->last_ts_end = -1ULL;
1093 BT_LOGD("CTF writer stream object belongs writer's trace: "
1094 "writer-addr=%p", writer);
1095 BT_ASSERT(writer);
1096
1097 if (stream_class->common.packet_context_field_type) {
1098 BT_LOGD("Creating stream's packet context field: "
1099 "ft-addr=%p",
1100 stream_class->common.packet_context_field_type);
1101 stream->packet_context = bt_ctf_field_create(
1102 (void *) stream_class->common.packet_context_field_type);
1103 if (!stream->packet_context) {
1104 BT_LOGW_STR("Cannot create stream's packet context field.");
1105 goto error;
1106 }
1107
1108 /* Initialize events_discarded */
1109 ret = try_set_structure_field_integer(
1110 stream->packet_context, "events_discarded", 0);
1111 if (ret < 0) {
1112 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1113 "ret=%d, packet-context-field-addr=%p",
1114 ret, stream->packet_context);
1115 goto error;
1116 }
1117 }
1118
1119 stream->events = g_ptr_array_new_with_free_func(
1120 (GDestroyNotify) release_event);
1121 if (!stream->events) {
1122 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1123 goto error;
1124 }
1125
1126 if (trace->common.packet_header_field_type) {
1127 BT_LOGD("Creating stream's packet header field: "
1128 "ft-addr=%p", trace->common.packet_header_field_type);
1129 stream->packet_header =
1130 bt_ctf_field_create(
1131 (void *) trace->common.packet_header_field_type);
1132 if (!stream->packet_header) {
1133 BT_LOGW_STR("Cannot create stream's packet header field.");
1134 goto error;
1135 }
1136 }
1137
1138 /*
1139 * Attempt to populate the default trace packet header fields
1140 * (magic, uuid and stream_id). This will _not_ fail shall the
1141 * fields not be found or be of an incompatible type; they will
1142 * simply not be populated automatically. The user will have to
1143 * make sure to set the trace packet header fields himself
1144 * before flushing.
1145 */
1146 ret = auto_populate_packet_header(stream);
1147 if (ret) {
1148 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1149 goto error;
1150 }
1151
1152 /* Create file associated with this stream */
1153 fd = create_stream_file(writer, stream);
1154 if (fd < 0) {
1155 BT_LOGW_STR("Cannot create stream file.");
1156 goto error;
1157 }
1158
1159 /* Freeze the writer */
1160 BT_LOGD_STR("Freezing stream's CTF writer.");
1161 bt_ctf_writer_freeze(writer);
1162
1163 /* Add this stream to the trace's streams */
1164 g_ptr_array_add(trace->common.streams, stream);
1165 stream_class->next_stream_id++;
1166 BT_LOGD("Created stream object: addr=%p", stream);
1167 goto end;
1168
1169 error:
1170 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream);
1171
1172 end:
1173 bt_ctf_object_put_ref(writer);
1174 return stream;
1175 }
1176
1177 struct bt_ctf_stream *bt_ctf_stream_create(
1178 struct bt_ctf_stream_class *stream_class,
1179 const char *name, uint64_t id_param)
1180 {
1181 return bt_ctf_stream_create_with_id(stream_class,
1182 name, id_param);
1183 }
1184
1185 int bt_ctf_stream_get_discarded_events_count(
1186 struct bt_ctf_stream *stream, uint64_t *count)
1187 {
1188 int ret = 0;
1189
1190 if (!stream) {
1191 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1192 ret = -1;
1193 goto end;
1194 }
1195
1196 if (!count) {
1197 BT_LOGW_STR("Invalid parameter: count is NULL.");
1198 ret = -1;
1199 goto end;
1200 }
1201
1202 *count = (uint64_t) stream->discarded_events;
1203
1204 end:
1205 return ret;
1206 }
1207
1208 static
1209 int set_packet_context_events_discarded_field(struct bt_ctf_stream *stream,
1210 uint64_t count)
1211 {
1212 int ret = 0;
1213 struct bt_ctf_field *events_discarded_field = NULL;
1214
1215 if (!stream->packet_context) {
1216 goto end;
1217 }
1218
1219 events_discarded_field = bt_ctf_field_structure_get_field_by_name(
1220 stream->packet_context, "events_discarded");
1221 if (!events_discarded_field) {
1222 goto end;
1223 }
1224
1225 ret = bt_ctf_field_integer_unsigned_set_value(
1226 events_discarded_field, count);
1227 if (ret) {
1228 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1229 "field-addr=%p, value=%" PRIu64,
1230 events_discarded_field, count);
1231 goto end;
1232 }
1233
1234 end:
1235 bt_ctf_object_put_ref(events_discarded_field);
1236 return ret;
1237 }
1238
1239 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
1240 uint64_t event_count)
1241 {
1242 int ret;
1243 uint64_t new_count;
1244 struct bt_ctf_field *events_discarded_field = NULL;
1245
1246 if (!stream) {
1247 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1248 goto end;
1249 }
1250
1251 BT_LOGV("Appending discarded events to stream: "
1252 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1253 stream, bt_ctf_stream_get_name(stream), event_count);
1254
1255 if (!stream->packet_context) {
1256 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1257 goto end;
1258 }
1259
1260 events_discarded_field = bt_ctf_field_structure_get_field_by_name(
1261 stream->packet_context, "events_discarded");
1262 if (!events_discarded_field) {
1263 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1264 goto end;
1265 }
1266
1267 new_count = stream->discarded_events + event_count;
1268 if (new_count < stream->discarded_events) {
1269 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1270 "cur-count=%" PRIu64 ", new-count=%" PRIu64,
1271 stream->discarded_events, new_count);
1272 goto end;
1273 }
1274
1275 ret = set_packet_context_events_discarded_field(stream, new_count);
1276 if (ret) {
1277 /* set_packet_context_events_discarded_field() logs errors */
1278 goto end;
1279 }
1280
1281 stream->discarded_events = new_count;
1282 BT_LOGV("Appended discarded events to stream: "
1283 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1284 stream, bt_ctf_stream_get_name(stream), event_count);
1285
1286 end:
1287 bt_ctf_object_put_ref(events_discarded_field);
1288 }
1289
1290 static int auto_populate_event_header(struct bt_ctf_stream *stream,
1291 struct bt_ctf_event *event)
1292 {
1293 int ret = 0;
1294 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
1295 struct bt_ctf_clock_class *mapped_clock_class = NULL;
1296 struct bt_ctf_stream_class *stream_class =
1297 BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class(
1298 BT_CTF_TO_COMMON(stream)));
1299 int64_t event_class_id;
1300
1301 BT_ASSERT(event);
1302
1303 if (!event->common.header_field) {
1304 goto end;
1305 }
1306
1307 if (event->common.frozen) {
1308 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1309 ret = -1;
1310 goto end;
1311 }
1312
1313 BT_LOGV("Automatically populating event's header field: "
1314 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1315 stream, bt_ctf_stream_get_name(stream), event);
1316
1317 id_field = bt_ctf_field_structure_get_field_by_name(
1318 (void *) event->common.header_field->field, "id");
1319 event_class_id = bt_ctf_event_class_common_get_id(event->common.class);
1320 BT_ASSERT(event_class_id >= 0);
1321
1322 if (id_field && bt_ctf_field_get_type_id(id_field) == BT_CTF_FIELD_TYPE_ID_INTEGER) {
1323 ret = set_integer_field_value(id_field, event_class_id);
1324 if (ret) {
1325 BT_LOGW("Cannot set event header's `id` field's value: "
1326 "addr=%p, value=%" PRIu64, id_field,
1327 event_class_id);
1328 goto end;
1329 }
1330 }
1331
1332 /*
1333 * The conditions to automatically set the timestamp are:
1334 *
1335 * 1. The event header field "timestamp" exists and is an
1336 * integer field.
1337 * 2. This stream's class has a registered clock (set with
1338 * bt_ctf_stream_class_set_clock()).
1339 * 3. The "timestamp" field is not set.
1340 */
1341 timestamp_field = bt_ctf_field_structure_get_field_by_name(
1342 (void *) event->common.header_field->field, "timestamp");
1343 if (timestamp_field && stream_class->clock &&
1344 bt_ctf_field_get_type_id(id_field) == BT_CTF_FIELD_TYPE_ID_INTEGER &&
1345 !bt_ctf_field_is_set_recursive(timestamp_field)) {
1346 mapped_clock_class =
1347 bt_ctf_field_type_integer_get_mapped_clock_class(
1348 (void *) ((struct bt_ctf_field_common *) timestamp_field)->type);
1349 if (mapped_clock_class) {
1350 uint64_t timestamp;
1351
1352 BT_ASSERT(mapped_clock_class ==
1353 stream_class->clock->clock_class);
1354 ret = bt_ctf_clock_get_value(
1355 stream_class->clock,
1356 &timestamp);
1357 BT_ASSERT(ret == 0);
1358 ret = set_integer_field_value(timestamp_field,
1359 timestamp);
1360 if (ret) {
1361 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1362 "addr=%p, value=%" PRIu64,
1363 timestamp_field, timestamp);
1364 goto end;
1365 }
1366 }
1367 }
1368
1369 BT_LOGV("Automatically populated event's header field: "
1370 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1371 stream, bt_ctf_stream_get_name(stream), event);
1372
1373 end:
1374 bt_ctf_object_put_ref(id_field);
1375 bt_ctf_object_put_ref(timestamp_field);
1376 bt_ctf_object_put_ref(mapped_clock_class);
1377 return ret;
1378 }
1379
1380 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
1381 struct bt_ctf_event *event)
1382 {
1383 int ret = 0;
1384
1385 if (!stream) {
1386 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1387 ret = -1;
1388 goto end;
1389 }
1390
1391 if (!event) {
1392 BT_LOGW_STR("Invalid parameter: event is NULL.");
1393 ret = -1;
1394 goto end;
1395 }
1396
1397 BT_LOGV("Appending event to stream: "
1398 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1399 "event-class-name=\"%s\", event-class-id=%" PRId64,
1400 stream, bt_ctf_stream_get_name(stream), event,
1401 bt_ctf_event_class_common_get_name(
1402 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))),
1403 bt_ctf_event_class_common_get_id(
1404 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))));
1405
1406 /*
1407 * The event is not supposed to have a parent stream at this
1408 * point. The only other way an event can have a parent stream
1409 * is if it was assigned when setting a packet to the event,
1410 * in which case the packet's stream is not a writer stream,
1411 * and thus the user is trying to append an event which belongs
1412 * to another stream.
1413 */
1414 if (event->common.base.parent) {
1415 ret = -1;
1416 goto end;
1417 }
1418
1419 bt_ctf_object_set_parent(&event->common.base, &stream->common.base);
1420 BT_LOGV_STR("Automatically populating the header of the event to append.");
1421 ret = auto_populate_event_header(stream, event);
1422 if (ret) {
1423 /* auto_populate_event_header() reports errors */
1424 goto error;
1425 }
1426
1427 /* Make sure the various scopes of the event are set */
1428 BT_LOGV_STR("Validating event to append.");
1429 BT_CTF_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event)) == 0,
1430 "Invalid event: event-addr=%p", event);
1431
1432 /* Save the new event and freeze it */
1433 BT_LOGV_STR("Freezing the event to append.");
1434 bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event), true);
1435 g_ptr_array_add(stream->events, event);
1436
1437 /*
1438 * Event had to hold a reference to its event class as long as it wasn't
1439 * part of the same trace hierarchy. From now on, the event and its
1440 * class share the same lifetime guarantees and the reference is no
1441 * longer needed.
1442 */
1443 BT_LOGV_STR("Putting the event's class.");
1444 bt_ctf_object_put_ref(event->common.class);
1445 BT_LOGV("Appended event to stream: "
1446 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1447 "event-class-name=\"%s\", event-class-id=%" PRId64,
1448 stream, bt_ctf_stream_get_name(stream), event,
1449 bt_ctf_event_class_common_get_name(
1450 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))),
1451 bt_ctf_event_class_common_get_id(
1452 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))));
1453
1454 end:
1455 return ret;
1456
1457 error:
1458 /*
1459 * Orphan the event; we were not successful in associating it to
1460 * a stream.
1461 */
1462 bt_ctf_object_set_parent(&event->common.base, NULL);
1463 return ret;
1464 }
1465
1466 struct bt_ctf_field *bt_ctf_stream_get_packet_context(struct bt_ctf_stream *stream)
1467 {
1468 struct bt_ctf_field *packet_context = NULL;
1469
1470 if (!stream) {
1471 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1472 goto end;
1473 }
1474
1475 packet_context = stream->packet_context;
1476 if (packet_context) {
1477 bt_ctf_object_get_ref(packet_context);
1478 }
1479 end:
1480 return packet_context;
1481 }
1482
1483 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
1484 struct bt_ctf_field *field)
1485 {
1486 int ret = 0;
1487 struct bt_ctf_field_type *field_type;
1488
1489 if (!stream) {
1490 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1491 ret = -1;
1492 goto end;
1493 }
1494
1495 field_type = bt_ctf_field_get_type(field);
1496 if (bt_ctf_field_type_common_compare((void *) field_type,
1497 stream->common.stream_class->packet_context_field_type)) {
1498 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1499 "stream-addr=%p, stream-name=\"%s\", "
1500 "packet-context-field-addr=%p, "
1501 "packet-context-ft-addr=%p",
1502 stream, bt_ctf_stream_get_name(stream),
1503 field, field_type);
1504 ret = -1;
1505 goto end;
1506 }
1507
1508 bt_ctf_object_put_ref(field_type);
1509 bt_ctf_object_put_ref(stream->packet_context);
1510 stream->packet_context = bt_ctf_object_get_ref(field);
1511 BT_LOGV("Set stream's packet context field: "
1512 "stream-addr=%p, stream-name=\"%s\", "
1513 "packet-context-field-addr=%p",
1514 stream, bt_ctf_stream_get_name(stream), field);
1515 end:
1516 return ret;
1517 }
1518
1519 struct bt_ctf_field *bt_ctf_stream_get_packet_header(struct bt_ctf_stream *stream)
1520 {
1521 struct bt_ctf_field *packet_header = NULL;
1522
1523 if (!stream) {
1524 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1525 goto end;
1526 }
1527
1528 packet_header = stream->packet_header;
1529 if (packet_header) {
1530 bt_ctf_object_get_ref(packet_header);
1531 }
1532 end:
1533 return packet_header;
1534 }
1535
1536 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
1537 struct bt_ctf_field *field)
1538 {
1539 int ret = 0;
1540 struct bt_ctf_trace *trace = NULL;
1541 struct bt_ctf_field_type *field_type = NULL;
1542
1543 if (!stream) {
1544 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1545 ret = -1;
1546 goto end;
1547 }
1548
1549 trace = (struct bt_ctf_trace *)
1550 bt_ctf_object_get_parent(&stream->common.base);
1551
1552 if (!field) {
1553 if (trace->common.packet_header_field_type) {
1554 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1555 "stream-addr=%p, stream-name=\"%s\", "
1556 "packet-header-field-addr=%p, "
1557 "expected-ft-addr=%p",
1558 stream, bt_ctf_stream_get_name(stream),
1559 field, trace->common.packet_header_field_type);
1560 ret = -1;
1561 goto end;
1562 }
1563
1564 goto skip_validation;
1565 }
1566
1567 field_type = bt_ctf_field_get_type(field);
1568 BT_ASSERT(field_type);
1569
1570 if (bt_ctf_field_type_common_compare((void *) field_type,
1571 trace->common.packet_header_field_type)) {
1572 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1573 "stream-addr=%p, stream-name=\"%s\", "
1574 "packet-header-field-addr=%p, "
1575 "packet-header-ft-addr=%p",
1576 stream, bt_ctf_stream_get_name(stream),
1577 field, field_type);
1578 ret = -1;
1579 goto end;
1580 }
1581
1582 skip_validation:
1583 bt_ctf_object_put_ref(stream->packet_header);
1584 stream->packet_header = bt_ctf_object_get_ref(field);
1585 BT_LOGV("Set stream's packet header field: "
1586 "stream-addr=%p, stream-name=\"%s\", "
1587 "packet-header-field-addr=%p",
1588 stream, bt_ctf_stream_get_name(stream), field);
1589 end:
1590 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace);
1591 bt_ctf_object_put_ref(field_type);
1592 return ret;
1593 }
1594
1595 static
1596 void reset_structure_field(struct bt_ctf_field *structure, const char *name)
1597 {
1598 struct bt_ctf_field *member;
1599
1600 member = bt_ctf_field_structure_get_field_by_name(structure, name);
1601 if (member) {
1602 bt_ctf_field_common_reset_recursive((void *) member);
1603 bt_ctf_object_put_ref(member);
1604 }
1605 }
1606
1607 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
1608 {
1609 int ret = 0;
1610 size_t i;
1611 uint64_t packet_context_offset_bits = 0;
1612 struct bt_ctf_trace *trace;
1613 enum bt_ctf_byte_order native_byte_order;
1614 bool has_packet_size = false;
1615 uint64_t packet_size_bits = 0;
1616 uint64_t content_size_bits = 0;
1617
1618 if (!stream) {
1619 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1620 ret = -1;
1621 goto end_no_stream;
1622 }
1623
1624 if (stream->packet_context) {
1625 struct bt_ctf_field *packet_size_field;
1626
1627 packet_size_field = bt_ctf_field_structure_get_field_by_name(
1628 stream->packet_context, "packet_size");
1629 has_packet_size = (packet_size_field != NULL);
1630 bt_ctf_object_put_ref(packet_size_field);
1631 }
1632
1633 if (stream->flushed_packet_count == 1) {
1634 if (!stream->packet_context) {
1635 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1636 ret = -1;
1637 goto end;
1638 }
1639
1640 if (!has_packet_size) {
1641 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1642 ret = -1;
1643 goto end;
1644 }
1645 }
1646
1647 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1648 "stream-name=\"%s\", packet-index=%u", stream,
1649 bt_ctf_stream_get_name(stream), stream->flushed_packet_count);
1650 trace = BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1651 stream->common.stream_class));
1652 BT_ASSERT(trace);
1653 native_byte_order = bt_ctf_trace_get_native_byte_order(trace);
1654
1655 ret = auto_populate_packet_header(stream);
1656 if (ret) {
1657 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1658 ret = -1;
1659 goto end;
1660 }
1661
1662 /* Initialize packet/content sizes to `0`; we will overwrite later */
1663 ret = auto_populate_packet_context(stream, true, 0, 0);
1664 if (ret) {
1665 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1666 ret = -1;
1667 goto end;
1668 }
1669
1670 ret = bt_ctfser_open_packet(&stream->ctfser);
1671 if (ret) {
1672 /* bt_ctfser_open_packet() logs errors */
1673 ret = -1;
1674 goto end;
1675 }
1676
1677 if (stream->packet_header) {
1678 BT_LOGV_STR("Serializing packet header field (initial).");
1679 ret = bt_ctf_field_serialize_recursive(stream->packet_header,
1680 &stream->ctfser, native_byte_order);
1681 if (ret) {
1682 BT_LOGW("Cannot serialize stream's packet header field: "
1683 "field-addr=%p", stream->packet_header);
1684 goto end;
1685 }
1686 }
1687
1688 if (stream->packet_context) {
1689 /* Save packet context's position to overwrite it later */
1690 packet_context_offset_bits =
1691 bt_ctfser_get_offset_in_current_packet_bits(
1692 &stream->ctfser);
1693
1694 /* Write packet context */
1695 BT_LOGV_STR("Serializing packet context field (initial).");
1696 ret = bt_ctf_field_serialize_recursive(stream->packet_context,
1697 &stream->ctfser, native_byte_order);
1698 if (ret) {
1699 BT_LOGW("Cannot serialize stream's packet context field: "
1700 "field-addr=%p", stream->packet_context);
1701 goto end;
1702 }
1703 }
1704
1705 BT_LOGV("Serializing events: count=%u", stream->events->len);
1706
1707 for (i = 0; i < stream->events->len; i++) {
1708 struct bt_ctf_event *event = g_ptr_array_index(
1709 stream->events, i);
1710 struct bt_ctf_event_class *event_class =
1711 BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class(
1712 BT_CTF_TO_COMMON(event)));
1713
1714 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1715 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1716 "ser-offset=%" PRIu64,
1717 i, event, bt_ctf_event_class_get_name(event_class),
1718 bt_ctf_event_class_get_id(event_class),
1719 bt_ctfser_get_offset_in_current_packet_bits(
1720 &stream->ctfser));
1721
1722 /* Write event header */
1723 if (event->common.header_field) {
1724 BT_LOGV_STR("Serializing event's header field.");
1725 ret = bt_ctf_field_serialize_recursive(
1726 (void *) event->common.header_field->field,
1727 &stream->ctfser, native_byte_order);
1728 if (ret) {
1729 BT_LOGW("Cannot serialize event's header field: "
1730 "field-addr=%p",
1731 event->common.header_field->field);
1732 goto end;
1733 }
1734 }
1735
1736 /* Write stream event context */
1737 if (event->common.stream_event_context_field) {
1738 BT_LOGV_STR("Serializing event's stream event context field.");
1739 ret = bt_ctf_field_serialize_recursive(
1740 (void *) event->common.stream_event_context_field,
1741 &stream->ctfser, native_byte_order);
1742 if (ret) {
1743 BT_LOGW("Cannot serialize event's stream event context field: "
1744 "field-addr=%p",
1745 event->common.stream_event_context_field);
1746 goto end;
1747 }
1748 }
1749
1750 /* Write event content */
1751 ret = bt_ctf_event_serialize(event, &stream->ctfser,
1752 native_byte_order);
1753 if (ret) {
1754 /* bt_ctf_event_serialize() logs errors */
1755 goto end;
1756 }
1757 }
1758
1759 content_size_bits = bt_ctfser_get_offset_in_current_packet_bits(
1760 &stream->ctfser);
1761
1762 if (!has_packet_size && content_size_bits % 8 != 0) {
1763 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1764 "but current content size is not a multiple of 8 bits: "
1765 "content-size=%" PRIu64 ", "
1766 "packet-size=%" PRIu64,
1767 content_size_bits,
1768 packet_size_bits);
1769 ret = -1;
1770 goto end;
1771 }
1772
1773 /* Set packet size; make it a multiple of 8 */
1774 packet_size_bits = (content_size_bits + 7) & ~UINT64_C(7);
1775
1776 if (stream->packet_context) {
1777 /*
1778 * The whole packet is serialized at this point. Make
1779 * sure that, if `packet_size` is missing, the current
1780 * content size is equal to the current packet size.
1781 */
1782 struct bt_ctf_field *field =
1783 bt_ctf_field_structure_get_field_by_name(
1784 stream->packet_context, "content_size");
1785
1786 bt_ctf_object_put_ref(field);
1787 if (!field) {
1788 if (content_size_bits != packet_size_bits) {
1789 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1790 "but current packet's content size is not equal to its packet size: "
1791 "content-size=%" PRIu64 ", "
1792 "packet-size=%" PRIu64,
1793 bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser),
1794 packet_size_bits);
1795 ret = -1;
1796 goto end;
1797 }
1798 }
1799
1800 /*
1801 * Overwrite the packet context now that the stream
1802 * position's packet and content sizes have the correct
1803 * values.
1804 */
1805 bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser,
1806 packet_context_offset_bits);
1807 ret = auto_populate_packet_context(stream, false,
1808 packet_size_bits, content_size_bits);
1809 if (ret) {
1810 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1811 ret = -1;
1812 goto end;
1813 }
1814
1815 BT_LOGV("Rewriting (serializing) packet context field.");
1816 ret = bt_ctf_field_serialize_recursive(stream->packet_context,
1817 &stream->ctfser, native_byte_order);
1818 if (ret) {
1819 BT_LOGW("Cannot serialize stream's packet context field: "
1820 "field-addr=%p", stream->packet_context);
1821 goto end;
1822 }
1823 }
1824
1825 g_ptr_array_set_size(stream->events, 0);
1826 stream->flushed_packet_count++;
1827 bt_ctfser_close_current_packet(&stream->ctfser, packet_size_bits / 8);
1828
1829 end:
1830 /* Reset automatically-set fields. */
1831 if (stream->packet_context) {
1832 reset_structure_field(stream->packet_context, "timestamp_begin");
1833 reset_structure_field(stream->packet_context, "timestamp_end");
1834 reset_structure_field(stream->packet_context, "packet_size");
1835 reset_structure_field(stream->packet_context, "content_size");
1836 reset_structure_field(stream->packet_context, "events_discarded");
1837 }
1838
1839 if (ret == 0) {
1840 BT_LOGV("Flushed stream's current packet: "
1841 "content-size=%" PRIu64 ", packet-size=%" PRIu64,
1842 content_size_bits, packet_size_bits);
1843 }
1844
1845 end_no_stream:
1846 return ret;
1847 }
1848
1849 static
1850 void bt_ctf_stream_destroy(struct bt_ctf_object *obj)
1851 {
1852 struct bt_ctf_stream *stream = (void *) obj;
1853
1854 BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"",
1855 stream, bt_ctf_stream_get_name(stream));
1856
1857 bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream));
1858 bt_ctfser_fini(&stream->ctfser);
1859
1860 if (stream->events) {
1861 BT_LOGD_STR("Putting events.");
1862 g_ptr_array_free(stream->events, TRUE);
1863 }
1864
1865 BT_LOGD_STR("Putting packet header field.");
1866 bt_ctf_object_put_ref(stream->packet_header);
1867 BT_LOGD_STR("Putting packet context field.");
1868 bt_ctf_object_put_ref(stream->packet_context);
1869 g_free(stream);
1870 }
1871
1872 static
1873 int _set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1874 uint64_t value, bt_bool force)
1875 {
1876 int ret = 0;
1877 struct bt_ctf_field_type *field_type = NULL;
1878 struct bt_ctf_field *integer;
1879
1880 BT_ASSERT(structure);
1881 BT_ASSERT(name);
1882
1883 integer = bt_ctf_field_structure_get_field_by_name(structure, name);
1884 if (!integer) {
1885 /* Field not found, not an error. */
1886 BT_LOGV("Field not found: struct-field-addr=%p, "
1887 "name=\"%s\", force=%d", structure, name, force);
1888 goto end;
1889 }
1890
1891 /* Make sure the payload has not already been set. */
1892 if (!force && bt_ctf_field_is_set_recursive(integer)) {
1893 /* Payload already set, not an error */
1894 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1895 "name=\"%s\", force=%d", structure, name, force);
1896 goto end;
1897 }
1898
1899 field_type = bt_ctf_field_get_type(integer);
1900 BT_ASSERT(field_type);
1901 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1902 /*
1903 * The user most likely meant for us to populate this field
1904 * automatically. However, we can only do this if the field
1905 * is an integer. Return an error.
1906 */
1907 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1908 "field-addr=%p, ft-addr=%p, ft-id=%s",
1909 integer, field_type,
1910 bt_ctf_field_type_id_string((int)
1911 bt_ctf_field_type_get_type_id(field_type)));
1912 ret = -1;
1913 goto end;
1914 }
1915
1916 if (bt_ctf_field_type_integer_is_signed(field_type)) {
1917 ret = bt_ctf_field_integer_signed_set_value(integer,
1918 (int64_t) value);
1919 } else {
1920 ret = bt_ctf_field_integer_unsigned_set_value(integer, value);
1921 }
1922 ret = !ret ? 1 : ret;
1923 end:
1924 bt_ctf_object_put_ref(integer);
1925 bt_ctf_object_put_ref(field_type);
1926 return ret;
1927 }
1928
1929 /*
1930 * Returns the following codes:
1931 * 1 if the field was found and set,
1932 * 0 if nothing was done (field not found, or was already set),
1933 * <0 if an error was encoutered
1934 */
1935 static
1936 int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1937 uint64_t value)
1938 {
1939 return _set_structure_field_integer(structure, name, value, BT_FALSE);
1940 }
1941
1942 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
1943 struct bt_ctf_stream *stream)
1944 {
1945 return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream)));
1946 }
1947
1948 const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
1949 {
1950 return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream));
1951 }
1952
1953 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream *stream)
1954 {
1955 return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream));
1956 }
This page took 0.068369 seconds and 4 git commands to generate.