testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / mem-break.c
blob712ad000daf167cd7a7154463da600b5b07cc300
1 /* Simulate breakpoints by patching locations in the target system, for GDB.
3 Copyright (C) 1990-2024 Free Software Foundation, Inc.
5 Contributed by Cygnus Support. Written by John Gilmore.
7 This file is part of GDB.
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.
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.
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/>. */
22 #include "symtab.h"
23 #include "breakpoint.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "gdbarch.h"
28 /* Insert a breakpoint on targets that don't have any better
29 breakpoint support. We read the contents of the target location
30 and stash it, then overwrite it with a breakpoint instruction.
31 BP_TGT->placed_address is the target location in the target
32 machine. BP_TGT->shadow_contents is some memory allocated for
33 saving the target contents. It is guaranteed by the caller to be
34 long enough to save BREAKPOINT_LEN bytes (this is accomplished via
35 BREAKPOINT_MAX). */
37 int
38 default_memory_insert_breakpoint (struct gdbarch *gdbarch,
39 struct bp_target_info *bp_tgt)
41 CORE_ADDR addr = bp_tgt->placed_address;
42 const unsigned char *bp;
43 gdb_byte *readbuf;
44 int bplen;
45 int val;
47 /* Determine appropriate breakpoint contents and size for this address. */
48 bp = gdbarch_sw_breakpoint_from_kind (gdbarch, bp_tgt->kind, &bplen);
50 /* Save the memory contents in the shadow_contents buffer and then
51 write the breakpoint instruction. */
52 readbuf = (gdb_byte *) alloca (bplen);
53 val = target_read_memory (addr, readbuf, bplen);
54 if (val == 0)
56 /* These must be set together, either before or after the shadow
57 read, so that if we're "reinserting" a breakpoint that
58 doesn't have a shadow yet, the breakpoint masking code inside
59 target_read_memory doesn't mask out this breakpoint using an
60 unfilled shadow buffer. The core may be trying to reinsert a
61 permanent breakpoint, for targets that support breakpoint
62 conditions/commands on the target side for some types of
63 breakpoints, such as target remote. */
64 bp_tgt->shadow_len = bplen;
65 memcpy (bp_tgt->shadow_contents, readbuf, bplen);
67 val = target_write_raw_memory (addr, bp, bplen);
70 return val;
74 int
75 default_memory_remove_breakpoint (struct gdbarch *gdbarch,
76 struct bp_target_info *bp_tgt)
78 int bplen;
80 gdbarch_sw_breakpoint_from_kind (gdbarch, bp_tgt->kind, &bplen);
82 return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
83 bplen);
87 int
88 memory_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
89 struct bp_target_info *bp_tgt)
91 return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt);
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)
99 return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt);
103 memory_validate_breakpoint (struct gdbarch *gdbarch,
104 struct bp_target_info *bp_tgt)
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];
112 /* Determine appropriate breakpoint contents and size for this
113 address. */
114 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
116 if (bp == NULL)
117 return 0;
119 /* Make sure we see the memory breakpoints. */
120 scoped_restore restore_memory
121 = make_scoped_restore_show_memory_breakpoints (1);
122 val = target_read_memory (addr, cur_contents, bplen);
124 /* If our breakpoint is no longer at the address, this means that
125 the program modified the code on us, so it is wrong to put back
126 the old value. */
127 return (val == 0 && memcmp (bp, cur_contents, bplen) == 0);