| 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 | |
| 41 | struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err, |
| 42 | struct bt_ctf_clock_class *clock_class) |
| 43 | { |
| 44 | int64_t offset, offset_s; |
| 45 | int int_ret; |
| 46 | uint64_t u64_ret; |
| 47 | const char *name, *description; |
| 48 | struct bt_ctf_clock_class *writer_clock_class = NULL; |
| 49 | |
| 50 | assert(err && clock_class); |
| 51 | |
| 52 | name = bt_ctf_clock_class_get_name(clock_class); |
| 53 | if (!name) { |
| 54 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 55 | __LINE__); |
| 56 | goto end; |
| 57 | } |
| 58 | |
| 59 | writer_clock_class = bt_ctf_clock_class_create(name); |
| 60 | if (!writer_clock_class) { |
| 61 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 62 | __LINE__); |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | description = bt_ctf_clock_class_get_description(clock_class); |
| 67 | if (!description) { |
| 68 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 69 | __LINE__); |
| 70 | goto end_destroy; |
| 71 | } |
| 72 | |
| 73 | int_ret = bt_ctf_clock_class_set_description(writer_clock_class, |
| 74 | description); |
| 75 | if (int_ret != 0) { |
| 76 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 77 | __LINE__); |
| 78 | goto end_destroy; |
| 79 | } |
| 80 | |
| 81 | u64_ret = bt_ctf_clock_class_get_frequency(clock_class); |
| 82 | if (u64_ret == -1ULL) { |
| 83 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 84 | __LINE__); |
| 85 | goto end_destroy; |
| 86 | } |
| 87 | int_ret = bt_ctf_clock_class_set_frequency(writer_clock_class, u64_ret); |
| 88 | if (int_ret != 0) { |
| 89 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 90 | __LINE__); |
| 91 | goto end_destroy; |
| 92 | } |
| 93 | |
| 94 | u64_ret = bt_ctf_clock_class_get_precision(clock_class); |
| 95 | if (u64_ret == -1ULL) { |
| 96 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 97 | __LINE__); |
| 98 | goto end_destroy; |
| 99 | } |
| 100 | int_ret = bt_ctf_clock_class_set_precision(writer_clock_class, |
| 101 | u64_ret); |
| 102 | if (int_ret != 0) { |
| 103 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 104 | __LINE__); |
| 105 | goto end_destroy; |
| 106 | } |
| 107 | |
| 108 | int_ret = bt_ctf_clock_class_get_offset_s(clock_class, &offset_s); |
| 109 | if (int_ret != 0) { |
| 110 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 111 | __LINE__); |
| 112 | goto end_destroy; |
| 113 | } |
| 114 | |
| 115 | int_ret = bt_ctf_clock_class_set_offset_s(writer_clock_class, offset_s); |
| 116 | if (int_ret != 0) { |
| 117 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 118 | __LINE__); |
| 119 | goto end_destroy; |
| 120 | } |
| 121 | |
| 122 | int_ret = bt_ctf_clock_class_get_offset_cycles(clock_class, &offset); |
| 123 | if (int_ret != 0) { |
| 124 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 125 | __LINE__); |
| 126 | goto end_destroy; |
| 127 | } |
| 128 | |
| 129 | int_ret = bt_ctf_clock_class_set_offset_cycles(writer_clock_class, offset); |
| 130 | if (int_ret != 0) { |
| 131 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 132 | __LINE__); |
| 133 | goto end_destroy; |
| 134 | } |
| 135 | |
| 136 | int_ret = bt_ctf_clock_class_get_is_absolute(clock_class); |
| 137 | if (int_ret == -1) { |
| 138 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 139 | __LINE__); |
| 140 | goto end_destroy; |
| 141 | } |
| 142 | |
| 143 | int_ret = bt_ctf_clock_class_set_is_absolute(writer_clock_class, int_ret); |
| 144 | if (int_ret != 0) { |
| 145 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 146 | __LINE__); |
| 147 | goto end_destroy; |
| 148 | } |
| 149 | |
| 150 | goto end; |
| 151 | |
| 152 | end_destroy: |
| 153 | BT_PUT(writer_clock_class); |
| 154 | end: |
| 155 | return writer_clock_class; |
| 156 | } |
| 157 | |
| 158 | enum bt_component_status ctf_copy_clock_classes(FILE *err, |
| 159 | struct bt_ctf_trace *writer_trace, |
| 160 | struct bt_ctf_stream_class *writer_stream_class, |
| 161 | struct bt_ctf_trace *trace) |
| 162 | { |
| 163 | enum bt_component_status ret; |
| 164 | |
| 165 | int int_ret, clock_class_count, i; |
| 166 | |
| 167 | clock_class_count = bt_ctf_trace_get_clock_class_count(trace); |
| 168 | |
| 169 | for (i = 0; i < clock_class_count; i++) { |
| 170 | struct bt_ctf_clock_class *writer_clock_class; |
| 171 | struct bt_ctf_clock_class *clock_class = |
| 172 | bt_ctf_trace_get_clock_class(trace, i); |
| 173 | |
| 174 | if (!clock_class) { |
| 175 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 176 | __LINE__); |
| 177 | ret = BT_COMPONENT_STATUS_ERROR; |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 181 | writer_clock_class = ctf_copy_clock_class(err, clock_class); |
| 182 | bt_put(clock_class); |
| 183 | if (!writer_clock_class) { |
| 184 | fprintf(err, "Failed to copy clock class"); |
| 185 | ret = BT_COMPONENT_STATUS_ERROR; |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | int_ret = bt_ctf_trace_add_clock_class(writer_trace, writer_clock_class); |
| 190 | if (int_ret != 0) { |
| 191 | BT_PUT(writer_clock_class); |
| 192 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 193 | __LINE__); |
| 194 | ret = BT_COMPONENT_STATUS_ERROR; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Ownership transferred to the writer and the stream_class. |
| 200 | */ |
| 201 | bt_put(writer_clock_class); |
| 202 | } |
| 203 | |
| 204 | ret = BT_COMPONENT_STATUS_OK; |
| 205 | |
| 206 | end: |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | struct bt_ctf_event_class *ctf_copy_event_class(FILE *err, |
| 211 | struct bt_ctf_event_class *event_class) |
| 212 | { |
| 213 | struct bt_ctf_event_class *writer_event_class = NULL; |
| 214 | const char *name; |
| 215 | int count, i; |
| 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(event_class, i); |
| 238 | if (!attr_name) { |
| 239 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 240 | __FILE__, __LINE__); |
| 241 | BT_PUT(writer_event_class); |
| 242 | goto end; |
| 243 | } |
| 244 | attr_value = bt_ctf_event_class_get_attribute_value(event_class, i); |
| 245 | if (!attr_value) { |
| 246 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 247 | __FILE__, __LINE__); |
| 248 | BT_PUT(writer_event_class); |
| 249 | goto end; |
| 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 | BT_PUT(writer_event_class); |
| 259 | goto end; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | count = bt_ctf_event_class_get_field_count(event_class); |
| 264 | for (i = 0; i < count; i++) { |
| 265 | const char *field_name; |
| 266 | struct bt_ctf_field_type *field_type; |
| 267 | int ret; |
| 268 | |
| 269 | ret = bt_ctf_event_class_get_field(event_class, &field_name, |
| 270 | &field_type, i); |
| 271 | if (ret < 0) { |
| 272 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 273 | BT_PUT(writer_event_class); |
| 274 | goto end; |
| 275 | } |
| 276 | |
| 277 | ret = bt_ctf_event_class_add_field(writer_event_class, field_type, |
| 278 | field_name); |
| 279 | if (ret < 0) { |
| 280 | fprintf(err, "[error] Cannot add field %s\n", field_name); |
| 281 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 282 | bt_put(field_type); |
| 283 | BT_PUT(writer_event_class); |
| 284 | goto end; |
| 285 | } |
| 286 | bt_put(field_type); |
| 287 | } |
| 288 | |
| 289 | end: |
| 290 | return writer_event_class; |
| 291 | } |
| 292 | |
| 293 | enum bt_component_status ctf_copy_event_classes(FILE *err, |
| 294 | struct bt_ctf_stream_class *stream_class, |
| 295 | struct bt_ctf_stream_class *writer_stream_class) |
| 296 | { |
| 297 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 298 | int count, i; |
| 299 | |
| 300 | count = bt_ctf_stream_class_get_event_class_count(stream_class); |
| 301 | if (count < 0) { |
| 302 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 303 | __FILE__, __LINE__); |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | for (i = 0; i < count; i++) { |
| 308 | struct bt_ctf_event_class *event_class, *writer_event_class; |
| 309 | struct bt_ctf_field_type *context; |
| 310 | int int_ret; |
| 311 | |
| 312 | event_class = bt_ctf_stream_class_get_event_class( |
| 313 | stream_class, i); |
| 314 | if (!event_class) { |
| 315 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 316 | __FILE__, __LINE__); |
| 317 | ret = BT_COMPONENT_STATUS_ERROR; |
| 318 | bt_put(event_class); |
| 319 | goto end; |
| 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 | bt_put(event_class); |
| 327 | goto end; |
| 328 | } |
| 329 | |
| 330 | context = bt_ctf_event_class_get_context_type(event_class); |
| 331 | ret = bt_ctf_event_class_set_context_type(writer_event_class, context); |
| 332 | bt_put(context); |
| 333 | if (ret < 0) { |
| 334 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 335 | __LINE__); |
| 336 | goto end; |
| 337 | } |
| 338 | |
| 339 | int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class, |
| 340 | writer_event_class); |
| 341 | if (int_ret < 0) { |
| 342 | fprintf(err, "[error] Failed to add event class\n"); |
| 343 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 344 | __FILE__, __LINE__); |
| 345 | ret = BT_COMPONENT_STATUS_ERROR; |
| 346 | bt_put(event_class); |
| 347 | goto end; |
| 348 | } |
| 349 | bt_put(event_class); |
| 350 | bt_put(writer_event_class); |
| 351 | } |
| 352 | |
| 353 | end: |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err, |
| 358 | struct bt_ctf_stream_class *stream_class) |
| 359 | { |
| 360 | struct bt_ctf_field_type *type; |
| 361 | struct bt_ctf_stream_class *writer_stream_class; |
| 362 | int ret_int; |
| 363 | const char *name = bt_ctf_stream_class_get_name(stream_class); |
| 364 | |
| 365 | if (strlen(name) == 0) { |
| 366 | name = NULL; |
| 367 | } |
| 368 | |
| 369 | writer_stream_class = bt_ctf_stream_class_create(name); |
| 370 | if (!writer_stream_class) { |
| 371 | fprintf(err, "[error] %s in %s:%d\n", |
| 372 | __func__, __FILE__, __LINE__); |
| 373 | goto end; |
| 374 | } |
| 375 | |
| 376 | type = bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 377 | if (!type) { |
| 378 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 379 | __LINE__); |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | ret_int = bt_ctf_stream_class_set_packet_context_type( |
| 384 | writer_stream_class, type); |
| 385 | bt_put(type); |
| 386 | if (ret_int < 0) { |
| 387 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 388 | __LINE__); |
| 389 | goto error; |
| 390 | } |
| 391 | |
| 392 | type = bt_ctf_stream_class_get_event_header_type(stream_class); |
| 393 | if (!type) { |
| 394 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 395 | __LINE__); |
| 396 | goto error; |
| 397 | } |
| 398 | |
| 399 | ret_int = bt_ctf_stream_class_set_event_header_type( |
| 400 | writer_stream_class, type); |
| 401 | if (ret_int < 0) { |
| 402 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 403 | __LINE__); |
| 404 | goto error; |
| 405 | } |
| 406 | |
| 407 | type = bt_ctf_stream_class_get_event_context_type(stream_class); |
| 408 | if (type) { |
| 409 | ret_int = bt_ctf_stream_class_set_event_context_type( |
| 410 | writer_stream_class, type); |
| 411 | bt_put(type); |
| 412 | if (ret_int < 0) { |
| 413 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 414 | __LINE__); |
| 415 | goto error; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | goto end; |
| 420 | |
| 421 | error: |
| 422 | BT_PUT(writer_stream_class); |
| 423 | end: |
| 424 | return writer_stream_class; |
| 425 | } |
| 426 | |
| 427 | enum bt_component_status ctf_copy_packet_context_field(FILE *err, |
| 428 | struct bt_ctf_field *field, const char *field_name, |
| 429 | struct bt_ctf_field *writer_packet_context, |
| 430 | struct bt_ctf_field_type *writer_packet_context_type) |
| 431 | { |
| 432 | enum bt_component_status ret; |
| 433 | struct bt_ctf_field *writer_field; |
| 434 | struct bt_ctf_field_type *field_type; |
| 435 | int int_ret; |
| 436 | uint64_t value; |
| 437 | |
| 438 | field_type = bt_ctf_field_get_type(field); |
| 439 | if (!field_type) { |
| 440 | ret = BT_COMPONENT_STATUS_ERROR; |
| 441 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 442 | __LINE__); |
| 443 | goto end; |
| 444 | } |
| 445 | /* |
| 446 | * Only support for integers for now. |
| 447 | */ |
| 448 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) { |
| 449 | fprintf(err, "[error] Unsupported packet context field type\n"); |
| 450 | bt_put(field_type); |
| 451 | ret = BT_COMPONENT_STATUS_ERROR; |
| 452 | goto end; |
| 453 | } |
| 454 | bt_put(field_type); |
| 455 | |
| 456 | /* |
| 457 | * TODO: handle the special case of the first/last packet that might |
| 458 | * be trimmed. In these cases, the timestamp_begin/end need to be |
| 459 | * explicitely set to the first/last event timestamps. |
| 460 | */ |
| 461 | writer_field = bt_ctf_field_structure_get_field(writer_packet_context, |
| 462 | field_name); |
| 463 | if (!writer_field) { |
| 464 | ret = BT_COMPONENT_STATUS_ERROR; |
| 465 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 466 | __LINE__); |
| 467 | goto end; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | int_ret = bt_ctf_field_unsigned_integer_get_value(field, &value); |
| 472 | if (int_ret < 0) { |
| 473 | fprintf(err, "[error] Wrong packet_context field type\n"); |
| 474 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 475 | __LINE__); |
| 476 | ret = BT_COMPONENT_STATUS_ERROR; |
| 477 | goto end_put_writer_field; |
| 478 | } |
| 479 | |
| 480 | int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value); |
| 481 | if (int_ret < 0) { |
| 482 | ret = BT_COMPONENT_STATUS_ERROR; |
| 483 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 484 | __LINE__); |
| 485 | goto end_put_writer_field; |
| 486 | } |
| 487 | |
| 488 | ret = BT_COMPONENT_STATUS_OK; |
| 489 | |
| 490 | end_put_writer_field: |
| 491 | bt_put(writer_field); |
| 492 | end: |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | enum bt_component_status ctf_copy_packet_context(FILE *err, |
| 497 | struct bt_ctf_packet *packet, |
| 498 | struct bt_ctf_stream *writer_stream) |
| 499 | { |
| 500 | enum bt_component_status ret; |
| 501 | struct bt_ctf_field *packet_context, *writer_packet_context; |
| 502 | struct bt_ctf_field_type *struct_type, *writer_packet_context_type; |
| 503 | struct bt_ctf_stream_class *writer_stream_class; |
| 504 | int nr_fields, i, int_ret; |
| 505 | |
| 506 | packet_context = bt_ctf_packet_get_context(packet); |
| 507 | if (!packet_context) { |
| 508 | ret = BT_COMPONENT_STATUS_ERROR; |
| 509 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 510 | __LINE__); |
| 511 | goto end; |
| 512 | } |
| 513 | |
| 514 | writer_stream_class = bt_ctf_stream_get_class(writer_stream); |
| 515 | if (!writer_stream_class) { |
| 516 | ret = BT_COMPONENT_STATUS_ERROR; |
| 517 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 518 | __LINE__); |
| 519 | goto end_put_packet_context; |
| 520 | } |
| 521 | |
| 522 | writer_packet_context_type = bt_ctf_stream_class_get_packet_context_type( |
| 523 | writer_stream_class); |
| 524 | if (!writer_packet_context_type) { |
| 525 | ret = BT_COMPONENT_STATUS_ERROR; |
| 526 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 527 | __LINE__); |
| 528 | goto end_put_writer_stream_class; |
| 529 | } |
| 530 | |
| 531 | struct_type = bt_ctf_field_get_type(packet_context); |
| 532 | if (!struct_type) { |
| 533 | ret = BT_COMPONENT_STATUS_ERROR; |
| 534 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 535 | __LINE__); |
| 536 | goto end_put_writer_packet_context_type; |
| 537 | } |
| 538 | |
| 539 | writer_packet_context = bt_ctf_field_create(writer_packet_context_type); |
| 540 | if (!writer_packet_context) { |
| 541 | ret = BT_COMPONENT_STATUS_ERROR; |
| 542 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 543 | __LINE__); |
| 544 | goto end_put_struct_type; |
| 545 | } |
| 546 | |
| 547 | nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type); |
| 548 | for (i = 0; i < nr_fields; i++) { |
| 549 | struct bt_ctf_field *field; |
| 550 | struct bt_ctf_field_type *field_type; |
| 551 | const char *field_name; |
| 552 | |
| 553 | field = bt_ctf_field_structure_get_field_by_index( |
| 554 | packet_context, i); |
| 555 | if (!field) { |
| 556 | ret = BT_COMPONENT_STATUS_ERROR; |
| 557 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 558 | __FILE__, __LINE__); |
| 559 | goto end_put_writer_packet_context; |
| 560 | } |
| 561 | if (bt_ctf_field_type_structure_get_field(struct_type, |
| 562 | &field_name, &field_type, i) < 0) { |
| 563 | ret = BT_COMPONENT_STATUS_ERROR; |
| 564 | bt_put(field); |
| 565 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 566 | __FILE__, __LINE__); |
| 567 | goto end_put_writer_packet_context; |
| 568 | } |
| 569 | if (!strncmp(field_name, "content_size", strlen("content_size")) || |
| 570 | !strncmp(field_name, "packet_size", |
| 571 | strlen("packet_size"))) { |
| 572 | bt_put(field_type); |
| 573 | bt_put(field); |
| 574 | continue; |
| 575 | } |
| 576 | |
| 577 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) { |
| 578 | fprintf(err, "[error] Unexpected packet context field type\n"); |
| 579 | bt_put(field); |
| 580 | ret = BT_COMPONENT_STATUS_ERROR; |
| 581 | goto end_put_writer_packet_context; |
| 582 | } |
| 583 | |
| 584 | ret = ctf_copy_packet_context_field(err, field, field_name, |
| 585 | writer_packet_context, writer_packet_context_type); |
| 586 | bt_put(field_type); |
| 587 | bt_put(field); |
| 588 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 589 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 590 | __FILE__, __LINE__); |
| 591 | goto end_put_writer_packet_context; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | int_ret = bt_ctf_stream_set_packet_context(writer_stream, |
| 596 | writer_packet_context); |
| 597 | if (int_ret < 0) { |
| 598 | ret = BT_COMPONENT_STATUS_ERROR; |
| 599 | goto end_put_writer_packet_context; |
| 600 | } |
| 601 | |
| 602 | end_put_writer_packet_context: |
| 603 | bt_put(writer_packet_context); |
| 604 | end_put_struct_type: |
| 605 | bt_put(struct_type); |
| 606 | end_put_writer_packet_context_type: |
| 607 | bt_put(writer_packet_context_type); |
| 608 | end_put_writer_stream_class: |
| 609 | bt_put(writer_stream_class); |
| 610 | end_put_packet_context: |
| 611 | bt_put(packet_context); |
| 612 | end: |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event, |
| 617 | struct bt_ctf_event_class *writer_event_class) |
| 618 | { |
| 619 | struct bt_ctf_event *writer_event; |
| 620 | struct bt_ctf_field *field, *copy_field; |
| 621 | int ret; |
| 622 | |
| 623 | writer_event = bt_ctf_event_create(writer_event_class); |
| 624 | if (!writer_event) { |
| 625 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 626 | __LINE__); |
| 627 | goto end; |
| 628 | } |
| 629 | |
| 630 | field = bt_ctf_event_get_header(event); |
| 631 | if (!field) { |
| 632 | BT_PUT(writer_event); |
| 633 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 634 | __FILE__, __LINE__); |
| 635 | goto end; |
| 636 | } |
| 637 | |
| 638 | copy_field = bt_ctf_field_copy(field); |
| 639 | bt_put(field); |
| 640 | if (copy_field) { |
| 641 | ret = bt_ctf_event_set_header(writer_event, copy_field); |
| 642 | if (ret < 0) { |
| 643 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 644 | __FILE__, __LINE__); |
| 645 | goto error; |
| 646 | } |
| 647 | bt_put(copy_field); |
| 648 | } |
| 649 | |
| 650 | /* Optional field, so it can fail silently. */ |
| 651 | field = bt_ctf_event_get_stream_event_context(event); |
| 652 | copy_field = bt_ctf_field_copy(field); |
| 653 | bt_put(field); |
| 654 | if (copy_field) { |
| 655 | ret = bt_ctf_event_set_stream_event_context(writer_event, |
| 656 | copy_field); |
| 657 | bt_put(copy_field); |
| 658 | if (ret < 0) { |
| 659 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 660 | __FILE__, __LINE__); |
| 661 | goto error; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* Optional field, so it can fail silently. */ |
| 666 | field = bt_ctf_event_get_event_context(event); |
| 667 | copy_field = bt_ctf_field_copy(field); |
| 668 | bt_put(field); |
| 669 | if (copy_field) { |
| 670 | ret = bt_ctf_event_set_event_context(writer_event, copy_field); |
| 671 | bt_put(copy_field); |
| 672 | if (ret < 0) { |
| 673 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 674 | __FILE__, __LINE__); |
| 675 | goto error; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | field = bt_ctf_event_get_payload_field(event); |
| 680 | if (!field) { |
| 681 | BT_PUT(writer_event); |
| 682 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 683 | __FILE__, __LINE__); |
| 684 | goto end; |
| 685 | } |
| 686 | copy_field = bt_ctf_field_copy(field); |
| 687 | bt_put(field); |
| 688 | if (copy_field) { |
| 689 | ret = bt_ctf_event_set_payload_field(writer_event, copy_field); |
| 690 | bt_put(copy_field); |
| 691 | if (ret < 0) { |
| 692 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 693 | __FILE__, __LINE__); |
| 694 | goto error; |
| 695 | } |
| 696 | } |
| 697 | goto end; |
| 698 | |
| 699 | error: |
| 700 | BT_PUT(writer_event); |
| 701 | end: |
| 702 | return writer_event; |
| 703 | } |
| 704 | |
| 705 | enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace, |
| 706 | struct bt_ctf_trace *writer_trace) |
| 707 | { |
| 708 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 709 | int field_count, i, int_ret; |
| 710 | struct bt_ctf_field_type *header_type; |
| 711 | |
| 712 | field_count = bt_ctf_trace_get_environment_field_count(trace); |
| 713 | for (i = 0; i < field_count; i++) { |
| 714 | int ret_int; |
| 715 | const char *name; |
| 716 | struct bt_value *value; |
| 717 | |
| 718 | name = bt_ctf_trace_get_environment_field_name(trace, i); |
| 719 | if (!name) { |
| 720 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 721 | __LINE__); |
| 722 | ret = BT_COMPONENT_STATUS_ERROR; |
| 723 | goto end_put_writer_trace; |
| 724 | } |
| 725 | value = bt_ctf_trace_get_environment_field_value(trace, i); |
| 726 | if (!value) { |
| 727 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 728 | __LINE__); |
| 729 | ret = BT_COMPONENT_STATUS_ERROR; |
| 730 | goto end_put_writer_trace; |
| 731 | } |
| 732 | |
| 733 | ret_int = bt_ctf_trace_set_environment_field(writer_trace, |
| 734 | name, value); |
| 735 | bt_put(value); |
| 736 | if (ret_int < 0) { |
| 737 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 738 | __LINE__); |
| 739 | fprintf(err, "[error] Unable to set environment field %s\n", |
| 740 | name); |
| 741 | ret = BT_COMPONENT_STATUS_ERROR; |
| 742 | goto end_put_writer_trace; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | header_type = bt_ctf_trace_get_packet_header_type(writer_trace); |
| 747 | if (!header_type) { |
| 748 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 749 | ret = BT_COMPONENT_STATUS_ERROR; |
| 750 | goto end_put_writer_trace; |
| 751 | } |
| 752 | |
| 753 | int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type); |
| 754 | if (int_ret < 0) { |
| 755 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 756 | ret = BT_COMPONENT_STATUS_ERROR; |
| 757 | goto end_put_header_type; |
| 758 | } |
| 759 | |
| 760 | end_put_header_type: |
| 761 | bt_put(header_type); |
| 762 | end_put_writer_trace: |
| 763 | return ret; |
| 764 | } |
| 765 | |