Commit 7c996f43 authored by Andrew Gallagher's avatar Andrew Gallagher Committed by Facebook Github Bot

folly/Singleton: remove use of `link_whole`

Summary:
We used `link_whole` to make sure the `setStackTraceGetter` constructor is
always linked when using Singleton's to setup symbolizer for stack traces.
However, using `link_whole` means we always link in the singleton library, even
if it's never used.

This diff merges `SingletonStackTrace.cpp` into `Singleton.cpp` to avoid the
need for `link_whole` and to link the former if and only if we use the latter.

Reviewed By: pixelb

Differential Revision: D7230733

fbshipit-source-id: de9be51a4eefee201563523d25800ab19ea777e3
parent c73cbb82
......@@ -15,6 +15,7 @@
*/
#include <folly/Singleton.h>
#include <folly/portability/Config.h>
#ifndef _WIN32
#include <dlfcn.h>
......@@ -30,6 +31,10 @@
#include <folly/Format.h>
#include <folly/ScopeGuard.h>
#ifdef FOLLY_USE_SYMBOLIZER
#include <folly/experimental/symbolizer/Symbolizer.h> // @manual
#endif
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__)
#define FOLLY_SINGLETON_HAVE_DLSYM 1
#endif
......@@ -377,4 +382,46 @@ void SingletonVault::scheduleDestroyInstances() {
std::atexit([] { SingletonVault::singleton()->destroyInstances(); });
}
// If we're using folly's Symbolizer, create a static initializer to setup
// Singltone's to use it to print stack traces. It's important that we keep
// this in the same compilation unit as the `SingletonVault` so that it's
// setup/used iff singleton's are used.
#ifdef FOLLY_USE_SYMBOLIZER
namespace {
std::string stackTraceGetter() {
// Get and symbolize stack trace
constexpr size_t kMaxStackTraceDepth = 100;
symbolizer::FrameArray<kMaxStackTraceDepth> addresses;
if (!getStackTraceSafe(addresses)) {
return "";
} else {
constexpr size_t kDefaultCapacity = 500;
symbolizer::ElfCache elfCache(kDefaultCapacity);
symbolizer::Symbolizer symbolizer(&elfCache);
symbolizer.symbolize(addresses);
symbolizer::StringSymbolizePrinter printer;
printer.println(addresses);
return printer.str();
}
}
struct SetStackTraceGetter {
SetStackTraceGetter() {
SingletonVault::stackTraceGetter().store(stackTraceGetter);
}
};
#ifdef __APPLE__
// OS X doesn't support constructor priorities.
SetStackTraceGetter setStackTraceGetter;
#else
SetStackTraceGetter __attribute__((__init_priority__(101))) setStackTraceGetter;
#endif
} // namespace
#endif
} // namespace folly
/*
* Copyright 2015-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/portability/Config.h>
#ifdef FOLLY_USE_SYMBOLIZER
#include <folly/Singleton.h>
#include <folly/experimental/symbolizer/Symbolizer.h> // @manual
namespace folly {
namespace {
std::string stackTraceGetter() {
// Get and symbolize stack trace
constexpr size_t kMaxStackTraceDepth = 100;
symbolizer::FrameArray<kMaxStackTraceDepth> addresses;
if (!getStackTraceSafe(addresses)) {
return "";
} else {
constexpr size_t kDefaultCapacity = 500;
symbolizer::ElfCache elfCache(kDefaultCapacity);
symbolizer::Symbolizer symbolizer(&elfCache);
symbolizer.symbolize(addresses);
symbolizer::StringSymbolizePrinter printer;
printer.println(addresses);
return printer.str();
}
}
struct SetStackTraceGetter {
SetStackTraceGetter() {
SingletonVault::stackTraceGetter().store(stackTraceGetter);
}
};
#ifdef __APPLE__
// OS X doesn't support constructor priorities.
SetStackTraceGetter setStackTraceGetter;
#else
SetStackTraceGetter __attribute__((__init_priority__(101))) setStackTraceGetter;
#endif
} // namespace
} // namespace folly
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment