1 /* Trace file TFILE format support in GDB.
3 Copyright (C) 1997-2016 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"
22 #include "readline/tilde.h"
23 #include "filestuff.h"
24 #include "rsp-low.h" /* bin2hex */
27 #include "gdbthread.h"
28 #include "exec.h" /* exec_bfd */
29 #include "completer.h"
30 #include "filenames.h"
32 #include "xml-tdesc.h"
33 #include "target-descriptions.h"
40 /* TFILE trace writer. */
42 struct tfile_trace_file_writer
44 struct trace_file_writer base;
46 /* File pointer to tfile trace file. */
48 /* Path name of the tfile trace file. */
52 /* This is the implementation of trace_file_write_ops method
53 target_save. We just call the generic target
54 target_save_trace_data to do target-side saving. */
57 tfile_target_save (struct trace_file_writer *self,
60 int err = target_save_trace_data (filename);
65 /* This is the implementation of trace_file_write_ops method
69 tfile_dtor (struct trace_file_writer *self)
71 struct tfile_trace_file_writer *writer
72 = (struct tfile_trace_file_writer *) self;
74 xfree (writer->pathname);
76 if (writer->fp != NULL)
80 /* This is the implementation of trace_file_write_ops method
81 start. It creates the trace file FILENAME and registers some
85 tfile_start (struct trace_file_writer *self, const char *filename)
87 struct tfile_trace_file_writer *writer
88 = (struct tfile_trace_file_writer *) self;
90 writer->pathname = tilde_expand (filename);
91 writer->fp = gdb_fopen_cloexec (writer->pathname, "wb");
92 if (writer->fp == NULL)
93 error (_("Unable to open file '%s' for saving trace data (%s)"),
94 writer->pathname, safe_strerror (errno));
97 /* This is the implementation of trace_file_write_ops method
98 write_header. Write the TFILE header. */
101 tfile_write_header (struct trace_file_writer *self)
103 struct tfile_trace_file_writer *writer
104 = (struct tfile_trace_file_writer *) self;
107 /* Write a file header, with a high-bit-set char to indicate a
108 binary file, plus a hint as what this file is, and a version
109 number in case of future needs. */
110 written = fwrite ("\x7fTRACE0\n", 8, 1, writer->fp);
112 perror_with_name (writer->pathname);
115 /* This is the implementation of trace_file_write_ops method
116 write_regblock_type. Write the size of register block. */
119 tfile_write_regblock_type (struct trace_file_writer *self, int size)
121 struct tfile_trace_file_writer *writer
122 = (struct tfile_trace_file_writer *) self;
124 fprintf (writer->fp, "R %x\n", size);
127 /* This is the implementation of trace_file_write_ops method
131 tfile_write_status (struct trace_file_writer *self,
132 struct trace_status *ts)
134 struct tfile_trace_file_writer *writer
135 = (struct tfile_trace_file_writer *) self;
137 fprintf (writer->fp, "status %c;%s",
138 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
139 if (ts->stop_reason == tracepoint_error
140 || ts->stop_reason == tstop_command)
142 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
144 bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
145 fprintf (writer->fp, ":%s", buf);
147 fprintf (writer->fp, ":%x", ts->stopping_tracepoint);
148 if (ts->traceframe_count >= 0)
149 fprintf (writer->fp, ";tframes:%x", ts->traceframe_count);
150 if (ts->traceframes_created >= 0)
151 fprintf (writer->fp, ";tcreated:%x", ts->traceframes_created);
152 if (ts->buffer_free >= 0)
153 fprintf (writer->fp, ";tfree:%x", ts->buffer_free);
154 if (ts->buffer_size >= 0)
155 fprintf (writer->fp, ";tsize:%x", ts->buffer_size);
156 if (ts->disconnected_tracing)
157 fprintf (writer->fp, ";disconn:%x", ts->disconnected_tracing);
158 if (ts->circular_buffer)
159 fprintf (writer->fp, ";circular:%x", ts->circular_buffer);
162 fprintf (writer->fp, ";starttime:%s",
163 phex_nz (ts->start_time, sizeof (ts->start_time)));
167 fprintf (writer->fp, ";stoptime:%s",
168 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
170 if (ts->notes != NULL)
172 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
174 bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
175 fprintf (writer->fp, ";notes:%s", buf);
177 if (ts->user_name != NULL)
179 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
181 bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
182 fprintf (writer->fp, ";username:%s", buf);
184 fprintf (writer->fp, "\n");
187 /* This is the implementation of trace_file_write_ops method
188 write_uploaded_tsv. */
191 tfile_write_uploaded_tsv (struct trace_file_writer *self,
192 struct uploaded_tsv *utsv)
195 struct tfile_trace_file_writer *writer
196 = (struct tfile_trace_file_writer *) self;
200 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
201 bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
204 fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
205 utsv->number, phex_nz (utsv->initial_value, 8),
212 #define MAX_TRACE_UPLOAD 2000
214 /* This is the implementation of trace_file_write_ops method
215 write_uploaded_tp. */
218 tfile_write_uploaded_tp (struct trace_file_writer *self,
219 struct uploaded_tp *utp)
221 struct tfile_trace_file_writer *writer
222 = (struct tfile_trace_file_writer *) self;
225 char buf[MAX_TRACE_UPLOAD];
227 fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
228 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
229 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
230 if (utp->type == bp_fast_tracepoint)
231 fprintf (writer->fp, ":F%x", utp->orig_size);
234 ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
236 fprintf (writer->fp, "\n");
237 for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
238 fprintf (writer->fp, "tp A%x:%s:%s\n",
239 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
240 for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
241 fprintf (writer->fp, "tp S%x:%s:%s\n",
242 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
245 encode_source_string (utp->number, utp->addr,
246 "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
247 fprintf (writer->fp, "tp Z%s\n", buf);
249 if (utp->cond_string)
251 encode_source_string (utp->number, utp->addr,
252 "cond", utp->cond_string,
253 buf, MAX_TRACE_UPLOAD);
254 fprintf (writer->fp, "tp Z%s\n", buf);
256 for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
258 encode_source_string (utp->number, utp->addr, "cmd", act,
259 buf, MAX_TRACE_UPLOAD);
260 fprintf (writer->fp, "tp Z%s\n", buf);
262 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
263 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
265 phex_nz (utp->traceframe_usage,
266 sizeof (utp->traceframe_usage)));
269 /* This is the implementation of trace_file_write_ops method
273 tfile_write_tdesc (struct trace_file_writer *self)
275 struct tfile_trace_file_writer *writer
276 = (struct tfile_trace_file_writer *) self;
277 char *tdesc = target_fetch_description_xml (¤t_target);
284 /* Write tdesc line by line, prefixing each line with "tdesc ". */
287 next = strchr (ptr, '\n');
290 fprintf (writer->fp, "tdesc %.*s\n", (int) (next - ptr), ptr);
294 else if (*ptr != '\0')
296 /* Last line, doesn't have a newline. */
297 fprintf (writer->fp, "tdesc %s\n", ptr);
305 /* This is the implementation of trace_file_write_ops method
306 write_definition_end. */
309 tfile_write_definition_end (struct trace_file_writer *self)
311 struct tfile_trace_file_writer *writer
312 = (struct tfile_trace_file_writer *) self;
314 fprintf (writer->fp, "\n");
317 /* This is the implementation of trace_file_write_ops method
321 tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
324 struct tfile_trace_file_writer *writer
325 = (struct tfile_trace_file_writer *) self;
327 if (fwrite (buf, len, 1, writer->fp) < 1)
328 perror_with_name (writer->pathname);
331 /* This is the implementation of trace_file_write_ops method
335 tfile_end (struct trace_file_writer *self)
337 struct tfile_trace_file_writer *writer
338 = (struct tfile_trace_file_writer *) self;
341 /* Mark the end of trace data. */
342 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
343 perror_with_name (writer->pathname);
346 /* Operations to write trace buffers into TFILE format. */
348 static const struct trace_file_write_ops tfile_write_ops =
354 tfile_write_regblock_type,
356 tfile_write_uploaded_tsv,
357 tfile_write_uploaded_tp,
359 tfile_write_definition_end,
360 tfile_write_raw_data,
365 /* Return a trace writer for TFILE format. */
367 struct trace_file_writer *
368 tfile_trace_file_writer_new (void)
370 struct tfile_trace_file_writer *writer
371 = XNEW (struct tfile_trace_file_writer);
373 writer->base.ops = &tfile_write_ops;
375 writer->pathname = NULL;
377 return (struct trace_file_writer *) writer;
380 /* target tfile command */
382 static struct target_ops tfile_ops;
384 /* Fill in tfile_ops with its defined operations and properties. */
386 #define TRACE_HEADER_SIZE 8
388 #define TFILE_PID (1)
390 static char *trace_filename;
391 static int trace_fd = -1;
392 static off_t trace_frames_offset;
393 static off_t cur_offset;
394 static int cur_data_size;
395 int trace_regblock_size;
396 static struct buffer trace_tdesc;
398 static void tfile_append_tdesc_line (const char *line);
399 static void tfile_interp_line (char *line,
400 struct uploaded_tp **utpp,
401 struct uploaded_tsv **utsvp);
403 /* Read SIZE bytes into READBUF from the trace frame, starting at
404 TRACE_FD's current position. Note that this call `read'
405 underneath, hence it advances the file's seek position. Throws an
406 error if the `read' syscall fails, or less than SIZE bytes are
410 tfile_read (gdb_byte *readbuf, int size)
414 gotten = read (trace_fd, readbuf, size);
416 perror_with_name (trace_filename);
417 else if (gotten < size)
418 error (_("Premature end of file while reading trace file"));
422 tfile_open (const char *arg, int from_tty)
425 struct cleanup *old_chain;
428 char header[TRACE_HEADER_SIZE];
429 char linebuf[1000]; /* Should be max remote packet size or so. */
432 struct trace_status *ts;
433 struct uploaded_tp *uploaded_tps = NULL;
434 struct uploaded_tsv *uploaded_tsvs = NULL;
437 target_preopen (from_tty);
439 error (_("No trace file specified."));
441 filename = tilde_expand (arg);
442 if (!IS_ABSOLUTE_PATH(filename))
444 temp = concat (current_directory, "/", filename, (char *) NULL);
449 old_chain = make_cleanup (xfree, filename);
451 flags = O_BINARY | O_LARGEFILE;
453 scratch_chan = gdb_open_cloexec (filename, flags, 0);
454 if (scratch_chan < 0)
455 perror_with_name (filename);
457 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
459 discard_cleanups (old_chain); /* Don't free filename any more. */
460 unpush_target (&tfile_ops);
462 trace_filename = xstrdup (filename);
463 trace_fd = scratch_chan;
465 /* Make sure this is clear. */
466 buffer_free (&trace_tdesc);
469 /* Read the file header and test for validity. */
470 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
472 bytes += TRACE_HEADER_SIZE;
473 if (!(header[0] == 0x7f
474 && (startswith (header + 1, "TRACE0\n"))))
475 error (_("File is not a valid trace file."));
477 push_target (&tfile_ops);
479 trace_regblock_size = 0;
480 ts = current_trace_status ();
481 /* We know we're working with a file. Record its name. */
482 ts->filename = trace_filename;
483 /* Set defaults in case there is no status line. */
484 ts->running_known = 0;
485 ts->stop_reason = trace_stop_reason_unknown;
486 ts->traceframe_count = -1;
488 ts->disconnected_tracing = 0;
489 ts->circular_buffer = 0;
493 /* Read through a section of newline-terminated lines that
494 define things like tracepoints. */
498 tfile_read (&byte, 1);
503 /* Empty line marks end of the definition section. */
508 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
513 error (_("Excessively long lines in trace file"));
516 /* By now, tdesc lines have been read from tfile - let's parse them. */
517 target_find_description ();
519 /* Record the starting offset of the binary trace data. */
520 trace_frames_offset = bytes;
522 /* If we don't have a blocksize, we can't interpret the
524 if (trace_regblock_size == 0)
525 error (_("No register block size recorded in trace file"));
527 CATCH (ex, RETURN_MASK_ALL)
529 /* Remove the partially set up target. */
530 unpush_target (&tfile_ops);
531 throw_exception (ex);
535 inferior_appeared (current_inferior (), TFILE_PID);
536 inferior_ptid = pid_to_ptid (TFILE_PID);
537 add_thread_silent (inferior_ptid);
539 if (ts->traceframe_count <= 0)
540 warning (_("No traceframes present in this file."));
542 /* Add the file's tracepoints and variables into the current mix. */
544 /* Get trace state variables first, they may be checked when parsing
545 uploaded commands. */
546 merge_uploaded_trace_state_variables (&uploaded_tsvs);
548 merge_uploaded_tracepoints (&uploaded_tps);
550 post_create_inferior (&tfile_ops, from_tty);
553 /* Interpret the given line from the definitions part of the trace
557 tfile_interp_line (char *line, struct uploaded_tp **utpp,
558 struct uploaded_tsv **utsvp)
562 if (startswith (p, "R "))
565 trace_regblock_size = strtol (p, &p, 16);
567 else if (startswith (p, "status "))
569 p += strlen ("status ");
570 parse_trace_status (p, current_trace_status ());
572 else if (startswith (p, "tp "))
575 parse_tracepoint_definition (p, utpp);
577 else if (startswith (p, "tsv "))
579 p += strlen ("tsv ");
580 parse_tsv_definition (p, utsvp);
582 else if (startswith (p, "tdesc "))
584 p += strlen ("tdesc ");
585 tfile_append_tdesc_line (p);
588 warning (_("Ignoring trace file definition \"%s\""), line);
591 /* Close the trace file and generally clean up. */
594 tfile_close (struct target_ops *self)
601 pid = ptid_get_pid (inferior_ptid);
602 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
603 exit_inferior_silent (pid);
607 xfree (trace_filename);
608 trace_filename = NULL;
609 buffer_free (&trace_tdesc);
611 trace_reset_local_state ();
615 tfile_files_info (struct target_ops *t)
617 printf_filtered ("\t`%s'\n", trace_filename);
621 tfile_get_tracepoint_status (struct target_ops *self,
622 struct breakpoint *tp, struct uploaded_tp *utp)
624 /* Other bits of trace status were collected as part of opening the
625 trace files, so nothing to do here. */
628 /* Given the position of a traceframe in the file, figure out what
629 address the frame was collected at. This would normally be the
630 value of a collected PC register, but if not available, we
634 tfile_get_traceframe_address (off_t tframe_offset)
638 struct tracepoint *tp;
639 off_t saved_offset = cur_offset;
641 /* FIXME dig pc out of collected registers. */
643 /* Fall back to using tracepoint address. */
644 lseek (trace_fd, tframe_offset, SEEK_SET);
645 tfile_read ((gdb_byte *) &tpnum, 2);
646 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
648 (target_gdbarch ()));
650 tp = get_tracepoint_by_number_on_target (tpnum);
651 /* FIXME this is a poor heuristic if multiple locations. */
652 if (tp && tp->base.loc)
653 addr = tp->base.loc->address;
655 /* Restore our seek position. */
656 cur_offset = saved_offset;
657 lseek (trace_fd, cur_offset, SEEK_SET);
661 /* Given a type of search and some parameters, scan the collection of
662 traceframes in the file looking for a match. When found, return
663 both the traceframe and tracepoint number, otherwise -1 for
667 tfile_trace_find (struct target_ops *self, enum trace_find_type type, int num,
668 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
671 int tfnum = 0, found = 0;
672 unsigned int data_size;
673 struct tracepoint *tp;
674 off_t offset, tframe_offset;
684 lseek (trace_fd, trace_frames_offset, SEEK_SET);
685 offset = trace_frames_offset;
688 tframe_offset = offset;
689 tfile_read ((gdb_byte *) &tpnum, 2);
690 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
692 (target_gdbarch ()));
696 tfile_read ((gdb_byte *) &data_size, 4);
697 data_size = (unsigned int) extract_unsigned_integer
698 ((gdb_byte *) &data_size, 4,
699 gdbarch_byte_order (target_gdbarch ()));
702 if (type == tfind_number)
704 /* Looking for a specific trace frame. */
710 /* Start from the _next_ trace frame. */
711 if (tfnum > get_traceframe_number ())
716 tfaddr = tfile_get_traceframe_address (tframe_offset);
721 tp = get_tracepoint (num);
722 if (tp && tpnum == tp->number_on_target)
726 tfaddr = tfile_get_traceframe_address (tframe_offset);
727 if (addr1 <= tfaddr && tfaddr <= addr2)
731 tfaddr = tfile_get_traceframe_address (tframe_offset);
732 if (!(addr1 <= tfaddr && tfaddr <= addr2))
736 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
746 cur_data_size = data_size;
750 /* Skip past the traceframe's data. */
751 lseek (trace_fd, data_size, SEEK_CUR);
753 /* Update our own count of traceframes. */
756 /* Did not find what we were looking for. */
762 /* Prototype of the callback passed to tframe_walk_blocks. */
763 typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
765 /* Callback for traceframe_walk_blocks, used to find a given block
766 type in a traceframe. */
769 match_blocktype (char blocktype, void *data)
771 char *wantedp = (char *) data;
773 if (*wantedp == blocktype)
779 /* Walk over all traceframe block starting at POS offset from
780 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
781 unmodified. If CALLBACK returns true, this returns the position in
782 the traceframe where the block is found, relative to the start of
783 the traceframe (cur_offset). Returns -1 if no callback call
784 returned true, indicating that all blocks have been walked. */
787 traceframe_walk_blocks (walk_blocks_callback_func callback,
790 /* Iterate through a traceframe's blocks, looking for a block of the
793 lseek (trace_fd, cur_offset + pos, SEEK_SET);
794 while (pos < cur_data_size)
799 tfile_read ((gdb_byte *) &block_type, 1);
803 if ((*callback) (block_type, data))
809 lseek (trace_fd, cur_offset + pos + trace_regblock_size, SEEK_SET);
810 pos += trace_regblock_size;
813 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
814 tfile_read ((gdb_byte *) &mlen, 2);
815 mlen = (unsigned short)
816 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
818 (target_gdbarch ()));
819 lseek (trace_fd, mlen, SEEK_CUR);
820 pos += (8 + 2 + mlen);
823 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
827 error (_("Unknown block type '%c' (0x%x) in trace frame"),
828 block_type, block_type);
836 /* Convenience wrapper around traceframe_walk_blocks. Looks for the
837 position offset of a block of type TYPE_WANTED in the current trace
838 frame, starting at POS. Returns -1 if no such block was found. */
841 traceframe_find_block_type (char type_wanted, int pos)
843 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
846 /* Look for a block of saved registers in the traceframe, and get the
847 requested register from it. */
850 tfile_fetch_registers (struct target_ops *ops,
851 struct regcache *regcache, int regno)
853 struct gdbarch *gdbarch = get_regcache_arch (regcache);
854 int offset, regn, regsize, dummy;
856 /* An uninitialized reg size says we're not going to be
857 successful at getting register blocks. */
858 if (!trace_regblock_size)
861 if (traceframe_find_block_type ('R', 0) >= 0)
863 gdb_byte *regs = (gdb_byte *) alloca (trace_regblock_size);
865 tfile_read (regs, trace_regblock_size);
867 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
869 if (!remote_register_number_and_offset (get_regcache_arch (regcache),
870 regn, &dummy, &offset))
873 regsize = register_size (gdbarch, regn);
874 /* Make sure we stay within block bounds. */
875 if (offset + regsize > trace_regblock_size)
877 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
881 regcache_raw_supply (regcache, regno, regs + offset);
884 else if (regno == -1)
886 regcache_raw_supply (regcache, regn, regs + offset);
892 tracefile_fetch_registers (regcache, regno);
895 static enum target_xfer_status
896 tfile_xfer_partial_features (struct target_ops *ops, const char *annex,
897 gdb_byte *readbuf, const gdb_byte *writebuf,
898 ULONGEST offset, ULONGEST len,
899 ULONGEST *xfered_len)
901 if (strcmp (annex, "target.xml"))
902 return TARGET_XFER_E_IO;
905 error (_("tfile_xfer_partial: tdesc is read-only"));
907 if (trace_tdesc.used_size == 0)
908 return TARGET_XFER_E_IO;
910 if (offset >= trace_tdesc.used_size)
911 return TARGET_XFER_EOF;
913 if (len > trace_tdesc.used_size - offset)
914 len = trace_tdesc.used_size - offset;
916 memcpy (readbuf, trace_tdesc.buffer + offset, len);
919 return TARGET_XFER_OK;
922 static enum target_xfer_status
923 tfile_xfer_partial (struct target_ops *ops, enum target_object object,
924 const char *annex, gdb_byte *readbuf,
925 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
926 ULONGEST *xfered_len)
928 /* We're only doing regular memory and tdesc for now. */
929 if (object == TARGET_OBJECT_AVAILABLE_FEATURES)
930 return tfile_xfer_partial_features (ops, annex, readbuf, writebuf,
931 offset, len, xfered_len);
932 if (object != TARGET_OBJECT_MEMORY)
933 return TARGET_XFER_E_IO;
936 error (_("tfile_xfer_partial: trace file is read-only"));
938 if (get_traceframe_number () != -1)
941 enum target_xfer_status res;
942 /* Records the lowest available address of all blocks that
943 intersects the requested range. */
944 ULONGEST low_addr_available = 0;
946 /* Iterate through the traceframe's blocks, looking for
948 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
952 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
954 tfile_read ((gdb_byte *) &maddr, 8);
955 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
957 tfile_read ((gdb_byte *) &mlen, 2);
958 mlen = (unsigned short)
959 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
961 /* If the block includes the first part of the desired
962 range, return as much it has; GDB will re-request the
963 remainder, which might be in a different block of this
965 if (maddr <= offset && offset < (maddr + mlen))
967 amt = (maddr + mlen) - offset;
972 lseek (trace_fd, offset - maddr, SEEK_CUR);
973 tfile_read (readbuf, amt);
975 return TARGET_XFER_OK;
978 if (offset < maddr && maddr < (offset + len))
979 if (low_addr_available == 0 || low_addr_available > maddr)
980 low_addr_available = maddr;
982 /* Skip over this block. */
983 pos += (8 + 2 + mlen);
986 /* Requested memory is unavailable in the context of traceframes,
987 and this address falls within a read-only section, fallback
988 to reading from executable, up to LOW_ADDR_AVAILABLE. */
989 if (offset < low_addr_available)
990 len = min (len, low_addr_available - offset);
991 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
993 if (res == TARGET_XFER_OK)
994 return TARGET_XFER_OK;
997 /* No use trying further, we know some memory starting
998 at MEMADDR isn't available. */
1000 return TARGET_XFER_UNAVAILABLE;
1005 /* Fallback to reading from read-only sections. */
1006 return section_table_read_available_memory (readbuf, offset, len,
1011 /* Iterate through the blocks of a trace frame, looking for a 'V'
1012 block with a matching tsv number. */
1015 tfile_get_trace_state_variable_value (struct target_ops *self,
1016 int tsvnum, LONGEST *val)
1021 /* Iterate over blocks in current frame and find the last 'V'
1022 block in which tsv number is TSVNUM. In one trace frame, there
1023 may be multiple 'V' blocks created for a given trace variable,
1024 and the last matched 'V' block contains the updated value. */
1026 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
1030 tfile_read ((gdb_byte *) &vnum, 4);
1031 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
1033 (target_gdbarch ()));
1036 tfile_read ((gdb_byte *) val, 8);
1037 *val = extract_signed_integer ((gdb_byte *) val, 8,
1039 (target_gdbarch ()));
1048 /* Callback for traceframe_walk_blocks. Builds a traceframe_info
1049 object for the tfile target's current traceframe. */
1052 build_traceframe_info (char blocktype, void *data)
1054 struct traceframe_info *info = (struct traceframe_info *) data;
1060 struct mem_range *r;
1062 unsigned short mlen;
1064 tfile_read ((gdb_byte *) &maddr, 8);
1065 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
1067 (target_gdbarch ()));
1068 tfile_read ((gdb_byte *) &mlen, 2);
1069 mlen = (unsigned short)
1070 extract_unsigned_integer ((gdb_byte *) &mlen,
1071 2, gdbarch_byte_order
1072 (target_gdbarch ()));
1074 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1084 tfile_read ((gdb_byte *) &vnum, 4);
1085 VEC_safe_push (int, info->tvars, vnum);
1093 warning (_("Unhandled trace block type (%d) '%c ' "
1094 "while building trace frame info."),
1095 blocktype, blocktype);
1102 static struct traceframe_info *
1103 tfile_traceframe_info (struct target_ops *self)
1105 struct traceframe_info *info = XCNEW (struct traceframe_info);
1107 traceframe_walk_blocks (build_traceframe_info, 0, info);
1111 /* Handles tdesc lines from tfile by appending the payload to
1112 a global trace_tdesc variable. */
1115 tfile_append_tdesc_line (const char *line)
1117 buffer_grow_str (&trace_tdesc, line);
1118 buffer_grow_str (&trace_tdesc, "\n");
1122 init_tfile_ops (void)
1124 init_tracefile_ops (&tfile_ops);
1126 tfile_ops.to_shortname = "tfile";
1127 tfile_ops.to_longname = "Local trace dump file";
1129 = "Use a trace file as a target. Specify the filename of the trace file.";
1130 tfile_ops.to_open = tfile_open;
1131 tfile_ops.to_close = tfile_close;
1132 tfile_ops.to_fetch_registers = tfile_fetch_registers;
1133 tfile_ops.to_xfer_partial = tfile_xfer_partial;
1134 tfile_ops.to_files_info = tfile_files_info;
1135 tfile_ops.to_get_tracepoint_status = tfile_get_tracepoint_status;
1136 tfile_ops.to_trace_find = tfile_trace_find;
1137 tfile_ops.to_get_trace_state_variable_value
1138 = tfile_get_trace_state_variable_value;
1139 tfile_ops.to_traceframe_info = tfile_traceframe_info;
1142 extern initialize_file_ftype _initialize_tracefile_tfile;
1145 _initialize_tracefile_tfile (void)
1149 add_target_with_completer (&tfile_ops, filename_completer);