| 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 "logging.h" |
| 26 | |
| 27 | #include <inttypes.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include <babeltrace2-ctf-writer/event.h> |
| 34 | #include <babeltrace2-ctf-writer/object.h> |
| 35 | #include <babeltrace2-ctf-writer/utils.h> |
| 36 | #include <babeltrace2/types.h> |
| 37 | |
| 38 | #include "common/assert.h" |
| 39 | #include "compat/compiler.h" |
| 40 | #include "compat/endian.h" |
| 41 | |
| 42 | #include "attributes.h" |
| 43 | #include "clock-class.h" |
| 44 | #include "clock.h" |
| 45 | #include "event-class.h" |
| 46 | #include "event.h" |
| 47 | #include "field-types.h" |
| 48 | #include "field-wrapper.h" |
| 49 | #include "functor.h" |
| 50 | #include "stream-class.h" |
| 51 | #include "stream.h" |
| 52 | #include "trace.h" |
| 53 | #include "utils.h" |
| 54 | #include "validation.h" |
| 55 | #include "values.h" |
| 56 | #include "visitor.h" |
| 57 | #include "writer.h" |
| 58 | |
| 59 | #define DEFAULT_IDENTIFIER_SIZE 128 |
| 60 | #define DEFAULT_METADATA_STRING_SIZE 4096 |
| 61 | |
| 62 | BT_HIDDEN |
| 63 | int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace, |
| 64 | bt_ctf_object_release_func release_func) |
| 65 | { |
| 66 | int ret = 0; |
| 67 | |
| 68 | BT_LOGD_STR("Initializing common trace object."); |
| 69 | trace->native_byte_order = BT_CTF_BYTE_ORDER_UNSPECIFIED; |
| 70 | bt_ctf_object_init_shared_with_parent(&trace->base, release_func); |
| 71 | trace->clock_classes = g_ptr_array_new_with_free_func( |
| 72 | (GDestroyNotify) bt_ctf_object_put_ref); |
| 73 | if (!trace->clock_classes) { |
| 74 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 75 | goto error; |
| 76 | } |
| 77 | |
| 78 | trace->streams = g_ptr_array_new_with_free_func( |
| 79 | (GDestroyNotify) bt_ctf_object_try_spec_release); |
| 80 | if (!trace->streams) { |
| 81 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | trace->stream_classes = g_ptr_array_new_with_free_func( |
| 86 | (GDestroyNotify) bt_ctf_object_try_spec_release); |
| 87 | if (!trace->stream_classes) { |
| 88 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | /* Create the environment array object */ |
| 93 | trace->environment = bt_ctf_attributes_create(); |
| 94 | if (!trace->environment) { |
| 95 | BT_LOGE_STR("Cannot create empty attributes object."); |
| 96 | goto error; |
| 97 | } |
| 98 | |
| 99 | BT_LOGD("Initialized common trace object: addr=%p", trace); |
| 100 | goto end; |
| 101 | |
| 102 | error: |
| 103 | ret = -1; |
| 104 | |
| 105 | end: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | BT_HIDDEN |
| 110 | void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace) |
| 111 | { |
| 112 | BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"", |
| 113 | trace, bt_ctf_trace_common_get_name(trace)); |
| 114 | |
| 115 | if (trace->environment) { |
| 116 | BT_LOGD_STR("Destroying environment attributes."); |
| 117 | bt_ctf_attributes_destroy(trace->environment); |
| 118 | } |
| 119 | |
| 120 | if (trace->name) { |
| 121 | g_string_free(trace->name, TRUE); |
| 122 | } |
| 123 | |
| 124 | if (trace->clock_classes) { |
| 125 | BT_LOGD_STR("Putting clock classes."); |
| 126 | g_ptr_array_free(trace->clock_classes, TRUE); |
| 127 | } |
| 128 | |
| 129 | if (trace->streams) { |
| 130 | BT_LOGD_STR("Destroying streams."); |
| 131 | g_ptr_array_free(trace->streams, TRUE); |
| 132 | } |
| 133 | |
| 134 | if (trace->stream_classes) { |
| 135 | BT_LOGD_STR("Destroying stream classes."); |
| 136 | g_ptr_array_free(trace->stream_classes, TRUE); |
| 137 | } |
| 138 | |
| 139 | BT_LOGD_STR("Putting packet header field type."); |
| 140 | bt_ctf_object_put_ref(trace->packet_header_field_type); |
| 141 | } |
| 142 | |
| 143 | BT_HIDDEN |
| 144 | int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name) |
| 145 | { |
| 146 | int ret = 0; |
| 147 | |
| 148 | if (!trace) { |
| 149 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 150 | ret = -1; |
| 151 | goto end; |
| 152 | } |
| 153 | |
| 154 | if (!name) { |
| 155 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 156 | ret = -1; |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | if (trace->frozen) { |
| 161 | BT_LOGW("Invalid parameter: trace is frozen: " |
| 162 | "addr=%p, name=\"%s\"", |
| 163 | trace, bt_ctf_trace_common_get_name(trace)); |
| 164 | ret = -1; |
| 165 | goto end; |
| 166 | } |
| 167 | |
| 168 | trace->name = trace->name ? g_string_assign(trace->name, name) : |
| 169 | g_string_new(name); |
| 170 | if (!trace->name) { |
| 171 | BT_LOGE_STR("Failed to allocate one GString."); |
| 172 | ret = -1; |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | BT_LOGT("Set trace's name: addr=%p, name=\"%s\"", trace, name); |
| 177 | |
| 178 | end: |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | BT_HIDDEN |
| 183 | int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, |
| 184 | const uint8_t *uuid) |
| 185 | { |
| 186 | int ret = 0; |
| 187 | |
| 188 | if (!trace) { |
| 189 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 190 | ret = -1; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | if (!uuid) { |
| 195 | BT_LOGW_STR("Invalid parameter: UUID is NULL."); |
| 196 | ret = -1; |
| 197 | goto end; |
| 198 | } |
| 199 | |
| 200 | if (trace->frozen) { |
| 201 | BT_LOGW("Invalid parameter: trace is frozen: " |
| 202 | "addr=%p, name=\"%s\"", |
| 203 | trace, bt_ctf_trace_common_get_name(trace)); |
| 204 | ret = -1; |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | bt_uuid_copy(trace->uuid, uuid); |
| 209 | trace->uuid_set = BT_CTF_TRUE; |
| 210 | BT_LOGT("Set trace's UUID: addr=%p, name=\"%s\", " |
| 211 | "uuid=\"" BT_UUID_FMT "\"", |
| 212 | trace, bt_ctf_trace_common_get_name(trace), |
| 213 | BT_UUID_FMT_VALUES(uuid)); |
| 214 | |
| 215 | end: |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | BT_HIDDEN |
| 220 | int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace, |
| 221 | const char *name, struct bt_ctf_private_value *value) |
| 222 | { |
| 223 | int ret = 0; |
| 224 | |
| 225 | if (!trace) { |
| 226 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 227 | ret = -1; |
| 228 | goto end; |
| 229 | } |
| 230 | |
| 231 | if (!name) { |
| 232 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 233 | ret = -1; |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | if (!value) { |
| 238 | BT_LOGW_STR("Invalid parameter: value is NULL."); |
| 239 | ret = -1; |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | if (!bt_ctf_identifier_is_valid(name)) { |
| 244 | BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: " |
| 245 | "trace-addr=%p, trace-name=\"%s\", " |
| 246 | "env-name=\"%s\"", |
| 247 | trace, bt_ctf_trace_common_get_name(trace), name); |
| 248 | ret = -1; |
| 249 | goto end; |
| 250 | } |
| 251 | |
| 252 | if (!bt_ctf_value_is_integer(bt_ctf_private_value_as_value(value)) && |
| 253 | !bt_ctf_value_is_string(bt_ctf_private_value_as_value(value))) { |
| 254 | BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: " |
| 255 | "trace-addr=%p, trace-name=\"%s\", " |
| 256 | "env-name=\"%s\", env-value-type=%s", |
| 257 | trace, bt_ctf_trace_common_get_name(trace), name, |
| 258 | bt_ctf_value_type_string( |
| 259 | bt_ctf_value_get_type( |
| 260 | bt_ctf_private_value_as_value(value)))); |
| 261 | ret = -1; |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 265 | if (trace->frozen) { |
| 266 | /* |
| 267 | * New environment fields may be added to a frozen trace, |
| 268 | * but existing fields may not be changed. |
| 269 | * |
| 270 | * The object passed is frozen like all other attributes. |
| 271 | */ |
| 272 | struct bt_ctf_private_value *attribute = |
| 273 | bt_ctf_attributes_borrow_field_value_by_name( |
| 274 | trace->environment, name); |
| 275 | |
| 276 | if (attribute) { |
| 277 | BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: " |
| 278 | "trace-addr=%p, trace-name=\"%s\", " |
| 279 | "env-name=\"%s\"", |
| 280 | trace, bt_ctf_trace_common_get_name(trace), name); |
| 281 | ret = -1; |
| 282 | goto end; |
| 283 | } |
| 284 | |
| 285 | bt_ctf_value_freeze(bt_ctf_private_value_as_value(value)); |
| 286 | } |
| 287 | |
| 288 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, |
| 289 | value); |
| 290 | if (ret) { |
| 291 | BT_LOGE("Cannot set environment field's value: " |
| 292 | "trace-addr=%p, trace-name=\"%s\", " |
| 293 | "env-name=\"%s\"", |
| 294 | trace, bt_ctf_trace_common_get_name(trace), name); |
| 295 | } else { |
| 296 | BT_LOGT("Set environment field's value: " |
| 297 | "trace-addr=%p, trace-name=\"%s\", " |
| 298 | "env-name=\"%s\", value-addr=%p", |
| 299 | trace, bt_ctf_trace_common_get_name(trace), name, value); |
| 300 | } |
| 301 | |
| 302 | end: |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | BT_HIDDEN |
| 307 | int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace, |
| 308 | const char *name, const char *value) |
| 309 | { |
| 310 | int ret = 0; |
| 311 | struct bt_ctf_private_value *env_value_string_obj = NULL; |
| 312 | |
| 313 | if (!value) { |
| 314 | BT_LOGW_STR("Invalid parameter: value is NULL."); |
| 315 | ret = -1; |
| 316 | goto end; |
| 317 | } |
| 318 | |
| 319 | env_value_string_obj = bt_ctf_private_value_string_create_init(value); |
| 320 | if (!env_value_string_obj) { |
| 321 | BT_LOGE_STR("Cannot create string value object."); |
| 322 | ret = -1; |
| 323 | goto end; |
| 324 | } |
| 325 | |
| 326 | /* bt_ctf_trace_common_set_environment_field() logs errors */ |
| 327 | ret = bt_ctf_trace_common_set_environment_field(trace, name, |
| 328 | env_value_string_obj); |
| 329 | |
| 330 | end: |
| 331 | bt_ctf_object_put_ref(env_value_string_obj); |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | BT_HIDDEN |
| 336 | int bt_ctf_trace_common_set_environment_field_integer( |
| 337 | struct bt_ctf_trace_common *trace, const char *name, int64_t value) |
| 338 | { |
| 339 | int ret = 0; |
| 340 | struct bt_ctf_private_value *env_value_integer_obj = NULL; |
| 341 | |
| 342 | env_value_integer_obj = bt_ctf_private_value_integer_create_init(value); |
| 343 | if (!env_value_integer_obj) { |
| 344 | BT_LOGE_STR("Cannot create integer value object."); |
| 345 | ret = -1; |
| 346 | goto end; |
| 347 | } |
| 348 | |
| 349 | /* bt_ctf_trace_common_set_environment_field() logs errors */ |
| 350 | ret = bt_ctf_trace_common_set_environment_field(trace, name, |
| 351 | env_value_integer_obj); |
| 352 | |
| 353 | end: |
| 354 | bt_ctf_object_put_ref(env_value_integer_obj); |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | BT_HIDDEN |
| 359 | int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace, |
| 360 | struct bt_ctf_clock_class *clock_class) |
| 361 | { |
| 362 | int ret = 0; |
| 363 | |
| 364 | if (!trace) { |
| 365 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 366 | ret = -1; |
| 367 | goto end; |
| 368 | } |
| 369 | |
| 370 | if (!bt_ctf_clock_class_is_valid(clock_class)) { |
| 371 | BT_LOGW("Invalid parameter: clock class is invalid: " |
| 372 | "trace-addr=%p, trace-name=\"%s\", " |
| 373 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 374 | trace, bt_ctf_trace_common_get_name(trace), |
| 375 | clock_class, bt_ctf_clock_class_get_name(clock_class)); |
| 376 | ret = -1; |
| 377 | goto end; |
| 378 | } |
| 379 | |
| 380 | /* Check for duplicate clock classes */ |
| 381 | if (bt_ctf_trace_common_has_clock_class(trace, clock_class)) { |
| 382 | BT_LOGW("Invalid parameter: clock class already exists in trace: " |
| 383 | "trace-addr=%p, trace-name=\"%s\", " |
| 384 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 385 | trace, bt_ctf_trace_common_get_name(trace), |
| 386 | clock_class, bt_ctf_clock_class_get_name(clock_class)); |
| 387 | ret = -1; |
| 388 | goto end; |
| 389 | } |
| 390 | |
| 391 | bt_ctf_object_get_ref(clock_class); |
| 392 | g_ptr_array_add(trace->clock_classes, clock_class); |
| 393 | |
| 394 | if (trace->frozen) { |
| 395 | BT_LOGT_STR("Freezing added clock class because trace is frozen."); |
| 396 | bt_ctf_clock_class_freeze(clock_class); |
| 397 | } |
| 398 | |
| 399 | BT_LOGT("Added clock class to trace: " |
| 400 | "trace-addr=%p, trace-name=\"%s\", " |
| 401 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 402 | trace, bt_ctf_trace_common_get_name(trace), |
| 403 | clock_class, bt_ctf_clock_class_get_name(clock_class)); |
| 404 | |
| 405 | end: |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | static |
| 410 | bool packet_header_field_type_is_valid(struct bt_ctf_trace_common *trace, |
| 411 | struct bt_ctf_field_type_common *packet_header_type) |
| 412 | { |
| 413 | int ret; |
| 414 | bool is_valid = true; |
| 415 | struct bt_ctf_field_type_common *field_type = NULL; |
| 416 | |
| 417 | if (!packet_header_type) { |
| 418 | /* |
| 419 | * No packet header field type: trace must have only |
| 420 | * one stream. At this point the stream class being |
| 421 | * added is not part of the trace yet, so we validate |
| 422 | * that the trace contains no stream classes yet. |
| 423 | */ |
| 424 | if (trace->stream_classes->len >= 1) { |
| 425 | BT_LOGW_STR("Invalid packet header field type: " |
| 426 | "packet header field type does not exist but there's more than one stream class in the trace."); |
| 427 | goto invalid; |
| 428 | } |
| 429 | |
| 430 | /* No packet header field type: valid at this point */ |
| 431 | goto end; |
| 432 | } |
| 433 | |
| 434 | /* Packet header field type, if it exists, must be a structure */ |
| 435 | if (packet_header_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 436 | BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: " |
| 437 | "ft-addr=%p, ft-id=%s", |
| 438 | packet_header_type, |
| 439 | bt_ctf_field_type_id_string(packet_header_type->id)); |
| 440 | goto invalid; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * If there's a `magic` field, it must be a 32-bit unsigned |
| 445 | * integer field type. Also it must be the first field of the |
| 446 | * packet header field type. |
| 447 | */ |
| 448 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 449 | packet_header_type, "magic"); |
| 450 | if (field_type) { |
| 451 | const char *field_name; |
| 452 | |
| 453 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 454 | BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: " |
| 455 | "magic-ft-addr=%p, magic-ft-id=%s", |
| 456 | field_type, |
| 457 | bt_ctf_field_type_id_string(field_type->id)); |
| 458 | goto invalid; |
| 459 | } |
| 460 | |
| 461 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 462 | BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: " |
| 463 | "magic-ft-addr=%p", field_type); |
| 464 | goto invalid; |
| 465 | } |
| 466 | |
| 467 | if (bt_ctf_field_type_common_integer_get_size(field_type) != 32) { |
| 468 | BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: " |
| 469 | "magic-ft-addr=%p, magic-ft-size=%u", |
| 470 | field_type, |
| 471 | bt_ctf_field_type_common_integer_get_size(field_type)); |
| 472 | goto invalid; |
| 473 | } |
| 474 | |
| 475 | ret = bt_ctf_field_type_common_structure_borrow_field_by_index( |
| 476 | packet_header_type, &field_name, NULL, 0); |
| 477 | BT_ASSERT_DBG(ret == 0); |
| 478 | |
| 479 | if (strcmp(field_name, "magic") != 0) { |
| 480 | BT_LOGW("Invalid packet header field type: `magic` field must be the first field: " |
| 481 | "magic-ft-addr=%p, first-field-name=\"%s\"", |
| 482 | field_type, field_name); |
| 483 | goto invalid; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * If there's a `uuid` field, it must be an array field type of |
| 489 | * length 16 with an 8-bit unsigned integer element field type. |
| 490 | */ |
| 491 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 492 | packet_header_type, "uuid"); |
| 493 | if (field_type) { |
| 494 | struct bt_ctf_field_type_common *elem_ft; |
| 495 | |
| 496 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) { |
| 497 | BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: " |
| 498 | "uuid-ft-addr=%p, uuid-ft-id=%s", |
| 499 | field_type, |
| 500 | bt_ctf_field_type_id_string(field_type->id)); |
| 501 | goto invalid; |
| 502 | } |
| 503 | |
| 504 | if (bt_ctf_field_type_common_array_get_length(field_type) != 16) { |
| 505 | BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: " |
| 506 | "uuid-ft-addr=%p, uuid-ft-length=%" PRId64, |
| 507 | field_type, |
| 508 | bt_ctf_field_type_common_array_get_length(field_type)); |
| 509 | goto invalid; |
| 510 | } |
| 511 | |
| 512 | elem_ft = bt_ctf_field_type_common_array_borrow_element_field_type(field_type); |
| 513 | BT_ASSERT_DBG(elem_ft); |
| 514 | |
| 515 | if (elem_ft->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 516 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: " |
| 517 | "elem-ft-addr=%p, elem-ft-id=%s", |
| 518 | elem_ft, |
| 519 | bt_ctf_field_type_id_string(elem_ft->id)); |
| 520 | goto invalid; |
| 521 | } |
| 522 | |
| 523 | if (bt_ctf_field_type_common_integer_is_signed(elem_ft)) { |
| 524 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: " |
| 525 | "elem-ft-addr=%p", elem_ft); |
| 526 | goto invalid; |
| 527 | } |
| 528 | |
| 529 | if (bt_ctf_field_type_common_integer_get_size(elem_ft) != 8) { |
| 530 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: " |
| 531 | "elem-ft-addr=%p, elem-ft-size=%u", |
| 532 | elem_ft, |
| 533 | bt_ctf_field_type_common_integer_get_size(elem_ft)); |
| 534 | goto invalid; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * The `stream_id` field must exist if there's more than one |
| 540 | * stream classes in the trace. |
| 541 | */ |
| 542 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 543 | packet_header_type, "stream_id"); |
| 544 | |
| 545 | if (!field_type && trace->stream_classes->len >= 1) { |
| 546 | BT_LOGW_STR("Invalid packet header field type: " |
| 547 | "`stream_id` field does not exist but there's more than one stream class in the trace."); |
| 548 | goto invalid; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * If there's a `stream_id` field, it must be an unsigned |
| 553 | * integer field type. |
| 554 | */ |
| 555 | if (field_type) { |
| 556 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 557 | BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: " |
| 558 | "stream-id-ft-addr=%p, stream-id-ft-id=%s", |
| 559 | field_type, |
| 560 | bt_ctf_field_type_id_string(field_type->id)); |
| 561 | goto invalid; |
| 562 | } |
| 563 | |
| 564 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 565 | BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: " |
| 566 | "stream-id-ft-addr=%p", field_type); |
| 567 | goto invalid; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * If there's a `packet_seq_num` field, it must be an unsigned |
| 573 | * integer field type. |
| 574 | */ |
| 575 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 576 | packet_header_type, "packet_seq_num"); |
| 577 | if (field_type) { |
| 578 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 579 | BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: " |
| 580 | "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s", |
| 581 | field_type, |
| 582 | bt_ctf_field_type_id_string(field_type->id)); |
| 583 | goto invalid; |
| 584 | } |
| 585 | |
| 586 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 587 | BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: " |
| 588 | "packet-seq-num-ft-addr=%p", field_type); |
| 589 | goto invalid; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | goto end; |
| 594 | |
| 595 | invalid: |
| 596 | is_valid = false; |
| 597 | |
| 598 | end: |
| 599 | return is_valid; |
| 600 | } |
| 601 | |
| 602 | static |
| 603 | bool packet_context_field_type_is_valid(struct bt_ctf_trace_common *trace, |
| 604 | struct bt_ctf_stream_class_common *stream_class, |
| 605 | struct bt_ctf_field_type_common *packet_context_type, |
| 606 | bool check_ts_begin_end_mapped) |
| 607 | { |
| 608 | bool is_valid = true; |
| 609 | struct bt_ctf_field_type_common *field_type = NULL; |
| 610 | |
| 611 | if (!packet_context_type) { |
| 612 | /* No packet context field type: valid at this point */ |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | /* Packet context field type, if it exists, must be a structure */ |
| 617 | if (packet_context_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 618 | BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: " |
| 619 | "ft-addr=%p, ft-id=%s", |
| 620 | packet_context_type, |
| 621 | bt_ctf_field_type_id_string(packet_context_type->id)); |
| 622 | goto invalid; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * If there's a `packet_size` field, it must be an unsigned |
| 627 | * integer field type. |
| 628 | */ |
| 629 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 630 | packet_context_type, "packet_size"); |
| 631 | if (field_type) { |
| 632 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 633 | BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: " |
| 634 | "packet-size-ft-addr=%p, packet-size-ft-id=%s", |
| 635 | field_type, |
| 636 | bt_ctf_field_type_id_string(field_type->id)); |
| 637 | goto invalid; |
| 638 | } |
| 639 | |
| 640 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 641 | BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: " |
| 642 | "packet-size-ft-addr=%p", field_type); |
| 643 | goto invalid; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * If there's a `content_size` field, it must be an unsigned |
| 649 | * integer field type. |
| 650 | */ |
| 651 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 652 | packet_context_type, "content_size"); |
| 653 | if (field_type) { |
| 654 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 655 | BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: " |
| 656 | "content-size-ft-addr=%p, content-size-ft-id=%s", |
| 657 | field_type, |
| 658 | bt_ctf_field_type_id_string(field_type->id)); |
| 659 | goto invalid; |
| 660 | } |
| 661 | |
| 662 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 663 | BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: " |
| 664 | "content-size-ft-addr=%p", field_type); |
| 665 | goto invalid; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * If there's a `events_discarded` field, it must be an unsigned |
| 671 | * integer field type. |
| 672 | */ |
| 673 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 674 | packet_context_type, "events_discarded"); |
| 675 | if (field_type) { |
| 676 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 677 | BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: " |
| 678 | "events-discarded-ft-addr=%p, events-discarded-ft-id=%s", |
| 679 | field_type, |
| 680 | bt_ctf_field_type_id_string(field_type->id)); |
| 681 | goto invalid; |
| 682 | } |
| 683 | |
| 684 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 685 | BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: " |
| 686 | "events-discarded-ft-addr=%p", field_type); |
| 687 | goto invalid; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * If there's a `timestamp_begin` field, it must be an unsigned |
| 693 | * integer field type. Also, if the trace is not a CTF writer's |
| 694 | * trace, then we cannot automatically set the mapped clock |
| 695 | * class of this field, so it must have a mapped clock class. |
| 696 | */ |
| 697 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 698 | packet_context_type, "timestamp_begin"); |
| 699 | if (field_type) { |
| 700 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 701 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: " |
| 702 | "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s", |
| 703 | field_type, |
| 704 | bt_ctf_field_type_id_string(field_type->id)); |
| 705 | goto invalid; |
| 706 | } |
| 707 | |
| 708 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 709 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: " |
| 710 | "timestamp-begin-ft-addr=%p", field_type); |
| 711 | goto invalid; |
| 712 | } |
| 713 | |
| 714 | if (check_ts_begin_end_mapped) { |
| 715 | struct bt_ctf_clock_class *clock_class = |
| 716 | bt_ctf_field_type_common_integer_borrow_mapped_clock_class( |
| 717 | field_type); |
| 718 | |
| 719 | if (!clock_class) { |
| 720 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: " |
| 721 | "timestamp-begin-ft-addr=%p", field_type); |
| 722 | goto invalid; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * If there's a `timestamp_end` field, it must be an unsigned |
| 729 | * integer field type. Also, if the trace is not a CTF writer's |
| 730 | * trace, then we cannot automatically set the mapped clock |
| 731 | * class of this field, so it must have a mapped clock class. |
| 732 | */ |
| 733 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 734 | packet_context_type, "timestamp_end"); |
| 735 | if (field_type) { |
| 736 | if (field_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 737 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: " |
| 738 | "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s", |
| 739 | field_type, |
| 740 | bt_ctf_field_type_id_string(field_type->id)); |
| 741 | goto invalid; |
| 742 | } |
| 743 | |
| 744 | if (bt_ctf_field_type_common_integer_is_signed(field_type)) { |
| 745 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: " |
| 746 | "timestamp-end-ft-addr=%p", field_type); |
| 747 | goto invalid; |
| 748 | } |
| 749 | |
| 750 | if (check_ts_begin_end_mapped) { |
| 751 | struct bt_ctf_clock_class *clock_class = |
| 752 | bt_ctf_field_type_common_integer_borrow_mapped_clock_class( |
| 753 | field_type); |
| 754 | |
| 755 | if (!clock_class) { |
| 756 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: " |
| 757 | "timestamp-end-ft-addr=%p", field_type); |
| 758 | goto invalid; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | goto end; |
| 764 | |
| 765 | invalid: |
| 766 | is_valid = false; |
| 767 | |
| 768 | end: |
| 769 | return is_valid; |
| 770 | } |
| 771 | |
| 772 | static |
| 773 | bool event_header_field_type_is_valid(struct bt_ctf_trace_common *trace, |
| 774 | struct bt_ctf_stream_class_common *stream_class, |
| 775 | struct bt_ctf_field_type_common *event_header_type) |
| 776 | { |
| 777 | bool is_valid = true; |
| 778 | struct bt_ctf_field_type_common *field_type = NULL; |
| 779 | |
| 780 | /* |
| 781 | * We do not validate that the `timestamp` field exists here |
| 782 | * because CTF does not require this exact name to be mapped to |
| 783 | * a clock class. |
| 784 | */ |
| 785 | |
| 786 | if (!event_header_type) { |
| 787 | /* |
| 788 | * No event header field type: stream class must have |
| 789 | * only one event class. |
| 790 | */ |
| 791 | if (bt_ctf_stream_class_common_get_event_class_count(stream_class) > 1) { |
| 792 | BT_LOGW_STR("Invalid event header field type: " |
| 793 | "event header field type does not exist but there's more than one event class in the stream class."); |
| 794 | goto invalid; |
| 795 | } |
| 796 | |
| 797 | /* No event header field type: valid at this point */ |
| 798 | goto end; |
| 799 | } |
| 800 | |
| 801 | /* Event header field type, if it exists, must be a structure */ |
| 802 | if (event_header_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 803 | BT_LOGW("Invalid event header field type: must be a structure field type if it exists: " |
| 804 | "ft-addr=%p, ft-id=%s", |
| 805 | event_header_type, |
| 806 | bt_ctf_field_type_id_string(event_header_type->id)); |
| 807 | goto invalid; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * If there's an `id` field, it must be an unsigned integer |
| 812 | * field type or an enumeration field type with an unsigned |
| 813 | * integer container field type. |
| 814 | */ |
| 815 | field_type = bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
| 816 | event_header_type, "id"); |
| 817 | if (field_type) { |
| 818 | struct bt_ctf_field_type_common *int_ft; |
| 819 | |
| 820 | if (field_type->id == BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 821 | int_ft = field_type; |
| 822 | } else if (field_type->id == BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 823 | int_ft = bt_ctf_field_type_common_enumeration_borrow_container_field_type( |
| 824 | field_type); |
| 825 | } else { |
| 826 | BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: " |
| 827 | "id-ft-addr=%p, id-ft-id=%s", |
| 828 | field_type, |
| 829 | bt_ctf_field_type_id_string(field_type->id)); |
| 830 | goto invalid; |
| 831 | } |
| 832 | |
| 833 | BT_ASSERT_DBG(int_ft); |
| 834 | if (bt_ctf_field_type_common_integer_is_signed(int_ft)) { |
| 835 | BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: " |
| 836 | "id-ft-addr=%p", int_ft); |
| 837 | goto invalid; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | goto end; |
| 842 | |
| 843 | invalid: |
| 844 | is_valid = false; |
| 845 | |
| 846 | end: |
| 847 | return is_valid; |
| 848 | } |
| 849 | |
| 850 | static |
| 851 | int check_packet_header_type_has_no_clock_class(struct bt_ctf_trace_common *trace) |
| 852 | { |
| 853 | int ret = 0; |
| 854 | |
| 855 | if (trace->packet_header_field_type) { |
| 856 | struct bt_ctf_clock_class *clock_class = NULL; |
| 857 | |
| 858 | ret = bt_ctf_field_type_common_validate_single_clock_class( |
| 859 | trace->packet_header_field_type, |
| 860 | &clock_class); |
| 861 | bt_ctf_object_put_ref(clock_class); |
| 862 | if (ret || clock_class) { |
| 863 | BT_LOGW("Trace's packet header field type cannot " |
| 864 | "contain a field type which is mapped to " |
| 865 | "a clock class: " |
| 866 | "trace-addr=%p, trace-name=\"%s\", " |
| 867 | "clock-class-name=\"%s\"", |
| 868 | trace, bt_ctf_trace_common_get_name(trace), |
| 869 | clock_class ? |
| 870 | bt_ctf_clock_class_get_name(clock_class) : |
| 871 | NULL); |
| 872 | ret = -1; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | BT_HIDDEN |
| 880 | int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common *trace, |
| 881 | struct bt_ctf_stream_class_common *stream_class, |
| 882 | bt_ctf_validation_flag_copy_field_type_func copy_field_type_func, |
| 883 | struct bt_ctf_clock_class *init_expected_clock_class, |
| 884 | int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class, |
| 885 | struct bt_ctf_field_type_common *packet_context_field_type, |
| 886 | struct bt_ctf_field_type_common *event_header_field_type), |
| 887 | bool check_ts_begin_end_mapped) |
| 888 | { |
| 889 | int ret; |
| 890 | int64_t i; |
| 891 | int64_t stream_id; |
| 892 | struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; |
| 893 | struct bt_ctf_validation_output *ec_validation_outputs = NULL; |
| 894 | const enum bt_ctf_validation_flag trace_sc_validation_flags = |
| 895 | BT_CTF_VALIDATION_FLAG_TRACE | |
| 896 | BT_CTF_VALIDATION_FLAG_STREAM; |
| 897 | const enum bt_ctf_validation_flag ec_validation_flags = |
| 898 | BT_CTF_VALIDATION_FLAG_EVENT; |
| 899 | struct bt_ctf_field_type_common *packet_header_type = NULL; |
| 900 | struct bt_ctf_field_type_common *packet_context_type = NULL; |
| 901 | struct bt_ctf_field_type_common *event_header_type = NULL; |
| 902 | struct bt_ctf_field_type_common *stream_event_ctx_type = NULL; |
| 903 | int64_t event_class_count = 0; |
| 904 | struct bt_ctf_trace_common *current_parent_trace = NULL; |
| 905 | struct bt_ctf_clock_class *expected_clock_class = |
| 906 | bt_ctf_object_get_ref(init_expected_clock_class); |
| 907 | |
| 908 | BT_ASSERT_DBG(copy_field_type_func); |
| 909 | |
| 910 | if (!trace) { |
| 911 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 912 | ret = -1; |
| 913 | goto end; |
| 914 | } |
| 915 | |
| 916 | if (!stream_class) { |
| 917 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 918 | ret = -1; |
| 919 | goto end; |
| 920 | } |
| 921 | |
| 922 | BT_LOGD("Adding stream class to trace: " |
| 923 | "trace-addr=%p, trace-name=\"%s\", " |
| 924 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 925 | "stream-class-id=%" PRId64, |
| 926 | trace, bt_ctf_trace_common_get_name(trace), |
| 927 | stream_class, bt_ctf_stream_class_common_get_name(stream_class), |
| 928 | bt_ctf_stream_class_common_get_id(stream_class)); |
| 929 | |
| 930 | current_parent_trace = bt_ctf_stream_class_common_borrow_trace(stream_class); |
| 931 | if (current_parent_trace) { |
| 932 | /* Stream class is already associated to a trace, abort. */ |
| 933 | BT_LOGW("Invalid parameter: stream class is already part of a trace: " |
| 934 | "stream-class-trace-addr=%p, " |
| 935 | "stream-class-trace-name=\"%s\"", |
| 936 | current_parent_trace, |
| 937 | bt_ctf_trace_common_get_name(current_parent_trace)); |
| 938 | ret = -1; |
| 939 | goto end; |
| 940 | } |
| 941 | |
| 942 | event_class_count = |
| 943 | bt_ctf_stream_class_common_get_event_class_count(stream_class); |
| 944 | BT_ASSERT_DBG(event_class_count >= 0); |
| 945 | |
| 946 | if (!stream_class->frozen) { |
| 947 | /* |
| 948 | * Stream class is not frozen yet. Validate that the |
| 949 | * stream class contains at most a single clock class |
| 950 | * because the previous |
| 951 | * bt_ctf_stream_class_common_add_event_class() calls did |
| 952 | * not make this validation since the stream class's |
| 953 | * direct field types (packet context, event header, |
| 954 | * event context) could change afterwards. This stream |
| 955 | * class is about to be frozen and those field types |
| 956 | * won't be changed if this function succeeds. |
| 957 | * |
| 958 | * At this point we're also sure that the stream class's |
| 959 | * clock, if any, has the same class as the stream |
| 960 | * class's expected clock class, if any. This is why, if |
| 961 | * bt_ctf_stream_class_common_validate_single_clock_class() |
| 962 | * succeeds below, the call to |
| 963 | * bt_ctf_stream_class_map_clock_class() at the end of this |
| 964 | * function is safe because it maps to the same, single |
| 965 | * clock class. |
| 966 | */ |
| 967 | ret = bt_ctf_stream_class_common_validate_single_clock_class( |
| 968 | stream_class, &expected_clock_class); |
| 969 | if (ret) { |
| 970 | BT_LOGW("Invalid parameter: stream class or one of its " |
| 971 | "event classes contains a field type which is " |
| 972 | "not recursively mapped to the expected " |
| 973 | "clock class: " |
| 974 | "stream-class-addr=%p, " |
| 975 | "stream-class-id=%" PRId64 ", " |
| 976 | "stream-class-name=\"%s\", " |
| 977 | "expected-clock-class-addr=%p, " |
| 978 | "expected-clock-class-name=\"%s\"", |
| 979 | stream_class, bt_ctf_stream_class_common_get_id(stream_class), |
| 980 | bt_ctf_stream_class_common_get_name(stream_class), |
| 981 | expected_clock_class, |
| 982 | expected_clock_class ? |
| 983 | bt_ctf_clock_class_get_name(expected_clock_class) : |
| 984 | NULL); |
| 985 | goto end; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | ret = check_packet_header_type_has_no_clock_class(trace); |
| 990 | if (ret) { |
| 991 | /* check_packet_header_type_has_no_clock_class() logs errors */ |
| 992 | goto end; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * We're about to freeze both the trace and the stream class. |
| 997 | * Also, each event class contained in this stream class are |
| 998 | * already frozen. |
| 999 | * |
| 1000 | * This trace, this stream class, and all its event classes |
| 1001 | * should be valid at this point. |
| 1002 | * |
| 1003 | * Validate trace and stream class first, then each event |
| 1004 | * class of this stream class can be validated individually. |
| 1005 | */ |
| 1006 | packet_header_type = |
| 1007 | bt_ctf_trace_common_borrow_packet_header_field_type(trace); |
| 1008 | packet_context_type = |
| 1009 | bt_ctf_stream_class_common_borrow_packet_context_field_type(stream_class); |
| 1010 | event_header_type = |
| 1011 | bt_ctf_stream_class_common_borrow_event_header_field_type(stream_class); |
| 1012 | stream_event_ctx_type = |
| 1013 | bt_ctf_stream_class_common_borrow_event_context_field_type(stream_class); |
| 1014 | |
| 1015 | BT_LOGD("Validating trace and stream class field types."); |
| 1016 | ret = bt_ctf_validate_class_types(trace->environment, |
| 1017 | packet_header_type, packet_context_type, event_header_type, |
| 1018 | stream_event_ctx_type, NULL, NULL, trace->valid, |
| 1019 | stream_class->valid, 1, &trace_sc_validation_output, |
| 1020 | trace_sc_validation_flags, copy_field_type_func); |
| 1021 | |
| 1022 | if (ret) { |
| 1023 | /* |
| 1024 | * This means something went wrong during the validation |
| 1025 | * process, not that the objects are invalid. |
| 1026 | */ |
| 1027 | BT_LOGE("Failed to validate trace and stream class field types: " |
| 1028 | "ret=%d", ret); |
| 1029 | goto end; |
| 1030 | } |
| 1031 | |
| 1032 | if ((trace_sc_validation_output.valid_flags & |
| 1033 | trace_sc_validation_flags) != |
| 1034 | trace_sc_validation_flags) { |
| 1035 | /* Invalid trace/stream class */ |
| 1036 | BT_LOGW("Invalid trace or stream class field types: " |
| 1037 | "valid-flags=0x%x", |
| 1038 | trace_sc_validation_output.valid_flags); |
| 1039 | ret = -1; |
| 1040 | goto end; |
| 1041 | } |
| 1042 | |
| 1043 | if (event_class_count > 0) { |
| 1044 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, |
| 1045 | event_class_count); |
| 1046 | if (!ec_validation_outputs) { |
| 1047 | BT_LOGE_STR("Failed to allocate one validation output structure."); |
| 1048 | ret = -1; |
| 1049 | goto end; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /* Validate each event class individually */ |
| 1054 | for (i = 0; i < event_class_count; i++) { |
| 1055 | struct bt_ctf_event_class_common *event_class = |
| 1056 | bt_ctf_stream_class_common_borrow_event_class_by_index( |
| 1057 | stream_class, i); |
| 1058 | struct bt_ctf_field_type_common *event_context_type = NULL; |
| 1059 | struct bt_ctf_field_type_common *event_payload_type = NULL; |
| 1060 | |
| 1061 | event_context_type = |
| 1062 | bt_ctf_event_class_common_borrow_context_field_type( |
| 1063 | event_class); |
| 1064 | event_payload_type = |
| 1065 | bt_ctf_event_class_common_borrow_payload_field_type( |
| 1066 | event_class); |
| 1067 | |
| 1068 | /* |
| 1069 | * It is important to use the field types returned by |
| 1070 | * the previous trace and stream class validation here |
| 1071 | * because copies could have been made. |
| 1072 | */ |
| 1073 | BT_LOGD("Validating event class's field types: " |
| 1074 | "addr=%p, name=\"%s\", id=%" PRId64, |
| 1075 | event_class, bt_ctf_event_class_common_get_name(event_class), |
| 1076 | bt_ctf_event_class_common_get_id(event_class)); |
| 1077 | ret = bt_ctf_validate_class_types(trace->environment, |
| 1078 | trace_sc_validation_output.packet_header_type, |
| 1079 | trace_sc_validation_output.packet_context_type, |
| 1080 | trace_sc_validation_output.event_header_type, |
| 1081 | trace_sc_validation_output.stream_event_ctx_type, |
| 1082 | event_context_type, event_payload_type, |
| 1083 | 1, 1, event_class->valid, &ec_validation_outputs[i], |
| 1084 | ec_validation_flags, copy_field_type_func); |
| 1085 | |
| 1086 | if (ret) { |
| 1087 | BT_LOGE("Failed to validate event class field types: " |
| 1088 | "ret=%d", ret); |
| 1089 | goto end; |
| 1090 | } |
| 1091 | |
| 1092 | if ((ec_validation_outputs[i].valid_flags & |
| 1093 | ec_validation_flags) != ec_validation_flags) { |
| 1094 | /* Invalid event class */ |
| 1095 | BT_LOGW("Invalid event class field types: " |
| 1096 | "valid-flags=0x%x", |
| 1097 | ec_validation_outputs[i].valid_flags); |
| 1098 | ret = -1; |
| 1099 | goto end; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | stream_id = bt_ctf_stream_class_common_get_id(stream_class); |
| 1104 | if (stream_id < 0) { |
| 1105 | stream_id = trace->next_stream_id++; |
| 1106 | if (stream_id < 0) { |
| 1107 | BT_LOGE_STR("No more stream class IDs available."); |
| 1108 | ret = -1; |
| 1109 | goto end; |
| 1110 | } |
| 1111 | |
| 1112 | /* Try to assign a new stream id */ |
| 1113 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 1114 | if (stream_id == bt_ctf_stream_class_common_get_id( |
| 1115 | trace->stream_classes->pdata[i])) { |
| 1116 | /* Duplicate stream id found */ |
| 1117 | BT_LOGW("Duplicate stream class ID: " |
| 1118 | "id=%" PRId64, (int64_t) stream_id); |
| 1119 | ret = -1; |
| 1120 | goto end; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | if (bt_ctf_stream_class_common_set_id_no_check(stream_class, |
| 1125 | stream_id)) { |
| 1126 | /* TODO Should retry with a different stream id */ |
| 1127 | BT_LOGE("Cannot set stream class's ID: " |
| 1128 | "id=%" PRId64, (int64_t) stream_id); |
| 1129 | ret = -1; |
| 1130 | goto end; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * At this point all the field types in the validation output |
| 1136 | * are valid. Validate the semantics of some scopes according to |
| 1137 | * the CTF specification. |
| 1138 | */ |
| 1139 | if (!packet_header_field_type_is_valid(trace, |
| 1140 | trace_sc_validation_output.packet_header_type)) { |
| 1141 | BT_LOGW_STR("Invalid trace's packet header field type."); |
| 1142 | ret = -1; |
| 1143 | goto end; |
| 1144 | } |
| 1145 | |
| 1146 | if (!packet_context_field_type_is_valid(trace, |
| 1147 | stream_class, |
| 1148 | trace_sc_validation_output.packet_context_type, |
| 1149 | check_ts_begin_end_mapped)) { |
| 1150 | BT_LOGW_STR("Invalid stream class's packet context field type."); |
| 1151 | ret = -1; |
| 1152 | goto end; |
| 1153 | } |
| 1154 | |
| 1155 | if (!event_header_field_type_is_valid(trace, |
| 1156 | stream_class, |
| 1157 | trace_sc_validation_output.event_header_type)) { |
| 1158 | BT_LOGW_STR("Invalid steam class's event header field type."); |
| 1159 | ret = -1; |
| 1160 | goto end; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Now is the time to automatically map specific field types of |
| 1165 | * the stream class's packet context and event header field |
| 1166 | * types to the stream class's clock's class if they are not |
| 1167 | * mapped to a clock class yet. We do it here because we know |
| 1168 | * that after this point, everything is frozen so it won't be |
| 1169 | * possible for the user to modify the stream class's clock, or |
| 1170 | * to map those field types to other clock classes. |
| 1171 | */ |
| 1172 | if (map_clock_classes_func) { |
| 1173 | if (map_clock_classes_func(stream_class, |
| 1174 | trace_sc_validation_output.packet_context_type, |
| 1175 | trace_sc_validation_output.event_header_type)) { |
| 1176 | /* map_clock_classes_func() logs errors */ |
| 1177 | ret = -1; |
| 1178 | goto end; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | bt_ctf_object_set_parent(&stream_class->base, &trace->base); |
| 1183 | g_ptr_array_add(trace->stream_classes, stream_class); |
| 1184 | |
| 1185 | /* |
| 1186 | * At this point we know that the function will be successful. |
| 1187 | * Therefore we can replace the trace and stream class field |
| 1188 | * types with what's in their validation output structure and |
| 1189 | * mark them as valid. We can also replace the field types of |
| 1190 | * all the event classes of the stream class and mark them as |
| 1191 | * valid. |
| 1192 | */ |
| 1193 | bt_ctf_validation_replace_types(trace, stream_class, NULL, |
| 1194 | &trace_sc_validation_output, trace_sc_validation_flags); |
| 1195 | trace->valid = 1; |
| 1196 | stream_class->valid = 1; |
| 1197 | |
| 1198 | /* |
| 1199 | * Put what was not moved in bt_ctf_validation_replace_types(). |
| 1200 | */ |
| 1201 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); |
| 1202 | |
| 1203 | for (i = 0; i < event_class_count; i++) { |
| 1204 | struct bt_ctf_event_class_common *event_class = |
| 1205 | bt_ctf_stream_class_common_borrow_event_class_by_index( |
| 1206 | stream_class, i); |
| 1207 | |
| 1208 | bt_ctf_validation_replace_types(NULL, NULL, event_class, |
| 1209 | &ec_validation_outputs[i], ec_validation_flags); |
| 1210 | event_class->valid = 1; |
| 1211 | |
| 1212 | /* |
| 1213 | * Put what was not moved in |
| 1214 | * bt_ctf_validation_replace_types(). |
| 1215 | */ |
| 1216 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Freeze the trace and the stream class. |
| 1221 | */ |
| 1222 | bt_ctf_stream_class_common_freeze(stream_class); |
| 1223 | bt_ctf_trace_common_freeze(trace); |
| 1224 | |
| 1225 | /* |
| 1226 | * It is safe to set the stream class's unique clock class |
| 1227 | * now because the stream class is frozen. |
| 1228 | */ |
| 1229 | if (expected_clock_class) { |
| 1230 | BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class); |
| 1231 | } |
| 1232 | |
| 1233 | BT_LOGD("Added stream class to trace: " |
| 1234 | "trace-addr=%p, trace-name=\"%s\", " |
| 1235 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 1236 | "stream-class-id=%" PRId64, |
| 1237 | trace, bt_ctf_trace_common_get_name(trace), |
| 1238 | stream_class, bt_ctf_stream_class_common_get_name(stream_class), |
| 1239 | bt_ctf_stream_class_common_get_id(stream_class)); |
| 1240 | |
| 1241 | end: |
| 1242 | if (ret) { |
| 1243 | bt_ctf_object_set_parent(&stream_class->base, NULL); |
| 1244 | |
| 1245 | if (ec_validation_outputs) { |
| 1246 | for (i = 0; i < event_class_count; i++) { |
| 1247 | bt_ctf_validation_output_put_types( |
| 1248 | &ec_validation_outputs[i]); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | g_free(ec_validation_outputs); |
| 1254 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); |
| 1255 | bt_ctf_object_put_ref(expected_clock_class); |
| 1256 | return ret; |
| 1257 | } |
| 1258 | |
| 1259 | BT_HIDDEN |
| 1260 | bt_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace, |
| 1261 | struct bt_ctf_clock_class *clock_class) |
| 1262 | { |
| 1263 | struct bt_ctf_search_query query = { .value = clock_class, .found = 0 }; |
| 1264 | |
| 1265 | BT_ASSERT_DBG(trace); |
| 1266 | BT_ASSERT_DBG(clock_class); |
| 1267 | |
| 1268 | g_ptr_array_foreach(trace->clock_classes, value_exists, &query); |
| 1269 | return query.found; |
| 1270 | } |
| 1271 | |
| 1272 | BT_HIDDEN |
| 1273 | int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common *trace, |
| 1274 | enum bt_ctf_byte_order byte_order, bool allow_unspecified) |
| 1275 | { |
| 1276 | int ret = 0; |
| 1277 | |
| 1278 | if (!trace) { |
| 1279 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 1280 | ret = -1; |
| 1281 | goto end; |
| 1282 | } |
| 1283 | |
| 1284 | if (trace->frozen) { |
| 1285 | BT_LOGW("Invalid parameter: trace is frozen: " |
| 1286 | "addr=%p, name=\"%s\"", |
| 1287 | trace, bt_ctf_trace_common_get_name(trace)); |
| 1288 | ret = -1; |
| 1289 | goto end; |
| 1290 | } |
| 1291 | |
| 1292 | if (byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED && !allow_unspecified) { |
| 1293 | BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed: " |
| 1294 | "addr=%p, name=\"%s\"", |
| 1295 | trace, bt_ctf_trace_common_get_name(trace)); |
| 1296 | ret = -1; |
| 1297 | goto end; |
| 1298 | } |
| 1299 | |
| 1300 | if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN && |
| 1301 | byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN && |
| 1302 | byte_order != BT_CTF_BYTE_ORDER_NETWORK) { |
| 1303 | BT_LOGW("Invalid parameter: invalid byte order: " |
| 1304 | "addr=%p, name=\"%s\", bo=%s", |
| 1305 | trace, bt_ctf_trace_common_get_name(trace), |
| 1306 | bt_ctf_byte_order_string(byte_order)); |
| 1307 | ret = -1; |
| 1308 | goto end; |
| 1309 | } |
| 1310 | |
| 1311 | trace->native_byte_order = byte_order; |
| 1312 | BT_LOGT("Set trace's native byte order: " |
| 1313 | "addr=%p, name=\"%s\", bo=%s", |
| 1314 | trace, bt_ctf_trace_common_get_name(trace), |
| 1315 | bt_ctf_byte_order_string(byte_order)); |
| 1316 | |
| 1317 | end: |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | BT_HIDDEN |
| 1322 | int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common *trace, |
| 1323 | struct bt_ctf_field_type_common *packet_header_type) |
| 1324 | { |
| 1325 | int ret = 0; |
| 1326 | |
| 1327 | if (!trace) { |
| 1328 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 1329 | ret = -1; |
| 1330 | goto end; |
| 1331 | } |
| 1332 | |
| 1333 | if (trace->frozen) { |
| 1334 | BT_LOGW("Invalid parameter: trace is frozen: " |
| 1335 | "addr=%p, name=\"%s\"", |
| 1336 | trace, bt_ctf_trace_common_get_name(trace)); |
| 1337 | ret = -1; |
| 1338 | goto end; |
| 1339 | } |
| 1340 | |
| 1341 | /* packet_header_type must be a structure. */ |
| 1342 | if (packet_header_type && |
| 1343 | packet_header_type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 1344 | BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: " |
| 1345 | "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s", |
| 1346 | trace, bt_ctf_trace_common_get_name(trace), |
| 1347 | packet_header_type, |
| 1348 | bt_ctf_field_type_id_string(packet_header_type->id)); |
| 1349 | ret = -1; |
| 1350 | goto end; |
| 1351 | } |
| 1352 | |
| 1353 | bt_ctf_object_put_ref(trace->packet_header_field_type); |
| 1354 | trace->packet_header_field_type = bt_ctf_object_get_ref(packet_header_type); |
| 1355 | BT_LOGT("Set trace's packet header field type: " |
| 1356 | "addr=%p, name=\"%s\", packet-context-ft-addr=%p", |
| 1357 | trace, bt_ctf_trace_common_get_name(trace), packet_header_type); |
| 1358 | end: |
| 1359 | return ret; |
| 1360 | } |
| 1361 | |
| 1362 | static |
| 1363 | int64_t get_stream_class_count(void *element) |
| 1364 | { |
| 1365 | return bt_ctf_trace_get_stream_class_count( |
| 1366 | (struct bt_ctf_trace *) element); |
| 1367 | } |
| 1368 | |
| 1369 | static |
| 1370 | void *get_stream_class(void *element, int i) |
| 1371 | { |
| 1372 | return bt_ctf_trace_get_stream_class_by_index( |
| 1373 | (struct bt_ctf_trace *) element, i); |
| 1374 | } |
| 1375 | |
| 1376 | static |
| 1377 | int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data) |
| 1378 | { |
| 1379 | return bt_ctf_stream_class_visit(object, visitor, data); |
| 1380 | } |
| 1381 | |
| 1382 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, |
| 1383 | bt_ctf_visitor visitor, void *data) |
| 1384 | { |
| 1385 | int ret; |
| 1386 | struct bt_ctf_visitor_object obj = { |
| 1387 | .object = trace, |
| 1388 | .type = BT_CTF_VISITOR_OBJECT_TYPE_TRACE |
| 1389 | }; |
| 1390 | |
| 1391 | if (!trace) { |
| 1392 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 1393 | ret = -1; |
| 1394 | goto end; |
| 1395 | } |
| 1396 | |
| 1397 | if (!visitor) { |
| 1398 | BT_LOGW_STR("Invalid parameter: visitor is NULL."); |
| 1399 | ret = -1; |
| 1400 | goto end; |
| 1401 | } |
| 1402 | |
| 1403 | BT_LOGT("Visiting trace: addr=%p, name=\"%s\"", |
| 1404 | trace, bt_ctf_trace_get_name(trace)); |
| 1405 | ret = bt_ctf_visitor_helper(&obj, get_stream_class_count, |
| 1406 | get_stream_class, visit_stream_class, visitor, data); |
| 1407 | end: |
| 1408 | return ret; |
| 1409 | } |
| 1410 | |
| 1411 | static |
| 1412 | void bt_ctf_trace_destroy(struct bt_ctf_object *obj) |
| 1413 | { |
| 1414 | struct bt_ctf_trace *trace = (void *) obj; |
| 1415 | |
| 1416 | BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"", |
| 1417 | trace, bt_ctf_trace_get_name(trace)); |
| 1418 | bt_ctf_trace_common_finalize(BT_CTF_TO_COMMON(trace)); |
| 1419 | g_free(trace); |
| 1420 | } |
| 1421 | |
| 1422 | BT_HIDDEN |
| 1423 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
| 1424 | { |
| 1425 | struct bt_ctf_trace *trace = NULL; |
| 1426 | int ret; |
| 1427 | |
| 1428 | BT_LOGD_STR("Creating CTF writer trace object."); |
| 1429 | trace = g_new0(struct bt_ctf_trace, 1); |
| 1430 | if (!trace) { |
| 1431 | BT_LOGE_STR("Failed to allocate one CTF writer trace."); |
| 1432 | goto error; |
| 1433 | } |
| 1434 | |
| 1435 | ret = bt_ctf_trace_common_initialize(BT_CTF_TO_COMMON(trace), |
| 1436 | bt_ctf_trace_destroy); |
| 1437 | if (ret) { |
| 1438 | /* bt_ctf_trace_common_initialize() logs errors */ |
| 1439 | goto error; |
| 1440 | } |
| 1441 | |
| 1442 | BT_LOGD("Created CTF writer trace object: addr=%p", trace); |
| 1443 | return trace; |
| 1444 | |
| 1445 | error: |
| 1446 | BT_CTF_OBJECT_PUT_REF_AND_RESET(trace); |
| 1447 | return trace; |
| 1448 | } |
| 1449 | |
| 1450 | const uint8_t *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace) |
| 1451 | { |
| 1452 | return bt_ctf_trace_common_get_uuid(BT_CTF_TO_COMMON(trace)); |
| 1453 | } |
| 1454 | |
| 1455 | int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, |
| 1456 | const uint8_t *uuid) |
| 1457 | { |
| 1458 | return bt_ctf_trace_common_set_uuid(BT_CTF_TO_COMMON(trace), uuid); |
| 1459 | } |
| 1460 | |
| 1461 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
| 1462 | const char *name, const char *value) |
| 1463 | { |
| 1464 | return bt_ctf_trace_common_set_environment_field_string(BT_CTF_TO_COMMON(trace), |
| 1465 | name, value); |
| 1466 | } |
| 1467 | |
| 1468 | int bt_ctf_trace_set_environment_field_integer( |
| 1469 | struct bt_ctf_trace *trace, const char *name, int64_t value) |
| 1470 | { |
| 1471 | return bt_ctf_trace_common_set_environment_field_integer( |
| 1472 | BT_CTF_TO_COMMON(trace), name, value); |
| 1473 | } |
| 1474 | |
| 1475 | int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
| 1476 | { |
| 1477 | return bt_ctf_trace_common_get_environment_field_count(BT_CTF_TO_COMMON(trace)); |
| 1478 | } |
| 1479 | |
| 1480 | const char * |
| 1481 | bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace, |
| 1482 | uint64_t index) |
| 1483 | { |
| 1484 | return bt_ctf_trace_common_get_environment_field_name_by_index( |
| 1485 | BT_CTF_TO_COMMON(trace), index); |
| 1486 | } |
| 1487 | |
| 1488 | struct bt_ctf_value *bt_ctf_trace_get_environment_field_value_by_index( |
| 1489 | struct bt_ctf_trace *trace, uint64_t index) |
| 1490 | { |
| 1491 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_index( |
| 1492 | BT_CTF_TO_COMMON(trace), index)); |
| 1493 | } |
| 1494 | |
| 1495 | struct bt_ctf_value *bt_ctf_trace_get_environment_field_value_by_name( |
| 1496 | struct bt_ctf_trace *trace, const char *name) |
| 1497 | { |
| 1498 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_environment_field_value_by_name( |
| 1499 | BT_CTF_TO_COMMON(trace), name)); |
| 1500 | } |
| 1501 | |
| 1502 | BT_HIDDEN |
| 1503 | int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace, |
| 1504 | struct bt_ctf_clock_class *clock_class) |
| 1505 | { |
| 1506 | return bt_ctf_trace_common_add_clock_class(BT_CTF_TO_COMMON(trace), |
| 1507 | (void *) clock_class); |
| 1508 | } |
| 1509 | |
| 1510 | BT_HIDDEN |
| 1511 | int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) |
| 1512 | { |
| 1513 | return bt_ctf_trace_common_get_clock_class_count(BT_CTF_TO_COMMON(trace)); |
| 1514 | } |
| 1515 | |
| 1516 | BT_HIDDEN |
| 1517 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index( |
| 1518 | struct bt_ctf_trace *trace, uint64_t index) |
| 1519 | { |
| 1520 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_clock_class_by_index( |
| 1521 | BT_CTF_TO_COMMON(trace), index)); |
| 1522 | } |
| 1523 | |
| 1524 | static |
| 1525 | int map_clock_classes_func(struct bt_ctf_stream_class_common *stream_class, |
| 1526 | struct bt_ctf_field_type_common *packet_context_type, |
| 1527 | struct bt_ctf_field_type_common *event_header_type) |
| 1528 | { |
| 1529 | int ret = bt_ctf_stream_class_map_clock_class( |
| 1530 | BT_CTF_FROM_COMMON(stream_class), |
| 1531 | BT_CTF_FROM_COMMON(packet_context_type), |
| 1532 | BT_CTF_FROM_COMMON(event_header_type)); |
| 1533 | |
| 1534 | if (ret) { |
| 1535 | BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); |
| 1536 | } |
| 1537 | |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
| 1542 | struct bt_ctf_stream_class *stream_class) |
| 1543 | { |
| 1544 | int ret = 0; |
| 1545 | struct bt_ctf_clock_class *expected_clock_class = NULL; |
| 1546 | |
| 1547 | if (!trace) { |
| 1548 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 1549 | ret = -1; |
| 1550 | goto end; |
| 1551 | } |
| 1552 | |
| 1553 | if (!stream_class) { |
| 1554 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 1555 | ret = -1; |
| 1556 | goto end; |
| 1557 | } |
| 1558 | |
| 1559 | if (stream_class->clock) { |
| 1560 | struct bt_ctf_clock_class *stream_clock_class = |
| 1561 | stream_class->clock->clock_class; |
| 1562 | |
| 1563 | /* |
| 1564 | * Make sure this clock was also added to the |
| 1565 | * trace (potentially through its CTF writer |
| 1566 | * owner). |
| 1567 | */ |
| 1568 | size_t i; |
| 1569 | |
| 1570 | for (i = 0; i < trace->common.clock_classes->len; i++) { |
| 1571 | if (trace->common.clock_classes->pdata[i] == |
| 1572 | stream_clock_class) { |
| 1573 | /* Found! */ |
| 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | if (i == trace->common.clock_classes->len) { |
| 1579 | /* Not found */ |
| 1580 | BT_LOGW("Stream class's clock's class is not part of the trace: " |
| 1581 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 1582 | stream_clock_class, |
| 1583 | bt_ctf_clock_class_get_name(stream_clock_class)); |
| 1584 | ret = -1; |
| 1585 | goto end; |
| 1586 | } |
| 1587 | |
| 1588 | if (stream_class->common.clock_class && |
| 1589 | stream_class->common.clock_class != |
| 1590 | stream_class->clock->clock_class) { |
| 1591 | /* |
| 1592 | * Stream class already has an expected clock |
| 1593 | * class, but it does not match its clock's |
| 1594 | * class. |
| 1595 | */ |
| 1596 | BT_LOGW("Invalid parameter: stream class's clock's " |
| 1597 | "class does not match stream class's " |
| 1598 | "expected clock class: " |
| 1599 | "stream-class-addr=%p, " |
| 1600 | "stream-class-id=%" PRId64 ", " |
| 1601 | "stream-class-name=\"%s\", " |
| 1602 | "expected-clock-class-addr=%p, " |
| 1603 | "expected-clock-class-name=\"%s\"", |
| 1604 | stream_class, |
| 1605 | bt_ctf_stream_class_get_id(stream_class), |
| 1606 | bt_ctf_stream_class_get_name(stream_class), |
| 1607 | stream_class->common.clock_class, |
| 1608 | bt_ctf_clock_class_get_name(stream_class->common.clock_class)); |
| 1609 | } else if (!stream_class->common.clock_class) { |
| 1610 | /* |
| 1611 | * Set expected clock class to stream class's |
| 1612 | * clock's class. |
| 1613 | */ |
| 1614 | expected_clock_class = stream_class->clock->clock_class; |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | |
| 1619 | ret = bt_ctf_trace_common_add_stream_class(BT_CTF_TO_COMMON(trace), |
| 1620 | BT_CTF_TO_COMMON(stream_class), |
| 1621 | (bt_ctf_validation_flag_copy_field_type_func) bt_ctf_field_type_copy, |
| 1622 | expected_clock_class, map_clock_classes_func, |
| 1623 | false); |
| 1624 | |
| 1625 | end: |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
| 1629 | int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace) |
| 1630 | { |
| 1631 | return bt_ctf_trace_common_get_stream_count(BT_CTF_TO_COMMON(trace)); |
| 1632 | } |
| 1633 | |
| 1634 | struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index( |
| 1635 | struct bt_ctf_trace *trace, uint64_t index) |
| 1636 | { |
| 1637 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_by_index( |
| 1638 | BT_CTF_TO_COMMON(trace), index)); |
| 1639 | } |
| 1640 | |
| 1641 | int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
| 1642 | { |
| 1643 | return bt_ctf_trace_common_get_stream_class_count(BT_CTF_TO_COMMON(trace)); |
| 1644 | } |
| 1645 | |
| 1646 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index( |
| 1647 | struct bt_ctf_trace *trace, uint64_t index) |
| 1648 | { |
| 1649 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_index( |
| 1650 | BT_CTF_TO_COMMON(trace), index)); |
| 1651 | } |
| 1652 | |
| 1653 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
| 1654 | struct bt_ctf_trace *trace, uint64_t id) |
| 1655 | { |
| 1656 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_stream_class_by_id( |
| 1657 | BT_CTF_TO_COMMON(trace), id)); |
| 1658 | } |
| 1659 | |
| 1660 | BT_HIDDEN |
| 1661 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
| 1662 | struct bt_ctf_trace *trace, const char *name) |
| 1663 | { |
| 1664 | return bt_ctf_object_get_ref( |
| 1665 | bt_ctf_trace_common_borrow_clock_class_by_name(BT_CTF_TO_COMMON(trace), |
| 1666 | name)); |
| 1667 | } |
| 1668 | |
| 1669 | static |
| 1670 | int append_trace_metadata(struct bt_ctf_trace *trace, |
| 1671 | struct metadata_context *context) |
| 1672 | { |
| 1673 | uint8_t *uuid = trace->common.uuid; |
| 1674 | int ret = 0; |
| 1675 | |
| 1676 | if (trace->common.native_byte_order == BT_CTF_BYTE_ORDER_NATIVE || |
| 1677 | trace->common.native_byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) { |
| 1678 | BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; " |
| 1679 | "set it with bt_ctf_trace_set_native_byte_order(): " |
| 1680 | "addr=%p, name=\"%s\"", |
| 1681 | trace, bt_ctf_trace_get_name(trace)); |
| 1682 | ret = -1; |
| 1683 | goto end; |
| 1684 | } |
| 1685 | |
| 1686 | g_string_append(context->string, "trace {\n"); |
| 1687 | g_string_append(context->string, "\tmajor = 1;\n"); |
| 1688 | g_string_append(context->string, "\tminor = 8;\n"); |
| 1689 | BT_ASSERT_DBG(trace->common.native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || |
| 1690 | trace->common.native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN || |
| 1691 | trace->common.native_byte_order == BT_CTF_BYTE_ORDER_NETWORK); |
| 1692 | |
| 1693 | if (trace->common.uuid_set) { |
| 1694 | g_string_append_printf(context->string, |
| 1695 | "\tuuid = \"" BT_UUID_FMT "\";\n", |
| 1696 | BT_UUID_FMT_VALUES(uuid)); |
| 1697 | } |
| 1698 | |
| 1699 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
| 1700 | bt_ctf_get_byte_order_string(trace->common.native_byte_order)); |
| 1701 | |
| 1702 | if (trace->common.packet_header_field_type) { |
| 1703 | g_string_append(context->string, "\tpacket.header := "); |
| 1704 | context->current_indentation_level++; |
| 1705 | g_string_assign(context->field_name, ""); |
| 1706 | BT_LOGD_STR("Serializing trace's packet header field type's metadata."); |
| 1707 | ret = bt_ctf_field_type_serialize_recursive( |
| 1708 | (void *) trace->common.packet_header_field_type, |
| 1709 | context); |
| 1710 | if (ret) { |
| 1711 | goto end; |
| 1712 | } |
| 1713 | context->current_indentation_level--; |
| 1714 | } |
| 1715 | |
| 1716 | g_string_append(context->string, ";\n};\n\n"); |
| 1717 | end: |
| 1718 | return ret; |
| 1719 | } |
| 1720 | |
| 1721 | static |
| 1722 | void append_env_metadata(struct bt_ctf_trace *trace, |
| 1723 | struct metadata_context *context) |
| 1724 | { |
| 1725 | int64_t i; |
| 1726 | int64_t env_size; |
| 1727 | |
| 1728 | env_size = bt_ctf_attributes_get_count(trace->common.environment); |
| 1729 | if (env_size <= 0) { |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | g_string_append(context->string, "env {\n"); |
| 1734 | |
| 1735 | for (i = 0; i < env_size; i++) { |
| 1736 | struct bt_ctf_private_value *env_field_value_obj = NULL; |
| 1737 | const char *entry_name; |
| 1738 | |
| 1739 | entry_name = bt_ctf_attributes_get_field_name( |
| 1740 | trace->common.environment, i); |
| 1741 | env_field_value_obj = bt_ctf_attributes_borrow_field_value( |
| 1742 | trace->common.environment, i); |
| 1743 | |
| 1744 | BT_ASSERT_DBG(entry_name); |
| 1745 | BT_ASSERT_DBG(env_field_value_obj); |
| 1746 | |
| 1747 | switch (bt_ctf_value_get_type( |
| 1748 | bt_ctf_private_value_as_value(env_field_value_obj))) { |
| 1749 | case BT_CTF_VALUE_TYPE_INTEGER: |
| 1750 | { |
| 1751 | int64_t int_value; |
| 1752 | |
| 1753 | int_value = bt_ctf_value_integer_get( |
| 1754 | bt_ctf_private_value_as_value( |
| 1755 | env_field_value_obj)); |
| 1756 | g_string_append_printf(context->string, |
| 1757 | "\t%s = %" PRId64 ";\n", entry_name, |
| 1758 | int_value); |
| 1759 | break; |
| 1760 | } |
| 1761 | case BT_CTF_VALUE_TYPE_STRING: |
| 1762 | { |
| 1763 | const char *str_value; |
| 1764 | char *escaped_str = NULL; |
| 1765 | |
| 1766 | str_value = bt_ctf_value_string_get( |
| 1767 | bt_ctf_private_value_as_value( |
| 1768 | env_field_value_obj)); |
| 1769 | escaped_str = g_strescape(str_value, NULL); |
| 1770 | if (!escaped_str) { |
| 1771 | BT_LOGE("Cannot escape string: string=\"%s\"", |
| 1772 | str_value); |
| 1773 | continue; |
| 1774 | } |
| 1775 | |
| 1776 | g_string_append_printf(context->string, |
| 1777 | "\t%s = \"%s\";\n", entry_name, escaped_str); |
| 1778 | free(escaped_str); |
| 1779 | break; |
| 1780 | } |
| 1781 | default: |
| 1782 | continue; |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | g_string_append(context->string, "};\n\n"); |
| 1787 | } |
| 1788 | |
| 1789 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) |
| 1790 | { |
| 1791 | char *metadata = NULL; |
| 1792 | struct metadata_context *context = NULL; |
| 1793 | int err = 0; |
| 1794 | size_t i; |
| 1795 | |
| 1796 | if (!trace) { |
| 1797 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
| 1798 | goto end; |
| 1799 | } |
| 1800 | |
| 1801 | context = g_new0(struct metadata_context, 1); |
| 1802 | if (!context) { |
| 1803 | BT_LOGE_STR("Failed to allocate one metadata context."); |
| 1804 | goto end; |
| 1805 | } |
| 1806 | |
| 1807 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); |
| 1808 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); |
| 1809 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); |
| 1810 | if (append_trace_metadata(trace, context)) { |
| 1811 | /* append_trace_metadata() logs errors */ |
| 1812 | goto error; |
| 1813 | } |
| 1814 | append_env_metadata(trace, context); |
| 1815 | g_ptr_array_foreach(trace->common.clock_classes, |
| 1816 | (GFunc) bt_ctf_clock_class_serialize, context); |
| 1817 | |
| 1818 | for (i = 0; i < trace->common.stream_classes->len; i++) { |
| 1819 | /* bt_ctf_stream_class_serialize() logs details */ |
| 1820 | err = bt_ctf_stream_class_serialize( |
| 1821 | trace->common.stream_classes->pdata[i], context); |
| 1822 | if (err) { |
| 1823 | /* bt_ctf_stream_class_serialize() logs errors */ |
| 1824 | goto error; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | metadata = context->string->str; |
| 1829 | |
| 1830 | error: |
| 1831 | g_string_free(context->string, err ? TRUE : FALSE); |
| 1832 | g_string_free(context->field_name, TRUE); |
| 1833 | g_free(context); |
| 1834 | |
| 1835 | end: |
| 1836 | return metadata; |
| 1837 | } |
| 1838 | |
| 1839 | enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order( |
| 1840 | struct bt_ctf_trace *trace) |
| 1841 | { |
| 1842 | return (int) bt_ctf_trace_common_get_native_byte_order(BT_CTF_TO_COMMON(trace)); |
| 1843 | } |
| 1844 | |
| 1845 | int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace, |
| 1846 | enum bt_ctf_byte_order byte_order) |
| 1847 | { |
| 1848 | return bt_ctf_trace_common_set_native_byte_order(BT_CTF_TO_COMMON(trace), |
| 1849 | (int) byte_order, false); |
| 1850 | } |
| 1851 | |
| 1852 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_field_type( |
| 1853 | struct bt_ctf_trace *trace) |
| 1854 | { |
| 1855 | return bt_ctf_object_get_ref(bt_ctf_trace_common_borrow_packet_header_field_type( |
| 1856 | BT_CTF_TO_COMMON(trace))); |
| 1857 | } |
| 1858 | |
| 1859 | int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace *trace, |
| 1860 | struct bt_ctf_field_type *packet_header_type) |
| 1861 | { |
| 1862 | return bt_ctf_trace_common_set_packet_header_field_type(BT_CTF_TO_COMMON(trace), |
| 1863 | (void *) packet_header_type); |
| 1864 | } |
| 1865 | |
| 1866 | const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace) |
| 1867 | { |
| 1868 | return bt_ctf_trace_common_get_name(BT_CTF_TO_COMMON(trace)); |
| 1869 | } |