update copyright year in version output of gdb, gdbserver and gdbreplay
[deliverable/binutils-gdb.git] / gdb / tracefile.c
CommitLineData
7951c4eb
YQ
1/* Trace file support in GDB.
2
42a4f53d 3 Copyright (C) 1997-2019 Free Software Foundation, Inc.
7951c4eb
YQ
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "tracefile.h"
518fe38c 22#include "tracectf.h"
1ca49d37 23#include "exec.h"
48b6e87e 24#include "regcache.h"
268a13a5 25#include "gdbsupport/byte-vector.h"
0d12e84c 26#include "gdbarch.h"
7951c4eb
YQ
27
28/* Helper macros. */
29
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), \
34 (size))
35#define TRACE_WRITE_M_BLOCK_MEMORY(writer, buf, size) \
36 writer->ops->frame_ops->write_m_block_memory ((writer), (buf), \
37 (size))
38#define TRACE_WRITE_V_BLOCK(writer, num, val) \
39 writer->ops->frame_ops->write_v_block ((writer), (num), (val))
40
d14b92bf 41/* A unique pointer policy class for trace_file_writer. */
7951c4eb 42
d14b92bf 43struct trace_file_writer_deleter
7951c4eb 44{
d14b92bf
TT
45 void operator() (struct trace_file_writer *writer)
46 {
47 writer->ops->dtor (writer);
48 xfree (writer);
49 }
50};
7951c4eb 51
d14b92bf
TT
52/* A unique_ptr specialization for trace_file_writer. */
53
54typedef std::unique_ptr<trace_file_writer, trace_file_writer_deleter>
55 trace_file_writer_up;
7951c4eb
YQ
56
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. */
61
62static void
63trace_save (const char *filename, struct trace_file_writer *writer,
64 int target_does_save)
65{
66 struct trace_status *ts = current_trace_status ();
7951c4eb
YQ
67 struct uploaded_tp *uploaded_tps = NULL, *utp;
68 struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;
69
70 ULONGEST offset = 0;
71#define MAX_TRACE_UPLOAD 2000
296956be 72 gdb::byte_vector buf (std::max (MAX_TRACE_UPLOAD, trace_regblock_size));
7951c4eb
YQ
73 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
74
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. */
77 if (target_does_save)
78 {
79 if (!writer->ops->target_save (writer, filename))
80 error (_("Target failed to save trace data to '%s'."),
81 filename);
82 return;
83 }
84
85 /* Get the trace status first before opening the file, so if the
ac298888
TT
86 target is losing, we can get out without touching files. Since
87 we're just calling this for side effects, we ignore the
88 result. */
89 target_get_trace_status (ts);
7951c4eb
YQ
90
91 writer->ops->start (writer, filename);
92
93 writer->ops->write_header (writer);
94
95 /* Write descriptive info. */
96
97 /* Write out the size of a register block. */
98 writer->ops->write_regblock_type (writer, trace_regblock_size);
99
18d3cec5
MK
100 /* Write out the target description info. */
101 writer->ops->write_tdesc (writer);
102
7951c4eb
YQ
103 /* Write out status of the tracing run (aka "tstatus" info). */
104 writer->ops->write_status (writer, ts);
105
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. */
112
113 /* Get trace state variables first, they may be checked when parsing
114 uploaded commands. */
115
116 target_upload_trace_state_variables (&uploaded_tsvs);
117
118 for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
119 writer->ops->write_uploaded_tsv (writer, utsv);
120
121 free_uploaded_tsvs (&uploaded_tsvs);
122
123 target_upload_tracepoints (&uploaded_tps);
124
125 for (utp = uploaded_tps; utp; utp = utp->next)
126 target_get_tracepoint_status (NULL, utp);
127
128 for (utp = uploaded_tps; utp; utp = utp->next)
129 writer->ops->write_uploaded_tp (writer, utp);
130
131 free_uploaded_tps (&uploaded_tps);
132
133 /* Mark the end of the definition section. */
134 writer->ops->write_definition_end (writer);
135
136 /* Get and write the trace data proper. */
137 while (1)
138 {
139 LONGEST gotten = 0;
140
141 /* The writer supports writing the contents of trace buffer
142 directly to trace file. Don't parse the contents of trace
143 buffer. */
144 if (writer->ops->write_trace_buffer != NULL)
145 {
146 /* We ask for big blocks, in the hopes of efficiency, but
147 will take less if the target has packet size limitations
148 or some such. */
296956be 149 gotten = target_get_raw_trace_data (buf.data (), offset,
7951c4eb
YQ
150 MAX_TRACE_UPLOAD);
151 if (gotten < 0)
152 error (_("Failure to get requested trace buffer data"));
153 /* No more data is forthcoming, we're done. */
154 if (gotten == 0)
155 break;
156
296956be 157 writer->ops->write_trace_buffer (writer, buf.data (), gotten);
7951c4eb
YQ
158
159 offset += gotten;
160 }
161 else
162 {
163 uint16_t tp_num;
164 uint32_t tf_size;
165 /* Parse the trace buffers according to how data are stored
166 in trace buffer in GDBserver. */
167
296956be 168 gotten = target_get_raw_trace_data (buf.data (), offset, 6);
7951c4eb
YQ
169
170 if (gotten == 0)
171 break;
172
173 /* Read the first six bytes in, which is the tracepoint
174 number and trace frame size. */
175 tp_num = (uint16_t)
296956be 176 extract_unsigned_integer (&((buf.data ())[0]), 2, byte_order);
7951c4eb
YQ
177
178 tf_size = (uint32_t)
296956be 179 extract_unsigned_integer (&((buf.data ())[2]), 4, byte_order);
7951c4eb
YQ
180
181 writer->ops->frame_ops->start (writer, tp_num);
182 gotten = 6;
183
184 if (tf_size > 0)
185 {
186 unsigned int block;
187
188 offset += 6;
189
190 for (block = 0; block < tf_size; )
191 {
192 gdb_byte block_type;
193
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. */
296956be
PFC
197 gotten = target_get_raw_trace_data (buf.data (),
198 offset, 1);
7951c4eb
YQ
199 if (gotten < 1)
200 error (_("Failure to get requested trace buffer data"));
201
202 gotten = 1;
203 block += 1;
204 offset += 1;
205
206 block_type = buf[0];
207 switch (block_type)
208 {
209 case 'R':
210 gotten
296956be 211 = target_get_raw_trace_data (buf.data (), offset,
7951c4eb
YQ
212 trace_regblock_size);
213 if (gotten < trace_regblock_size)
214 error (_("Failure to get requested trace"
215 " buffer data"));
216
296956be 217 TRACE_WRITE_R_BLOCK (writer, buf.data (),
7951c4eb
YQ
218 trace_regblock_size);
219 break;
220 case 'M':
221 {
222 unsigned short mlen;
223 ULONGEST addr;
224 LONGEST t;
225 int j;
226
296956be
PFC
227 t = target_get_raw_trace_data (buf.data (),
228 offset, 10);
7951c4eb
YQ
229 if (t < 10)
230 error (_("Failure to get requested trace"
231 " buffer data"));
232
233 offset += 10;
234 block += 10;
235
236 gotten = 0;
237 addr = (ULONGEST)
296956be 238 extract_unsigned_integer (buf.data (), 8,
7951c4eb
YQ
239 byte_order);
240 mlen = (unsigned short)
296956be 241 extract_unsigned_integer (&((buf.data ())[8]), 2,
7951c4eb
YQ
242 byte_order);
243
244 TRACE_WRITE_M_BLOCK_HEADER (writer, addr,
245 mlen);
246
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; )
251 {
252 unsigned int read_length;
253
254 if (mlen - j > MAX_TRACE_UPLOAD)
255 read_length = MAX_TRACE_UPLOAD;
256 else
257 read_length = mlen - j;
258
296956be 259 t = target_get_raw_trace_data (buf.data (),
7951c4eb
YQ
260 offset + j,
261 read_length);
262 if (t < read_length)
263 error (_("Failure to get requested"
264 " trace buffer data"));
265
296956be
PFC
266 TRACE_WRITE_M_BLOCK_MEMORY (writer,
267 buf.data (),
7951c4eb
YQ
268 read_length);
269
270 j += read_length;
271 gotten += read_length;
272 }
273
274 break;
275 }
276 case 'V':
277 {
278 int vnum;
279 LONGEST val;
280
281 gotten
296956be
PFC
282 = target_get_raw_trace_data (buf.data (),
283 offset, 12);
7951c4eb
YQ
284 if (gotten < 12)
285 error (_("Failure to get requested"
286 " trace buffer data"));
287
296956be 288 vnum = (int) extract_signed_integer (buf.data (),
7951c4eb
YQ
289 4,
290 byte_order);
291 val
296956be
PFC
292 = extract_signed_integer (&((buf.data ())[4]),
293 8, byte_order);
7951c4eb
YQ
294
295 TRACE_WRITE_V_BLOCK (writer, vnum, val);
296 }
297 break;
298 default:
299 error (_("Unknown block type '%c' (0x%x) in"
300 " trace frame"),
301 block_type, block_type);
302 }
303
304 block += gotten;
305 offset += gotten;
306 }
307 }
308 else
309 offset += gotten;
310
311 writer->ops->frame_ops->end (writer);
312 }
313 }
314
315 writer->ops->end (writer);
316}
317
318static void
0b39b52e 319tsave_command (const char *args, int from_tty)
7951c4eb
YQ
320{
321 int target_does_save = 0;
322 char **argv;
323 char *filename = NULL;
7951c4eb 324 int generate_ctf = 0;
7951c4eb
YQ
325
326 if (args == NULL)
327 error_no_arg (_("file in which to save trace data"));
328
773a1edc
TT
329 gdb_argv built_argv (args);
330 argv = built_argv.get ();
7951c4eb
YQ
331
332 for (; *argv; ++argv)
333 {
334 if (strcmp (*argv, "-r") == 0)
335 target_does_save = 1;
4a596fe2 336 else if (strcmp (*argv, "-ctf") == 0)
7951c4eb
YQ
337 generate_ctf = 1;
338 else if (**argv == '-')
339 error (_("unknown option `%s'"), *argv);
340 else
341 filename = *argv;
342 }
343
344 if (!filename)
345 error_no_arg (_("file in which to save trace data"));
346
347 if (generate_ctf)
d14b92bf 348 trace_save_ctf (filename, target_does_save);
7951c4eb 349 else
d14b92bf 350 trace_save_tfile (filename, target_does_save);
7951c4eb
YQ
351
352 if (from_tty)
353 printf_filtered (_("Trace data saved to %s '%s'.\n"),
354 generate_ctf ? "directory" : "file", filename);
7951c4eb
YQ
355}
356
357/* Save the trace data to file FILENAME of tfile format. */
358
359void
360trace_save_tfile (const char *filename, int target_does_save)
361{
d14b92bf
TT
362 trace_file_writer_up writer (tfile_trace_file_writer_new ());
363 trace_save (filename, writer.get (), target_does_save);
7951c4eb
YQ
364}
365
366/* Save the trace data to dir DIRNAME of ctf format. */
367
368void
369trace_save_ctf (const char *dirname, int target_does_save)
370{
d14b92bf
TT
371 trace_file_writer_up writer (ctf_trace_file_writer_new ());
372 trace_save (dirname, writer.get (), target_does_save);
7951c4eb
YQ
373}
374
48b6e87e
YQ
375/* Fetch register data from tracefile, shared for both tfile and
376 ctf. */
377
378void
379tracefile_fetch_registers (struct regcache *regcache, int regno)
380{
ac7936df 381 struct gdbarch *gdbarch = regcache->arch ();
5f034a78
MK
382 struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
383 int regn;
48b6e87e
YQ
384
385 /* We get here if no register data has been found. Mark registers
386 as unavailable. */
387 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
73e1c03f 388 regcache->raw_supply (regn, NULL);
48b6e87e
YQ
389
390 /* We can often usefully guess that the PC is going to be the same
391 as the address of the tracepoint. */
c1fc2657 392 if (tp == NULL || tp->loc == NULL)
48b6e87e
YQ
393 return;
394
5f034a78 395 /* But don't try to guess if tracepoint is multi-location... */
c1fc2657 396 if (tp->loc->next)
48b6e87e 397 {
5f034a78
MK
398 warning (_("Tracepoint %d has multiple "
399 "locations, cannot infer $pc"),
c1fc2657 400 tp->number);
5f034a78
MK
401 return;
402 }
403 /* ... or does while-stepping. */
404 else if (tp->step_count > 0)
405 {
406 warning (_("Tracepoint %d does while-stepping, "
407 "cannot infer $pc"),
c1fc2657 408 tp->number);
5f034a78 409 return;
48b6e87e 410 }
5f034a78
MK
411
412 /* Guess what we can from the tracepoint location. */
413 gdbarch_guess_tracepoint_registers (gdbarch, regcache,
c1fc2657 414 tp->loc->address);
48b6e87e
YQ
415}
416
a283690e
YQ
417/* This is the implementation of target_ops method to_has_all_memory. */
418
57810aa7 419bool
f6ac5f3d 420tracefile_target::has_all_memory ()
a283690e
YQ
421{
422 return 1;
423}
424
425/* This is the implementation of target_ops method to_has_memory. */
426
57810aa7 427bool
f6ac5f3d 428tracefile_target::has_memory ()
a283690e
YQ
429{
430 return 1;
431}
432
12e03cd0
YQ
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
435 frame. */
436
57810aa7 437bool
f6ac5f3d 438tracefile_target::has_stack ()
12e03cd0
YQ
439{
440 return get_traceframe_number () != -1;
441}
442
443/* This is the implementation of target_ops method to_has_registers.
444 The target has registers when GDB has already selected one trace
445 frame. */
446
57810aa7 447bool
f6ac5f3d 448tracefile_target::has_registers ()
12e03cd0
YQ
449{
450 return get_traceframe_number () != -1;
451}
452
453/* This is the implementation of target_ops method to_thread_alive.
454 tracefile has one thread faked by GDB. */
455
57810aa7 456bool
f6ac5f3d 457tracefile_target::thread_alive (ptid_t ptid)
12e03cd0
YQ
458{
459 return 1;
460}
461
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. */
464
f6ac5f3d
PA
465int
466tracefile_target::get_trace_status (struct trace_status *ts)
12e03cd0
YQ
467{
468 /* Other bits of trace status were collected as part of opening the
469 trace files, so nothing to do here. */
470
471 return -1;
472}
473
7951c4eb
YQ
474void
475_initialize_tracefile (void)
476{
e5a873b7 477 add_com ("tsave", class_trace, tsave_command, _("\
7951c4eb
YQ
478Save the trace data to a file.\n\
479Use the '-ctf' option to save the data to CTF format.\n\
480Use the '-r' option to direct the target to save directly to the file,\n\
481using its own filesystem."));
482}
This page took 0.808095 seconds and 4 git commands to generate.