]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/mem-break.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / mem-break.c
1 /* Simulate breakpoints by patching locations in the target system, for GDB.
2 Copyright 1990, 1991, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by John Gilmore.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22
23 /* This file is only useful if BREAKPOINT is set. If not, we punt. */
24
25 #include "symtab.h"
26 #include "breakpoint.h"
27 #include "inferior.h"
28 #include "target.h"
29
30
31 /* Use the program counter to determine the contents and size
32 of a breakpoint instruction. If no target-dependent macro
33 BREAKPOINT_FROM_PC has been defined to implement this function,
34 assume that the breakpoint doesn't depend on the PC, and
35 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
36 Return a pointer to a string of bytes that encode a breakpoint
37 instruction, stores the length of the string to *lenptr,
38 and optionally adjust the pc to point to the correct memory location
39 for inserting the breakpoint. */
40
41 unsigned char *
42 memory_breakpoint_from_pc (pcptr, lenptr)
43 CORE_ADDR *pcptr;
44 int *lenptr;
45 {
46 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
47 breakpoint. On some machines, breakpoints are handled by the
48 target environment and we don't have to worry about them here. */
49 #ifdef BIG_BREAKPOINT
50 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
51 {
52 static unsigned char big_break_insn[] = BIG_BREAKPOINT;
53 *lenptr = sizeof (big_break_insn);
54 return big_break_insn;
55 }
56 #endif
57 #ifdef LITTLE_BREAKPOINT
58 if (TARGET_BYTE_ORDER != BIG_ENDIAN)
59 {
60 static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
61 *lenptr = sizeof (little_break_insn);
62 return little_break_insn;
63 }
64 #endif
65 #ifdef BREAKPOINT
66 {
67 static unsigned char break_insn[] = BREAKPOINT;
68 *lenptr = sizeof (break_insn);
69 return break_insn;
70 }
71 #endif
72 *lenptr = 0;
73 return NULL;
74 }
75
76
77 /* Insert a breakpoint on targets that don't have any better breakpoint
78 support. We read the contents of the target location and stash it,
79 then overwrite it with a breakpoint instruction. ADDR is the target
80 location in the target machine. CONTENTS_CACHE is a pointer to
81 memory allocated for saving the target contents. It is guaranteed
82 by the caller to be long enough to save BREAKPOINT_LEN bytes (this
83 is accomplished via BREAKPOINT_MAX). */
84
85 int
86 memory_insert_breakpoint (addr, contents_cache)
87 CORE_ADDR addr;
88 char *contents_cache;
89 {
90 int val;
91 unsigned char *bp;
92 int bplen;
93
94 /* Determine appropriate breakpoint contents and size for this address. */
95 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
96 if (bp == NULL)
97 error ("Software breakpoints not implemented for this target.");
98
99 /* Save the memory contents. */
100 val = target_read_memory (addr, contents_cache, bplen);
101
102 /* Write the breakpoint. */
103 if (val == 0)
104 val = target_write_memory (addr, (char *)bp, bplen);
105
106 return val;
107 }
108
109
110 int
111 memory_remove_breakpoint (addr, contents_cache)
112 CORE_ADDR addr;
113 char *contents_cache;
114 {
115 unsigned char *bp;
116 int bplen;
117
118 /* Determine appropriate breakpoint contents and size for this address. */
119 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
120 if (bp == NULL)
121 error ("Software breakpoints not implemented for this target.");
122
123 return target_write_memory (addr, contents_cache, bplen);
124 }