6d878fd757c93c3fc8d170d21f6e3bdc118c6d63
[babeltrace.git] / lib / 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-ir/fields-internal.h>
30 #include <babeltrace/ctf-ir/field-types-internal.h>
31 #include <babeltrace/ctf-ir/clock-class.h>
32 #include <babeltrace/ctf-ir/event-internal.h>
33 #include <babeltrace/ctf-ir/event-class.h>
34 #include <babeltrace/ctf-ir/event-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/stream-internal.h>
38 #include <babeltrace/ctf-ir/packet.h>
39 #include <babeltrace/ctf-ir/packet-internal.h>
40 #include <babeltrace/ctf-ir/trace-internal.h>
41 #include <babeltrace/ctf-ir/validation-internal.h>
42 #include <babeltrace/ctf-ir/packet-internal.h>
43 #include <babeltrace/ctf-ir/utils.h>
44 #include <babeltrace/ctf-writer/serialize-internal.h>
45 #include <babeltrace/ref.h>
46 #include <babeltrace/ctf-ir/attributes-internal.h>
47 #include <babeltrace/compiler.h>
48
49 static
50 void bt_ctf_event_destroy(struct bt_object *obj);
51
52 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
53 {
54 int ret;
55 enum bt_ctf_validation_flag validation_flags =
56 BT_CTF_VALIDATION_FLAG_STREAM |
57 BT_CTF_VALIDATION_FLAG_EVENT;
58 struct bt_ctf_event *event = NULL;
59 struct bt_ctf_trace *trace = NULL;
60 struct bt_ctf_stream_class *stream_class = NULL;
61 struct bt_ctf_field_type *packet_header_type = NULL;
62 struct bt_ctf_field_type *packet_context_type = NULL;
63 struct bt_ctf_field_type *event_header_type = NULL;
64 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
65 struct bt_ctf_field_type *event_context_type = NULL;
66 struct bt_ctf_field_type *event_payload_type = NULL;
67 struct bt_ctf_field *event_header = NULL;
68 struct bt_ctf_field *stream_event_context = NULL;
69 struct bt_ctf_field *event_context = NULL;
70 struct bt_ctf_field *event_payload = NULL;
71 struct bt_value *environment = NULL;
72 struct bt_ctf_validation_output validation_output = { 0 };
73 int trace_valid = 0;
74
75 if (!event_class) {
76 goto error;
77 }
78
79 stream_class = bt_ctf_event_class_get_stream_class(event_class);
80
81 /*
82 * We disallow the creation of an event if its event class has not been
83 * associated to a stream class.
84 */
85 if (!stream_class) {
86 goto error;
87 }
88
89 /* A stream class should always have an existing event header type */
90 assert(stream_class->event_header_type);
91
92 /* The event class was frozen when added to its stream class */
93 assert(event_class->frozen);
94
95 /* Validate the trace (if any), the stream class, and the event class */
96 trace = bt_ctf_stream_class_get_trace(stream_class);
97 if (trace) {
98 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
99 trace_valid = trace->valid;
100 assert(trace_valid);
101 environment = trace->environment;
102 }
103
104 packet_context_type = bt_ctf_stream_class_get_packet_context_type(
105 stream_class);
106 event_header_type = bt_ctf_stream_class_get_event_header_type(
107 stream_class);
108 stream_event_ctx_type = bt_ctf_stream_class_get_event_context_type(
109 stream_class);
110 event_context_type = bt_ctf_event_class_get_context_type(event_class);
111 event_payload_type = bt_ctf_event_class_get_payload_type(event_class);
112 ret = bt_ctf_validate_class_types(environment, packet_header_type,
113 packet_context_type, event_header_type, stream_event_ctx_type,
114 event_context_type, event_payload_type, trace_valid,
115 stream_class->valid, event_class->valid,
116 &validation_output, validation_flags);
117 BT_PUT(packet_header_type);
118 BT_PUT(packet_context_type);
119 BT_PUT(event_header_type);
120 BT_PUT(stream_event_ctx_type);
121 BT_PUT(event_context_type);
122 BT_PUT(event_payload_type);
123 if (ret) {
124 /*
125 * This means something went wrong during the validation
126 * process, not that the objects are invalid.
127 */
128 goto error;
129 }
130
131 if ((validation_output.valid_flags & validation_flags) !=
132 validation_flags) {
133 /* Invalid trace/stream class/event class */
134 goto error;
135 }
136
137 /*
138 * At this point we know the trace (if associated to the stream
139 * class), the stream class, and the event class, with their
140 * current types, are valid. We may proceed with creating
141 * the event.
142 */
143 event = g_new0(struct bt_ctf_event, 1);
144 if (!event) {
145 goto error;
146 }
147
148 bt_object_init(event, bt_ctf_event_destroy);
149
150 /*
151 * event does not share a common ancestor with the event class; it has
152 * to guarantee its existence by holding a reference. This reference
153 * shall be released once the event is associated to a stream since,
154 * from that point, the event and its class will share the same
155 * lifetime.
156 */
157 event->event_class = bt_get(event_class);
158 event->clock_values = g_hash_table_new_full(g_direct_hash,
159 g_direct_equal, bt_put, bt_put);
160 event_header =
161 bt_ctf_field_create(validation_output.event_header_type);
162 if (!event_header) {
163 goto error;
164 }
165
166 if (validation_output.stream_event_ctx_type) {
167 stream_event_context = bt_ctf_field_create(
168 validation_output.stream_event_ctx_type);
169 if (!stream_event_context) {
170 goto error;
171 }
172 }
173
174 if (validation_output.event_context_type) {
175 event_context = bt_ctf_field_create(
176 validation_output.event_context_type);
177 if (!event_context) {
178 goto error;
179 }
180 }
181
182 if (validation_output.event_payload_type) {
183 event_payload = bt_ctf_field_create(
184 validation_output.event_payload_type);
185 if (!event_payload) {
186 goto error;
187 }
188 }
189
190 /*
191 * At this point all the fields are created, potentially from
192 * validated copies of field types, so that the field types and
193 * fields can be replaced in the trace, stream class,
194 * event class, and created event.
195 */
196 bt_ctf_validation_replace_types(trace, stream_class,
197 event_class, &validation_output, validation_flags);
198 BT_MOVE(event->event_header, event_header);
199 BT_MOVE(event->stream_event_context, stream_event_context);
200 BT_MOVE(event->context_payload, event_context);
201 BT_MOVE(event->fields_payload, event_payload);
202
203 /*
204 * Put what was not moved in bt_ctf_validation_replace_types().
205 */
206 bt_ctf_validation_output_put_types(&validation_output);
207
208 /*
209 * Freeze the stream class since the event header must not be changed
210 * anymore.
211 */
212 bt_ctf_stream_class_freeze(stream_class);
213
214 /*
215 * Mark stream class, and event class as valid since
216 * they're all frozen now.
217 */
218 stream_class->valid = 1;
219 event_class->valid = 1;
220
221 /* Put stuff we borrowed from the event class */
222 BT_PUT(stream_class);
223 BT_PUT(trace);
224
225 return event;
226
227 error:
228 bt_ctf_validation_output_put_types(&validation_output);
229 BT_PUT(event);
230 BT_PUT(stream_class);
231 BT_PUT(trace);
232 BT_PUT(event_header);
233 BT_PUT(stream_event_context);
234 BT_PUT(event_context);
235 BT_PUT(event_payload);
236 assert(!packet_header_type);
237 assert(!packet_context_type);
238 assert(!event_header_type);
239 assert(!stream_event_ctx_type);
240 assert(!event_context_type);
241 assert(!event_payload_type);
242
243 return event;
244 }
245
246 struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
247 {
248 struct bt_ctf_event_class *event_class = NULL;
249
250 if (!event) {
251 goto end;
252 }
253
254 event_class = event ? bt_get(bt_ctf_event_borrow_event_class(event)) :
255 NULL;
256 end:
257 return event_class;
258 }
259
260 struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
261 {
262 struct bt_ctf_stream *stream = NULL;
263
264 if (!event) {
265 goto end;
266 }
267
268 /*
269 * If the event has a parent, then this is its (writer) stream.
270 * If the event has no parent, then if it has a packet, this
271 * is its (non-writer) stream.
272 */
273 if (event->base.parent) {
274 stream = (struct bt_ctf_stream *) bt_object_get_parent(event);
275 } else {
276 if (event->packet) {
277 stream = bt_get(event->packet->stream);
278 }
279 }
280
281 end:
282 return stream;
283 }
284
285 int bt_ctf_event_set_payload(struct bt_ctf_event *event,
286 const char *name,
287 struct bt_ctf_field *payload)
288 {
289 int ret = 0;
290
291 if (!event || !payload || event->frozen) {
292 ret = -1;
293 goto end;
294 }
295
296 if (name) {
297 ret = bt_ctf_field_structure_set_field(event->fields_payload,
298 name, payload);
299 } else {
300 struct bt_ctf_field_type *payload_type;
301
302 payload_type = bt_ctf_field_get_type(payload);
303
304 if (bt_ctf_field_type_compare(payload_type,
305 event->event_class->fields) == 0) {
306 bt_put(event->fields_payload);
307 bt_get(payload);
308 event->fields_payload = payload;
309 } else {
310 ret = -1;
311 }
312
313 bt_put(payload_type);
314 }
315 end:
316 return ret;
317 }
318
319 struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event)
320 {
321 struct bt_ctf_field *payload = NULL;
322
323 if (!event || !event->fields_payload) {
324 goto end;
325 }
326
327 payload = event->fields_payload;
328 bt_get(payload);
329 end:
330 return payload;
331 }
332
333 int bt_ctf_event_set_payload_field(struct bt_ctf_event *event,
334 struct bt_ctf_field *payload)
335 {
336 int ret = 0;
337 struct bt_ctf_field_type *payload_type = NULL;
338
339 if (!event || event->frozen) {
340 ret = -1;
341 goto end;
342 }
343
344 payload_type = bt_ctf_field_get_type(payload);
345 if (!payload_type) {
346 ret = -1;
347 goto end;
348 }
349
350 if (bt_ctf_field_type_get_type_id(payload_type) !=
351 BT_CTF_FIELD_TYPE_ID_STRUCT) {
352 ret = -1;
353 goto end;
354 }
355
356 bt_put(event->fields_payload);
357 event->fields_payload = bt_get(payload);
358 end:
359 bt_put(payload_type);
360 return ret;
361 }
362
363 struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
364 const char *name)
365 {
366 struct bt_ctf_field *field = NULL;
367
368 if (!event) {
369 goto end;
370 }
371
372 if (name) {
373 field = bt_ctf_field_structure_get_field(event->fields_payload,
374 name);
375 } else {
376 field = event->fields_payload;
377 bt_get(field);
378 }
379 end:
380 return field;
381 }
382
383 struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
384 struct bt_ctf_event *event, int index)
385 {
386 struct bt_ctf_field *field = NULL;
387
388 if (!event || index < 0) {
389 goto end;
390 }
391
392 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
393 index);
394 end:
395 return field;
396 }
397
398 struct bt_ctf_field *bt_ctf_event_get_header(
399 struct bt_ctf_event *event)
400 {
401 struct bt_ctf_field *header = NULL;
402
403 if (!event || !event->event_header) {
404 goto end;
405 }
406
407 header = event->event_header;
408 bt_get(header);
409 end:
410 return header;
411 }
412
413 int bt_ctf_event_set_header(struct bt_ctf_event *event,
414 struct bt_ctf_field *header)
415 {
416 int ret = 0;
417 struct bt_ctf_field_type *field_type = NULL;
418 struct bt_ctf_stream_class *stream_class = NULL;
419
420 if (!event || event->frozen) {
421 ret = -1;
422 goto end;
423 }
424
425 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
426 event->event_class);
427 /*
428 * Ensure the provided header's type matches the one registered to the
429 * stream class.
430 */
431 field_type = bt_ctf_field_get_type(header);
432 if (bt_ctf_field_type_compare(field_type,
433 stream_class->event_header_type)) {
434 ret = -1;
435 goto end;
436 }
437
438 bt_put(event->event_header);
439 event->event_header = bt_get(header);
440 end:
441 bt_put(stream_class);
442 bt_put(field_type);
443 return ret;
444 }
445
446 struct bt_ctf_field *bt_ctf_event_get_event_context(
447 struct bt_ctf_event *event)
448 {
449 struct bt_ctf_field *context = NULL;
450
451 if (!event || !event->context_payload) {
452 goto end;
453 }
454
455 context = event->context_payload;
456 bt_get(context);
457 end:
458 return context;
459 }
460
461 int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
462 struct bt_ctf_field *context)
463 {
464 int ret = 0;
465 struct bt_ctf_field_type *field_type = NULL;
466
467 if (!event || event->frozen) {
468 ret = -1;
469 goto end;
470 }
471
472 field_type = bt_ctf_field_get_type(context);
473 if (bt_ctf_field_type_compare(field_type,
474 event->event_class->context)) {
475 ret = -1;
476 goto end;
477 }
478
479 bt_put(event->context_payload);
480 event->context_payload = bt_get(context);
481 end:
482 bt_put(field_type);
483 return ret;
484 }
485
486 struct bt_ctf_field *bt_ctf_event_get_stream_event_context(
487 struct bt_ctf_event *event)
488 {
489 struct bt_ctf_field *stream_event_context = NULL;
490
491 if (!event || !event->stream_event_context) {
492 goto end;
493 }
494
495 stream_event_context = event->stream_event_context;
496 end:
497 return bt_get(stream_event_context);
498 }
499
500 int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event,
501 struct bt_ctf_field *stream_event_context)
502 {
503 int ret = 0;
504 struct bt_ctf_field_type *field_type = NULL;
505 struct bt_ctf_stream_class *stream_class = NULL;
506
507 if (!event || event->frozen) {
508 ret = -1;
509 goto end;
510 }
511
512 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
513 /*
514 * We should not have been able to create the event without associating
515 * the event class to a stream class.
516 */
517 assert(stream_class);
518
519 field_type = bt_ctf_field_get_type(stream_event_context);
520 if (bt_ctf_field_type_compare(field_type,
521 stream_class->event_context_type)) {
522 ret = -1;
523 goto end;
524 }
525
526 bt_get(stream_event_context);
527 BT_MOVE(event->stream_event_context, stream_event_context);
528 end:
529 BT_PUT(stream_class);
530 bt_put(field_type);
531 return ret;
532 }
533
534 void bt_ctf_event_get(struct bt_ctf_event *event)
535 {
536 bt_get(event);
537 }
538
539 void bt_ctf_event_put(struct bt_ctf_event *event)
540 {
541 bt_put(event);
542 }
543
544 void bt_ctf_event_destroy(struct bt_object *obj)
545 {
546 struct bt_ctf_event *event;
547
548 event = container_of(obj, struct bt_ctf_event, base);
549 if (!event->base.parent) {
550 /*
551 * Event was keeping a reference to its class since it shared no
552 * common ancestor with it to guarantee they would both have the
553 * same lifetime.
554 */
555 bt_put(event->event_class);
556 }
557 g_hash_table_destroy(event->clock_values);
558 bt_put(event->event_header);
559 bt_put(event->stream_event_context);
560 bt_put(event->context_payload);
561 bt_put(event->fields_payload);
562 bt_put(event->packet);
563 g_free(event);
564 }
565
566 struct bt_ctf_clock_value *bt_ctf_event_get_clock_value(
567 struct bt_ctf_event *event, struct bt_ctf_clock_class *clock_class)
568 {
569 struct bt_ctf_clock_value *clock_value = NULL;
570
571 if (!event || !clock_class) {
572 goto end;
573 }
574
575 clock_value = g_hash_table_lookup(event->clock_values, clock_class);
576 if (!clock_value) {
577 goto end;
578 }
579
580 bt_get(clock_value);
581 end:
582 return clock_value;
583 }
584
585 int bt_ctf_event_set_clock_value(struct bt_ctf_event *event,
586 struct bt_ctf_clock_value *value)
587 {
588 int ret = 0;
589
590 if (!event || !value || event->frozen) {
591 ret = -1;
592 goto end;
593 }
594
595 g_hash_table_insert(event->clock_values,
596 bt_ctf_clock_value_get_class(value), bt_get(value));
597 end:
598 return ret;
599 }
600
601 BT_HIDDEN
602 int bt_ctf_event_validate(struct bt_ctf_event *event)
603 {
604 /* Make sure each field's payload has been set */
605 int ret;
606 struct bt_ctf_stream_class *stream_class = NULL;
607
608 assert(event);
609 ret = bt_ctf_field_validate(event->event_header);
610 if (ret) {
611 goto end;
612 }
613
614 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
615 /*
616 * We should not have been able to create the event without associating
617 * the event class to a stream class.
618 */
619 assert(stream_class);
620 if (stream_class->event_context_type) {
621 ret = bt_ctf_field_validate(event->stream_event_context);
622 if (ret) {
623 goto end;
624 }
625 }
626
627 ret = bt_ctf_field_validate(event->fields_payload);
628 if (ret) {
629 goto end;
630 }
631
632 if (event->event_class->context) {
633 ret = bt_ctf_field_validate(event->context_payload);
634 }
635 end:
636 bt_put(stream_class);
637 return ret;
638 }
639
640 BT_HIDDEN
641 int bt_ctf_event_serialize(struct bt_ctf_event *event,
642 struct bt_ctf_stream_pos *pos,
643 enum bt_ctf_byte_order native_byte_order)
644 {
645 int ret = 0;
646
647 assert(event);
648 assert(pos);
649
650 if (event->context_payload) {
651 ret = bt_ctf_field_serialize(event->context_payload, pos,
652 native_byte_order);
653 if (ret) {
654 goto end;
655 }
656 }
657
658 if (event->fields_payload) {
659 ret = bt_ctf_field_serialize(event->fields_payload, pos,
660 native_byte_order);
661 if (ret) {
662 goto end;
663 }
664 }
665 end:
666 return ret;
667 }
668
669 struct bt_ctf_packet *bt_ctf_event_get_packet(struct bt_ctf_event *event)
670 {
671 struct bt_ctf_packet *packet = NULL;
672
673 if (!event || !event->packet) {
674 goto end;
675 }
676
677 packet = bt_get(event->packet);
678 end:
679 return packet;
680 }
681
682 int bt_ctf_event_set_packet(struct bt_ctf_event *event,
683 struct bt_ctf_packet *packet)
684 {
685 struct bt_ctf_stream_class *event_stream_class = NULL;
686 struct bt_ctf_stream_class *packet_stream_class = NULL;
687 struct bt_ctf_stream *stream = NULL;
688 int ret = 0;
689
690 if (!event || !packet || event->frozen) {
691 ret = -1;
692 goto end;
693 }
694
695 /*
696 * Make sure the new packet was created by this event's
697 * stream, if it is set.
698 */
699 stream = bt_ctf_event_get_stream(event);
700 if (stream) {
701 if (packet->stream != stream) {
702 ret = -1;
703 goto end;
704 }
705 } else {
706 event_stream_class =
707 bt_ctf_event_class_get_stream_class(event->event_class);
708 packet_stream_class =
709 bt_ctf_stream_get_class(packet->stream);
710
711 assert(event_stream_class);
712 assert(packet_stream_class);
713
714 if (event_stream_class != packet_stream_class) {
715 ret = -1;
716 goto end;
717 }
718 }
719
720 bt_get(packet);
721 BT_MOVE(event->packet, packet);
722
723 end:
724 BT_PUT(stream);
725 BT_PUT(event_stream_class);
726 BT_PUT(packet_stream_class);
727
728 return ret;
729 }
730
731 BT_HIDDEN
732 void bt_ctf_event_freeze(struct bt_ctf_event *event)
733 {
734 assert(event);
735 bt_ctf_packet_freeze(event->packet);
736 bt_ctf_field_freeze(event->event_header);
737 bt_ctf_field_freeze(event->stream_event_context);
738 bt_ctf_field_freeze(event->context_payload);
739 bt_ctf_field_freeze(event->fields_payload);
740 event->frozen = 1;
741 }
This page took 0.042896 seconds and 3 git commands to generate.