堆栈展开在HP-UX和Linux
-
13-09-2019 - |
题
我需要在某些点上我的C应用程序的堆栈信息。我读过的文档和搜索的网络,但仍然想不通我怎么能做到这一点。你可以指向一个简单的过程解释?或者,甚至更好,栈展开的一个例子。我需要它HP-UX(安腾)和Linux。
解决方案
检查出的 LINUX / stacktrace.h 强>
下面是一种API参考:
HTTP://www.cs .cmu.edu / AFS / CS /网络/人/ 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 - 这是惠普最初开发一个跨平台的库开卷安腾堆栈跟踪(这是特别复杂的);但随后被扩展到许多其他的平台;既包括x86的Linux和Itanium的HPUX。
从libunwind(3)手册页;这里是使用libunwind写一个典型的“放映回溯”功能的例子:
#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);
}
}
此用于HPUX安腾shoulw工作: http://docs.hp.com/en/B9106-90012/ unwind.5.html
有关简单栈跟踪,尝试U_STACK_TRACE()。
不隶属于 StackOverflow