GDB (xrefs)
Loading...
Searching...
No Matches
/tmp/gdb-13.1/gdb/tracefile-tfile.c
Go to the documentation of this file.
1/* Trace file TFILE format support in GDB.
2
3 Copyright (C) 1997-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "tracefile.h"
22#include "readline/tilde.h"
23#include "gdbsupport/filestuff.h"
24#include "gdbsupport/rsp-low.h" /* bin2hex */
25#include "regcache.h"
26#include "inferior.h"
27#include "gdbthread.h"
28#include "exec.h"
29#include "completer.h"
30#include "filenames.h"
31#include "remote.h"
32#include "xml-tdesc.h"
33#include "target-descriptions.h"
34#include "gdbsupport/buffer.h"
35#include "gdbsupport/pathstuff.h"
36#include <algorithm>
37
38#ifndef O_LARGEFILE
39#define O_LARGEFILE 0
40#endif
41
42/* The tfile target. */
43
45 "tfile",
46 N_("Local trace dump file"),
47 N_("Use a trace file as a target.\n\
48Specify the filename of the trace file.")
49};
50
51class tfile_target final : public tracefile_target
52{
53 public:
54 const target_info &info () const override
55 { return tfile_target_info; }
56
57 void close () override;
58 void fetch_registers (struct regcache *, int) override;
60 const char *annex,
61 gdb_byte *readbuf,
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;
70
71 void get_tracepoint_status (struct breakpoint *tp,
72 struct uploaded_tp *utp) override;
73};
74
75/* TFILE trace writer. */
76
78{
80
81 /* File pointer to tfile trace file. */
82 FILE *fp;
83 /* Path name of the tfile trace file. */
84 char *pathname;
85};
86
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. */
90
91static int
93 const char *filename)
94{
95 int err = target_save_trace_data (filename);
96
97 return (err >= 0);
98}
99
100/* This is the implementation of trace_file_write_ops method
101 dtor. */
102
103static void
105{
106 struct tfile_trace_file_writer *writer
107 = (struct tfile_trace_file_writer *) self;
108
109 xfree (writer->pathname);
110
111 if (writer->fp != NULL)
112 fclose (writer->fp);
113}
114
115/* This is the implementation of trace_file_write_ops method
116 start. It creates the trace file FILENAME and registers some
117 cleanups. */
118
119static void
120tfile_start (struct trace_file_writer *self, const char *filename)
121{
122 struct tfile_trace_file_writer *writer
123 = (struct tfile_trace_file_writer *) self;
124
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));
130}
131
132/* This is the implementation of trace_file_write_ops method
133 write_header. Write the TFILE header. */
134
135static void
137{
138 struct tfile_trace_file_writer *writer
139 = (struct tfile_trace_file_writer *) self;
140 int written;
141
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);
146 if (written < 1)
147 perror_with_name (writer->pathname);
148}
149
150/* This is the implementation of trace_file_write_ops method
151 write_regblock_type. Write the size of register block. */
152
153static void
155{
156 struct tfile_trace_file_writer *writer
157 = (struct tfile_trace_file_writer *) self;
158
159 fprintf (writer->fp, "R %x\n", size);
160}
161
162/* This is the implementation of trace_file_write_ops method
163 write_status. */
164
165static void
167 struct trace_status *ts)
168{
169 struct tfile_trace_file_writer *writer
170 = (struct tfile_trace_file_writer *) self;
171
172 fprintf (writer->fp, "status %c;%s",
173 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
176 {
177 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
178
179 bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
180 fprintf (writer->fp, ":%s", buf);
181 }
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);
195 if (ts->start_time)
196 {
197 fprintf (writer->fp, ";starttime:%s",
198 phex_nz (ts->start_time, sizeof (ts->start_time)));
199 }
200 if (ts->stop_time)
201 {
202 fprintf (writer->fp, ";stoptime:%s",
203 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
204 }
205 if (ts->notes != NULL)
206 {
207 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
208
209 bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
210 fprintf (writer->fp, ";notes:%s", buf);
211 }
212 if (ts->user_name != NULL)
213 {
214 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
215
216 bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
217 fprintf (writer->fp, ";username:%s", buf);
218 }
219 fprintf (writer->fp, "\n");
220}
221
222/* This is the implementation of trace_file_write_ops method
223 write_uploaded_tsv. */
224
225static void
227 struct uploaded_tsv *utsv)
228{
229 char *buf = NULL;
230 struct tfile_trace_file_writer *writer
231 = (struct tfile_trace_file_writer *) self;
232
233 if (utsv->name)
234 {
235 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
236 bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
237 }
238
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 : "");
242
243 if (utsv->name)
244 xfree (buf);
245}
246
247#define MAX_TRACE_UPLOAD 2000
248
249/* This is the implementation of trace_file_write_ops method
250 write_uploaded_tp. */
251
252static void
254 struct uploaded_tp *utp)
255{
256 struct tfile_trace_file_writer *writer
257 = (struct tfile_trace_file_writer *) self;
258 char buf[MAX_TRACE_UPLOAD];
259
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);
265 if (utp->cond)
266 fprintf (writer->fp,
267 ":X%x,%s", (unsigned int) strlen (utp->cond.get ()) / 2,
268 utp->cond.get ());
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 ());
276 if (utp->at_string)
277 {
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);
282 }
283 if (utp->cond_string)
284 {
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);
289 }
290 for (const auto &act : utp->cmd_strings)
291 {
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);
295 }
296 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
297 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
298 utp->hit_count,
299 phex_nz (utp->traceframe_usage,
300 sizeof (utp->traceframe_usage)));
301}
302
303/* This is the implementation of trace_file_write_ops method
304 write_tdesc. */
305
306static void
308{
309 struct tfile_trace_file_writer *writer
310 = (struct tfile_trace_file_writer *) self;
311
312 gdb::optional<std::string> tdesc
314
315 if (!tdesc)
316 return;
317
318 const char *ptr = tdesc->c_str ();
319
320 /* Write tdesc line by line, prefixing each line with "tdesc ". */
321 while (ptr != NULL)
322 {
323 const char *next = strchr (ptr, '\n');
324 if (next != NULL)
325 {
326 fprintf (writer->fp, "tdesc %.*s\n", (int) (next - ptr), ptr);
327 /* Skip the \n. */
328 next++;
329 }
330 else if (*ptr != '\0')
331 {
332 /* Last line, doesn't have a newline. */
333 fprintf (writer->fp, "tdesc %s\n", ptr);
334 }
335 ptr = next;
336 }
337}
338
339/* This is the implementation of trace_file_write_ops method
340 write_definition_end. */
341
342static void
344{
345 struct tfile_trace_file_writer *writer
346 = (struct tfile_trace_file_writer *) self;
347
348 fprintf (writer->fp, "\n");
349}
350
351/* This is the implementation of trace_file_write_ops method
352 write_raw_data. */
353
354static void
355tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
356 LONGEST len)
357{
358 struct tfile_trace_file_writer *writer
359 = (struct tfile_trace_file_writer *) self;
360
361 if (fwrite (buf, len, 1, writer->fp) < 1)
362 perror_with_name (writer->pathname);
363}
364
365/* This is the implementation of trace_file_write_ops method
366 end. */
367
368static void
370{
371 struct tfile_trace_file_writer *writer
372 = (struct tfile_trace_file_writer *) self;
373 uint32_t gotten = 0;
374
375 /* Mark the end of trace data. */
376 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
377 perror_with_name (writer->pathname);
378}
379
380/* Operations to write trace buffers into TFILE format. */
381
383{
395 NULL,
396 tfile_end,
397};
398
399/* Return a trace writer for TFILE format. */
400
401struct trace_file_writer *
403{
404 struct tfile_trace_file_writer *writer
405 = XNEW (struct tfile_trace_file_writer);
406
407 writer->base.ops = &tfile_write_ops;
408 writer->fp = NULL;
409 writer->pathname = NULL;
410
411 return (struct trace_file_writer *) writer;
412}
413
414/* target tfile command */
415
417
418#define TRACE_HEADER_SIZE 8
419
420#define TFILE_PID (1)
421
422static char *trace_filename;
423static int trace_fd = -1;
425static off_t cur_offset;
426static int cur_data_size;
428static struct buffer trace_tdesc;
429
430static void tfile_append_tdesc_line (const char *line);
431static void tfile_interp_line (char *line,
432 struct uploaded_tp **utpp,
433 struct uploaded_tsv **utsvp);
434
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
439 read. */
440
441static void
442tfile_read (gdb_byte *readbuf, int size)
443{
444 int gotten;
445
446 gotten = read (trace_fd, readbuf, size);
447 if (gotten < 0)
449 else if (gotten < size)
450 error (_("Premature end of file while reading trace file"));
451}
452
453/* Open the tfile target. */
454
455static void
456tfile_target_open (const char *arg, int from_tty)
457{
458 int flags;
459 int scratch_chan;
460 char header[TRACE_HEADER_SIZE];
461 char linebuf[1000]; /* Should be max remote packet size or so. */
462 gdb_byte byte;
463 int bytes, i;
464 struct trace_status *ts;
465 struct uploaded_tp *uploaded_tps = NULL;
466 struct uploaded_tsv *uploaded_tsvs = NULL;
467
468 target_preopen (from_tty);
469 if (!arg)
470 error (_("No trace file specified."));
471
472 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
473 if (!IS_ABSOLUTE_PATH (filename.get ()))
474 filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
475
477 flags |= O_RDONLY;
478 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0).release ();
479 if (scratch_chan < 0)
480 perror_with_name (filename.get ());
481
482 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
483
485
486 trace_filename = filename.release ();
487 trace_fd = scratch_chan;
488
489 /* Make sure this is clear. */
490 buffer_free (&trace_tdesc);
491
492 bytes = 0;
493 /* Read the file header and test for validity. */
494 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
495
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."));
500
502
504 ts = current_trace_status ();
505 /* We know we're working with a file. Record its name. */
507 /* Set defaults in case there is no status line. */
508 ts->running_known = 0;
510 ts->traceframe_count = -1;
511 ts->buffer_free = 0;
512 ts->disconnected_tracing = 0;
513 ts->circular_buffer = 0;
514
515 try
516 {
517 /* Read through a section of newline-terminated lines that
518 define things like tracepoints. */
519 i = 0;
520 while (1)
521 {
522 tfile_read (&byte, 1);
523
524 ++bytes;
525 if (byte == '\n')
526 {
527 /* Empty line marks end of the definition section. */
528 if (i == 0)
529 break;
530 linebuf[i] = '\0';
531 i = 0;
532 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
533 }
534 else
535 linebuf[i++] = byte;
536 if (i >= 1000)
537 error (_("Excessively long lines in trace file"));
538 }
539
540 /* By now, tdesc lines have been read from tfile - let's parse them. */
542
543 /* Record the starting offset of the binary trace data. */
544 trace_frames_offset = bytes;
545
546 /* If we don't have a blocksize, we can't interpret the
547 traceframes. */
548 if (trace_regblock_size == 0)
549 error (_("No register block size recorded in trace file"));
550 }
551 catch (const gdb_exception &ex)
552 {
553 /* Remove the partially set up target. */
555 throw;
556 }
557
559
561 switch_to_thread (thr);
562
563 if (ts->traceframe_count <= 0)
564 warning (_("No traceframes present in this file."));
565
566 /* Add the file's tracepoints and variables into the current mix. */
567
568 /* Get trace state variables first, they may be checked when parsing
569 uploaded commands. */
571
572 merge_uploaded_tracepoints (&uploaded_tps);
573
574 post_create_inferior (from_tty);
575}
576
577/* Interpret the given line from the definitions part of the trace
578 file. */
579
580static void
581tfile_interp_line (char *line, struct uploaded_tp **utpp,
582 struct uploaded_tsv **utsvp)
583{
584 char *p = line;
585
586 if (startswith (p, "R "))
587 {
588 p += strlen ("R ");
589 trace_regblock_size = strtol (p, &p, 16);
590 }
591 else if (startswith (p, "status "))
592 {
593 p += strlen ("status ");
595 }
596 else if (startswith (p, "tp "))
597 {
598 p += strlen ("tp ");
600 }
601 else if (startswith (p, "tsv "))
602 {
603 p += strlen ("tsv ");
604 parse_tsv_definition (p, utsvp);
605 }
606 else if (startswith (p, "tdesc "))
607 {
608 p += strlen ("tdesc ");
610 }
611 else
612 warning (_("Ignoring trace file definition \"%s\""), line);
613}
614
615/* Close the trace file and generally clean up. */
616
617void
619{
620 gdb_assert (trace_fd != -1);
621
622 switch_to_no_thread (); /* Avoid confusion from thread stuff. */
624
626 trace_fd = -1;
628 trace_filename = NULL;
629 buffer_free (&trace_tdesc);
630
632}
633
634void
636{
637 gdb_printf ("\t`%s'\n", trace_filename);
638}
639
640void
642{
643 /* Other bits of trace status were collected as part of opening the
644 trace files, so nothing to do here. */
645}
646
647/* Given the position of a traceframe in the file, figure out what
648 address the frame was collected at. This would normally be the
649 value of a collected PC register, but if not available, we
650 improvise. */
651
652static CORE_ADDR
653tfile_get_traceframe_address (off_t tframe_offset)
654{
655 CORE_ADDR addr = 0;
656 short tpnum;
657 struct tracepoint *tp;
658 off_t saved_offset = cur_offset;
659
660 /* FIXME dig pc out of collected registers. */
661
662 /* Fall back to using tracepoint address. */
663 lseek (trace_fd, tframe_offset, SEEK_SET);
664 tfile_read ((gdb_byte *) &tpnum, 2);
665 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
667 (target_gdbarch ()));
668
670 /* FIXME this is a poor heuristic if multiple locations. */
671 if (tp && tp->loc)
672 addr = tp->loc->address;
673
674 /* Restore our seek position. */
675 cur_offset = saved_offset;
676 lseek (trace_fd, cur_offset, SEEK_SET);
677 return addr;
678}
679
680/* Given a type of search and some parameters, scan the collection of
681 traceframes in the file looking for a match. When found, return
682 both the traceframe and tracepoint number, otherwise -1 for
683 each. */
684
685int
687 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
688{
689 short tpnum;
690 int tfnum = 0, found = 0;
691 unsigned int data_size;
692 struct tracepoint *tp;
693 off_t offset, tframe_offset;
694 CORE_ADDR tfaddr;
695
696 if (num == -1)
697 {
698 if (tpp)
699 *tpp = -1;
700 return -1;
701 }
702
704 offset = trace_frames_offset;
705 while (1)
706 {
707 tframe_offset = offset;
708 tfile_read ((gdb_byte *) &tpnum, 2);
709 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
711 (target_gdbarch ()));
712 offset += 2;
713 if (tpnum == 0)
714 break;
715 tfile_read ((gdb_byte *) &data_size, 4);
716 data_size = (unsigned int) extract_unsigned_integer
717 ((gdb_byte *) &data_size, 4,
719 offset += 4;
720
721 if (type == tfind_number)
722 {
723 /* Looking for a specific trace frame. */
724 if (tfnum == num)
725 found = 1;
726 }
727 else
728 {
729 /* Start from the _next_ trace frame. */
730 if (tfnum > get_traceframe_number ())
731 {
732 switch (type)
733 {
734 case tfind_pc:
735 tfaddr = tfile_get_traceframe_address (tframe_offset);
736 if (tfaddr == addr1)
737 found = 1;
738 break;
739 case tfind_tp:
740 tp = get_tracepoint (num);
741 if (tp && tpnum == tp->number_on_target)
742 found = 1;
743 break;
744 case tfind_range:
745 tfaddr = tfile_get_traceframe_address (tframe_offset);
746 if (addr1 <= tfaddr && tfaddr <= addr2)
747 found = 1;
748 break;
749 case tfind_outside:
750 tfaddr = tfile_get_traceframe_address (tframe_offset);
751 if (!(addr1 <= tfaddr && tfaddr <= addr2))
752 found = 1;
753 break;
754 default:
755 internal_error (_("unknown tfind type"));
756 }
757 }
758 }
759
760 if (found)
761 {
762 if (tpp)
763 *tpp = tpnum;
764 cur_offset = offset;
765 cur_data_size = data_size;
766
767 return tfnum;
768 }
769 /* Skip past the traceframe's data. */
770 lseek (trace_fd, data_size, SEEK_CUR);
771 offset += data_size;
772 /* Update our own count of traceframes. */
773 ++tfnum;
774 }
775 /* Did not find what we were looking for. */
776 if (tpp)
777 *tpp = -1;
778 return -1;
779}
780
781/* Prototype of the callback passed to tframe_walk_blocks. */
782typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
783
784/* Callback for traceframe_walk_blocks, used to find a given block
785 type in a traceframe. */
786
787static int
788match_blocktype (char blocktype, void *data)
789{
790 char *wantedp = (char *) data;
791
792 if (*wantedp == blocktype)
793 return 1;
794
795 return 0;
796}
797
798/* Walk over all traceframe block starting at POS offset from
799 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
800 unmodified. If CALLBACK returns true, this returns the position in
801 the traceframe where the block is found, relative to the start of
802 the traceframe (cur_offset). Returns -1 if no callback call
803 returned true, indicating that all blocks have been walked. */
804
805static int
807 int pos, void *data)
808{
809 /* Iterate through a traceframe's blocks, looking for a block of the
810 requested type. */
811
812 lseek (trace_fd, cur_offset + pos, SEEK_SET);
813 while (pos < cur_data_size)
814 {
815 unsigned short mlen;
816 char block_type;
817
818 tfile_read ((gdb_byte *) &block_type, 1);
819
820 ++pos;
821
822 if ((*callback) (block_type, data))
823 return pos;
824
825 switch (block_type)
826 {
827 case 'R':
829 pos += trace_regblock_size;
830 break;
831 case 'M':
832 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
833 tfile_read ((gdb_byte *) &mlen, 2);
834 mlen = (unsigned short)
835 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
837 (target_gdbarch ()));
838 lseek (trace_fd, mlen, SEEK_CUR);
839 pos += (8 + 2 + mlen);
840 break;
841 case 'V':
842 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
843 pos += (4 + 8);
844 break;
845 default:
846 error (_("Unknown block type '%c' (0x%x) in trace frame"),
848 break;
849 }
850 }
851
852 return -1;
853}
854
855/* Convenience wrapper around traceframe_walk_blocks. Looks for the
856 position offset of a block of type TYPE_WANTED in the current trace
857 frame, starting at POS. Returns -1 if no such block was found. */
858
859static int
860traceframe_find_block_type (char type_wanted, int pos)
861{
862 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
863}
864
865/* Look for a block of saved registers in the traceframe, and get the
866 requested register from it. */
867
868void
870{
871 struct gdbarch *gdbarch = regcache->arch ();
872 int offset, regn, regsize, dummy;
873
874 /* An uninitialized reg size says we're not going to be
875 successful at getting register blocks. */
877 return;
878
879 if (traceframe_find_block_type ('R', 0) >= 0)
880 {
881 gdb_byte *regs = (gdb_byte *) alloca (trace_regblock_size);
882
884
885 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
886 {
888 regn, &dummy, &offset))
889 continue;
890
891 regsize = register_size (gdbarch, regn);
892 /* Make sure we stay within block bounds. */
893 if (offset + regsize > trace_regblock_size)
894 break;
895 if (regcache->get_register_status (regn) == REG_UNKNOWN)
896 {
897 if (regno == regn)
898 {
899 regcache->raw_supply (regno, regs + offset);
900 break;
901 }
902 else if (regno == -1)
903 {
904 regcache->raw_supply (regn, regs + offset);
905 }
906 }
907 }
908 }
909 else
911}
912
913static enum target_xfer_status
915 gdb_byte *readbuf, const gdb_byte *writebuf,
916 ULONGEST offset, ULONGEST len,
917 ULONGEST *xfered_len)
918{
919 if (strcmp (annex, "target.xml"))
920 return TARGET_XFER_E_IO;
921
922 if (readbuf == NULL)
923 error (_("tfile_xfer_partial: tdesc is read-only"));
924
925 if (trace_tdesc.used_size == 0)
926 return TARGET_XFER_E_IO;
927
928 if (offset >= trace_tdesc.used_size)
929 return TARGET_XFER_EOF;
930
931 if (len > trace_tdesc.used_size - offset)
932 len = trace_tdesc.used_size - offset;
933
934 memcpy (readbuf, trace_tdesc.buffer + offset, len);
935 *xfered_len = len;
936
937 return TARGET_XFER_OK;
938}
939
942 const char *annex, gdb_byte *readbuf,
943 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
944 ULONGEST *xfered_len)
945{
946 /* We're only doing regular memory and tdesc for now. */
948 return tfile_xfer_partial_features (annex, readbuf, writebuf,
949 offset, len, xfered_len);
950 if (object != TARGET_OBJECT_MEMORY)
951 return TARGET_XFER_E_IO;
952
953 if (readbuf == NULL)
954 error (_("tfile_xfer_partial: trace file is read-only"));
955
956 if (get_traceframe_number () != -1)
957 {
958 int pos = 0;
959 enum target_xfer_status res;
960 /* Records the lowest available address of all blocks that
961 intersects the requested range. */
962 ULONGEST low_addr_available = 0;
963
964 /* Iterate through the traceframe's blocks, looking for
965 memory. */
966 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
967 {
968 ULONGEST maddr, amt;
969 unsigned short mlen;
970 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
971
972 tfile_read ((gdb_byte *) &maddr, 8);
973 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
974 byte_order);
975 tfile_read ((gdb_byte *) &mlen, 2);
976 mlen = (unsigned short)
977 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
978
979 /* If the block includes the first part of the desired
980 range, return as much it has; GDB will re-request the
981 remainder, which might be in a different block of this
982 trace frame. */
983 if (maddr <= offset && offset < (maddr + mlen))
984 {
985 amt = (maddr + mlen) - offset;
986 if (amt > len)
987 amt = len;
988
989 if (maddr != offset)
990 lseek (trace_fd, offset - maddr, SEEK_CUR);
991 tfile_read (readbuf, amt);
992 *xfered_len = amt;
993 return TARGET_XFER_OK;
994 }
995
996 if (offset < maddr && maddr < (offset + len))
997 if (low_addr_available == 0 || low_addr_available > maddr)
998 low_addr_available = maddr;
999
1000 /* Skip over this block. */
1001 pos += (8 + 2 + mlen);
1002 }
1003
1004 /* Requested memory is unavailable in the context of traceframes,
1005 and this address falls within a read-only section, fallback
1006 to reading from executable, up to LOW_ADDR_AVAILABLE. */
1007 if (offset < low_addr_available)
1008 len = std::min (len, low_addr_available - offset);
1009 res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
1010
1011 if (res == TARGET_XFER_OK)
1012 return TARGET_XFER_OK;
1013 else
1014 {
1015 /* No use trying further, we know some memory starting
1016 at MEMADDR isn't available. */
1017 *xfered_len = len;
1019 }
1020 }
1021 else
1022 {
1023 /* Fallback to reading from read-only sections. */
1024 return section_table_read_available_memory (readbuf, offset, len,
1025 xfered_len);
1026 }
1027}
1028
1029/* Iterate through the blocks of a trace frame, looking for a 'V'
1030 block with a matching tsv number. */
1031
1032bool
1034{
1035 int pos;
1036 bool found = false;
1037
1038 /* Iterate over blocks in current frame and find the last 'V'
1039 block in which tsv number is TSVNUM. In one trace frame, there
1040 may be multiple 'V' blocks created for a given trace variable,
1041 and the last matched 'V' block contains the updated value. */
1042 pos = 0;
1043 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
1044 {
1045 int vnum;
1046
1047 tfile_read ((gdb_byte *) &vnum, 4);
1048 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
1050 (target_gdbarch ()));
1051 if (tsvnum == vnum)
1052 {
1053 tfile_read ((gdb_byte *) val, 8);
1054 *val = extract_signed_integer ((gdb_byte *) val, 8,
1056 (target_gdbarch ()));
1057 found = true;
1058 }
1059 pos += (4 + 8);
1060 }
1061
1062 return found;
1063}
1064
1065/* Callback for traceframe_walk_blocks. Builds a traceframe_info
1066 object for the tfile target's current traceframe. */
1067
1068static int
1069build_traceframe_info (char blocktype, void *data)
1070{
1071 struct traceframe_info *info = (struct traceframe_info *) data;
1072
1073 switch (blocktype)
1074 {
1075 case 'M':
1076 {
1077 ULONGEST maddr;
1078 unsigned short mlen;
1079
1080 tfile_read ((gdb_byte *) &maddr, 8);
1081 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
1083 (target_gdbarch ()));
1084 tfile_read ((gdb_byte *) &mlen, 2);
1085 mlen = (unsigned short)
1086 extract_unsigned_integer ((gdb_byte *) &mlen,
1088 (target_gdbarch ()));
1089
1090 info->memory.emplace_back (maddr, mlen);
1091 break;
1092 }
1093 case 'V':
1094 {
1095 int vnum;
1096
1097 tfile_read ((gdb_byte *) &vnum, 4);
1098 info->tvars.push_back (vnum);
1099 }
1100 case 'R':
1101 case 'S':
1102 {
1103 break;
1104 }
1105 default:
1106 warning (_("Unhandled trace block type (%d) '%c ' "
1107 "while building trace frame info."),
1108 blocktype, blocktype);
1109 break;
1110 }
1111
1112 return 0;
1113}
1114
1117{
1119
1121
1122 return info;
1123}
1124
1125/* Handles tdesc lines from tfile by appending the payload to
1126 a global trace_tdesc variable. */
1127
1128static void
1129tfile_append_tdesc_line (const char *line)
1130{
1131 buffer_grow_str (&trace_tdesc, line);
1132 buffer_grow_str (&trace_tdesc, "\n");
1133}
1134
1136void
1138{
1140}
void * xmalloc(YYSIZE_T)
void xfree(void *)
struct gdbarch * target_gdbarch(void)
Definition: arch-utils.c:1453
struct tracepoint * get_tracepoint_by_number_on_target(int num)
Definition: breakpoint.c:14045
struct tracepoint * get_tracepoint(int num)
Definition: breakpoint.c:14031
@ bp_fast_tracepoint
Definition: breakpoint.h:185
CORE_ADDR address
Definition: breakpoint.h:442
int unpush_target(struct target_ops *t)
Definition: inferior.c:98
void push_target(struct target_ops *t)
Definition: inferior.h:376
gdbarch * arch() const
Definition: regcache.c:230
void raw_supply(int regnum, const void *buf) override
Definition: regcache.c:1053
enum register_status get_register_status(int regnum) const override
Definition: regcache.c:303
bool get_trace_state_variable_value(int tsv, LONGEST *val) override
void fetch_registers(struct regcache *, int) override
const target_info & info() const override
int trace_find(enum trace_find_type type, int num, CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override
traceframe_info_up traceframe_info() override
void close() override
enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) override
void files_info() override
void get_tracepoint_status(struct breakpoint *tp, struct uploaded_tp *utp) override
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition: completer.c:204
#define SEEK_SET
Definition: defs.h:103
#define O_BINARY
Definition: defs.h:116
#define SEEK_CUR
Definition: defs.h:106
static LONGEST extract_signed_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition: defs.h:511
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition: defs.h:526
enum target_xfer_status section_table_read_available_memory(gdb_byte *readbuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
Definition: exec.c:772
enum target_xfer_status exec_read_partial_read_only(gdb_byte *readbuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
Definition: exec.c:689
ssize_t read(int fd, void *buf, size_t count)
Definition: expect-read1.c:26
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1370
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:1899
struct thread_info * add_thread_silent(process_stratum_target *targ, ptid_t ptid)
Definition: thread.c:263
void switch_to_thread(struct thread_info *thr)
Definition: thread.c:1335
void switch_to_no_thread()
Definition: thread.c:1320
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition: gnu-nat.c:1790
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition: gnu-nat.c:1862
unsigned dummy
Definition: go32-nat.c:8
size_t size
Definition: go32-nat.c:241
void post_create_inferior(int from_tty)
Definition: infcmd.c:241
void inferior_appeared(struct inferior *inf, int pid)
Definition: inferior.c:322
struct inferior * current_inferior(void)
Definition: inferior.c:54
void exit_inferior_silent(inferior *inf)
Definition: inferior.c:300
block_type
Definition: mdebugread.c:237
int register_size(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:170
int remote_register_number_and_offset(struct gdbarch *gdbarch, int regnum, int *pnum, int *poffset)
Definition: remote.c:1454
bp_location * loc
Definition: breakpoint.h:742
struct trace_file_writer base
const struct trace_file_write_ops * ops
Definition: tracefile.h:113
char * notes
Definition: tracepoint.h:151
int disconnected_tracing
Definition: tracepoint.h:136
int running_known
Definition: tracepoint.h:99
char * stop_desc
Definition: tracepoint.h:115
int traceframes_created
Definition: tracepoint.h:123
LONGEST stop_time
Definition: tracepoint.h:157
LONGEST start_time
Definition: tracepoint.h:156
int stopping_tracepoint
Definition: tracepoint.h:109
int circular_buffer
Definition: tracepoint.h:141
char * user_name
Definition: tracepoint.h:146
int traceframe_count
Definition: tracepoint.h:119
const char * filename
Definition: tracepoint.h:96
enum trace_stop_reason stop_reason
Definition: tracepoint.h:104
int number_on_target
Definition: breakpoint.h:1020
Definition: gdbtypes.h:922
std::vector< gdb::unique_xmalloc_ptr< char[]> > cmd_strings
Definition: tracepoint.h:195
gdb::unique_xmalloc_ptr< char[]> cond_string
Definition: tracepoint.h:192
enum bptype type
Definition: tracepoint.h:173
std::vector< gdb::unique_xmalloc_ptr< char[]> > step_actions
Definition: tracepoint.h:186
ULONGEST traceframe_usage
Definition: tracepoint.h:201
std::vector< gdb::unique_xmalloc_ptr< char[]> > actions
Definition: tracepoint.h:185
ULONGEST addr
Definition: tracepoint.h:174
gdb::unique_xmalloc_ptr< char[]> at_string
Definition: tracepoint.h:189
gdb::unique_xmalloc_ptr< char[]> cond
Definition: tracepoint.h:181
const char * name
Definition: tracepoint.h:210
LONGEST initial_value
Definition: tracepoint.h:212
void target_find_description(void)
void add_target(const target_info &t, target_open_ftype *func, completer_ftype *completer)
Definition: target.c:863
void target_preopen(int from_tty)
Definition: target.c:2479
int target_save_trace_data(const char *filename)
Definition: target.c:700
target_xfer_status
Definition: target.h:214
@ TARGET_XFER_E_IO
Definition: target.h:227
@ TARGET_XFER_EOF
Definition: target.h:219
@ TARGET_XFER_OK
Definition: target.h:216
@ TARGET_XFER_UNAVAILABLE
Definition: target.h:222
target_object
Definition: target.h:138
@ TARGET_OBJECT_AVAILABLE_FEATURES
Definition: target.h:169
@ TARGET_OBJECT_MEMORY
Definition: target.h:142
#define TFILE_PID
static int traceframe_walk_blocks(walk_blocks_callback_func callback, int pos, void *data)
struct trace_file_writer * tfile_trace_file_writer_new(void)
#define O_LARGEFILE
static int tfile_target_save(struct trace_file_writer *self, const char *filename)
static char * trace_filename
static void tfile_write_header(struct trace_file_writer *self)
static void tfile_read(gdb_byte *readbuf, int size)
static off_t trace_frames_offset
static void tfile_write_definition_end(struct trace_file_writer *self)
#define TRACE_HEADER_SIZE
static void tfile_write_regblock_type(struct trace_file_writer *self, int size)
static void tfile_target_open(const char *arg, int from_tty)
int trace_regblock_size
static struct buffer trace_tdesc
int(* walk_blocks_callback_func)(char blocktype, void *data)
static int cur_data_size
static int build_traceframe_info(char blocktype, void *data)
static CORE_ADDR tfile_get_traceframe_address(off_t tframe_offset)
static void tfile_write_status(struct trace_file_writer *self, struct trace_status *ts)
static void tfile_append_tdesc_line(const char *line)
static void tfile_write_uploaded_tp(struct trace_file_writer *self, struct uploaded_tp *utp)
static int traceframe_find_block_type(char type_wanted, int pos)
static void tfile_write_raw_data(struct trace_file_writer *self, gdb_byte *buf, LONGEST len)
static void tfile_write_tdesc(struct trace_file_writer *self)
static void tfile_end(struct trace_file_writer *self)
static void tfile_write_uploaded_tsv(struct trace_file_writer *self, struct uploaded_tsv *utsv)
static const target_info tfile_target_info
static enum target_xfer_status tfile_xfer_partial_features(const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
#define MAX_TRACE_UPLOAD
static off_t cur_offset
static void tfile_interp_line(char *line, struct uploaded_tp **utpp, struct uploaded_tsv **utsvp)
static const struct trace_file_write_ops tfile_write_ops
static void tfile_dtor(struct trace_file_writer *self)
static int match_blocktype(char blocktype, void *data)
static int trace_fd
static void tfile_start(struct trace_file_writer *self, const char *filename)
void _initialize_tracefile_tfile()
static tfile_target tfile_ops
void tracefile_fetch_registers(struct regcache *regcache, int regno)
Definition: tracefile.c:380
struct trace_status * current_trace_status(void)
Definition: tracepoint.c:175
const char * stop_reason_names[]
Definition: tracepoint.c:164
void parse_trace_status(const char *line, struct trace_status *ts)
Definition: tracepoint.c:3247
int get_traceframe_number(void)
Definition: tracepoint.c:2918
void parse_tsv_definition(const char *line, struct uploaded_tsv **utsvp)
Definition: tracepoint.c:3547
void trace_reset_local_state(void)
Definition: tracepoint.c:1554
void merge_uploaded_tracepoints(struct uploaded_tp **uploaded_tps)
Definition: tracepoint.c:3079
int encode_source_string(int tpnum, ULONGEST addr, const char *srctype, const char *src, char *buf, int buf_size)
Definition: tracepoint.c:2817
void parse_tracepoint_definition(const char *line, struct uploaded_tp **utpp)
Definition: tracepoint.c:3430
void merge_uploaded_trace_state_variables(struct uploaded_tsv **uploaded_tsvs)
Definition: tracepoint.c:3197
std::unique_ptr< traceframe_info > traceframe_info_up
Definition: tracepoint.h:40
@ tracepoint_error
Definition: tracepoint.h:89
@ trace_stop_command
Definition: tracepoint.h:85
@ trace_stop_reason_unknown
Definition: tracepoint.h:83
trace_find_type
Definition: tracepoint.h:408
@ tfind_outside
Definition: tracepoint.h:413
@ tfind_pc
Definition: tracepoint.h:410
@ tfind_number
Definition: tracepoint.h:409
@ tfind_range
Definition: tracepoint.h:412
@ tfind_tp
Definition: tracepoint.h:411
void perror_with_name(const char *string)
Definition: utils.c:643
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition: utils.c:1865
gdb::optional< std::string > target_fetch_description_xml(struct target_ops *ops)
Definition: xml-tdesc.c:725