1 /* Trace file support in GDB.
3 Copyright (C) 1997-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "tracefile.h"
25 #include "gdbsupport/byte-vector.h"
30 #define TRACE_WRITE_R_BLOCK(writer, buf, size) \
31 writer->ops->frame_ops->write_r_block ((writer), (buf), (size))
32 #define TRACE_WRITE_M_BLOCK_HEADER(writer, addr, size) \
33 writer->ops->frame_ops->write_m_block_header ((writer), (addr), \
35 #define TRACE_WRITE_M_BLOCK_MEMORY(writer, buf, size) \
36 writer->ops->frame_ops->write_m_block_memory ((writer), (buf), \
38 #define TRACE_WRITE_V_BLOCK(writer, num, val) \
39 writer->ops->frame_ops->write_v_block ((writer), (num), (val))
41 /* A unique pointer policy class for trace_file_writer. */
43 struct trace_file_writer_deleter
45 void operator() (struct trace_file_writer
*writer
)
47 writer
->ops
->dtor (writer
);
52 /* A unique_ptr specialization for trace_file_writer. */
54 typedef std::unique_ptr
<trace_file_writer
, trace_file_writer_deleter
>
57 /* Save tracepoint data to file named FILENAME through WRITER. WRITER
58 determines the trace file format. If TARGET_DOES_SAVE is non-zero,
59 the save is performed on the target, otherwise GDB obtains all trace
60 data and saves it locally. */
63 trace_save (const char *filename
, struct trace_file_writer
*writer
,
66 struct trace_status
*ts
= current_trace_status ();
67 struct uploaded_tp
*uploaded_tps
= NULL
, *utp
;
68 struct uploaded_tsv
*uploaded_tsvs
= NULL
, *utsv
;
71 #define MAX_TRACE_UPLOAD 2000
72 gdb::byte_vector
buf (std::max (MAX_TRACE_UPLOAD
, trace_regblock_size
));
73 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
75 /* If the target is to save the data to a file on its own, then just
76 send the command and be done with it. */
79 if (!writer
->ops
->target_save (writer
, filename
))
80 error (_("Target failed to save trace data to '%s'."),
85 /* Get the trace status first before opening the file, so if the
86 target is losing, we can get out without touching files. Since
87 we're just calling this for side effects, we ignore the
89 target_get_trace_status (ts
);
91 writer
->ops
->start (writer
, filename
);
93 writer
->ops
->write_header (writer
);
95 /* Write descriptive info. */
97 /* Write out the size of a register block. */
98 writer
->ops
->write_regblock_type (writer
, trace_regblock_size
);
100 /* Write out the target description info. */
101 writer
->ops
->write_tdesc (writer
);
103 /* Write out status of the tracing run (aka "tstatus" info). */
104 writer
->ops
->write_status (writer
, ts
);
106 /* Note that we want to upload tracepoints and save those, rather
107 than simply writing out the local ones, because the user may have
108 changed tracepoints in GDB in preparation for a future tracing
109 run, or maybe just mass-deleted all types of breakpoints as part
110 of cleaning up. So as not to contaminate the session, leave the
111 data in its uploaded form, don't make into real tracepoints. */
113 /* Get trace state variables first, they may be checked when parsing
114 uploaded commands. */
116 target_upload_trace_state_variables (&uploaded_tsvs
);
118 for (utsv
= uploaded_tsvs
; utsv
; utsv
= utsv
->next
)
119 writer
->ops
->write_uploaded_tsv (writer
, utsv
);
121 free_uploaded_tsvs (&uploaded_tsvs
);
123 target_upload_tracepoints (&uploaded_tps
);
125 for (utp
= uploaded_tps
; utp
; utp
= utp
->next
)
126 target_get_tracepoint_status (NULL
, utp
);
128 for (utp
= uploaded_tps
; utp
; utp
= utp
->next
)
129 writer
->ops
->write_uploaded_tp (writer
, utp
);
131 free_uploaded_tps (&uploaded_tps
);
133 /* Mark the end of the definition section. */
134 writer
->ops
->write_definition_end (writer
);
136 /* Get and write the trace data proper. */
141 /* The writer supports writing the contents of trace buffer
142 directly to trace file. Don't parse the contents of trace
144 if (writer
->ops
->write_trace_buffer
!= NULL
)
146 /* We ask for big blocks, in the hopes of efficiency, but
147 will take less if the target has packet size limitations
149 gotten
= target_get_raw_trace_data (buf
.data (), offset
,
152 error (_("Failure to get requested trace buffer data"));
153 /* No more data is forthcoming, we're done. */
157 writer
->ops
->write_trace_buffer (writer
, buf
.data (), gotten
);
165 /* Parse the trace buffers according to how data are stored
166 in trace buffer in GDBserver. */
168 gotten
= target_get_raw_trace_data (buf
.data (), offset
, 6);
173 /* Read the first six bytes in, which is the tracepoint
174 number and trace frame size. */
176 extract_unsigned_integer (&((buf
.data ())[0]), 2, byte_order
);
179 extract_unsigned_integer (&((buf
.data ())[2]), 4, byte_order
);
181 writer
->ops
->frame_ops
->start (writer
, tp_num
);
190 for (block
= 0; block
< tf_size
; )
194 /* We'll fetch one block each time, in order to
195 handle the extremely large 'M' block. We first
196 fetch one byte to get the type of the block. */
197 gotten
= target_get_raw_trace_data (buf
.data (),
200 error (_("Failure to get requested trace buffer data"));
211 = target_get_raw_trace_data (buf
.data (), offset
,
212 trace_regblock_size
);
213 if (gotten
< trace_regblock_size
)
214 error (_("Failure to get requested trace"
217 TRACE_WRITE_R_BLOCK (writer
, buf
.data (),
218 trace_regblock_size
);
227 t
= target_get_raw_trace_data (buf
.data (),
230 error (_("Failure to get requested trace"
238 extract_unsigned_integer (buf
.data (), 8,
240 mlen
= (unsigned short)
241 extract_unsigned_integer (&((buf
.data ())[8]), 2,
244 TRACE_WRITE_M_BLOCK_HEADER (writer
, addr
,
247 /* The memory contents in 'M' block may be
248 very large. Fetch the data from the target
249 and write them into file one by one. */
250 for (j
= 0; j
< mlen
; )
252 unsigned int read_length
;
254 if (mlen
- j
> MAX_TRACE_UPLOAD
)
255 read_length
= MAX_TRACE_UPLOAD
;
257 read_length
= mlen
- j
;
259 t
= target_get_raw_trace_data (buf
.data (),
263 error (_("Failure to get requested"
264 " trace buffer data"));
266 TRACE_WRITE_M_BLOCK_MEMORY (writer
,
271 gotten
+= read_length
;
282 = target_get_raw_trace_data (buf
.data (),
285 error (_("Failure to get requested"
286 " trace buffer data"));
288 vnum
= (int) extract_signed_integer (buf
.data (),
292 = extract_signed_integer (&((buf
.data ())[4]),
295 TRACE_WRITE_V_BLOCK (writer
, vnum
, val
);
299 error (_("Unknown block type '%c' (0x%x) in"
301 block_type
, block_type
);
311 writer
->ops
->frame_ops
->end (writer
);
315 writer
->ops
->end (writer
);
319 tsave_command (const char *args
, int from_tty
)
321 int target_does_save
= 0;
323 char *filename
= NULL
;
324 int generate_ctf
= 0;
327 error_no_arg (_("file in which to save trace data"));
329 gdb_argv
built_argv (args
);
330 argv
= built_argv
.get ();
332 for (; *argv
; ++argv
)
334 if (strcmp (*argv
, "-r") == 0)
335 target_does_save
= 1;
336 else if (strcmp (*argv
, "-ctf") == 0)
338 else if (**argv
== '-')
339 error (_("unknown option `%s'"), *argv
);
345 error_no_arg (_("file in which to save trace data"));
348 trace_save_ctf (filename
, target_does_save
);
350 trace_save_tfile (filename
, target_does_save
);
353 printf_filtered (_("Trace data saved to %s '%s'.\n"),
354 generate_ctf
? "directory" : "file", filename
);
357 /* Save the trace data to file FILENAME of tfile format. */
360 trace_save_tfile (const char *filename
, int target_does_save
)
362 trace_file_writer_up
writer (tfile_trace_file_writer_new ());
363 trace_save (filename
, writer
.get (), target_does_save
);
366 /* Save the trace data to dir DIRNAME of ctf format. */
369 trace_save_ctf (const char *dirname
, int target_does_save
)
371 trace_file_writer_up
writer (ctf_trace_file_writer_new ());
372 trace_save (dirname
, writer
.get (), target_does_save
);
375 /* Fetch register data from tracefile, shared for both tfile and
379 tracefile_fetch_registers (struct regcache
*regcache
, int regno
)
381 struct gdbarch
*gdbarch
= regcache
->arch ();
382 struct tracepoint
*tp
= get_tracepoint (get_tracepoint_number ());
385 /* We get here if no register data has been found. Mark registers
387 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
388 regcache
->raw_supply (regn
, NULL
);
390 /* We can often usefully guess that the PC is going to be the same
391 as the address of the tracepoint. */
392 if (tp
== NULL
|| tp
->loc
== NULL
)
395 /* But don't try to guess if tracepoint is multi-location... */
398 warning (_("Tracepoint %d has multiple "
399 "locations, cannot infer $pc"),
403 /* ... or does while-stepping. */
404 else if (tp
->step_count
> 0)
406 warning (_("Tracepoint %d does while-stepping, "
412 /* Guess what we can from the tracepoint location. */
413 gdbarch_guess_tracepoint_registers (gdbarch
, regcache
,
417 /* This is the implementation of target_ops method to_has_all_memory. */
420 tracefile_target::has_all_memory ()
425 /* This is the implementation of target_ops method to_has_memory. */
428 tracefile_target::has_memory ()
433 /* This is the implementation of target_ops method to_has_stack.
434 The target has a stack when GDB has already selected one trace
438 tracefile_target::has_stack ()
440 return get_traceframe_number () != -1;
443 /* This is the implementation of target_ops method to_has_registers.
444 The target has registers when GDB has already selected one trace
448 tracefile_target::has_registers ()
450 return get_traceframe_number () != -1;
453 /* This is the implementation of target_ops method to_thread_alive.
454 tracefile has one thread faked by GDB. */
457 tracefile_target::thread_alive (ptid_t ptid
)
462 /* This is the implementation of target_ops method to_get_trace_status.
463 The trace status for a file is that tracing can never be run. */
466 tracefile_target::get_trace_status (struct trace_status
*ts
)
468 /* Other bits of trace status were collected as part of opening the
469 trace files, so nothing to do here. */
474 void _initialize_tracefile ();
476 _initialize_tracefile ()
478 add_com ("tsave", class_trace
, tsave_command
, _("\
479 Save the trace data to a file.\n\
480 Use the '-ctf' option to save the data to CTF format.\n\
481 Use the '-r' option to direct the target to save directly to the file,\n\
482 using its own filesystem."));