2 * This software is part of the SBCL system. See the README file for
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.
18 #include "interrupt.h"
23 #include "breakpoint.h"
25 #include "genesis/code.h"
26 #include "genesis/fdefn.h"
28 #define REAL_LRA_SLOT 0
29 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
30 #define KNOWN_RETURN_P_SLOT 1
31 #define BOGUS_LRA_CONSTANTS 2
33 #define KNOWN_RETURN_P_SLOT 2
34 #define BOGUS_LRA_CONSTANTS 3
37 static void *compute_pc(lispobj code_obj, int pc_offset)
41 code = (struct code *)native_pointer(code_obj);
42 return (void *)((char *)code + HeaderValue(code->header)*sizeof(lispobj)
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)
68 #if (defined(LISP_FEATURE_SPARC) && defined (solaris))
69 undo_fake_foreign_function_call(context);
71 arch_do_displaced_inst(context, orig_inst);
74 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
75 static lispobj find_code(os_context_t *context)
78 lispobj code = *os_context_register_addr(context, reg_CODE);
81 if (lowtag_of(code) != OTHER_POINTER_LOWTAG)
84 header = *(lispobj *)(code-OTHER_POINTER_LOWTAG);
86 if (widetag_of(header) == CODE_HEADER_WIDETAG)
89 return code - HeaderValue(header)*sizeof(lispobj);
95 static lispobj find_code(os_context_t *context)
98 (lispobj)component_ptr_from_pc((lispobj *)(*os_context_pc_addr(context)));
103 return codeptr + OTHER_POINTER_LOWTAG;
108 static long compute_offset(os_context_t *context, lispobj code)
113 unsigned long code_start;
114 struct code *codeptr = (struct code *)native_pointer(code);
116 unsigned long pc = *os_context_pc_addr(context) & ~3;
118 unsigned long pc = *os_context_pc_addr(context);
121 code_start = (unsigned long)codeptr
122 + HeaderValue(codeptr->header)*sizeof(lispobj);
126 long offset = pc - code_start;
127 if (offset >= codeptr->code_size)
130 return make_fixnum(offset);
134 /* FIXME: I can see no really good reason these couldn't be merged, but haven't
135 * tried. The sigprocmask() call would work just as well on alpha as it
136 * presumably does on x86 -dan 2001.08.10
138 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
139 void handle_breakpoint(int signal, siginfo_t *info, os_context_t *context)
143 fake_foreign_function_call(context);
145 code = find_code(context);
146 /* FIXME we're calling into Lisp with signals masked here. Is this
147 * the right thing to do? */
148 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
149 compute_offset(context, code),
153 undo_fake_foreign_function_call(context);
156 void handle_breakpoint(int signal, siginfo_t* info, os_context_t *context)
158 lispobj code, context_sap = alloc_sap(context);
160 fake_foreign_function_call(context);
162 code = find_code(context);
164 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
165 * use debugger breakpoints anywhere in here. */
166 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
168 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
169 compute_offset(context, code),
173 undo_fake_foreign_function_call(context);
177 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
178 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
179 os_context_t *context)
182 struct code *codeptr;
184 fake_foreign_function_call(context);
186 code = find_code(context);
187 codeptr = (struct code *)native_pointer(code);
189 /* FIXME again, calling into Lisp with signals masked. Is this
191 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
192 compute_offset(context, code),
196 lra = codeptr->constants[REAL_LRA_SLOT];
198 if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL) {
199 *os_context_register_addr(context, reg_CODE) = lra;
202 undo_fake_foreign_function_call(context);
203 return (void *)(lra-OTHER_POINTER_LOWTAG+sizeof(lispobj));
206 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
207 os_context_t *context)
209 lispobj code, context_sap = alloc_sap(context);
210 struct code *codeptr;
212 fake_foreign_function_call(context);
214 code = find_code(context);
215 codeptr = (struct code *)native_pointer(code);
217 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
218 * use debugger breakpoints anywhere in here. */
219 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
221 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
222 compute_offset(context, code),
226 undo_fake_foreign_function_call(context);
228 return compute_pc(codeptr->constants[REAL_LRA_SLOT],
229 fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));