Fix: event_header is optional
[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 <inttypes.h>
51 #include <unistd.h>
52
53 static
54 void bt_ctf_stream_destroy(struct bt_object *obj);
55 static
56 int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
57
58 static
59 int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
60 {
61 int ret = 0;
62 struct bt_ctf_field_type *field_type = NULL;
63
64 if (!field) {
65 BT_LOGW_STR("Invalid parameter: field is NULL.");
66 ret = -1;
67 goto end;
68 }
69
70 field_type = bt_ctf_field_get_type(field);
71 assert(field_type);
72
73 if (bt_ctf_field_type_get_type_id(field_type) !=
74 BT_CTF_FIELD_TYPE_ID_INTEGER) {
75 /* Not an integer and the value is unset, error. */
76 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
77 "field-addr=%p, ft-addr=%p, ft-id=%s",
78 field, field_type,
79 bt_ctf_field_type_id_string(field_type->id));
80 ret = -1;
81 goto end;
82 }
83
84 if (bt_ctf_field_type_integer_get_signed(field_type)) {
85 ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value);
86 if (ret) {
87 /* Value is out of range, error. */
88 BT_LOGW("Cannot set signed integer field's value: "
89 "addr=%p, value=%" PRId64,
90 field, (int64_t) value);
91 goto end;
92 }
93 } else {
94 ret = bt_ctf_field_unsigned_integer_set_value(field, value);
95 if (ret) {
96 /* Value is out of range, error. */
97 BT_LOGW("Cannot set unsigned integer field's value: "
98 "addr=%p, value=%" PRIu64,
99 field, value);
100 goto end;
101 }
102 }
103 end:
104 bt_put(field_type);
105 return ret;
106 }
107
108 static
109 int set_packet_header_magic(struct bt_ctf_stream *stream)
110 {
111 int ret = 0;
112 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
113 stream->packet_header, "magic");
114 const uint32_t magic_value = 0xc1fc1fc1;
115
116 assert(stream);
117
118 if (!magic_field) {
119 /* No magic field found. Not an error, skip. */
120 BT_LOGV("No field named `magic` in packet header: skipping: "
121 "stream-addr=%p, stream-name=\"%s\"",
122 stream, bt_ctf_stream_get_name(stream));
123 goto end;
124 }
125
126 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
127 (uint64_t) magic_value);
128
129 if (ret) {
130 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
131 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
132 stream, bt_ctf_stream_get_name(stream),
133 magic_field, (uint64_t) magic_value);
134 } else {
135 BT_LOGV("Set packet header field's `magic` field's value: "
136 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
137 stream, bt_ctf_stream_get_name(stream),
138 magic_field, (uint64_t) magic_value);
139 }
140 end:
141 bt_put(magic_field);
142 return ret;
143 }
144
145 static
146 int set_packet_header_uuid(struct bt_ctf_stream *stream)
147 {
148 int ret = 0;
149 int64_t i;
150 struct bt_ctf_trace *trace = NULL;
151 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field(
152 stream->packet_header, "uuid");
153
154 assert(stream);
155
156 if (!uuid_field) {
157 /* No uuid field found. Not an error, skip. */
158 BT_LOGV("No field named `uuid` in packet header: skipping: "
159 "stream-addr=%p, stream-name=\"%s\"",
160 stream, bt_ctf_stream_get_name(stream));
161 goto end;
162 }
163
164 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
165 for (i = 0; i < 16; i++) {
166 struct bt_ctf_field *uuid_element =
167 bt_ctf_field_array_get_field(uuid_field, i);
168
169 ret = bt_ctf_field_unsigned_integer_set_value(
170 uuid_element, (uint64_t) trace->uuid[i]);
171 bt_put(uuid_element);
172 if (ret) {
173 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
174 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
175 "value=%" PRIu64 ", index=%" PRId64,
176 stream, bt_ctf_stream_get_name(stream),
177 uuid_element, (uint64_t) trace->uuid[i], i);
178 goto end;
179 }
180 }
181
182 BT_LOGV("Set packet header field's `uuid` field's value: "
183 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
184 stream, bt_ctf_stream_get_name(stream), uuid_field);
185
186 end:
187 bt_put(uuid_field);
188 BT_PUT(trace);
189 return ret;
190 }
191 static
192 int set_packet_header_stream_id(struct bt_ctf_stream *stream)
193 {
194 int ret = 0;
195 uint32_t stream_id;
196 struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field(
197 stream->packet_header, "stream_id");
198
199 if (!stream_id_field) {
200 /* No stream_id field found. Not an error, skip. */
201 BT_LOGV("No field named `stream_id` in packet header: skipping: "
202 "stream-addr=%p, stream-name=\"%s\"",
203 stream, bt_ctf_stream_get_name(stream));
204 goto end;
205 }
206
207 stream_id = stream->stream_class->id;
208 ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field,
209 (uint64_t) stream_id);
210 if (ret) {
211 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
212 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
213 stream, bt_ctf_stream_get_name(stream),
214 stream_id_field, (uint64_t) stream_id);
215 } else {
216 BT_LOGV("Set packet header field's `stream_id` field's value: "
217 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
218 stream, bt_ctf_stream_get_name(stream),
219 stream_id_field, (uint64_t) stream_id);
220 }
221
222 end:
223 bt_put(stream_id_field);
224 return ret;
225 }
226
227 static
228 int auto_populate_packet_header(struct bt_ctf_stream *stream)
229 {
230 int ret = 0;
231
232 if (!stream->packet_header) {
233 goto end;
234 }
235
236 ret = set_packet_header_magic(stream);
237 if (ret) {
238 BT_LOGW("Cannot set packet header's magic number field: "
239 "stream-addr=%p, stream-name=\"%s\"",
240 stream, bt_ctf_stream_get_name(stream));
241 goto end;
242 }
243
244 ret = set_packet_header_uuid(stream);
245 if (ret) {
246 BT_LOGW("Cannot set packet header's UUID field: "
247 "stream-addr=%p, stream-name=\"%s\"",
248 stream, bt_ctf_stream_get_name(stream));
249 goto end;
250 }
251
252 ret = set_packet_header_stream_id(stream);
253 if (ret) {
254 BT_LOGW("Cannot set packet header's stream class ID field: "
255 "stream-addr=%p, stream-name=\"%s\"",
256 stream, bt_ctf_stream_get_name(stream));
257 goto end;
258 }
259
260 BT_LOGV("Automatically populated stream's packet header's known fields: "
261 "stream-addr=%p, stream-name=\"%s\"",
262 stream, bt_ctf_stream_get_name(stream));
263
264 end:
265 return ret;
266 }
267
268 static
269 int set_packet_context_packet_size(struct bt_ctf_stream *stream)
270 {
271 int ret = 0;
272 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
273 stream->packet_context, "packet_size");
274
275 assert(stream);
276
277 if (!field) {
278 /* No packet size field found. Not an error, skip. */
279 BT_LOGV("No field named `packet_size` in packet context: skipping: "
280 "stream-addr=%p, stream-name=\"%s\"",
281 stream, bt_ctf_stream_get_name(stream));
282 goto end;
283 }
284
285 ret = bt_ctf_field_unsigned_integer_set_value(field,
286 stream->pos.packet_size);
287 if (ret) {
288 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
289 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
290 stream, bt_ctf_stream_get_name(stream),
291 field, stream->pos.packet_size);
292 } else {
293 BT_LOGV("Set packet context field's `packet_size` field's value: "
294 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
295 stream, bt_ctf_stream_get_name(stream),
296 field, stream->pos.packet_size);
297 }
298
299 end:
300 bt_put(field);
301 return ret;
302 }
303
304 static
305 int set_packet_context_content_size(struct bt_ctf_stream *stream)
306 {
307 int ret = 0;
308 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
309 stream->packet_context, "content_size");
310
311 assert(stream);
312
313 if (!field) {
314 /* No content size field found. Not an error, skip. */
315 BT_LOGV("No field named `content_size` in packet context: skipping: "
316 "stream-addr=%p, stream-name=\"%s\"",
317 stream, bt_ctf_stream_get_name(stream));
318 goto end;
319 }
320
321 ret = bt_ctf_field_unsigned_integer_set_value(field,
322 stream->pos.offset);
323 if (ret) {
324 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
325 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64,
326 stream, bt_ctf_stream_get_name(stream),
327 field, stream->pos.offset);
328 } else {
329 BT_LOGV("Set packet context field's `content_size` field's value: "
330 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64,
331 stream, bt_ctf_stream_get_name(stream),
332 field, stream->pos.offset);
333 }
334
335 end:
336 bt_put(field);
337 return ret;
338 }
339
340 static
341 int set_packet_context_events_discarded(struct bt_ctf_stream *stream)
342 {
343 int ret = 0;
344 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
345 stream->packet_context, "events_discarded");
346
347 assert(stream);
348
349 if (!field) {
350 /* No discarded events count field found. Not an error, skip. */
351 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
352 "stream-addr=%p, stream-name=\"%s\"",
353 stream, bt_ctf_stream_get_name(stream));
354 goto end;
355 }
356
357 /*
358 * If the field is set by the user, make sure that the value is
359 * greater than or equal to the stream's current count of
360 * discarded events. We do not allow wrapping here. If it's
361 * valid, update the stream's current count.
362 */
363 if (bt_ctf_field_is_set(field)) {
364 uint64_t user_val;
365
366 ret = bt_ctf_field_unsigned_integer_get_value(field,
367 &user_val);
368 if (ret) {
369 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
370 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
371 stream, bt_ctf_stream_get_name(stream), field);
372 goto end;
373 }
374
375 if (user_val < stream->discarded_events) {
376 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
377 "value is lesser than the stream's current discarded events count: "
378 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
379 "value=%" PRIu64 ", "
380 "stream-discarded-events-count=%" PRIu64,
381 stream, bt_ctf_stream_get_name(stream), field,
382 user_val, stream->discarded_events);
383 goto end;
384 }
385
386 stream->discarded_events = user_val;
387 } else {
388 ret = bt_ctf_field_unsigned_integer_set_value(field,
389 stream->discarded_events);
390 if (ret) {
391 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
392 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
393 stream, bt_ctf_stream_get_name(stream),
394 field, stream->discarded_events);
395 } else {
396 BT_LOGV("Set packet context field's `events_discarded` field's value: "
397 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
398 stream, bt_ctf_stream_get_name(stream),
399 field, stream->discarded_events);
400 }
401 }
402
403 end:
404 bt_put(field);
405 return ret;
406 }
407
408 static
409 int get_event_header_timestamp(struct bt_ctf_stream *stream,
410 struct bt_ctf_field *event_header, uint64_t *timestamp)
411 {
412 int ret = 0;
413 struct bt_ctf_field *timestamp_field = NULL;
414 struct bt_ctf_clock_class *ts_field_mapped_clock_class = NULL;
415
416 *timestamp = 0;
417
418 if (!event_header) {
419 BT_LOGV_STR("Event header does not exist.");
420 goto end;
421 }
422
423 timestamp_field = bt_ctf_field_structure_get_field(event_header,
424 "timestamp");
425 if (!timestamp_field) {
426 BT_LOGV("Cannot get event header's `timestamp` field: "
427 "event-header-field-addr=%p", event_header);
428 goto end;
429 }
430
431 if (!bt_ctf_field_type_is_integer(timestamp_field->type)) {
432 BT_LOGV("Event header's `timestamp` field's type is not an integer field type: "
433 "event-header-field-addr=%p", event_header);
434 goto end;
435 }
436
437 ts_field_mapped_clock_class =
438 bt_ctf_field_type_integer_get_mapped_clock_class(
439 timestamp_field->type);
440 if (!ts_field_mapped_clock_class) {
441 BT_LOGV("Event header's `timestamp` field's type is not mapped to a clock class: "
442 "event-header-field-addr=%p", event_header);
443 goto end;
444 }
445
446 if (ts_field_mapped_clock_class !=
447 stream->stream_class->clock->clock_class) {
448 BT_LOGV("Event header's `timestamp` field's type is not mapped to the stream's clock's class: "
449 "event-header-field-addr=%p", event_header);
450 goto end;
451 }
452
453 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
454 timestamp);
455 if (ret) {
456 BT_LOGW("Cannot get unsigned integer field's value: "
457 "event-header-field-addr=%p, "
458 "timestamp-field-addr=%p",
459 event_header, timestamp_field);
460 goto end;
461 }
462
463 end:
464 bt_put(timestamp_field);
465 bt_put(ts_field_mapped_clock_class);
466 return ret;
467 }
468
469 static
470 int set_packet_context_timestamp_field(struct bt_ctf_stream *stream,
471 const char *field_name, struct bt_ctf_event *event)
472 {
473 int ret = 0;
474 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
475 stream->packet_context, field_name);
476 struct bt_ctf_clock_class *field_mapped_clock_class = NULL;
477 uint64_t ts;
478
479 assert(stream);
480
481 if (!field) {
482 /* No beginning timestamp field found. Not an error, skip. */
483 BT_LOGV("No field named `%s` in packet context: skipping: "
484 "stream-addr=%p, stream-name=\"%s\"", field_name,
485 stream, bt_ctf_stream_get_name(stream));
486 goto end;
487 }
488
489 if (!stream->stream_class->clock) {
490 BT_LOGV("Stream has no clock: skipping: "
491 "stream-addr=%p, stream-name=\"%s\"",
492 stream, bt_ctf_stream_get_name(stream));
493 goto end;
494 }
495
496 field_mapped_clock_class =
497 bt_ctf_field_type_integer_get_mapped_clock_class(field->type);
498 if (!field_mapped_clock_class) {
499 BT_LOGV("Packet context's `%s` field's type is not mapped to a clock class: skipping: "
500 "stream-addr=%p, stream-name=\"%s\", "
501 "field-addr=%p, ft-addr=%p", field_name,
502 stream, bt_ctf_stream_get_name(stream),
503 field, field->type);
504 goto end;
505 }
506
507 if (field_mapped_clock_class !=
508 stream->stream_class->clock->clock_class) {
509 BT_LOGV("Packet context's `%s` field's type is not mapped to the stream's clock's class: skipping: "
510 "stream-addr=%p, stream-name=\"%s\", "
511 "field-addr=%p, ft-addr=%p, "
512 "ft-mapped-clock-class-addr=%p, "
513 "ft-mapped-clock-class-name=\"%s\", "
514 "stream-clock-class-addr=%p, "
515 "stream-clock-class-name=\"%s\"",
516 field_name,
517 stream, bt_ctf_stream_get_name(stream),
518 field, field->type,
519 field_mapped_clock_class,
520 bt_ctf_clock_class_get_name(field_mapped_clock_class),
521 stream->stream_class->clock->clock_class,
522 bt_ctf_clock_class_get_name(
523 stream->stream_class->clock->clock_class));
524 goto end;
525 }
526
527 if (get_event_header_timestamp(stream, event->event_header, &ts)) {
528 BT_LOGW("Cannot get event's timestamp: "
529 "event-header-field-addr=%p",
530 event->event_header);
531 ret = -1;
532 goto end;
533 }
534
535 ret = bt_ctf_field_unsigned_integer_set_value(field, ts);
536 if (ret) {
537 BT_LOGW("Cannot set packet context field's `%s` integer field's value: "
538 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
539 field_name, stream, bt_ctf_stream_get_name(stream),
540 field, stream->discarded_events);
541 } else {
542 BT_LOGV("Set packet context field's `%s` field's value: "
543 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
544 field_name, stream, bt_ctf_stream_get_name(stream),
545 field, stream->discarded_events);
546 }
547
548 end:
549 bt_put(field);
550 bt_put(field_mapped_clock_class);
551 return ret;
552 }
553
554 static
555 int set_packet_context_timestamp_begin(struct bt_ctf_stream *stream)
556 {
557 int ret = 0;
558
559 if (stream->events->len == 0) {
560 BT_LOGV("Current packet contains no events: skipping: "
561 "stream-addr=%p, stream-name=\"%s\"",
562 stream, bt_ctf_stream_get_name(stream));
563 goto end;
564 }
565
566 ret = set_packet_context_timestamp_field(stream, "timestamp_begin",
567 g_ptr_array_index(stream->events, 0));
568
569 end:
570 return ret;
571 }
572
573 static
574 int set_packet_context_timestamp_end(struct bt_ctf_stream *stream)
575 {
576 int ret = 0;
577
578 if (stream->events->len == 0) {
579 BT_LOGV("Current packet contains no events: skipping: "
580 "stream-addr=%p, stream-name=\"%s\"",
581 stream, bt_ctf_stream_get_name(stream));
582 goto end;
583 }
584
585 ret = set_packet_context_timestamp_field(stream, "timestamp_end",
586 g_ptr_array_index(stream->events, stream->events->len - 1));
587
588 end:
589 return ret;
590 }
591
592 static
593 int auto_populate_packet_context(struct bt_ctf_stream *stream)
594 {
595 int ret = 0;
596
597 if (!stream->packet_context) {
598 goto end;
599 }
600
601 ret = set_packet_context_packet_size(stream);
602 if (ret) {
603 BT_LOGW("Cannot set packet context's packet size field: "
604 "stream-addr=%p, stream-name=\"%s\"",
605 stream, bt_ctf_stream_get_name(stream));
606 goto end;
607 }
608
609 ret = set_packet_context_content_size(stream);
610 if (ret) {
611 BT_LOGW("Cannot set packet context's content size field: "
612 "stream-addr=%p, stream-name=\"%s\"",
613 stream, bt_ctf_stream_get_name(stream));
614 goto end;
615 }
616
617 ret = set_packet_context_timestamp_begin(stream);
618 if (ret) {
619 BT_LOGW("Cannot set packet context's beginning timestamp field: "
620 "stream-addr=%p, stream-name=\"%s\"",
621 stream, bt_ctf_stream_get_name(stream));
622 goto end;
623 }
624
625 ret = set_packet_context_timestamp_end(stream);
626 if (ret) {
627 BT_LOGW("Cannot set packet context's end timestamp field: "
628 "stream-addr=%p, stream-name=\"%s\"",
629 stream, bt_ctf_stream_get_name(stream));
630 goto end;
631 }
632
633 ret = set_packet_context_events_discarded(stream);
634 if (ret) {
635 BT_LOGW("Cannot set packet context's discarded events count field: "
636 "stream-addr=%p, stream-name=\"%s\"",
637 stream, bt_ctf_stream_get_name(stream));
638 goto end;
639 }
640
641 BT_LOGV("Automatically populated stream's packet context's known fields: "
642 "stream-addr=%p, stream-name=\"%s\"",
643 stream, bt_ctf_stream_get_name(stream));
644
645 end:
646 return ret;
647 }
648
649 static
650 void release_event(struct bt_ctf_event *event)
651 {
652 if (bt_object_get_ref_count(event)) {
653 /*
654 * The event is being orphaned, but it must guarantee the
655 * existence of its event class for the duration of its
656 * lifetime.
657 */
658 bt_get(event->event_class);
659 BT_PUT(event->base.parent);
660 } else {
661 bt_object_release(event);
662 }
663 }
664
665 static
666 int create_stream_file(struct bt_ctf_writer *writer,
667 struct bt_ctf_stream *stream)
668 {
669 int fd;
670 GString *filename = g_string_new(stream->stream_class->name->str);
671
672 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
673 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
674 writer, stream, bt_ctf_stream_get_name(stream),
675 stream->stream_class, stream->stream_class->name->str);
676
677 if (stream->stream_class->name->len == 0) {
678 int64_t ret;
679
680 ret = bt_ctf_stream_class_get_id(stream->stream_class);
681 if (ret < 0) {
682 BT_LOGW("Cannot get stream class's ID: "
683 "stream-class-addr=%p, "
684 "stream-class-name=\"%s\"",
685 stream->stream_class,
686 stream->stream_class->name->str);
687 fd = -1;
688 goto end;
689 }
690
691 g_string_printf(filename, "stream_%" PRId64, ret);
692 }
693
694 g_string_append_printf(filename, "_%" PRId64, stream->id);
695 fd = openat(writer->trace_dir_fd, filename->str,
696 O_RDWR | O_CREAT | O_TRUNC,
697 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
698 if (fd < 0) {
699 BT_LOGW("Failed to open stream file for writing: %s: "
700 "writer-trace-dir-fd=%d, filename=\"%s\", "
701 "ret=%d, errno=%d", strerror(errno),
702 writer->trace_dir_fd, filename->str, fd, errno);
703 goto end;
704 }
705
706 BT_LOGD("Created stream file for writing: "
707 "stream-addr=%p, stream-name=\"%s\", writer-trace-dir-fd=%d, "
708 "filename=\"%s\", fd=%d", stream, bt_ctf_stream_get_name(stream),
709 writer->trace_dir_fd, filename->str, fd);
710
711 end:
712 g_string_free(filename, TRUE);
713 return fd;
714 }
715
716 static
717 void set_stream_fd(struct bt_ctf_stream *stream, int fd)
718 {
719 (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR);
720 stream->pos.fd = fd;
721 }
722
723 static
724 void component_destroy_listener(struct bt_component *component, void *data)
725 {
726 struct bt_ctf_stream *stream = data;
727
728 BT_LOGD("Component is being destroyed, stream is notified: "
729 "comp-addr=%p, stream-addr=%p", component, stream);
730 g_hash_table_remove(stream->comp_cur_port, component);
731 }
732
733 static
734 struct bt_ctf_stream *bt_ctf_stream_create_with_id_no_check(
735 struct bt_ctf_stream_class *stream_class,
736 const char *name, uint64_t id)
737 {
738 int ret;
739 struct bt_ctf_stream *stream = NULL;
740 struct bt_ctf_trace *trace = NULL;
741 struct bt_ctf_writer *writer = NULL;
742
743 if (!stream_class) {
744 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
745 goto error;
746 }
747
748 BT_LOGD("Creating stream object: stream-class-addr=%p, "
749 "stream-class-name=\"%s\", stream-name=\"%s\", "
750 "stream-id=%" PRIu64,
751 stream_class, bt_ctf_stream_class_get_name(stream_class),
752 name, id);
753 trace = bt_ctf_stream_class_borrow_trace(stream_class);
754 if (!trace) {
755 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
756 "stream-class-addr=%p, stream-class-name=\"%s\", "
757 "stream-name=\"%s\"",
758 stream_class, bt_ctf_stream_class_get_name(stream_class),
759 name);
760 goto error;
761 }
762
763 if (bt_ctf_trace_is_static(trace)) {
764 /*
765 * A static trace has the property that all its stream
766 * classes, clock classes, and streams are definitive:
767 * no more can be added, and each object is also frozen.
768 */
769 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: "
770 "stream-class-addr=%p, stream-class-name=\"%s\", "
771 "stream-name=\"%s\", trace-addr=%p",
772 stream_class, bt_ctf_stream_class_get_name(stream_class),
773 name, trace);
774 goto error;
775 }
776
777 if (id != -1ULL) {
778 /*
779 * Validate that the given ID is unique amongst all the
780 * existing trace's streams created from the same stream
781 * class.
782 */
783 size_t i;
784
785 for (i = 0; i < trace->streams->len; i++) {
786 struct bt_ctf_stream *trace_stream =
787 g_ptr_array_index(trace->streams, i);
788
789 if (trace_stream->stream_class != stream_class) {
790 continue;
791 }
792
793 if (trace_stream->id == id) {
794 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
795 goto error;
796 }
797 }
798 }
799
800 stream = g_new0(struct bt_ctf_stream, 1);
801 if (!stream) {
802 BT_LOGE_STR("Failed to allocate one stream.");
803 goto error;
804 }
805
806 bt_object_init(stream, bt_ctf_stream_destroy);
807 /*
808 * Acquire reference to parent since stream will become publicly
809 * reachable; it needs its parent to remain valid.
810 */
811 bt_object_set_parent(stream, trace);
812 stream->stream_class = stream_class;
813 stream->pos.fd = -1;
814 stream->id = (int64_t) id;
815
816 stream->destroy_listeners = g_array_new(FALSE, TRUE,
817 sizeof(struct bt_ctf_stream_destroy_listener));
818 if (!stream->destroy_listeners) {
819 BT_LOGE_STR("Failed to allocate a GArray.");
820 goto error;
821 }
822
823 if (name) {
824 stream->name = g_string_new(name);
825 if (!stream->name) {
826 BT_LOGE_STR("Failed to allocate a GString.");
827 goto error;
828 }
829 }
830
831 BT_LOGD("Set stream's trace parent: trace-addr=%p", trace);
832
833 if (trace->is_created_by_writer) {
834 int fd;
835 writer = (struct bt_ctf_writer *) bt_object_get_parent(trace);
836 stream->id = (int64_t) stream_class->next_stream_id++;
837
838 BT_LOGD("Stream object belongs to a writer's trace: "
839 "writer-addr=%p", writer);
840 assert(writer);
841
842 if (stream_class->packet_context_type) {
843 BT_LOGD("Creating stream's packet context field: "
844 "ft-addr=%p", stream_class->packet_context_type);
845 stream->packet_context = bt_ctf_field_create(
846 stream_class->packet_context_type);
847 if (!stream->packet_context) {
848 BT_LOGW_STR("Cannot create stream's packet context field.");
849 goto error;
850 }
851
852 /* Initialize events_discarded */
853 ret = try_set_structure_field_integer(
854 stream->packet_context, "events_discarded", 0);
855 if (ret < 0) {
856 BT_LOGW("Cannot set `events_discarded` field in packet context: "
857 "ret=%d, packet-context-field-addr=%p",
858 ret, stream->packet_context);
859 goto error;
860 }
861 }
862
863 stream->events = g_ptr_array_new_with_free_func(
864 (GDestroyNotify) release_event);
865 if (!stream->events) {
866 BT_LOGE_STR("Failed to allocate a GPtrArray.");
867 goto error;
868 }
869
870 if (trace->packet_header_type) {
871 BT_LOGD("Creating stream's packet header field: "
872 "ft-addr=%p", trace->packet_header_type);
873 stream->packet_header =
874 bt_ctf_field_create(trace->packet_header_type);
875 if (!stream->packet_header) {
876 BT_LOGW_STR("Cannot create stream's packet header field.");
877 goto error;
878 }
879 }
880
881 /*
882 * Attempt to populate the default trace packet header fields
883 * (magic, uuid and stream_id). This will _not_ fail shall the
884 * fields not be found or be of an incompatible type; they will
885 * simply not be populated automatically. The user will have to
886 * make sure to set the trace packet header fields himself
887 * before flushing.
888 */
889 ret = auto_populate_packet_header(stream);
890 if (ret) {
891 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
892 goto error;
893 }
894
895 /* Create file associated with this stream */
896 fd = create_stream_file(writer, stream);
897 if (fd < 0) {
898 BT_LOGW_STR("Cannot create stream file.");
899 goto error;
900 }
901
902 set_stream_fd(stream, fd);
903
904 /* Freeze the writer */
905 BT_LOGD_STR("Freezing stream's CTF writer.");
906 bt_ctf_writer_freeze(writer);
907 } else {
908 /* Non-writer stream indicated by a negative FD */
909 set_stream_fd(stream, -1);
910 stream->comp_cur_port = g_hash_table_new(g_direct_hash,
911 g_direct_equal);
912 if (!stream->comp_cur_port) {
913 BT_LOGE_STR("Failed to allocate a GHashTable.");
914 goto error;
915 }
916 }
917
918 /* Add this stream to the trace's streams */
919 g_ptr_array_add(trace->streams, stream);
920 BT_LOGD("Created stream object: addr=%p", stream);
921 goto end;
922
923 error:
924 BT_PUT(stream);
925
926 end:
927 bt_put(writer);
928 return stream;
929 }
930
931 struct bt_ctf_stream *bt_ctf_stream_create_with_id(
932 struct bt_ctf_stream_class *stream_class,
933 const char *name, uint64_t id_param)
934 {
935 struct bt_ctf_trace *trace;
936 struct bt_ctf_stream *stream = NULL;
937 int64_t id = (int64_t) id_param;
938
939 if (!stream_class) {
940 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
941 goto end;
942 }
943
944 if (id < 0) {
945 BT_LOGW("Invalid parameter: invalid stream's ID: "
946 "name=\"%s\", id=%" PRIu64,
947 name, id_param);
948 goto end;
949 }
950
951 trace = bt_ctf_stream_class_borrow_trace(stream_class);
952 if (!trace) {
953 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
954 "stream-class-addr=%p, stream-class-name=\"%s\", "
955 "stream-name=\"%s\", stream-id=%" PRIu64,
956 stream_class, bt_ctf_stream_class_get_name(stream_class),
957 name, id_param);
958 goto end;
959 }
960
961 if (trace->is_created_by_writer) {
962 BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_ctf_stream_create(): "
963 "stream-class-addr=%p, stream-class-name=\"%s\", "
964 "stream-name=\"%s\", stream-id=%" PRIu64,
965 stream_class, bt_ctf_stream_class_get_name(stream_class),
966 name, id_param);
967 goto end;
968 }
969
970 stream = bt_ctf_stream_create_with_id_no_check(stream_class,
971 name, id_param);
972
973 end:
974 return stream;
975 }
976
977 struct bt_ctf_stream *bt_ctf_stream_create(
978 struct bt_ctf_stream_class *stream_class,
979 const char *name)
980 {
981 return bt_ctf_stream_create_with_id_no_check(stream_class,
982 name, -1ULL);
983 }
984
985 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
986 struct bt_ctf_stream *stream)
987 {
988 struct bt_ctf_stream_class *stream_class = NULL;
989
990 if (!stream) {
991 BT_LOGW_STR("Invalid parameter: stream is NULL.");
992 goto end;
993 }
994
995 stream_class = stream->stream_class;
996 bt_get(stream_class);
997 end:
998 return stream_class;
999 }
1000
1001 int bt_ctf_stream_get_discarded_events_count(
1002 struct bt_ctf_stream *stream, uint64_t *count)
1003 {
1004 int ret = 0;
1005
1006 if (!stream) {
1007 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1008 ret = -1;
1009 goto end;
1010 }
1011
1012 if (!count) {
1013 BT_LOGW_STR("Invalid parameter: count is NULL.");
1014 ret = -1;
1015 goto end;
1016 }
1017
1018 if (stream->pos.fd < 0) {
1019 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1020 "stream-addr=%p, stream-name=\"%s\"",
1021 stream, bt_ctf_stream_get_name(stream));
1022 ret = -1;
1023 goto end;
1024 }
1025
1026 *count = (uint64_t) stream->discarded_events;
1027
1028 end:
1029 return ret;
1030 }
1031
1032 static
1033 int set_packet_context_events_discarded_field(struct bt_ctf_stream *stream,
1034 uint64_t count)
1035 {
1036 int ret = 0;
1037 struct bt_ctf_field *events_discarded_field = NULL;
1038
1039 if (!stream->packet_context) {
1040 goto end;
1041 }
1042
1043 events_discarded_field = bt_ctf_field_structure_get_field(
1044 stream->packet_context, "events_discarded");
1045 if (!events_discarded_field) {
1046 goto end;
1047 }
1048
1049 ret = bt_ctf_field_unsigned_integer_set_value(
1050 events_discarded_field, count);
1051 if (ret) {
1052 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1053 "field-addr=%p, value=%" PRIu64,
1054 events_discarded_field, count);
1055 goto end;
1056 }
1057
1058 end:
1059 bt_put(events_discarded_field);
1060 return ret;
1061 }
1062
1063 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
1064 uint64_t event_count)
1065 {
1066 int ret;
1067 uint64_t new_count;
1068 struct bt_ctf_field *events_discarded_field = NULL;
1069
1070 if (!stream) {
1071 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1072 goto end;
1073 }
1074
1075 BT_LOGV("Appending discarded events to stream: "
1076 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1077 stream, bt_ctf_stream_get_name(stream), event_count);
1078
1079 if (!stream->packet_context) {
1080 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1081 goto end;
1082 }
1083
1084 if (stream->pos.fd < 0) {
1085 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1086 goto end;
1087 }
1088
1089 events_discarded_field = bt_ctf_field_structure_get_field(
1090 stream->packet_context, "events_discarded");
1091 if (!events_discarded_field) {
1092 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1093 goto end;
1094 }
1095
1096 new_count = stream->discarded_events + event_count;
1097 if (new_count < stream->discarded_events) {
1098 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1099 "cur-count=%" PRIu64 ", new-count=%" PRIu64,
1100 stream->discarded_events, new_count);
1101 goto end;
1102 }
1103
1104 ret = set_packet_context_events_discarded_field(stream, new_count);
1105 if (ret) {
1106 /* set_packet_context_events_discarded_field() logs errors */
1107 goto end;
1108 }
1109
1110 stream->discarded_events = new_count;
1111 BT_LOGV("Appended discarded events to stream: "
1112 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1113 stream, bt_ctf_stream_get_name(stream), event_count);
1114
1115 end:
1116 bt_put(events_discarded_field);
1117 }
1118
1119 static int auto_populate_event_header(struct bt_ctf_stream *stream,
1120 struct bt_ctf_event *event)
1121 {
1122 int ret = 0;
1123 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
1124 struct bt_ctf_clock_class *mapped_clock_class = NULL;
1125 uint64_t event_class_id;
1126
1127 assert(event);
1128
1129 if (!event->event_header) {
1130 goto end;
1131 }
1132
1133 if (event->frozen) {
1134 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1135 ret = -1;
1136 goto end;
1137 }
1138
1139 BT_LOGV("Automatically populating event's header field: "
1140 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1141 stream, bt_ctf_stream_get_name(stream), event);
1142
1143 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
1144 event_class_id = (uint64_t) bt_ctf_event_class_get_id(event->event_class);
1145 assert(event_class_id >= 0);
1146 if (id_field && bt_ctf_field_type_is_integer(id_field->type)) {
1147 ret = set_integer_field_value(id_field, event_class_id);
1148 if (ret) {
1149 BT_LOGW("Cannot set event header's `id` field's value: "
1150 "addr=%p, value=%" PRIu64, id_field,
1151 event_class_id);
1152 goto end;
1153 }
1154 }
1155
1156 /*
1157 * The conditions to automatically set the timestamp are:
1158 *
1159 * 1. The event header field "timestamp" exists and is an
1160 * integer field.
1161 * 2. This stream's class has a registered clock (set with
1162 * bt_ctf_stream_class_set_clock()).
1163 * 3. The event header field "timestamp" has its type mapped to
1164 * a clock class which is also the clock class of this
1165 * stream's class's registered clock.
1166 */
1167 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
1168 "timestamp");
1169 if (timestamp_field && stream->stream_class->clock &&
1170 bt_ctf_field_type_is_integer(timestamp_field->type)) {
1171 struct bt_ctf_clock_class *stream_class_clock_class =
1172 stream->stream_class->clock->clock_class;
1173
1174 mapped_clock_class =
1175 bt_ctf_field_type_integer_get_mapped_clock_class(
1176 timestamp_field->type);
1177 if (mapped_clock_class == stream_class_clock_class) {
1178 uint64_t timestamp;
1179
1180 ret = bt_ctf_clock_get_value(
1181 stream->stream_class->clock,
1182 &timestamp);
1183 assert(ret == 0);
1184 ret = set_integer_field_value(timestamp_field,
1185 timestamp);
1186 if (ret) {
1187 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1188 "addr=%p, value=%" PRIu64,
1189 timestamp_field, timestamp);
1190 goto end;
1191 }
1192 }
1193 }
1194
1195 BT_LOGV("Automatically populated event's header field: "
1196 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1197 stream, bt_ctf_stream_get_name(stream), event);
1198
1199 end:
1200 bt_put(id_field);
1201 bt_put(timestamp_field);
1202 bt_put(mapped_clock_class);
1203 return ret;
1204 }
1205
1206 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
1207 struct bt_ctf_event *event)
1208 {
1209 int ret = 0;
1210
1211 if (!stream) {
1212 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1213 ret = -1;
1214 goto end;
1215 }
1216
1217 if (!event) {
1218 BT_LOGW_STR("Invalid parameter: event is NULL.");
1219 ret = -1;
1220 goto end;
1221 }
1222
1223 if (stream->pos.fd < 0) {
1224 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1225 ret = -1;
1226 goto end;
1227 }
1228
1229 BT_LOGV("Appending event to stream: "
1230 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1231 "event-class-name=\"%s\", event-class-id=%" PRId64,
1232 stream, bt_ctf_stream_get_name(stream), event,
1233 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event)),
1234 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event)));
1235
1236 /*
1237 * The event is not supposed to have a parent stream at this
1238 * point. The only other way an event can have a parent stream
1239 * is if it was assigned when setting a packet to the event,
1240 * in which case the packet's stream is not a writer stream,
1241 * and thus the user is trying to append an event which belongs
1242 * to another stream.
1243 */
1244 if (event->base.parent) {
1245 ret = -1;
1246 goto end;
1247 }
1248
1249 bt_object_set_parent(event, stream);
1250 BT_LOGV_STR("Automatically populating the header of the event to append.");
1251 ret = auto_populate_event_header(stream, event);
1252 if (ret) {
1253 /* auto_populate_event_header() reports errors */
1254 goto error;
1255 }
1256
1257 /* Make sure the various scopes of the event are set */
1258 BT_LOGV_STR("Validating event to append.");
1259 ret = bt_ctf_event_validate(event);
1260 if (ret) {
1261 goto error;
1262 }
1263
1264 /* Save the new event and freeze it */
1265 BT_LOGV_STR("Freezing the event to append.");
1266 bt_ctf_event_freeze(event);
1267 g_ptr_array_add(stream->events, event);
1268
1269 /*
1270 * Event had to hold a reference to its event class as long as it wasn't
1271 * part of the same trace hierarchy. From now on, the event and its
1272 * class share the same lifetime guarantees and the reference is no
1273 * longer needed.
1274 */
1275 BT_LOGV_STR("Putting the event's class.");
1276 bt_put(event->event_class);
1277 BT_LOGV("Appended event to stream: "
1278 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1279 "event-class-name=\"%s\", event-class-id=%" PRId64,
1280 stream, bt_ctf_stream_get_name(stream), event,
1281 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event)),
1282 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event)));
1283
1284 end:
1285 return ret;
1286
1287 error:
1288 /*
1289 * Orphan the event; we were not successful in associating it to
1290 * a stream.
1291 */
1292 bt_object_set_parent(event, NULL);
1293
1294 return ret;
1295 }
1296
1297 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
1298 struct bt_ctf_stream *stream)
1299 {
1300 struct bt_ctf_field *packet_context = NULL;
1301
1302 if (!stream) {
1303 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1304 goto end;
1305 }
1306
1307 if (stream->pos.fd < 0) {
1308 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1309 "stream-addr=%p, stream-name=\"%s\"", stream,
1310 bt_ctf_stream_get_name(stream));
1311 goto end;
1312 }
1313
1314 packet_context = stream->packet_context;
1315 if (packet_context) {
1316 bt_get(packet_context);
1317 }
1318 end:
1319 return packet_context;
1320 }
1321
1322 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
1323 struct bt_ctf_field *field)
1324 {
1325 int ret = 0;
1326 struct bt_ctf_field_type *field_type;
1327
1328 if (!stream) {
1329 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1330 ret = -1;
1331 goto end;
1332 }
1333
1334 if (stream->pos.fd < 0) {
1335 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1336 ret = -1;
1337 goto end;
1338 }
1339
1340 field_type = bt_ctf_field_get_type(field);
1341 if (bt_ctf_field_type_compare(field_type,
1342 stream->stream_class->packet_context_type)) {
1343 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1344 "stream-addr=%p, stream-name=\"%s\", "
1345 "packet-context-field-addr=%p, "
1346 "packet-context-ft-addr=%p",
1347 stream, bt_ctf_stream_get_name(stream),
1348 field, field_type);
1349 ret = -1;
1350 goto end;
1351 }
1352
1353 bt_put(field_type);
1354 bt_put(stream->packet_context);
1355 stream->packet_context = bt_get(field);
1356 BT_LOGV("Set stream's packet context field: "
1357 "stream-addr=%p, stream-name=\"%s\", "
1358 "packet-context-field-addr=%p",
1359 stream, bt_ctf_stream_get_name(stream), field);
1360 end:
1361 return ret;
1362 }
1363
1364 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
1365 struct bt_ctf_stream *stream)
1366 {
1367 struct bt_ctf_field *packet_header = NULL;
1368
1369 if (!stream) {
1370 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1371 goto end;
1372 }
1373
1374 if (stream->pos.fd < 0) {
1375 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1376 "stream-addr=%p, stream-name=\"%s\"", stream,
1377 bt_ctf_stream_get_name(stream));
1378 goto end;
1379 }
1380
1381 packet_header = stream->packet_header;
1382 if (packet_header) {
1383 bt_get(packet_header);
1384 }
1385 end:
1386 return packet_header;
1387 }
1388
1389 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
1390 struct bt_ctf_field *field)
1391 {
1392 int ret = 0;
1393 struct bt_ctf_trace *trace = NULL;
1394 struct bt_ctf_field_type *field_type = NULL;
1395
1396 if (!stream) {
1397 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1398 ret = -1;
1399 goto end;
1400 }
1401
1402 if (stream->pos.fd < 0) {
1403 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1404 ret = -1;
1405 goto end;
1406 }
1407
1408 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
1409
1410 if (!field) {
1411 if (trace->packet_header_type) {
1412 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1413 "stream-addr=%p, stream-name=\"%s\", "
1414 "packet-header-field-addr=%p, "
1415 "expected-ft-addr=%p",
1416 stream, bt_ctf_stream_get_name(stream),
1417 field, trace->packet_header_type);
1418 ret = -1;
1419 goto end;
1420 }
1421
1422 goto skip_validation;
1423 }
1424
1425 field_type = bt_ctf_field_get_type(field);
1426 assert(field_type);
1427
1428 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
1429 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1430 "stream-addr=%p, stream-name=\"%s\", "
1431 "packet-header-field-addr=%p, "
1432 "packet-header-ft-addr=%p",
1433 stream, bt_ctf_stream_get_name(stream),
1434 field, field_type);
1435 ret = -1;
1436 goto end;
1437 }
1438
1439 skip_validation:
1440 bt_put(stream->packet_header);
1441 stream->packet_header = bt_get(field);
1442 BT_LOGV("Set stream's packet header field: "
1443 "stream-addr=%p, stream-name=\"%s\", "
1444 "packet-header-field-addr=%p",
1445 stream, bt_ctf_stream_get_name(stream), field);
1446 end:
1447 BT_PUT(trace);
1448 bt_put(field_type);
1449 return ret;
1450 }
1451
1452 static
1453 void reset_structure_field(struct bt_ctf_field *structure, const char *name)
1454 {
1455 struct bt_ctf_field *member;
1456
1457 member = bt_ctf_field_structure_get_field(structure, name);
1458 if (member) {
1459 (void) bt_ctf_field_reset(member);
1460 bt_put(member);
1461 }
1462 }
1463
1464 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
1465 {
1466 int ret = 0;
1467 size_t i;
1468 struct bt_ctf_stream_pos packet_context_pos;
1469 struct bt_ctf_trace *trace;
1470 enum bt_ctf_byte_order native_byte_order;
1471 bool has_packet_size = false;
1472
1473 if (!stream) {
1474 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1475 ret = -1;
1476 goto end;
1477 }
1478
1479 if (stream->pos.fd < 0) {
1480 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1481 ret = -1;
1482 goto end;
1483 }
1484
1485 if (stream->packet_context) {
1486 struct bt_ctf_field *packet_size_field;
1487
1488 packet_size_field = bt_ctf_field_structure_get_field(
1489 stream->packet_context, "packet_size");
1490 has_packet_size = (packet_size_field != NULL);
1491 bt_put(packet_size_field);
1492 }
1493
1494 if (stream->flushed_packet_count == 1) {
1495 if (!stream->packet_context) {
1496 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1497 ret = -1;
1498 goto end;
1499 }
1500
1501 if (!has_packet_size) {
1502 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1503 ret = -1;
1504 goto end;
1505 }
1506 }
1507
1508 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1509 "stream-name=\"%s\", packet-index=%u", stream,
1510 bt_ctf_stream_get_name(stream), stream->flushed_packet_count);
1511 trace = bt_ctf_stream_class_borrow_trace(stream->stream_class);
1512 assert(trace);
1513 native_byte_order = bt_ctf_trace_get_native_byte_order(trace);
1514
1515 ret = auto_populate_packet_header(stream);
1516 if (ret) {
1517 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1518 ret = -1;
1519 goto end;
1520 }
1521
1522 ret = auto_populate_packet_context(stream);
1523 if (ret) {
1524 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1525 ret = -1;
1526 goto end;
1527 }
1528
1529 /* mmap the next packet */
1530 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64,
1531 stream->pos.offset);
1532 bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR);
1533 assert(stream->pos.packet_size % 8 == 0);
1534
1535 if (stream->packet_header) {
1536 BT_LOGV_STR("Serializing packet header field.");
1537 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos,
1538 native_byte_order);
1539 if (ret) {
1540 BT_LOGW("Cannot serialize stream's packet header field: "
1541 "field-addr=%p", stream->packet_header);
1542 goto end;
1543 }
1544 }
1545
1546 if (stream->packet_context) {
1547 /* Write packet context */
1548 memcpy(&packet_context_pos, &stream->pos,
1549 sizeof(packet_context_pos));
1550 BT_LOGV_STR("Serializing packet context field.");
1551 ret = bt_ctf_field_serialize(stream->packet_context,
1552 &stream->pos, native_byte_order);
1553 if (ret) {
1554 BT_LOGW("Cannot serialize stream's packet context field: "
1555 "field-addr=%p", stream->packet_context);
1556 goto end;
1557 }
1558 }
1559
1560 BT_LOGV("Serializing events: count=%u", stream->events->len);
1561
1562 for (i = 0; i < stream->events->len; i++) {
1563 struct bt_ctf_event *event = g_ptr_array_index(
1564 stream->events, i);
1565 struct bt_ctf_event_class *event_class =
1566 bt_ctf_event_borrow_event_class(event);
1567
1568 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1569 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1570 "pos-offset=%" PRId64 ", packet-size=%" PRIu64,
1571 i, event, bt_ctf_event_class_get_name(event_class),
1572 bt_ctf_event_class_get_id(event_class),
1573 stream->pos.offset, stream->pos.packet_size);
1574
1575 /* Write event header */
1576 if (event->event_header) {
1577 BT_LOGV_STR("Serializing event's header field.");
1578 ret = bt_ctf_field_serialize(event->event_header,
1579 &stream->pos, native_byte_order);
1580 if (ret) {
1581 BT_LOGW("Cannot serialize event's header field: "
1582 "field-addr=%p", event->event_header);
1583 goto end;
1584 }
1585 }
1586
1587 /* Write stream event context */
1588 if (event->stream_event_context) {
1589 BT_LOGV_STR("Serializing event's stream event context field.");
1590 ret = bt_ctf_field_serialize(
1591 event->stream_event_context, &stream->pos,
1592 native_byte_order);
1593 if (ret) {
1594 BT_LOGW("Cannot serialize event's stream event context field: "
1595 "field-addr=%p", event->stream_event_context);
1596 goto end;
1597 }
1598 }
1599
1600 /* Write event content */
1601 ret = bt_ctf_event_serialize(event, &stream->pos,
1602 native_byte_order);
1603 if (ret) {
1604 /* bt_ctf_event_serialize() logs errors */
1605 goto end;
1606 }
1607 }
1608
1609 if (!has_packet_size && stream->pos.offset % 8 != 0) {
1610 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1611 "but current content size is not a multiple of 8 bits: "
1612 "content-size=%" PRId64 ", "
1613 "packet-size=%" PRIu64,
1614 stream->pos.offset,
1615 stream->pos.packet_size);
1616 ret = -1;
1617 goto end;
1618 }
1619
1620 assert(stream->pos.packet_size % 8 == 0);
1621
1622 /*
1623 * Remove extra padding bytes.
1624 */
1625 stream->pos.packet_size = (stream->pos.offset + 7) & ~7;
1626
1627 if (stream->packet_context) {
1628 /*
1629 * The whole packet is serialized at this point. Make sure that,
1630 * if `packet_size` is missing, the current content size is
1631 * equal to the current packet size.
1632 */
1633 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
1634 stream->packet_context, "content_size");
1635
1636 bt_put(field);
1637 if (!field) {
1638 if (stream->pos.offset != stream->pos.packet_size) {
1639 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1640 "but current packet's content size is not equal to its packet size: "
1641 "content-size=%" PRId64 ", "
1642 "packet-size=%" PRIu64,
1643 stream->pos.offset,
1644 stream->pos.packet_size);
1645 ret = -1;
1646 goto end;
1647 }
1648 }
1649
1650 /*
1651 * Overwrite the packet context now that the stream
1652 * position's packet and content sizes have the correct
1653 * values.
1654 *
1655 * Copy base_mma as the packet may have been remapped
1656 * (e.g. when a packet is resized).
1657 */
1658 packet_context_pos.base_mma = stream->pos.base_mma;
1659 ret = auto_populate_packet_context(stream);
1660 if (ret) {
1661 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1662 ret = -1;
1663 goto end;
1664 }
1665
1666 BT_LOGV("Rewriting (serializing) packet context field.");
1667 ret = bt_ctf_field_serialize(stream->packet_context,
1668 &packet_context_pos, native_byte_order);
1669 if (ret) {
1670 BT_LOGW("Cannot serialize stream's packet context field: "
1671 "field-addr=%p", stream->packet_context);
1672 goto end;
1673 }
1674 }
1675
1676 g_ptr_array_set_size(stream->events, 0);
1677 stream->flushed_packet_count++;
1678 stream->size += stream->pos.packet_size / CHAR_BIT;
1679
1680 do {
1681 ret = ftruncate(stream->pos.fd, stream->size);
1682 } while (ret == -1 && errno == EINTR);
1683 if (ret == -1) {
1684 BT_LOGE_ERRNO("Cannot ftruncate() stream file to new size",
1685 "size = %" PRIu64 ", name = %s",
1686 stream->size,
1687 stream->name ? stream->name->str : "(null)");
1688 }
1689
1690 end:
1691 /* Reset automatically-set fields. */
1692 if (stream->packet_context) {
1693 reset_structure_field(stream->packet_context, "timestamp_begin");
1694 reset_structure_field(stream->packet_context, "timestamp_end");
1695 reset_structure_field(stream->packet_context, "packet_size");
1696 reset_structure_field(stream->packet_context, "content_size");
1697 reset_structure_field(stream->packet_context, "events_discarded");
1698 }
1699
1700 if (ret < 0) {
1701 /*
1702 * We failed to write the packet. Its size is therefore set to 0
1703 * to ensure the next mapping is done in the same place rather
1704 * than advancing by "stream->pos.packet_size", which would
1705 * leave a corrupted packet in the trace.
1706 */
1707 stream->pos.packet_size = 0;
1708 } else {
1709 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", "
1710 "packet-size=%" PRIu64,
1711 stream->pos.offset, stream->pos.packet_size);
1712 }
1713 return ret;
1714 }
1715
1716 /* Pre-2.0 CTF writer backward compatibility */
1717 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
1718 {
1719 bt_get(stream);
1720 }
1721
1722 /* Pre-2.0 CTF writer backward compatibility */
1723 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
1724 {
1725 bt_put(stream);
1726 }
1727
1728 static
1729 void bt_ctf_stream_destroy(struct bt_object *obj)
1730 {
1731 struct bt_ctf_stream *stream;
1732 int i;
1733
1734 stream = container_of(obj, struct bt_ctf_stream, base);
1735 BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"",
1736 stream, bt_ctf_stream_get_name(stream));
1737
1738 /* Call destroy listeners in reverse registration order */
1739 for (i = stream->destroy_listeners->len - 1; i >= 0; i--) {
1740 struct bt_ctf_stream_destroy_listener *listener =
1741 &g_array_index(stream->destroy_listeners,
1742 struct bt_ctf_stream_destroy_listener, i);
1743
1744 BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d",
1745 listener->func, listener->data, i);
1746 listener->func(stream, listener->data);
1747 }
1748
1749 (void) bt_ctf_stream_pos_fini(&stream->pos);
1750 if (stream->pos.fd >= 0) {
1751 int ret;
1752
1753 /*
1754 * Truncate the file's size to the minimum required to fit the
1755 * last packet as we might have grown it too much on the last
1756 * mmap.
1757 */
1758 do {
1759 ret = ftruncate(stream->pos.fd, stream->size);
1760 } while (ret == -1 && errno == EINTR);
1761 if (ret) {
1762 BT_LOGE("Failed to truncate stream file: %s: "
1763 "ret=%d, errno=%d, size=%" PRIu64,
1764 strerror(errno), ret, errno,
1765 (uint64_t) stream->size);
1766 }
1767
1768 if (close(stream->pos.fd)) {
1769 BT_LOGE("Failed to close stream file: %s: "
1770 "ret=%d, errno=%d", strerror(errno),
1771 ret, errno);
1772 }
1773 }
1774
1775 if (stream->events) {
1776 BT_LOGD_STR("Putting events.");
1777 g_ptr_array_free(stream->events, TRUE);
1778 }
1779
1780 if (stream->name) {
1781 g_string_free(stream->name, TRUE);
1782 }
1783
1784 if (stream->comp_cur_port) {
1785 GHashTableIter ht_iter;
1786 gpointer comp_gptr, port_gptr;
1787
1788 /*
1789 * Since we're destroying the stream, remove the destroy
1790 * listeners that it registered for each component in
1791 * its component-port mapping hash table. Otherwise they
1792 * would be called and the stream would be accessed once
1793 * it's freed or another stream would be accessed.
1794 */
1795 g_hash_table_iter_init(&ht_iter, stream->comp_cur_port);
1796
1797 while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) {
1798 assert(comp_gptr);
1799 bt_component_remove_destroy_listener((void *) comp_gptr,
1800 component_destroy_listener, stream);
1801 }
1802
1803 g_hash_table_destroy(stream->comp_cur_port);
1804 }
1805
1806 if (stream->destroy_listeners) {
1807 g_array_free(stream->destroy_listeners, TRUE);
1808 }
1809
1810 BT_LOGD_STR("Putting packet header field.");
1811 bt_put(stream->packet_header);
1812 BT_LOGD_STR("Putting packet context field.");
1813 bt_put(stream->packet_context);
1814 g_free(stream);
1815 }
1816
1817 static
1818 int _set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1819 uint64_t value, bt_bool force)
1820 {
1821 int ret = 0;
1822 struct bt_ctf_field_type *field_type = NULL;
1823 struct bt_ctf_field *integer;
1824
1825 assert(structure);
1826 assert(name);
1827
1828 integer = bt_ctf_field_structure_get_field(structure, name);
1829 if (!integer) {
1830 /* Field not found, not an error. */
1831 BT_LOGV("Field not found: struct-field-addr=%p, "
1832 "name=\"%s\", force=%d", structure, name, force);
1833 goto end;
1834 }
1835
1836 /* Make sure the payload has not already been set. */
1837 if (!force && bt_ctf_field_is_set(integer)) {
1838 /* Payload already set, not an error */
1839 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1840 "name=\"%s\", force=%d", structure, name, force);
1841 goto end;
1842 }
1843
1844 field_type = bt_ctf_field_get_type(integer);
1845 assert(field_type);
1846 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1847 /*
1848 * The user most likely meant for us to populate this field
1849 * automatically. However, we can only do this if the field
1850 * is an integer. Return an error.
1851 */
1852 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1853 "field-addr=%p, ft-addr=%p, ft-id=%s",
1854 integer, field_type,
1855 bt_ctf_field_type_id_string(field_type->id));
1856 ret = -1;
1857 goto end;
1858 }
1859
1860 if (bt_ctf_field_type_integer_get_signed(field_type)) {
1861 ret = bt_ctf_field_signed_integer_set_value(integer,
1862 (int64_t) value);
1863 } else {
1864 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
1865 }
1866 ret = !ret ? 1 : ret;
1867 end:
1868 bt_put(integer);
1869 bt_put(field_type);
1870 return ret;
1871 }
1872
1873 /*
1874 * Returns the following codes:
1875 * 1 if the field was found and set,
1876 * 0 if nothing was done (field not found, or was already set),
1877 * <0 if an error was encoutered
1878 */
1879 static
1880 int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1881 uint64_t value)
1882 {
1883 return _set_structure_field_integer(structure, name, value, BT_FALSE);
1884 }
1885
1886 const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
1887 {
1888 const char *name = NULL;
1889
1890 if (!stream) {
1891 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1892 goto end;
1893 }
1894
1895 name = stream->name ? stream->name->str : NULL;
1896
1897 end:
1898 return name;
1899 }
1900
1901 int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream)
1902 {
1903 int ret = -1;
1904
1905 if (!stream) {
1906 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1907 goto end;
1908 }
1909
1910 ret = (stream->pos.fd >= 0);
1911
1912 end:
1913 return ret;
1914 }
1915
1916 BT_HIDDEN
1917 void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream *stream,
1918 struct bt_component *comp,
1919 struct bt_port *port)
1920 {
1921 assert(stream);
1922 assert(comp);
1923 assert(port);
1924 assert(stream->comp_cur_port);
1925
1926 /*
1927 * Do not take a reference to the component here because we
1928 * don't want the component to exist as long as this stream
1929 * exists. Instead, keep a weak reference, but add a destroy
1930 * listener so that we remove this hash table entry when we know
1931 * the component is destroyed.
1932 */
1933 BT_LOGV("Adding component's destroy listener for stream: "
1934 "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, "
1935 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1936 stream, bt_ctf_stream_get_name(stream),
1937 comp, bt_component_get_name(comp), port,
1938 bt_port_get_name(port));
1939 bt_component_add_destroy_listener(comp, component_destroy_listener,
1940 stream);
1941 g_hash_table_insert(stream->comp_cur_port, comp, port);
1942 BT_LOGV_STR("Mapped component to port for stream.");
1943 }
1944
1945 BT_HIDDEN
1946 struct bt_port *bt_ctf_stream_port_for_component(struct bt_ctf_stream *stream,
1947 struct bt_component *comp)
1948 {
1949 assert(stream);
1950 assert(comp);
1951 assert(stream->comp_cur_port);
1952 return g_hash_table_lookup(stream->comp_cur_port, comp);
1953 }
1954
1955 BT_HIDDEN
1956 void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream *stream,
1957 bt_ctf_stream_destroy_listener_func func, void *data)
1958 {
1959 struct bt_ctf_stream_destroy_listener listener;
1960
1961 assert(stream);
1962 assert(func);
1963 listener.func = func;
1964 listener.data = data;
1965 g_array_append_val(stream->destroy_listeners, listener);
1966 BT_LOGV("Added stream destroy listener: stream-addr=%p, "
1967 "stream-name=\"%s\", func=%p, data=%p",
1968 stream, bt_ctf_stream_get_name(stream), func, data);
1969 }
1970
1971 BT_HIDDEN
1972 void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream *stream,
1973 bt_ctf_stream_destroy_listener_func func, void *data)
1974 {
1975 size_t i;
1976
1977 assert(stream);
1978 assert(func);
1979
1980 for (i = 0; i < stream->destroy_listeners->len; i++) {
1981 struct bt_ctf_stream_destroy_listener *listener =
1982 &g_array_index(stream->destroy_listeners,
1983 struct bt_ctf_stream_destroy_listener, i);
1984
1985 if (listener->func == func && listener->data == data) {
1986 g_array_remove_index(stream->destroy_listeners, i);
1987 i--;
1988 BT_LOGV("Removed stream destroy listener: stream-addr=%p, "
1989 "stream-name=\"%s\", func=%p, data=%p",
1990 stream, bt_ctf_stream_get_name(stream),
1991 func, data);
1992 }
1993 }
1994 }
1995
1996 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream *stream)
1997 {
1998 int64_t ret;
1999
2000 if (!stream) {
2001 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2002 ret = (int64_t) -1;
2003 goto end;
2004 }
2005
2006 ret = stream->id;
2007 if (ret < 0) {
2008 BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"",
2009 stream, bt_ctf_stream_get_name(stream));
2010 }
2011
2012 end:
2013 return ret;
2014 }
This page took 0.067252 seconds and 5 git commands to generate.