lib: make BT_ASSERT_{PRE,POST}() always on; add BT_ASSERT_{PRE,POST}_DEV()
[babeltrace.git] / src / lib / trace-ir / event-class.c
... / ...
CommitLineData
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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
24#define BT_LOG_TAG "LIB/EVENT-CLASS"
25#include "lib/logging.h"
26
27#include "lib/assert-pre.h"
28#include <babeltrace2/trace-ir/field-class.h>
29#include <babeltrace2/trace-ir/event-class.h>
30#include <babeltrace2/trace-ir/event-class-const.h>
31#include <babeltrace2/trace-ir/stream-class.h>
32#include "compat/compiler.h"
33#include "compat/endian.h"
34#include <babeltrace2/types.h>
35#include "lib/value.h"
36#include "common/assert.h"
37#include <inttypes.h>
38#include <stdlib.h>
39
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"
50#include "lib/func-status.h"
51
52#define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
53 BT_ASSERT_PRE_DEV_HOT(((const struct bt_event_class *) (_ec)), \
54 "Event class", ": %!+E", (_ec))
55
56static
57void destroy_event_class(struct bt_object *obj)
58{
59 struct bt_event_class *event_class = (void *) obj;
60
61 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
62
63 if (event_class->name.str) {
64 g_string_free(event_class->name.str, TRUE);
65 event_class->name.str = NULL;
66 }
67
68 if (event_class->emf_uri.str) {
69 g_string_free(event_class->emf_uri.str, TRUE);
70 event_class->emf_uri.str = NULL;
71 }
72
73 BT_LOGD_STR("Putting context field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
75 BT_LOGD_STR("Putting payload field class.");
76 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
77 bt_object_pool_finalize(&event_class->event_pool);
78 g_free(obj);
79}
80
81static
82void free_event(struct bt_event *event,
83 struct bt_event_class *event_class)
84{
85 bt_event_destroy(event);
86}
87
88BT_ASSERT_PRE_FUNC
89static
90bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
91 uint64_t id)
92{
93 uint64_t i;
94 bool is_unique = true;
95
96 for (i = 0; i < stream_class->event_classes->len; i++) {
97 const struct bt_event_class *ec =
98 stream_class->event_classes->pdata[i];
99
100 if (ec->id == id) {
101 is_unique = false;
102 goto end;
103 }
104 }
105
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);
123 event_class = g_new0(struct bt_event_class, 1);
124 if (!event_class) {
125 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
126 goto error;
127 }
128
129 bt_object_init_shared_with_parent(&event_class->base,
130 destroy_event_class);
131 event_class->id = id;
132 bt_property_uint_init(&event_class->log_level,
133 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
134 event_class->name.str = g_string_new(NULL);
135 if (!event_class->name.str) {
136 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
137 ret = -1;
138 goto end;
139 }
140
141 event_class->emf_uri.str = g_string_new(NULL);
142 if (!event_class->emf_uri.str) {
143 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
144 ret = -1;
145 goto end;
146 }
147
148 ret = bt_object_pool_initialize(&event_class->event_pool,
149 (bt_object_pool_new_object_func) bt_event_new,
150 (bt_object_pool_destroy_object_func) free_event,
151 event_class);
152 if (ret) {
153 BT_LIB_LOGE_APPEND_CAUSE(
154 "Failed to initialize event pool: ret=%d",
155 ret);
156 goto error;
157 }
158
159 bt_object_set_parent(&event_class->base, &stream_class->base);
160 g_ptr_array_add(stream_class->event_classes, event_class);
161 bt_stream_class_freeze(stream_class);
162 BT_LIB_LOGD("Created event class object: %!+E", event_class);
163 goto end;
164
165error:
166 BT_OBJECT_PUT_REF_AND_RESET(event_class);
167
168end:
169 return event_class;
170}
171
172struct bt_event_class *bt_event_class_create(
173 struct bt_stream_class *stream_class)
174{
175 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
176 BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id,
177 "Stream class does not automatically assigns event class IDs: "
178 "%![sc-]+S", stream_class);
179 return create_event_class_with_id(stream_class,
180 (uint64_t) stream_class->event_classes->len);
181}
182
183struct bt_event_class *bt_event_class_create_with_id(
184 struct bt_stream_class *stream_class, uint64_t id)
185{
186 BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id,
187 "Stream class automatically assigns event class IDs: "
188 "%![sc-]+S", stream_class);
189 return create_event_class_with_id(stream_class, id);
190}
191
192const char *bt_event_class_get_name(const struct bt_event_class *event_class)
193{
194 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
195 return event_class->name.value;
196}
197
198enum bt_event_class_set_name_status bt_event_class_set_name(
199 struct bt_event_class *event_class, const char *name)
200{
201 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
202 BT_ASSERT_PRE_NON_NULL(name, "Name");
203 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
204 g_string_assign(event_class->name.str, name);
205 event_class->name.value = event_class->name.str->str;
206 BT_LIB_LOGD("Set event class's name: %!+E", event_class);
207 return BT_FUNC_STATUS_OK;
208}
209
210uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
211{
212 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
213 return event_class->id;
214}
215
216enum bt_property_availability bt_event_class_get_log_level(
217 const struct bt_event_class *event_class,
218 enum bt_event_class_log_level *log_level)
219{
220 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
221 BT_ASSERT_PRE_DEV_NON_NULL(log_level, "Log level (output)");
222 *log_level = (enum bt_event_class_log_level)
223 event_class->log_level.value;
224 return event_class->log_level.base.avail;
225}
226
227void bt_event_class_set_log_level(
228 struct bt_event_class *event_class,
229 enum bt_event_class_log_level log_level)
230{
231 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
232 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
233 bt_property_uint_set(&event_class->log_level,
234 (uint64_t) log_level);
235 BT_LIB_LOGD("Set event class's log level: %!+E", event_class);
236}
237
238const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
239{
240 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
241 return event_class->emf_uri.value;
242}
243
244enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri(
245 struct bt_event_class *event_class,
246 const char *emf_uri)
247{
248 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
249 BT_ASSERT_PRE_NON_NULL(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
257struct bt_stream_class *bt_event_class_borrow_stream_class(
258 struct bt_event_class *event_class)
259{
260 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
261 return bt_event_class_borrow_stream_class_inline(event_class);
262}
263
264const struct bt_stream_class *
265bt_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
271const struct bt_field_class *
272bt_event_class_borrow_specific_context_field_class_const(
273 const struct bt_event_class *event_class)
274{
275 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
276 return event_class->specific_context_fc;
277}
278
279struct bt_field_class *
280bt_event_class_borrow_specific_context_field_class(
281 struct bt_event_class *event_class)
282{
283 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
284 return event_class->specific_context_fc;
285}
286
287enum bt_event_class_set_field_class_status
288bt_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_NON_NULL(event_class, "Event class");
302 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
303 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
304 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
305 BT_FIELD_CLASS_TYPE_STRUCTURE,
306 "Specific context field class is not a structure field class: "
307 "%!+F", field_class);
308 stream_class = bt_event_class_borrow_stream_class_inline(
309 event_class);
310 resolve_ctx.packet_context = stream_class->packet_context_fc;
311 resolve_ctx.event_common_context =
312 stream_class->event_common_context_fc;
313
314 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
315 if (ret) {
316 /*
317 * This is the only reason for which
318 * bt_resolve_field_paths() can fail: anything else
319 * would be because a precondition is not satisfied.
320 */
321 ret = BT_FUNC_STATUS_MEMORY_ERROR;
322 goto end;
323 }
324
325 bt_field_class_make_part_of_trace_class(field_class);
326 bt_object_put_ref(event_class->specific_context_fc);
327 event_class->specific_context_fc = field_class;
328 bt_object_get_no_null_check(event_class->specific_context_fc);
329 bt_field_class_freeze(field_class);
330 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
331 event_class);
332
333end:
334 return ret;
335}
336
337const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
338 const struct bt_event_class *event_class)
339{
340 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
341 return event_class->payload_fc;
342}
343
344struct bt_field_class *bt_event_class_borrow_payload_field_class(
345 struct bt_event_class *event_class)
346{
347 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
348 return event_class->payload_fc;
349}
350
351enum bt_event_class_set_field_class_status
352bt_event_class_set_payload_field_class(
353 struct bt_event_class *event_class,
354 struct bt_field_class *field_class)
355{
356 int ret;
357 struct bt_stream_class *stream_class;
358 struct bt_resolve_field_path_context resolve_ctx = {
359 .packet_context = NULL,
360 .event_common_context = NULL,
361 .event_specific_context = NULL,
362 .event_payload = field_class,
363 };
364
365 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
366 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
367 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
368 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
369 BT_FIELD_CLASS_TYPE_STRUCTURE,
370 "Payload field class is not a structure field class: %!+F",
371 field_class);
372 stream_class = bt_event_class_borrow_stream_class_inline(
373 event_class);
374 resolve_ctx.packet_context = stream_class->packet_context_fc;
375 resolve_ctx.event_common_context =
376 stream_class->event_common_context_fc;
377 resolve_ctx.event_specific_context = event_class->specific_context_fc;
378
379 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
380 if (ret) {
381 /*
382 * This is the only reason for which
383 * bt_resolve_field_paths() can fail: anything else
384 * would be because a precondition is not satisfied.
385 */
386 ret = BT_FUNC_STATUS_MEMORY_ERROR;
387 goto end;
388 }
389
390 bt_field_class_make_part_of_trace_class(field_class);
391 bt_object_put_ref(event_class->payload_fc);
392 event_class->payload_fc = field_class;
393 bt_object_get_no_null_check(event_class->payload_fc);
394 bt_field_class_freeze(field_class);
395 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class);
396
397end:
398 return ret;
399}
400
401BT_HIDDEN
402void _bt_event_class_freeze(const struct bt_event_class *event_class)
403{
404 /* The field classes are already frozen */
405 BT_ASSERT(event_class);
406 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
407 ((struct bt_event_class *) event_class)->frozen = true;
408}
409
410void bt_event_class_get_ref(const struct bt_event_class *event_class)
411{
412 bt_object_get_ref(event_class);
413}
414
415void bt_event_class_put_ref(const struct bt_event_class *event_class)
416{
417 bt_object_put_ref(event_class);
418}
This page took 0.025262 seconds and 4 git commands to generate.