| 1 | /* |
| 2 | * trace.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Trace |
| 5 | * |
| 6 | * Copyright 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-ir/trace-internal.h> |
| 30 | #include <babeltrace/ctf-ir/clock-internal.h> |
| 31 | #include <babeltrace/ctf-ir/stream-internal.h> |
| 32 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 33 | #include <babeltrace/ctf-ir/event-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-class.h> |
| 35 | #include <babeltrace/ctf-ir/event-class-internal.h> |
| 36 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 37 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 38 | #include <babeltrace/ctf-ir/attributes-internal.h> |
| 39 | #include <babeltrace/ctf-ir/validation-internal.h> |
| 40 | #include <babeltrace/ctf-ir/visitor-internal.h> |
| 41 | #include <babeltrace/ctf-ir/utils.h> |
| 42 | #include <babeltrace/plugin/notification/schema.h> |
| 43 | #include <babeltrace/compiler.h> |
| 44 | #include <babeltrace/values.h> |
| 45 | #include <babeltrace/ref.h> |
| 46 | #include <babeltrace/endian.h> |
| 47 | |
| 48 | #define DEFAULT_IDENTIFIER_SIZE 128 |
| 49 | #define DEFAULT_METADATA_STRING_SIZE 4096 |
| 50 | |
| 51 | struct listener_wrapper { |
| 52 | bt_ctf_listener_cb listener; |
| 53 | void *data; |
| 54 | }; |
| 55 | |
| 56 | static |
| 57 | void bt_ctf_trace_destroy(struct bt_object *obj); |
| 58 | static |
| 59 | int init_trace_packet_header(struct bt_ctf_trace *trace); |
| 60 | static |
| 61 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace); |
| 62 | |
| 63 | static |
| 64 | const unsigned int field_type_aliases_alignments[] = { |
| 65 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, |
| 66 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, |
| 67 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, |
| 68 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, |
| 69 | }; |
| 70 | |
| 71 | static |
| 72 | const unsigned int field_type_aliases_sizes[] = { |
| 73 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, |
| 74 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, |
| 75 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, |
| 76 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, |
| 77 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, |
| 78 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, |
| 79 | }; |
| 80 | |
| 81 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
| 82 | { |
| 83 | struct bt_ctf_trace *trace = NULL; |
| 84 | |
| 85 | trace = g_new0(struct bt_ctf_trace, 1); |
| 86 | if (!trace) { |
| 87 | goto error; |
| 88 | } |
| 89 | |
| 90 | bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); |
| 91 | bt_object_init(trace, bt_ctf_trace_destroy); |
| 92 | trace->clocks = g_ptr_array_new_with_free_func( |
| 93 | (GDestroyNotify) bt_put); |
| 94 | trace->streams = g_ptr_array_new_with_free_func( |
| 95 | (GDestroyNotify) bt_object_release); |
| 96 | trace->stream_classes = g_ptr_array_new_with_free_func( |
| 97 | (GDestroyNotify) bt_object_release); |
| 98 | if (!trace->clocks || !trace->stream_classes || !trace->streams) { |
| 99 | goto error; |
| 100 | } |
| 101 | |
| 102 | /* Generate a trace UUID */ |
| 103 | uuid_generate(trace->uuid); |
| 104 | if (init_trace_packet_header(trace)) { |
| 105 | goto error; |
| 106 | } |
| 107 | |
| 108 | /* Create the environment array object */ |
| 109 | trace->environment = bt_ctf_attributes_create(); |
| 110 | if (!trace->environment) { |
| 111 | goto error; |
| 112 | } |
| 113 | |
| 114 | trace->listeners = g_ptr_array_new_with_free_func( |
| 115 | (GDestroyNotify) g_free); |
| 116 | if (!trace->listeners) { |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | return trace; |
| 121 | |
| 122 | error: |
| 123 | BT_PUT(trace); |
| 124 | return trace; |
| 125 | } |
| 126 | |
| 127 | void bt_ctf_trace_destroy(struct bt_object *obj) |
| 128 | { |
| 129 | struct bt_ctf_trace *trace; |
| 130 | |
| 131 | trace = container_of(obj, struct bt_ctf_trace, base); |
| 132 | if (trace->environment) { |
| 133 | bt_ctf_attributes_destroy(trace->environment); |
| 134 | } |
| 135 | |
| 136 | if (trace->clocks) { |
| 137 | g_ptr_array_free(trace->clocks, TRUE); |
| 138 | } |
| 139 | |
| 140 | if (trace->streams) { |
| 141 | g_ptr_array_free(trace->streams, TRUE); |
| 142 | } |
| 143 | |
| 144 | if (trace->stream_classes) { |
| 145 | g_ptr_array_free(trace->stream_classes, TRUE); |
| 146 | } |
| 147 | |
| 148 | if (trace->listeners) { |
| 149 | g_ptr_array_free(trace->listeners, TRUE); |
| 150 | } |
| 151 | |
| 152 | bt_put(trace->packet_header_type); |
| 153 | g_free(trace); |
| 154 | } |
| 155 | |
| 156 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
| 157 | const char *name, struct bt_value *value) |
| 158 | { |
| 159 | int ret = 0; |
| 160 | |
| 161 | if (!trace || !name || !value || |
| 162 | bt_ctf_validate_identifier(name) || |
| 163 | !(bt_value_is_integer(value) || bt_value_is_string(value))) { |
| 164 | ret = -1; |
| 165 | goto end; |
| 166 | } |
| 167 | |
| 168 | if (strchr(name, ' ')) { |
| 169 | ret = -1; |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | if (trace->frozen) { |
| 174 | /* |
| 175 | * New environment fields may be added to a frozen trace, |
| 176 | * but existing fields may not be changed. |
| 177 | * |
| 178 | * The object passed is frozen like all other attributes. |
| 179 | */ |
| 180 | struct bt_value *attribute = |
| 181 | bt_ctf_attributes_get_field_value_by_name( |
| 182 | trace->environment, name); |
| 183 | |
| 184 | if (attribute) { |
| 185 | BT_PUT(attribute); |
| 186 | ret = -1; |
| 187 | goto end; |
| 188 | } |
| 189 | |
| 190 | bt_value_freeze(value); |
| 191 | } |
| 192 | |
| 193 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, |
| 194 | value); |
| 195 | |
| 196 | end: |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
| 201 | const char *name, const char *value) |
| 202 | { |
| 203 | int ret = 0; |
| 204 | struct bt_value *env_value_string_obj = NULL; |
| 205 | |
| 206 | if (!trace || !name || !value) { |
| 207 | ret = -1; |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | if (trace->frozen) { |
| 212 | /* |
| 213 | * New environment fields may be added to a frozen trace, |
| 214 | * but existing fields may not be changed. |
| 215 | */ |
| 216 | struct bt_value *attribute = |
| 217 | bt_ctf_attributes_get_field_value_by_name( |
| 218 | trace->environment, name); |
| 219 | |
| 220 | if (attribute) { |
| 221 | BT_PUT(attribute); |
| 222 | ret = -1; |
| 223 | goto end; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | env_value_string_obj = bt_value_string_create_init(value); |
| 228 | |
| 229 | if (!env_value_string_obj) { |
| 230 | ret = -1; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | if (trace->frozen) { |
| 235 | bt_value_freeze(env_value_string_obj); |
| 236 | } |
| 237 | ret = bt_ctf_trace_set_environment_field(trace, name, |
| 238 | env_value_string_obj); |
| 239 | |
| 240 | end: |
| 241 | BT_PUT(env_value_string_obj); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, |
| 246 | const char *name, int64_t value) |
| 247 | { |
| 248 | int ret = 0; |
| 249 | struct bt_value *env_value_integer_obj = NULL; |
| 250 | |
| 251 | if (!trace || !name) { |
| 252 | ret = -1; |
| 253 | goto end; |
| 254 | } |
| 255 | |
| 256 | if (trace->frozen) { |
| 257 | /* |
| 258 | * New environment fields may be added to a frozen trace, |
| 259 | * but existing fields may not be changed. |
| 260 | */ |
| 261 | struct bt_value *attribute = |
| 262 | bt_ctf_attributes_get_field_value_by_name( |
| 263 | trace->environment, name); |
| 264 | |
| 265 | if (attribute) { |
| 266 | BT_PUT(attribute); |
| 267 | ret = -1; |
| 268 | goto end; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | env_value_integer_obj = bt_value_integer_create_init(value); |
| 273 | if (!env_value_integer_obj) { |
| 274 | ret = -1; |
| 275 | goto end; |
| 276 | } |
| 277 | |
| 278 | ret = bt_ctf_trace_set_environment_field(trace, name, |
| 279 | env_value_integer_obj); |
| 280 | if (trace->frozen) { |
| 281 | bt_value_freeze(env_value_integer_obj); |
| 282 | } |
| 283 | end: |
| 284 | BT_PUT(env_value_integer_obj); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
| 289 | { |
| 290 | int ret = 0; |
| 291 | |
| 292 | if (!trace) { |
| 293 | ret = -1; |
| 294 | goto end; |
| 295 | } |
| 296 | |
| 297 | ret = bt_ctf_attributes_get_count(trace->environment); |
| 298 | |
| 299 | end: |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | const char * |
| 304 | bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, |
| 305 | int index) |
| 306 | { |
| 307 | const char *ret = NULL; |
| 308 | |
| 309 | if (!trace) { |
| 310 | goto end; |
| 311 | } |
| 312 | |
| 313 | ret = bt_ctf_attributes_get_field_name(trace->environment, index); |
| 314 | |
| 315 | end: |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | struct bt_value *bt_ctf_trace_get_environment_field_value( |
| 320 | struct bt_ctf_trace *trace, int index) |
| 321 | { |
| 322 | struct bt_value *ret = NULL; |
| 323 | |
| 324 | if (!trace) { |
| 325 | goto end; |
| 326 | } |
| 327 | |
| 328 | ret = bt_ctf_attributes_get_field_value(trace->environment, index); |
| 329 | |
| 330 | end: |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( |
| 335 | struct bt_ctf_trace *trace, const char *name) |
| 336 | { |
| 337 | struct bt_value *ret = NULL; |
| 338 | |
| 339 | if (!trace || !name) { |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, |
| 344 | name); |
| 345 | |
| 346 | end: |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace, |
| 351 | struct bt_ctf_clock *clock) |
| 352 | { |
| 353 | int ret = 0; |
| 354 | struct search_query query = { .value = clock, .found = 0 }; |
| 355 | |
| 356 | if (!trace || !bt_ctf_clock_is_valid(clock)) { |
| 357 | ret = -1; |
| 358 | goto end; |
| 359 | } |
| 360 | |
| 361 | /* Check for duplicate clocks */ |
| 362 | g_ptr_array_foreach(trace->clocks, value_exists, &query); |
| 363 | if (query.found) { |
| 364 | ret = -1; |
| 365 | goto end; |
| 366 | } |
| 367 | |
| 368 | bt_get(clock); |
| 369 | g_ptr_array_add(trace->clocks, clock); |
| 370 | |
| 371 | if (!trace->is_created_by_writer) { |
| 372 | /* |
| 373 | * Non-writer mode trace: disable clock value functions |
| 374 | * because clock values are per-stream in that |
| 375 | * situation. |
| 376 | */ |
| 377 | clock->has_value = 0; |
| 378 | } |
| 379 | |
| 380 | if (trace->frozen) { |
| 381 | bt_ctf_clock_freeze(clock); |
| 382 | } |
| 383 | end: |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace) |
| 388 | { |
| 389 | int ret = -1; |
| 390 | |
| 391 | if (!trace) { |
| 392 | goto end; |
| 393 | } |
| 394 | |
| 395 | ret = trace->clocks->len; |
| 396 | end: |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace, |
| 401 | int index) |
| 402 | { |
| 403 | struct bt_ctf_clock *clock = NULL; |
| 404 | |
| 405 | if (!trace || index < 0 || index >= trace->clocks->len) { |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | clock = g_ptr_array_index(trace->clocks, index); |
| 410 | bt_get(clock); |
| 411 | end: |
| 412 | return clock; |
| 413 | } |
| 414 | |
| 415 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
| 416 | struct bt_ctf_stream_class *stream_class) |
| 417 | { |
| 418 | int ret, i; |
| 419 | int64_t stream_id; |
| 420 | struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; |
| 421 | struct bt_ctf_validation_output *ec_validation_outputs = NULL; |
| 422 | const enum bt_ctf_validation_flag trace_sc_validation_flags = |
| 423 | BT_CTF_VALIDATION_FLAG_TRACE | |
| 424 | BT_CTF_VALIDATION_FLAG_STREAM; |
| 425 | const enum bt_ctf_validation_flag ec_validation_flags = |
| 426 | BT_CTF_VALIDATION_FLAG_EVENT; |
| 427 | struct bt_ctf_field_type *packet_header_type = NULL; |
| 428 | struct bt_ctf_field_type *packet_context_type = NULL; |
| 429 | struct bt_ctf_field_type *event_header_type = NULL; |
| 430 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; |
| 431 | int event_class_count; |
| 432 | struct bt_ctf_clock *clock_to_add_to_trace = NULL; |
| 433 | struct bt_ctf_trace *current_parent_trace = NULL; |
| 434 | |
| 435 | if (!trace || !stream_class) { |
| 436 | ret = -1; |
| 437 | goto end; |
| 438 | } |
| 439 | |
| 440 | current_parent_trace = bt_ctf_stream_class_get_trace(stream_class); |
| 441 | if (current_parent_trace) { |
| 442 | /* Stream class is already associated to a trace, abort. */ |
| 443 | ret = -1; |
| 444 | goto end; |
| 445 | } |
| 446 | |
| 447 | event_class_count = |
| 448 | bt_ctf_stream_class_get_event_class_count(stream_class); |
| 449 | assert(event_class_count >= 0); |
| 450 | |
| 451 | /* Check for duplicate stream classes */ |
| 452 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 453 | if (trace->stream_classes->pdata[i] == stream_class) { |
| 454 | /* Stream class already registered to the trace */ |
| 455 | ret = -1; |
| 456 | goto end; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * If the stream class has a clock, register this clock to this |
| 462 | * trace if not already done. |
| 463 | */ |
| 464 | if (stream_class->clock) { |
| 465 | const char *clock_name = |
| 466 | bt_ctf_clock_get_name(stream_class->clock); |
| 467 | struct bt_ctf_clock *trace_clock; |
| 468 | |
| 469 | assert(clock_name); |
| 470 | trace_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name); |
| 471 | bt_put(trace_clock); |
| 472 | if (trace_clock) { |
| 473 | if (trace_clock != stream_class->clock) { |
| 474 | /* |
| 475 | * Error: two different clocks in the |
| 476 | * trace would share the same name. |
| 477 | */ |
| 478 | ret = -1; |
| 479 | goto end; |
| 480 | } |
| 481 | } else { |
| 482 | clock_to_add_to_trace = bt_get(stream_class->clock); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * We're about to freeze both the trace and the stream class. |
| 488 | * Also, each event class contained in this stream class are |
| 489 | * already frozen. |
| 490 | * |
| 491 | * This trace, this stream class, and all its event classes |
| 492 | * should be valid at this point. |
| 493 | * |
| 494 | * Validate trace and stream class first, then each event |
| 495 | * class of this stream class can be validated individually. |
| 496 | */ |
| 497 | packet_header_type = |
| 498 | bt_ctf_trace_get_packet_header_type(trace); |
| 499 | packet_context_type = |
| 500 | bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 501 | event_header_type = |
| 502 | bt_ctf_stream_class_get_event_header_type(stream_class); |
| 503 | stream_event_ctx_type = |
| 504 | bt_ctf_stream_class_get_event_context_type(stream_class); |
| 505 | ret = bt_ctf_validate_class_types(trace->environment, |
| 506 | packet_header_type, packet_context_type, event_header_type, |
| 507 | stream_event_ctx_type, NULL, NULL, trace->valid, |
| 508 | stream_class->valid, 1, &trace_sc_validation_output, |
| 509 | trace_sc_validation_flags); |
| 510 | BT_PUT(packet_header_type); |
| 511 | BT_PUT(packet_context_type); |
| 512 | BT_PUT(event_header_type); |
| 513 | BT_PUT(stream_event_ctx_type); |
| 514 | |
| 515 | if (ret) { |
| 516 | /* |
| 517 | * This means something went wrong during the validation |
| 518 | * process, not that the objects are invalid. |
| 519 | */ |
| 520 | goto end; |
| 521 | } |
| 522 | |
| 523 | if ((trace_sc_validation_output.valid_flags & |
| 524 | trace_sc_validation_flags) != |
| 525 | trace_sc_validation_flags) { |
| 526 | /* Invalid trace/stream class */ |
| 527 | ret = -1; |
| 528 | goto end; |
| 529 | } |
| 530 | |
| 531 | if (event_class_count > 0) { |
| 532 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, |
| 533 | event_class_count); |
| 534 | if (!ec_validation_outputs) { |
| 535 | ret = -1; |
| 536 | goto end; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* Validate each event class individually */ |
| 541 | for (i = 0; i < event_class_count; i++) { |
| 542 | struct bt_ctf_event_class *event_class = |
| 543 | bt_ctf_stream_class_get_event_class(stream_class, i); |
| 544 | struct bt_ctf_field_type *event_context_type = NULL; |
| 545 | struct bt_ctf_field_type *event_payload_type = NULL; |
| 546 | |
| 547 | event_context_type = |
| 548 | bt_ctf_event_class_get_context_type(event_class); |
| 549 | event_payload_type = |
| 550 | bt_ctf_event_class_get_payload_type(event_class); |
| 551 | |
| 552 | /* |
| 553 | * It is important to use the field types returned by |
| 554 | * the previous trace and stream class validation here |
| 555 | * because copies could have been made. |
| 556 | */ |
| 557 | ret = bt_ctf_validate_class_types(trace->environment, |
| 558 | trace_sc_validation_output.packet_header_type, |
| 559 | trace_sc_validation_output.packet_context_type, |
| 560 | trace_sc_validation_output.event_header_type, |
| 561 | trace_sc_validation_output.stream_event_ctx_type, |
| 562 | event_context_type, event_payload_type, |
| 563 | 1, 1, event_class->valid, &ec_validation_outputs[i], |
| 564 | ec_validation_flags); |
| 565 | BT_PUT(event_context_type); |
| 566 | BT_PUT(event_payload_type); |
| 567 | BT_PUT(event_class); |
| 568 | |
| 569 | if (ret) { |
| 570 | goto end; |
| 571 | } |
| 572 | |
| 573 | if ((ec_validation_outputs[i].valid_flags & |
| 574 | ec_validation_flags) != ec_validation_flags) { |
| 575 | /* Invalid event class */ |
| 576 | ret = -1; |
| 577 | goto end; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | stream_id = bt_ctf_stream_class_get_id(stream_class); |
| 582 | if (stream_id < 0) { |
| 583 | stream_id = trace->next_stream_id++; |
| 584 | |
| 585 | /* Try to assign a new stream id */ |
| 586 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 587 | if (stream_id == bt_ctf_stream_class_get_id( |
| 588 | trace->stream_classes->pdata[i])) { |
| 589 | /* Duplicate stream id found */ |
| 590 | ret = -1; |
| 591 | goto end; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (bt_ctf_stream_class_set_id_no_check(stream_class, |
| 596 | stream_id)) { |
| 597 | /* TODO Should retry with a different stream id */ |
| 598 | ret = -1; |
| 599 | goto end; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | bt_object_set_parent(stream_class, trace); |
| 604 | g_ptr_array_add(trace->stream_classes, stream_class); |
| 605 | |
| 606 | /* |
| 607 | * At this point we know that the function will be successful. |
| 608 | * Therefore we can replace the trace and stream class field |
| 609 | * types with what's in their validation output structure and |
| 610 | * mark them as valid. We can also replace the field types of |
| 611 | * all the event classes of the stream class and mark them as |
| 612 | * valid. |
| 613 | */ |
| 614 | bt_ctf_validation_replace_types(trace, stream_class, NULL, |
| 615 | &trace_sc_validation_output, trace_sc_validation_flags); |
| 616 | trace->valid = 1; |
| 617 | stream_class->valid = 1; |
| 618 | |
| 619 | /* |
| 620 | * Put what was not moved in bt_ctf_validation_replace_types(). |
| 621 | */ |
| 622 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); |
| 623 | |
| 624 | for (i = 0; i < event_class_count; i++) { |
| 625 | struct bt_ctf_event_class *event_class = |
| 626 | bt_ctf_stream_class_get_event_class(stream_class, i); |
| 627 | |
| 628 | bt_ctf_validation_replace_types(NULL, NULL, event_class, |
| 629 | &ec_validation_outputs[i], ec_validation_flags); |
| 630 | event_class->valid = 1; |
| 631 | BT_PUT(event_class); |
| 632 | |
| 633 | /* |
| 634 | * Put what was not moved in |
| 635 | * bt_ctf_validation_replace_types(). |
| 636 | */ |
| 637 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * All field type byte orders set as "native" byte ordering can now be |
| 642 | * safely set to trace's own endianness, including the stream class'. |
| 643 | */ |
| 644 | bt_ctf_field_type_set_native_byte_order(trace->packet_header_type, |
| 645 | trace->byte_order); |
| 646 | bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order); |
| 647 | |
| 648 | /* Add stream class's clock if it exists */ |
| 649 | if (clock_to_add_to_trace) { |
| 650 | int add_clock_ret = |
| 651 | bt_ctf_trace_add_clock(trace, clock_to_add_to_trace); |
| 652 | assert(add_clock_ret == 0); |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Freeze the trace and the stream class. |
| 657 | */ |
| 658 | bt_ctf_stream_class_freeze(stream_class); |
| 659 | bt_ctf_trace_freeze(trace); |
| 660 | |
| 661 | /* Notifiy listeners of the trace's schema modification. */ |
| 662 | bt_ctf_stream_class_visit(stream_class, |
| 663 | bt_ctf_trace_element_modification, trace); |
| 664 | end: |
| 665 | if (ret) { |
| 666 | bt_object_set_parent(stream_class, NULL); |
| 667 | |
| 668 | if (ec_validation_outputs) { |
| 669 | for (i = 0; i < event_class_count; i++) { |
| 670 | bt_ctf_validation_output_put_types( |
| 671 | &ec_validation_outputs[i]); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | g_free(ec_validation_outputs); |
| 677 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); |
| 678 | BT_PUT(clock_to_add_to_trace); |
| 679 | bt_put(current_parent_trace); |
| 680 | assert(!packet_header_type); |
| 681 | assert(!packet_context_type); |
| 682 | assert(!event_header_type); |
| 683 | assert(!stream_event_ctx_type); |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
| 689 | { |
| 690 | int ret; |
| 691 | |
| 692 | if (!trace) { |
| 693 | ret = -1; |
| 694 | goto end; |
| 695 | } |
| 696 | |
| 697 | ret = trace->stream_classes->len; |
| 698 | end: |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class( |
| 703 | struct bt_ctf_trace *trace, int index) |
| 704 | { |
| 705 | struct bt_ctf_stream_class *stream_class = NULL; |
| 706 | |
| 707 | if (!trace || index < 0 || index >= trace->stream_classes->len) { |
| 708 | goto end; |
| 709 | } |
| 710 | |
| 711 | stream_class = g_ptr_array_index(trace->stream_classes, index); |
| 712 | bt_get(stream_class); |
| 713 | end: |
| 714 | return stream_class; |
| 715 | } |
| 716 | |
| 717 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
| 718 | struct bt_ctf_trace *trace, uint32_t id) |
| 719 | { |
| 720 | int i; |
| 721 | struct bt_ctf_stream_class *stream_class = NULL; |
| 722 | |
| 723 | if (!trace) { |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 728 | struct bt_ctf_stream_class *stream_class_candidate; |
| 729 | |
| 730 | stream_class_candidate = |
| 731 | g_ptr_array_index(trace->stream_classes, i); |
| 732 | |
| 733 | if (bt_ctf_stream_class_get_id(stream_class_candidate) == |
| 734 | (int64_t) id) { |
| 735 | stream_class = stream_class_candidate; |
| 736 | bt_get(stream_class); |
| 737 | goto end; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | end: |
| 742 | return stream_class; |
| 743 | } |
| 744 | |
| 745 | struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name( |
| 746 | struct bt_ctf_trace *trace, const char *name) |
| 747 | { |
| 748 | size_t i; |
| 749 | struct bt_ctf_clock *clock = NULL; |
| 750 | |
| 751 | if (!trace || !name) { |
| 752 | goto end; |
| 753 | } |
| 754 | |
| 755 | for (i = 0; i < trace->clocks->len; i++) { |
| 756 | struct bt_ctf_clock *cur_clk = |
| 757 | g_ptr_array_index(trace->clocks, i); |
| 758 | const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk); |
| 759 | |
| 760 | if (!cur_clk_name) { |
| 761 | goto end; |
| 762 | } |
| 763 | |
| 764 | if (!strcmp(cur_clk_name, name)) { |
| 765 | clock = cur_clk; |
| 766 | bt_get(clock); |
| 767 | goto end; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | end: |
| 772 | return clock; |
| 773 | } |
| 774 | |
| 775 | BT_HIDDEN |
| 776 | const char *get_byte_order_string(int byte_order) |
| 777 | { |
| 778 | const char *string; |
| 779 | |
| 780 | switch (byte_order) { |
| 781 | case LITTLE_ENDIAN: |
| 782 | string = "le"; |
| 783 | break; |
| 784 | case BIG_ENDIAN: |
| 785 | string = "be"; |
| 786 | break; |
| 787 | default: |
| 788 | string = "unknown"; |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | return string; |
| 793 | } |
| 794 | |
| 795 | static |
| 796 | int append_trace_metadata(struct bt_ctf_trace *trace, |
| 797 | struct metadata_context *context) |
| 798 | { |
| 799 | unsigned char *uuid = trace->uuid; |
| 800 | int ret; |
| 801 | |
| 802 | g_string_append(context->string, "trace {\n"); |
| 803 | |
| 804 | g_string_append(context->string, "\tmajor = 1;\n"); |
| 805 | g_string_append(context->string, "\tminor = 8;\n"); |
| 806 | |
| 807 | g_string_append_printf(context->string, |
| 808 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", |
| 809 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 810 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 811 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 812 | uuid[12], uuid[13], uuid[14], uuid[15]); |
| 813 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
| 814 | get_byte_order_string(trace->byte_order)); |
| 815 | |
| 816 | g_string_append(context->string, "\tpacket.header := "); |
| 817 | context->current_indentation_level++; |
| 818 | g_string_assign(context->field_name, ""); |
| 819 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, |
| 820 | context); |
| 821 | if (ret) { |
| 822 | goto end; |
| 823 | } |
| 824 | context->current_indentation_level--; |
| 825 | |
| 826 | g_string_append(context->string, ";\n};\n\n"); |
| 827 | end: |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | static |
| 832 | void append_env_metadata(struct bt_ctf_trace *trace, |
| 833 | struct metadata_context *context) |
| 834 | { |
| 835 | int i; |
| 836 | int env_size; |
| 837 | |
| 838 | env_size = bt_ctf_attributes_get_count(trace->environment); |
| 839 | |
| 840 | if (env_size <= 0) { |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | g_string_append(context->string, "env {\n"); |
| 845 | |
| 846 | for (i = 0; i < env_size; i++) { |
| 847 | struct bt_value *env_field_value_obj = NULL; |
| 848 | const char *entry_name; |
| 849 | |
| 850 | entry_name = bt_ctf_attributes_get_field_name( |
| 851 | trace->environment, i); |
| 852 | env_field_value_obj = bt_ctf_attributes_get_field_value( |
| 853 | trace->environment, i); |
| 854 | |
| 855 | if (!entry_name || !env_field_value_obj) { |
| 856 | goto loop_next; |
| 857 | } |
| 858 | |
| 859 | switch (bt_value_get_type(env_field_value_obj)) { |
| 860 | case BT_VALUE_TYPE_INTEGER: |
| 861 | { |
| 862 | int ret; |
| 863 | int64_t int_value; |
| 864 | |
| 865 | ret = bt_value_integer_get(env_field_value_obj, |
| 866 | &int_value); |
| 867 | |
| 868 | if (ret) { |
| 869 | goto loop_next; |
| 870 | } |
| 871 | |
| 872 | g_string_append_printf(context->string, |
| 873 | "\t%s = %" PRId64 ";\n", entry_name, |
| 874 | int_value); |
| 875 | break; |
| 876 | } |
| 877 | case BT_VALUE_TYPE_STRING: |
| 878 | { |
| 879 | int ret; |
| 880 | const char *str_value; |
| 881 | char *escaped_str = NULL; |
| 882 | |
| 883 | ret = bt_value_string_get(env_field_value_obj, |
| 884 | &str_value); |
| 885 | |
| 886 | if (ret) { |
| 887 | goto loop_next; |
| 888 | } |
| 889 | |
| 890 | escaped_str = g_strescape(str_value, NULL); |
| 891 | |
| 892 | if (!escaped_str) { |
| 893 | goto loop_next; |
| 894 | } |
| 895 | |
| 896 | g_string_append_printf(context->string, |
| 897 | "\t%s = \"%s\";\n", entry_name, escaped_str); |
| 898 | free(escaped_str); |
| 899 | break; |
| 900 | } |
| 901 | |
| 902 | default: |
| 903 | goto loop_next; |
| 904 | } |
| 905 | |
| 906 | loop_next: |
| 907 | BT_PUT(env_field_value_obj); |
| 908 | } |
| 909 | |
| 910 | g_string_append(context->string, "};\n\n"); |
| 911 | } |
| 912 | |
| 913 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) |
| 914 | { |
| 915 | char *metadata = NULL; |
| 916 | struct metadata_context *context = NULL; |
| 917 | int err = 0; |
| 918 | size_t i; |
| 919 | |
| 920 | if (!trace) { |
| 921 | goto end; |
| 922 | } |
| 923 | |
| 924 | context = g_new0(struct metadata_context, 1); |
| 925 | if (!context) { |
| 926 | goto end; |
| 927 | } |
| 928 | |
| 929 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); |
| 930 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); |
| 931 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); |
| 932 | if (append_trace_metadata(trace, context)) { |
| 933 | goto error; |
| 934 | } |
| 935 | append_env_metadata(trace, context); |
| 936 | g_ptr_array_foreach(trace->clocks, |
| 937 | (GFunc)bt_ctf_clock_serialize, context); |
| 938 | |
| 939 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 940 | err = bt_ctf_stream_class_serialize( |
| 941 | trace->stream_classes->pdata[i], context); |
| 942 | if (err) { |
| 943 | goto error; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | metadata = context->string->str; |
| 948 | error: |
| 949 | g_string_free(context->string, err ? TRUE : FALSE); |
| 950 | g_string_free(context->field_name, TRUE); |
| 951 | g_free(context); |
| 952 | end: |
| 953 | return metadata; |
| 954 | } |
| 955 | |
| 956 | enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) |
| 957 | { |
| 958 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; |
| 959 | |
| 960 | if (!trace) { |
| 961 | goto end; |
| 962 | } |
| 963 | |
| 964 | switch (trace->byte_order) { |
| 965 | case BIG_ENDIAN: |
| 966 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; |
| 967 | break; |
| 968 | case LITTLE_ENDIAN: |
| 969 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; |
| 970 | break; |
| 971 | default: |
| 972 | break; |
| 973 | } |
| 974 | end: |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, |
| 979 | enum bt_ctf_byte_order byte_order) |
| 980 | { |
| 981 | int ret = 0; |
| 982 | int internal_byte_order; |
| 983 | |
| 984 | if (!trace || trace->frozen) { |
| 985 | ret = -1; |
| 986 | goto end; |
| 987 | } |
| 988 | |
| 989 | switch (byte_order) { |
| 990 | case BT_CTF_BYTE_ORDER_NATIVE: |
| 991 | /* |
| 992 | * This doesn't make sense since the CTF specification defines |
| 993 | * the "native" byte order as "the byte order described in the |
| 994 | * trace description". However, this behavior had been |
| 995 | * implemented as part of v1.2 and is kept to maintain |
| 996 | * compatibility. |
| 997 | * |
| 998 | * This may be changed on a major version bump only. |
| 999 | */ |
| 1000 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? |
| 1001 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 1002 | break; |
| 1003 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 1004 | internal_byte_order = LITTLE_ENDIAN; |
| 1005 | break; |
| 1006 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 1007 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 1008 | internal_byte_order = BIG_ENDIAN; |
| 1009 | break; |
| 1010 | default: |
| 1011 | ret = -1; |
| 1012 | goto end; |
| 1013 | } |
| 1014 | |
| 1015 | trace->byte_order = internal_byte_order; |
| 1016 | end: |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
| 1021 | struct bt_ctf_trace *trace) |
| 1022 | { |
| 1023 | struct bt_ctf_field_type *field_type = NULL; |
| 1024 | |
| 1025 | if (!trace) { |
| 1026 | goto end; |
| 1027 | } |
| 1028 | |
| 1029 | bt_get(trace->packet_header_type); |
| 1030 | field_type = trace->packet_header_type; |
| 1031 | end: |
| 1032 | return field_type; |
| 1033 | } |
| 1034 | |
| 1035 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, |
| 1036 | struct bt_ctf_field_type *packet_header_type) |
| 1037 | { |
| 1038 | int ret = 0; |
| 1039 | |
| 1040 | if (!trace || !packet_header_type || trace->frozen) { |
| 1041 | ret = -1; |
| 1042 | goto end; |
| 1043 | } |
| 1044 | |
| 1045 | /* packet_header_type must be a structure */ |
| 1046 | if (bt_ctf_field_type_get_type_id(packet_header_type) != |
| 1047 | BT_CTF_TYPE_ID_STRUCT) { |
| 1048 | ret = -1; |
| 1049 | goto end; |
| 1050 | } |
| 1051 | |
| 1052 | bt_get(packet_header_type); |
| 1053 | bt_put(trace->packet_header_type); |
| 1054 | trace->packet_header_type = packet_header_type; |
| 1055 | end: |
| 1056 | return ret; |
| 1057 | } |
| 1058 | |
| 1059 | static |
| 1060 | int get_stream_class_count(void *element) |
| 1061 | { |
| 1062 | return bt_ctf_trace_get_stream_class_count( |
| 1063 | (struct bt_ctf_trace *) element); |
| 1064 | } |
| 1065 | |
| 1066 | static |
| 1067 | void *get_stream_class(void *element, int i) |
| 1068 | { |
| 1069 | return bt_ctf_trace_get_stream_class( |
| 1070 | (struct bt_ctf_trace *) element, i); |
| 1071 | } |
| 1072 | |
| 1073 | static |
| 1074 | int visit_stream_class(void *element, bt_ctf_ir_visitor visitor,void *data) |
| 1075 | { |
| 1076 | return bt_ctf_stream_class_visit(element, visitor, data); |
| 1077 | } |
| 1078 | |
| 1079 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, |
| 1080 | bt_ctf_ir_visitor visitor, void *data) |
| 1081 | { |
| 1082 | int ret; |
| 1083 | struct bt_ctf_ir_element element = |
| 1084 | { .element = trace, .type = BT_CTF_IR_TYPE_TRACE }; |
| 1085 | |
| 1086 | if (!trace || !visitor) { |
| 1087 | ret = -1; |
| 1088 | goto end; |
| 1089 | } |
| 1090 | |
| 1091 | ret = visitor_helper(&element, get_stream_class_count, |
| 1092 | get_stream_class, visit_stream_class, visitor, data); |
| 1093 | end: |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | static |
| 1098 | int invoke_listener(struct bt_ctf_ir_element *element, void *data) |
| 1099 | { |
| 1100 | struct listener_wrapper *listener_wrapper = data; |
| 1101 | |
| 1102 | listener_wrapper->listener(element, listener_wrapper->data); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace, |
| 1107 | bt_ctf_listener_cb listener, void *listener_data) |
| 1108 | { |
| 1109 | int ret = 0; |
| 1110 | struct listener_wrapper *listener_wrapper = |
| 1111 | g_new0(struct listener_wrapper, 1); |
| 1112 | |
| 1113 | if (!trace || !listener || !listener_wrapper) { |
| 1114 | ret = -1; |
| 1115 | goto error; |
| 1116 | } |
| 1117 | |
| 1118 | listener_wrapper->listener = listener; |
| 1119 | listener_wrapper->data = listener_data; |
| 1120 | |
| 1121 | /* Visit the current schema. */ |
| 1122 | ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper); |
| 1123 | if (ret) { |
| 1124 | goto error; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Add listener to the array of callbacks which will be invoked on |
| 1129 | * schema changes. |
| 1130 | */ |
| 1131 | g_ptr_array_add(trace->listeners, listener_wrapper); |
| 1132 | return ret; |
| 1133 | error: |
| 1134 | g_free(listener_wrapper); |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
| 1138 | BT_HIDDEN |
| 1139 | int bt_ctf_trace_element_modification(struct bt_ctf_ir_element *element, |
| 1140 | void *trace_ptr) |
| 1141 | { |
| 1142 | size_t i; |
| 1143 | struct bt_ctf_trace *trace = trace_ptr; |
| 1144 | |
| 1145 | assert(trace); |
| 1146 | assert(element); |
| 1147 | |
| 1148 | if (trace->listeners->len == 0) { |
| 1149 | goto end; |
| 1150 | } |
| 1151 | |
| 1152 | for (i = 0; i < trace->listeners->len; i++) { |
| 1153 | struct listener_wrapper *listener = |
| 1154 | g_ptr_array_index(trace->listeners, i); |
| 1155 | |
| 1156 | listener->listener(element, listener->data); |
| 1157 | } |
| 1158 | end: |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | BT_HIDDEN |
| 1163 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) |
| 1164 | { |
| 1165 | int ret; |
| 1166 | unsigned int alignment, size; |
| 1167 | struct bt_ctf_field_type *field_type = NULL; |
| 1168 | |
| 1169 | if (alias >= NR_FIELD_TYPE_ALIAS) { |
| 1170 | goto end; |
| 1171 | } |
| 1172 | |
| 1173 | alignment = field_type_aliases_alignments[alias]; |
| 1174 | size = field_type_aliases_sizes[alias]; |
| 1175 | field_type = bt_ctf_field_type_integer_create(size); |
| 1176 | ret = bt_ctf_field_type_set_alignment(field_type, alignment); |
| 1177 | if (ret) { |
| 1178 | BT_PUT(field_type); |
| 1179 | } |
| 1180 | end: |
| 1181 | return field_type; |
| 1182 | } |
| 1183 | |
| 1184 | static |
| 1185 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) |
| 1186 | { |
| 1187 | int i; |
| 1188 | |
| 1189 | bt_ctf_field_type_freeze(trace->packet_header_type); |
| 1190 | bt_ctf_attributes_freeze(trace->environment); |
| 1191 | |
| 1192 | for (i = 0; i < trace->clocks->len; i++) { |
| 1193 | struct bt_ctf_clock *clock = |
| 1194 | g_ptr_array_index(trace->clocks, i); |
| 1195 | |
| 1196 | bt_ctf_clock_freeze(clock); |
| 1197 | } |
| 1198 | |
| 1199 | trace->frozen = 1; |
| 1200 | } |
| 1201 | |
| 1202 | static |
| 1203 | int init_trace_packet_header(struct bt_ctf_trace *trace) |
| 1204 | { |
| 1205 | int ret = 0; |
| 1206 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; |
| 1207 | struct bt_ctf_field_type *_uint32_t = |
| 1208 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 1209 | struct bt_ctf_field_type *_uint8_t = |
| 1210 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 1211 | struct bt_ctf_field_type *trace_packet_header_type = |
| 1212 | bt_ctf_field_type_structure_create(); |
| 1213 | struct bt_ctf_field_type *uuid_array_type = |
| 1214 | bt_ctf_field_type_array_create(_uint8_t, 16); |
| 1215 | |
| 1216 | if (!trace_packet_header_type || !uuid_array_type) { |
| 1217 | ret = -1; |
| 1218 | goto end; |
| 1219 | } |
| 1220 | |
| 1221 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 1222 | _uint32_t, "magic"); |
| 1223 | if (ret) { |
| 1224 | goto end; |
| 1225 | } |
| 1226 | |
| 1227 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 1228 | uuid_array_type, "uuid"); |
| 1229 | if (ret) { |
| 1230 | goto end; |
| 1231 | } |
| 1232 | |
| 1233 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 1234 | _uint32_t, "stream_id"); |
| 1235 | if (ret) { |
| 1236 | goto end; |
| 1237 | } |
| 1238 | |
| 1239 | ret = bt_ctf_trace_set_packet_header_type(trace, |
| 1240 | trace_packet_header_type); |
| 1241 | if (ret) { |
| 1242 | goto end; |
| 1243 | } |
| 1244 | end: |
| 1245 | bt_put(uuid_array_type); |
| 1246 | bt_put(_uint32_t); |
| 1247 | bt_put(_uint8_t); |
| 1248 | bt_put(magic); |
| 1249 | bt_put(uuid_array); |
| 1250 | bt_put(trace_packet_header_type); |
| 1251 | return ret; |
| 1252 | } |