1 //===-- MachTask.h ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //----------------------------------------------------------------------
10 //
11 //  MachTask.h
12 //  debugserver
13 //
14 //  Created by Greg Clayton on 12/5/08.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef __MachTask_h__
19 #define __MachTask_h__
20 
21 // C Includes
22 #include <mach/mach.h>
23 #include <sys/socket.h>
24 // C++ Includes
25 #include <map>
26 // Other libraries and framework includes
27 // Project includes
28 #include "MachException.h"
29 #include "MachVMMemory.h"
30 #include "PThreadMutex.h"
31 
32 class MachProcess;
33 
34 class MachTask
35 {
36 public:
37     //------------------------------------------------------------------
38     // Constructors and Destructors
39     //------------------------------------------------------------------
40                             MachTask (MachProcess *process);
41     virtual                 ~MachTask ();
42 
43             void            Clear ();
44 
45             kern_return_t   Suspend ();
46             kern_return_t   Resume ();
47 
48             nub_size_t      ReadMemory (nub_addr_t addr, nub_size_t size, void *buf);
49             nub_size_t      WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf);
50 
51             nub_addr_t      AllocateMemory (nub_size_t size, uint32_t permissions);
52             nub_bool_t      DeallocateMemory (nub_addr_t addr);
53 
54             mach_port_t     ExceptionPort () const;
55             bool            ExceptionPortIsValid () const;
56             kern_return_t   SaveExceptionPortInfo ();
57             kern_return_t   RestoreExceptionPortInfo ();
58             kern_return_t   ShutDownExcecptionThread ();
59 
60             bool            StartExceptionThread (DNBError &err);
61             nub_addr_t      GetDYLDAllImageInfosAddress (DNBError& err);
62             kern_return_t   BasicInfo (struct task_basic_info *info);
63     static  kern_return_t   BasicInfo (task_t task, struct task_basic_info *info);
64             bool            IsValid () const;
65     static  bool            IsValid (task_t task);
66     static  void *          ExceptionThread (void *arg);
67             task_t          TaskPort () const { return m_task; }
68             task_t          TaskPortForProcessID (DNBError &err);
69     static  task_t          TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries = 10, uint32_t usec_interval = 10000);
70 
71             MachProcess *   Process () { return m_process; }
72     const   MachProcess *   Process () const { return m_process; }
73 
74 protected:
75             MachProcess *   m_process;                  // The mach process that owns this MachTask
76             task_t          m_task;
77             MachVMMemory    m_vm_memory;                // Special mach memory reading class that will take care of watching for page and region boundaries
78             MachException::PortInfo
79                             m_exc_port_info;            // Saved settings for all exception ports
80             pthread_t       m_exception_thread;         // Thread ID for the exception thread in case we need it
81             mach_port_t     m_exception_port;           // Exception port on which we will receive child exceptions
82 
83             typedef std::map <mach_vm_address_t, size_t> allocation_collection;
84             allocation_collection m_allocations;
85 
86 private:
87     MachTask(const MachTask&); // Outlaw
88     MachTask& operator=(const MachTask& rhs);// Outlaw
89 };
90 
91 #endif  // __MachTask_h__
92