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/Range.h"
26 #include "folly/experimental/symbolizer/Elf.h"
27 #include "folly/experimental/symbolizer/Dwarf.h"
30 namespace symbolizer {
33 * Address information: symbol name and location.
35 * Note that both name and location are references in the Symbolizer object,
36 * which must outlive this AddressInfo object.
39 /* implicit */ AddressInfo(uintptr_t a=0, bool sf=false)
47 Dwarf::LocationInfo location;
51 * Get the current stack trace into addresses, which has room for at least
52 * maxAddresses entries. Skip the first (topmost) skip entries.
53 * Returns the number of entries in addresses on success, -1 on failure.
55 ssize_t getStackTrace(AddressInfo* addresses,
61 Symbolizer() : fileCount_(0) { }
64 * Symbolize given addresses.
66 void symbolize(AddressInfo* addresses, size_t addressCount);
69 * Shortcut to symbolize one address.
71 bool symbolize(AddressInfo& address) {
72 symbolize(&address, 1);
77 // We can't allocate memory, so we'll preallocate room.
78 // "1023 shared libraries should be enough for everyone"
79 static constexpr size_t kMaxFiles = 1024;
81 ElfFile files_[kMaxFiles];
85 * Print a list of symbolized addresses. Base class.
87 class SymbolizePrinter {
89 void print(const AddressInfo& ainfo);
90 void print(const AddressInfo* addresses, size_t addressCount);
92 virtual ~SymbolizePrinter() { }
94 virtual void doPrint(StringPiece sp) = 0;
98 * Print a list of symbolized addresses to a stream.
99 * Not reentrant. Do not use from signal handling code.
101 class OStreamSymbolizePrinter : public SymbolizePrinter {
103 explicit OStreamSymbolizePrinter(std::ostream& out) : out_(out) { }
105 void doPrint(StringPiece sp) override;
110 * Print a list of symbolized addresses to a file descriptor.
111 * Ignores errors. Async-signal-safe.
113 class FDSymbolizePrinter : public SymbolizePrinter {
115 explicit FDSymbolizePrinter(int fd) : fd_(fd) { }
117 void doPrint(StringPiece sp) override;
122 * Print an AddressInfo to a stream. Note that the Symbolizer that
123 * symbolized the address must outlive the AddressInfo. Just like
124 * OStreamSymbolizePrinter (which it uses internally), this is not
125 * reentrant; do not use from signal handling code.
127 std::ostream& operator<<(std::ostream& out, const AddressInfo& ainfo);
129 } // namespace symbolizer
132 #endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */