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 "MachException.h" 30 #include "MachVMMemory.h" 31 #include "PThreadMutex.h" 32 33 class MachProcess; 34 35 typedef uint64_t MachMallocEventId; 36 37 enum MachMallocEventType 38 { 39 eMachMallocEventTypeAlloc = 2, 40 eMachMallocEventTypeDealloc = 4, 41 eMachMallocEventTypeOther = 1 42 }; 43 44 struct MachMallocEvent 45 { 46 mach_vm_address_t m_base_address; 47 uint64_t m_size; 48 MachMallocEventType m_event_type; 49 MachMallocEventId m_event_id; 50 }; 51 52 class MachTask 53 { 54 public: 55 //------------------------------------------------------------------ 56 // Constructors and Destructors 57 //------------------------------------------------------------------ 58 MachTask (MachProcess *process); 59 virtual ~MachTask (); 60 61 void Clear (); 62 63 kern_return_t Suspend (); 64 kern_return_t Resume (); 65 66 nub_size_t ReadMemory (nub_addr_t addr, nub_size_t size, void *buf); 67 nub_size_t WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf); 68 int GetMemoryRegionInfo (nub_addr_t addr, DNBRegionInfo *region_info); 69 const char * GetProfileDataAsCString (); 70 71 nub_addr_t AllocateMemory (nub_size_t size, uint32_t permissions); 72 nub_bool_t DeallocateMemory (nub_addr_t addr); 73 74 mach_port_t ExceptionPort () const; 75 bool ExceptionPortIsValid () const; 76 kern_return_t SaveExceptionPortInfo (); 77 kern_return_t RestoreExceptionPortInfo (); 78 kern_return_t ShutDownExcecptionThread (); 79 80 bool StartExceptionThread (DNBError &err); 81 nub_addr_t GetDYLDAllImageInfosAddress (DNBError& err); 82 kern_return_t BasicInfo (struct task_basic_info *info); 83 static kern_return_t BasicInfo (task_t task, struct task_basic_info *info); 84 bool IsValid () const; 85 static bool IsValid (task_t task); 86 static void * ExceptionThread (void *arg); 87 task_t TaskPort () const { return m_task; } 88 task_t TaskPortForProcessID (DNBError &err); 89 static task_t TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries = 10, uint32_t usec_interval = 10000); 90 91 MachProcess * Process () { return m_process; } 92 const MachProcess * Process () const { return m_process; } 93 94 95 bool HasMallocLoggingEnabled (); 96 97 // enumerate the malloc records for a given address (starting with Mac OS X 10.6 Snow Leopard it should include 98 // all allocations that *include* address, rather than just those *starting* at address) 99 bool EnumerateMallocRecords (mach_vm_address_t address, 100 MachMallocEvent *event_buffer, 101 uint32_t buffer_size, 102 uint32_t *count); 103 104 // enumerate every malloc record generated by this task, no matter what the address 105 bool EnumerateMallocRecords (MachMallocEvent *event_buffer, 106 uint32_t buffer_size, 107 uint32_t *count); 108 109 // given a malloc event, report every stack frame that led to this event 110 bool EnumerateMallocFrames (MachMallocEventId event_id, 111 mach_vm_address_t *function_addresses_buffer, 112 uint32_t buffer_size, 113 uint32_t *count); 114 115 protected: 116 MachProcess * m_process; // The mach process that owns this MachTask 117 task_t m_task; 118 MachVMMemory m_vm_memory; // Special mach memory reading class that will take care of watching for page and region boundaries 119 MachException::PortInfo 120 m_exc_port_info; // Saved settings for all exception ports 121 pthread_t m_exception_thread; // Thread ID for the exception thread in case we need it 122 mach_port_t m_exception_port; // Exception port on which we will receive child exceptions 123 124 typedef std::map <mach_vm_address_t, size_t> allocation_collection; 125 allocation_collection m_allocations; 126 std::string m_profile_data; 127 128 private: 129 MachTask(const MachTask&); // Outlaw 130 MachTask& operator=(const MachTask& rhs);// Outlaw 131 }; 132 133 #endif // __MachTask_h__ 134