1 //===-- DWARFCallFrameInfo.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 //===----------------------------------------------------------------------===//
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Core/Timer.h"
21 #include "lldb/Host/Host.h"
22 #include "lldb/Symbol/DWARFCallFrameInfo.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/UnwindPlan.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/Thread.h"
29 using namespace lldb_private;
31 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) :
33 m_section_sp (section_sp),
34 m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind)
38 m_cfi_data_initialized (false),
40 m_fde_index_initialized (false),
41 m_is_eh_frame (is_eh_frame)
45 DWARFCallFrameInfo::~DWARFCallFrameInfo()
51 DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan)
53 FDEEntryMap::Entry fde_entry;
55 // Make sure that the Address we're searching for is the same object file
56 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
57 ModuleSP module_sp = addr.GetModule();
58 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile)
61 if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false)
63 return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan);
67 DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
70 // Make sure that the Address we're searching for is the same object file
71 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
72 ModuleSP module_sp = addr.GetModule();
73 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile)
76 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
79 FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress());
83 range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList());
88 DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry)
90 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
95 if (m_fde_index.IsEmpty())
98 FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr);
108 DWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info)
111 const size_t count = m_fde_index.GetSize();
112 function_info.Clear();
114 function_info.Reserve(count);
115 for (size_t i = 0; i < count; ++i)
117 const FDEEntryMap::Entry *func_offset_data_entry = m_fde_index.GetEntryAtIndex (i);
118 if (func_offset_data_entry)
120 FunctionAddressAndSizeVector::Entry function_offset_entry (func_offset_data_entry->base, func_offset_data_entry->size);
121 function_info.Append (function_offset_entry);
126 const DWARFCallFrameInfo::CIE*
127 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
129 cie_map_t::iterator pos = m_cie_map.find(cie_offset);
131 if (pos != m_cie_map.end())
133 // Parse and cache the CIE
134 if (pos->second.get() == nullptr)
135 pos->second = ParseCIE (cie_offset);
137 return pos->second.get();
142 DWARFCallFrameInfo::CIESP
143 DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
145 CIESP cie_sp(new CIE(cie_offset));
146 lldb::offset_t offset = cie_offset;
147 if (m_cfi_data_initialized == false)
149 uint32_t length = m_cfi_data.GetU32(&offset);
150 dw_offset_t cie_id, end_offset;
151 bool is_64bit = (length == UINT32_MAX);
153 length = m_cfi_data.GetU64(&offset);
154 cie_id = m_cfi_data.GetU64(&offset);
155 end_offset = cie_offset + length + 12;
157 cie_id = m_cfi_data.GetU32(&offset);
158 end_offset = cie_offset + length + 4;
160 if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul)))
163 // cie.offset = cie_offset;
164 // cie.length = length;
165 // cie.cieID = cieID;
166 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
167 cie_sp->version = m_cfi_data.GetU8(&offset);
169 for (i=0; i<CFI_AUG_MAX_SIZE; ++i)
171 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
172 if (cie_sp->augmentation[i] == '\0')
174 // Zero out remaining bytes in augmentation string
175 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j)
176 cie_sp->augmentation[j] = '\0';
182 if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0')
184 Host::SystemLog (Host::eSystemLogError, "CIE parse error: CIE augmentation string was too large for the fixed sized buffer of %d bytes.\n", CFI_AUG_MAX_SIZE);
187 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
188 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
189 cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
191 if (cie_sp->augmentation[0])
193 // Get the length of the eh_frame augmentation data
194 // which starts with a ULEB128 length in bytes
195 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
196 const size_t aug_data_end = offset + aug_data_len;
197 const size_t aug_str_len = strlen(cie_sp->augmentation);
198 // A 'z' may be present as the first character of the string.
199 // If present, the Augmentation Data field shall be present.
200 // The contents of the Augmentation Data shall be interpreted
201 // according to other characters in the Augmentation String.
202 if (cie_sp->augmentation[0] == 'z')
204 // Extract the Augmentation Data
205 size_t aug_str_idx = 0;
206 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++)
208 char aug = cie_sp->augmentation[aug_str_idx];
212 // Indicates the presence of one argument in the
213 // Augmentation Data of the CIE, and a corresponding
214 // argument in the Augmentation Data of the FDE. The
215 // argument in the Augmentation Data of the CIE is
216 // 1-byte and represents the pointer encoding used
217 // for the argument in the Augmentation Data of the
218 // FDE, which is the address of a language-specific
219 // data area (LSDA). The size of the LSDA pointer is
220 // specified by the pointer encoding used.
221 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
225 // Indicates the presence of two arguments in the
226 // Augmentation Data of the CIE. The first argument
227 // is 1-byte and represents the pointer encoding
228 // used for the second argument, which is the
229 // address of a personality routine handler. The
230 // size of the personality routine pointer is
231 // specified by the pointer encoding used.
233 // The address of the personality function will
234 // be stored at this location. Pre-execution, it
235 // will be all zero's so don't read it until we're
236 // trying to do an unwind & the reloc has been
239 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
240 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
241 cie_sp->personality_loc = m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, pc_rel_addr, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
246 // A 'R' may be present at any position after the
247 // first character of the string. The Augmentation
248 // Data shall include a 1 byte argument that
249 // represents the pointer encoding for the address
250 // pointers used in the FDE.
251 // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
252 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
257 else if (strcmp(cie_sp->augmentation, "eh") == 0)
259 // If the Augmentation string has the value "eh", then
260 // the EH Data field shall be present
263 // Set the offset to be the end of the augmentation data just in case
264 // we didn't understand any of the data.
265 offset = (uint32_t)aug_data_end;
268 if (end_offset > offset)
270 cie_sp->inst_offset = offset;
271 cie_sp->inst_length = end_offset - offset;
273 while (offset < end_offset)
275 uint8_t inst = m_cfi_data.GetU8(&offset);
276 uint8_t primary_opcode = inst & 0xC0;
277 uint8_t extended_opcode = inst & 0x3F;
279 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, cie_sp->data_align, offset, cie_sp->initial_row))
280 break; // Stop if we hit an unrecognized opcode
288 DWARFCallFrameInfo::GetCFIData()
290 if (m_cfi_data_initialized == false)
292 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
294 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
295 m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
296 m_cfi_data_initialized = true;
299 // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
300 // of the functions and a pointer back to the function's FDE for later expansion.
301 // Internalize CIEs as we come across them.
304 DWARFCallFrameInfo::GetFDEIndex ()
306 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
309 if (m_fde_index_initialized)
312 std::lock_guard<std::mutex> guard(m_fde_index_mutex);
314 if (m_fde_index_initialized) // if two threads hit the locker
317 Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString(""));
319 bool clear_address_zeroth_bit = false;
321 if (m_objfile.GetArchitecture (arch))
323 if (arch.GetTriple().getArch() == llvm::Triple::arm || arch.GetTriple().getArch() == llvm::Triple::thumb)
324 clear_address_zeroth_bit = true;
327 lldb::offset_t offset = 0;
328 if (m_cfi_data_initialized == false)
330 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
332 const dw_offset_t current_entry = offset;
333 dw_offset_t cie_id, next_entry, cie_offset;
334 uint32_t len = m_cfi_data.GetU32 (&offset);
335 bool is_64bit = (len == UINT32_MAX);
337 len = m_cfi_data.GetU64 (&offset);
338 cie_id = m_cfi_data.GetU64 (&offset);
339 next_entry = current_entry + len + 12;
340 cie_offset = current_entry + 12 - cie_id;
342 cie_id = m_cfi_data.GetU32 (&offset);
343 next_entry = current_entry + len + 4;
344 cie_offset = current_entry + 4 - cie_id;
347 if (next_entry > m_cfi_data.GetByteSize() + 1)
349 Host::SystemLog (Host::eSystemLogError,
350 "error: Invalid fde/cie next entry offset of 0x%x found in cie/fde at 0x%x\n",
353 // Don't trust anything in this eh_frame section if we find blatantly
356 m_fde_index_initialized = true;
359 if (cie_offset > m_cfi_data.GetByteSize())
361 Host::SystemLog (Host::eSystemLogError,
362 "error: Invalid cie offset of 0x%x found in cie/fde at 0x%x\n",
365 // Don't trust anything in this eh_frame section if we find blatantly
368 m_fde_index_initialized = true;
372 if (cie_id == 0 || cie_id == UINT32_MAX || len == 0)
374 m_cie_map[current_entry] = ParseCIE (current_entry);
379 const CIE *cie = GetCIE (cie_offset);
382 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
383 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
384 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
386 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
387 if (clear_address_zeroth_bit)
390 lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
391 FDEEntryMap::Entry fde (addr, length, current_entry);
392 m_fde_index.Append(fde);
396 Host::SystemLog (Host::eSystemLogError,
397 "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
405 m_fde_index_initialized = true;
409 DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan)
411 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
412 lldb::offset_t offset = dwarf_offset;
413 lldb::offset_t current_entry = offset;
415 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
418 if (m_cfi_data_initialized == false)
421 uint32_t length = m_cfi_data.GetU32 (&offset);
422 dw_offset_t cie_offset;
423 bool is_64bit = (length == UINT32_MAX);
425 length = m_cfi_data.GetU64 (&offset);
426 cie_offset = m_cfi_data.GetU64 (&offset);
428 cie_offset = m_cfi_data.GetU32 (&offset);
431 assert (cie_offset != 0 && cie_offset != UINT32_MAX);
433 // Translate the CIE_id from the eh_frame format, which
434 // is relative to the FDE offset, into a __eh_frame section
438 unwind_plan.SetSourceName ("eh_frame CFI");
439 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
440 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
444 unwind_plan.SetSourceName ("DWARF CFI");
445 // In theory the debug_frame info should be valid at all call sites
446 // ("asynchronous unwind info" as it is sometimes called) but in practice
447 // gcc et al all emit call frame info for the prologue and call sites, but
448 // not for the epilogue or all the other locations during the function reliably.
449 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
451 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
453 const CIE *cie = GetCIE (cie_offset);
454 assert (cie != nullptr);
456 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
458 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
459 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
460 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
461 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
462 lldb::addr_t range_len = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
463 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
464 range.SetByteSize (range_len);
466 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
468 if (cie->augmentation[0] == 'z')
470 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
471 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit)
473 offset_t saved_offset = offset;
474 lsda_data_file_address = m_cfi_data.GetGNUEHPointer(&offset, cie->lsda_addr_encoding, pc_rel_addr, text_addr, data_addr);
475 if (offset - saved_offset != aug_data_len)
477 // There is more in the augmentation region than we know how to process;
478 // don't read anything.
479 lsda_data_file_address = LLDB_INVALID_ADDRESS;
481 offset = saved_offset;
483 offset += aug_data_len;
486 Address personality_function_ptr;
488 if (lsda_data_file_address != LLDB_INVALID_ADDRESS && cie->personality_loc != LLDB_INVALID_ADDRESS)
490 m_objfile.GetModule()->ResolveFileAddress (lsda_data_file_address, lsda_data);
491 m_objfile.GetModule()->ResolveFileAddress (cie->personality_loc, personality_function_ptr);
494 if (lsda_data.IsValid() && personality_function_ptr.IsValid())
496 unwind_plan.SetLSDAAddress (lsda_data);
497 unwind_plan.SetPersonalityFunctionPtr (personality_function_ptr);
500 uint32_t code_align = cie->code_align;
501 int32_t data_align = cie->data_align;
503 unwind_plan.SetPlanValidAddressRange (range);
504 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
505 *cie_initial_row = cie->initial_row;
506 UnwindPlan::RowSP row(cie_initial_row);
508 unwind_plan.SetRegisterKind (m_reg_kind);
509 unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num);
511 std::vector<UnwindPlan::RowSP> stack;
513 UnwindPlan::Row::RegisterLocation reg_location;
514 while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
516 uint8_t inst = m_cfi_data.GetU8(&offset);
517 uint8_t primary_opcode = inst & 0xC0;
518 uint8_t extended_opcode = inst & 0x3F;
520 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align, offset, *row))
524 switch (primary_opcode)
526 case DW_CFA_advance_loc : // (Row Creation Instruction)
527 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
528 // takes a single argument that represents a constant delta. The
529 // required action is to create a new table row with a location
530 // value that is computed by taking the current entry's location
531 // value and adding (delta * code_align). All other
532 // values in the new row are initially identical to the current row.
533 unwind_plan.AppendRow(row);
534 UnwindPlan::Row *newrow = new UnwindPlan::Row;
535 *newrow = *row.get();
537 row->SlideOffset(extended_opcode * code_align);
541 case DW_CFA_restore :
542 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
543 // takes a single argument that represents a register number. The
544 // required action is to change the rule for the indicated register
545 // to the rule assigned it by the initial_instructions in the CIE.
546 uint32_t reg_num = extended_opcode;
547 // We only keep enough register locations around to
548 // unwind what is in our thread, and these are organized
549 // by the register index in that state, so we need to convert our
550 // eh_frame register number from the EH frame info, to a register index
552 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
553 row->SetRegisterInfo (reg_num, reg_location);
560 switch (extended_opcode)
562 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction)
564 // DW_CFA_set_loc takes a single argument that represents an address.
565 // The required action is to create a new table row using the
566 // specified address as the location. All other values in the new row
567 // are initially identical to the current row. The new location value
568 // should always be greater than the current one.
569 unwind_plan.AppendRow(row);
570 UnwindPlan::Row *newrow = new UnwindPlan::Row;
571 *newrow = *row.get();
573 row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
577 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction)
579 // takes a single uword argument that represents a constant delta.
580 // This instruction is identical to DW_CFA_advance_loc except for the
581 // encoding and size of the delta argument.
582 unwind_plan.AppendRow(row);
583 UnwindPlan::Row *newrow = new UnwindPlan::Row;
584 *newrow = *row.get();
586 row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
590 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction)
592 // takes a single uword argument that represents a constant delta.
593 // This instruction is identical to DW_CFA_advance_loc except for the
594 // encoding and size of the delta argument.
595 unwind_plan.AppendRow(row);
596 UnwindPlan::Row *newrow = new UnwindPlan::Row;
597 *newrow = *row.get();
599 row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
603 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction)
605 // takes a single uword argument that represents a constant delta.
606 // This instruction is identical to DW_CFA_advance_loc except for the
607 // encoding and size of the delta argument.
608 unwind_plan.AppendRow(row);
609 UnwindPlan::Row *newrow = new UnwindPlan::Row;
610 *newrow = *row.get();
612 row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
616 case DW_CFA_restore_extended : // 0x6
618 // takes a single unsigned LEB128 argument that represents a register
619 // number. This instruction is identical to DW_CFA_restore except for
620 // the encoding and size of the register argument.
621 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
622 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
623 row->SetRegisterInfo (reg_num, reg_location);
627 case DW_CFA_remember_state : // 0xA
629 // These instructions define a stack of information. Encountering the
630 // DW_CFA_remember_state instruction means to save the rules for every
631 // register on the current row on the stack. Encountering the
632 // DW_CFA_restore_state instruction means to pop the set of rules off
633 // the stack and place them in the current row. (This operation is
634 // useful for compilers that move epilogue code into the body of a
636 stack.push_back (row);
637 UnwindPlan::Row *newrow = new UnwindPlan::Row;
638 *newrow = *row.get();
643 case DW_CFA_restore_state : // 0xB
645 // These instructions define a stack of information. Encountering the
646 // DW_CFA_remember_state instruction means to save the rules for every
647 // register on the current row on the stack. Encountering the
648 // DW_CFA_restore_state instruction means to pop the set of rules off
649 // the stack and place them in the current row. (This operation is
650 // useful for compilers that move epilogue code into the body of a
656 "DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32 ", startaddr: %" PRIx64
657 " encountered DW_CFA_restore_state but state stack is empty. Corrupt unwind info?",
658 __FUNCTION__, dwarf_offset, startaddr.GetFileAddress());
661 lldb::addr_t offset = row->GetOffset ();
664 row->SetOffset (offset);
668 case DW_CFA_GNU_args_size: // 0x2e
670 // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128 operand
671 // representing an argument size. This instruction specifies the total of
672 // the size of the arguments which have been pushed onto the stack.
674 // TODO: Figure out how we should handle this.
675 m_cfi_data.GetULEB128(&offset);
679 case DW_CFA_val_offset : // 0x14
680 case DW_CFA_val_offset_sf : // 0x15
687 unwind_plan.AppendRow(row);
693 DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
694 uint8_t extended_opcode,
696 lldb::offset_t& offset,
697 UnwindPlan::Row& row)
699 UnwindPlan::Row::RegisterLocation reg_location;
703 switch (primary_opcode)
706 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register
707 // takes two arguments: an unsigned LEB128 constant representing a
708 // factored offset and a register number. The required action is to
709 // change the rule for the register indicated by the register number
710 // to be an offset(N) rule with a value of
711 // (N = factored offset * data_align).
712 uint8_t reg_num = extended_opcode;
713 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
714 reg_location.SetAtCFAPlusOffset(op_offset);
715 row.SetRegisterInfo(reg_num, reg_location);
722 switch (extended_opcode)
724 case DW_CFA_nop : // 0x0
727 case DW_CFA_offset_extended : // 0x5
729 // takes two unsigned LEB128 arguments representing a register number
730 // and a factored offset. This instruction is identical to DW_CFA_offset
731 // except for the encoding and size of the register argument.
732 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
733 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
734 UnwindPlan::Row::RegisterLocation reg_location;
735 reg_location.SetAtCFAPlusOffset(op_offset);
736 row.SetRegisterInfo(reg_num, reg_location);
740 case DW_CFA_undefined : // 0x7
742 // takes a single unsigned LEB128 argument that represents a register
743 // number. The required action is to set the rule for the specified
744 // register to undefined.
745 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
746 UnwindPlan::Row::RegisterLocation reg_location;
747 reg_location.SetUndefined();
748 row.SetRegisterInfo(reg_num, reg_location);
752 case DW_CFA_same_value : // 0x8
754 // takes a single unsigned LEB128 argument that represents a register
755 // number. The required action is to set the rule for the specified
756 // register to same value.
757 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
758 UnwindPlan::Row::RegisterLocation reg_location;
759 reg_location.SetSame();
760 row.SetRegisterInfo (reg_num, reg_location);
764 case DW_CFA_register : // 0x9
766 // takes two unsigned LEB128 arguments representing register numbers.
767 // The required action is to set the rule for the first register to be
768 // the second register.
769 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
770 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
771 UnwindPlan::Row::RegisterLocation reg_location;
772 reg_location.SetInRegister(other_reg_num);
773 row.SetRegisterInfo (reg_num, reg_location);
777 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction)
779 // Takes two unsigned LEB128 operands representing a register
780 // number and a (non-factored) offset. The required action
781 // is to define the current CFA rule to use the provided
782 // register and offset.
783 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
784 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
785 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset);
789 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction)
791 // takes a single unsigned LEB128 argument representing a register
792 // number. The required action is to define the current CFA rule to
793 // use the provided register (but to keep the old offset).
794 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
795 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, row.GetCFAValue().GetOffset());
799 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction)
801 // Takes a single unsigned LEB128 operand representing a
802 // (non-factored) offset. The required action is to define
803 // the current CFA rule to use the provided offset (but
804 // to keep the old register).
805 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
806 row.GetCFAValue().SetIsRegisterPlusOffset(row.GetCFAValue().GetRegisterNumber(), op_offset);
810 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction)
812 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
813 const uint8_t *block_data = static_cast<const uint8_t*>(m_cfi_data.GetData(&offset, block_len));
814 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
818 case DW_CFA_expression : // 0x10
820 // Takes two operands: an unsigned LEB128 value representing
821 // a register number, and a DW_FORM_block value representing a DWARF
822 // expression. The required action is to change the rule for the
823 // register indicated by the register number to be an expression(E)
824 // rule where E is the DWARF expression. That is, the DWARF
825 // expression computes the address. The value of the CFA is
826 // pushed on the DWARF evaluation stack prior to execution of
827 // the DWARF expression.
828 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
829 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
830 const uint8_t *block_data = static_cast<const uint8_t*>(m_cfi_data.GetData(&offset, block_len));
831 UnwindPlan::Row::RegisterLocation reg_location;
832 reg_location.SetAtDWARFExpression(block_data, block_len);
833 row.SetRegisterInfo(reg_num, reg_location);
837 case DW_CFA_offset_extended_sf : // 0x11
839 // takes two operands: an unsigned LEB128 value representing a
840 // register number and a signed LEB128 factored offset. This
841 // instruction is identical to DW_CFA_offset_extended except
842 //that the second operand is signed and factored.
843 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
844 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
845 UnwindPlan::Row::RegisterLocation reg_location;
846 reg_location.SetAtCFAPlusOffset(op_offset);
847 row.SetRegisterInfo(reg_num, reg_location);
851 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction)
853 // Takes two operands: an unsigned LEB128 value representing
854 // a register number and a signed LEB128 factored offset.
855 // This instruction is identical to DW_CFA_def_cfa except
856 // that the second operand is signed and factored.
857 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
858 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
859 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset);
863 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction)
865 // takes a signed LEB128 operand representing a factored
866 // offset. This instruction is identical to DW_CFA_def_cfa_offset
867 // except that the operand is signed and factored.
868 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
869 uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
870 row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
874 case DW_CFA_val_expression : // 0x16
876 // takes two operands: an unsigned LEB128 value representing a register
877 // number, and a DW_FORM_block value representing a DWARF expression.
878 // The required action is to change the rule for the register indicated
879 // by the register number to be a val_expression(E) rule where E is the
880 // DWARF expression. That is, the DWARF expression computes the value of
881 // the given register. The value of the CFA is pushed on the DWARF
882 // evaluation stack prior to execution of the DWARF expression.
883 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
884 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
885 const uint8_t* block_data = (const uint8_t*)m_cfi_data.GetData(&offset, block_len);
886 //#if defined(__i386__) || defined(__x86_64__)
887 // // The EH frame info for EIP and RIP contains code that looks for traps to
888 // // be a specific type and increments the PC.
890 // // DW_CFA_val_expression where:
891 // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
892 // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
893 // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
894 // // DW_OP_and, DW_OP_plus
895 // // This basically does a:
896 // // eip = ucontenxt.mcontext32->gpr.eip;
897 // // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
901 // // DW_CFA_val_expression where:
902 // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
903 // // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
904 // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
905 // // This basically does a:
906 // // rip = ucontenxt.mcontext64->gpr.rip;
907 // // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
909 // // The trap comparisons and increments are not needed as it hoses up the unwound PC which
910 // // is expected to point at least past the instruction that causes the fault/trap. So we
911 // // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
912 // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
914 // if (thread->Is64Bit())
916 // if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
921 // if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
926 reg_location.SetIsDWARFExpression(block_data, block_len);
927 row.SetRegisterInfo (reg_num, reg_location);
936 DWARFCallFrameInfo::ForEachFDEEntries(
937 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)>& callback)
941 for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i)
943 const FDEEntryMap::Entry& entry = m_fde_index.GetEntryRef(i);
944 if (!callback(entry.base, entry.size, entry.data))