| 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-internal.h> |
| 33 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
| 35 | #include <babeltrace/ctf-writer/stream.h> |
| 36 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 37 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 38 | #include <babeltrace/ctf-ir/utils.h> |
| 39 | #include <babeltrace/compiler.h> |
| 40 | #include <babeltrace/align.h> |
| 41 | |
| 42 | static |
| 43 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref); |
| 44 | static |
| 45 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
| 46 | static |
| 47 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
| 48 | |
| 49 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) |
| 50 | { |
| 51 | int ret; |
| 52 | struct bt_ctf_stream_class *stream_class = NULL; |
| 53 | |
| 54 | if (name && bt_ctf_validate_identifier(name)) { |
| 55 | goto error; |
| 56 | } |
| 57 | |
| 58 | stream_class = g_new0(struct bt_ctf_stream_class, 1); |
| 59 | if (!stream_class) { |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | stream_class->name = g_string_new(name); |
| 64 | stream_class->event_classes = g_ptr_array_new_with_free_func( |
| 65 | (GDestroyNotify)bt_ctf_event_class_put); |
| 66 | if (!stream_class->event_classes) { |
| 67 | goto error_destroy; |
| 68 | } |
| 69 | |
| 70 | ret = init_event_header(stream_class); |
| 71 | if (ret) { |
| 72 | goto error_destroy; |
| 73 | } |
| 74 | |
| 75 | ret = init_packet_context(stream_class); |
| 76 | if (ret) { |
| 77 | goto error_destroy; |
| 78 | } |
| 79 | |
| 80 | bt_ctf_ref_init(&stream_class->ref_count); |
| 81 | return stream_class; |
| 82 | |
| 83 | error_destroy: |
| 84 | bt_ctf_stream_class_destroy(&stream_class->ref_count); |
| 85 | stream_class = NULL; |
| 86 | error: |
| 87 | return stream_class; |
| 88 | } |
| 89 | |
| 90 | const char *bt_ctf_stream_class_get_name( |
| 91 | struct bt_ctf_stream_class *stream_class) |
| 92 | { |
| 93 | const char *name = NULL; |
| 94 | |
| 95 | if (!stream_class) { |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | name = stream_class->name->str; |
| 100 | end: |
| 101 | return name; |
| 102 | } |
| 103 | |
| 104 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
| 105 | const char *name) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | if (!stream_class || stream_class->frozen) { |
| 110 | ret = -1; |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | g_string_assign(stream_class->name, name); |
| 115 | end: |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
| 120 | struct bt_ctf_stream_class *stream_class) |
| 121 | { |
| 122 | struct bt_ctf_clock *clock = NULL; |
| 123 | |
| 124 | if (!stream_class || !stream_class->clock) { |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | clock = stream_class->clock; |
| 129 | bt_ctf_clock_get(clock); |
| 130 | end: |
| 131 | return clock; |
| 132 | } |
| 133 | |
| 134 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
| 135 | struct bt_ctf_clock *clock) |
| 136 | { |
| 137 | int ret = 0; |
| 138 | struct bt_ctf_field_type *timestamp_field = NULL; |
| 139 | |
| 140 | if (!stream_class || !clock || stream_class->frozen) { |
| 141 | ret = -1; |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Look for a "timestamp" field in the stream class' event header type |
| 147 | * and map the stream's clock to that field if no current mapping is |
| 148 | * currently set. |
| 149 | */ |
| 150 | timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name( |
| 151 | stream_class->event_header_type, "timestamp"); |
| 152 | if (timestamp_field) { |
| 153 | struct bt_ctf_clock *mapped_clock; |
| 154 | |
| 155 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( |
| 156 | timestamp_field); |
| 157 | if (mapped_clock) { |
| 158 | bt_ctf_clock_put(mapped_clock); |
| 159 | goto end; |
| 160 | } |
| 161 | |
| 162 | ret = bt_ctf_field_type_integer_set_mapped_clock( |
| 163 | timestamp_field, clock); |
| 164 | if (ret) { |
| 165 | goto end; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (stream_class->clock) { |
| 170 | bt_ctf_clock_put(stream_class->clock); |
| 171 | } |
| 172 | |
| 173 | stream_class->clock = clock; |
| 174 | bt_ctf_clock_get(clock); |
| 175 | end: |
| 176 | if (timestamp_field) { |
| 177 | bt_ctf_field_type_put(timestamp_field); |
| 178 | } |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
| 183 | { |
| 184 | int64_t ret; |
| 185 | |
| 186 | if (!stream_class || !stream_class->id_set) { |
| 187 | ret = -1; |
| 188 | goto end; |
| 189 | } |
| 190 | |
| 191 | ret = (int64_t) stream_class->id; |
| 192 | end: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | BT_HIDDEN |
| 197 | int _bt_ctf_stream_class_set_id( |
| 198 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 199 | { |
| 200 | stream_class->id = id; |
| 201 | stream_class->id_set = 1; |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
| 206 | uint32_t id) |
| 207 | { |
| 208 | int ret = 0; |
| 209 | |
| 210 | if (!stream_class || stream_class->frozen) { |
| 211 | ret = -1; |
| 212 | goto end; |
| 213 | } |
| 214 | |
| 215 | ret = _bt_ctf_stream_class_set_id(stream_class, id); |
| 216 | if (ret) { |
| 217 | goto end; |
| 218 | } |
| 219 | end: |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | int bt_ctf_stream_class_add_event_class( |
| 224 | struct bt_ctf_stream_class *stream_class, |
| 225 | struct bt_ctf_event_class *event_class) |
| 226 | { |
| 227 | int ret = 0; |
| 228 | int64_t event_id; |
| 229 | |
| 230 | if (!stream_class || !event_class) { |
| 231 | ret = -1; |
| 232 | goto end; |
| 233 | } |
| 234 | |
| 235 | /* Check for duplicate event classes */ |
| 236 | struct search_query query = { .value = event_class, .found = 0 }; |
| 237 | g_ptr_array_foreach(stream_class->event_classes, value_exists, &query); |
| 238 | if (query.found) { |
| 239 | ret = -1; |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | /* Only set an event id if none was explicitly set before */ |
| 244 | event_id = bt_ctf_event_class_get_id(event_class); |
| 245 | if (event_id < 0) { |
| 246 | if (bt_ctf_event_class_set_id(event_class, |
| 247 | stream_class->next_event_id++)) { |
| 248 | ret = -1; |
| 249 | goto end; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ret = bt_ctf_event_class_set_stream_class(event_class, stream_class); |
| 254 | if (ret) { |
| 255 | goto end; |
| 256 | } |
| 257 | |
| 258 | bt_ctf_event_class_get(event_class); |
| 259 | g_ptr_array_add(stream_class->event_classes, event_class); |
| 260 | bt_ctf_event_class_freeze(event_class); |
| 261 | |
| 262 | if (stream_class->byte_order) { |
| 263 | /* |
| 264 | * Only set native byte order if it has been initialized |
| 265 | * when the stream class was added to a trace. |
| 266 | * |
| 267 | * If not set here, this will be set when the stream |
| 268 | * classe will be added to a trace. |
| 269 | */ |
| 270 | bt_ctf_event_class_set_native_byte_order(event_class, |
| 271 | stream_class->byte_order); |
| 272 | } |
| 273 | end: |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | int bt_ctf_stream_class_get_event_class_count( |
| 278 | struct bt_ctf_stream_class *stream_class) |
| 279 | { |
| 280 | int ret; |
| 281 | |
| 282 | if (!stream_class) { |
| 283 | ret = -1; |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | ret = (int) stream_class->event_classes->len; |
| 288 | end: |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class( |
| 293 | struct bt_ctf_stream_class *stream_class, int index) |
| 294 | { |
| 295 | struct bt_ctf_event_class *event_class = NULL; |
| 296 | |
| 297 | if (!stream_class || index < 0 || |
| 298 | index >= stream_class->event_classes->len) { |
| 299 | goto end; |
| 300 | } |
| 301 | |
| 302 | event_class = g_ptr_array_index(stream_class->event_classes, index); |
| 303 | bt_ctf_event_class_get(event_class); |
| 304 | end: |
| 305 | return event_class; |
| 306 | } |
| 307 | |
| 308 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name( |
| 309 | struct bt_ctf_stream_class *stream_class, const char *name) |
| 310 | { |
| 311 | size_t i; |
| 312 | GQuark name_quark; |
| 313 | struct bt_ctf_event_class *event_class = NULL; |
| 314 | |
| 315 | if (!stream_class || !name) { |
| 316 | goto end; |
| 317 | } |
| 318 | |
| 319 | name_quark = g_quark_try_string(name); |
| 320 | if (!name_quark) { |
| 321 | goto end; |
| 322 | } |
| 323 | |
| 324 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 325 | struct bt_ctf_event_class *current_event_class = |
| 326 | g_ptr_array_index(stream_class->event_classes, i); |
| 327 | |
| 328 | if (name_quark == current_event_class->name) { |
| 329 | event_class = current_event_class; |
| 330 | bt_ctf_event_class_get(event_class); |
| 331 | goto end; |
| 332 | } |
| 333 | } |
| 334 | end: |
| 335 | return event_class; |
| 336 | } |
| 337 | |
| 338 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
| 339 | struct bt_ctf_stream_class *stream_class) |
| 340 | { |
| 341 | struct bt_ctf_field_type *ret = NULL; |
| 342 | |
| 343 | if (!stream_class) { |
| 344 | goto end; |
| 345 | } |
| 346 | |
| 347 | assert(stream_class->packet_context_type); |
| 348 | bt_ctf_field_type_get(stream_class->packet_context_type); |
| 349 | ret = stream_class->packet_context_type; |
| 350 | end: |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | int bt_ctf_stream_class_set_packet_context_type( |
| 355 | struct bt_ctf_stream_class *stream_class, |
| 356 | struct bt_ctf_field_type *packet_context_type) |
| 357 | { |
| 358 | int ret = 0; |
| 359 | |
| 360 | if (!stream_class || !packet_context_type || stream_class->frozen) { |
| 361 | ret = -1; |
| 362 | goto end; |
| 363 | } |
| 364 | |
| 365 | assert(stream_class->packet_context_type); |
| 366 | if (stream_class->packet_context_type == packet_context_type) { |
| 367 | goto end; |
| 368 | } |
| 369 | if (bt_ctf_field_type_get_type_id(packet_context_type) != |
| 370 | CTF_TYPE_STRUCT) { |
| 371 | /* A packet context must be a structure */ |
| 372 | ret = -1; |
| 373 | goto end; |
| 374 | } |
| 375 | |
| 376 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 377 | bt_ctf_field_type_get(packet_context_type); |
| 378 | stream_class->packet_context_type = packet_context_type; |
| 379 | end: |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
| 384 | struct bt_ctf_stream_class *stream_class) |
| 385 | { |
| 386 | struct bt_ctf_field_type *ret = NULL; |
| 387 | |
| 388 | if (!stream_class || !stream_class->event_header_type) { |
| 389 | goto end; |
| 390 | } |
| 391 | |
| 392 | assert(stream_class->event_header_type); |
| 393 | bt_ctf_field_type_get(stream_class->event_header_type); |
| 394 | ret = stream_class->event_header_type; |
| 395 | end: |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | int bt_ctf_stream_class_set_event_header_type( |
| 400 | struct bt_ctf_stream_class *stream_class, |
| 401 | struct bt_ctf_field_type *event_header_type) |
| 402 | { |
| 403 | int ret = 0; |
| 404 | |
| 405 | if (!stream_class || !event_header_type || stream_class->frozen) { |
| 406 | ret = -1; |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | assert(stream_class->event_header_type); |
| 411 | if (stream_class->event_header_type == event_header_type) { |
| 412 | goto end; |
| 413 | } |
| 414 | if (bt_ctf_field_type_get_type_id(event_header_type) != |
| 415 | CTF_TYPE_STRUCT) { |
| 416 | /* An event header must be a structure */ |
| 417 | ret = -1; |
| 418 | goto end; |
| 419 | } |
| 420 | |
| 421 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 422 | bt_ctf_field_type_get(event_header_type); |
| 423 | stream_class->event_header_type = event_header_type; |
| 424 | end: |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
| 429 | struct bt_ctf_stream_class *stream_class) |
| 430 | { |
| 431 | struct bt_ctf_field_type *ret = NULL; |
| 432 | |
| 433 | if (!stream_class || !stream_class->event_context_type) { |
| 434 | goto end; |
| 435 | } |
| 436 | |
| 437 | assert(stream_class->event_context_type); |
| 438 | bt_ctf_field_type_get(stream_class->event_context_type); |
| 439 | ret = stream_class->event_context_type; |
| 440 | end: |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | int bt_ctf_stream_class_set_event_context_type( |
| 445 | struct bt_ctf_stream_class *stream_class, |
| 446 | struct bt_ctf_field_type *event_context_type) |
| 447 | { |
| 448 | int ret = 0; |
| 449 | |
| 450 | if (!stream_class || !event_context_type || stream_class->frozen) { |
| 451 | ret = -1; |
| 452 | goto end; |
| 453 | } |
| 454 | |
| 455 | if (bt_ctf_field_type_get_type_id(event_context_type) != |
| 456 | CTF_TYPE_STRUCT) { |
| 457 | /* A packet context must be a structure */ |
| 458 | ret = -1; |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | bt_ctf_field_type_put(stream_class->event_context_type); |
| 463 | bt_ctf_field_type_get(event_context_type); |
| 464 | stream_class->event_context_type = event_context_type; |
| 465 | end: |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
| 470 | { |
| 471 | if (!stream_class) { |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | bt_ctf_ref_get(&stream_class->ref_count); |
| 476 | } |
| 477 | |
| 478 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) |
| 479 | { |
| 480 | if (!stream_class) { |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy); |
| 485 | } |
| 486 | |
| 487 | BT_HIDDEN |
| 488 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) |
| 489 | { |
| 490 | if (!stream_class) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | stream_class->frozen = 1; |
| 495 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
| 496 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
| 497 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
| 498 | bt_ctf_clock_freeze(stream_class->clock); |
| 499 | } |
| 500 | |
| 501 | BT_HIDDEN |
| 502 | int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class, |
| 503 | enum bt_ctf_byte_order byte_order) |
| 504 | { |
| 505 | int i, ret = 0; |
| 506 | int internal_byte_order; |
| 507 | |
| 508 | /* Note that "NATIVE" means the trace's endianness, not the host's. */ |
| 509 | if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN || |
| 510 | byte_order > BT_CTF_BYTE_ORDER_NETWORK) { |
| 511 | ret = -1; |
| 512 | goto end; |
| 513 | } |
| 514 | |
| 515 | switch (byte_order) { |
| 516 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 517 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 518 | internal_byte_order = BIG_ENDIAN; |
| 519 | break; |
| 520 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 521 | internal_byte_order = LITTLE_ENDIAN; |
| 522 | break; |
| 523 | default: |
| 524 | ret = -1; |
| 525 | goto end; |
| 526 | } |
| 527 | |
| 528 | stream_class->byte_order = internal_byte_order; |
| 529 | |
| 530 | /* Set native byte order to little or big endian */ |
| 531 | bt_ctf_field_type_set_native_byte_order( |
| 532 | stream_class->event_header_type, stream_class->byte_order); |
| 533 | bt_ctf_field_type_set_native_byte_order( |
| 534 | stream_class->packet_context_type, stream_class->byte_order); |
| 535 | bt_ctf_field_type_set_native_byte_order( |
| 536 | stream_class->event_context_type, stream_class->byte_order); |
| 537 | |
| 538 | /* Set all events' native byte order */ |
| 539 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 540 | bt_ctf_event_class_set_native_byte_order( |
| 541 | g_ptr_array_index(stream_class->event_classes, i), |
| 542 | stream_class->byte_order); |
| 543 | bt_ctf_event_class_freeze( |
| 544 | g_ptr_array_index(stream_class->event_classes, i)); |
| 545 | } |
| 546 | end: |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | BT_HIDDEN |
| 551 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, |
| 552 | struct metadata_context *context) |
| 553 | { |
| 554 | int64_t ret = 0; |
| 555 | size_t i; |
| 556 | |
| 557 | g_string_assign(context->field_name, ""); |
| 558 | context->current_indentation_level = 1; |
| 559 | if (!stream_class->id_set) { |
| 560 | ret = -1; |
| 561 | goto end; |
| 562 | } |
| 563 | |
| 564 | g_string_append_printf(context->string, |
| 565 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", |
| 566 | stream_class->id); |
| 567 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, |
| 568 | context); |
| 569 | if (ret) { |
| 570 | goto end; |
| 571 | } |
| 572 | |
| 573 | g_string_append(context->string, ";\n\n\tpacket.context := "); |
| 574 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, |
| 575 | context); |
| 576 | if (ret) { |
| 577 | goto end; |
| 578 | } |
| 579 | |
| 580 | if (stream_class->event_context_type) { |
| 581 | g_string_append(context->string, ";\n\n\tevent.context := "); |
| 582 | ret = bt_ctf_field_type_serialize( |
| 583 | stream_class->event_context_type, context); |
| 584 | if (ret) { |
| 585 | goto end; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | g_string_append(context->string, ";\n};\n\n"); |
| 590 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 591 | struct bt_ctf_event_class *event_class = |
| 592 | stream_class->event_classes->pdata[i]; |
| 593 | |
| 594 | ret = bt_ctf_event_class_serialize(event_class, context); |
| 595 | if (ret) { |
| 596 | goto end; |
| 597 | } |
| 598 | } |
| 599 | end: |
| 600 | context->current_indentation_level = 0; |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static |
| 605 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref) |
| 606 | { |
| 607 | struct bt_ctf_stream_class *stream_class; |
| 608 | |
| 609 | if (!ref) { |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count); |
| 614 | bt_ctf_clock_put(stream_class->clock); |
| 615 | |
| 616 | if (stream_class->event_classes) { |
| 617 | size_t i; |
| 618 | |
| 619 | /* Unregister this stream class from the event classes */ |
| 620 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 621 | struct bt_ctf_event_class *event_class = |
| 622 | g_ptr_array_index(stream_class->event_classes, |
| 623 | i); |
| 624 | |
| 625 | bt_ctf_event_class_set_stream_class(event_class, NULL); |
| 626 | } |
| 627 | |
| 628 | g_ptr_array_free(stream_class->event_classes, TRUE); |
| 629 | } |
| 630 | |
| 631 | if (stream_class->name) { |
| 632 | g_string_free(stream_class->name, TRUE); |
| 633 | } |
| 634 | |
| 635 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 636 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 637 | if (stream_class->event_context_type) { |
| 638 | bt_ctf_field_type_put(stream_class->event_context_type); |
| 639 | } |
| 640 | g_free(stream_class); |
| 641 | } |
| 642 | |
| 643 | static |
| 644 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
| 645 | { |
| 646 | int ret = 0; |
| 647 | struct bt_ctf_field_type *event_header_type = |
| 648 | bt_ctf_field_type_structure_create(); |
| 649 | struct bt_ctf_field_type *_uint32_t = |
| 650 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 651 | struct bt_ctf_field_type *_uint64_t = |
| 652 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 653 | |
| 654 | if (!event_header_type) { |
| 655 | ret = -1; |
| 656 | goto end; |
| 657 | } |
| 658 | |
| 659 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 660 | _uint32_t, "id"); |
| 661 | if (ret) { |
| 662 | goto end; |
| 663 | } |
| 664 | |
| 665 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 666 | _uint64_t, "timestamp"); |
| 667 | if (ret) { |
| 668 | goto end; |
| 669 | } |
| 670 | |
| 671 | if (stream_class->event_header_type) { |
| 672 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 673 | } |
| 674 | stream_class->event_header_type = event_header_type; |
| 675 | end: |
| 676 | if (ret) { |
| 677 | bt_ctf_field_type_put(event_header_type); |
| 678 | } |
| 679 | |
| 680 | bt_ctf_field_type_put(_uint32_t); |
| 681 | bt_ctf_field_type_put(_uint64_t); |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | static |
| 686 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
| 687 | { |
| 688 | int ret = 0; |
| 689 | struct bt_ctf_field_type *packet_context_type = |
| 690 | bt_ctf_field_type_structure_create(); |
| 691 | struct bt_ctf_field_type *_uint64_t = |
| 692 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 693 | |
| 694 | if (!packet_context_type) { |
| 695 | ret = -1; |
| 696 | goto end; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * We create a stream packet context as proposed in the CTF |
| 701 | * specification. |
| 702 | */ |
| 703 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 704 | _uint64_t, "timestamp_begin"); |
| 705 | if (ret) { |
| 706 | goto end; |
| 707 | } |
| 708 | |
| 709 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 710 | _uint64_t, "timestamp_end"); |
| 711 | if (ret) { |
| 712 | goto end; |
| 713 | } |
| 714 | |
| 715 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 716 | _uint64_t, "content_size"); |
| 717 | if (ret) { |
| 718 | goto end; |
| 719 | } |
| 720 | |
| 721 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 722 | _uint64_t, "packet_size"); |
| 723 | if (ret) { |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 728 | _uint64_t, "events_discarded"); |
| 729 | if (ret) { |
| 730 | goto end; |
| 731 | } |
| 732 | |
| 733 | if (stream_class->packet_context_type) { |
| 734 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 735 | } |
| 736 | stream_class->packet_context_type = packet_context_type; |
| 737 | end: |
| 738 | if (ret) { |
| 739 | bt_ctf_field_type_put(packet_context_type); |
| 740 | goto end; |
| 741 | } |
| 742 | |
| 743 | bt_ctf_field_type_put(_uint64_t); |
| 744 | return ret; |
| 745 | } |