testsuite: Fix timeout issues during print of vla-arrays.
[deliverable/binutils-gdb.git] / gdb / ctf.c
CommitLineData
d0353e76
YQ
1/* CTF format support.
2
618f726f 3 Copyright (C) 2012-2016 Free Software Foundation, Inc.
d0353e76
YQ
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"
53ce3c39 26#include <sys/stat.h>
393fd4c3 27#include "exec.h"
da9160e4 28#include "completer.h"
de7b2893
YQ
29#include "inferior.h"
30#include "gdbthread.h"
7951c4eb 31#include "tracefile.h"
d0353e76
YQ
32
33#include <ctype.h>
34
35/* GDB saves trace buffers and other information (such as trace
36 status) got from the remote target into Common Trace Format (CTF).
37 The following types of information are expected to save in CTF:
38
39 1. The length (in bytes) of register cache. Event "register" will
40 be defined in metadata, which includes the length.
41
393fd4c3
YQ
42 2. Trace status. Event "status" is defined in metadata, which
43 includes all aspects of trace status.
d0353e76 44
393fd4c3
YQ
45 3. Uploaded trace variables. Event "tsv_def" is defined in
46 metadata, which is about all aspects of a uploaded trace variable.
47 Uploaded tracepoints. Event "tp_def" is defined in meta, which
48 is about all aspects of an uploaded tracepoint. Note that the
49 "sequence" (a CTF type, which is a dynamically-sized array.) is
50 used for "actions" "step_actions" and "cmd_strings".
d0353e76
YQ
51
52 4. Trace frames. Each trace frame is composed by several blocks
53 of different types ('R', 'M', 'V'). One trace frame is saved in
54 one CTF packet and the blocks of this frame are saved as events.
55 4.1: The trace frame related information (such as the number of
56 tracepoint associated with this frame) is saved in the packet
57 context.
58 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
59 "register" and "tsv" respectively.
60 4.3: When iterating over events, babeltrace can't tell iterator
61 goes to a new packet, so we need a marker or anchor to tell GDB
62 that iterator goes into a new packet or frame. We define event
63 "frame". */
64
65#define CTF_MAGIC 0xC1FC1FC1
66#define CTF_SAVE_MAJOR 1
67#define CTF_SAVE_MINOR 8
68
69#define CTF_METADATA_NAME "metadata"
70#define CTF_DATASTREAM_NAME "datastream"
71
72/* Reserved event id. */
73
74#define CTF_EVENT_ID_REGISTER 0
75#define CTF_EVENT_ID_TSV 1
76#define CTF_EVENT_ID_MEMORY 2
77#define CTF_EVENT_ID_FRAME 3
393fd4c3
YQ
78#define CTF_EVENT_ID_STATUS 4
79#define CTF_EVENT_ID_TSV_DEF 5
80#define CTF_EVENT_ID_TP_DEF 6
d0353e76 81
de7b2893
YQ
82#define CTF_PID (2)
83
d0353e76
YQ
84/* The state kept while writing the CTF datastream file. */
85
86struct trace_write_handler
87{
88 /* File descriptor of metadata. */
89 FILE *metadata_fd;
90 /* File descriptor of traceframes. */
91 FILE *datastream_fd;
92
93 /* This is the content size of the current packet. */
94 size_t content_size;
95
96 /* This is the start offset of current packet. */
97 long packet_start;
98};
99
100/* Write metadata in FORMAT. */
101
77b64a49
PA
102static void
103ctf_save_write_metadata (struct trace_write_handler *handler,
104 const char *format, ...)
105 ATTRIBUTE_PRINTF (2, 3);
106
d0353e76
YQ
107static void
108ctf_save_write_metadata (struct trace_write_handler *handler,
109 const char *format, ...)
110{
111 va_list args;
112
113 va_start (args, format);
114 if (vfprintf (handler->metadata_fd, format, args) < 0)
115 error (_("Unable to write metadata file (%s)"),
116 safe_strerror (errno));
117 va_end (args);
118}
119
120/* Write BUF of length SIZE to datastream file represented by
121 HANDLER. */
122
123static int
124ctf_save_write (struct trace_write_handler *handler,
125 const gdb_byte *buf, size_t size)
126{
127 if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
128 error (_("Unable to write file for saving trace data (%s)"),
129 safe_strerror (errno));
130
131 handler->content_size += size;
132
133 return 0;
134}
135
136/* Write a unsigned 32-bit integer to datastream file represented by
137 HANDLER. */
138
139#define ctf_save_write_uint32(HANDLER, U32) \
140 ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
141
393fd4c3
YQ
142/* Write a signed 32-bit integer to datastream file represented by
143 HANDLER. */
144
145#define ctf_save_write_int32(HANDLER, INT32) \
146 ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
147
d0353e76
YQ
148/* Set datastream file position. Update HANDLER->content_size
149 if WHENCE is SEEK_CUR. */
150
151static int
152ctf_save_fseek (struct trace_write_handler *handler, long offset,
153 int whence)
154{
155 gdb_assert (whence != SEEK_END);
156 gdb_assert (whence != SEEK_SET
157 || offset <= handler->content_size + handler->packet_start);
158
159 if (fseek (handler->datastream_fd, offset, whence))
160 error (_("Unable to seek file for saving trace data (%s)"),
161 safe_strerror (errno));
162
163 if (whence == SEEK_CUR)
164 handler->content_size += offset;
165
166 return 0;
167}
168
169/* Change the datastream file position to align on ALIGN_SIZE,
170 and write BUF to datastream file. The size of BUF is SIZE. */
171
172static int
173ctf_save_align_write (struct trace_write_handler *handler,
174 const gdb_byte *buf,
175 size_t size, size_t align_size)
176{
177 long offset
178 = (align_up (handler->content_size, align_size)
179 - handler->content_size);
180
181 if (ctf_save_fseek (handler, offset, SEEK_CUR))
182 return -1;
183
184 if (ctf_save_write (handler, buf, size))
185 return -1;
186
187 return 0;
188}
189
190/* Write events to next new packet. */
191
192static void
193ctf_save_next_packet (struct trace_write_handler *handler)
194{
195 handler->packet_start += (handler->content_size + 4);
196 ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
197 handler->content_size = 0;
198}
199
200/* Write the CTF metadata header. */
201
202static void
203ctf_save_metadata_header (struct trace_write_handler *handler)
204{
205 const char metadata_fmt[] =
206 "\ntrace {\n"
207 " major = %u;\n"
208 " minor = %u;\n"
209 " byte_order = %s;\n" /* be or le */
210 " packet.header := struct {\n"
211 " uint32_t magic;\n"
212 " };\n"
213 "};\n"
214 "\n"
215 "stream {\n"
216 " packet.context := struct {\n"
217 " uint32_t content_size;\n"
218 " uint32_t packet_size;\n"
219 " uint16_t tpnum;\n"
220 " };\n"
221 " event.header := struct {\n"
222 " uint32_t id;\n"
223 " };\n"
224 "};\n";
225
226 ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
227 CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
228 ctf_save_write_metadata (handler,
229 "typealias integer { size = 8; align = 8; "
230 "signed = false; encoding = ascii;}"
231 " := ascii;\n");
232 ctf_save_write_metadata (handler,
233 "typealias integer { size = 8; align = 8; "
234 "signed = false; }"
235 " := uint8_t;\n");
236 ctf_save_write_metadata (handler,
237 "typealias integer { size = 16; align = 16;"
238 "signed = false; } := uint16_t;\n");
239 ctf_save_write_metadata (handler,
240 "typealias integer { size = 32; align = 32;"
241 "signed = false; } := uint32_t;\n");
242 ctf_save_write_metadata (handler,
243 "typealias integer { size = 64; align = 64;"
244 "signed = false; base = hex;}"
245 " := uint64_t;\n");
393fd4c3
YQ
246 ctf_save_write_metadata (handler,
247 "typealias integer { size = 32; align = 32;"
248 "signed = true; } := int32_t;\n");
249 ctf_save_write_metadata (handler,
250 "typealias integer { size = 64; align = 64;"
251 "signed = true; } := int64_t;\n");
252 ctf_save_write_metadata (handler,
253 "typealias string { encoding = ascii;"
254 " } := chars;\n");
d0353e76
YQ
255 ctf_save_write_metadata (handler, "\n");
256
8249a5a9
YQ
257 /* Get the byte order of the host and write CTF data in this byte
258 order. */
259#if WORDS_BIGENDIAN
260#define HOST_ENDIANNESS "be"
261#else
262#define HOST_ENDIANNESS "le"
263#endif
264
d0353e76
YQ
265 ctf_save_write_metadata (handler, metadata_fmt,
266 CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
8249a5a9 267 HOST_ENDIANNESS);
d0353e76
YQ
268 ctf_save_write_metadata (handler, "\n");
269}
270
271/* CTF trace writer. */
272
273struct ctf_trace_file_writer
274{
275 struct trace_file_writer base;
276
277 /* States related to writing CTF trace file. */
278 struct trace_write_handler tcs;
279};
280
281/* This is the implementation of trace_file_write_ops method
282 dtor. */
283
284static void
285ctf_dtor (struct trace_file_writer *self)
286{
287 struct ctf_trace_file_writer *writer
288 = (struct ctf_trace_file_writer *) self;
289
290 if (writer->tcs.metadata_fd != NULL)
291 fclose (writer->tcs.metadata_fd);
292
293 if (writer->tcs.datastream_fd != NULL)
294 fclose (writer->tcs.datastream_fd);
295
296}
297
298/* This is the implementation of trace_file_write_ops method
299 target_save. */
300
301static int
302ctf_target_save (struct trace_file_writer *self,
303 const char *dirname)
304{
305 /* Don't support save trace file to CTF format in the target. */
306 return 0;
307}
308
af307d6a 309#ifdef USE_WIN32API
a4341769
YQ
310#undef mkdir
311#define mkdir(pathname, mode) mkdir (pathname)
af307d6a
YQ
312#endif
313
d0353e76
YQ
314/* This is the implementation of trace_file_write_ops method
315 start. It creates the directory DIRNAME, metadata and datastream
316 in the directory. */
317
318static void
319ctf_start (struct trace_file_writer *self, const char *dirname)
320{
321 char *file_name;
322 struct cleanup *old_chain;
323 struct ctf_trace_file_writer *writer
324 = (struct ctf_trace_file_writer *) self;
325 int i;
840207d8 326 mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH;
d0353e76
YQ
327
328 /* Create DIRNAME. */
af307d6a 329 if (mkdir (dirname, hmode) && errno != EEXIST)
d0353e76
YQ
330 error (_("Unable to open directory '%s' for saving trace data (%s)"),
331 dirname, safe_strerror (errno));
332
333 memset (&writer->tcs, '\0', sizeof (writer->tcs));
334
335 file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
336 old_chain = make_cleanup (xfree, file_name);
337
338 writer->tcs.metadata_fd = fopen (file_name, "w");
339 if (writer->tcs.metadata_fd == NULL)
340 error (_("Unable to open file '%s' for saving trace data (%s)"),
341 file_name, safe_strerror (errno));
342 do_cleanups (old_chain);
343
344 ctf_save_metadata_header (&writer->tcs);
345
346 file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
347 old_chain = make_cleanup (xfree, file_name);
348 writer->tcs.datastream_fd = fopen (file_name, "w");
349 if (writer->tcs.datastream_fd == NULL)
350 error (_("Unable to open file '%s' for saving trace data (%s)"),
351 file_name, safe_strerror (errno));
352 do_cleanups (old_chain);
353}
354
355/* This is the implementation of trace_file_write_ops method
356 write_header. Write the types of events on trace variable and
357 frame. */
358
359static void
360ctf_write_header (struct trace_file_writer *self)
361{
362 struct ctf_trace_file_writer *writer
363 = (struct ctf_trace_file_writer *) self;
364
365
366 ctf_save_write_metadata (&writer->tcs, "\n");
367 ctf_save_write_metadata (&writer->tcs,
368 "event {\n\tname = \"memory\";\n\tid = %u;\n"
369 "\tfields := struct { \n"
370 "\t\tuint64_t address;\n"
371 "\t\tuint16_t length;\n"
372 "\t\tuint8_t contents[length];\n"
373 "\t};\n"
374 "};\n", CTF_EVENT_ID_MEMORY);
375
376 ctf_save_write_metadata (&writer->tcs, "\n");
377 ctf_save_write_metadata (&writer->tcs,
378 "event {\n\tname = \"tsv\";\n\tid = %u;\n"
379 "\tfields := struct { \n"
380 "\t\tuint64_t val;\n"
381 "\t\tuint32_t num;\n"
382 "\t};\n"
383 "};\n", CTF_EVENT_ID_TSV);
384
385 ctf_save_write_metadata (&writer->tcs, "\n");
386 ctf_save_write_metadata (&writer->tcs,
387 "event {\n\tname = \"frame\";\n\tid = %u;\n"
388 "\tfields := struct { \n"
389 "\t};\n"
390 "};\n", CTF_EVENT_ID_FRAME);
391
393fd4c3
YQ
392 ctf_save_write_metadata (&writer->tcs, "\n");
393 ctf_save_write_metadata (&writer->tcs,
394 "event {\n\tname = \"tsv_def\";\n"
395 "\tid = %u;\n\tfields := struct { \n"
396 "\t\tint64_t initial_value;\n"
397 "\t\tint32_t number;\n"
398 "\t\tint32_t builtin;\n"
399 "\t\tchars name;\n"
400 "\t};\n"
401 "};\n", CTF_EVENT_ID_TSV_DEF);
402
403 ctf_save_write_metadata (&writer->tcs, "\n");
404 ctf_save_write_metadata (&writer->tcs,
405 "event {\n\tname = \"tp_def\";\n"
406 "\tid = %u;\n\tfields := struct { \n"
407 "\t\tuint64_t addr;\n"
408 "\t\tuint64_t traceframe_usage;\n"
409 "\t\tint32_t number;\n"
410 "\t\tint32_t enabled;\n"
411 "\t\tint32_t step;\n"
412 "\t\tint32_t pass;\n"
413 "\t\tint32_t hit_count;\n"
414 "\t\tint32_t type;\n"
415 "\t\tchars cond;\n"
416
417 "\t\tuint32_t action_num;\n"
418 "\t\tchars actions[action_num];\n"
419
420 "\t\tuint32_t step_action_num;\n"
421 "\t\tchars step_actions[step_action_num];\n"
422
423 "\t\tchars at_string;\n"
424 "\t\tchars cond_string;\n"
425
426 "\t\tuint32_t cmd_num;\n"
427 "\t\tchars cmd_strings[cmd_num];\n"
428 "\t};\n"
429 "};\n", CTF_EVENT_ID_TP_DEF);
430
d0353e76
YQ
431 gdb_assert (writer->tcs.content_size == 0);
432 gdb_assert (writer->tcs.packet_start == 0);
393fd4c3
YQ
433
434 /* Create a new packet to contain this event. */
435 self->ops->frame_ops->start (self, 0);
d0353e76
YQ
436}
437
438/* This is the implementation of trace_file_write_ops method
439 write_regblock_type. Write the type of register event in
440 metadata. */
441
442static void
443ctf_write_regblock_type (struct trace_file_writer *self, int size)
444{
445 struct ctf_trace_file_writer *writer
446 = (struct ctf_trace_file_writer *) self;
447
448 ctf_save_write_metadata (&writer->tcs, "\n");
449
450 ctf_save_write_metadata (&writer->tcs,
451 "event {\n\tname = \"register\";\n\tid = %u;\n"
452 "\tfields := struct { \n"
453 "\t\tascii contents[%d];\n"
454 "\t};\n"
455 "};\n",
456 CTF_EVENT_ID_REGISTER, size);
457}
458
459/* This is the implementation of trace_file_write_ops method
460 write_status. */
461
462static void
463ctf_write_status (struct trace_file_writer *self,
464 struct trace_status *ts)
465{
393fd4c3
YQ
466 struct ctf_trace_file_writer *writer
467 = (struct ctf_trace_file_writer *) self;
468 uint32_t id;
469 int32_t int32;
470
471 ctf_save_write_metadata (&writer->tcs, "\n");
472 ctf_save_write_metadata (&writer->tcs,
473 "event {\n\tname = \"status\";\n\tid = %u;\n"
474 "\tfields := struct { \n"
475 "\t\tint32_t stop_reason;\n"
476 "\t\tint32_t stopping_tracepoint;\n"
477 "\t\tint32_t traceframe_count;\n"
478 "\t\tint32_t traceframes_created;\n"
479 "\t\tint32_t buffer_free;\n"
480 "\t\tint32_t buffer_size;\n"
481 "\t\tint32_t disconnected_tracing;\n"
482 "\t\tint32_t circular_buffer;\n"
483 "\t};\n"
484 "};\n",
485 CTF_EVENT_ID_STATUS);
486
487 id = CTF_EVENT_ID_STATUS;
488 /* Event Id. */
489 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
490
491 ctf_save_write_int32 (&writer->tcs, ts->stop_reason);
492 ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint);
493 ctf_save_write_int32 (&writer->tcs, ts->traceframe_count);
494 ctf_save_write_int32 (&writer->tcs, ts->traceframes_created);
495 ctf_save_write_int32 (&writer->tcs, ts->buffer_free);
496 ctf_save_write_int32 (&writer->tcs, ts->buffer_size);
497 ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing);
498 ctf_save_write_int32 (&writer->tcs, ts->circular_buffer);
d0353e76
YQ
499}
500
501/* This is the implementation of trace_file_write_ops method
502 write_uploaded_tsv. */
503
504static void
505ctf_write_uploaded_tsv (struct trace_file_writer *self,
506 struct uploaded_tsv *tsv)
507{
393fd4c3
YQ
508 struct ctf_trace_file_writer *writer
509 = (struct ctf_trace_file_writer *) self;
510 int32_t int32;
511 int64_t int64;
512 unsigned int len;
513 const gdb_byte zero = 0;
514
515 /* Event Id. */
516 int32 = CTF_EVENT_ID_TSV_DEF;
517 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
518
519 /* initial_value */
520 int64 = tsv->initial_value;
521 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
522
523 /* number */
524 ctf_save_write_int32 (&writer->tcs, tsv->number);
525
526 /* builtin */
527 ctf_save_write_int32 (&writer->tcs, tsv->builtin);
528
529 /* name */
530 if (tsv->name != NULL)
a398505b
PA
531 ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name,
532 strlen (tsv->name));
393fd4c3 533 ctf_save_write (&writer->tcs, &zero, 1);
d0353e76
YQ
534}
535
536/* This is the implementation of trace_file_write_ops method
537 write_uploaded_tp. */
538
539static void
540ctf_write_uploaded_tp (struct trace_file_writer *self,
541 struct uploaded_tp *tp)
542{
393fd4c3
YQ
543 struct ctf_trace_file_writer *writer
544 = (struct ctf_trace_file_writer *) self;
545 int32_t int32;
546 int64_t int64;
547 uint32_t u32;
548 const gdb_byte zero = 0;
549 int a;
550 char *act;
551
552 /* Event Id. */
553 int32 = CTF_EVENT_ID_TP_DEF;
554 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
555
556 /* address */
557 int64 = tp->addr;
558 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
559
560 /* traceframe_usage */
561 int64 = tp->traceframe_usage;
562 ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
563
564 /* number */
565 ctf_save_write_int32 (&writer->tcs, tp->number);
566
567 /* enabled */
568 ctf_save_write_int32 (&writer->tcs, tp->enabled);
569
570 /* step */
571 ctf_save_write_int32 (&writer->tcs, tp->step);
572
573 /* pass */
574 ctf_save_write_int32 (&writer->tcs, tp->pass);
575
576 /* hit_count */
577 ctf_save_write_int32 (&writer->tcs, tp->hit_count);
578
579 /* type */
580 ctf_save_write_int32 (&writer->tcs, tp->type);
581
582 /* condition */
583 if (tp->cond != NULL)
a398505b 584 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond, strlen (tp->cond));
393fd4c3
YQ
585 ctf_save_write (&writer->tcs, &zero, 1);
586
587 /* actions */
588 u32 = VEC_length (char_ptr, tp->actions);
589 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
590 for (a = 0; VEC_iterate (char_ptr, tp->actions, a, act); ++a)
a398505b 591 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
393fd4c3
YQ
592
593 /* step_actions */
594 u32 = VEC_length (char_ptr, tp->step_actions);
595 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
596 for (a = 0; VEC_iterate (char_ptr, tp->step_actions, a, act); ++a)
a398505b 597 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
393fd4c3
YQ
598
599 /* at_string */
600 if (tp->at_string != NULL)
a398505b 601 ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string,
393fd4c3
YQ
602 strlen (tp->at_string));
603 ctf_save_write (&writer->tcs, &zero, 1);
604
605 /* cond_string */
606 if (tp->cond_string != NULL)
a398505b 607 ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string,
393fd4c3
YQ
608 strlen (tp->cond_string));
609 ctf_save_write (&writer->tcs, &zero, 1);
610
611 /* cmd_strings */
612 u32 = VEC_length (char_ptr, tp->cmd_strings);
613 ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
614 for (a = 0; VEC_iterate (char_ptr, tp->cmd_strings, a, act); ++a)
a398505b 615 ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
393fd4c3 616
d0353e76
YQ
617}
618
18d3cec5
MK
619/* This is the implementation of trace_file_write_ops method
620 write_tdesc. */
621
622static void
623ctf_write_tdesc (struct trace_file_writer *self)
624{
625 /* Nothing so far. */
626}
627
d0353e76
YQ
628/* This is the implementation of trace_file_write_ops method
629 write_definition_end. */
630
631static void
632ctf_write_definition_end (struct trace_file_writer *self)
633{
393fd4c3
YQ
634 struct ctf_trace_file_writer *writer
635 = (struct ctf_trace_file_writer *) self;
636
637 self->ops->frame_ops->end (self);
d0353e76
YQ
638}
639
d0353e76
YQ
640/* This is the implementation of trace_file_write_ops method
641 end. */
642
643static void
644ctf_end (struct trace_file_writer *self)
645{
646 struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
647
648 gdb_assert (writer->tcs.content_size == 0);
d0353e76
YQ
649}
650
651/* This is the implementation of trace_frame_write_ops method
652 start. */
653
654static void
655ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
656{
657 struct ctf_trace_file_writer *writer
658 = (struct ctf_trace_file_writer *) self;
659 uint32_t id = CTF_EVENT_ID_FRAME;
660 uint32_t u32;
661
662 /* Step 1: Write packet context. */
663 /* magic. */
664 u32 = CTF_MAGIC;
665 ctf_save_write_uint32 (&writer->tcs, u32);
666 /* content_size and packet_size.. We still don't know the value,
667 write it later. */
668 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
669 ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
670 /* Tracepoint number. */
671 ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
672
673 /* Step 2: Write event "frame". */
674 /* Event Id. */
675 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
676}
677
678/* This is the implementation of trace_frame_write_ops method
679 write_r_block. */
680
681static void
682ctf_write_frame_r_block (struct trace_file_writer *self,
683 gdb_byte *buf, int32_t size)
684{
685 struct ctf_trace_file_writer *writer
686 = (struct ctf_trace_file_writer *) self;
687 uint32_t id = CTF_EVENT_ID_REGISTER;
688
689 /* Event Id. */
690 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
691
692 /* array contents. */
693 ctf_save_align_write (&writer->tcs, buf, size, 1);
694}
695
696/* This is the implementation of trace_frame_write_ops method
697 write_m_block_header. */
698
699static void
700ctf_write_frame_m_block_header (struct trace_file_writer *self,
701 uint64_t addr, uint16_t length)
702{
703 struct ctf_trace_file_writer *writer
704 = (struct ctf_trace_file_writer *) self;
705 uint32_t event_id = CTF_EVENT_ID_MEMORY;
706
707 /* Event Id. */
708 ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
709
710 /* Address. */
711 ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
712
713 /* Length. */
714 ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
715}
716
717/* This is the implementation of trace_frame_write_ops method
718 write_m_block_memory. */
719
720static void
721ctf_write_frame_m_block_memory (struct trace_file_writer *self,
722 gdb_byte *buf, uint16_t length)
723{
724 struct ctf_trace_file_writer *writer
725 = (struct ctf_trace_file_writer *) self;
726
727 /* Contents. */
728 ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
729}
730
731/* This is the implementation of trace_frame_write_ops method
732 write_v_block. */
733
734static void
735ctf_write_frame_v_block (struct trace_file_writer *self,
736 int32_t num, uint64_t val)
737{
738 struct ctf_trace_file_writer *writer
739 = (struct ctf_trace_file_writer *) self;
740 uint32_t id = CTF_EVENT_ID_TSV;
741
742 /* Event Id. */
743 ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
744
745 /* val. */
746 ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
747 /* num. */
748 ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
749}
750
751/* This is the implementation of trace_frame_write_ops method
752 end. */
753
754static void
755ctf_write_frame_end (struct trace_file_writer *self)
756{
757 struct ctf_trace_file_writer *writer
758 = (struct ctf_trace_file_writer *) self;
759 uint32_t u32;
760 uint32_t t;
761
762 /* Write the content size to packet header. */
763 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
764 SEEK_SET);
765 u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
766
767 t = writer->tcs.content_size;
768 ctf_save_write_uint32 (&writer->tcs, u32);
769
770 /* Write the packet size. */
771 u32 += 4 * TARGET_CHAR_BIT;
772 ctf_save_write_uint32 (&writer->tcs, u32);
773
774 writer->tcs.content_size = t;
775
776 /* Write zero at the end of the packet. */
777 ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
778 SEEK_SET);
779 u32 = 0;
780 ctf_save_write_uint32 (&writer->tcs, u32);
781 writer->tcs.content_size = t;
782
783 ctf_save_next_packet (&writer->tcs);
784}
785
786/* Operations to write various types of trace frames into CTF
787 format. */
788
789static const struct trace_frame_write_ops ctf_write_frame_ops =
790{
791 ctf_write_frame_start,
792 ctf_write_frame_r_block,
793 ctf_write_frame_m_block_header,
794 ctf_write_frame_m_block_memory,
795 ctf_write_frame_v_block,
796 ctf_write_frame_end,
797};
798
799/* Operations to write trace buffers into CTF format. */
800
801static const struct trace_file_write_ops ctf_write_ops =
802{
803 ctf_dtor,
804 ctf_target_save,
805 ctf_start,
806 ctf_write_header,
807 ctf_write_regblock_type,
808 ctf_write_status,
809 ctf_write_uploaded_tsv,
810 ctf_write_uploaded_tp,
18d3cec5 811 ctf_write_tdesc,
d0353e76
YQ
812 ctf_write_definition_end,
813 NULL,
814 &ctf_write_frame_ops,
815 ctf_end,
816};
817
818/* Return a trace writer for CTF format. */
819
820struct trace_file_writer *
821ctf_trace_file_writer_new (void)
822{
8d749320 823 struct ctf_trace_file_writer *writer = XNEW (struct ctf_trace_file_writer);
d0353e76
YQ
824
825 writer->base.ops = &ctf_write_ops;
826
827 return (struct trace_file_writer *) writer;
828}
393fd4c3
YQ
829
830#if HAVE_LIBBABELTRACE
831/* Use libbabeltrace to read CTF data. The libbabeltrace provides
832 iterator to iterate over each event in CTF data and APIs to get
833 details of event and packet, so it is very convenient to use
834 libbabeltrace to access events in CTF. */
835
836#include <babeltrace/babeltrace.h>
837#include <babeltrace/ctf/events.h>
838#include <babeltrace/ctf/iterator.h>
839
840/* The struct pointer for current CTF directory. */
614d5099 841static int handle_id = -1;
393fd4c3
YQ
842static struct bt_context *ctx = NULL;
843static struct bt_ctf_iter *ctf_iter = NULL;
844/* The position of the first packet containing trace frame. */
845static struct bt_iter_pos *start_pos;
846
847/* The name of CTF directory. */
848static char *trace_dirname;
849
850static struct target_ops ctf_ops;
851
852/* Destroy ctf iterator and context. */
853
854static void
855ctf_destroy (void)
856{
857 if (ctf_iter != NULL)
858 {
859 bt_ctf_iter_destroy (ctf_iter);
860 ctf_iter = NULL;
861 }
862 if (ctx != NULL)
863 {
864 bt_context_put (ctx);
865 ctx = NULL;
866 }
867}
868
869/* Open CTF trace data in DIRNAME. */
870
871static void
014f9477 872ctf_open_dir (const char *dirname)
393fd4c3 873{
393fd4c3
YQ
874 struct bt_iter_pos begin_pos;
875 struct bt_iter_pos *pos;
614d5099
YQ
876 unsigned int count, i;
877 struct bt_ctf_event_decl * const *list;
393fd4c3
YQ
878
879 ctx = bt_context_create ();
880 if (ctx == NULL)
881 error (_("Unable to create bt_context"));
614d5099
YQ
882 handle_id = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
883 if (handle_id < 0)
393fd4c3
YQ
884 {
885 ctf_destroy ();
886 error (_("Unable to use libbabeltrace on directory \"%s\""),
887 dirname);
888 }
889
890 begin_pos.type = BT_SEEK_BEGIN;
891 ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL);
892 if (ctf_iter == NULL)
893 {
894 ctf_destroy ();
895 error (_("Unable to create bt_iterator"));
896 }
897
614d5099
YQ
898 /* Look for the declaration of register block. Get the length of
899 array "contents" to set trace_regblock_size. */
393fd4c3 900
614d5099
YQ
901 bt_ctf_get_event_decl_list (handle_id, ctx, &list, &count);
902 for (i = 0; i < count; i++)
903 if (strcmp ("register", bt_ctf_get_decl_event_name (list[i])) == 0)
904 {
905 unsigned int j;
906 const struct bt_ctf_field_decl * const *field_list;
907 const struct bt_declaration *decl;
393fd4c3 908
614d5099
YQ
909 bt_ctf_get_decl_fields (list[i], BT_EVENT_FIELDS, &field_list,
910 &count);
393fd4c3 911
614d5099
YQ
912 gdb_assert (count == 1);
913 gdb_assert (0 == strcmp ("contents",
914 bt_ctf_get_decl_field_name (field_list[0])));
915 decl = bt_ctf_get_decl_from_field_decl (field_list[0]);
916 trace_regblock_size = bt_ctf_get_array_len (decl);
393fd4c3 917
393fd4c3 918 break;
614d5099 919 }
393fd4c3
YQ
920}
921
922#define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
923 (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
924 (SCOPE), \
925 #FIELD))
926
e0d13cbd
SM
927#define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \
928 (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
929 (SCOPE), \
930 #FIELD))
931
932
393fd4c3
YQ
933/* EVENT is the "status" event and TS is filled in. */
934
935static void
936ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts)
937{
938 const struct bt_definition *scope
939 = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS);
940
e0d13cbd 941 SET_ENUM_FIELD (event, scope, ts, enum trace_stop_reason, stop_reason);
393fd4c3
YQ
942 SET_INT32_FIELD (event, scope, ts, stopping_tracepoint);
943 SET_INT32_FIELD (event, scope, ts, traceframe_count);
944 SET_INT32_FIELD (event, scope, ts, traceframes_created);
945 SET_INT32_FIELD (event, scope, ts, buffer_free);
946 SET_INT32_FIELD (event, scope, ts, buffer_size);
947 SET_INT32_FIELD (event, scope, ts, disconnected_tracing);
948 SET_INT32_FIELD (event, scope, ts, circular_buffer);
949
950 bt_iter_next (bt_ctf_get_iter (ctf_iter));
951}
952
953/* Read the events "tsv_def" one by one, extract its contents and fill
954 in the list UPLOADED_TSVS. */
955
956static void
957ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
958{
959 gdb_assert (ctf_iter != NULL);
960
961 while (1)
962 {
963 struct bt_ctf_event *event;
964 const struct bt_definition *scope;
965 const struct bt_definition *def;
966 uint32_t event_id;
967 struct uploaded_tsv *utsv = NULL;
968
969 event = bt_ctf_iter_read_event (ctf_iter);
970 scope = bt_ctf_get_top_level_scope (event,
971 BT_STREAM_EVENT_HEADER);
972 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
973 "id"));
974 if (event_id != CTF_EVENT_ID_TSV_DEF)
975 break;
976
977 scope = bt_ctf_get_top_level_scope (event,
978 BT_EVENT_FIELDS);
979
980 def = bt_ctf_get_field (event, scope, "number");
981 utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def),
982 uploaded_tsvs);
983
984 def = bt_ctf_get_field (event, scope, "builtin");
985 utsv->builtin = (int32_t) bt_ctf_get_int64 (def);
986 def = bt_ctf_get_field (event, scope, "initial_value");
987 utsv->initial_value = bt_ctf_get_int64 (def);
988
989 def = bt_ctf_get_field (event, scope, "name");
990 utsv->name = xstrdup (bt_ctf_get_string (def));
991
992 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
993 break;
994 }
995
996}
997
998/* Read the value of element whose index is NUM from CTF and write it
999 to the corresponding VAR->ARRAY. */
1000
1001#define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
1002 do \
1003 { \
1004 uint32_t u32, i; \
1005 const struct bt_definition *def; \
1006 \
1007 u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
1008 (SCOPE), \
1009 #NUM)); \
1010 def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
1011 for (i = 0; i < u32; i++) \
1012 { \
1013 const struct bt_definition *element \
1014 = bt_ctf_get_index ((EVENT), def, i); \
1015 \
1016 VEC_safe_push (char_ptr, (VAR)->ARRAY, \
1017 xstrdup (bt_ctf_get_string (element))); \
1018 } \
1019 } \
1020 while (0)
1021
1022/* Read a string from CTF and set VAR->FIELD. If the length of string
1023 is zero, set VAR->FIELD to NULL. */
1024
1025#define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
1026 do \
1027 { \
1028 const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
1029 (SCOPE), \
1030 #FIELD)); \
1031 \
1032 if (strlen (p) > 0) \
1033 (VAR)->FIELD = xstrdup (p); \
1034 else \
1035 (VAR)->FIELD = NULL; \
1036 } \
1037 while (0)
1038
1039/* Read the events "tp_def" one by one, extract its contents and fill
1040 in the list UPLOADED_TPS. */
1041
1042static void
1043ctf_read_tp (struct uploaded_tp **uploaded_tps)
1044{
1045 gdb_assert (ctf_iter != NULL);
1046
1047 while (1)
1048 {
1049 struct bt_ctf_event *event;
1050 const struct bt_definition *scope;
1051 uint32_t u32;
1052 int32_t int32;
1053 uint64_t u64;
1054 struct uploaded_tp *utp = NULL;
1055
1056 event = bt_ctf_iter_read_event (ctf_iter);
1057 scope = bt_ctf_get_top_level_scope (event,
1058 BT_STREAM_EVENT_HEADER);
1059 u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1060 "id"));
1061 if (u32 != CTF_EVENT_ID_TP_DEF)
1062 break;
1063
1064 scope = bt_ctf_get_top_level_scope (event,
1065 BT_EVENT_FIELDS);
1066 int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event,
1067 scope,
1068 "number"));
1069 u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
1070 "addr"));
1071 utp = get_uploaded_tp (int32, u64, uploaded_tps);
1072
1073 SET_INT32_FIELD (event, scope, utp, enabled);
1074 SET_INT32_FIELD (event, scope, utp, step);
1075 SET_INT32_FIELD (event, scope, utp, pass);
1076 SET_INT32_FIELD (event, scope, utp, hit_count);
e0d13cbd 1077 SET_ENUM_FIELD (event, scope, utp, enum bptype, type);
393fd4c3
YQ
1078
1079 /* Read 'cmd_strings'. */
1080 SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings);
1081 /* Read 'actions'. */
1082 SET_ARRAY_FIELD (event, scope, utp, action_num, actions);
1083 /* Read 'step_actions'. */
1084 SET_ARRAY_FIELD (event, scope, utp, step_action_num,
1085 step_actions);
1086
1087 SET_STRING_FIELD(event, scope, utp, at_string);
1088 SET_STRING_FIELD(event, scope, utp, cond_string);
1089
1090 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1091 break;
1092 }
1093}
1094
1095/* This is the implementation of target_ops method to_open. Open CTF
1096 trace data, read trace status, trace state variables and tracepoint
1097 definitions from the first packet. Set the start position at the
1098 second packet which contains events on trace blocks. */
1099
1100static void
014f9477 1101ctf_open (const char *dirname, int from_tty)
393fd4c3
YQ
1102{
1103 struct bt_ctf_event *event;
1104 uint32_t event_id;
1105 const struct bt_definition *scope;
1106 struct uploaded_tsv *uploaded_tsvs = NULL;
1107 struct uploaded_tp *uploaded_tps = NULL;
1108
1109 if (!dirname)
1110 error (_("No CTF directory specified."));
1111
1112 ctf_open_dir (dirname);
1113
1114 target_preopen (from_tty);
1115
1116 /* Skip the first packet which about the trace status. The first
1117 event is "frame". */
1118 event = bt_ctf_iter_read_event (ctf_iter);
1119 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1120 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1121 if (event_id != CTF_EVENT_ID_FRAME)
1122 error (_("Wrong event id of the first event"));
1123 /* The second event is "status". */
1124 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1125 event = bt_ctf_iter_read_event (ctf_iter);
1126 scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
1127 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
1128 if (event_id != CTF_EVENT_ID_STATUS)
1129 error (_("Wrong event id of the second event"));
1130 ctf_read_status (event, current_trace_status ());
1131
1132 ctf_read_tsv (&uploaded_tsvs);
1133
1134 ctf_read_tp (&uploaded_tps);
1135
1136 event = bt_ctf_iter_read_event (ctf_iter);
1137 /* EVENT can be NULL if we've already gone to the end of stream of
1138 events. */
1139 if (event != NULL)
1140 {
1141 scope = bt_ctf_get_top_level_scope (event,
1142 BT_STREAM_EVENT_HEADER);
1143 event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event,
1144 scope, "id"));
1145 if (event_id != CTF_EVENT_ID_FRAME)
1146 error (_("Wrong event id of the first event of the second packet"));
1147 }
1148
1149 start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1150 gdb_assert (start_pos->type == BT_SEEK_RESTORE);
1151
1152 trace_dirname = xstrdup (dirname);
1153 push_target (&ctf_ops);
1154
de7b2893
YQ
1155 inferior_appeared (current_inferior (), CTF_PID);
1156 inferior_ptid = pid_to_ptid (CTF_PID);
1157 add_thread_silent (inferior_ptid);
1158
393fd4c3
YQ
1159 merge_uploaded_trace_state_variables (&uploaded_tsvs);
1160 merge_uploaded_tracepoints (&uploaded_tps);
5723a6fd
YQ
1161
1162 post_create_inferior (&ctf_ops, from_tty);
393fd4c3
YQ
1163}
1164
1165/* This is the implementation of target_ops method to_close. Destroy
1166 CTF iterator and context. */
1167
1168static void
de90e03d 1169ctf_close (struct target_ops *self)
393fd4c3 1170{
de7b2893
YQ
1171 int pid;
1172
393fd4c3
YQ
1173 ctf_destroy ();
1174 xfree (trace_dirname);
1175 trace_dirname = NULL;
aef525cb 1176
de7b2893
YQ
1177 pid = ptid_get_pid (inferior_ptid);
1178 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
1179 exit_inferior_silent (pid);
1180
aef525cb 1181 trace_reset_local_state ();
393fd4c3
YQ
1182}
1183
1184/* This is the implementation of target_ops method to_files_info.
1185 Print the directory name of CTF trace data. */
1186
1187static void
1188ctf_files_info (struct target_ops *t)
1189{
1190 printf_filtered ("\t`%s'\n", trace_dirname);
1191}
1192
1193/* This is the implementation of target_ops method to_fetch_registers.
1194 Iterate over events whose name is "register" in current frame,
1195 extract contents from events, and set REGCACHE with the contents.
1196 If no matched events are found, mark registers unavailable. */
1197
1198static void
1199ctf_fetch_registers (struct target_ops *ops,
1200 struct regcache *regcache, int regno)
1201{
1202 struct gdbarch *gdbarch = get_regcache_arch (regcache);
393fd4c3
YQ
1203 struct bt_ctf_event *event = NULL;
1204 struct bt_iter_pos *pos;
1205
1206 /* An uninitialized reg size says we're not going to be
1207 successful at getting register blocks. */
1208 if (trace_regblock_size == 0)
1209 return;
1210
1211 gdb_assert (ctf_iter != NULL);
1212 /* Save the current position. */
1213 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1214 gdb_assert (pos->type == BT_SEEK_RESTORE);
1215
1216 while (1)
1217 {
1218 const char *name;
1219 struct bt_ctf_event *event1;
1220
1221 event1 = bt_ctf_iter_read_event (ctf_iter);
1222
1223 name = bt_ctf_event_name (event1);
1224
1225 if (name == NULL || strcmp (name, "frame") == 0)
1226 break;
1227 else if (strcmp (name, "register") == 0)
1228 {
1229 event = event1;
1230 break;
1231 }
1232
1233 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1234 break;
1235 }
1236
1237 /* Restore the position. */
1238 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1239
1240 if (event != NULL)
1241 {
48b6e87e 1242 int offset, regsize, regn;
393fd4c3
YQ
1243 const struct bt_definition *scope
1244 = bt_ctf_get_top_level_scope (event,
1245 BT_EVENT_FIELDS);
1246 const struct bt_definition *array
1247 = bt_ctf_get_field (event, scope, "contents");
48b6e87e 1248 gdb_byte *regs = (gdb_byte *) bt_ctf_get_char_array (array);
393fd4c3 1249
393fd4c3
YQ
1250 /* Assume the block is laid out in GDB register number order,
1251 each register with the size that it has in GDB. */
1252 offset = 0;
1253 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
1254 {
1255 regsize = register_size (gdbarch, regn);
1256 /* Make sure we stay within block bounds. */
1257 if (offset + regsize >= trace_regblock_size)
1258 break;
1259 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
1260 {
1261 if (regno == regn)
1262 {
1263 regcache_raw_supply (regcache, regno, regs + offset);
1264 break;
1265 }
1266 else if (regno == -1)
1267 {
1268 regcache_raw_supply (regcache, regn, regs + offset);
1269 }
1270 }
1271 offset += regsize;
1272 }
393fd4c3 1273 }
48b6e87e
YQ
1274 else
1275 tracefile_fetch_registers (regcache, regno);
393fd4c3
YQ
1276}
1277
1278/* This is the implementation of target_ops method to_xfer_partial.
1279 Iterate over events whose name is "memory" in
1280 current frame, extract the address and length from events. If
1281 OFFSET is within the range, read the contents from events to
1282 READBUF. */
1283
9b409511 1284static enum target_xfer_status
393fd4c3
YQ
1285ctf_xfer_partial (struct target_ops *ops, enum target_object object,
1286 const char *annex, gdb_byte *readbuf,
1287 const gdb_byte *writebuf, ULONGEST offset,
9b409511 1288 ULONGEST len, ULONGEST *xfered_len)
393fd4c3
YQ
1289{
1290 /* We're only doing regular memory for now. */
1291 if (object != TARGET_OBJECT_MEMORY)
c9244484 1292 return TARGET_XFER_E_IO;
393fd4c3
YQ
1293
1294 if (readbuf == NULL)
1295 error (_("ctf_xfer_partial: trace file is read-only"));
1296
1297 if (get_traceframe_number () != -1)
1298 {
1299 struct bt_iter_pos *pos;
1300 int i = 0;
8acf9577 1301 enum target_xfer_status res;
290a839c
YQ
1302 /* Records the lowest available address of all blocks that
1303 intersects the requested range. */
1304 ULONGEST low_addr_available = 0;
393fd4c3
YQ
1305
1306 gdb_assert (ctf_iter != NULL);
1307 /* Save the current position. */
1308 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1309 gdb_assert (pos->type == BT_SEEK_RESTORE);
1310
1311 /* Iterate through the traceframe's blocks, looking for
1312 memory. */
1313 while (1)
1314 {
1315 ULONGEST amt;
1316 uint64_t maddr;
1317 uint16_t mlen;
1318 enum bfd_endian byte_order
1319 = gdbarch_byte_order (target_gdbarch ());
1320 const struct bt_definition *scope;
1321 const struct bt_definition *def;
1322 struct bt_ctf_event *event
1323 = bt_ctf_iter_read_event (ctf_iter);
1324 const char *name = bt_ctf_event_name (event);
1325
dac3e710 1326 if (name == NULL || strcmp (name, "frame") == 0)
393fd4c3
YQ
1327 break;
1328 else if (strcmp (name, "memory") != 0)
1329 {
1330 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1331 break;
1332
1333 continue;
1334 }
1335
1336 scope = bt_ctf_get_top_level_scope (event,
1337 BT_EVENT_FIELDS);
1338
1339 def = bt_ctf_get_field (event, scope, "address");
1340 maddr = bt_ctf_get_uint64 (def);
1341 def = bt_ctf_get_field (event, scope, "length");
1342 mlen = (uint16_t) bt_ctf_get_uint64 (def);
1343
1344 /* If the block includes the first part of the desired
1345 range, return as much it has; GDB will re-request the
1346 remainder, which might be in a different block of this
1347 trace frame. */
1348 if (maddr <= offset && offset < (maddr + mlen))
1349 {
1350 const struct bt_definition *array
1351 = bt_ctf_get_field (event, scope, "contents");
1352 const struct bt_declaration *decl
1353 = bt_ctf_get_decl_from_def (array);
1354 gdb_byte *contents;
1355 int k;
1356
224c3ddb 1357 contents = (gdb_byte *) xmalloc (mlen);
393fd4c3
YQ
1358
1359 for (k = 0; k < mlen; k++)
1360 {
1361 const struct bt_definition *element
1362 = bt_ctf_get_index (event, array, k);
1363
1364 contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
1365 }
1366
1367 amt = (maddr + mlen) - offset;
1368 if (amt > len)
1369 amt = len;
1370
1371 memcpy (readbuf, &contents[offset - maddr], amt);
1372
1373 xfree (contents);
1374
1375 /* Restore the position. */
1376 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1377
9b409511
YQ
1378 if (amt == 0)
1379 return TARGET_XFER_EOF;
1380 else
1381 {
1382 *xfered_len = amt;
1383 return TARGET_XFER_OK;
1384 }
393fd4c3
YQ
1385 }
1386
290a839c
YQ
1387 if (offset < maddr && maddr < (offset + len))
1388 if (low_addr_available == 0 || low_addr_available > maddr)
1389 low_addr_available = maddr;
1390
393fd4c3
YQ
1391 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1392 break;
1393 }
1394
1395 /* Restore the position. */
1396 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
393fd4c3 1397
8acf9577
YQ
1398 /* Requested memory is unavailable in the context of traceframes,
1399 and this address falls within a read-only section, fallback
290a839c
YQ
1400 to reading from executable, up to LOW_ADDR_AVAILABLE */
1401 if (offset < low_addr_available)
1402 len = min (len, low_addr_available - offset);
8acf9577
YQ
1403 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1404
1405 if (res == TARGET_XFER_OK)
1406 return TARGET_XFER_OK;
1407 else
1408 {
1409 /* No use trying further, we know some memory starting
1410 at MEMADDR isn't available. */
1411 *xfered_len = len;
1412 return TARGET_XFER_UNAVAILABLE;
1413 }
1ee79381
YQ
1414 }
1415 else
1416 {
1417 /* Fallback to reading from read-only sections. */
1418 return section_table_read_available_memory (readbuf, offset, len, xfered_len);
1419 }
393fd4c3
YQ
1420}
1421
1422/* This is the implementation of target_ops method
1423 to_get_trace_state_variable_value.
1424 Iterate over events whose name is "tsv" in current frame. When the
1425 trace variable is found, set the value of it to *VAL and return
1426 true, otherwise return false. */
1427
1428static int
4011015b
TT
1429ctf_get_trace_state_variable_value (struct target_ops *self,
1430 int tsvnum, LONGEST *val)
393fd4c3
YQ
1431{
1432 struct bt_iter_pos *pos;
1433 int found = 0;
1434
1435 gdb_assert (ctf_iter != NULL);
1436 /* Save the current position. */
1437 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1438 gdb_assert (pos->type == BT_SEEK_RESTORE);
1439
1440 /* Iterate through the traceframe's blocks, looking for 'V'
1441 block. */
1442 while (1)
1443 {
1444 struct bt_ctf_event *event
1445 = bt_ctf_iter_read_event (ctf_iter);
1446 const char *name = bt_ctf_event_name (event);
1447
1448 if (name == NULL || strcmp (name, "frame") == 0)
1449 break;
1450 else if (strcmp (name, "tsv") == 0)
1451 {
1452 const struct bt_definition *scope;
1453 const struct bt_definition *def;
1454
1455 scope = bt_ctf_get_top_level_scope (event,
1456 BT_EVENT_FIELDS);
1457
1458 def = bt_ctf_get_field (event, scope, "num");
1459 if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
1460 {
1461 def = bt_ctf_get_field (event, scope, "val");
1462 *val = bt_ctf_get_uint64 (def);
1463
1464 found = 1;
1465 }
1466 }
1467
1468 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1469 break;
1470 }
1471
1472 /* Restore the position. */
1473 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1474
1475 return found;
1476}
1477
1478/* Return the tracepoint number in "frame" event. */
1479
1480static int
1481ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
1482{
1483 /* The packet context of events has a field "tpnum". */
1484 const struct bt_definition *scope
1485 = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
1486 uint64_t tpnum
1487 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
1488
1489 return (int) tpnum;
1490}
1491
1492/* Return the address at which the current frame was collected. */
1493
1494static CORE_ADDR
1495ctf_get_traceframe_address (void)
1496{
1497 struct bt_ctf_event *event = NULL;
1498 struct bt_iter_pos *pos;
1499 CORE_ADDR addr = 0;
1500
1501 gdb_assert (ctf_iter != NULL);
1502 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1503 gdb_assert (pos->type == BT_SEEK_RESTORE);
1504
1505 while (1)
1506 {
1507 const char *name;
1508 struct bt_ctf_event *event1;
1509
1510 event1 = bt_ctf_iter_read_event (ctf_iter);
1511
1512 name = bt_ctf_event_name (event1);
1513
1514 if (name == NULL)
1515 break;
1516 else if (strcmp (name, "frame") == 0)
1517 {
1518 event = event1;
1519 break;
1520 }
1521
1522 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1523 break;
1524 }
1525
1526 if (event != NULL)
1527 {
1528 int tpnum = ctf_get_tpnum_from_frame_event (event);
1529 struct tracepoint *tp
1530 = get_tracepoint_by_number_on_target (tpnum);
1531
1532 if (tp && tp->base.loc)
1533 addr = tp->base.loc->address;
1534 }
1535
1536 /* Restore the position. */
1537 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1538
1539 return addr;
1540}
1541
1542/* This is the implementation of target_ops method to_trace_find.
1543 Iterate the events whose name is "frame", extract the tracepoint
1544 number in it. Return traceframe number when matched. */
1545
1546static int
bd4c6793 1547ctf_trace_find (struct target_ops *self, enum trace_find_type type, int num,
393fd4c3
YQ
1548 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
1549{
1550 int ret = -1;
1551 int tfnum = 0;
1552 int found = 0;
1553 struct bt_iter_pos pos;
1554
1555 if (num == -1)
1556 {
1557 if (tpp != NULL)
1558 *tpp = -1;
1559 return -1;
1560 }
1561
1562 gdb_assert (ctf_iter != NULL);
1563 /* Set iterator back to the start. */
1564 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
1565
1566 while (1)
1567 {
1568 int id;
1569 struct bt_ctf_event *event;
1570 const char *name;
1571
1572 event = bt_ctf_iter_read_event (ctf_iter);
1573
1574 name = bt_ctf_event_name (event);
1575
1576 if (event == NULL || name == NULL)
1577 break;
1578
1579 if (strcmp (name, "frame") == 0)
1580 {
1581 CORE_ADDR tfaddr;
1582
1583 if (type == tfind_number)
1584 {
1585 /* Looking for a specific trace frame. */
1586 if (tfnum == num)
1587 found = 1;
1588 }
1589 else
1590 {
1591 /* Start from the _next_ trace frame. */
1592 if (tfnum > get_traceframe_number ())
1593 {
1594 switch (type)
1595 {
1596 case tfind_tp:
1597 {
1598 struct tracepoint *tp = get_tracepoint (num);
1599
1600 if (tp != NULL
1601 && (tp->number_on_target
1602 == ctf_get_tpnum_from_frame_event (event)))
1603 found = 1;
1604 break;
1605 }
1606 case tfind_pc:
1607 tfaddr = ctf_get_traceframe_address ();
1608 if (tfaddr == addr1)
1609 found = 1;
1610 break;
1611 case tfind_range:
1612 tfaddr = ctf_get_traceframe_address ();
1613 if (addr1 <= tfaddr && tfaddr <= addr2)
1614 found = 1;
1615 break;
1616 case tfind_outside:
1617 tfaddr = ctf_get_traceframe_address ();
1618 if (!(addr1 <= tfaddr && tfaddr <= addr2))
1619 found = 1;
1620 break;
1621 default:
1622 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
1623 }
1624 }
1625 }
1626 if (found)
1627 {
1628 if (tpp != NULL)
1629 *tpp = ctf_get_tpnum_from_frame_event (event);
1630
1631 /* Skip the event "frame". */
1632 bt_iter_next (bt_ctf_get_iter (ctf_iter));
1633
1634 return tfnum;
1635 }
1636 tfnum++;
1637 }
1638
1639 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1640 break;
1641 }
1642
1643 return -1;
1644}
1645
393fd4c3
YQ
1646/* This is the implementation of target_ops method to_traceframe_info.
1647 Iterate the events whose name is "memory", in current
1648 frame, extract memory range information, and return them in
1649 traceframe_info. */
1650
1651static struct traceframe_info *
a893e81f 1652ctf_traceframe_info (struct target_ops *self)
393fd4c3
YQ
1653{
1654 struct traceframe_info *info = XCNEW (struct traceframe_info);
1655 const char *name;
1656 struct bt_iter_pos *pos;
1657
1658 gdb_assert (ctf_iter != NULL);
1659 /* Save the current position. */
1660 pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
1661 gdb_assert (pos->type == BT_SEEK_RESTORE);
1662
1663 do
1664 {
1665 struct bt_ctf_event *event
1666 = bt_ctf_iter_read_event (ctf_iter);
1667
1668 name = bt_ctf_event_name (event);
1669
1670 if (name == NULL || strcmp (name, "register") == 0
1671 || strcmp (name, "frame") == 0)
1672 ;
1673 else if (strcmp (name, "memory") == 0)
1674 {
1675 const struct bt_definition *scope
1676 = bt_ctf_get_top_level_scope (event,
1677 BT_EVENT_FIELDS);
1678 const struct bt_definition *def;
1679 struct mem_range *r;
1680
1681 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1682 def = bt_ctf_get_field (event, scope, "address");
1683 r->start = bt_ctf_get_uint64 (def);
1684
1685 def = bt_ctf_get_field (event, scope, "length");
1686 r->length = (uint16_t) bt_ctf_get_uint64 (def);
1687 }
28a93511
YQ
1688 else if (strcmp (name, "tsv") == 0)
1689 {
1690 int vnum;
1691 const struct bt_definition *scope
1692 = bt_ctf_get_top_level_scope (event,
1693 BT_EVENT_FIELDS);
1694 const struct bt_definition *def;
1695
1696 def = bt_ctf_get_field (event, scope, "num");
1697 vnum = (int) bt_ctf_get_int64 (def);
1698 VEC_safe_push (int, info->tvars, vnum);
1699 }
393fd4c3
YQ
1700 else
1701 {
1702 warning (_("Unhandled trace block type (%s) "
1703 "while building trace frame info."),
1704 name);
1705 }
1706
1707 if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
1708 break;
1709 }
1710 while (name != NULL && strcmp (name, "frame") != 0);
1711
1712 /* Restore the position. */
1713 bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
1714
1715 return info;
1716}
1717
393fd4c3
YQ
1718static void
1719init_ctf_ops (void)
1720{
1721 memset (&ctf_ops, 0, sizeof (ctf_ops));
1722
12e03cd0 1723 init_tracefile_ops (&ctf_ops);
393fd4c3
YQ
1724 ctf_ops.to_shortname = "ctf";
1725 ctf_ops.to_longname = "CTF file";
1726 ctf_ops.to_doc = "Use a CTF directory as a target.\n\
1727Specify the filename of the CTF directory.";
1728 ctf_ops.to_open = ctf_open;
1729 ctf_ops.to_close = ctf_close;
1730 ctf_ops.to_fetch_registers = ctf_fetch_registers;
1731 ctf_ops.to_xfer_partial = ctf_xfer_partial;
1732 ctf_ops.to_files_info = ctf_files_info;
393fd4c3
YQ
1733 ctf_ops.to_trace_find = ctf_trace_find;
1734 ctf_ops.to_get_trace_state_variable_value
1735 = ctf_get_trace_state_variable_value;
393fd4c3 1736 ctf_ops.to_traceframe_info = ctf_traceframe_info;
393fd4c3
YQ
1737}
1738
1739#endif
1740
1741/* -Wmissing-prototypes */
1742
1743extern initialize_file_ftype _initialize_ctf;
1744
1745/* module initialization */
1746
1747void
1748_initialize_ctf (void)
1749{
1750#if HAVE_LIBBABELTRACE
1751 init_ctf_ops ();
1752
da9160e4 1753 add_target_with_completer (&ctf_ops, filename_completer);
393fd4c3
YQ
1754#endif
1755}
This page took 0.308369 seconds and 4 git commands to generate.