1 //===-- MachTask.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
12 // Created by Greg Clayton on 12/5/08.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H
17 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H
19 #include <mach/mach.h>
20 #include <sys/socket.h>
24 #include "MachException.h"
25 #include "MachVMMemory.h"
26 #include "PThreadMutex.h"
30 typedef uint64_t MachMallocEventId
;
32 enum MachMallocEventType
{
33 eMachMallocEventTypeAlloc
= 2,
34 eMachMallocEventTypeDealloc
= 4,
35 eMachMallocEventTypeOther
= 1
38 struct MachMallocEvent
{
39 mach_vm_address_t m_base_address
;
41 MachMallocEventType m_event_type
;
42 MachMallocEventId m_event_id
;
47 // Constructors and Destructors
48 MachTask(MachProcess
*process
);
53 kern_return_t
Suspend();
54 kern_return_t
Resume();
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
);
61 nub_addr_t
AllocateMemory(nub_size_t size
, uint32_t permissions
);
62 nub_bool_t
DeallocateMemory(nub_addr_t addr
);
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();
70 bool StartExceptionThread(bool unmask_signals
, 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
);
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);
84 MachProcess
*Process() { return m_process
; }
85 const MachProcess
*Process() const { return m_process
; }
87 nub_size_t
PageSize();
88 void TaskWillExecProcessesSuspended() { m_exec_will_be_suspended
= true; }
91 MachProcess
*m_process
; // The mach process that owns this MachTask
93 MachVMMemory m_vm_memory
; // Special mach memory reading class that will take
94 // care of watching for page and region boundaries
95 MachException::PortInfo
96 m_exc_port_info
; // Saved settings for all exception ports
97 pthread_t m_exception_thread
; // Thread ID for the exception thread in case we
99 mach_port_t m_exception_port
; // Exception port on which we will receive child
101 bool m_exec_will_be_suspended
; // If this task exec's another process, that
102 // process will be launched suspended and we will
103 // need to execute one extra Resume to get it
104 // to progress from dyld_start.
105 bool m_do_double_resume
; // next time we task_resume(), do it twice to
106 // fix a too-high suspend count.
108 typedef std::map
<mach_vm_address_t
, size_t> allocation_collection
;
109 allocation_collection m_allocations
;
112 MachTask(const MachTask
&) = delete;
113 MachTask
&operator=(const MachTask
&rhs
) = delete;
116 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H