ir: refactor FT validation and resolving
[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
29#include <babeltrace/ctf-writer/event.h>
30#include <babeltrace/ctf-writer/event-types.h>
31#include <babeltrace/ctf-writer/event-fields.h>
adc315b8
JG
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>
2f100782 35#include <babeltrace/ctf-ir/stream-class.h>
c35a1669 36#include <babeltrace/ctf-ir/stream-class-internal.h>
bc37ae52 37#include <babeltrace/ctf-ir/trace-internal.h>
09840de5 38#include <babeltrace/ctf-ir/validation-internal.h>
654c1444 39#include <babeltrace/ctf-ir/utils.h>
83509119 40#include <babeltrace/ref.h>
44e0a4f5 41#include <babeltrace/ctf-ir/attributes-internal.h>
273b65be
JG
42#include <babeltrace/compiler.h>
43
44static
83509119 45void bt_ctf_event_class_destroy(struct bt_object *obj);
273b65be 46static
83509119 47void bt_ctf_event_destroy(struct bt_object *obj);
662e778c
JG
48static
49int set_integer_field_value(struct bt_ctf_field *field, uint64_t value);
273b65be
JG
50
51struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
52{
b8248cc0 53 int ret;
dac5c838 54 struct bt_value *obj = NULL;
273b65be
JG
55 struct bt_ctf_event_class *event_class = NULL;
56
654c1444 57 if (bt_ctf_validate_identifier(name)) {
b8248cc0 58 goto error;
273b65be
JG
59 }
60
61 event_class = g_new0(struct bt_ctf_event_class, 1);
62 if (!event_class) {
b8248cc0 63 goto error;
273b65be
JG
64 }
65
83509119 66 bt_object_init(event_class, bt_ctf_event_class_destroy);
c5a9aa19
JG
67 event_class->fields = bt_ctf_field_type_structure_create();
68 if (!event_class->fields) {
b8248cc0 69 goto error;
c5a9aa19
JG
70 }
71
b8248cc0
PP
72 event_class->attributes = bt_ctf_attributes_create();
73 if (!event_class->attributes) {
74 goto error;
75 }
76
dac5c838 77 obj = bt_value_integer_create_init(-1);
b8248cc0
PP
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
83509119 88 BT_PUT(obj);
b8248cc0 89
dac5c838 90 obj = bt_value_string_create_init(name);
b8248cc0
PP
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
83509119 101 BT_PUT(obj);
b8248cc0 102
273b65be 103 return event_class;
b8248cc0
PP
104
105error:
83509119
JG
106 BT_PUT(event_class);
107 BT_PUT(obj);
108 return event_class;
273b65be
JG
109}
110
2f100782
JG
111const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class)
112{
dac5c838 113 struct bt_value *obj = NULL;
2f100782
JG
114 const char *name = NULL;
115
116 if (!event_class) {
117 goto end;
118 }
119
b8248cc0
PP
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
dac5c838 126 if (bt_value_string_get(obj, &name)) {
b8248cc0
PP
127 name = NULL;
128 }
129
2f100782 130end:
83509119 131 BT_PUT(obj);
2f100782
JG
132 return name;
133}
134
135int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
136{
dac5c838 137 struct bt_value *obj = NULL;
9ea90c02 138 int64_t ret = 0;
2f100782 139
b8248cc0
PP
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
dac5c838 151 if (bt_value_integer_get(obj, &ret)) {
b8248cc0
PP
152 ret = -1;
153 }
154
155 if (ret < 0) {
156 /* means ID is not set */
2f100782
JG
157 ret = -1;
158 goto end;
159 }
160
2f100782 161end:
83509119 162 BT_PUT(obj);
2f100782
JG
163 return ret;
164}
165
166int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
167 uint32_t id)
168{
169 int ret = 0;
dac5c838 170 struct bt_value *obj = NULL;
e6a8e8e4
JG
171 struct bt_ctf_stream_class *stream_class = NULL;
172
2f100782
JG
173
174 if (!event_class) {
175 ret = -1;
176 goto end;
177 }
178
e6a8e8e4
JG
179 stream_class = bt_ctf_event_class_get_stream_class(event_class);
180 if (stream_class) {
2f100782
JG
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
b8248cc0
PP
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
dac5c838 195 if (bt_value_integer_set(obj, id)) {
b8248cc0
PP
196 ret = -1;
197 goto end;
198 }
199
200end:
83509119 201 BT_PUT(obj);
e6a8e8e4 202 BT_PUT(stream_class);
b8248cc0
PP
203 return ret;
204}
205
206int bt_ctf_event_class_set_attribute(
207 struct bt_ctf_event_class *event_class, const char *name,
dac5c838 208 struct bt_value *value)
b8248cc0
PP
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")) {
dac5c838 218 if (!bt_value_is_integer(value)) {
b8248cc0
PP
219 ret = -1;
220 goto end;
221 }
222 } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) {
dac5c838 223 if (!bt_value_is_string(value)) {
b8248cc0
PP
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
dac5c838 237 ret = bt_value_integer_get(value, &val);
b8248cc0
PP
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
2f100782
JG
252end:
253 return ret;
254}
255
b8248cc0
PP
256int 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
268end:
269 return ret;
270}
271
272const char *
273bt_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
285end:
286 return ret;
287}
288
dac5c838 289struct bt_value *
b8248cc0
PP
290bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class,
291 int index)
292{
dac5c838 293 struct bt_value *ret;
b8248cc0
PP
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
302end:
303 return ret;
304}
305
dac5c838 306struct bt_value *
b8248cc0
PP
307bt_ctf_event_class_get_attribute_value_by_name(
308 struct bt_ctf_event_class *event_class, const char *name)
309{
dac5c838 310 struct bt_value *ret;
b8248cc0
PP
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
320end:
321 return ret;
322
323}
324
2f100782
JG
325struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
326 struct bt_ctf_event_class *event_class)
327{
e6a8e8e4 328 return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class);
2f100782
JG
329}
330
c5a9aa19
JG
331struct 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
83509119 340 bt_get(event_class->fields);
c5a9aa19
JG
341 payload = event_class->fields;
342end:
343 return payload;
344}
345
346int 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
d2127f80
JG
351 if (!event_class || !payload ||
352 bt_ctf_field_type_get_type_id(payload) != CTF_TYPE_STRUCT) {
c5a9aa19
JG
353 ret = -1;
354 goto end;
355 }
356
83509119
JG
357 bt_get(payload);
358 bt_put(event_class->fields);
c5a9aa19
JG
359 event_class->fields = payload;
360end:
361 return ret;
362}
363
273b65be
JG
364int 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
654c1444 370 if (!event_class || !type || bt_ctf_validate_identifier(name) ||
273b65be
JG
371 event_class->frozen) {
372 ret = -1;
373 goto end;
374 }
375
c5a9aa19
JG
376 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
377 CTF_TYPE_STRUCT) {
378 ret = -1;
379 goto end;
273b65be
JG
380 }
381
382 ret = bt_ctf_field_type_structure_add_field(event_class->fields,
383 type, name);
384end:
385 return ret;
386}
387
074ee56d 388int bt_ctf_event_class_get_field_count(
2f100782
JG
389 struct bt_ctf_event_class *event_class)
390{
074ee56d 391 int ret;
2f100782
JG
392
393 if (!event_class) {
394 ret = -1;
395 goto end;
396 }
397
c5a9aa19
JG
398 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
399 CTF_TYPE_STRUCT) {
400 ret = -1;
401 goto end;
402 }
403
2f100782
JG
404 ret = bt_ctf_field_type_structure_get_field_count(event_class->fields);
405end:
406 return ret;
407}
408
409int 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,
074ee56d 411 int index)
2f100782
JG
412{
413 int ret;
414
074ee56d 415 if (!event_class || index < 0) {
2f100782
JG
416 ret = -1;
417 goto end;
418 }
419
c5a9aa19
JG
420 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
421 CTF_TYPE_STRUCT) {
422 ret = -1;
423 goto end;
424 }
425
2f100782
JG
426 ret = bt_ctf_field_type_structure_get_field(event_class->fields,
427 field_name, field_type, index);
428end:
429 return ret;
430}
431
432struct 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
c5a9aa19
JG
442 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
443 CTF_TYPE_STRUCT) {
444 goto end;
445 }
446
2f100782
JG
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);
458end:
459 return field_type;
460}
461
f655a84d
JG
462struct 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
83509119 471 bt_get(event_class->context);
f655a84d
JG
472 context_type = event_class->context;
473end:
474 return context_type;
475}
476
477int 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
83509119
JG
493 bt_get(context);
494 bt_put(event_class->context);
f655a84d
JG
495 event_class->context = context;
496end:
497 return ret;
498
499}
500
273b65be
JG
501void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
502{
83509119 503 bt_get(event_class);
273b65be
JG
504}
505
506void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
507{
83509119 508 bt_put(event_class);
273b65be
JG
509}
510
29664b2a
PP
511BT_HIDDEN
512int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
513 uint32_t stream_id)
514{
515 int ret = 0;
dac5c838 516 struct bt_value *obj;
29664b2a 517
dac5c838 518 obj = bt_value_integer_create_init(stream_id);
29664b2a
PP
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
528end:
83509119 529 BT_PUT(obj);
29664b2a
PP
530 return ret;
531}
532
273b65be
JG
533struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
534{
09840de5
PP
535 int ret;
536 enum bt_ctf_validation_flag validation_flags =
537 BT_CTF_VALIDATION_FLAG_STREAM |
538 BT_CTF_VALIDATION_FLAG_EVENT;
273b65be 539 struct bt_ctf_event *event = NULL;
09840de5 540 struct bt_ctf_trace *trace = NULL;
e6a8e8e4 541 struct bt_ctf_stream_class *stream_class = NULL;
09840de5
PP
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;
273b65be
JG
554
555 if (!event_class) {
e6a8e8e4 556 goto error;
273b65be
JG
557 }
558
e6a8e8e4 559 stream_class = bt_ctf_event_class_get_stream_class(event_class);
09840de5 560
662e778c 561 /*
e6a8e8e4
JG
562 * We disallow the creation of an event if its event class has not been
563 * associated to a stream class.
662e778c 564 */
e6a8e8e4
JG
565 if (!stream_class) {
566 goto error;
662e778c 567 }
09840de5
PP
568
569 /* A stream class should always have an existing event header type */
e6a8e8e4 570 assert(stream_class->event_header_type);
09840de5
PP
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 */
b8248cc0
PP
624 event = g_new0(struct bt_ctf_event, 1);
625 if (!event) {
e6a8e8e4 626 goto error;
b8248cc0
PP
627 }
628
83509119 629 bt_object_init(event, bt_ctf_event_destroy);
09840de5 630
e6a8e8e4
JG
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);
09840de5
PP
639 event_header =
640 bt_ctf_field_create(validation_output.event_header_type);
b8248cc0 641
09840de5 642 if (!event_header) {
83509119 643 goto error;
662e778c 644 }
09840de5
PP
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) {
83509119 650 goto error;
662e778c 651 }
f655a84d 652 }
09840de5
PP
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 }
662e778c
JG
660 }
661
09840de5
PP
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
662e778c
JG
679 /*
680 * Freeze the stream class since the event header must not be changed
681 * anymore.
682 */
e6a8e8e4 683 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
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 */
e6a8e8e4 693 BT_PUT(stream_class);
09840de5
PP
694 BT_PUT(trace);
695
273b65be 696 return event;
09840de5 697
83509119 698error:
09840de5 699 bt_ctf_validation_output_put_types(&validation_output);
83509119 700 BT_PUT(event);
e6a8e8e4 701 BT_PUT(stream_class);
09840de5
PP
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
83509119 713 return event;
273b65be
JG
714}
715
2f100782
JG
716struct 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;
83509119 725 bt_get(event_class);
2f100782
JG
726end:
727 return event_class;
728}
729
8e5003bb
JG
730struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
731{
e6a8e8e4 732 return (struct bt_ctf_stream *) bt_object_get_parent(event);
8e5003bb
JG
733}
734
2f100782
JG
735struct 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
760error_put_stream_class:
83509119 761 bt_put(stream_class);
2f100782 762error_put_event_class:
83509119 763 bt_put(event_class);
2f100782
JG
764end:
765 return clock;
766}
767
273b65be
JG
768int bt_ctf_event_set_payload(struct bt_ctf_event *event,
769 const char *name,
c5a9aa19 770 struct bt_ctf_field *payload)
273b65be
JG
771{
772 int ret = 0;
773
c5a9aa19 774 if (!event || !payload) {
273b65be
JG
775 ret = -1;
776 goto end;
777 }
778
c5a9aa19
JG
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);
09840de5
PP
786
787 if (bt_ctf_field_type_compare(payload_type,
788 event->event_class->fields) == 0) {
83509119
JG
789 bt_put(event->fields_payload);
790 bt_get(payload);
c5a9aa19
JG
791 event->fields_payload = payload;
792 } else {
793 ret = -1;
794 }
795
83509119 796 bt_put(payload_type);
c5a9aa19 797 }
273b65be
JG
798end:
799 return ret;
800}
801
71362d53
PP
802struct 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;
83509119 811 bt_get(payload);
71362d53
PP
812end:
813 return payload;
814}
273b65be 815
e5e6eb3a
JG
816int 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
83509119
JG
838 bt_get(payload);
839 bt_put(event->fields_payload);
e5e6eb3a
JG
840 event->fields_payload = payload;
841
842end:
83509119 843 bt_put(payload_type);
e5e6eb3a
JG
844 return ret;
845}
846
273b65be
JG
847struct 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
c5a9aa19 852 if (!event) {
273b65be
JG
853 goto end;
854 }
855
c5a9aa19
JG
856 if (name) {
857 field = bt_ctf_field_structure_get_field(event->fields_payload,
858 name);
859 } else {
860 field = event->fields_payload;
83509119 861 bt_get(field);
c5a9aa19 862 }
273b65be
JG
863end:
864 return field;
865}
866
2f100782 867struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
074ee56d 868 struct bt_ctf_event *event, int index)
2f100782
JG
869{
870 struct bt_ctf_field *field = NULL;
871
074ee56d 872 if (!event || index < 0) {
2f100782
JG
873 goto end;
874 }
875
876 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
877 index);
878end:
879 return field;
880}
881
286a2840 882struct bt_ctf_field *bt_ctf_event_get_header(
662e778c
JG
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;
83509119 892 bt_get(header);
662e778c
JG
893end:
894 return header;
895}
896
286a2840 897int bt_ctf_event_set_header(struct bt_ctf_event *event,
662e778c
JG
898 struct bt_ctf_field *header)
899{
900 int ret = 0;
901 struct bt_ctf_field_type *field_type = NULL;
e6a8e8e4 902 struct bt_ctf_stream_class *stream_class = NULL;
662e778c
JG
903
904 if (!event || !header) {
905 ret = -1;
906 goto end;
907 }
908
e6a8e8e4
JG
909 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
910 event->event_class);
662e778c
JG
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);
09840de5
PP
916 if (bt_ctf_field_type_compare(field_type,
917 stream_class->event_header_type)) {
662e778c
JG
918 ret = -1;
919 goto end;
920 }
921
83509119
JG
922 bt_get(header);
923 bt_put(event->event_header);
662e778c
JG
924 event->event_header = header;
925end:
e6a8e8e4 926 bt_put(stream_class);
83509119 927 bt_put(field_type);
662e778c
JG
928 return ret;
929}
930
f655a84d
JG
931struct 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;
83509119 941 bt_get(context);
f655a84d
JG
942end:
943 return context;
944}
945
946int 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);
09840de5
PP
958 if (bt_ctf_field_type_compare(field_type,
959 event->event_class->context)) {
f655a84d
JG
960 ret = -1;
961 goto end;
962 }
963
83509119
JG
964 bt_get(context);
965 bt_put(event->context_payload);
f655a84d
JG
966 event->context_payload = context;
967end:
83509119 968 bt_put(field_type);
f655a84d
JG
969 return ret;
970}
971
273b65be
JG
972void bt_ctf_event_get(struct bt_ctf_event *event)
973{
83509119 974 bt_get(event);
273b65be
JG
975}
976
977void bt_ctf_event_put(struct bt_ctf_event *event)
978{
83509119 979 bt_put(event);
273b65be
JG
980}
981
982static
83509119 983void bt_ctf_event_class_destroy(struct bt_object *obj)
273b65be
JG
984{
985 struct bt_ctf_event_class *event_class;
273b65be 986
83509119
JG
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);
273b65be
JG
991 g_free(event_class);
992}
993
994static
83509119 995void bt_ctf_event_destroy(struct bt_object *obj)
273b65be
JG
996{
997 struct bt_ctf_event *event;
998
83509119 999 event = container_of(obj, struct bt_ctf_event, base);
e6a8e8e4
JG
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 }
83509119
JG
1008 bt_put(event->event_header);
1009 bt_put(event->context_payload);
1010 bt_put(event->fields_payload);
273b65be
JG
1011 g_free(event);
1012}
1013
662e778c
JG
1014static
1015int 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 }
1052end:
83509119 1053 bt_put(field_type);
662e778c
JG
1054 return ret;
1055}
1056
273b65be
JG
1057BT_HIDDEN
1058void 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);
b8248cc0 1064 bt_ctf_attributes_freeze(event_class->attributes);
273b65be
JG
1065}
1066
273b65be
JG
1067BT_HIDDEN
1068int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
1069 struct metadata_context *context)
1070{
b8248cc0
PP
1071 int i;
1072 int count;
273b65be 1073 int ret = 0;
dac5c838 1074 struct bt_value *attr_value = NULL;
273b65be
JG
1075
1076 assert(event_class);
1077 assert(context);
b8248cc0
PP
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) {
2f100782
JG
1085 ret = -1;
1086 goto end;
1087 }
1088
b8248cc0
PP
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
dac5c838
PP
1102 switch (bt_value_get_type(attr_value)) {
1103 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
1104 {
1105 int64_t value;
1106
dac5c838 1107 ret = bt_value_integer_get(attr_value, &value);
b8248cc0
PP
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
dac5c838 1118 case BT_VALUE_TYPE_STRING:
b8248cc0
PP
1119 {
1120 const char *value;
1121
dac5c838 1122 ret = bt_value_string_get(attr_value, &value);
b8248cc0
PP
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
83509119 1139 BT_PUT(attr_value);
b8248cc0 1140 }
273b65be
JG
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");
1162end:
1163 context->current_indentation_level = 0;
83509119 1164 BT_PUT(attr_value);
273b65be
JG
1165 return ret;
1166}
1167
c35a1669
JG
1168void 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
445c3471
PP
1176 assert(byte_order == 0 || byte_order == LITTLE_ENDIAN ||
1177 byte_order == BIG_ENDIAN);
1178
c35a1669
JG
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
273b65be
JG
1185BT_HIDDEN
1186int 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);
662e778c
JG
1192 ret = bt_ctf_field_validate(event->event_header);
1193 if (ret) {
1194 goto end;
1195 }
1196
273b65be
JG
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 }
1205end:
1206 return ret;
1207}
1208
1209BT_HIDDEN
1210int 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 }
1230end:
1231 return ret;
1232}
1233
662e778c
JG
1234BT_HIDDEN
1235int 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,
b8248cc0
PP
1248 (uint64_t) bt_ctf_event_class_get_id(
1249 event->event_class));
662e778c
JG
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) {
9a220c32
JG
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);
83509119 1265 bt_put(timestamp_field_type);
9a220c32 1266 if (mapped_clock) {
61cf588b 1267 int64_t timestamp;
9a220c32 1268
61cf588b 1269 ret = bt_ctf_clock_get_time(mapped_clock, &timestamp);
83509119 1270 bt_put(mapped_clock);
61cf588b 1271 if (ret) {
9a220c32
JG
1272 goto end;
1273 }
1274
1275 ret = set_integer_field_value(timestamp_field,
1276 timestamp);
1277 if (ret) {
1278 goto end;
1279 }
662e778c
JG
1280 }
1281 }
1282end:
83509119
JG
1283 bt_put(id_field);
1284 bt_put(timestamp_field);
662e778c
JG
1285 return ret;
1286}
123fbdec 1287
9c4c8f6e
PP
1288struct 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
83509119 1301 bt_object_init(copy, bt_ctf_event_destroy);
e6a8e8e4 1302 copy->event_class = bt_get(event->event_class);
9c4c8f6e
PP
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
1331error:
83509119
JG
1332 BT_PUT(copy);
1333 return copy;
9c4c8f6e 1334}
This page took 0.093746 seconds and 4 git commands to generate.