ir: allow to set variant FT's tag FT even when frozen
[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>
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
43static
83509119 44void bt_ctf_event_class_destroy(struct bt_object *obj);
273b65be 45static
83509119 46void bt_ctf_event_destroy(struct bt_object *obj);
662e778c
JG
47static
48int set_integer_field_value(struct bt_ctf_field *field, uint64_t value);
273b65be
JG
49
50struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
51{
b8248cc0 52 int ret;
dac5c838 53 struct bt_value *obj = NULL;
273b65be
JG
54 struct bt_ctf_event_class *event_class = NULL;
55
654c1444 56 if (bt_ctf_validate_identifier(name)) {
b8248cc0 57 goto error;
273b65be
JG
58 }
59
60 event_class = g_new0(struct bt_ctf_event_class, 1);
61 if (!event_class) {
b8248cc0 62 goto error;
273b65be
JG
63 }
64
83509119 65 bt_object_init(event_class, bt_ctf_event_class_destroy);
c5a9aa19
JG
66 event_class->fields = bt_ctf_field_type_structure_create();
67 if (!event_class->fields) {
b8248cc0 68 goto error;
c5a9aa19
JG
69 }
70
b8248cc0
PP
71 event_class->attributes = bt_ctf_attributes_create();
72 if (!event_class->attributes) {
73 goto error;
74 }
75
dac5c838 76 obj = bt_value_integer_create_init(-1);
b8248cc0
PP
77 if (!obj) {
78 goto error;
79 }
80
81 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
82 "id", obj);
83 if (ret) {
84 goto error;
85 }
86
83509119 87 BT_PUT(obj);
b8248cc0 88
dac5c838 89 obj = bt_value_string_create_init(name);
b8248cc0
PP
90 if (!obj) {
91 goto error;
92 }
93
94 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
95 "name", obj);
96 if (ret) {
97 goto error;
98 }
99
83509119 100 BT_PUT(obj);
b8248cc0 101
273b65be 102 return event_class;
b8248cc0
PP
103
104error:
83509119
JG
105 BT_PUT(event_class);
106 BT_PUT(obj);
107 return event_class;
273b65be
JG
108}
109
2f100782
JG
110const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class)
111{
dac5c838 112 struct bt_value *obj = NULL;
2f100782
JG
113 const char *name = NULL;
114
115 if (!event_class) {
116 goto end;
117 }
118
b8248cc0
PP
119 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
120 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX);
121 if (!obj) {
122 goto end;
123 }
124
dac5c838 125 if (bt_value_string_get(obj, &name)) {
b8248cc0
PP
126 name = NULL;
127 }
128
2f100782 129end:
83509119 130 BT_PUT(obj);
2f100782
JG
131 return name;
132}
133
134int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
135{
dac5c838 136 struct bt_value *obj = NULL;
9ea90c02 137 int64_t ret = 0;
2f100782 138
b8248cc0
PP
139 if (!event_class) {
140 ret = -1;
141 goto end;
142 }
143
144 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
145 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
146 if (!obj) {
147 goto end;
148 }
149
dac5c838 150 if (bt_value_integer_get(obj, &ret)) {
b8248cc0
PP
151 ret = -1;
152 }
153
154 if (ret < 0) {
155 /* means ID is not set */
2f100782
JG
156 ret = -1;
157 goto end;
158 }
159
2f100782 160end:
83509119 161 BT_PUT(obj);
2f100782
JG
162 return ret;
163}
164
165int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
166 uint32_t id)
167{
168 int ret = 0;
dac5c838 169 struct bt_value *obj = NULL;
e6a8e8e4
JG
170 struct bt_ctf_stream_class *stream_class = NULL;
171
2f100782
JG
172
173 if (!event_class) {
174 ret = -1;
175 goto end;
176 }
177
e6a8e8e4
JG
178 stream_class = bt_ctf_event_class_get_stream_class(event_class);
179 if (stream_class) {
2f100782
JG
180 /*
181 * We don't allow changing the id if the event class has already
182 * been added to a stream class.
183 */
184 ret = -1;
185 goto end;
186 }
187
b8248cc0
PP
188 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
189 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
190 if (!obj) {
191 goto end;
192 }
193
dac5c838 194 if (bt_value_integer_set(obj, id)) {
b8248cc0
PP
195 ret = -1;
196 goto end;
197 }
198
199end:
83509119 200 BT_PUT(obj);
e6a8e8e4 201 BT_PUT(stream_class);
b8248cc0
PP
202 return ret;
203}
204
205int bt_ctf_event_class_set_attribute(
206 struct bt_ctf_event_class *event_class, const char *name,
dac5c838 207 struct bt_value *value)
b8248cc0
PP
208{
209 int ret = 0;
210
211 if (!event_class || !name || !value || event_class->frozen) {
212 ret = -1;
213 goto end;
214 }
215
216 if (!strcmp(name, "id") || !strcmp(name, "loglevel")) {
dac5c838 217 if (!bt_value_is_integer(value)) {
b8248cc0
PP
218 ret = -1;
219 goto end;
220 }
221 } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) {
dac5c838 222 if (!bt_value_is_string(value)) {
b8248cc0
PP
223 ret = -1;
224 goto end;
225 }
226 } else {
227 /* unknown attribute */
228 ret = -1;
229 goto end;
230 }
231
232 /* "id" special case: >= 0 */
233 if (!strcmp(name, "id")) {
234 int64_t val;
235
dac5c838 236 ret = bt_value_integer_get(value, &val);
b8248cc0
PP
237
238 if (ret) {
239 goto end;
240 }
241
242 if (val < 0) {
243 ret = -1;
244 goto end;
245 }
246 }
247
248 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
249 name, value);
250
2f100782
JG
251end:
252 return ret;
253}
254
b8248cc0
PP
255int bt_ctf_event_class_get_attribute_count(
256 struct bt_ctf_event_class *event_class)
257{
258 int ret = 0;
259
260 if (!event_class) {
261 ret = -1;
262 goto end;
263 }
264
265 ret = bt_ctf_attributes_get_count(event_class->attributes);
266
267end:
268 return ret;
269}
270
271const char *
272bt_ctf_event_class_get_attribute_name(
273 struct bt_ctf_event_class *event_class, int index)
274{
275 const char *ret;
276
277 if (!event_class) {
278 ret = NULL;
279 goto end;
280 }
281
282 ret = bt_ctf_attributes_get_field_name(event_class->attributes, index);
283
284end:
285 return ret;
286}
287
dac5c838 288struct bt_value *
b8248cc0
PP
289bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class,
290 int index)
291{
dac5c838 292 struct bt_value *ret;
b8248cc0
PP
293
294 if (!event_class) {
295 ret = NULL;
296 goto end;
297 }
298
299 ret = bt_ctf_attributes_get_field_value(event_class->attributes, index);
300
301end:
302 return ret;
303}
304
dac5c838 305struct bt_value *
b8248cc0
PP
306bt_ctf_event_class_get_attribute_value_by_name(
307 struct bt_ctf_event_class *event_class, const char *name)
308{
dac5c838 309 struct bt_value *ret;
b8248cc0
PP
310
311 if (!event_class || !name) {
312 ret = NULL;
313 goto end;
314 }
315
316 ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes,
317 name);
318
319end:
320 return ret;
321
322}
323
2f100782
JG
324struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
325 struct bt_ctf_event_class *event_class)
326{
e6a8e8e4 327 return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class);
2f100782
JG
328}
329
c5a9aa19
JG
330struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type(
331 struct bt_ctf_event_class *event_class)
332{
333 struct bt_ctf_field_type *payload = NULL;
334
335 if (!event_class) {
336 goto end;
337 }
338
83509119 339 bt_get(event_class->fields);
c5a9aa19
JG
340 payload = event_class->fields;
341end:
342 return payload;
343}
344
345int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class,
346 struct bt_ctf_field_type *payload)
347{
348 int ret = 0;
349
d2127f80
JG
350 if (!event_class || !payload ||
351 bt_ctf_field_type_get_type_id(payload) != CTF_TYPE_STRUCT) {
c5a9aa19
JG
352 ret = -1;
353 goto end;
354 }
355
83509119
JG
356 bt_get(payload);
357 bt_put(event_class->fields);
c5a9aa19
JG
358 event_class->fields = payload;
359end:
360 return ret;
361}
362
273b65be
JG
363int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
364 struct bt_ctf_field_type *type,
365 const char *name)
366{
367 int ret = 0;
368
654c1444 369 if (!event_class || !type || bt_ctf_validate_identifier(name) ||
273b65be
JG
370 event_class->frozen) {
371 ret = -1;
372 goto end;
373 }
374
c5a9aa19
JG
375 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
376 CTF_TYPE_STRUCT) {
377 ret = -1;
378 goto end;
273b65be
JG
379 }
380
381 ret = bt_ctf_field_type_structure_add_field(event_class->fields,
382 type, name);
383end:
384 return ret;
385}
386
074ee56d 387int bt_ctf_event_class_get_field_count(
2f100782
JG
388 struct bt_ctf_event_class *event_class)
389{
074ee56d 390 int ret;
2f100782
JG
391
392 if (!event_class) {
393 ret = -1;
394 goto end;
395 }
396
c5a9aa19
JG
397 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
398 CTF_TYPE_STRUCT) {
399 ret = -1;
400 goto end;
401 }
402
2f100782
JG
403 ret = bt_ctf_field_type_structure_get_field_count(event_class->fields);
404end:
405 return ret;
406}
407
408int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class,
409 const char **field_name, struct bt_ctf_field_type **field_type,
074ee56d 410 int index)
2f100782
JG
411{
412 int ret;
413
074ee56d 414 if (!event_class || index < 0) {
2f100782
JG
415 ret = -1;
416 goto end;
417 }
418
c5a9aa19
JG
419 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
420 CTF_TYPE_STRUCT) {
421 ret = -1;
422 goto end;
423 }
424
2f100782
JG
425 ret = bt_ctf_field_type_structure_get_field(event_class->fields,
426 field_name, field_type, index);
427end:
428 return ret;
429}
430
431struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(
432 struct bt_ctf_event_class *event_class, const char *name)
433{
434 GQuark name_quark;
435 struct bt_ctf_field_type *field_type = NULL;
436
437 if (!event_class || !name) {
438 goto end;
439 }
440
c5a9aa19
JG
441 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
442 CTF_TYPE_STRUCT) {
443 goto end;
444 }
445
2f100782
JG
446 name_quark = g_quark_try_string(name);
447 if (!name_quark) {
448 goto end;
449 }
450
451 /*
452 * No need to increment field_type's reference count since getting it
453 * from the structure already does.
454 */
455 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
456 event_class->fields, name);
457end:
458 return field_type;
459}
460
f655a84d
JG
461struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
462 struct bt_ctf_event_class *event_class)
463{
464 struct bt_ctf_field_type *context_type = NULL;
465
466 if (!event_class || !event_class->context) {
467 goto end;
468 }
469
83509119 470 bt_get(event_class->context);
f655a84d
JG
471 context_type = event_class->context;
472end:
473 return context_type;
474}
475
476int bt_ctf_event_class_set_context_type(
477 struct bt_ctf_event_class *event_class,
478 struct bt_ctf_field_type *context)
479{
480 int ret = 0;
481
482 if (!event_class || !context || event_class->frozen) {
483 ret = -1;
484 goto end;
485 }
486
487 if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) {
488 ret = -1;
489 goto end;
490 }
491
83509119
JG
492 bt_get(context);
493 bt_put(event_class->context);
f655a84d
JG
494 event_class->context = context;
495end:
496 return ret;
497
498}
499
273b65be
JG
500void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
501{
83509119 502 bt_get(event_class);
273b65be
JG
503}
504
505void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
506{
83509119 507 bt_put(event_class);
273b65be
JG
508}
509
29664b2a
PP
510BT_HIDDEN
511int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
512 uint32_t stream_id)
513{
514 int ret = 0;
dac5c838 515 struct bt_value *obj;
29664b2a 516
dac5c838 517 obj = bt_value_integer_create_init(stream_id);
29664b2a
PP
518
519 if (!obj) {
520 ret = -1;
521 goto end;
522 }
523
524 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
525 "stream_id", obj);
526
527end:
83509119 528 BT_PUT(obj);
29664b2a
PP
529 return ret;
530}
531
273b65be
JG
532struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
533{
534 struct bt_ctf_event *event = NULL;
e6a8e8e4 535 struct bt_ctf_stream_class *stream_class = NULL;
273b65be
JG
536
537 if (!event_class) {
e6a8e8e4 538 goto error;
273b65be
JG
539 }
540
e6a8e8e4 541 stream_class = bt_ctf_event_class_get_stream_class(event_class);
662e778c 542 /*
e6a8e8e4
JG
543 * We disallow the creation of an event if its event class has not been
544 * associated to a stream class.
662e778c 545 */
e6a8e8e4
JG
546 if (!stream_class) {
547 goto error;
662e778c 548 }
e6a8e8e4 549 assert(stream_class->event_header_type);
b8248cc0
PP
550 event = g_new0(struct bt_ctf_event, 1);
551 if (!event) {
e6a8e8e4 552 goto error;
b8248cc0
PP
553 }
554
83509119 555 bt_object_init(event, bt_ctf_event_destroy);
b8248cc0 556 bt_ctf_event_class_freeze(event_class);
e6a8e8e4
JG
557 /*
558 * event does not share a common ancestor with the event class; it has
559 * to guarantee its existence by holding a reference. This reference
560 * shall be released once the event is associated to a stream since,
561 * from that point, the event and its class will share the same
562 * lifetime.
563 */
564 event->event_class = bt_get(event_class);
b8248cc0 565
662e778c 566 event->event_header = bt_ctf_field_create(
e6a8e8e4 567 stream_class->event_header_type);
662e778c 568 if (!event->event_header) {
83509119 569 goto error;
662e778c 570 }
f655a84d
JG
571 if (event_class->context) {
572 event->context_payload = bt_ctf_field_create(
573 event_class->context);
662e778c 574 if (!event->context_payload) {
83509119 575 goto error;
662e778c 576 }
f655a84d 577 }
273b65be 578 event->fields_payload = bt_ctf_field_create(event_class->fields);
662e778c 579 if (!event->fields_payload) {
83509119 580 goto error;
662e778c
JG
581 }
582
583 /*
584 * Freeze the stream class since the event header must not be changed
585 * anymore.
586 */
e6a8e8e4
JG
587 bt_ctf_stream_class_freeze(stream_class);
588 BT_PUT(stream_class);
273b65be 589 return event;
83509119
JG
590error:
591 BT_PUT(event);
e6a8e8e4 592 BT_PUT(stream_class);
83509119 593 return event;
273b65be
JG
594}
595
2f100782
JG
596struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
597{
598 struct bt_ctf_event_class *event_class = NULL;
599
600 if (!event) {
601 goto end;
602 }
603
604 event_class = event->event_class;
83509119 605 bt_get(event_class);
2f100782
JG
606end:
607 return event_class;
608}
609
8e5003bb
JG
610struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
611{
e6a8e8e4 612 return (struct bt_ctf_stream *) bt_object_get_parent(event);
8e5003bb
JG
613}
614
2f100782
JG
615struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event)
616{
617 struct bt_ctf_clock *clock = NULL;
618 struct bt_ctf_event_class *event_class;
619 struct bt_ctf_stream_class *stream_class;
620
621 if (!event) {
622 goto end;
623 }
624
625 event_class = bt_ctf_event_get_class(event);
626 if (!event_class) {
627 goto end;
628 }
629
630 stream_class = bt_ctf_event_class_get_stream_class(event_class);
631 if (!stream_class) {
632 goto error_put_event_class;
633 }
634
635 clock = bt_ctf_stream_class_get_clock(stream_class);
636 if (!clock) {
637 goto error_put_stream_class;
638 }
639
640error_put_stream_class:
83509119 641 bt_put(stream_class);
2f100782 642error_put_event_class:
83509119 643 bt_put(event_class);
2f100782
JG
644end:
645 return clock;
646}
647
273b65be
JG
648int bt_ctf_event_set_payload(struct bt_ctf_event *event,
649 const char *name,
c5a9aa19 650 struct bt_ctf_field *payload)
273b65be
JG
651{
652 int ret = 0;
653
c5a9aa19 654 if (!event || !payload) {
273b65be
JG
655 ret = -1;
656 goto end;
657 }
658
c5a9aa19
JG
659 if (name) {
660 ret = bt_ctf_field_structure_set_field(event->fields_payload,
661 name, payload);
662 } else {
663 struct bt_ctf_field_type *payload_type;
664
665 payload_type = bt_ctf_field_get_type(payload);
666 if (payload_type == event->event_class->fields) {
83509119
JG
667 bt_put(event->fields_payload);
668 bt_get(payload);
c5a9aa19
JG
669 event->fields_payload = payload;
670 } else {
671 ret = -1;
672 }
673
83509119 674 bt_put(payload_type);
c5a9aa19 675 }
273b65be
JG
676end:
677 return ret;
678}
679
71362d53
PP
680struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event)
681{
682 struct bt_ctf_field *payload = NULL;
683
684 if (!event || !event->fields_payload) {
685 goto end;
686 }
687
688 payload = event->fields_payload;
83509119 689 bt_get(payload);
71362d53
PP
690end:
691 return payload;
692}
273b65be 693
e5e6eb3a
JG
694int bt_ctf_event_set_payload_field(struct bt_ctf_event *event,
695 struct bt_ctf_field *payload)
696{
697 int ret = 0;
698 struct bt_ctf_field_type *payload_type = NULL;
699
700 if (!event || !payload) {
701 ret = -1;
702 goto end;
703 }
704
705 payload_type = bt_ctf_field_get_type(payload);
706 if (!payload_type) {
707 ret = -1;
708 goto end;
709 }
710
711 if (bt_ctf_field_type_get_type_id(payload_type) != CTF_TYPE_STRUCT) {
712 ret = -1;
713 goto end;
714 }
715
83509119
JG
716 bt_get(payload);
717 bt_put(event->fields_payload);
e5e6eb3a
JG
718 event->fields_payload = payload;
719
720end:
83509119 721 bt_put(payload_type);
e5e6eb3a
JG
722 return ret;
723}
724
273b65be
JG
725struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
726 const char *name)
727{
728 struct bt_ctf_field *field = NULL;
729
c5a9aa19 730 if (!event) {
273b65be
JG
731 goto end;
732 }
733
c5a9aa19
JG
734 if (name) {
735 field = bt_ctf_field_structure_get_field(event->fields_payload,
736 name);
737 } else {
738 field = event->fields_payload;
83509119 739 bt_get(field);
c5a9aa19 740 }
273b65be
JG
741end:
742 return field;
743}
744
2f100782 745struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
074ee56d 746 struct bt_ctf_event *event, int index)
2f100782
JG
747{
748 struct bt_ctf_field *field = NULL;
749
074ee56d 750 if (!event || index < 0) {
2f100782
JG
751 goto end;
752 }
753
754 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
755 index);
756end:
757 return field;
758}
759
286a2840 760struct bt_ctf_field *bt_ctf_event_get_header(
662e778c
JG
761 struct bt_ctf_event *event)
762{
763 struct bt_ctf_field *header = NULL;
764
765 if (!event || !event->event_header) {
766 goto end;
767 }
768
769 header = event->event_header;
83509119 770 bt_get(header);
662e778c
JG
771end:
772 return header;
773}
774
286a2840 775int bt_ctf_event_set_header(struct bt_ctf_event *event,
662e778c
JG
776 struct bt_ctf_field *header)
777{
778 int ret = 0;
779 struct bt_ctf_field_type *field_type = NULL;
e6a8e8e4 780 struct bt_ctf_stream_class *stream_class = NULL;
662e778c
JG
781
782 if (!event || !header) {
783 ret = -1;
784 goto end;
785 }
786
e6a8e8e4
JG
787 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
788 event->event_class);
662e778c
JG
789 /*
790 * Ensure the provided header's type matches the one registered to the
791 * stream class.
792 */
793 field_type = bt_ctf_field_get_type(header);
e6a8e8e4 794 if (field_type != stream_class->event_header_type) {
662e778c
JG
795 ret = -1;
796 goto end;
797 }
798
83509119
JG
799 bt_get(header);
800 bt_put(event->event_header);
662e778c
JG
801 event->event_header = header;
802end:
e6a8e8e4 803 bt_put(stream_class);
83509119 804 bt_put(field_type);
662e778c
JG
805 return ret;
806}
807
f655a84d
JG
808struct bt_ctf_field *bt_ctf_event_get_event_context(
809 struct bt_ctf_event *event)
810{
811 struct bt_ctf_field *context = NULL;
812
813 if (!event || !event->context_payload) {
814 goto end;
815 }
816
817 context = event->context_payload;
83509119 818 bt_get(context);
f655a84d
JG
819end:
820 return context;
821}
822
823int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
824 struct bt_ctf_field *context)
825{
826 int ret = 0;
827 struct bt_ctf_field_type *field_type = NULL;
828
829 if (!event || !context) {
830 ret = -1;
831 goto end;
832 }
833
834 field_type = bt_ctf_field_get_type(context);
835 if (field_type != event->event_class->context) {
836 ret = -1;
837 goto end;
838 }
839
83509119
JG
840 bt_get(context);
841 bt_put(event->context_payload);
f655a84d
JG
842 event->context_payload = context;
843end:
83509119 844 bt_put(field_type);
f655a84d
JG
845 return ret;
846}
847
273b65be
JG
848void bt_ctf_event_get(struct bt_ctf_event *event)
849{
83509119 850 bt_get(event);
273b65be
JG
851}
852
853void bt_ctf_event_put(struct bt_ctf_event *event)
854{
83509119 855 bt_put(event);
273b65be
JG
856}
857
858static
83509119 859void bt_ctf_event_class_destroy(struct bt_object *obj)
273b65be
JG
860{
861 struct bt_ctf_event_class *event_class;
273b65be 862
83509119
JG
863 event_class = container_of(obj, struct bt_ctf_event_class, base);
864 bt_ctf_attributes_destroy(event_class->attributes);
865 bt_put(event_class->context);
866 bt_put(event_class->fields);
273b65be
JG
867 g_free(event_class);
868}
869
870static
83509119 871void bt_ctf_event_destroy(struct bt_object *obj)
273b65be
JG
872{
873 struct bt_ctf_event *event;
874
83509119 875 event = container_of(obj, struct bt_ctf_event, base);
e6a8e8e4
JG
876 if (!event->base.parent) {
877 /*
878 * Event was keeping a reference to its class since it shared no
879 * common ancestor with it to guarantee they would both have the
880 * same lifetime.
881 */
882 bt_put(event->event_class);
883 }
83509119
JG
884 bt_put(event->event_header);
885 bt_put(event->context_payload);
886 bt_put(event->fields_payload);
273b65be
JG
887 g_free(event);
888}
889
662e778c
JG
890static
891int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
892{
893 int ret = 0;
894 struct bt_ctf_field_type *field_type = NULL;
895
896 if (!field) {
897 ret = -1;
898 goto end;
899 }
900
901 if (!bt_ctf_field_validate(field)) {
902 /* Payload already set, skip! (not an error) */
903 goto end;
904 }
905
906 field_type = bt_ctf_field_get_type(field);
907 assert(field_type);
908
909 if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
910 /* Not an integer and the value is unset, error. */
911 ret = -1;
912 goto end;
913 }
914
915 if (bt_ctf_field_type_integer_get_signed(field_type)) {
916 ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value);
917 if (ret) {
918 /* Value is out of range, error. */
919 goto end;
920 }
921 } else {
922 ret = bt_ctf_field_unsigned_integer_set_value(field, value);
923 if (ret) {
924 /* Value is out of range, error. */
925 goto end;
926 }
927 }
928end:
83509119 929 bt_put(field_type);
662e778c
JG
930 return ret;
931}
932
273b65be
JG
933BT_HIDDEN
934void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
935{
936 assert(event_class);
937 event_class->frozen = 1;
938 bt_ctf_field_type_freeze(event_class->context);
939 bt_ctf_field_type_freeze(event_class->fields);
b8248cc0 940 bt_ctf_attributes_freeze(event_class->attributes);
273b65be
JG
941}
942
273b65be
JG
943BT_HIDDEN
944int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
945 struct metadata_context *context)
946{
b8248cc0
PP
947 int i;
948 int count;
273b65be 949 int ret = 0;
dac5c838 950 struct bt_value *attr_value = NULL;
273b65be
JG
951
952 assert(event_class);
953 assert(context);
b8248cc0
PP
954
955 context->current_indentation_level = 1;
956 g_string_assign(context->field_name, "");
957 g_string_append(context->string, "event {\n");
958 count = bt_ctf_event_class_get_attribute_count(event_class);
959
960 if (count < 0) {
2f100782
JG
961 ret = -1;
962 goto end;
963 }
964
b8248cc0
PP
965 for (i = 0; i < count; ++i) {
966 const char *attr_name = NULL;
967
968 attr_name = bt_ctf_event_class_get_attribute_name(
969 event_class, i);
970 attr_value = bt_ctf_event_class_get_attribute_value(
971 event_class, i);
972
973 if (!attr_name || !attr_value) {
974 ret = -1;
975 goto end;
976 }
977
dac5c838
PP
978 switch (bt_value_get_type(attr_value)) {
979 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
980 {
981 int64_t value;
982
dac5c838 983 ret = bt_value_integer_get(attr_value, &value);
b8248cc0
PP
984
985 if (ret) {
986 goto end;
987 }
988
989 g_string_append_printf(context->string,
990 "\t%s = %" PRId64 ";\n", attr_name, value);
991 break;
992 }
993
dac5c838 994 case BT_VALUE_TYPE_STRING:
b8248cc0
PP
995 {
996 const char *value;
997
dac5c838 998 ret = bt_value_string_get(attr_value, &value);
b8248cc0
PP
999
1000 if (ret) {
1001 goto end;
1002 }
1003
1004 g_string_append_printf(context->string,
1005 "\t%s = \"%s\";\n", attr_name, value);
1006 break;
1007 }
1008
1009 default:
1010 /* should never happen */
1011 assert(false);
1012 break;
1013 }
1014
83509119 1015 BT_PUT(attr_value);
b8248cc0 1016 }
273b65be
JG
1017
1018 if (event_class->context) {
1019 g_string_append(context->string, "\tcontext := ");
1020 ret = bt_ctf_field_type_serialize(event_class->context,
1021 context);
1022 if (ret) {
1023 goto end;
1024 }
1025 g_string_append(context->string, ";\n");
1026 }
1027
1028 if (event_class->fields) {
1029 g_string_append(context->string, "\tfields := ");
1030 ret = bt_ctf_field_type_serialize(event_class->fields, context);
1031 if (ret) {
1032 goto end;
1033 }
1034 g_string_append(context->string, ";\n");
1035 }
1036
1037 g_string_append(context->string, "};\n\n");
1038end:
1039 context->current_indentation_level = 0;
83509119 1040 BT_PUT(attr_value);
273b65be
JG
1041 return ret;
1042}
1043
c35a1669
JG
1044void bt_ctf_event_class_set_native_byte_order(
1045 struct bt_ctf_event_class *event_class,
1046 int byte_order)
1047{
1048 if (!event_class) {
1049 return;
1050 }
1051
445c3471
PP
1052 assert(byte_order == 0 || byte_order == LITTLE_ENDIAN ||
1053 byte_order == BIG_ENDIAN);
1054
c35a1669
JG
1055 bt_ctf_field_type_set_native_byte_order(event_class->context,
1056 byte_order);
1057 bt_ctf_field_type_set_native_byte_order(event_class->fields,
1058 byte_order);
1059}
1060
273b65be
JG
1061BT_HIDDEN
1062int bt_ctf_event_validate(struct bt_ctf_event *event)
1063{
1064 /* Make sure each field's payload has been set */
1065 int ret;
1066
1067 assert(event);
662e778c
JG
1068 ret = bt_ctf_field_validate(event->event_header);
1069 if (ret) {
1070 goto end;
1071 }
1072
273b65be
JG
1073 ret = bt_ctf_field_validate(event->fields_payload);
1074 if (ret) {
1075 goto end;
1076 }
1077
1078 if (event->event_class->context) {
1079 ret = bt_ctf_field_validate(event->context_payload);
1080 }
1081end:
1082 return ret;
1083}
1084
1085BT_HIDDEN
1086int bt_ctf_event_serialize(struct bt_ctf_event *event,
1087 struct ctf_stream_pos *pos)
1088{
1089 int ret = 0;
1090
1091 assert(event);
1092 assert(pos);
1093 if (event->context_payload) {
1094 ret = bt_ctf_field_serialize(event->context_payload, pos);
1095 if (ret) {
1096 goto end;
1097 }
1098 }
1099
1100 if (event->fields_payload) {
1101 ret = bt_ctf_field_serialize(event->fields_payload, pos);
1102 if (ret) {
1103 goto end;
1104 }
1105 }
1106end:
1107 return ret;
1108}
1109
662e778c
JG
1110BT_HIDDEN
1111int bt_ctf_event_populate_event_header(struct bt_ctf_event *event)
1112{
1113 int ret = 0;
1114 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
1115
1116 if (!event) {
1117 ret = -1;
1118 goto end;
1119 }
1120
1121 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
1122 if (id_field) {
1123 ret = set_integer_field_value(id_field,
b8248cc0
PP
1124 (uint64_t) bt_ctf_event_class_get_id(
1125 event->event_class));
662e778c
JG
1126 if (ret) {
1127 goto end;
1128 }
1129 }
1130
1131 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
1132 "timestamp");
1133 if (timestamp_field) {
9a220c32
JG
1134 struct bt_ctf_field_type *timestamp_field_type =
1135 bt_ctf_field_get_type(timestamp_field);
1136 struct bt_ctf_clock *mapped_clock;
1137
1138 assert(timestamp_field_type);
1139 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
1140 timestamp_field_type);
83509119 1141 bt_put(timestamp_field_type);
9a220c32 1142 if (mapped_clock) {
61cf588b 1143 int64_t timestamp;
9a220c32 1144
61cf588b 1145 ret = bt_ctf_clock_get_time(mapped_clock, &timestamp);
83509119 1146 bt_put(mapped_clock);
61cf588b 1147 if (ret) {
9a220c32
JG
1148 goto end;
1149 }
1150
1151 ret = set_integer_field_value(timestamp_field,
1152 timestamp);
1153 if (ret) {
1154 goto end;
1155 }
662e778c
JG
1156 }
1157 }
1158end:
83509119
JG
1159 bt_put(id_field);
1160 bt_put(timestamp_field);
662e778c
JG
1161 return ret;
1162}
123fbdec 1163
9c4c8f6e
PP
1164struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
1165{
1166 struct bt_ctf_event *copy = NULL;
1167
1168 if (!event) {
1169 goto error;
1170 }
1171
1172 copy = g_new0(struct bt_ctf_event, 1);
1173 if (!copy) {
1174 goto error;
1175 }
1176
83509119 1177 bt_object_init(copy, bt_ctf_event_destroy);
e6a8e8e4 1178 copy->event_class = bt_get(event->event_class);
9c4c8f6e
PP
1179
1180 if (event->event_header) {
1181 copy->event_header = bt_ctf_field_copy(event->event_header);
1182
1183 if (!copy->event_header) {
1184 goto error;
1185 }
1186 }
1187
1188 if (event->context_payload) {
1189 copy->context_payload = bt_ctf_field_copy(
1190 event->context_payload);
1191
1192 if (!copy->context_payload) {
1193 goto error;
1194 }
1195 }
1196
1197 if (event->fields_payload) {
1198 copy->fields_payload = bt_ctf_field_copy(event->fields_payload);
1199
1200 if (!copy->fields_payload) {
1201 goto error;
1202 }
1203 }
1204
1205 return copy;
1206
1207error:
83509119
JG
1208 BT_PUT(copy);
1209 return copy;
9c4c8f6e 1210}
This page took 0.086562 seconds and 4 git commands to generate.