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