1 #ifndef liblldb_UnwindPlan_h
2 #define liblldb_UnwindPlan_h
4 #include "lldb/lldb-private.h"
5 #include "lldb/Core/AddressRange.h"
6 #include "lldb/Core/Stream.h"
7 #include "lldb/Core/ConstString.h"
12 namespace lldb_private {
14 // The UnwindPlan object specifies how to unwind out of a function - where
15 // this function saves the caller's register values before modifying them
16 // (for non-volatile aka saved registers) and how to find this frame's
17 // Canonical Frame Address (CFA).
19 // Most commonly, registers are saved on the stack, offset some bytes from
20 // the Canonical Frame Address, or CFA, which is the starting address of
21 // this function's stack frame (the CFA is same as the eh_frame's CFA,
22 // whatever that may be on a given architecture).
23 // The CFA address for the stack frame does not change during
24 // the lifetime of the function.
26 // Internally, the UnwindPlan is structured as a vector of register locations
27 // organized by code address in the function, showing which registers have been
28 // saved at that point and where they are saved.
29 // It can be thought of as the expanded table form of the DWARF CFI
30 // encoded information.
32 // Other unwind information sources will be converted into UnwindPlans before
33 // being added to a FuncUnwinders object. The unwind source may be
34 // an eh_frame FDE, a DWARF debug_frame FDE, or assembly language based
36 // The UnwindPlan is the canonical form of this information that the unwinder
37 // code will use when walking the stack.
44 class RegisterLocation
50 unspecified, // not specified, we may be able to assume this
51 // is the same register. gcc doesn't specify all
52 // initial values so we really don't know...
53 undefined, // reg is not available, e.g. volatile reg
54 same, // reg is unchanged
55 atCFAPlusOffset, // reg = deref(CFA + offset)
56 isCFAPlusOffset, // reg = CFA + offset
57 inOtherRegister, // reg = other reg
58 atDWARFExpression, // reg = deref(eval(dwarf_expr))
59 isDWARFExpression // reg = eval(dwarf_expr)
69 operator == (const RegisterLocation& rhs) const;
72 operator != (const RegisterLocation &rhs) const
74 return !(*this == rhs);
98 return m_type == same;
102 IsUnspecified () const
104 return m_type == unspecified;
108 IsCFAPlusOffset () const
110 return m_type == isCFAPlusOffset;
114 IsAtCFAPlusOffset () const
116 return m_type == atCFAPlusOffset;
120 IsInOtherRegister () const
122 return m_type == inOtherRegister;
126 IsAtDWARFExpression () const
128 return m_type == atDWARFExpression;
132 IsDWARFExpression () const
134 return m_type == isDWARFExpression;
138 SetAtCFAPlusOffset (int32_t offset)
140 m_type = atCFAPlusOffset;
141 m_location.offset = offset;
145 SetIsCFAPlusOffset (int32_t offset)
147 m_type = isCFAPlusOffset;
148 m_location.offset = offset;
152 SetInRegister (uint32_t reg_num)
154 m_type = inOtherRegister;
155 m_location.reg_num = reg_num;
159 GetRegisterNumber () const
161 if (m_type == inOtherRegister)
162 return m_location.reg_num;
163 return LLDB_INVALID_REGNUM;
167 GetLocationType () const
175 if (m_type == atCFAPlusOffset || m_type == isCFAPlusOffset)
176 return m_location.offset;
181 GetDWARFExpr (const uint8_t **opcodes, uint16_t& len) const
183 if (m_type == atDWARFExpression || m_type == isDWARFExpression)
185 *opcodes = m_location.expr.opcodes;
186 len = m_location.expr.length;
196 SetAtDWARFExpression (const uint8_t *opcodes, uint32_t len);
199 SetIsDWARFExpression (const uint8_t *opcodes, uint32_t len);
202 GetDWARFExpressionBytes ()
204 if (m_type == atDWARFExpression || m_type == isDWARFExpression)
205 return m_location.expr.opcodes;
210 GetDWARFExpressionLength ()
212 if (m_type == atDWARFExpression || m_type == isDWARFExpression)
213 return m_location.expr.length;
219 const UnwindPlan* unwind_plan,
220 const UnwindPlan::Row* row,
225 RestoreType m_type; // How do we locate this register?
228 // For m_type == atCFAPlusOffset or m_type == isCFAPlusOffset
230 // For m_type == inOtherRegister
231 uint32_t reg_num; // The register number
232 // For m_type == atDWARFExpression or m_type == isDWARFExpression
234 const uint8_t *opcodes;
243 Row (const UnwindPlan::Row& rhs) :
244 m_offset (rhs.m_offset),
245 m_cfa_reg_num (rhs.m_cfa_reg_num),
246 m_cfa_offset (rhs.m_cfa_offset),
247 m_register_locations (rhs.m_register_locations)
252 operator == (const Row &rhs) const;
255 GetRegisterInfo (uint32_t reg_num, RegisterLocation& register_location) const;
258 SetRegisterInfo (uint32_t reg_num, const RegisterLocation register_location);
267 SetOffset(lldb::addr_t offset)
273 SlideOffset(lldb::addr_t offset)
279 GetCFARegister () const
281 return m_cfa_reg_num;
285 SetRegisterLocationToAtCFAPlusOffset (uint32_t reg_num,
290 SetRegisterLocationToIsCFAPlusOffset (uint32_t reg_num,
295 SetRegisterLocationToUndefined (uint32_t reg_num,
297 bool can_replace_only_if_unspecified);
300 SetRegisterLocationToUnspecified (uint32_t reg_num,
304 SetRegisterLocationToRegister (uint32_t reg_num,
305 uint32_t other_reg_num,
309 SetRegisterLocationToSame (uint32_t reg_num,
315 SetCFARegister (uint32_t reg_num);
318 GetCFAOffset () const
324 SetCFAOffset (int32_t offset)
326 m_cfa_offset = offset;
333 Dump (Stream& s, const UnwindPlan* unwind_plan, Thread* thread, lldb::addr_t base_addr) const;
336 typedef std::map<uint32_t, RegisterLocation> collection;
337 lldb::addr_t m_offset; // Offset into the function for this row
338 uint32_t m_cfa_reg_num; // The Call Frame Address register number
339 int32_t m_cfa_offset; // The offset from the CFA for this row
340 collection m_register_locations;
345 typedef std::shared_ptr<Row> RowSP;
347 UnwindPlan (lldb::RegisterKind reg_kind) :
349 m_plan_valid_address_range (),
350 m_register_kind (reg_kind),
351 m_return_addr_register (LLDB_INVALID_REGNUM),
353 m_plan_is_sourced_from_compiler (eLazyBoolCalculate),
354 m_plan_is_valid_at_all_instruction_locations (eLazyBoolCalculate)
363 Dump (Stream& s, Thread* thread, lldb::addr_t base_addr) const;
366 AppendRow (const RowSP& row_sp);
368 // Returns a pointer to the best row for the given offset into the function's instructions.
369 // If offset is -1 it indicates that the function start is unknown - the final row in the UnwindPlan is returned.
370 // In practice, the UnwindPlan for a function with no known start address will be the architectural default
371 // UnwindPlan which will only have one row.
373 GetRowForFunctionOffset (int offset) const;
376 GetRegisterKind () const
378 return m_register_kind;
382 SetRegisterKind (lldb::RegisterKind kind)
384 m_register_kind = kind;
388 SetReturnAddressRegister (uint32_t regnum)
390 m_return_addr_register = regnum;
394 GetReturnAddressRegister (void)
396 return m_return_addr_register;
400 GetInitialCFARegister () const
402 if (m_row_list.empty())
403 return LLDB_INVALID_REGNUM;
404 return m_row_list.front()->GetCFARegister();
407 // This UnwindPlan may not be valid at every address of the function span.
408 // For instance, a FastUnwindPlan will not be valid at the prologue setup
409 // instructions - only in the body of the function.
411 SetPlanValidAddressRange (const AddressRange& range);
414 GetAddressRange () const
416 return m_plan_valid_address_range;
420 PlanValidAtAddress (Address addr);
423 IsValidRowIndex (uint32_t idx) const;
425 const UnwindPlan::RowSP
426 GetRowAtIndex (uint32_t idx) const;
428 const UnwindPlan::RowSP
431 lldb_private::ConstString
432 GetSourceName () const;
435 SetSourceName (const char *);
437 // Was this UnwindPlan emitted by a compiler?
438 lldb_private::LazyBool
439 GetSourcedFromCompiler () const
441 return m_plan_is_sourced_from_compiler;
444 // Was this UnwindPlan emitted by a compiler?
446 SetSourcedFromCompiler (lldb_private::LazyBool from_compiler)
448 m_plan_is_sourced_from_compiler = from_compiler;
451 // Is this UnwindPlan valid at all instructions? If not, then it is assumed valid at call sites,
452 // e.g. for exception handling.
453 lldb_private::LazyBool
454 GetUnwindPlanValidAtAllInstructions () const
456 return m_plan_is_valid_at_all_instruction_locations;
459 // Is this UnwindPlan valid at all instructions? If not, then it is assumed valid at call sites,
460 // e.g. for exception handling.
462 SetUnwindPlanValidAtAllInstructions (lldb_private::LazyBool valid_at_all_insn)
464 m_plan_is_valid_at_all_instruction_locations = valid_at_all_insn;
468 GetRowCount () const;
474 m_plan_valid_address_range.Clear();
475 m_register_kind = lldb::eRegisterKindDWARF;
476 m_source_name.Clear();
480 GetRegisterInfo (Thread* thread, uint32_t reg_num) const;
485 typedef std::vector<RowSP> collection;
486 collection m_row_list;
487 AddressRange m_plan_valid_address_range;
488 lldb::RegisterKind m_register_kind; // The RegisterKind these register numbers are in terms of - will need to be
489 // translated to lldb native reg nums at unwind time
490 uint32_t m_return_addr_register; // The register that has the return address for the caller frame
491 // e.g. the lr on arm
492 lldb_private::ConstString m_source_name; // for logging, where this UnwindPlan originated from
493 lldb_private::LazyBool m_plan_is_sourced_from_compiler;
494 lldb_private::LazyBool m_plan_is_valid_at_all_instruction_locations;
495 }; // class UnwindPlan
497 } // namespace lldb_private
499 #endif //liblldb_UnwindPlan_h