aboutsummaryrefslogtreecommitdiff
path: root/tools/debugserver/source/MacOSX/MachTask.h
blob: 96b991478c7892624eec0c02555d126266071aa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//===-- MachTask.h ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//----------------------------------------------------------------------
//
//  MachTask.h
//  debugserver
//
//  Created by Greg Clayton on 12/5/08.
//
//===----------------------------------------------------------------------===//

#ifndef __MachTask_h__
#define __MachTask_h__

// C Includes
#include <mach/mach.h>
#include <sys/socket.h>
// C++ Includes
#include <map>
#include <string>
// Other libraries and framework includes
// Project includes
#include "DNBDefs.h"
#include "MachException.h"
#include "MachVMMemory.h"
#include "PThreadMutex.h"

class MachProcess;

typedef uint64_t MachMallocEventId;

enum MachMallocEventType
{
    eMachMallocEventTypeAlloc = 2,
    eMachMallocEventTypeDealloc = 4,
    eMachMallocEventTypeOther = 1
};

struct MachMallocEvent
{
    mach_vm_address_t m_base_address;
    uint64_t m_size;
    MachMallocEventType m_event_type;
    MachMallocEventId m_event_id;
};

class MachTask
{
public:
    //------------------------------------------------------------------
    // Constructors and Destructors
    //------------------------------------------------------------------
                            MachTask (MachProcess *process);
    virtual                 ~MachTask ();

            void            Clear ();

            kern_return_t   Suspend ();
            kern_return_t   Resume ();

            nub_size_t      ReadMemory (nub_addr_t addr, nub_size_t size, void *buf);
            nub_size_t      WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf);
            int             GetMemoryRegionInfo (nub_addr_t addr, DNBRegionInfo *region_info);
            std::string     GetProfileData (DNBProfileDataScanType scanType);

            nub_addr_t      AllocateMemory (nub_size_t size, uint32_t permissions);
            nub_bool_t      DeallocateMemory (nub_addr_t addr);

            mach_port_t     ExceptionPort () const;
            bool            ExceptionPortIsValid () const;
            kern_return_t   SaveExceptionPortInfo ();
            kern_return_t   RestoreExceptionPortInfo ();
            kern_return_t   ShutDownExcecptionThread ();

            bool            StartExceptionThread (DNBError &err);
            nub_addr_t      GetDYLDAllImageInfosAddress (DNBError& err);
            kern_return_t   BasicInfo (struct task_basic_info *info);
    static  kern_return_t   BasicInfo (task_t task, struct task_basic_info *info);
            bool            IsValid () const;
    static  bool            IsValid (task_t task);
    static  void *          ExceptionThread (void *arg);
            task_t          TaskPort () const { return m_task; }
            task_t          TaskPortForProcessID (DNBError &err, bool force = false);
    static  task_t          TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries = 10, uint32_t usec_interval = 10000);

            MachProcess *   Process () { return m_process; }
    const   MachProcess *   Process () const { return m_process; }
    
            nub_size_t      PageSize ();
    
            bool            HasMallocLoggingEnabled ();

            // enumerate the malloc records for a given address (starting with Mac OS X 10.6 Snow Leopard it should include
            // all allocations that *include* address, rather than just those *starting* at address)
            bool            EnumerateMallocRecords (mach_vm_address_t address,
                                                    MachMallocEvent *event_buffer,
                                                    uint32_t buffer_size,
                                                    uint32_t *count);
    
            // enumerate every malloc record generated by this task, no matter what the address
            bool            EnumerateMallocRecords (MachMallocEvent *event_buffer,
                                                    uint32_t buffer_size,
                                                    uint32_t *count);
        
            // given a malloc event, report every stack frame that led to this event
            bool            EnumerateMallocFrames (MachMallocEventId event_id,
                                                   mach_vm_address_t *function_addresses_buffer,
                                                   uint32_t buffer_size,
                                                   uint32_t *count);

protected:
            MachProcess *   m_process;                  // The mach process that owns this MachTask
            task_t          m_task;
            MachVMMemory    m_vm_memory;                // Special mach memory reading class that will take care of watching for page and region boundaries
            MachException::PortInfo
                            m_exc_port_info;            // Saved settings for all exception ports
            pthread_t       m_exception_thread;         // Thread ID for the exception thread in case we need it
            mach_port_t     m_exception_port;           // Exception port on which we will receive child exceptions

            typedef std::map <mach_vm_address_t, size_t> allocation_collection;
            allocation_collection m_allocations;

private:
    MachTask(const MachTask&); // Outlaw
    MachTask& operator=(const MachTask& rhs);// Outlaw
};

#endif  // __MachTask_h__