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>
7 This file is part of GDB.
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.
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.
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/>. */
24 #include "tracepoint.h"
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:
34 1. The length (in bytes) of register cache. Event "register" will
35 be defined in metadata, which includes the length.
37 2. Trace status. Not implemented yet in CTF writer.
39 3. Uploaded trace variables and tracepoints. Not implemented yet
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
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
55 #define CTF_MAGIC 0xC1FC1FC1
56 #define CTF_SAVE_MAJOR 1
57 #define CTF_SAVE_MINOR 8
59 #define CTF_METADATA_NAME "metadata"
60 #define CTF_DATASTREAM_NAME "datastream"
62 /* Reserved event id. */
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
69 /* The state kept while writing the CTF datastream file. */
71 struct trace_write_handler
73 /* File descriptor of metadata. */
75 /* File descriptor of traceframes. */
78 /* This is the content size of the current packet. */
81 /* This is the start offset of current packet. */
85 /* Write metadata in FORMAT. */
88 ctf_save_write_metadata (struct trace_write_handler *handler,
89 const char *format, ...)
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));
100 /* Write BUF of length SIZE to datastream file represented by
104 ctf_save_write (struct trace_write_handler *handler,
105 const gdb_byte *buf, size_t size)
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));
111 handler->content_size += size;
116 /* Write a unsigned 32-bit integer to datastream file represented by
119 #define ctf_save_write_uint32(HANDLER, U32) \
120 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
122 /* Set datastream file position. Update HANDLER->content_size
123 if WHENCE is SEEK_CUR. */
126 ctf_save_fseek (struct trace_write_handler *handler, long offset,
129 gdb_assert (whence != SEEK_END);
130 gdb_assert (whence != SEEK_SET
131 || offset <= handler->content_size + handler->packet_start);
133 if (fseek (handler->datastream_fd, offset, whence))
134 error (_("Unable to seek file for saving trace data (%s)"),
135 safe_strerror (errno));
137 if (whence == SEEK_CUR)
138 handler->content_size += offset;
143 /* Change the datastream file position to align on ALIGN_SIZE,
144 and write BUF to datastream file. The size of BUF is SIZE. */
147 ctf_save_align_write (struct trace_write_handler *handler,
149 size_t size, size_t align_size)
152 = (align_up (handler->content_size, align_size)
153 - handler->content_size);
155 if (ctf_save_fseek (handler, offset, SEEK_CUR))
158 if (ctf_save_write (handler, buf, size))
164 /* Write events to next new packet. */
167 ctf_save_next_packet (struct trace_write_handler *handler)
169 handler->packet_start += (handler->content_size + 4);
170 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
171 handler->content_size = 0;
174 /* Write the CTF metadata header. */
177 ctf_save_metadata_header (struct trace_write_handler *handler)
179 const char metadata_fmt[] =
183 " byte_order = %s;\n" /* be or le */
184 " packet.header := struct {\n"
190 " packet.context := struct {\n"
191 " uint32_t content_size;\n"
192 " uint32_t packet_size;\n"
195 " event.header := struct {\n"
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;}"
206 ctf_save_write_metadata (handler,
207 "typealias integer { size = 8; align = 8; "
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;}"
220 ctf_save_write_metadata (handler, "\n");
222 /* Get the byte order of the host and write CTF data in this byte
225 #define HOST_ENDIANNESS "be"
227 #define HOST_ENDIANNESS "le"
230 ctf_save_write_metadata (handler, metadata_fmt,
231 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
233 ctf_save_write_metadata (handler, "\n");
236 /* CTF trace writer. */
238 struct ctf_trace_file_writer
240 struct trace_file_writer base;
242 /* States related to writing CTF trace file. */
243 struct trace_write_handler tcs;
246 /* This is the implementation of trace_file_write_ops method
250 ctf_dtor (struct trace_file_writer *self)
252 struct ctf_trace_file_writer *writer
253 = (struct ctf_trace_file_writer *) self;
255 if (writer->tcs.metadata_fd != NULL)
256 fclose (writer->tcs.metadata_fd);
258 if (writer->tcs.datastream_fd != NULL)
259 fclose (writer->tcs.datastream_fd);
263 /* This is the implementation of trace_file_write_ops method
267 ctf_target_save (struct trace_file_writer *self,
270 /* Don't support save trace file to CTF format in the target. */
276 #define mkdir(pathname, mode) mkdir (pathname)
279 /* This is the implementation of trace_file_write_ops method
280 start. It creates the directory DIRNAME, metadata and datastream
284 ctf_start (struct trace_file_writer *self, const char *dirname)
287 struct cleanup *old_chain;
288 struct ctf_trace_file_writer *writer
289 = (struct ctf_trace_file_writer *) self;
291 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR
298 | S_IROTH /* Defined in common/gdb_stat.h if not defined. */
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));
309 memset (&writer->tcs, '\0', sizeof (writer->tcs));
311 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
312 old_chain = make_cleanup (xfree, file_name);
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);
320 ctf_save_metadata_header (&writer->tcs);
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);
331 /* This is the implementation of trace_file_write_ops method
332 write_header. Write the types of events on trace variable and
336 ctf_write_header (struct trace_file_writer *self)
338 struct ctf_trace_file_writer *writer
339 = (struct ctf_trace_file_writer *) self;
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"
350 "};\n", CTF_EVENT_ID_MEMORY);
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"
359 "};\n", CTF_EVENT_ID_TSV);
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"
366 "};\n", CTF_EVENT_ID_FRAME);
368 gdb_assert (writer->tcs.content_size == 0);
369 gdb_assert (writer->tcs.packet_start == 0);
372 /* This is the implementation of trace_file_write_ops method
373 write_regblock_type. Write the type of register event in
377 ctf_write_regblock_type (struct trace_file_writer *self, int size)
379 struct ctf_trace_file_writer *writer
380 = (struct ctf_trace_file_writer *) self;
382 ctf_save_write_metadata (&writer->tcs, "\n");
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"
390 CTF_EVENT_ID_REGISTER, size);
393 /* This is the implementation of trace_file_write_ops method
397 ctf_write_status (struct trace_file_writer *self,
398 struct trace_status *ts)
400 /* It is not supported yet to write trace status into CTF trace
404 /* This is the implementation of trace_file_write_ops method
405 write_uploaded_tsv. */
408 ctf_write_uploaded_tsv (struct trace_file_writer *self,
409 struct uploaded_tsv *tsv)
411 /* It is not supported yet to write uploaded trace variables
412 into CTF trace data. */
415 /* This is the implementation of trace_file_write_ops method
416 write_uploaded_tp. */
419 ctf_write_uploaded_tp (struct trace_file_writer *self,
420 struct uploaded_tp *tp)
422 /* It is not supported yet to write uploaded tracepoints
423 into CTF trace data. */
426 /* This is the implementation of trace_file_write_ops method
427 write_definition_end. */
430 ctf_write_definition_end (struct trace_file_writer *self)
432 /* Nothing to do for CTF. */
435 /* The minimal file size of data stream. It is required by
438 #define CTF_FILE_MIN_SIZE 4096
440 /* This is the implementation of trace_file_write_ops method
444 ctf_end (struct trace_file_writer *self)
446 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
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)
459 ctf_save_write_uint32 (&writer->tcs, u32);
463 ctf_save_write_uint32 (&writer->tcs, u32);
467 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
468 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
470 u32 *= TARGET_CHAR_BIT;
471 ctf_save_write_uint32 (&writer->tcs, u32);
475 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
477 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
479 if (CTF_FILE_MIN_SIZE
480 > (writer->tcs.packet_start + writer->tcs.content_size))
484 /* Fake the content size to avoid assertion failure in
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,
490 ctf_save_write (&writer->tcs, &b, 1);
495 /* This is the implementation of trace_frame_write_ops method
499 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
501 struct ctf_trace_file_writer *writer
502 = (struct ctf_trace_file_writer *) self;
503 uint32_t id = CTF_EVENT_ID_FRAME;
506 /* Step 1: Write packet context. */
509 ctf_save_write_uint32 (&writer->tcs, u32);
510 /* content_size and packet_size.. We still don't know the value,
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);
517 /* Step 2: Write event "frame". */
519 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
522 /* This is the implementation of trace_frame_write_ops method
526 ctf_write_frame_r_block (struct trace_file_writer *self,
527 gdb_byte *buf, int32_t size)
529 struct ctf_trace_file_writer *writer
530 = (struct ctf_trace_file_writer *) self;
531 uint32_t id = CTF_EVENT_ID_REGISTER;
534 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
536 /* array contents. */
537 ctf_save_align_write (&writer->tcs, buf, size, 1);
540 /* This is the implementation of trace_frame_write_ops method
541 write_m_block_header. */
544 ctf_write_frame_m_block_header (struct trace_file_writer *self,
545 uint64_t addr, uint16_t length)
547 struct ctf_trace_file_writer *writer
548 = (struct ctf_trace_file_writer *) self;
549 uint32_t event_id = CTF_EVENT_ID_MEMORY;
552 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
555 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
558 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
561 /* This is the implementation of trace_frame_write_ops method
562 write_m_block_memory. */
565 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
566 gdb_byte *buf, uint16_t length)
568 struct ctf_trace_file_writer *writer
569 = (struct ctf_trace_file_writer *) self;
572 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
575 /* This is the implementation of trace_frame_write_ops method
579 ctf_write_frame_v_block (struct trace_file_writer *self,
580 int32_t num, uint64_t val)
582 struct ctf_trace_file_writer *writer
583 = (struct ctf_trace_file_writer *) self;
584 uint32_t id = CTF_EVENT_ID_TSV;
587 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
590 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
592 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
595 /* This is the implementation of trace_frame_write_ops method
599 ctf_write_frame_end (struct trace_file_writer *self)
601 struct ctf_trace_file_writer *writer
602 = (struct ctf_trace_file_writer *) self;
606 /* Write the content size to packet header. */
607 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
609 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
611 t = writer->tcs.content_size;
612 ctf_save_write_uint32 (&writer->tcs, u32);
614 /* Write the packet size. */
615 u32 += 4 * TARGET_CHAR_BIT;
616 ctf_save_write_uint32 (&writer->tcs, u32);
618 writer->tcs.content_size = t;
620 /* Write zero at the end of the packet. */
621 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
624 ctf_save_write_uint32 (&writer->tcs, u32);
625 writer->tcs.content_size = t;
627 ctf_save_next_packet (&writer->tcs);
630 /* Operations to write various types of trace frames into CTF
633 static const struct trace_frame_write_ops ctf_write_frame_ops =
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,
643 /* Operations to write trace buffers into CTF format. */
645 static const struct trace_file_write_ops ctf_write_ops =
651 ctf_write_regblock_type,
653 ctf_write_uploaded_tsv,
654 ctf_write_uploaded_tp,
655 ctf_write_definition_end,
657 &ctf_write_frame_ops,
661 /* Return a trace writer for CTF format. */
663 struct trace_file_writer *
664 ctf_trace_file_writer_new (void)
666 struct ctf_trace_file_writer *writer
667 = xmalloc (sizeof (struct ctf_trace_file_writer));
669 writer->base.ops = &ctf_write_ops;
671 return (struct trace_file_writer *) writer;