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