73862553cb7ebec7483bcde06649226b87face13
[folly.git] / folly / experimental / symbolizer / test / SymbolizerTest.cpp
1 /*
2  * Copyright 2014 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 <gtest/gtest.h>
22
23 #include "folly/Range.h"
24 #include "folly/String.h"
25
26 namespace folly { namespace symbolizer { namespace test {
27
28 void foo() {
29 }
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   auto path = a.location.file.toString();
38   folly::StringPiece basename(path);
39   auto pos = basename.rfind('/');
40   if (pos != folly::StringPiece::npos) {
41     basename.advance(pos + 1);
42   }
43   EXPECT_EQ("SymbolizerTest.cpp", basename.str());
44 }
45
46 FrameArray<100> goldenFrames;
47
48 int comparator(const void* ap, const void* bp) {
49   getStackTrace(goldenFrames);
50
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
56 // Test stack frames...
57 FOLLY_NOINLINE void bar();
58
59 void bar() {
60   int a[2] = {1, 2};
61   // Use qsort, which is in a different library
62   qsort(a, 2, sizeof(int), comparator);
63 }
64
65 class ElfCacheTest : public testing::Test {
66  protected:
67   void SetUp();
68 };
69
70 // Capture "golden" stack trace with default-configured Symbolizer
71 void ElfCacheTest::SetUp() {
72   bar();
73   Symbolizer symbolizer;
74   symbolizer.symbolize(goldenFrames);
75   // At least 3 stack frames from us + getStackTrace()
76   ASSERT_LE(4, goldenFrames.frameCount);
77 }
78
79 void runElfCacheTest(Symbolizer& symbolizer) {
80   FrameArray<100> frames = goldenFrames;
81   for (size_t i = 0; i < frames.frameCount; ++i) {
82     frames.frames[i].clear();
83   }
84   symbolizer.symbolize(frames);
85   ASSERT_LE(4, frames.frameCount);
86   for (size_t i = 1; i < 4; ++i) {
87     EXPECT_STREQ(goldenFrames.frames[i].name, frames.frames[i].name);
88   }
89 }
90
91 TEST_F(ElfCacheTest, TinyElfCache) {
92   ElfCache cache(1);
93   Symbolizer symbolizer(&cache);
94   // Run twice, in case the wrong stuff gets evicted?
95   for (size_t i = 0; i < 2; ++i) {
96     runElfCacheTest(symbolizer);
97   }
98 }
99
100 TEST_F(ElfCacheTest, SignalSafeElfCache) {
101   SignalSafeElfCache cache(100);
102   Symbolizer symbolizer(&cache);
103   for (size_t i = 0; i < 2; ++i) {
104     runElfCacheTest(symbolizer);
105   }
106 }
107
108 }}}  // namespaces