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> | |
45 | #include <stdlib.h> | |
46 | ||
578e048b MJ |
47 | #include "clock-class.h" |
48 | #include "event-class.h" | |
49 | #include "event.h" | |
50 | #include "field-class.h" | |
51 | #include "field-wrapper.h" | |
52 | #include "resolve-field-path.h" | |
53 | #include "stream-class.h" | |
54 | #include "stream.h" | |
55 | #include "trace.h" | |
56 | #include "utils.h" | |
c6962c96 | 57 | #include "lib/value.h" |
d24d5663 | 58 | #include "lib/func-status.h" |
578e048b | 59 | |
ad5268b5 FD |
60 | struct bt_trace_class_destruction_listener_elem { |
61 | bt_trace_class_destruction_listener_func func; | |
62 | void *data; | |
63 | }; | |
64 | ||
bdb288b3 PP |
65 | #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \ |
66 | BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc)) | |
862ca4ed PP |
67 | |
68 | static | |
69 | void destroy_trace_class(struct bt_object *obj) | |
70 | { | |
71 | struct bt_trace_class *tc = (void *) obj; | |
72 | ||
73 | BT_LIB_LOGD("Destroying trace class object: %!+T", tc); | |
c6962c96 PP |
74 | BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes); |
75 | ||
ad5268b5 FD |
76 | /* |
77 | * Call destruction listener functions so that everything else | |
78 | * still exists in the trace class. | |
79 | */ | |
80 | if (tc->destruction_listeners) { | |
81 | uint64_t i; | |
3f7d4d90 | 82 | BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc); |
c47a6bec SM |
83 | |
84 | /* | |
85 | * The trace class' reference count is 0 if we're here. Increment | |
86 | * it to avoid a double-destroy (possibly infinitely recursive). | |
87 | * This could happen for example if a destruction listener did | |
88 | * bt_object_get_ref() (or anything that causes | |
89 | * bt_object_get_ref() to be called) on the trace class (ref. | |
90 | * count goes from 0 to 1), and then bt_object_put_ref(): the | |
91 | * reference count would go from 1 to 0 again and this function | |
92 | * would be called again. | |
93 | */ | |
94 | tc->base.ref_count++; | |
95 | ||
ad5268b5 FD |
96 | /* Call all the trace class destruction listeners */ |
97 | for (i = 0; i < tc->destruction_listeners->len; i++) { | |
98 | struct bt_trace_class_destruction_listener_elem elem = | |
99 | g_array_index(tc->destruction_listeners, | |
100 | struct bt_trace_class_destruction_listener_elem, i); | |
101 | ||
102 | if (elem.func) { | |
103 | elem.func(tc, elem.data); | |
104 | } | |
c47a6bec SM |
105 | |
106 | /* | |
107 | * The destruction listener should not have kept a | |
108 | * reference to the trace class. | |
109 | */ | |
3c634c85 | 110 | BT_ASSERT_POST(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc); |
ad5268b5 FD |
111 | } |
112 | g_array_free(tc->destruction_listeners, TRUE); | |
113 | tc->destruction_listeners = NULL; | |
114 | } | |
862ca4ed | 115 | |
862ca4ed PP |
116 | if (tc->stream_classes) { |
117 | BT_LOGD_STR("Destroying stream classes."); | |
118 | g_ptr_array_free(tc->stream_classes, TRUE); | |
119 | tc->stream_classes = NULL; | |
120 | } | |
121 | ||
862ca4ed PP |
122 | g_free(tc); |
123 | } | |
124 | ||
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 | |
41693723 | 129 | BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); |
862ca4ed PP |
130 | BT_LOGD_STR("Creating default trace class object."); |
131 | tc = g_new0(struct bt_trace_class, 1); | |
132 | if (!tc) { | |
870631a2 | 133 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class."); |
862ca4ed PP |
134 | goto error; |
135 | } | |
136 | ||
137 | bt_object_init_shared_with_parent(&tc->base, destroy_trace_class); | |
c6962c96 PP |
138 | tc->user_attributes = bt_value_map_create(); |
139 | if (!tc->user_attributes) { | |
140 | BT_LIB_LOGE_APPEND_CAUSE( | |
141 | "Failed to create a map value object."); | |
142 | goto error; | |
143 | } | |
862ca4ed PP |
144 | |
145 | tc->stream_classes = g_ptr_array_new_with_free_func( | |
146 | (GDestroyNotify) bt_object_try_spec_release); | |
147 | if (!tc->stream_classes) { | |
870631a2 | 148 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
862ca4ed PP |
149 | goto error; |
150 | } | |
151 | ||
ad5268b5 FD |
152 | tc->destruction_listeners = g_array_new(FALSE, TRUE, |
153 | sizeof(struct bt_trace_class_destruction_listener_elem)); | |
154 | if (!tc->destruction_listeners) { | |
870631a2 | 155 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
ad5268b5 FD |
156 | goto error; |
157 | } | |
158 | ||
862ca4ed | 159 | tc->assigns_automatic_stream_class_id = true; |
862ca4ed PP |
160 | BT_LIB_LOGD("Created trace class object: %!+T", tc); |
161 | goto end; | |
162 | ||
163 | error: | |
164 | BT_OBJECT_PUT_REF_AND_RESET(tc); | |
165 | ||
166 | end: | |
167 | return tc; | |
168 | } | |
169 | ||
d24d5663 | 170 | enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener( |
ad5268b5 FD |
171 | const struct bt_trace_class *_tc, |
172 | bt_trace_class_destruction_listener_func listener, | |
2054a0d1 | 173 | void *data, bt_listener_id *listener_id) |
ad5268b5 FD |
174 | { |
175 | struct bt_trace_class *tc = (void *) _tc; | |
176 | uint64_t i; | |
177 | struct bt_trace_class_destruction_listener_elem new_elem = { | |
178 | .func = listener, | |
179 | .data = data, | |
180 | }; | |
181 | ||
182 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
183 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
184 | ||
185 | /* Find the next available spot */ | |
186 | for (i = 0; i < tc->destruction_listeners->len; i++) { | |
187 | struct bt_trace_class_destruction_listener_elem elem = | |
188 | g_array_index(tc->destruction_listeners, | |
189 | struct bt_trace_class_destruction_listener_elem, i); | |
190 | ||
191 | if (!elem.func) { | |
192 | break; | |
193 | } | |
194 | } | |
195 | ||
196 | if (i == tc->destruction_listeners->len) { | |
197 | g_array_append_val(tc->destruction_listeners, new_elem); | |
198 | } else { | |
199 | g_array_insert_val(tc->destruction_listeners, i, new_elem); | |
200 | } | |
201 | ||
202 | if (listener_id) { | |
203 | *listener_id = i; | |
204 | } | |
205 | ||
3f7d4d90 | 206 | BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, " |
ad5268b5 | 207 | "listener-id=%" PRIu64, tc, i); |
d24d5663 | 208 | return BT_FUNC_STATUS_OK; |
ad5268b5 FD |
209 | } |
210 | ||
ad5268b5 FD |
211 | static |
212 | bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id) | |
213 | { | |
214 | BT_ASSERT(listener_id < tc->destruction_listeners->len); | |
215 | return (&g_array_index(tc->destruction_listeners, | |
216 | struct bt_trace_class_destruction_listener_elem, | |
5084732e | 217 | listener_id))->func; |
ad5268b5 FD |
218 | } |
219 | ||
d24d5663 | 220 | enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener( |
2054a0d1 | 221 | const struct bt_trace_class *_tc, bt_listener_id listener_id) |
ad5268b5 FD |
222 | { |
223 | struct bt_trace_class *tc = (void *) _tc; | |
224 | struct bt_trace_class_destruction_listener_elem *elem; | |
225 | ||
226 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
227 | BT_ASSERT_PRE(has_listener_id(tc, listener_id), | |
228 | "Trace class has no such trace class destruction listener ID: " | |
229 | "%![tc-]+T, %" PRIu64, tc, listener_id); | |
230 | elem = &g_array_index(tc->destruction_listeners, | |
231 | struct bt_trace_class_destruction_listener_elem, | |
232 | listener_id); | |
233 | BT_ASSERT(elem->func); | |
234 | ||
235 | elem->func = NULL; | |
236 | elem->data = NULL; | |
3f7d4d90 | 237 | BT_LIB_LOGD("Removed trace class destruction listener: " |
ad5268b5 FD |
238 | "%![tc-]+T, listener-id=%" PRIu64, |
239 | tc, listener_id); | |
d24d5663 | 240 | return BT_FUNC_STATUS_OK; |
ad5268b5 FD |
241 | } |
242 | ||
862ca4ed PP |
243 | uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc) |
244 | { | |
bdb288b3 | 245 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
862ca4ed PP |
246 | return (uint64_t) tc->stream_classes->len; |
247 | } | |
248 | ||
249 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index( | |
250 | struct bt_trace_class *tc, uint64_t index) | |
251 | { | |
bdb288b3 PP |
252 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
253 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len); | |
862ca4ed PP |
254 | return g_ptr_array_index(tc->stream_classes, index); |
255 | } | |
256 | ||
257 | const struct bt_stream_class * | |
258 | bt_trace_class_borrow_stream_class_by_index_const( | |
259 | const struct bt_trace_class *tc, uint64_t index) | |
260 | { | |
261 | return bt_trace_class_borrow_stream_class_by_index( | |
262 | (void *) tc, index); | |
263 | } | |
264 | ||
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 | ||
bdb288b3 | 271 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
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 | ||
287 | const struct bt_stream_class * | |
288 | bt_trace_class_borrow_stream_class_by_id_const( | |
289 | const struct bt_trace_class *tc, uint64_t id) | |
290 | { | |
291 | return bt_trace_class_borrow_stream_class_by_id((void *) tc, id); | |
292 | } | |
293 | ||
862ca4ed PP |
294 | BT_HIDDEN |
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 | ||
302 | bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc) | |
303 | { | |
bdb288b3 | 304 | BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class"); |
862ca4ed PP |
305 | return (bt_bool) tc->assigns_automatic_stream_class_id; |
306 | } | |
307 | ||
308 | void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc, | |
309 | bt_bool value) | |
310 | { | |
311 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
bdb288b3 | 312 | BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc); |
862ca4ed | 313 | tc->assigns_automatic_stream_class_id = (bool) value; |
3f7d4d90 | 314 | BT_LIB_LOGD("Set trace class's automatic stream class ID " |
862ca4ed PP |
315 | "assignment property: %!+T", tc); |
316 | } | |
c5b9b441 | 317 | |
c6962c96 PP |
318 | const struct bt_value *bt_trace_class_borrow_user_attributes_const( |
319 | const struct bt_trace_class *trace_class) | |
320 | { | |
321 | BT_ASSERT_PRE_DEV_NON_NULL(trace_class, "Trace class"); | |
322 | return trace_class->user_attributes; | |
323 | } | |
324 | ||
325 | struct bt_value *bt_trace_class_borrow_user_attributes( | |
326 | struct bt_trace_class *trace_class) | |
327 | { | |
328 | return (void *) bt_trace_class_borrow_user_attributes_const( | |
329 | (void *) trace_class); | |
330 | } | |
331 | ||
332 | void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class, | |
333 | const struct bt_value *user_attributes) | |
334 | { | |
335 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); | |
336 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
337 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
338 | "User attributes object is not a map value object."); | |
339 | BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class); | |
6871026b | 340 | bt_object_put_ref_no_null_check(trace_class->user_attributes); |
c6962c96 | 341 | trace_class->user_attributes = (void *) user_attributes; |
6871026b | 342 | bt_object_get_ref_no_null_check(trace_class->user_attributes); |
c6962c96 PP |
343 | } |
344 | ||
c5b9b441 PP |
345 | void bt_trace_class_get_ref(const struct bt_trace_class *trace_class) |
346 | { | |
347 | bt_object_get_ref(trace_class); | |
348 | } | |
349 | ||
350 | void bt_trace_class_put_ref(const struct bt_trace_class *trace_class) | |
351 | { | |
352 | bt_object_put_ref(trace_class); | |
353 | } |