4339010e1846f1b8a7fbbcbbac9a01b8e24e0a79
[babeltrace.git] / formats / ctf / ir / stream.c
1 /*
2 * stream.c
3 *
4 * Babeltrace CTF IR - Stream
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #include <babeltrace/ctf-ir/clock.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
32 #include <babeltrace/ctf-ir/event-internal.h>
33 #include <babeltrace/ctf-ir/field-types-internal.h>
34 #include <babeltrace/ctf-ir/fields-internal.h>
35 #include <babeltrace/ctf-ir/stream.h>
36 #include <babeltrace/ctf-ir/stream-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ref.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/compiler.h>
41 #include <babeltrace/align.h>
42 #include <babeltrace/ctf/ctf-index.h>
43
44 static
45 void bt_ctf_stream_destroy(struct bt_object *obj);
46 static
47 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
48
49 static
50 int set_packet_header_magic(struct bt_ctf_stream *stream)
51 {
52 int ret = 0;
53 struct bt_ctf_field_type *magic_field_type = NULL;
54 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
55 stream->packet_header, "magic");
56
57 if (!magic_field) {
58 /* No magic field found. Not an error, skip. */
59 goto end;
60 }
61
62 if (!bt_ctf_field_validate(magic_field)) {
63 /* Value already set. Not an error, skip. */
64 goto end;
65 }
66
67 magic_field_type = bt_ctf_field_get_type(magic_field);
68 assert(magic_field_type);
69
70 if (bt_ctf_field_type_get_type_id(magic_field_type) !=
71 BT_CTF_TYPE_ID_INTEGER) {
72 /* Magic field is not an integer. Not an error, skip. */
73 goto end;
74 }
75
76 if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) {
77 /*
78 * Magic field is not of the expected size.
79 * Not an error, skip.
80 */
81 goto end;
82 }
83
84 ret = bt_ctf_field_type_integer_get_signed(magic_field_type);
85 assert(ret >= 0);
86 if (ret) {
87 ret = bt_ctf_field_signed_integer_set_value(magic_field,
88 (int64_t) 0xC1FC1FC1);
89 } else {
90 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
91 (uint64_t) 0xC1FC1FC1);
92 }
93 end:
94 bt_put(magic_field);
95 bt_put(magic_field_type);
96 return ret;
97 }
98
99 static
100 int set_packet_header_uuid(struct bt_ctf_stream *stream)
101 {
102 int i, ret = 0;
103 struct bt_ctf_trace *trace = NULL;
104 struct bt_ctf_field_type *uuid_field_type = NULL;
105 struct bt_ctf_field_type *element_field_type = NULL;
106 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field(
107 stream->packet_header, "uuid");
108
109 if (!uuid_field) {
110 /* No uuid field found. Not an error, skip. */
111 goto end;
112 }
113
114 if (!bt_ctf_field_validate(uuid_field)) {
115 /* Value already set. Not an error, skip. */
116 goto end;
117 }
118
119 uuid_field_type = bt_ctf_field_get_type(uuid_field);
120 assert(uuid_field_type);
121 if (bt_ctf_field_type_get_type_id(uuid_field_type) !=
122 BT_CTF_TYPE_ID_ARRAY) {
123 /* UUID field is not an array. Not an error, skip. */
124 goto end;
125 }
126
127 if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) {
128 /*
129 * UUID field is not of the expected size.
130 * Not an error, skip.
131 */
132 goto end;
133 }
134
135 element_field_type = bt_ctf_field_type_array_get_element_type(
136 uuid_field_type);
137 assert(element_field_type);
138 if (bt_ctf_field_type_get_type_id(element_field_type) !=
139 BT_CTF_TYPE_ID_INTEGER) {
140 /* UUID array elements are not integers. Not an error, skip */
141 goto end;
142 }
143
144 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
145 for (i = 0; i < 16; i++) {
146 struct bt_ctf_field *uuid_element =
147 bt_ctf_field_array_get_field(uuid_field, i);
148
149 ret = bt_ctf_field_type_integer_get_signed(element_field_type);
150 assert(ret >= 0);
151
152 if (ret) {
153 ret = bt_ctf_field_signed_integer_set_value(
154 uuid_element, (int64_t) trace->uuid[i]);
155 } else {
156 ret = bt_ctf_field_unsigned_integer_set_value(
157 uuid_element,
158 (uint64_t) trace->uuid[i]);
159 }
160 bt_put(uuid_element);
161 if (ret) {
162 goto end;
163 }
164 }
165
166 end:
167 bt_put(uuid_field);
168 bt_put(uuid_field_type);
169 bt_put(element_field_type);
170 BT_PUT(trace);
171 return ret;
172 }
173 static
174 int set_packet_header_stream_id(struct bt_ctf_stream *stream)
175 {
176 int ret = 0;
177 uint32_t stream_id;
178 struct bt_ctf_field_type *stream_id_field_type = NULL;
179 struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field(
180 stream->packet_header, "stream_id");
181
182 if (!stream_id_field) {
183 /* No stream_id field found. Not an error, skip. */
184 goto end;
185 }
186
187 if (!bt_ctf_field_validate(stream_id_field)) {
188 /* Value already set. Not an error, skip. */
189 goto end;
190 }
191
192 stream_id_field_type = bt_ctf_field_get_type(stream_id_field);
193 assert(stream_id_field_type);
194 if (bt_ctf_field_type_get_type_id(stream_id_field_type) !=
195 BT_CTF_TYPE_ID_INTEGER) {
196 /* stream_id field is not an integer. Not an error, skip. */
197 goto end;
198 }
199
200 stream_id = stream->stream_class->id;
201 ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type);
202 assert(ret >= 0);
203 if (ret) {
204 ret = bt_ctf_field_signed_integer_set_value(stream_id_field,
205 (int64_t) stream_id);
206 } else {
207 ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field,
208 (uint64_t) stream_id);
209 }
210 end:
211 bt_put(stream_id_field);
212 bt_put(stream_id_field_type);
213 return ret;
214 }
215
216 static
217 int set_packet_header(struct bt_ctf_stream *stream)
218 {
219 int ret;
220
221 ret = set_packet_header_magic(stream);
222 if (ret) {
223 goto end;
224 }
225
226 ret = set_packet_header_uuid(stream);
227 if (ret) {
228 goto end;
229 }
230
231 ret = set_packet_header_stream_id(stream);
232 if (ret) {
233 goto end;
234 }
235 end:
236 return ret;
237 }
238
239 static
240 void release_event(struct bt_ctf_event *event)
241 {
242 if (bt_object_get_ref_count(event)) {
243 /*
244 * The event is being orphaned, but it must guarantee the
245 * existence of its event class for the duration of its
246 * lifetime.
247 */
248 bt_get(event->event_class);
249 BT_PUT(event->base.parent);
250 } else {
251 bt_object_release(event);
252 }
253 }
254
255 BT_HIDDEN
256 struct bt_ctf_stream *bt_ctf_stream_create(
257 struct bt_ctf_stream_class *stream_class,
258 struct bt_ctf_trace *trace)
259 {
260 int ret;
261 struct bt_ctf_stream *stream = NULL;
262
263 if (!stream_class || !trace) {
264 goto end;
265 }
266
267 stream = g_new0(struct bt_ctf_stream, 1);
268 if (!stream) {
269 goto end;
270 }
271
272 bt_object_init(stream, bt_ctf_stream_destroy);
273 /*
274 * Acquire reference to parent since stream will become publicly
275 * reachable; it needs its parent to remain valid.
276 */
277 bt_object_set_parent(stream, trace);
278 stream->packet_context = bt_ctf_field_create(
279 stream_class->packet_context_type);
280 if (!stream->packet_context) {
281 goto error;
282 }
283
284 /* Initialize events_discarded */
285 ret = set_structure_field_integer(stream->packet_context,
286 "events_discarded", 0);
287 if (ret) {
288 goto error;
289 }
290
291 stream->pos.fd = -1;
292 stream->id = stream_class->next_stream_id++;
293 stream->stream_class = stream_class;
294 stream->events = g_ptr_array_new_with_free_func(
295 (GDestroyNotify) release_event);
296 if (!stream->events) {
297 goto error;
298 }
299
300 /* A trace is not allowed to have a NULL packet header */
301 assert(trace->packet_header_type);
302 stream->packet_header = bt_ctf_field_create(trace->packet_header_type);
303 if (!stream->packet_header) {
304 goto error;
305 }
306 /*
307 * Attempt to populate the default trace packet header fields
308 * (magic, uuid and stream_id). This will _not_ fail shall the
309 * fields not be found or be of an incompatible type; they will
310 * simply not be populated automatically. The user will have to
311 * make sure to set the trace packet header fields himself before
312 * flushing.
313 */
314 ret = set_packet_header(stream);
315 if (ret) {
316 goto error;
317 }
318 end:
319 return stream;
320 error:
321 BT_PUT(stream);
322 bt_put(trace);
323 return stream;
324 }
325
326 BT_HIDDEN
327 int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
328 {
329 int ret = 0;
330
331 if (stream->pos.fd != -1) {
332 ret = -1;
333 goto end;
334 }
335
336 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
337 stream->pos.fd = fd;
338 end:
339 return ret;
340 }
341
342 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
343 struct bt_ctf_stream *stream)
344 {
345 struct bt_ctf_stream_class *stream_class = NULL;
346
347 if (!stream) {
348 goto end;
349 }
350
351 stream_class = stream->stream_class;
352 bt_get(stream_class);
353 end:
354 return stream_class;
355 }
356
357 int bt_ctf_stream_get_discarded_events_count(
358 struct bt_ctf_stream *stream, uint64_t *count)
359 {
360 int64_t ret = 0;
361 int field_signed;
362 struct bt_ctf_field *events_discarded_field = NULL;
363 struct bt_ctf_field_type *events_discarded_field_type = NULL;
364
365 if (!stream || !count || !stream->packet_context ||
366 stream->pos.fd < 0) {
367 ret = -1;
368 goto end;
369 }
370
371 events_discarded_field = bt_ctf_field_structure_get_field(
372 stream->packet_context, "events_discarded");
373 if (!events_discarded_field) {
374 ret = -1;
375 goto end;
376 }
377
378 events_discarded_field_type = bt_ctf_field_get_type(
379 events_discarded_field);
380 if (!events_discarded_field_type) {
381 ret = -1;
382 goto end;
383 }
384
385 field_signed = bt_ctf_field_type_integer_get_signed(
386 events_discarded_field_type);
387 if (field_signed < 0) {
388 ret = field_signed;
389 goto end;
390 }
391
392 if (field_signed) {
393 int64_t signed_count;
394
395 ret = bt_ctf_field_signed_integer_get_value(
396 events_discarded_field, &signed_count);
397 if (ret) {
398 goto end;
399 }
400 if (signed_count < 0) {
401 /* Invalid value */
402 ret = -1;
403 goto end;
404 }
405 *count = (uint64_t) signed_count;
406 } else {
407 ret = bt_ctf_field_unsigned_integer_get_value(
408 events_discarded_field, count);
409 if (ret) {
410 goto end;
411 }
412 }
413 end:
414 bt_put(events_discarded_field);
415 bt_put(events_discarded_field_type);
416 return ret;
417 }
418
419 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
420 uint64_t event_count)
421 {
422 int ret;
423 int field_signed;
424 uint64_t previous_count;
425 uint64_t new_count;
426 struct bt_ctf_field *events_discarded_field = NULL;
427 struct bt_ctf_field_type *events_discarded_field_type = NULL;
428
429 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
430 goto end;
431 }
432
433 ret = bt_ctf_stream_get_discarded_events_count(stream,
434 &previous_count);
435 if (ret) {
436 goto end;
437 }
438
439 events_discarded_field = bt_ctf_field_structure_get_field(
440 stream->packet_context, "events_discarded");
441 if (!events_discarded_field) {
442 goto end;
443 }
444
445 events_discarded_field_type = bt_ctf_field_get_type(
446 events_discarded_field);
447 if (!events_discarded_field_type) {
448 goto end;
449 }
450
451 field_signed = bt_ctf_field_type_integer_get_signed(
452 events_discarded_field_type);
453 if (field_signed < 0) {
454 goto end;
455 }
456
457 new_count = previous_count + event_count;
458 if (field_signed) {
459 ret = bt_ctf_field_signed_integer_set_value(
460 events_discarded_field, (int64_t) new_count);
461 if (ret) {
462 goto end;
463 }
464 } else {
465 ret = bt_ctf_field_unsigned_integer_set_value(
466 events_discarded_field, new_count);
467 if (ret) {
468 goto end;
469 }
470 }
471
472 end:
473 bt_put(events_discarded_field);
474 bt_put(events_discarded_field_type);
475 }
476
477 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
478 struct bt_ctf_event *event)
479 {
480 int ret = 0;
481
482 if (!stream || !event || stream->pos.fd < 0) {
483 ret = -1;
484 goto end;
485 }
486
487 bt_object_set_parent(event, stream);
488 ret = bt_ctf_event_populate_event_header(event);
489 if (ret) {
490 goto end;
491 }
492
493 /* Make sure the various scopes of the event are set */
494 ret = bt_ctf_event_validate(event);
495 if (ret) {
496 goto end;
497 }
498
499 /* Save the new event and freeze it */
500 bt_ctf_event_freeze(event);
501 g_ptr_array_add(stream->events, event);
502
503 /*
504 * Event had to hold a reference to its event class as long as it wasn't
505 * part of the same trace hierarchy. From now on, the event and its
506 * class share the same lifetime guarantees and the reference is no
507 * longer needed.
508 */
509 bt_put(event->event_class);
510 end:
511 if (ret) {
512 /*
513 * Orphan the event; we were not succesful in associating it to
514 * a stream.
515 */
516 bt_object_set_parent(event, NULL);
517 }
518 return ret;
519 }
520
521 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
522 struct bt_ctf_stream *stream)
523 {
524 struct bt_ctf_field *packet_context = NULL;
525
526 if (!stream || stream->pos.fd < 0) {
527 goto end;
528 }
529
530 packet_context = stream->packet_context;
531 if (packet_context) {
532 bt_get(packet_context);
533 }
534 end:
535 return packet_context;
536 }
537
538 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
539 struct bt_ctf_field *field)
540 {
541 int ret = 0;
542 struct bt_ctf_field_type *field_type;
543
544 if (!stream || !field || stream->pos.fd < 0) {
545 ret = -1;
546 goto end;
547 }
548
549 field_type = bt_ctf_field_get_type(field);
550 if (bt_ctf_field_type_compare(field_type,
551 stream->stream_class->packet_context_type)) {
552 ret = -1;
553 goto end;
554 }
555
556 bt_put(field_type);
557 bt_get(field);
558 bt_put(stream->packet_context);
559 stream->packet_context = field;
560 end:
561 return ret;
562 }
563
564 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
565 struct bt_ctf_stream *stream)
566 {
567 struct bt_ctf_field *packet_header = NULL;
568
569 if (!stream || stream->pos.fd < 0) {
570 goto end;
571 }
572
573 packet_header = stream->packet_header;
574 if (packet_header) {
575 bt_get(packet_header);
576 }
577 end:
578 return packet_header;
579 }
580
581 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
582 struct bt_ctf_field *field)
583 {
584 int ret = 0;
585 struct bt_ctf_trace *trace = NULL;
586 struct bt_ctf_field_type *field_type = NULL;
587
588 if (!stream || !field || stream->pos.fd < 0) {
589 ret = -1;
590 goto end;
591 }
592
593 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
594 field_type = bt_ctf_field_get_type(field);
595 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
596 ret = -1;
597 goto end;
598 }
599
600 bt_get(field);
601 bt_put(stream->packet_header);
602 stream->packet_header = field;
603 end:
604 BT_PUT(trace);
605 bt_put(field_type);
606 return ret;
607 }
608
609 static
610 int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
611 {
612 int ret = 0;
613 struct bt_ctf_field *timestamp_field = NULL;
614 struct bt_ctf_field_type *timestamp_field_type = NULL;
615
616 timestamp_field = bt_ctf_field_structure_get_field(event_header,
617 "timestamp");
618 if (!timestamp_field) {
619 ret = -1;
620 goto end;
621 }
622
623 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
624 assert(timestamp_field_type);
625 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
626 BT_CTF_TYPE_ID_INTEGER) {
627 ret = -1;
628 goto end;
629 }
630
631 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
632 int64_t val;
633
634 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
635 &val);
636 if (ret) {
637 goto end;
638 }
639 *timestamp = (uint64_t) val;
640 } else {
641 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
642 timestamp);
643 if (ret) {
644 goto end;
645 }
646 }
647 end:
648 bt_put(timestamp_field);
649 bt_put(timestamp_field_type);
650 return ret;
651 }
652
653 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
654 {
655 int ret = 0;
656 size_t i;
657 uint64_t timestamp_begin, timestamp_end, events_discarded;
658 struct bt_ctf_field *integer = NULL;
659 struct ctf_stream_pos packet_context_pos;
660
661 if (!stream || stream->pos.fd < 0) {
662 /*
663 * Stream does not have an associated fd. It is,
664 * therefore, not a stream being used to write events.
665 */
666 ret = -1;
667 goto end;
668 }
669
670 if (!stream->events->len) {
671 goto end;
672 }
673
674 ret = bt_ctf_field_validate(stream->packet_header);
675 if (ret) {
676 goto end;
677 }
678
679 /* mmap the next packet */
680 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
681
682 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
683 if (ret) {
684 goto end;
685 }
686
687 /* Set the default context attributes if present and unset. */
688 if (!get_event_header_timestamp(
689 ((struct bt_ctf_event *) g_ptr_array_index(
690 stream->events, 0))->event_header, &timestamp_begin)) {
691 ret = set_structure_field_integer(stream->packet_context,
692 "timestamp_begin", timestamp_begin);
693 if (ret) {
694 goto end;
695 }
696 }
697
698 if (!get_event_header_timestamp(
699 ((struct bt_ctf_event *) g_ptr_array_index(
700 stream->events, stream->events->len - 1))->event_header,
701 &timestamp_end)) {
702
703 ret = set_structure_field_integer(stream->packet_context,
704 "timestamp_end", timestamp_end);
705 if (ret) {
706 goto end;
707 }
708 }
709 ret = set_structure_field_integer(stream->packet_context,
710 "content_size", UINT64_MAX);
711 if (ret) {
712 goto end;
713 }
714
715 ret = set_structure_field_integer(stream->packet_context,
716 "packet_size", UINT64_MAX);
717 if (ret) {
718 goto end;
719 }
720
721 /* Write packet context */
722 memcpy(&packet_context_pos, &stream->pos,
723 sizeof(struct ctf_stream_pos));
724 ret = bt_ctf_field_serialize(stream->packet_context,
725 &stream->pos);
726 if (ret) {
727 goto end;
728 }
729
730 ret = bt_ctf_stream_get_discarded_events_count(stream,
731 &events_discarded);
732 if (ret) {
733 goto end;
734 }
735
736 /* Unset the packet context's fields. */
737 ret = bt_ctf_field_reset(stream->packet_context);
738 if (ret) {
739 goto end;
740 }
741
742 /* Set the previous number of discarded events. */
743 ret = set_structure_field_integer(stream->packet_context,
744 "events_discarded", events_discarded);
745 if (ret) {
746 goto end;
747 }
748
749 for (i = 0; i < stream->events->len; i++) {
750 struct bt_ctf_event *event = g_ptr_array_index(
751 stream->events, i);
752
753 ret = bt_ctf_field_reset(event->event_header);
754 if (ret) {
755 goto end;
756 }
757
758 /* Write event header */
759 ret = bt_ctf_field_serialize(event->event_header,
760 &stream->pos);
761 if (ret) {
762 goto end;
763 }
764
765 /* Write stream event context */
766 if (event->stream_event_context) {
767 ret = bt_ctf_field_serialize(
768 event->stream_event_context, &stream->pos);
769 if (ret) {
770 goto end;
771 }
772 }
773
774 /* Write event content */
775 ret = bt_ctf_event_serialize(event, &stream->pos);
776 if (ret) {
777 goto end;
778 }
779 }
780
781 /*
782 * Update the packet total size and content size and overwrite the
783 * packet context.
784 * Copy base_mma as the packet may have been remapped (e.g. when a
785 * packet is resized).
786 */
787 packet_context_pos.base_mma = stream->pos.base_mma;
788 ret = set_structure_field_integer(stream->packet_context,
789 "content_size", stream->pos.offset);
790 if (ret) {
791 goto end;
792 }
793
794 ret = set_structure_field_integer(stream->packet_context,
795 "packet_size", stream->pos.packet_size);
796 if (ret) {
797 goto end;
798 }
799
800 ret = bt_ctf_field_serialize(stream->packet_context,
801 &packet_context_pos);
802 if (ret) {
803 goto end;
804 }
805
806 g_ptr_array_set_size(stream->events, 0);
807 stream->flushed_packet_count++;
808 end:
809 bt_put(integer);
810 return ret;
811 }
812
813 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
814 {
815 bt_get(stream);
816 }
817
818 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
819 {
820 bt_put(stream);
821 }
822
823 static
824 void bt_ctf_stream_destroy(struct bt_object *obj)
825 {
826 struct bt_ctf_stream *stream;
827
828 stream = container_of(obj, struct bt_ctf_stream, base);
829 ctf_fini_pos(&stream->pos);
830 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
831 perror("close");
832 }
833
834 if (stream->events) {
835 g_ptr_array_free(stream->events, TRUE);
836 }
837 bt_put(stream->packet_header);
838 bt_put(stream->packet_context);
839 bt_put(stream->event_context);
840 g_free(stream);
841 }
842
843 static
844 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
845 uint64_t value)
846 {
847 int ret = 0;
848 struct bt_ctf_field_type *field_type = NULL;
849 struct bt_ctf_field *integer =
850 bt_ctf_field_structure_get_field(structure, name);
851
852 if (!structure || !name) {
853 ret = -1;
854 goto end;
855 }
856
857 if (!integer) {
858 /* Field not found, not an error. */
859 goto end;
860 }
861
862 /* Make sure the payload has not already been set. */
863 if (!bt_ctf_field_validate(integer)) {
864 /* Payload already set, not an error */
865 goto end;
866 }
867
868 field_type = bt_ctf_field_get_type(integer);
869 /* Something is serioulsly wrong */
870 assert(field_type);
871 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
872 /*
873 * The user most likely meant for us to populate this field
874 * automatically. However, we can only do this if the field
875 * is an integer. Return an error.
876 */
877 ret = -1;
878 goto end;
879 }
880
881 if (bt_ctf_field_type_integer_get_signed(field_type)) {
882 ret = bt_ctf_field_signed_integer_set_value(integer,
883 (int64_t) value);
884 } else {
885 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
886 }
887 end:
888 bt_put(integer);
889 bt_put(field_type);
890 return ret;
891 }
This page took 0.046434 seconds and 3 git commands to generate.