| 1 | /* |
| 2 | * copy.c |
| 3 | * |
| 4 | * Babeltrace Copy Trace Structure |
| 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 <inttypes.h> |
| 30 | #include <babeltrace/ctf-ir/event.h> |
| 31 | #include <babeltrace/ctf-ir/packet.h> |
| 32 | #include <babeltrace/ctf-ir/event-class.h> |
| 33 | #include <babeltrace/ctf-ir/stream.h> |
| 34 | #include <babeltrace/ctf-ir/stream-class.h> |
| 35 | #include <babeltrace/ctf-ir/clock-class.h> |
| 36 | #include <babeltrace/ctf-ir/fields.h> |
| 37 | #include <babeltrace/ctf-writer/stream-class.h> |
| 38 | #include <babeltrace/ctf-writer/stream.h> |
| 39 | |
| 40 | #include <ctfcopytrace.h> |
| 41 | #include "debug-info.h" |
| 42 | |
| 43 | static |
| 44 | struct bt_ctf_stream *insert_new_stream( |
| 45 | struct debug_info_iterator *debug_it, |
| 46 | struct bt_ctf_stream *stream, |
| 47 | struct debug_info_trace *di_trace); |
| 48 | |
| 49 | static |
| 50 | void unref_stream(struct bt_ctf_stream *stream) |
| 51 | { |
| 52 | bt_put(stream); |
| 53 | } |
| 54 | |
| 55 | static |
| 56 | void unref_packet(struct bt_ctf_packet *packet) |
| 57 | { |
| 58 | bt_put(packet); |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void unref_stream_class(struct bt_ctf_stream_class *stream_class) |
| 63 | { |
| 64 | bt_put(stream_class); |
| 65 | } |
| 66 | |
| 67 | static |
| 68 | void unref_debug_info(struct debug_info *debug_info) |
| 69 | { |
| 70 | debug_info_destroy(debug_info); |
| 71 | } |
| 72 | |
| 73 | static |
| 74 | void destroy_stream_state_key(gpointer key) |
| 75 | { |
| 76 | g_free((enum fs_writer_stream_state *) key); |
| 77 | } |
| 78 | |
| 79 | static |
| 80 | struct bt_ctf_field *get_payload_field(FILE *err, |
| 81 | struct bt_ctf_event *event, const char *field_name) |
| 82 | { |
| 83 | struct bt_ctf_field *field = NULL, *sec = NULL; |
| 84 | struct bt_ctf_field_type *sec_type = NULL; |
| 85 | |
| 86 | sec = bt_ctf_event_get_payload(event, NULL); |
| 87 | if (!sec) { |
| 88 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 89 | __FILE__, __LINE__); |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | sec_type = bt_ctf_field_get_type(sec); |
| 94 | if (!sec_type) { |
| 95 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 96 | __FILE__, __LINE__); |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | if (bt_ctf_field_type_get_type_id(sec_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 101 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 102 | __FILE__, __LINE__); |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | field = bt_ctf_field_structure_get_field(sec, field_name); |
| 107 | |
| 108 | end: |
| 109 | bt_put(sec_type); |
| 110 | bt_put(sec); |
| 111 | return field; |
| 112 | } |
| 113 | |
| 114 | static |
| 115 | struct bt_ctf_field *get_stream_event_context_field(FILE *err, |
| 116 | struct bt_ctf_event *event, const char *field_name) |
| 117 | { |
| 118 | struct bt_ctf_field *field = NULL, *sec = NULL; |
| 119 | struct bt_ctf_field_type *sec_type = NULL; |
| 120 | |
| 121 | sec = bt_ctf_event_get_stream_event_context(event); |
| 122 | if (!sec) { |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | sec_type = bt_ctf_field_get_type(sec); |
| 127 | if (!sec_type) { |
| 128 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 129 | __FILE__, __LINE__); |
| 130 | goto end; |
| 131 | } |
| 132 | |
| 133 | if (bt_ctf_field_type_get_type_id(sec_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 134 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 135 | __FILE__, __LINE__); |
| 136 | goto end; |
| 137 | } |
| 138 | |
| 139 | field = bt_ctf_field_structure_get_field(sec, field_name); |
| 140 | |
| 141 | end: |
| 142 | bt_put(sec_type); |
| 143 | bt_put(sec); |
| 144 | return field; |
| 145 | } |
| 146 | |
| 147 | BT_HIDDEN |
| 148 | int get_stream_event_context_unsigned_int_field_value(FILE *err, |
| 149 | struct bt_ctf_event *event, const char *field_name, |
| 150 | uint64_t *value) |
| 151 | { |
| 152 | int ret; |
| 153 | struct bt_ctf_field *field = NULL; |
| 154 | struct bt_ctf_field_type *field_type = NULL; |
| 155 | |
| 156 | field = get_stream_event_context_field(err, event, field_name); |
| 157 | if (!field) { |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | field_type = bt_ctf_field_get_type(field); |
| 162 | if (!field_type) { |
| 163 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 164 | __FILE__, __LINE__); |
| 165 | goto error; |
| 166 | } |
| 167 | |
| 168 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 169 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 170 | __FILE__, __LINE__); |
| 171 | goto error; |
| 172 | } |
| 173 | |
| 174 | if (bt_ctf_field_type_integer_get_signed(field_type) != 0) { |
| 175 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 176 | __FILE__, __LINE__); |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | ret = bt_ctf_field_unsigned_integer_get_value(field, value); |
| 181 | goto end; |
| 182 | |
| 183 | error: |
| 184 | ret = -1; |
| 185 | end: |
| 186 | bt_put(field_type); |
| 187 | bt_put(field); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | BT_HIDDEN |
| 192 | int get_stream_event_context_int_field_value(FILE *err, struct bt_ctf_event *event, |
| 193 | const char *field_name, int64_t *value) |
| 194 | { |
| 195 | struct bt_ctf_field *field = NULL; |
| 196 | struct bt_ctf_field_type *field_type = NULL; |
| 197 | int ret; |
| 198 | |
| 199 | field = get_stream_event_context_field(err, event, field_name); |
| 200 | if (!field) { |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | field_type = bt_ctf_field_get_type(field); |
| 205 | if (!field_type) { |
| 206 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 207 | __FILE__, __LINE__); |
| 208 | goto error; |
| 209 | } |
| 210 | |
| 211 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 212 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 213 | __FILE__, __LINE__); |
| 214 | goto error; |
| 215 | } |
| 216 | |
| 217 | if (bt_ctf_field_type_integer_get_signed(field_type) != 1) { |
| 218 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 219 | __FILE__, __LINE__); |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | ret = bt_ctf_field_signed_integer_get_value(field, value); |
| 224 | goto end; |
| 225 | |
| 226 | error: |
| 227 | ret = -1; |
| 228 | end: |
| 229 | bt_put(field_type); |
| 230 | bt_put(field); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | BT_HIDDEN |
| 235 | int get_payload_unsigned_int_field_value(FILE *err, |
| 236 | struct bt_ctf_event *event, const char *field_name, |
| 237 | uint64_t *value) |
| 238 | { |
| 239 | struct bt_ctf_field *field = NULL; |
| 240 | struct bt_ctf_field_type *field_type = NULL; |
| 241 | int ret; |
| 242 | |
| 243 | field = get_payload_field(err, event, field_name); |
| 244 | if (!field) { |
| 245 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 246 | __FILE__, __LINE__); |
| 247 | goto error; |
| 248 | } |
| 249 | |
| 250 | field_type = bt_ctf_field_get_type(field); |
| 251 | if (!field_type) { |
| 252 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 253 | __FILE__, __LINE__); |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 258 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 259 | __FILE__, __LINE__); |
| 260 | goto error; |
| 261 | } |
| 262 | |
| 263 | if (bt_ctf_field_type_integer_get_signed(field_type) != 0) { |
| 264 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 265 | __FILE__, __LINE__); |
| 266 | goto error; |
| 267 | } |
| 268 | |
| 269 | ret = bt_ctf_field_unsigned_integer_get_value(field, value); |
| 270 | goto end; |
| 271 | |
| 272 | error: |
| 273 | ret = -1; |
| 274 | end: |
| 275 | bt_put(field_type); |
| 276 | bt_put(field); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | BT_HIDDEN |
| 281 | int get_payload_int_field_value(FILE *err, struct bt_ctf_event *event, |
| 282 | const char *field_name, int64_t *value) |
| 283 | { |
| 284 | struct bt_ctf_field *field = NULL; |
| 285 | struct bt_ctf_field_type *field_type = NULL; |
| 286 | int ret; |
| 287 | |
| 288 | field = get_payload_field(err, event, field_name); |
| 289 | if (!field) { |
| 290 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 291 | __FILE__, __LINE__); |
| 292 | goto error; |
| 293 | } |
| 294 | |
| 295 | field_type = bt_ctf_field_get_type(field); |
| 296 | if (!field_type) { |
| 297 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 298 | __FILE__, __LINE__); |
| 299 | goto error; |
| 300 | } |
| 301 | |
| 302 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 303 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 304 | __FILE__, __LINE__); |
| 305 | goto error; |
| 306 | } |
| 307 | |
| 308 | if (bt_ctf_field_type_integer_get_signed(field_type) != 1) { |
| 309 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 310 | __FILE__, __LINE__); |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | ret = bt_ctf_field_signed_integer_get_value(field, value); |
| 315 | goto end; |
| 316 | |
| 317 | error: |
| 318 | ret = -1; |
| 319 | end: |
| 320 | bt_put(field_type); |
| 321 | bt_put(field); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | BT_HIDDEN |
| 326 | int get_payload_string_field_value(FILE *err, |
| 327 | struct bt_ctf_event *event, const char *field_name, |
| 328 | const char **value) |
| 329 | { |
| 330 | struct bt_ctf_field *field = NULL; |
| 331 | struct bt_ctf_field_type *field_type = NULL; |
| 332 | int ret; |
| 333 | |
| 334 | /* |
| 335 | * The field might not exist, no error here. |
| 336 | */ |
| 337 | field = get_payload_field(err, event, field_name); |
| 338 | if (!field) { |
| 339 | goto error; |
| 340 | } |
| 341 | |
| 342 | field_type = bt_ctf_field_get_type(field); |
| 343 | if (!field_type) { |
| 344 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 345 | __FILE__, __LINE__); |
| 346 | goto error; |
| 347 | } |
| 348 | |
| 349 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_STRING) { |
| 350 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 351 | __FILE__, __LINE__); |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | *value = bt_ctf_field_string_get_value(field); |
| 356 | if (!*value) { |
| 357 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 358 | __FILE__, __LINE__); |
| 359 | goto error; |
| 360 | } |
| 361 | |
| 362 | ret = 0; |
| 363 | goto end; |
| 364 | |
| 365 | error: |
| 366 | ret = -1; |
| 367 | end: |
| 368 | bt_put(field_type); |
| 369 | bt_put(field); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | BT_HIDDEN |
| 374 | int get_payload_build_id_field_value(FILE *err, |
| 375 | struct bt_ctf_event *event, const char *field_name, |
| 376 | uint8_t **build_id, uint64_t *build_id_len) |
| 377 | { |
| 378 | struct bt_ctf_field *field = NULL, *seq_len = NULL; |
| 379 | struct bt_ctf_field_type *field_type = NULL; |
| 380 | struct bt_ctf_field *seq_field = NULL; |
| 381 | uint64_t i; |
| 382 | int ret; |
| 383 | |
| 384 | *build_id = NULL; |
| 385 | |
| 386 | field = get_payload_field(err, event, field_name); |
| 387 | if (!field) { |
| 388 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 389 | __FILE__, __LINE__); |
| 390 | goto error; |
| 391 | } |
| 392 | |
| 393 | field_type = bt_ctf_field_get_type(field); |
| 394 | if (!field_type) { |
| 395 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 396 | __FILE__, __LINE__); |
| 397 | goto error; |
| 398 | } |
| 399 | |
| 400 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_SEQUENCE) { |
| 401 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 402 | __FILE__, __LINE__); |
| 403 | goto error; |
| 404 | } |
| 405 | BT_PUT(field_type); |
| 406 | |
| 407 | seq_len = bt_ctf_field_sequence_get_length(field); |
| 408 | if (!seq_len) { |
| 409 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 410 | __FILE__, __LINE__); |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | ret = bt_ctf_field_unsigned_integer_get_value(seq_len, build_id_len); |
| 415 | if (ret) { |
| 416 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 417 | __FILE__, __LINE__); |
| 418 | goto error; |
| 419 | } |
| 420 | BT_PUT(seq_len); |
| 421 | |
| 422 | *build_id = g_new0(uint8_t, *build_id_len); |
| 423 | if (!*build_id) { |
| 424 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 425 | __FILE__, __LINE__); |
| 426 | goto error; |
| 427 | } |
| 428 | |
| 429 | for (i = 0; i < *build_id_len; i++) { |
| 430 | uint64_t tmp; |
| 431 | |
| 432 | seq_field = bt_ctf_field_sequence_get_field(field, i); |
| 433 | if (!seq_field) { |
| 434 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 435 | __FILE__, __LINE__); |
| 436 | goto error; |
| 437 | } |
| 438 | |
| 439 | ret = bt_ctf_field_unsigned_integer_get_value(seq_field, &tmp); |
| 440 | if (ret) { |
| 441 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 442 | __FILE__, __LINE__); |
| 443 | goto error; |
| 444 | } |
| 445 | BT_PUT(seq_field); |
| 446 | (*build_id)[i] = (uint8_t) tmp; |
| 447 | } |
| 448 | ret = 0; |
| 449 | goto end; |
| 450 | |
| 451 | error: |
| 452 | g_free(*build_id); |
| 453 | ret = -1; |
| 454 | end: |
| 455 | bt_put(field_type); |
| 456 | bt_put(field); |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | static |
| 461 | struct debug_info *lookup_trace_debug_info(struct debug_info_iterator *debug_it, |
| 462 | struct bt_ctf_trace *writer_trace, |
| 463 | struct debug_info_trace *di_trace) |
| 464 | { |
| 465 | return (struct debug_info *) g_hash_table_lookup( |
| 466 | di_trace->trace_debug_map, |
| 467 | (gpointer) writer_trace); |
| 468 | } |
| 469 | |
| 470 | static |
| 471 | struct debug_info *insert_new_debug_info(struct debug_info_iterator *debug_it, |
| 472 | struct bt_ctf_trace *writer_trace, |
| 473 | struct debug_info_trace *di_trace) |
| 474 | { |
| 475 | struct debug_info *debug_info = NULL; |
| 476 | struct bt_value *field = NULL; |
| 477 | const char *str_value; |
| 478 | enum bt_value_status ret; |
| 479 | |
| 480 | field = bt_ctf_trace_get_environment_field_value_by_name(writer_trace, |
| 481 | "domain"); |
| 482 | /* No domain field, no debug info */ |
| 483 | if (!field) { |
| 484 | goto end; |
| 485 | } |
| 486 | ret = bt_value_string_get(field, &str_value); |
| 487 | if (ret != BT_VALUE_STATUS_OK) { |
| 488 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 489 | __FILE__, __LINE__); |
| 490 | goto end; |
| 491 | } |
| 492 | /* Domain not ust, no debug info */ |
| 493 | if (strcmp(str_value, "ust") != 0) { |
| 494 | goto end; |
| 495 | } |
| 496 | BT_PUT(field); |
| 497 | |
| 498 | /* No tracer_name, no debug info */ |
| 499 | field = bt_ctf_trace_get_environment_field_value_by_name(writer_trace, |
| 500 | "tracer_name"); |
| 501 | /* No tracer_name, no debug info */ |
| 502 | if (!field) { |
| 503 | goto end; |
| 504 | } |
| 505 | ret = bt_value_string_get(field, &str_value); |
| 506 | if (ret != BT_VALUE_STATUS_OK) { |
| 507 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 508 | __FILE__, __LINE__); |
| 509 | goto end; |
| 510 | } |
| 511 | /* Tracer_name not lttng-ust, no debug info */ |
| 512 | if (strcmp(str_value, "lttng-ust") != 0) { |
| 513 | goto end; |
| 514 | } |
| 515 | BT_PUT(field); |
| 516 | |
| 517 | debug_info = debug_info_create(debug_it->debug_info_component); |
| 518 | if (!debug_info) { |
| 519 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 520 | __FILE__, __LINE__); |
| 521 | goto end; |
| 522 | } |
| 523 | |
| 524 | g_hash_table_insert(di_trace->trace_debug_map, (gpointer) writer_trace, |
| 525 | debug_info); |
| 526 | |
| 527 | end: |
| 528 | bt_put(field); |
| 529 | return debug_info; |
| 530 | } |
| 531 | |
| 532 | static |
| 533 | struct debug_info *get_trace_debug_info(struct debug_info_iterator *debug_it, |
| 534 | struct bt_ctf_trace *writer_trace, |
| 535 | struct debug_info_trace *di_trace) |
| 536 | { |
| 537 | struct debug_info *debug_info; |
| 538 | |
| 539 | debug_info = lookup_trace_debug_info(debug_it, writer_trace, di_trace); |
| 540 | if (debug_info) { |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | debug_info = insert_new_debug_info(debug_it, writer_trace, di_trace); |
| 545 | |
| 546 | end: |
| 547 | return debug_info; |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | struct debug_info_trace *lookup_trace(struct debug_info_iterator *debug_it, |
| 552 | struct bt_ctf_trace *trace) |
| 553 | { |
| 554 | return (struct debug_info_trace *) g_hash_table_lookup( |
| 555 | debug_it->trace_map, |
| 556 | (gpointer) trace); |
| 557 | } |
| 558 | |
| 559 | static |
| 560 | enum debug_info_stream_state *insert_new_stream_state( |
| 561 | struct debug_info_iterator *debug_it, |
| 562 | struct debug_info_trace *di_trace, struct bt_ctf_stream *stream) |
| 563 | { |
| 564 | enum debug_info_stream_state *v = NULL; |
| 565 | |
| 566 | v = g_new0(enum debug_info_stream_state, 1); |
| 567 | if (!v) { |
| 568 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 569 | __FILE__, __LINE__); |
| 570 | goto end; |
| 571 | } |
| 572 | *v = DEBUG_INFO_UNKNOWN_STREAM; |
| 573 | |
| 574 | g_hash_table_insert(di_trace->stream_states, stream, v); |
| 575 | |
| 576 | end: |
| 577 | return v; |
| 578 | } |
| 579 | |
| 580 | static |
| 581 | void check_completed_trace(gpointer key, gpointer value, gpointer user_data) |
| 582 | { |
| 583 | enum debug_info_stream_state *state = value; |
| 584 | int *trace_completed = user_data; |
| 585 | |
| 586 | if (*state != DEBUG_INFO_COMPLETED_STREAM) { |
| 587 | *trace_completed = 0; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | static |
| 592 | gboolean empty_ht(gpointer key, gpointer value, gpointer user_data) |
| 593 | { |
| 594 | return TRUE; |
| 595 | } |
| 596 | |
| 597 | BT_HIDDEN |
| 598 | void debug_info_close_trace(struct debug_info_iterator *debug_it, |
| 599 | struct debug_info_trace *di_trace) |
| 600 | { |
| 601 | if (di_trace->static_listener_id >= 0) { |
| 602 | bt_ctf_trace_remove_is_static_listener(di_trace->trace, |
| 603 | di_trace->static_listener_id); |
| 604 | } |
| 605 | |
| 606 | /* Empty the stream class HT. */ |
| 607 | g_hash_table_foreach_remove(di_trace->stream_class_map, |
| 608 | empty_ht, NULL); |
| 609 | g_hash_table_destroy(di_trace->stream_class_map); |
| 610 | |
| 611 | /* Empty the stream HT. */ |
| 612 | g_hash_table_foreach_remove(di_trace->stream_map, |
| 613 | empty_ht, NULL); |
| 614 | g_hash_table_destroy(di_trace->stream_map); |
| 615 | |
| 616 | /* Empty the stream state HT. */ |
| 617 | g_hash_table_foreach_remove(di_trace->stream_states, |
| 618 | empty_ht, NULL); |
| 619 | g_hash_table_destroy(di_trace->stream_states); |
| 620 | |
| 621 | /* Empty the packet HT. */ |
| 622 | g_hash_table_foreach_remove(di_trace->packet_map, |
| 623 | empty_ht, NULL); |
| 624 | g_hash_table_destroy(di_trace->packet_map); |
| 625 | |
| 626 | /* Empty the trace_debug HT. */ |
| 627 | g_hash_table_foreach_remove(di_trace->trace_debug_map, |
| 628 | empty_ht, NULL); |
| 629 | g_hash_table_destroy(di_trace->trace_debug_map); |
| 630 | } |
| 631 | |
| 632 | static |
| 633 | int sync_event_classes(struct debug_info_iterator *debug_it, |
| 634 | struct bt_ctf_stream *stream, |
| 635 | struct bt_ctf_stream *writer_stream) |
| 636 | { |
| 637 | int int_ret; |
| 638 | struct bt_ctf_stream_class *stream_class = NULL, |
| 639 | *writer_stream_class = NULL; |
| 640 | enum bt_component_status ret; |
| 641 | |
| 642 | stream_class = bt_ctf_stream_get_class(stream); |
| 643 | if (!stream_class) { |
| 644 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 645 | __FILE__, __LINE__); |
| 646 | goto error; |
| 647 | } |
| 648 | |
| 649 | writer_stream_class = bt_ctf_stream_get_class(writer_stream); |
| 650 | if (!writer_stream_class) { |
| 651 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 652 | __FILE__, __LINE__); |
| 653 | goto error; |
| 654 | } |
| 655 | |
| 656 | ret = ctf_copy_event_classes(debug_it->err, stream_class, |
| 657 | writer_stream_class); |
| 658 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 659 | goto error; |
| 660 | } |
| 661 | |
| 662 | int_ret = 0; |
| 663 | goto end; |
| 664 | |
| 665 | error: |
| 666 | int_ret = -1; |
| 667 | end: |
| 668 | bt_put(stream_class); |
| 669 | bt_put(writer_stream_class); |
| 670 | return int_ret; |
| 671 | } |
| 672 | |
| 673 | static |
| 674 | void trace_is_static_listener(struct bt_ctf_trace *trace, void *data) |
| 675 | { |
| 676 | struct debug_info_trace *di_trace = data; |
| 677 | struct debug_info_iterator *debug_it = di_trace->debug_it; |
| 678 | int trace_completed = 1, ret, nr_stream, i; |
| 679 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
| 680 | struct bt_ctf_trace *writer_trace = di_trace->writer_trace; |
| 681 | |
| 682 | /* |
| 683 | * When the trace becomes static, make sure that we have all |
| 684 | * the event classes in our stream_class copies before setting it |
| 685 | * static as well. |
| 686 | */ |
| 687 | nr_stream = bt_ctf_trace_get_stream_count(trace); |
| 688 | for (i = 0; i < nr_stream; i++) { |
| 689 | stream = bt_ctf_trace_get_stream_by_index(trace, i); |
| 690 | if (!stream) { |
| 691 | fprintf(debug_it->err, |
| 692 | "[error] %s in %s:%d\n", __func__, |
| 693 | __FILE__, __LINE__); |
| 694 | goto error; |
| 695 | } |
| 696 | writer_stream = bt_ctf_trace_get_stream_by_index(writer_trace, i); |
| 697 | if (!writer_stream) { |
| 698 | fprintf(debug_it->err, |
| 699 | "[error] %s in %s:%d\n", __func__, |
| 700 | __FILE__, __LINE__); |
| 701 | goto error; |
| 702 | } |
| 703 | ret = sync_event_classes(di_trace->debug_it, stream, writer_stream); |
| 704 | if (ret) { |
| 705 | fprintf(debug_it->err, |
| 706 | "[error] %s in %s:%d\n", __func__, |
| 707 | __FILE__, __LINE__); |
| 708 | goto error; |
| 709 | } |
| 710 | BT_PUT(stream); |
| 711 | BT_PUT(writer_stream); |
| 712 | } |
| 713 | |
| 714 | bt_ctf_trace_set_is_static(di_trace->writer_trace); |
| 715 | di_trace->trace_static = 1; |
| 716 | |
| 717 | g_hash_table_foreach(di_trace->stream_states, |
| 718 | check_completed_trace, &trace_completed); |
| 719 | if (trace_completed) { |
| 720 | debug_info_close_trace(di_trace->debug_it, di_trace); |
| 721 | g_hash_table_remove(di_trace->debug_it->trace_map, |
| 722 | di_trace->trace); |
| 723 | } |
| 724 | |
| 725 | error: |
| 726 | bt_put(writer_stream); |
| 727 | bt_put(stream); |
| 728 | } |
| 729 | |
| 730 | static |
| 731 | struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it, |
| 732 | struct bt_ctf_stream *stream) { |
| 733 | struct bt_ctf_trace *writer_trace = NULL; |
| 734 | struct debug_info_trace *di_trace = NULL; |
| 735 | struct bt_ctf_trace *trace = NULL; |
| 736 | struct bt_ctf_stream_class *stream_class = NULL; |
| 737 | struct bt_ctf_stream *writer_stream = NULL; |
| 738 | int ret, nr_stream, i; |
| 739 | |
| 740 | writer_trace = bt_ctf_trace_create(); |
| 741 | if (!writer_trace) { |
| 742 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 743 | __FILE__, __LINE__); |
| 744 | goto error; |
| 745 | } |
| 746 | |
| 747 | stream_class = bt_ctf_stream_get_class(stream); |
| 748 | if (!stream_class) { |
| 749 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 750 | __FILE__, __LINE__); |
| 751 | goto error; |
| 752 | } |
| 753 | |
| 754 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 755 | if (!trace) { |
| 756 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 757 | __FILE__, __LINE__); |
| 758 | goto error; |
| 759 | } |
| 760 | |
| 761 | ret = ctf_copy_trace(debug_it->err, trace, writer_trace); |
| 762 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 763 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 764 | __FILE__, __LINE__); |
| 765 | goto error; |
| 766 | } |
| 767 | |
| 768 | di_trace = g_new0(struct debug_info_trace, 1); |
| 769 | if (!di_trace) { |
| 770 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 771 | __FILE__, __LINE__); |
| 772 | goto error; |
| 773 | } |
| 774 | |
| 775 | di_trace->trace = trace; |
| 776 | di_trace->writer_trace = writer_trace; |
| 777 | di_trace->debug_info_component = debug_it->debug_info_component; |
| 778 | di_trace->debug_it = debug_it; |
| 779 | di_trace->stream_map = g_hash_table_new_full(g_direct_hash, |
| 780 | g_direct_equal, NULL, (GDestroyNotify) unref_stream); |
| 781 | di_trace->stream_class_map = g_hash_table_new_full(g_direct_hash, |
| 782 | g_direct_equal, NULL, (GDestroyNotify) unref_stream_class); |
| 783 | di_trace->packet_map = g_hash_table_new_full(g_direct_hash, |
| 784 | g_direct_equal, NULL, (GDestroyNotify) unref_packet); |
| 785 | di_trace->trace_debug_map = g_hash_table_new_full(g_direct_hash, |
| 786 | g_direct_equal, NULL, (GDestroyNotify) unref_debug_info); |
| 787 | di_trace->stream_states = g_hash_table_new_full(g_direct_hash, |
| 788 | g_direct_equal, NULL, destroy_stream_state_key); |
| 789 | g_hash_table_insert(debug_it->trace_map, (gpointer) trace, di_trace); |
| 790 | |
| 791 | /* Set all the existing streams in the unknown state. */ |
| 792 | nr_stream = bt_ctf_trace_get_stream_count(trace); |
| 793 | for (i = 0; i < nr_stream; i++) { |
| 794 | stream = bt_ctf_trace_get_stream_by_index(trace, i); |
| 795 | if (!stream) { |
| 796 | fprintf(debug_it->err, |
| 797 | "[error] %s in %s:%d\n", __func__, |
| 798 | __FILE__, __LINE__); |
| 799 | goto error; |
| 800 | } |
| 801 | insert_new_stream_state(debug_it, di_trace, stream); |
| 802 | writer_stream = insert_new_stream(debug_it, stream, di_trace); |
| 803 | if (!writer_stream) { |
| 804 | fprintf(debug_it->err, |
| 805 | "[error] %s in %s:%d\n", __func__, |
| 806 | __FILE__, __LINE__); |
| 807 | goto error; |
| 808 | } |
| 809 | bt_get(writer_stream); |
| 810 | ret = sync_event_classes(debug_it, stream, writer_stream); |
| 811 | if (ret) { |
| 812 | fprintf(debug_it->err, |
| 813 | "[error] %s in %s:%d\n", __func__, |
| 814 | __FILE__, __LINE__); |
| 815 | goto error; |
| 816 | } |
| 817 | BT_PUT(writer_stream); |
| 818 | BT_PUT(stream); |
| 819 | } |
| 820 | |
| 821 | /* Check if the trace is already static or register a listener. */ |
| 822 | if (bt_ctf_trace_is_static(trace)) { |
| 823 | di_trace->trace_static = 1; |
| 824 | di_trace->static_listener_id = -1; |
| 825 | bt_ctf_trace_set_is_static(writer_trace); |
| 826 | } else { |
| 827 | ret = bt_ctf_trace_add_is_static_listener(trace, |
| 828 | trace_is_static_listener, di_trace); |
| 829 | if (ret < 0) { |
| 830 | fprintf(debug_it->err, |
| 831 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 832 | __LINE__); |
| 833 | goto error; |
| 834 | } |
| 835 | di_trace->static_listener_id = ret; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | goto end; |
| 840 | |
| 841 | error: |
| 842 | BT_PUT(writer_trace); |
| 843 | g_free(di_trace); |
| 844 | di_trace = NULL; |
| 845 | end: |
| 846 | bt_put(stream); |
| 847 | bt_put(writer_stream); |
| 848 | bt_put(stream_class); |
| 849 | bt_put(trace); |
| 850 | return di_trace; |
| 851 | } |
| 852 | |
| 853 | static |
| 854 | struct bt_ctf_packet *lookup_packet(struct debug_info_iterator *debug_it, |
| 855 | struct bt_ctf_packet *packet, |
| 856 | struct debug_info_trace *di_trace) |
| 857 | { |
| 858 | return (struct bt_ctf_packet *) g_hash_table_lookup( |
| 859 | di_trace->packet_map, |
| 860 | (gpointer) packet); |
| 861 | } |
| 862 | |
| 863 | static |
| 864 | struct bt_ctf_packet *insert_new_packet(struct debug_info_iterator *debug_it, |
| 865 | struct bt_ctf_packet *packet, |
| 866 | struct bt_ctf_stream *writer_stream, |
| 867 | struct debug_info_trace *di_trace) |
| 868 | { |
| 869 | struct bt_ctf_packet *writer_packet; |
| 870 | |
| 871 | writer_packet = bt_ctf_packet_create(writer_stream); |
| 872 | if (!writer_packet) { |
| 873 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 874 | __FILE__, __LINE__); |
| 875 | goto end; |
| 876 | } |
| 877 | g_hash_table_insert(di_trace->packet_map, (gpointer) packet, writer_packet); |
| 878 | |
| 879 | end: |
| 880 | return writer_packet; |
| 881 | } |
| 882 | |
| 883 | static |
| 884 | int add_debug_info_fields(FILE *err, |
| 885 | struct bt_ctf_field_type *writer_event_context_type, |
| 886 | struct debug_info_component *component) |
| 887 | { |
| 888 | struct bt_ctf_field_type *ip_field = NULL, *debug_field_type = NULL, |
| 889 | *bin_field_type = NULL, *func_field_type = NULL, |
| 890 | *src_field_type = NULL; |
| 891 | int ret = 0; |
| 892 | |
| 893 | ip_field = bt_ctf_field_type_structure_get_field_type_by_name( |
| 894 | writer_event_context_type, "_ip"); |
| 895 | /* No ip field, so no debug info. */ |
| 896 | if (!ip_field) { |
| 897 | goto end; |
| 898 | } |
| 899 | BT_PUT(ip_field); |
| 900 | |
| 901 | debug_field_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 902 | writer_event_context_type, |
| 903 | component->arg_debug_info_field_name); |
| 904 | /* Already existing debug_info field, no need to add it. */ |
| 905 | if (debug_field_type) { |
| 906 | goto end; |
| 907 | } |
| 908 | |
| 909 | debug_field_type = bt_ctf_field_type_structure_create(); |
| 910 | if (!debug_field_type) { |
| 911 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 912 | __FILE__, __LINE__); |
| 913 | goto error; |
| 914 | } |
| 915 | |
| 916 | bin_field_type = bt_ctf_field_type_string_create(); |
| 917 | if (!bin_field_type) { |
| 918 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 919 | __LINE__); |
| 920 | goto error; |
| 921 | } |
| 922 | |
| 923 | func_field_type = bt_ctf_field_type_string_create(); |
| 924 | if (!func_field_type) { |
| 925 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 926 | __LINE__); |
| 927 | goto error; |
| 928 | } |
| 929 | |
| 930 | src_field_type = bt_ctf_field_type_string_create(); |
| 931 | if (!src_field_type) { |
| 932 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 933 | __LINE__); |
| 934 | goto error; |
| 935 | } |
| 936 | |
| 937 | ret = bt_ctf_field_type_structure_add_field(debug_field_type, |
| 938 | bin_field_type, "bin"); |
| 939 | if (ret) { |
| 940 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 941 | __LINE__); |
| 942 | goto error; |
| 943 | } |
| 944 | |
| 945 | ret = bt_ctf_field_type_structure_add_field(debug_field_type, |
| 946 | func_field_type, "func"); |
| 947 | if (ret) { |
| 948 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 949 | __LINE__); |
| 950 | goto error; |
| 951 | } |
| 952 | |
| 953 | ret = bt_ctf_field_type_structure_add_field(debug_field_type, |
| 954 | src_field_type, "src"); |
| 955 | if (ret) { |
| 956 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 957 | __LINE__); |
| 958 | goto error; |
| 959 | } |
| 960 | |
| 961 | ret = bt_ctf_field_type_structure_add_field(writer_event_context_type, |
| 962 | debug_field_type, component->arg_debug_info_field_name); |
| 963 | if (ret) { |
| 964 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 965 | __LINE__); |
| 966 | goto error; |
| 967 | } |
| 968 | |
| 969 | ret = 0; |
| 970 | goto end; |
| 971 | |
| 972 | error: |
| 973 | BT_PUT(debug_field_type); |
| 974 | ret = -1; |
| 975 | end: |
| 976 | bt_put(src_field_type); |
| 977 | bt_put(func_field_type); |
| 978 | bt_put(bin_field_type); |
| 979 | bt_put(debug_field_type); |
| 980 | return ret; |
| 981 | } |
| 982 | |
| 983 | static |
| 984 | int create_debug_info_event_context_type(FILE *err, |
| 985 | struct bt_ctf_field_type *event_context_type, |
| 986 | struct bt_ctf_field_type *writer_event_context_type, |
| 987 | struct debug_info_component *component) |
| 988 | { |
| 989 | int ret, nr_fields, i; |
| 990 | |
| 991 | nr_fields = bt_ctf_field_type_structure_get_field_count(event_context_type); |
| 992 | for (i = 0; i < nr_fields; i++) { |
| 993 | struct bt_ctf_field_type *field_type = NULL; |
| 994 | const char *field_name; |
| 995 | |
| 996 | if (bt_ctf_field_type_structure_get_field(event_context_type, |
| 997 | &field_name, &field_type, i) < 0) { |
| 998 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 999 | __FILE__, __LINE__); |
| 1000 | goto error; |
| 1001 | } |
| 1002 | |
| 1003 | ret = bt_ctf_field_type_structure_add_field(writer_event_context_type, |
| 1004 | field_type, field_name); |
| 1005 | BT_PUT(field_type); |
| 1006 | if (ret) { |
| 1007 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1008 | __LINE__); |
| 1009 | goto error; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | ret = add_debug_info_fields(err, writer_event_context_type, |
| 1014 | component); |
| 1015 | goto end; |
| 1016 | |
| 1017 | error: |
| 1018 | ret = -1; |
| 1019 | end: |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
| 1023 | static |
| 1024 | struct bt_ctf_stream_class *copy_stream_class_debug_info(FILE *err, |
| 1025 | struct bt_ctf_stream_class *stream_class, |
| 1026 | struct bt_ctf_trace *writer_trace, |
| 1027 | struct debug_info_component *component) |
| 1028 | { |
| 1029 | struct bt_ctf_field_type *type = NULL; |
| 1030 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 1031 | struct bt_ctf_field_type *writer_event_context_type = NULL; |
| 1032 | int ret_int; |
| 1033 | const char *name = bt_ctf_stream_class_get_name(stream_class); |
| 1034 | |
| 1035 | if (strlen(name) == 0) { |
| 1036 | name = NULL; |
| 1037 | } |
| 1038 | |
| 1039 | writer_stream_class = bt_ctf_stream_class_create(name); |
| 1040 | if (!writer_stream_class) { |
| 1041 | fprintf(err, "[error] %s in %s:%d\n", |
| 1042 | __func__, __FILE__, __LINE__); |
| 1043 | goto error; |
| 1044 | } |
| 1045 | |
| 1046 | type = bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 1047 | if (!type) { |
| 1048 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1049 | __LINE__); |
| 1050 | goto error; |
| 1051 | } |
| 1052 | |
| 1053 | ret_int = bt_ctf_stream_class_set_packet_context_type( |
| 1054 | writer_stream_class, type); |
| 1055 | if (ret_int < 0) { |
| 1056 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1057 | __LINE__); |
| 1058 | goto error; |
| 1059 | } |
| 1060 | BT_PUT(type); |
| 1061 | |
| 1062 | type = bt_ctf_stream_class_get_event_header_type(stream_class); |
| 1063 | if (!type) { |
| 1064 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1065 | __LINE__); |
| 1066 | goto error; |
| 1067 | } |
| 1068 | |
| 1069 | ret_int = bt_ctf_stream_class_set_event_header_type( |
| 1070 | writer_stream_class, type); |
| 1071 | if (ret_int < 0) { |
| 1072 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1073 | __LINE__); |
| 1074 | goto error; |
| 1075 | } |
| 1076 | BT_PUT(type); |
| 1077 | |
| 1078 | type = bt_ctf_stream_class_get_event_context_type(stream_class); |
| 1079 | if (type) { |
| 1080 | writer_event_context_type = bt_ctf_field_type_structure_create(); |
| 1081 | if (!writer_event_context_type) { |
| 1082 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1083 | __LINE__); |
| 1084 | goto error; |
| 1085 | } |
| 1086 | ret_int = create_debug_info_event_context_type(err, type, |
| 1087 | writer_event_context_type, component); |
| 1088 | if (ret_int) { |
| 1089 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1090 | __LINE__); |
| 1091 | goto error; |
| 1092 | } |
| 1093 | BT_PUT(type); |
| 1094 | |
| 1095 | ret_int = bt_ctf_stream_class_set_event_context_type( |
| 1096 | writer_stream_class, writer_event_context_type); |
| 1097 | if (ret_int < 0) { |
| 1098 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1099 | __LINE__); |
| 1100 | goto error; |
| 1101 | } |
| 1102 | BT_PUT(writer_event_context_type); |
| 1103 | } |
| 1104 | |
| 1105 | goto end; |
| 1106 | |
| 1107 | error: |
| 1108 | BT_PUT(writer_stream_class); |
| 1109 | end: |
| 1110 | bt_put(writer_event_context_type); |
| 1111 | bt_put(type); |
| 1112 | return writer_stream_class; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Add the original clock classes to the new trace, we do not need to copy |
| 1117 | * them, and if we did, we would have to manually inspect the stream class |
| 1118 | * to update the integers mapping to a clock. |
| 1119 | */ |
| 1120 | static |
| 1121 | int add_clock_classes(FILE *err, struct bt_ctf_trace *writer_trace, |
| 1122 | struct bt_ctf_stream_class *writer_stream_class, |
| 1123 | struct bt_ctf_trace *trace) |
| 1124 | { |
| 1125 | int ret, clock_class_count, i; |
| 1126 | |
| 1127 | clock_class_count = bt_ctf_trace_get_clock_class_count(trace); |
| 1128 | |
| 1129 | for (i = 0; i < clock_class_count; i++) { |
| 1130 | struct bt_ctf_clock_class *clock_class = |
| 1131 | bt_ctf_trace_get_clock_class_by_index(trace, i); |
| 1132 | |
| 1133 | if (!clock_class) { |
| 1134 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1135 | __LINE__); |
| 1136 | goto error; |
| 1137 | } |
| 1138 | |
| 1139 | ret = bt_ctf_trace_add_clock_class(writer_trace, clock_class); |
| 1140 | BT_PUT(clock_class); |
| 1141 | if (ret != 0) { |
| 1142 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1143 | __LINE__); |
| 1144 | goto error; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | ret = 0; |
| 1149 | goto end; |
| 1150 | |
| 1151 | error: |
| 1152 | ret = -1; |
| 1153 | end: |
| 1154 | return ret; |
| 1155 | |
| 1156 | } |
| 1157 | |
| 1158 | static |
| 1159 | struct bt_ctf_stream_class *insert_new_stream_class( |
| 1160 | struct debug_info_iterator *debug_it, |
| 1161 | struct bt_ctf_stream_class *stream_class) |
| 1162 | { |
| 1163 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 1164 | struct bt_ctf_trace *trace, *writer_trace = NULL; |
| 1165 | struct debug_info_trace *di_trace; |
| 1166 | enum bt_component_status ret; |
| 1167 | int int_ret; |
| 1168 | |
| 1169 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 1170 | if (!trace) { |
| 1171 | fprintf(debug_it->err, |
| 1172 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1173 | __LINE__); |
| 1174 | goto error; |
| 1175 | } |
| 1176 | |
| 1177 | di_trace = lookup_trace(debug_it, trace); |
| 1178 | if (!di_trace) { |
| 1179 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 1180 | __FILE__, __LINE__); |
| 1181 | ret = BT_COMPONENT_STATUS_ERROR; |
| 1182 | goto error; |
| 1183 | } |
| 1184 | writer_trace = di_trace->writer_trace; |
| 1185 | bt_get(writer_trace); |
| 1186 | |
| 1187 | writer_stream_class = copy_stream_class_debug_info(debug_it->err, stream_class, |
| 1188 | writer_trace, debug_it->debug_info_component); |
| 1189 | if (!writer_stream_class) { |
| 1190 | fprintf(debug_it->err, "[error] Failed to copy stream class\n"); |
| 1191 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1192 | __func__, __FILE__, __LINE__); |
| 1193 | goto error; |
| 1194 | } |
| 1195 | |
| 1196 | int_ret = bt_ctf_trace_add_stream_class(writer_trace, writer_stream_class); |
| 1197 | if (int_ret) { |
| 1198 | fprintf(debug_it->err, |
| 1199 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1200 | __LINE__); |
| 1201 | goto error; |
| 1202 | } |
| 1203 | |
| 1204 | ret = add_clock_classes(debug_it->err, writer_trace, |
| 1205 | writer_stream_class, trace); |
| 1206 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1207 | fprintf(debug_it->err, |
| 1208 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1209 | __LINE__); |
| 1210 | goto error; |
| 1211 | } |
| 1212 | |
| 1213 | g_hash_table_insert(di_trace->stream_class_map, |
| 1214 | (gpointer) stream_class, writer_stream_class); |
| 1215 | |
| 1216 | goto end; |
| 1217 | |
| 1218 | error: |
| 1219 | BT_PUT(writer_stream_class); |
| 1220 | end: |
| 1221 | bt_put(trace); |
| 1222 | bt_put(writer_trace); |
| 1223 | return writer_stream_class; |
| 1224 | } |
| 1225 | |
| 1226 | static |
| 1227 | struct bt_ctf_stream *insert_new_stream( |
| 1228 | struct debug_info_iterator *debug_it, |
| 1229 | struct bt_ctf_stream *stream, |
| 1230 | struct debug_info_trace *di_trace) |
| 1231 | { |
| 1232 | struct bt_ctf_stream *writer_stream = NULL; |
| 1233 | struct bt_ctf_stream_class *stream_class = NULL; |
| 1234 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 1235 | |
| 1236 | stream_class = bt_ctf_stream_get_class(stream); |
| 1237 | if (!stream_class) { |
| 1238 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1239 | __func__, __FILE__, __LINE__); |
| 1240 | goto error; |
| 1241 | } |
| 1242 | |
| 1243 | writer_stream_class = g_hash_table_lookup( |
| 1244 | di_trace->stream_class_map, |
| 1245 | (gpointer) stream_class); |
| 1246 | |
| 1247 | if (!writer_stream_class) { |
| 1248 | writer_stream_class = insert_new_stream_class(debug_it, |
| 1249 | stream_class); |
| 1250 | if (!writer_stream_class) { |
| 1251 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1252 | __func__, __FILE__, __LINE__); |
| 1253 | goto error; |
| 1254 | } |
| 1255 | } |
| 1256 | bt_get(writer_stream_class); |
| 1257 | |
| 1258 | writer_stream = bt_ctf_stream_create(writer_stream_class, |
| 1259 | bt_ctf_stream_get_name(stream)); |
| 1260 | if (!writer_stream) { |
| 1261 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1262 | __func__, __FILE__, __LINE__); |
| 1263 | goto error; |
| 1264 | } |
| 1265 | |
| 1266 | g_hash_table_insert(di_trace->stream_map, (gpointer) stream, |
| 1267 | writer_stream); |
| 1268 | |
| 1269 | goto end; |
| 1270 | |
| 1271 | error: |
| 1272 | BT_PUT(writer_stream); |
| 1273 | end: |
| 1274 | bt_put(stream_class); |
| 1275 | bt_put(writer_stream_class); |
| 1276 | return writer_stream; |
| 1277 | } |
| 1278 | |
| 1279 | static |
| 1280 | struct bt_ctf_stream *lookup_stream(struct debug_info_iterator *debug_it, |
| 1281 | struct bt_ctf_stream *stream, |
| 1282 | struct debug_info_trace *di_trace) |
| 1283 | { |
| 1284 | return (struct bt_ctf_stream *) g_hash_table_lookup( |
| 1285 | di_trace->stream_map, (gpointer) stream); |
| 1286 | } |
| 1287 | |
| 1288 | static |
| 1289 | struct bt_ctf_event_class *get_event_class(struct debug_info_iterator *debug_it, |
| 1290 | struct bt_ctf_stream_class *writer_stream_class, |
| 1291 | struct bt_ctf_event_class *event_class) |
| 1292 | { |
| 1293 | return bt_ctf_stream_class_get_event_class_by_id(writer_stream_class, |
| 1294 | bt_ctf_event_class_get_id(event_class)); |
| 1295 | } |
| 1296 | |
| 1297 | static |
| 1298 | struct debug_info_trace *lookup_di_trace_from_stream( |
| 1299 | struct debug_info_iterator *debug_it, |
| 1300 | struct bt_ctf_stream *stream) |
| 1301 | { |
| 1302 | struct bt_ctf_stream_class *stream_class = NULL; |
| 1303 | struct bt_ctf_trace *trace = NULL; |
| 1304 | struct debug_info_trace *di_trace = NULL; |
| 1305 | |
| 1306 | stream_class = bt_ctf_stream_get_class(stream); |
| 1307 | if (!stream_class) { |
| 1308 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1309 | __func__, __FILE__, __LINE__); |
| 1310 | goto end; |
| 1311 | } |
| 1312 | |
| 1313 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 1314 | if (!trace) { |
| 1315 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1316 | __func__, __FILE__, __LINE__); |
| 1317 | goto end; |
| 1318 | } |
| 1319 | |
| 1320 | di_trace = (struct debug_info_trace *) g_hash_table_lookup( |
| 1321 | debug_it->trace_map, (gpointer) trace); |
| 1322 | |
| 1323 | end: |
| 1324 | BT_PUT(stream_class); |
| 1325 | BT_PUT(trace); |
| 1326 | return di_trace; |
| 1327 | } |
| 1328 | |
| 1329 | static |
| 1330 | struct bt_ctf_stream *get_writer_stream( |
| 1331 | struct debug_info_iterator *debug_it, |
| 1332 | struct bt_ctf_packet *packet, struct bt_ctf_stream *stream) |
| 1333 | { |
| 1334 | struct bt_ctf_stream_class *stream_class = NULL; |
| 1335 | struct bt_ctf_stream *writer_stream = NULL; |
| 1336 | struct debug_info_trace *di_trace = NULL; |
| 1337 | |
| 1338 | stream_class = bt_ctf_stream_get_class(stream); |
| 1339 | if (!stream_class) { |
| 1340 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1341 | __func__, __FILE__, __LINE__); |
| 1342 | goto error; |
| 1343 | } |
| 1344 | |
| 1345 | di_trace = lookup_di_trace_from_stream(debug_it, stream); |
| 1346 | if (!di_trace) { |
| 1347 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1348 | __func__, __FILE__, __LINE__); |
| 1349 | goto error; |
| 1350 | } |
| 1351 | |
| 1352 | writer_stream = lookup_stream(debug_it, stream, di_trace); |
| 1353 | if (!writer_stream) { |
| 1354 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1355 | __func__, __FILE__, __LINE__); |
| 1356 | goto error; |
| 1357 | } |
| 1358 | bt_get(writer_stream); |
| 1359 | |
| 1360 | goto end; |
| 1361 | |
| 1362 | error: |
| 1363 | BT_PUT(writer_stream); |
| 1364 | end: |
| 1365 | bt_put(stream_class); |
| 1366 | return writer_stream; |
| 1367 | } |
| 1368 | |
| 1369 | BT_HIDDEN |
| 1370 | struct bt_ctf_packet *debug_info_new_packet( |
| 1371 | struct debug_info_iterator *debug_it, |
| 1372 | struct bt_ctf_packet *packet) |
| 1373 | { |
| 1374 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
| 1375 | struct bt_ctf_field *writer_packet_context = NULL; |
| 1376 | struct bt_ctf_packet *writer_packet = NULL; |
| 1377 | struct debug_info_trace *di_trace; |
| 1378 | int int_ret; |
| 1379 | |
| 1380 | stream = bt_ctf_packet_get_stream(packet); |
| 1381 | if (!stream) { |
| 1382 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1383 | __func__, __FILE__, __LINE__); |
| 1384 | goto error; |
| 1385 | } |
| 1386 | |
| 1387 | writer_stream = get_writer_stream(debug_it, packet, stream); |
| 1388 | if (!writer_stream) { |
| 1389 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1390 | __func__, __FILE__, __LINE__); |
| 1391 | goto error; |
| 1392 | } |
| 1393 | |
| 1394 | di_trace = lookup_di_trace_from_stream(debug_it, stream); |
| 1395 | if (!di_trace) { |
| 1396 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 1397 | __FILE__, __LINE__); |
| 1398 | goto error; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * If a packet was already opened, close it and remove it from |
| 1403 | * the HT. |
| 1404 | */ |
| 1405 | writer_packet = lookup_packet(debug_it, packet, di_trace); |
| 1406 | if (writer_packet) { |
| 1407 | g_hash_table_remove(di_trace->packet_map, packet); |
| 1408 | BT_PUT(writer_packet); |
| 1409 | } |
| 1410 | |
| 1411 | writer_packet = insert_new_packet(debug_it, packet, writer_stream, |
| 1412 | di_trace); |
| 1413 | if (!writer_packet) { |
| 1414 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1415 | __func__, __FILE__, __LINE__); |
| 1416 | goto error; |
| 1417 | } |
| 1418 | |
| 1419 | writer_packet_context = ctf_copy_packet_context(debug_it->err, packet, |
| 1420 | writer_stream); |
| 1421 | if (!writer_packet_context) { |
| 1422 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1423 | __func__, __FILE__, __LINE__); |
| 1424 | goto error; |
| 1425 | } |
| 1426 | |
| 1427 | int_ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context); |
| 1428 | if (int_ret) { |
| 1429 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1430 | __func__, __FILE__, __LINE__); |
| 1431 | goto error; |
| 1432 | } |
| 1433 | |
| 1434 | bt_get(writer_packet); |
| 1435 | goto end; |
| 1436 | |
| 1437 | error: |
| 1438 | |
| 1439 | end: |
| 1440 | bt_put(writer_packet_context); |
| 1441 | bt_put(writer_stream); |
| 1442 | bt_put(stream); |
| 1443 | return writer_packet; |
| 1444 | } |
| 1445 | |
| 1446 | BT_HIDDEN |
| 1447 | struct bt_ctf_packet *debug_info_close_packet( |
| 1448 | struct debug_info_iterator *debug_it, |
| 1449 | struct bt_ctf_packet *packet) |
| 1450 | { |
| 1451 | struct bt_ctf_packet *writer_packet = NULL; |
| 1452 | struct bt_ctf_stream *stream = NULL; |
| 1453 | struct debug_info_trace *di_trace; |
| 1454 | |
| 1455 | stream = bt_ctf_packet_get_stream(packet); |
| 1456 | if (!stream) { |
| 1457 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 1458 | __FILE__, __LINE__); |
| 1459 | goto end; |
| 1460 | } |
| 1461 | |
| 1462 | di_trace = lookup_di_trace_from_stream(debug_it, stream); |
| 1463 | if (!di_trace) { |
| 1464 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 1465 | __FILE__, __LINE__); |
| 1466 | goto end; |
| 1467 | } |
| 1468 | |
| 1469 | writer_packet = lookup_packet(debug_it, packet, di_trace); |
| 1470 | if (!writer_packet) { |
| 1471 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1472 | __func__, __FILE__, __LINE__); |
| 1473 | goto end; |
| 1474 | } |
| 1475 | bt_get(writer_packet); |
| 1476 | g_hash_table_remove(di_trace->packet_map, packet); |
| 1477 | |
| 1478 | end: |
| 1479 | bt_put(stream); |
| 1480 | return writer_packet; |
| 1481 | } |
| 1482 | |
| 1483 | BT_HIDDEN |
| 1484 | struct bt_ctf_stream *debug_info_stream_begin( |
| 1485 | struct debug_info_iterator *debug_it, |
| 1486 | struct bt_ctf_stream *stream) |
| 1487 | { |
| 1488 | struct bt_ctf_stream *writer_stream = NULL; |
| 1489 | enum debug_info_stream_state *state; |
| 1490 | struct debug_info_trace *di_trace = NULL; |
| 1491 | |
| 1492 | di_trace = lookup_di_trace_from_stream(debug_it, stream); |
| 1493 | if (!di_trace) { |
| 1494 | di_trace = insert_new_trace(debug_it, stream); |
| 1495 | if (!di_trace) { |
| 1496 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1497 | __func__, __FILE__, __LINE__); |
| 1498 | goto error; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | /* Set the stream as active */ |
| 1503 | state = g_hash_table_lookup(di_trace->stream_states, stream); |
| 1504 | if (!state) { |
| 1505 | if (di_trace->trace_static) { |
| 1506 | fprintf(debug_it->err, "[error] Adding a new stream " |
| 1507 | "on a static trace\n"); |
| 1508 | goto error; |
| 1509 | } |
| 1510 | state = insert_new_stream_state(debug_it, di_trace, |
| 1511 | stream); |
| 1512 | if (!state) { |
| 1513 | fprintf(debug_it->err, "[error] Adding a new stream " |
| 1514 | "on a static trace\n"); |
| 1515 | goto error; |
| 1516 | } |
| 1517 | } |
| 1518 | if (*state != DEBUG_INFO_UNKNOWN_STREAM) { |
| 1519 | fprintf(debug_it->err, "[error] Unexpected stream state %d\n", |
| 1520 | *state); |
| 1521 | goto error; |
| 1522 | } |
| 1523 | *state = DEBUG_INFO_ACTIVE_STREAM; |
| 1524 | |
| 1525 | writer_stream = lookup_stream(debug_it, stream, di_trace); |
| 1526 | if (!writer_stream) { |
| 1527 | writer_stream = insert_new_stream(debug_it, stream, di_trace); |
| 1528 | } |
| 1529 | bt_get(writer_stream); |
| 1530 | |
| 1531 | goto end; |
| 1532 | |
| 1533 | error: |
| 1534 | BT_PUT(writer_stream); |
| 1535 | end: |
| 1536 | return writer_stream; |
| 1537 | } |
| 1538 | |
| 1539 | BT_HIDDEN |
| 1540 | struct bt_ctf_stream *debug_info_stream_end(struct debug_info_iterator *debug_it, |
| 1541 | struct bt_ctf_stream *stream) |
| 1542 | { |
| 1543 | struct bt_ctf_stream *writer_stream = NULL; |
| 1544 | struct debug_info_trace *di_trace = NULL; |
| 1545 | enum debug_info_stream_state *state; |
| 1546 | |
| 1547 | di_trace = lookup_di_trace_from_stream(debug_it, stream); |
| 1548 | if (!di_trace) { |
| 1549 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1550 | __func__, __FILE__, __LINE__); |
| 1551 | goto error; |
| 1552 | } |
| 1553 | |
| 1554 | writer_stream = lookup_stream(debug_it, stream, di_trace); |
| 1555 | if (!writer_stream) { |
| 1556 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 1557 | __func__, __FILE__, __LINE__); |
| 1558 | goto error; |
| 1559 | } |
| 1560 | /* |
| 1561 | * Take the ref on the stream and keep it until the notification |
| 1562 | * is created. |
| 1563 | */ |
| 1564 | bt_get(writer_stream); |
| 1565 | |
| 1566 | state = g_hash_table_lookup(di_trace->stream_states, stream); |
| 1567 | if (*state != DEBUG_INFO_ACTIVE_STREAM) { |
| 1568 | fprintf(debug_it->err, "[error] Unexpected stream " |
| 1569 | "state %d\n", *state); |
| 1570 | goto error; |
| 1571 | } |
| 1572 | *state = DEBUG_INFO_COMPLETED_STREAM; |
| 1573 | |
| 1574 | g_hash_table_remove(di_trace->stream_map, stream); |
| 1575 | |
| 1576 | if (di_trace->trace_static) { |
| 1577 | int trace_completed = 1; |
| 1578 | |
| 1579 | g_hash_table_foreach(di_trace->stream_states, |
| 1580 | check_completed_trace, &trace_completed); |
| 1581 | if (trace_completed) { |
| 1582 | debug_info_close_trace(debug_it, di_trace); |
| 1583 | g_hash_table_remove(debug_it->trace_map, |
| 1584 | di_trace->trace); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | goto end; |
| 1589 | |
| 1590 | error: |
| 1591 | BT_PUT(writer_stream); |
| 1592 | |
| 1593 | end: |
| 1594 | return writer_stream; |
| 1595 | } |
| 1596 | |
| 1597 | static |
| 1598 | struct debug_info_source *lookup_debug_info(FILE *err, |
| 1599 | struct bt_ctf_event *event, |
| 1600 | struct debug_info *debug_info) |
| 1601 | { |
| 1602 | int64_t vpid; |
| 1603 | uint64_t ip; |
| 1604 | struct debug_info_source *dbg_info_src = NULL; |
| 1605 | int ret; |
| 1606 | |
| 1607 | ret = get_stream_event_context_int_field_value(err, event, |
| 1608 | "_vpid", &vpid); |
| 1609 | if (ret) { |
| 1610 | goto end; |
| 1611 | } |
| 1612 | |
| 1613 | ret = get_stream_event_context_unsigned_int_field_value(err, event, |
| 1614 | "_ip", &ip); |
| 1615 | if (ret) { |
| 1616 | goto end; |
| 1617 | } |
| 1618 | |
| 1619 | /* Get debug info for this context. */ |
| 1620 | dbg_info_src = debug_info_query(debug_info, vpid, ip); |
| 1621 | |
| 1622 | end: |
| 1623 | return dbg_info_src; |
| 1624 | } |
| 1625 | |
| 1626 | static |
| 1627 | int set_debug_info_field(FILE *err, struct bt_ctf_field *debug_field, |
| 1628 | struct debug_info_source *dbg_info_src, |
| 1629 | struct debug_info_component *component) |
| 1630 | { |
| 1631 | int i, nr_fields, ret = 0; |
| 1632 | struct bt_ctf_field_type *debug_field_type = NULL; |
| 1633 | struct bt_ctf_field *field = NULL; |
| 1634 | struct bt_ctf_field_type *field_type = NULL; |
| 1635 | |
| 1636 | debug_field_type = bt_ctf_field_get_type(debug_field); |
| 1637 | if (!debug_field_type) { |
| 1638 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1639 | __FILE__, __LINE__); |
| 1640 | goto error; |
| 1641 | } |
| 1642 | |
| 1643 | nr_fields = bt_ctf_field_type_structure_get_field_count(debug_field_type); |
| 1644 | for (i = 0; i < nr_fields; i++) { |
| 1645 | const char *field_name; |
| 1646 | |
| 1647 | if (bt_ctf_field_type_structure_get_field(debug_field_type, |
| 1648 | &field_name, &field_type, i) < 0) { |
| 1649 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1650 | __FILE__, __LINE__); |
| 1651 | goto error; |
| 1652 | } |
| 1653 | BT_PUT(field_type); |
| 1654 | |
| 1655 | field = bt_ctf_field_structure_get_field_by_index(debug_field, i); |
| 1656 | if (!strcmp(field_name, "bin")) { |
| 1657 | if (dbg_info_src && dbg_info_src->bin_path) { |
| 1658 | GString *tmp = g_string_new(NULL); |
| 1659 | |
| 1660 | if (component->arg_full_path) { |
| 1661 | g_string_printf(tmp, "%s%s", |
| 1662 | dbg_info_src->bin_path, |
| 1663 | dbg_info_src->bin_loc); |
| 1664 | } else { |
| 1665 | g_string_printf(tmp, "%s%s", |
| 1666 | dbg_info_src->short_bin_path, |
| 1667 | dbg_info_src->bin_loc); |
| 1668 | } |
| 1669 | ret = bt_ctf_field_string_set_value(field, tmp->str); |
| 1670 | g_string_free(tmp, true); |
| 1671 | } else { |
| 1672 | ret = bt_ctf_field_string_set_value(field, ""); |
| 1673 | } |
| 1674 | } else if (!strcmp(field_name, "func")) { |
| 1675 | if (dbg_info_src && dbg_info_src->func) { |
| 1676 | ret = bt_ctf_field_string_set_value(field, |
| 1677 | dbg_info_src->func); |
| 1678 | } else { |
| 1679 | ret = bt_ctf_field_string_set_value(field, ""); |
| 1680 | } |
| 1681 | } else if (!strcmp(field_name, "src")) { |
| 1682 | if (dbg_info_src && dbg_info_src->src_path) { |
| 1683 | GString *tmp = g_string_new(NULL); |
| 1684 | |
| 1685 | if (component->arg_full_path) { |
| 1686 | g_string_printf(tmp, "%s:%" PRId64, |
| 1687 | dbg_info_src->src_path, |
| 1688 | dbg_info_src->line_no); |
| 1689 | } else { |
| 1690 | g_string_printf(tmp, "%s:%" PRId64, |
| 1691 | dbg_info_src->short_src_path, |
| 1692 | dbg_info_src->line_no); |
| 1693 | } |
| 1694 | ret = bt_ctf_field_string_set_value(field, tmp->str); |
| 1695 | g_string_free(tmp, true); |
| 1696 | } else { |
| 1697 | ret = bt_ctf_field_string_set_value(field, ""); |
| 1698 | } |
| 1699 | } |
| 1700 | BT_PUT(field); |
| 1701 | if (ret) { |
| 1702 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1703 | __FILE__, __LINE__); |
| 1704 | goto error; |
| 1705 | } |
| 1706 | } |
| 1707 | ret = 0; |
| 1708 | goto end; |
| 1709 | |
| 1710 | error: |
| 1711 | ret = -1; |
| 1712 | end: |
| 1713 | bt_put(field_type); |
| 1714 | bt_put(field); |
| 1715 | bt_put(debug_field_type); |
| 1716 | return ret; |
| 1717 | } |
| 1718 | |
| 1719 | static |
| 1720 | int copy_set_debug_info_stream_event_context(FILE *err, |
| 1721 | struct bt_ctf_field *event_context, |
| 1722 | struct bt_ctf_event *event, |
| 1723 | struct bt_ctf_event *writer_event, |
| 1724 | struct debug_info *debug_info, |
| 1725 | struct debug_info_component *component) |
| 1726 | { |
| 1727 | struct bt_ctf_field_type *writer_event_context_type = NULL, |
| 1728 | *event_context_type = NULL; |
| 1729 | struct bt_ctf_field *writer_event_context = NULL; |
| 1730 | struct bt_ctf_field *field = NULL, *copy_field = NULL, *debug_field = NULL; |
| 1731 | struct bt_ctf_field_type *field_type = NULL; |
| 1732 | struct debug_info_source *dbg_info_src; |
| 1733 | int ret, nr_fields, i; |
| 1734 | |
| 1735 | writer_event_context = bt_ctf_event_get_stream_event_context(writer_event); |
| 1736 | if (!writer_event_context) { |
| 1737 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); |
| 1738 | goto error; |
| 1739 | } |
| 1740 | |
| 1741 | writer_event_context_type = bt_ctf_field_get_type(writer_event_context); |
| 1742 | if (!writer_event_context_type) { |
| 1743 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1744 | __FILE__, __LINE__); |
| 1745 | goto error; |
| 1746 | } |
| 1747 | |
| 1748 | event_context_type = bt_ctf_field_get_type(event_context); |
| 1749 | if (!event_context_type) { |
| 1750 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1751 | __FILE__, __LINE__); |
| 1752 | goto error; |
| 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | * If it is not a structure, we did not modify it to add the debug info |
| 1757 | * fields, so just assign it as is. |
| 1758 | */ |
| 1759 | if (bt_ctf_field_type_get_type_id(writer_event_context_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 1760 | ret = bt_ctf_event_set_event_context(writer_event, event_context); |
| 1761 | goto end; |
| 1762 | } |
| 1763 | |
| 1764 | dbg_info_src = lookup_debug_info(err, event, debug_info); |
| 1765 | |
| 1766 | nr_fields = bt_ctf_field_type_structure_get_field_count(writer_event_context_type); |
| 1767 | for (i = 0; i < nr_fields; i++) { |
| 1768 | const char *field_name; |
| 1769 | |
| 1770 | if (bt_ctf_field_type_structure_get_field(writer_event_context_type, |
| 1771 | &field_name, &field_type, i) < 0) { |
| 1772 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1773 | __FILE__, __LINE__); |
| 1774 | goto error; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * Prevent illegal access in the event_context. |
| 1779 | */ |
| 1780 | if (i < bt_ctf_field_type_structure_get_field_count(event_context_type)) { |
| 1781 | field = bt_ctf_field_structure_get_field_by_index(event_context, i); |
| 1782 | } |
| 1783 | /* |
| 1784 | * The debug_info field, only exists in the writer event or |
| 1785 | * if it was set by a earlier pass of the debug_info plugin. |
| 1786 | */ |
| 1787 | if (!strcmp(field_name, component->arg_debug_info_field_name) && |
| 1788 | !field) { |
| 1789 | debug_field = bt_ctf_field_structure_get_field_by_index( |
| 1790 | writer_event_context, i); |
| 1791 | if (!debug_field) { |
| 1792 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1793 | __FILE__, __LINE__); |
| 1794 | goto error; |
| 1795 | } |
| 1796 | ret = set_debug_info_field(err, debug_field, |
| 1797 | dbg_info_src, component); |
| 1798 | if (ret) { |
| 1799 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1800 | __FILE__, __LINE__); |
| 1801 | goto error; |
| 1802 | } |
| 1803 | BT_PUT(debug_field); |
| 1804 | } else { |
| 1805 | copy_field = bt_ctf_field_copy(field); |
| 1806 | if (!copy_field) { |
| 1807 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1808 | __FILE__, __LINE__); |
| 1809 | goto error; |
| 1810 | } |
| 1811 | |
| 1812 | ret = bt_ctf_field_structure_set_field(writer_event_context, |
| 1813 | field_name, copy_field); |
| 1814 | if (ret) { |
| 1815 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1816 | __FILE__, __LINE__); |
| 1817 | goto error; |
| 1818 | } |
| 1819 | BT_PUT(copy_field); |
| 1820 | } |
| 1821 | BT_PUT(field_type); |
| 1822 | BT_PUT(field); |
| 1823 | } |
| 1824 | |
| 1825 | ret = 0; |
| 1826 | goto end; |
| 1827 | |
| 1828 | error: |
| 1829 | ret = -1; |
| 1830 | end: |
| 1831 | bt_put(event_context_type); |
| 1832 | bt_put(writer_event_context_type); |
| 1833 | bt_put(writer_event_context); |
| 1834 | bt_put(field); |
| 1835 | bt_put(copy_field); |
| 1836 | bt_put(debug_field); |
| 1837 | bt_put(field_type); |
| 1838 | return ret; |
| 1839 | } |
| 1840 | |
| 1841 | static |
| 1842 | struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err, |
| 1843 | struct bt_ctf_stream_class *stream_class) |
| 1844 | { |
| 1845 | struct bt_ctf_trace *trace = NULL; |
| 1846 | struct bt_ctf_clock_class *clock_class = NULL; |
| 1847 | |
| 1848 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 1849 | if (!trace) { |
| 1850 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1851 | __LINE__); |
| 1852 | goto end; |
| 1853 | } |
| 1854 | |
| 1855 | /* FIXME multi-clock? */ |
| 1856 | clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0); |
| 1857 | |
| 1858 | bt_put(trace); |
| 1859 | |
| 1860 | end: |
| 1861 | return clock_class; |
| 1862 | } |
| 1863 | |
| 1864 | static |
| 1865 | struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event) |
| 1866 | { |
| 1867 | struct bt_ctf_event_class *event_class = NULL; |
| 1868 | struct bt_ctf_stream_class *stream_class = NULL; |
| 1869 | struct bt_ctf_clock_class *clock_class = NULL; |
| 1870 | |
| 1871 | event_class = bt_ctf_event_get_class(event); |
| 1872 | if (!event_class) { |
| 1873 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1874 | __LINE__); |
| 1875 | goto error; |
| 1876 | } |
| 1877 | |
| 1878 | stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 1879 | if (!stream_class) { |
| 1880 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1881 | __LINE__); |
| 1882 | goto error; |
| 1883 | } |
| 1884 | |
| 1885 | clock_class = stream_class_get_clock_class(err, stream_class); |
| 1886 | goto end; |
| 1887 | |
| 1888 | error: |
| 1889 | BT_PUT(clock_class); |
| 1890 | end: |
| 1891 | bt_put(stream_class); |
| 1892 | bt_put(event_class); |
| 1893 | return clock_class; |
| 1894 | } |
| 1895 | |
| 1896 | static |
| 1897 | int set_event_clock_value(FILE *err, struct bt_ctf_event *event, |
| 1898 | struct bt_ctf_event *writer_event) |
| 1899 | { |
| 1900 | struct bt_ctf_clock_class *clock_class = NULL; |
| 1901 | struct bt_ctf_clock_value *clock_value = NULL; |
| 1902 | int ret; |
| 1903 | |
| 1904 | clock_class = event_get_clock_class(err, event); |
| 1905 | if (!clock_class) { |
| 1906 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1907 | __LINE__); |
| 1908 | goto error; |
| 1909 | } |
| 1910 | |
| 1911 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); |
| 1912 | if (!clock_value) { |
| 1913 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1914 | __LINE__); |
| 1915 | goto error; |
| 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * We share the same clocks, so we can assign the clock value to the |
| 1920 | * writer event. |
| 1921 | */ |
| 1922 | ret = bt_ctf_event_set_clock_value(writer_event, clock_value); |
| 1923 | if (ret) { |
| 1924 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1925 | __LINE__); |
| 1926 | goto error; |
| 1927 | } |
| 1928 | |
| 1929 | ret = 0; |
| 1930 | goto end; |
| 1931 | |
| 1932 | error: |
| 1933 | ret = -1; |
| 1934 | end: |
| 1935 | bt_put(clock_class); |
| 1936 | bt_put(clock_value); |
| 1937 | return ret; |
| 1938 | } |
| 1939 | |
| 1940 | static |
| 1941 | struct bt_ctf_event *debug_info_copy_event(FILE *err, struct bt_ctf_event *event, |
| 1942 | struct bt_ctf_event_class *writer_event_class, |
| 1943 | struct debug_info *debug_info, |
| 1944 | struct debug_info_component *component) |
| 1945 | { |
| 1946 | struct bt_ctf_event *writer_event = NULL; |
| 1947 | struct bt_ctf_field *field = NULL, *copy_field = NULL; |
| 1948 | int ret; |
| 1949 | |
| 1950 | writer_event = bt_ctf_event_create(writer_event_class); |
| 1951 | if (!writer_event) { |
| 1952 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
| 1953 | __LINE__); |
| 1954 | goto error; |
| 1955 | } |
| 1956 | |
| 1957 | ret = set_event_clock_value(err, event, writer_event); |
| 1958 | if (ret) { |
| 1959 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1960 | __FILE__, __LINE__); |
| 1961 | goto error; |
| 1962 | } |
| 1963 | |
| 1964 | /* Optional field, so it can fail silently. */ |
| 1965 | field = bt_ctf_event_get_header(event); |
| 1966 | if (field) { |
| 1967 | ret = ctf_copy_event_header(err, event, writer_event_class, |
| 1968 | writer_event, field); |
| 1969 | if (ret) { |
| 1970 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1971 | __FILE__, __LINE__); |
| 1972 | goto error; |
| 1973 | } |
| 1974 | BT_PUT(field); |
| 1975 | } |
| 1976 | |
| 1977 | /* Optional field, so it can fail silently. */ |
| 1978 | field = bt_ctf_event_get_stream_event_context(event); |
| 1979 | if (field) { |
| 1980 | ret = copy_set_debug_info_stream_event_context(err, |
| 1981 | field, event, writer_event, debug_info, |
| 1982 | component); |
| 1983 | if (ret < 0) { |
| 1984 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1985 | __FILE__, __LINE__); |
| 1986 | goto error; |
| 1987 | } |
| 1988 | BT_PUT(field); |
| 1989 | } |
| 1990 | |
| 1991 | /* Optional field, so it can fail silently. */ |
| 1992 | field = bt_ctf_event_get_event_context(event); |
| 1993 | if (field) { |
| 1994 | copy_field = bt_ctf_field_copy(field); |
| 1995 | if (!copy_field) { |
| 1996 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 1997 | __FILE__, __LINE__); |
| 1998 | goto error; |
| 1999 | } |
| 2000 | ret = bt_ctf_event_set_event_context(writer_event, copy_field); |
| 2001 | if (ret < 0) { |
| 2002 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 2003 | __FILE__, __LINE__); |
| 2004 | goto error; |
| 2005 | } |
| 2006 | BT_PUT(copy_field); |
| 2007 | BT_PUT(field); |
| 2008 | } |
| 2009 | |
| 2010 | field = bt_ctf_event_get_event_payload(event); |
| 2011 | if (!field) { |
| 2012 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 2013 | __FILE__, __LINE__); |
| 2014 | goto error; |
| 2015 | } |
| 2016 | copy_field = bt_ctf_field_copy(field); |
| 2017 | if (copy_field) { |
| 2018 | ret = bt_ctf_event_set_event_payload(writer_event, copy_field); |
| 2019 | if (ret < 0) { |
| 2020 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
| 2021 | __FILE__, __LINE__); |
| 2022 | goto error; |
| 2023 | } |
| 2024 | BT_PUT(copy_field); |
| 2025 | } |
| 2026 | BT_PUT(field); |
| 2027 | |
| 2028 | goto end; |
| 2029 | |
| 2030 | error: |
| 2031 | BT_PUT(writer_event); |
| 2032 | end: |
| 2033 | bt_put(copy_field); |
| 2034 | bt_put(field); |
| 2035 | return writer_event; |
| 2036 | } |
| 2037 | |
| 2038 | BT_HIDDEN |
| 2039 | struct bt_ctf_event *debug_info_output_event( |
| 2040 | struct debug_info_iterator *debug_it, |
| 2041 | struct bt_ctf_event *event) |
| 2042 | { |
| 2043 | struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL; |
| 2044 | struct bt_ctf_stream_class *stream_class = NULL, *writer_stream_class = NULL; |
| 2045 | struct bt_ctf_event *writer_event = NULL; |
| 2046 | struct bt_ctf_packet *packet = NULL, *writer_packet = NULL; |
| 2047 | struct bt_ctf_trace *writer_trace = NULL; |
| 2048 | struct bt_ctf_stream *stream = NULL; |
| 2049 | struct debug_info_trace *di_trace; |
| 2050 | struct debug_info *debug_info; |
| 2051 | const char *event_name; |
| 2052 | int int_ret; |
| 2053 | |
| 2054 | event_class = bt_ctf_event_get_class(event); |
| 2055 | if (!event_class) { |
| 2056 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2057 | __FILE__, __LINE__); |
| 2058 | goto error; |
| 2059 | } |
| 2060 | |
| 2061 | event_name = bt_ctf_event_class_get_name(event_class); |
| 2062 | if (!event_name) { |
| 2063 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2064 | __FILE__, __LINE__); |
| 2065 | goto error; |
| 2066 | } |
| 2067 | |
| 2068 | stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 2069 | if (!stream_class) { |
| 2070 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2071 | __FILE__, __LINE__); |
| 2072 | goto error; |
| 2073 | } |
| 2074 | stream = bt_ctf_event_get_stream(event); |
| 2075 | if (!stream) { |
| 2076 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2077 | __FILE__, __LINE__); |
| 2078 | goto error; |
| 2079 | } |
| 2080 | di_trace = lookup_di_trace_from_stream(debug_it, stream); |
| 2081 | if (!di_trace) { |
| 2082 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2083 | __FILE__, __LINE__); |
| 2084 | goto error; |
| 2085 | } |
| 2086 | |
| 2087 | writer_stream_class = g_hash_table_lookup( |
| 2088 | di_trace->stream_class_map, |
| 2089 | (gpointer) stream_class); |
| 2090 | if (!writer_stream_class) { |
| 2091 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2092 | __FILE__, __LINE__); |
| 2093 | goto error; |
| 2094 | } |
| 2095 | bt_get(writer_stream_class); |
| 2096 | |
| 2097 | writer_event_class = get_event_class(debug_it, |
| 2098 | writer_stream_class, event_class); |
| 2099 | if (!writer_event_class) { |
| 2100 | writer_event_class = ctf_copy_event_class(debug_it->err, |
| 2101 | event_class); |
| 2102 | if (!writer_event_class) { |
| 2103 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 2104 | __func__, __FILE__, __LINE__); |
| 2105 | goto error; |
| 2106 | } |
| 2107 | int_ret = bt_ctf_stream_class_add_event_class( |
| 2108 | writer_stream_class, writer_event_class); |
| 2109 | if (int_ret) { |
| 2110 | fprintf(debug_it->err, "[error] %s in %s:%d\n", |
| 2111 | __func__, __FILE__, __LINE__); |
| 2112 | goto error; |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | writer_trace = bt_ctf_stream_class_get_trace(writer_stream_class); |
| 2117 | if (!writer_trace) { |
| 2118 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2119 | __FILE__, __LINE__); |
| 2120 | goto error; |
| 2121 | } |
| 2122 | |
| 2123 | debug_info = get_trace_debug_info(debug_it, writer_trace, di_trace); |
| 2124 | if (debug_info) { |
| 2125 | debug_info_handle_event(debug_it->err, event, debug_info); |
| 2126 | } |
| 2127 | |
| 2128 | writer_event = debug_info_copy_event(debug_it->err, event, |
| 2129 | writer_event_class, debug_info, |
| 2130 | debug_it->debug_info_component); |
| 2131 | if (!writer_event) { |
| 2132 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2133 | __FILE__, __LINE__); |
| 2134 | fprintf(debug_it->err, "[error] Failed to copy event %s\n", |
| 2135 | bt_ctf_event_class_get_name(writer_event_class)); |
| 2136 | goto error; |
| 2137 | } |
| 2138 | |
| 2139 | packet = bt_ctf_event_get_packet(event); |
| 2140 | if (!packet) { |
| 2141 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2142 | __FILE__, __LINE__); |
| 2143 | goto error; |
| 2144 | } |
| 2145 | |
| 2146 | writer_packet = lookup_packet(debug_it, packet, di_trace); |
| 2147 | if (!writer_packet) { |
| 2148 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2149 | __FILE__, __LINE__); |
| 2150 | goto error; |
| 2151 | } |
| 2152 | bt_get(writer_packet); |
| 2153 | |
| 2154 | int_ret = bt_ctf_event_set_packet(writer_event, writer_packet); |
| 2155 | if (int_ret < 0) { |
| 2156 | fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__, |
| 2157 | __FILE__, __LINE__); |
| 2158 | fprintf(debug_it->err, "[error] Failed to append event %s\n", |
| 2159 | bt_ctf_event_class_get_name(writer_event_class)); |
| 2160 | goto error; |
| 2161 | } |
| 2162 | |
| 2163 | /* Keep the reference on the writer event */ |
| 2164 | goto end; |
| 2165 | |
| 2166 | error: |
| 2167 | BT_PUT(writer_event); |
| 2168 | |
| 2169 | end: |
| 2170 | bt_put(stream); |
| 2171 | bt_put(writer_trace); |
| 2172 | bt_put(writer_packet); |
| 2173 | bt_put(packet); |
| 2174 | bt_put(writer_event_class); |
| 2175 | bt_put(writer_stream_class); |
| 2176 | bt_put(stream_class); |
| 2177 | bt_put(event_class); |
| 2178 | return writer_event; |
| 2179 | } |