Merge PPC port
[sbcl/lichteblau.git] / src / runtime / breakpoint.c
blobe45082f8c1a3224627e3a522eb08dc9c79002299
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
12 #include <stdio.h>
13 #include <signal.h>
15 #include "runtime.h"
16 #include "os.h"
17 #include "sbcl.h"
18 #include "interrupt.h"
19 #include "arch.h"
20 #include "lispregs.h"
21 #include "globals.h"
22 #include "alloc.h"
23 #include "breakpoint.h"
25 #define REAL_LRA_SLOT 0
26 #ifndef __i386__
27 #define KNOWN_RETURN_P_SLOT 1
28 #define BOGUS_LRA_CONSTANTS 2
29 #else
30 #define KNOWN_RETURN_P_SLOT 2
31 #define BOGUS_LRA_CONSTANTS 3
32 #endif
34 static void *compute_pc(lispobj code_obj, int pc_offset)
36 struct code *code;
38 code = (struct code *)native_pointer(code_obj);
39 return (void *)((char *)code + HeaderValue(code->header)*sizeof(lispobj)
40 + pc_offset);
43 unsigned long breakpoint_install(lispobj code_obj, int pc_offset)
45 return arch_install_breakpoint(compute_pc(code_obj, pc_offset));
48 void breakpoint_remove(lispobj code_obj, int pc_offset,
49 unsigned long orig_inst)
51 arch_remove_breakpoint(compute_pc(code_obj, pc_offset), orig_inst);
54 void breakpoint_do_displaced_inst(os_context_t* context,
55 unsigned long orig_inst)
57 /* on platforms with sigreturn(), we go directly back from
58 * arch_do_displaced_inst() to lisp code, so we need to clean up
59 * our bindings now. (side note: I'd love to know in exactly what
60 * scenario the speed of breakpoint handling is critical enough to
61 * justify this maintenance mess)
63 * -dan 2001.08.09 */
65 #if (defined(sparc) && defined (solaris))
66 undo_fake_foreign_function_call(context);
67 #endif
68 arch_do_displaced_inst(context, orig_inst);
71 #ifndef __i386__
72 static lispobj find_code(os_context_t *context)
74 #ifdef reg_CODE
75 lispobj code = *os_context_register_addr(context, reg_CODE);
76 lispobj header;
78 if (lowtag_of(code) != OTHER_POINTER_LOWTAG)
79 return NIL;
81 header = *(lispobj *)(code-OTHER_POINTER_LOWTAG);
83 if (widetag_of(header) == CODE_HEADER_WIDETAG)
84 return code;
85 else
86 return code - HeaderValue(header)*sizeof(lispobj);
87 #else
88 return NIL;
89 #endif
91 #endif
93 #ifdef __i386__
94 static lispobj find_code(os_context_t *context)
96 lispobj codeptr =
97 (lispobj)component_ptr_from_pc((lispobj *)(*os_context_pc_addr(context)));
99 if (codeptr == 0) {
100 return NIL;
101 } else {
102 return codeptr + OTHER_POINTER_LOWTAG;
105 #endif
107 static int compute_offset(os_context_t *context, lispobj code)
109 if (code == NIL)
110 return 0;
111 else {
112 unsigned long code_start;
113 struct code *codeptr = (struct code *)native_pointer(code);
114 #ifdef parisc
115 unsigned long pc = *os_context_pc_addr(context) & ~3;
116 #else
117 unsigned long pc = *os_context_pc_addr(context);
118 #endif
120 code_start = (unsigned long)codeptr
121 + HeaderValue(codeptr->header)*sizeof(lispobj);
122 if (pc < code_start)
123 return 0;
124 else {
125 int offset = pc - code_start;
126 if (offset >= codeptr->code_size)
127 return 0;
128 else
129 return make_fixnum(offset);
133 /* FIXME: I can see no really good reason these couldn't be merged, but haven't
134 * tried. The sigprocmask() call would work just as well on alpha as it
135 * presumably does on x86 -dan 2001.08.10
137 #ifndef __i386__
138 void handle_breakpoint(int signal, siginfo_t *info, os_context_t *context)
140 lispobj code;
142 fake_foreign_function_call(context);
144 code = find_code(context);
146 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
147 compute_offset(context, code),
148 code,
149 alloc_sap(context));
151 undo_fake_foreign_function_call(context);
153 #else
154 void handle_breakpoint(int signal, siginfo_t* info, os_context_t *context)
156 lispobj code, context_sap = alloc_sap(context);
158 fake_foreign_function_call(context);
160 code = find_code(context);
162 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
163 * use debugger breakpoints anywhere in here. */
164 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
166 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
167 compute_offset(context, code),
168 code,
169 context_sap);
171 undo_fake_foreign_function_call(context);
173 #endif
175 #ifndef __i386__
176 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
177 os_context_t *context)
179 lispobj code, lra;
180 struct code *codeptr;
182 fake_foreign_function_call(context);
184 code = find_code(context);
185 codeptr = (struct code *)native_pointer(code);
187 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
188 compute_offset(context, code),
189 code,
190 alloc_sap(context));
192 lra = codeptr->constants[REAL_LRA_SLOT];
193 #ifdef reg_CODE
194 if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL) {
195 *os_context_register_addr(context, reg_CODE) = lra;
197 #endif
198 undo_fake_foreign_function_call(context);
199 return (void *)(lra-OTHER_POINTER_LOWTAG+sizeof(lispobj));
201 #else
202 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
203 os_context_t *context)
205 lispobj code, context_sap = alloc_sap(context);
206 struct code *codeptr;
208 fake_foreign_function_call(context);
210 code = find_code(context);
211 codeptr = (struct code *)native_pointer(code);
213 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
214 * use debugger breakpoints anywhere in here. */
215 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
217 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
218 compute_offset(context, code),
219 code,
220 context_sap);
222 undo_fake_foreign_function_call(context);
224 return compute_pc(codeptr->constants[REAL_LRA_SLOT],
225 fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));
227 #endif