3 Copyright (C) 2012-2016 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 const char metadata_fmt
[] =
209 " byte_order = %s;\n" /* be or le */
210 " packet.header := struct {\n"
216 " packet.context := struct {\n"
217 " uint32_t content_size;\n"
218 " uint32_t packet_size;\n"
221 " event.header := struct {\n"
226 ctf_save_write_metadata (handler
, "/* CTF %d.%d */\n",
227 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
);
228 ctf_save_write_metadata (handler
,
229 "typealias integer { size = 8; align = 8; "
230 "signed = false; encoding = ascii;}"
232 ctf_save_write_metadata (handler
,
233 "typealias integer { size = 8; align = 8; "
236 ctf_save_write_metadata (handler
,
237 "typealias integer { size = 16; align = 16;"
238 "signed = false; } := uint16_t;\n");
239 ctf_save_write_metadata (handler
,
240 "typealias integer { size = 32; align = 32;"
241 "signed = false; } := uint32_t;\n");
242 ctf_save_write_metadata (handler
,
243 "typealias integer { size = 64; align = 64;"
244 "signed = false; base = hex;}"
246 ctf_save_write_metadata (handler
,
247 "typealias integer { size = 32; align = 32;"
248 "signed = true; } := int32_t;\n");
249 ctf_save_write_metadata (handler
,
250 "typealias integer { size = 64; align = 64;"
251 "signed = true; } := int64_t;\n");
252 ctf_save_write_metadata (handler
,
253 "typealias string { encoding = ascii;"
255 ctf_save_write_metadata (handler
, "\n");
257 /* Get the byte order of the host and write CTF data in this byte
260 #define HOST_ENDIANNESS "be"
262 #define HOST_ENDIANNESS "le"
265 ctf_save_write_metadata (handler
, metadata_fmt
,
266 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
,
268 ctf_save_write_metadata (handler
, "\n");
271 /* CTF trace writer. */
273 struct ctf_trace_file_writer
275 struct trace_file_writer base
;
277 /* States related to writing CTF trace file. */
278 struct trace_write_handler tcs
;
281 /* This is the implementation of trace_file_write_ops method
285 ctf_dtor (struct trace_file_writer
*self
)
287 struct ctf_trace_file_writer
*writer
288 = (struct ctf_trace_file_writer
*) self
;
290 if (writer
->tcs
.metadata_fd
!= NULL
)
291 fclose (writer
->tcs
.metadata_fd
);
293 if (writer
->tcs
.datastream_fd
!= NULL
)
294 fclose (writer
->tcs
.datastream_fd
);
298 /* This is the implementation of trace_file_write_ops method
302 ctf_target_save (struct trace_file_writer
*self
,
305 /* Don't support save trace file to CTF format in the target. */
311 #define mkdir(pathname, mode) mkdir (pathname)
314 /* This is the implementation of trace_file_write_ops method
315 start. It creates the directory DIRNAME, metadata and datastream
319 ctf_start (struct trace_file_writer
*self
, const char *dirname
)
322 struct cleanup
*old_chain
;
323 struct ctf_trace_file_writer
*writer
324 = (struct ctf_trace_file_writer
*) self
;
326 mode_t hmode
= S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
;
328 /* Create DIRNAME. */
329 if (mkdir (dirname
, hmode
) && errno
!= EEXIST
)
330 error (_("Unable to open directory '%s' for saving trace data (%s)"),
331 dirname
, safe_strerror (errno
));
333 memset (&writer
->tcs
, '\0', sizeof (writer
->tcs
));
335 file_name
= xstrprintf ("%s/%s", dirname
, CTF_METADATA_NAME
);
336 old_chain
= make_cleanup (xfree
, file_name
);
338 writer
->tcs
.metadata_fd
= fopen (file_name
, "w");
339 if (writer
->tcs
.metadata_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
);
344 ctf_save_metadata_header (&writer
->tcs
);
346 file_name
= xstrprintf ("%s/%s", dirname
, CTF_DATASTREAM_NAME
);
347 old_chain
= make_cleanup (xfree
, file_name
);
348 writer
->tcs
.datastream_fd
= fopen (file_name
, "w");
349 if (writer
->tcs
.datastream_fd
== NULL
)
350 error (_("Unable to open file '%s' for saving trace data (%s)"),
351 file_name
, safe_strerror (errno
));
352 do_cleanups (old_chain
);
355 /* This is the implementation of trace_file_write_ops method
356 write_header. Write the types of events on trace variable and
360 ctf_write_header (struct trace_file_writer
*self
)
362 struct ctf_trace_file_writer
*writer
363 = (struct ctf_trace_file_writer
*) self
;
366 ctf_save_write_metadata (&writer
->tcs
, "\n");
367 ctf_save_write_metadata (&writer
->tcs
,
368 "event {\n\tname = \"memory\";\n\tid = %u;\n"
369 "\tfields := struct { \n"
370 "\t\tuint64_t address;\n"
371 "\t\tuint16_t length;\n"
372 "\t\tuint8_t contents[length];\n"
374 "};\n", CTF_EVENT_ID_MEMORY
);
376 ctf_save_write_metadata (&writer
->tcs
, "\n");
377 ctf_save_write_metadata (&writer
->tcs
,
378 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
379 "\tfields := struct { \n"
380 "\t\tuint64_t val;\n"
381 "\t\tuint32_t num;\n"
383 "};\n", CTF_EVENT_ID_TSV
);
385 ctf_save_write_metadata (&writer
->tcs
, "\n");
386 ctf_save_write_metadata (&writer
->tcs
,
387 "event {\n\tname = \"frame\";\n\tid = %u;\n"
388 "\tfields := struct { \n"
390 "};\n", CTF_EVENT_ID_FRAME
);
392 ctf_save_write_metadata (&writer
->tcs
, "\n");
393 ctf_save_write_metadata (&writer
->tcs
,
394 "event {\n\tname = \"tsv_def\";\n"
395 "\tid = %u;\n\tfields := struct { \n"
396 "\t\tint64_t initial_value;\n"
397 "\t\tint32_t number;\n"
398 "\t\tint32_t builtin;\n"
401 "};\n", CTF_EVENT_ID_TSV_DEF
);
403 ctf_save_write_metadata (&writer
->tcs
, "\n");
404 ctf_save_write_metadata (&writer
->tcs
,
405 "event {\n\tname = \"tp_def\";\n"
406 "\tid = %u;\n\tfields := struct { \n"
407 "\t\tuint64_t addr;\n"
408 "\t\tuint64_t traceframe_usage;\n"
409 "\t\tint32_t number;\n"
410 "\t\tint32_t enabled;\n"
411 "\t\tint32_t step;\n"
412 "\t\tint32_t pass;\n"
413 "\t\tint32_t hit_count;\n"
414 "\t\tint32_t type;\n"
417 "\t\tuint32_t action_num;\n"
418 "\t\tchars actions[action_num];\n"
420 "\t\tuint32_t step_action_num;\n"
421 "\t\tchars step_actions[step_action_num];\n"
423 "\t\tchars at_string;\n"
424 "\t\tchars cond_string;\n"
426 "\t\tuint32_t cmd_num;\n"
427 "\t\tchars cmd_strings[cmd_num];\n"
429 "};\n", CTF_EVENT_ID_TP_DEF
);
431 gdb_assert (writer
->tcs
.content_size
== 0);
432 gdb_assert (writer
->tcs
.packet_start
== 0);
434 /* Create a new packet to contain this event. */
435 self
->ops
->frame_ops
->start (self
, 0);
438 /* This is the implementation of trace_file_write_ops method
439 write_regblock_type. Write the type of register event in
443 ctf_write_regblock_type (struct trace_file_writer
*self
, int size
)
445 struct ctf_trace_file_writer
*writer
446 = (struct ctf_trace_file_writer
*) self
;
448 ctf_save_write_metadata (&writer
->tcs
, "\n");
450 ctf_save_write_metadata (&writer
->tcs
,
451 "event {\n\tname = \"register\";\n\tid = %u;\n"
452 "\tfields := struct { \n"
453 "\t\tascii contents[%d];\n"
456 CTF_EVENT_ID_REGISTER
, size
);
459 /* This is the implementation of trace_file_write_ops method
463 ctf_write_status (struct trace_file_writer
*self
,
464 struct trace_status
*ts
)
466 struct ctf_trace_file_writer
*writer
467 = (struct ctf_trace_file_writer
*) self
;
471 ctf_save_write_metadata (&writer
->tcs
, "\n");
472 ctf_save_write_metadata (&writer
->tcs
,
473 "event {\n\tname = \"status\";\n\tid = %u;\n"
474 "\tfields := struct { \n"
475 "\t\tint32_t stop_reason;\n"
476 "\t\tint32_t stopping_tracepoint;\n"
477 "\t\tint32_t traceframe_count;\n"
478 "\t\tint32_t traceframes_created;\n"
479 "\t\tint32_t buffer_free;\n"
480 "\t\tint32_t buffer_size;\n"
481 "\t\tint32_t disconnected_tracing;\n"
482 "\t\tint32_t circular_buffer;\n"
485 CTF_EVENT_ID_STATUS
);
487 id
= CTF_EVENT_ID_STATUS
;
489 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
491 ctf_save_write_int32 (&writer
->tcs
, ts
->stop_reason
);
492 ctf_save_write_int32 (&writer
->tcs
, ts
->stopping_tracepoint
);
493 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframe_count
);
494 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframes_created
);
495 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_free
);
496 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_size
);
497 ctf_save_write_int32 (&writer
->tcs
, ts
->disconnected_tracing
);
498 ctf_save_write_int32 (&writer
->tcs
, ts
->circular_buffer
);
501 /* This is the implementation of trace_file_write_ops method
502 write_uploaded_tsv. */
505 ctf_write_uploaded_tsv (struct trace_file_writer
*self
,
506 struct uploaded_tsv
*tsv
)
508 struct ctf_trace_file_writer
*writer
509 = (struct ctf_trace_file_writer
*) self
;
513 const gdb_byte zero
= 0;
516 int32
= CTF_EVENT_ID_TSV_DEF
;
517 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
520 int64
= tsv
->initial_value
;
521 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
524 ctf_save_write_int32 (&writer
->tcs
, tsv
->number
);
527 ctf_save_write_int32 (&writer
->tcs
, tsv
->builtin
);
530 if (tsv
->name
!= NULL
)
531 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tsv
->name
,
533 ctf_save_write (&writer
->tcs
, &zero
, 1);
536 /* This is the implementation of trace_file_write_ops method
537 write_uploaded_tp. */
540 ctf_write_uploaded_tp (struct trace_file_writer
*self
,
541 struct uploaded_tp
*tp
)
543 struct ctf_trace_file_writer
*writer
544 = (struct ctf_trace_file_writer
*) self
;
548 const gdb_byte zero
= 0;
553 int32
= CTF_EVENT_ID_TP_DEF
;
554 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
558 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
560 /* traceframe_usage */
561 int64
= tp
->traceframe_usage
;
562 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
565 ctf_save_write_int32 (&writer
->tcs
, tp
->number
);
568 ctf_save_write_int32 (&writer
->tcs
, tp
->enabled
);
571 ctf_save_write_int32 (&writer
->tcs
, tp
->step
);
574 ctf_save_write_int32 (&writer
->tcs
, tp
->pass
);
577 ctf_save_write_int32 (&writer
->tcs
, tp
->hit_count
);
580 ctf_save_write_int32 (&writer
->tcs
, tp
->type
);
583 if (tp
->cond
!= NULL
)
584 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond
, strlen (tp
->cond
));
585 ctf_save_write (&writer
->tcs
, &zero
, 1);
588 u32
= VEC_length (char_ptr
, tp
->actions
);
589 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
590 for (a
= 0; VEC_iterate (char_ptr
, tp
->actions
, a
, act
); ++a
)
591 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
594 u32
= VEC_length (char_ptr
, tp
->step_actions
);
595 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
596 for (a
= 0; VEC_iterate (char_ptr
, tp
->step_actions
, a
, act
); ++a
)
597 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
600 if (tp
->at_string
!= NULL
)
601 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->at_string
,
602 strlen (tp
->at_string
));
603 ctf_save_write (&writer
->tcs
, &zero
, 1);
606 if (tp
->cond_string
!= NULL
)
607 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond_string
,
608 strlen (tp
->cond_string
));
609 ctf_save_write (&writer
->tcs
, &zero
, 1);
612 u32
= VEC_length (char_ptr
, tp
->cmd_strings
);
613 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
614 for (a
= 0; VEC_iterate (char_ptr
, tp
->cmd_strings
, a
, act
); ++a
)
615 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
619 /* This is the implementation of trace_file_write_ops method
620 write_definition_end. */
623 ctf_write_definition_end (struct trace_file_writer
*self
)
625 struct ctf_trace_file_writer
*writer
626 = (struct ctf_trace_file_writer
*) self
;
628 self
->ops
->frame_ops
->end (self
);
631 /* This is the implementation of trace_file_write_ops method
635 ctf_end (struct trace_file_writer
*self
)
637 struct ctf_trace_file_writer
*writer
= (struct ctf_trace_file_writer
*) self
;
639 gdb_assert (writer
->tcs
.content_size
== 0);
642 /* This is the implementation of trace_frame_write_ops method
646 ctf_write_frame_start (struct trace_file_writer
*self
, uint16_t tpnum
)
648 struct ctf_trace_file_writer
*writer
649 = (struct ctf_trace_file_writer
*) self
;
650 uint32_t id
= CTF_EVENT_ID_FRAME
;
653 /* Step 1: Write packet context. */
656 ctf_save_write_uint32 (&writer
->tcs
, u32
);
657 /* content_size and packet_size.. We still don't know the value,
659 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
660 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
661 /* Tracepoint number. */
662 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &tpnum
, 2);
664 /* Step 2: Write event "frame". */
666 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
669 /* This is the implementation of trace_frame_write_ops method
673 ctf_write_frame_r_block (struct trace_file_writer
*self
,
674 gdb_byte
*buf
, int32_t size
)
676 struct ctf_trace_file_writer
*writer
677 = (struct ctf_trace_file_writer
*) self
;
678 uint32_t id
= CTF_EVENT_ID_REGISTER
;
681 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
683 /* array contents. */
684 ctf_save_align_write (&writer
->tcs
, buf
, size
, 1);
687 /* This is the implementation of trace_frame_write_ops method
688 write_m_block_header. */
691 ctf_write_frame_m_block_header (struct trace_file_writer
*self
,
692 uint64_t addr
, uint16_t length
)
694 struct ctf_trace_file_writer
*writer
695 = (struct ctf_trace_file_writer
*) self
;
696 uint32_t event_id
= CTF_EVENT_ID_MEMORY
;
699 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &event_id
, 4, 4);
702 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &addr
, 8, 8);
705 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &length
, 2, 2);
708 /* This is the implementation of trace_frame_write_ops method
709 write_m_block_memory. */
712 ctf_write_frame_m_block_memory (struct trace_file_writer
*self
,
713 gdb_byte
*buf
, uint16_t length
)
715 struct ctf_trace_file_writer
*writer
716 = (struct ctf_trace_file_writer
*) self
;
719 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) buf
, length
, 1);
722 /* This is the implementation of trace_frame_write_ops method
726 ctf_write_frame_v_block (struct trace_file_writer
*self
,
727 int32_t num
, uint64_t val
)
729 struct ctf_trace_file_writer
*writer
730 = (struct ctf_trace_file_writer
*) self
;
731 uint32_t id
= CTF_EVENT_ID_TSV
;
734 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
737 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &val
, 8, 8);
739 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &num
, 4, 4);
742 /* This is the implementation of trace_frame_write_ops method
746 ctf_write_frame_end (struct trace_file_writer
*self
)
748 struct ctf_trace_file_writer
*writer
749 = (struct ctf_trace_file_writer
*) self
;
753 /* Write the content size to packet header. */
754 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ 4,
756 u32
= writer
->tcs
.content_size
* TARGET_CHAR_BIT
;
758 t
= writer
->tcs
.content_size
;
759 ctf_save_write_uint32 (&writer
->tcs
, u32
);
761 /* Write the packet size. */
762 u32
+= 4 * TARGET_CHAR_BIT
;
763 ctf_save_write_uint32 (&writer
->tcs
, u32
);
765 writer
->tcs
.content_size
= t
;
767 /* Write zero at the end of the packet. */
768 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ t
,
771 ctf_save_write_uint32 (&writer
->tcs
, u32
);
772 writer
->tcs
.content_size
= t
;
774 ctf_save_next_packet (&writer
->tcs
);
777 /* Operations to write various types of trace frames into CTF
780 static const struct trace_frame_write_ops ctf_write_frame_ops
=
782 ctf_write_frame_start
,
783 ctf_write_frame_r_block
,
784 ctf_write_frame_m_block_header
,
785 ctf_write_frame_m_block_memory
,
786 ctf_write_frame_v_block
,
790 /* Operations to write trace buffers into CTF format. */
792 static const struct trace_file_write_ops ctf_write_ops
=
798 ctf_write_regblock_type
,
800 ctf_write_uploaded_tsv
,
801 ctf_write_uploaded_tp
,
802 ctf_write_definition_end
,
804 &ctf_write_frame_ops
,
808 /* Return a trace writer for CTF format. */
810 struct trace_file_writer
*
811 ctf_trace_file_writer_new (void)
813 struct ctf_trace_file_writer
*writer
= XNEW (struct ctf_trace_file_writer
);
815 writer
->base
.ops
= &ctf_write_ops
;
817 return (struct trace_file_writer
*) writer
;
820 #if HAVE_LIBBABELTRACE
821 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
822 iterator to iterate over each event in CTF data and APIs to get
823 details of event and packet, so it is very convenient to use
824 libbabeltrace to access events in CTF. */
826 #include <babeltrace/babeltrace.h>
827 #include <babeltrace/ctf/events.h>
828 #include <babeltrace/ctf/iterator.h>
830 /* The struct pointer for current CTF directory. */
831 static int handle_id
= -1;
832 static struct bt_context
*ctx
= NULL
;
833 static struct bt_ctf_iter
*ctf_iter
= NULL
;
834 /* The position of the first packet containing trace frame. */
835 static struct bt_iter_pos
*start_pos
;
837 /* The name of CTF directory. */
838 static char *trace_dirname
;
840 static struct target_ops ctf_ops
;
842 /* Destroy ctf iterator and context. */
847 if (ctf_iter
!= NULL
)
849 bt_ctf_iter_destroy (ctf_iter
);
854 bt_context_put (ctx
);
859 /* Open CTF trace data in DIRNAME. */
862 ctf_open_dir (const char *dirname
)
864 struct bt_iter_pos begin_pos
;
865 struct bt_iter_pos
*pos
;
866 unsigned int count
, i
;
867 struct bt_ctf_event_decl
* const *list
;
869 ctx
= bt_context_create ();
871 error (_("Unable to create bt_context"));
872 handle_id
= bt_context_add_trace (ctx
, dirname
, "ctf", NULL
, NULL
, NULL
);
876 error (_("Unable to use libbabeltrace on directory \"%s\""),
880 begin_pos
.type
= BT_SEEK_BEGIN
;
881 ctf_iter
= bt_ctf_iter_create (ctx
, &begin_pos
, NULL
);
882 if (ctf_iter
== NULL
)
885 error (_("Unable to create bt_iterator"));
888 /* Look for the declaration of register block. Get the length of
889 array "contents" to set trace_regblock_size. */
891 bt_ctf_get_event_decl_list (handle_id
, ctx
, &list
, &count
);
892 for (i
= 0; i
< count
; i
++)
893 if (strcmp ("register", bt_ctf_get_decl_event_name (list
[i
])) == 0)
896 const struct bt_ctf_field_decl
* const *field_list
;
897 const struct bt_declaration
*decl
;
899 bt_ctf_get_decl_fields (list
[i
], BT_EVENT_FIELDS
, &field_list
,
902 gdb_assert (count
== 1);
903 gdb_assert (0 == strcmp ("contents",
904 bt_ctf_get_decl_field_name (field_list
[0])));
905 decl
= bt_ctf_get_decl_from_field_decl (field_list
[0]);
906 trace_regblock_size
= bt_ctf_get_array_len (decl
);
912 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
913 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
917 #define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \
918 (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
923 /* EVENT is the "status" event and TS is filled in. */
926 ctf_read_status (struct bt_ctf_event
*event
, struct trace_status
*ts
)
928 const struct bt_definition
*scope
929 = bt_ctf_get_top_level_scope (event
, BT_EVENT_FIELDS
);
931 SET_ENUM_FIELD (event
, scope
, ts
, enum trace_stop_reason
, stop_reason
);
932 SET_INT32_FIELD (event
, scope
, ts
, stopping_tracepoint
);
933 SET_INT32_FIELD (event
, scope
, ts
, traceframe_count
);
934 SET_INT32_FIELD (event
, scope
, ts
, traceframes_created
);
935 SET_INT32_FIELD (event
, scope
, ts
, buffer_free
);
936 SET_INT32_FIELD (event
, scope
, ts
, buffer_size
);
937 SET_INT32_FIELD (event
, scope
, ts
, disconnected_tracing
);
938 SET_INT32_FIELD (event
, scope
, ts
, circular_buffer
);
940 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
943 /* Read the events "tsv_def" one by one, extract its contents and fill
944 in the list UPLOADED_TSVS. */
947 ctf_read_tsv (struct uploaded_tsv
**uploaded_tsvs
)
949 gdb_assert (ctf_iter
!= NULL
);
953 struct bt_ctf_event
*event
;
954 const struct bt_definition
*scope
;
955 const struct bt_definition
*def
;
957 struct uploaded_tsv
*utsv
= NULL
;
959 event
= bt_ctf_iter_read_event (ctf_iter
);
960 scope
= bt_ctf_get_top_level_scope (event
,
961 BT_STREAM_EVENT_HEADER
);
962 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
964 if (event_id
!= CTF_EVENT_ID_TSV_DEF
)
967 scope
= bt_ctf_get_top_level_scope (event
,
970 def
= bt_ctf_get_field (event
, scope
, "number");
971 utsv
= get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def
),
974 def
= bt_ctf_get_field (event
, scope
, "builtin");
975 utsv
->builtin
= (int32_t) bt_ctf_get_int64 (def
);
976 def
= bt_ctf_get_field (event
, scope
, "initial_value");
977 utsv
->initial_value
= bt_ctf_get_int64 (def
);
979 def
= bt_ctf_get_field (event
, scope
, "name");
980 utsv
->name
= xstrdup (bt_ctf_get_string (def
));
982 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
988 /* Read the value of element whose index is NUM from CTF and write it
989 to the corresponding VAR->ARRAY. */
991 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
995 const struct bt_definition *def; \
997 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1000 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1001 for (i = 0; i < u32; i++) \
1003 const struct bt_definition *element \
1004 = bt_ctf_get_index ((EVENT), def, i); \
1006 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1007 xstrdup (bt_ctf_get_string (element))); \
1012 /* Read a string from CTF and set VAR->FIELD. If the length of string
1013 is zero, set VAR->FIELD to NULL. */
1015 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1018 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1022 if (strlen (p) > 0) \
1023 (VAR)->FIELD = xstrdup (p); \
1025 (VAR)->FIELD = NULL; \
1029 /* Read the events "tp_def" one by one, extract its contents and fill
1030 in the list UPLOADED_TPS. */
1033 ctf_read_tp (struct uploaded_tp
**uploaded_tps
)
1035 gdb_assert (ctf_iter
!= NULL
);
1039 struct bt_ctf_event
*event
;
1040 const struct bt_definition
*scope
;
1044 struct uploaded_tp
*utp
= NULL
;
1046 event
= bt_ctf_iter_read_event (ctf_iter
);
1047 scope
= bt_ctf_get_top_level_scope (event
,
1048 BT_STREAM_EVENT_HEADER
);
1049 u32
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1051 if (u32
!= CTF_EVENT_ID_TP_DEF
)
1054 scope
= bt_ctf_get_top_level_scope (event
,
1056 int32
= (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event
,
1059 u64
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1061 utp
= get_uploaded_tp (int32
, u64
, uploaded_tps
);
1063 SET_INT32_FIELD (event
, scope
, utp
, enabled
);
1064 SET_INT32_FIELD (event
, scope
, utp
, step
);
1065 SET_INT32_FIELD (event
, scope
, utp
, pass
);
1066 SET_INT32_FIELD (event
, scope
, utp
, hit_count
);
1067 SET_ENUM_FIELD (event
, scope
, utp
, enum bptype
, type
);
1069 /* Read 'cmd_strings'. */
1070 SET_ARRAY_FIELD (event
, scope
, utp
, cmd_num
, cmd_strings
);
1071 /* Read 'actions'. */
1072 SET_ARRAY_FIELD (event
, scope
, utp
, action_num
, actions
);
1073 /* Read 'step_actions'. */
1074 SET_ARRAY_FIELD (event
, scope
, utp
, step_action_num
,
1077 SET_STRING_FIELD(event
, scope
, utp
, at_string
);
1078 SET_STRING_FIELD(event
, scope
, utp
, cond_string
);
1080 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1085 /* This is the implementation of target_ops method to_open. Open CTF
1086 trace data, read trace status, trace state variables and tracepoint
1087 definitions from the first packet. Set the start position at the
1088 second packet which contains events on trace blocks. */
1091 ctf_open (const char *dirname
, int from_tty
)
1093 struct bt_ctf_event
*event
;
1095 const struct bt_definition
*scope
;
1096 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
1097 struct uploaded_tp
*uploaded_tps
= NULL
;
1100 error (_("No CTF directory specified."));
1102 ctf_open_dir (dirname
);
1104 target_preopen (from_tty
);
1106 /* Skip the first packet which about the trace status. The first
1107 event is "frame". */
1108 event
= bt_ctf_iter_read_event (ctf_iter
);
1109 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1110 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1111 if (event_id
!= CTF_EVENT_ID_FRAME
)
1112 error (_("Wrong event id of the first event"));
1113 /* The second event is "status". */
1114 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1115 event
= bt_ctf_iter_read_event (ctf_iter
);
1116 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1117 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1118 if (event_id
!= CTF_EVENT_ID_STATUS
)
1119 error (_("Wrong event id of the second event"));
1120 ctf_read_status (event
, current_trace_status ());
1122 ctf_read_tsv (&uploaded_tsvs
);
1124 ctf_read_tp (&uploaded_tps
);
1126 event
= bt_ctf_iter_read_event (ctf_iter
);
1127 /* EVENT can be NULL if we've already gone to the end of stream of
1131 scope
= bt_ctf_get_top_level_scope (event
,
1132 BT_STREAM_EVENT_HEADER
);
1133 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
,
1135 if (event_id
!= CTF_EVENT_ID_FRAME
)
1136 error (_("Wrong event id of the first event of the second packet"));
1139 start_pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1140 gdb_assert (start_pos
->type
== BT_SEEK_RESTORE
);
1142 trace_dirname
= xstrdup (dirname
);
1143 push_target (&ctf_ops
);
1145 inferior_appeared (current_inferior (), CTF_PID
);
1146 inferior_ptid
= pid_to_ptid (CTF_PID
);
1147 add_thread_silent (inferior_ptid
);
1149 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
1150 merge_uploaded_tracepoints (&uploaded_tps
);
1152 post_create_inferior (&ctf_ops
, from_tty
);
1155 /* This is the implementation of target_ops method to_close. Destroy
1156 CTF iterator and context. */
1159 ctf_close (struct target_ops
*self
)
1164 xfree (trace_dirname
);
1165 trace_dirname
= NULL
;
1167 pid
= ptid_get_pid (inferior_ptid
);
1168 inferior_ptid
= null_ptid
; /* Avoid confusion from thread stuff. */
1169 exit_inferior_silent (pid
);
1171 trace_reset_local_state ();
1174 /* This is the implementation of target_ops method to_files_info.
1175 Print the directory name of CTF trace data. */
1178 ctf_files_info (struct target_ops
*t
)
1180 printf_filtered ("\t`%s'\n", trace_dirname
);
1183 /* This is the implementation of target_ops method to_fetch_registers.
1184 Iterate over events whose name is "register" in current frame,
1185 extract contents from events, and set REGCACHE with the contents.
1186 If no matched events are found, mark registers unavailable. */
1189 ctf_fetch_registers (struct target_ops
*ops
,
1190 struct regcache
*regcache
, int regno
)
1192 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1193 struct bt_ctf_event
*event
= NULL
;
1194 struct bt_iter_pos
*pos
;
1196 /* An uninitialized reg size says we're not going to be
1197 successful at getting register blocks. */
1198 if (trace_regblock_size
== 0)
1201 gdb_assert (ctf_iter
!= NULL
);
1202 /* Save the current position. */
1203 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1204 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1209 struct bt_ctf_event
*event1
;
1211 event1
= bt_ctf_iter_read_event (ctf_iter
);
1213 name
= bt_ctf_event_name (event1
);
1215 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1217 else if (strcmp (name
, "register") == 0)
1223 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1227 /* Restore the position. */
1228 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1232 int offset
, regsize
, regn
;
1233 const struct bt_definition
*scope
1234 = bt_ctf_get_top_level_scope (event
,
1236 const struct bt_definition
*array
1237 = bt_ctf_get_field (event
, scope
, "contents");
1238 gdb_byte
*regs
= (gdb_byte
*) bt_ctf_get_char_array (array
);
1240 /* Assume the block is laid out in GDB register number order,
1241 each register with the size that it has in GDB. */
1243 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1245 regsize
= register_size (gdbarch
, regn
);
1246 /* Make sure we stay within block bounds. */
1247 if (offset
+ regsize
>= trace_regblock_size
)
1249 if (regcache_register_status (regcache
, regn
) == REG_UNKNOWN
)
1253 regcache_raw_supply (regcache
, regno
, regs
+ offset
);
1256 else if (regno
== -1)
1258 regcache_raw_supply (regcache
, regn
, regs
+ offset
);
1265 tracefile_fetch_registers (regcache
, regno
);
1268 /* This is the implementation of target_ops method to_xfer_partial.
1269 Iterate over events whose name is "memory" in
1270 current frame, extract the address and length from events. If
1271 OFFSET is within the range, read the contents from events to
1274 static enum target_xfer_status
1275 ctf_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1276 const char *annex
, gdb_byte
*readbuf
,
1277 const gdb_byte
*writebuf
, ULONGEST offset
,
1278 ULONGEST len
, ULONGEST
*xfered_len
)
1280 /* We're only doing regular memory for now. */
1281 if (object
!= TARGET_OBJECT_MEMORY
)
1282 return TARGET_XFER_E_IO
;
1284 if (readbuf
== NULL
)
1285 error (_("ctf_xfer_partial: trace file is read-only"));
1287 if (get_traceframe_number () != -1)
1289 struct bt_iter_pos
*pos
;
1291 enum target_xfer_status res
;
1292 /* Records the lowest available address of all blocks that
1293 intersects the requested range. */
1294 ULONGEST low_addr_available
= 0;
1296 gdb_assert (ctf_iter
!= NULL
);
1297 /* Save the current position. */
1298 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1299 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1301 /* Iterate through the traceframe's blocks, looking for
1308 enum bfd_endian byte_order
1309 = gdbarch_byte_order (target_gdbarch ());
1310 const struct bt_definition
*scope
;
1311 const struct bt_definition
*def
;
1312 struct bt_ctf_event
*event
1313 = bt_ctf_iter_read_event (ctf_iter
);
1314 const char *name
= bt_ctf_event_name (event
);
1316 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1318 else if (strcmp (name
, "memory") != 0)
1320 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1326 scope
= bt_ctf_get_top_level_scope (event
,
1329 def
= bt_ctf_get_field (event
, scope
, "address");
1330 maddr
= bt_ctf_get_uint64 (def
);
1331 def
= bt_ctf_get_field (event
, scope
, "length");
1332 mlen
= (uint16_t) bt_ctf_get_uint64 (def
);
1334 /* If the block includes the first part of the desired
1335 range, return as much it has; GDB will re-request the
1336 remainder, which might be in a different block of this
1338 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
1340 const struct bt_definition
*array
1341 = bt_ctf_get_field (event
, scope
, "contents");
1342 const struct bt_declaration
*decl
1343 = bt_ctf_get_decl_from_def (array
);
1347 contents
= (gdb_byte
*) xmalloc (mlen
);
1349 for (k
= 0; k
< mlen
; k
++)
1351 const struct bt_definition
*element
1352 = bt_ctf_get_index (event
, array
, k
);
1354 contents
[k
] = (gdb_byte
) bt_ctf_get_uint64 (element
);
1357 amt
= (maddr
+ mlen
) - offset
;
1361 memcpy (readbuf
, &contents
[offset
- maddr
], amt
);
1365 /* Restore the position. */
1366 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1369 return TARGET_XFER_EOF
;
1373 return TARGET_XFER_OK
;
1377 if (offset
< maddr
&& maddr
< (offset
+ len
))
1378 if (low_addr_available
== 0 || low_addr_available
> maddr
)
1379 low_addr_available
= maddr
;
1381 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1385 /* Restore the position. */
1386 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1388 /* Requested memory is unavailable in the context of traceframes,
1389 and this address falls within a read-only section, fallback
1390 to reading from executable, up to LOW_ADDR_AVAILABLE */
1391 if (offset
< low_addr_available
)
1392 len
= min (len
, low_addr_available
- offset
);
1393 res
= exec_read_partial_read_only (readbuf
, offset
, len
, xfered_len
);
1395 if (res
== TARGET_XFER_OK
)
1396 return TARGET_XFER_OK
;
1399 /* No use trying further, we know some memory starting
1400 at MEMADDR isn't available. */
1402 return TARGET_XFER_UNAVAILABLE
;
1407 /* Fallback to reading from read-only sections. */
1408 return section_table_read_available_memory (readbuf
, offset
, len
, xfered_len
);
1412 /* This is the implementation of target_ops method
1413 to_get_trace_state_variable_value.
1414 Iterate over events whose name is "tsv" in current frame. When the
1415 trace variable is found, set the value of it to *VAL and return
1416 true, otherwise return false. */
1419 ctf_get_trace_state_variable_value (struct target_ops
*self
,
1420 int tsvnum
, LONGEST
*val
)
1422 struct bt_iter_pos
*pos
;
1425 gdb_assert (ctf_iter
!= NULL
);
1426 /* Save the current position. */
1427 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1428 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1430 /* Iterate through the traceframe's blocks, looking for 'V'
1434 struct bt_ctf_event
*event
1435 = bt_ctf_iter_read_event (ctf_iter
);
1436 const char *name
= bt_ctf_event_name (event
);
1438 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1440 else if (strcmp (name
, "tsv") == 0)
1442 const struct bt_definition
*scope
;
1443 const struct bt_definition
*def
;
1445 scope
= bt_ctf_get_top_level_scope (event
,
1448 def
= bt_ctf_get_field (event
, scope
, "num");
1449 if (tsvnum
== (int32_t) bt_ctf_get_uint64 (def
))
1451 def
= bt_ctf_get_field (event
, scope
, "val");
1452 *val
= bt_ctf_get_uint64 (def
);
1458 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1462 /* Restore the position. */
1463 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1468 /* Return the tracepoint number in "frame" event. */
1471 ctf_get_tpnum_from_frame_event (struct bt_ctf_event
*event
)
1473 /* The packet context of events has a field "tpnum". */
1474 const struct bt_definition
*scope
1475 = bt_ctf_get_top_level_scope (event
, BT_STREAM_PACKET_CONTEXT
);
1477 = bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "tpnum"));
1482 /* Return the address at which the current frame was collected. */
1485 ctf_get_traceframe_address (void)
1487 struct bt_ctf_event
*event
= NULL
;
1488 struct bt_iter_pos
*pos
;
1491 gdb_assert (ctf_iter
!= NULL
);
1492 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1493 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1498 struct bt_ctf_event
*event1
;
1500 event1
= bt_ctf_iter_read_event (ctf_iter
);
1502 name
= bt_ctf_event_name (event1
);
1506 else if (strcmp (name
, "frame") == 0)
1512 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1518 int tpnum
= ctf_get_tpnum_from_frame_event (event
);
1519 struct tracepoint
*tp
1520 = get_tracepoint_by_number_on_target (tpnum
);
1522 if (tp
&& tp
->base
.loc
)
1523 addr
= tp
->base
.loc
->address
;
1526 /* Restore the position. */
1527 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1532 /* This is the implementation of target_ops method to_trace_find.
1533 Iterate the events whose name is "frame", extract the tracepoint
1534 number in it. Return traceframe number when matched. */
1537 ctf_trace_find (struct target_ops
*self
, enum trace_find_type type
, int num
,
1538 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
1543 struct bt_iter_pos pos
;
1552 gdb_assert (ctf_iter
!= NULL
);
1553 /* Set iterator back to the start. */
1554 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), start_pos
);
1559 struct bt_ctf_event
*event
;
1562 event
= bt_ctf_iter_read_event (ctf_iter
);
1564 name
= bt_ctf_event_name (event
);
1566 if (event
== NULL
|| name
== NULL
)
1569 if (strcmp (name
, "frame") == 0)
1573 if (type
== tfind_number
)
1575 /* Looking for a specific trace frame. */
1581 /* Start from the _next_ trace frame. */
1582 if (tfnum
> get_traceframe_number ())
1588 struct tracepoint
*tp
= get_tracepoint (num
);
1591 && (tp
->number_on_target
1592 == ctf_get_tpnum_from_frame_event (event
)))
1597 tfaddr
= ctf_get_traceframe_address ();
1598 if (tfaddr
== addr1
)
1602 tfaddr
= ctf_get_traceframe_address ();
1603 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
1607 tfaddr
= ctf_get_traceframe_address ();
1608 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
1612 internal_error (__FILE__
, __LINE__
, _("unknown tfind type"));
1619 *tpp
= ctf_get_tpnum_from_frame_event (event
);
1621 /* Skip the event "frame". */
1622 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1629 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1636 /* This is the implementation of target_ops method to_traceframe_info.
1637 Iterate the events whose name is "memory", in current
1638 frame, extract memory range information, and return them in
1641 static struct traceframe_info
*
1642 ctf_traceframe_info (struct target_ops
*self
)
1644 struct traceframe_info
*info
= XCNEW (struct traceframe_info
);
1646 struct bt_iter_pos
*pos
;
1648 gdb_assert (ctf_iter
!= NULL
);
1649 /* Save the current position. */
1650 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1651 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1655 struct bt_ctf_event
*event
1656 = bt_ctf_iter_read_event (ctf_iter
);
1658 name
= bt_ctf_event_name (event
);
1660 if (name
== NULL
|| strcmp (name
, "register") == 0
1661 || strcmp (name
, "frame") == 0)
1663 else if (strcmp (name
, "memory") == 0)
1665 const struct bt_definition
*scope
1666 = bt_ctf_get_top_level_scope (event
,
1668 const struct bt_definition
*def
;
1669 struct mem_range
*r
;
1671 r
= VEC_safe_push (mem_range_s
, info
->memory
, NULL
);
1672 def
= bt_ctf_get_field (event
, scope
, "address");
1673 r
->start
= bt_ctf_get_uint64 (def
);
1675 def
= bt_ctf_get_field (event
, scope
, "length");
1676 r
->length
= (uint16_t) bt_ctf_get_uint64 (def
);
1678 else if (strcmp (name
, "tsv") == 0)
1681 const struct bt_definition
*scope
1682 = bt_ctf_get_top_level_scope (event
,
1684 const struct bt_definition
*def
;
1686 def
= bt_ctf_get_field (event
, scope
, "num");
1687 vnum
= (int) bt_ctf_get_int64 (def
);
1688 VEC_safe_push (int, info
->tvars
, vnum
);
1692 warning (_("Unhandled trace block type (%s) "
1693 "while building trace frame info."),
1697 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1700 while (name
!= NULL
&& strcmp (name
, "frame") != 0);
1702 /* Restore the position. */
1703 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1711 memset (&ctf_ops
, 0, sizeof (ctf_ops
));
1713 init_tracefile_ops (&ctf_ops
);
1714 ctf_ops
.to_shortname
= "ctf";
1715 ctf_ops
.to_longname
= "CTF file";
1716 ctf_ops
.to_doc
= "Use a CTF directory as a target.\n\
1717 Specify the filename of the CTF directory.";
1718 ctf_ops
.to_open
= ctf_open
;
1719 ctf_ops
.to_close
= ctf_close
;
1720 ctf_ops
.to_fetch_registers
= ctf_fetch_registers
;
1721 ctf_ops
.to_xfer_partial
= ctf_xfer_partial
;
1722 ctf_ops
.to_files_info
= ctf_files_info
;
1723 ctf_ops
.to_trace_find
= ctf_trace_find
;
1724 ctf_ops
.to_get_trace_state_variable_value
1725 = ctf_get_trace_state_variable_value
;
1726 ctf_ops
.to_traceframe_info
= ctf_traceframe_info
;
1731 /* -Wmissing-prototypes */
1733 extern initialize_file_ftype _initialize_ctf
;
1735 /* module initialization */
1738 _initialize_ctf (void)
1740 #if HAVE_LIBBABELTRACE
1743 add_target_with_completer (&ctf_ops
, filename_completer
);