Home | History | Annotate | Download | only in processor
      1 // Copyright (c) 2010 Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 //
     30 // basic_source_line_types.h: definition of nested classes/structs in
     31 // BasicSourceLineResolver.  It moves the definitions out of
     32 // basic_source_line_resolver.cc, so that other classes could have access
     33 // to these private nested types without including basic_source_line_resolver.cc
     34 //
     35 // Author: Siyang Xie (lambxsy (at) google.com)
     36 
     37 #ifndef PROCESSOR_BASIC_SOURCE_LINE_RESOLVER_TYPES_H__
     38 #define PROCESSOR_BASIC_SOURCE_LINE_RESOLVER_TYPES_H__
     39 
     40 #include <map>
     41 #include <string>
     42 
     43 #include "common/scoped_ptr.h"
     44 #include "google_breakpad/processor/basic_source_line_resolver.h"
     45 #include "processor/source_line_resolver_base_types.h"
     46 
     47 #include "processor/address_map-inl.h"
     48 #include "processor/range_map-inl.h"
     49 #include "processor/contained_range_map-inl.h"
     50 
     51 #include "processor/linked_ptr.h"
     52 #include "google_breakpad/processor/stack_frame.h"
     53 #include "processor/cfi_frame_info.h"
     54 #include "processor/windows_frame_info.h"
     55 
     56 namespace google_breakpad {
     57 
     58 struct
     59 BasicSourceLineResolver::Function : public SourceLineResolverBase::Function {
     60   Function(const string &function_name,
     61            MemAddr function_address,
     62            MemAddr code_size,
     63            int set_parameter_size) : Base(function_name,
     64                                           function_address,
     65                                           code_size,
     66                                           set_parameter_size),
     67                                      lines() { }
     68   RangeMap< MemAddr, linked_ptr<Line> > lines;
     69  private:
     70   typedef SourceLineResolverBase::Function Base;
     71 };
     72 
     73 
     74 class BasicSourceLineResolver::Module : public SourceLineResolverBase::Module {
     75  public:
     76   explicit Module(const string &name) : name_(name), is_corrupt_(false) { }
     77   virtual ~Module() { }
     78 
     79   // Loads a map from the given buffer in char* type.
     80   // Does NOT have ownership of memory_buffer.
     81   // The passed in |memory buffer| is of size |memory_buffer_size|.  If it is
     82   // not null terminated, LoadMapFromMemory() will null terminate it by
     83   // modifying the passed in buffer.
     84   virtual bool LoadMapFromMemory(char *memory_buffer,
     85                                  size_t memory_buffer_size);
     86 
     87   // Tells whether the loaded symbol data is corrupt.  Return value is
     88   // undefined, if the symbol data hasn't been loaded yet.
     89   virtual bool IsCorrupt() const { return is_corrupt_; }
     90 
     91   // Looks up the given relative address, and fills the StackFrame struct
     92   // with the result.
     93   virtual void LookupAddress(StackFrame *frame) const;
     94 
     95   // If Windows stack walking information is available covering ADDRESS,
     96   // return a WindowsFrameInfo structure describing it. If the information
     97   // is not available, returns NULL. A NULL return value does not indicate
     98   // an error. The caller takes ownership of any returned WindowsFrameInfo
     99   // object.
    100   virtual WindowsFrameInfo *FindWindowsFrameInfo(const StackFrame *frame) const;
    101 
    102   // If CFI stack walking information is available covering ADDRESS,
    103   // return a CFIFrameInfo structure describing it. If the information
    104   // is not available, return NULL. The caller takes ownership of any
    105   // returned CFIFrameInfo object.
    106   virtual CFIFrameInfo *FindCFIFrameInfo(const StackFrame *frame) const;
    107 
    108  private:
    109   // Friend declarations.
    110   friend class BasicSourceLineResolver;
    111   friend class ModuleComparer;
    112   friend class ModuleSerializer;
    113 
    114   typedef std::map<int, string> FileMap;
    115 
    116   // Logs parse errors.  |*num_errors| is increased every time LogParseError is
    117   // called.
    118   static void LogParseError(
    119       const string &message,
    120       int line_number,
    121       int *num_errors);
    122 
    123   // Parses a file declaration
    124   bool ParseFile(char *file_line);
    125 
    126   // Parses a function declaration, returning a new Function object.
    127   Function* ParseFunction(char *function_line);
    128 
    129   // Parses a line declaration, returning a new Line object.
    130   Line* ParseLine(char *line_line);
    131 
    132   // Parses a PUBLIC symbol declaration, storing it in public_symbols_.
    133   // Returns false if an error occurs.
    134   bool ParsePublicSymbol(char *public_line);
    135 
    136   // Parses a STACK WIN or STACK CFI frame info declaration, storing
    137   // it in the appropriate table.
    138   bool ParseStackInfo(char *stack_info_line);
    139 
    140   // Parses a STACK CFI record, storing it in cfi_frame_info_.
    141   bool ParseCFIFrameInfo(char *stack_info_line);
    142 
    143   string name_;
    144   FileMap files_;
    145   RangeMap< MemAddr, linked_ptr<Function> > functions_;
    146   AddressMap< MemAddr, linked_ptr<PublicSymbol> > public_symbols_;
    147   bool is_corrupt_;
    148 
    149   // Each element in the array is a ContainedRangeMap for a type
    150   // listed in WindowsFrameInfoTypes. These are split by type because
    151   // there may be overlaps between maps of different types, but some
    152   // information is only available as certain types.
    153   ContainedRangeMap< MemAddr, linked_ptr<WindowsFrameInfo> >
    154     windows_frame_info_[WindowsFrameInfo::STACK_INFO_LAST];
    155 
    156   // DWARF CFI stack walking data. The Module stores the initial rule sets
    157   // and rule deltas as strings, just as they appear in the symbol file:
    158   // although the file may contain hundreds of thousands of STACK CFI
    159   // records, walking a stack will only ever use a few of them, so it's
    160   // best to delay parsing a record until it's actually needed.
    161 
    162   // STACK CFI INIT records: for each range, an initial set of register
    163   // recovery rules. The RangeMap's itself gives the starting and ending
    164   // addresses.
    165   RangeMap<MemAddr, string> cfi_initial_rules_;
    166 
    167   // STACK CFI records: at a given address, the changes to the register
    168   // recovery rules that take effect at that address. The map key is the
    169   // starting address; the ending address is the key of the next entry in
    170   // this map, or the end of the range as given by the cfi_initial_rules_
    171   // entry (which FindCFIFrameInfo looks up first).
    172   std::map<MemAddr, string> cfi_delta_rules_;
    173 };
    174 
    175 }  // namespace google_breakpad
    176 
    177 #endif  // PROCESSOR_BASIC_SOURCE_LINE_RESOLVER_TYPES_H__
    178