3 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 Contributed by Hui Zhu <hui_zhu@mentor.com>
5 Contributed by Yao Qi <yao@codesourcery.com>
7 This file is part of GDB.
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.
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.
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/>. */
24 #include "tracepoint.h"
28 #include "completer.h"
30 #include "gdbthread.h"
31 #include "tracefile.h"
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:
39 1. The length (in bytes) of register cache. Event "register" will
40 be defined in metadata, which includes the length.
42 2. Trace status. Event "status" is defined in metadata, which
43 includes all aspects of trace status.
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".
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
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
65 #define CTF_MAGIC 0xC1FC1FC1
66 #define CTF_SAVE_MAJOR 1
67 #define CTF_SAVE_MINOR 8
69 #define CTF_METADATA_NAME "metadata"
70 #define CTF_DATASTREAM_NAME "datastream"
72 /* Reserved event id. */
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
84 /* The state kept while writing the CTF datastream file. */
86 struct trace_write_handler
88 /* File descriptor of metadata. */
90 /* File descriptor of traceframes. */
93 /* This is the content size of the current packet. */
96 /* This is the start offset of current packet. */
100 /* Write metadata in FORMAT. */
103 ctf_save_write_metadata (struct trace_write_handler
*handler
,
104 const char *format
, ...)
105 ATTRIBUTE_PRINTF (2, 3);
108 ctf_save_write_metadata (struct trace_write_handler
*handler
,
109 const char *format
, ...)
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
));
120 /* Write BUF of length SIZE to datastream file represented by
124 ctf_save_write (struct trace_write_handler
*handler
,
125 const gdb_byte
*buf
, size_t size
)
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
));
131 handler
->content_size
+= size
;
136 /* Write a unsigned 32-bit integer to datastream file represented by
139 #define ctf_save_write_uint32(HANDLER, U32) \
140 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
142 /* Write a signed 32-bit integer to datastream file represented by
145 #define ctf_save_write_int32(HANDLER, INT32) \
146 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
148 /* Set datastream file position. Update HANDLER->content_size
149 if WHENCE is SEEK_CUR. */
152 ctf_save_fseek (struct trace_write_handler
*handler
, long offset
,
155 gdb_assert (whence
!= SEEK_END
);
156 gdb_assert (whence
!= SEEK_SET
157 || offset
<= handler
->content_size
+ handler
->packet_start
);
159 if (fseek (handler
->datastream_fd
, offset
, whence
))
160 error (_("Unable to seek file for saving trace data (%s)"),
161 safe_strerror (errno
));
163 if (whence
== SEEK_CUR
)
164 handler
->content_size
+= offset
;
169 /* Change the datastream file position to align on ALIGN_SIZE,
170 and write BUF to datastream file. The size of BUF is SIZE. */
173 ctf_save_align_write (struct trace_write_handler
*handler
,
175 size_t size
, size_t align_size
)
178 = (align_up (handler
->content_size
, align_size
)
179 - handler
->content_size
);
181 if (ctf_save_fseek (handler
, offset
, SEEK_CUR
))
184 if (ctf_save_write (handler
, buf
, size
))
190 /* Write events to next new packet. */
193 ctf_save_next_packet (struct trace_write_handler
*handler
)
195 handler
->packet_start
+= (handler
->content_size
+ 4);
196 ctf_save_fseek (handler
, handler
->packet_start
, SEEK_SET
);
197 handler
->content_size
= 0;
200 /* Write the CTF metadata header. */
203 ctf_save_metadata_header (struct trace_write_handler
*handler
)
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;}"
211 ctf_save_write_metadata (handler
,
212 "typealias integer { size = 8; align = 8; "
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;}"
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;"
234 ctf_save_write_metadata (handler
, "\n");
236 /* Get the byte order of the host and write CTF data in this byte
239 #define HOST_ENDIANNESS "be"
241 #define HOST_ENDIANNESS "le"
244 ctf_save_write_metadata (handler
,
248 " byte_order = %s;\n"
249 " packet.header := struct {\n"
255 " packet.context := struct {\n"
256 " uint32_t content_size;\n"
257 " uint32_t packet_size;\n"
260 " event.header := struct {\n"
264 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
,
266 ctf_save_write_metadata (handler
, "\n");
269 /* CTF trace writer. */
271 struct ctf_trace_file_writer
273 struct trace_file_writer base
;
275 /* States related to writing CTF trace file. */
276 struct trace_write_handler tcs
;
279 /* This is the implementation of trace_file_write_ops method
283 ctf_dtor (struct trace_file_writer
*self
)
285 struct ctf_trace_file_writer
*writer
286 = (struct ctf_trace_file_writer
*) self
;
288 if (writer
->tcs
.metadata_fd
!= NULL
)
289 fclose (writer
->tcs
.metadata_fd
);
291 if (writer
->tcs
.datastream_fd
!= NULL
)
292 fclose (writer
->tcs
.datastream_fd
);
296 /* This is the implementation of trace_file_write_ops method
300 ctf_target_save (struct trace_file_writer
*self
,
303 /* Don't support save trace file to CTF format in the target. */
307 /* This is the implementation of trace_file_write_ops method
308 start. It creates the directory DIRNAME, metadata and datastream
312 ctf_start (struct trace_file_writer
*self
, const char *dirname
)
314 struct ctf_trace_file_writer
*writer
315 = (struct ctf_trace_file_writer
*) self
;
316 mode_t hmode
= S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
;
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
));
323 memset (&writer
->tcs
, '\0', sizeof (writer
->tcs
));
325 std::string file_name
= string_printf ("%s/%s", dirname
, CTF_METADATA_NAME
);
327 writer
->tcs
.metadata_fd
= fopen (file_name
.c_str (), "w");
328 if (writer
->tcs
.metadata_fd
== NULL
)
329 error (_("Unable to open file '%s' for saving trace data (%s)"),
330 file_name
.c_str (), safe_strerror (errno
));
332 ctf_save_metadata_header (&writer
->tcs
);
334 file_name
= string_printf ("%s/%s", dirname
, CTF_DATASTREAM_NAME
);
335 writer
->tcs
.datastream_fd
= fopen (file_name
.c_str (), "w");
336 if (writer
->tcs
.datastream_fd
== NULL
)
337 error (_("Unable to open file '%s' for saving trace data (%s)"),
338 file_name
.c_str (), safe_strerror (errno
));
341 /* This is the implementation of trace_file_write_ops method
342 write_header. Write the types of events on trace variable and
346 ctf_write_header (struct trace_file_writer
*self
)
348 struct ctf_trace_file_writer
*writer
349 = (struct ctf_trace_file_writer
*) self
;
352 ctf_save_write_metadata (&writer
->tcs
, "\n");
353 ctf_save_write_metadata (&writer
->tcs
,
354 "event {\n\tname = \"memory\";\n\tid = %u;\n"
355 "\tfields := struct { \n"
356 "\t\tuint64_t address;\n"
357 "\t\tuint16_t length;\n"
358 "\t\tuint8_t contents[length];\n"
360 "};\n", CTF_EVENT_ID_MEMORY
);
362 ctf_save_write_metadata (&writer
->tcs
, "\n");
363 ctf_save_write_metadata (&writer
->tcs
,
364 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
365 "\tfields := struct { \n"
366 "\t\tuint64_t val;\n"
367 "\t\tuint32_t num;\n"
369 "};\n", CTF_EVENT_ID_TSV
);
371 ctf_save_write_metadata (&writer
->tcs
, "\n");
372 ctf_save_write_metadata (&writer
->tcs
,
373 "event {\n\tname = \"frame\";\n\tid = %u;\n"
374 "\tfields := struct { \n"
376 "};\n", CTF_EVENT_ID_FRAME
);
378 ctf_save_write_metadata (&writer
->tcs
, "\n");
379 ctf_save_write_metadata (&writer
->tcs
,
380 "event {\n\tname = \"tsv_def\";\n"
381 "\tid = %u;\n\tfields := struct { \n"
382 "\t\tint64_t initial_value;\n"
383 "\t\tint32_t number;\n"
384 "\t\tint32_t builtin;\n"
387 "};\n", CTF_EVENT_ID_TSV_DEF
);
389 ctf_save_write_metadata (&writer
->tcs
, "\n");
390 ctf_save_write_metadata (&writer
->tcs
,
391 "event {\n\tname = \"tp_def\";\n"
392 "\tid = %u;\n\tfields := struct { \n"
393 "\t\tuint64_t addr;\n"
394 "\t\tuint64_t traceframe_usage;\n"
395 "\t\tint32_t number;\n"
396 "\t\tint32_t enabled;\n"
397 "\t\tint32_t step;\n"
398 "\t\tint32_t pass;\n"
399 "\t\tint32_t hit_count;\n"
400 "\t\tint32_t type;\n"
403 "\t\tuint32_t action_num;\n"
404 "\t\tchars actions[action_num];\n"
406 "\t\tuint32_t step_action_num;\n"
407 "\t\tchars step_actions[step_action_num];\n"
409 "\t\tchars at_string;\n"
410 "\t\tchars cond_string;\n"
412 "\t\tuint32_t cmd_num;\n"
413 "\t\tchars cmd_strings[cmd_num];\n"
415 "};\n", CTF_EVENT_ID_TP_DEF
);
417 gdb_assert (writer
->tcs
.content_size
== 0);
418 gdb_assert (writer
->tcs
.packet_start
== 0);
420 /* Create a new packet to contain this event. */
421 self
->ops
->frame_ops
->start (self
, 0);
424 /* This is the implementation of trace_file_write_ops method
425 write_regblock_type. Write the type of register event in
429 ctf_write_regblock_type (struct trace_file_writer
*self
, int size
)
431 struct ctf_trace_file_writer
*writer
432 = (struct ctf_trace_file_writer
*) self
;
434 ctf_save_write_metadata (&writer
->tcs
, "\n");
436 ctf_save_write_metadata (&writer
->tcs
,
437 "event {\n\tname = \"register\";\n\tid = %u;\n"
438 "\tfields := struct { \n"
439 "\t\tascii contents[%d];\n"
442 CTF_EVENT_ID_REGISTER
, size
);
445 /* This is the implementation of trace_file_write_ops method
449 ctf_write_status (struct trace_file_writer
*self
,
450 struct trace_status
*ts
)
452 struct ctf_trace_file_writer
*writer
453 = (struct ctf_trace_file_writer
*) self
;
456 ctf_save_write_metadata (&writer
->tcs
, "\n");
457 ctf_save_write_metadata (&writer
->tcs
,
458 "event {\n\tname = \"status\";\n\tid = %u;\n"
459 "\tfields := struct { \n"
460 "\t\tint32_t stop_reason;\n"
461 "\t\tint32_t stopping_tracepoint;\n"
462 "\t\tint32_t traceframe_count;\n"
463 "\t\tint32_t traceframes_created;\n"
464 "\t\tint32_t buffer_free;\n"
465 "\t\tint32_t buffer_size;\n"
466 "\t\tint32_t disconnected_tracing;\n"
467 "\t\tint32_t circular_buffer;\n"
470 CTF_EVENT_ID_STATUS
);
472 id
= CTF_EVENT_ID_STATUS
;
474 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
476 ctf_save_write_int32 (&writer
->tcs
, ts
->stop_reason
);
477 ctf_save_write_int32 (&writer
->tcs
, ts
->stopping_tracepoint
);
478 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframe_count
);
479 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframes_created
);
480 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_free
);
481 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_size
);
482 ctf_save_write_int32 (&writer
->tcs
, ts
->disconnected_tracing
);
483 ctf_save_write_int32 (&writer
->tcs
, ts
->circular_buffer
);
486 /* This is the implementation of trace_file_write_ops method
487 write_uploaded_tsv. */
490 ctf_write_uploaded_tsv (struct trace_file_writer
*self
,
491 struct uploaded_tsv
*tsv
)
493 struct ctf_trace_file_writer
*writer
494 = (struct ctf_trace_file_writer
*) self
;
497 const gdb_byte zero
= 0;
500 int32
= CTF_EVENT_ID_TSV_DEF
;
501 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
504 int64
= tsv
->initial_value
;
505 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
508 ctf_save_write_int32 (&writer
->tcs
, tsv
->number
);
511 ctf_save_write_int32 (&writer
->tcs
, tsv
->builtin
);
514 if (tsv
->name
!= NULL
)
515 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tsv
->name
,
517 ctf_save_write (&writer
->tcs
, &zero
, 1);
520 /* This is the implementation of trace_file_write_ops method
521 write_uploaded_tp. */
524 ctf_write_uploaded_tp (struct trace_file_writer
*self
,
525 struct uploaded_tp
*tp
)
527 struct ctf_trace_file_writer
*writer
528 = (struct ctf_trace_file_writer
*) self
;
532 const gdb_byte zero
= 0;
537 int32
= CTF_EVENT_ID_TP_DEF
;
538 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
542 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
544 /* traceframe_usage */
545 int64
= tp
->traceframe_usage
;
546 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
549 ctf_save_write_int32 (&writer
->tcs
, tp
->number
);
552 ctf_save_write_int32 (&writer
->tcs
, tp
->enabled
);
555 ctf_save_write_int32 (&writer
->tcs
, tp
->step
);
558 ctf_save_write_int32 (&writer
->tcs
, tp
->pass
);
561 ctf_save_write_int32 (&writer
->tcs
, tp
->hit_count
);
564 ctf_save_write_int32 (&writer
->tcs
, tp
->type
);
567 if (tp
->cond
!= NULL
)
568 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond
, strlen (tp
->cond
));
569 ctf_save_write (&writer
->tcs
, &zero
, 1);
572 u32
= VEC_length (char_ptr
, tp
->actions
);
573 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
574 for (a
= 0; VEC_iterate (char_ptr
, tp
->actions
, a
, act
); ++a
)
575 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
578 u32
= VEC_length (char_ptr
, tp
->step_actions
);
579 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
580 for (a
= 0; VEC_iterate (char_ptr
, tp
->step_actions
, a
, act
); ++a
)
581 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
584 if (tp
->at_string
!= NULL
)
585 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->at_string
,
586 strlen (tp
->at_string
));
587 ctf_save_write (&writer
->tcs
, &zero
, 1);
590 if (tp
->cond_string
!= NULL
)
591 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond_string
,
592 strlen (tp
->cond_string
));
593 ctf_save_write (&writer
->tcs
, &zero
, 1);
596 u32
= VEC_length (char_ptr
, tp
->cmd_strings
);
597 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
598 for (a
= 0; VEC_iterate (char_ptr
, tp
->cmd_strings
, a
, act
); ++a
)
599 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
603 /* This is the implementation of trace_file_write_ops method
607 ctf_write_tdesc (struct trace_file_writer
*self
)
609 /* Nothing so far. */
612 /* This is the implementation of trace_file_write_ops method
613 write_definition_end. */
616 ctf_write_definition_end (struct trace_file_writer
*self
)
618 self
->ops
->frame_ops
->end (self
);
621 /* This is the implementation of trace_file_write_ops method
625 ctf_end (struct trace_file_writer
*self
)
627 struct ctf_trace_file_writer
*writer
= (struct ctf_trace_file_writer
*) self
;
629 gdb_assert (writer
->tcs
.content_size
== 0);
632 /* This is the implementation of trace_frame_write_ops method
636 ctf_write_frame_start (struct trace_file_writer
*self
, uint16_t tpnum
)
638 struct ctf_trace_file_writer
*writer
639 = (struct ctf_trace_file_writer
*) self
;
640 uint32_t id
= CTF_EVENT_ID_FRAME
;
643 /* Step 1: Write packet context. */
646 ctf_save_write_uint32 (&writer
->tcs
, u32
);
647 /* content_size and packet_size.. We still don't know the value,
649 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
650 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
651 /* Tracepoint number. */
652 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &tpnum
, 2);
654 /* Step 2: Write event "frame". */
656 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
659 /* This is the implementation of trace_frame_write_ops method
663 ctf_write_frame_r_block (struct trace_file_writer
*self
,
664 gdb_byte
*buf
, int32_t size
)
666 struct ctf_trace_file_writer
*writer
667 = (struct ctf_trace_file_writer
*) self
;
668 uint32_t id
= CTF_EVENT_ID_REGISTER
;
671 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
673 /* array contents. */
674 ctf_save_align_write (&writer
->tcs
, buf
, size
, 1);
677 /* This is the implementation of trace_frame_write_ops method
678 write_m_block_header. */
681 ctf_write_frame_m_block_header (struct trace_file_writer
*self
,
682 uint64_t addr
, uint16_t length
)
684 struct ctf_trace_file_writer
*writer
685 = (struct ctf_trace_file_writer
*) self
;
686 uint32_t event_id
= CTF_EVENT_ID_MEMORY
;
689 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &event_id
, 4, 4);
692 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &addr
, 8, 8);
695 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &length
, 2, 2);
698 /* This is the implementation of trace_frame_write_ops method
699 write_m_block_memory. */
702 ctf_write_frame_m_block_memory (struct trace_file_writer
*self
,
703 gdb_byte
*buf
, uint16_t length
)
705 struct ctf_trace_file_writer
*writer
706 = (struct ctf_trace_file_writer
*) self
;
709 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) buf
, length
, 1);
712 /* This is the implementation of trace_frame_write_ops method
716 ctf_write_frame_v_block (struct trace_file_writer
*self
,
717 int32_t num
, uint64_t val
)
719 struct ctf_trace_file_writer
*writer
720 = (struct ctf_trace_file_writer
*) self
;
721 uint32_t id
= CTF_EVENT_ID_TSV
;
724 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
727 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &val
, 8, 8);
729 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &num
, 4, 4);
732 /* This is the implementation of trace_frame_write_ops method
736 ctf_write_frame_end (struct trace_file_writer
*self
)
738 struct ctf_trace_file_writer
*writer
739 = (struct ctf_trace_file_writer
*) self
;
743 /* Write the content size to packet header. */
744 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ 4,
746 u32
= writer
->tcs
.content_size
* TARGET_CHAR_BIT
;
748 t
= writer
->tcs
.content_size
;
749 ctf_save_write_uint32 (&writer
->tcs
, u32
);
751 /* Write the packet size. */
752 u32
+= 4 * TARGET_CHAR_BIT
;
753 ctf_save_write_uint32 (&writer
->tcs
, u32
);
755 writer
->tcs
.content_size
= t
;
757 /* Write zero at the end of the packet. */
758 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ t
,
761 ctf_save_write_uint32 (&writer
->tcs
, u32
);
762 writer
->tcs
.content_size
= t
;
764 ctf_save_next_packet (&writer
->tcs
);
767 /* Operations to write various types of trace frames into CTF
770 static const struct trace_frame_write_ops ctf_write_frame_ops
=
772 ctf_write_frame_start
,
773 ctf_write_frame_r_block
,
774 ctf_write_frame_m_block_header
,
775 ctf_write_frame_m_block_memory
,
776 ctf_write_frame_v_block
,
780 /* Operations to write trace buffers into CTF format. */
782 static const struct trace_file_write_ops ctf_write_ops
=
788 ctf_write_regblock_type
,
790 ctf_write_uploaded_tsv
,
791 ctf_write_uploaded_tp
,
793 ctf_write_definition_end
,
795 &ctf_write_frame_ops
,
799 /* Return a trace writer for CTF format. */
801 struct trace_file_writer
*
802 ctf_trace_file_writer_new (void)
804 struct ctf_trace_file_writer
*writer
= XNEW (struct ctf_trace_file_writer
);
806 writer
->base
.ops
= &ctf_write_ops
;
808 return (struct trace_file_writer
*) writer
;
811 #if HAVE_LIBBABELTRACE
812 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
813 iterator to iterate over each event in CTF data and APIs to get
814 details of event and packet, so it is very convenient to use
815 libbabeltrace to access events in CTF. */
817 #include <babeltrace/babeltrace.h>
818 #include <babeltrace/ctf/events.h>
819 #include <babeltrace/ctf/iterator.h>
821 /* The struct pointer for current CTF directory. */
822 static int handle_id
= -1;
823 static struct bt_context
*ctx
= NULL
;
824 static struct bt_ctf_iter
*ctf_iter
= NULL
;
825 /* The position of the first packet containing trace frame. */
826 static struct bt_iter_pos
*start_pos
;
828 /* The name of CTF directory. */
829 static char *trace_dirname
;
831 static struct target_ops ctf_ops
;
833 /* Destroy ctf iterator and context. */
838 if (ctf_iter
!= NULL
)
840 bt_ctf_iter_destroy (ctf_iter
);
845 bt_context_put (ctx
);
850 /* Open CTF trace data in DIRNAME. */
853 ctf_open_dir (const char *dirname
)
855 struct bt_iter_pos begin_pos
;
856 unsigned int count
, i
;
857 struct bt_ctf_event_decl
* const *list
;
859 ctx
= bt_context_create ();
861 error (_("Unable to create bt_context"));
862 handle_id
= bt_context_add_trace (ctx
, dirname
, "ctf", NULL
, NULL
, NULL
);
866 error (_("Unable to use libbabeltrace on directory \"%s\""),
870 begin_pos
.type
= BT_SEEK_BEGIN
;
871 ctf_iter
= bt_ctf_iter_create (ctx
, &begin_pos
, NULL
);
872 if (ctf_iter
== NULL
)
875 error (_("Unable to create bt_iterator"));
878 /* Look for the declaration of register block. Get the length of
879 array "contents" to set trace_regblock_size. */
881 bt_ctf_get_event_decl_list (handle_id
, ctx
, &list
, &count
);
882 for (i
= 0; i
< count
; i
++)
883 if (strcmp ("register", bt_ctf_get_decl_event_name (list
[i
])) == 0)
885 const struct bt_ctf_field_decl
* const *field_list
;
886 const struct bt_declaration
*decl
;
888 bt_ctf_get_decl_fields (list
[i
], BT_EVENT_FIELDS
, &field_list
,
891 gdb_assert (count
== 1);
892 gdb_assert (0 == strcmp ("contents",
893 bt_ctf_get_decl_field_name (field_list
[0])));
894 decl
= bt_ctf_get_decl_from_field_decl (field_list
[0]);
895 trace_regblock_size
= bt_ctf_get_array_len (decl
);
901 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
902 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
906 #define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \
907 (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
912 /* EVENT is the "status" event and TS is filled in. */
915 ctf_read_status (struct bt_ctf_event
*event
, struct trace_status
*ts
)
917 const struct bt_definition
*scope
918 = bt_ctf_get_top_level_scope (event
, BT_EVENT_FIELDS
);
920 SET_ENUM_FIELD (event
, scope
, ts
, enum trace_stop_reason
, stop_reason
);
921 SET_INT32_FIELD (event
, scope
, ts
, stopping_tracepoint
);
922 SET_INT32_FIELD (event
, scope
, ts
, traceframe_count
);
923 SET_INT32_FIELD (event
, scope
, ts
, traceframes_created
);
924 SET_INT32_FIELD (event
, scope
, ts
, buffer_free
);
925 SET_INT32_FIELD (event
, scope
, ts
, buffer_size
);
926 SET_INT32_FIELD (event
, scope
, ts
, disconnected_tracing
);
927 SET_INT32_FIELD (event
, scope
, ts
, circular_buffer
);
929 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
932 /* Read the events "tsv_def" one by one, extract its contents and fill
933 in the list UPLOADED_TSVS. */
936 ctf_read_tsv (struct uploaded_tsv
**uploaded_tsvs
)
938 gdb_assert (ctf_iter
!= NULL
);
942 struct bt_ctf_event
*event
;
943 const struct bt_definition
*scope
;
944 const struct bt_definition
*def
;
946 struct uploaded_tsv
*utsv
= NULL
;
948 event
= bt_ctf_iter_read_event (ctf_iter
);
949 scope
= bt_ctf_get_top_level_scope (event
,
950 BT_STREAM_EVENT_HEADER
);
951 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
953 if (event_id
!= CTF_EVENT_ID_TSV_DEF
)
956 scope
= bt_ctf_get_top_level_scope (event
,
959 def
= bt_ctf_get_field (event
, scope
, "number");
960 utsv
= get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def
),
963 def
= bt_ctf_get_field (event
, scope
, "builtin");
964 utsv
->builtin
= (int32_t) bt_ctf_get_int64 (def
);
965 def
= bt_ctf_get_field (event
, scope
, "initial_value");
966 utsv
->initial_value
= bt_ctf_get_int64 (def
);
968 def
= bt_ctf_get_field (event
, scope
, "name");
969 utsv
->name
= xstrdup (bt_ctf_get_string (def
));
971 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
977 /* Read the value of element whose index is NUM from CTF and write it
978 to the corresponding VAR->ARRAY. */
980 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
984 const struct bt_definition *def; \
986 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
989 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
990 for (i = 0; i < u32; i++) \
992 const struct bt_definition *element \
993 = bt_ctf_get_index ((EVENT), def, i); \
995 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
996 xstrdup (bt_ctf_get_string (element))); \
1001 /* Read a string from CTF and set VAR->FIELD. If the length of string
1002 is zero, set VAR->FIELD to NULL. */
1004 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1007 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1011 if (strlen (p) > 0) \
1012 (VAR)->FIELD = xstrdup (p); \
1014 (VAR)->FIELD = NULL; \
1018 /* Read the events "tp_def" one by one, extract its contents and fill
1019 in the list UPLOADED_TPS. */
1022 ctf_read_tp (struct uploaded_tp
**uploaded_tps
)
1024 gdb_assert (ctf_iter
!= NULL
);
1028 struct bt_ctf_event
*event
;
1029 const struct bt_definition
*scope
;
1033 struct uploaded_tp
*utp
= NULL
;
1035 event
= bt_ctf_iter_read_event (ctf_iter
);
1036 scope
= bt_ctf_get_top_level_scope (event
,
1037 BT_STREAM_EVENT_HEADER
);
1038 u32
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1040 if (u32
!= CTF_EVENT_ID_TP_DEF
)
1043 scope
= bt_ctf_get_top_level_scope (event
,
1045 int32
= (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event
,
1048 u64
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1050 utp
= get_uploaded_tp (int32
, u64
, uploaded_tps
);
1052 SET_INT32_FIELD (event
, scope
, utp
, enabled
);
1053 SET_INT32_FIELD (event
, scope
, utp
, step
);
1054 SET_INT32_FIELD (event
, scope
, utp
, pass
);
1055 SET_INT32_FIELD (event
, scope
, utp
, hit_count
);
1056 SET_ENUM_FIELD (event
, scope
, utp
, enum bptype
, type
);
1058 /* Read 'cmd_strings'. */
1059 SET_ARRAY_FIELD (event
, scope
, utp
, cmd_num
, cmd_strings
);
1060 /* Read 'actions'. */
1061 SET_ARRAY_FIELD (event
, scope
, utp
, action_num
, actions
);
1062 /* Read 'step_actions'. */
1063 SET_ARRAY_FIELD (event
, scope
, utp
, step_action_num
,
1066 SET_STRING_FIELD(event
, scope
, utp
, at_string
);
1067 SET_STRING_FIELD(event
, scope
, utp
, cond_string
);
1069 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1074 /* This is the implementation of target_ops method to_open. Open CTF
1075 trace data, read trace status, trace state variables and tracepoint
1076 definitions from the first packet. Set the start position at the
1077 second packet which contains events on trace blocks. */
1080 ctf_open (const char *dirname
, int from_tty
)
1082 struct bt_ctf_event
*event
;
1084 const struct bt_definition
*scope
;
1085 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
1086 struct uploaded_tp
*uploaded_tps
= NULL
;
1089 error (_("No CTF directory specified."));
1091 ctf_open_dir (dirname
);
1093 target_preopen (from_tty
);
1095 /* Skip the first packet which about the trace status. The first
1096 event is "frame". */
1097 event
= bt_ctf_iter_read_event (ctf_iter
);
1098 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1099 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1100 if (event_id
!= CTF_EVENT_ID_FRAME
)
1101 error (_("Wrong event id of the first event"));
1102 /* The second event is "status". */
1103 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1104 event
= bt_ctf_iter_read_event (ctf_iter
);
1105 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1106 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1107 if (event_id
!= CTF_EVENT_ID_STATUS
)
1108 error (_("Wrong event id of the second event"));
1109 ctf_read_status (event
, current_trace_status ());
1111 ctf_read_tsv (&uploaded_tsvs
);
1113 ctf_read_tp (&uploaded_tps
);
1115 event
= bt_ctf_iter_read_event (ctf_iter
);
1116 /* EVENT can be NULL if we've already gone to the end of stream of
1120 scope
= bt_ctf_get_top_level_scope (event
,
1121 BT_STREAM_EVENT_HEADER
);
1122 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
,
1124 if (event_id
!= CTF_EVENT_ID_FRAME
)
1125 error (_("Wrong event id of the first event of the second packet"));
1128 start_pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1129 gdb_assert (start_pos
->type
== BT_SEEK_RESTORE
);
1131 trace_dirname
= xstrdup (dirname
);
1132 push_target (&ctf_ops
);
1134 inferior_appeared (current_inferior (), CTF_PID
);
1135 inferior_ptid
= pid_to_ptid (CTF_PID
);
1136 add_thread_silent (inferior_ptid
);
1138 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
1139 merge_uploaded_tracepoints (&uploaded_tps
);
1141 post_create_inferior (&ctf_ops
, from_tty
);
1144 /* This is the implementation of target_ops method to_close. Destroy
1145 CTF iterator and context. */
1148 ctf_close (struct target_ops
*self
)
1153 xfree (trace_dirname
);
1154 trace_dirname
= NULL
;
1156 pid
= ptid_get_pid (inferior_ptid
);
1157 inferior_ptid
= null_ptid
; /* Avoid confusion from thread stuff. */
1158 exit_inferior_silent (pid
);
1160 trace_reset_local_state ();
1163 /* This is the implementation of target_ops method to_files_info.
1164 Print the directory name of CTF trace data. */
1167 ctf_files_info (struct target_ops
*t
)
1169 printf_filtered ("\t`%s'\n", trace_dirname
);
1172 /* This is the implementation of target_ops method to_fetch_registers.
1173 Iterate over events whose name is "register" in current frame,
1174 extract contents from events, and set REGCACHE with the contents.
1175 If no matched events are found, mark registers unavailable. */
1178 ctf_fetch_registers (struct target_ops
*ops
,
1179 struct regcache
*regcache
, int regno
)
1181 struct gdbarch
*gdbarch
= regcache
->arch ();
1182 struct bt_ctf_event
*event
= NULL
;
1183 struct bt_iter_pos
*pos
;
1185 /* An uninitialized reg size says we're not going to be
1186 successful at getting register blocks. */
1187 if (trace_regblock_size
== 0)
1190 gdb_assert (ctf_iter
!= NULL
);
1191 /* Save the current position. */
1192 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1193 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1198 struct bt_ctf_event
*event1
;
1200 event1
= bt_ctf_iter_read_event (ctf_iter
);
1202 name
= bt_ctf_event_name (event1
);
1204 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1206 else if (strcmp (name
, "register") == 0)
1212 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1216 /* Restore the position. */
1217 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1221 int offset
, regsize
, regn
;
1222 const struct bt_definition
*scope
1223 = bt_ctf_get_top_level_scope (event
,
1225 const struct bt_definition
*array
1226 = bt_ctf_get_field (event
, scope
, "contents");
1227 gdb_byte
*regs
= (gdb_byte
*) bt_ctf_get_char_array (array
);
1229 /* Assume the block is laid out in GDB register number order,
1230 each register with the size that it has in GDB. */
1232 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1234 regsize
= register_size (gdbarch
, regn
);
1235 /* Make sure we stay within block bounds. */
1236 if (offset
+ regsize
>= trace_regblock_size
)
1238 if (regcache_register_status (regcache
, regn
) == REG_UNKNOWN
)
1242 regcache_raw_supply (regcache
, regno
, regs
+ offset
);
1245 else if (regno
== -1)
1247 regcache_raw_supply (regcache
, regn
, regs
+ offset
);
1254 tracefile_fetch_registers (regcache
, regno
);
1257 /* This is the implementation of target_ops method to_xfer_partial.
1258 Iterate over events whose name is "memory" in
1259 current frame, extract the address and length from events. If
1260 OFFSET is within the range, read the contents from events to
1263 static enum target_xfer_status
1264 ctf_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1265 const char *annex
, gdb_byte
*readbuf
,
1266 const gdb_byte
*writebuf
, ULONGEST offset
,
1267 ULONGEST len
, ULONGEST
*xfered_len
)
1269 /* We're only doing regular memory for now. */
1270 if (object
!= TARGET_OBJECT_MEMORY
)
1271 return TARGET_XFER_E_IO
;
1273 if (readbuf
== NULL
)
1274 error (_("ctf_xfer_partial: trace file is read-only"));
1276 if (get_traceframe_number () != -1)
1278 struct bt_iter_pos
*pos
;
1279 enum target_xfer_status res
;
1280 /* Records the lowest available address of all blocks that
1281 intersects the requested range. */
1282 ULONGEST low_addr_available
= 0;
1284 gdb_assert (ctf_iter
!= NULL
);
1285 /* Save the current position. */
1286 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1287 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1289 /* Iterate through the traceframe's blocks, looking for
1296 const struct bt_definition
*scope
;
1297 const struct bt_definition
*def
;
1298 struct bt_ctf_event
*event
1299 = bt_ctf_iter_read_event (ctf_iter
);
1300 const char *name
= bt_ctf_event_name (event
);
1302 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1304 else if (strcmp (name
, "memory") != 0)
1306 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1312 scope
= bt_ctf_get_top_level_scope (event
,
1315 def
= bt_ctf_get_field (event
, scope
, "address");
1316 maddr
= bt_ctf_get_uint64 (def
);
1317 def
= bt_ctf_get_field (event
, scope
, "length");
1318 mlen
= (uint16_t) bt_ctf_get_uint64 (def
);
1320 /* If the block includes the first part of the desired
1321 range, return as much it has; GDB will re-request the
1322 remainder, which might be in a different block of this
1324 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
1326 const struct bt_definition
*array
1327 = bt_ctf_get_field (event
, scope
, "contents");
1331 contents
= (gdb_byte
*) xmalloc (mlen
);
1333 for (k
= 0; k
< mlen
; k
++)
1335 const struct bt_definition
*element
1336 = bt_ctf_get_index (event
, array
, k
);
1338 contents
[k
] = (gdb_byte
) bt_ctf_get_uint64 (element
);
1341 amt
= (maddr
+ mlen
) - offset
;
1345 memcpy (readbuf
, &contents
[offset
- maddr
], amt
);
1349 /* Restore the position. */
1350 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1353 return TARGET_XFER_EOF
;
1357 return TARGET_XFER_OK
;
1361 if (offset
< maddr
&& maddr
< (offset
+ len
))
1362 if (low_addr_available
== 0 || low_addr_available
> maddr
)
1363 low_addr_available
= maddr
;
1365 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1369 /* Restore the position. */
1370 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1372 /* Requested memory is unavailable in the context of traceframes,
1373 and this address falls within a read-only section, fallback
1374 to reading from executable, up to LOW_ADDR_AVAILABLE */
1375 if (offset
< low_addr_available
)
1376 len
= std::min (len
, low_addr_available
- offset
);
1377 res
= exec_read_partial_read_only (readbuf
, offset
, len
, xfered_len
);
1379 if (res
== TARGET_XFER_OK
)
1380 return TARGET_XFER_OK
;
1383 /* No use trying further, we know some memory starting
1384 at MEMADDR isn't available. */
1386 return TARGET_XFER_UNAVAILABLE
;
1391 /* Fallback to reading from read-only sections. */
1392 return section_table_read_available_memory (readbuf
, offset
, len
, xfered_len
);
1396 /* This is the implementation of target_ops method
1397 to_get_trace_state_variable_value.
1398 Iterate over events whose name is "tsv" in current frame. When the
1399 trace variable is found, set the value of it to *VAL and return
1400 true, otherwise return false. */
1403 ctf_get_trace_state_variable_value (struct target_ops
*self
,
1404 int tsvnum
, LONGEST
*val
)
1406 struct bt_iter_pos
*pos
;
1409 gdb_assert (ctf_iter
!= NULL
);
1410 /* Save the current position. */
1411 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1412 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1414 /* Iterate through the traceframe's blocks, looking for 'V'
1418 struct bt_ctf_event
*event
1419 = bt_ctf_iter_read_event (ctf_iter
);
1420 const char *name
= bt_ctf_event_name (event
);
1422 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1424 else if (strcmp (name
, "tsv") == 0)
1426 const struct bt_definition
*scope
;
1427 const struct bt_definition
*def
;
1429 scope
= bt_ctf_get_top_level_scope (event
,
1432 def
= bt_ctf_get_field (event
, scope
, "num");
1433 if (tsvnum
== (int32_t) bt_ctf_get_uint64 (def
))
1435 def
= bt_ctf_get_field (event
, scope
, "val");
1436 *val
= bt_ctf_get_uint64 (def
);
1442 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1446 /* Restore the position. */
1447 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1452 /* Return the tracepoint number in "frame" event. */
1455 ctf_get_tpnum_from_frame_event (struct bt_ctf_event
*event
)
1457 /* The packet context of events has a field "tpnum". */
1458 const struct bt_definition
*scope
1459 = bt_ctf_get_top_level_scope (event
, BT_STREAM_PACKET_CONTEXT
);
1461 = bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "tpnum"));
1466 /* Return the address at which the current frame was collected. */
1469 ctf_get_traceframe_address (void)
1471 struct bt_ctf_event
*event
= NULL
;
1472 struct bt_iter_pos
*pos
;
1475 gdb_assert (ctf_iter
!= NULL
);
1476 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1477 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1482 struct bt_ctf_event
*event1
;
1484 event1
= bt_ctf_iter_read_event (ctf_iter
);
1486 name
= bt_ctf_event_name (event1
);
1490 else if (strcmp (name
, "frame") == 0)
1496 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1502 int tpnum
= ctf_get_tpnum_from_frame_event (event
);
1503 struct tracepoint
*tp
1504 = get_tracepoint_by_number_on_target (tpnum
);
1507 addr
= tp
->loc
->address
;
1510 /* Restore the position. */
1511 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1516 /* This is the implementation of target_ops method to_trace_find.
1517 Iterate the events whose name is "frame", extract the tracepoint
1518 number in it. Return traceframe number when matched. */
1521 ctf_trace_find (struct target_ops
*self
, enum trace_find_type type
, int num
,
1522 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
1534 gdb_assert (ctf_iter
!= NULL
);
1535 /* Set iterator back to the start. */
1536 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), start_pos
);
1540 struct bt_ctf_event
*event
;
1543 event
= bt_ctf_iter_read_event (ctf_iter
);
1545 name
= bt_ctf_event_name (event
);
1547 if (event
== NULL
|| name
== NULL
)
1550 if (strcmp (name
, "frame") == 0)
1554 if (type
== tfind_number
)
1556 /* Looking for a specific trace frame. */
1562 /* Start from the _next_ trace frame. */
1563 if (tfnum
> get_traceframe_number ())
1569 struct tracepoint
*tp
= get_tracepoint (num
);
1572 && (tp
->number_on_target
1573 == ctf_get_tpnum_from_frame_event (event
)))
1578 tfaddr
= ctf_get_traceframe_address ();
1579 if (tfaddr
== addr1
)
1583 tfaddr
= ctf_get_traceframe_address ();
1584 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
1588 tfaddr
= ctf_get_traceframe_address ();
1589 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
1593 internal_error (__FILE__
, __LINE__
, _("unknown tfind type"));
1600 *tpp
= ctf_get_tpnum_from_frame_event (event
);
1602 /* Skip the event "frame". */
1603 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1610 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1617 /* This is the implementation of target_ops method to_traceframe_info.
1618 Iterate the events whose name is "memory", in current
1619 frame, extract memory range information, and return them in
1622 static traceframe_info_up
1623 ctf_traceframe_info (struct target_ops
*self
)
1625 traceframe_info_up
info (new traceframe_info
);
1627 struct bt_iter_pos
*pos
;
1629 gdb_assert (ctf_iter
!= NULL
);
1630 /* Save the current position. */
1631 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1632 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1636 struct bt_ctf_event
*event
1637 = bt_ctf_iter_read_event (ctf_iter
);
1639 name
= bt_ctf_event_name (event
);
1641 if (name
== NULL
|| strcmp (name
, "register") == 0
1642 || strcmp (name
, "frame") == 0)
1644 else if (strcmp (name
, "memory") == 0)
1646 const struct bt_definition
*scope
1647 = bt_ctf_get_top_level_scope (event
,
1649 const struct bt_definition
*def
;
1651 def
= bt_ctf_get_field (event
, scope
, "address");
1652 CORE_ADDR start
= bt_ctf_get_uint64 (def
);
1654 def
= bt_ctf_get_field (event
, scope
, "length");
1655 int length
= (uint16_t) bt_ctf_get_uint64 (def
);
1657 info
->memory
.emplace_back (start
, length
);
1659 else if (strcmp (name
, "tsv") == 0)
1662 const struct bt_definition
*scope
1663 = bt_ctf_get_top_level_scope (event
,
1665 const struct bt_definition
*def
;
1667 def
= bt_ctf_get_field (event
, scope
, "num");
1668 vnum
= (int) bt_ctf_get_uint64 (def
);
1669 info
->tvars
.push_back (vnum
);
1673 warning (_("Unhandled trace block type (%s) "
1674 "while building trace frame info."),
1678 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1681 while (name
!= NULL
&& strcmp (name
, "frame") != 0);
1683 /* Restore the position. */
1684 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1692 memset (&ctf_ops
, 0, sizeof (ctf_ops
));
1694 init_tracefile_ops (&ctf_ops
);
1695 ctf_ops
.to_shortname
= "ctf";
1696 ctf_ops
.to_longname
= "CTF file";
1697 ctf_ops
.to_doc
= "Use a CTF directory as a target.\n\
1698 Specify the filename of the CTF directory.";
1699 ctf_ops
.to_open
= ctf_open
;
1700 ctf_ops
.to_close
= ctf_close
;
1701 ctf_ops
.to_fetch_registers
= ctf_fetch_registers
;
1702 ctf_ops
.to_xfer_partial
= ctf_xfer_partial
;
1703 ctf_ops
.to_files_info
= ctf_files_info
;
1704 ctf_ops
.to_trace_find
= ctf_trace_find
;
1705 ctf_ops
.to_get_trace_state_variable_value
1706 = ctf_get_trace_state_variable_value
;
1707 ctf_ops
.to_traceframe_info
= ctf_traceframe_info
;
1712 /* module initialization */
1715 _initialize_ctf (void)
1717 #if HAVE_LIBBABELTRACE
1720 add_target_with_completer (&ctf_ops
, filename_completer
);