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