-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRubyDebugger.java
More file actions
282 lines (232 loc) · 10.8 KB
/
Copy pathRubyDebugger.java
File metadata and controls
282 lines (232 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* header & license
* Copyright (c) 2007 Martin Krauskopf
* Copyright (c) 2007 Peter Brant
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.jruby.debug;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyKernel;
import org.jruby.RubyModule;
import org.jruby.RubyProc;
import org.jruby.anno.JRubyMethod;
import org.jruby.debug.RubyDebugBaseLibrary.DebugThread;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.builtin.IRubyObject;
public final class RubyDebugger {
private RubyDebugger() {/* forbid instances */}
static final String DEBUG_THREAD_NAME = "DebugThread";
static final String CONTEXT_NAME = "Context";
private static Debugger debugger;
public static RubyModule createDebuggerModule(Ruby runtime) {
/* Debugger module. */
RubyModule debuggerMod = runtime.defineModule("Debugger");
debuggerMod.defineAnnotatedMethods(RubyDebugger.class);
/* Debugger::ThreadsTable */
/* RubyClass threadsTable = */ debuggerMod.defineClassUnder("ThreadsTable", runtime.getObject(), runtime.getObject().getAllocator());
/* Debugger::DebugThread */
RubyClass debugThread = debuggerMod.defineClassUnder(DEBUG_THREAD_NAME, runtime.getClass("Thread"), runtime.getClass("Thread").getAllocator());
debugThread.defineAnnotatedMethods(DebugThread.class);
/* Debugger::Breakpoint */
RubyClass breakpoint = debuggerMod.defineClassUnder("Breakpoint", runtime.getObject(), BREAKPOINT_ALLOCATOR);
breakpoint.defineAnnotatedMethods(Breakpoint.class);
/* Debugger::Context */
RubyClass context = debuggerMod.defineClassUnder(CONTEXT_NAME, runtime.getObject(), CONTEXT_ALLOCATOR);
context.defineAnnotatedMethods(Context.class);
return debuggerMod;
}
private static Debugger debugger() {
synchronized (RubyDebugger.class) {
if (debugger == null) {
debugger = new Debugger();
}
}
return debugger;
}
/**
* This method activates the debugger. If it's called without a block it
* returns +true+, unless debugger was already started. If a block is given,
* it starts debugger and yields to block. When the block is finished
* executing it stops the debugger with Debugger.stop method.
* <p>
* <i>Note that if you want to stop debugger, you must call Debugger.stop as
* many time as you called Debugger.start method.</i>
* </p>
*/
@JRubyMethod(name="start_", module=true)
public static IRubyObject start(IRubyObject recv, Block block) {
return debugger().start(recv, block);
}
/**
* This method deactivates the debugger. It returns +true+ if the debugger
* is deactivated, otherwise it returns +false+.
* <p>
* <i>Note that if you want to stop debugger, you must call Debugger.stop as
* many time as you called Debugger.start method.</i>
* </p>
*/
@JRubyMethod(name="stop", module=true)
public static IRubyObject stop(IRubyObject recv, Block block) {
boolean stopped = debugger().stop(recv.getRuntime());
return Util.toRBoolean(recv, stopped);
}
@JRubyMethod(name="started?", module=true)
public static IRubyObject started_p(IRubyObject recv, Block block) {
return Util.toRBoolean(recv, debugger().isStarted());
}
@JRubyMethod(name="breakpoints", module=true)
public static IRubyObject breakpoints(IRubyObject recv, Block block) {
debugger().checkStarted(recv);
return debugger().getBreakpoints();
}
@JRubyMethod(name="add_breakpoint", module=true, required=2, optional=1)
public static IRubyObject add_breakpoint(IRubyObject recv, IRubyObject[] args, Block block) {
return debugger().addBreakpoint(recv, args);
}
@JRubyMethod(name="remove_breakpoint", module=true, required=1)
public static IRubyObject remove_breakpoint(IRubyObject recv, IRubyObject breakpointId, Block block) {
return debugger().removeBreakpoint(recv, breakpointId);
}
@JRubyMethod(name="catchpoints", module=true)
public static IRubyObject catchpoint(IRubyObject recv, Block block) {
debugger().checkStarted(recv);
return debugger().getCatchpoints();
}
@JRubyMethod(name="add_catchpoint", module=true, required=1)
public static IRubyObject addCatchpoint(IRubyObject recv, IRubyObject catchpoint, Block block) {
debugger().addCatchpoint(recv, catchpoint);
return catchpoint;
}
@JRubyMethod(name="last_context", module=true)
public static IRubyObject last_context(IRubyObject recv, Block block) {
return debugger().lastInterrupted(recv);
}
@JRubyMethod(name="contexts", module=true)
public static IRubyObject contexts(IRubyObject recv, Block block) {
return debugger().getDebugContexts(recv);
}
@JRubyMethod(name="current_context", module=true)
public static IRubyObject current_context(IRubyObject recv, Block block) {
return debugger().getCurrentContext(recv);
}
@JRubyMethod(name="thread_context", module=true, required=1)
public static IRubyObject thread_context(IRubyObject recv, IRubyObject context, Block block) {
return debugger().getCurrentContext(recv);
}
@JRubyMethod(name="suspend", module=true)
public static IRubyObject suspend(IRubyObject recv, Block block) {
debugger().suspend(recv);
return recv;
}
@JRubyMethod(name="resume", module=true)
public static IRubyObject resume(IRubyObject recv, Block block) {
debugger().resume(recv);
return recv;
}
@JRubyMethod(name="tracing", module=true)
public static IRubyObject tracing(IRubyObject recv, Block block) {
return recv.getRuntime().newBoolean(debugger().isTracing());
}
@JRubyMethod(name="tracing=", module=true, required=1)
public static IRubyObject tracing_set(IRubyObject recv, IRubyObject tracing, Block block) {
debugger().setTracing(tracing.isTrue());
return tracing;
}
/**
* <pre>
* Debugger.debug_load(file, stop = false, increment_start = false) -> nil
* </pre>
* <p>
* Same as Kernel#load but resets current context's frames.
* <p>
* FOR INTERNAL USE ONLY.
* @param stop parameter forces the debugger to stop at the first line of
* code in the +file+
* @param increment_start determines if start_count should be incremented.
* When control threads are used, they have to be set up before
* loading the debugger; so here +increment_start+ will be false.
*/
@JRubyMethod(name="debug_load", module=true, required=1, optional=2)
public static IRubyObject debug_load(IRubyObject recv, IRubyObject[] args, Block block) {
return debugger().load(recv, args);
}
@JRubyMethod(name="skip", module=true)
public static IRubyObject skip(IRubyObject recv, Block block) {
return debugger().skip(recv, block);
}
@JRubyMethod(name="debug_at_exit", module=true)
public static IRubyObject debug_at_exit(IRubyObject recv, Block block) {
RubyProc proc = RubyKernel.proc(recv.getRuntime().getCurrentContext(), recv, block);
recv.getRuntime().pushExitBlock(proc);
return proc;
}
@JRubyMethod(name="post_mortem?", module=true)
public static IRubyObject post_mortem_p(IRubyObject recv, Block block) {
return Util.toRBoolean(recv, debugger().isPostMortem());
}
@JRubyMethod(name="post_mortem=", module=true, required=1)
public static IRubyObject post_mortem_set(IRubyObject recv, IRubyObject postMortem, Block block) {
throw recv.getRuntime().newRuntimeError("Post mortem debugging is not (yet) supported");
/*
debugger().setPostMortem(postMortem.isTrue());
return postMortem;
*/
}
@JRubyMethod(name="keep_frame_binding?", module=true)
public static IRubyObject keep_frame_binding_p(IRubyObject recv, Block block) {
return recv.getRuntime().newBoolean(debugger().isKeepFrameBinding());
}
@JRubyMethod(name="keep_frame_binding=", module=true, required=1)
public static IRubyObject keep_frame_binding_set(IRubyObject recv, IRubyObject keepFrameBinding, Block block) {
debugger().setKeepFrameBinding(keepFrameBinding.isTrue());
return keepFrameBinding;
}
@JRubyMethod(name="track_frame_args?", module=true)
public static IRubyObject track_frame_args_p(IRubyObject recv, Block block) {
return recv.getRuntime().newBoolean(debugger().isTrackFrameArgs());
}
@JRubyMethod(name="track_frame_args=", module=true, required=1)
public static IRubyObject track_frame_args_set(IRubyObject recv, IRubyObject traceFrameArgs, Block block) {
debugger().setTrackFrameArgs(traceFrameArgs.isTrue());
return traceFrameArgs;
}
@JRubyMethod(name="debug", module=true)
public static IRubyObject debug(IRubyObject recv, Block block) {
return Util.toRBoolean(recv, debugger().isDebug());
}
@JRubyMethod(name="debug=", module=true, required=1)
public static IRubyObject debug_set(IRubyObject recv, IRubyObject debug, Block block) {
debugger().setDebug(debug.isTrue());
return debug;
}
private static final ObjectAllocator BREAKPOINT_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new Breakpoint(runtime, klass);
}
};
private static final ObjectAllocator CONTEXT_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new Context(runtime, klass, debugger());
}
};
}