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 /* Get the byte order of the host and write CTF data in this byte
224 #define HOST_ENDIANNESS "be"
226 #define HOST_ENDIANNESS "le"
229 ctf_save_write_metadata (handler
, metadata_fmt
,
230 CTF_SAVE_MAJOR
, CTF_SAVE_MINOR
,
232 ctf_save_write_metadata (handler
, "\n");
235 /* CTF trace writer. */
237 struct ctf_trace_file_writer
239 struct trace_file_writer base
;
241 /* States related to writing CTF trace file. */
242 struct trace_write_handler tcs
;
245 /* This is the implementation of trace_file_write_ops method
249 ctf_dtor (struct trace_file_writer
*self
)
251 struct ctf_trace_file_writer
*writer
252 = (struct ctf_trace_file_writer
*) self
;
254 if (writer
->tcs
.metadata_fd
!= NULL
)
255 fclose (writer
->tcs
.metadata_fd
);
257 if (writer
->tcs
.datastream_fd
!= NULL
)
258 fclose (writer
->tcs
.datastream_fd
);
262 /* This is the implementation of trace_file_write_ops method
266 ctf_target_save (struct trace_file_writer
*self
,
269 /* Don't support save trace file to CTF format in the target. */
273 /* This is the implementation of trace_file_write_ops method
274 start. It creates the directory DIRNAME, metadata and datastream
278 ctf_start (struct trace_file_writer
*self
, const char *dirname
)
281 struct cleanup
*old_chain
;
282 struct ctf_trace_file_writer
*writer
283 = (struct ctf_trace_file_writer
*) self
;
286 /* Create DIRNAME. */
287 if (mkdir (dirname
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)
289 error (_("Unable to open directory '%s' for saving trace data (%s)"),
290 dirname
, safe_strerror (errno
));
292 memset (&writer
->tcs
, '\0', sizeof (writer
->tcs
));
294 file_name
= xstrprintf ("%s/%s", dirname
, CTF_METADATA_NAME
);
295 old_chain
= make_cleanup (xfree
, file_name
);
297 writer
->tcs
.metadata_fd
= fopen (file_name
, "w");
298 if (writer
->tcs
.metadata_fd
== NULL
)
299 error (_("Unable to open file '%s' for saving trace data (%s)"),
300 file_name
, safe_strerror (errno
));
301 do_cleanups (old_chain
);
303 ctf_save_metadata_header (&writer
->tcs
);
305 file_name
= xstrprintf ("%s/%s", dirname
, CTF_DATASTREAM_NAME
);
306 old_chain
= make_cleanup (xfree
, file_name
);
307 writer
->tcs
.datastream_fd
= fopen (file_name
, "w");
308 if (writer
->tcs
.datastream_fd
== NULL
)
309 error (_("Unable to open file '%s' for saving trace data (%s)"),
310 file_name
, safe_strerror (errno
));
311 do_cleanups (old_chain
);
314 /* This is the implementation of trace_file_write_ops method
315 write_header. Write the types of events on trace variable and
319 ctf_write_header (struct trace_file_writer
*self
)
321 struct ctf_trace_file_writer
*writer
322 = (struct ctf_trace_file_writer
*) self
;
325 ctf_save_write_metadata (&writer
->tcs
, "\n");
326 ctf_save_write_metadata (&writer
->tcs
,
327 "event {\n\tname = \"memory\";\n\tid = %u;\n"
328 "\tfields := struct { \n"
329 "\t\tuint64_t address;\n"
330 "\t\tuint16_t length;\n"
331 "\t\tuint8_t contents[length];\n"
333 "};\n", CTF_EVENT_ID_MEMORY
);
335 ctf_save_write_metadata (&writer
->tcs
, "\n");
336 ctf_save_write_metadata (&writer
->tcs
,
337 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
338 "\tfields := struct { \n"
339 "\t\tuint64_t val;\n"
340 "\t\tuint32_t num;\n"
342 "};\n", CTF_EVENT_ID_TSV
);
344 ctf_save_write_metadata (&writer
->tcs
, "\n");
345 ctf_save_write_metadata (&writer
->tcs
,
346 "event {\n\tname = \"frame\";\n\tid = %u;\n"
347 "\tfields := struct { \n"
349 "};\n", CTF_EVENT_ID_FRAME
);
351 gdb_assert (writer
->tcs
.content_size
== 0);
352 gdb_assert (writer
->tcs
.packet_start
== 0);
355 /* This is the implementation of trace_file_write_ops method
356 write_regblock_type. Write the type of register event in
360 ctf_write_regblock_type (struct trace_file_writer
*self
, int size
)
362 struct ctf_trace_file_writer
*writer
363 = (struct ctf_trace_file_writer
*) self
;
365 ctf_save_write_metadata (&writer
->tcs
, "\n");
367 ctf_save_write_metadata (&writer
->tcs
,
368 "event {\n\tname = \"register\";\n\tid = %u;\n"
369 "\tfields := struct { \n"
370 "\t\tascii contents[%d];\n"
373 CTF_EVENT_ID_REGISTER
, size
);
376 /* This is the implementation of trace_file_write_ops method
380 ctf_write_status (struct trace_file_writer
*self
,
381 struct trace_status
*ts
)
383 /* It is not supported yet to write trace status into CTF trace
387 /* This is the implementation of trace_file_write_ops method
388 write_uploaded_tsv. */
391 ctf_write_uploaded_tsv (struct trace_file_writer
*self
,
392 struct uploaded_tsv
*tsv
)
394 /* It is not supported yet to write uploaded trace variables
395 into CTF trace data. */
398 /* This is the implementation of trace_file_write_ops method
399 write_uploaded_tp. */
402 ctf_write_uploaded_tp (struct trace_file_writer
*self
,
403 struct uploaded_tp
*tp
)
405 /* It is not supported yet to write uploaded tracepoints
406 into CTF trace data. */
409 /* This is the implementation of trace_file_write_ops method
410 write_definition_end. */
413 ctf_write_definition_end (struct trace_file_writer
*self
)
415 /* Nothing to do for CTF. */
418 /* The minimal file size of data stream. It is required by
421 #define CTF_FILE_MIN_SIZE 4096
423 /* This is the implementation of trace_file_write_ops method
427 ctf_end (struct trace_file_writer
*self
)
429 struct ctf_trace_file_writer
*writer
= (struct ctf_trace_file_writer
*) self
;
431 gdb_assert (writer
->tcs
.content_size
== 0);
432 /* The babeltrace requires or assumes that the size of datastream
433 file is greater than 4096 bytes. If we don't generate enough
434 packets and events, create a fake packet which has zero event,
435 to use up the space. */
436 if (writer
->tcs
.packet_start
< CTF_FILE_MIN_SIZE
)
442 ctf_save_write_uint32 (&writer
->tcs
, u32
);
446 ctf_save_write_uint32 (&writer
->tcs
, u32
);
450 if (writer
->tcs
.packet_start
+ u32
< CTF_FILE_MIN_SIZE
)
451 u32
= CTF_FILE_MIN_SIZE
- writer
->tcs
.packet_start
;
453 u32
*= TARGET_CHAR_BIT
;
454 ctf_save_write_uint32 (&writer
->tcs
, u32
);
458 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &u32
, 2);
460 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
462 if (CTF_FILE_MIN_SIZE
463 > (writer
->tcs
.packet_start
+ writer
->tcs
.content_size
))
467 /* Fake the content size to avoid assertion failure in
469 writer
->tcs
.content_size
= (CTF_FILE_MIN_SIZE
470 - 1 - writer
->tcs
.packet_start
);
471 ctf_save_fseek (&writer
->tcs
, CTF_FILE_MIN_SIZE
- 1,
473 ctf_save_write (&writer
->tcs
, &b
, 1);
478 /* This is the implementation of trace_frame_write_ops method
482 ctf_write_frame_start (struct trace_file_writer
*self
, uint16_t tpnum
)
484 struct ctf_trace_file_writer
*writer
485 = (struct ctf_trace_file_writer
*) self
;
486 uint32_t id
= CTF_EVENT_ID_FRAME
;
489 /* Step 1: Write packet context. */
492 ctf_save_write_uint32 (&writer
->tcs
, u32
);
493 /* content_size and packet_size.. We still don't know the value,
495 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
496 ctf_save_fseek (&writer
->tcs
, 4, SEEK_CUR
);
497 /* Tracepoint number. */
498 ctf_save_write (&writer
->tcs
, (gdb_byte
*) &tpnum
, 2);
500 /* Step 2: Write event "frame". */
502 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
505 /* This is the implementation of trace_frame_write_ops method
509 ctf_write_frame_r_block (struct trace_file_writer
*self
,
510 gdb_byte
*buf
, int32_t size
)
512 struct ctf_trace_file_writer
*writer
513 = (struct ctf_trace_file_writer
*) self
;
514 uint32_t id
= CTF_EVENT_ID_REGISTER
;
517 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
519 /* array contents. */
520 ctf_save_align_write (&writer
->tcs
, buf
, size
, 1);
523 /* This is the implementation of trace_frame_write_ops method
524 write_m_block_header. */
527 ctf_write_frame_m_block_header (struct trace_file_writer
*self
,
528 uint64_t addr
, uint16_t length
)
530 struct ctf_trace_file_writer
*writer
531 = (struct ctf_trace_file_writer
*) self
;
532 uint32_t event_id
= CTF_EVENT_ID_MEMORY
;
535 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &event_id
, 4, 4);
538 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &addr
, 8, 8);
541 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &length
, 2, 2);
544 /* This is the implementation of trace_frame_write_ops method
545 write_m_block_memory. */
548 ctf_write_frame_m_block_memory (struct trace_file_writer
*self
,
549 gdb_byte
*buf
, uint16_t length
)
551 struct ctf_trace_file_writer
*writer
552 = (struct ctf_trace_file_writer
*) self
;
555 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) buf
, length
, 1);
558 /* This is the implementation of trace_frame_write_ops method
562 ctf_write_frame_v_block (struct trace_file_writer
*self
,
563 int32_t num
, uint64_t val
)
565 struct ctf_trace_file_writer
*writer
566 = (struct ctf_trace_file_writer
*) self
;
567 uint32_t id
= CTF_EVENT_ID_TSV
;
570 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &id
, 4, 4);
573 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &val
, 8, 8);
575 ctf_save_align_write (&writer
->tcs
, (gdb_byte
*) &num
, 4, 4);
578 /* This is the implementation of trace_frame_write_ops method
582 ctf_write_frame_end (struct trace_file_writer
*self
)
584 struct ctf_trace_file_writer
*writer
585 = (struct ctf_trace_file_writer
*) self
;
589 /* Write the content size to packet header. */
590 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ 4,
592 u32
= writer
->tcs
.content_size
* TARGET_CHAR_BIT
;
594 t
= writer
->tcs
.content_size
;
595 ctf_save_write_uint32 (&writer
->tcs
, u32
);
597 /* Write the packet size. */
598 u32
+= 4 * TARGET_CHAR_BIT
;
599 ctf_save_write_uint32 (&writer
->tcs
, u32
);
601 writer
->tcs
.content_size
= t
;
603 /* Write zero at the end of the packet. */
604 ctf_save_fseek (&writer
->tcs
, writer
->tcs
.packet_start
+ t
,
607 ctf_save_write_uint32 (&writer
->tcs
, u32
);
608 writer
->tcs
.content_size
= t
;
610 ctf_save_next_packet (&writer
->tcs
);
613 /* Operations to write various types of trace frames into CTF
616 static const struct trace_frame_write_ops ctf_write_frame_ops
=
618 ctf_write_frame_start
,
619 ctf_write_frame_r_block
,
620 ctf_write_frame_m_block_header
,
621 ctf_write_frame_m_block_memory
,
622 ctf_write_frame_v_block
,
626 /* Operations to write trace buffers into CTF format. */
628 static const struct trace_file_write_ops ctf_write_ops
=
634 ctf_write_regblock_type
,
636 ctf_write_uploaded_tsv
,
637 ctf_write_uploaded_tp
,
638 ctf_write_definition_end
,
640 &ctf_write_frame_ops
,
644 /* Return a trace writer for CTF format. */
646 struct trace_file_writer
*
647 ctf_trace_file_writer_new (void)
649 struct ctf_trace_file_writer
*writer
650 = xmalloc (sizeof (struct ctf_trace_file_writer
));
652 writer
->base
.ops
= &ctf_write_ops
;
654 return (struct trace_file_writer
*) writer
;