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