輸出nginx執行過程中函數調用關係

來源:互聯網
上載者:User
添加源檔案

首先在src/core/目錄下添加兩個檔案,分別是my_debug.h和my_debug.c。

#ifndef MY_DEBUG_LENKY_H#define MY_DEBUG_LENKY_H#include 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_MAINextern FILE *my_debug_fd;#elseFILE *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/shif [ $# != 3 ]; thenecho'Usage: addr2line.sh executefile addressfile functionfile'exitfi;cat $2 | whileread linedoif [ "$line" = 'Enter' ]; thenread line1        read line2#       echo $line >> $3        addr2line -e$1-f$line1-s >> $3echo"--->" >> $3        addr2line -e$1-f$line2-s | sed 's/^/  /' >> $3echo >> $3elif [ "$line" = 'Exit' ]; thenread line1        read line2        addr2line -e$1-f$line2-s | sed 's/^/  /' >> $3echo"<---" >> $3        addr2line -e$1-f$line1-s >> $3echo >> $3fi;done

執行指令碼:

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

就可以在mydebug.log中看到從nginx啟動開始調用的各函數之間的關係。
截取一部分如下:

mainnginx.c:279--->    ngx_regex_initngx_regex.c:74ngx_regex_initngx_regex.c:74<---mainnginx.c:279mainnginx.c:313--->    ngx_os_initngx_posix_init.c:34ngx_os_initngx_posix_init.c:38--->    ngx_os_specific_initngx_linux_init.c:35ngx_os_specific_initngx_linux_init.c:35<---ngx_os_initngx_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檔案中‘\’表示不換行,但是我在’\’後面多加了空格,只要把空格刪除就好了。

以上就介紹了 輸出nginx執行過程中函數調用關係,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.