mem-break.c revision 1.1.1.2 1 1.1 christos /* Simulate breakpoints by patching locations in the target system, for GDB.
2 1.1 christos
3 1.1.1.2 christos Copyright (C) 1990-2015 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 "defs.h"
23 1.1 christos #include "symtab.h"
24 1.1 christos #include "breakpoint.h"
25 1.1 christos #include "inferior.h"
26 1.1 christos #include "target.h"
27 1.1 christos /* Insert a breakpoint on targets that don't have any better
28 1.1 christos breakpoint support. We read the contents of the target location
29 1.1 christos and stash it, then overwrite it with a breakpoint instruction.
30 1.1 christos BP_TGT->placed_address is the target location in the target
31 1.1 christos machine. BP_TGT->shadow_contents is some memory allocated for
32 1.1 christos saving the target contents. It is guaranteed by the caller to be
33 1.1 christos long enough to save BREAKPOINT_LEN bytes (this is accomplished via
34 1.1 christos BREAKPOINT_MAX). */
35 1.1 christos
36 1.1 christos int
37 1.1 christos default_memory_insert_breakpoint (struct gdbarch *gdbarch,
38 1.1 christos struct bp_target_info *bp_tgt)
39 1.1 christos {
40 1.1.1.2 christos CORE_ADDR addr = bp_tgt->reqstd_address;
41 1.1 christos const unsigned char *bp;
42 1.1 christos gdb_byte *readbuf;
43 1.1.1.2 christos int bplen;
44 1.1.1.2 christos int val;
45 1.1 christos
46 1.1 christos /* Determine appropriate breakpoint contents and size for this address. */
47 1.1.1.2 christos bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
48 1.1 christos if (bp == NULL)
49 1.1 christos error (_("Software breakpoints not implemented for this target."));
50 1.1 christos
51 1.1.1.2 christos bp_tgt->placed_address = addr;
52 1.1.1.2 christos bp_tgt->placed_size = bplen;
53 1.1.1.2 christos
54 1.1 christos /* Save the memory contents in the shadow_contents buffer and then
55 1.1 christos write the breakpoint instruction. */
56 1.1.1.2 christos bp_tgt->shadow_len = bplen;
57 1.1.1.2 christos readbuf = alloca (bplen);
58 1.1.1.2 christos val = target_read_memory (addr, readbuf, bplen);
59 1.1 christos if (val == 0)
60 1.1 christos {
61 1.1.1.2 christos memcpy (bp_tgt->shadow_contents, readbuf, bplen);
62 1.1.1.2 christos val = target_write_raw_memory (addr, bp, bplen);
63 1.1 christos }
64 1.1 christos
65 1.1 christos return val;
66 1.1 christos }
67 1.1 christos
68 1.1 christos
69 1.1 christos int
70 1.1 christos default_memory_remove_breakpoint (struct gdbarch *gdbarch,
71 1.1 christos struct bp_target_info *bp_tgt)
72 1.1 christos {
73 1.1 christos return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
74 1.1 christos bp_tgt->placed_size);
75 1.1 christos }
76 1.1 christos
77 1.1 christos
78 1.1 christos int
79 1.1.1.2 christos memory_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
80 1.1 christos struct bp_target_info *bp_tgt)
81 1.1 christos {
82 1.1 christos return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt);
83 1.1 christos }
84 1.1 christos
85 1.1 christos int
86 1.1.1.2 christos memory_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
87 1.1 christos struct bp_target_info *bp_tgt)
88 1.1 christos {
89 1.1 christos return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt);
90 1.1 christos }
91 1.1.1.2 christos
92 1.1.1.2 christos int
93 1.1.1.2 christos memory_validate_breakpoint (struct gdbarch *gdbarch,
94 1.1.1.2 christos struct bp_target_info *bp_tgt)
95 1.1.1.2 christos {
96 1.1.1.2 christos CORE_ADDR addr = bp_tgt->placed_address;
97 1.1.1.2 christos const gdb_byte *bp;
98 1.1.1.2 christos int val;
99 1.1.1.2 christos int bplen;
100 1.1.1.2 christos gdb_byte cur_contents[BREAKPOINT_MAX];
101 1.1.1.2 christos struct cleanup *cleanup;
102 1.1.1.2 christos int ret;
103 1.1.1.2 christos
104 1.1.1.2 christos /* Determine appropriate breakpoint contents and size for this
105 1.1.1.2 christos address. */
106 1.1.1.2 christos bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
107 1.1.1.2 christos
108 1.1.1.2 christos if (bp == NULL || bp_tgt->placed_size != bplen)
109 1.1.1.2 christos return 0;
110 1.1.1.2 christos
111 1.1.1.2 christos /* Make sure we see the memory breakpoints. */
112 1.1.1.2 christos cleanup = make_show_memory_breakpoints_cleanup (1);
113 1.1.1.2 christos val = target_read_memory (addr, cur_contents, bplen);
114 1.1.1.2 christos
115 1.1.1.2 christos /* If our breakpoint is no longer at the address, this means that
116 1.1.1.2 christos the program modified the code on us, so it is wrong to put back
117 1.1.1.2 christos the old value. */
118 1.1.1.2 christos ret = (val == 0 && memcmp (bp, cur_contents, bplen) == 0);
119 1.1.1.2 christos
120 1.1.1.2 christos do_cleanups (cleanup);
121 1.1.1.2 christos return ret;
122 1.1.1.2 christos }
123