3 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4 Contributed by Hui Zhu <hui_zhu@mentor.com>
5 Contributed by Yao Qi <yao@codesourcery.com>
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
)
315 struct cleanup
*old_chain
;
316 struct ctf_trace_file_writer
*writer
317 = (struct ctf_trace_file_writer
*) self
;
319 mode_t hmode
= S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
;
321 /* Create DIRNAME. */
322 if (mkdir (dirname
, hmode
) && errno
!= EEXIST
)
323 error (_("Unable to open directory '%s' for saving trace data (%s)"),
324 dirname
, safe_strerror (errno
));
326 memset (&writer
->tcs
, '\0', sizeof (writer
->tcs
));
328 file_name
= xstrprintf ("%s/%s", dirname
, CTF_METADATA_NAME
);
329 old_chain
= make_cleanup (xfree
, file_name
);
331 writer
->tcs
.metadata_fd
= fopen (file_name
, "w");
332 if (writer
->tcs
.metadata_fd
== NULL
)
333 error (_("Unable to open file '%s' for saving trace data (%s)"),
334 file_name
, safe_strerror (errno
));
335 do_cleanups (old_chain
);
337 ctf_save_metadata_header (&writer
->tcs
);
339 file_name
= xstrprintf ("%s/%s", dirname
, CTF_DATASTREAM_NAME
);
340 old_chain
= make_cleanup (xfree
, file_name
);
341 writer
->tcs
.datastream_fd
= fopen (file_name
, "w");
342 if (writer
->tcs
.datastream_fd
== NULL
)
343 error (_("Unable to open file '%s' for saving trace data (%s)"),
344 file_name
, safe_strerror (errno
));
345 do_cleanups (old_chain
);
348 /* This is the implementation of trace_file_write_ops method
349 write_header. Write the types of events on trace variable and
353 ctf_write_header (struct trace_file_writer
*self
)
355 struct ctf_trace_file_writer
*writer
356 = (struct ctf_trace_file_writer
*) self
;
359 ctf_save_write_metadata (&writer
->tcs
, "\n");
360 ctf_save_write_metadata (&writer
->tcs
,
361 "event {\n\tname = \"memory\";\n\tid = %u;\n"
362 "\tfields := struct { \n"
363 "\t\tuint64_t address;\n"
364 "\t\tuint16_t length;\n"
365 "\t\tuint8_t contents[length];\n"
367 "};\n", CTF_EVENT_ID_MEMORY
);
369 ctf_save_write_metadata (&writer
->tcs
, "\n");
370 ctf_save_write_metadata (&writer
->tcs
,
371 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
372 "\tfields := struct { \n"
373 "\t\tuint64_t val;\n"
374 "\t\tuint32_t num;\n"
376 "};\n", CTF_EVENT_ID_TSV
);
378 ctf_save_write_metadata (&writer
->tcs
, "\n");
379 ctf_save_write_metadata (&writer
->tcs
,
380 "event {\n\tname = \"frame\";\n\tid = %u;\n"
381 "\tfields := struct { \n"
383 "};\n", CTF_EVENT_ID_FRAME
);
385 ctf_save_write_metadata (&writer
->tcs
, "\n");
386 ctf_save_write_metadata (&writer
->tcs
,
387 "event {\n\tname = \"tsv_def\";\n"
388 "\tid = %u;\n\tfields := struct { \n"
389 "\t\tint64_t initial_value;\n"
390 "\t\tint32_t number;\n"
391 "\t\tint32_t builtin;\n"
394 "};\n", CTF_EVENT_ID_TSV_DEF
);
396 ctf_save_write_metadata (&writer
->tcs
, "\n");
397 ctf_save_write_metadata (&writer
->tcs
,
398 "event {\n\tname = \"tp_def\";\n"
399 "\tid = %u;\n\tfields := struct { \n"
400 "\t\tuint64_t addr;\n"
401 "\t\tuint64_t traceframe_usage;\n"
402 "\t\tint32_t number;\n"
403 "\t\tint32_t enabled;\n"
404 "\t\tint32_t step;\n"
405 "\t\tint32_t pass;\n"
406 "\t\tint32_t hit_count;\n"
407 "\t\tint32_t type;\n"
410 "\t\tuint32_t action_num;\n"
411 "\t\tchars actions[action_num];\n"
413 "\t\tuint32_t step_action_num;\n"
414 "\t\tchars step_actions[step_action_num];\n"
416 "\t\tchars at_string;\n"
417 "\t\tchars cond_string;\n"
419 "\t\tuint32_t cmd_num;\n"
420 "\t\tchars cmd_strings[cmd_num];\n"
422 "};\n", CTF_EVENT_ID_TP_DEF
);
424 gdb_assert (writer
->tcs
.content_size
== 0);
425 gdb_assert (writer
->tcs
.packet_start
== 0);
427 /* Create a new packet to contain this event. */
428 self
->ops
->frame_ops
->start (self
, 0);
431 /* This is the implementation of trace_file_write_ops method
432 write_regblock_type. Write the type of register event in
436 ctf_write_regblock_type (struct trace_file_writer
*self
, int size
)
438 struct ctf_trace_file_writer
*writer
439 = (struct ctf_trace_file_writer
*) self
;
441 ctf_save_write_metadata (&writer
->tcs
, "\n");
443 ctf_save_write_metadata (&writer
->tcs
,
444 "event {\n\tname = \"register\";\n\tid = %u;\n"
445 "\tfields := struct { \n"
446 "\t\tascii contents[%d];\n"
449 CTF_EVENT_ID_REGISTER
, size
);
452 /* This is the implementation of trace_file_write_ops method
456 ctf_write_status (struct trace_file_writer
*self
,
457 struct trace_status
*ts
)
459 struct ctf_trace_file_writer
*writer
460 = (struct ctf_trace_file_writer
*) self
;
464 ctf_save_write_metadata (&writer
->tcs
, "\n");
465 ctf_save_write_metadata (&writer
->tcs
,
466 "event {\n\tname = \"status\";\n\tid = %u;\n"
467 "\tfields := struct { \n"
468 "\t\tint32_t stop_reason;\n"
469 "\t\tint32_t stopping_tracepoint;\n"
470 "\t\tint32_t traceframe_count;\n"
471 "\t\tint32_t traceframes_created;\n"
472 "\t\tint32_t buffer_free;\n"
473 "\t\tint32_t buffer_size;\n"
474 "\t\tint32_t disconnected_tracing;\n"
475 "\t\tint32_t circular_buffer;\n"
478 CTF_EVENT_ID_STATUS
);
480 id
= CTF_EVENT_ID_STATUS
;
482 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
484 ctf_save_write_int32 (&writer
->tcs
, ts
->stop_reason
);
485 ctf_save_write_int32 (&writer
->tcs
, ts
->stopping_tracepoint
);
486 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframe_count
);
487 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframes_created
);
488 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_free
);
489 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_size
);
490 ctf_save_write_int32 (&writer
->tcs
, ts
->disconnected_tracing
);
491 ctf_save_write_int32 (&writer
->tcs
, ts
->circular_buffer
);
494 /* This is the implementation of trace_file_write_ops method
495 write_uploaded_tsv. */
498 ctf_write_uploaded_tsv (struct trace_file_writer
*self
,
499 struct uploaded_tsv
*tsv
)
501 struct ctf_trace_file_writer
*writer
502 = (struct ctf_trace_file_writer
*) self
;
506 const gdb_byte zero
= 0;
509 int32
= CTF_EVENT_ID_TSV_DEF
;
510 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
513 int64
= tsv
->initial_value
;
514 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
517 ctf_save_write_int32 (&writer
->tcs
, tsv
->number
);
520 ctf_save_write_int32 (&writer
->tcs
, tsv
->builtin
);
523 if (tsv
->name
!= NULL
)
524 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tsv
->name
,
526 ctf_save_write (&writer
->tcs
, &zero
, 1);
529 /* This is the implementation of trace_file_write_ops method
530 write_uploaded_tp. */
533 ctf_write_uploaded_tp (struct trace_file_writer
*self
,
534 struct uploaded_tp
*tp
)
536 struct ctf_trace_file_writer
*writer
537 = (struct ctf_trace_file_writer
*) self
;
541 const gdb_byte zero
= 0;
546 int32
= CTF_EVENT_ID_TP_DEF
;
547 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
551 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
553 /* traceframe_usage */
554 int64
= tp
->traceframe_usage
;
555 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
558 ctf_save_write_int32 (&writer
->tcs
, tp
->number
);
561 ctf_save_write_int32 (&writer
->tcs
, tp
->enabled
);
564 ctf_save_write_int32 (&writer
->tcs
, tp
->step
);
567 ctf_save_write_int32 (&writer
->tcs
, tp
->pass
);
570 ctf_save_write_int32 (&writer
->tcs
, tp
->hit_count
);
573 ctf_save_write_int32 (&writer
->tcs
, tp
->type
);
576 if (tp
->cond
!= NULL
)
577 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond
, strlen (tp
->cond
));
578 ctf_save_write (&writer
->tcs
, &zero
, 1);
581 u32
= VEC_length (char_ptr
, tp
->actions
);
582 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
583 for (a
= 0; VEC_iterate (char_ptr
, tp
->actions
, a
, act
); ++a
)
584 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
587 u32
= VEC_length (char_ptr
, tp
->step_actions
);
588 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
589 for (a
= 0; VEC_iterate (char_ptr
, tp
->step_actions
, a
, act
); ++a
)
590 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
593 if (tp
->at_string
!= NULL
)
594 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->at_string
,
595 strlen (tp
->at_string
));
596 ctf_save_write (&writer
->tcs
, &zero
, 1);
599 if (tp
->cond_string
!= NULL
)
600 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond_string
,
601 strlen (tp
->cond_string
));
602 ctf_save_write (&writer
->tcs
, &zero
, 1);
605 u32
= VEC_length (char_ptr
, tp
->cmd_strings
);
606 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
607 for (a
= 0; VEC_iterate (char_ptr
, tp
->cmd_strings
, a
, act
); ++a
)
608 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
612 /* This is the implementation of trace_file_write_ops method
616 ctf_write_tdesc (struct trace_file_writer
*self
)
618 /* Nothing so far. */
621 /* This is the implementation of trace_file_write_ops method
622 write_definition_end. */
625 ctf_write_definition_end (struct trace_file_writer
*self
)
627 struct ctf_trace_file_writer
*writer
628 = (struct ctf_trace_file_writer
*) self
;
630 self
->ops
->frame_ops
->end (self
);
633 /* This is the implementation of trace_file_write_ops method
637 ctf_end (struct trace_file_writer
*self
)
639 struct ctf_trace_file_writer
*writer
= (struct ctf_trace_file_writer
*) self
;
641 gdb_assert (writer
->tcs
.content_size
== 0);
644 /* This is the implementation of trace_frame_write_ops method
648 ctf_write_frame_start (struct trace_file_writer
*self
, uint16_t tpnum
)
650 struct ctf_trace_file_writer
*writer
651 = (struct ctf_trace_file_writer
*) self
;
652 uint32_t id
= CTF_EVENT_ID_FRAME
;
655 /* Step 1: Write packet context. */
658 ctf_save_write_uint32 (&writer
->tcs
, u32
);
659 /* content_size and packet_size.. We still don't know the value,
661 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
662 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
663 /* Tracepoint number. */
664 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &tpnum
, 2);
666 /* Step 2: Write event "frame". */
668 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
671 /* This is the implementation of trace_frame_write_ops method
675 ctf_write_frame_r_block (struct trace_file_writer
*self
,
676 gdb_byte
*buf
, int32_t size
)
678 struct ctf_trace_file_writer
*writer
679 = (struct ctf_trace_file_writer
*) self
;
680 uint32_t id
= CTF_EVENT_ID_REGISTER
;
683 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
685 /* array contents. */
686 ctf_save_align_write (&writer
->tcs
, buf
, size
, 1);
689 /* This is the implementation of trace_frame_write_ops method
690 write_m_block_header. */
693 ctf_write_frame_m_block_header (struct trace_file_writer
*self
,
694 uint64_t addr
, uint16_t length
)
696 struct ctf_trace_file_writer
*writer
697 = (struct ctf_trace_file_writer
*) self
;
698 uint32_t event_id
= CTF_EVENT_ID_MEMORY
;
701 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &event_id
, 4, 4);
704 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &addr
, 8, 8);
707 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &length
, 2, 2);
710 /* This is the implementation of trace_frame_write_ops method
711 write_m_block_memory. */
714 ctf_write_frame_m_block_memory (struct trace_file_writer
*self
,
715 gdb_byte
*buf
, uint16_t length
)
717 struct ctf_trace_file_writer
*writer
718 = (struct ctf_trace_file_writer
*) self
;
721 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) buf
, length
, 1);
724 /* This is the implementation of trace_frame_write_ops method
728 ctf_write_frame_v_block (struct trace_file_writer
*self
,
729 int32_t num
, uint64_t val
)
731 struct ctf_trace_file_writer
*writer
732 = (struct ctf_trace_file_writer
*) self
;
733 uint32_t id
= CTF_EVENT_ID_TSV
;
736 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
739 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &val
, 8, 8);
741 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &num
, 4, 4);
744 /* This is the implementation of trace_frame_write_ops method
748 ctf_write_frame_end (struct trace_file_writer
*self
)
750 struct ctf_trace_file_writer
*writer
751 = (struct ctf_trace_file_writer
*) self
;
755 /* Write the content size to packet header. */
756 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ 4,
758 u32
= writer
->tcs
.content_size
* TARGET_CHAR_BIT
;
760 t
= writer
->tcs
.content_size
;
761 ctf_save_write_uint32 (&writer
->tcs
, u32
);
763 /* Write the packet size. */
764 u32
+= 4 * TARGET_CHAR_BIT
;
765 ctf_save_write_uint32 (&writer
->tcs
, u32
);
767 writer
->tcs
.content_size
= t
;
769 /* Write zero at the end of the packet. */
770 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ t
,
773 ctf_save_write_uint32 (&writer
->tcs
, u32
);
774 writer
->tcs
.content_size
= t
;
776 ctf_save_next_packet (&writer
->tcs
);
779 /* Operations to write various types of trace frames into CTF
782 static const struct trace_frame_write_ops ctf_write_frame_ops
=
784 ctf_write_frame_start
,
785 ctf_write_frame_r_block
,
786 ctf_write_frame_m_block_header
,
787 ctf_write_frame_m_block_memory
,
788 ctf_write_frame_v_block
,
792 /* Operations to write trace buffers into CTF format. */
794 static const struct trace_file_write_ops ctf_write_ops
=
800 ctf_write_regblock_type
,
802 ctf_write_uploaded_tsv
,
803 ctf_write_uploaded_tp
,
805 ctf_write_definition_end
,
807 &ctf_write_frame_ops
,
811 /* Return a trace writer for CTF format. */
813 struct trace_file_writer
*
814 ctf_trace_file_writer_new (void)
816 struct ctf_trace_file_writer
*writer
= XNEW (struct ctf_trace_file_writer
);
818 writer
->base
.ops
= &ctf_write_ops
;
820 return (struct trace_file_writer
*) writer
;
823 #if HAVE_LIBBABELTRACE
824 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
825 iterator to iterate over each event in CTF data and APIs to get
826 details of event and packet, so it is very convenient to use
827 libbabeltrace to access events in CTF. */
829 #include <babeltrace/babeltrace.h>
830 #include <babeltrace/ctf/events.h>
831 #include <babeltrace/ctf/iterator.h>
833 /* The struct pointer for current CTF directory. */
834 static int handle_id
= -1;
835 static struct bt_context
*ctx
= NULL
;
836 static struct bt_ctf_iter
*ctf_iter
= NULL
;
837 /* The position of the first packet containing trace frame. */
838 static struct bt_iter_pos
*start_pos
;
840 /* The name of CTF directory. */
841 static char *trace_dirname
;
843 static struct target_ops ctf_ops
;
845 /* Destroy ctf iterator and context. */
850 if (ctf_iter
!= NULL
)
852 bt_ctf_iter_destroy (ctf_iter
);
857 bt_context_put (ctx
);
862 /* Open CTF trace data in DIRNAME. */
865 ctf_open_dir (const char *dirname
)
867 struct bt_iter_pos begin_pos
;
868 struct bt_iter_pos
*pos
;
869 unsigned int count
, i
;
870 struct bt_ctf_event_decl
* const *list
;
872 ctx
= bt_context_create ();
874 error (_("Unable to create bt_context"));
875 handle_id
= bt_context_add_trace (ctx
, dirname
, "ctf", NULL
, NULL
, NULL
);
879 error (_("Unable to use libbabeltrace on directory \"%s\""),
883 begin_pos
.type
= BT_SEEK_BEGIN
;
884 ctf_iter
= bt_ctf_iter_create (ctx
, &begin_pos
, NULL
);
885 if (ctf_iter
== NULL
)
888 error (_("Unable to create bt_iterator"));
891 /* Look for the declaration of register block. Get the length of
892 array "contents" to set trace_regblock_size. */
894 bt_ctf_get_event_decl_list (handle_id
, ctx
, &list
, &count
);
895 for (i
= 0; i
< count
; i
++)
896 if (strcmp ("register", bt_ctf_get_decl_event_name (list
[i
])) == 0)
899 const struct bt_ctf_field_decl
* const *field_list
;
900 const struct bt_declaration
*decl
;
902 bt_ctf_get_decl_fields (list
[i
], BT_EVENT_FIELDS
, &field_list
,
905 gdb_assert (count
== 1);
906 gdb_assert (0 == strcmp ("contents",
907 bt_ctf_get_decl_field_name (field_list
[0])));
908 decl
= bt_ctf_get_decl_from_field_decl (field_list
[0]);
909 trace_regblock_size
= bt_ctf_get_array_len (decl
);
915 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
916 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
920 #define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \
921 (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
926 /* EVENT is the "status" event and TS is filled in. */
929 ctf_read_status (struct bt_ctf_event
*event
, struct trace_status
*ts
)
931 const struct bt_definition
*scope
932 = bt_ctf_get_top_level_scope (event
, BT_EVENT_FIELDS
);
934 SET_ENUM_FIELD (event
, scope
, ts
, enum trace_stop_reason
, stop_reason
);
935 SET_INT32_FIELD (event
, scope
, ts
, stopping_tracepoint
);
936 SET_INT32_FIELD (event
, scope
, ts
, traceframe_count
);
937 SET_INT32_FIELD (event
, scope
, ts
, traceframes_created
);
938 SET_INT32_FIELD (event
, scope
, ts
, buffer_free
);
939 SET_INT32_FIELD (event
, scope
, ts
, buffer_size
);
940 SET_INT32_FIELD (event
, scope
, ts
, disconnected_tracing
);
941 SET_INT32_FIELD (event
, scope
, ts
, circular_buffer
);
943 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
946 /* Read the events "tsv_def" one by one, extract its contents and fill
947 in the list UPLOADED_TSVS. */
950 ctf_read_tsv (struct uploaded_tsv
**uploaded_tsvs
)
952 gdb_assert (ctf_iter
!= NULL
);
956 struct bt_ctf_event
*event
;
957 const struct bt_definition
*scope
;
958 const struct bt_definition
*def
;
960 struct uploaded_tsv
*utsv
= NULL
;
962 event
= bt_ctf_iter_read_event (ctf_iter
);
963 scope
= bt_ctf_get_top_level_scope (event
,
964 BT_STREAM_EVENT_HEADER
);
965 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
967 if (event_id
!= CTF_EVENT_ID_TSV_DEF
)
970 scope
= bt_ctf_get_top_level_scope (event
,
973 def
= bt_ctf_get_field (event
, scope
, "number");
974 utsv
= get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def
),
977 def
= bt_ctf_get_field (event
, scope
, "builtin");
978 utsv
->builtin
= (int32_t) bt_ctf_get_int64 (def
);
979 def
= bt_ctf_get_field (event
, scope
, "initial_value");
980 utsv
->initial_value
= bt_ctf_get_int64 (def
);
982 def
= bt_ctf_get_field (event
, scope
, "name");
983 utsv
->name
= xstrdup (bt_ctf_get_string (def
));
985 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
991 /* Read the value of element whose index is NUM from CTF and write it
992 to the corresponding VAR->ARRAY. */
994 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
998 const struct bt_definition *def; \
1000 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1003 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1004 for (i = 0; i < u32; i++) \
1006 const struct bt_definition *element \
1007 = bt_ctf_get_index ((EVENT), def, i); \
1009 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1010 xstrdup (bt_ctf_get_string (element))); \
1015 /* Read a string from CTF and set VAR->FIELD. If the length of string
1016 is zero, set VAR->FIELD to NULL. */
1018 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1021 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1025 if (strlen (p) > 0) \
1026 (VAR)->FIELD = xstrdup (p); \
1028 (VAR)->FIELD = NULL; \
1032 /* Read the events "tp_def" one by one, extract its contents and fill
1033 in the list UPLOADED_TPS. */
1036 ctf_read_tp (struct uploaded_tp
**uploaded_tps
)
1038 gdb_assert (ctf_iter
!= NULL
);
1042 struct bt_ctf_event
*event
;
1043 const struct bt_definition
*scope
;
1047 struct uploaded_tp
*utp
= NULL
;
1049 event
= bt_ctf_iter_read_event (ctf_iter
);
1050 scope
= bt_ctf_get_top_level_scope (event
,
1051 BT_STREAM_EVENT_HEADER
);
1052 u32
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1054 if (u32
!= CTF_EVENT_ID_TP_DEF
)
1057 scope
= bt_ctf_get_top_level_scope (event
,
1059 int32
= (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event
,
1062 u64
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1064 utp
= get_uploaded_tp (int32
, u64
, uploaded_tps
);
1066 SET_INT32_FIELD (event
, scope
, utp
, enabled
);
1067 SET_INT32_FIELD (event
, scope
, utp
, step
);
1068 SET_INT32_FIELD (event
, scope
, utp
, pass
);
1069 SET_INT32_FIELD (event
, scope
, utp
, hit_count
);
1070 SET_ENUM_FIELD (event
, scope
, utp
, enum bptype
, type
);
1072 /* Read 'cmd_strings'. */
1073 SET_ARRAY_FIELD (event
, scope
, utp
, cmd_num
, cmd_strings
);
1074 /* Read 'actions'. */
1075 SET_ARRAY_FIELD (event
, scope
, utp
, action_num
, actions
);
1076 /* Read 'step_actions'. */
1077 SET_ARRAY_FIELD (event
, scope
, utp
, step_action_num
,
1080 SET_STRING_FIELD(event
, scope
, utp
, at_string
);
1081 SET_STRING_FIELD(event
, scope
, utp
, cond_string
);
1083 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1088 /* This is the implementation of target_ops method to_open. Open CTF
1089 trace data, read trace status, trace state variables and tracepoint
1090 definitions from the first packet. Set the start position at the
1091 second packet which contains events on trace blocks. */
1094 ctf_open (const char *dirname
, int from_tty
)
1096 struct bt_ctf_event
*event
;
1098 const struct bt_definition
*scope
;
1099 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
1100 struct uploaded_tp
*uploaded_tps
= NULL
;
1103 error (_("No CTF directory specified."));
1105 ctf_open_dir (dirname
);
1107 target_preopen (from_tty
);
1109 /* Skip the first packet which about the trace status. The first
1110 event is "frame". */
1111 event
= bt_ctf_iter_read_event (ctf_iter
);
1112 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1113 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1114 if (event_id
!= CTF_EVENT_ID_FRAME
)
1115 error (_("Wrong event id of the first event"));
1116 /* The second event is "status". */
1117 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1118 event
= bt_ctf_iter_read_event (ctf_iter
);
1119 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1120 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1121 if (event_id
!= CTF_EVENT_ID_STATUS
)
1122 error (_("Wrong event id of the second event"));
1123 ctf_read_status (event
, current_trace_status ());
1125 ctf_read_tsv (&uploaded_tsvs
);
1127 ctf_read_tp (&uploaded_tps
);
1129 event
= bt_ctf_iter_read_event (ctf_iter
);
1130 /* EVENT can be NULL if we've already gone to the end of stream of
1134 scope
= bt_ctf_get_top_level_scope (event
,
1135 BT_STREAM_EVENT_HEADER
);
1136 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
,
1138 if (event_id
!= CTF_EVENT_ID_FRAME
)
1139 error (_("Wrong event id of the first event of the second packet"));
1142 start_pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1143 gdb_assert (start_pos
->type
== BT_SEEK_RESTORE
);
1145 trace_dirname
= xstrdup (dirname
);
1146 push_target (&ctf_ops
);
1148 inferior_appeared (current_inferior (), CTF_PID
);
1149 inferior_ptid
= pid_to_ptid (CTF_PID
);
1150 add_thread_silent (inferior_ptid
);
1152 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
1153 merge_uploaded_tracepoints (&uploaded_tps
);
1155 post_create_inferior (&ctf_ops
, from_tty
);
1158 /* This is the implementation of target_ops method to_close. Destroy
1159 CTF iterator and context. */
1162 ctf_close (struct target_ops
*self
)
1167 xfree (trace_dirname
);
1168 trace_dirname
= NULL
;
1170 pid
= ptid_get_pid (inferior_ptid
);
1171 inferior_ptid
= null_ptid
; /* Avoid confusion from thread stuff. */
1172 exit_inferior_silent (pid
);
1174 trace_reset_local_state ();
1177 /* This is the implementation of target_ops method to_files_info.
1178 Print the directory name of CTF trace data. */
1181 ctf_files_info (struct target_ops
*t
)
1183 printf_filtered ("\t`%s'\n", trace_dirname
);
1186 /* This is the implementation of target_ops method to_fetch_registers.
1187 Iterate over events whose name is "register" in current frame,
1188 extract contents from events, and set REGCACHE with the contents.
1189 If no matched events are found, mark registers unavailable. */
1192 ctf_fetch_registers (struct target_ops
*ops
,
1193 struct regcache
*regcache
, int regno
)
1195 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1196 struct bt_ctf_event
*event
= NULL
;
1197 struct bt_iter_pos
*pos
;
1199 /* An uninitialized reg size says we're not going to be
1200 successful at getting register blocks. */
1201 if (trace_regblock_size
== 0)
1204 gdb_assert (ctf_iter
!= NULL
);
1205 /* Save the current position. */
1206 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1207 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1212 struct bt_ctf_event
*event1
;
1214 event1
= bt_ctf_iter_read_event (ctf_iter
);
1216 name
= bt_ctf_event_name (event1
);
1218 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1220 else if (strcmp (name
, "register") == 0)
1226 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1230 /* Restore the position. */
1231 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1235 int offset
, regsize
, regn
;
1236 const struct bt_definition
*scope
1237 = bt_ctf_get_top_level_scope (event
,
1239 const struct bt_definition
*array
1240 = bt_ctf_get_field (event
, scope
, "contents");
1241 gdb_byte
*regs
= (gdb_byte
*) bt_ctf_get_char_array (array
);
1243 /* Assume the block is laid out in GDB register number order,
1244 each register with the size that it has in GDB. */
1246 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1248 regsize
= register_size (gdbarch
, regn
);
1249 /* Make sure we stay within block bounds. */
1250 if (offset
+ regsize
>= trace_regblock_size
)
1252 if (regcache_register_status (regcache
, regn
) == REG_UNKNOWN
)
1256 regcache_raw_supply (regcache
, regno
, regs
+ offset
);
1259 else if (regno
== -1)
1261 regcache_raw_supply (regcache
, regn
, regs
+ offset
);
1268 tracefile_fetch_registers (regcache
, regno
);
1271 /* This is the implementation of target_ops method to_xfer_partial.
1272 Iterate over events whose name is "memory" in
1273 current frame, extract the address and length from events. If
1274 OFFSET is within the range, read the contents from events to
1277 static enum target_xfer_status
1278 ctf_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1279 const char *annex
, gdb_byte
*readbuf
,
1280 const gdb_byte
*writebuf
, ULONGEST offset
,
1281 ULONGEST len
, ULONGEST
*xfered_len
)
1283 /* We're only doing regular memory for now. */
1284 if (object
!= TARGET_OBJECT_MEMORY
)
1285 return TARGET_XFER_E_IO
;
1287 if (readbuf
== NULL
)
1288 error (_("ctf_xfer_partial: trace file is read-only"));
1290 if (get_traceframe_number () != -1)
1292 struct bt_iter_pos
*pos
;
1294 enum target_xfer_status res
;
1295 /* Records the lowest available address of all blocks that
1296 intersects the requested range. */
1297 ULONGEST low_addr_available
= 0;
1299 gdb_assert (ctf_iter
!= NULL
);
1300 /* Save the current position. */
1301 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1302 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1304 /* Iterate through the traceframe's blocks, looking for
1311 enum bfd_endian byte_order
1312 = gdbarch_byte_order (target_gdbarch ());
1313 const struct bt_definition
*scope
;
1314 const struct bt_definition
*def
;
1315 struct bt_ctf_event
*event
1316 = bt_ctf_iter_read_event (ctf_iter
);
1317 const char *name
= bt_ctf_event_name (event
);
1319 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1321 else if (strcmp (name
, "memory") != 0)
1323 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1329 scope
= bt_ctf_get_top_level_scope (event
,
1332 def
= bt_ctf_get_field (event
, scope
, "address");
1333 maddr
= bt_ctf_get_uint64 (def
);
1334 def
= bt_ctf_get_field (event
, scope
, "length");
1335 mlen
= (uint16_t) bt_ctf_get_uint64 (def
);
1337 /* If the block includes the first part of the desired
1338 range, return as much it has; GDB will re-request the
1339 remainder, which might be in a different block of this
1341 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
1343 const struct bt_definition
*array
1344 = bt_ctf_get_field (event
, scope
, "contents");
1345 const struct bt_declaration
*decl
1346 = bt_ctf_get_decl_from_def (array
);
1350 contents
= (gdb_byte
*) xmalloc (mlen
);
1352 for (k
= 0; k
< mlen
; k
++)
1354 const struct bt_definition
*element
1355 = bt_ctf_get_index (event
, array
, k
);
1357 contents
[k
] = (gdb_byte
) bt_ctf_get_uint64 (element
);
1360 amt
= (maddr
+ mlen
) - offset
;
1364 memcpy (readbuf
, &contents
[offset
- maddr
], amt
);
1368 /* Restore the position. */
1369 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1372 return TARGET_XFER_EOF
;
1376 return TARGET_XFER_OK
;
1380 if (offset
< maddr
&& maddr
< (offset
+ len
))
1381 if (low_addr_available
== 0 || low_addr_available
> maddr
)
1382 low_addr_available
= maddr
;
1384 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1388 /* Restore the position. */
1389 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1391 /* Requested memory is unavailable in the context of traceframes,
1392 and this address falls within a read-only section, fallback
1393 to reading from executable, up to LOW_ADDR_AVAILABLE */
1394 if (offset
< low_addr_available
)
1395 len
= std::min (len
, low_addr_available
- offset
);
1396 res
= exec_read_partial_read_only (readbuf
, offset
, len
, xfered_len
);
1398 if (res
== TARGET_XFER_OK
)
1399 return TARGET_XFER_OK
;
1402 /* No use trying further, we know some memory starting
1403 at MEMADDR isn't available. */
1405 return TARGET_XFER_UNAVAILABLE
;
1410 /* Fallback to reading from read-only sections. */
1411 return section_table_read_available_memory (readbuf
, offset
, len
, xfered_len
);
1415 /* This is the implementation of target_ops method
1416 to_get_trace_state_variable_value.
1417 Iterate over events whose name is "tsv" in current frame. When the
1418 trace variable is found, set the value of it to *VAL and return
1419 true, otherwise return false. */
1422 ctf_get_trace_state_variable_value (struct target_ops
*self
,
1423 int tsvnum
, LONGEST
*val
)
1425 struct bt_iter_pos
*pos
;
1428 gdb_assert (ctf_iter
!= NULL
);
1429 /* Save the current position. */
1430 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1431 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1433 /* Iterate through the traceframe's blocks, looking for 'V'
1437 struct bt_ctf_event
*event
1438 = bt_ctf_iter_read_event (ctf_iter
);
1439 const char *name
= bt_ctf_event_name (event
);
1441 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1443 else if (strcmp (name
, "tsv") == 0)
1445 const struct bt_definition
*scope
;
1446 const struct bt_definition
*def
;
1448 scope
= bt_ctf_get_top_level_scope (event
,
1451 def
= bt_ctf_get_field (event
, scope
, "num");
1452 if (tsvnum
== (int32_t) bt_ctf_get_uint64 (def
))
1454 def
= bt_ctf_get_field (event
, scope
, "val");
1455 *val
= bt_ctf_get_uint64 (def
);
1461 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1465 /* Restore the position. */
1466 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1471 /* Return the tracepoint number in "frame" event. */
1474 ctf_get_tpnum_from_frame_event (struct bt_ctf_event
*event
)
1476 /* The packet context of events has a field "tpnum". */
1477 const struct bt_definition
*scope
1478 = bt_ctf_get_top_level_scope (event
, BT_STREAM_PACKET_CONTEXT
);
1480 = bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "tpnum"));
1485 /* Return the address at which the current frame was collected. */
1488 ctf_get_traceframe_address (void)
1490 struct bt_ctf_event
*event
= NULL
;
1491 struct bt_iter_pos
*pos
;
1494 gdb_assert (ctf_iter
!= NULL
);
1495 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1496 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1501 struct bt_ctf_event
*event1
;
1503 event1
= bt_ctf_iter_read_event (ctf_iter
);
1505 name
= bt_ctf_event_name (event1
);
1509 else if (strcmp (name
, "frame") == 0)
1515 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1521 int tpnum
= ctf_get_tpnum_from_frame_event (event
);
1522 struct tracepoint
*tp
1523 = get_tracepoint_by_number_on_target (tpnum
);
1526 addr
= tp
->loc
->address
;
1529 /* Restore the position. */
1530 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1535 /* This is the implementation of target_ops method to_trace_find.
1536 Iterate the events whose name is "frame", extract the tracepoint
1537 number in it. Return traceframe number when matched. */
1540 ctf_trace_find (struct target_ops
*self
, enum trace_find_type type
, int num
,
1541 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
1546 struct bt_iter_pos pos
;
1555 gdb_assert (ctf_iter
!= NULL
);
1556 /* Set iterator back to the start. */
1557 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), start_pos
);
1562 struct bt_ctf_event
*event
;
1565 event
= bt_ctf_iter_read_event (ctf_iter
);
1567 name
= bt_ctf_event_name (event
);
1569 if (event
== NULL
|| name
== NULL
)
1572 if (strcmp (name
, "frame") == 0)
1576 if (type
== tfind_number
)
1578 /* Looking for a specific trace frame. */
1584 /* Start from the _next_ trace frame. */
1585 if (tfnum
> get_traceframe_number ())
1591 struct tracepoint
*tp
= get_tracepoint (num
);
1594 && (tp
->number_on_target
1595 == ctf_get_tpnum_from_frame_event (event
)))
1600 tfaddr
= ctf_get_traceframe_address ();
1601 if (tfaddr
== addr1
)
1605 tfaddr
= ctf_get_traceframe_address ();
1606 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
1610 tfaddr
= ctf_get_traceframe_address ();
1611 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
1615 internal_error (__FILE__
, __LINE__
, _("unknown tfind type"));
1622 *tpp
= ctf_get_tpnum_from_frame_event (event
);
1624 /* Skip the event "frame". */
1625 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1632 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1639 /* This is the implementation of target_ops method to_traceframe_info.
1640 Iterate the events whose name is "memory", in current
1641 frame, extract memory range information, and return them in
1644 static struct traceframe_info
*
1645 ctf_traceframe_info (struct target_ops
*self
)
1647 struct traceframe_info
*info
= XCNEW (struct traceframe_info
);
1649 struct bt_iter_pos
*pos
;
1651 gdb_assert (ctf_iter
!= NULL
);
1652 /* Save the current position. */
1653 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1654 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1658 struct bt_ctf_event
*event
1659 = bt_ctf_iter_read_event (ctf_iter
);
1661 name
= bt_ctf_event_name (event
);
1663 if (name
== NULL
|| strcmp (name
, "register") == 0
1664 || strcmp (name
, "frame") == 0)
1666 else if (strcmp (name
, "memory") == 0)
1668 const struct bt_definition
*scope
1669 = bt_ctf_get_top_level_scope (event
,
1671 const struct bt_definition
*def
;
1672 struct mem_range
*r
;
1674 r
= VEC_safe_push (mem_range_s
, info
->memory
, NULL
);
1675 def
= bt_ctf_get_field (event
, scope
, "address");
1676 r
->start
= bt_ctf_get_uint64 (def
);
1678 def
= bt_ctf_get_field (event
, scope
, "length");
1679 r
->length
= (uint16_t) bt_ctf_get_uint64 (def
);
1681 else if (strcmp (name
, "tsv") == 0)
1684 const struct bt_definition
*scope
1685 = bt_ctf_get_top_level_scope (event
,
1687 const struct bt_definition
*def
;
1689 def
= bt_ctf_get_field (event
, scope
, "num");
1690 vnum
= (int) bt_ctf_get_uint64 (def
);
1691 VEC_safe_push (int, info
->tvars
, vnum
);
1695 warning (_("Unhandled trace block type (%s) "
1696 "while building trace frame info."),
1700 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1703 while (name
!= NULL
&& strcmp (name
, "frame") != 0);
1705 /* Restore the position. */
1706 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1714 memset (&ctf_ops
, 0, sizeof (ctf_ops
));
1716 init_tracefile_ops (&ctf_ops
);
1717 ctf_ops
.to_shortname
= "ctf";
1718 ctf_ops
.to_longname
= "CTF file";
1719 ctf_ops
.to_doc
= "Use a CTF directory as a target.\n\
1720 Specify the filename of the CTF directory.";
1721 ctf_ops
.to_open
= ctf_open
;
1722 ctf_ops
.to_close
= ctf_close
;
1723 ctf_ops
.to_fetch_registers
= ctf_fetch_registers
;
1724 ctf_ops
.to_xfer_partial
= ctf_xfer_partial
;
1725 ctf_ops
.to_files_info
= ctf_files_info
;
1726 ctf_ops
.to_trace_find
= ctf_trace_find
;
1727 ctf_ops
.to_get_trace_state_variable_value
1728 = ctf_get_trace_state_variable_value
;
1729 ctf_ops
.to_traceframe_info
= ctf_traceframe_info
;
1734 /* -Wmissing-prototypes */
1736 extern initialize_file_ftype _initialize_ctf
;
1738 /* module initialization */
1741 _initialize_ctf (void)
1743 #if HAVE_LIBBABELTRACE
1746 add_target_with_completer (&ctf_ops
, filename_completer
);