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