Linux上使用Visual Studio进行.NET远程调试

33

我想在Visual Studio中远程调试运行在Linux上的C#控制台应用程序。以下是目前我找到的信息:

http://www.mono-project.com/Debugger

Mono运行时实现了调试接口,使得调试器和IDE可以调试托管代码。这被称为Soft Debugger,同时被MonoDevelop、Xamarin Studio、Visual Studio(需要安装适当的插件)和命令行SDB客户端支持。

Mono提供了一个API来与调试器通信,可以通过Mono.Debugger.Soft.dll程序集创建自己的调试UI。

下面的页面讨论了当前MonoVS调试器实现的一些问题,但对我来说都没问题。

http://mono-project.com/Visual_Studio_Integration

该页面还链接到MonoVS入门指南:

http://mono-project.com/GettingStartedWithMonoVS

其中包含一个MonoTools的下载链接:

http://mono-tools.com/download/

然而,该下载链接现在转向了:

http://xamarin.com/download/

在该页面,我可以下载Xamarin Studio Starter Edition。点击Pricing链接,我看到需要至少Business版本才能获得Visual Studio支持,价格为每年$999。很抱歉,我不需要。

这就是我所卡住的地方。我的环境具体如下:

开发环境:

  • Windows 7 64-bit
  • Visual Studio Pro 2013(如果它更好,也可以使用2010)

目标环境:

  • Raspberry Pi
  • Raspbian Wheezy
  • -> 树莓派 Wheezy操作系统
  • Mono 3.2.8
  • -> Mono 3.2.8框架
  • Running console application over SSH
  • -> 通过SSH运行控制台应用程序

    MonoVS是Novell销售的商业产品,当时大部分Mono团队成员都被Novell聘用(在Novell收购Ximian之后)。自从Xamarin推出以来,该产品不再可用。为了清晰起见,他们应该从Mono主页中删除这些页面。无论如何,答案中显示的远程调试器是一种相当有趣的方法。 - Lex Li
    6个回答

    17

    谢谢,我还没有找到解决方案,所以这看起来非常有趣。 - Anlo
    @Anlo 希望能帮到你!如果你遇到麻烦,请随时标记为答案或发表新的问题。 - Gutemberg Ribeiro
    1
    嗯,运行得很好有点夸张。 是的,你可以调试,但只能设置断点并逐步执行。只能监视本地基本类型如int。你无法在调用堆栈中导航,也无法重置当前指令位置。 因此,基本上只能逐步执行代码 - 但这是一个开始 :) - Papa Mufflon
    当然有帮助。在VS 2015 Update 2上,要小心使用MonoRemoteDebugger v1.0.4来解决与Xamarin VS扩展的冲突。 - JPBlanc
    有没有解决方案可以在Linux下使用Mono调试Visual Studio .NET应用程序?http://stackoverflow.com/questions/23721964/how-to-debug-visual-studio-net-application-under-mono-in-linux - Roy Doron
    MonoRemoteDebugger现在已经更新到v1.2.0版本,但是我在Raspbian Jessie(2017-04-10)上遇到了问题。sudo apt-get install mono-complete安装的是mono 3.2.8版本,但是pdb2mdb工具无法读取由VS2015编译的程序集:PdbDebugException: Unknown custom metadata item kind: 6。我可能需要在Pi上构建一个更高版本的mono,但我将首先重新审视Xamarin解决方案。 - Anlo

    4
    我找到了这个指南,讲解如何使用Xamarin Studio在Windows上对Linux进行远程调试。现在Xamarin Studio是免费的,除了iOS和Android开发。我在树莓派Zero W上运行Raspbian Jessie Lite (2017-04-10)进行测试时遇到了问题,因此我对其进行了修复。
    1. 下载并安装Xamarin Studio, GTK# for .NET.NET Framework 4.6.2。 Xamarin Studio需要.NET 4.5,但GTK#需要.NET 4.6。我使用的是Xamarin Studio版本6.1.2 (build 44)和GTK#版本2.12.44。
    2. 我在一个不同于我的VS2015机器的虚拟机上安装了Xamarin Studio,因此还需要下载并安装MSBuild Tools 2013MSBuild Tools 2015
    3. 创建一个环境变量MONODEVELOP_SDB_TEST = 1 (我的电脑->属性->高级系统设置->环境变量)。
    4. 启动Xamarin Studio。如果没有显示窗口,请检查文件夹%localappdata%\XamarinStudio-6.0\Logs中的日志文件以查看发生了什么错误。
    5. 我在Visual Studio 2015中创建了一个.NET 4.6控制台应用程序,并添加了NuGet软件包Mono.Unofficial.pdb2mdb,我使用的版本是4.2.3.4.软件包Mono.pdb2mdb版本0.1.0.20130128似乎无法处理由VS2015构建的程序集(PdbDebugException: Unknown custom metadata item kind: 6)。
    6. 从Visual Studio加载.sln或.csproj文件到Xamarin Studio中。生成解决方案,并使用NuGet软件包文件夹中的pdb2mdb工具创建.mdb文件:pdb2mdb MyProgram.exe
    7. 我使用WinSCP将MyProgram.exe和MyProgram.exe.mdb复制到我的Raspberry Pi上。
    8. 使用SSH登录Putty,并在树莓派上安装Mono版本3.2.8:sudo apt-get install mono-complete
    9. 使用调试器标志启动Mono运行时:mono --debug --debugger-agent=transport=dt_socket,address=0.0.0.0:12345,server=y /path/to/MyProgram.exe。这将启动程序,但会暂停执行直到Xamarin Studio调试器已连接。
    10. 在Xamarin Studio中设置断点,并选择菜单项Run -> Run with -> Custom Configuration...,选择Run Action = Debug - Custom Command Mono Soft Debugger。点击Debug
    11. IPPort字段中填写您的Linux系统的IP地址和Mono命令行中指定的端口12345。点击Connect,执行将开始,在设置的断点处停止。

    Screenshot of Xamarin Studio Debugging

    可以设置条件断点,跳入/跳出/跳过代码,监视原始类型和对象等,而且速度相当快。我更喜欢直接从Visual Studio调试,但这似乎是一个完全可行的解决方案。

    2
    我终于找到了一种很好的方法来远程调试在我的Raspberry Pi上运行的C#代码。我已经从Mono切换到.NET Core,现在可以使用Visual Studio对在Raspberry Pi上运行的代码进行调试,几乎与在开发机器上运行一样容易。
    以下步骤是使用Windows 10版本1909、Visual Studio 2019版本16.4.3、Raspbian Buster Lite版本2020-02-13和Raspberry Pi 2 Model B测试的。.NET Core需要ARMv7 CPU,因此它无法在Raspberry Pi 1和Zero上运行。
    1. Go to https://dotnet.microsoft.com/download/dotnet-core and select .NET Core 3.1 (or later). Click the link for Linux ARM32 runtime binaries and copy the direct link displayed on the next page. (Do not right-click the ARM32 link and select copy link address, as you will end up with a webpage if you download that link.)

      .NET Core download website

    2. Open a SSH session to the Raspberry Pi, download and install the binaries:

      ssh pi@192.168.0.xxx
      wget https://download.visualstudio.microsoft.com/download/pr/c11e9248-404f-4e5b-bd99-175079419d6f/83902a43e06f9fb4e45a4c6a6d5afc0b/dotnet-runtime-3.1.3-linux-arm.tar.gz
      sudo mkdir /opt/dotnet
      sudo tar zxf dotnet-runtime-3.1.3-linux-arm.tar.gz -C /opt/dotnet
      sudo ln -s /opt/dotnet/dotnet /usr/bin/dotnet
      
    3. Add the following line to ~/.bashrc, logout and login again to activate:

      export DOTNET_ROOT=/opt/dotnet
      
    4. Check that .NET Core has been installed correctly:

      dotnet --info
      

      Output from dotnet --info

    5. Create a *.NET Core Console App```lang-none in Visual Studio. Set Target framework = .NET Core 3.1 (or the version you downloaded to the Raspberry Pi). Make sure that Project → Properties → Build → Output → Advanced → Debugging information = Portable.

      Advanced build settings

    6. Build the project in Visual Studio and copy all *.dll, *.pdb, *.deps.json and *.runtimeconfig.json files from the development machine to the Pi. PuTTY's pscp command can be used:

      cd bin\Debug\netcoreapp3.1
      pscp -pw <password> *.dll *.pdb *.deps.json *.runtimeconfig.json pi@192.168.0.xxx:/home/pi
      
    7. Open a SSH session to the Raspberry Pi and run the program, or start it from the development machine using SSH:

      ssh pi@192.168.0.xxx dotnet /home/pi/MyProgram.dll
      
    8. Attach to the remote process by selecting the Debug → Attach to Process menu item in Visual Studio. Select Connection type = SSH and in the Connection target textbox, type pi@192.168.0.xxx and press Enter.

      Connection target

    9. Enter password and click the Connect button.

      Connect window

    10. Click the Select button.

      Attach to process image

    11. Select Managed (.NET Core for Unix) and click the OK button.

      Select code type

    12. Select the dotnet MyProgram.dll process and click the Attach button. The first connection might take several minutes, but subsequent connections are much faster.

    享受设置断点、添加观察点、逐步执行代码,甚至使用“设置下一语句”——几乎和在本地机器上调试一样快。到目前为止,我唯一缺少的是编辑并继续,但不足以调查是否可能实现。


    0
    你需要的解决方案将在今年与MonoDebugger.NET一起推出。开发者“承诺”我们可以部署到任何Mono设备,并在Visual Studio(2010至2015)中进行调试。

    1
    那么,我猜就这样了? - Mark

    0

    Gutemberg Ribeiro的回答的基础上,我成功地让MonoRemoteDebugger在运行Raspbian Jessie Lite(2017-04-10)的树莓派Zero W上与VS2015配合使用。诀窍是安装一个版本大于3.2.8的Mono:

    1. 安装MonoRemoteDebugger Visual Studio扩展,我使用的是版本1.2.0。
    2. 在Visual Studio中创建一个.NET 4.6控制台应用程序。
    3. 如果Raspberry Pi上安装了旧版的Mono,请使用以下命令将其卸载:

      sudo apt-get purge mono-complete

      sudo apt-get autoremove

    4. 安装Mono版本4.0.2:

      sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

      echo "deb http://plugwash.raspbian.org/mono4 jessie-mono4 main" | sudo tee -a /etc/apt/sources.list

      sudo apt-get update

      sudo apt-get install mono-complete

    5. 安装MonoRemoteDebugger服务器:

      wget https://github.com/techl/MonoRemoteDebugger/releases/download/v1.2.0/MonoRemoteDebugger.Server.zip

      unzip -d MonoRemoteDebugger.Server MonoRemoteDebugger.Server.zip

    6. 启动MonoRemoteDebugger服务器:

      mono MonoRemoteDebugger.Server/MonoRemoteDebugger.Server.exe

    7. 在Visual Studio中设置断点,并选择菜单项MonoRemoteDebugger -> Debug with Mono (remote)
    8. 将远程IP设置为Raspberry Pi的IP地址,然后单击连接。MonoRemoteDebugger将编译并传输程序到Raspberry Pi。无需手动运行pdb2mdb,.mdb文件将由MonoRemoteDebugger服务器创建。

    Screenshot

    说实话,调试能力相当有限。简单的断点和代码步入/步出/步过似乎还可以。在函数中设置断点,然后跳过该函数调用将不会停在断点处。

    原始类型可以被监视,但我尝试监视的对象无法显示。调用堆栈视图非常有限,线程视图为空。异常没有被捕获,但会导致MonoRemoteDebuggerServer出现"[ERROR] FATAL UNHANDLED EXCEPTION"消息。但如果您可以接受这些限制,那么这个设置比Xamarin Studio route更简单。


    0

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接