1 //===-- MachTask.h ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 //----------------------------------------------------------------------
14 // Created by Greg Clayton on 12/5/08.
16 //===----------------------------------------------------------------------===//
18 #ifndef __MachTask_h__
19 #define __MachTask_h__
22 #include <mach/mach.h>
23 #include <sys/socket.h>
27 // Other libraries and framework includes
30 #include "MachException.h"
31 #include "MachVMMemory.h"
32 #include "PThreadMutex.h"
36 typedef uint64_t MachMallocEventId;
38 enum MachMallocEventType {
39 eMachMallocEventTypeAlloc = 2,
40 eMachMallocEventTypeDealloc = 4,
41 eMachMallocEventTypeOther = 1
44 struct MachMallocEvent {
45 mach_vm_address_t m_base_address;
47 MachMallocEventType m_event_type;
48 MachMallocEventId m_event_id;
53 //------------------------------------------------------------------
54 // Constructors and Destructors
55 //------------------------------------------------------------------
56 MachTask(MachProcess *process);
61 kern_return_t Suspend();
62 kern_return_t Resume();
64 nub_size_t ReadMemory(nub_addr_t addr, nub_size_t size, void *buf);
65 nub_size_t WriteMemory(nub_addr_t addr, nub_size_t size, const void *buf);
66 int GetMemoryRegionInfo(nub_addr_t addr, DNBRegionInfo *region_info);
67 std::string GetProfileData(DNBProfileDataScanType scanType);
69 nub_addr_t AllocateMemory(nub_size_t size, uint32_t permissions);
70 nub_bool_t DeallocateMemory(nub_addr_t addr);
72 mach_port_t ExceptionPort() const;
73 bool ExceptionPortIsValid() const;
74 kern_return_t SaveExceptionPortInfo();
75 kern_return_t RestoreExceptionPortInfo();
76 kern_return_t ShutDownExcecptionThread();
78 bool StartExceptionThread(DNBError &err);
79 nub_addr_t GetDYLDAllImageInfosAddress(DNBError &err);
80 kern_return_t BasicInfo(struct task_basic_info *info);
81 static kern_return_t BasicInfo(task_t task, struct task_basic_info *info);
83 static bool IsValid(task_t task);
84 static void *ExceptionThread(void *arg);
85 void TaskPortChanged(task_t task);
86 task_t TaskPort() const { return m_task; }
87 task_t TaskPortForProcessID(DNBError &err, bool force = false);
88 static task_t TaskPortForProcessID(pid_t pid, DNBError &err,
89 uint32_t num_retries = 10,
90 uint32_t usec_interval = 10000);
92 MachProcess *Process() { return m_process; }
93 const MachProcess *Process() const { return m_process; }
95 nub_size_t PageSize();
98 MachProcess *m_process; // The mach process that owns this MachTask
100 MachVMMemory m_vm_memory; // Special mach memory reading class that will take
101 // care of watching for page and region boundaries
102 MachException::PortInfo
103 m_exc_port_info; // Saved settings for all exception ports
104 pthread_t m_exception_thread; // Thread ID for the exception thread in case we
106 mach_port_t m_exception_port; // Exception port on which we will receive child
109 typedef std::map<mach_vm_address_t, size_t> allocation_collection;
110 allocation_collection m_allocations;
113 MachTask(const MachTask &); // Outlaw
114 MachTask &operator=(const MachTask &rhs); // Outlaw
117 #endif // __MachTask_h__