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