2 * Copyright 2014 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 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);
43 EXPECT_EQ("SymbolizerTest.cpp", basename.str());
46 FrameArray<100> goldenFrames;
48 int comparator(const void* ap, const void* bp) {
49 getStackTrace(goldenFrames);
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;
56 // Test stack frames...
57 FOLLY_NOINLINE void bar();
61 // Use qsort, which is in a different library
62 qsort(a, 2, sizeof(int), comparator);
65 class ElfCacheTest : public testing::Test {
70 // Capture "golden" stack trace with default-configured Symbolizer
71 void ElfCacheTest::SetUp() {
73 Symbolizer symbolizer;
74 symbolizer.symbolize(goldenFrames);
75 // At least 3 stack frames from us + getStackTrace()
76 ASSERT_LE(4, goldenFrames.frameCount);
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();
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);
91 TEST_F(ElfCacheTest, TinyElfCache) {
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);
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);