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