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