標籤:script environ daemon 系統 bat man figure 注意 ogr
typora-copy-images-to: imagewindows下編譯調試nginx
linux使用gdb跟蹤代碼效率不高,在通過跟蹤代碼進行源碼分析,與定位複雜邏輯問題時,如果有一個簡單易用調試環境能極大提高效率。由於不想折騰linux的案頭系統,也擔心x-window連結效能問題,顧考慮在windows下編譯nginx並進行調試。
我使用的編譯器是MinGW的gcc套件,介面使用VS code。
2篇核心的參考連結如下:
《Building nginx on the Win32 platform with Visual C》
《C/C++ for VS Code》
安裝MinGW套件
下載安裝地址:http://www.mingw.org/
下載安裝後是一個MingGW Installation Manager,可以選擇mingw32-base和msys-base兩個包。
mingw32-base包括基本的gcc套件,msys-base有一些基本的linux命令工具,在後續執行configure時需要使用。
下載nginx源碼與依賴庫
下載源碼必須通過http://hg.nginx.org/nginx 進行下載,不可使用發布的源碼包,否則無法支援windows下編譯。可以選擇在網頁下載或通過hg下載。
可以按《Building nginx on the Win32 platform with Visual C》連結所示,現在依賴的第三方庫。
如果僅作代碼跟蹤調試使用,可以先利用調整編譯參數,去除對第三方庫的依賴。下文提供的編譯參數即不需要使用第三方庫
編譯nginx
解壓源碼後,啟動MinGE的msys.bat,進入到源碼目錄,執行以下命令產生Makefile。msys執行configure的速度較慢,需要耐心等待。
./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 --without-http_rewrite_module --without-http_gzip_module --with-select_module
我的指令碼與官方連結中的幾個區別:
- 使用gcc作為編譯器,而不是vs的cl
- 增加-O0參數,方便代碼跟蹤調試
- 沒添加http_ssl_module、http_rewrite_module、http_gzip_module省去第三方庫依賴
成功產生Makefile後通過執行make命令進行編譯,編譯的結果在objs\nginx.exe
調試nginx
參考《C/C++ for VS Code》進行配置,配置launch.json檔案。
{ "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": false, "MIMode": "gdb", "miDebuggerPath": "D:/MinGW/bin/gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ]}
修改了模板中的program、cwd、miDebuggerPath三個參數。
為了調試nginx方便,可以配置nginx.conf中的以下幾個參數。
daemon off;master_process off;worker_processes 1;
注意msys不支援動態斷點,需要在啟動調試前,或者調試狀態下設定斷點。
windows下編譯調試nginx