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/experimental/symbolizer/Elf.h"
27 #include "folly/experimental/symbolizer/Dwarf.h"
28 #include "folly/experimental/symbolizer/StackTrace.h"
31 namespace symbolizer {
34 * Frame information: symbol name and location.
36 * Note that both name and location are references in the Symbolizer object,
37 * which must outlive this SymbolizedFrame object.
39 struct SymbolizedFrame {
40 SymbolizedFrame() : found(false) { }
44 Dwarf::LocationInfo location;
49 FrameArray() : frameCount(0) { }
52 uintptr_t addresses[N];
53 SymbolizedFrame frames[N];
57 * Get stack trace into a given FrameArray, return true on success (and
58 * set frameCount to the actual frame count, which may be > N) and false
63 bool fixFrameArray(FrameArray<N>& fa, ssize_t n) {
66 for (size_t i = 0; i < fa.frameCount; ++i) {
67 fa.frames[i].found = false;
78 bool getStackTrace(FrameArray<N>& fa) {
79 return detail::fixFrameArray(fa, getStackTrace(fa.addresses, N));
83 bool getStackTraceSafe(FrameArray<N>& fa) {
84 return detail::fixFrameArray(fa, getStackTraceSafe(fa.addresses, N));
89 Symbolizer() : fileCount_(0) { }
92 * Symbolize given addresses.
94 void symbolize(const uintptr_t* addresses,
95 SymbolizedFrame* frames,
99 void symbolize(FrameArray<N>& fa) {
100 symbolize(fa.addresses, fa.frames, fa.frameCount);
104 * Shortcut to symbolize one address.
106 bool symbolize(uintptr_t address, SymbolizedFrame& frame) {
107 symbolize(&address, &frame, 1);
112 // We can't allocate memory, so we'll preallocate room.
113 // "1023 shared libraries should be enough for everyone"
114 static constexpr size_t kMaxFiles = 1024;
116 ElfFile files_[kMaxFiles];
120 * Print a list of symbolized addresses. Base class.
122 class SymbolizePrinter {
125 * Print one address, no ending newline.
127 void print(uintptr_t address, const SymbolizedFrame& frame);
130 * Print one address with ending newline.
132 void println(uintptr_t address, const SymbolizedFrame& frame);
135 * Print multiple addresses on separate lines.
137 void println(const uintptr_t* addresses,
138 const SymbolizedFrame* frames,
142 * Print multiple addresses on separate lines, skipping the first
146 void println(const FrameArray<N>& fa, size_t skip=0) {
147 if (skip < fa.frameCount) {
148 println(fa.addresses + skip, fa.frames + skip, fa.frameCount - skip);
152 virtual ~SymbolizePrinter() { }
155 // Skip file and line information
156 NO_FILE_AND_LINE = 1 << 0,
158 // As terse as it gets: function name if found, address otherwise
161 // Always colorize output (ANSI escape code)
164 // Colorize output only if output is printed to a TTY (ANSI escape code)
165 COLOR_IF_TTY = 1 << 3,
168 enum Color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, WHITE, PURPLE };
172 explicit SymbolizePrinter(int options, bool isTty = false)
181 void printTerse(uintptr_t address, const SymbolizedFrame& frame);
182 virtual void doPrint(StringPiece sp) = 0;
186 * Print a list of symbolized addresses to a stream.
187 * Not reentrant. Do not use from signal handling code.
189 class OStreamSymbolizePrinter : public SymbolizePrinter {
191 explicit OStreamSymbolizePrinter(std::ostream& out, int options=0);
193 void doPrint(StringPiece sp) override;
198 * Print a list of symbolized addresses to a file descriptor.
199 * Ignores errors. Async-signal-safe.
201 class FDSymbolizePrinter : public SymbolizePrinter {
203 explicit FDSymbolizePrinter(int fd, int options=0);
205 void doPrint(StringPiece sp) override;
210 * Print a list of symbolized addresses to a FILE*.
211 * Ignores errors. Not reentrant. Do not use from signal handling code.
213 class FILESymbolizePrinter : public SymbolizePrinter {
215 explicit FILESymbolizePrinter(FILE* file, int options=0);
217 void doPrint(StringPiece sp) override;
222 * Print a list of symbolized addresses to a std::string.
223 * Not reentrant. Do not use from signal handling code.
225 class StringSymbolizePrinter : public SymbolizePrinter {
227 explicit StringSymbolizePrinter(int options=0) : SymbolizePrinter(options) { }
229 std::string str() const { return buf_.toStdString(); }
230 const fbstring& fbstr() const { return buf_; }
231 fbstring moveFbString() { return std::move(buf_); }
234 void doPrint(StringPiece sp) override;
238 } // namespace symbolizer
241 #endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */