michael@0: // -*- mode: C++ -*- michael@0: michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // cfi_frame_info.h: Define the CFIFrameInfo class, which holds the michael@0: // set of 'STACK CFI'-derived register recovery rules that apply at a michael@0: // given instruction. michael@0: michael@0: #ifndef PROCESSOR_CFI_FRAME_INFO_H_ michael@0: #define PROCESSOR_CFI_FRAME_INFO_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "common/using_std_string.h" michael@0: #include "common/unique_string.h" michael@0: #include "google_breakpad/common/breakpad_types.h" michael@0: #include "common/module.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using std::map; michael@0: michael@0: class MemoryRegion; michael@0: michael@0: // A set of rules for recovering the calling frame's registers' michael@0: // values, when the PC is at a given address in the current frame's michael@0: // function. See the description of 'STACK CFI' records at: michael@0: // michael@0: // http://code.google.com/p/google-breakpad/wiki/SymbolFiles michael@0: // michael@0: // To prepare an instance of CFIFrameInfo for use at a given michael@0: // instruction, first populate it with the rules from the 'STACK CFI michael@0: // INIT' record that covers that instruction, and then apply the michael@0: // changes given by the 'STACK CFI' records up to our instruction's michael@0: // address. Then, use the FindCallerRegs member function to apply the michael@0: // rules to the callee frame's register values, yielding the caller michael@0: // frame's register values. michael@0: class CFIFrameInfo { michael@0: public: michael@0: // A map from register names onto values. michael@0: template class RegisterValueMap: michael@0: public UniqueStringMap { }; michael@0: michael@0: // Set the expression for computing a call frame address, return michael@0: // address, or register's value. At least the CFA rule and the RA michael@0: // rule must be set before calling FindCallerRegs. michael@0: void SetCFARule(const Module::Expr& rule) { cfa_rule_ = rule; } michael@0: void SetRARule(const Module::Expr& rule) { ra_rule_ = rule; } michael@0: void SetRegisterRule(const UniqueString* register_name, michael@0: const Module::Expr& rule) { michael@0: register_rules_[register_name] = rule; michael@0: } michael@0: michael@0: // Compute the values of the calling frame's registers, according to michael@0: // this rule set. Use ValueType in expression evaluation; this michael@0: // should be uint32_t on machines with 32-bit addresses, or michael@0: // uint64_t on machines with 64-bit addresses. michael@0: // michael@0: // Return true on success, false otherwise. michael@0: // michael@0: // MEMORY provides access to the contents of the stack. REGISTERS is michael@0: // a dictionary mapping the names of registers whose values are michael@0: // known in the current frame to their values. CALLER_REGISTERS is michael@0: // populated with the values of the recoverable registers in the michael@0: // frame that called the current frame. michael@0: // michael@0: // In addition, CALLER_REGISTERS[".ra"] will be the return address, michael@0: // and CALLER_REGISTERS[".cfa"] will be the call frame address. michael@0: // These may be helpful in computing the caller's PC and stack michael@0: // pointer, if their values are not explicitly specified. michael@0: template michael@0: bool FindCallerRegs(const RegisterValueMap ®isters, michael@0: const MemoryRegion &memory, michael@0: RegisterValueMap *caller_registers) const; michael@0: michael@0: // Serialize the rules in this object into a string in the format michael@0: // of STACK CFI records. michael@0: string Serialize() const; michael@0: michael@0: private: michael@0: michael@0: // A map from register names onto evaluation rules. michael@0: typedef map RuleMap; michael@0: michael@0: // An expression for computing the current frame's CFA (call michael@0: // frame address). The CFA is a reference address for the frame that michael@0: // remains unchanged throughout the frame's lifetime. You should michael@0: // evaluate this expression with a dictionary initially populated michael@0: // with the values of the current frame's known registers. michael@0: Module::Expr cfa_rule_; michael@0: michael@0: // The following expressions should be evaluated with a dictionary michael@0: // initially populated with the values of the current frame's known michael@0: // registers, and with ".cfa" set to the result of evaluating the michael@0: // cfa_rule expression, above. michael@0: michael@0: // An expression for computing the current frame's return address. michael@0: Module::Expr ra_rule_; michael@0: michael@0: // For a register named REG, rules[REG] is a postfix expression michael@0: // which leaves the value of REG in the calling frame on the top of michael@0: // the stack. You should evaluate this expression michael@0: RuleMap register_rules_; michael@0: }; michael@0: michael@0: // A parser for STACK CFI-style rule sets. michael@0: // This may seem bureaucratic: there's no legitimate run-time reason michael@0: // to use a parser/handler pattern for this, as it's not a likely michael@0: // reuse boundary. But doing so makes finer-grained unit testing michael@0: // possible. michael@0: class CFIRuleParser { michael@0: public: michael@0: michael@0: class Handler { michael@0: public: michael@0: Handler() { } michael@0: virtual ~Handler() { } michael@0: michael@0: // The input specifies EXPRESSION as the CFA/RA computation rule. michael@0: virtual void CFARule(const string &expression) = 0; michael@0: virtual void RARule(const string &expression) = 0; michael@0: michael@0: // The input specifies EXPRESSION as the recovery rule for register NAME. michael@0: virtual void RegisterRule(const UniqueString* name, michael@0: const string &expression) = 0; michael@0: }; michael@0: michael@0: // Construct a parser which feeds its results to HANDLER. michael@0: CFIRuleParser(Handler *handler) : handler_(handler) { } michael@0: michael@0: // Parse RULE_SET as a set of CFA computation and RA/register michael@0: // recovery rules, as appearing in STACK CFI records. Report the michael@0: // results of parsing by making the appropriate calls to handler_. michael@0: // Return true if parsing was successful, false otherwise. michael@0: bool Parse(const string &rule_set); michael@0: michael@0: private: michael@0: // Report any accumulated rule to handler_ michael@0: bool Report(); michael@0: michael@0: // The handler to which the parser reports its findings. michael@0: Handler *handler_; michael@0: michael@0: // Working data. michael@0: const UniqueString* name_; michael@0: string expression_; michael@0: }; michael@0: michael@0: // A handler for rule set parsing that populates a CFIFrameInfo with michael@0: // the results. michael@0: class CFIFrameInfoParseHandler: public CFIRuleParser::Handler { michael@0: public: michael@0: // Populate FRAME_INFO with the results of parsing. michael@0: CFIFrameInfoParseHandler(CFIFrameInfo *frame_info) michael@0: : frame_info_(frame_info) { } michael@0: michael@0: void CFARule(const string &expression); michael@0: void RARule(const string &expression); michael@0: void RegisterRule(const UniqueString* name, const string &expression); michael@0: michael@0: private: michael@0: CFIFrameInfo *frame_info_; michael@0: }; michael@0: michael@0: // A utility class template for simple 'STACK CFI'-driven stack walkers. michael@0: // Given a CFIFrameInfo instance, a table describing the architecture's michael@0: // register set, and a context holding the last frame's registers, an michael@0: // instance of this class can populate a new context with the caller's michael@0: // registers. michael@0: // michael@0: // This class template doesn't use any internal knowledge of CFIFrameInfo michael@0: // or the other stack walking structures; it just uses the public interface michael@0: // of CFIFrameInfo to do the usual things. But the logic it handles should michael@0: // be common to many different architectures' stack walkers, so wrapping it michael@0: // up in a class should allow the walkers to share code. michael@0: // michael@0: // RegisterType should be the type of this architecture's registers, either michael@0: // uint32_t or uint64_t. RawContextType should be the raw context michael@0: // structure type for this architecture. michael@0: template michael@0: class SimpleCFIWalker { michael@0: public: michael@0: // A structure describing one architecture register. michael@0: struct RegisterSet { michael@0: // The register name, as it appears in STACK CFI rules. michael@0: const UniqueString* name; michael@0: michael@0: // An alternate name that the register's value might be found michael@0: // under in a register value dictionary, or NULL. When generating michael@0: // names, prefer NAME to this value. It's common to list ".cfa" as michael@0: // an alternative name for the stack pointer, and ".ra" as an michael@0: // alternative name for the instruction pointer. michael@0: const UniqueString* alternate_name; michael@0: michael@0: // True if the callee is expected to preserve the value of this michael@0: // register. If this flag is true for some register R, and the STACK michael@0: // CFI records provide no rule to recover R, then SimpleCFIWalker michael@0: // assumes that the callee has not changed R's value, and the caller's michael@0: // value for R is that currently in the callee's context. michael@0: bool callee_saves; michael@0: michael@0: // The ContextValidity flag representing the register's presence. michael@0: int validity_flag; michael@0: michael@0: // A pointer to the RawContextType member that holds the michael@0: // register's value. michael@0: RegisterType RawContextType::*context_member; michael@0: }; michael@0: michael@0: // Create a simple CFI-based frame walker, given a description of the michael@0: // architecture's register set. REGISTER_MAP is an array of michael@0: // RegisterSet structures; MAP_SIZE is the number of elements in the michael@0: // array. michael@0: SimpleCFIWalker(const RegisterSet *register_map, size_t map_size) michael@0: : register_map_(register_map), map_size_(map_size) { } michael@0: michael@0: // Compute the calling frame's raw context given the callee's raw michael@0: // context. michael@0: // michael@0: // Given: michael@0: // michael@0: // - MEMORY, holding the stack's contents, michael@0: // - CFI_FRAME_INFO, describing the called function, michael@0: // - CALLEE_CONTEXT, holding the called frame's registers, and michael@0: // - CALLEE_VALIDITY, indicating which registers in CALLEE_CONTEXT are valid, michael@0: // michael@0: // fill in CALLER_CONTEXT with the caller's register values, and set michael@0: // CALLER_VALIDITY to indicate which registers are valid in michael@0: // CALLER_CONTEXT. Return true on success, or false on failure. michael@0: bool FindCallerRegisters(const MemoryRegion &memory, michael@0: const CFIFrameInfo &cfi_frame_info, michael@0: const RawContextType &callee_context, michael@0: int callee_validity, michael@0: RawContextType *caller_context, michael@0: int *caller_validity) const; michael@0: michael@0: private: michael@0: const RegisterSet *register_map_; michael@0: size_t map_size_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #include "cfi_frame_info-inl.h" michael@0: michael@0: #endif // PROCESSOR_CFI_FRAME_INFO_H_