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