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