1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/dwarf_line_to_module.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,188 @@ 1.4 +// -*- mode: c++ -*- 1.5 + 1.6 +// Copyright (c) 2010 Google Inc. 1.7 +// All rights reserved. 1.8 +// 1.9 +// Redistribution and use in source and binary forms, with or without 1.10 +// modification, are permitted provided that the following conditions are 1.11 +// met: 1.12 +// 1.13 +// * Redistributions of source code must retain the above copyright 1.14 +// notice, this list of conditions and the following disclaimer. 1.15 +// * Redistributions in binary form must reproduce the above 1.16 +// copyright notice, this list of conditions and the following disclaimer 1.17 +// in the documentation and/or other materials provided with the 1.18 +// distribution. 1.19 +// * Neither the name of Google Inc. nor the names of its 1.20 +// contributors may be used to endorse or promote products derived from 1.21 +// this software without specific prior written permission. 1.22 +// 1.23 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + 1.35 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> 1.36 + 1.37 +// The DwarfLineToModule class accepts line number information from a 1.38 +// DWARF parser and adds it to a google_breakpad::Module. The Module 1.39 +// can write that data out as a Breakpad symbol file. 1.40 + 1.41 +#ifndef COMMON_LINUX_DWARF_LINE_TO_MODULE_H 1.42 +#define COMMON_LINUX_DWARF_LINE_TO_MODULE_H 1.43 + 1.44 +#include <string> 1.45 + 1.46 +#include "common/module.h" 1.47 +#include "common/dwarf/dwarf2reader.h" 1.48 +#include "common/using_std_string.h" 1.49 + 1.50 +namespace google_breakpad { 1.51 + 1.52 +// A class for producing a vector of google_breakpad::Module::Line 1.53 +// instances from parsed DWARF line number data. 1.54 +// 1.55 +// An instance of this class can be provided as a handler to a 1.56 +// dwarf2reader::LineInfo DWARF line number information parser. The 1.57 +// handler accepts source location information from the parser and 1.58 +// uses it to produce a vector of google_breakpad::Module::Line 1.59 +// objects, referring to google_breakpad::Module::File objects added 1.60 +// to a particular google_breakpad::Module. 1.61 +// 1.62 +// GNU toolchain omitted sections support: 1.63 +// ====================================== 1.64 +// 1.65 +// Given the right options, the GNU toolchain will omit unreferenced 1.66 +// functions from the final executable. Unfortunately, when it does so, it 1.67 +// does not remove the associated portions of the DWARF line number 1.68 +// program; instead, it gives the DW_LNE_set_address instructions referring 1.69 +// to the now-deleted code addresses of zero. Given this input, the DWARF 1.70 +// line parser will call AddLine with a series of lines starting at address 1.71 +// zero. For example, here is the output from 'readelf -wl' for a program 1.72 +// with four functions, the first three of which have been omitted: 1.73 +// 1.74 +// Line Number Statements: 1.75 +// Extended opcode 2: set Address to 0x0 1.76 +// Advance Line by 14 to 15 1.77 +// Copy 1.78 +// Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 16 1.79 +// Special opcode 119: advance Address by 8 to 0xb and Line by 2 to 18 1.80 +// Advance PC by 2 to 0xd 1.81 +// Extended opcode 1: End of Sequence 1.82 +// 1.83 +// Extended opcode 2: set Address to 0x0 1.84 +// Advance Line by 14 to 15 1.85 +// Copy 1.86 +// Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 16 1.87 +// Special opcode 119: advance Address by 8 to 0xb and Line by 2 to 18 1.88 +// Advance PC by 2 to 0xd 1.89 +// Extended opcode 1: End of Sequence 1.90 +// 1.91 +// Extended opcode 2: set Address to 0x0 1.92 +// Advance Line by 19 to 20 1.93 +// Copy 1.94 +// Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 21 1.95 +// Special opcode 76: advance Address by 5 to 0x8 and Line by 1 to 22 1.96 +// Advance PC by 2 to 0xa 1.97 +// Extended opcode 1: End of Sequence 1.98 +// 1.99 +// Extended opcode 2: set Address to 0x80483a4 1.100 +// Advance Line by 23 to 24 1.101 +// Copy 1.102 +// Special opcode 202: advance Address by 14 to 0x80483b2 and Line by 1 to 25 1.103 +// Special opcode 76: advance Address by 5 to 0x80483b7 and Line by 1 to 26 1.104 +// Advance PC by 6 to 0x80483bd 1.105 +// Extended opcode 1: End of Sequence 1.106 +// 1.107 +// Instead of collecting runs of lines describing code that is not there, 1.108 +// we try to recognize and drop them. Since the linker doesn't explicitly 1.109 +// distinguish references to dropped sections from genuine references to 1.110 +// code at address zero, we must use a heuristic. We have chosen: 1.111 +// 1.112 +// - If a line starts at address zero, omit it. (On the platforms 1.113 +// breakpad targets, it is extremely unlikely that there will be code 1.114 +// at address zero.) 1.115 +// 1.116 +// - If a line starts immediately after an omitted line, omit it too. 1.117 +class DwarfLineToModule: public dwarf2reader::LineInfoHandler { 1.118 + public: 1.119 + // As the DWARF line info parser passes us line records, add source 1.120 + // files to MODULE, and add all lines to the end of LINES. LINES 1.121 + // need not be empty. If the parser hands us a zero-length line, we 1.122 + // omit it. If the parser hands us a line that extends beyond the 1.123 + // end of the address space, we clip it. It's up to our client to 1.124 + // sort out which lines belong to which functions; we don't add them 1.125 + // to any particular function in MODULE ourselves. 1.126 + DwarfLineToModule(Module *module, const string& compilation_dir, 1.127 + vector<Module::Line> *lines) 1.128 + : module_(module), 1.129 + compilation_dir_(compilation_dir), 1.130 + lines_(lines), 1.131 + highest_file_number_(-1), 1.132 + omitted_line_end_(0), 1.133 + warned_bad_file_number_(false), 1.134 + warned_bad_directory_number_(false) { } 1.135 + 1.136 + ~DwarfLineToModule() { } 1.137 + 1.138 + void DefineDir(const string &name, uint32 dir_num); 1.139 + void DefineFile(const string &name, int32 file_num, 1.140 + uint32 dir_num, uint64 mod_time, 1.141 + uint64 length); 1.142 + void AddLine(uint64 address, uint64 length, 1.143 + uint32 file_num, uint32 line_num, uint32 column_num); 1.144 + 1.145 + private: 1.146 + 1.147 + typedef std::map<uint32, string> DirectoryTable; 1.148 + typedef std::map<uint32, Module::File *> FileTable; 1.149 + 1.150 + // The module we're contributing debugging info to. Owned by our 1.151 + // client. 1.152 + Module *module_; 1.153 + 1.154 + // The compilation directory for the current compilation unit whose 1.155 + // lines are being accumulated. 1.156 + string compilation_dir_; 1.157 + 1.158 + // The vector of lines we're accumulating. Owned by our client. 1.159 + // 1.160 + // In a Module, as in a breakpad symbol file, lines belong to 1.161 + // specific functions, but DWARF simply assigns lines to addresses; 1.162 + // one must infer the line/function relationship using the 1.163 + // functions' beginning and ending addresses. So we can't add these 1.164 + // to the appropriate function from module_ until we've read the 1.165 + // function info as well. Instead, we accumulate lines here, and let 1.166 + // whoever constructed this sort it all out. 1.167 + vector<Module::Line> *lines_; 1.168 + 1.169 + // A table mapping directory numbers to paths. 1.170 + DirectoryTable directories_; 1.171 + 1.172 + // A table mapping file numbers to Module::File pointers. 1.173 + FileTable files_; 1.174 + 1.175 + // The highest file number we've seen so far, or -1 if we've seen 1.176 + // none. Used for dynamically defined file numbers. 1.177 + int32 highest_file_number_; 1.178 + 1.179 + // This is the ending address of the last line we omitted, or zero if we 1.180 + // didn't omit the previous line. It is zero before we have received any 1.181 + // AddLine calls. 1.182 + uint64 omitted_line_end_; 1.183 + 1.184 + // True if we've warned about: 1.185 + bool warned_bad_file_number_; // bad file numbers 1.186 + bool warned_bad_directory_number_; // bad directory numbers 1.187 +}; 1.188 + 1.189 +} // namespace google_breakpad 1.190 + 1.191 +#endif // COMMON_LINUX_DWARF_LINE_TO_MODULE_H