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