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