Embedded, Debugger-API: force stop current thread of JS execution

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Embedded, Debugger-API: force stop current thread of JS execution

Benjamin Kircher
Hi there!

tl/dr;
How to forcefully terminate the current thread of JavaScript execution?


I'm currently working on a JS remote debugger engine that is used to debug JS scripts running in an embedded SpiderMonkey. Of course, the debugger engine is using Debugger-API.

Now I want to implement a 'Stop' feature that is whenever a user hits the 'Stop' button in his IDE, the current debuggee should stop its execution, but I fail to see how I would do something like this with the Debugger-API.

Besides Debugger-API, one idea I had in mind is to use JS_SetOperationCallback, or JS_SetInterruptCallback, respectively, to check a flag variable and terminate the script by returning false from the callback but I am really not sure if this is the way to go. Another guess of mine is to look for an "uncatchable" exception that I can throw, or return false from a native function w/o setting an exception.

I'm looking for something like V8::TerminateExecution in SpiderMonkey.

Any advice is greatly appreciated.

BK
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Embedded, Debugger-API: force stop current thread of JS execution

Myk Melez-4
> Benjamin Kircher <mailto:[hidden email]>
> 2017 January 24 at 13:30
> I'm looking for something like V8::TerminateExecution in SpiderMonkey.
I'm unsure if it'll work for your particular use case, but SpiderNode
implements V8::TerminateExecution by calling JS_RequestInterruptCallback
and then returning false from the callback:

https://github.com/mozilla/spidernode/blob/fa6dd41/deps/spidershim/src/v8isolate.cc#L342-L347
https://github.com/mozilla/spidernode/blob/fa6dd41/deps/spidershim/src/v8isolate.cc#L107-L109

-myk

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Embedded, Debugger-API: force stop current thread of JS execution

Shu-yu Guo
In reply to this post by Benjamin Kircher
Hi Benjamin,

Using the Debugger API to terminate execution is easy: return `null` from
any Debugger handler that expects a completion value [1] to be returned.
For example, returning null from a breakpoint handler or an onEnterFrame
will terminate debuggee execution.

[1]
https://developer.mozilla.org/en-US/docs/Tools/Debugger-API/Conventions#completion-values

HTH,

On Tue, Jan 24, 2017 at 1:30 PM, Benjamin Kircher <
[hidden email]> wrote:

> Hi there!
>
> tl/dr;
> How to forcefully terminate the current thread of JavaScript execution?
>
>
> I'm currently working on a JS remote debugger engine that is used to debug
> JS scripts running in an embedded SpiderMonkey. Of course, the debugger
> engine is using Debugger-API.
>
> Now I want to implement a 'Stop' feature that is whenever a user hits the
> 'Stop' button in his IDE, the current debuggee should stop its execution,
> but I fail to see how I would do something like this with the Debugger-API.
>
> Besides Debugger-API, one idea I had in mind is to use
> JS_SetOperationCallback, or JS_SetInterruptCallback, respectively, to check
> a flag variable and terminate the script by returning false from the
> callback but I am really not sure if this is the way to go. Another guess
> of mine is to look for an "uncatchable" exception that I can throw, or
> return false from a native function w/o setting an exception.
>
> I'm looking for something like V8::TerminateExecution in SpiderMonkey.
>
> Any advice is greatly appreciated.
>
> BK
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>



--
       shu
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Embedded, Debugger-API: force stop current thread of JS execution

Benjamin Kircher

> On 25 Jan 2017, at 01:34, Shu-yu Guo <[hidden email]> wrote:
>
> Hi Benjamin,
>
> Using the Debugger API to terminate execution is easy: return `null` from any Debugger handler that expects a completion value [1] to be returned. For example, returning null from a breakpoint handler or an onEnterFrame will terminate debuggee execution.
>
> [1] https://developer.mozilla.org/en-US/docs/Tools/Debugger-API/Conventions#completion-values

Thank you! Exactly what I'm looking for!

It took me some time to find some useful example code out there. Until I looked at the test suite and there it is, blindingly obvious.

This is from js/src/jit-test/tests/debug/Frame-onStep-resumption-03.js:

    // If frame.onStep returns null, the debuggee terminates.

    var g = newGlobal('new-compartment');
    g.eval("function h() { debugger; }");

    var dbg = Debugger(g);
    var hits = 0;
    dbg.onDebuggerStatement = function (frame) {
        hits++;
        if (hits == 1) {
            var rv = frame.eval("h();\n" +
                                "throw 'fail';\n");
            assertEq(rv, null);
        } else {
            frame.older.onStep = function () { return null; };
        }
    };
    g.h();
    assertEq(hits, 2);

BK

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Embedded, Debugger-API: force stop current thread of JS execution

Benjamin Kircher
In reply to this post by Myk Melez-4

> On 24 Jan 2017, at 22:53, Myk Melez <[hidden email]> wrote:
>
>> I'm looking for something like V8::TerminateExecution in SpiderMonkey.
> I'm unsure if it'll work for your particular use case, but SpiderNode implements V8::TerminateExecution by calling JS_RequestInterruptCallback and then returning false from the callback:
>
> https://github.com/mozilla/spidernode/blob/fa6dd41/deps/spidershim/src/v8isolate.cc#L342-L347
> https://github.com/mozilla/spidernode/blob/fa6dd41/deps/spidershim/src/v8isolate.cc#L107-L109
>
> -myk

Didn’t know about SpiderNode before. It is a great resource for comparing SpiderMonkey JS API to V8 API.

Thanks!

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine