//代码片段2.4-1,文件名: 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
//代码片段2.4-2,文件名: my_debug.c
#include "my_debug.h"
#define MY_DEBUG_FILE_PATH "/usr/local/nginx/sbin/mydebug.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);
}
//代码片段2.4-3,文件名: Makefile
CFLAGS = -pipe -O0 -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused- function -Wunused-va riable -Wunused-value -Werror -g -finstrument-functions
//代码片段2.4-4,文件名: Makefile
…
CORE_DEPS = src/core/nginx.h \
src/core/my_debug.h \
…
HTTP_DEPS = src/http/ngx_http.h \
src/core/my_debug.h \
…
objs/nginx: objs/src/core/nginx.o \
objs/src/core/my_debug.o \
…
$(LINK) -o objs/nginx \
objs/src/core/my_debug.o \
…
objs/src/core/my_debug.o: $(CORE_DEPS) \
src/core/my_debug.c
$(CC) -c $(CFLAGS) $(CORE_INCS) \
-o objs/src/core/my_debug.o \
src/core/my_debug.c
…
//代码片段2.4-5,文件名: ngx_core.h
#include "my_debug.h"
//代码片段2.4-6,文件名: nginx.c
#define MY_DEBUG_MAIN 1
#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>
//代码片段2.4-7,文件名: nginx.c
main(int argc, char *const *argv)
{
…
enable_my_debug();
//代码片段2.4-8,文件名: nginx.conf
master_process off;
error_log logs/error.log emerg;
//代码片段2.4-9,文件名: addr2line.sh
#!/bin/sh
if [ $# != 3 ]; then
echo 'Usage: addr2line.sh executefile addressfile functionfile'
exit
fi;
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
#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
//代码片段2.4-2,文件名: my_debug.c
#include "my_debug.h"
#define MY_DEBUG_FILE_PATH "/usr/local/nginx/sbin/mydebug.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);
}
//代码片段2.4-3,文件名: Makefile
CFLAGS = -pipe -O0 -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused- function -Wunused-va riable -Wunused-value -Werror -g -finstrument-functions
//代码片段2.4-4,文件名: Makefile
…
CORE_DEPS = src/core/nginx.h \
src/core/my_debug.h \
…
HTTP_DEPS = src/http/ngx_http.h \
src/core/my_debug.h \
…
objs/nginx: objs/src/core/nginx.o \
objs/src/core/my_debug.o \
…
$(LINK) -o objs/nginx \
objs/src/core/my_debug.o \
…
objs/src/core/my_debug.o: $(CORE_DEPS) \
src/core/my_debug.c
$(CC) -c $(CFLAGS) $(CORE_INCS) \
-o objs/src/core/my_debug.o \
src/core/my_debug.c
…
//代码片段2.4-5,文件名: ngx_core.h
#include "my_debug.h"
//代码片段2.4-6,文件名: nginx.c
#define MY_DEBUG_MAIN 1
#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>
//代码片段2.4-7,文件名: nginx.c
main(int argc, char *const *argv)
{
…
enable_my_debug();
//代码片段2.4-8,文件名: nginx.conf
master_process off;
error_log logs/error.log emerg;
//代码片段2.4-9,文件名: addr2line.sh
#!/bin/sh
if [ $# != 3 ]; then
echo 'Usage: addr2line.sh executefile addressfile functionfile'
exit
fi;
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