Cache open ELF files in Symbolizer
[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()",
36             demangle(a.name.str().c_str()));
37
38   auto path = a.location.file.toString();
39   folly::StringPiece basename(path);
40   auto pos = basename.rfind('/');
41   if (pos != folly::StringPiece::npos) {
42     basename.advance(pos + 1);
43   }
44   EXPECT_EQ("SymbolizerTest.cpp", basename.str());
45 }
46
47 FrameArray<100> goldenFrames;
48
49 int comparator(const void* ap, const void* bp) {
50   getStackTrace(goldenFrames);
51
52   int a = *static_cast<const int*>(ap);
53   int b = *static_cast<const int*>(bp);
54   return a < b ? -1 : a > b ? 1 : 0;
55 }
56
57 // Test stack frames...
58 void bar() __attribute__((noinline));
59
60 void bar() {
61   int a[2] = {1, 2};
62   // Use qsort, which is in a different library
63   qsort(a, 2, sizeof(int), comparator);
64 }
65
66 class ElfCacheTest : public testing::Test {
67  protected:
68   void SetUp();
69 };
70
71 // Capture "golden" stack trace with default-configured Symbolizer
72 void ElfCacheTest::SetUp() {
73   bar();
74   Symbolizer symbolizer;
75   symbolizer.symbolize(goldenFrames);
76   // At least 3 stack frames from us + getStackTrace()
77   ASSERT_LE(4, goldenFrames.frameCount);
78 }
79
80 void runElfCacheTest(Symbolizer& symbolizer) {
81   FrameArray<100> frames = goldenFrames;
82   for (size_t i = 0; i < frames.frameCount; ++i) {
83     auto& f = frames.frames[i];
84     f.found = false;
85     f.name.clear();
86   }
87   symbolizer.symbolize(frames);
88   ASSERT_LE(4, frames.frameCount);
89   for (size_t i = 1; i < 4; ++i) {
90     EXPECT_EQ(goldenFrames.frames[i].name, frames.frames[i].name);
91   }
92 }
93
94 TEST_F(ElfCacheTest, TinyElfCache) {
95   ElfCache cache(1);
96   Symbolizer symbolizer(&cache);
97   // Run twice, in case the wrong stuff gets evicted?
98   for (size_t i = 0; i < 2; ++i) {
99     runElfCacheTest(symbolizer);
100   }
101 }
102
103 TEST_F(ElfCacheTest, SignalSafeElfCache) {
104   SignalSafeElfCache cache(100);
105   Symbolizer symbolizer(&cache);
106   for (size_t i = 0; i < 2; ++i) {
107     runElfCacheTest(symbolizer);
108   }
109 }
110
111 }}}  // namespaces