Commit | Line | Data |
---|---|---|
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 | |
d98421f2 | 11 | #include "lib/assert-cond.h" |
3fadfbc0 | 12 | #include <babeltrace2/trace-ir/trace-class.h> |
3fadfbc0 | 13 | #include <babeltrace2/trace-ir/event-class.h> |
578e048b | 14 | #include "compat/compiler.h" |
3fadfbc0 | 15 | #include <babeltrace2/value.h> |
578e048b | 16 | #include "lib/value.h" |
3fadfbc0 | 17 | #include <babeltrace2/types.h> |
578e048b MJ |
18 | #include "compat/endian.h" |
19 | #include "common/assert.h" | |
862ca4ed PP |
20 | #include <inttypes.h> |
21 | #include <stdint.h> | |
22 | #include <string.h> | |
c4f23e30 | 23 | #include <stdbool.h> |
862ca4ed PP |
24 | #include <stdlib.h> |
25 | ||
426de380 | 26 | #include "trace-class.h" |
578e048b MJ |
27 | #include "stream-class.h" |
28 | #include "stream.h" | |
c6962c96 | 29 | #include "lib/value.h" |
d24d5663 | 30 | #include "lib/func-status.h" |
578e048b | 31 | |
ad5268b5 FD |
32 | struct bt_trace_class_destruction_listener_elem { |
33 | bt_trace_class_destruction_listener_func func; | |
34 | void *data; | |
35 | }; | |
36 | ||
bdb288b3 | 37 | #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \ |
1778c2a4 PP |
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" | |
862ca4ed PP |
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); | |
c6962c96 PP |
49 | BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes); |
50 | ||
ad5268b5 FD |
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; | |
42a63165 SM |
57 | const struct bt_error *saved_error; |
58 | ||
3f7d4d90 | 59 | BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc); |
c47a6bec SM |
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 | ||
42a63165 SM |
73 | saved_error = bt_current_thread_take_error(); |
74 | ||
ad5268b5 FD |
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 = | |
d50d46f3 | 78 | bt_g_array_index(tc->destruction_listeners, |
ad5268b5 FD |
79 | struct bt_trace_class_destruction_listener_elem, i); |
80 | ||
81 | if (elem.func) { | |
82 | elem.func(tc, elem.data); | |
1778c2a4 PP |
83 | BT_ASSERT_POST_NO_ERROR( |
84 | DESTRUCTION_LISTENER_FUNC_NAME); | |
ad5268b5 | 85 | } |
c47a6bec SM |
86 | |
87 | /* | |
88 | * The destruction listener should not have kept a | |
89 | * reference to the trace class. | |
90 | */ | |
1778c2a4 PP |
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); | |
ad5268b5 FD |
96 | } |
97 | g_array_free(tc->destruction_listeners, TRUE); | |
98 | tc->destruction_listeners = NULL; | |
42a63165 SM |
99 | |
100 | if (saved_error) { | |
101 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error); | |
102 | } | |
ad5268b5 | 103 | } |
862ca4ed | 104 | |
862ca4ed PP |
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 | ||
862ca4ed PP |
111 | g_free(tc); |
112 | } | |
113 | ||
1353b066 | 114 | BT_EXPORT |
41693723 | 115 | struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp) |
862ca4ed PP |
116 | { |
117 | struct bt_trace_class *tc = NULL; | |
862ca4ed | 118 | |
17f3083a | 119 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b | 120 | BT_ASSERT_PRE_COMP_NON_NULL(self_comp); |
862ca4ed PP |
121 | BT_LOGD_STR("Creating default trace class object."); |
122 | tc = g_new0(struct bt_trace_class, 1); | |
123 | if (!tc) { | |
870631a2 | 124 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class."); |
862ca4ed PP |
125 | goto error; |
126 | } | |
127 | ||
128 | bt_object_init_shared_with_parent(&tc->base, destroy_trace_class); | |
c6962c96 PP |
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 | } | |
862ca4ed PP |
135 | |
136 | tc->stream_classes = g_ptr_array_new_with_free_func( | |
137 | (GDestroyNotify) bt_object_try_spec_release); | |
138 | if (!tc->stream_classes) { | |
870631a2 | 139 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
862ca4ed PP |
140 | goto error; |
141 | } | |
142 | ||
ad5268b5 FD |
143 | tc->destruction_listeners = g_array_new(FALSE, TRUE, |
144 | sizeof(struct bt_trace_class_destruction_listener_elem)); | |
145 | if (!tc->destruction_listeners) { | |
870631a2 | 146 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
ad5268b5 FD |
147 | goto error; |
148 | } | |
149 | ||
862ca4ed | 150 | tc->assigns_automatic_stream_class_id = true; |
862ca4ed PP |
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 | ||
1353b066 | 161 | BT_EXPORT |
d24d5663 | 162 | enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener( |
ad5268b5 FD |
163 | const struct bt_trace_class *_tc, |
164 | bt_trace_class_destruction_listener_func listener, | |
2054a0d1 | 165 | void *data, bt_listener_id *listener_id) |
ad5268b5 FD |
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 | ||
17f3083a | 174 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
175 | BT_ASSERT_PRE_TC_NON_NULL(tc); |
176 | BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener); | |
ad5268b5 FD |
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 = | |
d50d46f3 | 181 | bt_g_array_index(tc->destruction_listeners, |
ad5268b5 FD |
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 | ||
3f7d4d90 | 199 | BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, " |
ad5268b5 | 200 | "listener-id=%" PRIu64, tc, i); |
d24d5663 | 201 | return BT_FUNC_STATUS_OK; |
ad5268b5 FD |
202 | } |
203 | ||
ad5268b5 FD |
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); | |
d50d46f3 | 208 | return (&bt_g_array_index(tc->destruction_listeners, |
ad5268b5 | 209 | struct bt_trace_class_destruction_listener_elem, |
5084732e | 210 | listener_id))->func; |
ad5268b5 FD |
211 | } |
212 | ||
1353b066 | 213 | BT_EXPORT |
d24d5663 | 214 | enum 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(); |
d5b13b9b | 221 | BT_ASSERT_PRE_TC_NON_NULL(tc); |
1778c2a4 PP |
222 | BT_ASSERT_PRE("listener-id-exists", |
223 | has_listener_id(tc, listener_id), | |
ad5268b5 FD |
224 | "Trace class has no such trace class destruction listener ID: " |
225 | "%![tc-]+T, %" PRIu64, tc, listener_id); | |
d50d46f3 | 226 | elem = &bt_g_array_index(tc->destruction_listeners, |
ad5268b5 FD |
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; | |
3f7d4d90 | 233 | BT_LIB_LOGD("Removed trace class destruction listener: " |
ad5268b5 FD |
234 | "%![tc-]+T, listener-id=%" PRIu64, |
235 | tc, listener_id); | |
d24d5663 | 236 | return BT_FUNC_STATUS_OK; |
ad5268b5 FD |
237 | } |
238 | ||
1353b066 | 239 | BT_EXPORT |
862ca4ed PP |
240 | uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc) |
241 | { | |
d5b13b9b | 242 | BT_ASSERT_PRE_DEV_TC_NON_NULL(tc); |
862ca4ed PP |
243 | return (uint64_t) tc->stream_classes->len; |
244 | } | |
245 | ||
1353b066 | 246 | BT_EXPORT |
862ca4ed PP |
247 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index( |
248 | struct bt_trace_class *tc, uint64_t index) | |
249 | { | |
d5b13b9b | 250 | BT_ASSERT_PRE_DEV_TC_NON_NULL(tc); |
bdb288b3 | 251 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len); |
862ca4ed PP |
252 | return g_ptr_array_index(tc->stream_classes, index); |
253 | } | |
254 | ||
1353b066 | 255 | BT_EXPORT |
862ca4ed PP |
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 | ||
1353b066 | 264 | BT_EXPORT |
862ca4ed PP |
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 | ||
d5b13b9b | 271 | BT_ASSERT_PRE_DEV_TC_NON_NULL(tc); |
862ca4ed PP |
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 | ||
1353b066 | 287 | BT_EXPORT |
862ca4ed PP |
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 | ||
862ca4ed PP |
295 | void _bt_trace_class_freeze(const struct bt_trace_class *tc) |
296 | { | |
862ca4ed PP |
297 | BT_ASSERT(tc); |
298 | BT_LIB_LOGD("Freezing trace class: %!+T", tc); | |
299 | ((struct bt_trace_class *) tc)->frozen = true; | |
300 | } | |
301 | ||
1353b066 | 302 | BT_EXPORT |
862ca4ed PP |
303 | bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc) |
304 | { | |
d5b13b9b | 305 | BT_ASSERT_PRE_DEV_TC_NON_NULL(tc); |
862ca4ed PP |
306 | return (bt_bool) tc->assigns_automatic_stream_class_id; |
307 | } | |
308 | ||
1353b066 | 309 | BT_EXPORT |
862ca4ed PP |
310 | void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc, |
311 | bt_bool value) | |
312 | { | |
d5b13b9b | 313 | BT_ASSERT_PRE_TC_NON_NULL(tc); |
bdb288b3 | 314 | BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc); |
862ca4ed | 315 | tc->assigns_automatic_stream_class_id = (bool) value; |
3f7d4d90 | 316 | BT_LIB_LOGD("Set trace class's automatic stream class ID " |
862ca4ed PP |
317 | "assignment property: %!+T", tc); |
318 | } | |
c5b9b441 | 319 | |
1353b066 | 320 | BT_EXPORT |
c6962c96 PP |
321 | const struct bt_value *bt_trace_class_borrow_user_attributes_const( |
322 | const struct bt_trace_class *trace_class) | |
323 | { | |
d5b13b9b | 324 | BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class); |
c6962c96 PP |
325 | return trace_class->user_attributes; |
326 | } | |
327 | ||
1353b066 | 328 | BT_EXPORT |
c6962c96 PP |
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 | ||
1353b066 | 336 | BT_EXPORT |
c6962c96 PP |
337 | void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class, |
338 | const struct bt_value *user_attributes) | |
339 | { | |
d5b13b9b PP |
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); | |
c6962c96 | 343 | BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class); |
6871026b | 344 | bt_object_put_ref_no_null_check(trace_class->user_attributes); |
c6962c96 | 345 | trace_class->user_attributes = (void *) user_attributes; |
6871026b | 346 | bt_object_get_ref_no_null_check(trace_class->user_attributes); |
c6962c96 PP |
347 | } |
348 | ||
1353b066 | 349 | BT_EXPORT |
c5b9b441 PP |
350 | void bt_trace_class_get_ref(const struct bt_trace_class *trace_class) |
351 | { | |
352 | bt_object_get_ref(trace_class); | |
353 | } | |
354 | ||
1353b066 | 355 | BT_EXPORT |
c5b9b441 PP |
356 | void bt_trace_class_put_ref(const struct bt_trace_class *trace_class) |
357 | { | |
358 | bt_object_put_ref(trace_class); | |
359 | } |