14 1月 2015

[WinDbg] Remote Kernel Debug over Network cable

WinDbg 不僅可以用來 Debug User mode 的 process,還可以用來 Debug Kernel mode 的 Windows,基本上使用的方式必須有兩台電腦,或是 VM ,因為 你如果要做 local 的 Kernel Debug 的話,中斷點 hit 到的話,整台電腦應該就會 block 住,但是 local Kernel Debug 無法這麼做的。

既然要透過 remote 的方式來做 Kernel Debug,所以 Windows 提供了不少方法讓你連線到另一台電腦,包括 Serial cable (COM)1394 cableNetwork cableUSB cable …等等,其中 Serial cable & 1394 使用上的限制比較少,support 的平台也比較多,Network cable 與 USB cable 對硬體與平台的限制都不少,所以要確認你的硬體平台有支援的話,才可以透過 Network cable, USB cable 來做 debug。

13 1月 2015

[C#] Allocate structure buffer array from DLL

C# 透過 DllImport 的方式讓你可以直接使用 C/C++ DLL 的 function,C/C++ 的程式常使用 struct 來儲存 data ,C# 也提供了讓你在 C# 中 define struct layout,這個轉換方式可以在 C# 中,直接宣告一個 struct 變數,丟入 DLL 中使用。

這點跟 Python 其實滿像的,Python 有 ctypes 可以做到類似的行為。script 中的 memory 跟 C/C++ 的 memory 無法互通,都必須透過轉換的方式讓 interpreter 可以順利看得懂 memory buffer。

C# 單一個 struct buffer 使用上都沒什麼問題,但是當你試著想要從 DLL 中撈回一串 struct array buffer時,就無法用直覺的方法完成這件事。因為 C# 在 DllImport 時的參數傳遞動了點手腳,所以如果直接將 C# 的 struct array 加 ref  傳至 DLL 可能會導致 crash 的產生。