我需要在某些点获取C应用程序的堆栈信息。我已阅读文档并搜索了网络,但仍无法弄清楚我能做到这一点。你能指出一个简单的过程解释吗?或者,甚至更好的是一个堆栈展开的例子。我需要它用于HP-UX(Itanium)和Linux。在HP-UX和Linux上展开堆栈
5
A
回答
4
退房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));
}
3
你想看看libunwind - 这是平仓安腾堆栈跟踪惠普最初开发一个跨平台的库(这是特别复杂的);但随后已扩展到许多其他平台;包括x86-Linux和Itanium-HPUX。
从libunwind(3)手册页;这里是一个使用libunwind写一个典型的 '秀回溯' 功能的例子:对于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工作: http://docs.hp.com/en/B9106-90012/unwind.5.html
对于简单的堆栈跟踪,尽量U_STACK_TRACE()。
相关问题
- 1. 如何在HPUX上查找堆/堆栈损坏?
- 2. 从堆栈中间展开堆栈
- 3. 是否有hpux _UNW_STACK_TRACE堆栈展开功能的信号安全版本?
- 4. 使用unwind.h展开堆栈。
- 5. 堆栈展开支持?
- 6. 堆栈安全展开
- 7. 堆栈展开异常C++
- 8. Javascript关闭和堆栈展开
- 9. 在堆栈展开期间打印堆栈变量
- 10. 堆栈在侏儒2展开
- 11. 在Linux上分开堆?
- 12. Linux堆栈大小
- 13. 在x86上没有帧指针的堆栈展开
- 14. 展开PHP堆栈跟踪参数
- 15. 类Python和开发堆栈
- 16. 64堆栈展开和检查,如果RIP是在结语
- 17. Linux上的Zigbee堆栈(主线内核)?
- 18. JVM - 堆栈和堆栈
- 19. 在Reeder和Photos上展开/压缩堆栈的技术是什么?
- 20. 任务堆栈和背堆栈在android
- 21. Linux - 获取线程的堆栈内存的开始和结束
- 22. 在linux中获取每个进程的堆栈和堆栈的大小
- 23. 在PHP中堆栈和堆?
- 24. Antergos Linux - LEMP堆栈 - Nginx 403
- 25. 什么是Linux堆栈?
- 26. x64和x32中的不同行为展开堆栈
- 27. 例外,堆栈展开,包封的堆内存,出口()
- 28. OOP和堆/堆栈
- 29. 块,堆栈和堆
- 30. 在Linux上访问x86-64体系结构上的堆栈帧
我不知道API存在;多么有用! – Jamie 2009-06-10 16:12:23