ir: stream: prefer to use stream's name for (CTF writer) stream's path
[babeltrace.git] / lib / ctf-ir / stream.c
CommitLineData
273b65be
JG
1/*
2 * stream.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Stream
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
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
19abc2c6
PP
29#define BT_LOG_TAG "STREAM"
30#include <babeltrace/lib-logging-internal.h>
31
ac0c6bdd
PP
32#include <babeltrace/ctf-ir/clock-class.h>
33#include <babeltrace/ctf-writer/clock.h>
34#include <babeltrace/ctf-writer/clock-internal.h>
273b65be 35#include <babeltrace/ctf-writer/event.h>
adc315b8 36#include <babeltrace/ctf-ir/event-internal.h>
2e33ac5a
PP
37#include <babeltrace/ctf-ir/field-types-internal.h>
38#include <babeltrace/ctf-ir/fields-internal.h>
3f043b05
JG
39#include <babeltrace/ctf-ir/stream.h>
40#include <babeltrace/ctf-ir/stream-internal.h>
adc315b8 41#include <babeltrace/ctf-ir/stream-class-internal.h>
5acf2ae6 42#include <babeltrace/ctf-ir/trace.h>
319fd969
PP
43#include <babeltrace/ctf-ir/trace-internal.h>
44#include <babeltrace/ctf-writer/writer-internal.h>
3230ee6b 45#include <babeltrace/graph/component-internal.h>
83509119 46#include <babeltrace/ref.h>
273b65be 47#include <babeltrace/ctf-writer/functor-internal.h>
3d9990ac
PP
48#include <babeltrace/compiler-internal.h>
49#include <babeltrace/align-internal.h>
dc3fffef 50#include <inttypes.h>
95076212 51#include <unistd.h>
273b65be
JG
52
53static
83509119 54void bt_ctf_stream_destroy(struct bt_object *obj);
273b65be 55static
af9296f3 56int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
273b65be 57
ac0c6bdd
PP
58static
59int 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) {
19abc2c6 65 BT_LOGW_STR("Invalid parameter: field is NULL.");
ac0c6bdd
PP
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) !=
1487a16a 74 BT_CTF_FIELD_TYPE_ID_INTEGER) {
ac0c6bdd 75 /* Not an integer and the value is unset, error. */
19abc2c6
PP
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));
ac0c6bdd
PP
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. */
19abc2c6
PP
88 BT_LOGW("Cannot set signed integer field's value: "
89 "addr=%p, value=%" PRId64,
90 field, (int64_t) value);
ac0c6bdd
PP
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. */
19abc2c6
PP
97 BT_LOGW("Cannot set unsigned integer field's value: "
98 "addr=%p, value=%" PRIu64,
99 field, value);
ac0c6bdd
PP
100 goto end;
101 }
102 }
103end:
104 bt_put(field_type);
105 return ret;
106}
107
d246b111
JG
108static
109int set_packet_header_magic(struct bt_ctf_stream *stream)
110{
111 int ret = 0;
d246b111
JG
112 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
113 stream->packet_header, "magic");
19abc2c6
PP
114 const uint32_t magic_value = 0xc1fc1fc1;
115
116 assert(stream);
d246b111
JG
117
118 if (!magic_field) {
119 /* No magic field found. Not an error, skip. */
19abc2c6
PP
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));
d246b111
JG
123 goto end;
124 }
125
b3376dd9
PP
126 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
127 (uint64_t) magic_value);
d246b111 128
d246b111 129 if (ret) {
b3376dd9
PP
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,
19abc2c6
PP
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: "
b3376dd9 136 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
19abc2c6
PP
137 stream, bt_ctf_stream_get_name(stream),
138 magic_field, (uint64_t) magic_value);
d246b111
JG
139 }
140end:
83509119 141 bt_put(magic_field);
d246b111
JG
142 return ret;
143}
144
145static
146int set_packet_header_uuid(struct bt_ctf_stream *stream)
147{
19abc2c6
PP
148 int ret = 0;
149 int64_t i;
e6a8e8e4 150 struct bt_ctf_trace *trace = NULL;
d246b111
JG
151 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field(
152 stream->packet_header, "uuid");
153
19abc2c6
PP
154 assert(stream);
155
d246b111
JG
156 if (!uuid_field) {
157 /* No uuid field found. Not an error, skip. */
19abc2c6
PP
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));
d246b111
JG
161 goto end;
162 }
163
e6a8e8e4 164 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
d246b111
JG
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
b3376dd9
PP
169 ret = bt_ctf_field_unsigned_integer_set_value(
170 uuid_element, (uint64_t) trace->uuid[i]);
83509119 171 bt_put(uuid_element);
d246b111 172 if (ret) {
19abc2c6
PP
173 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
174 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
b3376dd9 175 "value=%" PRIu64 ", index=%" PRId64,
19abc2c6
PP
176 stream, bt_ctf_stream_get_name(stream),
177 uuid_element, (uint64_t) trace->uuid[i], i);
d246b111
JG
178 goto end;
179 }
180 }
181
19abc2c6
PP
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
d246b111 186end:
83509119 187 bt_put(uuid_field);
e6a8e8e4 188 BT_PUT(trace);
d246b111
JG
189 return ret;
190}
191static
192int set_packet_header_stream_id(struct bt_ctf_stream *stream)
193{
194 int ret = 0;
195 uint32_t stream_id;
d246b111
JG
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. */
19abc2c6
PP
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));
d246b111
JG
204 goto end;
205 }
206
b3376dd9
PP
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
222end:
223 bt_put(stream_id_field);
224 return ret;
225}
226
227static
228int 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: "
19abc2c6
PP
247 "stream-addr=%p, stream-name=\"%s\"",
248 stream, bt_ctf_stream_get_name(stream));
d246b111
JG
249 goto end;
250 }
251
b3376dd9
PP
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));
d246b111
JG
257 goto end;
258 }
259
b3376dd9
PP
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
264end:
265 return ret;
266}
267
268static
269int 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);
d246b111 287 if (ret) {
b3376dd9
PP
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);
d246b111 292 } else {
b3376dd9
PP
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);
d246b111 297 }
19abc2c6 298
b3376dd9
PP
299end:
300 bt_put(field);
301 return ret;
302}
303
304static
305int 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);
19abc2c6 323 if (ret) {
b3376dd9
PP
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,
19abc2c6 326 stream, bt_ctf_stream_get_name(stream),
b3376dd9 327 field, stream->pos.offset);
19abc2c6 328 } else {
b3376dd9
PP
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,
19abc2c6 331 stream, bt_ctf_stream_get_name(stream),
b3376dd9 332 field, stream->pos.offset);
19abc2c6
PP
333 }
334
d246b111 335end:
b3376dd9 336 bt_put(field);
d246b111
JG
337 return ret;
338}
339
340static
b3376dd9 341int set_packet_context_events_discarded(struct bt_ctf_stream *stream)
d246b111 342{
b3376dd9
PP
343 int ret = 0;
344 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
345 stream->packet_context, "events_discarded");
d246b111 346
b3376dd9
PP
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
59a09b0e
PP
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;
b3376dd9 387 } else {
59a09b0e
PP
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 }
b3376dd9
PP
401 }
402
403end:
404 bt_put(field);
405 return ret;
406}
407
408static
409int 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
463end:
464 bt_put(timestamp_field);
465 bt_put(ts_field_mapped_clock_class);
466 return ret;
467}
468
469static
470int 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: "
19abc2c6
PP
491 "stream-addr=%p, stream-name=\"%s\"",
492 stream, bt_ctf_stream_get_name(stream));
d246b111
JG
493 goto end;
494 }
495
b3376dd9
PP
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);
d246b111 536 if (ret) {
b3376dd9
PP
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
548end:
549 bt_put(field);
550 bt_put(field_mapped_clock_class);
551 return ret;
552}
553
554static
555int 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: "
19abc2c6
PP
561 "stream-addr=%p, stream-name=\"%s\"",
562 stream, bt_ctf_stream_get_name(stream));
d246b111
JG
563 goto end;
564 }
565
b3376dd9
PP
566 ret = set_packet_context_timestamp_field(stream, "timestamp_begin",
567 g_ptr_array_index(stream->events, 0));
568
569end:
570 return ret;
571}
572
573static
574int 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
588end:
589 return ret;
590}
591
592static
593int 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);
d246b111 602 if (ret) {
b3376dd9
PP
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: "
19abc2c6
PP
612 "stream-addr=%p, stream-name=\"%s\"",
613 stream, bt_ctf_stream_get_name(stream));
d246b111
JG
614 goto end;
615 }
19abc2c6 616
b3376dd9
PP
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: "
19abc2c6
PP
642 "stream-addr=%p, stream-name=\"%s\"",
643 stream, bt_ctf_stream_get_name(stream));
644
d246b111
JG
645end:
646 return ret;
647}
648
123fbdec 649static
e6a8e8e4 650void release_event(struct bt_ctf_event *event)
123fbdec 651{
e6a8e8e4
JG
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 }
123fbdec
JG
663}
664
319fd969
PP
665static
666int create_stream_file(struct bt_ctf_writer *writer,
667 struct bt_ctf_stream *stream)
668{
669 int fd;
ab22d18f
PP
670 GString *filename = g_string_new(NULL);
671 int64_t stream_class_id;
672 char *file_path = NULL;
319fd969 673
19abc2c6
PP
674 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
675 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
676 writer, stream, bt_ctf_stream_get_name(stream),
677 stream->stream_class, stream->stream_class->name->str);
678
ab22d18f
PP
679 if (stream->name && stream->name->len > 0) {
680 /* Use stream name's base name as prefix */
681 gchar *basename = g_path_get_basename(stream->name->str);
682
683 assert(basename);
684
685 if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) {
686 g_string_assign(filename, "stream");
687 } else {
688 g_string_assign(filename, basename);
689 }
690
691 g_free(basename);
692 goto append_ids;
693 }
694
695 if (stream->stream_class->name &&
696 stream->stream_class->name->len > 0) {
697 /* Use stream class name's base name as prefix */
698 gchar *basename =
699 g_path_get_basename(stream->stream_class->name->str);
700
701 assert(basename);
702
703 if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) {
704 g_string_assign(filename, "stream");
705 } else {
706 g_string_assign(filename, basename);
319fd969
PP
707 }
708
ab22d18f
PP
709 g_free(basename);
710 goto append_ids;
319fd969
PP
711 }
712
ab22d18f
PP
713 /* Default to using `stream-` as prefix */
714 g_string_assign(filename, "stream");
715
716append_ids:
717 stream_class_id = bt_ctf_stream_class_get_id(stream->stream_class);
718 assert(stream_class_id >= 0);
719 assert(stream->id >= 0);
720 g_string_append_printf(filename, "-%" PRId64 "-%" PRId64,
721 stream_class_id, stream->id);
722
ebd04048
MJ
723 file_path = g_build_filename(writer->path->str, filename->str, NULL);
724 if (file_path == NULL) {
725 fd = -1;
726 goto end;
727 }
ab22d18f 728
ebd04048 729 fd = open(file_path,
319fd969
PP
730 O_RDWR | O_CREAT | O_TRUNC,
731 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
ebd04048 732 g_free(file_path);
19abc2c6 733 if (fd < 0) {
b3376dd9 734 BT_LOGW("Failed to open stream file for writing: %s: "
ebd04048 735 "filename=\"%s\", "
b3376dd9 736 "ret=%d, errno=%d", strerror(errno),
ebd04048 737 filename->str, fd, errno);
19abc2c6
PP
738 goto end;
739 }
740
741 BT_LOGD("Created stream file for writing: "
ebd04048 742 "stream-addr=%p, stream-name=\"%s\", "
19abc2c6 743 "filename=\"%s\", fd=%d", stream, bt_ctf_stream_get_name(stream),
ebd04048 744 filename->str, fd);
19abc2c6
PP
745
746end:
319fd969
PP
747 g_string_free(filename, TRUE);
748 return fd;
749}
750
751static
19abc2c6 752void set_stream_fd(struct bt_ctf_stream *stream, int fd)
319fd969 753{
dc3fffef 754 (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR);
319fd969 755 stream->pos.fd = fd;
319fd969
PP
756}
757
3230ee6b
PP
758static
759void component_destroy_listener(struct bt_component *component, void *data)
760{
761 struct bt_ctf_stream *stream = data;
762
19abc2c6
PP
763 BT_LOGD("Component is being destroyed, stream is notified: "
764 "comp-addr=%p, stream-addr=%p", component, stream);
3230ee6b
PP
765 g_hash_table_remove(stream->comp_cur_port, component);
766}
767
cfe11c58
PP
768static
769struct bt_ctf_stream *bt_ctf_stream_create_with_id_no_check(
b71d7298 770 struct bt_ctf_stream_class *stream_class,
cfe11c58 771 const char *name, uint64_t id)
273b65be 772{
12c8a1a3 773 int ret;
273b65be 774 struct bt_ctf_stream *stream = NULL;
319fd969
PP
775 struct bt_ctf_trace *trace = NULL;
776 struct bt_ctf_writer *writer = NULL;
273b65be 777
319fd969 778 if (!stream_class) {
19abc2c6 779 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
b71d7298 780 goto error;
319fd969
PP
781 }
782
19abc2c6 783 BT_LOGD("Creating stream object: stream-class-addr=%p, "
cfe11c58
PP
784 "stream-class-name=\"%s\", stream-name=\"%s\", "
785 "stream-id=%" PRIu64,
19abc2c6 786 stream_class, bt_ctf_stream_class_get_name(stream_class),
cfe11c58
PP
787 name, id);
788 trace = bt_ctf_stream_class_borrow_trace(stream_class);
319fd969 789 if (!trace) {
19abc2c6 790 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
b3376dd9
PP
791 "stream-class-addr=%p, stream-class-name=\"%s\", "
792 "stream-name=\"%s\"",
19abc2c6
PP
793 stream_class, bt_ctf_stream_class_get_name(stream_class),
794 name);
b71d7298 795 goto error;
273b65be
JG
796 }
797
5acf2ae6
PP
798 if (bt_ctf_trace_is_static(trace)) {
799 /*
800 * A static trace has the property that all its stream
801 * classes, clock classes, and streams are definitive:
802 * no more can be added, and each object is also frozen.
803 */
19abc2c6 804 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: "
b3376dd9
PP
805 "stream-class-addr=%p, stream-class-name=\"%s\", "
806 "stream-name=\"%s\", trace-addr=%p",
19abc2c6 807 stream_class, bt_ctf_stream_class_get_name(stream_class),
b3376dd9 808 name, trace);
5acf2ae6
PP
809 goto error;
810 }
811
cfe11c58
PP
812 if (id != -1ULL) {
813 /*
814 * Validate that the given ID is unique amongst all the
815 * existing trace's streams created from the same stream
816 * class.
817 */
818 size_t i;
819
820 for (i = 0; i < trace->streams->len; i++) {
821 struct bt_ctf_stream *trace_stream =
822 g_ptr_array_index(trace->streams, i);
823
824 if (trace_stream->stream_class != stream_class) {
825 continue;
826 }
827
828 if (trace_stream->id == id) {
829 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
830 goto error;
831 }
832 }
833 }
834
273b65be
JG
835 stream = g_new0(struct bt_ctf_stream, 1);
836 if (!stream) {
19abc2c6 837 BT_LOGE_STR("Failed to allocate one stream.");
b71d7298 838 goto error;
273b65be
JG
839 }
840
83509119 841 bt_object_init(stream, bt_ctf_stream_destroy);
e6a8e8e4
JG
842 /*
843 * Acquire reference to parent since stream will become publicly
844 * reachable; it needs its parent to remain valid.
845 */
846 bt_object_set_parent(stream, trace);
273b65be 847 stream->stream_class = stream_class;
319fd969 848 stream->pos.fd = -1;
cfe11c58 849 stream->id = (int64_t) id;
d246b111 850
3230ee6b
PP
851 stream->destroy_listeners = g_array_new(FALSE, TRUE,
852 sizeof(struct bt_ctf_stream_destroy_listener));
853 if (!stream->destroy_listeners) {
19abc2c6 854 BT_LOGE_STR("Failed to allocate a GArray.");
3230ee6b
PP
855 goto error;
856 }
857
b71d7298
PP
858 if (name) {
859 stream->name = g_string_new(name);
860 if (!stream->name) {
19abc2c6 861 BT_LOGE_STR("Failed to allocate a GString.");
b71d7298
PP
862 goto error;
863 }
864 }
865
19abc2c6
PP
866 BT_LOGD("Set stream's trace parent: trace-addr=%p", trace);
867
319fd969
PP
868 if (trace->is_created_by_writer) {
869 int fd;
cfe11c58
PP
870 writer = (struct bt_ctf_writer *) bt_object_get_parent(trace);
871 stream->id = (int64_t) stream_class->next_stream_id++;
319fd969 872
19abc2c6
PP
873 BT_LOGD("Stream object belongs to a writer's trace: "
874 "writer-addr=%p", writer);
319fd969 875 assert(writer);
b3376dd9 876
98edd02c 877 if (stream_class->packet_context_type) {
19abc2c6
PP
878 BT_LOGD("Creating stream's packet context field: "
879 "ft-addr=%p", stream_class->packet_context_type);
98edd02c
JG
880 stream->packet_context = bt_ctf_field_create(
881 stream_class->packet_context_type);
882 if (!stream->packet_context) {
19abc2c6 883 BT_LOGW_STR("Cannot create stream's packet context field.");
98edd02c
JG
884 goto error;
885 }
319fd969 886
98edd02c 887 /* Initialize events_discarded */
af9296f3 888 ret = try_set_structure_field_integer(
19abc2c6 889 stream->packet_context, "events_discarded", 0);
7ee636df 890 if (ret < 0) {
19abc2c6
PP
891 BT_LOGW("Cannot set `events_discarded` field in packet context: "
892 "ret=%d, packet-context-field-addr=%p",
893 ret, stream->packet_context);
98edd02c
JG
894 goto error;
895 }
319fd969
PP
896 }
897
898 stream->events = g_ptr_array_new_with_free_func(
899 (GDestroyNotify) release_event);
900 if (!stream->events) {
19abc2c6 901 BT_LOGE_STR("Failed to allocate a GPtrArray.");
319fd969
PP
902 goto error;
903 }
904
b3376dd9
PP
905 if (trace->packet_header_type) {
906 BT_LOGD("Creating stream's packet header field: "
907 "ft-addr=%p", trace->packet_header_type);
908 stream->packet_header =
909 bt_ctf_field_create(trace->packet_header_type);
910 if (!stream->packet_header) {
911 BT_LOGW_STR("Cannot create stream's packet header field.");
912 goto error;
913 }
319fd969
PP
914 }
915
916 /*
917 * Attempt to populate the default trace packet header fields
918 * (magic, uuid and stream_id). This will _not_ fail shall the
919 * fields not be found or be of an incompatible type; they will
920 * simply not be populated automatically. The user will have to
921 * make sure to set the trace packet header fields himself
922 * before flushing.
923 */
b3376dd9 924 ret = auto_populate_packet_header(stream);
319fd969 925 if (ret) {
b3376dd9 926 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
319fd969
PP
927 goto error;
928 }
929
930 /* Create file associated with this stream */
931 fd = create_stream_file(writer, stream);
932 if (fd < 0) {
19abc2c6 933 BT_LOGW_STR("Cannot create stream file.");
319fd969
PP
934 goto error;
935 }
936
19abc2c6 937 set_stream_fd(stream, fd);
319fd969
PP
938
939 /* Freeze the writer */
19abc2c6 940 BT_LOGD_STR("Freezing stream's CTF writer.");
319fd969
PP
941 bt_ctf_writer_freeze(writer);
942 } else {
943 /* Non-writer stream indicated by a negative FD */
19abc2c6 944 set_stream_fd(stream, -1);
3230ee6b
PP
945 stream->comp_cur_port = g_hash_table_new(g_direct_hash,
946 g_direct_equal);
947 if (!stream->comp_cur_port) {
19abc2c6 948 BT_LOGE_STR("Failed to allocate a GHashTable.");
3230ee6b
PP
949 goto error;
950 }
d246b111 951 }
319fd969
PP
952
953 /* Add this stream to the trace's streams */
954 g_ptr_array_add(trace->streams, stream);
19abc2c6 955 BT_LOGD("Created stream object: addr=%p", stream);
cfe11c58
PP
956 goto end;
957
83509119
JG
958error:
959 BT_PUT(stream);
cfe11c58
PP
960
961end:
962 bt_put(writer);
963 return stream;
964}
965
966struct bt_ctf_stream *bt_ctf_stream_create_with_id(
967 struct bt_ctf_stream_class *stream_class,
968 const char *name, uint64_t id_param)
969{
970 struct bt_ctf_trace *trace;
971 struct bt_ctf_stream *stream = NULL;
972 int64_t id = (int64_t) id_param;
973
974 if (!stream_class) {
975 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
976 goto end;
977 }
978
979 if (id < 0) {
980 BT_LOGW("Invalid parameter: invalid stream's ID: "
981 "name=\"%s\", id=%" PRIu64,
982 name, id_param);
983 goto end;
984 }
985
986 trace = bt_ctf_stream_class_borrow_trace(stream_class);
987 if (!trace) {
988 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
989 "stream-class-addr=%p, stream-class-name=\"%s\", "
990 "stream-name=\"%s\", stream-id=%" PRIu64,
991 stream_class, bt_ctf_stream_class_get_name(stream_class),
992 name, id_param);
993 goto end;
994 }
995
996 if (trace->is_created_by_writer) {
997 BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_ctf_stream_create(): "
998 "stream-class-addr=%p, stream-class-name=\"%s\", "
999 "stream-name=\"%s\", stream-id=%" PRIu64,
1000 stream_class, bt_ctf_stream_class_get_name(stream_class),
1001 name, id_param);
1002 goto end;
1003 }
1004
1005 stream = bt_ctf_stream_create_with_id_no_check(stream_class,
1006 name, id_param);
1007
1008end:
83509119 1009 return stream;
273b65be
JG
1010}
1011
cfe11c58
PP
1012struct bt_ctf_stream *bt_ctf_stream_create(
1013 struct bt_ctf_stream_class *stream_class,
1014 const char *name)
1015{
1016 return bt_ctf_stream_create_with_id_no_check(stream_class,
1017 name, -1ULL);
1018}
1019
3baf0856
JG
1020struct bt_ctf_stream_class *bt_ctf_stream_get_class(
1021 struct bt_ctf_stream *stream)
1022{
1023 struct bt_ctf_stream_class *stream_class = NULL;
1024
1025 if (!stream) {
19abc2c6 1026 BT_LOGW_STR("Invalid parameter: stream is NULL.");
3baf0856
JG
1027 goto end;
1028 }
1029
1030 stream_class = stream->stream_class;
83509119 1031 bt_get(stream_class);
3baf0856
JG
1032end:
1033 return stream_class;
1034}
1035
37949e9e 1036int bt_ctf_stream_get_discarded_events_count(
a78a2e25
JG
1037 struct bt_ctf_stream *stream, uint64_t *count)
1038{
37949e9e 1039 int ret = 0;
a78a2e25 1040
19abc2c6
PP
1041 if (!stream) {
1042 BT_LOGW_STR("Invalid parameter: stream is NULL.");
37949e9e 1043 ret = -1;
19abc2c6
PP
1044 goto end;
1045 }
1046
1047 if (!count) {
1048 BT_LOGW_STR("Invalid parameter: count is NULL.");
37949e9e 1049 ret = -1;
19abc2c6
PP
1050 goto end;
1051 }
1052
b3376dd9
PP
1053 if (stream->pos.fd < 0) {
1054 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
19abc2c6
PP
1055 "stream-addr=%p, stream-name=\"%s\"",
1056 stream, bt_ctf_stream_get_name(stream));
37949e9e 1057 ret = -1;
19abc2c6
PP
1058 goto end;
1059 }
1060
b3376dd9
PP
1061 *count = (uint64_t) stream->discarded_events;
1062
1063end:
1064 return ret;
1065}
1066
1067static
1068int set_packet_context_events_discarded_field(struct bt_ctf_stream *stream,
1069 uint64_t count)
1070{
1071 int ret = 0;
1072 struct bt_ctf_field *events_discarded_field = NULL;
1073
1074 if (!stream->packet_context) {
a78a2e25
JG
1075 goto end;
1076 }
1077
12c8a1a3
JG
1078 events_discarded_field = bt_ctf_field_structure_get_field(
1079 stream->packet_context, "events_discarded");
1080 if (!events_discarded_field) {
12c8a1a3
JG
1081 goto end;
1082 }
1083
b3376dd9
PP
1084 ret = bt_ctf_field_unsigned_integer_set_value(
1085 events_discarded_field, count);
1086 if (ret) {
1087 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1088 "field-addr=%p, value=%" PRIu64,
12c8a1a3 1089 events_discarded_field, count);
b3376dd9 1090 goto end;
12c8a1a3 1091 }
b3376dd9 1092
a78a2e25 1093end:
83509119 1094 bt_put(events_discarded_field);
a78a2e25
JG
1095 return ret;
1096}
1097
273b65be
JG
1098void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
1099 uint64_t event_count)
1100{
12c8a1a3 1101 int ret;
12c8a1a3
JG
1102 uint64_t new_count;
1103 struct bt_ctf_field *events_discarded_field = NULL;
12c8a1a3 1104
19abc2c6
PP
1105 if (!stream) {
1106 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1107 goto end;
1108 }
1109
1110 BT_LOGV("Appending discarded events to stream: "
1111 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1112 stream, bt_ctf_stream_get_name(stream), event_count);
1113
1114 if (!stream->packet_context) {
1115 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1116 goto end;
1117 }
1118
1119 if (stream->pos.fd < 0) {
1120 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
12c8a1a3
JG
1121 goto end;
1122 }
1123
12c8a1a3
JG
1124 events_discarded_field = bt_ctf_field_structure_get_field(
1125 stream->packet_context, "events_discarded");
1126 if (!events_discarded_field) {
b3376dd9 1127 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
12c8a1a3
JG
1128 goto end;
1129 }
1130
b3376dd9
PP
1131 new_count = stream->discarded_events + event_count;
1132 if (new_count < stream->discarded_events) {
1133 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1134 "cur-count=%" PRIu64 ", new-count=%" PRIu64,
1135 stream->discarded_events, new_count);
1136 goto end;
1137 }
1138
1139 ret = set_packet_context_events_discarded_field(stream, new_count);
1140 if (ret) {
1141 /* set_packet_context_events_discarded_field() logs errors */
1142 goto end;
12c8a1a3
JG
1143 }
1144
b3376dd9 1145 stream->discarded_events = new_count;
19abc2c6
PP
1146 BT_LOGV("Appended discarded events to stream: "
1147 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64,
1148 stream, bt_ctf_stream_get_name(stream), event_count);
1149
12c8a1a3 1150end:
83509119 1151 bt_put(events_discarded_field);
273b65be
JG
1152}
1153
ac0c6bdd
PP
1154static int auto_populate_event_header(struct bt_ctf_stream *stream,
1155 struct bt_ctf_event *event)
1156{
1157 int ret = 0;
1158 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
1159 struct bt_ctf_clock_class *mapped_clock_class = NULL;
19abc2c6 1160 uint64_t event_class_id;
ac0c6bdd 1161
19abc2c6
PP
1162 assert(event);
1163
06b019a7
JD
1164 if (!event->event_header) {
1165 goto end;
1166 }
1167
19abc2c6
PP
1168 if (event->frozen) {
1169 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
ac0c6bdd
PP
1170 ret = -1;
1171 goto end;
1172 }
1173
b3376dd9 1174 BT_LOGV("Automatically populating event's header field: "
19abc2c6
PP
1175 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1176 stream, bt_ctf_stream_get_name(stream), event);
1177
ac0c6bdd 1178 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
19abc2c6
PP
1179 event_class_id = (uint64_t) bt_ctf_event_class_get_id(event->event_class);
1180 assert(event_class_id >= 0);
b3376dd9 1181 if (id_field && bt_ctf_field_type_is_integer(id_field->type)) {
19abc2c6 1182 ret = set_integer_field_value(id_field, event_class_id);
ac0c6bdd 1183 if (ret) {
19abc2c6
PP
1184 BT_LOGW("Cannot set event header's `id` field's value: "
1185 "addr=%p, value=%" PRIu64, id_field,
1186 event_class_id);
ac0c6bdd
PP
1187 goto end;
1188 }
1189 }
1190
1191 /*
1192 * The conditions to automatically set the timestamp are:
1193 *
1194 * 1. The event header field "timestamp" exists and is an
1195 * integer field.
1196 * 2. This stream's class has a registered clock (set with
1197 * bt_ctf_stream_class_set_clock()).
1198 * 3. The event header field "timestamp" has its type mapped to
1199 * a clock class which is also the clock class of this
1200 * stream's class's registered clock.
ac0c6bdd
PP
1201 */
1202 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
1203 "timestamp");
b3376dd9
PP
1204 if (timestamp_field && stream->stream_class->clock &&
1205 bt_ctf_field_type_is_integer(timestamp_field->type)) {
ac0c6bdd
PP
1206 struct bt_ctf_clock_class *stream_class_clock_class =
1207 stream->stream_class->clock->clock_class;
ac0c6bdd 1208
ac0c6bdd
PP
1209 mapped_clock_class =
1210 bt_ctf_field_type_integer_get_mapped_clock_class(
b3376dd9 1211 timestamp_field->type);
ac0c6bdd
PP
1212 if (mapped_clock_class == stream_class_clock_class) {
1213 uint64_t timestamp;
1214
1215 ret = bt_ctf_clock_get_value(
1216 stream->stream_class->clock,
1217 &timestamp);
19abc2c6 1218 assert(ret == 0);
ac0c6bdd
PP
1219 ret = set_integer_field_value(timestamp_field,
1220 timestamp);
1221 if (ret) {
19abc2c6
PP
1222 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1223 "addr=%p, value=%" PRIu64,
1224 timestamp_field, timestamp);
ac0c6bdd
PP
1225 goto end;
1226 }
1227 }
1228 }
1229
b3376dd9 1230 BT_LOGV("Automatically populated event's header field: "
19abc2c6
PP
1231 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1232 stream, bt_ctf_stream_get_name(stream), event);
1233
ac0c6bdd
PP
1234end:
1235 bt_put(id_field);
1236 bt_put(timestamp_field);
1237 bt_put(mapped_clock_class);
1238 return ret;
1239}
1240
273b65be
JG
1241int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
1242 struct bt_ctf_event *event)
1243{
1244 int ret = 0;
273b65be 1245
19abc2c6
PP
1246 if (!stream) {
1247 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1248 ret = -1;
1249 goto end;
1250 }
1251
1252 if (!event) {
1253 BT_LOGW_STR("Invalid parameter: event is NULL.");
1254 ret = -1;
1255 goto end;
1256 }
1257
1258 if (stream->pos.fd < 0) {
1259 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
273b65be
JG
1260 ret = -1;
1261 goto end;
1262 }
1263
19abc2c6
PP
1264 BT_LOGV("Appending event to stream: "
1265 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1266 "event-class-name=\"%s\", event-class-id=%" PRId64,
1267 stream, bt_ctf_stream_get_name(stream), event,
1268 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event)),
1269 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event)));
1270
fa29ba83
PP
1271 /*
1272 * The event is not supposed to have a parent stream at this
1273 * point. The only other way an event can have a parent stream
1274 * is if it was assigned when setting a packet to the event,
1275 * in which case the packet's stream is not a writer stream,
1276 * and thus the user is trying to append an event which belongs
1277 * to another stream.
1278 */
1279 if (event->base.parent) {
1280 ret = -1;
1281 goto end;
1282 }
1283
e6a8e8e4 1284 bt_object_set_parent(event, stream);
19abc2c6 1285 BT_LOGV_STR("Automatically populating the header of the event to append.");
ac0c6bdd 1286 ret = auto_populate_event_header(stream, event);
662e778c 1287 if (ret) {
19abc2c6 1288 /* auto_populate_event_header() reports errors */
37f30168 1289 goto error;
662e778c
JG
1290 }
1291
5fd2e9fd 1292 /* Make sure the various scopes of the event are set */
19abc2c6 1293 BT_LOGV_STR("Validating event to append.");
273b65be
JG
1294 ret = bt_ctf_event_validate(event);
1295 if (ret) {
37f30168 1296 goto error;
273b65be
JG
1297 }
1298
0d688c15 1299 /* Save the new event and freeze it */
19abc2c6 1300 BT_LOGV_STR("Freezing the event to append.");
0d688c15 1301 bt_ctf_event_freeze(event);
273b65be 1302 g_ptr_array_add(stream->events, event);
5fd2e9fd 1303
e6a8e8e4
JG
1304 /*
1305 * Event had to hold a reference to its event class as long as it wasn't
1306 * part of the same trace hierarchy. From now on, the event and its
1307 * class share the same lifetime guarantees and the reference is no
1308 * longer needed.
1309 */
19abc2c6 1310 BT_LOGV_STR("Putting the event's class.");
e6a8e8e4 1311 bt_put(event->event_class);
19abc2c6
PP
1312 BT_LOGV("Appended event to stream: "
1313 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1314 "event-class-name=\"%s\", event-class-id=%" PRId64,
1315 stream, bt_ctf_stream_get_name(stream), event,
1316 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event)),
1317 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event)));
37f30168 1318
273b65be 1319end:
37f30168
PP
1320 return ret;
1321
1322error:
1323 /*
1324 * Orphan the event; we were not successful in associating it to
1325 * a stream.
1326 */
1327 bt_object_set_parent(event, NULL);
1328
273b65be
JG
1329 return ret;
1330}
1331
12c8a1a3
JG
1332struct bt_ctf_field *bt_ctf_stream_get_packet_context(
1333 struct bt_ctf_stream *stream)
1334{
1335 struct bt_ctf_field *packet_context = NULL;
1336
19abc2c6
PP
1337 if (!stream) {
1338 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1339 goto end;
1340 }
1341
1342 if (stream->pos.fd < 0) {
1343 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1344 "stream-addr=%p, stream-name=\"%s\"", stream,
1345 bt_ctf_stream_get_name(stream));
12c8a1a3
JG
1346 goto end;
1347 }
1348
1349 packet_context = stream->packet_context;
12c8a1a3 1350 if (packet_context) {
83509119 1351 bt_get(packet_context);
12c8a1a3 1352 }
34629a55 1353end:
12c8a1a3
JG
1354 return packet_context;
1355}
1356
1357int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
1358 struct bt_ctf_field *field)
1359{
1360 int ret = 0;
1361 struct bt_ctf_field_type *field_type;
1362
19abc2c6
PP
1363 if (!stream) {
1364 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1365 ret = -1;
1366 goto end;
1367 }
1368
1369 if (stream->pos.fd < 0) {
1370 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
12c8a1a3
JG
1371 ret = -1;
1372 goto end;
1373 }
1374
1375 field_type = bt_ctf_field_get_type(field);
09840de5
PP
1376 if (bt_ctf_field_type_compare(field_type,
1377 stream->stream_class->packet_context_type)) {
19abc2c6
PP
1378 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1379 "stream-addr=%p, stream-name=\"%s\", "
1380 "packet-context-field-addr=%p, "
1381 "packet-context-ft-addr=%p",
1382 stream, bt_ctf_stream_get_name(stream),
1383 field, field_type);
12c8a1a3
JG
1384 ret = -1;
1385 goto end;
1386 }
1387
83509119 1388 bt_put(field_type);
83509119 1389 bt_put(stream->packet_context);
835b2d10 1390 stream->packet_context = bt_get(field);
19abc2c6
PP
1391 BT_LOGV("Set stream's packet context field: "
1392 "stream-addr=%p, stream-name=\"%s\", "
1393 "packet-context-field-addr=%p",
1394 stream, bt_ctf_stream_get_name(stream), field);
12c8a1a3
JG
1395end:
1396 return ret;
1397}
1398
263a7df5
JG
1399struct bt_ctf_field *bt_ctf_stream_get_packet_header(
1400 struct bt_ctf_stream *stream)
1401{
1402 struct bt_ctf_field *packet_header = NULL;
1403
19abc2c6
PP
1404 if (!stream) {
1405 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1406 goto end;
1407 }
1408
1409 if (stream->pos.fd < 0) {
1410 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1411 "stream-addr=%p, stream-name=\"%s\"", stream,
1412 bt_ctf_stream_get_name(stream));
263a7df5
JG
1413 goto end;
1414 }
1415
1416 packet_header = stream->packet_header;
1417 if (packet_header) {
83509119 1418 bt_get(packet_header);
263a7df5
JG
1419 }
1420end:
1421 return packet_header;
1422}
1423
1424int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
1425 struct bt_ctf_field *field)
1426{
1427 int ret = 0;
e6a8e8e4 1428 struct bt_ctf_trace *trace = NULL;
263a7df5
JG
1429 struct bt_ctf_field_type *field_type = NULL;
1430
19abc2c6
PP
1431 if (!stream) {
1432 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1433 ret = -1;
1434 goto end;
1435 }
1436
1437 if (stream->pos.fd < 0) {
1438 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
263a7df5
JG
1439 ret = -1;
1440 goto end;
1441 }
1442
e6a8e8e4 1443 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
b3376dd9
PP
1444
1445 if (!field) {
1446 if (trace->packet_header_type) {
1447 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1448 "stream-addr=%p, stream-name=\"%s\", "
1449 "packet-header-field-addr=%p, "
1450 "expected-ft-addr=%p",
1451 stream, bt_ctf_stream_get_name(stream),
1452 field, trace->packet_header_type);
1453 ret = -1;
1454 goto end;
1455 }
1456
1457 goto skip_validation;
1458 }
1459
263a7df5 1460 field_type = bt_ctf_field_get_type(field);
b3376dd9
PP
1461 assert(field_type);
1462
09840de5 1463 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
19abc2c6
PP
1464 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1465 "stream-addr=%p, stream-name=\"%s\", "
1466 "packet-header-field-addr=%p, "
1467 "packet-header-ft-addr=%p",
1468 stream, bt_ctf_stream_get_name(stream),
1469 field, field_type);
263a7df5
JG
1470 ret = -1;
1471 goto end;
1472 }
1473
b3376dd9 1474skip_validation:
83509119 1475 bt_put(stream->packet_header);
835b2d10 1476 stream->packet_header = bt_get(field);
19abc2c6
PP
1477 BT_LOGV("Set stream's packet header field: "
1478 "stream-addr=%p, stream-name=\"%s\", "
1479 "packet-header-field-addr=%p",
1480 stream, bt_ctf_stream_get_name(stream), field);
263a7df5 1481end:
e6a8e8e4 1482 BT_PUT(trace);
83509119 1483 bt_put(field_type);
263a7df5
JG
1484 return ret;
1485}
1486
c9af50d1
JG
1487static
1488void reset_structure_field(struct bt_ctf_field *structure, const char *name)
1489{
1490 struct bt_ctf_field *member;
1491
1492 member = bt_ctf_field_structure_get_field(structure, name);
db1af8cc
JD
1493 if (member) {
1494 (void) bt_ctf_field_reset(member);
1495 bt_put(member);
1496 }
c9af50d1
JG
1497}
1498
273b65be
JG
1499int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
1500{
1501 int ret = 0;
1502 size_t i;
dc3fffef
PP
1503 struct bt_ctf_stream_pos packet_context_pos;
1504 struct bt_ctf_trace *trace;
1505 enum bt_ctf_byte_order native_byte_order;
1c1d572f 1506 bool has_packet_size = false;
273b65be 1507
19abc2c6
PP
1508 if (!stream) {
1509 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1510 ret = -1;
1511 goto end;
1512 }
1513
1514 if (stream->pos.fd < 0) {
1515 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
273b65be
JG
1516 ret = -1;
1517 goto end;
1518 }
1519
1c1d572f 1520 if (stream->packet_context) {
b3376dd9
PP
1521 struct bt_ctf_field *packet_size_field;
1522
1c1d572f
JD
1523 packet_size_field = bt_ctf_field_structure_get_field(
1524 stream->packet_context, "packet_size");
1525 has_packet_size = (packet_size_field != NULL);
1526 bt_put(packet_size_field);
1527 }
1528
1529 if (stream->flushed_packet_count == 1) {
b3376dd9
PP
1530 if (!stream->packet_context) {
1531 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1532 ret = -1;
1533 goto end;
1534 }
1535
1c1d572f 1536 if (!has_packet_size) {
b3376dd9
PP
1537 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1538 ret = -1;
1539 goto end;
1540 }
664f50c8
JG
1541 }
1542
19abc2c6
PP
1543 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1544 "stream-name=\"%s\", packet-index=%u", stream,
1545 bt_ctf_stream_get_name(stream), stream->flushed_packet_count);
dc3fffef
PP
1546 trace = bt_ctf_stream_class_borrow_trace(stream->stream_class);
1547 assert(trace);
8a716c8d 1548 native_byte_order = bt_ctf_trace_get_native_byte_order(trace);
d246b111 1549
b3376dd9 1550 ret = auto_populate_packet_header(stream);
d246b111 1551 if (ret) {
b3376dd9
PP
1552 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1553 ret = -1;
d246b111 1554 goto end;
273b65be
JG
1555 }
1556
b3376dd9
PP
1557 ret = auto_populate_packet_context(stream);
1558 if (ret) {
1559 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1560 ret = -1;
1561 goto end;
1562 }
98edd02c 1563
b3376dd9
PP
1564 /* mmap the next packet */
1565 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64,
1566 stream->pos.offset);
1567 bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR);
1568 assert(stream->pos.packet_size % 8 == 0);
273b65be 1569
b3376dd9
PP
1570 if (stream->packet_header) {
1571 BT_LOGV_STR("Serializing packet header field.");
1572 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos,
1573 native_byte_order);
1574 if (ret) {
1575 BT_LOGW("Cannot serialize stream's packet header field: "
1576 "field-addr=%p", stream->packet_header);
98edd02c
JG
1577 goto end;
1578 }
b3376dd9 1579 }
12c8a1a3 1580
b3376dd9 1581 if (stream->packet_context) {
10cb7b41
JG
1582 /* Write packet context */
1583 memcpy(&packet_context_pos, &stream->pos,
dc3fffef 1584 sizeof(packet_context_pos));
19abc2c6 1585 BT_LOGV_STR("Serializing packet context field.");
10cb7b41 1586 ret = bt_ctf_field_serialize(stream->packet_context,
dc3fffef 1587 &stream->pos, native_byte_order);
98edd02c 1588 if (ret) {
b3376dd9 1589 BT_LOGW("Cannot serialize stream's packet context field: "
19abc2c6 1590 "field-addr=%p", stream->packet_context);
98edd02c
JG
1591 goto end;
1592 }
12c8a1a3
JG
1593 }
1594
19abc2c6
PP
1595 BT_LOGV("Serializing events: count=%u", stream->events->len);
1596
273b65be
JG
1597 for (i = 0; i < stream->events->len; i++) {
1598 struct bt_ctf_event *event = g_ptr_array_index(
1599 stream->events, i);
19abc2c6
PP
1600 struct bt_ctf_event_class *event_class =
1601 bt_ctf_event_borrow_event_class(event);
1602
b3376dd9 1603 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
19abc2c6
PP
1604 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1605 "pos-offset=%" PRId64 ", packet-size=%" PRIu64,
1606 i, event, bt_ctf_event_class_get_name(event_class),
1607 bt_ctf_event_class_get_id(event_class),
1608 stream->pos.offset, stream->pos.packet_size);
273b65be 1609
273b65be 1610 /* Write event header */
06b019a7
JD
1611 if (event->event_header) {
1612 BT_LOGV_STR("Serializing event's header field.");
1613 ret = bt_ctf_field_serialize(event->event_header,
1614 &stream->pos, native_byte_order);
1615 if (ret) {
1616 BT_LOGW("Cannot serialize event's header field: "
1617 "field-addr=%p", event->event_header);
1618 goto end;
1619 }
273b65be
JG
1620 }
1621
8bfa3f9c 1622 /* Write stream event context */
5fd2e9fd 1623 if (event->stream_event_context) {
19abc2c6 1624 BT_LOGV_STR("Serializing event's stream event context field.");
8bfa3f9c 1625 ret = bt_ctf_field_serialize(
dc3fffef
PP
1626 event->stream_event_context, &stream->pos,
1627 native_byte_order);
8bfa3f9c 1628 if (ret) {
b3376dd9 1629 BT_LOGW("Cannot serialize event's stream event context field: "
19abc2c6 1630 "field-addr=%p", event->stream_event_context);
8bfa3f9c
JG
1631 goto end;
1632 }
1633 }
1634
273b65be 1635 /* Write event content */
dc3fffef
PP
1636 ret = bt_ctf_event_serialize(event, &stream->pos,
1637 native_byte_order);
273b65be 1638 if (ret) {
19abc2c6 1639 /* bt_ctf_event_serialize() logs errors */
273b65be
JG
1640 goto end;
1641 }
1642 }
1643
1c1d572f
JD
1644 if (!has_packet_size && stream->pos.offset % 8 != 0) {
1645 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1646 "but current content size is not a multiple of 8 bits: "
1647 "content-size=%" PRId64 ", "
1648 "packet-size=%" PRIu64,
1649 stream->pos.offset,
1650 stream->pos.packet_size);
1651 ret = -1;
1652 goto end;
1653 }
1654
b3376dd9 1655 assert(stream->pos.packet_size % 8 == 0);
0686ef94 1656
1c1d572f
JD
1657 /*
1658 * Remove extra padding bytes.
1659 */
1660 stream->pos.packet_size = (stream->pos.offset + 7) & ~7;
1661
98edd02c
JG
1662 if (stream->packet_context) {
1663 /*
b3376dd9
PP
1664 * The whole packet is serialized at this point. Make sure that,
1665 * if `packet_size` is missing, the current content size is
1666 * equal to the current packet size.
98edd02c 1667 */
b3376dd9
PP
1668 struct bt_ctf_field *field = bt_ctf_field_structure_get_field(
1669 stream->packet_context, "content_size");
1670
1671 bt_put(field);
1672 if (!field) {
1673 if (stream->pos.offset != stream->pos.packet_size) {
1674 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1675 "but current packet's content size is not equal to its packet size: "
1676 "content-size=%" PRId64 ", "
1677 "packet-size=%" PRIu64,
1678 stream->pos.offset,
1679 stream->pos.packet_size);
1680 ret = -1;
c9af50d1
JG
1681 goto end;
1682 }
98edd02c 1683 }
273b65be 1684
b3376dd9
PP
1685 /*
1686 * Overwrite the packet context now that the stream
1687 * position's packet and content sizes have the correct
1688 * values.
1689 *
1690 * Copy base_mma as the packet may have been remapped
1691 * (e.g. when a packet is resized).
1692 */
1693 packet_context_pos.base_mma = stream->pos.base_mma;
1694 ret = auto_populate_packet_context(stream);
1695 if (ret) {
1696 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1697 ret = -1;
1698 goto end;
98edd02c 1699 }
273b65be 1700
19abc2c6 1701 BT_LOGV("Rewriting (serializing) packet context field.");
98edd02c 1702 ret = bt_ctf_field_serialize(stream->packet_context,
dc3fffef 1703 &packet_context_pos, native_byte_order);
98edd02c 1704 if (ret) {
b3376dd9 1705 BT_LOGW("Cannot serialize stream's packet context field: "
19abc2c6 1706 "field-addr=%p", stream->packet_context);
98edd02c
JG
1707 goto end;
1708 }
273b65be
JG
1709 }
1710
1711 g_ptr_array_set_size(stream->events, 0);
1712 stream->flushed_packet_count++;
b3376dd9 1713 stream->size += stream->pos.packet_size / CHAR_BIT;
1c1d572f
JD
1714
1715 do {
1716 ret = ftruncate(stream->pos.fd, stream->size);
1717 } while (ret == -1 && errno == EINTR);
1718 if (ret == -1) {
0290b3c5
PP
1719 BT_LOGE_ERRNO("Cannot truncate stream file to new size",
1720 ": size=%" PRIu64 ", stream-addr=%p, "
1721 "stream-name=\"%s\"",
1722 stream->size, stream,
1723 bt_ctf_stream_get_name(stream));
1c1d572f
JD
1724 }
1725
273b65be 1726end:
c9af50d1 1727 /* Reset automatically-set fields. */
1c1d572f
JD
1728 if (stream->packet_context) {
1729 reset_structure_field(stream->packet_context, "timestamp_begin");
1730 reset_structure_field(stream->packet_context, "timestamp_end");
1731 reset_structure_field(stream->packet_context, "packet_size");
1732 reset_structure_field(stream->packet_context, "content_size");
1733 reset_structure_field(stream->packet_context, "events_discarded");
1734 }
cd7d8fb7
JG
1735
1736 if (ret < 0) {
1737 /*
1738 * We failed to write the packet. Its size is therefore set to 0
1739 * to ensure the next mapping is done in the same place rather
1740 * than advancing by "stream->pos.packet_size", which would
1741 * leave a corrupted packet in the trace.
1742 */
1743 stream->pos.packet_size = 0;
19abc2c6 1744 } else {
b3376dd9
PP
1745 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", "
1746 "packet-size=%" PRIu64,
1747 stream->pos.offset, stream->pos.packet_size);
cd7d8fb7 1748 }
273b65be
JG
1749 return ret;
1750}
1751
19abc2c6 1752/* Pre-2.0 CTF writer backward compatibility */
273b65be
JG
1753void bt_ctf_stream_get(struct bt_ctf_stream *stream)
1754{
83509119 1755 bt_get(stream);
273b65be
JG
1756}
1757
19abc2c6 1758/* Pre-2.0 CTF writer backward compatibility */
273b65be
JG
1759void bt_ctf_stream_put(struct bt_ctf_stream *stream)
1760{
83509119 1761 bt_put(stream);
273b65be
JG
1762}
1763
1764static
83509119 1765void bt_ctf_stream_destroy(struct bt_object *obj)
273b65be
JG
1766{
1767 struct bt_ctf_stream *stream;
3230ee6b 1768 int i;
273b65be 1769
83509119 1770 stream = container_of(obj, struct bt_ctf_stream, base);
19abc2c6
PP
1771 BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"",
1772 stream, bt_ctf_stream_get_name(stream));
3230ee6b
PP
1773
1774 /* Call destroy listeners in reverse registration order */
1775 for (i = stream->destroy_listeners->len - 1; i >= 0; i--) {
1776 struct bt_ctf_stream_destroy_listener *listener =
1777 &g_array_index(stream->destroy_listeners,
1778 struct bt_ctf_stream_destroy_listener, i);
1779
19abc2c6
PP
1780 BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d",
1781 listener->func, listener->data, i);
3230ee6b
PP
1782 listener->func(stream, listener->data);
1783 }
1784
dc3fffef 1785 (void) bt_ctf_stream_pos_fini(&stream->pos);
0686ef94
JG
1786 if (stream->pos.fd >= 0) {
1787 int ret;
1788
1789 /*
1790 * Truncate the file's size to the minimum required to fit the
1791 * last packet as we might have grown it too much on the last
1792 * mmap.
1793 */
1794 do {
1795 ret = ftruncate(stream->pos.fd, stream->size);
1796 } while (ret == -1 && errno == EINTR);
1797 if (ret) {
19abc2c6
PP
1798 BT_LOGE("Failed to truncate stream file: %s: "
1799 "ret=%d, errno=%d, size=%" PRIu64,
1800 strerror(errno), ret, errno,
1801 (uint64_t) stream->size);
0686ef94
JG
1802 }
1803
1804 if (close(stream->pos.fd)) {
19abc2c6
PP
1805 BT_LOGE("Failed to close stream file: %s: "
1806 "ret=%d, errno=%d", strerror(errno),
1807 ret, errno);
0686ef94 1808 }
9f56e450 1809 }
12c8a1a3 1810
12c8a1a3 1811 if (stream->events) {
19abc2c6 1812 BT_LOGD_STR("Putting events.");
12c8a1a3
JG
1813 g_ptr_array_free(stream->events, TRUE);
1814 }
b71d7298
PP
1815
1816 if (stream->name) {
1817 g_string_free(stream->name, TRUE);
1818 }
41ac640a 1819
3230ee6b
PP
1820 if (stream->comp_cur_port) {
1821 GHashTableIter ht_iter;
1822 gpointer comp_gptr, port_gptr;
1823
1824 /*
1825 * Since we're destroying the stream, remove the destroy
1826 * listeners that it registered for each component in
1827 * its component-port mapping hash table. Otherwise they
1828 * would be called and the stream would be accessed once
1829 * it's freed or another stream would be accessed.
1830 */
1831 g_hash_table_iter_init(&ht_iter, stream->comp_cur_port);
1832
1833 while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) {
1834 assert(comp_gptr);
1835 bt_component_remove_destroy_listener((void *) comp_gptr,
1836 component_destroy_listener, stream);
1837 }
1838
1839 g_hash_table_destroy(stream->comp_cur_port);
1840 }
1841
1842 if (stream->destroy_listeners) {
1843 g_array_free(stream->destroy_listeners, TRUE);
1844 }
1845
19abc2c6 1846 BT_LOGD_STR("Putting packet header field.");
83509119 1847 bt_put(stream->packet_header);
19abc2c6 1848 BT_LOGD_STR("Putting packet context field.");
83509119 1849 bt_put(stream->packet_context);
273b65be
JG
1850 g_free(stream);
1851}
1852
273b65be 1853static
af9296f3 1854int _set_structure_field_integer(struct bt_ctf_field *structure, char *name,
c55a9f58 1855 uint64_t value, bt_bool force)
273b65be
JG
1856{
1857 int ret = 0;
d7b1ea66 1858 struct bt_ctf_field_type *field_type = NULL;
19abc2c6 1859 struct bt_ctf_field *integer;
12c8a1a3 1860
19abc2c6
PP
1861 assert(structure);
1862 assert(name);
273b65be 1863
19abc2c6 1864 integer = bt_ctf_field_structure_get_field(structure, name);
12c8a1a3
JG
1865 if (!integer) {
1866 /* Field not found, not an error. */
19abc2c6
PP
1867 BT_LOGV("Field not found: struct-field-addr=%p, "
1868 "name=\"%s\", force=%d", structure, name, force);
12c8a1a3
JG
1869 goto end;
1870 }
1871
1872 /* Make sure the payload has not already been set. */
af9296f3 1873 if (!force && bt_ctf_field_is_set(integer)) {
12c8a1a3 1874 /* Payload already set, not an error */
19abc2c6
PP
1875 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1876 "name=\"%s\", force=%d", structure, name, force);
12c8a1a3
JG
1877 goto end;
1878 }
1879
d7b1ea66 1880 field_type = bt_ctf_field_get_type(integer);
d7b1ea66 1881 assert(field_type);
1487a16a 1882 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
d7b1ea66
JG
1883 /*
1884 * The user most likely meant for us to populate this field
1885 * automatically. However, we can only do this if the field
1886 * is an integer. Return an error.
1887 */
19abc2c6
PP
1888 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1889 "field-addr=%p, ft-addr=%p, ft-id=%s",
1890 integer, field_type,
1891 bt_ctf_field_type_id_string(field_type->id));
d7b1ea66
JG
1892 ret = -1;
1893 goto end;
1894 }
1895
1896 if (bt_ctf_field_type_integer_get_signed(field_type)) {
1897 ret = bt_ctf_field_signed_integer_set_value(integer,
1898 (int64_t) value);
1899 } else {
1900 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
1901 }
af9296f3 1902 ret = !ret ? 1 : ret;
273b65be 1903end:
83509119
JG
1904 bt_put(integer);
1905 bt_put(field_type);
273b65be
JG
1906 return ret;
1907}
b71d7298 1908
af9296f3
JG
1909/*
1910 * Returns the following codes:
1911 * 1 if the field was found and set,
1912 * 0 if nothing was done (field not found, or was already set),
1913 * <0 if an error was encoutered
1914 */
1915static
1916int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1917 uint64_t value)
1918{
c55a9f58 1919 return _set_structure_field_integer(structure, name, value, BT_FALSE);
af9296f3
JG
1920}
1921
b71d7298
PP
1922const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
1923{
1924 const char *name = NULL;
1925
1926 if (!stream) {
19abc2c6 1927 BT_LOGW_STR("Invalid parameter: stream is NULL.");
b71d7298
PP
1928 goto end;
1929 }
1930
1931 name = stream->name ? stream->name->str : NULL;
1932
1933end:
1934 return name;
1935}
98a4cbef
PP
1936
1937int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream)
1938{
1939 int ret = -1;
1940
1941 if (!stream) {
19abc2c6 1942 BT_LOGW_STR("Invalid parameter: stream is NULL.");
98a4cbef
PP
1943 goto end;
1944 }
1945
1946 ret = (stream->pos.fd >= 0);
1947
1948end:
1949 return ret;
1950}
3230ee6b
PP
1951
1952BT_HIDDEN
1953void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream *stream,
1954 struct bt_component *comp,
1955 struct bt_port *port)
1956{
1957 assert(stream);
1958 assert(comp);
1959 assert(port);
1960 assert(stream->comp_cur_port);
1961
1962 /*
1963 * Do not take a reference to the component here because we
1964 * don't want the component to exist as long as this stream
1965 * exists. Instead, keep a weak reference, but add a destroy
1966 * listener so that we remove this hash table entry when we know
1967 * the component is destroyed.
1968 */
a2136eca
PP
1969 BT_LOGV("Adding component's destroy listener for stream: "
1970 "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, "
1971 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
19abc2c6
PP
1972 stream, bt_ctf_stream_get_name(stream),
1973 comp, bt_component_get_name(comp), port,
1974 bt_port_get_name(port));
a2136eca
PP
1975 bt_component_add_destroy_listener(comp, component_destroy_listener,
1976 stream);
1977 g_hash_table_insert(stream->comp_cur_port, comp, port);
1978 BT_LOGV_STR("Mapped component to port for stream.");
3230ee6b
PP
1979}
1980
1981BT_HIDDEN
1982struct bt_port *bt_ctf_stream_port_for_component(struct bt_ctf_stream *stream,
1983 struct bt_component *comp)
1984{
1985 assert(stream);
1986 assert(comp);
1987 assert(stream->comp_cur_port);
1988 return g_hash_table_lookup(stream->comp_cur_port, comp);
1989}
1990
1991BT_HIDDEN
1992void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream *stream,
1993 bt_ctf_stream_destroy_listener_func func, void *data)
1994{
1995 struct bt_ctf_stream_destroy_listener listener;
1996
1997 assert(stream);
1998 assert(func);
1999 listener.func = func;
2000 listener.data = data;
2001 g_array_append_val(stream->destroy_listeners, listener);
19abc2c6
PP
2002 BT_LOGV("Added stream destroy listener: stream-addr=%p, "
2003 "stream-name=\"%s\", func=%p, data=%p",
2004 stream, bt_ctf_stream_get_name(stream), func, data);
3230ee6b
PP
2005}
2006
2007BT_HIDDEN
2008void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream *stream,
2009 bt_ctf_stream_destroy_listener_func func, void *data)
2010{
2011 size_t i;
2012
2013 assert(stream);
2014 assert(func);
2015
2016 for (i = 0; i < stream->destroy_listeners->len; i++) {
2017 struct bt_ctf_stream_destroy_listener *listener =
2018 &g_array_index(stream->destroy_listeners,
2019 struct bt_ctf_stream_destroy_listener, i);
2020
2021 if (listener->func == func && listener->data == data) {
2022 g_array_remove_index(stream->destroy_listeners, i);
2023 i--;
19abc2c6
PP
2024 BT_LOGV("Removed stream destroy listener: stream-addr=%p, "
2025 "stream-name=\"%s\", func=%p, data=%p",
2026 stream, bt_ctf_stream_get_name(stream),
2027 func, data);
3230ee6b
PP
2028 }
2029 }
2030}
cfe11c58
PP
2031
2032int64_t bt_ctf_stream_get_id(struct bt_ctf_stream *stream)
2033{
2034 int64_t ret;
2035
2036 if (!stream) {
2037 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2038 ret = (int64_t) -1;
2039 goto end;
2040 }
2041
2042 ret = stream->id;
2043 if (ret < 0) {
2044 BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"",
2045 stream, bt_ctf_stream_get_name(stream));
2046 }
2047
2048end:
2049 return ret;
2050}
This page took 0.146903 seconds and 4 git commands to generate.