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