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