1 /* CTF format support. 2 3 Copyright (C) 2012-2024 Free Software Foundation, Inc. 4 Contributed by Hui Zhu <hui_zhu@mentor.com> 5 Contributed by Yao Qi <yao@codesourcery.com> 6 7 This file is part of GDB. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 22 #include "tracectf.h" 23 #include "tracepoint.h" 24 #include "regcache.h" 25 #include <sys/stat.h> 26 #include "exec.h" 27 #include "completer.h" 28 #include "inferior.h" 29 #include "gdbthread.h" 30 #include "tracefile.h" 31 #include <ctype.h> 32 #include <algorithm> 33 #include "gdbsupport/filestuff.h" 34 #include "gdbarch.h" 35 36 /* GDB saves trace buffers and other information (such as trace 37 status) got from the remote target into Common Trace Format (CTF). 38 The following types of information are expected to save in CTF: 39 40 1. The length (in bytes) of register cache. Event "register" will 41 be defined in metadata, which includes the length. 42 43 2. Trace status. Event "status" is defined in metadata, which 44 includes all aspects of trace status. 45 46 3. Uploaded trace variables. Event "tsv_def" is defined in 47 metadata, which is about all aspects of a uploaded trace variable. 48 Uploaded tracepoints. Event "tp_def" is defined in meta, which 49 is about all aspects of an uploaded tracepoint. Note that the 50 "sequence" (a CTF type, which is a dynamically-sized array.) is 51 used for "actions" "step_actions" and "cmd_strings". 52 53 4. Trace frames. Each trace frame is composed by several blocks 54 of different types ('R', 'M', 'V'). One trace frame is saved in 55 one CTF packet and the blocks of this frame are saved as events. 56 4.1: The trace frame related information (such as the number of 57 tracepoint associated with this frame) is saved in the packet 58 context. 59 4.2: The block 'M', 'R' and 'V' are saved in event "memory", 60 "register" and "tsv" respectively. 61 4.3: When iterating over events, babeltrace can't tell iterator 62 goes to a new packet, so we need a marker or anchor to tell GDB 63 that iterator goes into a new packet or frame. We define event 64 "frame". */ 65 66 #define CTF_MAGIC 0xC1FC1FC1 67 #define CTF_SAVE_MAJOR 1 68 #define CTF_SAVE_MINOR 8 69 70 #define CTF_METADATA_NAME "metadata" 71 #define CTF_DATASTREAM_NAME "datastream" 72 73 /* Reserved event id. */ 74 75 #define CTF_EVENT_ID_REGISTER 0 76 #define CTF_EVENT_ID_TSV 1 77 #define CTF_EVENT_ID_MEMORY 2 78 #define CTF_EVENT_ID_FRAME 3 79 #define CTF_EVENT_ID_STATUS 4 80 #define CTF_EVENT_ID_TSV_DEF 5 81 #define CTF_EVENT_ID_TP_DEF 6 82 83 #define CTF_PID (2) 84 85 /* The state kept while writing the CTF datastream file. */ 86 87 struct trace_write_handler 88 { 89 /* File descriptor of metadata. */ 90 FILE *metadata_fd; 91 /* File descriptor of traceframes. */ 92 FILE *datastream_fd; 93 94 /* This is the content size of the current packet. */ 95 size_t content_size; 96 97 /* This is the start offset of current packet. */ 98 long packet_start; 99 }; 100 101 /* Write metadata in FORMAT. */ 102 103 static void 104 ctf_save_write_metadata (struct trace_write_handler *handler, 105 const char *format, ...) 106 ATTRIBUTE_PRINTF (2, 3); 107 108 static void 109 ctf_save_write_metadata (struct trace_write_handler *handler, 110 const char *format, ...) 111 { 112 va_list args; 113 114 va_start (args, format); 115 if (vfprintf (handler->metadata_fd, format, args) < 0) 116 error (_("Unable to write metadata file (%s)"), 117 safe_strerror (errno)); 118 va_end (args); 119 } 120 121 /* Write BUF of length SIZE to datastream file represented by 122 HANDLER. */ 123 124 static int 125 ctf_save_write (struct trace_write_handler *handler, 126 const gdb_byte *buf, size_t size) 127 { 128 if (fwrite (buf, size, 1, handler->datastream_fd) != 1) 129 error (_("Unable to write file for saving trace data (%s)"), 130 safe_strerror (errno)); 131 132 handler->content_size += size; 133 134 return 0; 135 } 136 137 /* Write a unsigned 32-bit integer to datastream file represented by 138 HANDLER. */ 139 140 #define ctf_save_write_uint32(HANDLER, U32) \ 141 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4) 142 143 /* Write a signed 32-bit integer to datastream file represented by 144 HANDLER. */ 145 146 #define ctf_save_write_int32(HANDLER, INT32) \ 147 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4) 148 149 /* Set datastream file position. Update HANDLER->content_size 150 if WHENCE is SEEK_CUR. */ 151 152 static int 153 ctf_save_fseek (struct trace_write_handler *handler, long offset, 154 int whence) 155 { 156 gdb_assert (whence != SEEK_END); 157 gdb_assert (whence != SEEK_SET 158 || offset <= handler->content_size + handler->packet_start); 159 160 if (fseek (handler->datastream_fd, offset, whence)) 161 error (_("Unable to seek file for saving trace data (%s)"), 162 safe_strerror (errno)); 163 164 if (whence == SEEK_CUR) 165 handler->content_size += offset; 166 167 return 0; 168 } 169 170 /* Change the datastream file position to align on ALIGN_SIZE, 171 and write BUF to datastream file. The size of BUF is SIZE. */ 172 173 static int 174 ctf_save_align_write (struct trace_write_handler *handler, 175 const gdb_byte *buf, 176 size_t size, size_t align_size) 177 { 178 long offset 179 = (align_up (handler->content_size, align_size) 180 - handler->content_size); 181 182 if (ctf_save_fseek (handler, offset, SEEK_CUR)) 183 return -1; 184 185 if (ctf_save_write (handler, buf, size)) 186 return -1; 187 188 return 0; 189 } 190 191 /* Write events to next new packet. */ 192 193 static void 194 ctf_save_next_packet (struct trace_write_handler *handler) 195 { 196 handler->packet_start += (handler->content_size + 4); 197 ctf_save_fseek (handler, handler->packet_start, SEEK_SET); 198 handler->content_size = 0; 199 } 200 201 /* Write the CTF metadata header. */ 202 203 static void 204 ctf_save_metadata_header (struct trace_write_handler *handler) 205 { 206 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n", 207 CTF_SAVE_MAJOR, CTF_SAVE_MINOR); 208 ctf_save_write_metadata (handler, 209 "typealias integer { size = 8; align = 8; " 210 "signed = false; encoding = ascii;}" 211 " := ascii;\n"); 212 ctf_save_write_metadata (handler, 213 "typealias integer { size = 8; align = 8; " 214 "signed = false; }" 215 " := uint8_t;\n"); 216 ctf_save_write_metadata (handler, 217 "typealias integer { size = 16; align = 16;" 218 "signed = false; } := uint16_t;\n"); 219 ctf_save_write_metadata (handler, 220 "typealias integer { size = 32; align = 32;" 221 "signed = false; } := uint32_t;\n"); 222 ctf_save_write_metadata (handler, 223 "typealias integer { size = 64; align = 64;" 224 "signed = false; base = hex;}" 225 " := uint64_t;\n"); 226 ctf_save_write_metadata (handler, 227 "typealias integer { size = 32; align = 32;" 228 "signed = true; } := int32_t;\n"); 229 ctf_save_write_metadata (handler, 230 "typealias integer { size = 64; align = 64;" 231 "signed = true; } := int64_t;\n"); 232 ctf_save_write_metadata (handler, 233 "typealias string { encoding = ascii;" 234 " } := chars;\n"); 235 ctf_save_write_metadata (handler, "\n"); 236 237 /* Get the byte order of the host and write CTF data in this byte 238 order. */ 239 #if WORDS_BIGENDIAN 240 #define HOST_ENDIANNESS "be" 241 #else 242 #define HOST_ENDIANNESS "le" 243 #endif 244 245 ctf_save_write_metadata (handler, 246 "\ntrace {\n" 247 " major = %u;\n" 248 " minor = %u;\n" 249 " byte_order = %s;\n" 250 " packet.header := struct {\n" 251 " uint32_t magic;\n" 252 " };\n" 253 "};\n" 254 "\n" 255 "stream {\n" 256 " packet.context := struct {\n" 257 " uint32_t content_size;\n" 258 " uint32_t packet_size;\n" 259 " uint16_t tpnum;\n" 260 " };\n" 261 " event.header := struct {\n" 262 " uint32_t id;\n" 263 " };\n" 264 "};\n", 265 CTF_SAVE_MAJOR, CTF_SAVE_MINOR, 266 HOST_ENDIANNESS); 267 ctf_save_write_metadata (handler, "\n"); 268 } 269 270 /* CTF trace writer. */ 271 272 struct ctf_trace_file_writer 273 { 274 struct trace_file_writer base; 275 276 /* States related to writing CTF trace file. */ 277 struct trace_write_handler tcs; 278 }; 279 280 /* This is the implementation of trace_file_write_ops method 281 dtor. */ 282 283 static void 284 ctf_dtor (struct trace_file_writer *self) 285 { 286 struct ctf_trace_file_writer *writer 287 = (struct ctf_trace_file_writer *) self; 288 289 if (writer->tcs.metadata_fd != NULL) 290 fclose (writer->tcs.metadata_fd); 291 292 if (writer->tcs.datastream_fd != NULL) 293 fclose (writer->tcs.datastream_fd); 294 295 } 296 297 /* This is the implementation of trace_file_write_ops method 298 target_save. */ 299 300 static int 301 ctf_target_save (struct trace_file_writer *self, 302 const char *dirname) 303 { 304 /* Don't support save trace file to CTF format in the target. */ 305 return 0; 306 } 307 308 /* This is the implementation of trace_file_write_ops method 309 start. It creates the directory DIRNAME, metadata and datastream 310 in the directory. */ 311 312 static void 313 ctf_start (struct trace_file_writer *self, const char *dirname) 314 { 315 struct ctf_trace_file_writer *writer 316 = (struct ctf_trace_file_writer *) self; 317 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH; 318 319 /* Create DIRNAME. */ 320 if (mkdir (dirname, hmode) && errno != EEXIST) 321 error (_("Unable to open directory '%s' for saving trace data (%s)"), 322 dirname, safe_strerror (errno)); 323 324 memset (&writer->tcs, '\0', sizeof (writer->tcs)); 325 326 std::string file_name = string_printf ("%s/%s", dirname, CTF_METADATA_NAME); 327 328 writer->tcs.metadata_fd 329 = gdb_fopen_cloexec (file_name.c_str (), "w").release (); 330 if (writer->tcs.metadata_fd == NULL) 331 error (_("Unable to open file '%s' for saving trace data (%s)"), 332 file_name.c_str (), safe_strerror (errno)); 333 334 ctf_save_metadata_header (&writer->tcs); 335 336 file_name = string_printf ("%s/%s", dirname, CTF_DATASTREAM_NAME); 337 writer->tcs.datastream_fd 338 = gdb_fopen_cloexec (file_name.c_str (), "w").release (); 339 if (writer->tcs.datastream_fd == NULL) 340 error (_("Unable to open file '%s' for saving trace data (%s)"), 341 file_name.c_str (), safe_strerror (errno)); 342 } 343 344 /* This is the implementation of trace_file_write_ops method 345 write_header. Write the types of events on trace variable and 346 frame. */ 347 348 static void 349 ctf_write_header (struct trace_file_writer *self) 350 { 351 struct ctf_trace_file_writer *writer 352 = (struct ctf_trace_file_writer *) self; 353 354 355 ctf_save_write_metadata (&writer->tcs, "\n"); 356 ctf_save_write_metadata (&writer->tcs, 357 "event {\n\tname = \"memory\";\n\tid = %u;\n" 358 "\tfields := struct { \n" 359 "\t\tuint64_t address;\n" 360 "\t\tuint16_t length;\n" 361 "\t\tuint8_t contents[length];\n" 362 "\t};\n" 363 "};\n", CTF_EVENT_ID_MEMORY); 364 365 ctf_save_write_metadata (&writer->tcs, "\n"); 366 ctf_save_write_metadata (&writer->tcs, 367 "event {\n\tname = \"tsv\";\n\tid = %u;\n" 368 "\tfields := struct { \n" 369 "\t\tuint64_t val;\n" 370 "\t\tuint32_t num;\n" 371 "\t};\n" 372 "};\n", CTF_EVENT_ID_TSV); 373 374 ctf_save_write_metadata (&writer->tcs, "\n"); 375 ctf_save_write_metadata (&writer->tcs, 376 "event {\n\tname = \"frame\";\n\tid = %u;\n" 377 "\tfields := struct { \n" 378 "\t};\n" 379 "};\n", CTF_EVENT_ID_FRAME); 380 381 ctf_save_write_metadata (&writer->tcs, "\n"); 382 ctf_save_write_metadata (&writer->tcs, 383 "event {\n\tname = \"tsv_def\";\n" 384 "\tid = %u;\n\tfields := struct { \n" 385 "\t\tint64_t initial_value;\n" 386 "\t\tint32_t number;\n" 387 "\t\tint32_t builtin;\n" 388 "\t\tchars name;\n" 389 "\t};\n" 390 "};\n", CTF_EVENT_ID_TSV_DEF); 391 392 ctf_save_write_metadata (&writer->tcs, "\n"); 393 ctf_save_write_metadata (&writer->tcs, 394 "event {\n\tname = \"tp_def\";\n" 395 "\tid = %u;\n\tfields := struct { \n" 396 "\t\tuint64_t addr;\n" 397 "\t\tuint64_t traceframe_usage;\n" 398 "\t\tint32_t number;\n" 399 "\t\tint32_t enabled;\n" 400 "\t\tint32_t step;\n" 401 "\t\tint32_t pass;\n" 402 "\t\tint32_t hit_count;\n" 403 "\t\tint32_t type;\n" 404 "\t\tchars cond;\n" 405 406 "\t\tuint32_t action_num;\n" 407 "\t\tchars actions[action_num];\n" 408 409 "\t\tuint32_t step_action_num;\n" 410 "\t\tchars step_actions[step_action_num];\n" 411 412 "\t\tchars at_string;\n" 413 "\t\tchars cond_string;\n" 414 415 "\t\tuint32_t cmd_num;\n" 416 "\t\tchars cmd_strings[cmd_num];\n" 417 "\t};\n" 418 "};\n", CTF_EVENT_ID_TP_DEF); 419 420 gdb_assert (writer->tcs.content_size == 0); 421 gdb_assert (writer->tcs.packet_start == 0); 422 423 /* Create a new packet to contain this event. */ 424 self->ops->frame_ops->start (self, 0); 425 } 426 427 /* This is the implementation of trace_file_write_ops method 428 write_regblock_type. Write the type of register event in 429 metadata. */ 430 431 static void 432 ctf_write_regblock_type (struct trace_file_writer *self, int size) 433 { 434 struct ctf_trace_file_writer *writer 435 = (struct ctf_trace_file_writer *) self; 436 437 ctf_save_write_metadata (&writer->tcs, "\n"); 438 439 ctf_save_write_metadata (&writer->tcs, 440 "event {\n\tname = \"register\";\n\tid = %u;\n" 441 "\tfields := struct { \n" 442 "\t\tascii contents[%d];\n" 443 "\t};\n" 444 "};\n", 445 CTF_EVENT_ID_REGISTER, size); 446 } 447 448 /* This is the implementation of trace_file_write_ops method 449 write_status. */ 450 451 static void 452 ctf_write_status (struct trace_file_writer *self, 453 struct trace_status *ts) 454 { 455 struct ctf_trace_file_writer *writer 456 = (struct ctf_trace_file_writer *) self; 457 uint32_t id; 458 459 ctf_save_write_metadata (&writer->tcs, "\n"); 460 ctf_save_write_metadata (&writer->tcs, 461 "event {\n\tname = \"status\";\n\tid = %u;\n" 462 "\tfields := struct { \n" 463 "\t\tint32_t stop_reason;\n" 464 "\t\tint32_t stopping_tracepoint;\n" 465 "\t\tint32_t traceframe_count;\n" 466 "\t\tint32_t traceframes_created;\n" 467 "\t\tint32_t buffer_free;\n" 468 "\t\tint32_t buffer_size;\n" 469 "\t\tint32_t disconnected_tracing;\n" 470 "\t\tint32_t circular_buffer;\n" 471 "\t};\n" 472 "};\n", 473 CTF_EVENT_ID_STATUS); 474 475 id = CTF_EVENT_ID_STATUS; 476 /* Event Id. */ 477 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); 478 479 ctf_save_write_int32 (&writer->tcs, ts->stop_reason); 480 ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint); 481 ctf_save_write_int32 (&writer->tcs, ts->traceframe_count); 482 ctf_save_write_int32 (&writer->tcs, ts->traceframes_created); 483 ctf_save_write_int32 (&writer->tcs, ts->buffer_free); 484 ctf_save_write_int32 (&writer->tcs, ts->buffer_size); 485 ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing); 486 ctf_save_write_int32 (&writer->tcs, ts->circular_buffer); 487 } 488 489 /* This is the implementation of trace_file_write_ops method 490 write_uploaded_tsv. */ 491 492 static void 493 ctf_write_uploaded_tsv (struct trace_file_writer *self, 494 struct uploaded_tsv *tsv) 495 { 496 struct ctf_trace_file_writer *writer 497 = (struct ctf_trace_file_writer *) self; 498 int32_t int32; 499 int64_t int64; 500 const gdb_byte zero = 0; 501 502 /* Event Id. */ 503 int32 = CTF_EVENT_ID_TSV_DEF; 504 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4); 505 506 /* initial_value */ 507 int64 = tsv->initial_value; 508 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8); 509 510 /* number */ 511 ctf_save_write_int32 (&writer->tcs, tsv->number); 512 513 /* builtin */ 514 ctf_save_write_int32 (&writer->tcs, tsv->builtin); 515 516 /* name */ 517 if (tsv->name != NULL) 518 ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name, 519 strlen (tsv->name)); 520 ctf_save_write (&writer->tcs, &zero, 1); 521 } 522 523 /* This is the implementation of trace_file_write_ops method 524 write_uploaded_tp. */ 525 526 static void 527 ctf_write_uploaded_tp (struct trace_file_writer *self, 528 struct uploaded_tp *tp) 529 { 530 struct ctf_trace_file_writer *writer 531 = (struct ctf_trace_file_writer *) self; 532 int32_t int32; 533 int64_t int64; 534 uint32_t u32; 535 const gdb_byte zero = 0; 536 537 /* Event Id. */ 538 int32 = CTF_EVENT_ID_TP_DEF; 539 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4); 540 541 /* address */ 542 int64 = tp->addr; 543 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8); 544 545 /* traceframe_usage */ 546 int64 = tp->traceframe_usage; 547 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8); 548 549 /* number */ 550 ctf_save_write_int32 (&writer->tcs, tp->number); 551 552 /* enabled */ 553 ctf_save_write_int32 (&writer->tcs, tp->enabled); 554 555 /* step */ 556 ctf_save_write_int32 (&writer->tcs, tp->step); 557 558 /* pass */ 559 ctf_save_write_int32 (&writer->tcs, tp->pass); 560 561 /* hit_count */ 562 ctf_save_write_int32 (&writer->tcs, tp->hit_count); 563 564 /* type */ 565 ctf_save_write_int32 (&writer->tcs, tp->type); 566 567 /* condition */ 568 if (tp->cond != NULL) 569 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond.get (), 570 strlen (tp->cond.get ())); 571 ctf_save_write (&writer->tcs, &zero, 1); 572 573 /* actions */ 574 u32 = tp->actions.size (); 575 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4); 576 for (const auto &act : tp->actions) 577 ctf_save_write (&writer->tcs, (gdb_byte *) act.get (), 578 strlen (act.get ()) + 1); 579 580 /* step_actions */ 581 u32 = tp->step_actions.size (); 582 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4); 583 for (const auto &act : tp->step_actions) 584 ctf_save_write (&writer->tcs, (gdb_byte *) act.get (), 585 strlen (act.get ()) + 1); 586 587 /* at_string */ 588 if (tp->at_string != NULL) 589 ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string.get (), 590 strlen (tp->at_string.get ())); 591 ctf_save_write (&writer->tcs, &zero, 1); 592 593 /* cond_string */ 594 if (tp->cond_string != NULL) 595 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string.get (), 596 strlen (tp->cond_string.get ())); 597 ctf_save_write (&writer->tcs, &zero, 1); 598 599 /* cmd_strings */ 600 u32 = tp->cmd_strings.size (); 601 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4); 602 for (const auto &act : tp->cmd_strings) 603 ctf_save_write (&writer->tcs, (gdb_byte *) act.get (), 604 strlen (act.get ()) + 1); 605 606 } 607 608 /* This is the implementation of trace_file_write_ops method 609 write_tdesc. */ 610 611 static void 612 ctf_write_tdesc (struct trace_file_writer *self) 613 { 614 /* Nothing so far. */ 615 } 616 617 /* This is the implementation of trace_file_write_ops method 618 write_definition_end. */ 619 620 static void 621 ctf_write_definition_end (struct trace_file_writer *self) 622 { 623 self->ops->frame_ops->end (self); 624 } 625 626 /* This is the implementation of trace_file_write_ops method 627 end. */ 628 629 static void 630 ctf_end (struct trace_file_writer *self) 631 { 632 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self; 633 634 gdb_assert (writer->tcs.content_size == 0); 635 } 636 637 /* This is the implementation of trace_frame_write_ops method 638 start. */ 639 640 static void 641 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum) 642 { 643 struct ctf_trace_file_writer *writer 644 = (struct ctf_trace_file_writer *) self; 645 uint32_t id = CTF_EVENT_ID_FRAME; 646 uint32_t u32; 647 648 /* Step 1: Write packet context. */ 649 /* magic. */ 650 u32 = CTF_MAGIC; 651 ctf_save_write_uint32 (&writer->tcs, u32); 652 /* content_size and packet_size.. We still don't know the value, 653 write it later. */ 654 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR); 655 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR); 656 /* Tracepoint number. */ 657 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2); 658 659 /* Step 2: Write event "frame". */ 660 /* Event Id. */ 661 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); 662 } 663 664 /* This is the implementation of trace_frame_write_ops method 665 write_r_block. */ 666 667 static void 668 ctf_write_frame_r_block (struct trace_file_writer *self, 669 gdb_byte *buf, int32_t size) 670 { 671 struct ctf_trace_file_writer *writer 672 = (struct ctf_trace_file_writer *) self; 673 uint32_t id = CTF_EVENT_ID_REGISTER; 674 675 /* Event Id. */ 676 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); 677 678 /* array contents. */ 679 ctf_save_align_write (&writer->tcs, buf, size, 1); 680 } 681 682 /* This is the implementation of trace_frame_write_ops method 683 write_m_block_header. */ 684 685 static void 686 ctf_write_frame_m_block_header (struct trace_file_writer *self, 687 uint64_t addr, uint16_t length) 688 { 689 struct ctf_trace_file_writer *writer 690 = (struct ctf_trace_file_writer *) self; 691 uint32_t event_id = CTF_EVENT_ID_MEMORY; 692 693 /* Event Id. */ 694 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4); 695 696 /* Address. */ 697 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8); 698 699 /* Length. */ 700 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2); 701 } 702 703 /* This is the implementation of trace_frame_write_ops method 704 write_m_block_memory. */ 705 706 static void 707 ctf_write_frame_m_block_memory (struct trace_file_writer *self, 708 gdb_byte *buf, uint16_t length) 709 { 710 struct ctf_trace_file_writer *writer 711 = (struct ctf_trace_file_writer *) self; 712 713 /* Contents. */ 714 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1); 715 } 716 717 /* This is the implementation of trace_frame_write_ops method 718 write_v_block. */ 719 720 static void 721 ctf_write_frame_v_block (struct trace_file_writer *self, 722 int32_t num, uint64_t val) 723 { 724 struct ctf_trace_file_writer *writer 725 = (struct ctf_trace_file_writer *) self; 726 uint32_t id = CTF_EVENT_ID_TSV; 727 728 /* Event Id. */ 729 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); 730 731 /* val. */ 732 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8); 733 /* num. */ 734 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4); 735 } 736 737 /* This is the implementation of trace_frame_write_ops method 738 end. */ 739 740 static void 741 ctf_write_frame_end (struct trace_file_writer *self) 742 { 743 struct ctf_trace_file_writer *writer 744 = (struct ctf_trace_file_writer *) self; 745 uint32_t u32; 746 uint32_t t; 747 748 /* Write the content size to packet header. */ 749 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4, 750 SEEK_SET); 751 u32 = writer->tcs.content_size * TARGET_CHAR_BIT; 752 753 t = writer->tcs.content_size; 754 ctf_save_write_uint32 (&writer->tcs, u32); 755 756 /* Write the packet size. */ 757 u32 += 4 * TARGET_CHAR_BIT; 758 ctf_save_write_uint32 (&writer->tcs, u32); 759 760 writer->tcs.content_size = t; 761 762 /* Write zero at the end of the packet. */ 763 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t, 764 SEEK_SET); 765 u32 = 0; 766 ctf_save_write_uint32 (&writer->tcs, u32); 767 writer->tcs.content_size = t; 768 769 ctf_save_next_packet (&writer->tcs); 770 } 771 772 /* Operations to write various types of trace frames into CTF 773 format. */ 774 775 static const struct trace_frame_write_ops ctf_write_frame_ops = 776 { 777 ctf_write_frame_start, 778 ctf_write_frame_r_block, 779 ctf_write_frame_m_block_header, 780 ctf_write_frame_m_block_memory, 781 ctf_write_frame_v_block, 782 ctf_write_frame_end, 783 }; 784 785 /* Operations to write trace buffers into CTF format. */ 786 787 static const struct trace_file_write_ops ctf_write_ops = 788 { 789 ctf_dtor, 790 ctf_target_save, 791 ctf_start, 792 ctf_write_header, 793 ctf_write_regblock_type, 794 ctf_write_status, 795 ctf_write_uploaded_tsv, 796 ctf_write_uploaded_tp, 797 ctf_write_tdesc, 798 ctf_write_definition_end, 799 NULL, 800 &ctf_write_frame_ops, 801 ctf_end, 802 }; 803 804 /* Return a trace writer for CTF format. */ 805 806 struct trace_file_writer * 807 ctf_trace_file_writer_new (void) 808 { 809 struct ctf_trace_file_writer *writer = XNEW (struct ctf_trace_file_writer); 810 811 writer->base.ops = &ctf_write_ops; 812 813 return (struct trace_file_writer *) writer; 814 } 815 816 #if HAVE_LIBBABELTRACE 817 /* Use libbabeltrace to read CTF data. The libbabeltrace provides 818 iterator to iterate over each event in CTF data and APIs to get 819 details of event and packet, so it is very convenient to use 820 libbabeltrace to access events in CTF. */ 821 822 #include <babeltrace/babeltrace.h> 823 #include <babeltrace/ctf/events.h> 824 #include <babeltrace/ctf/iterator.h> 825 826 /* The CTF target. */ 827 828 static const target_info ctf_target_info = { 829 "ctf", 830 N_("CTF file"), 831 N_("(Use a CTF directory as a target.\n\ 832 Specify the filename of the CTF directory.") 833 }; 834 835 class ctf_target final : public tracefile_target 836 { 837 public: 838 const target_info &info () const override 839 { return ctf_target_info; } 840 841 void close () override; 842 void fetch_registers (struct regcache *, int) override; 843 enum target_xfer_status xfer_partial (enum target_object object, 844 const char *annex, 845 gdb_byte *readbuf, 846 const gdb_byte *writebuf, 847 ULONGEST offset, ULONGEST len, 848 ULONGEST *xfered_len) override; 849 void files_info () override; 850 int trace_find (enum trace_find_type type, int num, 851 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override; 852 bool get_trace_state_variable_value (int tsv, LONGEST *val) override; 853 traceframe_info_up traceframe_info () override; 854 }; 855 856 /* The struct pointer for current CTF directory. */ 857 static struct bt_context *ctx = NULL; 858 static struct bt_ctf_iter *ctf_iter = NULL; 859 /* The position of the first packet containing trace frame. */ 860 static struct bt_iter_pos *start_pos; 861 862 /* The name of CTF directory. */ 863 static gdb::unique_xmalloc_ptr<char> trace_dirname; 864 865 static ctf_target ctf_ops; 866 867 /* Destroy ctf iterator and context. */ 868 869 static void 870 ctf_destroy (void) 871 { 872 if (ctf_iter != NULL) 873 { 874 bt_ctf_iter_destroy (ctf_iter); 875 ctf_iter = NULL; 876 } 877 if (ctx != NULL) 878 { 879 bt_context_put (ctx); 880 ctx = NULL; 881 } 882 } 883 884 /* Open CTF trace data in DIRNAME. */ 885 886 static void 887 ctf_open_dir (const char *dirname) 888 { 889 struct bt_iter_pos begin_pos; 890 unsigned int count, i; 891 struct bt_ctf_event_decl * const *list; 892 893 ctx = bt_context_create (); 894 if (ctx == NULL) 895 error (_("Unable to create bt_context")); 896 int handle_id = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL); 897 if (handle_id < 0) 898 { 899 ctf_destroy (); 900 error (_("Unable to use libbabeltrace on directory \"%s\""), 901 dirname); 902 } 903 904 begin_pos.type = BT_SEEK_BEGIN; 905 ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL); 906 if (ctf_iter == NULL) 907 { 908 ctf_destroy (); 909 error (_("Unable to create bt_iterator")); 910 } 911 912 /* Look for the declaration of register block. Get the length of 913 array "contents" to set trace_regblock_size. */ 914 915 bt_ctf_get_event_decl_list (handle_id, ctx, &list, &count); 916 for (i = 0; i < count; i++) 917 if (strcmp ("register", bt_ctf_get_decl_event_name (list[i])) == 0) 918 { 919 const struct bt_ctf_field_decl * const *field_list; 920 const struct bt_declaration *decl; 921 922 bt_ctf_get_decl_fields (list[i], BT_EVENT_FIELDS, &field_list, 923 &count); 924 925 gdb_assert (count == 1); 926 gdb_assert (0 == strcmp ("contents", 927 bt_ctf_get_decl_field_name (field_list[0]))); 928 decl = bt_ctf_get_decl_from_field_decl (field_list[0]); 929 trace_regblock_size = bt_ctf_get_array_len (decl); 930 931 break; 932 } 933 } 934 935 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \ 936 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \ 937 (SCOPE), \ 938 #FIELD)) 939 940 #define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \ 941 (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \ 942 (SCOPE), \ 943 #FIELD)) 944 945 946 /* EVENT is the "status" event and TS is filled in. */ 947 948 static void 949 ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts) 950 { 951 const struct bt_definition *scope 952 = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS); 953 954 SET_ENUM_FIELD (event, scope, ts, enum trace_stop_reason, stop_reason); 955 SET_INT32_FIELD (event, scope, ts, stopping_tracepoint); 956 SET_INT32_FIELD (event, scope, ts, traceframe_count); 957 SET_INT32_FIELD (event, scope, ts, traceframes_created); 958 SET_INT32_FIELD (event, scope, ts, buffer_free); 959 SET_INT32_FIELD (event, scope, ts, buffer_size); 960 SET_INT32_FIELD (event, scope, ts, disconnected_tracing); 961 SET_INT32_FIELD (event, scope, ts, circular_buffer); 962 963 bt_iter_next (bt_ctf_get_iter (ctf_iter)); 964 } 965 966 /* Read the events "tsv_def" one by one, extract its contents and fill 967 in the list UPLOADED_TSVS. */ 968 969 static void 970 ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs) 971 { 972 gdb_assert (ctf_iter != NULL); 973 974 while (1) 975 { 976 struct bt_ctf_event *event; 977 const struct bt_definition *scope; 978 const struct bt_definition *def; 979 uint32_t event_id; 980 struct uploaded_tsv *utsv = NULL; 981 982 event = bt_ctf_iter_read_event (ctf_iter); 983 scope = bt_ctf_get_top_level_scope (event, 984 BT_STREAM_EVENT_HEADER); 985 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, 986 "id")); 987 if (event_id != CTF_EVENT_ID_TSV_DEF) 988 break; 989 990 scope = bt_ctf_get_top_level_scope (event, 991 BT_EVENT_FIELDS); 992 993 def = bt_ctf_get_field (event, scope, "number"); 994 utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def), 995 uploaded_tsvs); 996 997 def = bt_ctf_get_field (event, scope, "builtin"); 998 utsv->builtin = (int32_t) bt_ctf_get_int64 (def); 999 def = bt_ctf_get_field (event, scope, "initial_value"); 1000 utsv->initial_value = bt_ctf_get_int64 (def); 1001 1002 def = bt_ctf_get_field (event, scope, "name"); 1003 utsv->name = xstrdup (bt_ctf_get_string (def)); 1004 1005 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1006 break; 1007 } 1008 1009 } 1010 1011 /* Read the value of element whose index is NUM from CTF and write it 1012 to the corresponding VAR->ARRAY. */ 1013 1014 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \ 1015 do \ 1016 { \ 1017 uint32_t lu32, i; \ 1018 const struct bt_definition *def; \ 1019 \ 1020 lu32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \ 1021 (SCOPE), \ 1022 #NUM)); \ 1023 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \ 1024 for (i = 0; i < lu32; i++) \ 1025 { \ 1026 const struct bt_definition *element \ 1027 = bt_ctf_get_index ((EVENT), def, i); \ 1028 \ 1029 (VAR)->ARRAY.emplace_back \ 1030 (xstrdup (bt_ctf_get_string (element))); \ 1031 } \ 1032 } \ 1033 while (0) 1034 1035 /* Read a string from CTF and set VAR->FIELD. If the length of string 1036 is zero, set VAR->FIELD to NULL. */ 1037 1038 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \ 1039 do \ 1040 { \ 1041 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \ 1042 (SCOPE), \ 1043 #FIELD)); \ 1044 \ 1045 if (strlen (p) > 0) \ 1046 (VAR)->FIELD.reset (xstrdup (p)); \ 1047 else \ 1048 (VAR)->FIELD = NULL; \ 1049 } \ 1050 while (0) 1051 1052 /* Read the events "tp_def" one by one, extract its contents and fill 1053 in the list UPLOADED_TPS. */ 1054 1055 static void 1056 ctf_read_tp (struct uploaded_tp **uploaded_tps) 1057 { 1058 gdb_assert (ctf_iter != NULL); 1059 1060 while (1) 1061 { 1062 struct bt_ctf_event *event; 1063 const struct bt_definition *scope; 1064 uint32_t u32; 1065 int32_t int32; 1066 uint64_t u64; 1067 struct uploaded_tp *utp = NULL; 1068 1069 event = bt_ctf_iter_read_event (ctf_iter); 1070 scope = bt_ctf_get_top_level_scope (event, 1071 BT_STREAM_EVENT_HEADER); 1072 u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, 1073 "id")); 1074 if (u32 != CTF_EVENT_ID_TP_DEF) 1075 break; 1076 1077 scope = bt_ctf_get_top_level_scope (event, 1078 BT_EVENT_FIELDS); 1079 int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event, 1080 scope, 1081 "number")); 1082 u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, 1083 "addr")); 1084 utp = get_uploaded_tp (int32, u64, uploaded_tps); 1085 1086 SET_INT32_FIELD (event, scope, utp, enabled); 1087 SET_INT32_FIELD (event, scope, utp, step); 1088 SET_INT32_FIELD (event, scope, utp, pass); 1089 SET_INT32_FIELD (event, scope, utp, hit_count); 1090 SET_ENUM_FIELD (event, scope, utp, enum bptype, type); 1091 1092 /* Read 'cmd_strings'. */ 1093 SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings); 1094 /* Read 'actions'. */ 1095 SET_ARRAY_FIELD (event, scope, utp, action_num, actions); 1096 /* Read 'step_actions'. */ 1097 SET_ARRAY_FIELD (event, scope, utp, step_action_num, 1098 step_actions); 1099 1100 SET_STRING_FIELD(event, scope, utp, at_string); 1101 SET_STRING_FIELD(event, scope, utp, cond_string); 1102 1103 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1104 break; 1105 } 1106 } 1107 1108 /* This is the implementation of target_ops method to_open. Open CTF 1109 trace data, read trace status, trace state variables and tracepoint 1110 definitions from the first packet. Set the start position at the 1111 second packet which contains events on trace blocks. */ 1112 1113 static void 1114 ctf_target_open (const char *dirname, int from_tty) 1115 { 1116 struct bt_ctf_event *event; 1117 uint32_t event_id; 1118 const struct bt_definition *scope; 1119 struct uploaded_tsv *uploaded_tsvs = NULL; 1120 struct uploaded_tp *uploaded_tps = NULL; 1121 1122 if (!dirname) 1123 error (_("No CTF directory specified.")); 1124 1125 ctf_open_dir (dirname); 1126 1127 target_preopen (from_tty); 1128 1129 /* Skip the first packet which about the trace status. The first 1130 event is "frame". */ 1131 event = bt_ctf_iter_read_event (ctf_iter); 1132 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER); 1133 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id")); 1134 if (event_id != CTF_EVENT_ID_FRAME) 1135 error (_("Wrong event id of the first event")); 1136 /* The second event is "status". */ 1137 bt_iter_next (bt_ctf_get_iter (ctf_iter)); 1138 event = bt_ctf_iter_read_event (ctf_iter); 1139 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER); 1140 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id")); 1141 if (event_id != CTF_EVENT_ID_STATUS) 1142 error (_("Wrong event id of the second event")); 1143 ctf_read_status (event, current_trace_status ()); 1144 1145 ctf_read_tsv (&uploaded_tsvs); 1146 1147 ctf_read_tp (&uploaded_tps); 1148 1149 event = bt_ctf_iter_read_event (ctf_iter); 1150 /* EVENT can be NULL if we've already gone to the end of stream of 1151 events. */ 1152 if (event != NULL) 1153 { 1154 scope = bt_ctf_get_top_level_scope (event, 1155 BT_STREAM_EVENT_HEADER); 1156 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, 1157 scope, "id")); 1158 if (event_id != CTF_EVENT_ID_FRAME) 1159 error (_("Wrong event id of the first event of the second packet")); 1160 } 1161 1162 start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); 1163 gdb_assert (start_pos->type == BT_SEEK_RESTORE); 1164 1165 trace_dirname = make_unique_xstrdup (dirname); 1166 current_inferior ()->push_target (&ctf_ops); 1167 1168 inferior_appeared (current_inferior (), CTF_PID); 1169 1170 thread_info *thr = add_thread_silent (&ctf_ops, ptid_t (CTF_PID)); 1171 switch_to_thread (thr); 1172 1173 merge_uploaded_trace_state_variables (&uploaded_tsvs); 1174 merge_uploaded_tracepoints (&uploaded_tps); 1175 1176 post_create_inferior (from_tty); 1177 } 1178 1179 /* This is the implementation of target_ops method to_close. Destroy 1180 CTF iterator and context. */ 1181 1182 void 1183 ctf_target::close () 1184 { 1185 ctf_destroy (); 1186 trace_dirname.reset (); 1187 1188 switch_to_no_thread (); /* Avoid confusion from thread stuff. */ 1189 exit_inferior (current_inferior ()); 1190 1191 trace_reset_local_state (); 1192 } 1193 1194 /* This is the implementation of target_ops method to_files_info. 1195 Print the directory name of CTF trace data. */ 1196 1197 void 1198 ctf_target::files_info () 1199 { 1200 gdb_printf ("\t`%s'\n", trace_dirname.get ()); 1201 } 1202 1203 /* This is the implementation of target_ops method to_fetch_registers. 1204 Iterate over events whose name is "register" in current frame, 1205 extract contents from events, and set REGCACHE with the contents. 1206 If no matched events are found, mark registers unavailable. */ 1207 1208 void 1209 ctf_target::fetch_registers (struct regcache *regcache, int regno) 1210 { 1211 struct gdbarch *gdbarch = regcache->arch (); 1212 struct bt_ctf_event *event = NULL; 1213 struct bt_iter_pos *pos; 1214 1215 /* An uninitialized reg size says we're not going to be 1216 successful at getting register blocks. */ 1217 if (trace_regblock_size == 0) 1218 return; 1219 1220 gdb_assert (ctf_iter != NULL); 1221 /* Save the current position. */ 1222 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); 1223 gdb_assert (pos->type == BT_SEEK_RESTORE); 1224 1225 while (1) 1226 { 1227 const char *name; 1228 struct bt_ctf_event *event1; 1229 1230 event1 = bt_ctf_iter_read_event (ctf_iter); 1231 1232 name = bt_ctf_event_name (event1); 1233 1234 if (name == NULL || strcmp (name, "frame") == 0) 1235 break; 1236 else if (strcmp (name, "register") == 0) 1237 { 1238 event = event1; 1239 break; 1240 } 1241 1242 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1243 break; 1244 } 1245 1246 /* Restore the position. */ 1247 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); 1248 1249 if (event != NULL) 1250 { 1251 int offset, regsize, regn; 1252 const struct bt_definition *scope 1253 = bt_ctf_get_top_level_scope (event, 1254 BT_EVENT_FIELDS); 1255 const struct bt_definition *array 1256 = bt_ctf_get_field (event, scope, "contents"); 1257 gdb_byte *regs = (gdb_byte *) bt_ctf_get_char_array (array); 1258 1259 /* Assume the block is laid out in GDB register number order, 1260 each register with the size that it has in GDB. */ 1261 offset = 0; 1262 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++) 1263 { 1264 regsize = register_size (gdbarch, regn); 1265 /* Make sure we stay within block bounds. */ 1266 if (offset + regsize >= trace_regblock_size) 1267 break; 1268 if (regcache->get_register_status (regn) == REG_UNKNOWN) 1269 { 1270 if (regno == regn) 1271 { 1272 regcache->raw_supply (regno, regs + offset); 1273 break; 1274 } 1275 else if (regno == -1) 1276 { 1277 regcache->raw_supply (regn, regs + offset); 1278 } 1279 } 1280 offset += regsize; 1281 } 1282 } 1283 else 1284 tracefile_fetch_registers (regcache, regno); 1285 } 1286 1287 /* This is the implementation of target_ops method to_xfer_partial. 1288 Iterate over events whose name is "memory" in 1289 current frame, extract the address and length from events. If 1290 OFFSET is within the range, read the contents from events to 1291 READBUF. */ 1292 1293 enum target_xfer_status 1294 ctf_target::xfer_partial (enum target_object object, 1295 const char *annex, gdb_byte *readbuf, 1296 const gdb_byte *writebuf, ULONGEST offset, 1297 ULONGEST len, ULONGEST *xfered_len) 1298 { 1299 /* We're only doing regular memory for now. */ 1300 if (object != TARGET_OBJECT_MEMORY) 1301 return TARGET_XFER_E_IO; 1302 1303 if (readbuf == NULL) 1304 error (_("ctf_xfer_partial: trace file is read-only")); 1305 1306 if (get_traceframe_number () != -1) 1307 { 1308 struct bt_iter_pos *pos; 1309 enum target_xfer_status res; 1310 /* Records the lowest available address of all blocks that 1311 intersects the requested range. */ 1312 ULONGEST low_addr_available = 0; 1313 1314 gdb_assert (ctf_iter != NULL); 1315 /* Save the current position. */ 1316 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); 1317 gdb_assert (pos->type == BT_SEEK_RESTORE); 1318 1319 /* Iterate through the traceframe's blocks, looking for 1320 memory. */ 1321 while (1) 1322 { 1323 ULONGEST amt; 1324 uint64_t maddr; 1325 uint16_t mlen; 1326 const struct bt_definition *scope; 1327 const struct bt_definition *def; 1328 struct bt_ctf_event *event 1329 = bt_ctf_iter_read_event (ctf_iter); 1330 const char *name = bt_ctf_event_name (event); 1331 1332 if (name == NULL || strcmp (name, "frame") == 0) 1333 break; 1334 else if (strcmp (name, "memory") != 0) 1335 { 1336 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1337 break; 1338 1339 continue; 1340 } 1341 1342 scope = bt_ctf_get_top_level_scope (event, 1343 BT_EVENT_FIELDS); 1344 1345 def = bt_ctf_get_field (event, scope, "address"); 1346 maddr = bt_ctf_get_uint64 (def); 1347 def = bt_ctf_get_field (event, scope, "length"); 1348 mlen = (uint16_t) bt_ctf_get_uint64 (def); 1349 1350 /* If the block includes the first part of the desired 1351 range, return as much it has; GDB will re-request the 1352 remainder, which might be in a different block of this 1353 trace frame. */ 1354 if (maddr <= offset && offset < (maddr + mlen)) 1355 { 1356 const struct bt_definition *array 1357 = bt_ctf_get_field (event, scope, "contents"); 1358 int k; 1359 1360 gdb::byte_vector contents (mlen); 1361 1362 for (k = 0; k < mlen; k++) 1363 { 1364 const struct bt_definition *element 1365 = bt_ctf_get_index (event, array, k); 1366 1367 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element); 1368 } 1369 1370 amt = (maddr + mlen) - offset; 1371 if (amt > len) 1372 amt = len; 1373 1374 memcpy (readbuf, &contents[offset - maddr], amt); 1375 1376 /* Restore the position. */ 1377 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); 1378 1379 if (amt == 0) 1380 return TARGET_XFER_EOF; 1381 else 1382 { 1383 *xfered_len = amt; 1384 return TARGET_XFER_OK; 1385 } 1386 } 1387 1388 if (offset < maddr && maddr < (offset + len)) 1389 if (low_addr_available == 0 || low_addr_available > maddr) 1390 low_addr_available = maddr; 1391 1392 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1393 break; 1394 } 1395 1396 /* Restore the position. */ 1397 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); 1398 1399 /* Requested memory is unavailable in the context of traceframes, 1400 and this address falls within a read-only section, fallback 1401 to reading from executable, up to LOW_ADDR_AVAILABLE */ 1402 if (offset < low_addr_available) 1403 len = std::min (len, low_addr_available - offset); 1404 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len); 1405 1406 if (res == TARGET_XFER_OK) 1407 return TARGET_XFER_OK; 1408 else 1409 { 1410 /* No use trying further, we know some memory starting 1411 at MEMADDR isn't available. */ 1412 *xfered_len = len; 1413 return TARGET_XFER_UNAVAILABLE; 1414 } 1415 } 1416 else 1417 { 1418 /* Fallback to reading from read-only sections. */ 1419 return section_table_read_available_memory (readbuf, offset, len, xfered_len); 1420 } 1421 } 1422 1423 /* This is the implementation of target_ops method 1424 to_get_trace_state_variable_value. 1425 Iterate over events whose name is "tsv" in current frame. When the 1426 trace variable is found, set the value of it to *VAL and return 1427 true, otherwise return false. */ 1428 1429 bool 1430 ctf_target::get_trace_state_variable_value (int tsvnum, LONGEST *val) 1431 { 1432 struct bt_iter_pos *pos; 1433 bool found = false; 1434 1435 gdb_assert (ctf_iter != NULL); 1436 /* Save the current position. */ 1437 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); 1438 gdb_assert (pos->type == BT_SEEK_RESTORE); 1439 1440 /* Iterate through the traceframe's blocks, looking for 'V' 1441 block. */ 1442 while (1) 1443 { 1444 struct bt_ctf_event *event 1445 = bt_ctf_iter_read_event (ctf_iter); 1446 const char *name = bt_ctf_event_name (event); 1447 1448 if (name == NULL || strcmp (name, "frame") == 0) 1449 break; 1450 else if (strcmp (name, "tsv") == 0) 1451 { 1452 const struct bt_definition *scope; 1453 const struct bt_definition *def; 1454 1455 scope = bt_ctf_get_top_level_scope (event, 1456 BT_EVENT_FIELDS); 1457 1458 def = bt_ctf_get_field (event, scope, "num"); 1459 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def)) 1460 { 1461 def = bt_ctf_get_field (event, scope, "val"); 1462 *val = bt_ctf_get_uint64 (def); 1463 1464 found = true; 1465 } 1466 } 1467 1468 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1469 break; 1470 } 1471 1472 /* Restore the position. */ 1473 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); 1474 1475 return found; 1476 } 1477 1478 /* Return the tracepoint number in "frame" event. */ 1479 1480 static int 1481 ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event) 1482 { 1483 /* The packet context of events has a field "tpnum". */ 1484 const struct bt_definition *scope 1485 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT); 1486 uint64_t tpnum 1487 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum")); 1488 1489 return (int) tpnum; 1490 } 1491 1492 /* Return the address at which the current frame was collected. */ 1493 1494 static CORE_ADDR 1495 ctf_get_traceframe_address (void) 1496 { 1497 struct bt_ctf_event *event = NULL; 1498 struct bt_iter_pos *pos; 1499 CORE_ADDR addr = 0; 1500 1501 gdb_assert (ctf_iter != NULL); 1502 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); 1503 gdb_assert (pos->type == BT_SEEK_RESTORE); 1504 1505 while (1) 1506 { 1507 const char *name; 1508 struct bt_ctf_event *event1; 1509 1510 event1 = bt_ctf_iter_read_event (ctf_iter); 1511 1512 name = bt_ctf_event_name (event1); 1513 1514 if (name == NULL) 1515 break; 1516 else if (strcmp (name, "frame") == 0) 1517 { 1518 event = event1; 1519 break; 1520 } 1521 1522 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1523 break; 1524 } 1525 1526 if (event != NULL) 1527 { 1528 int tpnum = ctf_get_tpnum_from_frame_event (event); 1529 struct tracepoint *tp 1530 = get_tracepoint_by_number_on_target (tpnum); 1531 1532 if (tp != nullptr && tp->has_locations ()) 1533 addr = tp->first_loc ().address; 1534 } 1535 1536 /* Restore the position. */ 1537 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); 1538 1539 return addr; 1540 } 1541 1542 /* This is the implementation of target_ops method to_trace_find. 1543 Iterate the events whose name is "frame", extract the tracepoint 1544 number in it. Return traceframe number when matched. */ 1545 1546 int 1547 ctf_target::trace_find (enum trace_find_type type, int num, 1548 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) 1549 { 1550 int tfnum = 0; 1551 int found = 0; 1552 1553 if (num == -1) 1554 { 1555 if (tpp != NULL) 1556 *tpp = -1; 1557 return -1; 1558 } 1559 1560 gdb_assert (ctf_iter != NULL); 1561 /* Set iterator back to the start. */ 1562 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos); 1563 1564 while (1) 1565 { 1566 struct bt_ctf_event *event; 1567 const char *name; 1568 1569 event = bt_ctf_iter_read_event (ctf_iter); 1570 1571 name = bt_ctf_event_name (event); 1572 1573 if (event == NULL || name == NULL) 1574 break; 1575 1576 if (strcmp (name, "frame") == 0) 1577 { 1578 CORE_ADDR tfaddr; 1579 1580 if (type == tfind_number) 1581 { 1582 /* Looking for a specific trace frame. */ 1583 if (tfnum == num) 1584 found = 1; 1585 } 1586 else 1587 { 1588 /* Start from the _next_ trace frame. */ 1589 if (tfnum > get_traceframe_number ()) 1590 { 1591 switch (type) 1592 { 1593 case tfind_tp: 1594 { 1595 struct tracepoint *tp = get_tracepoint (num); 1596 1597 if (tp != NULL 1598 && (tp->number_on_target 1599 == ctf_get_tpnum_from_frame_event (event))) 1600 found = 1; 1601 break; 1602 } 1603 case tfind_pc: 1604 tfaddr = ctf_get_traceframe_address (); 1605 if (tfaddr == addr1) 1606 found = 1; 1607 break; 1608 case tfind_range: 1609 tfaddr = ctf_get_traceframe_address (); 1610 if (addr1 <= tfaddr && tfaddr <= addr2) 1611 found = 1; 1612 break; 1613 case tfind_outside: 1614 tfaddr = ctf_get_traceframe_address (); 1615 if (!(addr1 <= tfaddr && tfaddr <= addr2)) 1616 found = 1; 1617 break; 1618 default: 1619 internal_error (_("unknown tfind type")); 1620 } 1621 } 1622 } 1623 if (found) 1624 { 1625 if (tpp != NULL) 1626 *tpp = ctf_get_tpnum_from_frame_event (event); 1627 1628 /* Skip the event "frame". */ 1629 bt_iter_next (bt_ctf_get_iter (ctf_iter)); 1630 1631 return tfnum; 1632 } 1633 tfnum++; 1634 } 1635 1636 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1637 break; 1638 } 1639 1640 return -1; 1641 } 1642 1643 /* This is the implementation of target_ops method to_traceframe_info. 1644 Iterate the events whose name is "memory", in current 1645 frame, extract memory range information, and return them in 1646 traceframe_info. */ 1647 1648 traceframe_info_up 1649 ctf_target::traceframe_info () 1650 { 1651 traceframe_info_up info (new struct traceframe_info); 1652 const char *name; 1653 struct bt_iter_pos *pos; 1654 1655 gdb_assert (ctf_iter != NULL); 1656 /* Save the current position. */ 1657 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); 1658 gdb_assert (pos->type == BT_SEEK_RESTORE); 1659 1660 do 1661 { 1662 struct bt_ctf_event *event 1663 = bt_ctf_iter_read_event (ctf_iter); 1664 1665 name = bt_ctf_event_name (event); 1666 1667 if (name == NULL || strcmp (name, "register") == 0 1668 || strcmp (name, "frame") == 0) 1669 ; 1670 else if (strcmp (name, "memory") == 0) 1671 { 1672 const struct bt_definition *scope 1673 = bt_ctf_get_top_level_scope (event, 1674 BT_EVENT_FIELDS); 1675 const struct bt_definition *def; 1676 1677 def = bt_ctf_get_field (event, scope, "address"); 1678 CORE_ADDR start = bt_ctf_get_uint64 (def); 1679 1680 def = bt_ctf_get_field (event, scope, "length"); 1681 int length = (uint16_t) bt_ctf_get_uint64 (def); 1682 1683 info->memory.emplace_back (start, length); 1684 } 1685 else if (strcmp (name, "tsv") == 0) 1686 { 1687 int vnum; 1688 const struct bt_definition *scope 1689 = bt_ctf_get_top_level_scope (event, 1690 BT_EVENT_FIELDS); 1691 const struct bt_definition *def; 1692 1693 def = bt_ctf_get_field (event, scope, "num"); 1694 vnum = (int) bt_ctf_get_uint64 (def); 1695 info->tvars.push_back (vnum); 1696 } 1697 else 1698 { 1699 warning (_("Unhandled trace block type (%s) " 1700 "while building trace frame info."), 1701 name); 1702 } 1703 1704 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) 1705 break; 1706 } 1707 while (name != NULL && strcmp (name, "frame") != 0); 1708 1709 /* Restore the position. */ 1710 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); 1711 1712 return info; 1713 } 1714 1715 #endif 1716 1717 /* module initialization */ 1718 1719 void _initialize_ctf (); 1720 void 1721 _initialize_ctf () 1722 { 1723 #if HAVE_LIBBABELTRACE 1724 add_target (ctf_target_info, ctf_target_open, filename_completer); 1725 #endif 1726 } 1727