1 //===-- ProcessMachCore.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_ProcessMachCore_h_
10 #define liblldb_ProcessMachCore_h_
11 
12 #include <list>
13 #include <vector>
14 
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/ConstString.h"
17 #include "lldb/Utility/Status.h"
18 
19 class ThreadKDP;
20 
21 class ProcessMachCore : public lldb_private::Process {
22 public:
23   // Constructors and Destructors
24   ProcessMachCore(lldb::TargetSP target_sp, lldb::ListenerSP listener,
25                   const lldb_private::FileSpec &core_file);
26 
27   ~ProcessMachCore() override;
28 
29   static lldb::ProcessSP
30   CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener,
31                  const lldb_private::FileSpec *crash_file_path);
32 
33   static void Initialize();
34 
35   static void Terminate();
36 
37   static lldb_private::ConstString GetPluginNameStatic();
38 
39   static const char *GetPluginDescriptionStatic();
40 
41   // Check if a given Process
42   bool CanDebug(lldb::TargetSP target_sp,
43                 bool plugin_specified_by_name) override;
44 
45   // Creating a new process, or attaching to an existing one
46   lldb_private::Status DoLoadCore() override;
47 
48   lldb_private::DynamicLoader *GetDynamicLoader() override;
49 
50   // PluginInterface protocol
51   lldb_private::ConstString GetPluginName() override;
52 
53   uint32_t GetPluginVersion() override;
54 
55   // Process Control
56   lldb_private::Status DoDestroy() override;
57 
58   void RefreshStateAfterStop() override;
59 
60   // Process Queries
61   bool IsAlive() override;
62 
63   bool WarnBeforeDetach() const override;
64 
65   // Process Memory
66   size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
67                     lldb_private::Status &error) override;
68 
69   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
70                       lldb_private::Status &error) override;
71 
72   lldb_private::Status
73   GetMemoryRegionInfo(lldb::addr_t load_addr,
74                       lldb_private::MemoryRegionInfo &region_info) override;
75 
76   lldb::addr_t GetImageInfoAddress() override;
77 
78 protected:
79   friend class ThreadMachCore;
80 
81   void Clear();
82 
83   bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
84                         lldb_private::ThreadList &new_thread_list) override;
85 
86   lldb_private::ObjectFile *GetCoreObjectFile();
87 
88 private:
89   bool GetDynamicLoaderAddress(lldb::addr_t addr);
90 
91   enum CorefilePreference { eUserProcessCorefile, eKernelCorefile };
92 
93   /// If a core file can be interpreted multiple ways, this establishes
94   /// which style wins.
95   ///
96   /// If a core file contains both a kernel binary and a user-process
97   /// dynamic loader, lldb needs to pick one over the other.  This could
98   /// be a kernel corefile that happens to have a copy of dyld in its
99   /// memory.  Or it could be a user process coredump of lldb while doing
100   /// kernel debugging - so a copy of the kernel is in its heap.  This
101   /// should become a setting so it can be over-ridden when necessary.
102   CorefilePreference GetCorefilePreference() {
103     // For now, if both user process and kernel binaries a present,
104     // assume this is a kernel coredump which has a copy of a user
105     // process dyld in one of its pages.
106     return eKernelCorefile;
107   }
108 
109   // For ProcessMachCore only
110   typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;
111   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange>
112       VMRangeToFileOffset;
113   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
114       VMRangeToPermissions;
115 
116   VMRangeToFileOffset m_core_aranges;
117   VMRangeToPermissions m_core_range_infos;
118   lldb::ModuleSP m_core_module_sp;
119   lldb_private::FileSpec m_core_file;
120   lldb::addr_t m_dyld_addr;
121   lldb::addr_t m_mach_kernel_addr;
122   lldb_private::ConstString m_dyld_plugin_name;
123 
124   DISALLOW_COPY_AND_ASSIGN(ProcessMachCore);
125 };
126 
127 #endif // liblldb_ProcessMachCore_h_
128