参考文档
windows10下使用VSCode成功调试nginx源码
如何在 Visual Studio 编译调试 Windows 版本的 Nginx 源码?
环境准备
- 下载nginx源码
- 下载VS code
- 下载MingGW
下载nginx源码
地址:hg.nginx.org/nginx
点击tag,选择对应版本的nginx源码
编译
- 现在解压后的nginx源码中创建一个objs文件夹(可以命名为其他文件名)
- 在objs文件夹下再创建lib文件夹,并把对应模块的包解压后放在该文件夹下
这里我只添加了pcre模块,用于配置正则相关的配置
- 编译nginx
打开安装好的MinGW,找到msys/1.0下的msys.bat,双击运行,如下所示:
运行后,将目录切换到到nginx源码目录,输入如下命令进行编译:
./auto/configure \
--with-cc=gcc \
--with-cc-opt=-O0 \
--with-debug \
--prefix= \
--conf-path=conf/nginx.conf \
--pid-path=logs/nginx.pid \
--http-log-path=logs/access.log \
--error-log-path=logs/error.log \
--sbin-path=nginx.exe \
--http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--http-scgi-temp-path=temp/scgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--with-pcre=objs/lib/pcre-8.45 \
--without-http_gzip_module \
--with-select_module
编译结束后,再执行:
nmake -f objs/Makefile
运行
上述操作执行完成后,在VS code中配置launch.json文件,我的配置如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [ {
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/objs/nginx.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/objs",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\devTool\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
最后点击如下图标就可以运行起来了
备注:本文写的略为粗糙,可结合文章开头提到的两篇文献进行操作,其中nginx编译文件略有不同,这个读者可以根据自己的需要自行修改。