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