Bug 595243 - Expose debugMode to JSD. Relanding test bustage fixed up. r=gal.

This commit is contained in:
Robert Sayre 2010-10-30 12:13:02 -04:00
parent a6295f5897
commit 7b6b35d47a
14 changed files with 201 additions and 61 deletions

View File

@ -993,10 +993,6 @@ nsJSContext::DOMOperationCallback(JSContext *cx)
if (NS_SUCCEEDED(rv)) {
jsds->GetDebuggerHook(getter_AddRefs(jsdHook));
jsds->GetIsOn(&jsds_IsOn);
if (jsds_IsOn) { // If this is not true, the next call would start jsd...
rv = jsds->OnForRuntime(cx->runtime);
jsds_IsOn = NS_SUCCEEDED(rv);
}
}
// If there is a debug handler registered for this runtime AND

View File

@ -72,12 +72,13 @@ interface jsdIScript;
interface jsdIValue;
interface jsdIObject;
interface jsdIProperty;
interface jsdIActivationCallback;
/**
* Debugger service. It's not a good idea to have more than one active client of
* the debugger service.
*/
[scriptable, uuid(dc0a24db-f8ac-4889-80d0-6016545a2dda)]
[scriptable, uuid(01769775-c77c-47f9-8848-0abbab404215)]
interface jsdIDebuggerService : nsISupports
{
/** Internal use only. */
@ -216,19 +217,34 @@ interface jsdIDebuggerService : nsISupports
*/
readonly attribute boolean isOn;
/**
* Synchronous activation of the debugger is no longer supported,
* and will throw an exception.
*/
void on ();
/**
* Turn on the debugger. This function should only be called from JavaScript
* code. The debugger will be enabled on the runtime the call is made on,
* as determined by nsIXPCNativeCallContext.
*/
void on ();
/**
* Turn on the debugger for a given runtime.
*
* @param rt The runtime you want to debug. You cannot turn the debugger
* on for multiple runtimes.
* The debugger will be activated asynchronously, because there can be no JS
* on the stack when code is to be re-compiled for debug mode.
*/
[noscript] void onForRuntime (in JSRuntime rt);
void asyncOn (in jsdIActivationCallback callback);
/**
* Called by nsIXPConnect after it's had a chance to recompile for
* debug mode.
*/
[noscript] void activateDebugger(in JSRuntime rt);
/**
* Recompile all active scripts in the runtime for debugMode.
*/
[noscript] void recompileForDebugMode(in JSRuntime rt, in PRBool mode);
/**
* Turn the debugger off. This will invalidate all of your jsdIEphemeral
* derived objects, and clear all of your breakpoints. In theory you
@ -457,6 +473,15 @@ interface jsdIFilter : nsISupports
attribute unsigned long endLine;
};
/**
* Notify client code that debugMode has been activated.
*/
[scriptable, uuid(6da7f5fb-3a84-4abe-9e23-8b2045960732)]
interface jsdIActivationCallback : nsISupports
{
void onDebuggerActivated ();
};
/**
* Pass an instance of one of these to jsdIDebuggerService::enterNestedEventLoop.
*/

View File

@ -585,11 +585,6 @@ jsd_NewScriptHookProc(
if( JSD_IS_DANGEROUS_THREAD(jsdc) )
return;
#ifdef LIVEWIRE
if( 1 == lineno )
jsdlw_PreLoadSource(jsdc, LWDBG_GetCurrentApp(), filename, JS_TRUE );
#endif
JSD_LOCK_SCRIPTS(jsdc);
jsdscript = _newJSDScript(jsdc, cx, script, fun);
JSD_UNLOCK_SCRIPTS(jsdc);
@ -611,7 +606,7 @@ jsd_NewScriptHookProc(
if( hook )
hook(jsdc, jsdscript, JS_TRUE, hookData);
}
}
void
jsd_DestroyScriptHookProc(

View File

@ -2396,6 +2396,12 @@ jsdService::GetIsOn (PRBool *_rval)
NS_IMETHODIMP
jsdService::On (void)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
jsdService::AsyncOn (jsdIActivationCallback *activationCallback)
{
nsresult rv;
@ -2410,18 +2416,32 @@ jsdService::On (void)
JSContext *cx;
rv = cc->GetJSContext (&cx);
if (NS_FAILED(rv)) return rv;
mActivationCallback = activationCallback;
return OnForRuntime(JS_GetRuntime (cx));
return xpc->SetDebugModeWhenPossible(PR_TRUE);
}
NS_IMETHODIMP
jsdService::OnForRuntime (JSRuntime *rt)
jsdService::RecompileForDebugMode (JSRuntime *rt, JSBool mode) {
JSContext *cx;
JSContext *iter = NULL;
while ((cx = JS_ContextIterator (rt, &iter))) {
JS_SetDebugMode(cx, mode);
}
return NS_OK;
}
NS_IMETHODIMP
jsdService::ActivateDebugger (JSRuntime *rt)
{
if (mOn)
return (rt == mRuntime) ? NS_OK : NS_ERROR_ALREADY_INITIALIZED;
mRuntime = rt;
RecompileForDebugMode(rt, JS_TRUE);
if (gLastGCProc == jsds_GCCallbackProc)
/* condition indicates that the callback proc has not been set yet */
@ -2471,6 +2491,10 @@ jsdService::OnForRuntime (JSRuntime *rt)
#ifdef DEBUG
printf ("+++ JavaScript debugging hooks installed.\n");
#endif
if (mActivationCallback)
return mActivationCallback->OnDebuggerActivated();
return NS_OK;
}
@ -2521,6 +2545,13 @@ jsdService::Off (void)
printf ("+++ JavaScript debugging hooks removed.\n");
#endif
nsresult rv;
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
if (NS_FAILED(rv))
return rv;
xpc->SetDebugModeWhenPossible(PR_FALSE);
return NS_OK;
}
@ -2969,7 +3000,7 @@ jsdService::SetErrorHook (jsdIErrorHook *aHook)
mErrorHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3013,7 +3044,7 @@ jsdService::SetDebugHook (jsdIExecutionHook *aHook)
mDebugHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3041,7 +3072,7 @@ jsdService::SetDebuggerHook (jsdIExecutionHook *aHook)
mDebuggerHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3069,7 +3100,7 @@ jsdService::SetInterruptHook (jsdIExecutionHook *aHook)
mInterruptHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3097,7 +3128,7 @@ jsdService::SetScriptHook (jsdIScriptHook *aHook)
mScriptHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3125,7 +3156,7 @@ jsdService::SetThrowHook (jsdIExecutionHook *aHook)
mThrowHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3153,7 +3184,7 @@ jsdService::SetTopLevelHook (jsdICallHook *aHook)
mTopLevelHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3181,7 +3212,7 @@ jsdService::SetFunctionHook (jsdICallHook *aHook)
mFunctionHook = aHook;
/* if the debugger isn't initialized, that's all we can do for now. The
* OnForRuntime() method will do the rest when the coast is clear.
* ActivateDebugger() method will do the rest when the coast is clear.
*/
if (!mCx || mPauseLevel)
return NS_OK;
@ -3274,7 +3305,7 @@ jsdASObserver::Observe (nsISupports *aSubject, const char *aTopic,
if (NS_FAILED(rv))
return rv;
rv = jsds->OnForRuntime(rt);
rv = jsds->ActivateDebugger(rt);
if (NS_FAILED(rv))
return rv;

View File

@ -305,7 +305,7 @@ class jsdService : public jsdIDebuggerService
nsCOMPtr<jsdIExecutionHook> mThrowHook;
nsCOMPtr<jsdICallHook> mTopLevelHook;
nsCOMPtr<jsdICallHook> mFunctionHook;
nsCOMPtr<jsdIActivationCallback> mActivationCallback;
};
#endif /* JSDSERVICE_H___ */

View File

@ -16,8 +16,39 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=507448
</div>
<pre id="test">
<script>
function f() {}
function g(a,b) {}
function h(me, too, here) { var x = 1; }
function annoying(a, b, a, b, b, a) {}
function manyLocals(a, b, c, d, e, f, g, h, i, j, k, l, m) {
var n, o, p, q, r, s, t, u, v, w, x, y, z;
}
</script>
<script>
function testJSD() {
ok(jsd.isOn, "JSD needs to be running for this test.");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
assertArraysEqual(jsd.wrapValue(f).script.getParameterNames(), []);
assertArraysEqual(jsd.wrapValue(g).script.getParameterNames(), ["a", "b"]);
assertArraysEqual(jsd.wrapValue(h).script.getParameterNames(), ["me", "too", "here"]);
assertArraysEqual(jsd.wrapValue(annoying).script.getParameterNames(),
["a", "b", "a", "b", "b", "a"]);
assertArraysEqual(jsd.wrapValue(manyLocals).script.getParameterNames(),
"abcdefghijklm".split(""));
if (!jsdOnAtStart) {
// turn JSD off if it wasn't on when this test started
jsd.off();
ok(!jsd.isOn, "JSD shouldn't be running at the end of this test.");
}
SimpleTest.finish();
}
</script>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
/** Test for Bug 507448 **/
function assertArraysEqual(arr1, arr2) {
is(arr1.length, arr2.length, "Lengths not equal");
@ -34,35 +65,20 @@ netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var jsdIDebuggerService = Components.interfaces.jsdIDebuggerService;
var jsd = Components.classes['@mozilla.org/js/jsd/debugger-service;1']
.getService(jsdIDebuggerService);
var jsdOn = jsd.isOn;
if (!jsdOn) {
jsd.on();
ok(jsd.isOn, "JSD should be running.");
}
</script>
<script>
function f() {}
function g(a,b) {}
function h(me, too, here) { var x = 1; }
function annoying(a, b, a, b, b, a) {}
function manyLocals(a, b, c, d, e, f, g, h, i, j, k, l, m) {
var n, o, p, q, r, s, t, u, v, w, x, y, z;
}
</script>
<script>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
assertArraysEqual(jsd.wrapValue(f).script.getParameterNames(), []);
assertArraysEqual(jsd.wrapValue(g).script.getParameterNames(), ["a", "b"]);
assertArraysEqual(jsd.wrapValue(h).script.getParameterNames(), ["me", "too", "here"]);
assertArraysEqual(jsd.wrapValue(annoying).script.getParameterNames(),
["a", "b", "a", "b", "b", "a"]);
assertArraysEqual(jsd.wrapValue(manyLocals).script.getParameterNames(),
"abcdefghijklm".split(""));
if (!jsdOn) {
jsd.off();
ok(!jsd.isOn, "JSD shouldn't be running anymore.");
}
var jsdOnAtStart = jsd.isOn;
if (jsdOnAtStart) {
testJSD();
} else {
jsd.asyncOn(
{
debuggerActivated: function() {
testJSD();
}
}
);
}
</script>
</pre>
</body>

View File

@ -653,6 +653,9 @@ JSRuntime::init(uint32 maxbytes)
if (!debuggerLock)
return false;
#endif
debugMode = JS_FALSE;
return propertyTree.init() && js_InitThreads(this);
}

View File

@ -1380,6 +1380,11 @@ struct JSRuntime {
/* Per runtime debug hooks -- see jsprvtd.h and jsdbgapi.h. */
JSDebugHooks globalDebugHooks;
/*
* Right now, we only support runtime-wide debugging.
*/
JSBool debugMode;
#ifdef JS_TRACER
/* True if any debug hooks not supported by the JIT are enabled. */
bool debuggerInhibitsJIT() const {

View File

@ -54,7 +54,7 @@ using namespace js;
using namespace js::gc;
JSCompartment::JSCompartment(JSRuntime *rt)
: rt(rt), principals(NULL), data(NULL), marked(false), debugMode(false),
: rt(rt), principals(NULL), data(NULL), marked(false), debugMode(rt->debugMode),
anynameObject(NULL), functionNamespaceObject(NULL)
{
JS_INIT_CLIST(&scripts);

View File

@ -108,6 +108,12 @@ IsScriptLive(JSContext *cx, JSScript *script)
}
#endif
JS_PUBLIC_API(void)
JS_SetRuntimeDebugMode(JSRuntime *rt, JSBool debug)
{
rt->debugMode = debug;
}
JS_FRIEND_API(JSBool)
js_SetDebugMode(JSContext *cx, JSBool debug)
{

View File

@ -49,6 +49,13 @@
JS_BEGIN_EXTERN_C
/*
* Currently, we only support runtime-wide debugging. In the future, we should
* be able to support compartment-wide debugging.
*/
extern JS_PUBLIC_API(void)
JS_SetRuntimeDebugMode(JSRuntime *rt, JSBool debug);
/*
* Debug mode is a compartment-wide mode that enables a debugger to attach
* to and interact with running methodjit-ed frames. In particular, it causes

View File

@ -399,7 +399,7 @@ interface nsIXPCFunctionThisTranslator : nsISupports
{ 0xbd, 0xd6, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
%}
[uuid(fb780ace-dced-432b-bb82-8df7d4f919c8)]
[uuid(c825b64b-d537-4e53-822e-547049aae9c9)]
interface nsIXPConnect : nsISupports
{
%{ C++
@ -827,4 +827,13 @@ interface nsIXPConnect : nsISupports
*/
[noscript,notxpcom] void getCaller(out JSContextPtr aJSContext,
out JSObjectPtr aObject);
/**
* When we place the browser in JS debug mode, there can't be any
* JS on the stack. This is because we currently activate debugMode
* on all scripts in the JSRuntime when the debugger is activated.
* This method will turn debug mode on or off when the context
* stack reaches zero length.
*/
[noscript] void setDebugModeWhenPossible(in PRBool mode);
};

View File

@ -61,6 +61,8 @@
#include "WrapperFactory.h"
#include "AccessCheck.h"
#include "jsdIDebuggerService.h"
NS_IMPL_THREADSAFE_ISUPPORTS6(nsXPConnect,
nsIXPConnect,
nsISupportsWeakReference,
@ -72,6 +74,8 @@ NS_IMPL_THREADSAFE_ISUPPORTS6(nsXPConnect,
nsXPConnect* nsXPConnect::gSelf = nsnull;
JSBool nsXPConnect::gOnceAliveNowDead = JS_FALSE;
PRUint32 nsXPConnect::gReportAllJSExceptions = 0;
JSBool nsXPConnect::gDebugMode = JS_FALSE;
JSBool nsXPConnect::gDesiredDebugMode = JS_FALSE;
// Global cache of the default script security manager (QI'd to
// nsIScriptSecurityManager)
@ -2407,6 +2411,30 @@ nsXPConnect::Peek(JSContext * *_retval)
return data->GetJSContextStack()->Peek(_retval);
}
void
nsXPConnect::CheckForDebugMode(JSRuntime *rt) {
if (gDebugMode != gDesiredDebugMode) {
nsresult rv;
const char jsdServiceCtrID[] = "@mozilla.org/js/jsd/debugger-service;1";
nsCOMPtr<jsdIDebuggerService> jsds = do_GetService(jsdServiceCtrID, &rv);
if (NS_SUCCEEDED(rv)) {
if (gDesiredDebugMode == PR_FALSE) {
rv = jsds->RecompileForDebugMode(rt, PR_FALSE);
} else {
rv = jsds->ActivateDebugger(rt);
}
}
if (NS_SUCCEEDED(rv)) {
JS_SetRuntimeDebugMode(rt, gDesiredDebugMode);
gDebugMode = gDesiredDebugMode;
} else {
// if the attempt failed, cancel the debugMode request
gDesiredDebugMode = gDebugMode;
}
}
}
/* JSContext Pop (); */
NS_IMETHODIMP
nsXPConnect::Pop(JSContext * *_retval)
@ -2432,6 +2460,15 @@ nsXPConnect::Push(JSContext * cx)
if(!data)
return NS_ERROR_FAILURE;
PRInt32 count;
nsresult rv;
rv = data->GetJSContextStack()->GetCount(&count);
if (NS_FAILED(rv))
return rv;
if (count == 0)
CheckForDebugMode(mRuntime->GetJSRuntime());
return data->GetJSContextStack()->Push(cx);
}
@ -2539,6 +2576,13 @@ nsXPConnect::GetCaller(JSContext **aJSContext, JSObject **aObj)
*aObj = ccx->GetFlattenedJSObject();
}
NS_IMETHODIMP
nsXPConnect::SetDebugModeWhenPossible(PRBool mode)
{
gDesiredDebugMode = mode;
return NS_OK;
}
/* These are here to be callable from a debugger */
JS_BEGIN_EXTERN_C
JS_EXPORT_API(void) DumpJSStack()

View File

@ -626,6 +626,9 @@ private:
nsCOMPtr<nsIXPCScriptable> mBackstagePass;
static PRUint32 gReportAllJSExceptions;
static JSBool gDebugMode;
static JSBool gDesiredDebugMode;
static inline void CheckForDebugMode(JSRuntime *rt);
public:
static nsIScriptSecurityManager *gScriptSecurityManager;