| 1 | /* |
| 2 | * writer.c |
| 3 | * |
| 4 | * Babeltrace CTF Writer |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/ctf-writer/writer.h> |
| 30 | #include <babeltrace/ctf-writer/clock.h> |
| 31 | #include <babeltrace/ctf-ir/clock-internal.h> |
| 32 | #include <babeltrace/ctf-writer/writer-internal.h> |
| 33 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
| 35 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 36 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 37 | #include <babeltrace/ctf-writer/stream-internal.h> |
| 38 | #include <babeltrace/ctf-writer/stream.h> |
| 39 | #include <babeltrace/compiler.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <errno.h> |
| 44 | #include <unistd.h> |
| 45 | #include <fcntl.h> |
| 46 | #include <inttypes.h> |
| 47 | |
| 48 | #define DEFAULT_IDENTIFIER_SIZE 128 |
| 49 | #define DEFAULT_METADATA_STRING_SIZE 4096 |
| 50 | |
| 51 | static |
| 52 | void environment_variable_destroy(struct environment_variable *var); |
| 53 | static |
| 54 | void bt_ctf_writer_destroy(struct bt_ctf_ref *ref); |
| 55 | static |
| 56 | int init_trace_packet_header(struct bt_ctf_writer *writer); |
| 57 | static |
| 58 | int create_stream_file(struct bt_ctf_writer *writer, |
| 59 | struct bt_ctf_stream *stream); |
| 60 | static |
| 61 | void stream_flush_cb(struct bt_ctf_stream *stream, |
| 62 | struct bt_ctf_writer *writer); |
| 63 | |
| 64 | static |
| 65 | const char * const reserved_keywords_str[] = {"align", "callsite", |
| 66 | "const", "char", "clock", "double", "enum", "env", "event", |
| 67 | "floating_point", "float", "integer", "int", "long", "short", "signed", |
| 68 | "stream", "string", "struct", "trace", "typealias", "typedef", |
| 69 | "unsigned", "variant", "void" "_Bool", "_Complex", "_Imaginary"}; |
| 70 | |
| 71 | static |
| 72 | const unsigned int field_type_aliases_alignments[] = { |
| 73 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, |
| 74 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, |
| 75 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, |
| 76 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, |
| 77 | }; |
| 78 | |
| 79 | static |
| 80 | const unsigned int field_type_aliases_sizes[] = { |
| 81 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, |
| 82 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, |
| 83 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, |
| 84 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, |
| 85 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, |
| 86 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, |
| 87 | }; |
| 88 | |
| 89 | static GHashTable *reserved_keywords_set; |
| 90 | static int init_done; |
| 91 | static int global_data_refcount; |
| 92 | |
| 93 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path) |
| 94 | { |
| 95 | struct bt_ctf_writer *writer = NULL; |
| 96 | |
| 97 | if (!path) { |
| 98 | goto error; |
| 99 | } |
| 100 | |
| 101 | writer = g_new0(struct bt_ctf_writer, 1); |
| 102 | if (!writer) { |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE); |
| 107 | bt_ctf_ref_init(&writer->ref_count); |
| 108 | writer->path = g_string_new(path); |
| 109 | if (!writer->path) { |
| 110 | goto error_destroy; |
| 111 | } |
| 112 | |
| 113 | /* Create trace directory if necessary and open a metadata file */ |
| 114 | if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { |
| 115 | perror("g_mkdir_with_parents"); |
| 116 | goto error_destroy; |
| 117 | } |
| 118 | |
| 119 | writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); |
| 120 | if (writer->trace_dir_fd < 0) { |
| 121 | perror("open"); |
| 122 | goto error_destroy; |
| 123 | } |
| 124 | |
| 125 | writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", |
| 126 | O_WRONLY | O_CREAT | O_TRUNC, |
| 127 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 128 | writer->environment = g_ptr_array_new_with_free_func( |
| 129 | (GDestroyNotify)environment_variable_destroy); |
| 130 | writer->clocks = g_ptr_array_new_with_free_func( |
| 131 | (GDestroyNotify)bt_ctf_clock_put); |
| 132 | writer->streams = g_ptr_array_new_with_free_func( |
| 133 | (GDestroyNotify)bt_ctf_stream_put); |
| 134 | writer->stream_classes = g_ptr_array_new_with_free_func( |
| 135 | (GDestroyNotify)bt_ctf_stream_class_put); |
| 136 | if (!writer->environment || !writer->clocks || |
| 137 | !writer->stream_classes || !writer->streams) { |
| 138 | goto error_destroy; |
| 139 | } |
| 140 | |
| 141 | /* Generate a trace UUID */ |
| 142 | uuid_generate(writer->uuid); |
| 143 | if (init_trace_packet_header(writer)) { |
| 144 | goto error_destroy; |
| 145 | } |
| 146 | |
| 147 | return writer; |
| 148 | error_destroy: |
| 149 | unlinkat(writer->trace_dir_fd, "metadata", 0); |
| 150 | bt_ctf_writer_destroy(&writer->ref_count); |
| 151 | writer = NULL; |
| 152 | error: |
| 153 | return writer; |
| 154 | } |
| 155 | |
| 156 | void bt_ctf_writer_destroy(struct bt_ctf_ref *ref) |
| 157 | { |
| 158 | struct bt_ctf_writer *writer; |
| 159 | |
| 160 | if (!ref) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | writer = container_of(ref, struct bt_ctf_writer, ref_count); |
| 165 | bt_ctf_writer_flush_metadata(writer); |
| 166 | if (writer->path) { |
| 167 | g_string_free(writer->path, TRUE); |
| 168 | } |
| 169 | |
| 170 | if (writer->trace_dir_fd > 0) { |
| 171 | if (close(writer->trace_dir_fd)) { |
| 172 | perror("close"); |
| 173 | abort(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (writer->metadata_fd > 0) { |
| 178 | if (close(writer->metadata_fd)) { |
| 179 | perror("close"); |
| 180 | abort(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (writer->environment) { |
| 185 | g_ptr_array_free(writer->environment, TRUE); |
| 186 | } |
| 187 | |
| 188 | if (writer->clocks) { |
| 189 | g_ptr_array_free(writer->clocks, TRUE); |
| 190 | } |
| 191 | |
| 192 | if (writer->streams) { |
| 193 | g_ptr_array_free(writer->streams, TRUE); |
| 194 | } |
| 195 | |
| 196 | if (writer->stream_classes) { |
| 197 | g_ptr_array_free(writer->stream_classes, TRUE); |
| 198 | } |
| 199 | |
| 200 | bt_ctf_field_type_put(writer->trace_packet_header_type); |
| 201 | bt_ctf_field_put(writer->trace_packet_header); |
| 202 | g_free(writer); |
| 203 | } |
| 204 | |
| 205 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, |
| 206 | struct bt_ctf_stream_class *stream_class) |
| 207 | { |
| 208 | int ret; |
| 209 | int stream_class_found = 0; |
| 210 | size_t i; |
| 211 | int stream_fd; |
| 212 | struct bt_ctf_stream *stream = NULL; |
| 213 | |
| 214 | if (!writer || !stream_class) { |
| 215 | goto error; |
| 216 | } |
| 217 | |
| 218 | stream = bt_ctf_stream_create(stream_class); |
| 219 | if (!stream) { |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | stream_fd = create_stream_file(writer, stream); |
| 224 | if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) { |
| 225 | goto error; |
| 226 | } |
| 227 | |
| 228 | bt_ctf_stream_set_flush_callback(stream, (flush_func)stream_flush_cb, |
| 229 | writer); |
| 230 | ret = bt_ctf_stream_class_set_byte_order(stream->stream_class, |
| 231 | writer->byte_order == LITTLE_ENDIAN ? |
| 232 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN); |
| 233 | if (ret) { |
| 234 | goto error; |
| 235 | } |
| 236 | |
| 237 | for (i = 0; i < writer->stream_classes->len; i++) { |
| 238 | if (writer->stream_classes->pdata[i] == stream->stream_class) { |
| 239 | stream_class_found = 1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (!stream_class_found) { |
| 244 | if (bt_ctf_stream_class_set_id(stream->stream_class, |
| 245 | writer->next_stream_id++)) { |
| 246 | goto error; |
| 247 | } |
| 248 | |
| 249 | bt_ctf_stream_class_get(stream->stream_class); |
| 250 | g_ptr_array_add(writer->stream_classes, stream->stream_class); |
| 251 | } |
| 252 | |
| 253 | bt_ctf_stream_get(stream); |
| 254 | g_ptr_array_add(writer->streams, stream); |
| 255 | writer->frozen = 1; |
| 256 | return stream; |
| 257 | error: |
| 258 | bt_ctf_stream_put(stream); |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, |
| 263 | const char *name, |
| 264 | const char *value) |
| 265 | { |
| 266 | struct environment_variable *var = NULL; |
| 267 | char *escaped_value = NULL; |
| 268 | int ret = 0; |
| 269 | |
| 270 | if (!writer || !name || !value || validate_identifier(name)) { |
| 271 | ret = -1; |
| 272 | goto error; |
| 273 | } |
| 274 | |
| 275 | if (strchr(name, ' ')) { |
| 276 | ret = -1; |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | var = g_new0(struct environment_variable, 1); |
| 281 | if (!var) { |
| 282 | ret = -1; |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | escaped_value = g_strescape(value, NULL); |
| 287 | if (!escaped_value) { |
| 288 | ret = -1; |
| 289 | goto error; |
| 290 | } |
| 291 | |
| 292 | var->name = g_string_new(name); |
| 293 | var->value = g_string_new(escaped_value); |
| 294 | g_free(escaped_value); |
| 295 | if (!var->name || !var->value) { |
| 296 | ret = -1; |
| 297 | goto error; |
| 298 | } |
| 299 | |
| 300 | g_ptr_array_add(writer->environment, var); |
| 301 | return ret; |
| 302 | |
| 303 | error: |
| 304 | if (var && var->name) { |
| 305 | g_string_free(var->name, TRUE); |
| 306 | } |
| 307 | |
| 308 | if (var && var->value) { |
| 309 | g_string_free(var->value, TRUE); |
| 310 | } |
| 311 | |
| 312 | g_free(var); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, |
| 317 | struct bt_ctf_clock *clock) |
| 318 | { |
| 319 | int ret = 0; |
| 320 | struct search_query query = { .value = clock, .found = 0 }; |
| 321 | |
| 322 | if (!writer || !clock) { |
| 323 | ret = -1; |
| 324 | goto end; |
| 325 | } |
| 326 | |
| 327 | /* Check for duplicate clocks */ |
| 328 | g_ptr_array_foreach(writer->clocks, value_exists, &query); |
| 329 | if (query.found) { |
| 330 | ret = -1; |
| 331 | goto end; |
| 332 | } |
| 333 | |
| 334 | bt_ctf_clock_get(clock); |
| 335 | g_ptr_array_add(writer->clocks, clock); |
| 336 | end: |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | BT_HIDDEN |
| 341 | const char *get_byte_order_string(int byte_order) |
| 342 | { |
| 343 | const char *string; |
| 344 | |
| 345 | switch (byte_order) { |
| 346 | case LITTLE_ENDIAN: |
| 347 | string = "le"; |
| 348 | break; |
| 349 | case BIG_ENDIAN: |
| 350 | string = "be"; |
| 351 | break; |
| 352 | default: |
| 353 | string = "unknown"; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | return string; |
| 358 | } |
| 359 | |
| 360 | static |
| 361 | int append_trace_metadata(struct bt_ctf_writer *writer, |
| 362 | struct metadata_context *context) |
| 363 | { |
| 364 | unsigned char *uuid = writer->uuid; |
| 365 | int ret; |
| 366 | |
| 367 | g_string_append(context->string, "trace {\n"); |
| 368 | |
| 369 | g_string_append(context->string, "\tmajor = 1;\n"); |
| 370 | g_string_append(context->string, "\tminor = 8;\n"); |
| 371 | |
| 372 | g_string_append_printf(context->string, |
| 373 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", |
| 374 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 375 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 376 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 377 | uuid[12], uuid[13], uuid[14], uuid[15]); |
| 378 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
| 379 | get_byte_order_string(writer->byte_order)); |
| 380 | |
| 381 | g_string_append(context->string, "\tpacket.header := "); |
| 382 | context->current_indentation_level++; |
| 383 | g_string_assign(context->field_name, ""); |
| 384 | ret = bt_ctf_field_type_serialize(writer->trace_packet_header_type, |
| 385 | context); |
| 386 | if (ret) { |
| 387 | goto end; |
| 388 | } |
| 389 | context->current_indentation_level--; |
| 390 | |
| 391 | g_string_append(context->string, ";\n};\n\n"); |
| 392 | end: |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static |
| 397 | void append_env_field_metadata(struct environment_variable *var, |
| 398 | struct metadata_context *context) |
| 399 | { |
| 400 | g_string_append_printf(context->string, "\t%s = \"%s\";\n", |
| 401 | var->name->str, var->value->str); |
| 402 | } |
| 403 | |
| 404 | static |
| 405 | void append_env_metadata(struct bt_ctf_writer *writer, |
| 406 | struct metadata_context *context) |
| 407 | { |
| 408 | if (writer->environment->len == 0) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | g_string_append(context->string, "env {\n"); |
| 413 | g_ptr_array_foreach(writer->environment, |
| 414 | (GFunc)append_env_field_metadata, context); |
| 415 | g_string_append(context->string, "};\n\n"); |
| 416 | } |
| 417 | |
| 418 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) |
| 419 | { |
| 420 | char *metadata = NULL; |
| 421 | struct metadata_context *context = NULL; |
| 422 | int err = 0; |
| 423 | size_t i; |
| 424 | |
| 425 | if (!writer) { |
| 426 | goto end; |
| 427 | } |
| 428 | |
| 429 | context = g_new0(struct metadata_context, 1); |
| 430 | if (!context) { |
| 431 | goto end; |
| 432 | } |
| 433 | |
| 434 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); |
| 435 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); |
| 436 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); |
| 437 | if (append_trace_metadata(writer, context)) { |
| 438 | goto error; |
| 439 | } |
| 440 | append_env_metadata(writer, context); |
| 441 | g_ptr_array_foreach(writer->clocks, |
| 442 | (GFunc)bt_ctf_clock_serialize, context); |
| 443 | |
| 444 | for (i = 0; i < writer->stream_classes->len; i++) { |
| 445 | err = bt_ctf_stream_class_serialize( |
| 446 | writer->stream_classes->pdata[i], context); |
| 447 | if (err) { |
| 448 | goto error; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | metadata = context->string->str; |
| 453 | error: |
| 454 | g_string_free(context->string, err ? TRUE : FALSE); |
| 455 | g_string_free(context->field_name, TRUE); |
| 456 | g_free(context); |
| 457 | end: |
| 458 | return metadata; |
| 459 | } |
| 460 | |
| 461 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) |
| 462 | { |
| 463 | int ret; |
| 464 | char *metadata_string = NULL; |
| 465 | |
| 466 | if (!writer) { |
| 467 | goto end; |
| 468 | } |
| 469 | |
| 470 | metadata_string = bt_ctf_writer_get_metadata_string(writer); |
| 471 | if (!metadata_string) { |
| 472 | goto end; |
| 473 | } |
| 474 | |
| 475 | if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { |
| 476 | perror("lseek"); |
| 477 | goto end; |
| 478 | } |
| 479 | |
| 480 | if (ftruncate(writer->metadata_fd, 0)) { |
| 481 | perror("ftruncate"); |
| 482 | goto end; |
| 483 | } |
| 484 | |
| 485 | ret = write(writer->metadata_fd, metadata_string, |
| 486 | strlen(metadata_string)); |
| 487 | if (ret < 0) { |
| 488 | perror("write"); |
| 489 | goto end; |
| 490 | } |
| 491 | end: |
| 492 | g_free(metadata_string); |
| 493 | } |
| 494 | |
| 495 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, |
| 496 | enum bt_ctf_byte_order byte_order) |
| 497 | { |
| 498 | int ret = 0; |
| 499 | int internal_byte_order; |
| 500 | |
| 501 | if (!writer || writer->frozen) { |
| 502 | ret = -1; |
| 503 | goto end; |
| 504 | } |
| 505 | |
| 506 | switch (byte_order) { |
| 507 | case BT_CTF_BYTE_ORDER_NATIVE: |
| 508 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? |
| 509 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 510 | break; |
| 511 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 512 | internal_byte_order = LITTLE_ENDIAN; |
| 513 | break; |
| 514 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 515 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 516 | internal_byte_order = BIG_ENDIAN; |
| 517 | break; |
| 518 | default: |
| 519 | ret = -1; |
| 520 | goto end; |
| 521 | } |
| 522 | |
| 523 | writer->byte_order = internal_byte_order; |
| 524 | if (writer->trace_packet_header_type || |
| 525 | writer->trace_packet_header) { |
| 526 | init_trace_packet_header(writer); |
| 527 | } |
| 528 | end: |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | void bt_ctf_writer_get(struct bt_ctf_writer *writer) |
| 533 | { |
| 534 | if (!writer) { |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | bt_ctf_ref_get(&writer->ref_count); |
| 539 | } |
| 540 | |
| 541 | void bt_ctf_writer_put(struct bt_ctf_writer *writer) |
| 542 | { |
| 543 | if (!writer) { |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy); |
| 548 | } |
| 549 | |
| 550 | BT_HIDDEN |
| 551 | int validate_identifier(const char *input_string) |
| 552 | { |
| 553 | int ret = 0; |
| 554 | char *string = NULL; |
| 555 | char *save_ptr, *token; |
| 556 | |
| 557 | if (!input_string || input_string[0] == '\0') { |
| 558 | ret = -1; |
| 559 | goto end; |
| 560 | } |
| 561 | |
| 562 | string = strdup(input_string); |
| 563 | if (!string) { |
| 564 | ret = -1; |
| 565 | goto end; |
| 566 | } |
| 567 | |
| 568 | token = strtok_r(string, " ", &save_ptr); |
| 569 | while (token) { |
| 570 | if (g_hash_table_lookup_extended(reserved_keywords_set, |
| 571 | GINT_TO_POINTER(g_quark_from_string(token)), |
| 572 | NULL, NULL)) { |
| 573 | ret = -1; |
| 574 | goto end; |
| 575 | } |
| 576 | |
| 577 | token = strtok_r(NULL, " ", &save_ptr); |
| 578 | } |
| 579 | end: |
| 580 | free(string); |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | BT_HIDDEN |
| 585 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) |
| 586 | { |
| 587 | unsigned int alignment, size; |
| 588 | struct bt_ctf_field_type *field_type; |
| 589 | |
| 590 | if (alias >= NR_FIELD_TYPE_ALIAS) { |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | alignment = field_type_aliases_alignments[alias]; |
| 595 | size = field_type_aliases_sizes[alias]; |
| 596 | field_type = bt_ctf_field_type_integer_create(size); |
| 597 | bt_ctf_field_type_set_alignment(field_type, alignment); |
| 598 | return field_type; |
| 599 | } |
| 600 | |
| 601 | static |
| 602 | int init_trace_packet_header(struct bt_ctf_writer *writer) |
| 603 | { |
| 604 | size_t i; |
| 605 | int ret = 0; |
| 606 | struct bt_ctf_field *trace_packet_header = NULL, |
| 607 | *magic = NULL, *uuid_array = NULL; |
| 608 | struct bt_ctf_field_type *_uint32_t = |
| 609 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 610 | struct bt_ctf_field_type *_uint8_t = |
| 611 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
| 612 | struct bt_ctf_field_type *trace_packet_header_type = |
| 613 | bt_ctf_field_type_structure_create(); |
| 614 | struct bt_ctf_field_type *uuid_array_type = |
| 615 | bt_ctf_field_type_array_create(_uint8_t, 16); |
| 616 | |
| 617 | if (!trace_packet_header_type || !uuid_array_type) { |
| 618 | ret = -1; |
| 619 | goto end; |
| 620 | } |
| 621 | |
| 622 | ret = bt_ctf_field_type_set_byte_order(_uint32_t, |
| 623 | (writer->byte_order == LITTLE_ENDIAN ? |
| 624 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : |
| 625 | BT_CTF_BYTE_ORDER_BIG_ENDIAN)); |
| 626 | if (ret) { |
| 627 | goto end; |
| 628 | } |
| 629 | |
| 630 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 631 | _uint32_t, "magic"); |
| 632 | if (ret) { |
| 633 | goto end; |
| 634 | } |
| 635 | |
| 636 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 637 | uuid_array_type, "uuid"); |
| 638 | if (ret) { |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
| 643 | _uint32_t, "stream_id"); |
| 644 | if (ret) { |
| 645 | goto end; |
| 646 | } |
| 647 | |
| 648 | trace_packet_header = bt_ctf_field_create(trace_packet_header_type); |
| 649 | if (!trace_packet_header) { |
| 650 | ret = -1; |
| 651 | goto end; |
| 652 | } |
| 653 | |
| 654 | magic = bt_ctf_field_structure_get_field(trace_packet_header, "magic"); |
| 655 | ret = bt_ctf_field_unsigned_integer_set_value(magic, 0xC1FC1FC1); |
| 656 | if (ret) { |
| 657 | goto end; |
| 658 | } |
| 659 | |
| 660 | uuid_array = bt_ctf_field_structure_get_field(trace_packet_header, |
| 661 | "uuid"); |
| 662 | for (i = 0; i < 16; i++) { |
| 663 | struct bt_ctf_field *uuid_element = |
| 664 | bt_ctf_field_array_get_field(uuid_array, i); |
| 665 | ret = bt_ctf_field_unsigned_integer_set_value(uuid_element, |
| 666 | writer->uuid[i]); |
| 667 | bt_ctf_field_put(uuid_element); |
| 668 | if (ret) { |
| 669 | goto end; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | bt_ctf_field_type_put(writer->trace_packet_header_type); |
| 674 | bt_ctf_field_put(writer->trace_packet_header); |
| 675 | writer->trace_packet_header_type = trace_packet_header_type; |
| 676 | writer->trace_packet_header = trace_packet_header; |
| 677 | end: |
| 678 | bt_ctf_field_type_put(uuid_array_type); |
| 679 | bt_ctf_field_type_put(_uint32_t); |
| 680 | bt_ctf_field_type_put(_uint8_t); |
| 681 | bt_ctf_field_put(magic); |
| 682 | bt_ctf_field_put(uuid_array); |
| 683 | if (ret) { |
| 684 | bt_ctf_field_type_put(trace_packet_header_type); |
| 685 | bt_ctf_field_put(trace_packet_header); |
| 686 | } |
| 687 | |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static |
| 692 | void environment_variable_destroy(struct environment_variable *var) |
| 693 | { |
| 694 | g_string_free(var->name, TRUE); |
| 695 | g_string_free(var->value, TRUE); |
| 696 | g_free(var); |
| 697 | } |
| 698 | |
| 699 | static |
| 700 | int create_stream_file(struct bt_ctf_writer *writer, |
| 701 | struct bt_ctf_stream *stream) |
| 702 | { |
| 703 | int fd; |
| 704 | GString *filename = g_string_new(stream->stream_class->name->str); |
| 705 | |
| 706 | g_string_append_printf(filename, "_%" PRIu32, stream->id); |
| 707 | fd = openat(writer->trace_dir_fd, filename->str, |
| 708 | O_RDWR | O_CREAT | O_TRUNC, |
| 709 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 710 | g_string_free(filename, TRUE); |
| 711 | return fd; |
| 712 | } |
| 713 | |
| 714 | static |
| 715 | void stream_flush_cb(struct bt_ctf_stream *stream, struct bt_ctf_writer *writer) |
| 716 | { |
| 717 | struct bt_ctf_field *stream_id; |
| 718 | |
| 719 | /* Start a new packet in the stream */ |
| 720 | if (stream->flushed_packet_count) { |
| 721 | /* ctf_init_pos has already initialized the first packet */ |
| 722 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); |
| 723 | } |
| 724 | |
| 725 | stream_id = bt_ctf_field_structure_get_field( |
| 726 | writer->trace_packet_header, "stream_id"); |
| 727 | bt_ctf_field_unsigned_integer_set_value(stream_id, stream->stream_class->id); |
| 728 | bt_ctf_field_put(stream_id); |
| 729 | |
| 730 | /* Write the trace_packet_header */ |
| 731 | bt_ctf_field_serialize(writer->trace_packet_header, &stream->pos); |
| 732 | } |
| 733 | |
| 734 | static __attribute__((constructor)) |
| 735 | void writer_init(void) |
| 736 | { |
| 737 | size_t i; |
| 738 | const size_t reserved_keywords_count = |
| 739 | sizeof(reserved_keywords_str) / sizeof(char *); |
| 740 | |
| 741 | global_data_refcount++; |
| 742 | if (init_done) { |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | reserved_keywords_set = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 747 | for (i = 0; i < reserved_keywords_count; i++) { |
| 748 | gpointer quark = GINT_TO_POINTER(g_quark_from_string( |
| 749 | reserved_keywords_str[i])); |
| 750 | |
| 751 | g_hash_table_insert(reserved_keywords_set, quark, quark); |
| 752 | } |
| 753 | |
| 754 | init_done = 1; |
| 755 | } |
| 756 | |
| 757 | static __attribute__((destructor)) |
| 758 | void writer_finalize(void) |
| 759 | { |
| 760 | if (--global_data_refcount == 0) { |
| 761 | g_hash_table_destroy(reserved_keywords_set); |
| 762 | } |
| 763 | } |