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