1 //===-- UnwindPlan.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_UnwindPlan_h
11 #define liblldb_UnwindPlan_h
19 // Other libraries and framework includes
21 #include "lldb/lldb-private.h"
22 #include "lldb/Core/AddressRange.h"
23 #include "lldb/Core/Stream.h"
24 #include "lldb/Core/ConstString.h"
26 namespace lldb_private {
28 // The UnwindPlan object specifies how to unwind out of a function - where
29 // this function saves the caller's register values before modifying them
30 // (for non-volatile aka saved registers) and how to find this frame's
31 // Canonical Frame Address (CFA).
33 // Most commonly, registers are saved on the stack, offset some bytes from
34 // the Canonical Frame Address, or CFA, which is the starting address of
35 // this function's stack frame (the CFA is same as the eh_frame's CFA,
36 // whatever that may be on a given architecture).
37 // The CFA address for the stack frame does not change during
38 // the lifetime of the function.
40 // Internally, the UnwindPlan is structured as a vector of register locations
41 // organized by code address in the function, showing which registers have been
42 // saved at that point and where they are saved.
43 // It can be thought of as the expanded table form of the DWARF CFI
44 // encoded information.
46 // Other unwind information sources will be converted into UnwindPlans before
47 // being added to a FuncUnwinders object. The unwind source may be
48 // an eh_frame FDE, a DWARF debug_frame FDE, or assembly language based
50 // The UnwindPlan is the canonical form of this information that the unwinder
51 // code will use when walking the stack.
57 class RegisterLocation
62 unspecified, // not specified, we may be able to assume this
63 // is the same register. gcc doesn't specify all
64 // initial values so we really don't know...
65 undefined, // reg is not available, e.g. volatile reg
66 same, // reg is unchanged
67 atCFAPlusOffset, // reg = deref(CFA + offset)
68 isCFAPlusOffset, // reg = CFA + offset
69 inOtherRegister, // reg = other reg
70 atDWARFExpression, // reg = deref(eval(dwarf_expr))
71 isDWARFExpression // reg = eval(dwarf_expr)
81 operator == (const RegisterLocation& rhs) const;
84 operator != (const RegisterLocation &rhs) const
86 return !(*this == rhs);
110 return m_type == same;
114 IsUnspecified () const
116 return m_type == unspecified;
120 IsCFAPlusOffset () const
122 return m_type == isCFAPlusOffset;
126 IsAtCFAPlusOffset () const
128 return m_type == atCFAPlusOffset;
132 IsInOtherRegister () const
134 return m_type == inOtherRegister;
138 IsAtDWARFExpression () const
140 return m_type == atDWARFExpression;
144 IsDWARFExpression () const
146 return m_type == isDWARFExpression;
150 SetAtCFAPlusOffset (int32_t offset)
152 m_type = atCFAPlusOffset;
153 m_location.offset = offset;
157 SetIsCFAPlusOffset (int32_t offset)
159 m_type = isCFAPlusOffset;
160 m_location.offset = offset;
164 SetInRegister (uint32_t reg_num)
166 m_type = inOtherRegister;
167 m_location.reg_num = reg_num;
171 GetRegisterNumber () const
173 if (m_type == inOtherRegister)
174 return m_location.reg_num;
175 return LLDB_INVALID_REGNUM;
179 GetLocationType () const
187 if (m_type == atCFAPlusOffset || m_type == isCFAPlusOffset)
188 return m_location.offset;
193 GetDWARFExpr (const uint8_t **opcodes, uint16_t& len) const
195 if (m_type == atDWARFExpression || m_type == isDWARFExpression)
197 *opcodes = m_location.expr.opcodes;
198 len = m_location.expr.length;
208 SetAtDWARFExpression (const uint8_t *opcodes, uint32_t len);
211 SetIsDWARFExpression (const uint8_t *opcodes, uint32_t len);
214 GetDWARFExpressionBytes ()
216 if (m_type == atDWARFExpression || m_type == isDWARFExpression)
217 return m_location.expr.opcodes;
222 GetDWARFExpressionLength ()
224 if (m_type == atDWARFExpression || m_type == isDWARFExpression)
225 return m_location.expr.length;
231 const UnwindPlan* unwind_plan,
232 const UnwindPlan::Row* row,
237 RestoreType m_type; // How do we locate this register?
240 // For m_type == atCFAPlusOffset or m_type == isCFAPlusOffset
242 // For m_type == inOtherRegister
243 uint32_t reg_num; // The register number
244 // For m_type == atDWARFExpression or m_type == isDWARFExpression
246 const uint8_t *opcodes;
257 unspecified, // not specified
258 isRegisterPlusOffset, // CFA = register + offset
259 isRegisterDereferenced, // CFA = [reg]
260 isDWARFExpression // CFA = eval(dwarf_expr)
270 operator == (const CFAValue& rhs) const;
273 operator != (const CFAValue &rhs) const
275 return !(*this == rhs);
281 m_type = unspecified;
285 IsUnspecified () const
287 return m_type == unspecified;
291 IsRegisterPlusOffset () const
293 return m_type == isRegisterPlusOffset;
297 SetIsRegisterPlusOffset (uint32_t reg_num, int32_t offset)
299 m_type = isRegisterPlusOffset;
300 m_value.reg.reg_num = reg_num;
301 m_value.reg.offset = offset;
305 IsRegisterDereferenced () const
307 return m_type == isRegisterDereferenced;
311 SetIsRegisterDereferenced (uint32_t reg_num)
313 m_type = isRegisterDereferenced;
314 m_value.reg.reg_num = reg_num;
318 IsDWARFExpression () const
320 return m_type == isDWARFExpression;
324 SetIsDWARFExpression (const uint8_t *opcodes, uint32_t len)
326 m_type = isDWARFExpression;
327 m_value.expr.opcodes = opcodes;
328 m_value.expr.length = len;
332 GetRegisterNumber () const
334 if (m_type == isRegisterDereferenced || m_type == isRegisterPlusOffset)
335 return m_value.reg.reg_num;
336 return LLDB_INVALID_REGNUM;
340 GetValueType () const
348 if (m_type == isRegisterPlusOffset)
349 return m_value.reg.offset;
353 void IncOffset (int32_t delta)
355 if (m_type == isRegisterPlusOffset)
356 m_value.reg.offset += delta;
359 void SetOffset (int32_t offset)
361 if (m_type == isRegisterPlusOffset)
362 m_value.reg.offset = offset;
366 GetDWARFExpr (const uint8_t **opcodes, uint16_t& len) const
368 if (m_type == isDWARFExpression)
370 *opcodes = m_value.expr.opcodes;
371 len = m_value.expr.length;
381 GetDWARFExpressionBytes ()
383 if (m_type == isDWARFExpression)
384 return m_value.expr.opcodes;
389 GetDWARFExpressionLength ()
391 if (m_type == isDWARFExpression)
392 return m_value.expr.length;
398 const UnwindPlan* unwind_plan,
399 Thread* thread) const;
402 ValueType m_type; // How do we compute CFA value?
406 // For m_type == isRegisterPlusOffset or m_type == isRegisterDereferenced
407 uint32_t reg_num; // The register number
408 // For m_type == isRegisterPlusOffset
411 // For m_type == isDWARFExpression
413 const uint8_t *opcodes;
422 Row (const UnwindPlan::Row& rhs) = default;
425 operator == (const Row &rhs) const;
428 GetRegisterInfo (uint32_t reg_num, RegisterLocation& register_location) const;
431 SetRegisterInfo (uint32_t reg_num, const RegisterLocation register_location);
434 RemoveRegisterInfo (uint32_t reg_num);
443 SetOffset(lldb::addr_t offset)
449 SlideOffset(lldb::addr_t offset)
454 CFAValue& GetCFAValue()
460 SetRegisterLocationToAtCFAPlusOffset (uint32_t reg_num,
465 SetRegisterLocationToIsCFAPlusOffset (uint32_t reg_num,
470 SetRegisterLocationToUndefined (uint32_t reg_num,
472 bool can_replace_only_if_unspecified);
475 SetRegisterLocationToUnspecified (uint32_t reg_num,
479 SetRegisterLocationToRegister (uint32_t reg_num,
480 uint32_t other_reg_num,
484 SetRegisterLocationToSame (uint32_t reg_num,
491 Dump (Stream& s, const UnwindPlan* unwind_plan, Thread* thread, lldb::addr_t base_addr) const;
494 typedef std::map<uint32_t, RegisterLocation> collection;
495 lldb::addr_t m_offset; // Offset into the function for this row
497 CFAValue m_cfa_value;
498 collection m_register_locations;
502 typedef std::shared_ptr<Row> RowSP;
504 UnwindPlan (lldb::RegisterKind reg_kind) :
506 m_plan_valid_address_range (),
507 m_register_kind (reg_kind),
508 m_return_addr_register (LLDB_INVALID_REGNUM),
510 m_plan_is_sourced_from_compiler (eLazyBoolCalculate),
511 m_plan_is_valid_at_all_instruction_locations (eLazyBoolCalculate),
513 m_personality_func_addr ()
517 // Performs a deep copy of the plan, including all the rows (expensive).
518 UnwindPlan (const UnwindPlan &rhs) :
519 m_plan_valid_address_range (rhs.m_plan_valid_address_range),
520 m_register_kind (rhs.m_register_kind),
521 m_return_addr_register (rhs.m_return_addr_register),
522 m_source_name (rhs.m_source_name),
523 m_plan_is_sourced_from_compiler (rhs.m_plan_is_sourced_from_compiler),
524 m_plan_is_valid_at_all_instruction_locations (rhs.m_plan_is_valid_at_all_instruction_locations),
525 m_lsda_address (rhs.m_lsda_address),
526 m_personality_func_addr (rhs.m_personality_func_addr)
528 m_row_list.reserve (rhs.m_row_list.size());
529 for (const RowSP &row_sp: rhs.m_row_list)
530 m_row_list.emplace_back (new Row (*row_sp));
533 ~UnwindPlan() = default;
536 Dump (Stream& s, Thread* thread, lldb::addr_t base_addr) const;
539 AppendRow (const RowSP& row_sp);
542 InsertRow (const RowSP& row_sp);
544 // Returns a pointer to the best row for the given offset into the function's instructions.
545 // If offset is -1 it indicates that the function start is unknown - the final row in the UnwindPlan is returned.
546 // In practice, the UnwindPlan for a function with no known start address will be the architectural default
547 // UnwindPlan which will only have one row.
549 GetRowForFunctionOffset (int offset) const;
552 GetRegisterKind () const
554 return m_register_kind;
558 SetRegisterKind (lldb::RegisterKind kind)
560 m_register_kind = kind;
564 SetReturnAddressRegister (uint32_t regnum)
566 m_return_addr_register = regnum;
570 GetReturnAddressRegister (void)
572 return m_return_addr_register;
576 GetInitialCFARegister () const
578 if (m_row_list.empty())
579 return LLDB_INVALID_REGNUM;
580 return m_row_list.front()->GetCFAValue().GetRegisterNumber();
583 // This UnwindPlan may not be valid at every address of the function span.
584 // For instance, a FastUnwindPlan will not be valid at the prologue setup
585 // instructions - only in the body of the function.
587 SetPlanValidAddressRange (const AddressRange& range);
590 GetAddressRange () const
592 return m_plan_valid_address_range;
596 PlanValidAtAddress (Address addr);
599 IsValidRowIndex (uint32_t idx) const;
601 const UnwindPlan::RowSP
602 GetRowAtIndex (uint32_t idx) const;
604 const UnwindPlan::RowSP
607 lldb_private::ConstString
608 GetSourceName () const;
611 SetSourceName (const char *);
613 // Was this UnwindPlan emitted by a compiler?
614 lldb_private::LazyBool
615 GetSourcedFromCompiler () const
617 return m_plan_is_sourced_from_compiler;
620 // Was this UnwindPlan emitted by a compiler?
622 SetSourcedFromCompiler (lldb_private::LazyBool from_compiler)
624 m_plan_is_sourced_from_compiler = from_compiler;
627 // Is this UnwindPlan valid at all instructions? If not, then it is assumed valid at call sites,
628 // e.g. for exception handling.
629 lldb_private::LazyBool
630 GetUnwindPlanValidAtAllInstructions () const
632 return m_plan_is_valid_at_all_instruction_locations;
635 // Is this UnwindPlan valid at all instructions? If not, then it is assumed valid at call sites,
636 // e.g. for exception handling.
638 SetUnwindPlanValidAtAllInstructions (lldb_private::LazyBool valid_at_all_insn)
640 m_plan_is_valid_at_all_instruction_locations = valid_at_all_insn;
644 GetRowCount () const;
650 m_plan_valid_address_range.Clear();
651 m_register_kind = lldb::eRegisterKindDWARF;
652 m_source_name.Clear();
653 m_plan_is_sourced_from_compiler = eLazyBoolCalculate;
654 m_plan_is_valid_at_all_instruction_locations = eLazyBoolCalculate;
655 m_lsda_address.Clear();
656 m_personality_func_addr.Clear();
660 GetRegisterInfo (Thread* thread, uint32_t reg_num) const;
663 GetLSDAAddress () const
665 return m_lsda_address;
669 SetLSDAAddress (Address lsda_addr)
671 m_lsda_address = lsda_addr;
675 GetPersonalityFunctionPtr () const
677 return m_personality_func_addr;
681 SetPersonalityFunctionPtr (Address presonality_func_ptr)
683 m_personality_func_addr = presonality_func_ptr;
687 typedef std::vector<RowSP> collection;
688 collection m_row_list;
689 AddressRange m_plan_valid_address_range;
690 lldb::RegisterKind m_register_kind; // The RegisterKind these register numbers are in terms of - will need to be
691 // translated to lldb native reg nums at unwind time
692 uint32_t m_return_addr_register; // The register that has the return address for the caller frame
693 // e.g. the lr on arm
694 lldb_private::ConstString m_source_name; // for logging, where this UnwindPlan originated from
695 lldb_private::LazyBool m_plan_is_sourced_from_compiler;
696 lldb_private::LazyBool m_plan_is_valid_at_all_instruction_locations;
698 Address m_lsda_address; // Where the language specific data area exists in the module - used
699 // in exception handling.
700 Address m_personality_func_addr; // The address of a pointer to the personality function - used in
701 // exception handling.
702 }; // class UnwindPlan
704 } // namespace lldb_private
706 #endif // liblldb_UnwindPlan_h