2007-10-03 17:39:37 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2007 LuaJ. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
******************************************************************************/
|
2007-08-11 04:53:35 +00:00
|
|
|
package lua.debug;
|
|
|
|
|
|
2007-10-17 00:56:08 +00:00
|
|
|
import java.io.IOException;
|
2007-10-12 01:36:28 +00:00
|
|
|
import java.util.Hashtable;
|
2007-10-17 00:56:08 +00:00
|
|
|
import java.util.Timer;
|
|
|
|
|
import java.util.TimerTask;
|
2007-10-12 01:36:28 +00:00
|
|
|
import java.util.Vector;
|
2007-08-11 04:53:35 +00:00
|
|
|
|
|
|
|
|
import lua.CallInfo;
|
2007-10-04 21:04:38 +00:00
|
|
|
import lua.Lua;
|
2007-08-11 04:53:35 +00:00
|
|
|
import lua.StackState;
|
2007-10-03 17:39:37 +00:00
|
|
|
import lua.addon.compile.LexState;
|
2007-10-12 01:42:42 +00:00
|
|
|
import lua.debug.event.DebugEvent;
|
|
|
|
|
import lua.debug.event.DebugEventBreakpoint;
|
2007-10-24 20:21:16 +00:00
|
|
|
import lua.debug.event.DebugEventError;
|
2007-10-17 00:56:08 +00:00
|
|
|
import lua.debug.event.DebugEventType;
|
2007-10-12 01:42:42 +00:00
|
|
|
import lua.debug.request.DebugRequest;
|
|
|
|
|
import lua.debug.request.DebugRequestLineBreakpointToggle;
|
|
|
|
|
import lua.debug.request.DebugRequestListener;
|
|
|
|
|
import lua.debug.request.DebugRequestStack;
|
|
|
|
|
import lua.debug.request.DebugRequestType;
|
|
|
|
|
import lua.debug.response.DebugResponse;
|
|
|
|
|
import lua.debug.response.DebugResponseCallgraph;
|
|
|
|
|
import lua.debug.response.DebugResponseSimple;
|
|
|
|
|
import lua.debug.response.DebugResponseStack;
|
2007-08-11 04:53:35 +00:00
|
|
|
import lua.io.LocVars;
|
2007-09-18 22:55:22 +00:00
|
|
|
import lua.io.Proto;
|
2007-10-03 17:39:37 +00:00
|
|
|
import lua.value.LTable;
|
|
|
|
|
import lua.value.LValue;
|
2007-08-11 04:53:35 +00:00
|
|
|
|
|
|
|
|
public class DebugStackState extends StackState implements DebugRequestListener {
|
2007-09-18 22:55:22 +00:00
|
|
|
|
2007-10-24 20:21:16 +00:00
|
|
|
// stepping constants and stepping state
|
|
|
|
|
protected static final int STEP_NONE = 0;
|
|
|
|
|
protected static final int STEP_OVER = 1;
|
|
|
|
|
protected static final int STEP_INTO = 2;
|
|
|
|
|
protected static final int STEP_RETURN = 3;
|
|
|
|
|
protected int stepping = STEP_NONE;
|
|
|
|
|
protected boolean shouldPauseForStepping = false;
|
|
|
|
|
protected int steppingFrame = -1;
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-10-12 01:36:28 +00:00
|
|
|
protected Hashtable breakpoints = new Hashtable();
|
2007-10-03 17:39:37 +00:00
|
|
|
protected boolean exiting = false;
|
|
|
|
|
protected boolean suspended = false;
|
|
|
|
|
protected int lastline = -1;
|
2007-10-24 20:21:16 +00:00
|
|
|
protected String lastSource;
|
2007-10-25 00:11:23 +00:00
|
|
|
protected DebugSupport debugSupport;
|
|
|
|
|
protected VMException lastException;
|
2007-10-24 20:21:16 +00:00
|
|
|
|
2007-10-17 00:56:08 +00:00
|
|
|
|
|
|
|
|
public DebugStackState() {}
|
2007-08-11 04:53:35 +00:00
|
|
|
|
2007-10-17 00:56:08 +00:00
|
|
|
public void setDebugSupport(DebugSupport debugSupport) throws IOException {
|
|
|
|
|
if (debugSupport == null) {
|
|
|
|
|
throw new IllegalArgumentException("DebugSupport cannot be null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.debugSupport = debugSupport;
|
|
|
|
|
debugSupport.setDebugStackState(this);
|
|
|
|
|
debugSupport.start();
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-06 18:48:37 +00:00
|
|
|
protected void debugAssert(boolean b) {
|
|
|
|
|
if ( ! b )
|
|
|
|
|
error( "assert failure" );
|
|
|
|
|
}
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-09-18 22:55:22 +00:00
|
|
|
private String getFileLine(int cindex) {
|
|
|
|
|
String func = "?";
|
|
|
|
|
String line = "?";
|
|
|
|
|
String source = "?";
|
|
|
|
|
if ( cindex >= 0 ) {
|
|
|
|
|
CallInfo call = this.calls[cindex];
|
|
|
|
|
Proto p = call.closure.p;
|
|
|
|
|
if ( p != null && p.source != null )
|
|
|
|
|
source = p.source.toJavaString();
|
2007-10-03 17:39:37 +00:00
|
|
|
if ( p.lineinfo != null && p.lineinfo.length > call.pc )
|
|
|
|
|
line = String.valueOf( p.lineinfo[call.pc] );
|
2007-09-18 22:55:22 +00:00
|
|
|
// TODO: reverse lookup on function name ????
|
2007-10-18 17:47:01 +00:00
|
|
|
func = call.closure.toJavaString();
|
2007-09-18 22:55:22 +00:00
|
|
|
}
|
|
|
|
|
return source+":"+line+"("+func+")";
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-03 17:39:37 +00:00
|
|
|
// override and fill in line number info
|
2007-10-23 00:34:04 +00:00
|
|
|
public void error(String message, int level) {
|
|
|
|
|
super.error( level<=0? message: getFileLine(cc+1-level)+": "+message );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use line numbers by default
|
2007-10-04 23:51:39 +00:00
|
|
|
public void error(String message) {
|
2007-10-23 00:34:04 +00:00
|
|
|
error(message, 1);
|
2007-09-18 22:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// intercept exceptions and fill in line numbers
|
|
|
|
|
public void exec() {
|
|
|
|
|
try {
|
|
|
|
|
super.exec();
|
2007-10-25 00:11:23 +00:00
|
|
|
} catch ( AbortException e ) {
|
2007-10-03 17:39:37 +00:00
|
|
|
// ignored. Client aborts the debugging session.
|
2007-10-25 00:11:23 +00:00
|
|
|
} catch ( VMException e ) {
|
|
|
|
|
// let VM exceptions be processed
|
2007-10-24 20:21:16 +00:00
|
|
|
// the same as the base class to minimize differences
|
|
|
|
|
// between the debug and non-debug behavior
|
2007-10-25 00:11:23 +00:00
|
|
|
throw e;
|
|
|
|
|
} catch ( Exception e ) {
|
|
|
|
|
lastException = new VMException(e);
|
|
|
|
|
debugSupport.notifyDebugEvent(new DebugEventError(e.getMessage()));
|
2007-10-24 20:21:16 +00:00
|
|
|
suspend();
|
2007-09-18 22:55:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
// debug hooks
|
|
|
|
|
public void debugHooks( int pc ) {
|
2007-10-12 23:39:27 +00:00
|
|
|
if ( exiting ) {
|
|
|
|
|
throw new AbortException("aborted by debug client");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(DebugUtils.IS_DEBUG)
|
2007-10-24 20:21:16 +00:00
|
|
|
DebugUtils.println("entered debugHook on pc=" + pc + "...");
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
synchronized ( this ) {
|
2007-10-24 20:21:16 +00:00
|
|
|
CallInfo currentCallInfo = calls[cc];
|
|
|
|
|
Proto currentProto = currentCallInfo.closure.p;
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-10-24 20:21:16 +00:00
|
|
|
// if we are not stepping, we keep going if the line doesn't change
|
|
|
|
|
int line = getLineNumber(currentCallInfo);
|
|
|
|
|
String source = DebugUtils.getSourceFileName(currentProto.source);
|
|
|
|
|
if ( !isStepping() && lastline == line && source.equals(lastSource) ) {
|
2007-08-11 05:29:56 +00:00
|
|
|
return;
|
2007-10-03 17:39:37 +00:00
|
|
|
}
|
2007-10-24 20:21:16 +00:00
|
|
|
|
2007-10-12 01:36:28 +00:00
|
|
|
if(DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("debugHook - executing line: " + line);
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-10-24 20:21:16 +00:00
|
|
|
int i = currentProto.code[pc];
|
|
|
|
|
int opCode = StackState.GET_OPCODE(i);
|
|
|
|
|
if (isStepping() &&
|
|
|
|
|
opCode == StackState.OP_RETURN && cc == 0) {
|
|
|
|
|
cancelStepping();
|
|
|
|
|
} else if (shouldPauseForStepping) {
|
|
|
|
|
shouldPauseForStepping = false;
|
|
|
|
|
suspendOnStepping();
|
|
|
|
|
} else if ( stepping == STEP_INTO ) {
|
|
|
|
|
if (lastline != line){
|
|
|
|
|
suspendOnStepping();
|
|
|
|
|
} else if (opCode == StackState.OP_CALL) {
|
|
|
|
|
shouldPauseForStepping = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (stepping == STEP_OVER) {
|
|
|
|
|
if ((lastline != line && steppingFrame == cc) || (steppingFrame > cc)) {
|
|
|
|
|
suspendOnStepping();
|
|
|
|
|
}
|
|
|
|
|
} else if (stepping == STEP_RETURN) {
|
|
|
|
|
if ((opCode == StackState.OP_RETURN && cc == this.steppingFrame) ||
|
|
|
|
|
(opCode == StackState.OP_TAILCALL && cc == this.steppingFrame)){
|
|
|
|
|
shouldPauseForStepping = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for a break point if we aren't suspended already
|
|
|
|
|
if ( !suspended && lastline != line) {
|
|
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("Source: " + currentProto.source);
|
|
|
|
|
|
|
|
|
|
String breakpointKey = constructBreakpointKey(source, line);
|
|
|
|
|
if (breakpoints.containsKey(breakpointKey)){
|
|
|
|
|
if(DebugUtils.IS_DEBUG)
|
2007-10-12 01:36:28 +00:00
|
|
|
DebugUtils.println("hitting breakpoint " + constructBreakpointKey(source, line));
|
2007-10-24 20:21:16 +00:00
|
|
|
|
|
|
|
|
debugSupport.notifyDebugEvent(new DebugEventBreakpoint(source, line));
|
|
|
|
|
suspended = true;
|
|
|
|
|
}
|
2007-08-11 05:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-24 20:21:16 +00:00
|
|
|
// save line in case next op is a step
|
|
|
|
|
lastline = line;
|
|
|
|
|
lastSource = source;
|
|
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
// wait for a state change
|
2007-10-03 17:39:37 +00:00
|
|
|
while (suspended && !exiting ) {
|
2007-08-11 04:53:35 +00:00
|
|
|
try {
|
|
|
|
|
this.wait();
|
2007-10-12 01:36:28 +00:00
|
|
|
if(DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("resuming execution...");
|
2007-10-25 00:11:23 +00:00
|
|
|
|
|
|
|
|
if (lastException != null) {
|
|
|
|
|
throw lastException;
|
|
|
|
|
}
|
2007-08-11 04:53:35 +00:00
|
|
|
} catch ( InterruptedException ie ) {
|
|
|
|
|
ie.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-10-24 20:21:16 +00:00
|
|
|
private boolean isStepping() {
|
|
|
|
|
return stepping != STEP_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cancelStepping() {
|
|
|
|
|
debugSupport.notifyDebugEvent(new DebugEvent(DebugEventType.resumedOnSteppingEnd));
|
|
|
|
|
stepping = STEP_NONE;
|
|
|
|
|
steppingFrame = -1;
|
|
|
|
|
shouldPauseForStepping = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void suspendOnStepping() {
|
|
|
|
|
debugSupport.notifyDebugEvent(new DebugEvent(DebugEventType.suspendedOnStepping));
|
|
|
|
|
suspended = true;
|
|
|
|
|
steppingFrame = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-03 17:39:37 +00:00
|
|
|
/**
|
|
|
|
|
* Get the current line number
|
|
|
|
|
* @param pc program counter
|
|
|
|
|
* @return the line number corresponding to the pc
|
|
|
|
|
*/
|
|
|
|
|
private int getLineNumber(CallInfo ci) {
|
|
|
|
|
int[] lineNumbers = ci.closure.p.lineinfo;
|
|
|
|
|
int pc = ci.pc;
|
|
|
|
|
int line = (lineNumbers != null && lineNumbers.length > pc ? lineNumbers[pc] : -1);
|
|
|
|
|
return line;
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
2007-10-03 17:39:37 +00:00
|
|
|
|
|
|
|
|
// ------------------ commands coming from the debugger -------------------
|
2007-08-11 04:53:35 +00:00
|
|
|
|
2007-10-03 17:39:37 +00:00
|
|
|
public DebugResponse handleRequest(DebugRequest request) {
|
2007-10-17 00:56:08 +00:00
|
|
|
if (this.debugSupport == null) {
|
|
|
|
|
throw new IllegalStateException("DebugStackState is not equiped with DebugSupport.");
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("DebugStackState is handling request: " + request.toString());
|
|
|
|
|
|
2007-10-17 00:56:08 +00:00
|
|
|
DebugRequestType requestType = request.getType();
|
|
|
|
|
if (DebugRequestType.start == requestType) {
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.started);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
2007-10-03 17:39:37 +00:00
|
|
|
return DebugResponseSimple.SUCCESS;
|
2007-10-17 00:56:08 +00:00
|
|
|
} else if (DebugRequestType.exit == requestType) {
|
|
|
|
|
stop();
|
|
|
|
|
return DebugResponseSimple.SUCCESS;
|
|
|
|
|
} else if (DebugRequestType.suspend == requestType) {
|
|
|
|
|
suspend();
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.suspendedByClient);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
2007-10-03 17:39:37 +00:00
|
|
|
return DebugResponseSimple.SUCCESS;
|
2007-10-17 00:56:08 +00:00
|
|
|
} else if (DebugRequestType.resume == requestType) {
|
|
|
|
|
resume();
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.resumedByClient);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
2007-10-03 17:39:37 +00:00
|
|
|
return DebugResponseSimple.SUCCESS;
|
2007-10-10 00:26:23 +00:00
|
|
|
} else if (DebugRequestType.lineBreakpointSet == requestType) {
|
2007-10-03 17:39:37 +00:00
|
|
|
DebugRequestLineBreakpointToggle setBreakpointRequest
|
|
|
|
|
= (DebugRequestLineBreakpointToggle)request;
|
|
|
|
|
setBreakpoint(setBreakpointRequest.getSource(), setBreakpointRequest.getLineNumber());
|
|
|
|
|
return DebugResponseSimple.SUCCESS;
|
2007-10-10 00:26:23 +00:00
|
|
|
} else if (DebugRequestType.lineBreakpointClear == requestType) {
|
2007-10-03 17:39:37 +00:00
|
|
|
DebugRequestLineBreakpointToggle clearBreakpointRequest
|
|
|
|
|
= (DebugRequestLineBreakpointToggle)request;
|
|
|
|
|
clearBreakpoint(clearBreakpointRequest.getSource(), clearBreakpointRequest.getLineNumber());
|
|
|
|
|
return DebugResponseSimple.SUCCESS;
|
2007-10-10 00:26:23 +00:00
|
|
|
} else if (DebugRequestType.callgraph == requestType) {
|
2007-10-03 17:39:37 +00:00
|
|
|
return new DebugResponseCallgraph(getCallgraph());
|
2007-10-10 00:26:23 +00:00
|
|
|
} else if (DebugRequestType.stack == requestType) {
|
2007-10-03 17:39:37 +00:00
|
|
|
DebugRequestStack stackRequest = (DebugRequestStack) request;
|
|
|
|
|
int index = stackRequest.getIndex();
|
|
|
|
|
return new DebugResponseStack(getStack(index));
|
2007-10-24 20:21:16 +00:00
|
|
|
} else if (DebugRequestType.stepInto == requestType) {
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.resumedOnSteppingInto);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
|
|
|
|
stepInto();
|
|
|
|
|
return DebugResponseSimple.SUCCESS;
|
|
|
|
|
} else if (DebugRequestType.stepOver == requestType) {
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.resumedOnSteppingOver);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
|
|
|
|
stepOver();
|
|
|
|
|
return DebugResponseSimple.SUCCESS;
|
|
|
|
|
} else if (DebugRequestType.stepReturn == requestType) {
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.resumedOnSteppingReturn);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
|
|
|
|
stepReturn();
|
2007-10-03 17:39:37 +00:00
|
|
|
return DebugResponseSimple.SUCCESS;
|
|
|
|
|
}
|
2007-10-10 00:26:23 +00:00
|
|
|
|
|
|
|
|
throw new java.lang.IllegalArgumentException( "unkown request type: "+ request.getType());
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* suspend the execution
|
|
|
|
|
*/
|
|
|
|
|
public void suspend() {
|
|
|
|
|
synchronized ( this ) {
|
|
|
|
|
suspended = true;
|
2007-10-24 20:21:16 +00:00
|
|
|
stepping = STEP_NONE;
|
2007-08-11 04:53:35 +00:00
|
|
|
lastline = -1;
|
|
|
|
|
this.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
/**
|
|
|
|
|
* resume the execution
|
|
|
|
|
*/
|
|
|
|
|
public void resume() {
|
|
|
|
|
synchronized ( this ) {
|
|
|
|
|
suspended = false;
|
2007-10-24 20:21:16 +00:00
|
|
|
stepping = STEP_NONE;
|
2007-08-11 04:53:35 +00:00
|
|
|
this.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-17 00:56:08 +00:00
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
if (this.debugSupport == null) {
|
|
|
|
|
throw new IllegalStateException("DebugStackState is not equiped with DebugSupport.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DebugEvent event = new DebugEvent(DebugEventType.terminated);
|
|
|
|
|
debugSupport.notifyDebugEvent(event);
|
|
|
|
|
|
|
|
|
|
new Timer().schedule(new TimerTask() {
|
|
|
|
|
public void run () {
|
|
|
|
|
debugSupport.stop();
|
|
|
|
|
debugSupport = null;
|
|
|
|
|
}
|
2007-10-24 20:21:16 +00:00
|
|
|
}, 500);
|
2007-10-17 00:56:08 +00:00
|
|
|
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
/**
|
|
|
|
|
* terminate the execution
|
|
|
|
|
*/
|
|
|
|
|
public void exit() {
|
|
|
|
|
synchronized ( this ) {
|
|
|
|
|
exiting = true;
|
|
|
|
|
this.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set breakpoint at line N
|
|
|
|
|
* @param N the line to set the breakpoint at
|
|
|
|
|
*/
|
2007-10-03 17:39:37 +00:00
|
|
|
public void setBreakpoint(String source, int lineNumber) {
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG) {
|
|
|
|
|
DebugUtils.print("source: " + source + " line:" + lineNumber);
|
|
|
|
|
DebugUtils.println("adding breakpoint " + constructBreakpointKey(source, lineNumber));
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
synchronized ( this ) {
|
2007-10-24 20:21:16 +00:00
|
|
|
String normalizedFileName = DebugUtils.getSourceFileName(source);
|
|
|
|
|
breakpoints.put(constructBreakpointKey(normalizedFileName, lineNumber), Boolean.TRUE );
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-03 17:39:37 +00:00
|
|
|
protected String constructBreakpointKey(String source, int lineNumber) {
|
|
|
|
|
return source + ":" + lineNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
/**
|
2007-10-03 17:39:37 +00:00
|
|
|
* clear breakpoint at line lineNumber of source source
|
2007-08-11 04:53:35 +00:00
|
|
|
*/
|
2007-10-03 17:39:37 +00:00
|
|
|
public void clearBreakpoint(String source, int lineNumber) {
|
2007-10-12 01:36:28 +00:00
|
|
|
if(DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("removing breakpoint " + constructBreakpointKey(source, lineNumber));
|
2007-08-11 04:53:35 +00:00
|
|
|
synchronized ( this ) {
|
2007-10-24 20:21:16 +00:00
|
|
|
String normalizedFileName = DebugUtils.getSourceFileName(source);
|
|
|
|
|
breakpoints.remove(constructBreakpointKey(normalizedFileName, lineNumber));
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* return the current call graph (i.e. stack frames from
|
|
|
|
|
* old to new, include information about file, method, etc.)
|
|
|
|
|
*/
|
2007-10-03 17:39:37 +00:00
|
|
|
public StackFrame[] getCallgraph() {
|
2007-08-11 04:53:35 +00:00
|
|
|
int n = cc;
|
2007-10-03 17:39:37 +00:00
|
|
|
|
2007-08-11 04:53:35 +00:00
|
|
|
if ( n < 0 || n >= calls.length )
|
2007-10-03 17:39:37 +00:00
|
|
|
return new StackFrame[0];
|
|
|
|
|
|
|
|
|
|
StackFrame[] frames = new StackFrame[n+1];
|
|
|
|
|
for ( int i = 0; i <= n; i++ ) {
|
2007-08-11 04:53:35 +00:00
|
|
|
CallInfo ci = calls[i];
|
2007-10-03 17:39:37 +00:00
|
|
|
frames[i] = new StackFrame(ci, getLineNumber(ci));
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
2007-10-03 17:39:37 +00:00
|
|
|
return frames;
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-03 17:39:37 +00:00
|
|
|
public Variable[] getStack(int index) {
|
|
|
|
|
if (index < 0 || index >= calls.length) {
|
|
|
|
|
//TODO: this is an error, handle it differently
|
|
|
|
|
return new Variable[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CallInfo callInfo = calls[index];
|
2007-10-12 01:36:28 +00:00
|
|
|
if(DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("Stack Frame: " + index + "[" + callInfo.base + "," + callInfo.top + "]");
|
2007-10-03 17:39:37 +00:00
|
|
|
int top = callInfo.top < callInfo.base ? callInfo.base : callInfo.top;
|
|
|
|
|
Proto prototype = callInfo.closure.p;
|
|
|
|
|
LocVars[] localVariables = prototype.locvars;
|
2007-10-12 01:36:28 +00:00
|
|
|
Vector variables = new Vector();
|
2007-10-03 17:39:37 +00:00
|
|
|
int localVariableCount = 0;
|
2007-10-12 01:36:28 +00:00
|
|
|
Hashtable variablesSeen = new Hashtable();
|
2007-10-03 17:39:37 +00:00
|
|
|
for (int i = 0; localVariables != null && i < localVariables.length && i <= top; i++) {
|
|
|
|
|
String varName = localVariables[i].varname.toString();
|
2007-10-12 01:36:28 +00:00
|
|
|
if(DebugUtils.IS_DEBUG) {
|
|
|
|
|
DebugUtils.print("\tVariable: " + varName);
|
|
|
|
|
DebugUtils.print("\tValue: " + stack[callInfo.base + i]);
|
|
|
|
|
}
|
2007-10-03 17:39:37 +00:00
|
|
|
if (!variablesSeen.contains(varName) &&
|
|
|
|
|
!LexState.isReservedKeyword(varName)) {
|
2007-10-12 01:36:28 +00:00
|
|
|
variablesSeen.put(varName, varName);
|
2007-10-03 17:39:37 +00:00
|
|
|
LValue value = stack[callInfo.base + i];
|
|
|
|
|
if (value != null) {
|
2007-10-04 21:04:38 +00:00
|
|
|
int type = value.luaGetType();
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.print("\tType: " + Lua.TYPE_NAMES[type]);
|
2007-10-04 21:04:38 +00:00
|
|
|
if (type == Lua.LUA_TTABLE) {
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println(" (selected)");
|
2007-10-12 20:39:26 +00:00
|
|
|
variables.addElement(
|
2007-10-03 17:39:37 +00:00
|
|
|
new TableVariable(localVariableCount++,
|
|
|
|
|
varName,
|
|
|
|
|
type,
|
|
|
|
|
(LTable) value));
|
2007-10-04 21:04:38 +00:00
|
|
|
} else if (type != Lua.LUA_TFUNCTION &&
|
|
|
|
|
type != LUA_TTHREAD) {
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println(" (selected)");
|
2007-10-12 20:39:26 +00:00
|
|
|
variables.addElement(
|
2007-10-03 17:39:37 +00:00
|
|
|
new Variable(localVariableCount++,
|
|
|
|
|
varName,
|
|
|
|
|
type,
|
|
|
|
|
value.toString()));
|
|
|
|
|
} else {
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("");
|
2007-10-03 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("");
|
2007-10-03 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2007-10-24 20:21:16 +00:00
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("");
|
2007-10-03 17:39:37 +00:00
|
|
|
}
|
2007-10-12 20:39:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable[] result = new Variable[variables.size()];
|
|
|
|
|
for (int i = 0; i < variables.size(); i++) {
|
|
|
|
|
result[i] = (Variable) variables.elementAt(i);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-10-24 20:21:16 +00:00
|
|
|
* step over to next line
|
2007-08-11 04:53:35 +00:00
|
|
|
*/
|
2007-10-24 20:21:16 +00:00
|
|
|
public void stepOver() {
|
|
|
|
|
synchronized ( this ) {
|
|
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("stepOver on cc=" + cc + "...");
|
|
|
|
|
|
|
|
|
|
suspended = false;
|
|
|
|
|
stepping = STEP_OVER;
|
|
|
|
|
steppingFrame = cc;
|
|
|
|
|
this.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* step a single statement
|
|
|
|
|
*/
|
|
|
|
|
public void stepInto() {
|
2007-08-11 04:53:35 +00:00
|
|
|
synchronized ( this ) {
|
2007-10-03 17:39:37 +00:00
|
|
|
suspended = false;
|
2007-10-24 20:21:16 +00:00
|
|
|
stepping = STEP_INTO;
|
2007-08-11 04:53:35 +00:00
|
|
|
this.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-24 20:21:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* return from the method call
|
|
|
|
|
*/
|
|
|
|
|
public void stepReturn() {
|
|
|
|
|
synchronized ( this ) {
|
|
|
|
|
if (DebugUtils.IS_DEBUG)
|
|
|
|
|
DebugUtils.println("stepReturn on cc=" + cc + "...");
|
|
|
|
|
|
|
|
|
|
suspended = false;
|
|
|
|
|
stepping = STEP_RETURN;
|
|
|
|
|
steppingFrame = cc;
|
|
|
|
|
this.notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-11 04:53:35 +00:00
|
|
|
}
|