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/Target/Process.h"
13 #include "lldb/Target/RegisterContext.h"
14 #include "lldb/Target/Thread.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/Log.h"
19 using namespace lldb_private;
21 bool UnwindPlan::Row::RegisterLocation::
22 operator==(const UnwindPlan::Row::RegisterLocation &rhs) const {
23 if (m_type == rhs.m_type) {
32 return m_location.offset == rhs.m_location.offset;
35 return m_location.reg_num == rhs.m_location.reg_num;
37 case atDWARFExpression:
38 case isDWARFExpression:
39 if (m_location.expr.length == rhs.m_location.expr.length)
40 return !memcmp(m_location.expr.opcodes, rhs.m_location.expr.opcodes,
41 m_location.expr.length);
48 // This function doesn't copy the dwarf expression bytes; they must remain in
49 // allocated memory for the lifespan of this UnwindPlan object.
50 void UnwindPlan::Row::RegisterLocation::SetAtDWARFExpression(
51 const uint8_t *opcodes, uint32_t len) {
52 m_type = atDWARFExpression;
53 m_location.expr.opcodes = opcodes;
54 m_location.expr.length = len;
57 // This function doesn't copy the dwarf expression bytes; they must remain in
58 // allocated memory for the lifespan of this UnwindPlan object.
59 void UnwindPlan::Row::RegisterLocation::SetIsDWARFExpression(
60 const uint8_t *opcodes, uint32_t len) {
61 m_type = isDWARFExpression;
62 m_location.expr.opcodes = opcodes;
63 m_location.expr.length = len;
66 void UnwindPlan::Row::RegisterLocation::Dump(Stream &s,
67 const UnwindPlan *unwind_plan,
68 const UnwindPlan::Row *row,
74 s.PutCString("=<unspec>");
80 s.PutCString("=<undef>");
85 s.PutCString("= <same>");
89 case isCFAPlusOffset: {
91 if (m_type == atCFAPlusOffset)
93 s.Printf("CFA%+d", m_location.offset);
94 if (m_type == atCFAPlusOffset)
98 case inOtherRegister: {
99 const RegisterInfo *other_reg_info = nullptr;
101 other_reg_info = unwind_plan->GetRegisterInfo(thread, m_location.reg_num);
103 s.Printf("=%s", other_reg_info->name);
105 s.Printf("=reg(%u)", m_location.reg_num);
108 case atDWARFExpression:
109 case isDWARFExpression: {
111 if (m_type == atDWARFExpression)
112 s.PutCString("[dwarf-expr]");
114 s.PutCString("dwarf-expr");
119 static void DumpRegisterName(Stream &s, const UnwindPlan *unwind_plan,
120 Thread *thread, uint32_t reg_num) {
121 const RegisterInfo *reg_info = unwind_plan->GetRegisterInfo(thread, reg_num);
123 s.PutCString(reg_info->name);
125 s.Printf("reg(%u)", reg_num);
128 bool UnwindPlan::Row::CFAValue::
129 operator==(const UnwindPlan::Row::CFAValue &rhs) const {
130 if (m_type == rhs.m_type) {
135 case isRegisterPlusOffset:
136 return m_value.reg.offset == rhs.m_value.reg.offset;
138 case isRegisterDereferenced:
139 return m_value.reg.reg_num == rhs.m_value.reg.reg_num;
141 case isDWARFExpression:
142 if (m_value.expr.length == rhs.m_value.expr.length)
143 return !memcmp(m_value.expr.opcodes, rhs.m_value.expr.opcodes,
144 m_value.expr.length);
151 void UnwindPlan::Row::CFAValue::Dump(Stream &s, const UnwindPlan *unwind_plan,
152 Thread *thread) const {
154 case isRegisterPlusOffset:
155 DumpRegisterName(s, unwind_plan, thread, m_value.reg.reg_num);
156 s.Printf("%+3d", m_value.reg.offset);
158 case isRegisterDereferenced:
160 DumpRegisterName(s, unwind_plan, thread, m_value.reg.reg_num);
163 case isDWARFExpression:
164 s.PutCString("dwarf-expr");
167 s.PutCString("unspecified");
172 void UnwindPlan::Row::Clear() {
173 m_cfa_value.SetUnspecified();
175 m_register_locations.clear();
178 void UnwindPlan::Row::Dump(Stream &s, const UnwindPlan *unwind_plan,
179 Thread *thread, addr_t base_addr) const {
180 if (base_addr != LLDB_INVALID_ADDRESS)
181 s.Printf("0x%16.16" PRIx64 ": CFA=", base_addr + GetOffset());
183 s.Printf("%4" PRId64 ": CFA=", GetOffset());
185 m_cfa_value.Dump(s, unwind_plan, thread);
187 for (collection::const_iterator idx = m_register_locations.begin();
188 idx != m_register_locations.end(); ++idx) {
189 DumpRegisterName(s, unwind_plan, thread, idx->first);
190 const bool verbose = false;
191 idx->second.Dump(s, unwind_plan, this, thread, verbose);
197 UnwindPlan::Row::Row() : m_offset(0), m_cfa_value(), m_register_locations() {}
199 bool UnwindPlan::Row::GetRegisterInfo(
201 UnwindPlan::Row::RegisterLocation ®ister_location) const {
202 collection::const_iterator pos = m_register_locations.find(reg_num);
203 if (pos != m_register_locations.end()) {
204 register_location = pos->second;
210 void UnwindPlan::Row::RemoveRegisterInfo(uint32_t reg_num) {
211 collection::const_iterator pos = m_register_locations.find(reg_num);
212 if (pos != m_register_locations.end()) {
213 m_register_locations.erase(pos);
217 void UnwindPlan::Row::SetRegisterInfo(
219 const UnwindPlan::Row::RegisterLocation register_location) {
220 m_register_locations[reg_num] = register_location;
223 bool UnwindPlan::Row::SetRegisterLocationToAtCFAPlusOffset(uint32_t reg_num,
227 m_register_locations.find(reg_num) != m_register_locations.end())
229 RegisterLocation reg_loc;
230 reg_loc.SetAtCFAPlusOffset(offset);
231 m_register_locations[reg_num] = reg_loc;
235 bool UnwindPlan::Row::SetRegisterLocationToIsCFAPlusOffset(uint32_t reg_num,
239 m_register_locations.find(reg_num) != m_register_locations.end())
241 RegisterLocation reg_loc;
242 reg_loc.SetIsCFAPlusOffset(offset);
243 m_register_locations[reg_num] = reg_loc;
247 bool UnwindPlan::Row::SetRegisterLocationToUndefined(
248 uint32_t reg_num, bool can_replace, bool can_replace_only_if_unspecified) {
249 collection::iterator pos = m_register_locations.find(reg_num);
250 collection::iterator end = m_register_locations.end();
255 if (can_replace_only_if_unspecified && !pos->second.IsUnspecified())
258 RegisterLocation reg_loc;
259 reg_loc.SetUndefined();
260 m_register_locations[reg_num] = reg_loc;
264 bool UnwindPlan::Row::SetRegisterLocationToUnspecified(uint32_t reg_num,
267 m_register_locations.find(reg_num) != m_register_locations.end())
269 RegisterLocation reg_loc;
270 reg_loc.SetUnspecified();
271 m_register_locations[reg_num] = reg_loc;
275 bool UnwindPlan::Row::SetRegisterLocationToRegister(uint32_t reg_num,
276 uint32_t other_reg_num,
279 m_register_locations.find(reg_num) != m_register_locations.end())
281 RegisterLocation reg_loc;
282 reg_loc.SetInRegister(other_reg_num);
283 m_register_locations[reg_num] = reg_loc;
287 bool UnwindPlan::Row::SetRegisterLocationToSame(uint32_t reg_num,
290 m_register_locations.find(reg_num) == m_register_locations.end())
292 RegisterLocation reg_loc;
294 m_register_locations[reg_num] = reg_loc;
298 bool UnwindPlan::Row::operator==(const UnwindPlan::Row &rhs) const {
299 return m_offset == rhs.m_offset && m_cfa_value == rhs.m_cfa_value &&
300 m_register_locations == rhs.m_register_locations;
303 void UnwindPlan::AppendRow(const UnwindPlan::RowSP &row_sp) {
304 if (m_row_list.empty() ||
305 m_row_list.back()->GetOffset() != row_sp->GetOffset())
306 m_row_list.push_back(row_sp);
308 m_row_list.back() = row_sp;
311 void UnwindPlan::InsertRow(const UnwindPlan::RowSP &row_sp,
312 bool replace_existing) {
313 collection::iterator it = m_row_list.begin();
314 while (it != m_row_list.end()) {
316 if (row->GetOffset() >= row_sp->GetOffset())
320 if (it == m_row_list.end() || (*it)->GetOffset() != row_sp->GetOffset())
321 m_row_list.insert(it, row_sp);
322 else if (replace_existing)
326 UnwindPlan::RowSP UnwindPlan::GetRowForFunctionOffset(int offset) const {
328 if (!m_row_list.empty()) {
330 row = m_row_list.back();
332 collection::const_iterator pos, end = m_row_list.end();
333 for (pos = m_row_list.begin(); pos != end; ++pos) {
334 if ((*pos)->GetOffset() <= static_cast<lldb::offset_t>(offset))
344 bool UnwindPlan::IsValidRowIndex(uint32_t idx) const {
345 return idx < m_row_list.size();
348 const UnwindPlan::RowSP UnwindPlan::GetRowAtIndex(uint32_t idx) const {
349 if (idx < m_row_list.size())
350 return m_row_list[idx];
352 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
354 log->Printf("error: UnwindPlan::GetRowAtIndex(idx = %u) invalid index "
355 "(number rows is %u)",
356 idx, (uint32_t)m_row_list.size());
357 return UnwindPlan::RowSP();
361 const UnwindPlan::RowSP UnwindPlan::GetLastRow() const {
362 if (m_row_list.empty()) {
363 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
365 log->Printf("UnwindPlan::GetLastRow() when rows are empty");
366 return UnwindPlan::RowSP();
368 return m_row_list.back();
371 int UnwindPlan::GetRowCount() const { return m_row_list.size(); }
373 void UnwindPlan::SetPlanValidAddressRange(const AddressRange &range) {
374 if (range.GetBaseAddress().IsValid() && range.GetByteSize() != 0)
375 m_plan_valid_address_range = range;
378 bool UnwindPlan::PlanValidAtAddress(Address addr) {
379 // If this UnwindPlan has no rows, it is an invalid UnwindPlan.
380 if (GetRowCount() == 0) {
381 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
384 if (addr.Dump(&s, nullptr, Address::DumpStyleSectionNameOffset)) {
385 log->Printf("UnwindPlan is invalid -- no unwind rows for UnwindPlan "
386 "'%s' at address %s",
387 m_source_name.GetCString(), s.GetData());
390 "UnwindPlan is invalid -- no unwind rows for UnwindPlan '%s'",
391 m_source_name.GetCString());
397 // If the 0th Row of unwind instructions is missing, or if it doesn't provide
398 // a register to use to find the Canonical Frame Address, this is not a valid
400 if (GetRowAtIndex(0).get() == nullptr ||
401 GetRowAtIndex(0)->GetCFAValue().GetValueType() ==
402 Row::CFAValue::unspecified) {
403 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
406 if (addr.Dump(&s, nullptr, Address::DumpStyleSectionNameOffset)) {
407 log->Printf("UnwindPlan is invalid -- no CFA register defined in row 0 "
408 "for UnwindPlan '%s' at address %s",
409 m_source_name.GetCString(), s.GetData());
411 log->Printf("UnwindPlan is invalid -- no CFA register defined in row 0 "
412 "for UnwindPlan '%s'",
413 m_source_name.GetCString());
419 if (!m_plan_valid_address_range.GetBaseAddress().IsValid() ||
420 m_plan_valid_address_range.GetByteSize() == 0)
426 if (m_plan_valid_address_range.ContainsFileAddress(addr))
432 void UnwindPlan::Dump(Stream &s, Thread *thread, lldb::addr_t base_addr) const {
433 if (!m_source_name.IsEmpty()) {
434 s.Printf("This UnwindPlan originally sourced from %s\n",
435 m_source_name.GetCString());
437 if (m_lsda_address.IsValid() && m_personality_func_addr.IsValid()) {
438 TargetSP target_sp(thread->CalculateTarget());
439 addr_t lsda_load_addr = m_lsda_address.GetLoadAddress(target_sp.get());
440 addr_t personality_func_load_addr =
441 m_personality_func_addr.GetLoadAddress(target_sp.get());
443 if (lsda_load_addr != LLDB_INVALID_ADDRESS &&
444 personality_func_load_addr != LLDB_INVALID_ADDRESS) {
445 s.Printf("LSDA address 0x%" PRIx64
446 ", personality routine is at address 0x%" PRIx64 "\n",
447 lsda_load_addr, personality_func_load_addr);
450 s.Printf("This UnwindPlan is sourced from the compiler: ");
451 switch (m_plan_is_sourced_from_compiler) {
458 case eLazyBoolCalculate:
459 s.Printf("not specified.\n");
462 s.Printf("This UnwindPlan is valid at all instruction locations: ");
463 switch (m_plan_is_valid_at_all_instruction_locations) {
470 case eLazyBoolCalculate:
471 s.Printf("not specified.\n");
474 if (m_plan_valid_address_range.GetBaseAddress().IsValid() &&
475 m_plan_valid_address_range.GetByteSize() > 0) {
476 s.PutCString("Address range of this UnwindPlan: ");
477 TargetSP target_sp(thread->CalculateTarget());
478 m_plan_valid_address_range.Dump(&s, target_sp.get(),
479 Address::DumpStyleSectionNameOffset);
482 collection::const_iterator pos, begin = m_row_list.begin(),
483 end = m_row_list.end();
484 for (pos = begin; pos != end; ++pos) {
485 s.Printf("row[%u]: ", (uint32_t)std::distance(begin, pos));
486 (*pos)->Dump(s, this, thread, base_addr);
490 void UnwindPlan::SetSourceName(const char *source) {
491 m_source_name = ConstString(source);
494 ConstString UnwindPlan::GetSourceName() const { return m_source_name; }
496 const RegisterInfo *UnwindPlan::GetRegisterInfo(Thread *thread,
497 uint32_t unwind_reg) const {
499 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
502 if (m_register_kind == eRegisterKindLLDB)
505 reg = reg_ctx->ConvertRegisterKindToRegisterNumber(m_register_kind,
507 if (reg != LLDB_INVALID_REGNUM)
508 return reg_ctx->GetRegisterInfoAtIndex(reg);