第一篇将nginx源码从下载到运行进行了说明,这一节继续讲解如何调试nginx源代码。本人使用vscode进行调试,选择vscode是因为其比较轻巧,python/C++/C/js等开发都可以,适用性强。
众所周知,nginx是多进程应用程序,master默认启动是守护进程方式启动。所以调试master进程和work进程的方式不同。下面就进入正题。
1.修改编译参数,保证生成可以调试的程序
gcc编译debug程序的两个常用参数:
-g: 创建调试符号表,符号表包含了程序中使用的变量名称的列表。关闭所有的优化机制,以便程序执行过程中严格按照原来的C代码进行。
-O0: 编译不做任何优化,这是默认的编译选项。
- ./configure --prefix=/home/baidu123/Downloads/nginx-1.22.1/nginx/bin --with-http_ssl_module --with-stream --with-cc-opt='-O0 -g'
- sudo make CFLAGS="-g -oO"
- sudo make install
编译成功见如下目录结构:
2.调试master进程
(1) 在 conf/nginx.conf 配置文件中添加配置项:daemon off; 如下图所示:
(2) 启动文件launch.json配置如下:
- "version": "0.2.0",
- "configurations": [
- {
- "name": "debug_master",
- "type": "cppdbg",
- "request": "launch",
- "program": "${workspaceFolder}/nginx/bin/sbin/nginx",
- "args": [
- "-c", "${workspaceFolder}/nginx/bin/conf/nginx.conf"
- ],
- "stopAtEntry": false,
- "cwd": "${workspaceFolder}",
- "environment": [],
- "externalConsole": false,
- "MIMode": "gdb",
- "setupCommands": [
- {
- "description": "Enable pretty-printing for gdb",
- "text": "-enable-pretty-printing",
- "ignoreFailures": true
- }
- ]
- }
- ]
- }
效果如下:
3.调试work进程
启动文件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": "debug_worker",
- "type": "cppdbg",
- "request": "attach",
- "program": "${workspaceFolder}/nginx/bin/sbin/nginx",
- "processId": "28759",
- "MIMode": "gdb",
- "setupCommands": [
- {
- "description": "Enable pretty-printing for gdb",
- "text": "-enable-pretty-printing",
- "ignoreFailures": true
- }
- ]
- }
- ]
- }
注意:此处是以gdb attach的方式来调试运行中的进程,需要先使用ps查看nginx woker进程的pid,然后再attach上去,如上图显示work进程id为28759,launch.json文件中的processId也要是28759。
work进程调试效果如下:
在右侧可以看到函数调用栈帧信息
疑问:
我在如下两个函数设置断点:
static void
ngx_http_wait_request_handler(ngx_event_t *rev);
ngx_int_t
ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b);
1.刷新网页调用堆栈如下
- //先停顿388行
- ngx_http_wait_request_handler(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:388)
- ngx_epoll_process_events(ngx_cycle_t * cycle, ngx_msec_t timer, ngx_uint_t flags) (/home/baidu123/Downloads/nginx-1.22.1/src/event/modules/ngx_epoll_module.c:901)
- ngx_process_events_and_timers(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event.c:248)
- ngx_worker_process_cycle(ngx_cycle_t * cycle, void * data) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:721)
- ngx_spawn_process(ngx_cycle_t * cycle, ngx_spawn_proc_pt proc, void * data, char * name, ngx_int_t respawn) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process.c:199)
- ngx_start_worker_processes(ngx_cycle_t * cycle, ngx_int_t n, ngx_int_t type) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:344)
- ngx_master_process_cycle(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:130)
- main(int argc, char * const * argv) (/home/baidu123/Downloads/nginx-1.22.1/src/core/nginx.c:383)
-
- //后停顿135行
- ngx_http_parse_request_line(ngx_http_request_t * r, ngx_buf_t * b) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_parse.c:135)
- ngx_http_process_request_line(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:1085)
- ngx_http_wait_request_handler(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:503)
- ngx_epoll_process_events(ngx_cycle_t * cycle, ngx_msec_t timer, ngx_uint_t flags) (/home/baidu123/Downloads/nginx-1.22.1/src/event/modules/ngx_epoll_module.c:901)
- ngx_process_events_and_timers(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event.c:248)
- ngx_worker_process_cycle(ngx_cycle_t * cycle, void * data) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:721)
- ngx_spawn_process(ngx_cycle_t * cycle, ngx_spawn_proc_pt proc, void * data, char * name, ngx_int_t respawn) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process.c:199)
- ngx_start_worker_processes(ngx_cycle_t * cycle, ngx_int_t n, ngx_int_t type) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:344)
- ngx_master_process_cycle(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:130)
- main(int argc, char * const * argv) (/home/baidu123/Downloads/nginx-1.22.1/src/core/nginx.c:383)
2.nginx内部自动执行,只停顿到388行
- ngx_http_wait_request_handler(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:388)
- ngx_event_expire_timers() (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event_timer.c:94)
- ngx_process_events_and_timers(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event.c:261)
- ngx_worker_process_cycle(ngx_cycle_t * cycle, void * data) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:721)
- ngx_spawn_process(ngx_cycle_t * cycle, ngx_spawn_proc_pt proc, void * data, char * name, ngx_int_t respawn) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process.c:199)
- ngx_start_worker_processes(ngx_cycle_t * cycle, ngx_int_t n, ngx_int_t type) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:344)
- ngx_master_process_cycle(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:130)
- main(int argc, char * const * argv) (/home/baidu123/Downloads/nginx-1.22.1/src/core/nginx.c:383)