Trace IR and notification APIs: split into private and public 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);
9cf5d083
PP
66 }
67
7b33a0e0
PP
68 if (event_class->emf_uri.str) {
69 g_string_free(event_class->emf_uri.str, TRUE);
272df73e
PP
70 }
71
939190b3 72 BT_LOGD_STR("Putting context field classe.");
8138bfe1 73 bt_object_put_ref(event_class->specific_context_fc);
939190b3 74 BT_LOGD_STR("Putting payload field classe.");
8138bfe1 75 bt_object_put_ref(event_class->payload_fc);
a6918753 76 bt_object_pool_finalize(&event_class->event_pool);
8deee039 77 g_free(obj);
272df73e
PP
78}
79
a6918753
PP
80static
81void free_event(struct bt_event *event,
82 struct bt_event_class *event_class)
83{
84 bt_event_destroy(event);
85}
86
7b33a0e0
PP
87BT_ASSERT_PRE_FUNC
88static
89bool event_class_id_is_unique(struct bt_stream_class *stream_class, uint64_t id)
272df73e 90{
7b33a0e0
PP
91 uint64_t i;
92 bool is_unique = true;
272df73e 93
7b33a0e0
PP
94 for (i = 0; i < stream_class->event_classes->len; i++) {
95 struct bt_event_class *ec =
96 stream_class->event_classes->pdata[i];
97
98 if (ec->id == id) {
99 is_unique = false;
100 goto end;
101 }
9cf5d083 102 }
272df73e 103
7b33a0e0
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);
8deee039 121 event_class = g_new0(struct bt_event_class, 1);
272df73e 122 if (!event_class) {
8deee039
PP
123 BT_LOGE_STR("Failed to allocate one event class.");
124 goto error;
272df73e
PP
125 }
126
7b33a0e0
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
a6918753
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
7b33a0e0
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);
8deee039 160 goto end;
272df73e 161
8deee039 162error:
8138bfe1 163 BT_OBJECT_PUT_REF_AND_RESET(event_class);
272df73e
PP
164
165end:
8deee039 166 return event_class;
272df73e
PP
167}
168
9e550e5f
PP
169struct bt_private_event_class *bt_private_event_class_create(
170 struct bt_private_stream_class *priv_stream_class)
7b33a0e0 171{
9e550e5f
PP
172 struct bt_stream_class *stream_class = (void *) priv_stream_class;
173
7b33a0e0
PP
174 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
175 BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id,
176 "Stream class does not automatically assigns event class IDs: "
177 "%![sc-]+S", stream_class);
9e550e5f 178 return (void *) create_event_class_with_id((void *) stream_class,
7b33a0e0
PP
179 (uint64_t) stream_class->event_classes->len);
180}
181
9e550e5f
PP
182struct bt_private_event_class *bt_private_event_class_create_with_id(
183 struct bt_private_stream_class *priv_stream_class, uint64_t id)
7b33a0e0 184{
9e550e5f
PP
185 struct bt_stream_class *stream_class = (void *) priv_stream_class;
186
7b33a0e0
PP
187 BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id,
188 "Stream class automatically assigns event class IDs: "
189 "%![sc-]+S", stream_class);
9e550e5f 190 return (void *) create_event_class_with_id((void *) stream_class, id);
7b33a0e0
PP
191}
192
8deee039 193const char *bt_event_class_get_name(struct bt_event_class *event_class)
272df73e 194{
18acc6f8 195 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0 196 return event_class->name.value;
272df73e
PP
197}
198
9e550e5f 199int bt_private_event_class_set_name(struct bt_private_event_class *priv_event_class,
7b33a0e0 200 const char *name)
272df73e 201{
9e550e5f
PP
202 struct bt_event_class *event_class = (void *) priv_event_class;
203
18acc6f8 204 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0
PP
205 BT_ASSERT_PRE_NON_NULL(name, "Name");
206 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
207 g_string_assign(event_class->name.str, name);
208 event_class->name.value = event_class->name.str->str;
209 BT_LIB_LOGV("Set event class's name: %!+E", event_class);
210 return 0;
272df73e
PP
211}
212
7b33a0e0 213uint64_t bt_event_class_get_id(struct bt_event_class *event_class)
272df73e 214{
7b33a0e0
PP
215 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
216 return event_class->id;
272df73e
PP
217}
218
7b33a0e0
PP
219enum bt_property_availability bt_event_class_get_log_level(
220 struct bt_event_class *event_class,
221 enum bt_event_class_log_level *log_level)
272df73e 222{
18acc6f8 223 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0
PP
224 BT_ASSERT_PRE_NON_NULL(log_level, "Log level (output)");
225 *log_level = (enum bt_event_class_log_level)
226 event_class->log_level.value;
227 return event_class->log_level.base.avail;
272df73e
PP
228}
229
9e550e5f
PP
230int bt_private_event_class_set_log_level(
231 struct bt_private_event_class *priv_event_class,
8deee039 232 enum bt_event_class_log_level log_level)
272df73e 233{
9e550e5f
PP
234 struct bt_event_class *event_class = (void *) priv_event_class;
235
7b33a0e0
PP
236 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
237 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
238 bt_property_uint_set(&event_class->log_level,
239 (uint64_t) log_level);
240 BT_LIB_LOGV("Set event class's log level: %!+E", event_class);
241 return 0;
272df73e
PP
242}
243
8deee039 244const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class)
272df73e 245{
18acc6f8 246 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0 247 return event_class->emf_uri.value;
272df73e
PP
248}
249
9e550e5f
PP
250int bt_private_event_class_set_emf_uri(
251 struct bt_private_event_class *priv_event_class,
8deee039 252 const char *emf_uri)
272df73e 253{
9e550e5f
PP
254 struct bt_event_class *event_class = (void *) priv_event_class;
255
7b33a0e0
PP
256 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
257 BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI");
258 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
259 g_string_assign(event_class->emf_uri.str, emf_uri);
260 event_class->emf_uri.value = event_class->emf_uri.str->str;
261 BT_LIB_LOGV("Set event class's EMF URI: %!+E", event_class);
262 return 0;
272df73e
PP
263}
264
5fe68922 265struct bt_stream_class *bt_event_class_borrow_stream_class(
839d52a5 266 struct bt_event_class *event_class)
272df73e 267{
44ea72c5 268 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
7b33a0e0 269 return bt_event_class_borrow_stream_class_inline(event_class);
272df73e
PP
270}
271
9e550e5f
PP
272struct bt_private_stream_class *
273bt_private_event_class_borrow_private_stream_class(
274 struct bt_private_event_class *event_class)
275{
276 return (void *) bt_event_class_borrow_stream_class(
277 (void *) event_class);
278}
279
939190b3 280struct bt_field_class *bt_event_class_borrow_specific_context_field_class(
8deee039 281 struct bt_event_class *event_class)
272df73e 282{
18acc6f8 283 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 284 return event_class->specific_context_fc;
272df73e
PP
285}
286
9e550e5f
PP
287struct bt_private_field_class *
288bt_private_event_class_borrow_specific_context_private_field_class(
289 struct bt_private_event_class *event_class)
290{
291 return (void *) bt_event_class_borrow_specific_context_field_class(
292 (void *) event_class);
293}
294
295int bt_private_event_class_set_specific_context_private_field_class(
296 struct bt_private_event_class *priv_event_class,
297 struct bt_private_field_class *priv_field_class)
272df73e 298{
7b33a0e0 299 int ret;
9e550e5f
PP
300 struct bt_event_class *event_class = (void *) priv_event_class;
301 struct bt_field_class *field_class = (void *) priv_field_class;
7b33a0e0
PP
302 struct bt_stream_class *stream_class;
303 struct bt_trace *trace;
304 struct bt_resolve_field_path_context resolve_ctx = {
305 .packet_header = NULL,
306 .packet_context = NULL,
307 .event_header = NULL,
308 .event_common_context = NULL,
939190b3 309 .event_specific_context = field_class,
7b33a0e0
PP
310 .event_payload = NULL,
311 };
18acc6f8 312
7b33a0e0 313 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 314 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
7b33a0e0 315 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
af0c18e3
PP
316 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
317 BT_FIELD_CLASS_TYPE_STRUCTURE,
939190b3
PP
318 "Specific context field classe is not a structure field classe: "
319 "%!+F", field_class);
7b33a0e0
PP
320 stream_class = bt_event_class_borrow_stream_class_inline(
321 event_class);
322 trace = bt_stream_class_borrow_trace_inline(stream_class);
939190b3
PP
323 resolve_ctx.packet_header = trace->packet_header_fc;
324 resolve_ctx.packet_context = stream_class->packet_context_fc;
325 resolve_ctx.event_header = stream_class->event_header_fc;
7b33a0e0 326 resolve_ctx.event_common_context =
939190b3 327 stream_class->event_common_context_fc;
7b33a0e0 328
939190b3 329 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
7b33a0e0 330 if (ret) {
18acc6f8
PP
331 goto end;
332 }
333
939190b3 334 bt_field_class_make_part_of_trace(field_class);
8138bfe1
PP
335 bt_object_put_ref(event_class->specific_context_fc);
336 event_class->specific_context_fc = bt_object_get_ref(field_class);
939190b3
PP
337 bt_field_class_freeze(field_class);
338 BT_LIB_LOGV("Set event class's specific context field classe: %!+E",
7b33a0e0 339 event_class);
18acc6f8 340
18acc6f8
PP
341end:
342 return ret;
272df73e
PP
343}
344
939190b3 345struct bt_field_class *bt_event_class_borrow_payload_field_class(
8deee039 346 struct bt_event_class *event_class)
272df73e 347{
18acc6f8 348 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 349 return event_class->payload_fc;
272df73e
PP
350}
351
9e550e5f
PP
352struct bt_private_field_class *bt_private_event_class_borrow_payload_private_field_class(
353 struct bt_private_event_class *event_class)
354{
355 return (void *) bt_event_class_borrow_payload_field_class(
356 (void *) event_class);
357}
358
359int bt_private_event_class_set_payload_private_field_class(
360 struct bt_private_event_class *priv_event_class,
361 struct bt_private_field_class *priv_field_class)
272df73e 362{
7b33a0e0 363 int ret;
9e550e5f
PP
364 struct bt_event_class *event_class = (void *) priv_event_class;
365 struct bt_field_class *field_class = (void *) priv_field_class;
7b33a0e0
PP
366 struct bt_stream_class *stream_class;
367 struct bt_trace *trace;
368 struct bt_resolve_field_path_context resolve_ctx = {
369 .packet_header = NULL,
370 .packet_context = NULL,
371 .event_header = NULL,
372 .event_common_context = NULL,
373 .event_specific_context = NULL,
939190b3 374 .event_payload = field_class,
7b33a0e0 375 };
18acc6f8 376
7b33a0e0 377 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
939190b3 378 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
7b33a0e0 379 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
af0c18e3
PP
380 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
381 BT_FIELD_CLASS_TYPE_STRUCTURE,
939190b3
PP
382 "Payload field classe is not a structure field classe: %!+F",
383 field_class);
7b33a0e0
PP
384 stream_class = bt_event_class_borrow_stream_class_inline(
385 event_class);
386 trace = bt_stream_class_borrow_trace_inline(stream_class);
939190b3
PP
387 resolve_ctx.packet_header = trace->packet_header_fc;
388 resolve_ctx.packet_context = stream_class->packet_context_fc;
389 resolve_ctx.event_header = stream_class->event_header_fc;
7b33a0e0 390 resolve_ctx.event_common_context =
939190b3
PP
391 stream_class->event_common_context_fc;
392 resolve_ctx.event_specific_context = event_class->specific_context_fc;
7b33a0e0 393
939190b3 394 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
7b33a0e0 395 if (ret) {
18acc6f8
PP
396 goto end;
397 }
398
939190b3 399 bt_field_class_make_part_of_trace(field_class);
8138bfe1
PP
400 bt_object_put_ref(event_class->payload_fc);
401 event_class->payload_fc = bt_object_get_ref(field_class);
939190b3
PP
402 bt_field_class_freeze(field_class);
403 BT_LIB_LOGV("Set event class's payload field classe: %!+E", event_class);
18acc6f8
PP
404
405end:
406 return ret;
272df73e
PP
407}
408
409BT_HIDDEN
7b33a0e0 410void _bt_event_class_freeze(struct bt_event_class *event_class)
8c4a29ba 411{
939190b3 412 /* The field classes are already frozen */
8b45963b 413 BT_ASSERT(event_class);
7b33a0e0
PP
414 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
415 event_class->frozen = true;
8c4a29ba 416}
9e550e5f
PP
417
418struct bt_event_class *bt_event_class_borrow_from_private(
419 struct bt_private_event_class *priv_event_class)
420{
421 return (void *) priv_event_class;
422}
This page took 0.064302 seconds and 4 git commands to generate.