1 /* Trace file TFILE format support in GDB.
3 Copyright (C) 1997-2020 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 "gdbsupport/filestuff.h"
24 #include "gdbsupport/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 "gdbsupport/buffer.h"
35 #include "gdbsupport/pathstuff.h"
42 /* The tfile target. */
44 static const target_info tfile_target_info
= {
46 N_("Local trace dump file"),
47 N_("Use a trace file as a target.\n\
48 Specify the filename of the trace file.")
51 class tfile_target final
: public tracefile_target
54 const target_info
&info () const override
55 { return tfile_target_info
; }
57 void close () override
;
58 void fetch_registers (struct regcache
*, int) override
;
59 enum target_xfer_status
xfer_partial (enum target_object object
,
62 const gdb_byte
*writebuf
,
63 ULONGEST offset
, ULONGEST len
,
64 ULONGEST
*xfered_len
) override
;
65 void files_info () override
;
66 int trace_find (enum trace_find_type type
, int num
,
67 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
68 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
69 traceframe_info_up
traceframe_info () override
;
71 void get_tracepoint_status (struct breakpoint
*tp
,
72 struct uploaded_tp
*utp
) override
;
75 /* TFILE trace writer. */
77 struct tfile_trace_file_writer
79 struct trace_file_writer base
;
81 /* File pointer to tfile trace file. */
83 /* Path name of the tfile trace file. */
87 /* This is the implementation of trace_file_write_ops method
88 target_save. We just call the generic target
89 target_save_trace_data to do target-side saving. */
92 tfile_target_save (struct trace_file_writer
*self
,
95 int err
= target_save_trace_data (filename
);
100 /* This is the implementation of trace_file_write_ops method
104 tfile_dtor (struct trace_file_writer
*self
)
106 struct tfile_trace_file_writer
*writer
107 = (struct tfile_trace_file_writer
*) self
;
109 xfree (writer
->pathname
);
111 if (writer
->fp
!= NULL
)
115 /* This is the implementation of trace_file_write_ops method
116 start. It creates the trace file FILENAME and registers some
120 tfile_start (struct trace_file_writer
*self
, const char *filename
)
122 struct tfile_trace_file_writer
*writer
123 = (struct tfile_trace_file_writer
*) self
;
125 writer
->pathname
= tilde_expand (filename
);
126 writer
->fp
= gdb_fopen_cloexec (writer
->pathname
, "wb").release ();
127 if (writer
->fp
== NULL
)
128 error (_("Unable to open file '%s' for saving trace data (%s)"),
129 writer
->pathname
, safe_strerror (errno
));
132 /* This is the implementation of trace_file_write_ops method
133 write_header. Write the TFILE header. */
136 tfile_write_header (struct trace_file_writer
*self
)
138 struct tfile_trace_file_writer
*writer
139 = (struct tfile_trace_file_writer
*) self
;
142 /* Write a file header, with a high-bit-set char to indicate a
143 binary file, plus a hint as what this file is, and a version
144 number in case of future needs. */
145 written
= fwrite ("\x7fTRACE0\n", 8, 1, writer
->fp
);
147 perror_with_name (writer
->pathname
);
150 /* This is the implementation of trace_file_write_ops method
151 write_regblock_type. Write the size of register block. */
154 tfile_write_regblock_type (struct trace_file_writer
*self
, int size
)
156 struct tfile_trace_file_writer
*writer
157 = (struct tfile_trace_file_writer
*) self
;
159 fprintf (writer
->fp
, "R %x\n", size
);
162 /* This is the implementation of trace_file_write_ops method
166 tfile_write_status (struct trace_file_writer
*self
,
167 struct trace_status
*ts
)
169 struct tfile_trace_file_writer
*writer
170 = (struct tfile_trace_file_writer
*) self
;
172 fprintf (writer
->fp
, "status %c;%s",
173 (ts
->running
? '1' : '0'), stop_reason_names
[ts
->stop_reason
]);
174 if (ts
->stop_reason
== tracepoint_error
175 || ts
->stop_reason
== trace_stop_command
)
177 char *buf
= (char *) alloca (strlen (ts
->stop_desc
) * 2 + 1);
179 bin2hex ((gdb_byte
*) ts
->stop_desc
, buf
, strlen (ts
->stop_desc
));
180 fprintf (writer
->fp
, ":%s", buf
);
182 fprintf (writer
->fp
, ":%x", ts
->stopping_tracepoint
);
183 if (ts
->traceframe_count
>= 0)
184 fprintf (writer
->fp
, ";tframes:%x", ts
->traceframe_count
);
185 if (ts
->traceframes_created
>= 0)
186 fprintf (writer
->fp
, ";tcreated:%x", ts
->traceframes_created
);
187 if (ts
->buffer_free
>= 0)
188 fprintf (writer
->fp
, ";tfree:%x", ts
->buffer_free
);
189 if (ts
->buffer_size
>= 0)
190 fprintf (writer
->fp
, ";tsize:%x", ts
->buffer_size
);
191 if (ts
->disconnected_tracing
)
192 fprintf (writer
->fp
, ";disconn:%x", ts
->disconnected_tracing
);
193 if (ts
->circular_buffer
)
194 fprintf (writer
->fp
, ";circular:%x", ts
->circular_buffer
);
197 fprintf (writer
->fp
, ";starttime:%s",
198 phex_nz (ts
->start_time
, sizeof (ts
->start_time
)));
202 fprintf (writer
->fp
, ";stoptime:%s",
203 phex_nz (ts
->stop_time
, sizeof (ts
->stop_time
)));
205 if (ts
->notes
!= NULL
)
207 char *buf
= (char *) alloca (strlen (ts
->notes
) * 2 + 1);
209 bin2hex ((gdb_byte
*) ts
->notes
, buf
, strlen (ts
->notes
));
210 fprintf (writer
->fp
, ";notes:%s", buf
);
212 if (ts
->user_name
!= NULL
)
214 char *buf
= (char *) alloca (strlen (ts
->user_name
) * 2 + 1);
216 bin2hex ((gdb_byte
*) ts
->user_name
, buf
, strlen (ts
->user_name
));
217 fprintf (writer
->fp
, ";username:%s", buf
);
219 fprintf (writer
->fp
, "\n");
222 /* This is the implementation of trace_file_write_ops method
223 write_uploaded_tsv. */
226 tfile_write_uploaded_tsv (struct trace_file_writer
*self
,
227 struct uploaded_tsv
*utsv
)
230 struct tfile_trace_file_writer
*writer
231 = (struct tfile_trace_file_writer
*) self
;
235 buf
= (char *) xmalloc (strlen (utsv
->name
) * 2 + 1);
236 bin2hex ((gdb_byte
*) (utsv
->name
), buf
, strlen (utsv
->name
));
239 fprintf (writer
->fp
, "tsv %x:%s:%x:%s\n",
240 utsv
->number
, phex_nz (utsv
->initial_value
, 8),
241 utsv
->builtin
, buf
!= NULL
? buf
: "");
247 #define MAX_TRACE_UPLOAD 2000
249 /* This is the implementation of trace_file_write_ops method
250 write_uploaded_tp. */
253 tfile_write_uploaded_tp (struct trace_file_writer
*self
,
254 struct uploaded_tp
*utp
)
256 struct tfile_trace_file_writer
*writer
257 = (struct tfile_trace_file_writer
*) self
;
258 char buf
[MAX_TRACE_UPLOAD
];
260 fprintf (writer
->fp
, "tp T%x:%s:%c:%x:%x",
261 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)),
262 (utp
->enabled
? 'E' : 'D'), utp
->step
, utp
->pass
);
263 if (utp
->type
== bp_fast_tracepoint
)
264 fprintf (writer
->fp
, ":F%x", utp
->orig_size
);
267 ":X%x,%s", (unsigned int) strlen (utp
->cond
.get ()) / 2,
269 fprintf (writer
->fp
, "\n");
270 for (const auto &act
: utp
->actions
)
271 fprintf (writer
->fp
, "tp A%x:%s:%s\n",
272 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)), act
.get ());
273 for (const auto &act
: utp
->step_actions
)
274 fprintf (writer
->fp
, "tp S%x:%s:%s\n",
275 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)), act
.get ());
278 encode_source_string (utp
->number
, utp
->addr
,
279 "at", utp
->at_string
.get (),
280 buf
, MAX_TRACE_UPLOAD
);
281 fprintf (writer
->fp
, "tp Z%s\n", buf
);
283 if (utp
->cond_string
)
285 encode_source_string (utp
->number
, utp
->addr
,
286 "cond", utp
->cond_string
.get (),
287 buf
, MAX_TRACE_UPLOAD
);
288 fprintf (writer
->fp
, "tp Z%s\n", buf
);
290 for (const auto &act
: utp
->cmd_strings
)
292 encode_source_string (utp
->number
, utp
->addr
, "cmd", act
.get (),
293 buf
, MAX_TRACE_UPLOAD
);
294 fprintf (writer
->fp
, "tp Z%s\n", buf
);
296 fprintf (writer
->fp
, "tp V%x:%s:%x:%s\n",
297 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)),
299 phex_nz (utp
->traceframe_usage
,
300 sizeof (utp
->traceframe_usage
)));
303 /* This is the implementation of trace_file_write_ops method
307 tfile_write_tdesc (struct trace_file_writer
*self
)
309 struct tfile_trace_file_writer
*writer
310 = (struct tfile_trace_file_writer
*) self
;
312 gdb::optional
<std::string
> tdesc
313 = target_fetch_description_xml (current_top_target ());
318 const char *ptr
= tdesc
->c_str ();
320 /* Write tdesc line by line, prefixing each line with "tdesc ". */
323 const char *next
= strchr (ptr
, '\n');
326 fprintf (writer
->fp
, "tdesc %.*s\n", (int) (next
- ptr
), ptr
);
330 else if (*ptr
!= '\0')
332 /* Last line, doesn't have a newline. */
333 fprintf (writer
->fp
, "tdesc %s\n", ptr
);
339 /* This is the implementation of trace_file_write_ops method
340 write_definition_end. */
343 tfile_write_definition_end (struct trace_file_writer
*self
)
345 struct tfile_trace_file_writer
*writer
346 = (struct tfile_trace_file_writer
*) self
;
348 fprintf (writer
->fp
, "\n");
351 /* This is the implementation of trace_file_write_ops method
355 tfile_write_raw_data (struct trace_file_writer
*self
, gdb_byte
*buf
,
358 struct tfile_trace_file_writer
*writer
359 = (struct tfile_trace_file_writer
*) self
;
361 if (fwrite (buf
, len
, 1, writer
->fp
) < 1)
362 perror_with_name (writer
->pathname
);
365 /* This is the implementation of trace_file_write_ops method
369 tfile_end (struct trace_file_writer
*self
)
371 struct tfile_trace_file_writer
*writer
372 = (struct tfile_trace_file_writer
*) self
;
375 /* Mark the end of trace data. */
376 if (fwrite (&gotten
, 4, 1, writer
->fp
) < 1)
377 perror_with_name (writer
->pathname
);
380 /* Operations to write trace buffers into TFILE format. */
382 static const struct trace_file_write_ops tfile_write_ops
=
388 tfile_write_regblock_type
,
390 tfile_write_uploaded_tsv
,
391 tfile_write_uploaded_tp
,
393 tfile_write_definition_end
,
394 tfile_write_raw_data
,
399 /* Return a trace writer for TFILE format. */
401 struct trace_file_writer
*
402 tfile_trace_file_writer_new (void)
404 struct tfile_trace_file_writer
*writer
405 = XNEW (struct tfile_trace_file_writer
);
407 writer
->base
.ops
= &tfile_write_ops
;
409 writer
->pathname
= NULL
;
411 return (struct trace_file_writer
*) writer
;
414 /* target tfile command */
416 static tfile_target tfile_ops
;
418 #define TRACE_HEADER_SIZE 8
420 #define TFILE_PID (1)
422 static char *trace_filename
;
423 static int trace_fd
= -1;
424 static off_t trace_frames_offset
;
425 static off_t cur_offset
;
426 static int cur_data_size
;
427 int trace_regblock_size
;
428 static struct buffer trace_tdesc
;
430 static void tfile_append_tdesc_line (const char *line
);
431 static void tfile_interp_line (char *line
,
432 struct uploaded_tp
**utpp
,
433 struct uploaded_tsv
**utsvp
);
435 /* Read SIZE bytes into READBUF from the trace frame, starting at
436 TRACE_FD's current position. Note that this call `read'
437 underneath, hence it advances the file's seek position. Throws an
438 error if the `read' syscall fails, or less than SIZE bytes are
442 tfile_read (gdb_byte
*readbuf
, int size
)
446 gotten
= read (trace_fd
, readbuf
, size
);
448 perror_with_name (trace_filename
);
449 else if (gotten
< size
)
450 error (_("Premature end of file while reading trace file"));
453 /* Open the tfile target. */
456 tfile_target_open (const char *arg
, int from_tty
)
460 char header
[TRACE_HEADER_SIZE
];
461 char linebuf
[1000]; /* Should be max remote packet size or so. */
464 struct trace_status
*ts
;
465 struct uploaded_tp
*uploaded_tps
= NULL
;
466 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
468 target_preopen (from_tty
);
470 error (_("No trace file specified."));
472 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (arg
));
473 if (!IS_ABSOLUTE_PATH (filename
.get ()))
474 filename
= gdb_abspath (filename
.get ());
476 flags
= O_BINARY
| O_LARGEFILE
;
478 scratch_chan
= gdb_open_cloexec (filename
.get (), flags
, 0);
479 if (scratch_chan
< 0)
480 perror_with_name (filename
.get ());
482 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
484 unpush_target (&tfile_ops
);
486 trace_filename
= filename
.release ();
487 trace_fd
= scratch_chan
;
489 /* Make sure this is clear. */
490 buffer_free (&trace_tdesc
);
493 /* Read the file header and test for validity. */
494 tfile_read ((gdb_byte
*) &header
, TRACE_HEADER_SIZE
);
496 bytes
+= TRACE_HEADER_SIZE
;
497 if (!(header
[0] == 0x7f
498 && (startswith (header
+ 1, "TRACE0\n"))))
499 error (_("File is not a valid trace file."));
501 push_target (&tfile_ops
);
503 trace_regblock_size
= 0;
504 ts
= current_trace_status ();
505 /* We know we're working with a file. Record its name. */
506 ts
->filename
= trace_filename
;
507 /* Set defaults in case there is no status line. */
508 ts
->running_known
= 0;
509 ts
->stop_reason
= trace_stop_reason_unknown
;
510 ts
->traceframe_count
= -1;
512 ts
->disconnected_tracing
= 0;
513 ts
->circular_buffer
= 0;
517 /* Read through a section of newline-terminated lines that
518 define things like tracepoints. */
522 tfile_read (&byte
, 1);
527 /* Empty line marks end of the definition section. */
532 tfile_interp_line (linebuf
, &uploaded_tps
, &uploaded_tsvs
);
537 error (_("Excessively long lines in trace file"));
540 /* By now, tdesc lines have been read from tfile - let's parse them. */
541 target_find_description ();
543 /* Record the starting offset of the binary trace data. */
544 trace_frames_offset
= bytes
;
546 /* If we don't have a blocksize, we can't interpret the
548 if (trace_regblock_size
== 0)
549 error (_("No register block size recorded in trace file"));
551 catch (const gdb_exception
&ex
)
553 /* Remove the partially set up target. */
554 unpush_target (&tfile_ops
);
558 inferior_appeared (current_inferior (), TFILE_PID
);
559 inferior_ptid
= ptid_t (TFILE_PID
);
560 add_thread_silent (&tfile_ops
, 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 ()
619 gdb_assert (trace_fd
!= -1);
621 inferior_ptid
= null_ptid
; /* Avoid confusion from thread stuff. */
622 exit_inferior_silent (current_inferior ());
626 xfree (trace_filename
);
627 trace_filename
= NULL
;
628 buffer_free (&trace_tdesc
);
630 trace_reset_local_state ();
634 tfile_target::files_info ()
636 printf_filtered ("\t`%s'\n", trace_filename
);
640 tfile_target::get_tracepoint_status (struct breakpoint
*tp
, struct uploaded_tp
*utp
)
642 /* Other bits of trace status were collected as part of opening the
643 trace files, so nothing to do here. */
646 /* Given the position of a traceframe in the file, figure out what
647 address the frame was collected at. This would normally be the
648 value of a collected PC register, but if not available, we
652 tfile_get_traceframe_address (off_t tframe_offset
)
656 struct tracepoint
*tp
;
657 off_t saved_offset
= cur_offset
;
659 /* FIXME dig pc out of collected registers. */
661 /* Fall back to using tracepoint address. */
662 lseek (trace_fd
, tframe_offset
, SEEK_SET
);
663 tfile_read ((gdb_byte
*) &tpnum
, 2);
664 tpnum
= (short) extract_signed_integer ((gdb_byte
*) &tpnum
, 2,
666 (target_gdbarch ()));
668 tp
= get_tracepoint_by_number_on_target (tpnum
);
669 /* FIXME this is a poor heuristic if multiple locations. */
671 addr
= tp
->loc
->address
;
673 /* Restore our seek position. */
674 cur_offset
= saved_offset
;
675 lseek (trace_fd
, cur_offset
, SEEK_SET
);
679 /* Given a type of search and some parameters, scan the collection of
680 traceframes in the file looking for a match. When found, return
681 both the traceframe and tracepoint number, otherwise -1 for
685 tfile_target::trace_find (enum trace_find_type type
, int num
,
686 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
689 int tfnum
= 0, found
= 0;
690 unsigned int data_size
;
691 struct tracepoint
*tp
;
692 off_t offset
, tframe_offset
;
702 lseek (trace_fd
, trace_frames_offset
, SEEK_SET
);
703 offset
= trace_frames_offset
;
706 tframe_offset
= offset
;
707 tfile_read ((gdb_byte
*) &tpnum
, 2);
708 tpnum
= (short) extract_signed_integer ((gdb_byte
*) &tpnum
, 2,
710 (target_gdbarch ()));
714 tfile_read ((gdb_byte
*) &data_size
, 4);
715 data_size
= (unsigned int) extract_unsigned_integer
716 ((gdb_byte
*) &data_size
, 4,
717 gdbarch_byte_order (target_gdbarch ()));
720 if (type
== tfind_number
)
722 /* Looking for a specific trace frame. */
728 /* Start from the _next_ trace frame. */
729 if (tfnum
> get_traceframe_number ())
734 tfaddr
= tfile_get_traceframe_address (tframe_offset
);
739 tp
= get_tracepoint (num
);
740 if (tp
&& tpnum
== tp
->number_on_target
)
744 tfaddr
= tfile_get_traceframe_address (tframe_offset
);
745 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
749 tfaddr
= tfile_get_traceframe_address (tframe_offset
);
750 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
754 internal_error (__FILE__
, __LINE__
, _("unknown tfind type"));
764 cur_data_size
= data_size
;
768 /* Skip past the traceframe's data. */
769 lseek (trace_fd
, data_size
, SEEK_CUR
);
771 /* Update our own count of traceframes. */
774 /* Did not find what we were looking for. */
780 /* Prototype of the callback passed to tframe_walk_blocks. */
781 typedef int (*walk_blocks_callback_func
) (char blocktype
, void *data
);
783 /* Callback for traceframe_walk_blocks, used to find a given block
784 type in a traceframe. */
787 match_blocktype (char blocktype
, void *data
)
789 char *wantedp
= (char *) data
;
791 if (*wantedp
== blocktype
)
797 /* Walk over all traceframe block starting at POS offset from
798 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
799 unmodified. If CALLBACK returns true, this returns the position in
800 the traceframe where the block is found, relative to the start of
801 the traceframe (cur_offset). Returns -1 if no callback call
802 returned true, indicating that all blocks have been walked. */
805 traceframe_walk_blocks (walk_blocks_callback_func callback
,
808 /* Iterate through a traceframe's blocks, looking for a block of the
811 lseek (trace_fd
, cur_offset
+ pos
, SEEK_SET
);
812 while (pos
< cur_data_size
)
817 tfile_read ((gdb_byte
*) &block_type
, 1);
821 if ((*callback
) (block_type
, data
))
827 lseek (trace_fd
, cur_offset
+ pos
+ trace_regblock_size
, SEEK_SET
);
828 pos
+= trace_regblock_size
;
831 lseek (trace_fd
, cur_offset
+ pos
+ 8, SEEK_SET
);
832 tfile_read ((gdb_byte
*) &mlen
, 2);
833 mlen
= (unsigned short)
834 extract_unsigned_integer ((gdb_byte
*) &mlen
, 2,
836 (target_gdbarch ()));
837 lseek (trace_fd
, mlen
, SEEK_CUR
);
838 pos
+= (8 + 2 + mlen
);
841 lseek (trace_fd
, cur_offset
+ pos
+ 4 + 8, SEEK_SET
);
845 error (_("Unknown block type '%c' (0x%x) in trace frame"),
846 block_type
, block_type
);
854 /* Convenience wrapper around traceframe_walk_blocks. Looks for the
855 position offset of a block of type TYPE_WANTED in the current trace
856 frame, starting at POS. Returns -1 if no such block was found. */
859 traceframe_find_block_type (char type_wanted
, int pos
)
861 return traceframe_walk_blocks (match_blocktype
, pos
, &type_wanted
);
864 /* Look for a block of saved registers in the traceframe, and get the
865 requested register from it. */
868 tfile_target::fetch_registers (struct regcache
*regcache
, int regno
)
870 struct gdbarch
*gdbarch
= regcache
->arch ();
871 int offset
, regn
, regsize
, dummy
;
873 /* An uninitialized reg size says we're not going to be
874 successful at getting register blocks. */
875 if (!trace_regblock_size
)
878 if (traceframe_find_block_type ('R', 0) >= 0)
880 gdb_byte
*regs
= (gdb_byte
*) alloca (trace_regblock_size
);
882 tfile_read (regs
, trace_regblock_size
);
884 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
886 if (!remote_register_number_and_offset (regcache
->arch (),
887 regn
, &dummy
, &offset
))
890 regsize
= register_size (gdbarch
, regn
);
891 /* Make sure we stay within block bounds. */
892 if (offset
+ regsize
> trace_regblock_size
)
894 if (regcache
->get_register_status (regn
) == REG_UNKNOWN
)
898 regcache
->raw_supply (regno
, regs
+ offset
);
901 else if (regno
== -1)
903 regcache
->raw_supply (regn
, regs
+ offset
);
909 tracefile_fetch_registers (regcache
, regno
);
912 static enum target_xfer_status
913 tfile_xfer_partial_features (const char *annex
,
914 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
915 ULONGEST offset
, ULONGEST len
,
916 ULONGEST
*xfered_len
)
918 if (strcmp (annex
, "target.xml"))
919 return TARGET_XFER_E_IO
;
922 error (_("tfile_xfer_partial: tdesc is read-only"));
924 if (trace_tdesc
.used_size
== 0)
925 return TARGET_XFER_E_IO
;
927 if (offset
>= trace_tdesc
.used_size
)
928 return TARGET_XFER_EOF
;
930 if (len
> trace_tdesc
.used_size
- offset
)
931 len
= trace_tdesc
.used_size
- offset
;
933 memcpy (readbuf
, trace_tdesc
.buffer
+ offset
, len
);
936 return TARGET_XFER_OK
;
939 enum target_xfer_status
940 tfile_target::xfer_partial (enum target_object object
,
941 const char *annex
, gdb_byte
*readbuf
,
942 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
943 ULONGEST
*xfered_len
)
945 /* We're only doing regular memory and tdesc for now. */
946 if (object
== TARGET_OBJECT_AVAILABLE_FEATURES
)
947 return tfile_xfer_partial_features (annex
, readbuf
, writebuf
,
948 offset
, len
, xfered_len
);
949 if (object
!= TARGET_OBJECT_MEMORY
)
950 return TARGET_XFER_E_IO
;
953 error (_("tfile_xfer_partial: trace file is read-only"));
955 if (get_traceframe_number () != -1)
958 enum target_xfer_status res
;
959 /* Records the lowest available address of all blocks that
960 intersects the requested range. */
961 ULONGEST low_addr_available
= 0;
963 /* Iterate through the traceframe's blocks, looking for
965 while ((pos
= traceframe_find_block_type ('M', pos
)) >= 0)
969 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
971 tfile_read ((gdb_byte
*) &maddr
, 8);
972 maddr
= extract_unsigned_integer ((gdb_byte
*) &maddr
, 8,
974 tfile_read ((gdb_byte
*) &mlen
, 2);
975 mlen
= (unsigned short)
976 extract_unsigned_integer ((gdb_byte
*) &mlen
, 2, byte_order
);
978 /* If the block includes the first part of the desired
979 range, return as much it has; GDB will re-request the
980 remainder, which might be in a different block of this
982 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
984 amt
= (maddr
+ mlen
) - offset
;
989 lseek (trace_fd
, offset
- maddr
, SEEK_CUR
);
990 tfile_read (readbuf
, amt
);
992 return TARGET_XFER_OK
;
995 if (offset
< maddr
&& maddr
< (offset
+ len
))
996 if (low_addr_available
== 0 || low_addr_available
> maddr
)
997 low_addr_available
= maddr
;
999 /* Skip over this block. */
1000 pos
+= (8 + 2 + mlen
);
1003 /* Requested memory is unavailable in the context of traceframes,
1004 and this address falls within a read-only section, fallback
1005 to reading from executable, up to LOW_ADDR_AVAILABLE. */
1006 if (offset
< low_addr_available
)
1007 len
= std::min (len
, low_addr_available
- offset
);
1008 res
= exec_read_partial_read_only (readbuf
, offset
, len
, xfered_len
);
1010 if (res
== TARGET_XFER_OK
)
1011 return TARGET_XFER_OK
;
1014 /* No use trying further, we know some memory starting
1015 at MEMADDR isn't available. */
1017 return TARGET_XFER_UNAVAILABLE
;
1022 /* Fallback to reading from read-only sections. */
1023 return section_table_read_available_memory (readbuf
, offset
, len
,
1028 /* Iterate through the blocks of a trace frame, looking for a 'V'
1029 block with a matching tsv number. */
1032 tfile_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
1037 /* Iterate over blocks in current frame and find the last 'V'
1038 block in which tsv number is TSVNUM. In one trace frame, there
1039 may be multiple 'V' blocks created for a given trace variable,
1040 and the last matched 'V' block contains the updated value. */
1042 while ((pos
= traceframe_find_block_type ('V', pos
)) >= 0)
1046 tfile_read ((gdb_byte
*) &vnum
, 4);
1047 vnum
= (int) extract_signed_integer ((gdb_byte
*) &vnum
, 4,
1049 (target_gdbarch ()));
1052 tfile_read ((gdb_byte
*) val
, 8);
1053 *val
= extract_signed_integer ((gdb_byte
*) val
, 8,
1055 (target_gdbarch ()));
1064 /* Callback for traceframe_walk_blocks. Builds a traceframe_info
1065 object for the tfile target's current traceframe. */
1068 build_traceframe_info (char blocktype
, void *data
)
1070 struct traceframe_info
*info
= (struct traceframe_info
*) data
;
1077 unsigned short mlen
;
1079 tfile_read ((gdb_byte
*) &maddr
, 8);
1080 maddr
= extract_unsigned_integer ((gdb_byte
*) &maddr
, 8,
1082 (target_gdbarch ()));
1083 tfile_read ((gdb_byte
*) &mlen
, 2);
1084 mlen
= (unsigned short)
1085 extract_unsigned_integer ((gdb_byte
*) &mlen
,
1086 2, gdbarch_byte_order
1087 (target_gdbarch ()));
1089 info
->memory
.emplace_back (maddr
, mlen
);
1096 tfile_read ((gdb_byte
*) &vnum
, 4);
1097 info
->tvars
.push_back (vnum
);
1105 warning (_("Unhandled trace block type (%d) '%c ' "
1106 "while building trace frame info."),
1107 blocktype
, blocktype
);
1115 tfile_target::traceframe_info ()
1117 traceframe_info_up
info (new struct traceframe_info
);
1119 traceframe_walk_blocks (build_traceframe_info
, 0, info
.get ());
1124 /* Handles tdesc lines from tfile by appending the payload to
1125 a global trace_tdesc variable. */
1128 tfile_append_tdesc_line (const char *line
)
1130 buffer_grow_str (&trace_tdesc
, line
);
1131 buffer_grow_str (&trace_tdesc
, "\n");
1134 void _initialize_tracefile_tfile ();
1136 _initialize_tracefile_tfile ()
1138 add_target (tfile_target_info
, tfile_target_open
, filename_completer
);