]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ctf.c
gdb/
[thirdparty/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
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 #define mkdir(pathname, mode) _mkdir (pathname)
276 #endif
277
278 /* This is the implementation of trace_file_write_ops method
279 start. It creates the directory DIRNAME, metadata and datastream
280 in the directory. */
281
282 static void
283 ctf_start (struct trace_file_writer *self, const char *dirname)
284 {
285 char *file_name;
286 struct cleanup *old_chain;
287 struct ctf_trace_file_writer *writer
288 = (struct ctf_trace_file_writer *) self;
289 int i;
290 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR
291 #ifdef S_IRGRP
292 | S_IRGRP
293 #endif
294 #ifdef S_IXGRP
295 | S_IXGRP
296 #endif
297 | S_IROTH /* Defined in common/gdb_stat.h if not defined. */
298 #ifdef S_IXOTH
299 | S_IXOTH
300 #endif
301 ;
302
303 /* Create DIRNAME. */
304 if (mkdir (dirname, hmode) && errno != EEXIST)
305 error (_("Unable to open directory '%s' for saving trace data (%s)"),
306 dirname, safe_strerror (errno));
307
308 memset (&writer->tcs, '\0', sizeof (writer->tcs));
309
310 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
311 old_chain = make_cleanup (xfree, file_name);
312
313 writer->tcs.metadata_fd = fopen (file_name, "w");
314 if (writer->tcs.metadata_fd == NULL)
315 error (_("Unable to open file '%s' for saving trace data (%s)"),
316 file_name, safe_strerror (errno));
317 do_cleanups (old_chain);
318
319 ctf_save_metadata_header (&writer->tcs);
320
321 file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
322 old_chain = make_cleanup (xfree, file_name);
323 writer->tcs.datastream_fd = fopen (file_name, "w");
324 if (writer->tcs.datastream_fd == NULL)
325 error (_("Unable to open file '%s' for saving trace data (%s)"),
326 file_name, safe_strerror (errno));
327 do_cleanups (old_chain);
328 }
329
330 /* This is the implementation of trace_file_write_ops method
331 write_header. Write the types of events on trace variable and
332 frame. */
333
334 static void
335 ctf_write_header (struct trace_file_writer *self)
336 {
337 struct ctf_trace_file_writer *writer
338 = (struct ctf_trace_file_writer *) self;
339
340
341 ctf_save_write_metadata (&writer->tcs, "\n");
342 ctf_save_write_metadata (&writer->tcs,
343 "event {\n\tname = \"memory\";\n\tid = %u;\n"
344 "\tfields := struct { \n"
345 "\t\tuint64_t address;\n"
346 "\t\tuint16_t length;\n"
347 "\t\tuint8_t contents[length];\n"
348 "\t};\n"
349 "};\n", CTF_EVENT_ID_MEMORY);
350
351 ctf_save_write_metadata (&writer->tcs, "\n");
352 ctf_save_write_metadata (&writer->tcs,
353 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
354 "\tfields := struct { \n"
355 "\t\tuint64_t val;\n"
356 "\t\tuint32_t num;\n"
357 "\t};\n"
358 "};\n", CTF_EVENT_ID_TSV);
359
360 ctf_save_write_metadata (&writer->tcs, "\n");
361 ctf_save_write_metadata (&writer->tcs,
362 "event {\n\tname = \"frame\";\n\tid = %u;\n"
363 "\tfields := struct { \n"
364 "\t};\n"
365 "};\n", CTF_EVENT_ID_FRAME);
366
367 gdb_assert (writer->tcs.content_size == 0);
368 gdb_assert (writer->tcs.packet_start == 0);
369 }
370
371 /* This is the implementation of trace_file_write_ops method
372 write_regblock_type. Write the type of register event in
373 metadata. */
374
375 static void
376 ctf_write_regblock_type (struct trace_file_writer *self, int size)
377 {
378 struct ctf_trace_file_writer *writer
379 = (struct ctf_trace_file_writer *) self;
380
381 ctf_save_write_metadata (&writer->tcs, "\n");
382
383 ctf_save_write_metadata (&writer->tcs,
384 "event {\n\tname = \"register\";\n\tid = %u;\n"
385 "\tfields := struct { \n"
386 "\t\tascii contents[%d];\n"
387 "\t};\n"
388 "};\n",
389 CTF_EVENT_ID_REGISTER, size);
390 }
391
392 /* This is the implementation of trace_file_write_ops method
393 write_status. */
394
395 static void
396 ctf_write_status (struct trace_file_writer *self,
397 struct trace_status *ts)
398 {
399 /* It is not supported yet to write trace status into CTF trace
400 data. */
401 }
402
403 /* This is the implementation of trace_file_write_ops method
404 write_uploaded_tsv. */
405
406 static void
407 ctf_write_uploaded_tsv (struct trace_file_writer *self,
408 struct uploaded_tsv *tsv)
409 {
410 /* It is not supported yet to write uploaded trace variables
411 into CTF trace data. */
412 }
413
414 /* This is the implementation of trace_file_write_ops method
415 write_uploaded_tp. */
416
417 static void
418 ctf_write_uploaded_tp (struct trace_file_writer *self,
419 struct uploaded_tp *tp)
420 {
421 /* It is not supported yet to write uploaded tracepoints
422 into CTF trace data. */
423 }
424
425 /* This is the implementation of trace_file_write_ops method
426 write_definition_end. */
427
428 static void
429 ctf_write_definition_end (struct trace_file_writer *self)
430 {
431 /* Nothing to do for CTF. */
432 }
433
434 /* The minimal file size of data stream. It is required by
435 babeltrace. */
436
437 #define CTF_FILE_MIN_SIZE 4096
438
439 /* This is the implementation of trace_file_write_ops method
440 end. */
441
442 static void
443 ctf_end (struct trace_file_writer *self)
444 {
445 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
446
447 gdb_assert (writer->tcs.content_size == 0);
448 /* The babeltrace requires or assumes that the size of datastream
449 file is greater than 4096 bytes. If we don't generate enough
450 packets and events, create a fake packet which has zero event,
451 to use up the space. */
452 if (writer->tcs.packet_start < CTF_FILE_MIN_SIZE)
453 {
454 uint32_t u32;
455
456 /* magic. */
457 u32 = CTF_MAGIC;
458 ctf_save_write_uint32 (&writer->tcs, u32);
459
460 /* content_size. */
461 u32 = 0;
462 ctf_save_write_uint32 (&writer->tcs, u32);
463
464 /* packet_size. */
465 u32 = 12;
466 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
467 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
468
469 u32 *= TARGET_CHAR_BIT;
470 ctf_save_write_uint32 (&writer->tcs, u32);
471
472 /* tpnum. */
473 u32 = 0;
474 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
475
476 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
477 than that. */
478 if (CTF_FILE_MIN_SIZE
479 > (writer->tcs.packet_start + writer->tcs.content_size))
480 {
481 gdb_byte b = 0;
482
483 /* Fake the content size to avoid assertion failure in
484 ctf_save_fseek. */
485 writer->tcs.content_size = (CTF_FILE_MIN_SIZE
486 - 1 - writer->tcs.packet_start);
487 ctf_save_fseek (&writer->tcs, CTF_FILE_MIN_SIZE - 1,
488 SEEK_SET);
489 ctf_save_write (&writer->tcs, &b, 1);
490 }
491 }
492 }
493
494 /* This is the implementation of trace_frame_write_ops method
495 start. */
496
497 static void
498 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
499 {
500 struct ctf_trace_file_writer *writer
501 = (struct ctf_trace_file_writer *) self;
502 uint32_t id = CTF_EVENT_ID_FRAME;
503 uint32_t u32;
504
505 /* Step 1: Write packet context. */
506 /* magic. */
507 u32 = CTF_MAGIC;
508 ctf_save_write_uint32 (&writer->tcs, u32);
509 /* content_size and packet_size.. We still don't know the value,
510 write it later. */
511 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
512 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
513 /* Tracepoint number. */
514 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
515
516 /* Step 2: Write event "frame". */
517 /* Event Id. */
518 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
519 }
520
521 /* This is the implementation of trace_frame_write_ops method
522 write_r_block. */
523
524 static void
525 ctf_write_frame_r_block (struct trace_file_writer *self,
526 gdb_byte *buf, int32_t size)
527 {
528 struct ctf_trace_file_writer *writer
529 = (struct ctf_trace_file_writer *) self;
530 uint32_t id = CTF_EVENT_ID_REGISTER;
531
532 /* Event Id. */
533 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
534
535 /* array contents. */
536 ctf_save_align_write (&writer->tcs, buf, size, 1);
537 }
538
539 /* This is the implementation of trace_frame_write_ops method
540 write_m_block_header. */
541
542 static void
543 ctf_write_frame_m_block_header (struct trace_file_writer *self,
544 uint64_t addr, uint16_t length)
545 {
546 struct ctf_trace_file_writer *writer
547 = (struct ctf_trace_file_writer *) self;
548 uint32_t event_id = CTF_EVENT_ID_MEMORY;
549
550 /* Event Id. */
551 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
552
553 /* Address. */
554 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
555
556 /* Length. */
557 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
558 }
559
560 /* This is the implementation of trace_frame_write_ops method
561 write_m_block_memory. */
562
563 static void
564 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
565 gdb_byte *buf, uint16_t length)
566 {
567 struct ctf_trace_file_writer *writer
568 = (struct ctf_trace_file_writer *) self;
569
570 /* Contents. */
571 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
572 }
573
574 /* This is the implementation of trace_frame_write_ops method
575 write_v_block. */
576
577 static void
578 ctf_write_frame_v_block (struct trace_file_writer *self,
579 int32_t num, uint64_t val)
580 {
581 struct ctf_trace_file_writer *writer
582 = (struct ctf_trace_file_writer *) self;
583 uint32_t id = CTF_EVENT_ID_TSV;
584
585 /* Event Id. */
586 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
587
588 /* val. */
589 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
590 /* num. */
591 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
592 }
593
594 /* This is the implementation of trace_frame_write_ops method
595 end. */
596
597 static void
598 ctf_write_frame_end (struct trace_file_writer *self)
599 {
600 struct ctf_trace_file_writer *writer
601 = (struct ctf_trace_file_writer *) self;
602 uint32_t u32;
603 uint32_t t;
604
605 /* Write the content size to packet header. */
606 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
607 SEEK_SET);
608 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
609
610 t = writer->tcs.content_size;
611 ctf_save_write_uint32 (&writer->tcs, u32);
612
613 /* Write the packet size. */
614 u32 += 4 * TARGET_CHAR_BIT;
615 ctf_save_write_uint32 (&writer->tcs, u32);
616
617 writer->tcs.content_size = t;
618
619 /* Write zero at the end of the packet. */
620 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
621 SEEK_SET);
622 u32 = 0;
623 ctf_save_write_uint32 (&writer->tcs, u32);
624 writer->tcs.content_size = t;
625
626 ctf_save_next_packet (&writer->tcs);
627 }
628
629 /* Operations to write various types of trace frames into CTF
630 format. */
631
632 static const struct trace_frame_write_ops ctf_write_frame_ops =
633 {
634 ctf_write_frame_start,
635 ctf_write_frame_r_block,
636 ctf_write_frame_m_block_header,
637 ctf_write_frame_m_block_memory,
638 ctf_write_frame_v_block,
639 ctf_write_frame_end,
640 };
641
642 /* Operations to write trace buffers into CTF format. */
643
644 static const struct trace_file_write_ops ctf_write_ops =
645 {
646 ctf_dtor,
647 ctf_target_save,
648 ctf_start,
649 ctf_write_header,
650 ctf_write_regblock_type,
651 ctf_write_status,
652 ctf_write_uploaded_tsv,
653 ctf_write_uploaded_tp,
654 ctf_write_definition_end,
655 NULL,
656 &ctf_write_frame_ops,
657 ctf_end,
658 };
659
660 /* Return a trace writer for CTF format. */
661
662 struct trace_file_writer *
663 ctf_trace_file_writer_new (void)
664 {
665 struct ctf_trace_file_writer *writer
666 = xmalloc (sizeof (struct ctf_trace_file_writer));
667
668 writer->base.ops = &ctf_write_ops;
669
670 return (struct trace_file_writer *) writer;
671 }