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