backtrace_ut.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <gtest/gtest.h>
  2. #include <gmock/gmock.h>
  3. #include <library/cpp/yt/memory/safe_memory_reader.h>
  4. #include <library/cpp/yt/backtrace/cursors/frame_pointer/frame_pointer_cursor.h>
  5. #include <library/cpp/yt/backtrace/cursors/interop/interop.h>
  6. #include <util/system/compiler.h>
  7. #include <contrib/libs/libunwind/include/libunwind.h>
  8. namespace NYT::NBacktrace {
  9. namespace {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. template <int Depth, class TFn>
  12. Y_NO_INLINE void RunInDeepStack(TFn cb)
  13. {
  14. if constexpr (Depth == 0) {
  15. cb();
  16. } else {
  17. std::vector<int> touchMem;
  18. touchMem.push_back(0);
  19. RunInDeepStack<Depth-1>(cb);
  20. DoNotOptimizeAway(touchMem);
  21. }
  22. }
  23. TEST(TFramePointerCursor, FramePointerCursor)
  24. {
  25. std::vector<const void*> backtrace;
  26. RunInDeepStack<64>([&] {
  27. unw_context_t unwContext;
  28. ASSERT_TRUE(unw_getcontext(&unwContext) == 0);
  29. unw_cursor_t unwCursor;
  30. ASSERT_TRUE(unw_init_local(&unwCursor, &unwContext) == 0);
  31. TSafeMemoryReader reader;
  32. auto fpCursorContext = NBacktrace::FramePointerCursorContextFromLibunwindCursor(unwCursor);
  33. NBacktrace::TFramePointerCursor fpCursor(&reader, fpCursorContext);
  34. while (!fpCursor.IsFinished()) {
  35. backtrace.push_back(fpCursor.GetCurrentIP());
  36. fpCursor.MoveNext();
  37. }
  38. });
  39. ASSERT_THAT(backtrace, testing::SizeIs(testing::Ge(64u)));
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. } // namespace
  43. } // namespace NYT::NBacktrace