2 * Copyright 2013 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.
18 #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
19 #define FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
23 #include <unordered_map>
25 #include "folly/FBString.h"
26 #include "folly/Range.h"
27 #include "folly/experimental/symbolizer/Elf.h"
28 #include "folly/experimental/symbolizer/Dwarf.h"
29 #include "folly/experimental/symbolizer/StackTrace.h"
32 namespace symbolizer {
35 * Frame information: symbol name and location.
37 * Note that both name and location are references in the Symbolizer object,
38 * which must outlive this SymbolizedFrame object.
40 struct SymbolizedFrame {
41 SymbolizedFrame() : found(false) { }
45 Dwarf::LocationInfo location;
50 FrameArray() : frameCount(0) { }
53 uintptr_t addresses[N];
54 SymbolizedFrame frames[N];
58 * Get stack trace into a given FrameArray, return true on success (and
59 * set frameCount to the actual frame count, which may be > N) and false
63 bool getStackTrace(FrameArray<N>& fa) {
64 ssize_t n = getStackTrace(fa.addresses, N);
67 for (size_t i = 0; i < fa.frameCount; ++i) {
68 fa.frames[i].found = false;
79 Symbolizer() : fileCount_(0) { }
82 * Symbolize given addresses.
84 void symbolize(const uintptr_t* addresses,
85 SymbolizedFrame* frames,
89 void symbolize(FrameArray<N>& fa) {
90 symbolize(fa.addresses, fa.frames, fa.frameCount);
94 * Shortcut to symbolize one address.
96 bool symbolize(uintptr_t address, SymbolizedFrame& frame) {
97 symbolize(&address, &frame, 1);
102 // We can't allocate memory, so we'll preallocate room.
103 // "1023 shared libraries should be enough for everyone"
104 static constexpr size_t kMaxFiles = 1024;
106 ElfFile files_[kMaxFiles];
110 * Print a list of symbolized addresses. Base class.
112 class SymbolizePrinter {
115 * Print one address, no ending newline.
117 void print(uintptr_t address, const SymbolizedFrame& frame);
120 * Print one address with ending newline.
122 void println(uintptr_t address, const SymbolizedFrame& frame);
125 * Print multiple addresses on separate lines.
127 void println(const uintptr_t* addresses,
128 const SymbolizedFrame* frames,
132 * Print multiple addresses on separate lines, skipping the first
136 void println(const FrameArray<N>& fa, size_t skip=0) {
137 if (skip < fa.frameCount) {
138 println(fa.addresses + skip, fa.frames + skip, fa.frameCount - skip);
142 virtual ~SymbolizePrinter() { }
145 // Skip file and line information
146 NO_FILE_AND_LINE = 1 << 0,
148 // As terse as it gets: function name if found, address otherwise
153 explicit SymbolizePrinter(int options) : options_(options) { }
157 void printTerse(uintptr_t address, const SymbolizedFrame& frame);
158 virtual void doPrint(StringPiece sp) = 0;
162 * Print a list of symbolized addresses to a stream.
163 * Not reentrant. Do not use from signal handling code.
165 class OStreamSymbolizePrinter : public SymbolizePrinter {
167 explicit OStreamSymbolizePrinter(std::ostream& out, int options=0)
168 : SymbolizePrinter(options),
171 void doPrint(StringPiece sp) override;
176 * Print a list of symbolized addresses to a file descriptor.
177 * Ignores errors. Async-signal-safe.
179 class FDSymbolizePrinter : public SymbolizePrinter {
181 explicit FDSymbolizePrinter(int fd, int options=0)
182 : SymbolizePrinter(options),
185 void doPrint(StringPiece sp) override;
190 * Print a list of symbolized addresses to a FILE*.
191 * Ignores errors. Not reentrant. Do not use from signal handling code.
193 class FILESymbolizePrinter : public SymbolizePrinter {
195 explicit FILESymbolizePrinter(FILE* file, int options=0)
196 : SymbolizePrinter(options),
199 void doPrint(StringPiece sp) override;
204 * Print a list of symbolized addresses to a std::string.
205 * Not reentrant. Do not use from signal handling code.
207 class StringSymbolizePrinter : public SymbolizePrinter {
209 explicit StringSymbolizePrinter(int options=0) : SymbolizePrinter(options) { }
211 std::string str() const { return buf_.toStdString(); }
212 const fbstring& fbstr() const { return buf_; }
213 fbstring moveFbString() { return std::move(buf_); }
216 void doPrint(StringPiece sp) override;
220 } // namespace symbolizer
223 #endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */