gdbarch_breakpoint_from_pc doesn't return NULL
[deliverable/binutils-gdb.git] / gdb / mem-break.c
CommitLineData
c906108c 1/* Simulate breakpoints by patching locations in the target system, for GDB.
f4f9705a 2
618f726f 3 Copyright (C) 1990-2016 Free Software Foundation, Inc.
f4f9705a 4
c906108c
SS
5 Contributed by Cygnus Support. Written by John Gilmore.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
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.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
c906108c
SS
23#include "symtab.h"
24#include "breakpoint.h"
25#include "inferior.h"
26#include "target.h"
8181d85f
DJ
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). */
c906108c
SS
35
36int
ae4b2284
MD
37default_memory_insert_breakpoint (struct gdbarch *gdbarch,
38 struct bp_target_info *bp_tgt)
c906108c 39{
0d5ed153 40 CORE_ADDR addr = bp_tgt->reqstd_address;
f4f9705a 41 const unsigned char *bp;
35c63cd8 42 gdb_byte *readbuf;
0d5ed153
MR
43 int bplen;
44 int val;
c906108c
SS
45
46 /* Determine appropriate breakpoint contents and size for this address. */
0d5ed153 47 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
c906108c 48
0d5ed153
MR
49 bp_tgt->placed_address = addr;
50 bp_tgt->placed_size = bplen;
51
35c63cd8
JB
52 /* Save the memory contents in the shadow_contents buffer and then
53 write the breakpoint instruction. */
224c3ddb 54 readbuf = (gdb_byte *) alloca (bplen);
0d5ed153 55 val = target_read_memory (addr, readbuf, bplen);
c906108c 56 if (val == 0)
35c63cd8 57 {
68901c4d
PA
58 /* These must be set together, either before or after the shadow
59 read, so that if we're "reinserting" a breakpoint that
60 doesn't have a shadow yet, the breakpoint masking code inside
61 target_read_memory doesn't mask out this breakpoint using an
62 unfilled shadow buffer. The core may be trying to reinsert a
63 permanent breakpoint, for targets that support breakpoint
64 conditions/commands on the target side for some types of
65 breakpoints, such as target remote. */
66 bp_tgt->shadow_len = bplen;
0d5ed153 67 memcpy (bp_tgt->shadow_contents, readbuf, bplen);
68901c4d 68
0d5ed153 69 val = target_write_raw_memory (addr, bp, bplen);
35c63cd8 70 }
c906108c
SS
71
72 return val;
73}
74
75
76int
ae4b2284
MD
77default_memory_remove_breakpoint (struct gdbarch *gdbarch,
78 struct bp_target_info *bp_tgt)
c906108c 79{
f0ba3972
PA
80 return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
81 bp_tgt->placed_size);
c906108c 82}
917317f4
JM
83
84
917317f4 85int
3db08215 86memory_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
a6d9a66e 87 struct bp_target_info *bp_tgt)
917317f4 88{
a6d9a66e 89 return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt);
917317f4
JM
90}
91
917317f4 92int
3db08215 93memory_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
73971819
PA
94 struct bp_target_info *bp_tgt,
95 enum remove_bp_reason reason)
917317f4 96{
a6d9a66e 97 return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt);
917317f4 98}
08351840
PA
99
100int
101memory_validate_breakpoint (struct gdbarch *gdbarch,
102 struct bp_target_info *bp_tgt)
103{
104 CORE_ADDR addr = bp_tgt->placed_address;
105 const gdb_byte *bp;
106 int val;
107 int bplen;
108 gdb_byte cur_contents[BREAKPOINT_MAX];
109 struct cleanup *cleanup;
110 int ret;
111
112 /* Determine appropriate breakpoint contents and size for this
113 address. */
114 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
115
116 if (bp == NULL || bp_tgt->placed_size != bplen)
117 return 0;
118
119 /* Make sure we see the memory breakpoints. */
120 cleanup = make_show_memory_breakpoints_cleanup (1);
121 val = target_read_memory (addr, cur_contents, bplen);
122
123 /* If our breakpoint is no longer at the address, this means that
124 the program modified the code on us, so it is wrong to put back
125 the old value. */
126 ret = (val == 0 && memcmp (bp, cur_contents, bplen) == 0);
127
128 do_cleanups (cleanup);
129 return ret;
130}
This page took 1.37499 seconds and 4 git commands to generate.