字段
file_reader: *File.Reader
msf: Msf
allocator: Allocator
string_table: ?*MsfStream
ipi: ?[]u8
modules: []Module
sect_contribs: []pdb.SectionContribEntry
guid: [16]u8
age: u32
stdIoFileAllocatorpdbassertPdbModuleinitdeinitparseDbiStreamparseIpiStreamparseInfoStreamgetProcSymInlineSiteSymIteratorBinaryAnnotationfindInlineeNamegetInlineesgetBinaryAnnotationsgetInlineSiteSourceLocationgetFileNamegetSymbolNameinlineeSourceLineLessThancompareInlineeSourceLineInlineeInlineeSourceLocationIteratorgetInlineeSourceLinesgetLineNumberInfogetModulegetStreamByIdgetStreamMsfMsfStreamreadSparseBitVectorblockCountFromSize
std.debug.Pdb.Allocator
别名
别名目标 std.mem.Allocator
std.debug.Pdb.assert
别名
别名目标 std.debug.assert
std.debug.Pdb.BinaryAnnotation
容器
字段
code_offset: u32
change_code_offset_base: u32
change_code_offset: u32
change_code_length: u32
change_file: u32
change_line_offset: i32
change_line_end_delta: u32
change_range_kind: RangeKind
change_column_start: u32
change_column_end_delta: i32
change_code_offset_and_line_offset: struct { code_delta: u32, line_delta: i32 }
change_code_length_and_code_offset: struct { length: u32, delta: u32 }
change_column_end: u32
std.debug.Pdb.BinaryAnnotation.decodeI32
函数
pub fn decodeI32(u: u32) i32
参数
u: u32
std.debug.Pdb.BinaryAnnotation.expect
函数
fn expect(value: anytype) error{ReadFailed}!@typeInfo(@TypeOf(value)).error_union.payload
错误
- ReadFailed
std.debug.Pdb.BinaryAnnotation.Iterator
容器
字段
std.debug.Pdb.BinaryAnnotation.Iterator.empty
常量
Iterator
std.debug.Pdb.BinaryAnnotation.Iterator.next
函数
pub fn next(self: *Iterator) error{InvalidDebugInfo}!?BinaryAnnotation
参数
self: *Iterator
错误
- InvalidDebugInfo
std.debug.Pdb.BinaryAnnotation.RangeIterator
容器
处理二进制注解的虚拟机。
字段
std.debug.Pdb.BinaryAnnotation.RangeIterator.init
函数
pub fn init(annotations: Iterator) RangeIterator
参数
annotations: Iterator
std.debug.Pdb.BinaryAnnotation.RangeIterator.next
函数
pub fn next(self: *RangeIterator) error{InvalidDebugInfo}!?Range
参数
self: *RangeIterator
错误
- InvalidDebugInfo
std.debug.Pdb.BinaryAnnotation.RangeIterator.PartialRange
容器
字段
line_offset: i32
file_id: ?u32
code_offset: u32
code_length: ?u32
std.debug.Pdb.BinaryAnnotation.RangeIterator.PartialRange.resolve
函数
将不完整范围解析为长度确定的范围; 如果无法解析,则返回 null。
fn resolve(self: PartialRange, next_code_offset: ?u32) ?Range
参数
self: PartialRange
next_code_offset: ?u32
std.debug.Pdb.BinaryAnnotation.RangeIterator.Range
容器
字段
line_offset: i32
file_id: ?u32
code_offset: u32
code_length: u32
std.debug.Pdb.BinaryAnnotation.RangeIterator.Range.contains
函数
pub fn contains(self: Range, offset_in_func: usize) bool
参数
self: Range
offset_in_func: usize
std.debug.Pdb.BinaryAnnotation.RangeKind
容器
字段
expression = 0
statement = 1
std.debug.Pdb.BinaryAnnotation.take
函数
pub fn take(reader: *Io.Reader) Io.Reader.Error!BinaryAnnotation
参数
错误
- EndOfStream
- ReadFailed
详细诊断参见
Reader的实现。
std.debug.Pdb.BinaryAnnotation.takePackedI32
函数
pub fn takePackedI32(reader: *Io.Reader) Io.Reader.Error!i32
参数
错误
- EndOfStream
- ReadFailed
详细诊断参见
Reader的实现。
std.debug.Pdb.BinaryAnnotation.takePackedU32
函数
pub fn takePackedU32(reader: *Io.Reader) Io.Reader.Error!u32
参数
错误
- EndOfStream
- ReadFailed
详细诊断参见
Reader的实现。
std.debug.Pdb.blockCountFromSize
函数
fn blockCountFromSize(size: u32, block_size: u32) u32
参数
size: u32
block_size: u32
std.debug.Pdb.compareInlineeSourceLineInlinee
函数
fn compareInlineeSourceLineInlinee( inlinee: u32, inlinee_src_line: *align(1) const pdb.InlineeSourceLine, ) std.math.Order
参数
inlinee: u32
inlinee_src_line: *align(1) const pdb.InlineeSourceLine
std.debug.Pdb.deinit
函数
pub fn deinit(self: *Pdb) void
参数
self: *Pdb
std.debug.Pdb.File
别名
别名目标 std.Io.File
std.debug.Pdb.findInlineeName
函数
pub fn findInlineeName(self: *const Pdb, inlinee: u32) ?[]const u8
参数
self: *const Pdb
inlinee: u32
std.debug.Pdb.getBinaryAnnotations
函数
pub fn getBinaryAnnotations(self: *Pdb, module: *Module, site: *align(1) const pdb.InlineSiteSym) BinaryAnnotation.Iterator
参数
std.debug.Pdb.getFileName
函数
pub fn getFileName(self: *Pdb, gpa: Allocator, mod: *Module, file_id: u32) ![]const u8
参数
std.debug.Pdb.getInlinees
函数
pub fn getInlinees(self: *Pdb, module: *Module, proc_sym: *align(1) const pdb.ProcSym) InlineSiteSymIterator
参数
std.debug.Pdb.getInlineeSourceLines
函数
返回给定模块中指定被内联函数的全部 pdb.InlineeSourceLine。理想情况下,
每个被内联函数只有一个条目,但 LLVM 似乎给所有同名函数分配
同一个 inlinee 标识符。这是一个缺陷:https://github.com/llvm/llvm-project/issues/191787
目前调用方最多只能输出所有结果。
pub fn getInlineeSourceLines(self: *Pdb, mod: *Module, inlinee: u32) InlineeSourceLocationIterator
参数
std.debug.Pdb.getInlineSiteSourceLocation
函数
pub fn getInlineSiteSourceLocation( self: *Pdb, gpa: Allocator, mod: *Module, site: *align(1) const pdb.InlineSiteSym, inlinee_src_line: *align(1) const pdb.InlineeSourceLine, offset_in_func: usize, ) !?std.debug.SourceLocation
参数
self: *Pdb
gpa: Allocator
mod: *Module
site: *align(1) const pdb.InlineSiteSym
inlinee_src_line: *align(1) const pdb.InlineeSourceLine
offset_in_func: usize
std.debug.Pdb.getLineNumberInfo
函数
pub fn getLineNumberInfo(self: *Pdb, gpa: Allocator, module: *Module, address: u64) !std.debug.SourceLocation
参数
std.debug.Pdb.getModule
函数
pub fn getModule(self: *Pdb, index: usize) !?*Module
参数
self: *Pdb
index: usize
std.debug.Pdb.getProcSym
函数
pub fn getProcSym(self: *Pdb, module: *Module, address: u64) ?*align(1) pdb.ProcSym
参数
std.debug.Pdb.getStream
函数
pub fn getStream(self: *Pdb, stream: pdb.StreamType) ?*MsfStream
参数
self: *Pdb
stream: pdb.StreamType
std.debug.Pdb.getStreamById
函数
pub fn getStreamById(self: *Pdb, id: u32) ?*MsfStream
参数
self: *Pdb
id: u32
std.debug.Pdb.getSymbolName
函数
pub fn getSymbolName(self: *Pdb, proc_sym: *align(1) const pdb.ProcSym) []const u8
参数
std.debug.Pdb.init
函数
pub fn init(gpa: Allocator, file_reader: *File.Reader) !Pdb
参数
std.debug.Pdb.inlineeSourceLineLessThan
函数
fn inlineeSourceLineLessThan( _: void, lhs: *align(1) const pdb.InlineeSourceLine, rhs: *align(1) const pdb.InlineeSourceLine, ) bool
参数
std.debug.Pdb.InlineeSourceLocationIterator
容器
字段
lines: []*align(1) const pdb.InlineeSourceLine迭代器假定切片中的所有源码行属于同一个 被内联函数,且先按文件、再按行号排序。
std.debug.Pdb.InlineeSourceLocationIterator.empty
常量
InlineeSourceLocationIterator
std.debug.Pdb.InlineeSourceLocationIterator.next
函数
pub fn next(iter: *InlineeSourceLocationIterator) ?*align(1) const pdb.InlineeSourceLine
参数
std.debug.Pdb.InlineSiteSymIterator
容器
字段
module_index: usize
offset: usize
end: usize
std.debug.Pdb.InlineSiteSymIterator.empty
常量
InlineSiteSymIterator
std.debug.Pdb.InlineSiteSymIterator.next
函数
pub fn next(iter: *InlineSiteSymIterator, module: *Module) ?*align(1) pdb.InlineSiteSym
参数
iter: *InlineSiteSymIterator
module: *Module
std.debug.Pdb.Io
别名
别名目标 std.Io
std.debug.Pdb.Module
容器
字段
mod_info: pdb.ModInfo
module_name: []u8
obj_file_name: []u8
populated: bool
symbols: []u8
subsect_info: []u8
checksum_offset: ?usize
inlinee_source_lines: []*align(1) const pdb.InlineeSourceLine被内联函数的源码行,依次按被内联函数、文件、行号排序,避免反复线性搜索全部被内联函数。
由于 LLVM 有时会为同一个被内联函数标识输出多条记录,因此优先使用二分搜索,而不是哈希映射;详情见 getInlineeSourceLines。
std.debug.Pdb.Module.deinit
函数
pub fn deinit(self: *Module, allocator: Allocator) void
参数
std.debug.Pdb.Msf
容器
字段
std.debug.Pdb.Msf.deinit
函数
fn deinit(self: *Msf, gpa: Allocator) void
参数
std.debug.Pdb.Msf.init
函数
fn init(gpa: Allocator, file_reader: *File.Reader) !Msf
参数
std.debug.Pdb.MsfStream
容器
字段
std.debug.Pdb.MsfStream.empty
常量
MsfStream
std.debug.Pdb.MsfStream.Error
别名
std.debug.Pdb.MsfStream.getFilePos
函数
fn getFilePos(ms: *const MsfStream) u64
参数
ms: *const MsfStream
std.debug.Pdb.MsfStream.getSize
函数
fn getSize(ms: *const MsfStream) u64
参数
ms: *const MsfStream
std.debug.Pdb.MsfStream.init
函数
fn init(block_size: u32, file_reader: *File.Reader, blocks: []u32, buffer: []u8) MsfStream
参数
std.debug.Pdb.MsfStream.logicalPos
函数
pub fn logicalPos(ms: *const MsfStream) u64
参数
ms: *const MsfStream
std.debug.Pdb.MsfStream.seekBy
函数
pub fn seekBy(ms: *MsfStream, len: i64) !void
参数
ms: *MsfStream
len: i64
std.debug.Pdb.MsfStream.seekTo
函数
pub fn seekTo(ms: *MsfStream, len: u64) !void
参数
ms: *MsfStream
len: u64
std.debug.Pdb.MsfStream.stream
函数
fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize
参数
错误
std.debug.Pdb.parseDbiStream
函数
pub fn parseDbiStream(self: *Pdb) !void
参数
self: *Pdb
std.debug.Pdb.parseInfoStream
函数
pub fn parseInfoStream(self: *Pdb) !void
参数
self: *Pdb
std.debug.Pdb.parseInfoStream.maxLoad
函数
fn maxLoad(cap: u32) u32
参数
cap: u32
std.debug.Pdb.parseIpiStream
函数
pub fn parseIpiStream(self: *Pdb) !void
参数
self: *Pdb
std.debug.Pdb.pdb
别名
别名目标 std.pdb
std.debug.Pdb.Pdb
别名
别名目标 std.debug.Pdb
std.debug.Pdb.readSparseBitVector
函数
fn readSparseBitVector(reader: *Io.Reader, gpa: Allocator) ![]u32
参数
std.debug.Pdb.std
别名
别名目标 std
源码
std/debug/Pdb.zig
const std = @import("../std.zig");
const Io = std.Io;
const File = Io.File;
const Allocator = std.mem.Allocator;
const pdb = std.pdb;
const assert = std.debug.assert;
const Pdb = @This();
file_reader: *File.Reader,
msf: Msf,
allocator: Allocator,
string_table: ?*MsfStream,
ipi: ?[]u8,
modules: []Module,
sect_contribs: []pdb.SectionContribEntry,
guid: [16]u8,
age: u32,
pub const Module = struct {
mod_info: pdb.ModInfo,
module_name: []u8,
obj_file_name: []u8,
// 以下字段按需填充。
populated: bool,
symbols: []u8,
subsect_info: []u8,
checksum_offset: ?usize,
/// 被内联函数的源码行,依次按被内联函数、文件、行号排序,避免反复线性搜索全部被内联函数。
/// 由于 LLVM 有时会为同一个被内联函数标识输出多条记录,因此优先使用二分搜索,而不是哈希映射;详情见 `getInlineeSourceLines`。
///
///
inlinee_source_lines: []*align(1) const pdb.InlineeSourceLine,
pub fn deinit(self: *Module, allocator: Allocator) void {
allocator.free(self.module_name);
allocator.free(self.obj_file_name);
if (self.populated) {
allocator.free(self.symbols);
allocator.free(self.subsect_info);
allocator.free(self.inlinee_source_lines);
}
}
};
pub fn init(gpa: Allocator, file_reader: *File.Reader) !Pdb {
return .{
.file_reader = file_reader,
.allocator = gpa,
.string_table = null,
.ipi = null,
.msf = try Msf.init(gpa, file_reader),
.modules = &.{},
.sect_contribs = &.{},
.guid = undefined,
.age = undefined,
};
}
pub fn deinit(self: *Pdb) void {
const gpa = self.allocator;
self.msf.deinit(gpa);
if (self.ipi) |ipi| gpa.free(ipi);
for (self.modules) |*module| {
module.deinit(gpa);
}
gpa.free(self.modules);
gpa.free(self.sect_contribs);
}
pub fn parseDbiStream(self: *Pdb) !void {
var stream = self.getStream(pdb.StreamType.dbi) orelse
return error.InvalidDebugInfo;
const gpa = self.allocator;
const reader = &stream.interface;
const header = try reader.takeStruct(pdb.DbiStreamHeader, .little);
if (header.version_header != 19990903) // V70,LLVM 团队唯一观察到的值
return error.UnknownPDBVersion;
// if (header.Age != age)
// return error.UnmatchingPDB;
const mod_info_size = header.mod_info_size;
const section_contrib_size = header.section_contribution_size;
var modules: std.ArrayList(Module) = .empty;
defer modules.deinit(gpa);
// 模块信息子流
var mod_info_offset: usize = 0;
while (mod_info_offset != mod_info_size) {
const mod_info = try reader.takeStruct(pdb.ModInfo, .little);
var this_record_len: usize = @sizeOf(pdb.ModInfo);
var module_name: Io.Writer.Allocating = .init(gpa);
defer module_name.deinit();
this_record_len += try reader.streamDelimiterLimit(&module_name.writer, 0, .limited(1024));
assert(reader.buffered()[0] == 0); // TODO 更改 streamDelimiterLimit API
reader.toss(1);
this_record_len += 1;
var obj_file_name: Io.Writer.Allocating = .init(gpa);
defer obj_file_name.deinit();
this_record_len += try reader.streamDelimiterLimit(&obj_file_name.writer, 0, .limited(1024));
assert(reader.buffered()[0] == 0); // TODO 更改 streamDelimiterLimit API
reader.toss(1);
this_record_len += 1;
if (this_record_len % 4 != 0) {
const round_to_next_4 = (this_record_len | 0x3) + 1;
const march_forward_bytes = round_to_next_4 - this_record_len;
try stream.seekBy(@as(isize, @intCast(march_forward_bytes)));
this_record_len += march_forward_bytes;
}
try modules.ensureUnusedCapacity(gpa, 1);
const module_name_slice = try module_name.toOwnedSlice();
errdefer gpa.free(module_name_slice);
const obj_file_name_slice = try obj_file_name.toOwnedSlice();
errdefer gpa.free(obj_file_name_slice);
modules.appendAssumeCapacity(.{
.mod_info = mod_info,
.module_name = module_name_slice,
.obj_file_name = obj_file_name_slice,
.populated = false,
.symbols = undefined,
.subsect_info = undefined,
.checksum_offset = null,
.inlinee_source_lines = undefined,
});
mod_info_offset += this_record_len;
if (mod_info_offset > mod_info_size)
return error.InvalidDebugInfo;
}
// 节贡献子流
var sect_contribs: std.ArrayList(pdb.SectionContribEntry) = .empty;
defer sect_contribs.deinit(gpa);
var sect_cont_offset: usize = 0;
if (section_contrib_size != 0) {
const version = reader.takeEnum(pdb.SectionContrSubstreamVersion, .little) catch |err| switch (err) {
error.InvalidEnumTag, error.EndOfStream => return error.InvalidDebugInfo,
error.ReadFailed => |e| return e,
};
_ = version;
sect_cont_offset += @sizeOf(u32);
}
while (sect_cont_offset != section_contrib_size) {
const entry = try sect_contribs.addOne(gpa);
entry.* = try reader.takeStruct(pdb.SectionContribEntry, .little);
sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
if (sect_cont_offset > section_contrib_size)
return error.InvalidDebugInfo;
}
try sect_contribs.shrinkToLen(gpa);
try modules.shrinkToLen(gpa);
self.sect_contribs = sect_contribs.toOwnedSliceAssert();
self.modules = modules.toOwnedSliceAssert();
}
pub fn parseIpiStream(self: *Pdb) !void {
const gpa = self.allocator;
const stream = self.getStream(.ipi) orelse return;
const header = try stream.interface.peekStruct(pdb.IpiStreamHeader, .little);
if (header.version != .v80) // LLVM 团队唯一观察到的值
return error.UnknownPDBVersion;
self.ipi = try stream.interface.readAllocAll(gpa, @sizeOf(pdb.IpiStreamHeader) + header.type_record_bytes);
}
pub fn parseInfoStream(self: *Pdb) !void {
var stream = self.getStream(pdb.StreamType.pdb) orelse return error.InvalidDebugInfo;
const reader = &stream.interface;
// 解析 InfoStreamHeader。
const version = try reader.takeInt(u32, .little);
const signature = try reader.takeInt(u32, .little);
_ = signature;
const age = try reader.takeInt(u32, .little);
const guid = try reader.takeArray(16);
if (version != 20000404) // VC70,LLVM 团队观察到的唯一值
return error.UnknownPDBVersion;
self.guid = guid.*;
self.age = age;
const gpa = self.allocator;
// 查找字符串表。
const string_table_index = str_tab_index: {
const name_bytes_len = try reader.takeInt(u32, .little);
const name_bytes = try reader.readAllocAll(gpa, name_bytes_len);
defer gpa.free(name_bytes);
const HashTableHeader = extern struct {
size: u32,
capacity: u32,
fn maxLoad(cap: u32) u32 {
return cap * 2 / 3 + 1;
}
};
const hash_tbl_hdr = try reader.takeStruct(HashTableHeader, .little);
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(reader, gpa);
defer gpa.free(present);
if (present.len != hash_tbl_hdr.size)
return error.InvalidDebugInfo;
const deleted = try readSparseBitVector(reader, gpa);
defer gpa.free(deleted);
for (present) |_| {
const name_offset = try reader.takeInt(u32, .little);
const name_index = try reader.takeInt(u32, .little);
if (name_offset > name_bytes.len)
return error.InvalidDebugInfo;
const name = std.mem.sliceTo(name_bytes[name_offset..], 0);
if (std.mem.eql(u8, name, "/names")) {
break :str_tab_index name_index;
}
}
return error.MissingDebugInfo;
};
self.string_table = self.getStreamById(string_table_index) orelse
return error.MissingDebugInfo;
}
pub fn getProcSym(self: *Pdb, module: *Module, address: u64) ?*align(1) pdb.ProcSym {
_ = self;
std.debug.assert(module.populated);
var reader: Io.Reader = .fixed(module.symbols);
while (true) {
const prefix = reader.takeStructPointer(pdb.RecordPrefix) catch return null;
if (prefix.record_len < 2)
return null;
reader.discardAll(prefix.record_len - @sizeOf(u16)) catch return null;
switch (prefix.record_kind) {
.lproc32, .gproc32 => {
const proc_sym: *align(1) pdb.ProcSym = @ptrCast(prefix);
if (address >= proc_sym.code_offset and address < proc_sym.code_offset + proc_sym.code_size) {
return proc_sym;
}
},
else => {},
}
}
return null;
}
pub const InlineSiteSymIterator = struct {
module_index: usize,
offset: usize,
end: usize,
const empty: InlineSiteSymIterator = .{
.module_index = 0,
.offset = 0,
.end = 0,
};
pub fn next(iter: *InlineSiteSymIterator, module: *Module) ?*align(1) pdb.InlineSiteSym {
while (iter.offset < iter.end) {
const inline_prefix: *align(1) pdb.RecordPrefix = @ptrCast(&module.symbols[iter.offset]);
const end = iter.offset + inline_prefix.record_len + @sizeOf(u16);
if (end > iter.end) return null;
defer iter.offset = end;
switch (inline_prefix.record_kind) {
// 跳过嵌套过程
.lproc32,
.lproc32_st,
.gproc32,
.gproc32_st,
.lproc32_id,
.gproc32_id,
.lproc32_dpc,
.lproc32_dpc_id,
=> {
const skip: *align(1) pdb.ProcSym = @ptrCast(inline_prefix);
iter.offset = skip.end;
},
.inlinesite,
.inlinesite2,
=> return @ptrCast(inline_prefix),
else => {},
}
}
return null;
}
};
pub const BinaryAnnotation = union(enum) {
code_offset: u32,
change_code_offset_base: u32,
change_code_offset: u32,
change_code_length: u32,
change_file: u32,
change_line_offset: i32,
change_line_end_delta: u32,
change_range_kind: RangeKind,
change_column_start: u32,
change_column_end_delta: i32,
change_code_offset_and_line_offset: struct { code_delta: u32, line_delta: i32 },
change_code_length_and_code_offset: struct { length: u32, delta: u32 },
change_column_end: u32,
pub const RangeKind = enum(u32) { expression = 0, statement = 1 };
/// 处理二进制注解的虚拟机。
pub const RangeIterator = struct {
annotations: Iterator,
curr: PartialRange,
/// 跟踪前一个范围,因为代码长度有时由后续
/// 范围隐含确定。
prev: ?PartialRange,
const PartialRange = struct {
line_offset: i32,
file_id: ?u32,
code_offset: u32,
code_length: ?u32,
/// 将不完整范围解析为长度确定的范围;
/// 如果无法解析,则返回 null。
fn resolve(self: PartialRange, next_code_offset: ?u32) ?Range {
return .{
.line_offset = self.line_offset,
.file_id = self.file_id,
.code_offset = self.code_offset,
.code_length = b: {
if (self.code_length) |l| break :b l;
const end = next_code_offset orelse return null;
break :b end - self.code_offset;
},
};
}
};
pub fn init(annotations: Iterator) RangeIterator {
return .{
.annotations = annotations,
.curr = .{
.line_offset = 0,
.file_id = null,
.code_offset = 0,
.code_length = null,
},
.prev = null,
};
}
pub const Range = struct {
line_offset: i32,
file_id: ?u32,
code_offset: u32,
code_length: u32,
pub fn contains(self: Range, offset_in_func: usize) bool {
return self.code_offset <= offset_in_func and
offset_in_func < self.code_offset + self.code_length;
}
};
pub fn next(self: *RangeIterator) error{InvalidDebugInfo}!?Range {
while (try self.annotations.next()) |annotation| {
switch (annotation) {
.change_code_offset => |delta| {
self.curr.code_offset += delta;
},
.change_code_length => |length| {
if (self.prev) |*prev| prev.code_length = prev.code_length orelse length;
self.curr.code_offset += length;
},
// LLVM 有生成这些内容的代码,但未能确定如何触发,
// 因此此逻辑未经测试。
.change_file => |file_id| {
self.curr.file_id = file_id;
},
// LLVM 从不输出此操作码,但其解释方式足够明确,因此仍进行处理,以防今后输出
//
.change_code_length_and_code_offset => |info| {
self.curr.code_length = info.length;
self.curr.code_offset += info.delta;
},
.change_line_offset => |delta| {
self.curr.line_offset += delta;
},
.change_code_offset_and_line_offset => |info| {
self.curr.code_offset += info.code_delta;
self.curr.line_offset += info.line_delta;
},
// 编写时 LLVM 尚不输出此内容,且不希望在没有测试用例的情况下添加支持。
// 目前未使用此信息,因此可以安全忽略。
.change_line_end_delta,
.change_column_start,
.change_column_end_delta,
.change_column_end,
=> {},
// 编写时 LLVM 尚不输出这些操作码。不同资料对其解释存在冲突,
// 因此不尝试处理。
.code_offset,
.change_code_offset_base,
.change_range_kind,
=> {
self.annotations = .empty;
self.prev = null;
return null;
},
}
// 如果得到新的代码偏移量,且存在前一个范围,则返回前一个范围,
// 必要时确定其长度。
switch (annotation) {
.change_code_offset,
.change_code_offset_and_line_offset,
.change_code_length_and_code_offset,
=> {},
else => continue,
}
defer self.prev = self.curr;
const prev = self.prev orelse continue;
return prev.resolve(self.curr.code_offset);
}
// 若已处理所有二元运算,但仍剩余一个已知长度的前序范围,
// 则返回该范围。
const prev = self.prev orelse return null;
defer self.prev = null;
return prev.resolve(null);
}
};
pub const Iterator = struct {
reader: Io.Reader,
pub const empty: Iterator = .{ .reader = .ending_instance };
pub fn next(self: *Iterator) error{InvalidDebugInfo}!?BinaryAnnotation {
return take(&self.reader) catch |err| switch (err) {
error.ReadFailed => return error.InvalidDebugInfo,
error.EndOfStream => return null,
};
}
};
pub fn take(reader: *Io.Reader) Io.Reader.Error!BinaryAnnotation {
const op = std.enums.fromInt(
pdb.BinaryAnnotationOpcode,
try takePackedU32(reader),
) orelse return error.ReadFailed;
switch (op) {
// 微软文档称 invalid 用作填充,但未明确
// 说明是允许内部填充,还是仅允许所有指令结束后的填充。
// 根据实验,似乎是后者,至少在我测试过的 LLVM
// 输出中如此。
.invalid => return error.EndOfStream,
.code_offset => return .{
.code_offset = try expect(takePackedU32(reader)),
},
.change_code_offset_base => return .{
.change_code_offset_base = try expect(takePackedU32(reader)),
},
.change_code_offset => return .{
.change_code_offset = try expect(takePackedU32(reader)),
},
.change_code_length => return .{
.change_code_length = try expect(takePackedU32(reader)),
},
.change_file => return .{
.change_file = try expect(takePackedU32(reader)),
},
.change_line_offset => return .{
.change_line_offset = try expect(takePackedI32(reader)),
},
.change_line_end_delta => return .{
.change_line_end_delta = try expect(takePackedU32(reader)),
},
.change_range_kind => return .{
.change_range_kind = std.enums.fromInt(
RangeKind,
try expect(takePackedU32(reader)),
) orelse return error.ReadFailed,
},
.change_column_start => return .{
.change_column_start = try expect(takePackedU32(reader)),
},
.change_column_end_delta => return .{
.change_column_end_delta = try expect(takePackedI32(reader)),
},
.change_code_offset_and_line_offset => {
const EncodedArgs = packed struct(u32) {
code_delta: u4,
encoded_line_delta: u28,
};
const args: EncodedArgs = @bitCast(try expect(takePackedU32(reader)));
return .{
.change_code_offset_and_line_offset = .{
.code_delta = args.code_delta,
.line_delta = decodeI32(args.encoded_line_delta),
},
};
},
.change_code_length_and_code_offset => return .{
.change_code_length_and_code_offset = .{
.length = try expect(takePackedU32(reader)),
.delta = try expect(takePackedU32(reader)),
},
},
.change_column_end => return .{
.change_column_end = try expect(takePackedU32(reader)),
},
}
}
// 改编自:
// https://github.com/microsoft/microsoft-pdb/blob/805655a28bd8198004be2ac27e6e0290121a5e89/include/cvinfo.h#L4942
pub fn takePackedU32(reader: *Io.Reader) Io.Reader.Error!u32 {
const b0: u32 = try reader.takeByte();
if (b0 & 0x80 == 0x00) return b0;
const b1: u32 = try reader.takeByte();
if (b0 & 0xC0 == 0x80) return ((b0 & 0x3F) << 8) | b1;
const b2: u32 = try reader.takeByte();
const b3: u32 = try reader.takeByte();
if (b0 & 0xE0 == 0xC0) return ((b0 & 0x1f) << 24) | (b1 << 16) | (b2 << 8) | b3;
return error.ReadFailed;
}
pub fn takePackedI32(reader: *Io.Reader) Io.Reader.Error!i32 {
return decodeI32(try takePackedU32(reader));
}
pub fn decodeI32(u: u32) i32 {
const i: i32 = @bitCast(u);
if (i & 1 != 0) {
return -(i >> 1);
} else {
return i >> 1;
}
}
fn expect(value: anytype) error{ReadFailed}!@typeInfo(@TypeOf(value)).error_union.payload {
comptime assert(@typeInfo(@TypeOf(value)).error_union.error_set == Io.Reader.Error);
return value catch error.ReadFailed;
}
};
pub fn findInlineeName(self: *const Pdb, inlinee: u32) ?[]const u8 {
// 根据 LLVM 文档,最高位可以表示类型索引来自 ipi 流,此时需要清除此位。LLVM 不会以此方式生成数据,但由于只需执行一次按位与,这里仍加以处理。
// https://llvm.org/docs/PDB/TpiStream.html#type-indices
//
//
const type_index = inlinee & 0x7FFFFFFF;
var reader: Io.Reader = .fixed(self.ipi orelse return null);
const header = reader.takeStructPointer(pdb.IpiStreamHeader) catch return null;
for (header.type_index_begin..header.type_index_end) |curr_type_index| {
const prefix = reader.takeStructPointer(pdb.LfRecordPrefix) catch return null;
if (prefix.len < 2) return null;
reader.discardAll(prefix.len - @sizeOf(u16)) catch return null;
if (curr_type_index == type_index) {
switch (prefix.kind) {
.func_id => {
const func: *align(1) pdb.LfFuncId = @ptrCast(prefix);
return std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&func.name[0])), 0);
},
.mfunc_id => {
const func: *align(1) pdb.LfMFuncId = @ptrCast(prefix);
return std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&func.name[0])), 0);
},
else => return null,
}
}
}
return null;
}
pub fn getInlinees(self: *Pdb, module: *Module, proc_sym: *align(1) const pdb.ProcSym) InlineSiteSymIterator {
const module_index = module - self.modules.ptr;
const offset = @intFromPtr(proc_sym) -
@intFromPtr(module.symbols.ptr) +
proc_sym.record_len +
@sizeOf(u16);
const symbols_end = @intFromPtr(module.symbols.ptr) + module.symbols.len;
if (offset > symbols_end or proc_sym.end > symbols_end) return .empty;
return .{
.module_index = module_index,
.offset = offset,
.end = proc_sym.end,
};
}
pub fn getBinaryAnnotations(self: *Pdb, module: *Module, site: *align(1) const pdb.InlineSiteSym) BinaryAnnotation.Iterator {
_ = self;
var start: usize = @intFromPtr(site) + @sizeOf(pdb.InlineSiteSym);
var end = start + site.record_len + @sizeOf(u16) - @sizeOf(pdb.InlineSiteSym);
switch (site.record_kind) {
.inlinesite => {},
.inlinesite2 => start += @sizeOf(pdb.InlineSiteSym2) - @sizeOf(pdb.InlineSiteSym),
else => end = start,
}
if (start < @intFromPtr(module.symbols.ptr) or end > @intFromPtr(module.symbols.ptr) + module.symbols.len) return .empty;
const len = end - start;
const ptr: [*]const u8 = @ptrFromInt(start);
const slice = ptr[0..len];
return .{ .reader = Io.Reader.fixed(slice) };
}
pub fn getInlineSiteSourceLocation(
self: *Pdb,
gpa: Allocator,
mod: *Module,
site: *align(1) const pdb.InlineSiteSym,
inlinee_src_line: *align(1) const pdb.InlineeSourceLine,
offset_in_func: usize,
) !?std.debug.SourceLocation {
var ranges: BinaryAnnotation.RangeIterator = .init(self.getBinaryAnnotations(mod, site));
while (try ranges.next()) |range| {
if (!range.contains(offset_in_func)) continue;
const file_id = range.file_id orelse inlinee_src_line.file_id;
const file_name = try self.getFileName(gpa, mod, file_id);
errdefer self.allocator.free(file_name);
return .{
.line = inlinee_src_line.source_line_num +% @as(u32, @bitCast(range.line_offset)),
// LLVM 目前不会在 PDB 中为内联调用输出列信息。
.column = 0,
.file_name = file_name,
};
}
return null;
}
pub fn getFileName(self: *Pdb, gpa: Allocator, mod: *Module, file_id: u32) ![]const u8 {
const checksum_offset = mod.checksum_offset orelse return error.MissingDebugInfo;
const subsect_index = checksum_offset + file_id;
const chksum_hdr: *align(1) pdb.FileChecksumEntryHeader = @ptrCast(&mod.subsect_info[subsect_index]);
const strtab_offset = @sizeOf(pdb.StringTableHeader) + chksum_hdr.file_name_offset;
self.string_table.?.seekTo(strtab_offset) catch return error.InvalidDebugInfo;
const string_reader = &self.string_table.?.interface;
var source_file_name: Io.Writer.Allocating = .init(gpa);
defer source_file_name.deinit();
_ = try string_reader.streamDelimiterLimit(&source_file_name.writer, 0, .limited(1024));
assert(string_reader.buffered()[0] == 0); // TODO 更改 streamDelimiterLimit API
string_reader.toss(1);
return try source_file_name.toOwnedSlice();
}
pub fn getSymbolName(self: *Pdb, proc_sym: *align(1) const pdb.ProcSym) []const u8 {
_ = self;
return std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&proc_sym.name[0])), 0);
}
fn inlineeSourceLineLessThan(
_: void,
lhs: *align(1) const pdb.InlineeSourceLine,
rhs: *align(1) const pdb.InlineeSourceLine,
) bool {
if (lhs.inlinee < rhs.inlinee) return true;
if (lhs.inlinee > rhs.inlinee) return false;
if (lhs.file_id < rhs.file_id) return true;
if (lhs.file_id > rhs.file_id) return false;
return lhs.source_line_num < rhs.source_line_num;
}
fn compareInlineeSourceLineInlinee(
inlinee: u32,
inlinee_src_line: *align(1) const pdb.InlineeSourceLine,
) std.math.Order {
return std.math.order(inlinee, inlinee_src_line.inlinee);
}
pub const InlineeSourceLocationIterator = struct {
/// 迭代器假定切片中的所有源码行属于同一个
/// 被内联函数,且先按文件、再按行号排序。
lines: []*align(1) const pdb.InlineeSourceLine,
pub const empty: InlineeSourceLocationIterator = .{ .lines = &.{} };
pub fn next(iter: *InlineeSourceLocationIterator) ?*align(1) const pdb.InlineeSourceLine {
if (iter.lines.len == 0) return null;
const line = iter.lines[0];
iter.lines = iter.lines[1..];
// 过滤重复条目
while (iter.lines.len != 0 and
iter.lines[0].file_id == line.file_id and
iter.lines[0].source_line_num == line.source_line_num)
{
iter.lines = iter.lines[1..];
}
return line;
}
};
/// 返回给定模块中指定被内联函数的全部 `pdb.InlineeSourceLine`。理想情况下,
/// 每个被内联函数只有一个条目,但 LLVM 似乎给所有同名函数分配
/// 同一个 inlinee 标识符。这是一个缺陷:https://github.com/llvm/llvm-project/issues/191787
/// 目前调用方最多只能输出所有结果。
pub fn getInlineeSourceLines(self: *Pdb, mod: *Module, inlinee: u32) InlineeSourceLocationIterator {
_ = self;
// 二分查找到任意匹配项;若还有其他匹配项,它们将与此项相邻
const any = std.sort.binarySearch(
*align(1) const pdb.InlineeSourceLine,
mod.inlinee_source_lines,
inlinee,
compareInlineeSourceLineInlinee,
) orelse return .empty;
// 线性扫描至首个匹配项
const begin = b: {
var begin = any;
while (begin > 0) {
const prev = begin - 1;
if (mod.inlinee_source_lines[prev].inlinee != inlinee) break;
begin = prev;
}
break :b begin;
};
// 线性扫描至最后一个匹配项
const end = b: {
var end = any + 1;
while (end < mod.inlinee_source_lines.len and
mod.inlinee_source_lines[end].inlinee == inlinee) : (end += 1)
{}
break :b end;
};
// 返回遍历所有匹配项的迭代器(迭代器会过滤重复条目)
return .{ .lines = mod.inlinee_source_lines[begin..end] };
}
pub fn getLineNumberInfo(self: *Pdb, gpa: Allocator, module: *Module, address: u64) !std.debug.SourceLocation {
std.debug.assert(module.populated);
const subsect_info = module.subsect_info;
var sect_offset: usize = 0;
var skip_len: usize = undefined;
while (sect_offset != subsect_info.len) : (sect_offset += skip_len) {
const subsect_hdr: *align(1) pdb.DebugSubsectionHeader = @ptrCast(&subsect_info[sect_offset]);
skip_len = subsect_hdr.length;
sect_offset += @sizeOf(pdb.DebugSubsectionHeader);
switch (subsect_hdr.kind) {
.lines => {
var line_index = sect_offset;
const line_hdr: *align(1) pdb.LineFragmentHeader = @ptrCast(&subsect_info[line_index]);
if (line_hdr.reloc_segment == 0)
return error.MissingDebugInfo;
line_index += @sizeOf(pdb.LineFragmentHeader);
const frag_vaddr_start = line_hdr.reloc_offset;
const frag_vaddr_end = frag_vaddr_start + line_hdr.code_size;
if (address >= frag_vaddr_start and address < frag_vaddr_end) {
// 从此处开始有数量未知的 LineBlockFragmentHeader 及其配套的行列记录。
// 依次遍历,最终找到所需的 SourceLocation 后,
// 跳出到 :subsections。否则,确保不会读取此子节以外的数据。
const subsection_end_index = sect_offset + subsect_hdr.length;
while (line_index < subsection_end_index) {
const block_hdr: *align(1) pdb.LineBlockFragmentHeader = @ptrCast(&subsect_info[line_index]);
line_index += @sizeOf(pdb.LineBlockFragmentHeader);
const start_line_index = line_index;
const has_column = line_hdr.flags.have_columns;
// 所有行条目都在其行块中按起始地址升序存储。
// 启发式策略:查找最后一个
// 满足 vaddr_start <= address 的行条目。
// 通过简单的线性查找完成。
var line_i: u32 = 0;
while (line_i < block_hdr.num_lines) : (line_i += 1) {
const line_num_entry: *align(1) pdb.LineNumberEntry = @ptrCast(&subsect_info[line_index]);
line_index += @sizeOf(pdb.LineNumberEntry);
const vaddr_start = frag_vaddr_start + line_num_entry.offset;
if (address < vaddr_start) {
break;
}
}
// line_i == 0 表示未找到匹配的 pdb.LineNumberEntry。
if (line_i > 0) {
const file_name = try self.getFileName(gpa, module, block_hdr.name_index);
errdefer gpa.free(file_name);
const line_entry_idx = line_i - 1;
const column = if (has_column) blk: {
const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.num_lines;
const col_index = start_col_index + @sizeOf(pdb.ColumnNumberEntry) * line_entry_idx;
const col_num_entry: *align(1) pdb.ColumnNumberEntry = @ptrCast(&subsect_info[col_index]);
break :blk col_num_entry.start_column;
} else 0;
const found_line_index = start_line_index + line_entry_idx * @sizeOf(pdb.LineNumberEntry);
const line_num_entry: *align(1) pdb.LineNumberEntry = @ptrCast(&subsect_info[found_line_index]);
return .{
.file_name = file_name,
.line = line_num_entry.flags.start,
.column = column,
};
}
}
// 检查在可能包含多个块片段的数据之后,没有读取到无效数据。
if (line_index != subsection_end_index) {
return error.InvalidDebugInfo;
}
}
},
else => {},
}
if (sect_offset > subsect_info.len)
return error.InvalidDebugInfo;
}
return error.MissingDebugInfo;
}
pub fn getModule(self: *Pdb, index: usize) !?*Module {
if (index >= self.modules.len)
return null;
const mod = &self.modules[index];
if (mod.populated)
return mod;
// 至多一个可以非零。
if (mod.mod_info.c11_byte_size != 0 and mod.mod_info.c13_byte_size != 0)
return error.InvalidDebugInfo;
if (mod.mod_info.c13_byte_size == 0)
return error.InvalidDebugInfo;
const stream = self.getStreamById(mod.mod_info.module_sym_stream) orelse
return error.MissingDebugInfo;
const reader = &stream.interface;
const signature = try reader.takeInt(u32, .little);
if (signature != 4)
return error.InvalidDebugInfo;
const gpa = self.allocator;
mod.symbols = try reader.readAllocAll(gpa, mod.mod_info.sym_byte_size - 4);
errdefer gpa.free(mod.symbols);
mod.subsect_info = try reader.readAllocAll(gpa, mod.mod_info.c13_byte_size);
errdefer gpa.free(mod.subsect_info);
mod.inlinee_source_lines = b: {
var inlinee_source_lines: std.ArrayList(*align(1) const pdb.InlineeSourceLine) = .empty;
defer inlinee_source_lines.deinit(gpa);
var subsects: Io.Reader = .fixed(mod.subsect_info);
while (subsects.takeStructPointer(pdb.DebugSubsectionHeader) catch null) |subsect_hdr| {
var subsect: Io.Reader = .fixed(subsects.take(subsect_hdr.length) catch return null);
if (subsect_hdr.kind == .inlinee_lines) {
const inlinee_source_line_signature = subsect.takeEnum(pdb.InlineeSourceLineSignature, .little) catch return error.InvalidDebugInfo;
const has_extra_files = switch (inlinee_source_line_signature) {
.normal => false,
.ex => true,
else => continue,
};
while (subsect.takeStructPointer(pdb.InlineeSourceLine) catch null) |info| {
if (has_extra_files) {
const file_count = subsect.takeInt(u32, .little) catch
return error.InvalidDebugInfo;
const file_bytes = std.math.mul(usize, file_count, @sizeOf(u32)) catch return error.InvalidDebugInfo;
subsect.discardAll(file_bytes) catch
return error.InvalidDebugInfo;
}
try inlinee_source_lines.append(gpa, info);
}
}
}
std.mem.sortUnstable(
*align(1) const pdb.InlineeSourceLine,
inlinee_source_lines.items,
{},
inlineeSourceLineLessThan,
);
break :b try inlinee_source_lines.toOwnedSlice(gpa);
};
errdefer gpa.free(mod.inlinee_source_lines);
var sect_offset: usize = 0;
var skip_len: usize = undefined;
while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) {
const subsect_hdr: *align(1) pdb.DebugSubsectionHeader = @ptrCast(&mod.subsect_info[sect_offset]);
skip_len = subsect_hdr.length;
sect_offset += @sizeOf(pdb.DebugSubsectionHeader);
switch (subsect_hdr.kind) {
.file_checksums => {
mod.checksum_offset = sect_offset;
break;
},
else => {},
}
if (sect_offset > mod.subsect_info.len)
return error.InvalidDebugInfo;
}
mod.populated = true;
return mod;
}
pub fn getStreamById(self: *Pdb, id: u32) ?*MsfStream {
if (id >= self.msf.streams.len) return null;
return &self.msf.streams[id];
}
pub fn getStream(self: *Pdb, stream: pdb.StreamType) ?*MsfStream {
const id = @backingInt(stream);
return self.getStreamById(id);
}
/// https://llvm.org/docs/PDB/MsfFile.html
const Msf = struct {
directory: MsfStream,
streams: []MsfStream,
fn init(gpa: Allocator, file_reader: *File.Reader) !Msf {
const superblock = try file_reader.interface.takeStruct(pdb.SuperBlock, .little);
if (!std.mem.eql(u8, &superblock.file_magic, pdb.SuperBlock.expect_magic))
return error.InvalidDebugInfo;
if (superblock.free_block_map_block != 1 and superblock.free_block_map_block != 2)
return error.InvalidDebugInfo;
if (superblock.num_blocks * superblock.block_size != try file_reader.getSize())
return error.InvalidDebugInfo;
switch (superblock.block_size) {
// llvm 仅支持 4096,但此处可处理这些值中的任意一个
512, 1024, 2048, 4096 => {},
else => return error.InvalidDebugInfo,
}
const dir_block_count = blockCountFromSize(superblock.num_directory_bytes, superblock.block_size);
if (dir_block_count > superblock.block_size / @sizeOf(u32))
return error.UnhandledBigDirectoryStream; // 参阅 BlockMapAddr 注释。
try file_reader.seekTo(superblock.block_size * superblock.block_map_addr);
const dir_blocks = try gpa.alloc(u32, dir_block_count);
errdefer gpa.free(dir_blocks);
for (dir_blocks) |*b| {
b.* = try file_reader.interface.takeInt(u32, .little);
}
var directory_buffer: [64]u8 = undefined;
var directory = MsfStream.init(superblock.block_size, file_reader, dir_blocks, &directory_buffer);
const begin = directory.logicalPos();
const stream_count = try directory.interface.takeInt(u32, .little);
const stream_sizes = try gpa.alloc(u32, stream_count);
defer gpa.free(stream_sizes);
// Microsoft 的实现使用 @as(u32, -1) 表示不存在的流。
// 这些流虽不被使用,但仍参与文件结构,
// 解析流索引时必须将其计算在内。
const nil_size = 0xFFFFFFFF;
for (stream_sizes) |*s| {
const size = try directory.interface.takeInt(u32, .little);
s.* = if (size == nil_size) 0 else blockCountFromSize(size, superblock.block_size);
}
const streams = try gpa.alloc(MsfStream, stream_count);
errdefer gpa.free(streams);
for (streams, stream_sizes) |*stream, size| {
if (size == 0) {
stream.* = .empty;
continue;
}
const blocks = try gpa.alloc(u32, size);
errdefer gpa.free(blocks);
for (blocks) |*block| {
const block_id = try directory.interface.takeInt(u32, .little);
// 索引 0 预留给超级块。
// 理论上,每个位置为 `n * block_size + 1` 或 `n * block_size + 2` 的页
// 也会预留给其中一个 FPM。但观察到 LLVM 会将
// 这些页映射到实际流中,因此为兼容性允许此行为。
if (block_id == 0 or block_id >= superblock.num_blocks) return error.InvalidBlockIndex;
block.* = block_id;
}
const buffer = try gpa.alloc(u8, 64);
errdefer gpa.free(buffer);
stream.* = .init(superblock.block_size, file_reader, blocks, buffer);
}
const end = directory.logicalPos();
if (end - begin != superblock.num_directory_bytes)
return error.InvalidStreamDirectory;
return .{
.directory = directory,
.streams = streams,
};
}
fn deinit(self: *Msf, gpa: Allocator) void {
gpa.free(self.directory.blocks);
for (self.streams) |*stream| {
gpa.free(stream.interface.buffer);
gpa.free(stream.blocks);
}
gpa.free(self.streams);
}
};
const MsfStream = struct {
file_reader: *File.Reader,
next_read_pos: u64,
blocks: []u32,
block_size: u32,
interface: Io.Reader,
err: ?Error,
const Error = File.Reader.SeekError;
const empty: MsfStream = .{
.file_reader = undefined,
.next_read_pos = 0,
.blocks = &.{},
.block_size = undefined,
.interface = .ending_instance,
.err = null,
};
fn init(block_size: u32, file_reader: *File.Reader, blocks: []u32, buffer: []u8) MsfStream {
return .{
.file_reader = file_reader,
.next_read_pos = 0,
.blocks = blocks,
.block_size = block_size,
.interface = .{
.vtable = &.{ .stream = stream },
.buffer = buffer,
.seek = 0,
.end = 0,
},
.err = null,
};
}
fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize {
const ms: *MsfStream = @alignCast(@fieldParentPtr("interface", r));
var block_id: usize = @intCast(ms.next_read_pos / ms.block_size);
if (block_id >= ms.blocks.len) return error.EndOfStream;
var block = ms.blocks[block_id];
var offset = ms.next_read_pos % ms.block_size;
ms.file_reader.seekTo(block * ms.block_size + offset) catch |err| {
ms.err = err;
return error.ReadFailed;
};
var remaining = @backingInt(limit);
while (remaining != 0) {
const stream_len: usize = @min(remaining, ms.block_size - offset);
const n = try ms.file_reader.interface.stream(w, .limited(stream_len));
remaining -= n;
offset += n;
// 若位于块末尾,则移至下一个块。
if (offset == ms.block_size) {
offset = 0;
block_id += 1;
if (block_id >= ms.blocks.len) break; // 流结束
block = ms.blocks[block_id];
ms.file_reader.seekTo(block * ms.block_size) catch |err| {
ms.err = err;
return error.ReadFailed;
};
}
}
const total = @backingInt(limit) - remaining;
ms.next_read_pos += total;
return total;
}
pub fn logicalPos(ms: *const MsfStream) u64 {
return ms.next_read_pos - ms.interface.bufferedLen();
}
pub fn seekBy(ms: *MsfStream, len: i64) !void {
ms.next_read_pos = @as(u64, @intCast(@as(i64, @intCast(ms.logicalPos())) + len));
if (ms.next_read_pos >= ms.blocks.len * ms.block_size) return error.EOF;
ms.interface.tossBuffered();
}
pub fn seekTo(ms: *MsfStream, len: u64) !void {
ms.next_read_pos = len;
if (ms.next_read_pos >= ms.blocks.len * ms.block_size) return error.EOF;
ms.interface.tossBuffered();
}
fn getSize(ms: *const MsfStream) u64 {
return ms.blocks.len * ms.block_size;
}
fn getFilePos(ms: *const MsfStream) u64 {
const pos = ms.logicalPos();
const block_id = pos / ms.block_size;
const block = ms.blocks[block_id];
const offset = pos % ms.block_size;
return block * ms.block_size + offset;
}
};
fn readSparseBitVector(reader: *Io.Reader, gpa: Allocator) ![]u32 {
const num_words = try reader.takeInt(u32, .little);
var list: std.ArrayList(u32) = .empty;
defer list.deinit(gpa);
var word_i: u32 = 0;
while (word_i != num_words) : (word_i += 1) {
const word = try reader.takeInt(u32, .little);
var bit_i: u5 = 0;
while (true) : (bit_i += 1) {
if (word & (@as(u32, 1) << bit_i) != 0) {
try list.append(gpa, word_i * 32 + bit_i);
}
if (bit_i == std.math.maxInt(u5)) break;
}
}
return try list.toOwnedSlice(gpa);
}
fn blockCountFromSize(size: u32, block_size: u32) u32 {
return (size + block_size - 1) / block_size;
}