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