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