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