microsoft / vscode-js-debug

A DAP-compatible JavaScript debugger. Used in VS Code, VS, + more
MIT License
1.67k stars 283 forks source link

Cannot set breakpoints in c++ code transpiled to wasm using emscripten #2099

Closed matusthemostlygreat closed 1 month ago

matusthemostlygreat commented 1 month ago

I have a c++ project built into wasm using emscripten, and I have the extension installed and built using -g. I can set breakpoints in javascript, and it does break on javascript exceptions. However, it does not break on c++ breakpoints, so I am unable to step through my code.

Been following this page: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_debugging-webassembly

Can you point me in the right direction please? What do I need to look at to investigate why it's not working? debuggerbug

This is my launch json

{ "configurations": [ { "type": "chrome", "name": "visualizer", "request": "launch", "url": "http://localhost:3001/visualizer.html" } ] }

connor4312 commented 1 month ago

Please collect a log file:

If you're able to, add "trace": true to your launch.json and reproduce the issue. The location of the log file on your disk will be written to the Debug Console. Share that with us.

⚠️ This log file will not contain source code, but will contain file paths. You can drop it into https://microsoft.github.io/vscode-pwa-analyzer/index.html to see what it contains. If you'd rather not share the log publicly, you can email it to connor@xbox.com

Also bear in mind if you expect to be able to break on exceptions you need to enable exception support in emscripten with -fexceptions https://emscripten.org/docs/porting/exceptions.html

matusthemostlygreat commented 1 month ago

vscode-debugadapter-1ab06081.json.gz

connor4312 commented 1 month ago

In that log, it looks like you don't have any breakpoints set up. But I see the sources get parsed correctly, and in your screenshot above it looks like they got bound successfully

Image

matusthemostlygreat commented 1 month ago

i disabled them cause i didn't think the log was being generated correctly. here is a fresh run with a single breakpoint set and matching log file.

image vscode-debugadapter-cab056d0.json.gz

matusthemostlygreat commented 1 month ago

as you can see in the debug output spew, execution was not stopped at the breakpoint. the red text was output as a result of a failure inside app.Run()

connor4312 commented 1 month ago

It looks like it was bound, but execution may have passed that point before the breakpoint was set. Did you refresh the page to try to hit the breakpoint?

Breakpoints in WebAssembly code are resolved asynchronously, so breakpoints hit early on in a program's lifecycle may be missed. There are plans to fix this in the future. If you're debugging in a browser, you can refresh the page for your breakpoint to be hit. If you're in Node.js, you can add an artificial delay, or set another breakpoint, after your WebAssembly module is loaded but before your desired breakpoint is hit.

matusthemostlygreat commented 1 month ago

thanks, that was it! if i set a breakpoint in the main generated .js file at function run() where it calls preRun(), after hitting that js breakpoint i can then hit my c++ breakpoint in main().

connor4312 commented 1 month ago

Yea, that's how to do it. Unfortunately in V8 we can't set an 'entrypoint' breakpoint when WASM modules are parsed like we can for normal scripts (which we use to set breakpoints when a script is sourcemapped.) There's an upstream bug for it, and it should 'just work' once that's fixed.

connor4312 commented 1 month ago

Ended up making a workaround in https://github.com/microsoft/vscode-js-debug/pull/2102, will arrive on nightly this evening.