| 1 | /* |
| 2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define BT_LOG_TAG "CTF-WRITER-TRACE" |
| 25 | #include <babeltrace/lib-logging-internal.h> |
| 26 | |
| 27 | #include <babeltrace/ctf-ir/trace-internal.h> |
| 28 | #include <babeltrace/ctf-ir/clock-class.h> |
| 29 | #include <babeltrace/ctf-writer/clock-internal.h> |
| 30 | #include <babeltrace/ctf-writer/trace-internal.h> |
| 31 | #include <babeltrace/ctf-writer/stream-class-internal.h> |
| 32 | #include <babeltrace/ctf-writer/writer-internal.h> |
| 33 | #include <babeltrace/ctf-writer/field-types-internal.h> |
| 34 | |
| 35 | #define DEFAULT_IDENTIFIER_SIZE 128 |
| 36 | #define DEFAULT_METADATA_STRING_SIZE 4096 |
| 37 | |
| 38 | static |
| 39 | void bt_ctf_trace_destroy(struct bt_object *obj) |
| 40 | { |
| 41 | struct bt_ctf_trace *trace = (void *) obj; |
| 42 | |
| 43 | BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"", |
| 44 | trace, bt_ctf_trace_get_name(trace)); |
| 45 | bt_trace_common_finalize(BT_TO_COMMON(trace)); |
| 46 | g_free(trace); |
| 47 | } |
| 48 | |
| 49 | BT_HIDDEN |
| 50 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
| 51 | { |
| 52 | struct bt_ctf_trace *trace = NULL; |
| 53 | int ret; |
| 54 | |
| 55 | BT_LOGD_STR("Creating CTF writer trace object."); |
| 56 | trace = g_new0(struct bt_ctf_trace, 1); |
| 57 | if (!trace) { |
| 58 | BT_LOGE_STR("Failed to allocate one CTF writer trace."); |
| 59 | goto error; |
| 60 | } |
| 61 | |
| 62 | ret = bt_trace_common_initialize(BT_TO_COMMON(trace), |
| 63 | bt_ctf_trace_destroy); |
| 64 | if (ret) { |
| 65 | /* bt_trace_common_initialize() logs errors */ |
| 66 | goto error; |
| 67 | } |
| 68 | |
| 69 | BT_LOGD("Created CTF writer trace object: addr=%p", trace); |
| 70 | return trace; |
| 71 | |
| 72 | error: |
| 73 | BT_PUT(trace); |
| 74 | return trace; |
| 75 | } |
| 76 | |
| 77 | const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace) |
| 78 | { |
| 79 | return bt_trace_common_get_uuid(BT_TO_COMMON(trace)); |
| 80 | } |
| 81 | |
| 82 | int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, |
| 83 | const unsigned char *uuid) |
| 84 | { |
| 85 | return bt_trace_common_set_uuid(BT_TO_COMMON(trace), uuid); |
| 86 | } |
| 87 | |
| 88 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
| 89 | const char *name, struct bt_value *value) |
| 90 | { |
| 91 | return bt_trace_common_set_environment_field(BT_TO_COMMON(trace), |
| 92 | name, value); |
| 93 | } |
| 94 | |
| 95 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
| 96 | const char *name, const char *value) |
| 97 | { |
| 98 | return bt_trace_common_set_environment_field_string(BT_TO_COMMON(trace), |
| 99 | name, value); |
| 100 | } |
| 101 | |
| 102 | int bt_ctf_trace_set_environment_field_integer( |
| 103 | struct bt_ctf_trace *trace, const char *name, int64_t value) |
| 104 | { |
| 105 | return bt_trace_common_set_environment_field_integer( |
| 106 | BT_TO_COMMON(trace), name, value); |
| 107 | } |
| 108 | |
| 109 | int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
| 110 | { |
| 111 | return bt_trace_common_get_environment_field_count(BT_TO_COMMON(trace)); |
| 112 | } |
| 113 | |
| 114 | const char * |
| 115 | bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace, |
| 116 | uint64_t index) |
| 117 | { |
| 118 | return bt_trace_common_get_environment_field_name_by_index( |
| 119 | BT_TO_COMMON(trace), index); |
| 120 | } |
| 121 | |
| 122 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_index( |
| 123 | struct bt_ctf_trace *trace, uint64_t index) |
| 124 | { |
| 125 | return bt_trace_common_get_environment_field_value_by_index( |
| 126 | BT_TO_COMMON(trace), index); |
| 127 | } |
| 128 | |
| 129 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( |
| 130 | struct bt_ctf_trace *trace, const char *name) |
| 131 | { |
| 132 | return bt_trace_common_get_environment_field_value_by_name( |
| 133 | BT_TO_COMMON(trace), name); |
| 134 | } |
| 135 | |
| 136 | int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace, |
| 137 | struct bt_ctf_clock_class *clock_class) |
| 138 | { |
| 139 | return bt_trace_common_add_clock_class(BT_TO_COMMON(trace), |
| 140 | (void *) clock_class); |
| 141 | } |
| 142 | |
| 143 | int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) |
| 144 | { |
| 145 | return bt_trace_common_get_clock_class_count(BT_TO_COMMON(trace)); |
| 146 | } |
| 147 | |
| 148 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index( |
| 149 | struct bt_ctf_trace *trace, uint64_t index) |
| 150 | { |
| 151 | return (void *) bt_trace_common_get_clock_class_by_index( |
| 152 | BT_TO_COMMON(trace), index); |
| 153 | } |
| 154 | |
| 155 | static |
| 156 | int map_clock_classes_func(struct bt_stream_class_common *stream_class, |
| 157 | struct bt_field_type_common *packet_context_type, |
| 158 | struct bt_field_type_common *event_header_type) |
| 159 | { |
| 160 | int ret = bt_ctf_stream_class_map_clock_class( |
| 161 | BT_FROM_COMMON(stream_class), |
| 162 | BT_FROM_COMMON(packet_context_type), |
| 163 | BT_FROM_COMMON(event_header_type)); |
| 164 | |
| 165 | if (ret) { |
| 166 | BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
| 173 | struct bt_ctf_stream_class *stream_class) |
| 174 | { |
| 175 | int ret = 0; |
| 176 | struct bt_clock_class *expected_clock_class = NULL; |
| 177 | |
| 178 | if (!trace) { |
| 179 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 180 | ret = -1; |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | if (!stream_class) { |
| 185 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 186 | ret = -1; |
| 187 | goto end; |
| 188 | } |
| 189 | |
| 190 | if (stream_class->clock) { |
| 191 | struct bt_ctf_clock_class *stream_clock_class = |
| 192 | stream_class->clock->clock_class; |
| 193 | |
| 194 | /* |
| 195 | * Make sure this clock was also added to the |
| 196 | * trace (potentially through its CTF writer |
| 197 | * owner). |
| 198 | */ |
| 199 | size_t i; |
| 200 | |
| 201 | for (i = 0; i < trace->common.clock_classes->len; i++) { |
| 202 | if (trace->common.clock_classes->pdata[i] == |
| 203 | stream_clock_class) { |
| 204 | /* Found! */ |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (i == trace->common.clock_classes->len) { |
| 210 | /* Not found */ |
| 211 | BT_LOGW("Stream class's clock's class is not part of the trace: " |
| 212 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 213 | stream_clock_class, |
| 214 | bt_clock_class_get_name( |
| 215 | BT_TO_COMMON(stream_clock_class))); |
| 216 | ret = -1; |
| 217 | goto end; |
| 218 | } |
| 219 | |
| 220 | if (stream_class->common.clock_class && |
| 221 | stream_class->common.clock_class != |
| 222 | BT_TO_COMMON(stream_class->clock->clock_class)) { |
| 223 | /* |
| 224 | * Stream class already has an expected clock |
| 225 | * class, but it does not match its clock's |
| 226 | * class. |
| 227 | */ |
| 228 | BT_LOGW("Invalid parameter: stream class's clock's " |
| 229 | "class does not match stream class's " |
| 230 | "expected clock class: " |
| 231 | "stream-class-addr=%p, " |
| 232 | "stream-class-id=%" PRId64 ", " |
| 233 | "stream-class-name=\"%s\", " |
| 234 | "expected-clock-class-addr=%p, " |
| 235 | "expected-clock-class-name=\"%s\"", |
| 236 | stream_class, |
| 237 | bt_ctf_stream_class_get_id(stream_class), |
| 238 | bt_ctf_stream_class_get_name(stream_class), |
| 239 | stream_class->common.clock_class, |
| 240 | bt_clock_class_get_name(stream_class->common.clock_class)); |
| 241 | } else if (!stream_class->common.clock_class) { |
| 242 | /* |
| 243 | * Set expected clock class to stream class's |
| 244 | * clock's class. |
| 245 | */ |
| 246 | expected_clock_class = |
| 247 | BT_TO_COMMON(stream_class->clock->clock_class); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | ret = bt_trace_common_add_stream_class(BT_TO_COMMON(trace), |
| 253 | BT_TO_COMMON(stream_class), |
| 254 | (bt_validation_flag_copy_field_type_func) bt_ctf_field_type_copy, |
| 255 | expected_clock_class, map_clock_classes_func, |
| 256 | false); |
| 257 | |
| 258 | end: |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace) |
| 263 | { |
| 264 | return bt_trace_common_get_stream_count(BT_TO_COMMON(trace)); |
| 265 | } |
| 266 | |
| 267 | struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index( |
| 268 | struct bt_ctf_trace *trace, uint64_t index) |
| 269 | { |
| 270 | return BT_FROM_COMMON(bt_trace_common_get_stream_by_index( |
| 271 | BT_TO_COMMON(trace), index)); |
| 272 | } |
| 273 | |
| 274 | int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
| 275 | { |
| 276 | return bt_trace_common_get_stream_class_count(BT_TO_COMMON(trace)); |
| 277 | } |
| 278 | |
| 279 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index( |
| 280 | struct bt_ctf_trace *trace, uint64_t index) |
| 281 | { |
| 282 | return BT_FROM_COMMON(bt_trace_common_get_stream_class_by_index( |
| 283 | BT_TO_COMMON(trace), index)); |
| 284 | } |
| 285 | |
| 286 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
| 287 | struct bt_ctf_trace *trace, uint64_t id) |
| 288 | { |
| 289 | return BT_FROM_COMMON( |
| 290 | bt_trace_common_get_stream_class_by_id( |
| 291 | BT_TO_COMMON(trace), id)); |
| 292 | } |
| 293 | |
| 294 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
| 295 | struct bt_ctf_trace *trace, const char *name) |
| 296 | { |
| 297 | return BT_FROM_COMMON( |
| 298 | bt_trace_common_get_clock_class_by_name(BT_TO_COMMON(trace), |
| 299 | name)); |
| 300 | } |
| 301 | |
| 302 | static |
| 303 | int append_trace_metadata(struct bt_ctf_trace *trace, |
| 304 | struct metadata_context *context) |
| 305 | { |
| 306 | unsigned char *uuid = trace->common.uuid; |
| 307 | int ret = 0; |
| 308 | |
| 309 | if (trace->common.native_byte_order == BT_BYTE_ORDER_NATIVE || |
| 310 | trace->common.native_byte_order == BT_BYTE_ORDER_UNSPECIFIED) { |
| 311 | BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; " |
| 312 | "set it with bt_ctf_trace_set_native_byte_order(): " |
| 313 | "addr=%p, name=\"%s\"", |
| 314 | trace, bt_ctf_trace_get_name(trace)); |
| 315 | ret = -1; |
| 316 | goto end; |
| 317 | } |
| 318 | |
| 319 | g_string_append(context->string, "trace {\n"); |
| 320 | g_string_append(context->string, "\tmajor = 1;\n"); |
| 321 | g_string_append(context->string, "\tminor = 8;\n"); |
| 322 | BT_ASSERT(trace->common.native_byte_order == BT_BYTE_ORDER_LITTLE_ENDIAN || |
| 323 | trace->common.native_byte_order == BT_BYTE_ORDER_BIG_ENDIAN || |
| 324 | trace->common.native_byte_order == BT_BYTE_ORDER_NETWORK); |
| 325 | |
| 326 | if (trace->common.uuid_set) { |
| 327 | g_string_append_printf(context->string, |
| 328 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", |
| 329 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 330 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 331 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 332 | uuid[12], uuid[13], uuid[14], uuid[15]); |
| 333 | } |
| 334 | |
| 335 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
| 336 | get_byte_order_string(trace->common.native_byte_order)); |
| 337 | |
| 338 | if (trace->common.packet_header_field_type) { |
| 339 | g_string_append(context->string, "\tpacket.header := "); |
| 340 | context->current_indentation_level++; |
| 341 | g_string_assign(context->field_name, ""); |
| 342 | BT_LOGD_STR("Serializing trace's packet header field type's metadata."); |
| 343 | ret = bt_ctf_field_type_serialize_recursive( |
| 344 | (void *) trace->common.packet_header_field_type, |
| 345 | context); |
| 346 | if (ret) { |
| 347 | goto end; |
| 348 | } |
| 349 | context->current_indentation_level--; |
| 350 | } |
| 351 | |
| 352 | g_string_append(context->string, ";\n};\n\n"); |
| 353 | end: |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static |
| 358 | void append_env_metadata(struct bt_ctf_trace *trace, |
| 359 | struct metadata_context *context) |
| 360 | { |
| 361 | int64_t i; |
| 362 | int64_t env_size; |
| 363 | |
| 364 | env_size = bt_attributes_get_count(trace->common.environment); |
| 365 | if (env_size <= 0) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | g_string_append(context->string, "env {\n"); |
| 370 | |
| 371 | for (i = 0; i < env_size; i++) { |
| 372 | struct bt_value *env_field_value_obj = NULL; |
| 373 | const char *entry_name; |
| 374 | |
| 375 | entry_name = bt_attributes_get_field_name( |
| 376 | trace->common.environment, i); |
| 377 | env_field_value_obj = bt_attributes_get_field_value( |
| 378 | trace->common.environment, i); |
| 379 | |
| 380 | BT_ASSERT(entry_name); |
| 381 | BT_ASSERT(env_field_value_obj); |
| 382 | |
| 383 | switch (bt_value_get_type(env_field_value_obj)) { |
| 384 | case BT_VALUE_TYPE_INTEGER: |
| 385 | { |
| 386 | int ret; |
| 387 | int64_t int_value; |
| 388 | |
| 389 | ret = bt_value_integer_get(env_field_value_obj, |
| 390 | &int_value); |
| 391 | BT_ASSERT(ret == 0); |
| 392 | g_string_append_printf(context->string, |
| 393 | "\t%s = %" PRId64 ";\n", entry_name, |
| 394 | int_value); |
| 395 | break; |
| 396 | } |
| 397 | case BT_VALUE_TYPE_STRING: |
| 398 | { |
| 399 | int ret; |
| 400 | const char *str_value; |
| 401 | char *escaped_str = NULL; |
| 402 | |
| 403 | ret = bt_value_string_get(env_field_value_obj, |
| 404 | &str_value); |
| 405 | BT_ASSERT(ret == 0); |
| 406 | escaped_str = g_strescape(str_value, NULL); |
| 407 | if (!escaped_str) { |
| 408 | BT_LOGE("Cannot escape string: string=\"%s\"", |
| 409 | str_value); |
| 410 | goto loop_next; |
| 411 | } |
| 412 | |
| 413 | g_string_append_printf(context->string, |
| 414 | "\t%s = \"%s\";\n", entry_name, escaped_str); |
| 415 | free(escaped_str); |
| 416 | break; |
| 417 | } |
| 418 | default: |
| 419 | goto loop_next; |
| 420 | } |
| 421 | |
| 422 | loop_next: |
| 423 | BT_PUT(env_field_value_obj); |
| 424 | } |
| 425 | |
| 426 | g_string_append(context->string, "};\n\n"); |
| 427 | } |
| 428 | |
| 429 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) |
| 430 | { |
| 431 | char *metadata = NULL; |
| 432 | struct metadata_context *context = NULL; |
| 433 | int err = 0; |
| 434 | size_t i; |
| 435 | |
| 436 | if (!trace) { |
| 437 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 438 | goto end; |
| 439 | } |
| 440 | |
| 441 | context = g_new0(struct metadata_context, 1); |
| 442 | if (!context) { |
| 443 | BT_LOGE_STR("Failed to allocate one metadata context."); |
| 444 | goto end; |
| 445 | } |
| 446 | |
| 447 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); |
| 448 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); |
| 449 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); |
| 450 | if (append_trace_metadata(trace, context)) { |
| 451 | /* append_trace_metadata() logs errors */ |
| 452 | goto error; |
| 453 | } |
| 454 | append_env_metadata(trace, context); |
| 455 | g_ptr_array_foreach(trace->common.clock_classes, |
| 456 | (GFunc) bt_ctf_clock_class_serialize, context); |
| 457 | |
| 458 | for (i = 0; i < trace->common.stream_classes->len; i++) { |
| 459 | /* bt_stream_class_serialize() logs details */ |
| 460 | err = bt_ctf_stream_class_serialize( |
| 461 | trace->common.stream_classes->pdata[i], context); |
| 462 | if (err) { |
| 463 | /* bt_stream_class_serialize() logs errors */ |
| 464 | goto error; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | metadata = context->string->str; |
| 469 | |
| 470 | error: |
| 471 | g_string_free(context->string, err ? TRUE : FALSE); |
| 472 | g_string_free(context->field_name, TRUE); |
| 473 | g_free(context); |
| 474 | |
| 475 | end: |
| 476 | return metadata; |
| 477 | } |
| 478 | |
| 479 | enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order( |
| 480 | struct bt_ctf_trace *trace) |
| 481 | { |
| 482 | return (int) bt_trace_common_get_native_byte_order(BT_TO_COMMON(trace)); |
| 483 | } |
| 484 | |
| 485 | int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace, |
| 486 | enum bt_ctf_byte_order byte_order) |
| 487 | { |
| 488 | return bt_trace_common_set_native_byte_order(BT_TO_COMMON(trace), |
| 489 | (int) byte_order, false); |
| 490 | } |
| 491 | |
| 492 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_field_type( |
| 493 | struct bt_ctf_trace *trace) |
| 494 | { |
| 495 | return BT_FROM_COMMON(bt_trace_common_get_packet_header_field_type( |
| 496 | BT_TO_COMMON(trace))); |
| 497 | } |
| 498 | |
| 499 | int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace *trace, |
| 500 | struct bt_ctf_field_type *packet_header_type) |
| 501 | { |
| 502 | return bt_trace_common_set_packet_header_field_type(BT_TO_COMMON(trace), |
| 503 | (void *) packet_header_type); |
| 504 | } |
| 505 | |
| 506 | const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace) |
| 507 | { |
| 508 | return bt_trace_common_get_name(BT_TO_COMMON(trace)); |
| 509 | } |