diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-03-19 09:45:05 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2023-03-19 09:45:05 +0100 |
| commit | f026939a40b3567f1a798547391b398dc40db49a (patch) | |
| tree | 699f7b7b6c1af84f878a787260e8b598f20b8e26 /src | |
| parent | 6f15eedff1bd32085808ab58f095ab549b493745 (diff) | |
| download | zig-f026939a40b3567f1a798547391b398dc40db49a.tar.gz zig-f026939a40b3567f1a798547391b398dc40db49a.zip | |
macho: enable hot update state only when on compatible host
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/MachO.zig | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 3d0b8b2f77..3ee2ccde2d 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -221,8 +221,13 @@ lazy_bindings: BindingTable = .{}, /// Table of tracked Decls. decls: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, -/// Mach task used when the compiler is in hot-code swapping mode. -mach_task: ?std.os.darwin.MachTask = null, +/// Hot-code swapping state. +hot_state: if (is_hot_update_compatible) HotUpdateState else struct {} = .{}, + +const is_hot_update_compatible = switch (builtin.target.os.tag) { + .macos => true, + else => false, +}; const DeclMetadata = struct { atom: Atom.Index, @@ -303,6 +308,10 @@ pub const SymbolWithLoc = struct { } }; +const HotUpdateState = struct { + mach_task: ?std.os.darwin.MachTask = null, +}; + /// When allocating, the ideal_capacity is calculated by /// actual_capacity + (actual_capacity / ideal_factor) const ideal_factor = 3; @@ -1085,14 +1094,16 @@ pub fn writeAtom(self: *MachO, atom_index: Atom.Index, code: []u8) !void { try Atom.resolveRelocations(self, atom_index, relocs.items, code); } - if (self.base.child_pid) |pid| blk: { - const task = self.mach_task orelse { - log.warn("cannot hot swap: no Mach task acquired for child process with pid {d}", .{pid}); - break :blk; - }; - self.updateAtomInMemory(task, section.segment_index, sym.n_value, code) catch |err| { - log.warn("cannot hot swap: writing to memory failed: {s}", .{@errorName(err)}); - }; + if (is_hot_update_compatible) { + if (self.base.child_pid) |pid| blk: { + const task = self.hot_state.mach_task orelse { + log.warn("cannot hot swap: no Mach task acquired for child process with pid {d}", .{pid}); + break :blk; + }; + self.updateAtomInMemory(task, section.segment_index, sym.n_value, code) catch |err| { + log.warn("cannot hot swap: writing to memory failed: {s}", .{@errorName(err)}); + }; + } } try self.base.file.?.pwriteAll(code, file_offset); @@ -3812,9 +3823,11 @@ pub fn allocatedVirtualSize(self: *MachO, start: u64) u64 { } pub fn ptraceAttach(self: *MachO, pid: std.os.pid_t) !void { + if (!is_hot_update_compatible) return; + const mach_task = try std.os.darwin.machTaskForPid(pid); log.debug("Mach task for pid {d}: {any}", .{ pid, mach_task }); - self.mach_task = mach_task; + self.hot_state.mach_task = mach_task; // TODO start exception handler in another thread @@ -3823,6 +3836,8 @@ pub fn ptraceAttach(self: *MachO, pid: std.os.pid_t) !void { } pub fn ptraceDetach(self: *MachO, pid: std.os.pid_t) !void { + if (!is_hot_update_compatible) return; + _ = pid; // TODO stop exception handler @@ -3830,7 +3845,7 @@ pub fn ptraceDetach(self: *MachO, pid: std.os.pid_t) !void { // TODO see comment in ptraceAttach // try std.os.ptrace(std.os.darwin.PT.DETACH, pid, 0, 0); - self.mach_task = null; + self.hot_state.mach_task = null; } pub fn makeStaticString(bytes: []const u8) [16]u8 { |
