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. */
275 #define mkdir(pathname, mode) _mkdir (pathname)
278 /* This is the implementation of trace_file_write_ops method
279 start. It creates the directory DIRNAME, metadata and datastream
283 ctf_start (struct trace_file_writer *self, const char *dirname)
286 struct cleanup *old_chain;
287 struct ctf_trace_file_writer *writer
288 = (struct ctf_trace_file_writer *) self;
290 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR
297 | S_IROTH /* Defined in common/gdb_stat.h if not defined. */
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));
308 memset (&writer->tcs, '\0', sizeof (writer->tcs));
310 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
311 old_chain = make_cleanup (xfree, file_name);
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);
319 ctf_save_metadata_header (&writer->tcs);
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);
330 /* This is the implementation of trace_file_write_ops method
331 write_header. Write the types of events on trace variable and
335 ctf_write_header (struct trace_file_writer *self)
337 struct ctf_trace_file_writer *writer
338 = (struct ctf_trace_file_writer *) self;
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"
349 "};\n", CTF_EVENT_ID_MEMORY);
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"
358 "};\n", CTF_EVENT_ID_TSV);
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"
365 "};\n", CTF_EVENT_ID_FRAME);
367 gdb_assert (writer->tcs.content_size == 0);
368 gdb_assert (writer->tcs.packet_start == 0);
371 /* This is the implementation of trace_file_write_ops method
372 write_regblock_type. Write the type of register event in
376 ctf_write_regblock_type (struct trace_file_writer *self, int size)
378 struct ctf_trace_file_writer *writer
379 = (struct ctf_trace_file_writer *) self;
381 ctf_save_write_metadata (&writer->tcs, "\n");
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"
389 CTF_EVENT_ID_REGISTER, size);
392 /* This is the implementation of trace_file_write_ops method
396 ctf_write_status (struct trace_file_writer *self,
397 struct trace_status *ts)
399 /* It is not supported yet to write trace status into CTF trace
403 /* This is the implementation of trace_file_write_ops method
404 write_uploaded_tsv. */
407 ctf_write_uploaded_tsv (struct trace_file_writer *self,
408 struct uploaded_tsv *tsv)
410 /* It is not supported yet to write uploaded trace variables
411 into CTF trace data. */
414 /* This is the implementation of trace_file_write_ops method
415 write_uploaded_tp. */
418 ctf_write_uploaded_tp (struct trace_file_writer *self,
419 struct uploaded_tp *tp)
421 /* It is not supported yet to write uploaded tracepoints
422 into CTF trace data. */
425 /* This is the implementation of trace_file_write_ops method
426 write_definition_end. */
429 ctf_write_definition_end (struct trace_file_writer *self)
431 /* Nothing to do for CTF. */
434 /* The minimal file size of data stream. It is required by
437 #define CTF_FILE_MIN_SIZE 4096
439 /* This is the implementation of trace_file_write_ops method
443 ctf_end (struct trace_file_writer *self)
445 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
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)
458 ctf_save_write_uint32 (&writer->tcs, u32);
462 ctf_save_write_uint32 (&writer->tcs, u32);
466 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
467 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
469 u32 *= TARGET_CHAR_BIT;
470 ctf_save_write_uint32 (&writer->tcs, u32);
474 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
476 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
478 if (CTF_FILE_MIN_SIZE
479 > (writer->tcs.packet_start + writer->tcs.content_size))
483 /* Fake the content size to avoid assertion failure in
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,
489 ctf_save_write (&writer->tcs, &b, 1);
494 /* This is the implementation of trace_frame_write_ops method
498 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
500 struct ctf_trace_file_writer *writer
501 = (struct ctf_trace_file_writer *) self;
502 uint32_t id = CTF_EVENT_ID_FRAME;
505 /* Step 1: Write packet context. */
508 ctf_save_write_uint32 (&writer->tcs, u32);
509 /* content_size and packet_size.. We still don't know the value,
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);
516 /* Step 2: Write event "frame". */
518 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
521 /* This is the implementation of trace_frame_write_ops method
525 ctf_write_frame_r_block (struct trace_file_writer *self,
526 gdb_byte *buf, int32_t size)
528 struct ctf_trace_file_writer *writer
529 = (struct ctf_trace_file_writer *) self;
530 uint32_t id = CTF_EVENT_ID_REGISTER;
533 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
535 /* array contents. */
536 ctf_save_align_write (&writer->tcs, buf, size, 1);
539 /* This is the implementation of trace_frame_write_ops method
540 write_m_block_header. */
543 ctf_write_frame_m_block_header (struct trace_file_writer *self,
544 uint64_t addr, uint16_t length)
546 struct ctf_trace_file_writer *writer
547 = (struct ctf_trace_file_writer *) self;
548 uint32_t event_id = CTF_EVENT_ID_MEMORY;
551 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
554 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
557 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
560 /* This is the implementation of trace_frame_write_ops method
561 write_m_block_memory. */
564 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
565 gdb_byte *buf, uint16_t length)
567 struct ctf_trace_file_writer *writer
568 = (struct ctf_trace_file_writer *) self;
571 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
574 /* This is the implementation of trace_frame_write_ops method
578 ctf_write_frame_v_block (struct trace_file_writer *self,
579 int32_t num, uint64_t val)
581 struct ctf_trace_file_writer *writer
582 = (struct ctf_trace_file_writer *) self;
583 uint32_t id = CTF_EVENT_ID_TSV;
586 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
589 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
591 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
594 /* This is the implementation of trace_frame_write_ops method
598 ctf_write_frame_end (struct trace_file_writer *self)
600 struct ctf_trace_file_writer *writer
601 = (struct ctf_trace_file_writer *) self;
605 /* Write the content size to packet header. */
606 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
608 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
610 t = writer->tcs.content_size;
611 ctf_save_write_uint32 (&writer->tcs, u32);
613 /* Write the packet size. */
614 u32 += 4 * TARGET_CHAR_BIT;
615 ctf_save_write_uint32 (&writer->tcs, u32);
617 writer->tcs.content_size = t;
619 /* Write zero at the end of the packet. */
620 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
623 ctf_save_write_uint32 (&writer->tcs, u32);
624 writer->tcs.content_size = t;
626 ctf_save_next_packet (&writer->tcs);
629 /* Operations to write various types of trace frames into CTF
632 static const struct trace_frame_write_ops ctf_write_frame_ops =
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,
642 /* Operations to write trace buffers into CTF format. */
644 static const struct trace_file_write_ops ctf_write_ops =
650 ctf_write_regblock_type,
652 ctf_write_uploaded_tsv,
653 ctf_write_uploaded_tp,
654 ctf_write_definition_end,
656 &ctf_write_frame_ops,
660 /* Return a trace writer for CTF format. */
662 struct trace_file_writer *
663 ctf_trace_file_writer_new (void)
665 struct ctf_trace_file_writer *writer
666 = xmalloc (sizeof (struct ctf_trace_file_writer));
668 writer->base.ops = &ctf_write_ops;
670 return (struct trace_file_writer *) writer;