| 1 | /* |
| 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 | |
| 25 | #define BT_LOG_TAG "TRACE" |
| 26 | #include <babeltrace/lib-logging-internal.h> |
| 27 | |
| 28 | #include <babeltrace/assert-pre-internal.h> |
| 29 | #include <babeltrace/trace-ir/trace-const.h> |
| 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> |
| 37 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 38 | #include <babeltrace/ctf-writer/clock-internal.h> |
| 39 | #include <babeltrace/trace-ir/field-wrapper-internal.h> |
| 40 | #include <babeltrace/trace-ir/field-classes-internal.h> |
| 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> |
| 44 | #include <babeltrace/compiler-internal.h> |
| 45 | #include <babeltrace/values.h> |
| 46 | #include <babeltrace/values-const.h> |
| 47 | #include <babeltrace/values-internal.h> |
| 48 | #include <babeltrace/object.h> |
| 49 | #include <babeltrace/types.h> |
| 50 | #include <babeltrace/endian-internal.h> |
| 51 | #include <babeltrace/assert-internal.h> |
| 52 | #include <babeltrace/compat/glib-internal.h> |
| 53 | #include <inttypes.h> |
| 54 | #include <stdint.h> |
| 55 | #include <string.h> |
| 56 | #include <stdlib.h> |
| 57 | |
| 58 | struct bt_trace_is_static_listener_elem { |
| 59 | bt_trace_is_static_listener_func func; |
| 60 | bt_trace_listener_removed_func removed; |
| 61 | void *data; |
| 62 | }; |
| 63 | |
| 64 | #define BT_ASSERT_PRE_TRACE_HOT(_trace) \ |
| 65 | BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace)) |
| 66 | |
| 67 | static |
| 68 | void destroy_trace(struct bt_object *obj) |
| 69 | { |
| 70 | struct bt_trace *trace = (void *) obj; |
| 71 | |
| 72 | BT_LIB_LOGD("Destroying trace object: %!+t", trace); |
| 73 | |
| 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; |
| 80 | |
| 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); |
| 85 | |
| 86 | if (elem.removed) { |
| 87 | elem.removed((void *) trace, elem.data); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | g_array_free(trace->is_static_listeners, TRUE); |
| 92 | trace->is_static_listeners = NULL; |
| 93 | } |
| 94 | |
| 95 | bt_object_pool_finalize(&trace->packet_header_field_pool); |
| 96 | |
| 97 | if (trace->environment) { |
| 98 | BT_LOGD_STR("Destroying environment attributes."); |
| 99 | bt_attributes_destroy(trace->environment); |
| 100 | trace->environment = NULL; |
| 101 | } |
| 102 | |
| 103 | if (trace->name.str) { |
| 104 | g_string_free(trace->name.str, TRUE); |
| 105 | trace->name.str = NULL; |
| 106 | trace->name.value = NULL; |
| 107 | } |
| 108 | |
| 109 | if (trace->streams) { |
| 110 | BT_LOGD_STR("Destroying streams."); |
| 111 | g_ptr_array_free(trace->streams, TRUE); |
| 112 | trace->streams = NULL; |
| 113 | } |
| 114 | |
| 115 | if (trace->stream_classes) { |
| 116 | BT_LOGD_STR("Destroying stream classes."); |
| 117 | g_ptr_array_free(trace->stream_classes, TRUE); |
| 118 | trace->stream_classes = NULL; |
| 119 | } |
| 120 | |
| 121 | if (trace->stream_classes_stream_count) { |
| 122 | g_hash_table_destroy(trace->stream_classes_stream_count); |
| 123 | trace->stream_classes_stream_count = NULL; |
| 124 | } |
| 125 | |
| 126 | BT_LOGD_STR("Putting packet header field classe."); |
| 127 | bt_object_put_ref(trace->packet_header_fc); |
| 128 | trace->packet_header_fc = NULL; |
| 129 | g_free(trace); |
| 130 | } |
| 131 | |
| 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 | |
| 139 | struct bt_trace *bt_trace_create(void) |
| 140 | { |
| 141 | struct bt_trace *trace = NULL; |
| 142 | int ret; |
| 143 | |
| 144 | BT_LOGD_STR("Creating default trace object."); |
| 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 | |
| 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."); |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | trace->stream_classes = g_ptr_array_new_with_free_func( |
| 160 | (GDestroyNotify) bt_object_try_spec_release); |
| 161 | if (!trace->stream_classes) { |
| 162 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 163 | goto error; |
| 164 | } |
| 165 | |
| 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 | |
| 185 | trace->is_static_listeners = g_array_new(FALSE, TRUE, |
| 186 | sizeof(struct bt_trace_is_static_listener_elem)); |
| 187 | if (!trace->is_static_listeners) { |
| 188 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | trace->assigns_automatic_stream_class_id = true; |
| 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 | |
| 203 | BT_LIB_LOGD("Created trace object: %!+t", trace); |
| 204 | goto end; |
| 205 | |
| 206 | error: |
| 207 | BT_OBJECT_PUT_REF_AND_RESET(trace); |
| 208 | |
| 209 | end: |
| 210 | return (void *) trace; |
| 211 | } |
| 212 | |
| 213 | const char *bt_trace_get_name(const struct bt_trace *trace) |
| 214 | { |
| 215 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 216 | return trace->name.value; |
| 217 | } |
| 218 | |
| 219 | int bt_trace_set_name(struct bt_trace *trace, const char *name) |
| 220 | { |
| 221 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 222 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 223 | BT_ASSERT_PRE_TRACE_HOT(trace); |
| 224 | g_string_assign(trace->name.str, name); |
| 225 | trace->name.value = trace->name.str->str; |
| 226 | BT_LIB_LOGV("Set trace's name: %!+t", trace); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | bt_uuid bt_trace_get_uuid(const struct bt_trace *trace) |
| 231 | { |
| 232 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 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"); |
| 240 | BT_ASSERT_PRE_TRACE_HOT(trace); |
| 241 | memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN); |
| 242 | trace->uuid.value = trace->uuid.uuid; |
| 243 | BT_LIB_LOGV("Set trace's UUID: %!+t", trace); |
| 244 | } |
| 245 | |
| 246 | BT_ASSERT_FUNC |
| 247 | static |
| 248 | bool trace_has_environment_entry(const struct bt_trace *trace, const char *name) |
| 249 | { |
| 250 | BT_ASSERT(trace); |
| 251 | |
| 252 | return bt_attributes_borrow_field_value_by_name( |
| 253 | trace->environment, name) != NULL; |
| 254 | } |
| 255 | |
| 256 | static |
| 257 | int set_environment_entry(struct bt_trace *trace, const char *name, |
| 258 | 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 | bt_value_freeze(value); |
| 272 | if (ret) { |
| 273 | BT_LIB_LOGE("Cannot set trace's environment entry: " |
| 274 | "%![trace-]+t, entry-name=\"%s\"", trace, name); |
| 275 | } else { |
| 276 | BT_LIB_LOGV("Set trace's environment entry: " |
| 277 | "%![trace-]+t, entry-name=\"%s\"", trace, name); |
| 278 | } |
| 279 | |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | int bt_trace_set_environment_entry_string( |
| 284 | struct bt_trace *trace, |
| 285 | const char *name, const char *value) |
| 286 | { |
| 287 | int ret; |
| 288 | struct bt_value *value_obj; |
| 289 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 290 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 291 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
| 292 | value_obj = bt_value_string_create_init(value); |
| 293 | if (!value_obj) { |
| 294 | BT_LOGE_STR("Cannot create a string value object."); |
| 295 | ret = -1; |
| 296 | goto end; |
| 297 | } |
| 298 | |
| 299 | /* set_environment_entry() logs errors */ |
| 300 | ret = set_environment_entry(trace, name, value_obj); |
| 301 | |
| 302 | end: |
| 303 | bt_object_put_ref(value_obj); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | int bt_trace_set_environment_entry_integer(struct bt_trace *trace, |
| 308 | const char *name, int64_t value) |
| 309 | { |
| 310 | int ret; |
| 311 | struct bt_value *value_obj; |
| 312 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 313 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 314 | value_obj = bt_value_integer_create_init(value); |
| 315 | if (!value_obj) { |
| 316 | BT_LOGE_STR("Cannot create an integer value object."); |
| 317 | ret = -1; |
| 318 | goto end; |
| 319 | } |
| 320 | |
| 321 | /* set_environment_entry() logs errors */ |
| 322 | ret = set_environment_entry(trace, name, value_obj); |
| 323 | |
| 324 | end: |
| 325 | bt_object_put_ref(value_obj); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace) |
| 330 | { |
| 331 | int64_t ret; |
| 332 | |
| 333 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 334 | ret = bt_attributes_get_count(trace->environment); |
| 335 | BT_ASSERT(ret >= 0); |
| 336 | return (uint64_t) ret; |
| 337 | } |
| 338 | |
| 339 | void bt_trace_borrow_environment_entry_by_index_const( |
| 340 | const struct bt_trace *trace, uint64_t index, |
| 341 | const char **name, const struct bt_value **value) |
| 342 | { |
| 343 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 344 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 345 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
| 346 | BT_ASSERT_PRE_VALID_INDEX(index, |
| 347 | bt_attributes_get_count(trace->environment)); |
| 348 | *value = bt_attributes_borrow_field_value(trace->environment, index); |
| 349 | BT_ASSERT(*value); |
| 350 | *name = bt_attributes_get_field_name(trace->environment, index); |
| 351 | BT_ASSERT(*name); |
| 352 | } |
| 353 | |
| 354 | const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const( |
| 355 | const struct bt_trace *trace, const char *name) |
| 356 | { |
| 357 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 358 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 359 | return bt_attributes_borrow_field_value_by_name(trace->environment, |
| 360 | name); |
| 361 | } |
| 362 | |
| 363 | uint64_t bt_trace_get_stream_count(const struct bt_trace *trace) |
| 364 | { |
| 365 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 366 | return (uint64_t) trace->streams->len; |
| 367 | } |
| 368 | |
| 369 | struct bt_stream *bt_trace_borrow_stream_by_index( |
| 370 | struct bt_trace *trace, uint64_t index) |
| 371 | { |
| 372 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 373 | BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len); |
| 374 | return g_ptr_array_index(trace->streams, index); |
| 375 | } |
| 376 | |
| 377 | const struct bt_stream *bt_trace_borrow_stream_by_index_const( |
| 378 | const struct bt_trace *trace, uint64_t index) |
| 379 | { |
| 380 | return bt_trace_borrow_stream_by_index((void *) trace, index); |
| 381 | } |
| 382 | |
| 383 | struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace, |
| 384 | uint64_t id) |
| 385 | { |
| 386 | struct bt_stream *stream = NULL; |
| 387 | uint64_t i; |
| 388 | |
| 389 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 390 | |
| 391 | for (i = 0; i < trace->streams->len; i++) { |
| 392 | struct bt_stream *stream_candidate = |
| 393 | g_ptr_array_index(trace->streams, i); |
| 394 | |
| 395 | if (stream_candidate->id == id) { |
| 396 | stream = stream_candidate; |
| 397 | goto end; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | end: |
| 402 | return stream; |
| 403 | } |
| 404 | |
| 405 | const struct bt_stream *bt_trace_borrow_stream_by_id_const( |
| 406 | const struct bt_trace *trace, uint64_t id) |
| 407 | { |
| 408 | return bt_trace_borrow_stream_by_id((void *) trace, id); |
| 409 | } |
| 410 | |
| 411 | uint64_t bt_trace_get_stream_class_count(const struct bt_trace *trace) |
| 412 | { |
| 413 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 414 | return (uint64_t) trace->stream_classes->len; |
| 415 | } |
| 416 | |
| 417 | struct bt_stream_class *bt_trace_borrow_stream_class_by_index( |
| 418 | struct bt_trace *trace, uint64_t index) |
| 419 | { |
| 420 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 421 | BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len); |
| 422 | return g_ptr_array_index(trace->stream_classes, index); |
| 423 | } |
| 424 | |
| 425 | const struct bt_stream_class * |
| 426 | bt_trace_borrow_stream_class_by_index_const( |
| 427 | const struct bt_trace *trace, uint64_t index) |
| 428 | { |
| 429 | return bt_trace_borrow_stream_class_by_index( |
| 430 | (void *) trace, index); |
| 431 | } |
| 432 | |
| 433 | struct bt_stream_class *bt_trace_borrow_stream_class_by_id( |
| 434 | struct bt_trace *trace, uint64_t id) |
| 435 | { |
| 436 | struct bt_stream_class *stream_class = NULL; |
| 437 | uint64_t i; |
| 438 | |
| 439 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 440 | |
| 441 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 442 | struct bt_stream_class *stream_class_candidate = |
| 443 | g_ptr_array_index(trace->stream_classes, i); |
| 444 | |
| 445 | if (stream_class_candidate->id == id) { |
| 446 | stream_class = stream_class_candidate; |
| 447 | goto end; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | end: |
| 452 | return stream_class; |
| 453 | } |
| 454 | |
| 455 | const struct bt_stream_class * |
| 456 | bt_trace_borrow_stream_class_by_id_const( |
| 457 | const struct bt_trace *trace, uint64_t id) |
| 458 | { |
| 459 | return bt_trace_borrow_stream_class_by_id((void *) trace, id); |
| 460 | } |
| 461 | |
| 462 | const struct bt_field_class *bt_trace_borrow_packet_header_field_class_const( |
| 463 | const struct bt_trace *trace) |
| 464 | { |
| 465 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 466 | return trace->packet_header_fc; |
| 467 | } |
| 468 | |
| 469 | int bt_trace_set_packet_header_field_class( |
| 470 | struct bt_trace *trace, |
| 471 | struct bt_field_class *field_class) |
| 472 | { |
| 473 | int ret; |
| 474 | struct bt_resolve_field_path_context resolve_ctx = { |
| 475 | .packet_header = field_class, |
| 476 | .packet_context = NULL, |
| 477 | .event_header = NULL, |
| 478 | .event_common_context = NULL, |
| 479 | .event_specific_context = NULL, |
| 480 | .event_payload = NULL, |
| 481 | }; |
| 482 | |
| 483 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 484 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
| 485 | BT_ASSERT_PRE_TRACE_HOT(trace); |
| 486 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
| 487 | BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 488 | "Packet header field classe is not a structure field classe: %!+F", |
| 489 | field_class); |
| 490 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); |
| 491 | if (ret) { |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | bt_field_class_make_part_of_trace(field_class); |
| 496 | bt_object_put_ref(trace->packet_header_fc); |
| 497 | trace->packet_header_fc = field_class; |
| 498 | bt_object_get_no_null_check(trace->packet_header_fc); |
| 499 | bt_field_class_freeze(field_class); |
| 500 | BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace); |
| 501 | |
| 502 | end: |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | bt_bool bt_trace_is_static(const struct bt_trace *trace) |
| 507 | { |
| 508 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 509 | return (bt_bool) trace->is_static; |
| 510 | } |
| 511 | |
| 512 | int bt_trace_make_static(struct bt_trace *trace) |
| 513 | { uint64_t i; |
| 514 | |
| 515 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 516 | trace->is_static = true; |
| 517 | bt_trace_freeze(trace); |
| 518 | BT_LIB_LOGV("Trace is now static: %!+t", trace); |
| 519 | |
| 520 | /* Call all the "trace is static" listeners */ |
| 521 | for (i = 0; i < trace->is_static_listeners->len; i++) { |
| 522 | struct bt_trace_is_static_listener_elem elem = |
| 523 | g_array_index(trace->is_static_listeners, |
| 524 | struct bt_trace_is_static_listener_elem, i); |
| 525 | |
| 526 | if (elem.func) { |
| 527 | elem.func((void *) trace, elem.data); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | int bt_trace_add_is_static_listener( |
| 535 | const struct bt_trace *c_trace, |
| 536 | bt_trace_is_static_listener_func listener, |
| 537 | bt_trace_listener_removed_func listener_removed, void *data, |
| 538 | uint64_t *listener_id) |
| 539 | { |
| 540 | struct bt_trace *trace = (void *) c_trace; |
| 541 | uint64_t i; |
| 542 | struct bt_trace_is_static_listener_elem new_elem = { |
| 543 | .func = listener, |
| 544 | .removed = listener_removed, |
| 545 | .data = data, |
| 546 | }; |
| 547 | |
| 548 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 549 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); |
| 550 | BT_ASSERT_PRE(!trace->is_static, |
| 551 | "Trace is already static: %!+t", trace); |
| 552 | BT_ASSERT_PRE(trace->in_remove_listener, |
| 553 | "Cannot call this function while executing a " |
| 554 | "remove listener: %!+t", trace); |
| 555 | |
| 556 | /* Find the next available spot */ |
| 557 | for (i = 0; i < trace->is_static_listeners->len; i++) { |
| 558 | struct bt_trace_is_static_listener_elem elem = |
| 559 | g_array_index(trace->is_static_listeners, |
| 560 | struct bt_trace_is_static_listener_elem, i); |
| 561 | |
| 562 | if (!elem.func) { |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (i == trace->is_static_listeners->len) { |
| 568 | g_array_append_val(trace->is_static_listeners, new_elem); |
| 569 | } else { |
| 570 | g_array_insert_val(trace->is_static_listeners, i, new_elem); |
| 571 | } |
| 572 | |
| 573 | if (listener_id) { |
| 574 | *listener_id = i; |
| 575 | } |
| 576 | |
| 577 | BT_LIB_LOGV("Added \"trace is static\" listener: " |
| 578 | "%![trace-]+t, listener-id=%" PRIu64, trace, i); |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | BT_ASSERT_PRE_FUNC |
| 583 | static |
| 584 | bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id) |
| 585 | { |
| 586 | BT_ASSERT(listener_id < trace->is_static_listeners->len); |
| 587 | return (&g_array_index(trace->is_static_listeners, |
| 588 | struct bt_trace_is_static_listener_elem, |
| 589 | listener_id))->func != NULL; |
| 590 | } |
| 591 | |
| 592 | int bt_trace_remove_is_static_listener(const struct bt_trace *c_trace, |
| 593 | uint64_t listener_id) |
| 594 | { |
| 595 | struct bt_trace *trace = (void *) c_trace; |
| 596 | struct bt_trace_is_static_listener_elem *elem; |
| 597 | |
| 598 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 599 | BT_ASSERT_PRE(!trace->is_static, |
| 600 | "Trace is already static: %!+t", trace); |
| 601 | BT_ASSERT_PRE(trace->in_remove_listener, |
| 602 | "Cannot call this function while executing a " |
| 603 | "remove listener: %!+t", trace); |
| 604 | BT_ASSERT_PRE(has_listener_id(trace, listener_id), |
| 605 | "Trace has no such \"trace is static\" listener ID: " |
| 606 | "%![trace-]+t, %" PRIu64, trace, listener_id); |
| 607 | elem = &g_array_index(trace->is_static_listeners, |
| 608 | struct bt_trace_is_static_listener_elem, |
| 609 | listener_id); |
| 610 | BT_ASSERT(elem->func); |
| 611 | |
| 612 | if (elem->removed) { |
| 613 | /* Call remove listener */ |
| 614 | BT_LIB_LOGV("Calling remove listener: " |
| 615 | "%![trace-]+t, listener-id=%" PRIu64, |
| 616 | trace, listener_id); |
| 617 | trace->in_remove_listener = true; |
| 618 | elem->removed((void *) trace, elem->data); |
| 619 | trace->in_remove_listener = false; |
| 620 | } |
| 621 | |
| 622 | elem->func = NULL; |
| 623 | elem->removed = NULL; |
| 624 | elem->data = NULL; |
| 625 | BT_LIB_LOGV("Removed \"trace is static\" listener: " |
| 626 | "%![trace-]+t, listener-id=%" PRIu64, |
| 627 | trace, listener_id); |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | BT_HIDDEN |
| 632 | void _bt_trace_freeze(const struct bt_trace *trace) |
| 633 | { |
| 634 | /* The packet header field classe is already frozen */ |
| 635 | BT_ASSERT(trace); |
| 636 | BT_LIB_LOGD("Freezing trace: %!+t", trace); |
| 637 | ((struct bt_trace *) trace)->frozen = true; |
| 638 | } |
| 639 | |
| 640 | bt_bool bt_trace_assigns_automatic_stream_class_id(const struct bt_trace *trace) |
| 641 | { |
| 642 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 643 | return (bt_bool) trace->assigns_automatic_stream_class_id; |
| 644 | } |
| 645 | |
| 646 | void bt_trace_set_assigns_automatic_stream_class_id(struct bt_trace *trace, |
| 647 | bt_bool value) |
| 648 | { |
| 649 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 650 | BT_ASSERT_PRE_TRACE_HOT(trace); |
| 651 | trace->assigns_automatic_stream_class_id = (bool) value; |
| 652 | BT_LIB_LOGV("Set trace's automatic stream class ID " |
| 653 | "assignment property: %!+t", trace); |
| 654 | } |
| 655 | |
| 656 | BT_HIDDEN |
| 657 | void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream) |
| 658 | { |
| 659 | guint count = 0; |
| 660 | |
| 661 | bt_object_set_parent(&stream->base, &trace->base); |
| 662 | g_ptr_array_add(trace->streams, stream); |
| 663 | bt_trace_freeze(trace); |
| 664 | |
| 665 | if (bt_g_hash_table_contains(trace->stream_classes_stream_count, |
| 666 | stream->class)) { |
| 667 | count = GPOINTER_TO_UINT(g_hash_table_lookup( |
| 668 | trace->stream_classes_stream_count, stream->class)); |
| 669 | } |
| 670 | |
| 671 | g_hash_table_insert(trace->stream_classes_stream_count, |
| 672 | stream->class, GUINT_TO_POINTER(count + 1)); |
| 673 | } |
| 674 | |
| 675 | BT_HIDDEN |
| 676 | uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace, |
| 677 | const struct bt_stream_class *stream_class) |
| 678 | { |
| 679 | gpointer orig_key; |
| 680 | gpointer value; |
| 681 | uint64_t id = 0; |
| 682 | |
| 683 | BT_ASSERT(stream_class); |
| 684 | BT_ASSERT(trace); |
| 685 | if (g_hash_table_lookup_extended(trace->stream_classes_stream_count, |
| 686 | stream_class, &orig_key, &value)) { |
| 687 | id = (uint64_t) GPOINTER_TO_UINT(value); |
| 688 | } |
| 689 | |
| 690 | return id; |
| 691 | } |