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