Initial revision
[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 /*
13  * $Header$
14  */
15
16 #include <stdio.h>
17 #include <signal.h>
18
19 #include "runtime.h"
20 #include "os.h"
21 #include "sbcl.h"
22 #include "interrupt.h"
23 #include "arch.h"
24 #include "lispregs.h"
25 #include "globals.h"
26 #include "alloc.h"
27 #include "breakpoint.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 *)PTR(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 #if !defined(hpux) && !defined(irix) && !defined(__i386__)
62     undo_fake_foreign_function_call(context);
63 #endif
64     arch_do_displaced_inst(context, orig_inst);
65 }
66
67 #ifndef __i386__
68 static lispobj find_code(os_context_t *context)
69 {
70 #ifdef reg_CODE
71     lispobj code = *os_context_register_addr(context, reg_CODE);
72     lispobj header;
73
74     if (LowtagOf(code) != type_OtherPointer)
75         return NIL;
76
77     header = *(lispobj *)(code-type_OtherPointer);
78
79     if (TypeOf(header) == type_CodeHeader)
80         return code;
81     else
82         return code - HeaderValue(header)*sizeof(lispobj);
83 #else
84     return NIL;
85 #endif
86 }
87 #endif
88
89 #ifdef __i386__
90 static lispobj find_code(os_context_t *context)
91 {
92   lispobj codeptr = component_ptr_from_pc(*os_context_pc_addr(context));
93
94   if (codeptr == 0) {
95       return NIL;
96   } else {
97       return codeptr + type_OtherPointer;
98   }
99 }
100 #endif
101
102 static int compute_offset(os_context_t *context, lispobj code)
103 {
104     if (code == NIL)
105         return 0;
106     else {
107         unsigned long code_start;
108         struct code *codeptr = (struct code *)PTR(code);
109 #ifdef parisc
110         unsigned long pc = *os_context_pc_addr(context) & ~3;
111 #else
112         unsigned long pc = *os_context_pc_addr(context);
113 #endif
114
115         code_start = (unsigned long)codeptr
116             + HeaderValue(codeptr->header)*sizeof(lispobj);
117         if (pc < code_start)
118             return 0;
119         else {
120             int offset = pc - code_start;
121             if (offset >= codeptr->code_size)
122                 return 0;
123             else
124                 return make_fixnum(offset);
125         }
126     }
127 }
128
129 #ifndef __i386__
130 void handle_breakpoint(int signal, siginfo_t *info, os_context_t *context)
131 {
132     lispobj code;
133
134     fake_foreign_function_call(context);
135
136     code = find_code(context);
137
138     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
139              compute_offset(context, code),
140              code,
141              alloc_sap(context));
142
143     undo_fake_foreign_function_call(context);
144 }
145 #else
146 void handle_breakpoint(int signal, siginfo_t* info, os_context_t *context)
147 {
148     lispobj code, context_sap = alloc_sap(context);
149
150     fake_foreign_function_call(context);
151
152     code = find_code(context);
153
154     /* Don't disallow recursive breakpoint traps. Otherwise, we can't
155      * use debugger breakpoints anywhere in here. */
156     sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
157
158     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
159              compute_offset(context, code),
160              code,
161              context_sap);
162
163     undo_fake_foreign_function_call(context);
164 }
165 #endif
166
167 #ifndef __i386__
168 void *handle_function_end_breakpoint(int signal, siginfo_t *info,
169                                      os_context_t *context)
170 {
171     lispobj code, lra;
172     struct code *codeptr;
173
174     fake_foreign_function_call(context);
175
176     code = find_code(context);
177     codeptr = (struct code *)PTR(code);
178
179     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
180              compute_offset(context, code),
181              code,
182              alloc_sap(context));
183
184     lra = codeptr->constants[REAL_LRA_SLOT];
185 #ifdef reg_CODE
186     if (codeptr->constants[KNOWN_RETURN_P_SLOT] == NIL) {
187         *os_context_register_addr(context, reg_CODE) = lra;
188     }
189 #endif
190     undo_fake_foreign_function_call(context);
191     return (void *)(lra-type_OtherPointer+sizeof(lispobj));
192 }
193 #else
194 void *handle_function_end_breakpoint(int signal, siginfo_t *info,
195                                      os_context_t *context)
196 {
197     lispobj code, context_sap = alloc_sap(context);
198     struct code *codeptr;
199
200     fake_foreign_function_call(context);
201
202     code = find_code(context);
203     codeptr = (struct code *)PTR(code);
204
205     /* Don't disallow recursive breakpoint traps. Otherwise, we can't
206      * use debugger breakpoints anywhere in here. */
207     sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
208
209     funcall3(SymbolFunction(HANDLE_BREAKPOINT),
210              compute_offset(context, code),
211              code,
212              context_sap);
213
214     undo_fake_foreign_function_call(context);
215
216     return compute_pc(codeptr->constants[REAL_LRA_SLOT],
217                       fixnum_value(codeptr->constants[REAL_LRA_SLOT+1]));
218 }
219 #endif