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