*** empty log message ***
[external/binutils.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
28 #include <ctype.h>
29
30 /* GDB saves trace buffers and other information (such as trace
31    status) got from the remote target into Common Trace Format (CTF).
32    The following types of information are expected to save in CTF:
33
34    1. The length (in bytes) of register cache.  Event "register" will
35    be defined in metadata, which includes the length.
36
37    2. Trace status.  Not implemented yet in CTF writer.
38
39    3. Uploaded trace variables and tracepoints.  Not implemented yet
40    in CTF writer.
41
42    4. Trace frames.  Each trace frame is composed by several blocks
43    of different types ('R', 'M', 'V').  One trace frame is saved in
44    one CTF packet and the blocks of this frame are saved as events.
45    4.1: The trace frame related information (such as the number of
46    tracepoint associated with this frame) is saved in the packet
47    context.
48    4.2: The block 'M', 'R' and 'V' are saved in event "memory",
49    "register" and "tsv" respectively.
50    4.3: When iterating over events, babeltrace can't tell iterator
51    goes to a new packet, so we need a marker or anchor to tell GDB
52    that iterator goes into a new packet or frame.  We define event
53    "frame".  */
54
55 #define CTF_MAGIC               0xC1FC1FC1
56 #define CTF_SAVE_MAJOR          1
57 #define CTF_SAVE_MINOR          8
58
59 #define CTF_METADATA_NAME       "metadata"
60 #define CTF_DATASTREAM_NAME     "datastream"
61
62 /* Reserved event id.  */
63
64 #define CTF_EVENT_ID_REGISTER 0
65 #define CTF_EVENT_ID_TSV 1
66 #define CTF_EVENT_ID_MEMORY 2
67 #define CTF_EVENT_ID_FRAME 3
68
69 /* The state kept while writing the CTF datastream file.  */
70
71 struct trace_write_handler
72 {
73   /* File descriptor of metadata.  */
74   FILE *metadata_fd;
75   /* File descriptor of traceframes.  */
76   FILE *datastream_fd;
77
78   /* This is the content size of the current packet.  */
79   size_t content_size;
80
81   /* This is the start offset of current packet.  */
82   long packet_start;
83 };
84
85 /* Write metadata in FORMAT.  */
86
87 static void
88 ctf_save_write_metadata (struct trace_write_handler *handler,
89                          const char *format, ...)
90 {
91   va_list args;
92
93   va_start (args, format);
94   if (vfprintf (handler->metadata_fd, format, args) < 0)
95     error (_("Unable to write metadata file (%s)"),
96              safe_strerror (errno));
97   va_end (args);
98 }
99
100 /* Write BUF of length SIZE to datastream file represented by
101    HANDLER.  */
102
103 static int
104 ctf_save_write (struct trace_write_handler *handler,
105                 const gdb_byte *buf, size_t size)
106 {
107   if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
108     error (_("Unable to write file for saving trace data (%s)"),
109            safe_strerror (errno));
110
111   handler->content_size += size;
112
113   return 0;
114 }
115
116 /* Write a unsigned 32-bit integer to datastream file represented by
117    HANDLER.  */
118
119 #define ctf_save_write_uint32(HANDLER, U32) \
120   ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
121
122 /* Set datastream file position.  Update HANDLER->content_size
123    if WHENCE is SEEK_CUR.  */
124
125 static int
126 ctf_save_fseek (struct trace_write_handler *handler, long offset,
127                 int whence)
128 {
129   gdb_assert (whence != SEEK_END);
130   gdb_assert (whence != SEEK_SET
131               || offset <= handler->content_size + handler->packet_start);
132
133   if (fseek (handler->datastream_fd, offset, whence))
134     error (_("Unable to seek file for saving trace data (%s)"),
135            safe_strerror (errno));
136
137   if (whence == SEEK_CUR)
138     handler->content_size += offset;
139
140   return 0;
141 }
142
143 /* Change the datastream file position to align on ALIGN_SIZE,
144    and write BUF to datastream file.  The size of BUF is SIZE.  */
145
146 static int
147 ctf_save_align_write (struct trace_write_handler *handler,
148                       const gdb_byte *buf,
149                       size_t size, size_t align_size)
150 {
151   long offset
152     = (align_up (handler->content_size, align_size)
153        - handler->content_size);
154
155   if (ctf_save_fseek (handler, offset, SEEK_CUR))
156     return -1;
157
158   if (ctf_save_write (handler, buf, size))
159     return -1;
160
161   return 0;
162 }
163
164 /* Write events to next new packet.  */
165
166 static void
167 ctf_save_next_packet (struct trace_write_handler *handler)
168 {
169   handler->packet_start += (handler->content_size + 4);
170   ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
171   handler->content_size = 0;
172 }
173
174 /* Write the CTF metadata header.  */
175
176 static void
177 ctf_save_metadata_header (struct trace_write_handler *handler)
178 {
179   const char metadata_fmt[] =
180   "\ntrace {\n"
181   "     major = %u;\n"
182   "     minor = %u;\n"
183   "     byte_order = %s;\n"             /* be or le */
184   "     packet.header := struct {\n"
185   "             uint32_t magic;\n"
186   "     };\n"
187   "};\n"
188   "\n"
189   "stream {\n"
190   "     packet.context := struct {\n"
191   "             uint32_t content_size;\n"
192   "             uint32_t packet_size;\n"
193   "             uint16_t tpnum;\n"
194   "     };\n"
195   "     event.header := struct {\n"
196   "             uint32_t id;\n"
197   "     };\n"
198   "};\n";
199
200   ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
201                            CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
202   ctf_save_write_metadata (handler,
203                            "typealias integer { size = 8; align = 8; "
204                            "signed = false; encoding = ascii;}"
205                            " := ascii;\n");
206   ctf_save_write_metadata (handler,
207                            "typealias integer { size = 8; align = 8; "
208                            "signed = false; }"
209                            " := uint8_t;\n");
210   ctf_save_write_metadata (handler,
211                            "typealias integer { size = 16; align = 16;"
212                            "signed = false; } := uint16_t;\n");
213   ctf_save_write_metadata (handler,
214                            "typealias integer { size = 32; align = 32;"
215                            "signed = false; } := uint32_t;\n");
216   ctf_save_write_metadata (handler,
217                            "typealias integer { size = 64; align = 64;"
218                            "signed = false; base = hex;}"
219                            " := uint64_t;\n");
220   ctf_save_write_metadata (handler, "\n");
221
222   /* Get the byte order of the host and write CTF data in this byte
223      order.  */
224 #if WORDS_BIGENDIAN
225 #define HOST_ENDIANNESS "be"
226 #else
227 #define HOST_ENDIANNESS "le"
228 #endif
229
230   ctf_save_write_metadata (handler, metadata_fmt,
231                            CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
232                            HOST_ENDIANNESS);
233   ctf_save_write_metadata (handler, "\n");
234 }
235
236 /* CTF trace writer.  */
237
238 struct ctf_trace_file_writer
239 {
240   struct trace_file_writer base;
241
242   /* States related to writing CTF trace file.  */
243   struct trace_write_handler tcs;
244 };
245
246 /* This is the implementation of trace_file_write_ops method
247    dtor.  */
248
249 static void
250 ctf_dtor (struct trace_file_writer *self)
251 {
252   struct ctf_trace_file_writer *writer
253     = (struct ctf_trace_file_writer *) self;
254
255   if (writer->tcs.metadata_fd != NULL)
256     fclose (writer->tcs.metadata_fd);
257
258   if (writer->tcs.datastream_fd != NULL)
259     fclose (writer->tcs.datastream_fd);
260
261 }
262
263 /* This is the implementation of trace_file_write_ops method
264    target_save.  */
265
266 static int
267 ctf_target_save (struct trace_file_writer *self,
268                  const char *dirname)
269 {
270   /* Don't support save trace file to CTF format in the target.  */
271   return 0;
272 }
273
274 #ifdef USE_WIN32API
275 #undef mkdir
276 #define mkdir(pathname, mode) mkdir (pathname)
277 #endif
278
279 /* This is the implementation of trace_file_write_ops method
280    start.  It creates the directory DIRNAME, metadata and datastream
281    in the directory.  */
282
283 static void
284 ctf_start (struct trace_file_writer *self, const char *dirname)
285 {
286   char *file_name;
287   struct cleanup *old_chain;
288   struct ctf_trace_file_writer *writer
289     = (struct ctf_trace_file_writer *) self;
290   int i;
291   mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR
292 #ifdef S_IRGRP
293     | S_IRGRP
294 #endif
295 #ifdef S_IXGRP
296     | S_IXGRP
297 #endif
298     | S_IROTH /* Defined in common/gdb_stat.h if not defined.  */
299 #ifdef S_IXOTH
300     | S_IXOTH
301 #endif
302     ;
303
304   /* Create DIRNAME.  */
305   if (mkdir (dirname, hmode) && errno != EEXIST)
306     error (_("Unable to open directory '%s' for saving trace data (%s)"),
307            dirname, safe_strerror (errno));
308
309   memset (&writer->tcs, '\0', sizeof (writer->tcs));
310
311   file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
312   old_chain = make_cleanup (xfree, file_name);
313
314   writer->tcs.metadata_fd = fopen (file_name, "w");
315   if (writer->tcs.metadata_fd == NULL)
316     error (_("Unable to open file '%s' for saving trace data (%s)"),
317            file_name, safe_strerror (errno));
318   do_cleanups (old_chain);
319
320   ctf_save_metadata_header (&writer->tcs);
321
322   file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
323   old_chain = make_cleanup (xfree, file_name);
324   writer->tcs.datastream_fd = fopen (file_name, "w");
325   if (writer->tcs.datastream_fd == NULL)
326     error (_("Unable to open file '%s' for saving trace data (%s)"),
327            file_name, safe_strerror (errno));
328   do_cleanups (old_chain);
329 }
330
331 /* This is the implementation of trace_file_write_ops method
332    write_header.  Write the types of events on trace variable and
333    frame.  */
334
335 static void
336 ctf_write_header (struct trace_file_writer *self)
337 {
338   struct ctf_trace_file_writer *writer
339     = (struct ctf_trace_file_writer *) self;
340
341
342   ctf_save_write_metadata (&writer->tcs, "\n");
343   ctf_save_write_metadata (&writer->tcs,
344                            "event {\n\tname = \"memory\";\n\tid = %u;\n"
345                            "\tfields := struct { \n"
346                            "\t\tuint64_t address;\n"
347                            "\t\tuint16_t length;\n"
348                            "\t\tuint8_t contents[length];\n"
349                            "\t};\n"
350                            "};\n", CTF_EVENT_ID_MEMORY);
351
352   ctf_save_write_metadata (&writer->tcs, "\n");
353   ctf_save_write_metadata (&writer->tcs,
354                            "event {\n\tname = \"tsv\";\n\tid = %u;\n"
355                            "\tfields := struct { \n"
356                            "\t\tuint64_t val;\n"
357                            "\t\tuint32_t num;\n"
358                            "\t};\n"
359                            "};\n", CTF_EVENT_ID_TSV);
360
361   ctf_save_write_metadata (&writer->tcs, "\n");
362   ctf_save_write_metadata (&writer->tcs,
363                            "event {\n\tname = \"frame\";\n\tid = %u;\n"
364                            "\tfields := struct { \n"
365                            "\t};\n"
366                            "};\n", CTF_EVENT_ID_FRAME);
367
368   gdb_assert (writer->tcs.content_size == 0);
369   gdb_assert (writer->tcs.packet_start == 0);
370 }
371
372 /* This is the implementation of trace_file_write_ops method
373    write_regblock_type.  Write the type of register event in
374    metadata.  */
375
376 static void
377 ctf_write_regblock_type (struct trace_file_writer *self, int size)
378 {
379   struct ctf_trace_file_writer *writer
380     = (struct ctf_trace_file_writer *) self;
381
382   ctf_save_write_metadata (&writer->tcs, "\n");
383
384   ctf_save_write_metadata (&writer->tcs,
385                            "event {\n\tname = \"register\";\n\tid = %u;\n"
386                            "\tfields := struct { \n"
387                            "\t\tascii contents[%d];\n"
388                            "\t};\n"
389                            "};\n",
390                            CTF_EVENT_ID_REGISTER, size);
391 }
392
393 /* This is the implementation of trace_file_write_ops method
394    write_status.  */
395
396 static void
397 ctf_write_status (struct trace_file_writer *self,
398                   struct trace_status *ts)
399 {
400   /* It is not supported yet to write trace status into CTF trace
401      data.  */
402 }
403
404 /* This is the implementation of trace_file_write_ops method
405    write_uploaded_tsv.  */
406
407 static void
408 ctf_write_uploaded_tsv (struct trace_file_writer *self,
409                         struct uploaded_tsv *tsv)
410 {
411   /* It is not supported yet to write uploaded trace variables
412      into CTF trace data.  */
413 }
414
415 /* This is the implementation of trace_file_write_ops method
416    write_uploaded_tp.  */
417
418 static void
419 ctf_write_uploaded_tp (struct trace_file_writer *self,
420                        struct uploaded_tp *tp)
421 {
422   /* It is not supported yet to write uploaded tracepoints
423      into CTF trace data.  */
424 }
425
426 /* This is the implementation of trace_file_write_ops method
427    write_definition_end.  */
428
429 static void
430 ctf_write_definition_end (struct trace_file_writer *self)
431 {
432   /* Nothing to do for CTF.  */
433 }
434
435 /* The minimal file size of data stream.  It is required by
436    babeltrace.  */
437
438 #define CTF_FILE_MIN_SIZE               4096
439
440 /* This is the implementation of trace_file_write_ops method
441    end.  */
442
443 static void
444 ctf_end (struct trace_file_writer *self)
445 {
446   struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
447
448   gdb_assert (writer->tcs.content_size == 0);
449   /* The babeltrace requires or assumes that the size of datastream
450      file is greater than 4096 bytes.  If we don't generate enough
451      packets and events, create a fake packet which has zero event,
452       to use up the space.  */
453   if (writer->tcs.packet_start < CTF_FILE_MIN_SIZE)
454     {
455       uint32_t u32;
456
457       /* magic.  */
458       u32 = CTF_MAGIC;
459       ctf_save_write_uint32 (&writer->tcs, u32);
460
461       /* content_size.  */
462       u32 = 0;
463       ctf_save_write_uint32 (&writer->tcs, u32);
464
465       /* packet_size.  */
466       u32 = 12;
467       if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
468         u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
469
470       u32 *= TARGET_CHAR_BIT;
471       ctf_save_write_uint32 (&writer->tcs, u32);
472
473       /* tpnum.  */
474       u32 = 0;
475       ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
476
477       /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
478          than that.  */
479       if (CTF_FILE_MIN_SIZE
480           > (writer->tcs.packet_start + writer->tcs.content_size))
481         {
482           gdb_byte b = 0;
483
484           /* Fake the content size to avoid assertion failure in
485              ctf_save_fseek.  */
486           writer->tcs.content_size = (CTF_FILE_MIN_SIZE
487                                       - 1 - writer->tcs.packet_start);
488           ctf_save_fseek (&writer->tcs, CTF_FILE_MIN_SIZE - 1,
489                           SEEK_SET);
490           ctf_save_write (&writer->tcs, &b, 1);
491         }
492     }
493 }
494
495 /* This is the implementation of trace_frame_write_ops method
496    start.  */
497
498 static void
499 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
500 {
501   struct ctf_trace_file_writer *writer
502     = (struct ctf_trace_file_writer *) self;
503   uint32_t id = CTF_EVENT_ID_FRAME;
504   uint32_t u32;
505
506   /* Step 1: Write packet context.  */
507   /* magic.  */
508   u32 = CTF_MAGIC;
509   ctf_save_write_uint32 (&writer->tcs, u32);
510   /* content_size and packet_size..  We still don't know the value,
511      write it later.  */
512   ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
513   ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
514   /* Tracepoint number.  */
515   ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
516
517   /* Step 2: Write event "frame".  */
518   /* Event Id.  */
519   ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
520 }
521
522 /* This is the implementation of trace_frame_write_ops method
523    write_r_block.  */
524
525 static void
526 ctf_write_frame_r_block (struct trace_file_writer *self,
527                          gdb_byte *buf, int32_t size)
528 {
529   struct ctf_trace_file_writer *writer
530     = (struct ctf_trace_file_writer *) self;
531   uint32_t id = CTF_EVENT_ID_REGISTER;
532
533   /* Event Id.  */
534   ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
535
536   /* array contents.  */
537   ctf_save_align_write (&writer->tcs, buf, size, 1);
538 }
539
540 /* This is the implementation of trace_frame_write_ops method
541    write_m_block_header.  */
542
543 static void
544 ctf_write_frame_m_block_header (struct trace_file_writer *self,
545                                 uint64_t addr, uint16_t length)
546 {
547   struct ctf_trace_file_writer *writer
548     = (struct ctf_trace_file_writer *) self;
549   uint32_t event_id = CTF_EVENT_ID_MEMORY;
550
551   /* Event Id.  */
552   ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
553
554   /* Address.  */
555   ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
556
557   /* Length.  */
558   ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
559 }
560
561 /* This is the implementation of trace_frame_write_ops method
562    write_m_block_memory.  */
563
564 static void
565 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
566                                 gdb_byte *buf, uint16_t length)
567 {
568   struct ctf_trace_file_writer *writer
569     = (struct ctf_trace_file_writer *) self;
570
571   /* Contents.  */
572   ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
573 }
574
575 /* This is the implementation of trace_frame_write_ops method
576    write_v_block.  */
577
578 static void
579 ctf_write_frame_v_block (struct trace_file_writer *self,
580                          int32_t num, uint64_t val)
581 {
582   struct ctf_trace_file_writer *writer
583     = (struct ctf_trace_file_writer *) self;
584   uint32_t id = CTF_EVENT_ID_TSV;
585
586   /* Event Id.  */
587   ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
588
589   /* val.  */
590   ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
591   /* num.  */
592   ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
593 }
594
595 /* This is the implementation of trace_frame_write_ops method
596    end.  */
597
598 static void
599 ctf_write_frame_end (struct trace_file_writer *self)
600 {
601   struct ctf_trace_file_writer *writer
602     = (struct ctf_trace_file_writer *) self;
603   uint32_t u32;
604   uint32_t t;
605
606   /* Write the content size to packet header.  */
607   ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
608                   SEEK_SET);
609   u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
610
611   t = writer->tcs.content_size;
612   ctf_save_write_uint32 (&writer->tcs, u32);
613
614   /* Write the packet size.  */
615   u32 += 4 * TARGET_CHAR_BIT;
616   ctf_save_write_uint32 (&writer->tcs, u32);
617
618   writer->tcs.content_size = t;
619
620   /* Write zero at the end of the packet.  */
621   ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
622                   SEEK_SET);
623   u32 = 0;
624   ctf_save_write_uint32 (&writer->tcs, u32);
625   writer->tcs.content_size = t;
626
627   ctf_save_next_packet (&writer->tcs);
628 }
629
630 /* Operations to write various types of trace frames into CTF
631    format.  */
632
633 static const struct trace_frame_write_ops ctf_write_frame_ops =
634 {
635   ctf_write_frame_start,
636   ctf_write_frame_r_block,
637   ctf_write_frame_m_block_header,
638   ctf_write_frame_m_block_memory,
639   ctf_write_frame_v_block,
640   ctf_write_frame_end,
641 };
642
643 /* Operations to write trace buffers into CTF format.  */
644
645 static const struct trace_file_write_ops ctf_write_ops =
646 {
647   ctf_dtor,
648   ctf_target_save,
649   ctf_start,
650   ctf_write_header,
651   ctf_write_regblock_type,
652   ctf_write_status,
653   ctf_write_uploaded_tsv,
654   ctf_write_uploaded_tp,
655   ctf_write_definition_end,
656   NULL,
657   &ctf_write_frame_ops,
658   ctf_end,
659 };
660
661 /* Return a trace writer for CTF format.  */
662
663 struct trace_file_writer *
664 ctf_trace_file_writer_new (void)
665 {
666   struct ctf_trace_file_writer *writer
667     = xmalloc (sizeof (struct ctf_trace_file_writer));
668
669   writer->base.ops = &ctf_write_ops;
670
671   return (struct trace_file_writer *) writer;
672 }