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