| 1 | /* |
| 2 | * stream-class.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Stream Class |
| 5 | * |
| 6 | * Copyright 2013, 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 "STREAM-CLASS" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/ctf-writer/clock.h> |
| 33 | #include <babeltrace/ctf-writer/clock-internal.h> |
| 34 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
| 35 | #include <babeltrace/ctf-writer/event.h> |
| 36 | #include <babeltrace/ctf-ir/event-class-internal.h> |
| 37 | #include <babeltrace/ctf-ir/event-internal.h> |
| 38 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 39 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 40 | #include <babeltrace/ctf-writer/stream.h> |
| 41 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 42 | #include <babeltrace/ctf-ir/validation-internal.h> |
| 43 | #include <babeltrace/ctf-ir/visitor-internal.h> |
| 44 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 45 | #include <babeltrace/ctf-ir/utils.h> |
| 46 | #include <babeltrace/ref.h> |
| 47 | #include <babeltrace/compiler-internal.h> |
| 48 | #include <babeltrace/align-internal.h> |
| 49 | #include <babeltrace/endian-internal.h> |
| 50 | #include <inttypes.h> |
| 51 | #include <stdint.h> |
| 52 | #include <stdbool.h> |
| 53 | |
| 54 | static |
| 55 | void bt_ctf_stream_class_destroy(struct bt_object *obj); |
| 56 | static |
| 57 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
| 58 | static |
| 59 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
| 60 | |
| 61 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) |
| 62 | { |
| 63 | struct bt_ctf_stream_class *stream_class; |
| 64 | int ret; |
| 65 | |
| 66 | BT_LOGD("Creating default stream class object: name=\"%s\"", name); |
| 67 | stream_class = bt_ctf_stream_class_create_empty(name); |
| 68 | if (!stream_class) { |
| 69 | BT_LOGD_STR("Cannot create empty stream class."); |
| 70 | goto error; |
| 71 | } |
| 72 | |
| 73 | ret = init_event_header(stream_class); |
| 74 | if (ret) { |
| 75 | BT_LOGE_STR("Cannot initialize stream class's event header field type."); |
| 76 | goto error; |
| 77 | } |
| 78 | |
| 79 | ret = init_packet_context(stream_class); |
| 80 | if (ret) { |
| 81 | BT_LOGE_STR("Cannot initialize stream class's packet context field type."); |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | BT_LOGD("Created default stream class object: addr=%p, name=\"%s\"", |
| 86 | stream_class, name); |
| 87 | return stream_class; |
| 88 | |
| 89 | error: |
| 90 | BT_PUT(stream_class); |
| 91 | return stream_class; |
| 92 | } |
| 93 | |
| 94 | struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name) |
| 95 | { |
| 96 | struct bt_ctf_stream_class *stream_class = NULL; |
| 97 | |
| 98 | BT_LOGD("Creating empty stream class object: name=\"%s\"", name); |
| 99 | |
| 100 | if (name && bt_ctf_validate_identifier(name)) { |
| 101 | BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: " |
| 102 | "name=\"%s\"", name); |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | stream_class = g_new0(struct bt_ctf_stream_class, 1); |
| 107 | if (!stream_class) { |
| 108 | BT_LOGE_STR("Failed to allocate one stream class."); |
| 109 | goto error; |
| 110 | } |
| 111 | |
| 112 | stream_class->name = g_string_new(name); |
| 113 | stream_class->event_classes = g_ptr_array_new_with_free_func( |
| 114 | (GDestroyNotify) bt_object_release); |
| 115 | if (!stream_class->event_classes) { |
| 116 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash, |
| 121 | g_int64_equal, g_free, NULL); |
| 122 | if (!stream_class->event_classes_ht) { |
| 123 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | stream_class->packet_context_type = bt_ctf_field_type_structure_create(); |
| 128 | if (!stream_class->packet_context_type) { |
| 129 | BT_LOGE_STR("Cannot create stream class's initial packet context field type."); |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | stream_class->event_header_type = bt_ctf_field_type_structure_create(); |
| 134 | if (!stream_class->event_header_type) { |
| 135 | BT_LOGE_STR("Cannot create stream class's initial event header field type."); |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | stream_class->event_context_type = bt_ctf_field_type_structure_create(); |
| 140 | if (!stream_class->event_context_type) { |
| 141 | BT_LOGE_STR("Cannot create stream class's initial event context field type."); |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | bt_object_init(stream_class, bt_ctf_stream_class_destroy); |
| 146 | BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"", |
| 147 | stream_class, name); |
| 148 | return stream_class; |
| 149 | |
| 150 | error: |
| 151 | BT_PUT(stream_class); |
| 152 | return stream_class; |
| 153 | } |
| 154 | |
| 155 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
| 156 | struct bt_ctf_stream_class *stream_class) |
| 157 | { |
| 158 | return stream_class ? |
| 159 | bt_get(bt_ctf_stream_class_borrow_trace(stream_class)) : |
| 160 | NULL; |
| 161 | } |
| 162 | |
| 163 | const char *bt_ctf_stream_class_get_name( |
| 164 | struct bt_ctf_stream_class *stream_class) |
| 165 | { |
| 166 | const char *name = NULL; |
| 167 | |
| 168 | if (!stream_class) { |
| 169 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | name = stream_class->name->str; |
| 174 | end: |
| 175 | return name; |
| 176 | } |
| 177 | |
| 178 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
| 179 | const char *name) |
| 180 | { |
| 181 | int ret = 0; |
| 182 | |
| 183 | if (!stream_class) { |
| 184 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 185 | ret = -1; |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | if (stream_class->frozen) { |
| 190 | BT_LOGW("Invalid parameter: stream class is frozen: " |
| 191 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 192 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 193 | bt_ctf_stream_class_get_id(stream_class)); |
| 194 | ret = -1; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | g_string_assign(stream_class->name, name); |
| 199 | BT_LOGV("Set stream class's name: " |
| 200 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 201 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 202 | bt_ctf_stream_class_get_id(stream_class)); |
| 203 | end: |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
| 208 | struct bt_ctf_stream_class *stream_class) |
| 209 | { |
| 210 | struct bt_ctf_clock *clock = NULL; |
| 211 | |
| 212 | if (!stream_class) { |
| 213 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 214 | goto end; |
| 215 | } |
| 216 | |
| 217 | if (!stream_class->clock) { |
| 218 | BT_LOGV("Stream class has no clock: " |
| 219 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 220 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 221 | bt_ctf_stream_class_get_id(stream_class)); |
| 222 | goto end; |
| 223 | } |
| 224 | |
| 225 | clock = bt_get(stream_class->clock); |
| 226 | end: |
| 227 | return clock; |
| 228 | } |
| 229 | |
| 230 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
| 231 | struct bt_ctf_clock *clock) |
| 232 | { |
| 233 | int ret = 0; |
| 234 | struct bt_ctf_field_type *timestamp_field = NULL; |
| 235 | |
| 236 | if (!stream_class || !clock) { |
| 237 | BT_LOGW("Invalid parameter: stream class or clock is NULL: " |
| 238 | "stream-class-addr=%p, clock-addr=%p", |
| 239 | stream_class, clock); |
| 240 | ret = -1; |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | if (stream_class->frozen) { |
| 245 | BT_LOGW("Invalid parameter: stream class is frozen: " |
| 246 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 247 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 248 | bt_ctf_stream_class_get_id(stream_class)); |
| 249 | ret = -1; |
| 250 | goto end; |
| 251 | } |
| 252 | |
| 253 | /* Replace the current clock of this stream class. */ |
| 254 | bt_put(stream_class->clock); |
| 255 | stream_class->clock = bt_get(clock); |
| 256 | BT_LOGV("Set stream class's clock: " |
| 257 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 258 | "clock-addr=%p, clock-name=\"%s\"", |
| 259 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 260 | bt_ctf_stream_class_get_id(stream_class), |
| 261 | stream_class->clock, |
| 262 | bt_ctf_clock_get_name(stream_class->clock)); |
| 263 | |
| 264 | end: |
| 265 | bt_put(timestamp_field); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
| 270 | { |
| 271 | int64_t ret; |
| 272 | |
| 273 | if (!stream_class) { |
| 274 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 275 | ret = (int64_t) -1; |
| 276 | goto end; |
| 277 | } |
| 278 | |
| 279 | if (!stream_class->id_set) { |
| 280 | BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"", |
| 281 | stream_class, |
| 282 | bt_ctf_stream_class_get_name(stream_class)); |
| 283 | ret = (int64_t) -1; |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | ret = stream_class->id; |
| 288 | end: |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | BT_HIDDEN |
| 293 | void _bt_ctf_stream_class_set_id( |
| 294 | struct bt_ctf_stream_class *stream_class, int64_t id) |
| 295 | { |
| 296 | assert(stream_class); |
| 297 | stream_class->id = id; |
| 298 | stream_class->id_set = 1; |
| 299 | BT_LOGV("Set stream class's ID (internal): " |
| 300 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 301 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 302 | bt_ctf_stream_class_get_id(stream_class)); |
| 303 | } |
| 304 | |
| 305 | struct event_class_set_stream_class_id_data { |
| 306 | int64_t stream_class_id; |
| 307 | int ret; |
| 308 | }; |
| 309 | |
| 310 | static |
| 311 | void event_class_set_stream_id(gpointer event_class, gpointer data) |
| 312 | { |
| 313 | struct event_class_set_stream_class_id_data *typed_data = data; |
| 314 | |
| 315 | typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class, |
| 316 | typed_data->stream_class_id); |
| 317 | } |
| 318 | |
| 319 | BT_HIDDEN |
| 320 | int bt_ctf_stream_class_set_id_no_check( |
| 321 | struct bt_ctf_stream_class *stream_class, int64_t id) |
| 322 | { |
| 323 | int ret = 0; |
| 324 | struct event_class_set_stream_class_id_data data = |
| 325 | { .stream_class_id = id, .ret = 0 }; |
| 326 | |
| 327 | /* |
| 328 | * Make sure all event classes have their "stream_id" attribute |
| 329 | * set to this value. |
| 330 | */ |
| 331 | g_ptr_array_foreach(stream_class->event_classes, |
| 332 | event_class_set_stream_id, &data); |
| 333 | ret = data.ret; |
| 334 | if (ret) { |
| 335 | BT_LOGE("Cannot set the IDs of all stream class's event classes: " |
| 336 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 337 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 338 | bt_ctf_stream_class_get_id(stream_class)); |
| 339 | goto end; |
| 340 | } |
| 341 | |
| 342 | _bt_ctf_stream_class_set_id(stream_class, id); |
| 343 | end: |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
| 348 | uint64_t id_param) |
| 349 | { |
| 350 | int ret = 0; |
| 351 | int64_t id = (int64_t) id_param; |
| 352 | |
| 353 | if (!stream_class) { |
| 354 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 355 | ret = -1; |
| 356 | goto end; |
| 357 | } |
| 358 | |
| 359 | if (stream_class->frozen) { |
| 360 | BT_LOGW("Invalid parameter: stream class is frozen: " |
| 361 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 362 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 363 | bt_ctf_stream_class_get_id(stream_class)); |
| 364 | ret = -1; |
| 365 | goto end; |
| 366 | } |
| 367 | |
| 368 | if (id < 0) { |
| 369 | BT_LOGW("Invalid parameter: invalid stream class's ID: " |
| 370 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 371 | "stream-class-id=%" PRId64 ", id=%" PRIu64, |
| 372 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 373 | bt_ctf_stream_class_get_id(stream_class), |
| 374 | id_param); |
| 375 | ret = -1; |
| 376 | goto end; |
| 377 | } |
| 378 | |
| 379 | ret = bt_ctf_stream_class_set_id_no_check(stream_class, id); |
| 380 | if (ret == 0) { |
| 381 | BT_LOGV("Set stream class's ID: " |
| 382 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 383 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 384 | bt_ctf_stream_class_get_id(stream_class)); |
| 385 | } |
| 386 | end: |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | static |
| 391 | void event_class_exists(gpointer element, gpointer query) |
| 392 | { |
| 393 | struct bt_ctf_event_class *event_class_a = element; |
| 394 | struct search_query *search_query = query; |
| 395 | struct bt_ctf_event_class *event_class_b = search_query->value; |
| 396 | int64_t id_a, id_b; |
| 397 | |
| 398 | if (search_query->value == element) { |
| 399 | search_query->found = 1; |
| 400 | goto end; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Two event classes cannot share the same ID in a given |
| 405 | * stream class. |
| 406 | */ |
| 407 | id_a = bt_ctf_event_class_get_id(event_class_a); |
| 408 | id_b = bt_ctf_event_class_get_id(event_class_b); |
| 409 | |
| 410 | if (id_a < 0 || id_b < 0) { |
| 411 | /* at least one ID is not set: will be automatically set later */ |
| 412 | goto end; |
| 413 | } |
| 414 | |
| 415 | if (id_a == id_b) { |
| 416 | BT_LOGW("Event class with this ID already exists in the stream class: " |
| 417 | "id=%" PRId64 ", name=\"%s\"", |
| 418 | id_a, bt_ctf_event_class_get_name(event_class_a)); |
| 419 | search_query->found = 1; |
| 420 | goto end; |
| 421 | } |
| 422 | |
| 423 | end: |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | int bt_ctf_stream_class_add_event_class( |
| 428 | struct bt_ctf_stream_class *stream_class, |
| 429 | struct bt_ctf_event_class *event_class) |
| 430 | { |
| 431 | int ret = 0; |
| 432 | int64_t *event_id = NULL; |
| 433 | struct bt_ctf_trace *trace = NULL; |
| 434 | struct bt_ctf_stream_class *old_stream_class = NULL; |
| 435 | struct bt_ctf_validation_output validation_output = { 0 }; |
| 436 | struct bt_ctf_field_type *packet_header_type = NULL; |
| 437 | struct bt_ctf_field_type *packet_context_type = NULL; |
| 438 | struct bt_ctf_field_type *event_header_type = NULL; |
| 439 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; |
| 440 | struct bt_ctf_field_type *event_context_type = NULL; |
| 441 | struct bt_ctf_field_type *event_payload_type = NULL; |
| 442 | const enum bt_ctf_validation_flag validation_flags = |
| 443 | BT_CTF_VALIDATION_FLAG_EVENT; |
| 444 | |
| 445 | if (!stream_class || !event_class) { |
| 446 | BT_LOGW("Invalid parameter: stream class or event class is NULL: " |
| 447 | "stream-class-addr=%p, event-class-addr=%p", |
| 448 | stream_class, event_class); |
| 449 | ret = -1; |
| 450 | goto end; |
| 451 | } |
| 452 | |
| 453 | BT_LOGD("Adding event class to stream class: " |
| 454 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 455 | "stream-class-id=%" PRId64 ", event-class-addr=%p, " |
| 456 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 457 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 458 | bt_ctf_stream_class_get_id(stream_class), |
| 459 | event_class, |
| 460 | bt_ctf_event_class_get_name(event_class), |
| 461 | bt_ctf_event_class_get_id(event_class)); |
| 462 | |
| 463 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 464 | if (trace && trace->is_static) { |
| 465 | ret = -1; |
| 466 | goto end; |
| 467 | } |
| 468 | |
| 469 | event_id = g_new(int64_t, 1); |
| 470 | if (!event_id) { |
| 471 | BT_LOGE_STR("Failed to allocate one int64_t."); |
| 472 | ret = -1; |
| 473 | goto end; |
| 474 | } |
| 475 | |
| 476 | /* Check for duplicate event classes */ |
| 477 | struct search_query query = { .value = event_class, .found = 0 }; |
| 478 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
| 479 | &query); |
| 480 | if (query.found) { |
| 481 | BT_LOGW_STR("Another event class part of this stream class has the same ID."); |
| 482 | ret = -1; |
| 483 | goto end; |
| 484 | } |
| 485 | |
| 486 | old_stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 487 | if (old_stream_class) { |
| 488 | /* Event class is already associated to a stream class. */ |
| 489 | BT_LOGW("Event class is already part of another stream class: " |
| 490 | "event-class-stream-class-addr=%p, " |
| 491 | "event-class-stream-class-name=\"%s\", " |
| 492 | "event-class-stream-class-id=%" PRId64, |
| 493 | old_stream_class, |
| 494 | bt_ctf_stream_class_get_name(old_stream_class), |
| 495 | bt_ctf_stream_class_get_id(old_stream_class)); |
| 496 | ret = -1; |
| 497 | goto end; |
| 498 | } |
| 499 | |
| 500 | if (trace) { |
| 501 | /* |
| 502 | * If the stream class is associated with a trace, then |
| 503 | * both those objects are frozen. Also, this event class |
| 504 | * is about to be frozen. |
| 505 | * |
| 506 | * Therefore the event class must be validated here. |
| 507 | * The trace and stream class should be valid at this |
| 508 | * point. |
| 509 | */ |
| 510 | assert(trace->valid); |
| 511 | assert(stream_class->valid); |
| 512 | packet_header_type = |
| 513 | bt_ctf_trace_get_packet_header_type(trace); |
| 514 | packet_context_type = |
| 515 | bt_ctf_stream_class_get_packet_context_type( |
| 516 | stream_class); |
| 517 | event_header_type = |
| 518 | bt_ctf_stream_class_get_event_header_type(stream_class); |
| 519 | stream_event_ctx_type = |
| 520 | bt_ctf_stream_class_get_event_context_type( |
| 521 | stream_class); |
| 522 | event_context_type = |
| 523 | bt_ctf_event_class_get_context_type(event_class); |
| 524 | event_payload_type = |
| 525 | bt_ctf_event_class_get_payload_type(event_class); |
| 526 | ret = bt_ctf_validate_class_types( |
| 527 | trace->environment, packet_header_type, |
| 528 | packet_context_type, event_header_type, |
| 529 | stream_event_ctx_type, event_context_type, |
| 530 | event_payload_type, trace->valid, |
| 531 | stream_class->valid, event_class->valid, |
| 532 | &validation_output, validation_flags); |
| 533 | BT_PUT(packet_header_type); |
| 534 | BT_PUT(packet_context_type); |
| 535 | BT_PUT(event_header_type); |
| 536 | BT_PUT(stream_event_ctx_type); |
| 537 | BT_PUT(event_context_type); |
| 538 | BT_PUT(event_payload_type); |
| 539 | |
| 540 | if (ret) { |
| 541 | /* |
| 542 | * This means something went wrong during the |
| 543 | * validation process, not that the objects are |
| 544 | * invalid. |
| 545 | */ |
| 546 | BT_LOGE("Failed to validate event class: ret=%d", ret); |
| 547 | goto end; |
| 548 | } |
| 549 | |
| 550 | if ((validation_output.valid_flags & validation_flags) != |
| 551 | validation_flags) { |
| 552 | /* Invalid event class */ |
| 553 | BT_LOGW("Invalid trace, stream class, or event class: " |
| 554 | "valid-flags=0x%x", |
| 555 | validation_output.valid_flags); |
| 556 | ret = -1; |
| 557 | goto end; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* Only set an event ID if none was explicitly set before */ |
| 562 | *event_id = bt_ctf_event_class_get_id(event_class); |
| 563 | if (*event_id < 0) { |
| 564 | BT_LOGV("Event class has no ID: automatically setting it: " |
| 565 | "id=%" PRId64, stream_class->next_event_id); |
| 566 | |
| 567 | if (bt_ctf_event_class_set_id(event_class, |
| 568 | stream_class->next_event_id)) { |
| 569 | BT_LOGE("Cannot set event class's ID: id=%" PRId64, |
| 570 | stream_class->next_event_id); |
| 571 | ret = -1; |
| 572 | goto end; |
| 573 | } |
| 574 | stream_class->next_event_id++; |
| 575 | *event_id = stream_class->next_event_id; |
| 576 | } |
| 577 | |
| 578 | ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); |
| 579 | if (ret) { |
| 580 | BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d", |
| 581 | ret); |
| 582 | goto end; |
| 583 | } |
| 584 | |
| 585 | bt_object_set_parent(event_class, stream_class); |
| 586 | |
| 587 | if (trace) { |
| 588 | /* |
| 589 | * At this point we know that the function will be |
| 590 | * successful. Therefore we can replace the event |
| 591 | * class's field types with what's in the validation |
| 592 | * output structure and mark this event class as valid. |
| 593 | */ |
| 594 | bt_ctf_validation_replace_types(NULL, NULL, event_class, |
| 595 | &validation_output, validation_flags); |
| 596 | event_class->valid = 1; |
| 597 | |
| 598 | /* |
| 599 | * Put what was not moved in |
| 600 | * bt_ctf_validation_replace_types(). |
| 601 | */ |
| 602 | bt_ctf_validation_output_put_types(&validation_output); |
| 603 | } |
| 604 | |
| 605 | /* Add to the event classes of the stream class */ |
| 606 | g_ptr_array_add(stream_class->event_classes, event_class); |
| 607 | g_hash_table_insert(stream_class->event_classes_ht, event_id, |
| 608 | event_class); |
| 609 | event_id = NULL; |
| 610 | |
| 611 | /* Freeze the event class */ |
| 612 | bt_ctf_event_class_freeze(event_class); |
| 613 | |
| 614 | /* Notifiy listeners of the trace's schema modification. */ |
| 615 | if (trace) { |
| 616 | struct bt_ctf_object obj = { .object = event_class, |
| 617 | .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS }; |
| 618 | |
| 619 | (void) bt_ctf_trace_object_modification(&obj, trace); |
| 620 | } |
| 621 | |
| 622 | BT_LOGD("Added event class to stream class: " |
| 623 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 624 | "stream-class-id=%" PRId64 ", event-class-addr=%p, " |
| 625 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 626 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 627 | bt_ctf_stream_class_get_id(stream_class), |
| 628 | event_class, |
| 629 | bt_ctf_event_class_get_name(event_class), |
| 630 | bt_ctf_event_class_get_id(event_class)); |
| 631 | |
| 632 | end: |
| 633 | BT_PUT(trace); |
| 634 | BT_PUT(old_stream_class); |
| 635 | bt_ctf_validation_output_put_types(&validation_output); |
| 636 | assert(!packet_header_type); |
| 637 | assert(!packet_context_type); |
| 638 | assert(!event_header_type); |
| 639 | assert(!stream_event_ctx_type); |
| 640 | assert(!event_context_type); |
| 641 | assert(!event_payload_type); |
| 642 | g_free(event_id); |
| 643 | |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | int64_t bt_ctf_stream_class_get_event_class_count( |
| 648 | struct bt_ctf_stream_class *stream_class) |
| 649 | { |
| 650 | int64_t ret; |
| 651 | |
| 652 | if (!stream_class) { |
| 653 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 654 | ret = (int64_t) -1; |
| 655 | goto end; |
| 656 | } |
| 657 | |
| 658 | ret = (int64_t) stream_class->event_classes->len; |
| 659 | end: |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_index( |
| 664 | struct bt_ctf_stream_class *stream_class, uint64_t index) |
| 665 | { |
| 666 | struct bt_ctf_event_class *event_class = NULL; |
| 667 | |
| 668 | if (!stream_class) { |
| 669 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 670 | goto end; |
| 671 | } |
| 672 | |
| 673 | if (index >= stream_class->event_classes->len) { |
| 674 | BT_LOGW("Invalid parameter: index is out of bounds: " |
| 675 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 676 | "index=%" PRIu64 ", count=%u", |
| 677 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 678 | bt_ctf_stream_class_get_id(stream_class), |
| 679 | index, stream_class->event_classes->len); |
| 680 | goto end; |
| 681 | } |
| 682 | |
| 683 | event_class = g_ptr_array_index(stream_class->event_classes, index); |
| 684 | bt_get(event_class); |
| 685 | end: |
| 686 | return event_class; |
| 687 | } |
| 688 | |
| 689 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
| 690 | struct bt_ctf_stream_class *stream_class, uint64_t id) |
| 691 | { |
| 692 | int64_t id_key = (int64_t) id; |
| 693 | struct bt_ctf_event_class *event_class = NULL; |
| 694 | |
| 695 | if (!stream_class) { |
| 696 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 697 | goto end; |
| 698 | } |
| 699 | |
| 700 | if (id_key < 0) { |
| 701 | BT_LOGW("Invalid parameter: invalid event class's ID: " |
| 702 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 703 | "stream-class-id=%" PRId64 ", event-class-id=%" PRIu64, |
| 704 | stream_class, |
| 705 | bt_ctf_stream_class_get_name(stream_class), |
| 706 | bt_ctf_stream_class_get_id(stream_class), id); |
| 707 | goto end; |
| 708 | } |
| 709 | |
| 710 | event_class = g_hash_table_lookup(stream_class->event_classes_ht, |
| 711 | &id_key); |
| 712 | bt_get(event_class); |
| 713 | end: |
| 714 | return event_class; |
| 715 | } |
| 716 | |
| 717 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
| 718 | struct bt_ctf_stream_class *stream_class) |
| 719 | { |
| 720 | struct bt_ctf_field_type *ret = NULL; |
| 721 | |
| 722 | if (!stream_class) { |
| 723 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | bt_get(stream_class->packet_context_type); |
| 728 | ret = stream_class->packet_context_type; |
| 729 | end: |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | int bt_ctf_stream_class_set_packet_context_type( |
| 734 | struct bt_ctf_stream_class *stream_class, |
| 735 | struct bt_ctf_field_type *packet_context_type) |
| 736 | { |
| 737 | int ret = 0; |
| 738 | |
| 739 | if (!stream_class) { |
| 740 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 741 | ret = -1; |
| 742 | goto end; |
| 743 | } |
| 744 | |
| 745 | if (stream_class->frozen) { |
| 746 | BT_LOGW("Invalid parameter: stream class is frozen: " |
| 747 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 748 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 749 | bt_ctf_stream_class_get_id(stream_class)); |
| 750 | ret = -1; |
| 751 | goto end; |
| 752 | } |
| 753 | |
| 754 | if (packet_context_type && |
| 755 | bt_ctf_field_type_get_type_id(packet_context_type) != |
| 756 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 757 | /* A packet context must be a structure. */ |
| 758 | BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: " |
| 759 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 760 | "packet-context-ft-addr=%p, packet-context-ft-id=%s", |
| 761 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 762 | bt_ctf_stream_class_get_id(stream_class), |
| 763 | packet_context_type, |
| 764 | bt_ctf_field_type_id_string( |
| 765 | bt_ctf_field_type_get_type_id(packet_context_type))); |
| 766 | ret = -1; |
| 767 | goto end; |
| 768 | } |
| 769 | |
| 770 | bt_put(stream_class->packet_context_type); |
| 771 | bt_get(packet_context_type); |
| 772 | stream_class->packet_context_type = packet_context_type; |
| 773 | BT_LOGV("Set stream class's packet context field type: " |
| 774 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 775 | "packet-context-ft-addr=%p", |
| 776 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 777 | bt_ctf_stream_class_get_id(stream_class), |
| 778 | packet_context_type); |
| 779 | |
| 780 | end: |
| 781 | return ret; |
| 782 | } |
| 783 | |
| 784 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
| 785 | struct bt_ctf_stream_class *stream_class) |
| 786 | { |
| 787 | struct bt_ctf_field_type *ret = NULL; |
| 788 | |
| 789 | if (!stream_class) { |
| 790 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 791 | goto end; |
| 792 | } |
| 793 | |
| 794 | if (!stream_class->event_header_type) { |
| 795 | BT_LOGV("Stream class has no event header field type: " |
| 796 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 797 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 798 | bt_ctf_stream_class_get_id(stream_class)); |
| 799 | goto end; |
| 800 | } |
| 801 | |
| 802 | bt_get(stream_class->event_header_type); |
| 803 | ret = stream_class->event_header_type; |
| 804 | end: |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | int bt_ctf_stream_class_set_event_header_type( |
| 809 | struct bt_ctf_stream_class *stream_class, |
| 810 | struct bt_ctf_field_type *event_header_type) |
| 811 | { |
| 812 | int ret = 0; |
| 813 | |
| 814 | if (!stream_class) { |
| 815 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 816 | ret = -1; |
| 817 | goto end; |
| 818 | } |
| 819 | |
| 820 | if (stream_class->frozen) { |
| 821 | BT_LOGW("Invalid parameter: stream class is frozen: " |
| 822 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 823 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 824 | bt_ctf_stream_class_get_id(stream_class)); |
| 825 | ret = -1; |
| 826 | goto end; |
| 827 | } |
| 828 | |
| 829 | if (event_header_type && |
| 830 | bt_ctf_field_type_get_type_id(event_header_type) != |
| 831 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 832 | /* An event header must be a structure. */ |
| 833 | BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: " |
| 834 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 835 | "event-header-ft-addr=%p, event-header-ft-id=%s", |
| 836 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 837 | bt_ctf_stream_class_get_id(stream_class), |
| 838 | event_header_type, |
| 839 | bt_ctf_field_type_id_string( |
| 840 | bt_ctf_field_type_get_type_id(event_header_type))); |
| 841 | ret = -1; |
| 842 | goto end; |
| 843 | } |
| 844 | |
| 845 | bt_put(stream_class->event_header_type); |
| 846 | stream_class->event_header_type = bt_get(event_header_type); |
| 847 | BT_LOGV("Set stream class's event header field type: " |
| 848 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 849 | "event-header-ft-addr=%p", |
| 850 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 851 | bt_ctf_stream_class_get_id(stream_class), |
| 852 | event_header_type); |
| 853 | end: |
| 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
| 858 | struct bt_ctf_stream_class *stream_class) |
| 859 | { |
| 860 | struct bt_ctf_field_type *ret = NULL; |
| 861 | |
| 862 | if (!stream_class) { |
| 863 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 864 | goto end; |
| 865 | } |
| 866 | |
| 867 | if (!stream_class->event_context_type) { |
| 868 | goto end; |
| 869 | } |
| 870 | |
| 871 | bt_get(stream_class->event_context_type); |
| 872 | ret = stream_class->event_context_type; |
| 873 | end: |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | int bt_ctf_stream_class_set_event_context_type( |
| 878 | struct bt_ctf_stream_class *stream_class, |
| 879 | struct bt_ctf_field_type *event_context_type) |
| 880 | { |
| 881 | int ret = 0; |
| 882 | |
| 883 | if (!stream_class) { |
| 884 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 885 | ret = -1; |
| 886 | goto end; |
| 887 | } |
| 888 | |
| 889 | if (stream_class->frozen) { |
| 890 | BT_LOGW("Invalid parameter: stream class is frozen: " |
| 891 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 892 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 893 | bt_ctf_stream_class_get_id(stream_class)); |
| 894 | ret = -1; |
| 895 | goto end; |
| 896 | } |
| 897 | |
| 898 | if (event_context_type && |
| 899 | bt_ctf_field_type_get_type_id(event_context_type) != |
| 900 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 901 | /* A packet context must be a structure. */ |
| 902 | BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: " |
| 903 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 904 | "event-context-ft-addr=%p, event-context-ft-id=%s", |
| 905 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 906 | bt_ctf_stream_class_get_id(stream_class), |
| 907 | event_context_type, |
| 908 | bt_ctf_field_type_id_string( |
| 909 | bt_ctf_field_type_get_type_id(event_context_type))); |
| 910 | ret = -1; |
| 911 | goto end; |
| 912 | } |
| 913 | |
| 914 | bt_put(stream_class->event_context_type); |
| 915 | stream_class->event_context_type = bt_get(event_context_type); |
| 916 | BT_LOGV("Set stream class's event context field type: " |
| 917 | "addr=%p, name=\"%s\", id=%" PRId64 ", " |
| 918 | "event-context-ft-addr=%p", |
| 919 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 920 | bt_ctf_stream_class_get_id(stream_class), |
| 921 | event_context_type); |
| 922 | end: |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | /* Pre-2.0 CTF writer backward compatibility */ |
| 927 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
| 928 | { |
| 929 | bt_get(stream_class); |
| 930 | } |
| 931 | |
| 932 | /* Pre-2.0 CTF writer backward compatibility */ |
| 933 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) |
| 934 | { |
| 935 | bt_put(stream_class); |
| 936 | } |
| 937 | |
| 938 | static |
| 939 | int64_t get_event_class_count(void *element) |
| 940 | { |
| 941 | return bt_ctf_stream_class_get_event_class_count( |
| 942 | (struct bt_ctf_stream_class *) element); |
| 943 | } |
| 944 | |
| 945 | static |
| 946 | void *get_event_class(void *element, int i) |
| 947 | { |
| 948 | return bt_ctf_stream_class_get_event_class_by_index( |
| 949 | (struct bt_ctf_stream_class *) element, i); |
| 950 | } |
| 951 | |
| 952 | static |
| 953 | int visit_event_class(void *object, bt_ctf_visitor visitor,void *data) |
| 954 | { |
| 955 | struct bt_ctf_object obj = |
| 956 | { .object = object, |
| 957 | .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS }; |
| 958 | |
| 959 | return visitor(&obj, data); |
| 960 | } |
| 961 | |
| 962 | int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class, |
| 963 | bt_ctf_visitor visitor, void *data) |
| 964 | { |
| 965 | int ret; |
| 966 | struct bt_ctf_object obj = |
| 967 | { .object = stream_class, |
| 968 | .type = BT_CTF_OBJECT_TYPE_STREAM_CLASS }; |
| 969 | |
| 970 | if (!stream_class || !visitor) { |
| 971 | BT_LOGW("Invalid parameter: stream class or visitor is NULL: " |
| 972 | "stream-class-addr=%p, visitor=%p", |
| 973 | stream_class, visitor); |
| 974 | ret = -1; |
| 975 | goto end; |
| 976 | } |
| 977 | |
| 978 | ret = visitor_helper(&obj, get_event_class_count, |
| 979 | get_event_class, |
| 980 | visit_event_class, visitor, data); |
| 981 | BT_LOGV("visitor_helper() returned: ret=%d", ret); |
| 982 | end: |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | BT_HIDDEN |
| 987 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) |
| 988 | { |
| 989 | if (!stream_class || stream_class->frozen) { |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64, |
| 994 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 995 | bt_ctf_stream_class_get_id(stream_class)); |
| 996 | stream_class->frozen = 1; |
| 997 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
| 998 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
| 999 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
| 1000 | |
| 1001 | if (stream_class->clock) { |
| 1002 | bt_ctf_clock_class_freeze(stream_class->clock->clock_class); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | BT_HIDDEN |
| 1007 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, |
| 1008 | struct metadata_context *context) |
| 1009 | { |
| 1010 | int ret = 0; |
| 1011 | size_t i; |
| 1012 | |
| 1013 | BT_LOGD("Serializing stream class's metadata: " |
| 1014 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 1015 | "stream-class-id=%" PRId64 ", metadata-context-addr=%p", |
| 1016 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1017 | bt_ctf_stream_class_get_id(stream_class), context); |
| 1018 | g_string_assign(context->field_name, ""); |
| 1019 | context->current_indentation_level = 1; |
| 1020 | if (!stream_class->id_set) { |
| 1021 | BT_LOGW_STR("Stream class's ID is not set."); |
| 1022 | ret = -1; |
| 1023 | goto end; |
| 1024 | } |
| 1025 | |
| 1026 | g_string_append_printf(context->string, |
| 1027 | "stream {\n\tid = %" PRId64 ";\n", stream_class->id); |
| 1028 | if (stream_class->event_header_type) { |
| 1029 | BT_LOGD_STR("Serializing stream class's event header field type's metadata."); |
| 1030 | g_string_append(context->string, "\tevent.header := "); |
| 1031 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, |
| 1032 | context); |
| 1033 | if (ret) { |
| 1034 | BT_LOGW("Cannot serialize stream class's event header field type's metadata: " |
| 1035 | "ret=%d", ret); |
| 1036 | goto end; |
| 1037 | } |
| 1038 | g_string_append(context->string, ";"); |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | if (stream_class->packet_context_type) { |
| 1043 | BT_LOGD_STR("Serializing stream class's packet context field type's metadata."); |
| 1044 | g_string_append(context->string, "\n\n\tpacket.context := "); |
| 1045 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, |
| 1046 | context); |
| 1047 | if (ret) { |
| 1048 | BT_LOGW("Cannot serialize stream class's packet context field type's metadata: " |
| 1049 | "ret=%d", ret); |
| 1050 | goto end; |
| 1051 | } |
| 1052 | g_string_append(context->string, ";"); |
| 1053 | } |
| 1054 | |
| 1055 | if (stream_class->event_context_type) { |
| 1056 | BT_LOGD_STR("Serializing stream class's event context field type's metadata."); |
| 1057 | g_string_append(context->string, "\n\n\tevent.context := "); |
| 1058 | ret = bt_ctf_field_type_serialize( |
| 1059 | stream_class->event_context_type, context); |
| 1060 | if (ret) { |
| 1061 | BT_LOGW("Cannot serialize stream class's event context field type's metadata: " |
| 1062 | "ret=%d", ret); |
| 1063 | goto end; |
| 1064 | } |
| 1065 | g_string_append(context->string, ";"); |
| 1066 | } |
| 1067 | |
| 1068 | g_string_append(context->string, "\n};\n\n"); |
| 1069 | |
| 1070 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 1071 | struct bt_ctf_event_class *event_class = |
| 1072 | stream_class->event_classes->pdata[i]; |
| 1073 | |
| 1074 | ret = bt_ctf_event_class_serialize(event_class, context); |
| 1075 | if (ret) { |
| 1076 | BT_LOGW("Cannot serialize event class's metadata: " |
| 1077 | "event-class-addr=%p, event-class-name=\"%s\", " |
| 1078 | "event-class-id=%" PRId64, |
| 1079 | event_class, |
| 1080 | bt_ctf_event_class_get_name(event_class), |
| 1081 | bt_ctf_event_class_get_id(event_class)); |
| 1082 | goto end; |
| 1083 | } |
| 1084 | } |
| 1085 | end: |
| 1086 | context->current_indentation_level = 0; |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
| 1090 | static |
| 1091 | void bt_ctf_stream_class_destroy(struct bt_object *obj) |
| 1092 | { |
| 1093 | struct bt_ctf_stream_class *stream_class; |
| 1094 | |
| 1095 | stream_class = container_of(obj, struct bt_ctf_stream_class, base); |
| 1096 | BT_LOGD("Destroying stream class: addr=%p, name=\"%s\", id=%" PRId64, |
| 1097 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1098 | bt_ctf_stream_class_get_id(stream_class)); |
| 1099 | bt_put(stream_class->clock); |
| 1100 | |
| 1101 | if (stream_class->event_classes_ht) { |
| 1102 | g_hash_table_destroy(stream_class->event_classes_ht); |
| 1103 | } |
| 1104 | if (stream_class->event_classes) { |
| 1105 | g_ptr_array_free(stream_class->event_classes, TRUE); |
| 1106 | } |
| 1107 | |
| 1108 | if (stream_class->name) { |
| 1109 | g_string_free(stream_class->name, TRUE); |
| 1110 | } |
| 1111 | |
| 1112 | bt_put(stream_class->event_header_type); |
| 1113 | bt_put(stream_class->packet_context_type); |
| 1114 | bt_put(stream_class->event_context_type); |
| 1115 | g_free(stream_class); |
| 1116 | } |
| 1117 | |
| 1118 | static |
| 1119 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
| 1120 | { |
| 1121 | int ret = 0; |
| 1122 | struct bt_ctf_field_type *event_header_type = |
| 1123 | bt_ctf_field_type_structure_create(); |
| 1124 | struct bt_ctf_field_type *_uint32_t = |
| 1125 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 1126 | struct bt_ctf_field_type *_uint64_t = |
| 1127 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 1128 | |
| 1129 | if (!event_header_type) { |
| 1130 | BT_LOGE_STR("Cannot create empty structure field type."); |
| 1131 | ret = -1; |
| 1132 | goto end; |
| 1133 | } |
| 1134 | |
| 1135 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 1136 | _uint32_t, "id"); |
| 1137 | if (ret) { |
| 1138 | BT_LOGE_STR("Cannot add `id` field to event header field type."); |
| 1139 | goto end; |
| 1140 | } |
| 1141 | |
| 1142 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 1143 | _uint64_t, "timestamp"); |
| 1144 | if (ret) { |
| 1145 | BT_LOGE_STR("Cannot add `timestamp` field to event header field type."); |
| 1146 | goto end; |
| 1147 | } |
| 1148 | |
| 1149 | BT_MOVE(stream_class->event_header_type, event_header_type); |
| 1150 | end: |
| 1151 | if (ret) { |
| 1152 | bt_put(event_header_type); |
| 1153 | } |
| 1154 | |
| 1155 | bt_put(_uint32_t); |
| 1156 | bt_put(_uint64_t); |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
| 1160 | static |
| 1161 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
| 1162 | { |
| 1163 | int ret = 0; |
| 1164 | struct bt_ctf_field_type *packet_context_type = |
| 1165 | bt_ctf_field_type_structure_create(); |
| 1166 | struct bt_ctf_field_type *_uint64_t = |
| 1167 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 1168 | struct bt_ctf_field_type *ts_begin_end_uint64_t; |
| 1169 | |
| 1170 | if (!packet_context_type) { |
| 1171 | BT_LOGE_STR("Cannot create empty structure field type."); |
| 1172 | ret = -1; |
| 1173 | goto end; |
| 1174 | } |
| 1175 | |
| 1176 | ts_begin_end_uint64_t = bt_ctf_field_type_copy(_uint64_t); |
| 1177 | if (!ts_begin_end_uint64_t) { |
| 1178 | BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields."); |
| 1179 | ret = -1; |
| 1180 | goto end; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * We create a stream packet context as proposed in the CTF |
| 1185 | * specification. |
| 1186 | */ |
| 1187 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 1188 | ts_begin_end_uint64_t, "timestamp_begin"); |
| 1189 | if (ret) { |
| 1190 | BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type."); |
| 1191 | goto end; |
| 1192 | } |
| 1193 | |
| 1194 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 1195 | ts_begin_end_uint64_t, "timestamp_end"); |
| 1196 | if (ret) { |
| 1197 | BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type."); |
| 1198 | goto end; |
| 1199 | } |
| 1200 | |
| 1201 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 1202 | _uint64_t, "content_size"); |
| 1203 | if (ret) { |
| 1204 | BT_LOGE_STR("Cannot add `content_size` field to event header field type."); |
| 1205 | goto end; |
| 1206 | } |
| 1207 | |
| 1208 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 1209 | _uint64_t, "packet_size"); |
| 1210 | if (ret) { |
| 1211 | BT_LOGE_STR("Cannot add `packet_size` field to event header field type."); |
| 1212 | goto end; |
| 1213 | } |
| 1214 | |
| 1215 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 1216 | _uint64_t, "events_discarded"); |
| 1217 | if (ret) { |
| 1218 | BT_LOGE_STR("Cannot add `events_discarded` field to event header field type."); |
| 1219 | goto end; |
| 1220 | } |
| 1221 | |
| 1222 | BT_MOVE(stream_class->packet_context_type, packet_context_type); |
| 1223 | end: |
| 1224 | if (ret) { |
| 1225 | bt_put(packet_context_type); |
| 1226 | goto end; |
| 1227 | } |
| 1228 | |
| 1229 | bt_put(_uint64_t); |
| 1230 | bt_put(ts_begin_end_uint64_t); |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | static |
| 1235 | int try_map_clock_class(struct bt_ctf_stream_class *stream_class, |
| 1236 | struct bt_ctf_field_type *ft) |
| 1237 | { |
| 1238 | struct bt_ctf_clock_class *mapped_clock_class = NULL; |
| 1239 | int ret = 0; |
| 1240 | |
| 1241 | if (!ft) { |
| 1242 | /* Field does not exist: not an error */ |
| 1243 | goto end; |
| 1244 | } |
| 1245 | |
| 1246 | assert(bt_ctf_field_type_is_integer(ft)); |
| 1247 | mapped_clock_class = |
| 1248 | bt_ctf_field_type_integer_get_mapped_clock_class(ft); |
| 1249 | if (!mapped_clock_class) { |
| 1250 | if (!stream_class->clock) { |
| 1251 | BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: " |
| 1252 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 1253 | "stream-class-id=%" PRId64 ", ft-addr=%p", |
| 1254 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1255 | bt_ctf_stream_class_get_id(stream_class), ft); |
| 1256 | ret = -1; |
| 1257 | goto end; |
| 1258 | } |
| 1259 | |
| 1260 | ret = bt_ctf_field_type_integer_set_mapped_clock_class_no_check( |
| 1261 | ft, stream_class->clock->clock_class); |
| 1262 | if (ret) { |
| 1263 | BT_LOGW("Cannot set field type's mapped clock class: " |
| 1264 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 1265 | "stream-class-id=%" PRId64 ", ft-addr=%p", |
| 1266 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1267 | bt_ctf_stream_class_get_id(stream_class), ft); |
| 1268 | goto end; |
| 1269 | } |
| 1270 | |
| 1271 | BT_LOGV("Automatically mapped field type to stream class's clock class: " |
| 1272 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 1273 | "stream-class-id=%" PRId64 ", ft-addr=%p", |
| 1274 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1275 | bt_ctf_stream_class_get_id(stream_class), ft); |
| 1276 | } |
| 1277 | |
| 1278 | end: |
| 1279 | bt_put(mapped_clock_class); |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | BT_HIDDEN |
| 1284 | int bt_ctf_stream_class_map_clock_class( |
| 1285 | struct bt_ctf_stream_class *stream_class, |
| 1286 | struct bt_ctf_field_type *packet_context_type, |
| 1287 | struct bt_ctf_field_type *event_header_type) |
| 1288 | { |
| 1289 | struct bt_ctf_field_type *ft = NULL; |
| 1290 | int ret = 0; |
| 1291 | |
| 1292 | assert(stream_class); |
| 1293 | |
| 1294 | if (packet_context_type) { |
| 1295 | ft = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1296 | packet_context_type, "timestamp_begin"); |
| 1297 | if (try_map_clock_class(stream_class, ft)) { |
| 1298 | BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class."); |
| 1299 | ret = -1; |
| 1300 | goto end; |
| 1301 | } |
| 1302 | |
| 1303 | bt_put(ft); |
| 1304 | ft = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1305 | packet_context_type, "timestamp_end"); |
| 1306 | if (try_map_clock_class(stream_class, ft)) { |
| 1307 | BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class."); |
| 1308 | ret = -1; |
| 1309 | goto end; |
| 1310 | } |
| 1311 | |
| 1312 | BT_PUT(ft); |
| 1313 | } |
| 1314 | |
| 1315 | if (event_header_type) { |
| 1316 | ft = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1317 | event_header_type, "timestamp"); |
| 1318 | if (try_map_clock_class(stream_class, ft)) { |
| 1319 | BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class."); |
| 1320 | ret = -1; |
| 1321 | goto end; |
| 1322 | } |
| 1323 | |
| 1324 | BT_PUT(ft); |
| 1325 | } |
| 1326 | |
| 1327 | end: |
| 1328 | return ret; |
| 1329 | } |