| 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/private-trace.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/private-values.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_private_trace_is_static_listener func; |
| 60 | bt_private_trace_listener_removed 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_private_trace *bt_private_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(struct bt_trace *trace) |
| 214 | { |
| 215 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 216 | return trace->name.value; |
| 217 | } |
| 218 | |
| 219 | int bt_private_trace_set_name(struct bt_private_trace *priv_trace, |
| 220 | const char *name) |
| 221 | { |
| 222 | struct bt_trace *trace = (void *) priv_trace; |
| 223 | |
| 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; |
| 231 | } |
| 232 | |
| 233 | bt_uuid bt_trace_get_uuid(struct bt_trace *trace) |
| 234 | { |
| 235 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 236 | return trace->uuid.value; |
| 237 | } |
| 238 | |
| 239 | void bt_private_trace_set_uuid(struct bt_private_trace *priv_trace, |
| 240 | bt_uuid uuid) |
| 241 | { |
| 242 | struct bt_trace *trace = (void *) priv_trace; |
| 243 | |
| 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); |
| 250 | } |
| 251 | |
| 252 | BT_ASSERT_FUNC |
| 253 | static |
| 254 | bool trace_has_environment_entry(struct bt_trace *trace, const char *name) |
| 255 | { |
| 256 | BT_ASSERT(trace); |
| 257 | |
| 258 | return bt_attributes_borrow_field_value_by_name( |
| 259 | trace->environment, name) != NULL; |
| 260 | } |
| 261 | |
| 262 | static |
| 263 | int set_environment_entry(struct bt_trace *trace, const char *name, |
| 264 | struct bt_private_value *value) |
| 265 | { |
| 266 | int ret; |
| 267 | |
| 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); |
| 275 | ret = bt_attributes_set_field_value(trace->environment, name, |
| 276 | value); |
| 277 | bt_value_freeze(bt_private_value_borrow_value(value)); |
| 278 | if (ret) { |
| 279 | BT_LIB_LOGE("Cannot set trace's environment entry: " |
| 280 | "%![trace-]+t, entry-name=\"%s\"", trace, name); |
| 281 | } else { |
| 282 | BT_LIB_LOGV("Set trace's environment entry: " |
| 283 | "%![trace-]+t, entry-name=\"%s\"", trace, name); |
| 284 | } |
| 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | int bt_private_trace_set_environment_entry_string( |
| 290 | struct bt_private_trace *priv_trace, |
| 291 | const char *name, const char *value) |
| 292 | { |
| 293 | int ret; |
| 294 | struct bt_private_value *value_obj; |
| 295 | struct bt_trace *trace = (void *) priv_trace; |
| 296 | |
| 297 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 298 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 299 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
| 300 | value_obj = bt_private_value_string_create_init(value); |
| 301 | if (!value_obj) { |
| 302 | BT_LOGE_STR("Cannot create a string value object."); |
| 303 | ret = -1; |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | /* set_environment_entry() logs errors */ |
| 308 | ret = set_environment_entry(trace, name, value_obj); |
| 309 | |
| 310 | end: |
| 311 | bt_object_put_ref(value_obj); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | int bt_private_trace_set_environment_entry_integer( |
| 316 | struct bt_private_trace *priv_trace, |
| 317 | const char *name, int64_t value) |
| 318 | { |
| 319 | int ret; |
| 320 | struct bt_private_value *value_obj; |
| 321 | struct bt_trace *trace = (void *) priv_trace; |
| 322 | |
| 323 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 324 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 325 | value_obj = bt_private_value_integer_create_init(value); |
| 326 | if (!value_obj) { |
| 327 | BT_LOGE_STR("Cannot create an integer value object."); |
| 328 | ret = -1; |
| 329 | goto end; |
| 330 | } |
| 331 | |
| 332 | /* set_environment_entry() logs errors */ |
| 333 | ret = set_environment_entry(trace, name, value_obj); |
| 334 | |
| 335 | end: |
| 336 | bt_object_put_ref(value_obj); |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace) |
| 341 | { |
| 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); |
| 347 | return (uint64_t) ret; |
| 348 | } |
| 349 | |
| 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) |
| 353 | { |
| 354 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 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)); |
| 359 | *value = bt_private_value_borrow_value( |
| 360 | bt_attributes_borrow_field_value(trace->environment, index)); |
| 361 | BT_ASSERT(*value); |
| 362 | *name = bt_attributes_get_field_name(trace->environment, index); |
| 363 | BT_ASSERT(*name); |
| 364 | } |
| 365 | |
| 366 | void bt_private_trace_borrow_environment_entry_by_index( |
| 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 | |
| 374 | struct bt_value *bt_trace_borrow_environment_entry_value_by_name( |
| 375 | struct bt_trace *trace, const char *name) |
| 376 | { |
| 377 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 378 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 379 | return bt_private_value_borrow_value( |
| 380 | bt_attributes_borrow_field_value_by_name(trace->environment, |
| 381 | name)); |
| 382 | } |
| 383 | |
| 384 | struct bt_private_value * |
| 385 | bt_private_trace_borrow_environment_entry_value_by_name( |
| 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 | |
| 392 | uint64_t bt_trace_get_stream_count(struct bt_trace *trace) |
| 393 | { |
| 394 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 395 | return (uint64_t) trace->streams->len; |
| 396 | } |
| 397 | |
| 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 | } |
| 405 | |
| 406 | struct bt_private_stream *bt_private_trace_borrow_stream_by_index( |
| 407 | struct bt_private_trace *trace, uint64_t index) |
| 408 | { |
| 409 | return (void *) bt_trace_borrow_stream_by_index((void *) trace, index); |
| 410 | } |
| 411 | |
| 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; |
| 417 | |
| 418 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 419 | |
| 420 | for (i = 0; i < trace->streams->len; i++) { |
| 421 | struct bt_stream *stream_candidate = |
| 422 | g_ptr_array_index(trace->streams, i); |
| 423 | |
| 424 | if (stream_candidate->id == id) { |
| 425 | stream = stream_candidate; |
| 426 | goto end; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | end: |
| 431 | return stream; |
| 432 | } |
| 433 | |
| 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 | |
| 440 | uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace) |
| 441 | { |
| 442 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 443 | return (uint64_t) trace->stream_classes->len; |
| 444 | } |
| 445 | |
| 446 | struct bt_stream_class *bt_trace_borrow_stream_class_by_index( |
| 447 | struct bt_trace *trace, uint64_t index) |
| 448 | { |
| 449 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 450 | BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len); |
| 451 | return g_ptr_array_index(trace->stream_classes, index); |
| 452 | } |
| 453 | |
| 454 | struct bt_private_stream_class * |
| 455 | bt_private_trace_borrow_stream_class_by_index( |
| 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 | |
| 462 | struct bt_stream_class *bt_trace_borrow_stream_class_by_id( |
| 463 | struct bt_trace *trace, uint64_t id) |
| 464 | { |
| 465 | struct bt_stream_class *stream_class = NULL; |
| 466 | uint64_t i; |
| 467 | |
| 468 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 469 | |
| 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); |
| 473 | |
| 474 | if (stream_class_candidate->id == id) { |
| 475 | stream_class = stream_class_candidate; |
| 476 | goto end; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | end: |
| 481 | return stream_class; |
| 482 | } |
| 483 | |
| 484 | struct bt_private_stream_class * |
| 485 | bt_private_trace_borrow_stream_class_by_id( |
| 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 | |
| 491 | struct bt_field_class *bt_trace_borrow_packet_header_field_class( |
| 492 | struct bt_trace *trace) |
| 493 | { |
| 494 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 495 | return trace->packet_header_fc; |
| 496 | } |
| 497 | |
| 498 | int bt_private_trace_set_packet_header_field_class( |
| 499 | struct bt_private_trace *priv_trace, |
| 500 | struct bt_private_field_class *priv_field_class) |
| 501 | { |
| 502 | int ret; |
| 503 | struct bt_trace *trace = (void *) priv_trace; |
| 504 | struct bt_field_class *field_class = (void *) priv_field_class; |
| 505 | struct bt_resolve_field_path_context resolve_ctx = { |
| 506 | .packet_header = field_class, |
| 507 | .packet_context = NULL, |
| 508 | .event_header = NULL, |
| 509 | .event_common_context = NULL, |
| 510 | .event_specific_context = NULL, |
| 511 | .event_payload = NULL, |
| 512 | }; |
| 513 | |
| 514 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 515 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
| 516 | BT_ASSERT_PRE_TRACE_HOT(trace); |
| 517 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
| 518 | BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 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); |
| 522 | if (ret) { |
| 523 | goto end; |
| 524 | } |
| 525 | |
| 526 | bt_field_class_make_part_of_trace(field_class); |
| 527 | bt_object_put_ref(trace->packet_header_fc); |
| 528 | trace->packet_header_fc = bt_object_get_ref(field_class); |
| 529 | bt_field_class_freeze(field_class); |
| 530 | BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace); |
| 531 | |
| 532 | end: |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | bt_bool bt_trace_is_static(struct bt_trace *trace) |
| 537 | { |
| 538 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 539 | return (bt_bool) trace->is_static; |
| 540 | } |
| 541 | |
| 542 | int bt_private_trace_make_static(struct bt_private_trace *priv_trace) |
| 543 | { |
| 544 | struct bt_trace *trace = (void *) priv_trace; |
| 545 | uint64_t i; |
| 546 | |
| 547 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 548 | trace->is_static = true; |
| 549 | bt_trace_freeze(trace); |
| 550 | BT_LIB_LOGV("Trace is now static: %!+t", trace); |
| 551 | |
| 552 | /* Call all the "trace is static" listeners */ |
| 553 | for (i = 0; i < trace->is_static_listeners->len; i++) { |
| 554 | struct bt_trace_is_static_listener_elem elem = |
| 555 | g_array_index(trace->is_static_listeners, |
| 556 | struct bt_trace_is_static_listener_elem, i); |
| 557 | |
| 558 | if (elem.func) { |
| 559 | elem.func((void *) trace, elem.data); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | int bt_private_trace_add_is_static_listener( |
| 567 | struct bt_private_trace *priv_trace, |
| 568 | bt_private_trace_is_static_listener listener, |
| 569 | bt_private_trace_listener_removed listener_removed, void *data, |
| 570 | uint64_t *listener_id) |
| 571 | { |
| 572 | struct bt_trace *trace = (void *) priv_trace; |
| 573 | uint64_t i; |
| 574 | struct bt_trace_is_static_listener_elem new_elem = { |
| 575 | .func = listener, |
| 576 | .removed = listener_removed, |
| 577 | .data = data, |
| 578 | }; |
| 579 | |
| 580 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 581 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); |
| 582 | BT_ASSERT_PRE(!trace->is_static, |
| 583 | "Trace is already static: %!+t", trace); |
| 584 | BT_ASSERT_PRE(trace->in_remove_listener, |
| 585 | "Cannot call this function while executing a " |
| 586 | "remove listener: %!+t", trace); |
| 587 | |
| 588 | /* Find the next available spot */ |
| 589 | for (i = 0; i < trace->is_static_listeners->len; i++) { |
| 590 | struct bt_trace_is_static_listener_elem elem = |
| 591 | g_array_index(trace->is_static_listeners, |
| 592 | struct bt_trace_is_static_listener_elem, i); |
| 593 | |
| 594 | if (!elem.func) { |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (i == trace->is_static_listeners->len) { |
| 600 | g_array_append_val(trace->is_static_listeners, new_elem); |
| 601 | } else { |
| 602 | g_array_insert_val(trace->is_static_listeners, i, new_elem); |
| 603 | } |
| 604 | |
| 605 | if (listener_id) { |
| 606 | *listener_id = i; |
| 607 | } |
| 608 | |
| 609 | BT_LIB_LOGV("Added \"trace is static\" listener: " |
| 610 | "%![trace-]+t, listener-id=%" PRIu64, trace, i); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | BT_ASSERT_PRE_FUNC |
| 615 | static |
| 616 | bool has_listener_id(struct bt_trace *trace, uint64_t listener_id) |
| 617 | { |
| 618 | BT_ASSERT(listener_id < trace->is_static_listeners->len); |
| 619 | return (&g_array_index(trace->is_static_listeners, |
| 620 | struct bt_trace_is_static_listener_elem, |
| 621 | listener_id))->func != NULL; |
| 622 | } |
| 623 | |
| 624 | int bt_private_trace_remove_is_static_listener( |
| 625 | struct bt_private_trace *priv_trace, uint64_t listener_id) |
| 626 | { |
| 627 | struct bt_trace *trace = (void *) priv_trace; |
| 628 | struct bt_trace_is_static_listener_elem *elem; |
| 629 | |
| 630 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 631 | BT_ASSERT_PRE(!trace->is_static, |
| 632 | "Trace is already static: %!+t", trace); |
| 633 | BT_ASSERT_PRE(trace->in_remove_listener, |
| 634 | "Cannot call this function while executing a " |
| 635 | "remove listener: %!+t", trace); |
| 636 | BT_ASSERT_PRE(has_listener_id(trace, listener_id), |
| 637 | "Trace has no such \"trace is static\" listener ID: " |
| 638 | "%![trace-]+t, %" PRIu64, trace, listener_id); |
| 639 | elem = &g_array_index(trace->is_static_listeners, |
| 640 | struct bt_trace_is_static_listener_elem, |
| 641 | listener_id); |
| 642 | BT_ASSERT(elem->func); |
| 643 | |
| 644 | if (elem->removed) { |
| 645 | /* Call remove listener */ |
| 646 | BT_LIB_LOGV("Calling remove listener: " |
| 647 | "%![trace-]+t, listener-id=%" PRIu64, |
| 648 | trace, listener_id); |
| 649 | trace->in_remove_listener = true; |
| 650 | elem->removed((void *) trace, elem->data); |
| 651 | trace->in_remove_listener = false; |
| 652 | } |
| 653 | |
| 654 | elem->func = NULL; |
| 655 | elem->removed = NULL; |
| 656 | elem->data = NULL; |
| 657 | BT_LIB_LOGV("Removed \"trace is static\" listener: " |
| 658 | "%![trace-]+t, listener-id=%" PRIu64, |
| 659 | trace, listener_id); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | BT_HIDDEN |
| 664 | void _bt_trace_freeze(struct bt_trace *trace) |
| 665 | { |
| 666 | /* The packet header field classe is already frozen */ |
| 667 | BT_ASSERT(trace); |
| 668 | BT_LIB_LOGD("Freezing trace: %!+t", trace); |
| 669 | trace->frozen = true; |
| 670 | } |
| 671 | |
| 672 | bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace) |
| 673 | { |
| 674 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 675 | return (bt_bool) trace->assigns_automatic_stream_class_id; |
| 676 | } |
| 677 | |
| 678 | void bt_private_trace_set_assigns_automatic_stream_class_id( |
| 679 | struct bt_private_trace *priv_trace, bt_bool value) |
| 680 | { |
| 681 | struct bt_trace *trace = (void *) priv_trace; |
| 682 | |
| 683 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 684 | BT_ASSERT_PRE_TRACE_HOT(trace); |
| 685 | trace->assigns_automatic_stream_class_id = (bool) value; |
| 686 | BT_LIB_LOGV("Set trace's automatic stream class ID " |
| 687 | "assignment property: %!+t", trace); |
| 688 | } |
| 689 | |
| 690 | BT_HIDDEN |
| 691 | void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream) |
| 692 | { |
| 693 | guint count = 0; |
| 694 | |
| 695 | bt_object_set_parent(&stream->base, &trace->base); |
| 696 | g_ptr_array_add(trace->streams, stream); |
| 697 | bt_trace_freeze(trace); |
| 698 | |
| 699 | if (bt_g_hash_table_contains(trace->stream_classes_stream_count, |
| 700 | stream->class)) { |
| 701 | count = GPOINTER_TO_UINT(g_hash_table_lookup( |
| 702 | trace->stream_classes_stream_count, stream->class)); |
| 703 | } |
| 704 | |
| 705 | g_hash_table_insert(trace->stream_classes_stream_count, |
| 706 | stream->class, GUINT_TO_POINTER(count + 1)); |
| 707 | } |
| 708 | |
| 709 | BT_HIDDEN |
| 710 | uint64_t bt_trace_get_automatic_stream_id(struct bt_trace *trace, |
| 711 | struct bt_stream_class *stream_class) |
| 712 | { |
| 713 | gpointer orig_key; |
| 714 | gpointer value; |
| 715 | uint64_t id = 0; |
| 716 | |
| 717 | BT_ASSERT(stream_class); |
| 718 | BT_ASSERT(trace); |
| 719 | if (g_hash_table_lookup_extended(trace->stream_classes_stream_count, |
| 720 | stream_class, &orig_key, &value)) { |
| 721 | id = (uint64_t) GPOINTER_TO_UINT(value); |
| 722 | } |
| 723 | |
| 724 | return id; |
| 725 | } |