1 /* Trace file TFILE format support in GDB.
3 Copyright (C) 1997-2014 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"
36 /* TFILE trace writer. */
38 struct tfile_trace_file_writer
40 struct trace_file_writer base;
42 /* File pointer to tfile trace file. */
44 /* Path name of the tfile trace file. */
48 /* This is the implementation of trace_file_write_ops method
49 target_save. We just call the generic target
50 target_save_trace_data to do target-side saving. */
53 tfile_target_save (struct trace_file_writer *self,
56 int err = target_save_trace_data (filename);
61 /* This is the implementation of trace_file_write_ops method
65 tfile_dtor (struct trace_file_writer *self)
67 struct tfile_trace_file_writer *writer
68 = (struct tfile_trace_file_writer *) self;
70 xfree (writer->pathname);
72 if (writer->fp != NULL)
76 /* This is the implementation of trace_file_write_ops method
77 start. It creates the trace file FILENAME and registers some
81 tfile_start (struct trace_file_writer *self, const char *filename)
83 struct tfile_trace_file_writer *writer
84 = (struct tfile_trace_file_writer *) self;
86 writer->pathname = tilde_expand (filename);
87 writer->fp = gdb_fopen_cloexec (writer->pathname, "wb");
88 if (writer->fp == NULL)
89 error (_("Unable to open file '%s' for saving trace data (%s)"),
90 writer->pathname, safe_strerror (errno));
93 /* This is the implementation of trace_file_write_ops method
94 write_header. Write the TFILE header. */
97 tfile_write_header (struct trace_file_writer *self)
99 struct tfile_trace_file_writer *writer
100 = (struct tfile_trace_file_writer *) self;
103 /* Write a file header, with a high-bit-set char to indicate a
104 binary file, plus a hint as what this file is, and a version
105 number in case of future needs. */
106 written = fwrite ("\x7fTRACE0\n", 8, 1, writer->fp);
108 perror_with_name (writer->pathname);
111 /* This is the implementation of trace_file_write_ops method
112 write_regblock_type. Write the size of register block. */
115 tfile_write_regblock_type (struct trace_file_writer *self, int size)
117 struct tfile_trace_file_writer *writer
118 = (struct tfile_trace_file_writer *) self;
120 fprintf (writer->fp, "R %x\n", size);
123 /* This is the implementation of trace_file_write_ops method
127 tfile_write_status (struct trace_file_writer *self,
128 struct trace_status *ts)
130 struct tfile_trace_file_writer *writer
131 = (struct tfile_trace_file_writer *) self;
133 fprintf (writer->fp, "status %c;%s",
134 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
135 if (ts->stop_reason == tracepoint_error
136 || ts->stop_reason == tstop_command)
138 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
140 bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
141 fprintf (writer->fp, ":%s", buf);
143 fprintf (writer->fp, ":%x", ts->stopping_tracepoint);
144 if (ts->traceframe_count >= 0)
145 fprintf (writer->fp, ";tframes:%x", ts->traceframe_count);
146 if (ts->traceframes_created >= 0)
147 fprintf (writer->fp, ";tcreated:%x", ts->traceframes_created);
148 if (ts->buffer_free >= 0)
149 fprintf (writer->fp, ";tfree:%x", ts->buffer_free);
150 if (ts->buffer_size >= 0)
151 fprintf (writer->fp, ";tsize:%x", ts->buffer_size);
152 if (ts->disconnected_tracing)
153 fprintf (writer->fp, ";disconn:%x", ts->disconnected_tracing);
154 if (ts->circular_buffer)
155 fprintf (writer->fp, ";circular:%x", ts->circular_buffer);
158 fprintf (writer->fp, ";starttime:%s",
159 phex_nz (ts->start_time, sizeof (ts->start_time)));
163 fprintf (writer->fp, ";stoptime:%s",
164 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
166 if (ts->notes != NULL)
168 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
170 bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
171 fprintf (writer->fp, ";notes:%s", buf);
173 if (ts->user_name != NULL)
175 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
177 bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
178 fprintf (writer->fp, ";username:%s", buf);
180 fprintf (writer->fp, "\n");
183 /* This is the implementation of trace_file_write_ops method
184 write_uploaded_tsv. */
187 tfile_write_uploaded_tsv (struct trace_file_writer *self,
188 struct uploaded_tsv *utsv)
191 struct tfile_trace_file_writer *writer
192 = (struct tfile_trace_file_writer *) self;
196 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
197 bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
200 fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
201 utsv->number, phex_nz (utsv->initial_value, 8),
208 #define MAX_TRACE_UPLOAD 2000
210 /* This is the implementation of trace_file_write_ops method
211 write_uploaded_tp. */
214 tfile_write_uploaded_tp (struct trace_file_writer *self,
215 struct uploaded_tp *utp)
217 struct tfile_trace_file_writer *writer
218 = (struct tfile_trace_file_writer *) self;
221 char buf[MAX_TRACE_UPLOAD];
223 fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
224 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
225 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
226 if (utp->type == bp_fast_tracepoint)
227 fprintf (writer->fp, ":F%x", utp->orig_size);
230 ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
232 fprintf (writer->fp, "\n");
233 for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
234 fprintf (writer->fp, "tp A%x:%s:%s\n",
235 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
236 for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
237 fprintf (writer->fp, "tp S%x:%s:%s\n",
238 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
241 encode_source_string (utp->number, utp->addr,
242 "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
243 fprintf (writer->fp, "tp Z%s\n", buf);
245 if (utp->cond_string)
247 encode_source_string (utp->number, utp->addr,
248 "cond", utp->cond_string,
249 buf, MAX_TRACE_UPLOAD);
250 fprintf (writer->fp, "tp Z%s\n", buf);
252 for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
254 encode_source_string (utp->number, utp->addr, "cmd", act,
255 buf, MAX_TRACE_UPLOAD);
256 fprintf (writer->fp, "tp Z%s\n", buf);
258 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
259 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
261 phex_nz (utp->traceframe_usage,
262 sizeof (utp->traceframe_usage)));
265 /* This is the implementation of trace_file_write_ops method
266 write_definition_end. */
269 tfile_write_definition_end (struct trace_file_writer *self)
271 struct tfile_trace_file_writer *writer
272 = (struct tfile_trace_file_writer *) self;
274 fprintf (writer->fp, "\n");
277 /* This is the implementation of trace_file_write_ops method
281 tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
284 struct tfile_trace_file_writer *writer
285 = (struct tfile_trace_file_writer *) self;
287 if (fwrite (buf, len, 1, writer->fp) < 1)
288 perror_with_name (writer->pathname);
291 /* This is the implementation of trace_file_write_ops method
295 tfile_end (struct trace_file_writer *self)
297 struct tfile_trace_file_writer *writer
298 = (struct tfile_trace_file_writer *) self;
301 /* Mark the end of trace data. */
302 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
303 perror_with_name (writer->pathname);
306 /* Operations to write trace buffers into TFILE format. */
308 static const struct trace_file_write_ops tfile_write_ops =
314 tfile_write_regblock_type,
316 tfile_write_uploaded_tsv,
317 tfile_write_uploaded_tp,
318 tfile_write_definition_end,
319 tfile_write_raw_data,
324 /* Return a trace writer for TFILE format. */
326 struct trace_file_writer *
327 tfile_trace_file_writer_new (void)
329 struct tfile_trace_file_writer *writer
330 = xmalloc (sizeof (struct tfile_trace_file_writer));
332 writer->base.ops = &tfile_write_ops;
334 writer->pathname = NULL;
336 return (struct trace_file_writer *) writer;
339 /* target tfile command */
341 static struct target_ops tfile_ops;
343 /* Fill in tfile_ops with its defined operations and properties. */
345 #define TRACE_HEADER_SIZE 8
347 #define TFILE_PID (1)
349 static char *trace_filename;
350 static int trace_fd = -1;
351 static off_t trace_frames_offset;
352 static off_t cur_offset;
353 static int cur_data_size;
354 int trace_regblock_size;
356 static void tfile_interp_line (char *line,
357 struct uploaded_tp **utpp,
358 struct uploaded_tsv **utsvp);
360 /* Read SIZE bytes into READBUF from the trace frame, starting at
361 TRACE_FD's current position. Note that this call `read'
362 underneath, hence it advances the file's seek position. Throws an
363 error if the `read' syscall fails, or less than SIZE bytes are
367 tfile_read (gdb_byte *readbuf, int size)
371 gotten = read (trace_fd, readbuf, size);
373 perror_with_name (trace_filename);
374 else if (gotten < size)
375 error (_("Premature end of file while reading trace file"));
379 tfile_open (char *filename, int from_tty)
381 volatile struct gdb_exception ex;
383 struct cleanup *old_chain;
386 char header[TRACE_HEADER_SIZE];
387 char linebuf[1000]; /* Should be max remote packet size or so. */
390 struct trace_status *ts;
391 struct uploaded_tp *uploaded_tps = NULL;
392 struct uploaded_tsv *uploaded_tsvs = NULL;
394 target_preopen (from_tty);
396 error (_("No trace file specified."));
398 filename = tilde_expand (filename);
399 if (!IS_ABSOLUTE_PATH(filename))
401 temp = concat (current_directory, "/", filename, (char *) NULL);
406 old_chain = make_cleanup (xfree, filename);
408 flags = O_BINARY | O_LARGEFILE;
410 scratch_chan = gdb_open_cloexec (filename, flags, 0);
411 if (scratch_chan < 0)
412 perror_with_name (filename);
414 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
416 discard_cleanups (old_chain); /* Don't free filename any more. */
417 unpush_target (&tfile_ops);
419 trace_filename = xstrdup (filename);
420 trace_fd = scratch_chan;
423 /* Read the file header and test for validity. */
424 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
426 bytes += TRACE_HEADER_SIZE;
427 if (!(header[0] == 0x7f
428 && (strncmp (header + 1, "TRACE0\n", 7) == 0)))
429 error (_("File is not a valid trace file."));
431 push_target (&tfile_ops);
433 trace_regblock_size = 0;
434 ts = current_trace_status ();
435 /* We know we're working with a file. Record its name. */
436 ts->filename = trace_filename;
437 /* Set defaults in case there is no status line. */
438 ts->running_known = 0;
439 ts->stop_reason = trace_stop_reason_unknown;
440 ts->traceframe_count = -1;
442 ts->disconnected_tracing = 0;
443 ts->circular_buffer = 0;
445 TRY_CATCH (ex, RETURN_MASK_ALL)
447 /* Read through a section of newline-terminated lines that
448 define things like tracepoints. */
452 tfile_read (&byte, 1);
457 /* Empty line marks end of the definition section. */
462 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
467 error (_("Excessively long lines in trace file"));
470 /* Record the starting offset of the binary trace data. */
471 trace_frames_offset = bytes;
473 /* If we don't have a blocksize, we can't interpret the
475 if (trace_regblock_size == 0)
476 error (_("No register block size recorded in trace file"));
480 /* Remove the partially set up target. */
481 unpush_target (&tfile_ops);
482 throw_exception (ex);
485 inferior_appeared (current_inferior (), TFILE_PID);
486 inferior_ptid = pid_to_ptid (TFILE_PID);
487 add_thread_silent (inferior_ptid);
489 if (ts->traceframe_count <= 0)
490 warning (_("No traceframes present in this file."));
492 /* Add the file's tracepoints and variables into the current mix. */
494 /* Get trace state variables first, they may be checked when parsing
495 uploaded commands. */
496 merge_uploaded_trace_state_variables (&uploaded_tsvs);
498 merge_uploaded_tracepoints (&uploaded_tps);
500 post_create_inferior (&tfile_ops, from_tty);
503 /* Interpret the given line from the definitions part of the trace
507 tfile_interp_line (char *line, struct uploaded_tp **utpp,
508 struct uploaded_tsv **utsvp)
512 if (strncmp (p, "R ", strlen ("R ")) == 0)
515 trace_regblock_size = strtol (p, &p, 16);
517 else if (strncmp (p, "status ", strlen ("status ")) == 0)
519 p += strlen ("status ");
520 parse_trace_status (p, current_trace_status ());
522 else if (strncmp (p, "tp ", strlen ("tp ")) == 0)
525 parse_tracepoint_definition (p, utpp);
527 else if (strncmp (p, "tsv ", strlen ("tsv ")) == 0)
529 p += strlen ("tsv ");
530 parse_tsv_definition (p, utsvp);
533 warning (_("Ignoring trace file definition \"%s\""), line);
536 /* Close the trace file and generally clean up. */
539 tfile_close (struct target_ops *self)
546 pid = ptid_get_pid (inferior_ptid);
547 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff. */
548 exit_inferior_silent (pid);
552 xfree (trace_filename);
553 trace_filename = NULL;
555 trace_reset_local_state ();
559 tfile_files_info (struct target_ops *t)
561 printf_filtered ("\t`%s'\n", trace_filename);
564 /* The trace status for a file is that tracing can never be run. */
567 tfile_get_trace_status (struct target_ops *self, struct trace_status *ts)
569 /* Other bits of trace status were collected as part of opening the
570 trace files, so nothing to do here. */
576 tfile_get_tracepoint_status (struct target_ops *self,
577 struct breakpoint *tp, struct uploaded_tp *utp)
579 /* Other bits of trace status were collected as part of opening the
580 trace files, so nothing to do here. */
583 /* Given the position of a traceframe in the file, figure out what
584 address the frame was collected at. This would normally be the
585 value of a collected PC register, but if not available, we
589 tfile_get_traceframe_address (off_t tframe_offset)
593 struct tracepoint *tp;
594 off_t saved_offset = cur_offset;
596 /* FIXME dig pc out of collected registers. */
598 /* Fall back to using tracepoint address. */
599 lseek (trace_fd, tframe_offset, SEEK_SET);
600 tfile_read ((gdb_byte *) &tpnum, 2);
601 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
603 (target_gdbarch ()));
605 tp = get_tracepoint_by_number_on_target (tpnum);
606 /* FIXME this is a poor heuristic if multiple locations. */
607 if (tp && tp->base.loc)
608 addr = tp->base.loc->address;
610 /* Restore our seek position. */
611 cur_offset = saved_offset;
612 lseek (trace_fd, cur_offset, SEEK_SET);
616 /* Given a type of search and some parameters, scan the collection of
617 traceframes in the file looking for a match. When found, return
618 both the traceframe and tracepoint number, otherwise -1 for
622 tfile_trace_find (struct target_ops *self, enum trace_find_type type, int num,
623 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
626 int tfnum = 0, found = 0;
627 unsigned int data_size;
628 struct tracepoint *tp;
629 off_t offset, tframe_offset;
639 lseek (trace_fd, trace_frames_offset, SEEK_SET);
640 offset = trace_frames_offset;
643 tframe_offset = offset;
644 tfile_read ((gdb_byte *) &tpnum, 2);
645 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
647 (target_gdbarch ()));
651 tfile_read ((gdb_byte *) &data_size, 4);
652 data_size = (unsigned int) extract_unsigned_integer
653 ((gdb_byte *) &data_size, 4,
654 gdbarch_byte_order (target_gdbarch ()));
657 if (type == tfind_number)
659 /* Looking for a specific trace frame. */
665 /* Start from the _next_ trace frame. */
666 if (tfnum > get_traceframe_number ())
671 tfaddr = tfile_get_traceframe_address (tframe_offset);
676 tp = get_tracepoint (num);
677 if (tp && tpnum == tp->number_on_target)
681 tfaddr = tfile_get_traceframe_address (tframe_offset);
682 if (addr1 <= tfaddr && tfaddr <= addr2)
686 tfaddr = tfile_get_traceframe_address (tframe_offset);
687 if (!(addr1 <= tfaddr && tfaddr <= addr2))
691 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
701 cur_data_size = data_size;
705 /* Skip past the traceframe's data. */
706 lseek (trace_fd, data_size, SEEK_CUR);
708 /* Update our own count of traceframes. */
711 /* Did not find what we were looking for. */
717 /* Prototype of the callback passed to tframe_walk_blocks. */
718 typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
720 /* Callback for traceframe_walk_blocks, used to find a given block
721 type in a traceframe. */
724 match_blocktype (char blocktype, void *data)
726 char *wantedp = data;
728 if (*wantedp == blocktype)
734 /* Walk over all traceframe block starting at POS offset from
735 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
736 unmodified. If CALLBACK returns true, this returns the position in
737 the traceframe where the block is found, relative to the start of
738 the traceframe (cur_offset). Returns -1 if no callback call
739 returned true, indicating that all blocks have been walked. */
742 traceframe_walk_blocks (walk_blocks_callback_func callback,
745 /* Iterate through a traceframe's blocks, looking for a block of the
748 lseek (trace_fd, cur_offset + pos, SEEK_SET);
749 while (pos < cur_data_size)
754 tfile_read ((gdb_byte *) &block_type, 1);
758 if ((*callback) (block_type, data))
764 lseek (trace_fd, cur_offset + pos + trace_regblock_size, SEEK_SET);
765 pos += trace_regblock_size;
768 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
769 tfile_read ((gdb_byte *) &mlen, 2);
770 mlen = (unsigned short)
771 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
773 (target_gdbarch ()));
774 lseek (trace_fd, mlen, SEEK_CUR);
775 pos += (8 + 2 + mlen);
778 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
782 error (_("Unknown block type '%c' (0x%x) in trace frame"),
783 block_type, block_type);
791 /* Convenience wrapper around traceframe_walk_blocks. Looks for the
792 position offset of a block of type TYPE_WANTED in the current trace
793 frame, starting at POS. Returns -1 if no such block was found. */
796 traceframe_find_block_type (char type_wanted, int pos)
798 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
801 /* Look for a block of saved registers in the traceframe, and get the
802 requested register from it. */
805 tfile_fetch_registers (struct target_ops *ops,
806 struct regcache *regcache, int regno)
808 struct gdbarch *gdbarch = get_regcache_arch (regcache);
809 int offset, regn, regsize, pc_regno;
812 /* An uninitialized reg size says we're not going to be
813 successful at getting register blocks. */
814 if (!trace_regblock_size)
817 regs = alloca (trace_regblock_size);
819 if (traceframe_find_block_type ('R', 0) >= 0)
821 tfile_read (regs, trace_regblock_size);
823 /* Assume the block is laid out in GDB register number order,
824 each register with the size that it has in GDB. */
826 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
828 regsize = register_size (gdbarch, regn);
829 /* Make sure we stay within block bounds. */
830 if (offset + regsize >= trace_regblock_size)
832 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
836 regcache_raw_supply (regcache, regno, regs + offset);
839 else if (regno == -1)
841 regcache_raw_supply (regcache, regn, regs + offset);
849 /* We get here if no register data has been found. Mark registers
851 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
852 regcache_raw_supply (regcache, regn, NULL);
854 /* We can often usefully guess that the PC is going to be the same
855 as the address of the tracepoint. */
856 pc_regno = gdbarch_pc_regnum (gdbarch);
858 /* XXX This guessing code below only works if the PC register isn't
859 a pseudo-register. The value of a pseudo-register isn't stored
860 in the (non-readonly) regcache -- instead it's recomputed
861 (probably from some other cached raw register) whenever the
862 register is read. This guesswork should probably move to some
864 if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
867 if (regno == -1 || regno == pc_regno)
869 struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
871 if (tp && tp->base.loc)
873 /* But don't try to guess if tracepoint is multi-location... */
874 if (tp->base.loc->next)
876 warning (_("Tracepoint %d has multiple "
877 "locations, cannot infer $pc"),
881 /* ... or does while-stepping. */
882 if (tp->step_count > 0)
884 warning (_("Tracepoint %d does while-stepping, "
890 store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
891 gdbarch_byte_order (gdbarch),
892 tp->base.loc->address);
893 regcache_raw_supply (regcache, pc_regno, regs);
898 static enum target_xfer_status
899 tfile_xfer_partial (struct target_ops *ops, enum target_object object,
900 const char *annex, gdb_byte *readbuf,
901 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
902 ULONGEST *xfered_len)
904 /* We're only doing regular memory for now. */
905 if (object != TARGET_OBJECT_MEMORY)
906 return TARGET_XFER_E_IO;
909 error (_("tfile_xfer_partial: trace file is read-only"));
911 if (get_traceframe_number () != -1)
915 /* Iterate through the traceframe's blocks, looking for
917 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
921 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
923 tfile_read ((gdb_byte *) &maddr, 8);
924 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
926 tfile_read ((gdb_byte *) &mlen, 2);
927 mlen = (unsigned short)
928 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
930 /* If the block includes the first part of the desired
931 range, return as much it has; GDB will re-request the
932 remainder, which might be in a different block of this
934 if (maddr <= offset && offset < (maddr + mlen))
936 amt = (maddr + mlen) - offset;
941 lseek (trace_fd, offset - maddr, SEEK_CUR);
942 tfile_read (readbuf, amt);
944 return TARGET_XFER_OK;
947 /* Skip over this block. */
948 pos += (8 + 2 + mlen);
952 /* It's unduly pedantic to refuse to look at the executable for
953 read-only pieces; so do the equivalent of readonly regions aka
955 /* FIXME account for relocation at some point. */
962 for (s = exec_bfd->sections; s; s = s->next)
964 if ((s->flags & SEC_LOAD) == 0
965 || (s->flags & SEC_READONLY) == 0)
969 size = bfd_get_section_size (s);
970 if (vma <= offset && offset < (vma + size))
974 amt = (vma + size) - offset;
978 *xfered_len = bfd_get_section_contents (exec_bfd, s,
979 readbuf, offset - vma, amt);
980 return TARGET_XFER_OK;
985 /* Indicate failure to find the requested memory block. */
986 return TARGET_XFER_E_IO;
989 /* Iterate through the blocks of a trace frame, looking for a 'V'
990 block with a matching tsv number. */
993 tfile_get_trace_state_variable_value (struct target_ops *self,
994 int tsvnum, LONGEST *val)
999 /* Iterate over blocks in current frame and find the last 'V'
1000 block in which tsv number is TSVNUM. In one trace frame, there
1001 may be multiple 'V' blocks created for a given trace variable,
1002 and the last matched 'V' block contains the updated value. */
1004 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
1008 tfile_read ((gdb_byte *) &vnum, 4);
1009 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
1011 (target_gdbarch ()));
1014 tfile_read ((gdb_byte *) val, 8);
1015 *val = extract_signed_integer ((gdb_byte *) val, 8,
1017 (target_gdbarch ()));
1027 tfile_has_all_memory (struct target_ops *ops)
1033 tfile_has_memory (struct target_ops *ops)
1039 tfile_has_stack (struct target_ops *ops)
1041 return get_traceframe_number () != -1;
1045 tfile_has_registers (struct target_ops *ops)
1047 return get_traceframe_number () != -1;
1051 tfile_thread_alive (struct target_ops *ops, ptid_t ptid)
1056 /* Callback for traceframe_walk_blocks. Builds a traceframe_info
1057 object for the tfile target's current traceframe. */
1060 build_traceframe_info (char blocktype, void *data)
1062 struct traceframe_info *info = data;
1068 struct mem_range *r;
1070 unsigned short mlen;
1072 tfile_read ((gdb_byte *) &maddr, 8);
1073 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
1075 (target_gdbarch ()));
1076 tfile_read ((gdb_byte *) &mlen, 2);
1077 mlen = (unsigned short)
1078 extract_unsigned_integer ((gdb_byte *) &mlen,
1079 2, gdbarch_byte_order
1080 (target_gdbarch ()));
1082 r = VEC_safe_push (mem_range_s, info->memory, NULL);
1092 tfile_read ((gdb_byte *) &vnum, 4);
1093 VEC_safe_push (int, info->tvars, vnum);
1101 warning (_("Unhandled trace block type (%d) '%c ' "
1102 "while building trace frame info."),
1103 blocktype, blocktype);
1110 static struct traceframe_info *
1111 tfile_traceframe_info (struct target_ops *self)
1113 struct traceframe_info *info = XCNEW (struct traceframe_info);
1115 traceframe_walk_blocks (build_traceframe_info, 0, info);
1120 init_tfile_ops (void)
1122 tfile_ops.to_shortname = "tfile";
1123 tfile_ops.to_longname = "Local trace dump file";
1125 = "Use a trace file as a target. Specify the filename of the trace file.";
1126 tfile_ops.to_open = tfile_open;
1127 tfile_ops.to_close = tfile_close;
1128 tfile_ops.to_fetch_registers = tfile_fetch_registers;
1129 tfile_ops.to_xfer_partial = tfile_xfer_partial;
1130 tfile_ops.to_files_info = tfile_files_info;
1131 tfile_ops.to_get_trace_status = tfile_get_trace_status;
1132 tfile_ops.to_get_tracepoint_status = tfile_get_tracepoint_status;
1133 tfile_ops.to_trace_find = tfile_trace_find;
1134 tfile_ops.to_get_trace_state_variable_value
1135 = tfile_get_trace_state_variable_value;
1136 tfile_ops.to_stratum = process_stratum;
1137 tfile_ops.to_has_all_memory = tfile_has_all_memory;
1138 tfile_ops.to_has_memory = tfile_has_memory;
1139 tfile_ops.to_has_stack = tfile_has_stack;
1140 tfile_ops.to_has_registers = tfile_has_registers;
1141 tfile_ops.to_traceframe_info = tfile_traceframe_info;
1142 tfile_ops.to_thread_alive = tfile_thread_alive;
1143 tfile_ops.to_magic = OPS_MAGIC;
1146 extern initialize_file_ftype _initialize_tracefile_tfile;
1149 _initialize_tracefile_tfile (void)
1153 add_target_with_completer (&tfile_ops, filename_completer);