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