1 //===-- ProcessMachCore.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 <errno.h> 12 #include <stdlib.h> 13 14 // C++ Includes 15 #include "llvm/Support/MathExtras.h" 16 17 // Other libraries and framework includes 18 #include "lldb/Core/Debugger.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Core/Module.h" 21 #include "lldb/Core/ModuleSpec.h" 22 #include "lldb/Core/Section.h" 23 #include "lldb/Core/State.h" 24 #include "lldb/Host/Host.h" 25 #include "lldb/Symbol/ObjectFile.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Target/Thread.h" 28 29 // Project includes 30 #include "ProcessMachCore.h" 31 #include "ThreadMachCore.h" 32 #include "StopInfoMachException.h" 33 34 // Needed for the plug-in names for the dynamic loaders. 35 #include "lldb/Utility/SafeMachO.h" 36 37 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h" 38 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" 39 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" 40 41 using namespace lldb; 42 using namespace lldb_private; 43 44 ConstString 45 ProcessMachCore::GetPluginNameStatic() 46 { 47 static ConstString g_name("mach-o-core"); 48 return g_name; 49 } 50 51 const char * 52 ProcessMachCore::GetPluginDescriptionStatic() 53 { 54 return "Mach-O core file debugging plug-in."; 55 } 56 57 void 58 ProcessMachCore::Terminate() 59 { 60 PluginManager::UnregisterPlugin (ProcessMachCore::CreateInstance); 61 } 62 63 64 lldb::ProcessSP 65 ProcessMachCore::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file) 66 { 67 lldb::ProcessSP process_sp; 68 if (crash_file) 69 { 70 const size_t header_size = sizeof(llvm::MachO::mach_header); 71 lldb::DataBufferSP data_sp (crash_file->ReadFileContents(0, header_size)); 72 if (data_sp->GetByteSize() == header_size) 73 { 74 DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); 75 76 lldb::offset_t data_offset = 0; 77 llvm::MachO::mach_header mach_header; 78 if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) 79 { 80 if (mach_header.filetype == llvm::MachO::MH_CORE) 81 process_sp.reset(new ProcessMachCore (target, listener, *crash_file)); 82 } 83 } 84 85 } 86 return process_sp; 87 } 88 89 bool 90 ProcessMachCore::CanDebug(Target &target, bool plugin_specified_by_name) 91 { 92 if (plugin_specified_by_name) 93 return true; 94 95 // For now we are just making sure the file exists for a given module 96 if (!m_core_module_sp && m_core_file.Exists()) 97 { 98 ModuleSpec core_module_spec(m_core_file, target.GetArchitecture()); 99 Error error (ModuleList::GetSharedModule (core_module_spec, 100 m_core_module_sp, 101 NULL, 102 NULL, 103 NULL)); 104 105 if (m_core_module_sp) 106 { 107 const llvm::Triple &triple_ref = m_core_module_sp->GetArchitecture().GetTriple(); 108 if (triple_ref.getVendor() == llvm::Triple::Apple) 109 { 110 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 111 if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile) 112 return true; 113 } 114 } 115 } 116 return false; 117 } 118 119 //---------------------------------------------------------------------- 120 // ProcessMachCore constructor 121 //---------------------------------------------------------------------- 122 ProcessMachCore::ProcessMachCore(Target& target, Listener &listener, const FileSpec &core_file) : 123 Process (target, listener), 124 m_core_aranges (), 125 m_core_module_sp (), 126 m_core_file (core_file), 127 m_dyld_addr (LLDB_INVALID_ADDRESS), 128 m_mach_kernel_addr (LLDB_INVALID_ADDRESS), 129 m_dyld_plugin_name () 130 { 131 } 132 133 //---------------------------------------------------------------------- 134 // Destructor 135 //---------------------------------------------------------------------- 136 ProcessMachCore::~ProcessMachCore() 137 { 138 Clear(); 139 // We need to call finalize on the process before destroying ourselves 140 // to make sure all of the broadcaster cleanup goes as planned. If we 141 // destruct this class, then Process::~Process() might have problems 142 // trying to fully destroy the broadcaster. 143 Finalize(); 144 } 145 146 //---------------------------------------------------------------------- 147 // PluginInterface 148 //---------------------------------------------------------------------- 149 ConstString 150 ProcessMachCore::GetPluginName() 151 { 152 return GetPluginNameStatic(); 153 } 154 155 uint32_t 156 ProcessMachCore::GetPluginVersion() 157 { 158 return 1; 159 } 160 161 bool 162 ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) 163 { 164 llvm::MachO::mach_header header; 165 Error error; 166 if (DoReadMemory (addr, &header, sizeof(header), error) != sizeof(header)) 167 return false; 168 if (header.magic == llvm::MachO::MH_CIGAM || 169 header.magic == llvm::MachO::MH_CIGAM_64) 170 { 171 header.magic = llvm::ByteSwap_32(header.magic); 172 header.cputype = llvm::ByteSwap_32(header.cputype); 173 header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype); 174 header.filetype = llvm::ByteSwap_32(header.filetype); 175 header.ncmds = llvm::ByteSwap_32(header.ncmds); 176 header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds); 177 header.flags = llvm::ByteSwap_32(header.flags); 178 } 179 180 // TODO: swap header if needed... 181 //printf("0x%16.16" PRIx64 ": magic = 0x%8.8x, file_type= %u\n", vaddr, header.magic, header.filetype); 182 if (header.magic == llvm::MachO::MH_MAGIC || 183 header.magic == llvm::MachO::MH_MAGIC_64) 184 { 185 // Check MH_EXECUTABLE to see if we can find the mach image 186 // that contains the shared library list. The dynamic loader 187 // (dyld) is what contains the list for user applications, 188 // and the mach kernel contains a global that has the list 189 // of kexts to load 190 switch (header.filetype) 191 { 192 case llvm::MachO::MH_DYLINKER: 193 //printf("0x%16.16" PRIx64 ": file_type = MH_DYLINKER\n", vaddr); 194 // Address of dyld "struct mach_header" in the core file 195 m_dyld_addr = addr; 196 return true; 197 198 case llvm::MachO::MH_EXECUTE: 199 //printf("0x%16.16" PRIx64 ": file_type = MH_EXECUTE\n", vaddr); 200 // Check MH_EXECUTABLE file types to see if the dynamic link object flag 201 // is NOT set. If it isn't, then we have a mach_kernel. 202 if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) 203 { 204 // Address of the mach kernel "struct mach_header" in the core file. 205 m_mach_kernel_addr = addr; 206 return true; 207 } 208 break; 209 } 210 } 211 return false; 212 } 213 214 //---------------------------------------------------------------------- 215 // Process Control 216 //---------------------------------------------------------------------- 217 Error 218 ProcessMachCore::DoLoadCore () 219 { 220 Error error; 221 if (!m_core_module_sp) 222 { 223 error.SetErrorString ("invalid core module"); 224 return error; 225 } 226 227 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 228 if (core_objfile == NULL) 229 { 230 error.SetErrorString ("invalid core object file"); 231 return error; 232 } 233 234 if (core_objfile->GetNumThreadContexts() == 0) 235 { 236 error.SetErrorString ("core file doesn't contain any LC_THREAD load commands, or the LC_THREAD architecture is not supported in this lldb"); 237 return error; 238 } 239 240 SectionList *section_list = core_objfile->GetSectionList(); 241 if (section_list == NULL) 242 { 243 error.SetErrorString ("core file has no sections"); 244 return error; 245 } 246 247 const uint32_t num_sections = section_list->GetNumSections(0); 248 if (num_sections == 0) 249 { 250 error.SetErrorString ("core file has no sections"); 251 return error; 252 } 253 254 SetCanJIT(false); 255 256 llvm::MachO::mach_header header; 257 DataExtractor data (&header, 258 sizeof(header), 259 m_core_module_sp->GetArchitecture().GetByteOrder(), 260 m_core_module_sp->GetArchitecture().GetAddressByteSize()); 261 262 bool ranges_are_sorted = true; 263 addr_t vm_addr = 0; 264 for (uint32_t i=0; i<num_sections; ++i) 265 { 266 Section *section = section_list->GetSectionAtIndex (i).get(); 267 if (section) 268 { 269 lldb::addr_t section_vm_addr = section->GetFileAddress(); 270 FileRange file_range (section->GetFileOffset(), section->GetFileSize()); 271 VMRangeToFileOffset::Entry range_entry (section_vm_addr, 272 section->GetByteSize(), 273 file_range); 274 275 if (vm_addr > section_vm_addr) 276 ranges_are_sorted = false; 277 vm_addr = section->GetFileAddress(); 278 VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back(); 279 // printf ("LC_SEGMENT[%u] arange=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), frange=[0x%8.8x - 0x%8.8x)\n", 280 // i, 281 // range_entry.GetRangeBase(), 282 // range_entry.GetRangeEnd(), 283 // range_entry.data.GetRangeBase(), 284 // range_entry.data.GetRangeEnd()); 285 286 if (last_entry && 287 last_entry->GetRangeEnd() == range_entry.GetRangeBase() && 288 last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase()) 289 { 290 last_entry->SetRangeEnd (range_entry.GetRangeEnd()); 291 last_entry->data.SetRangeEnd (range_entry.data.GetRangeEnd()); 292 //puts("combine"); 293 } 294 else 295 { 296 m_core_aranges.Append(range_entry); 297 } 298 } 299 } 300 if (!ranges_are_sorted) 301 { 302 m_core_aranges.Sort(); 303 } 304 305 if (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS) 306 { 307 // We need to locate the main executable in the memory ranges 308 // we have in the core file. We need to search for both a user-process dyld binary 309 // and a kernel binary in memory; we must look at all the pages in the binary so 310 // we don't miss one or the other. If we find a user-process dyld binary, stop 311 // searching -- that's the one we'll prefer over the mach kernel. 312 const size_t num_core_aranges = m_core_aranges.GetSize(); 313 for (size_t i=0; i<num_core_aranges && m_dyld_addr == LLDB_INVALID_ADDRESS; ++i) 314 { 315 const VMRangeToFileOffset::Entry *entry = m_core_aranges.GetEntryAtIndex(i); 316 lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); 317 lldb::addr_t section_vm_addr_end = entry->GetRangeEnd(); 318 for (lldb::addr_t section_vm_addr = section_vm_addr_start; 319 section_vm_addr < section_vm_addr_end; 320 section_vm_addr += 0x1000) 321 { 322 GetDynamicLoaderAddress (section_vm_addr); 323 } 324 } 325 } 326 327 // If we found both a user-process dyld and a kernel binary, we need to decide 328 // which to prefer. 329 if (GetCorefilePreference() == eKernelCorefile) 330 { 331 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) 332 { 333 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 334 } 335 else if (m_dyld_addr != LLDB_INVALID_ADDRESS) 336 { 337 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 338 } 339 } 340 else 341 { 342 if (m_dyld_addr != LLDB_INVALID_ADDRESS) 343 { 344 m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); 345 } 346 else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) 347 { 348 m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); 349 } 350 } 351 352 // Even if the architecture is set in the target, we need to override 353 // it to match the core file which is always single arch. 354 ArchSpec arch (m_core_module_sp->GetArchitecture()); 355 if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) 356 { 357 arch.SetTriple ("i386", m_target.GetPlatform().get()); 358 } 359 if (arch.IsValid()) 360 m_target.SetArchitecture(arch); 361 362 return error; 363 } 364 365 lldb_private::DynamicLoader * 366 ProcessMachCore::GetDynamicLoader () 367 { 368 if (m_dyld_ap.get() == NULL) 369 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); 370 return m_dyld_ap.get(); 371 } 372 373 bool 374 ProcessMachCore::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 375 { 376 if (old_thread_list.GetSize(false) == 0) 377 { 378 // Make up the thread the first time this is called so we can setup our one and only 379 // core thread state. 380 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 381 382 if (core_objfile) 383 { 384 const uint32_t num_threads = core_objfile->GetNumThreadContexts (); 385 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) 386 { 387 ThreadSP thread_sp(new ThreadMachCore (*this, tid)); 388 new_thread_list.AddThread (thread_sp); 389 } 390 } 391 } 392 else 393 { 394 const uint32_t num_threads = old_thread_list.GetSize(false); 395 for (uint32_t i=0; i<num_threads; ++i) 396 new_thread_list.AddThread (old_thread_list.GetThreadAtIndex (i)); 397 } 398 return new_thread_list.GetSize(false) > 0; 399 } 400 401 void 402 ProcessMachCore::RefreshStateAfterStop () 403 { 404 // Let all threads recover from stopping and do any clean up based 405 // on the previous thread state (if any). 406 m_thread_list.RefreshStateAfterStop(); 407 //SetThreadStopInfo (m_last_stop_packet); 408 } 409 410 Error 411 ProcessMachCore::DoDestroy () 412 { 413 return Error(); 414 } 415 416 //------------------------------------------------------------------ 417 // Process Queries 418 //------------------------------------------------------------------ 419 420 bool 421 ProcessMachCore::IsAlive () 422 { 423 return true; 424 } 425 426 bool 427 ProcessMachCore::WarnBeforeDetach () const 428 { 429 return false; 430 } 431 432 //------------------------------------------------------------------ 433 // Process Memory 434 //------------------------------------------------------------------ 435 size_t 436 ProcessMachCore::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) 437 { 438 // Don't allow the caching that lldb_private::Process::ReadMemory does 439 // since in core files we have it all cached our our core file anyway. 440 return DoReadMemory (addr, buf, size, error); 441 } 442 443 size_t 444 ProcessMachCore::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) 445 { 446 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); 447 448 if (core_objfile) 449 { 450 const VMRangeToFileOffset::Entry *core_memory_entry = m_core_aranges.FindEntryThatContains (addr); 451 if (core_memory_entry) 452 { 453 const addr_t offset = addr - core_memory_entry->GetRangeBase(); 454 const addr_t bytes_left = core_memory_entry->GetRangeEnd() - addr; 455 size_t bytes_to_read = size; 456 if (bytes_to_read > bytes_left) 457 bytes_to_read = bytes_left; 458 return core_objfile->CopyData (core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, buf); 459 } 460 else 461 { 462 error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr); 463 } 464 } 465 return 0; 466 } 467 468 void 469 ProcessMachCore::Clear() 470 { 471 m_thread_list.Clear(); 472 } 473 474 void 475 ProcessMachCore::Initialize() 476 { 477 static bool g_initialized = false; 478 479 if (g_initialized == false) 480 { 481 g_initialized = true; 482 PluginManager::RegisterPlugin (GetPluginNameStatic(), 483 GetPluginDescriptionStatic(), 484 CreateInstance); 485 } 486 } 487 488 addr_t 489 ProcessMachCore::GetImageInfoAddress() 490 { 491 // If we found both a user-process dyld and a kernel binary, we need to decide 492 // which to prefer. 493 if (GetCorefilePreference() == eKernelCorefile) 494 { 495 if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) 496 { 497 return m_mach_kernel_addr; 498 } 499 return m_dyld_addr; 500 } 501 else 502 { 503 if (m_dyld_addr != LLDB_INVALID_ADDRESS) 504 { 505 return m_dyld_addr; 506 } 507 return m_mach_kernel_addr; 508 } 509 } 510 511 512 lldb_private::ObjectFile * 513 ProcessMachCore::GetCoreObjectFile () 514 { 515 return m_core_module_sp->GetObjectFile(); 516 } 517