1 /* Trace file TFILE format support in GDB.
3 Copyright (C) 1997-2017 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"
41 /* TFILE trace writer. */
43 struct tfile_trace_file_writer
45 struct trace_file_writer base;
47 /* File pointer to tfile trace file. */
49 /* Path name of the tfile trace file. */
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. */
58 tfile_target_save (struct trace_file_writer *self,
61 int err = target_save_trace_data (filename);
66 /* This is the implementation of trace_file_write_ops method
70 tfile_dtor (struct trace_file_writer *self)
72 struct tfile_trace_file_writer *writer
73 = (struct tfile_trace_file_writer *) self;
75 xfree (writer->pathname);
77 if (writer->fp != NULL)
81 /* This is the implementation of trace_file_write_ops method
82 start. It creates the trace file FILENAME and registers some
86 tfile_start (struct trace_file_writer *self, const char *filename)
88 struct tfile_trace_file_writer *writer
89 = (struct tfile_trace_file_writer *) self;
91 writer->pathname = tilde_expand (filename);
92 writer->fp = gdb_fopen_cloexec (writer->pathname, "wb").release ();
93 if (writer->fp == NULL)
94 error (_("Unable to open file '%s' for saving trace data (%s)"),
95 writer->pathname, safe_strerror (errno));
98 /* This is the implementation of trace_file_write_ops method
99 write_header. Write the TFILE header. */
102 tfile_write_header (struct trace_file_writer *self)
104 struct tfile_trace_file_writer *writer
105 = (struct tfile_trace_file_writer *) self;
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);
113 perror_with_name (writer->pathname);
116 /* This is the implementation of trace_file_write_ops method
117 write_regblock_type. Write the size of register block. */
120 tfile_write_regblock_type (struct trace_file_writer *self, int size)
122 struct tfile_trace_file_writer *writer
123 = (struct tfile_trace_file_writer *) self;
125 fprintf (writer->fp, "R %x\n", size);
128 /* This is the implementation of trace_file_write_ops method
132 tfile_write_status (struct trace_file_writer *self,
133 struct trace_status *ts)
135 struct tfile_trace_file_writer *writer
136 = (struct tfile_trace_file_writer *) self;
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
141 || ts->stop_reason == trace_stop_command)
143 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
145 bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
146 fprintf (writer->fp, ":%s", buf);
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);
163 fprintf (writer->fp, ";starttime:%s",
164 phex_nz (ts->start_time, sizeof (ts->start_time)));
168 fprintf (writer->fp, ";stoptime:%s",
169 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
171 if (ts->notes != NULL)
173 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
175 bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
176 fprintf (writer->fp, ";notes:%s", buf);
178 if (ts->user_name != NULL)
180 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
182 bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
183 fprintf (writer->fp, ";username:%s", buf);
185 fprintf (writer->fp, "\n");
188 /* This is the implementation of trace_file_write_ops method
189 write_uploaded_tsv. */
192 tfile_write_uploaded_tsv (struct trace_file_writer *self,
193 struct uploaded_tsv *utsv)
196 struct tfile_trace_file_writer *writer
197 = (struct tfile_trace_file_writer *) self;
201 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
202 bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
205 fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
206 utsv->number, phex_nz (utsv->initial_value, 8),
207 utsv->builtin, buf != NULL ? buf : "");
213 #define MAX_TRACE_UPLOAD 2000
215 /* This is the implementation of trace_file_write_ops method
216 write_uploaded_tp. */
219 tfile_write_uploaded_tp (struct trace_file_writer *self,
220 struct uploaded_tp *utp)
222 struct tfile_trace_file_writer *writer
223 = (struct tfile_trace_file_writer *) self;
226 char buf[MAX_TRACE_UPLOAD];
228 fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
229 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
230 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
231 if (utp->type == bp_fast_tracepoint)
232 fprintf (writer->fp, ":F%x", utp->orig_size);
235 ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
237 fprintf (writer->fp, "\n");
238 for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
239 fprintf (writer->fp, "tp A%x:%s:%s\n",
240 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
241 for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
242 fprintf (writer->fp, "tp S%x:%s:%s\n",
243 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
246 encode_source_string (utp->number, utp->addr,
247 "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
248 fprintf (writer->fp, "tp Z%s\n", buf);
250 if (utp->cond_string)
252 encode_source_string (utp->number, utp->addr,
253 "cond", utp->cond_string,
254 buf, MAX_TRACE_UPLOAD);
255 fprintf (writer->fp, "tp Z%s\n", buf);
257 for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
259 encode_source_string (utp->number, utp->addr, "cmd", act,
260 buf, MAX_TRACE_UPLOAD);
261 fprintf (writer->fp, "tp Z%s\n", buf);
263 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
264 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
266 phex_nz (utp->traceframe_usage,
267 sizeof (utp->traceframe_usage)));
270 /* This is the implementation of trace_file_write_ops method
274 tfile_write_tdesc (struct trace_file_writer *self)
276 struct tfile_trace_file_writer *writer
277 = (struct tfile_trace_file_writer *) self;
279 gdb::optional<std::string> tdesc
280 = target_fetch_description_xml (¤t_target);
285 const char *ptr = tdesc->c_str ();
287 /* Write tdesc line by line, prefixing each line with "tdesc ". */
290 const char *next = strchr (ptr, '\n');
293 fprintf (writer->fp, "tdesc %.*s\n", (int) (next - ptr), ptr);
297 else if (*ptr != '\0')
299 /* Last line, doesn't have a newline. */
300 fprintf (writer->fp, "tdesc %s\n", ptr);
306 /* This is the implementation of trace_file_write_ops method
307 write_definition_end. */
310 tfile_write_definition_end (struct trace_file_writer *self)
312 struct tfile_trace_file_writer *writer
313 = (struct tfile_trace_file_writer *) self;
315 fprintf (writer->fp, "\n");
318 /* This is the implementation of trace_file_write_ops method
322 tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
325 struct tfile_trace_file_writer *writer
326 = (struct tfile_trace_file_writer *) self;
328 if (fwrite (buf, len, 1, writer->fp) < 1)
329 perror_with_name (writer->pathname);
332 /* This is the implementation of trace_file_write_ops method
336 tfile_end (struct trace_file_writer *self)
338 struct tfile_trace_file_writer *writer
339 = (struct tfile_trace_file_writer *) self;
342 /* Mark the end of trace data. */
343 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
344 perror_with_name (writer->pathname);
347 /* Operations to write trace buffers into TFILE format. */
349 static const struct trace_file_write_ops tfile_write_ops =
355 tfile_write_regblock_type,
357 tfile_write_uploaded_tsv,
358 tfile_write_uploaded_tp,
360 tfile_write_definition_end,
361 tfile_write_raw_data,
366 /* Return a trace writer for TFILE format. */
368 struct trace_file_writer *
369 tfile_trace_file_writer_new (void)
371 struct tfile_trace_file_writer *writer
372 = XNEW (struct tfile_trace_file_writer);
374 writer->base.ops = &tfile_write_ops;
376 writer->pathname = NULL;
378 return (struct trace_file_writer *) writer;
381 /* target tfile command */
383 static struct target_ops tfile_ops;
385 /* Fill in tfile_ops with its defined operations and properties. */
387 #define TRACE_HEADER_SIZE 8
389 #define TFILE_PID (1)
391 static char *trace_filename;
392 static int trace_fd = -1;
393 static off_t trace_frames_offset;
394 static off_t cur_offset;
395 static int cur_data_size;
396 int trace_regblock_size;
397 static struct buffer trace_tdesc;
399 static void tfile_append_tdesc_line (const char *line);
400 static void tfile_interp_line (char *line,
401 struct uploaded_tp **utpp,
402 struct uploaded_tsv **utsvp);
404 /* Read SIZE bytes into READBUF from the trace frame, starting at
405 TRACE_FD's current position. Note that this call `read'
406 underneath, hence it advances the file's seek position. Throws an
407 error if the `read' syscall fails, or less than SIZE bytes are
411 tfile_read (gdb_byte *readbuf, int size)
415 gotten = read (trace_fd, readbuf, size);
417 perror_with_name (trace_filename);
418 else if (gotten < size)
419 error (_("Premature end of file while reading trace file"));
423 tfile_open (const char *arg, int from_tty)
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;
436 target_preopen (from_tty);
438 error (_("No trace file specified."));
440 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
441 if (!IS_ABSOLUTE_PATH (filename.get ()))
442 filename.reset (concat (current_directory, "/", filename.get (),
445 flags = O_BINARY | O_LARGEFILE;
447 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
448 if (scratch_chan < 0)
449 perror_with_name (filename.get ());
451 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
453 unpush_target (&tfile_ops);
455 trace_filename = filename.release ();
456 trace_fd = scratch_chan;
458 /* Make sure this is clear. */
459 buffer_free (&trace_tdesc);
462 /* Read the file header and test for validity. */
463 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
465 bytes += TRACE_HEADER_SIZE;
466 if (!(header[0] == 0x7f
467 && (startswith (header + 1, "TRACE0\n"))))
468 error (_("File is not a valid trace file."));
470 push_target (&tfile_ops);
472 trace_regblock_size = 0;
473 ts = current_trace_status ();
474 /* We know we're working with a file. Record its name. */
475 ts->filename = trace_filename;
476 /* Set defaults in case there is no status line. */
477 ts->running_known = 0;
478 ts->stop_reason = trace_stop_reason_unknown;
479 ts->traceframe_count = -1;
481 ts->disconnected_tracing = 0;
482 ts->circular_buffer = 0;
486 /* Read through a section of newline-terminated lines that
487 define things like tracepoints. */
491 tfile_read (&byte, 1);
496 /* Empty line marks end of the definition section. */
501 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
506 error (_("Excessively long lines in trace file"));
509 /* By now, tdesc lines have been read from tfile - let's parse them. */
510 target_find_description ();
512 /* Record the starting offset of the binary trace data. */
513 trace_frames_offset = bytes;
515 /* If we don't have a blocksize, we can't interpret the
517 if (trace_regblock_size == 0)
518 error (_("No register block size recorded in trace file"));
520 CATCH (ex, RETURN_MASK_ALL)
522 /* Remove the partially set up target. */
523 unpush_target (&tfile_ops);
524 throw_exception (ex);
528 inferior_appeared (current_inferior (), TFILE_PID);
529 inferior_ptid = pid_to_ptid (TFILE_PID);
530 add_thread_silent (inferior_ptid);
532 if (ts->traceframe_count <= 0)
533 warning (_("No traceframes present in this file."));
535 /* Add the file's tracepoints and variables into the current mix. */
537 /* Get trace state variables first, they may be checked when parsing
538 uploaded commands. */
539 merge_uploaded_trace_state_variables (&uploaded_tsvs);
541 merge_uploaded_tracepoints (&uploaded_tps);
543 post_create_inferior (&tfile_ops, from_tty);
546 /* Interpret the given line from the definitions part of the trace
550 tfile_interp_line (char *line, struct uploaded_tp **utpp,
551 struct uploaded_tsv **utsvp)
555 if (startswith (p, "R "))
558 trace_regblock_size = strtol (p, &p, 16);
560 else if (startswith (p, "status "))
562 p += strlen ("status ");
563 parse_trace_status (p, current_trace_status ());
565 else if (startswith (p, "tp "))
568 parse_tracepoint_definition (p, utpp);
570 else if (startswith (p, "tsv "))
572 p += strlen ("tsv ");
573 parse_tsv_definition (p, utsvp);
575 else if (startswith (p, "tdesc "))
577 p += strlen ("tdesc ");
578 tfile_append_tdesc_line (p);
581 warning (_("Ignoring trace file definition \"%s\""), line);
584 /* Close the trace file and generally clean up. */
587 tfile_close (struct target_ops *self)
594 pid = ptid_get_pid (inferior_ptid);
595 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
596 exit_inferior_silent (pid);
600 xfree (trace_filename);
601 trace_filename = NULL;
602 buffer_free (&trace_tdesc);
604 trace_reset_local_state ();
608 tfile_files_info (struct target_ops *t)
610 printf_filtered ("\t`%s'\n", trace_filename);
614 tfile_get_tracepoint_status (struct target_ops *self,
615 struct breakpoint *tp, struct uploaded_tp *utp)
617 /* Other bits of trace status were collected as part of opening the
618 trace files, so nothing to do here. */
621 /* Given the position of a traceframe in the file, figure out what
622 address the frame was collected at. This would normally be the
623 value of a collected PC register, but if not available, we
627 tfile_get_traceframe_address (off_t tframe_offset)
631 struct tracepoint *tp;
632 off_t saved_offset = cur_offset;
634 /* FIXME dig pc out of collected registers. */
636 /* Fall back to using tracepoint address. */
637 lseek (trace_fd, tframe_offset, SEEK_SET);
638 tfile_read ((gdb_byte *) &tpnum, 2);
639 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
641 (target_gdbarch ()));
643 tp = get_tracepoint_by_number_on_target (tpnum);
644 /* FIXME this is a poor heuristic if multiple locations. */
646 addr = tp->loc->address;
648 /* Restore our seek position. */
649 cur_offset = saved_offset;
650 lseek (trace_fd, cur_offset, SEEK_SET);
654 /* Given a type of search and some parameters, scan the collection of
655 traceframes in the file looking for a match. When found, return
656 both the traceframe and tracepoint number, otherwise -1 for
660 tfile_trace_find (struct target_ops *self, enum trace_find_type type, int num,
661 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
664 int tfnum = 0, found = 0;
665 unsigned int data_size;
666 struct tracepoint *tp;
667 off_t offset, tframe_offset;
677 lseek (trace_fd, trace_frames_offset, SEEK_SET);
678 offset = trace_frames_offset;
681 tframe_offset = offset;
682 tfile_read ((gdb_byte *) &tpnum, 2);
683 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
685 (target_gdbarch ()));
689 tfile_read ((gdb_byte *) &data_size, 4);
690 data_size = (unsigned int) extract_unsigned_integer
691 ((gdb_byte *) &data_size, 4,
692 gdbarch_byte_order (target_gdbarch ()));
695 if (type == tfind_number)
697 /* Looking for a specific trace frame. */
703 /* Start from the _next_ trace frame. */
704 if (tfnum > get_traceframe_number ())
709 tfaddr = tfile_get_traceframe_address (tframe_offset);
714 tp = get_tracepoint (num);
715 if (tp && tpnum == tp->number_on_target)
719 tfaddr = tfile_get_traceframe_address (tframe_offset);
720 if (addr1 <= tfaddr && tfaddr <= addr2)
724 tfaddr = tfile_get_traceframe_address (tframe_offset);
725 if (!(addr1 <= tfaddr && tfaddr <= addr2))
729 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
739 cur_data_size = data_size;
743 /* Skip past the traceframe's data. */
744 lseek (trace_fd, data_size, SEEK_CUR);
746 /* Update our own count of traceframes. */
749 /* Did not find what we were looking for. */
755 /* Prototype of the callback passed to tframe_walk_blocks. */
756 typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
758 /* Callback for traceframe_walk_blocks, used to find a given block
759 type in a traceframe. */
762 match_blocktype (char blocktype, void *data)
764 char *wantedp = (char *) data;
766 if (*wantedp == blocktype)
772 /* Walk over all traceframe block starting at POS offset from
773 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
774 unmodified. If CALLBACK returns true, this returns the position in
775 the traceframe where the block is found, relative to the start of
776 the traceframe (cur_offset). Returns -1 if no callback call
777 returned true, indicating that all blocks have been walked. */
780 traceframe_walk_blocks (walk_blocks_callback_func callback,
783 /* Iterate through a traceframe's blocks, looking for a block of the
786 lseek (trace_fd, cur_offset + pos, SEEK_SET);
787 while (pos < cur_data_size)
792 tfile_read ((gdb_byte *) &block_type, 1);
796 if ((*callback) (block_type, data))
802 lseek (trace_fd, cur_offset + pos + trace_regblock_size, SEEK_SET);
803 pos += trace_regblock_size;
806 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
807 tfile_read ((gdb_byte *) &mlen, 2);
808 mlen = (unsigned short)
809 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
811 (target_gdbarch ()));
812 lseek (trace_fd, mlen, SEEK_CUR);
813 pos += (8 + 2 + mlen);
816 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
820 error (_("Unknown block type '%c' (0x%x) in trace frame"),
821 block_type, block_type);
829 /* Convenience wrapper around traceframe_walk_blocks. Looks for the
830 position offset of a block of type TYPE_WANTED in the current trace
831 frame, starting at POS. Returns -1 if no such block was found. */
834 traceframe_find_block_type (char type_wanted, int pos)
836 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
839 /* Look for a block of saved registers in the traceframe, and get the
840 requested register from it. */
843 tfile_fetch_registers (struct target_ops *ops,
844 struct regcache *regcache, int regno)
846 struct gdbarch *gdbarch = get_regcache_arch (regcache);
847 int offset, regn, regsize, dummy;
849 /* An uninitialized reg size says we're not going to be
850 successful at getting register blocks. */
851 if (!trace_regblock_size)
854 if (traceframe_find_block_type ('R', 0) >= 0)
856 gdb_byte *regs = (gdb_byte *) alloca (trace_regblock_size);
858 tfile_read (regs, trace_regblock_size);
860 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
862 if (!remote_register_number_and_offset (get_regcache_arch (regcache),
863 regn, &dummy, &offset))
866 regsize = register_size (gdbarch, regn);
867 /* Make sure we stay within block bounds. */
868 if (offset + regsize > trace_regblock_size)
870 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
874 regcache_raw_supply (regcache, regno, regs + offset);
877 else if (regno == -1)
879 regcache_raw_supply (regcache, regn, regs + offset);
885 tracefile_fetch_registers (regcache, regno);
888 static enum target_xfer_status
889 tfile_xfer_partial_features (struct target_ops *ops, const char *annex,
890 gdb_byte *readbuf, const gdb_byte *writebuf,
891 ULONGEST offset, ULONGEST len,
892 ULONGEST *xfered_len)
894 if (strcmp (annex, "target.xml"))
895 return TARGET_XFER_E_IO;
898 error (_("tfile_xfer_partial: tdesc is read-only"));
900 if (trace_tdesc.used_size == 0)
901 return TARGET_XFER_E_IO;
903 if (offset >= trace_tdesc.used_size)
904 return TARGET_XFER_EOF;
906 if (len > trace_tdesc.used_size - offset)
907 len = trace_tdesc.used_size - offset;
909 memcpy (readbuf, trace_tdesc.buffer + offset, len);
912 return TARGET_XFER_OK;
915 static enum target_xfer_status
916 tfile_xfer_partial (struct target_ops *ops, enum target_object object,
917 const char *annex, gdb_byte *readbuf,
918 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
919 ULONGEST *xfered_len)
921 /* We're only doing regular memory and tdesc for now. */
922 if (object == TARGET_OBJECT_AVAILABLE_FEATURES)
923 return tfile_xfer_partial_features (ops, annex, readbuf, writebuf,
924 offset, len, xfered_len);
925 if (object != TARGET_OBJECT_MEMORY)
926 return TARGET_XFER_E_IO;
929 error (_("tfile_xfer_partial: trace file is read-only"));
931 if (get_traceframe_number () != -1)
934 enum target_xfer_status res;
935 /* Records the lowest available address of all blocks that
936 intersects the requested range. */
937 ULONGEST low_addr_available = 0;
939 /* Iterate through the traceframe's blocks, looking for
941 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
945 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
947 tfile_read ((gdb_byte *) &maddr, 8);
948 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
950 tfile_read ((gdb_byte *) &mlen, 2);
951 mlen = (unsigned short)
952 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
954 /* If the block includes the first part of the desired
955 range, return as much it has; GDB will re-request the
956 remainder, which might be in a different block of this
958 if (maddr <= offset && offset < (maddr + mlen))
960 amt = (maddr + mlen) - offset;
965 lseek (trace_fd, offset - maddr, SEEK_CUR);
966 tfile_read (readbuf, amt);
968 return TARGET_XFER_OK;
971 if (offset < maddr && maddr < (offset + len))
972 if (low_addr_available == 0 || low_addr_available > maddr)
973 low_addr_available = maddr;
975 /* Skip over this block. */
976 pos += (8 + 2 + mlen);
979 /* Requested memory is unavailable in the context of traceframes,
980 and this address falls within a read-only section, fallback
981 to reading from executable, up to LOW_ADDR_AVAILABLE. */
982 if (offset < low_addr_available)
983 len = std::min (len, low_addr_available - offset);
984 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
986 if (res == TARGET_XFER_OK)
987 return TARGET_XFER_OK;
990 /* No use trying further, we know some memory starting
991 at MEMADDR isn't available. */
993 return TARGET_XFER_UNAVAILABLE;
998 /* Fallback to reading from read-only sections. */
999 return section_table_read_available_memory (readbuf, offset, len,
1004 /* Iterate through the blocks of a trace frame, looking for a 'V'
1005 block with a matching tsv number. */
1008 tfile_get_trace_state_variable_value (struct target_ops *self,
1009 int tsvnum, LONGEST *val)
1014 /* Iterate over blocks in current frame and find the last 'V'
1015 block in which tsv number is TSVNUM. In one trace frame, there
1016 may be multiple 'V' blocks created for a given trace variable,
1017 and the last matched 'V' block contains the updated value. */
1019 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
1023 tfile_read ((gdb_byte *) &vnum, 4);
1024 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
1026 (target_gdbarch ()));
1029 tfile_read ((gdb_byte *) val, 8);
1030 *val = extract_signed_integer ((gdb_byte *) val, 8,
1032 (target_gdbarch ()));
1041 /* Callback for traceframe_walk_blocks. Builds a traceframe_info
1042 object for the tfile target's current traceframe. */
1045 build_traceframe_info (char blocktype, void *data)
1047 struct traceframe_info *info = (struct traceframe_info *) data;
1053 struct mem_range *r;
1055 unsigned short mlen;
1057 tfile_read ((gdb_byte *) &maddr, 8);
1058 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
1060 (target_gdbarch ()));
1061 tfile_read ((gdb_byte *) &mlen, 2);
1062 mlen = (unsigned short)
1063 extract_unsigned_integer ((gdb_byte *) &mlen,
1064 2, gdbarch_byte_order
1065 (target_gdbarch ()));
1067 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1077 tfile_read ((gdb_byte *) &vnum, 4);
1078 VEC_safe_push (int, info->tvars, vnum);
1086 warning (_("Unhandled trace block type (%d) '%c ' "
1087 "while building trace frame info."),
1088 blocktype, blocktype);
1095 static struct traceframe_info *
1096 tfile_traceframe_info (struct target_ops *self)
1098 struct traceframe_info *info = XCNEW (struct traceframe_info);
1100 traceframe_walk_blocks (build_traceframe_info, 0, info);
1104 /* Handles tdesc lines from tfile by appending the payload to
1105 a global trace_tdesc variable. */
1108 tfile_append_tdesc_line (const char *line)
1110 buffer_grow_str (&trace_tdesc, line);
1111 buffer_grow_str (&trace_tdesc, "\n");
1115 init_tfile_ops (void)
1117 init_tracefile_ops (&tfile_ops);
1119 tfile_ops.to_shortname = "tfile";
1120 tfile_ops.to_longname = "Local trace dump file";
1122 = "Use a trace file as a target. Specify the filename of the trace file.";
1123 tfile_ops.to_open = tfile_open;
1124 tfile_ops.to_close = tfile_close;
1125 tfile_ops.to_fetch_registers = tfile_fetch_registers;
1126 tfile_ops.to_xfer_partial = tfile_xfer_partial;
1127 tfile_ops.to_files_info = tfile_files_info;
1128 tfile_ops.to_get_tracepoint_status = tfile_get_tracepoint_status;
1129 tfile_ops.to_trace_find = tfile_trace_find;
1130 tfile_ops.to_get_trace_state_variable_value
1131 = tfile_get_trace_state_variable_value;
1132 tfile_ops.to_traceframe_info = tfile_traceframe_info;
1136 _initialize_tracefile_tfile (void)
1140 add_target_with_completer (&tfile_ops, filename_completer);