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