[quickjs-devel] Re: QuickJS Debugger implementation for VSCode

  • From: Koushik Dutta <koush@xxxxxxxxxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Fri, 8 Nov 2019 09:26:30 -0800

Thanks, I'll grab it from your fork and merge it.

Regarding filenames:
If you're launching from VS Code, the path normalization of the "program"
entry can probably happen there. There may be a bug in the extension where
the path is using the wrong slashes for the host environment. As far as I
can tell, quickjs uses the path given to it, verbatim, so it's likely a bug
a malformed path is coming from the extension.

In any case, normalization and path resolution needs to happen in the
extension: the debug server may be on a remote device, with a different
remote path. Such as when debugging a mobile device, which is my use case.
That remote path would need to be resolved to a local path on the debug
client in VS Code. Also, when using a tool like webpack or transpiling with
babel or typescript, the source being used by qjs will not be the actual
source being debugged from the development environment. The paths will need
to be resolved via a source map. This also needs to happen in the VSCode
extension.

I'll be adding source map support to the extension, as I need it for my
project, and I'll reevaluate how the path resolution works currenly. Right
now, the debug server does not send any events for source files being
loaded, but I suspect that will be necessary.

Koush

On Fri, Nov 8, 2019 at 4:38 AM Etienne Cochard <etienne.cochard@xxxxxxxxx>
wrote:

really nice job.
i have done the windows transport port, if you need.
you will face a small problem under windows, the debugger will send you
breakpoints in the for
C:\path\to\\the\debug/file.js

C (drive letter) in upper case
mix between / & \

you will have to normalize paths in js_debugger_check_breakpoint function.






Le jeu. 7 nov. 2019 à 10:04, Koushik Dutta <koush@xxxxxxxxxxxxxxxx> a
écrit :

Thanks for the excellent QuickJS project.

I spent the last couple days implementing for a QuickJS runtime debugger.
The debugger front end is VS Code. It supports all the features you'd
expect from a debugger: javascript breakpoints (and exception breakpoints),
evaluation, stepping, etc.

Fork of QuickJS with the necessary changes:
https://github.com/koush/quickjs

VSCode Extension:
https://github.com/koush/vscode-quickjs-debug

VSCode Marketplace Link:
https://marketplace.visualstudio.com/items?itemName=koush.quickjs-debug

Usage instructions are in the marketplace link. It will launch qjs with a
test.js script.

Technical details:

VS Code was chosen because it has a lightweight protocol that allows
piecemeal feature support. The Chrome debugger protocol was considered, but
it requires websockets and has a whole lot of other baggage. One could
potentially write a CLI client that uses the VS Code protocol.

The debugger code has been split into separate files.

quickjs.c:
Contains the necessary hooks and changes in the runtime. The changes are
nearly unintrusive. There's a few fields that were necessary on JSContext
and JSFunctionBytecode.
Most of the code lives at the bottom of the file. There are a number of
internal APIs necessary for the functional debugger: getting a list of
closure and local variables, dumping the stack frames, and evaluating an
expression at any stack frame. A roll up of the changes can be seen here:

https://github.com/koush/quickjs/compare/33c91ce2bc1c9422d32f5f26ccef56ff6b4d9bda..master#diff-8561242cf01b9394bd9d00e57a0f7db3

quickjs-debugger.c
This contains the debug session management and wire protocol to talk to
VS Code.
https://github.com/koush/quickjs/blob/master/quickjs-debugger.c

quickjs-debugger-transport-unix.c:
This is the default transport implementation that uses sockets. Other
transports could be swapped in by implementing the 5 transport_* functions.

https://github.com/koush/quickjs/blob/master/quickjs-debugger-transport-unix.c
Currently no windows transport is implemented, but I'll do that within
the next couple days.

Debugging embedded QuickJS notes:
Debugger connection initiation is done with
js_debugger_connect(JSContext *, const char *address)
Where <address> is the "address:port" value that will connect to the VS
Code extension waiting for incoming QuickJS debug sessions.
Connection initiation can also be done automatically by setting the
QUICKJS_DEBUG_ADDRESS environment variable to "address:port".

For example:
QUICKJS_DEBUG_ADDRESS=localhost:5555 ./qjs test.js
or
QUICKJS_DEBUG_ADDRESS=localhost:5555 ./my_program_with_qjs_embedded

I'd be happy to work with Fabrice and Charlie to get these changes in
quickjs.c approved, if possible. At the very least, the hooks necessary for
a debugger. Specifically, these functions need to live in quickjs.c due to
them needing to access internal data structures:
https://github.com/koush/quickjs/blob/master/quickjs-debugger.h#L63

Thanks. Feel free to open github issues for questions or help on getting
the debugger working in your project.

Koush


Other related posts: