| 1 | /* |
| 2 | * Copyright 2018-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/detail/SingletonStackTrace.h> |
| 18 | |
| 19 | #include <folly/portability/Config.h> |
| 20 | |
| 21 | #if FOLLY_USE_SYMBOLIZER |
| 22 | #include <folly/experimental/symbolizer/Symbolizer.h> // @manual |
| 23 | #endif |
| 24 | |
| 25 | namespace folly { |
| 26 | namespace detail { |
| 27 | |
| 28 | std::string getSingletonStackTrace() { |
| 29 | #if FOLLY_USE_SYMBOLIZER |
| 30 | |
| 31 | // Get and symbolize stack trace |
| 32 | constexpr size_t kMaxStackTraceDepth = 100; |
| 33 | symbolizer::FrameArray<kMaxStackTraceDepth> addresses; |
| 34 | |
| 35 | if (!getStackTraceSafe(addresses)) { |
| 36 | return "" ; |
| 37 | } else { |
| 38 | constexpr size_t kDefaultCapacity = 500; |
| 39 | symbolizer::ElfCache elfCache(kDefaultCapacity); |
| 40 | |
| 41 | symbolizer::Symbolizer symbolizer(&elfCache); |
| 42 | symbolizer.symbolize(addresses); |
| 43 | |
| 44 | symbolizer::StringSymbolizePrinter printer; |
| 45 | printer.println(addresses); |
| 46 | return printer.str(); |
| 47 | } |
| 48 | |
| 49 | #else |
| 50 | |
| 51 | return "" ; |
| 52 | |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | } // namespace detail |
| 57 | } // namespace folly |
| 58 | |