Visibility hidden by default
[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 "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"
34 #include "lib/func-status.h"
35
36 #define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
37 BT_ASSERT_PRE_DEV_HOT("event-class", \
38 ((const struct bt_event_class *) (_ec)), \
39 "Event class", ": %!+E", (_ec))
40
41 static
42 void destroy_event_class(struct bt_object *obj)
43 {
44 struct bt_event_class *event_class = (void *) obj;
45
46 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
47 BT_OBJECT_PUT_REF_AND_RESET(event_class->user_attributes);
48
49 if (event_class->name.str) {
50 g_string_free(event_class->name.str, TRUE);
51 event_class->name.str = NULL;
52 }
53
54 if (event_class->emf_uri.str) {
55 g_string_free(event_class->emf_uri.str, TRUE);
56 event_class->emf_uri.str = NULL;
57 }
58
59 BT_LOGD_STR("Putting context field class.");
60 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
61 BT_LOGD_STR("Putting payload field class.");
62 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
63 bt_object_pool_finalize(&event_class->event_pool);
64 g_free(obj);
65 }
66
67 static
68 void free_event(struct bt_event *event,
69 struct bt_event_class *event_class)
70 {
71 bt_event_destroy(event);
72 }
73
74 static
75 bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
76 uint64_t id)
77 {
78 uint64_t i;
79 bool is_unique = true;
80
81 for (i = 0; i < stream_class->event_classes->len; i++) {
82 const struct bt_event_class *ec =
83 stream_class->event_classes->pdata[i];
84
85 if (ec->id == id) {
86 is_unique = false;
87 goto end;
88 }
89 }
90
91 end:
92 return is_unique;
93 }
94
95 static
96 struct 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);
103 BT_ASSERT_PRE("event-class-id-is-unique",
104 event_class_id_is_unique(stream_class, id),
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);
109 event_class = g_new0(struct bt_event_class, 1);
110 if (!event_class) {
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
112 goto error;
113 }
114
115 bt_object_init_shared_with_parent(&event_class->base,
116 destroy_event_class);
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
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) {
129 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
130 goto error;
131 }
132
133 event_class->emf_uri.str = g_string_new(NULL);
134 if (!event_class->emf_uri.str) {
135 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
136 goto error;
137 }
138
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) {
144 BT_LIB_LOGE_APPEND_CAUSE(
145 "Failed to initialize event pool: ret=%d",
146 ret);
147 goto error;
148 }
149
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);
154 goto end;
155
156 error:
157 BT_OBJECT_PUT_REF_AND_RESET(event_class);
158
159 end:
160 return event_class;
161 }
162
163 BT_EXPORT
164 struct bt_event_class *bt_event_class_create(
165 struct bt_stream_class *stream_class)
166 {
167 BT_ASSERT_PRE_NO_ERROR();
168 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
169 BT_ASSERT_PRE(
170 "stream-class-automatically-assigns-event-class-ids",
171 stream_class->assigns_automatic_event_class_id,
172 "Stream class does not automatically assigns event class IDs: "
173 "%![sc-]+S", stream_class);
174 return create_event_class_with_id(stream_class,
175 (uint64_t) stream_class->event_classes->len);
176 }
177
178 BT_EXPORT
179 struct bt_event_class *bt_event_class_create_with_id(
180 struct bt_stream_class *stream_class, uint64_t id)
181 {
182 BT_ASSERT_PRE_NO_ERROR();
183 BT_ASSERT_PRE(
184 "stream-class-does-not-automatically-assigns-event-class-ids",
185 !stream_class->assigns_automatic_event_class_id,
186 "Stream class automatically assigns event class IDs: "
187 "%![sc-]+S", stream_class);
188 return create_event_class_with_id(stream_class, id);
189 }
190
191 BT_EXPORT
192 const char *bt_event_class_get_name(const struct bt_event_class *event_class)
193 {
194 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
195 return event_class->name.value;
196 }
197
198 BT_EXPORT
199 enum bt_event_class_set_name_status bt_event_class_set_name(
200 struct bt_event_class *event_class, const char *name)
201 {
202 BT_ASSERT_PRE_NO_ERROR();
203 BT_ASSERT_PRE_EC_NON_NULL(event_class);
204 BT_ASSERT_PRE_NAME_NON_NULL(name);
205 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
206 g_string_assign(event_class->name.str, name);
207 event_class->name.value = event_class->name.str->str;
208 BT_LIB_LOGD("Set event class's name: %!+E", event_class);
209 return BT_FUNC_STATUS_OK;
210 }
211
212 BT_EXPORT
213 uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
214 {
215 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
216 return event_class->id;
217 }
218
219 BT_EXPORT
220 enum bt_property_availability bt_event_class_get_log_level(
221 const struct bt_event_class *event_class,
222 enum bt_event_class_log_level *log_level)
223 {
224 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
225 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level,
226 "Log level (output)");
227 *log_level = (enum bt_event_class_log_level)
228 event_class->log_level.value;
229 return event_class->log_level.base.avail;
230 }
231
232 BT_EXPORT
233 void bt_event_class_set_log_level(
234 struct bt_event_class *event_class,
235 enum bt_event_class_log_level log_level)
236 {
237 BT_ASSERT_PRE_EC_NON_NULL(event_class);
238 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
239 bt_property_uint_set(&event_class->log_level,
240 (uint64_t) log_level);
241 BT_LIB_LOGD("Set event class's log level: %!+E", event_class);
242 }
243
244 BT_EXPORT
245 const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
246 {
247 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
248 return event_class->emf_uri.value;
249 }
250
251 BT_EXPORT
252 enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri(
253 struct bt_event_class *event_class,
254 const char *emf_uri)
255 {
256 BT_ASSERT_PRE_NO_ERROR();
257 BT_ASSERT_PRE_EC_NON_NULL(event_class);
258 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri, "EMF URI");
259 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
260 g_string_assign(event_class->emf_uri.str, emf_uri);
261 event_class->emf_uri.value = event_class->emf_uri.str->str;
262 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class);
263 return BT_FUNC_STATUS_OK;
264 }
265
266 BT_EXPORT
267 struct bt_stream_class *bt_event_class_borrow_stream_class(
268 struct bt_event_class *event_class)
269 {
270 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
271 return bt_event_class_borrow_stream_class_inline(event_class);
272 }
273
274 BT_EXPORT
275 const struct bt_stream_class *
276 bt_event_class_borrow_stream_class_const(
277 const struct bt_event_class *event_class)
278 {
279 return bt_event_class_borrow_stream_class((void *) event_class);
280 }
281
282 BT_EXPORT
283 const struct bt_field_class *
284 bt_event_class_borrow_specific_context_field_class_const(
285 const struct bt_event_class *event_class)
286 {
287 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
288 return event_class->specific_context_fc;
289 }
290
291 BT_EXPORT
292 struct bt_field_class *
293 bt_event_class_borrow_specific_context_field_class(
294 struct bt_event_class *event_class)
295 {
296 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
297 return event_class->specific_context_fc;
298 }
299
300 BT_EXPORT
301 enum bt_event_class_set_field_class_status
302 bt_event_class_set_specific_context_field_class(
303 struct bt_event_class *event_class,
304 struct bt_field_class *field_class)
305 {
306 int ret;
307 struct bt_stream_class *stream_class;
308 struct bt_resolve_field_path_context resolve_ctx = {
309 .packet_context = NULL,
310 .event_common_context = NULL,
311 .event_specific_context = field_class,
312 .event_payload = NULL,
313 };
314
315 BT_ASSERT_PRE_NO_ERROR();
316 BT_ASSERT_PRE_EC_NON_NULL(event_class);
317 BT_ASSERT_PRE_FC_NON_NULL(field_class);
318 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
319 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class,
320 "Specific context field class");
321 stream_class = bt_event_class_borrow_stream_class_inline(
322 event_class);
323 resolve_ctx.packet_context = stream_class->packet_context_fc;
324 resolve_ctx.event_common_context =
325 stream_class->event_common_context_fc;
326
327 ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
328 if (ret) {
329 /*
330 * This is the only reason for which
331 * bt_resolve_field_paths() can fail: anything else
332 * would be because a precondition is not satisfied.
333 */
334 ret = BT_FUNC_STATUS_MEMORY_ERROR;
335 goto end;
336 }
337
338 bt_field_class_make_part_of_trace_class(field_class);
339 bt_object_put_ref(event_class->specific_context_fc);
340 event_class->specific_context_fc = field_class;
341 bt_object_get_ref_no_null_check(event_class->specific_context_fc);
342 bt_field_class_freeze(field_class);
343 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
344 event_class);
345
346 end:
347 return ret;
348 }
349
350 BT_EXPORT
351 const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
352 const struct bt_event_class *event_class)
353 {
354 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
355 return event_class->payload_fc;
356 }
357
358 BT_EXPORT
359 struct bt_field_class *bt_event_class_borrow_payload_field_class(
360 struct bt_event_class *event_class)
361 {
362 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
363 return event_class->payload_fc;
364 }
365
366 BT_EXPORT
367 enum bt_event_class_set_field_class_status
368 bt_event_class_set_payload_field_class(
369 struct bt_event_class *event_class,
370 struct bt_field_class *field_class)
371 {
372 int ret;
373 struct bt_stream_class *stream_class;
374 struct bt_resolve_field_path_context resolve_ctx = {
375 .packet_context = NULL,
376 .event_common_context = NULL,
377 .event_specific_context = NULL,
378 .event_payload = field_class,
379 };
380
381 BT_ASSERT_PRE_NO_ERROR();
382 BT_ASSERT_PRE_EC_NON_NULL(event_class);
383 BT_ASSERT_PRE_FC_NON_NULL(field_class);
384 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
385 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class, "Payload field class");
386 stream_class = bt_event_class_borrow_stream_class_inline(
387 event_class);
388 resolve_ctx.packet_context = stream_class->packet_context_fc;
389 resolve_ctx.event_common_context =
390 stream_class->event_common_context_fc;
391 resolve_ctx.event_specific_context = event_class->specific_context_fc;
392
393 ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
394 if (ret) {
395 /*
396 * This is the only reason for which
397 * bt_resolve_field_paths() can fail: anything else
398 * would be because a precondition is not satisfied.
399 */
400 ret = BT_FUNC_STATUS_MEMORY_ERROR;
401 goto end;
402 }
403
404 bt_field_class_make_part_of_trace_class(field_class);
405 bt_object_put_ref(event_class->payload_fc);
406 event_class->payload_fc = field_class;
407 bt_object_get_ref_no_null_check(event_class->payload_fc);
408 bt_field_class_freeze(field_class);
409 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class);
410
411 end:
412 return ret;
413 }
414
415 void _bt_event_class_freeze(const struct bt_event_class *event_class)
416 {
417 /* The field classes are already frozen */
418 BT_ASSERT(event_class);
419 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
420 event_class->user_attributes);
421 bt_value_freeze(event_class->user_attributes);
422 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
423 ((struct bt_event_class *) event_class)->frozen = true;
424 }
425
426 BT_EXPORT
427 const struct bt_value *bt_event_class_borrow_user_attributes_const(
428 const struct bt_event_class *event_class)
429 {
430 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
431 return event_class->user_attributes;
432 }
433
434 BT_EXPORT
435 struct bt_value *bt_event_class_borrow_user_attributes(
436 struct bt_event_class *event_class)
437 {
438 return (void *) bt_event_class_borrow_user_attributes_const(
439 (void *) event_class);
440 }
441
442 BT_EXPORT
443 void bt_event_class_set_user_attributes(
444 struct bt_event_class *event_class,
445 const struct bt_value *user_attributes)
446 {
447 BT_ASSERT_PRE_EC_NON_NULL(event_class);
448 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
449 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
450 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
451 bt_object_put_ref_no_null_check(event_class->user_attributes);
452 event_class->user_attributes = (void *) user_attributes;
453 bt_object_get_ref_no_null_check(event_class->user_attributes);
454 }
455
456 BT_EXPORT
457 void bt_event_class_get_ref(const struct bt_event_class *event_class)
458 {
459 bt_object_get_ref(event_class);
460 }
461
462 BT_EXPORT
463 void bt_event_class_put_ref(const struct bt_event_class *event_class)
464 {
465 bt_object_put_ref(event_class);
466 }
This page took 0.039117 seconds and 4 git commands to generate.