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