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