Eliminate literal line numbers in mi-console.exp
[binutils-gdb.git] / gdb / ctf.c
blob9fd8c047c918a07c6b407a467ecfc14c2c80f272
1 /* CTF format support.
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/>. */
22 #include "defs.h"
23 #include "ctf.h"
24 #include "tracepoint.h"
25 #include "regcache.h"
26 #include <sys/stat.h>
27 #include "exec.h"
28 #include "completer.h"
29 #include "inferior.h"
30 #include "gdbthread.h"
31 #include "tracefile.h"
33 #include <ctype.h>
35 /* GDB saves trace buffers and other information (such as trace
36 status) got from the remote target into Common Trace Format (CTF).
37 The following types of information are expected to save in CTF:
39 1. The length (in bytes) of register cache. Event "register" will
40 be defined in metadata, which includes the length.
42 2. Trace status. Event "status" is defined in metadata, which
43 includes all aspects of trace status.
45 3. Uploaded trace variables. Event "tsv_def" is defined in
46 metadata, which is about all aspects of a uploaded trace variable.
47 Uploaded tracepoints. Event "tp_def" is defined in meta, which
48 is about all aspects of an uploaded tracepoint. Note that the
49 "sequence" (a CTF type, which is a dynamically-sized array.) is
50 used for "actions" "step_actions" and "cmd_strings".
52 4. Trace frames. Each trace frame is composed by several blocks
53 of different types ('R', 'M', 'V'). One trace frame is saved in
54 one CTF packet and the blocks of this frame are saved as events.
55 4.1: The trace frame related information (such as the number of
56 tracepoint associated with this frame) is saved in the packet
57 context.
58 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
59 "register" and "tsv" respectively.
60 4.3: When iterating over events, babeltrace can't tell iterator
61 goes to a new packet, so we need a marker or anchor to tell GDB
62 that iterator goes into a new packet or frame. We define event
63 "frame". */
65 #define CTF_MAGIC 0xC1FC1FC1
66 #define CTF_SAVE_MAJOR 1
67 #define CTF_SAVE_MINOR 8
69 #define CTF_METADATA_NAME "metadata"
70 #define CTF_DATASTREAM_NAME "datastream"
72 /* Reserved event id. */
74 #define CTF_EVENT_ID_REGISTER 0
75 #define CTF_EVENT_ID_TSV 1
76 #define CTF_EVENT_ID_MEMORY 2
77 #define CTF_EVENT_ID_FRAME 3
78 #define CTF_EVENT_ID_STATUS 4
79 #define CTF_EVENT_ID_TSV_DEF 5
80 #define CTF_EVENT_ID_TP_DEF 6
82 #define CTF_PID (2)
84 /* The state kept while writing the CTF datastream file. */
86 struct trace_write_handler
88 /* File descriptor of metadata. */
89 FILE *metadata_fd;
90 /* File descriptor of traceframes. */
91 FILE *datastream_fd;
93 /* This is the content size of the current packet. */
94 size_t content_size;
96 /* This is the start offset of current packet. */
97 long packet_start;
100 /* Write metadata in FORMAT. */
102 static void
103 ctf_save_write_metadata (struct trace_write_handler *handler,
104 const char *format, ...)
106 va_list args;
108 va_start (args, format);
109 if (vfprintf (handler->metadata_fd, format, args) < 0)
110 error (_("Unable to write metadata file (%s)"),
111 safe_strerror (errno));
112 va_end (args);
115 /* Write BUF of length SIZE to datastream file represented by
116 HANDLER. */
118 static int
119 ctf_save_write (struct trace_write_handler *handler,
120 const gdb_byte *buf, size_t size)
122 if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
123 error (_("Unable to write file for saving trace data (%s)"),
124 safe_strerror (errno));
126 handler->content_size += size;
128 return 0;
131 /* Write a unsigned 32-bit integer to datastream file represented by
132 HANDLER. */
134 #define ctf_save_write_uint32(HANDLER, U32) \
135 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
137 /* Write a signed 32-bit integer to datastream file represented by
138 HANDLER. */
140 #define ctf_save_write_int32(HANDLER, INT32) \
141 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
143 /* Set datastream file position. Update HANDLER->content_size
144 if WHENCE is SEEK_CUR. */
146 static int
147 ctf_save_fseek (struct trace_write_handler *handler, long offset,
148 int whence)
150 gdb_assert (whence != SEEK_END);
151 gdb_assert (whence != SEEK_SET
152 || offset <= handler->content_size + handler->packet_start);
154 if (fseek (handler->datastream_fd, offset, whence))
155 error (_("Unable to seek file for saving trace data (%s)"),
156 safe_strerror (errno));
158 if (whence == SEEK_CUR)
159 handler->content_size += offset;
161 return 0;
164 /* Change the datastream file position to align on ALIGN_SIZE,
165 and write BUF to datastream file. The size of BUF is SIZE. */
167 static int
168 ctf_save_align_write (struct trace_write_handler *handler,
169 const gdb_byte *buf,
170 size_t size, size_t align_size)
172 long offset
173 = (align_up (handler->content_size, align_size)
174 - handler->content_size);
176 if (ctf_save_fseek (handler, offset, SEEK_CUR))
177 return -1;
179 if (ctf_save_write (handler, buf, size))
180 return -1;
182 return 0;
185 /* Write events to next new packet. */
187 static void
188 ctf_save_next_packet (struct trace_write_handler *handler)
190 handler->packet_start += (handler->content_size + 4);
191 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
192 handler->content_size = 0;
195 /* Write the CTF metadata header. */
197 static void
198 ctf_save_metadata_header (struct trace_write_handler *handler)
200 const char metadata_fmt[] =
201 "\ntrace {\n"
202 " major = %u;\n"
203 " minor = %u;\n"
204 " byte_order = %s;\n" /* be or le */
205 " packet.header := struct {\n"
206 " uint32_t magic;\n"
207 " };\n"
208 "};\n"
209 "\n"
210 "stream {\n"
211 " packet.context := struct {\n"
212 " uint32_t content_size;\n"
213 " uint32_t packet_size;\n"
214 " uint16_t tpnum;\n"
215 " };\n"
216 " event.header := struct {\n"
217 " uint32_t id;\n"
218 " };\n"
219 "};\n";
221 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
222 CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
223 ctf_save_write_metadata (handler,
224 "typealias integer { size = 8; align = 8; "
225 "signed = false; encoding = ascii;}"
226 " := ascii;\n");
227 ctf_save_write_metadata (handler,
228 "typealias integer { size = 8; align = 8; "
229 "signed = false; }"
230 " := uint8_t;\n");
231 ctf_save_write_metadata (handler,
232 "typealias integer { size = 16; align = 16;"
233 "signed = false; } := uint16_t;\n");
234 ctf_save_write_metadata (handler,
235 "typealias integer { size = 32; align = 32;"
236 "signed = false; } := uint32_t;\n");
237 ctf_save_write_metadata (handler,
238 "typealias integer { size = 64; align = 64;"
239 "signed = false; base = hex;}"
240 " := uint64_t;\n");
241 ctf_save_write_metadata (handler,
242 "typealias integer { size = 32; align = 32;"
243 "signed = true; } := int32_t;\n");
244 ctf_save_write_metadata (handler,
245 "typealias integer { size = 64; align = 64;"
246 "signed = true; } := int64_t;\n");
247 ctf_save_write_metadata (handler,
248 "typealias string { encoding = ascii;"
249 " } := chars;\n");
250 ctf_save_write_metadata (handler, "\n");
252 /* Get the byte order of the host and write CTF data in this byte
253 order. */
254 #if WORDS_BIGENDIAN
255 #define HOST_ENDIANNESS "be"
256 #else
257 #define HOST_ENDIANNESS "le"
258 #endif
260 ctf_save_write_metadata (handler, metadata_fmt,
261 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
262 HOST_ENDIANNESS);
263 ctf_save_write_metadata (handler, "\n");
266 /* CTF trace writer. */
268 struct ctf_trace_file_writer
270 struct trace_file_writer base;
272 /* States related to writing CTF trace file. */
273 struct trace_write_handler tcs;
276 /* This is the implementation of trace_file_write_ops method
277 dtor. */
279 static void
280 ctf_dtor (struct trace_file_writer *self)
282 struct ctf_trace_file_writer *writer
283 = (struct ctf_trace_file_writer *) self;
285 if (writer->tcs.metadata_fd != NULL)
286 fclose (writer->tcs.metadata_fd);
288 if (writer->tcs.datastream_fd != NULL)
289 fclose (writer->tcs.datastream_fd);
293 /* This is the implementation of trace_file_write_ops method
294 target_save. */
296 static int
297 ctf_target_save (struct trace_file_writer *self,
298 const char *dirname)
300 /* Don't support save trace file to CTF format in the target. */
301 return 0;
304 #ifdef USE_WIN32API
305 #undef mkdir
306 #define mkdir(pathname, mode) mkdir (pathname)
307 #endif
309 /* This is the implementation of trace_file_write_ops method
310 start. It creates the directory DIRNAME, metadata and datastream
311 in the directory. */
313 static void
314 ctf_start (struct trace_file_writer *self, const char *dirname)
316 char *file_name;
317 struct cleanup *old_chain;
318 struct ctf_trace_file_writer *writer
319 = (struct ctf_trace_file_writer *) self;
320 int i;
321 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH;
323 /* Create DIRNAME. */
324 if (mkdir (dirname, hmode) && errno != EEXIST)
325 error (_("Unable to open directory '%s' for saving trace data (%s)"),
326 dirname, safe_strerror (errno));
328 memset (&writer->tcs, '\0', sizeof (writer->tcs));
330 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
331 old_chain = make_cleanup (xfree, file_name);
333 writer->tcs.metadata_fd = fopen (file_name, "w");
334 if (writer->tcs.metadata_fd == NULL)
335 error (_("Unable to open file '%s' for saving trace data (%s)"),
336 file_name, safe_strerror (errno));
337 do_cleanups (old_chain);
339 ctf_save_metadata_header (&writer->tcs);
341 file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
342 old_chain = make_cleanup (xfree, file_name);
343 writer->tcs.datastream_fd = fopen (file_name, "w");
344 if (writer->tcs.datastream_fd == NULL)
345 error (_("Unable to open file '%s' for saving trace data (%s)"),
346 file_name, safe_strerror (errno));
347 do_cleanups (old_chain);
350 /* This is the implementation of trace_file_write_ops method
351 write_header. Write the types of events on trace variable and
352 frame. */
354 static void
355 ctf_write_header (struct trace_file_writer *self)
357 struct ctf_trace_file_writer *writer
358 = (struct ctf_trace_file_writer *) self;
361 ctf_save_write_metadata (&writer->tcs, "\n");
362 ctf_save_write_metadata (&writer->tcs,
363 "event {\n\tname = \"memory\";\n\tid = %u;\n"
364 "\tfields := struct { \n"
365 "\t\tuint64_t address;\n"
366 "\t\tuint16_t length;\n"
367 "\t\tuint8_t contents[length];\n"
368 "\t};\n"
369 "};\n", CTF_EVENT_ID_MEMORY);
371 ctf_save_write_metadata (&writer->tcs, "\n");
372 ctf_save_write_metadata (&writer->tcs,
373 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
374 "\tfields := struct { \n"
375 "\t\tuint64_t val;\n"
376 "\t\tuint32_t num;\n"
377 "\t};\n"
378 "};\n", CTF_EVENT_ID_TSV);
380 ctf_save_write_metadata (&writer->tcs, "\n");
381 ctf_save_write_metadata (&writer->tcs,
382 "event {\n\tname = \"frame\";\n\tid = %u;\n"
383 "\tfields := struct { \n"
384 "\t};\n"
385 "};\n", CTF_EVENT_ID_FRAME);
387 ctf_save_write_metadata (&writer->tcs, "\n");
388 ctf_save_write_metadata (&writer->tcs,
389 "event {\n\tname = \"tsv_def\";\n"
390 "\tid = %u;\n\tfields := struct { \n"
391 "\t\tint64_t initial_value;\n"
392 "\t\tint32_t number;\n"
393 "\t\tint32_t builtin;\n"
394 "\t\tchars name;\n"
395 "\t};\n"
396 "};\n", CTF_EVENT_ID_TSV_DEF);
398 ctf_save_write_metadata (&writer->tcs, "\n");
399 ctf_save_write_metadata (&writer->tcs,
400 "event {\n\tname = \"tp_def\";\n"
401 "\tid = %u;\n\tfields := struct { \n"
402 "\t\tuint64_t addr;\n"
403 "\t\tuint64_t traceframe_usage;\n"
404 "\t\tint32_t number;\n"
405 "\t\tint32_t enabled;\n"
406 "\t\tint32_t step;\n"
407 "\t\tint32_t pass;\n"
408 "\t\tint32_t hit_count;\n"
409 "\t\tint32_t type;\n"
410 "\t\tchars cond;\n"
412 "\t\tuint32_t action_num;\n"
413 "\t\tchars actions[action_num];\n"
415 "\t\tuint32_t step_action_num;\n"
416 "\t\tchars step_actions[step_action_num];\n"
418 "\t\tchars at_string;\n"
419 "\t\tchars cond_string;\n"
421 "\t\tuint32_t cmd_num;\n"
422 "\t\tchars cmd_strings[cmd_num];\n"
423 "\t};\n"
424 "};\n", CTF_EVENT_ID_TP_DEF);
426 gdb_assert (writer->tcs.content_size == 0);
427 gdb_assert (writer->tcs.packet_start == 0);
429 /* Create a new packet to contain this event. */
430 self->ops->frame_ops->start (self, 0);
433 /* This is the implementation of trace_file_write_ops method
434 write_regblock_type. Write the type of register event in
435 metadata. */
437 static void
438 ctf_write_regblock_type (struct trace_file_writer *self, int size)
440 struct ctf_trace_file_writer *writer
441 = (struct ctf_trace_file_writer *) self;
443 ctf_save_write_metadata (&writer->tcs, "\n");
445 ctf_save_write_metadata (&writer->tcs,
446 "event {\n\tname = \"register\";\n\tid = %u;\n"
447 "\tfields := struct { \n"
448 "\t\tascii contents[%d];\n"
449 "\t};\n"
450 "};\n",
451 CTF_EVENT_ID_REGISTER, size);
454 /* This is the implementation of trace_file_write_ops method
455 write_status. */
457 static void
458 ctf_write_status (struct trace_file_writer *self,
459 struct trace_status *ts)
461 struct ctf_trace_file_writer *writer
462 = (struct ctf_trace_file_writer *) self;
463 uint32_t id;
464 int32_t int32;
466 ctf_save_write_metadata (&writer->tcs, "\n");
467 ctf_save_write_metadata (&writer->tcs,
468 "event {\n\tname = \"status\";\n\tid = %u;\n"
469 "\tfields := struct { \n"
470 "\t\tint32_t stop_reason;\n"
471 "\t\tint32_t stopping_tracepoint;\n"
472 "\t\tint32_t traceframe_count;\n"
473 "\t\tint32_t traceframes_created;\n"
474 "\t\tint32_t buffer_free;\n"
475 "\t\tint32_t buffer_size;\n"
476 "\t\tint32_t disconnected_tracing;\n"
477 "\t\tint32_t circular_buffer;\n"
478 "\t};\n"
479 "};\n",
480 CTF_EVENT_ID_STATUS);
482 id = CTF_EVENT_ID_STATUS;
483 /* Event Id. */
484 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
486 ctf_save_write_int32 (&writer->tcs, ts->stop_reason);
487 ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint);
488 ctf_save_write_int32 (&writer->tcs, ts->traceframe_count);
489 ctf_save_write_int32 (&writer->tcs, ts->traceframes_created);
490 ctf_save_write_int32 (&writer->tcs, ts->buffer_free);
491 ctf_save_write_int32 (&writer->tcs, ts->buffer_size);
492 ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing);
493 ctf_save_write_int32 (&writer->tcs, ts->circular_buffer);
496 /* This is the implementation of trace_file_write_ops method
497 write_uploaded_tsv. */
499 static void
500 ctf_write_uploaded_tsv (struct trace_file_writer *self,
501 struct uploaded_tsv *tsv)
503 struct ctf_trace_file_writer *writer
504 = (struct ctf_trace_file_writer *) self;
505 int32_t int32;
506 int64_t int64;
507 unsigned int len;
508 const gdb_byte zero = 0;
510 /* Event Id. */
511 int32 = CTF_EVENT_ID_TSV_DEF;
512 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
514 /* initial_value */
515 int64 = tsv->initial_value;
516 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
518 /* number */
519 ctf_save_write_int32 (&writer->tcs, tsv->number);
521 /* builtin */
522 ctf_save_write_int32 (&writer->tcs, tsv->builtin);
524 /* name */
525 if (tsv->name != NULL)
526 ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name,
527 strlen (tsv->name));
528 ctf_save_write (&writer->tcs, &zero, 1);
531 /* This is the implementation of trace_file_write_ops method
532 write_uploaded_tp. */
534 static void
535 ctf_write_uploaded_tp (struct trace_file_writer *self,
536 struct uploaded_tp *tp)
538 struct ctf_trace_file_writer *writer
539 = (struct ctf_trace_file_writer *) self;
540 int32_t int32;
541 int64_t int64;
542 uint32_t u32;
543 const gdb_byte zero = 0;
544 int a;
545 char *act;
547 /* Event Id. */
548 int32 = CTF_EVENT_ID_TP_DEF;
549 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
551 /* address */
552 int64 = tp->addr;
553 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
555 /* traceframe_usage */
556 int64 = tp->traceframe_usage;
557 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
559 /* number */
560 ctf_save_write_int32 (&writer->tcs, tp->number);
562 /* enabled */
563 ctf_save_write_int32 (&writer->tcs, tp->enabled);
565 /* step */
566 ctf_save_write_int32 (&writer->tcs, tp->step);
568 /* pass */
569 ctf_save_write_int32 (&writer->tcs, tp->pass);
571 /* hit_count */
572 ctf_save_write_int32 (&writer->tcs, tp->hit_count);
574 /* type */
575 ctf_save_write_int32 (&writer->tcs, tp->type);
577 /* condition */
578 if (tp->cond != NULL)
579 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond, strlen (tp->cond));
580 ctf_save_write (&writer->tcs, &zero, 1);
582 /* actions */
583 u32 = VEC_length (char_ptr, tp->actions);
584 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
585 for (a = 0; VEC_iterate (char_ptr, tp->actions, a, act); ++a)
586 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
588 /* step_actions */
589 u32 = VEC_length (char_ptr, tp->step_actions);
590 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
591 for (a = 0; VEC_iterate (char_ptr, tp->step_actions, a, act); ++a)
592 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
594 /* at_string */
595 if (tp->at_string != NULL)
596 ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string,
597 strlen (tp->at_string));
598 ctf_save_write (&writer->tcs, &zero, 1);
600 /* cond_string */
601 if (tp->cond_string != NULL)
602 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string,
603 strlen (tp->cond_string));
604 ctf_save_write (&writer->tcs, &zero, 1);
606 /* cmd_strings */
607 u32 = VEC_length (char_ptr, tp->cmd_strings);
608 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
609 for (a = 0; VEC_iterate (char_ptr, tp->cmd_strings, a, act); ++a)
610 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
614 /* This is the implementation of trace_file_write_ops method
615 write_definition_end. */
617 static void
618 ctf_write_definition_end (struct trace_file_writer *self)
620 struct ctf_trace_file_writer *writer
621 = (struct ctf_trace_file_writer *) self;
623 self->ops->frame_ops->end (self);
626 /* This is the implementation of trace_file_write_ops method
627 end. */
629 static void
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);
637 /* This is the implementation of trace_frame_write_ops method
638 start. */
640 static void
641 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
643 struct ctf_trace_file_writer *writer
644 = (struct ctf_trace_file_writer *) self;
645 uint32_t id = CTF_EVENT_ID_FRAME;
646 uint32_t u32;
648 /* Step 1: Write packet context. */
649 /* magic. */
650 u32 = CTF_MAGIC;
651 ctf_save_write_uint32 (&writer->tcs, u32);
652 /* content_size and packet_size.. We still don't know the value,
653 write it later. */
654 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
655 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
656 /* Tracepoint number. */
657 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
659 /* Step 2: Write event "frame". */
660 /* Event Id. */
661 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
664 /* This is the implementation of trace_frame_write_ops method
665 write_r_block. */
667 static void
668 ctf_write_frame_r_block (struct trace_file_writer *self,
669 gdb_byte *buf, int32_t size)
671 struct ctf_trace_file_writer *writer
672 = (struct ctf_trace_file_writer *) self;
673 uint32_t id = CTF_EVENT_ID_REGISTER;
675 /* Event Id. */
676 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
678 /* array contents. */
679 ctf_save_align_write (&writer->tcs, buf, size, 1);
682 /* This is the implementation of trace_frame_write_ops method
683 write_m_block_header. */
685 static void
686 ctf_write_frame_m_block_header (struct trace_file_writer *self,
687 uint64_t addr, uint16_t length)
689 struct ctf_trace_file_writer *writer
690 = (struct ctf_trace_file_writer *) self;
691 uint32_t event_id = CTF_EVENT_ID_MEMORY;
693 /* Event Id. */
694 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
696 /* Address. */
697 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
699 /* Length. */
700 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
703 /* This is the implementation of trace_frame_write_ops method
704 write_m_block_memory. */
706 static void
707 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
708 gdb_byte *buf, uint16_t length)
710 struct ctf_trace_file_writer *writer
711 = (struct ctf_trace_file_writer *) self;
713 /* Contents. */
714 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
717 /* This is the implementation of trace_frame_write_ops method
718 write_v_block. */
720 static void
721 ctf_write_frame_v_block (struct trace_file_writer *self,
722 int32_t num, uint64_t val)
724 struct ctf_trace_file_writer *writer
725 = (struct ctf_trace_file_writer *) self;
726 uint32_t id = CTF_EVENT_ID_TSV;
728 /* Event Id. */
729 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
731 /* val. */
732 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
733 /* num. */
734 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
737 /* This is the implementation of trace_frame_write_ops method
738 end. */
740 static void
741 ctf_write_frame_end (struct trace_file_writer *self)
743 struct ctf_trace_file_writer *writer
744 = (struct ctf_trace_file_writer *) self;
745 uint32_t u32;
746 uint32_t t;
748 /* Write the content size to packet header. */
749 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
750 SEEK_SET);
751 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
753 t = writer->tcs.content_size;
754 ctf_save_write_uint32 (&writer->tcs, u32);
756 /* Write the packet size. */
757 u32 += 4 * TARGET_CHAR_BIT;
758 ctf_save_write_uint32 (&writer->tcs, u32);
760 writer->tcs.content_size = t;
762 /* Write zero at the end of the packet. */
763 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
764 SEEK_SET);
765 u32 = 0;
766 ctf_save_write_uint32 (&writer->tcs, u32);
767 writer->tcs.content_size = t;
769 ctf_save_next_packet (&writer->tcs);
772 /* Operations to write various types of trace frames into CTF
773 format. */
775 static const struct trace_frame_write_ops ctf_write_frame_ops =
777 ctf_write_frame_start,
778 ctf_write_frame_r_block,
779 ctf_write_frame_m_block_header,
780 ctf_write_frame_m_block_memory,
781 ctf_write_frame_v_block,
782 ctf_write_frame_end,
785 /* Operations to write trace buffers into CTF format. */
787 static const struct trace_file_write_ops ctf_write_ops =
789 ctf_dtor,
790 ctf_target_save,
791 ctf_start,
792 ctf_write_header,
793 ctf_write_regblock_type,
794 ctf_write_status,
795 ctf_write_uploaded_tsv,
796 ctf_write_uploaded_tp,
797 ctf_write_definition_end,
798 NULL,
799 &ctf_write_frame_ops,
800 ctf_end,
803 /* Return a trace writer for CTF format. */
805 struct trace_file_writer *
806 ctf_trace_file_writer_new (void)
808 struct ctf_trace_file_writer *writer
809 = xmalloc (sizeof (struct ctf_trace_file_writer));
811 writer->base.ops = &ctf_write_ops;
813 return (struct trace_file_writer *) writer;
816 #if HAVE_LIBBABELTRACE
817 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
818 iterator to iterate over each event in CTF data and APIs to get
819 details of event and packet, so it is very convenient to use
820 libbabeltrace to access events in CTF. */
822 #include <babeltrace/babeltrace.h>
823 #include <babeltrace/ctf/events.h>
824 #include <babeltrace/ctf/iterator.h>
826 /* The struct pointer for current CTF directory. */
827 static int handle_id = -1;
828 static struct bt_context *ctx = NULL;
829 static struct bt_ctf_iter *ctf_iter = NULL;
830 /* The position of the first packet containing trace frame. */
831 static struct bt_iter_pos *start_pos;
833 /* The name of CTF directory. */
834 static char *trace_dirname;
836 static struct target_ops ctf_ops;
838 /* Destroy ctf iterator and context. */
840 static void
841 ctf_destroy (void)
843 if (ctf_iter != NULL)
845 bt_ctf_iter_destroy (ctf_iter);
846 ctf_iter = NULL;
848 if (ctx != NULL)
850 bt_context_put (ctx);
851 ctx = NULL;
855 /* Open CTF trace data in DIRNAME. */
857 static void
858 ctf_open_dir (const char *dirname)
860 struct bt_iter_pos begin_pos;
861 struct bt_iter_pos *pos;
862 unsigned int count, i;
863 struct bt_ctf_event_decl * const *list;
865 ctx = bt_context_create ();
866 if (ctx == NULL)
867 error (_("Unable to create bt_context"));
868 handle_id = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
869 if (handle_id < 0)
871 ctf_destroy ();
872 error (_("Unable to use libbabeltrace on directory \"%s\""),
873 dirname);
876 begin_pos.type = BT_SEEK_BEGIN;
877 ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL);
878 if (ctf_iter == NULL)
880 ctf_destroy ();
881 error (_("Unable to create bt_iterator"));
884 /* Look for the declaration of register block. Get the length of
885 array "contents" to set trace_regblock_size. */
887 bt_ctf_get_event_decl_list (handle_id, ctx, &list, &count);
888 for (i = 0; i < count; i++)
889 if (strcmp ("register", bt_ctf_get_decl_event_name (list[i])) == 0)
891 unsigned int j;
892 const struct bt_ctf_field_decl * const *field_list;
893 const struct bt_declaration *decl;
895 bt_ctf_get_decl_fields (list[i], BT_EVENT_FIELDS, &field_list,
896 &count);
898 gdb_assert (count == 1);
899 gdb_assert (0 == strcmp ("contents",
900 bt_ctf_get_decl_field_name (field_list[0])));
901 decl = bt_ctf_get_decl_from_field_decl (field_list[0]);
902 trace_regblock_size = bt_ctf_get_array_len (decl);
904 break;
908 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
909 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
910 (SCOPE), \
911 #FIELD))
913 /* EVENT is the "status" event and TS is filled in. */
915 static void
916 ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts)
918 const struct bt_definition *scope
919 = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS);
921 SET_INT32_FIELD (event, scope, ts, stop_reason);
922 SET_INT32_FIELD (event, scope, ts, stopping_tracepoint);
923 SET_INT32_FIELD (event, scope, ts, traceframe_count);
924 SET_INT32_FIELD (event, scope, ts, traceframes_created);
925 SET_INT32_FIELD (event, scope, ts, buffer_free);
926 SET_INT32_FIELD (event, scope, ts, buffer_size);
927 SET_INT32_FIELD (event, scope, ts, disconnected_tracing);
928 SET_INT32_FIELD (event, scope, ts, circular_buffer);
930 bt_iter_next (bt_ctf_get_iter (ctf_iter));
933 /* Read the events "tsv_def" one by one, extract its contents and fill
934 in the list UPLOADED_TSVS. */
936 static void
937 ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
939 gdb_assert (ctf_iter != NULL);
941 while (1)
943 struct bt_ctf_event *event;
944 const struct bt_definition *scope;
945 const struct bt_definition *def;
946 uint32_t event_id;
947 struct uploaded_tsv *utsv = NULL;
949 event = bt_ctf_iter_read_event (ctf_iter);
950 scope = bt_ctf_get_top_level_scope (event,
951 BT_STREAM_EVENT_HEADER);
952 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
953 "id"));
954 if (event_id != CTF_EVENT_ID_TSV_DEF)
955 break;
957 scope = bt_ctf_get_top_level_scope (event,
958 BT_EVENT_FIELDS);
960 def = bt_ctf_get_field (event, scope, "number");
961 utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def),
962 uploaded_tsvs);
964 def = bt_ctf_get_field (event, scope, "builtin");
965 utsv->builtin = (int32_t) bt_ctf_get_int64 (def);
966 def = bt_ctf_get_field (event, scope, "initial_value");
967 utsv->initial_value = bt_ctf_get_int64 (def);
969 def = bt_ctf_get_field (event, scope, "name");
970 utsv->name = xstrdup (bt_ctf_get_string (def));
972 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
973 break;
978 /* Read the value of element whose index is NUM from CTF and write it
979 to the corresponding VAR->ARRAY. */
981 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
982 do \
984 uint32_t u32, i; \
985 const struct bt_definition *def; \
987 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
988 (SCOPE), \
989 #NUM)); \
990 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
991 for (i = 0; i < u32; i++) \
993 const struct bt_definition *element \
994 = bt_ctf_get_index ((EVENT), def, i); \
996 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
997 xstrdup (bt_ctf_get_string (element))); \
1000 while (0)
1002 /* Read a string from CTF and set VAR->FIELD. If the length of string
1003 is zero, set VAR->FIELD to NULL. */
1005 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1006 do \
1008 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1009 (SCOPE), \
1010 #FIELD)); \
1012 if (strlen (p) > 0) \
1013 (VAR)->FIELD = xstrdup (p); \
1014 else \
1015 (VAR)->FIELD = NULL; \
1017 while (0)
1019 /* Read the events "tp_def" one by one, extract its contents and fill
1020 in the list UPLOADED_TPS. */
1022 static void
1023 ctf_read_tp (struct uploaded_tp **uploaded_tps)
1025 gdb_assert (ctf_iter != NULL);
1027 while (1)
1029 struct bt_ctf_event *event;
1030 const struct bt_definition *scope;
1031 uint32_t u32;
1032 int32_t int32;
1033 uint64_t u64;
1034 struct uploaded_tp *utp = NULL;
1036 event = bt_ctf_iter_read_event (ctf_iter);
1037 scope = bt_ctf_get_top_level_scope (event,
1038 BT_STREAM_EVENT_HEADER);
1039 u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1040 "id"));
1041 if (u32 != CTF_EVENT_ID_TP_DEF)
1042 break;
1044 scope = bt_ctf_get_top_level_scope (event,
1045 BT_EVENT_FIELDS);
1046 int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event,
1047 scope,
1048 "number"));
1049 u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1050 "addr"));
1051 utp = get_uploaded_tp (int32, u64, uploaded_tps);
1053 SET_INT32_FIELD (event, scope, utp, enabled);
1054 SET_INT32_FIELD (event, scope, utp, step);
1055 SET_INT32_FIELD (event, scope, utp, pass);
1056 SET_INT32_FIELD (event, scope, utp, hit_count);
1057 SET_INT32_FIELD (event, scope, utp, type);
1059 /* Read 'cmd_strings'. */
1060 SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings);
1061 /* Read 'actions'. */
1062 SET_ARRAY_FIELD (event, scope, utp, action_num, actions);
1063 /* Read 'step_actions'. */
1064 SET_ARRAY_FIELD (event, scope, utp, step_action_num,
1065 step_actions);
1067 SET_STRING_FIELD(event, scope, utp, at_string);
1068 SET_STRING_FIELD(event, scope, utp, cond_string);
1070 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1071 break;
1075 /* This is the implementation of target_ops method to_open. Open CTF
1076 trace data, read trace status, trace state variables and tracepoint
1077 definitions from the first packet. Set the start position at the
1078 second packet which contains events on trace blocks. */
1080 static void
1081 ctf_open (const char *dirname, int from_tty)
1083 struct bt_ctf_event *event;
1084 uint32_t event_id;
1085 const struct bt_definition *scope;
1086 struct uploaded_tsv *uploaded_tsvs = NULL;
1087 struct uploaded_tp *uploaded_tps = NULL;
1089 if (!dirname)
1090 error (_("No CTF directory specified."));
1092 ctf_open_dir (dirname);
1094 target_preopen (from_tty);
1096 /* Skip the first packet which about the trace status. The first
1097 event is "frame". */
1098 event = bt_ctf_iter_read_event (ctf_iter);
1099 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1100 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1101 if (event_id != CTF_EVENT_ID_FRAME)
1102 error (_("Wrong event id of the first event"));
1103 /* The second event is "status". */
1104 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1105 event = bt_ctf_iter_read_event (ctf_iter);
1106 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1107 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1108 if (event_id != CTF_EVENT_ID_STATUS)
1109 error (_("Wrong event id of the second event"));
1110 ctf_read_status (event, current_trace_status ());
1112 ctf_read_tsv (&uploaded_tsvs);
1114 ctf_read_tp (&uploaded_tps);
1116 event = bt_ctf_iter_read_event (ctf_iter);
1117 /* EVENT can be NULL if we've already gone to the end of stream of
1118 events. */
1119 if (event != NULL)
1121 scope = bt_ctf_get_top_level_scope (event,
1122 BT_STREAM_EVENT_HEADER);
1123 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event,
1124 scope, "id"));
1125 if (event_id != CTF_EVENT_ID_FRAME)
1126 error (_("Wrong event id of the first event of the second packet"));
1129 start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1130 gdb_assert (start_pos->type == BT_SEEK_RESTORE);
1132 trace_dirname = xstrdup (dirname);
1133 push_target (&ctf_ops);
1135 inferior_appeared (current_inferior (), CTF_PID);
1136 inferior_ptid = pid_to_ptid (CTF_PID);
1137 add_thread_silent (inferior_ptid);
1139 merge_uploaded_trace_state_variables (&uploaded_tsvs);
1140 merge_uploaded_tracepoints (&uploaded_tps);
1142 post_create_inferior (&ctf_ops, from_tty);
1145 /* This is the implementation of target_ops method to_close. Destroy
1146 CTF iterator and context. */
1148 static void
1149 ctf_close (struct target_ops *self)
1151 int pid;
1153 ctf_destroy ();
1154 xfree (trace_dirname);
1155 trace_dirname = NULL;
1157 pid = ptid_get_pid (inferior_ptid);
1158 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
1159 exit_inferior_silent (pid);
1161 trace_reset_local_state ();
1164 /* This is the implementation of target_ops method to_files_info.
1165 Print the directory name of CTF trace data. */
1167 static void
1168 ctf_files_info (struct target_ops *t)
1170 printf_filtered ("\t`%s'\n", trace_dirname);
1173 /* This is the implementation of target_ops method to_fetch_registers.
1174 Iterate over events whose name is "register" in current frame,
1175 extract contents from events, and set REGCACHE with the contents.
1176 If no matched events are found, mark registers unavailable. */
1178 static void
1179 ctf_fetch_registers (struct target_ops *ops,
1180 struct regcache *regcache, int regno)
1182 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1183 struct bt_ctf_event *event = NULL;
1184 struct bt_iter_pos *pos;
1186 /* An uninitialized reg size says we're not going to be
1187 successful at getting register blocks. */
1188 if (trace_regblock_size == 0)
1189 return;
1191 gdb_assert (ctf_iter != NULL);
1192 /* Save the current position. */
1193 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1194 gdb_assert (pos->type == BT_SEEK_RESTORE);
1196 while (1)
1198 const char *name;
1199 struct bt_ctf_event *event1;
1201 event1 = bt_ctf_iter_read_event (ctf_iter);
1203 name = bt_ctf_event_name (event1);
1205 if (name == NULL || strcmp (name, "frame") == 0)
1206 break;
1207 else if (strcmp (name, "register") == 0)
1209 event = event1;
1210 break;
1213 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1214 break;
1217 /* Restore the position. */
1218 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1220 if (event != NULL)
1222 int offset, regsize, regn;
1223 const struct bt_definition *scope
1224 = bt_ctf_get_top_level_scope (event,
1225 BT_EVENT_FIELDS);
1226 const struct bt_definition *array
1227 = bt_ctf_get_field (event, scope, "contents");
1228 gdb_byte *regs = (gdb_byte *) bt_ctf_get_char_array (array);
1230 /* Assume the block is laid out in GDB register number order,
1231 each register with the size that it has in GDB. */
1232 offset = 0;
1233 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1235 regsize = register_size (gdbarch, regn);
1236 /* Make sure we stay within block bounds. */
1237 if (offset + regsize >= trace_regblock_size)
1238 break;
1239 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
1241 if (regno == regn)
1243 regcache_raw_supply (regcache, regno, regs + offset);
1244 break;
1246 else if (regno == -1)
1248 regcache_raw_supply (regcache, regn, regs + offset);
1251 offset += regsize;
1254 else
1255 tracefile_fetch_registers (regcache, regno);
1258 /* This is the implementation of target_ops method to_xfer_partial.
1259 Iterate over events whose name is "memory" in
1260 current frame, extract the address and length from events. If
1261 OFFSET is within the range, read the contents from events to
1262 READBUF. */
1264 static enum target_xfer_status
1265 ctf_xfer_partial (struct target_ops *ops, enum target_object object,
1266 const char *annex, gdb_byte *readbuf,
1267 const gdb_byte *writebuf, ULONGEST offset,
1268 ULONGEST len, ULONGEST *xfered_len)
1270 /* We're only doing regular memory for now. */
1271 if (object != TARGET_OBJECT_MEMORY)
1272 return -1;
1274 if (readbuf == NULL)
1275 error (_("ctf_xfer_partial: trace file is read-only"));
1277 if (get_traceframe_number () != -1)
1279 struct bt_iter_pos *pos;
1280 int i = 0;
1281 enum target_xfer_status res;
1282 /* Records the lowest available address of all blocks that
1283 intersects the requested range. */
1284 ULONGEST low_addr_available = 0;
1286 gdb_assert (ctf_iter != NULL);
1287 /* Save the current position. */
1288 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1289 gdb_assert (pos->type == BT_SEEK_RESTORE);
1291 /* Iterate through the traceframe's blocks, looking for
1292 memory. */
1293 while (1)
1295 ULONGEST amt;
1296 uint64_t maddr;
1297 uint16_t mlen;
1298 enum bfd_endian byte_order
1299 = gdbarch_byte_order (target_gdbarch ());
1300 const struct bt_definition *scope;
1301 const struct bt_definition *def;
1302 struct bt_ctf_event *event
1303 = bt_ctf_iter_read_event (ctf_iter);
1304 const char *name = bt_ctf_event_name (event);
1306 if (name == NULL || strcmp (name, "frame") == 0)
1307 break;
1308 else if (strcmp (name, "memory") != 0)
1310 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1311 break;
1313 continue;
1316 scope = bt_ctf_get_top_level_scope (event,
1317 BT_EVENT_FIELDS);
1319 def = bt_ctf_get_field (event, scope, "address");
1320 maddr = bt_ctf_get_uint64 (def);
1321 def = bt_ctf_get_field (event, scope, "length");
1322 mlen = (uint16_t) bt_ctf_get_uint64 (def);
1324 /* If the block includes the first part of the desired
1325 range, return as much it has; GDB will re-request the
1326 remainder, which might be in a different block of this
1327 trace frame. */
1328 if (maddr <= offset && offset < (maddr + mlen))
1330 const struct bt_definition *array
1331 = bt_ctf_get_field (event, scope, "contents");
1332 const struct bt_declaration *decl
1333 = bt_ctf_get_decl_from_def (array);
1334 gdb_byte *contents;
1335 int k;
1337 contents = xmalloc (mlen);
1339 for (k = 0; k < mlen; k++)
1341 const struct bt_definition *element
1342 = bt_ctf_get_index (event, array, k);
1344 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
1347 amt = (maddr + mlen) - offset;
1348 if (amt > len)
1349 amt = len;
1351 memcpy (readbuf, &contents[offset - maddr], amt);
1353 xfree (contents);
1355 /* Restore the position. */
1356 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1358 if (amt == 0)
1359 return TARGET_XFER_EOF;
1360 else
1362 *xfered_len = amt;
1363 return TARGET_XFER_OK;
1367 if (offset < maddr && maddr < (offset + len))
1368 if (low_addr_available == 0 || low_addr_available > maddr)
1369 low_addr_available = maddr;
1371 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1372 break;
1375 /* Restore the position. */
1376 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1378 /* Requested memory is unavailable in the context of traceframes,
1379 and this address falls within a read-only section, fallback
1380 to reading from executable, up to LOW_ADDR_AVAILABLE */
1381 if (offset < low_addr_available)
1382 len = min (len, low_addr_available - offset);
1383 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1385 if (res == TARGET_XFER_OK)
1386 return TARGET_XFER_OK;
1387 else
1389 /* No use trying further, we know some memory starting
1390 at MEMADDR isn't available. */
1391 *xfered_len = len;
1392 return TARGET_XFER_UNAVAILABLE;
1395 else
1397 /* Fallback to reading from read-only sections. */
1398 return section_table_read_available_memory (readbuf, offset, len, xfered_len);
1402 /* This is the implementation of target_ops method
1403 to_get_trace_state_variable_value.
1404 Iterate over events whose name is "tsv" in current frame. When the
1405 trace variable is found, set the value of it to *VAL and return
1406 true, otherwise return false. */
1408 static int
1409 ctf_get_trace_state_variable_value (struct target_ops *self,
1410 int tsvnum, LONGEST *val)
1412 struct bt_iter_pos *pos;
1413 int found = 0;
1415 gdb_assert (ctf_iter != NULL);
1416 /* Save the current position. */
1417 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1418 gdb_assert (pos->type == BT_SEEK_RESTORE);
1420 /* Iterate through the traceframe's blocks, looking for 'V'
1421 block. */
1422 while (1)
1424 struct bt_ctf_event *event
1425 = bt_ctf_iter_read_event (ctf_iter);
1426 const char *name = bt_ctf_event_name (event);
1428 if (name == NULL || strcmp (name, "frame") == 0)
1429 break;
1430 else if (strcmp (name, "tsv") == 0)
1432 const struct bt_definition *scope;
1433 const struct bt_definition *def;
1435 scope = bt_ctf_get_top_level_scope (event,
1436 BT_EVENT_FIELDS);
1438 def = bt_ctf_get_field (event, scope, "num");
1439 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
1441 def = bt_ctf_get_field (event, scope, "val");
1442 *val = bt_ctf_get_uint64 (def);
1444 found = 1;
1448 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1449 break;
1452 /* Restore the position. */
1453 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1455 return found;
1458 /* Return the tracepoint number in "frame" event. */
1460 static int
1461 ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
1463 /* The packet context of events has a field "tpnum". */
1464 const struct bt_definition *scope
1465 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
1466 uint64_t tpnum
1467 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
1469 return (int) tpnum;
1472 /* Return the address at which the current frame was collected. */
1474 static CORE_ADDR
1475 ctf_get_traceframe_address (void)
1477 struct bt_ctf_event *event = NULL;
1478 struct bt_iter_pos *pos;
1479 CORE_ADDR addr = 0;
1481 gdb_assert (ctf_iter != NULL);
1482 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1483 gdb_assert (pos->type == BT_SEEK_RESTORE);
1485 while (1)
1487 const char *name;
1488 struct bt_ctf_event *event1;
1490 event1 = bt_ctf_iter_read_event (ctf_iter);
1492 name = bt_ctf_event_name (event1);
1494 if (name == NULL)
1495 break;
1496 else if (strcmp (name, "frame") == 0)
1498 event = event1;
1499 break;
1502 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1503 break;
1506 if (event != NULL)
1508 int tpnum = ctf_get_tpnum_from_frame_event (event);
1509 struct tracepoint *tp
1510 = get_tracepoint_by_number_on_target (tpnum);
1512 if (tp && tp->base.loc)
1513 addr = tp->base.loc->address;
1516 /* Restore the position. */
1517 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1519 return addr;
1522 /* This is the implementation of target_ops method to_trace_find.
1523 Iterate the events whose name is "frame", extract the tracepoint
1524 number in it. Return traceframe number when matched. */
1526 static int
1527 ctf_trace_find (struct target_ops *self, enum trace_find_type type, int num,
1528 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
1530 int ret = -1;
1531 int tfnum = 0;
1532 int found = 0;
1533 struct bt_iter_pos pos;
1535 if (num == -1)
1537 if (tpp != NULL)
1538 *tpp = -1;
1539 return -1;
1542 gdb_assert (ctf_iter != NULL);
1543 /* Set iterator back to the start. */
1544 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
1546 while (1)
1548 int id;
1549 struct bt_ctf_event *event;
1550 const char *name;
1552 event = bt_ctf_iter_read_event (ctf_iter);
1554 name = bt_ctf_event_name (event);
1556 if (event == NULL || name == NULL)
1557 break;
1559 if (strcmp (name, "frame") == 0)
1561 CORE_ADDR tfaddr;
1563 if (type == tfind_number)
1565 /* Looking for a specific trace frame. */
1566 if (tfnum == num)
1567 found = 1;
1569 else
1571 /* Start from the _next_ trace frame. */
1572 if (tfnum > get_traceframe_number ())
1574 switch (type)
1576 case tfind_tp:
1578 struct tracepoint *tp = get_tracepoint (num);
1580 if (tp != NULL
1581 && (tp->number_on_target
1582 == ctf_get_tpnum_from_frame_event (event)))
1583 found = 1;
1584 break;
1586 case tfind_pc:
1587 tfaddr = ctf_get_traceframe_address ();
1588 if (tfaddr == addr1)
1589 found = 1;
1590 break;
1591 case tfind_range:
1592 tfaddr = ctf_get_traceframe_address ();
1593 if (addr1 <= tfaddr && tfaddr <= addr2)
1594 found = 1;
1595 break;
1596 case tfind_outside:
1597 tfaddr = ctf_get_traceframe_address ();
1598 if (!(addr1 <= tfaddr && tfaddr <= addr2))
1599 found = 1;
1600 break;
1601 default:
1602 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
1606 if (found)
1608 if (tpp != NULL)
1609 *tpp = ctf_get_tpnum_from_frame_event (event);
1611 /* Skip the event "frame". */
1612 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1614 return tfnum;
1616 tfnum++;
1619 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1620 break;
1623 return -1;
1626 /* This is the implementation of target_ops method to_traceframe_info.
1627 Iterate the events whose name is "memory", in current
1628 frame, extract memory range information, and return them in
1629 traceframe_info. */
1631 static struct traceframe_info *
1632 ctf_traceframe_info (struct target_ops *self)
1634 struct traceframe_info *info = XCNEW (struct traceframe_info);
1635 const char *name;
1636 struct bt_iter_pos *pos;
1638 gdb_assert (ctf_iter != NULL);
1639 /* Save the current position. */
1640 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1641 gdb_assert (pos->type == BT_SEEK_RESTORE);
1645 struct bt_ctf_event *event
1646 = bt_ctf_iter_read_event (ctf_iter);
1648 name = bt_ctf_event_name (event);
1650 if (name == NULL || strcmp (name, "register") == 0
1651 || strcmp (name, "frame") == 0)
1653 else if (strcmp (name, "memory") == 0)
1655 const struct bt_definition *scope
1656 = bt_ctf_get_top_level_scope (event,
1657 BT_EVENT_FIELDS);
1658 const struct bt_definition *def;
1659 struct mem_range *r;
1661 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1662 def = bt_ctf_get_field (event, scope, "address");
1663 r->start = bt_ctf_get_uint64 (def);
1665 def = bt_ctf_get_field (event, scope, "length");
1666 r->length = (uint16_t) bt_ctf_get_uint64 (def);
1668 else if (strcmp (name, "tsv") == 0)
1670 int vnum;
1671 const struct bt_definition *scope
1672 = bt_ctf_get_top_level_scope (event,
1673 BT_EVENT_FIELDS);
1674 const struct bt_definition *def;
1676 def = bt_ctf_get_field (event, scope, "num");
1677 vnum = (int) bt_ctf_get_int64 (def);
1678 VEC_safe_push (int, info->tvars, vnum);
1680 else
1682 warning (_("Unhandled trace block type (%s) "
1683 "while building trace frame info."),
1684 name);
1687 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1688 break;
1690 while (name != NULL && strcmp (name, "frame") != 0);
1692 /* Restore the position. */
1693 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1695 return info;
1698 static void
1699 init_ctf_ops (void)
1701 memset (&ctf_ops, 0, sizeof (ctf_ops));
1703 init_tracefile_ops (&ctf_ops);
1704 ctf_ops.to_shortname = "ctf";
1705 ctf_ops.to_longname = "CTF file";
1706 ctf_ops.to_doc = "Use a CTF directory as a target.\n\
1707 Specify the filename of the CTF directory.";
1708 ctf_ops.to_open = ctf_open;
1709 ctf_ops.to_close = ctf_close;
1710 ctf_ops.to_fetch_registers = ctf_fetch_registers;
1711 ctf_ops.to_xfer_partial = ctf_xfer_partial;
1712 ctf_ops.to_files_info = ctf_files_info;
1713 ctf_ops.to_trace_find = ctf_trace_find;
1714 ctf_ops.to_get_trace_state_variable_value
1715 = ctf_get_trace_state_variable_value;
1716 ctf_ops.to_traceframe_info = ctf_traceframe_info;
1719 #endif
1721 /* -Wmissing-prototypes */
1723 extern initialize_file_ftype _initialize_ctf;
1725 /* module initialization */
1727 void
1728 _initialize_ctf (void)
1730 #if HAVE_LIBBABELTRACE
1731 init_ctf_ops ();
1733 add_target_with_completer (&ctf_ops, filename_completer);
1734 #endif