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