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