输出nginx执行过程中函数调用关系

添加源文件

首先在src/core/目录下添加两个文件,分别是my_debug.h和my_debug.c。

#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
#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)
{

}

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);
}

修改Makefile

makefile有如下几个地方需要修改(加粗部分):
1. CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -finstrument-functions
2. CORE_DEPS = src/core/nginx.h \
src/core/my_debug.h \
3. HTTP_DEPS = src/http/ngx_http.h \
src/core/my_debug.h \
4. objs/nginx: objs/src/core/nginx.o \
objs/src/core/my_debug.o \
5. $(LINK) -o objs/nginx \
objs/src/core/my_debug.o \

将my_debug.h包含到源码中

需要修改如下两个地方:
1. 在ngx_core.h中添加:

#include "my_debug.h"
  1. 在nginx.c中添加:
#define MY_DEBUG_MAIN 1

运行程序并用addr2line导出函数调用关系

运行程序:./objs/nginx,生成文件/usr/local/nginx/sbin/mydebug.log,路径是在my_debug.c中定义的。里面显示的只是函数的调用地址,可以写一个脚本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 >> $3
    fi;
done

执行脚本:

./addr2line.sh ./objs/nginx /usr/local/nginx/sbin/mydebug.log myfun.log

就可以在mydebug.log中看到从nginx启动开始调用的各函数之间的关系。
截取一部分如下:

main
nginx.c:279
--->
    ngx_regex_init
    ngx_regex.c:74

    ngx_regex_init
    ngx_regex.c:74
<---
main
nginx.c:279

main
nginx.c:313
--->
    ngx_os_init
    ngx_posix_init.c:34

ngx_os_init
ngx_posix_init.c:38
--->
    ngx_os_specific_init
    ngx_linux_init.c:35

    ngx_os_specific_init
    ngx_linux_init.c:35
<---
ngx_os_init
ngx_posix_init.c:38

遇到的问题

1.我是在windows编辑shell脚本,因此文件格式是dos,可以在linux中用vi打开,在vi中用命令行查看:

set ff

如果这样执行会出现错误:
/bin/sh^M: bad interpreter: No such file or directory
解决办法:
在vi中执行命令

set ff = unix
  1. 在执行make命令时出现错误,
    no target ‘\’
    解决办法:
    在Makefile文件中‘\’表示不换行,但是我在’\’后面多加了空格,只要把空格删除就好了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值