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 #define REAL_LRA_SLOT 0
27 #define KNOWN_RETURN_P_SLOT 1
28 #define BOGUS_LRA_CONSTANTS 2
30 #define KNOWN_RETURN_P_SLOT 2
31 #define BOGUS_LRA_CONSTANTS 3
34 static void *compute_pc(lispobj code_obj, int pc_offset)
38 code = (struct code *)PTR(code_obj);
39 return (void *)((char *)code + HeaderValue(code->header)*sizeof(lispobj)
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 #if !defined(hpux) && !defined(irix) && !defined(__i386__)
58 undo_fake_foreign_function_call(context);
60 arch_do_displaced_inst(context, orig_inst);
64 static lispobj find_code(os_context_t *context)
67 lispobj code = *os_context_register_addr(context, reg_CODE);
70 if (LowtagOf(code) != type_OtherPointer)
73 header = *(lispobj *)(code-type_OtherPointer);
75 if (TypeOf(header) == type_CodeHeader)
78 return code - HeaderValue(header)*sizeof(lispobj);
86 static lispobj find_code(os_context_t *context)
88 lispobj codeptr = component_ptr_from_pc(*os_context_pc_addr(context));
93 return codeptr + type_OtherPointer;
98 static int compute_offset(os_context_t *context, lispobj code)
103 unsigned long code_start;
104 struct code *codeptr = (struct code *)PTR(code);
106 unsigned long pc = *os_context_pc_addr(context) & ~3;
108 unsigned long pc = *os_context_pc_addr(context);
111 code_start = (unsigned long)codeptr
112 + HeaderValue(codeptr->header)*sizeof(lispobj);
116 int offset = pc - code_start;
117 if (offset >= codeptr->code_size)
120 return make_fixnum(offset);
126 void handle_breakpoint(int signal, siginfo_t *info, os_context_t *context)
130 fake_foreign_function_call(context);
132 code = find_code(context);
134 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
135 compute_offset(context, code),
139 undo_fake_foreign_function_call(context);
142 void handle_breakpoint(int signal, siginfo_t* info, os_context_t *context)
144 lispobj code, context_sap = alloc_sap(context);
146 fake_foreign_function_call(context);
148 code = find_code(context);
150 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
151 * use debugger breakpoints anywhere in here. */
152 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
154 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
155 compute_offset(context, code),
159 undo_fake_foreign_function_call(context);
164 void *handle_function_end_breakpoint(int signal, siginfo_t *info,
165 os_context_t *context)
168 struct code *codeptr;
170 fake_foreign_function_call(context);
172 code = find_code(context);
173 codeptr = (struct code *)PTR(code);
175 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
176 compute_offset(context, code),
180 lra = codeptr->constants[REAL_LRA_SLOT];
182 if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL) {
183 *os_context_register_addr(context, reg_CODE) = lra;
186 undo_fake_foreign_function_call(context);
187 return (void *)(lra-type_OtherPointer+sizeof(lispobj));
190 void *handle_function_end_breakpoint(int signal, siginfo_t *info,
191 os_context_t *context)
193 lispobj code, context_sap = alloc_sap(context);
194 struct code *codeptr;
196 fake_foreign_function_call(context);
198 code = find_code(context);
199 codeptr = (struct code *)PTR(code);
201 /* Don't disallow recursive breakpoint traps. Otherwise, we can't
202 * use debugger breakpoints anywhere in here. */
203 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
205 funcall3(SymbolFunction(HANDLE_BREAKPOINT),
206 compute_offset(context, code),
210 undo_fake_foreign_function_call(context);
212 return compute_pc(codeptr->constants[REAL_LRA_SLOT],
213 fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));