I have finally found a good way to perform remote debugging of C# code running on my Raspberry Pi. I have switched from Mono to .NET Core and can now use Visual Studio to debug code running on the Raspberry Pi almost as easy as when running on my development machine.
The following steps were tested using Windows 10 version 1909, Visual Studio 2019 version 16.4.3, Raspbian Buster Lite version 2020-02-13 and a Raspberry Pi 2 Model B. .NET Core requires an ARMv7 CPU, so it will not run on Raspberry Pi 1 and Zero.
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.)
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
Add the following line to
~/.bashrc
, logout and login again to activate:export DOTNET_ROOT=/opt/dotnet
Check that .NET Core has been installed correctly:
dotnet --info
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.
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
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
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.
Enter password and click the Connect button.
Click the Select button.
Select Managed (.NET Core for Unix) and click the OK button.
Select the
dotnet MyProgram.dll
process and click the Attach button. The first connection might take several minutes, but subsequent connections are much faster.
Enjoy setting breakpoints, adding watches, stepping through code and even using "Set Next Statement" - almost as fast as when debugging on the local machine. The only thing I'm missing so far is Edit and Continue, but not enough to investigate if it is possible to achieve.