Test.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/
  2. // published under the WTFPL v2.0
  3. #ifndef _STACKTRACE_H_
  4. #define _STACKTRACE_H_
  5. #include <cxxabi.h>
  6. #include <execinfo.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. /** Print a demangled stack backtrace of the caller function to FILE* out. */
  10. inline static void print_stacktrace(
  11. FILE* out = stdout, unsigned int max_frames = 3)
  12. {
  13. fprintf(out, "stack trace:\n");
  14. // storage array for stack trace address data
  15. void* addrlist[max_frames + 1];
  16. // retrieve current stack addresses
  17. int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
  18. if (addrlen == 0)
  19. {
  20. fprintf(out, " <empty, possibly corrupt>\n");
  21. return;
  22. }
  23. // resolve addresses into strings containing "filename(function+address)",
  24. // this array must be free()-ed
  25. char** symbollist = backtrace_symbols(addrlist, addrlen);
  26. // allocate string which will be filled with the demangled function name
  27. size_t funcnamesize = 256;
  28. char* funcname = (char*)malloc(funcnamesize);
  29. // iterate over the returned symbol lines. skip the first, it is the
  30. // address of this function.
  31. for (int i = 1; i < addrlen; i++)
  32. {
  33. char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
  34. // find parentheses and +address offset surrounding the mangled name:
  35. // ./module(function+0x15c) [0x8048a6d]
  36. for (char* p = symbollist[i]; *p; ++p)
  37. {
  38. if (*p == '(')
  39. begin_name = p;
  40. else if (*p == '+')
  41. begin_offset = p;
  42. else if (*p == ')' && begin_offset)
  43. {
  44. end_offset = p;
  45. break;
  46. }
  47. }
  48. if (begin_name && begin_offset && end_offset
  49. && begin_name < begin_offset)
  50. {
  51. *begin_name++ = '\0';
  52. *begin_offset++ = '\0';
  53. *end_offset = '\0';
  54. // mangled name is now in [begin_name, begin_offset) and caller
  55. // offset in [begin_offset, end_offset). now apply
  56. // __cxa_demangle():
  57. int status;
  58. char* ret = abi::__cxa_demangle(
  59. begin_name, funcname, &funcnamesize, &status);
  60. if (status == 0)
  61. {
  62. funcname = ret; // use possibly realloc()-ed string
  63. char line[256];
  64. sprintf(line,
  65. " %s : %s+%s\n",
  66. symbollist[i],
  67. funcname,
  68. begin_offset);
  69. int y = 0;
  70. }
  71. else
  72. {
  73. // demangling failed. Output function name as a C function with
  74. // no arguments.
  75. char line[256];
  76. sprintf(line,
  77. " %s : %s()+%s\n",
  78. symbollist[i],
  79. begin_name,
  80. begin_offset);
  81. int x = 0;
  82. }
  83. }
  84. else
  85. {
  86. // couldn't parse the line? print the whole line.
  87. char line[256];
  88. sprintf(line, " %s\n", symbollist[i]);
  89. int x = 0;
  90. }
  91. }
  92. free(funcname);
  93. free(symbollist);
  94. }
  95. #endif // _STACKTRACE_H_