mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-20 07:22:32 +00:00
Added JavaScript debug assistance to figure out the problems inside JavaScript
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11722 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
0adce5329f
commit
289a414fb0
3 files changed
+177
No files matched your search
@@ -6,6 +6,7 @@ import com.gargoylesoftware.htmlunit.Page;
|
||||
import com.gargoylesoftware.htmlunit.WebRequestSettings;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
|
||||
import com.gargoylesoftware.htmlunit.javascript.host.Stylesheet;
|
||||
import hudson.model.Descriptor;
|
||||
import hudson.model.FreeStyleProject;
|
||||
@@ -16,6 +17,7 @@ import junit.framework.TestCase;
|
||||
import org.jvnet.hudson.test.HudsonHomeLoader.CopyExisting;
|
||||
import org.jvnet.hudson.test.recipes.Recipe;
|
||||
import org.jvnet.hudson.test.recipes.Recipe.Runner;
|
||||
import org.jvnet.hudson.test.rhino.JavaScriptDebugger;
|
||||
import org.mortbay.jetty.Server;
|
||||
import org.mortbay.jetty.bio.SocketConnector;
|
||||
import org.mortbay.jetty.security.HashUserRealm;
|
||||
@@ -23,6 +25,8 @@ import org.mortbay.jetty.security.UserRealm;
|
||||
import org.mortbay.jetty.webapp.Configuration;
|
||||
import org.mortbay.jetty.webapp.WebAppContext;
|
||||
import org.mortbay.jetty.webapp.WebXmlConfiguration;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.w3c.css.sac.CSSException;
|
||||
import org.w3c.css.sac.CSSParseException;
|
||||
import org.w3c.css.sac.ErrorHandler;
|
||||
@@ -72,6 +76,17 @@ public abstract class HudsonTestCase extends TestCase {
|
||||
*/
|
||||
private List<WeakReference<WebClient>> clients = new ArrayList<WeakReference<WebClient>>();
|
||||
|
||||
/**
|
||||
* JavaScript "debugger" that provides you information about the JavaScript call stack
|
||||
* and the current values of the local variables in those stack frame.
|
||||
*
|
||||
* <p>
|
||||
* Unlike Java debugger, which you as a human interfaces directly and interactively,
|
||||
* this JavaScript debugger is to be interfaced by your program (or through the
|
||||
* expression evaluation capability of your Java debugger.)
|
||||
*/
|
||||
protected JavaScriptDebugger jsDebugger = new JavaScriptDebugger();
|
||||
|
||||
protected HudsonTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
@@ -108,6 +123,19 @@ public abstract class HudsonTestCase extends TestCase {
|
||||
env.dispose();
|
||||
}
|
||||
|
||||
protected void runTest() throws Throwable {
|
||||
new JavaScriptEngine(null); // ensure that ContextFactory is initialized
|
||||
Context cx= ContextFactory.getGlobal().enterContext();
|
||||
try {
|
||||
cx.setOptimizationLevel(-1);
|
||||
cx.setDebugger(jsDebugger,null);
|
||||
|
||||
super.runTest();
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link Hudson}. If the derived class wants to create it in a different way,
|
||||
* you can override it.
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.jvnet.hudson.test.rhino;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.debug.DebugFrame;
|
||||
import org.mozilla.javascript.debug.DebuggableScript;
|
||||
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Stack frame.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class CallStackFrame implements DebugFrame {
|
||||
/**
|
||||
* {@link JavaScriptDebugger} that this stack frame lives in.
|
||||
*/
|
||||
public final JavaScriptDebugger owner;
|
||||
/**
|
||||
* The function being executed.
|
||||
*/
|
||||
public final DebuggableScript fnOrScript;
|
||||
|
||||
private Scriptable activation;
|
||||
private Scriptable thisObj;
|
||||
private Object[] args;
|
||||
private int line;
|
||||
|
||||
public CallStackFrame(JavaScriptDebugger owner, DebuggableScript fnOrScript) {
|
||||
this.owner = owner;
|
||||
this.fnOrScript = fnOrScript;
|
||||
}
|
||||
|
||||
public void onEnter(Context cx, Scriptable activation, Scriptable thisObj, Object[] args) {
|
||||
this.activation = activation;
|
||||
this.thisObj = thisObj;
|
||||
this.args = args;
|
||||
owner.callStack.add(this);
|
||||
}
|
||||
|
||||
public void onExit(Context cx, boolean byThrow, Object resultOrException) {
|
||||
// can't simply call removeFirst, because due to tail call elimination,
|
||||
// intermediate frames can be dropped at any time
|
||||
// TODO: shouldn't it be suffice to just check the end?
|
||||
for( int i=owner.callStack.size()-1; i>=0; i-- )
|
||||
if(owner.callStack.get(i)==this) {
|
||||
owner.callStack.remove(i);
|
||||
break;
|
||||
}
|
||||
|
||||
activation = null;
|
||||
thisObj = null;
|
||||
args = null;
|
||||
line = -1;
|
||||
}
|
||||
|
||||
public void onLineChange(Context cx, int lineNumber) {
|
||||
this.line = lineNumber;
|
||||
}
|
||||
|
||||
public void onExceptionThrown(Context cx, Throwable ex) {
|
||||
}
|
||||
|
||||
public void onDebuggerStatement(Context cx) {
|
||||
}
|
||||
|
||||
/**
|
||||
* In-scope variables.
|
||||
*/
|
||||
public SortedMap<String,Object> getVariables() {
|
||||
SortedMap<String,Object> r = new TreeMap<String,Object>();
|
||||
for( int i=fnOrScript.getParamAndVarCount()-1; i>=0; i-- ) {
|
||||
String name =fnOrScript.getParamOrVarName(i);
|
||||
r.put(name,activation.get(name,activation));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats this call stack, arguments, and its local variables as a human readable string.
|
||||
*/
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(fnOrScript.getFunctionName());
|
||||
buf.append('(');
|
||||
for( int i=0; i<args.length; i++ ) {
|
||||
if(i!=0) buf.append(',');
|
||||
buf.append(args[i]);
|
||||
}
|
||||
buf.append(')');
|
||||
buf.append("\n at ").append(fnOrScript.getSourceName()).append('#').append(line);
|
||||
|
||||
buf.append("\n variables=").append(getVariables());
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.jvnet.hudson.test.rhino;
|
||||
|
||||
import org.mozilla.javascript.debug.Debugger;
|
||||
import org.mozilla.javascript.debug.DebuggableScript;
|
||||
import org.mozilla.javascript.debug.DebugFrame;
|
||||
import org.mozilla.javascript.Context;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Monitors the execution of the JavaScript inside HtmlUnit, and provides debug information
|
||||
* such as call stacks, variables, arguments, etc.
|
||||
*
|
||||
* <h2>Usage</h2>
|
||||
* <p>
|
||||
* When you set a break point in Java code that are directly/indirectly invoked through JavaScript,
|
||||
* use {@link #toString()} to see the JavaScript stack trace (and variables at each stack frame.)
|
||||
* This helps you see where the problem is.
|
||||
*
|
||||
* <p>
|
||||
* TODO: add programmatic break point API, selective method invocation tracing, and
|
||||
* allow arbitrary script evaluation in arbitrary stack frame.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class JavaScriptDebugger implements Debugger {
|
||||
/**
|
||||
* Call stack as a list. The list grows at the end, so the first element in the list
|
||||
* is the oldest stack frame.
|
||||
*/
|
||||
public final List<CallStackFrame> callStack = new ArrayList<CallStackFrame>();
|
||||
|
||||
public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, String source) {
|
||||
}
|
||||
|
||||
public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
|
||||
return new CallStackFrame(this,fnOrScript);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the current call stack into a human readable string.
|
||||
*/
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for( int i=callStack.size()-1; i>=0; i-- )
|
||||
buf.append(callStack.get(i)).append('\n');
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user