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