1 /* Process record and replay target for GDB, the GNU debugger. 2 3 Copyright (C) 2008, 2009 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "gdbcmd.h" 22 #include "regcache.h" 23 #include "gdbthread.h" 24 #include "event-top.h" 25 #include "exceptions.h" 26 #include "record.h" 27 28 #include <signal.h> 29 30 #define DEFAULT_RECORD_INSN_MAX_NUM 200000 31 32 #define RECORD_IS_REPLAY \ 33 (record_list->next || execution_direction == EXEC_REVERSE) 34 35 /* These are the core structs of the process record functionality. 36 37 A record_entry is a record of the value change of a register 38 ("record_reg") or a part of memory ("record_mem"). And each 39 instruction must have a struct record_entry ("record_end") that 40 indicates that this is the last struct record_entry of this 41 instruction. 42 43 Each struct record_entry is linked to "record_list" by "prev" and 44 "next" pointers. */ 45 46 struct record_reg_entry 47 { 48 int num; 49 gdb_byte *val; 50 }; 51 52 struct record_mem_entry 53 { 54 CORE_ADDR addr; 55 int len; 56 /* Set this flag if target memory for this entry 57 can no longer be accessed. */ 58 int mem_entry_not_accessible; 59 gdb_byte *val; 60 }; 61 62 struct record_end_entry 63 { 64 enum target_signal sigval; 65 }; 66 67 enum record_type 68 { 69 record_end = 0, 70 record_reg, 71 record_mem 72 }; 73 74 struct record_entry 75 { 76 struct record_entry *prev; 77 struct record_entry *next; 78 enum record_type type; 79 union 80 { 81 /* reg */ 82 struct record_reg_entry reg; 83 /* mem */ 84 struct record_mem_entry mem; 85 /* end */ 86 struct record_end_entry end; 87 } u; 88 }; 89 90 /* This is the debug switch for process record. */ 91 int record_debug = 0; 92 93 /* These list is for execution log. */ 94 static struct record_entry record_first; 95 static struct record_entry *record_list = &record_first; 96 static struct record_entry *record_arch_list_head = NULL; 97 static struct record_entry *record_arch_list_tail = NULL; 98 99 /* 1 ask user. 0 auto delete the last struct record_entry. */ 100 static int record_stop_at_limit = 1; 101 static int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM; 102 static int record_insn_num = 0; 103 104 /* The target_ops of process record. */ 105 static struct target_ops record_ops; 106 107 /* The beneath function pointers. */ 108 static struct target_ops *record_beneath_to_resume_ops; 109 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int, 110 enum target_signal); 111 static struct target_ops *record_beneath_to_wait_ops; 112 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t, 113 struct target_waitstatus *, 114 int); 115 static struct target_ops *record_beneath_to_store_registers_ops; 116 static void (*record_beneath_to_store_registers) (struct target_ops *, 117 struct regcache *, 118 int regno); 119 static struct target_ops *record_beneath_to_xfer_partial_ops; 120 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops, 121 enum target_object object, 122 const char *annex, 123 gdb_byte *readbuf, 124 const gdb_byte *writebuf, 125 ULONGEST offset, 126 LONGEST len); 127 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *, 128 struct bp_target_info *); 129 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *, 130 struct bp_target_info *); 131 132 static void 133 record_list_release (struct record_entry *rec) 134 { 135 struct record_entry *tmp; 136 137 if (!rec) 138 return; 139 140 while (rec->next) 141 { 142 rec = rec->next; 143 } 144 145 while (rec->prev) 146 { 147 tmp = rec; 148 rec = rec->prev; 149 if (tmp->type == record_reg) 150 xfree (tmp->u.reg.val); 151 else if (tmp->type == record_mem) 152 xfree (tmp->u.mem.val); 153 xfree (tmp); 154 } 155 156 if (rec != &record_first) 157 xfree (rec); 158 } 159 160 static void 161 record_list_release_next (void) 162 { 163 struct record_entry *rec = record_list; 164 struct record_entry *tmp = rec->next; 165 rec->next = NULL; 166 while (tmp) 167 { 168 rec = tmp->next; 169 if (tmp->type == record_end) 170 record_insn_num--; 171 else if (tmp->type == record_reg) 172 xfree (tmp->u.reg.val); 173 else if (tmp->type == record_mem) 174 xfree (tmp->u.mem.val); 175 xfree (tmp); 176 tmp = rec; 177 } 178 } 179 180 static void 181 record_list_release_first (void) 182 { 183 struct record_entry *tmp = NULL; 184 enum record_type type; 185 186 if (!record_first.next) 187 return; 188 189 while (1) 190 { 191 type = record_first.next->type; 192 193 if (type == record_reg) 194 xfree (record_first.next->u.reg.val); 195 else if (type == record_mem) 196 xfree (record_first.next->u.mem.val); 197 tmp = record_first.next; 198 record_first.next = tmp->next; 199 xfree (tmp); 200 201 if (!record_first.next) 202 { 203 gdb_assert (record_insn_num == 1); 204 break; 205 } 206 207 record_first.next->prev = &record_first; 208 209 if (type == record_end) 210 break; 211 } 212 213 record_insn_num--; 214 } 215 216 /* Add a struct record_entry to record_arch_list. */ 217 218 static void 219 record_arch_list_add (struct record_entry *rec) 220 { 221 if (record_debug > 1) 222 fprintf_unfiltered (gdb_stdlog, 223 "Process record: record_arch_list_add %s.\n", 224 host_address_to_string (rec)); 225 226 if (record_arch_list_tail) 227 { 228 record_arch_list_tail->next = rec; 229 rec->prev = record_arch_list_tail; 230 record_arch_list_tail = rec; 231 } 232 else 233 { 234 record_arch_list_head = rec; 235 record_arch_list_tail = rec; 236 } 237 } 238 239 /* Record the value of a register NUM to record_arch_list. */ 240 241 int 242 record_arch_list_add_reg (struct regcache *regcache, int num) 243 { 244 struct record_entry *rec; 245 246 if (record_debug > 1) 247 fprintf_unfiltered (gdb_stdlog, 248 "Process record: add register num = %d to " 249 "record list.\n", 250 num); 251 252 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry)); 253 rec->u.reg.val = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE); 254 rec->prev = NULL; 255 rec->next = NULL; 256 rec->type = record_reg; 257 rec->u.reg.num = num; 258 259 regcache_raw_read (regcache, num, rec->u.reg.val); 260 261 record_arch_list_add (rec); 262 263 return 0; 264 } 265 266 /* Record the value of a region of memory whose address is ADDR and 267 length is LEN to record_arch_list. */ 268 269 int 270 record_arch_list_add_mem (CORE_ADDR addr, int len) 271 { 272 struct record_entry *rec; 273 274 if (record_debug > 1) 275 fprintf_unfiltered (gdb_stdlog, 276 "Process record: add mem addr = %s len = %d to " 277 "record list.\n", 278 paddress (target_gdbarch, addr), len); 279 280 if (!addr) 281 return 0; 282 283 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry)); 284 rec->u.mem.val = (gdb_byte *) xmalloc (len); 285 rec->prev = NULL; 286 rec->next = NULL; 287 rec->type = record_mem; 288 rec->u.mem.addr = addr; 289 rec->u.mem.len = len; 290 rec->u.mem.mem_entry_not_accessible = 0; 291 292 if (target_read_memory (addr, rec->u.mem.val, len)) 293 { 294 if (record_debug) 295 fprintf_unfiltered (gdb_stdlog, 296 "Process record: error reading memory at " 297 "addr = %s len = %d.\n", 298 paddress (target_gdbarch, addr), len); 299 xfree (rec->u.mem.val); 300 xfree (rec); 301 return -1; 302 } 303 304 record_arch_list_add (rec); 305 306 return 0; 307 } 308 309 /* Add a record_end type struct record_entry to record_arch_list. */ 310 311 int 312 record_arch_list_add_end (void) 313 { 314 struct record_entry *rec; 315 316 if (record_debug > 1) 317 fprintf_unfiltered (gdb_stdlog, 318 "Process record: add end to arch list.\n"); 319 320 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry)); 321 rec->prev = NULL; 322 rec->next = NULL; 323 rec->type = record_end; 324 rec->u.end.sigval = TARGET_SIGNAL_0; 325 326 record_arch_list_add (rec); 327 328 return 0; 329 } 330 331 static void 332 record_check_insn_num (int set_terminal) 333 { 334 if (record_insn_max_num) 335 { 336 gdb_assert (record_insn_num <= record_insn_max_num); 337 if (record_insn_num == record_insn_max_num) 338 { 339 /* Ask user what to do. */ 340 if (record_stop_at_limit) 341 { 342 int q; 343 if (set_terminal) 344 target_terminal_ours (); 345 q = yquery (_("Do you want to auto delete previous execution " 346 "log entries when record/replay buffer becomes " 347 "full (record stop-at-limit)?")); 348 if (set_terminal) 349 target_terminal_inferior (); 350 if (q) 351 record_stop_at_limit = 0; 352 else 353 error (_("Process record: inferior program stopped.")); 354 } 355 } 356 } 357 } 358 359 /* Before inferior step (when GDB record the running message, inferior 360 only can step), GDB will call this function to record the values to 361 record_list. This function will call gdbarch_process_record to 362 record the running message of inferior and set them to 363 record_arch_list, and add it to record_list. */ 364 365 static void 366 record_message_cleanups (void *ignore) 367 { 368 record_list_release (record_arch_list_tail); 369 } 370 371 struct record_message_args { 372 struct regcache *regcache; 373 enum target_signal signal; 374 }; 375 376 static int 377 record_message (void *args) 378 { 379 int ret; 380 struct record_message_args *myargs = args; 381 struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache); 382 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0); 383 384 record_arch_list_head = NULL; 385 record_arch_list_tail = NULL; 386 387 /* Check record_insn_num. */ 388 record_check_insn_num (1); 389 390 /* If gdb sends a signal value to target_resume, 391 save it in the 'end' field of the previous instruction. 392 393 Maybe process record should record what really happened, 394 rather than what gdb pretends has happened. 395 396 So if Linux delivered the signal to the child process during 397 the record mode, we will record it and deliver it again in 398 the replay mode. 399 400 If user says "ignore this signal" during the record mode, then 401 it will be ignored again during the replay mode (no matter if 402 the user says something different, like "deliver this signal" 403 during the replay mode). 404 405 User should understand that nothing he does during the replay 406 mode will change the behavior of the child. If he tries, 407 then that is a user error. 408 409 But we should still deliver the signal to gdb during the replay, 410 if we delivered it during the recording. Therefore we should 411 record the signal during record_wait, not record_resume. */ 412 if (record_list != &record_first) /* FIXME better way to check */ 413 { 414 gdb_assert (record_list->type == record_end); 415 record_list->u.end.sigval = myargs->signal; 416 } 417 418 if (myargs->signal == TARGET_SIGNAL_0 419 || !gdbarch_process_record_signal_p (gdbarch)) 420 ret = gdbarch_process_record (gdbarch, 421 myargs->regcache, 422 regcache_read_pc (myargs->regcache)); 423 else 424 ret = gdbarch_process_record_signal (gdbarch, 425 myargs->regcache, 426 myargs->signal); 427 428 if (ret > 0) 429 error (_("Process record: inferior program stopped.")); 430 if (ret < 0) 431 error (_("Process record: failed to record execution log.")); 432 433 discard_cleanups (old_cleanups); 434 435 record_list->next = record_arch_list_head; 436 record_arch_list_head->prev = record_list; 437 record_list = record_arch_list_tail; 438 439 if (record_insn_num == record_insn_max_num && record_insn_max_num) 440 record_list_release_first (); 441 else 442 record_insn_num++; 443 444 return 1; 445 } 446 447 static int 448 do_record_message (struct regcache *regcache, 449 enum target_signal signal) 450 { 451 struct record_message_args args; 452 453 args.regcache = regcache; 454 args.signal = signal; 455 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL); 456 } 457 458 /* Set to 1 if record_store_registers and record_xfer_partial 459 doesn't need record. */ 460 461 static int record_gdb_operation_disable = 0; 462 463 struct cleanup * 464 record_gdb_operation_disable_set (void) 465 { 466 struct cleanup *old_cleanups = NULL; 467 468 old_cleanups = 469 make_cleanup_restore_integer (&record_gdb_operation_disable); 470 record_gdb_operation_disable = 1; 471 472 return old_cleanups; 473 } 474 475 static void 476 record_open (char *name, int from_tty) 477 { 478 struct target_ops *t; 479 480 if (record_debug) 481 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n"); 482 483 /* check exec */ 484 if (!target_has_execution) 485 error (_("Process record: the program is not being run.")); 486 if (non_stop) 487 error (_("Process record target can't debug inferior in non-stop mode " 488 "(non-stop).")); 489 if (target_async_permitted) 490 error (_("Process record target can't debug inferior in asynchronous " 491 "mode (target-async).")); 492 493 if (!gdbarch_process_record_p (target_gdbarch)) 494 error (_("Process record: the current architecture doesn't support " 495 "record function.")); 496 497 /* Check if record target is already running. */ 498 if (current_target.to_stratum == record_stratum) 499 error (_("Process record target already running. Use \"record stop\" to " 500 "stop record target first.")); 501 502 /*Reset the beneath function pointers. */ 503 record_beneath_to_resume = NULL; 504 record_beneath_to_wait = NULL; 505 record_beneath_to_store_registers = NULL; 506 record_beneath_to_xfer_partial = NULL; 507 record_beneath_to_insert_breakpoint = NULL; 508 record_beneath_to_remove_breakpoint = NULL; 509 510 /* Set the beneath function pointers. */ 511 for (t = current_target.beneath; t != NULL; t = t->beneath) 512 { 513 if (!record_beneath_to_resume) 514 { 515 record_beneath_to_resume = t->to_resume; 516 record_beneath_to_resume_ops = t; 517 } 518 if (!record_beneath_to_wait) 519 { 520 record_beneath_to_wait = t->to_wait; 521 record_beneath_to_wait_ops = t; 522 } 523 if (!record_beneath_to_store_registers) 524 { 525 record_beneath_to_store_registers = t->to_store_registers; 526 record_beneath_to_store_registers_ops = t; 527 } 528 if (!record_beneath_to_xfer_partial) 529 { 530 record_beneath_to_xfer_partial = t->to_xfer_partial; 531 record_beneath_to_xfer_partial_ops = t; 532 } 533 if (!record_beneath_to_insert_breakpoint) 534 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint; 535 if (!record_beneath_to_remove_breakpoint) 536 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint; 537 } 538 if (!record_beneath_to_resume) 539 error (_("Process record can't get to_resume.")); 540 if (!record_beneath_to_wait) 541 error (_("Process record can't get to_wait.")); 542 if (!record_beneath_to_store_registers) 543 error (_("Process record can't get to_store_registers.")); 544 if (!record_beneath_to_xfer_partial) 545 error (_("Process record can't get to_xfer_partial.")); 546 if (!record_beneath_to_insert_breakpoint) 547 error (_("Process record can't get to_insert_breakpoint.")); 548 if (!record_beneath_to_remove_breakpoint) 549 error (_("Process record can't get to_remove_breakpoint.")); 550 551 push_target (&record_ops); 552 553 /* Reset */ 554 record_insn_num = 0; 555 record_list = &record_first; 556 record_list->next = NULL; 557 } 558 559 static void 560 record_close (int quitting) 561 { 562 if (record_debug) 563 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n"); 564 565 record_list_release (record_list); 566 } 567 568 static int record_resume_step = 0; 569 static int record_resume_error; 570 571 static void 572 record_resume (struct target_ops *ops, ptid_t ptid, int step, 573 enum target_signal signal) 574 { 575 record_resume_step = step; 576 577 if (!RECORD_IS_REPLAY) 578 { 579 if (do_record_message (get_current_regcache (), signal)) 580 { 581 record_resume_error = 0; 582 } 583 else 584 { 585 record_resume_error = 1; 586 return; 587 } 588 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1, 589 signal); 590 } 591 } 592 593 static int record_get_sig = 0; 594 595 static void 596 record_sig_handler (int signo) 597 { 598 if (record_debug) 599 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n"); 600 601 /* It will break the running inferior in replay mode. */ 602 record_resume_step = 1; 603 604 /* It will let record_wait set inferior status to get the signal 605 SIGINT. */ 606 record_get_sig = 1; 607 } 608 609 static void 610 record_wait_cleanups (void *ignore) 611 { 612 if (execution_direction == EXEC_REVERSE) 613 { 614 if (record_list->next) 615 record_list = record_list->next; 616 } 617 else 618 record_list = record_list->prev; 619 } 620 621 /* In replay mode, this function examines the recorded log and 622 determines where to stop. */ 623 624 static ptid_t 625 record_wait (struct target_ops *ops, 626 ptid_t ptid, struct target_waitstatus *status, 627 int options) 628 { 629 struct cleanup *set_cleanups = record_gdb_operation_disable_set (); 630 631 if (record_debug) 632 fprintf_unfiltered (gdb_stdlog, 633 "Process record: record_wait " 634 "record_resume_step = %d\n", 635 record_resume_step); 636 637 if (!RECORD_IS_REPLAY) 638 { 639 if (record_resume_error) 640 { 641 /* If record_resume get error, return directly. */ 642 status->kind = TARGET_WAITKIND_STOPPED; 643 status->value.sig = TARGET_SIGNAL_ABRT; 644 return inferior_ptid; 645 } 646 647 if (record_resume_step) 648 { 649 /* This is a single step. */ 650 return record_beneath_to_wait (record_beneath_to_wait_ops, 651 ptid, status, options); 652 } 653 else 654 { 655 /* This is not a single step. */ 656 ptid_t ret; 657 CORE_ADDR tmp_pc; 658 659 while (1) 660 { 661 ret = record_beneath_to_wait (record_beneath_to_wait_ops, 662 ptid, status, options); 663 664 /* Is this a SIGTRAP? */ 665 if (status->kind == TARGET_WAITKIND_STOPPED 666 && status->value.sig == TARGET_SIGNAL_TRAP) 667 { 668 /* Yes -- check if there is a breakpoint. */ 669 registers_changed (); 670 tmp_pc = regcache_read_pc (get_current_regcache ()); 671 if (breakpoint_inserted_here_p (tmp_pc)) 672 { 673 /* There is a breakpoint. GDB will want to stop. */ 674 CORE_ADDR decr_pc_after_break = 675 gdbarch_decr_pc_after_break 676 (get_regcache_arch (get_current_regcache ())); 677 if (decr_pc_after_break) 678 { 679 regcache_write_pc (get_thread_regcache (ret), 680 tmp_pc + decr_pc_after_break); 681 } 682 } 683 else 684 { 685 /* There is not a breakpoint, and gdb is not 686 stepping, therefore gdb will not stop. 687 Therefore we will not return to gdb. 688 Record the insn and resume. */ 689 if (!do_record_message (get_current_regcache (), 690 TARGET_SIGNAL_0)) 691 { 692 break; 693 } 694 record_beneath_to_resume (record_beneath_to_resume_ops, 695 ptid, 1, 696 TARGET_SIGNAL_0); 697 continue; 698 } 699 } 700 701 /* The inferior is broken by a breakpoint or a signal. */ 702 break; 703 } 704 705 return ret; 706 } 707 } 708 else 709 { 710 struct regcache *regcache = get_current_regcache (); 711 struct gdbarch *gdbarch = get_regcache_arch (regcache); 712 int continue_flag = 1; 713 int first_record_end = 1; 714 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0); 715 CORE_ADDR tmp_pc; 716 717 status->kind = TARGET_WAITKIND_STOPPED; 718 719 /* Check breakpoint when forward execute. */ 720 if (execution_direction == EXEC_FORWARD) 721 { 722 tmp_pc = regcache_read_pc (regcache); 723 if (breakpoint_inserted_here_p (tmp_pc)) 724 { 725 if (record_debug) 726 fprintf_unfiltered (gdb_stdlog, 727 "Process record: break at %s.\n", 728 paddress (gdbarch, tmp_pc)); 729 if (gdbarch_decr_pc_after_break (gdbarch) 730 && !record_resume_step) 731 regcache_write_pc (regcache, 732 tmp_pc + 733 gdbarch_decr_pc_after_break (gdbarch)); 734 goto replay_out; 735 } 736 } 737 738 record_get_sig = 0; 739 signal (SIGINT, record_sig_handler); 740 /* If GDB is in terminal_inferior mode, it will not get the signal. 741 And in GDB replay mode, GDB doesn't need to be in terminal_inferior 742 mode, because inferior will not executed. 743 Then set it to terminal_ours to make GDB get the signal. */ 744 target_terminal_ours (); 745 746 /* In EXEC_FORWARD mode, record_list points to the tail of prev 747 instruction. */ 748 if (execution_direction == EXEC_FORWARD && record_list->next) 749 record_list = record_list->next; 750 751 /* Loop over the record_list, looking for the next place to 752 stop. */ 753 do 754 { 755 /* Check for beginning and end of log. */ 756 if (execution_direction == EXEC_REVERSE 757 && record_list == &record_first) 758 { 759 /* Hit beginning of record log in reverse. */ 760 status->kind = TARGET_WAITKIND_NO_HISTORY; 761 break; 762 } 763 if (execution_direction != EXEC_REVERSE && !record_list->next) 764 { 765 /* Hit end of record log going forward. */ 766 status->kind = TARGET_WAITKIND_NO_HISTORY; 767 break; 768 } 769 770 /* Set ptid, register and memory according to record_list. */ 771 if (record_list->type == record_reg) 772 { 773 /* reg */ 774 gdb_byte reg[MAX_REGISTER_SIZE]; 775 if (record_debug > 1) 776 fprintf_unfiltered (gdb_stdlog, 777 "Process record: record_reg %s to " 778 "inferior num = %d.\n", 779 host_address_to_string (record_list), 780 record_list->u.reg.num); 781 regcache_cooked_read (regcache, record_list->u.reg.num, reg); 782 regcache_cooked_write (regcache, record_list->u.reg.num, 783 record_list->u.reg.val); 784 memcpy (record_list->u.reg.val, reg, MAX_REGISTER_SIZE); 785 } 786 else if (record_list->type == record_mem) 787 { 788 /* mem */ 789 /* Nothing to do if the entry is flagged not_accessible. */ 790 if (!record_list->u.mem.mem_entry_not_accessible) 791 { 792 gdb_byte *mem = alloca (record_list->u.mem.len); 793 if (record_debug > 1) 794 fprintf_unfiltered (gdb_stdlog, 795 "Process record: record_mem %s to " 796 "inferior addr = %s len = %d.\n", 797 host_address_to_string (record_list), 798 paddress (gdbarch, 799 record_list->u.mem.addr), 800 record_list->u.mem.len); 801 802 if (target_read_memory (record_list->u.mem.addr, mem, 803 record_list->u.mem.len)) 804 { 805 if (execution_direction != EXEC_REVERSE) 806 error (_("Process record: error reading memory at " 807 "addr = %s len = %d."), 808 paddress (gdbarch, record_list->u.mem.addr), 809 record_list->u.mem.len); 810 else 811 /* Read failed -- 812 flag entry as not_accessible. */ 813 record_list->u.mem.mem_entry_not_accessible = 1; 814 } 815 else 816 { 817 if (target_write_memory (record_list->u.mem.addr, 818 record_list->u.mem.val, 819 record_list->u.mem.len)) 820 { 821 if (execution_direction != EXEC_REVERSE) 822 error (_("Process record: error writing memory at " 823 "addr = %s len = %d."), 824 paddress (gdbarch, record_list->u.mem.addr), 825 record_list->u.mem.len); 826 else 827 /* Write failed -- 828 flag entry as not_accessible. */ 829 record_list->u.mem.mem_entry_not_accessible = 1; 830 } 831 else 832 { 833 memcpy (record_list->u.mem.val, mem, 834 record_list->u.mem.len); 835 } 836 } 837 } 838 } 839 else 840 { 841 if (record_debug > 1) 842 fprintf_unfiltered (gdb_stdlog, 843 "Process record: record_end %s to " 844 "inferior.\n", 845 host_address_to_string (record_list)); 846 847 if (first_record_end && execution_direction == EXEC_REVERSE) 848 { 849 /* When reverse excute, the first record_end is the part of 850 current instruction. */ 851 first_record_end = 0; 852 } 853 else 854 { 855 /* In EXEC_REVERSE mode, this is the record_end of prev 856 instruction. 857 In EXEC_FORWARD mode, this is the record_end of current 858 instruction. */ 859 /* step */ 860 if (record_resume_step) 861 { 862 if (record_debug > 1) 863 fprintf_unfiltered (gdb_stdlog, 864 "Process record: step.\n"); 865 continue_flag = 0; 866 } 867 868 /* check breakpoint */ 869 tmp_pc = regcache_read_pc (regcache); 870 if (breakpoint_inserted_here_p (tmp_pc)) 871 { 872 if (record_debug) 873 fprintf_unfiltered (gdb_stdlog, 874 "Process record: break " 875 "at %s.\n", 876 paddress (gdbarch, tmp_pc)); 877 if (gdbarch_decr_pc_after_break (gdbarch) 878 && execution_direction == EXEC_FORWARD 879 && !record_resume_step) 880 regcache_write_pc (regcache, 881 tmp_pc + 882 gdbarch_decr_pc_after_break (gdbarch)); 883 continue_flag = 0; 884 } 885 /* Check target signal */ 886 if (record_list->u.end.sigval != TARGET_SIGNAL_0) 887 /* FIXME: better way to check */ 888 continue_flag = 0; 889 } 890 } 891 892 if (continue_flag) 893 { 894 if (execution_direction == EXEC_REVERSE) 895 { 896 if (record_list->prev) 897 record_list = record_list->prev; 898 } 899 else 900 { 901 if (record_list->next) 902 record_list = record_list->next; 903 } 904 } 905 } 906 while (continue_flag); 907 908 signal (SIGINT, handle_sigint); 909 910 replay_out: 911 if (record_get_sig) 912 status->value.sig = TARGET_SIGNAL_INT; 913 else if (record_list->u.end.sigval != TARGET_SIGNAL_0) 914 /* FIXME: better way to check */ 915 status->value.sig = record_list->u.end.sigval; 916 else 917 status->value.sig = TARGET_SIGNAL_TRAP; 918 919 discard_cleanups (old_cleanups); 920 } 921 922 do_cleanups (set_cleanups); 923 return inferior_ptid; 924 } 925 926 static void 927 record_disconnect (struct target_ops *target, char *args, int from_tty) 928 { 929 if (record_debug) 930 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n"); 931 932 unpush_target (&record_ops); 933 target_disconnect (args, from_tty); 934 } 935 936 static void 937 record_detach (struct target_ops *ops, char *args, int from_tty) 938 { 939 if (record_debug) 940 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n"); 941 942 unpush_target (&record_ops); 943 target_detach (args, from_tty); 944 } 945 946 static void 947 record_mourn_inferior (struct target_ops *ops) 948 { 949 if (record_debug) 950 fprintf_unfiltered (gdb_stdlog, "Process record: " 951 "record_mourn_inferior\n"); 952 953 unpush_target (&record_ops); 954 target_mourn_inferior (); 955 } 956 957 /* Close process record target before killing the inferior process. */ 958 959 static void 960 record_kill (struct target_ops *ops) 961 { 962 if (record_debug) 963 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n"); 964 965 unpush_target (&record_ops); 966 target_kill (); 967 } 968 969 /* Record registers change (by user or by GDB) to list as an instruction. */ 970 971 static void 972 record_registers_change (struct regcache *regcache, int regnum) 973 { 974 /* Check record_insn_num. */ 975 record_check_insn_num (0); 976 977 record_arch_list_head = NULL; 978 record_arch_list_tail = NULL; 979 980 if (regnum < 0) 981 { 982 int i; 983 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) 984 { 985 if (record_arch_list_add_reg (regcache, i)) 986 { 987 record_list_release (record_arch_list_tail); 988 error (_("Process record: failed to record execution log.")); 989 } 990 } 991 } 992 else 993 { 994 if (record_arch_list_add_reg (regcache, regnum)) 995 { 996 record_list_release (record_arch_list_tail); 997 error (_("Process record: failed to record execution log.")); 998 } 999 } 1000 if (record_arch_list_add_end ()) 1001 { 1002 record_list_release (record_arch_list_tail); 1003 error (_("Process record: failed to record execution log.")); 1004 } 1005 record_list->next = record_arch_list_head; 1006 record_arch_list_head->prev = record_list; 1007 record_list = record_arch_list_tail; 1008 1009 if (record_insn_num == record_insn_max_num && record_insn_max_num) 1010 record_list_release_first (); 1011 else 1012 record_insn_num++; 1013 } 1014 1015 static void 1016 record_store_registers (struct target_ops *ops, struct regcache *regcache, 1017 int regno) 1018 { 1019 if (!record_gdb_operation_disable) 1020 { 1021 if (RECORD_IS_REPLAY) 1022 { 1023 int n; 1024 1025 /* Let user choose if he wants to write register or not. */ 1026 if (regno < 0) 1027 n = 1028 query (_("Because GDB is in replay mode, changing the " 1029 "value of a register will make the execution " 1030 "log unusable from this point onward. " 1031 "Change all registers?")); 1032 else 1033 n = 1034 query (_("Because GDB is in replay mode, changing the value " 1035 "of a register will make the execution log unusable " 1036 "from this point onward. Change register %s?"), 1037 gdbarch_register_name (get_regcache_arch (regcache), 1038 regno)); 1039 1040 if (!n) 1041 { 1042 /* Invalidate the value of regcache that was set in function 1043 "regcache_raw_write". */ 1044 if (regno < 0) 1045 { 1046 int i; 1047 for (i = 0; 1048 i < gdbarch_num_regs (get_regcache_arch (regcache)); 1049 i++) 1050 regcache_invalidate (regcache, i); 1051 } 1052 else 1053 regcache_invalidate (regcache, regno); 1054 1055 error (_("Process record canceled the operation.")); 1056 } 1057 1058 /* Destroy the record from here forward. */ 1059 record_list_release_next (); 1060 } 1061 1062 record_registers_change (regcache, regno); 1063 } 1064 record_beneath_to_store_registers (record_beneath_to_store_registers_ops, 1065 regcache, regno); 1066 } 1067 1068 /* Behavior is conditional on RECORD_IS_REPLAY. 1069 In replay mode, we cannot write memory unles we are willing to 1070 invalidate the record/replay log from this point forward. */ 1071 1072 static LONGEST 1073 record_xfer_partial (struct target_ops *ops, enum target_object object, 1074 const char *annex, gdb_byte *readbuf, 1075 const gdb_byte *writebuf, ULONGEST offset, LONGEST len) 1076 { 1077 if (!record_gdb_operation_disable 1078 && (object == TARGET_OBJECT_MEMORY 1079 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf) 1080 { 1081 if (RECORD_IS_REPLAY) 1082 { 1083 /* Let user choose if he wants to write memory or not. */ 1084 if (!query (_("Because GDB is in replay mode, writing to memory " 1085 "will make the execution log unusable from this " 1086 "point onward. Write memory at address %s?"), 1087 paddress (target_gdbarch, offset))) 1088 error (_("Process record canceled the operation.")); 1089 1090 /* Destroy the record from here forward. */ 1091 record_list_release_next (); 1092 } 1093 1094 /* Check record_insn_num */ 1095 record_check_insn_num (0); 1096 1097 /* Record registers change to list as an instruction. */ 1098 record_arch_list_head = NULL; 1099 record_arch_list_tail = NULL; 1100 if (record_arch_list_add_mem (offset, len)) 1101 { 1102 record_list_release (record_arch_list_tail); 1103 if (record_debug) 1104 fprintf_unfiltered (gdb_stdlog, 1105 _("Process record: failed to record " 1106 "execution log.")); 1107 return -1; 1108 } 1109 if (record_arch_list_add_end ()) 1110 { 1111 record_list_release (record_arch_list_tail); 1112 if (record_debug) 1113 fprintf_unfiltered (gdb_stdlog, 1114 _("Process record: failed to record " 1115 "execution log.")); 1116 return -1; 1117 } 1118 record_list->next = record_arch_list_head; 1119 record_arch_list_head->prev = record_list; 1120 record_list = record_arch_list_tail; 1121 1122 if (record_insn_num == record_insn_max_num && record_insn_max_num) 1123 record_list_release_first (); 1124 else 1125 record_insn_num++; 1126 } 1127 1128 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops, 1129 object, annex, readbuf, writebuf, 1130 offset, len); 1131 } 1132 1133 /* Behavior is conditional on RECORD_IS_REPLAY. 1134 We will not actually insert or remove breakpoints when replaying, 1135 nor when recording. */ 1136 1137 static int 1138 record_insert_breakpoint (struct gdbarch *gdbarch, 1139 struct bp_target_info *bp_tgt) 1140 { 1141 if (!RECORD_IS_REPLAY) 1142 { 1143 struct cleanup *old_cleanups = record_gdb_operation_disable_set (); 1144 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt); 1145 1146 do_cleanups (old_cleanups); 1147 1148 return ret; 1149 } 1150 1151 return 0; 1152 } 1153 1154 static int 1155 record_remove_breakpoint (struct gdbarch *gdbarch, 1156 struct bp_target_info *bp_tgt) 1157 { 1158 if (!RECORD_IS_REPLAY) 1159 { 1160 struct cleanup *old_cleanups = record_gdb_operation_disable_set (); 1161 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt); 1162 1163 do_cleanups (old_cleanups); 1164 1165 return ret; 1166 } 1167 1168 return 0; 1169 } 1170 1171 static int 1172 record_can_execute_reverse (void) 1173 { 1174 return 1; 1175 } 1176 1177 static void 1178 init_record_ops (void) 1179 { 1180 record_ops.to_shortname = "record"; 1181 record_ops.to_longname = "Process record and replay target"; 1182 record_ops.to_doc = 1183 "Log program while executing and replay execution from log."; 1184 record_ops.to_open = record_open; 1185 record_ops.to_close = record_close; 1186 record_ops.to_resume = record_resume; 1187 record_ops.to_wait = record_wait; 1188 record_ops.to_disconnect = record_disconnect; 1189 record_ops.to_detach = record_detach; 1190 record_ops.to_mourn_inferior = record_mourn_inferior; 1191 record_ops.to_kill = record_kill; 1192 record_ops.to_create_inferior = find_default_create_inferior; 1193 record_ops.to_store_registers = record_store_registers; 1194 record_ops.to_xfer_partial = record_xfer_partial; 1195 record_ops.to_insert_breakpoint = record_insert_breakpoint; 1196 record_ops.to_remove_breakpoint = record_remove_breakpoint; 1197 record_ops.to_can_execute_reverse = record_can_execute_reverse; 1198 record_ops.to_stratum = record_stratum; 1199 record_ops.to_magic = OPS_MAGIC; 1200 } 1201 1202 static void 1203 show_record_debug (struct ui_file *file, int from_tty, 1204 struct cmd_list_element *c, const char *value) 1205 { 1206 fprintf_filtered (file, _("Debugging of process record target is %s.\n"), 1207 value); 1208 } 1209 1210 /* Alias for "target record". */ 1211 1212 static void 1213 cmd_record_start (char *args, int from_tty) 1214 { 1215 execute_command ("target record", from_tty); 1216 } 1217 1218 /* Truncate the record log from the present point 1219 of replay until the end. */ 1220 1221 static void 1222 cmd_record_delete (char *args, int from_tty) 1223 { 1224 if (current_target.to_stratum == record_stratum) 1225 { 1226 if (RECORD_IS_REPLAY) 1227 { 1228 if (!from_tty || query (_("Delete the log from this point forward " 1229 "and begin to record the running message " 1230 "at current PC?"))) 1231 record_list_release_next (); 1232 } 1233 else 1234 printf_unfiltered (_("Already at end of record list.\n")); 1235 1236 } 1237 else 1238 printf_unfiltered (_("Process record is not started.\n")); 1239 } 1240 1241 /* Implement the "stoprecord" command. */ 1242 1243 static void 1244 cmd_record_stop (char *args, int from_tty) 1245 { 1246 if (current_target.to_stratum == record_stratum) 1247 { 1248 unpush_target (&record_ops); 1249 printf_unfiltered (_("Process record is stoped and all execution " 1250 "log is deleted.\n")); 1251 } 1252 else 1253 printf_unfiltered (_("Process record is not started.\n")); 1254 } 1255 1256 /* Set upper limit of record log size. */ 1257 1258 static void 1259 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c) 1260 { 1261 if (record_insn_num > record_insn_max_num && record_insn_max_num) 1262 { 1263 printf_unfiltered (_("Record instructions number is bigger than " 1264 "record instructions max number. Auto delete " 1265 "the first ones?\n")); 1266 1267 while (record_insn_num > record_insn_max_num) 1268 record_list_release_first (); 1269 } 1270 } 1271 1272 /* Print the current index into the record log (number of insns recorded 1273 so far). */ 1274 1275 static void 1276 show_record_insn_number (char *ignore, int from_tty) 1277 { 1278 printf_unfiltered (_("Record instruction number is %d.\n"), 1279 record_insn_num); 1280 } 1281 1282 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist, 1283 *show_record_cmdlist, *info_record_cmdlist; 1284 1285 static void 1286 set_record_command (char *args, int from_tty) 1287 { 1288 printf_unfiltered (_("\ 1289 \"set record\" must be followed by an apporpriate subcommand.\n")); 1290 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout); 1291 } 1292 1293 static void 1294 show_record_command (char *args, int from_tty) 1295 { 1296 cmd_show_list (show_record_cmdlist, from_tty, ""); 1297 } 1298 1299 static void 1300 info_record_command (char *args, int from_tty) 1301 { 1302 cmd_show_list (info_record_cmdlist, from_tty, ""); 1303 } 1304 1305 void 1306 _initialize_record (void) 1307 { 1308 /* Init record_first. */ 1309 record_first.prev = NULL; 1310 record_first.next = NULL; 1311 record_first.type = record_end; 1312 1313 init_record_ops (); 1314 add_target (&record_ops); 1315 1316 add_setshow_zinteger_cmd ("record", no_class, &record_debug, 1317 _("Set debugging of record/replay feature."), 1318 _("Show debugging of record/replay feature."), 1319 _("When enabled, debugging output for " 1320 "record/replay feature is displayed."), 1321 NULL, show_record_debug, &setdebuglist, 1322 &showdebuglist); 1323 1324 add_prefix_cmd ("record", class_obscure, cmd_record_start, 1325 _("Abbreviated form of \"target record\" command."), 1326 &record_cmdlist, "record ", 0, &cmdlist); 1327 add_com_alias ("rec", "record", class_obscure, 1); 1328 add_prefix_cmd ("record", class_support, set_record_command, 1329 _("Set record options"), &set_record_cmdlist, 1330 "set record ", 0, &setlist); 1331 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist); 1332 add_prefix_cmd ("record", class_support, show_record_command, 1333 _("Show record options"), &show_record_cmdlist, 1334 "show record ", 0, &showlist); 1335 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist); 1336 add_prefix_cmd ("record", class_support, info_record_command, 1337 _("Info record options"), &info_record_cmdlist, 1338 "info record ", 0, &infolist); 1339 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist); 1340 1341 1342 add_cmd ("delete", class_obscure, cmd_record_delete, 1343 _("Delete the rest of execution log and start recording it anew."), 1344 &record_cmdlist); 1345 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist); 1346 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist); 1347 1348 add_cmd ("stop", class_obscure, cmd_record_stop, 1349 _("Stop the record/replay target."), 1350 &record_cmdlist); 1351 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist); 1352 1353 /* Record instructions number limit command. */ 1354 add_setshow_boolean_cmd ("stop-at-limit", no_class, 1355 &record_stop_at_limit, _("\ 1356 Set whether record/replay stops when record/replay buffer becomes full."), _("\ 1357 Show whether record/replay stops when record/replay buffer becomes full."), _("\ 1358 Default is ON.\n\ 1359 When ON, if the record/replay buffer becomes full, ask user what to do.\n\ 1360 When OFF, if the record/replay buffer becomes full,\n\ 1361 delete the oldest recorded instruction to make room for each new one."), 1362 NULL, NULL, 1363 &set_record_cmdlist, &show_record_cmdlist); 1364 add_setshow_zinteger_cmd ("insn-number-max", no_class, 1365 &record_insn_max_num, 1366 _("Set record/replay buffer limit."), 1367 _("Show record/replay buffer limit."), _("\ 1368 Set the maximum number of instructions to be stored in the\n\ 1369 record/replay buffer. Zero means unlimited. Default is 200000."), 1370 set_record_insn_max_num, 1371 NULL, &set_record_cmdlist, &show_record_cmdlist); 1372 add_cmd ("insn-number", class_obscure, show_record_insn_number, 1373 _("Show the current number of instructions in the " 1374 "record/replay buffer."), &info_record_cmdlist); 1375 } 1376