CTF writer: use own `bt_ctf_object` and `bt_ctf_value` internal APIs
[babeltrace.git] / lib / trace-ir / event-class.c
CommitLineData
272df73e 1/*
272df73e
PP
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
f8b979f9
PP
25#define BT_LOG_TAG "EVENT-CLASS"
26#include <babeltrace/lib-logging-internal.h>
27
8deee039 28#include <babeltrace/assert-pre-internal.h>
108b91d0
PP
29#include <babeltrace/trace-ir/clock-value-internal.h>
30#include <babeltrace/trace-ir/fields-internal.h>
939190b3
PP
31#include <babeltrace/trace-ir/field-classes.h>
32#include <babeltrace/trace-ir/field-classes-internal.h>
9e550e5f 33#include <babeltrace/trace-ir/private-event-class.h>
108b91d0
PP
34#include <babeltrace/trace-ir/event-class.h>
35#include <babeltrace/trace-ir/event-class-internal.h>
36#include <babeltrace/trace-ir/event-internal.h>
9e550e5f 37#include <babeltrace/trace-ir/private-stream-class.h>
108b91d0
PP
38#include <babeltrace/trace-ir/stream-class.h>
39#include <babeltrace/trace-ir/stream-class-internal.h>
40#include <babeltrace/trace-ir/trace-internal.h>
41#include <babeltrace/trace-ir/utils-internal.h>
42#include <babeltrace/trace-ir/resolve-field-path-internal.h>
8138bfe1 43#include <babeltrace/object.h>
108b91d0 44#include <babeltrace/trace-ir/attributes-internal.h>
3d9990ac
PP
45#include <babeltrace/compiler-internal.h>
46#include <babeltrace/endian-internal.h>
c55a9f58 47#include <babeltrace/types.h>
f8b979f9 48#include <babeltrace/values-internal.h>
8b45963b 49#include <babeltrace/assert-internal.h>
dc3fffef 50#include <inttypes.h>
0fbb9a9f 51#include <stdlib.h>
272df73e 52
7b33a0e0 53#define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \
9e550e5f
PP
54 BT_ASSERT_PRE_HOT(((struct bt_event_class *) (_ec)), \
55 "Event class", ": %!+E", (_ec))
7b33a0e0
PP
56
57static
58void destroy_event_class(struct bt_object *obj)
272df73e 59{
7b33a0e0 60 struct bt_event_class *event_class = (void *) obj;
272df73e 61
7b33a0e0 62 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
f8b979f9 63
7b33a0e0
PP
64 if (event_class->name.str) {
65 g_string_free(event_class->name.str, TRUE);
1248f5ea 66 event_class->name.str = NULL;
9cf5d083
PP
67 }
68
7b33a0e0
PP
69 if (event_class->emf_uri.str) {
70 g_string_free(event_class->emf_uri.str, TRUE);
1248f5ea 71 event_class->emf_uri.str = NULL;
272df73e
PP
72 }
73
939190b3 74 BT_LOGD_STR("Putting context field classe.");
1248f5ea 75 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
939190b3 76 BT_LOGD_STR("Putting payload field classe.");
1248f5ea 77 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
a6918753 78 bt_object_pool_finalize(&event_class->event_pool);
8deee039 79 g_free(obj);
272df73e
PP
80}
81
a6918753
PP
82static
83void free_event(struct bt_event *event,
84 struct bt_event_class *event_class)
85{
86 bt_event_destroy(event);
87}
88
7b33a0e0
PP
89BT_ASSERT_PRE_FUNC
90static
91bool event_class_id_is_unique(struct bt_stream_class *stream_class, uint64_t id)
272df73e 92{
7b33a0e0
PP
93 uint64_t i;
94 bool is_unique = true;
272df73e 95
7b33a0e0
PP
96 for (i = 0; i < stream_class->event_classes->len; i++) {
97 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 }
9cf5d083 104 }
272df73e 105
7b33a0e0
PP
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);
8deee039 123 event_class = g_new0(struct bt_event_class, 1);
272df73e 124 if (!event_class) {
8deee039
PP
125 BT_LOGE_STR("Failed to allocate one event class.");
126 goto error;
272df73e
PP
127 }
128
7b33a0e0
PP
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_LOGE_STR("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_LOGE_STR("Failed to allocate a GString.");
144 ret = -1;
145 goto end;
272df73e
PP
146 }
147
a6918753
PP
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_LOGE("Failed to initialize event pool: ret=%d",
154 ret);
155 goto error;
156 }
157
7b33a0e0
PP
158 bt_object_set_parent(&event_class->base, &stream_class->base);
159 g_ptr_array_add(stream_class->event_classes, event_class);
160 bt_stream_class_freeze(stream_class);
161 BT_LIB_LOGD("Created event class object: %!+E", event_class);
8deee039 162 goto end;
272df73e 163
8deee039 164error:
8138bfe1 165 BT_OBJECT_PUT_REF_AND_RESET(event_class);
272df73e
PP
166
167end:
8deee039 168 return event_class;
272df73e
PP
169}
170
9e550e5f
PP
171struct bt_private_event_class *bt_private_event_class_create(
172 struct bt_private_stream_class *priv_stream_class)
7b33a0e0 173{
9e550e5f
PP
174 struct bt_stream_class *stream_class = (void *) priv_stream_class;
175
7b33a0e0
PP
176 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
177 BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id,
178 "Stream class does not automatically assigns event class IDs: "
179 "%![sc-]+S", stream_class);
9e550e5f 180 return (void *) create_event_class_with_id((void *) stream_class,
7b33a0e0
PP
181 (uint64_t) stream_class->event_classes->len);
182}
183
9e550e5f
PP
184struct bt_private_event_class *bt_private_event_class_create_with_id(
185 struct bt_private_stream_class *priv_stream_class, uint64_t id)
7b33a0e0 186{
9e550e5f
PP
187 struct bt_stream_class *stream_class = (void *) priv_stream_class;
188
7b33a0e0
PP
189 BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id,
190 "Stream class automatically assigns event class IDs: "
191 "%![sc-]+S", stream_class);
9e550e5f 192 return (void *) create_event_class_with_id((void *) stream_class, id);
7b33a0e0
PP
193}
194
8deee039 195const char *bt_event_class_get_name(struct bt_event_class *event_class)
272df73e 196{
18acc6f8 197 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0 198 return event_class->name.value;
272df73e
PP
199}
200
9e550e5f 201int bt_private_event_class_set_name(struct bt_private_event_class *priv_event_class,
7b33a0e0 202 const char *name)
272df73e 203{
9e550e5f
PP
204 struct bt_event_class *event_class = (void *) priv_event_class;
205
18acc6f8 206 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0
PP
207 BT_ASSERT_PRE_NON_NULL(name, "Name");
208 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
209 g_string_assign(event_class->name.str, name);
210 event_class->name.value = event_class->name.str->str;
211 BT_LIB_LOGV("Set event class's name: %!+E", event_class);
212 return 0;
272df73e
PP
213}
214
7b33a0e0 215uint64_t bt_event_class_get_id(struct bt_event_class *event_class)
272df73e 216{
7b33a0e0
PP
217 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
218 return event_class->id;
272df73e
PP
219}
220
7b33a0e0
PP
221enum bt_property_availability bt_event_class_get_log_level(
222 struct bt_event_class *event_class,
223 enum bt_event_class_log_level *log_level)
272df73e 224{
18acc6f8 225 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0
PP
226 BT_ASSERT_PRE_NON_NULL(log_level, "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;
272df73e
PP
230}
231
c8bbf821 232void bt_private_event_class_set_log_level(
9e550e5f 233 struct bt_private_event_class *priv_event_class,
8deee039 234 enum bt_event_class_log_level log_level)
272df73e 235{
9e550e5f
PP
236 struct bt_event_class *event_class = (void *) priv_event_class;
237
7b33a0e0
PP
238 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
239 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
240 bt_property_uint_set(&event_class->log_level,
241 (uint64_t) log_level);
242 BT_LIB_LOGV("Set event class's log level: %!+E", event_class);
272df73e
PP
243}
244
8deee039 245const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class)
272df73e 246{
18acc6f8 247 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0 248 return event_class->emf_uri.value;
272df73e
PP
249}
250
9e550e5f
PP
251int bt_private_event_class_set_emf_uri(
252 struct bt_private_event_class *priv_event_class,
8deee039 253 const char *emf_uri)
272df73e 254{
9e550e5f
PP
255 struct bt_event_class *event_class = (void *) priv_event_class;
256
7b33a0e0
PP
257 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
258 BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI");
259 BT_ASSERT_PRE_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_LOGV("Set event class's EMF URI: %!+E", event_class);
263 return 0;
272df73e
PP
264}
265
5fe68922 266struct bt_stream_class *bt_event_class_borrow_stream_class(
839d52a5 267 struct bt_event_class *event_class)
272df73e 268{
44ea72c5 269 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0 270 return bt_event_class_borrow_stream_class_inline(event_class);
272df73e
PP
271}
272
9e550e5f 273struct bt_private_stream_class *
96854e6a 274bt_private_event_class_borrow_stream_class(
9e550e5f
PP
275 struct bt_private_event_class *event_class)
276{
277 return (void *) bt_event_class_borrow_stream_class(
278 (void *) event_class);
279}
280
939190b3 281struct bt_field_class *bt_event_class_borrow_specific_context_field_class(
8deee039 282 struct bt_event_class *event_class)
272df73e 283{
18acc6f8 284 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 285 return event_class->specific_context_fc;
272df73e
PP
286}
287
9e550e5f 288struct bt_private_field_class *
96854e6a 289bt_private_event_class_borrow_specific_context_field_class(
9e550e5f
PP
290 struct bt_private_event_class *event_class)
291{
292 return (void *) bt_event_class_borrow_specific_context_field_class(
293 (void *) event_class);
294}
295
96854e6a 296int bt_private_event_class_set_specific_context_field_class(
9e550e5f
PP
297 struct bt_private_event_class *priv_event_class,
298 struct bt_private_field_class *priv_field_class)
272df73e 299{
7b33a0e0 300 int ret;
9e550e5f
PP
301 struct bt_event_class *event_class = (void *) priv_event_class;
302 struct bt_field_class *field_class = (void *) priv_field_class;
7b33a0e0
PP
303 struct bt_stream_class *stream_class;
304 struct bt_trace *trace;
305 struct bt_resolve_field_path_context resolve_ctx = {
306 .packet_header = NULL,
307 .packet_context = NULL,
308 .event_header = NULL,
309 .event_common_context = NULL,
939190b3 310 .event_specific_context = field_class,
7b33a0e0
PP
311 .event_payload = NULL,
312 };
18acc6f8 313
7b33a0e0 314 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 315 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
7b33a0e0 316 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
af0c18e3
PP
317 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
318 BT_FIELD_CLASS_TYPE_STRUCTURE,
939190b3
PP
319 "Specific context field classe is not a structure field classe: "
320 "%!+F", field_class);
7b33a0e0
PP
321 stream_class = bt_event_class_borrow_stream_class_inline(
322 event_class);
323 trace = bt_stream_class_borrow_trace_inline(stream_class);
939190b3
PP
324 resolve_ctx.packet_header = trace->packet_header_fc;
325 resolve_ctx.packet_context = stream_class->packet_context_fc;
326 resolve_ctx.event_header = stream_class->event_header_fc;
7b33a0e0 327 resolve_ctx.event_common_context =
939190b3 328 stream_class->event_common_context_fc;
7b33a0e0 329
939190b3 330 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
7b33a0e0 331 if (ret) {
18acc6f8
PP
332 goto end;
333 }
334
939190b3 335 bt_field_class_make_part_of_trace(field_class);
8138bfe1
PP
336 bt_object_put_ref(event_class->specific_context_fc);
337 event_class->specific_context_fc = bt_object_get_ref(field_class);
939190b3
PP
338 bt_field_class_freeze(field_class);
339 BT_LIB_LOGV("Set event class's specific context field classe: %!+E",
7b33a0e0 340 event_class);
18acc6f8 341
18acc6f8
PP
342end:
343 return ret;
272df73e
PP
344}
345
939190b3 346struct bt_field_class *bt_event_class_borrow_payload_field_class(
8deee039 347 struct bt_event_class *event_class)
272df73e 348{
18acc6f8 349 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 350 return event_class->payload_fc;
272df73e
PP
351}
352
96854e6a 353struct bt_private_field_class *bt_private_event_class_borrow_payload_field_class(
9e550e5f
PP
354 struct bt_private_event_class *event_class)
355{
356 return (void *) bt_event_class_borrow_payload_field_class(
357 (void *) event_class);
358}
359
96854e6a 360int bt_private_event_class_set_payload_field_class(
9e550e5f
PP
361 struct bt_private_event_class *priv_event_class,
362 struct bt_private_field_class *priv_field_class)
272df73e 363{
7b33a0e0 364 int ret;
9e550e5f
PP
365 struct bt_event_class *event_class = (void *) priv_event_class;
366 struct bt_field_class *field_class = (void *) priv_field_class;
7b33a0e0
PP
367 struct bt_stream_class *stream_class;
368 struct bt_trace *trace;
369 struct bt_resolve_field_path_context resolve_ctx = {
370 .packet_header = NULL,
371 .packet_context = NULL,
372 .event_header = NULL,
373 .event_common_context = NULL,
374 .event_specific_context = NULL,
939190b3 375 .event_payload = field_class,
7b33a0e0 376 };
18acc6f8 377
7b33a0e0 378 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 379 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
7b33a0e0 380 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
af0c18e3
PP
381 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
382 BT_FIELD_CLASS_TYPE_STRUCTURE,
939190b3
PP
383 "Payload field classe is not a structure field classe: %!+F",
384 field_class);
7b33a0e0
PP
385 stream_class = bt_event_class_borrow_stream_class_inline(
386 event_class);
387 trace = bt_stream_class_borrow_trace_inline(stream_class);
939190b3
PP
388 resolve_ctx.packet_header = trace->packet_header_fc;
389 resolve_ctx.packet_context = stream_class->packet_context_fc;
390 resolve_ctx.event_header = stream_class->event_header_fc;
7b33a0e0 391 resolve_ctx.event_common_context =
939190b3
PP
392 stream_class->event_common_context_fc;
393 resolve_ctx.event_specific_context = event_class->specific_context_fc;
7b33a0e0 394
939190b3 395 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
7b33a0e0 396 if (ret) {
18acc6f8
PP
397 goto end;
398 }
399
939190b3 400 bt_field_class_make_part_of_trace(field_class);
8138bfe1
PP
401 bt_object_put_ref(event_class->payload_fc);
402 event_class->payload_fc = bt_object_get_ref(field_class);
939190b3
PP
403 bt_field_class_freeze(field_class);
404 BT_LIB_LOGV("Set event class's payload field classe: %!+E", event_class);
18acc6f8
PP
405
406end:
407 return ret;
272df73e
PP
408}
409
410BT_HIDDEN
7b33a0e0 411void _bt_event_class_freeze(struct bt_event_class *event_class)
8c4a29ba 412{
939190b3 413 /* The field classes are already frozen */
8b45963b 414 BT_ASSERT(event_class);
7b33a0e0
PP
415 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
416 event_class->frozen = true;
8c4a29ba 417}
This page took 0.078457 seconds and 4 git commands to generate.