1 // -*- mode: C++ -*- 2 3 // Copyright 2010 Google LLC 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google LLC nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // Original author: Jim Blandy <[email protected]> <[email protected]> 32 33 // dump_stabs.h: Define the StabsToModule class, which receives 34 // STABS debugging information from a parser and adds it to a Breakpad 35 // symbol file. 36 37 #ifndef BREAKPAD_COMMON_STABS_TO_MODULE_H_ 38 #define BREAKPAD_COMMON_STABS_TO_MODULE_H_ 39 40 #include <stdint.h> 41 42 #include <string> 43 #include <vector> 44 45 #include "common/module.h" 46 #include "common/stabs_reader.h" 47 #include "common/using_std_string.h" 48 49 namespace google_breakpad { 50 51 using std::vector; 52 53 // A StabsToModule is a handler that receives parsed STABS debugging 54 // information from a StabsReader, and uses that to populate 55 // a Module. (All classes are in the google_breakpad namespace.) A 56 // Module represents the contents of a Breakpad symbol file, and knows 57 // how to write itself out as such. A StabsToModule thus acts as 58 // the bridge between STABS and Breakpad data. 59 // When processing Darwin Mach-O files, this also receives public linker 60 // symbols, like those found in system libraries. 61 class StabsToModule: public google_breakpad::StabsHandler { 62 public: 63 // Receive parsed debugging information from a StabsReader, and 64 // store it all in MODULE. StabsToModule(Module * module)65 StabsToModule(Module *module) : 66 module_(module), 67 in_compilation_unit_(false), 68 comp_unit_base_address_(0), 69 current_function_(NULL), 70 current_source_file_(NULL), 71 current_source_file_name_(NULL) { } 72 ~StabsToModule(); 73 74 // The standard StabsHandler virtual member functions. 75 bool StartCompilationUnit(const char *name, uint64_t address, 76 const char *build_directory); 77 bool EndCompilationUnit(uint64_t address); 78 bool StartFunction(const string& name, uint64_t address); 79 bool EndFunction(uint64_t address); 80 bool Line(uint64_t address, const char *name, int number); 81 bool Extern(const string& name, uint64_t address); 82 void Warning(const char *format, ...); 83 84 // Do any final processing necessary to make module_ contain all the 85 // data provided by the STABS reader. 86 // 87 // Because STABS does not provide reliable size information for 88 // functions and lines, we need to make a pass over the data after 89 // processing all the STABS to compute those sizes. We take care of 90 // that here. 91 void Finalize(); 92 93 private: 94 95 // An arbitrary, but very large, size to use for functions whose 96 // size we can't compute properly. 97 static const uint64_t kFallbackSize = 0x10000000; 98 99 // The module we're contributing debugging info to. 100 Module *module_; 101 102 // The functions we've generated so far. We don't add these to 103 // module_ as we parse them. Instead, we wait until we've computed 104 // their ending address, and their lines' ending addresses. 105 // 106 // We could just stick them in module_ from the outset, but if 107 // module_ already contains data gathered from other debugging 108 // formats, that would complicate the size computation. 109 vector<Module::Function*> functions_; 110 111 // Boundary addresses. STABS doesn't necessarily supply sizes for 112 // functions and lines, so we need to compute them ourselves by 113 // finding the next object. 114 vector<Module::Address> boundaries_; 115 116 // True if we are currently within a compilation unit: we have gotten a 117 // StartCompilationUnit call, but no matching EndCompilationUnit call 118 // yet. We use this for sanity checks. 119 bool in_compilation_unit_; 120 121 // The base address of the current compilation unit. We use this to 122 // recognize functions we should omit from the symbol file. (If you 123 // know the details of why we omit these, please patch this 124 // comment.) 125 Module::Address comp_unit_base_address_; 126 127 // The function we're currently contributing lines to. 128 Module::Function *current_function_; 129 130 // The last Module::File we got a line number in. 131 Module::File *current_source_file_; 132 133 // The pointer in the .stabstr section of the name that 134 // current_source_file_ is built from. This allows us to quickly 135 // recognize when the current line is in the same file as the 136 // previous one (which it usually is). 137 const char *current_source_file_name_; 138 }; 139 140 } // namespace google_breakpad 141 142 #endif // BREAKPAD_COMMON_STABS_TO_MODULE_H_ 143