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