標籤:linux核心 linux c語言 debug callback
一、引言backtrace()系列函數可用來輸出代碼出錯時的函數調用關係。
A backtrace is the series of currently active function calls for the program.
#include <execinfo.h>int backtrace(void **buffer, int size);char **backtrace_symbols(void *const *buffer, int size);void backtrace_symbols_fd(void *const *buffer, int size, int fd);
二、樣本:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <termios.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <strings.h>#include <string.h>#include <errno.h>#include <signal.h>#include <sys/ioctl.h>#include <execinfo.h> #define SIZE(100)static sigset_t signals_handled;static void dbg_backtrace(void){int i, nline;void *buffer[SIZE];char **strings;nline = backtrace(buffer, SIZE);printf("addr: %d = backtrace()\n", nline);strings = backtrace_symbols(buffer, nline);if (NULL == strings) {perror("backtrace_symbols()\n");exit(EXIT_FAILURE);}for (i = 0; i < nline; i ++) {printf("callback: %s\n", strings[j]);}/* backtrace_symbols() 內部有調用malloc函數 */free(strings);}static void sig_handler_hup(int sig){printf("%s() %d\n\n", __func__, sig);}static void sig_handler_debug(int sig){printf("%s() %d\n\n", __func__, sig);}static void sig_handler_usr2(int sig){printf("%s() %d\n\n", __func__, sig);}static void sig_handler_fatal(int sig){printf("Fatal signal %d\n\n", sig);dbg_backtrace();exit(127);}void signals_setup(void){struct sigaction sa;sigemptyset(&signals_handled);sigaddset(&signals_handled, SIGHUP);sigaddset(&signals_handled, SIGINT);sigaddset(&signals_handled, SIGTERM);sigaddset(&signals_handled, SIGUSR2);#define SIGNAL(s, handler)do { sa.sa_handler = handler;if (sigaction(s, &sa, NULL) < 0) printf("could not set signal handler (%d): %m", s); } while(0);sa.sa_mask = signals_handled;sa.sa_flags = 0;SIGNAL(SIGHUP, sig_handler_hup);SIGNAL(SIGUSR1, sig_handler_debug);SIGNAL(SIGUSR2, sig_handler_usr2);SIGNAL(SIGABRT, sig_handler_fatal);SIGNAL(SIGALRM, sig_handler_fatal);SIGNAL(SIGFPE, sig_handler_fatal);SIGNAL(SIGILL, sig_handler_fatal);SIGNAL(SIGPIPE, sig_handler_fatal);SIGNAL(SIGQUIT, sig_handler_fatal);SIGNAL(SIGSEGV, sig_handler_fatal);signal(SIGPIPE, SIG_IGN);}void mem_err(void){char *prt = NULL;printf("%s()\n", __func__);*prt = '1';}int main(int argc, char **argv){signals_setup();int i = 0;for (; ;) {i ++;if (i > 10) {mem_err();}// printf("i: %d\n", i);sleep(2);}return 0;}
運行結果:
mem_err()Fatal signal 11
addr: 7 = backtrace()callback: ./sig_setup() [0x804867b]callback: ./sig_setup() [0x8048791]callback: [0xb77af400]callback: ./sig_setup() [0x8048ae7]callback: ./sig_setup() [0x8048b13]callback: /lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0xb764ce66]callback: ./sig_setup() [0x8048591]
三、backtrace()系列函數含義:
int backtrace(void **buffer, int size);
backtrace()返回程式調用的backtrace資訊,結果(即地址:address from the corresponding stack frame)存放在buffer指標指向的數組中。size參數指定了buffer指向的數組可儲存的結果的最大個數;若結果個數大於size,數組將儲存那些最近執行的函數調用。
返回值代表buffer指向的數組實際元素的個數。
char **backtrace_symbols(void *const *buffer, int size);
backtrace()返回的數組中存放的結果是地址值,backtrace_symbols()則把這些地址轉化成對應的字串。
每一個元素(字串)包含地址值對應的函數名(若不能擷取,則不存在)、內部語句相對函數的位移地址(hexadecimal)、語句的實際地址(hexadecimal)。如:./prog(myfunc3+0x5c) [0x80487f0]backtrace_symbols()函數內部進行了malloc操作,所以調用者必須free記憶體。
操作執行失敗返回NULL。
void backtrace_symbols_fd(void *const *buffer, int size, int fd);
功能和backtrace_symbols()函數一樣,只是它把返回結果按行寫到了fd代表的檔案裡。backtrace_symbols_fd函數內部沒有進行malloc操作!
四、結果分析:先執行反組譯碼:objdump -d test > dump.txt
callback: ./sig_setup() [0x804867b]:0804865c <dbg_backtrace>: 804865c:55 push %ebp 804865d:89 e5 mov %esp,%ebp 804865f:81 ec b8 01 00 00 sub $0x1b8,%esp 8048665:c7 44 24 04 64 00 00 movl $0x64,0x4(%esp) 804866c:00 804866d:8d 85 5c fe ff ff lea -0x1a4(%ebp),%eax 8048673:89 04 24 mov %eax,(%esp) 8048676:e8 e5 fe ff ff call 8048560 <[email protected]> 804867b:89 45 f0 mov %eax,-0x10(%ebp)callback: ./sig_setup() [0x8048791]08048773 <sig_handler_fatal>: 8048773:55 push %ebp 8048774:89 e5 mov %esp,%ebp 8048776:83 ec 18 sub $0x18,%esp 8048779:8b 45 08 mov 0x8(%ebp),%eax 804877c:89 44 24 04 mov %eax,0x4(%esp) 8048780:c7 04 24 15 8c 04 08 movl $0x8048c15,(%esp) 8048787:e8 14 fd ff ff call 80484a0 <[email protected]> 804878c:e8 cb fe ff ff call 804865c <dbg_backtrace> 8048791:c7 04 24 7f 00 00 00 movl $0x7f,(%esp)callback: ./sig_setup() [0x8048ae7]08048ac3 <mem_err>: 8048ac3:55 push %ebp 8048ac4:89 e5 mov %esp,%ebp 8048ac6:83 ec 28 sub $0x28,%esp 8048ac9:c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp) 8048ad0:c7 44 24 04 5b 8c 04 movl $0x8048c5b,0x4(%esp) 8048ad7:08 8048ad8:c7 04 24 4e 8c 04 08 movl $0x8048c4e,(%esp) 8048adf:e8 bc f9 ff ff call 80484a0 <[email protected]> 8048ae4:8b 45 f4 mov -0xc(%ebp),%eax 8048ae7:c6 00 31 movb $0x31,(%eax)
至此可知,這裡的函數調用,造成了非法的記憶體操作。
五、backtrace()系列函數注意事項:這三個函數都假設函數的返回地址按它認為的方式儲存在棧上,故使用時應注意:
1、幀指標(Frame pointers)的屏蔽/忽略可能導致上述假設失效2、inline關鍵字描述的函數沒有棧幀(stack frames)3、尾調用(Tail-call)最佳化會造成一個棧幀被另一個替換掉4、對於特定編譯器,應該指定連結選項,否則函數名欄位可能是無效的;對於使用GNU linker的系統,使用-rdynamic連結項5、static關鍵字描述的函數不會被顯示,and won‘t be available in the backtrace
PS:棧幀(stack frames):In computer science, a stack frame is a memory management strategy used to create and destroy temporary (automatic) variables in some programming languages. Stack frames only exist at run-time.在電腦科學裡,棧幀是一種記憶體管理原則,在某些程式設計語言裡它用來建立/銷毀臨時(自動)變數。棧幀只在運行時存在。尾調用(Tail-call):In computer science, a tail call is a subroutine call performed as the final action of a procedure.在電腦科學裡,尾調用是指一個函數裡的最後一個動作是一個函數調用的情形:即這個調用的返回值直接被當前函數返回的情形。
參考資料:幀指標(Frame pointers):http://stackoverflow.com/questions/579262/what-is-the-purpose-of-the-frame-pointerhttp://blog.chinaunix.net/uid-25871104-id-2938389.html棧幀(stack frames):http://en.citizendium.org/wiki/Stack_frame尾調用(Tail-call):http://en.wikipedia.org/wiki/Tail_call
Linux中backtrace()系列函數的應用執行個體