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"
29 /* GDB saves trace buffers and other information (such as trace
30 status) got from the remote target into Common Trace Format (CTF).
31 The following types of information are expected to save in CTF:
33 1. The length (in bytes) of register cache. Event "register" will
34 be defined in metadata, which includes the length.
36 2. Trace status. Not implemented yet in CTF writer.
38 3. Uploaded trace variables and tracepoints. Not implemented yet
41 4. Trace frames. Each trace frame is composed by several blocks
42 of different types ('R', 'M', 'V'). One trace frame is saved in
43 one CTF packet and the blocks of this frame are saved as events.
44 4.1: The trace frame related information (such as the number of
45 tracepoint associated with this frame) is saved in the packet
47 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
48 "register" and "tsv" respectively.
49 4.3: When iterating over events, babeltrace can't tell iterator
50 goes to a new packet, so we need a marker or anchor to tell GDB
51 that iterator goes into a new packet or frame. We define event
54 #define CTF_MAGIC 0xC1FC1FC1
55 #define CTF_SAVE_MAJOR 1
56 #define CTF_SAVE_MINOR 8
58 #define CTF_METADATA_NAME "metadata"
59 #define CTF_DATASTREAM_NAME "datastream"
61 /* Reserved event id. */
63 #define CTF_EVENT_ID_REGISTER 0
64 #define CTF_EVENT_ID_TSV 1
65 #define CTF_EVENT_ID_MEMORY 2
66 #define CTF_EVENT_ID_FRAME 3
68 /* The state kept while writing the CTF datastream file. */
70 struct trace_write_handler
72 /* File descriptor of metadata. */
74 /* File descriptor of traceframes. */
77 /* This is the content size of the current packet. */
80 /* This is the start offset of current packet. */
84 /* Write metadata in FORMAT. */
87 ctf_save_write_metadata (struct trace_write_handler *handler,
88 const char *format, ...)
92 va_start (args, format);
93 if (vfprintf (handler->metadata_fd, format, args) < 0)
94 error (_("Unable to write metadata file (%s)"),
95 safe_strerror (errno));
99 /* Write BUF of length SIZE to datastream file represented by
103 ctf_save_write (struct trace_write_handler *handler,
104 const gdb_byte *buf, size_t size)
106 if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
107 error (_("Unable to write file for saving trace data (%s)"),
108 safe_strerror (errno));
110 handler->content_size += size;
115 /* Write a unsigned 32-bit integer to datastream file represented by
118 #define ctf_save_write_uint32(HANDLER, U32) \
119 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
121 /* Set datastream file position. Update HANDLER->content_size
122 if WHENCE is SEEK_CUR. */
125 ctf_save_fseek (struct trace_write_handler *handler, long offset,
128 gdb_assert (whence != SEEK_END);
129 gdb_assert (whence != SEEK_SET
130 || offset <= handler->content_size + handler->packet_start);
132 if (fseek (handler->datastream_fd, offset, whence))
133 error (_("Unable to seek file for saving trace data (%s)"),
134 safe_strerror (errno));
136 if (whence == SEEK_CUR)
137 handler->content_size += offset;
142 /* Change the datastream file position to align on ALIGN_SIZE,
143 and write BUF to datastream file. The size of BUF is SIZE. */
146 ctf_save_align_write (struct trace_write_handler *handler,
148 size_t size, size_t align_size)
151 = (align_up (handler->content_size, align_size)
152 - handler->content_size);
154 if (ctf_save_fseek (handler, offset, SEEK_CUR))
157 if (ctf_save_write (handler, buf, size))
163 /* Write events to next new packet. */
166 ctf_save_next_packet (struct trace_write_handler *handler)
168 handler->packet_start += (handler->content_size + 4);
169 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
170 handler->content_size = 0;
173 /* Write the CTF metadata header. */
176 ctf_save_metadata_header (struct trace_write_handler *handler)
178 const char metadata_fmt[] =
182 " byte_order = %s;\n" /* be or le */
183 " packet.header := struct {\n"
189 " packet.context := struct {\n"
190 " uint32_t content_size;\n"
191 " uint32_t packet_size;\n"
194 " event.header := struct {\n"
199 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
200 CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
201 ctf_save_write_metadata (handler,
202 "typealias integer { size = 8; align = 8; "
203 "signed = false; encoding = ascii;}"
205 ctf_save_write_metadata (handler,
206 "typealias integer { size = 8; align = 8; "
209 ctf_save_write_metadata (handler,
210 "typealias integer { size = 16; align = 16;"
211 "signed = false; } := uint16_t;\n");
212 ctf_save_write_metadata (handler,
213 "typealias integer { size = 32; align = 32;"
214 "signed = false; } := uint32_t;\n");
215 ctf_save_write_metadata (handler,
216 "typealias integer { size = 64; align = 64;"
217 "signed = false; base = hex;}"
219 ctf_save_write_metadata (handler, "\n");
221 ctf_save_write_metadata (handler, metadata_fmt,
222 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
223 BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be");
224 ctf_save_write_metadata (handler, "\n");
227 /* CTF trace writer. */
229 struct ctf_trace_file_writer
231 struct trace_file_writer base;
233 /* States related to writing CTF trace file. */
234 struct trace_write_handler tcs;
237 /* This is the implementation of trace_file_write_ops method
241 ctf_dtor (struct trace_file_writer *self)
243 struct ctf_trace_file_writer *writer
244 = (struct ctf_trace_file_writer *) self;
246 if (writer->tcs.metadata_fd != NULL)
247 fclose (writer->tcs.metadata_fd);
249 if (writer->tcs.datastream_fd != NULL)
250 fclose (writer->tcs.datastream_fd);
254 /* This is the implementation of trace_file_write_ops method
258 ctf_target_save (struct trace_file_writer *self,
261 /* Don't support save trace file to CTF format in the target. */
265 /* This is the implementation of trace_file_write_ops method
266 start. It creates the directory DIRNAME, metadata and datastream
270 ctf_start (struct trace_file_writer *self, const char *dirname)
273 struct cleanup *old_chain;
274 struct ctf_trace_file_writer *writer
275 = (struct ctf_trace_file_writer *) self;
278 /* Create DIRNAME. */
279 if (mkdir (dirname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
281 error (_("Unable to open directory '%s' for saving trace data (%s)"),
282 dirname, safe_strerror (errno));
284 memset (&writer->tcs, '\0', sizeof (writer->tcs));
286 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
287 old_chain = make_cleanup (xfree, file_name);
289 writer->tcs.metadata_fd = fopen (file_name, "w");
290 if (writer->tcs.metadata_fd == NULL)
291 error (_("Unable to open file '%s' for saving trace data (%s)"),
292 file_name, safe_strerror (errno));
293 do_cleanups (old_chain);
295 ctf_save_metadata_header (&writer->tcs);
297 file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
298 old_chain = make_cleanup (xfree, file_name);
299 writer->tcs.datastream_fd = fopen (file_name, "w");
300 if (writer->tcs.datastream_fd == NULL)
301 error (_("Unable to open file '%s' for saving trace data (%s)"),
302 file_name, safe_strerror (errno));
303 do_cleanups (old_chain);
306 /* This is the implementation of trace_file_write_ops method
307 write_header. Write the types of events on trace variable and
311 ctf_write_header (struct trace_file_writer *self)
313 struct ctf_trace_file_writer *writer
314 = (struct ctf_trace_file_writer *) self;
317 ctf_save_write_metadata (&writer->tcs, "\n");
318 ctf_save_write_metadata (&writer->tcs,
319 "event {\n\tname = \"memory\";\n\tid = %u;\n"
320 "\tfields := struct { \n"
321 "\t\tuint64_t address;\n"
322 "\t\tuint16_t length;\n"
323 "\t\tuint8_t contents[length];\n"
325 "};\n", CTF_EVENT_ID_MEMORY);
327 ctf_save_write_metadata (&writer->tcs, "\n");
328 ctf_save_write_metadata (&writer->tcs,
329 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
330 "\tfields := struct { \n"
331 "\t\tuint64_t val;\n"
332 "\t\tuint32_t num;\n"
334 "};\n", CTF_EVENT_ID_TSV);
336 ctf_save_write_metadata (&writer->tcs, "\n");
337 ctf_save_write_metadata (&writer->tcs,
338 "event {\n\tname = \"frame\";\n\tid = %u;\n"
339 "\tfields := struct { \n"
341 "};\n", CTF_EVENT_ID_FRAME);
343 gdb_assert (writer->tcs.content_size == 0);
344 gdb_assert (writer->tcs.packet_start == 0);
347 /* This is the implementation of trace_file_write_ops method
348 write_regblock_type. Write the type of register event in
352 ctf_write_regblock_type (struct trace_file_writer *self, int size)
354 struct ctf_trace_file_writer *writer
355 = (struct ctf_trace_file_writer *) self;
357 ctf_save_write_metadata (&writer->tcs, "\n");
359 ctf_save_write_metadata (&writer->tcs,
360 "event {\n\tname = \"register\";\n\tid = %u;\n"
361 "\tfields := struct { \n"
362 "\t\tascii contents[%d];\n"
365 CTF_EVENT_ID_REGISTER, size);
368 /* This is the implementation of trace_file_write_ops method
372 ctf_write_status (struct trace_file_writer *self,
373 struct trace_status *ts)
375 /* It is not supported yet to write trace status into CTF trace
379 /* This is the implementation of trace_file_write_ops method
380 write_uploaded_tsv. */
383 ctf_write_uploaded_tsv (struct trace_file_writer *self,
384 struct uploaded_tsv *tsv)
386 /* It is not supported yet to write uploaded trace variables
387 into CTF trace data. */
390 /* This is the implementation of trace_file_write_ops method
391 write_uploaded_tp. */
394 ctf_write_uploaded_tp (struct trace_file_writer *self,
395 struct uploaded_tp *tp)
397 /* It is not supported yet to write uploaded tracepoints
398 into CTF trace data. */
401 /* This is the implementation of trace_file_write_ops method
402 write_definition_end. */
405 ctf_write_definition_end (struct trace_file_writer *self)
407 /* Nothing to do for CTF. */
410 /* The minimal file size of data stream. It is required by
413 #define CTF_FILE_MIN_SIZE 4096
415 /* This is the implementation of trace_file_write_ops method
419 ctf_end (struct trace_file_writer *self)
421 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
423 gdb_assert (writer->tcs.content_size == 0);
424 /* The babeltrace requires or assumes that the size of datastream
425 file is greater than 4096 bytes. If we don't generate enough
426 packets and events, create a fake packet which has zero event,
427 to use up the space. */
428 if (writer->tcs.packet_start < CTF_FILE_MIN_SIZE)
434 ctf_save_write_uint32 (&writer->tcs, u32);
438 ctf_save_write_uint32 (&writer->tcs, u32);
442 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
443 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
445 u32 *= TARGET_CHAR_BIT;
446 ctf_save_write_uint32 (&writer->tcs, u32);
450 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
452 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
454 if (CTF_FILE_MIN_SIZE
455 > (writer->tcs.packet_start + writer->tcs.content_size))
459 /* Fake the content size to avoid assertion failure in
461 writer->tcs.content_size = (CTF_FILE_MIN_SIZE
462 - 1 - writer->tcs.packet_start);
463 ctf_save_fseek (&writer->tcs, CTF_FILE_MIN_SIZE - 1,
465 ctf_save_write (&writer->tcs, &b, 1);
470 /* This is the implementation of trace_frame_write_ops method
474 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
476 struct ctf_trace_file_writer *writer
477 = (struct ctf_trace_file_writer *) self;
478 uint32_t id = CTF_EVENT_ID_FRAME;
481 /* Step 1: Write packet context. */
484 ctf_save_write_uint32 (&writer->tcs, u32);
485 /* content_size and packet_size.. We still don't know the value,
487 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
488 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
489 /* Tracepoint number. */
490 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
492 /* Step 2: Write event "frame". */
494 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
497 /* This is the implementation of trace_frame_write_ops method
501 ctf_write_frame_r_block (struct trace_file_writer *self,
502 gdb_byte *buf, int32_t size)
504 struct ctf_trace_file_writer *writer
505 = (struct ctf_trace_file_writer *) self;
506 uint32_t id = CTF_EVENT_ID_REGISTER;
509 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
511 /* array contents. */
512 ctf_save_align_write (&writer->tcs, buf, size, 1);
515 /* This is the implementation of trace_frame_write_ops method
516 write_m_block_header. */
519 ctf_write_frame_m_block_header (struct trace_file_writer *self,
520 uint64_t addr, uint16_t length)
522 struct ctf_trace_file_writer *writer
523 = (struct ctf_trace_file_writer *) self;
524 uint32_t event_id = CTF_EVENT_ID_MEMORY;
527 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
530 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
533 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
536 /* This is the implementation of trace_frame_write_ops method
537 write_m_block_memory. */
540 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
541 gdb_byte *buf, uint16_t length)
543 struct ctf_trace_file_writer *writer
544 = (struct ctf_trace_file_writer *) self;
547 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
550 /* This is the implementation of trace_frame_write_ops method
554 ctf_write_frame_v_block (struct trace_file_writer *self,
555 int32_t num, uint64_t val)
557 struct ctf_trace_file_writer *writer
558 = (struct ctf_trace_file_writer *) self;
559 uint32_t id = CTF_EVENT_ID_TSV;
562 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
565 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
567 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
570 /* This is the implementation of trace_frame_write_ops method
574 ctf_write_frame_end (struct trace_file_writer *self)
576 struct ctf_trace_file_writer *writer
577 = (struct ctf_trace_file_writer *) self;
581 /* Write the content size to packet header. */
582 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
584 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
586 t = writer->tcs.content_size;
587 ctf_save_write_uint32 (&writer->tcs, u32);
589 /* Write the packet size. */
590 u32 += 4 * TARGET_CHAR_BIT;
591 ctf_save_write_uint32 (&writer->tcs, u32);
593 writer->tcs.content_size = t;
595 /* Write zero at the end of the packet. */
596 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
599 ctf_save_write_uint32 (&writer->tcs, u32);
600 writer->tcs.content_size = t;
602 ctf_save_next_packet (&writer->tcs);
605 /* Operations to write various types of trace frames into CTF
608 static const struct trace_frame_write_ops ctf_write_frame_ops =
610 ctf_write_frame_start,
611 ctf_write_frame_r_block,
612 ctf_write_frame_m_block_header,
613 ctf_write_frame_m_block_memory,
614 ctf_write_frame_v_block,
618 /* Operations to write trace buffers into CTF format. */
620 static const struct trace_file_write_ops ctf_write_ops =
626 ctf_write_regblock_type,
628 ctf_write_uploaded_tsv,
629 ctf_write_uploaded_tp,
630 ctf_write_definition_end,
632 &ctf_write_frame_ops,
636 /* Return a trace writer for CTF format. */
638 struct trace_file_writer *
639 ctf_trace_file_writer_new (void)
641 struct ctf_trace_file_writer *writer
642 = xmalloc (sizeof (struct ctf_trace_file_writer));
644 writer->base.ops = &ctf_write_ops;
646 return (struct trace_file_writer *) writer;