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