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