mono可以讓.net程序運行在linux平臺上。于是.net程序員有了mono之后就轉身跨平臺了。但開放環境往往還是在windows下,于是有了這樣的需求,是否可以用windows下的源碼來實機調試linux下的程序呢?
如今Xamarin已經被廣泛地使用在移動平臺的應用開發上,當然也能夠支持實機調試。
大概是內部維護一個TCP連接,傳遞調用堆棧信息。
查閱了一些文檔和stackoverflow搜索結果之后看到下面這樣的描述:
Remote debugging is actually really easy with the Mono soft debugger. The IDE sends commands over TCP/ip to the Mono Soft Debugger agent inside the runtime. Depending how you launch the debuggee, you can either have it connect to the IDE over TCP, or have it open a port and wait for the IDE to connect to it.
For simple PRototyping purposes, you can just set the MONODEVELOP_SDB_TEST env var, and a new "Run->Run With->Custom Soft Debugger" command will show up in Xamarin Studio / MonoDevelop, and you can specify an arbitrary IP and port or connect or or listen on, and optionally a command to run. Then you just have to start the debuggee with the correct --debugger-agentarguments (see the Mono manpage for details), start the connection, and start debugging.
For a production workflow, you'd typically create a MonoDevelop addin with a debugger engine and session subclassing the soft debugger classes, and overriding how to launch the app and set up the connection parameters. You'd typically have a custom project type too subclassing the DotNetProject, so you could override how the project was built and executed, and so that the new debugger engine could be the primary debugger for projects of that type. You'd get all the default .NET/Mono project and debugger functionality "for free".
恩,就和手機調試一樣,linux上運行mono,遠程使用XamarinStudio調試也非常方便。
我的測試代碼如下
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
while (true) {
string input =Console.ReadLine ();
Console.WriteLine (input);//此處可做斷點
if (input == "q") {
break;
}
}
Console.WriteLine ("byebye");
Console.ReadKey ();
}
首先,在mono運行的時候帶上參數
例如:mono --debug --debugger-agent=transport=dt_socket,address=127.0.0.1:8088,server=y,suspend=y MyApp.exe
因為我設置了server參數為y,表示這里是socket的監聽方,然后suspend=y。之后MyApp.exe并沒開始運行,而是等待連接。
然后,在另一端,比如windows下,安裝monodevelop(XamarinStudio), 配置環境變量
MONODEVELOP_SDB_TEST=1
啟動monodevelop,打開項目,設置默認F5(Run)為"Run->Run With->Custom Soft Debugger"
會打開一個對話框,在對話框中輸入需要連接的mono運行機器的ip和端口,因為是linux端監聽,所以選擇connect(當然如果mono運行時的參數server是n,那就是相反啦)。
連接成功,linux界面輸出hello world,然后隨意輸入字符串,回車,windows下獲得斷點,檢查變量值調用堆棧ok數據獲取成功。
遠程調試測試成功。
怎么樣,是不是覺得很像使用Unity的感覺呢?
是的,看這個:相關的情報
另外,如果想要mono運行MyApp.exe不等待連接,設置suspend為n即可,程序將先運行而不阻塞等待連接。
但是,我仍然不知道連接之后如何斷開調試而不結束進程,希望知道的朋友能夠給予幫助。
注意,還有一個--debug的參數非常重要,與之配套的是exe(dll)程序集配套生成的.mdb文件。mono的調試信息中包括源代碼的路徑、對應的代碼在幾行等等信息都在里面,遠程調試的時候都需要這些信息。
不然即使連接建立,mono也不知道如何將數據和本地代碼聯系起來。所以源碼也不要輕易地移動目錄哦。
.mdb文件可以用monodevelop生成,也可以到mono安裝目錄下的bin文件夾中找到mdb生成工具生成。
新聞熱點
疑難解答