0.pre8.28
[sbcl/lichteblau.git] / src / runtime / breakpoint.c
blob5ff8509e65bb675081d94be3aff8eba0558037c9
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"
24 #include "thread.h"
25 #include "genesis/code.h"
26 #include "genesis/fdefn.h"
28 #define REAL_LRA_SLOT 0
29 #ifndef __i386__
30 #define KNOWN_RETURN_P_SLOT 1
31 #define BOGUS_LRA_CONSTANTS 2
32 #else
33 #define KNOWN_RETURN_P_SLOT 2
34 #define BOGUS_LRA_CONSTANTS 3
35 #endif
37 static void *compute_pc(lispobj code_obj, int pc_offset)
39 struct code *code;
41 code = (struct code *)native_pointer(code_obj);
42 return (void *)((char *)code + HeaderValue(code->header)*sizeof(lispobj)
43 + pc_offset);
46 unsigned long breakpoint_install(lispobj code_obj, int pc_offset)
48 return arch_install_breakpoint(compute_pc(code_obj, pc_offset));
51 void breakpoint_remove(lispobj code_obj, int pc_offset,
52 unsigned long orig_inst)
54 arch_remove_breakpoint(compute_pc(code_obj, pc_offset), orig_inst);
57 void breakpoint_do_displaced_inst(os_context_t* context,
58 unsigned long orig_inst)
60 /* on platforms with sigreturn(), we go directly back from
61 * arch_do_displaced_inst() to lisp code, so we need to clean up
62 * our bindings now. (side note: I'd love to know in exactly what
63 * scenario the speed of breakpoint handling is critical enough to
64 * justify this maintenance mess)
66 * -dan 2001.08.09 */
68 #if (defined(sparc) && defined (solaris))
69 undo_fake_foreign_function_call(context);
70 #endif
71 arch_do_displaced_inst(context, orig_inst);
74 #ifndef __i386__
75 static lispobj find_code(os_context_t *context)
77 #ifdef reg_CODE
78 lispobj code = *os_context_register_addr(context, reg_CODE);
79 lispobj header;
81 if (lowtag_of(code) != OTHER_POINTER_LOWTAG)
82 return NIL;
84 header = *(lispobj *)(code-OTHER_POINTER_LOWTAG);
86 if (widetag_of(header) == CODE_HEADER_WIDETAG)
87 return code;
88 else
89 return code - HeaderValue(header)*sizeof(lispobj);
90 #else
91 return NIL;
92 #endif
94 #endif
96 #ifdef __i386__
97 static lispobj find_code(os_context_t *context)
99 lispobj codeptr =
100 (lispobj)component_ptr_from_pc((lispobj *)(*os_context_pc_addr(context)));
102 if (codeptr == 0) {
103 return NIL;
104 } else {
105 return codeptr + OTHER_POINTER_LOWTAG;
108 #endif
110 static int compute_offset(os_context_t *context, lispobj code)
112 if (code == NIL)
113 return 0;
114 else {
115 unsigned long code_start;
116 struct code *codeptr = (struct code *)native_pointer(code);
117 #ifdef parisc
118 unsigned long pc = *os_context_pc_addr(context) & ~3;
119 #else
120 unsigned long pc = *os_context_pc_addr(context);
121 #endif
123 code_start = (unsigned long)codeptr
124 + HeaderValue(codeptr->header)*sizeof(lispobj);
125 if (pc < code_start)
126 return 0;
127 else {
128 int offset = pc - code_start;
129 if (offset >= codeptr->code_size)
130 return 0;
131 else
132 return make_fixnum(offset);
136 /* FIXME: I can see no really good reason these couldn't be merged, but haven't
137 * tried. The sigprocmask() call would work just as well on alpha as it
138 * presumably does on x86 -dan 2001.08.10
140 #ifndef __i386__
141 void handle_breakpoint(int signal, siginfo_t *info, os_context_t *context)
143 lispobj code;
145 fake_foreign_function_call(context);
147 code = find_code(context);
149 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
150 compute_offset(context, code),
151 code,
152 alloc_sap(context));
154 undo_fake_foreign_function_call(context);
156 #else
157 void handle_breakpoint(int signal, siginfo_t* info, os_context_t *context)
159 lispobj code, context_sap = alloc_sap(context);
161 fake_foreign_function_call(context);
163 code = find_code(context);
165 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
166 * use debugger breakpoints anywhere in here. */
167 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
169 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
170 compute_offset(context, code),
171 code,
172 context_sap);
174 undo_fake_foreign_function_call(context);
176 #endif
178 #ifndef __i386__
179 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
180 os_context_t *context)
182 lispobj code, lra;
183 struct code *codeptr;
185 fake_foreign_function_call(context);
187 code = find_code(context);
188 codeptr = (struct code *)native_pointer(code);
190 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
191 compute_offset(context, code),
192 code,
193 alloc_sap(context));
195 lra = codeptr->constants[REAL_LRA_SLOT];
196 #ifdef reg_CODE
197 if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL) {
198 *os_context_register_addr(context, reg_CODE) = lra;
200 #endif
201 undo_fake_foreign_function_call(context);
202 return (void *)(lra-OTHER_POINTER_LOWTAG+sizeof(lispobj));
204 #else
205 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
206 os_context_t *context)
208 lispobj code, context_sap = alloc_sap(context);
209 struct code *codeptr;
211 fake_foreign_function_call(context);
213 code = find_code(context);
214 codeptr = (struct code *)native_pointer(code);
216 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
217 * use debugger breakpoints anywhere in here. */
218 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
220 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
221 compute_offset(context, code),
222 code,
223 context_sap);
225 undo_fake_foreign_function_call(context);
227 return compute_pc(codeptr->constants[REAL_LRA_SLOT],
228 fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));
230 #endif