Mmap debug info on linux

Closes #907.
This commit is contained in:
Marc Tiehuis 2019-05-13 20:04:25 +12:00
parent c1793d6106
commit c4d1597f50
3 changed files with 106 additions and 28 deletions

View File

@ -1004,21 +1004,33 @@ pub fn openElfDebugInfo(
fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo {
const S = struct {
var self_exe_file: os.File = undefined;
var self_exe_seekable_stream: os.File.SeekableStream = undefined;
var self_exe_in_stream: os.File.InStream = undefined;
var self_exe_mmap_seekable: io.SliceSeekableInStream = undefined;
};
S.self_exe_file = try os.openSelfExe();
errdefer S.self_exe_file.close();
S.self_exe_seekable_stream = S.self_exe_file.seekableStream();
S.self_exe_in_stream = S.self_exe_file.inStream();
const self_exe_mmap_len = try S.self_exe_file.getEndPos();
const self_exe_mmap = os.posix.mmap(
null,
self_exe_mmap_len,
os.posix.PROT_READ,
os.posix.MAP_SHARED,
S.self_exe_file.handle,
0,
);
if (self_exe_mmap == os.posix.MAP_FAILED) return error.OutOfMemory;
errdefer assert(os.posix.munmap(self_exe_mmap, self_exe_mmap_len) == 0);
const file_mmap_slice = @intToPtr([*]const u8, self_exe_mmap)[0..self_exe_mmap_len];
S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(file_mmap_slice);
return openElfDebugInfo(
allocator,
// TODO https://github.com/ziglang/zig/issues/764
@ptrCast(*DwarfSeekableStream, &S.self_exe_seekable_stream.stream),
@ptrCast(*DwarfSeekableStream, &S.self_exe_mmap_seekable.seekable_stream),
// TODO https://github.com/ziglang/zig/issues/764
@ptrCast(*DwarfInStream, &S.self_exe_in_stream.stream),
@ptrCast(*DwarfInStream, &S.self_exe_mmap_seekable.stream),
);
}
@ -1478,16 +1490,14 @@ fn parseFormValueTargetAddrSize(in_stream: var) !u64 {
}
fn parseFormValueRef(allocator: *mem.Allocator, in_stream: var, size: i32) !FormValue {
return FormValue{
.Ref = switch (size) {
1 => try in_stream.readIntLittle(u8),
2 => try in_stream.readIntLittle(u16),
4 => try in_stream.readIntLittle(u32),
8 => try in_stream.readIntLittle(u64),
-1 => try leb.readULEB128(u64, in_stream),
else => unreachable,
},
};
return FormValue{ .Ref = switch (size) {
1 => try in_stream.readIntLittle(u8),
2 => try in_stream.readIntLittle(u16),
4 => try in_stream.readIntLittle(u32),
8 => try in_stream.readIntLittle(u64),
-1 => try leb.readULEB128(u64, in_stream),
else => unreachable,
} };
}
fn parseFormValue(allocator: *mem.Allocator, in_stream: var, form_id: u64, is_64: bool) anyerror!FormValue {

View File

@ -36,6 +36,7 @@ pub fn getStdIn() GetStdIoErrs!File {
}
pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream;
pub const SliceSeekableInStream = @import("io/seekable_stream.zig").SliceSeekableInStream;
pub const COutStream = @import("io/c_out_stream.zig").COutStream;
pub fn InStream(comptime ReadError: type) type {
@ -1115,12 +1116,10 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
pub const Stream = InStream(Error);
pub fn init(in_stream: *Stream) Self {
return Self{
.in_stream = switch (packing) {
.Bit => BitInStream(endian, Stream.Error).init(in_stream),
.Byte => in_stream,
},
};
return Self{ .in_stream = switch (packing) {
.Bit => BitInStream(endian, Stream.Error).init(in_stream),
.Byte => in_stream,
} };
}
pub fn alignToByte(self: *Self) void {
@ -1326,12 +1325,10 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
pub const Stream = OutStream(Error);
pub fn init(out_stream: *Stream) Self {
return Self{
.out_stream = switch (packing) {
.Bit => BitOutStream(endian, Stream.Error).init(out_stream),
.Byte => out_stream,
},
};
return Self{ .out_stream = switch (packing) {
.Bit => BitOutStream(endian, Stream.Error).init(out_stream),
.Byte => out_stream,
} };
}
/// Flushes any unwritten bits to the stream

View File

@ -30,3 +30,74 @@ pub fn SeekableStream(comptime SeekErrorType: type, comptime GetSeekPosErrorType
}
};
}
pub const SliceSeekableInStream = struct {
const Self = @This();
pub const Error = error{};
pub const SeekError = error{EndOfStream};
pub const GetSeekPosError = error{};
pub const Stream = InStream(Error);
pub const SeekableInStream = SeekableStream(SeekError, GetSeekPosError);
pub stream: Stream,
pub seekable_stream: SeekableInStream,
pos: usize,
slice: []const u8,
pub fn init(slice: []const u8) Self {
return Self{
.slice = slice,
.pos = 0,
.stream = Stream{ .readFn = readFn },
.seekable_stream = SeekableInStream{
.seekToFn = seekToFn,
.seekForwardFn = seekForwardFn,
.getEndPosFn = getEndPosFn,
.getPosFn = getPosFn,
},
};
}
fn readFn(in_stream: *Stream, dest: []u8) Error!usize {
const self = @fieldParentPtr(Self, "stream", in_stream);
const size = std.math.min(dest.len, self.slice.len - self.pos);
const end = self.pos + size;
std.mem.copy(u8, dest[0..size], self.slice[self.pos..end]);
self.pos = end;
return size;
}
fn seekToFn(in_stream: *SeekableInStream, pos: u64) SeekError!void {
const self = @fieldParentPtr(Self, "seekable_stream", in_stream);
const usize_pos = @intCast(usize, pos);
if (usize_pos >= self.slice.len) return error.EndOfStream;
self.pos = usize_pos;
}
fn seekForwardFn(in_stream: *SeekableInStream, amt: i64) SeekError!void {
const self = @fieldParentPtr(Self, "seekable_stream", in_stream);
if (amt < 0) {
const abs_amt = @intCast(usize, -amt);
if (abs_amt > self.pos) return error.EndOfStream;
self.pos -= abs_amt;
} else {
const usize_amt = @intCast(usize, amt);
if (self.pos + usize_amt >= self.slice.len) return error.EndOfStream;
self.pos += usize_amt;
}
}
fn getEndPosFn(in_stream: *SeekableInStream) GetSeekPosError!u64 {
const self = @fieldParentPtr(Self, "seekable_stream", in_stream);
return @intCast(u64, self.slice.len);
}
fn getPosFn(in_stream: *SeekableInStream) GetSeekPosError!u64 {
const self = @fieldParentPtr(Self, "seekable_stream", in_stream);
return @intCast(u64, self.pos);
}
};