mono的遠端偵錯

來源:互聯網
上載者:User

標籤:style   http   使用   os   檔案   資料   

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產生工具產生。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.