nginx执行流程

目标:打印nginx执行之后的流程方法

my_debug.c

cat my_debug.c
#include "my_debug.h"
#define MY_DEBUG_FILE_PATH1 "/usr/local/nginx_sendfile/sbin/trace.txt"
#define MY_DEBUG_FILE_PATH "/data/haoning/mygit/mynginxmodule/nginx_release/debug/my_debug.log"
int _flag=0;
#define open_my_debug_file() \
(my_debug_fd=fopen(MY_DEBUG_FILE_PATH,"a"))
#define close_my_debug_file() \
do { \
if (NULL != my_debug_fd) { \
fclose(my_debug_fd); \
} \
}while(0)

#define my_debug_print(args,fmt...) \
do{ \
if (0 == _flag) { \
break; \
} \
if (NULL == my_debug_fd && NULL == open_my_debug_file()) { \
printf("Err: can not open output file.\n"); \
break; \
} \
fprintf(my_debug_fd,args,##fmt); \
fflush(my_debug_fd); \
}while(0)

void enable_my_debug( void )
{
_flag = 1;
}
void disable_my_debug( void )
{
_flag = 0;
}
int get_my_debug_flag( void )
{
return _flag;
}
void set_my_debug_flag( int flag )
{
_flag = flag;
}
void main_constructor( void )
{
//do nothing
}
void main_destructor( void )
{
close_my_debug_file();
}
void __cyg_profile_func_enter( void *this,void *call )
{
my_debug_print("enter\n%p\n%p\n",call,this);
}
void __cyg_profile_func_exit( void *this,void *call )
{
my_debug_print("exit\n%p\n%p\n",call,this);
}

my_debug.h

#ifndef MY_DEBUG_LENKY_H
#define MY_DEBUG_LENKY_H
#include <stdio.h>

void enable_my_debug( void ) __attribute__((no_instrument_function));
void disable_my_debug( void ) __attribute__((no_instrument_function));
int get_my_debug_flag( void ) __attribute__((no_instrument_function));
void set_my_debug_flag( int ) __attribute__((no_instrument_function));
void main_constructor( void ) __attribute__((no_instrument_function,constructor));
void main_destructor( void ) __attribute__((no_instrument_function,destructor));
void __cyg_profile_func_enter( void *,void * ) __attribute__((no_instrument_function));
void __cyg_profile_func_exit( void *,void * ) __attribute__((no_instrument_function));

#ifndef MY_DEBUG_MAIN
extern FILE *my_debug_fd;
#else
FILE *my_debug_fd;
#endif
#endif

cp my_debug.c my_debug.h ../nginx-1.5.6/src/core/

vim ../nginx-1.5.6/src/core/nginx.c

11 #include <nginx.h>
12 #include "my_debug.h"
.....
204 main(int argc, char *const *argv)
205 {
206 enable_my_debug();

configure之后修改objs/Makefile

CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -finstrument-functions
...
18 CORE_DEPS = src/core/nginx.h \
19 src/core/my_debug.h \
.....
84 HTTP_DEPS = src/http/ngx_http.h \
85 src/core/my_debug.h \
....

105 objs/nginx: objs/src/core/nginx.o \
106 objs/src/core/my_debug.o \
......
216 $(LINK) -o objs/nginx \
217 objs/src/core/nginx.o \
218 objs/src/core/my_debug.o \
......
331 objs/src/core/my_debug.o: $(CORE_DEPS) src/core/my_debug.c
332 $(CC) -c $(CFLAGS) $(CORE_INCS) \
333 -o objs/src/core/my_debug.o \
334 src/core/my_debug.c



make &&make install 之后
启动
./nginx
得到
/data/haoning/mygit/mynginxmodule/nginx_release/debug/my_debug.log
编写处理脚本
addr2line.sh

#!/bin/sh
if [ $# != 3 ]; then
echo 'Usage: addr2line.sh executefile addressfile functionfile'
exit;
fi;
echo "begin"
cat $2 |while read line
do
if [ "$line" = 'enter' ]; then
read line1
read line2
# echo $line >> $3
addr2line -e $1 -f $line1 -s >>$3
echo "--->" >> $3
addr2line -e $1 -f $line2 -s | sed 's/^/ /' >> $3
echo >> $3
elif [ "$line" = 'exit' ]; then
read line1
read line2
addr2line -e $1 -f $line2 -s | sed 's/^/ /' >> $3
echo "<---" >> $3
addr2line -e $1 -f $line1 -s >> $3
# echo $line >> $3
echo >> $3
fi;
done
echo "end"


./addr2line.sh /usr/local/nginx_sendfile/sbin/nginx my_debug.log a.log
生成a.log为所要的结果
类似如下

main
nginx.c:216
--->
ngx_strerror_init
ngx_errno.c:47

ngx_strerror_init
ngx_errno.c:47
<---
main
nginx.c:216

??
??:0
--->
main
nginx.c:205

main
nginx.c:205
<---
??
??:0

main
nginx.c:280
--->
ngx_time_init
ngx_times.c:61

ngx_time_init
ngx_times.c:69
--->
ngx_time_update
ngx_times.c:75

ngx_time_update
ngx_times.c:116
--->
ngx_gmtime
ngx_times.c:284

ngx_gmtime
ngx_times.c:284
<---
ngx_time_update
ngx_times.c:116

附件中a.log.jpg 重命名为a.log查看具体内容
### 安装 Nginx 的详细步骤 #### 准备工作 在 CentOS 上安装 Nginx 前,需确保系统已更新至最新版本并具备必要的开发工具包。可以通过以下命令完成环境准备: ```bash sudo yum update -y sudo yum groupinstall "Development Tools" -y ``` #### 步骤一:安装依赖项 为了成功编译和运行 Nginx,需要先安装一些必需的软件包。以下是具体的安装命令[^1]: ```bash sudo yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel ``` #### 步骤二:下载 Nginx 源码 通过 `wget` 下载最新的稳定版 Nginx 源码文件[^2]: ```bash wget http://nginx.org/download/nginx-1.26.2.tar.gz ``` #### 步骤三:解压源码包 使用 `tar` 工具解压缩刚刚下载的 Nginx 源码包: ```bash tar -zxvf nginx-1.26.2.tar.gz cd nginx-1.26.2 ``` #### 步骤四:配置编译选项 进入解压后的目录后,运行 `./configure` 脚本来设置编译参数[^4]。可以根据需求调整模块支持,默认情况下可以使用如下命令: ```bash ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-stream \ --with-mail \ --with-file-aio ``` #### 步骤五:编译与安装 执行 `make` 和 `make install` 来完成 Nginx 的编译和安装过程: ```bash make sudo make install ``` #### 步骤六:验证安装 安装完成后,检查 Nginx 是否正常启动以及其所在路径是否正确: ```bash /usr/local/nginx/sbin/nginx curl http://localhost ``` 如果返回 HTML 页面,则表示 Nginx 成功启动。 #### 配置优化 编辑 Nginx 主配置文件 `/usr/local/nginx/conf/nginx.conf` 进行进一步定制化设置[^3]。例如修改监听端口、增加虚拟主机等。 #### 开放防火墙端口 对于生产环境中使用的服务器,可能还需要开放 HTTP (80) 或 HTTPS (443) 端口以便外部访问: ```bash sudo firewall-cmd --zone=public --add-port=80/tcp --permanent sudo firewall-cmd --reload ``` --- ### 注意事项 - 如果使用的是云服务提供商(如阿里云),还需手动添加对应的安全组规则允许入站流量到达指定端口号。 - 默认情况下,Nginx 将静态页面存放在 `/usr/local/nginx/html/` 文件夹内;可根据实际项目结构调整此位置。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值