]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ctf.c
Remove ptid_build
[thirdparty/binutils-gdb.git] / gdb / ctf.c
CommitLineData
d0353e76
YQ
1/* CTF format support.
2
e2882c85 3 Copyright (C) 2012-2018 Free Software Foundation, Inc.
d0353e76
YQ
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"
53ce3c39 26#include <sys/stat.h>
393fd4c3 27#include "exec.h"
da9160e4 28#include "completer.h"
de7b2893
YQ
29#include "inferior.h"
30#include "gdbthread.h"
7951c4eb 31#include "tracefile.h"
d0353e76 32#include <ctype.h>
325fac50 33#include <algorithm>
d0353e76 34
f6ac5f3d
PA
35/* The CTF target. */
36
d9f719f1
PA
37static const target_info ctf_target_info = {
38 "ctf",
39 N_("CTF file"),
40 N_("(Use a CTF directory as a target.\n\
41Specify the filename of the CTF directory.")
42};
43
f6ac5f3d
PA
44class ctf_target final : public tracefile_target
45{
46public:
d9f719f1
PA
47 const target_info &info () const override
48 { return ctf_target_info; }
f6ac5f3d 49
f6ac5f3d
PA
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;
57810aa7 61 bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
f6ac5f3d
PA
62 traceframe_info_up traceframe_info () override;
63};
64
d0353e76
YQ
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
393fd4c3
YQ
72 2. Trace status. Event "status" is defined in metadata, which
73 includes all aspects of trace status.
d0353e76 74
393fd4c3
YQ
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".
d0353e76
YQ
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
393fd4c3
YQ
108#define CTF_EVENT_ID_STATUS 4
109#define CTF_EVENT_ID_TSV_DEF 5
110#define CTF_EVENT_ID_TP_DEF 6
d0353e76 111
de7b2893
YQ
112#define CTF_PID (2)
113
d0353e76
YQ
114/* The state kept while writing the CTF datastream file. */
115
116struct 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
77b64a49
PA
132static void
133ctf_save_write_metadata (struct trace_write_handler *handler,
134 const char *format, ...)
135 ATTRIBUTE_PRINTF (2, 3);
136
d0353e76
YQ
137static void
138ctf_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
153static int
154ctf_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
393fd4c3
YQ
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
d0353e76
YQ
178/* Set datastream file position. Update HANDLER->content_size
179 if WHENCE is SEEK_CUR. */
180
181static int
182ctf_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
202static int
203ctf_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
222static void
223ctf_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
232static void
233ctf_save_metadata_header (struct trace_write_handler *handler)
234{
d0353e76
YQ
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");
393fd4c3
YQ
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");
d0353e76
YQ
264 ctf_save_write_metadata (handler, "\n");
265
8249a5a9
YQ
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
7f31862a
PA
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",
d0353e76 294 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
8249a5a9 295 HOST_ENDIANNESS);
d0353e76
YQ
296 ctf_save_write_metadata (handler, "\n");
297}
298
299/* CTF trace writer. */
300
301struct 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
312static void
313ctf_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
329static int
330ctf_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
341static void
342ctf_start (struct trace_file_writer *self, const char *dirname)
343{
d0353e76
YQ
344 struct ctf_trace_file_writer *writer
345 = (struct ctf_trace_file_writer *) self;
840207d8 346 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH;
d0353e76
YQ
347
348 /* Create DIRNAME. */
af307d6a 349 if (mkdir (dirname, hmode) && errno != EEXIST)
d0353e76
YQ
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
50feb4bd 355 std::string file_name = string_printf ("%s/%s", dirname, CTF_METADATA_NAME);
d0353e76 356
50feb4bd 357 writer->tcs.metadata_fd = fopen (file_name.c_str (), "w");
d0353e76
YQ
358 if (writer->tcs.metadata_fd == NULL)
359 error (_("Unable to open file '%s' for saving trace data (%s)"),
50feb4bd 360 file_name.c_str (), safe_strerror (errno));
d0353e76
YQ
361
362 ctf_save_metadata_header (&writer->tcs);
363
50feb4bd
TT
364 file_name = string_printf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
365 writer->tcs.datastream_fd = fopen (file_name.c_str (), "w");
d0353e76
YQ
366 if (writer->tcs.datastream_fd == NULL)
367 error (_("Unable to open file '%s' for saving trace data (%s)"),
50feb4bd 368 file_name.c_str (), safe_strerror (errno));
d0353e76
YQ
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
375static void
376ctf_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
393fd4c3
YQ
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
d0353e76
YQ
447 gdb_assert (writer->tcs.content_size == 0);
448 gdb_assert (writer->tcs.packet_start == 0);
393fd4c3
YQ
449
450 /* Create a new packet to contain this event. */
451 self->ops->frame_ops->start (self, 0);
d0353e76
YQ
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
458static void
459ctf_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
478static void
479ctf_write_status (struct trace_file_writer *self,
480 struct trace_status *ts)
481{
393fd4c3
YQ
482 struct ctf_trace_file_writer *writer
483 = (struct ctf_trace_file_writer *) self;
484 uint32_t id;
393fd4c3
YQ
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);
d0353e76
YQ
514}
515
516/* This is the implementation of trace_file_write_ops method
517 write_uploaded_tsv. */
518
519static void
520ctf_write_uploaded_tsv (struct trace_file_writer *self,
521 struct uploaded_tsv *tsv)
522{
393fd4c3
YQ
523 struct ctf_trace_file_writer *writer
524 = (struct ctf_trace_file_writer *) self;
525 int32_t int32;
526 int64_t int64;
393fd4c3
YQ
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)
a398505b
PA
545 ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name,
546 strlen (tsv->name));
393fd4c3 547 ctf_save_write (&writer->tcs, &zero, 1);
d0353e76
YQ
548}
549
550/* This is the implementation of trace_file_write_ops method
551 write_uploaded_tp. */
552
553static void
554ctf_write_uploaded_tp (struct trace_file_writer *self,
555 struct uploaded_tp *tp)
556{
393fd4c3
YQ
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;
393fd4c3
YQ
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)
a398505b 596 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond, strlen (tp->cond));
393fd4c3
YQ
597 ctf_save_write (&writer->tcs, &zero, 1);
598
599 /* actions */
a18ba4e4 600 u32 = tp->actions.size ();
393fd4c3 601 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
a18ba4e4 602 for (char *act : tp->actions)
a398505b 603 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
393fd4c3
YQ
604
605 /* step_actions */
a18ba4e4 606 u32 = tp->step_actions.size ();
393fd4c3 607 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
a18ba4e4 608 for (char *act : tp->step_actions)
a398505b 609 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
393fd4c3
YQ
610
611 /* at_string */
612 if (tp->at_string != NULL)
a398505b 613 ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string,
393fd4c3
YQ
614 strlen (tp->at_string));
615 ctf_save_write (&writer->tcs, &zero, 1);
616
617 /* cond_string */
618 if (tp->cond_string != NULL)
a398505b 619 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string,
393fd4c3
YQ
620 strlen (tp->cond_string));
621 ctf_save_write (&writer->tcs, &zero, 1);
622
623 /* cmd_strings */
a18ba4e4 624 u32 = tp->cmd_strings.size ();
393fd4c3 625 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
a18ba4e4 626 for (char *act : tp->cmd_strings)
a398505b 627 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
393fd4c3 628
d0353e76
YQ
629}
630
18d3cec5
MK
631/* This is the implementation of trace_file_write_ops method
632 write_tdesc. */
633
634static void
635ctf_write_tdesc (struct trace_file_writer *self)
636{
637 /* Nothing so far. */
638}
639
d0353e76
YQ
640/* This is the implementation of trace_file_write_ops method
641 write_definition_end. */
642
643static void
644ctf_write_definition_end (struct trace_file_writer *self)
645{
393fd4c3 646 self->ops->frame_ops->end (self);
d0353e76
YQ
647}
648
d0353e76
YQ
649/* This is the implementation of trace_file_write_ops method
650 end. */
651
652static void
653ctf_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);
d0353e76
YQ
658}
659
660/* This is the implementation of trace_frame_write_ops method
661 start. */
662
663static void
664ctf_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
690static void
691ctf_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
708static void
709ctf_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
729static void
730ctf_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
743static void
744ctf_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
763static void
764ctf_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
798static 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
810static 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,
18d3cec5 820 ctf_write_tdesc,
d0353e76
YQ
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
829struct trace_file_writer *
830ctf_trace_file_writer_new (void)
831{
8d749320 832 struct ctf_trace_file_writer *writer = XNEW (struct ctf_trace_file_writer);
d0353e76
YQ
833
834 writer->base.ops = &ctf_write_ops;
835
836 return (struct trace_file_writer *) writer;
837}
393fd4c3
YQ
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. */
614d5099 850static int handle_id = -1;
393fd4c3
YQ
851static struct bt_context *ctx = NULL;
852static struct bt_ctf_iter *ctf_iter = NULL;
853/* The position of the first packet containing trace frame. */
854static struct bt_iter_pos *start_pos;
855
856/* The name of CTF directory. */
857static char *trace_dirname;
858
f6ac5f3d 859static ctf_target ctf_ops;
393fd4c3
YQ
860
861/* Destroy ctf iterator and context. */
862
863static void
864ctf_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
880static void
014f9477 881ctf_open_dir (const char *dirname)
393fd4c3 882{
393fd4c3 883 struct bt_iter_pos begin_pos;
614d5099
YQ
884 unsigned int count, i;
885 struct bt_ctf_event_decl * const *list;
393fd4c3
YQ
886
887 ctx = bt_context_create ();
888 if (ctx == NULL)
889 error (_("Unable to create bt_context"));
614d5099
YQ
890 handle_id = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
891 if (handle_id < 0)
393fd4c3
YQ
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
614d5099
YQ
906 /* Look for the declaration of register block. Get the length of
907 array "contents" to set trace_regblock_size. */
393fd4c3 908
614d5099
YQ
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 {
614d5099
YQ
913 const struct bt_ctf_field_decl * const *field_list;
914 const struct bt_declaration *decl;
393fd4c3 915
614d5099
YQ
916 bt_ctf_get_decl_fields (list[i], BT_EVENT_FIELDS, &field_list,
917 &count);
393fd4c3 918
614d5099
YQ
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);
393fd4c3 924
393fd4c3 925 break;
614d5099 926 }
393fd4c3
YQ
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
e0d13cbd
SM
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
393fd4c3
YQ
940/* EVENT is the "status" event and TS is filled in. */
941
942static void
943ctf_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
e0d13cbd 948 SET_ENUM_FIELD (event, scope, ts, enum trace_stop_reason, stop_reason);
393fd4c3
YQ
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
963static void
964ctf_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 \
a18ba4e4
SM
1023 (VAR)->ARRAY.push_back \
1024 (xstrdup (bt_ctf_get_string (element))); \
393fd4c3
YQ
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
1049static void
1050ctf_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);
e0d13cbd 1084 SET_ENUM_FIELD (event, scope, utp, enum bptype, type);
393fd4c3
YQ
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
d9f719f1
PA
1107static void
1108ctf_target_open (const char *dirname, int from_tty)
393fd4c3
YQ
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
de7b2893
YQ
1162 inferior_appeared (current_inferior (), CTF_PID);
1163 inferior_ptid = pid_to_ptid (CTF_PID);
1164 add_thread_silent (inferior_ptid);
1165
393fd4c3
YQ
1166 merge_uploaded_trace_state_variables (&uploaded_tsvs);
1167 merge_uploaded_tracepoints (&uploaded_tps);
5723a6fd
YQ
1168
1169 post_create_inferior (&ctf_ops, from_tty);
393fd4c3
YQ
1170}
1171
1172/* This is the implementation of target_ops method to_close. Destroy
1173 CTF iterator and context. */
1174
f6ac5f3d
PA
1175void
1176ctf_target::close ()
393fd4c3
YQ
1177{
1178 ctf_destroy ();
1179 xfree (trace_dirname);
1180 trace_dirname = NULL;
aef525cb 1181
de7b2893 1182 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
00431a78 1183 exit_inferior_silent (current_inferior ());
de7b2893 1184
aef525cb 1185 trace_reset_local_state ();
393fd4c3
YQ
1186}
1187
1188/* This is the implementation of target_ops method to_files_info.
1189 Print the directory name of CTF trace data. */
1190
f6ac5f3d
PA
1191void
1192ctf_target::files_info ()
393fd4c3
YQ
1193{
1194 printf_filtered ("\t`%s'\n", trace_dirname);
1195}
1196
1197/* This is the implementation of target_ops method to_fetch_registers.
1198 Iterate over events whose name is "register" in current frame,
1199 extract contents from events, and set REGCACHE with the contents.
1200 If no matched events are found, mark registers unavailable. */
1201
f6ac5f3d
PA
1202void
1203ctf_target::fetch_registers (struct regcache *regcache, int regno)
393fd4c3 1204{
ac7936df 1205 struct gdbarch *gdbarch = regcache->arch ();
393fd4c3
YQ
1206 struct bt_ctf_event *event = NULL;
1207 struct bt_iter_pos *pos;
1208
1209 /* An uninitialized reg size says we're not going to be
1210 successful at getting register blocks. */
1211 if (trace_regblock_size == 0)
1212 return;
1213
1214 gdb_assert (ctf_iter != NULL);
1215 /* Save the current position. */
1216 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1217 gdb_assert (pos->type == BT_SEEK_RESTORE);
1218
1219 while (1)
1220 {
1221 const char *name;
1222 struct bt_ctf_event *event1;
1223
1224 event1 = bt_ctf_iter_read_event (ctf_iter);
1225
1226 name = bt_ctf_event_name (event1);
1227
1228 if (name == NULL || strcmp (name, "frame") == 0)
1229 break;
1230 else if (strcmp (name, "register") == 0)
1231 {
1232 event = event1;
1233 break;
1234 }
1235
1236 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1237 break;
1238 }
1239
1240 /* Restore the position. */
1241 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1242
1243 if (event != NULL)
1244 {
48b6e87e 1245 int offset, regsize, regn;
393fd4c3
YQ
1246 const struct bt_definition *scope
1247 = bt_ctf_get_top_level_scope (event,
1248 BT_EVENT_FIELDS);
1249 const struct bt_definition *array
1250 = bt_ctf_get_field (event, scope, "contents");
48b6e87e 1251 gdb_byte *regs = (gdb_byte *) bt_ctf_get_char_array (array);
393fd4c3 1252
393fd4c3
YQ
1253 /* Assume the block is laid out in GDB register number order,
1254 each register with the size that it has in GDB. */
1255 offset = 0;
1256 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1257 {
1258 regsize = register_size (gdbarch, regn);
1259 /* Make sure we stay within block bounds. */
1260 if (offset + regsize >= trace_regblock_size)
1261 break;
0ec9f114 1262 if (regcache->get_register_status (regn) == REG_UNKNOWN)
393fd4c3
YQ
1263 {
1264 if (regno == regn)
1265 {
73e1c03f 1266 regcache->raw_supply (regno, regs + offset);
393fd4c3
YQ
1267 break;
1268 }
1269 else if (regno == -1)
1270 {
73e1c03f 1271 regcache->raw_supply (regn, regs + offset);
393fd4c3
YQ
1272 }
1273 }
1274 offset += regsize;
1275 }
393fd4c3 1276 }
48b6e87e
YQ
1277 else
1278 tracefile_fetch_registers (regcache, regno);
393fd4c3
YQ
1279}
1280
1281/* This is the implementation of target_ops method to_xfer_partial.
1282 Iterate over events whose name is "memory" in
1283 current frame, extract the address and length from events. If
1284 OFFSET is within the range, read the contents from events to
1285 READBUF. */
1286
f6ac5f3d
PA
1287enum target_xfer_status
1288ctf_target::xfer_partial (enum target_object object,
1289 const char *annex, gdb_byte *readbuf,
1290 const gdb_byte *writebuf, ULONGEST offset,
1291 ULONGEST len, ULONGEST *xfered_len)
393fd4c3
YQ
1292{
1293 /* We're only doing regular memory for now. */
1294 if (object != TARGET_OBJECT_MEMORY)
c9244484 1295 return TARGET_XFER_E_IO;
393fd4c3
YQ
1296
1297 if (readbuf == NULL)
1298 error (_("ctf_xfer_partial: trace file is read-only"));
1299
1300 if (get_traceframe_number () != -1)
1301 {
1302 struct bt_iter_pos *pos;
8acf9577 1303 enum target_xfer_status res;
290a839c
YQ
1304 /* Records the lowest available address of all blocks that
1305 intersects the requested range. */
1306 ULONGEST low_addr_available = 0;
393fd4c3
YQ
1307
1308 gdb_assert (ctf_iter != NULL);
1309 /* Save the current position. */
1310 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1311 gdb_assert (pos->type == BT_SEEK_RESTORE);
1312
1313 /* Iterate through the traceframe's blocks, looking for
1314 memory. */
1315 while (1)
1316 {
1317 ULONGEST amt;
1318 uint64_t maddr;
1319 uint16_t mlen;
393fd4c3
YQ
1320 const struct bt_definition *scope;
1321 const struct bt_definition *def;
1322 struct bt_ctf_event *event
1323 = bt_ctf_iter_read_event (ctf_iter);
1324 const char *name = bt_ctf_event_name (event);
1325
dac3e710 1326 if (name == NULL || strcmp (name, "frame") == 0)
393fd4c3
YQ
1327 break;
1328 else if (strcmp (name, "memory") != 0)
1329 {
1330 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1331 break;
1332
1333 continue;
1334 }
1335
1336 scope = bt_ctf_get_top_level_scope (event,
1337 BT_EVENT_FIELDS);
1338
1339 def = bt_ctf_get_field (event, scope, "address");
1340 maddr = bt_ctf_get_uint64 (def);
1341 def = bt_ctf_get_field (event, scope, "length");
1342 mlen = (uint16_t) bt_ctf_get_uint64 (def);
1343
1344 /* If the block includes the first part of the desired
1345 range, return as much it has; GDB will re-request the
1346 remainder, which might be in a different block of this
1347 trace frame. */
1348 if (maddr <= offset && offset < (maddr + mlen))
1349 {
1350 const struct bt_definition *array
1351 = bt_ctf_get_field (event, scope, "contents");
393fd4c3
YQ
1352 gdb_byte *contents;
1353 int k;
1354
224c3ddb 1355 contents = (gdb_byte *) xmalloc (mlen);
393fd4c3
YQ
1356
1357 for (k = 0; k < mlen; k++)
1358 {
1359 const struct bt_definition *element
1360 = bt_ctf_get_index (event, array, k);
1361
1362 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
1363 }
1364
1365 amt = (maddr + mlen) - offset;
1366 if (amt > len)
1367 amt = len;
1368
1369 memcpy (readbuf, &contents[offset - maddr], amt);
1370
1371 xfree (contents);
1372
1373 /* Restore the position. */
1374 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1375
9b409511
YQ
1376 if (amt == 0)
1377 return TARGET_XFER_EOF;
1378 else
1379 {
1380 *xfered_len = amt;
1381 return TARGET_XFER_OK;
1382 }
393fd4c3
YQ
1383 }
1384
290a839c
YQ
1385 if (offset < maddr && maddr < (offset + len))
1386 if (low_addr_available == 0 || low_addr_available > maddr)
1387 low_addr_available = maddr;
1388
393fd4c3
YQ
1389 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1390 break;
1391 }
1392
1393 /* Restore the position. */
1394 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
393fd4c3 1395
8acf9577
YQ
1396 /* Requested memory is unavailable in the context of traceframes,
1397 and this address falls within a read-only section, fallback
290a839c
YQ
1398 to reading from executable, up to LOW_ADDR_AVAILABLE */
1399 if (offset < low_addr_available)
325fac50 1400 len = std::min (len, low_addr_available - offset);
8acf9577
YQ
1401 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1402
1403 if (res == TARGET_XFER_OK)
1404 return TARGET_XFER_OK;
1405 else
1406 {
1407 /* No use trying further, we know some memory starting
1408 at MEMADDR isn't available. */
1409 *xfered_len = len;
1410 return TARGET_XFER_UNAVAILABLE;
1411 }
1ee79381
YQ
1412 }
1413 else
1414 {
1415 /* Fallback to reading from read-only sections. */
1416 return section_table_read_available_memory (readbuf, offset, len, xfered_len);
1417 }
393fd4c3
YQ
1418}
1419
1420/* This is the implementation of target_ops method
1421 to_get_trace_state_variable_value.
1422 Iterate over events whose name is "tsv" in current frame. When the
1423 trace variable is found, set the value of it to *VAL and return
1424 true, otherwise return false. */
1425
57810aa7 1426bool
f6ac5f3d 1427ctf_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
393fd4c3
YQ
1428{
1429 struct bt_iter_pos *pos;
57810aa7 1430 bool found = false;
393fd4c3
YQ
1431
1432 gdb_assert (ctf_iter != NULL);
1433 /* Save the current position. */
1434 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1435 gdb_assert (pos->type == BT_SEEK_RESTORE);
1436
1437 /* Iterate through the traceframe's blocks, looking for 'V'
1438 block. */
1439 while (1)
1440 {
1441 struct bt_ctf_event *event
1442 = bt_ctf_iter_read_event (ctf_iter);
1443 const char *name = bt_ctf_event_name (event);
1444
1445 if (name == NULL || strcmp (name, "frame") == 0)
1446 break;
1447 else if (strcmp (name, "tsv") == 0)
1448 {
1449 const struct bt_definition *scope;
1450 const struct bt_definition *def;
1451
1452 scope = bt_ctf_get_top_level_scope (event,
1453 BT_EVENT_FIELDS);
1454
1455 def = bt_ctf_get_field (event, scope, "num");
1456 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
1457 {
1458 def = bt_ctf_get_field (event, scope, "val");
1459 *val = bt_ctf_get_uint64 (def);
1460
57810aa7 1461 found = true;
393fd4c3
YQ
1462 }
1463 }
1464
1465 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1466 break;
1467 }
1468
1469 /* Restore the position. */
1470 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1471
1472 return found;
1473}
1474
1475/* Return the tracepoint number in "frame" event. */
1476
1477static int
1478ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
1479{
1480 /* The packet context of events has a field "tpnum". */
1481 const struct bt_definition *scope
1482 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
1483 uint64_t tpnum
1484 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
1485
1486 return (int) tpnum;
1487}
1488
1489/* Return the address at which the current frame was collected. */
1490
1491static CORE_ADDR
1492ctf_get_traceframe_address (void)
1493{
1494 struct bt_ctf_event *event = NULL;
1495 struct bt_iter_pos *pos;
1496 CORE_ADDR addr = 0;
1497
1498 gdb_assert (ctf_iter != NULL);
1499 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1500 gdb_assert (pos->type == BT_SEEK_RESTORE);
1501
1502 while (1)
1503 {
1504 const char *name;
1505 struct bt_ctf_event *event1;
1506
1507 event1 = bt_ctf_iter_read_event (ctf_iter);
1508
1509 name = bt_ctf_event_name (event1);
1510
1511 if (name == NULL)
1512 break;
1513 else if (strcmp (name, "frame") == 0)
1514 {
1515 event = event1;
1516 break;
1517 }
1518
1519 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1520 break;
1521 }
1522
1523 if (event != NULL)
1524 {
1525 int tpnum = ctf_get_tpnum_from_frame_event (event);
1526 struct tracepoint *tp
1527 = get_tracepoint_by_number_on_target (tpnum);
1528
c1fc2657
SM
1529 if (tp && tp->loc)
1530 addr = tp->loc->address;
393fd4c3
YQ
1531 }
1532
1533 /* Restore the position. */
1534 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1535
1536 return addr;
1537}
1538
1539/* This is the implementation of target_ops method to_trace_find.
1540 Iterate the events whose name is "frame", extract the tracepoint
1541 number in it. Return traceframe number when matched. */
1542
f6ac5f3d
PA
1543int
1544ctf_target::trace_find (enum trace_find_type type, int num,
1545 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
393fd4c3 1546{
393fd4c3
YQ
1547 int tfnum = 0;
1548 int found = 0;
393fd4c3
YQ
1549
1550 if (num == -1)
1551 {
1552 if (tpp != NULL)
1553 *tpp = -1;
1554 return -1;
1555 }
1556
1557 gdb_assert (ctf_iter != NULL);
1558 /* Set iterator back to the start. */
1559 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
1560
1561 while (1)
1562 {
393fd4c3
YQ
1563 struct bt_ctf_event *event;
1564 const char *name;
1565
1566 event = bt_ctf_iter_read_event (ctf_iter);
1567
1568 name = bt_ctf_event_name (event);
1569
1570 if (event == NULL || name == NULL)
1571 break;
1572
1573 if (strcmp (name, "frame") == 0)
1574 {
1575 CORE_ADDR tfaddr;
1576
1577 if (type == tfind_number)
1578 {
1579 /* Looking for a specific trace frame. */
1580 if (tfnum == num)
1581 found = 1;
1582 }
1583 else
1584 {
1585 /* Start from the _next_ trace frame. */
1586 if (tfnum > get_traceframe_number ())
1587 {
1588 switch (type)
1589 {
1590 case tfind_tp:
1591 {
1592 struct tracepoint *tp = get_tracepoint (num);
1593
1594 if (tp != NULL
1595 && (tp->number_on_target
1596 == ctf_get_tpnum_from_frame_event (event)))
1597 found = 1;
1598 break;
1599 }
1600 case tfind_pc:
1601 tfaddr = ctf_get_traceframe_address ();
1602 if (tfaddr == addr1)
1603 found = 1;
1604 break;
1605 case tfind_range:
1606 tfaddr = ctf_get_traceframe_address ();
1607 if (addr1 <= tfaddr && tfaddr <= addr2)
1608 found = 1;
1609 break;
1610 case tfind_outside:
1611 tfaddr = ctf_get_traceframe_address ();
1612 if (!(addr1 <= tfaddr && tfaddr <= addr2))
1613 found = 1;
1614 break;
1615 default:
1616 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
1617 }
1618 }
1619 }
1620 if (found)
1621 {
1622 if (tpp != NULL)
1623 *tpp = ctf_get_tpnum_from_frame_event (event);
1624
1625 /* Skip the event "frame". */
1626 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1627
1628 return tfnum;
1629 }
1630 tfnum++;
1631 }
1632
1633 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1634 break;
1635 }
1636
1637 return -1;
1638}
1639
393fd4c3
YQ
1640/* This is the implementation of target_ops method to_traceframe_info.
1641 Iterate the events whose name is "memory", in current
1642 frame, extract memory range information, and return them in
1643 traceframe_info. */
1644
f6ac5f3d
PA
1645traceframe_info_up
1646ctf_target::traceframe_info ()
393fd4c3 1647{
f6ac5f3d 1648 traceframe_info_up info (new struct traceframe_info);
393fd4c3
YQ
1649 const char *name;
1650 struct bt_iter_pos *pos;
1651
1652 gdb_assert (ctf_iter != NULL);
1653 /* Save the current position. */
1654 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1655 gdb_assert (pos->type == BT_SEEK_RESTORE);
1656
1657 do
1658 {
1659 struct bt_ctf_event *event
1660 = bt_ctf_iter_read_event (ctf_iter);
1661
1662 name = bt_ctf_event_name (event);
1663
1664 if (name == NULL || strcmp (name, "register") == 0
1665 || strcmp (name, "frame") == 0)
1666 ;
1667 else if (strcmp (name, "memory") == 0)
1668 {
1669 const struct bt_definition *scope
1670 = bt_ctf_get_top_level_scope (event,
1671 BT_EVENT_FIELDS);
1672 const struct bt_definition *def;
393fd4c3 1673
393fd4c3 1674 def = bt_ctf_get_field (event, scope, "address");
4cdd21a8 1675 CORE_ADDR start = bt_ctf_get_uint64 (def);
393fd4c3
YQ
1676
1677 def = bt_ctf_get_field (event, scope, "length");
4cdd21a8
SM
1678 int length = (uint16_t) bt_ctf_get_uint64 (def);
1679
1680 info->memory.emplace_back (start, length);
393fd4c3 1681 }
28a93511
YQ
1682 else if (strcmp (name, "tsv") == 0)
1683 {
1684 int vnum;
1685 const struct bt_definition *scope
1686 = bt_ctf_get_top_level_scope (event,
1687 BT_EVENT_FIELDS);
1688 const struct bt_definition *def;
1689
1690 def = bt_ctf_get_field (event, scope, "num");
eed2386e 1691 vnum = (int) bt_ctf_get_uint64 (def);
d0d292a2 1692 info->tvars.push_back (vnum);
28a93511 1693 }
393fd4c3
YQ
1694 else
1695 {
1696 warning (_("Unhandled trace block type (%s) "
1697 "while building trace frame info."),
1698 name);
1699 }
1700
1701 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1702 break;
1703 }
1704 while (name != NULL && strcmp (name, "frame") != 0);
1705
1706 /* Restore the position. */
1707 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1708
1709 return info;
1710}
1711
393fd4c3
YQ
1712#endif
1713
393fd4c3
YQ
1714/* module initialization */
1715
1716void
1717_initialize_ctf (void)
1718{
1719#if HAVE_LIBBABELTRACE
d9f719f1 1720 add_target (ctf_target_info, ctf_target_open, filename_completer);
393fd4c3
YQ
1721#endif
1722}