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