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"
32 /* GDB saves trace buffers and other information (such as trace
33 status) got from the remote target into Common Trace Format (CTF).
34 The following types of information are expected to save in CTF:
36 1. The length (in bytes) of register cache. Event "register" will
37 be defined in metadata, which includes the length.
39 2. Trace status. Event "status" is defined in metadata, which
40 includes all aspects of trace status.
42 3. Uploaded trace variables. Event "tsv_def" is defined in
43 metadata, which is about all aspects of a uploaded trace variable.
44 Uploaded tracepoints. Event "tp_def" is defined in meta, which
45 is about all aspects of an uploaded tracepoint. Note that the
46 "sequence" (a CTF type, which is a dynamically-sized array.) is
47 used for "actions" "step_actions" and "cmd_strings".
49 4. Trace frames. Each trace frame is composed by several blocks
50 of different types ('R', 'M', 'V'). One trace frame is saved in
51 one CTF packet and the blocks of this frame are saved as events.
52 4.1: The trace frame related information (such as the number of
53 tracepoint associated with this frame) is saved in the packet
55 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
56 "register" and "tsv" respectively.
57 4.3: When iterating over events, babeltrace can't tell iterator
58 goes to a new packet, so we need a marker or anchor to tell GDB
59 that iterator goes into a new packet or frame. We define event
62 #define CTF_MAGIC 0xC1FC1FC1
63 #define CTF_SAVE_MAJOR 1
64 #define CTF_SAVE_MINOR 8
66 #define CTF_METADATA_NAME "metadata"
67 #define CTF_DATASTREAM_NAME "datastream"
69 /* Reserved event id. */
71 #define CTF_EVENT_ID_REGISTER 0
72 #define CTF_EVENT_ID_TSV 1
73 #define CTF_EVENT_ID_MEMORY 2
74 #define CTF_EVENT_ID_FRAME 3
75 #define CTF_EVENT_ID_STATUS 4
76 #define CTF_EVENT_ID_TSV_DEF 5
77 #define CTF_EVENT_ID_TP_DEF 6
79 /* The state kept while writing the CTF datastream file. */
81 struct trace_write_handler
83 /* File descriptor of metadata. */
85 /* File descriptor of traceframes. */
88 /* This is the content size of the current packet. */
91 /* This is the start offset of current packet. */
95 /* Write metadata in FORMAT. */
98 ctf_save_write_metadata (struct trace_write_handler
*handler
,
99 const char *format
, ...)
103 va_start (args
, format
);
104 if (vfprintf (handler
->metadata_fd
, format
, args
) < 0)
105 error (_("Unable to write metadata file (%s)"),
106 safe_strerror (errno
));
110 /* Write BUF of length SIZE to datastream file represented by
114 ctf_save_write (struct trace_write_handler
*handler
,
115 const gdb_byte
*buf
, size_t size
)
117 if (fwrite (buf
, size
, 1, handler
->datastream_fd
) != 1)
118 error (_("Unable to write file for saving trace data (%s)"),
119 safe_strerror (errno
));
121 handler
->content_size
+= size
;
126 /* Write a unsigned 32-bit integer to datastream file represented by
129 #define ctf_save_write_uint32(HANDLER, U32) \
130 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
132 /* Write a signed 32-bit integer to datastream file represented by
135 #define ctf_save_write_int32(HANDLER, INT32) \
136 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
138 /* Set datastream file position. Update HANDLER->content_size
139 if WHENCE is SEEK_CUR. */
142 ctf_save_fseek (struct trace_write_handler
*handler
, long offset
,
145 gdb_assert (whence
!= SEEK_END
);
146 gdb_assert (whence
!= SEEK_SET
147 || offset
<= handler
->content_size
+ handler
->packet_start
);
149 if (fseek (handler
->datastream_fd
, offset
, whence
))
150 error (_("Unable to seek file for saving trace data (%s)"),
151 safe_strerror (errno
));
153 if (whence
== SEEK_CUR
)
154 handler
->content_size
+= offset
;
159 /* Change the datastream file position to align on ALIGN_SIZE,
160 and write BUF to datastream file. The size of BUF is SIZE. */
163 ctf_save_align_write (struct trace_write_handler
*handler
,
165 size_t size
, size_t align_size
)
168 = (align_up (handler
->content_size
, align_size
)
169 - handler
->content_size
);
171 if (ctf_save_fseek (handler
, offset
, SEEK_CUR
))
174 if (ctf_save_write (handler
, buf
, size
))
180 /* Write events to next new packet. */
183 ctf_save_next_packet (struct trace_write_handler
*handler
)
185 handler
->packet_start
+= (handler
->content_size
+ 4);
186 ctf_save_fseek (handler
, handler
->packet_start
, SEEK_SET
);
187 handler
->content_size
= 0;
190 /* Write the CTF metadata header. */
193 ctf_save_metadata_header (struct trace_write_handler
*handler
)
195 const char metadata_fmt
[] =
199 " byte_order = %s;\n" /* be or le */
200 " packet.header := struct {\n"
206 " packet.context := struct {\n"
207 " uint32_t content_size;\n"
208 " uint32_t packet_size;\n"
211 " event.header := struct {\n"
216 ctf_save_write_metadata (handler
, "/* CTF %d.%d */\n",
217 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
);
218 ctf_save_write_metadata (handler
,
219 "typealias integer { size = 8; align = 8; "
220 "signed = false; encoding = ascii;}"
222 ctf_save_write_metadata (handler
,
223 "typealias integer { size = 8; align = 8; "
226 ctf_save_write_metadata (handler
,
227 "typealias integer { size = 16; align = 16;"
228 "signed = false; } := uint16_t;\n");
229 ctf_save_write_metadata (handler
,
230 "typealias integer { size = 32; align = 32;"
231 "signed = false; } := uint32_t;\n");
232 ctf_save_write_metadata (handler
,
233 "typealias integer { size = 64; align = 64;"
234 "signed = false; base = hex;}"
236 ctf_save_write_metadata (handler
,
237 "typealias integer { size = 32; align = 32;"
238 "signed = true; } := int32_t;\n");
239 ctf_save_write_metadata (handler
,
240 "typealias integer { size = 64; align = 64;"
241 "signed = true; } := int64_t;\n");
242 ctf_save_write_metadata (handler
,
243 "typealias string { encoding = ascii;"
245 ctf_save_write_metadata (handler
, "\n");
247 /* Get the byte order of the host and write CTF data in this byte
250 #define HOST_ENDIANNESS "be"
252 #define HOST_ENDIANNESS "le"
255 ctf_save_write_metadata (handler
, metadata_fmt
,
256 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
,
258 ctf_save_write_metadata (handler
, "\n");
261 /* CTF trace writer. */
263 struct ctf_trace_file_writer
265 struct trace_file_writer base
;
267 /* States related to writing CTF trace file. */
268 struct trace_write_handler tcs
;
271 /* This is the implementation of trace_file_write_ops method
275 ctf_dtor (struct trace_file_writer
*self
)
277 struct ctf_trace_file_writer
*writer
278 = (struct ctf_trace_file_writer
*) self
;
280 if (writer
->tcs
.metadata_fd
!= NULL
)
281 fclose (writer
->tcs
.metadata_fd
);
283 if (writer
->tcs
.datastream_fd
!= NULL
)
284 fclose (writer
->tcs
.datastream_fd
);
288 /* This is the implementation of trace_file_write_ops method
292 ctf_target_save (struct trace_file_writer
*self
,
295 /* Don't support save trace file to CTF format in the target. */
301 #define mkdir(pathname, mode) mkdir (pathname)
304 /* This is the implementation of trace_file_write_ops method
305 start. It creates the directory DIRNAME, metadata and datastream
309 ctf_start (struct trace_file_writer
*self
, const char *dirname
)
312 struct cleanup
*old_chain
;
313 struct ctf_trace_file_writer
*writer
314 = (struct ctf_trace_file_writer
*) self
;
316 mode_t hmode
= S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
;
318 /* Create DIRNAME. */
319 if (mkdir (dirname
, hmode
) && errno
!= EEXIST
)
320 error (_("Unable to open directory '%s' for saving trace data (%s)"),
321 dirname
, safe_strerror (errno
));
323 memset (&writer
->tcs
, '\0', sizeof (writer
->tcs
));
325 file_name
= xstrprintf ("%s/%s", dirname
, CTF_METADATA_NAME
);
326 old_chain
= make_cleanup (xfree
, file_name
);
328 writer
->tcs
.metadata_fd
= fopen (file_name
, "w");
329 if (writer
->tcs
.metadata_fd
== NULL
)
330 error (_("Unable to open file '%s' for saving trace data (%s)"),
331 file_name
, safe_strerror (errno
));
332 do_cleanups (old_chain
);
334 ctf_save_metadata_header (&writer
->tcs
);
336 file_name
= xstrprintf ("%s/%s", dirname
, CTF_DATASTREAM_NAME
);
337 old_chain
= make_cleanup (xfree
, file_name
);
338 writer
->tcs
.datastream_fd
= fopen (file_name
, "w");
339 if (writer
->tcs
.datastream_fd
== NULL
)
340 error (_("Unable to open file '%s' for saving trace data (%s)"),
341 file_name
, safe_strerror (errno
));
342 do_cleanups (old_chain
);
345 /* This is the implementation of trace_file_write_ops method
346 write_header. Write the types of events on trace variable and
350 ctf_write_header (struct trace_file_writer
*self
)
352 struct ctf_trace_file_writer
*writer
353 = (struct ctf_trace_file_writer
*) self
;
356 ctf_save_write_metadata (&writer
->tcs
, "\n");
357 ctf_save_write_metadata (&writer
->tcs
,
358 "event {\n\tname = \"memory\";\n\tid = %u;\n"
359 "\tfields := struct { \n"
360 "\t\tuint64_t address;\n"
361 "\t\tuint16_t length;\n"
362 "\t\tuint8_t contents[length];\n"
364 "};\n", CTF_EVENT_ID_MEMORY
);
366 ctf_save_write_metadata (&writer
->tcs
, "\n");
367 ctf_save_write_metadata (&writer
->tcs
,
368 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
369 "\tfields := struct { \n"
370 "\t\tuint64_t val;\n"
371 "\t\tuint32_t num;\n"
373 "};\n", CTF_EVENT_ID_TSV
);
375 ctf_save_write_metadata (&writer
->tcs
, "\n");
376 ctf_save_write_metadata (&writer
->tcs
,
377 "event {\n\tname = \"frame\";\n\tid = %u;\n"
378 "\tfields := struct { \n"
380 "};\n", CTF_EVENT_ID_FRAME
);
382 ctf_save_write_metadata (&writer
->tcs
, "\n");
383 ctf_save_write_metadata (&writer
->tcs
,
384 "event {\n\tname = \"tsv_def\";\n"
385 "\tid = %u;\n\tfields := struct { \n"
386 "\t\tint64_t initial_value;\n"
387 "\t\tint32_t number;\n"
388 "\t\tint32_t builtin;\n"
391 "};\n", CTF_EVENT_ID_TSV_DEF
);
393 ctf_save_write_metadata (&writer
->tcs
, "\n");
394 ctf_save_write_metadata (&writer
->tcs
,
395 "event {\n\tname = \"tp_def\";\n"
396 "\tid = %u;\n\tfields := struct { \n"
397 "\t\tuint64_t addr;\n"
398 "\t\tuint64_t traceframe_usage;\n"
399 "\t\tint32_t number;\n"
400 "\t\tint32_t enabled;\n"
401 "\t\tint32_t step;\n"
402 "\t\tint32_t pass;\n"
403 "\t\tint32_t hit_count;\n"
404 "\t\tint32_t type;\n"
407 "\t\tuint32_t action_num;\n"
408 "\t\tchars actions[action_num];\n"
410 "\t\tuint32_t step_action_num;\n"
411 "\t\tchars step_actions[step_action_num];\n"
413 "\t\tchars at_string;\n"
414 "\t\tchars cond_string;\n"
416 "\t\tuint32_t cmd_num;\n"
417 "\t\tchars cmd_strings[cmd_num];\n"
419 "};\n", CTF_EVENT_ID_TP_DEF
);
421 gdb_assert (writer
->tcs
.content_size
== 0);
422 gdb_assert (writer
->tcs
.packet_start
== 0);
424 /* Create a new packet to contain this event. */
425 self
->ops
->frame_ops
->start (self
, 0);
428 /* This is the implementation of trace_file_write_ops method
429 write_regblock_type. Write the type of register event in
433 ctf_write_regblock_type (struct trace_file_writer
*self
, int size
)
435 struct ctf_trace_file_writer
*writer
436 = (struct ctf_trace_file_writer
*) self
;
438 ctf_save_write_metadata (&writer
->tcs
, "\n");
440 ctf_save_write_metadata (&writer
->tcs
,
441 "event {\n\tname = \"register\";\n\tid = %u;\n"
442 "\tfields := struct { \n"
443 "\t\tascii contents[%d];\n"
446 CTF_EVENT_ID_REGISTER
, size
);
449 /* This is the implementation of trace_file_write_ops method
453 ctf_write_status (struct trace_file_writer
*self
,
454 struct trace_status
*ts
)
456 struct ctf_trace_file_writer
*writer
457 = (struct ctf_trace_file_writer
*) self
;
461 ctf_save_write_metadata (&writer
->tcs
, "\n");
462 ctf_save_write_metadata (&writer
->tcs
,
463 "event {\n\tname = \"status\";\n\tid = %u;\n"
464 "\tfields := struct { \n"
465 "\t\tint32_t stop_reason;\n"
466 "\t\tint32_t stopping_tracepoint;\n"
467 "\t\tint32_t traceframe_count;\n"
468 "\t\tint32_t traceframes_created;\n"
469 "\t\tint32_t buffer_free;\n"
470 "\t\tint32_t buffer_size;\n"
471 "\t\tint32_t disconnected_tracing;\n"
472 "\t\tint32_t circular_buffer;\n"
475 CTF_EVENT_ID_STATUS
);
477 id
= CTF_EVENT_ID_STATUS
;
479 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
481 ctf_save_write_int32 (&writer
->tcs
, ts
->stop_reason
);
482 ctf_save_write_int32 (&writer
->tcs
, ts
->stopping_tracepoint
);
483 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframe_count
);
484 ctf_save_write_int32 (&writer
->tcs
, ts
->traceframes_created
);
485 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_free
);
486 ctf_save_write_int32 (&writer
->tcs
, ts
->buffer_size
);
487 ctf_save_write_int32 (&writer
->tcs
, ts
->disconnected_tracing
);
488 ctf_save_write_int32 (&writer
->tcs
, ts
->circular_buffer
);
491 /* This is the implementation of trace_file_write_ops method
492 write_uploaded_tsv. */
495 ctf_write_uploaded_tsv (struct trace_file_writer
*self
,
496 struct uploaded_tsv
*tsv
)
498 struct ctf_trace_file_writer
*writer
499 = (struct ctf_trace_file_writer
*) self
;
503 const gdb_byte zero
= 0;
506 int32
= CTF_EVENT_ID_TSV_DEF
;
507 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
510 int64
= tsv
->initial_value
;
511 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
514 ctf_save_write_int32 (&writer
->tcs
, tsv
->number
);
517 ctf_save_write_int32 (&writer
->tcs
, tsv
->builtin
);
520 if (tsv
->name
!= NULL
)
521 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tsv
->name
,
523 ctf_save_write (&writer
->tcs
, &zero
, 1);
526 /* This is the implementation of trace_file_write_ops method
527 write_uploaded_tp. */
530 ctf_write_uploaded_tp (struct trace_file_writer
*self
,
531 struct uploaded_tp
*tp
)
533 struct ctf_trace_file_writer
*writer
534 = (struct ctf_trace_file_writer
*) self
;
538 const gdb_byte zero
= 0;
543 int32
= CTF_EVENT_ID_TP_DEF
;
544 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int32
, 4, 4);
548 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
550 /* traceframe_usage */
551 int64
= tp
->traceframe_usage
;
552 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &int64
, 8, 8);
555 ctf_save_write_int32 (&writer
->tcs
, tp
->number
);
558 ctf_save_write_int32 (&writer
->tcs
, tp
->enabled
);
561 ctf_save_write_int32 (&writer
->tcs
, tp
->step
);
564 ctf_save_write_int32 (&writer
->tcs
, tp
->pass
);
567 ctf_save_write_int32 (&writer
->tcs
, tp
->hit_count
);
570 ctf_save_write_int32 (&writer
->tcs
, tp
->type
);
573 if (tp
->cond
!= NULL
)
574 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond
, strlen (tp
->cond
));
575 ctf_save_write (&writer
->tcs
, &zero
, 1);
578 u32
= VEC_length (char_ptr
, tp
->actions
);
579 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
580 for (a
= 0; VEC_iterate (char_ptr
, tp
->actions
, a
, act
); ++a
)
581 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
584 u32
= VEC_length (char_ptr
, tp
->step_actions
);
585 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
586 for (a
= 0; VEC_iterate (char_ptr
, tp
->step_actions
, a
, act
); ++a
)
587 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
590 if (tp
->at_string
!= NULL
)
591 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->at_string
,
592 strlen (tp
->at_string
));
593 ctf_save_write (&writer
->tcs
, &zero
, 1);
596 if (tp
->cond_string
!= NULL
)
597 ctf_save_write (&writer
->tcs
, (gdb_byte
*) tp
->cond_string
,
598 strlen (tp
->cond_string
));
599 ctf_save_write (&writer
->tcs
, &zero
, 1);
602 u32
= VEC_length (char_ptr
, tp
->cmd_strings
);
603 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &u32
, 4, 4);
604 for (a
= 0; VEC_iterate (char_ptr
, tp
->cmd_strings
, a
, act
); ++a
)
605 ctf_save_write (&writer
->tcs
, (gdb_byte
*) act
, strlen (act
) + 1);
609 /* This is the implementation of trace_file_write_ops method
610 write_definition_end. */
613 ctf_write_definition_end (struct trace_file_writer
*self
)
615 struct ctf_trace_file_writer
*writer
616 = (struct ctf_trace_file_writer
*) self
;
618 self
->ops
->frame_ops
->end (self
);
621 /* The minimal file size of data stream. It is required by
624 #define CTF_FILE_MIN_SIZE 4096
626 /* This is the implementation of trace_file_write_ops method
630 ctf_end (struct trace_file_writer
*self
)
632 struct ctf_trace_file_writer
*writer
= (struct ctf_trace_file_writer
*) self
;
634 gdb_assert (writer
->tcs
.content_size
== 0);
635 /* The babeltrace requires or assumes that the size of datastream
636 file is greater than 4096 bytes. If we don't generate enough
637 packets and events, create a fake packet which has zero event,
638 to use up the space. */
639 if (writer
->tcs
.packet_start
< CTF_FILE_MIN_SIZE
)
645 ctf_save_write_uint32 (&writer
->tcs
, u32
);
649 ctf_save_write_uint32 (&writer
->tcs
, u32
);
653 if (writer
->tcs
.packet_start
+ u32
< CTF_FILE_MIN_SIZE
)
654 u32
= CTF_FILE_MIN_SIZE
- writer
->tcs
.packet_start
;
656 u32
*= TARGET_CHAR_BIT
;
657 ctf_save_write_uint32 (&writer
->tcs
, u32
);
661 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &u32
, 2);
663 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
665 if (CTF_FILE_MIN_SIZE
666 > (writer
->tcs
.packet_start
+ writer
->tcs
.content_size
))
670 /* Fake the content size to avoid assertion failure in
672 writer
->tcs
.content_size
= (CTF_FILE_MIN_SIZE
673 - 1 - writer
->tcs
.packet_start
);
674 ctf_save_fseek (&writer
->tcs
, CTF_FILE_MIN_SIZE
- 1,
676 ctf_save_write (&writer
->tcs
, &b
, 1);
681 /* This is the implementation of trace_frame_write_ops method
685 ctf_write_frame_start (struct trace_file_writer
*self
, uint16_t tpnum
)
687 struct ctf_trace_file_writer
*writer
688 = (struct ctf_trace_file_writer
*) self
;
689 uint32_t id
= CTF_EVENT_ID_FRAME
;
692 /* Step 1: Write packet context. */
695 ctf_save_write_uint32 (&writer
->tcs
, u32
);
696 /* content_size and packet_size.. We still don't know the value,
698 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
699 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
700 /* Tracepoint number. */
701 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &tpnum
, 2);
703 /* Step 2: Write event "frame". */
705 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
708 /* This is the implementation of trace_frame_write_ops method
712 ctf_write_frame_r_block (struct trace_file_writer
*self
,
713 gdb_byte
*buf
, int32_t size
)
715 struct ctf_trace_file_writer
*writer
716 = (struct ctf_trace_file_writer
*) self
;
717 uint32_t id
= CTF_EVENT_ID_REGISTER
;
720 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
722 /* array contents. */
723 ctf_save_align_write (&writer
->tcs
, buf
, size
, 1);
726 /* This is the implementation of trace_frame_write_ops method
727 write_m_block_header. */
730 ctf_write_frame_m_block_header (struct trace_file_writer
*self
,
731 uint64_t addr
, uint16_t length
)
733 struct ctf_trace_file_writer
*writer
734 = (struct ctf_trace_file_writer
*) self
;
735 uint32_t event_id
= CTF_EVENT_ID_MEMORY
;
738 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &event_id
, 4, 4);
741 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &addr
, 8, 8);
744 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &length
, 2, 2);
747 /* This is the implementation of trace_frame_write_ops method
748 write_m_block_memory. */
751 ctf_write_frame_m_block_memory (struct trace_file_writer
*self
,
752 gdb_byte
*buf
, uint16_t length
)
754 struct ctf_trace_file_writer
*writer
755 = (struct ctf_trace_file_writer
*) self
;
758 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) buf
, length
, 1);
761 /* This is the implementation of trace_frame_write_ops method
765 ctf_write_frame_v_block (struct trace_file_writer
*self
,
766 int32_t num
, uint64_t val
)
768 struct ctf_trace_file_writer
*writer
769 = (struct ctf_trace_file_writer
*) self
;
770 uint32_t id
= CTF_EVENT_ID_TSV
;
773 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
776 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &val
, 8, 8);
778 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &num
, 4, 4);
781 /* This is the implementation of trace_frame_write_ops method
785 ctf_write_frame_end (struct trace_file_writer
*self
)
787 struct ctf_trace_file_writer
*writer
788 = (struct ctf_trace_file_writer
*) self
;
792 /* Write the content size to packet header. */
793 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ 4,
795 u32
= writer
->tcs
.content_size
* TARGET_CHAR_BIT
;
797 t
= writer
->tcs
.content_size
;
798 ctf_save_write_uint32 (&writer
->tcs
, u32
);
800 /* Write the packet size. */
801 u32
+= 4 * TARGET_CHAR_BIT
;
802 ctf_save_write_uint32 (&writer
->tcs
, u32
);
804 writer
->tcs
.content_size
= t
;
806 /* Write zero at the end of the packet. */
807 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ t
,
810 ctf_save_write_uint32 (&writer
->tcs
, u32
);
811 writer
->tcs
.content_size
= t
;
813 ctf_save_next_packet (&writer
->tcs
);
816 /* Operations to write various types of trace frames into CTF
819 static const struct trace_frame_write_ops ctf_write_frame_ops
=
821 ctf_write_frame_start
,
822 ctf_write_frame_r_block
,
823 ctf_write_frame_m_block_header
,
824 ctf_write_frame_m_block_memory
,
825 ctf_write_frame_v_block
,
829 /* Operations to write trace buffers into CTF format. */
831 static const struct trace_file_write_ops ctf_write_ops
=
837 ctf_write_regblock_type
,
839 ctf_write_uploaded_tsv
,
840 ctf_write_uploaded_tp
,
841 ctf_write_definition_end
,
843 &ctf_write_frame_ops
,
847 /* Return a trace writer for CTF format. */
849 struct trace_file_writer
*
850 ctf_trace_file_writer_new (void)
852 struct ctf_trace_file_writer
*writer
853 = xmalloc (sizeof (struct ctf_trace_file_writer
));
855 writer
->base
.ops
= &ctf_write_ops
;
857 return (struct trace_file_writer
*) writer
;
860 #if HAVE_LIBBABELTRACE
861 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
862 iterator to iterate over each event in CTF data and APIs to get
863 details of event and packet, so it is very convenient to use
864 libbabeltrace to access events in CTF. */
866 #include <babeltrace/babeltrace.h>
867 #include <babeltrace/ctf/events.h>
868 #include <babeltrace/ctf/iterator.h>
870 /* The struct pointer for current CTF directory. */
871 static struct bt_context
*ctx
= NULL
;
872 static struct bt_ctf_iter
*ctf_iter
= NULL
;
873 /* The position of the first packet containing trace frame. */
874 static struct bt_iter_pos
*start_pos
;
876 /* The name of CTF directory. */
877 static char *trace_dirname
;
879 static struct target_ops ctf_ops
;
881 /* Destroy ctf iterator and context. */
886 if (ctf_iter
!= NULL
)
888 bt_ctf_iter_destroy (ctf_iter
);
893 bt_context_put (ctx
);
898 /* Open CTF trace data in DIRNAME. */
901 ctf_open_dir (char *dirname
)
904 struct bt_iter_pos begin_pos
;
905 struct bt_iter_pos
*pos
;
907 ctx
= bt_context_create ();
909 error (_("Unable to create bt_context"));
910 ret
= bt_context_add_trace (ctx
, dirname
, "ctf", NULL
, NULL
, NULL
);
914 error (_("Unable to use libbabeltrace on directory \"%s\""),
918 begin_pos
.type
= BT_SEEK_BEGIN
;
919 ctf_iter
= bt_ctf_iter_create (ctx
, &begin_pos
, NULL
);
920 if (ctf_iter
== NULL
)
923 error (_("Unable to create bt_iterator"));
926 /* Iterate over events, and look for an event for register block
927 to set trace_regblock_size. */
929 /* Save the current position. */
930 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
931 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
936 struct bt_ctf_event
*event
;
938 event
= bt_ctf_iter_read_event (ctf_iter
);
940 name
= bt_ctf_event_name (event
);
944 else if (strcmp (name
, "register") == 0)
946 const struct bt_definition
*scope
947 = bt_ctf_get_top_level_scope (event
,
949 const struct bt_definition
*array
950 = bt_ctf_get_field (event
, scope
, "contents");
953 = bt_ctf_get_array_len (bt_ctf_get_decl_from_def (array
));
956 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
960 /* Restore the position. */
961 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
964 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
965 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
969 /* EVENT is the "status" event and TS is filled in. */
972 ctf_read_status (struct bt_ctf_event
*event
, struct trace_status
*ts
)
974 const struct bt_definition
*scope
975 = bt_ctf_get_top_level_scope (event
, BT_EVENT_FIELDS
);
977 SET_INT32_FIELD (event
, scope
, ts
, stop_reason
);
978 SET_INT32_FIELD (event
, scope
, ts
, stopping_tracepoint
);
979 SET_INT32_FIELD (event
, scope
, ts
, traceframe_count
);
980 SET_INT32_FIELD (event
, scope
, ts
, traceframes_created
);
981 SET_INT32_FIELD (event
, scope
, ts
, buffer_free
);
982 SET_INT32_FIELD (event
, scope
, ts
, buffer_size
);
983 SET_INT32_FIELD (event
, scope
, ts
, disconnected_tracing
);
984 SET_INT32_FIELD (event
, scope
, ts
, circular_buffer
);
986 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
989 /* Read the events "tsv_def" one by one, extract its contents and fill
990 in the list UPLOADED_TSVS. */
993 ctf_read_tsv (struct uploaded_tsv
**uploaded_tsvs
)
995 gdb_assert (ctf_iter
!= NULL
);
999 struct bt_ctf_event
*event
;
1000 const struct bt_definition
*scope
;
1001 const struct bt_definition
*def
;
1003 struct uploaded_tsv
*utsv
= NULL
;
1005 event
= bt_ctf_iter_read_event (ctf_iter
);
1006 scope
= bt_ctf_get_top_level_scope (event
,
1007 BT_STREAM_EVENT_HEADER
);
1008 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1010 if (event_id
!= CTF_EVENT_ID_TSV_DEF
)
1013 scope
= bt_ctf_get_top_level_scope (event
,
1016 def
= bt_ctf_get_field (event
, scope
, "number");
1017 utsv
= get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def
),
1020 def
= bt_ctf_get_field (event
, scope
, "builtin");
1021 utsv
->builtin
= (int32_t) bt_ctf_get_int64 (def
);
1022 def
= bt_ctf_get_field (event
, scope
, "initial_value");
1023 utsv
->initial_value
= bt_ctf_get_int64 (def
);
1025 def
= bt_ctf_get_field (event
, scope
, "name");
1026 utsv
->name
= xstrdup (bt_ctf_get_string (def
));
1028 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1034 /* Read the value of element whose index is NUM from CTF and write it
1035 to the corresponding VAR->ARRAY. */
1037 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
1041 const struct bt_definition *def; \
1043 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1046 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1047 for (i = 0; i < u32; i++) \
1049 const struct bt_definition *element \
1050 = bt_ctf_get_index ((EVENT), def, i); \
1052 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1053 xstrdup (bt_ctf_get_string (element))); \
1058 /* Read a string from CTF and set VAR->FIELD. If the length of string
1059 is zero, set VAR->FIELD to NULL. */
1061 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1064 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1068 if (strlen (p) > 0) \
1069 (VAR)->FIELD = xstrdup (p); \
1071 (VAR)->FIELD = NULL; \
1075 /* Read the events "tp_def" one by one, extract its contents and fill
1076 in the list UPLOADED_TPS. */
1079 ctf_read_tp (struct uploaded_tp
**uploaded_tps
)
1081 gdb_assert (ctf_iter
!= NULL
);
1085 struct bt_ctf_event
*event
;
1086 const struct bt_definition
*scope
;
1090 struct uploaded_tp
*utp
= NULL
;
1092 event
= bt_ctf_iter_read_event (ctf_iter
);
1093 scope
= bt_ctf_get_top_level_scope (event
,
1094 BT_STREAM_EVENT_HEADER
);
1095 u32
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1097 if (u32
!= CTF_EVENT_ID_TP_DEF
)
1100 scope
= bt_ctf_get_top_level_scope (event
,
1102 int32
= (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event
,
1105 u64
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
,
1107 utp
= get_uploaded_tp (int32
, u64
, uploaded_tps
);
1109 SET_INT32_FIELD (event
, scope
, utp
, enabled
);
1110 SET_INT32_FIELD (event
, scope
, utp
, step
);
1111 SET_INT32_FIELD (event
, scope
, utp
, pass
);
1112 SET_INT32_FIELD (event
, scope
, utp
, hit_count
);
1113 SET_INT32_FIELD (event
, scope
, utp
, type
);
1115 /* Read 'cmd_strings'. */
1116 SET_ARRAY_FIELD (event
, scope
, utp
, cmd_num
, cmd_strings
);
1117 /* Read 'actions'. */
1118 SET_ARRAY_FIELD (event
, scope
, utp
, action_num
, actions
);
1119 /* Read 'step_actions'. */
1120 SET_ARRAY_FIELD (event
, scope
, utp
, step_action_num
,
1123 SET_STRING_FIELD(event
, scope
, utp
, at_string
);
1124 SET_STRING_FIELD(event
, scope
, utp
, cond_string
);
1126 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1131 /* This is the implementation of target_ops method to_open. Open CTF
1132 trace data, read trace status, trace state variables and tracepoint
1133 definitions from the first packet. Set the start position at the
1134 second packet which contains events on trace blocks. */
1137 ctf_open (char *dirname
, int from_tty
)
1139 struct bt_ctf_event
*event
;
1141 const struct bt_definition
*scope
;
1142 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
1143 struct uploaded_tp
*uploaded_tps
= NULL
;
1146 error (_("No CTF directory specified."));
1148 ctf_open_dir (dirname
);
1150 target_preopen (from_tty
);
1152 /* Skip the first packet which about the trace status. The first
1153 event is "frame". */
1154 event
= bt_ctf_iter_read_event (ctf_iter
);
1155 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1156 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1157 if (event_id
!= CTF_EVENT_ID_FRAME
)
1158 error (_("Wrong event id of the first event"));
1159 /* The second event is "status". */
1160 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1161 event
= bt_ctf_iter_read_event (ctf_iter
);
1162 scope
= bt_ctf_get_top_level_scope (event
, BT_STREAM_EVENT_HEADER
);
1163 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "id"));
1164 if (event_id
!= CTF_EVENT_ID_STATUS
)
1165 error (_("Wrong event id of the second event"));
1166 ctf_read_status (event
, current_trace_status ());
1168 ctf_read_tsv (&uploaded_tsvs
);
1170 ctf_read_tp (&uploaded_tps
);
1172 event
= bt_ctf_iter_read_event (ctf_iter
);
1173 /* EVENT can be NULL if we've already gone to the end of stream of
1177 scope
= bt_ctf_get_top_level_scope (event
,
1178 BT_STREAM_EVENT_HEADER
);
1179 event_id
= bt_ctf_get_uint64 (bt_ctf_get_field (event
,
1181 if (event_id
!= CTF_EVENT_ID_FRAME
)
1182 error (_("Wrong event id of the first event of the second packet"));
1185 start_pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1186 gdb_assert (start_pos
->type
== BT_SEEK_RESTORE
);
1188 trace_dirname
= xstrdup (dirname
);
1189 push_target (&ctf_ops
);
1191 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
1192 merge_uploaded_tracepoints (&uploaded_tps
);
1195 /* This is the implementation of target_ops method to_close. Destroy
1196 CTF iterator and context. */
1202 xfree (trace_dirname
);
1203 trace_dirname
= NULL
;
1205 trace_reset_local_state ();
1208 /* This is the implementation of target_ops method to_files_info.
1209 Print the directory name of CTF trace data. */
1212 ctf_files_info (struct target_ops
*t
)
1214 printf_filtered ("\t`%s'\n", trace_dirname
);
1217 /* This is the implementation of target_ops method to_fetch_registers.
1218 Iterate over events whose name is "register" in current frame,
1219 extract contents from events, and set REGCACHE with the contents.
1220 If no matched events are found, mark registers unavailable. */
1223 ctf_fetch_registers (struct target_ops
*ops
,
1224 struct regcache
*regcache
, int regno
)
1226 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1227 int offset
, regn
, regsize
, pc_regno
;
1228 gdb_byte
*regs
= NULL
;
1229 struct bt_ctf_event
*event
= NULL
;
1230 struct bt_iter_pos
*pos
;
1232 /* An uninitialized reg size says we're not going to be
1233 successful at getting register blocks. */
1234 if (trace_regblock_size
== 0)
1237 gdb_assert (ctf_iter
!= NULL
);
1238 /* Save the current position. */
1239 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1240 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1245 struct bt_ctf_event
*event1
;
1247 event1
= bt_ctf_iter_read_event (ctf_iter
);
1249 name
= bt_ctf_event_name (event1
);
1251 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1253 else if (strcmp (name
, "register") == 0)
1259 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1263 /* Restore the position. */
1264 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1268 const struct bt_definition
*scope
1269 = bt_ctf_get_top_level_scope (event
,
1271 const struct bt_definition
*array
1272 = bt_ctf_get_field (event
, scope
, "contents");
1274 regs
= (gdb_byte
*) bt_ctf_get_char_array (array
);
1275 /* Assume the block is laid out in GDB register number order,
1276 each register with the size that it has in GDB. */
1278 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1280 regsize
= register_size (gdbarch
, regn
);
1281 /* Make sure we stay within block bounds. */
1282 if (offset
+ regsize
>= trace_regblock_size
)
1284 if (regcache_register_status (regcache
, regn
) == REG_UNKNOWN
)
1288 regcache_raw_supply (regcache
, regno
, regs
+ offset
);
1291 else if (regno
== -1)
1293 regcache_raw_supply (regcache
, regn
, regs
+ offset
);
1301 regs
= alloca (trace_regblock_size
);
1303 /* We get here if no register data has been found. Mark registers
1305 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
1306 regcache_raw_supply (regcache
, regn
, NULL
);
1308 /* We can often usefully guess that the PC is going to be the same
1309 as the address of the tracepoint. */
1310 pc_regno
= gdbarch_pc_regnum (gdbarch
);
1311 if (pc_regno
>= 0 && (regno
== -1 || regno
== pc_regno
))
1313 struct tracepoint
*tp
= get_tracepoint (get_tracepoint_number ());
1315 if (tp
!= NULL
&& tp
->base
.loc
)
1317 /* But don't try to guess if tracepoint is multi-location... */
1318 if (tp
->base
.loc
->next
!= NULL
)
1320 warning (_("Tracepoint %d has multiple "
1321 "locations, cannot infer $pc"),
1325 /* ... or does while-stepping. */
1326 if (tp
->step_count
> 0)
1328 warning (_("Tracepoint %d does while-stepping, "
1329 "cannot infer $pc"),
1334 store_unsigned_integer (regs
, register_size (gdbarch
, pc_regno
),
1335 gdbarch_byte_order (gdbarch
),
1336 tp
->base
.loc
->address
);
1337 regcache_raw_supply (regcache
, pc_regno
, regs
);
1342 /* This is the implementation of target_ops method to_xfer_partial.
1343 Iterate over events whose name is "memory" in
1344 current frame, extract the address and length from events. If
1345 OFFSET is within the range, read the contents from events to
1349 ctf_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1350 const char *annex
, gdb_byte
*readbuf
,
1351 const gdb_byte
*writebuf
, ULONGEST offset
,
1354 /* We're only doing regular memory for now. */
1355 if (object
!= TARGET_OBJECT_MEMORY
)
1358 if (readbuf
== NULL
)
1359 error (_("ctf_xfer_partial: trace file is read-only"));
1361 if (get_traceframe_number () != -1)
1363 struct bt_iter_pos
*pos
;
1366 gdb_assert (ctf_iter
!= NULL
);
1367 /* Save the current position. */
1368 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1369 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1371 /* Iterate through the traceframe's blocks, looking for
1378 enum bfd_endian byte_order
1379 = gdbarch_byte_order (target_gdbarch ());
1380 const struct bt_definition
*scope
;
1381 const struct bt_definition
*def
;
1382 struct bt_ctf_event
*event
1383 = bt_ctf_iter_read_event (ctf_iter
);
1384 const char *name
= bt_ctf_event_name (event
);
1386 if (strcmp (name
, "frame") == 0)
1388 else if (strcmp (name
, "memory") != 0)
1390 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1396 scope
= bt_ctf_get_top_level_scope (event
,
1399 def
= bt_ctf_get_field (event
, scope
, "address");
1400 maddr
= bt_ctf_get_uint64 (def
);
1401 def
= bt_ctf_get_field (event
, scope
, "length");
1402 mlen
= (uint16_t) bt_ctf_get_uint64 (def
);
1404 /* If the block includes the first part of the desired
1405 range, return as much it has; GDB will re-request the
1406 remainder, which might be in a different block of this
1408 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
1410 const struct bt_definition
*array
1411 = bt_ctf_get_field (event
, scope
, "contents");
1412 const struct bt_declaration
*decl
1413 = bt_ctf_get_decl_from_def (array
);
1417 contents
= xmalloc (mlen
);
1419 for (k
= 0; k
< mlen
; k
++)
1421 const struct bt_definition
*element
1422 = bt_ctf_get_index (event
, array
, k
);
1424 contents
[k
] = (gdb_byte
) bt_ctf_get_uint64 (element
);
1427 amt
= (maddr
+ mlen
) - offset
;
1431 memcpy (readbuf
, &contents
[offset
- maddr
], amt
);
1435 /* Restore the position. */
1436 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1441 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1445 /* Restore the position. */
1446 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1449 /* It's unduly pedantic to refuse to look at the executable for
1450 read-only pieces; so do the equivalent of readonly regions aka
1452 if (exec_bfd
!= NULL
)
1458 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
1460 if ((s
->flags
& SEC_LOAD
) == 0
1461 || (s
->flags
& SEC_READONLY
) == 0)
1465 size
= bfd_get_section_size (s
);
1466 if (vma
<= offset
&& offset
< (vma
+ size
))
1470 amt
= (vma
+ size
) - offset
;
1474 amt
= bfd_get_section_contents (exec_bfd
, s
,
1475 readbuf
, offset
- vma
, amt
);
1481 /* Indicate failure to find the requested memory block. */
1485 /* This is the implementation of target_ops method
1486 to_get_trace_state_variable_value.
1487 Iterate over events whose name is "tsv" in current frame. When the
1488 trace variable is found, set the value of it to *VAL and return
1489 true, otherwise return false. */
1492 ctf_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
1494 struct bt_iter_pos
*pos
;
1497 gdb_assert (ctf_iter
!= NULL
);
1498 /* Save the current position. */
1499 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1500 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1502 /* Iterate through the traceframe's blocks, looking for 'V'
1506 struct bt_ctf_event
*event
1507 = bt_ctf_iter_read_event (ctf_iter
);
1508 const char *name
= bt_ctf_event_name (event
);
1510 if (name
== NULL
|| strcmp (name
, "frame") == 0)
1512 else if (strcmp (name
, "tsv") == 0)
1514 const struct bt_definition
*scope
;
1515 const struct bt_definition
*def
;
1517 scope
= bt_ctf_get_top_level_scope (event
,
1520 def
= bt_ctf_get_field (event
, scope
, "num");
1521 if (tsvnum
== (int32_t) bt_ctf_get_uint64 (def
))
1523 def
= bt_ctf_get_field (event
, scope
, "val");
1524 *val
= bt_ctf_get_uint64 (def
);
1530 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1534 /* Restore the position. */
1535 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1540 /* Return the tracepoint number in "frame" event. */
1543 ctf_get_tpnum_from_frame_event (struct bt_ctf_event
*event
)
1545 /* The packet context of events has a field "tpnum". */
1546 const struct bt_definition
*scope
1547 = bt_ctf_get_top_level_scope (event
, BT_STREAM_PACKET_CONTEXT
);
1549 = bt_ctf_get_uint64 (bt_ctf_get_field (event
, scope
, "tpnum"));
1554 /* Return the address at which the current frame was collected. */
1557 ctf_get_traceframe_address (void)
1559 struct bt_ctf_event
*event
= NULL
;
1560 struct bt_iter_pos
*pos
;
1563 gdb_assert (ctf_iter
!= NULL
);
1564 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1565 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1570 struct bt_ctf_event
*event1
;
1572 event1
= bt_ctf_iter_read_event (ctf_iter
);
1574 name
= bt_ctf_event_name (event1
);
1578 else if (strcmp (name
, "frame") == 0)
1584 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1590 int tpnum
= ctf_get_tpnum_from_frame_event (event
);
1591 struct tracepoint
*tp
1592 = get_tracepoint_by_number_on_target (tpnum
);
1594 if (tp
&& tp
->base
.loc
)
1595 addr
= tp
->base
.loc
->address
;
1598 /* Restore the position. */
1599 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1604 /* This is the implementation of target_ops method to_trace_find.
1605 Iterate the events whose name is "frame", extract the tracepoint
1606 number in it. Return traceframe number when matched. */
1609 ctf_trace_find (enum trace_find_type type
, int num
,
1610 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
1615 struct bt_iter_pos pos
;
1624 gdb_assert (ctf_iter
!= NULL
);
1625 /* Set iterator back to the start. */
1626 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), start_pos
);
1631 struct bt_ctf_event
*event
;
1634 event
= bt_ctf_iter_read_event (ctf_iter
);
1636 name
= bt_ctf_event_name (event
);
1638 if (event
== NULL
|| name
== NULL
)
1641 if (strcmp (name
, "frame") == 0)
1645 if (type
== tfind_number
)
1647 /* Looking for a specific trace frame. */
1653 /* Start from the _next_ trace frame. */
1654 if (tfnum
> get_traceframe_number ())
1660 struct tracepoint
*tp
= get_tracepoint (num
);
1663 && (tp
->number_on_target
1664 == ctf_get_tpnum_from_frame_event (event
)))
1669 tfaddr
= ctf_get_traceframe_address ();
1670 if (tfaddr
== addr1
)
1674 tfaddr
= ctf_get_traceframe_address ();
1675 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
1679 tfaddr
= ctf_get_traceframe_address ();
1680 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
1684 internal_error (__FILE__
, __LINE__
, _("unknown tfind type"));
1691 *tpp
= ctf_get_tpnum_from_frame_event (event
);
1693 /* Skip the event "frame". */
1694 bt_iter_next (bt_ctf_get_iter (ctf_iter
));
1701 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1708 /* This is the implementation of target_ops method to_has_stack.
1709 The target has a stack when GDB has already selected one trace
1713 ctf_has_stack (struct target_ops
*ops
)
1715 return get_traceframe_number () != -1;
1718 /* This is the implementation of target_ops method to_has_registers.
1719 The target has registers when GDB has already selected one trace
1723 ctf_has_registers (struct target_ops
*ops
)
1725 return get_traceframe_number () != -1;
1728 /* This is the implementation of target_ops method to_traceframe_info.
1729 Iterate the events whose name is "memory", in current
1730 frame, extract memory range information, and return them in
1733 static struct traceframe_info
*
1734 ctf_traceframe_info (void)
1736 struct traceframe_info
*info
= XCNEW (struct traceframe_info
);
1738 struct bt_iter_pos
*pos
;
1740 gdb_assert (ctf_iter
!= NULL
);
1741 /* Save the current position. */
1742 pos
= bt_iter_get_pos (bt_ctf_get_iter (ctf_iter
));
1743 gdb_assert (pos
->type
== BT_SEEK_RESTORE
);
1747 struct bt_ctf_event
*event
1748 = bt_ctf_iter_read_event (ctf_iter
);
1750 name
= bt_ctf_event_name (event
);
1752 if (name
== NULL
|| strcmp (name
, "register") == 0
1753 || strcmp (name
, "frame") == 0)
1755 else if (strcmp (name
, "memory") == 0)
1757 const struct bt_definition
*scope
1758 = bt_ctf_get_top_level_scope (event
,
1760 const struct bt_definition
*def
;
1761 struct mem_range
*r
;
1763 r
= VEC_safe_push (mem_range_s
, info
->memory
, NULL
);
1764 def
= bt_ctf_get_field (event
, scope
, "address");
1765 r
->start
= bt_ctf_get_uint64 (def
);
1767 def
= bt_ctf_get_field (event
, scope
, "length");
1768 r
->length
= (uint16_t) bt_ctf_get_uint64 (def
);
1770 else if (strcmp (name
, "tsv") == 0)
1773 const struct bt_definition
*scope
1774 = bt_ctf_get_top_level_scope (event
,
1776 const struct bt_definition
*def
;
1778 def
= bt_ctf_get_field (event
, scope
, "num");
1779 vnum
= (int) bt_ctf_get_int64 (def
);
1780 VEC_safe_push (int, info
->tvars
, vnum
);
1784 warning (_("Unhandled trace block type (%s) "
1785 "while building trace frame info."),
1789 if (bt_iter_next (bt_ctf_get_iter (ctf_iter
)) < 0)
1792 while (name
!= NULL
&& strcmp (name
, "frame") != 0);
1794 /* Restore the position. */
1795 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter
), pos
);
1800 /* This is the implementation of target_ops method to_get_trace_status.
1801 The trace status for a file is that tracing can never be run. */
1804 ctf_get_trace_status (struct trace_status
*ts
)
1806 /* Other bits of trace status were collected as part of opening the
1807 trace files, so nothing to do here. */
1815 memset (&ctf_ops
, 0, sizeof (ctf_ops
));
1817 ctf_ops
.to_shortname
= "ctf";
1818 ctf_ops
.to_longname
= "CTF file";
1819 ctf_ops
.to_doc
= "Use a CTF directory as a target.\n\
1820 Specify the filename of the CTF directory.";
1821 ctf_ops
.to_open
= ctf_open
;
1822 ctf_ops
.to_close
= ctf_close
;
1823 ctf_ops
.to_fetch_registers
= ctf_fetch_registers
;
1824 ctf_ops
.to_xfer_partial
= ctf_xfer_partial
;
1825 ctf_ops
.to_files_info
= ctf_files_info
;
1826 ctf_ops
.to_get_trace_status
= ctf_get_trace_status
;
1827 ctf_ops
.to_trace_find
= ctf_trace_find
;
1828 ctf_ops
.to_get_trace_state_variable_value
1829 = ctf_get_trace_state_variable_value
;
1830 ctf_ops
.to_stratum
= process_stratum
;
1831 ctf_ops
.to_has_stack
= ctf_has_stack
;
1832 ctf_ops
.to_has_registers
= ctf_has_registers
;
1833 ctf_ops
.to_traceframe_info
= ctf_traceframe_info
;
1834 ctf_ops
.to_magic
= OPS_MAGIC
;
1839 /* -Wmissing-prototypes */
1841 extern initialize_file_ftype _initialize_ctf
;
1843 /* module initialization */
1846 _initialize_ctf (void)
1848 #if HAVE_LIBBABELTRACE
1851 add_target_with_completer (&ctf_ops
, filename_completer
);