4 * Babeltrace CTF IR - Event class
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/ctf-ir/fields-internal.h>
30 #include <babeltrace/ctf-ir/field-types-internal.h>
31 #include <babeltrace/ctf-ir/event-class.h>
32 #include <babeltrace/ctf-ir/event-class-internal.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/stream-class-internal.h>
35 #include <babeltrace/ctf-ir/trace-internal.h>
36 #include <babeltrace/ctf-ir/validation-internal.h>
37 #include <babeltrace/ctf-ir/utils.h>
38 #include <babeltrace/ref.h>
39 #include <babeltrace/ctf-ir/attributes-internal.h>
40 #include <babeltrace/compiler-internal.h>
41 #include <babeltrace/endian-internal.h>
42 #include <babeltrace/types.h>
46 void bt_ctf_event_class_destroy(struct bt_object
*obj
);
48 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
51 struct bt_value
*obj
= NULL
;
52 struct bt_ctf_event_class
*event_class
= NULL
;
54 if (bt_ctf_validate_identifier(name
)) {
58 event_class
= g_new0(struct bt_ctf_event_class
, 1);
64 bt_object_init(event_class
, bt_ctf_event_class_destroy
);
65 event_class
->fields
= bt_ctf_field_type_structure_create();
66 if (!event_class
->fields
) {
70 event_class
->context
= bt_ctf_field_type_structure_create();
71 if (!event_class
->context
) {
75 event_class
->attributes
= bt_ctf_attributes_create();
76 if (!event_class
->attributes
) {
80 obj
= bt_value_integer_create_init(-1);
85 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
93 obj
= bt_value_string_create_init(name
);
98 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
114 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
116 struct bt_value
*obj
= NULL
;
117 const char *name
= NULL
;
123 if (event_class
->name
) {
124 name
= event_class
->name
;
128 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
129 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX
);
134 if (bt_value_string_get(obj
, &name
)) {
143 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
145 struct bt_value
*obj
= NULL
;
153 if (event_class
->id
>= 0) {
154 ret
= (int64_t) event_class
->id
;
158 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
159 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
164 if (bt_value_integer_get(obj
, &ret
)) {
169 /* means ID is not set */
179 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
183 struct bt_value
*obj
= NULL
;
184 struct bt_ctf_stream_class
*stream_class
= NULL
;
185 int64_t id
= (int64_t) id_param
;
187 if (!event_class
|| id
< 0) {
192 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
195 * We don't allow changing the id if the event class has already
196 * been added to a stream class.
202 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
203 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
208 if (bt_value_integer_set(obj
, id
)) {
215 BT_PUT(stream_class
);
219 int bt_ctf_event_class_set_attribute(
220 struct bt_ctf_event_class
*event_class
, const char *name
,
221 struct bt_value
*value
)
225 if (!event_class
|| !name
|| !value
|| event_class
->frozen
) {
230 if (!strcmp(name
, "id") || !strcmp(name
, "loglevel") ||
231 !strcmp(name
, "stream_id")) {
232 if (!bt_value_is_integer(value
)) {
236 } else if (!strcmp(name
, "name") || !strcmp(name
, "model.emf.uri") ||
237 !strcmp(name
, "loglevel_string")) {
238 if (!bt_value_is_string(value
)) {
243 /* unknown attribute */
248 /* "id" special case: >= 0 */
249 if (!strcmp(name
, "id")) {
252 ret
= bt_value_integer_get(value
, &val
);
264 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
271 int64_t bt_ctf_event_class_get_attribute_count(
272 struct bt_ctf_event_class
*event_class
)
281 ret
= bt_ctf_attributes_get_count(event_class
->attributes
);
288 bt_ctf_event_class_get_attribute_name_by_index(
289 struct bt_ctf_event_class
*event_class
, uint64_t index
)
298 ret
= bt_ctf_attributes_get_field_name(event_class
->attributes
, index
);
305 bt_ctf_event_class_get_attribute_value_by_index(
306 struct bt_ctf_event_class
*event_class
, uint64_t index
)
308 struct bt_value
*ret
;
315 ret
= bt_ctf_attributes_get_field_value(event_class
->attributes
, index
);
322 bt_ctf_event_class_get_attribute_value_by_name(
323 struct bt_ctf_event_class
*event_class
, const char *name
)
325 struct bt_value
*ret
;
327 if (!event_class
|| !name
) {
332 ret
= bt_ctf_attributes_get_field_value_by_name(event_class
->attributes
,
340 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
341 struct bt_ctf_event_class
*event_class
)
344 bt_get(bt_ctf_event_class_borrow_stream_class(event_class
)) :
348 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
349 struct bt_ctf_event_class
*event_class
)
351 struct bt_ctf_field_type
*payload
= NULL
;
357 bt_get(event_class
->fields
);
358 payload
= event_class
->fields
;
363 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
364 struct bt_ctf_field_type
*payload
)
373 if (payload
&& bt_ctf_field_type_get_type_id(payload
) !=
374 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
379 bt_put(event_class
->fields
);
380 event_class
->fields
= bt_get(payload
);
385 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
386 struct bt_ctf_field_type
*type
,
391 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
392 event_class
->frozen
) {
397 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
398 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
403 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
409 int64_t bt_ctf_event_class_get_payload_type_field_count(
410 struct bt_ctf_event_class
*event_class
)
419 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
420 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
425 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
430 int bt_ctf_event_class_get_payload_type_field_by_index(
431 struct bt_ctf_event_class
*event_class
,
432 const char **field_name
, struct bt_ctf_field_type
**field_type
,
442 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
443 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
448 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
449 field_name
, field_type
, index
);
454 struct bt_ctf_field_type
*
455 bt_ctf_event_class_get_payload_type_field_type_by_name(
456 struct bt_ctf_event_class
*event_class
, const char *name
)
459 struct bt_ctf_field_type
*field_type
= NULL
;
461 if (!event_class
|| !name
) {
465 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
466 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
470 name_quark
= g_quark_try_string(name
);
476 * No need to increment field_type's reference count since getting it
477 * from the structure already does.
479 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
480 event_class
->fields
, name
);
485 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
486 struct bt_ctf_event_class
*event_class
)
488 struct bt_ctf_field_type
*context_type
= NULL
;
490 if (!event_class
|| !event_class
->context
) {
494 bt_get(event_class
->context
);
495 context_type
= event_class
->context
;
500 int bt_ctf_event_class_set_context_type(
501 struct bt_ctf_event_class
*event_class
,
502 struct bt_ctf_field_type
*context
)
506 if (!event_class
|| event_class
->frozen
) {
511 if (context
&& bt_ctf_field_type_get_type_id(context
) !=
512 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
517 bt_put(event_class
->context
);
518 event_class
->context
= bt_get(context
);
524 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
529 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
535 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class
*event_class
,
536 uint64_t stream_id_param
)
539 struct bt_value
*obj
= NULL
;
540 int64_t stream_id
= (int64_t) stream_id_param
;
549 obj
= bt_value_integer_create_init(stream_id
);
556 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
559 if (event_class
->frozen
) {
560 bt_ctf_attributes_freeze(event_class
->attributes
);
569 void bt_ctf_event_class_destroy(struct bt_object
*obj
)
571 struct bt_ctf_event_class
*event_class
;
573 event_class
= container_of(obj
, struct bt_ctf_event_class
, base
);
574 bt_ctf_attributes_destroy(event_class
->attributes
);
575 bt_put(event_class
->context
);
576 bt_put(event_class
->fields
);
581 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
584 event_class
->frozen
= 1;
585 event_class
->name
= bt_ctf_event_class_get_name(event_class
);
586 event_class
->id
= bt_ctf_event_class_get_id(event_class
);
587 bt_ctf_field_type_freeze(event_class
->context
);
588 bt_ctf_field_type_freeze(event_class
->fields
);
589 bt_ctf_attributes_freeze(event_class
->attributes
);
593 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
594 struct metadata_context
*context
)
599 struct bt_value
*attr_value
= NULL
;
604 context
->current_indentation_level
= 1;
605 g_string_assign(context
->field_name
, "");
606 g_string_append(context
->string
, "event {\n");
607 count
= bt_ctf_event_class_get_attribute_count(event_class
);
614 for (i
= 0; i
< count
; ++i
) {
615 const char *attr_name
= NULL
;
617 attr_name
= bt_ctf_event_class_get_attribute_name_by_index(
619 attr_value
= bt_ctf_event_class_get_attribute_value_by_index(
622 if (!attr_name
|| !attr_value
) {
627 switch (bt_value_get_type(attr_value
)) {
628 case BT_VALUE_TYPE_INTEGER
:
632 ret
= bt_value_integer_get(attr_value
, &value
);
638 g_string_append_printf(context
->string
,
639 "\t%s = %" PRId64
";\n", attr_name
, value
);
643 case BT_VALUE_TYPE_STRING
:
647 ret
= bt_value_string_get(attr_value
, &value
);
653 g_string_append_printf(context
->string
,
654 "\t%s = \"%s\";\n", attr_name
, value
);
659 /* should never happen */
667 if (event_class
->context
) {
668 g_string_append(context
->string
, "\tcontext := ");
669 ret
= bt_ctf_field_type_serialize(event_class
->context
,
674 g_string_append(context
->string
, ";\n");
677 if (event_class
->fields
) {
678 g_string_append(context
->string
, "\tfields := ");
679 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
683 g_string_append(context
->string
, ";\n");
686 g_string_append(context
->string
, "};\n\n");
688 context
->current_indentation_level
= 0;