2 * Copyright 2016 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 <gtest/gtest.h>
23 #include <folly/Range.h>
24 #include <folly/String.h>
26 namespace folly { namespace symbolizer { namespace test {
31 TEST(Symbolizer, Single) {
32 Symbolizer symbolizer;
34 ASSERT_TRUE(symbolizer.symbolize(reinterpret_cast<uintptr_t>(foo), a));
35 EXPECT_EQ("folly::symbolizer::test::foo()", a.demangledName());
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 =
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);
52 EXPECT_EQ("SymbolizerTest.cpp", basename.str());
56 FrameArray<100> goldenFrames;
58 int comparator(const void* ap, const void* bp) {
59 getStackTrace(goldenFrames);
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;
66 // Test stack frames...
67 FOLLY_NOINLINE void bar();
71 // Use qsort, which is in a different library
72 qsort(a, 2, sizeof(int), comparator);
75 class ElfCacheTest : public testing::Test {
77 void SetUp() override;
80 // Capture "golden" stack trace with default-configured Symbolizer
81 void ElfCacheTest::SetUp() {
83 Symbolizer symbolizer;
84 symbolizer.symbolize(goldenFrames);
85 // At least 3 stack frames from us + getStackTrace()
86 ASSERT_LE(4, goldenFrames.frameCount);
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();
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);
101 TEST_F(ElfCacheTest, TinyElfCache) {
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);
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);
120 // Can't use initFacebookLight since that would install its own signal handlers
121 // Can't use initFacebookNoSignals since we cannot depend on common
122 int main(int argc, char** argv) {
123 ::testing::InitGoogleTest(&argc, argv);
124 return RUN_ALL_TESTS();