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-ir/stream-class.h>
36 #include <babeltrace/ctf-writer/writer-internal.h>
37 #include <babeltrace/compiler.h>
40 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
42 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
44 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
46 struct bt_ctf_event_class
*event_class
= NULL
;
48 if (validate_identifier(name
)) {
52 event_class
= g_new0(struct bt_ctf_event_class
, 1);
57 bt_ctf_ref_init(&event_class
->ref_count
);
58 event_class
->name
= g_quark_from_string(name
);
63 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
65 const char *name
= NULL
;
71 name
= g_quark_to_string(event_class
->name
);
76 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
80 if (!event_class
|| !event_class
->id_set
) {
85 ret
= (int64_t) event_class
->id
;
90 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
100 if (event_class
->stream_class
) {
102 * We don't allow changing the id if the event class has already
103 * been added to a stream class.
109 event_class
->id
= id
;
110 event_class
->id_set
= 1;
115 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
116 struct bt_ctf_event_class
*event_class
)
118 struct bt_ctf_stream_class
*stream_class
= NULL
;
124 stream_class
= event_class
->stream_class
;
125 bt_ctf_stream_class_get(stream_class
);
130 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
131 struct bt_ctf_field_type
*type
,
136 if (!event_class
|| !type
|| validate_identifier(name
) ||
137 event_class
->frozen
) {
142 if (!event_class
->fields
) {
143 event_class
->fields
= bt_ctf_field_type_structure_create();
144 if (!event_class
->fields
) {
150 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
156 int64_t bt_ctf_event_class_get_field_count(
157 struct bt_ctf_event_class
*event_class
)
166 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
171 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
172 const char **field_name
, struct bt_ctf_field_type
**field_type
,
182 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
183 field_name
, field_type
, index
);
188 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
189 struct bt_ctf_event_class
*event_class
, const char *name
)
192 struct bt_ctf_field_type
*field_type
= NULL
;
194 if (!event_class
|| !name
) {
198 name_quark
= g_quark_try_string(name
);
204 * No need to increment field_type's reference count since getting it
205 * from the structure already does.
207 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
208 event_class
->fields
, name
);
213 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
219 bt_ctf_ref_get(&event_class
->ref_count
);
222 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
228 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
231 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
233 struct bt_ctf_event
*event
= NULL
;
239 event
= g_new0(struct bt_ctf_event
, 1);
244 bt_ctf_ref_init(&event
->ref_count
);
245 bt_ctf_event_class_get(event_class
);
246 bt_ctf_event_class_freeze(event_class
);
247 event
->event_class
= event_class
;
248 event
->context_payload
= bt_ctf_field_create(event_class
->context
);
249 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
254 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
256 struct bt_ctf_event_class
*event_class
= NULL
;
262 event_class
= event
->event_class
;
263 bt_ctf_event_class_get(event_class
);
268 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
270 struct bt_ctf_clock
*clock
= NULL
;
271 struct bt_ctf_event_class
*event_class
;
272 struct bt_ctf_stream_class
*stream_class
;
278 event_class
= bt_ctf_event_get_class(event
);
283 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
285 goto error_put_event_class
;
288 clock
= bt_ctf_stream_class_get_clock(stream_class
);
290 goto error_put_stream_class
;
293 error_put_stream_class
:
294 bt_ctf_stream_class_put(stream_class
);
295 error_put_event_class
:
296 bt_ctf_event_class_put(event_class
);
301 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
303 struct bt_ctf_field
*value
)
307 if (!event
|| !value
|| validate_identifier(name
)) {
312 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
319 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
322 struct bt_ctf_field
*field
= NULL
;
324 if (!event
|| !name
) {
328 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
333 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
334 struct bt_ctf_event
*event
, size_t index
)
336 struct bt_ctf_field
*field
= NULL
;
342 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
348 void bt_ctf_event_get(struct bt_ctf_event
*event
)
354 bt_ctf_ref_get(&event
->ref_count
);
357 void bt_ctf_event_put(struct bt_ctf_event
*event
)
363 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
367 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
369 struct bt_ctf_event_class
*event_class
;
376 * Don't call put() on the stream class. See comment in
377 * bt_ctf_event_class_set_stream_class for explanation.
379 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
380 bt_ctf_field_type_put(event_class
->context
);
381 bt_ctf_field_type_put(event_class
->fields
);
386 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
388 struct bt_ctf_event
*event
;
394 event
= container_of(ref
, struct bt_ctf_event
,
396 bt_ctf_event_class_put(event
->event_class
);
397 bt_ctf_field_put(event
->context_payload
);
398 bt_ctf_field_put(event
->fields_payload
);
403 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
406 event_class
->frozen
= 1;
407 bt_ctf_field_type_freeze(event_class
->context
);
408 bt_ctf_field_type_freeze(event_class
->fields
);
412 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
413 struct bt_ctf_stream_class
*stream_class
)
422 /* Allow a NULL stream_class to unset the current stream_class */
423 if (stream_class
&& event_class
->stream_class
) {
428 event_class
->stream_class
= stream_class
;
430 * We don't get() the stream_class since doing so would introduce
431 * a circular ownership between event classes and stream classes.
433 * A stream class will always unset itself from its events before
434 * being destroyed. This ensures that a user won't get a pointer
435 * to a stale stream class instance from an event class.
442 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
443 struct metadata_context
*context
)
450 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
456 context
->current_indentation_level
= 1;
457 g_string_assign(context
->field_name
, "");
458 g_string_append_printf(context
->string
, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
459 g_quark_to_string(event_class
->name
),
463 if (event_class
->context
) {
464 g_string_append(context
->string
, "\tcontext := ");
465 ret
= bt_ctf_field_type_serialize(event_class
->context
,
470 g_string_append(context
->string
, ";\n");
473 if (event_class
->fields
) {
474 g_string_append(context
->string
, "\tfields := ");
475 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
479 g_string_append(context
->string
, ";\n");
482 g_string_append(context
->string
, "};\n\n");
484 context
->current_indentation_level
= 0;
489 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
491 /* Make sure each field's payload has been set */
495 ret
= bt_ctf_field_validate(event
->fields_payload
);
500 if (event
->event_class
->context
) {
501 ret
= bt_ctf_field_validate(event
->context_payload
);
508 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
509 struct ctf_stream_pos
*pos
)
515 if (event
->context_payload
) {
516 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
522 if (event
->fields_payload
) {
523 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
533 int bt_ctf_event_set_timestamp(struct bt_ctf_event
*event
,
539 if (event
->timestamp
) {
544 event
->timestamp
= timestamp
;
550 uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event
*event
)
553 return event
->timestamp
;