2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/symbolizer/Symbolizer.h>
21 #include <folly/Range.h>
22 #include <folly/String.h>
23 #include <folly/portability/GTest.h>
25 namespace folly { namespace symbolizer { namespace test {
30 TEST(Symbolizer, Single) {
31 Symbolizer symbolizer;
33 ASSERT_TRUE(symbolizer.symbolize(reinterpret_cast<uintptr_t>(foo), a));
34 EXPECT_EQ("folly::symbolizer::test::foo()", a.demangledName());
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 =
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);
51 EXPECT_EQ("SymbolizerTest.cpp", basename.str());
55 FrameArray<100> goldenFrames;
57 int comparator(const void* ap, const void* bp) {
58 getStackTrace(goldenFrames);
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;
65 // Test stack frames...
66 FOLLY_NOINLINE void bar();
70 // Use qsort, which is in a different library
71 qsort(a, 2, sizeof(int), comparator);
74 class ElfCacheTest : public testing::Test {
76 void SetUp() override;
79 // Capture "golden" stack trace with default-configured Symbolizer
80 void ElfCacheTest::SetUp() {
82 Symbolizer symbolizer;
83 symbolizer.symbolize(goldenFrames);
84 // At least 3 stack frames from us + getStackTrace()
85 ASSERT_LE(4, goldenFrames.frameCount);
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();
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);
100 TEST_F(ElfCacheTest, TinyElfCache) {
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);
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);
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();