lib: metadata: add missing non-const borrowing accessors
[deliverable/babeltrace.git] / lib / trace-ir / event-class.c
CommitLineData
272df73e 1/*
0554db57 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
272df73e
PP
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
272df73e
PP
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
f8b979f9
PP
24#define BT_LOG_TAG "EVENT-CLASS"
25#include <babeltrace/lib-logging-internal.h>
26
3dca2276 27#include <babeltrace/assert-pre-internal.h>
b1824349 28#include <babeltrace/trace-ir/clock-snapshot-internal.h>
c319d0ae
PP
29#include <babeltrace/trace-ir/field-internal.h>
30#include <babeltrace/trace-ir/field-class.h>
31#include <babeltrace/trace-ir/field-class-internal.h>
577dc45a 32#include <babeltrace/trace-ir/event-class.h>
dadf29b9 33#include <babeltrace/trace-ir/event-class-const.h>
577dc45a
PP
34#include <babeltrace/trace-ir/event-class-internal.h>
35#include <babeltrace/trace-ir/event-internal.h>
36#include <babeltrace/trace-ir/stream-class.h>
37#include <babeltrace/trace-ir/stream-class-internal.h>
38#include <babeltrace/trace-ir/trace-internal.h>
39#include <babeltrace/trace-ir/utils-internal.h>
40#include <babeltrace/trace-ir/resolve-field-path-internal.h>
577dc45a 41#include <babeltrace/trace-ir/attributes-internal.h>
3d9990ac
PP
42#include <babeltrace/compiler-internal.h>
43#include <babeltrace/endian-internal.h>
c55a9f58 44#include <babeltrace/types.h>
c319d0ae 45#include <babeltrace/value-internal.h>
f6ccaed9 46#include <babeltrace/assert-internal.h>
dc3fffef 47#include <inttypes.h>
0fbb9a9f 48#include <stdlib.h>
272df73e 49
61f59358 50#define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \
dadf29b9 51 BT_ASSERT_PRE_HOT(((const struct bt_event_class *) (_ec)), \
351946b4 52 "Event class", ": %!+E", (_ec))
61f59358
PP
53
54static
55void destroy_event_class(struct bt_object *obj)
272df73e 56{
61f59358 57 struct bt_event_class *event_class = (void *) obj;
272df73e 58
61f59358 59 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
f8b979f9 60
61f59358
PP
61 if (event_class->name.str) {
62 g_string_free(event_class->name.str, TRUE);
53b97e38 63 event_class->name.str = NULL;
cf76ce92
PP
64 }
65
61f59358
PP
66 if (event_class->emf_uri.str) {
67 g_string_free(event_class->emf_uri.str, TRUE);
53b97e38 68 event_class->emf_uri.str = NULL;
272df73e
PP
69 }
70
82823b42 71 BT_LOGD_STR("Putting context field class.");
53b97e38 72 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
82823b42 73 BT_LOGD_STR("Putting payload field class.");
53b97e38 74 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
312c056a 75 bt_object_pool_finalize(&event_class->event_pool);
3dca2276 76 g_free(obj);
272df73e
PP
77}
78
312c056a
PP
79static
80void free_event(struct bt_event *event,
81 struct bt_event_class *event_class)
82{
83 bt_event_destroy(event);
84}
85
61f59358
PP
86BT_ASSERT_PRE_FUNC
87static
dadf29b9
PP
88bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
89 uint64_t id)
272df73e 90{
61f59358
PP
91 uint64_t i;
92 bool is_unique = true;
272df73e 93
61f59358 94 for (i = 0; i < stream_class->event_classes->len; i++) {
dadf29b9 95 const struct bt_event_class *ec =
61f59358
PP
96 stream_class->event_classes->pdata[i];
97
98 if (ec->id == id) {
99 is_unique = false;
100 goto end;
101 }
cf76ce92 102 }
272df73e 103
61f59358
PP
104end:
105 return is_unique;
106}
107
108static
109struct bt_event_class *create_event_class_with_id(
110 struct bt_stream_class *stream_class, uint64_t id)
111{
112 int ret;
113 struct bt_event_class *event_class;
114
115 BT_ASSERT(stream_class);
116 BT_ASSERT_PRE(event_class_id_is_unique(stream_class, id),
117 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64,
118 stream_class, id);
119 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64,
120 stream_class, id);
3dca2276 121 event_class = g_new0(struct bt_event_class, 1);
272df73e 122 if (!event_class) {
3dca2276
PP
123 BT_LOGE_STR("Failed to allocate one event class.");
124 goto error;
272df73e
PP
125 }
126
61f59358
PP
127 bt_object_init_shared_with_parent(&event_class->base,
128 destroy_event_class);
129 event_class->id = id;
130 bt_property_uint_init(&event_class->log_level,
131 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
132 event_class->name.str = g_string_new(NULL);
133 if (!event_class->name.str) {
134 BT_LOGE_STR("Failed to allocate a GString.");
135 ret = -1;
136 goto end;
137 }
138
139 event_class->emf_uri.str = g_string_new(NULL);
140 if (!event_class->emf_uri.str) {
141 BT_LOGE_STR("Failed to allocate a GString.");
142 ret = -1;
143 goto end;
272df73e
PP
144 }
145
312c056a
PP
146 ret = bt_object_pool_initialize(&event_class->event_pool,
147 (bt_object_pool_new_object_func) bt_event_new,
148 (bt_object_pool_destroy_object_func) free_event,
149 event_class);
150 if (ret) {
151 BT_LOGE("Failed to initialize event pool: ret=%d",
152 ret);
153 goto error;
154 }
155
61f59358
PP
156 bt_object_set_parent(&event_class->base, &stream_class->base);
157 g_ptr_array_add(stream_class->event_classes, event_class);
158 bt_stream_class_freeze(stream_class);
159 BT_LIB_LOGD("Created event class object: %!+E", event_class);
3dca2276 160 goto end;
272df73e 161
3dca2276 162error:
e2317362 163 BT_OBJECT_PUT_REF_AND_RESET(event_class);
272df73e
PP
164
165end:
3dca2276 166 return event_class;
272df73e
PP
167}
168
dadf29b9
PP
169struct bt_event_class *bt_event_class_create(
170 struct bt_stream_class *stream_class)
61f59358
PP
171{
172 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
173 BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id,
174 "Stream class does not automatically assigns event class IDs: "
175 "%![sc-]+S", stream_class);
dadf29b9 176 return create_event_class_with_id(stream_class,
61f59358
PP
177 (uint64_t) stream_class->event_classes->len);
178}
179
dadf29b9
PP
180struct bt_event_class *bt_event_class_create_with_id(
181 struct bt_stream_class *stream_class, uint64_t id)
61f59358
PP
182{
183 BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id,
184 "Stream class automatically assigns event class IDs: "
185 "%![sc-]+S", stream_class);
dadf29b9 186 return create_event_class_with_id(stream_class, id);
61f59358
PP
187}
188
dadf29b9 189const char *bt_event_class_get_name(const struct bt_event_class *event_class)
272df73e 190{
cb6f1f7d 191 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
61f59358 192 return event_class->name.value;
272df73e
PP
193}
194
649ae362
PP
195enum bt_event_class_status bt_event_class_set_name(
196 struct bt_event_class *event_class, const char *name)
272df73e 197{
cb6f1f7d 198 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
61f59358
PP
199 BT_ASSERT_PRE_NON_NULL(name, "Name");
200 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
201 g_string_assign(event_class->name.str, name);
202 event_class->name.value = event_class->name.str->str;
203 BT_LIB_LOGV("Set event class's name: %!+E", event_class);
649ae362 204 return BT_EVENT_CLASS_STATUS_OK;
272df73e
PP
205}
206
dadf29b9 207uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
272df73e 208{
61f59358
PP
209 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
210 return event_class->id;
272df73e
PP
211}
212
61f59358 213enum bt_property_availability bt_event_class_get_log_level(
dadf29b9 214 const struct bt_event_class *event_class,
61f59358 215 enum bt_event_class_log_level *log_level)
272df73e 216{
cb6f1f7d 217 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
61f59358
PP
218 BT_ASSERT_PRE_NON_NULL(log_level, "Log level (output)");
219 *log_level = (enum bt_event_class_log_level)
220 event_class->log_level.value;
221 return event_class->log_level.base.avail;
272df73e
PP
222}
223
dadf29b9
PP
224void bt_event_class_set_log_level(
225 struct bt_event_class *event_class,
3dca2276 226 enum bt_event_class_log_level log_level)
272df73e 227{
61f59358
PP
228 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
229 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
230 bt_property_uint_set(&event_class->log_level,
231 (uint64_t) log_level);
232 BT_LIB_LOGV("Set event class's log level: %!+E", event_class);
272df73e
PP
233}
234
dadf29b9 235const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
272df73e 236{
cb6f1f7d 237 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
61f59358 238 return event_class->emf_uri.value;
272df73e
PP
239}
240
649ae362 241enum bt_event_class_status bt_event_class_set_emf_uri(
dadf29b9 242 struct bt_event_class *event_class,
3dca2276 243 const char *emf_uri)
272df73e 244{
61f59358
PP
245 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
246 BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI");
247 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
248 g_string_assign(event_class->emf_uri.str, emf_uri);
249 event_class->emf_uri.value = event_class->emf_uri.str->str;
250 BT_LIB_LOGV("Set event class's EMF URI: %!+E", event_class);
649ae362 251 return BT_EVENT_CLASS_STATUS_OK;
272df73e
PP
252}
253
094ff7c0 254struct bt_stream_class *bt_event_class_borrow_stream_class(
50842bdc 255 struct bt_event_class *event_class)
272df73e 256{
d975f66c 257 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
61f59358 258 return bt_event_class_borrow_stream_class_inline(event_class);
272df73e
PP
259}
260
dadf29b9
PP
261const struct bt_stream_class *
262bt_event_class_borrow_stream_class_const(
263 const struct bt_event_class *event_class)
351946b4 264{
dadf29b9 265 return bt_event_class_borrow_stream_class((void *) event_class);
351946b4
PP
266}
267
dadf29b9
PP
268const struct bt_field_class *
269bt_event_class_borrow_specific_context_field_class_const(
270 const struct bt_event_class *event_class)
272df73e 271{
cb6f1f7d 272 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
c82deff2 273 return event_class->specific_context_fc;
272df73e
PP
274}
275
8f16d5b7
PP
276struct bt_field_class *
277bt_event_class_borrow_specific_context_field_class(
278 struct bt_event_class *event_class)
279{
280 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
281 return event_class->specific_context_fc;
282}
283
649ae362 284enum bt_event_class_status bt_event_class_set_specific_context_field_class(
dadf29b9
PP
285 struct bt_event_class *event_class,
286 struct bt_field_class *field_class)
272df73e 287{
61f59358
PP
288 int ret;
289 struct bt_stream_class *stream_class;
61f59358 290 struct bt_resolve_field_path_context resolve_ctx = {
61f59358 291 .packet_context = NULL,
61f59358 292 .event_common_context = NULL,
c82deff2 293 .event_specific_context = field_class,
61f59358
PP
294 .event_payload = NULL,
295 };
cb6f1f7d 296
61f59358 297 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
c82deff2 298 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
61f59358 299 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
63749226
PP
300 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
301 BT_FIELD_CLASS_TYPE_STRUCTURE,
82823b42 302 "Specific context field class is not a structure field class: "
c82deff2 303 "%!+F", field_class);
61f59358
PP
304 stream_class = bt_event_class_borrow_stream_class_inline(
305 event_class);
c82deff2 306 resolve_ctx.packet_context = stream_class->packet_context_fc;
61f59358 307 resolve_ctx.event_common_context =
c82deff2 308 stream_class->event_common_context_fc;
61f59358 309
c82deff2 310 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
61f59358 311 if (ret) {
649ae362
PP
312 /*
313 * This is the only reason for which
314 * bt_resolve_field_paths() can fail: anything else
315 * would be because a precondition is not satisfied.
316 */
317 ret = BT_EVENT_CLASS_STATUS_NOMEM;
cb6f1f7d
PP
318 goto end;
319 }
320
3b4c8c43 321 bt_field_class_make_part_of_trace_class(field_class);
e2317362 322 bt_object_put_ref(event_class->specific_context_fc);
5e8988cb
PP
323 event_class->specific_context_fc = field_class;
324 bt_object_get_no_null_check(event_class->specific_context_fc);
c82deff2 325 bt_field_class_freeze(field_class);
82823b42 326 BT_LIB_LOGV("Set event class's specific context field class: %!+E",
61f59358 327 event_class);
cb6f1f7d 328
cb6f1f7d
PP
329end:
330 return ret;
272df73e
PP
331}
332
dadf29b9
PP
333const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
334 const struct bt_event_class *event_class)
272df73e 335{
cb6f1f7d 336 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
c82deff2 337 return event_class->payload_fc;
272df73e
PP
338}
339
8f16d5b7
PP
340struct bt_field_class *bt_event_class_borrow_payload_field_class(
341 struct bt_event_class *event_class)
342{
343 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
344 return event_class->payload_fc;
345}
346
649ae362 347enum bt_event_class_status bt_event_class_set_payload_field_class(
dadf29b9
PP
348 struct bt_event_class *event_class,
349 struct bt_field_class *field_class)
272df73e 350{
61f59358
PP
351 int ret;
352 struct bt_stream_class *stream_class;
61f59358 353 struct bt_resolve_field_path_context resolve_ctx = {
61f59358 354 .packet_context = NULL,
61f59358
PP
355 .event_common_context = NULL,
356 .event_specific_context = NULL,
c82deff2 357 .event_payload = field_class,
61f59358 358 };
cb6f1f7d 359
61f59358 360 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
c82deff2 361 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
61f59358 362 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
63749226
PP
363 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
364 BT_FIELD_CLASS_TYPE_STRUCTURE,
82823b42 365 "Payload field class is not a structure field class: %!+F",
c82deff2 366 field_class);
61f59358
PP
367 stream_class = bt_event_class_borrow_stream_class_inline(
368 event_class);
c82deff2 369 resolve_ctx.packet_context = stream_class->packet_context_fc;
61f59358 370 resolve_ctx.event_common_context =
c82deff2
PP
371 stream_class->event_common_context_fc;
372 resolve_ctx.event_specific_context = event_class->specific_context_fc;
61f59358 373
c82deff2 374 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
61f59358 375 if (ret) {
649ae362
PP
376 /*
377 * This is the only reason for which
378 * bt_resolve_field_paths() can fail: anything else
379 * would be because a precondition is not satisfied.
380 */
381 ret = BT_EVENT_CLASS_STATUS_NOMEM;
cb6f1f7d
PP
382 goto end;
383 }
384
3b4c8c43 385 bt_field_class_make_part_of_trace_class(field_class);
e2317362 386 bt_object_put_ref(event_class->payload_fc);
5e8988cb
PP
387 event_class->payload_fc = field_class;
388 bt_object_get_no_null_check(event_class->payload_fc);
c82deff2 389 bt_field_class_freeze(field_class);
82823b42 390 BT_LIB_LOGV("Set event class's payload field class: %!+E", event_class);
cb6f1f7d
PP
391
392end:
393 return ret;
272df73e
PP
394}
395
396BT_HIDDEN
dadf29b9 397void _bt_event_class_freeze(const struct bt_event_class *event_class)
2a3ced3c 398{
c82deff2 399 /* The field classes are already frozen */
f6ccaed9 400 BT_ASSERT(event_class);
61f59358 401 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
dadf29b9 402 ((struct bt_event_class *) event_class)->frozen = true;
2a3ced3c 403}
317e35ec
PP
404
405void bt_event_class_get_ref(const struct bt_event_class *event_class)
406{
407 bt_object_get_ref(event_class);
408}
409
410void bt_event_class_put_ref(const struct bt_event_class *event_class)
411{
412 bt_object_put_ref(event_class);
413}
This page took 0.080064 seconds and 5 git commands to generate.