2 * Copyright 2016 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 // DWARF record parser
21 #include <boost/variant.hpp>
23 #include <folly/experimental/symbolizer/Elf.h>
24 #include <folly/Range.h>
27 namespace symbolizer {
30 * DWARF record parser.
32 * We only implement enough DWARF functionality to convert from PC address
33 * to file and line number information.
35 * This means (although they're not part of the public API of this class), we
36 * can parse Debug Information Entries (DIEs), abbreviations, attributes (of
37 * all forms), and we can interpret bytecode for the line number VM.
39 * We can interpret DWARF records of version 2, 3, or 4, although we don't
40 * actually support many of the version 4 features (such as VLIW, multiple
41 * operations per instruction)
43 * Note that the DWARF record parser does not allocate heap memory at all.
44 * This is on purpose: you can use the parser from
45 * memory-constrained situations (such as an exception handler for
46 * std::out_of_memory) If it weren't for this requirement, some things would
47 * be much simpler: the Path class would be unnecessary and would be replaced
48 * with a std::string; the list of file names in the line number VM would be
49 * kept as a vector of strings instead of re-executing the program to look for
50 * DW_LNE_define_file instructions, etc.
53 // Note that Dwarf uses (and returns) StringPiece a lot.
54 // The StringPieces point within sections in the ELF file, and so will
55 // be live for as long as the passed-in ElfFile is live.
57 /** Create a DWARF parser around an ELF file. */
58 explicit Dwarf(const ElfFile* elf);
61 * Represent a file path a s collection of three parts (base directory,
62 * subdirectory, and file).
68 Path(folly::StringPiece baseDir, folly::StringPiece subDir,
69 folly::StringPiece file);
71 folly::StringPiece baseDir() const { return baseDir_; };
72 folly::StringPiece subDir() const { return subDir_; }
73 folly::StringPiece file() const { return file_; }
78 * Copy the Path to a buffer of size bufSize.
80 * toBuffer behaves like snprintf: It will always null-terminate the
81 * buffer (so it will copy at most bufSize-1 bytes), and it will return
82 * the number of bytes that would have been written if there had been
83 * enough room, so, if toBuffer returns a value >= bufSize, the output
86 size_t toBuffer(char* buf, size_t bufSize) const;
88 void toString(std::string& dest) const;
89 std::string toString() const {
95 // TODO(tudorb): Implement operator==, operator!=; not as easy as it
96 // seems as the same path can be represented in multiple ways
98 folly::StringPiece baseDir_;
99 folly::StringPiece subDir_;
100 folly::StringPiece file_;
103 enum class LocationInfoMode {
104 // Don't resolve location info.
106 // Perform CU lookup using .debug_aranges (might be incomplete).
108 // Scan all CU in .debug_info (slow!) on .debug_aranges lookup failure.
112 struct LocationInfo {
113 bool hasMainFile = false;
116 bool hasFileAndLine = false;
122 * Find the file and line number information corresponding to address.
124 bool findAddress(uintptr_t address,
126 LocationInfoMode mode) const;
129 static bool findDebugInfoOffset(uintptr_t address,
134 bool findLocation(uintptr_t address,
135 StringPiece& infoEntry,
136 LocationInfo& info) const;
140 // DWARF section made up of chunks, each prefixed with a length header.
141 // The length indicates whether the chunk is DWARF-32 or DWARF-64, which
142 // guides interpretation of "section offset" records.
143 // (yes, DWARF-32 and DWARF-64 sections may coexist in the same file)
146 Section() : is64Bit_(false) { }
148 explicit Section(folly::StringPiece d);
150 // Return next chunk, if any; the 4- or 12-byte length was already
151 // parsed and isn't part of the chunk.
152 bool next(folly::StringPiece& chunk);
154 // Is the current chunk 64 bit?
155 bool is64Bit() const { return is64Bit_; }
158 // Yes, 32- and 64- bit sections may coexist. Yikes!
160 folly::StringPiece data_;
163 // Abbreviation for a Debugging Information Entry.
164 struct DIEAbbreviation {
174 folly::StringPiece attributes;
177 // Interpreter for the line number bytecode VM
180 LineNumberVM(folly::StringPiece data,
181 folly::StringPiece compilationDirectory);
183 bool findAddress(uintptr_t address, Path& file, uint64_t& line);
189 // Execute until we commit one new row to the line number matrix
190 bool next(folly::StringPiece& program);
192 CONTINUE, // Continue feeding opcodes
193 COMMIT, // Commit new <address, file, line> tuple
194 END, // End of sequence
196 // Execute one opcode
197 StepResult step(folly::StringPiece& program);
200 folly::StringPiece relativeName;
201 // 0 = current compilation directory
202 // otherwise, 1-based index in the list of include directories
203 uint64_t directoryIndex;
205 // Read one FileName object, advance sp
206 static bool readFileName(folly::StringPiece& sp, FileName& fn);
208 // Get file name at given index; may be in the initial table
209 // (fileNames_) or defined using DW_LNE_define_file (and we reexecute
210 // enough of the program to find it, if so)
211 FileName getFileName(uint64_t index) const;
213 // Get include directory at given index
214 folly::StringPiece getIncludeDirectory(uint64_t index) const;
216 // Execute opcodes until finding a DW_LNE_define_file and return true;
217 // return file at the end.
218 bool nextDefineFile(folly::StringPiece& program, FileName& fn) const;
222 folly::StringPiece data_;
223 folly::StringPiece compilationDirectory_;
232 const uint8_t* standardOpcodeLengths_;
234 folly::StringPiece includeDirectories_;
235 size_t includeDirectoryCount_;
237 folly::StringPiece fileNames_;
238 size_t fileNameCount_;
240 // State machine registers
251 uint64_t discriminator_;
254 // Read an abbreviation from a StringPiece, return true if at end; advance sp
255 static bool readAbbreviation(folly::StringPiece& sp, DIEAbbreviation& abbr);
257 // Get abbreviation corresponding to a code, in the chunk starting at
258 // offset in the .debug_abbrev section
259 DIEAbbreviation getAbbreviation(uint64_t code, uint64_t offset) const;
261 // Read one attribute <name, form> pair, advance sp; returns <0, 0> at end.
262 static DIEAbbreviation::Attribute readAttribute(folly::StringPiece& sp);
264 // Read one attribute value, advance sp
265 typedef boost::variant<uint64_t, folly::StringPiece> AttributeValue;
266 AttributeValue readAttributeValue(
267 folly::StringPiece& sp,
271 // Get an ELF section by name, return true if found
272 bool getSection(const char* name, folly::StringPiece* section) const;
274 // Get a string from the .debug_str section
275 folly::StringPiece getStringFromStringSection(uint64_t offset) const;
277 folly::StringPiece info_; // .debug_info
278 folly::StringPiece abbrev_; // .debug_abbrev
279 folly::StringPiece aranges_; // .debug_aranges
280 folly::StringPiece line_; // .debug_line
281 folly::StringPiece strings_; // .debug_str
284 inline std::ostream& operator<<(std::ostream& out, const Dwarf::Path& path) {
285 return out << path.toString();
288 } // namespace symbolizer