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