be487fad5f3888149c2df6b3db0bab576260470a
[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 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
75 static lispobj find_code(os_context_t *context)
76 {
77 #ifdef reg_CODE
78     lispobj code = *os_context_register_addr(context, reg_CODE);
79     lispobj header;
80
81     if (lowtag_of(code) != OTHER_POINTER_LOWTAG)
82         return NIL;
83
84     header = *(lispobj *)(code-OTHER_POINTER_LOWTAG);
85
86     if (widetag_of(header) == CODE_HEADER_WIDETAG)
87         return code;
88     else
89         return code - HeaderValue(header)*sizeof(lispobj);
90 #else
91     return NIL;
92 #endif
93 }
94 #else
95 static lispobj find_code(os_context_t *context)
96 {
97     lispobj codeptr =
98         (lispobj)component_ptr_from_pc((lispobj *)(*os_context_pc_addr(context)));
99
100     if (codeptr == 0) {
101         return NIL;
102     } else {
103         return codeptr + OTHER_POINTER_LOWTAG;
104     }
105 }
106 #endif
107
108 static long compute_offset(os_context_t *context, lispobj code)
109 {
110     if (code == NIL)
111         return 0;
112     else {
113         unsigned long code_start;
114         struct code *codeptr = (struct code *)native_pointer(code);
115 #ifdef parisc
116         unsigned long pc = *os_context_pc_addr(context) & ~3;
117 #else
118         unsigned long pc = *os_context_pc_addr(context);
119 #endif
120
121         code_start = (unsigned long)codeptr
122             + HeaderValue(codeptr->header)*sizeof(lispobj);
123         if (pc < code_start)
124             return 0;
125         else {
126             long offset = pc - code_start;
127             if (offset >= codeptr->code_size)
128                 return 0;
129             else
130                 return make_fixnum(offset);
131         }
132     }
133 }
134
135 void handle_breakpoint(int signal, siginfo_t* info, os_context_t *context)
136 {
137     lispobj code, context_sap;
138
139     fake_foreign_function_call(context);
140
141     context_sap = alloc_sap(context);
142     code = find_code(context);
143
144     /* Don't disallow recursive breakpoint traps. Otherwise, we can't
145      * use debugger breakpoints anywhere in here. */
146     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
147
148     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
149              compute_offset(context, code),
150              code,
151              context_sap);
152
153     undo_fake_foreign_function_call(context);
154 }
155
156 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
157 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
158                                 os_context_t *context)
159 {
160     lispobj code, lra;
161     struct code *codeptr;
162
163     fake_foreign_function_call(context);
164
165     code = find_code(context);
166     codeptr = (struct code *)native_pointer(code);
167
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
172     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
173              compute_offset(context, code),
174              code,
175              alloc_sap(context));
176
177     lra = codeptr->constants[REAL_LRA_SLOT];
178 #ifdef reg_CODE
179     if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL) {
180         *os_context_register_addr(context, reg_CODE) = lra;
181     }
182 #endif
183     undo_fake_foreign_function_call(context);
184     return (void *)(lra-OTHER_POINTER_LOWTAG+sizeof(lispobj));
185 }
186 #else
187 void *handle_fun_end_breakpoint(int signal, siginfo_t *info,
188                                 os_context_t *context)
189 {
190     lispobj code, context_sap;
191     struct code *codeptr;
192
193     fake_foreign_function_call(context);
194
195     context_sap = alloc_sap(context);
196     code = find_code(context);
197     codeptr = (struct code *)native_pointer(code);
198
199     /* Don't disallow recursive breakpoint traps. Otherwise, we can't
200      * use debugger breakpoints anywhere in here. */
201     thread_sigmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
202
203     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
204              compute_offset(context, code),
205              code,
206              context_sap);
207
208     undo_fake_foreign_function_call(context);
209
210     return compute_pc(codeptr->constants[REAL_LRA_SLOT],
211                       fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));
212 }
213 #endif