stacktrace_emscripten-inl.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Portable implementation - just use glibc
  16. //
  17. // Note: The glibc implementation may cause a call to malloc.
  18. // This can cause a deadlock in HeapProfiler.
  19. #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_EMSCRIPTEN_INL_H_
  20. #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_EMSCRIPTEN_INL_H_
  21. #error #include <emscripten.h>
  22. #include <atomic>
  23. #include <cstring>
  24. #include "absl/base/attributes.h"
  25. #include "absl/debugging/stacktrace.h"
  26. extern "C" {
  27. uintptr_t emscripten_stack_snapshot();
  28. uint32_t emscripten_stack_unwind_buffer(uintptr_t pc, void *buffer,
  29. uint32_t depth);
  30. }
  31. // Sometimes, we can try to get a stack trace from within a stack
  32. // trace, which can cause a self-deadlock.
  33. // Protect against such reentrant call by failing to get a stack trace.
  34. //
  35. // We use __thread here because the code here is extremely low level -- it is
  36. // called while collecting stack traces from within malloc and mmap, and thus
  37. // can not call anything which might call malloc or mmap itself.
  38. static __thread int recursive = 0;
  39. // The stack trace function might be invoked very early in the program's
  40. // execution (e.g. from the very first malloc).
  41. // As such, we suppress usage of backtrace during this early stage of execution.
  42. static std::atomic<bool> disable_stacktraces(true); // Disabled until healthy.
  43. // Waiting until static initializers run seems to be late enough.
  44. // This file is included into stacktrace.cc so this will only run once.
  45. ABSL_ATTRIBUTE_UNUSED static int stacktraces_enabler = []() {
  46. // Check if we can even create stacktraces. If not, bail early and leave
  47. // disable_stacktraces set as-is.
  48. // clang-format off
  49. if (!EM_ASM_INT({ return (typeof wasmOffsetConverter !== 'undefined'); })) {
  50. return 0;
  51. }
  52. // clang-format on
  53. disable_stacktraces.store(false, std::memory_order_relaxed);
  54. return 0;
  55. }();
  56. template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
  57. static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
  58. const void *ucp, int *min_dropped_frames) {
  59. if (recursive || disable_stacktraces.load(std::memory_order_relaxed)) {
  60. return 0;
  61. }
  62. ++recursive;
  63. static_cast<void>(ucp); // Unused.
  64. constexpr int kStackLength = 64;
  65. void *stack[kStackLength];
  66. int size;
  67. uintptr_t pc = emscripten_stack_snapshot();
  68. size = emscripten_stack_unwind_buffer(pc, stack, kStackLength);
  69. int result_count = size - skip_count;
  70. if (result_count < 0) result_count = 0;
  71. if (result_count > max_depth) result_count = max_depth;
  72. for (int i = 0; i < result_count; i++) result[i] = stack[i + skip_count];
  73. if (IS_STACK_FRAMES) {
  74. // No implementation for finding out the stack frame sizes yet.
  75. memset(sizes, 0, sizeof(*sizes) * result_count);
  76. }
  77. if (min_dropped_frames != nullptr) {
  78. if (size - skip_count - max_depth > 0) {
  79. *min_dropped_frames = size - skip_count - max_depth;
  80. } else {
  81. *min_dropped_frames = 0;
  82. }
  83. }
  84. --recursive;
  85. return result_count;
  86. }
  87. namespace absl {
  88. ABSL_NAMESPACE_BEGIN
  89. namespace debugging_internal {
  90. bool StackTraceWorksForTest() { return true; }
  91. } // namespace debugging_internal
  92. ABSL_NAMESPACE_END
  93. } // namespace absl
  94. #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_EMSCRIPTEN_INL_H_