| 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/STREAM" |
| 25 | #include "logging.h" |
| 26 | |
| 27 | #include <inttypes.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <stdint.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <babeltrace2-ctf-writer/field-types.h> |
| 33 | #include <babeltrace2-ctf-writer/object.h> |
| 34 | #include <babeltrace2-ctf-writer/stream-class.h> |
| 35 | #include <babeltrace2-ctf-writer/stream.h> |
| 36 | #include <babeltrace2-ctf-writer/trace.h> |
| 37 | |
| 38 | #include "common/align.h" |
| 39 | #include "common/assert.h" |
| 40 | #include "compat/compiler.h" |
| 41 | #include "ctfser/ctfser.h" |
| 42 | |
| 43 | #include "assert-pre.h" |
| 44 | #include "event-class.h" |
| 45 | #include "event.h" |
| 46 | #include "fields.h" |
| 47 | #include "stream-class.h" |
| 48 | #include "stream.h" |
| 49 | #include "trace.h" |
| 50 | #include "writer.h" |
| 51 | |
| 52 | BT_HIDDEN |
| 53 | void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common *stream) |
| 54 | { |
| 55 | BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"", |
| 56 | stream, bt_ctf_stream_common_get_name(stream)); |
| 57 | |
| 58 | if (stream->name) { |
| 59 | g_string_free(stream->name, TRUE); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | BT_HIDDEN |
| 64 | int bt_ctf_stream_common_initialize( |
| 65 | struct bt_ctf_stream_common *stream, |
| 66 | struct bt_ctf_stream_class_common *stream_class, const char *name, |
| 67 | uint64_t id, bt_ctf_object_release_func release_func) |
| 68 | { |
| 69 | int ret = 0; |
| 70 | struct bt_ctf_trace_common *trace = NULL; |
| 71 | |
| 72 | bt_ctf_object_init_shared_with_parent(&stream->base, release_func); |
| 73 | |
| 74 | if (!stream_class) { |
| 75 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
| 76 | goto error; |
| 77 | } |
| 78 | |
| 79 | BT_LOGD("Initializing common stream object: stream-class-addr=%p, " |
| 80 | "stream-class-name=\"%s\", stream-name=\"%s\", " |
| 81 | "stream-id=%" PRIu64, |
| 82 | stream_class, bt_ctf_stream_class_common_get_name(stream_class), |
| 83 | name, id); |
| 84 | trace = bt_ctf_stream_class_common_borrow_trace(stream_class); |
| 85 | if (!trace) { |
| 86 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " |
| 87 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 88 | "stream-name=\"%s\"", |
| 89 | stream_class, |
| 90 | bt_ctf_stream_class_common_get_name(stream_class), name); |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | if (id != -1ULL) { |
| 95 | /* |
| 96 | * Validate that the given ID is unique amongst all the |
| 97 | * existing trace's streams created from the same stream |
| 98 | * class. |
| 99 | */ |
| 100 | size_t i; |
| 101 | |
| 102 | for (i = 0; i < trace->streams->len; i++) { |
| 103 | struct bt_ctf_stream_common *trace_stream = |
| 104 | g_ptr_array_index(trace->streams, i); |
| 105 | |
| 106 | if (trace_stream->stream_class != (void *) stream_class) { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | if (trace_stream->id == id) { |
| 111 | BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID."); |
| 112 | goto error; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Acquire reference to parent since stream will become publicly |
| 119 | * reachable; it needs its parent to remain valid. |
| 120 | */ |
| 121 | bt_ctf_object_set_parent(&stream->base, &trace->base); |
| 122 | stream->stream_class = stream_class; |
| 123 | stream->id = (int64_t) id; |
| 124 | |
| 125 | if (name) { |
| 126 | stream->name = g_string_new(name); |
| 127 | if (!stream->name) { |
| 128 | BT_LOGE_STR("Failed to allocate a GString."); |
| 129 | goto error; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace); |
| 134 | |
| 135 | /* Add this stream to the trace's streams */ |
| 136 | BT_LOGD("Created common stream object: addr=%p", stream); |
| 137 | goto end; |
| 138 | |
| 139 | error: |
| 140 | ret = -1; |
| 141 | |
| 142 | end: |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static |
| 147 | void bt_ctf_stream_destroy(struct bt_ctf_object *obj); |
| 148 | |
| 149 | static |
| 150 | int try_set_structure_field_integer(struct bt_ctf_field *, const char *, uint64_t); |
| 151 | |
| 152 | static |
| 153 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) |
| 154 | { |
| 155 | int ret = 0; |
| 156 | struct bt_ctf_field_type *field_type = NULL; |
| 157 | |
| 158 | if (!field) { |
| 159 | BT_LOGW_STR("Invalid parameter: field is NULL."); |
| 160 | ret = -1; |
| 161 | goto end; |
| 162 | } |
| 163 | |
| 164 | field_type = bt_ctf_field_get_type(field); |
| 165 | BT_ASSERT_DBG(field_type); |
| 166 | |
| 167 | if (bt_ctf_field_type_get_type_id(field_type) != |
| 168 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 169 | /* Not an integer and the value is unset, error. */ |
| 170 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
| 171 | "field-addr=%p, ft-addr=%p, ft-id=%s", |
| 172 | field, field_type, |
| 173 | bt_ctf_field_type_id_string((int) |
| 174 | bt_ctf_field_type_get_type_id(field_type))); |
| 175 | ret = -1; |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | if (bt_ctf_field_type_integer_is_signed(field_type)) { |
| 180 | ret = bt_ctf_field_integer_signed_set_value(field, (int64_t) value); |
| 181 | if (ret) { |
| 182 | /* Value is out of range, error. */ |
| 183 | BT_LOGW("Cannot set signed integer field's value: " |
| 184 | "addr=%p, value=%" PRId64, |
| 185 | field, (int64_t) value); |
| 186 | goto end; |
| 187 | } |
| 188 | } else { |
| 189 | ret = bt_ctf_field_integer_unsigned_set_value(field, value); |
| 190 | if (ret) { |
| 191 | /* Value is out of range, error. */ |
| 192 | BT_LOGW("Cannot set unsigned integer field's value: " |
| 193 | "addr=%p, value=%" PRIu64, |
| 194 | field, value); |
| 195 | goto end; |
| 196 | } |
| 197 | } |
| 198 | end: |
| 199 | bt_ctf_object_put_ref(field_type); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static |
| 204 | int set_packet_header_magic(struct bt_ctf_stream *stream) |
| 205 | { |
| 206 | int ret = 0; |
| 207 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field_by_name( |
| 208 | stream->packet_header, "magic"); |
| 209 | const uint32_t magic_value = 0xc1fc1fc1; |
| 210 | |
| 211 | BT_ASSERT_DBG(stream); |
| 212 | |
| 213 | if (!magic_field) { |
| 214 | /* No magic field found. Not an error, skip. */ |
| 215 | BT_LOGT("No field named `magic` in packet header: skipping: " |
| 216 | "stream-addr=%p, stream-name=\"%s\"", |
| 217 | stream, bt_ctf_stream_get_name(stream)); |
| 218 | goto end; |
| 219 | } |
| 220 | |
| 221 | ret = bt_ctf_field_integer_unsigned_set_value(magic_field, |
| 222 | (uint64_t) magic_value); |
| 223 | |
| 224 | if (ret) { |
| 225 | BT_LOGW("Cannot set packet header field's `magic` integer field's value: " |
| 226 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 227 | stream, bt_ctf_stream_get_name(stream), |
| 228 | magic_field, (uint64_t) magic_value); |
| 229 | } else { |
| 230 | BT_LOGT("Set packet header field's `magic` field's value: " |
| 231 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 232 | stream, bt_ctf_stream_get_name(stream), |
| 233 | magic_field, (uint64_t) magic_value); |
| 234 | } |
| 235 | end: |
| 236 | bt_ctf_object_put_ref(magic_field); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static |
| 241 | int set_packet_header_uuid(struct bt_ctf_stream *stream) |
| 242 | { |
| 243 | int ret = 0; |
| 244 | int64_t i; |
| 245 | struct bt_ctf_trace *trace = NULL; |
| 246 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field_by_name( |
| 247 | stream->packet_header, "uuid"); |
| 248 | |
| 249 | BT_ASSERT_DBG(stream); |
| 250 | |
| 251 | if (!uuid_field) { |
| 252 | /* No uuid field found. Not an error, skip. */ |
| 253 | BT_LOGT("No field named `uuid` in packet header: skipping: " |
| 254 | "stream-addr=%p, stream-name=\"%s\"", |
| 255 | stream, bt_ctf_stream_get_name(stream)); |
| 256 | goto end; |
| 257 | } |
| 258 | |
| 259 | trace = (struct bt_ctf_trace *) |
| 260 | bt_ctf_object_get_parent(&stream->common.base); |
| 261 | |
| 262 | for (i = 0; i < 16; i++) { |
| 263 | struct bt_ctf_field *uuid_element = |
| 264 | bt_ctf_field_array_get_field(uuid_field, i); |
| 265 | |
| 266 | ret = bt_ctf_field_integer_unsigned_set_value( |
| 267 | uuid_element, (uint64_t) trace->common.uuid[i]); |
| 268 | bt_ctf_object_put_ref(uuid_element); |
| 269 | if (ret) { |
| 270 | BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): " |
| 271 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, " |
| 272 | "value=%" PRIu64 ", index=%" PRId64, |
| 273 | stream, bt_ctf_stream_get_name(stream), |
| 274 | uuid_element, (uint64_t) trace->common.uuid[i], i); |
| 275 | goto end; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | BT_LOGT("Set packet header field's `uuid` field's value: " |
| 280 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p", |
| 281 | stream, bt_ctf_stream_get_name(stream), uuid_field); |
| 282 | |
| 283 | end: |
| 284 | bt_ctf_object_put_ref(uuid_field); |
| 285 | BT_CTF_OBJECT_PUT_REF_AND_RESET(trace); |
| 286 | return ret; |
| 287 | } |
| 288 | static |
| 289 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) |
| 290 | { |
| 291 | int ret = 0; |
| 292 | uint32_t stream_id; |
| 293 | struct bt_ctf_field *stream_id_field = |
| 294 | bt_ctf_field_structure_get_field_by_name( |
| 295 | stream->packet_header, "stream_id"); |
| 296 | |
| 297 | if (!stream_id_field) { |
| 298 | /* No stream_id field found. Not an error, skip. */ |
| 299 | BT_LOGT("No field named `stream_id` in packet header: skipping: " |
| 300 | "stream-addr=%p, stream-name=\"%s\"", |
| 301 | stream, bt_ctf_stream_get_name(stream)); |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | stream_id = stream->common.stream_class->id; |
| 306 | ret = bt_ctf_field_integer_unsigned_set_value(stream_id_field, |
| 307 | (uint64_t) stream_id); |
| 308 | if (ret) { |
| 309 | BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: " |
| 310 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 311 | stream, bt_ctf_stream_get_name(stream), |
| 312 | stream_id_field, (uint64_t) stream_id); |
| 313 | } else { |
| 314 | BT_LOGT("Set packet header field's `stream_id` field's value: " |
| 315 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 316 | stream, bt_ctf_stream_get_name(stream), |
| 317 | stream_id_field, (uint64_t) stream_id); |
| 318 | } |
| 319 | |
| 320 | end: |
| 321 | bt_ctf_object_put_ref(stream_id_field); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | static |
| 326 | int auto_populate_packet_header(struct bt_ctf_stream *stream) |
| 327 | { |
| 328 | int ret = 0; |
| 329 | |
| 330 | if (!stream->packet_header) { |
| 331 | goto end; |
| 332 | } |
| 333 | |
| 334 | ret = set_packet_header_magic(stream); |
| 335 | if (ret) { |
| 336 | BT_LOGW("Cannot set packet header's magic number field: " |
| 337 | "stream-addr=%p, stream-name=\"%s\"", |
| 338 | stream, bt_ctf_stream_get_name(stream)); |
| 339 | goto end; |
| 340 | } |
| 341 | |
| 342 | ret = set_packet_header_uuid(stream); |
| 343 | if (ret) { |
| 344 | BT_LOGW("Cannot set packet header's UUID field: " |
| 345 | "stream-addr=%p, stream-name=\"%s\"", |
| 346 | stream, bt_ctf_stream_get_name(stream)); |
| 347 | goto end; |
| 348 | } |
| 349 | |
| 350 | ret = set_packet_header_stream_id(stream); |
| 351 | if (ret) { |
| 352 | BT_LOGW("Cannot set packet header's stream class ID field: " |
| 353 | "stream-addr=%p, stream-name=\"%s\"", |
| 354 | stream, bt_ctf_stream_get_name(stream)); |
| 355 | goto end; |
| 356 | } |
| 357 | |
| 358 | BT_LOGT("Automatically populated stream's packet header's known fields: " |
| 359 | "stream-addr=%p, stream-name=\"%s\"", |
| 360 | stream, bt_ctf_stream_get_name(stream)); |
| 361 | |
| 362 | end: |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static |
| 367 | int set_packet_context_packet_size(struct bt_ctf_stream *stream, |
| 368 | uint64_t packet_size_bits) |
| 369 | { |
| 370 | int ret = 0; |
| 371 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name( |
| 372 | stream->packet_context, "packet_size"); |
| 373 | |
| 374 | ret = bt_ctf_field_integer_unsigned_set_value(field, packet_size_bits); |
| 375 | if (ret) { |
| 376 | BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: " |
| 377 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 378 | stream, bt_ctf_stream_get_name(stream), |
| 379 | field, packet_size_bits); |
| 380 | } else { |
| 381 | BT_LOGT("Set packet context field's `packet_size` field's value: " |
| 382 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 383 | stream, bt_ctf_stream_get_name(stream), |
| 384 | field, packet_size_bits); |
| 385 | } |
| 386 | |
| 387 | bt_ctf_object_put_ref(field); |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static |
| 392 | int set_packet_context_content_size(struct bt_ctf_stream *stream, |
| 393 | uint64_t content_size_bits) |
| 394 | { |
| 395 | int ret = 0; |
| 396 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name( |
| 397 | stream->packet_context, "content_size"); |
| 398 | |
| 399 | BT_ASSERT_DBG(stream); |
| 400 | |
| 401 | if (!field) { |
| 402 | /* No content size field found. Not an error, skip. */ |
| 403 | BT_LOGT("No field named `content_size` in packet context: skipping: " |
| 404 | "stream-addr=%p, stream-name=\"%s\"", |
| 405 | stream, bt_ctf_stream_get_name(stream)); |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | ret = bt_ctf_field_integer_unsigned_set_value(field, content_size_bits); |
| 410 | if (ret) { |
| 411 | BT_LOGW("Cannot set packet context field's `content_size` integer field's value: " |
| 412 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 413 | stream, bt_ctf_stream_get_name(stream), |
| 414 | field, content_size_bits); |
| 415 | } else { |
| 416 | BT_LOGT("Set packet context field's `content_size` field's value: " |
| 417 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 418 | stream, bt_ctf_stream_get_name(stream), |
| 419 | field, content_size_bits); |
| 420 | } |
| 421 | |
| 422 | end: |
| 423 | bt_ctf_object_put_ref(field); |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static |
| 428 | int set_packet_context_events_discarded(struct bt_ctf_stream *stream) |
| 429 | { |
| 430 | int ret = 0; |
| 431 | struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name( |
| 432 | stream->packet_context, "events_discarded"); |
| 433 | |
| 434 | BT_ASSERT_DBG(stream); |
| 435 | |
| 436 | if (!field) { |
| 437 | /* No discarded events count field found. Not an error, skip. */ |
| 438 | BT_LOGT("No field named `events_discarded` in packet context: skipping: " |
| 439 | "stream-addr=%p, stream-name=\"%s\"", |
| 440 | stream, bt_ctf_stream_get_name(stream)); |
| 441 | goto end; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * If the field is set by the user, make sure that the value is |
| 446 | * greater than or equal to the stream's current count of |
| 447 | * discarded events. We do not allow wrapping here. If it's |
| 448 | * valid, update the stream's current count. |
| 449 | */ |
| 450 | if (bt_ctf_field_is_set_recursive(field)) { |
| 451 | uint64_t user_val; |
| 452 | |
| 453 | ret = bt_ctf_field_integer_unsigned_get_value(field, |
| 454 | &user_val); |
| 455 | if (ret) { |
| 456 | BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: " |
| 457 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p", |
| 458 | stream, bt_ctf_stream_get_name(stream), field); |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | if (user_val < stream->discarded_events) { |
| 463 | BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: " |
| 464 | "value is lesser than the stream's current discarded events count: " |
| 465 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, " |
| 466 | "value=%" PRIu64 ", " |
| 467 | "stream-discarded-events-count=%" PRIu64, |
| 468 | stream, bt_ctf_stream_get_name(stream), field, |
| 469 | user_val, stream->discarded_events); |
| 470 | goto end; |
| 471 | } |
| 472 | |
| 473 | stream->discarded_events = user_val; |
| 474 | } else { |
| 475 | ret = bt_ctf_field_integer_unsigned_set_value(field, |
| 476 | stream->discarded_events); |
| 477 | if (ret) { |
| 478 | BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: " |
| 479 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 480 | stream, bt_ctf_stream_get_name(stream), |
| 481 | field, stream->discarded_events); |
| 482 | } else { |
| 483 | BT_LOGT("Set packet context field's `events_discarded` field's value: " |
| 484 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
| 485 | stream, bt_ctf_stream_get_name(stream), |
| 486 | field, stream->discarded_events); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | end: |
| 491 | bt_ctf_object_put_ref(field); |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static |
| 496 | void update_clock_value(uint64_t *val, uint64_t new_val, |
| 497 | unsigned int new_val_size) |
| 498 | { |
| 499 | const uint64_t pow2 = 1ULL << new_val_size; |
| 500 | const uint64_t mask = pow2 - 1; |
| 501 | uint64_t val_masked; |
| 502 | |
| 503 | #ifdef BT_LOG_ENABLED_TRACE |
| 504 | uint64_t old_val = *val; |
| 505 | #endif |
| 506 | |
| 507 | if (new_val_size == 64) { |
| 508 | *val = new_val; |
| 509 | goto end; |
| 510 | } |
| 511 | |
| 512 | val_masked = *val & mask; |
| 513 | |
| 514 | if (new_val < val_masked) { |
| 515 | /* Wrapped once */ |
| 516 | new_val |= pow2; |
| 517 | } |
| 518 | |
| 519 | *val &= ~mask; |
| 520 | *val |= new_val; |
| 521 | |
| 522 | end: |
| 523 | BT_LOGT("Updated clock value: old-val=%" PRIu64 ", new-val=%" PRIu64, |
| 524 | old_val, *val); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | static |
| 529 | int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val) |
| 530 | { |
| 531 | int ret = 0; |
| 532 | struct bt_ctf_field_common *field_common = (void *) field; |
| 533 | |
| 534 | if (!field) { |
| 535 | goto end; |
| 536 | } |
| 537 | |
| 538 | switch (bt_ctf_field_get_type_id(field)) { |
| 539 | case BT_CTF_FIELD_TYPE_ID_INTEGER: |
| 540 | { |
| 541 | struct bt_ctf_clock_class *cc = |
| 542 | bt_ctf_field_type_integer_get_mapped_clock_class( |
| 543 | (void *) field_common->type); |
| 544 | int val_size; |
| 545 | uint64_t uval; |
| 546 | |
| 547 | if (!cc) { |
| 548 | goto end; |
| 549 | } |
| 550 | |
| 551 | bt_ctf_object_put_ref(cc); |
| 552 | val_size = bt_ctf_field_type_integer_get_size( |
| 553 | (void *) field_common->type); |
| 554 | BT_ASSERT_DBG(val_size >= 1); |
| 555 | |
| 556 | if (bt_ctf_field_type_integer_is_signed( |
| 557 | (void *) field_common->type)) { |
| 558 | int64_t ival; |
| 559 | |
| 560 | ret = bt_ctf_field_integer_signed_get_value(field, &ival); |
| 561 | uval = (uint64_t) ival; |
| 562 | } else { |
| 563 | ret = bt_ctf_field_integer_unsigned_get_value(field, &uval); |
| 564 | } |
| 565 | |
| 566 | if (ret) { |
| 567 | /* Not set */ |
| 568 | goto end; |
| 569 | } |
| 570 | |
| 571 | update_clock_value(val, uval, val_size); |
| 572 | break; |
| 573 | } |
| 574 | case BT_CTF_FIELD_TYPE_ID_ENUM: |
| 575 | { |
| 576 | struct bt_ctf_field *int_field = |
| 577 | bt_ctf_field_enumeration_get_container(field); |
| 578 | |
| 579 | BT_ASSERT_DBG(int_field); |
| 580 | ret = visit_field_update_clock_value(int_field, val); |
| 581 | bt_ctf_object_put_ref(int_field); |
| 582 | break; |
| 583 | } |
| 584 | case BT_CTF_FIELD_TYPE_ID_ARRAY: |
| 585 | { |
| 586 | uint64_t i; |
| 587 | int64_t len = bt_ctf_field_type_array_get_length( |
| 588 | (void *) field_common->type); |
| 589 | |
| 590 | BT_ASSERT_DBG(len >= 0); |
| 591 | |
| 592 | for (i = 0; i < len; i++) { |
| 593 | struct bt_ctf_field *elem_field = |
| 594 | bt_ctf_field_array_get_field(field, i); |
| 595 | |
| 596 | BT_ASSERT_DBG(elem_field); |
| 597 | ret = visit_field_update_clock_value(elem_field, val); |
| 598 | bt_ctf_object_put_ref(elem_field); |
| 599 | if (ret) { |
| 600 | goto end; |
| 601 | } |
| 602 | } |
| 603 | break; |
| 604 | } |
| 605 | case BT_CTF_FIELD_TYPE_ID_SEQUENCE: |
| 606 | { |
| 607 | uint64_t i; |
| 608 | int64_t len = bt_ctf_field_common_sequence_get_length( |
| 609 | (void *) field); |
| 610 | |
| 611 | if (len < 0) { |
| 612 | ret = -1; |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | for (i = 0; i < len; i++) { |
| 617 | struct bt_ctf_field *elem_field = |
| 618 | bt_ctf_field_sequence_get_field(field, i); |
| 619 | |
| 620 | BT_ASSERT_DBG(elem_field); |
| 621 | ret = visit_field_update_clock_value(elem_field, val); |
| 622 | bt_ctf_object_put_ref(elem_field); |
| 623 | if (ret) { |
| 624 | goto end; |
| 625 | } |
| 626 | } |
| 627 | break; |
| 628 | } |
| 629 | case BT_CTF_FIELD_TYPE_ID_STRUCT: |
| 630 | { |
| 631 | uint64_t i; |
| 632 | int64_t len = bt_ctf_field_type_structure_get_field_count( |
| 633 | (void *) field_common->type); |
| 634 | |
| 635 | BT_ASSERT_DBG(len >= 0); |
| 636 | |
| 637 | for (i = 0; i < len; i++) { |
| 638 | struct bt_ctf_field *member_field = |
| 639 | bt_ctf_field_structure_get_field_by_index(field, i); |
| 640 | |
| 641 | BT_ASSERT_DBG(member_field); |
| 642 | ret = visit_field_update_clock_value(member_field, val); |
| 643 | bt_ctf_object_put_ref(member_field); |
| 644 | if (ret) { |
| 645 | goto end; |
| 646 | } |
| 647 | } |
| 648 | break; |
| 649 | } |
| 650 | case BT_CTF_FIELD_TYPE_ID_VARIANT: |
| 651 | { |
| 652 | struct bt_ctf_field *cur_field = |
| 653 | bt_ctf_field_variant_get_current_field(field); |
| 654 | |
| 655 | if (!cur_field) { |
| 656 | ret = -1; |
| 657 | goto end; |
| 658 | } |
| 659 | |
| 660 | ret = visit_field_update_clock_value(cur_field, val); |
| 661 | bt_ctf_object_put_ref(cur_field); |
| 662 | break; |
| 663 | } |
| 664 | default: |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | end: |
| 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | static |
| 673 | int visit_event_update_clock_value(struct bt_ctf_event *event, uint64_t *val) |
| 674 | { |
| 675 | int ret = 0; |
| 676 | struct bt_ctf_field *field; |
| 677 | |
| 678 | field = bt_ctf_event_get_header(event); |
| 679 | ret = visit_field_update_clock_value(field, val); |
| 680 | bt_ctf_object_put_ref(field); |
| 681 | if (ret) { |
| 682 | BT_LOGW_STR("Cannot automatically update clock value in " |
| 683 | "event's header."); |
| 684 | goto end; |
| 685 | } |
| 686 | |
| 687 | field = bt_ctf_event_get_stream_event_context(event); |
| 688 | ret = visit_field_update_clock_value(field, val); |
| 689 | bt_ctf_object_put_ref(field); |
| 690 | if (ret) { |
| 691 | BT_LOGW_STR("Cannot automatically update clock value in " |
| 692 | "event's stream event context."); |
| 693 | goto end; |
| 694 | } |
| 695 | |
| 696 | field = bt_ctf_event_get_context(event); |
| 697 | ret = visit_field_update_clock_value(field, val); |
| 698 | bt_ctf_object_put_ref(field); |
| 699 | if (ret) { |
| 700 | BT_LOGW_STR("Cannot automatically update clock value in " |
| 701 | "event's context."); |
| 702 | goto end; |
| 703 | } |
| 704 | |
| 705 | field = bt_ctf_event_get_payload_field(event); |
| 706 | ret = visit_field_update_clock_value(field, val); |
| 707 | bt_ctf_object_put_ref(field); |
| 708 | if (ret) { |
| 709 | BT_LOGW_STR("Cannot automatically update clock value in " |
| 710 | "event's payload."); |
| 711 | goto end; |
| 712 | } |
| 713 | |
| 714 | end: |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | static |
| 719 | int set_packet_context_timestamps(struct bt_ctf_stream *stream) |
| 720 | { |
| 721 | int ret = 0; |
| 722 | uint64_t val; |
| 723 | uint64_t cur_clock_value; |
| 724 | uint64_t init_clock_value = 0; |
| 725 | struct bt_ctf_field *ts_begin_field = bt_ctf_field_structure_get_field_by_name( |
| 726 | stream->packet_context, "timestamp_begin"); |
| 727 | struct bt_ctf_field *ts_end_field = bt_ctf_field_structure_get_field_by_name( |
| 728 | stream->packet_context, "timestamp_end"); |
| 729 | struct bt_ctf_field_common *packet_context = |
| 730 | (void *) stream->packet_context; |
| 731 | uint64_t i; |
| 732 | int64_t len; |
| 733 | |
| 734 | if (ts_begin_field && bt_ctf_field_is_set_recursive(ts_begin_field)) { |
| 735 | /* Use provided `timestamp_begin` value as starting value */ |
| 736 | ret = bt_ctf_field_integer_unsigned_get_value(ts_begin_field, &val); |
| 737 | BT_ASSERT_DBG(ret == 0); |
| 738 | init_clock_value = val; |
| 739 | } else if (stream->last_ts_end != -1ULL) { |
| 740 | /* Use last packet's ending timestamp as starting value */ |
| 741 | init_clock_value = stream->last_ts_end; |
| 742 | } |
| 743 | |
| 744 | cur_clock_value = init_clock_value; |
| 745 | |
| 746 | if (stream->last_ts_end != -1ULL && |
| 747 | cur_clock_value < stream->last_ts_end) { |
| 748 | BT_LOGW("Packet's initial timestamp is less than previous " |
| 749 | "packet's final timestamp: " |
| 750 | "stream-addr=%p, stream-name=\"%s\", " |
| 751 | "cur-packet-ts-begin=%" PRIu64 ", " |
| 752 | "prev-packet-ts-end=%" PRIu64, |
| 753 | stream, bt_ctf_stream_get_name(stream), |
| 754 | cur_clock_value, stream->last_ts_end); |
| 755 | ret = -1; |
| 756 | goto end; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Visit all the packet context fields, followed by all the |
| 761 | * fields of all the events, in order, updating our current |
| 762 | * clock value as we visit. |
| 763 | * |
| 764 | * While visiting the packet context fields, do not consider |
| 765 | * `timestamp_begin` and `timestamp_end` because this function's |
| 766 | * purpose is to set them anyway. Also do not consider |
| 767 | * `packet_size`, `content_size`, `events_discarded`, and |
| 768 | * `packet_seq_num` if they are not set because those are |
| 769 | * autopopulating fields. |
| 770 | */ |
| 771 | len = bt_ctf_field_type_structure_get_field_count( |
| 772 | (void *) packet_context->type); |
| 773 | BT_ASSERT_DBG(len >= 0); |
| 774 | |
| 775 | for (i = 0; i < len; i++) { |
| 776 | const char *member_name; |
| 777 | struct bt_ctf_field *member_field; |
| 778 | |
| 779 | ret = bt_ctf_field_type_structure_get_field_by_index( |
| 780 | (void *) packet_context->type, &member_name, NULL, i); |
| 781 | BT_ASSERT_DBG(ret == 0); |
| 782 | |
| 783 | if (strcmp(member_name, "timestamp_begin") == 0 || |
| 784 | strcmp(member_name, "timestamp_end") == 0) { |
| 785 | continue; |
| 786 | } |
| 787 | |
| 788 | member_field = bt_ctf_field_structure_get_field_by_index( |
| 789 | stream->packet_context, i); |
| 790 | BT_ASSERT_DBG(member_field); |
| 791 | |
| 792 | if (strcmp(member_name, "packet_size") == 0 && |
| 793 | !bt_ctf_field_is_set_recursive(member_field)) { |
| 794 | bt_ctf_object_put_ref(member_field); |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | if (strcmp(member_name, "content_size") == 0 && |
| 799 | !bt_ctf_field_is_set_recursive(member_field)) { |
| 800 | bt_ctf_object_put_ref(member_field); |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | if (strcmp(member_name, "events_discarded") == 0 && |
| 805 | !bt_ctf_field_is_set_recursive(member_field)) { |
| 806 | bt_ctf_object_put_ref(member_field); |
| 807 | continue; |
| 808 | } |
| 809 | |
| 810 | if (strcmp(member_name, "packet_seq_num") == 0 && |
| 811 | !bt_ctf_field_is_set_recursive(member_field)) { |
| 812 | bt_ctf_object_put_ref(member_field); |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | ret = visit_field_update_clock_value(member_field, |
| 817 | &cur_clock_value); |
| 818 | bt_ctf_object_put_ref(member_field); |
| 819 | if (ret) { |
| 820 | BT_LOGW("Cannot automatically update clock value " |
| 821 | "in stream's packet context: " |
| 822 | "stream-addr=%p, stream-name=\"%s\", " |
| 823 | "field-name=\"%s\"", |
| 824 | stream, bt_ctf_stream_get_name(stream), |
| 825 | member_name); |
| 826 | goto end; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | for (i = 0; i < stream->events->len; i++) { |
| 831 | struct bt_ctf_event *event = g_ptr_array_index(stream->events, i); |
| 832 | |
| 833 | BT_ASSERT_DBG(event); |
| 834 | ret = visit_event_update_clock_value(event, &cur_clock_value); |
| 835 | if (ret) { |
| 836 | BT_LOGW("Cannot automatically update clock value " |
| 837 | "in stream's packet context: " |
| 838 | "stream-addr=%p, stream-name=\"%s\", " |
| 839 | "index=%" PRIu64 ", event-addr=%p, " |
| 840 | "event-class-id=%" PRId64 ", " |
| 841 | "event-class-name=\"%s\"", |
| 842 | stream, bt_ctf_stream_get_name(stream), |
| 843 | i, event, |
| 844 | bt_ctf_event_class_common_get_id(event->common.class), |
| 845 | bt_ctf_event_class_common_get_name(event->common.class)); |
| 846 | goto end; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Everything is visited, thus the current clock value |
| 852 | * corresponds to the ending timestamp. Validate this value |
| 853 | * against the provided value of `timestamp_end`, if any, |
| 854 | * otherwise set it. |
| 855 | */ |
| 856 | if (ts_end_field && bt_ctf_field_is_set_recursive(ts_end_field)) { |
| 857 | ret = bt_ctf_field_integer_unsigned_get_value(ts_end_field, &val); |
| 858 | BT_ASSERT_DBG(ret == 0); |
| 859 | |
| 860 | if (val < cur_clock_value) { |
| 861 | BT_LOGW("Packet's final timestamp is less than " |
| 862 | "computed packet's final timestamp: " |
| 863 | "stream-addr=%p, stream-name=\"%s\", " |
| 864 | "cur-packet-ts-end=%" PRIu64 ", " |
| 865 | "computed-packet-ts-end=%" PRIu64, |
| 866 | stream, bt_ctf_stream_get_name(stream), |
| 867 | val, cur_clock_value); |
| 868 | ret = -1; |
| 869 | goto end; |
| 870 | } |
| 871 | |
| 872 | stream->last_ts_end = val; |
| 873 | } |
| 874 | |
| 875 | if (ts_end_field && !bt_ctf_field_is_set_recursive(ts_end_field)) { |
| 876 | ret = set_integer_field_value(ts_end_field, cur_clock_value); |
| 877 | BT_ASSERT_DBG(ret == 0); |
| 878 | stream->last_ts_end = cur_clock_value; |
| 879 | } |
| 880 | |
| 881 | if (!ts_end_field) { |
| 882 | stream->last_ts_end = cur_clock_value; |
| 883 | } |
| 884 | |
| 885 | /* Set `timestamp_begin` field to initial clock value */ |
| 886 | if (ts_begin_field && !bt_ctf_field_is_set_recursive(ts_begin_field)) { |
| 887 | ret = set_integer_field_value(ts_begin_field, init_clock_value); |
| 888 | BT_ASSERT_DBG(ret == 0); |
| 889 | } |
| 890 | |
| 891 | end: |
| 892 | bt_ctf_object_put_ref(ts_begin_field); |
| 893 | bt_ctf_object_put_ref(ts_end_field); |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | static |
| 898 | int auto_populate_packet_context(struct bt_ctf_stream *stream, bool set_ts, |
| 899 | uint64_t packet_size_bits, uint64_t content_size_bits) |
| 900 | { |
| 901 | int ret = 0; |
| 902 | |
| 903 | if (!stream->packet_context) { |
| 904 | goto end; |
| 905 | } |
| 906 | |
| 907 | ret = set_packet_context_packet_size(stream, packet_size_bits); |
| 908 | if (ret) { |
| 909 | BT_LOGW("Cannot set packet context's packet size field: " |
| 910 | "stream-addr=%p, stream-name=\"%s\"", |
| 911 | stream, bt_ctf_stream_get_name(stream)); |
| 912 | goto end; |
| 913 | } |
| 914 | |
| 915 | ret = set_packet_context_content_size(stream, content_size_bits); |
| 916 | if (ret) { |
| 917 | BT_LOGW("Cannot set packet context's content size field: " |
| 918 | "stream-addr=%p, stream-name=\"%s\"", |
| 919 | stream, bt_ctf_stream_get_name(stream)); |
| 920 | goto end; |
| 921 | } |
| 922 | |
| 923 | if (set_ts) { |
| 924 | ret = set_packet_context_timestamps(stream); |
| 925 | if (ret) { |
| 926 | BT_LOGW("Cannot set packet context's timestamp fields: " |
| 927 | "stream-addr=%p, stream-name=\"%s\"", |
| 928 | stream, bt_ctf_stream_get_name(stream)); |
| 929 | goto end; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | ret = set_packet_context_events_discarded(stream); |
| 934 | if (ret) { |
| 935 | BT_LOGW("Cannot set packet context's discarded events count field: " |
| 936 | "stream-addr=%p, stream-name=\"%s\"", |
| 937 | stream, bt_ctf_stream_get_name(stream)); |
| 938 | goto end; |
| 939 | } |
| 940 | |
| 941 | BT_LOGT("Automatically populated stream's packet context's known fields: " |
| 942 | "stream-addr=%p, stream-name=\"%s\"", |
| 943 | stream, bt_ctf_stream_get_name(stream)); |
| 944 | |
| 945 | end: |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | static |
| 950 | void release_event(struct bt_ctf_event *event) |
| 951 | { |
| 952 | if (bt_ctf_object_get_ref_count(&event->common.base)) { |
| 953 | /* |
| 954 | * The event is being orphaned, but it must guarantee the |
| 955 | * existence of its event class for the duration of its |
| 956 | * lifetime. |
| 957 | */ |
| 958 | bt_ctf_object_get_ref(event->common.class); |
| 959 | BT_CTF_OBJECT_PUT_REF_AND_RESET(event->common.base.parent); |
| 960 | } else { |
| 961 | bt_ctf_object_try_spec_release(&event->common.base); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | static |
| 966 | int create_stream_file(struct bt_ctf_writer *writer, |
| 967 | struct bt_ctf_stream *stream) |
| 968 | { |
| 969 | int ret = 0; |
| 970 | GString *filename = g_string_new(NULL); |
| 971 | int64_t stream_class_id; |
| 972 | char *file_path = NULL; |
| 973 | |
| 974 | BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, " |
| 975 | "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"", |
| 976 | writer, stream, bt_ctf_stream_get_name(stream), |
| 977 | stream->common.stream_class, |
| 978 | stream->common.stream_class->name->str); |
| 979 | |
| 980 | if (stream->common.name && stream->common.name->len > 0) { |
| 981 | /* Use stream name's base name as prefix */ |
| 982 | gchar *basename = g_path_get_basename(stream->common.name->str); |
| 983 | |
| 984 | BT_ASSERT_DBG(basename); |
| 985 | |
| 986 | if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) { |
| 987 | g_string_assign(filename, "stream"); |
| 988 | } else { |
| 989 | g_string_assign(filename, basename); |
| 990 | } |
| 991 | |
| 992 | g_free(basename); |
| 993 | goto append_ids; |
| 994 | } |
| 995 | |
| 996 | if (stream->common.stream_class->name && |
| 997 | stream->common.stream_class->name->len > 0) { |
| 998 | /* Use stream class name's base name as prefix */ |
| 999 | gchar *basename = |
| 1000 | g_path_get_basename( |
| 1001 | stream->common.stream_class->name->str); |
| 1002 | |
| 1003 | BT_ASSERT_DBG(basename); |
| 1004 | |
| 1005 | if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) { |
| 1006 | g_string_assign(filename, "stream"); |
| 1007 | } else { |
| 1008 | g_string_assign(filename, basename); |
| 1009 | } |
| 1010 | |
| 1011 | g_free(basename); |
| 1012 | goto append_ids; |
| 1013 | } |
| 1014 | |
| 1015 | /* Default to using `stream-` as prefix */ |
| 1016 | g_string_assign(filename, "stream"); |
| 1017 | |
| 1018 | append_ids: |
| 1019 | stream_class_id = bt_ctf_stream_class_common_get_id(stream->common.stream_class); |
| 1020 | BT_ASSERT_DBG(stream_class_id >= 0); |
| 1021 | BT_ASSERT_DBG(stream->common.id >= 0); |
| 1022 | g_string_append_printf(filename, "-%" PRId64 "-%" PRId64, |
| 1023 | stream_class_id, stream->common.id); |
| 1024 | |
| 1025 | file_path = g_build_filename(writer->path->str, filename->str, NULL); |
| 1026 | if (!file_path) { |
| 1027 | ret = -1; |
| 1028 | goto end; |
| 1029 | } |
| 1030 | |
| 1031 | ret = bt_ctfser_init(&stream->ctfser, file_path, |
| 1032 | BT_LOG_OUTPUT_LEVEL); |
| 1033 | g_free(file_path); |
| 1034 | if (ret) { |
| 1035 | /* bt_ctfser_init() logs errors */ |
| 1036 | goto end; |
| 1037 | } |
| 1038 | |
| 1039 | BT_LOGD("Created stream file for writing: " |
| 1040 | "stream-addr=%p, stream-name=\"%s\", " |
| 1041 | "filename=\"%s\"", stream, bt_ctf_stream_get_name(stream), |
| 1042 | filename->str); |
| 1043 | |
| 1044 | end: |
| 1045 | g_string_free(filename, TRUE); |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | BT_HIDDEN |
| 1050 | struct bt_ctf_stream *bt_ctf_stream_create_with_id( |
| 1051 | struct bt_ctf_stream_class *stream_class, |
| 1052 | const char *name, uint64_t id) |
| 1053 | { |
| 1054 | int ret; |
| 1055 | int fd; |
| 1056 | struct bt_ctf_stream *stream = NULL; |
| 1057 | struct bt_ctf_trace *trace = NULL; |
| 1058 | struct bt_ctf_writer *writer = NULL; |
| 1059 | |
| 1060 | BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, " |
| 1061 | "stream-class-name=\"%s\", stream-name=\"%s\", " |
| 1062 | "stream-id=%" PRIu64, |
| 1063 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1064 | name, id); |
| 1065 | stream = g_new0(struct bt_ctf_stream, 1); |
| 1066 | if (!stream) { |
| 1067 | BT_LOGE_STR("Failed to allocate one stream."); |
| 1068 | goto error; |
| 1069 | } |
| 1070 | |
| 1071 | if (id == -1ULL) { |
| 1072 | id = stream_class->next_stream_id; |
| 1073 | } |
| 1074 | |
| 1075 | ret = bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream), |
| 1076 | BT_CTF_TO_COMMON(stream_class), name, id, bt_ctf_stream_destroy); |
| 1077 | if (ret) { |
| 1078 | /* bt_ctf_stream_common_initialize() logs errors */ |
| 1079 | goto error; |
| 1080 | } |
| 1081 | |
| 1082 | trace = BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace( |
| 1083 | BT_CTF_TO_COMMON(stream_class))); |
| 1084 | if (!trace) { |
| 1085 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " |
| 1086 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 1087 | "stream-name=\"%s\"", |
| 1088 | stream_class, bt_ctf_stream_class_get_name(stream_class), |
| 1089 | name); |
| 1090 | goto error; |
| 1091 | } |
| 1092 | |
| 1093 | writer = (struct bt_ctf_writer *) |
| 1094 | bt_ctf_object_get_parent(&trace->common.base); |
| 1095 | stream->last_ts_end = -1ULL; |
| 1096 | BT_LOGD("CTF writer stream object belongs writer's trace: " |
| 1097 | "writer-addr=%p", writer); |
| 1098 | BT_ASSERT_DBG(writer); |
| 1099 | |
| 1100 | if (stream_class->common.packet_context_field_type) { |
| 1101 | BT_LOGD("Creating stream's packet context field: " |
| 1102 | "ft-addr=%p", |
| 1103 | stream_class->common.packet_context_field_type); |
| 1104 | stream->packet_context = bt_ctf_field_create( |
| 1105 | (void *) stream_class->common.packet_context_field_type); |
| 1106 | if (!stream->packet_context) { |
| 1107 | BT_LOGW_STR("Cannot create stream's packet context field."); |
| 1108 | goto error; |
| 1109 | } |
| 1110 | |
| 1111 | /* Initialize events_discarded */ |
| 1112 | ret = try_set_structure_field_integer( |
| 1113 | stream->packet_context, "events_discarded", 0); |
| 1114 | if (ret < 0) { |
| 1115 | BT_LOGW("Cannot set `events_discarded` field in packet context: " |
| 1116 | "ret=%d, packet-context-field-addr=%p", |
| 1117 | ret, stream->packet_context); |
| 1118 | goto error; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | stream->events = g_ptr_array_new_with_free_func( |
| 1123 | (GDestroyNotify) release_event); |
| 1124 | if (!stream->events) { |
| 1125 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 1126 | goto error; |
| 1127 | } |
| 1128 | |
| 1129 | if (trace->common.packet_header_field_type) { |
| 1130 | BT_LOGD("Creating stream's packet header field: " |
| 1131 | "ft-addr=%p", trace->common.packet_header_field_type); |
| 1132 | stream->packet_header = |
| 1133 | bt_ctf_field_create( |
| 1134 | (void *) trace->common.packet_header_field_type); |
| 1135 | if (!stream->packet_header) { |
| 1136 | BT_LOGW_STR("Cannot create stream's packet header field."); |
| 1137 | goto error; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Attempt to populate the default trace packet header fields |
| 1143 | * (magic, uuid and stream_id). This will _not_ fail shall the |
| 1144 | * fields not be found or be of an incompatible type; they will |
| 1145 | * simply not be populated automatically. The user will have to |
| 1146 | * make sure to set the trace packet header fields himself |
| 1147 | * before flushing. |
| 1148 | */ |
| 1149 | ret = auto_populate_packet_header(stream); |
| 1150 | if (ret) { |
| 1151 | BT_LOGW_STR("Cannot automatically populate the stream's packet header."); |
| 1152 | goto error; |
| 1153 | } |
| 1154 | |
| 1155 | /* Create file associated with this stream */ |
| 1156 | fd = create_stream_file(writer, stream); |
| 1157 | if (fd < 0) { |
| 1158 | BT_LOGW_STR("Cannot create stream file."); |
| 1159 | goto error; |
| 1160 | } |
| 1161 | |
| 1162 | /* Freeze the writer */ |
| 1163 | BT_LOGD_STR("Freezing stream's CTF writer."); |
| 1164 | bt_ctf_writer_freeze(writer); |
| 1165 | |
| 1166 | /* Add this stream to the trace's streams */ |
| 1167 | g_ptr_array_add(trace->common.streams, stream); |
| 1168 | stream_class->next_stream_id++; |
| 1169 | BT_LOGD("Created stream object: addr=%p", stream); |
| 1170 | goto end; |
| 1171 | |
| 1172 | error: |
| 1173 | BT_CTF_OBJECT_PUT_REF_AND_RESET(stream); |
| 1174 | |
| 1175 | end: |
| 1176 | bt_ctf_object_put_ref(writer); |
| 1177 | return stream; |
| 1178 | } |
| 1179 | |
| 1180 | struct bt_ctf_stream *bt_ctf_stream_create( |
| 1181 | struct bt_ctf_stream_class *stream_class, |
| 1182 | const char *name, uint64_t id_param) |
| 1183 | { |
| 1184 | return bt_ctf_stream_create_with_id(stream_class, |
| 1185 | name, id_param); |
| 1186 | } |
| 1187 | |
| 1188 | int bt_ctf_stream_get_discarded_events_count( |
| 1189 | struct bt_ctf_stream *stream, uint64_t *count) |
| 1190 | { |
| 1191 | int ret = 0; |
| 1192 | |
| 1193 | if (!stream) { |
| 1194 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1195 | ret = -1; |
| 1196 | goto end; |
| 1197 | } |
| 1198 | |
| 1199 | if (!count) { |
| 1200 | BT_LOGW_STR("Invalid parameter: count is NULL."); |
| 1201 | ret = -1; |
| 1202 | goto end; |
| 1203 | } |
| 1204 | |
| 1205 | *count = (uint64_t) stream->discarded_events; |
| 1206 | |
| 1207 | end: |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
| 1211 | static |
| 1212 | int set_packet_context_events_discarded_field(struct bt_ctf_stream *stream, |
| 1213 | uint64_t count) |
| 1214 | { |
| 1215 | int ret = 0; |
| 1216 | struct bt_ctf_field *events_discarded_field = NULL; |
| 1217 | |
| 1218 | if (!stream->packet_context) { |
| 1219 | goto end; |
| 1220 | } |
| 1221 | |
| 1222 | events_discarded_field = bt_ctf_field_structure_get_field_by_name( |
| 1223 | stream->packet_context, "events_discarded"); |
| 1224 | if (!events_discarded_field) { |
| 1225 | goto end; |
| 1226 | } |
| 1227 | |
| 1228 | ret = bt_ctf_field_integer_unsigned_set_value( |
| 1229 | events_discarded_field, count); |
| 1230 | if (ret) { |
| 1231 | BT_LOGW("Cannot set packet context's `events_discarded` field: " |
| 1232 | "field-addr=%p, value=%" PRIu64, |
| 1233 | events_discarded_field, count); |
| 1234 | goto end; |
| 1235 | } |
| 1236 | |
| 1237 | end: |
| 1238 | bt_ctf_object_put_ref(events_discarded_field); |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
| 1242 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
| 1243 | uint64_t event_count) |
| 1244 | { |
| 1245 | int ret; |
| 1246 | uint64_t new_count; |
| 1247 | struct bt_ctf_field *events_discarded_field = NULL; |
| 1248 | |
| 1249 | if (!stream) { |
| 1250 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1251 | goto end; |
| 1252 | } |
| 1253 | |
| 1254 | BT_LOGT("Appending discarded events to stream: " |
| 1255 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, |
| 1256 | stream, bt_ctf_stream_get_name(stream), event_count); |
| 1257 | |
| 1258 | if (!stream->packet_context) { |
| 1259 | BT_LOGW_STR("Invalid parameter: stream has no packet context field."); |
| 1260 | goto end; |
| 1261 | } |
| 1262 | |
| 1263 | events_discarded_field = bt_ctf_field_structure_get_field_by_name( |
| 1264 | stream->packet_context, "events_discarded"); |
| 1265 | if (!events_discarded_field) { |
| 1266 | BT_LOGW_STR("No field named `events_discarded` in stream's packet context."); |
| 1267 | goto end; |
| 1268 | } |
| 1269 | |
| 1270 | new_count = stream->discarded_events + event_count; |
| 1271 | if (new_count < stream->discarded_events) { |
| 1272 | BT_LOGW("New discarded events count is less than the stream's current discarded events count: " |
| 1273 | "cur-count=%" PRIu64 ", new-count=%" PRIu64, |
| 1274 | stream->discarded_events, new_count); |
| 1275 | goto end; |
| 1276 | } |
| 1277 | |
| 1278 | ret = set_packet_context_events_discarded_field(stream, new_count); |
| 1279 | if (ret) { |
| 1280 | /* set_packet_context_events_discarded_field() logs errors */ |
| 1281 | goto end; |
| 1282 | } |
| 1283 | |
| 1284 | stream->discarded_events = new_count; |
| 1285 | BT_LOGT("Appended discarded events to stream: " |
| 1286 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, |
| 1287 | stream, bt_ctf_stream_get_name(stream), event_count); |
| 1288 | |
| 1289 | end: |
| 1290 | bt_ctf_object_put_ref(events_discarded_field); |
| 1291 | } |
| 1292 | |
| 1293 | static int auto_populate_event_header(struct bt_ctf_stream *stream, |
| 1294 | struct bt_ctf_event *event) |
| 1295 | { |
| 1296 | int ret = 0; |
| 1297 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; |
| 1298 | struct bt_ctf_clock_class *mapped_clock_class = NULL; |
| 1299 | struct bt_ctf_stream_class *stream_class = |
| 1300 | BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class( |
| 1301 | BT_CTF_TO_COMMON(stream))); |
| 1302 | int64_t event_class_id; |
| 1303 | |
| 1304 | BT_ASSERT_DBG(event); |
| 1305 | |
| 1306 | if (!event->common.header_field) { |
| 1307 | goto end; |
| 1308 | } |
| 1309 | |
| 1310 | if (event->common.frozen) { |
| 1311 | BT_LOGW_STR("Cannot populate event header field: event is frozen."); |
| 1312 | ret = -1; |
| 1313 | goto end; |
| 1314 | } |
| 1315 | |
| 1316 | BT_LOGT("Automatically populating event's header field: " |
| 1317 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
| 1318 | stream, bt_ctf_stream_get_name(stream), event); |
| 1319 | |
| 1320 | id_field = bt_ctf_field_structure_get_field_by_name( |
| 1321 | (void *) event->common.header_field->field, "id"); |
| 1322 | event_class_id = bt_ctf_event_class_common_get_id(event->common.class); |
| 1323 | BT_ASSERT_DBG(event_class_id >= 0); |
| 1324 | |
| 1325 | if (id_field && bt_ctf_field_get_type_id(id_field) == BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1326 | ret = set_integer_field_value(id_field, event_class_id); |
| 1327 | if (ret) { |
| 1328 | BT_LOGW("Cannot set event header's `id` field's value: " |
| 1329 | "addr=%p, value=%" PRIu64, id_field, |
| 1330 | event_class_id); |
| 1331 | goto end; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * The conditions to automatically set the timestamp are: |
| 1337 | * |
| 1338 | * 1. The event header field "timestamp" exists and is an |
| 1339 | * integer field. |
| 1340 | * 2. This stream's class has a registered clock (set with |
| 1341 | * bt_ctf_stream_class_set_clock()). |
| 1342 | * 3. The "timestamp" field is not set. |
| 1343 | */ |
| 1344 | timestamp_field = bt_ctf_field_structure_get_field_by_name( |
| 1345 | (void *) event->common.header_field->field, "timestamp"); |
| 1346 | if (timestamp_field && stream_class->clock && |
| 1347 | bt_ctf_field_get_type_id(id_field) == BT_CTF_FIELD_TYPE_ID_INTEGER && |
| 1348 | !bt_ctf_field_is_set_recursive(timestamp_field)) { |
| 1349 | mapped_clock_class = |
| 1350 | bt_ctf_field_type_integer_get_mapped_clock_class( |
| 1351 | (void *) ((struct bt_ctf_field_common *) timestamp_field)->type); |
| 1352 | if (mapped_clock_class) { |
| 1353 | uint64_t timestamp; |
| 1354 | |
| 1355 | BT_ASSERT_DBG(mapped_clock_class == |
| 1356 | stream_class->clock->clock_class); |
| 1357 | ret = bt_ctf_clock_get_value( |
| 1358 | stream_class->clock, |
| 1359 | ×tamp); |
| 1360 | BT_ASSERT_DBG(ret == 0); |
| 1361 | ret = set_integer_field_value(timestamp_field, |
| 1362 | timestamp); |
| 1363 | if (ret) { |
| 1364 | BT_LOGW("Cannot set event header's `timestamp` field's value: " |
| 1365 | "addr=%p, value=%" PRIu64, |
| 1366 | timestamp_field, timestamp); |
| 1367 | goto end; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | BT_LOGT("Automatically populated event's header field: " |
| 1373 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
| 1374 | stream, bt_ctf_stream_get_name(stream), event); |
| 1375 | |
| 1376 | end: |
| 1377 | bt_ctf_object_put_ref(id_field); |
| 1378 | bt_ctf_object_put_ref(timestamp_field); |
| 1379 | bt_ctf_object_put_ref(mapped_clock_class); |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
| 1383 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, |
| 1384 | struct bt_ctf_event *event) |
| 1385 | { |
| 1386 | int ret = 0; |
| 1387 | |
| 1388 | if (!stream) { |
| 1389 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1390 | ret = -1; |
| 1391 | goto end; |
| 1392 | } |
| 1393 | |
| 1394 | if (!event) { |
| 1395 | BT_LOGW_STR("Invalid parameter: event is NULL."); |
| 1396 | ret = -1; |
| 1397 | goto end; |
| 1398 | } |
| 1399 | |
| 1400 | BT_LOGT("Appending event to stream: " |
| 1401 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " |
| 1402 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 1403 | stream, bt_ctf_stream_get_name(stream), event, |
| 1404 | bt_ctf_event_class_common_get_name( |
| 1405 | bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))), |
| 1406 | bt_ctf_event_class_common_get_id( |
| 1407 | bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event)))); |
| 1408 | |
| 1409 | /* |
| 1410 | * The event is not supposed to have a parent stream at this |
| 1411 | * point. The only other way an event can have a parent stream |
| 1412 | * is if it was assigned when setting a packet to the event, |
| 1413 | * in which case the packet's stream is not a writer stream, |
| 1414 | * and thus the user is trying to append an event which belongs |
| 1415 | * to another stream. |
| 1416 | */ |
| 1417 | if (event->common.base.parent) { |
| 1418 | ret = -1; |
| 1419 | goto end; |
| 1420 | } |
| 1421 | |
| 1422 | bt_ctf_object_set_parent(&event->common.base, &stream->common.base); |
| 1423 | BT_LOGT_STR("Automatically populating the header of the event to append."); |
| 1424 | ret = auto_populate_event_header(stream, event); |
| 1425 | if (ret) { |
| 1426 | /* auto_populate_event_header() reports errors */ |
| 1427 | goto error; |
| 1428 | } |
| 1429 | |
| 1430 | /* Make sure the various scopes of the event are set */ |
| 1431 | BT_LOGT_STR("Validating event to append."); |
| 1432 | BT_CTF_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event)) == 0, |
| 1433 | "Invalid event: event-addr=%p", event); |
| 1434 | |
| 1435 | /* Save the new event and freeze it */ |
| 1436 | BT_LOGT_STR("Freezing the event to append."); |
| 1437 | bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event), true); |
| 1438 | g_ptr_array_add(stream->events, event); |
| 1439 | |
| 1440 | /* |
| 1441 | * Event had to hold a reference to its event class as long as it wasn't |
| 1442 | * part of the same trace hierarchy. From now on, the event and its |
| 1443 | * class share the same lifetime guarantees and the reference is no |
| 1444 | * longer needed. |
| 1445 | */ |
| 1446 | BT_LOGT_STR("Putting the event's class."); |
| 1447 | bt_ctf_object_put_ref(event->common.class); |
| 1448 | BT_LOGT("Appended event to stream: " |
| 1449 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " |
| 1450 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 1451 | stream, bt_ctf_stream_get_name(stream), event, |
| 1452 | bt_ctf_event_class_common_get_name( |
| 1453 | bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))), |
| 1454 | bt_ctf_event_class_common_get_id( |
| 1455 | bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event)))); |
| 1456 | |
| 1457 | end: |
| 1458 | return ret; |
| 1459 | |
| 1460 | error: |
| 1461 | /* |
| 1462 | * Orphan the event; we were not successful in associating it to |
| 1463 | * a stream. |
| 1464 | */ |
| 1465 | bt_ctf_object_set_parent(&event->common.base, NULL); |
| 1466 | return ret; |
| 1467 | } |
| 1468 | |
| 1469 | struct bt_ctf_field *bt_ctf_stream_get_packet_context(struct bt_ctf_stream *stream) |
| 1470 | { |
| 1471 | struct bt_ctf_field *packet_context = NULL; |
| 1472 | |
| 1473 | if (!stream) { |
| 1474 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1475 | goto end; |
| 1476 | } |
| 1477 | |
| 1478 | packet_context = stream->packet_context; |
| 1479 | if (packet_context) { |
| 1480 | bt_ctf_object_get_ref(packet_context); |
| 1481 | } |
| 1482 | end: |
| 1483 | return packet_context; |
| 1484 | } |
| 1485 | |
| 1486 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, |
| 1487 | struct bt_ctf_field *field) |
| 1488 | { |
| 1489 | int ret = 0; |
| 1490 | struct bt_ctf_field_type *field_type; |
| 1491 | |
| 1492 | if (!stream) { |
| 1493 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1494 | ret = -1; |
| 1495 | goto end; |
| 1496 | } |
| 1497 | |
| 1498 | field_type = bt_ctf_field_get_type(field); |
| 1499 | if (bt_ctf_field_type_common_compare((void *) field_type, |
| 1500 | stream->common.stream_class->packet_context_field_type)) { |
| 1501 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: " |
| 1502 | "stream-addr=%p, stream-name=\"%s\", " |
| 1503 | "packet-context-field-addr=%p, " |
| 1504 | "packet-context-ft-addr=%p", |
| 1505 | stream, bt_ctf_stream_get_name(stream), |
| 1506 | field, field_type); |
| 1507 | ret = -1; |
| 1508 | goto end; |
| 1509 | } |
| 1510 | |
| 1511 | bt_ctf_object_put_ref(field_type); |
| 1512 | bt_ctf_object_put_ref(stream->packet_context); |
| 1513 | stream->packet_context = bt_ctf_object_get_ref(field); |
| 1514 | BT_LOGT("Set stream's packet context field: " |
| 1515 | "stream-addr=%p, stream-name=\"%s\", " |
| 1516 | "packet-context-field-addr=%p", |
| 1517 | stream, bt_ctf_stream_get_name(stream), field); |
| 1518 | end: |
| 1519 | return ret; |
| 1520 | } |
| 1521 | |
| 1522 | struct bt_ctf_field *bt_ctf_stream_get_packet_header(struct bt_ctf_stream *stream) |
| 1523 | { |
| 1524 | struct bt_ctf_field *packet_header = NULL; |
| 1525 | |
| 1526 | if (!stream) { |
| 1527 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1528 | goto end; |
| 1529 | } |
| 1530 | |
| 1531 | packet_header = stream->packet_header; |
| 1532 | if (packet_header) { |
| 1533 | bt_ctf_object_get_ref(packet_header); |
| 1534 | } |
| 1535 | end: |
| 1536 | return packet_header; |
| 1537 | } |
| 1538 | |
| 1539 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, |
| 1540 | struct bt_ctf_field *field) |
| 1541 | { |
| 1542 | int ret = 0; |
| 1543 | struct bt_ctf_trace *trace = NULL; |
| 1544 | struct bt_ctf_field_type *field_type = NULL; |
| 1545 | |
| 1546 | if (!stream) { |
| 1547 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1548 | ret = -1; |
| 1549 | goto end; |
| 1550 | } |
| 1551 | |
| 1552 | trace = (struct bt_ctf_trace *) |
| 1553 | bt_ctf_object_get_parent(&stream->common.base); |
| 1554 | |
| 1555 | if (!field) { |
| 1556 | if (trace->common.packet_header_field_type) { |
| 1557 | BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: " |
| 1558 | "stream-addr=%p, stream-name=\"%s\", " |
| 1559 | "packet-header-field-addr=%p, " |
| 1560 | "expected-ft-addr=%p", |
| 1561 | stream, bt_ctf_stream_get_name(stream), |
| 1562 | field, trace->common.packet_header_field_type); |
| 1563 | ret = -1; |
| 1564 | goto end; |
| 1565 | } |
| 1566 | |
| 1567 | goto skip_validation; |
| 1568 | } |
| 1569 | |
| 1570 | field_type = bt_ctf_field_get_type(field); |
| 1571 | BT_ASSERT_DBG(field_type); |
| 1572 | |
| 1573 | if (bt_ctf_field_type_common_compare((void *) field_type, |
| 1574 | trace->common.packet_header_field_type)) { |
| 1575 | BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: " |
| 1576 | "stream-addr=%p, stream-name=\"%s\", " |
| 1577 | "packet-header-field-addr=%p, " |
| 1578 | "packet-header-ft-addr=%p", |
| 1579 | stream, bt_ctf_stream_get_name(stream), |
| 1580 | field, field_type); |
| 1581 | ret = -1; |
| 1582 | goto end; |
| 1583 | } |
| 1584 | |
| 1585 | skip_validation: |
| 1586 | bt_ctf_object_put_ref(stream->packet_header); |
| 1587 | stream->packet_header = bt_ctf_object_get_ref(field); |
| 1588 | BT_LOGT("Set stream's packet header field: " |
| 1589 | "stream-addr=%p, stream-name=\"%s\", " |
| 1590 | "packet-header-field-addr=%p", |
| 1591 | stream, bt_ctf_stream_get_name(stream), field); |
| 1592 | end: |
| 1593 | BT_CTF_OBJECT_PUT_REF_AND_RESET(trace); |
| 1594 | bt_ctf_object_put_ref(field_type); |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | static |
| 1599 | void reset_structure_field(struct bt_ctf_field *structure, const char *name) |
| 1600 | { |
| 1601 | struct bt_ctf_field *member; |
| 1602 | |
| 1603 | member = bt_ctf_field_structure_get_field_by_name(structure, name); |
| 1604 | if (member) { |
| 1605 | bt_ctf_field_common_reset_recursive((void *) member); |
| 1606 | bt_ctf_object_put_ref(member); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
| 1611 | { |
| 1612 | int ret = 0; |
| 1613 | size_t i; |
| 1614 | uint64_t packet_context_offset_bits = 0; |
| 1615 | struct bt_ctf_trace *trace; |
| 1616 | enum bt_ctf_byte_order native_byte_order; |
| 1617 | bool has_packet_size = false; |
| 1618 | uint64_t packet_size_bits = 0; |
| 1619 | uint64_t content_size_bits = 0; |
| 1620 | |
| 1621 | if (!stream) { |
| 1622 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 1623 | ret = -1; |
| 1624 | goto end_no_stream; |
| 1625 | } |
| 1626 | |
| 1627 | if (stream->packet_context) { |
| 1628 | struct bt_ctf_field *packet_size_field; |
| 1629 | |
| 1630 | packet_size_field = bt_ctf_field_structure_get_field_by_name( |
| 1631 | stream->packet_context, "packet_size"); |
| 1632 | has_packet_size = packet_size_field; |
| 1633 | bt_ctf_object_put_ref(packet_size_field); |
| 1634 | } |
| 1635 | |
| 1636 | if (stream->flushed_packet_count == 1) { |
| 1637 | if (!stream->packet_context) { |
| 1638 | BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once."); |
| 1639 | ret = -1; |
| 1640 | goto end; |
| 1641 | } |
| 1642 | |
| 1643 | if (!has_packet_size) { |
| 1644 | BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once."); |
| 1645 | ret = -1; |
| 1646 | goto end; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | BT_LOGT("Flushing stream's current packet: stream-addr=%p, " |
| 1651 | "stream-name=\"%s\", packet-index=%u", stream, |
| 1652 | bt_ctf_stream_get_name(stream), stream->flushed_packet_count); |
| 1653 | trace = BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace( |
| 1654 | stream->common.stream_class)); |
| 1655 | BT_ASSERT_DBG(trace); |
| 1656 | native_byte_order = bt_ctf_trace_get_native_byte_order(trace); |
| 1657 | |
| 1658 | ret = auto_populate_packet_header(stream); |
| 1659 | if (ret) { |
| 1660 | BT_LOGW_STR("Cannot automatically populate the stream's packet header field."); |
| 1661 | ret = -1; |
| 1662 | goto end; |
| 1663 | } |
| 1664 | |
| 1665 | /* Initialize packet/content sizes to `0`; we will overwrite later */ |
| 1666 | ret = auto_populate_packet_context(stream, true, 0, 0); |
| 1667 | if (ret) { |
| 1668 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); |
| 1669 | ret = -1; |
| 1670 | goto end; |
| 1671 | } |
| 1672 | |
| 1673 | ret = bt_ctfser_open_packet(&stream->ctfser); |
| 1674 | if (ret) { |
| 1675 | /* bt_ctfser_open_packet() logs errors */ |
| 1676 | ret = -1; |
| 1677 | goto end; |
| 1678 | } |
| 1679 | |
| 1680 | if (stream->packet_header) { |
| 1681 | BT_LOGT_STR("Serializing packet header field (initial)."); |
| 1682 | ret = bt_ctf_field_serialize_recursive(stream->packet_header, |
| 1683 | &stream->ctfser, native_byte_order); |
| 1684 | if (ret) { |
| 1685 | BT_LOGW("Cannot serialize stream's packet header field: " |
| 1686 | "field-addr=%p", stream->packet_header); |
| 1687 | goto end; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | if (stream->packet_context) { |
| 1692 | /* Save packet context's position to overwrite it later */ |
| 1693 | packet_context_offset_bits = |
| 1694 | bt_ctfser_get_offset_in_current_packet_bits( |
| 1695 | &stream->ctfser); |
| 1696 | |
| 1697 | /* Write packet context */ |
| 1698 | BT_LOGT_STR("Serializing packet context field (initial)."); |
| 1699 | ret = bt_ctf_field_serialize_recursive(stream->packet_context, |
| 1700 | &stream->ctfser, native_byte_order); |
| 1701 | if (ret) { |
| 1702 | BT_LOGW("Cannot serialize stream's packet context field: " |
| 1703 | "field-addr=%p", stream->packet_context); |
| 1704 | goto end; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | BT_LOGT("Serializing events: count=%u", stream->events->len); |
| 1709 | |
| 1710 | for (i = 0; i < stream->events->len; i++) { |
| 1711 | struct bt_ctf_event *event = g_ptr_array_index( |
| 1712 | stream->events, i); |
| 1713 | struct bt_ctf_event_class *event_class = |
| 1714 | BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class( |
| 1715 | BT_CTF_TO_COMMON(event))); |
| 1716 | |
| 1717 | BT_LOGT("Serializing event: index=%zu, event-addr=%p, " |
| 1718 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
| 1719 | "ser-offset=%" PRIu64, |
| 1720 | i, event, bt_ctf_event_class_get_name(event_class), |
| 1721 | bt_ctf_event_class_get_id(event_class), |
| 1722 | bt_ctfser_get_offset_in_current_packet_bits( |
| 1723 | &stream->ctfser)); |
| 1724 | |
| 1725 | /* Write event header */ |
| 1726 | if (event->common.header_field) { |
| 1727 | BT_LOGT_STR("Serializing event's header field."); |
| 1728 | ret = bt_ctf_field_serialize_recursive( |
| 1729 | (void *) event->common.header_field->field, |
| 1730 | &stream->ctfser, native_byte_order); |
| 1731 | if (ret) { |
| 1732 | BT_LOGW("Cannot serialize event's header field: " |
| 1733 | "field-addr=%p", |
| 1734 | event->common.header_field->field); |
| 1735 | goto end; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | /* Write stream event context */ |
| 1740 | if (event->common.stream_event_context_field) { |
| 1741 | BT_LOGT_STR("Serializing event's stream event context field."); |
| 1742 | ret = bt_ctf_field_serialize_recursive( |
| 1743 | (void *) event->common.stream_event_context_field, |
| 1744 | &stream->ctfser, native_byte_order); |
| 1745 | if (ret) { |
| 1746 | BT_LOGW("Cannot serialize event's stream event context field: " |
| 1747 | "field-addr=%p", |
| 1748 | event->common.stream_event_context_field); |
| 1749 | goto end; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | /* Write event content */ |
| 1754 | ret = bt_ctf_event_serialize(event, &stream->ctfser, |
| 1755 | native_byte_order); |
| 1756 | if (ret) { |
| 1757 | /* bt_ctf_event_serialize() logs errors */ |
| 1758 | goto end; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | content_size_bits = bt_ctfser_get_offset_in_current_packet_bits( |
| 1763 | &stream->ctfser); |
| 1764 | |
| 1765 | if (!has_packet_size && content_size_bits % 8 != 0) { |
| 1766 | BT_LOGW("Stream's packet context field type has no `packet_size` field, " |
| 1767 | "but current content size is not a multiple of 8 bits: " |
| 1768 | "content-size=%" PRIu64 ", " |
| 1769 | "packet-size=%" PRIu64, |
| 1770 | content_size_bits, |
| 1771 | packet_size_bits); |
| 1772 | ret = -1; |
| 1773 | goto end; |
| 1774 | } |
| 1775 | |
| 1776 | /* Set packet size; make it a multiple of 8 */ |
| 1777 | packet_size_bits = (content_size_bits + 7) & ~UINT64_C(7); |
| 1778 | |
| 1779 | if (stream->packet_context) { |
| 1780 | /* |
| 1781 | * The whole packet is serialized at this point. Make |
| 1782 | * sure that, if `packet_size` is missing, the current |
| 1783 | * content size is equal to the current packet size. |
| 1784 | */ |
| 1785 | struct bt_ctf_field *field = |
| 1786 | bt_ctf_field_structure_get_field_by_name( |
| 1787 | stream->packet_context, "content_size"); |
| 1788 | |
| 1789 | bt_ctf_object_put_ref(field); |
| 1790 | if (!field) { |
| 1791 | if (content_size_bits != packet_size_bits) { |
| 1792 | BT_LOGW("Stream's packet context's `content_size` field is missing, " |
| 1793 | "but current packet's content size is not equal to its packet size: " |
| 1794 | "content-size=%" PRIu64 ", " |
| 1795 | "packet-size=%" PRIu64, |
| 1796 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser), |
| 1797 | packet_size_bits); |
| 1798 | ret = -1; |
| 1799 | goto end; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * Overwrite the packet context now that the stream |
| 1805 | * position's packet and content sizes have the correct |
| 1806 | * values. |
| 1807 | */ |
| 1808 | bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser, |
| 1809 | packet_context_offset_bits); |
| 1810 | ret = auto_populate_packet_context(stream, false, |
| 1811 | packet_size_bits, content_size_bits); |
| 1812 | if (ret) { |
| 1813 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); |
| 1814 | ret = -1; |
| 1815 | goto end; |
| 1816 | } |
| 1817 | |
| 1818 | BT_LOGT("Rewriting (serializing) packet context field."); |
| 1819 | ret = bt_ctf_field_serialize_recursive(stream->packet_context, |
| 1820 | &stream->ctfser, native_byte_order); |
| 1821 | if (ret) { |
| 1822 | BT_LOGW("Cannot serialize stream's packet context field: " |
| 1823 | "field-addr=%p", stream->packet_context); |
| 1824 | goto end; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | g_ptr_array_set_size(stream->events, 0); |
| 1829 | stream->flushed_packet_count++; |
| 1830 | bt_ctfser_close_current_packet(&stream->ctfser, packet_size_bits / 8); |
| 1831 | |
| 1832 | end: |
| 1833 | /* Reset automatically-set fields. */ |
| 1834 | if (stream->packet_context) { |
| 1835 | reset_structure_field(stream->packet_context, "timestamp_begin"); |
| 1836 | reset_structure_field(stream->packet_context, "timestamp_end"); |
| 1837 | reset_structure_field(stream->packet_context, "packet_size"); |
| 1838 | reset_structure_field(stream->packet_context, "content_size"); |
| 1839 | reset_structure_field(stream->packet_context, "events_discarded"); |
| 1840 | } |
| 1841 | |
| 1842 | if (ret == 0) { |
| 1843 | BT_LOGT("Flushed stream's current packet: " |
| 1844 | "content-size=%" PRIu64 ", packet-size=%" PRIu64, |
| 1845 | content_size_bits, packet_size_bits); |
| 1846 | } |
| 1847 | |
| 1848 | end_no_stream: |
| 1849 | return ret; |
| 1850 | } |
| 1851 | |
| 1852 | static |
| 1853 | void bt_ctf_stream_destroy(struct bt_ctf_object *obj) |
| 1854 | { |
| 1855 | struct bt_ctf_stream *stream = (void *) obj; |
| 1856 | |
| 1857 | BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"", |
| 1858 | stream, bt_ctf_stream_get_name(stream)); |
| 1859 | |
| 1860 | bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream)); |
| 1861 | bt_ctfser_fini(&stream->ctfser); |
| 1862 | |
| 1863 | if (stream->events) { |
| 1864 | BT_LOGD_STR("Putting events."); |
| 1865 | g_ptr_array_free(stream->events, TRUE); |
| 1866 | } |
| 1867 | |
| 1868 | BT_LOGD_STR("Putting packet header field."); |
| 1869 | bt_ctf_object_put_ref(stream->packet_header); |
| 1870 | BT_LOGD_STR("Putting packet context field."); |
| 1871 | bt_ctf_object_put_ref(stream->packet_context); |
| 1872 | g_free(stream); |
| 1873 | } |
| 1874 | |
| 1875 | static |
| 1876 | int _set_structure_field_integer(struct bt_ctf_field *structure, const char *name, |
| 1877 | uint64_t value, bt_ctf_bool force) |
| 1878 | { |
| 1879 | int ret = 0; |
| 1880 | struct bt_ctf_field_type *field_type = NULL; |
| 1881 | struct bt_ctf_field *integer; |
| 1882 | |
| 1883 | BT_ASSERT_DBG(structure); |
| 1884 | BT_ASSERT_DBG(name); |
| 1885 | |
| 1886 | integer = bt_ctf_field_structure_get_field_by_name(structure, name); |
| 1887 | if (!integer) { |
| 1888 | /* Field not found, not an error. */ |
| 1889 | BT_LOGT("Field not found: struct-field-addr=%p, " |
| 1890 | "name=\"%s\", force=%d", structure, name, force); |
| 1891 | goto end; |
| 1892 | } |
| 1893 | |
| 1894 | /* Make sure the payload has not already been set. */ |
| 1895 | if (!force && bt_ctf_field_is_set_recursive(integer)) { |
| 1896 | /* Payload already set, not an error */ |
| 1897 | BT_LOGT("Field's payload is already set: struct-field-addr=%p, " |
| 1898 | "name=\"%s\", force=%d", structure, name, force); |
| 1899 | goto end; |
| 1900 | } |
| 1901 | |
| 1902 | field_type = bt_ctf_field_get_type(integer); |
| 1903 | BT_ASSERT_DBG(field_type); |
| 1904 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1905 | /* |
| 1906 | * The user most likely meant for us to populate this field |
| 1907 | * automatically. However, we can only do this if the field |
| 1908 | * is an integer. Return an error. |
| 1909 | */ |
| 1910 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
| 1911 | "field-addr=%p, ft-addr=%p, ft-id=%s", |
| 1912 | integer, field_type, |
| 1913 | bt_ctf_field_type_id_string((int) |
| 1914 | bt_ctf_field_type_get_type_id(field_type))); |
| 1915 | ret = -1; |
| 1916 | goto end; |
| 1917 | } |
| 1918 | |
| 1919 | if (bt_ctf_field_type_integer_is_signed(field_type)) { |
| 1920 | ret = bt_ctf_field_integer_signed_set_value(integer, |
| 1921 | (int64_t) value); |
| 1922 | } else { |
| 1923 | ret = bt_ctf_field_integer_unsigned_set_value(integer, value); |
| 1924 | } |
| 1925 | ret = !ret ? 1 : ret; |
| 1926 | end: |
| 1927 | bt_ctf_object_put_ref(integer); |
| 1928 | bt_ctf_object_put_ref(field_type); |
| 1929 | return ret; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * Returns the following codes: |
| 1934 | * 1 if the field was found and set, |
| 1935 | * 0 if nothing was done (field not found, or was already set), |
| 1936 | * <0 if an error was encoutered |
| 1937 | */ |
| 1938 | static |
| 1939 | int try_set_structure_field_integer(struct bt_ctf_field *structure, const char *name, |
| 1940 | uint64_t value) |
| 1941 | { |
| 1942 | return _set_structure_field_integer(structure, name, value, BT_CTF_FALSE); |
| 1943 | } |
| 1944 | |
| 1945 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
| 1946 | struct bt_ctf_stream *stream) |
| 1947 | { |
| 1948 | return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream))); |
| 1949 | } |
| 1950 | |
| 1951 | const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) |
| 1952 | { |
| 1953 | return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream)); |
| 1954 | } |
| 1955 | |
| 1956 | int64_t bt_ctf_stream_get_id(struct bt_ctf_stream *stream) |
| 1957 | { |
| 1958 | return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream)); |
| 1959 | } |