1 /* Simulate breakpoints by patching locations in the target system, for GDB. 2 3 Copyright (C) 1990-2016 Free Software Foundation, Inc. 4 5 Contributed by Cygnus Support. Written by John Gilmore. 6 7 This file is part of GDB. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 22 #include "defs.h" 23 #include "symtab.h" 24 #include "breakpoint.h" 25 #include "inferior.h" 26 #include "target.h" 27 /* Insert a breakpoint on targets that don't have any better 28 breakpoint support. We read the contents of the target location 29 and stash it, then overwrite it with a breakpoint instruction. 30 BP_TGT->placed_address is the target location in the target 31 machine. BP_TGT->shadow_contents is some memory allocated for 32 saving the target contents. It is guaranteed by the caller to be 33 long enough to save BREAKPOINT_LEN bytes (this is accomplished via 34 BREAKPOINT_MAX). */ 35 36 int 37 default_memory_insert_breakpoint (struct gdbarch *gdbarch, 38 struct bp_target_info *bp_tgt) 39 { 40 CORE_ADDR addr = bp_tgt->reqstd_address; 41 const unsigned char *bp; 42 gdb_byte *readbuf; 43 int bplen; 44 int val; 45 46 /* Determine appropriate breakpoint contents and size for this address. */ 47 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen); 48 if (bp == NULL) 49 error (_("Software breakpoints not implemented for this target.")); 50 51 bp_tgt->placed_address = addr; 52 bp_tgt->placed_size = bplen; 53 54 /* Save the memory contents in the shadow_contents buffer and then 55 write the breakpoint instruction. */ 56 readbuf = (gdb_byte *) alloca (bplen); 57 val = target_read_memory (addr, readbuf, bplen); 58 if (val == 0) 59 { 60 /* These must be set together, either before or after the shadow 61 read, so that if we're "reinserting" a breakpoint that 62 doesn't have a shadow yet, the breakpoint masking code inside 63 target_read_memory doesn't mask out this breakpoint using an 64 unfilled shadow buffer. The core may be trying to reinsert a 65 permanent breakpoint, for targets that support breakpoint 66 conditions/commands on the target side for some types of 67 breakpoints, such as target remote. */ 68 bp_tgt->shadow_len = bplen; 69 memcpy (bp_tgt->shadow_contents, readbuf, bplen); 70 71 val = target_write_raw_memory (addr, bp, bplen); 72 } 73 74 return val; 75 } 76 77 78 int 79 default_memory_remove_breakpoint (struct gdbarch *gdbarch, 80 struct bp_target_info *bp_tgt) 81 { 82 return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents, 83 bp_tgt->placed_size); 84 } 85 86 87 int 88 memory_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch, 89 struct bp_target_info *bp_tgt) 90 { 91 return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt); 92 } 93 94 int 95 memory_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch, 96 struct bp_target_info *bp_tgt, 97 enum remove_bp_reason reason) 98 { 99 return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt); 100 } 101 102 int 103 memory_validate_breakpoint (struct gdbarch *gdbarch, 104 struct bp_target_info *bp_tgt) 105 { 106 CORE_ADDR addr = bp_tgt->placed_address; 107 const gdb_byte *bp; 108 int val; 109 int bplen; 110 gdb_byte cur_contents[BREAKPOINT_MAX]; 111 struct cleanup *cleanup; 112 int ret; 113 114 /* Determine appropriate breakpoint contents and size for this 115 address. */ 116 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen); 117 118 if (bp == NULL || bp_tgt->placed_size != bplen) 119 return 0; 120 121 /* Make sure we see the memory breakpoints. */ 122 cleanup = make_show_memory_breakpoints_cleanup (1); 123 val = target_read_memory (addr, cur_contents, bplen); 124 125 /* If our breakpoint is no longer at the address, this means that 126 the program modified the code on us, so it is wrong to put back 127 the old value. */ 128 ret = (val == 0 && memcmp (bp, cur_contents, bplen) == 0); 129 130 do_cleanups (cleanup); 131 return ret; 132 } 133