1.在www.nginx.org下载nginx源代码后 tar -zxvf xxx.tar.gz 然后进入解压后的目录,./configure --prefix=/usr/local/nginx 编译成功后(如果出现什么错误请百度自己解决)会在目录下出现objs文件夹,然后cd objs,ll一下会出现如下所示
(.configure的作用参考:http://www.xuebuyuan.com/2230733.html make文件语法参考http://www.ruanyifeng.com/blog/2015/02/make.html)
前提是已经装好了带有hello_word模块的nginx,执行情况如下
nginx配置:
location /test
{
hello_world;
}
root@ubuntu:/var/local/nginx-1.9.12# curl http://xuji.test.com/test
hello world
开始调试之旅:参考http://www.cnblogs.com/yjf512/archive/2012/05/10/2494635.html http://www.cnblogs.com/paulweihan/p/4667216.html
1.重新编译nginx --with-debug
编译之前先修改auto/cc/conf
ngx_compile_opt="-c"
变为
ngx_compile_opt="-c -g"
./configure --prefix=/usr/local/nginx --with-pcre=/var/local/pcre-8.37 --with-zlib=/var/local/zlib-1.2.8 --with-http_ssl_module --add-module=/var/local/nginx-1.9.12/nginx-hello-world-module --with-debug
make
将make后的nginx程序复制到/usr/local/nginx/sbin目录中
修改nginx.conf
worker_processes 1;
#master_process off; # 单进程模式
#daemon off;
#error_log logs/error.log debug;
2.cd /usr/local/nginx/sbin
执行.nginx(这样就开启了nginx程序)
可以在另一个标签中执行curl http://xuji.test.com/test 可以看到hello world
ps -ef | grep nginx找到master进程id
root 15795 1 0 14:20 ? 00:00:00 nginx: master process ./nginx
nobody 15796 15795 0 14:20 ? 00:00:00 nginx: worker process
gcore 15795
kill -s 9 15795 15796
gdb ./nginx core.15795
进入gdb命令行i li main(i li +函数名 查看该函数所在的文件及位置)一下找到main函数所在的位置
(gdb) i li main
Line 178 of "src/core/nginx.c" starts at address 0x804a90d <main> and ends at 0x804a91c <main+15>.
然后就是在src/core/nginx.c中看代码了
(gdb) i var ngx_show_version //显示变量的定义
All variables matching regular expression "ngx_show_version":
File src/core/nginx.c:
static ngx_uint_t ngx_show_version;
在i li ngx_debug_init时 出现Function "ngx_debug_init" not defined.这时改用grep -r ngx_debug_init可发现
src/os/unix/ngx_posix_config.h:#define ngx_debug_init()
src/os/unix/ngx_freebsd_init.c:ngx_debug_init(void)
src/os/unix/ngx_linux_config.h:#define ngx_debug_init()//在linux系统中 这个函数什么都没做,所以显示not defined
src/os/unix/ngx_darwin.h:void ngx_debug_init(void);
src/os/unix/ngx_darwin_init.c:ngx_debug_init(void)
src/os/unix/ngx_solaris_config.h:#define ngx_debug_init()
src/os/unix/ngx_freebsd.h:void ngx_debug_init(void);
src/core/nginx.c: ngx_debug_init();
在nginx源代码中看到ngx_xxx()的函数时说明是nginx自己定义的函数 ,如果没有ngx_的时候,说明是系统函数可用man命令查看该函数的定义.
gdb调试命令(参考文档:http://blog.chinaunix.net/uid-122937-id-208210.html)
r //开始执行程序
b 215//在第215行加断点
clear 215//删除215行断点
delete 4//删除第4个断点
info b//查看所有断点
l //查看代码 l -10往前走10行 l +10 往后走10行 如果想查看某个函数可l 函数名 如:l ngx_crc32_table_init
p *log// p*log->file 查看值
n //执行下一句
s // 执行下一句 如果有函数调用则进入函数