aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-08-19 22:07:35 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-08-19 22:07:35 -0700
commitd432dad666293d5cef02774d72b44f6a74d9d966 (patch)
treec8d8014093a8cb0c4fbc2de2d1e743b061033dde
parenteec5bfb23a0fc227842584bd0aa8d9b82c2dded0 (diff)
downloadbun-d432dad666293d5cef02774d72b44f6a74d9d966.tar.gz
bun-d432dad666293d5cef02774d72b44f6a74d9d966.tar.zst
bun-d432dad666293d5cef02774d72b44f6a74d9d966.zip
Introduce `bun --inspect-wait`
This waits for the inspector to connect before beginning execution
-rw-r--r--src/bun.js/bindings/BunDebugger.cpp10
-rw-r--r--src/bun.js/javascript.zig32
-rw-r--r--src/cli.zig24
3 files changed, 47 insertions, 19 deletions
diff --git a/src/bun.js/bindings/BunDebugger.cpp b/src/bun.js/bindings/BunDebugger.cpp
index 0df94b6d1..31cc1c778 100644
--- a/src/bun.js/bindings/BunDebugger.cpp
+++ b/src/bun.js/bindings/BunDebugger.cpp
@@ -29,6 +29,8 @@ enum class ConnectionStatus : int32_t {
Disconnecting = 2,
Disconnected = 3,
};
+static bool waitingForConnection = false;
+extern "C" void Debugger__didConnect();
class BunInspectorConnection : public Inspector::FrontendChannel {
@@ -84,6 +86,10 @@ public:
inspector.setInspectable(true);
inspector.connect(*connection);
+ if (waitingForConnection) {
+ waitingForConnection = false;
+ Debugger__didConnect();
+ }
Inspector::JSGlobalObjectDebugger* debugger = reinterpret_cast<Inspector::JSGlobalObjectDebugger*>(globalObject->debugger());
if (debugger) {
@@ -420,9 +426,7 @@ extern "C" void Bun__ensureDebugger(ScriptExecutionContextIdentifier scriptId, b
BunInspectorConnection::runWhilePaused(globalObject, isDoneProcessingEvents);
};
}
-
- if (pauseOnStart)
- inspector.pauseWaitingForAutomaticInspection();
+ waitingForConnection = true;
}
JSC_DEFINE_HOST_FUNCTION(jsFunctionCreateConnection, (JSGlobalObject * globalObject, CallFrame* callFrame))
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index e57702e8b..a74da82c8 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -747,7 +747,7 @@ pub const VirtualMachine = struct {
script_execution_context_id: u32 = 0,
next_debugger_id: u64 = 1,
poll_ref: JSC.PollRef = .{},
- auto_pause: bool = false,
+ wait_for_connection: bool = false,
const debug = Output.scoped(.DEBUGGER, false);
extern "C" fn Bun__createJSDebugger(*JSC.JSGlobalObject) u32;
@@ -759,7 +759,8 @@ pub const VirtualMachine = struct {
pub fn create(this: *VirtualMachine, globalObject: *JSGlobalObject) !void {
debug("create", .{});
JSC.markBinding(@src());
- this.debugger.?.script_execution_context_id = Bun__createJSDebugger(globalObject);
+ var debugger = &this.debugger.?;
+ debugger.script_execution_context_id = Bun__createJSDebugger(globalObject);
if (!has_started_debugger_thread) {
has_started_debugger_thread = true;
futex_atomic = std.atomic.Atomic(u32).init(0);
@@ -767,9 +768,11 @@ pub const VirtualMachine = struct {
thread.detach();
}
this.eventLoop().ensureWaker();
- if (this.debugger.?.auto_pause) {
- this.debugger.?.poll_ref.ref(this);
+
+ if (debugger.wait_for_connection) {
+ debugger.poll_ref.ref(this);
}
+
debug("spin", .{});
while (futex_atomic.load(.Monotonic) > 0) std.Thread.Futex.wait(&futex_atomic, 1);
if (comptime Environment.allow_assert)
@@ -778,7 +781,10 @@ pub const VirtualMachine = struct {
.duration_ns = @truncate(@as(u128, @intCast(std.time.nanoTimestamp() - bun.CLI.start_time))),
}});
- Bun__ensureDebugger(this.debugger.?.script_execution_context_id, this.debugger.?.auto_pause);
+ Bun__ensureDebugger(debugger.script_execution_context_id, debugger.wait_for_connection);
+ while (debugger.wait_for_connection) {
+ this.eventLoop().tick();
+ }
}
pub fn startJSDebuggerThread(other_vm: *VirtualMachine) void {
@@ -804,6 +810,13 @@ pub const VirtualMachine = struct {
pub export var Bun__debugger_server_url: bun.String = undefined;
+ pub export fn Debugger__didConnect() void {
+ var this = VirtualMachine.get();
+ std.debug.assert(this.debugger.?.wait_for_connection);
+ this.debugger.?.wait_for_connection = false;
+ this.debugger.?.poll_ref.unref(this);
+ }
+
fn start(other_vm: *VirtualMachine) void {
JSC.markBinding(@src());
@@ -824,9 +837,10 @@ pub const VirtualMachine = struct {
Output.flush();
}
+ debug("wake", .{});
futex_atomic.store(0, .Monotonic);
std.Thread.Futex.wake(&futex_atomic, 1);
- debug("wake", .{});
+
this.eventLoop().tick();
while (true) {
@@ -1119,11 +1133,9 @@ pub const VirtualMachine = struct {
switch (debugger) {
.unspecified => {},
.enable => {
- this.debugger = Debugger{};
- },
- .path_or_port => {
this.debugger = Debugger{
- .path_or_port = debugger.path_or_port,
+ .path_or_port = debugger.enable.path_or_port,
+ .wait_for_connection = debugger.enable.wait_for_connection,
};
},
}
diff --git a/src/cli.zig b/src/cli.zig
index fcf9358e8..88b6aa057 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -161,7 +161,7 @@ pub const Arguments = struct {
clap.parseParam("--no-macros Disable macros from being executed in the bundler, transpiler and runtime") catch unreachable,
clap.parseParam("--target <STR> The intended execution environment for the bundle. \"browser\", \"bun\" or \"node\"") catch unreachable,
clap.parseParam("--inspect <STR>? Activate Bun's Debugger") catch unreachable,
- clap.parseParam("--inspect-brk <STR>? Activate Bun's Debugger and pause immediately") catch unreachable,
+ clap.parseParam("--inspect-wait <STR>? Activate Bun's Debugger and wait for a connection before executing") catch unreachable,
clap.parseParam("<POS>... ") catch unreachable,
};
@@ -515,11 +515,21 @@ pub const Arguments = struct {
ctx.runtime_options.smol = args.flag("--smol");
if (args.option("--inspect")) |inspect_flag| {
ctx.runtime_options.debugger = if (inspect_flag.len == 0)
- Command.Debugger{ .enable = {} }
+ Command.Debugger{ .enable = .{} }
else
- Command.Debugger{
+ Command.Debugger{ .enable = .{
.path_or_port = inspect_flag,
- };
+ } };
+ } else if (args.option("--inspect-wait")) |inspect_flag| {
+ ctx.runtime_options.debugger = if (inspect_flag.len == 0)
+ Command.Debugger{ .enable = .{
+ .wait_for_connection = true,
+ } }
+ else
+ Command.Debugger{ .enable = .{
+ .path_or_port = inspect_flag,
+ .wait_for_connection = true,
+ } };
}
}
@@ -956,8 +966,10 @@ pub const Command = struct {
pub const Debugger = union(enum) {
unspecified: void,
- enable: void,
- path_or_port: []const u8,
+ enable: struct {
+ path_or_port: []const u8 = "",
+ wait_for_connection: bool = false,
+ },
};
pub const RuntimeOptions = struct {