1 //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 11 // C Includes 12 // C++ Includes 13 #include <list> 14 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" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) : 32 m_objfile (objfile), 33 m_section_sp (section_sp), 34 m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind) 35 m_flags (), 36 m_cie_map (), 37 m_cfi_data (), 38 m_cfi_data_initialized (false), 39 m_fde_index (), 40 m_fde_index_initialized (false), 41 m_is_eh_frame (is_eh_frame) 42 { 43 } 44 45 DWARFCallFrameInfo::~DWARFCallFrameInfo() 46 { 47 } 48 49 50 bool 51 DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan) 52 { 53 FDEEntryMap::Entry fde_entry; 54 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() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile) 59 return false; 60 61 if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false) 62 return false; 63 return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan); 64 } 65 66 bool 67 DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range) 68 { 69 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() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile) 74 return false; 75 76 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 77 return false; 78 GetFDEIndex(); 79 FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress()); 80 if (!fde_entry) 81 return false; 82 83 range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList()); 84 return true; 85 } 86 87 bool 88 DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry) 89 { 90 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 91 return false; 92 93 GetFDEIndex(); 94 95 if (m_fde_index.IsEmpty()) 96 return false; 97 98 FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr); 99 100 if (fde == NULL) 101 return false; 102 103 fde_entry = *fde; 104 return true; 105 } 106 107 void 108 DWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info) 109 { 110 GetFDEIndex(); 111 const size_t count = m_fde_index.GetSize(); 112 function_info.Clear(); 113 if (count > 0) 114 function_info.Reserve(count); 115 for (size_t i = 0; i < count; ++i) 116 { 117 const FDEEntryMap::Entry *func_offset_data_entry = m_fde_index.GetEntryAtIndex (i); 118 if (func_offset_data_entry) 119 { 120 FunctionAddressAndSizeVector::Entry function_offset_entry (func_offset_data_entry->base, func_offset_data_entry->size); 121 function_info.Append (function_offset_entry); 122 } 123 } 124 } 125 126 const DWARFCallFrameInfo::CIE* 127 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) 128 { 129 cie_map_t::iterator pos = m_cie_map.find(cie_offset); 130 131 if (pos != m_cie_map.end()) 132 { 133 // Parse and cache the CIE 134 if (pos->second.get() == NULL) 135 pos->second = ParseCIE (cie_offset); 136 137 return pos->second.get(); 138 } 139 return NULL; 140 } 141 142 DWARFCallFrameInfo::CIESP 143 DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset) 144 { 145 CIESP cie_sp(new CIE(cie_offset)); 146 lldb::offset_t offset = cie_offset; 147 if (m_cfi_data_initialized == false) 148 GetCFIData(); 149 const uint32_t length = m_cfi_data.GetU32(&offset); 150 const dw_offset_t cie_id = m_cfi_data.GetU32(&offset); 151 const dw_offset_t end_offset = cie_offset + length + 4; 152 if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul))) 153 { 154 size_t i; 155 // cie.offset = cie_offset; 156 // cie.length = length; 157 // cie.cieID = cieID; 158 cie_sp->ptr_encoding = DW_EH_PE_absptr; 159 cie_sp->version = m_cfi_data.GetU8(&offset); 160 161 for (i=0; i<CFI_AUG_MAX_SIZE; ++i) 162 { 163 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); 164 if (cie_sp->augmentation[i] == '\0') 165 { 166 // Zero out remaining bytes in augmentation string 167 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j) 168 cie_sp->augmentation[j] = '\0'; 169 170 break; 171 } 172 } 173 174 if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0') 175 { 176 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); 177 return cie_sp; 178 } 179 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); 180 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); 181 cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset); 182 183 if (cie_sp->augmentation[0]) 184 { 185 // Get the length of the eh_frame augmentation data 186 // which starts with a ULEB128 length in bytes 187 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); 188 const size_t aug_data_end = offset + aug_data_len; 189 const size_t aug_str_len = strlen(cie_sp->augmentation); 190 // A 'z' may be present as the first character of the string. 191 // If present, the Augmentation Data field shall be present. 192 // The contents of the Augmentation Data shall be intepreted 193 // according to other characters in the Augmentation String. 194 if (cie_sp->augmentation[0] == 'z') 195 { 196 // Extract the Augmentation Data 197 size_t aug_str_idx = 0; 198 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) 199 { 200 char aug = cie_sp->augmentation[aug_str_idx]; 201 switch (aug) 202 { 203 case 'L': 204 // Indicates the presence of one argument in the 205 // Augmentation Data of the CIE, and a corresponding 206 // argument in the Augmentation Data of the FDE. The 207 // argument in the Augmentation Data of the CIE is 208 // 1-byte and represents the pointer encoding used 209 // for the argument in the Augmentation Data of the 210 // FDE, which is the address of a language-specific 211 // data area (LSDA). The size of the LSDA pointer is 212 // specified by the pointer encoding used. 213 m_cfi_data.GetU8(&offset); 214 break; 215 216 case 'P': 217 // Indicates the presence of two arguments in the 218 // Augmentation Data of the cie_sp-> The first argument 219 // is 1-byte and represents the pointer encoding 220 // used for the second argument, which is the 221 // address of a personality routine handler. The 222 // size of the personality routine pointer is 223 // specified by the pointer encoding used. 224 { 225 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); 226 m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); 227 } 228 break; 229 230 case 'R': 231 // A 'R' may be present at any position after the 232 // first character of the string. The Augmentation 233 // Data shall include a 1 byte argument that 234 // represents the pointer encoding for the address 235 // pointers used in the FDE. 236 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); 237 break; 238 } 239 } 240 } 241 else if (strcmp(cie_sp->augmentation, "eh") == 0) 242 { 243 // If the Augmentation string has the value "eh", then 244 // the EH Data field shall be present 245 } 246 247 // Set the offset to be the end of the augmentation data just in case 248 // we didn't understand any of the data. 249 offset = (uint32_t)aug_data_end; 250 } 251 252 if (end_offset > offset) 253 { 254 cie_sp->inst_offset = offset; 255 cie_sp->inst_length = end_offset - offset; 256 } 257 while (offset < end_offset) 258 { 259 uint8_t inst = m_cfi_data.GetU8(&offset); 260 uint8_t primary_opcode = inst & 0xC0; 261 uint8_t extended_opcode = inst & 0x3F; 262 263 if (extended_opcode == DW_CFA_def_cfa) 264 { 265 // Takes two unsigned LEB128 operands representing a register 266 // number and a (non-factored) offset. The required action 267 // is to define the current CFA rule to use the provided 268 // register and offset. 269 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 270 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 271 cie_sp->initial_row.SetCFARegister (reg_num); 272 cie_sp->initial_row.SetCFAOffset (op_offset); 273 continue; 274 } 275 if (primary_opcode == DW_CFA_offset) 276 { 277 // 0x80 - high 2 bits are 0x2, lower 6 bits are register. 278 // Takes two arguments: an unsigned LEB128 constant representing a 279 // factored offset and a register number. The required action is to 280 // change the rule for the register indicated by the register number 281 // to be an offset(N) rule with a value of 282 // (N = factored offset * data_align). 283 uint32_t reg_num = extended_opcode; 284 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align; 285 UnwindPlan::Row::RegisterLocation reg_location; 286 reg_location.SetAtCFAPlusOffset(op_offset); 287 cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location); 288 continue; 289 } 290 if (extended_opcode == DW_CFA_nop) 291 { 292 continue; 293 } 294 break; // Stop if we hit an unrecognized opcode 295 } 296 } 297 298 return cie_sp; 299 } 300 301 void 302 DWARFCallFrameInfo::GetCFIData() 303 { 304 if (m_cfi_data_initialized == false) 305 { 306 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 307 if (log) 308 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); 309 m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data); 310 m_cfi_data_initialized = true; 311 } 312 } 313 // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses 314 // of the functions and a pointer back to the function's FDE for later expansion. 315 // Internalize CIEs as we come across them. 316 317 void 318 DWARFCallFrameInfo::GetFDEIndex () 319 { 320 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 321 return; 322 323 if (m_fde_index_initialized) 324 return; 325 326 Mutex::Locker locker(m_fde_index_mutex); 327 328 if (m_fde_index_initialized) // if two threads hit the locker 329 return; 330 331 Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString("")); 332 333 lldb::offset_t offset = 0; 334 if (m_cfi_data_initialized == false) 335 GetCFIData(); 336 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8)) 337 { 338 const dw_offset_t current_entry = offset; 339 uint32_t len = m_cfi_data.GetU32 (&offset); 340 dw_offset_t next_entry = current_entry + len + 4; 341 dw_offset_t cie_id = m_cfi_data.GetU32 (&offset); 342 343 if (cie_id == 0 || cie_id == UINT32_MAX) 344 { 345 m_cie_map[current_entry] = ParseCIE (current_entry); 346 offset = next_entry; 347 continue; 348 } 349 350 const dw_offset_t cie_offset = current_entry + 4 - cie_id; 351 const CIE *cie = GetCIE (cie_offset); 352 if (cie) 353 { 354 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 355 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 356 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 357 358 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); 359 lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr); 360 FDEEntryMap::Entry fde (addr, length, current_entry); 361 m_fde_index.Append(fde); 362 } 363 else 364 { 365 Host::SystemLog (Host::eSystemLogError, 366 "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n", 367 cie_offset, 368 cie_id, 369 current_entry); 370 } 371 offset = next_entry; 372 } 373 m_fde_index.Sort(); 374 m_fde_index_initialized = true; 375 } 376 377 bool 378 DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan) 379 { 380 lldb::offset_t offset = dwarf_offset; 381 lldb::offset_t current_entry = offset; 382 383 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 384 return false; 385 386 if (m_cfi_data_initialized == false) 387 GetCFIData(); 388 389 uint32_t length = m_cfi_data.GetU32 (&offset); 390 dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset); 391 392 assert (cie_offset != 0 && cie_offset != UINT32_MAX); 393 394 // Translate the CIE_id from the eh_frame format, which 395 // is relative to the FDE offset, into a __eh_frame section 396 // offset 397 if (m_is_eh_frame) 398 { 399 unwind_plan.SetSourceName ("eh_frame CFI"); 400 cie_offset = current_entry + 4 - cie_offset; 401 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 402 } 403 else 404 { 405 unwind_plan.SetSourceName ("DWARF CFI"); 406 // In theory the debug_frame info should be valid at all call sites 407 // ("asynchronous unwind info" as it is sometimes called) but in practice 408 // gcc et al all emit call frame info for the prologue and call sites, but 409 // not for the epilogue or all the other locations during the function reliably. 410 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 411 } 412 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 413 414 const CIE *cie = GetCIE (cie_offset); 415 assert (cie != NULL); 416 417 const dw_offset_t end_offset = current_entry + length + 4; 418 419 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 420 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 421 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 422 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); 423 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); 424 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList()); 425 range.SetByteSize (range_len); 426 427 if (cie->augmentation[0] == 'z') 428 { 429 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 430 offset += aug_data_len; 431 } 432 433 uint32_t reg_num = 0; 434 int32_t op_offset = 0; 435 uint32_t code_align = cie->code_align; 436 int32_t data_align = cie->data_align; 437 438 unwind_plan.SetPlanValidAddressRange (range); 439 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; 440 *cie_initial_row = cie->initial_row; 441 UnwindPlan::RowSP row(cie_initial_row); 442 443 unwind_plan.SetRegisterKind (m_reg_kind); 444 unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num); 445 446 UnwindPlan::Row::RegisterLocation reg_location; 447 while (m_cfi_data.ValidOffset(offset) && offset < end_offset) 448 { 449 uint8_t inst = m_cfi_data.GetU8(&offset); 450 uint8_t primary_opcode = inst & 0xC0; 451 uint8_t extended_opcode = inst & 0x3F; 452 453 if (primary_opcode) 454 { 455 switch (primary_opcode) 456 { 457 case DW_CFA_advance_loc : // (Row Creation Instruction) 458 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta 459 // takes a single argument that represents a constant delta. The 460 // required action is to create a new table row with a location 461 // value that is computed by taking the current entry's location 462 // value and adding (delta * code_align). All other 463 // values in the new row are initially identical to the current row. 464 unwind_plan.AppendRow(row); 465 UnwindPlan::Row *newrow = new UnwindPlan::Row; 466 *newrow = *row.get(); 467 row.reset (newrow); 468 row->SlideOffset(extended_opcode * code_align); 469 } 470 break; 471 472 case DW_CFA_offset : 473 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register 474 // takes two arguments: an unsigned LEB128 constant representing a 475 // factored offset and a register number. The required action is to 476 // change the rule for the register indicated by the register number 477 // to be an offset(N) rule with a value of 478 // (N = factored offset * data_align). 479 reg_num = extended_opcode; 480 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 481 reg_location.SetAtCFAPlusOffset(op_offset); 482 row->SetRegisterInfo (reg_num, reg_location); 483 } 484 break; 485 486 case DW_CFA_restore : 487 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register 488 // takes a single argument that represents a register number. The 489 // required action is to change the rule for the indicated register 490 // to the rule assigned it by the initial_instructions in the CIE. 491 reg_num = extended_opcode; 492 // We only keep enough register locations around to 493 // unwind what is in our thread, and these are organized 494 // by the register index in that state, so we need to convert our 495 // GCC register number from the EH frame info, to a register index 496 497 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) 498 row->SetRegisterInfo (reg_num, reg_location); 499 } 500 break; 501 } 502 } 503 else 504 { 505 switch (extended_opcode) 506 { 507 case DW_CFA_nop : // 0x0 508 break; 509 510 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction) 511 { 512 // DW_CFA_set_loc takes a single argument that represents an address. 513 // The required action is to create a new table row using the 514 // specified address as the location. All other values in the new row 515 // are initially identical to the current row. The new location value 516 // should always be greater than the current one. 517 unwind_plan.AppendRow(row); 518 UnwindPlan::Row *newrow = new UnwindPlan::Row; 519 *newrow = *row.get(); 520 row.reset (newrow); 521 row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress()); 522 } 523 break; 524 525 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction) 526 { 527 // takes a single uword argument that represents a constant delta. 528 // This instruction is identical to DW_CFA_advance_loc except for the 529 // encoding and size of the delta argument. 530 unwind_plan.AppendRow(row); 531 UnwindPlan::Row *newrow = new UnwindPlan::Row; 532 *newrow = *row.get(); 533 row.reset (newrow); 534 row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align); 535 } 536 break; 537 538 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction) 539 { 540 // takes a single uword argument that represents a constant delta. 541 // This instruction is identical to DW_CFA_advance_loc except for the 542 // encoding and size of the delta argument. 543 unwind_plan.AppendRow(row); 544 UnwindPlan::Row *newrow = new UnwindPlan::Row; 545 *newrow = *row.get(); 546 row.reset (newrow); 547 row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align); 548 } 549 break; 550 551 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction) 552 { 553 // takes a single uword argument that represents a constant delta. 554 // This instruction is identical to DW_CFA_advance_loc except for the 555 // encoding and size of the delta argument. 556 unwind_plan.AppendRow(row); 557 UnwindPlan::Row *newrow = new UnwindPlan::Row; 558 *newrow = *row.get(); 559 row.reset (newrow); 560 row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align); 561 } 562 break; 563 564 case DW_CFA_offset_extended : // 0x5 565 { 566 // takes two unsigned LEB128 arguments representing a register number 567 // and a factored offset. This instruction is identical to DW_CFA_offset 568 // except for the encoding and size of the register argument. 569 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 570 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 571 reg_location.SetAtCFAPlusOffset(op_offset); 572 row->SetRegisterInfo (reg_num, reg_location); 573 } 574 break; 575 576 case DW_CFA_restore_extended : // 0x6 577 { 578 // takes a single unsigned LEB128 argument that represents a register 579 // number. This instruction is identical to DW_CFA_restore except for 580 // the encoding and size of the register argument. 581 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 582 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) 583 row->SetRegisterInfo (reg_num, reg_location); 584 } 585 break; 586 587 case DW_CFA_undefined : // 0x7 588 { 589 // takes a single unsigned LEB128 argument that represents a register 590 // number. The required action is to set the rule for the specified 591 // register to undefined. 592 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 593 reg_location.SetUndefined(); 594 row->SetRegisterInfo (reg_num, reg_location); 595 } 596 break; 597 598 case DW_CFA_same_value : // 0x8 599 { 600 // takes a single unsigned LEB128 argument that represents a register 601 // number. The required action is to set the rule for the specified 602 // register to same value. 603 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 604 reg_location.SetSame(); 605 row->SetRegisterInfo (reg_num, reg_location); 606 } 607 break; 608 609 case DW_CFA_register : // 0x9 610 { 611 // takes two unsigned LEB128 arguments representing register numbers. 612 // The required action is to set the rule for the first register to be 613 // the second register. 614 615 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 616 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 617 reg_location.SetInRegister(other_reg_num); 618 row->SetRegisterInfo (reg_num, reg_location); 619 } 620 break; 621 622 case DW_CFA_remember_state : // 0xA 623 { 624 // These instructions define a stack of information. Encountering the 625 // DW_CFA_remember_state instruction means to save the rules for every 626 // register on the current row on the stack. Encountering the 627 // DW_CFA_restore_state instruction means to pop the set of rules off 628 // the stack and place them in the current row. (This operation is 629 // useful for compilers that move epilogue code into the body of a 630 // function.) 631 unwind_plan.AppendRow (row); 632 UnwindPlan::Row *newrow = new UnwindPlan::Row; 633 *newrow = *row.get(); 634 row.reset (newrow); 635 } 636 break; 637 638 case DW_CFA_restore_state : // 0xB 639 // These instructions define a stack of information. Encountering the 640 // DW_CFA_remember_state instruction means to save the rules for every 641 // register on the current row on the stack. Encountering the 642 // DW_CFA_restore_state instruction means to pop the set of rules off 643 // the stack and place them in the current row. (This operation is 644 // useful for compilers that move epilogue code into the body of a 645 // function.) 646 { 647 row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1); 648 } 649 break; 650 651 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction) 652 { 653 // Takes two unsigned LEB128 operands representing a register 654 // number and a (non-factored) offset. The required action 655 // is to define the current CFA rule to use the provided 656 // register and offset. 657 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 658 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 659 row->SetCFARegister (reg_num); 660 row->SetCFAOffset (op_offset); 661 } 662 break; 663 664 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction) 665 { 666 // takes a single unsigned LEB128 argument representing a register 667 // number. The required action is to define the current CFA rule to 668 // use the provided register (but to keep the old offset). 669 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 670 row->SetCFARegister (reg_num); 671 } 672 break; 673 674 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction) 675 { 676 // Takes a single unsigned LEB128 operand representing a 677 // (non-factored) offset. The required action is to define 678 // the current CFA rule to use the provided offset (but 679 // to keep the old register). 680 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 681 row->SetCFAOffset (op_offset); 682 } 683 break; 684 685 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction) 686 { 687 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); 688 offset += (uint32_t)block_len; 689 } 690 break; 691 692 case DW_CFA_expression : // 0x10 693 { 694 // Takes two operands: an unsigned LEB128 value representing 695 // a register number, and a DW_FORM_block value representing a DWARF 696 // expression. The required action is to change the rule for the 697 // register indicated by the register number to be an expression(E) 698 // rule where E is the DWARF expression. That is, the DWARF 699 // expression computes the address. The value of the CFA is 700 // pushed on the DWARF evaluation stack prior to execution of 701 // the DWARF expression. 702 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 703 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 704 const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len); 705 706 reg_location.SetAtDWARFExpression(block_data, block_len); 707 row->SetRegisterInfo (reg_num, reg_location); 708 } 709 break; 710 711 case DW_CFA_offset_extended_sf : // 0x11 712 { 713 // takes two operands: an unsigned LEB128 value representing a 714 // register number and a signed LEB128 factored offset. This 715 // instruction is identical to DW_CFA_offset_extended except 716 //that the second operand is signed and factored. 717 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 718 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 719 reg_location.SetAtCFAPlusOffset(op_offset); 720 row->SetRegisterInfo (reg_num, reg_location); 721 } 722 break; 723 724 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction) 725 { 726 // Takes two operands: an unsigned LEB128 value representing 727 // a register number and a signed LEB128 factored offset. 728 // This instruction is identical to DW_CFA_def_cfa except 729 // that the second operand is signed and factored. 730 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 731 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 732 row->SetCFARegister (reg_num); 733 row->SetCFAOffset (op_offset); 734 } 735 break; 736 737 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction) 738 { 739 // takes a signed LEB128 operand representing a factored 740 // offset. This instruction is identical to DW_CFA_def_cfa_offset 741 // except that the operand is signed and factored. 742 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 743 row->SetCFAOffset (op_offset); 744 } 745 break; 746 747 case DW_CFA_val_expression : // 0x16 748 { 749 // takes two operands: an unsigned LEB128 value representing a register 750 // number, and a DW_FORM_block value representing a DWARF expression. 751 // The required action is to change the rule for the register indicated 752 // by the register number to be a val_expression(E) rule where E is the 753 // DWARF expression. That is, the DWARF expression computes the value of 754 // the given register. The value of the CFA is pushed on the DWARF 755 // evaluation stack prior to execution of the DWARF expression. 756 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 757 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 758 const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len); 759 //#if defined(__i386__) || defined(__x86_64__) 760 // // The EH frame info for EIP and RIP contains code that looks for traps to 761 // // be a specific type and increments the PC. 762 // // For i386: 763 // // DW_CFA_val_expression where: 764 // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34), 765 // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref, 766 // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, 767 // // DW_OP_and, DW_OP_plus 768 // // This basically does a: 769 // // eip = ucontenxt.mcontext32->gpr.eip; 770 // // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4) 771 // // eip++; 772 // // 773 // // For x86_64: 774 // // DW_CFA_val_expression where: 775 // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref, 776 // // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3, 777 // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus 778 // // This basically does a: 779 // // rip = ucontenxt.mcontext64->gpr.rip; 780 // // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4) 781 // // rip++; 782 // // The trap comparisons and increments are not needed as it hoses up the unwound PC which 783 // // is expected to point at least past the instruction that causes the fault/trap. So we 784 // // take it out by trimming the expression right at the first "DW_OP_swap" opcodes 785 // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num) 786 // { 787 // if (thread->Is64Bit()) 788 // { 789 // if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst) 790 // block_len = 8; 791 // } 792 // else 793 // { 794 // if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst) 795 // block_len = 7; 796 // } 797 // } 798 //#endif 799 reg_location.SetIsDWARFExpression(block_data, block_len); 800 row->SetRegisterInfo (reg_num, reg_location); 801 } 802 break; 803 804 case DW_CFA_val_offset : // 0x14 805 case DW_CFA_val_offset_sf : // 0x15 806 default: 807 break; 808 } 809 } 810 } 811 unwind_plan.AppendRow(row); 812 813 return true; 814 } 815