堆栈展开在HP-UX和Linux [英] Stack unwinding on HP-UX and Linux
问题描述
我需要在某些点上我的C应用程序的堆栈信息。我读过的文档和搜索的网络,但仍然想不通我怎么能做到这一点。您可以指向一个简单的过程解释?或者,甚至更好,栈展开的一个例子。我需要它适用于HP-UX(安腾)和Linux。
I need to get the stack information of my C application in certain points. I've read the documentation and searched the Net but still cannot figure out how I can do it. Can you point to a simple process explanation? Or, even better, to an example of stack unwinding. I need it for HP-UX (Itanium) and Linux.
推荐答案
查看的Linux / stacktrace.h
下面是一个API参考:
Here is an API reference:
HTTP://www.cs。 cmu.edu/afs/cs/Web/People/tekkotsu/dox/StackTrace_8h.html
应该在所有的Linux内核上工作。
Should work on all Linux kernels
下面是在C可替代的示例从
Here is an alternative example in C from
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));
}
这篇关于堆栈展开在HP-UX和Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!