Commit | Line | Data |
---|---|---|
bc37ae52 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
bc37ae52 JG |
3 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
bc37ae52 JG |
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 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/TRACE" |
c2d9d9cf | 25 | #include "lib/logging.h" |
e011d2c1 | 26 | |
578e048b | 27 | #include "lib/assert-pre.h" |
3c634c85 | 28 | #include "lib/assert-post.h" |
3fadfbc0 | 29 | #include <babeltrace2/trace-ir/trace.h> |
3fadfbc0 | 30 | #include <babeltrace2/trace-ir/event-class.h> |
578e048b MJ |
31 | #include "ctf-writer/functor.h" |
32 | #include "ctf-writer/clock.h" | |
33 | #include "compat/compiler.h" | |
3fadfbc0 | 34 | #include <babeltrace2/value.h> |
578e048b | 35 | #include "lib/value.h" |
3fadfbc0 | 36 | #include <babeltrace2/types.h> |
578e048b MJ |
37 | #include "compat/endian.h" |
38 | #include "common/assert.h" | |
39 | #include "compat/glib.h" | |
dc3fffef | 40 | #include <inttypes.h> |
544d0515 | 41 | #include <stdint.h> |
4a32fda0 | 42 | #include <string.h> |
c4f23e30 | 43 | #include <stdbool.h> |
0fbb9a9f | 44 | #include <stdlib.h> |
bc37ae52 | 45 | |
578e048b MJ |
46 | #include "attributes.h" |
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-class.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_destruction_listener_elem { |
62 | bt_trace_destruction_listener_func func; | |
3602afb0 PP |
63 | void *data; |
64 | }; | |
65 | ||
bdb288b3 PP |
66 | #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \ |
67 | BT_ASSERT_PRE_DEV_HOT((_trace), "Trace", ": %!+t", (_trace)) | |
cb6f1f7d | 68 | |
bc37ae52 | 69 | static |
44c440bc | 70 | void destroy_trace(struct bt_object *obj) |
3dca2276 PP |
71 | { |
72 | struct bt_trace *trace = (void *) obj; | |
bc37ae52 | 73 | |
44c440bc | 74 | BT_LIB_LOGD("Destroying trace object: %!+t", trace); |
c6962c96 | 75 | BT_OBJECT_PUT_REF_AND_RESET(trace->user_attributes); |
bc37ae52 | 76 | |
3dca2276 | 77 | /* |
ad5268b5 FD |
78 | * Call destruction listener functions so that everything else |
79 | * still exists in the trace. | |
3dca2276 | 80 | */ |
ad5268b5 FD |
81 | if (trace->destruction_listeners) { |
82 | uint64_t i; | |
42a63165 SM |
83 | const struct bt_error *saved_error; |
84 | ||
3f7d4d90 | 85 | BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace); |
c47a6bec SM |
86 | |
87 | /* | |
88 | * The trace's 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 (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 | trace->base.ref_count++; | |
98 | ||
42a63165 SM |
99 | saved_error = bt_current_thread_take_error(); |
100 | ||
ad5268b5 FD |
101 | /* Call all the trace destruction listeners */ |
102 | for (i = 0; i < trace->destruction_listeners->len; i++) { | |
103 | struct bt_trace_destruction_listener_elem elem = | |
104 | g_array_index(trace->destruction_listeners, | |
105 | struct bt_trace_destruction_listener_elem, i); | |
106 | ||
107 | if (elem.func) { | |
108 | elem.func(trace, elem.data); | |
6ecdcca3 | 109 | BT_ASSERT_POST_NO_ERROR(); |
3dca2276 | 110 | } |
c47a6bec SM |
111 | |
112 | /* | |
113 | * The destruction listener should not have kept a | |
114 | * reference to the trace. | |
115 | */ | |
3c634c85 | 116 | BT_ASSERT_POST(trace->base.ref_count == 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace); |
3dca2276 | 117 | } |
ad5268b5 FD |
118 | g_array_free(trace->destruction_listeners, TRUE); |
119 | trace->destruction_listeners = NULL; | |
42a63165 SM |
120 | |
121 | if (saved_error) { | |
122 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error); | |
123 | } | |
bc37ae52 JG |
124 | } |
125 | ||
44c440bc PP |
126 | if (trace->name.str) { |
127 | g_string_free(trace->name.str, TRUE); | |
238b7404 PP |
128 | trace->name.str = NULL; |
129 | trace->name.value = NULL; | |
95c09b3a PP |
130 | } |
131 | ||
335a2da5 PP |
132 | if (trace->environment) { |
133 | BT_LOGD_STR("Destroying environment attributes."); | |
134 | bt_attributes_destroy(trace->environment); | |
135 | trace->environment = NULL; | |
136 | } | |
137 | ||
44c440bc PP |
138 | if (trace->streams) { |
139 | BT_LOGD_STR("Destroying streams."); | |
140 | g_ptr_array_free(trace->streams, TRUE); | |
238b7404 | 141 | trace->streams = NULL; |
bc37ae52 JG |
142 | } |
143 | ||
44c440bc PP |
144 | if (trace->stream_classes_stream_count) { |
145 | g_hash_table_destroy(trace->stream_classes_stream_count); | |
238b7404 | 146 | trace->stream_classes_stream_count = NULL; |
44c440bc | 147 | } |
3dca2276 | 148 | |
862ca4ed PP |
149 | BT_LOGD_STR("Putting trace's class."); |
150 | bt_object_put_ref(trace->class); | |
151 | trace->class = NULL; | |
44c440bc | 152 | g_free(trace); |
3dca2276 PP |
153 | } |
154 | ||
862ca4ed | 155 | struct bt_trace *bt_trace_create(struct bt_trace_class *tc) |
3dca2276 PP |
156 | { |
157 | struct bt_trace *trace = NULL; | |
3dca2276 | 158 | |
17f3083a SM |
159 | BT_ASSERT_PRE_NO_ERROR(); |
160 | ||
862ca4ed | 161 | BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc); |
3dca2276 PP |
162 | trace = g_new0(struct bt_trace, 1); |
163 | if (!trace) { | |
870631a2 | 164 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace."); |
3dca2276 PP |
165 | goto error; |
166 | } | |
167 | ||
862ca4ed | 168 | bt_object_init_shared(&trace->base, destroy_trace); |
c6962c96 PP |
169 | trace->user_attributes = bt_value_map_create(); |
170 | if (!trace->user_attributes) { | |
171 | BT_LIB_LOGE_APPEND_CAUSE( | |
172 | "Failed to create a map value object."); | |
173 | goto error; | |
174 | } | |
175 | ||
44c440bc PP |
176 | trace->streams = g_ptr_array_new_with_free_func( |
177 | (GDestroyNotify) bt_object_try_spec_release); | |
178 | if (!trace->streams) { | |
870631a2 | 179 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
3dca2276 PP |
180 | goto error; |
181 | } | |
182 | ||
44c440bc PP |
183 | trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash, |
184 | g_direct_equal); | |
185 | if (!trace->stream_classes_stream_count) { | |
870631a2 | 186 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable."); |
44c440bc PP |
187 | goto error; |
188 | } | |
189 | ||
190 | trace->name.str = g_string_new(NULL); | |
191 | if (!trace->name.str) { | |
870631a2 | 192 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); |
44c440bc PP |
193 | goto error; |
194 | } | |
195 | ||
335a2da5 PP |
196 | trace->environment = bt_attributes_create(); |
197 | if (!trace->environment) { | |
198 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object."); | |
199 | goto error; | |
200 | } | |
201 | ||
ad5268b5 FD |
202 | trace->destruction_listeners = g_array_new(FALSE, TRUE, |
203 | sizeof(struct bt_trace_destruction_listener_elem)); | |
204 | if (!trace->destruction_listeners) { | |
870631a2 | 205 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
3602afb0 PP |
206 | goto error; |
207 | } | |
208 | ||
862ca4ed | 209 | trace->class = tc; |
6871026b | 210 | bt_object_get_ref_no_null_check(trace->class); |
44c440bc PP |
211 | BT_LIB_LOGD("Created trace object: %!+t", trace); |
212 | goto end; | |
bc37ae52 | 213 | |
bc37ae52 | 214 | error: |
65300d60 | 215 | BT_OBJECT_PUT_REF_AND_RESET(trace); |
44c440bc PP |
216 | |
217 | end: | |
862ca4ed | 218 | return trace; |
bc37ae52 JG |
219 | } |
220 | ||
40f4ba76 | 221 | const char *bt_trace_get_name(const struct bt_trace *trace) |
e96045d4 | 222 | { |
bdb288b3 | 223 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
44c440bc | 224 | return trace->name.value; |
e96045d4 JG |
225 | } |
226 | ||
d24d5663 PP |
227 | enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace, |
228 | const char *name) | |
e96045d4 | 229 | { |
17f3083a | 230 | BT_ASSERT_PRE_NO_ERROR(); |
44c440bc PP |
231 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
232 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
bdb288b3 | 233 | BT_ASSERT_PRE_DEV_TRACE_HOT(trace); |
44c440bc PP |
234 | g_string_assign(trace->name.str, name); |
235 | trace->name.value = trace->name.str->str; | |
3f7d4d90 | 236 | BT_LIB_LOGD("Set trace's name: %!+t", trace); |
d24d5663 | 237 | return BT_FUNC_STATUS_OK; |
e96045d4 JG |
238 | } |
239 | ||
335a2da5 PP |
240 | bt_uuid bt_trace_get_uuid(const struct bt_trace *trace) |
241 | { | |
bdb288b3 | 242 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
335a2da5 PP |
243 | return trace->uuid.value; |
244 | } | |
245 | ||
246 | void bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid) | |
247 | { | |
248 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
249 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
bdb288b3 | 250 | BT_ASSERT_PRE_DEV_TRACE_HOT(trace); |
6162e6b7 | 251 | bt_uuid_copy(trace->uuid.uuid, uuid); |
335a2da5 PP |
252 | trace->uuid.value = trace->uuid.uuid; |
253 | BT_LIB_LOGD("Set trace's UUID: %!+t", trace); | |
254 | } | |
255 | ||
335a2da5 PP |
256 | static |
257 | bool trace_has_environment_entry(const struct bt_trace *trace, const char *name) | |
258 | { | |
259 | BT_ASSERT(trace); | |
260 | ||
261 | return bt_attributes_borrow_field_value_by_name( | |
5084732e | 262 | trace->environment, name); |
335a2da5 PP |
263 | } |
264 | ||
265 | static | |
266 | enum bt_trace_set_environment_entry_status set_environment_entry( | |
267 | struct bt_trace *trace, | |
268 | const char *name, struct bt_value *value) | |
269 | { | |
270 | int ret; | |
271 | ||
272 | BT_ASSERT(trace); | |
273 | BT_ASSERT(name); | |
274 | BT_ASSERT(value); | |
275 | BT_ASSERT_PRE(!trace->frozen || | |
276 | !trace_has_environment_entry(trace, name), | |
277 | "Trace is frozen: cannot replace environment entry: " | |
278 | "%![trace-]+t, entry-name=\"%s\"", trace, name); | |
279 | ret = bt_attributes_set_field_value(trace->environment, name, | |
280 | value); | |
281 | if (ret) { | |
282 | ret = BT_FUNC_STATUS_MEMORY_ERROR; | |
283 | BT_LIB_LOGE_APPEND_CAUSE( | |
284 | "Cannot set trace's environment entry: " | |
285 | "%![trace-]+t, entry-name=\"%s\"", trace, name); | |
286 | } else { | |
287 | bt_value_freeze(value); | |
288 | BT_LIB_LOGD("Set trace's environment entry: " | |
289 | "%![trace-]+t, entry-name=\"%s\"", trace, name); | |
290 | } | |
291 | ||
292 | return ret; | |
293 | } | |
294 | ||
295 | enum bt_trace_set_environment_entry_status | |
296 | bt_trace_set_environment_entry_string( | |
297 | struct bt_trace *trace, const char *name, const char *value) | |
298 | { | |
299 | int ret; | |
300 | struct bt_value *value_obj; | |
17f3083a SM |
301 | |
302 | BT_ASSERT_PRE_NO_ERROR(); | |
335a2da5 PP |
303 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
304 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
305 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
17f3083a | 306 | |
335a2da5 PP |
307 | value_obj = bt_value_string_create_init(value); |
308 | if (!value_obj) { | |
309 | BT_LIB_LOGE_APPEND_CAUSE( | |
310 | "Cannot create a string value object."); | |
311 | ret = -1; | |
312 | goto end; | |
313 | } | |
314 | ||
315 | /* set_environment_entry() logs errors */ | |
316 | ret = set_environment_entry(trace, name, value_obj); | |
317 | ||
318 | end: | |
319 | bt_object_put_ref(value_obj); | |
320 | return ret; | |
321 | } | |
322 | ||
323 | enum bt_trace_set_environment_entry_status | |
324 | bt_trace_set_environment_entry_integer( | |
325 | struct bt_trace *trace, const char *name, int64_t value) | |
326 | { | |
327 | int ret; | |
328 | struct bt_value *value_obj; | |
17f3083a SM |
329 | |
330 | BT_ASSERT_PRE_NO_ERROR(); | |
335a2da5 PP |
331 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
332 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
17f3083a | 333 | |
9c08c816 | 334 | value_obj = bt_value_integer_signed_create_init(value); |
335a2da5 PP |
335 | if (!value_obj) { |
336 | BT_LIB_LOGE_APPEND_CAUSE( | |
337 | "Cannot create an integer value object."); | |
338 | ret = BT_FUNC_STATUS_MEMORY_ERROR; | |
339 | goto end; | |
340 | } | |
341 | ||
342 | /* set_environment_entry() logs errors */ | |
343 | ret = set_environment_entry(trace, name, value_obj); | |
344 | ||
345 | end: | |
346 | bt_object_put_ref(value_obj); | |
347 | return ret; | |
348 | } | |
349 | ||
350 | uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace) | |
351 | { | |
bdb288b3 | 352 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
99b4b64b | 353 | return bt_attributes_get_count(trace->environment); |
335a2da5 PP |
354 | } |
355 | ||
356 | void bt_trace_borrow_environment_entry_by_index_const( | |
357 | const struct bt_trace *trace, uint64_t index, | |
358 | const char **name, const struct bt_value **value) | |
359 | { | |
bdb288b3 PP |
360 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
361 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Name"); | |
362 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); | |
363 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, | |
335a2da5 PP |
364 | bt_attributes_get_count(trace->environment)); |
365 | *value = bt_attributes_borrow_field_value(trace->environment, index); | |
366 | BT_ASSERT(*value); | |
367 | *name = bt_attributes_get_field_name(trace->environment, index); | |
368 | BT_ASSERT(*name); | |
369 | } | |
370 | ||
371 | const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const( | |
372 | const struct bt_trace *trace, const char *name) | |
373 | { | |
bdb288b3 PP |
374 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
375 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Name"); | |
335a2da5 PP |
376 | return bt_attributes_borrow_field_value_by_name(trace->environment, |
377 | name); | |
378 | } | |
379 | ||
40f4ba76 | 380 | uint64_t bt_trace_get_stream_count(const struct bt_trace *trace) |
bc37ae52 | 381 | { |
bdb288b3 | 382 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
44c440bc PP |
383 | return (uint64_t) trace->streams->len; |
384 | } | |
95c09b3a | 385 | |
44c440bc PP |
386 | struct bt_stream *bt_trace_borrow_stream_by_index( |
387 | struct bt_trace *trace, uint64_t index) | |
388 | { | |
bdb288b3 PP |
389 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
390 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, trace->streams->len); | |
44c440bc PP |
391 | return g_ptr_array_index(trace->streams, index); |
392 | } | |
cb6f1f7d | 393 | |
40f4ba76 PP |
394 | const struct bt_stream *bt_trace_borrow_stream_by_index_const( |
395 | const struct bt_trace *trace, uint64_t index) | |
e5be10ef | 396 | { |
40f4ba76 | 397 | return bt_trace_borrow_stream_by_index((void *) trace, index); |
e5be10ef PP |
398 | } |
399 | ||
40f4ba76 PP |
400 | struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace, |
401 | uint64_t id) | |
44c440bc PP |
402 | { |
403 | struct bt_stream *stream = NULL; | |
404 | uint64_t i; | |
bc37ae52 | 405 | |
bdb288b3 | 406 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
bc37ae52 | 407 | |
44c440bc PP |
408 | for (i = 0; i < trace->streams->len; i++) { |
409 | struct bt_stream *stream_candidate = | |
410 | g_ptr_array_index(trace->streams, i); | |
cfeb617e | 411 | |
44c440bc PP |
412 | if (stream_candidate->id == id) { |
413 | stream = stream_candidate; | |
414 | goto end; | |
415 | } | |
6474e016 | 416 | } |
95c09b3a | 417 | |
bc37ae52 | 418 | end: |
44c440bc | 419 | return stream; |
bc37ae52 JG |
420 | } |
421 | ||
40f4ba76 PP |
422 | const struct bt_stream *bt_trace_borrow_stream_by_id_const( |
423 | const struct bt_trace *trace, uint64_t id) | |
e5be10ef | 424 | { |
40f4ba76 | 425 | return bt_trace_borrow_stream_by_id((void *) trace, id); |
e5be10ef PP |
426 | } |
427 | ||
d24d5663 | 428 | enum bt_trace_add_listener_status bt_trace_add_destruction_listener( |
40f4ba76 | 429 | const struct bt_trace *c_trace, |
ad5268b5 | 430 | bt_trace_destruction_listener_func listener, |
2054a0d1 | 431 | void *data, bt_listener_id *listener_id) |
3602afb0 | 432 | { |
40f4ba76 | 433 | struct bt_trace *trace = (void *) c_trace; |
44c440bc | 434 | uint64_t i; |
ad5268b5 | 435 | struct bt_trace_destruction_listener_elem new_elem = { |
3602afb0 PP |
436 | .func = listener, |
437 | .data = data, | |
438 | }; | |
439 | ||
17f3083a | 440 | BT_ASSERT_PRE_NO_ERROR(); |
44c440bc PP |
441 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
442 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
8480c8cc | 443 | |
3602afb0 | 444 | /* Find the next available spot */ |
ad5268b5 FD |
445 | for (i = 0; i < trace->destruction_listeners->len; i++) { |
446 | struct bt_trace_destruction_listener_elem elem = | |
447 | g_array_index(trace->destruction_listeners, | |
448 | struct bt_trace_destruction_listener_elem, i); | |
3602afb0 PP |
449 | |
450 | if (!elem.func) { | |
451 | break; | |
452 | } | |
453 | } | |
454 | ||
ad5268b5 FD |
455 | if (i == trace->destruction_listeners->len) { |
456 | g_array_append_val(trace->destruction_listeners, new_elem); | |
3602afb0 | 457 | } else { |
ad5268b5 | 458 | g_array_insert_val(trace->destruction_listeners, i, new_elem); |
3602afb0 PP |
459 | } |
460 | ||
44c440bc PP |
461 | if (listener_id) { |
462 | *listener_id = i; | |
463 | } | |
3602afb0 | 464 | |
3f7d4d90 | 465 | BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, " |
ad5268b5 | 466 | "listener-id=%" PRIu64, trace, i); |
d24d5663 | 467 | return BT_FUNC_STATUS_OK; |
44c440bc PP |
468 | } |
469 | ||
44c440bc | 470 | static |
40f4ba76 | 471 | bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id) |
44c440bc | 472 | { |
ad5268b5 FD |
473 | BT_ASSERT(listener_id < trace->destruction_listeners->len); |
474 | return (&g_array_index(trace->destruction_listeners, | |
475 | struct bt_trace_destruction_listener_elem, | |
5084732e | 476 | listener_id))->func; |
3602afb0 PP |
477 | } |
478 | ||
d24d5663 | 479 | enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener( |
2054a0d1 | 480 | const struct bt_trace *c_trace, bt_listener_id listener_id) |
3602afb0 | 481 | { |
40f4ba76 | 482 | struct bt_trace *trace = (void *) c_trace; |
ad5268b5 | 483 | struct bt_trace_destruction_listener_elem *elem; |
3602afb0 | 484 | |
17f3083a | 485 | BT_ASSERT_PRE_NO_ERROR(); |
44c440bc | 486 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 487 | BT_ASSERT_PRE(has_listener_id(trace, listener_id), |
ad5268b5 | 488 | "Trace has no such trace destruction listener ID: " |
44c440bc | 489 | "%![trace-]+t, %" PRIu64, trace, listener_id); |
ad5268b5 FD |
490 | elem = &g_array_index(trace->destruction_listeners, |
491 | struct bt_trace_destruction_listener_elem, | |
3602afb0 | 492 | listener_id); |
44c440bc | 493 | BT_ASSERT(elem->func); |
3602afb0 PP |
494 | |
495 | elem->func = NULL; | |
496 | elem->data = NULL; | |
3f7d4d90 | 497 | BT_LIB_LOGD("Removed \"trace destruction listener: " |
44c440bc PP |
498 | "%![trace-]+t, listener-id=%" PRIu64, |
499 | trace, listener_id); | |
d24d5663 | 500 | return BT_FUNC_STATUS_OK; |
44c440bc | 501 | } |
3602afb0 | 502 | |
44c440bc | 503 | BT_HIDDEN |
40f4ba76 | 504 | void _bt_trace_freeze(const struct bt_trace *trace) |
44c440bc | 505 | { |
44c440bc | 506 | BT_ASSERT(trace); |
862ca4ed PP |
507 | BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class); |
508 | bt_trace_class_freeze(trace->class); | |
c6962c96 PP |
509 | BT_LIB_LOGD("Freezing trace's user attributes: %!+v", |
510 | trace->user_attributes); | |
511 | bt_value_freeze(trace->user_attributes); | |
44c440bc | 512 | BT_LIB_LOGD("Freezing trace: %!+t", trace); |
40f4ba76 | 513 | ((struct bt_trace *) trace)->frozen = true; |
5acf2ae6 | 514 | } |
312c056a | 515 | |
44c440bc PP |
516 | BT_HIDDEN |
517 | void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream) | |
518 | { | |
519 | guint count = 0; | |
520 | ||
521 | bt_object_set_parent(&stream->base, &trace->base); | |
522 | g_ptr_array_add(trace->streams, stream); | |
cb6f1f7d | 523 | bt_trace_freeze(trace); |
312c056a | 524 | |
44c440bc PP |
525 | if (bt_g_hash_table_contains(trace->stream_classes_stream_count, |
526 | stream->class)) { | |
527 | count = GPOINTER_TO_UINT(g_hash_table_lookup( | |
528 | trace->stream_classes_stream_count, stream->class)); | |
312c056a PP |
529 | } |
530 | ||
44c440bc PP |
531 | g_hash_table_insert(trace->stream_classes_stream_count, |
532 | stream->class, GUINT_TO_POINTER(count + 1)); | |
533 | } | |
534 | ||
535 | BT_HIDDEN | |
40f4ba76 PP |
536 | uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace, |
537 | const struct bt_stream_class *stream_class) | |
44c440bc PP |
538 | { |
539 | gpointer orig_key; | |
540 | gpointer value; | |
541 | uint64_t id = 0; | |
542 | ||
543 | BT_ASSERT(stream_class); | |
544 | BT_ASSERT(trace); | |
545 | if (g_hash_table_lookup_extended(trace->stream_classes_stream_count, | |
546 | stream_class, &orig_key, &value)) { | |
547 | id = (uint64_t) GPOINTER_TO_UINT(value); | |
548 | } | |
549 | ||
550 | return id; | |
312c056a | 551 | } |
862ca4ed PP |
552 | |
553 | struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace) | |
554 | { | |
bdb288b3 | 555 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); |
862ca4ed PP |
556 | return trace->class; |
557 | } | |
558 | ||
559 | const struct bt_trace_class *bt_trace_borrow_class_const( | |
560 | const struct bt_trace *trace) | |
561 | { | |
562 | return bt_trace_borrow_class((void *) trace); | |
563 | } | |
c5b9b441 | 564 | |
c6962c96 PP |
565 | const struct bt_value *bt_trace_borrow_user_attributes_const( |
566 | const struct bt_trace *trace) | |
567 | { | |
568 | BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace"); | |
569 | return trace->user_attributes; | |
570 | } | |
571 | ||
572 | struct bt_value *bt_trace_borrow_user_attributes(struct bt_trace *trace) | |
573 | { | |
574 | return (void *) bt_trace_borrow_user_attributes_const((void *) trace); | |
575 | } | |
576 | ||
577 | void bt_trace_set_user_attributes( | |
578 | struct bt_trace *trace, | |
579 | const struct bt_value *user_attributes) | |
580 | { | |
581 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
582 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
583 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
584 | "User attributes object is not a map value object."); | |
585 | BT_ASSERT_PRE_DEV_TRACE_HOT(trace); | |
6871026b | 586 | bt_object_put_ref_no_null_check(trace->user_attributes); |
c6962c96 | 587 | trace->user_attributes = (void *) user_attributes; |
6871026b | 588 | bt_object_get_ref_no_null_check(trace->user_attributes); |
c6962c96 PP |
589 | } |
590 | ||
c5b9b441 PP |
591 | void bt_trace_get_ref(const struct bt_trace *trace) |
592 | { | |
593 | bt_object_get_ref(trace); | |
594 | } | |
595 | ||
596 | void bt_trace_put_ref(const struct bt_trace *trace) | |
597 | { | |
598 | bt_object_put_ref(trace); | |
599 | } |