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