1 /* Trace file TFILE format support in GDB.
3 Copyright (C) 1997-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "tracefile.h"
22 #include "readline/tilde.h"
23 #include "common/filestuff.h"
24 #include "common/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"
34 #include "common/buffer.h"
41 /* The tfile target. */
43 static const target_info tfile_target_info = {
45 N_("Local trace dump file"),
46 N_("Use a trace file as a target. Specify the filename of the trace file.")
49 class tfile_target final : public tracefile_target
52 const target_info &info () const override
53 { return tfile_target_info; }
55 void close () override;
56 void fetch_registers (struct regcache *, int) override;
57 enum target_xfer_status xfer_partial (enum target_object object,
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;
66 bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
67 traceframe_info_up traceframe_info () override;
69 void get_tracepoint_status (struct breakpoint *tp,
70 struct uploaded_tp *utp) override;
73 /* TFILE trace writer. */
75 struct tfile_trace_file_writer
77 struct trace_file_writer base;
79 /* File pointer to tfile trace file. */
81 /* Path name of the tfile trace file. */
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. */
90 tfile_target_save (struct trace_file_writer *self,
93 int err = target_save_trace_data (filename);
98 /* This is the implementation of trace_file_write_ops method
102 tfile_dtor (struct trace_file_writer *self)
104 struct tfile_trace_file_writer *writer
105 = (struct tfile_trace_file_writer *) self;
107 xfree (writer->pathname);
109 if (writer->fp != NULL)
113 /* This is the implementation of trace_file_write_ops method
114 start. It creates the trace file FILENAME and registers some
118 tfile_start (struct trace_file_writer *self, const char *filename)
120 struct tfile_trace_file_writer *writer
121 = (struct tfile_trace_file_writer *) self;
123 writer->pathname = tilde_expand (filename);
124 writer->fp = gdb_fopen_cloexec (writer->pathname, "wb").release ();
125 if (writer->fp == NULL)
126 error (_("Unable to open file '%s' for saving trace data (%s)"),
127 writer->pathname, safe_strerror (errno));
130 /* This is the implementation of trace_file_write_ops method
131 write_header. Write the TFILE header. */
134 tfile_write_header (struct trace_file_writer *self)
136 struct tfile_trace_file_writer *writer
137 = (struct tfile_trace_file_writer *) self;
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);
145 perror_with_name (writer->pathname);
148 /* This is the implementation of trace_file_write_ops method
149 write_regblock_type. Write the size of register block. */
152 tfile_write_regblock_type (struct trace_file_writer *self, int size)
154 struct tfile_trace_file_writer *writer
155 = (struct tfile_trace_file_writer *) self;
157 fprintf (writer->fp, "R %x\n", size);
160 /* This is the implementation of trace_file_write_ops method
164 tfile_write_status (struct trace_file_writer *self,
165 struct trace_status *ts)
167 struct tfile_trace_file_writer *writer
168 = (struct tfile_trace_file_writer *) self;
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
173 || ts->stop_reason == trace_stop_command)
175 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
177 bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
178 fprintf (writer->fp, ":%s", buf);
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);
195 fprintf (writer->fp, ";starttime:%s",
196 phex_nz (ts->start_time, sizeof (ts->start_time)));
200 fprintf (writer->fp, ";stoptime:%s",
201 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
203 if (ts->notes != NULL)
205 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
207 bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
208 fprintf (writer->fp, ";notes:%s", buf);
210 if (ts->user_name != NULL)
212 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
214 bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
215 fprintf (writer->fp, ";username:%s", buf);
217 fprintf (writer->fp, "\n");
220 /* This is the implementation of trace_file_write_ops method
221 write_uploaded_tsv. */
224 tfile_write_uploaded_tsv (struct trace_file_writer *self,
225 struct uploaded_tsv *utsv)
228 struct tfile_trace_file_writer *writer
229 = (struct tfile_trace_file_writer *) self;
233 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
234 bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
237 fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
238 utsv->number, phex_nz (utsv->initial_value, 8),
239 utsv->builtin, buf != NULL ? buf : "");
245 #define MAX_TRACE_UPLOAD 2000
247 /* This is the implementation of trace_file_write_ops method
248 write_uploaded_tp. */
251 tfile_write_uploaded_tp (struct trace_file_writer *self,
252 struct uploaded_tp *utp)
254 struct tfile_trace_file_writer *writer
255 = (struct tfile_trace_file_writer *) self;
256 char buf[MAX_TRACE_UPLOAD];
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);
265 ":X%x,%s", (unsigned int) strlen (utp->cond.get ()) / 2,
267 fprintf (writer->fp, "\n");
268 for (const auto &act : utp->actions)
269 fprintf (writer->fp, "tp A%x:%s:%s\n",
270 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
271 for (const auto &act : utp->step_actions)
272 fprintf (writer->fp, "tp S%x:%s:%s\n",
273 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
276 encode_source_string (utp->number, utp->addr,
277 "at", utp->at_string.get (),
278 buf, MAX_TRACE_UPLOAD);
279 fprintf (writer->fp, "tp Z%s\n", buf);
281 if (utp->cond_string)
283 encode_source_string (utp->number, utp->addr,
284 "cond", utp->cond_string.get (),
285 buf, MAX_TRACE_UPLOAD);
286 fprintf (writer->fp, "tp Z%s\n", buf);
288 for (const auto &act : utp->cmd_strings)
290 encode_source_string (utp->number, utp->addr, "cmd", act.get (),
291 buf, MAX_TRACE_UPLOAD);
292 fprintf (writer->fp, "tp Z%s\n", buf);
294 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
295 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
297 phex_nz (utp->traceframe_usage,
298 sizeof (utp->traceframe_usage)));
301 /* This is the implementation of trace_file_write_ops method
305 tfile_write_tdesc (struct trace_file_writer *self)
307 struct tfile_trace_file_writer *writer
308 = (struct tfile_trace_file_writer *) self;
310 gdb::optional<std::string> tdesc
311 = target_fetch_description_xml (current_top_target ());
316 const char *ptr = tdesc->c_str ();
318 /* Write tdesc line by line, prefixing each line with "tdesc ". */
321 const char *next = strchr (ptr, '\n');
324 fprintf (writer->fp, "tdesc %.*s\n", (int) (next - ptr), ptr);
328 else if (*ptr != '\0')
330 /* Last line, doesn't have a newline. */
331 fprintf (writer->fp, "tdesc %s\n", ptr);
337 /* This is the implementation of trace_file_write_ops method
338 write_definition_end. */
341 tfile_write_definition_end (struct trace_file_writer *self)
343 struct tfile_trace_file_writer *writer
344 = (struct tfile_trace_file_writer *) self;
346 fprintf (writer->fp, "\n");
349 /* This is the implementation of trace_file_write_ops method
353 tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
356 struct tfile_trace_file_writer *writer
357 = (struct tfile_trace_file_writer *) self;
359 if (fwrite (buf, len, 1, writer->fp) < 1)
360 perror_with_name (writer->pathname);
363 /* This is the implementation of trace_file_write_ops method
367 tfile_end (struct trace_file_writer *self)
369 struct tfile_trace_file_writer *writer
370 = (struct tfile_trace_file_writer *) self;
373 /* Mark the end of trace data. */
374 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
375 perror_with_name (writer->pathname);
378 /* Operations to write trace buffers into TFILE format. */
380 static const struct trace_file_write_ops tfile_write_ops =
386 tfile_write_regblock_type,
388 tfile_write_uploaded_tsv,
389 tfile_write_uploaded_tp,
391 tfile_write_definition_end,
392 tfile_write_raw_data,
397 /* Return a trace writer for TFILE format. */
399 struct trace_file_writer *
400 tfile_trace_file_writer_new (void)
402 struct tfile_trace_file_writer *writer
403 = XNEW (struct tfile_trace_file_writer);
405 writer->base.ops = &tfile_write_ops;
407 writer->pathname = NULL;
409 return (struct trace_file_writer *) writer;
412 /* target tfile command */
414 static tfile_target tfile_ops;
416 #define TRACE_HEADER_SIZE 8
418 #define TFILE_PID (1)
420 static char *trace_filename;
421 static int trace_fd = -1;
422 static off_t trace_frames_offset;
423 static off_t cur_offset;
424 static int cur_data_size;
425 int trace_regblock_size;
426 static struct buffer trace_tdesc;
428 static void tfile_append_tdesc_line (const char *line);
429 static void tfile_interp_line (char *line,
430 struct uploaded_tp **utpp,
431 struct uploaded_tsv **utsvp);
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
440 tfile_read (gdb_byte *readbuf, int size)
444 gotten = read (trace_fd, readbuf, size);
446 perror_with_name (trace_filename);
447 else if (gotten < size)
448 error (_("Premature end of file while reading trace file"));
451 /* Open the tfile target. */
454 tfile_target_open (const char *arg, int from_tty)
458 char header[TRACE_HEADER_SIZE];
459 char linebuf[1000]; /* Should be max remote packet size or so. */
462 struct trace_status *ts;
463 struct uploaded_tp *uploaded_tps = NULL;
464 struct uploaded_tsv *uploaded_tsvs = NULL;
466 target_preopen (from_tty);
468 error (_("No trace file specified."));
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 (),
475 flags = O_BINARY | O_LARGEFILE;
477 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
478 if (scratch_chan < 0)
479 perror_with_name (filename.get ());
481 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
483 unpush_target (&tfile_ops);
485 trace_filename = filename.release ();
486 trace_fd = scratch_chan;
488 /* Make sure this is clear. */
489 buffer_free (&trace_tdesc);
492 /* Read the file header and test for validity. */
493 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
495 bytes += TRACE_HEADER_SIZE;
496 if (!(header[0] == 0x7f
497 && (startswith (header + 1, "TRACE0\n"))))
498 error (_("File is not a valid trace file."));
500 push_target (&tfile_ops);
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;
511 ts->disconnected_tracing = 0;
512 ts->circular_buffer = 0;
516 /* Read through a section of newline-terminated lines that
517 define things like tracepoints. */
521 tfile_read (&byte, 1);
526 /* Empty line marks end of the definition section. */
531 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
536 error (_("Excessively long lines in trace file"));
539 /* By now, tdesc lines have been read from tfile - let's parse them. */
540 target_find_description ();
542 /* Record the starting offset of the binary trace data. */
543 trace_frames_offset = bytes;
545 /* If we don't have a blocksize, we can't interpret the
547 if (trace_regblock_size == 0)
548 error (_("No register block size recorded in trace file"));
550 CATCH (ex, RETURN_MASK_ALL)
552 /* Remove the partially set up target. */
553 unpush_target (&tfile_ops);
554 throw_exception (ex);
558 inferior_appeared (current_inferior (), TFILE_PID);
559 inferior_ptid = ptid_t (TFILE_PID);
560 add_thread_silent (inferior_ptid);
562 if (ts->traceframe_count <= 0)
563 warning (_("No traceframes present in this file."));
565 /* Add the file's tracepoints and variables into the current mix. */
567 /* Get trace state variables first, they may be checked when parsing
568 uploaded commands. */
569 merge_uploaded_trace_state_variables (&uploaded_tsvs);
571 merge_uploaded_tracepoints (&uploaded_tps);
573 post_create_inferior (&tfile_ops, from_tty);
576 /* Interpret the given line from the definitions part of the trace
580 tfile_interp_line (char *line, struct uploaded_tp **utpp,
581 struct uploaded_tsv **utsvp)
585 if (startswith (p, "R "))
588 trace_regblock_size = strtol (p, &p, 16);
590 else if (startswith (p, "status "))
592 p += strlen ("status ");
593 parse_trace_status (p, current_trace_status ());
595 else if (startswith (p, "tp "))
598 parse_tracepoint_definition (p, utpp);
600 else if (startswith (p, "tsv "))
602 p += strlen ("tsv ");
603 parse_tsv_definition (p, utsvp);
605 else if (startswith (p, "tdesc "))
607 p += strlen ("tdesc ");
608 tfile_append_tdesc_line (p);
611 warning (_("Ignoring trace file definition \"%s\""), line);
614 /* Close the trace file and generally clean up. */
617 tfile_target::close ()
622 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
623 exit_inferior_silent (current_inferior ());
627 xfree (trace_filename);
628 trace_filename = NULL;
629 buffer_free (&trace_tdesc);
631 trace_reset_local_state ();
635 tfile_target::files_info ()
637 printf_filtered ("\t`%s'\n", trace_filename);
641 tfile_target::get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
643 /* Other bits of trace status were collected as part of opening the
644 trace files, so nothing to do here. */
647 /* Given the position of a traceframe in the file, figure out what
648 address the frame was collected at. This would normally be the
649 value of a collected PC register, but if not available, we
653 tfile_get_traceframe_address (off_t tframe_offset)
657 struct tracepoint *tp;
658 off_t saved_offset = cur_offset;
660 /* FIXME dig pc out of collected registers. */
662 /* Fall back to using tracepoint address. */
663 lseek (trace_fd, tframe_offset, SEEK_SET);
664 tfile_read ((gdb_byte *) &tpnum, 2);
665 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
667 (target_gdbarch ()));
669 tp = get_tracepoint_by_number_on_target (tpnum);
670 /* FIXME this is a poor heuristic if multiple locations. */
672 addr = tp->loc->address;
674 /* Restore our seek position. */
675 cur_offset = saved_offset;
676 lseek (trace_fd, cur_offset, SEEK_SET);
680 /* Given a type of search and some parameters, scan the collection of
681 traceframes in the file looking for a match. When found, return
682 both the traceframe and tracepoint number, otherwise -1 for
686 tfile_target::trace_find (enum trace_find_type type, int num,
687 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
690 int tfnum = 0, found = 0;
691 unsigned int data_size;
692 struct tracepoint *tp;
693 off_t offset, tframe_offset;
703 lseek (trace_fd, trace_frames_offset, SEEK_SET);
704 offset = trace_frames_offset;
707 tframe_offset = offset;
708 tfile_read ((gdb_byte *) &tpnum, 2);
709 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
711 (target_gdbarch ()));
715 tfile_read ((gdb_byte *) &data_size, 4);
716 data_size = (unsigned int) extract_unsigned_integer
717 ((gdb_byte *) &data_size, 4,
718 gdbarch_byte_order (target_gdbarch ()));
721 if (type == tfind_number)
723 /* Looking for a specific trace frame. */
729 /* Start from the _next_ trace frame. */
730 if (tfnum > get_traceframe_number ())
735 tfaddr = tfile_get_traceframe_address (tframe_offset);
740 tp = get_tracepoint (num);
741 if (tp && tpnum == tp->number_on_target)
745 tfaddr = tfile_get_traceframe_address (tframe_offset);
746 if (addr1 <= tfaddr && tfaddr <= addr2)
750 tfaddr = tfile_get_traceframe_address (tframe_offset);
751 if (!(addr1 <= tfaddr && tfaddr <= addr2))
755 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
765 cur_data_size = data_size;
769 /* Skip past the traceframe's data. */
770 lseek (trace_fd, data_size, SEEK_CUR);
772 /* Update our own count of traceframes. */
775 /* Did not find what we were looking for. */
781 /* Prototype of the callback passed to tframe_walk_blocks. */
782 typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
784 /* Callback for traceframe_walk_blocks, used to find a given block
785 type in a traceframe. */
788 match_blocktype (char blocktype, void *data)
790 char *wantedp = (char *) data;
792 if (*wantedp == blocktype)
798 /* Walk over all traceframe block starting at POS offset from
799 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
800 unmodified. If CALLBACK returns true, this returns the position in
801 the traceframe where the block is found, relative to the start of
802 the traceframe (cur_offset). Returns -1 if no callback call
803 returned true, indicating that all blocks have been walked. */
806 traceframe_walk_blocks (walk_blocks_callback_func callback,
809 /* Iterate through a traceframe's blocks, looking for a block of the
812 lseek (trace_fd, cur_offset + pos, SEEK_SET);
813 while (pos < cur_data_size)
818 tfile_read ((gdb_byte *) &block_type, 1);
822 if ((*callback) (block_type, data))
828 lseek (trace_fd, cur_offset + pos + trace_regblock_size, SEEK_SET);
829 pos += trace_regblock_size;
832 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
833 tfile_read ((gdb_byte *) &mlen, 2);
834 mlen = (unsigned short)
835 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
837 (target_gdbarch ()));
838 lseek (trace_fd, mlen, SEEK_CUR);
839 pos += (8 + 2 + mlen);
842 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
846 error (_("Unknown block type '%c' (0x%x) in trace frame"),
847 block_type, block_type);
855 /* Convenience wrapper around traceframe_walk_blocks. Looks for the
856 position offset of a block of type TYPE_WANTED in the current trace
857 frame, starting at POS. Returns -1 if no such block was found. */
860 traceframe_find_block_type (char type_wanted, int pos)
862 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
865 /* Look for a block of saved registers in the traceframe, and get the
866 requested register from it. */
869 tfile_target::fetch_registers (struct regcache *regcache, int regno)
871 struct gdbarch *gdbarch = regcache->arch ();
872 int offset, regn, regsize, dummy;
874 /* An uninitialized reg size says we're not going to be
875 successful at getting register blocks. */
876 if (!trace_regblock_size)
879 if (traceframe_find_block_type ('R', 0) >= 0)
881 gdb_byte *regs = (gdb_byte *) alloca (trace_regblock_size);
883 tfile_read (regs, trace_regblock_size);
885 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
887 if (!remote_register_number_and_offset (regcache->arch (),
888 regn, &dummy, &offset))
891 regsize = register_size (gdbarch, regn);
892 /* Make sure we stay within block bounds. */
893 if (offset + regsize > trace_regblock_size)
895 if (regcache->get_register_status (regn) == REG_UNKNOWN)
899 regcache->raw_supply (regno, regs + offset);
902 else if (regno == -1)
904 regcache->raw_supply (regn, regs + offset);
910 tracefile_fetch_registers (regcache, regno);
913 static enum target_xfer_status
914 tfile_xfer_partial_features (const char *annex,
915 gdb_byte *readbuf, const gdb_byte *writebuf,
916 ULONGEST offset, ULONGEST len,
917 ULONGEST *xfered_len)
919 if (strcmp (annex, "target.xml"))
920 return TARGET_XFER_E_IO;
923 error (_("tfile_xfer_partial: tdesc is read-only"));
925 if (trace_tdesc.used_size == 0)
926 return TARGET_XFER_E_IO;
928 if (offset >= trace_tdesc.used_size)
929 return TARGET_XFER_EOF;
931 if (len > trace_tdesc.used_size - offset)
932 len = trace_tdesc.used_size - offset;
934 memcpy (readbuf, trace_tdesc.buffer + offset, len);
937 return TARGET_XFER_OK;
940 enum target_xfer_status
941 tfile_target::xfer_partial (enum target_object object,
942 const char *annex, gdb_byte *readbuf,
943 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
944 ULONGEST *xfered_len)
946 /* We're only doing regular memory and tdesc for now. */
947 if (object == TARGET_OBJECT_AVAILABLE_FEATURES)
948 return tfile_xfer_partial_features (annex, readbuf, writebuf,
949 offset, len, xfered_len);
950 if (object != TARGET_OBJECT_MEMORY)
951 return TARGET_XFER_E_IO;
954 error (_("tfile_xfer_partial: trace file is read-only"));
956 if (get_traceframe_number () != -1)
959 enum target_xfer_status res;
960 /* Records the lowest available address of all blocks that
961 intersects the requested range. */
962 ULONGEST low_addr_available = 0;
964 /* Iterate through the traceframe's blocks, looking for
966 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
970 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
972 tfile_read ((gdb_byte *) &maddr, 8);
973 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
975 tfile_read ((gdb_byte *) &mlen, 2);
976 mlen = (unsigned short)
977 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
979 /* If the block includes the first part of the desired
980 range, return as much it has; GDB will re-request the
981 remainder, which might be in a different block of this
983 if (maddr <= offset && offset < (maddr + mlen))
985 amt = (maddr + mlen) - offset;
990 lseek (trace_fd, offset - maddr, SEEK_CUR);
991 tfile_read (readbuf, amt);
993 return TARGET_XFER_OK;
996 if (offset < maddr && maddr < (offset + len))
997 if (low_addr_available == 0 || low_addr_available > maddr)
998 low_addr_available = maddr;
1000 /* Skip over this block. */
1001 pos += (8 + 2 + mlen);
1004 /* Requested memory is unavailable in the context of traceframes,
1005 and this address falls within a read-only section, fallback
1006 to reading from executable, up to LOW_ADDR_AVAILABLE. */
1007 if (offset < low_addr_available)
1008 len = std::min (len, low_addr_available - offset);
1009 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1011 if (res == TARGET_XFER_OK)
1012 return TARGET_XFER_OK;
1015 /* No use trying further, we know some memory starting
1016 at MEMADDR isn't available. */
1018 return TARGET_XFER_UNAVAILABLE;
1023 /* Fallback to reading from read-only sections. */
1024 return section_table_read_available_memory (readbuf, offset, len,
1029 /* Iterate through the blocks of a trace frame, looking for a 'V'
1030 block with a matching tsv number. */
1033 tfile_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
1038 /* Iterate over blocks in current frame and find the last 'V'
1039 block in which tsv number is TSVNUM. In one trace frame, there
1040 may be multiple 'V' blocks created for a given trace variable,
1041 and the last matched 'V' block contains the updated value. */
1043 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
1047 tfile_read ((gdb_byte *) &vnum, 4);
1048 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
1050 (target_gdbarch ()));
1053 tfile_read ((gdb_byte *) val, 8);
1054 *val = extract_signed_integer ((gdb_byte *) val, 8,
1056 (target_gdbarch ()));
1065 /* Callback for traceframe_walk_blocks. Builds a traceframe_info
1066 object for the tfile target's current traceframe. */
1069 build_traceframe_info (char blocktype, void *data)
1071 struct traceframe_info *info = (struct traceframe_info *) data;
1078 unsigned short mlen;
1080 tfile_read ((gdb_byte *) &maddr, 8);
1081 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
1083 (target_gdbarch ()));
1084 tfile_read ((gdb_byte *) &mlen, 2);
1085 mlen = (unsigned short)
1086 extract_unsigned_integer ((gdb_byte *) &mlen,
1087 2, gdbarch_byte_order
1088 (target_gdbarch ()));
1090 info->memory.emplace_back (maddr, mlen);
1097 tfile_read ((gdb_byte *) &vnum, 4);
1098 info->tvars.push_back (vnum);
1106 warning (_("Unhandled trace block type (%d) '%c ' "
1107 "while building trace frame info."),
1108 blocktype, blocktype);
1116 tfile_target::traceframe_info ()
1118 traceframe_info_up info (new struct traceframe_info);
1120 traceframe_walk_blocks (build_traceframe_info, 0, info.get ());
1125 /* Handles tdesc lines from tfile by appending the payload to
1126 a global trace_tdesc variable. */
1129 tfile_append_tdesc_line (const char *line)
1131 buffer_grow_str (&trace_tdesc, line);
1132 buffer_grow_str (&trace_tdesc, "\n");
1136 _initialize_tracefile_tfile (void)
1138 add_target (tfile_target_info, tfile_target_open, filename_completer);