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 #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
18 #define FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
22 #include <unordered_map>
24 #include "folly/FBString.h"
25 #include "folly/Range.h"
26 #include "folly/String.h"
27 #include "folly/experimental/symbolizer/Elf.h"
28 #include "folly/experimental/symbolizer/ElfCache.h"
29 #include "folly/experimental/symbolizer/Dwarf.h"
30 #include "folly/experimental/symbolizer/StackTrace.h"
33 namespace symbolizer {
36 * Frame information: symbol name and location.
38 * Note that both name and location are references in the Symbolizer object,
39 * which must outlive this SymbolizedFrame object.
41 struct SymbolizedFrame {
42 SymbolizedFrame() : found(false) { }
46 Dwarf::LocationInfo location;
49 * Demangle the name and return it. Not async-signal-safe; allocates memory.
51 fbstring demangledName() const {
52 return demangle(name.fbstr().c_str());
58 FrameArray() : frameCount(0) { }
61 uintptr_t addresses[N];
62 SymbolizedFrame frames[N];
66 * Get stack trace into a given FrameArray, return true on success (and
67 * set frameCount to the actual frame count, which may be > N) and false
72 bool fixFrameArray(FrameArray<N>& fa, ssize_t n) {
75 for (size_t i = 0; i < fa.frameCount; ++i) {
76 fa.frames[i].found = false;
86 // Always inline these functions; they don't do much, and unittests rely
87 // on them never showing up in a stack trace.
89 inline bool getStackTrace(FrameArray<N>& fa) __attribute__((always_inline));
92 inline bool getStackTrace(FrameArray<N>& fa) {
93 return detail::fixFrameArray(fa, getStackTrace(fa.addresses, N));
96 inline bool getStackTraceSafe(FrameArray<N>& fa) __attribute__((always_inline));
99 inline bool getStackTraceSafe(FrameArray<N>& fa) {
100 return detail::fixFrameArray(fa, getStackTraceSafe(fa.addresses, N));
105 explicit Symbolizer(ElfCacheBase* cache = nullptr);
108 * Symbolize given addresses.
110 void symbolize(const uintptr_t* addresses,
111 SymbolizedFrame* frames,
115 void symbolize(FrameArray<N>& fa) {
116 symbolize(fa.addresses, fa.frames, fa.frameCount);
120 * Shortcut to symbolize one address.
122 bool symbolize(uintptr_t address, SymbolizedFrame& frame) {
123 symbolize(&address, &frame, 1);
128 ElfCacheBase* cache_;
132 * Print a list of symbolized addresses. Base class.
134 class SymbolizePrinter {
137 * Print one address, no ending newline.
139 void print(uintptr_t address, const SymbolizedFrame& frame);
142 * Print one address with ending newline.
144 void println(uintptr_t address, const SymbolizedFrame& frame);
147 * Print multiple addresses on separate lines.
149 void println(const uintptr_t* addresses,
150 const SymbolizedFrame* frames,
154 * Print multiple addresses on separate lines, skipping the first
158 void println(const FrameArray<N>& fa, size_t skip=0) {
159 if (skip < fa.frameCount) {
160 println(fa.addresses + skip, fa.frames + skip, fa.frameCount - skip);
164 virtual ~SymbolizePrinter() { }
167 // Skip file and line information
168 NO_FILE_AND_LINE = 1 << 0,
170 // As terse as it gets: function name if found, address otherwise
173 // Always colorize output (ANSI escape code)
176 // Colorize output only if output is printed to a TTY (ANSI escape code)
177 COLOR_IF_TTY = 1 << 3,
180 enum Color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, WHITE, PURPLE };
184 explicit SymbolizePrinter(int options, bool isTty = false)
193 void printTerse(uintptr_t address, const SymbolizedFrame& frame);
194 virtual void doPrint(StringPiece sp) = 0;
198 * Print a list of symbolized addresses to a stream.
199 * Not reentrant. Do not use from signal handling code.
201 class OStreamSymbolizePrinter : public SymbolizePrinter {
203 explicit OStreamSymbolizePrinter(std::ostream& out, int options=0);
205 void doPrint(StringPiece sp) override;
210 * Print a list of symbolized addresses to a file descriptor.
211 * Ignores errors. Async-signal-safe.
213 class FDSymbolizePrinter : public SymbolizePrinter {
215 explicit FDSymbolizePrinter(int fd, int options=0);
217 void doPrint(StringPiece sp) override;
222 * Print a list of symbolized addresses to a FILE*.
223 * Ignores errors. Not reentrant. Do not use from signal handling code.
225 class FILESymbolizePrinter : public SymbolizePrinter {
227 explicit FILESymbolizePrinter(FILE* file, int options=0);
229 void doPrint(StringPiece sp) override;
234 * Print a list of symbolized addresses to a std::string.
235 * Not reentrant. Do not use from signal handling code.
237 class StringSymbolizePrinter : public SymbolizePrinter {
239 explicit StringSymbolizePrinter(int options=0) : SymbolizePrinter(options) { }
241 std::string str() const { return buf_.toStdString(); }
242 const fbstring& fbstr() const { return buf_; }
243 fbstring moveFbString() { return std::move(buf_); }
246 void doPrint(StringPiece sp) override;
250 } // namespace symbolizer
253 #endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */