1 /* Trace file support in GDB.
3 Copyright (C) 1997-2019 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 "common/byte-vector.h"
29 #define TRACE_WRITE_R_BLOCK(writer, buf, size) \
30 writer->ops->frame_ops->write_r_block ((writer), (buf), (size))
31 #define TRACE_WRITE_M_BLOCK_HEADER(writer, addr, size) \
32 writer->ops->frame_ops->write_m_block_header ((writer), (addr), \
34 #define TRACE_WRITE_M_BLOCK_MEMORY(writer, buf, size) \
35 writer->ops->frame_ops->write_m_block_memory ((writer), (buf), \
37 #define TRACE_WRITE_V_BLOCK(writer, num, val) \
38 writer->ops->frame_ops->write_v_block ((writer), (num), (val))
40 /* A unique pointer policy class for trace_file_writer. */
42 struct trace_file_writer_deleter
44 void operator() (struct trace_file_writer *writer)
46 writer->ops->dtor (writer);
51 /* A unique_ptr specialization for trace_file_writer. */
53 typedef std::unique_ptr<trace_file_writer, trace_file_writer_deleter>
56 /* Save tracepoint data to file named FILENAME through WRITER. WRITER
57 determines the trace file format. If TARGET_DOES_SAVE is non-zero,
58 the save is performed on the target, otherwise GDB obtains all trace
59 data and saves it locally. */
62 trace_save (const char *filename, struct trace_file_writer *writer,
65 struct trace_status *ts = current_trace_status ();
66 struct uploaded_tp *uploaded_tps = NULL, *utp;
67 struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;
70 #define MAX_TRACE_UPLOAD 2000
71 gdb::byte_vector buf (std::max (MAX_TRACE_UPLOAD, trace_regblock_size));
72 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
74 /* If the target is to save the data to a file on its own, then just
75 send the command and be done with it. */
78 if (!writer->ops->target_save (writer, filename))
79 error (_("Target failed to save trace data to '%s'."),
84 /* Get the trace status first before opening the file, so if the
85 target is losing, we can get out without touching files. Since
86 we're just calling this for side effects, we ignore the
88 target_get_trace_status (ts);
90 writer->ops->start (writer, filename);
92 writer->ops->write_header (writer);
94 /* Write descriptive info. */
96 /* Write out the size of a register block. */
97 writer->ops->write_regblock_type (writer, trace_regblock_size);
99 /* Write out the target description info. */
100 writer->ops->write_tdesc (writer);
102 /* Write out status of the tracing run (aka "tstatus" info). */
103 writer->ops->write_status (writer, ts);
105 /* Note that we want to upload tracepoints and save those, rather
106 than simply writing out the local ones, because the user may have
107 changed tracepoints in GDB in preparation for a future tracing
108 run, or maybe just mass-deleted all types of breakpoints as part
109 of cleaning up. So as not to contaminate the session, leave the
110 data in its uploaded form, don't make into real tracepoints. */
112 /* Get trace state variables first, they may be checked when parsing
113 uploaded commands. */
115 target_upload_trace_state_variables (&uploaded_tsvs);
117 for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
118 writer->ops->write_uploaded_tsv (writer, utsv);
120 free_uploaded_tsvs (&uploaded_tsvs);
122 target_upload_tracepoints (&uploaded_tps);
124 for (utp = uploaded_tps; utp; utp = utp->next)
125 target_get_tracepoint_status (NULL, utp);
127 for (utp = uploaded_tps; utp; utp = utp->next)
128 writer->ops->write_uploaded_tp (writer, utp);
130 free_uploaded_tps (&uploaded_tps);
132 /* Mark the end of the definition section. */
133 writer->ops->write_definition_end (writer);
135 /* Get and write the trace data proper. */
140 /* The writer supports writing the contents of trace buffer
141 directly to trace file. Don't parse the contents of trace
143 if (writer->ops->write_trace_buffer != NULL)
145 /* We ask for big blocks, in the hopes of efficiency, but
146 will take less if the target has packet size limitations
148 gotten = target_get_raw_trace_data (buf.data (), offset,
151 error (_("Failure to get requested trace buffer data"));
152 /* No more data is forthcoming, we're done. */
156 writer->ops->write_trace_buffer (writer, buf.data (), gotten);
164 /* Parse the trace buffers according to how data are stored
165 in trace buffer in GDBserver. */
167 gotten = target_get_raw_trace_data (buf.data (), offset, 6);
172 /* Read the first six bytes in, which is the tracepoint
173 number and trace frame size. */
175 extract_unsigned_integer (&((buf.data ())[0]), 2, byte_order);
178 extract_unsigned_integer (&((buf.data ())[2]), 4, byte_order);
180 writer->ops->frame_ops->start (writer, tp_num);
189 for (block = 0; block < tf_size; )
193 /* We'll fetch one block each time, in order to
194 handle the extremely large 'M' block. We first
195 fetch one byte to get the type of the block. */
196 gotten = target_get_raw_trace_data (buf.data (),
199 error (_("Failure to get requested trace buffer data"));
210 = target_get_raw_trace_data (buf.data (), offset,
211 trace_regblock_size);
212 if (gotten < trace_regblock_size)
213 error (_("Failure to get requested trace"
216 TRACE_WRITE_R_BLOCK (writer, buf.data (),
217 trace_regblock_size);
226 t = target_get_raw_trace_data (buf.data (),
229 error (_("Failure to get requested trace"
237 extract_unsigned_integer (buf.data (), 8,
239 mlen = (unsigned short)
240 extract_unsigned_integer (&((buf.data ())[8]), 2,
243 TRACE_WRITE_M_BLOCK_HEADER (writer, addr,
246 /* The memory contents in 'M' block may be
247 very large. Fetch the data from the target
248 and write them into file one by one. */
249 for (j = 0; j < mlen; )
251 unsigned int read_length;
253 if (mlen - j > MAX_TRACE_UPLOAD)
254 read_length = MAX_TRACE_UPLOAD;
256 read_length = mlen - j;
258 t = target_get_raw_trace_data (buf.data (),
262 error (_("Failure to get requested"
263 " trace buffer data"));
265 TRACE_WRITE_M_BLOCK_MEMORY (writer,
270 gotten += read_length;
281 = target_get_raw_trace_data (buf.data (),
284 error (_("Failure to get requested"
285 " trace buffer data"));
287 vnum = (int) extract_signed_integer (buf.data (),
291 = extract_signed_integer (&((buf.data ())[4]),
294 TRACE_WRITE_V_BLOCK (writer, vnum, val);
298 error (_("Unknown block type '%c' (0x%x) in"
300 block_type, block_type);
310 writer->ops->frame_ops->end (writer);
314 writer->ops->end (writer);
318 tsave_command (const char *args, int from_tty)
320 int target_does_save = 0;
322 char *filename = NULL;
323 int generate_ctf = 0;
326 error_no_arg (_("file in which to save trace data"));
328 gdb_argv built_argv (args);
329 argv = built_argv.get ();
331 for (; *argv; ++argv)
333 if (strcmp (*argv, "-r") == 0)
334 target_does_save = 1;
335 else if (strcmp (*argv, "-ctf") == 0)
337 else if (**argv == '-')
338 error (_("unknown option `%s'"), *argv);
344 error_no_arg (_("file in which to save trace data"));
347 trace_save_ctf (filename, target_does_save);
349 trace_save_tfile (filename, target_does_save);
352 printf_filtered (_("Trace data saved to %s '%s'.\n"),
353 generate_ctf ? "directory" : "file", filename);
356 /* Save the trace data to file FILENAME of tfile format. */
359 trace_save_tfile (const char *filename, int target_does_save)
361 trace_file_writer_up writer (tfile_trace_file_writer_new ());
362 trace_save (filename, writer.get (), target_does_save);
365 /* Save the trace data to dir DIRNAME of ctf format. */
368 trace_save_ctf (const char *dirname, int target_does_save)
370 trace_file_writer_up writer (ctf_trace_file_writer_new ());
371 trace_save (dirname, writer.get (), target_does_save);
374 /* Fetch register data from tracefile, shared for both tfile and
378 tracefile_fetch_registers (struct regcache *regcache, int regno)
380 struct gdbarch *gdbarch = regcache->arch ();
381 struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
384 /* We get here if no register data has been found. Mark registers
386 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
387 regcache->raw_supply (regn, NULL);
389 /* We can often usefully guess that the PC is going to be the same
390 as the address of the tracepoint. */
391 if (tp == NULL || tp->loc == NULL)
394 /* But don't try to guess if tracepoint is multi-location... */
397 warning (_("Tracepoint %d has multiple "
398 "locations, cannot infer $pc"),
402 /* ... or does while-stepping. */
403 else if (tp->step_count > 0)
405 warning (_("Tracepoint %d does while-stepping, "
411 /* Guess what we can from the tracepoint location. */
412 gdbarch_guess_tracepoint_registers (gdbarch, regcache,
416 /* This is the implementation of target_ops method to_has_all_memory. */
419 tracefile_target::has_all_memory ()
424 /* This is the implementation of target_ops method to_has_memory. */
427 tracefile_target::has_memory ()
432 /* This is the implementation of target_ops method to_has_stack.
433 The target has a stack when GDB has already selected one trace
437 tracefile_target::has_stack ()
439 return get_traceframe_number () != -1;
442 /* This is the implementation of target_ops method to_has_registers.
443 The target has registers when GDB has already selected one trace
447 tracefile_target::has_registers ()
449 return get_traceframe_number () != -1;
452 /* This is the implementation of target_ops method to_thread_alive.
453 tracefile has one thread faked by GDB. */
456 tracefile_target::thread_alive (ptid_t ptid)
461 /* This is the implementation of target_ops method to_get_trace_status.
462 The trace status for a file is that tracing can never be run. */
465 tracefile_target::get_trace_status (struct trace_status *ts)
467 /* Other bits of trace status were collected as part of opening the
468 trace files, so nothing to do here. */
474 _initialize_tracefile (void)
476 add_com ("tsave", class_trace, tsave_command, _("\
477 Save the trace data to a file.\n\
478 Use the '-ctf' option to save the data to CTF format.\n\
479 Use the '-r' option to direct the target to save directly to the file,\n\
480 using its own filesystem."));