| 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 | struct bt_ctf_event_class *event_class = NULL; |
| 313 | |
| 314 | if (!stream_class || !name) { |
| 315 | goto end; |
| 316 | } |
| 317 | |
| 318 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 319 | struct bt_ctf_event_class *cur_event_class = |
| 320 | g_ptr_array_index(stream_class->event_classes, i); |
| 321 | const char *cur_event_class_name = |
| 322 | bt_ctf_event_class_get_name(cur_event_class); |
| 323 | |
| 324 | if (!strcmp(name, cur_event_class_name)) { |
| 325 | event_class = cur_event_class; |
| 326 | bt_ctf_event_class_get(event_class); |
| 327 | goto end; |
| 328 | } |
| 329 | } |
| 330 | end: |
| 331 | return event_class; |
| 332 | } |
| 333 | |
| 334 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
| 335 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 336 | { |
| 337 | size_t i; |
| 338 | struct bt_ctf_event_class *event_class = NULL; |
| 339 | |
| 340 | if (!stream_class) { |
| 341 | goto end; |
| 342 | } |
| 343 | |
| 344 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 345 | struct bt_ctf_event_class *current_event_class = |
| 346 | g_ptr_array_index(stream_class->event_classes, i); |
| 347 | |
| 348 | if (bt_ctf_event_class_get_id(current_event_class) == id) { |
| 349 | event_class = current_event_class; |
| 350 | bt_ctf_event_class_get(event_class); |
| 351 | goto end; |
| 352 | } |
| 353 | } |
| 354 | end: |
| 355 | return event_class; |
| 356 | } |
| 357 | |
| 358 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
| 359 | struct bt_ctf_stream_class *stream_class) |
| 360 | { |
| 361 | struct bt_ctf_field_type *ret = NULL; |
| 362 | |
| 363 | if (!stream_class) { |
| 364 | goto end; |
| 365 | } |
| 366 | |
| 367 | assert(stream_class->packet_context_type); |
| 368 | bt_ctf_field_type_get(stream_class->packet_context_type); |
| 369 | ret = stream_class->packet_context_type; |
| 370 | end: |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | int bt_ctf_stream_class_set_packet_context_type( |
| 375 | struct bt_ctf_stream_class *stream_class, |
| 376 | struct bt_ctf_field_type *packet_context_type) |
| 377 | { |
| 378 | int ret = 0; |
| 379 | |
| 380 | if (!stream_class || !packet_context_type || stream_class->frozen) { |
| 381 | ret = -1; |
| 382 | goto end; |
| 383 | } |
| 384 | |
| 385 | assert(stream_class->packet_context_type); |
| 386 | if (stream_class->packet_context_type == packet_context_type) { |
| 387 | goto end; |
| 388 | } |
| 389 | if (bt_ctf_field_type_get_type_id(packet_context_type) != |
| 390 | CTF_TYPE_STRUCT) { |
| 391 | /* A packet context must be a structure */ |
| 392 | ret = -1; |
| 393 | goto end; |
| 394 | } |
| 395 | |
| 396 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 397 | bt_ctf_field_type_get(packet_context_type); |
| 398 | stream_class->packet_context_type = packet_context_type; |
| 399 | end: |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
| 404 | struct bt_ctf_stream_class *stream_class) |
| 405 | { |
| 406 | struct bt_ctf_field_type *ret = NULL; |
| 407 | |
| 408 | if (!stream_class || !stream_class->event_header_type) { |
| 409 | goto end; |
| 410 | } |
| 411 | |
| 412 | assert(stream_class->event_header_type); |
| 413 | bt_ctf_field_type_get(stream_class->event_header_type); |
| 414 | ret = stream_class->event_header_type; |
| 415 | end: |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | int bt_ctf_stream_class_set_event_header_type( |
| 420 | struct bt_ctf_stream_class *stream_class, |
| 421 | struct bt_ctf_field_type *event_header_type) |
| 422 | { |
| 423 | int ret = 0; |
| 424 | |
| 425 | if (!stream_class || !event_header_type || stream_class->frozen) { |
| 426 | ret = -1; |
| 427 | goto end; |
| 428 | } |
| 429 | |
| 430 | assert(stream_class->event_header_type); |
| 431 | if (stream_class->event_header_type == event_header_type) { |
| 432 | goto end; |
| 433 | } |
| 434 | if (bt_ctf_field_type_get_type_id(event_header_type) != |
| 435 | CTF_TYPE_STRUCT) { |
| 436 | /* An event header must be a structure */ |
| 437 | ret = -1; |
| 438 | goto end; |
| 439 | } |
| 440 | |
| 441 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 442 | bt_ctf_field_type_get(event_header_type); |
| 443 | stream_class->event_header_type = event_header_type; |
| 444 | end: |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
| 449 | struct bt_ctf_stream_class *stream_class) |
| 450 | { |
| 451 | struct bt_ctf_field_type *ret = NULL; |
| 452 | |
| 453 | if (!stream_class || !stream_class->event_context_type) { |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | assert(stream_class->event_context_type); |
| 458 | bt_ctf_field_type_get(stream_class->event_context_type); |
| 459 | ret = stream_class->event_context_type; |
| 460 | end: |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | int bt_ctf_stream_class_set_event_context_type( |
| 465 | struct bt_ctf_stream_class *stream_class, |
| 466 | struct bt_ctf_field_type *event_context_type) |
| 467 | { |
| 468 | int ret = 0; |
| 469 | |
| 470 | if (!stream_class || !event_context_type || stream_class->frozen) { |
| 471 | ret = -1; |
| 472 | goto end; |
| 473 | } |
| 474 | |
| 475 | if (bt_ctf_field_type_get_type_id(event_context_type) != |
| 476 | CTF_TYPE_STRUCT) { |
| 477 | /* A packet context must be a structure */ |
| 478 | ret = -1; |
| 479 | goto end; |
| 480 | } |
| 481 | |
| 482 | bt_ctf_field_type_put(stream_class->event_context_type); |
| 483 | bt_ctf_field_type_get(event_context_type); |
| 484 | stream_class->event_context_type = event_context_type; |
| 485 | end: |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
| 490 | { |
| 491 | if (!stream_class) { |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | bt_ctf_ref_get(&stream_class->ref_count); |
| 496 | } |
| 497 | |
| 498 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) |
| 499 | { |
| 500 | if (!stream_class) { |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy); |
| 505 | } |
| 506 | |
| 507 | BT_HIDDEN |
| 508 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) |
| 509 | { |
| 510 | if (!stream_class) { |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | stream_class->frozen = 1; |
| 515 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
| 516 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
| 517 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
| 518 | bt_ctf_clock_freeze(stream_class->clock); |
| 519 | } |
| 520 | |
| 521 | BT_HIDDEN |
| 522 | int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class, |
| 523 | enum bt_ctf_byte_order byte_order) |
| 524 | { |
| 525 | int i, ret = 0; |
| 526 | int internal_byte_order; |
| 527 | |
| 528 | /* Note that "NATIVE" means the trace's endianness, not the host's. */ |
| 529 | if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN || |
| 530 | byte_order > BT_CTF_BYTE_ORDER_NETWORK) { |
| 531 | ret = -1; |
| 532 | goto end; |
| 533 | } |
| 534 | |
| 535 | switch (byte_order) { |
| 536 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 537 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 538 | internal_byte_order = BIG_ENDIAN; |
| 539 | break; |
| 540 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 541 | internal_byte_order = LITTLE_ENDIAN; |
| 542 | break; |
| 543 | default: |
| 544 | ret = -1; |
| 545 | goto end; |
| 546 | } |
| 547 | |
| 548 | stream_class->byte_order = internal_byte_order; |
| 549 | |
| 550 | /* Set native byte order to little or big endian */ |
| 551 | bt_ctf_field_type_set_native_byte_order( |
| 552 | stream_class->event_header_type, stream_class->byte_order); |
| 553 | bt_ctf_field_type_set_native_byte_order( |
| 554 | stream_class->packet_context_type, stream_class->byte_order); |
| 555 | bt_ctf_field_type_set_native_byte_order( |
| 556 | stream_class->event_context_type, stream_class->byte_order); |
| 557 | |
| 558 | /* Set all events' native byte order */ |
| 559 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 560 | bt_ctf_event_class_set_native_byte_order( |
| 561 | g_ptr_array_index(stream_class->event_classes, i), |
| 562 | stream_class->byte_order); |
| 563 | bt_ctf_event_class_freeze( |
| 564 | g_ptr_array_index(stream_class->event_classes, i)); |
| 565 | } |
| 566 | end: |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | BT_HIDDEN |
| 571 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, |
| 572 | struct metadata_context *context) |
| 573 | { |
| 574 | int64_t ret = 0; |
| 575 | size_t i; |
| 576 | |
| 577 | g_string_assign(context->field_name, ""); |
| 578 | context->current_indentation_level = 1; |
| 579 | if (!stream_class->id_set) { |
| 580 | ret = -1; |
| 581 | goto end; |
| 582 | } |
| 583 | |
| 584 | g_string_append_printf(context->string, |
| 585 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", |
| 586 | stream_class->id); |
| 587 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, |
| 588 | context); |
| 589 | if (ret) { |
| 590 | goto end; |
| 591 | } |
| 592 | |
| 593 | g_string_append(context->string, ";\n\n\tpacket.context := "); |
| 594 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, |
| 595 | context); |
| 596 | if (ret) { |
| 597 | goto end; |
| 598 | } |
| 599 | |
| 600 | if (stream_class->event_context_type) { |
| 601 | g_string_append(context->string, ";\n\n\tevent.context := "); |
| 602 | ret = bt_ctf_field_type_serialize( |
| 603 | stream_class->event_context_type, context); |
| 604 | if (ret) { |
| 605 | goto end; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | g_string_append(context->string, ";\n};\n\n"); |
| 610 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 611 | struct bt_ctf_event_class *event_class = |
| 612 | stream_class->event_classes->pdata[i]; |
| 613 | |
| 614 | ret = bt_ctf_event_class_serialize(event_class, context); |
| 615 | if (ret) { |
| 616 | goto end; |
| 617 | } |
| 618 | } |
| 619 | end: |
| 620 | context->current_indentation_level = 0; |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | static |
| 625 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref) |
| 626 | { |
| 627 | struct bt_ctf_stream_class *stream_class; |
| 628 | |
| 629 | if (!ref) { |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count); |
| 634 | bt_ctf_clock_put(stream_class->clock); |
| 635 | |
| 636 | if (stream_class->event_classes) { |
| 637 | size_t i; |
| 638 | |
| 639 | /* Unregister this stream class from the event classes */ |
| 640 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 641 | struct bt_ctf_event_class *event_class = |
| 642 | g_ptr_array_index(stream_class->event_classes, |
| 643 | i); |
| 644 | |
| 645 | bt_ctf_event_class_set_stream_class(event_class, NULL); |
| 646 | } |
| 647 | |
| 648 | g_ptr_array_free(stream_class->event_classes, TRUE); |
| 649 | } |
| 650 | |
| 651 | if (stream_class->name) { |
| 652 | g_string_free(stream_class->name, TRUE); |
| 653 | } |
| 654 | |
| 655 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 656 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 657 | if (stream_class->event_context_type) { |
| 658 | bt_ctf_field_type_put(stream_class->event_context_type); |
| 659 | } |
| 660 | g_free(stream_class); |
| 661 | } |
| 662 | |
| 663 | static |
| 664 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
| 665 | { |
| 666 | int ret = 0; |
| 667 | struct bt_ctf_field_type *event_header_type = |
| 668 | bt_ctf_field_type_structure_create(); |
| 669 | struct bt_ctf_field_type *_uint32_t = |
| 670 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 671 | struct bt_ctf_field_type *_uint64_t = |
| 672 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 673 | |
| 674 | if (!event_header_type) { |
| 675 | ret = -1; |
| 676 | goto end; |
| 677 | } |
| 678 | |
| 679 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 680 | _uint32_t, "id"); |
| 681 | if (ret) { |
| 682 | goto end; |
| 683 | } |
| 684 | |
| 685 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 686 | _uint64_t, "timestamp"); |
| 687 | if (ret) { |
| 688 | goto end; |
| 689 | } |
| 690 | |
| 691 | if (stream_class->event_header_type) { |
| 692 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 693 | } |
| 694 | stream_class->event_header_type = event_header_type; |
| 695 | end: |
| 696 | if (ret) { |
| 697 | bt_ctf_field_type_put(event_header_type); |
| 698 | } |
| 699 | |
| 700 | bt_ctf_field_type_put(_uint32_t); |
| 701 | bt_ctf_field_type_put(_uint64_t); |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | static |
| 706 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
| 707 | { |
| 708 | int ret = 0; |
| 709 | struct bt_ctf_field_type *packet_context_type = |
| 710 | bt_ctf_field_type_structure_create(); |
| 711 | struct bt_ctf_field_type *_uint64_t = |
| 712 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 713 | |
| 714 | if (!packet_context_type) { |
| 715 | ret = -1; |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * We create a stream packet context as proposed in the CTF |
| 721 | * specification. |
| 722 | */ |
| 723 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 724 | _uint64_t, "timestamp_begin"); |
| 725 | if (ret) { |
| 726 | goto end; |
| 727 | } |
| 728 | |
| 729 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 730 | _uint64_t, "timestamp_end"); |
| 731 | if (ret) { |
| 732 | goto end; |
| 733 | } |
| 734 | |
| 735 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 736 | _uint64_t, "content_size"); |
| 737 | if (ret) { |
| 738 | goto end; |
| 739 | } |
| 740 | |
| 741 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 742 | _uint64_t, "packet_size"); |
| 743 | if (ret) { |
| 744 | goto end; |
| 745 | } |
| 746 | |
| 747 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 748 | _uint64_t, "events_discarded"); |
| 749 | if (ret) { |
| 750 | goto end; |
| 751 | } |
| 752 | |
| 753 | if (stream_class->packet_context_type) { |
| 754 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 755 | } |
| 756 | stream_class->packet_context_type = packet_context_type; |
| 757 | end: |
| 758 | if (ret) { |
| 759 | bt_ctf_field_type_put(packet_context_type); |
| 760 | goto end; |
| 761 | } |
| 762 | |
| 763 | bt_ctf_field_type_put(_uint64_t); |
| 764 | return ret; |
| 765 | } |