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