3 Copyright (C) 2012-2014 Free Software Foundation, Inc.
4 Contributed by Hui Zhu <hui_zhu@mentor.com>
5 Contributed by Yao Qi <yao@codesourcery.com>
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
, ...)
108 va_start (args
, format
);
109 if (vfprintf (handler
->metadata_fd
, format
, args
) < 0)
110 error (_("Unable to write metadata file (%s)"),
111 safe_strerror (errno
));
115 /* Write BUF of length SIZE to datastream file represented by
119 ctf_save_write (struct trace_write_handler
*handler
,
120 const gdb_byte
*buf
, size_t size
)
122 if (fwrite (buf
, size
, 1, handler
->datastream_fd
) != 1)
123 error (_("Unable to write file for saving trace data (%s)"),
124 safe_strerror (errno
));
126 handler
->content_size
+= size
;
131 /* Write a unsigned 32-bit integer to datastream file represented by
134 #define ctf_save_write_uint32(HANDLER, U32) \
135 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
137 /* Write a signed 32-bit integer to datastream file represented by
140 #define ctf_save_write_int32(HANDLER, INT32) \
141 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
143 /* Set datastream file position. Update HANDLER->content_size
144 if WHENCE is SEEK_CUR. */
147 ctf_save_fseek (struct trace_write_handler
*handler
, long offset
,
150 gdb_assert (whence
!= SEEK_END
);
151 gdb_assert (whence
!= SEEK_SET
152 || offset
<= handler
->content_size
+ handler
->packet_start
);
154 if (fseek (handler
->datastream_fd
, offset
, whence
))
155 error (_("Unable to seek file for saving trace data (%s)"),
156 safe_strerror (errno
));
158 if (whence
== SEEK_CUR
)
159 handler
->content_size
+= offset
;
164 /* Change the datastream file position to align on ALIGN_SIZE,
165 and write BUF to datastream file. The size of BUF is SIZE. */
168 ctf_save_align_write (struct trace_write_handler
*handler
,
170 size_t size
, size_t align_size
)
173 = (align_up (handler
->content_size
, align_size
)
174 - handler
->content_size
);
176 if (ctf_save_fseek (handler
, offset
, SEEK_CUR
))
179 if (ctf_save_write (handler
, buf
, size
))
185 /* Write events to next new packet. */
188 ctf_save_next_packet (struct trace_write_handler
*handler
)
190 handler
->packet_start
+= (handler
->content_size
+ 4);
191 ctf_save_fseek (handler
, handler
->packet_start
, SEEK_SET
);
192 handler
->content_size
= 0;
195 /* Write the CTF metadata header. */
198 ctf_save_metadata_header (struct trace_write_handler
*handler
)
200 const char metadata_fmt
[] =
204 " byte_order = %s;\n" /* be or le */
205 " packet.header := struct {\n"
211 " packet.context := struct {\n"
212 " uint32_t content_size;\n"
213 " uint32_t packet_size;\n"
216 " event.header := struct {\n"
221 ctf_save_write_metadata (handler
, "/* CTF %d.%d */\n",
222 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
);
223 ctf_save_write_metadata (handler
,
224 "typealias integer { size = 8; align = 8; "
225 "signed = false; encoding = ascii;}"
227 ctf_save_write_metadata (handler
,
228 "typealias integer { size = 8; align = 8; "
231 ctf_save_write_metadata (handler
,
232 "typealias integer { size = 16; align = 16;"
233 "signed = false; } := uint16_t;\n");
234 ctf_save_write_metadata (handler
,
235 "typealias integer { size = 32; align = 32;"
236 "signed = false; } := uint32_t;\n");
237 ctf_save_write_metadata (handler
,
238 "typealias integer { size = 64; align = 64;"
239 "signed = false; base = hex;}"
241 ctf_save_write_metadata (handler
,
242 "typealias integer { size = 32; align = 32;"
243 "signed = true; } := int32_t;\n");
244 ctf_save_write_metadata (handler
,
245 "typealias integer { size = 64; align = 64;"
246 "signed = true; } := int64_t;\n");
247 ctf_save_write_metadata (handler
,
248 "typealias string { encoding = ascii;"
250 ctf_save_write_metadata (handler
, "\n");
252 /* Get the byte order of the host and write CTF data in this byte
255 #define HOST_ENDIANNESS "be"
257 #define HOST_ENDIANNESS "le"
260 ctf_save_write_metadata (handler
, metadata_fmt
,
261 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
,
263 ctf_save_write_metadata (handler
, "\n");
266 /* CTF trace writer. */
268 struct ctf_trace_file_writer
270 struct trace_file_writer base
;
272 /* States related to writing CTF trace file. */
273 struct trace_write_handler tcs
;
276 /* This is the implementation of trace_file_write_ops method
280 ctf_dtor (struct trace_file_writer
*self
)
282 struct ctf_trace_file_writer
*writer
283 = (struct ctf_trace_file_writer
*) self
;
285 if (writer
->tcs
.metadata_fd
!= NULL
)
286 fclose (writer
->tcs
.metadata_fd
);
288 if (writer
->tcs
.datastream_fd
!= NULL
)
289 fclose (writer
->tcs
.datastream_fd
);
293 /* This is the implementation of trace_file_write_ops method
297 ctf_target_save (struct trace_file_writer
*self
,
300 /* Don't support save trace file to CTF format in the target. */
306 #define mkdir(pathname, mode) mkdir (pathname)
309 /* This is the implementation of trace_file_write_ops method
310 start. It creates the directory DIRNAME, metadata and datastream
314 ctf_start (struct trace_file_writer
*self
, const char *dirname
)
317 struct cleanup
*old_chain
;
318 struct ctf_trace_file_writer
*writer
319 = (struct ctf_trace_file_writer
*) self
;
321 mode_t hmode
= S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
;
323 /* Create DIRNAME. */
324 if (mkdir (dirname
, hmode
) && errno
!= EEXIST
)
325 error (_("Unable to open directory '%s' for saving trace data (%s)"),
326 dirname
, safe_strerror (errno
));
328 memset (&writer
->tcs
, '\0', sizeof (writer
->tcs
));
330 file_name
= xstrprintf ("%s/%s", dirname
, CTF_METADATA_NAME
);
331 old_chain
= make_cleanup (xfree
, file_name
);
333 writer
->tcs
.metadata_fd
= fopen (file_name
, "w");
334 if (writer
->tcs
.metadata_fd
== NULL
)
335 error (_("Unable to open file '%s' for saving trace data (%s)"),
336 file_name
, safe_strerror (errno
));
337 do_cleanups (old_chain
);
339 ctf_save_metadata_header (&writer
->tcs
);
341 file_name
= xstrprintf ("%s/%s", dirname
, CTF_DATASTREAM_NAME
);
342 old_chain
= make_cleanup (xfree
, file_name
);
343 writer
->tcs
.datastream_fd
= fopen (file_name
, "w");
344 if (writer
->tcs
.datastream_fd
== NULL
)
345 error (_("Unable to open file '%s' for saving trace data (%s)"),
346 file_name
, safe_strerror (errno
));
347 do_cleanups (old_chain
);
350 /* This is the implementation of trace_file_write_ops method
351 write_header. Write the types of events on trace variable and
355 ctf_write_header (struct trace_file_writer
*self
)
357 struct ctf_trace_file_writer
*writer
358 = (struct ctf_trace_file_writer
*) self
;
361 ctf_save_write_metadata (&writer
->tcs
, "\n");
362 ctf_save_write_metadata (&writer
->tcs
,
363 "event {\n\tname = \"memory\";\n\tid = %u;\n"
364 "\tfields := struct { \n"
365 "\t\tuint64_t address;\n"
366 "\t\tuint16_t length;\n"
367 "\t\tuint8_t contents[length];\n"
369 "};\n", CTF_EVENT_ID_MEMORY
);
371 ctf_save_write_metadata (&writer
->tcs
, "\n");
372 ctf_save_write_metadata (&writer
->tcs
,
373 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
374 "\tfields := struct { \n"
375 "\t\tuint64_t val;\n"
376 "\t\tuint32_t num;\n"
378 "};\n", CTF_EVENT_ID_TSV
);
380 ctf_save_write_metadata (&writer
->tcs
, "\n");
381 ctf_save_write_metadata (&writer
->tcs
,
382 "event {\n\tname = \"frame\";\n\tid = %u;\n"
383 "\tfields := struct { \n"
385 "};\n", CTF_EVENT_ID_FRAME
);
387 ctf_save_write_metadata (&writer
->tcs
, "\n");
388 ctf_save_write_metadata (&writer
->tcs
,
389 "event {\n\tname = \"tsv_def\";\n"
390 "\tid = %u;\n\tfields := struct { \n"
391 "\t\tint64_t initial_value;\n"
392 "\t\tint32_t number;\n"
393 "\t\tint32_t builtin;\n"
396 "};\n", CTF_EVENT_ID_TSV_DEF
);
398 ctf_save_write_metadata (&writer
->tcs
, "\n");
399 ctf_save_write_metadata (&writer
->tcs
,
400 "event {\n\tname = \"tp_def\";\n"
401 "\tid = %u;\n\tfields := struct { \n"
402 "\t\tuint64_t addr;\n"
403 "\t\tuint64_t traceframe_usage;\n"
404 "\t\tint32_t number;\n"
405 "\t\tint32_t enabled;\n"
406 "\t\tint32_t step;\n"
407 "\t\tint32_t pass;\n"
408 "\t\tint32_t hit_count;\n"
409 "\t\tint32_t type;\n"
412 "\t\tuint32_t action_num;\n"
413 "\t\tchars actions[action_num];\n"
415 "\t\tuint32_t step_action_num;\n"
416 "\t\tchars step_actions[step_action_num];\n"
418 "\t\tchars at_string;\n"
419 "\t\tchars cond_string;\n"
421 "\t\tuint32_t cmd_num;\n"
422 "\t\tchars cmd_strings[cmd_num];\n"
424 "};\n", CTF_EVENT_ID_TP_DEF
);
426 gdb_assert (writer
->tcs
.content_size
== 0);
427 gdb_assert (writer
->tcs
.packet_start
== 0);
429 /* Create a new packet to contain this event. */
430 self
->ops
->frame_ops
->start (self
, 0);
433 /* This is the implementation of trace_file_write_ops method
434 write_regblock_type. Write the type of register event in
438 ctf_write_regblock_type (struct trace_file_writer
*self
, int size
)
440 struct ctf_trace_file_writer
*writer
441 = (struct ctf_trace_file_writer
*) self
;
443 ctf_save_write_metadata (&writer
->tcs
, "\n");
445 ctf_save_write_metadata (&writer
->tcs
,
446 "event {\n\tname = \"register\";\n\tid = %u;\n"
447 "\tfields := struct { \n"
448 "\t\tascii contents[%d];\n"
451 CTF_EVENT_ID_REGISTER
, size
);
454 /* This is the implementation of trace_file_write_ops method
458 ctf_write_status (struct trace_file_writer
*self
,
459 struct trace_status
*ts
)
461 struct ctf_trace_file_writer
*writer
462 = (struct ctf_trace_file_writer
*) self
;
466 ctf_save_write_metadata (&writer
->tcs
, "\n");
467 ctf_save_write_metadata (&writer
->tcs
,
468 "event {\n\tname = \"status\";\n\tid = %u;\n"
469 "\tfields := struct { \n"
470 "\t\tint32_t stop_reason;\n"
471 "\t\tint32_t stopping_tracepoint;\n"
472 "\t\tint32_t traceframe_count;\n"
473 "\t\tint32_t traceframes_created;\n"
474 "\t\tint32_t buffer_free;\n"
475 "\t\tint32_t buffer_size;\n"
476 "\t\tint32_t disconnected_tracing;\n"
477 "\t\tint32_t circular_buffer;\n"
480 CTF_EVENT_ID_STATUS
);
482 id
= CTF_EVENT_ID_STATUS
;
484 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
486 ctf_save_write_int32 (&writer
->tcs
, ts
->stop_reason
);
487 ctf_save_write_int32 (&writer
->tcs
, ts
->stopping_tracepoint
);
488 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframe_count
);
489 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframes_created
);
490 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_free
);
491 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_size
);
492 ctf_save_write_int32 (&writer
->tcs
, ts
->disconnected_tracing
);
493 ctf_save_write_int32 (&writer
->tcs
, ts
->circular_buffer
);
496 /* This is the implementation of trace_file_write_ops method
497 write_uploaded_tsv. */
500 ctf_write_uploaded_tsv (struct trace_file_writer
*self
,
501 struct uploaded_tsv
*tsv
)
503 struct ctf_trace_file_writer
*writer
504 = (struct ctf_trace_file_writer
*) self
;
508 const gdb_byte zero
= 0;
511 int32
= CTF_EVENT_ID_TSV_DEF
;
512 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
515 int64
= tsv
->initial_value
;
516 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
519 ctf_save_write_int32 (&writer
->tcs
, tsv
->number
);
522 ctf_save_write_int32 (&writer
->tcs
, tsv
->builtin
);
525 if (tsv
->name
!= NULL
)
526 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tsv
->name
,
528 ctf_save_write (&writer
->tcs
, &zero
, 1);
531 /* This is the implementation of trace_file_write_ops method
532 write_uploaded_tp. */
535 ctf_write_uploaded_tp (struct trace_file_writer
*self
,
536 struct uploaded_tp
*tp
)
538 struct ctf_trace_file_writer
*writer
539 = (struct ctf_trace_file_writer
*) self
;
543 const gdb_byte zero
= 0;
548 int32
= CTF_EVENT_ID_TP_DEF
;
549 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
553 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
555 /* traceframe_usage */
556 int64
= tp
->traceframe_usage
;
557 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
560 ctf_save_write_int32 (&writer
->tcs
, tp
->number
);
563 ctf_save_write_int32 (&writer
->tcs
, tp
->enabled
);
566 ctf_save_write_int32 (&writer
->tcs
, tp
->step
);
569 ctf_save_write_int32 (&writer
->tcs
, tp
->pass
);
572 ctf_save_write_int32 (&writer
->tcs
, tp
->hit_count
);
575 ctf_save_write_int32 (&writer
->tcs
, tp
->type
);
578 if (tp
->cond
!= NULL
)
579 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond
, strlen (tp
->cond
));
580 ctf_save_write (&writer
->tcs
, &zero
, 1);
583 u32
= VEC_length (char_ptr
, tp
->actions
);
584 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
585 for (a
= 0; VEC_iterate (char_ptr
, tp
->actions
, a
, act
); ++a
)
586 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
589 u32
= VEC_length (char_ptr
, tp
->step_actions
);
590 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
591 for (a
= 0; VEC_iterate (char_ptr
, tp
->step_actions
, a
, act
); ++a
)
592 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
595 if (tp
->at_string
!= NULL
)
596 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->at_string
,
597 strlen (tp
->at_string
));
598 ctf_save_write (&writer
->tcs
, &zero
, 1);
601 if (tp
->cond_string
!= NULL
)
602 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond_string
,
603 strlen (tp
->cond_string
));
604 ctf_save_write (&writer
->tcs
, &zero
, 1);
607 u32
= VEC_length (char_ptr
, tp
->cmd_strings
);
608 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
609 for (a
= 0; VEC_iterate (char_ptr
, tp
->cmd_strings
, a
, act
); ++a
)
610 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
614 /* This is the implementation of trace_file_write_ops method
615 write_definition_end. */
618 ctf_write_definition_end (struct trace_file_writer
*self
)
620 struct ctf_trace_file_writer
*writer
621 = (struct ctf_trace_file_writer
*) self
;
623 self
->ops
->frame_ops
->end (self
);
626 /* The minimal file size of data stream. It is required by
629 #define CTF_FILE_MIN_SIZE 4096
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);
640 /* The babeltrace requires or assumes that the size of datastream
641 file is greater than 4096 bytes. If we don't generate enough
642 packets and events, create a fake packet which has zero event,
643 to use up the space. */
644 if (writer
->tcs
.packet_start
< CTF_FILE_MIN_SIZE
)
650 ctf_save_write_uint32 (&writer
->tcs
, u32
);
654 ctf_save_write_uint32 (&writer
->tcs
, u32
);
658 if (writer
->tcs
.packet_start
+ u32
< CTF_FILE_MIN_SIZE
)
659 u32
= CTF_FILE_MIN_SIZE
- writer
->tcs
.packet_start
;
661 u32
*= TARGET_CHAR_BIT
;
662 ctf_save_write_uint32 (&writer
->tcs
, u32
);
666 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &u32
, 2);
668 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
670 if (CTF_FILE_MIN_SIZE
671 > (writer
->tcs
.packet_start
+ writer
->tcs
.content_size
))
675 /* Fake the content size to avoid assertion failure in
677 writer
->tcs
.content_size
= (CTF_FILE_MIN_SIZE
678 - 1 - writer
->tcs
.packet_start
);
679 ctf_save_fseek (&writer
->tcs
, CTF_FILE_MIN_SIZE
- 1,
681 ctf_save_write (&writer
->tcs
, &b
, 1);
686 /* This is the implementation of trace_frame_write_ops method
690 ctf_write_frame_start (struct trace_file_writer
*self
, uint16_t tpnum
)
692 struct ctf_trace_file_writer
*writer
693 = (struct ctf_trace_file_writer
*) self
;
694 uint32_t id
= CTF_EVENT_ID_FRAME
;
697 /* Step 1: Write packet context. */
700 ctf_save_write_uint32 (&writer
->tcs
, u32
);
701 /* content_size and packet_size.. We still don't know the value,
703 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
704 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
705 /* Tracepoint number. */
706 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &tpnum
, 2);
708 /* Step 2: Write event "frame". */
710 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
713 /* This is the implementation of trace_frame_write_ops method
717 ctf_write_frame_r_block (struct trace_file_writer
*self
,
718 gdb_byte
*buf
, int32_t size
)
720 struct ctf_trace_file_writer
*writer
721 = (struct ctf_trace_file_writer
*) self
;
722 uint32_t id
= CTF_EVENT_ID_REGISTER
;
725 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
727 /* array contents. */
728 ctf_save_align_write (&writer
->tcs
, buf
, size
, 1);
731 /* This is the implementation of trace_frame_write_ops method
732 write_m_block_header. */
735 ctf_write_frame_m_block_header (struct trace_file_writer
*self
,
736 uint64_t addr
, uint16_t length
)
738 struct ctf_trace_file_writer
*writer
739 = (struct ctf_trace_file_writer
*) self
;
740 uint32_t event_id
= CTF_EVENT_ID_MEMORY
;
743 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &event_id
, 4, 4);
746 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &addr
, 8, 8);
749 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &length
, 2, 2);
752 /* This is the implementation of trace_frame_write_ops method
753 write_m_block_memory. */
756 ctf_write_frame_m_block_memory (struct trace_file_writer
*self
,
757 gdb_byte
*buf
, uint16_t length
)
759 struct ctf_trace_file_writer
*writer
760 = (struct ctf_trace_file_writer
*) self
;
763 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) buf
, length
, 1);
766 /* This is the implementation of trace_frame_write_ops method
770 ctf_write_frame_v_block (struct trace_file_writer
*self
,
771 int32_t num
, uint64_t val
)
773 struct ctf_trace_file_writer
*writer
774 = (struct ctf_trace_file_writer
*) self
;
775 uint32_t id
= CTF_EVENT_ID_TSV
;
778 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
781 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &val
, 8, 8);
783 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &num
, 4, 4);
786 /* This is the implementation of trace_frame_write_ops method
790 ctf_write_frame_end (struct trace_file_writer
*self
)
792 struct ctf_trace_file_writer
*writer
793 = (struct ctf_trace_file_writer
*) self
;
797 /* Write the content size to packet header. */
798 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ 4,
800 u32
= writer
->tcs
.content_size
* TARGET_CHAR_BIT
;
802 t
= writer
->tcs
.content_size
;
803 ctf_save_write_uint32 (&writer
->tcs
, u32
);
805 /* Write the packet size. */
806 u32
+= 4 * TARGET_CHAR_BIT
;
807 ctf_save_write_uint32 (&writer
->tcs
, u32
);
809 writer
->tcs
.content_size
= t
;
811 /* Write zero at the end of the packet. */
812 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ t
,
815 ctf_save_write_uint32 (&writer
->tcs
, u32
);
816 writer
->tcs
.content_size
= t
;
818 ctf_save_next_packet (&writer
->tcs
);
821 /* Operations to write various types of trace frames into CTF
824 static const struct trace_frame_write_ops ctf_write_frame_ops
=
826 ctf_write_frame_start
,
827 ctf_write_frame_r_block
,
828 ctf_write_frame_m_block_header
,
829 ctf_write_frame_m_block_memory
,
830 ctf_write_frame_v_block
,
834 /* Operations to write trace buffers into CTF format. */
836 static const struct trace_file_write_ops ctf_write_ops
=
842 ctf_write_regblock_type
,
844 ctf_write_uploaded_tsv
,
845 ctf_write_uploaded_tp
,
846 ctf_write_definition_end
,
848 &ctf_write_frame_ops
,
852 /* Return a trace writer for CTF format. */
854 struct trace_file_writer
*
855 ctf_trace_file_writer_new (void)
857 struct ctf_trace_file_writer
*writer
858 = xmalloc (sizeof (struct ctf_trace_file_writer
));
860 writer
->base
.ops
= &ctf_write_ops
;
862 return (struct trace_file_writer
*) writer
;
865 #if HAVE_LIBBABELTRACE
866 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
867 iterator to iterate over each event in CTF data and APIs to get
868 details of event and packet, so it is very convenient to use
869 libbabeltrace to access events in CTF. */
871 #include <babeltrace/babeltrace.h>
872 #include <babeltrace/ctf/events.h>
873 #include <babeltrace/ctf/iterator.h>
875 /* The struct pointer for current CTF directory. */
876 static struct bt_context
*ctx
= NULL
;
877 static struct bt_ctf_iter
*ctf_iter
= NULL
;
878 /* The position of the first packet containing trace frame. */
879 static struct bt_iter_pos
*start_pos
;
881 /* The name of CTF directory. */
882 static char *trace_dirname
;
884 static struct target_ops ctf_ops
;
886 /* Destroy ctf iterator and context. */
891 if (ctf_iter
!= NULL
)
893 bt_ctf_iter_destroy (ctf_iter
);
898 bt_context_put (ctx
);
903 /* Open CTF trace data in DIRNAME. */
906 ctf_open_dir (char *dirname
)
909 struct bt_iter_pos begin_pos
;
910 struct bt_iter_pos
*pos
;
912 ctx
= bt_context_create ();
914 error (_("Unable to create bt_context"));
915 ret
= bt_context_add_trace (ctx
, dirname
, "ctf", NULL
, NULL
, NULL
);
919 error (_("Unable to use libbabeltrace on directory \"%s\""),
923 begin_pos
.type
= BT_SEEK_BEGIN
;
924 ctf_iter
= bt_ctf_iter_create (ctx
, &begin_pos
, NULL
);
925 if (ctf_iter
== NULL
)
928 error (_("Unable to create bt_iterator"));
931 /* Iterate over events, and look for an event for register block
932 to set trace_regblock_size. */
934 /* Save the current position. */
935 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
936 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
941 struct bt_ctf_event
*event
;
943 event
= bt_ctf_iter_read_event (ctf_iter
);
945 name
= bt_ctf_event_name (event
);
949 else if (strcmp (name
, "register") == 0)
951 const struct bt_definition
*scope
952 = bt_ctf_get_top_level_scope (event
,
954 const struct bt_definition
*array
955 = bt_ctf_get_field (event
, scope
, "contents");
958 = bt_ctf_get_array_len (bt_ctf_get_decl_from_def (array
));
961 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
965 /* Restore the position. */
966 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
969 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
970 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
974 /* EVENT is the "status" event and TS is filled in. */
977 ctf_read_status (struct bt_ctf_event
*event
, struct trace_status
*ts
)
979 const struct bt_definition
*scope
980 = bt_ctf_get_top_level_scope (event
, BT_EVENT_FIELDS
);
982 SET_INT32_FIELD (event
, scope
, ts
, stop_reason
);
983 SET_INT32_FIELD (event
, scope
, ts
, stopping_tracepoint
);
984 SET_INT32_FIELD (event
, scope
, ts
, traceframe_count
);
985 SET_INT32_FIELD (event
, scope
, ts
, traceframes_created
);
986 SET_INT32_FIELD (event
, scope
, ts
, buffer_free
);
987 SET_INT32_FIELD (event
, scope
, ts
, buffer_size
);
988 SET_INT32_FIELD (event
, scope
, ts
, disconnected_tracing
);
989 SET_INT32_FIELD (event
, scope
, ts
, circular_buffer
);
991 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
994 /* Read the events "tsv_def" one by one, extract its contents and fill
995 in the list UPLOADED_TSVS. */
998 ctf_read_tsv (struct uploaded_tsv
**uploaded_tsvs
)
1000 gdb_assert (ctf_iter
!= NULL
);
1004 struct bt_ctf_event
*event
;
1005 const struct bt_definition
*scope
;
1006 const struct bt_definition
*def
;
1008 struct uploaded_tsv
*utsv
= NULL
;
1010 event
= bt_ctf_iter_read_event (ctf_iter
);
1011 scope
= bt_ctf_get_top_level_scope (event
,
1012 BT_STREAM_EVENT_HEADER
);
1013 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1015 if (event_id
!= CTF_EVENT_ID_TSV_DEF
)
1018 scope
= bt_ctf_get_top_level_scope (event
,
1021 def
= bt_ctf_get_field (event
, scope
, "number");
1022 utsv
= get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def
),
1025 def
= bt_ctf_get_field (event
, scope
, "builtin");
1026 utsv
->builtin
= (int32_t) bt_ctf_get_int64 (def
);
1027 def
= bt_ctf_get_field (event
, scope
, "initial_value");
1028 utsv
->initial_value
= bt_ctf_get_int64 (def
);
1030 def
= bt_ctf_get_field (event
, scope
, "name");
1031 utsv
->name
= xstrdup (bt_ctf_get_string (def
));
1033 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1039 /* Read the value of element whose index is NUM from CTF and write it
1040 to the corresponding VAR->ARRAY. */
1042 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
1046 const struct bt_definition *def; \
1048 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1051 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1052 for (i = 0; i < u32; i++) \
1054 const struct bt_definition *element \
1055 = bt_ctf_get_index ((EVENT), def, i); \
1057 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1058 xstrdup (bt_ctf_get_string (element))); \
1063 /* Read a string from CTF and set VAR->FIELD. If the length of string
1064 is zero, set VAR->FIELD to NULL. */
1066 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1069 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1073 if (strlen (p) > 0) \
1074 (VAR)->FIELD = xstrdup (p); \
1076 (VAR)->FIELD = NULL; \
1080 /* Read the events "tp_def" one by one, extract its contents and fill
1081 in the list UPLOADED_TPS. */
1084 ctf_read_tp (struct uploaded_tp
**uploaded_tps
)
1086 gdb_assert (ctf_iter
!= NULL
);
1090 struct bt_ctf_event
*event
;
1091 const struct bt_definition
*scope
;
1095 struct uploaded_tp
*utp
= NULL
;
1097 event
= bt_ctf_iter_read_event (ctf_iter
);
1098 scope
= bt_ctf_get_top_level_scope (event
,
1099 BT_STREAM_EVENT_HEADER
);
1100 u32
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1102 if (u32
!= CTF_EVENT_ID_TP_DEF
)
1105 scope
= bt_ctf_get_top_level_scope (event
,
1107 int32
= (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event
,
1110 u64
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1112 utp
= get_uploaded_tp (int32
, u64
, uploaded_tps
);
1114 SET_INT32_FIELD (event
, scope
, utp
, enabled
);
1115 SET_INT32_FIELD (event
, scope
, utp
, step
);
1116 SET_INT32_FIELD (event
, scope
, utp
, pass
);
1117 SET_INT32_FIELD (event
, scope
, utp
, hit_count
);
1118 SET_INT32_FIELD (event
, scope
, utp
, type
);
1120 /* Read 'cmd_strings'. */
1121 SET_ARRAY_FIELD (event
, scope
, utp
, cmd_num
, cmd_strings
);
1122 /* Read 'actions'. */
1123 SET_ARRAY_FIELD (event
, scope
, utp
, action_num
, actions
);
1124 /* Read 'step_actions'. */
1125 SET_ARRAY_FIELD (event
, scope
, utp
, step_action_num
,
1128 SET_STRING_FIELD(event
, scope
, utp
, at_string
);
1129 SET_STRING_FIELD(event
, scope
, utp
, cond_string
);
1131 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1136 /* This is the implementation of target_ops method to_open. Open CTF
1137 trace data, read trace status, trace state variables and tracepoint
1138 definitions from the first packet. Set the start position at the
1139 second packet which contains events on trace blocks. */
1142 ctf_open (char *dirname
, int from_tty
)
1144 struct bt_ctf_event
*event
;
1146 const struct bt_definition
*scope
;
1147 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
1148 struct uploaded_tp
*uploaded_tps
= NULL
;
1151 error (_("No CTF directory specified."));
1153 ctf_open_dir (dirname
);
1155 target_preopen (from_tty
);
1157 /* Skip the first packet which about the trace status. The first
1158 event is "frame". */
1159 event
= bt_ctf_iter_read_event (ctf_iter
);
1160 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1161 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1162 if (event_id
!= CTF_EVENT_ID_FRAME
)
1163 error (_("Wrong event id of the first event"));
1164 /* The second event is "status". */
1165 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1166 event
= bt_ctf_iter_read_event (ctf_iter
);
1167 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1168 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1169 if (event_id
!= CTF_EVENT_ID_STATUS
)
1170 error (_("Wrong event id of the second event"));
1171 ctf_read_status (event
, current_trace_status ());
1173 ctf_read_tsv (&uploaded_tsvs
);
1175 ctf_read_tp (&uploaded_tps
);
1177 event
= bt_ctf_iter_read_event (ctf_iter
);
1178 /* EVENT can be NULL if we've already gone to the end of stream of
1182 scope
= bt_ctf_get_top_level_scope (event
,
1183 BT_STREAM_EVENT_HEADER
);
1184 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
,
1186 if (event_id
!= CTF_EVENT_ID_FRAME
)
1187 error (_("Wrong event id of the first event of the second packet"));
1190 start_pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1191 gdb_assert (start_pos
->type
== BT_SEEK_RESTORE
);
1193 trace_dirname
= xstrdup (dirname
);
1194 push_target (&ctf_ops
);
1196 inferior_appeared (current_inferior (), CTF_PID
);
1197 inferior_ptid
= pid_to_ptid (CTF_PID
);
1198 add_thread_silent (inferior_ptid
);
1200 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
1201 merge_uploaded_tracepoints (&uploaded_tps
);
1204 /* This is the implementation of target_ops method to_close. Destroy
1205 CTF iterator and context. */
1208 ctf_close (struct target_ops
*self
)
1213 xfree (trace_dirname
);
1214 trace_dirname
= NULL
;
1216 pid
= ptid_get_pid (inferior_ptid
);
1217 inferior_ptid
= null_ptid
; /* Avoid confusion from thread stuff. */
1218 exit_inferior_silent (pid
);
1220 trace_reset_local_state ();
1223 /* This is the implementation of target_ops method to_files_info.
1224 Print the directory name of CTF trace data. */
1227 ctf_files_info (struct target_ops
*t
)
1229 printf_filtered ("\t`%s'\n", trace_dirname
);
1232 /* This is the implementation of target_ops method to_fetch_registers.
1233 Iterate over events whose name is "register" in current frame,
1234 extract contents from events, and set REGCACHE with the contents.
1235 If no matched events are found, mark registers unavailable. */
1238 ctf_fetch_registers (struct target_ops
*ops
,
1239 struct regcache
*regcache
, int regno
)
1241 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1242 int offset
, regn
, regsize
, pc_regno
;
1243 gdb_byte
*regs
= NULL
;
1244 struct bt_ctf_event
*event
= NULL
;
1245 struct bt_iter_pos
*pos
;
1247 /* An uninitialized reg size says we're not going to be
1248 successful at getting register blocks. */
1249 if (trace_regblock_size
== 0)
1252 gdb_assert (ctf_iter
!= NULL
);
1253 /* Save the current position. */
1254 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1255 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1260 struct bt_ctf_event
*event1
;
1262 event1
= bt_ctf_iter_read_event (ctf_iter
);
1264 name
= bt_ctf_event_name (event1
);
1266 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1268 else if (strcmp (name
, "register") == 0)
1274 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1278 /* Restore the position. */
1279 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1283 const struct bt_definition
*scope
1284 = bt_ctf_get_top_level_scope (event
,
1286 const struct bt_definition
*array
1287 = bt_ctf_get_field (event
, scope
, "contents");
1289 regs
= (gdb_byte
*) bt_ctf_get_char_array (array
);
1290 /* Assume the block is laid out in GDB register number order,
1291 each register with the size that it has in GDB. */
1293 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1295 regsize
= register_size (gdbarch
, regn
);
1296 /* Make sure we stay within block bounds. */
1297 if (offset
+ regsize
>= trace_regblock_size
)
1299 if (regcache_register_status (regcache
, regn
) == REG_UNKNOWN
)
1303 regcache_raw_supply (regcache
, regno
, regs
+ offset
);
1306 else if (regno
== -1)
1308 regcache_raw_supply (regcache
, regn
, regs
+ offset
);
1316 regs
= alloca (trace_regblock_size
);
1318 /* We get here if no register data has been found. Mark registers
1320 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1321 regcache_raw_supply (regcache
, regn
, NULL
);
1323 /* We can often usefully guess that the PC is going to be the same
1324 as the address of the tracepoint. */
1325 pc_regno
= gdbarch_pc_regnum (gdbarch
);
1326 if (pc_regno
>= 0 && (regno
== -1 || regno
== pc_regno
))
1328 struct tracepoint
*tp
= get_tracepoint (get_tracepoint_number ());
1330 if (tp
!= NULL
&& tp
->base
.loc
)
1332 /* But don't try to guess if tracepoint is multi-location... */
1333 if (tp
->base
.loc
->next
!= NULL
)
1335 warning (_("Tracepoint %d has multiple "
1336 "locations, cannot infer $pc"),
1340 /* ... or does while-stepping. */
1341 if (tp
->step_count
> 0)
1343 warning (_("Tracepoint %d does while-stepping, "
1344 "cannot infer $pc"),
1349 store_unsigned_integer (regs
, register_size (gdbarch
, pc_regno
),
1350 gdbarch_byte_order (gdbarch
),
1351 tp
->base
.loc
->address
);
1352 regcache_raw_supply (regcache
, pc_regno
, regs
);
1357 /* This is the implementation of target_ops method to_xfer_partial.
1358 Iterate over events whose name is "memory" in
1359 current frame, extract the address and length from events. If
1360 OFFSET is within the range, read the contents from events to
1363 static enum target_xfer_status
1364 ctf_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1365 const char *annex
, gdb_byte
*readbuf
,
1366 const gdb_byte
*writebuf
, ULONGEST offset
,
1367 ULONGEST len
, ULONGEST
*xfered_len
)
1369 /* We're only doing regular memory for now. */
1370 if (object
!= TARGET_OBJECT_MEMORY
)
1373 if (readbuf
== NULL
)
1374 error (_("ctf_xfer_partial: trace file is read-only"));
1376 if (get_traceframe_number () != -1)
1378 struct bt_iter_pos
*pos
;
1380 enum target_xfer_status res
;
1382 gdb_assert (ctf_iter
!= NULL
);
1383 /* Save the current position. */
1384 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1385 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1387 /* Iterate through the traceframe's blocks, looking for
1394 enum bfd_endian byte_order
1395 = gdbarch_byte_order (target_gdbarch ());
1396 const struct bt_definition
*scope
;
1397 const struct bt_definition
*def
;
1398 struct bt_ctf_event
*event
1399 = bt_ctf_iter_read_event (ctf_iter
);
1400 const char *name
= bt_ctf_event_name (event
);
1402 if (strcmp (name
, "frame") == 0)
1404 else if (strcmp (name
, "memory") != 0)
1406 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1412 scope
= bt_ctf_get_top_level_scope (event
,
1415 def
= bt_ctf_get_field (event
, scope
, "address");
1416 maddr
= bt_ctf_get_uint64 (def
);
1417 def
= bt_ctf_get_field (event
, scope
, "length");
1418 mlen
= (uint16_t) bt_ctf_get_uint64 (def
);
1420 /* If the block includes the first part of the desired
1421 range, return as much it has; GDB will re-request the
1422 remainder, which might be in a different block of this
1424 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
1426 const struct bt_definition
*array
1427 = bt_ctf_get_field (event
, scope
, "contents");
1428 const struct bt_declaration
*decl
1429 = bt_ctf_get_decl_from_def (array
);
1433 contents
= xmalloc (mlen
);
1435 for (k
= 0; k
< mlen
; k
++)
1437 const struct bt_definition
*element
1438 = bt_ctf_get_index (event
, array
, k
);
1440 contents
[k
] = (gdb_byte
) bt_ctf_get_uint64 (element
);
1443 amt
= (maddr
+ mlen
) - offset
;
1447 memcpy (readbuf
, &contents
[offset
- maddr
], amt
);
1451 /* Restore the position. */
1452 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1455 return TARGET_XFER_EOF
;
1459 return TARGET_XFER_OK
;
1463 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1467 /* Restore the position. */
1468 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1470 /* Requested memory is unavailable in the context of traceframes,
1471 and this address falls within a read-only section, fallback
1472 to reading from executable. */
1473 res
= exec_read_partial_read_only (readbuf
, offset
, len
, xfered_len
);
1475 if (res
== TARGET_XFER_OK
)
1476 return TARGET_XFER_OK
;
1479 /* No use trying further, we know some memory starting
1480 at MEMADDR isn't available. */
1482 return TARGET_XFER_UNAVAILABLE
;
1487 /* Fallback to reading from read-only sections. */
1488 return section_table_read_available_memory (readbuf
, offset
, len
, xfered_len
);
1492 /* This is the implementation of target_ops method
1493 to_get_trace_state_variable_value.
1494 Iterate over events whose name is "tsv" in current frame. When the
1495 trace variable is found, set the value of it to *VAL and return
1496 true, otherwise return false. */
1499 ctf_get_trace_state_variable_value (struct target_ops
*self
,
1500 int tsvnum
, LONGEST
*val
)
1502 struct bt_iter_pos
*pos
;
1505 gdb_assert (ctf_iter
!= NULL
);
1506 /* Save the current position. */
1507 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1508 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1510 /* Iterate through the traceframe's blocks, looking for 'V'
1514 struct bt_ctf_event
*event
1515 = bt_ctf_iter_read_event (ctf_iter
);
1516 const char *name
= bt_ctf_event_name (event
);
1518 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1520 else if (strcmp (name
, "tsv") == 0)
1522 const struct bt_definition
*scope
;
1523 const struct bt_definition
*def
;
1525 scope
= bt_ctf_get_top_level_scope (event
,
1528 def
= bt_ctf_get_field (event
, scope
, "num");
1529 if (tsvnum
== (int32_t) bt_ctf_get_uint64 (def
))
1531 def
= bt_ctf_get_field (event
, scope
, "val");
1532 *val
= bt_ctf_get_uint64 (def
);
1538 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1542 /* Restore the position. */
1543 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1548 /* Return the tracepoint number in "frame" event. */
1551 ctf_get_tpnum_from_frame_event (struct bt_ctf_event
*event
)
1553 /* The packet context of events has a field "tpnum". */
1554 const struct bt_definition
*scope
1555 = bt_ctf_get_top_level_scope (event
, BT_STREAM_PACKET_CONTEXT
);
1557 = bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "tpnum"));
1562 /* Return the address at which the current frame was collected. */
1565 ctf_get_traceframe_address (void)
1567 struct bt_ctf_event
*event
= NULL
;
1568 struct bt_iter_pos
*pos
;
1571 gdb_assert (ctf_iter
!= NULL
);
1572 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1573 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1578 struct bt_ctf_event
*event1
;
1580 event1
= bt_ctf_iter_read_event (ctf_iter
);
1582 name
= bt_ctf_event_name (event1
);
1586 else if (strcmp (name
, "frame") == 0)
1592 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1598 int tpnum
= ctf_get_tpnum_from_frame_event (event
);
1599 struct tracepoint
*tp
1600 = get_tracepoint_by_number_on_target (tpnum
);
1602 if (tp
&& tp
->base
.loc
)
1603 addr
= tp
->base
.loc
->address
;
1606 /* Restore the position. */
1607 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1612 /* This is the implementation of target_ops method to_trace_find.
1613 Iterate the events whose name is "frame", extract the tracepoint
1614 number in it. Return traceframe number when matched. */
1617 ctf_trace_find (struct target_ops
*self
, enum trace_find_type type
, int num
,
1618 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
1623 struct bt_iter_pos pos
;
1632 gdb_assert (ctf_iter
!= NULL
);
1633 /* Set iterator back to the start. */
1634 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), start_pos
);
1639 struct bt_ctf_event
*event
;
1642 event
= bt_ctf_iter_read_event (ctf_iter
);
1644 name
= bt_ctf_event_name (event
);
1646 if (event
== NULL
|| name
== NULL
)
1649 if (strcmp (name
, "frame") == 0)
1653 if (type
== tfind_number
)
1655 /* Looking for a specific trace frame. */
1661 /* Start from the _next_ trace frame. */
1662 if (tfnum
> get_traceframe_number ())
1668 struct tracepoint
*tp
= get_tracepoint (num
);
1671 && (tp
->number_on_target
1672 == ctf_get_tpnum_from_frame_event (event
)))
1677 tfaddr
= ctf_get_traceframe_address ();
1678 if (tfaddr
== addr1
)
1682 tfaddr
= ctf_get_traceframe_address ();
1683 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
1687 tfaddr
= ctf_get_traceframe_address ();
1688 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
1692 internal_error (__FILE__
, __LINE__
, _("unknown tfind type"));
1699 *tpp
= ctf_get_tpnum_from_frame_event (event
);
1701 /* Skip the event "frame". */
1702 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1709 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1716 /* This is the implementation of target_ops method to_traceframe_info.
1717 Iterate the events whose name is "memory", in current
1718 frame, extract memory range information, and return them in
1721 static struct traceframe_info
*
1722 ctf_traceframe_info (struct target_ops
*self
)
1724 struct traceframe_info
*info
= XCNEW (struct traceframe_info
);
1726 struct bt_iter_pos
*pos
;
1728 gdb_assert (ctf_iter
!= NULL
);
1729 /* Save the current position. */
1730 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1731 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1735 struct bt_ctf_event
*event
1736 = bt_ctf_iter_read_event (ctf_iter
);
1738 name
= bt_ctf_event_name (event
);
1740 if (name
== NULL
|| strcmp (name
, "register") == 0
1741 || strcmp (name
, "frame") == 0)
1743 else if (strcmp (name
, "memory") == 0)
1745 const struct bt_definition
*scope
1746 = bt_ctf_get_top_level_scope (event
,
1748 const struct bt_definition
*def
;
1749 struct mem_range
*r
;
1751 r
= VEC_safe_push (mem_range_s
, info
->memory
, NULL
);
1752 def
= bt_ctf_get_field (event
, scope
, "address");
1753 r
->start
= bt_ctf_get_uint64 (def
);
1755 def
= bt_ctf_get_field (event
, scope
, "length");
1756 r
->length
= (uint16_t) bt_ctf_get_uint64 (def
);
1758 else if (strcmp (name
, "tsv") == 0)
1761 const struct bt_definition
*scope
1762 = bt_ctf_get_top_level_scope (event
,
1764 const struct bt_definition
*def
;
1766 def
= bt_ctf_get_field (event
, scope
, "num");
1767 vnum
= (int) bt_ctf_get_int64 (def
);
1768 VEC_safe_push (int, info
->tvars
, vnum
);
1772 warning (_("Unhandled trace block type (%s) "
1773 "while building trace frame info."),
1777 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1780 while (name
!= NULL
&& strcmp (name
, "frame") != 0);
1782 /* Restore the position. */
1783 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1791 memset (&ctf_ops
, 0, sizeof (ctf_ops
));
1793 init_tracefile_ops (&ctf_ops
);
1794 ctf_ops
.to_shortname
= "ctf";
1795 ctf_ops
.to_longname
= "CTF file";
1796 ctf_ops
.to_doc
= "Use a CTF directory as a target.\n\
1797 Specify the filename of the CTF directory.";
1798 ctf_ops
.to_open
= ctf_open
;
1799 ctf_ops
.to_close
= ctf_close
;
1800 ctf_ops
.to_fetch_registers
= ctf_fetch_registers
;
1801 ctf_ops
.to_xfer_partial
= ctf_xfer_partial
;
1802 ctf_ops
.to_files_info
= ctf_files_info
;
1803 ctf_ops
.to_trace_find
= ctf_trace_find
;
1804 ctf_ops
.to_get_trace_state_variable_value
1805 = ctf_get_trace_state_variable_value
;
1806 ctf_ops
.to_traceframe_info
= ctf_traceframe_info
;
1811 /* -Wmissing-prototypes */
1813 extern initialize_file_ftype _initialize_ctf
;
1815 /* module initialization */
1818 _initialize_ctf (void)
1820 #if HAVE_LIBBABELTRACE
1823 add_target_with_completer (&ctf_ops
, filename_completer
);