| 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 | #include <babeltrace/ctf-writer/clock.h> |
| 30 | #include <babeltrace/ctf-ir/clock-internal.h> |
| 31 | #include <babeltrace/ctf-writer/event.h> |
| 32 | #include <babeltrace/ctf-ir/event-class-internal.h> |
| 33 | #include <babeltrace/ctf-ir/event-internal.h> |
| 34 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 35 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 36 | #include <babeltrace/ctf-writer/stream.h> |
| 37 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 38 | #include <babeltrace/ctf-ir/validation-internal.h> |
| 39 | #include <babeltrace/ctf-ir/visitor-internal.h> |
| 40 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 41 | #include <babeltrace/ctf-ir/utils.h> |
| 42 | #include <babeltrace/ref.h> |
| 43 | #include <babeltrace/compiler.h> |
| 44 | #include <babeltrace/align.h> |
| 45 | #include <babeltrace/endian.h> |
| 46 | |
| 47 | static |
| 48 | void bt_ctf_stream_class_destroy(struct bt_object *obj); |
| 49 | static |
| 50 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
| 51 | static |
| 52 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
| 53 | |
| 54 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) |
| 55 | { |
| 56 | int ret; |
| 57 | struct bt_ctf_stream_class *stream_class = NULL; |
| 58 | |
| 59 | if (name && bt_ctf_validate_identifier(name)) { |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | stream_class = g_new0(struct bt_ctf_stream_class, 1); |
| 64 | if (!stream_class) { |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | stream_class->name = g_string_new(name); |
| 69 | stream_class->event_classes = g_ptr_array_new_with_free_func( |
| 70 | (GDestroyNotify) bt_object_release); |
| 71 | if (!stream_class->event_classes) { |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | ret = init_event_header(stream_class); |
| 76 | if (ret) { |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | ret = init_packet_context(stream_class); |
| 81 | if (ret) { |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | bt_object_init(stream_class, bt_ctf_stream_class_destroy); |
| 86 | return stream_class; |
| 87 | |
| 88 | error: |
| 89 | BT_PUT(stream_class); |
| 90 | return stream_class; |
| 91 | } |
| 92 | |
| 93 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
| 94 | struct bt_ctf_stream_class *stream_class) |
| 95 | { |
| 96 | return (struct bt_ctf_trace *) bt_object_get_parent( |
| 97 | stream_class); |
| 98 | } |
| 99 | |
| 100 | const char *bt_ctf_stream_class_get_name( |
| 101 | struct bt_ctf_stream_class *stream_class) |
| 102 | { |
| 103 | const char *name = NULL; |
| 104 | |
| 105 | if (!stream_class) { |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | name = stream_class->name->str; |
| 110 | end: |
| 111 | return name; |
| 112 | } |
| 113 | |
| 114 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
| 115 | const char *name) |
| 116 | { |
| 117 | int ret = 0; |
| 118 | |
| 119 | if (!stream_class || stream_class->frozen) { |
| 120 | ret = -1; |
| 121 | goto end; |
| 122 | } |
| 123 | |
| 124 | g_string_assign(stream_class->name, name); |
| 125 | end: |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
| 130 | struct bt_ctf_stream_class *stream_class) |
| 131 | { |
| 132 | struct bt_ctf_clock *clock = NULL; |
| 133 | |
| 134 | if (!stream_class || !stream_class->clock) { |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | clock = stream_class->clock; |
| 139 | bt_get(clock); |
| 140 | end: |
| 141 | return clock; |
| 142 | } |
| 143 | |
| 144 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
| 145 | struct bt_ctf_clock *clock) |
| 146 | { |
| 147 | int ret = 0; |
| 148 | struct bt_ctf_field_type *timestamp_field = NULL; |
| 149 | |
| 150 | if (!stream_class || !clock || stream_class->frozen) { |
| 151 | ret = -1; |
| 152 | goto end; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Look for a "timestamp" field in the stream class' event header type |
| 157 | * and map the stream's clock to that field if no current mapping is |
| 158 | * currently set. |
| 159 | */ |
| 160 | timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name( |
| 161 | stream_class->event_header_type, "timestamp"); |
| 162 | if (timestamp_field) { |
| 163 | struct bt_ctf_clock *mapped_clock; |
| 164 | |
| 165 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( |
| 166 | timestamp_field); |
| 167 | if (mapped_clock) { |
| 168 | bt_put(mapped_clock); |
| 169 | goto end; |
| 170 | } |
| 171 | |
| 172 | ret = bt_ctf_field_type_integer_set_mapped_clock( |
| 173 | timestamp_field, clock); |
| 174 | if (ret) { |
| 175 | goto end; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (stream_class->clock) { |
| 180 | bt_put(stream_class->clock); |
| 181 | } |
| 182 | |
| 183 | stream_class->clock = clock; |
| 184 | bt_get(clock); |
| 185 | end: |
| 186 | if (timestamp_field) { |
| 187 | bt_put(timestamp_field); |
| 188 | } |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
| 193 | { |
| 194 | int64_t ret; |
| 195 | |
| 196 | if (!stream_class || !stream_class->id_set) { |
| 197 | ret = -1; |
| 198 | goto end; |
| 199 | } |
| 200 | |
| 201 | ret = (int64_t) stream_class->id; |
| 202 | end: |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | BT_HIDDEN |
| 207 | int _bt_ctf_stream_class_set_id( |
| 208 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 209 | { |
| 210 | stream_class->id = id; |
| 211 | stream_class->id_set = 1; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | struct event_class_set_stream_id_data { |
| 216 | uint32_t stream_id; |
| 217 | int ret; |
| 218 | }; |
| 219 | |
| 220 | static |
| 221 | void event_class_set_stream_id(gpointer event_class, gpointer data) |
| 222 | { |
| 223 | struct event_class_set_stream_id_data *typed_data = data; |
| 224 | |
| 225 | typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class, |
| 226 | typed_data->stream_id); |
| 227 | } |
| 228 | |
| 229 | BT_HIDDEN |
| 230 | int bt_ctf_stream_class_set_id_no_check( |
| 231 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 232 | { |
| 233 | int ret = 0; |
| 234 | struct event_class_set_stream_id_data data = |
| 235 | { .stream_id = id, .ret = 0 }; |
| 236 | |
| 237 | /* |
| 238 | * Make sure all event classes have their "stream_id" attribute |
| 239 | * set to this value. |
| 240 | */ |
| 241 | g_ptr_array_foreach(stream_class->event_classes, |
| 242 | event_class_set_stream_id, &data); |
| 243 | ret = data.ret; |
| 244 | if (ret) { |
| 245 | goto end; |
| 246 | } |
| 247 | |
| 248 | ret = _bt_ctf_stream_class_set_id(stream_class, id); |
| 249 | if (ret) { |
| 250 | goto end; |
| 251 | } |
| 252 | end: |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
| 257 | uint32_t id) |
| 258 | { |
| 259 | int ret = 0; |
| 260 | |
| 261 | if (!stream_class || stream_class->frozen) { |
| 262 | ret = -1; |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | ret = bt_ctf_stream_class_set_id_no_check(stream_class, id); |
| 267 | end: |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static |
| 272 | void event_class_exists(gpointer element, gpointer query) |
| 273 | { |
| 274 | struct bt_ctf_event_class *event_class_a = element; |
| 275 | struct search_query *search_query = query; |
| 276 | struct bt_ctf_event_class *event_class_b = search_query->value; |
| 277 | int64_t id_a, id_b; |
| 278 | |
| 279 | if (search_query->value == element) { |
| 280 | search_query->found = 1; |
| 281 | goto end; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Two event classes cannot share the same name in a given |
| 286 | * stream class. |
| 287 | */ |
| 288 | if (!strcmp(bt_ctf_event_class_get_name(event_class_a), |
| 289 | bt_ctf_event_class_get_name(event_class_b))) { |
| 290 | search_query->found = 1; |
| 291 | goto end; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Two event classes cannot share the same ID in a given |
| 296 | * stream class. |
| 297 | */ |
| 298 | id_a = bt_ctf_event_class_get_id(event_class_a); |
| 299 | id_b = bt_ctf_event_class_get_id(event_class_b); |
| 300 | |
| 301 | if (id_a < 0 || id_b < 0) { |
| 302 | /* at least one ID is not set: will be automatically set later */ |
| 303 | goto end; |
| 304 | } |
| 305 | |
| 306 | if (id_a == id_b) { |
| 307 | search_query->found = 1; |
| 308 | goto end; |
| 309 | } |
| 310 | |
| 311 | end: |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | int bt_ctf_stream_class_add_event_class( |
| 316 | struct bt_ctf_stream_class *stream_class, |
| 317 | struct bt_ctf_event_class *event_class) |
| 318 | { |
| 319 | int ret = 0; |
| 320 | int64_t event_id; |
| 321 | struct bt_ctf_trace *trace = NULL; |
| 322 | struct bt_ctf_stream_class *old_stream_class = NULL; |
| 323 | struct bt_ctf_validation_output validation_output = { 0 }; |
| 324 | struct bt_ctf_field_type *packet_header_type = NULL; |
| 325 | struct bt_ctf_field_type *packet_context_type = NULL; |
| 326 | struct bt_ctf_field_type *event_header_type = NULL; |
| 327 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; |
| 328 | struct bt_ctf_field_type *event_context_type = NULL; |
| 329 | struct bt_ctf_field_type *event_payload_type = NULL; |
| 330 | const enum bt_ctf_validation_flag validation_flags = |
| 331 | BT_CTF_VALIDATION_FLAG_EVENT; |
| 332 | |
| 333 | if (!stream_class || !event_class) { |
| 334 | ret = -1; |
| 335 | goto end; |
| 336 | } |
| 337 | |
| 338 | /* Check for duplicate event classes */ |
| 339 | struct search_query query = { .value = event_class, .found = 0 }; |
| 340 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
| 341 | &query); |
| 342 | if (query.found) { |
| 343 | ret = -1; |
| 344 | goto end; |
| 345 | } |
| 346 | |
| 347 | old_stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 348 | if (old_stream_class) { |
| 349 | /* Event class is already associated to a stream class. */ |
| 350 | ret = -1; |
| 351 | goto end; |
| 352 | } |
| 353 | |
| 354 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 355 | if (trace) { |
| 356 | /* |
| 357 | * If the stream class is associated with a trace, then |
| 358 | * both those objects are frozen. Also, this event class |
| 359 | * is about to be frozen. |
| 360 | * |
| 361 | * Therefore the event class must be validated here. |
| 362 | * The trace and stream class should be valid at this |
| 363 | * point. |
| 364 | */ |
| 365 | assert(trace->valid); |
| 366 | assert(stream_class->valid); |
| 367 | packet_header_type = |
| 368 | bt_ctf_trace_get_packet_header_type(trace); |
| 369 | packet_context_type = |
| 370 | bt_ctf_stream_class_get_packet_context_type( |
| 371 | stream_class); |
| 372 | event_header_type = |
| 373 | bt_ctf_stream_class_get_event_header_type(stream_class); |
| 374 | stream_event_ctx_type = |
| 375 | bt_ctf_stream_class_get_event_context_type( |
| 376 | stream_class); |
| 377 | event_context_type = |
| 378 | bt_ctf_event_class_get_context_type(event_class); |
| 379 | event_payload_type = |
| 380 | bt_ctf_event_class_get_payload_type(event_class); |
| 381 | ret = bt_ctf_validate_class_types( |
| 382 | trace->environment, packet_header_type, |
| 383 | packet_context_type, event_header_type, |
| 384 | stream_event_ctx_type, event_context_type, |
| 385 | event_payload_type, trace->valid, |
| 386 | stream_class->valid, event_class->valid, |
| 387 | &validation_output, validation_flags); |
| 388 | BT_PUT(packet_header_type); |
| 389 | BT_PUT(packet_context_type); |
| 390 | BT_PUT(event_header_type); |
| 391 | BT_PUT(stream_event_ctx_type); |
| 392 | BT_PUT(event_context_type); |
| 393 | BT_PUT(event_payload_type); |
| 394 | |
| 395 | if (ret) { |
| 396 | /* |
| 397 | * This means something went wrong during the |
| 398 | * validation process, not that the objects are |
| 399 | * invalid. |
| 400 | */ |
| 401 | goto end; |
| 402 | } |
| 403 | |
| 404 | if ((validation_output.valid_flags & validation_flags) != |
| 405 | validation_flags) { |
| 406 | /* Invalid event class */ |
| 407 | ret = -1; |
| 408 | goto end; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* Only set an event ID if none was explicitly set before */ |
| 413 | event_id = bt_ctf_event_class_get_id(event_class); |
| 414 | if (event_id < 0) { |
| 415 | if (bt_ctf_event_class_set_id(event_class, |
| 416 | stream_class->next_event_id++)) { |
| 417 | ret = -1; |
| 418 | goto end; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); |
| 423 | if (ret) { |
| 424 | goto end; |
| 425 | } |
| 426 | |
| 427 | bt_object_set_parent(event_class, stream_class); |
| 428 | |
| 429 | if (trace) { |
| 430 | /* |
| 431 | * At this point we know that the function will be |
| 432 | * successful. Therefore we can replace the event |
| 433 | * class's field types with what's in the validation |
| 434 | * output structure and mark this event class as valid. |
| 435 | */ |
| 436 | bt_ctf_validation_replace_types(NULL, NULL, event_class, |
| 437 | &validation_output, validation_flags); |
| 438 | event_class->valid = 1; |
| 439 | |
| 440 | /* |
| 441 | * Put what was not moved in |
| 442 | * bt_ctf_validation_replace_types(). |
| 443 | */ |
| 444 | bt_ctf_validation_output_put_types(&validation_output); |
| 445 | } |
| 446 | |
| 447 | /* Add to the event classes of the stream class */ |
| 448 | g_ptr_array_add(stream_class->event_classes, event_class); |
| 449 | |
| 450 | /* Freeze the event class */ |
| 451 | bt_ctf_event_class_freeze(event_class); |
| 452 | |
| 453 | if (stream_class->byte_order) { |
| 454 | /* |
| 455 | * Only set native byte order if it has been initialized |
| 456 | * when the stream class was added to a trace. |
| 457 | * |
| 458 | * If not set here, this will be set when the stream |
| 459 | * class is added to a trace. |
| 460 | */ |
| 461 | bt_ctf_event_class_set_native_byte_order(event_class, |
| 462 | stream_class->byte_order); |
| 463 | } |
| 464 | |
| 465 | /* Notifiy listeners of the trace's schema modification. */ |
| 466 | if (trace) { |
| 467 | struct bt_ctf_ir_element element = { .element = event_class, |
| 468 | .type = BT_CTF_IR_TYPE_EVENT_CLASS }; |
| 469 | |
| 470 | (void) bt_ctf_trace_element_modification(&element, trace); |
| 471 | } |
| 472 | end: |
| 473 | BT_PUT(trace); |
| 474 | BT_PUT(old_stream_class); |
| 475 | bt_ctf_validation_output_put_types(&validation_output); |
| 476 | assert(!packet_header_type); |
| 477 | assert(!packet_context_type); |
| 478 | assert(!event_header_type); |
| 479 | assert(!stream_event_ctx_type); |
| 480 | assert(!event_context_type); |
| 481 | assert(!event_payload_type); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | int bt_ctf_stream_class_get_event_class_count( |
| 487 | struct bt_ctf_stream_class *stream_class) |
| 488 | { |
| 489 | int ret; |
| 490 | |
| 491 | if (!stream_class) { |
| 492 | ret = -1; |
| 493 | goto end; |
| 494 | } |
| 495 | |
| 496 | ret = (int) stream_class->event_classes->len; |
| 497 | end: |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class( |
| 502 | struct bt_ctf_stream_class *stream_class, int index) |
| 503 | { |
| 504 | struct bt_ctf_event_class *event_class = NULL; |
| 505 | |
| 506 | if (!stream_class || index < 0 || |
| 507 | index >= stream_class->event_classes->len) { |
| 508 | goto end; |
| 509 | } |
| 510 | |
| 511 | event_class = g_ptr_array_index(stream_class->event_classes, index); |
| 512 | bt_get(event_class); |
| 513 | end: |
| 514 | return event_class; |
| 515 | } |
| 516 | |
| 517 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name( |
| 518 | struct bt_ctf_stream_class *stream_class, const char *name) |
| 519 | { |
| 520 | size_t i; |
| 521 | struct bt_ctf_event_class *event_class = NULL; |
| 522 | |
| 523 | if (!stream_class || !name) { |
| 524 | goto end; |
| 525 | } |
| 526 | |
| 527 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 528 | struct bt_ctf_event_class *cur_event_class = |
| 529 | g_ptr_array_index(stream_class->event_classes, i); |
| 530 | const char *cur_event_class_name = |
| 531 | bt_ctf_event_class_get_name(cur_event_class); |
| 532 | |
| 533 | if (!strcmp(name, cur_event_class_name)) { |
| 534 | event_class = cur_event_class; |
| 535 | bt_get(event_class); |
| 536 | goto end; |
| 537 | } |
| 538 | } |
| 539 | end: |
| 540 | return event_class; |
| 541 | } |
| 542 | |
| 543 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
| 544 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 545 | { |
| 546 | size_t i; |
| 547 | struct bt_ctf_event_class *event_class = NULL; |
| 548 | |
| 549 | if (!stream_class) { |
| 550 | goto end; |
| 551 | } |
| 552 | |
| 553 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 554 | struct bt_ctf_event_class *current_event_class = |
| 555 | g_ptr_array_index(stream_class->event_classes, i); |
| 556 | |
| 557 | if (bt_ctf_event_class_get_id(current_event_class) == id) { |
| 558 | event_class = current_event_class; |
| 559 | bt_get(event_class); |
| 560 | goto end; |
| 561 | } |
| 562 | } |
| 563 | end: |
| 564 | return event_class; |
| 565 | } |
| 566 | |
| 567 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
| 568 | struct bt_ctf_stream_class *stream_class) |
| 569 | { |
| 570 | struct bt_ctf_field_type *ret = NULL; |
| 571 | |
| 572 | if (!stream_class) { |
| 573 | goto end; |
| 574 | } |
| 575 | |
| 576 | assert(stream_class->packet_context_type); |
| 577 | bt_get(stream_class->packet_context_type); |
| 578 | ret = stream_class->packet_context_type; |
| 579 | end: |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | int bt_ctf_stream_class_set_packet_context_type( |
| 584 | struct bt_ctf_stream_class *stream_class, |
| 585 | struct bt_ctf_field_type *packet_context_type) |
| 586 | { |
| 587 | int ret = 0; |
| 588 | |
| 589 | if (!stream_class || !packet_context_type || stream_class->frozen) { |
| 590 | ret = -1; |
| 591 | goto end; |
| 592 | } |
| 593 | |
| 594 | assert(stream_class->packet_context_type); |
| 595 | if (stream_class->packet_context_type == packet_context_type) { |
| 596 | goto end; |
| 597 | } |
| 598 | if (bt_ctf_field_type_get_type_id(packet_context_type) != |
| 599 | BT_CTF_TYPE_ID_STRUCT) { |
| 600 | /* A packet context must be a structure */ |
| 601 | ret = -1; |
| 602 | goto end; |
| 603 | } |
| 604 | |
| 605 | bt_put(stream_class->packet_context_type); |
| 606 | bt_get(packet_context_type); |
| 607 | stream_class->packet_context_type = packet_context_type; |
| 608 | end: |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
| 613 | struct bt_ctf_stream_class *stream_class) |
| 614 | { |
| 615 | struct bt_ctf_field_type *ret = NULL; |
| 616 | |
| 617 | if (!stream_class || !stream_class->event_header_type) { |
| 618 | goto end; |
| 619 | } |
| 620 | |
| 621 | assert(stream_class->event_header_type); |
| 622 | bt_get(stream_class->event_header_type); |
| 623 | ret = stream_class->event_header_type; |
| 624 | end: |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | int bt_ctf_stream_class_set_event_header_type( |
| 629 | struct bt_ctf_stream_class *stream_class, |
| 630 | struct bt_ctf_field_type *event_header_type) |
| 631 | { |
| 632 | int ret = 0; |
| 633 | |
| 634 | if (!stream_class || !event_header_type || stream_class->frozen) { |
| 635 | ret = -1; |
| 636 | goto end; |
| 637 | } |
| 638 | |
| 639 | assert(stream_class->event_header_type); |
| 640 | if (stream_class->event_header_type == event_header_type) { |
| 641 | goto end; |
| 642 | } |
| 643 | if (bt_ctf_field_type_get_type_id(event_header_type) != |
| 644 | BT_CTF_TYPE_ID_STRUCT) { |
| 645 | /* An event header must be a structure */ |
| 646 | ret = -1; |
| 647 | goto end; |
| 648 | } |
| 649 | |
| 650 | bt_put(stream_class->event_header_type); |
| 651 | bt_get(event_header_type); |
| 652 | stream_class->event_header_type = event_header_type; |
| 653 | end: |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
| 658 | struct bt_ctf_stream_class *stream_class) |
| 659 | { |
| 660 | struct bt_ctf_field_type *ret = NULL; |
| 661 | |
| 662 | if (!stream_class || !stream_class->event_context_type) { |
| 663 | goto end; |
| 664 | } |
| 665 | |
| 666 | assert(stream_class->event_context_type); |
| 667 | bt_get(stream_class->event_context_type); |
| 668 | ret = stream_class->event_context_type; |
| 669 | end: |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | int bt_ctf_stream_class_set_event_context_type( |
| 674 | struct bt_ctf_stream_class *stream_class, |
| 675 | struct bt_ctf_field_type *event_context_type) |
| 676 | { |
| 677 | int ret = 0; |
| 678 | |
| 679 | if (!stream_class || !event_context_type || stream_class->frozen) { |
| 680 | ret = -1; |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | if (bt_ctf_field_type_get_type_id(event_context_type) != |
| 685 | BT_CTF_TYPE_ID_STRUCT) { |
| 686 | /* A packet context must be a structure */ |
| 687 | ret = -1; |
| 688 | goto end; |
| 689 | } |
| 690 | |
| 691 | bt_put(stream_class->event_context_type); |
| 692 | bt_get(event_context_type); |
| 693 | stream_class->event_context_type = event_context_type; |
| 694 | end: |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
| 699 | { |
| 700 | bt_get(stream_class); |
| 701 | } |
| 702 | |
| 703 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) |
| 704 | { |
| 705 | bt_put(stream_class); |
| 706 | } |
| 707 | |
| 708 | static |
| 709 | int get_event_class_count(void *element) |
| 710 | { |
| 711 | return bt_ctf_stream_class_get_event_class_count( |
| 712 | (struct bt_ctf_stream_class *) element); |
| 713 | } |
| 714 | |
| 715 | static |
| 716 | void *get_event_class(void *element, int i) |
| 717 | { |
| 718 | return bt_ctf_stream_class_get_event_class( |
| 719 | (struct bt_ctf_stream_class *) element, i); |
| 720 | } |
| 721 | |
| 722 | static |
| 723 | int visit_event_class(void *element, bt_ctf_ir_visitor visitor,void *data) |
| 724 | { |
| 725 | struct bt_ctf_ir_element ir_element = |
| 726 | { .element = element, |
| 727 | .type = BT_CTF_IR_TYPE_EVENT_CLASS }; |
| 728 | |
| 729 | return visitor(&ir_element, data); |
| 730 | } |
| 731 | |
| 732 | int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class, |
| 733 | bt_ctf_ir_visitor visitor, void *data) |
| 734 | { |
| 735 | int ret; |
| 736 | struct bt_ctf_ir_element element = |
| 737 | { .element = stream_class, |
| 738 | .type = BT_CTF_IR_TYPE_STREAM_CLASS }; |
| 739 | |
| 740 | if (!stream_class || !visitor) { |
| 741 | ret = -1; |
| 742 | goto end; |
| 743 | } |
| 744 | |
| 745 | ret = visitor_helper(&element, get_event_class_count, |
| 746 | get_event_class, |
| 747 | visit_event_class, visitor, data); |
| 748 | end: |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | BT_HIDDEN |
| 753 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) |
| 754 | { |
| 755 | if (!stream_class) { |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | stream_class->frozen = 1; |
| 760 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
| 761 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
| 762 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
| 763 | bt_ctf_clock_freeze(stream_class->clock); |
| 764 | } |
| 765 | |
| 766 | BT_HIDDEN |
| 767 | void bt_ctf_stream_class_set_byte_order( |
| 768 | struct bt_ctf_stream_class *stream_class, int byte_order) |
| 769 | { |
| 770 | int i; |
| 771 | |
| 772 | assert(stream_class); |
| 773 | assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN); |
| 774 | stream_class->byte_order = byte_order; |
| 775 | |
| 776 | /* Set native byte order to little or big endian */ |
| 777 | bt_ctf_field_type_set_native_byte_order( |
| 778 | stream_class->event_header_type, byte_order); |
| 779 | bt_ctf_field_type_set_native_byte_order( |
| 780 | stream_class->packet_context_type, byte_order); |
| 781 | bt_ctf_field_type_set_native_byte_order( |
| 782 | stream_class->event_context_type, byte_order); |
| 783 | |
| 784 | /* Set all events' native byte order */ |
| 785 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 786 | struct bt_ctf_event_class *event_class = |
| 787 | g_ptr_array_index(stream_class->event_classes, i); |
| 788 | |
| 789 | bt_ctf_event_class_set_native_byte_order(event_class, |
| 790 | byte_order); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | BT_HIDDEN |
| 795 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, |
| 796 | struct metadata_context *context) |
| 797 | { |
| 798 | int64_t ret = 0; |
| 799 | size_t i; |
| 800 | |
| 801 | g_string_assign(context->field_name, ""); |
| 802 | context->current_indentation_level = 1; |
| 803 | if (!stream_class->id_set) { |
| 804 | ret = -1; |
| 805 | goto end; |
| 806 | } |
| 807 | |
| 808 | g_string_append_printf(context->string, |
| 809 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", |
| 810 | stream_class->id); |
| 811 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, |
| 812 | context); |
| 813 | if (ret) { |
| 814 | goto end; |
| 815 | } |
| 816 | |
| 817 | g_string_append(context->string, ";\n\n\tpacket.context := "); |
| 818 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, |
| 819 | context); |
| 820 | if (ret) { |
| 821 | goto end; |
| 822 | } |
| 823 | |
| 824 | if (stream_class->event_context_type) { |
| 825 | g_string_append(context->string, ";\n\n\tevent.context := "); |
| 826 | ret = bt_ctf_field_type_serialize( |
| 827 | stream_class->event_context_type, context); |
| 828 | if (ret) { |
| 829 | goto end; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | g_string_append(context->string, ";\n};\n\n"); |
| 834 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 835 | struct bt_ctf_event_class *event_class = |
| 836 | stream_class->event_classes->pdata[i]; |
| 837 | |
| 838 | ret = bt_ctf_event_class_serialize(event_class, context); |
| 839 | if (ret) { |
| 840 | goto end; |
| 841 | } |
| 842 | } |
| 843 | end: |
| 844 | context->current_indentation_level = 0; |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | static |
| 849 | void bt_ctf_stream_class_destroy(struct bt_object *obj) |
| 850 | { |
| 851 | struct bt_ctf_stream_class *stream_class; |
| 852 | |
| 853 | stream_class = container_of(obj, struct bt_ctf_stream_class, base); |
| 854 | bt_put(stream_class->clock); |
| 855 | |
| 856 | if (stream_class->event_classes) { |
| 857 | g_ptr_array_free(stream_class->event_classes, TRUE); |
| 858 | } |
| 859 | |
| 860 | if (stream_class->name) { |
| 861 | g_string_free(stream_class->name, TRUE); |
| 862 | } |
| 863 | |
| 864 | bt_put(stream_class->event_header_type); |
| 865 | bt_put(stream_class->packet_context_type); |
| 866 | bt_put(stream_class->event_context_type); |
| 867 | g_free(stream_class); |
| 868 | } |
| 869 | |
| 870 | static |
| 871 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
| 872 | { |
| 873 | int ret = 0; |
| 874 | struct bt_ctf_field_type *event_header_type = |
| 875 | bt_ctf_field_type_structure_create(); |
| 876 | struct bt_ctf_field_type *_uint32_t = |
| 877 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 878 | struct bt_ctf_field_type *_uint64_t = |
| 879 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 880 | |
| 881 | if (!event_header_type) { |
| 882 | ret = -1; |
| 883 | goto end; |
| 884 | } |
| 885 | |
| 886 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 887 | _uint32_t, "id"); |
| 888 | if (ret) { |
| 889 | goto end; |
| 890 | } |
| 891 | |
| 892 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 893 | _uint64_t, "timestamp"); |
| 894 | if (ret) { |
| 895 | goto end; |
| 896 | } |
| 897 | |
| 898 | if (stream_class->event_header_type) { |
| 899 | bt_put(stream_class->event_header_type); |
| 900 | } |
| 901 | stream_class->event_header_type = event_header_type; |
| 902 | end: |
| 903 | if (ret) { |
| 904 | bt_put(event_header_type); |
| 905 | } |
| 906 | |
| 907 | bt_put(_uint32_t); |
| 908 | bt_put(_uint64_t); |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | static |
| 913 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
| 914 | { |
| 915 | int ret = 0; |
| 916 | struct bt_ctf_field_type *packet_context_type = |
| 917 | bt_ctf_field_type_structure_create(); |
| 918 | struct bt_ctf_field_type *_uint64_t = |
| 919 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 920 | |
| 921 | if (!packet_context_type) { |
| 922 | ret = -1; |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * We create a stream packet context as proposed in the CTF |
| 928 | * specification. |
| 929 | */ |
| 930 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 931 | _uint64_t, "timestamp_begin"); |
| 932 | if (ret) { |
| 933 | goto end; |
| 934 | } |
| 935 | |
| 936 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 937 | _uint64_t, "timestamp_end"); |
| 938 | if (ret) { |
| 939 | goto end; |
| 940 | } |
| 941 | |
| 942 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 943 | _uint64_t, "content_size"); |
| 944 | if (ret) { |
| 945 | goto end; |
| 946 | } |
| 947 | |
| 948 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 949 | _uint64_t, "packet_size"); |
| 950 | if (ret) { |
| 951 | goto end; |
| 952 | } |
| 953 | |
| 954 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 955 | _uint64_t, "events_discarded"); |
| 956 | if (ret) { |
| 957 | goto end; |
| 958 | } |
| 959 | |
| 960 | bt_put(stream_class->packet_context_type); |
| 961 | stream_class->packet_context_type = packet_context_type; |
| 962 | end: |
| 963 | if (ret) { |
| 964 | bt_put(packet_context_type); |
| 965 | goto end; |
| 966 | } |
| 967 | |
| 968 | bt_put(_uint64_t); |
| 969 | return ret; |
| 970 | } |