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