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()",
36 demangle(a.name.str().c_str()));
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);
44 EXPECT_EQ("SymbolizerTest.cpp", basename.str());
47 FrameArray<100> goldenFrames;
49 int comparator(const void* ap, const void* bp) {
50 getStackTrace(goldenFrames);
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;
57 // Test stack frames...
58 void bar() __attribute__((noinline));
62 // Use qsort, which is in a different library
63 qsort(a, 2, sizeof(int), comparator);
66 class ElfCacheTest : public testing::Test {
71 // Capture "golden" stack trace with default-configured Symbolizer
72 void ElfCacheTest::SetUp() {
74 Symbolizer symbolizer;
75 symbolizer.symbolize(goldenFrames);
76 // At least 3 stack frames from us + getStackTrace()
77 ASSERT_LE(4, goldenFrames.frameCount);
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];
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);
94 TEST_F(ElfCacheTest, TinyElfCache) {
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);
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);