| 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 LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H |
| 17 | #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H |
| 18 | |
| 19 | #include "DNBDefs.h" |
| 20 | #include "MachException.h" |
| 21 | #include "MachVMMemory.h" |
| 22 | #include "RNBContext.h" |
| 23 | #include <mach/mach.h> |
| 24 | #include <map> |
| 25 | #include <string> |
| 26 | #include <sys/socket.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 | void ClearAllocations(); |
| 64 | |
| 65 | mach_port_t ExceptionPort() const; |
| 66 | bool ExceptionPortIsValid() const; |
| 67 | kern_return_t SaveExceptionPortInfo(); |
| 68 | kern_return_t RestoreExceptionPortInfo(); |
| 69 | kern_return_t ShutDownExcecptionThread(); |
| 70 | |
| 71 | bool StartExceptionThread( |
| 72 | const RNBContext::IgnoredExceptions &ignored_exceptions, DNBError &err); |
| 73 | nub_addr_t GetDYLDAllImageInfosAddress(DNBError &err); |
| 74 | kern_return_t BasicInfo(struct task_basic_info *info); |
| 75 | static kern_return_t BasicInfo(task_t task, struct task_basic_info *info); |
| 76 | bool IsValid() const; |
| 77 | static bool IsValid(task_t task); |
| 78 | static void *ExceptionThread(void *arg); |
| 79 | void TaskPortChanged(task_t task); |
| 80 | task_t TaskPort() const { return m_task; } |
| 81 | task_t TaskPortForProcessID(DNBError &err, bool force = false); |
| 82 | static task_t TaskPortForProcessID(pid_t pid, DNBError &err, |
| 83 | uint32_t num_retries = 10, |
| 84 | uint32_t usec_interval = 10000); |
| 85 | |
| 86 | MachProcess *Process() { return m_process; } |
| 87 | const MachProcess *Process() const { return m_process; } |
| 88 | |
| 89 | nub_size_t PageSize(); |
| 90 | void TaskWillExecProcessesSuspended() { m_exec_will_be_suspended = true; } |
| 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 | bool m_exec_will_be_suspended; // If this task exec's another process, that |
| 104 | // process will be launched suspended and we will |
| 105 | // need to execute one extra Resume to get it |
| 106 | // to progress from dyld_start. |
| 107 | bool m_do_double_resume; // next time we task_resume(), do it twice to |
| 108 | // fix a too-high suspend count. |
| 109 | |
| 110 | typedef std::map<mach_vm_address_t, size_t> allocation_collection; |
| 111 | allocation_collection m_allocations; |
| 112 | |
| 113 | private: |
| 114 | MachTask(const MachTask &) = delete; |
| 115 | MachTask &operator=(const MachTask &rhs) = delete; |
| 116 | }; |
| 117 | |
| 118 | #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H |
| 119 | |