目标:打印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查看具体内容