Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / trace-ir / trace-class.c
CommitLineData
862ca4ed 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
862ca4ed 5 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
862ca4ed
PP
6 */
7
a1a6f91b 8#define BT_LOG_TAG "LIB/TRACE-CLASS"
c2d9d9cf 9#include "lib/logging.h"
862ca4ed 10
578e048b 11#include "lib/assert-pre.h"
3c634c85 12#include "lib/assert-post.h"
3fadfbc0 13#include <babeltrace2/trace-ir/trace-class.h>
3fadfbc0 14#include <babeltrace2/trace-ir/event-class.h>
578e048b
MJ
15#include "ctf-writer/functor.h"
16#include "ctf-writer/clock.h"
17#include "compat/compiler.h"
3fadfbc0 18#include <babeltrace2/value.h>
578e048b 19#include "lib/value.h"
3fadfbc0 20#include <babeltrace2/types.h>
578e048b
MJ
21#include "compat/endian.h"
22#include "common/assert.h"
23#include "compat/glib.h"
862ca4ed
PP
24#include <inttypes.h>
25#include <stdint.h>
26#include <string.h>
c4f23e30 27#include <stdbool.h>
862ca4ed
PP
28#include <stdlib.h>
29
578e048b
MJ
30#include "clock-class.h"
31#include "event-class.h"
32#include "event.h"
33#include "field-class.h"
34#include "field-wrapper.h"
35#include "resolve-field-path.h"
36#include "stream-class.h"
37#include "stream.h"
38#include "trace.h"
39#include "utils.h"
c6962c96 40#include "lib/value.h"
d24d5663 41#include "lib/func-status.h"
578e048b 42
ad5268b5
FD
43struct bt_trace_class_destruction_listener_elem {
44 bt_trace_class_destruction_listener_func func;
45 void *data;
46};
47
bdb288b3
PP
48#define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
49 BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc))
862ca4ed
PP
50
51static
52void destroy_trace_class(struct bt_object *obj)
53{
54 struct bt_trace_class *tc = (void *) obj;
55
56 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
c6962c96
PP
57 BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes);
58
ad5268b5
FD
59 /*
60 * Call destruction listener functions so that everything else
61 * still exists in the trace class.
62 */
63 if (tc->destruction_listeners) {
64 uint64_t i;
42a63165
SM
65 const struct bt_error *saved_error;
66
3f7d4d90 67 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
c47a6bec
SM
68
69 /*
70 * The trace class' reference count is 0 if we're here. Increment
71 * it to avoid a double-destroy (possibly infinitely recursive).
72 * This could happen for example if a destruction listener did
73 * bt_object_get_ref() (or anything that causes
74 * bt_object_get_ref() to be called) on the trace class (ref.
75 * count goes from 0 to 1), and then bt_object_put_ref(): the
76 * reference count would go from 1 to 0 again and this function
77 * would be called again.
78 */
79 tc->base.ref_count++;
80
42a63165
SM
81 saved_error = bt_current_thread_take_error();
82
ad5268b5
FD
83 /* Call all the trace class destruction listeners */
84 for (i = 0; i < tc->destruction_listeners->len; i++) {
85 struct bt_trace_class_destruction_listener_elem elem =
86 g_array_index(tc->destruction_listeners,
87 struct bt_trace_class_destruction_listener_elem, i);
88
89 if (elem.func) {
90 elem.func(tc, elem.data);
6ecdcca3 91 BT_ASSERT_POST_NO_ERROR();
ad5268b5 92 }
c47a6bec
SM
93
94 /*
95 * The destruction listener should not have kept a
96 * reference to the trace class.
97 */
3c634c85 98 BT_ASSERT_POST(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc);
ad5268b5
FD
99 }
100 g_array_free(tc->destruction_listeners, TRUE);
101 tc->destruction_listeners = NULL;
42a63165
SM
102
103 if (saved_error) {
104 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
105 }
ad5268b5 106 }
862ca4ed 107
862ca4ed
PP
108 if (tc->stream_classes) {
109 BT_LOGD_STR("Destroying stream classes.");
110 g_ptr_array_free(tc->stream_classes, TRUE);
111 tc->stream_classes = NULL;
112 }
113
862ca4ed
PP
114 g_free(tc);
115}
116
41693723 117struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
118{
119 struct bt_trace_class *tc = NULL;
862ca4ed 120
17f3083a 121 BT_ASSERT_PRE_NO_ERROR();
41693723 122 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
862ca4ed
PP
123 BT_LOGD_STR("Creating default trace class object.");
124 tc = g_new0(struct bt_trace_class, 1);
125 if (!tc) {
870631a2 126 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
862ca4ed
PP
127 goto error;
128 }
129
130 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
c6962c96
PP
131 tc->user_attributes = bt_value_map_create();
132 if (!tc->user_attributes) {
133 BT_LIB_LOGE_APPEND_CAUSE(
134 "Failed to create a map value object.");
135 goto error;
136 }
862ca4ed
PP
137
138 tc->stream_classes = g_ptr_array_new_with_free_func(
139 (GDestroyNotify) bt_object_try_spec_release);
140 if (!tc->stream_classes) {
870631a2 141 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
862ca4ed
PP
142 goto error;
143 }
144
ad5268b5
FD
145 tc->destruction_listeners = g_array_new(FALSE, TRUE,
146 sizeof(struct bt_trace_class_destruction_listener_elem));
147 if (!tc->destruction_listeners) {
870631a2 148 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
ad5268b5
FD
149 goto error;
150 }
151
862ca4ed 152 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
153 BT_LIB_LOGD("Created trace class object: %!+T", tc);
154 goto end;
155
156error:
157 BT_OBJECT_PUT_REF_AND_RESET(tc);
158
159end:
160 return tc;
161}
162
d24d5663 163enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
ad5268b5
FD
164 const struct bt_trace_class *_tc,
165 bt_trace_class_destruction_listener_func listener,
2054a0d1 166 void *data, bt_listener_id *listener_id)
ad5268b5
FD
167{
168 struct bt_trace_class *tc = (void *) _tc;
169 uint64_t i;
170 struct bt_trace_class_destruction_listener_elem new_elem = {
171 .func = listener,
172 .data = data,
173 };
174
17f3083a 175 BT_ASSERT_PRE_NO_ERROR();
ad5268b5
FD
176 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
177 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
178
179 /* Find the next available spot */
180 for (i = 0; i < tc->destruction_listeners->len; i++) {
181 struct bt_trace_class_destruction_listener_elem elem =
182 g_array_index(tc->destruction_listeners,
183 struct bt_trace_class_destruction_listener_elem, i);
184
185 if (!elem.func) {
186 break;
187 }
188 }
189
190 if (i == tc->destruction_listeners->len) {
191 g_array_append_val(tc->destruction_listeners, new_elem);
192 } else {
193 g_array_insert_val(tc->destruction_listeners, i, new_elem);
194 }
195
196 if (listener_id) {
197 *listener_id = i;
198 }
199
3f7d4d90 200 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
ad5268b5 201 "listener-id=%" PRIu64, tc, i);
d24d5663 202 return BT_FUNC_STATUS_OK;
ad5268b5
FD
203}
204
ad5268b5
FD
205static
206bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
207{
208 BT_ASSERT(listener_id < tc->destruction_listeners->len);
209 return (&g_array_index(tc->destruction_listeners,
210 struct bt_trace_class_destruction_listener_elem,
5084732e 211 listener_id))->func;
ad5268b5
FD
212}
213
d24d5663 214enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
2054a0d1 215 const struct bt_trace_class *_tc, bt_listener_id listener_id)
ad5268b5
FD
216{
217 struct bt_trace_class *tc = (void *) _tc;
218 struct bt_trace_class_destruction_listener_elem *elem;
219
17f3083a 220 BT_ASSERT_PRE_NO_ERROR();
ad5268b5
FD
221 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
222 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
223 "Trace class has no such trace class destruction listener ID: "
224 "%![tc-]+T, %" PRIu64, tc, listener_id);
225 elem = &g_array_index(tc->destruction_listeners,
226 struct bt_trace_class_destruction_listener_elem,
227 listener_id);
228 BT_ASSERT(elem->func);
229
230 elem->func = NULL;
231 elem->data = NULL;
3f7d4d90 232 BT_LIB_LOGD("Removed trace class destruction listener: "
ad5268b5
FD
233 "%![tc-]+T, listener-id=%" PRIu64,
234 tc, listener_id);
d24d5663 235 return BT_FUNC_STATUS_OK;
ad5268b5
FD
236}
237
862ca4ed
PP
238uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
239{
bdb288b3 240 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
241 return (uint64_t) tc->stream_classes->len;
242}
243
244struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
245 struct bt_trace_class *tc, uint64_t index)
246{
bdb288b3
PP
247 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
248 BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len);
862ca4ed
PP
249 return g_ptr_array_index(tc->stream_classes, index);
250}
251
252const struct bt_stream_class *
253bt_trace_class_borrow_stream_class_by_index_const(
254 const struct bt_trace_class *tc, uint64_t index)
255{
256 return bt_trace_class_borrow_stream_class_by_index(
257 (void *) tc, index);
258}
259
260struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
261 struct bt_trace_class *tc, uint64_t id)
262{
263 struct bt_stream_class *stream_class = NULL;
264 uint64_t i;
265
bdb288b3 266 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
267
268 for (i = 0; i < tc->stream_classes->len; i++) {
269 struct bt_stream_class *stream_class_candidate =
270 g_ptr_array_index(tc->stream_classes, i);
271
272 if (stream_class_candidate->id == id) {
273 stream_class = stream_class_candidate;
274 goto end;
275 }
276 }
277
278end:
279 return stream_class;
280}
281
282const struct bt_stream_class *
283bt_trace_class_borrow_stream_class_by_id_const(
284 const struct bt_trace_class *tc, uint64_t id)
285{
286 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
287}
288
862ca4ed
PP
289BT_HIDDEN
290void _bt_trace_class_freeze(const struct bt_trace_class *tc)
291{
862ca4ed
PP
292 BT_ASSERT(tc);
293 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
294 ((struct bt_trace_class *) tc)->frozen = true;
295}
296
297bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
298{
bdb288b3 299 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
300 return (bt_bool) tc->assigns_automatic_stream_class_id;
301}
302
303void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
304 bt_bool value)
305{
306 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
bdb288b3 307 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
862ca4ed 308 tc->assigns_automatic_stream_class_id = (bool) value;
3f7d4d90 309 BT_LIB_LOGD("Set trace class's automatic stream class ID "
862ca4ed
PP
310 "assignment property: %!+T", tc);
311}
c5b9b441 312
c6962c96
PP
313const struct bt_value *bt_trace_class_borrow_user_attributes_const(
314 const struct bt_trace_class *trace_class)
315{
316 BT_ASSERT_PRE_DEV_NON_NULL(trace_class, "Trace class");
317 return trace_class->user_attributes;
318}
319
320struct bt_value *bt_trace_class_borrow_user_attributes(
321 struct bt_trace_class *trace_class)
322{
323 return (void *) bt_trace_class_borrow_user_attributes_const(
324 (void *) trace_class);
325}
326
327void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class,
328 const struct bt_value *user_attributes)
329{
330 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
331 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
332 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
333 "User attributes object is not a map value object.");
334 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class);
6871026b 335 bt_object_put_ref_no_null_check(trace_class->user_attributes);
c6962c96 336 trace_class->user_attributes = (void *) user_attributes;
6871026b 337 bt_object_get_ref_no_null_check(trace_class->user_attributes);
c6962c96
PP
338}
339
c5b9b441
PP
340void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
341{
342 bt_object_get_ref(trace_class);
343}
344
345void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
346{
347 bt_object_put_ref(trace_class);
348}
This page took 0.070653 seconds and 4 git commands to generate.