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"
34 /* GDB saves trace buffers and other information (such as trace
35 status) got from the remote target into Common Trace Format (CTF).
36 The following types of information are expected to save in CTF:
38 1. The length (in bytes) of register cache. Event "register" will
39 be defined in metadata, which includes the length.
41 2. Trace status. Event "status" is defined in metadata, which
42 includes all aspects of trace status.
44 3. Uploaded trace variables. Event "tsv_def" is defined in
45 metadata, which is about all aspects of a uploaded trace variable.
46 Uploaded tracepoints. Event "tp_def" is defined in meta, which
47 is about all aspects of an uploaded tracepoint. Note that the
48 "sequence" (a CTF type, which is a dynamically-sized array.) is
49 used for "actions" "step_actions" and "cmd_strings".
51 4. Trace frames. Each trace frame is composed by several blocks
52 of different types ('R', 'M', 'V'). One trace frame is saved in
53 one CTF packet and the blocks of this frame are saved as events.
54 4.1: The trace frame related information (such as the number of
55 tracepoint associated with this frame) is saved in the packet
57 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
58 "register" and "tsv" respectively.
59 4.3: When iterating over events, babeltrace can't tell iterator
60 goes to a new packet, so we need a marker or anchor to tell GDB
61 that iterator goes into a new packet or frame. We define event
64 #define CTF_MAGIC 0xC1FC1FC1
65 #define CTF_SAVE_MAJOR 1
66 #define CTF_SAVE_MINOR 8
68 #define CTF_METADATA_NAME "metadata"
69 #define CTF_DATASTREAM_NAME "datastream"
71 /* Reserved event id. */
73 #define CTF_EVENT_ID_REGISTER 0
74 #define CTF_EVENT_ID_TSV 1
75 #define CTF_EVENT_ID_MEMORY 2
76 #define CTF_EVENT_ID_FRAME 3
77 #define CTF_EVENT_ID_STATUS 4
78 #define CTF_EVENT_ID_TSV_DEF 5
79 #define CTF_EVENT_ID_TP_DEF 6
83 /* The state kept while writing the CTF datastream file. */
85 struct trace_write_handler
87 /* File descriptor of metadata. */
89 /* File descriptor of traceframes. */
92 /* This is the content size of the current packet. */
95 /* This is the start offset of current packet. */
99 /* Write metadata in FORMAT. */
102 ctf_save_write_metadata (struct trace_write_handler *handler,
103 const char *format, ...)
107 va_start (args, format);
108 if (vfprintf (handler->metadata_fd, format, args) < 0)
109 error (_("Unable to write metadata file (%s)"),
110 safe_strerror (errno));
114 /* Write BUF of length SIZE to datastream file represented by
118 ctf_save_write (struct trace_write_handler *handler,
119 const gdb_byte *buf, size_t size)
121 if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
122 error (_("Unable to write file for saving trace data (%s)"),
123 safe_strerror (errno));
125 handler->content_size += size;
130 /* Write a unsigned 32-bit integer to datastream file represented by
133 #define ctf_save_write_uint32(HANDLER, U32) \
134 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
136 /* Write a signed 32-bit integer to datastream file represented by
139 #define ctf_save_write_int32(HANDLER, INT32) \
140 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
142 /* Set datastream file position. Update HANDLER->content_size
143 if WHENCE is SEEK_CUR. */
146 ctf_save_fseek (struct trace_write_handler *handler, long offset,
149 gdb_assert (whence != SEEK_END);
150 gdb_assert (whence != SEEK_SET
151 || offset <= handler->content_size + handler->packet_start);
153 if (fseek (handler->datastream_fd, offset, whence))
154 error (_("Unable to seek file for saving trace data (%s)"),
155 safe_strerror (errno));
157 if (whence == SEEK_CUR)
158 handler->content_size += offset;
163 /* Change the datastream file position to align on ALIGN_SIZE,
164 and write BUF to datastream file. The size of BUF is SIZE. */
167 ctf_save_align_write (struct trace_write_handler *handler,
169 size_t size, size_t align_size)
172 = (align_up (handler->content_size, align_size)
173 - handler->content_size);
175 if (ctf_save_fseek (handler, offset, SEEK_CUR))
178 if (ctf_save_write (handler, buf, size))
184 /* Write events to next new packet. */
187 ctf_save_next_packet (struct trace_write_handler *handler)
189 handler->packet_start += (handler->content_size + 4);
190 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
191 handler->content_size = 0;
194 /* Write the CTF metadata header. */
197 ctf_save_metadata_header (struct trace_write_handler *handler)
199 const char metadata_fmt[] =
203 " byte_order = %s;\n" /* be or le */
204 " packet.header := struct {\n"
210 " packet.context := struct {\n"
211 " uint32_t content_size;\n"
212 " uint32_t packet_size;\n"
215 " event.header := struct {\n"
220 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
221 CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
222 ctf_save_write_metadata (handler,
223 "typealias integer { size = 8; align = 8; "
224 "signed = false; encoding = ascii;}"
226 ctf_save_write_metadata (handler,
227 "typealias integer { size = 8; align = 8; "
230 ctf_save_write_metadata (handler,
231 "typealias integer { size = 16; align = 16;"
232 "signed = false; } := uint16_t;\n");
233 ctf_save_write_metadata (handler,
234 "typealias integer { size = 32; align = 32;"
235 "signed = false; } := uint32_t;\n");
236 ctf_save_write_metadata (handler,
237 "typealias integer { size = 64; align = 64;"
238 "signed = false; base = hex;}"
240 ctf_save_write_metadata (handler,
241 "typealias integer { size = 32; align = 32;"
242 "signed = true; } := int32_t;\n");
243 ctf_save_write_metadata (handler,
244 "typealias integer { size = 64; align = 64;"
245 "signed = true; } := int64_t;\n");
246 ctf_save_write_metadata (handler,
247 "typealias string { encoding = ascii;"
249 ctf_save_write_metadata (handler, "\n");
251 /* Get the byte order of the host and write CTF data in this byte
254 #define HOST_ENDIANNESS "be"
256 #define HOST_ENDIANNESS "le"
259 ctf_save_write_metadata (handler, metadata_fmt,
260 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
262 ctf_save_write_metadata (handler, "\n");
265 /* CTF trace writer. */
267 struct ctf_trace_file_writer
269 struct trace_file_writer base;
271 /* States related to writing CTF trace file. */
272 struct trace_write_handler tcs;
275 /* This is the implementation of trace_file_write_ops method
279 ctf_dtor (struct trace_file_writer *self)
281 struct ctf_trace_file_writer *writer
282 = (struct ctf_trace_file_writer *) self;
284 if (writer->tcs.metadata_fd != NULL)
285 fclose (writer->tcs.metadata_fd);
287 if (writer->tcs.datastream_fd != NULL)
288 fclose (writer->tcs.datastream_fd);
292 /* This is the implementation of trace_file_write_ops method
296 ctf_target_save (struct trace_file_writer *self,
299 /* Don't support save trace file to CTF format in the target. */
305 #define mkdir(pathname, mode) mkdir (pathname)
308 /* This is the implementation of trace_file_write_ops method
309 start. It creates the directory DIRNAME, metadata and datastream
313 ctf_start (struct trace_file_writer *self, const char *dirname)
316 struct cleanup *old_chain;
317 struct ctf_trace_file_writer *writer
318 = (struct ctf_trace_file_writer *) self;
320 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH;
322 /* Create DIRNAME. */
323 if (mkdir (dirname, hmode) && errno != EEXIST)
324 error (_("Unable to open directory '%s' for saving trace data (%s)"),
325 dirname, safe_strerror (errno));
327 memset (&writer->tcs, '\0', sizeof (writer->tcs));
329 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
330 old_chain = make_cleanup (xfree, file_name);
332 writer->tcs.metadata_fd = fopen (file_name, "w");
333 if (writer->tcs.metadata_fd == NULL)
334 error (_("Unable to open file '%s' for saving trace data (%s)"),
335 file_name, safe_strerror (errno));
336 do_cleanups (old_chain);
338 ctf_save_metadata_header (&writer->tcs);
340 file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
341 old_chain = make_cleanup (xfree, file_name);
342 writer->tcs.datastream_fd = fopen (file_name, "w");
343 if (writer->tcs.datastream_fd == NULL)
344 error (_("Unable to open file '%s' for saving trace data (%s)"),
345 file_name, safe_strerror (errno));
346 do_cleanups (old_chain);
349 /* This is the implementation of trace_file_write_ops method
350 write_header. Write the types of events on trace variable and
354 ctf_write_header (struct trace_file_writer *self)
356 struct ctf_trace_file_writer *writer
357 = (struct ctf_trace_file_writer *) self;
360 ctf_save_write_metadata (&writer->tcs, "\n");
361 ctf_save_write_metadata (&writer->tcs,
362 "event {\n\tname = \"memory\";\n\tid = %u;\n"
363 "\tfields := struct { \n"
364 "\t\tuint64_t address;\n"
365 "\t\tuint16_t length;\n"
366 "\t\tuint8_t contents[length];\n"
368 "};\n", CTF_EVENT_ID_MEMORY);
370 ctf_save_write_metadata (&writer->tcs, "\n");
371 ctf_save_write_metadata (&writer->tcs,
372 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
373 "\tfields := struct { \n"
374 "\t\tuint64_t val;\n"
375 "\t\tuint32_t num;\n"
377 "};\n", CTF_EVENT_ID_TSV);
379 ctf_save_write_metadata (&writer->tcs, "\n");
380 ctf_save_write_metadata (&writer->tcs,
381 "event {\n\tname = \"frame\";\n\tid = %u;\n"
382 "\tfields := struct { \n"
384 "};\n", CTF_EVENT_ID_FRAME);
386 ctf_save_write_metadata (&writer->tcs, "\n");
387 ctf_save_write_metadata (&writer->tcs,
388 "event {\n\tname = \"tsv_def\";\n"
389 "\tid = %u;\n\tfields := struct { \n"
390 "\t\tint64_t initial_value;\n"
391 "\t\tint32_t number;\n"
392 "\t\tint32_t builtin;\n"
395 "};\n", CTF_EVENT_ID_TSV_DEF);
397 ctf_save_write_metadata (&writer->tcs, "\n");
398 ctf_save_write_metadata (&writer->tcs,
399 "event {\n\tname = \"tp_def\";\n"
400 "\tid = %u;\n\tfields := struct { \n"
401 "\t\tuint64_t addr;\n"
402 "\t\tuint64_t traceframe_usage;\n"
403 "\t\tint32_t number;\n"
404 "\t\tint32_t enabled;\n"
405 "\t\tint32_t step;\n"
406 "\t\tint32_t pass;\n"
407 "\t\tint32_t hit_count;\n"
408 "\t\tint32_t type;\n"
411 "\t\tuint32_t action_num;\n"
412 "\t\tchars actions[action_num];\n"
414 "\t\tuint32_t step_action_num;\n"
415 "\t\tchars step_actions[step_action_num];\n"
417 "\t\tchars at_string;\n"
418 "\t\tchars cond_string;\n"
420 "\t\tuint32_t cmd_num;\n"
421 "\t\tchars cmd_strings[cmd_num];\n"
423 "};\n", CTF_EVENT_ID_TP_DEF);
425 gdb_assert (writer->tcs.content_size == 0);
426 gdb_assert (writer->tcs.packet_start == 0);
428 /* Create a new packet to contain this event. */
429 self->ops->frame_ops->start (self, 0);
432 /* This is the implementation of trace_file_write_ops method
433 write_regblock_type. Write the type of register event in
437 ctf_write_regblock_type (struct trace_file_writer *self, int size)
439 struct ctf_trace_file_writer *writer
440 = (struct ctf_trace_file_writer *) self;
442 ctf_save_write_metadata (&writer->tcs, "\n");
444 ctf_save_write_metadata (&writer->tcs,
445 "event {\n\tname = \"register\";\n\tid = %u;\n"
446 "\tfields := struct { \n"
447 "\t\tascii contents[%d];\n"
450 CTF_EVENT_ID_REGISTER, size);
453 /* This is the implementation of trace_file_write_ops method
457 ctf_write_status (struct trace_file_writer *self,
458 struct trace_status *ts)
460 struct ctf_trace_file_writer *writer
461 = (struct ctf_trace_file_writer *) self;
465 ctf_save_write_metadata (&writer->tcs, "\n");
466 ctf_save_write_metadata (&writer->tcs,
467 "event {\n\tname = \"status\";\n\tid = %u;\n"
468 "\tfields := struct { \n"
469 "\t\tint32_t stop_reason;\n"
470 "\t\tint32_t stopping_tracepoint;\n"
471 "\t\tint32_t traceframe_count;\n"
472 "\t\tint32_t traceframes_created;\n"
473 "\t\tint32_t buffer_free;\n"
474 "\t\tint32_t buffer_size;\n"
475 "\t\tint32_t disconnected_tracing;\n"
476 "\t\tint32_t circular_buffer;\n"
479 CTF_EVENT_ID_STATUS);
481 id = CTF_EVENT_ID_STATUS;
483 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
485 ctf_save_write_int32 (&writer->tcs, ts->stop_reason);
486 ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint);
487 ctf_save_write_int32 (&writer->tcs, ts->traceframe_count);
488 ctf_save_write_int32 (&writer->tcs, ts->traceframes_created);
489 ctf_save_write_int32 (&writer->tcs, ts->buffer_free);
490 ctf_save_write_int32 (&writer->tcs, ts->buffer_size);
491 ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing);
492 ctf_save_write_int32 (&writer->tcs, ts->circular_buffer);
495 /* This is the implementation of trace_file_write_ops method
496 write_uploaded_tsv. */
499 ctf_write_uploaded_tsv (struct trace_file_writer *self,
500 struct uploaded_tsv *tsv)
502 struct ctf_trace_file_writer *writer
503 = (struct ctf_trace_file_writer *) self;
507 const gdb_byte zero = 0;
510 int32 = CTF_EVENT_ID_TSV_DEF;
511 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
514 int64 = tsv->initial_value;
515 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
518 ctf_save_write_int32 (&writer->tcs, tsv->number);
521 ctf_save_write_int32 (&writer->tcs, tsv->builtin);
524 if (tsv->name != NULL)
525 ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name,
527 ctf_save_write (&writer->tcs, &zero, 1);
530 /* This is the implementation of trace_file_write_ops method
531 write_uploaded_tp. */
534 ctf_write_uploaded_tp (struct trace_file_writer *self,
535 struct uploaded_tp *tp)
537 struct ctf_trace_file_writer *writer
538 = (struct ctf_trace_file_writer *) self;
542 const gdb_byte zero = 0;
547 int32 = CTF_EVENT_ID_TP_DEF;
548 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
552 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
554 /* traceframe_usage */
555 int64 = tp->traceframe_usage;
556 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
559 ctf_save_write_int32 (&writer->tcs, tp->number);
562 ctf_save_write_int32 (&writer->tcs, tp->enabled);
565 ctf_save_write_int32 (&writer->tcs, tp->step);
568 ctf_save_write_int32 (&writer->tcs, tp->pass);
571 ctf_save_write_int32 (&writer->tcs, tp->hit_count);
574 ctf_save_write_int32 (&writer->tcs, tp->type);
577 if (tp->cond != NULL)
578 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond, strlen (tp->cond));
579 ctf_save_write (&writer->tcs, &zero, 1);
582 u32 = VEC_length (char_ptr, tp->actions);
583 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
584 for (a = 0; VEC_iterate (char_ptr, tp->actions, a, act); ++a)
585 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
588 u32 = VEC_length (char_ptr, tp->step_actions);
589 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
590 for (a = 0; VEC_iterate (char_ptr, tp->step_actions, a, act); ++a)
591 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
594 if (tp->at_string != NULL)
595 ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string,
596 strlen (tp->at_string));
597 ctf_save_write (&writer->tcs, &zero, 1);
600 if (tp->cond_string != NULL)
601 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string,
602 strlen (tp->cond_string));
603 ctf_save_write (&writer->tcs, &zero, 1);
606 u32 = VEC_length (char_ptr, tp->cmd_strings);
607 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
608 for (a = 0; VEC_iterate (char_ptr, tp->cmd_strings, a, act); ++a)
609 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
613 /* This is the implementation of trace_file_write_ops method
614 write_definition_end. */
617 ctf_write_definition_end (struct trace_file_writer *self)
619 struct ctf_trace_file_writer *writer
620 = (struct ctf_trace_file_writer *) self;
622 self->ops->frame_ops->end (self);
625 /* The minimal file size of data stream. It is required by
628 #define CTF_FILE_MIN_SIZE 4096
630 /* This is the implementation of trace_file_write_ops method
634 ctf_end (struct trace_file_writer *self)
636 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
638 gdb_assert (writer->tcs.content_size == 0);
639 /* The babeltrace requires or assumes that the size of datastream
640 file is greater than 4096 bytes. If we don't generate enough
641 packets and events, create a fake packet which has zero event,
642 to use up the space. */
643 if (writer->tcs.packet_start < CTF_FILE_MIN_SIZE)
649 ctf_save_write_uint32 (&writer->tcs, u32);
653 ctf_save_write_uint32 (&writer->tcs, u32);
657 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
658 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
660 u32 *= TARGET_CHAR_BIT;
661 ctf_save_write_uint32 (&writer->tcs, u32);
665 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
667 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
669 if (CTF_FILE_MIN_SIZE
670 > (writer->tcs.packet_start + writer->tcs.content_size))
674 /* Fake the content size to avoid assertion failure in
676 writer->tcs.content_size = (CTF_FILE_MIN_SIZE
677 - 1 - writer->tcs.packet_start);
678 ctf_save_fseek (&writer->tcs, CTF_FILE_MIN_SIZE - 1,
680 ctf_save_write (&writer->tcs, &b, 1);
685 /* This is the implementation of trace_frame_write_ops method
689 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
691 struct ctf_trace_file_writer *writer
692 = (struct ctf_trace_file_writer *) self;
693 uint32_t id = CTF_EVENT_ID_FRAME;
696 /* Step 1: Write packet context. */
699 ctf_save_write_uint32 (&writer->tcs, u32);
700 /* content_size and packet_size.. We still don't know the value,
702 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
703 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
704 /* Tracepoint number. */
705 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
707 /* Step 2: Write event "frame". */
709 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
712 /* This is the implementation of trace_frame_write_ops method
716 ctf_write_frame_r_block (struct trace_file_writer *self,
717 gdb_byte *buf, int32_t size)
719 struct ctf_trace_file_writer *writer
720 = (struct ctf_trace_file_writer *) self;
721 uint32_t id = CTF_EVENT_ID_REGISTER;
724 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
726 /* array contents. */
727 ctf_save_align_write (&writer->tcs, buf, size, 1);
730 /* This is the implementation of trace_frame_write_ops method
731 write_m_block_header. */
734 ctf_write_frame_m_block_header (struct trace_file_writer *self,
735 uint64_t addr, uint16_t length)
737 struct ctf_trace_file_writer *writer
738 = (struct ctf_trace_file_writer *) self;
739 uint32_t event_id = CTF_EVENT_ID_MEMORY;
742 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
745 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
748 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
751 /* This is the implementation of trace_frame_write_ops method
752 write_m_block_memory. */
755 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
756 gdb_byte *buf, uint16_t length)
758 struct ctf_trace_file_writer *writer
759 = (struct ctf_trace_file_writer *) self;
762 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
765 /* This is the implementation of trace_frame_write_ops method
769 ctf_write_frame_v_block (struct trace_file_writer *self,
770 int32_t num, uint64_t val)
772 struct ctf_trace_file_writer *writer
773 = (struct ctf_trace_file_writer *) self;
774 uint32_t id = CTF_EVENT_ID_TSV;
777 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
780 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
782 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
785 /* This is the implementation of trace_frame_write_ops method
789 ctf_write_frame_end (struct trace_file_writer *self)
791 struct ctf_trace_file_writer *writer
792 = (struct ctf_trace_file_writer *) self;
796 /* Write the content size to packet header. */
797 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
799 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
801 t = writer->tcs.content_size;
802 ctf_save_write_uint32 (&writer->tcs, u32);
804 /* Write the packet size. */
805 u32 += 4 * TARGET_CHAR_BIT;
806 ctf_save_write_uint32 (&writer->tcs, u32);
808 writer->tcs.content_size = t;
810 /* Write zero at the end of the packet. */
811 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
814 ctf_save_write_uint32 (&writer->tcs, u32);
815 writer->tcs.content_size = t;
817 ctf_save_next_packet (&writer->tcs);
820 /* Operations to write various types of trace frames into CTF
823 static const struct trace_frame_write_ops ctf_write_frame_ops =
825 ctf_write_frame_start,
826 ctf_write_frame_r_block,
827 ctf_write_frame_m_block_header,
828 ctf_write_frame_m_block_memory,
829 ctf_write_frame_v_block,
833 /* Operations to write trace buffers into CTF format. */
835 static const struct trace_file_write_ops ctf_write_ops =
841 ctf_write_regblock_type,
843 ctf_write_uploaded_tsv,
844 ctf_write_uploaded_tp,
845 ctf_write_definition_end,
847 &ctf_write_frame_ops,
851 /* Return a trace writer for CTF format. */
853 struct trace_file_writer *
854 ctf_trace_file_writer_new (void)
856 struct ctf_trace_file_writer *writer
857 = xmalloc (sizeof (struct ctf_trace_file_writer));
859 writer->base.ops = &ctf_write_ops;
861 return (struct trace_file_writer *) writer;
864 #if HAVE_LIBBABELTRACE
865 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
866 iterator to iterate over each event in CTF data and APIs to get
867 details of event and packet, so it is very convenient to use
868 libbabeltrace to access events in CTF. */
870 #include <babeltrace/babeltrace.h>
871 #include <babeltrace/ctf/events.h>
872 #include <babeltrace/ctf/iterator.h>
874 /* The struct pointer for current CTF directory. */
875 static struct bt_context *ctx = NULL;
876 static struct bt_ctf_iter *ctf_iter = NULL;
877 /* The position of the first packet containing trace frame. */
878 static struct bt_iter_pos *start_pos;
880 /* The name of CTF directory. */
881 static char *trace_dirname;
883 static struct target_ops ctf_ops;
885 /* Destroy ctf iterator and context. */
890 if (ctf_iter != NULL)
892 bt_ctf_iter_destroy (ctf_iter);
897 bt_context_put (ctx);
902 /* Open CTF trace data in DIRNAME. */
905 ctf_open_dir (char *dirname)
908 struct bt_iter_pos begin_pos;
909 struct bt_iter_pos *pos;
911 ctx = bt_context_create ();
913 error (_("Unable to create bt_context"));
914 ret = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
918 error (_("Unable to use libbabeltrace on directory \"%s\""),
922 begin_pos.type = BT_SEEK_BEGIN;
923 ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL);
924 if (ctf_iter == NULL)
927 error (_("Unable to create bt_iterator"));
930 /* Iterate over events, and look for an event for register block
931 to set trace_regblock_size. */
933 /* Save the current position. */
934 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
935 gdb_assert (pos->type == BT_SEEK_RESTORE);
940 struct bt_ctf_event *event;
942 event = bt_ctf_iter_read_event (ctf_iter);
944 name = bt_ctf_event_name (event);
948 else if (strcmp (name, "register") == 0)
950 const struct bt_definition *scope
951 = bt_ctf_get_top_level_scope (event,
953 const struct bt_definition *array
954 = bt_ctf_get_field (event, scope, "contents");
957 = bt_ctf_get_array_len (bt_ctf_get_decl_from_def (array));
960 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
964 /* Restore the position. */
965 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
968 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
969 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
973 /* EVENT is the "status" event and TS is filled in. */
976 ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts)
978 const struct bt_definition *scope
979 = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS);
981 SET_INT32_FIELD (event, scope, ts, stop_reason);
982 SET_INT32_FIELD (event, scope, ts, stopping_tracepoint);
983 SET_INT32_FIELD (event, scope, ts, traceframe_count);
984 SET_INT32_FIELD (event, scope, ts, traceframes_created);
985 SET_INT32_FIELD (event, scope, ts, buffer_free);
986 SET_INT32_FIELD (event, scope, ts, buffer_size);
987 SET_INT32_FIELD (event, scope, ts, disconnected_tracing);
988 SET_INT32_FIELD (event, scope, ts, circular_buffer);
990 bt_iter_next (bt_ctf_get_iter (ctf_iter));
993 /* Read the events "tsv_def" one by one, extract its contents and fill
994 in the list UPLOADED_TSVS. */
997 ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
999 gdb_assert (ctf_iter != NULL);
1003 struct bt_ctf_event *event;
1004 const struct bt_definition *scope;
1005 const struct bt_definition *def;
1007 struct uploaded_tsv *utsv = NULL;
1009 event = bt_ctf_iter_read_event (ctf_iter);
1010 scope = bt_ctf_get_top_level_scope (event,
1011 BT_STREAM_EVENT_HEADER);
1012 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1014 if (event_id != CTF_EVENT_ID_TSV_DEF)
1017 scope = bt_ctf_get_top_level_scope (event,
1020 def = bt_ctf_get_field (event, scope, "number");
1021 utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def),
1024 def = bt_ctf_get_field (event, scope, "builtin");
1025 utsv->builtin = (int32_t) bt_ctf_get_int64 (def);
1026 def = bt_ctf_get_field (event, scope, "initial_value");
1027 utsv->initial_value = bt_ctf_get_int64 (def);
1029 def = bt_ctf_get_field (event, scope, "name");
1030 utsv->name = xstrdup (bt_ctf_get_string (def));
1032 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1038 /* Read the value of element whose index is NUM from CTF and write it
1039 to the corresponding VAR->ARRAY. */
1041 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
1045 const struct bt_definition *def; \
1047 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1050 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1051 for (i = 0; i < u32; i++) \
1053 const struct bt_definition *element \
1054 = bt_ctf_get_index ((EVENT), def, i); \
1056 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1057 xstrdup (bt_ctf_get_string (element))); \
1062 /* Read a string from CTF and set VAR->FIELD. If the length of string
1063 is zero, set VAR->FIELD to NULL. */
1065 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1068 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1072 if (strlen (p) > 0) \
1073 (VAR)->FIELD = xstrdup (p); \
1075 (VAR)->FIELD = NULL; \
1079 /* Read the events "tp_def" one by one, extract its contents and fill
1080 in the list UPLOADED_TPS. */
1083 ctf_read_tp (struct uploaded_tp **uploaded_tps)
1085 gdb_assert (ctf_iter != NULL);
1089 struct bt_ctf_event *event;
1090 const struct bt_definition *scope;
1094 struct uploaded_tp *utp = NULL;
1096 event = bt_ctf_iter_read_event (ctf_iter);
1097 scope = bt_ctf_get_top_level_scope (event,
1098 BT_STREAM_EVENT_HEADER);
1099 u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1101 if (u32 != CTF_EVENT_ID_TP_DEF)
1104 scope = bt_ctf_get_top_level_scope (event,
1106 int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event,
1109 u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1111 utp = get_uploaded_tp (int32, u64, uploaded_tps);
1113 SET_INT32_FIELD (event, scope, utp, enabled);
1114 SET_INT32_FIELD (event, scope, utp, step);
1115 SET_INT32_FIELD (event, scope, utp, pass);
1116 SET_INT32_FIELD (event, scope, utp, hit_count);
1117 SET_INT32_FIELD (event, scope, utp, type);
1119 /* Read 'cmd_strings'. */
1120 SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings);
1121 /* Read 'actions'. */
1122 SET_ARRAY_FIELD (event, scope, utp, action_num, actions);
1123 /* Read 'step_actions'. */
1124 SET_ARRAY_FIELD (event, scope, utp, step_action_num,
1127 SET_STRING_FIELD(event, scope, utp, at_string);
1128 SET_STRING_FIELD(event, scope, utp, cond_string);
1130 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1135 /* This is the implementation of target_ops method to_open. Open CTF
1136 trace data, read trace status, trace state variables and tracepoint
1137 definitions from the first packet. Set the start position at the
1138 second packet which contains events on trace blocks. */
1141 ctf_open (char *dirname, int from_tty)
1143 struct bt_ctf_event *event;
1145 const struct bt_definition *scope;
1146 struct uploaded_tsv *uploaded_tsvs = NULL;
1147 struct uploaded_tp *uploaded_tps = NULL;
1150 error (_("No CTF directory specified."));
1152 ctf_open_dir (dirname);
1154 target_preopen (from_tty);
1156 /* Skip the first packet which about the trace status. The first
1157 event is "frame". */
1158 event = bt_ctf_iter_read_event (ctf_iter);
1159 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1160 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1161 if (event_id != CTF_EVENT_ID_FRAME)
1162 error (_("Wrong event id of the first event"));
1163 /* The second event is "status". */
1164 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1165 event = bt_ctf_iter_read_event (ctf_iter);
1166 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1167 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1168 if (event_id != CTF_EVENT_ID_STATUS)
1169 error (_("Wrong event id of the second event"));
1170 ctf_read_status (event, current_trace_status ());
1172 ctf_read_tsv (&uploaded_tsvs);
1174 ctf_read_tp (&uploaded_tps);
1176 event = bt_ctf_iter_read_event (ctf_iter);
1177 /* EVENT can be NULL if we've already gone to the end of stream of
1181 scope = bt_ctf_get_top_level_scope (event,
1182 BT_STREAM_EVENT_HEADER);
1183 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event,
1185 if (event_id != CTF_EVENT_ID_FRAME)
1186 error (_("Wrong event id of the first event of the second packet"));
1189 start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1190 gdb_assert (start_pos->type == BT_SEEK_RESTORE);
1192 trace_dirname = xstrdup (dirname);
1193 push_target (&ctf_ops);
1195 inferior_appeared (current_inferior (), CTF_PID);
1196 inferior_ptid = pid_to_ptid (CTF_PID);
1197 add_thread_silent (inferior_ptid);
1199 merge_uploaded_trace_state_variables (&uploaded_tsvs);
1200 merge_uploaded_tracepoints (&uploaded_tps);
1203 /* This is the implementation of target_ops method to_close. Destroy
1204 CTF iterator and context. */
1212 xfree (trace_dirname);
1213 trace_dirname = NULL;
1215 pid = ptid_get_pid (inferior_ptid);
1216 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
1217 exit_inferior_silent (pid);
1219 trace_reset_local_state ();
1222 /* This is the implementation of target_ops method to_files_info.
1223 Print the directory name of CTF trace data. */
1226 ctf_files_info (struct target_ops *t)
1228 printf_filtered ("\t`%s'\n", trace_dirname);
1231 /* This is the implementation of target_ops method to_fetch_registers.
1232 Iterate over events whose name is "register" in current frame,
1233 extract contents from events, and set REGCACHE with the contents.
1234 If no matched events are found, mark registers unavailable. */
1237 ctf_fetch_registers (struct target_ops *ops,
1238 struct regcache *regcache, int regno)
1240 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1241 int offset, regn, regsize, pc_regno;
1242 gdb_byte *regs = NULL;
1243 struct bt_ctf_event *event = NULL;
1244 struct bt_iter_pos *pos;
1246 /* An uninitialized reg size says we're not going to be
1247 successful at getting register blocks. */
1248 if (trace_regblock_size == 0)
1251 gdb_assert (ctf_iter != NULL);
1252 /* Save the current position. */
1253 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1254 gdb_assert (pos->type == BT_SEEK_RESTORE);
1259 struct bt_ctf_event *event1;
1261 event1 = bt_ctf_iter_read_event (ctf_iter);
1263 name = bt_ctf_event_name (event1);
1265 if (name == NULL || strcmp (name, "frame") == 0)
1267 else if (strcmp (name, "register") == 0)
1273 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1277 /* Restore the position. */
1278 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1282 const struct bt_definition *scope
1283 = bt_ctf_get_top_level_scope (event,
1285 const struct bt_definition *array
1286 = bt_ctf_get_field (event, scope, "contents");
1288 regs = (gdb_byte *) bt_ctf_get_char_array (array);
1289 /* Assume the block is laid out in GDB register number order,
1290 each register with the size that it has in GDB. */
1292 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1294 regsize = register_size (gdbarch, regn);
1295 /* Make sure we stay within block bounds. */
1296 if (offset + regsize >= trace_regblock_size)
1298 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
1302 regcache_raw_supply (regcache, regno, regs + offset);
1305 else if (regno == -1)
1307 regcache_raw_supply (regcache, regn, regs + offset);
1315 regs = alloca (trace_regblock_size);
1317 /* We get here if no register data has been found. Mark registers
1319 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1320 regcache_raw_supply (regcache, regn, NULL);
1322 /* We can often usefully guess that the PC is going to be the same
1323 as the address of the tracepoint. */
1324 pc_regno = gdbarch_pc_regnum (gdbarch);
1325 if (pc_regno >= 0 && (regno == -1 || regno == pc_regno))
1327 struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
1329 if (tp != NULL && tp->base.loc)
1331 /* But don't try to guess if tracepoint is multi-location... */
1332 if (tp->base.loc->next != NULL)
1334 warning (_("Tracepoint %d has multiple "
1335 "locations, cannot infer $pc"),
1339 /* ... or does while-stepping. */
1340 if (tp->step_count > 0)
1342 warning (_("Tracepoint %d does while-stepping, "
1343 "cannot infer $pc"),
1348 store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
1349 gdbarch_byte_order (gdbarch),
1350 tp->base.loc->address);
1351 regcache_raw_supply (regcache, pc_regno, regs);
1356 /* This is the implementation of target_ops method to_xfer_partial.
1357 Iterate over events whose name is "memory" in
1358 current frame, extract the address and length from events. If
1359 OFFSET is within the range, read the contents from events to
1363 ctf_xfer_partial (struct target_ops *ops, enum target_object object,
1364 const char *annex, gdb_byte *readbuf,
1365 const gdb_byte *writebuf, ULONGEST offset,
1368 /* We're only doing regular memory for now. */
1369 if (object != TARGET_OBJECT_MEMORY)
1372 if (readbuf == NULL)
1373 error (_("ctf_xfer_partial: trace file is read-only"));
1375 if (get_traceframe_number () != -1)
1377 struct bt_iter_pos *pos;
1380 gdb_assert (ctf_iter != NULL);
1381 /* Save the current position. */
1382 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1383 gdb_assert (pos->type == BT_SEEK_RESTORE);
1385 /* Iterate through the traceframe's blocks, looking for
1392 enum bfd_endian byte_order
1393 = gdbarch_byte_order (target_gdbarch ());
1394 const struct bt_definition *scope;
1395 const struct bt_definition *def;
1396 struct bt_ctf_event *event
1397 = bt_ctf_iter_read_event (ctf_iter);
1398 const char *name = bt_ctf_event_name (event);
1400 if (strcmp (name, "frame") == 0)
1402 else if (strcmp (name, "memory") != 0)
1404 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1410 scope = bt_ctf_get_top_level_scope (event,
1413 def = bt_ctf_get_field (event, scope, "address");
1414 maddr = bt_ctf_get_uint64 (def);
1415 def = bt_ctf_get_field (event, scope, "length");
1416 mlen = (uint16_t) bt_ctf_get_uint64 (def);
1418 /* If the block includes the first part of the desired
1419 range, return as much it has; GDB will re-request the
1420 remainder, which might be in a different block of this
1422 if (maddr <= offset && offset < (maddr + mlen))
1424 const struct bt_definition *array
1425 = bt_ctf_get_field (event, scope, "contents");
1426 const struct bt_declaration *decl
1427 = bt_ctf_get_decl_from_def (array);
1431 contents = xmalloc (mlen);
1433 for (k = 0; k < mlen; k++)
1435 const struct bt_definition *element
1436 = bt_ctf_get_index (event, array, k);
1438 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
1441 amt = (maddr + mlen) - offset;
1445 memcpy (readbuf, &contents[offset - maddr], amt);
1449 /* Restore the position. */
1450 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1455 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1459 /* Restore the position. */
1460 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1463 /* It's unduly pedantic to refuse to look at the executable for
1464 read-only pieces; so do the equivalent of readonly regions aka
1466 if (exec_bfd != NULL)
1472 for (s = exec_bfd->sections; s; s = s->next)
1474 if ((s->flags & SEC_LOAD) == 0
1475 || (s->flags & SEC_READONLY) == 0)
1479 size = bfd_get_section_size (s);
1480 if (vma <= offset && offset < (vma + size))
1484 amt = (vma + size) - offset;
1488 amt = bfd_get_section_contents (exec_bfd, s,
1489 readbuf, offset - vma, amt);
1495 /* Indicate failure to find the requested memory block. */
1496 return TARGET_XFER_E_IO;
1499 /* This is the implementation of target_ops method
1500 to_get_trace_state_variable_value.
1501 Iterate over events whose name is "tsv" in current frame. When the
1502 trace variable is found, set the value of it to *VAL and return
1503 true, otherwise return false. */
1506 ctf_get_trace_state_variable_value (int tsvnum, LONGEST *val)
1508 struct bt_iter_pos *pos;
1511 gdb_assert (ctf_iter != NULL);
1512 /* Save the current position. */
1513 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1514 gdb_assert (pos->type == BT_SEEK_RESTORE);
1516 /* Iterate through the traceframe's blocks, looking for 'V'
1520 struct bt_ctf_event *event
1521 = bt_ctf_iter_read_event (ctf_iter);
1522 const char *name = bt_ctf_event_name (event);
1524 if (name == NULL || strcmp (name, "frame") == 0)
1526 else if (strcmp (name, "tsv") == 0)
1528 const struct bt_definition *scope;
1529 const struct bt_definition *def;
1531 scope = bt_ctf_get_top_level_scope (event,
1534 def = bt_ctf_get_field (event, scope, "num");
1535 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
1537 def = bt_ctf_get_field (event, scope, "val");
1538 *val = bt_ctf_get_uint64 (def);
1544 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1548 /* Restore the position. */
1549 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1554 /* Return the tracepoint number in "frame" event. */
1557 ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
1559 /* The packet context of events has a field "tpnum". */
1560 const struct bt_definition *scope
1561 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
1563 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
1568 /* Return the address at which the current frame was collected. */
1571 ctf_get_traceframe_address (void)
1573 struct bt_ctf_event *event = NULL;
1574 struct bt_iter_pos *pos;
1577 gdb_assert (ctf_iter != NULL);
1578 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1579 gdb_assert (pos->type == BT_SEEK_RESTORE);
1584 struct bt_ctf_event *event1;
1586 event1 = bt_ctf_iter_read_event (ctf_iter);
1588 name = bt_ctf_event_name (event1);
1592 else if (strcmp (name, "frame") == 0)
1598 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1604 int tpnum = ctf_get_tpnum_from_frame_event (event);
1605 struct tracepoint *tp
1606 = get_tracepoint_by_number_on_target (tpnum);
1608 if (tp && tp->base.loc)
1609 addr = tp->base.loc->address;
1612 /* Restore the position. */
1613 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1618 /* This is the implementation of target_ops method to_trace_find.
1619 Iterate the events whose name is "frame", extract the tracepoint
1620 number in it. Return traceframe number when matched. */
1623 ctf_trace_find (enum trace_find_type type, int num,
1624 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
1629 struct bt_iter_pos pos;
1638 gdb_assert (ctf_iter != NULL);
1639 /* Set iterator back to the start. */
1640 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
1645 struct bt_ctf_event *event;
1648 event = bt_ctf_iter_read_event (ctf_iter);
1650 name = bt_ctf_event_name (event);
1652 if (event == NULL || name == NULL)
1655 if (strcmp (name, "frame") == 0)
1659 if (type == tfind_number)
1661 /* Looking for a specific trace frame. */
1667 /* Start from the _next_ trace frame. */
1668 if (tfnum > get_traceframe_number ())
1674 struct tracepoint *tp = get_tracepoint (num);
1677 && (tp->number_on_target
1678 == ctf_get_tpnum_from_frame_event (event)))
1683 tfaddr = ctf_get_traceframe_address ();
1684 if (tfaddr == addr1)
1688 tfaddr = ctf_get_traceframe_address ();
1689 if (addr1 <= tfaddr && tfaddr <= addr2)
1693 tfaddr = ctf_get_traceframe_address ();
1694 if (!(addr1 <= tfaddr && tfaddr <= addr2))
1698 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
1705 *tpp = ctf_get_tpnum_from_frame_event (event);
1707 /* Skip the event "frame". */
1708 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1715 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1722 /* This is the implementation of target_ops method to_has_stack.
1723 The target has a stack when GDB has already selected one trace
1727 ctf_has_stack (struct target_ops *ops)
1729 return get_traceframe_number () != -1;
1732 /* This is the implementation of target_ops method to_has_registers.
1733 The target has registers when GDB has already selected one trace
1737 ctf_has_registers (struct target_ops *ops)
1739 return get_traceframe_number () != -1;
1742 /* This is the implementation of target_ops method to_thread_alive.
1743 CTF trace data has one thread faked by GDB. */
1746 ctf_thread_alive (struct target_ops *ops, ptid_t ptid)
1751 /* This is the implementation of target_ops method to_traceframe_info.
1752 Iterate the events whose name is "memory", in current
1753 frame, extract memory range information, and return them in
1756 static struct traceframe_info *
1757 ctf_traceframe_info (void)
1759 struct traceframe_info *info = XCNEW (struct traceframe_info);
1761 struct bt_iter_pos *pos;
1763 gdb_assert (ctf_iter != NULL);
1764 /* Save the current position. */
1765 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1766 gdb_assert (pos->type == BT_SEEK_RESTORE);
1770 struct bt_ctf_event *event
1771 = bt_ctf_iter_read_event (ctf_iter);
1773 name = bt_ctf_event_name (event);
1775 if (name == NULL || strcmp (name, "register") == 0
1776 || strcmp (name, "frame") == 0)
1778 else if (strcmp (name, "memory") == 0)
1780 const struct bt_definition *scope
1781 = bt_ctf_get_top_level_scope (event,
1783 const struct bt_definition *def;
1784 struct mem_range *r;
1786 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1787 def = bt_ctf_get_field (event, scope, "address");
1788 r->start = bt_ctf_get_uint64 (def);
1790 def = bt_ctf_get_field (event, scope, "length");
1791 r->length = (uint16_t) bt_ctf_get_uint64 (def);
1793 else if (strcmp (name, "tsv") == 0)
1796 const struct bt_definition *scope
1797 = bt_ctf_get_top_level_scope (event,
1799 const struct bt_definition *def;
1801 def = bt_ctf_get_field (event, scope, "num");
1802 vnum = (int) bt_ctf_get_int64 (def);
1803 VEC_safe_push (int, info->tvars, vnum);
1807 warning (_("Unhandled trace block type (%s) "
1808 "while building trace frame info."),
1812 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1815 while (name != NULL && strcmp (name, "frame") != 0);
1817 /* Restore the position. */
1818 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1823 /* This is the implementation of target_ops method to_get_trace_status.
1824 The trace status for a file is that tracing can never be run. */
1827 ctf_get_trace_status (struct trace_status *ts)
1829 /* Other bits of trace status were collected as part of opening the
1830 trace files, so nothing to do here. */
1838 memset (&ctf_ops, 0, sizeof (ctf_ops));
1840 ctf_ops.to_shortname = "ctf";
1841 ctf_ops.to_longname = "CTF file";
1842 ctf_ops.to_doc = "Use a CTF directory as a target.\n\
1843 Specify the filename of the CTF directory.";
1844 ctf_ops.to_open = ctf_open;
1845 ctf_ops.to_close = ctf_close;
1846 ctf_ops.to_fetch_registers = ctf_fetch_registers;
1847 ctf_ops.to_xfer_partial = ctf_xfer_partial;
1848 ctf_ops.to_files_info = ctf_files_info;
1849 ctf_ops.to_get_trace_status = ctf_get_trace_status;
1850 ctf_ops.to_trace_find = ctf_trace_find;
1851 ctf_ops.to_get_trace_state_variable_value
1852 = ctf_get_trace_state_variable_value;
1853 ctf_ops.to_stratum = process_stratum;
1854 ctf_ops.to_has_stack = ctf_has_stack;
1855 ctf_ops.to_has_registers = ctf_has_registers;
1856 ctf_ops.to_traceframe_info = ctf_traceframe_info;
1857 ctf_ops.to_thread_alive = ctf_thread_alive;
1858 ctf_ops.to_magic = OPS_MAGIC;
1863 /* -Wmissing-prototypes */
1865 extern initialize_file_ftype _initialize_ctf;
1867 /* module initialization */
1870 _initialize_ctf (void)
1872 #if HAVE_LIBBABELTRACE
1875 add_target_with_completer (&ctf_ops, filename_completer);