HP-UX 및 Linux에서 풀림을 쌓습니다
-
13-09-2019 - |
문제
특정 지점에서 C 응용 프로그램의 스택 정보를 가져와야합니다. 문서를 읽고 인터넷을 검색했지만 여전히 어떻게 할 수 있는지 알 수 없습니다. 간단한 프로세스 설명을 가리킬 수 있습니까? 또는 더 나은 것은 스택 풀기의 예에 더 적합합니다. HP-UX (Itanium) 및 Linux에 필요합니다.
해결책
체크 아웃 Linux/Stacktrace.h
API 참조는 다음과 같습니다.
http://www.cs.cmu.edu/afs/cs/web/people/tekkotsu/dox/stacktrace_8h.html
모든 Linux 커널에서 작동해야합니다
다음은 C의 대체 예입니다
http://www.linuxjournal.com/article/6391
#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));
}
다른 팁
당신은보고 싶어 Libunwind - 이것은 Itanium Stack Traces를 풀기 위해 HP가 원래 개발 한 크로스 플랫폼 라이브러리입니다 (특히 복잡한). 그러나 그 후 많은 다른 플랫폼으로 확장되었습니다. X86-Linux 및 Itanium-HPUX를 포함합니다.
Libunwind (3) Man Page에서; 다음은 Libunwind를 사용하여 일반적인 'Show Backtrace'기능을 작성하는 예입니다.
#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);
}
}
이 Shoulw는 HPUX Itanium에서 작동합니다.http://docs.hp.com/en/b9106-90012/unwind.5.html
간단한 스택 추적의 경우 u_stack_trace ()를 사용해보십시오.
제휴하지 않습니다 StackOverflow