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