crash/minidump: locationReader to read locations in a minidump

This commit is contained in:
Mitchell Hashimoto
2024-09-08 21:25:34 -07:00
parent 5b1d729748
commit 9178d1cf38
2 changed files with 22 additions and 1 deletions

View File

@ -54,6 +54,9 @@ pub fn Reader(comptime S: type) type {
const SourceReader = @typeInfo(@TypeOf(SourceCallable.reader)).Fn.return_type.?;
const SourceSeeker = @typeInfo(@TypeOf(SourceCallable.seekableStream)).Fn.return_type.?;
/// A limited reader for reading data from the source.
pub const LimitedReader = std.io.LimitedReader(SourceReader);
/// The source type for the reader.
pub const Source = S;
@ -72,7 +75,6 @@ pub fn Reader(comptime S: type) type {
/// reader() is called.
limit_reader: LimitedReader = undefined,
const LimitedReader = std.io.LimitedReader(SourceReader);
pub const Reader = LimitedReader.Reader;
/// Returns a Reader implementation that reads the bytes of the
@ -164,6 +166,19 @@ pub fn Reader(comptime S: type) type {
self.endian,
);
}
/// Return a reader for the given location descriptor. This is only
/// valid until the reader source is modified in some way.
pub fn locationReader(
self: *const Self,
loc: external.LocationDescriptor,
) !LimitedReader {
try self.source.seekableStream().seekTo(loc.rva);
return .{
.inner_reader = self.source.reader(),
.bytes_left = loc.data_size,
};
}
};
}

View File

@ -89,6 +89,7 @@ pub fn ThreadListReader(comptime R: type) type {
test "minidump: threadlist" {
const testing = std.testing;
const alloc = testing.allocator;
var fbs = std.io.fixedBufferStream(@embedFile("../testdata/macos.dmp"));
const R = Reader(*@TypeOf(fbs));
@ -107,5 +108,10 @@ test "minidump: threadlist" {
for (0..v.count) |i| {
const t = try v.thread(i);
log.warn("thread i={} thread={}", .{ i, t });
// Read our stack memory
var stack_reader = try r.locationReader(t.stack.memory);
const bytes = try stack_reader.reader().readAllAlloc(alloc, t.stack.memory.data_size);
defer alloc.free(bytes);
}
}