·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> mono的远程调试

mono的远程调试

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

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