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