Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for breakpoint debugging #710

Merged
merged 2 commits into from
Aug 1, 2023
Merged

Conversation

ohodson
Copy link
Contributor

@ohodson ohodson commented May 26, 2023

No description provided.

@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch 2 times, most recently from d4e73da to 6586c4d Compare June 5, 2023 16:20
@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch 2 times, most recently from 50fc6e4 to b776b91 Compare June 20, 2023 09:49
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch 5 times, most recently from af925c7 to f5afe97 Compare June 30, 2023 13:40
@ohodson ohodson changed the title Not For Commit: breakpoint debugging prototype Initial support for breakpoint debugging Jun 30, 2023
@ohodson ohodson marked this pull request as ready for review June 30, 2023 13:43
@ohodson ohodson requested review from kentonv and jasnell June 30, 2023 13:44
@ohodson
Copy link
Contributor Author

ohodson commented Jun 30, 2023

PTAL, this is a moderately large change that we'll likely have to iterate on quite a bit.

The major shortcoming here is that this is only worker for single service configs. This should be fixable, but it would be good to get feedback in the interim. The specific example that fails is ssh://git@bitbucket.cfdata.org:7999/~bcoll/workerd-bus-error-10.git

Thanks.

@ohodson ohodson changed the title Initial support for breakpoint debugging Support for breakpoint debugging Jul 3, 2023
@ohodson
Copy link
Contributor Author

ohodson commented Jul 3, 2023

The major shortcoming here is that this is only worker for single service configs. This should be fixable, but it would be good to get feedback in the interim. The specific example that fails is ssh://git@bitbucket.cfdata.org:7999/~bcoll/workerd-bus-error-10.git

This is addressed in the second commit. Wrangler does not expect to package multiple user written workers in a config, but may in practice (e.g. code that wrangler itself adds). With the second commit chrome devtools would be able to debug any code referenced by the config. Wrangler devtools only wants to be able to debug the user written workers (except if they need to debug their stuff).

src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch 2 times, most recently from 96940d3 to dea77d4 Compare July 5, 2023 14:40
Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just started looking at this, realize I need more time than I have now, so posting the couple comments I already wrote but I'll try to look at the whole thing either later today or tomorrow.

src/workerd/server/server.c++ Outdated Show resolved Hide resolved
src/workerd/server/server.c++ Show resolved Hide resolved
src/workerd/server/server.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(still reading, but posting a batch before going to more meetings...)

src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch 4 times, most recently from 512bd36 to 1063006 Compare July 6, 2023 14:47
Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some review comments that seem to have disappeared, but I also notice that github says I have a pending review, but it says there are no comments, but maybe if I post it anyway they will come back? Trying...

@kentonv
Copy link
Member

kentonv commented Jul 14, 2023

Sadly, it appears my comments were lost. Ugh.

src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch 2 times, most recently from a4d477b to 6409cca Compare July 14, 2023 19:59
@ohodson
Copy link
Contributor Author

ohodson commented Jul 21, 2023

@kentonv, friendly ping, last comment incorporated on 7/14, thanks.

Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you've tested this running in the fiddle service (internal codebase)? That should be a pretty easy way to verify it works with the internal threading model, in addition to the workerd model.

src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Show resolved Hide resolved
@kentonv
Copy link
Member

kentonv commented Jul 24, 2023

(Sorry for the slow review, this fell off my radar last week somehow.)

@paplco
Copy link

paplco commented Jul 28, 2023

awesome, approved!
hopefully this fixes setting breakpoints in vscode
is there a target release to include this PR?
Thanks!

@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch from 6409cca to f937d74 Compare July 31, 2023 10:38
@ohodson
Copy link
Contributor Author

ohodson commented Jul 31, 2023

I assume you've tested this running in the fiddle service (internal codebase)? That should be a pretty easy way to verify it works with the internal threading model, in addition to the workerd model.

Yes, you can set breakpoints and single step. Logging to console and heap querying appear to still work as before.

@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch from f937d74 to f9aa9e6 Compare July 31, 2023 14:09
try {
auto lockedOutgoingQueue = outgoingQueue.lockExclusive();
if (lockedOutgoingQueue->status == MessageQueue::Status::CLOSED) {
co_await webSocket.close(1000, "client closed connection");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the behavior changed slightly in the coroutine conversion, previously we would send any remaining messages before closing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, fixed, thanks.

for (;;) {
co_await outgoingQueueNotifier->awaitNotification();
try {
auto lockedOutgoingQueue = outgoingQueue.lockExclusive();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to drop this lock before co_awaiting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch from f9aa9e6 to fb85369 Compare August 1, 2023 08:52
This change places the InspectorService in a separate thread that
manages communication with over the websocket to the inspector.

The change also adds support for runMessageLoopOnPause() and
quitMessageLoopOnPause() to support breakpoints and debugger
break statements.

There is also refactoring of the CDP message handling code so it can
be called with or without the isolate lock held.

This requires workerd to run with the command-line flag -i to turn
on inspector support.

This change only works for single service configurations. Support for
multi service configurations to follow.

To try this out using samples/helloworld as an example:

1) Edit "samples/helloworld/worker.js" and add a debugger statement
   to the handle(request) method.
2) Open workerd in VSCode, select 'workerd with inspector enabled (dbg)'
   as the Run and Debug Target panel. Hit F5 to run and select
   `samples/helloworld/config.capnp` as the config to use.
3) Open devtools in Chrome using either:
   * https://devtools.devprod.cloudflare.dev/js_app?ws=localhost:9229/main
   * chrome:://inspect
4) On the command-line run, `curl http://localhost:8080/`
5) Devtools should break into the running worker.

Bug: #371
Test: manual
Test: existing internal ew tests do not break
If there are multiple service workers in a config, expose them to
devtools. This is largely just for Chrome devtools since wrangler
does not expect to generate more than one worker per config that
might need to be debugged.

Test: manual using chrome devtools and https://bitbucket.cfdata.org/users/bcoll/repos/workerd-bus-error-10/browse
@ohodson ohodson force-pushed the orion/breakpoint-debugging-pt2 branch from fb85369 to 45a8779 Compare August 1, 2023 09:23
@ohodson ohodson merged commit eba4bea into main Aug 1, 2023
7 checks passed
@ohodson ohodson deleted the orion/breakpoint-debugging-pt2 branch August 1, 2023 09:58
mrbbot added a commit to cloudflare/miniflare that referenced this pull request Aug 15, 2023
With cloudflare/workerd#710, `workerd` supports breakpoint debugging!
Support for this in Miniflare just worked, assuming you were using a
plain JavaScript worker, or you had inline source maps. `workerd`
doesn't know where workers are located on disk, it just knows files'
locations relative to each other. This means it's unable to resolve
locations of corresponding linked `.map` files in `sourceMappingURL`
comments.

Miniflare _does_ have this information though. This change detects
linked source maps and rewrites `sourceMappingURL` comments to `http`
URLs pointing to Miniflare's loopback server. This then looks for the
source map relative to the known on-disk source location. Source
maps' `sourceRoot` attributes are updated to ensure correct locations
are displayed in DevTools.

**This enables breakpoint debugging for compiled TypeScript with
linked source maps!** 🎉

Closes DEVX-872
mrbbot added a commit to cloudflare/miniflare that referenced this pull request Aug 16, 2023
With cloudflare/workerd#710, `workerd` supports breakpoint debugging!
Support for this in Miniflare just worked, assuming you were using a
plain JavaScript worker, or you had inline source maps. `workerd`
doesn't know where workers are located on disk, it just knows files'
locations relative to each other. This means it's unable to resolve
locations of corresponding linked `.map` files in `sourceMappingURL`
comments.

Miniflare _does_ have this information though. This change detects
linked source maps and rewrites `sourceMappingURL` comments to `http`
URLs pointing to Miniflare's loopback server. This then looks for the
source map relative to the known on-disk source location. Source
maps' `sourceRoot` attributes are updated to ensure correct locations
are displayed in DevTools.

**This enables breakpoint debugging for compiled TypeScript with
linked source maps!** 🎉

Closes DEVX-872
mrbbot added a commit to cloudflare/workers-sdk that referenced this pull request Oct 31, 2023
With cloudflare/workerd#710, `workerd` supports breakpoint debugging!
Support for this in Miniflare just worked, assuming you were using a
plain JavaScript worker, or you had inline source maps. `workerd`
doesn't know where workers are located on disk, it just knows files'
locations relative to each other. This means it's unable to resolve
locations of corresponding linked `.map` files in `sourceMappingURL`
comments.

Miniflare _does_ have this information though. This change detects
linked source maps and rewrites `sourceMappingURL` comments to `http`
URLs pointing to Miniflare's loopback server. This then looks for the
source map relative to the known on-disk source location. Source
maps' `sourceRoot` attributes are updated to ensure correct locations
are displayed in DevTools.

**This enables breakpoint debugging for compiled TypeScript with
linked source maps!** 🎉

Closes DEVX-872
mrbbot added a commit to cloudflare/workers-sdk that referenced this pull request Nov 1, 2023
With cloudflare/workerd#710, `workerd` supports breakpoint debugging!
Support for this in Miniflare just worked, assuming you were using a
plain JavaScript worker, or you had inline source maps. `workerd`
doesn't know where workers are located on disk, it just knows files'
locations relative to each other. This means it's unable to resolve
locations of corresponding linked `.map` files in `sourceMappingURL`
comments.

Miniflare _does_ have this information though. This change detects
linked source maps and rewrites `sourceMappingURL` comments to `http`
URLs pointing to Miniflare's loopback server. This then looks for the
source map relative to the known on-disk source location. Source
maps' `sourceRoot` attributes are updated to ensure correct locations
are displayed in DevTools.

**This enables breakpoint debugging for compiled TypeScript with
linked source maps!** 🎉

Closes DEVX-872
mrbbot added a commit to cloudflare/workers-sdk that referenced this pull request Nov 1, 2023
With cloudflare/workerd#710, `workerd` supports breakpoint debugging!
Support for this in Miniflare just worked, assuming you were using a
plain JavaScript worker, or you had inline source maps. `workerd`
doesn't know where workers are located on disk, it just knows files'
locations relative to each other. This means it's unable to resolve
locations of corresponding linked `.map` files in `sourceMappingURL`
comments.

Miniflare _does_ have this information though. This change detects
linked source maps and rewrites `sourceMappingURL` comments to `http`
URLs pointing to Miniflare's loopback server. This then looks for the
source map relative to the known on-disk source location. Source
maps' `sourceRoot` attributes are updated to ensure correct locations
are displayed in DevTools.

**This enables breakpoint debugging for compiled TypeScript with
linked source maps!** 🎉

Closes DEVX-872
mrbbot added a commit to cloudflare/workers-sdk that referenced this pull request Nov 1, 2023
With cloudflare/workerd#710, `workerd` supports breakpoint debugging!
Support for this in Miniflare just worked, assuming you were using a
plain JavaScript worker, or you had inline source maps. `workerd`
doesn't know where workers are located on disk, it just knows files'
locations relative to each other. This means it's unable to resolve
locations of corresponding linked `.map` files in `sourceMappingURL`
comments.

Miniflare _does_ have this information though. This change detects
linked source maps and rewrites `sourceMappingURL` comments to `http`
URLs pointing to Miniflare's loopback server. This then looks for the
source map relative to the known on-disk source location. Source
maps' `sourceRoot` attributes are updated to ensure correct locations
are displayed in DevTools.

**This enables breakpoint debugging for compiled TypeScript with
linked source maps!** 🎉

Closes DEVX-872
harrishancock added a commit that referenced this pull request Jun 6, 2024
V8's profiler does not expect isolates to be locked from different threads. Pull request #710 moved inspector protocol handling to a separate thread, meaning profiling was started and stopped on the inspector thread, not the Worker's request connection thread. If I remember right, the result of this is that the profiler ends up sampling the inspector thread, which doesn't normally run JavaScript, and causes a bunch of "(program)" placeholder spans to show up in the profile.

We previously discovered this issue with our internal runtime, which has always been heavily multi-threaded, and patched around the problem. This commit copies the two relevant patches from our internal repo.

Fixes #1754.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants