1 //===-- ProcessElfCore.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 // C Includes 11 #include <stdlib.h> 12 13 // C++ Includes 14 #include <mutex> 15 16 // Other libraries and framework includes 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/ModuleSpec.h" 20 #include "lldb/Core/Section.h" 21 #include "lldb/Core/State.h" 22 #include "lldb/Core/DataBufferHeap.h" 23 #include "lldb/Core/Log.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Target/DynamicLoader.h" 26 #include "lldb/Target/UnixSignals.h" 27 28 #include "llvm/Support/ELF.h" 29 30 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h" 31 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h" 32 33 // Project includes 34 #include "ProcessElfCore.h" 35 #include "ThreadElfCore.h" 36 37 using namespace lldb_private; 38 39 ConstString 40 ProcessElfCore::GetPluginNameStatic() 41 { 42 static ConstString g_name("elf-core"); 43 return g_name; 44 } 45 46 const char * 47 ProcessElfCore::GetPluginDescriptionStatic() 48 { 49 return "ELF core dump plug-in."; 50 } 51 52 void 53 ProcessElfCore::Terminate() 54 { 55 PluginManager::UnregisterPlugin (ProcessElfCore::CreateInstance); 56 } 57 58 59 lldb::ProcessSP 60 ProcessElfCore::CreateInstance (lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) 61 { 62 lldb::ProcessSP process_sp; 63 if (crash_file) 64 { 65 // Read enough data for a ELF32 header or ELF64 header 66 const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr); 67 68 lldb::DataBufferSP data_sp (crash_file->ReadFileContents(0, header_size)); 69 if (data_sp && data_sp->GetByteSize() == header_size && 70 elf::ELFHeader::MagicBytesMatch (data_sp->GetBytes())) 71 { 72 elf::ELFHeader elf_header; 73 DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); 74 lldb::offset_t data_offset = 0; 75 if (elf_header.Parse(data, &data_offset)) 76 { 77 if (elf_header.e_type == llvm::ELF::ET_CORE) 78 process_sp.reset(new ProcessElfCore (target_sp, listener, *crash_file)); 79 } 80 } 81 } 82 return process_sp; 83 } 84 85 bool 86 ProcessElfCore::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) 87 { 88 // For now we are just making sure the file exists for a given module 89 if (!m_core_module_sp && m_core_file.Exists()) 90 { 91 ModuleSpec core_module_spec(m_core_file, target_sp->GetArchitecture()); 92 Error error (ModuleList::GetSharedModule (core_module_spec, m_core_module_sp, 93 NULL, NULL, NULL)); 94 if (m_core_module_sp) 95 { 96 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 97 if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile) 98 return true; 99 } 100 } 101 return false; 102 } 103 104 //---------------------------------------------------------------------- 105 // ProcessElfCore constructor 106 //---------------------------------------------------------------------- 107 ProcessElfCore::ProcessElfCore(lldb::TargetSP target_sp, Listener &listener, 108 const FileSpec &core_file) : 109 Process (target_sp, listener), 110 m_core_module_sp (), 111 m_core_file (core_file), 112 m_dyld_plugin_name (), 113 m_os(llvm::Triple::UnknownOS), 114 m_thread_data_valid(false), 115 m_thread_data(), 116 m_core_aranges () 117 { 118 } 119 120 //---------------------------------------------------------------------- 121 // Destructor 122 //---------------------------------------------------------------------- 123 ProcessElfCore::~ProcessElfCore() 124 { 125 Clear(); 126 // We need to call finalize on the process before destroying ourselves 127 // to make sure all of the broadcaster cleanup goes as planned. If we 128 // destruct this class, then Process::~Process() might have problems 129 // trying to fully destroy the broadcaster. 130 Finalize(); 131 } 132 133 //---------------------------------------------------------------------- 134 // PluginInterface 135 //---------------------------------------------------------------------- 136 ConstString 137 ProcessElfCore::GetPluginName() 138 { 139 return GetPluginNameStatic(); 140 } 141 142 uint32_t 143 ProcessElfCore::GetPluginVersion() 144 { 145 return 1; 146 } 147 148 lldb::addr_t 149 ProcessElfCore::AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader *header) 150 { 151 lldb::addr_t addr = header->p_vaddr; 152 FileRange file_range (header->p_offset, header->p_filesz); 153 VMRangeToFileOffset::Entry range_entry(addr, header->p_memsz, file_range); 154 155 VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back(); 156 if (last_entry && 157 last_entry->GetRangeEnd() == range_entry.GetRangeBase() && 158 last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase() && 159 last_entry->GetByteSize() == last_entry->data.GetByteSize()) 160 { 161 last_entry->SetRangeEnd (range_entry.GetRangeEnd()); 162 last_entry->data.SetRangeEnd (range_entry.data.GetRangeEnd()); 163 } 164 else 165 { 166 m_core_aranges.Append(range_entry); 167 } 168 169 return addr; 170 } 171 172 //---------------------------------------------------------------------- 173 // Process Control 174 //---------------------------------------------------------------------- 175 Error 176 ProcessElfCore::DoLoadCore () 177 { 178 Error error; 179 if (!m_core_module_sp) 180 { 181 error.SetErrorString ("invalid core module"); 182 return error; 183 } 184 185 ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile()); 186 if (core == NULL) 187 { 188 error.SetErrorString ("invalid core object file"); 189 return error; 190 } 191 192 const uint32_t num_segments = core->GetProgramHeaderCount(); 193 if (num_segments == 0) 194 { 195 error.SetErrorString ("core file has no segments"); 196 return error; 197 } 198 199 SetCanJIT(false); 200 201 m_thread_data_valid = true; 202 203 bool ranges_are_sorted = true; 204 lldb::addr_t vm_addr = 0; 205 /// Walk through segments and Thread and Address Map information. 206 /// PT_NOTE - Contains Thread and Register information 207 /// PT_LOAD - Contains a contiguous range of Process Address Space 208 for(uint32_t i = 1; i <= num_segments; i++) 209 { 210 const elf::ELFProgramHeader *header = core->GetProgramHeaderByIndex(i); 211 assert(header != NULL); 212 213 DataExtractor data = core->GetSegmentDataByIndex(i); 214 215 // Parse thread contexts and auxv structure 216 if (header->p_type == llvm::ELF::PT_NOTE) 217 ParseThreadContextsFromNoteSegment(header, data); 218 219 // PT_LOAD segments contains address map 220 if (header->p_type == llvm::ELF::PT_LOAD) 221 { 222 lldb::addr_t last_addr = AddAddressRangeFromLoadSegment(header); 223 if (vm_addr > last_addr) 224 ranges_are_sorted = false; 225 vm_addr = last_addr; 226 } 227 } 228 229 if (!ranges_are_sorted) 230 m_core_aranges.Sort(); 231 232 // Even if the architecture is set in the target, we need to override 233 // it to match the core file which is always single arch. 234 ArchSpec arch (m_core_module_sp->GetArchitecture()); 235 if (arch.IsValid()) 236 GetTarget().SetArchitecture(arch); 237 238 SetUnixSignals(UnixSignals::Create(GetArchitecture())); 239 240 return error; 241 } 242 243 lldb_private::DynamicLoader * 244 ProcessElfCore::GetDynamicLoader () 245 { 246 if (m_dyld_ap.get() == NULL) 247 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderPOSIXDYLD::GetPluginNameStatic().GetCString())); 248 return m_dyld_ap.get(); 249 } 250 251 bool 252 ProcessElfCore::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 253 { 254 const uint32_t num_threads = GetNumThreadContexts (); 255 if (!m_thread_data_valid) 256 return false; 257 258 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) 259 { 260 const ThreadData &td = m_thread_data[tid]; 261 lldb::ThreadSP thread_sp(new ThreadElfCore (*this, td)); 262 new_thread_list.AddThread (thread_sp); 263 } 264 return new_thread_list.GetSize(false) > 0; 265 } 266 267 void 268 ProcessElfCore::RefreshStateAfterStop () 269 { 270 } 271 272 Error 273 ProcessElfCore::DoDestroy () 274 { 275 return Error(); 276 } 277 278 //------------------------------------------------------------------ 279 // Process Queries 280 //------------------------------------------------------------------ 281 282 bool 283 ProcessElfCore::IsAlive () 284 { 285 return true; 286 } 287 288 //------------------------------------------------------------------ 289 // Process Memory 290 //------------------------------------------------------------------ 291 size_t 292 ProcessElfCore::ReadMemory (lldb::addr_t addr, void *buf, size_t size, Error &error) 293 { 294 // Don't allow the caching that lldb_private::Process::ReadMemory does 295 // since in core files we have it all cached our our core file anyway. 296 return DoReadMemory (addr, buf, size, error); 297 } 298 299 size_t 300 ProcessElfCore::DoReadMemory (lldb::addr_t addr, void *buf, size_t size, Error &error) 301 { 302 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 303 304 if (core_objfile == NULL) 305 return 0; 306 307 // Get the address range 308 const VMRangeToFileOffset::Entry *address_range = m_core_aranges.FindEntryThatContains (addr); 309 if (address_range == NULL || address_range->GetRangeEnd() < addr) 310 { 311 error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr); 312 return 0; 313 } 314 315 // Convert the address into core file offset 316 const lldb::addr_t offset = addr - address_range->GetRangeBase(); 317 const lldb::addr_t file_start = address_range->data.GetRangeBase(); 318 const lldb::addr_t file_end = address_range->data.GetRangeEnd(); 319 size_t bytes_to_read = size; // Number of bytes to read from the core file 320 size_t bytes_copied = 0; // Number of bytes actually read from the core file 321 size_t zero_fill_size = 0; // Padding 322 lldb::addr_t bytes_left = 0; // Number of bytes available in the core file from the given address 323 324 // Figure out how many on-disk bytes remain in this segment 325 // starting at the given offset 326 if (file_end > file_start + offset) 327 bytes_left = file_end - (file_start + offset); 328 329 // Figure out how many bytes we need to zero-fill if we are 330 // reading more bytes than available in the on-disk segment 331 if (bytes_to_read > bytes_left) 332 { 333 zero_fill_size = bytes_to_read - bytes_left; 334 bytes_to_read = bytes_left; 335 } 336 337 // If there is data available on the core file read it 338 if (bytes_to_read) 339 bytes_copied = core_objfile->CopyData(offset + file_start, bytes_to_read, buf); 340 341 assert(zero_fill_size <= size); 342 // Pad remaining bytes 343 if (zero_fill_size) 344 memset(((char *)buf) + bytes_copied, 0, zero_fill_size); 345 346 return bytes_copied + zero_fill_size; 347 } 348 349 void 350 ProcessElfCore::Clear() 351 { 352 m_thread_list.Clear(); 353 m_os = llvm::Triple::UnknownOS; 354 355 SetUnixSignals(std::make_shared<UnixSignals>()); 356 } 357 358 void 359 ProcessElfCore::Initialize() 360 { 361 static std::once_flag g_once_flag; 362 363 std::call_once(g_once_flag, []() 364 { 365 PluginManager::RegisterPlugin (GetPluginNameStatic(), 366 GetPluginDescriptionStatic(), CreateInstance); 367 }); 368 } 369 370 lldb::addr_t 371 ProcessElfCore::GetImageInfoAddress() 372 { 373 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile(); 374 Address addr = obj_file->GetImageInfoAddress(&GetTarget()); 375 376 if (addr.IsValid()) 377 return addr.GetLoadAddress(&GetTarget()); 378 return LLDB_INVALID_ADDRESS; 379 } 380 381 /// Core files PT_NOTE segment descriptor types 382 enum { 383 NT_PRSTATUS = 1, 384 NT_FPREGSET, 385 NT_PRPSINFO, 386 NT_TASKSTRUCT, 387 NT_PLATFORM, 388 NT_AUXV 389 }; 390 391 namespace FREEBSD { 392 393 enum { 394 NT_PRSTATUS = 1, 395 NT_FPREGSET, 396 NT_PRPSINFO, 397 NT_THRMISC = 7, 398 NT_PROCSTAT_AUXV = 16, 399 NT_PPC_VMX = 0x100 400 }; 401 402 } 403 404 // Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details. 405 static void 406 ParseFreeBSDPrStatus(ThreadData &thread_data, DataExtractor &data, 407 ArchSpec &arch) 408 { 409 lldb::offset_t offset = 0; 410 bool lp64 = (arch.GetMachine() == llvm::Triple::aarch64 || 411 arch.GetMachine() == llvm::Triple::mips64 || 412 arch.GetMachine() == llvm::Triple::ppc64 || 413 arch.GetMachine() == llvm::Triple::x86_64); 414 int pr_version = data.GetU32(&offset); 415 416 Log *log (GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 417 if (log) 418 { 419 if (pr_version > 1) 420 log->Printf("FreeBSD PRSTATUS unexpected version %d", pr_version); 421 } 422 423 // Skip padding, pr_statussz, pr_gregsetsz, pr_fpregsetsz, pr_osreldate 424 if (lp64) 425 offset += 32; 426 else 427 offset += 16; 428 429 thread_data.signo = data.GetU32(&offset); // pr_cursig 430 thread_data.tid = data.GetU32(&offset); // pr_pid 431 if (lp64) 432 offset += 4; 433 434 size_t len = data.GetByteSize() - offset; 435 thread_data.gpregset = DataExtractor(data, offset, len); 436 } 437 438 static void 439 ParseFreeBSDThrMisc(ThreadData &thread_data, DataExtractor &data) 440 { 441 lldb::offset_t offset = 0; 442 thread_data.name = data.GetCStr(&offset, 20); 443 } 444 445 /// Parse Thread context from PT_NOTE segment and store it in the thread list 446 /// Notes: 447 /// 1) A PT_NOTE segment is composed of one or more NOTE entries. 448 /// 2) NOTE Entry contains a standard header followed by variable size data. 449 /// (see ELFNote structure) 450 /// 3) A Thread Context in a core file usually described by 3 NOTE entries. 451 /// a) NT_PRSTATUS - Register context 452 /// b) NT_PRPSINFO - Process info(pid..) 453 /// c) NT_FPREGSET - Floating point registers 454 /// 4) The NOTE entries can be in any order 455 /// 5) If a core file contains multiple thread contexts then there is two data forms 456 /// a) Each thread context(2 or more NOTE entries) contained in its own segment (PT_NOTE) 457 /// b) All thread context is stored in a single segment(PT_NOTE). 458 /// This case is little tricker since while parsing we have to find where the 459 /// new thread starts. The current implementation marks beginning of 460 /// new thread when it finds NT_PRSTATUS or NT_PRPSINFO NOTE entry. 461 /// For case (b) there may be either one NT_PRPSINFO per thread, or a single 462 /// one that applies to all threads (depending on the platform type). 463 void 464 ProcessElfCore::ParseThreadContextsFromNoteSegment(const elf::ELFProgramHeader *segment_header, 465 DataExtractor segment_data) 466 { 467 assert(segment_header && segment_header->p_type == llvm::ELF::PT_NOTE); 468 469 lldb::offset_t offset = 0; 470 std::unique_ptr<ThreadData> thread_data(new ThreadData); 471 bool have_prstatus = false; 472 bool have_prpsinfo = false; 473 474 ArchSpec arch = GetArchitecture(); 475 ELFLinuxPrPsInfo prpsinfo; 476 ELFLinuxPrStatus prstatus; 477 size_t header_size; 478 size_t len; 479 480 // Loop through the NOTE entires in the segment 481 while (offset < segment_header->p_filesz) 482 { 483 ELFNote note = ELFNote(); 484 note.Parse(segment_data, &offset); 485 486 // Beginning of new thread 487 if ((note.n_type == NT_PRSTATUS && have_prstatus) || 488 (note.n_type == NT_PRPSINFO && have_prpsinfo)) 489 { 490 assert(thread_data->gpregset.GetByteSize() > 0); 491 // Add the new thread to thread list 492 m_thread_data.push_back(*thread_data); 493 *thread_data = ThreadData(); 494 have_prstatus = false; 495 have_prpsinfo = false; 496 } 497 498 size_t note_start, note_size; 499 note_start = offset; 500 note_size = llvm::RoundUpToAlignment(note.n_descsz, 4); 501 502 // Store the NOTE information in the current thread 503 DataExtractor note_data (segment_data, note_start, note_size); 504 if (note.n_name == "FreeBSD") 505 { 506 m_os = llvm::Triple::FreeBSD; 507 switch (note.n_type) 508 { 509 case FREEBSD::NT_PRSTATUS: 510 have_prstatus = true; 511 ParseFreeBSDPrStatus(*thread_data, note_data, arch); 512 break; 513 case FREEBSD::NT_FPREGSET: 514 thread_data->fpregset = note_data; 515 break; 516 case FREEBSD::NT_PRPSINFO: 517 have_prpsinfo = true; 518 break; 519 case FREEBSD::NT_THRMISC: 520 ParseFreeBSDThrMisc(*thread_data, note_data); 521 break; 522 case FREEBSD::NT_PROCSTAT_AUXV: 523 // FIXME: FreeBSD sticks an int at the beginning of the note 524 m_auxv = DataExtractor(segment_data, note_start + 4, note_size - 4); 525 break; 526 case FREEBSD::NT_PPC_VMX: 527 thread_data->vregset = note_data; 528 break; 529 default: 530 break; 531 } 532 } 533 else 534 { 535 switch (note.n_type) 536 { 537 case NT_PRSTATUS: 538 have_prstatus = true; 539 prstatus.Parse(note_data, arch); 540 thread_data->signo = prstatus.pr_cursig; 541 header_size = ELFLinuxPrStatus::GetSize(arch); 542 len = note_data.GetByteSize() - header_size; 543 thread_data->gpregset = DataExtractor(note_data, header_size, len); 544 // FIXME: Obtain actual tid on Linux 545 thread_data->tid = m_thread_data.size(); 546 break; 547 case NT_FPREGSET: 548 thread_data->fpregset = note_data; 549 break; 550 case NT_PRPSINFO: 551 have_prpsinfo = true; 552 prpsinfo.Parse(note_data, arch); 553 thread_data->name = prpsinfo.pr_fname; 554 break; 555 case NT_AUXV: 556 m_auxv = DataExtractor(note_data); 557 break; 558 default: 559 break; 560 } 561 } 562 563 offset += note_size; 564 } 565 // Add last entry in the note section 566 if (thread_data && thread_data->gpregset.GetByteSize() > 0) 567 { 568 m_thread_data.push_back(*thread_data); 569 } 570 } 571 572 uint32_t 573 ProcessElfCore::GetNumThreadContexts () 574 { 575 if (!m_thread_data_valid) 576 DoLoadCore(); 577 return m_thread_data.size(); 578 } 579 580 ArchSpec 581 ProcessElfCore::GetArchitecture() 582 { 583 ObjectFileELF *core_file = (ObjectFileELF *)(m_core_module_sp->GetObjectFile()); 584 ArchSpec arch; 585 core_file->GetArchitecture(arch); 586 return arch; 587 } 588 589 const lldb::DataBufferSP 590 ProcessElfCore::GetAuxvData() 591 { 592 const uint8_t *start = m_auxv.GetDataStart(); 593 size_t len = m_auxv.GetByteSize(); 594 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(start, len)); 595 return buffer; 596 } 597