]> 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
27 #include <ctype.h>
28
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:
32
33 1. The length (in bytes) of register cache. Event "register" will
34 be defined in metadata, which includes the length.
35
36 2. Trace status. Not implemented yet in CTF writer.
37
38 3. Uploaded trace variables and tracepoints. Not implemented yet
39 in CTF writer.
40
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
46 context.
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
52 "frame". */
53
54 #define CTF_MAGIC 0xC1FC1FC1
55 #define CTF_SAVE_MAJOR 1
56 #define CTF_SAVE_MINOR 8
57
58 #define CTF_METADATA_NAME "metadata"
59 #define CTF_DATASTREAM_NAME "datastream"
60
61 /* Reserved event id. */
62
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
67
68 /* The state kept while writing the CTF datastream file. */
69
70 struct trace_write_handler
71 {
72 /* File descriptor of metadata. */
73 FILE *metadata_fd;
74 /* File descriptor of traceframes. */
75 FILE *datastream_fd;
76
77 /* This is the content size of the current packet. */
78 size_t content_size;
79
80 /* This is the start offset of current packet. */
81 long packet_start;
82 };
83
84 /* Write metadata in FORMAT. */
85
86 static void
87 ctf_save_write_metadata (struct trace_write_handler *handler,
88 const char *format, ...)
89 {
90 va_list args;
91
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));
96 va_end (args);
97 }
98
99 /* Write BUF of length SIZE to datastream file represented by
100 HANDLER. */
101
102 static int
103 ctf_save_write (struct trace_write_handler *handler,
104 const gdb_byte *buf, size_t size)
105 {
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));
109
110 handler->content_size += size;
111
112 return 0;
113 }
114
115 /* Write a unsigned 32-bit integer to datastream file represented by
116 HANDLER. */
117
118 #define ctf_save_write_uint32(HANDLER, U32) \
119 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
120
121 /* Set datastream file position. Update HANDLER->content_size
122 if WHENCE is SEEK_CUR. */
123
124 static int
125 ctf_save_fseek (struct trace_write_handler *handler, long offset,
126 int whence)
127 {
128 gdb_assert (whence != SEEK_END);
129 gdb_assert (whence != SEEK_SET
130 || offset <= handler->content_size + handler->packet_start);
131
132 if (fseek (handler->datastream_fd, offset, whence))
133 error (_("Unable to seek file for saving trace data (%s)"),
134 safe_strerror (errno));
135
136 if (whence == SEEK_CUR)
137 handler->content_size += offset;
138
139 return 0;
140 }
141
142 /* Change the datastream file position to align on ALIGN_SIZE,
143 and write BUF to datastream file. The size of BUF is SIZE. */
144
145 static int
146 ctf_save_align_write (struct trace_write_handler *handler,
147 const gdb_byte *buf,
148 size_t size, size_t align_size)
149 {
150 long offset
151 = (align_up (handler->content_size, align_size)
152 - handler->content_size);
153
154 if (ctf_save_fseek (handler, offset, SEEK_CUR))
155 return -1;
156
157 if (ctf_save_write (handler, buf, size))
158 return -1;
159
160 return 0;
161 }
162
163 /* Write events to next new packet. */
164
165 static void
166 ctf_save_next_packet (struct trace_write_handler *handler)
167 {
168 handler->packet_start += (handler->content_size + 4);
169 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
170 handler->content_size = 0;
171 }
172
173 /* Write the CTF metadata header. */
174
175 static void
176 ctf_save_metadata_header (struct trace_write_handler *handler)
177 {
178 const char metadata_fmt[] =
179 "\ntrace {\n"
180 " major = %u;\n"
181 " minor = %u;\n"
182 " byte_order = %s;\n" /* be or le */
183 " packet.header := struct {\n"
184 " uint32_t magic;\n"
185 " };\n"
186 "};\n"
187 "\n"
188 "stream {\n"
189 " packet.context := struct {\n"
190 " uint32_t content_size;\n"
191 " uint32_t packet_size;\n"
192 " uint16_t tpnum;\n"
193 " };\n"
194 " event.header := struct {\n"
195 " uint32_t id;\n"
196 " };\n"
197 "};\n";
198
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;}"
204 " := ascii;\n");
205 ctf_save_write_metadata (handler,
206 "typealias integer { size = 8; align = 8; "
207 "signed = false; }"
208 " := uint8_t;\n");
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;}"
218 " := uint64_t;\n");
219 ctf_save_write_metadata (handler, "\n");
220
221 /* Get the byte order of the host and write CTF data in this byte
222 order. */
223 #if WORDS_BIGENDIAN
224 #define HOST_ENDIANNESS "be"
225 #else
226 #define HOST_ENDIANNESS "le"
227 #endif
228
229 ctf_save_write_metadata (handler, metadata_fmt,
230 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
231 HOST_ENDIANNESS);
232 ctf_save_write_metadata (handler, "\n");
233 }
234
235 /* CTF trace writer. */
236
237 struct ctf_trace_file_writer
238 {
239 struct trace_file_writer base;
240
241 /* States related to writing CTF trace file. */
242 struct trace_write_handler tcs;
243 };
244
245 /* This is the implementation of trace_file_write_ops method
246 dtor. */
247
248 static void
249 ctf_dtor (struct trace_file_writer *self)
250 {
251 struct ctf_trace_file_writer *writer
252 = (struct ctf_trace_file_writer *) self;
253
254 if (writer->tcs.metadata_fd != NULL)
255 fclose (writer->tcs.metadata_fd);
256
257 if (writer->tcs.datastream_fd != NULL)
258 fclose (writer->tcs.datastream_fd);
259
260 }
261
262 /* This is the implementation of trace_file_write_ops method
263 target_save. */
264
265 static int
266 ctf_target_save (struct trace_file_writer *self,
267 const char *dirname)
268 {
269 /* Don't support save trace file to CTF format in the target. */
270 return 0;
271 }
272
273 /* This is the implementation of trace_file_write_ops method
274 start. It creates the directory DIRNAME, metadata and datastream
275 in the directory. */
276
277 static void
278 ctf_start (struct trace_file_writer *self, const char *dirname)
279 {
280 char *file_name;
281 struct cleanup *old_chain;
282 struct ctf_trace_file_writer *writer
283 = (struct ctf_trace_file_writer *) self;
284 int i;
285
286 /* Create DIRNAME. */
287 if (mkdir (dirname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
288 && errno != EEXIST)
289 error (_("Unable to open directory '%s' for saving trace data (%s)"),
290 dirname, safe_strerror (errno));
291
292 memset (&writer->tcs, '\0', sizeof (writer->tcs));
293
294 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
295 old_chain = make_cleanup (xfree, file_name);
296
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);
302
303 ctf_save_metadata_header (&writer->tcs);
304
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);
312 }
313
314 /* This is the implementation of trace_file_write_ops method
315 write_header. Write the types of events on trace variable and
316 frame. */
317
318 static void
319 ctf_write_header (struct trace_file_writer *self)
320 {
321 struct ctf_trace_file_writer *writer
322 = (struct ctf_trace_file_writer *) self;
323
324
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"
332 "\t};\n"
333 "};\n", CTF_EVENT_ID_MEMORY);
334
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"
341 "\t};\n"
342 "};\n", CTF_EVENT_ID_TSV);
343
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"
348 "\t};\n"
349 "};\n", CTF_EVENT_ID_FRAME);
350
351 gdb_assert (writer->tcs.content_size == 0);
352 gdb_assert (writer->tcs.packet_start == 0);
353 }
354
355 /* This is the implementation of trace_file_write_ops method
356 write_regblock_type. Write the type of register event in
357 metadata. */
358
359 static void
360 ctf_write_regblock_type (struct trace_file_writer *self, int size)
361 {
362 struct ctf_trace_file_writer *writer
363 = (struct ctf_trace_file_writer *) self;
364
365 ctf_save_write_metadata (&writer->tcs, "\n");
366
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"
371 "\t};\n"
372 "};\n",
373 CTF_EVENT_ID_REGISTER, size);
374 }
375
376 /* This is the implementation of trace_file_write_ops method
377 write_status. */
378
379 static void
380 ctf_write_status (struct trace_file_writer *self,
381 struct trace_status *ts)
382 {
383 /* It is not supported yet to write trace status into CTF trace
384 data. */
385 }
386
387 /* This is the implementation of trace_file_write_ops method
388 write_uploaded_tsv. */
389
390 static void
391 ctf_write_uploaded_tsv (struct trace_file_writer *self,
392 struct uploaded_tsv *tsv)
393 {
394 /* It is not supported yet to write uploaded trace variables
395 into CTF trace data. */
396 }
397
398 /* This is the implementation of trace_file_write_ops method
399 write_uploaded_tp. */
400
401 static void
402 ctf_write_uploaded_tp (struct trace_file_writer *self,
403 struct uploaded_tp *tp)
404 {
405 /* It is not supported yet to write uploaded tracepoints
406 into CTF trace data. */
407 }
408
409 /* This is the implementation of trace_file_write_ops method
410 write_definition_end. */
411
412 static void
413 ctf_write_definition_end (struct trace_file_writer *self)
414 {
415 /* Nothing to do for CTF. */
416 }
417
418 /* The minimal file size of data stream. It is required by
419 babeltrace. */
420
421 #define CTF_FILE_MIN_SIZE 4096
422
423 /* This is the implementation of trace_file_write_ops method
424 end. */
425
426 static void
427 ctf_end (struct trace_file_writer *self)
428 {
429 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
430
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)
437 {
438 uint32_t u32;
439
440 /* magic. */
441 u32 = CTF_MAGIC;
442 ctf_save_write_uint32 (&writer->tcs, u32);
443
444 /* content_size. */
445 u32 = 0;
446 ctf_save_write_uint32 (&writer->tcs, u32);
447
448 /* packet_size. */
449 u32 = 12;
450 if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE)
451 u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start;
452
453 u32 *= TARGET_CHAR_BIT;
454 ctf_save_write_uint32 (&writer->tcs, u32);
455
456 /* tpnum. */
457 u32 = 0;
458 ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2);
459
460 /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less
461 than that. */
462 if (CTF_FILE_MIN_SIZE
463 > (writer->tcs.packet_start + writer->tcs.content_size))
464 {
465 gdb_byte b = 0;
466
467 /* Fake the content size to avoid assertion failure in
468 ctf_save_fseek. */
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,
472 SEEK_SET);
473 ctf_save_write (&writer->tcs, &b, 1);
474 }
475 }
476 }
477
478 /* This is the implementation of trace_frame_write_ops method
479 start. */
480
481 static void
482 ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
483 {
484 struct ctf_trace_file_writer *writer
485 = (struct ctf_trace_file_writer *) self;
486 uint32_t id = CTF_EVENT_ID_FRAME;
487 uint32_t u32;
488
489 /* Step 1: Write packet context. */
490 /* magic. */
491 u32 = CTF_MAGIC;
492 ctf_save_write_uint32 (&writer->tcs, u32);
493 /* content_size and packet_size.. We still don't know the value,
494 write it later. */
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);
499
500 /* Step 2: Write event "frame". */
501 /* Event Id. */
502 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
503 }
504
505 /* This is the implementation of trace_frame_write_ops method
506 write_r_block. */
507
508 static void
509 ctf_write_frame_r_block (struct trace_file_writer *self,
510 gdb_byte *buf, int32_t size)
511 {
512 struct ctf_trace_file_writer *writer
513 = (struct ctf_trace_file_writer *) self;
514 uint32_t id = CTF_EVENT_ID_REGISTER;
515
516 /* Event Id. */
517 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
518
519 /* array contents. */
520 ctf_save_align_write (&writer->tcs, buf, size, 1);
521 }
522
523 /* This is the implementation of trace_frame_write_ops method
524 write_m_block_header. */
525
526 static void
527 ctf_write_frame_m_block_header (struct trace_file_writer *self,
528 uint64_t addr, uint16_t length)
529 {
530 struct ctf_trace_file_writer *writer
531 = (struct ctf_trace_file_writer *) self;
532 uint32_t event_id = CTF_EVENT_ID_MEMORY;
533
534 /* Event Id. */
535 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
536
537 /* Address. */
538 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
539
540 /* Length. */
541 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
542 }
543
544 /* This is the implementation of trace_frame_write_ops method
545 write_m_block_memory. */
546
547 static void
548 ctf_write_frame_m_block_memory (struct trace_file_writer *self,
549 gdb_byte *buf, uint16_t length)
550 {
551 struct ctf_trace_file_writer *writer
552 = (struct ctf_trace_file_writer *) self;
553
554 /* Contents. */
555 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
556 }
557
558 /* This is the implementation of trace_frame_write_ops method
559 write_v_block. */
560
561 static void
562 ctf_write_frame_v_block (struct trace_file_writer *self,
563 int32_t num, uint64_t val)
564 {
565 struct ctf_trace_file_writer *writer
566 = (struct ctf_trace_file_writer *) self;
567 uint32_t id = CTF_EVENT_ID_TSV;
568
569 /* Event Id. */
570 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
571
572 /* val. */
573 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
574 /* num. */
575 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
576 }
577
578 /* This is the implementation of trace_frame_write_ops method
579 end. */
580
581 static void
582 ctf_write_frame_end (struct trace_file_writer *self)
583 {
584 struct ctf_trace_file_writer *writer
585 = (struct ctf_trace_file_writer *) self;
586 uint32_t u32;
587 uint32_t t;
588
589 /* Write the content size to packet header. */
590 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
591 SEEK_SET);
592 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
593
594 t = writer->tcs.content_size;
595 ctf_save_write_uint32 (&writer->tcs, u32);
596
597 /* Write the packet size. */
598 u32 += 4 * TARGET_CHAR_BIT;
599 ctf_save_write_uint32 (&writer->tcs, u32);
600
601 writer->tcs.content_size = t;
602
603 /* Write zero at the end of the packet. */
604 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
605 SEEK_SET);
606 u32 = 0;
607 ctf_save_write_uint32 (&writer->tcs, u32);
608 writer->tcs.content_size = t;
609
610 ctf_save_next_packet (&writer->tcs);
611 }
612
613 /* Operations to write various types of trace frames into CTF
614 format. */
615
616 static const struct trace_frame_write_ops ctf_write_frame_ops =
617 {
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,
623 ctf_write_frame_end,
624 };
625
626 /* Operations to write trace buffers into CTF format. */
627
628 static const struct trace_file_write_ops ctf_write_ops =
629 {
630 ctf_dtor,
631 ctf_target_save,
632 ctf_start,
633 ctf_write_header,
634 ctf_write_regblock_type,
635 ctf_write_status,
636 ctf_write_uploaded_tsv,
637 ctf_write_uploaded_tp,
638 ctf_write_definition_end,
639 NULL,
640 &ctf_write_frame_ops,
641 ctf_end,
642 };
643
644 /* Return a trace writer for CTF format. */
645
646 struct trace_file_writer *
647 ctf_trace_file_writer_new (void)
648 {
649 struct ctf_trace_file_writer *writer
650 = xmalloc (sizeof (struct ctf_trace_file_writer));
651
652 writer->base.ops = &ctf_write_ops;
653
654 return (struct trace_file_writer *) writer;
655 }