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