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