You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

86 line
2.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. #if HAVE_CXXABI_H
  12. # include <cxxabi.h>
  13. #endif
  14. #if HAVE_EXECINFO_H
  15. # include <execinfo.h>
  16. #endif
  17. #if EMSCRIPTEN
  18. # include <emscripten.h>
  19. #endif
  20. namespace lol
  21. {
  22. void DumpStack()
  23. {
  24. #if EMSCRIPTEN
  25. /* This would require demangling but we don't care yet. */
  26. Log::Debug("%s\n", emscripten_run_script_string("(new Error).stack"));
  27. #elif HAVE_CXA_DEMANGLE && HAVE_BACKTRACE_SYMBOLS
  28. /* Get current stack frames */
  29. void *stack_ptrs[50];
  30. size_t size = backtrace(stack_ptrs, 50);
  31. char **callstack = backtrace_symbols(stack_ptrs, size);
  32. if (size > 1)
  33. Log::Debug("%d functions in stack trace:\n", (int)size - 1);
  34. /* Parse stack frames, skipping the first element (because
  35. * that’s ourselves) and print information. */
  36. for (size_t i = 1; i < size; ++i)
  37. {
  38. char *name = 0, *offset = 0, *address = 0;
  39. for (char *p = callstack[i]; *p; ++p)
  40. {
  41. if (*p == '(')
  42. name = p;
  43. else if (*p == '+')
  44. offset = p;
  45. else if (*p == ')')
  46. {
  47. address = p;
  48. break;
  49. }
  50. }
  51. if (name && offset && address && name < offset)
  52. {
  53. *name++ = *offset++ = *address++ = '\0';
  54. int ret;
  55. char *realname = abi::__cxa_demangle(name, 0, 0, &ret);
  56. if (ret == 0)
  57. name = realname;
  58. Log::Debug("#%d %s: %s+%s %s\n", (int)i,
  59. callstack[i], name, offset, address);
  60. free(realname);
  61. }
  62. else
  63. {
  64. Log::Debug("#%d %s\n", (int)i, callstack[i]);
  65. }
  66. }
  67. free(callstack);
  68. #endif
  69. }
  70. } /* namespace lol */