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