]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ctf.c
Remove regcache_register_status
[thirdparty/binutils-gdb.git] / gdb / ctf.c
1 /* CTF format support.
2
3 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 Contributed by Hui Zhu <hui_zhu@mentor.com>
5 Contributed by Yao Qi <yao@codesourcery.com>
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
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"
32 #include <ctype.h>
33 #include <algorithm>
34
35 /* The CTF target. */
36
37 static const target_info ctf_target_info = {
38 "ctf",
39 N_("CTF file"),
40 N_("(Use a CTF directory as a target.\n\
41 Specify the filename of the CTF directory.")
42 };
43
44 class ctf_target final : public tracefile_target
45 {
46 public:
47 const target_info &info () const override
48 { return ctf_target_info; }
49
50 void close () override;
51 void fetch_registers (struct regcache *, int) override;
52 enum target_xfer_status xfer_partial (enum target_object object,
53 const char *annex,
54 gdb_byte *readbuf,
55 const gdb_byte *writebuf,
56 ULONGEST offset, ULONGEST len,
57 ULONGEST *xfered_len) override;
58 void files_info () override;
59 int trace_find (enum trace_find_type type, int num,
60 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override;
61 bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
62 traceframe_info_up traceframe_info () override;
63 };
64
65 /* GDB saves trace buffers and other information (such as trace
66 status) got from the remote target into Common Trace Format (CTF).
67 The following types of information are expected to save in CTF:
68
69 1. The length (in bytes) of register cache. Event "register" will
70 be defined in metadata, which includes the length.
71
72 2. Trace status. Event "status" is defined in metadata, which
73 includes all aspects of trace status.
74
75 3. Uploaded trace variables. Event "tsv_def" is defined in
76 metadata, which is about all aspects of a uploaded trace variable.
77 Uploaded tracepoints. Event "tp_def" is defined in meta, which
78 is about all aspects of an uploaded tracepoint. Note that the
79 "sequence" (a CTF type, which is a dynamically-sized array.) is
80 used for "actions" "step_actions" and "cmd_strings".
81
82 4. Trace frames. Each trace frame is composed by several blocks
83 of different types ('R', 'M', 'V'). One trace frame is saved in
84 one CTF packet and the blocks of this frame are saved as events.
85 4.1: The trace frame related information (such as the number of
86 tracepoint associated with this frame) is saved in the packet
87 context.
88 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
89 "register" and "tsv" respectively.
90 4.3: When iterating over events, babeltrace can't tell iterator
91 goes to a new packet, so we need a marker or anchor to tell GDB
92 that iterator goes into a new packet or frame. We define event
93 "frame". */
94
95 #define CTF_MAGIC 0xC1FC1FC1
96 #define CTF_SAVE_MAJOR 1
97 #define CTF_SAVE_MINOR 8
98
99 #define CTF_METADATA_NAME "metadata"
100 #define CTF_DATASTREAM_NAME "datastream"
101
102 /* Reserved event id. */
103
104 #define CTF_EVENT_ID_REGISTER 0
105 #define CTF_EVENT_ID_TSV 1
106 #define CTF_EVENT_ID_MEMORY 2
107 #define CTF_EVENT_ID_FRAME 3
108 #define CTF_EVENT_ID_STATUS 4
109 #define CTF_EVENT_ID_TSV_DEF 5
110 #define CTF_EVENT_ID_TP_DEF 6
111
112 #define CTF_PID (2)
113
114 /* The state kept while writing the CTF datastream file. */
115
116 struct trace_write_handler
117 {
118 /* File descriptor of metadata. */
119 FILE *metadata_fd;
120 /* File descriptor of traceframes. */
121 FILE *datastream_fd;
122
123 /* This is the content size of the current packet. */
124 size_t content_size;
125
126 /* This is the start offset of current packet. */
127 long packet_start;
128 };
129
130 /* Write metadata in FORMAT. */
131
132 static void
133 ctf_save_write_metadata (struct trace_write_handler *handler,
134 const char *format, ...)
135 ATTRIBUTE_PRINTF (2, 3);
136
137 static void
138 ctf_save_write_metadata (struct trace_write_handler *handler,
139 const char *format, ...)
140 {
141 va_list args;
142
143 va_start (args, format);
144 if (vfprintf (handler->metadata_fd, format, args) < 0)
145 error (_("Unable to write metadata file (%s)"),
146 safe_strerror (errno));
147 va_end (args);
148 }
149
150 /* Write BUF of length SIZE to datastream file represented by
151 HANDLER. */
152
153 static int
154 ctf_save_write (struct trace_write_handler *handler,
155 const gdb_byte *buf, size_t size)
156 {
157 if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
158 error (_("Unable to write file for saving trace data (%s)"),
159 safe_strerror (errno));
160
161 handler->content_size += size;
162
163 return 0;
164 }
165
166 /* Write a unsigned 32-bit integer to datastream file represented by
167 HANDLER. */
168
169 #define ctf_save_write_uint32(HANDLER, U32) \
170 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
171
172 /* Write a signed 32-bit integer to datastream file represented by
173 HANDLER. */
174
175 #define ctf_save_write_int32(HANDLER, INT32) \
176 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
177
178 /* Set datastream file position. Update HANDLER->content_size
179 if WHENCE is SEEK_CUR. */
180
181 static int
182 ctf_save_fseek (struct trace_write_handler *handler, long offset,
183 int whence)
184 {
185 gdb_assert (whence != SEEK_END);
186 gdb_assert (whence != SEEK_SET
187 || offset <= handler->content_size + handler->packet_start);
188
189 if (fseek (handler->datastream_fd, offset, whence))
190 error (_("Unable to seek file for saving trace data (%s)"),
191 safe_strerror (errno));
192
193 if (whence == SEEK_CUR)
194 handler->content_size += offset;
195
196 return 0;
197 }
198
199 /* Change the datastream file position to align on ALIGN_SIZE,
200 and write BUF to datastream file. The size of BUF is SIZE. */
201
202 static int
203 ctf_save_align_write (struct trace_write_handler *handler,
204 const gdb_byte *buf,
205 size_t size, size_t align_size)
206 {
207 long offset
208 = (align_up (handler->content_size, align_size)
209 - handler->content_size);
210
211 if (ctf_save_fseek (handler, offset, SEEK_CUR))
212 return -1;
213
214 if (ctf_save_write (handler, buf, size))
215 return -1;
216
217 return 0;
218 }
219
220 /* Write events to next new packet. */
221
222 static void
223 ctf_save_next_packet (struct trace_write_handler *handler)
224 {
225 handler->packet_start += (handler->content_size + 4);
226 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
227 handler->content_size = 0;
228 }
229
230 /* Write the CTF metadata header. */
231
232 static void
233 ctf_save_metadata_header (struct trace_write_handler *handler)
234 {
235 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
236 CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
237 ctf_save_write_metadata (handler,
238 "typealias integer { size = 8; align = 8; "
239 "signed = false; encoding = ascii;}"
240 " := ascii;\n");
241 ctf_save_write_metadata (handler,
242 "typealias integer { size = 8; align = 8; "
243 "signed = false; }"
244 " := uint8_t;\n");
245 ctf_save_write_metadata (handler,
246 "typealias integer { size = 16; align = 16;"
247 "signed = false; } := uint16_t;\n");
248 ctf_save_write_metadata (handler,
249 "typealias integer { size = 32; align = 32;"
250 "signed = false; } := uint32_t;\n");
251 ctf_save_write_metadata (handler,
252 "typealias integer { size = 64; align = 64;"
253 "signed = false; base = hex;}"
254 " := uint64_t;\n");
255 ctf_save_write_metadata (handler,
256 "typealias integer { size = 32; align = 32;"
257 "signed = true; } := int32_t;\n");
258 ctf_save_write_metadata (handler,
259 "typealias integer { size = 64; align = 64;"
260 "signed = true; } := int64_t;\n");
261 ctf_save_write_metadata (handler,
262 "typealias string { encoding = ascii;"
263 " } := chars;\n");
264 ctf_save_write_metadata (handler, "\n");
265
266 /* Get the byte order of the host and write CTF data in this byte
267 order. */
268 #if WORDS_BIGENDIAN
269 #define HOST_ENDIANNESS "be"
270 #else
271 #define HOST_ENDIANNESS "le"
272 #endif
273
274 ctf_save_write_metadata (handler,
275 "\ntrace {\n"
276 " major = %u;\n"
277 " minor = %u;\n"
278 " byte_order = %s;\n"
279 " packet.header := struct {\n"
280 " uint32_t magic;\n"
281 " };\n"
282 "};\n"
283 "\n"
284 "stream {\n"
285 " packet.context := struct {\n"
286 " uint32_t content_size;\n"
287 " uint32_t packet_size;\n"
288 " uint16_t tpnum;\n"
289 " };\n"
290 " event.header := struct {\n"
291 " uint32_t id;\n"
292 " };\n"
293 "};\n",
294 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
295 HOST_ENDIANNESS);
296 ctf_save_write_metadata (handler, "\n");
297 }
298
299 /* CTF trace writer. */
300
301 struct ctf_trace_file_writer
302 {
303 struct trace_file_writer base;
304
305 /* States related to writing CTF trace file. */
306 struct trace_write_handler tcs;
307 };
308
309 /* This is the implementation of trace_file_write_ops method
310 dtor. */
311
312 static void
313 ctf_dtor (struct trace_file_writer *self)
314 {
315 struct ctf_trace_file_writer *writer
316 = (struct ctf_trace_file_writer *) self;
317
318 if (writer->tcs.metadata_fd != NULL)
319 fclose (writer->tcs.metadata_fd);
320
321 if (writer->tcs.datastream_fd != NULL)
322 fclose (writer->tcs.datastream_fd);
323
324 }
325
326 /* This is the implementation of trace_file_write_ops method
327 target_save. */
328
329 static int
330 ctf_target_save (struct trace_file_writer *self,
331 const char *dirname)
332 {
333 /* Don't support save trace file to CTF format in the target. */
334 return 0;
335 }
336
337 /* This is the implementation of trace_file_write_ops method
338 start. It creates the directory DIRNAME, metadata and datastream
339 in the directory. */
340
341 static void
342 ctf_start (struct trace_file_writer *self, const char *dirname)
343 {
344 struct ctf_trace_file_writer *writer
345 = (struct ctf_trace_file_writer *) self;
346 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH;
347
348 /* Create DIRNAME. */
349 if (mkdir (dirname, hmode) && errno != EEXIST)
350 error (_("Unable to open directory '%s' for saving trace data (%s)"),
351 dirname, safe_strerror (errno));
352
353 memset (&writer->tcs, '\0', sizeof (writer->tcs));
354
355 std::string file_name = string_printf ("%s/%s", dirname, CTF_METADATA_NAME);
356
357 writer->tcs.metadata_fd = fopen (file_name.c_str (), "w");
358 if (writer->tcs.metadata_fd == NULL)
359 error (_("Unable to open file '%s' for saving trace data (%s)"),
360 file_name.c_str (), safe_strerror (errno));
361
362 ctf_save_metadata_header (&writer->tcs);
363
364 file_name = string_printf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
365 writer->tcs.datastream_fd = fopen (file_name.c_str (), "w");
366 if (writer->tcs.datastream_fd == NULL)
367 error (_("Unable to open file '%s' for saving trace data (%s)"),
368 file_name.c_str (), safe_strerror (errno));
369 }
370
371 /* This is the implementation of trace_file_write_ops method
372 write_header. Write the types of events on trace variable and
373 frame. */
374
375 static void
376 ctf_write_header (struct trace_file_writer *self)
377 {
378 struct ctf_trace_file_writer *writer
379 = (struct ctf_trace_file_writer *) self;
380
381
382 ctf_save_write_metadata (&writer->tcs, "\n");
383 ctf_save_write_metadata (&writer->tcs,
384 "event {\n\tname = \"memory\";\n\tid = %u;\n"
385 "\tfields := struct { \n"
386 "\t\tuint64_t address;\n"
387 "\t\tuint16_t length;\n"
388 "\t\tuint8_t contents[length];\n"
389 "\t};\n"
390 "};\n", CTF_EVENT_ID_MEMORY);
391
392 ctf_save_write_metadata (&writer->tcs, "\n");
393 ctf_save_write_metadata (&writer->tcs,
394 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
395 "\tfields := struct { \n"
396 "\t\tuint64_t val;\n"
397 "\t\tuint32_t num;\n"
398 "\t};\n"
399 "};\n", CTF_EVENT_ID_TSV);
400
401 ctf_save_write_metadata (&writer->tcs, "\n");
402 ctf_save_write_metadata (&writer->tcs,
403 "event {\n\tname = \"frame\";\n\tid = %u;\n"
404 "\tfields := struct { \n"
405 "\t};\n"
406 "};\n", CTF_EVENT_ID_FRAME);
407
408 ctf_save_write_metadata (&writer->tcs, "\n");
409 ctf_save_write_metadata (&writer->tcs,
410 "event {\n\tname = \"tsv_def\";\n"
411 "\tid = %u;\n\tfields := struct { \n"
412 "\t\tint64_t initial_value;\n"
413 "\t\tint32_t number;\n"
414 "\t\tint32_t builtin;\n"
415 "\t\tchars name;\n"
416 "\t};\n"
417 "};\n", CTF_EVENT_ID_TSV_DEF);
418
419 ctf_save_write_metadata (&writer->tcs, "\n");
420 ctf_save_write_metadata (&writer->tcs,
421 "event {\n\tname = \"tp_def\";\n"
422 "\tid = %u;\n\tfields := struct { \n"
423 "\t\tuint64_t addr;\n"
424 "\t\tuint64_t traceframe_usage;\n"
425 "\t\tint32_t number;\n"
426 "\t\tint32_t enabled;\n"
427 "\t\tint32_t step;\n"
428 "\t\tint32_t pass;\n"
429 "\t\tint32_t hit_count;\n"
430 "\t\tint32_t type;\n"
431 "\t\tchars cond;\n"
432
433 "\t\tuint32_t action_num;\n"
434 "\t\tchars actions[action_num];\n"
435
436 "\t\tuint32_t step_action_num;\n"
437 "\t\tchars step_actions[step_action_num];\n"
438
439 "\t\tchars at_string;\n"
440 "\t\tchars cond_string;\n"
441
442 "\t\tuint32_t cmd_num;\n"
443 "\t\tchars cmd_strings[cmd_num];\n"
444 "\t};\n"
445 "};\n", CTF_EVENT_ID_TP_DEF);
446
447 gdb_assert (writer->tcs.content_size == 0);
448 gdb_assert (writer->tcs.packet_start == 0);
449
450 /* Create a new packet to contain this event. */
451 self->ops->frame_ops->start (self, 0);
452 }
453
454 /* This is the implementation of trace_file_write_ops method
455 write_regblock_type. Write the type of register event in
456 metadata. */
457
458 static void
459 ctf_write_regblock_type (struct trace_file_writer *self, int size)
460 {
461 struct ctf_trace_file_writer *writer
462 = (struct ctf_trace_file_writer *) self;
463
464 ctf_save_write_metadata (&writer->tcs, "\n");
465
466 ctf_save_write_metadata (&writer->tcs,
467 "event {\n\tname = \"register\";\n\tid = %u;\n"
468 "\tfields := struct { \n"
469 "\t\tascii contents[%d];\n"
470 "\t};\n"
471 "};\n",
472 CTF_EVENT_ID_REGISTER, size);
473 }
474
475 /* This is the implementation of trace_file_write_ops method
476 write_status. */
477
478 static void
479 ctf_write_status (struct trace_file_writer *self,
480 struct trace_status *ts)
481 {
482 struct ctf_trace_file_writer *writer
483 = (struct ctf_trace_file_writer *) self;
484 uint32_t id;
485
486 ctf_save_write_metadata (&writer->tcs, "\n");
487 ctf_save_write_metadata (&writer->tcs,
488 "event {\n\tname = \"status\";\n\tid = %u;\n"
489 "\tfields := struct { \n"
490 "\t\tint32_t stop_reason;\n"
491 "\t\tint32_t stopping_tracepoint;\n"
492 "\t\tint32_t traceframe_count;\n"
493 "\t\tint32_t traceframes_created;\n"
494 "\t\tint32_t buffer_free;\n"
495 "\t\tint32_t buffer_size;\n"
496 "\t\tint32_t disconnected_tracing;\n"
497 "\t\tint32_t circular_buffer;\n"
498 "\t};\n"
499 "};\n",
500 CTF_EVENT_ID_STATUS);
501
502 id = CTF_EVENT_ID_STATUS;
503 /* Event Id. */
504 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
505
506 ctf_save_write_int32 (&writer->tcs, ts->stop_reason);
507 ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint);
508 ctf_save_write_int32 (&writer->tcs, ts->traceframe_count);
509 ctf_save_write_int32 (&writer->tcs, ts->traceframes_created);
510 ctf_save_write_int32 (&writer->tcs, ts->buffer_free);
511 ctf_save_write_int32 (&writer->tcs, ts->buffer_size);
512 ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing);
513 ctf_save_write_int32 (&writer->tcs, ts->circular_buffer);
514 }
515
516 /* This is the implementation of trace_file_write_ops method
517 write_uploaded_tsv. */
518
519 static void
520 ctf_write_uploaded_tsv (struct trace_file_writer *self,
521 struct uploaded_tsv *tsv)
522 {
523 struct ctf_trace_file_writer *writer
524 = (struct ctf_trace_file_writer *) self;
525 int32_t int32;
526 int64_t int64;
527 const gdb_byte zero = 0;
528
529 /* Event Id. */
530 int32 = CTF_EVENT_ID_TSV_DEF;
531 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
532
533 /* initial_value */
534 int64 = tsv->initial_value;
535 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
536
537 /* number */
538 ctf_save_write_int32 (&writer->tcs, tsv->number);
539
540 /* builtin */
541 ctf_save_write_int32 (&writer->tcs, tsv->builtin);
542
543 /* name */
544 if (tsv->name != NULL)
545 ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name,
546 strlen (tsv->name));
547 ctf_save_write (&writer->tcs, &zero, 1);
548 }
549
550 /* This is the implementation of trace_file_write_ops method
551 write_uploaded_tp. */
552
553 static void
554 ctf_write_uploaded_tp (struct trace_file_writer *self,
555 struct uploaded_tp *tp)
556 {
557 struct ctf_trace_file_writer *writer
558 = (struct ctf_trace_file_writer *) self;
559 int32_t int32;
560 int64_t int64;
561 uint32_t u32;
562 const gdb_byte zero = 0;
563
564 /* Event Id. */
565 int32 = CTF_EVENT_ID_TP_DEF;
566 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
567
568 /* address */
569 int64 = tp->addr;
570 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
571
572 /* traceframe_usage */
573 int64 = tp->traceframe_usage;
574 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
575
576 /* number */
577 ctf_save_write_int32 (&writer->tcs, tp->number);
578
579 /* enabled */
580 ctf_save_write_int32 (&writer->tcs, tp->enabled);
581
582 /* step */
583 ctf_save_write_int32 (&writer->tcs, tp->step);
584
585 /* pass */
586 ctf_save_write_int32 (&writer->tcs, tp->pass);
587
588 /* hit_count */
589 ctf_save_write_int32 (&writer->tcs, tp->hit_count);
590
591 /* type */
592 ctf_save_write_int32 (&writer->tcs, tp->type);
593
594 /* condition */
595 if (tp->cond != NULL)
596 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond, strlen (tp->cond));
597 ctf_save_write (&writer->tcs, &zero, 1);
598
599 /* actions */
600 u32 = tp->actions.size ();
601 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
602 for (char *act : tp->actions)
603 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
604
605 /* step_actions */
606 u32 = tp->step_actions.size ();
607 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
608 for (char *act : tp->step_actions)
609 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
610
611 /* at_string */
612 if (tp->at_string != NULL)
613 ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string,
614 strlen (tp->at_string));
615 ctf_save_write (&writer->tcs, &zero, 1);
616
617 /* cond_string */
618 if (tp->cond_string != NULL)
619 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string,
620 strlen (tp->cond_string));
621 ctf_save_write (&writer->tcs, &zero, 1);
622
623 /* cmd_strings */
624 u32 = tp->cmd_strings.size ();
625 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
626 for (char *act : tp->cmd_strings)
627 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
628
629 }
630
631 /* This is the implementation of trace_file_write_ops method
632 write_tdesc. */
633
634 static void
635 ctf_write_tdesc (struct trace_file_writer *self)
636 {
637 /* Nothing so far. */
638 }
639
640 /* This is the implementation of trace_file_write_ops method
641 write_definition_end. */
642
643 static void
644 ctf_write_definition_end (struct trace_file_writer *self)
645 {
646 self->ops->frame_ops->end (self);
647 }
648
649 /* This is the implementation of trace_file_write_ops method
650 end. */
651
652 static void
653 ctf_end (struct trace_file_writer *self)
654 {
655 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
656
657 gdb_assert (writer->tcs.content_size == 0);
658 }
659
660 /* This is the implementation of trace_frame_write_ops method
661 start. */
662
663 static void
664 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
665 {
666 struct ctf_trace_file_writer *writer
667 = (struct ctf_trace_file_writer *) self;
668 uint32_t id = CTF_EVENT_ID_FRAME;
669 uint32_t u32;
670
671 /* Step 1: Write packet context. */
672 /* magic. */
673 u32 = CTF_MAGIC;
674 ctf_save_write_uint32 (&writer->tcs, u32);
675 /* content_size and packet_size.. We still don't know the value,
676 write it later. */
677 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
678 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
679 /* Tracepoint number. */
680 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
681
682 /* Step 2: Write event "frame". */
683 /* Event Id. */
684 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
685 }
686
687 /* This is the implementation of trace_frame_write_ops method
688 write_r_block. */
689
690 static void
691 ctf_write_frame_r_block (struct trace_file_writer *self,
692 gdb_byte *buf, int32_t size)
693 {
694 struct ctf_trace_file_writer *writer
695 = (struct ctf_trace_file_writer *) self;
696 uint32_t id = CTF_EVENT_ID_REGISTER;
697
698 /* Event Id. */
699 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
700
701 /* array contents. */
702 ctf_save_align_write (&writer->tcs, buf, size, 1);
703 }
704
705 /* This is the implementation of trace_frame_write_ops method
706 write_m_block_header. */
707
708 static void
709 ctf_write_frame_m_block_header (struct trace_file_writer *self,
710 uint64_t addr, uint16_t length)
711 {
712 struct ctf_trace_file_writer *writer
713 = (struct ctf_trace_file_writer *) self;
714 uint32_t event_id = CTF_EVENT_ID_MEMORY;
715
716 /* Event Id. */
717 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
718
719 /* Address. */
720 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
721
722 /* Length. */
723 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
724 }
725
726 /* This is the implementation of trace_frame_write_ops method
727 write_m_block_memory. */
728
729 static void
730 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
731 gdb_byte *buf, uint16_t length)
732 {
733 struct ctf_trace_file_writer *writer
734 = (struct ctf_trace_file_writer *) self;
735
736 /* Contents. */
737 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
738 }
739
740 /* This is the implementation of trace_frame_write_ops method
741 write_v_block. */
742
743 static void
744 ctf_write_frame_v_block (struct trace_file_writer *self,
745 int32_t num, uint64_t val)
746 {
747 struct ctf_trace_file_writer *writer
748 = (struct ctf_trace_file_writer *) self;
749 uint32_t id = CTF_EVENT_ID_TSV;
750
751 /* Event Id. */
752 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
753
754 /* val. */
755 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
756 /* num. */
757 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
758 }
759
760 /* This is the implementation of trace_frame_write_ops method
761 end. */
762
763 static void
764 ctf_write_frame_end (struct trace_file_writer *self)
765 {
766 struct ctf_trace_file_writer *writer
767 = (struct ctf_trace_file_writer *) self;
768 uint32_t u32;
769 uint32_t t;
770
771 /* Write the content size to packet header. */
772 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
773 SEEK_SET);
774 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
775
776 t = writer->tcs.content_size;
777 ctf_save_write_uint32 (&writer->tcs, u32);
778
779 /* Write the packet size. */
780 u32 += 4 * TARGET_CHAR_BIT;
781 ctf_save_write_uint32 (&writer->tcs, u32);
782
783 writer->tcs.content_size = t;
784
785 /* Write zero at the end of the packet. */
786 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
787 SEEK_SET);
788 u32 = 0;
789 ctf_save_write_uint32 (&writer->tcs, u32);
790 writer->tcs.content_size = t;
791
792 ctf_save_next_packet (&writer->tcs);
793 }
794
795 /* Operations to write various types of trace frames into CTF
796 format. */
797
798 static const struct trace_frame_write_ops ctf_write_frame_ops =
799 {
800 ctf_write_frame_start,
801 ctf_write_frame_r_block,
802 ctf_write_frame_m_block_header,
803 ctf_write_frame_m_block_memory,
804 ctf_write_frame_v_block,
805 ctf_write_frame_end,
806 };
807
808 /* Operations to write trace buffers into CTF format. */
809
810 static const struct trace_file_write_ops ctf_write_ops =
811 {
812 ctf_dtor,
813 ctf_target_save,
814 ctf_start,
815 ctf_write_header,
816 ctf_write_regblock_type,
817 ctf_write_status,
818 ctf_write_uploaded_tsv,
819 ctf_write_uploaded_tp,
820 ctf_write_tdesc,
821 ctf_write_definition_end,
822 NULL,
823 &ctf_write_frame_ops,
824 ctf_end,
825 };
826
827 /* Return a trace writer for CTF format. */
828
829 struct trace_file_writer *
830 ctf_trace_file_writer_new (void)
831 {
832 struct ctf_trace_file_writer *writer = XNEW (struct ctf_trace_file_writer);
833
834 writer->base.ops = &ctf_write_ops;
835
836 return (struct trace_file_writer *) writer;
837 }
838
839 #if HAVE_LIBBABELTRACE
840 /* Use libbabeltrace to read CTF data. The libbabeltrace provides
841 iterator to iterate over each event in CTF data and APIs to get
842 details of event and packet, so it is very convenient to use
843 libbabeltrace to access events in CTF. */
844
845 #include <babeltrace/babeltrace.h>
846 #include <babeltrace/ctf/events.h>
847 #include <babeltrace/ctf/iterator.h>
848
849 /* The struct pointer for current CTF directory. */
850 static int handle_id = -1;
851 static struct bt_context *ctx = NULL;
852 static struct bt_ctf_iter *ctf_iter = NULL;
853 /* The position of the first packet containing trace frame. */
854 static struct bt_iter_pos *start_pos;
855
856 /* The name of CTF directory. */
857 static char *trace_dirname;
858
859 static ctf_target ctf_ops;
860
861 /* Destroy ctf iterator and context. */
862
863 static void
864 ctf_destroy (void)
865 {
866 if (ctf_iter != NULL)
867 {
868 bt_ctf_iter_destroy (ctf_iter);
869 ctf_iter = NULL;
870 }
871 if (ctx != NULL)
872 {
873 bt_context_put (ctx);
874 ctx = NULL;
875 }
876 }
877
878 /* Open CTF trace data in DIRNAME. */
879
880 static void
881 ctf_open_dir (const char *dirname)
882 {
883 struct bt_iter_pos begin_pos;
884 unsigned int count, i;
885 struct bt_ctf_event_decl * const *list;
886
887 ctx = bt_context_create ();
888 if (ctx == NULL)
889 error (_("Unable to create bt_context"));
890 handle_id = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
891 if (handle_id < 0)
892 {
893 ctf_destroy ();
894 error (_("Unable to use libbabeltrace on directory \"%s\""),
895 dirname);
896 }
897
898 begin_pos.type = BT_SEEK_BEGIN;
899 ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL);
900 if (ctf_iter == NULL)
901 {
902 ctf_destroy ();
903 error (_("Unable to create bt_iterator"));
904 }
905
906 /* Look for the declaration of register block. Get the length of
907 array "contents" to set trace_regblock_size. */
908
909 bt_ctf_get_event_decl_list (handle_id, ctx, &list, &count);
910 for (i = 0; i < count; i++)
911 if (strcmp ("register", bt_ctf_get_decl_event_name (list[i])) == 0)
912 {
913 const struct bt_ctf_field_decl * const *field_list;
914 const struct bt_declaration *decl;
915
916 bt_ctf_get_decl_fields (list[i], BT_EVENT_FIELDS, &field_list,
917 &count);
918
919 gdb_assert (count == 1);
920 gdb_assert (0 == strcmp ("contents",
921 bt_ctf_get_decl_field_name (field_list[0])));
922 decl = bt_ctf_get_decl_from_field_decl (field_list[0]);
923 trace_regblock_size = bt_ctf_get_array_len (decl);
924
925 break;
926 }
927 }
928
929 #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
930 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
931 (SCOPE), \
932 #FIELD))
933
934 #define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \
935 (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
936 (SCOPE), \
937 #FIELD))
938
939
940 /* EVENT is the "status" event and TS is filled in. */
941
942 static void
943 ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts)
944 {
945 const struct bt_definition *scope
946 = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS);
947
948 SET_ENUM_FIELD (event, scope, ts, enum trace_stop_reason, stop_reason);
949 SET_INT32_FIELD (event, scope, ts, stopping_tracepoint);
950 SET_INT32_FIELD (event, scope, ts, traceframe_count);
951 SET_INT32_FIELD (event, scope, ts, traceframes_created);
952 SET_INT32_FIELD (event, scope, ts, buffer_free);
953 SET_INT32_FIELD (event, scope, ts, buffer_size);
954 SET_INT32_FIELD (event, scope, ts, disconnected_tracing);
955 SET_INT32_FIELD (event, scope, ts, circular_buffer);
956
957 bt_iter_next (bt_ctf_get_iter (ctf_iter));
958 }
959
960 /* Read the events "tsv_def" one by one, extract its contents and fill
961 in the list UPLOADED_TSVS. */
962
963 static void
964 ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
965 {
966 gdb_assert (ctf_iter != NULL);
967
968 while (1)
969 {
970 struct bt_ctf_event *event;
971 const struct bt_definition *scope;
972 const struct bt_definition *def;
973 uint32_t event_id;
974 struct uploaded_tsv *utsv = NULL;
975
976 event = bt_ctf_iter_read_event (ctf_iter);
977 scope = bt_ctf_get_top_level_scope (event,
978 BT_STREAM_EVENT_HEADER);
979 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
980 "id"));
981 if (event_id != CTF_EVENT_ID_TSV_DEF)
982 break;
983
984 scope = bt_ctf_get_top_level_scope (event,
985 BT_EVENT_FIELDS);
986
987 def = bt_ctf_get_field (event, scope, "number");
988 utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def),
989 uploaded_tsvs);
990
991 def = bt_ctf_get_field (event, scope, "builtin");
992 utsv->builtin = (int32_t) bt_ctf_get_int64 (def);
993 def = bt_ctf_get_field (event, scope, "initial_value");
994 utsv->initial_value = bt_ctf_get_int64 (def);
995
996 def = bt_ctf_get_field (event, scope, "name");
997 utsv->name = xstrdup (bt_ctf_get_string (def));
998
999 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1000 break;
1001 }
1002
1003 }
1004
1005 /* Read the value of element whose index is NUM from CTF and write it
1006 to the corresponding VAR->ARRAY. */
1007
1008 #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
1009 do \
1010 { \
1011 uint32_t u32, i; \
1012 const struct bt_definition *def; \
1013 \
1014 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1015 (SCOPE), \
1016 #NUM)); \
1017 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1018 for (i = 0; i < u32; i++) \
1019 { \
1020 const struct bt_definition *element \
1021 = bt_ctf_get_index ((EVENT), def, i); \
1022 \
1023 (VAR)->ARRAY.push_back \
1024 (xstrdup (bt_ctf_get_string (element))); \
1025 } \
1026 } \
1027 while (0)
1028
1029 /* Read a string from CTF and set VAR->FIELD. If the length of string
1030 is zero, set VAR->FIELD to NULL. */
1031
1032 #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1033 do \
1034 { \
1035 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1036 (SCOPE), \
1037 #FIELD)); \
1038 \
1039 if (strlen (p) > 0) \
1040 (VAR)->FIELD = xstrdup (p); \
1041 else \
1042 (VAR)->FIELD = NULL; \
1043 } \
1044 while (0)
1045
1046 /* Read the events "tp_def" one by one, extract its contents and fill
1047 in the list UPLOADED_TPS. */
1048
1049 static void
1050 ctf_read_tp (struct uploaded_tp **uploaded_tps)
1051 {
1052 gdb_assert (ctf_iter != NULL);
1053
1054 while (1)
1055 {
1056 struct bt_ctf_event *event;
1057 const struct bt_definition *scope;
1058 uint32_t u32;
1059 int32_t int32;
1060 uint64_t u64;
1061 struct uploaded_tp *utp = NULL;
1062
1063 event = bt_ctf_iter_read_event (ctf_iter);
1064 scope = bt_ctf_get_top_level_scope (event,
1065 BT_STREAM_EVENT_HEADER);
1066 u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1067 "id"));
1068 if (u32 != CTF_EVENT_ID_TP_DEF)
1069 break;
1070
1071 scope = bt_ctf_get_top_level_scope (event,
1072 BT_EVENT_FIELDS);
1073 int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event,
1074 scope,
1075 "number"));
1076 u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1077 "addr"));
1078 utp = get_uploaded_tp (int32, u64, uploaded_tps);
1079
1080 SET_INT32_FIELD (event, scope, utp, enabled);
1081 SET_INT32_FIELD (event, scope, utp, step);
1082 SET_INT32_FIELD (event, scope, utp, pass);
1083 SET_INT32_FIELD (event, scope, utp, hit_count);
1084 SET_ENUM_FIELD (event, scope, utp, enum bptype, type);
1085
1086 /* Read 'cmd_strings'. */
1087 SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings);
1088 /* Read 'actions'. */
1089 SET_ARRAY_FIELD (event, scope, utp, action_num, actions);
1090 /* Read 'step_actions'. */
1091 SET_ARRAY_FIELD (event, scope, utp, step_action_num,
1092 step_actions);
1093
1094 SET_STRING_FIELD(event, scope, utp, at_string);
1095 SET_STRING_FIELD(event, scope, utp, cond_string);
1096
1097 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1098 break;
1099 }
1100 }
1101
1102 /* This is the implementation of target_ops method to_open. Open CTF
1103 trace data, read trace status, trace state variables and tracepoint
1104 definitions from the first packet. Set the start position at the
1105 second packet which contains events on trace blocks. */
1106
1107 static void
1108 ctf_target_open (const char *dirname, int from_tty)
1109 {
1110 struct bt_ctf_event *event;
1111 uint32_t event_id;
1112 const struct bt_definition *scope;
1113 struct uploaded_tsv *uploaded_tsvs = NULL;
1114 struct uploaded_tp *uploaded_tps = NULL;
1115
1116 if (!dirname)
1117 error (_("No CTF directory specified."));
1118
1119 ctf_open_dir (dirname);
1120
1121 target_preopen (from_tty);
1122
1123 /* Skip the first packet which about the trace status. The first
1124 event is "frame". */
1125 event = bt_ctf_iter_read_event (ctf_iter);
1126 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1127 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1128 if (event_id != CTF_EVENT_ID_FRAME)
1129 error (_("Wrong event id of the first event"));
1130 /* The second event is "status". */
1131 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1132 event = bt_ctf_iter_read_event (ctf_iter);
1133 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1134 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1135 if (event_id != CTF_EVENT_ID_STATUS)
1136 error (_("Wrong event id of the second event"));
1137 ctf_read_status (event, current_trace_status ());
1138
1139 ctf_read_tsv (&uploaded_tsvs);
1140
1141 ctf_read_tp (&uploaded_tps);
1142
1143 event = bt_ctf_iter_read_event (ctf_iter);
1144 /* EVENT can be NULL if we've already gone to the end of stream of
1145 events. */
1146 if (event != NULL)
1147 {
1148 scope = bt_ctf_get_top_level_scope (event,
1149 BT_STREAM_EVENT_HEADER);
1150 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event,
1151 scope, "id"));
1152 if (event_id != CTF_EVENT_ID_FRAME)
1153 error (_("Wrong event id of the first event of the second packet"));
1154 }
1155
1156 start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1157 gdb_assert (start_pos->type == BT_SEEK_RESTORE);
1158
1159 trace_dirname = xstrdup (dirname);
1160 push_target (&ctf_ops);
1161
1162 inferior_appeared (current_inferior (), CTF_PID);
1163 inferior_ptid = pid_to_ptid (CTF_PID);
1164 add_thread_silent (inferior_ptid);
1165
1166 merge_uploaded_trace_state_variables (&uploaded_tsvs);
1167 merge_uploaded_tracepoints (&uploaded_tps);
1168
1169 post_create_inferior (&ctf_ops, from_tty);
1170 }
1171
1172 /* This is the implementation of target_ops method to_close. Destroy
1173 CTF iterator and context. */
1174
1175 void
1176 ctf_target::close ()
1177 {
1178 int pid;
1179
1180 ctf_destroy ();
1181 xfree (trace_dirname);
1182 trace_dirname = NULL;
1183
1184 pid = ptid_get_pid (inferior_ptid);
1185 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
1186 exit_inferior_silent (pid);
1187
1188 trace_reset_local_state ();
1189 }
1190
1191 /* This is the implementation of target_ops method to_files_info.
1192 Print the directory name of CTF trace data. */
1193
1194 void
1195 ctf_target::files_info ()
1196 {
1197 printf_filtered ("\t`%s'\n", trace_dirname);
1198 }
1199
1200 /* This is the implementation of target_ops method to_fetch_registers.
1201 Iterate over events whose name is "register" in current frame,
1202 extract contents from events, and set REGCACHE with the contents.
1203 If no matched events are found, mark registers unavailable. */
1204
1205 void
1206 ctf_target::fetch_registers (struct regcache *regcache, int regno)
1207 {
1208 struct gdbarch *gdbarch = regcache->arch ();
1209 struct bt_ctf_event *event = NULL;
1210 struct bt_iter_pos *pos;
1211
1212 /* An uninitialized reg size says we're not going to be
1213 successful at getting register blocks. */
1214 if (trace_regblock_size == 0)
1215 return;
1216
1217 gdb_assert (ctf_iter != NULL);
1218 /* Save the current position. */
1219 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1220 gdb_assert (pos->type == BT_SEEK_RESTORE);
1221
1222 while (1)
1223 {
1224 const char *name;
1225 struct bt_ctf_event *event1;
1226
1227 event1 = bt_ctf_iter_read_event (ctf_iter);
1228
1229 name = bt_ctf_event_name (event1);
1230
1231 if (name == NULL || strcmp (name, "frame") == 0)
1232 break;
1233 else if (strcmp (name, "register") == 0)
1234 {
1235 event = event1;
1236 break;
1237 }
1238
1239 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1240 break;
1241 }
1242
1243 /* Restore the position. */
1244 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1245
1246 if (event != NULL)
1247 {
1248 int offset, regsize, regn;
1249 const struct bt_definition *scope
1250 = bt_ctf_get_top_level_scope (event,
1251 BT_EVENT_FIELDS);
1252 const struct bt_definition *array
1253 = bt_ctf_get_field (event, scope, "contents");
1254 gdb_byte *regs = (gdb_byte *) bt_ctf_get_char_array (array);
1255
1256 /* Assume the block is laid out in GDB register number order,
1257 each register with the size that it has in GDB. */
1258 offset = 0;
1259 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1260 {
1261 regsize = register_size (gdbarch, regn);
1262 /* Make sure we stay within block bounds. */
1263 if (offset + regsize >= trace_regblock_size)
1264 break;
1265 if (regcache->get_register_status (regn) == REG_UNKNOWN)
1266 {
1267 if (regno == regn)
1268 {
1269 regcache_raw_supply (regcache, regno, regs + offset);
1270 break;
1271 }
1272 else if (regno == -1)
1273 {
1274 regcache_raw_supply (regcache, regn, regs + offset);
1275 }
1276 }
1277 offset += regsize;
1278 }
1279 }
1280 else
1281 tracefile_fetch_registers (regcache, regno);
1282 }
1283
1284 /* This is the implementation of target_ops method to_xfer_partial.
1285 Iterate over events whose name is "memory" in
1286 current frame, extract the address and length from events. If
1287 OFFSET is within the range, read the contents from events to
1288 READBUF. */
1289
1290 enum target_xfer_status
1291 ctf_target::xfer_partial (enum target_object object,
1292 const char *annex, gdb_byte *readbuf,
1293 const gdb_byte *writebuf, ULONGEST offset,
1294 ULONGEST len, ULONGEST *xfered_len)
1295 {
1296 /* We're only doing regular memory for now. */
1297 if (object != TARGET_OBJECT_MEMORY)
1298 return TARGET_XFER_E_IO;
1299
1300 if (readbuf == NULL)
1301 error (_("ctf_xfer_partial: trace file is read-only"));
1302
1303 if (get_traceframe_number () != -1)
1304 {
1305 struct bt_iter_pos *pos;
1306 enum target_xfer_status res;
1307 /* Records the lowest available address of all blocks that
1308 intersects the requested range. */
1309 ULONGEST low_addr_available = 0;
1310
1311 gdb_assert (ctf_iter != NULL);
1312 /* Save the current position. */
1313 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1314 gdb_assert (pos->type == BT_SEEK_RESTORE);
1315
1316 /* Iterate through the traceframe's blocks, looking for
1317 memory. */
1318 while (1)
1319 {
1320 ULONGEST amt;
1321 uint64_t maddr;
1322 uint16_t mlen;
1323 const struct bt_definition *scope;
1324 const struct bt_definition *def;
1325 struct bt_ctf_event *event
1326 = bt_ctf_iter_read_event (ctf_iter);
1327 const char *name = bt_ctf_event_name (event);
1328
1329 if (name == NULL || strcmp (name, "frame") == 0)
1330 break;
1331 else if (strcmp (name, "memory") != 0)
1332 {
1333 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1334 break;
1335
1336 continue;
1337 }
1338
1339 scope = bt_ctf_get_top_level_scope (event,
1340 BT_EVENT_FIELDS);
1341
1342 def = bt_ctf_get_field (event, scope, "address");
1343 maddr = bt_ctf_get_uint64 (def);
1344 def = bt_ctf_get_field (event, scope, "length");
1345 mlen = (uint16_t) bt_ctf_get_uint64 (def);
1346
1347 /* If the block includes the first part of the desired
1348 range, return as much it has; GDB will re-request the
1349 remainder, which might be in a different block of this
1350 trace frame. */
1351 if (maddr <= offset && offset < (maddr + mlen))
1352 {
1353 const struct bt_definition *array
1354 = bt_ctf_get_field (event, scope, "contents");
1355 gdb_byte *contents;
1356 int k;
1357
1358 contents = (gdb_byte *) xmalloc (mlen);
1359
1360 for (k = 0; k < mlen; k++)
1361 {
1362 const struct bt_definition *element
1363 = bt_ctf_get_index (event, array, k);
1364
1365 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
1366 }
1367
1368 amt = (maddr + mlen) - offset;
1369 if (amt > len)
1370 amt = len;
1371
1372 memcpy (readbuf, &contents[offset - maddr], amt);
1373
1374 xfree (contents);
1375
1376 /* Restore the position. */
1377 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1378
1379 if (amt == 0)
1380 return TARGET_XFER_EOF;
1381 else
1382 {
1383 *xfered_len = amt;
1384 return TARGET_XFER_OK;
1385 }
1386 }
1387
1388 if (offset < maddr && maddr < (offset + len))
1389 if (low_addr_available == 0 || low_addr_available > maddr)
1390 low_addr_available = maddr;
1391
1392 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1393 break;
1394 }
1395
1396 /* Restore the position. */
1397 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1398
1399 /* Requested memory is unavailable in the context of traceframes,
1400 and this address falls within a read-only section, fallback
1401 to reading from executable, up to LOW_ADDR_AVAILABLE */
1402 if (offset < low_addr_available)
1403 len = std::min (len, low_addr_available - offset);
1404 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1405
1406 if (res == TARGET_XFER_OK)
1407 return TARGET_XFER_OK;
1408 else
1409 {
1410 /* No use trying further, we know some memory starting
1411 at MEMADDR isn't available. */
1412 *xfered_len = len;
1413 return TARGET_XFER_UNAVAILABLE;
1414 }
1415 }
1416 else
1417 {
1418 /* Fallback to reading from read-only sections. */
1419 return section_table_read_available_memory (readbuf, offset, len, xfered_len);
1420 }
1421 }
1422
1423 /* This is the implementation of target_ops method
1424 to_get_trace_state_variable_value.
1425 Iterate over events whose name is "tsv" in current frame. When the
1426 trace variable is found, set the value of it to *VAL and return
1427 true, otherwise return false. */
1428
1429 bool
1430 ctf_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
1431 {
1432 struct bt_iter_pos *pos;
1433 bool found = false;
1434
1435 gdb_assert (ctf_iter != NULL);
1436 /* Save the current position. */
1437 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1438 gdb_assert (pos->type == BT_SEEK_RESTORE);
1439
1440 /* Iterate through the traceframe's blocks, looking for 'V'
1441 block. */
1442 while (1)
1443 {
1444 struct bt_ctf_event *event
1445 = bt_ctf_iter_read_event (ctf_iter);
1446 const char *name = bt_ctf_event_name (event);
1447
1448 if (name == NULL || strcmp (name, "frame") == 0)
1449 break;
1450 else if (strcmp (name, "tsv") == 0)
1451 {
1452 const struct bt_definition *scope;
1453 const struct bt_definition *def;
1454
1455 scope = bt_ctf_get_top_level_scope (event,
1456 BT_EVENT_FIELDS);
1457
1458 def = bt_ctf_get_field (event, scope, "num");
1459 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
1460 {
1461 def = bt_ctf_get_field (event, scope, "val");
1462 *val = bt_ctf_get_uint64 (def);
1463
1464 found = true;
1465 }
1466 }
1467
1468 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1469 break;
1470 }
1471
1472 /* Restore the position. */
1473 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1474
1475 return found;
1476 }
1477
1478 /* Return the tracepoint number in "frame" event. */
1479
1480 static int
1481 ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
1482 {
1483 /* The packet context of events has a field "tpnum". */
1484 const struct bt_definition *scope
1485 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
1486 uint64_t tpnum
1487 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
1488
1489 return (int) tpnum;
1490 }
1491
1492 /* Return the address at which the current frame was collected. */
1493
1494 static CORE_ADDR
1495 ctf_get_traceframe_address (void)
1496 {
1497 struct bt_ctf_event *event = NULL;
1498 struct bt_iter_pos *pos;
1499 CORE_ADDR addr = 0;
1500
1501 gdb_assert (ctf_iter != NULL);
1502 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1503 gdb_assert (pos->type == BT_SEEK_RESTORE);
1504
1505 while (1)
1506 {
1507 const char *name;
1508 struct bt_ctf_event *event1;
1509
1510 event1 = bt_ctf_iter_read_event (ctf_iter);
1511
1512 name = bt_ctf_event_name (event1);
1513
1514 if (name == NULL)
1515 break;
1516 else if (strcmp (name, "frame") == 0)
1517 {
1518 event = event1;
1519 break;
1520 }
1521
1522 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1523 break;
1524 }
1525
1526 if (event != NULL)
1527 {
1528 int tpnum = ctf_get_tpnum_from_frame_event (event);
1529 struct tracepoint *tp
1530 = get_tracepoint_by_number_on_target (tpnum);
1531
1532 if (tp && tp->loc)
1533 addr = tp->loc->address;
1534 }
1535
1536 /* Restore the position. */
1537 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1538
1539 return addr;
1540 }
1541
1542 /* This is the implementation of target_ops method to_trace_find.
1543 Iterate the events whose name is "frame", extract the tracepoint
1544 number in it. Return traceframe number when matched. */
1545
1546 int
1547 ctf_target::trace_find (enum trace_find_type type, int num,
1548 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
1549 {
1550 int tfnum = 0;
1551 int found = 0;
1552
1553 if (num == -1)
1554 {
1555 if (tpp != NULL)
1556 *tpp = -1;
1557 return -1;
1558 }
1559
1560 gdb_assert (ctf_iter != NULL);
1561 /* Set iterator back to the start. */
1562 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
1563
1564 while (1)
1565 {
1566 struct bt_ctf_event *event;
1567 const char *name;
1568
1569 event = bt_ctf_iter_read_event (ctf_iter);
1570
1571 name = bt_ctf_event_name (event);
1572
1573 if (event == NULL || name == NULL)
1574 break;
1575
1576 if (strcmp (name, "frame") == 0)
1577 {
1578 CORE_ADDR tfaddr;
1579
1580 if (type == tfind_number)
1581 {
1582 /* Looking for a specific trace frame. */
1583 if (tfnum == num)
1584 found = 1;
1585 }
1586 else
1587 {
1588 /* Start from the _next_ trace frame. */
1589 if (tfnum > get_traceframe_number ())
1590 {
1591 switch (type)
1592 {
1593 case tfind_tp:
1594 {
1595 struct tracepoint *tp = get_tracepoint (num);
1596
1597 if (tp != NULL
1598 && (tp->number_on_target
1599 == ctf_get_tpnum_from_frame_event (event)))
1600 found = 1;
1601 break;
1602 }
1603 case tfind_pc:
1604 tfaddr = ctf_get_traceframe_address ();
1605 if (tfaddr == addr1)
1606 found = 1;
1607 break;
1608 case tfind_range:
1609 tfaddr = ctf_get_traceframe_address ();
1610 if (addr1 <= tfaddr && tfaddr <= addr2)
1611 found = 1;
1612 break;
1613 case tfind_outside:
1614 tfaddr = ctf_get_traceframe_address ();
1615 if (!(addr1 <= tfaddr && tfaddr <= addr2))
1616 found = 1;
1617 break;
1618 default:
1619 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
1620 }
1621 }
1622 }
1623 if (found)
1624 {
1625 if (tpp != NULL)
1626 *tpp = ctf_get_tpnum_from_frame_event (event);
1627
1628 /* Skip the event "frame". */
1629 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1630
1631 return tfnum;
1632 }
1633 tfnum++;
1634 }
1635
1636 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1637 break;
1638 }
1639
1640 return -1;
1641 }
1642
1643 /* This is the implementation of target_ops method to_traceframe_info.
1644 Iterate the events whose name is "memory", in current
1645 frame, extract memory range information, and return them in
1646 traceframe_info. */
1647
1648 traceframe_info_up
1649 ctf_target::traceframe_info ()
1650 {
1651 traceframe_info_up info (new struct traceframe_info);
1652 const char *name;
1653 struct bt_iter_pos *pos;
1654
1655 gdb_assert (ctf_iter != NULL);
1656 /* Save the current position. */
1657 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1658 gdb_assert (pos->type == BT_SEEK_RESTORE);
1659
1660 do
1661 {
1662 struct bt_ctf_event *event
1663 = bt_ctf_iter_read_event (ctf_iter);
1664
1665 name = bt_ctf_event_name (event);
1666
1667 if (name == NULL || strcmp (name, "register") == 0
1668 || strcmp (name, "frame") == 0)
1669 ;
1670 else if (strcmp (name, "memory") == 0)
1671 {
1672 const struct bt_definition *scope
1673 = bt_ctf_get_top_level_scope (event,
1674 BT_EVENT_FIELDS);
1675 const struct bt_definition *def;
1676
1677 def = bt_ctf_get_field (event, scope, "address");
1678 CORE_ADDR start = bt_ctf_get_uint64 (def);
1679
1680 def = bt_ctf_get_field (event, scope, "length");
1681 int length = (uint16_t) bt_ctf_get_uint64 (def);
1682
1683 info->memory.emplace_back (start, length);
1684 }
1685 else if (strcmp (name, "tsv") == 0)
1686 {
1687 int vnum;
1688 const struct bt_definition *scope
1689 = bt_ctf_get_top_level_scope (event,
1690 BT_EVENT_FIELDS);
1691 const struct bt_definition *def;
1692
1693 def = bt_ctf_get_field (event, scope, "num");
1694 vnum = (int) bt_ctf_get_uint64 (def);
1695 info->tvars.push_back (vnum);
1696 }
1697 else
1698 {
1699 warning (_("Unhandled trace block type (%s) "
1700 "while building trace frame info."),
1701 name);
1702 }
1703
1704 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1705 break;
1706 }
1707 while (name != NULL && strcmp (name, "frame") != 0);
1708
1709 /* Restore the position. */
1710 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1711
1712 return info;
1713 }
1714
1715 #endif
1716
1717 /* module initialization */
1718
1719 void
1720 _initialize_ctf (void)
1721 {
1722 #if HAVE_LIBBABELTRACE
1723 add_target (ctf_target_info, ctf_target_open, filename_completer);
1724 #endif
1725 }