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