Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

94 linhas
2.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2020 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. #include <../legacy/lol/base/assert.h>
  14. #if HAVE_CXXABI_H
  15. # include <cxxabi.h>
  16. #endif
  17. #if HAVE_EXECINFO_H
  18. # include <execinfo.h>
  19. #endif
  20. #if HAVE_EMSCRIPTEN_H
  21. # include <emscripten.h>
  22. #endif
  23. namespace lol
  24. {
  25. namespace debug
  26. {
  27. void dump_stack()
  28. {
  29. #if __EMSCRIPTEN__
  30. /* This would require demangling but we don't care yet. */
  31. msg::debug("%s\n", emscripten_run_script_string("(new Error).stack"));
  32. #elif HAVE_CXA_DEMANGLE && HAVE_BACKTRACE_SYMBOLS
  33. /* Get current stack frames */
  34. void *stack_ptrs[50];
  35. size_t size = backtrace(stack_ptrs, 50);
  36. char **callstack = backtrace_symbols(stack_ptrs, size);
  37. if (size > 1)
  38. msg::debug("%d functions in stack trace:\n", (int)size - 1);
  39. /* Parse stack frames, skipping the first element (because
  40. * that’s ourselves) and print information. */
  41. for (size_t i = 1; i < size; ++i)
  42. {
  43. char *name = 0, *offset = 0, *address = 0;
  44. for (char *p = callstack[i]; *p; ++p)
  45. {
  46. if (*p == '(')
  47. name = p;
  48. else if (*p == '+')
  49. offset = p;
  50. else if (*p == ')')
  51. {
  52. address = p;
  53. break;
  54. }
  55. }
  56. if (name && offset && address && name < offset)
  57. {
  58. *name++ = *offset++ = *address++ = '\0';
  59. int ret;
  60. char *realname = abi::__cxa_demangle(name, 0, 0, &ret);
  61. if (ret == 0)
  62. name = realname;
  63. msg::debug("#%d %s: %s+%s %s\n", (int)i,
  64. callstack[i], name, offset, address);
  65. free(realname);
  66. }
  67. else
  68. {
  69. msg::debug("#%d %s\n", (int)i, callstack[i]);
  70. }
  71. }
  72. free(callstack);
  73. #endif
  74. }
  75. } /* namespace debug */
  76. } /* namespace lol */