pandora_print_callstack.m4 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. dnl Copyright (C) 2009 Sun Microsystems, Inc.
  2. dnl This file is free software; Sun Microsystems, Inc.
  3. dnl gives unlimited permission to copy and/or distribute it,
  4. dnl with or without modifications, as long as this notice is preserved.
  5. dnl Try to define a macro to dump the current callstack.
  6. AC_DEFUN([PANDORA_PRINT_CALLSTACK],[
  7. AC_CHECK_HEADERS([ucontext.h])
  8. AS_IF([test "x$ac_cv_header_ucontext_h" = "xyes"],
  9. [ AC_CHECK_FUNCS([printstack]) ])
  10. AS_IF([ test "x$ac_cv_func_printstack" != "xyes"],
  11. [ AC_CHECK_HEADERS([dlfcn.h])
  12. AC_CHECK_HEADERS([execinfo.h])
  13. AC_CHECK_FUNCS([backtrace])
  14. AC_CHECK_FUNCS([backtrace_symbols_fd]) ])
  15. AH_BOTTOM([
  16. #ifdef __cplusplus
  17. #include <cstdio>
  18. #define PANDORA_PRINTSTACK_STD_PREFIX std::
  19. #else
  20. #include <stdio.h>
  21. #define PANDORA_PRINTSTACK_STD_PREFIX
  22. #endif
  23. #if defined(HAVE_UCONTEXT_H) && defined(HAVE_PRINTSTACK)
  24. #include <ucontext.h>
  25. #define pandora_print_callstack(a) \
  26. printstack(PANDORA_PRINTSTACK_STD_PREFIX fileno(a))
  27. #elif defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS_FD)
  28. #include <execinfo.h>
  29. #define pandora_print_callstack(a) \
  30. { \
  31. void *stack[100]; \
  32. int depth = backtrace(stack, 100); \
  33. backtrace_symbols_fd(stack, depth, PANDORA_PRINTSTACK_STD_PREFIX fileno(a)); \
  34. }
  35. #elif defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS) && !defined(HAVE_BACKTRACE_SYMBOLS_FD)
  36. #include <execinfo.h>
  37. #define pandora_print_callstack(a) \
  38. { \
  39. void *stack[100]; \
  40. int depth= backtrace(stack, 100); \
  41. char **symbol= backtrace_symbols(stack, depth); \
  42. for (int x= 0; x < size; ++x) \
  43. PANDORA_PRINTSTACK_STD_PREFIX fprintf(a, "%s\n", symbol[x]); \
  44. }
  45. #else
  46. #define pandora_print_callstack(a) \
  47. PANDORA_PRINTSTACK_STD_PREFIX fprintf(a, \
  48. "Stackdump not supported for this platform\n");
  49. #endif
  50. ])
  51. ])