mono的远程调试

简介: mono可以让.net程序运行在linux平台上。于是.net程序员有了mono之后就转身跨平台了。但开放环境往往还是在windows下,于是有了这样的需求,是否可以用windows下的源码来实机调试linux下的程序呢? 如今Xamarin已经被广泛地使用在移动平台的应用开发上,当然也能够支持实机调试。

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

目录
相关文章
|
Linux 网络安全 开发工具
【开发工具】【windows】Visual Studio Code(VS Code)远程Linux服务器环境搭建——SFTP篇
【开发工具】【windows】Visual Studio Code(VS Code)远程Linux服务器环境搭建——SFTP篇
923 0
【开发工具】【windows】Visual Studio Code(VS Code)远程Linux服务器环境搭建——SFTP篇
|
11月前
|
Oracle Java 关系型数据库
Windows 10系统下超详细的Java 开发环境配置详解
Windows 10系统下超详细的Java 开发环境配置详解
120 0
|
IDE 开发工具 Android开发
开始mono开发
使用mono框架开发android程序,第一步当然是构建开发环境,严格意义上说是使用 mono for android开发android程序。 参考Mono for Android安装配置破解  mono for android官网上有一个一键安装包,但是安装不是很方便。
1713 0
|
Go Windows 开发工具
Windows下visual studio code搭建golang开发环境
前言 其实环境搭建没什么难的,但是遇到一些问题,主要是有些网站资源访问不了(如:golang.org),导致一些包无法安装,最终会导致环境搭建失败,跟据这个教程几步,我们将可以快速的构建golang的开发环境。
1870 0
|
NoSQL Linux C++
使用Visual Studio 利用WinGDB编译和远程调试嵌入式Linux的程序
写这篇文章的目的在于帮助那些既要使用Visual Studio编写程序又要开发和调试嵌入式Linux 程序的苦命程序员们! 第一步, 安装 WinGDB ,下载位置  http://www.wingdb.
1937 0
|
C++ Windows Python
google protobuf学习笔记一:windows下环境配置
Windows下google protobuf开发环境配置 最近项目需求,Client与Server的网络通信协议传输使用google protobuf rpc。对于Protobuf,以前是只知道有这个东西,知道它干嘛,而且一直很仰慕,但是从来没学习过,最近刚好趁着项目机会,好好学习学习。
3131 0
|
图形学 自然语言处理