ExceptionStackTraceLib.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <exception>
  17. #include <folly/experimental/exception_tracer/ExceptionAbi.h>
  18. #include <folly/experimental/exception_tracer/ExceptionTracer.h>
  19. #include <folly/experimental/exception_tracer/ExceptionTracerLib.h>
  20. #include <folly/experimental/exception_tracer/StackTrace.h>
  21. #include <folly/experimental/symbolizer/Symbolizer.h>
  22. using namespace folly::exception_tracer;
  23. namespace {
  24. // If we somehow ended up in an invalid state, we don't want to print any stack
  25. // trace at all because in could be bogus
  26. FOLLY_TLS bool invalid;
  27. FOLLY_TLS StackTraceStack activeExceptions;
  28. FOLLY_TLS StackTraceStack caughtExceptions;
  29. } // namespace
  30. // This function is exported and may be found via dlsym(RTLD_NEXT, ...)
  31. extern "C" StackTraceStack* getExceptionStackTraceStack() {
  32. return invalid ? nullptr : &caughtExceptions;
  33. }
  34. namespace {
  35. void addActiveException() {
  36. // Capture stack trace
  37. if (!invalid) {
  38. if (!activeExceptions.pushCurrent()) {
  39. activeExceptions.clear();
  40. caughtExceptions.clear();
  41. invalid = true;
  42. }
  43. }
  44. }
  45. void moveTopException(StackTraceStack& from, StackTraceStack& to) {
  46. if (invalid) {
  47. return;
  48. }
  49. if (!to.moveTopFrom(from)) {
  50. from.clear();
  51. to.clear();
  52. invalid = true;
  53. }
  54. }
  55. struct Initializer {
  56. Initializer() {
  57. registerCxaThrowCallback(
  58. [](void*, std::type_info*, void (*)(void*)) noexcept {
  59. addActiveException();
  60. });
  61. registerCxaBeginCatchCallback([](void*) noexcept {
  62. moveTopException(activeExceptions, caughtExceptions);
  63. });
  64. registerCxaRethrowCallback([]() noexcept {
  65. moveTopException(caughtExceptions, activeExceptions);
  66. });
  67. registerCxaEndCatchCallback([]() noexcept {
  68. if (invalid) {
  69. return;
  70. }
  71. __cxxabiv1::__cxa_exception* top =
  72. __cxxabiv1::__cxa_get_globals_fast()->caughtExceptions;
  73. // This is gcc specific and not specified in the ABI:
  74. // abs(handlerCount) is the number of active handlers, it's negative
  75. // for rethrown exceptions and positive (always 1) for regular
  76. // exceptions.
  77. // In the rethrow case, we've already popped the exception off the
  78. // caught stack, so we don't do anything here.
  79. // For Lua interop, we see the handlerCount = 0
  80. if ((top->handlerCount == 1) || (top->handlerCount == 0)) {
  81. if (!caughtExceptions.pop()) {
  82. activeExceptions.clear();
  83. invalid = true;
  84. }
  85. }
  86. });
  87. registerRethrowExceptionCallback(
  88. [](std::exception_ptr) noexcept { addActiveException(); });
  89. try {
  90. ::folly::exception_tracer::installHandlers();
  91. } catch (...) {
  92. }
  93. }
  94. };
  95. Initializer initializer;
  96. } // namespace