Fix: lib: add NULL check for destroy_listeners in destroy_component_class
[babeltrace.git] / src / lib / trace-ir / event-class.c
CommitLineData
272df73e 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
272df73e 5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
272df73e
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/EVENT-CLASS"
c2d9d9cf 9#include "lib/logging.h"
f8b979f9 10
d98421f2 11#include "lib/assert-cond.h"
3fadfbc0 12#include <babeltrace2/trace-ir/field-class.h>
3fadfbc0 13#include <babeltrace2/trace-ir/event-class.h>
3fadfbc0 14#include <babeltrace2/trace-ir/stream-class.h>
578e048b
MJ
15#include "compat/compiler.h"
16#include "compat/endian.h"
3fadfbc0 17#include <babeltrace2/types.h>
578e048b
MJ
18#include "lib/value.h"
19#include "common/assert.h"
dc3fffef 20#include <inttypes.h>
c4f23e30 21#include <stdbool.h>
0fbb9a9f 22#include <stdlib.h>
272df73e 23
578e048b
MJ
24#include "attributes.h"
25#include "clock-snapshot.h"
26#include "event-class.h"
27#include "event.h"
28#include "field-class.h"
29#include "field.h"
30#include "resolve-field-path.h"
31#include "stream-class.h"
32#include "trace.h"
33#include "utils.h"
d24d5663 34#include "lib/func-status.h"
578e048b 35
d5b13b9b 36#define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
1778c2a4
PP
37 BT_ASSERT_PRE_DEV_HOT("event-class", \
38 ((const struct bt_event_class *) (_ec)), \
e5be10ef 39 "Event class", ": %!+E", (_ec))
44c440bc
PP
40
41static
42void destroy_event_class(struct bt_object *obj)
272df73e 43{
44c440bc 44 struct bt_event_class *event_class = (void *) obj;
272df73e 45
44c440bc 46 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
c6962c96 47 BT_OBJECT_PUT_REF_AND_RESET(event_class->user_attributes);
f8b979f9 48
44c440bc
PP
49 if (event_class->name.str) {
50 g_string_free(event_class->name.str, TRUE);
238b7404 51 event_class->name.str = NULL;
cf76ce92
PP
52 }
53
44c440bc
PP
54 if (event_class->emf_uri.str) {
55 g_string_free(event_class->emf_uri.str, TRUE);
238b7404 56 event_class->emf_uri.str = NULL;
272df73e
PP
57 }
58
e6276565 59 BT_LOGD_STR("Putting context field class.");
238b7404 60 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
e6276565 61 BT_LOGD_STR("Putting payload field class.");
238b7404 62 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
312c056a 63 bt_object_pool_finalize(&event_class->event_pool);
3dca2276 64 g_free(obj);
272df73e
PP
65}
66
312c056a
PP
67static
68void free_event(struct bt_event *event,
69 struct bt_event_class *event_class)
70{
71 bt_event_destroy(event);
72}
73
44c440bc 74static
40f4ba76
PP
75bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
76 uint64_t id)
272df73e 77{
44c440bc
PP
78 uint64_t i;
79 bool is_unique = true;
272df73e 80
44c440bc 81 for (i = 0; i < stream_class->event_classes->len; i++) {
40f4ba76 82 const struct bt_event_class *ec =
44c440bc
PP
83 stream_class->event_classes->pdata[i];
84
85 if (ec->id == id) {
86 is_unique = false;
87 goto end;
88 }
cf76ce92 89 }
272df73e 90
44c440bc
PP
91end:
92 return is_unique;
93}
94
95static
96struct bt_event_class *create_event_class_with_id(
97 struct bt_stream_class *stream_class, uint64_t id)
98{
99 int ret;
100 struct bt_event_class *event_class;
101
102 BT_ASSERT(stream_class);
1778c2a4
PP
103 BT_ASSERT_PRE("event-class-id-is-unique",
104 event_class_id_is_unique(stream_class, id),
44c440bc
PP
105 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64,
106 stream_class, id);
107 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64,
108 stream_class, id);
3dca2276 109 event_class = g_new0(struct bt_event_class, 1);
272df73e 110 if (!event_class) {
870631a2 111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
3dca2276 112 goto error;
272df73e
PP
113 }
114
44c440bc
PP
115 bt_object_init_shared_with_parent(&event_class->base,
116 destroy_event_class);
c6962c96
PP
117 event_class->user_attributes = bt_value_map_create();
118 if (!event_class->user_attributes) {
119 BT_LIB_LOGE_APPEND_CAUSE(
120 "Failed to create a map value object.");
121 goto error;
122 }
123
44c440bc
PP
124 event_class->id = id;
125 bt_property_uint_init(&event_class->log_level,
126 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
127 event_class->name.str = g_string_new(NULL);
128 if (!event_class->name.str) {
870631a2 129 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
c6962c96 130 goto error;
44c440bc
PP
131 }
132
133 event_class->emf_uri.str = g_string_new(NULL);
134 if (!event_class->emf_uri.str) {
870631a2 135 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
c6962c96 136 goto error;
272df73e
PP
137 }
138
312c056a
PP
139 ret = bt_object_pool_initialize(&event_class->event_pool,
140 (bt_object_pool_new_object_func) bt_event_new,
141 (bt_object_pool_destroy_object_func) free_event,
142 event_class);
143 if (ret) {
870631a2
PP
144 BT_LIB_LOGE_APPEND_CAUSE(
145 "Failed to initialize event pool: ret=%d",
312c056a
PP
146 ret);
147 goto error;
148 }
149
44c440bc
PP
150 bt_object_set_parent(&event_class->base, &stream_class->base);
151 g_ptr_array_add(stream_class->event_classes, event_class);
152 bt_stream_class_freeze(stream_class);
153 BT_LIB_LOGD("Created event class object: %!+E", event_class);
3dca2276 154 goto end;
272df73e 155
3dca2276 156error:
65300d60 157 BT_OBJECT_PUT_REF_AND_RESET(event_class);
272df73e
PP
158
159end:
3dca2276 160 return event_class;
272df73e
PP
161}
162
40f4ba76
PP
163struct bt_event_class *bt_event_class_create(
164 struct bt_stream_class *stream_class)
44c440bc 165{
17f3083a 166 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 167 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
1778c2a4
PP
168 BT_ASSERT_PRE(
169 "stream-class-automatically-assigns-event-class-ids",
170 stream_class->assigns_automatic_event_class_id,
44c440bc
PP
171 "Stream class does not automatically assigns event class IDs: "
172 "%![sc-]+S", stream_class);
40f4ba76 173 return create_event_class_with_id(stream_class,
44c440bc
PP
174 (uint64_t) stream_class->event_classes->len);
175}
176
40f4ba76
PP
177struct bt_event_class *bt_event_class_create_with_id(
178 struct bt_stream_class *stream_class, uint64_t id)
44c440bc 179{
17f3083a 180 BT_ASSERT_PRE_NO_ERROR();
1778c2a4
PP
181 BT_ASSERT_PRE(
182 "stream-class-does-not-automatically-assigns-event-class-ids",
183 !stream_class->assigns_automatic_event_class_id,
44c440bc
PP
184 "Stream class automatically assigns event class IDs: "
185 "%![sc-]+S", stream_class);
40f4ba76 186 return create_event_class_with_id(stream_class, id);
44c440bc
PP
187}
188
40f4ba76 189const char *bt_event_class_get_name(const struct bt_event_class *event_class)
272df73e 190{
d5b13b9b 191 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
44c440bc 192 return event_class->name.value;
272df73e
PP
193}
194
d24d5663 195enum bt_event_class_set_name_status bt_event_class_set_name(
d08300f5 196 struct bt_event_class *event_class, const char *name)
272df73e 197{
17f3083a 198 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
199 BT_ASSERT_PRE_EC_NON_NULL(event_class);
200 BT_ASSERT_PRE_NAME_NON_NULL(name);
bdb288b3 201 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
44c440bc
PP
202 g_string_assign(event_class->name.str, name);
203 event_class->name.value = event_class->name.str->str;
3f7d4d90 204 BT_LIB_LOGD("Set event class's name: %!+E", event_class);
d24d5663 205 return BT_FUNC_STATUS_OK;
272df73e
PP
206}
207
40f4ba76 208uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
272df73e 209{
d5b13b9b 210 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
44c440bc 211 return event_class->id;
272df73e
PP
212}
213
44c440bc 214enum bt_property_availability bt_event_class_get_log_level(
40f4ba76 215 const struct bt_event_class *event_class,
44c440bc 216 enum bt_event_class_log_level *log_level)
272df73e 217{
d5b13b9b 218 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
1778c2a4
PP
219 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level,
220 "Log level (output)");
44c440bc
PP
221 *log_level = (enum bt_event_class_log_level)
222 event_class->log_level.value;
223 return event_class->log_level.base.avail;
272df73e
PP
224}
225
40f4ba76
PP
226void bt_event_class_set_log_level(
227 struct bt_event_class *event_class,
3dca2276 228 enum bt_event_class_log_level log_level)
272df73e 229{
d5b13b9b 230 BT_ASSERT_PRE_EC_NON_NULL(event_class);
bdb288b3 231 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
44c440bc
PP
232 bt_property_uint_set(&event_class->log_level,
233 (uint64_t) log_level);
3f7d4d90 234 BT_LIB_LOGD("Set event class's log level: %!+E", event_class);
272df73e
PP
235}
236
40f4ba76 237const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
272df73e 238{
d5b13b9b 239 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
44c440bc 240 return event_class->emf_uri.value;
272df73e
PP
241}
242
d24d5663 243enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri(
40f4ba76 244 struct bt_event_class *event_class,
3dca2276 245 const char *emf_uri)
272df73e 246{
17f3083a 247 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 248 BT_ASSERT_PRE_EC_NON_NULL(event_class);
1778c2a4 249 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri, "EMF URI");
bdb288b3 250 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
44c440bc
PP
251 g_string_assign(event_class->emf_uri.str, emf_uri);
252 event_class->emf_uri.value = event_class->emf_uri.str->str;
3f7d4d90 253 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class);
d24d5663 254 return BT_FUNC_STATUS_OK;
272df73e
PP
255}
256
094ff7c0 257struct bt_stream_class *bt_event_class_borrow_stream_class(
50842bdc 258 struct bt_event_class *event_class)
272df73e 259{
d5b13b9b 260 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
44c440bc 261 return bt_event_class_borrow_stream_class_inline(event_class);
272df73e
PP
262}
263
40f4ba76
PP
264const struct bt_stream_class *
265bt_event_class_borrow_stream_class_const(
266 const struct bt_event_class *event_class)
e5be10ef 267{
40f4ba76 268 return bt_event_class_borrow_stream_class((void *) event_class);
e5be10ef
PP
269}
270
40f4ba76
PP
271const struct bt_field_class *
272bt_event_class_borrow_specific_context_field_class_const(
273 const struct bt_event_class *event_class)
272df73e 274{
d5b13b9b 275 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
5cd6d0e5 276 return event_class->specific_context_fc;
272df73e
PP
277}
278
740faaf4
PP
279struct bt_field_class *
280bt_event_class_borrow_specific_context_field_class(
281 struct bt_event_class *event_class)
282{
d5b13b9b 283 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
740faaf4
PP
284 return event_class->specific_context_fc;
285}
286
d24d5663
PP
287enum bt_event_class_set_field_class_status
288bt_event_class_set_specific_context_field_class(
40f4ba76
PP
289 struct bt_event_class *event_class,
290 struct bt_field_class *field_class)
272df73e 291{
44c440bc
PP
292 int ret;
293 struct bt_stream_class *stream_class;
44c440bc 294 struct bt_resolve_field_path_context resolve_ctx = {
44c440bc 295 .packet_context = NULL,
44c440bc 296 .event_common_context = NULL,
5cd6d0e5 297 .event_specific_context = field_class,
44c440bc
PP
298 .event_payload = NULL,
299 };
cb6f1f7d 300
17f3083a 301 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
302 BT_ASSERT_PRE_EC_NON_NULL(event_class);
303 BT_ASSERT_PRE_FC_NON_NULL(field_class);
bdb288b3 304 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
1778c2a4
PP
305 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class,
306 "Specific context field class");
44c440bc
PP
307 stream_class = bt_event_class_borrow_stream_class_inline(
308 event_class);
5cd6d0e5 309 resolve_ctx.packet_context = stream_class->packet_context_fc;
44c440bc 310 resolve_ctx.event_common_context =
5cd6d0e5 311 stream_class->event_common_context_fc;
44c440bc 312
1778c2a4 313 ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
44c440bc 314 if (ret) {
d08300f5
PP
315 /*
316 * This is the only reason for which
317 * bt_resolve_field_paths() can fail: anything else
318 * would be because a precondition is not satisfied.
319 */
d24d5663 320 ret = BT_FUNC_STATUS_MEMORY_ERROR;
cb6f1f7d
PP
321 goto end;
322 }
323
862ca4ed 324 bt_field_class_make_part_of_trace_class(field_class);
65300d60 325 bt_object_put_ref(event_class->specific_context_fc);
398454ed 326 event_class->specific_context_fc = field_class;
6871026b 327 bt_object_get_ref_no_null_check(event_class->specific_context_fc);
5cd6d0e5 328 bt_field_class_freeze(field_class);
3f7d4d90 329 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
44c440bc 330 event_class);
cb6f1f7d 331
cb6f1f7d
PP
332end:
333 return ret;
272df73e
PP
334}
335
40f4ba76
PP
336const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
337 const struct bt_event_class *event_class)
272df73e 338{
d5b13b9b 339 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
5cd6d0e5 340 return event_class->payload_fc;
272df73e
PP
341}
342
740faaf4
PP
343struct bt_field_class *bt_event_class_borrow_payload_field_class(
344 struct bt_event_class *event_class)
345{
d5b13b9b 346 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
740faaf4
PP
347 return event_class->payload_fc;
348}
349
d24d5663
PP
350enum bt_event_class_set_field_class_status
351bt_event_class_set_payload_field_class(
40f4ba76
PP
352 struct bt_event_class *event_class,
353 struct bt_field_class *field_class)
272df73e 354{
44c440bc
PP
355 int ret;
356 struct bt_stream_class *stream_class;
44c440bc 357 struct bt_resolve_field_path_context resolve_ctx = {
44c440bc 358 .packet_context = NULL,
44c440bc
PP
359 .event_common_context = NULL,
360 .event_specific_context = NULL,
5cd6d0e5 361 .event_payload = field_class,
44c440bc 362 };
cb6f1f7d 363
17f3083a 364 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
365 BT_ASSERT_PRE_EC_NON_NULL(event_class);
366 BT_ASSERT_PRE_FC_NON_NULL(field_class);
bdb288b3 367 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
1778c2a4 368 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class, "Payload field class");
44c440bc
PP
369 stream_class = bt_event_class_borrow_stream_class_inline(
370 event_class);
5cd6d0e5 371 resolve_ctx.packet_context = stream_class->packet_context_fc;
44c440bc 372 resolve_ctx.event_common_context =
5cd6d0e5
PP
373 stream_class->event_common_context_fc;
374 resolve_ctx.event_specific_context = event_class->specific_context_fc;
44c440bc 375
1778c2a4 376 ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
44c440bc 377 if (ret) {
d08300f5
PP
378 /*
379 * This is the only reason for which
380 * bt_resolve_field_paths() can fail: anything else
381 * would be because a precondition is not satisfied.
382 */
d24d5663 383 ret = BT_FUNC_STATUS_MEMORY_ERROR;
cb6f1f7d
PP
384 goto end;
385 }
386
862ca4ed 387 bt_field_class_make_part_of_trace_class(field_class);
65300d60 388 bt_object_put_ref(event_class->payload_fc);
398454ed 389 event_class->payload_fc = field_class;
6871026b 390 bt_object_get_ref_no_null_check(event_class->payload_fc);
5cd6d0e5 391 bt_field_class_freeze(field_class);
3f7d4d90 392 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class);
cb6f1f7d
PP
393
394end:
395 return ret;
272df73e
PP
396}
397
398BT_HIDDEN
40f4ba76 399void _bt_event_class_freeze(const struct bt_event_class *event_class)
2a3ced3c 400{
5cd6d0e5 401 /* The field classes are already frozen */
f6ccaed9 402 BT_ASSERT(event_class);
c6962c96
PP
403 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
404 event_class->user_attributes);
405 bt_value_freeze(event_class->user_attributes);
44c440bc 406 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
40f4ba76 407 ((struct bt_event_class *) event_class)->frozen = true;
2a3ced3c 408}
c5b9b441 409
c6962c96
PP
410const struct bt_value *bt_event_class_borrow_user_attributes_const(
411 const struct bt_event_class *event_class)
412{
d5b13b9b 413 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
c6962c96
PP
414 return event_class->user_attributes;
415}
416
417struct bt_value *bt_event_class_borrow_user_attributes(
418 struct bt_event_class *event_class)
419{
420 return (void *) bt_event_class_borrow_user_attributes_const(
421 (void *) event_class);
422}
423
424void bt_event_class_set_user_attributes(
425 struct bt_event_class *event_class,
426 const struct bt_value *user_attributes)
427{
d5b13b9b 428 BT_ASSERT_PRE_EC_NON_NULL(event_class);
c6962c96 429 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
d5b13b9b
PP
430 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
431 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
6871026b 432 bt_object_put_ref_no_null_check(event_class->user_attributes);
c6962c96 433 event_class->user_attributes = (void *) user_attributes;
6871026b 434 bt_object_get_ref_no_null_check(event_class->user_attributes);
c6962c96
PP
435}
436
c5b9b441
PP
437void bt_event_class_get_ref(const struct bt_event_class *event_class)
438{
439 bt_object_get_ref(event_class);
440}
441
442void bt_event_class_put_ref(const struct bt_event_class *event_class)
443{
444 bt_object_put_ref(event_class);
445}
This page took 0.11316 seconds and 4 git commands to generate.