選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

93 行
2.2 KiB

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