2009-06-10 3 views
5

특정 시점에 내 C 응용 프로그램의 스택 정보를 가져와야합니다. 설명서를 읽고 인터넷을 검색했지만 어떻게 할 수 있는지 아직 알 수 없습니다. 간단한 프로세스 설명을 가르쳐 주시겠습니까? 또는 스택 언 와인딩 예제에 더 좋을 수도 있습니다. HP-UX (Itanium) 및 Linux에 필요합니다. 여기 HP-UX 및 Linux에서 스택 되감기

는 C에서 다른 예제입니다

http://www.cs.cmu.edu/afs/cs/Web/People/tekkotsu/dox/StackTrace_8h.html

모든 리눅스 커널에서 작동합니다 :

답변

4

체크 아웃 리눅스는/stacktrace.h 여기

는 API 참조입니다

http://www.linuxjournal.com/article/6391

당신은 libunwind보고 싶지
#include <stdio.h> 
#include <signal.h> 
#include <execinfo.h> 

void show_stackframe() { 
    void *trace[16]; 
    char **messages = (char **)NULL; 
    int i, trace_size = 0; 

    trace_size = backtrace(trace, 16); 
    messages = backtrace_symbols(trace, trace_size); 
    printf("[bt] Execution path:\n"); 
    for (i=0; i<trace_size; ++i) 
    printf("[bt] %s\n", messages[i]); 
} 


int func_low(int p1, int p2) { 

    p1 = p1 - p2; 
    show_stackframe(); 

    return 2*p1; 
} 

int func_high(int p1, int p2) { 

    p1 = p1 + p2; 
    show_stackframe(); 

    return 2*p1; 
} 


int test(int p1) { 
    int res; 

    if (p1<10) 
    res = 5+func_low(p1, 2*p1); 
    else 
    res = 5+func_high(p1, 2*p1); 
    return res; 
} 



int main() { 

    printf("First call: %d\n\n", test(27)); 
    printf("Second call: %d\n", test(4)); 

} 
+0

API가 존재한다는 것을 알지 못했습니다. 얼마나 유용한가! – Jamie

+0

HP-UX에서 도움이되지 않습니다.) – DaveR

+0

@dave, nit-picker : P –

3

-이 아이테니엄 스택 추적을 풀기 HP에 의해 처음 개발 된 크로스 플랫폼 라이브러리입니다 (이 특히 복잡하다); 많은 다른 플랫폼으로 확장되었습니다. x86-Linux와 Itanium-HPUX를 모두 포함합니다.

libunwind (3) 매뉴얼 페이지에서; HPUX 아이테니엄에 대한

#define UNW_LOCAL_ONLY 
#include <libunwind.h> 

void show_backtrace (void) { 
    unw_cursor_t cursor; unw_context_t uc; 
    unw_word_t ip, sp; 

    unw_getcontext(&uc); 
    unw_init_local(&cursor, &uc); 
    while (unw_step(&cursor) > 0) { 
    unw_get_reg(&cursor, UNW_REG_IP, &ip); 
    unw_get_reg(&cursor, UNW_REG_SP, &sp); 
    printf ("ip = %lx, sp = %lx\n", (long) ip, (long) sp); 
    } 
} 
0

이 shoulw 작업 : 여기에 전형적인 '쇼 역 추적'기능을 작성하는 libunwind는 사용의 예입니다 http://docs.hp.com/en/B9106-90012/unwind.5.html

간단한 스택 추적의 경우는,() U_STACK_TRACE을 시도해보십시오.