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