Commit | Line | Data |
---|---|---|
bc37ae52 | 1 | /* |
bc37ae52 JG |
2 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
e011d2c1 PP |
25 | #define BT_LOG_TAG "TRACE" |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
3dca2276 | 28 | #include <babeltrace/assert-pre-internal.h> |
e5be10ef | 29 | #include <babeltrace/trace-ir/private-trace.h> |
56e18c4c PP |
30 | #include <babeltrace/trace-ir/trace-internal.h> |
31 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
32 | #include <babeltrace/trace-ir/stream-internal.h> | |
33 | #include <babeltrace/trace-ir/stream-class-internal.h> | |
34 | #include <babeltrace/trace-ir/event-internal.h> | |
35 | #include <babeltrace/trace-ir/event-class.h> | |
36 | #include <babeltrace/trace-ir/event-class-internal.h> | |
bc37ae52 | 37 | #include <babeltrace/ctf-writer/functor-internal.h> |
ac0c6bdd | 38 | #include <babeltrace/ctf-writer/clock-internal.h> |
56e18c4c | 39 | #include <babeltrace/trace-ir/field-wrapper-internal.h> |
5cd6d0e5 | 40 | #include <babeltrace/trace-ir/field-classes-internal.h> |
56e18c4c PP |
41 | #include <babeltrace/trace-ir/attributes-internal.h> |
42 | #include <babeltrace/trace-ir/utils-internal.h> | |
43 | #include <babeltrace/trace-ir/resolve-field-path-internal.h> | |
3d9990ac | 44 | #include <babeltrace/compiler-internal.h> |
dac5c838 | 45 | #include <babeltrace/values.h> |
da91b29a | 46 | #include <babeltrace/private-values.h> |
95c09b3a | 47 | #include <babeltrace/values-internal.h> |
65300d60 | 48 | #include <babeltrace/object.h> |
c55a9f58 | 49 | #include <babeltrace/types.h> |
3d9990ac | 50 | #include <babeltrace/endian-internal.h> |
f6ccaed9 | 51 | #include <babeltrace/assert-internal.h> |
44c440bc | 52 | #include <babeltrace/compat/glib-internal.h> |
dc3fffef | 53 | #include <inttypes.h> |
544d0515 | 54 | #include <stdint.h> |
4a32fda0 | 55 | #include <string.h> |
0fbb9a9f | 56 | #include <stdlib.h> |
bc37ae52 | 57 | |
50842bdc | 58 | struct bt_trace_is_static_listener_elem { |
e5be10ef PP |
59 | bt_private_trace_is_static_listener func; |
60 | bt_private_trace_listener_removed removed; | |
3602afb0 PP |
61 | void *data; |
62 | }; | |
63 | ||
44c440bc PP |
64 | #define BT_ASSERT_PRE_TRACE_HOT(_trace) \ |
65 | BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace)) | |
cb6f1f7d | 66 | |
bc37ae52 | 67 | static |
44c440bc | 68 | void destroy_trace(struct bt_object *obj) |
3dca2276 PP |
69 | { |
70 | struct bt_trace *trace = (void *) obj; | |
bc37ae52 | 71 | |
44c440bc | 72 | BT_LIB_LOGD("Destroying trace object: %!+t", trace); |
bc37ae52 | 73 | |
3dca2276 PP |
74 | /* |
75 | * Call remove listeners first so that everything else still | |
76 | * exists in the trace. | |
77 | */ | |
78 | if (trace->is_static_listeners) { | |
79 | size_t i; | |
bc37ae52 | 80 | |
3dca2276 PP |
81 | for (i = 0; i < trace->is_static_listeners->len; i++) { |
82 | struct bt_trace_is_static_listener_elem elem = | |
83 | g_array_index(trace->is_static_listeners, | |
84 | struct bt_trace_is_static_listener_elem, i); | |
bc37ae52 | 85 | |
3dca2276 | 86 | if (elem.removed) { |
e5be10ef | 87 | elem.removed((void *) trace, elem.data); |
3dca2276 PP |
88 | } |
89 | } | |
90 | ||
91 | g_array_free(trace->is_static_listeners, TRUE); | |
238b7404 | 92 | trace->is_static_listeners = NULL; |
bc37ae52 JG |
93 | } |
94 | ||
312c056a | 95 | bt_object_pool_finalize(&trace->packet_header_field_pool); |
3dca2276 | 96 | |
44c440bc PP |
97 | if (trace->environment) { |
98 | BT_LOGD_STR("Destroying environment attributes."); | |
99 | bt_attributes_destroy(trace->environment); | |
238b7404 | 100 | trace->environment = NULL; |
95c09b3a PP |
101 | } |
102 | ||
44c440bc PP |
103 | if (trace->name.str) { |
104 | g_string_free(trace->name.str, TRUE); | |
238b7404 PP |
105 | trace->name.str = NULL; |
106 | trace->name.value = NULL; | |
95c09b3a PP |
107 | } |
108 | ||
44c440bc PP |
109 | if (trace->streams) { |
110 | BT_LOGD_STR("Destroying streams."); | |
111 | g_ptr_array_free(trace->streams, TRUE); | |
238b7404 | 112 | trace->streams = NULL; |
bc37ae52 JG |
113 | } |
114 | ||
44c440bc PP |
115 | if (trace->stream_classes) { |
116 | BT_LOGD_STR("Destroying stream classes."); | |
117 | g_ptr_array_free(trace->stream_classes, TRUE); | |
238b7404 | 118 | trace->stream_classes = NULL; |
7f800dc7 PP |
119 | } |
120 | ||
44c440bc PP |
121 | if (trace->stream_classes_stream_count) { |
122 | g_hash_table_destroy(trace->stream_classes_stream_count); | |
238b7404 | 123 | trace->stream_classes_stream_count = NULL; |
44c440bc | 124 | } |
3dca2276 | 125 | |
5cd6d0e5 | 126 | BT_LOGD_STR("Putting packet header field classe."); |
65300d60 | 127 | bt_object_put_ref(trace->packet_header_fc); |
238b7404 | 128 | trace->packet_header_fc = NULL; |
44c440bc | 129 | g_free(trace); |
3dca2276 PP |
130 | } |
131 | ||
312c056a PP |
132 | static |
133 | void free_packet_header_field(struct bt_field_wrapper *field_wrapper, | |
134 | struct bt_trace *trace) | |
135 | { | |
136 | bt_field_wrapper_destroy(field_wrapper); | |
137 | } | |
138 | ||
e5be10ef | 139 | struct bt_private_trace *bt_private_trace_create(void) |
3dca2276 PP |
140 | { |
141 | struct bt_trace *trace = NULL; | |
142 | int ret; | |
143 | ||
44c440bc | 144 | BT_LOGD_STR("Creating default trace object."); |
3dca2276 PP |
145 | trace = g_new0(struct bt_trace, 1); |
146 | if (!trace) { | |
147 | BT_LOGE_STR("Failed to allocate one trace."); | |
148 | goto error; | |
149 | } | |
150 | ||
44c440bc PP |
151 | bt_object_init_shared_with_parent(&trace->base, destroy_trace); |
152 | trace->streams = g_ptr_array_new_with_free_func( | |
153 | (GDestroyNotify) bt_object_try_spec_release); | |
154 | if (!trace->streams) { | |
155 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
3dca2276 PP |
156 | goto error; |
157 | } | |
158 | ||
44c440bc PP |
159 | trace->stream_classes = g_ptr_array_new_with_free_func( |
160 | (GDestroyNotify) bt_object_try_spec_release); | |
161 | if (!trace->stream_classes) { | |
95c09b3a | 162 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
9b888ff3 JG |
163 | goto error; |
164 | } | |
165 | ||
44c440bc PP |
166 | trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash, |
167 | g_direct_equal); | |
168 | if (!trace->stream_classes_stream_count) { | |
169 | BT_LOGE_STR("Failed to allocate one GHashTable."); | |
170 | goto error; | |
171 | } | |
172 | ||
173 | trace->name.str = g_string_new(NULL); | |
174 | if (!trace->name.str) { | |
175 | BT_LOGE_STR("Failed to allocate one GString."); | |
176 | goto error; | |
177 | } | |
178 | ||
179 | trace->environment = bt_attributes_create(); | |
180 | if (!trace->environment) { | |
181 | BT_LOGE_STR("Cannot create empty attributes object."); | |
182 | goto error; | |
183 | } | |
184 | ||
3602afb0 | 185 | trace->is_static_listeners = g_array_new(FALSE, TRUE, |
50842bdc | 186 | sizeof(struct bt_trace_is_static_listener_elem)); |
3602afb0 PP |
187 | if (!trace->is_static_listeners) { |
188 | BT_LOGE_STR("Failed to allocate one GArray."); | |
189 | goto error; | |
190 | } | |
191 | ||
44c440bc | 192 | trace->assigns_automatic_stream_class_id = true; |
312c056a PP |
193 | ret = bt_object_pool_initialize(&trace->packet_header_field_pool, |
194 | (bt_object_pool_new_object_func) bt_field_wrapper_new, | |
195 | (bt_object_pool_destroy_object_func) free_packet_header_field, | |
196 | trace); | |
197 | if (ret) { | |
198 | BT_LOGE("Failed to initialize packet header field pool: ret=%d", | |
199 | ret); | |
200 | goto error; | |
201 | } | |
202 | ||
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: | |
e5be10ef | 210 | return (void *) trace; |
bc37ae52 JG |
211 | } |
212 | ||
50842bdc | 213 | const char *bt_trace_get_name(struct bt_trace *trace) |
e96045d4 | 214 | { |
cb6f1f7d | 215 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 216 | return trace->name.value; |
e96045d4 JG |
217 | } |
218 | ||
e5be10ef PP |
219 | int bt_private_trace_set_name(struct bt_private_trace *priv_trace, |
220 | const char *name) | |
e96045d4 | 221 | { |
e5be10ef PP |
222 | struct bt_trace *trace = (void *) priv_trace; |
223 | ||
44c440bc PP |
224 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
225 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
226 | BT_ASSERT_PRE_TRACE_HOT(trace); | |
227 | g_string_assign(trace->name.str, name); | |
228 | trace->name.value = trace->name.str->str; | |
229 | BT_LIB_LOGV("Set trace's name: %!+t", trace); | |
230 | return 0; | |
e96045d4 JG |
231 | } |
232 | ||
44c440bc | 233 | bt_uuid bt_trace_get_uuid(struct bt_trace *trace) |
4a32fda0 | 234 | { |
cb6f1f7d | 235 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 236 | return trace->uuid.value; |
4a32fda0 PP |
237 | } |
238 | ||
140e6d94 PP |
239 | void bt_private_trace_set_uuid(struct bt_private_trace *priv_trace, |
240 | bt_uuid uuid) | |
4a32fda0 | 241 | { |
e5be10ef PP |
242 | struct bt_trace *trace = (void *) priv_trace; |
243 | ||
44c440bc PP |
244 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
245 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
246 | BT_ASSERT_PRE_TRACE_HOT(trace); | |
247 | memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
248 | trace->uuid.value = trace->uuid.uuid; | |
249 | BT_LIB_LOGV("Set trace's UUID: %!+t", trace); | |
4a32fda0 PP |
250 | } |
251 | ||
44c440bc PP |
252 | BT_ASSERT_FUNC |
253 | static | |
254 | bool trace_has_environment_entry(struct bt_trace *trace, const char *name) | |
bc37ae52 | 255 | { |
44c440bc | 256 | BT_ASSERT(trace); |
95c09b3a | 257 | |
da91b29a PP |
258 | return bt_attributes_borrow_field_value_by_name( |
259 | trace->environment, name) != NULL; | |
44c440bc | 260 | } |
a0d12916 | 261 | |
44c440bc PP |
262 | static |
263 | int set_environment_entry(struct bt_trace *trace, const char *name, | |
da91b29a | 264 | struct bt_private_value *value) |
44c440bc PP |
265 | { |
266 | int ret; | |
a0d12916 | 267 | |
44c440bc PP |
268 | BT_ASSERT(trace); |
269 | BT_ASSERT(name); | |
270 | BT_ASSERT(value); | |
271 | BT_ASSERT_PRE(!trace->frozen || | |
272 | !trace_has_environment_entry(trace, name), | |
273 | "Trace is frozen: cannot replace environment entry: " | |
274 | "%![trace-]+t, entry-name=\"%s\"", trace, name); | |
50842bdc | 275 | ret = bt_attributes_set_field_value(trace->environment, name, |
7f800dc7 | 276 | value); |
707b7d35 | 277 | bt_value_freeze(bt_private_value_as_value(value)); |
95c09b3a | 278 | if (ret) { |
44c440bc PP |
279 | BT_LIB_LOGE("Cannot set trace's environment entry: " |
280 | "%![trace-]+t, entry-name=\"%s\"", trace, name); | |
95c09b3a | 281 | } else { |
44c440bc PP |
282 | BT_LIB_LOGV("Set trace's environment entry: " |
283 | "%![trace-]+t, entry-name=\"%s\"", trace, name); | |
95c09b3a | 284 | } |
bc37ae52 | 285 | |
7f800dc7 PP |
286 | return ret; |
287 | } | |
bc37ae52 | 288 | |
28e6ca8b | 289 | int bt_private_trace_set_environment_entry_string( |
e5be10ef | 290 | struct bt_private_trace *priv_trace, |
7f800dc7 PP |
291 | const char *name, const char *value) |
292 | { | |
44c440bc | 293 | int ret; |
da91b29a | 294 | struct bt_private_value *value_obj; |
e5be10ef | 295 | struct bt_trace *trace = (void *) priv_trace; |
bc37ae52 | 296 | |
44c440bc PP |
297 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
298 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
299 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
da91b29a | 300 | value_obj = bt_private_value_string_create_init(value); |
44c440bc PP |
301 | if (!value_obj) { |
302 | BT_LOGE_STR("Cannot create a string value object."); | |
7f800dc7 PP |
303 | ret = -1; |
304 | goto end; | |
bc37ae52 JG |
305 | } |
306 | ||
44c440bc PP |
307 | /* set_environment_entry() logs errors */ |
308 | ret = set_environment_entry(trace, name, value_obj); | |
7f800dc7 PP |
309 | |
310 | end: | |
65300d60 | 311 | bt_object_put_ref(value_obj); |
3487c9f3 JG |
312 | return ret; |
313 | } | |
314 | ||
28e6ca8b | 315 | int bt_private_trace_set_environment_entry_integer( |
e5be10ef PP |
316 | struct bt_private_trace *priv_trace, |
317 | const char *name, int64_t value) | |
3487c9f3 | 318 | { |
44c440bc | 319 | int ret; |
da91b29a | 320 | struct bt_private_value *value_obj; |
e5be10ef | 321 | struct bt_trace *trace = (void *) priv_trace; |
3487c9f3 | 322 | |
44c440bc PP |
323 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
324 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
da91b29a | 325 | value_obj = bt_private_value_integer_create_init(value); |
44c440bc PP |
326 | if (!value_obj) { |
327 | BT_LOGE_STR("Cannot create an integer value object."); | |
3487c9f3 | 328 | ret = -1; |
7f800dc7 | 329 | goto end; |
3487c9f3 JG |
330 | } |
331 | ||
44c440bc PP |
332 | /* set_environment_entry() logs errors */ |
333 | ret = set_environment_entry(trace, name, value_obj); | |
95c09b3a | 334 | |
7f800dc7 | 335 | end: |
65300d60 | 336 | bt_object_put_ref(value_obj); |
bc37ae52 JG |
337 | return ret; |
338 | } | |
339 | ||
44c440bc | 340 | uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace) |
3dca2276 | 341 | { |
cb6f1f7d PP |
342 | int64_t ret; |
343 | ||
344 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
345 | ret = bt_attributes_get_count(trace->environment); | |
346 | BT_ASSERT(ret >= 0); | |
44c440bc | 347 | return (uint64_t) ret; |
e6fa2160 JG |
348 | } |
349 | ||
44c440bc PP |
350 | void bt_trace_borrow_environment_entry_by_index( |
351 | struct bt_trace *trace, uint64_t index, | |
352 | const char **name, struct bt_value **value) | |
e6fa2160 | 353 | { |
cb6f1f7d | 354 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc PP |
355 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
356 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
357 | BT_ASSERT_PRE_VALID_INDEX(index, | |
358 | bt_attributes_get_count(trace->environment)); | |
707b7d35 | 359 | *value = bt_private_value_as_value( |
da91b29a | 360 | bt_attributes_borrow_field_value(trace->environment, index)); |
44c440bc PP |
361 | BT_ASSERT(*value); |
362 | *name = bt_attributes_get_field_name(trace->environment, index); | |
363 | BT_ASSERT(*name); | |
e6fa2160 JG |
364 | } |
365 | ||
28e6ca8b | 366 | void bt_private_trace_borrow_environment_entry_by_index( |
e5be10ef PP |
367 | struct bt_private_trace *trace, uint64_t index, |
368 | const char **name, struct bt_private_value **value) | |
369 | { | |
370 | bt_trace_borrow_environment_entry_by_index((void *) trace, | |
371 | index, name, (void *) value); | |
372 | } | |
373 | ||
44c440bc | 374 | struct bt_value *bt_trace_borrow_environment_entry_value_by_name( |
50842bdc | 375 | struct bt_trace *trace, const char *name) |
e6fa2160 | 376 | { |
cb6f1f7d PP |
377 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
378 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
707b7d35 | 379 | return bt_private_value_as_value( |
da91b29a PP |
380 | bt_attributes_borrow_field_value_by_name(trace->environment, |
381 | name)); | |
e6fa2160 JG |
382 | } |
383 | ||
e5be10ef | 384 | struct bt_private_value * |
28e6ca8b | 385 | bt_private_trace_borrow_environment_entry_value_by_name( |
e5be10ef PP |
386 | struct bt_private_trace *trace, const char *name) |
387 | { | |
388 | return (void *) bt_trace_borrow_environment_entry_value_by_name( | |
389 | (void *) trace, name); | |
390 | } | |
391 | ||
44c440bc | 392 | uint64_t bt_trace_get_stream_count(struct bt_trace *trace) |
bc37ae52 | 393 | { |
44c440bc PP |
394 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
395 | return (uint64_t) trace->streams->len; | |
396 | } | |
95c09b3a | 397 | |
44c440bc PP |
398 | struct bt_stream *bt_trace_borrow_stream_by_index( |
399 | struct bt_trace *trace, uint64_t index) | |
400 | { | |
401 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
402 | BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len); | |
403 | return g_ptr_array_index(trace->streams, index); | |
404 | } | |
cb6f1f7d | 405 | |
28e6ca8b | 406 | struct bt_private_stream *bt_private_trace_borrow_stream_by_index( |
e5be10ef PP |
407 | struct bt_private_trace *trace, uint64_t index) |
408 | { | |
409 | return (void *) bt_trace_borrow_stream_by_index((void *) trace, index); | |
410 | } | |
411 | ||
44c440bc PP |
412 | struct bt_stream *bt_trace_borrow_stream_by_id( |
413 | struct bt_trace *trace, uint64_t id) | |
414 | { | |
415 | struct bt_stream *stream = NULL; | |
416 | uint64_t i; | |
bc37ae52 | 417 | |
44c440bc | 418 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
bc37ae52 | 419 | |
44c440bc PP |
420 | for (i = 0; i < trace->streams->len; i++) { |
421 | struct bt_stream *stream_candidate = | |
422 | g_ptr_array_index(trace->streams, i); | |
cfeb617e | 423 | |
44c440bc PP |
424 | if (stream_candidate->id == id) { |
425 | stream = stream_candidate; | |
426 | goto end; | |
427 | } | |
6474e016 | 428 | } |
95c09b3a | 429 | |
bc37ae52 | 430 | end: |
44c440bc | 431 | return stream; |
bc37ae52 JG |
432 | } |
433 | ||
e5be10ef PP |
434 | struct bt_private_stream *bt_private_trace_borrow_private_stream_by_id( |
435 | struct bt_private_trace *trace, uint64_t id) | |
436 | { | |
437 | return (void *) bt_trace_borrow_stream_by_id((void *) trace, id); | |
438 | } | |
439 | ||
44c440bc | 440 | uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace) |
884cd6c3 | 441 | { |
cb6f1f7d | 442 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 443 | return (uint64_t) trace->stream_classes->len; |
884cd6c3 JG |
444 | } |
445 | ||
44c440bc | 446 | struct bt_stream_class *bt_trace_borrow_stream_class_by_index( |
50842bdc | 447 | struct bt_trace *trace, uint64_t index) |
884cd6c3 | 448 | { |
cb6f1f7d | 449 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc PP |
450 | BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len); |
451 | return g_ptr_array_index(trace->stream_classes, index); | |
884cd6c3 JG |
452 | } |
453 | ||
e5be10ef | 454 | struct bt_private_stream_class * |
28e6ca8b | 455 | bt_private_trace_borrow_stream_class_by_index( |
e5be10ef PP |
456 | struct bt_private_trace *trace, uint64_t index) |
457 | { | |
458 | return (void *) bt_trace_borrow_stream_class_by_index( | |
459 | (void *) trace, index); | |
460 | } | |
461 | ||
44c440bc PP |
462 | struct bt_stream_class *bt_trace_borrow_stream_class_by_id( |
463 | struct bt_trace *trace, uint64_t id) | |
e011d2c1 | 464 | { |
44c440bc PP |
465 | struct bt_stream_class *stream_class = NULL; |
466 | uint64_t i; | |
e011d2c1 | 467 | |
44c440bc | 468 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
e011d2c1 | 469 | |
44c440bc PP |
470 | for (i = 0; i < trace->stream_classes->len; i++) { |
471 | struct bt_stream_class *stream_class_candidate = | |
472 | g_ptr_array_index(trace->stream_classes, i); | |
e011d2c1 | 473 | |
44c440bc PP |
474 | if (stream_class_candidate->id == id) { |
475 | stream_class = stream_class_candidate; | |
476 | goto end; | |
e011d2c1 | 477 | } |
44c440bc | 478 | } |
e011d2c1 | 479 | |
44c440bc PP |
480 | end: |
481 | return stream_class; | |
482 | } | |
e011d2c1 | 483 | |
e5be10ef | 484 | struct bt_private_stream_class * |
28e6ca8b | 485 | bt_private_trace_borrow_stream_class_by_id( |
e5be10ef PP |
486 | struct bt_private_trace *trace, uint64_t id) |
487 | { | |
488 | return (void *) bt_trace_borrow_stream_class_by_id((void *) trace, id); | |
489 | } | |
490 | ||
5cd6d0e5 | 491 | struct bt_field_class *bt_trace_borrow_packet_header_field_class( |
44c440bc PP |
492 | struct bt_trace *trace) |
493 | { | |
494 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
5cd6d0e5 | 495 | return trace->packet_header_fc; |
44c440bc | 496 | } |
e011d2c1 | 497 | |
28e6ca8b | 498 | int bt_private_trace_set_packet_header_field_class( |
e5be10ef PP |
499 | struct bt_private_trace *priv_trace, |
500 | struct bt_private_field_class *priv_field_class) | |
44c440bc PP |
501 | { |
502 | int ret; | |
e5be10ef PP |
503 | struct bt_trace *trace = (void *) priv_trace; |
504 | struct bt_field_class *field_class = (void *) priv_field_class; | |
44c440bc | 505 | struct bt_resolve_field_path_context resolve_ctx = { |
5cd6d0e5 | 506 | .packet_header = field_class, |
44c440bc PP |
507 | .packet_context = NULL, |
508 | .event_header = NULL, | |
509 | .event_common_context = NULL, | |
510 | .event_specific_context = NULL, | |
511 | .event_payload = NULL, | |
512 | }; | |
e011d2c1 | 513 | |
44c440bc | 514 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
5cd6d0e5 | 515 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
44c440bc | 516 | BT_ASSERT_PRE_TRACE_HOT(trace); |
864cad70 PP |
517 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
518 | BT_FIELD_CLASS_TYPE_STRUCTURE, | |
5cd6d0e5 PP |
519 | "Packet header field classe is not a structure field classe: %!+F", |
520 | field_class); | |
521 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); | |
44c440bc PP |
522 | if (ret) { |
523 | goto end; | |
e011d2c1 PP |
524 | } |
525 | ||
5cd6d0e5 | 526 | bt_field_class_make_part_of_trace(field_class); |
65300d60 | 527 | bt_object_put_ref(trace->packet_header_fc); |
398454ed PP |
528 | trace->packet_header_fc = field_class; |
529 | bt_object_get_no_null_check(trace->packet_header_fc); | |
5cd6d0e5 PP |
530 | bt_field_class_freeze(field_class); |
531 | BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace); | |
e011d2c1 | 532 | |
8bf65fbd JG |
533 | end: |
534 | return ret; | |
535 | } | |
536 | ||
50842bdc | 537 | bt_bool bt_trace_is_static(struct bt_trace *trace) |
5acf2ae6 | 538 | { |
d975f66c | 539 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 540 | return (bt_bool) trace->is_static; |
5acf2ae6 PP |
541 | } |
542 | ||
e5be10ef | 543 | int bt_private_trace_make_static(struct bt_private_trace *priv_trace) |
5acf2ae6 | 544 | { |
e5be10ef | 545 | struct bt_trace *trace = (void *) priv_trace; |
44c440bc | 546 | uint64_t i; |
726106d3 | 547 | |
44c440bc PP |
548 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
549 | trace->is_static = true; | |
cb6f1f7d | 550 | bt_trace_freeze(trace); |
44c440bc | 551 | BT_LIB_LOGV("Trace is now static: %!+t", trace); |
5acf2ae6 | 552 | |
3602afb0 PP |
553 | /* Call all the "trace is static" listeners */ |
554 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
50842bdc | 555 | struct bt_trace_is_static_listener_elem elem = |
3602afb0 | 556 | g_array_index(trace->is_static_listeners, |
50842bdc | 557 | struct bt_trace_is_static_listener_elem, i); |
3602afb0 PP |
558 | |
559 | if (elem.func) { | |
e5be10ef | 560 | elem.func((void *) trace, elem.data); |
3602afb0 PP |
561 | } |
562 | } | |
563 | ||
44c440bc | 564 | return 0; |
3602afb0 PP |
565 | } |
566 | ||
e5be10ef PP |
567 | int bt_private_trace_add_is_static_listener( |
568 | struct bt_private_trace *priv_trace, | |
569 | bt_private_trace_is_static_listener listener, | |
570 | bt_private_trace_listener_removed listener_removed, void *data, | |
44c440bc | 571 | uint64_t *listener_id) |
3602afb0 | 572 | { |
e5be10ef | 573 | struct bt_trace *trace = (void *) priv_trace; |
44c440bc | 574 | uint64_t i; |
50842bdc | 575 | struct bt_trace_is_static_listener_elem new_elem = { |
3602afb0 | 576 | .func = listener, |
8480c8cc | 577 | .removed = listener_removed, |
3602afb0 PP |
578 | .data = data, |
579 | }; | |
580 | ||
44c440bc PP |
581 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
582 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
583 | BT_ASSERT_PRE(!trace->is_static, | |
584 | "Trace is already static: %!+t", trace); | |
585 | BT_ASSERT_PRE(trace->in_remove_listener, | |
586 | "Cannot call this function while executing a " | |
587 | "remove listener: %!+t", trace); | |
8480c8cc | 588 | |
3602afb0 PP |
589 | /* Find the next available spot */ |
590 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
50842bdc | 591 | struct bt_trace_is_static_listener_elem elem = |
3602afb0 | 592 | g_array_index(trace->is_static_listeners, |
50842bdc | 593 | struct bt_trace_is_static_listener_elem, i); |
3602afb0 PP |
594 | |
595 | if (!elem.func) { | |
596 | break; | |
597 | } | |
598 | } | |
599 | ||
600 | if (i == trace->is_static_listeners->len) { | |
601 | g_array_append_val(trace->is_static_listeners, new_elem); | |
602 | } else { | |
603 | g_array_insert_val(trace->is_static_listeners, i, new_elem); | |
604 | } | |
605 | ||
44c440bc PP |
606 | if (listener_id) { |
607 | *listener_id = i; | |
608 | } | |
3602afb0 | 609 | |
44c440bc PP |
610 | BT_LIB_LOGV("Added \"trace is static\" listener: " |
611 | "%![trace-]+t, listener-id=%" PRIu64, trace, i); | |
612 | return 0; | |
613 | } | |
614 | ||
615 | BT_ASSERT_PRE_FUNC | |
616 | static | |
617 | bool has_listener_id(struct bt_trace *trace, uint64_t listener_id) | |
618 | { | |
619 | BT_ASSERT(listener_id < trace->is_static_listeners->len); | |
620 | return (&g_array_index(trace->is_static_listeners, | |
621 | struct bt_trace_is_static_listener_elem, | |
622 | listener_id))->func != NULL; | |
3602afb0 PP |
623 | } |
624 | ||
e5be10ef PP |
625 | int bt_private_trace_remove_is_static_listener( |
626 | struct bt_private_trace *priv_trace, uint64_t listener_id) | |
3602afb0 | 627 | { |
e5be10ef | 628 | struct bt_trace *trace = (void *) priv_trace; |
50842bdc | 629 | struct bt_trace_is_static_listener_elem *elem; |
3602afb0 | 630 | |
44c440bc PP |
631 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
632 | BT_ASSERT_PRE(!trace->is_static, | |
633 | "Trace is already static: %!+t", trace); | |
634 | BT_ASSERT_PRE(trace->in_remove_listener, | |
635 | "Cannot call this function while executing a " | |
636 | "remove listener: %!+t", trace); | |
637 | BT_ASSERT_PRE(has_listener_id(trace, listener_id), | |
638 | "Trace has no such \"trace is static\" listener ID: " | |
639 | "%![trace-]+t, %" PRIu64, trace, listener_id); | |
3602afb0 | 640 | elem = &g_array_index(trace->is_static_listeners, |
50842bdc | 641 | struct bt_trace_is_static_listener_elem, |
3602afb0 | 642 | listener_id); |
44c440bc | 643 | BT_ASSERT(elem->func); |
3602afb0 | 644 | |
8480c8cc PP |
645 | if (elem->removed) { |
646 | /* Call remove listener */ | |
44c440bc PP |
647 | BT_LIB_LOGV("Calling remove listener: " |
648 | "%![trace-]+t, listener-id=%" PRIu64, | |
649 | trace, listener_id); | |
650 | trace->in_remove_listener = true; | |
e5be10ef | 651 | elem->removed((void *) trace, elem->data); |
44c440bc | 652 | trace->in_remove_listener = false; |
8480c8cc PP |
653 | } |
654 | ||
3602afb0 | 655 | elem->func = NULL; |
8480c8cc | 656 | elem->removed = NULL; |
3602afb0 | 657 | elem->data = NULL; |
44c440bc PP |
658 | BT_LIB_LOGV("Removed \"trace is static\" listener: " |
659 | "%![trace-]+t, listener-id=%" PRIu64, | |
660 | trace, listener_id); | |
661 | return 0; | |
662 | } | |
3602afb0 | 663 | |
44c440bc PP |
664 | BT_HIDDEN |
665 | void _bt_trace_freeze(struct bt_trace *trace) | |
666 | { | |
5cd6d0e5 | 667 | /* The packet header field classe is already frozen */ |
44c440bc PP |
668 | BT_ASSERT(trace); |
669 | BT_LIB_LOGD("Freezing trace: %!+t", trace); | |
670 | trace->frozen = true; | |
5acf2ae6 | 671 | } |
312c056a | 672 | |
44c440bc | 673 | bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace) |
312c056a | 674 | { |
44c440bc PP |
675 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
676 | return (bt_bool) trace->assigns_automatic_stream_class_id; | |
677 | } | |
312c056a | 678 | |
140e6d94 | 679 | void bt_private_trace_set_assigns_automatic_stream_class_id( |
e5be10ef | 680 | struct bt_private_trace *priv_trace, bt_bool value) |
44c440bc | 681 | { |
e5be10ef PP |
682 | struct bt_trace *trace = (void *) priv_trace; |
683 | ||
312c056a | 684 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc PP |
685 | BT_ASSERT_PRE_TRACE_HOT(trace); |
686 | trace->assigns_automatic_stream_class_id = (bool) value; | |
687 | BT_LIB_LOGV("Set trace's automatic stream class ID " | |
688 | "assignment property: %!+t", trace); | |
44c440bc | 689 | } |
312c056a | 690 | |
44c440bc PP |
691 | BT_HIDDEN |
692 | void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream) | |
693 | { | |
694 | guint count = 0; | |
695 | ||
696 | bt_object_set_parent(&stream->base, &trace->base); | |
697 | g_ptr_array_add(trace->streams, stream); | |
cb6f1f7d | 698 | bt_trace_freeze(trace); |
312c056a | 699 | |
44c440bc PP |
700 | if (bt_g_hash_table_contains(trace->stream_classes_stream_count, |
701 | stream->class)) { | |
702 | count = GPOINTER_TO_UINT(g_hash_table_lookup( | |
703 | trace->stream_classes_stream_count, stream->class)); | |
312c056a PP |
704 | } |
705 | ||
44c440bc PP |
706 | g_hash_table_insert(trace->stream_classes_stream_count, |
707 | stream->class, GUINT_TO_POINTER(count + 1)); | |
708 | } | |
709 | ||
710 | BT_HIDDEN | |
711 | uint64_t bt_trace_get_automatic_stream_id(struct bt_trace *trace, | |
712 | struct bt_stream_class *stream_class) | |
713 | { | |
714 | gpointer orig_key; | |
715 | gpointer value; | |
716 | uint64_t id = 0; | |
717 | ||
718 | BT_ASSERT(stream_class); | |
719 | BT_ASSERT(trace); | |
720 | if (g_hash_table_lookup_extended(trace->stream_classes_stream_count, | |
721 | stream_class, &orig_key, &value)) { | |
722 | id = (uint64_t) GPOINTER_TO_UINT(value); | |
723 | } | |
724 | ||
725 | return id; | |
312c056a | 726 | } |