1 //===-- UnwindPlan.cpp ----------------------------------*- 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 #include "lldb/Symbol/UnwindPlan.h"
12 #include "lldb/Core/ConstString.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/Thread.h"
19 using namespace lldb_private;
22 UnwindPlan::Row::RegisterLocation::operator == (const UnwindPlan::Row::RegisterLocation& rhs) const
24 if (m_type == rhs.m_type)
35 return m_location.offset == rhs.m_location.offset;
38 return m_location.reg_num == rhs.m_location.reg_num;
40 case atDWARFExpression:
41 case isDWARFExpression:
42 if (m_location.expr.length == rhs.m_location.expr.length)
43 return !memcmp (m_location.expr.opcodes, rhs.m_location.expr.opcodes, m_location.expr.length);
50 // This function doesn't copy the dwarf expression bytes; they must remain in allocated
51 // memory for the lifespan of this UnwindPlan object.
53 UnwindPlan::Row::RegisterLocation::SetAtDWARFExpression (const uint8_t *opcodes, uint32_t len)
55 m_type = atDWARFExpression;
56 m_location.expr.opcodes = opcodes;
57 m_location.expr.length = len;
60 // This function doesn't copy the dwarf expression bytes; they must remain in allocated
61 // memory for the lifespan of this UnwindPlan object.
63 UnwindPlan::Row::RegisterLocation::SetIsDWARFExpression (const uint8_t *opcodes, uint32_t len)
65 m_type = isDWARFExpression;
66 m_location.expr.opcodes = opcodes;
67 m_location.expr.length = len;
71 UnwindPlan::Row::RegisterLocation::Dump (Stream &s, const UnwindPlan* unwind_plan, const UnwindPlan::Row* row, Thread* thread, bool verbose) const
77 s.PutCString ("=<unspec>");
83 s.PutCString ("=<undef>");
88 s.PutCString ("= <same>");
95 if (m_type == atCFAPlusOffset)
98 s.Printf ("CFA%+d", m_location.offset);
100 if (unwind_plan && row)
102 const uint32_t cfa_reg = row->GetCFARegister();
103 const RegisterInfo *cfa_reg_info = unwind_plan->GetRegisterInfo (thread, cfa_reg);
104 const int32_t offset = row->GetCFAOffset() + m_location.offset;
108 s.Printf (" (%s%+d)", cfa_reg_info->name, offset);
110 s.Printf (" (reg(%u)%+d)", cfa_reg, offset);
115 s.Printf ("%s", cfa_reg_info->name);
117 s.Printf ("reg(%u)", cfa_reg);
119 s.Printf ("%+d", offset);
122 if (m_type == atCFAPlusOffset)
127 case inOtherRegister:
129 const RegisterInfo *other_reg_info = nullptr;
131 other_reg_info = unwind_plan->GetRegisterInfo (thread, m_location.reg_num);
133 s.Printf ("=%s", other_reg_info->name);
135 s.Printf ("=reg(%u)", m_location.reg_num);
139 case atDWARFExpression:
140 case isDWARFExpression:
143 if (m_type == atDWARFExpression)
144 s.PutCString("[dwarf-expr]");
146 s.PutCString("dwarf-expr");
154 UnwindPlan::Row::Clear ()
156 m_cfa_type = CFAIsRegisterPlusOffset;
158 m_cfa_reg_num = LLDB_INVALID_REGNUM;
160 m_register_locations.clear();
164 UnwindPlan::Row::Dump (Stream& s, const UnwindPlan* unwind_plan, Thread* thread, addr_t base_addr) const
166 const RegisterInfo *reg_info = unwind_plan->GetRegisterInfo (thread, GetCFARegister());
168 if (base_addr != LLDB_INVALID_ADDRESS)
169 s.Printf ("0x%16.16" PRIx64 ": CFA=", base_addr + GetOffset());
171 s.Printf ("%4" PRId64 ": CFA=", GetOffset());
174 s.Printf ("%s", reg_info->name);
176 s.Printf ("reg(%u)", GetCFARegister());
177 s.Printf ("%+3d => ", GetCFAOffset ());
178 for (collection::const_iterator idx = m_register_locations.begin (); idx != m_register_locations.end (); ++idx)
180 reg_info = unwind_plan->GetRegisterInfo (thread, idx->first);
182 s.Printf ("%s", reg_info->name);
184 s.Printf ("reg(%u)", idx->first);
185 const bool verbose = false;
186 idx->second.Dump(s, unwind_plan, this, thread, verbose);
192 UnwindPlan::Row::Row() :
194 m_cfa_type (CFAIsRegisterPlusOffset),
195 m_cfa_reg_num (LLDB_INVALID_REGNUM),
197 m_register_locations ()
202 UnwindPlan::Row::GetRegisterInfo (uint32_t reg_num, UnwindPlan::Row::RegisterLocation& register_location) const
204 collection::const_iterator pos = m_register_locations.find(reg_num);
205 if (pos != m_register_locations.end())
207 register_location = pos->second;
214 UnwindPlan::Row::RemoveRegisterInfo (uint32_t reg_num)
216 collection::const_iterator pos = m_register_locations.find(reg_num);
217 if (pos != m_register_locations.end())
219 m_register_locations.erase(pos);
224 UnwindPlan::Row::SetRegisterInfo (uint32_t reg_num, const UnwindPlan::Row::RegisterLocation register_location)
226 m_register_locations[reg_num] = register_location;
230 UnwindPlan::Row::SetRegisterLocationToAtCFAPlusOffset (uint32_t reg_num, int32_t offset, bool can_replace)
232 if (!can_replace && m_register_locations.find(reg_num) != m_register_locations.end())
234 RegisterLocation reg_loc;
235 reg_loc.SetAtCFAPlusOffset(offset);
236 m_register_locations[reg_num] = reg_loc;
241 UnwindPlan::Row::SetRegisterLocationToIsCFAPlusOffset (uint32_t reg_num, int32_t offset, bool can_replace)
243 if (!can_replace && m_register_locations.find(reg_num) != m_register_locations.end())
245 RegisterLocation reg_loc;
246 reg_loc.SetIsCFAPlusOffset(offset);
247 m_register_locations[reg_num] = reg_loc;
252 UnwindPlan::Row::SetRegisterLocationToUndefined (uint32_t reg_num, bool can_replace, bool can_replace_only_if_unspecified)
254 collection::iterator pos = m_register_locations.find(reg_num);
255 collection::iterator end = m_register_locations.end();
261 if (can_replace_only_if_unspecified && !pos->second.IsUnspecified())
264 RegisterLocation reg_loc;
265 reg_loc.SetUndefined();
266 m_register_locations[reg_num] = reg_loc;
271 UnwindPlan::Row::SetRegisterLocationToUnspecified (uint32_t reg_num, bool can_replace)
273 if (!can_replace && m_register_locations.find(reg_num) != m_register_locations.end())
275 RegisterLocation reg_loc;
276 reg_loc.SetUnspecified();
277 m_register_locations[reg_num] = reg_loc;
282 UnwindPlan::Row::SetRegisterLocationToRegister (uint32_t reg_num,
283 uint32_t other_reg_num,
286 if (!can_replace && m_register_locations.find(reg_num) != m_register_locations.end())
288 RegisterLocation reg_loc;
289 reg_loc.SetInRegister(other_reg_num);
290 m_register_locations[reg_num] = reg_loc;
295 UnwindPlan::Row::SetRegisterLocationToSame (uint32_t reg_num, bool must_replace)
297 if (must_replace && m_register_locations.find(reg_num) == m_register_locations.end())
299 RegisterLocation reg_loc;
301 m_register_locations[reg_num] = reg_loc;
306 UnwindPlan::Row::SetCFARegister (uint32_t reg_num)
308 m_cfa_reg_num = reg_num;
312 UnwindPlan::Row::operator == (const UnwindPlan::Row& rhs) const
314 if (m_offset != rhs.m_offset || m_cfa_reg_num != rhs.m_cfa_reg_num || m_cfa_offset != rhs.m_cfa_offset)
317 if (m_cfa_type != rhs.m_cfa_type)
320 if (m_cfa_type == CFAIsRegisterPlusOffset)
322 if (m_cfa_reg_num != rhs.m_cfa_reg_num)
324 if (m_cfa_offset != rhs.m_cfa_offset)
327 if (m_cfa_type == CFAIsRegisterDereferenced)
329 if (m_cfa_reg_num != rhs.m_cfa_reg_num)
333 return m_register_locations == rhs.m_register_locations;
337 UnwindPlan::AppendRow (const UnwindPlan::RowSP &row_sp)
339 if (m_row_list.empty() || m_row_list.back()->GetOffset() != row_sp->GetOffset())
340 m_row_list.push_back(row_sp);
342 m_row_list.back() = row_sp;
346 UnwindPlan::InsertRow (const UnwindPlan::RowSP &row_sp)
348 collection::iterator it = m_row_list.begin();
349 while (it != m_row_list.end()) {
351 if (row->GetOffset() > row_sp->GetOffset())
355 m_row_list.insert(it, row_sp);
359 UnwindPlan::GetRowForFunctionOffset (int offset) const
362 if (!m_row_list.empty())
365 row = m_row_list.back();
368 collection::const_iterator pos, end = m_row_list.end();
369 for (pos = m_row_list.begin(); pos != end; ++pos)
371 if ((*pos)->GetOffset() <= static_cast<lldb::offset_t>(offset))
382 UnwindPlan::IsValidRowIndex (uint32_t idx) const
384 return idx < m_row_list.size();
387 const UnwindPlan::RowSP
388 UnwindPlan::GetRowAtIndex (uint32_t idx) const
390 // You must call IsValidRowIndex(idx) first before calling this!!!
391 assert (idx < m_row_list.size());
392 return m_row_list[idx];
395 const UnwindPlan::RowSP
396 UnwindPlan::GetLastRow () const
398 // You must call GetRowCount() first to make sure there is at least one row
399 assert (!m_row_list.empty());
400 return m_row_list.back();
404 UnwindPlan::GetRowCount () const
406 return m_row_list.size ();
410 UnwindPlan::SetPlanValidAddressRange (const AddressRange& range)
412 if (range.GetBaseAddress().IsValid() && range.GetByteSize() != 0)
413 m_plan_valid_address_range = range;
417 UnwindPlan::PlanValidAtAddress (Address addr)
419 // If this UnwindPlan has no rows, it is an invalid UnwindPlan.
420 if (GetRowCount() == 0)
422 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
426 if (addr.Dump (&s, nullptr, Address::DumpStyleSectionNameOffset))
428 log->Printf ("UnwindPlan is invalid -- no unwind rows for UnwindPlan '%s' at address %s",
429 m_source_name.GetCString(), s.GetData());
433 log->Printf ("UnwindPlan is invalid -- no unwind rows for UnwindPlan '%s'",
434 m_source_name.GetCString());
440 // If the 0th Row of unwind instructions is missing, or if it doesn't provide
441 // a register to use to find the Canonical Frame Address, this is not a valid UnwindPlan.
442 if (GetRowAtIndex(0).get() == nullptr || GetRowAtIndex(0)->GetCFARegister() == LLDB_INVALID_REGNUM)
444 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
448 if (addr.Dump (&s, nullptr, Address::DumpStyleSectionNameOffset))
450 log->Printf ("UnwindPlan is invalid -- no CFA register defined in row 0 for UnwindPlan '%s' at address %s",
451 m_source_name.GetCString(), s.GetData());
455 log->Printf ("UnwindPlan is invalid -- no CFA register defined in row 0 for UnwindPlan '%s'",
456 m_source_name.GetCString());
462 if (!m_plan_valid_address_range.GetBaseAddress().IsValid() || m_plan_valid_address_range.GetByteSize() == 0)
468 if (m_plan_valid_address_range.ContainsFileAddress (addr))
475 UnwindPlan::Dump (Stream& s, Thread *thread, lldb::addr_t base_addr) const
477 if (!m_source_name.IsEmpty())
479 s.Printf ("This UnwindPlan originally sourced from %s\n", m_source_name.GetCString());
481 if (m_lsda_address.IsValid() && m_personality_func_addr.IsValid())
483 TargetSP target_sp(thread->CalculateTarget());
484 addr_t lsda_load_addr = m_lsda_address.GetLoadAddress (target_sp.get());
485 addr_t personality_func_load_addr = m_personality_func_addr.GetLoadAddress (target_sp.get());
487 if (lsda_load_addr != LLDB_INVALID_ADDRESS && personality_func_load_addr != LLDB_INVALID_ADDRESS)
489 s.Printf("LSDA address 0x%" PRIx64 ", personality routine is at address 0x%" PRIx64 "\n",
490 lsda_load_addr, personality_func_load_addr);
493 s.Printf ("This UnwindPlan is sourced from the compiler: ");
494 switch (m_plan_is_sourced_from_compiler)
502 case eLazyBoolCalculate:
503 s.Printf ("not specified.\n");
506 s.Printf ("This UnwindPlan is valid at all instruction locations: ");
507 switch (m_plan_is_valid_at_all_instruction_locations)
515 case eLazyBoolCalculate:
516 s.Printf ("not specified.\n");
519 if (m_plan_valid_address_range.GetBaseAddress().IsValid() && m_plan_valid_address_range.GetByteSize() > 0)
521 s.PutCString ("Address range of this UnwindPlan: ");
522 TargetSP target_sp(thread->CalculateTarget());
523 m_plan_valid_address_range.Dump (&s, target_sp.get(), Address::DumpStyleSectionNameOffset);
526 collection::const_iterator pos, begin = m_row_list.begin(), end = m_row_list.end();
527 for (pos = begin; pos != end; ++pos)
529 s.Printf ("row[%u]: ", (uint32_t)std::distance (begin, pos));
530 (*pos)->Dump(s, this, thread, base_addr);
535 UnwindPlan::SetSourceName (const char *source)
537 m_source_name = ConstString (source);
541 UnwindPlan::GetSourceName () const
543 return m_source_name;
547 UnwindPlan::GetRegisterInfo (Thread* thread, uint32_t unwind_reg) const
551 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
555 if (m_register_kind == eRegisterKindLLDB)
558 reg = reg_ctx->ConvertRegisterKindToRegisterNumber (m_register_kind, unwind_reg);
559 if (reg != LLDB_INVALID_REGNUM)
560 return reg_ctx->GetRegisterInfoAtIndex (reg);