3 Copyright (C) 2012-2013 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"
31 /* GDB saves trace buffers and other information (such as trace
32 status) got from the remote target into Common Trace Format (CTF).
33 The following types of information are expected to save in CTF:
35 1. The length (in bytes) of register cache. Event "register" will
36 be defined in metadata, which includes the length.
38 2. Trace status. Event "status" is defined in metadata, which
39 includes all aspects of trace status.
41 3. Uploaded trace variables. Event "tsv_def" is defined in
42 metadata, which is about all aspects of a uploaded trace variable.
43 Uploaded tracepoints. Event "tp_def" is defined in meta, which
44 is about all aspects of an uploaded tracepoint. Note that the
45 "sequence" (a CTF type, which is a dynamically-sized array.) is
46 used for "actions" "step_actions" and "cmd_strings".
48 4. Trace frames. Each trace frame is composed by several blocks
49 of different types ('R', 'M', 'V'). One trace frame is saved in
50 one CTF packet and the blocks of this frame are saved as events.
51 4.1: The trace frame related information (such as the number of
52 tracepoint associated with this frame) is saved in the packet
54 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
55 "register" and "tsv" respectively.
56 4.3: When iterating over events, babeltrace can't tell iterator
57 goes to a new packet, so we need a marker or anchor to tell GDB
58 that iterator goes into a new packet or frame. We define event
61 #define CTF_MAGIC 0xC1FC1FC1
62 #define CTF_SAVE_MAJOR 1
63 #define CTF_SAVE_MINOR 8
65 #define CTF_METADATA_NAME "metadata"
66 #define CTF_DATASTREAM_NAME "datastream"
68 /* Reserved event id. */
70 #define CTF_EVENT_ID_REGISTER 0
71 #define CTF_EVENT_ID_TSV 1
72 #define CTF_EVENT_ID_MEMORY 2
73 #define CTF_EVENT_ID_FRAME 3
74 #define CTF_EVENT_ID_STATUS 4
75 #define CTF_EVENT_ID_TSV_DEF 5
76 #define CTF_EVENT_ID_TP_DEF 6
78 /* The state kept while writing the CTF datastream file. */
80 struct trace_write_handler
82 /* File descriptor of metadata. */
84 /* File descriptor of traceframes. */
87 /* This is the content size of the current packet. */
90 /* This is the start offset of current packet. */
94 /* Write metadata in FORMAT. */
97 ctf_save_write_metadata (struct trace_write_handler *handler,
98 const char *format, ...)
102 va_start (args, format);
103 if (vfprintf (handler->metadata_fd, format, args) < 0)
104 error (_("Unable to write metadata file (%s)"),
105 safe_strerror (errno));
109 /* Write BUF of length SIZE to datastream file represented by
113 ctf_save_write (struct trace_write_handler *handler,
114 const gdb_byte *buf, size_t size)
116 if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
117 error (_("Unable to write file for saving trace data (%s)"),
118 safe_strerror (errno));
120 handler->content_size += size;
125 /* Write a unsigned 32-bit integer to datastream file represented by
128 #define ctf_save_write_uint32(HANDLER, U32) \
129 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
131 /* Write a signed 32-bit integer to datastream file represented by
134 #define ctf_save_write_int32(HANDLER, INT32) \
135 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
137 /* Set datastream file position. Update HANDLER->content_size
138 if WHENCE is SEEK_CUR. */
141 ctf_save_fseek (struct trace_write_handler *handler, long offset,
144 gdb_assert (whence != SEEK_END);
145 gdb_assert (whence != SEEK_SET
146 || offset <= handler->content_size + handler->packet_start);
148 if (fseek (handler->datastream_fd, offset, whence))
149 error (_("Unable to seek file for saving trace data (%s)"),
150 safe_strerror (errno));
152 if (whence == SEEK_CUR)
153 handler->content_size += offset;
158 /* Change the datastream file position to align on ALIGN_SIZE,
159 and write BUF to datastream file. The size of BUF is SIZE. */
162 ctf_save_align_write (struct trace_write_handler *handler,
164 size_t size, size_t align_size)
167 = (align_up (handler->content_size, align_size)
168 - handler->content_size);
170 if (ctf_save_fseek (handler, offset, SEEK_CUR))
173 if (ctf_save_write (handler, buf, size))
179 /* Write events to next new packet. */
182 ctf_save_next_packet (struct trace_write_handler *handler)
184 handler->packet_start += (handler->content_size + 4);
185 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
186 handler->content_size = 0;
189 /* Write the CTF metadata header. */
192 ctf_save_metadata_header (struct trace_write_handler *handler)
194 const char metadata_fmt[] =
198 " byte_order = %s;\n" /* be or le */
199 " packet.header := struct {\n"
205 " packet.context := struct {\n"
206 " uint32_t content_size;\n"
207 " uint32_t packet_size;\n"
210 " event.header := struct {\n"
215 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
216 CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
217 ctf_save_write_metadata (handler,
218 "typealias integer { size = 8; align = 8; "
219 "signed = false; encoding = ascii;}"
221 ctf_save_write_metadata (handler,
222 "typealias integer { size = 8; align = 8; "
225 ctf_save_write_metadata (handler,
226 "typealias integer { size = 16; align = 16;"
227 "signed = false; } := uint16_t;\n");
228 ctf_save_write_metadata (handler,
229 "typealias integer { size = 32; align = 32;"
230 "signed = false; } := uint32_t;\n");
231 ctf_save_write_metadata (handler,
232 "typealias integer { size = 64; align = 64;"
233 "signed = false; base = hex;}"
235 ctf_save_write_metadata (handler,
236 "typealias integer { size = 32; align = 32;"
237 "signed = true; } := int32_t;\n");
238 ctf_save_write_metadata (handler,
239 "typealias integer { size = 64; align = 64;"
240 "signed = true; } := int64_t;\n");
241 ctf_save_write_metadata (handler,
242 "typealias string { encoding = ascii;"
244 ctf_save_write_metadata (handler, "\n");
246 /* Get the byte order of the host and write CTF data in this byte
249 #define HOST_ENDIANNESS "be"
251 #define HOST_ENDIANNESS "le"
254 ctf_save_write_metadata (handler, metadata_fmt,
255 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
257 ctf_save_write_metadata (handler, "\n");
260 /* CTF trace writer. */
262 struct ctf_trace_file_writer
264 struct trace_file_writer base;
266 /* States related to writing CTF trace file. */
267 struct trace_write_handler tcs;
270 /* This is the implementation of trace_file_write_ops method
274 ctf_dtor (struct trace_file_writer *self)
276 struct ctf_trace_file_writer *writer
277 = (struct ctf_trace_file_writer *) self;
279 if (writer->tcs.metadata_fd != NULL)
280 fclose (writer->tcs.metadata_fd);
282 if (writer->tcs.datastream_fd != NULL)
283 fclose (writer->tcs.datastream_fd);
287 /* This is the implementation of trace_file_write_ops method
291 ctf_target_save (struct trace_file_writer *self,
294 /* Don't support save trace file to CTF format in the target. */
300 #define mkdir(pathname, mode) mkdir (pathname)
303 /* This is the implementation of trace_file_write_ops method
304 start. It creates the directory DIRNAME, metadata and datastream
308 ctf_start (struct trace_file_writer *self, const char *dirname)
311 struct cleanup *old_chain;
312 struct ctf_trace_file_writer *writer
313 = (struct ctf_trace_file_writer *) self;
315 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR
322 | S_IROTH /* Defined in common/gdb_stat.h if not defined. */
328 /* Create DIRNAME. */
329 if (mkdir (dirname, hmode) && errno != EEXIST)
330 error (_("Unable to open directory '%s' for saving trace data (%s)"),
331 dirname, safe_strerror (errno));
333 memset (&writer->tcs, '\0', sizeof (writer->tcs));
335 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
336 old_chain = make_cleanup (xfree, file_name);
338 writer->tcs.metadata_fd = fopen (file_name, "w");
339 if (writer->tcs.metadata_fd == NULL)
340 error (_("Unable to open file '%s' for saving trace data (%s)"),
341 file_name, safe_strerror (errno));
342 do_cleanups (old_chain);
344 ctf_save_metadata_header (&writer->tcs);
346 file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
347 old_chain = make_cleanup (xfree, file_name);
348 writer->tcs.datastream_fd = fopen (file_name, "w");
349 if (writer->tcs.datastream_fd == NULL)
350 error (_("Unable to open file '%s' for saving trace data (%s)"),
351 file_name, safe_strerror (errno));
352 do_cleanups (old_chain);
355 /* This is the implementation of trace_file_write_ops method
356 write_header. Write the types of events on trace variable and
360 ctf_write_header (struct trace_file_writer *self)
362 struct ctf_trace_file_writer *writer
363 = (struct ctf_trace_file_writer *) self;
366 ctf_save_write_metadata (&writer->tcs, "\n");
367 ctf_save_write_metadata (&writer->tcs,
368 "event {\n\tname = \"memory\";\n\tid = %u;\n"
369 "\tfields := struct { \n"
370 "\t\tuint64_t address;\n"
371 "\t\tuint16_t length;\n"
372 "\t\tuint8_t contents[length];\n"
374 "};\n", CTF_EVENT_ID_MEMORY);
376 ctf_save_write_metadata (&writer->tcs, "\n");
377 ctf_save_write_metadata (&writer->tcs,
378 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
379 "\tfields := struct { \n"
380 "\t\tuint64_t val;\n"
381 "\t\tuint32_t num;\n"
383 "};\n", CTF_EVENT_ID_TSV);
385 ctf_save_write_metadata (&writer->tcs, "\n");
386 ctf_save_write_metadata (&writer->tcs,
387 "event {\n\tname = \"frame\";\n\tid = %u;\n"
388 "\tfields := struct { \n"
390 "};\n", CTF_EVENT_ID_FRAME);
392 ctf_save_write_metadata (&writer->tcs, "\n");
393 ctf_save_write_metadata (&writer->tcs,
394 "event {\n\tname = \"tsv_def\";\n"
395 "\tid = %u;\n\tfields := struct { \n"
396 "\t\tint64_t initial_value;\n"
397 "\t\tint32_t number;\n"
398 "\t\tint32_t builtin;\n"
401 "};\n", CTF_EVENT_ID_TSV_DEF);
403 ctf_save_write_metadata (&writer->tcs, "\n");
404 ctf_save_write_metadata (&writer->tcs,
405 "event {\n\tname = \"tp_def\";\n"
406 "\tid = %u;\n\tfields := struct { \n"
407 "\t\tuint64_t addr;\n"
408 "\t\tuint64_t traceframe_usage;\n"
409 "\t\tint32_t number;\n"
410 "\t\tint32_t enabled;\n"
411 "\t\tint32_t step;\n"
412 "\t\tint32_t pass;\n"
413 "\t\tint32_t hit_count;\n"
414 "\t\tint32_t type;\n"
417 "\t\tuint32_t action_num;\n"
418 "\t\tchars actions[action_num];\n"
420 "\t\tuint32_t step_action_num;\n"
421 "\t\tchars step_actions[step_action_num];\n"
423 "\t\tchars at_string;\n"
424 "\t\tchars cond_string;\n"
426 "\t\tuint32_t cmd_num;\n"
427 "\t\tchars cmd_strings[cmd_num];\n"
429 "};\n", CTF_EVENT_ID_TP_DEF);
431 gdb_assert (writer->tcs.content_size == 0);
432 gdb_assert (writer->tcs.packet_start == 0);
434 /* Create a new packet to contain this event. */
435 self->ops->frame_ops->start (self, 0);
438 /* This is the implementation of trace_file_write_ops method
439 write_regblock_type. Write the type of register event in
443 ctf_write_regblock_type (struct trace_file_writer *self, int size)
445 struct ctf_trace_file_writer *writer
446 = (struct ctf_trace_file_writer *) self;
448 ctf_save_write_metadata (&writer->tcs, "\n");
450 ctf_save_write_metadata (&writer->tcs,
451 "event {\n\tname = \"register\";\n\tid = %u;\n"
452 "\tfields := struct { \n"
453 "\t\tascii contents[%d];\n"
456 CTF_EVENT_ID_REGISTER, size);
459 /* This is the implementation of trace_file_write_ops method
463 ctf_write_status (struct trace_file_writer *self,
464 struct trace_status *ts)
466 struct ctf_trace_file_writer *writer
467 = (struct ctf_trace_file_writer *) self;
471 ctf_save_write_metadata (&writer->tcs, "\n");
472 ctf_save_write_metadata (&writer->tcs,
473 "event {\n\tname = \"status\";\n\tid = %u;\n"
474 "\tfields := struct { \n"
475 "\t\tint32_t stop_reason;\n"
476 "\t\tint32_t stopping_tracepoint;\n"
477 "\t\tint32_t traceframe_count;\n"
478 "\t\tint32_t traceframes_created;\n"
479 "\t\tint32_t buffer_free;\n"
480 "\t\tint32_t buffer_size;\n"
481 "\t\tint32_t disconnected_tracing;\n"
482 "\t\tint32_t circular_buffer;\n"
485 CTF_EVENT_ID_STATUS);
487 id = CTF_EVENT_ID_STATUS;
489 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
491 ctf_save_write_int32 (&writer->tcs, ts->stop_reason);
492 ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint);
493 ctf_save_write_int32 (&writer->tcs, ts->traceframe_count);
494 ctf_save_write_int32 (&writer->tcs, ts->traceframes_created);
495 ctf_save_write_int32 (&writer->tcs, ts->buffer_free);
496 ctf_save_write_int32 (&writer->tcs, ts->buffer_size);
497 ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing);
498 ctf_save_write_int32 (&writer->tcs, ts->circular_buffer);
501 /* This is the implementation of trace_file_write_ops method
502 write_uploaded_tsv. */
505 ctf_write_uploaded_tsv (struct trace_file_writer *self,
506 struct uploaded_tsv *tsv)
508 struct ctf_trace_file_writer *writer
509 = (struct ctf_trace_file_writer *) self;
513 const gdb_byte zero = 0;
516 int32 = CTF_EVENT_ID_TSV_DEF;
517 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
520 int64 = tsv->initial_value;
521 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
524 ctf_save_write_int32 (&writer->tcs, tsv->number);
527 ctf_save_write_int32 (&writer->tcs, tsv->builtin);
530 if (tsv->name != NULL)
531 ctf_save_write (&writer->tcs, tsv->name, strlen (tsv->name));
532 ctf_save_write (&writer->tcs, &zero, 1);
535 /* This is the implementation of trace_file_write_ops method
536 write_uploaded_tp. */
539 ctf_write_uploaded_tp (struct trace_file_writer *self,
540 struct uploaded_tp *tp)
542 struct ctf_trace_file_writer *writer
543 = (struct ctf_trace_file_writer *) self;
547 const gdb_byte zero = 0;
552 int32 = CTF_EVENT_ID_TP_DEF;
553 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
557 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
559 /* traceframe_usage */
560 int64 = tp->traceframe_usage;
561 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
564 ctf_save_write_int32 (&writer->tcs, tp->number);
567 ctf_save_write_int32 (&writer->tcs, tp->enabled);
570 ctf_save_write_int32 (&writer->tcs, tp->step);
573 ctf_save_write_int32 (&writer->tcs, tp->pass);
576 ctf_save_write_int32 (&writer->tcs, tp->hit_count);
579 ctf_save_write_int32 (&writer->tcs, tp->type);
582 if (tp->cond != NULL)
583 ctf_save_write (&writer->tcs, tp->cond, strlen (tp->cond));
584 ctf_save_write (&writer->tcs, &zero, 1);
587 u32 = VEC_length (char_ptr, tp->actions);
588 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
589 for (a = 0; VEC_iterate (char_ptr, tp->actions, a, act); ++a)
590 ctf_save_write (&writer->tcs, act, strlen (act) + 1);
593 u32 = VEC_length (char_ptr, tp->step_actions);
594 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
595 for (a = 0; VEC_iterate (char_ptr, tp->step_actions, a, act); ++a)
596 ctf_save_write (&writer->tcs, act, strlen (act) + 1);
599 if (tp->at_string != NULL)
600 ctf_save_write (&writer->tcs, tp->at_string,
601 strlen (tp->at_string));
602 ctf_save_write (&writer->tcs, &zero, 1);
605 if (tp->cond_string != NULL)
606 ctf_save_write (&writer->tcs, tp->cond_string,
607 strlen (tp->cond_string));
608 ctf_save_write (&writer->tcs, &zero, 1);
611 u32 = VEC_length (char_ptr, tp->cmd_strings);
612 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
613 for (a = 0; VEC_iterate (char_ptr, tp->cmd_strings, a, act); ++a)
614 ctf_save_write (&writer->tcs, act, strlen (act) + 1);
618 /* This is the implementation of trace_file_write_ops method
619 write_definition_end. */
622 ctf_write_definition_end (struct trace_file_writer *self)
624 struct ctf_trace_file_writer *writer
625 = (struct ctf_trace_file_writer *) self;
627 self->ops->frame_ops->end (self);
630 /* The minimal file size of data stream. It is required by
633 #define CTF_FILE_MIN_SIZE 4096
635 /* This is the implementation of trace_file_write_ops method
639 ctf_end (struct trace_file_writer *self)
641 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
643 gdb_assert (writer->tcs.content_size == 0);
644 /* The babeltrace requires or assumes that the size of datastream
645 file is greater than 4096 bytes. If we don't generate enough
646 packets and events, create a fake packet which has zero event,
647 to use up the space. */
648 if (writer->tcs.packet_start < CTF_FILE_MIN_SIZE)
654 ctf_save_write_uint32 (&writer->tcs, u32);
658 ctf_save_write_uint32 (&writer->tcs, u32);
662 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
663 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
665 u32 *= TARGET_CHAR_BIT;
666 ctf_save_write_uint32 (&writer->tcs, u32);
670 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
672 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
674 if (CTF_FILE_MIN_SIZE
675 > (writer->tcs.packet_start + writer->tcs.content_size))
679 /* Fake the content size to avoid assertion failure in
681 writer->tcs.content_size = (CTF_FILE_MIN_SIZE
682 - 1 - writer->tcs.packet_start);
683 ctf_save_fseek (&writer->tcs, CTF_FILE_MIN_SIZE - 1,
685 ctf_save_write (&writer->tcs, &b, 1);
690 /* This is the implementation of trace_frame_write_ops method
694 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
696 struct ctf_trace_file_writer *writer
697 = (struct ctf_trace_file_writer *) self;
698 uint32_t id = CTF_EVENT_ID_FRAME;
701 /* Step 1: Write packet context. */
704 ctf_save_write_uint32 (&writer->tcs, u32);
705 /* content_size and packet_size.. We still don't know the value,
707 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
708 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
709 /* Tracepoint number. */
710 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
712 /* Step 2: Write event "frame". */
714 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
717 /* This is the implementation of trace_frame_write_ops method
721 ctf_write_frame_r_block (struct trace_file_writer *self,
722 gdb_byte *buf, int32_t size)
724 struct ctf_trace_file_writer *writer
725 = (struct ctf_trace_file_writer *) self;
726 uint32_t id = CTF_EVENT_ID_REGISTER;
729 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
731 /* array contents. */
732 ctf_save_align_write (&writer->tcs, buf, size, 1);
735 /* This is the implementation of trace_frame_write_ops method
736 write_m_block_header. */
739 ctf_write_frame_m_block_header (struct trace_file_writer *self,
740 uint64_t addr, uint16_t length)
742 struct ctf_trace_file_writer *writer
743 = (struct ctf_trace_file_writer *) self;
744 uint32_t event_id = CTF_EVENT_ID_MEMORY;
747 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
750 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
753 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
756 /* This is the implementation of trace_frame_write_ops method
757 write_m_block_memory. */
760 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
761 gdb_byte *buf, uint16_t length)
763 struct ctf_trace_file_writer *writer
764 = (struct ctf_trace_file_writer *) self;
767 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
770 /* This is the implementation of trace_frame_write_ops method
774 ctf_write_frame_v_block (struct trace_file_writer *self,
775 int32_t num, uint64_t val)
777 struct ctf_trace_file_writer *writer
778 = (struct ctf_trace_file_writer *) self;
779 uint32_t id = CTF_EVENT_ID_TSV;
782 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
785 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
787 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
790 /* This is the implementation of trace_frame_write_ops method
794 ctf_write_frame_end (struct trace_file_writer *self)
796 struct ctf_trace_file_writer *writer
797 = (struct ctf_trace_file_writer *) self;
801 /* Write the content size to packet header. */
802 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
804 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
806 t = writer->tcs.content_size;
807 ctf_save_write_uint32 (&writer->tcs, u32);
809 /* Write the packet size. */
810 u32 += 4 * TARGET_CHAR_BIT;
811 ctf_save_write_uint32 (&writer->tcs, u32);
813 writer->tcs.content_size = t;
815 /* Write zero at the end of the packet. */
816 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
819 ctf_save_write_uint32 (&writer->tcs, u32);
820 writer->tcs.content_size = t;
822 ctf_save_next_packet (&writer->tcs);
825 /* Operations to write various types of trace frames into CTF
828 static const struct trace_frame_write_ops ctf_write_frame_ops =
830 ctf_write_frame_start,
831 ctf_write_frame_r_block,
832 ctf_write_frame_m_block_header,
833 ctf_write_frame_m_block_memory,
834 ctf_write_frame_v_block,
838 /* Operations to write trace buffers into CTF format. */
840 static const struct trace_file_write_ops ctf_write_ops =
846 ctf_write_regblock_type,
848 ctf_write_uploaded_tsv,
849 ctf_write_uploaded_tp,
850 ctf_write_definition_end,
852 &ctf_write_frame_ops,
856 /* Return a trace writer for CTF format. */
858 struct trace_file_writer *
859 ctf_trace_file_writer_new (void)
861 struct ctf_trace_file_writer *writer
862 = xmalloc (sizeof (struct ctf_trace_file_writer));
864 writer->base.ops = &ctf_write_ops;
866 return (struct trace_file_writer *) writer;
869 #if HAVE_LIBBABELTRACE
870 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
871 iterator to iterate over each event in CTF data and APIs to get
872 details of event and packet, so it is very convenient to use
873 libbabeltrace to access events in CTF. */
875 #include <babeltrace/babeltrace.h>
876 #include <babeltrace/ctf/events.h>
877 #include <babeltrace/ctf/iterator.h>
879 /* The struct pointer for current CTF directory. */
880 static struct bt_context *ctx = NULL;
881 static struct bt_ctf_iter *ctf_iter = NULL;
882 /* The position of the first packet containing trace frame. */
883 static struct bt_iter_pos *start_pos;
885 /* The name of CTF directory. */
886 static char *trace_dirname;
888 static struct target_ops ctf_ops;
890 /* Destroy ctf iterator and context. */
895 if (ctf_iter != NULL)
897 bt_ctf_iter_destroy (ctf_iter);
902 bt_context_put (ctx);
907 /* Open CTF trace data in DIRNAME. */
910 ctf_open_dir (char *dirname)
913 struct bt_iter_pos begin_pos;
914 struct bt_iter_pos *pos;
916 ctx = bt_context_create ();
918 error (_("Unable to create bt_context"));
919 ret = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
923 error (_("Unable to use libbabeltrace on directory \"%s\""),
927 begin_pos.type = BT_SEEK_BEGIN;
928 ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL);
929 if (ctf_iter == NULL)
932 error (_("Unable to create bt_iterator"));
935 /* Iterate over events, and look for an event for register block
936 to set trace_regblock_size. */
938 /* Save the current position. */
939 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
940 gdb_assert (pos->type == BT_SEEK_RESTORE);
945 struct bt_ctf_event *event;
947 event = bt_ctf_iter_read_event (ctf_iter);
949 name = bt_ctf_event_name (event);
953 else if (strcmp (name, "register") == 0)
955 const struct bt_definition *scope
956 = bt_ctf_get_top_level_scope (event,
958 const struct bt_definition *array
959 = bt_ctf_get_field (event, scope, "contents");
962 = bt_ctf_get_array_len (bt_ctf_get_decl_from_def (array));
965 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
969 /* Restore the position. */
970 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
973 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
974 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
978 /* EVENT is the "status" event and TS is filled in. */
981 ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts)
983 const struct bt_definition *scope
984 = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS);
986 SET_INT32_FIELD (event, scope, ts, stop_reason);
987 SET_INT32_FIELD (event, scope, ts, stopping_tracepoint);
988 SET_INT32_FIELD (event, scope, ts, traceframe_count);
989 SET_INT32_FIELD (event, scope, ts, traceframes_created);
990 SET_INT32_FIELD (event, scope, ts, buffer_free);
991 SET_INT32_FIELD (event, scope, ts, buffer_size);
992 SET_INT32_FIELD (event, scope, ts, disconnected_tracing);
993 SET_INT32_FIELD (event, scope, ts, circular_buffer);
995 bt_iter_next (bt_ctf_get_iter (ctf_iter));
998 /* Read the events "tsv_def" one by one, extract its contents and fill
999 in the list UPLOADED_TSVS. */
1002 ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
1004 gdb_assert (ctf_iter != NULL);
1008 struct bt_ctf_event *event;
1009 const struct bt_definition *scope;
1010 const struct bt_definition *def;
1012 struct uploaded_tsv *utsv = NULL;
1014 event = bt_ctf_iter_read_event (ctf_iter);
1015 scope = bt_ctf_get_top_level_scope (event,
1016 BT_STREAM_EVENT_HEADER);
1017 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1019 if (event_id != CTF_EVENT_ID_TSV_DEF)
1022 scope = bt_ctf_get_top_level_scope (event,
1025 def = bt_ctf_get_field (event, scope, "number");
1026 utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def),
1029 def = bt_ctf_get_field (event, scope, "builtin");
1030 utsv->builtin = (int32_t) bt_ctf_get_int64 (def);
1031 def = bt_ctf_get_field (event, scope, "initial_value");
1032 utsv->initial_value = bt_ctf_get_int64 (def);
1034 def = bt_ctf_get_field (event, scope, "name");
1035 utsv->name = xstrdup (bt_ctf_get_string (def));
1037 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1043 /* Read the value of element whose index is NUM from CTF and write it
1044 to the corresponding VAR->ARRAY. */
1046 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
1050 const struct bt_definition *def; \
1052 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1055 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1056 for (i = 0; i < u32; i++) \
1058 const struct bt_definition *element \
1059 = bt_ctf_get_index ((EVENT), def, i); \
1061 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1062 xstrdup (bt_ctf_get_string (element))); \
1067 /* Read a string from CTF and set VAR->FIELD. If the length of string
1068 is zero, set VAR->FIELD to NULL. */
1070 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1073 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1077 if (strlen (p) > 0) \
1078 (VAR)->FIELD = xstrdup (p); \
1080 (VAR)->FIELD = NULL; \
1084 /* Read the events "tp_def" one by one, extract its contents and fill
1085 in the list UPLOADED_TPS. */
1088 ctf_read_tp (struct uploaded_tp **uploaded_tps)
1090 gdb_assert (ctf_iter != NULL);
1094 struct bt_ctf_event *event;
1095 const struct bt_definition *scope;
1099 struct uploaded_tp *utp = NULL;
1101 event = bt_ctf_iter_read_event (ctf_iter);
1102 scope = bt_ctf_get_top_level_scope (event,
1103 BT_STREAM_EVENT_HEADER);
1104 u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1106 if (u32 != CTF_EVENT_ID_TP_DEF)
1109 scope = bt_ctf_get_top_level_scope (event,
1111 int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event,
1114 u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1116 utp = get_uploaded_tp (int32, u64, uploaded_tps);
1118 SET_INT32_FIELD (event, scope, utp, enabled);
1119 SET_INT32_FIELD (event, scope, utp, step);
1120 SET_INT32_FIELD (event, scope, utp, pass);
1121 SET_INT32_FIELD (event, scope, utp, hit_count);
1122 SET_INT32_FIELD (event, scope, utp, type);
1124 /* Read 'cmd_strings'. */
1125 SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings);
1126 /* Read 'actions'. */
1127 SET_ARRAY_FIELD (event, scope, utp, action_num, actions);
1128 /* Read 'step_actions'. */
1129 SET_ARRAY_FIELD (event, scope, utp, step_action_num,
1132 SET_STRING_FIELD(event, scope, utp, at_string);
1133 SET_STRING_FIELD(event, scope, utp, cond_string);
1135 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1140 /* This is the implementation of target_ops method to_open. Open CTF
1141 trace data, read trace status, trace state variables and tracepoint
1142 definitions from the first packet. Set the start position at the
1143 second packet which contains events on trace blocks. */
1146 ctf_open (char *dirname, int from_tty)
1148 struct bt_ctf_event *event;
1150 const struct bt_definition *scope;
1151 struct uploaded_tsv *uploaded_tsvs = NULL;
1152 struct uploaded_tp *uploaded_tps = NULL;
1155 error (_("No CTF directory specified."));
1157 ctf_open_dir (dirname);
1159 target_preopen (from_tty);
1161 /* Skip the first packet which about the trace status. The first
1162 event is "frame". */
1163 event = bt_ctf_iter_read_event (ctf_iter);
1164 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1165 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1166 if (event_id != CTF_EVENT_ID_FRAME)
1167 error (_("Wrong event id of the first event"));
1168 /* The second event is "status". */
1169 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1170 event = bt_ctf_iter_read_event (ctf_iter);
1171 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1172 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1173 if (event_id != CTF_EVENT_ID_STATUS)
1174 error (_("Wrong event id of the second event"));
1175 ctf_read_status (event, current_trace_status ());
1177 ctf_read_tsv (&uploaded_tsvs);
1179 ctf_read_tp (&uploaded_tps);
1181 event = bt_ctf_iter_read_event (ctf_iter);
1182 /* EVENT can be NULL if we've already gone to the end of stream of
1186 scope = bt_ctf_get_top_level_scope (event,
1187 BT_STREAM_EVENT_HEADER);
1188 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event,
1190 if (event_id != CTF_EVENT_ID_FRAME)
1191 error (_("Wrong event id of the first event of the second packet"));
1194 start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1195 gdb_assert (start_pos->type == BT_SEEK_RESTORE);
1197 trace_dirname = xstrdup (dirname);
1198 push_target (&ctf_ops);
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. */
1211 xfree (trace_dirname);
1212 trace_dirname = NULL;
1215 /* This is the implementation of target_ops method to_files_info.
1216 Print the directory name of CTF trace data. */
1219 ctf_files_info (struct target_ops *t)
1221 printf_filtered ("\t`%s'\n", trace_dirname);
1224 /* This is the implementation of target_ops method to_fetch_registers.
1225 Iterate over events whose name is "register" in current frame,
1226 extract contents from events, and set REGCACHE with the contents.
1227 If no matched events are found, mark registers unavailable. */
1230 ctf_fetch_registers (struct target_ops *ops,
1231 struct regcache *regcache, int regno)
1233 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1234 int offset, regn, regsize, pc_regno;
1236 struct bt_ctf_event *event = NULL;
1237 struct bt_iter_pos *pos;
1239 /* An uninitialized reg size says we're not going to be
1240 successful at getting register blocks. */
1241 if (trace_regblock_size == 0)
1244 gdb_assert (ctf_iter != NULL);
1245 /* Save the current position. */
1246 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1247 gdb_assert (pos->type == BT_SEEK_RESTORE);
1252 struct bt_ctf_event *event1;
1254 event1 = bt_ctf_iter_read_event (ctf_iter);
1256 name = bt_ctf_event_name (event1);
1258 if (name == NULL || strcmp (name, "frame") == 0)
1260 else if (strcmp (name, "register") == 0)
1266 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1270 /* Restore the position. */
1271 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1275 const struct bt_definition *scope
1276 = bt_ctf_get_top_level_scope (event,
1278 const struct bt_definition *array
1279 = bt_ctf_get_field (event, scope, "contents");
1281 regs = bt_ctf_get_char_array (array);
1282 /* Assume the block is laid out in GDB register number order,
1283 each register with the size that it has in GDB. */
1285 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1287 regsize = register_size (gdbarch, regn);
1288 /* Make sure we stay within block bounds. */
1289 if (offset + regsize >= trace_regblock_size)
1291 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
1295 regcache_raw_supply (regcache, regno, regs + offset);
1298 else if (regno == -1)
1300 regcache_raw_supply (regcache, regn, regs + offset);
1308 regs = alloca (trace_regblock_size);
1310 /* We get here if no register data has been found. Mark registers
1312 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1313 regcache_raw_supply (regcache, regn, NULL);
1315 /* We can often usefully guess that the PC is going to be the same
1316 as the address of the tracepoint. */
1317 pc_regno = gdbarch_pc_regnum (gdbarch);
1318 if (pc_regno >= 0 && (regno == -1 || regno == pc_regno))
1320 struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
1322 if (tp != NULL && tp->base.loc)
1324 /* But don't try to guess if tracepoint is multi-location... */
1325 if (tp->base.loc->next != NULL)
1327 warning (_("Tracepoint %d has multiple "
1328 "locations, cannot infer $pc"),
1332 /* ... or does while-stepping. */
1333 if (tp->step_count > 0)
1335 warning (_("Tracepoint %d does while-stepping, "
1336 "cannot infer $pc"),
1341 store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
1342 gdbarch_byte_order (gdbarch),
1343 tp->base.loc->address);
1344 regcache_raw_supply (regcache, pc_regno, regs);
1349 /* This is the implementation of target_ops method to_xfer_partial.
1350 Iterate over events whose name is "memory" in
1351 current frame, extract the address and length from events. If
1352 OFFSET is within the range, read the contents from events to
1356 ctf_xfer_partial (struct target_ops *ops, enum target_object object,
1357 const char *annex, gdb_byte *readbuf,
1358 const gdb_byte *writebuf, ULONGEST offset,
1361 /* We're only doing regular memory for now. */
1362 if (object != TARGET_OBJECT_MEMORY)
1365 if (readbuf == NULL)
1366 error (_("ctf_xfer_partial: trace file is read-only"));
1368 if (get_traceframe_number () != -1)
1370 struct bt_iter_pos *pos;
1373 gdb_assert (ctf_iter != NULL);
1374 /* Save the current position. */
1375 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1376 gdb_assert (pos->type == BT_SEEK_RESTORE);
1378 /* Iterate through the traceframe's blocks, looking for
1385 enum bfd_endian byte_order
1386 = gdbarch_byte_order (target_gdbarch ());
1387 const struct bt_definition *scope;
1388 const struct bt_definition *def;
1389 struct bt_ctf_event *event
1390 = bt_ctf_iter_read_event (ctf_iter);
1391 const char *name = bt_ctf_event_name (event);
1393 if (strcmp (name, "frame") == 0)
1395 else if (strcmp (name, "memory") != 0)
1397 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1403 scope = bt_ctf_get_top_level_scope (event,
1406 def = bt_ctf_get_field (event, scope, "address");
1407 maddr = bt_ctf_get_uint64 (def);
1408 def = bt_ctf_get_field (event, scope, "length");
1409 mlen = (uint16_t) bt_ctf_get_uint64 (def);
1411 /* If the block includes the first part of the desired
1412 range, return as much it has; GDB will re-request the
1413 remainder, which might be in a different block of this
1415 if (maddr <= offset && offset < (maddr + mlen))
1417 const struct bt_definition *array
1418 = bt_ctf_get_field (event, scope, "contents");
1419 const struct bt_declaration *decl
1420 = bt_ctf_get_decl_from_def (array);
1424 contents = xmalloc (mlen);
1426 for (k = 0; k < mlen; k++)
1428 const struct bt_definition *element
1429 = bt_ctf_get_index (event, array, k);
1431 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
1434 amt = (maddr + mlen) - offset;
1438 memcpy (readbuf, &contents[offset - maddr], amt);
1442 /* Restore the position. */
1443 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1448 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1452 /* Restore the position. */
1453 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1456 /* It's unduly pedantic to refuse to look at the executable for
1457 read-only pieces; so do the equivalent of readonly regions aka
1459 if (exec_bfd != NULL)
1465 for (s = exec_bfd->sections; s; s = s->next)
1467 if ((s->flags & SEC_LOAD) == 0
1468 || (s->flags & SEC_READONLY) == 0)
1472 size = bfd_get_section_size (s);
1473 if (vma <= offset && offset < (vma + size))
1477 amt = (vma + size) - offset;
1481 amt = bfd_get_section_contents (exec_bfd, s,
1482 readbuf, offset - vma, amt);
1488 /* Indicate failure to find the requested memory block. */
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 (int tsvnum, LONGEST *val)
1501 struct bt_iter_pos *pos;
1504 gdb_assert (ctf_iter != NULL);
1505 /* Save the current position. */
1506 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1507 gdb_assert (pos->type == BT_SEEK_RESTORE);
1509 /* Iterate through the traceframe's blocks, looking for 'V'
1513 struct bt_ctf_event *event
1514 = bt_ctf_iter_read_event (ctf_iter);
1515 const char *name = bt_ctf_event_name (event);
1517 if (name == NULL || strcmp (name, "frame") == 0)
1519 else if (strcmp (name, "tsv") == 0)
1521 const struct bt_definition *scope;
1522 const struct bt_definition *def;
1524 scope = bt_ctf_get_top_level_scope (event,
1527 def = bt_ctf_get_field (event, scope, "num");
1528 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
1530 def = bt_ctf_get_field (event, scope, "val");
1531 *val = bt_ctf_get_uint64 (def);
1537 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1541 /* Restore the position. */
1542 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1547 /* Return the tracepoint number in "frame" event. */
1550 ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
1552 /* The packet context of events has a field "tpnum". */
1553 const struct bt_definition *scope
1554 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
1556 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
1561 /* Return the address at which the current frame was collected. */
1564 ctf_get_traceframe_address (void)
1566 struct bt_ctf_event *event = NULL;
1567 struct bt_iter_pos *pos;
1570 gdb_assert (ctf_iter != NULL);
1571 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1572 gdb_assert (pos->type == BT_SEEK_RESTORE);
1577 struct bt_ctf_event *event1;
1579 event1 = bt_ctf_iter_read_event (ctf_iter);
1581 name = bt_ctf_event_name (event1);
1585 else if (strcmp (name, "frame") == 0)
1591 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1597 int tpnum = ctf_get_tpnum_from_frame_event (event);
1598 struct tracepoint *tp
1599 = get_tracepoint_by_number_on_target (tpnum);
1601 if (tp && tp->base.loc)
1602 addr = tp->base.loc->address;
1605 /* Restore the position. */
1606 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1611 /* This is the implementation of target_ops method to_trace_find.
1612 Iterate the events whose name is "frame", extract the tracepoint
1613 number in it. Return traceframe number when matched. */
1616 ctf_trace_find (enum trace_find_type type, int num,
1617 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
1622 struct bt_iter_pos pos;
1631 gdb_assert (ctf_iter != NULL);
1632 /* Set iterator back to the start. */
1633 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
1638 struct bt_ctf_event *event;
1641 event = bt_ctf_iter_read_event (ctf_iter);
1643 name = bt_ctf_event_name (event);
1645 if (event == NULL || name == NULL)
1648 if (strcmp (name, "frame") == 0)
1652 if (type == tfind_number)
1654 /* Looking for a specific trace frame. */
1660 /* Start from the _next_ trace frame. */
1661 if (tfnum > get_traceframe_number ())
1667 struct tracepoint *tp = get_tracepoint (num);
1670 && (tp->number_on_target
1671 == ctf_get_tpnum_from_frame_event (event)))
1676 tfaddr = ctf_get_traceframe_address ();
1677 if (tfaddr == addr1)
1681 tfaddr = ctf_get_traceframe_address ();
1682 if (addr1 <= tfaddr && tfaddr <= addr2)
1686 tfaddr = ctf_get_traceframe_address ();
1687 if (!(addr1 <= tfaddr && tfaddr <= addr2))
1691 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
1698 *tpp = ctf_get_tpnum_from_frame_event (event);
1700 /* Skip the event "frame". */
1701 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1708 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1715 /* This is the implementation of target_ops method to_has_stack.
1716 The target has a stack when GDB has already selected one trace
1720 ctf_has_stack (struct target_ops *ops)
1722 return get_traceframe_number () != -1;
1725 /* This is the implementation of target_ops method to_has_registers.
1726 The target has registers when GDB has already selected one trace
1730 ctf_has_registers (struct target_ops *ops)
1732 return get_traceframe_number () != -1;
1735 /* This is the implementation of target_ops method to_traceframe_info.
1736 Iterate the events whose name is "memory", in current
1737 frame, extract memory range information, and return them in
1740 static struct traceframe_info *
1741 ctf_traceframe_info (void)
1743 struct traceframe_info *info = XCNEW (struct traceframe_info);
1745 struct bt_iter_pos *pos;
1747 gdb_assert (ctf_iter != NULL);
1748 /* Save the current position. */
1749 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1750 gdb_assert (pos->type == BT_SEEK_RESTORE);
1754 struct bt_ctf_event *event
1755 = bt_ctf_iter_read_event (ctf_iter);
1757 name = bt_ctf_event_name (event);
1759 if (name == NULL || strcmp (name, "register") == 0
1760 || strcmp (name, "frame") == 0)
1762 else if (strcmp (name, "memory") == 0)
1764 const struct bt_definition *scope
1765 = bt_ctf_get_top_level_scope (event,
1767 const struct bt_definition *def;
1768 struct mem_range *r;
1770 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1771 def = bt_ctf_get_field (event, scope, "address");
1772 r->start = bt_ctf_get_uint64 (def);
1774 def = bt_ctf_get_field (event, scope, "length");
1775 r->length = (uint16_t) bt_ctf_get_uint64 (def);
1779 warning (_("Unhandled trace block type (%s) "
1780 "while building trace frame info."),
1784 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1787 while (name != NULL && strcmp (name, "frame") != 0);
1789 /* Restore the position. */
1790 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1795 /* This is the implementation of target_ops method to_get_trace_status.
1796 The trace status for a file is that tracing can never be run. */
1799 ctf_get_trace_status (struct trace_status *ts)
1801 /* Other bits of trace status were collected as part of opening the
1802 trace files, so nothing to do here. */
1810 memset (&ctf_ops, 0, sizeof (ctf_ops));
1812 ctf_ops.to_shortname = "ctf";
1813 ctf_ops.to_longname = "CTF file";
1814 ctf_ops.to_doc = "Use a CTF directory as a target.\n\
1815 Specify the filename of the CTF directory.";
1816 ctf_ops.to_open = ctf_open;
1817 ctf_ops.to_close = ctf_close;
1818 ctf_ops.to_fetch_registers = ctf_fetch_registers;
1819 ctf_ops.to_xfer_partial = ctf_xfer_partial;
1820 ctf_ops.to_files_info = ctf_files_info;
1821 ctf_ops.to_get_trace_status = ctf_get_trace_status;
1822 ctf_ops.to_trace_find = ctf_trace_find;
1823 ctf_ops.to_get_trace_state_variable_value
1824 = ctf_get_trace_state_variable_value;
1825 ctf_ops.to_stratum = process_stratum;
1826 ctf_ops.to_has_stack = ctf_has_stack;
1827 ctf_ops.to_has_registers = ctf_has_registers;
1828 ctf_ops.to_traceframe_info = ctf_traceframe_info;
1829 ctf_ops.to_magic = OPS_MAGIC;
1834 /* -Wmissing-prototypes */
1836 extern initialize_file_ftype _initialize_ctf;
1838 /* module initialization */
1841 _initialize_ctf (void)
1843 #if HAVE_LIBBABELTRACE
1846 add_target (&ctf_ops);