| author | |
| committer | |
| log | 686663239af6afd8dea814a9fe6a8885f06d6cb3 |
| tree | c272fddf103f5466f3a900b996ecdbfaf7393cd6 |
| parent | f1b71053de6c5e71e2e1f73daeaff29280b69350 |
5 files changed, 120 insertions(+), 57 deletions(-)
std/coff.zig+4-4| ... | ... | @@ -41,7 +41,7 @@ pub const Coff = struct { |
| 41 | 41 | pub fn loadHeader(self: *Coff) !void { |
| 42 | 42 | const pe_pointer_offset = 0x3C; |
| 43 | 43 | |
| 44 | var file_stream = io.FileInStream.init(&self.in_file); | |
| 44 | var file_stream = io.FileInStream.init(self.in_file); | |
| 45 | 45 | const in = &file_stream.stream; |
| 46 | 46 | |
| 47 | 47 | var magic: [2]u8 = undefined; |
| ... | ... | @@ -126,7 +126,7 @@ pub const Coff = struct { |
| 126 | 126 | std.debug.warn("file offset {x}\n", file_offset); |
| 127 | 127 | try self.in_file.seekTo(file_offset + debug_dir.size); |
| 128 | 128 | |
| 129 | var file_stream = io.FileInStream.init(&self.in_file); | |
| 129 | var file_stream = io.FileInStream.init(self.in_file); | |
| 130 | 130 | const in = &file_stream.stream; |
| 131 | 131 | |
| 132 | 132 | var cv_signature: [4]u8 = undefined; // CodeView signature |
| ... | ... | @@ -158,7 +158,7 @@ pub const Coff = struct { |
| 158 | 158 | |
| 159 | 159 | self.sections = ArrayList(Section).init(self.allocator); |
| 160 | 160 | |
| 161 | var file_stream = io.FileInStream.init(&self.in_file); | |
| 161 | var file_stream = io.FileInStream.init(self.in_file); | |
| 162 | 162 | const in = &file_stream.stream; |
| 163 | 163 | |
| 164 | 164 | var name: [8]u8 = undefined; |
| ... | ... | @@ -235,4 +235,4 @@ const SectionHeader = struct { |
| 235 | 235 | number_of_relocations: u16, |
| 236 | 236 | number_of_line_numbers: u16, |
| 237 | 237 | characteristics: u32, |
| 238 | }; | |
| \ No newline at end of file | ||
| 238 | }; |
std/debug/index.zig+5-5| ... | ... | @@ -40,7 +40,7 @@ pub fn getStderrStream() !*io.OutStream(io.FileOutStream.Error) { |
| 40 | 40 | return st; |
| 41 | 41 | } else { |
| 42 | 42 | stderr_file = try io.getStdErr(); |
| 43 | stderr_file_out_stream = io.FileOutStream.init(&stderr_file); | |
| 43 | stderr_file_out_stream = io.FileOutStream.init(stderr_file); | |
| 44 | 44 | const st = &stderr_file_out_stream.stream; |
| 45 | 45 | stderr_stream = st; |
| 46 | 46 | return st; |
| ... | ... | @@ -73,7 +73,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { |
| 73 | 73 | stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; |
| 74 | 74 | return; |
| 75 | 75 | }; |
| 76 | writeCurrentStackTrace(stderr, getDebugInfoAllocator(), debug_info, wantTtyColor(), start_addr) catch |err| { | |
| 76 | writeCurrentStackTrace(stderr, debug_info, wantTtyColor(), start_addr) catch |err| { | |
| 77 | 77 | stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; |
| 78 | 78 | return; |
| 79 | 79 | }; |
| ... | ... | @@ -194,9 +194,9 @@ pub inline fn getReturnAddress(frame_count: usize) usize { |
| 194 | 194 | return @intToPtr(*const usize, fp + @sizeOf(usize)).*; |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | pub fn writeCurrentStackTrace(out_stream: var, allocator: *mem.Allocator, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void { | |
| 197 | pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void { | |
| 198 | 198 | switch (builtin.os) { |
| 199 | builtin.Os.windows => return writeCurrentStackTraceWindows(out_stream, allocator, debug_info, tty_color, start_addr), | |
| 199 | builtin.Os.windows => return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr), | |
| 200 | 200 | else => {}, |
| 201 | 201 | } |
| 202 | 202 | const AddressState = union(enum) { |
| ... | ... | @@ -231,7 +231,7 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: *mem.Allocator, debug_ |
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | pub fn writeCurrentStackTraceWindows(out_stream: var, allocator: *mem.Allocator, debug_info: *DebugInfo, | |
| 234 | pub fn writeCurrentStackTraceWindows(out_stream: var, debug_info: *DebugInfo, | |
| 235 | 235 | tty_color: bool, start_addr: ?usize) !void |
| 236 | 236 | { |
| 237 | 237 | var addr_buf: [1024]usize = undefined; |
std/io.zig+4-4| ... | ... | @@ -34,13 +34,13 @@ pub fn getStdIn() GetStdIoErrs!File { |
| 34 | 34 | |
| 35 | 35 | /// Implementation of InStream trait for File |
| 36 | 36 | pub const FileInStream = struct { |
| 37 | file: *File, | |
| 37 | file: File, | |
| 38 | 38 | stream: Stream, |
| 39 | 39 | |
| 40 | 40 | pub const Error = @typeOf(File.read).ReturnType.ErrorSet; |
| 41 | 41 | pub const Stream = InStream(Error); |
| 42 | 42 | |
| 43 | pub fn init(file: *File) FileInStream { | |
| 43 | pub fn init(file: File) FileInStream { | |
| 44 | 44 | return FileInStream{ |
| 45 | 45 | .file = file, |
| 46 | 46 | .stream = Stream{ .readFn = readFn }, |
| ... | ... | @@ -55,13 +55,13 @@ pub const FileInStream = struct { |
| 55 | 55 | |
| 56 | 56 | /// Implementation of OutStream trait for File |
| 57 | 57 | pub const FileOutStream = struct { |
| 58 | file: *File, | |
| 58 | file: File, | |
| 59 | 59 | stream: Stream, |
| 60 | 60 | |
| 61 | 61 | pub const Error = File.WriteError; |
| 62 | 62 | pub const Stream = OutStream(Error); |
| 63 | 63 | |
| 64 | pub fn init(file: *File) FileOutStream { | |
| 64 | pub fn init(file: File) FileOutStream { | |
| 65 | 65 | return FileOutStream{ |
| 66 | 66 | .file = file, |
| 67 | 67 | .stream = Stream{ .writeFn = writeFn }, |
std/os/file.zig+9-10| ... | ... | @@ -205,17 +205,16 @@ pub const File = struct { |
| 205 | 205 | |
| 206 | 206 | /// Upon success, the stream is in an uninitialized state. To continue using it, |
| 207 | 207 | /// you must use the open() function. |
| 208 | pub fn close(self: *File) void { | |
| 208 | pub fn close(self: File) void { | |
| 209 | 209 | os.close(self.handle); |
| 210 | self.handle = undefined; | |
| 211 | 210 | } |
| 212 | 211 | |
| 213 | 212 | /// Calls `os.isTty` on `self.handle`. |
| 214 | pub fn isTty(self: *File) bool { | |
| 213 | pub fn isTty(self: File) bool { | |
| 215 | 214 | return os.isTty(self.handle); |
| 216 | 215 | } |
| 217 | 216 | |
| 218 | pub fn seekForward(self: *File, amount: isize) !void { | |
| 217 | pub fn seekForward(self: File, amount: isize) !void { | |
| 219 | 218 | switch (builtin.os) { |
| 220 | 219 | Os.linux, Os.macosx, Os.ios => { |
| 221 | 220 | const result = posix.lseek(self.handle, amount, posix.SEEK_CUR); |
| ... | ... | @@ -246,7 +245,7 @@ pub const File = struct { |
| 246 | 245 | } |
| 247 | 246 | } |
| 248 | 247 | |
| 249 | pub fn seekTo(self: *File, pos: usize) !void { | |
| 248 | pub fn seekTo(self: File, pos: usize) !void { | |
| 250 | 249 | switch (builtin.os) { |
| 251 | 250 | Os.linux, Os.macosx, Os.ios => { |
| 252 | 251 | const ipos = try math.cast(isize, pos); |
| ... | ... | @@ -280,7 +279,7 @@ pub const File = struct { |
| 280 | 279 | } |
| 281 | 280 | } |
| 282 | 281 | |
| 283 | pub fn getPos(self: *File) !usize { | |
| 282 | pub fn getPos(self: File) !usize { | |
| 284 | 283 | switch (builtin.os) { |
| 285 | 284 | Os.linux, Os.macosx, Os.ios => { |
| 286 | 285 | const result = posix.lseek(self.handle, 0, posix.SEEK_CUR); |
| ... | ... | @@ -316,7 +315,7 @@ pub const File = struct { |
| 316 | 315 | } |
| 317 | 316 | } |
| 318 | 317 | |
| 319 | pub fn getEndPos(self: *File) !usize { | |
| 318 | pub fn getEndPos(self: File) !usize { | |
| 320 | 319 | if (is_posix) { |
| 321 | 320 | const stat = try os.posixFStat(self.handle); |
| 322 | 321 | return @intCast(usize, stat.size); |
| ... | ... | @@ -341,7 +340,7 @@ pub const File = struct { |
| 341 | 340 | Unexpected, |
| 342 | 341 | }; |
| 343 | 342 | |
| 344 | pub fn mode(self: *File) ModeError!Mode { | |
| 343 | pub fn mode(self: File) ModeError!Mode { | |
| 345 | 344 | if (is_posix) { |
| 346 | 345 | var stat: posix.Stat = undefined; |
| 347 | 346 | const err = posix.getErrno(posix.fstat(self.handle, &stat)); |
| ... | ... | @@ -375,7 +374,7 @@ pub const File = struct { |
| 375 | 374 | Unexpected, |
| 376 | 375 | }; |
| 377 | 376 | |
| 378 | pub fn read(self: *File, buffer: []u8) ReadError!usize { | |
| 377 | pub fn read(self: File, buffer: []u8) ReadError!usize { | |
| 379 | 378 | if (is_posix) { |
| 380 | 379 | var index: usize = 0; |
| 381 | 380 | while (index < buffer.len) { |
| ... | ... | @@ -423,7 +422,7 @@ pub const File = struct { |
| 423 | 422 | |
| 424 | 423 | pub const WriteError = os.WindowsWriteError || os.PosixWriteError; |
| 425 | 424 | |
| 426 | pub fn write(self: *File, bytes: []const u8) WriteError!void { | |
| 425 | pub fn write(self: File, bytes: []const u8) WriteError!void { | |
| 427 | 426 | if (is_posix) { |
| 428 | 427 | try os.posixWrite(self.handle, bytes); |
| 429 | 428 | } else if (is_windows) { |
std/pdb.zig+98-34| ... | ... | @@ -8,9 +8,58 @@ const warn = std.debug.warn; |
| 8 | 8 | |
| 9 | 9 | const ArrayList = std.ArrayList; |
| 10 | 10 | |
| 11 | pub const PdbError = error { | |
| 12 | InvalidPdbMagic, | |
| 13 | CorruptedFile, | |
| 11 | // https://llvm.org/docs/PDB/DbiStream.html#stream-header | |
| 12 | const DbiStreamHeader = packed struct { | |
| 13 | VersionSignature: i32, | |
| 14 | VersionHeader: u32, | |
| 15 | Age: u32, | |
| 16 | GlobalStreamIndex: u16, | |
| 17 | BuildNumber: u16, | |
| 18 | PublicStreamIndex: u16, | |
| 19 | PdbDllVersion: u16, | |
| 20 | SymRecordStream: u16, | |
| 21 | PdbDllRbld: u16, | |
| 22 | ModInfoSize: u32, | |
| 23 | SectionContributionSize: i32, | |
| 24 | SectionMapSize: i32, | |
| 25 | SourceInfoSize: i32, | |
| 26 | TypeServerSize: i32, | |
| 27 | MFCTypeServerIndex: u32, | |
| 28 | OptionalDbgHeaderSize: i32, | |
| 29 | ECSubstreamSize: i32, | |
| 30 | Flags: u16, | |
| 31 | Machine: u16, | |
| 32 | Padding: u32, | |
| 33 | }; | |
| 34 | ||
| 35 | const SectionContribEntry = packed struct { | |
| 36 | Section: u16, | |
| 37 | Padding1: [2]u8, | |
| 38 | Offset: i32, | |
| 39 | Size: i32, | |
| 40 | Characteristics: u32, | |
| 41 | ModuleIndex: u16, | |
| 42 | Padding2: [2]u8, | |
| 43 | DataCrc: u32, | |
| 44 | RelocCrc: u32, | |
| 45 | }; | |
| 46 | ||
| 47 | const ModInfo = packed struct { | |
| 48 | Unused1: u32, | |
| 49 | SectionContr: SectionContribEntry, | |
| 50 | Flags: u16, | |
| 51 | ModuleSymStream: u16, | |
| 52 | SymByteSize: u32, | |
| 53 | C11ByteSize: u32, | |
| 54 | C13ByteSize: u32, | |
| 55 | SourceFileCount: u16, | |
| 56 | Padding: [2]u8, | |
| 57 | Unused2: u32, | |
| 58 | SourceFileNameIndex: u32, | |
| 59 | PdbFilePathNameIndex: u32, | |
| 60 | // These fields are variable length | |
| 61 | //ModuleName: char[], | |
| 62 | //ObjFileName: char[], | |
| 14 | 63 | }; |
| 15 | 64 | |
| 16 | 65 | pub const StreamType = enum(u16) { |
| ... | ... | @@ -30,7 +79,7 @@ pub const Pdb = struct { |
| 30 | 79 | self.in_file = try os.File.openRead(file_name[0..]); |
| 31 | 80 | self.allocator = allocator; |
| 32 | 81 | |
| 33 | try self.msf.openFile(allocator, &self.in_file); | |
| 82 | try self.msf.openFile(allocator, self.in_file); | |
| 34 | 83 | } |
| 35 | 84 | |
| 36 | 85 | pub fn getStream(self: *Pdb, stream: StreamType) ?*MsfStream { |
| ... | ... | @@ -41,29 +90,32 @@ pub const Pdb = struct { |
| 41 | 90 | } |
| 42 | 91 | |
| 43 | 92 | pub fn getSourceLine(self: *Pdb, address: usize) !void { |
| 44 | const dbi = self.getStream(StreamType.Dbi) orelse return error.CorruptedFile; | |
| 93 | const dbi = self.getStream(StreamType.Dbi) orelse return error.InvalidDebugInfo; | |
| 45 | 94 | |
| 46 | 95 | // Dbi Header |
| 47 | try dbi.seekForward(@sizeOf(u32) * 3 + @sizeOf(u16) * 6); | |
| 48 | warn("dbi stream at {} (file offset)\n", dbi.getFilePos()); | |
| 49 | const module_info_size = try dbi.stream.readIntLe(u32); | |
| 50 | const section_contribution_size = try dbi.stream.readIntLe(u32); | |
| 51 | const section_map_size = try dbi.stream.readIntLe(u32); | |
| 52 | const source_info_size = try dbi.stream.readIntLe(u32); | |
| 53 | warn("module_info_size: {}\n", module_info_size); | |
| 54 | warn("section_contribution_size: {}\n", section_contribution_size); | |
| 55 | warn("section_map_size: {}\n", section_map_size); | |
| 56 | warn("source_info_size: {}\n", source_info_size); | |
| 57 | try dbi.seekForward(@sizeOf(u32) * 5 + @sizeOf(u16) * 2); | |
| 96 | var header: DbiStreamHeader = undefined; | |
| 97 | try dbi.stream.readStruct(DbiStreamHeader, &header); | |
| 98 | std.debug.warn("{}\n", header); | |
| 58 | 99 | warn("after header dbi stream at {} (file offset)\n", dbi.getFilePos()); |
| 59 | 100 | |
| 60 | 101 | // Module Info Substream |
| 61 | try dbi.seekForward(@sizeOf(u32) + @sizeOf(u16) + @sizeOf(u8) * 2); | |
| 62 | const offset = try dbi.stream.readIntLe(u32); | |
| 63 | const size = try dbi.stream.readIntLe(u32); | |
| 64 | try dbi.seekForward(@sizeOf(u32)); | |
| 65 | const module_index = try dbi.stream.readIntLe(u16); | |
| 66 | warn("module {} of size {} at {}\n", module_index, size, offset); | |
| 102 | var mod_info_offset: usize = 0; | |
| 103 | while (mod_info_offset < header.ModInfoSize) { | |
| 104 | var mod_info: ModInfo = undefined; | |
| 105 | try dbi.stream.readStruct(ModInfo, &mod_info); | |
| 106 | std.debug.warn("{}\n", mod_info); | |
| 107 | mod_info_offset += @sizeOf(ModInfo); | |
| 108 | ||
| 109 | const module_name = try dbi.readNullTermString(self.allocator); | |
| 110 | std.debug.warn("module_name {}\n", module_name); | |
| 111 | mod_info_offset += module_name.len + 1; | |
| 112 | ||
| 113 | const obj_file_name = try dbi.readNullTermString(self.allocator); | |
| 114 | std.debug.warn("obj_file_name {}\n", obj_file_name); | |
| 115 | mod_info_offset += obj_file_name.len + 1; | |
| 116 | } | |
| 117 | std.debug.warn("end modules\n"); | |
| 118 | ||
| 67 | 119 | |
| 68 | 120 | // TODO: locate corresponding source line information |
| 69 | 121 | } |
| ... | ... | @@ -75,7 +127,7 @@ const Msf = struct { |
| 75 | 127 | directory: MsfStream, |
| 76 | 128 | streams: ArrayList(MsfStream), |
| 77 | 129 | |
| 78 | fn openFile(self: *Msf, allocator: *mem.Allocator, file: *os.File) !void { | |
| 130 | fn openFile(self: *Msf, allocator: *mem.Allocator, file: os.File) !void { | |
| 79 | 131 | var file_stream = io.FileInStream.init(file); |
| 80 | 132 | const in = &file_stream.stream; |
| 81 | 133 | |
| ... | ... | @@ -84,7 +136,7 @@ const Msf = struct { |
| 84 | 136 | warn("magic: '{}'\n", magic); |
| 85 | 137 | |
| 86 | 138 | if (!mem.eql(u8, magic, SuperBlock.FileMagic)) |
| 87 | return error.InvalidPdbMagic; | |
| 139 | return error.InvalidDebugInfo; | |
| 88 | 140 | |
| 89 | 141 | self.superblock = SuperBlock { |
| 90 | 142 | .block_size = try in.readIntLe(u32), |
| ... | ... | @@ -97,11 +149,11 @@ const Msf = struct { |
| 97 | 149 | |
| 98 | 150 | switch (self.superblock.block_size) { |
| 99 | 151 | 512, 1024, 2048, 4096 => {}, // llvm only uses 4096 |
| 100 | else => return error.InvalidPdbMagic | |
| 152 | else => return error.InvalidDebugInfo | |
| 101 | 153 | } |
| 102 | 154 | |
| 103 | 155 | if (self.superblock.fileSize() != try file.getEndPos()) |
| 104 | return error.CorruptedFile; // Should always stand. | |
| 156 | return error.InvalidDebugInfo; // Should always stand. | |
| 105 | 157 | |
| 106 | 158 | self.directory = try MsfStream.init( |
| 107 | 159 | self.superblock.block_size, |
| ... | ... | @@ -165,12 +217,18 @@ const SuperBlock = struct { |
| 165 | 217 | }; |
| 166 | 218 | |
| 167 | 219 | const MsfStream = struct { |
| 168 | in_file: *os.File, | |
| 220 | in_file: os.File, | |
| 169 | 221 | pos: usize, |
| 170 | 222 | blocks: ArrayList(u32), |
| 171 | 223 | block_size: u32, |
| 172 | 224 | |
| 173 | fn init(block_size: u32, block_count: u32, pos: usize, file: *os.File, allocator: *mem.Allocator) !MsfStream { | |
| 225 | /// Implementation of InStream trait for Pdb.MsfStream | |
| 226 | stream: Stream, | |
| 227 | ||
| 228 | pub const Error = @typeOf(read).ReturnType.ErrorSet; | |
| 229 | pub const Stream = io.InStream(Error); | |
| 230 | ||
| 231 | fn init(block_size: u32, block_count: u32, pos: usize, file: os.File, allocator: *mem.Allocator) !MsfStream { | |
| 174 | 232 | var stream = MsfStream { |
| 175 | 233 | .in_file = file, |
| 176 | 234 | .pos = 0, |
| ... | ... | @@ -198,6 +256,18 @@ const MsfStream = struct { |
| 198 | 256 | return stream; |
| 199 | 257 | } |
| 200 | 258 | |
| 259 | fn readNullTermString(self: *MsfStream, allocator: *mem.Allocator) ![]u8 { | |
| 260 | var list = ArrayList(u8).init(allocator); | |
| 261 | defer list.deinit(); | |
| 262 | while (true) { | |
| 263 | const byte = try self.stream.readByte(); | |
| 264 | if (byte == 0) { | |
| 265 | return list.toSlice(); | |
| 266 | } | |
| 267 | try list.append(byte); | |
| 268 | } | |
| 269 | } | |
| 270 | ||
| 201 | 271 | fn read(self: *MsfStream, buffer: []u8) !usize { |
| 202 | 272 | var block_id = self.pos / self.block_size; |
| 203 | 273 | var block = self.blocks.items[block_id]; |
| ... | ... | @@ -252,12 +322,6 @@ const MsfStream = struct { |
| 252 | 322 | return block * self.block_size + offset; |
| 253 | 323 | } |
| 254 | 324 | |
| 255 | /// Implementation of InStream trait for Pdb.MsfStream | |
| 256 | pub const Error = @typeOf(read).ReturnType.ErrorSet; | |
| 257 | pub const Stream = io.InStream(Error); | |
| 258 | ||
| 259 | stream: Stream, | |
| 260 | ||
| 261 | 325 | fn readFn(in_stream: *Stream, buffer: []u8) Error!usize { |
| 262 | 326 | const self = @fieldParentPtr(MsfStream, "stream", in_stream); |
| 263 | 327 | return self.read(buffer); |