lib: assign a unique ID to each pre/postcond. and report it on failure
[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 struct bt_event_class *bt_event_class_create(
164 struct bt_stream_class *stream_class)
165 {
166 BT_ASSERT_PRE_NO_ERROR();
167 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
168 BT_ASSERT_PRE(
169 "stream-class-automatically-assigns-event-class-ids",
170 stream_class->assigns_automatic_event_class_id,
171 "Stream class does not automatically assigns event class IDs: "
172 "%![sc-]+S", stream_class);
173 return create_event_class_with_id(stream_class,
174 (uint64_t) stream_class->event_classes->len);
175 }
176
177 struct bt_event_class *bt_event_class_create_with_id(
178 struct bt_stream_class *stream_class, uint64_t id)
179 {
180 BT_ASSERT_PRE_NO_ERROR();
181 BT_ASSERT_PRE(
182 "stream-class-does-not-automatically-assigns-event-class-ids",
183 !stream_class->assigns_automatic_event_class_id,
184 "Stream class automatically assigns event class IDs: "
185 "%![sc-]+S", stream_class);
186 return create_event_class_with_id(stream_class, id);
187 }
188
189 const char *bt_event_class_get_name(const struct bt_event_class *event_class)
190 {
191 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
192 return event_class->name.value;
193 }
194
195 enum bt_event_class_set_name_status bt_event_class_set_name(
196 struct bt_event_class *event_class, const char *name)
197 {
198 BT_ASSERT_PRE_NO_ERROR();
199 BT_ASSERT_PRE_EC_NON_NULL(event_class);
200 BT_ASSERT_PRE_NAME_NON_NULL(name);
201 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
202 g_string_assign(event_class->name.str, name);
203 event_class->name.value = event_class->name.str->str;
204 BT_LIB_LOGD("Set event class's name: %!+E", event_class);
205 return BT_FUNC_STATUS_OK;
206 }
207
208 uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
209 {
210 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
211 return event_class->id;
212 }
213
214 enum bt_property_availability bt_event_class_get_log_level(
215 const struct bt_event_class *event_class,
216 enum bt_event_class_log_level *log_level)
217 {
218 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
219 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level,
220 "Log level (output)");
221 *log_level = (enum bt_event_class_log_level)
222 event_class->log_level.value;
223 return event_class->log_level.base.avail;
224 }
225
226 void bt_event_class_set_log_level(
227 struct bt_event_class *event_class,
228 enum bt_event_class_log_level log_level)
229 {
230 BT_ASSERT_PRE_EC_NON_NULL(event_class);
231 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
232 bt_property_uint_set(&event_class->log_level,
233 (uint64_t) log_level);
234 BT_LIB_LOGD("Set event class's log level: %!+E", event_class);
235 }
236
237 const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
238 {
239 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
240 return event_class->emf_uri.value;
241 }
242
243 enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri(
244 struct bt_event_class *event_class,
245 const char *emf_uri)
246 {
247 BT_ASSERT_PRE_NO_ERROR();
248 BT_ASSERT_PRE_EC_NON_NULL(event_class);
249 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri, "EMF URI");
250 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
251 g_string_assign(event_class->emf_uri.str, emf_uri);
252 event_class->emf_uri.value = event_class->emf_uri.str->str;
253 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class);
254 return BT_FUNC_STATUS_OK;
255 }
256
257 struct bt_stream_class *bt_event_class_borrow_stream_class(
258 struct bt_event_class *event_class)
259 {
260 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
261 return bt_event_class_borrow_stream_class_inline(event_class);
262 }
263
264 const struct bt_stream_class *
265 bt_event_class_borrow_stream_class_const(
266 const struct bt_event_class *event_class)
267 {
268 return bt_event_class_borrow_stream_class((void *) event_class);
269 }
270
271 const struct bt_field_class *
272 bt_event_class_borrow_specific_context_field_class_const(
273 const struct bt_event_class *event_class)
274 {
275 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
276 return event_class->specific_context_fc;
277 }
278
279 struct bt_field_class *
280 bt_event_class_borrow_specific_context_field_class(
281 struct bt_event_class *event_class)
282 {
283 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
284 return event_class->specific_context_fc;
285 }
286
287 enum bt_event_class_set_field_class_status
288 bt_event_class_set_specific_context_field_class(
289 struct bt_event_class *event_class,
290 struct bt_field_class *field_class)
291 {
292 int ret;
293 struct bt_stream_class *stream_class;
294 struct bt_resolve_field_path_context resolve_ctx = {
295 .packet_context = NULL,
296 .event_common_context = NULL,
297 .event_specific_context = field_class,
298 .event_payload = NULL,
299 };
300
301 BT_ASSERT_PRE_NO_ERROR();
302 BT_ASSERT_PRE_EC_NON_NULL(event_class);
303 BT_ASSERT_PRE_FC_NON_NULL(field_class);
304 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
305 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class,
306 "Specific context field class");
307 stream_class = bt_event_class_borrow_stream_class_inline(
308 event_class);
309 resolve_ctx.packet_context = stream_class->packet_context_fc;
310 resolve_ctx.event_common_context =
311 stream_class->event_common_context_fc;
312
313 ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
314 if (ret) {
315 /*
316 * This is the only reason for which
317 * bt_resolve_field_paths() can fail: anything else
318 * would be because a precondition is not satisfied.
319 */
320 ret = BT_FUNC_STATUS_MEMORY_ERROR;
321 goto end;
322 }
323
324 bt_field_class_make_part_of_trace_class(field_class);
325 bt_object_put_ref(event_class->specific_context_fc);
326 event_class->specific_context_fc = field_class;
327 bt_object_get_ref_no_null_check(event_class->specific_context_fc);
328 bt_field_class_freeze(field_class);
329 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
330 event_class);
331
332 end:
333 return ret;
334 }
335
336 const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
337 const struct bt_event_class *event_class)
338 {
339 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
340 return event_class->payload_fc;
341 }
342
343 struct bt_field_class *bt_event_class_borrow_payload_field_class(
344 struct bt_event_class *event_class)
345 {
346 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
347 return event_class->payload_fc;
348 }
349
350 enum bt_event_class_set_field_class_status
351 bt_event_class_set_payload_field_class(
352 struct bt_event_class *event_class,
353 struct bt_field_class *field_class)
354 {
355 int ret;
356 struct bt_stream_class *stream_class;
357 struct bt_resolve_field_path_context resolve_ctx = {
358 .packet_context = NULL,
359 .event_common_context = NULL,
360 .event_specific_context = NULL,
361 .event_payload = field_class,
362 };
363
364 BT_ASSERT_PRE_NO_ERROR();
365 BT_ASSERT_PRE_EC_NON_NULL(event_class);
366 BT_ASSERT_PRE_FC_NON_NULL(field_class);
367 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
368 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class, "Payload field class");
369 stream_class = bt_event_class_borrow_stream_class_inline(
370 event_class);
371 resolve_ctx.packet_context = stream_class->packet_context_fc;
372 resolve_ctx.event_common_context =
373 stream_class->event_common_context_fc;
374 resolve_ctx.event_specific_context = event_class->specific_context_fc;
375
376 ret = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
377 if (ret) {
378 /*
379 * This is the only reason for which
380 * bt_resolve_field_paths() can fail: anything else
381 * would be because a precondition is not satisfied.
382 */
383 ret = BT_FUNC_STATUS_MEMORY_ERROR;
384 goto end;
385 }
386
387 bt_field_class_make_part_of_trace_class(field_class);
388 bt_object_put_ref(event_class->payload_fc);
389 event_class->payload_fc = field_class;
390 bt_object_get_ref_no_null_check(event_class->payload_fc);
391 bt_field_class_freeze(field_class);
392 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class);
393
394 end:
395 return ret;
396 }
397
398 BT_HIDDEN
399 void _bt_event_class_freeze(const struct bt_event_class *event_class)
400 {
401 /* The field classes are already frozen */
402 BT_ASSERT(event_class);
403 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
404 event_class->user_attributes);
405 bt_value_freeze(event_class->user_attributes);
406 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
407 ((struct bt_event_class *) event_class)->frozen = true;
408 }
409
410 const struct bt_value *bt_event_class_borrow_user_attributes_const(
411 const struct bt_event_class *event_class)
412 {
413 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
414 return event_class->user_attributes;
415 }
416
417 struct bt_value *bt_event_class_borrow_user_attributes(
418 struct bt_event_class *event_class)
419 {
420 return (void *) bt_event_class_borrow_user_attributes_const(
421 (void *) event_class);
422 }
423
424 void bt_event_class_set_user_attributes(
425 struct bt_event_class *event_class,
426 const struct bt_value *user_attributes)
427 {
428 BT_ASSERT_PRE_EC_NON_NULL(event_class);
429 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
430 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
431 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
432 bt_object_put_ref_no_null_check(event_class->user_attributes);
433 event_class->user_attributes = (void *) user_attributes;
434 bt_object_get_ref_no_null_check(event_class->user_attributes);
435 }
436
437 void bt_event_class_get_ref(const struct bt_event_class *event_class)
438 {
439 bt_object_get_ref(event_class);
440 }
441
442 void bt_event_class_put_ref(const struct bt_event_class *event_class)
443 {
444 bt_object_put_ref(event_class);
445 }
This page took 0.039953 seconds and 4 git commands to generate.