gdbtypes.resolve_dynamic_range: Add function description.
[deliverable/binutils-gdb.git] / gdb / record-full.c
CommitLineData
d02ed0bb
MM
1/* Process record and replay target for GDB, the GNU debugger.
2
ecd75fc8 3 Copyright (C) 2013-2014 Free Software Foundation, Inc.
d02ed0bb
MM
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 "gdbcmd.h"
22#include "regcache.h"
23#include "gdbthread.h"
24#include "event-top.h"
25#include "exceptions.h"
26#include "completer.h"
27#include "arch-utils.h"
28#include "gdbcore.h"
29#include "exec.h"
30#include "record.h"
31#include "record-full.h"
32#include "elf-bfd.h"
33#include "gcore.h"
34#include "event-loop.h"
35#include "inf-loop.h"
36#include "gdb_bfd.h"
37#include "observer.h"
45741a9c 38#include "infrun.h"
d02ed0bb
MM
39
40#include <signal.h>
41
42/* This module implements "target record-full", also known as "process
43 record and replay". This target sits on top of a "normal" target
44 (a target that "has execution"), and provides a record and replay
45 functionality, including reverse debugging.
46
47 Target record has two modes: recording, and replaying.
48
49 In record mode, we intercept the to_resume and to_wait methods.
50 Whenever gdb resumes the target, we run the target in single step
51 mode, and we build up an execution log in which, for each executed
52 instruction, we record all changes in memory and register state.
53 This is invisible to the user, to whom it just looks like an
54 ordinary debugging session (except for performance degredation).
55
56 In replay mode, instead of actually letting the inferior run as a
57 process, we simulate its execution by playing back the recorded
58 execution log. For each instruction in the log, we simulate the
59 instruction's side effects by duplicating the changes that it would
60 have made on memory and registers. */
61
88d1aa9d 62#define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
d02ed0bb 63
88d1aa9d
MM
64#define RECORD_FULL_IS_REPLAY \
65 (record_full_list->next || execution_direction == EXEC_REVERSE)
d02ed0bb 66
88d1aa9d 67#define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
d02ed0bb
MM
68
69/* These are the core structs of the process record functionality.
70
88d1aa9d
MM
71 A record_full_entry is a record of the value change of a register
72 ("record_full_reg") or a part of memory ("record_full_mem"). And each
73 instruction must have a struct record_full_entry ("record_full_end")
74 that indicates that this is the last struct record_full_entry of this
d02ed0bb
MM
75 instruction.
76
88d1aa9d
MM
77 Each struct record_full_entry is linked to "record_full_list" by "prev"
78 and "next" pointers. */
d02ed0bb 79
88d1aa9d 80struct record_full_mem_entry
d02ed0bb
MM
81{
82 CORE_ADDR addr;
83 int len;
84 /* Set this flag if target memory for this entry
85 can no longer be accessed. */
86 int mem_entry_not_accessible;
87 union
88 {
89 gdb_byte *ptr;
90 gdb_byte buf[sizeof (gdb_byte *)];
91 } u;
92};
93
88d1aa9d 94struct record_full_reg_entry
d02ed0bb
MM
95{
96 unsigned short num;
97 unsigned short len;
98 union
99 {
100 gdb_byte *ptr;
101 gdb_byte buf[2 * sizeof (gdb_byte *)];
102 } u;
103};
104
88d1aa9d 105struct record_full_end_entry
d02ed0bb
MM
106{
107 enum gdb_signal sigval;
108 ULONGEST insn_num;
109};
110
88d1aa9d 111enum record_full_type
d02ed0bb 112{
88d1aa9d
MM
113 record_full_end = 0,
114 record_full_reg,
115 record_full_mem
d02ed0bb
MM
116};
117
118/* This is the data structure that makes up the execution log.
119
120 The execution log consists of a single linked list of entries
88d1aa9d 121 of type "struct record_full_entry". It is doubly linked so that it
d02ed0bb
MM
122 can be traversed in either direction.
123
124 The start of the list is anchored by a struct called
88d1aa9d
MM
125 "record_full_first". The pointer "record_full_list" either points
126 to the last entry that was added to the list (in record mode), or to
127 the next entry in the list that will be executed (in replay mode).
d02ed0bb 128
88d1aa9d
MM
129 Each list element (struct record_full_entry), in addition to next
130 and prev pointers, consists of a union of three entry types: mem,
131 reg, and end. A field called "type" determines which entry type is
d02ed0bb
MM
132 represented by a given list element.
133
134 Each instruction that is added to the execution log is represented
135 by a variable number of list elements ('entries'). The instruction
136 will have one "reg" entry for each register that is changed by
137 executing the instruction (including the PC in every case). It
138 will also have one "mem" entry for each memory change. Finally,
139 each instruction will have an "end" entry that separates it from
140 the changes associated with the next instruction. */
141
88d1aa9d 142struct record_full_entry
d02ed0bb 143{
88d1aa9d
MM
144 struct record_full_entry *prev;
145 struct record_full_entry *next;
146 enum record_full_type type;
d02ed0bb
MM
147 union
148 {
149 /* reg */
88d1aa9d 150 struct record_full_reg_entry reg;
d02ed0bb 151 /* mem */
88d1aa9d 152 struct record_full_mem_entry mem;
d02ed0bb 153 /* end */
88d1aa9d 154 struct record_full_end_entry end;
d02ed0bb
MM
155 } u;
156};
157
158/* If true, query if PREC cannot record memory
159 change of next instruction. */
25ea693b 160int record_full_memory_query = 0;
d02ed0bb 161
88d1aa9d 162struct record_full_core_buf_entry
d02ed0bb 163{
88d1aa9d 164 struct record_full_core_buf_entry *prev;
d02ed0bb
MM
165 struct target_section *p;
166 bfd_byte *buf;
167};
168
169/* Record buf with core target. */
88d1aa9d
MM
170static gdb_byte *record_full_core_regbuf = NULL;
171static struct target_section *record_full_core_start;
172static struct target_section *record_full_core_end;
173static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
d02ed0bb
MM
174
175/* The following variables are used for managing the linked list that
176 represents the execution log.
177
88d1aa9d
MM
178 record_full_first is the anchor that holds down the beginning of
179 the list.
d02ed0bb 180
88d1aa9d 181 record_full_list serves two functions:
d02ed0bb
MM
182 1) In record mode, it anchors the end of the list.
183 2) In replay mode, it traverses the list and points to
184 the next instruction that must be emulated.
185
88d1aa9d
MM
186 record_full_arch_list_head and record_full_arch_list_tail are used
187 to manage a separate list, which is used to build up the change
188 elements of the currently executing instruction during record mode.
189 When this instruction has been completely annotated in the "arch
190 list", it will be appended to the main execution log. */
d02ed0bb 191
88d1aa9d
MM
192static struct record_full_entry record_full_first;
193static struct record_full_entry *record_full_list = &record_full_first;
194static struct record_full_entry *record_full_arch_list_head = NULL;
195static struct record_full_entry *record_full_arch_list_tail = NULL;
d02ed0bb 196
88d1aa9d
MM
197/* 1 ask user. 0 auto delete the last struct record_full_entry. */
198static int record_full_stop_at_limit = 1;
d02ed0bb 199/* Maximum allowed number of insns in execution log. */
88d1aa9d
MM
200static unsigned int record_full_insn_max_num
201 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
d02ed0bb 202/* Actual count of insns presently in execution log. */
7ee70bf5 203static unsigned int record_full_insn_num = 0;
d02ed0bb
MM
204/* Count of insns logged so far (may be larger
205 than count of insns presently in execution log). */
88d1aa9d 206static ULONGEST record_full_insn_count;
d02ed0bb
MM
207
208/* The target_ops of process record. */
88d1aa9d
MM
209static struct target_ops record_full_ops;
210static struct target_ops record_full_core_ops;
d02ed0bb 211
8213266a
PA
212/* See record-full.h. */
213
214int
215record_full_is_used (void)
216{
217 struct target_ops *t;
218
219 t = find_record_target ();
220 return (t == &record_full_ops
221 || t == &record_full_core_ops);
222}
223
224
d02ed0bb
MM
225/* Command lists for "set/show record full". */
226static struct cmd_list_element *set_record_full_cmdlist;
227static struct cmd_list_element *show_record_full_cmdlist;
228
229/* Command list for "record full". */
230static struct cmd_list_element *record_full_cmdlist;
231
88d1aa9d
MM
232static void record_full_goto_insn (struct record_full_entry *entry,
233 enum exec_direction_kind dir);
1390f529
TT
234static void record_full_save (struct target_ops *self,
235 const char *recfilename);
88d1aa9d
MM
236
237/* Alloc and free functions for record_full_reg, record_full_mem, and
238 record_full_end entries. */
239
240/* Alloc a record_full_reg record entry. */
241
242static inline struct record_full_entry *
243record_full_reg_alloc (struct regcache *regcache, int regnum)
244{
245 struct record_full_entry *rec;
d02ed0bb
MM
246 struct gdbarch *gdbarch = get_regcache_arch (regcache);
247
88d1aa9d
MM
248 rec = xcalloc (1, sizeof (struct record_full_entry));
249 rec->type = record_full_reg;
d02ed0bb
MM
250 rec->u.reg.num = regnum;
251 rec->u.reg.len = register_size (gdbarch, regnum);
252 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
253 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
254
255 return rec;
256}
257
88d1aa9d 258/* Free a record_full_reg record entry. */
d02ed0bb
MM
259
260static inline void
88d1aa9d 261record_full_reg_release (struct record_full_entry *rec)
d02ed0bb 262{
88d1aa9d 263 gdb_assert (rec->type == record_full_reg);
d02ed0bb
MM
264 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
265 xfree (rec->u.reg.u.ptr);
266 xfree (rec);
267}
268
88d1aa9d 269/* Alloc a record_full_mem record entry. */
d02ed0bb 270
88d1aa9d
MM
271static inline struct record_full_entry *
272record_full_mem_alloc (CORE_ADDR addr, int len)
d02ed0bb 273{
88d1aa9d 274 struct record_full_entry *rec;
d02ed0bb 275
88d1aa9d
MM
276 rec = xcalloc (1, sizeof (struct record_full_entry));
277 rec->type = record_full_mem;
d02ed0bb
MM
278 rec->u.mem.addr = addr;
279 rec->u.mem.len = len;
280 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
281 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
282
283 return rec;
284}
285
88d1aa9d 286/* Free a record_full_mem record entry. */
d02ed0bb
MM
287
288static inline void
88d1aa9d 289record_full_mem_release (struct record_full_entry *rec)
d02ed0bb 290{
88d1aa9d 291 gdb_assert (rec->type == record_full_mem);
d02ed0bb
MM
292 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
293 xfree (rec->u.mem.u.ptr);
294 xfree (rec);
295}
296
88d1aa9d 297/* Alloc a record_full_end record entry. */
d02ed0bb 298
88d1aa9d
MM
299static inline struct record_full_entry *
300record_full_end_alloc (void)
d02ed0bb 301{
88d1aa9d 302 struct record_full_entry *rec;
d02ed0bb 303
88d1aa9d
MM
304 rec = xcalloc (1, sizeof (struct record_full_entry));
305 rec->type = record_full_end;
d02ed0bb
MM
306
307 return rec;
308}
309
88d1aa9d 310/* Free a record_full_end record entry. */
d02ed0bb
MM
311
312static inline void
88d1aa9d 313record_full_end_release (struct record_full_entry *rec)
d02ed0bb
MM
314{
315 xfree (rec);
316}
317
318/* Free one record entry, any type.
319 Return entry->type, in case caller wants to know. */
320
88d1aa9d
MM
321static inline enum record_full_type
322record_full_entry_release (struct record_full_entry *rec)
d02ed0bb 323{
88d1aa9d 324 enum record_full_type type = rec->type;
d02ed0bb
MM
325
326 switch (type) {
88d1aa9d
MM
327 case record_full_reg:
328 record_full_reg_release (rec);
d02ed0bb 329 break;
88d1aa9d
MM
330 case record_full_mem:
331 record_full_mem_release (rec);
d02ed0bb 332 break;
88d1aa9d
MM
333 case record_full_end:
334 record_full_end_release (rec);
d02ed0bb
MM
335 break;
336 }
337 return type;
338}
339
340/* Free all record entries in list pointed to by REC. */
341
342static void
88d1aa9d 343record_full_list_release (struct record_full_entry *rec)
d02ed0bb
MM
344{
345 if (!rec)
346 return;
347
348 while (rec->next)
349 rec = rec->next;
350
351 while (rec->prev)
352 {
353 rec = rec->prev;
88d1aa9d 354 record_full_entry_release (rec->next);
d02ed0bb
MM
355 }
356
88d1aa9d 357 if (rec == &record_full_first)
d02ed0bb 358 {
88d1aa9d
MM
359 record_full_insn_num = 0;
360 record_full_first.next = NULL;
d02ed0bb
MM
361 }
362 else
88d1aa9d 363 record_full_entry_release (rec);
d02ed0bb
MM
364}
365
366/* Free all record entries forward of the given list position. */
367
368static void
88d1aa9d 369record_full_list_release_following (struct record_full_entry *rec)
d02ed0bb 370{
88d1aa9d 371 struct record_full_entry *tmp = rec->next;
d02ed0bb
MM
372
373 rec->next = NULL;
374 while (tmp)
375 {
376 rec = tmp->next;
88d1aa9d 377 if (record_full_entry_release (tmp) == record_full_end)
d02ed0bb 378 {
88d1aa9d
MM
379 record_full_insn_num--;
380 record_full_insn_count--;
d02ed0bb
MM
381 }
382 tmp = rec;
383 }
384}
385
386/* Delete the first instruction from the beginning of the log, to make
387 room for adding a new instruction at the end of the log.
388
88d1aa9d 389 Note -- this function does not modify record_full_insn_num. */
d02ed0bb
MM
390
391static void
88d1aa9d 392record_full_list_release_first (void)
d02ed0bb 393{
88d1aa9d 394 struct record_full_entry *tmp;
d02ed0bb 395
88d1aa9d 396 if (!record_full_first.next)
d02ed0bb
MM
397 return;
398
88d1aa9d 399 /* Loop until a record_full_end. */
d02ed0bb
MM
400 while (1)
401 {
88d1aa9d
MM
402 /* Cut record_full_first.next out of the linked list. */
403 tmp = record_full_first.next;
404 record_full_first.next = tmp->next;
405 tmp->next->prev = &record_full_first;
d02ed0bb
MM
406
407 /* tmp is now isolated, and can be deleted. */
88d1aa9d
MM
408 if (record_full_entry_release (tmp) == record_full_end)
409 break; /* End loop at first record_full_end. */
d02ed0bb 410
88d1aa9d 411 if (!record_full_first.next)
d02ed0bb 412 {
88d1aa9d 413 gdb_assert (record_full_insn_num == 1);
d02ed0bb
MM
414 break; /* End loop when list is empty. */
415 }
416 }
417}
418
88d1aa9d 419/* Add a struct record_full_entry to record_full_arch_list. */
d02ed0bb
MM
420
421static void
88d1aa9d 422record_full_arch_list_add (struct record_full_entry *rec)
d02ed0bb
MM
423{
424 if (record_debug > 1)
425 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 426 "Process record: record_full_arch_list_add %s.\n",
d02ed0bb
MM
427 host_address_to_string (rec));
428
88d1aa9d 429 if (record_full_arch_list_tail)
d02ed0bb 430 {
88d1aa9d
MM
431 record_full_arch_list_tail->next = rec;
432 rec->prev = record_full_arch_list_tail;
433 record_full_arch_list_tail = rec;
d02ed0bb
MM
434 }
435 else
436 {
88d1aa9d
MM
437 record_full_arch_list_head = rec;
438 record_full_arch_list_tail = rec;
d02ed0bb
MM
439 }
440}
441
442/* Return the value storage location of a record entry. */
443static inline gdb_byte *
88d1aa9d 444record_full_get_loc (struct record_full_entry *rec)
d02ed0bb
MM
445{
446 switch (rec->type) {
88d1aa9d 447 case record_full_mem:
d02ed0bb
MM
448 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
449 return rec->u.mem.u.ptr;
450 else
451 return rec->u.mem.u.buf;
88d1aa9d 452 case record_full_reg:
d02ed0bb
MM
453 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
454 return rec->u.reg.u.ptr;
455 else
456 return rec->u.reg.u.buf;
88d1aa9d 457 case record_full_end:
d02ed0bb 458 default:
88d1aa9d 459 gdb_assert_not_reached ("unexpected record_full_entry type");
d02ed0bb
MM
460 return NULL;
461 }
462}
463
88d1aa9d 464/* Record the value of a register NUM to record_full_arch_list. */
d02ed0bb
MM
465
466int
25ea693b 467record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
d02ed0bb 468{
88d1aa9d 469 struct record_full_entry *rec;
d02ed0bb
MM
470
471 if (record_debug > 1)
472 fprintf_unfiltered (gdb_stdlog,
473 "Process record: add register num = %d to "
474 "record list.\n",
475 regnum);
476
88d1aa9d 477 rec = record_full_reg_alloc (regcache, regnum);
d02ed0bb 478
88d1aa9d 479 regcache_raw_read (regcache, regnum, record_full_get_loc (rec));
d02ed0bb 480
88d1aa9d 481 record_full_arch_list_add (rec);
d02ed0bb
MM
482
483 return 0;
484}
485
486/* Record the value of a region of memory whose address is ADDR and
88d1aa9d 487 length is LEN to record_full_arch_list. */
d02ed0bb
MM
488
489int
25ea693b 490record_full_arch_list_add_mem (CORE_ADDR addr, int len)
d02ed0bb 491{
88d1aa9d 492 struct record_full_entry *rec;
d02ed0bb
MM
493
494 if (record_debug > 1)
495 fprintf_unfiltered (gdb_stdlog,
496 "Process record: add mem addr = %s len = %d to "
497 "record list.\n",
498 paddress (target_gdbarch (), addr), len);
499
500 if (!addr) /* FIXME: Why? Some arch must permit it... */
501 return 0;
502
88d1aa9d 503 rec = record_full_mem_alloc (addr, len);
d02ed0bb 504
88d1aa9d
MM
505 if (record_read_memory (target_gdbarch (), addr,
506 record_full_get_loc (rec), len))
d02ed0bb 507 {
88d1aa9d 508 record_full_mem_release (rec);
d02ed0bb
MM
509 return -1;
510 }
511
88d1aa9d 512 record_full_arch_list_add (rec);
d02ed0bb
MM
513
514 return 0;
515}
516
88d1aa9d
MM
517/* Add a record_full_end type struct record_full_entry to
518 record_full_arch_list. */
d02ed0bb
MM
519
520int
25ea693b 521record_full_arch_list_add_end (void)
d02ed0bb 522{
88d1aa9d 523 struct record_full_entry *rec;
d02ed0bb
MM
524
525 if (record_debug > 1)
526 fprintf_unfiltered (gdb_stdlog,
527 "Process record: add end to arch list.\n");
528
88d1aa9d 529 rec = record_full_end_alloc ();
d02ed0bb 530 rec->u.end.sigval = GDB_SIGNAL_0;
88d1aa9d 531 rec->u.end.insn_num = ++record_full_insn_count;
d02ed0bb 532
88d1aa9d 533 record_full_arch_list_add (rec);
d02ed0bb
MM
534
535 return 0;
536}
537
538static void
88d1aa9d 539record_full_check_insn_num (int set_terminal)
d02ed0bb 540{
7ee70bf5 541 if (record_full_insn_num == record_full_insn_max_num)
d02ed0bb 542 {
7ee70bf5
PA
543 /* Ask user what to do. */
544 if (record_full_stop_at_limit)
d02ed0bb 545 {
7ee70bf5
PA
546 int q;
547
548 if (set_terminal)
549 target_terminal_ours ();
550 q = yquery (_("Do you want to auto delete previous execution "
551 "log entries when record/replay buffer becomes "
552 "full (record full stop-at-limit)?"));
553 if (set_terminal)
554 target_terminal_inferior ();
555 if (q)
556 record_full_stop_at_limit = 0;
557 else
558 error (_("Process record: stopped by user."));
d02ed0bb
MM
559 }
560 }
561}
562
563static void
88d1aa9d 564record_full_arch_list_cleanups (void *ignore)
d02ed0bb 565{
88d1aa9d 566 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
567}
568
569/* Before inferior step (when GDB record the running message, inferior
570 only can step), GDB will call this function to record the values to
88d1aa9d 571 record_full_list. This function will call gdbarch_process_record to
d02ed0bb 572 record the running message of inferior and set them to
88d1aa9d 573 record_full_arch_list, and add it to record_full_list. */
d02ed0bb
MM
574
575static int
88d1aa9d 576record_full_message (struct regcache *regcache, enum gdb_signal signal)
d02ed0bb
MM
577{
578 int ret;
579 struct gdbarch *gdbarch = get_regcache_arch (regcache);
88d1aa9d
MM
580 struct cleanup *old_cleanups
581 = make_cleanup (record_full_arch_list_cleanups, 0);
d02ed0bb 582
88d1aa9d
MM
583 record_full_arch_list_head = NULL;
584 record_full_arch_list_tail = NULL;
d02ed0bb 585
88d1aa9d
MM
586 /* Check record_full_insn_num. */
587 record_full_check_insn_num (1);
d02ed0bb
MM
588
589 /* If gdb sends a signal value to target_resume,
590 save it in the 'end' field of the previous instruction.
591
592 Maybe process record should record what really happened,
593 rather than what gdb pretends has happened.
594
595 So if Linux delivered the signal to the child process during
596 the record mode, we will record it and deliver it again in
597 the replay mode.
598
599 If user says "ignore this signal" during the record mode, then
600 it will be ignored again during the replay mode (no matter if
601 the user says something different, like "deliver this signal"
602 during the replay mode).
603
604 User should understand that nothing he does during the replay
605 mode will change the behavior of the child. If he tries,
606 then that is a user error.
607
608 But we should still deliver the signal to gdb during the replay,
609 if we delivered it during the recording. Therefore we should
88d1aa9d
MM
610 record the signal during record_full_wait, not
611 record_full_resume. */
612 if (record_full_list != &record_full_first) /* FIXME better way to check */
d02ed0bb 613 {
88d1aa9d
MM
614 gdb_assert (record_full_list->type == record_full_end);
615 record_full_list->u.end.sigval = signal;
d02ed0bb
MM
616 }
617
618 if (signal == GDB_SIGNAL_0
619 || !gdbarch_process_record_signal_p (gdbarch))
620 ret = gdbarch_process_record (gdbarch,
621 regcache,
622 regcache_read_pc (regcache));
623 else
624 ret = gdbarch_process_record_signal (gdbarch,
625 regcache,
626 signal);
627
628 if (ret > 0)
629 error (_("Process record: inferior program stopped."));
630 if (ret < 0)
631 error (_("Process record: failed to record execution log."));
632
633 discard_cleanups (old_cleanups);
634
88d1aa9d
MM
635 record_full_list->next = record_full_arch_list_head;
636 record_full_arch_list_head->prev = record_full_list;
637 record_full_list = record_full_arch_list_tail;
d02ed0bb 638
7ee70bf5 639 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 640 record_full_list_release_first ();
d02ed0bb 641 else
88d1aa9d 642 record_full_insn_num++;
d02ed0bb
MM
643
644 return 1;
645}
646
88d1aa9d 647struct record_full_message_args {
d02ed0bb
MM
648 struct regcache *regcache;
649 enum gdb_signal signal;
650};
651
652static int
88d1aa9d 653record_full_message_wrapper (void *args)
d02ed0bb 654{
88d1aa9d 655 struct record_full_message_args *record_full_args = args;
d02ed0bb 656
88d1aa9d
MM
657 return record_full_message (record_full_args->regcache,
658 record_full_args->signal);
d02ed0bb
MM
659}
660
661static int
88d1aa9d
MM
662record_full_message_wrapper_safe (struct regcache *regcache,
663 enum gdb_signal signal)
d02ed0bb 664{
88d1aa9d 665 struct record_full_message_args args;
d02ed0bb
MM
666
667 args.regcache = regcache;
668 args.signal = signal;
669
88d1aa9d
MM
670 return catch_errors (record_full_message_wrapper, &args, NULL,
671 RETURN_MASK_ALL);
d02ed0bb
MM
672}
673
88d1aa9d 674/* Set to 1 if record_full_store_registers and record_full_xfer_partial
d02ed0bb
MM
675 doesn't need record. */
676
88d1aa9d 677static int record_full_gdb_operation_disable = 0;
d02ed0bb
MM
678
679struct cleanup *
25ea693b 680record_full_gdb_operation_disable_set (void)
d02ed0bb
MM
681{
682 struct cleanup *old_cleanups = NULL;
683
684 old_cleanups =
88d1aa9d
MM
685 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
686 record_full_gdb_operation_disable = 1;
d02ed0bb
MM
687
688 return old_cleanups;
689}
690
691/* Flag set to TRUE for target_stopped_by_watchpoint. */
88d1aa9d 692static int record_full_hw_watchpoint = 0;
d02ed0bb
MM
693
694/* Execute one instruction from the record log. Each instruction in
695 the log will be represented by an arbitrary sequence of register
696 entries and memory entries, followed by an 'end' entry. */
697
698static inline void
88d1aa9d
MM
699record_full_exec_insn (struct regcache *regcache,
700 struct gdbarch *gdbarch,
701 struct record_full_entry *entry)
d02ed0bb
MM
702{
703 switch (entry->type)
704 {
88d1aa9d 705 case record_full_reg: /* reg */
d02ed0bb
MM
706 {
707 gdb_byte reg[MAX_REGISTER_SIZE];
708
709 if (record_debug > 1)
710 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 711 "Process record: record_full_reg %s to "
d02ed0bb
MM
712 "inferior num = %d.\n",
713 host_address_to_string (entry),
714 entry->u.reg.num);
715
716 regcache_cooked_read (regcache, entry->u.reg.num, reg);
717 regcache_cooked_write (regcache, entry->u.reg.num,
88d1aa9d
MM
718 record_full_get_loc (entry));
719 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
d02ed0bb
MM
720 }
721 break;
722
88d1aa9d 723 case record_full_mem: /* mem */
d02ed0bb
MM
724 {
725 /* Nothing to do if the entry is flagged not_accessible. */
726 if (!entry->u.mem.mem_entry_not_accessible)
727 {
728 gdb_byte *mem = alloca (entry->u.mem.len);
729
730 if (record_debug > 1)
731 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 732 "Process record: record_full_mem %s to "
d02ed0bb
MM
733 "inferior addr = %s len = %d.\n",
734 host_address_to_string (entry),
735 paddress (gdbarch, entry->u.mem.addr),
736 entry->u.mem.len);
737
738 if (record_read_memory (gdbarch,
739 entry->u.mem.addr, mem, entry->u.mem.len))
740 entry->u.mem.mem_entry_not_accessible = 1;
741 else
742 {
743 if (target_write_memory (entry->u.mem.addr,
88d1aa9d 744 record_full_get_loc (entry),
d02ed0bb
MM
745 entry->u.mem.len))
746 {
747 entry->u.mem.mem_entry_not_accessible = 1;
748 if (record_debug)
749 warning (_("Process record: error writing memory at "
750 "addr = %s len = %d."),
751 paddress (gdbarch, entry->u.mem.addr),
752 entry->u.mem.len);
753 }
754 else
755 {
88d1aa9d
MM
756 memcpy (record_full_get_loc (entry), mem,
757 entry->u.mem.len);
d02ed0bb
MM
758
759 /* We've changed memory --- check if a hardware
760 watchpoint should trap. Note that this
761 presently assumes the target beneath supports
762 continuable watchpoints. On non-continuable
763 watchpoints target, we'll want to check this
764 _before_ actually doing the memory change, and
765 not doing the change at all if the watchpoint
766 traps. */
767 if (hardware_watchpoint_inserted_in_range
768 (get_regcache_aspace (regcache),
769 entry->u.mem.addr, entry->u.mem.len))
88d1aa9d 770 record_full_hw_watchpoint = 1;
d02ed0bb
MM
771 }
772 }
773 }
774 }
775 break;
776 }
777}
778
88d1aa9d 779static void record_full_restore (void);
d02ed0bb
MM
780
781/* Asynchronous signal handle registered as event loop source for when
782 we have pending events ready to be passed to the core. */
783
88d1aa9d 784static struct async_event_handler *record_full_async_inferior_event_token;
d02ed0bb
MM
785
786static void
88d1aa9d 787record_full_async_inferior_event_handler (gdb_client_data data)
d02ed0bb
MM
788{
789 inferior_event_handler (INF_REG_EVENT, NULL);
790}
791
792/* Open the process record target. */
793
794static void
88d1aa9d 795record_full_core_open_1 (char *name, int from_tty)
d02ed0bb
MM
796{
797 struct regcache *regcache = get_current_regcache ();
798 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
799 int i;
800
88d1aa9d 801 /* Get record_full_core_regbuf. */
d02ed0bb 802 target_fetch_registers (regcache, -1);
88d1aa9d 803 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
d02ed0bb
MM
804 for (i = 0; i < regnum; i ++)
805 regcache_raw_collect (regcache, i,
88d1aa9d 806 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
d02ed0bb 807
88d1aa9d
MM
808 /* Get record_full_core_start and record_full_core_end. */
809 if (build_section_table (core_bfd, &record_full_core_start,
810 &record_full_core_end))
d02ed0bb 811 {
88d1aa9d
MM
812 xfree (record_full_core_regbuf);
813 record_full_core_regbuf = NULL;
d02ed0bb
MM
814 error (_("\"%s\": Can't find sections: %s"),
815 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
816 }
817
88d1aa9d
MM
818 push_target (&record_full_core_ops);
819 record_full_restore ();
d02ed0bb
MM
820}
821
822/* "to_open" target method for 'live' processes. */
823
824static void
88d1aa9d 825record_full_open_1 (char *name, int from_tty)
d02ed0bb
MM
826{
827 if (record_debug)
88d1aa9d 828 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
d02ed0bb
MM
829
830 /* check exec */
831 if (!target_has_execution)
832 error (_("Process record: the program is not being run."));
833 if (non_stop)
834 error (_("Process record target can't debug inferior in non-stop mode "
835 "(non-stop)."));
836
837 if (!gdbarch_process_record_p (target_gdbarch ()))
838 error (_("Process record: the current architecture doesn't support "
839 "record function."));
840
88d1aa9d 841 push_target (&record_full_ops);
d02ed0bb
MM
842}
843
88d1aa9d 844static void record_full_init_record_breakpoints (void);
d02ed0bb
MM
845
846/* "to_open" target method. Open the process record target. */
847
848static void
88d1aa9d 849record_full_open (char *name, int from_tty)
d02ed0bb
MM
850{
851 struct target_ops *t;
852
853 if (record_debug)
88d1aa9d 854 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
d02ed0bb 855
8213266a 856 record_preopen ();
d02ed0bb 857
d02ed0bb 858 /* Reset */
88d1aa9d
MM
859 record_full_insn_num = 0;
860 record_full_insn_count = 0;
861 record_full_list = &record_full_first;
862 record_full_list->next = NULL;
d02ed0bb 863
d02ed0bb 864 if (core_bfd)
88d1aa9d 865 record_full_core_open_1 (name, from_tty);
d02ed0bb 866 else
88d1aa9d 867 record_full_open_1 (name, from_tty);
d02ed0bb
MM
868
869 /* Register extra event sources in the event loop. */
88d1aa9d
MM
870 record_full_async_inferior_event_token
871 = create_async_event_handler (record_full_async_inferior_event_handler,
d02ed0bb
MM
872 NULL);
873
88d1aa9d 874 record_full_init_record_breakpoints ();
d02ed0bb
MM
875
876 observer_notify_record_changed (current_inferior (), 1);
877}
878
879/* "to_close" target method. Close the process record target. */
880
881static void
de90e03d 882record_full_close (struct target_ops *self)
d02ed0bb 883{
88d1aa9d 884 struct record_full_core_buf_entry *entry;
d02ed0bb
MM
885
886 if (record_debug)
88d1aa9d 887 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
d02ed0bb 888
88d1aa9d 889 record_full_list_release (record_full_list);
d02ed0bb 890
88d1aa9d
MM
891 /* Release record_full_core_regbuf. */
892 if (record_full_core_regbuf)
d02ed0bb 893 {
88d1aa9d
MM
894 xfree (record_full_core_regbuf);
895 record_full_core_regbuf = NULL;
d02ed0bb
MM
896 }
897
88d1aa9d
MM
898 /* Release record_full_core_buf_list. */
899 if (record_full_core_buf_list)
d02ed0bb 900 {
88d1aa9d
MM
901 for (entry = record_full_core_buf_list->prev; entry;
902 entry = entry->prev)
d02ed0bb 903 {
88d1aa9d
MM
904 xfree (record_full_core_buf_list);
905 record_full_core_buf_list = entry;
d02ed0bb 906 }
88d1aa9d 907 record_full_core_buf_list = NULL;
d02ed0bb
MM
908 }
909
88d1aa9d
MM
910 if (record_full_async_inferior_event_token)
911 delete_async_event_handler (&record_full_async_inferior_event_token);
d02ed0bb
MM
912}
913
88d1aa9d 914static int record_full_resume_step = 0;
d02ed0bb 915
88d1aa9d
MM
916/* True if we've been resumed, and so each record_full_wait call should
917 advance execution. If this is false, record_full_wait will return a
d02ed0bb 918 TARGET_WAITKIND_IGNORE. */
88d1aa9d 919static int record_full_resumed = 0;
d02ed0bb
MM
920
921/* The execution direction of the last resume we got. This is
922 necessary for async mode. Vis (order is not strictly accurate):
923
924 1. user has the global execution direction set to forward
925 2. user does a reverse-step command
88d1aa9d 926 3. record_full_resume is called with global execution direction
d02ed0bb
MM
927 temporarily switched to reverse
928 4. GDB's execution direction is reverted back to forward
929 5. target record notifies event loop there's an event to handle
930 6. infrun asks the target which direction was it going, and switches
931 the global execution direction accordingly (to reverse)
932 7. infrun polls an event out of the record target, and handles it
933 8. GDB goes back to the event loop, and goto #4.
934*/
88d1aa9d 935static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
d02ed0bb
MM
936
937/* "to_resume" target method. Resume the process record target. */
938
939static void
88d1aa9d
MM
940record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
941 enum gdb_signal signal)
d02ed0bb 942{
88d1aa9d
MM
943 record_full_resume_step = step;
944 record_full_resumed = 1;
945 record_full_execution_dir = execution_direction;
d02ed0bb 946
88d1aa9d 947 if (!RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
948 {
949 struct gdbarch *gdbarch = target_thread_architecture (ptid);
950
88d1aa9d 951 record_full_message (get_current_regcache (), signal);
d02ed0bb
MM
952
953 if (!step)
954 {
955 /* This is not hard single step. */
956 if (!gdbarch_software_single_step_p (gdbarch))
957 {
958 /* This is a normal continue. */
959 step = 1;
960 }
961 else
962 {
963 /* This arch support soft sigle step. */
964 if (single_step_breakpoints_inserted ())
965 {
966 /* This is a soft single step. */
88d1aa9d 967 record_full_resume_step = 1;
d02ed0bb
MM
968 }
969 else
970 {
971 /* This is a continue.
972 Try to insert a soft single step breakpoint. */
973 if (!gdbarch_software_single_step (gdbarch,
974 get_current_frame ()))
975 {
976 /* This system don't want use soft single step.
977 Use hard sigle step. */
978 step = 1;
979 }
980 }
981 }
982 }
983
984 /* Make sure the target beneath reports all signals. */
985 target_pass_signals (0, NULL);
986
6b84065d 987 ops->beneath->to_resume (ops->beneath, ptid, step, signal);
d02ed0bb
MM
988 }
989
990 /* We are about to start executing the inferior (or simulate it),
991 let's register it with the event loop. */
992 if (target_can_async_p ())
993 {
994 target_async (inferior_event_handler, 0);
995 /* Notify the event loop there's an event to wait for. We do
88d1aa9d
MM
996 most of the work in record_full_wait. */
997 mark_async_event_handler (record_full_async_inferior_event_token);
d02ed0bb
MM
998 }
999}
1000
88d1aa9d 1001static int record_full_get_sig = 0;
d02ed0bb
MM
1002
1003/* SIGINT signal handler, registered by "to_wait" method. */
1004
1005static void
88d1aa9d 1006record_full_sig_handler (int signo)
d02ed0bb
MM
1007{
1008 if (record_debug)
1009 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1010
1011 /* It will break the running inferior in replay mode. */
88d1aa9d 1012 record_full_resume_step = 1;
d02ed0bb 1013
88d1aa9d 1014 /* It will let record_full_wait set inferior status to get the signal
d02ed0bb 1015 SIGINT. */
88d1aa9d 1016 record_full_get_sig = 1;
d02ed0bb
MM
1017}
1018
1019static void
88d1aa9d 1020record_full_wait_cleanups (void *ignore)
d02ed0bb
MM
1021{
1022 if (execution_direction == EXEC_REVERSE)
1023 {
88d1aa9d
MM
1024 if (record_full_list->next)
1025 record_full_list = record_full_list->next;
d02ed0bb
MM
1026 }
1027 else
88d1aa9d 1028 record_full_list = record_full_list->prev;
d02ed0bb
MM
1029}
1030
1031/* "to_wait" target method for process record target.
1032
1033 In record mode, the target is always run in singlestep mode
1034 (even when gdb says to continue). The to_wait method intercepts
1035 the stop events and determines which ones are to be passed on to
1036 gdb. Most stop events are just singlestep events that gdb is not
1037 to know about, so the to_wait method just records them and keeps
1038 singlestepping.
1039
1040 In replay mode, this function emulates the recorded execution log,
1041 one instruction at a time (forward or backward), and determines
1042 where to stop. */
1043
1044static ptid_t
88d1aa9d
MM
1045record_full_wait_1 (struct target_ops *ops,
1046 ptid_t ptid, struct target_waitstatus *status,
1047 int options)
d02ed0bb 1048{
25ea693b 1049 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
1050
1051 if (record_debug)
1052 fprintf_unfiltered (gdb_stdlog,
88d1aa9d
MM
1053 "Process record: record_full_wait "
1054 "record_full_resume_step = %d, "
1055 "record_full_resumed = %d, direction=%s\n",
1056 record_full_resume_step, record_full_resumed,
1057 record_full_execution_dir == EXEC_FORWARD
1058 ? "forward" : "reverse");
1059
1060 if (!record_full_resumed)
d02ed0bb
MM
1061 {
1062 gdb_assert ((options & TARGET_WNOHANG) != 0);
1063
1064 /* No interesting event. */
1065 status->kind = TARGET_WAITKIND_IGNORE;
1066 return minus_one_ptid;
1067 }
1068
88d1aa9d
MM
1069 record_full_get_sig = 0;
1070 signal (SIGINT, record_full_sig_handler);
d02ed0bb 1071
88d1aa9d 1072 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
d02ed0bb 1073 {
88d1aa9d 1074 if (record_full_resume_step)
d02ed0bb
MM
1075 {
1076 /* This is a single step. */
6b84065d 1077 return ops->beneath->to_wait (ops->beneath, ptid, status, options);
d02ed0bb
MM
1078 }
1079 else
1080 {
1081 /* This is not a single step. */
1082 ptid_t ret;
1083 CORE_ADDR tmp_pc;
1084 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1085
1086 while (1)
1087 {
6b84065d 1088 ret = ops->beneath->to_wait (ops->beneath, ptid, status, options);
d02ed0bb
MM
1089 if (status->kind == TARGET_WAITKIND_IGNORE)
1090 {
1091 if (record_debug)
1092 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1093 "Process record: record_full_wait "
d02ed0bb
MM
1094 "target beneath not done yet\n");
1095 return ret;
1096 }
1097
1098 if (single_step_breakpoints_inserted ())
1099 remove_single_step_breakpoints ();
1100
88d1aa9d 1101 if (record_full_resume_step)
d02ed0bb
MM
1102 return ret;
1103
1104 /* Is this a SIGTRAP? */
1105 if (status->kind == TARGET_WAITKIND_STOPPED
1106 && status->value.sig == GDB_SIGNAL_TRAP)
1107 {
1108 struct regcache *regcache;
1109 struct address_space *aspace;
1110
1111 /* Yes -- this is likely our single-step finishing,
1112 but check if there's any reason the core would be
1113 interested in the event. */
1114
1115 registers_changed ();
1116 regcache = get_current_regcache ();
1117 tmp_pc = regcache_read_pc (regcache);
1118 aspace = get_regcache_aspace (regcache);
1119
1120 if (target_stopped_by_watchpoint ())
1121 {
1122 /* Always interested in watchpoints. */
1123 }
1124 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1125 {
1126 /* There is a breakpoint here. Let the core
1127 handle it. */
1128 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1129 {
1130 struct gdbarch *gdbarch
1131 = get_regcache_arch (regcache);
1132 CORE_ADDR decr_pc_after_break
118e6252 1133 = target_decr_pc_after_break (gdbarch);
d02ed0bb
MM
1134 if (decr_pc_after_break)
1135 regcache_write_pc (regcache,
1136 tmp_pc + decr_pc_after_break);
1137 }
1138 }
1139 else
1140 {
1141 /* This is a single-step trap. Record the
1142 insn and issue another step.
1143 FIXME: this part can be a random SIGTRAP too.
1144 But GDB cannot handle it. */
1145 int step = 1;
1146
88d1aa9d
MM
1147 if (!record_full_message_wrapper_safe (regcache,
1148 GDB_SIGNAL_0))
d02ed0bb
MM
1149 {
1150 status->kind = TARGET_WAITKIND_STOPPED;
1151 status->value.sig = GDB_SIGNAL_0;
1152 break;
1153 }
1154
1155 if (gdbarch_software_single_step_p (gdbarch))
1156 {
1157 /* Try to insert the software single step breakpoint.
1158 If insert success, set step to 0. */
1159 set_executing (inferior_ptid, 0);
1160 reinit_frame_cache ();
1161 if (gdbarch_software_single_step (gdbarch,
1162 get_current_frame ()))
1163 step = 0;
1164 set_executing (inferior_ptid, 1);
1165 }
1166
1167 if (record_debug)
1168 fprintf_unfiltered (gdb_stdlog,
88d1aa9d
MM
1169 "Process record: record_full_wait "
1170 "issuing one more step in the "
1171 "target beneath\n");
6b84065d
TT
1172 ops->beneath->to_resume (ops->beneath, ptid, step,
1173 GDB_SIGNAL_0);
d02ed0bb
MM
1174 continue;
1175 }
1176 }
1177
1178 /* The inferior is broken by a breakpoint or a signal. */
1179 break;
1180 }
1181
1182 return ret;
1183 }
1184 }
1185 else
1186 {
1187 struct regcache *regcache = get_current_regcache ();
1188 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1189 struct address_space *aspace = get_regcache_aspace (regcache);
1190 int continue_flag = 1;
88d1aa9d
MM
1191 int first_record_full_end = 1;
1192 struct cleanup *old_cleanups
1193 = make_cleanup (record_full_wait_cleanups, 0);
d02ed0bb
MM
1194 CORE_ADDR tmp_pc;
1195
88d1aa9d 1196 record_full_hw_watchpoint = 0;
d02ed0bb
MM
1197 status->kind = TARGET_WAITKIND_STOPPED;
1198
1199 /* Check breakpoint when forward execute. */
1200 if (execution_direction == EXEC_FORWARD)
1201 {
1202 tmp_pc = regcache_read_pc (regcache);
1203 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1204 {
118e6252 1205 int decr_pc_after_break = target_decr_pc_after_break (gdbarch);
d02ed0bb
MM
1206
1207 if (record_debug)
1208 fprintf_unfiltered (gdb_stdlog,
1209 "Process record: break at %s.\n",
1210 paddress (gdbarch, tmp_pc));
1211
1212 if (decr_pc_after_break
88d1aa9d 1213 && !record_full_resume_step
d02ed0bb
MM
1214 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1215 regcache_write_pc (regcache,
1216 tmp_pc + decr_pc_after_break);
1217 goto replay_out;
1218 }
1219 }
1220
1221 /* If GDB is in terminal_inferior mode, it will not get the signal.
1222 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1223 mode, because inferior will not executed.
1224 Then set it to terminal_ours to make GDB get the signal. */
1225 target_terminal_ours ();
1226
88d1aa9d 1227 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
d02ed0bb 1228 instruction. */
88d1aa9d
MM
1229 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1230 record_full_list = record_full_list->next;
d02ed0bb 1231
88d1aa9d 1232 /* Loop over the record_full_list, looking for the next place to
d02ed0bb
MM
1233 stop. */
1234 do
1235 {
1236 /* Check for beginning and end of log. */
1237 if (execution_direction == EXEC_REVERSE
88d1aa9d 1238 && record_full_list == &record_full_first)
d02ed0bb
MM
1239 {
1240 /* Hit beginning of record log in reverse. */
1241 status->kind = TARGET_WAITKIND_NO_HISTORY;
1242 break;
1243 }
88d1aa9d 1244 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
d02ed0bb
MM
1245 {
1246 /* Hit end of record log going forward. */
1247 status->kind = TARGET_WAITKIND_NO_HISTORY;
1248 break;
1249 }
1250
88d1aa9d 1251 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 1252
88d1aa9d 1253 if (record_full_list->type == record_full_end)
d02ed0bb
MM
1254 {
1255 if (record_debug > 1)
1256 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1257 "Process record: record_full_end %s to "
d02ed0bb 1258 "inferior.\n",
88d1aa9d 1259 host_address_to_string (record_full_list));
d02ed0bb 1260
88d1aa9d 1261 if (first_record_full_end && execution_direction == EXEC_REVERSE)
d02ed0bb 1262 {
88d1aa9d
MM
1263 /* When reverse excute, the first record_full_end is the
1264 part of current instruction. */
1265 first_record_full_end = 0;
d02ed0bb
MM
1266 }
1267 else
1268 {
88d1aa9d 1269 /* In EXEC_REVERSE mode, this is the record_full_end of prev
d02ed0bb 1270 instruction.
88d1aa9d
MM
1271 In EXEC_FORWARD mode, this is the record_full_end of
1272 current instruction. */
d02ed0bb 1273 /* step */
88d1aa9d 1274 if (record_full_resume_step)
d02ed0bb
MM
1275 {
1276 if (record_debug > 1)
1277 fprintf_unfiltered (gdb_stdlog,
1278 "Process record: step.\n");
1279 continue_flag = 0;
1280 }
1281
1282 /* check breakpoint */
1283 tmp_pc = regcache_read_pc (regcache);
1284 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1285 {
1286 int decr_pc_after_break
118e6252 1287 = target_decr_pc_after_break (gdbarch);
d02ed0bb
MM
1288
1289 if (record_debug)
1290 fprintf_unfiltered (gdb_stdlog,
1291 "Process record: break "
1292 "at %s.\n",
1293 paddress (gdbarch, tmp_pc));
1294 if (decr_pc_after_break
1295 && execution_direction == EXEC_FORWARD
88d1aa9d 1296 && !record_full_resume_step
d02ed0bb
MM
1297 && software_breakpoint_inserted_here_p (aspace,
1298 tmp_pc))
1299 regcache_write_pc (regcache,
1300 tmp_pc + decr_pc_after_break);
1301 continue_flag = 0;
1302 }
1303
88d1aa9d 1304 if (record_full_hw_watchpoint)
d02ed0bb
MM
1305 {
1306 if (record_debug)
1307 fprintf_unfiltered (gdb_stdlog,
1308 "Process record: hit hw "
1309 "watchpoint.\n");
1310 continue_flag = 0;
1311 }
1312 /* Check target signal */
88d1aa9d 1313 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
d02ed0bb
MM
1314 /* FIXME: better way to check */
1315 continue_flag = 0;
1316 }
1317 }
1318
1319 if (continue_flag)
1320 {
1321 if (execution_direction == EXEC_REVERSE)
1322 {
88d1aa9d
MM
1323 if (record_full_list->prev)
1324 record_full_list = record_full_list->prev;
d02ed0bb
MM
1325 }
1326 else
1327 {
88d1aa9d
MM
1328 if (record_full_list->next)
1329 record_full_list = record_full_list->next;
d02ed0bb
MM
1330 }
1331 }
1332 }
1333 while (continue_flag);
1334
1335replay_out:
88d1aa9d 1336 if (record_full_get_sig)
d02ed0bb 1337 status->value.sig = GDB_SIGNAL_INT;
88d1aa9d 1338 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
d02ed0bb 1339 /* FIXME: better way to check */
88d1aa9d 1340 status->value.sig = record_full_list->u.end.sigval;
d02ed0bb
MM
1341 else
1342 status->value.sig = GDB_SIGNAL_TRAP;
1343
1344 discard_cleanups (old_cleanups);
1345 }
1346
1347 signal (SIGINT, handle_sigint);
1348
1349 do_cleanups (set_cleanups);
1350 return inferior_ptid;
1351}
1352
1353static ptid_t
88d1aa9d
MM
1354record_full_wait (struct target_ops *ops,
1355 ptid_t ptid, struct target_waitstatus *status,
1356 int options)
d02ed0bb
MM
1357{
1358 ptid_t return_ptid;
1359
88d1aa9d 1360 return_ptid = record_full_wait_1 (ops, ptid, status, options);
d02ed0bb
MM
1361 if (status->kind != TARGET_WAITKIND_IGNORE)
1362 {
1363 /* We're reporting a stop. Make sure any spurious
1364 target_wait(WNOHANG) doesn't advance the target until the
1365 core wants us resumed again. */
88d1aa9d 1366 record_full_resumed = 0;
d02ed0bb
MM
1367 }
1368 return return_ptid;
1369}
1370
1371static int
6a109b6b 1372record_full_stopped_by_watchpoint (struct target_ops *ops)
d02ed0bb 1373{
88d1aa9d
MM
1374 if (RECORD_FULL_IS_REPLAY)
1375 return record_full_hw_watchpoint;
d02ed0bb 1376 else
6b84065d 1377 return ops->beneath->to_stopped_by_watchpoint (ops->beneath);
d02ed0bb
MM
1378}
1379
d02ed0bb 1380static int
88d1aa9d 1381record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
d02ed0bb 1382{
88d1aa9d 1383 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1384 return 0;
1385 else
6b84065d 1386 return ops->beneath->to_stopped_data_address (ops->beneath, addr_p);
d02ed0bb
MM
1387}
1388
1389/* Record registers change (by user or by GDB) to list as an instruction. */
1390
1391static void
88d1aa9d 1392record_full_registers_change (struct regcache *regcache, int regnum)
d02ed0bb 1393{
88d1aa9d
MM
1394 /* Check record_full_insn_num. */
1395 record_full_check_insn_num (0);
d02ed0bb 1396
88d1aa9d
MM
1397 record_full_arch_list_head = NULL;
1398 record_full_arch_list_tail = NULL;
d02ed0bb
MM
1399
1400 if (regnum < 0)
1401 {
1402 int i;
1403
1404 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1405 {
25ea693b 1406 if (record_full_arch_list_add_reg (regcache, i))
d02ed0bb 1407 {
88d1aa9d 1408 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1409 error (_("Process record: failed to record execution log."));
1410 }
1411 }
1412 }
1413 else
1414 {
25ea693b 1415 if (record_full_arch_list_add_reg (regcache, regnum))
d02ed0bb 1416 {
88d1aa9d 1417 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1418 error (_("Process record: failed to record execution log."));
1419 }
1420 }
25ea693b 1421 if (record_full_arch_list_add_end ())
d02ed0bb 1422 {
88d1aa9d 1423 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1424 error (_("Process record: failed to record execution log."));
1425 }
88d1aa9d
MM
1426 record_full_list->next = record_full_arch_list_head;
1427 record_full_arch_list_head->prev = record_full_list;
1428 record_full_list = record_full_arch_list_tail;
d02ed0bb 1429
7ee70bf5 1430 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 1431 record_full_list_release_first ();
d02ed0bb 1432 else
88d1aa9d 1433 record_full_insn_num++;
d02ed0bb
MM
1434}
1435
1436/* "to_store_registers" method for process record target. */
1437
1438static void
88d1aa9d
MM
1439record_full_store_registers (struct target_ops *ops,
1440 struct regcache *regcache,
1441 int regno)
d02ed0bb 1442{
88d1aa9d 1443 if (!record_full_gdb_operation_disable)
d02ed0bb 1444 {
88d1aa9d 1445 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1446 {
1447 int n;
1448
1449 /* Let user choose if he wants to write register or not. */
1450 if (regno < 0)
1451 n =
1452 query (_("Because GDB is in replay mode, changing the "
1453 "value of a register will make the execution "
1454 "log unusable from this point onward. "
1455 "Change all registers?"));
1456 else
1457 n =
1458 query (_("Because GDB is in replay mode, changing the value "
1459 "of a register will make the execution log unusable "
1460 "from this point onward. Change register %s?"),
1461 gdbarch_register_name (get_regcache_arch (regcache),
1462 regno));
1463
1464 if (!n)
1465 {
1466 /* Invalidate the value of regcache that was set in function
1467 "regcache_raw_write". */
1468 if (regno < 0)
1469 {
1470 int i;
1471
1472 for (i = 0;
1473 i < gdbarch_num_regs (get_regcache_arch (regcache));
1474 i++)
1475 regcache_invalidate (regcache, i);
1476 }
1477 else
1478 regcache_invalidate (regcache, regno);
1479
1480 error (_("Process record canceled the operation."));
1481 }
1482
1483 /* Destroy the record from here forward. */
88d1aa9d 1484 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1485 }
1486
88d1aa9d 1487 record_full_registers_change (regcache, regno);
d02ed0bb 1488 }
6b84065d 1489 ops->beneath->to_store_registers (ops->beneath, regcache, regno);
d02ed0bb
MM
1490}
1491
88d1aa9d
MM
1492/* "to_xfer_partial" method. Behavior is conditional on
1493 RECORD_FULL_IS_REPLAY.
d02ed0bb
MM
1494 In replay mode, we cannot write memory unles we are willing to
1495 invalidate the record/replay log from this point forward. */
1496
9b409511 1497static enum target_xfer_status
88d1aa9d
MM
1498record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1499 const char *annex, gdb_byte *readbuf,
1500 const gdb_byte *writebuf, ULONGEST offset,
9b409511 1501 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb 1502{
88d1aa9d 1503 if (!record_full_gdb_operation_disable
d02ed0bb
MM
1504 && (object == TARGET_OBJECT_MEMORY
1505 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1506 {
88d1aa9d 1507 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1508 {
1509 /* Let user choose if he wants to write memory or not. */
1510 if (!query (_("Because GDB is in replay mode, writing to memory "
1511 "will make the execution log unusable from this "
1512 "point onward. Write memory at address %s?"),
1513 paddress (target_gdbarch (), offset)))
1514 error (_("Process record canceled the operation."));
1515
1516 /* Destroy the record from here forward. */
88d1aa9d 1517 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1518 }
1519
88d1aa9d
MM
1520 /* Check record_full_insn_num */
1521 record_full_check_insn_num (0);
d02ed0bb
MM
1522
1523 /* Record registers change to list as an instruction. */
88d1aa9d
MM
1524 record_full_arch_list_head = NULL;
1525 record_full_arch_list_tail = NULL;
25ea693b 1526 if (record_full_arch_list_add_mem (offset, len))
d02ed0bb 1527 {
88d1aa9d 1528 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1529 if (record_debug)
1530 fprintf_unfiltered (gdb_stdlog,
1531 "Process record: failed to record "
1532 "execution log.");
2ed4b548 1533 return TARGET_XFER_E_IO;
d02ed0bb 1534 }
25ea693b 1535 if (record_full_arch_list_add_end ())
d02ed0bb 1536 {
88d1aa9d 1537 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1538 if (record_debug)
1539 fprintf_unfiltered (gdb_stdlog,
1540 "Process record: failed to record "
1541 "execution log.");
2ed4b548 1542 return TARGET_XFER_E_IO;
d02ed0bb 1543 }
88d1aa9d
MM
1544 record_full_list->next = record_full_arch_list_head;
1545 record_full_arch_list_head->prev = record_full_list;
1546 record_full_list = record_full_arch_list_tail;
d02ed0bb 1547
7ee70bf5 1548 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 1549 record_full_list_release_first ();
d02ed0bb 1550 else
88d1aa9d 1551 record_full_insn_num++;
d02ed0bb
MM
1552 }
1553
6b84065d
TT
1554 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1555 readbuf, writebuf, offset,
1556 len, xfered_len);
d02ed0bb
MM
1557}
1558
1559/* This structure represents a breakpoint inserted while the record
1560 target is active. We use this to know when to install/remove
1561 breakpoints in/from the target beneath. For example, a breakpoint
1562 may be inserted while recording, but removed when not replaying nor
1563 recording. In that case, the breakpoint had not been inserted on
1564 the target beneath, so we should not try to remove it there. */
1565
88d1aa9d 1566struct record_full_breakpoint
d02ed0bb
MM
1567{
1568 /* The address and address space the breakpoint was set at. */
1569 struct address_space *address_space;
1570 CORE_ADDR addr;
1571
1572 /* True when the breakpoint has been also installed in the target
1573 beneath. This will be false for breakpoints set during replay or
1574 when recording. */
1575 int in_target_beneath;
1576};
1577
88d1aa9d
MM
1578typedef struct record_full_breakpoint *record_full_breakpoint_p;
1579DEF_VEC_P(record_full_breakpoint_p);
d02ed0bb
MM
1580
1581/* The list of breakpoints inserted while the record target is
1582 active. */
88d1aa9d 1583VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
d02ed0bb
MM
1584
1585static void
88d1aa9d 1586record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
d02ed0bb
MM
1587{
1588 if (loc->loc_type != bp_loc_software_breakpoint)
1589 return;
1590
1591 if (loc->inserted)
1592 {
88d1aa9d 1593 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
d02ed0bb
MM
1594
1595 bp->addr = loc->target_info.placed_address;
1596 bp->address_space = loc->target_info.placed_address_space;
1597
1598 bp->in_target_beneath = 1;
1599
88d1aa9d 1600 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
d02ed0bb
MM
1601 }
1602}
1603
88d1aa9d 1604/* Sync existing breakpoints to record_full_breakpoints. */
d02ed0bb
MM
1605
1606static void
88d1aa9d 1607record_full_init_record_breakpoints (void)
d02ed0bb 1608{
88d1aa9d 1609 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
d02ed0bb 1610
88d1aa9d 1611 iterate_over_bp_locations (record_full_sync_record_breakpoints);
d02ed0bb
MM
1612}
1613
88d1aa9d 1614/* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
d02ed0bb
MM
1615 insert or remove breakpoints in the real target when replaying, nor
1616 when recording. */
1617
1618static int
3db08215
MM
1619record_full_insert_breakpoint (struct target_ops *ops,
1620 struct gdbarch *gdbarch,
88d1aa9d 1621 struct bp_target_info *bp_tgt)
d02ed0bb 1622{
88d1aa9d 1623 struct record_full_breakpoint *bp;
d02ed0bb
MM
1624 int in_target_beneath = 0;
1625
88d1aa9d 1626 if (!RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1627 {
1628 /* When recording, we currently always single-step, so we don't
1629 really need to install regular breakpoints in the inferior.
1630 However, we do have to insert software single-step
1631 breakpoints, in case the target can't hardware step. To keep
1632 things single, we always insert. */
1633 struct cleanup *old_cleanups;
1634 int ret;
1635
25ea693b 1636 old_cleanups = record_full_gdb_operation_disable_set ();
6b84065d 1637 ret = ops->beneath->to_insert_breakpoint (ops->beneath, gdbarch, bp_tgt);
d02ed0bb
MM
1638 do_cleanups (old_cleanups);
1639
1640 if (ret != 0)
1641 return ret;
1642
1643 in_target_beneath = 1;
1644 }
1645
88d1aa9d 1646 bp = XNEW (struct record_full_breakpoint);
d02ed0bb
MM
1647 bp->addr = bp_tgt->placed_address;
1648 bp->address_space = bp_tgt->placed_address_space;
1649 bp->in_target_beneath = in_target_beneath;
88d1aa9d 1650 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
d02ed0bb
MM
1651 return 0;
1652}
1653
1654/* "to_remove_breakpoint" method for process record target. */
1655
1656static int
3db08215
MM
1657record_full_remove_breakpoint (struct target_ops *ops,
1658 struct gdbarch *gdbarch,
88d1aa9d 1659 struct bp_target_info *bp_tgt)
d02ed0bb 1660{
88d1aa9d 1661 struct record_full_breakpoint *bp;
d02ed0bb
MM
1662 int ix;
1663
1664 for (ix = 0;
88d1aa9d
MM
1665 VEC_iterate (record_full_breakpoint_p,
1666 record_full_breakpoints, ix, bp);
d02ed0bb
MM
1667 ++ix)
1668 {
1669 if (bp->addr == bp_tgt->placed_address
1670 && bp->address_space == bp_tgt->placed_address_space)
1671 {
1672 if (bp->in_target_beneath)
1673 {
1674 struct cleanup *old_cleanups;
1675 int ret;
1676
25ea693b 1677 old_cleanups = record_full_gdb_operation_disable_set ();
6b84065d
TT
1678 ret = ops->beneath->to_remove_breakpoint (ops->beneath, gdbarch,
1679 bp_tgt);
d02ed0bb
MM
1680 do_cleanups (old_cleanups);
1681
1682 if (ret != 0)
1683 return ret;
1684 }
1685
88d1aa9d
MM
1686 VEC_unordered_remove (record_full_breakpoint_p,
1687 record_full_breakpoints, ix);
d02ed0bb
MM
1688 return 0;
1689 }
1690 }
1691
1692 gdb_assert_not_reached ("removing unknown breakpoint");
1693}
1694
1695/* "to_can_execute_reverse" method for process record target. */
1696
1697static int
19db3e69 1698record_full_can_execute_reverse (struct target_ops *self)
d02ed0bb
MM
1699{
1700 return 1;
1701}
1702
1703/* "to_get_bookmark" method for process record and prec over core. */
1704
1705static gdb_byte *
dd0e2830 1706record_full_get_bookmark (struct target_ops *self, char *args, int from_tty)
d02ed0bb 1707{
0f928d68 1708 char *ret = NULL;
d02ed0bb
MM
1709
1710 /* Return stringified form of instruction count. */
88d1aa9d
MM
1711 if (record_full_list && record_full_list->type == record_full_end)
1712 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1713
1714 if (record_debug)
1715 {
1716 if (ret)
1717 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1718 "record_full_get_bookmark returns %s\n", ret);
d02ed0bb
MM
1719 else
1720 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1721 "record_full_get_bookmark returns NULL\n");
d02ed0bb 1722 }
0f928d68 1723 return (gdb_byte *) ret;
d02ed0bb
MM
1724}
1725
1726/* "to_goto_bookmark" method for process record and prec over core. */
1727
1728static void
3c80fb48
TT
1729record_full_goto_bookmark (struct target_ops *self,
1730 gdb_byte *raw_bookmark, int from_tty)
d02ed0bb 1731{
0f928d68
PA
1732 char *bookmark = (char *) raw_bookmark;
1733
d02ed0bb
MM
1734 if (record_debug)
1735 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1736 "record_full_goto_bookmark receives %s\n", bookmark);
d02ed0bb
MM
1737
1738 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1739 {
1740 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1741 error (_("Unbalanced quotes: %s"), bookmark);
1742
1743 /* Strip trailing quote. */
1744 bookmark[strlen (bookmark) - 1] = '\0';
1745 /* Strip leading quote. */
1746 bookmark++;
88d1aa9d 1747 /* Pass along to cmd_record_full_goto. */
d02ed0bb
MM
1748 }
1749
0f928d68 1750 cmd_record_goto (bookmark, from_tty);
d02ed0bb
MM
1751 return;
1752}
1753
d02ed0bb 1754static enum exec_direction_kind
4c612759 1755record_full_execution_direction (struct target_ops *self)
d02ed0bb 1756{
88d1aa9d 1757 return record_full_execution_dir;
d02ed0bb
MM
1758}
1759
1760static void
630d6a4a 1761record_full_info (struct target_ops *self)
d02ed0bb 1762{
88d1aa9d 1763 struct record_full_entry *p;
d02ed0bb 1764
88d1aa9d 1765 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1766 printf_filtered (_("Replay mode:\n"));
1767 else
1768 printf_filtered (_("Record mode:\n"));
1769
1770 /* Find entry for first actual instruction in the log. */
88d1aa9d
MM
1771 for (p = record_full_first.next;
1772 p != NULL && p->type != record_full_end;
d02ed0bb
MM
1773 p = p->next)
1774 ;
1775
1776 /* Do we have a log at all? */
88d1aa9d 1777 if (p != NULL && p->type == record_full_end)
d02ed0bb
MM
1778 {
1779 /* Display instruction number for first instruction in the log. */
1780 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1781 pulongest (p->u.end.insn_num));
1782
1783 /* If in replay mode, display where we are in the log. */
88d1aa9d 1784 if (RECORD_FULL_IS_REPLAY)
d02ed0bb 1785 printf_filtered (_("Current instruction number is %s.\n"),
88d1aa9d 1786 pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1787
1788 /* Display instruction number for last instruction in the log. */
1789 printf_filtered (_("Highest recorded instruction number is %s.\n"),
88d1aa9d 1790 pulongest (record_full_insn_count));
d02ed0bb
MM
1791
1792 /* Display log count. */
7ee70bf5 1793 printf_filtered (_("Log contains %u instructions.\n"),
88d1aa9d 1794 record_full_insn_num);
d02ed0bb
MM
1795 }
1796 else
1797 printf_filtered (_("No instructions have been logged.\n"));
1798
1799 /* Display max log size. */
7ee70bf5 1800 printf_filtered (_("Max logged instructions is %u.\n"),
88d1aa9d 1801 record_full_insn_max_num);
d02ed0bb
MM
1802}
1803
1804/* The "to_record_delete" target method. */
1805
1806static void
d1b55219 1807record_full_delete (struct target_ops *self)
d02ed0bb 1808{
88d1aa9d 1809 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1810}
1811
1812/* The "to_record_is_replaying" target method. */
1813
1814static int
1c63c994 1815record_full_is_replaying (struct target_ops *self)
d02ed0bb 1816{
88d1aa9d 1817 return RECORD_FULL_IS_REPLAY;
d02ed0bb
MM
1818}
1819
1820/* Go to a specific entry. */
1821
1822static void
88d1aa9d 1823record_full_goto_entry (struct record_full_entry *p)
d02ed0bb
MM
1824{
1825 if (p == NULL)
1826 error (_("Target insn not found."));
88d1aa9d 1827 else if (p == record_full_list)
d02ed0bb 1828 error (_("Already at target insn."));
88d1aa9d 1829 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
d02ed0bb
MM
1830 {
1831 printf_filtered (_("Go forward to insn number %s\n"),
1832 pulongest (p->u.end.insn_num));
88d1aa9d 1833 record_full_goto_insn (p, EXEC_FORWARD);
d02ed0bb
MM
1834 }
1835 else
1836 {
1837 printf_filtered (_("Go backward to insn number %s\n"),
1838 pulongest (p->u.end.insn_num));
88d1aa9d 1839 record_full_goto_insn (p, EXEC_REVERSE);
d02ed0bb
MM
1840 }
1841
1842 registers_changed ();
1843 reinit_frame_cache ();
08d72866 1844 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
1845}
1846
1847/* The "to_goto_record_begin" target method. */
1848
1849static void
08475817 1850record_full_goto_begin (struct target_ops *self)
d02ed0bb 1851{
88d1aa9d 1852 struct record_full_entry *p = NULL;
d02ed0bb 1853
88d1aa9d
MM
1854 for (p = &record_full_first; p != NULL; p = p->next)
1855 if (p->type == record_full_end)
d02ed0bb
MM
1856 break;
1857
88d1aa9d 1858 record_full_goto_entry (p);
d02ed0bb
MM
1859}
1860
1861/* The "to_goto_record_end" target method. */
1862
1863static void
307a1b91 1864record_full_goto_end (struct target_ops *self)
d02ed0bb 1865{
88d1aa9d 1866 struct record_full_entry *p = NULL;
d02ed0bb 1867
88d1aa9d 1868 for (p = record_full_list; p->next != NULL; p = p->next)
d02ed0bb
MM
1869 ;
1870 for (; p!= NULL; p = p->prev)
88d1aa9d 1871 if (p->type == record_full_end)
d02ed0bb
MM
1872 break;
1873
88d1aa9d 1874 record_full_goto_entry (p);
d02ed0bb
MM
1875}
1876
1877/* The "to_goto_record" target method. */
1878
1879static void
606183ac 1880record_full_goto (struct target_ops *self, ULONGEST target_insn)
d02ed0bb 1881{
88d1aa9d 1882 struct record_full_entry *p = NULL;
d02ed0bb 1883
88d1aa9d
MM
1884 for (p = &record_full_first; p != NULL; p = p->next)
1885 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
d02ed0bb
MM
1886 break;
1887
88d1aa9d 1888 record_full_goto_entry (p);
d02ed0bb
MM
1889}
1890
1891static void
88d1aa9d 1892init_record_full_ops (void)
d02ed0bb 1893{
88d1aa9d
MM
1894 record_full_ops.to_shortname = "record-full";
1895 record_full_ops.to_longname = "Process record and replay target";
1896 record_full_ops.to_doc =
d02ed0bb 1897 "Log program while executing and replay execution from log.";
88d1aa9d
MM
1898 record_full_ops.to_open = record_full_open;
1899 record_full_ops.to_close = record_full_close;
1900 record_full_ops.to_resume = record_full_resume;
1901 record_full_ops.to_wait = record_full_wait;
7c1687a9
MM
1902 record_full_ops.to_disconnect = record_disconnect;
1903 record_full_ops.to_detach = record_detach;
1904 record_full_ops.to_mourn_inferior = record_mourn_inferior;
1905 record_full_ops.to_kill = record_kill;
88d1aa9d
MM
1906 record_full_ops.to_store_registers = record_full_store_registers;
1907 record_full_ops.to_xfer_partial = record_full_xfer_partial;
1908 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
1909 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
1910 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
1911 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
1912 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
1913 record_full_ops.to_stratum = record_stratum;
d02ed0bb 1914 /* Add bookmark target methods. */
88d1aa9d
MM
1915 record_full_ops.to_get_bookmark = record_full_get_bookmark;
1916 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
88d1aa9d
MM
1917 record_full_ops.to_execution_direction = record_full_execution_direction;
1918 record_full_ops.to_info_record = record_full_info;
1919 record_full_ops.to_save_record = record_full_save;
1920 record_full_ops.to_delete_record = record_full_delete;
1921 record_full_ops.to_record_is_replaying = record_full_is_replaying;
1922 record_full_ops.to_goto_record_begin = record_full_goto_begin;
1923 record_full_ops.to_goto_record_end = record_full_goto_end;
1924 record_full_ops.to_goto_record = record_full_goto;
1925 record_full_ops.to_magic = OPS_MAGIC;
d02ed0bb
MM
1926}
1927
1928/* "to_resume" method for prec over corefile. */
1929
1930static void
88d1aa9d
MM
1931record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1932 enum gdb_signal signal)
d02ed0bb 1933{
88d1aa9d
MM
1934 record_full_resume_step = step;
1935 record_full_resumed = 1;
1936 record_full_execution_dir = execution_direction;
d02ed0bb
MM
1937
1938 /* We are about to start executing the inferior (or simulate it),
1939 let's register it with the event loop. */
1940 if (target_can_async_p ())
1941 {
1942 target_async (inferior_event_handler, 0);
1943
1944 /* Notify the event loop there's an event to wait for. */
88d1aa9d 1945 mark_async_event_handler (record_full_async_inferior_event_token);
d02ed0bb
MM
1946 }
1947}
1948
1949/* "to_kill" method for prec over corefile. */
1950
1951static void
88d1aa9d 1952record_full_core_kill (struct target_ops *ops)
d02ed0bb
MM
1953{
1954 if (record_debug)
88d1aa9d 1955 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
d02ed0bb 1956
88d1aa9d 1957 unpush_target (&record_full_core_ops);
d02ed0bb
MM
1958}
1959
1960/* "to_fetch_registers" method for prec over corefile. */
1961
1962static void
88d1aa9d
MM
1963record_full_core_fetch_registers (struct target_ops *ops,
1964 struct regcache *regcache,
1965 int regno)
d02ed0bb
MM
1966{
1967 if (regno < 0)
1968 {
1969 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1970 int i;
1971
1972 for (i = 0; i < num; i ++)
1973 regcache_raw_supply (regcache, i,
88d1aa9d 1974 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
d02ed0bb
MM
1975 }
1976 else
1977 regcache_raw_supply (regcache, regno,
88d1aa9d 1978 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
d02ed0bb
MM
1979}
1980
1981/* "to_prepare_to_store" method for prec over corefile. */
1982
1983static void
f32dbf8c
MM
1984record_full_core_prepare_to_store (struct target_ops *self,
1985 struct regcache *regcache)
d02ed0bb
MM
1986{
1987}
1988
1989/* "to_store_registers" method for prec over corefile. */
1990
1991static void
88d1aa9d 1992record_full_core_store_registers (struct target_ops *ops,
d02ed0bb
MM
1993 struct regcache *regcache,
1994 int regno)
1995{
88d1aa9d 1996 if (record_full_gdb_operation_disable)
d02ed0bb 1997 regcache_raw_collect (regcache, regno,
88d1aa9d 1998 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
d02ed0bb
MM
1999 else
2000 error (_("You can't do that without a process to debug."));
2001}
2002
2003/* "to_xfer_partial" method for prec over corefile. */
2004
9b409511 2005static enum target_xfer_status
88d1aa9d
MM
2006record_full_core_xfer_partial (struct target_ops *ops,
2007 enum target_object object,
2008 const char *annex, gdb_byte *readbuf,
2009 const gdb_byte *writebuf, ULONGEST offset,
9b409511 2010 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb
MM
2011{
2012 if (object == TARGET_OBJECT_MEMORY)
2013 {
88d1aa9d 2014 if (record_full_gdb_operation_disable || !writebuf)
d02ed0bb
MM
2015 {
2016 struct target_section *p;
2017
88d1aa9d 2018 for (p = record_full_core_start; p < record_full_core_end; p++)
d02ed0bb
MM
2019 {
2020 if (offset >= p->addr)
2021 {
88d1aa9d 2022 struct record_full_core_buf_entry *entry;
d02ed0bb
MM
2023 ULONGEST sec_offset;
2024
2025 if (offset >= p->endaddr)
2026 continue;
2027
2028 if (offset + len > p->endaddr)
2029 len = p->endaddr - offset;
2030
2031 sec_offset = offset - p->addr;
2032
2033 /* Read readbuf or write writebuf p, offset, len. */
2034 /* Check flags. */
2035 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2036 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2037 {
2038 if (readbuf)
2039 memset (readbuf, 0, len);
9b409511
YQ
2040
2041 *xfered_len = len;
2042 return TARGET_XFER_OK;
d02ed0bb 2043 }
88d1aa9d
MM
2044 /* Get record_full_core_buf_entry. */
2045 for (entry = record_full_core_buf_list; entry;
d02ed0bb
MM
2046 entry = entry->prev)
2047 if (entry->p == p)
2048 break;
2049 if (writebuf)
2050 {
2051 if (!entry)
2052 {
2053 /* Add a new entry. */
88d1aa9d
MM
2054 entry = (struct record_full_core_buf_entry *)
2055 xmalloc
2056 (sizeof (struct record_full_core_buf_entry));
d02ed0bb 2057 entry->p = p;
2b2848e2
DE
2058 if (!bfd_malloc_and_get_section
2059 (p->the_bfd_section->owner,
2060 p->the_bfd_section,
2061 &entry->buf))
d02ed0bb
MM
2062 {
2063 xfree (entry);
9b409511 2064 return TARGET_XFER_EOF;
d02ed0bb 2065 }
88d1aa9d
MM
2066 entry->prev = record_full_core_buf_list;
2067 record_full_core_buf_list = entry;
d02ed0bb
MM
2068 }
2069
2070 memcpy (entry->buf + sec_offset, writebuf,
2071 (size_t) len);
2072 }
2073 else
2074 {
2075 if (!entry)
6b84065d
TT
2076 return ops->beneath->to_xfer_partial (ops->beneath,
2077 object, annex,
2078 readbuf, writebuf,
2079 offset, len,
2080 xfered_len);
d02ed0bb
MM
2081
2082 memcpy (readbuf, entry->buf + sec_offset,
2083 (size_t) len);
2084 }
2085
9b409511
YQ
2086 *xfered_len = len;
2087 return TARGET_XFER_OK;
d02ed0bb
MM
2088 }
2089 }
2090
2ed4b548 2091 return TARGET_XFER_E_IO;
d02ed0bb
MM
2092 }
2093 else
2094 error (_("You can't do that without a process to debug."));
2095 }
2096
6b84065d
TT
2097 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
2098 readbuf, writebuf, offset, len,
2099 xfered_len);
d02ed0bb
MM
2100}
2101
2102/* "to_insert_breakpoint" method for prec over corefile. */
2103
2104static int
3db08215
MM
2105record_full_core_insert_breakpoint (struct target_ops *ops,
2106 struct gdbarch *gdbarch,
88d1aa9d 2107 struct bp_target_info *bp_tgt)
d02ed0bb
MM
2108{
2109 return 0;
2110}
2111
2112/* "to_remove_breakpoint" method for prec over corefile. */
2113
2114static int
3db08215
MM
2115record_full_core_remove_breakpoint (struct target_ops *ops,
2116 struct gdbarch *gdbarch,
88d1aa9d 2117 struct bp_target_info *bp_tgt)
d02ed0bb
MM
2118{
2119 return 0;
2120}
2121
2122/* "to_has_execution" method for prec over corefile. */
2123
2124static int
88d1aa9d 2125record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
d02ed0bb
MM
2126{
2127 return 1;
2128}
2129
2130static void
88d1aa9d 2131init_record_full_core_ops (void)
d02ed0bb 2132{
88d1aa9d
MM
2133 record_full_core_ops.to_shortname = "record-core";
2134 record_full_core_ops.to_longname = "Process record and replay target";
2135 record_full_core_ops.to_doc =
d02ed0bb 2136 "Log program while executing and replay execution from log.";
88d1aa9d
MM
2137 record_full_core_ops.to_open = record_full_open;
2138 record_full_core_ops.to_close = record_full_close;
2139 record_full_core_ops.to_resume = record_full_core_resume;
2140 record_full_core_ops.to_wait = record_full_wait;
2141 record_full_core_ops.to_kill = record_full_core_kill;
2142 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2143 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2144 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2145 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2146 record_full_core_ops.to_insert_breakpoint
2147 = record_full_core_insert_breakpoint;
2148 record_full_core_ops.to_remove_breakpoint
2149 = record_full_core_remove_breakpoint;
2150 record_full_core_ops.to_stopped_by_watchpoint
2151 = record_full_stopped_by_watchpoint;
2152 record_full_core_ops.to_stopped_data_address
2153 = record_full_stopped_data_address;
2154 record_full_core_ops.to_can_execute_reverse
2155 = record_full_can_execute_reverse;
2156 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2157 record_full_core_ops.to_stratum = record_stratum;
d02ed0bb 2158 /* Add bookmark target methods. */
88d1aa9d
MM
2159 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2160 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
88d1aa9d
MM
2161 record_full_core_ops.to_execution_direction
2162 = record_full_execution_direction;
2163 record_full_core_ops.to_info_record = record_full_info;
2164 record_full_core_ops.to_delete_record = record_full_delete;
2165 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2166 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2167 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2168 record_full_core_ops.to_goto_record = record_full_goto;
2169 record_full_core_ops.to_magic = OPS_MAGIC;
d02ed0bb
MM
2170}
2171
2172/* Record log save-file format
2173 Version 1 (never released)
2174
2175 Header:
2176 4 bytes: magic number htonl(0x20090829).
2177 NOTE: be sure to change whenever this file format changes!
2178
2179 Records:
88d1aa9d
MM
2180 record_full_end:
2181 1 byte: record type (record_full_end, see enum record_full_type).
2182 record_full_reg:
2183 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2184 8 bytes: register id (network byte order).
2185 MAX_REGISTER_SIZE bytes: register value.
88d1aa9d
MM
2186 record_full_mem:
2187 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2188 8 bytes: memory length (network byte order).
2189 8 bytes: memory address (network byte order).
2190 n bytes: memory value (n == memory length).
2191
2192 Version 2
2193 4 bytes: magic number netorder32(0x20091016).
2194 NOTE: be sure to change whenever this file format changes!
2195
2196 Records:
88d1aa9d
MM
2197 record_full_end:
2198 1 byte: record type (record_full_end, see enum record_full_type).
d02ed0bb
MM
2199 4 bytes: signal
2200 4 bytes: instruction count
88d1aa9d
MM
2201 record_full_reg:
2202 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2203 4 bytes: register id (network byte order).
2204 n bytes: register value (n == actual register size).
2205 (eg. 4 bytes for x86 general registers).
88d1aa9d
MM
2206 record_full_mem:
2207 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2208 4 bytes: memory length (network byte order).
2209 8 bytes: memory address (network byte order).
2210 n bytes: memory value (n == memory length).
2211
2212*/
2213
2214/* bfdcore_read -- read bytes from a core file section. */
2215
2216static inline void
2217bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2218{
2219 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2220
2221 if (ret)
2222 *offset += len;
2223 else
2224 error (_("Failed to read %d bytes from core file %s ('%s')."),
2225 len, bfd_get_filename (obfd),
2226 bfd_errmsg (bfd_get_error ()));
2227}
2228
2229static inline uint64_t
2230netorder64 (uint64_t input)
2231{
2232 uint64_t ret;
2233
2234 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2235 BFD_ENDIAN_BIG, input);
2236 return ret;
2237}
2238
2239static inline uint32_t
2240netorder32 (uint32_t input)
2241{
2242 uint32_t ret;
2243
2244 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2245 BFD_ENDIAN_BIG, input);
2246 return ret;
2247}
2248
2249static inline uint16_t
2250netorder16 (uint16_t input)
2251{
2252 uint16_t ret;
2253
2254 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2255 BFD_ENDIAN_BIG, input);
2256 return ret;
2257}
2258
2259/* Restore the execution log from a core_bfd file. */
2260static void
88d1aa9d 2261record_full_restore (void)
d02ed0bb
MM
2262{
2263 uint32_t magic;
2264 struct cleanup *old_cleanups;
88d1aa9d 2265 struct record_full_entry *rec;
d02ed0bb
MM
2266 asection *osec;
2267 uint32_t osec_size;
2268 int bfd_offset = 0;
2269 struct regcache *regcache;
2270
2271 /* We restore the execution log from the open core bfd,
2272 if there is one. */
2273 if (core_bfd == NULL)
2274 return;
2275
88d1aa9d
MM
2276 /* "record_full_restore" can only be called when record list is empty. */
2277 gdb_assert (record_full_first.next == NULL);
d02ed0bb
MM
2278
2279 if (record_debug)
2280 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2281
2282 /* Now need to find our special note section. */
2283 osec = bfd_get_section_by_name (core_bfd, "null0");
2284 if (record_debug)
2285 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2286 osec ? "succeeded" : "failed");
2287 if (osec == NULL)
2288 return;
2289 osec_size = bfd_section_size (core_bfd, osec);
2290 if (record_debug)
2291 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2292
2293 /* Check the magic code. */
2294 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
88d1aa9d 2295 if (magic != RECORD_FULL_FILE_MAGIC)
d02ed0bb
MM
2296 error (_("Version mis-match or file format error in core file %s."),
2297 bfd_get_filename (core_bfd));
2298 if (record_debug)
2299 fprintf_unfiltered (gdb_stdlog,
2300 " Reading 4-byte magic cookie "
88d1aa9d 2301 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb
MM
2302 phex_nz (netorder32 (magic), 4));
2303
88d1aa9d
MM
2304 /* Restore the entries in recfd into record_full_arch_list_head and
2305 record_full_arch_list_tail. */
2306 record_full_arch_list_head = NULL;
2307 record_full_arch_list_tail = NULL;
2308 record_full_insn_num = 0;
2309 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
d02ed0bb
MM
2310 regcache = get_current_regcache ();
2311
2312 while (1)
2313 {
2314 uint8_t rectype;
2315 uint32_t regnum, len, signal, count;
2316 uint64_t addr;
2317
2318 /* We are finished when offset reaches osec_size. */
2319 if (bfd_offset >= osec_size)
2320 break;
2321 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2322
2323 switch (rectype)
2324 {
88d1aa9d 2325 case record_full_reg: /* reg */
d02ed0bb
MM
2326 /* Get register number to regnum. */
2327 bfdcore_read (core_bfd, osec, &regnum,
2328 sizeof (regnum), &bfd_offset);
2329 regnum = netorder32 (regnum);
2330
88d1aa9d 2331 rec = record_full_reg_alloc (regcache, regnum);
d02ed0bb
MM
2332
2333 /* Get val. */
88d1aa9d 2334 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
d02ed0bb
MM
2335 rec->u.reg.len, &bfd_offset);
2336
2337 if (record_debug)
2338 fprintf_unfiltered (gdb_stdlog,
2339 " Reading register %d (1 "
2340 "plus %lu plus %d bytes)\n",
2341 rec->u.reg.num,
2342 (unsigned long) sizeof (regnum),
2343 rec->u.reg.len);
2344 break;
2345
88d1aa9d 2346 case record_full_mem: /* mem */
d02ed0bb
MM
2347 /* Get len. */
2348 bfdcore_read (core_bfd, osec, &len,
2349 sizeof (len), &bfd_offset);
2350 len = netorder32 (len);
2351
2352 /* Get addr. */
2353 bfdcore_read (core_bfd, osec, &addr,
2354 sizeof (addr), &bfd_offset);
2355 addr = netorder64 (addr);
2356
88d1aa9d 2357 rec = record_full_mem_alloc (addr, len);
d02ed0bb
MM
2358
2359 /* Get val. */
88d1aa9d 2360 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
d02ed0bb
MM
2361 rec->u.mem.len, &bfd_offset);
2362
2363 if (record_debug)
2364 fprintf_unfiltered (gdb_stdlog,
2365 " Reading memory %s (1 plus "
2366 "%lu plus %lu plus %d bytes)\n",
2367 paddress (get_current_arch (),
2368 rec->u.mem.addr),
2369 (unsigned long) sizeof (addr),
2370 (unsigned long) sizeof (len),
2371 rec->u.mem.len);
2372 break;
2373
88d1aa9d
MM
2374 case record_full_end: /* end */
2375 rec = record_full_end_alloc ();
2376 record_full_insn_num ++;
d02ed0bb
MM
2377
2378 /* Get signal value. */
2379 bfdcore_read (core_bfd, osec, &signal,
2380 sizeof (signal), &bfd_offset);
2381 signal = netorder32 (signal);
2382 rec->u.end.sigval = signal;
2383
2384 /* Get insn count. */
2385 bfdcore_read (core_bfd, osec, &count,
2386 sizeof (count), &bfd_offset);
2387 count = netorder32 (count);
2388 rec->u.end.insn_num = count;
88d1aa9d 2389 record_full_insn_count = count + 1;
d02ed0bb
MM
2390 if (record_debug)
2391 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2392 " Reading record_full_end (1 + "
d02ed0bb
MM
2393 "%lu + %lu bytes), offset == %s\n",
2394 (unsigned long) sizeof (signal),
2395 (unsigned long) sizeof (count),
2396 paddress (get_current_arch (),
2397 bfd_offset));
2398 break;
2399
2400 default:
2401 error (_("Bad entry type in core file %s."),
2402 bfd_get_filename (core_bfd));
2403 break;
2404 }
2405
2406 /* Add rec to record arch list. */
88d1aa9d 2407 record_full_arch_list_add (rec);
d02ed0bb
MM
2408 }
2409
2410 discard_cleanups (old_cleanups);
2411
88d1aa9d
MM
2412 /* Add record_full_arch_list_head to the end of record list. */
2413 record_full_first.next = record_full_arch_list_head;
2414 record_full_arch_list_head->prev = &record_full_first;
2415 record_full_arch_list_tail->next = NULL;
2416 record_full_list = &record_full_first;
d02ed0bb 2417
88d1aa9d
MM
2418 /* Update record_full_insn_max_num. */
2419 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2420 {
88d1aa9d 2421 record_full_insn_max_num = record_full_insn_num;
7ee70bf5 2422 warning (_("Auto increase record/replay buffer limit to %u."),
88d1aa9d 2423 record_full_insn_max_num);
d02ed0bb
MM
2424 }
2425
2426 /* Succeeded. */
2427 printf_filtered (_("Restored records from core file %s.\n"),
2428 bfd_get_filename (core_bfd));
2429
08d72866 2430 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
2431}
2432
2433/* bfdcore_write -- write bytes into a core file section. */
2434
2435static inline void
2436bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2437{
2438 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2439
2440 if (ret)
2441 *offset += len;
2442 else
2443 error (_("Failed to write %d bytes to core file %s ('%s')."),
2444 len, bfd_get_filename (obfd),
2445 bfd_errmsg (bfd_get_error ()));
2446}
2447
2448/* Restore the execution log from a file. We use a modified elf
2449 corefile format, with an extra section for our data. */
2450
2451static void
88d1aa9d 2452cmd_record_full_restore (char *args, int from_tty)
d02ed0bb
MM
2453{
2454 core_file_command (args, from_tty);
88d1aa9d 2455 record_full_open (args, from_tty);
d02ed0bb
MM
2456}
2457
2458static void
88d1aa9d 2459record_full_save_cleanups (void *data)
d02ed0bb
MM
2460{
2461 bfd *obfd = data;
2462 char *pathname = xstrdup (bfd_get_filename (obfd));
2463
2464 gdb_bfd_unref (obfd);
2465 unlink (pathname);
2466 xfree (pathname);
2467}
2468
2469/* Save the execution log to a file. We use a modified elf corefile
2470 format, with an extra section for our data. */
2471
2472static void
1390f529 2473record_full_save (struct target_ops *self, const char *recfilename)
d02ed0bb 2474{
88d1aa9d 2475 struct record_full_entry *cur_record_full_list;
d02ed0bb
MM
2476 uint32_t magic;
2477 struct regcache *regcache;
2478 struct gdbarch *gdbarch;
2479 struct cleanup *old_cleanups;
2480 struct cleanup *set_cleanups;
2481 bfd *obfd;
2482 int save_size = 0;
2483 asection *osec = NULL;
2484 int bfd_offset = 0;
2485
2486 /* Open the save file. */
2487 if (record_debug)
2488 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2489 recfilename);
2490
2491 /* Open the output file. */
2492 obfd = create_gcore_bfd (recfilename);
88d1aa9d 2493 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
d02ed0bb 2494
88d1aa9d
MM
2495 /* Save the current record entry to "cur_record_full_list". */
2496 cur_record_full_list = record_full_list;
d02ed0bb
MM
2497
2498 /* Get the values of regcache and gdbarch. */
2499 regcache = get_current_regcache ();
2500 gdbarch = get_regcache_arch (regcache);
2501
2502 /* Disable the GDB operation record. */
25ea693b 2503 set_cleanups = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
2504
2505 /* Reverse execute to the begin of record list. */
2506 while (1)
2507 {
2508 /* Check for beginning and end of log. */
88d1aa9d 2509 if (record_full_list == &record_full_first)
d02ed0bb
MM
2510 break;
2511
88d1aa9d 2512 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2513
88d1aa9d
MM
2514 if (record_full_list->prev)
2515 record_full_list = record_full_list->prev;
d02ed0bb
MM
2516 }
2517
2518 /* Compute the size needed for the extra bfd section. */
2519 save_size = 4; /* magic cookie */
88d1aa9d
MM
2520 for (record_full_list = record_full_first.next; record_full_list;
2521 record_full_list = record_full_list->next)
2522 switch (record_full_list->type)
d02ed0bb 2523 {
88d1aa9d 2524 case record_full_end:
d02ed0bb
MM
2525 save_size += 1 + 4 + 4;
2526 break;
88d1aa9d
MM
2527 case record_full_reg:
2528 save_size += 1 + 4 + record_full_list->u.reg.len;
d02ed0bb 2529 break;
88d1aa9d
MM
2530 case record_full_mem:
2531 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
d02ed0bb
MM
2532 break;
2533 }
2534
2535 /* Make the new bfd section. */
2536 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2537 SEC_HAS_CONTENTS
2538 | SEC_READONLY);
2539 if (osec == NULL)
2540 error (_("Failed to create 'precord' section for corefile %s: %s"),
2541 recfilename,
2542 bfd_errmsg (bfd_get_error ()));
2543 bfd_set_section_size (obfd, osec, save_size);
2544 bfd_set_section_vma (obfd, osec, 0);
2545 bfd_set_section_alignment (obfd, osec, 0);
2546 bfd_section_lma (obfd, osec) = 0;
2547
2548 /* Save corefile state. */
2549 write_gcore_file (obfd);
2550
2551 /* Write out the record log. */
2552 /* Write the magic code. */
88d1aa9d 2553 magic = RECORD_FULL_FILE_MAGIC;
d02ed0bb
MM
2554 if (record_debug)
2555 fprintf_unfiltered (gdb_stdlog,
2556 " Writing 4-byte magic cookie "
88d1aa9d 2557 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb
MM
2558 phex_nz (magic, 4));
2559 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2560
2561 /* Save the entries to recfd and forward execute to the end of
2562 record list. */
88d1aa9d 2563 record_full_list = &record_full_first;
d02ed0bb
MM
2564 while (1)
2565 {
2566 /* Save entry. */
88d1aa9d 2567 if (record_full_list != &record_full_first)
d02ed0bb
MM
2568 {
2569 uint8_t type;
2570 uint32_t regnum, len, signal, count;
2571 uint64_t addr;
2572
88d1aa9d 2573 type = record_full_list->type;
d02ed0bb
MM
2574 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2575
88d1aa9d 2576 switch (record_full_list->type)
d02ed0bb 2577 {
88d1aa9d 2578 case record_full_reg: /* reg */
d02ed0bb
MM
2579 if (record_debug)
2580 fprintf_unfiltered (gdb_stdlog,
2581 " Writing register %d (1 "
2582 "plus %lu plus %d bytes)\n",
88d1aa9d 2583 record_full_list->u.reg.num,
d02ed0bb 2584 (unsigned long) sizeof (regnum),
88d1aa9d 2585 record_full_list->u.reg.len);
d02ed0bb
MM
2586
2587 /* Write regnum. */
88d1aa9d 2588 regnum = netorder32 (record_full_list->u.reg.num);
d02ed0bb
MM
2589 bfdcore_write (obfd, osec, &regnum,
2590 sizeof (regnum), &bfd_offset);
2591
2592 /* Write regval. */
88d1aa9d
MM
2593 bfdcore_write (obfd, osec,
2594 record_full_get_loc (record_full_list),
2595 record_full_list->u.reg.len, &bfd_offset);
d02ed0bb
MM
2596 break;
2597
88d1aa9d 2598 case record_full_mem: /* mem */
d02ed0bb
MM
2599 if (record_debug)
2600 fprintf_unfiltered (gdb_stdlog,
2601 " Writing memory %s (1 plus "
2602 "%lu plus %lu plus %d bytes)\n",
2603 paddress (gdbarch,
88d1aa9d 2604 record_full_list->u.mem.addr),
d02ed0bb
MM
2605 (unsigned long) sizeof (addr),
2606 (unsigned long) sizeof (len),
88d1aa9d 2607 record_full_list->u.mem.len);
d02ed0bb
MM
2608
2609 /* Write memlen. */
88d1aa9d 2610 len = netorder32 (record_full_list->u.mem.len);
d02ed0bb
MM
2611 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2612
2613 /* Write memaddr. */
88d1aa9d 2614 addr = netorder64 (record_full_list->u.mem.addr);
d02ed0bb
MM
2615 bfdcore_write (obfd, osec, &addr,
2616 sizeof (addr), &bfd_offset);
2617
2618 /* Write memval. */
88d1aa9d
MM
2619 bfdcore_write (obfd, osec,
2620 record_full_get_loc (record_full_list),
2621 record_full_list->u.mem.len, &bfd_offset);
d02ed0bb
MM
2622 break;
2623
88d1aa9d 2624 case record_full_end:
d02ed0bb
MM
2625 if (record_debug)
2626 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2627 " Writing record_full_end (1 + "
d02ed0bb
MM
2628 "%lu + %lu bytes)\n",
2629 (unsigned long) sizeof (signal),
2630 (unsigned long) sizeof (count));
2631 /* Write signal value. */
88d1aa9d 2632 signal = netorder32 (record_full_list->u.end.sigval);
d02ed0bb
MM
2633 bfdcore_write (obfd, osec, &signal,
2634 sizeof (signal), &bfd_offset);
2635
2636 /* Write insn count. */
88d1aa9d 2637 count = netorder32 (record_full_list->u.end.insn_num);
d02ed0bb
MM
2638 bfdcore_write (obfd, osec, &count,
2639 sizeof (count), &bfd_offset);
2640 break;
2641 }
2642 }
2643
2644 /* Execute entry. */
88d1aa9d 2645 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2646
88d1aa9d
MM
2647 if (record_full_list->next)
2648 record_full_list = record_full_list->next;
d02ed0bb
MM
2649 else
2650 break;
2651 }
2652
88d1aa9d 2653 /* Reverse execute to cur_record_full_list. */
d02ed0bb
MM
2654 while (1)
2655 {
2656 /* Check for beginning and end of log. */
88d1aa9d 2657 if (record_full_list == cur_record_full_list)
d02ed0bb
MM
2658 break;
2659
88d1aa9d 2660 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2661
88d1aa9d
MM
2662 if (record_full_list->prev)
2663 record_full_list = record_full_list->prev;
d02ed0bb
MM
2664 }
2665
2666 do_cleanups (set_cleanups);
2667 gdb_bfd_unref (obfd);
2668 discard_cleanups (old_cleanups);
2669
2670 /* Succeeded. */
2671 printf_filtered (_("Saved core file %s with execution log.\n"),
2672 recfilename);
2673}
2674
88d1aa9d 2675/* record_full_goto_insn -- rewind the record log (forward or backward,
d02ed0bb
MM
2676 depending on DIR) to the given entry, changing the program state
2677 correspondingly. */
2678
2679static void
88d1aa9d
MM
2680record_full_goto_insn (struct record_full_entry *entry,
2681 enum exec_direction_kind dir)
d02ed0bb 2682{
25ea693b 2683 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
2684 struct regcache *regcache = get_current_regcache ();
2685 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2686
2687 /* Assume everything is valid: we will hit the entry,
2688 and we will not hit the end of the recording. */
2689
2690 if (dir == EXEC_FORWARD)
88d1aa9d 2691 record_full_list = record_full_list->next;
d02ed0bb
MM
2692
2693 do
2694 {
88d1aa9d 2695 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2696 if (dir == EXEC_REVERSE)
88d1aa9d 2697 record_full_list = record_full_list->prev;
d02ed0bb 2698 else
88d1aa9d
MM
2699 record_full_list = record_full_list->next;
2700 } while (record_full_list != entry);
d02ed0bb
MM
2701 do_cleanups (set_cleanups);
2702}
2703
2704/* Alias for "target record-full". */
2705
2706static void
88d1aa9d 2707cmd_record_full_start (char *args, int from_tty)
d02ed0bb
MM
2708{
2709 execute_command ("target record-full", from_tty);
2710}
2711
2712static void
88d1aa9d
MM
2713set_record_full_insn_max_num (char *args, int from_tty,
2714 struct cmd_list_element *c)
d02ed0bb 2715{
7ee70bf5 2716 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2717 {
88d1aa9d
MM
2718 /* Count down record_full_insn_num while releasing records from list. */
2719 while (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2720 {
88d1aa9d
MM
2721 record_full_list_release_first ();
2722 record_full_insn_num--;
d02ed0bb
MM
2723 }
2724 }
2725}
2726
2727/* The "set record full" command. */
2728
2729static void
2730set_record_full_command (char *args, int from_tty)
2731{
2732 printf_unfiltered (_("\"set record full\" must be followed "
2733 "by an apporpriate subcommand.\n"));
2734 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2735 gdb_stdout);
2736}
2737
2738/* The "show record full" command. */
2739
2740static void
2741show_record_full_command (char *args, int from_tty)
2742{
2743 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2744}
2745
2746/* Provide a prototype to silence -Wmissing-prototypes. */
2747extern initialize_file_ftype _initialize_record_full;
2748
2749void
2750_initialize_record_full (void)
2751{
2752 struct cmd_list_element *c;
2753
88d1aa9d
MM
2754 /* Init record_full_first. */
2755 record_full_first.prev = NULL;
2756 record_full_first.next = NULL;
2757 record_full_first.type = record_full_end;
d02ed0bb 2758
88d1aa9d
MM
2759 init_record_full_ops ();
2760 add_target (&record_full_ops);
2761 add_deprecated_target_alias (&record_full_ops, "record");
2762 init_record_full_core_ops ();
2763 add_target (&record_full_core_ops);
d02ed0bb 2764
88d1aa9d 2765 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
d02ed0bb
MM
2766 _("Start full execution recording."), &record_full_cmdlist,
2767 "record full ", 0, &record_cmdlist);
2768
88d1aa9d 2769 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
d02ed0bb
MM
2770 _("Restore the execution log from a file.\n\
2771Argument is filename. File must be created with 'record save'."),
2772 &record_full_cmdlist);
2773 set_cmd_completer (c, filename_completer);
2774
2775 /* Deprecate the old version without "full" prefix. */
2776 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2777 &record_cmdlist);
2778 set_cmd_completer (c, filename_completer);
2779 deprecate_cmd (c, "record full restore");
2780
2781 add_prefix_cmd ("full", class_support, set_record_full_command,
2782 _("Set record options"), &set_record_full_cmdlist,
2783 "set record full ", 0, &set_record_cmdlist);
2784
2785 add_prefix_cmd ("full", class_support, show_record_full_command,
2786 _("Show record options"), &show_record_full_cmdlist,
2787 "show record full ", 0, &show_record_cmdlist);
2788
2789 /* Record instructions number limit command. */
2790 add_setshow_boolean_cmd ("stop-at-limit", no_class,
88d1aa9d 2791 &record_full_stop_at_limit, _("\
d02ed0bb
MM
2792Set whether record/replay stops when record/replay buffer becomes full."), _("\
2793Show whether record/replay stops when record/replay buffer becomes full."),
2794 _("Default is ON.\n\
2795When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2796When OFF, if the record/replay buffer becomes full,\n\
2797delete the oldest recorded instruction to make room for each new one."),
2798 NULL, NULL,
2799 &set_record_full_cmdlist, &show_record_full_cmdlist);
2800
2801 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2802 &set_record_cmdlist);
2803 deprecate_cmd (c, "set record full stop-at-limit");
2804
2805 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2806 &show_record_cmdlist);
2807 deprecate_cmd (c, "show record full stop-at-limit");
2808
88d1aa9d
MM
2809 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2810 &record_full_insn_max_num,
d02ed0bb
MM
2811 _("Set record/replay buffer limit."),
2812 _("Show record/replay buffer limit."), _("\
2813Set the maximum number of instructions to be stored in the\n\
f81d1120
PA
2814record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2815limit. Default is 200000."),
88d1aa9d 2816 set_record_full_insn_max_num,
d02ed0bb
MM
2817 NULL, &set_record_full_cmdlist,
2818 &show_record_full_cmdlist);
2819
2820 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2821 &set_record_cmdlist);
2822 deprecate_cmd (c, "set record full insn-number-max");
2823
2824 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2825 &show_record_cmdlist);
2826 deprecate_cmd (c, "show record full insn-number-max");
2827
88d1aa9d 2828 add_setshow_boolean_cmd ("memory-query", no_class,
25ea693b 2829 &record_full_memory_query, _("\
d02ed0bb
MM
2830Set whether query if PREC cannot record memory change of next instruction."),
2831 _("\
2832Show whether query if PREC cannot record memory change of next instruction."),
2833 _("\
2834Default is OFF.\n\
2835When ON, query if PREC cannot record memory change of next instruction."),
2836 NULL, NULL,
88d1aa9d
MM
2837 &set_record_full_cmdlist,
2838 &show_record_full_cmdlist);
d02ed0bb
MM
2839
2840 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2841 &set_record_cmdlist);
2842 deprecate_cmd (c, "set record full memory-query");
2843
2844 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2845 &show_record_cmdlist);
2846 deprecate_cmd (c, "show record full memory-query");
2847}
This page took 0.294028 seconds and 4 git commands to generate.