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, ListenerSP listener_sp, 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_sp, *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, ListenerSP listener_sp, const FileSpec &core_file) :
126     Process (target_sp, listener_sp),
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         if (GetTarget().GetArchitecture().IsValid() == false && m_core_module_sp.get())
348         {
349             GetTarget().SetArchitecture (m_core_module_sp->GetArchitecture());
350         }
351 
352         // SearchForDarwinKernel will end up calling back into this this class in the GetImageInfoAddress
353         // method which will give it the m_mach_kernel_addr/m_dyld_addr it already has.  Save that aside
354         // and set m_mach_kernel_addr/m_dyld_addr to an invalid address temporarily so
355         // DynamicLoaderDarwinKernel does a real search for the kernel using its own heuristics.
356 
357         addr_t saved_mach_kernel_addr = m_mach_kernel_addr;
358         addr_t saved_user_dyld_addr = m_dyld_addr;
359         m_mach_kernel_addr = LLDB_INVALID_ADDRESS;
360         m_dyld_addr = LLDB_INVALID_ADDRESS;
361 
362         addr_t better_kernel_address = DynamicLoaderDarwinKernel::SearchForDarwinKernel (this);
363 
364         m_mach_kernel_addr = saved_mach_kernel_addr;
365         m_dyld_addr = saved_user_dyld_addr;
366 
367         if (better_kernel_address != LLDB_INVALID_ADDRESS)
368         {
369             if (log)
370                 log->Printf ("ProcessMachCore::DoLoadCore: Using the kernel address from DynamicLoaderDarwinKernel");
371             m_mach_kernel_addr = better_kernel_address;
372         }
373     }
374 
375 
376     // If we found both a user-process dyld and a kernel binary, we need to decide
377     // which to prefer.
378     if (GetCorefilePreference() == eKernelCorefile)
379     {
380         if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS)
381         {
382             if (log)
383                 log->Printf ("ProcessMachCore::DoLoadCore: Using kernel corefile image at 0x%" PRIx64, m_mach_kernel_addr);
384             m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
385         }
386         else if (m_dyld_addr != LLDB_INVALID_ADDRESS)
387         {
388             if (log)
389                 log->Printf ("ProcessMachCore::DoLoadCore: Using user process dyld image at 0x%" PRIx64, m_dyld_addr);
390             m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic();
391         }
392     }
393     else
394     {
395         if (m_dyld_addr != LLDB_INVALID_ADDRESS)
396         {
397             if (log)
398                 log->Printf ("ProcessMachCore::DoLoadCore: Using user process dyld image at 0x%" PRIx64, m_dyld_addr);
399             m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic();
400         }
401         else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS)
402         {
403             if (log)
404                 log->Printf ("ProcessMachCore::DoLoadCore: Using kernel corefile image at 0x%" PRIx64, m_mach_kernel_addr);
405             m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
406         }
407     }
408 
409     // Even if the architecture is set in the target, we need to override
410     // it to match the core file which is always single arch.
411     ArchSpec arch (m_core_module_sp->GetArchitecture());
412     if (arch.GetCore() == ArchSpec::eCore_x86_32_i486)
413     {
414         arch.SetTriple ("i386", GetTarget().GetPlatform().get());
415     }
416     if (arch.IsValid())
417         GetTarget().SetArchitecture(arch);
418 
419     return error;
420 }
421 
422 lldb_private::DynamicLoader *
423 ProcessMachCore::GetDynamicLoader ()
424 {
425     if (m_dyld_ap.get() == NULL)
426         m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
427     return m_dyld_ap.get();
428 }
429 
430 bool
431 ProcessMachCore::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
432 {
433     if (old_thread_list.GetSize(false) == 0)
434     {
435         // Make up the thread the first time this is called so we can setup our one and only
436         // core thread state.
437         ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
438 
439         if (core_objfile)
440         {
441             const uint32_t num_threads = core_objfile->GetNumThreadContexts ();
442             for (lldb::tid_t tid = 0; tid < num_threads; ++tid)
443             {
444                 ThreadSP thread_sp(new ThreadMachCore (*this, tid));
445                 new_thread_list.AddThread (thread_sp);
446             }
447         }
448     }
449     else
450     {
451         const uint32_t num_threads = old_thread_list.GetSize(false);
452         for (uint32_t i=0; i<num_threads; ++i)
453             new_thread_list.AddThread (old_thread_list.GetThreadAtIndex (i, false));
454     }
455     return new_thread_list.GetSize(false) > 0;
456 }
457 
458 void
459 ProcessMachCore::RefreshStateAfterStop ()
460 {
461     // Let all threads recover from stopping and do any clean up based
462     // on the previous thread state (if any).
463     m_thread_list.RefreshStateAfterStop();
464     //SetThreadStopInfo (m_last_stop_packet);
465 }
466 
467 Error
468 ProcessMachCore::DoDestroy ()
469 {
470     return Error();
471 }
472 
473 //------------------------------------------------------------------
474 // Process Queries
475 //------------------------------------------------------------------
476 
477 bool
478 ProcessMachCore::IsAlive ()
479 {
480     return true;
481 }
482 
483 bool
484 ProcessMachCore::WarnBeforeDetach () const
485 {
486     return false;
487 }
488 
489 //------------------------------------------------------------------
490 // Process Memory
491 //------------------------------------------------------------------
492 size_t
493 ProcessMachCore::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
494 {
495     // Don't allow the caching that lldb_private::Process::ReadMemory does
496     // since in core files we have it all cached our our core file anyway.
497     return DoReadMemory (addr, buf, size, error);
498 }
499 
500 size_t
501 ProcessMachCore::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error)
502 {
503     ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
504 
505     if (core_objfile)
506     {
507         const VMRangeToFileOffset::Entry *core_memory_entry = m_core_aranges.FindEntryThatContains (addr);
508         if (core_memory_entry)
509         {
510             const addr_t offset = addr - core_memory_entry->GetRangeBase();
511             const addr_t bytes_left = core_memory_entry->GetRangeEnd() - addr;
512             size_t bytes_to_read = size;
513             if (bytes_to_read > bytes_left)
514                 bytes_to_read = bytes_left;
515             return core_objfile->CopyData (core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, buf);
516         }
517         else
518         {
519             error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr);
520         }
521     }
522     return 0;
523 }
524 
525 void
526 ProcessMachCore::Clear()
527 {
528     m_thread_list.Clear();
529 }
530 
531 void
532 ProcessMachCore::Initialize()
533 {
534     static std::once_flag g_once_flag;
535 
536     std::call_once(g_once_flag, []() {
537         PluginManager::RegisterPlugin (GetPluginNameStatic(),
538                                        GetPluginDescriptionStatic(),
539                                        CreateInstance);
540     });
541 }
542 
543 addr_t
544 ProcessMachCore::GetImageInfoAddress()
545 {
546     // If we found both a user-process dyld and a kernel binary, we need to decide
547     // which to prefer.
548     if (GetCorefilePreference() == eKernelCorefile)
549     {
550         if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS)
551         {
552             return m_mach_kernel_addr;
553         }
554         return m_dyld_addr;
555     }
556     else
557     {
558         if (m_dyld_addr != LLDB_INVALID_ADDRESS)
559         {
560             return m_dyld_addr;
561         }
562         return m_mach_kernel_addr;
563     }
564 }
565 
566 
567 lldb_private::ObjectFile *
568 ProcessMachCore::GetCoreObjectFile ()
569 {
570     return m_core_module_sp->GetObjectFile();
571 }
572