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