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