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