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