1 | //===-- MachTask.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 | // MachTask.h |
10 | // debugserver |
11 | // |
12 | // Created by Greg Clayton on 12/5/08. |
13 | // |
14 | //===----------------------------------------------------------------------===// |
15 | |
16 | #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H |
17 | #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H |
18 | |
19 | #include <mach/mach.h> |
20 | #include <sys/socket.h> |
21 | #include <map> |
22 | #include <string> |
23 | #include "DNBDefs.h" |
24 | #include "RNBContext.h" |
25 | #include "MachException.h" |
26 | #include "MachVMMemory.h" |
27 | #include "PThreadMutex.h" |
28 | |
29 | class MachProcess; |
30 | |
31 | typedef uint64_t MachMallocEventId; |
32 | |
33 | enum MachMallocEventType { |
34 | eMachMallocEventTypeAlloc = 2, |
35 | eMachMallocEventTypeDealloc = 4, |
36 | eMachMallocEventTypeOther = 1 |
37 | }; |
38 | |
39 | struct MachMallocEvent { |
40 | mach_vm_address_t m_base_address; |
41 | uint64_t m_size; |
42 | MachMallocEventType m_event_type; |
43 | MachMallocEventId m_event_id; |
44 | }; |
45 | |
46 | class MachTask { |
47 | public: |
48 | // Constructors and Destructors |
49 | MachTask(MachProcess *process); |
50 | virtual ~MachTask(); |
51 | |
52 | void Clear(); |
53 | |
54 | kern_return_t Suspend(); |
55 | kern_return_t Resume(); |
56 | |
57 | nub_size_t ReadMemory(nub_addr_t addr, nub_size_t size, void *buf); |
58 | nub_size_t WriteMemory(nub_addr_t addr, nub_size_t size, const void *buf); |
59 | int GetMemoryRegionInfo(nub_addr_t addr, DNBRegionInfo *region_info); |
60 | std::string GetProfileData(DNBProfileDataScanType scanType); |
61 | |
62 | nub_addr_t AllocateMemory(nub_size_t size, uint32_t permissions); |
63 | nub_bool_t DeallocateMemory(nub_addr_t addr); |
64 | void ClearAllocations(); |
65 | |
66 | mach_port_t ExceptionPort() const; |
67 | bool ExceptionPortIsValid() const; |
68 | kern_return_t SaveExceptionPortInfo(); |
69 | kern_return_t RestoreExceptionPortInfo(); |
70 | kern_return_t ShutDownExcecptionThread(); |
71 | |
72 | bool StartExceptionThread( |
73 | const RNBContext::IgnoredExceptions &ignored_exceptions, DNBError &err); |
74 | nub_addr_t GetDYLDAllImageInfosAddress(DNBError &err); |
75 | kern_return_t BasicInfo(struct task_basic_info *info); |
76 | static kern_return_t BasicInfo(task_t task, struct task_basic_info *info); |
77 | bool IsValid() const; |
78 | static bool IsValid(task_t task); |
79 | static void *ExceptionThread(void *arg); |
80 | void TaskPortChanged(task_t task); |
81 | task_t TaskPort() const { return m_task; } |
82 | task_t TaskPortForProcessID(DNBError &err, bool force = false); |
83 | static task_t TaskPortForProcessID(pid_t pid, DNBError &err, |
84 | uint32_t num_retries = 10, |
85 | uint32_t usec_interval = 10000); |
86 | |
87 | MachProcess *Process() { return m_process; } |
88 | const MachProcess *Process() const { return m_process; } |
89 | |
90 | nub_size_t PageSize(); |
91 | void TaskWillExecProcessesSuspended() { m_exec_will_be_suspended = true; } |
92 | |
93 | protected: |
94 | MachProcess *m_process; // The mach process that owns this MachTask |
95 | task_t m_task; |
96 | MachVMMemory m_vm_memory; // Special mach memory reading class that will take |
97 | // care of watching for page and region boundaries |
98 | MachException::PortInfo |
99 | m_exc_port_info; // Saved settings for all exception ports |
100 | pthread_t m_exception_thread; // Thread ID for the exception thread in case we |
101 | // need it |
102 | mach_port_t m_exception_port; // Exception port on which we will receive child |
103 | // exceptions |
104 | bool m_exec_will_be_suspended; // If this task exec's another process, that |
105 | // process will be launched suspended and we will |
106 | // need to execute one extra Resume to get it |
107 | // to progress from dyld_start. |
108 | bool m_do_double_resume; // next time we task_resume(), do it twice to |
109 | // fix a too-high suspend count. |
110 | |
111 | typedef std::map<mach_vm_address_t, size_t> allocation_collection; |
112 | allocation_collection m_allocations; |
113 | |
114 | private: |
115 | MachTask(const MachTask &) = delete; |
116 | MachTask &operator=(const MachTask &rhs) = delete; |
117 | }; |
118 | |
119 | #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H |
120 | |