From baa1656de322718b68c3e66a5fcfe342ce1d9a8b Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 16 Feb 2015 15:31:04 +0300 Subject: [PATCH] win32 debugging support, continue --- dlangide.visualdproj | 1 + src/ddebug/windows/debuginfo.d | 97 ++++++++++++++++++++++++++++++++++ src/dlangide.d | 5 ++ 3 files changed, 103 insertions(+) create mode 100644 src/ddebug/windows/debuginfo.d diff --git a/dlangide.visualdproj b/dlangide.visualdproj index b4e63e5..facf860 100644 --- a/dlangide.visualdproj +++ b/dlangide.visualdproj @@ -200,6 +200,7 @@ + diff --git a/src/ddebug/windows/debuginfo.d b/src/ddebug/windows/debuginfo.d new file mode 100644 index 0000000..ccf6113 --- /dev/null +++ b/src/ddebug/windows/debuginfo.d @@ -0,0 +1,97 @@ +module ddebug.windows.debuginfo; + +import dlangui.core.logger; +import std.file; +import std.algorithm; +import std.conv; +import std.exception; + +class FileFormatException : Exception { + this(string msg, Exception baseException = null, string file = __FILE__, size_t line = __LINE__) { + super(msg, baseException, file, line); + } + this(Exception baseException = null, string file = __FILE__, size_t line = __LINE__) { + super("", baseException, file, line); + } + //this(string file = __FILE__, size_t line = __LINE__) { + // super("Exception while parsing file format", file, line); + //} +} + + +struct Buffer { + ubyte[] buf; + void skip(uint bytes) { + enforce(bytes <= buf.length, new FileFormatException("skip: index is outside file range")); + buf = buf[bytes .. $]; + } + uint uintAt(uint pos) { + enforce(pos + 4 <= buf.length, new FileFormatException("uintAt: index is outside file range")); + return cast(uint)buf[pos] | (cast(uint)buf[pos + 1] << 8) | (cast(uint)buf[pos + 2] << 16) | (cast(uint)buf[pos + 3] << 24); + } + ushort ushortAt(uint pos) { + enforce(pos + 2 <= buf.length, new FileFormatException("ushortAt: index is outside file range")); + return cast(ushort)buf[pos] | (cast(ushort)buf[pos + 1] << 8); + } + ubyte ubyteAt(uint pos) { + enforce(pos + 1 <= buf.length, new FileFormatException("ubyteAt: index is outside file range")); + return buf[pos]; + } + //void check(uint pos, string data) { + // enforce(pos + data.length <= buf.length, new FileFormatException("check: index is outside file range")); + // enforce(equal(buf[pos..pos + data.length], cast(ubyte[])data), new FileFormatException("pattern does not match")); + //} + void check(uint pos, ubyte[] data) { + enforce(pos + data.length <= buf.length, new FileFormatException("check: index is outside file range")); + enforce(equal(buf[pos..pos + data.length], data), new FileFormatException("pattern does not match")); + } + + ubyte[] sectionAt(uint pos) { + uint rva = uintAt(pos); + uint sz = uintAt(pos + 4); + enforce(pos + sz <= buf.length, new FileFormatException("sectionAt: index is outside file range")); + return buf[rva .. rva + sz]; + } +} + +struct Section { + +} + +class OMFDebugInfo { + Buffer data; + uint peoffset; + bool load(string filename) { + try { + data.buf = cast(ubyte[])std.file.read(filename); + data.check(0, ['M', 'Z']); + peoffset = data.uintAt(0x3c); + data.skip(peoffset); + data.check(0, ['P', 'E', 0, 0]); + ushort objectCount = data.ushortAt(0x06); + ushort flags = data.ushortAt(0x16); + ushort subsystem = data.ushortAt(0x5c); + return true; + } catch (Exception e) { + throw new FileFormatException(e); + } + } +} + +debug(DebugInfo) { + void debugInfoTest(string filename) { + OMFDebugInfo omf = new OMFDebugInfo(); + Log.d("Loading debug info from file ", filename); + try { + if (omf.load(filename)) { + Log.d("Loaded ok"); + } else { + Log.d("Failed"); + } + } catch (FileFormatException e) { + Log.e("FileFormatException: ", e); + } + + } + +} diff --git a/src/dlangide.d b/src/dlangide.d index 545ca38..465e438 100644 --- a/src/dlangide.d +++ b/src/dlangide.d @@ -42,6 +42,11 @@ extern (C) int UIAppMain(string[] args) { //import ddc.lexer.tokenizer; //runTokenizerTest(); + debug(DebugInfo) { + import ddebug.windows.debuginfo; + import std.file; + debugInfoTest(thisExePath); + } // create window Window window = Platform.instance.createWindow("Dlang IDE", null, WindowFlag.Resizable, 900, 700);