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