Handle negative time and offset from Epoch
[babeltrace.git] / formats / ctf / ir / event.c
1 /*
2 * event.c
3 *
4 * Babeltrace CTF IR - Event
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-writer/event.h>
30 #include <babeltrace/ctf-writer/event-types.h>
31 #include <babeltrace/ctf-writer/event-fields.h>
32 #include <babeltrace/ctf-ir/event-fields-internal.h>
33 #include <babeltrace/ctf-ir/event-types-internal.h>
34 #include <babeltrace/ctf-ir/event-internal.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/trace-internal.h>
38 #include <babeltrace/ctf-ir/utils.h>
39 #include <babeltrace/ref.h>
40 #include <babeltrace/ctf-ir/attributes-internal.h>
41 #include <babeltrace/compiler.h>
42
43 static
44 void bt_ctf_event_class_destroy(struct bt_object *obj);
45 static
46 void bt_ctf_event_destroy(struct bt_object *obj);
47 static
48 int set_integer_field_value(struct bt_ctf_field *field, uint64_t value);
49
50 struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
51 {
52 int ret;
53 struct bt_value *obj = NULL;
54 struct bt_ctf_event_class *event_class = NULL;
55
56 if (bt_ctf_validate_identifier(name)) {
57 goto error;
58 }
59
60 event_class = g_new0(struct bt_ctf_event_class, 1);
61 if (!event_class) {
62 goto error;
63 }
64
65 bt_object_init(event_class, bt_ctf_event_class_destroy);
66 event_class->fields = bt_ctf_field_type_structure_create();
67 if (!event_class->fields) {
68 goto error;
69 }
70
71 event_class->attributes = bt_ctf_attributes_create();
72 if (!event_class->attributes) {
73 goto error;
74 }
75
76 obj = bt_value_integer_create_init(-1);
77 if (!obj) {
78 goto error;
79 }
80
81 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
82 "id", obj);
83 if (ret) {
84 goto error;
85 }
86
87 BT_PUT(obj);
88
89 obj = bt_value_string_create_init(name);
90 if (!obj) {
91 goto error;
92 }
93
94 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
95 "name", obj);
96 if (ret) {
97 goto error;
98 }
99
100 BT_PUT(obj);
101
102 return event_class;
103
104 error:
105 BT_PUT(event_class);
106 BT_PUT(obj);
107 return event_class;
108 }
109
110 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class)
111 {
112 struct bt_value *obj = NULL;
113 const char *name = NULL;
114
115 if (!event_class) {
116 goto end;
117 }
118
119 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
120 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX);
121 if (!obj) {
122 goto end;
123 }
124
125 if (bt_value_string_get(obj, &name)) {
126 name = NULL;
127 }
128
129 end:
130 BT_PUT(obj);
131 return name;
132 }
133
134 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
135 {
136 struct bt_value *obj = NULL;
137 int64_t ret = 0;
138
139 if (!event_class) {
140 ret = -1;
141 goto end;
142 }
143
144 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
145 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
146 if (!obj) {
147 goto end;
148 }
149
150 if (bt_value_integer_get(obj, &ret)) {
151 ret = -1;
152 }
153
154 if (ret < 0) {
155 /* means ID is not set */
156 ret = -1;
157 goto end;
158 }
159
160 end:
161 BT_PUT(obj);
162 return ret;
163 }
164
165 int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
166 uint32_t id)
167 {
168 int ret = 0;
169 struct bt_value *obj = NULL;
170 struct bt_ctf_stream_class *stream_class = NULL;
171
172
173 if (!event_class) {
174 ret = -1;
175 goto end;
176 }
177
178 stream_class = bt_ctf_event_class_get_stream_class(event_class);
179 if (stream_class) {
180 /*
181 * We don't allow changing the id if the event class has already
182 * been added to a stream class.
183 */
184 ret = -1;
185 goto end;
186 }
187
188 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
189 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
190 if (!obj) {
191 goto end;
192 }
193
194 if (bt_value_integer_set(obj, id)) {
195 ret = -1;
196 goto end;
197 }
198
199 end:
200 BT_PUT(obj);
201 BT_PUT(stream_class);
202 return ret;
203 }
204
205 int bt_ctf_event_class_set_attribute(
206 struct bt_ctf_event_class *event_class, const char *name,
207 struct bt_value *value)
208 {
209 int ret = 0;
210
211 if (!event_class || !name || !value || event_class->frozen) {
212 ret = -1;
213 goto end;
214 }
215
216 if (!strcmp(name, "id") || !strcmp(name, "loglevel")) {
217 if (!bt_value_is_integer(value)) {
218 ret = -1;
219 goto end;
220 }
221 } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) {
222 if (!bt_value_is_string(value)) {
223 ret = -1;
224 goto end;
225 }
226 } else {
227 /* unknown attribute */
228 ret = -1;
229 goto end;
230 }
231
232 /* "id" special case: >= 0 */
233 if (!strcmp(name, "id")) {
234 int64_t val;
235
236 ret = bt_value_integer_get(value, &val);
237
238 if (ret) {
239 goto end;
240 }
241
242 if (val < 0) {
243 ret = -1;
244 goto end;
245 }
246 }
247
248 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
249 name, value);
250
251 end:
252 return ret;
253 }
254
255 int bt_ctf_event_class_get_attribute_count(
256 struct bt_ctf_event_class *event_class)
257 {
258 int ret = 0;
259
260 if (!event_class) {
261 ret = -1;
262 goto end;
263 }
264
265 ret = bt_ctf_attributes_get_count(event_class->attributes);
266
267 end:
268 return ret;
269 }
270
271 const char *
272 bt_ctf_event_class_get_attribute_name(
273 struct bt_ctf_event_class *event_class, int index)
274 {
275 const char *ret;
276
277 if (!event_class) {
278 ret = NULL;
279 goto end;
280 }
281
282 ret = bt_ctf_attributes_get_field_name(event_class->attributes, index);
283
284 end:
285 return ret;
286 }
287
288 struct bt_value *
289 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class,
290 int index)
291 {
292 struct bt_value *ret;
293
294 if (!event_class) {
295 ret = NULL;
296 goto end;
297 }
298
299 ret = bt_ctf_attributes_get_field_value(event_class->attributes, index);
300
301 end:
302 return ret;
303 }
304
305 struct bt_value *
306 bt_ctf_event_class_get_attribute_value_by_name(
307 struct bt_ctf_event_class *event_class, const char *name)
308 {
309 struct bt_value *ret;
310
311 if (!event_class || !name) {
312 ret = NULL;
313 goto end;
314 }
315
316 ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes,
317 name);
318
319 end:
320 return ret;
321
322 }
323
324 struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
325 struct bt_ctf_event_class *event_class)
326 {
327 return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class);
328 }
329
330 struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type(
331 struct bt_ctf_event_class *event_class)
332 {
333 struct bt_ctf_field_type *payload = NULL;
334
335 if (!event_class) {
336 goto end;
337 }
338
339 bt_get(event_class->fields);
340 payload = event_class->fields;
341 end:
342 return payload;
343 }
344
345 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class,
346 struct bt_ctf_field_type *payload)
347 {
348 int ret = 0;
349
350 if (!event_class || !payload ||
351 bt_ctf_field_type_get_type_id(payload) != CTF_TYPE_STRUCT) {
352 ret = -1;
353 goto end;
354 }
355
356 bt_get(payload);
357 bt_put(event_class->fields);
358 event_class->fields = payload;
359 end:
360 return ret;
361 }
362
363 int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
364 struct bt_ctf_field_type *type,
365 const char *name)
366 {
367 int ret = 0;
368
369 if (!event_class || !type || bt_ctf_validate_identifier(name) ||
370 event_class->frozen) {
371 ret = -1;
372 goto end;
373 }
374
375 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
376 CTF_TYPE_STRUCT) {
377 ret = -1;
378 goto end;
379 }
380
381 ret = bt_ctf_field_type_structure_add_field(event_class->fields,
382 type, name);
383 end:
384 return ret;
385 }
386
387 int bt_ctf_event_class_get_field_count(
388 struct bt_ctf_event_class *event_class)
389 {
390 int ret;
391
392 if (!event_class) {
393 ret = -1;
394 goto end;
395 }
396
397 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
398 CTF_TYPE_STRUCT) {
399 ret = -1;
400 goto end;
401 }
402
403 ret = bt_ctf_field_type_structure_get_field_count(event_class->fields);
404 end:
405 return ret;
406 }
407
408 int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class,
409 const char **field_name, struct bt_ctf_field_type **field_type,
410 int index)
411 {
412 int ret;
413
414 if (!event_class || index < 0) {
415 ret = -1;
416 goto end;
417 }
418
419 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
420 CTF_TYPE_STRUCT) {
421 ret = -1;
422 goto end;
423 }
424
425 ret = bt_ctf_field_type_structure_get_field(event_class->fields,
426 field_name, field_type, index);
427 end:
428 return ret;
429 }
430
431 struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(
432 struct bt_ctf_event_class *event_class, const char *name)
433 {
434 GQuark name_quark;
435 struct bt_ctf_field_type *field_type = NULL;
436
437 if (!event_class || !name) {
438 goto end;
439 }
440
441 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
442 CTF_TYPE_STRUCT) {
443 goto end;
444 }
445
446 name_quark = g_quark_try_string(name);
447 if (!name_quark) {
448 goto end;
449 }
450
451 /*
452 * No need to increment field_type's reference count since getting it
453 * from the structure already does.
454 */
455 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
456 event_class->fields, name);
457 end:
458 return field_type;
459 }
460
461 struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
462 struct bt_ctf_event_class *event_class)
463 {
464 struct bt_ctf_field_type *context_type = NULL;
465
466 if (!event_class || !event_class->context) {
467 goto end;
468 }
469
470 bt_get(event_class->context);
471 context_type = event_class->context;
472 end:
473 return context_type;
474 }
475
476 int bt_ctf_event_class_set_context_type(
477 struct bt_ctf_event_class *event_class,
478 struct bt_ctf_field_type *context)
479 {
480 int ret = 0;
481
482 if (!event_class || !context || event_class->frozen) {
483 ret = -1;
484 goto end;
485 }
486
487 if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) {
488 ret = -1;
489 goto end;
490 }
491
492 bt_get(context);
493 bt_put(event_class->context);
494 event_class->context = context;
495 end:
496 return ret;
497
498 }
499
500 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
501 {
502 bt_get(event_class);
503 }
504
505 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
506 {
507 bt_put(event_class);
508 }
509
510 BT_HIDDEN
511 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
512 uint32_t stream_id)
513 {
514 int ret = 0;
515 struct bt_value *obj;
516
517 obj = bt_value_integer_create_init(stream_id);
518
519 if (!obj) {
520 ret = -1;
521 goto end;
522 }
523
524 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
525 "stream_id", obj);
526
527 end:
528 BT_PUT(obj);
529 return ret;
530 }
531
532 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
533 {
534 struct bt_ctf_event *event = NULL;
535 struct bt_ctf_stream_class *stream_class = NULL;
536
537 if (!event_class) {
538 goto error;
539 }
540
541 stream_class = bt_ctf_event_class_get_stream_class(event_class);
542 /*
543 * We disallow the creation of an event if its event class has not been
544 * associated to a stream class.
545 */
546 if (!stream_class) {
547 goto error;
548 }
549 assert(stream_class->event_header_type);
550 event = g_new0(struct bt_ctf_event, 1);
551 if (!event) {
552 goto error;
553 }
554
555 bt_object_init(event, bt_ctf_event_destroy);
556 bt_ctf_event_class_freeze(event_class);
557 /*
558 * event does not share a common ancestor with the event class; it has
559 * to guarantee its existence by holding a reference. This reference
560 * shall be released once the event is associated to a stream since,
561 * from that point, the event and its class will share the same
562 * lifetime.
563 */
564 event->event_class = bt_get(event_class);
565
566 event->event_header = bt_ctf_field_create(
567 stream_class->event_header_type);
568 if (!event->event_header) {
569 goto error;
570 }
571 if (event_class->context) {
572 event->context_payload = bt_ctf_field_create(
573 event_class->context);
574 if (!event->context_payload) {
575 goto error;
576 }
577 }
578 event->fields_payload = bt_ctf_field_create(event_class->fields);
579 if (!event->fields_payload) {
580 goto error;
581 }
582
583 /*
584 * Freeze the stream class since the event header must not be changed
585 * anymore.
586 */
587 bt_ctf_stream_class_freeze(stream_class);
588 BT_PUT(stream_class);
589 return event;
590 error:
591 BT_PUT(event);
592 BT_PUT(stream_class);
593 return event;
594 }
595
596 struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
597 {
598 struct bt_ctf_event_class *event_class = NULL;
599
600 if (!event) {
601 goto end;
602 }
603
604 event_class = event->event_class;
605 bt_get(event_class);
606 end:
607 return event_class;
608 }
609
610 struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
611 {
612 return (struct bt_ctf_stream *) bt_object_get_parent(event);
613 }
614
615 struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event)
616 {
617 struct bt_ctf_clock *clock = NULL;
618 struct bt_ctf_event_class *event_class;
619 struct bt_ctf_stream_class *stream_class;
620
621 if (!event) {
622 goto end;
623 }
624
625 event_class = bt_ctf_event_get_class(event);
626 if (!event_class) {
627 goto end;
628 }
629
630 stream_class = bt_ctf_event_class_get_stream_class(event_class);
631 if (!stream_class) {
632 goto error_put_event_class;
633 }
634
635 clock = bt_ctf_stream_class_get_clock(stream_class);
636 if (!clock) {
637 goto error_put_stream_class;
638 }
639
640 error_put_stream_class:
641 bt_put(stream_class);
642 error_put_event_class:
643 bt_put(event_class);
644 end:
645 return clock;
646 }
647
648 int bt_ctf_event_set_payload(struct bt_ctf_event *event,
649 const char *name,
650 struct bt_ctf_field *payload)
651 {
652 int ret = 0;
653
654 if (!event || !payload) {
655 ret = -1;
656 goto end;
657 }
658
659 if (name) {
660 ret = bt_ctf_field_structure_set_field(event->fields_payload,
661 name, payload);
662 } else {
663 struct bt_ctf_field_type *payload_type;
664
665 payload_type = bt_ctf_field_get_type(payload);
666 if (payload_type == event->event_class->fields) {
667 bt_put(event->fields_payload);
668 bt_get(payload);
669 event->fields_payload = payload;
670 } else {
671 ret = -1;
672 }
673
674 bt_put(payload_type);
675 }
676 end:
677 return ret;
678 }
679
680 struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event)
681 {
682 struct bt_ctf_field *payload = NULL;
683
684 if (!event || !event->fields_payload) {
685 goto end;
686 }
687
688 payload = event->fields_payload;
689 bt_get(payload);
690 end:
691 return payload;
692 }
693
694 int bt_ctf_event_set_payload_field(struct bt_ctf_event *event,
695 struct bt_ctf_field *payload)
696 {
697 int ret = 0;
698 struct bt_ctf_field_type *payload_type = NULL;
699
700 if (!event || !payload) {
701 ret = -1;
702 goto end;
703 }
704
705 payload_type = bt_ctf_field_get_type(payload);
706 if (!payload_type) {
707 ret = -1;
708 goto end;
709 }
710
711 if (bt_ctf_field_type_get_type_id(payload_type) != CTF_TYPE_STRUCT) {
712 ret = -1;
713 goto end;
714 }
715
716 bt_get(payload);
717 bt_put(event->fields_payload);
718 event->fields_payload = payload;
719
720 end:
721 bt_put(payload_type);
722 return ret;
723 }
724
725 struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
726 const char *name)
727 {
728 struct bt_ctf_field *field = NULL;
729
730 if (!event) {
731 goto end;
732 }
733
734 if (name) {
735 field = bt_ctf_field_structure_get_field(event->fields_payload,
736 name);
737 } else {
738 field = event->fields_payload;
739 bt_get(field);
740 }
741 end:
742 return field;
743 }
744
745 struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
746 struct bt_ctf_event *event, int index)
747 {
748 struct bt_ctf_field *field = NULL;
749
750 if (!event || index < 0) {
751 goto end;
752 }
753
754 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
755 index);
756 end:
757 return field;
758 }
759
760 struct bt_ctf_field *bt_ctf_event_get_header(
761 struct bt_ctf_event *event)
762 {
763 struct bt_ctf_field *header = NULL;
764
765 if (!event || !event->event_header) {
766 goto end;
767 }
768
769 header = event->event_header;
770 bt_get(header);
771 end:
772 return header;
773 }
774
775 int bt_ctf_event_set_header(struct bt_ctf_event *event,
776 struct bt_ctf_field *header)
777 {
778 int ret = 0;
779 struct bt_ctf_field_type *field_type = NULL;
780 struct bt_ctf_stream_class *stream_class = NULL;
781
782 if (!event || !header) {
783 ret = -1;
784 goto end;
785 }
786
787 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
788 event->event_class);
789 /*
790 * Ensure the provided header's type matches the one registered to the
791 * stream class.
792 */
793 field_type = bt_ctf_field_get_type(header);
794 if (field_type != stream_class->event_header_type) {
795 ret = -1;
796 goto end;
797 }
798
799 bt_get(header);
800 bt_put(event->event_header);
801 event->event_header = header;
802 end:
803 bt_put(stream_class);
804 bt_put(field_type);
805 return ret;
806 }
807
808 struct bt_ctf_field *bt_ctf_event_get_event_context(
809 struct bt_ctf_event *event)
810 {
811 struct bt_ctf_field *context = NULL;
812
813 if (!event || !event->context_payload) {
814 goto end;
815 }
816
817 context = event->context_payload;
818 bt_get(context);
819 end:
820 return context;
821 }
822
823 int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
824 struct bt_ctf_field *context)
825 {
826 int ret = 0;
827 struct bt_ctf_field_type *field_type = NULL;
828
829 if (!event || !context) {
830 ret = -1;
831 goto end;
832 }
833
834 field_type = bt_ctf_field_get_type(context);
835 if (field_type != event->event_class->context) {
836 ret = -1;
837 goto end;
838 }
839
840 bt_get(context);
841 bt_put(event->context_payload);
842 event->context_payload = context;
843 end:
844 bt_put(field_type);
845 return ret;
846 }
847
848 void bt_ctf_event_get(struct bt_ctf_event *event)
849 {
850 bt_get(event);
851 }
852
853 void bt_ctf_event_put(struct bt_ctf_event *event)
854 {
855 bt_put(event);
856 }
857
858 static
859 void bt_ctf_event_class_destroy(struct bt_object *obj)
860 {
861 struct bt_ctf_event_class *event_class;
862
863 /*
864 * Don't call put() on the stream class. See comment in
865 * bt_ctf_event_class_set_stream_class for explanation.
866 */
867 event_class = container_of(obj, struct bt_ctf_event_class, base);
868 bt_ctf_attributes_destroy(event_class->attributes);
869 bt_put(event_class->context);
870 bt_put(event_class->fields);
871 g_free(event_class);
872 }
873
874 static
875 void bt_ctf_event_destroy(struct bt_object *obj)
876 {
877 struct bt_ctf_event *event;
878
879 event = container_of(obj, struct bt_ctf_event, base);
880 if (!event->base.parent) {
881 /*
882 * Event was keeping a reference to its class since it shared no
883 * common ancestor with it to guarantee they would both have the
884 * same lifetime.
885 */
886 bt_put(event->event_class);
887 }
888 bt_put(event->event_header);
889 bt_put(event->context_payload);
890 bt_put(event->fields_payload);
891 g_free(event);
892 }
893
894 static
895 int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
896 {
897 int ret = 0;
898 struct bt_ctf_field_type *field_type = NULL;
899
900 if (!field) {
901 ret = -1;
902 goto end;
903 }
904
905 if (!bt_ctf_field_validate(field)) {
906 /* Payload already set, skip! (not an error) */
907 goto end;
908 }
909
910 field_type = bt_ctf_field_get_type(field);
911 assert(field_type);
912
913 if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
914 /* Not an integer and the value is unset, error. */
915 ret = -1;
916 goto end;
917 }
918
919 if (bt_ctf_field_type_integer_get_signed(field_type)) {
920 ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value);
921 if (ret) {
922 /* Value is out of range, error. */
923 goto end;
924 }
925 } else {
926 ret = bt_ctf_field_unsigned_integer_set_value(field, value);
927 if (ret) {
928 /* Value is out of range, error. */
929 goto end;
930 }
931 }
932 end:
933 bt_put(field_type);
934 return ret;
935 }
936
937 BT_HIDDEN
938 void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
939 {
940 assert(event_class);
941 event_class->frozen = 1;
942 bt_ctf_field_type_freeze(event_class->context);
943 bt_ctf_field_type_freeze(event_class->fields);
944 bt_ctf_attributes_freeze(event_class->attributes);
945 }
946
947 BT_HIDDEN
948 int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
949 struct metadata_context *context)
950 {
951 int i;
952 int count;
953 int ret = 0;
954 struct bt_value *attr_value = NULL;
955
956 assert(event_class);
957 assert(context);
958
959 context->current_indentation_level = 1;
960 g_string_assign(context->field_name, "");
961 g_string_append(context->string, "event {\n");
962 count = bt_ctf_event_class_get_attribute_count(event_class);
963
964 if (count < 0) {
965 ret = -1;
966 goto end;
967 }
968
969 for (i = 0; i < count; ++i) {
970 const char *attr_name = NULL;
971
972 attr_name = bt_ctf_event_class_get_attribute_name(
973 event_class, i);
974 attr_value = bt_ctf_event_class_get_attribute_value(
975 event_class, i);
976
977 if (!attr_name || !attr_value) {
978 ret = -1;
979 goto end;
980 }
981
982 switch (bt_value_get_type(attr_value)) {
983 case BT_VALUE_TYPE_INTEGER:
984 {
985 int64_t value;
986
987 ret = bt_value_integer_get(attr_value, &value);
988
989 if (ret) {
990 goto end;
991 }
992
993 g_string_append_printf(context->string,
994 "\t%s = %" PRId64 ";\n", attr_name, value);
995 break;
996 }
997
998 case BT_VALUE_TYPE_STRING:
999 {
1000 const char *value;
1001
1002 ret = bt_value_string_get(attr_value, &value);
1003
1004 if (ret) {
1005 goto end;
1006 }
1007
1008 g_string_append_printf(context->string,
1009 "\t%s = \"%s\";\n", attr_name, value);
1010 break;
1011 }
1012
1013 default:
1014 /* should never happen */
1015 assert(false);
1016 break;
1017 }
1018
1019 BT_PUT(attr_value);
1020 }
1021
1022 if (event_class->context) {
1023 g_string_append(context->string, "\tcontext := ");
1024 ret = bt_ctf_field_type_serialize(event_class->context,
1025 context);
1026 if (ret) {
1027 goto end;
1028 }
1029 g_string_append(context->string, ";\n");
1030 }
1031
1032 if (event_class->fields) {
1033 g_string_append(context->string, "\tfields := ");
1034 ret = bt_ctf_field_type_serialize(event_class->fields, context);
1035 if (ret) {
1036 goto end;
1037 }
1038 g_string_append(context->string, ";\n");
1039 }
1040
1041 g_string_append(context->string, "};\n\n");
1042 end:
1043 context->current_indentation_level = 0;
1044 BT_PUT(attr_value);
1045 return ret;
1046 }
1047
1048 void bt_ctf_event_class_set_native_byte_order(
1049 struct bt_ctf_event_class *event_class,
1050 int byte_order)
1051 {
1052 if (!event_class) {
1053 return;
1054 }
1055
1056 bt_ctf_field_type_set_native_byte_order(event_class->context,
1057 byte_order);
1058 bt_ctf_field_type_set_native_byte_order(event_class->fields,
1059 byte_order);
1060 }
1061
1062 BT_HIDDEN
1063 int bt_ctf_event_validate(struct bt_ctf_event *event)
1064 {
1065 /* Make sure each field's payload has been set */
1066 int ret;
1067
1068 assert(event);
1069 ret = bt_ctf_field_validate(event->event_header);
1070 if (ret) {
1071 goto end;
1072 }
1073
1074 ret = bt_ctf_field_validate(event->fields_payload);
1075 if (ret) {
1076 goto end;
1077 }
1078
1079 if (event->event_class->context) {
1080 ret = bt_ctf_field_validate(event->context_payload);
1081 }
1082 end:
1083 return ret;
1084 }
1085
1086 BT_HIDDEN
1087 int bt_ctf_event_serialize(struct bt_ctf_event *event,
1088 struct ctf_stream_pos *pos)
1089 {
1090 int ret = 0;
1091
1092 assert(event);
1093 assert(pos);
1094 if (event->context_payload) {
1095 ret = bt_ctf_field_serialize(event->context_payload, pos);
1096 if (ret) {
1097 goto end;
1098 }
1099 }
1100
1101 if (event->fields_payload) {
1102 ret = bt_ctf_field_serialize(event->fields_payload, pos);
1103 if (ret) {
1104 goto end;
1105 }
1106 }
1107 end:
1108 return ret;
1109 }
1110
1111 BT_HIDDEN
1112 int bt_ctf_event_populate_event_header(struct bt_ctf_event *event)
1113 {
1114 int ret = 0;
1115 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
1116
1117 if (!event) {
1118 ret = -1;
1119 goto end;
1120 }
1121
1122 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
1123 if (id_field) {
1124 ret = set_integer_field_value(id_field,
1125 (uint64_t) bt_ctf_event_class_get_id(
1126 event->event_class));
1127 if (ret) {
1128 goto end;
1129 }
1130 }
1131
1132 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
1133 "timestamp");
1134 if (timestamp_field) {
1135 struct bt_ctf_field_type *timestamp_field_type =
1136 bt_ctf_field_get_type(timestamp_field);
1137 struct bt_ctf_clock *mapped_clock;
1138
1139 assert(timestamp_field_type);
1140 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
1141 timestamp_field_type);
1142 bt_put(timestamp_field_type);
1143 if (mapped_clock) {
1144 int64_t timestamp;
1145
1146 ret = bt_ctf_clock_get_time(mapped_clock, &timestamp);
1147 bt_put(mapped_clock);
1148 if (ret) {
1149 goto end;
1150 }
1151
1152 ret = set_integer_field_value(timestamp_field,
1153 timestamp);
1154 if (ret) {
1155 goto end;
1156 }
1157 }
1158 }
1159 end:
1160 bt_put(id_field);
1161 bt_put(timestamp_field);
1162 return ret;
1163 }
1164
1165 struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
1166 {
1167 struct bt_ctf_event *copy = NULL;
1168
1169 if (!event) {
1170 goto error;
1171 }
1172
1173 copy = g_new0(struct bt_ctf_event, 1);
1174 if (!copy) {
1175 goto error;
1176 }
1177
1178 bt_object_init(copy, bt_ctf_event_destroy);
1179 copy->event_class = bt_get(event->event_class);
1180
1181 if (event->event_header) {
1182 copy->event_header = bt_ctf_field_copy(event->event_header);
1183
1184 if (!copy->event_header) {
1185 goto error;
1186 }
1187 }
1188
1189 if (event->context_payload) {
1190 copy->context_payload = bt_ctf_field_copy(
1191 event->context_payload);
1192
1193 if (!copy->context_payload) {
1194 goto error;
1195 }
1196 }
1197
1198 if (event->fields_payload) {
1199 copy->fields_payload = bt_ctf_field_copy(event->fields_payload);
1200
1201 if (!copy->fields_payload) {
1202 goto error;
1203 }
1204 }
1205
1206 return copy;
1207
1208 error:
1209 BT_PUT(copy);
1210 return copy;
1211 }
This page took 0.058459 seconds and 4 git commands to generate.