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