2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "EVENT-CLASS"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/clock-snapshot-internal.h>
29 #include <babeltrace/trace-ir/field-internal.h>
30 #include <babeltrace/trace-ir/field-class.h>
31 #include <babeltrace/trace-ir/field-class-internal.h>
32 #include <babeltrace/trace-ir/event-class.h>
33 #include <babeltrace/trace-ir/event-class-const.h>
34 #include <babeltrace/trace-ir/event-class-internal.h>
35 #include <babeltrace/trace-ir/event-internal.h>
36 #include <babeltrace/trace-ir/stream-class.h>
37 #include <babeltrace/trace-ir/stream-class-internal.h>
38 #include <babeltrace/trace-ir/trace-internal.h>
39 #include <babeltrace/trace-ir/utils-internal.h>
40 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
41 #include <babeltrace/trace-ir/attributes-internal.h>
42 #include <babeltrace/compiler-internal.h>
43 #include <babeltrace/endian-internal.h>
44 #include <babeltrace/types.h>
45 #include <babeltrace/value-internal.h>
46 #include <babeltrace/assert-internal.h>
50 #define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \
51 BT_ASSERT_PRE_HOT(((const struct bt_event_class *) (_ec)), \
52 "Event class", ": %!+E", (_ec))
55 void destroy_event_class(struct bt_object
*obj
)
57 struct bt_event_class
*event_class
= (void *) obj
;
59 BT_LIB_LOGD("Destroying event class: %!+E", event_class
);
61 if (event_class
->name
.str
) {
62 g_string_free(event_class
->name
.str
, TRUE
);
63 event_class
->name
.str
= NULL
;
66 if (event_class
->emf_uri
.str
) {
67 g_string_free(event_class
->emf_uri
.str
, TRUE
);
68 event_class
->emf_uri
.str
= NULL
;
71 BT_LOGD_STR("Putting context field class.");
72 BT_OBJECT_PUT_REF_AND_RESET(event_class
->specific_context_fc
);
73 BT_LOGD_STR("Putting payload field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(event_class
->payload_fc
);
75 bt_object_pool_finalize(&event_class
->event_pool
);
80 void free_event(struct bt_event
*event
,
81 struct bt_event_class
*event_class
)
83 bt_event_destroy(event
);
88 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
92 bool is_unique
= true;
94 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
95 const struct bt_event_class
*ec
=
96 stream_class
->event_classes
->pdata
[i
];
109 struct bt_event_class
*create_event_class_with_id(
110 struct bt_stream_class
*stream_class
, uint64_t id
)
113 struct bt_event_class
*event_class
;
115 BT_ASSERT(stream_class
);
116 BT_ASSERT_PRE(event_class_id_is_unique(stream_class
, id
),
117 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64
,
119 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
121 event_class
= g_new0(struct bt_event_class
, 1);
123 BT_LOGE_STR("Failed to allocate one event class.");
127 bt_object_init_shared_with_parent(&event_class
->base
,
128 destroy_event_class
);
129 event_class
->id
= id
;
130 bt_property_uint_init(&event_class
->log_level
,
131 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
132 event_class
->name
.str
= g_string_new(NULL
);
133 if (!event_class
->name
.str
) {
134 BT_LOGE_STR("Failed to allocate a GString.");
139 event_class
->emf_uri
.str
= g_string_new(NULL
);
140 if (!event_class
->emf_uri
.str
) {
141 BT_LOGE_STR("Failed to allocate a GString.");
146 ret
= bt_object_pool_initialize(&event_class
->event_pool
,
147 (bt_object_pool_new_object_func
) bt_event_new
,
148 (bt_object_pool_destroy_object_func
) free_event
,
151 BT_LOGE("Failed to initialize event pool: ret=%d",
156 bt_object_set_parent(&event_class
->base
, &stream_class
->base
);
157 g_ptr_array_add(stream_class
->event_classes
, event_class
);
158 bt_stream_class_freeze(stream_class
);
159 BT_LIB_LOGD("Created event class object: %!+E", event_class
);
163 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
169 struct bt_event_class
*bt_event_class_create(
170 struct bt_stream_class
*stream_class
)
172 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
173 BT_ASSERT_PRE(stream_class
->assigns_automatic_event_class_id
,
174 "Stream class does not automatically assigns event class IDs: "
175 "%![sc-]+S", stream_class
);
176 return create_event_class_with_id(stream_class
,
177 (uint64_t) stream_class
->event_classes
->len
);
180 struct bt_event_class
*bt_event_class_create_with_id(
181 struct bt_stream_class
*stream_class
, uint64_t id
)
183 BT_ASSERT_PRE(!stream_class
->assigns_automatic_event_class_id
,
184 "Stream class automatically assigns event class IDs: "
185 "%![sc-]+S", stream_class
);
186 return create_event_class_with_id(stream_class
, id
);
189 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
191 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
192 return event_class
->name
.value
;
195 enum bt_event_class_status
bt_event_class_set_name(
196 struct bt_event_class
*event_class
, const char *name
)
198 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
199 BT_ASSERT_PRE_NON_NULL(name
, "Name");
200 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class
);
201 g_string_assign(event_class
->name
.str
, name
);
202 event_class
->name
.value
= event_class
->name
.str
->str
;
203 BT_LIB_LOGV("Set event class's name: %!+E", event_class
);
204 return BT_EVENT_CLASS_STATUS_OK
;
207 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
209 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
210 return event_class
->id
;
213 enum bt_property_availability
bt_event_class_get_log_level(
214 const struct bt_event_class
*event_class
,
215 enum bt_event_class_log_level
*log_level
)
217 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
218 BT_ASSERT_PRE_NON_NULL(log_level
, "Log level (output)");
219 *log_level
= (enum bt_event_class_log_level
)
220 event_class
->log_level
.value
;
221 return event_class
->log_level
.base
.avail
;
224 void bt_event_class_set_log_level(
225 struct bt_event_class
*event_class
,
226 enum bt_event_class_log_level log_level
)
228 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
229 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class
);
230 bt_property_uint_set(&event_class
->log_level
,
231 (uint64_t) log_level
);
232 BT_LIB_LOGV("Set event class's log level: %!+E", event_class
);
235 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
237 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
238 return event_class
->emf_uri
.value
;
241 enum bt_event_class_status
bt_event_class_set_emf_uri(
242 struct bt_event_class
*event_class
,
245 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
246 BT_ASSERT_PRE_NON_NULL(emf_uri
, "EMF URI");
247 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class
);
248 g_string_assign(event_class
->emf_uri
.str
, emf_uri
);
249 event_class
->emf_uri
.value
= event_class
->emf_uri
.str
->str
;
250 BT_LIB_LOGV("Set event class's EMF URI: %!+E", event_class
);
251 return BT_EVENT_CLASS_STATUS_OK
;
254 struct bt_stream_class
*bt_event_class_borrow_stream_class(
255 struct bt_event_class
*event_class
)
257 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
258 return bt_event_class_borrow_stream_class_inline(event_class
);
261 const struct bt_stream_class
*
262 bt_event_class_borrow_stream_class_const(
263 const struct bt_event_class
*event_class
)
265 return bt_event_class_borrow_stream_class((void *) event_class
);
268 const struct bt_field_class
*
269 bt_event_class_borrow_specific_context_field_class_const(
270 const struct bt_event_class
*event_class
)
272 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
273 return event_class
->specific_context_fc
;
276 enum bt_event_class_status
bt_event_class_set_specific_context_field_class(
277 struct bt_event_class
*event_class
,
278 struct bt_field_class
*field_class
)
281 struct bt_stream_class
*stream_class
;
282 struct bt_trace_class
*trace_class
;
283 struct bt_resolve_field_path_context resolve_ctx
= {
284 .packet_header
= NULL
,
285 .packet_context
= NULL
,
286 .event_header
= NULL
,
287 .event_common_context
= NULL
,
288 .event_specific_context
= field_class
,
289 .event_payload
= NULL
,
292 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
293 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
294 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class
);
295 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
296 BT_FIELD_CLASS_TYPE_STRUCTURE
,
297 "Specific context field class is not a structure field class: "
298 "%!+F", field_class
);
299 stream_class
= bt_event_class_borrow_stream_class_inline(
301 trace_class
= bt_stream_class_borrow_trace_class_inline(stream_class
);
302 resolve_ctx
.packet_header
= trace_class
->packet_header_fc
;
303 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
304 resolve_ctx
.event_header
= stream_class
->event_header_fc
;
305 resolve_ctx
.event_common_context
=
306 stream_class
->event_common_context_fc
;
308 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
311 * This is the only reason for which
312 * bt_resolve_field_paths() can fail: anything else
313 * would be because a precondition is not satisfied.
315 ret
= BT_EVENT_CLASS_STATUS_NOMEM
;
319 bt_field_class_make_part_of_trace_class(field_class
);
320 bt_object_put_ref(event_class
->specific_context_fc
);
321 event_class
->specific_context_fc
= field_class
;
322 bt_object_get_no_null_check(event_class
->specific_context_fc
);
323 bt_field_class_freeze(field_class
);
324 BT_LIB_LOGV("Set event class's specific context field class: %!+E",
331 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
332 const struct bt_event_class
*event_class
)
334 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
335 return event_class
->payload_fc
;
338 enum bt_event_class_status
bt_event_class_set_payload_field_class(
339 struct bt_event_class
*event_class
,
340 struct bt_field_class
*field_class
)
343 struct bt_stream_class
*stream_class
;
344 struct bt_trace_class
*trace_class
;
345 struct bt_resolve_field_path_context resolve_ctx
= {
346 .packet_header
= NULL
,
347 .packet_context
= NULL
,
348 .event_header
= NULL
,
349 .event_common_context
= NULL
,
350 .event_specific_context
= NULL
,
351 .event_payload
= field_class
,
354 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
355 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
356 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class
);
357 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
358 BT_FIELD_CLASS_TYPE_STRUCTURE
,
359 "Payload field class is not a structure field class: %!+F",
361 stream_class
= bt_event_class_borrow_stream_class_inline(
363 trace_class
= bt_stream_class_borrow_trace_class_inline(stream_class
);
364 resolve_ctx
.packet_header
= trace_class
->packet_header_fc
;
365 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
366 resolve_ctx
.event_header
= stream_class
->event_header_fc
;
367 resolve_ctx
.event_common_context
=
368 stream_class
->event_common_context_fc
;
369 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
371 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
374 * This is the only reason for which
375 * bt_resolve_field_paths() can fail: anything else
376 * would be because a precondition is not satisfied.
378 ret
= BT_EVENT_CLASS_STATUS_NOMEM
;
382 bt_field_class_make_part_of_trace_class(field_class
);
383 bt_object_put_ref(event_class
->payload_fc
);
384 event_class
->payload_fc
= field_class
;
385 bt_object_get_no_null_check(event_class
->payload_fc
);
386 bt_field_class_freeze(field_class
);
387 BT_LIB_LOGV("Set event class's payload field class: %!+E", event_class
);
394 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
396 /* The field classes are already frozen */
397 BT_ASSERT(event_class
);
398 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
399 ((struct bt_event_class
*) event_class
)->frozen
= true;
402 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
404 bt_object_get_ref(event_class
);
407 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
409 bt_object_put_ref(event_class
);