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