authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-28 08:47:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:52-07:00
log6f64c8b693446a6f8d170bb09324e5c5e506ce6f
tree620a2126901c6d92536012ec6599c0b96759d313
parent1553c8eae7d830cb793e78c3e52ad816fa8efd23

std.debug.SelfInfo.Windows: less invasive change

restores code closer to master branch in hopes of avoiding a regression that was introduced when this was based on openSelfExe rather than GetModuleFileNameExW.

2 files changed, 16 insertions(+), 11 deletions(-)

lib/std/Io/Threaded.zig+3-3
......@@ -2052,10 +2052,10 @@ fn dirOpenFileWindows(
20522052 const sub_path_w_array = try windows.sliceToPrefixedFileW(dir.handle, sub_path);
20532053 const sub_path_w = sub_path_w_array.span();
20542054 const dir_handle = if (std.fs.path.isAbsoluteWindowsWtf16(sub_path_w)) null else dir.handle;
2055 return dirOpenFileWindowsInner(t, dir_handle, sub_path_w, flags);
2055 return dirOpenFileWtf16(t, dir_handle, sub_path_w, flags);
20562056}
20572057
2058fn dirOpenFileWindowsInner(
2058pub fn dirOpenFileWtf16(
20592059 t: *Threaded,
20602060 dir_handle: ?windows.HANDLE,
20612061 sub_path_w: [:0]const u16,
......@@ -2800,7 +2800,7 @@ fn openSelfExe(userdata: ?*anyopaque, flags: Io.File.OpenFlags) Io.File.OpenSelf
28002800 const image_path_unicode_string = &windows.peb().ProcessParameters.ImagePathName;
28012801 const image_path_name = image_path_unicode_string.Buffer.?[0 .. image_path_unicode_string.Length / 2 :0];
28022802 const prefixed_path_w = try windows.wToPrefixedFileW(null, image_path_name);
2803 return dirOpenFileWindowsInner(t, null, prefixed_path_w.span(), flags);
2803 return dirOpenFileWtf16(t, null, prefixed_path_w.span(), flags);
28042804 },
28052805 else => @panic("TODO implement openSelfExe"),
28062806 }
lib/std/debug/SelfInfo/Windows.zig+13-8
......@@ -297,7 +297,19 @@ const Module = struct {
297297 // a binary is produced with -gdwarf, since the section names are longer than 8 bytes.
298298 const mapped_file: ?DebugInfo.MappedFile = mapped: {
299299 if (!coff_obj.strtabRequired()) break :mapped null;
300 const coff_file = Io.File.openSelfExe(io, .{}) catch |err| switch (err) {
300 var name_buffer: [windows.PATH_MAX_WIDE + 4:0]u16 = undefined;
301 name_buffer[0..4].* = .{ '\\', '?', '?', '\\' }; // openFileAbsoluteW requires the prefix to be present
302 const process_handle = windows.GetCurrentProcess();
303 const len = windows.kernel32.GetModuleFileNameExW(
304 process_handle,
305 module.handle,
306 name_buffer[4..],
307 windows.PATH_MAX_WIDE,
308 );
309 if (len == 0) return error.MissingDebugInfo;
310 const name_w = name_buffer[0 .. len + 4 :0];
311 var threaded: Io.Threaded = .init_single_threaded;
312 const coff_file = threaded.dirOpenFileWtf16(null, name_w, .{}) catch |err| switch (err) {
301313 error.Canceled => |e| return e,
302314 error.Unexpected => |e| return e,
303315 error.FileNotFound => return error.MissingDebugInfo,
......@@ -327,12 +339,6 @@ const Module = struct {
327339 error.SystemFdQuotaExceeded,
328340 error.FileLocksNotSupported,
329341 error.FileBusy,
330 error.InputOutput,
331 error.NotSupported,
332 error.FileSystem,
333 error.NotLink,
334 error.UnrecognizedVolume,
335 error.UnknownName,
336342 => return error.ReadFailed,
337343 };
338344 errdefer coff_file.close(io);
......@@ -352,7 +358,6 @@ const Module = struct {
352358 errdefer windows.CloseHandle(section_handle);
353359 var coff_len: usize = 0;
354360 var section_view_ptr: ?[*]const u8 = null;
355 const process_handle = windows.GetCurrentProcess();
356361 const map_section_rc = windows.ntdll.NtMapViewOfSection(
357362 section_handle,
358363 process_handle,