一、先来看微软官方解释
https://docs.microsoft.com/en-us/windows/win32/services/debugging-a-service
1) 服务程序运行后,使用调试器attach上进行调试
2) 代码中嵌入Debugbreak()函数,开发环境中运行后自动调用即时调试器
3) Image File Execution Options注册表大法,Vista后不支持,因为session的原因
4) 使用Event tracing机制即ETW记录日志
5) 某些时候,可以用控制台方式来运行服务(做参数切换)来方便调试
二、如果一定要用调试器调试以服务方式运行的服务入口,可以试试这个
1) 安装windbg,并找到目录
2) 先停止你的服务(比如sc stop xxxx)
3) 找到Image File Execution Options注册表键,创建debugger的值如下(注意windbg的安装目录及PDB路径要改成你自己的)
C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\windbg.exe -server tcp:port=9999 -c -pt 600 -y “srv*c:\symbols*http://msdl.microsoft.com/download/symbols;e:\your_pdb_path”
4) 启动服务(比如Sc start xxxx)
5) 打开windbg.exe,选择File->Connect to remote session…,在弹出的对话框中输入:
tcp:port=9999
然后点connect
6) Windbg就会断到你的service进程中, 然后你可以设置断点进行调试了