1 /* Trace file TFILE format support in GDB.
3 Copyright (C) 1997-2024 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/>. */
20 #include "extract-store-integer.h"
21 #include "tracefile.h"
22 #include "readline/tilde.h"
23 #include "gdbsupport/filestuff.h"
24 #include "gdbsupport/rsp-low.h"
27 #include "gdbthread.h"
29 #include "completer.h"
30 #include "filenames.h"
32 #include "xml-tdesc.h"
33 #include "target-descriptions.h"
34 #include "gdbsupport/pathstuff.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.\n\
47 Specify the filename of the trace file.")
50 class tfile_target final
: public tracefile_target
53 const target_info
&info () const override
54 { return tfile_target_info
; }
56 void close () override
;
57 void fetch_registers (struct regcache
*, int) override
;
58 enum target_xfer_status
xfer_partial (enum target_object object
,
61 const gdb_byte
*writebuf
,
62 ULONGEST offset
, ULONGEST len
,
63 ULONGEST
*xfered_len
) override
;
64 void files_info () override
;
65 int trace_find (enum trace_find_type type
, int num
,
66 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
67 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
68 traceframe_info_up
traceframe_info () override
;
70 void get_tracepoint_status (tracepoint
*tp
,
71 struct uploaded_tp
*utp
) override
;
74 /* TFILE trace writer. */
76 struct tfile_trace_file_writer
78 struct trace_file_writer base
;
80 /* File pointer to tfile trace file. */
82 /* Path name of the tfile trace file. */
86 /* This is the implementation of trace_file_write_ops method
87 target_save. We just call the generic target
88 target_save_trace_data to do target-side saving. */
91 tfile_target_save (struct trace_file_writer
*self
,
94 int err
= target_save_trace_data (filename
);
99 /* This is the implementation of trace_file_write_ops method
103 tfile_dtor (struct trace_file_writer
*self
)
105 struct tfile_trace_file_writer
*writer
106 = (struct tfile_trace_file_writer
*) self
;
108 xfree (writer
->pathname
);
110 if (writer
->fp
!= NULL
)
114 /* This is the implementation of trace_file_write_ops method
115 start. It creates the trace file FILENAME and registers some
119 tfile_start (struct trace_file_writer
*self
, const char *filename
)
121 struct tfile_trace_file_writer
*writer
122 = (struct tfile_trace_file_writer
*) self
;
124 writer
->pathname
= tilde_expand (filename
);
125 writer
->fp
= gdb_fopen_cloexec (writer
->pathname
, "wb").release ();
126 if (writer
->fp
== NULL
)
127 error (_("Unable to open file '%s' for saving trace data (%s)"),
128 writer
->pathname
, safe_strerror (errno
));
131 /* This is the implementation of trace_file_write_ops method
132 write_header. Write the TFILE header. */
135 tfile_write_header (struct trace_file_writer
*self
)
137 struct tfile_trace_file_writer
*writer
138 = (struct tfile_trace_file_writer
*) self
;
141 /* Write a file header, with a high-bit-set char to indicate a
142 binary file, plus a hint as what this file is, and a version
143 number in case of future needs. */
144 written
= fwrite ("\x7fTRACE0\n", 8, 1, writer
->fp
);
146 perror_with_name (writer
->pathname
);
149 /* This is the implementation of trace_file_write_ops method
150 write_regblock_type. Write the size of register block. */
153 tfile_write_regblock_type (struct trace_file_writer
*self
, int size
)
155 struct tfile_trace_file_writer
*writer
156 = (struct tfile_trace_file_writer
*) self
;
158 fprintf (writer
->fp
, "R %x\n", size
);
161 /* This is the implementation of trace_file_write_ops method
165 tfile_write_status (struct trace_file_writer
*self
,
166 struct trace_status
*ts
)
168 struct tfile_trace_file_writer
*writer
169 = (struct tfile_trace_file_writer
*) self
;
171 fprintf (writer
->fp
, "status %c;%s",
172 (ts
->running
? '1' : '0'), stop_reason_names
[ts
->stop_reason
]);
173 if (ts
->stop_reason
== tracepoint_error
174 || ts
->stop_reason
== trace_stop_command
)
176 char *buf
= (char *) alloca (strlen (ts
->stop_desc
) * 2 + 1);
178 bin2hex ((gdb_byte
*) ts
->stop_desc
, buf
, strlen (ts
->stop_desc
));
179 fprintf (writer
->fp
, ":%s", buf
);
181 fprintf (writer
->fp
, ":%x", ts
->stopping_tracepoint
);
182 if (ts
->traceframe_count
>= 0)
183 fprintf (writer
->fp
, ";tframes:%x", ts
->traceframe_count
);
184 if (ts
->traceframes_created
>= 0)
185 fprintf (writer
->fp
, ";tcreated:%x", ts
->traceframes_created
);
186 if (ts
->buffer_free
>= 0)
187 fprintf (writer
->fp
, ";tfree:%x", ts
->buffer_free
);
188 if (ts
->buffer_size
>= 0)
189 fprintf (writer
->fp
, ";tsize:%x", ts
->buffer_size
);
190 if (ts
->disconnected_tracing
)
191 fprintf (writer
->fp
, ";disconn:%x", ts
->disconnected_tracing
);
192 if (ts
->circular_buffer
)
193 fprintf (writer
->fp
, ";circular:%x", ts
->circular_buffer
);
196 fprintf (writer
->fp
, ";starttime:%s",
197 phex_nz (ts
->start_time
, sizeof (ts
->start_time
)));
201 fprintf (writer
->fp
, ";stoptime:%s",
202 phex_nz (ts
->stop_time
, sizeof (ts
->stop_time
)));
204 if (ts
->notes
!= NULL
)
206 char *buf
= (char *) alloca (strlen (ts
->notes
) * 2 + 1);
208 bin2hex ((gdb_byte
*) ts
->notes
, buf
, strlen (ts
->notes
));
209 fprintf (writer
->fp
, ";notes:%s", buf
);
211 if (ts
->user_name
!= NULL
)
213 char *buf
= (char *) alloca (strlen (ts
->user_name
) * 2 + 1);
215 bin2hex ((gdb_byte
*) ts
->user_name
, buf
, strlen (ts
->user_name
));
216 fprintf (writer
->fp
, ";username:%s", buf
);
218 fprintf (writer
->fp
, "\n");
221 /* This is the implementation of trace_file_write_ops method
222 write_uploaded_tsv. */
225 tfile_write_uploaded_tsv (struct trace_file_writer
*self
,
226 struct uploaded_tsv
*utsv
)
228 gdb::unique_xmalloc_ptr
<char> buf
;
229 struct tfile_trace_file_writer
*writer
230 = (struct tfile_trace_file_writer
*) self
;
234 buf
.reset ((char *) xmalloc (strlen (utsv
->name
) * 2 + 1));
235 bin2hex ((gdb_byte
*) (utsv
->name
), buf
.get (), strlen (utsv
->name
));
238 fprintf (writer
->fp
, "tsv %x:%s:%x:%s\n",
239 utsv
->number
, phex_nz (utsv
->initial_value
, 8),
240 utsv
->builtin
, buf
!= NULL
? buf
.get () : "");
243 #define MAX_TRACE_UPLOAD 2000
245 /* This is the implementation of trace_file_write_ops method
246 write_uploaded_tp. */
249 tfile_write_uploaded_tp (struct trace_file_writer
*self
,
250 struct uploaded_tp
*utp
)
252 struct tfile_trace_file_writer
*writer
253 = (struct tfile_trace_file_writer
*) self
;
254 char buf
[MAX_TRACE_UPLOAD
];
256 fprintf (writer
->fp
, "tp T%x:%s:%c:%x:%x",
257 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)),
258 (utp
->enabled
? 'E' : 'D'), utp
->step
, utp
->pass
);
259 if (utp
->type
== bp_fast_tracepoint
)
260 fprintf (writer
->fp
, ":F%x", utp
->orig_size
);
263 ":X%x,%s", (unsigned int) strlen (utp
->cond
.get ()) / 2,
265 fprintf (writer
->fp
, "\n");
266 for (const auto &act
: utp
->actions
)
267 fprintf (writer
->fp
, "tp A%x:%s:%s\n",
268 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)), act
.get ());
269 for (const auto &act
: utp
->step_actions
)
270 fprintf (writer
->fp
, "tp S%x:%s:%s\n",
271 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)), act
.get ());
274 encode_source_string (utp
->number
, utp
->addr
,
275 "at", utp
->at_string
.get (),
276 buf
, MAX_TRACE_UPLOAD
);
277 fprintf (writer
->fp
, "tp Z%s\n", buf
);
279 if (utp
->cond_string
)
281 encode_source_string (utp
->number
, utp
->addr
,
282 "cond", utp
->cond_string
.get (),
283 buf
, MAX_TRACE_UPLOAD
);
284 fprintf (writer
->fp
, "tp Z%s\n", buf
);
286 for (const auto &act
: utp
->cmd_strings
)
288 encode_source_string (utp
->number
, utp
->addr
, "cmd", act
.get (),
289 buf
, MAX_TRACE_UPLOAD
);
290 fprintf (writer
->fp
, "tp Z%s\n", buf
);
292 fprintf (writer
->fp
, "tp V%x:%s:%x:%s\n",
293 utp
->number
, phex_nz (utp
->addr
, sizeof (utp
->addr
)),
295 phex_nz (utp
->traceframe_usage
,
296 sizeof (utp
->traceframe_usage
)));
299 /* This is the implementation of trace_file_write_ops method
303 tfile_write_tdesc (struct trace_file_writer
*self
)
305 struct tfile_trace_file_writer
*writer
306 = (struct tfile_trace_file_writer
*) self
;
308 std::optional
<std::string
> tdesc
309 = target_fetch_description_xml (current_inferior ()->top_target ());
314 const char *ptr
= tdesc
->c_str ();
316 /* Write tdesc line by line, prefixing each line with "tdesc ". */
319 const char *next
= strchr (ptr
, '\n');
322 fprintf (writer
->fp
, "tdesc %.*s\n", (int) (next
- ptr
), ptr
);
326 else if (*ptr
!= '\0')
328 /* Last line, doesn't have a newline. */
329 fprintf (writer
->fp
, "tdesc %s\n", ptr
);
335 /* This is the implementation of trace_file_write_ops method
336 write_definition_end. */
339 tfile_write_definition_end (struct trace_file_writer
*self
)
341 struct tfile_trace_file_writer
*writer
342 = (struct tfile_trace_file_writer
*) self
;
344 fprintf (writer
->fp
, "\n");
347 /* This is the implementation of trace_file_write_ops method
351 tfile_write_raw_data (struct trace_file_writer
*self
, gdb_byte
*buf
,
354 struct tfile_trace_file_writer
*writer
355 = (struct tfile_trace_file_writer
*) self
;
357 if (fwrite (buf
, len
, 1, writer
->fp
) < 1)
358 perror_with_name (writer
->pathname
);
361 /* This is the implementation of trace_file_write_ops method
365 tfile_end (struct trace_file_writer
*self
)
367 struct tfile_trace_file_writer
*writer
368 = (struct tfile_trace_file_writer
*) self
;
371 /* Mark the end of trace data. */
372 if (fwrite (&gotten
, 4, 1, writer
->fp
) < 1)
373 perror_with_name (writer
->pathname
);
376 /* Operations to write trace buffers into TFILE format. */
378 static const struct trace_file_write_ops tfile_write_ops
=
384 tfile_write_regblock_type
,
386 tfile_write_uploaded_tsv
,
387 tfile_write_uploaded_tp
,
389 tfile_write_definition_end
,
390 tfile_write_raw_data
,
395 /* Return a trace writer for TFILE format. */
397 struct trace_file_writer
*
398 tfile_trace_file_writer_new (void)
400 struct tfile_trace_file_writer
*writer
401 = XNEW (struct tfile_trace_file_writer
);
403 writer
->base
.ops
= &tfile_write_ops
;
405 writer
->pathname
= NULL
;
407 return (struct trace_file_writer
*) writer
;
410 /* target tfile command */
412 static tfile_target tfile_ops
;
414 #define TRACE_HEADER_SIZE 8
416 #define TFILE_PID (1)
418 static gdb::unique_xmalloc_ptr
<char> trace_filename
;
419 static int trace_fd
= -1;
420 static off_t trace_frames_offset
;
421 static off_t cur_offset
;
422 static int cur_data_size
;
423 int trace_regblock_size
;
424 static std::string trace_tdesc
;
426 static void tfile_append_tdesc_line (const char *line
);
427 static void tfile_interp_line (const char *line
,
428 struct uploaded_tp
**utpp
,
429 struct uploaded_tsv
**utsvp
);
431 /* Read SIZE bytes into READBUF from the trace frame, starting at
432 TRACE_FD's current position. Note that this call `read'
433 underneath, hence it advances the file's seek position. Throws an
434 error if the `read' syscall fails, or less than SIZE bytes are
438 tfile_read (gdb_byte
*readbuf
, int size
)
442 gotten
= read (trace_fd
, readbuf
, size
);
444 perror_with_name (trace_filename
.get ());
445 else if (gotten
< size
)
446 error (_("Premature end of file while reading trace file"));
449 /* Open the tfile target. */
452 tfile_target_open (const char *arg
, int from_tty
)
456 char header
[TRACE_HEADER_SIZE
];
457 char linebuf
[1000]; /* Should be max remote packet size or so. */
460 struct trace_status
*ts
;
461 struct uploaded_tp
*uploaded_tps
= NULL
;
462 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
464 target_preopen (from_tty
);
465 std::string filename
= extract_single_filename_arg (arg
);
466 if (filename
.empty ())
467 error (_("No trace file specified."));
469 if (!IS_ABSOLUTE_PATH (filename
.c_str ()))
470 filename
= gdb_abspath (filename
);
472 flags
= O_BINARY
| O_LARGEFILE
;
474 scratch_chan
= gdb_open_cloexec (filename
.c_str (), flags
, 0).release ();
475 if (scratch_chan
< 0)
476 perror_with_name (filename
.c_str ());
478 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
480 current_inferior ()->unpush_target (&tfile_ops
);
482 trace_filename
= make_unique_xstrdup (filename
.c_str ());
483 trace_fd
= scratch_chan
;
485 /* Make sure this is clear. */
486 trace_tdesc
.clear ();
489 /* Read the file header and test for validity. */
490 tfile_read ((gdb_byte
*) &header
, TRACE_HEADER_SIZE
);
492 bytes
+= TRACE_HEADER_SIZE
;
493 if (!(header
[0] == 0x7f
494 && (startswith (header
+ 1, "TRACE0\n"))))
495 error (_("File is not a valid trace file."));
497 current_inferior ()->push_target (&tfile_ops
);
499 trace_regblock_size
= 0;
500 ts
= current_trace_status ();
501 /* We know we're working with a file. Record its name. */
502 ts
->filename
= trace_filename
.get ();
503 /* Set defaults in case there is no status line. */
504 ts
->running_known
= 0;
505 ts
->stop_reason
= trace_stop_reason_unknown
;
506 ts
->traceframe_count
= -1;
508 ts
->disconnected_tracing
= 0;
509 ts
->circular_buffer
= 0;
513 /* Read through a section of newline-terminated lines that
514 define things like tracepoints. */
518 tfile_read (&byte
, 1);
523 /* Empty line marks end of the definition section. */
528 tfile_interp_line (linebuf
, &uploaded_tps
, &uploaded_tsvs
);
533 error (_("Excessively long lines in trace file"));
536 /* By now, tdesc lines have been read from tfile - let's parse them. */
537 target_find_description ();
539 /* Record the starting offset of the binary trace data. */
540 trace_frames_offset
= bytes
;
542 /* If we don't have a blocksize, we can't interpret the
544 if (trace_regblock_size
== 0)
545 error (_("No register block size recorded in trace file"));
547 catch (const gdb_exception
&ex
)
549 /* Remove the partially set up target. */
550 current_inferior ()->unpush_target (&tfile_ops
);
554 inferior_appeared (current_inferior (), TFILE_PID
);
556 thread_info
*thr
= add_thread_silent (&tfile_ops
, ptid_t (TFILE_PID
));
557 switch_to_thread (thr
);
559 if (ts
->traceframe_count
<= 0)
560 warning (_("No traceframes present in this file."));
562 /* Add the file's tracepoints and variables into the current mix. */
564 /* Get trace state variables first, they may be checked when parsing
565 uploaded commands. */
566 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
568 merge_uploaded_tracepoints (&uploaded_tps
);
570 post_create_inferior (from_tty
);
573 /* Interpret the given line from the definitions part of the trace
577 tfile_interp_line (const char *line
, struct uploaded_tp
**utpp
,
578 struct uploaded_tsv
**utsvp
)
580 const char *p
= line
;
582 if (startswith (p
, "R "))
585 trace_regblock_size
= strtol (p
, nullptr, 16);
587 else if (startswith (p
, "status "))
589 p
+= strlen ("status ");
590 parse_trace_status (p
, current_trace_status ());
592 else if (startswith (p
, "tp "))
595 parse_tracepoint_definition (p
, utpp
);
597 else if (startswith (p
, "tsv "))
599 p
+= strlen ("tsv ");
600 parse_tsv_definition (p
, utsvp
);
602 else if (startswith (p
, "tdesc "))
604 p
+= strlen ("tdesc ");
605 tfile_append_tdesc_line (p
);
608 warning (_("Ignoring trace file definition \"%s\""), line
);
611 /* Close the trace file and generally clean up. */
614 tfile_target::close ()
616 gdb_assert (trace_fd
!= -1);
618 switch_to_no_thread (); /* Avoid confusion from thread stuff. */
619 exit_inferior (current_inferior ());
623 trace_filename
.reset ();
624 trace_tdesc
.clear ();
626 trace_reset_local_state ();
630 tfile_target::files_info ()
632 gdb_printf ("\t`%s'\n", trace_filename
.get ());
636 tfile_target::get_tracepoint_status (tracepoint
*tp
, struct uploaded_tp
*utp
)
638 /* Other bits of trace status were collected as part of opening the
639 trace files, so nothing to do here. */
642 /* Given the position of a traceframe in the file, figure out what
643 address the frame was collected at. This would normally be the
644 value of a collected PC register, but if not available, we
648 tfile_get_traceframe_address (off_t tframe_offset
)
652 struct tracepoint
*tp
;
653 off_t saved_offset
= cur_offset
;
655 /* FIXME dig pc out of collected registers. */
657 /* Fall back to using tracepoint address. */
658 lseek (trace_fd
, tframe_offset
, SEEK_SET
);
659 tfile_read ((gdb_byte
*) &tpnum
, 2);
660 tpnum
= (short) extract_signed_integer ((gdb_byte
*) &tpnum
, 2,
662 (current_inferior ()->arch ()));
664 tp
= get_tracepoint_by_number_on_target (tpnum
);
665 /* FIXME this is a poor heuristic if multiple locations. */
666 if (tp
!= nullptr && tp
->has_locations ())
667 addr
= tp
->first_loc ().address
;
669 /* Restore our seek position. */
670 cur_offset
= saved_offset
;
671 lseek (trace_fd
, cur_offset
, SEEK_SET
);
675 /* Given a type of search and some parameters, scan the collection of
676 traceframes in the file looking for a match. When found, return
677 both the traceframe and tracepoint number, otherwise -1 for
681 tfile_target::trace_find (enum trace_find_type type
, int num
,
682 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
685 int tfnum
= 0, found
= 0;
686 unsigned int data_size
;
687 struct tracepoint
*tp
;
688 off_t offset
, tframe_offset
;
698 lseek (trace_fd
, trace_frames_offset
, SEEK_SET
);
699 offset
= trace_frames_offset
;
702 tframe_offset
= offset
;
703 tfile_read ((gdb_byte
*) &tpnum
, 2);
704 tpnum
= (short) extract_signed_integer ((gdb_byte
*) &tpnum
, 2,
706 (current_inferior ()->arch ()));
710 tfile_read ((gdb_byte
*) &data_size
, 4);
711 data_size
= (unsigned int) extract_unsigned_integer
712 ((gdb_byte
*) &data_size
, 4,
714 (current_inferior ()->arch ()));
717 if (type
== tfind_number
)
719 /* Looking for a specific trace frame. */
725 /* Start from the _next_ trace frame. */
726 if (tfnum
> get_traceframe_number ())
731 tfaddr
= tfile_get_traceframe_address (tframe_offset
);
736 tp
= get_tracepoint (num
);
737 if (tp
&& tpnum
== tp
->number_on_target
)
741 tfaddr
= tfile_get_traceframe_address (tframe_offset
);
742 if (addr1
<= tfaddr
&& tfaddr
<= addr2
)
746 tfaddr
= tfile_get_traceframe_address (tframe_offset
);
747 if (!(addr1
<= tfaddr
&& tfaddr
<= addr2
))
751 internal_error (_("unknown tfind type"));
761 cur_data_size
= data_size
;
765 /* Skip past the traceframe's data. */
766 lseek (trace_fd
, data_size
, SEEK_CUR
);
768 /* Update our own count of traceframes. */
771 /* Did not find what we were looking for. */
777 /* Walk over all traceframe block starting at POS offset from
778 CUR_OFFSET, and call CALLBACK for each block found. If CALLBACK
779 returns true, this returns the position in the traceframe where the
780 block is found, relative to the start of the traceframe
781 (cur_offset). Returns -1 if no callback call returned true,
782 indicating that all blocks have been walked. */
785 traceframe_walk_blocks (gdb::function_view
<bool (char)> callback
, int pos
)
787 /* Iterate through a traceframe's blocks, looking for a block of the
790 lseek (trace_fd
, cur_offset
+ pos
, SEEK_SET
);
791 while (pos
< cur_data_size
)
796 tfile_read ((gdb_byte
*) &block_type
, 1);
800 if (callback (block_type
))
806 lseek (trace_fd
, cur_offset
+ pos
+ trace_regblock_size
, SEEK_SET
);
807 pos
+= trace_regblock_size
;
810 lseek (trace_fd
, cur_offset
+ pos
+ 8, SEEK_SET
);
811 tfile_read ((gdb_byte
*) &mlen
, 2);
812 mlen
= (unsigned short)
813 extract_unsigned_integer ((gdb_byte
*) &mlen
, 2,
815 (current_inferior ()->arch ()));
816 lseek (trace_fd
, mlen
, SEEK_CUR
);
817 pos
+= (8 + 2 + mlen
);
820 lseek (trace_fd
, cur_offset
+ pos
+ 4 + 8, SEEK_SET
);
824 error (_("Unknown block type '%c' (0x%x) in trace frame"),
825 block_type
, block_type
);
833 /* Convenience wrapper around traceframe_walk_blocks. Looks for the
834 position offset of a block of type TYPE_WANTED in the current trace
835 frame, starting at POS. Returns -1 if no such block was found. */
838 traceframe_find_block_type (char type_wanted
, int pos
)
840 return traceframe_walk_blocks ([&] (char blocktype
)
842 return blocktype
== type_wanted
;
846 /* Look for a block of saved registers in the traceframe, and get the
847 requested register from it. */
850 tfile_target::fetch_registers (struct regcache
*regcache
, int regno
)
852 struct gdbarch
*gdbarch
= regcache
->arch ();
853 int offset
, regn
, regsize
, dummy
;
855 /* An uninitialized reg size says we're not going to be
856 successful at getting register blocks. */
857 if (!trace_regblock_size
)
860 if (traceframe_find_block_type ('R', 0) >= 0)
862 gdb_byte
*regs
= (gdb_byte
*) alloca (trace_regblock_size
);
864 tfile_read (regs
, trace_regblock_size
);
866 for (regn
= 0; regn
< gdbarch_num_regs (gdbarch
); regn
++)
868 if (!remote_register_number_and_offset (regcache
->arch (),
869 regn
, &dummy
, &offset
))
872 regsize
= register_size (gdbarch
, regn
);
873 /* Make sure we stay within block bounds. */
874 if (offset
+ regsize
> trace_regblock_size
)
876 if (regcache
->get_register_status (regn
) == REG_UNKNOWN
)
880 regcache
->raw_supply (regno
, regs
+ offset
);
883 else if (regno
== -1)
885 regcache
->raw_supply (regn
, regs
+ offset
);
891 tracefile_fetch_registers (regcache
, regno
);
894 static enum target_xfer_status
895 tfile_xfer_partial_features (const char *annex
,
896 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
897 ULONGEST offset
, ULONGEST len
,
898 ULONGEST
*xfered_len
)
900 if (strcmp (annex
, "target.xml"))
901 return TARGET_XFER_E_IO
;
904 error (_("tfile_xfer_partial: tdesc is read-only"));
906 if (trace_tdesc
.empty ())
907 return TARGET_XFER_E_IO
;
909 if (offset
>= trace_tdesc
.length ())
910 return TARGET_XFER_EOF
;
912 if (len
> trace_tdesc
.length () - offset
)
913 len
= trace_tdesc
.length () - offset
;
915 memcpy (readbuf
, trace_tdesc
.c_str () + offset
, len
);
918 return TARGET_XFER_OK
;
921 enum target_xfer_status
922 tfile_target::xfer_partial (enum target_object object
,
923 const char *annex
, gdb_byte
*readbuf
,
924 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
925 ULONGEST
*xfered_len
)
927 /* We're only doing regular memory and tdesc for now. */
928 if (object
== TARGET_OBJECT_AVAILABLE_FEATURES
)
929 return tfile_xfer_partial_features (annex
, readbuf
, writebuf
,
930 offset
, len
, xfered_len
);
931 if (object
!= TARGET_OBJECT_MEMORY
)
932 return TARGET_XFER_E_IO
;
935 error (_("tfile_xfer_partial: trace file is read-only"));
937 if (get_traceframe_number () != -1)
940 enum target_xfer_status res
;
941 /* Records the lowest available address of all blocks that
942 intersects the requested range. */
943 ULONGEST low_addr_available
= 0;
945 /* Iterate through the traceframe's blocks, looking for
947 while ((pos
= traceframe_find_block_type ('M', pos
)) >= 0)
951 bfd_endian byte_order
952 = gdbarch_byte_order (current_inferior ()->arch ());
954 tfile_read ((gdb_byte
*) &maddr
, 8);
955 maddr
= extract_unsigned_integer ((gdb_byte
*) &maddr
, 8,
957 tfile_read ((gdb_byte
*) &mlen
, 2);
958 mlen
= (unsigned short)
959 extract_unsigned_integer ((gdb_byte
*) &mlen
, 2, byte_order
);
961 /* If the block includes the first part of the desired
962 range, return as much it has; GDB will re-request the
963 remainder, which might be in a different block of this
965 if (maddr
<= offset
&& offset
< (maddr
+ mlen
))
967 amt
= (maddr
+ mlen
) - offset
;
972 lseek (trace_fd
, offset
- maddr
, SEEK_CUR
);
973 tfile_read (readbuf
, amt
);
975 return TARGET_XFER_OK
;
978 if (offset
< maddr
&& maddr
< (offset
+ len
))
979 if (low_addr_available
== 0 || low_addr_available
> maddr
)
980 low_addr_available
= maddr
;
982 /* Skip over this block. */
983 pos
+= (8 + 2 + mlen
);
986 /* Requested memory is unavailable in the context of traceframes,
987 and this address falls within a read-only section, fallback
988 to reading from executable, up to LOW_ADDR_AVAILABLE. */
989 if (offset
< low_addr_available
)
990 len
= std::min (len
, low_addr_available
- offset
);
991 res
= exec_read_partial_read_only (readbuf
, offset
, len
, xfered_len
);
993 if (res
== TARGET_XFER_OK
)
994 return TARGET_XFER_OK
;
997 /* No use trying further, we know some memory starting
998 at MEMADDR isn't available. */
1000 return TARGET_XFER_UNAVAILABLE
;
1005 /* Fallback to reading from read-only sections. */
1006 return section_table_read_available_memory (readbuf
, offset
, len
,
1011 /* Iterate through the blocks of a trace frame, looking for a 'V'
1012 block with a matching tsv number. */
1015 tfile_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
1020 /* Iterate over blocks in current frame and find the last 'V'
1021 block in which tsv number is TSVNUM. In one trace frame, there
1022 may be multiple 'V' blocks created for a given trace variable,
1023 and the last matched 'V' block contains the updated value. */
1025 while ((pos
= traceframe_find_block_type ('V', pos
)) >= 0)
1029 tfile_read ((gdb_byte
*) &vnum
, 4);
1030 vnum
= (int) extract_signed_integer ((gdb_byte
*) &vnum
, 4,
1032 (current_inferior ()->arch ()));
1035 tfile_read ((gdb_byte
*) val
, 8);
1036 *val
= extract_signed_integer ((gdb_byte
*) val
, 8,
1038 (current_inferior ()->arch ()));
1047 /* Callback for traceframe_walk_blocks. Builds a traceframe_info
1048 object for the tfile target's current traceframe. */
1051 build_traceframe_info (char blocktype
, struct traceframe_info
*info
)
1058 unsigned short mlen
;
1060 tfile_read ((gdb_byte
*) &maddr
, 8);
1061 maddr
= extract_unsigned_integer ((gdb_byte
*) &maddr
, 8,
1063 (current_inferior ()->arch ()));
1064 tfile_read ((gdb_byte
*) &mlen
, 2);
1065 mlen
= (unsigned short)
1066 extract_unsigned_integer ((gdb_byte
*) &mlen
,
1069 (current_inferior ()->arch ()));
1071 info
->memory
.emplace_back (maddr
, mlen
);
1078 tfile_read ((gdb_byte
*) &vnum
, 4);
1079 info
->tvars
.push_back (vnum
);
1087 warning (_("Unhandled trace block type (%d) '%c ' "
1088 "while building trace frame info."),
1089 blocktype
, blocktype
);
1097 tfile_target::traceframe_info ()
1099 traceframe_info_up
info (new struct traceframe_info
);
1101 traceframe_walk_blocks ([&] (char blocktype
)
1103 return build_traceframe_info (blocktype
, info
.get ());
1109 /* Handles tdesc lines from tfile by appending the payload to
1110 a global trace_tdesc variable. */
1113 tfile_append_tdesc_line (const char *line
)
1115 trace_tdesc
+= line
;
1116 trace_tdesc
+= "\n";
1119 void _initialize_tracefile_tfile ();
1121 _initialize_tracefile_tfile ()
1123 add_target (tfile_target_info
, tfile_target_open
,
1124 filename_maybe_quoted_completer
);