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