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 MJ |
29 | #include <babeltrace2/trace-ir/trace-class.h> |
30 | #include <babeltrace2/trace-ir/trace-class-const.h> | |
71c5da58 | 31 | #include <babeltrace2/trace-ir/event-class.h> |
57952005 MJ |
32 | #include "ctf-writer/functor.h" |
33 | #include "ctf-writer/clock.h" | |
34 | #include "compat/compiler.h" | |
71c5da58 MJ |
35 | #include <babeltrace2/value.h> |
36 | #include <babeltrace2/value-const.h> | |
57952005 | 37 | #include "lib/value.h" |
71c5da58 | 38 | #include <babeltrace2/types.h> |
57952005 MJ |
39 | #include "compat/endian.h" |
40 | #include "common/assert.h" | |
41 | #include "compat/glib.h" | |
10b7a2e4 PP |
42 | #include <inttypes.h> |
43 | #include <stdint.h> | |
44 | #include <string.h> | |
969c1d8a | 45 | #include <stdbool.h> |
10b7a2e4 PP |
46 | #include <stdlib.h> |
47 | ||
57952005 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" | |
af90138b | 58 | #include "lib/value.h" |
fb25b9e3 | 59 | #include "lib/func-status.h" |
57952005 | 60 | |
1122415c FD |
61 | struct bt_trace_class_destruction_listener_elem { |
62 | bt_trace_class_destruction_listener_func func; | |
63 | void *data; | |
64 | }; | |
65 | ||
fa6cfec3 PP |
66 | #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \ |
67 | BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc)) | |
10b7a2e4 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); | |
af90138b PP |
75 | BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes); |
76 | ||
1122415c 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; | |
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 | ||
1122415c FD |
97 | /* Call all the trace class destruction listeners */ |
98 | for (i = 0; i < tc->destruction_listeners->len; i++) { | |
99 | struct bt_trace_class_destruction_listener_elem elem = | |
100 | g_array_index(tc->destruction_listeners, | |
101 | struct bt_trace_class_destruction_listener_elem, i); | |
102 | ||
103 | if (elem.func) { | |
104 | elem.func(tc, elem.data); | |
d6f6a5aa | 105 | BT_ASSERT_POST_NO_ERROR(); |
1122415c | 106 | } |
69868f44 SM |
107 | |
108 | /* | |
109 | * The destruction listener should not have kept a | |
110 | * reference to the trace class. | |
111 | */ | |
727170fc | 112 | BT_ASSERT_POST(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc); |
1122415c FD |
113 | } |
114 | g_array_free(tc->destruction_listeners, TRUE); | |
115 | tc->destruction_listeners = NULL; | |
116 | } | |
10b7a2e4 | 117 | |
10b7a2e4 PP |
118 | if (tc->stream_classes) { |
119 | BT_LOGD_STR("Destroying stream classes."); | |
120 | g_ptr_array_free(tc->stream_classes, TRUE); | |
121 | tc->stream_classes = NULL; | |
122 | } | |
123 | ||
10b7a2e4 PP |
124 | g_free(tc); |
125 | } | |
126 | ||
e5e71bf8 | 127 | struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp) |
10b7a2e4 PP |
128 | { |
129 | struct bt_trace_class *tc = NULL; | |
10b7a2e4 | 130 | |
e5e71bf8 | 131 | BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); |
10b7a2e4 PP |
132 | BT_LOGD_STR("Creating default trace class object."); |
133 | tc = g_new0(struct bt_trace_class, 1); | |
134 | if (!tc) { | |
a8f90e5d | 135 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class."); |
10b7a2e4 PP |
136 | goto error; |
137 | } | |
138 | ||
139 | bt_object_init_shared_with_parent(&tc->base, destroy_trace_class); | |
af90138b PP |
140 | tc->user_attributes = bt_value_map_create(); |
141 | if (!tc->user_attributes) { | |
142 | BT_LIB_LOGE_APPEND_CAUSE( | |
143 | "Failed to create a map value object."); | |
144 | goto error; | |
145 | } | |
10b7a2e4 PP |
146 | |
147 | tc->stream_classes = g_ptr_array_new_with_free_func( | |
148 | (GDestroyNotify) bt_object_try_spec_release); | |
149 | if (!tc->stream_classes) { | |
a8f90e5d | 150 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
10b7a2e4 PP |
151 | goto error; |
152 | } | |
153 | ||
1122415c FD |
154 | tc->destruction_listeners = g_array_new(FALSE, TRUE, |
155 | sizeof(struct bt_trace_class_destruction_listener_elem)); | |
156 | if (!tc->destruction_listeners) { | |
a8f90e5d | 157 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
1122415c FD |
158 | goto error; |
159 | } | |
160 | ||
10b7a2e4 | 161 | tc->assigns_automatic_stream_class_id = true; |
10b7a2e4 PP |
162 | BT_LIB_LOGD("Created trace class object: %!+T", tc); |
163 | goto end; | |
164 | ||
165 | error: | |
166 | BT_OBJECT_PUT_REF_AND_RESET(tc); | |
167 | ||
168 | end: | |
169 | return tc; | |
170 | } | |
171 | ||
fb25b9e3 | 172 | enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener( |
1122415c FD |
173 | const struct bt_trace_class *_tc, |
174 | bt_trace_class_destruction_listener_func listener, | |
eba7ea21 | 175 | void *data, bt_listener_id *listener_id) |
1122415c 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 | ||
184 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
185 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
186 | ||
187 | /* Find the next available spot */ | |
188 | for (i = 0; i < tc->destruction_listeners->len; i++) { | |
189 | struct bt_trace_class_destruction_listener_elem elem = | |
190 | g_array_index(tc->destruction_listeners, | |
191 | struct bt_trace_class_destruction_listener_elem, i); | |
192 | ||
193 | if (!elem.func) { | |
194 | break; | |
195 | } | |
196 | } | |
197 | ||
198 | if (i == tc->destruction_listeners->len) { | |
199 | g_array_append_val(tc->destruction_listeners, new_elem); | |
200 | } else { | |
201 | g_array_insert_val(tc->destruction_listeners, i, new_elem); | |
202 | } | |
203 | ||
204 | if (listener_id) { | |
205 | *listener_id = i; | |
206 | } | |
207 | ||
a684a357 | 208 | BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, " |
1122415c | 209 | "listener-id=%" PRIu64, tc, i); |
fb25b9e3 | 210 | return BT_FUNC_STATUS_OK; |
1122415c FD |
211 | } |
212 | ||
1122415c FD |
213 | static |
214 | bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id) | |
215 | { | |
216 | BT_ASSERT(listener_id < tc->destruction_listeners->len); | |
217 | return (&g_array_index(tc->destruction_listeners, | |
218 | struct bt_trace_class_destruction_listener_elem, | |
8e01f2d9 | 219 | listener_id))->func; |
1122415c FD |
220 | } |
221 | ||
fb25b9e3 | 222 | enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener( |
eba7ea21 | 223 | const struct bt_trace_class *_tc, bt_listener_id listener_id) |
1122415c FD |
224 | { |
225 | struct bt_trace_class *tc = (void *) _tc; | |
226 | struct bt_trace_class_destruction_listener_elem *elem; | |
227 | ||
228 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
229 | BT_ASSERT_PRE(has_listener_id(tc, listener_id), | |
230 | "Trace class has no such trace class destruction listener ID: " | |
231 | "%![tc-]+T, %" PRIu64, tc, listener_id); | |
232 | elem = &g_array_index(tc->destruction_listeners, | |
233 | struct bt_trace_class_destruction_listener_elem, | |
234 | listener_id); | |
235 | BT_ASSERT(elem->func); | |
236 | ||
237 | elem->func = NULL; | |
238 | elem->data = NULL; | |
a684a357 | 239 | BT_LIB_LOGD("Removed trace class destruction listener: " |
1122415c FD |
240 | "%![tc-]+T, listener-id=%" PRIu64, |
241 | tc, listener_id); | |
fb25b9e3 | 242 | return BT_FUNC_STATUS_OK; |
1122415c FD |
243 | } |
244 | ||
10b7a2e4 PP |
245 | uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc) |
246 | { | |
fa6cfec3 | 247 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
10b7a2e4 PP |
248 | return (uint64_t) tc->stream_classes->len; |
249 | } | |
250 | ||
251 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index( | |
252 | struct bt_trace_class *tc, uint64_t index) | |
253 | { | |
fa6cfec3 PP |
254 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
255 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len); | |
10b7a2e4 PP |
256 | return g_ptr_array_index(tc->stream_classes, index); |
257 | } | |
258 | ||
259 | const struct bt_stream_class * | |
260 | bt_trace_class_borrow_stream_class_by_index_const( | |
261 | const struct bt_trace_class *tc, uint64_t index) | |
262 | { | |
263 | return bt_trace_class_borrow_stream_class_by_index( | |
264 | (void *) tc, index); | |
265 | } | |
266 | ||
267 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id( | |
268 | struct bt_trace_class *tc, uint64_t id) | |
269 | { | |
270 | struct bt_stream_class *stream_class = NULL; | |
271 | uint64_t i; | |
272 | ||
fa6cfec3 | 273 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
10b7a2e4 PP |
274 | |
275 | for (i = 0; i < tc->stream_classes->len; i++) { | |
276 | struct bt_stream_class *stream_class_candidate = | |
277 | g_ptr_array_index(tc->stream_classes, i); | |
278 | ||
279 | if (stream_class_candidate->id == id) { | |
280 | stream_class = stream_class_candidate; | |
281 | goto end; | |
282 | } | |
283 | } | |
284 | ||
285 | end: | |
286 | return stream_class; | |
287 | } | |
288 | ||
289 | const struct bt_stream_class * | |
290 | bt_trace_class_borrow_stream_class_by_id_const( | |
291 | const struct bt_trace_class *tc, uint64_t id) | |
292 | { | |
293 | return bt_trace_class_borrow_stream_class_by_id((void *) tc, id); | |
294 | } | |
295 | ||
10b7a2e4 PP |
296 | BT_HIDDEN |
297 | void _bt_trace_class_freeze(const struct bt_trace_class *tc) | |
298 | { | |
10b7a2e4 PP |
299 | BT_ASSERT(tc); |
300 | BT_LIB_LOGD("Freezing trace class: %!+T", tc); | |
301 | ((struct bt_trace_class *) tc)->frozen = true; | |
302 | } | |
303 | ||
304 | bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc) | |
305 | { | |
fa6cfec3 | 306 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
10b7a2e4 PP |
307 | return (bt_bool) tc->assigns_automatic_stream_class_id; |
308 | } | |
309 | ||
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_NON_NULL(tc, "Trace class"); | |
fa6cfec3 | 314 | BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc); |
10b7a2e4 | 315 | tc->assigns_automatic_stream_class_id = (bool) value; |
a684a357 | 316 | BT_LIB_LOGD("Set trace class's automatic stream class ID " |
10b7a2e4 PP |
317 | "assignment property: %!+T", tc); |
318 | } | |
8c6884d9 | 319 | |
af90138b PP |
320 | const struct bt_value *bt_trace_class_borrow_user_attributes_const( |
321 | const struct bt_trace_class *trace_class) | |
322 | { | |
323 | BT_ASSERT_PRE_DEV_NON_NULL(trace_class, "Trace class"); | |
324 | return trace_class->user_attributes; | |
325 | } | |
326 | ||
327 | struct bt_value *bt_trace_class_borrow_user_attributes( | |
328 | struct bt_trace_class *trace_class) | |
329 | { | |
330 | return (void *) bt_trace_class_borrow_user_attributes_const( | |
331 | (void *) trace_class); | |
332 | } | |
333 | ||
334 | void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class, | |
335 | const struct bt_value *user_attributes) | |
336 | { | |
337 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); | |
338 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
339 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
340 | "User attributes object is not a map value object."); | |
341 | BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class); | |
864aa43f | 342 | bt_object_put_ref_no_null_check(trace_class->user_attributes); |
af90138b | 343 | trace_class->user_attributes = (void *) user_attributes; |
864aa43f | 344 | bt_object_get_ref_no_null_check(trace_class->user_attributes); |
af90138b PP |
345 | } |
346 | ||
8c6884d9 PP |
347 | void bt_trace_class_get_ref(const struct bt_trace_class *trace_class) |
348 | { | |
349 | bt_object_get_ref(trace_class); | |
350 | } | |
351 | ||
352 | void bt_trace_class_put_ref(const struct bt_trace_class *trace_class) | |
353 | { | |
354 | bt_object_put_ref(trace_class); | |
355 | } |