| 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-writer/functor-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 35 | #include <babeltrace/ctf-ir/attributes-internal.h> |
| 36 | #include <babeltrace/ctf-ir/visitor-internal.h> |
| 37 | #include <babeltrace/ctf-ir/utils.h> |
| 38 | #include <babeltrace/compiler.h> |
| 39 | #include <babeltrace/objects.h> |
| 40 | |
| 41 | #define DEFAULT_IDENTIFIER_SIZE 128 |
| 42 | #define DEFAULT_METADATA_STRING_SIZE 4096 |
| 43 | |
| 44 | static |
| 45 | void bt_ctf_trace_destroy(struct bt_ctf_ref *ref); |
| 46 | static |
| 47 | int init_trace_packet_header(struct bt_ctf_trace *trace); |
| 48 | static |
| 49 | int bt_ctf_trace_freeze(struct bt_ctf_trace *trace); |
| 50 | |
| 51 | static |
| 52 | const unsigned int field_type_aliases_alignments[] = { |
| 53 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, |
| 54 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, |
| 55 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, |
| 56 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, |
| 57 | }; |
| 58 | |
| 59 | static |
| 60 | const unsigned int field_type_aliases_sizes[] = { |
| 61 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, |
| 62 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, |
| 63 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, |
| 64 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, |
| 65 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, |
| 66 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, |
| 67 | }; |
| 68 | |
| 69 | static |
| 70 | void put_stream_class(struct bt_ctf_stream_class *stream_class) |
| 71 | { |
| 72 | (void) bt_ctf_stream_class_set_trace(stream_class, NULL); |
| 73 | bt_ctf_stream_class_put(stream_class); |
| 74 | } |
| 75 | |
| 76 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
| 77 | { |
| 78 | struct bt_ctf_trace *trace = NULL; |
| 79 | |
| 80 | trace = g_new0(struct bt_ctf_trace, 1); |
| 81 | if (!trace) { |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); |
| 86 | bt_ctf_ref_init(&trace->ref_count); |
| 87 | trace->clocks = g_ptr_array_new_with_free_func( |
| 88 | (GDestroyNotify) bt_ctf_clock_put); |
| 89 | trace->streams = g_ptr_array_new_with_free_func( |
| 90 | (GDestroyNotify) bt_ctf_stream_put); |
| 91 | trace->stream_classes = g_ptr_array_new_with_free_func( |
| 92 | (GDestroyNotify) put_stream_class); |
| 93 | if (!trace->clocks || !trace->stream_classes || !trace->streams) { |
| 94 | goto error_destroy; |
| 95 | } |
| 96 | |
| 97 | /* Generate a trace UUID */ |
| 98 | uuid_generate(trace->uuid); |
| 99 | if (init_trace_packet_header(trace)) { |
| 100 | goto error_destroy; |
| 101 | } |
| 102 | |
| 103 | /* Create the environment array object */ |
| 104 | trace->environment = bt_ctf_attributes_create(); |
| 105 | if (!trace->environment) { |
| 106 | goto error_destroy; |
| 107 | } |
| 108 | |
| 109 | return trace; |
| 110 | |
| 111 | error_destroy: |
| 112 | bt_ctf_trace_destroy(&trace->ref_count); |
| 113 | trace = NULL; |
| 114 | error: |
| 115 | return trace; |
| 116 | } |
| 117 | |
| 118 | void bt_ctf_trace_destroy(struct bt_ctf_ref *ref) |
| 119 | { |
| 120 | struct bt_ctf_trace *trace; |
| 121 | |
| 122 | if (!ref) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | trace = container_of(ref, struct bt_ctf_trace, ref_count); |
| 127 | if (trace->environment) { |
| 128 | bt_ctf_attributes_destroy(trace->environment); |
| 129 | } |
| 130 | |
| 131 | if (trace->clocks) { |
| 132 | g_ptr_array_free(trace->clocks, TRUE); |
| 133 | } |
| 134 | |
| 135 | if (trace->streams) { |
| 136 | g_ptr_array_free(trace->streams, TRUE); |
| 137 | } |
| 138 | |
| 139 | if (trace->stream_classes) { |
| 140 | g_ptr_array_free(trace->stream_classes, TRUE); |
| 141 | } |
| 142 | |
| 143 | bt_ctf_field_type_put(trace->packet_header_type); |
| 144 | g_free(trace); |
| 145 | } |
| 146 | |
| 147 | struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace, |
| 148 | struct bt_ctf_stream_class *stream_class) |
| 149 | { |
| 150 | int ret; |
| 151 | int stream_class_found = 0; |
| 152 | size_t i; |
| 153 | struct bt_ctf_stream *stream = NULL; |
| 154 | |
| 155 | if (!trace || !stream_class) { |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | stream = bt_ctf_stream_create(stream_class, trace); |
| 160 | if (!stream) { |
| 161 | goto error; |
| 162 | } |
| 163 | |
| 164 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 165 | if (trace->stream_classes->pdata[i] == stream_class) { |
| 166 | stream_class_found = 1; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (!stream_class_found) { |
| 171 | ret = bt_ctf_trace_add_stream_class(trace, stream_class); |
| 172 | if (ret) { |
| 173 | goto error; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bt_ctf_stream_get(stream); |
| 178 | g_ptr_array_add(trace->streams, stream); |
| 179 | |
| 180 | return stream; |
| 181 | error: |
| 182 | bt_ctf_stream_put(stream); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
| 187 | const char *name, struct bt_object *value) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | |
| 191 | if (!trace || !name || !value || |
| 192 | bt_ctf_validate_identifier(name) || |
| 193 | !(bt_object_is_integer(value) || bt_object_is_string(value))) { |
| 194 | ret = -1; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | if (strchr(name, ' ')) { |
| 199 | ret = -1; |
| 200 | goto end; |
| 201 | } |
| 202 | |
| 203 | if (trace->frozen) { |
| 204 | /* |
| 205 | * New environment fields may be added to a frozen trace, |
| 206 | * but existing fields may not be changed. |
| 207 | * |
| 208 | * The object passed is frozen like all other attributes. |
| 209 | */ |
| 210 | struct bt_object *attribute = |
| 211 | bt_ctf_attributes_get_field_value_by_name( |
| 212 | trace->environment, name); |
| 213 | |
| 214 | if (attribute) { |
| 215 | BT_OBJECT_PUT(attribute); |
| 216 | ret = -1; |
| 217 | goto end; |
| 218 | } |
| 219 | |
| 220 | bt_object_freeze(value); |
| 221 | } |
| 222 | |
| 223 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, |
| 224 | value); |
| 225 | |
| 226 | end: |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
| 231 | const char *name, const char *value) |
| 232 | { |
| 233 | int ret = 0; |
| 234 | struct bt_object *env_value_string_obj = NULL; |
| 235 | |
| 236 | if (!trace || !name || !value) { |
| 237 | ret = -1; |
| 238 | goto end; |
| 239 | } |
| 240 | |
| 241 | if (trace->frozen) { |
| 242 | /* |
| 243 | * New environment fields may be added to a frozen trace, |
| 244 | * but existing fields may not be changed. |
| 245 | */ |
| 246 | struct bt_object *attribute = |
| 247 | bt_ctf_attributes_get_field_value_by_name( |
| 248 | trace->environment, name); |
| 249 | |
| 250 | if (attribute) { |
| 251 | BT_OBJECT_PUT(attribute); |
| 252 | ret = -1; |
| 253 | goto end; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | env_value_string_obj = bt_object_string_create_init(value); |
| 258 | |
| 259 | if (!env_value_string_obj) { |
| 260 | ret = -1; |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | if (trace->frozen) { |
| 265 | bt_object_freeze(env_value_string_obj); |
| 266 | } |
| 267 | ret = bt_ctf_trace_set_environment_field(trace, name, |
| 268 | env_value_string_obj); |
| 269 | |
| 270 | end: |
| 271 | BT_OBJECT_PUT(env_value_string_obj); |
| 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, |
| 277 | const char *name, int64_t value) |
| 278 | { |
| 279 | int ret = 0; |
| 280 | struct bt_object *env_value_integer_obj = NULL; |
| 281 | |
| 282 | if (!trace || !name) { |
| 283 | ret = -1; |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | if (trace->frozen) { |
| 288 | /* |
| 289 | * New environment fields may be added to a frozen trace, |
| 290 | * but existing fields may not be changed. |
| 291 | */ |
| 292 | struct bt_object *attribute = |
| 293 | bt_ctf_attributes_get_field_value_by_name( |
| 294 | trace->environment, name); |
| 295 | |
| 296 | if (attribute) { |
| 297 | BT_OBJECT_PUT(attribute); |
| 298 | ret = -1; |
| 299 | goto end; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | env_value_integer_obj = bt_object_integer_create_init(value); |
| 304 | if (!env_value_integer_obj) { |
| 305 | ret = -1; |
| 306 | goto end; |
| 307 | } |
| 308 | |
| 309 | ret = bt_ctf_trace_set_environment_field(trace, name, |
| 310 | env_value_integer_obj); |
| 311 | if (trace->frozen) { |
| 312 | bt_object_freeze(env_value_integer_obj); |
| 313 | } |
| 314 | end: |
| 315 | BT_OBJECT_PUT(env_value_integer_obj); |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
| 321 | { |
| 322 | int ret = 0; |
| 323 | |
| 324 | if (!trace) { |
| 325 | ret = -1; |
| 326 | goto end; |
| 327 | } |
| 328 | |
| 329 | ret = bt_ctf_attributes_get_count(trace->environment); |
| 330 | |
| 331 | end: |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | const char * |
| 336 | bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, |
| 337 | int index) |
| 338 | { |
| 339 | const char *ret = NULL; |
| 340 | |
| 341 | if (!trace) { |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | ret = bt_ctf_attributes_get_field_name(trace->environment, index); |
| 346 | |
| 347 | end: |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | struct bt_object *bt_ctf_trace_get_environment_field_value( |
| 352 | struct bt_ctf_trace *trace, int index) |
| 353 | { |
| 354 | struct bt_object *ret = NULL; |
| 355 | |
| 356 | if (!trace) { |
| 357 | goto end; |
| 358 | } |
| 359 | |
| 360 | ret = bt_ctf_attributes_get_field_value(trace->environment, index); |
| 361 | |
| 362 | end: |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | struct bt_object *bt_ctf_trace_get_environment_field_value_by_name( |
| 367 | struct bt_ctf_trace *trace, const char *name) |
| 368 | { |
| 369 | struct bt_object *ret = NULL; |
| 370 | |
| 371 | if (!trace || !name) { |
| 372 | goto end; |
| 373 | } |
| 374 | |
| 375 | ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, |
| 376 | name); |
| 377 | |
| 378 | end: |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace, |
| 383 | struct bt_ctf_clock *clock) |
| 384 | { |
| 385 | int ret = 0; |
| 386 | struct search_query query = { .value = clock, .found = 0 }; |
| 387 | |
| 388 | if (!trace || !clock) { |
| 389 | ret = -1; |
| 390 | goto end; |
| 391 | } |
| 392 | |
| 393 | /* Check for duplicate clocks */ |
| 394 | g_ptr_array_foreach(trace->clocks, value_exists, &query); |
| 395 | if (query.found) { |
| 396 | ret = -1; |
| 397 | goto end; |
| 398 | } |
| 399 | |
| 400 | bt_ctf_clock_get(clock); |
| 401 | g_ptr_array_add(trace->clocks, clock); |
| 402 | end: |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace) |
| 407 | { |
| 408 | int ret = -1; |
| 409 | |
| 410 | if (!trace) { |
| 411 | goto end; |
| 412 | } |
| 413 | |
| 414 | ret = trace->clocks->len; |
| 415 | end: |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace, |
| 420 | int index) |
| 421 | { |
| 422 | struct bt_ctf_clock *clock = NULL; |
| 423 | |
| 424 | if (!trace || index < 0 || index >= trace->clocks->len) { |
| 425 | goto end; |
| 426 | } |
| 427 | |
| 428 | clock = g_ptr_array_index(trace->clocks, index); |
| 429 | bt_ctf_clock_get(clock); |
| 430 | end: |
| 431 | return clock; |
| 432 | } |
| 433 | |
| 434 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
| 435 | struct bt_ctf_stream_class *stream_class) |
| 436 | { |
| 437 | int ret, i; |
| 438 | int64_t stream_id; |
| 439 | |
| 440 | if (!trace || !stream_class) { |
| 441 | ret = -1; |
| 442 | goto end; |
| 443 | } |
| 444 | |
| 445 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 446 | if (trace->stream_classes->pdata[i] == stream_class) { |
| 447 | /* Stream already registered to the trace */ |
| 448 | ret = -1; |
| 449 | goto end; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | ret = bt_ctf_stream_class_resolve_types(stream_class, trace); |
| 454 | if (ret) { |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | stream_id = bt_ctf_stream_class_get_id(stream_class); |
| 459 | if (stream_id < 0) { |
| 460 | stream_id = trace->next_stream_id++; |
| 461 | |
| 462 | /* Try to assign a new stream id */ |
| 463 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 464 | if (stream_id == bt_ctf_stream_class_get_id( |
| 465 | trace->stream_classes->pdata[i])) { |
| 466 | /* Duplicate stream id found */ |
| 467 | ret = -1; |
| 468 | goto end; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | if (bt_ctf_stream_class_set_id_no_check(stream_class, |
| 473 | stream_id)) { |
| 474 | /* TODO Should retry with a different stream id */ |
| 475 | ret = -1; |
| 476 | goto end; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* Set weak reference to trace in stream class */ |
| 481 | ret = bt_ctf_stream_class_set_trace(stream_class, trace); |
| 482 | if (ret) { |
| 483 | /* Stream class already part of another trace */ |
| 484 | goto end; |
| 485 | } |
| 486 | |
| 487 | bt_ctf_stream_class_get(stream_class); |
| 488 | g_ptr_array_add(trace->stream_classes, stream_class); |
| 489 | |
| 490 | /* |
| 491 | * Freeze the trace and its packet header. |
| 492 | * |
| 493 | * All field type byte orders set as "native" byte ordering can now be |
| 494 | * safely set to trace's own endianness, including the stream class'. |
| 495 | */ |
| 496 | bt_ctf_field_type_set_native_byte_order(trace->packet_header_type, |
| 497 | trace->byte_order); |
| 498 | ret = bt_ctf_stream_class_set_byte_order(stream_class, |
| 499 | trace->byte_order == LITTLE_ENDIAN ? |
| 500 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN); |
| 501 | if (ret) { |
| 502 | goto end; |
| 503 | } |
| 504 | |
| 505 | bt_ctf_stream_class_freeze(stream_class); |
| 506 | if (!trace->frozen) { |
| 507 | ret = bt_ctf_trace_freeze(trace); |
| 508 | goto end; |
| 509 | } |
| 510 | end: |
| 511 | if (ret) { |
| 512 | (void) bt_ctf_stream_class_set_trace(stream_class, NULL); |
| 513 | } |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
| 518 | { |
| 519 | int ret; |
| 520 | |
| 521 | if (!trace) { |
| 522 | ret = -1; |
| 523 | goto end; |
| 524 | } |
| 525 | |
| 526 | ret = trace->stream_classes->len; |
| 527 | end: |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class( |
| 532 | struct bt_ctf_trace *trace, int index) |
| 533 | { |
| 534 | struct bt_ctf_stream_class *stream_class = NULL; |
| 535 | |
| 536 | if (!trace || index < 0 || index >= trace->stream_classes->len) { |
| 537 | goto end; |
| 538 | } |
| 539 | |
| 540 | stream_class = g_ptr_array_index(trace->stream_classes, index); |
| 541 | bt_ctf_stream_class_get(stream_class); |
| 542 | end: |
| 543 | return stream_class; |
| 544 | } |
| 545 | |
| 546 | struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name( |
| 547 | struct bt_ctf_trace *trace, const char *name) |
| 548 | { |
| 549 | size_t i; |
| 550 | struct bt_ctf_clock *clock = NULL; |
| 551 | |
| 552 | if (!trace || !name) { |
| 553 | goto end; |
| 554 | } |
| 555 | |
| 556 | for (i = 0; i < trace->clocks->len; ++i) { |
| 557 | struct bt_ctf_clock *cur_clk = |
| 558 | g_ptr_array_index(trace->clocks, i); |
| 559 | const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk); |
| 560 | |
| 561 | if (!cur_clk_name) { |
| 562 | goto end; |
| 563 | } |
| 564 | |
| 565 | if (!strcmp(cur_clk_name, name)) { |
| 566 | clock = cur_clk; |
| 567 | bt_ctf_clock_get(clock); |
| 568 | goto end; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | end: |
| 573 | return clock; |
| 574 | } |
| 575 | |
| 576 | BT_HIDDEN |
| 577 | const char *get_byte_order_string(int byte_order) |
| 578 | { |
| 579 | const char *string; |
| 580 | |
| 581 | switch (byte_order) { |
| 582 | case LITTLE_ENDIAN: |
| 583 | string = "le"; |
| 584 | break; |
| 585 | case BIG_ENDIAN: |
| 586 | string = "be"; |
| 587 | break; |
| 588 | default: |
| 589 | string = "unknown"; |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | return string; |
| 594 | } |
| 595 | |
| 596 | static |
| 597 | int append_trace_metadata(struct bt_ctf_trace *trace, |
| 598 | struct metadata_context *context) |
| 599 | { |
| 600 | unsigned char *uuid = trace->uuid; |
| 601 | int ret; |
| 602 | |
| 603 | g_string_append(context->string, "trace {\n"); |
| 604 | |
| 605 | g_string_append(context->string, "\tmajor = 1;\n"); |
| 606 | g_string_append(context->string, "\tminor = 8;\n"); |
| 607 | |
| 608 | g_string_append_printf(context->string, |
| 609 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", |
| 610 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 611 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 612 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 613 | uuid[12], uuid[13], uuid[14], uuid[15]); |
| 614 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
| 615 | get_byte_order_string(trace->byte_order)); |
| 616 | |
| 617 | g_string_append(context->string, "\tpacket.header := "); |
| 618 | context->current_indentation_level++; |
| 619 | g_string_assign(context->field_name, ""); |
| 620 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, |
| 621 | context); |
| 622 | if (ret) { |
| 623 | goto end; |
| 624 | } |
| 625 | context->current_indentation_level--; |
| 626 | |
| 627 | g_string_append(context->string, ";\n};\n\n"); |
| 628 | end: |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | static |
| 633 | void append_env_metadata(struct bt_ctf_trace *trace, |
| 634 | struct metadata_context *context) |
| 635 | { |
| 636 | int i; |
| 637 | int env_size; |
| 638 | |
| 639 | env_size = bt_ctf_attributes_get_count(trace->environment); |
| 640 | |
| 641 | if (env_size <= 0) { |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | g_string_append(context->string, "env {\n"); |
| 646 | |
| 647 | for (i = 0; i < env_size; ++i) { |
| 648 | struct bt_object *env_field_value_obj = NULL; |
| 649 | const char *entry_name; |
| 650 | |
| 651 | entry_name = bt_ctf_attributes_get_field_name( |
| 652 | trace->environment, i); |
| 653 | env_field_value_obj = bt_ctf_attributes_get_field_value( |
| 654 | trace->environment, i); |
| 655 | |
| 656 | if (!entry_name || !env_field_value_obj) { |
| 657 | goto loop_next; |
| 658 | } |
| 659 | |
| 660 | switch (bt_object_get_type(env_field_value_obj)) { |
| 661 | case BT_OBJECT_TYPE_INTEGER: |
| 662 | { |
| 663 | int ret; |
| 664 | int64_t int_value; |
| 665 | |
| 666 | ret = bt_object_integer_get(env_field_value_obj, |
| 667 | &int_value); |
| 668 | |
| 669 | if (ret) { |
| 670 | goto loop_next; |
| 671 | } |
| 672 | |
| 673 | g_string_append_printf(context->string, |
| 674 | "\t%s = %" PRId64 ";\n", entry_name, |
| 675 | int_value); |
| 676 | break; |
| 677 | } |
| 678 | case BT_OBJECT_TYPE_STRING: |
| 679 | { |
| 680 | int ret; |
| 681 | const char *str_value; |
| 682 | char *escaped_str = NULL; |
| 683 | |
| 684 | ret = bt_object_string_get(env_field_value_obj, |
| 685 | &str_value); |
| 686 | |
| 687 | if (ret) { |
| 688 | goto loop_next; |
| 689 | } |
| 690 | |
| 691 | escaped_str = g_strescape(str_value, NULL); |
| 692 | |
| 693 | if (!escaped_str) { |
| 694 | goto loop_next; |
| 695 | } |
| 696 | |
| 697 | g_string_append_printf(context->string, |
| 698 | "\t%s = \"%s\";\n", entry_name, escaped_str); |
| 699 | free(escaped_str); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | default: |
| 704 | goto loop_next; |
| 705 | } |
| 706 | |
| 707 | loop_next: |
| 708 | BT_OBJECT_PUT(env_field_value_obj); |
| 709 | } |
| 710 | |
| 711 | g_string_append(context->string, "};\n\n"); |
| 712 | } |
| 713 | |
| 714 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) |
| 715 | { |
| 716 | char *metadata = NULL; |
| 717 | struct metadata_context *context = NULL; |
| 718 | int err = 0; |
| 719 | size_t i; |
| 720 | |
| 721 | if (!trace) { |
| 722 | goto end; |
| 723 | } |
| 724 | |
| 725 | context = g_new0(struct metadata_context, 1); |
| 726 | if (!context) { |
| 727 | goto end; |
| 728 | } |
| 729 | |
| 730 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); |
| 731 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); |
| 732 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); |
| 733 | if (append_trace_metadata(trace, context)) { |
| 734 | goto error; |
| 735 | } |
| 736 | append_env_metadata(trace, context); |
| 737 | g_ptr_array_foreach(trace->clocks, |
| 738 | (GFunc)bt_ctf_clock_serialize, context); |
| 739 | |
| 740 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 741 | err = bt_ctf_stream_class_serialize( |
| 742 | trace->stream_classes->pdata[i], context); |
| 743 | if (err) { |
| 744 | goto error; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | metadata = context->string->str; |
| 749 | error: |
| 750 | g_string_free(context->string, err ? TRUE : FALSE); |
| 751 | g_string_free(context->field_name, TRUE); |
| 752 | g_free(context); |
| 753 | end: |
| 754 | return metadata; |
| 755 | } |
| 756 | |
| 757 | enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) |
| 758 | { |
| 759 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; |
| 760 | |
| 761 | if (!trace) { |
| 762 | goto end; |
| 763 | } |
| 764 | |
| 765 | switch (trace->byte_order) { |
| 766 | case BIG_ENDIAN: |
| 767 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; |
| 768 | break; |
| 769 | case LITTLE_ENDIAN: |
| 770 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; |
| 771 | break; |
| 772 | default: |
| 773 | break; |
| 774 | } |
| 775 | end: |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, |
| 780 | enum bt_ctf_byte_order byte_order) |
| 781 | { |
| 782 | int ret = 0; |
| 783 | int internal_byte_order; |
| 784 | |
| 785 | if (!trace || trace->frozen) { |
| 786 | ret = -1; |
| 787 | goto end; |
| 788 | } |
| 789 | |
| 790 | switch (byte_order) { |
| 791 | case BT_CTF_BYTE_ORDER_NATIVE: |
| 792 | /* |
| 793 | * This doesn't make sense since the CTF specification defines |
| 794 | * the "native" byte order as "the byte order described in the |
| 795 | * trace description". However, this behavior had been |
| 796 | * implemented as part of v1.2 and is kept to maintain |
| 797 | * compatibility. |
| 798 | * |
| 799 | * This may be changed on a major version bump only. |
| 800 | */ |
| 801 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? |
| 802 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 803 | break; |
| 804 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 805 | internal_byte_order = LITTLE_ENDIAN; |
| 806 | break; |
| 807 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 808 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 809 | internal_byte_order = BIG_ENDIAN; |
| 810 | break; |
| 811 | default: |
| 812 | ret = -1; |
| 813 | goto end; |
| 814 | } |
| 815 | |
| 816 | trace->byte_order = internal_byte_order; |
| 817 | end: |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
| 822 | struct bt_ctf_trace *trace) |
| 823 | { |
| 824 | struct bt_ctf_field_type *field_type = NULL; |
| 825 | |
| 826 | if (!trace) { |
| 827 | goto end; |
| 828 | } |
| 829 | |
| 830 | bt_ctf_field_type_get(trace->packet_header_type); |
| 831 | field_type = trace->packet_header_type; |
| 832 | end: |
| 833 | return field_type; |
| 834 | } |
| 835 | |
| 836 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, |
| 837 | struct bt_ctf_field_type *packet_header_type) |
| 838 | { |
| 839 | int ret = 0; |
| 840 | |
| 841 | if (!trace || !packet_header_type || trace->frozen) { |
| 842 | ret = -1; |
| 843 | goto end; |
| 844 | } |
| 845 | |
| 846 | /* packet_header_type must be a structure */ |
| 847 | if (bt_ctf_field_type_get_type_id(packet_header_type) != |
| 848 | CTF_TYPE_STRUCT) { |
| 849 | ret = -1; |
| 850 | goto end; |
| 851 | } |
| 852 | |
| 853 | bt_ctf_field_type_get(packet_header_type); |
| 854 | bt_ctf_field_type_put(trace->packet_header_type); |
| 855 | trace->packet_header_type = packet_header_type; |
| 856 | end: |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | void bt_ctf_trace_get(struct bt_ctf_trace *trace) |
| 861 | { |
| 862 | if (!trace) { |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | bt_ctf_ref_get(&trace->ref_count); |
| 867 | } |
| 868 | |
| 869 | void bt_ctf_trace_put(struct bt_ctf_trace *trace) |
| 870 | { |
| 871 | if (!trace) { |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy); |
| 876 | } |
| 877 | |
| 878 | BT_HIDDEN |
| 879 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) |
| 880 | { |
| 881 | unsigned int alignment, size; |
| 882 | struct bt_ctf_field_type *field_type = NULL; |
| 883 | |
| 884 | if (alias >= NR_FIELD_TYPE_ALIAS) { |
| 885 | goto end; |
| 886 | } |
| 887 | |
| 888 | alignment = field_type_aliases_alignments[alias]; |
| 889 | size = field_type_aliases_sizes[alias]; |
| 890 | field_type = bt_ctf_field_type_integer_create(size); |
| 891 | bt_ctf_field_type_set_alignment(field_type, alignment); |
| 892 | end: |
| 893 | return field_type; |
| 894 | } |
| 895 | |
| 896 | static |
| 897 | int bt_ctf_trace_freeze(struct bt_ctf_trace *trace) |
| 898 | { |
| 899 | int ret = 0; |
| 900 | |
| 901 | ret = bt_ctf_trace_resolve_types(trace); |
| 902 | if (ret) { |
| 903 | goto end; |
| 904 | } |
| 905 | |
| 906 | bt_ctf_attributes_freeze(trace->environment); |
| 907 | trace->frozen = 1; |
| 908 | end: |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | static |
| 913 | int init_trace_packet_header(struct bt_ctf_trace *trace) |
| 914 | { |
| 915 | int ret = 0; |
| 916 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; |
| 917 | struct bt_ctf_field_type *_uint32_t = |
| 918 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 919 | struct bt_ctf_field_type *_uint8_t = |
| 920 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 921 | struct bt_ctf_field_type *trace_packet_header_type = |
| 922 | bt_ctf_field_type_structure_create(); |
| 923 | struct bt_ctf_field_type *uuid_array_type = |
| 924 | bt_ctf_field_type_array_create(_uint8_t, 16); |
| 925 | |
| 926 | if (!trace_packet_header_type || !uuid_array_type) { |
| 927 | ret = -1; |
| 928 | goto end; |
| 929 | } |
| 930 | |
| 931 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 932 | _uint32_t, "magic"); |
| 933 | if (ret) { |
| 934 | goto end; |
| 935 | } |
| 936 | |
| 937 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 938 | uuid_array_type, "uuid"); |
| 939 | if (ret) { |
| 940 | goto end; |
| 941 | } |
| 942 | |
| 943 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 944 | _uint32_t, "stream_id"); |
| 945 | if (ret) { |
| 946 | goto end; |
| 947 | } |
| 948 | |
| 949 | ret = bt_ctf_trace_set_packet_header_type(trace, |
| 950 | trace_packet_header_type); |
| 951 | if (ret) { |
| 952 | goto end; |
| 953 | } |
| 954 | end: |
| 955 | bt_ctf_field_type_put(uuid_array_type); |
| 956 | bt_ctf_field_type_put(_uint32_t); |
| 957 | bt_ctf_field_type_put(_uint8_t); |
| 958 | bt_ctf_field_put(magic); |
| 959 | bt_ctf_field_put(uuid_array); |
| 960 | bt_ctf_field_type_put(trace_packet_header_type); |
| 961 | |
| 962 | return ret; |
| 963 | } |