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