gdb: Don't skip prologue for explicit line breakpoints in assembler
[deliverable/binutils-gdb.git] / gdb / tracefile-tfile.c
CommitLineData
7951c4eb
YQ
1/* Trace file TFILE format 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"
22#include "readline/tilde.h"
0747795c
TT
23#include "common/filestuff.h"
24#include "common/rsp-low.h" /* bin2hex */
11395323
YQ
25#include "regcache.h"
26#include "inferior.h"
27#include "gdbthread.h"
28#include "exec.h" /* exec_bfd */
29#include "completer.h"
30#include "filenames.h"
e909d859 31#include "remote.h"
18d3cec5 32#include "xml-tdesc.h"
5ac87a99 33#include "target-descriptions.h"
0747795c 34#include "common/buffer.h"
325fac50 35#include <algorithm>
11395323
YQ
36
37#ifndef O_LARGEFILE
38#define O_LARGEFILE 0
39#endif
7951c4eb 40
f6ac5f3d
PA
41/* The tfile target. */
42
d9f719f1
PA
43static const target_info tfile_target_info = {
44 "tfile",
45 N_("Local trace dump file"),
46 N_("Use a trace file as a target. Specify the filename of the trace file.")
47};
48
f6ac5f3d
PA
49class tfile_target final : public tracefile_target
50{
51 public:
d9f719f1
PA
52 const target_info &info () const override
53 { return tfile_target_info; }
f6ac5f3d 54
f6ac5f3d
PA
55 void close () override;
56 void fetch_registers (struct regcache *, int) override;
57 enum target_xfer_status xfer_partial (enum target_object object,
58 const char *annex,
59 gdb_byte *readbuf,
60 const gdb_byte *writebuf,
61 ULONGEST offset, ULONGEST len,
62 ULONGEST *xfered_len) override;
63 void files_info () override;
64 int trace_find (enum trace_find_type type, int num,
65 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override;
57810aa7 66 bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
f6ac5f3d
PA
67 traceframe_info_up traceframe_info () override;
68
69 void get_tracepoint_status (struct breakpoint *tp,
70 struct uploaded_tp *utp) override;
71};
72
7951c4eb
YQ
73/* TFILE trace writer. */
74
75struct tfile_trace_file_writer
76{
77 struct trace_file_writer base;
78
79 /* File pointer to tfile trace file. */
80 FILE *fp;
81 /* Path name of the tfile trace file. */
82 char *pathname;
83};
84
85/* This is the implementation of trace_file_write_ops method
86 target_save. We just call the generic target
87 target_save_trace_data to do target-side saving. */
88
89static int
90tfile_target_save (struct trace_file_writer *self,
91 const char *filename)
92{
93 int err = target_save_trace_data (filename);
94
95 return (err >= 0);
96}
97
98/* This is the implementation of trace_file_write_ops method
99 dtor. */
100
101static void
102tfile_dtor (struct trace_file_writer *self)
103{
104 struct tfile_trace_file_writer *writer
105 = (struct tfile_trace_file_writer *) self;
106
107 xfree (writer->pathname);
108
109 if (writer->fp != NULL)
110 fclose (writer->fp);
111}
112
113/* This is the implementation of trace_file_write_ops method
114 start. It creates the trace file FILENAME and registers some
115 cleanups. */
116
117static void
118tfile_start (struct trace_file_writer *self, const char *filename)
119{
120 struct tfile_trace_file_writer *writer
121 = (struct tfile_trace_file_writer *) self;
122
123 writer->pathname = tilde_expand (filename);
d419f42d 124 writer->fp = gdb_fopen_cloexec (writer->pathname, "wb").release ();
7951c4eb
YQ
125 if (writer->fp == NULL)
126 error (_("Unable to open file '%s' for saving trace data (%s)"),
127 writer->pathname, safe_strerror (errno));
128}
129
130/* This is the implementation of trace_file_write_ops method
131 write_header. Write the TFILE header. */
132
133static void
134tfile_write_header (struct trace_file_writer *self)
135{
136 struct tfile_trace_file_writer *writer
137 = (struct tfile_trace_file_writer *) self;
138 int written;
139
140 /* Write a file header, with a high-bit-set char to indicate a
141 binary file, plus a hint as what this file is, and a version
142 number in case of future needs. */
143 written = fwrite ("\x7fTRACE0\n", 8, 1, writer->fp);
144 if (written < 1)
145 perror_with_name (writer->pathname);
146}
147
148/* This is the implementation of trace_file_write_ops method
149 write_regblock_type. Write the size of register block. */
150
151static void
152tfile_write_regblock_type (struct trace_file_writer *self, int size)
153{
154 struct tfile_trace_file_writer *writer
155 = (struct tfile_trace_file_writer *) self;
156
157 fprintf (writer->fp, "R %x\n", size);
158}
159
160/* This is the implementation of trace_file_write_ops method
161 write_status. */
162
163static void
164tfile_write_status (struct trace_file_writer *self,
165 struct trace_status *ts)
166{
167 struct tfile_trace_file_writer *writer
168 = (struct tfile_trace_file_writer *) self;
169
170 fprintf (writer->fp, "status %c;%s",
171 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
172 if (ts->stop_reason == tracepoint_error
e5a873b7 173 || ts->stop_reason == trace_stop_command)
7951c4eb
YQ
174 {
175 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
176
177 bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
178 fprintf (writer->fp, ":%s", buf);
179 }
180 fprintf (writer->fp, ":%x", ts->stopping_tracepoint);
181 if (ts->traceframe_count >= 0)
182 fprintf (writer->fp, ";tframes:%x", ts->traceframe_count);
183 if (ts->traceframes_created >= 0)
184 fprintf (writer->fp, ";tcreated:%x", ts->traceframes_created);
185 if (ts->buffer_free >= 0)
186 fprintf (writer->fp, ";tfree:%x", ts->buffer_free);
187 if (ts->buffer_size >= 0)
188 fprintf (writer->fp, ";tsize:%x", ts->buffer_size);
189 if (ts->disconnected_tracing)
190 fprintf (writer->fp, ";disconn:%x", ts->disconnected_tracing);
191 if (ts->circular_buffer)
192 fprintf (writer->fp, ";circular:%x", ts->circular_buffer);
193 if (ts->start_time)
194 {
195 fprintf (writer->fp, ";starttime:%s",
196 phex_nz (ts->start_time, sizeof (ts->start_time)));
197 }
198 if (ts->stop_time)
199 {
200 fprintf (writer->fp, ";stoptime:%s",
201 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
202 }
203 if (ts->notes != NULL)
204 {
205 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
206
207 bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
208 fprintf (writer->fp, ";notes:%s", buf);
209 }
210 if (ts->user_name != NULL)
211 {
212 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
213
214 bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
215 fprintf (writer->fp, ";username:%s", buf);
216 }
217 fprintf (writer->fp, "\n");
218}
219
220/* This is the implementation of trace_file_write_ops method
221 write_uploaded_tsv. */
222
223static void
224tfile_write_uploaded_tsv (struct trace_file_writer *self,
225 struct uploaded_tsv *utsv)
226{
a121b7c1 227 char *buf = NULL;
7951c4eb
YQ
228 struct tfile_trace_file_writer *writer
229 = (struct tfile_trace_file_writer *) self;
230
231 if (utsv->name)
232 {
233 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
234 bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
235 }
236
237 fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
238 utsv->number, phex_nz (utsv->initial_value, 8),
a121b7c1 239 utsv->builtin, buf != NULL ? buf : "");
7951c4eb
YQ
240
241 if (utsv->name)
242 xfree (buf);
243}
244
245#define MAX_TRACE_UPLOAD 2000
246
247/* This is the implementation of trace_file_write_ops method
248 write_uploaded_tp. */
249
250static void
251tfile_write_uploaded_tp (struct trace_file_writer *self,
252 struct uploaded_tp *utp)
253{
254 struct tfile_trace_file_writer *writer
255 = (struct tfile_trace_file_writer *) self;
7951c4eb
YQ
256 char buf[MAX_TRACE_UPLOAD];
257
258 fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
259 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
260 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
261 if (utp->type == bp_fast_tracepoint)
262 fprintf (writer->fp, ":F%x", utp->orig_size);
263 if (utp->cond)
264 fprintf (writer->fp,
67aa1f3c
PA
265 ":X%x,%s", (unsigned int) strlen (utp->cond.get ()) / 2,
266 utp->cond.get ());
7951c4eb 267 fprintf (writer->fp, "\n");
67aa1f3c 268 for (const auto &act : utp->actions)
7951c4eb 269 fprintf (writer->fp, "tp A%x:%s:%s\n",
67aa1f3c
PA
270 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
271 for (const auto &act : utp->step_actions)
7951c4eb 272 fprintf (writer->fp, "tp S%x:%s:%s\n",
67aa1f3c 273 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
7951c4eb
YQ
274 if (utp->at_string)
275 {
276 encode_source_string (utp->number, utp->addr,
67aa1f3c
PA
277 "at", utp->at_string.get (),
278 buf, MAX_TRACE_UPLOAD);
7951c4eb
YQ
279 fprintf (writer->fp, "tp Z%s\n", buf);
280 }
281 if (utp->cond_string)
282 {
283 encode_source_string (utp->number, utp->addr,
67aa1f3c 284 "cond", utp->cond_string.get (),
7951c4eb
YQ
285 buf, MAX_TRACE_UPLOAD);
286 fprintf (writer->fp, "tp Z%s\n", buf);
287 }
67aa1f3c 288 for (const auto &act : utp->cmd_strings)
7951c4eb 289 {
67aa1f3c 290 encode_source_string (utp->number, utp->addr, "cmd", act.get (),
7951c4eb
YQ
291 buf, MAX_TRACE_UPLOAD);
292 fprintf (writer->fp, "tp Z%s\n", buf);
293 }
294 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
295 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
296 utp->hit_count,
297 phex_nz (utp->traceframe_usage,
298 sizeof (utp->traceframe_usage)));
299}
300
18d3cec5
MK
301/* This is the implementation of trace_file_write_ops method
302 write_tdesc. */
303
304static void
305tfile_write_tdesc (struct trace_file_writer *self)
306{
307 struct tfile_trace_file_writer *writer
308 = (struct tfile_trace_file_writer *) self;
18d3cec5 309
bd8a901f 310 gdb::optional<std::string> tdesc
8b88a78e 311 = target_fetch_description_xml (current_top_target ());
bd8a901f
PA
312
313 if (!tdesc)
18d3cec5
MK
314 return;
315
bd8a901f
PA
316 const char *ptr = tdesc->c_str ();
317
18d3cec5
MK
318 /* Write tdesc line by line, prefixing each line with "tdesc ". */
319 while (ptr != NULL)
320 {
bd8a901f 321 const char *next = strchr (ptr, '\n');
18d3cec5
MK
322 if (next != NULL)
323 {
324 fprintf (writer->fp, "tdesc %.*s\n", (int) (next - ptr), ptr);
325 /* Skip the \n. */
326 next++;
327 }
328 else if (*ptr != '\0')
329 {
330 /* Last line, doesn't have a newline. */
331 fprintf (writer->fp, "tdesc %s\n", ptr);
332 }
333 ptr = next;
334 }
18d3cec5
MK
335}
336
7951c4eb
YQ
337/* This is the implementation of trace_file_write_ops method
338 write_definition_end. */
339
340static void
341tfile_write_definition_end (struct trace_file_writer *self)
342{
343 struct tfile_trace_file_writer *writer
344 = (struct tfile_trace_file_writer *) self;
345
346 fprintf (writer->fp, "\n");
347}
348
349/* This is the implementation of trace_file_write_ops method
350 write_raw_data. */
351
352static void
353tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
354 LONGEST len)
355{
356 struct tfile_trace_file_writer *writer
357 = (struct tfile_trace_file_writer *) self;
358
359 if (fwrite (buf, len, 1, writer->fp) < 1)
360 perror_with_name (writer->pathname);
361}
362
363/* This is the implementation of trace_file_write_ops method
364 end. */
365
366static void
367tfile_end (struct trace_file_writer *self)
368{
369 struct tfile_trace_file_writer *writer
370 = (struct tfile_trace_file_writer *) self;
371 uint32_t gotten = 0;
372
373 /* Mark the end of trace data. */
374 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
375 perror_with_name (writer->pathname);
376}
377
378/* Operations to write trace buffers into TFILE format. */
379
380static const struct trace_file_write_ops tfile_write_ops =
381{
382 tfile_dtor,
383 tfile_target_save,
384 tfile_start,
385 tfile_write_header,
386 tfile_write_regblock_type,
387 tfile_write_status,
388 tfile_write_uploaded_tsv,
389 tfile_write_uploaded_tp,
18d3cec5 390 tfile_write_tdesc,
7951c4eb
YQ
391 tfile_write_definition_end,
392 tfile_write_raw_data,
393 NULL,
394 tfile_end,
395};
396
397/* Return a trace writer for TFILE format. */
398
399struct trace_file_writer *
400tfile_trace_file_writer_new (void)
401{
402 struct tfile_trace_file_writer *writer
8d749320 403 = XNEW (struct tfile_trace_file_writer);
7951c4eb
YQ
404
405 writer->base.ops = &tfile_write_ops;
406 writer->fp = NULL;
407 writer->pathname = NULL;
408
409 return (struct trace_file_writer *) writer;
410}
11395323
YQ
411
412/* target tfile command */
413
f6ac5f3d 414static tfile_target tfile_ops;
11395323
YQ
415
416#define TRACE_HEADER_SIZE 8
417
418#define TFILE_PID (1)
419
420static char *trace_filename;
421static int trace_fd = -1;
422static off_t trace_frames_offset;
423static off_t cur_offset;
424static int cur_data_size;
425int trace_regblock_size;
5ac87a99 426static struct buffer trace_tdesc;
11395323 427
5ac87a99 428static void tfile_append_tdesc_line (const char *line);
11395323
YQ
429static void tfile_interp_line (char *line,
430 struct uploaded_tp **utpp,
431 struct uploaded_tsv **utsvp);
432
433/* Read SIZE bytes into READBUF from the trace frame, starting at
434 TRACE_FD's current position. Note that this call `read'
435 underneath, hence it advances the file's seek position. Throws an
436 error if the `read' syscall fails, or less than SIZE bytes are
437 read. */
438
439static void
440tfile_read (gdb_byte *readbuf, int size)
441{
442 int gotten;
443
444 gotten = read (trace_fd, readbuf, size);
445 if (gotten < 0)
446 perror_with_name (trace_filename);
447 else if (gotten < size)
448 error (_("Premature end of file while reading trace file"));
449}
450
d9f719f1
PA
451/* Open the tfile target. */
452
453static void
454tfile_target_open (const char *arg, int from_tty)
11395323 455{
11395323
YQ
456 int flags;
457 int scratch_chan;
458 char header[TRACE_HEADER_SIZE];
459 char linebuf[1000]; /* Should be max remote packet size or so. */
460 gdb_byte byte;
461 int bytes, i;
462 struct trace_status *ts;
463 struct uploaded_tp *uploaded_tps = NULL;
464 struct uploaded_tsv *uploaded_tsvs = NULL;
465
466 target_preopen (from_tty);
014f9477 467 if (!arg)
11395323
YQ
468 error (_("No trace file specified."));
469
ee0c3293
TT
470 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
471 if (!IS_ABSOLUTE_PATH (filename.get ()))
472 filename.reset (concat (current_directory, "/", filename.get (),
473 (char *) NULL));
11395323
YQ
474
475 flags = O_BINARY | O_LARGEFILE;
476 flags |= O_RDONLY;
ee0c3293 477 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
11395323 478 if (scratch_chan < 0)
ee0c3293 479 perror_with_name (filename.get ());
11395323
YQ
480
481 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
482
11395323
YQ
483 unpush_target (&tfile_ops);
484
ee0c3293 485 trace_filename = filename.release ();
11395323
YQ
486 trace_fd = scratch_chan;
487
5ac87a99
MK
488 /* Make sure this is clear. */
489 buffer_free (&trace_tdesc);
490
11395323
YQ
491 bytes = 0;
492 /* Read the file header and test for validity. */
493 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
494
495 bytes += TRACE_HEADER_SIZE;
496 if (!(header[0] == 0x7f
61012eef 497 && (startswith (header + 1, "TRACE0\n"))))
11395323
YQ
498 error (_("File is not a valid trace file."));
499
500 push_target (&tfile_ops);
501
502 trace_regblock_size = 0;
503 ts = current_trace_status ();
504 /* We know we're working with a file. Record its name. */
505 ts->filename = trace_filename;
506 /* Set defaults in case there is no status line. */
507 ts->running_known = 0;
508 ts->stop_reason = trace_stop_reason_unknown;
509 ts->traceframe_count = -1;
510 ts->buffer_free = 0;
511 ts->disconnected_tracing = 0;
512 ts->circular_buffer = 0;
513
a70b8144 514 try
11395323
YQ
515 {
516 /* Read through a section of newline-terminated lines that
517 define things like tracepoints. */
518 i = 0;
519 while (1)
520 {
521 tfile_read (&byte, 1);
522
523 ++bytes;
524 if (byte == '\n')
525 {
526 /* Empty line marks end of the definition section. */
527 if (i == 0)
528 break;
529 linebuf[i] = '\0';
530 i = 0;
531 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
532 }
533 else
534 linebuf[i++] = byte;
535 if (i >= 1000)
536 error (_("Excessively long lines in trace file"));
537 }
538
5ac87a99
MK
539 /* By now, tdesc lines have been read from tfile - let's parse them. */
540 target_find_description ();
541
11395323
YQ
542 /* Record the starting offset of the binary trace data. */
543 trace_frames_offset = bytes;
544
545 /* If we don't have a blocksize, we can't interpret the
546 traceframes. */
547 if (trace_regblock_size == 0)
548 error (_("No register block size recorded in trace file"));
549 }
230d2906 550 catch (const gdb_exception &ex)
11395323
YQ
551 {
552 /* Remove the partially set up target. */
553 unpush_target (&tfile_ops);
eedc3f4f 554 throw;
11395323
YQ
555 }
556
557 inferior_appeared (current_inferior (), TFILE_PID);
f2907e49 558 inferior_ptid = ptid_t (TFILE_PID);
11395323
YQ
559 add_thread_silent (inferior_ptid);
560
561 if (ts->traceframe_count <= 0)
562 warning (_("No traceframes present in this file."));
563
564 /* Add the file's tracepoints and variables into the current mix. */
565
566 /* Get trace state variables first, they may be checked when parsing
567 uploaded commands. */
568 merge_uploaded_trace_state_variables (&uploaded_tsvs);
569
570 merge_uploaded_tracepoints (&uploaded_tps);
571
572 post_create_inferior (&tfile_ops, from_tty);
573}
574
575/* Interpret the given line from the definitions part of the trace
576 file. */
577
578static void
579tfile_interp_line (char *line, struct uploaded_tp **utpp,
580 struct uploaded_tsv **utsvp)
581{
582 char *p = line;
583
61012eef 584 if (startswith (p, "R "))
11395323
YQ
585 {
586 p += strlen ("R ");
587 trace_regblock_size = strtol (p, &p, 16);
588 }
61012eef 589 else if (startswith (p, "status "))
11395323
YQ
590 {
591 p += strlen ("status ");
592 parse_trace_status (p, current_trace_status ());
593 }
61012eef 594 else if (startswith (p, "tp "))
11395323
YQ
595 {
596 p += strlen ("tp ");
597 parse_tracepoint_definition (p, utpp);
598 }
61012eef 599 else if (startswith (p, "tsv "))
11395323
YQ
600 {
601 p += strlen ("tsv ");
602 parse_tsv_definition (p, utsvp);
603 }
5ac87a99
MK
604 else if (startswith (p, "tdesc "))
605 {
606 p += strlen ("tdesc ");
607 tfile_append_tdesc_line (p);
608 }
11395323
YQ
609 else
610 warning (_("Ignoring trace file definition \"%s\""), line);
611}
612
613/* Close the trace file and generally clean up. */
614
f6ac5f3d
PA
615void
616tfile_target::close ()
11395323 617{
11395323
YQ
618 if (trace_fd < 0)
619 return;
620
11395323 621 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
00431a78 622 exit_inferior_silent (current_inferior ());
11395323 623
f6ac5f3d 624 ::close (trace_fd);
11395323
YQ
625 trace_fd = -1;
626 xfree (trace_filename);
627 trace_filename = NULL;
5ac87a99 628 buffer_free (&trace_tdesc);
11395323
YQ
629
630 trace_reset_local_state ();
631}
632
f6ac5f3d
PA
633void
634tfile_target::files_info ()
11395323
YQ
635{
636 printf_filtered ("\t`%s'\n", trace_filename);
637}
638
f6ac5f3d
PA
639void
640tfile_target::get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
11395323
YQ
641{
642 /* Other bits of trace status were collected as part of opening the
643 trace files, so nothing to do here. */
644}
645
646/* Given the position of a traceframe in the file, figure out what
647 address the frame was collected at. This would normally be the
648 value of a collected PC register, but if not available, we
649 improvise. */
650
651static CORE_ADDR
652tfile_get_traceframe_address (off_t tframe_offset)
653{
654 CORE_ADDR addr = 0;
655 short tpnum;
656 struct tracepoint *tp;
657 off_t saved_offset = cur_offset;
658
659 /* FIXME dig pc out of collected registers. */
660
661 /* Fall back to using tracepoint address. */
662 lseek (trace_fd, tframe_offset, SEEK_SET);
663 tfile_read ((gdb_byte *) &tpnum, 2);
664 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
665 gdbarch_byte_order
666 (target_gdbarch ()));
667
668 tp = get_tracepoint_by_number_on_target (tpnum);
669 /* FIXME this is a poor heuristic if multiple locations. */
c1fc2657
SM
670 if (tp && tp->loc)
671 addr = tp->loc->address;
11395323
YQ
672
673 /* Restore our seek position. */
674 cur_offset = saved_offset;
675 lseek (trace_fd, cur_offset, SEEK_SET);
676 return addr;
677}
678
679/* Given a type of search and some parameters, scan the collection of
680 traceframes in the file looking for a match. When found, return
681 both the traceframe and tracepoint number, otherwise -1 for
682 each. */
683
f6ac5f3d
PA
684int
685tfile_target::trace_find (enum trace_find_type type, int num,
686 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
11395323
YQ
687{
688 short tpnum;
689 int tfnum = 0, found = 0;
690 unsigned int data_size;
691 struct tracepoint *tp;
692 off_t offset, tframe_offset;
693 CORE_ADDR tfaddr;
694
695 if (num == -1)
696 {
697 if (tpp)
698 *tpp = -1;
699 return -1;
700 }
701
702 lseek (trace_fd, trace_frames_offset, SEEK_SET);
703 offset = trace_frames_offset;
704 while (1)
705 {
706 tframe_offset = offset;
707 tfile_read ((gdb_byte *) &tpnum, 2);
708 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
709 gdbarch_byte_order
710 (target_gdbarch ()));
711 offset += 2;
712 if (tpnum == 0)
713 break;
714 tfile_read ((gdb_byte *) &data_size, 4);
715 data_size = (unsigned int) extract_unsigned_integer
716 ((gdb_byte *) &data_size, 4,
717 gdbarch_byte_order (target_gdbarch ()));
718 offset += 4;
719
720 if (type == tfind_number)
721 {
722 /* Looking for a specific trace frame. */
723 if (tfnum == num)
724 found = 1;
725 }
726 else
727 {
728 /* Start from the _next_ trace frame. */
729 if (tfnum > get_traceframe_number ())
730 {
731 switch (type)
732 {
733 case tfind_pc:
734 tfaddr = tfile_get_traceframe_address (tframe_offset);
735 if (tfaddr == addr1)
736 found = 1;
737 break;
738 case tfind_tp:
739 tp = get_tracepoint (num);
740 if (tp && tpnum == tp->number_on_target)
741 found = 1;
742 break;
743 case tfind_range:
744 tfaddr = tfile_get_traceframe_address (tframe_offset);
745 if (addr1 <= tfaddr && tfaddr <= addr2)
746 found = 1;
747 break;
748 case tfind_outside:
749 tfaddr = tfile_get_traceframe_address (tframe_offset);
750 if (!(addr1 <= tfaddr && tfaddr <= addr2))
751 found = 1;
752 break;
753 default:
754 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
755 }
756 }
757 }
758
759 if (found)
760 {
761 if (tpp)
762 *tpp = tpnum;
763 cur_offset = offset;
764 cur_data_size = data_size;
765
766 return tfnum;
767 }
768 /* Skip past the traceframe's data. */
769 lseek (trace_fd, data_size, SEEK_CUR);
770 offset += data_size;
771 /* Update our own count of traceframes. */
772 ++tfnum;
773 }
774 /* Did not find what we were looking for. */
775 if (tpp)
776 *tpp = -1;
777 return -1;
778}
779
780/* Prototype of the callback passed to tframe_walk_blocks. */
781typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
782
783/* Callback for traceframe_walk_blocks, used to find a given block
784 type in a traceframe. */
785
786static int
787match_blocktype (char blocktype, void *data)
788{
19ba03f4 789 char *wantedp = (char *) data;
11395323
YQ
790
791 if (*wantedp == blocktype)
792 return 1;
793
794 return 0;
795}
796
797/* Walk over all traceframe block starting at POS offset from
798 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
799 unmodified. If CALLBACK returns true, this returns the position in
800 the traceframe where the block is found, relative to the start of
801 the traceframe (cur_offset). Returns -1 if no callback call
802 returned true, indicating that all blocks have been walked. */
803
804static int
805traceframe_walk_blocks (walk_blocks_callback_func callback,
806 int pos, void *data)
807{
808 /* Iterate through a traceframe's blocks, looking for a block of the
809 requested type. */
810
811 lseek (trace_fd, cur_offset + pos, SEEK_SET);
812 while (pos < cur_data_size)
813 {
814 unsigned short mlen;
815 char block_type;
816
817 tfile_read ((gdb_byte *) &block_type, 1);
818
819 ++pos;
820
821 if ((*callback) (block_type, data))
822 return pos;
823
824 switch (block_type)
825 {
826 case 'R':
827 lseek (trace_fd, cur_offset + pos + trace_regblock_size, SEEK_SET);
828 pos += trace_regblock_size;
829 break;
830 case 'M':
831 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
832 tfile_read ((gdb_byte *) &mlen, 2);
833 mlen = (unsigned short)
834 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
835 gdbarch_byte_order
836 (target_gdbarch ()));
837 lseek (trace_fd, mlen, SEEK_CUR);
838 pos += (8 + 2 + mlen);
839 break;
840 case 'V':
841 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
842 pos += (4 + 8);
843 break;
844 default:
845 error (_("Unknown block type '%c' (0x%x) in trace frame"),
846 block_type, block_type);
847 break;
848 }
849 }
850
851 return -1;
852}
853
854/* Convenience wrapper around traceframe_walk_blocks. Looks for the
855 position offset of a block of type TYPE_WANTED in the current trace
856 frame, starting at POS. Returns -1 if no such block was found. */
857
858static int
859traceframe_find_block_type (char type_wanted, int pos)
860{
861 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
862}
863
864/* Look for a block of saved registers in the traceframe, and get the
865 requested register from it. */
866
f6ac5f3d
PA
867void
868tfile_target::fetch_registers (struct regcache *regcache, int regno)
11395323 869{
ac7936df 870 struct gdbarch *gdbarch = regcache->arch ();
e909d859 871 int offset, regn, regsize, dummy;
11395323
YQ
872
873 /* An uninitialized reg size says we're not going to be
874 successful at getting register blocks. */
875 if (!trace_regblock_size)
876 return;
877
11395323
YQ
878 if (traceframe_find_block_type ('R', 0) >= 0)
879 {
224c3ddb 880 gdb_byte *regs = (gdb_byte *) alloca (trace_regblock_size);
48b6e87e 881
11395323
YQ
882 tfile_read (regs, trace_regblock_size);
883
11395323
YQ
884 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
885 {
ac7936df 886 if (!remote_register_number_and_offset (regcache->arch (),
e909d859
MK
887 regn, &dummy, &offset))
888 continue;
889
11395323
YQ
890 regsize = register_size (gdbarch, regn);
891 /* Make sure we stay within block bounds. */
473b99e5 892 if (offset + regsize > trace_regblock_size)
11395323 893 break;
0ec9f114 894 if (regcache->get_register_status (regn) == REG_UNKNOWN)
11395323
YQ
895 {
896 if (regno == regn)
897 {
73e1c03f 898 regcache->raw_supply (regno, regs + offset);
11395323
YQ
899 break;
900 }
901 else if (regno == -1)
902 {
73e1c03f 903 regcache->raw_supply (regn, regs + offset);
11395323
YQ
904 }
905 }
11395323 906 }
11395323 907 }
48b6e87e
YQ
908 else
909 tracefile_fetch_registers (regcache, regno);
11395323
YQ
910}
911
5ac87a99 912static enum target_xfer_status
f6ac5f3d 913tfile_xfer_partial_features (const char *annex,
5ac87a99
MK
914 gdb_byte *readbuf, const gdb_byte *writebuf,
915 ULONGEST offset, ULONGEST len,
916 ULONGEST *xfered_len)
917{
918 if (strcmp (annex, "target.xml"))
919 return TARGET_XFER_E_IO;
920
921 if (readbuf == NULL)
922 error (_("tfile_xfer_partial: tdesc is read-only"));
923
924 if (trace_tdesc.used_size == 0)
925 return TARGET_XFER_E_IO;
926
927 if (offset >= trace_tdesc.used_size)
928 return TARGET_XFER_EOF;
929
930 if (len > trace_tdesc.used_size - offset)
931 len = trace_tdesc.used_size - offset;
932
933 memcpy (readbuf, trace_tdesc.buffer + offset, len);
934 *xfered_len = len;
935
936 return TARGET_XFER_OK;
937}
938
f6ac5f3d
PA
939enum target_xfer_status
940tfile_target::xfer_partial (enum target_object object,
941 const char *annex, gdb_byte *readbuf,
942 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
943 ULONGEST *xfered_len)
11395323 944{
5ac87a99
MK
945 /* We're only doing regular memory and tdesc for now. */
946 if (object == TARGET_OBJECT_AVAILABLE_FEATURES)
f6ac5f3d 947 return tfile_xfer_partial_features (annex, readbuf, writebuf,
5ac87a99 948 offset, len, xfered_len);
11395323
YQ
949 if (object != TARGET_OBJECT_MEMORY)
950 return TARGET_XFER_E_IO;
951
952 if (readbuf == NULL)
953 error (_("tfile_xfer_partial: trace file is read-only"));
954
955 if (get_traceframe_number () != -1)
956 {
957 int pos = 0;
8acf9577 958 enum target_xfer_status res;
290a839c
YQ
959 /* Records the lowest available address of all blocks that
960 intersects the requested range. */
961 ULONGEST low_addr_available = 0;
11395323
YQ
962
963 /* Iterate through the traceframe's blocks, looking for
964 memory. */
965 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
966 {
967 ULONGEST maddr, amt;
968 unsigned short mlen;
969 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
970
971 tfile_read ((gdb_byte *) &maddr, 8);
972 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
973 byte_order);
974 tfile_read ((gdb_byte *) &mlen, 2);
975 mlen = (unsigned short)
976 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
977
978 /* If the block includes the first part of the desired
979 range, return as much it has; GDB will re-request the
980 remainder, which might be in a different block of this
981 trace frame. */
982 if (maddr <= offset && offset < (maddr + mlen))
983 {
984 amt = (maddr + mlen) - offset;
985 if (amt > len)
986 amt = len;
987
988 if (maddr != offset)
989 lseek (trace_fd, offset - maddr, SEEK_CUR);
990 tfile_read (readbuf, amt);
991 *xfered_len = amt;
992 return TARGET_XFER_OK;
993 }
994
290a839c
YQ
995 if (offset < maddr && maddr < (offset + len))
996 if (low_addr_available == 0 || low_addr_available > maddr)
997 low_addr_available = maddr;
998
11395323
YQ
999 /* Skip over this block. */
1000 pos += (8 + 2 + mlen);
1001 }
11395323 1002
8acf9577
YQ
1003 /* Requested memory is unavailable in the context of traceframes,
1004 and this address falls within a read-only section, fallback
290a839c
YQ
1005 to reading from executable, up to LOW_ADDR_AVAILABLE. */
1006 if (offset < low_addr_available)
325fac50 1007 len = std::min (len, low_addr_available - offset);
8acf9577
YQ
1008 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1009
1010 if (res == TARGET_XFER_OK)
1011 return TARGET_XFER_OK;
1012 else
1013 {
1014 /* No use trying further, we know some memory starting
1015 at MEMADDR isn't available. */
1016 *xfered_len = len;
1017 return TARGET_XFER_UNAVAILABLE;
1018 }
1ee79381
YQ
1019 }
1020 else
1021 {
1022 /* Fallback to reading from read-only sections. */
1023 return section_table_read_available_memory (readbuf, offset, len,
1024 xfered_len);
1025 }
11395323
YQ
1026}
1027
1028/* Iterate through the blocks of a trace frame, looking for a 'V'
1029 block with a matching tsv number. */
1030
57810aa7 1031bool
f6ac5f3d 1032tfile_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
11395323
YQ
1033{
1034 int pos;
57810aa7 1035 bool found = false;
11395323
YQ
1036
1037 /* Iterate over blocks in current frame and find the last 'V'
1038 block in which tsv number is TSVNUM. In one trace frame, there
1039 may be multiple 'V' blocks created for a given trace variable,
1040 and the last matched 'V' block contains the updated value. */
1041 pos = 0;
1042 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
1043 {
1044 int vnum;
1045
1046 tfile_read ((gdb_byte *) &vnum, 4);
1047 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
1048 gdbarch_byte_order
1049 (target_gdbarch ()));
1050 if (tsvnum == vnum)
1051 {
1052 tfile_read ((gdb_byte *) val, 8);
1053 *val = extract_signed_integer ((gdb_byte *) val, 8,
1054 gdbarch_byte_order
1055 (target_gdbarch ()));
57810aa7 1056 found = true;
11395323
YQ
1057 }
1058 pos += (4 + 8);
1059 }
1060
1061 return found;
1062}
1063
11395323
YQ
1064/* Callback for traceframe_walk_blocks. Builds a traceframe_info
1065 object for the tfile target's current traceframe. */
1066
1067static int
1068build_traceframe_info (char blocktype, void *data)
1069{
19ba03f4 1070 struct traceframe_info *info = (struct traceframe_info *) data;
11395323
YQ
1071
1072 switch (blocktype)
1073 {
1074 case 'M':
1075 {
11395323
YQ
1076 ULONGEST maddr;
1077 unsigned short mlen;
1078
1079 tfile_read ((gdb_byte *) &maddr, 8);
1080 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
1081 gdbarch_byte_order
1082 (target_gdbarch ()));
1083 tfile_read ((gdb_byte *) &mlen, 2);
1084 mlen = (unsigned short)
1085 extract_unsigned_integer ((gdb_byte *) &mlen,
1086 2, gdbarch_byte_order
1087 (target_gdbarch ()));
1088
4cdd21a8 1089 info->memory.emplace_back (maddr, mlen);
11395323
YQ
1090 break;
1091 }
1092 case 'V':
1093 {
1094 int vnum;
1095
1096 tfile_read ((gdb_byte *) &vnum, 4);
d0d292a2 1097 info->tvars.push_back (vnum);
11395323
YQ
1098 }
1099 case 'R':
1100 case 'S':
1101 {
1102 break;
1103 }
1104 default:
1105 warning (_("Unhandled trace block type (%d) '%c ' "
1106 "while building trace frame info."),
1107 blocktype, blocktype);
1108 break;
1109 }
1110
1111 return 0;
1112}
1113
f6ac5f3d
PA
1114traceframe_info_up
1115tfile_target::traceframe_info ()
11395323 1116{
f6ac5f3d 1117 traceframe_info_up info (new struct traceframe_info);
2098b393
SM
1118
1119 traceframe_walk_blocks (build_traceframe_info, 0, info.get ());
11395323 1120
11395323
YQ
1121 return info;
1122}
1123
5ac87a99
MK
1124/* Handles tdesc lines from tfile by appending the payload to
1125 a global trace_tdesc variable. */
1126
1127static void
1128tfile_append_tdesc_line (const char *line)
1129{
1130 buffer_grow_str (&trace_tdesc, line);
1131 buffer_grow_str (&trace_tdesc, "\n");
1132}
1133
11395323
YQ
1134void
1135_initialize_tracefile_tfile (void)
1136{
d9f719f1 1137 add_target (tfile_target_info, tfile_target_open, filename_completer);
11395323 1138}
This page took 0.909367 seconds and 4 git commands to generate.