• nginx系列第二篇:nginx源码调试


    第一篇将nginx源码从下载到运行进行了说明,这一节继续讲解如何调试nginx源代码。本人使用vscode进行调试,选择vscode是因为其比较轻巧,python/C++/C/js等开发都可以,适用性强。

    众所周知,nginx是多进程应用程序,master默认启动是守护进程方式启动。所以调试master进程和work进程的方式不同。下面就进入正题。

    1.修改编译参数,保证生成可以调试的程序

    gcc编译debug程序的两个常用参数:

    -g: 创建调试符号表,符号表包含了程序中使用的变量名称的列表。关闭所有的优化机制,以便程序执行过程中严格按照原来的C代码进行。

    -O0: 编译不做任何优化,这是默认的编译选项。 

    1. ./configure --prefix=/home/baidu123/Downloads/nginx-1.22.1/nginx/bin --with-http_ssl_module --with-stream --with-cc-opt='-O0 -g'
    2. sudo make CFLAGS="-g -oO"
    3. sudo make install

     编译成功见如下目录结构:

     2.调试master进程

    (1) 在 conf/nginx.conf 配置文件中添加配置项:daemon off; 如下图所示:

     (2) 启动文件launch.json配置如下:

    1. "version": "0.2.0",
    2. "configurations": [
    3. {
    4. "name": "debug_master",
    5. "type": "cppdbg",
    6. "request": "launch",
    7. "program": "${workspaceFolder}/nginx/bin/sbin/nginx",
    8. "args": [
    9. "-c", "${workspaceFolder}/nginx/bin/conf/nginx.conf"
    10. ],
    11. "stopAtEntry": false,
    12. "cwd": "${workspaceFolder}",
    13. "environment": [],
    14. "externalConsole": false,
    15. "MIMode": "gdb",
    16. "setupCommands": [
    17. {
    18. "description": "Enable pretty-printing for gdb",
    19. "text": "-enable-pretty-printing",
    20. "ignoreFailures": true
    21. }
    22. ]
    23. }
    24. ]
    25. }

     效果如下:

     3.调试work进程

    启动文件launch.json配置如下:

    1. {
    2. // Use IntelliSense to learn about possible attributes.
    3. // Hover to view descriptions of existing attributes.
    4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    5. "version": "0.2.0",
    6. "configurations": [
    7. {
    8. "name": "debug_worker",
    9. "type": "cppdbg",
    10. "request": "attach",
    11. "program": "${workspaceFolder}/nginx/bin/sbin/nginx",
    12. "processId": "28759",
    13. "MIMode": "gdb",
    14. "setupCommands": [
    15. {
    16. "description": "Enable pretty-printing for gdb",
    17. "text": "-enable-pretty-printing",
    18. "ignoreFailures": true
    19. }
    20. ]
    21. }
    22. ]
    23. }

     注意:此处是以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.刷新网页调用堆栈如下

    1. //先停顿388行
    2. ngx_http_wait_request_handler(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:388)
    3. 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)
    4. ngx_process_events_and_timers(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event.c:248)
    5. 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)
    6. 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)
    7. 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)
    8. ngx_master_process_cycle(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:130)
    9. main(int argc, char * const * argv) (/home/baidu123/Downloads/nginx-1.22.1/src/core/nginx.c:383)
    10. //后停顿135行
    11. 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)
    12. ngx_http_process_request_line(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:1085)
    13. ngx_http_wait_request_handler(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:503)
    14. 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)
    15. ngx_process_events_and_timers(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event.c:248)
    16. 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)
    17. 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)
    18. 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)
    19. ngx_master_process_cycle(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:130)
    20. main(int argc, char * const * argv) (/home/baidu123/Downloads/nginx-1.22.1/src/core/nginx.c:383)

    2.nginx内部自动执行,只停顿到388行

    1. ngx_http_wait_request_handler(ngx_event_t * rev) (/home/baidu123/Downloads/nginx-1.22.1/src/http/ngx_http_request.c:388)
    2. ngx_event_expire_timers() (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event_timer.c:94)
    3. ngx_process_events_and_timers(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/event/ngx_event.c:261)
    4. 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)
    5. 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)
    6. 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)
    7. ngx_master_process_cycle(ngx_cycle_t * cycle) (/home/baidu123/Downloads/nginx-1.22.1/src/os/unix/ngx_process_cycle.c:130)
    8. main(int argc, char * const * argv) (/home/baidu123/Downloads/nginx-1.22.1/src/core/nginx.c:383)
  • 相关阅读:
    scala语法(二)(有java基础速学)
    Android Studio Chipmunk 发布啦,快来看看有什么更新
    docker+nginx 安装部署修改资源目录配置文件和容器端口信息
    好用免费的PPT模板
    Jest单测实践篇
    Springboot+网上眼镜商场 毕业设计-附源码241659
    云原生周刊:Gateway API v1.1 发布 | 2024.6.3
    Debezium系列之:解析默认值字符集
    Hadoop总结
    白帽SEO是什么?白帽SEO手法有哪些?
  • 原文地址:https://blog.csdn.net/hsy12342611/article/details/127875841