在HP-UX和Linux上进行堆栈展开

时间:2021-10-18 08:10:33

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.

我需要在某些点获取我的C应用程序的堆栈信息。我已经阅读了文档并搜索了网络,但仍然无法弄清楚我是如何做到的。你能指出一个简单的过程解释吗?或者,甚至更好,以堆栈展开为例。我需要它用于HP-UX(Itanium)和Linux。

3 个解决方案

#1


4  

Check out linux/stacktrace.h

查看linux / stacktrace.h

Here is an API reference:

这是一个API参考:

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

Should work on all Linux kernels

应该适用于所有Linux内核

Here is an alternative example in C from

这是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));

}

#2


3  

You want to look at libunwind - this is a cross-platform library developed originally by HP for unwinding Itanium stack traces (which are particularly complex); but has subsequently been expanded to many other platforms; including both x86-Linux and Itanium-HPUX.

您想要查看libunwind - 这是一个最初由HP开发的用于展开Itanium堆栈跟踪(特别复杂)的跨平台库;但随后又扩展到许多其他平台;包括x86-Linux和Itanium-HPUX。

From the libunwind(3) man page; here is an example of using libunwind to write a typical 'show backtrace' function:

从libunwind(3)手册页;这是一个使用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);
  }
}

#3


0  

This shoulw work for HPUX itanium: http://docs.hp.com/en/B9106-90012/unwind.5.html

这适用于HPUX itanium:http://docs.hp.com/en/B9106-90012/unwind.5.html

For simple stack trace, try U_STACK_TRACE().

对于简单的堆栈跟踪,请尝试U_STACK_TRACE()。

#1


4  

Check out linux/stacktrace.h

查看linux / stacktrace.h

Here is an API reference:

这是一个API参考:

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

Should work on all Linux kernels

应该适用于所有Linux内核

Here is an alternative example in C from

这是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));

}

#2


3  

You want to look at libunwind - this is a cross-platform library developed originally by HP for unwinding Itanium stack traces (which are particularly complex); but has subsequently been expanded to many other platforms; including both x86-Linux and Itanium-HPUX.

您想要查看libunwind - 这是一个最初由HP开发的用于展开Itanium堆栈跟踪(特别复杂)的跨平台库;但随后又扩展到许多其他平台;包括x86-Linux和Itanium-HPUX。

From the libunwind(3) man page; here is an example of using libunwind to write a typical 'show backtrace' function:

从libunwind(3)手册页;这是一个使用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);
  }
}

#3


0  

This shoulw work for HPUX itanium: http://docs.hp.com/en/B9106-90012/unwind.5.html

这适用于HPUX itanium:http://docs.hp.com/en/B9106-90012/unwind.5.html

For simple stack trace, try U_STACK_TRACE().

对于简单的堆栈跟踪,请尝试U_STACK_TRACE()。