4 * Babeltrace CTF Writer
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-writer/event.h>
30 #include <babeltrace/ctf-writer/event-types.h>
31 #include <babeltrace/ctf-writer/event-fields.h>
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>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/compiler.h>
39 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
41 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
43 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
45 struct bt_ctf_event_class
*event_class
= NULL
;
47 if (validate_identifier(name
)) {
51 event_class
= g_new0(struct bt_ctf_event_class
, 1);
56 bt_ctf_ref_init(&event_class
->ref_count
);
57 event_class
->name
= g_quark_from_string(name
);
62 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
63 struct bt_ctf_field_type
*type
,
68 if (!event_class
|| !type
|| validate_identifier(name
) ||
69 event_class
->frozen
) {
74 if (!event_class
->fields
) {
75 event_class
->fields
= bt_ctf_field_type_structure_create();
76 if (!event_class
->fields
) {
82 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
88 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
94 bt_ctf_ref_get(&event_class
->ref_count
);
97 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
103 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
106 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
108 struct bt_ctf_event
*event
= NULL
;
114 event
= g_new0(struct bt_ctf_event
, 1);
119 bt_ctf_ref_init(&event
->ref_count
);
120 bt_ctf_event_class_get(event_class
);
121 bt_ctf_event_class_freeze(event_class
);
122 event
->event_class
= event_class
;
123 event
->context_payload
= bt_ctf_field_create(event_class
->context
);
124 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
129 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
131 struct bt_ctf_field
*value
)
135 if (!event
|| !value
|| validate_identifier(name
)) {
140 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
147 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
150 struct bt_ctf_field
*field
= NULL
;
152 if (!event
|| !name
) {
156 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
161 void bt_ctf_event_get(struct bt_ctf_event
*event
)
167 bt_ctf_ref_get(&event
->ref_count
);
170 void bt_ctf_event_put(struct bt_ctf_event
*event
)
176 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
180 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
182 struct bt_ctf_event_class
*event_class
;
188 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
189 bt_ctf_field_type_put(event_class
->context
);
190 bt_ctf_field_type_put(event_class
->fields
);
195 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
197 struct bt_ctf_event
*event
;
203 event
= container_of(ref
, struct bt_ctf_event
,
205 bt_ctf_event_class_put(event
->event_class
);
206 bt_ctf_field_put(event
->context_payload
);
207 bt_ctf_field_put(event
->fields_payload
);
212 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
215 event_class
->frozen
= 1;
216 bt_ctf_field_type_freeze(event_class
->context
);
217 bt_ctf_field_type_freeze(event_class
->fields
);
221 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
226 if (event_class
->id_set
&& id
!= event_class
->id
) {
231 event_class
->id
= id
;
232 event_class
->id_set
= 1;
238 uint32_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
241 return event_class
->id
;
245 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class
*event_class
,
251 if (event_class
->stream_id_set
&& id
!= event_class
->stream_id
) {
256 event_class
->stream_id
= id
;
257 event_class
->stream_id_set
= 1;
263 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
264 struct metadata_context
*context
)
270 context
->current_indentation_level
= 1;
271 g_string_assign(context
->field_name
, "");
272 g_string_append_printf(context
->string
, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRIu32
";\n",
273 g_quark_to_string(event_class
->name
),
275 event_class
->stream_id
);
277 if (event_class
->context
) {
278 g_string_append(context
->string
, "\tcontext := ");
279 ret
= bt_ctf_field_type_serialize(event_class
->context
,
284 g_string_append(context
->string
, ";\n");
287 if (event_class
->fields
) {
288 g_string_append(context
->string
, "\tfields := ");
289 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
293 g_string_append(context
->string
, ";\n");
296 g_string_append(context
->string
, "};\n\n");
298 context
->current_indentation_level
= 0;
303 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
305 /* Make sure each field's payload has been set */
309 ret
= bt_ctf_field_validate(event
->fields_payload
);
314 if (event
->event_class
->context
) {
315 ret
= bt_ctf_field_validate(event
->context_payload
);
322 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
323 struct ctf_stream_pos
*pos
)
329 if (event
->context_payload
) {
330 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
336 if (event
->fields_payload
) {
337 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
347 int bt_ctf_event_set_timestamp(struct bt_ctf_event
*event
,
353 if (event
->timestamp
) {
358 event
->timestamp
= timestamp
;
364 uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event
*event
)
367 return event
->timestamp
;