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