Handle negative time and offset from Epoch
[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
2f100782
JG
863 /*
864 * Don't call put() on the stream class. See comment in
865 * bt_ctf_event_class_set_stream_class for explanation.
866 */
83509119
JG
867 event_class = container_of(obj, struct bt_ctf_event_class, base);
868 bt_ctf_attributes_destroy(event_class->attributes);
869 bt_put(event_class->context);
870 bt_put(event_class->fields);
273b65be
JG
871 g_free(event_class);
872}
873
874static
83509119 875void bt_ctf_event_destroy(struct bt_object *obj)
273b65be
JG
876{
877 struct bt_ctf_event *event;
878
83509119 879 event = container_of(obj, struct bt_ctf_event, base);
e6a8e8e4
JG
880 if (!event->base.parent) {
881 /*
882 * Event was keeping a reference to its class since it shared no
883 * common ancestor with it to guarantee they would both have the
884 * same lifetime.
885 */
886 bt_put(event->event_class);
887 }
83509119
JG
888 bt_put(event->event_header);
889 bt_put(event->context_payload);
890 bt_put(event->fields_payload);
273b65be
JG
891 g_free(event);
892}
893
662e778c
JG
894static
895int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
896{
897 int ret = 0;
898 struct bt_ctf_field_type *field_type = NULL;
899
900 if (!field) {
901 ret = -1;
902 goto end;
903 }
904
905 if (!bt_ctf_field_validate(field)) {
906 /* Payload already set, skip! (not an error) */
907 goto end;
908 }
909
910 field_type = bt_ctf_field_get_type(field);
911 assert(field_type);
912
913 if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
914 /* Not an integer and the value is unset, error. */
915 ret = -1;
916 goto end;
917 }
918
919 if (bt_ctf_field_type_integer_get_signed(field_type)) {
920 ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value);
921 if (ret) {
922 /* Value is out of range, error. */
923 goto end;
924 }
925 } else {
926 ret = bt_ctf_field_unsigned_integer_set_value(field, value);
927 if (ret) {
928 /* Value is out of range, error. */
929 goto end;
930 }
931 }
932end:
83509119 933 bt_put(field_type);
662e778c
JG
934 return ret;
935}
936
273b65be
JG
937BT_HIDDEN
938void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
939{
940 assert(event_class);
941 event_class->frozen = 1;
942 bt_ctf_field_type_freeze(event_class->context);
943 bt_ctf_field_type_freeze(event_class->fields);
b8248cc0 944 bt_ctf_attributes_freeze(event_class->attributes);
273b65be
JG
945}
946
273b65be
JG
947BT_HIDDEN
948int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
949 struct metadata_context *context)
950{
b8248cc0
PP
951 int i;
952 int count;
273b65be 953 int ret = 0;
dac5c838 954 struct bt_value *attr_value = NULL;
273b65be
JG
955
956 assert(event_class);
957 assert(context);
b8248cc0
PP
958
959 context->current_indentation_level = 1;
960 g_string_assign(context->field_name, "");
961 g_string_append(context->string, "event {\n");
962 count = bt_ctf_event_class_get_attribute_count(event_class);
963
964 if (count < 0) {
2f100782
JG
965 ret = -1;
966 goto end;
967 }
968
b8248cc0
PP
969 for (i = 0; i < count; ++i) {
970 const char *attr_name = NULL;
971
972 attr_name = bt_ctf_event_class_get_attribute_name(
973 event_class, i);
974 attr_value = bt_ctf_event_class_get_attribute_value(
975 event_class, i);
976
977 if (!attr_name || !attr_value) {
978 ret = -1;
979 goto end;
980 }
981
dac5c838
PP
982 switch (bt_value_get_type(attr_value)) {
983 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
984 {
985 int64_t value;
986
dac5c838 987 ret = bt_value_integer_get(attr_value, &value);
b8248cc0
PP
988
989 if (ret) {
990 goto end;
991 }
992
993 g_string_append_printf(context->string,
994 "\t%s = %" PRId64 ";\n", attr_name, value);
995 break;
996 }
997
dac5c838 998 case BT_VALUE_TYPE_STRING:
b8248cc0
PP
999 {
1000 const char *value;
1001
dac5c838 1002 ret = bt_value_string_get(attr_value, &value);
b8248cc0
PP
1003
1004 if (ret) {
1005 goto end;
1006 }
1007
1008 g_string_append_printf(context->string,
1009 "\t%s = \"%s\";\n", attr_name, value);
1010 break;
1011 }
1012
1013 default:
1014 /* should never happen */
1015 assert(false);
1016 break;
1017 }
1018
83509119 1019 BT_PUT(attr_value);
b8248cc0 1020 }
273b65be
JG
1021
1022 if (event_class->context) {
1023 g_string_append(context->string, "\tcontext := ");
1024 ret = bt_ctf_field_type_serialize(event_class->context,
1025 context);
1026 if (ret) {
1027 goto end;
1028 }
1029 g_string_append(context->string, ";\n");
1030 }
1031
1032 if (event_class->fields) {
1033 g_string_append(context->string, "\tfields := ");
1034 ret = bt_ctf_field_type_serialize(event_class->fields, context);
1035 if (ret) {
1036 goto end;
1037 }
1038 g_string_append(context->string, ";\n");
1039 }
1040
1041 g_string_append(context->string, "};\n\n");
1042end:
1043 context->current_indentation_level = 0;
83509119 1044 BT_PUT(attr_value);
273b65be
JG
1045 return ret;
1046}
1047
c35a1669
JG
1048void bt_ctf_event_class_set_native_byte_order(
1049 struct bt_ctf_event_class *event_class,
1050 int byte_order)
1051{
1052 if (!event_class) {
1053 return;
1054 }
1055
1056 bt_ctf_field_type_set_native_byte_order(event_class->context,
1057 byte_order);
1058 bt_ctf_field_type_set_native_byte_order(event_class->fields,
1059 byte_order);
1060}
1061
273b65be
JG
1062BT_HIDDEN
1063int bt_ctf_event_validate(struct bt_ctf_event *event)
1064{
1065 /* Make sure each field's payload has been set */
1066 int ret;
1067
1068 assert(event);
662e778c
JG
1069 ret = bt_ctf_field_validate(event->event_header);
1070 if (ret) {
1071 goto end;
1072 }
1073
273b65be
JG
1074 ret = bt_ctf_field_validate(event->fields_payload);
1075 if (ret) {
1076 goto end;
1077 }
1078
1079 if (event->event_class->context) {
1080 ret = bt_ctf_field_validate(event->context_payload);
1081 }
1082end:
1083 return ret;
1084}
1085
1086BT_HIDDEN
1087int bt_ctf_event_serialize(struct bt_ctf_event *event,
1088 struct ctf_stream_pos *pos)
1089{
1090 int ret = 0;
1091
1092 assert(event);
1093 assert(pos);
1094 if (event->context_payload) {
1095 ret = bt_ctf_field_serialize(event->context_payload, pos);
1096 if (ret) {
1097 goto end;
1098 }
1099 }
1100
1101 if (event->fields_payload) {
1102 ret = bt_ctf_field_serialize(event->fields_payload, pos);
1103 if (ret) {
1104 goto end;
1105 }
1106 }
1107end:
1108 return ret;
1109}
1110
662e778c
JG
1111BT_HIDDEN
1112int bt_ctf_event_populate_event_header(struct bt_ctf_event *event)
1113{
1114 int ret = 0;
1115 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
1116
1117 if (!event) {
1118 ret = -1;
1119 goto end;
1120 }
1121
1122 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
1123 if (id_field) {
1124 ret = set_integer_field_value(id_field,
b8248cc0
PP
1125 (uint64_t) bt_ctf_event_class_get_id(
1126 event->event_class));
662e778c
JG
1127 if (ret) {
1128 goto end;
1129 }
1130 }
1131
1132 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
1133 "timestamp");
1134 if (timestamp_field) {
9a220c32
JG
1135 struct bt_ctf_field_type *timestamp_field_type =
1136 bt_ctf_field_get_type(timestamp_field);
1137 struct bt_ctf_clock *mapped_clock;
1138
1139 assert(timestamp_field_type);
1140 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
1141 timestamp_field_type);
83509119 1142 bt_put(timestamp_field_type);
9a220c32 1143 if (mapped_clock) {
61cf588b 1144 int64_t timestamp;
9a220c32 1145
61cf588b 1146 ret = bt_ctf_clock_get_time(mapped_clock, &timestamp);
83509119 1147 bt_put(mapped_clock);
61cf588b 1148 if (ret) {
9a220c32
JG
1149 goto end;
1150 }
1151
1152 ret = set_integer_field_value(timestamp_field,
1153 timestamp);
1154 if (ret) {
1155 goto end;
1156 }
662e778c
JG
1157 }
1158 }
1159end:
83509119
JG
1160 bt_put(id_field);
1161 bt_put(timestamp_field);
662e778c
JG
1162 return ret;
1163}
123fbdec 1164
9c4c8f6e
PP
1165struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
1166{
1167 struct bt_ctf_event *copy = NULL;
1168
1169 if (!event) {
1170 goto error;
1171 }
1172
1173 copy = g_new0(struct bt_ctf_event, 1);
1174 if (!copy) {
1175 goto error;
1176 }
1177
83509119 1178 bt_object_init(copy, bt_ctf_event_destroy);
e6a8e8e4 1179 copy->event_class = bt_get(event->event_class);
9c4c8f6e
PP
1180
1181 if (event->event_header) {
1182 copy->event_header = bt_ctf_field_copy(event->event_header);
1183
1184 if (!copy->event_header) {
1185 goto error;
1186 }
1187 }
1188
1189 if (event->context_payload) {
1190 copy->context_payload = bt_ctf_field_copy(
1191 event->context_payload);
1192
1193 if (!copy->context_payload) {
1194 goto error;
1195 }
1196 }
1197
1198 if (event->fields_payload) {
1199 copy->fields_payload = bt_ctf_field_copy(event->fields_payload);
1200
1201 if (!copy->fields_payload) {
1202 goto error;
1203 }
1204 }
1205
1206 return copy;
1207
1208error:
83509119
JG
1209 BT_PUT(copy);
1210 return copy;
9c4c8f6e 1211}
This page took 0.082114 seconds and 4 git commands to generate.