nginx获取函数执行调用关系

本文介绍了一个自定义的调试模块实现,该模块通过一系列宏定义和函数来控制调试信息的输出,并利用GCC属性来避免对性能的影响。此外,还提供了一个脚本来解析调试信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//代码片段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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值