Remote debugging of nashorn scripts with NetBeans IDE using "debugger" statements

You can debug nashorn scripts - even if you do not create any NetBeans project or even open the JavaScript files in NetBeans! You just need to connect NetBeans debugger to a remote Java process that evaluates Nashorn scripts!

You can use ECMAScript debugger statements to stop the script execution at the places of interest.

Start the process running your script with debugger agent

I'm using "jjs" tool to run a sample script - but you may use any java application that evaluates Nashorn scripts via javax.script API.

File: test.js

print("in test script");
debugger;
var obj = { foo: 2 };
obj.foo++;
print(obj.foo);

jjs command with debugger turned on


jjs -J-agentlib:jdwp=server=y,suspend=y,transport=dt_socket test
Listening for transport dt_socket at address: 50349

Set breakpoint in nashorn's internal DEBUGGER method

Nashorn compiles ECMAScript "debugger" statements as calls to jdk.nashorn.internal.runtime.ScriptRuntime.DEBUGGER method.

Use Debug->New Breakpoint... menu to create a new breakpoint. Add breakpoint in jdk.nashorn.internal.runtime.ScriptRuntime.DEBUGGER method.

Attach NetBeans to remote process

Use Debug->Attach Debugger... menu to attach NetBeans to the debuggee process

Getting to 'debugger' statements

Whenever script executes ECMAScript "debugger" statement, breakpoint hits into that nashorn DEBUGGER method. On pressing F8, execution control reaches the "debugger" statement in script file. NetBeans automatically opens the script file and stops at the "debugger" statement!

Comments:

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search


Archives
« February 2017
SunMonTueWedThuFriSat
   
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
    
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder