ChakraCore.NET icon indicating copy to clipboard operation
ChakraCore.NET copied to clipboard

Attach the VS debugger

Open DrEight opened this issue 7 years ago • 12 comments

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 ?

DrEight avatar Apr 13 '18 13:04 DrEight

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:

  • Attach to host via tcp
  • Wait for host to attach via tcp(useful for uwp)
  • breakpoints
  • step in, step over, continue
  • variant inspector
  • expression evaluation

JohnMasen avatar Apr 15 '18 02:04 JohnMasen

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 ?

DrEight avatar Apr 15 '18 13:04 DrEight

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.

JohnMasen avatar Apr 16 '18 03:04 JohnMasen

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.

DrEight avatar Apr 16 '18 08:04 DrEight

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 avatar Apr 18 '18 06:04 JohnMasen

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 avatar Apr 20 '18 02:04 JohnMasen

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

JohnMasen avatar Apr 22 '18 01:04 JohnMasen

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

DrEight avatar Apr 23 '18 11:04 DrEight

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.

JohnMasen avatar Apr 23 '18 11:04 JohnMasen

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?

DrEight avatar Apr 24 '18 09:04 DrEight

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.

JohnMasen avatar Apr 25 '18 12:04 JohnMasen

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!

ricklee7679 avatar May 22 '21 20:05 ricklee7679