From 98dc943c0784b93ed28099bb75044c536174a144 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 2 Sep 2018 15:58:08 -0400 Subject: [PATCH] rework code to avoid duplicate operations --- std/coff.zig | 8 - std/debug/index.zig | 435 ++++++++++++++++++++++++++++++++---- std/os/windows/index.zig | 13 ++ std/os/windows/kernel32.zig | 18 ++ std/pdb.zig | 287 +++--------------------- 5 files changed, 452 insertions(+), 309 deletions(-) diff --git a/std/coff.zig b/std/coff.zig index 2921109bd6..379fd1af42 100644 --- a/std/coff.zig +++ b/std/coff.zig @@ -83,7 +83,6 @@ pub const Coff = struct { fn loadOptionalHeader(self: *Coff, file_stream: *io.FileInStream) !void { const in = &file_stream.stream; self.pe_header.magic = try in.readIntLe(u16); - std.debug.warn("reading pe optional\n"); // For now we're only interested in finding the reference to the .pdb, // so we'll skip most of this header, which size is different in 32 // 64 bits by the way. @@ -97,11 +96,9 @@ pub const Coff = struct { else return error.InvalidPEMagic; - std.debug.warn("skipping {}\n", skip_size); try self.in_file.seekForward(skip_size); const number_of_rva_and_sizes = try in.readIntLe(u32); - //std.debug.warn("indicating {} data dirs\n", number_of_rva_and_sizes); if (number_of_rva_and_sizes != IMAGE_NUMBEROF_DIRECTORY_ENTRIES) return error.InvalidPEHeader; @@ -110,9 +107,7 @@ pub const Coff = struct { .virtual_address = try in.readIntLe(u32), .size = try in.readIntLe(u32), }; - //std.debug.warn("data_dir @ {x}, size {}\n", data_dir.virtual_address, data_dir.size); } - std.debug.warn("loaded data directories\n"); } pub fn getPdbPath(self: *Coff, buffer: []u8) !usize { @@ -123,7 +118,6 @@ pub const Coff = struct { // debug_directory. const debug_dir = &self.pe_header.data_directory[DEBUG_DIRECTORY]; const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data; - std.debug.warn("file offset {x}\n", file_offset); try self.in_file.seekTo(file_offset + debug_dir.size); var file_stream = io.FileInStream.init(self.in_file); @@ -134,7 +128,6 @@ pub const Coff = struct { // 'RSDS' indicates PDB70 format, used by lld. if (!mem.eql(u8, cv_signature, "RSDS")) return error.InvalidPEMagic; - std.debug.warn("cv_signature {}\n", cv_signature); try in.readNoEof(self.guid[0..]); self.age = try in.readIntLe(u32); @@ -181,7 +174,6 @@ pub const Coff = struct { }, }); } - std.debug.warn("loaded {} sections\n", self.coff_header.number_of_sections); } pub fn getSection(self: *Coff, comptime name: []const u8) ?*Section { diff --git a/std/debug/index.zig b/std/debug/index.zig index 2883465931..de4d7745c6 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -20,6 +20,17 @@ pub const runtime_safety = switch (builtin.mode) { builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => false, }; +const Module = struct { + mod_info: pdb.ModInfo, + module_name: []u8, + obj_file_name: []u8, + + populated: bool, + symbols: []u8, + subsect_info: []u8, + checksums: []u32, +}; + /// Tries to write to stderr, unbuffered, and ignores any error returned. /// Does not append a newline. var stderr_file: os.File = undefined; @@ -258,12 +269,277 @@ pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: us } } -fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { +fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_address: usize, tty_color: bool) !void { + const allocator = getDebugInfoAllocator(); const base_address = os.getBaseAddress(); - const relative_address = address - base_address; - std.debug.warn("{x} - {x} => {x}\n", address, base_address, relative_address); - try di.pdb.getSourceLine(relative_address); - return error.UnsupportedDebugInfo; + const relative_address = relocated_address - base_address; + + var coff_section: *coff.Section = undefined; + const mod_index = for (di.sect_contribs) |sect_contrib| { + coff_section = &di.coff.sections.toSlice()[sect_contrib.Section]; + + const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset; + const vaddr_end = vaddr_start + sect_contrib.Size; + if (relative_address >= vaddr_start and relative_address < vaddr_end) { + break sect_contrib.ModuleIndex; + } + } else { + // we have no information to add to the address + if (tty_color) { + try out_stream.print("???:?:?: "); + setTtyColor(TtyColor.Dim); + try out_stream.print("0x{x} in ??? (???)", relocated_address); + setTtyColor(TtyColor.Reset); + try out_stream.print("\n\n\n"); + } else { + try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", relocated_address); + } + return; + }; + + const mod = &di.modules[mod_index]; + try populateModule(di, mod); + const obj_basename = os.path.basename(mod.obj_file_name); + + var symbol_i: usize = 0; + const symbol_name = while (symbol_i != mod.symbols.len) { + const prefix = @ptrCast(*pdb.RecordPrefix, &mod.symbols[symbol_i]); + if (prefix.RecordLen < 2) + return error.InvalidDebugInfo; + switch (prefix.RecordKind) { + pdb.SymbolKind.S_LPROC32 => { + const proc_sym = @ptrCast(*pdb.ProcSym, &mod.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)]); + const vaddr_start = coff_section.header.virtual_address + proc_sym.CodeOffset; + const vaddr_end = vaddr_start + proc_sym.CodeSize; + if (relative_address >= vaddr_start and relative_address < vaddr_end) { + break mem.toSliceConst(u8, @ptrCast([*]u8, proc_sym) + @sizeOf(pdb.ProcSym)); + } + }, + else => {}, + } + symbol_i += prefix.RecordLen + @sizeOf(u16); + if (symbol_i > mod.symbols.len) + return error.InvalidDebugInfo; + } else "???"; + + const subsect_info = mod.subsect_info; + + var sect_offset: usize = 0; + var skip_len: usize = undefined; + const opt_line_info = subsections: while (sect_offset != subsect_info.len) : (sect_offset += skip_len) { + const subsect_hdr = @ptrCast(*pdb.DebugSubsectionHeader, &subsect_info[sect_offset]); + skip_len = subsect_hdr.Length; + sect_offset += @sizeOf(pdb.DebugSubsectionHeader); + + switch (subsect_hdr.Kind) { + pdb.DebugSubsectionKind.Lines => { + var line_index: usize = sect_offset; + + const line_hdr = @ptrCast(*pdb.LineFragmentHeader, &subsect_info[line_index]); + if (line_hdr.RelocSegment == 0) return error.MissingDebugInfo; + line_index += @sizeOf(pdb.LineFragmentHeader); + + const block_hdr = @ptrCast(*pdb.LineBlockFragmentHeader, &subsect_info[line_index]); + line_index += @sizeOf(pdb.LineBlockFragmentHeader); + + const has_column = line_hdr.Flags.LF_HaveColumns; + + const frag_vaddr_start = coff_section.header.virtual_address + line_hdr.RelocOffset; + const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize; + if (relative_address >= frag_vaddr_start and relative_address < frag_vaddr_end) { + var line_i: usize = 0; + const start_line_index = line_index; + while (line_i < block_hdr.NumLines) : (line_i += 1) { + const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[line_index]); + line_index += @sizeOf(pdb.LineNumberEntry); + const flags = @ptrCast(*pdb.LineNumberEntry.Flags, &line_num_entry.Flags); + const vaddr_start = frag_vaddr_start + line_num_entry.Offset; + const vaddr_end = if (flags.End == 0) frag_vaddr_end else vaddr_start + flags.End; + if (relative_address >= vaddr_start and relative_address < vaddr_end) { + const chksum_index = block_hdr.NameIndex; + std.debug.warn("looking up checksum {}\n", chksum_index); + const strtab_offset = mod.checksums[chksum_index]; + try di.pdb.string_table.seekTo(@sizeOf(pdb.PDBStringTableHeader) + strtab_offset); + const source_file_name = try di.pdb.string_table.readNullTermString(allocator); + const line = flags.Start; + const column = if (has_column) blk: { + line_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines; + line_index += @sizeOf(pdb.ColumnNumberEntry) * line_i; + const col_num_entry = @ptrCast(*pdb.ColumnNumberEntry, &subsect_info[line_index]); + break :blk col_num_entry.StartColumn; + } else 0; + break :subsections LineInfo{ + .allocator = allocator, + .file_name = source_file_name, + .line = line, + .column = column, + }; + } + } + break :subsections null; + } + }, + else => {}, + } + + if (sect_offset > subsect_info.len) + return error.InvalidDebugInfo; + } else null; + + if (tty_color) { + if (opt_line_info) |li| { + try out_stream.print("{}:{}:{}: ", li.file_name, li.line, li.column); + } else { + try out_stream.print("???:?:?: "); + } + setTtyColor(TtyColor.Dim); + try out_stream.print("0x{x} in {} ({})", relocated_address, symbol_name, obj_basename); + setTtyColor(TtyColor.Reset); + + if (opt_line_info) |line_info| { + try out_stream.print("\n"); + if (printLineFromFile(out_stream, line_info)) { + if (line_info.column == 0) { + try out_stream.write("\n"); + } else { + { + var col_i: usize = 1; + while (col_i < line_info.column) : (col_i += 1) { + try out_stream.writeByte(' '); + } + } + setTtyColor(TtyColor.Green); + try out_stream.write("^"); + setTtyColor(TtyColor.Reset); + try out_stream.write("\n"); + } + } else |err| switch (err) { + error.EndOfFile => {}, + else => return err, + } + } else { + try out_stream.print("\n\n\n"); + } + } else { + if (opt_line_info) |li| { + try out_stream.print("{}:{}:{}: 0x{x} in {} ({})\n\n\n", li.file_name, li.line, li.column, relocated_address, symbol_name, obj_basename); + } else { + try out_stream.print("???:?:?: 0x{x} in {} ({})\n\n\n", relocated_address, symbol_name, obj_basename); + } + } +} + +const TtyColor = enum{ + Red, + Green, + Cyan, + White, + Dim, + Bold, + Reset, +}; + +/// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt +fn setTtyColor(tty_color: TtyColor) void { + const S = struct { + var attrs: windows.WORD = undefined; + var init_attrs = false; + }; + if (!S.init_attrs) { + S.init_attrs = true; + var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; + // TODO handle error + _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); + S.attrs = info.wAttributes; + } + + // TODO handle errors + switch (tty_color) { + TtyColor.Red => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED|windows.FOREGROUND_INTENSITY); + }, + TtyColor.Green => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN|windows.FOREGROUND_INTENSITY); + }, + TtyColor.Cyan => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, + windows.FOREGROUND_GREEN|windows.FOREGROUND_BLUE|windows.FOREGROUND_INTENSITY); + }, + TtyColor.White, TtyColor.Bold => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, + windows.FOREGROUND_RED|windows.FOREGROUND_GREEN|windows.FOREGROUND_BLUE|windows.FOREGROUND_INTENSITY); + }, + TtyColor.Dim => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, + windows.FOREGROUND_RED|windows.FOREGROUND_GREEN|windows.FOREGROUND_BLUE); + }, + TtyColor.Reset => { + _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); + }, + } +} + +fn populateModule(di: *DebugInfo, mod: *Module) !void { + if (mod.populated) + return; + const allocator = getDebugInfoAllocator(); + + if (mod.mod_info.C11ByteSize != 0) + return error.InvalidDebugInfo; + + if (mod.mod_info.C13ByteSize == 0) + return error.MissingDebugInfo; + + const modi = di.pdb.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.MissingDebugInfo; + + const signature = try modi.stream.readIntLe(u32); + if (signature != 4) + return error.InvalidDebugInfo; + + mod.symbols = try allocator.alloc(u8, mod.mod_info.SymByteSize - 4); + try modi.stream.readNoEof(mod.symbols); + + mod.subsect_info = try allocator.alloc(u8, mod.mod_info.C13ByteSize); + try modi.stream.readNoEof(mod.subsect_info); + + var checksum_list = ArrayList(u32).init(allocator); + var sect_offset: usize = 0; + var skip_len: usize = undefined; + while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) { + const subsect_hdr = @ptrCast(*pdb.DebugSubsectionHeader, &mod.subsect_info[sect_offset]); + skip_len = subsect_hdr.Length; + sect_offset += @sizeOf(pdb.DebugSubsectionHeader); + + switch (subsect_hdr.Kind) { + pdb.DebugSubsectionKind.FileChecksums => { + var chksum_index: usize = sect_offset; + + while (chksum_index != mod.subsect_info.len) { + const chksum_hdr = @ptrCast(*pdb.FileChecksumEntryHeader, &mod.subsect_info[chksum_index]); + std.debug.warn("{} {}\n", checksum_list.len, chksum_hdr); + try checksum_list.append(chksum_hdr.FileNameOffset); + const len = @sizeOf(pdb.FileChecksumEntryHeader) + chksum_hdr.ChecksumSize; + chksum_index += len + (len % 4); + if (chksum_index > mod.subsect_info.len) + return error.InvalidDebugInfo; + } + + }, + else => {}, + } + + if (sect_offset > mod.subsect_info.len) + return error.InvalidDebugInfo; + } + mod.checksums = checksum_list.toOwnedSlice(); + + for (mod.checksums) |strtab_offset| { + try di.pdb.string_table.seekTo(@sizeOf(pdb.PDBStringTableHeader) + strtab_offset); + const source_file_name = try di.pdb.string_table.readNullTermString(allocator); + std.debug.warn("{}={}\n", strtab_offset, source_file_name); + } + + mod.populated = true; } fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol { @@ -425,6 +701,8 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { var di = DebugInfo{ .coff = coff_obj, .pdb = undefined, + .sect_contribs = undefined, + .modules = undefined, }; try di.coff.loadHeader(); @@ -432,15 +710,12 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { var path_buf: [windows.MAX_PATH]u8 = undefined; const len = try di.coff.getPdbPath(path_buf[0..]); const raw_path = path_buf[0..len]; - std.debug.warn("pdb raw path {}\n", raw_path); const path = try os.path.resolve(allocator, raw_path); - std.debug.warn("pdb resolved path {}\n", path); try di.pdb.openFile(di.coff, path); var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo; - std.debug.warn("pdb real filepos {}\n", pdb_stream.getFilePos()); const version = try pdb_stream.stream.readIntLe(u32); const signature = try pdb_stream.stream.readIntLe(u32); const age = try pdb_stream.stream.readIntLe(u32); @@ -448,51 +723,119 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { try pdb_stream.stream.readNoEof(guid[0..]); if (!mem.eql(u8, di.coff.guid, guid) or di.coff.age != age) return error.InvalidDebugInfo; - std.debug.warn("v {} s {} a {}\n", version, signature, age); // We validated the executable and pdb match. - const name_bytes_len = try pdb_stream.stream.readIntLe(u32); - const name_bytes = try allocator.alloc(u8, name_bytes_len); - try pdb_stream.stream.readNoEof(name_bytes); + const string_table_index = str_tab_index: { + const name_bytes_len = try pdb_stream.stream.readIntLe(u32); + const name_bytes = try allocator.alloc(u8, name_bytes_len); + try pdb_stream.stream.readNoEof(name_bytes); - const HashTableHeader = packed struct { - Size: u32, - Capacity: u32, + const HashTableHeader = packed struct { + Size: u32, + Capacity: u32, - fn maxLoad(cap: u32) u32 { - return cap * 2 / 3 + 1; + fn maxLoad(cap: u32) u32 { + return cap * 2 / 3 + 1; + } + }; + var hash_tbl_hdr: HashTableHeader = undefined; + try pdb_stream.stream.readStruct(HashTableHeader, &hash_tbl_hdr); + if (hash_tbl_hdr.Capacity == 0) + return error.InvalidDebugInfo; + + if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity)) + return error.InvalidDebugInfo; + + const present = try readSparseBitVector(&pdb_stream.stream, allocator); + if (present.len != hash_tbl_hdr.Size) + return error.InvalidDebugInfo; + const deleted = try readSparseBitVector(&pdb_stream.stream, allocator); + + const Bucket = struct { + first: u32, + second: u32, + }; + const bucket_list = try allocator.alloc(Bucket, present.len); + for (present) |_| { + const name_offset = try pdb_stream.stream.readIntLe(u32); + const name_index = try pdb_stream.stream.readIntLe(u32); + const name = mem.toSlice(u8, name_bytes.ptr + name_offset); + if (mem.eql(u8, name, "/names")) { + break :str_tab_index name_index; + } } + return error.MissingDebugInfo; }; - var hash_tbl_hdr: HashTableHeader = undefined; - try pdb_stream.stream.readStruct(HashTableHeader, &hash_tbl_hdr); - if (hash_tbl_hdr.Capacity == 0) - return error.InvalidDebugInfo; - - if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity)) - return error.InvalidDebugInfo; - - std.debug.warn("{}\n", hash_tbl_hdr); - - const present = try readSparseBitVector(&pdb_stream.stream, allocator); - if (present.len != hash_tbl_hdr.Size) - return error.InvalidDebugInfo; - const deleted = try readSparseBitVector(&pdb_stream.stream, allocator); - - const Bucket = struct { - first: u32, - second: u32, - }; - const bucket_list = try allocator.alloc(Bucket, present.len); - const string_table_index = for (present) |_| { - const name_offset = try pdb_stream.stream.readIntLe(u32); - const name_index = try pdb_stream.stream.readIntLe(u32); - const name = mem.toSlice(u8, name_bytes.ptr + name_offset); - if (mem.eql(u8, name, "/names")) { - break name_index; - } - } else return error.MissingDebugInfo; di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.InvalidDebugInfo; + di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo; + + const dbi = di.pdb.dbi; + + // Dbi Header + var dbi_stream_header: pdb.DbiStreamHeader = undefined; + try dbi.stream.readStruct(pdb.DbiStreamHeader, &dbi_stream_header); + const mod_info_size = dbi_stream_header.ModInfoSize; + const section_contrib_size = dbi_stream_header.SectionContributionSize; + + var modules = ArrayList(Module).init(allocator); + + // Module Info Substream + var mod_info_offset: usize = 0; + while (mod_info_offset != mod_info_size) { + var mod_info: pdb.ModInfo = undefined; + try dbi.stream.readStruct(pdb.ModInfo, &mod_info); + var this_record_len: usize = @sizeOf(pdb.ModInfo); + + const module_name = try dbi.readNullTermString(allocator); + this_record_len += module_name.len + 1; + + const obj_file_name = try dbi.readNullTermString(allocator); + this_record_len += obj_file_name.len + 1; + + const march_forward_bytes = this_record_len % 4; + if (march_forward_bytes != 0) { + try dbi.seekForward(march_forward_bytes); + this_record_len += march_forward_bytes; + } + + try modules.append(Module{ + .mod_info = mod_info, + .module_name = module_name, + .obj_file_name = obj_file_name, + + .populated = false, + .symbols = undefined, + .subsect_info = undefined, + .checksums = undefined, + }); + + mod_info_offset += this_record_len; + if (mod_info_offset > mod_info_size) + return error.InvalidDebugInfo; + } + + di.modules = modules.toOwnedSlice(); + + // Section Contribution Substream + var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator); + var sect_cont_offset: usize = 0; + if (section_contrib_size != 0) { + const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.stream.readIntLe(u32)); + if (ver != pdb.SectionContrSubstreamVersion.Ver60) + return error.InvalidDebugInfo; + sect_cont_offset += @sizeOf(u32); + } + while (sect_cont_offset != section_contrib_size) { + const entry = try sect_contribs.addOne(); + try dbi.stream.readStruct(pdb.SectionContribEntry, entry); + sect_cont_offset += @sizeOf(pdb.SectionContribEntry); + + if (sect_cont_offset > section_contrib_size) + return error.InvalidDebugInfo; + } + + di.sect_contribs = sect_contribs.toOwnedSlice(); return di; } @@ -715,6 +1058,8 @@ pub const DebugInfo = switch (builtin.os) { builtin.Os.windows => struct { pdb: pdb.Pdb, coff: *coff.Coff, + sect_contribs: []pdb.SectionContribEntry, + modules: []Module, }, builtin.Os.linux => struct { self_exe_file: os.File, diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig index ca6299dc5e..9286b7d090 100644 --- a/std/os/windows/index.zig +++ b/std/os/windows/index.zig @@ -15,6 +15,7 @@ test "import" { pub const ERROR = @import("error.zig"); +pub const SHORT = c_short; pub const BOOL = c_int; pub const BOOLEAN = BYTE; pub const BYTE = u8; @@ -364,3 +365,15 @@ pub const FILE_FLAG_RANDOM_ACCESS = 0x10000000; pub const FILE_FLAG_SESSION_AWARE = 0x00800000; pub const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000; pub const FILE_FLAG_WRITE_THROUGH = 0x80000000; + +pub const SMALL_RECT = extern struct { + Left: SHORT, + Top: SHORT, + Right: SHORT, + Bottom: SHORT, +}; + +pub const COORD = extern struct { + X: SHORT, + Y: SHORT, +}; diff --git a/std/os/windows/kernel32.zig b/std/os/windows/kernel32.zig index 65f10a5a2a..ffa4422760 100644 --- a/std/os/windows/kernel32.zig +++ b/std/os/windows/kernel32.zig @@ -72,6 +72,8 @@ pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR; pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) BOOL; +pub extern "kernel32" stdcallcc fn GetConsoleScreenBufferInfo(hConsoleOutput: HANDLE, lpConsoleScreenBufferInfo: *CONSOLE_SCREEN_BUFFER_INFO) BOOL; + pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: DWORD, lpBuffer: ?[*]CHAR) DWORD; pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: ?[*]WCHAR) DWORD; @@ -179,6 +181,8 @@ pub extern "kernel32" stdcallcc fn ReadFile( pub extern "kernel32" stdcallcc fn RemoveDirectoryA(lpPathName: LPCSTR) BOOL; +pub extern "kernel32" stdcallcc fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) BOOL; + pub extern "kernel32" stdcallcc fn SetFilePointerEx( in_fFile: HANDLE, in_liDistanceToMove: LARGE_INTEGER, @@ -234,3 +238,17 @@ pub const FILE_NOTIFY_CHANGE_LAST_WRITE = 16; pub const FILE_NOTIFY_CHANGE_DIR_NAME = 2; pub const FILE_NOTIFY_CHANGE_FILE_NAME = 1; pub const FILE_NOTIFY_CHANGE_ATTRIBUTES = 4; + + +pub const CONSOLE_SCREEN_BUFFER_INFO = extern struct { + dwSize: COORD, + dwCursorPosition: COORD, + wAttributes: WORD, + srWindow: SMALL_RECT, + dwMaximumWindowSize: COORD, +}; + +pub const FOREGROUND_BLUE = 1; +pub const FOREGROUND_GREEN = 2; +pub const FOREGROUND_RED = 4; +pub const FOREGROUND_INTENSITY = 8; diff --git a/std/pdb.zig b/std/pdb.zig index 3aefc4f724..907eddff04 100644 --- a/std/pdb.zig +++ b/std/pdb.zig @@ -10,7 +10,7 @@ const coff = std.coff; const ArrayList = std.ArrayList; // https://llvm.org/docs/PDB/DbiStream.html#stream-header -const DbiStreamHeader = packed struct { +pub const DbiStreamHeader = packed struct { VersionSignature: i32, VersionHeader: u32, Age: u32, @@ -33,7 +33,7 @@ const DbiStreamHeader = packed struct { Padding: u32, }; -const SectionContribEntry = packed struct { +pub const SectionContribEntry = packed struct { Section: u16, Padding1: [2]u8, Offset: u32, @@ -45,7 +45,7 @@ const SectionContribEntry = packed struct { RelocCrc: u32, }; -const ModInfo = packed struct { +pub const ModInfo = packed struct { Unused1: u32, SectionContr: SectionContribEntry, Flags: u16, @@ -63,12 +63,12 @@ const ModInfo = packed struct { //ObjFileName: char[], }; -const SectionMapHeader = packed struct { +pub const SectionMapHeader = packed struct { Count: u16, /// Number of segment descriptors LogCount: u16, /// Number of logical segment descriptors }; -const SectionMapEntry = packed struct { +pub const SectionMapEntry = packed struct { Flags: u16 , /// See the SectionMapEntryFlags enum below. Ovl: u16 , /// Logical overlay number Group: u16 , /// Group index into descriptor array. @@ -86,12 +86,6 @@ pub const StreamType = enum(u16) { Ipi = 4, }; -const Module = struct { - mod_info: ModInfo, - module_name: []u8, - obj_file_name: []u8, -}; - /// Duplicate copy of SymbolRecordKind, but using the official CV names. Useful /// for reference purposes and when dealing with unknown record types. pub const SymbolKind = packed enum(u16) { @@ -293,9 +287,9 @@ pub const SymbolKind = packed enum(u16) { S_GTHREAD32 = 4371, }; -const TypeIndex = u32; +pub const TypeIndex = u32; -const ProcSym = packed struct { +pub const ProcSym = packed struct { Parent: u32 , End: u32 , Next: u32 , @@ -310,7 +304,7 @@ const ProcSym = packed struct { // Name: [*]u8, }; -const ProcSymFlags = packed struct { +pub const ProcSymFlags = packed struct { HasFP: bool, HasIRET: bool, HasFRET: bool, @@ -321,24 +315,24 @@ const ProcSymFlags = packed struct { HasOptimizedDebugInfo: bool, }; -const SectionContrSubstreamVersion = enum(u32) { +pub const SectionContrSubstreamVersion = enum(u32) { Ver60 = 0xeffe0000 + 19970605, V2 = 0xeffe0000 + 20140516 }; -const RecordPrefix = packed struct { +pub const RecordPrefix = packed struct { RecordLen: u16, /// Record length, starting from &RecordKind. RecordKind: SymbolKind, /// Record kind enum (SymRecordKind or TypeRecordKind) }; -const LineFragmentHeader = packed struct { +pub const LineFragmentHeader = packed struct { RelocOffset: u32, /// Code offset of line contribution. RelocSegment: u16, /// Code segment of line contribution. Flags: LineFlags, CodeSize: u32, /// Code size of this line contribution. }; -const LineFlags = packed struct { +pub const LineFlags = packed struct { LF_HaveColumns: bool, /// CV_LINES_HAVE_COLUMNS unused: u15, }; @@ -347,7 +341,7 @@ const LineFlags = packed struct { /// header. The structure definitions follow. /// LineNumberEntry Lines[NumLines]; /// ColumnNumberEntry Columns[NumLines]; -const LineBlockFragmentHeader = packed struct { +pub const LineBlockFragmentHeader = packed struct { /// Offset of FileChecksum entry in File /// checksums buffer. The checksum entry then /// contains another offset into the string @@ -358,7 +352,7 @@ const LineBlockFragmentHeader = packed struct { }; -const LineNumberEntry = packed struct { +pub const LineNumberEntry = packed struct { Offset: u32, /// Offset to start of code bytes for line number Flags: u32, @@ -370,19 +364,19 @@ const LineNumberEntry = packed struct { }; }; -const ColumnNumberEntry = packed struct { +pub const ColumnNumberEntry = packed struct { StartColumn: u16, EndColumn: u16, }; /// Checksum bytes follow. -const FileChecksumEntryHeader = packed struct { +pub const FileChecksumEntryHeader = packed struct { FileNameOffset: u32, /// Byte offset of filename in global string table. ChecksumSize: u8, /// Number of bytes of checksum. ChecksumKind: u8, /// FileChecksumKind }; -const DebugSubsectionKind = packed enum(u32) { +pub const DebugSubsectionKind = packed enum(u32) { None = 0, Symbols = 0xf1, Lines = 0xf2, @@ -402,11 +396,25 @@ const DebugSubsectionKind = packed enum(u32) { CoffSymbolRVA = 0xfd, }; + +pub const DebugSubsectionHeader = packed struct { + Kind: DebugSubsectionKind, /// codeview::DebugSubsectionKind enum + Length: u32, /// number of bytes occupied by this record. +}; + + +pub const PDBStringTableHeader = packed struct { + Signature: u32, /// PDBStringTableSignature + HashVersion: u32, /// 1 or 2 + ByteSize: u32, /// Number of bytes of names buffer. +}; + pub const Pdb = struct { in_file: os.File, allocator: *mem.Allocator, coff: *coff.Coff, string_table: *MsfStream, + dbi: *MsfStream, msf: Msf, @@ -428,230 +436,6 @@ pub const Pdb = struct { const id = @enumToInt(stream); return self.getStreamById(id); } - - pub fn getSourceLine(self: *Pdb, address: usize) !void { - const dbi = self.getStream(StreamType.Dbi) orelse return error.InvalidDebugInfo; - - // Dbi Header - var header: DbiStreamHeader = undefined; - try dbi.stream.readStruct(DbiStreamHeader, &header); - std.debug.warn("{}\n", header); - warn("after header dbi stream at {} (file offset)\n", dbi.getFilePos()); - - var modules = ArrayList(Module).init(self.allocator); - - // Module Info Substream - var mod_info_offset: usize = 0; - while (mod_info_offset != header.ModInfoSize) { - var mod_info: ModInfo = undefined; - try dbi.stream.readStruct(ModInfo, &mod_info); - std.debug.warn("{}\n", mod_info); - var this_record_len: usize = @sizeOf(ModInfo); - - const module_name = try dbi.readNullTermString(self.allocator); - std.debug.warn("module_name '{}'\n", module_name); - this_record_len += module_name.len + 1; - - const obj_file_name = try dbi.readNullTermString(self.allocator); - std.debug.warn("obj_file_name '{}'\n", obj_file_name); - this_record_len += obj_file_name.len + 1; - - const march_forward_bytes = this_record_len % 4; - if (march_forward_bytes != 0) { - try dbi.seekForward(march_forward_bytes); - this_record_len += march_forward_bytes; - } - - try modules.append(Module{ - .mod_info = mod_info, - .module_name = module_name, - .obj_file_name = obj_file_name, - }); - - mod_info_offset += this_record_len; - if (mod_info_offset > header.ModInfoSize) - return error.InvalidDebugInfo; - } - - // Section Contribution Substream - var sect_contribs = ArrayList(SectionContribEntry).init(self.allocator); - std.debug.warn("looking at Section Contributinos now\n"); - var sect_cont_offset: usize = 0; - if (header.SectionContributionSize != 0) { - const ver = @intToEnum(SectionContrSubstreamVersion, try dbi.stream.readIntLe(u32)); - if (ver != SectionContrSubstreamVersion.Ver60) - return error.InvalidDebugInfo; - sect_cont_offset += @sizeOf(u32); - } - while (sect_cont_offset != header.SectionContributionSize) { - const entry = try sect_contribs.addOne(); - try dbi.stream.readStruct(SectionContribEntry, entry); - std.debug.warn("{}\n", entry); - sect_cont_offset += @sizeOf(SectionContribEntry); - - if (sect_cont_offset > header.SectionContributionSize) - return error.InvalidDebugInfo; - } - //std.debug.warn("looking at section map now\n"); - //if (header.SectionMapSize == 0) - // return error.MissingDebugInfo; - - //var sect_map_hdr: SectionMapHeader = undefined; - //try dbi.stream.readStruct(SectionMapHeader, §_map_hdr); - - //const sect_entries = try self.allocator.alloc(SectionMapEntry, sect_map_hdr.Count); - //const as_bytes = @sliceToBytes(sect_entries); - //if (as_bytes.len + @sizeOf(SectionMapHeader) != header.SectionMapSize) - // return error.InvalidDebugInfo; - //try dbi.stream.readNoEof(as_bytes); - - //for (sect_entries) |sect_entry| { - // std.debug.warn("{}\n", sect_entry); - //} - - var coff_section: *coff.Section = undefined; - const mod_index = for (sect_contribs.toSlice()) |sect_contrib| { - coff_section = &self.coff.sections.toSlice()[sect_contrib.Section]; - std.debug.warn("looking in coff name: {}\n", mem.toSliceConst(u8, &coff_section.header.name)); - - const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset; - const vaddr_end = vaddr_start + sect_contrib.Size; - if (address >= vaddr_start and address < vaddr_end) { - std.debug.warn("found sect contrib: {}\n", sect_contrib); - break sect_contrib.ModuleIndex; - } - } else return error.MissingDebugInfo; - - const mod = &modules.toSlice()[mod_index]; - const modi = self.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.InvalidDebugInfo; - - const signature = try modi.stream.readIntLe(u32); - if (signature != 4) - return error.InvalidDebugInfo; - - const symbols = try self.allocator.alloc(u8, mod.mod_info.SymByteSize - 4); - std.debug.warn("read {} bytes of symbol info\n", symbols.len); - try modi.stream.readNoEof(symbols); - var symbol_i: usize = 0; - const proc_sym = while (symbol_i != symbols.len) { - const prefix = @ptrCast(*RecordPrefix, &symbols[symbol_i]); - if (prefix.RecordLen < 2) - return error.InvalidDebugInfo; - switch (prefix.RecordKind) { - SymbolKind.S_LPROC32 => { - const proc_sym = @ptrCast(*ProcSym, &symbols[symbol_i + @sizeOf(RecordPrefix)]); - const vaddr_start = coff_section.header.virtual_address + proc_sym.CodeOffset; - const vaddr_end = vaddr_start + proc_sym.CodeSize; - std.debug.warn(" {}\n", proc_sym); - if (address >= vaddr_start and address < vaddr_end) { - break proc_sym; - } - }, - else => {}, - } - symbol_i += prefix.RecordLen + @sizeOf(u16); - if (symbol_i > symbols.len) - return error.InvalidDebugInfo; - } else return error.MissingDebugInfo; - - std.debug.warn("found in {s}: {}\n", @ptrCast([*]u8, proc_sym) + @sizeOf(ProcSym), proc_sym); - - if (mod.mod_info.C11ByteSize != 0) - return error.InvalidDebugInfo; - - if (mod.mod_info.C13ByteSize == 0) { - return error.MissingDebugInfo; - } - - const subsect_info = try self.allocator.alloc(u8, mod.mod_info.C13ByteSize); - std.debug.warn("read C13 line info {} bytes\n", subsect_info.len); - const line_info_file_pos = modi.getFilePos(); - try modi.stream.readNoEof(subsect_info); - - const DebugSubsectionHeader = packed struct { - Kind: DebugSubsectionKind, /// codeview::DebugSubsectionKind enum - Length: u32, /// number of bytes occupied by this record. - }; - var sect_offset: usize = 0; - var skip_len: usize = undefined; - var have_line_info: bool = false; - subsections: while (sect_offset != subsect_info.len) : (sect_offset += skip_len) { - const subsect_hdr = @ptrCast(*DebugSubsectionHeader, &subsect_info[sect_offset]); - skip_len = subsect_hdr.Length; - sect_offset += @sizeOf(DebugSubsectionHeader); - - switch (subsect_hdr.Kind) { - DebugSubsectionKind.Lines => { - if (have_line_info) - continue :subsections; - - var line_index: usize = sect_offset; - - const line_hdr = @ptrCast(*LineFragmentHeader, &subsect_info[line_index]); - if (line_hdr.RelocSegment == 0) return error.MissingDebugInfo; - std.debug.warn("{}\n", line_hdr); - line_index += @sizeOf(LineFragmentHeader); - - const block_hdr = @ptrCast(*LineBlockFragmentHeader, &subsect_info[line_index]); - std.debug.warn("{}\n", block_hdr); - line_index += @sizeOf(LineBlockFragmentHeader); - - const has_column = line_hdr.Flags.LF_HaveColumns; - std.debug.warn("has column: {}\n", has_column); - - const frag_vaddr_start = coff_section.header.virtual_address + line_hdr.RelocOffset; - const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize; - if (address >= frag_vaddr_start and address < frag_vaddr_end) { - std.debug.warn("found line listing\n"); - var line_i: usize = 0; - const start_line_index = line_index; - while (line_i < block_hdr.NumLines) : (line_i += 1) { - const line_num_entry = @ptrCast(*LineNumberEntry, &subsect_info[line_index]); - line_index += @sizeOf(LineNumberEntry); - const flags = @ptrCast(*LineNumberEntry.Flags, &line_num_entry.Flags); - std.debug.warn("{} {}\n", line_num_entry, flags); - const vaddr_start = frag_vaddr_start + line_num_entry.Offset; - const vaddr_end = if (flags.End == 0) frag_vaddr_end else vaddr_start + flags.End; - std.debug.warn("test {x} <= {x} < {x}\n", vaddr_start, address, vaddr_end); - if (address >= vaddr_start and address < vaddr_end) { - std.debug.warn("{} line {}\n", block_hdr.NameIndex, flags.Start); - if (has_column) { - line_index = start_line_index + @sizeOf(LineNumberEntry) * block_hdr.NumLines; - line_index += @sizeOf(ColumnNumberEntry) * line_i; - const col_num_entry = @ptrCast(*ColumnNumberEntry, &subsect_info[line_index]); - std.debug.warn("col {}\n", col_num_entry.StartColumn); - } - have_line_info = true; - continue :subsections; - } - } - return error.MissingDebugInfo; - } - - }, - DebugSubsectionKind.FileChecksums => { - var chksum_index: usize = sect_offset; - - while (chksum_index != subsect_info.len) { - const chksum_hdr = @ptrCast(*FileChecksumEntryHeader, &subsect_info[chksum_index]); - std.debug.warn("{}\n", chksum_hdr); - const len = @sizeOf(FileChecksumEntryHeader) + chksum_hdr.ChecksumSize; - chksum_index += len + (len % 4); - if (chksum_index > subsect_info.len) - return error.InvalidDebugInfo; - } - - }, - else => { - std.debug.warn("ignore subsection {}\n", @tagName(subsect_hdr.Kind)); - }, - } - - if (sect_offset > subsect_info.len) - return error.InvalidDebugInfo; - } - std.debug.warn("end subsections\n"); - } }; // see https://llvm.org/docs/PDB/MsfFile.html @@ -687,13 +471,11 @@ const Msf = struct { ); const stream_count = try self.directory.stream.readIntLe(u32); - warn("stream count {}\n", stream_count); const stream_sizes = try allocator.alloc(u32, stream_count); for (stream_sizes) |*s| { const size = try self.directory.stream.readIntLe(u32); s.* = blockCountFromSize(size, superblock.BlockSize); - warn("stream {}B {} blocks\n", size, s.*); } self.streams = try allocator.alloc(MsfStream, stream_count); @@ -784,13 +566,10 @@ const MsfStream = struct { const in = &file_stream.stream; try file.seekTo(pos); - warn("stream with blocks"); var i: u32 = 0; while (i < block_count) : (i += 1) { stream.blocks[i] = try in.readIntLe(u32); - warn(" {}", stream.blocks[i]); } - warn("\n"); return stream; } @@ -812,10 +591,6 @@ const MsfStream = struct { var block = self.blocks[block_id]; var offset = self.pos % self.block_size; - //std.debug.warn("seek {} read {}B: block_id={} block={} offset={}\n", - // block * self.block_size + offset, - // buffer.len, block_id, block, offset); - try self.in_file.seekTo(block * self.block_size + offset); var file_stream = io.FileInStream.init(self.in_file); const in = &file_stream.stream;