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