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