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.h>
43 void bt_ctf_event_class_destroy(struct bt_object
*obj
);
45 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
48 struct bt_value
*obj
= NULL
;
49 struct bt_ctf_event_class
*event_class
= NULL
;
51 if (bt_ctf_validate_identifier(name
)) {
55 event_class
= g_new0(struct bt_ctf_event_class
, 1);
60 bt_object_init(event_class
, bt_ctf_event_class_destroy
);
61 event_class
->fields
= bt_ctf_field_type_structure_create();
62 if (!event_class
->fields
) {
66 event_class
->attributes
= bt_ctf_attributes_create();
67 if (!event_class
->attributes
) {
71 obj
= bt_value_integer_create_init(-1);
76 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
84 obj
= bt_value_string_create_init(name
);
89 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
105 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
107 struct bt_value
*obj
= NULL
;
108 const char *name
= NULL
;
114 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
115 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX
);
120 if (bt_value_string_get(obj
, &name
)) {
129 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
131 struct bt_value
*obj
= NULL
;
139 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
140 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
145 if (bt_value_integer_get(obj
, &ret
)) {
150 /* means ID is not set */
160 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
164 struct bt_value
*obj
= NULL
;
165 struct bt_ctf_stream_class
*stream_class
= NULL
;
173 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
176 * We don't allow changing the id if the event class has already
177 * been added to a stream class.
183 obj
= bt_ctf_attributes_get_field_value(event_class
->attributes
,
184 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX
);
189 if (bt_value_integer_set(obj
, id
)) {
196 BT_PUT(stream_class
);
200 int bt_ctf_event_class_set_attribute(
201 struct bt_ctf_event_class
*event_class
, const char *name
,
202 struct bt_value
*value
)
206 if (!event_class
|| !name
|| !value
|| event_class
->frozen
) {
211 if (!strcmp(name
, "id") || !strcmp(name
, "loglevel")) {
212 if (!bt_value_is_integer(value
)) {
216 } else if (!strcmp(name
, "name") || !strcmp(name
, "model.emf.uri")) {
217 if (!bt_value_is_string(value
)) {
222 /* unknown attribute */
227 /* "id" special case: >= 0 */
228 if (!strcmp(name
, "id")) {
231 ret
= bt_value_integer_get(value
, &val
);
243 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
250 int bt_ctf_event_class_get_attribute_count(
251 struct bt_ctf_event_class
*event_class
)
260 ret
= bt_ctf_attributes_get_count(event_class
->attributes
);
267 bt_ctf_event_class_get_attribute_name(
268 struct bt_ctf_event_class
*event_class
, int index
)
277 ret
= bt_ctf_attributes_get_field_name(event_class
->attributes
, index
);
284 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class
*event_class
,
287 struct bt_value
*ret
;
294 ret
= bt_ctf_attributes_get_field_value(event_class
->attributes
, index
);
301 bt_ctf_event_class_get_attribute_value_by_name(
302 struct bt_ctf_event_class
*event_class
, const char *name
)
304 struct bt_value
*ret
;
306 if (!event_class
|| !name
) {
311 ret
= bt_ctf_attributes_get_field_value_by_name(event_class
->attributes
,
319 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
320 struct bt_ctf_event_class
*event_class
)
322 return (struct bt_ctf_stream_class
*) bt_object_get_parent(event_class
);
325 struct bt_ctf_field_type
*bt_ctf_event_class_get_payload_type(
326 struct bt_ctf_event_class
*event_class
)
328 struct bt_ctf_field_type
*payload
= NULL
;
334 bt_get(event_class
->fields
);
335 payload
= event_class
->fields
;
340 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class
*event_class
,
341 struct bt_ctf_field_type
*payload
)
345 if (!event_class
|| !payload
||
346 bt_ctf_field_type_get_type_id(payload
) !=
347 BT_CTF_TYPE_ID_STRUCT
) {
353 bt_put(event_class
->fields
);
354 event_class
->fields
= payload
;
359 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
360 struct bt_ctf_field_type
*type
,
365 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
366 event_class
->frozen
) {
371 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
372 BT_CTF_TYPE_ID_STRUCT
) {
377 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
383 int bt_ctf_event_class_get_field_count(
384 struct bt_ctf_event_class
*event_class
)
393 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
394 BT_CTF_TYPE_ID_STRUCT
) {
399 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
404 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
405 const char **field_name
, struct bt_ctf_field_type
**field_type
,
410 if (!event_class
|| index
< 0) {
415 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
416 BT_CTF_TYPE_ID_STRUCT
) {
421 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
422 field_name
, field_type
, index
);
427 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
428 struct bt_ctf_event_class
*event_class
, const char *name
)
431 struct bt_ctf_field_type
*field_type
= NULL
;
433 if (!event_class
|| !name
) {
437 if (bt_ctf_field_type_get_type_id(event_class
->fields
) !=
438 BT_CTF_TYPE_ID_STRUCT
) {
442 name_quark
= g_quark_try_string(name
);
448 * No need to increment field_type's reference count since getting it
449 * from the structure already does.
451 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
452 event_class
->fields
, name
);
457 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
458 struct bt_ctf_event_class
*event_class
)
460 struct bt_ctf_field_type
*context_type
= NULL
;
462 if (!event_class
|| !event_class
->context
) {
466 bt_get(event_class
->context
);
467 context_type
= event_class
->context
;
472 int bt_ctf_event_class_set_context_type(
473 struct bt_ctf_event_class
*event_class
,
474 struct bt_ctf_field_type
*context
)
478 if (!event_class
|| !context
|| event_class
->frozen
) {
483 if (bt_ctf_field_type_get_type_id(context
) != BT_CTF_TYPE_ID_STRUCT
) {
489 bt_put(event_class
->context
);
490 event_class
->context
= context
;
496 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
501 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
507 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class
*event_class
,
511 struct bt_value
*obj
;
513 obj
= bt_value_integer_create_init(stream_id
);
520 ret
= bt_ctf_attributes_set_field_value(event_class
->attributes
,
523 if (event_class
->frozen
) {
524 bt_ctf_attributes_freeze(event_class
->attributes
);
533 void bt_ctf_event_class_destroy(struct bt_object
*obj
)
535 struct bt_ctf_event_class
*event_class
;
537 event_class
= container_of(obj
, struct bt_ctf_event_class
, base
);
538 bt_ctf_attributes_destroy(event_class
->attributes
);
539 bt_put(event_class
->context
);
540 bt_put(event_class
->fields
);
545 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
548 event_class
->frozen
= 1;
549 bt_ctf_field_type_freeze(event_class
->context
);
550 bt_ctf_field_type_freeze(event_class
->fields
);
551 bt_ctf_attributes_freeze(event_class
->attributes
);
555 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
556 struct metadata_context
*context
)
561 struct bt_value
*attr_value
= NULL
;
566 context
->current_indentation_level
= 1;
567 g_string_assign(context
->field_name
, "");
568 g_string_append(context
->string
, "event {\n");
569 count
= bt_ctf_event_class_get_attribute_count(event_class
);
576 for (i
= 0; i
< count
; ++i
) {
577 const char *attr_name
= NULL
;
579 attr_name
= bt_ctf_event_class_get_attribute_name(
581 attr_value
= bt_ctf_event_class_get_attribute_value(
584 if (!attr_name
|| !attr_value
) {
589 switch (bt_value_get_type(attr_value
)) {
590 case BT_VALUE_TYPE_INTEGER
:
594 ret
= bt_value_integer_get(attr_value
, &value
);
600 g_string_append_printf(context
->string
,
601 "\t%s = %" PRId64
";\n", attr_name
, value
);
605 case BT_VALUE_TYPE_STRING
:
609 ret
= bt_value_string_get(attr_value
, &value
);
615 g_string_append_printf(context
->string
,
616 "\t%s = \"%s\";\n", attr_name
, value
);
621 /* should never happen */
629 if (event_class
->context
) {
630 g_string_append(context
->string
, "\tcontext := ");
631 ret
= bt_ctf_field_type_serialize(event_class
->context
,
636 g_string_append(context
->string
, ";\n");
639 if (event_class
->fields
) {
640 g_string_append(context
->string
, "\tfields := ");
641 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
645 g_string_append(context
->string
, ";\n");
648 g_string_append(context
->string
, "};\n\n");
650 context
->current_indentation_level
= 0;
655 void bt_ctf_event_class_set_native_byte_order(
656 struct bt_ctf_event_class
*event_class
,
663 assert(byte_order
== 0 || byte_order
== LITTLE_ENDIAN
||
664 byte_order
== BIG_ENDIAN
);
666 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
668 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,