1 //===-- UnwindLLDB.h --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef lldb_UnwindLLDB_h_ 11 #define lldb_UnwindLLDB_h_ 12 13 #include <vector> 14 15 #include "lldb/Symbol/FuncUnwinders.h" 16 #include "lldb/Symbol/SymbolContext.h" 17 #include "lldb/Symbol/UnwindPlan.h" 18 #include "lldb/Target/RegisterContext.h" 19 #include "lldb/Target/Unwind.h" 20 #include "lldb/Utility/ConstString.h" 21 #include "lldb/lldb-public.h" 22 23 namespace lldb_private { 24 25 class RegisterContextLLDB; 26 27 class UnwindLLDB : public lldb_private::Unwind { 28 public: 29 UnwindLLDB(lldb_private::Thread &thread); 30 31 ~UnwindLLDB() override = default; 32 33 enum RegisterSearchResult { 34 eRegisterFound = 0, 35 eRegisterNotFound, 36 eRegisterIsVolatile 37 }; 38 39 protected: 40 friend class lldb_private::RegisterContextLLDB; 41 42 struct RegisterLocation { 43 enum RegisterLocationTypes { 44 eRegisterNotSaved = 0, // register was not preserved by callee. If 45 // volatile reg, is unavailable 46 eRegisterSavedAtMemoryLocation, // register is saved at a specific word of 47 // target mem (target_memory_location) 48 eRegisterInRegister, // register is available in a (possible other) 49 // register (register_number) 50 eRegisterSavedAtHostMemoryLocation, // register is saved at a word in 51 // lldb's address space 52 eRegisterValueInferred, // register val was computed (and is in 53 // inferred_value) 54 eRegisterInLiveRegisterContext // register value is in a live (stack frame 55 // #0) register 56 }; 57 int type; 58 union { 59 lldb::addr_t target_memory_location; 60 uint32_t 61 register_number; // in eRegisterKindLLDB register numbering system 62 void *host_memory_location; 63 uint64_t inferred_value; // eRegisterValueInferred - e.g. stack pointer == 64 // cfa + offset 65 } location; 66 }; 67 DoClear()68 void DoClear() override { 69 m_frames.clear(); 70 m_candidate_frame.reset(); 71 m_unwind_complete = false; 72 } 73 74 uint32_t DoGetFrameCount() override; 75 76 bool DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa, 77 lldb::addr_t &start_pc) override; 78 79 lldb::RegisterContextSP 80 DoCreateRegisterContextForFrame(lldb_private::StackFrame *frame) override; 81 82 typedef std::shared_ptr<RegisterContextLLDB> RegisterContextLLDBSP; 83 84 // Needed to retrieve the "next" frame (e.g. frame 2 needs to retrieve frame 85 // 1's RegisterContextLLDB) 86 // The RegisterContext for frame_num must already exist or this returns an 87 // empty shared pointer. 88 RegisterContextLLDBSP GetRegisterContextForFrameNum(uint32_t frame_num); 89 90 // Iterate over the RegisterContextLLDB's in our m_frames vector, look for the 91 // first one that 92 // has a saved location for this reg. 93 bool SearchForSavedLocationForRegister( 94 uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc, 95 uint32_t starting_frame_num, bool pc_register); 96 97 //------------------------------------------------------------------ 98 /// Provide the list of user-specified trap handler functions 99 /// 100 /// The Platform is one source of trap handler function names; that 101 /// may be augmented via a setting. The setting needs to be converted 102 /// into an array of ConstStrings before it can be used - we only want 103 /// to do that once per thread so it's here in the UnwindLLDB object. 104 /// 105 /// @return 106 /// Vector of ConstStrings of trap handler function names. May be 107 /// empty. 108 //------------------------------------------------------------------ GetUserSpecifiedTrapHandlerFunctionNames()109 const std::vector<ConstString> &GetUserSpecifiedTrapHandlerFunctionNames() { 110 return m_user_supplied_trap_handler_functions; 111 } 112 113 private: 114 struct Cursor { 115 lldb::addr_t start_pc; // The start address of the function/symbol for this 116 // frame - current pc if unknown 117 lldb::addr_t cfa; // The canonical frame address for this stack frame 118 lldb_private::SymbolContext sctx; // A symbol context we'll contribute to & 119 // provide to the StackFrame creation 120 RegisterContextLLDBSP 121 reg_ctx_lldb_sp; // These are all RegisterContextLLDB's 122 CursorCursor123 Cursor() 124 : start_pc(LLDB_INVALID_ADDRESS), cfa(LLDB_INVALID_ADDRESS), sctx(), 125 reg_ctx_lldb_sp() {} 126 127 private: 128 DISALLOW_COPY_AND_ASSIGN(Cursor); 129 }; 130 131 typedef std::shared_ptr<Cursor> CursorSP; 132 std::vector<CursorSP> m_frames; 133 CursorSP m_candidate_frame; 134 bool m_unwind_complete; // If this is true, we've enumerated all the frames in 135 // the stack, and m_frames.size() is the 136 // number of frames, etc. Otherwise we've only gone as far as directly asked, 137 // and m_frames.size() 138 // is how far we've currently gone. 139 140 std::vector<ConstString> m_user_supplied_trap_handler_functions; 141 142 //----------------------------------------------------------------- 143 // Check if Full UnwindPlan of First frame is valid or not. 144 // If not then try Fallback UnwindPlan of the frame. If Fallback 145 // UnwindPlan succeeds then update the Full UnwindPlan with the 146 // Fallback UnwindPlan. 147 //----------------------------------------------------------------- 148 void UpdateUnwindPlanForFirstFrameIfInvalid(ABI *abi); 149 150 CursorSP GetOneMoreFrame(ABI *abi); 151 152 bool AddOneMoreFrame(ABI *abi); 153 154 bool AddFirstFrame(); 155 156 //------------------------------------------------------------------ 157 // For UnwindLLDB only 158 //------------------------------------------------------------------ 159 DISALLOW_COPY_AND_ASSIGN(UnwindLLDB); 160 }; 161 162 } // namespace lldb_private 163 164 #endif // lldb_UnwindLLDB_h_ 165