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