| 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 | #include <babeltrace/ctf-ir/clock-class.h> |
| 30 | #include <babeltrace/ctf-writer/clock.h> |
| 31 | #include <babeltrace/ctf-writer/clock-internal.h> |
| 32 | #include <babeltrace/ctf-writer/event.h> |
| 33 | #include <babeltrace/ctf-ir/event-internal.h> |
| 34 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 35 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 36 | #include <babeltrace/ctf-ir/stream.h> |
| 37 | #include <babeltrace/ctf-ir/stream-internal.h> |
| 38 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 39 | #include <babeltrace/ctf-ir/trace-internal.h> |
| 40 | #include <babeltrace/ctf-writer/writer-internal.h> |
| 41 | #include <babeltrace/ref.h> |
| 42 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 43 | #include <babeltrace/compiler.h> |
| 44 | #include <babeltrace/align.h> |
| 45 | #include <babeltrace/ctf/ctf-index.h> |
| 46 | |
| 47 | static |
| 48 | void bt_ctf_stream_destroy(struct bt_object *obj); |
| 49 | static |
| 50 | int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
| 51 | static |
| 52 | int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
| 53 | |
| 54 | static |
| 55 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) |
| 56 | { |
| 57 | int ret = 0; |
| 58 | struct bt_ctf_field_type *field_type = NULL; |
| 59 | |
| 60 | if (!field) { |
| 61 | ret = -1; |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | field_type = bt_ctf_field_get_type(field); |
| 66 | assert(field_type); |
| 67 | |
| 68 | if (bt_ctf_field_type_get_type_id(field_type) != |
| 69 | BT_CTF_TYPE_ID_INTEGER) { |
| 70 | /* Not an integer and the value is unset, error. */ |
| 71 | ret = -1; |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | if (bt_ctf_field_type_integer_get_signed(field_type)) { |
| 76 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); |
| 77 | if (ret) { |
| 78 | /* Value is out of range, error. */ |
| 79 | goto end; |
| 80 | } |
| 81 | } else { |
| 82 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); |
| 83 | if (ret) { |
| 84 | /* Value is out of range, error. */ |
| 85 | goto end; |
| 86 | } |
| 87 | } |
| 88 | end: |
| 89 | bt_put(field_type); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static |
| 94 | int set_packet_header_magic(struct bt_ctf_stream *stream) |
| 95 | { |
| 96 | int ret = 0; |
| 97 | struct bt_ctf_field_type *magic_field_type = NULL; |
| 98 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field( |
| 99 | stream->packet_header, "magic"); |
| 100 | |
| 101 | if (!magic_field) { |
| 102 | /* No magic field found. Not an error, skip. */ |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | if (bt_ctf_field_is_set(magic_field)) { |
| 107 | /* Value already set. Not an error, skip. */ |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | magic_field_type = bt_ctf_field_get_type(magic_field); |
| 112 | assert(magic_field_type); |
| 113 | |
| 114 | if (bt_ctf_field_type_get_type_id(magic_field_type) != |
| 115 | BT_CTF_TYPE_ID_INTEGER) { |
| 116 | /* Magic field is not an integer. Not an error, skip. */ |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) { |
| 121 | /* |
| 122 | * Magic field is not of the expected size. |
| 123 | * Not an error, skip. |
| 124 | */ |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | ret = bt_ctf_field_type_integer_get_signed(magic_field_type); |
| 129 | assert(ret >= 0); |
| 130 | if (ret) { |
| 131 | ret = bt_ctf_field_signed_integer_set_value(magic_field, |
| 132 | (int64_t) 0xC1FC1FC1); |
| 133 | } else { |
| 134 | ret = bt_ctf_field_unsigned_integer_set_value(magic_field, |
| 135 | (uint64_t) 0xC1FC1FC1); |
| 136 | } |
| 137 | end: |
| 138 | bt_put(magic_field); |
| 139 | bt_put(magic_field_type); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static |
| 144 | int set_packet_header_uuid(struct bt_ctf_stream *stream) |
| 145 | { |
| 146 | int i, ret = 0; |
| 147 | struct bt_ctf_trace *trace = NULL; |
| 148 | struct bt_ctf_field_type *uuid_field_type = NULL; |
| 149 | struct bt_ctf_field_type *element_field_type = NULL; |
| 150 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field( |
| 151 | stream->packet_header, "uuid"); |
| 152 | |
| 153 | if (!uuid_field) { |
| 154 | /* No uuid field found. Not an error, skip. */ |
| 155 | goto end; |
| 156 | } |
| 157 | |
| 158 | if (bt_ctf_field_is_set(uuid_field)) { |
| 159 | /* Value already set. Not an error, skip. */ |
| 160 | goto end; |
| 161 | } |
| 162 | |
| 163 | uuid_field_type = bt_ctf_field_get_type(uuid_field); |
| 164 | assert(uuid_field_type); |
| 165 | if (bt_ctf_field_type_get_type_id(uuid_field_type) != |
| 166 | BT_CTF_TYPE_ID_ARRAY) { |
| 167 | /* UUID field is not an array. Not an error, skip. */ |
| 168 | goto end; |
| 169 | } |
| 170 | |
| 171 | if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) { |
| 172 | /* |
| 173 | * UUID field is not of the expected size. |
| 174 | * Not an error, skip. |
| 175 | */ |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | element_field_type = bt_ctf_field_type_array_get_element_type( |
| 180 | uuid_field_type); |
| 181 | assert(element_field_type); |
| 182 | if (bt_ctf_field_type_get_type_id(element_field_type) != |
| 183 | BT_CTF_TYPE_ID_INTEGER) { |
| 184 | /* UUID array elements are not integers. Not an error, skip */ |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
| 189 | for (i = 0; i < 16; i++) { |
| 190 | struct bt_ctf_field *uuid_element = |
| 191 | bt_ctf_field_array_get_field(uuid_field, i); |
| 192 | |
| 193 | ret = bt_ctf_field_type_integer_get_signed(element_field_type); |
| 194 | assert(ret >= 0); |
| 195 | |
| 196 | if (ret) { |
| 197 | ret = bt_ctf_field_signed_integer_set_value( |
| 198 | uuid_element, (int64_t) trace->uuid[i]); |
| 199 | } else { |
| 200 | ret = bt_ctf_field_unsigned_integer_set_value( |
| 201 | uuid_element, |
| 202 | (uint64_t) trace->uuid[i]); |
| 203 | } |
| 204 | bt_put(uuid_element); |
| 205 | if (ret) { |
| 206 | goto end; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | end: |
| 211 | bt_put(uuid_field); |
| 212 | bt_put(uuid_field_type); |
| 213 | bt_put(element_field_type); |
| 214 | BT_PUT(trace); |
| 215 | return ret; |
| 216 | } |
| 217 | static |
| 218 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) |
| 219 | { |
| 220 | int ret = 0; |
| 221 | uint32_t stream_id; |
| 222 | struct bt_ctf_field_type *stream_id_field_type = NULL; |
| 223 | struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field( |
| 224 | stream->packet_header, "stream_id"); |
| 225 | |
| 226 | if (!stream_id_field) { |
| 227 | /* No stream_id field found. Not an error, skip. */ |
| 228 | goto end; |
| 229 | } |
| 230 | |
| 231 | if (bt_ctf_field_is_set(stream_id_field)) { |
| 232 | /* Value already set. Not an error, skip. */ |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | stream_id_field_type = bt_ctf_field_get_type(stream_id_field); |
| 237 | assert(stream_id_field_type); |
| 238 | if (bt_ctf_field_type_get_type_id(stream_id_field_type) != |
| 239 | BT_CTF_TYPE_ID_INTEGER) { |
| 240 | /* stream_id field is not an integer. Not an error, skip. */ |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | stream_id = stream->stream_class->id; |
| 245 | ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type); |
| 246 | assert(ret >= 0); |
| 247 | if (ret) { |
| 248 | ret = bt_ctf_field_signed_integer_set_value(stream_id_field, |
| 249 | (int64_t) stream_id); |
| 250 | } else { |
| 251 | ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field, |
| 252 | (uint64_t) stream_id); |
| 253 | } |
| 254 | end: |
| 255 | bt_put(stream_id_field); |
| 256 | bt_put(stream_id_field_type); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static |
| 261 | int set_packet_header(struct bt_ctf_stream *stream) |
| 262 | { |
| 263 | int ret; |
| 264 | |
| 265 | ret = set_packet_header_magic(stream); |
| 266 | if (ret) { |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | ret = set_packet_header_uuid(stream); |
| 271 | if (ret) { |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | ret = set_packet_header_stream_id(stream); |
| 276 | if (ret) { |
| 277 | goto end; |
| 278 | } |
| 279 | end: |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static |
| 284 | void release_event(struct bt_ctf_event *event) |
| 285 | { |
| 286 | if (bt_object_get_ref_count(event)) { |
| 287 | /* |
| 288 | * The event is being orphaned, but it must guarantee the |
| 289 | * existence of its event class for the duration of its |
| 290 | * lifetime. |
| 291 | */ |
| 292 | bt_get(event->event_class); |
| 293 | BT_PUT(event->base.parent); |
| 294 | } else { |
| 295 | bt_object_release(event); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static |
| 300 | int create_stream_file(struct bt_ctf_writer *writer, |
| 301 | struct bt_ctf_stream *stream) |
| 302 | { |
| 303 | int fd; |
| 304 | GString *filename = g_string_new(stream->stream_class->name->str); |
| 305 | |
| 306 | if (stream->stream_class->name->len == 0) { |
| 307 | int64_t ret; |
| 308 | |
| 309 | ret = bt_ctf_stream_class_get_id(stream->stream_class); |
| 310 | if (ret < 0) { |
| 311 | fd = -1; |
| 312 | goto error; |
| 313 | } |
| 314 | |
| 315 | g_string_printf(filename, "stream_%" PRId64, ret); |
| 316 | } |
| 317 | |
| 318 | g_string_append_printf(filename, "_%" PRIu32, stream->id); |
| 319 | fd = openat(writer->trace_dir_fd, filename->str, |
| 320 | O_RDWR | O_CREAT | O_TRUNC, |
| 321 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 322 | error: |
| 323 | g_string_free(filename, TRUE); |
| 324 | return fd; |
| 325 | } |
| 326 | |
| 327 | static |
| 328 | int set_stream_fd(struct bt_ctf_stream *stream, int fd) |
| 329 | { |
| 330 | int ret = 0; |
| 331 | |
| 332 | if (stream->pos.fd != -1) { |
| 333 | ret = -1; |
| 334 | goto end; |
| 335 | } |
| 336 | |
| 337 | ctf_init_pos(&stream->pos, NULL, fd, O_RDWR); |
| 338 | stream->pos.fd = fd; |
| 339 | end: |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | struct bt_ctf_stream *bt_ctf_stream_create( |
| 344 | struct bt_ctf_stream_class *stream_class, |
| 345 | const char *name) |
| 346 | { |
| 347 | int ret; |
| 348 | struct bt_ctf_stream *stream = NULL; |
| 349 | struct bt_ctf_trace *trace = NULL; |
| 350 | struct bt_ctf_writer *writer = NULL; |
| 351 | |
| 352 | if (!stream_class) { |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 357 | if (!trace) { |
| 358 | goto error; |
| 359 | } |
| 360 | |
| 361 | stream = g_new0(struct bt_ctf_stream, 1); |
| 362 | if (!stream) { |
| 363 | goto error; |
| 364 | } |
| 365 | |
| 366 | bt_object_init(stream, bt_ctf_stream_destroy); |
| 367 | /* |
| 368 | * Acquire reference to parent since stream will become publicly |
| 369 | * reachable; it needs its parent to remain valid. |
| 370 | */ |
| 371 | bt_object_set_parent(stream, trace); |
| 372 | stream->id = stream_class->next_stream_id++; |
| 373 | stream->stream_class = stream_class; |
| 374 | stream->pos.fd = -1; |
| 375 | |
| 376 | if (name) { |
| 377 | stream->name = g_string_new(name); |
| 378 | if (!stream->name) { |
| 379 | goto error; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (trace->is_created_by_writer) { |
| 384 | int fd; |
| 385 | writer = (struct bt_ctf_writer *) |
| 386 | bt_object_get_parent(trace); |
| 387 | |
| 388 | assert(writer); |
| 389 | if (stream_class->packet_context_type) { |
| 390 | stream->packet_context = bt_ctf_field_create( |
| 391 | stream_class->packet_context_type); |
| 392 | if (!stream->packet_context) { |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | /* Initialize events_discarded */ |
| 397 | ret = try_set_structure_field_integer( |
| 398 | stream->packet_context, |
| 399 | "events_discarded", 0); |
| 400 | if (ret != 1) { |
| 401 | goto error; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | stream->events = g_ptr_array_new_with_free_func( |
| 406 | (GDestroyNotify) release_event); |
| 407 | if (!stream->events) { |
| 408 | goto error; |
| 409 | } |
| 410 | |
| 411 | /* A trace is not allowed to have a NULL packet header */ |
| 412 | assert(trace->packet_header_type); |
| 413 | stream->packet_header = |
| 414 | bt_ctf_field_create(trace->packet_header_type); |
| 415 | if (!stream->packet_header) { |
| 416 | goto error; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Attempt to populate the default trace packet header fields |
| 421 | * (magic, uuid and stream_id). This will _not_ fail shall the |
| 422 | * fields not be found or be of an incompatible type; they will |
| 423 | * simply not be populated automatically. The user will have to |
| 424 | * make sure to set the trace packet header fields himself |
| 425 | * before flushing. |
| 426 | */ |
| 427 | ret = set_packet_header(stream); |
| 428 | if (ret) { |
| 429 | goto error; |
| 430 | } |
| 431 | |
| 432 | /* Create file associated with this stream */ |
| 433 | fd = create_stream_file(writer, stream); |
| 434 | if (fd < 0) { |
| 435 | goto error; |
| 436 | } |
| 437 | |
| 438 | ret = set_stream_fd(stream, fd); |
| 439 | if (ret) { |
| 440 | goto error; |
| 441 | } |
| 442 | |
| 443 | /* Freeze the writer */ |
| 444 | bt_ctf_writer_freeze(writer); |
| 445 | } else { |
| 446 | /* Non-writer stream indicated by a negative FD */ |
| 447 | ret = set_stream_fd(stream, -1); |
| 448 | if (ret) { |
| 449 | goto error; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /* Add this stream to the trace's streams */ |
| 454 | g_ptr_array_add(trace->streams, stream); |
| 455 | |
| 456 | BT_PUT(trace); |
| 457 | BT_PUT(writer); |
| 458 | return stream; |
| 459 | error: |
| 460 | BT_PUT(stream); |
| 461 | BT_PUT(trace); |
| 462 | BT_PUT(writer); |
| 463 | return stream; |
| 464 | } |
| 465 | |
| 466 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
| 467 | struct bt_ctf_stream *stream) |
| 468 | { |
| 469 | struct bt_ctf_stream_class *stream_class = NULL; |
| 470 | |
| 471 | if (!stream) { |
| 472 | goto end; |
| 473 | } |
| 474 | |
| 475 | stream_class = stream->stream_class; |
| 476 | bt_get(stream_class); |
| 477 | end: |
| 478 | return stream_class; |
| 479 | } |
| 480 | |
| 481 | int bt_ctf_stream_get_discarded_events_count( |
| 482 | struct bt_ctf_stream *stream, uint64_t *count) |
| 483 | { |
| 484 | int64_t ret = 0; |
| 485 | int field_signed; |
| 486 | struct bt_ctf_field *events_discarded_field = NULL; |
| 487 | struct bt_ctf_field_type *events_discarded_field_type = NULL; |
| 488 | |
| 489 | if (!stream || !count || !stream->packet_context || |
| 490 | stream->pos.fd < 0) { |
| 491 | ret = -1; |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | events_discarded_field = bt_ctf_field_structure_get_field( |
| 496 | stream->packet_context, "events_discarded"); |
| 497 | if (!events_discarded_field) { |
| 498 | ret = -1; |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | events_discarded_field_type = bt_ctf_field_get_type( |
| 503 | events_discarded_field); |
| 504 | if (!events_discarded_field_type) { |
| 505 | ret = -1; |
| 506 | goto end; |
| 507 | } |
| 508 | |
| 509 | field_signed = bt_ctf_field_type_integer_get_signed( |
| 510 | events_discarded_field_type); |
| 511 | if (field_signed < 0) { |
| 512 | ret = field_signed; |
| 513 | goto end; |
| 514 | } |
| 515 | |
| 516 | if (field_signed) { |
| 517 | int64_t signed_count; |
| 518 | |
| 519 | ret = bt_ctf_field_signed_integer_get_value( |
| 520 | events_discarded_field, &signed_count); |
| 521 | if (ret) { |
| 522 | goto end; |
| 523 | } |
| 524 | if (signed_count < 0) { |
| 525 | /* Invalid value */ |
| 526 | ret = -1; |
| 527 | goto end; |
| 528 | } |
| 529 | *count = (uint64_t) signed_count; |
| 530 | } else { |
| 531 | ret = bt_ctf_field_unsigned_integer_get_value( |
| 532 | events_discarded_field, count); |
| 533 | if (ret) { |
| 534 | goto end; |
| 535 | } |
| 536 | } |
| 537 | end: |
| 538 | bt_put(events_discarded_field); |
| 539 | bt_put(events_discarded_field_type); |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
| 544 | uint64_t event_count) |
| 545 | { |
| 546 | int ret; |
| 547 | int field_signed; |
| 548 | uint64_t previous_count; |
| 549 | uint64_t new_count; |
| 550 | struct bt_ctf_field *events_discarded_field = NULL; |
| 551 | struct bt_ctf_field_type *events_discarded_field_type = NULL; |
| 552 | |
| 553 | if (!stream || !stream->packet_context || stream->pos.fd < 0) { |
| 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | ret = bt_ctf_stream_get_discarded_events_count(stream, |
| 558 | &previous_count); |
| 559 | if (ret) { |
| 560 | goto end; |
| 561 | } |
| 562 | |
| 563 | events_discarded_field = bt_ctf_field_structure_get_field( |
| 564 | stream->packet_context, "events_discarded"); |
| 565 | if (!events_discarded_field) { |
| 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | events_discarded_field_type = bt_ctf_field_get_type( |
| 570 | events_discarded_field); |
| 571 | if (!events_discarded_field_type) { |
| 572 | goto end; |
| 573 | } |
| 574 | |
| 575 | field_signed = bt_ctf_field_type_integer_get_signed( |
| 576 | events_discarded_field_type); |
| 577 | if (field_signed < 0) { |
| 578 | goto end; |
| 579 | } |
| 580 | |
| 581 | new_count = previous_count + event_count; |
| 582 | if (field_signed) { |
| 583 | ret = bt_ctf_field_signed_integer_set_value( |
| 584 | events_discarded_field, (int64_t) new_count); |
| 585 | if (ret) { |
| 586 | goto end; |
| 587 | } |
| 588 | } else { |
| 589 | ret = bt_ctf_field_unsigned_integer_set_value( |
| 590 | events_discarded_field, new_count); |
| 591 | if (ret) { |
| 592 | goto end; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | end: |
| 597 | bt_put(events_discarded_field); |
| 598 | bt_put(events_discarded_field_type); |
| 599 | } |
| 600 | |
| 601 | static int auto_populate_event_header(struct bt_ctf_stream *stream, |
| 602 | struct bt_ctf_event *event) |
| 603 | { |
| 604 | int ret = 0; |
| 605 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; |
| 606 | struct bt_ctf_clock_class *mapped_clock_class = NULL; |
| 607 | |
| 608 | if (!event || event->frozen) { |
| 609 | ret = -1; |
| 610 | goto end; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * The condition to automatically set the ID are: |
| 615 | * |
| 616 | * 1. The event header field "id" exists and is an integer |
| 617 | * field. |
| 618 | * 2. The event header field "id" is NOT set. |
| 619 | */ |
| 620 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); |
| 621 | if (id_field && !bt_ctf_field_is_set(id_field)) { |
| 622 | ret = set_integer_field_value(id_field, |
| 623 | (uint64_t) bt_ctf_event_class_get_id( |
| 624 | event->event_class)); |
| 625 | if (ret) { |
| 626 | goto end; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * The conditions to automatically set the timestamp are: |
| 632 | * |
| 633 | * 1. The event header field "timestamp" exists and is an |
| 634 | * integer field. |
| 635 | * 2. This stream's class has a registered clock (set with |
| 636 | * bt_ctf_stream_class_set_clock()). |
| 637 | * 3. The event header field "timestamp" has its type mapped to |
| 638 | * a clock class which is also the clock class of this |
| 639 | * stream's class's registered clock. |
| 640 | * 4. The event header field "timestamp" is NOT set. |
| 641 | */ |
| 642 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, |
| 643 | "timestamp"); |
| 644 | if (timestamp_field && !bt_ctf_field_is_set(timestamp_field) && |
| 645 | stream->stream_class->clock) { |
| 646 | struct bt_ctf_clock_class *stream_class_clock_class = |
| 647 | stream->stream_class->clock->clock_class; |
| 648 | struct bt_ctf_field_type *timestamp_field_type = |
| 649 | bt_ctf_field_get_type(timestamp_field); |
| 650 | |
| 651 | assert(timestamp_field_type); |
| 652 | mapped_clock_class = |
| 653 | bt_ctf_field_type_integer_get_mapped_clock_class( |
| 654 | timestamp_field_type); |
| 655 | BT_PUT(timestamp_field_type); |
| 656 | if (mapped_clock_class == stream_class_clock_class) { |
| 657 | uint64_t timestamp; |
| 658 | |
| 659 | ret = bt_ctf_clock_get_value( |
| 660 | stream->stream_class->clock, |
| 661 | ×tamp); |
| 662 | if (ret) { |
| 663 | goto end; |
| 664 | } |
| 665 | |
| 666 | ret = set_integer_field_value(timestamp_field, |
| 667 | timestamp); |
| 668 | if (ret) { |
| 669 | goto end; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | end: |
| 675 | bt_put(id_field); |
| 676 | bt_put(timestamp_field); |
| 677 | bt_put(mapped_clock_class); |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, |
| 682 | struct bt_ctf_event *event) |
| 683 | { |
| 684 | int ret = 0; |
| 685 | |
| 686 | if (!stream || !event || stream->pos.fd < 0) { |
| 687 | ret = -1; |
| 688 | goto end; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * The event is not supposed to have a parent stream at this |
| 693 | * point. The only other way an event can have a parent stream |
| 694 | * is if it was assigned when setting a packet to the event, |
| 695 | * in which case the packet's stream is not a writer stream, |
| 696 | * and thus the user is trying to append an event which belongs |
| 697 | * to another stream. |
| 698 | */ |
| 699 | if (event->base.parent) { |
| 700 | ret = -1; |
| 701 | goto end; |
| 702 | } |
| 703 | |
| 704 | bt_object_set_parent(event, stream); |
| 705 | ret = auto_populate_event_header(stream, event); |
| 706 | if (ret) { |
| 707 | goto error; |
| 708 | } |
| 709 | |
| 710 | /* Make sure the various scopes of the event are set */ |
| 711 | ret = bt_ctf_event_validate(event); |
| 712 | if (ret) { |
| 713 | goto error; |
| 714 | } |
| 715 | |
| 716 | /* Save the new event and freeze it */ |
| 717 | bt_ctf_event_freeze(event); |
| 718 | g_ptr_array_add(stream->events, event); |
| 719 | |
| 720 | /* |
| 721 | * Event had to hold a reference to its event class as long as it wasn't |
| 722 | * part of the same trace hierarchy. From now on, the event and its |
| 723 | * class share the same lifetime guarantees and the reference is no |
| 724 | * longer needed. |
| 725 | */ |
| 726 | bt_put(event->event_class); |
| 727 | |
| 728 | end: |
| 729 | return ret; |
| 730 | |
| 731 | error: |
| 732 | /* |
| 733 | * Orphan the event; we were not successful in associating it to |
| 734 | * a stream. |
| 735 | */ |
| 736 | bt_object_set_parent(event, NULL); |
| 737 | |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
| 742 | struct bt_ctf_stream *stream) |
| 743 | { |
| 744 | struct bt_ctf_field *packet_context = NULL; |
| 745 | |
| 746 | if (!stream || stream->pos.fd < 0) { |
| 747 | goto end; |
| 748 | } |
| 749 | |
| 750 | packet_context = stream->packet_context; |
| 751 | if (packet_context) { |
| 752 | bt_get(packet_context); |
| 753 | } |
| 754 | end: |
| 755 | return packet_context; |
| 756 | } |
| 757 | |
| 758 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, |
| 759 | struct bt_ctf_field *field) |
| 760 | { |
| 761 | int ret = 0; |
| 762 | struct bt_ctf_field_type *field_type; |
| 763 | |
| 764 | if (!stream || stream->pos.fd < 0) { |
| 765 | ret = -1; |
| 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | field_type = bt_ctf_field_get_type(field); |
| 770 | if (bt_ctf_field_type_compare(field_type, |
| 771 | stream->stream_class->packet_context_type)) { |
| 772 | ret = -1; |
| 773 | goto end; |
| 774 | } |
| 775 | |
| 776 | bt_put(field_type); |
| 777 | bt_put(stream->packet_context); |
| 778 | stream->packet_context = bt_get(field); |
| 779 | end: |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
| 784 | struct bt_ctf_stream *stream) |
| 785 | { |
| 786 | struct bt_ctf_field *packet_header = NULL; |
| 787 | |
| 788 | if (!stream || stream->pos.fd < 0) { |
| 789 | goto end; |
| 790 | } |
| 791 | |
| 792 | packet_header = stream->packet_header; |
| 793 | if (packet_header) { |
| 794 | bt_get(packet_header); |
| 795 | } |
| 796 | end: |
| 797 | return packet_header; |
| 798 | } |
| 799 | |
| 800 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, |
| 801 | struct bt_ctf_field *field) |
| 802 | { |
| 803 | int ret = 0; |
| 804 | struct bt_ctf_trace *trace = NULL; |
| 805 | struct bt_ctf_field_type *field_type = NULL; |
| 806 | |
| 807 | if (!stream || stream->pos.fd < 0) { |
| 808 | ret = -1; |
| 809 | goto end; |
| 810 | } |
| 811 | |
| 812 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
| 813 | field_type = bt_ctf_field_get_type(field); |
| 814 | if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) { |
| 815 | ret = -1; |
| 816 | goto end; |
| 817 | } |
| 818 | |
| 819 | bt_put(stream->packet_header); |
| 820 | stream->packet_header = bt_get(field); |
| 821 | end: |
| 822 | BT_PUT(trace); |
| 823 | bt_put(field_type); |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | static |
| 828 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) |
| 829 | { |
| 830 | int ret = 0; |
| 831 | struct bt_ctf_field *timestamp_field = NULL; |
| 832 | struct bt_ctf_field_type *timestamp_field_type = NULL; |
| 833 | |
| 834 | timestamp_field = bt_ctf_field_structure_get_field(event_header, |
| 835 | "timestamp"); |
| 836 | if (!timestamp_field) { |
| 837 | ret = -1; |
| 838 | goto end; |
| 839 | } |
| 840 | |
| 841 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); |
| 842 | assert(timestamp_field_type); |
| 843 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != |
| 844 | BT_CTF_TYPE_ID_INTEGER) { |
| 845 | ret = -1; |
| 846 | goto end; |
| 847 | } |
| 848 | |
| 849 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { |
| 850 | int64_t val; |
| 851 | |
| 852 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, |
| 853 | &val); |
| 854 | if (ret) { |
| 855 | goto end; |
| 856 | } |
| 857 | *timestamp = (uint64_t) val; |
| 858 | } else { |
| 859 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, |
| 860 | timestamp); |
| 861 | if (ret) { |
| 862 | goto end; |
| 863 | } |
| 864 | } |
| 865 | end: |
| 866 | bt_put(timestamp_field); |
| 867 | bt_put(timestamp_field_type); |
| 868 | return ret; |
| 869 | } |
| 870 | |
| 871 | static |
| 872 | void reset_structure_field(struct bt_ctf_field *structure, const char *name) |
| 873 | { |
| 874 | struct bt_ctf_field *member; |
| 875 | |
| 876 | member = bt_ctf_field_structure_get_field(structure, name); |
| 877 | assert(member); |
| 878 | (void) bt_ctf_field_reset(member); |
| 879 | bt_put(member); |
| 880 | } |
| 881 | |
| 882 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
| 883 | { |
| 884 | int ret = 0; |
| 885 | size_t i; |
| 886 | uint64_t timestamp_begin, timestamp_end; |
| 887 | struct bt_ctf_field *integer = NULL; |
| 888 | struct ctf_stream_pos packet_context_pos; |
| 889 | bool empty_packet; |
| 890 | uint64_t packet_size_bits; |
| 891 | struct { |
| 892 | bool timestamp_begin; |
| 893 | bool timestamp_end; |
| 894 | bool content_size; |
| 895 | bool packet_size; |
| 896 | } auto_set_fields = { 0 }; |
| 897 | |
| 898 | if (!stream || stream->pos.fd < 0) { |
| 899 | /* |
| 900 | * Stream does not have an associated fd. It is, |
| 901 | * therefore, not a stream being used to write events. |
| 902 | */ |
| 903 | ret = -1; |
| 904 | goto end; |
| 905 | } |
| 906 | |
| 907 | if (!stream->packet_context && stream->flushed_packet_count > 0) { |
| 908 | /* |
| 909 | * A stream without a packet context, and thus without |
| 910 | * content and packet size members, can't have more than |
| 911 | * one packet. |
| 912 | */ |
| 913 | ret = -1; |
| 914 | goto end; |
| 915 | } |
| 916 | |
| 917 | empty_packet = (stream->events->len == 0); |
| 918 | |
| 919 | /* mmap the next packet */ |
| 920 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); |
| 921 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos); |
| 922 | if (ret) { |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | if (stream->packet_context) { |
| 927 | /* Set the default context attributes if present and unset. */ |
| 928 | if (!empty_packet && !get_event_header_timestamp( |
| 929 | ((struct bt_ctf_event *) g_ptr_array_index( |
| 930 | stream->events, 0))->event_header, ×tamp_begin)) { |
| 931 | ret = try_set_structure_field_integer( |
| 932 | stream->packet_context, |
| 933 | "timestamp_begin", timestamp_begin); |
| 934 | if (ret < 0) { |
| 935 | goto end; |
| 936 | } |
| 937 | auto_set_fields.timestamp_begin = ret == 1; |
| 938 | } |
| 939 | |
| 940 | if (!empty_packet && !get_event_header_timestamp( |
| 941 | ((struct bt_ctf_event *) g_ptr_array_index( |
| 942 | stream->events, stream->events->len - 1))->event_header, |
| 943 | ×tamp_end)) { |
| 944 | |
| 945 | ret = try_set_structure_field_integer( |
| 946 | stream->packet_context, |
| 947 | "timestamp_end", timestamp_end); |
| 948 | if (ret < 0) { |
| 949 | goto end; |
| 950 | } |
| 951 | auto_set_fields.timestamp_end = ret == 1; |
| 952 | } |
| 953 | ret = try_set_structure_field_integer(stream->packet_context, |
| 954 | "content_size", UINT64_MAX); |
| 955 | if (ret < 0) { |
| 956 | goto end; |
| 957 | } |
| 958 | auto_set_fields.content_size = ret == 1; |
| 959 | |
| 960 | ret = try_set_structure_field_integer(stream->packet_context, |
| 961 | "packet_size", UINT64_MAX); |
| 962 | if (ret < 0) { |
| 963 | goto end; |
| 964 | } |
| 965 | auto_set_fields.packet_size = ret == 1; |
| 966 | |
| 967 | /* Write packet context */ |
| 968 | memcpy(&packet_context_pos, &stream->pos, |
| 969 | sizeof(struct ctf_stream_pos)); |
| 970 | ret = bt_ctf_field_serialize(stream->packet_context, |
| 971 | &stream->pos); |
| 972 | if (ret) { |
| 973 | goto end; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | for (i = 0; i < stream->events->len; i++) { |
| 978 | struct bt_ctf_event *event = g_ptr_array_index( |
| 979 | stream->events, i); |
| 980 | |
| 981 | /* Write event header */ |
| 982 | ret = bt_ctf_field_serialize(event->event_header, |
| 983 | &stream->pos); |
| 984 | if (ret) { |
| 985 | goto end; |
| 986 | } |
| 987 | |
| 988 | /* Write stream event context */ |
| 989 | if (event->stream_event_context) { |
| 990 | ret = bt_ctf_field_serialize( |
| 991 | event->stream_event_context, &stream->pos); |
| 992 | if (ret) { |
| 993 | goto end; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /* Write event content */ |
| 998 | ret = bt_ctf_event_serialize(event, &stream->pos); |
| 999 | if (ret) { |
| 1000 | goto end; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /* Rounded-up in case content_size is not byte-aligned. */ |
| 1005 | packet_size_bits = (stream->pos.offset + (CHAR_BIT - 1)) & |
| 1006 | ~(CHAR_BIT - 1); |
| 1007 | stream->pos.packet_size = packet_size_bits; |
| 1008 | |
| 1009 | if (stream->packet_context) { |
| 1010 | /* |
| 1011 | * Update the packet total size and content size and overwrite |
| 1012 | * the packet context. |
| 1013 | * Copy base_mma as the packet may have been remapped (e.g. when |
| 1014 | * a packet is resized). |
| 1015 | */ |
| 1016 | packet_context_pos.base_mma = stream->pos.base_mma; |
| 1017 | if (auto_set_fields.content_size) { |
| 1018 | ret = set_structure_field_integer( |
| 1019 | stream->packet_context, |
| 1020 | "content_size", stream->pos.offset); |
| 1021 | if (ret < 0) { |
| 1022 | goto end; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if (auto_set_fields.packet_size) { |
| 1027 | ret = set_structure_field_integer(stream->packet_context, |
| 1028 | "packet_size", packet_size_bits); |
| 1029 | if (ret < 0) { |
| 1030 | goto end; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | ret = bt_ctf_field_serialize(stream->packet_context, |
| 1035 | &packet_context_pos); |
| 1036 | if (ret) { |
| 1037 | goto end; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | g_ptr_array_set_size(stream->events, 0); |
| 1042 | stream->flushed_packet_count++; |
| 1043 | stream->size += packet_size_bits / CHAR_BIT; |
| 1044 | end: |
| 1045 | /* Reset automatically-set fields. */ |
| 1046 | if (auto_set_fields.timestamp_begin) { |
| 1047 | reset_structure_field(stream->packet_context, |
| 1048 | "timestamp_begin"); |
| 1049 | } |
| 1050 | if (auto_set_fields.timestamp_end) { |
| 1051 | reset_structure_field(stream->packet_context, |
| 1052 | "timestamp_end"); |
| 1053 | } |
| 1054 | if (auto_set_fields.packet_size) { |
| 1055 | reset_structure_field(stream->packet_context, |
| 1056 | "packet_size"); |
| 1057 | } |
| 1058 | if (auto_set_fields.content_size) { |
| 1059 | reset_structure_field(stream->packet_context, |
| 1060 | "content_size"); |
| 1061 | } |
| 1062 | bt_put(integer); |
| 1063 | |
| 1064 | if (ret < 0) { |
| 1065 | /* |
| 1066 | * We failed to write the packet. Its size is therefore set to 0 |
| 1067 | * to ensure the next mapping is done in the same place rather |
| 1068 | * than advancing by "stream->pos.packet_size", which would |
| 1069 | * leave a corrupted packet in the trace. |
| 1070 | */ |
| 1071 | stream->pos.packet_size = 0; |
| 1072 | } |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) |
| 1077 | { |
| 1078 | bt_get(stream); |
| 1079 | } |
| 1080 | |
| 1081 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) |
| 1082 | { |
| 1083 | bt_put(stream); |
| 1084 | } |
| 1085 | |
| 1086 | static |
| 1087 | void bt_ctf_stream_destroy(struct bt_object *obj) |
| 1088 | { |
| 1089 | struct bt_ctf_stream *stream; |
| 1090 | |
| 1091 | stream = container_of(obj, struct bt_ctf_stream, base); |
| 1092 | ctf_fini_pos(&stream->pos); |
| 1093 | if (stream->pos.fd >= 0) { |
| 1094 | int ret; |
| 1095 | |
| 1096 | /* |
| 1097 | * Truncate the file's size to the minimum required to fit the |
| 1098 | * last packet as we might have grown it too much on the last |
| 1099 | * mmap. |
| 1100 | */ |
| 1101 | do { |
| 1102 | ret = ftruncate(stream->pos.fd, stream->size); |
| 1103 | } while (ret == -1 && errno == EINTR); |
| 1104 | if (ret) { |
| 1105 | perror("ftruncate"); |
| 1106 | } |
| 1107 | |
| 1108 | if (close(stream->pos.fd)) { |
| 1109 | perror("close"); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | if (stream->events) { |
| 1114 | g_ptr_array_free(stream->events, TRUE); |
| 1115 | } |
| 1116 | |
| 1117 | if (stream->name) { |
| 1118 | g_string_free(stream->name, TRUE); |
| 1119 | } |
| 1120 | |
| 1121 | bt_put(stream->packet_header); |
| 1122 | bt_put(stream->packet_context); |
| 1123 | g_free(stream); |
| 1124 | } |
| 1125 | |
| 1126 | static |
| 1127 | int _set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
| 1128 | uint64_t value, bool force) |
| 1129 | { |
| 1130 | int ret = 0; |
| 1131 | struct bt_ctf_field_type *field_type = NULL; |
| 1132 | struct bt_ctf_field *integer = |
| 1133 | bt_ctf_field_structure_get_field(structure, name); |
| 1134 | |
| 1135 | if (!structure || !name) { |
| 1136 | ret = -1; |
| 1137 | goto end; |
| 1138 | } |
| 1139 | |
| 1140 | if (!integer) { |
| 1141 | /* Field not found, not an error. */ |
| 1142 | goto end; |
| 1143 | } |
| 1144 | |
| 1145 | /* Make sure the payload has not already been set. */ |
| 1146 | if (!force && bt_ctf_field_is_set(integer)) { |
| 1147 | /* Payload already set, not an error */ |
| 1148 | goto end; |
| 1149 | } |
| 1150 | |
| 1151 | field_type = bt_ctf_field_get_type(integer); |
| 1152 | assert(field_type); |
| 1153 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) { |
| 1154 | /* |
| 1155 | * The user most likely meant for us to populate this field |
| 1156 | * automatically. However, we can only do this if the field |
| 1157 | * is an integer. Return an error. |
| 1158 | */ |
| 1159 | ret = -1; |
| 1160 | goto end; |
| 1161 | } |
| 1162 | |
| 1163 | if (bt_ctf_field_type_integer_get_signed(field_type)) { |
| 1164 | ret = bt_ctf_field_signed_integer_set_value(integer, |
| 1165 | (int64_t) value); |
| 1166 | } else { |
| 1167 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); |
| 1168 | } |
| 1169 | ret = !ret ? 1 : ret; |
| 1170 | end: |
| 1171 | bt_put(integer); |
| 1172 | bt_put(field_type); |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
| 1176 | static |
| 1177 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
| 1178 | uint64_t value) |
| 1179 | { |
| 1180 | return _set_structure_field_integer(structure, name, value, true); |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Returns the following codes: |
| 1185 | * 1 if the field was found and set, |
| 1186 | * 0 if nothing was done (field not found, or was already set), |
| 1187 | * <0 if an error was encoutered |
| 1188 | */ |
| 1189 | static |
| 1190 | int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
| 1191 | uint64_t value) |
| 1192 | { |
| 1193 | return _set_structure_field_integer(structure, name, value, false); |
| 1194 | } |
| 1195 | |
| 1196 | const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) |
| 1197 | { |
| 1198 | const char *name = NULL; |
| 1199 | |
| 1200 | if (!stream) { |
| 1201 | goto end; |
| 1202 | } |
| 1203 | |
| 1204 | name = stream->name ? stream->name->str : NULL; |
| 1205 | |
| 1206 | end: |
| 1207 | return name; |
| 1208 | } |
| 1209 | |
| 1210 | int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream) |
| 1211 | { |
| 1212 | int ret = -1; |
| 1213 | |
| 1214 | if (!stream) { |
| 1215 | goto end; |
| 1216 | } |
| 1217 | |
| 1218 | ret = (stream->pos.fd >= 0); |
| 1219 | |
| 1220 | end: |
| 1221 | return ret; |
| 1222 | } |