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