folly: fix make_optional compliation issue with gnu++17
[folly.git] / folly / SingletonStackTrace.cpp
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/portability/Config.h>
18
19 #ifdef FOLLY_USE_SYMBOLIZER
20 #include <folly/Singleton.h>
21 #include <folly/experimental/symbolizer/Symbolizer.h> // @manual
22
23 namespace folly {
24
25 namespace {
26
27 std::string stackTraceGetter() {
28   // Get and symbolize stack trace
29   constexpr size_t kMaxStackTraceDepth = 100;
30   symbolizer::FrameArray<kMaxStackTraceDepth> addresses;
31
32   if (!getStackTraceSafe(addresses)) {
33     return "";
34   } else {
35     constexpr size_t kDefaultCapacity = 500;
36     symbolizer::ElfCache elfCache(kDefaultCapacity);
37
38     symbolizer::Symbolizer symbolizer(&elfCache);
39     symbolizer.symbolize(addresses);
40
41     symbolizer::StringSymbolizePrinter printer;
42     printer.println(addresses);
43     return printer.str();
44   }
45 }
46
47 struct SetStackTraceGetter {
48   SetStackTraceGetter() {
49     SingletonVault::stackTraceGetter().store(stackTraceGetter);
50   }
51 };
52
53 #ifdef __APPLE__
54 // OS X doesn't support constructor priorities.
55 SetStackTraceGetter setStackTraceGetter;
56 #else
57 SetStackTraceGetter __attribute__((__init_priority__(101))) setStackTraceGetter;
58 #endif
59 } // namespace
60 } // namespace folly
61 #endif