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   typedef enum CorefilePreference {
92     eUserProcessCorefile,
93     eKernelCorefile
94   } CorefilePreferences;
95 
96   /// If a core file can be interpreted multiple ways, this establishes
97   /// which style wins.
98   ///
99   /// If a core file contains both a kernel binary and a user-process
100   /// dynamic loader, lldb needs to pick one over the other.  This could
101   /// be a kernel corefile that happens to have a copy of dyld in its
102   /// memory.  Or it could be a user process coredump of lldb while doing
103   /// kernel debugging - so a copy of the kernel is in its heap.  This
104   /// should become a setting so it can be over-ridden when necessary.
105   CorefilePreference GetCorefilePreference() {
106     // For now, if both user process and kernel binaries a present,
107     // assume this is a kernel coredump which has a copy of a user
108     // process dyld in one of its pages.
109     return eKernelCorefile;
110   }
111 
112   // For ProcessMachCore only
113   typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;
114   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange>
115       VMRangeToFileOffset;
116   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
117       VMRangeToPermissions;
118 
119   VMRangeToFileOffset m_core_aranges;
120   VMRangeToPermissions m_core_range_infos;
121   lldb::ModuleSP m_core_module_sp;
122   lldb_private::FileSpec m_core_file;
123   lldb::addr_t m_dyld_addr;
124   lldb::addr_t m_mach_kernel_addr;
125   lldb_private::ConstString m_dyld_plugin_name;
126 
127   DISALLOW_COPY_AND_ASSIGN(ProcessMachCore);
128 };
129 
130 #endif // liblldb_ProcessMachCore_h_
131