Revert "[GlobalMerge][NFC] Skip sorting by profitability when it is not needed" ...
[llvm-project.git] / lldb / tools / debugserver / source / MacOSX / MachTask.h
blob286a57b9e0dd1c58b4cee17cfd36bd720d94712b
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
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>
21 #include <map>
22 #include <string>
23 #include "DNBDefs.h"
24 #include "RNBContext.h"
25 #include "MachException.h"
26 #include "MachVMMemory.h"
27 #include "PThreadMutex.h"
29 class MachProcess;
31 typedef uint64_t MachMallocEventId;
33 enum MachMallocEventType {
34 eMachMallocEventTypeAlloc = 2,
35 eMachMallocEventTypeDealloc = 4,
36 eMachMallocEventTypeOther = 1
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;
46 class MachTask {
47 public:
48 // Constructors and Destructors
49 MachTask(MachProcess *process);
50 virtual ~MachTask();
52 void Clear();
54 kern_return_t Suspend();
55 kern_return_t Resume();
57 nub_size_t ReadMemory(nub_addr_t addr, nub_size_t size, void *buf);
58 nub_size_t WriteMemory(nub_addr_t addr, nub_size_t size, const void *buf);
59 int GetMemoryRegionInfo(nub_addr_t addr, DNBRegionInfo *region_info);
60 std::string GetProfileData(DNBProfileDataScanType scanType);
62 nub_addr_t AllocateMemory(nub_size_t size, uint32_t permissions);
63 nub_bool_t DeallocateMemory(nub_addr_t addr);
64 void ClearAllocations();
66 mach_port_t ExceptionPort() const;
67 bool ExceptionPortIsValid() const;
68 kern_return_t SaveExceptionPortInfo();
69 kern_return_t RestoreExceptionPortInfo();
70 kern_return_t ShutDownExcecptionThread();
72 bool StartExceptionThread(
73 const RNBContext::IgnoredExceptions &ignored_exceptions, 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);
87 MachProcess *Process() { return m_process; }
88 const MachProcess *Process() const { return m_process; }
90 nub_size_t PageSize();
91 void TaskWillExecProcessesSuspended() { m_exec_will_be_suspended = true; }
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 bool m_exec_will_be_suspended; // If this task exec's another process, that
105 // process will be launched suspended and we will
106 // need to execute one extra Resume to get it
107 // to progress from dyld_start.
108 bool m_do_double_resume; // next time we task_resume(), do it twice to
109 // fix a too-high suspend count.
111 typedef std::map<mach_vm_address_t, size_t> allocation_collection;
112 allocation_collection m_allocations;
114 private:
115 MachTask(const MachTask &) = delete;
116 MachTask &operator=(const MachTask &rhs) = delete;
119 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H