SymbolizerTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2013-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 <folly/experimental/symbolizer/Symbolizer.h>
  17. #include <cstdlib>
  18. #include <folly/Range.h>
  19. #include <folly/String.h>
  20. #include <folly/portability/GTest.h>
  21. namespace folly {
  22. namespace symbolizer {
  23. namespace test {
  24. void foo() {}
  25. TEST(Symbolizer, Single) {
  26. Symbolizer symbolizer;
  27. SymbolizedFrame a;
  28. ASSERT_TRUE(symbolizer.symbolize(reinterpret_cast<uintptr_t>(foo), a));
  29. EXPECT_EQ("folly::symbolizer::test::foo()", a.demangledName());
  30. // The version of clang we use doesn't generate a `.debug_aranges` section,
  31. // which the symbolizer needs to lookup the filename.
  32. constexpr bool built_with_clang =
  33. #ifdef __clang__
  34. true;
  35. #else
  36. false;
  37. #endif
  38. if (!built_with_clang) {
  39. auto path = a.location.file.toString();
  40. folly::StringPiece basename(path);
  41. auto pos = basename.rfind('/');
  42. if (pos != folly::StringPiece::npos) {
  43. basename.advance(pos + 1);
  44. }
  45. EXPECT_EQ("SymbolizerTest.cpp", basename.str());
  46. }
  47. }
  48. FrameArray<100>* framesToFill{nullptr};
  49. int comparator(const void* ap, const void* bp) {
  50. getStackTrace(*framesToFill);
  51. int a = *static_cast<const int*>(ap);
  52. int b = *static_cast<const int*>(bp);
  53. return a < b ? -1 : a > b ? 1 : 0;
  54. }
  55. // Test stack frames...
  56. FOLLY_NOINLINE void bar();
  57. void bar(FrameArray<100>& frames) {
  58. framesToFill = &frames;
  59. int a[2] = {1, 2};
  60. // Use qsort, which is in a different library
  61. qsort(a, 2, sizeof(int), comparator);
  62. framesToFill = nullptr;
  63. }
  64. class ElfCacheTest : public testing::Test {
  65. protected:
  66. void SetUp() override;
  67. };
  68. // Capture "golden" stack trace with default-configured Symbolizer
  69. FrameArray<100> goldenFrames;
  70. void ElfCacheTest::SetUp() {
  71. bar(goldenFrames);
  72. Symbolizer symbolizer;
  73. symbolizer.symbolize(goldenFrames);
  74. // At least 3 stack frames from us + getStackTrace()
  75. ASSERT_LE(4, goldenFrames.frameCount);
  76. }
  77. void runElfCacheTest(Symbolizer& symbolizer) {
  78. FrameArray<100> frames = goldenFrames;
  79. for (size_t i = 0; i < frames.frameCount; ++i) {
  80. frames.frames[i].clear();
  81. }
  82. symbolizer.symbolize(frames);
  83. ASSERT_LE(4, frames.frameCount);
  84. for (size_t i = 1; i < 4; ++i) {
  85. EXPECT_STREQ(goldenFrames.frames[i].name, frames.frames[i].name);
  86. }
  87. }
  88. TEST_F(ElfCacheTest, TinyElfCache) {
  89. ElfCache cache(1);
  90. Symbolizer symbolizer(&cache);
  91. // Run twice, in case the wrong stuff gets evicted?
  92. for (size_t i = 0; i < 2; ++i) {
  93. runElfCacheTest(symbolizer);
  94. }
  95. }
  96. TEST_F(ElfCacheTest, SignalSafeElfCache) {
  97. SignalSafeElfCache cache(100);
  98. Symbolizer symbolizer(&cache);
  99. for (size_t i = 0; i < 2; ++i) {
  100. runElfCacheTest(symbolizer);
  101. }
  102. }
  103. TEST(SymbolizerTest, SymbolCache) {
  104. Symbolizer symbolizer(nullptr, Dwarf::LocationInfoMode::FULL, 100);
  105. FrameArray<100> frames;
  106. bar(frames);
  107. symbolizer.symbolize(frames);
  108. FrameArray<100> frames2;
  109. bar(frames2);
  110. symbolizer.symbolize(frames2);
  111. for (size_t i = 0; i < frames.frameCount; i++) {
  112. EXPECT_STREQ(frames.frames[i].name, frames2.frames[i].name);
  113. }
  114. }
  115. } // namespace test
  116. } // namespace symbolizer
  117. } // namespace folly
  118. // Can't use initFacebookLight since that would install its own signal handlers
  119. // Can't use initFacebookNoSignals since we cannot depend on common
  120. int main(int argc, char** argv) {
  121. ::testing::InitGoogleTest(&argc, argv);
  122. return RUN_ALL_TESTS();
  123. }