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