Add safepoint mechanism
[sbcl.git] / src / runtime / breakpoint.c
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
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.
10  */
11
12 #include <stdio.h>
13 #include <signal.h>
14
15 #include "sbcl.h"
16 #include "runtime.h"
17 #include "os.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"
27
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
32 #else
33 #define KNOWN_RETURN_P_SLOT 2
34 #define BOGUS_LRA_CONSTANTS 3
35 #endif
36
37 static void *compute_pc(lispobj code_obj, int pc_offset)
38 {
39     struct code *code;
40
41     code = (struct code *)native_pointer(code_obj);
42     return (void *)((char *)code + HeaderValue(code->header)*sizeof(lispobj)
43                     + pc_offset);
44 }
45
46 unsigned int breakpoint_install(lispobj code_obj, int pc_offset)
47 {
48     return arch_install_breakpoint(compute_pc(code_obj, pc_offset));
49 }
50
51 void breakpoint_remove(lispobj code_obj, int pc_offset,
52                        unsigned int orig_inst)
53 {
54     arch_remove_breakpoint(compute_pc(code_obj, pc_offset), orig_inst);
55 }
56
57 void breakpoint_do_displaced_inst(os_context_t* context,
58                                   unsigned int orig_inst)
59 {
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)
65      *
66      * -dan 2001.08.09 */
67
68 #if (defined(LISP_FEATURE_SPARC) && defined (solaris))
69     undo_fake_foreign_function_call(context);
70 #endif
71     arch_do_displaced_inst(context, orig_inst);
72 }
73
74 static lispobj find_code(os_context_t *context)
75 {
76 #ifdef reg_CODE
77     lispobj code = *os_context_register_addr(context, reg_CODE);
78     lispobj header;
79
80     if (lowtag_of(code) != OTHER_POINTER_LOWTAG)
81         return NIL;
82
83     header = *(lispobj *)(code-OTHER_POINTER_LOWTAG);
84
85     if (widetag_of(header) == CODE_HEADER_WIDETAG)
86         return code;
87     else
88         return code - HeaderValue(header)*sizeof(lispobj);
89 #else
90     lispobj codeptr =
91         (lispobj)component_ptr_from_pc((lispobj *)(*os_context_pc_addr(context)));
92
93     if (codeptr == 0)
94         return NIL;
95     else
96         return codeptr + OTHER_POINTER_LOWTAG;
97 #endif
98 }
99
100 static long compute_offset(os_context_t *context, lispobj code)
101 {
102     if (code == NIL)
103         return 0;
104     else {
105         unsigned long code_start;
106         struct code *codeptr = (struct code *)native_pointer(code);
107 #ifdef LISP_FEATURE_HPPA
108         unsigned long pc = *os_context_pc_addr(context) & ~3;
109 #else
110         unsigned long pc = *os_context_pc_addr(context);
111 #endif
112
113         code_start = (unsigned long)codeptr
114             + HeaderValue(codeptr->header)*sizeof(lispobj);
115         if (pc < code_start)
116             return 0;
117         else {
118             unsigned long offset = pc - code_start;
119             if (offset >= (N_WORD_BYTES * fixnum_value(codeptr->code_size)))
120                 return 0;
121             else
122                 return make_fixnum(offset);
123         }
124     }
125 }
126
127 void handle_breakpoint(os_context_t *context)
128 {
129     lispobj code, context_sap;
130
131     fake_foreign_function_call(context);
132
133 #ifndef LISP_FEATURE_SB_SAFEPOINT
134     unblock_gc_signals(0, 0);
135 #endif
136     context_sap = alloc_sap(context);
137     code = find_code(context);
138
139 #ifndef LISP_FEATURE_WIN32
140     /* Don't disallow recursive breakpoint traps. Otherwise, we can't
141      * use debugger breakpoints anywhere in here. */
142     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
143 #endif
144
145     funcall3(StaticSymbolFunction(HANDLE_BREAKPOINT),
146              compute_offset(context, code),
147              code,
148              context_sap);
149
150     undo_fake_foreign_function_call(context);
151 }
152
153 void *handle_fun_end_breakpoint(os_context_t *context)
154 {
155     lispobj code, context_sap, lra;
156     struct code *codeptr;
157
158     fake_foreign_function_call(context);
159
160 #ifndef LISP_FEATURE_SB_SAFEPOINT
161     unblock_gc_signals(0, 0);
162 #endif
163     context_sap = alloc_sap(context);
164     code = find_code(context);
165     codeptr = (struct code *)native_pointer(code);
166
167 #ifndef LISP_FEATURE_WIN32
168     /* Don't disallow recursive breakpoint traps. Otherwise, we can't
169      * use debugger breakpoints anywhere in here. */
170     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
171 #endif
172
173     funcall3(StaticSymbolFunction(HANDLE_BREAKPOINT),
174              compute_offset(context, code),
175              code,
176              context_sap);
177
178     lra = codeptr->constants[REAL_LRA_SLOT];
179
180 #ifdef LISP_FEATURE_PPC
181     /* PPC now passes LRA objects in reg_LRA during return.  Other
182      * platforms should as well, but haven't been fixed yet. */
183     if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL)
184         *os_context_register_addr(context, reg_LRA) = lra;
185 #else
186 #ifdef reg_CODE
187     if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL)
188         *os_context_register_addr(context, reg_CODE) = lra;
189 #endif
190 #endif
191
192     undo_fake_foreign_function_call(context);
193
194 #ifdef reg_LRA
195     return (void *)(lra-OTHER_POINTER_LOWTAG+sizeof(lispobj));
196 #else
197     return compute_pc(lra, fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));
198 #endif
199 }
200
201 void
202 handle_single_step_trap (os_context_t *context, int kind, int register_offset)
203 {
204     fake_foreign_function_call(context);
205
206 #ifndef LISP_FEATURE_WIN32
207     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
208 #endif
209
210     funcall2(StaticSymbolFunction(HANDLE_SINGLE_STEP_TRAP),
211              make_fixnum(kind),
212              make_fixnum(register_offset));
213
214     undo_fake_foreign_function_call(context); /* blocks signals again */
215 }