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