119 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// MachTask.h10// debugserver11//12// Created by Greg Clayton on 12/5/08.13//14//===----------------------------------------------------------------------===//15 16#ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H17#define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H18 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 28class MachProcess;29 30typedef uint64_t MachMallocEventId;31 32enum MachMallocEventType {33 eMachMallocEventTypeAlloc = 2,34 eMachMallocEventTypeDealloc = 4,35 eMachMallocEventTypeOther = 136};37 38struct 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 45class MachTask {46public:47 // Constructors and Destructors48 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 nub_bool_t GetMemoryTags(nub_addr_t addr, nub_size_t size,60 std::vector<uint8_t> &tags);61 std::string GetProfileData(DNBProfileDataScanType scanType);62 63 nub_addr_t AllocateMemory(nub_size_t size, uint32_t permissions);64 nub_bool_t DeallocateMemory(nub_addr_t addr);65 void ClearAllocations();66 67 mach_port_t ExceptionPort() const;68 bool ExceptionPortIsValid() const;69 kern_return_t SaveExceptionPortInfo();70 kern_return_t RestoreExceptionPortInfo();71 void ShutDownExceptionThread();72 73 bool StartExceptionThread(74 const RNBContext::IgnoredExceptions &ignored_exceptions, DNBError &err);75 nub_addr_t GetDYLDAllImageInfosAddress(DNBError &err);76 kern_return_t BasicInfo(struct task_basic_info *info);77 static kern_return_t BasicInfo(task_t task, struct task_basic_info *info);78 bool IsValid() const;79 static bool IsValid(task_t task);80 static void *ExceptionThread(void *arg);81 void TaskPortChanged(task_t task);82 task_t TaskPort() const { return m_task; }83 task_t TaskPortForProcessID(DNBError &err, bool force = false);84 static task_t TaskPortForProcessID(pid_t pid, DNBError &err);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 92protected:93 MachProcess *m_process; // The mach process that owns this MachTask94 task_t m_task;95 MachVMMemory m_vm_memory; // Special mach memory reading class that will take96 // care of watching for page and region boundaries97 MachException::PortInfo98 m_exc_port_info; // Saved settings for all exception ports99 pthread_t m_exception_thread; // Thread ID for the exception thread in case we100 // need it101 mach_port_t m_exception_port; // Exception port on which we will receive child102 // exceptions103 bool m_exec_will_be_suspended; // If this task exec's another process, that104 // process will be launched suspended and we will105 // need to execute one extra Resume to get it106 // to progress from dyld_start.107 bool m_do_double_resume; // next time we task_resume(), do it twice to108 // 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 113private:114 MachTask(const MachTask &) = delete;115 MachTask &operator=(const MachTask &rhs) = delete;116};117 118#endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTASK_H119