JohnMasen / ChakraCore.NET

A dotnet hosting library for chakra (javascript) engine
MIT License
71 stars 22 forks source link

Attach the VS debugger #12

Open DrEight opened 6 years ago

DrEight commented 6 years ago

I've seen that there is a VSCode adapter, but I don't understand how it works and if it works. VSCode apart, is there any ways to attach the old VS debugger to js ?

JohnMasen commented 6 years ago

the build-in vs-code JavaScript debugger works with node.js only, chakracore.net requires its own debugger which is nearly finished. I will add document and demo when it’s done. The following feature is supported:

DrEight commented 6 years ago

I used this launch.json, on your sample ( run script ), but I receive a lot of exceptions that I tried to fix without success for example in the class 'TaskQueueRunner' . My Launch.json : "version": "0.2.0", "configurations": [

    {
        "type": "node",
        "name": "attach to chakra",
        "request": "attach",
        "port": 4711
    }
]

is it correct ?

JohnMasen commented 6 years ago

Run script contains a debug sample under the scripts\debugging folder. Ideally Change the run script launch config to “debugging” in visual studio (not vscode), press f5 , then open the scripts\debugging folder in vs code, and start debug with config “launch” in vscode should work. The check in I committed last night may break the steps above, please use previous version or wait for debug adapter finish.

DrEight commented 6 years ago

no I was not able to make it works. I'll wait for the 'debug adapter'. Do you have any estimation date ? Thank you for the work you are doing.

JohnMasen commented 6 years ago

I plan to finish this feature before 23 April. However I’m baby sitting my 2 yr son these days, hope I can finish it on time.

JohnMasen commented 6 years ago

Hi DrEight, the debugging code is completed, there's still known issues but it works :) I'll publish it to the VSCode market in the next few days. however, you can try it now without the publication is done:

  1. Start "RunScript" with "Debugging" profile, a console window should popup and last line should be "Script ready, Waiting for debugger" image

  2. Open "VSCodeDebugAdapter" folder with VSCode

  3. Start debug with "extension" launch config, a new VSCode window will popup

  4. In the new VSCode window, open folder "source\RunScript\Scripts\Debugging\"

  5. add breakpoint(s) in app.js (known issue: Sometimes, if you don't set breakpoints, the script engine may not start correctly after VSCode is connected with launch command. Workaround: you can restart from step 1 to try again)

  6. Start debug with "ccn Launch" launch config

You should see the break point hit like this image

  1. when the script is finished. hit the [enter] in runscript window to run the js function again, this should trigger the breakpoint once more.
JohnMasen commented 6 years ago

the VSCode debugger is published, just install the extension and ignore step 2,3 in previous post. image

DrEight commented 6 years ago

Thank you it works! Is it possibile also to attach to a running js script ?

JohnMasen commented 6 years ago

Yes, you can comment ln 53 and uncomment ln54 in program.cs (RunScript) to tell the engine start the execution without wait for launch command. then use the "ccn Attach" profile to attach your script. the correct order should be:

  1. start RunScript
  2. start debugging in VSCode with "ccn attach"
  3. press [Enter] in the RunScript window, this will trigger the script run again and you will see the breakpoint(s) hit.
DrEight commented 6 years ago

the 'attach' works in your sample, but I tried with a very basic code like this:

        ChakraRuntime runtime = ChakraRuntime.Create();
        ChakraContext context = runtime.CreateContext(true);

        // debug
        var debugCTS = new System.Threading.CancellationTokenSource();
        var adapter = new VSCodeDebugAdapter(false);//start program, wait for attach command from VSCode
        adapter.OnLaunch += (sender, arguments) => { Console.WriteLine($"Launch requested,arguments={arguments}"); };
        adapter.OnAttach += (sender, arguments) => { Console.WriteLine($"Attach requested,arguments={arguments}"); };
        adapter.OnAdapterMessage += (sender, msg) => { Console.WriteLine(msg); };
        adapter.OnStatusChang += (sender, e) => { Console.WriteLine(e); };
        adapter.RunServer(3515, debugCTS.Token);
        adapter.Init(m_context.ServiceNode.GetService<IRuntimeDebuggingService>());

        string js = System.IO.File.ReadAllText(@"c:\scripts\test.js");
        //context.RunScript(q + " function internalCallback(s, callback) {return s + callback(s)};");

        context.RunScript(js);

        Console.ReadKey(); // wait for the debugger to attach

        var ret = context.GlobalObject.CallFunction<string,string>("MyFunction","hello world");

The VS code it does not attach, but it throws an exception: ' Attached failed, engine is waiting for Launch' Looking in the code it seems like the debugger works only if there is a module, that it is not my case. What am I doing wrong?

JohnMasen commented 6 years ago

you did nothing wrong. the debugger is designed to support module only.

  1. debugger requires set breakpoint after script is compiled, RunScript does not support this.
  2. import/export feature is only available if you load the root script as module
  3. based on my experience, the module system provides better integration advantage.

can I suggest you change your design to use JavaScript module? If you have questions I'm glad to help.

ricklee7679 commented 3 years ago

I am able to debug in VS Code for console application targeting .NET Core 2.1 (similar to the RunScript example project's) , but getting "connect ETIMEDOUT 127.0.0.1:3515", when running the same codes (c#/javascript) in a Universal Windows console app or UWP app. Does VS DebuggerAdapter work with UWP applications? Thanks!