Linux安装vsdbg实现远程调试程序

2,564 阅读3分钟

转自:www.codeprj.com/blog/da61c2…
转自:www.plcnext-community.net/makersblog/…

环境

服务器:centos7、.net core、SSH

本机:windows、vs2019及以上、.net core

vs2019设置,vs2022同理

第1步:附加到进程,调试>附加到进程->选择SSH连接类型,连接目标中输入IP地址,SSH用户名、密码进行连接。

image.png 找到dotnet进程,并选择你的应用程序

 

第2步:确定要附加的进程,dotnetcore程序,筛选dotnet找到运行的进程,附加,选择托管(.NET Core for Unix)

image.png

如果是第一次使用的话,他是基于目标服务器上的vs-debugger的安装程序来运行的,所以,在第一次附加的时候,会等待好长时间的正在启动调试适配器,因为目标服务器在下载安装vsdbg-linux-x64.tar.gz。整个过程应该都是自动去,不需要人为干预,因为下载源在国外,所以下载特别慢,所以,VS这面可以强制关闭了,做其他的,等目标服务器上达到预期的结果了,再重试附加。 还有一种办法是手动下载vsdbg-linux-x64.tar.gz,从vsdebugger.azureedge.net/vsdbg-17-0-… 这个网站能下载,我这个是vs2022的地址,不同的地址,中间的路由不同,详情可以去参考GetVsDbg.sh脚本中的代码。

预期过程: 在过程中,我发现,在目标服务器的/root/路径下,多了一个.vs-debugger文件夹,
如果你是通过vs2019附加的话,会存在目录如下:
/root/.vs-debugger/GetVsDbg.sh
/root/.vs-debugger/vs2019/vsdbg-linux-x64.tar.gz
如果你是通过vs2022附加的话,会存在目录如下:
/root/.vs-debugger/GetVsDbg.sh
/root/.vs-debugger/vs2022/vsdbg-linux-x64.tar.gz

当然这个tar是临时存在的,而且是不断变大的,因为下载慢,他还在下载中。下载完毕后,就会自动变成一堆解压缩的文件,此时,vs-debugger的安装就结束了,可以远程调试了。
还有一种方法,就是,如果你提前已经下载好了tar包了,你可以按照上面的目录结构,将tar包、GetVsDbg.sh、vs2019或者vs2022目录结构配置好,同时修改GetVsDbg.sh脚本文件,将__SkipDownloads=false设置为true,然后执行命令:

//2019的执行命令
/bin/sh /root/.vs-debugger/GetVsDbg.sh -v vs2019 -l /root/.vs-debugger/vs2019 -a /remote_debugger

//2022的执行命令
/bin/sh /root/.vs-debugger/GetVsDbg.sh -v vs2022 -l /root/.vs-debugger/vs2022 -a /remote_debugger

我也不确定这个是不是必须要执行的,还是说可以省略的,这个有待验证,如果自动不行了,就手动执行一下,准没错

命令的详情参数可参考如下:

    echo 'GetVsDbg.sh [-usho] -v V [-l L] [-r R] [-d M] [-e E]'
    echo ''
    echo 'This script downloads and configures vsdbg, the Cross Platform .NET Debugger'
    echo '-u    Deletes the existing installation directory of the debugger before installing the current version.'
    echo '-s    Skips any steps which requires downloading from the internet.'
    echo '-d M  Launches debugger after the script completion. Where M is the mode, "mi" or "vscode"'
    echo '-h    Prints usage information.'
    echo '-v V  Version V can be "latest" or a version number such as 15.0.25930.0'
    echo '-l L  Location L where the debugger should be installed. Can be absolute or relative'
    echo '-r R  Debugger for the RuntimeID will be installed'
    echo '-a A  Specify a different alternate location that the debugger might already be installed.'
    echo '-o    Enables "Offline Mode" This enables structured output to use if the current machine does not have access to internet.'
    echo '-e E  E is the full path to the compressed package obtained from outside of the script.'
    echo ''
    echo 'For more information about using this script with Visual Studio Code see:'
    echo 'https://github.com/OmniSharp/omnisharp-vscode/wiki/Attaching-to-remote-processes'
    echo ''
    echo 'For more information about using this script with Visual Studio see:'
    echo 'https://github.com/Microsoft/MIEngine/wiki/Offroad-Debugging-of-.NET-Core-on-Linux---OSX-from-Visual-Studio'
    echo ''
    echo 'To report issues, see:'
    echo 'https://github.com/omnisharp/omnisharp-vscode/issues'

结果如图: image.png

image.png

image.png

最后你发现,原来你的tar包也被清除了 查看GetVsDbg.sh中脚本代码,发现465行有一句 rm "$__VsdbgCompressedFile",有心人可以试试把这行去掉,看看是否能保留下来原始tar包,毕竟下载不易

linux服务器需要安装SSH服务器

查看是否安装了ssh:rpm -qa|grep -E "openssh"

安装缺失的软件:sudo yum install openssh*

注册使用服务:

sudo systemctl enable sshd 

sudo systemctl start sshd 或者

service sshd start

 

开启防火墙的22端口:

sudo firewall-cmd --zone=public --add-port=22/tcp --permanent