| 1 | /* |
| 2 | * copytrace.c |
| 3 | * |
| 4 | * Babeltrace library to create a copy of a CTF trace |
| 5 | * |
| 6 | * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com> |
| 7 | * |
| 8 | * Author: Julien Desfossez <jdesfossez@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/event.h> |
| 30 | #include <babeltrace/ctf-ir/packet.h> |
| 31 | #include <babeltrace/ctf-ir/event-class.h> |
| 32 | #include <babeltrace/ctf-ir/stream.h> |
| 33 | #include <babeltrace/ctf-ir/stream-class.h> |
| 34 | #include <babeltrace/ctf-ir/clock-class.h> |
| 35 | #include <babeltrace/ctf-ir/fields.h> |
| 36 | #include <babeltrace/ctf-writer/stream.h> |
| 37 | #include <assert.h> |
| 38 | |
| 39 | #include "ctfcopytrace.h" |
| 40 | #include "clock-fields.h" |
| 41 | |
| 42 | BT_HIDDEN |
| 43 | struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err, |
| 44 | struct bt_ctf_clock_class *clock_class) |
| 45 | { |
| 46 | int64_t offset, offset_s; |
| 47 | int int_ret; |
| 48 | uint64_t u64_ret; |
| 49 | const char *name, *description; |
| 50 | struct bt_ctf_clock_class *writer_clock_class = NULL; |
| 51 | |
| 52 | assert(err && clock_class); |
| 53 | |
| 54 | name = bt_ctf_clock_class_get_name(clock_class); |
| 55 | if (!name) { |
| 56 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 57 | __LINE__); |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | writer_clock_class = bt_ctf_clock_class_create(name); |
| 62 | if (!writer_clock_class) { |
| 63 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 64 | __LINE__); |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | description = bt_ctf_clock_class_get_description(clock_class); |
| 69 | if (!description) { |
| 70 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 71 | __LINE__); |
| 72 | goto end_destroy; |
| 73 | } |
| 74 | |
| 75 | int_ret = bt_ctf_clock_class_set_description(writer_clock_class, |
| 76 | description); |
| 77 | if (int_ret != 0) { |
| 78 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 79 | __LINE__); |
| 80 | goto end_destroy; |
| 81 | } |
| 82 | |
| 83 | u64_ret = bt_ctf_clock_class_get_frequency(clock_class); |
| 84 | if (u64_ret == -1ULL) { |
| 85 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 86 | __LINE__); |
| 87 | goto end_destroy; |
| 88 | } |
| 89 | int_ret = bt_ctf_clock_class_set_frequency(writer_clock_class, u64_ret); |
| 90 | if (int_ret != 0) { |
| 91 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 92 | __LINE__); |
| 93 | goto end_destroy; |
| 94 | } |
| 95 | |
| 96 | u64_ret = bt_ctf_clock_class_get_precision(clock_class); |
| 97 | if (u64_ret == -1ULL) { |
| 98 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 99 | __LINE__); |
| 100 | goto end_destroy; |
| 101 | } |
| 102 | int_ret = bt_ctf_clock_class_set_precision(writer_clock_class, |
| 103 | u64_ret); |
| 104 | if (int_ret != 0) { |
| 105 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 106 | __LINE__); |
| 107 | goto end_destroy; |
| 108 | } |
| 109 | |
| 110 | int_ret = bt_ctf_clock_class_get_offset_s(clock_class, &offset_s); |
| 111 | if (int_ret != 0) { |
| 112 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 113 | __LINE__); |
| 114 | goto end_destroy; |
| 115 | } |
| 116 | |
| 117 | int_ret = bt_ctf_clock_class_set_offset_s(writer_clock_class, offset_s); |
| 118 | if (int_ret != 0) { |
| 119 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 120 | __LINE__); |
| 121 | goto end_destroy; |
| 122 | } |
| 123 | |
| 124 | int_ret = bt_ctf_clock_class_get_offset_cycles(clock_class, &offset); |
| 125 | if (int_ret != 0) { |
| 126 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 127 | __LINE__); |
| 128 | goto end_destroy; |
| 129 | } |
| 130 | |
| 131 | int_ret = bt_ctf_clock_class_set_offset_cycles(writer_clock_class, offset); |
| 132 | if (int_ret != 0) { |
| 133 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 134 | __LINE__); |
| 135 | goto end_destroy; |
| 136 | } |
| 137 | |
| 138 | int_ret = bt_ctf_clock_class_is_absolute(clock_class); |
| 139 | if (int_ret == -1) { |
| 140 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 141 | __LINE__); |
| 142 | goto end_destroy; |
| 143 | } |
| 144 | |
| 145 | int_ret = bt_ctf_clock_class_set_is_absolute(writer_clock_class, int_ret); |
| 146 | if (int_ret != 0) { |
| 147 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 148 | __LINE__); |
| 149 | goto end_destroy; |
| 150 | } |
| 151 | |
| 152 | goto end; |
| 153 | |
| 154 | end_destroy: |
| 155 | BT_PUT(writer_clock_class); |
| 156 | end: |
| 157 | return writer_clock_class; |
| 158 | } |
| 159 | |
| 160 | BT_HIDDEN |
| 161 | enum bt_component_status ctf_copy_clock_classes(FILE *err, |
| 162 | struct bt_ctf_trace *writer_trace, |
| 163 | struct bt_ctf_stream_class *writer_stream_class, |
| 164 | struct bt_ctf_trace *trace) |
| 165 | { |
| 166 | enum bt_component_status ret; |
| 167 | int int_ret, clock_class_count, i; |
| 168 | |
| 169 | clock_class_count = bt_ctf_trace_get_clock_class_count(trace); |
| 170 | |
| 171 | for (i = 0; i < clock_class_count; i++) { |
| 172 | struct bt_ctf_clock_class *writer_clock_class; |
| 173 | struct bt_ctf_clock_class *clock_class = |
| 174 | bt_ctf_trace_get_clock_class_by_index(trace, i); |
| 175 | |
| 176 | if (!clock_class) { |
| 177 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 178 | __LINE__); |
| 179 | ret = BT_COMPONENT_STATUS_ERROR; |
| 180 | goto end; |
| 181 | } |
| 182 | |
| 183 | writer_clock_class = ctf_copy_clock_class(err, clock_class); |
| 184 | bt_put(clock_class); |
| 185 | if (!writer_clock_class) { |
| 186 | fprintf(err, "Failed to copy clock class"); |
| 187 | ret = BT_COMPONENT_STATUS_ERROR; |
| 188 | goto end; |
| 189 | } |
| 190 | |
| 191 | int_ret = bt_ctf_trace_add_clock_class(writer_trace, writer_clock_class); |
| 192 | if (int_ret != 0) { |
| 193 | BT_PUT(writer_clock_class); |
| 194 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 195 | __LINE__); |
| 196 | ret = BT_COMPONENT_STATUS_ERROR; |
| 197 | goto end; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Ownership transferred to the trace. |
| 202 | */ |
| 203 | bt_put(writer_clock_class); |
| 204 | } |
| 205 | |
| 206 | ret = BT_COMPONENT_STATUS_OK; |
| 207 | |
| 208 | end: |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | BT_HIDDEN |
| 213 | struct bt_ctf_event_class *ctf_copy_event_class(FILE *err, |
| 214 | struct bt_ctf_event_class *event_class) |
| 215 | { |
| 216 | struct bt_ctf_event_class *writer_event_class = NULL; |
| 217 | const char *name; |
| 218 | int count, i; |
| 219 | |
| 220 | name = bt_ctf_event_class_get_name(event_class); |
| 221 | if (!name) { |
| 222 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 223 | __FILE__, __LINE__); |
| 224 | goto end; |
| 225 | } |
| 226 | |
| 227 | writer_event_class = bt_ctf_event_class_create(name); |
| 228 | if (!writer_event_class) { |
| 229 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 230 | __FILE__, __LINE__); |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | count = bt_ctf_event_class_get_attribute_count(event_class); |
| 235 | for (i = 0; i < count; i++) { |
| 236 | const char *attr_name; |
| 237 | struct bt_value *attr_value; |
| 238 | int ret; |
| 239 | |
| 240 | attr_name = bt_ctf_event_class_get_attribute_name_by_index( |
| 241 | event_class, i); |
| 242 | if (!attr_name) { |
| 243 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 244 | __FILE__, __LINE__); |
| 245 | goto error; |
| 246 | } |
| 247 | attr_value = bt_ctf_event_class_get_attribute_value_by_index( |
| 248 | event_class, i); |
| 249 | if (!attr_value) { |
| 250 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 251 | __FILE__, __LINE__); |
| 252 | goto error; |
| 253 | } |
| 254 | |
| 255 | ret = bt_ctf_event_class_set_attribute(writer_event_class, |
| 256 | attr_name, attr_value); |
| 257 | BT_PUT(attr_value); |
| 258 | if (ret < 0) { |
| 259 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 260 | __FILE__, __LINE__); |
| 261 | goto error; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | count = bt_ctf_event_class_get_payload_type_field_count(event_class); |
| 266 | for (i = 0; i < count; i++) { |
| 267 | const char *field_name; |
| 268 | struct bt_ctf_field_type *field_type; |
| 269 | int ret; |
| 270 | |
| 271 | ret = bt_ctf_event_class_get_payload_type_field_by_index( |
| 272 | event_class, &field_name, &field_type, i); |
| 273 | if (ret < 0) { |
| 274 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 275 | goto error; |
| 276 | } |
| 277 | |
| 278 | ret = bt_ctf_event_class_add_field(writer_event_class, field_type, |
| 279 | field_name); |
| 280 | BT_PUT(field_type); |
| 281 | if (ret < 0) { |
| 282 | fprintf(err, "[error] Cannot add field %s\n", field_name); |
| 283 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 284 | goto error; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | goto end; |
| 289 | |
| 290 | error: |
| 291 | BT_PUT(writer_event_class); |
| 292 | end: |
| 293 | return writer_event_class; |
| 294 | } |
| 295 | |
| 296 | BT_HIDDEN |
| 297 | enum bt_component_status ctf_copy_event_classes(FILE *err, |
| 298 | struct bt_ctf_stream_class *stream_class, |
| 299 | struct bt_ctf_stream_class *writer_stream_class) |
| 300 | { |
| 301 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 302 | struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL; |
| 303 | int count, i; |
| 304 | |
| 305 | count = bt_ctf_stream_class_get_event_class_count(stream_class); |
| 306 | if (count < 0) { |
| 307 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 308 | __FILE__, __LINE__); |
| 309 | goto end; |
| 310 | } |
| 311 | |
| 312 | for (i = 0; i < count; i++) { |
| 313 | struct bt_ctf_field_type *context; |
| 314 | int int_ret; |
| 315 | |
| 316 | event_class = bt_ctf_stream_class_get_event_class_by_index( |
| 317 | stream_class, i); |
| 318 | if (!event_class) { |
| 319 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 320 | __FILE__, __LINE__); |
| 321 | ret = BT_COMPONENT_STATUS_ERROR; |
| 322 | goto error; |
| 323 | } |
| 324 | writer_event_class = ctf_copy_event_class(err, event_class); |
| 325 | if (!writer_event_class) { |
| 326 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 327 | __FILE__, __LINE__); |
| 328 | ret = BT_COMPONENT_STATUS_ERROR; |
| 329 | goto error; |
| 330 | } |
| 331 | |
| 332 | context = bt_ctf_event_class_get_context_type(event_class); |
| 333 | if (!context) { |
| 334 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 335 | __FILE__, __LINE__); |
| 336 | ret = BT_COMPONENT_STATUS_ERROR; |
| 337 | goto error; |
| 338 | } |
| 339 | ret = bt_ctf_event_class_set_context_type(writer_event_class, context); |
| 340 | BT_PUT(context); |
| 341 | if (ret < 0) { |
| 342 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 343 | __LINE__); |
| 344 | goto error; |
| 345 | } |
| 346 | |
| 347 | int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class, |
| 348 | writer_event_class); |
| 349 | if (int_ret < 0) { |
| 350 | fprintf(err, "[error] Failed to add event class\n"); |
| 351 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 352 | __FILE__, __LINE__); |
| 353 | ret = BT_COMPONENT_STATUS_ERROR; |
| 354 | goto error; |
| 355 | } |
| 356 | BT_PUT(writer_event_class); |
| 357 | BT_PUT(event_class); |
| 358 | } |
| 359 | |
| 360 | goto end; |
| 361 | |
| 362 | error: |
| 363 | bt_put(event_class); |
| 364 | bt_put(writer_event_class); |
| 365 | end: |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | BT_HIDDEN |
| 370 | struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err, |
| 371 | struct bt_ctf_stream_class *stream_class, |
| 372 | struct bt_ctf_trace *writer_trace, |
| 373 | bool override_ts64) |
| 374 | { |
| 375 | struct bt_ctf_field_type *type = NULL; |
| 376 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 377 | int ret_int; |
| 378 | const char *name = bt_ctf_stream_class_get_name(stream_class); |
| 379 | |
| 380 | if (strlen(name) == 0) { |
| 381 | name = NULL; |
| 382 | } |
| 383 | |
| 384 | writer_stream_class = bt_ctf_stream_class_create(name); |
| 385 | if (!writer_stream_class) { |
| 386 | fprintf(err, "[error] %s in %s:%d\n", |
| 387 | __func__, __FILE__, __LINE__); |
| 388 | goto end; |
| 389 | } |
| 390 | |
| 391 | type = bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 392 | if (!type) { |
| 393 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 394 | __LINE__); |
| 395 | goto error; |
| 396 | } |
| 397 | |
| 398 | ret_int = bt_ctf_stream_class_set_packet_context_type( |
| 399 | writer_stream_class, type); |
| 400 | if (ret_int < 0) { |
| 401 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 402 | __LINE__); |
| 403 | goto error; |
| 404 | } |
| 405 | BT_PUT(type); |
| 406 | |
| 407 | type = bt_ctf_stream_class_get_event_header_type(stream_class); |
| 408 | if (!type) { |
| 409 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 410 | __LINE__); |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | if (override_ts64) { |
| 415 | struct bt_ctf_field_type *new_event_header_type; |
| 416 | |
| 417 | new_event_header_type = override_header_type(err, type, |
| 418 | writer_trace); |
| 419 | if (!new_event_header_type) { |
| 420 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 421 | __LINE__); |
| 422 | goto error; |
| 423 | } |
| 424 | ret_int = bt_ctf_stream_class_set_event_header_type( |
| 425 | writer_stream_class, new_event_header_type); |
| 426 | BT_PUT(new_event_header_type); |
| 427 | if (ret_int < 0) { |
| 428 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 429 | __LINE__); |
| 430 | goto error; |
| 431 | } |
| 432 | } else { |
| 433 | ret_int = bt_ctf_stream_class_set_event_header_type( |
| 434 | writer_stream_class, type); |
| 435 | if (ret_int < 0) { |
| 436 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 437 | __LINE__); |
| 438 | goto error; |
| 439 | } |
| 440 | } |
| 441 | BT_PUT(type); |
| 442 | |
| 443 | type = bt_ctf_stream_class_get_event_context_type(stream_class); |
| 444 | if (type) { |
| 445 | ret_int = bt_ctf_stream_class_set_event_context_type( |
| 446 | writer_stream_class, type); |
| 447 | if (ret_int < 0) { |
| 448 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 449 | __LINE__); |
| 450 | goto error; |
| 451 | } |
| 452 | } |
| 453 | BT_PUT(type); |
| 454 | |
| 455 | goto end; |
| 456 | |
| 457 | error: |
| 458 | BT_PUT(writer_stream_class); |
| 459 | end: |
| 460 | bt_put(type); |
| 461 | return writer_stream_class; |
| 462 | } |
| 463 | |
| 464 | BT_HIDDEN |
| 465 | enum bt_component_status ctf_copy_packet_context_field(FILE *err, |
| 466 | struct bt_ctf_field *field, const char *field_name, |
| 467 | struct bt_ctf_field *writer_packet_context, |
| 468 | struct bt_ctf_field_type *writer_packet_context_type) |
| 469 | { |
| 470 | enum bt_component_status ret; |
| 471 | struct bt_ctf_field *writer_field = NULL; |
| 472 | struct bt_ctf_field_type *field_type = NULL; |
| 473 | int int_ret; |
| 474 | uint64_t value; |
| 475 | |
| 476 | field_type = bt_ctf_field_get_type(field); |
| 477 | if (!field_type) { |
| 478 | ret = BT_COMPONENT_STATUS_ERROR; |
| 479 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 480 | __LINE__); |
| 481 | goto end; |
| 482 | } |
| 483 | /* |
| 484 | * Only support for integers for now. |
| 485 | */ |
| 486 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 487 | fprintf(err, "[error] Unsupported packet context field type\n"); |
| 488 | ret = BT_COMPONENT_STATUS_ERROR; |
| 489 | goto error; |
| 490 | } |
| 491 | BT_PUT(field_type); |
| 492 | |
| 493 | writer_field = bt_ctf_field_structure_get_field_by_name( |
| 494 | writer_packet_context, field_name); |
| 495 | if (!writer_field) { |
| 496 | ret = BT_COMPONENT_STATUS_ERROR; |
| 497 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 498 | __LINE__); |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | int_ret = bt_ctf_field_unsigned_integer_get_value(field, &value); |
| 503 | if (int_ret < 0) { |
| 504 | fprintf(err, "[error] Wrong packet_context field type\n"); |
| 505 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 506 | __LINE__); |
| 507 | ret = BT_COMPONENT_STATUS_ERROR; |
| 508 | goto end; |
| 509 | } |
| 510 | |
| 511 | int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value); |
| 512 | if (int_ret < 0) { |
| 513 | ret = BT_COMPONENT_STATUS_ERROR; |
| 514 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 515 | __LINE__); |
| 516 | goto end; |
| 517 | } |
| 518 | |
| 519 | ret = BT_COMPONENT_STATUS_OK; |
| 520 | |
| 521 | goto end; |
| 522 | |
| 523 | error: |
| 524 | bt_put(field_type); |
| 525 | end: |
| 526 | bt_put(writer_field); |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | BT_HIDDEN |
| 531 | struct bt_ctf_field *ctf_copy_packet_context(FILE *err, |
| 532 | struct bt_ctf_packet *packet, |
| 533 | struct bt_ctf_stream *writer_stream) |
| 534 | { |
| 535 | enum bt_component_status ret; |
| 536 | struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL; |
| 537 | struct bt_ctf_field_type *struct_type = NULL, *writer_packet_context_type = NULL; |
| 538 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 539 | struct bt_ctf_field *field = NULL; |
| 540 | struct bt_ctf_field_type *field_type; |
| 541 | int nr_fields, i; |
| 542 | |
| 543 | packet_context = bt_ctf_packet_get_context(packet); |
| 544 | if (!packet_context) { |
| 545 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 546 | __LINE__); |
| 547 | goto error; |
| 548 | } |
| 549 | |
| 550 | writer_stream_class = bt_ctf_stream_get_class(writer_stream); |
| 551 | if (!writer_stream_class) { |
| 552 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 553 | __LINE__); |
| 554 | goto error; |
| 555 | } |
| 556 | |
| 557 | writer_packet_context_type = bt_ctf_stream_class_get_packet_context_type( |
| 558 | writer_stream_class); |
| 559 | BT_PUT(writer_stream_class); |
| 560 | if (!writer_packet_context_type) { |
| 561 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 562 | __LINE__); |
| 563 | goto error; |
| 564 | } |
| 565 | |
| 566 | struct_type = bt_ctf_field_get_type(packet_context); |
| 567 | if (!struct_type) { |
| 568 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 569 | __LINE__); |
| 570 | goto error; |
| 571 | } |
| 572 | |
| 573 | writer_packet_context = bt_ctf_field_create(writer_packet_context_type); |
| 574 | if (!writer_packet_context) { |
| 575 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 576 | __LINE__); |
| 577 | goto error; |
| 578 | } |
| 579 | |
| 580 | nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type); |
| 581 | for (i = 0; i < nr_fields; i++) { |
| 582 | const char *field_name; |
| 583 | |
| 584 | field = bt_ctf_field_structure_get_field_by_index( |
| 585 | packet_context, i); |
| 586 | if (!field) { |
| 587 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 588 | __FILE__, __LINE__); |
| 589 | goto error; |
| 590 | } |
| 591 | if (bt_ctf_field_type_structure_get_field_by_index(struct_type, |
| 592 | &field_name, &field_type, i) < 0) { |
| 593 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 594 | __FILE__, __LINE__); |
| 595 | goto error; |
| 596 | } |
| 597 | if (!strncmp(field_name, "content_size", strlen("content_size")) || |
| 598 | !strncmp(field_name, "packet_size", |
| 599 | strlen("packet_size"))) { |
| 600 | BT_PUT(field_type); |
| 601 | BT_PUT(field); |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 606 | fprintf(err, "[error] Unexpected packet context field type\n"); |
| 607 | goto error; |
| 608 | } |
| 609 | |
| 610 | ret = ctf_copy_packet_context_field(err, field, field_name, |
| 611 | writer_packet_context, writer_packet_context_type); |
| 612 | BT_PUT(field_type); |
| 613 | BT_PUT(field); |
| 614 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 615 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 616 | __FILE__, __LINE__); |
| 617 | goto error; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | goto end; |
| 622 | |
| 623 | error: |
| 624 | BT_PUT(writer_packet_context); |
| 625 | end: |
| 626 | bt_put(field); |
| 627 | bt_put(field_type); |
| 628 | bt_put(struct_type); |
| 629 | bt_put(writer_packet_context_type); |
| 630 | bt_put(writer_stream_class); |
| 631 | bt_put(packet_context); |
| 632 | return writer_packet_context; |
| 633 | } |
| 634 | |
| 635 | BT_HIDDEN |
| 636 | int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event, |
| 637 | struct bt_ctf_event_class *writer_event_class, |
| 638 | struct bt_ctf_event *writer_event, |
| 639 | struct bt_ctf_field *event_header) |
| 640 | { |
| 641 | struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL; |
| 642 | struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL; |
| 643 | |
| 644 | int ret; |
| 645 | struct bt_ctf_field *writer_event_header = NULL; |
| 646 | uint64_t value; |
| 647 | |
| 648 | clock_class = event_get_clock_class(err, event); |
| 649 | if (!clock_class) { |
| 650 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 651 | __LINE__); |
| 652 | goto error; |
| 653 | } |
| 654 | |
| 655 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); |
| 656 | BT_PUT(clock_class); |
| 657 | if (!clock_value) { |
| 658 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 659 | __LINE__); |
| 660 | goto error; |
| 661 | } |
| 662 | |
| 663 | ret = bt_ctf_clock_value_get_value(clock_value, &value); |
| 664 | BT_PUT(clock_value); |
| 665 | if (ret) { |
| 666 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 667 | __LINE__); |
| 668 | goto error; |
| 669 | } |
| 670 | |
| 671 | writer_clock_class = event_get_clock_class(err, writer_event); |
| 672 | if (!writer_clock_class) { |
| 673 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 674 | __LINE__); |
| 675 | goto error; |
| 676 | } |
| 677 | |
| 678 | writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value); |
| 679 | BT_PUT(writer_clock_class); |
| 680 | if (!writer_clock_value) { |
| 681 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 682 | __LINE__); |
| 683 | goto error; |
| 684 | } |
| 685 | |
| 686 | ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value); |
| 687 | BT_PUT(writer_clock_value); |
| 688 | if (ret) { |
| 689 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 690 | __LINE__); |
| 691 | goto error; |
| 692 | } |
| 693 | |
| 694 | writer_event_header = bt_ctf_field_copy(event_header); |
| 695 | if (!writer_event_header) { |
| 696 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 697 | __FILE__, __LINE__); |
| 698 | goto end; |
| 699 | } |
| 700 | |
| 701 | ret = bt_ctf_event_set_header(writer_event, writer_event_header); |
| 702 | BT_PUT(writer_event_header); |
| 703 | if (ret < 0) { |
| 704 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 705 | __FILE__, __LINE__); |
| 706 | goto error; |
| 707 | } |
| 708 | |
| 709 | ret = 0; |
| 710 | |
| 711 | goto end; |
| 712 | |
| 713 | error: |
| 714 | ret = -1; |
| 715 | end: |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | BT_HIDDEN |
| 720 | struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event, |
| 721 | struct bt_ctf_event_class *writer_event_class, |
| 722 | bool override_ts64) |
| 723 | { |
| 724 | struct bt_ctf_event *writer_event = NULL; |
| 725 | struct bt_ctf_field *field = NULL, *copy_field = NULL; |
| 726 | int ret; |
| 727 | |
| 728 | writer_event = bt_ctf_event_create(writer_event_class); |
| 729 | if (!writer_event) { |
| 730 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 731 | __LINE__); |
| 732 | goto end; |
| 733 | } |
| 734 | |
| 735 | field = bt_ctf_event_get_header(event); |
| 736 | if (!field) { |
| 737 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 738 | __FILE__, __LINE__); |
| 739 | goto error; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * If override_ts64, we override all integer fields mapped to a clock |
| 744 | * to a uint64_t field type, otherwise, we just copy it as is. |
| 745 | */ |
| 746 | if (override_ts64) { |
| 747 | copy_field = bt_ctf_event_get_header(writer_event); |
| 748 | if (!copy_field) { |
| 749 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 750 | __FILE__, __LINE__); |
| 751 | goto error; |
| 752 | } |
| 753 | |
| 754 | ret = copy_override_field(err, event, writer_event, field, |
| 755 | copy_field); |
| 756 | if (ret) { |
| 757 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 758 | __FILE__, __LINE__); |
| 759 | goto error; |
| 760 | } |
| 761 | BT_PUT(copy_field); |
| 762 | } else { |
| 763 | ret = ctf_copy_event_header(err, event, writer_event_class, |
| 764 | writer_event, field); |
| 765 | if (ret) { |
| 766 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 767 | __FILE__, __LINE__); |
| 768 | goto error; |
| 769 | } |
| 770 | } |
| 771 | BT_PUT(field); |
| 772 | |
| 773 | /* Optional field, so it can fail silently. */ |
| 774 | field = bt_ctf_event_get_stream_event_context(event); |
| 775 | copy_field = bt_ctf_field_copy(field); |
| 776 | if (copy_field) { |
| 777 | ret = bt_ctf_event_set_stream_event_context(writer_event, |
| 778 | copy_field); |
| 779 | if (ret < 0) { |
| 780 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 781 | __FILE__, __LINE__); |
| 782 | goto error; |
| 783 | } |
| 784 | } |
| 785 | BT_PUT(field); |
| 786 | BT_PUT(copy_field); |
| 787 | |
| 788 | /* Optional field, so it can fail silently. */ |
| 789 | field = bt_ctf_event_get_event_context(event); |
| 790 | copy_field = bt_ctf_field_copy(field); |
| 791 | if (copy_field) { |
| 792 | ret = bt_ctf_event_set_event_context(writer_event, copy_field); |
| 793 | if (ret < 0) { |
| 794 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 795 | __FILE__, __LINE__); |
| 796 | goto error; |
| 797 | } |
| 798 | } |
| 799 | BT_PUT(field); |
| 800 | BT_PUT(copy_field); |
| 801 | |
| 802 | field = bt_ctf_event_get_event_payload(event); |
| 803 | if (!field) { |
| 804 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 805 | __FILE__, __LINE__); |
| 806 | goto error; |
| 807 | } |
| 808 | copy_field = bt_ctf_field_copy(field); |
| 809 | if (copy_field) { |
| 810 | ret = bt_ctf_event_set_event_payload(writer_event, copy_field); |
| 811 | if (ret < 0) { |
| 812 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 813 | __FILE__, __LINE__); |
| 814 | goto error; |
| 815 | } |
| 816 | } |
| 817 | BT_PUT(field); |
| 818 | BT_PUT(copy_field); |
| 819 | |
| 820 | goto end; |
| 821 | |
| 822 | error: |
| 823 | BT_PUT(writer_event); |
| 824 | end: |
| 825 | bt_put(field); |
| 826 | bt_put(copy_field); |
| 827 | return writer_event; |
| 828 | } |
| 829 | |
| 830 | BT_HIDDEN |
| 831 | enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace, |
| 832 | struct bt_ctf_trace *writer_trace) |
| 833 | { |
| 834 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 835 | int field_count, i, int_ret; |
| 836 | struct bt_ctf_field_type *header_type = NULL; |
| 837 | |
| 838 | field_count = bt_ctf_trace_get_environment_field_count(trace); |
| 839 | for (i = 0; i < field_count; i++) { |
| 840 | int ret_int; |
| 841 | const char *name; |
| 842 | struct bt_value *value = NULL; |
| 843 | |
| 844 | name = bt_ctf_trace_get_environment_field_name_by_index( |
| 845 | trace, i); |
| 846 | if (!name) { |
| 847 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 848 | __LINE__); |
| 849 | ret = BT_COMPONENT_STATUS_ERROR; |
| 850 | goto end; |
| 851 | } |
| 852 | value = bt_ctf_trace_get_environment_field_value_by_index( |
| 853 | trace, i); |
| 854 | if (!value) { |
| 855 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 856 | __LINE__); |
| 857 | ret = BT_COMPONENT_STATUS_ERROR; |
| 858 | goto end; |
| 859 | } |
| 860 | |
| 861 | ret_int = bt_ctf_trace_set_environment_field(writer_trace, |
| 862 | name, value); |
| 863 | BT_PUT(value); |
| 864 | if (ret_int < 0) { |
| 865 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 866 | __LINE__); |
| 867 | fprintf(err, "[error] Unable to set environment field %s\n", |
| 868 | name); |
| 869 | ret = BT_COMPONENT_STATUS_ERROR; |
| 870 | goto end; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | header_type = bt_ctf_trace_get_packet_header_type(writer_trace); |
| 875 | if (!header_type) { |
| 876 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 877 | ret = BT_COMPONENT_STATUS_ERROR; |
| 878 | goto end; |
| 879 | } |
| 880 | |
| 881 | int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type); |
| 882 | BT_PUT(header_type); |
| 883 | if (int_ret < 0) { |
| 884 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 885 | ret = BT_COMPONENT_STATUS_ERROR; |
| 886 | goto end; |
| 887 | } |
| 888 | |
| 889 | end: |
| 890 | return ret; |
| 891 | } |