xref: /openbsd-src/gnu/usr.bin/binutils/gdb/mem-break.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
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 /* Either BREAKPOINT should be defined, or both of LITTLE_BREAKPOINT,
24    BIG_BREAKPOINT should be defined.  */
25 
26 #if defined (BREAKPOINT) || (defined (LITTLE_BREAKPOINT) && defined (BIG_BREAKPOINT))
27 
28 /* This file is only useful if BREAKPOINT is set.  If not, we punt.  */
29 
30 #include "symtab.h"
31 #include "breakpoint.h"
32 #include "inferior.h"
33 #include "target.h"
34 
35 /* If the target isn't bi-endian, just pretend it is.  */
36 #if defined(BREAKPOINT) && !defined (LITTLE_BREAKPOINT) && !defined (BIG_BREAKPOINT)
37 #define LITTLE_BREAKPOINT BREAKPOINT
38 #define BIG_BREAKPOINT BREAKPOINT
39 #endif
40 
41 /* This is the sequence of bytes we insert for a breakpoint.  On some
42    machines, breakpoints are handled by the target environment and we
43    don't have to worry about them here.  */
44 
45 static unsigned char big_break_insn[] = BIG_BREAKPOINT;
46 static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
47 
48 /* FIXME: We assume big and little breakpoints are the same size.  */
49 #define BREAKPOINT_LEN (sizeof (big_break_insn))
50 
51 /* Insert a breakpoint on targets that don't have any better breakpoint
52    support.  We read the contents of the target location and stash it,
53    then overwrite it with a breakpoint instruction.  ADDR is the target
54    location in the target machine.  CONTENTS_CACHE is a pointer to
55    memory allocated for saving the target contents.  It is guaranteed
56    by the caller to be long enough to save BREAKPOINT_LEN bytes (this
57    is accomplished via BREAKPOINT_MAX).  */
58 
59 int
60 memory_insert_breakpoint (addr, contents_cache)
61      CORE_ADDR addr;
62      char *contents_cache;
63 {
64   int val;
65 
66   val = target_read_memory (addr, contents_cache, BREAKPOINT_LEN);
67 
68   if (val == 0)
69     {
70       if (TARGET_BYTE_ORDER == BIG_ENDIAN)
71 	val = target_write_memory (addr, (char *)big_break_insn,
72 				   BREAKPOINT_LEN);
73       else
74 	val = target_write_memory (addr, (char *)little_break_insn,
75 				   BREAKPOINT_LEN);
76     }
77 
78   return val;
79 }
80 
81 
82 int
83 memory_remove_breakpoint (addr, contents_cache)
84      CORE_ADDR addr;
85      char *contents_cache;
86 {
87   return target_write_memory (addr, contents_cache, BREAKPOINT_LEN);
88 }
89 
90 
91 /* FIXME: This is a hack and should depend on the debugging target.
92    See comment in breakpoint.c where this is used.  */
93 
94 int memory_breakpoint_size = BREAKPOINT_LEN;
95 
96 
97 #else  /* BREAKPOINT */
98 
99 char nogo[] = "Breakpoints not implemented for this target.";
100 
101 int
102 memory_insert_breakpoint (addr, contents_cache)
103      CORE_ADDR addr;
104      char *contents_cache;
105 {
106   error (nogo);
107   return 0;	/* lint */
108 }
109 
110 int
111 memory_remove_breakpoint (addr, contents_cache)
112      CORE_ADDR addr;
113      char *contents_cache;
114 {
115   error (nogo);
116   return 0;	/* lint */
117 }
118 
119 int memory_breakpoint_size = -1;
120 
121 #endif /* BREAKPOINT */
122