64f82d4a58b70f21bca144985db19d0f4e961982
[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 void component_destroy_listener(struct bt_component *component, void *data)
964 {
965 struct bt_stream *stream = data;
966
967 BT_LOGD("Component is being destroyed, stream is notified: "
968 "comp-addr=%p, stream-addr=%p", component, stream);
969 g_hash_table_remove(stream->comp_cur_port, component);
970 }
971
972 static
973 struct bt_stream *bt_stream_create_with_id_no_check(
974 struct bt_stream_class *stream_class,
975 const char *name, uint64_t id)
976 {
977 int ret;
978 struct bt_stream *stream = NULL;
979 struct bt_trace *trace = NULL;
980 struct bt_ctf_writer *writer = NULL;
981
982 if (!stream_class) {
983 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
984 goto error;
985 }
986
987 BT_LOGD("Creating stream object: stream-class-addr=%p, "
988 "stream-class-name=\"%s\", stream-name=\"%s\", "
989 "stream-id=%" PRIu64,
990 stream_class, bt_stream_class_get_name(stream_class),
991 name, id);
992 trace = bt_stream_class_borrow_trace(stream_class);
993 if (!trace) {
994 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
995 "stream-class-addr=%p, stream-class-name=\"%s\", "
996 "stream-name=\"%s\"",
997 stream_class, bt_stream_class_get_name(stream_class),
998 name);
999 goto error;
1000 }
1001
1002 if (bt_trace_is_static(trace)) {
1003 /*
1004 * A static trace has the property that all its stream
1005 * classes, clock classes, and streams are definitive:
1006 * no more can be added, and each object is also frozen.
1007 */
1008 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: "
1009 "stream-class-addr=%p, stream-class-name=\"%s\", "
1010 "stream-name=\"%s\", trace-addr=%p",
1011 stream_class, bt_stream_class_get_name(stream_class),
1012 name, trace);
1013 goto error;
1014 }
1015
1016 if (id != -1ULL) {
1017 /*
1018 * Validate that the given ID is unique amongst all the
1019 * existing trace's streams created from the same stream
1020 * class.
1021 */
1022 size_t i;
1023
1024 for (i = 0; i < trace->streams->len; i++) {
1025 struct bt_stream *trace_stream =
1026 g_ptr_array_index(trace->streams, i);
1027
1028 if (trace_stream->stream_class != stream_class) {
1029 continue;
1030 }
1031
1032 if (trace_stream->id == id) {
1033 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
1034 goto error;
1035 }
1036 }
1037 }
1038
1039 stream = g_new0(struct bt_stream, 1);
1040 if (!stream) {
1041 BT_LOGE_STR("Failed to allocate one stream.");
1042 goto error;
1043 }
1044
1045 bt_object_init(stream, bt_stream_destroy);
1046 /*
1047 * Acquire reference to parent since stream will become publicly
1048 * reachable; it needs its parent to remain valid.
1049 */
1050 bt_object_set_parent(stream, trace);
1051 stream->stream_class = stream_class;
1052 stream->pos.fd = -1;
1053 stream->id = (int64_t) id;
1054
1055 stream->destroy_listeners = g_array_new(FALSE, TRUE,
1056 sizeof(struct bt_stream_destroy_listener));
1057 if (!stream->destroy_listeners) {
1058 BT_LOGE_STR("Failed to allocate a GArray.");
1059 goto error;
1060 }
1061
1062 if (name) {
1063 stream->name = g_string_new(name);
1064 if (!stream->name) {
1065 BT_LOGE_STR("Failed to allocate a GString.");
1066 goto error;
1067 }
1068 }
1069
1070 BT_LOGD("Set stream's trace parent: trace-addr=%p", trace);
1071
1072 if (trace->is_created_by_writer) {
1073 int fd;
1074
1075 writer = (struct bt_ctf_writer *) bt_object_get_parent(trace);
1076 stream->id = (int64_t) stream_class->next_stream_id++;
1077 stream->last_ts_end = -1ULL;
1078
1079 BT_LOGD("Stream object belongs to a writer's trace: "
1080 "writer-addr=%p", writer);
1081 BT_ASSERT(writer);
1082
1083 if (stream_class->packet_context_type) {
1084 BT_LOGD("Creating stream's packet context field: "
1085 "ft-addr=%p", stream_class->packet_context_type);
1086 stream->packet_context = bt_field_create(
1087 stream_class->packet_context_type);
1088 if (!stream->packet_context) {
1089 BT_LOGW_STR("Cannot create stream's packet context field.");
1090 goto error;
1091 }
1092
1093 /* Initialize events_discarded */
1094 ret = try_set_structure_field_integer(
1095 stream->packet_context, "events_discarded", 0);
1096 if (ret < 0) {
1097 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1098 "ret=%d, packet-context-field-addr=%p",
1099 ret, stream->packet_context);
1100 goto error;
1101 }
1102 }
1103
1104 stream->events = g_ptr_array_new_with_free_func(
1105 (GDestroyNotify) release_event);
1106 if (!stream->events) {
1107 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1108 goto error;
1109 }
1110
1111 if (trace->packet_header_type) {
1112 BT_LOGD("Creating stream's packet header field: "
1113 "ft-addr=%p", trace->packet_header_type);
1114 stream->packet_header =
1115 bt_field_create(trace->packet_header_type);
1116 if (!stream->packet_header) {
1117 BT_LOGW_STR("Cannot create stream's packet header field.");
1118 goto error;
1119 }
1120 }
1121
1122 /*
1123 * Attempt to populate the default trace packet header fields
1124 * (magic, uuid and stream_id). This will _not_ fail shall the
1125 * fields not be found or be of an incompatible type; they will
1126 * simply not be populated automatically. The user will have to
1127 * make sure to set the trace packet header fields himself
1128 * before flushing.
1129 */
1130 ret = auto_populate_packet_header(stream);
1131 if (ret) {
1132 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1133 goto error;
1134 }
1135
1136 /* Create file associated with this stream */
1137 fd = create_stream_file(writer, stream);
1138 if (fd < 0) {
1139 BT_LOGW_STR("Cannot create stream file.");
1140 goto error;
1141 }
1142
1143 set_stream_fd(stream, fd);
1144
1145 /* Freeze the writer */
1146 BT_LOGD_STR("Freezing stream's CTF writer.");
1147 bt_ctf_writer_freeze(writer);
1148 } else {
1149 /* Non-writer stream indicated by a negative FD */
1150 set_stream_fd(stream, -1);
1151 stream->comp_cur_port = g_hash_table_new(g_direct_hash,
1152 g_direct_equal);
1153 if (!stream->comp_cur_port) {
1154 BT_LOGE_STR("Failed to allocate a GHashTable.");
1155 goto error;
1156 }
1157 }
1158
1159 /* Add this stream to the trace's streams */
1160 g_ptr_array_add(trace->streams, stream);
1161 BT_LOGD("Created stream object: addr=%p", stream);
1162 goto end;
1163
1164 error:
1165 BT_PUT(stream);
1166
1167 end:
1168 bt_put(writer);
1169 return stream;
1170 }
1171
1172 struct bt_stream *bt_stream_create_with_id(
1173 struct bt_stream_class *stream_class,
1174 const char *name, uint64_t id_param)
1175 {
1176 struct bt_trace *trace;
1177 struct bt_stream *stream = NULL;
1178 int64_t id = (int64_t) id_param;
1179
1180 if (!stream_class) {
1181 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1182 goto end;
1183 }
1184
1185 if (id < 0) {
1186 BT_LOGW("Invalid parameter: invalid stream's ID: "
1187 "name=\"%s\", id=%" PRIu64,
1188 name, id_param);
1189 goto end;
1190 }
1191
1192 trace = bt_stream_class_borrow_trace(stream_class);
1193 if (!trace) {
1194 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1195 "stream-class-addr=%p, stream-class-name=\"%s\", "
1196 "stream-name=\"%s\", stream-id=%" PRIu64,
1197 stream_class, bt_stream_class_get_name(stream_class),
1198 name, id_param);
1199 goto end;
1200 }
1201
1202 if (trace->is_created_by_writer) {
1203 BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_stream_create(): "
1204 "stream-class-addr=%p, stream-class-name=\"%s\", "
1205 "stream-name=\"%s\", stream-id=%" PRIu64,
1206 stream_class, bt_stream_class_get_name(stream_class),
1207 name, id_param);
1208 goto end;
1209 }
1210
1211 stream = bt_stream_create_with_id_no_check(stream_class,
1212 name, id_param);
1213
1214 end:
1215 return stream;
1216 }
1217
1218 struct bt_stream *bt_stream_create(
1219 struct bt_stream_class *stream_class,
1220 const char *name)
1221 {
1222 return bt_stream_create_with_id_no_check(stream_class,
1223 name, -1ULL);
1224 }
1225
1226 struct bt_stream_class *bt_stream_get_class(
1227 struct bt_stream *stream)
1228 {
1229 struct bt_stream_class *stream_class = NULL;
1230
1231 if (!stream) {
1232 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1233 goto end;
1234 }
1235
1236 stream_class = stream->stream_class;
1237 bt_get(stream_class);
1238 end:
1239 return stream_class;
1240 }
1241
1242 int bt_stream_get_discarded_events_count(
1243 struct bt_stream *stream, uint64_t *count)
1244 {
1245 int ret = 0;
1246
1247 if (!stream) {
1248 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1249 ret = -1;
1250 goto end;
1251 }
1252
1253 if (!count) {
1254 BT_LOGW_STR("Invalid parameter: count is NULL.");
1255 ret = -1;
1256 goto end;
1257 }
1258
1259 if (stream->pos.fd < 0) {
1260 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1261 "stream-addr=%p, stream-name=\"%s\"",
1262 stream, bt_stream_get_name(stream));
1263 ret = -1;
1264 goto end;
1265 }
1266
1267 *count = (uint64_t) stream->discarded_events;
1268
1269 end:
1270 return ret;
1271 }
1272
1273 static
1274 int set_packet_context_events_discarded_field(struct bt_stream *stream,
1275 uint64_t count)
1276 {
1277 int ret = 0;
1278 struct bt_field *events_discarded_field = NULL;
1279
1280 if (!stream->packet_context) {
1281 goto end;
1282 }
1283
1284 events_discarded_field = bt_field_structure_get_field_by_name(
1285 stream->packet_context, "events_discarded");
1286 if (!events_discarded_field) {
1287 goto end;
1288 }
1289
1290 ret = bt_field_unsigned_integer_set_value(
1291 events_discarded_field, count);
1292 if (ret) {
1293 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1294 "field-addr=%p, value=%" PRIu64,
1295 events_discarded_field, count);
1296 goto end;
1297 }
1298
1299 end:
1300 bt_put(events_discarded_field);
1301 return ret;
1302 }
1303
1304 void bt_stream_append_discarded_events(struct bt_stream *stream,
1305 uint64_t event_count)
1306 {
1307 int ret;
1308 uint64_t new_count;
1309 struct bt_field *events_discarded_field = NULL;
1310
1311 if (!stream) {
1312 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1313 goto end;
1314 }
1315
1316 BT_LOGV("Appending discarded events to stream: "
1317 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1318 stream, bt_stream_get_name(stream), event_count);
1319
1320 if (!stream->packet_context) {
1321 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1322 goto end;
1323 }
1324
1325 if (stream->pos.fd < 0) {
1326 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1327 goto end;
1328 }
1329
1330 events_discarded_field = bt_field_structure_get_field_by_name(
1331 stream->packet_context, "events_discarded");
1332 if (!events_discarded_field) {
1333 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1334 goto end;
1335 }
1336
1337 new_count = stream->discarded_events + event_count;
1338 if (new_count < stream->discarded_events) {
1339 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1340 "cur-count=%" PRIu64 ", new-count=%" PRIu64,
1341 stream->discarded_events, new_count);
1342 goto end;
1343 }
1344
1345 ret = set_packet_context_events_discarded_field(stream, new_count);
1346 if (ret) {
1347 /* set_packet_context_events_discarded_field() logs errors */
1348 goto end;
1349 }
1350
1351 stream->discarded_events = new_count;
1352 BT_LOGV("Appended discarded events to stream: "
1353 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1354 stream, bt_stream_get_name(stream), event_count);
1355
1356 end:
1357 bt_put(events_discarded_field);
1358 }
1359
1360 static int auto_populate_event_header(struct bt_stream *stream,
1361 struct bt_event *event)
1362 {
1363 int ret = 0;
1364 struct bt_field *id_field = NULL, *timestamp_field = NULL;
1365 struct bt_clock_class *mapped_clock_class = NULL;
1366 int64_t event_class_id;
1367
1368 BT_ASSERT(event);
1369
1370 if (!event->event_header) {
1371 goto end;
1372 }
1373
1374 if (event->frozen) {
1375 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1376 ret = -1;
1377 goto end;
1378 }
1379
1380 BT_LOGV("Automatically populating event's header field: "
1381 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1382 stream, bt_stream_get_name(stream), event);
1383
1384 id_field = bt_field_structure_get_field_by_name(event->event_header, "id");
1385 event_class_id = bt_event_class_get_id(event->event_class);
1386 BT_ASSERT(event_class_id >= 0);
1387
1388 if (id_field && bt_field_type_is_integer(id_field->type)) {
1389 ret = set_integer_field_value(id_field, event_class_id);
1390 if (ret) {
1391 BT_LOGW("Cannot set event header's `id` field's value: "
1392 "addr=%p, value=%" PRIu64, id_field,
1393 event_class_id);
1394 goto end;
1395 }
1396 }
1397
1398 /*
1399 * The conditions to automatically set the timestamp are:
1400 *
1401 * 1. The event header field "timestamp" exists and is an
1402 * integer field.
1403 * 2. This stream's class has a registered clock (set with
1404 * bt_stream_class_set_clock()).
1405 * 3. The "timestamp" field is not set.
1406 */
1407 timestamp_field = bt_field_structure_get_field_by_name(
1408 event->event_header, "timestamp");
1409 if (timestamp_field && stream->stream_class->clock &&
1410 bt_field_type_is_integer(timestamp_field->type) &&
1411 !bt_field_is_set_recursive(timestamp_field)) {
1412 mapped_clock_class =
1413 bt_field_type_integer_get_mapped_clock_class(
1414 timestamp_field->type);
1415 if (mapped_clock_class) {
1416 uint64_t timestamp;
1417
1418 BT_ASSERT(mapped_clock_class ==
1419 stream->stream_class->clock->clock_class);
1420 ret = bt_ctf_clock_get_value(
1421 stream->stream_class->clock,
1422 &timestamp);
1423 BT_ASSERT(ret == 0);
1424 ret = set_integer_field_value(timestamp_field,
1425 timestamp);
1426 if (ret) {
1427 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1428 "addr=%p, value=%" PRIu64,
1429 timestamp_field, timestamp);
1430 goto end;
1431 }
1432 }
1433 }
1434
1435 BT_LOGV("Automatically populated event's header field: "
1436 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1437 stream, bt_stream_get_name(stream), event);
1438
1439 end:
1440 bt_put(id_field);
1441 bt_put(timestamp_field);
1442 bt_put(mapped_clock_class);
1443 return ret;
1444 }
1445
1446 int bt_stream_append_event(struct bt_stream *stream,
1447 struct bt_event *event)
1448 {
1449 int ret = 0;
1450
1451 if (!stream) {
1452 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1453 ret = -1;
1454 goto end;
1455 }
1456
1457 if (!event) {
1458 BT_LOGW_STR("Invalid parameter: event is NULL.");
1459 ret = -1;
1460 goto end;
1461 }
1462
1463 if (stream->pos.fd < 0) {
1464 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1465 ret = -1;
1466 goto end;
1467 }
1468
1469 BT_LOGV("Appending event to stream: "
1470 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1471 "event-class-name=\"%s\", event-class-id=%" PRId64,
1472 stream, bt_stream_get_name(stream), event,
1473 bt_event_class_get_name(bt_event_borrow_event_class(event)),
1474 bt_event_class_get_id(bt_event_borrow_event_class(event)));
1475
1476 /*
1477 * The event is not supposed to have a parent stream at this
1478 * point. The only other way an event can have a parent stream
1479 * is if it was assigned when setting a packet to the event,
1480 * in which case the packet's stream is not a writer stream,
1481 * and thus the user is trying to append an event which belongs
1482 * to another stream.
1483 */
1484 if (event->base.parent) {
1485 ret = -1;
1486 goto end;
1487 }
1488
1489 bt_object_set_parent(event, stream);
1490 BT_LOGV_STR("Automatically populating the header of the event to append.");
1491 ret = auto_populate_event_header(stream, event);
1492 if (ret) {
1493 /* auto_populate_event_header() reports errors */
1494 goto error;
1495 }
1496
1497 /* Make sure the various scopes of the event are set */
1498 BT_LOGV_STR("Validating event to append.");
1499 ret = bt_event_validate(event);
1500 if (ret) {
1501 goto error;
1502 }
1503
1504 /* Save the new event and freeze it */
1505 BT_LOGV_STR("Freezing the event to append.");
1506 bt_event_freeze(event);
1507 g_ptr_array_add(stream->events, event);
1508
1509 /*
1510 * Event had to hold a reference to its event class as long as it wasn't
1511 * part of the same trace hierarchy. From now on, the event and its
1512 * class share the same lifetime guarantees and the reference is no
1513 * longer needed.
1514 */
1515 BT_LOGV_STR("Putting the event's class.");
1516 bt_put(event->event_class);
1517 BT_LOGV("Appended event to stream: "
1518 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1519 "event-class-name=\"%s\", event-class-id=%" PRId64,
1520 stream, bt_stream_get_name(stream), event,
1521 bt_event_class_get_name(bt_event_borrow_event_class(event)),
1522 bt_event_class_get_id(bt_event_borrow_event_class(event)));
1523
1524 end:
1525 return ret;
1526
1527 error:
1528 /*
1529 * Orphan the event; we were not successful in associating it to
1530 * a stream.
1531 */
1532 bt_object_set_parent(event, NULL);
1533
1534 return ret;
1535 }
1536
1537 struct bt_field *bt_stream_get_packet_context(struct bt_stream *stream)
1538 {
1539 struct bt_field *packet_context = NULL;
1540
1541 if (!stream) {
1542 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1543 goto end;
1544 }
1545
1546 if (stream->pos.fd < 0) {
1547 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1548 "stream-addr=%p, stream-name=\"%s\"", stream,
1549 bt_stream_get_name(stream));
1550 goto end;
1551 }
1552
1553 packet_context = stream->packet_context;
1554 if (packet_context) {
1555 bt_get(packet_context);
1556 }
1557 end:
1558 return packet_context;
1559 }
1560
1561 int bt_stream_set_packet_context(struct bt_stream *stream,
1562 struct bt_field *field)
1563 {
1564 int ret = 0;
1565 struct bt_field_type *field_type;
1566
1567 if (!stream) {
1568 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1569 ret = -1;
1570 goto end;
1571 }
1572
1573 if (stream->pos.fd < 0) {
1574 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1575 ret = -1;
1576 goto end;
1577 }
1578
1579 field_type = bt_field_get_type(field);
1580 if (bt_field_type_compare(field_type,
1581 stream->stream_class->packet_context_type)) {
1582 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1583 "stream-addr=%p, stream-name=\"%s\", "
1584 "packet-context-field-addr=%p, "
1585 "packet-context-ft-addr=%p",
1586 stream, bt_stream_get_name(stream),
1587 field, field_type);
1588 ret = -1;
1589 goto end;
1590 }
1591
1592 bt_put(field_type);
1593 bt_put(stream->packet_context);
1594 stream->packet_context = bt_get(field);
1595 BT_LOGV("Set stream's packet context field: "
1596 "stream-addr=%p, stream-name=\"%s\", "
1597 "packet-context-field-addr=%p",
1598 stream, bt_stream_get_name(stream), field);
1599 end:
1600 return ret;
1601 }
1602
1603 struct bt_field *bt_stream_get_packet_header(struct bt_stream *stream)
1604 {
1605 struct bt_field *packet_header = NULL;
1606
1607 if (!stream) {
1608 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1609 goto end;
1610 }
1611
1612 if (stream->pos.fd < 0) {
1613 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1614 "stream-addr=%p, stream-name=\"%s\"", stream,
1615 bt_stream_get_name(stream));
1616 goto end;
1617 }
1618
1619 packet_header = stream->packet_header;
1620 if (packet_header) {
1621 bt_get(packet_header);
1622 }
1623 end:
1624 return packet_header;
1625 }
1626
1627 int bt_stream_set_packet_header(struct bt_stream *stream,
1628 struct bt_field *field)
1629 {
1630 int ret = 0;
1631 struct bt_trace *trace = NULL;
1632 struct bt_field_type *field_type = NULL;
1633
1634 if (!stream) {
1635 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1636 ret = -1;
1637 goto end;
1638 }
1639
1640 if (stream->pos.fd < 0) {
1641 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1642 ret = -1;
1643 goto end;
1644 }
1645
1646 trace = (struct bt_trace *) bt_object_get_parent(stream);
1647
1648 if (!field) {
1649 if (trace->packet_header_type) {
1650 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1651 "stream-addr=%p, stream-name=\"%s\", "
1652 "packet-header-field-addr=%p, "
1653 "expected-ft-addr=%p",
1654 stream, bt_stream_get_name(stream),
1655 field, trace->packet_header_type);
1656 ret = -1;
1657 goto end;
1658 }
1659
1660 goto skip_validation;
1661 }
1662
1663 field_type = bt_field_get_type(field);
1664 BT_ASSERT(field_type);
1665
1666 if (bt_field_type_compare(field_type, trace->packet_header_type)) {
1667 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1668 "stream-addr=%p, stream-name=\"%s\", "
1669 "packet-header-field-addr=%p, "
1670 "packet-header-ft-addr=%p",
1671 stream, bt_stream_get_name(stream),
1672 field, field_type);
1673 ret = -1;
1674 goto end;
1675 }
1676
1677 skip_validation:
1678 bt_put(stream->packet_header);
1679 stream->packet_header = bt_get(field);
1680 BT_LOGV("Set stream's packet header field: "
1681 "stream-addr=%p, stream-name=\"%s\", "
1682 "packet-header-field-addr=%p",
1683 stream, bt_stream_get_name(stream), field);
1684 end:
1685 BT_PUT(trace);
1686 bt_put(field_type);
1687 return ret;
1688 }
1689
1690 static
1691 void reset_structure_field(struct bt_field *structure, const char *name)
1692 {
1693 struct bt_field *member;
1694
1695 member = bt_field_structure_get_field_by_name(structure, name);
1696 if (member) {
1697 (void) bt_field_reset_recursive(member);
1698 bt_put(member);
1699 }
1700 }
1701
1702 int bt_stream_flush(struct bt_stream *stream)
1703 {
1704 int ret = 0;
1705 size_t i;
1706 struct bt_stream_pos packet_context_pos;
1707 struct bt_trace *trace;
1708 enum bt_byte_order native_byte_order;
1709 bool has_packet_size = false;
1710
1711 if (!stream) {
1712 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1713 ret = -1;
1714 goto end_no_stream;
1715 }
1716
1717 if (stream->pos.fd < 0) {
1718 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1719 ret = -1;
1720 goto end;
1721 }
1722
1723 if (stream->packet_context) {
1724 struct bt_field *packet_size_field;
1725
1726 packet_size_field = bt_field_structure_get_field_by_name(
1727 stream->packet_context, "packet_size");
1728 has_packet_size = (packet_size_field != NULL);
1729 bt_put(packet_size_field);
1730 }
1731
1732 if (stream->flushed_packet_count == 1) {
1733 if (!stream->packet_context) {
1734 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1735 ret = -1;
1736 goto end;
1737 }
1738
1739 if (!has_packet_size) {
1740 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1741 ret = -1;
1742 goto end;
1743 }
1744 }
1745
1746 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1747 "stream-name=\"%s\", packet-index=%u", stream,
1748 bt_stream_get_name(stream), stream->flushed_packet_count);
1749 trace = bt_stream_class_borrow_trace(stream->stream_class);
1750 BT_ASSERT(trace);
1751 native_byte_order = bt_trace_get_native_byte_order(trace);
1752
1753 ret = auto_populate_packet_header(stream);
1754 if (ret) {
1755 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1756 ret = -1;
1757 goto end;
1758 }
1759
1760 ret = auto_populate_packet_context(stream, true);
1761 if (ret) {
1762 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1763 ret = -1;
1764 goto end;
1765 }
1766
1767 /* mmap the next packet */
1768 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64,
1769 stream->pos.offset);
1770 bt_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR);
1771 BT_ASSERT(stream->pos.packet_size % 8 == 0);
1772
1773 if (stream->packet_header) {
1774 BT_LOGV_STR("Serializing packet header field.");
1775 ret = bt_field_serialize_recursive(stream->packet_header,
1776 &stream->pos, native_byte_order);
1777 if (ret) {
1778 BT_LOGW("Cannot serialize stream's packet header field: "
1779 "field-addr=%p", stream->packet_header);
1780 goto end;
1781 }
1782 }
1783
1784 if (stream->packet_context) {
1785 /* Write packet context */
1786 memcpy(&packet_context_pos, &stream->pos,
1787 sizeof(packet_context_pos));
1788 BT_LOGV_STR("Serializing packet context field.");
1789 ret = bt_field_serialize_recursive(stream->packet_context,
1790 &stream->pos, native_byte_order);
1791 if (ret) {
1792 BT_LOGW("Cannot serialize stream's packet context field: "
1793 "field-addr=%p", stream->packet_context);
1794 goto end;
1795 }
1796 }
1797
1798 BT_LOGV("Serializing events: count=%u", stream->events->len);
1799
1800 for (i = 0; i < stream->events->len; i++) {
1801 struct bt_event *event = g_ptr_array_index(
1802 stream->events, i);
1803 struct bt_event_class *event_class =
1804 bt_event_borrow_event_class(event);
1805
1806 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1807 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1808 "pos-offset=%" PRId64 ", packet-size=%" PRIu64,
1809 i, event, bt_event_class_get_name(event_class),
1810 bt_event_class_get_id(event_class),
1811 stream->pos.offset, stream->pos.packet_size);
1812
1813 /* Write event header */
1814 if (event->event_header) {
1815 BT_LOGV_STR("Serializing event's header field.");
1816 ret = bt_field_serialize_recursive(event->event_header,
1817 &stream->pos, native_byte_order);
1818 if (ret) {
1819 BT_LOGW("Cannot serialize event's header field: "
1820 "field-addr=%p", event->event_header);
1821 goto end;
1822 }
1823 }
1824
1825 /* Write stream event context */
1826 if (event->stream_event_context) {
1827 BT_LOGV_STR("Serializing event's stream event context field.");
1828 ret = bt_field_serialize_recursive(
1829 event->stream_event_context, &stream->pos,
1830 native_byte_order);
1831 if (ret) {
1832 BT_LOGW("Cannot serialize event's stream event context field: "
1833 "field-addr=%p", event->stream_event_context);
1834 goto end;
1835 }
1836 }
1837
1838 /* Write event content */
1839 ret = bt_event_serialize(event, &stream->pos,
1840 native_byte_order);
1841 if (ret) {
1842 /* bt_event_serialize() logs errors */
1843 goto end;
1844 }
1845 }
1846
1847 if (!has_packet_size && stream->pos.offset % 8 != 0) {
1848 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1849 "but current content size is not a multiple of 8 bits: "
1850 "content-size=%" PRId64 ", "
1851 "packet-size=%" PRIu64,
1852 stream->pos.offset,
1853 stream->pos.packet_size);
1854 ret = -1;
1855 goto end;
1856 }
1857
1858 BT_ASSERT(stream->pos.packet_size % 8 == 0);
1859
1860 /*
1861 * Remove extra padding bytes.
1862 */
1863 stream->pos.packet_size = (stream->pos.offset + 7) & ~7;
1864
1865 if (stream->packet_context) {
1866 /*
1867 * The whole packet is serialized at this point. Make sure that,
1868 * if `packet_size` is missing, the current content size is
1869 * equal to the current packet size.
1870 */
1871 struct bt_field *field = bt_field_structure_get_field_by_name(
1872 stream->packet_context, "content_size");
1873
1874 bt_put(field);
1875 if (!field) {
1876 if (stream->pos.offset != stream->pos.packet_size) {
1877 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1878 "but current packet's content size is not equal to its packet size: "
1879 "content-size=%" PRId64 ", "
1880 "packet-size=%" PRIu64,
1881 stream->pos.offset,
1882 stream->pos.packet_size);
1883 ret = -1;
1884 goto end;
1885 }
1886 }
1887
1888 /*
1889 * Overwrite the packet context now that the stream
1890 * position's packet and content sizes have the correct
1891 * values.
1892 *
1893 * Copy base_mma as the packet may have been remapped
1894 * (e.g. when a packet is resized).
1895 */
1896 packet_context_pos.base_mma = stream->pos.base_mma;
1897 ret = auto_populate_packet_context(stream, false);
1898 if (ret) {
1899 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1900 ret = -1;
1901 goto end;
1902 }
1903
1904 BT_LOGV("Rewriting (serializing) packet context field.");
1905 ret = bt_field_serialize_recursive(stream->packet_context,
1906 &packet_context_pos, native_byte_order);
1907 if (ret) {
1908 BT_LOGW("Cannot serialize stream's packet context field: "
1909 "field-addr=%p", stream->packet_context);
1910 goto end;
1911 }
1912 }
1913
1914 g_ptr_array_set_size(stream->events, 0);
1915 stream->flushed_packet_count++;
1916 stream->size += stream->pos.packet_size / CHAR_BIT;
1917
1918 end:
1919 /* Reset automatically-set fields. */
1920 if (stream->packet_context) {
1921 reset_structure_field(stream->packet_context, "timestamp_begin");
1922 reset_structure_field(stream->packet_context, "timestamp_end");
1923 reset_structure_field(stream->packet_context, "packet_size");
1924 reset_structure_field(stream->packet_context, "content_size");
1925 reset_structure_field(stream->packet_context, "events_discarded");
1926 }
1927
1928 if (ret < 0) {
1929 /*
1930 * We failed to write the packet. Its size is therefore set to 0
1931 * to ensure the next mapping is done in the same place rather
1932 * than advancing by "stream->pos.packet_size", which would
1933 * leave a corrupted packet in the trace.
1934 */
1935 stream->pos.packet_size = 0;
1936 } else {
1937 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", "
1938 "packet-size=%" PRIu64,
1939 stream->pos.offset, stream->pos.packet_size);
1940 }
1941
1942 end_no_stream:
1943 return ret;
1944 }
1945
1946 /* Pre-2.0 CTF writer backward compatibility */
1947 void bt_ctf_stream_get(struct bt_stream *stream)
1948 {
1949 bt_get(stream);
1950 }
1951
1952 /* Pre-2.0 CTF writer backward compatibility */
1953 void bt_ctf_stream_put(struct bt_stream *stream)
1954 {
1955 bt_put(stream);
1956 }
1957
1958 static
1959 void bt_stream_destroy(struct bt_object *obj)
1960 {
1961 struct bt_stream *stream;
1962 int i;
1963
1964 stream = container_of(obj, struct bt_stream, base);
1965 BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"",
1966 stream, bt_stream_get_name(stream));
1967
1968 /* Call destroy listeners in reverse registration order */
1969 for (i = stream->destroy_listeners->len - 1; i >= 0; i--) {
1970 struct bt_stream_destroy_listener *listener =
1971 &g_array_index(stream->destroy_listeners,
1972 struct bt_stream_destroy_listener, i);
1973
1974 BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d",
1975 listener->func, listener->data, i);
1976 listener->func(stream, listener->data);
1977 }
1978
1979 (void) bt_stream_pos_fini(&stream->pos);
1980 if (stream->pos.fd >= 0) {
1981 int ret;
1982
1983 /*
1984 * Truncate the file's size to the minimum required to fit the
1985 * last packet as we might have grown it too much on the last
1986 * mmap.
1987 */
1988 do {
1989 ret = ftruncate(stream->pos.fd, stream->size);
1990 } while (ret == -1 && errno == EINTR);
1991 if (ret) {
1992 BT_LOGE_ERRNO("Failed to truncate stream file",
1993 ": ret=%d, size=%" PRIu64,
1994 ret, (uint64_t) stream->size);
1995 }
1996
1997 if (close(stream->pos.fd)) {
1998 BT_LOGE_ERRNO("Failed to close stream file",
1999 ": ret=%d", ret);
2000 }
2001 }
2002
2003 if (stream->events) {
2004 BT_LOGD_STR("Putting events.");
2005 g_ptr_array_free(stream->events, TRUE);
2006 }
2007
2008 if (stream->name) {
2009 g_string_free(stream->name, TRUE);
2010 }
2011
2012 if (stream->comp_cur_port) {
2013 GHashTableIter ht_iter;
2014 gpointer comp_gptr, port_gptr;
2015
2016 /*
2017 * Since we're destroying the stream, remove the destroy
2018 * listeners that it registered for each component in
2019 * its component-port mapping hash table. Otherwise they
2020 * would be called and the stream would be accessed once
2021 * it's freed or another stream would be accessed.
2022 */
2023 g_hash_table_iter_init(&ht_iter, stream->comp_cur_port);
2024
2025 while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) {
2026 BT_ASSERT(comp_gptr);
2027 bt_component_remove_destroy_listener((void *) comp_gptr,
2028 component_destroy_listener, stream);
2029 }
2030
2031 g_hash_table_destroy(stream->comp_cur_port);
2032 }
2033
2034 if (stream->destroy_listeners) {
2035 g_array_free(stream->destroy_listeners, TRUE);
2036 }
2037
2038 BT_LOGD_STR("Putting packet header field.");
2039 bt_put(stream->packet_header);
2040 BT_LOGD_STR("Putting packet context field.");
2041 bt_put(stream->packet_context);
2042 g_free(stream);
2043 }
2044
2045 static
2046 int _set_structure_field_integer(struct bt_field *structure, char *name,
2047 uint64_t value, bt_bool force)
2048 {
2049 int ret = 0;
2050 struct bt_field_type *field_type = NULL;
2051 struct bt_field *integer;
2052
2053 BT_ASSERT(structure);
2054 BT_ASSERT(name);
2055
2056 integer = bt_field_structure_get_field_by_name(structure, name);
2057 if (!integer) {
2058 /* Field not found, not an error. */
2059 BT_LOGV("Field not found: struct-field-addr=%p, "
2060 "name=\"%s\", force=%d", structure, name, force);
2061 goto end;
2062 }
2063
2064 /* Make sure the payload has not already been set. */
2065 if (!force && bt_field_is_set_recursive(integer)) {
2066 /* Payload already set, not an error */
2067 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
2068 "name=\"%s\", force=%d", structure, name, force);
2069 goto end;
2070 }
2071
2072 field_type = bt_field_get_type(integer);
2073 BT_ASSERT(field_type);
2074 if (bt_field_type_get_type_id(field_type) != BT_FIELD_TYPE_ID_INTEGER) {
2075 /*
2076 * The user most likely meant for us to populate this field
2077 * automatically. However, we can only do this if the field
2078 * is an integer. Return an error.
2079 */
2080 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
2081 "field-addr=%p, ft-addr=%p, ft-id=%s",
2082 integer, field_type,
2083 bt_field_type_id_string(field_type->id));
2084 ret = -1;
2085 goto end;
2086 }
2087
2088 if (bt_field_type_integer_is_signed(field_type)) {
2089 ret = bt_field_signed_integer_set_value(integer,
2090 (int64_t) value);
2091 } else {
2092 ret = bt_field_unsigned_integer_set_value(integer, value);
2093 }
2094 ret = !ret ? 1 : ret;
2095 end:
2096 bt_put(integer);
2097 bt_put(field_type);
2098 return ret;
2099 }
2100
2101 /*
2102 * Returns the following codes:
2103 * 1 if the field was found and set,
2104 * 0 if nothing was done (field not found, or was already set),
2105 * <0 if an error was encoutered
2106 */
2107 static
2108 int try_set_structure_field_integer(struct bt_field *structure, char *name,
2109 uint64_t value)
2110 {
2111 return _set_structure_field_integer(structure, name, value, BT_FALSE);
2112 }
2113
2114 const char *bt_stream_get_name(struct bt_stream *stream)
2115 {
2116 const char *name = NULL;
2117
2118 if (!stream) {
2119 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2120 goto end;
2121 }
2122
2123 name = stream->name ? stream->name->str : NULL;
2124
2125 end:
2126 return name;
2127 }
2128
2129 int bt_stream_is_writer(struct bt_stream *stream)
2130 {
2131 int ret = -1;
2132
2133 if (!stream) {
2134 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2135 goto end;
2136 }
2137
2138 ret = (stream->pos.fd >= 0);
2139
2140 end:
2141 return ret;
2142 }
2143
2144 BT_HIDDEN
2145 void bt_stream_map_component_to_port(struct bt_stream *stream,
2146 struct bt_component *comp,
2147 struct bt_port *port)
2148 {
2149 BT_ASSERT(stream);
2150 BT_ASSERT(comp);
2151 BT_ASSERT(port);
2152 BT_ASSERT(stream->comp_cur_port);
2153
2154 /*
2155 * Do not take a reference to the component here because we
2156 * don't want the component to exist as long as this stream
2157 * exists. Instead, keep a weak reference, but add a destroy
2158 * listener so that we remove this hash table entry when we know
2159 * the component is destroyed.
2160 */
2161 BT_LOGV("Adding component's destroy listener for stream: "
2162 "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, "
2163 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2164 stream, bt_stream_get_name(stream),
2165 comp, bt_component_get_name(comp), port,
2166 bt_port_get_name(port));
2167 bt_component_add_destroy_listener(comp, component_destroy_listener,
2168 stream);
2169 g_hash_table_insert(stream->comp_cur_port, comp, port);
2170 BT_LOGV_STR("Mapped component to port for stream.");
2171 }
2172
2173 BT_HIDDEN
2174 struct bt_port *bt_stream_port_for_component(struct bt_stream *stream,
2175 struct bt_component *comp)
2176 {
2177 BT_ASSERT(stream);
2178 BT_ASSERT(comp);
2179 BT_ASSERT(stream->comp_cur_port);
2180 return g_hash_table_lookup(stream->comp_cur_port, comp);
2181 }
2182
2183 BT_HIDDEN
2184 void bt_stream_add_destroy_listener(struct bt_stream *stream,
2185 bt_stream_destroy_listener_func func, void *data)
2186 {
2187 struct bt_stream_destroy_listener listener;
2188
2189 BT_ASSERT(stream);
2190 BT_ASSERT(func);
2191 listener.func = func;
2192 listener.data = data;
2193 g_array_append_val(stream->destroy_listeners, listener);
2194 BT_LOGV("Added stream destroy listener: stream-addr=%p, "
2195 "stream-name=\"%s\", func=%p, data=%p",
2196 stream, bt_stream_get_name(stream), func, data);
2197 }
2198
2199 BT_HIDDEN
2200 void bt_stream_remove_destroy_listener(struct bt_stream *stream,
2201 bt_stream_destroy_listener_func func, void *data)
2202 {
2203 size_t i;
2204
2205 BT_ASSERT(stream);
2206 BT_ASSERT(func);
2207
2208 for (i = 0; i < stream->destroy_listeners->len; i++) {
2209 struct bt_stream_destroy_listener *listener =
2210 &g_array_index(stream->destroy_listeners,
2211 struct bt_stream_destroy_listener, i);
2212
2213 if (listener->func == func && listener->data == data) {
2214 g_array_remove_index(stream->destroy_listeners, i);
2215 i--;
2216 BT_LOGV("Removed stream destroy listener: stream-addr=%p, "
2217 "stream-name=\"%s\", func=%p, data=%p",
2218 stream, bt_stream_get_name(stream),
2219 func, data);
2220 }
2221 }
2222 }
2223
2224 int64_t bt_stream_get_id(struct bt_stream *stream)
2225 {
2226 int64_t ret;
2227
2228 if (!stream) {
2229 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2230 ret = (int64_t) -1;
2231 goto end;
2232 }
2233
2234 ret = stream->id;
2235 if (ret < 0) {
2236 BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"",
2237 stream, bt_stream_get_name(stream));
2238 }
2239
2240 end:
2241 return ret;
2242 }
This page took 0.069314 seconds and 3 git commands to generate.