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