MachTask.h revision fb9cee64303d36d6fe5d87e63dd8701d1ddb70a9
10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//===-- MachTask.h ----------------------------------------------*- C++ -*-===//
20529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//
30529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//                     The LLVM Compiler Infrastructure
40529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//
50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// This file is distributed under the University of Illinois Open Source
60529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// License. See LICENSE.TXT for details.
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//===----------------------------------------------------------------------===//
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//----------------------------------------------------------------------
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//  MachTask.h
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//  debugserver
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//  Created by Greg Clayton on 12/5/08.
150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//
160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//===----------------------------------------------------------------------===//
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#ifndef __MachTask_h__
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#define __MachTask_h__
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// C Includes
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include <mach/mach.h>
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include <sys/socket.h>
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// C++ Includes
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include <map>
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include <string>
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Other libraries and framework includes
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Project includes
290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "MachException.h"
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "MachVMMemory.h"
310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "PThreadMutex.h"
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass MachProcess;
340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochtypedef uint64_t MachMallocEventId;
360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochenum MachMallocEventType
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch{
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    eMachMallocEventTypeAlloc = 2,
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    eMachMallocEventTypeDealloc = 4,
410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    eMachMallocEventTypeOther = 1
420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch};
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochstruct MachMallocEvent
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch{
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    mach_vm_address_t m_base_address;
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    uint64_t m_size;
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    MachMallocEventType m_event_type;
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    MachMallocEventId m_event_id;
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch};
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass MachTask
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch{
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochpublic:
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    //------------------------------------------------------------------
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // Constructors and Destructors
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    //------------------------------------------------------------------
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            MachTask (MachProcess *process);
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    virtual                 ~MachTask ();
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            void            Clear ();
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            kern_return_t   Suspend ();
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            kern_return_t   Resume ();
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            nub_size_t      ReadMemory (nub_addr_t addr, nub_size_t size, void *buf);
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            nub_size_t      WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf);
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            int             GetMemoryRegionInfo (nub_addr_t addr, DNBRegionInfo *region_info);
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            const char *    GetProfileDataAsCString ();
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            nub_addr_t      AllocateMemory (nub_size_t size, uint32_t permissions);
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            nub_bool_t      DeallocateMemory (nub_addr_t addr);
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mach_port_t     ExceptionPort () const;
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            bool            ExceptionPortIsValid () const;
760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            kern_return_t   SaveExceptionPortInfo ();
770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            kern_return_t   RestoreExceptionPortInfo ();
780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            kern_return_t   ShutDownExcecptionThread ();
790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            bool            StartExceptionThread (DNBError &err);
810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            nub_addr_t      GetDYLDAllImageInfosAddress (DNBError& err);
820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            kern_return_t   BasicInfo (struct task_basic_info *info);
830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    static  kern_return_t   BasicInfo (task_t task, struct task_basic_info *info);
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            bool            IsValid () const;
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    static  bool            IsValid (task_t task);
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    static  void *          ExceptionThread (void *arg);
870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            task_t          TaskPort () const { return m_task; }
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            task_t          TaskPortForProcessID (DNBError &err);
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    static  task_t          TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries = 10, uint32_t usec_interval = 10000);
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            MachProcess *   Process () { return m_process; }
920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const   MachProcess *   Process () const { return m_process; }
930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            bool            HasMallocLoggingEnabled ();
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            // enumerate the malloc records for a given address (starting with Mac OS X 10.6 Snow Leopard it should include
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            // all allocations that *include* address, rather than just those *starting* at address)
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            bool            EnumerateMallocRecords (mach_vm_address_t address,
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                    MachMallocEvent *event_buffer,
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                    uint32_t buffer_size,
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                    uint32_t *count);
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            // enumerate every malloc record generated by this task, no matter what the address
1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            bool            EnumerateMallocRecords (MachMallocEvent *event_buffer,
1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                    uint32_t buffer_size,
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                    uint32_t *count);
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            // given a malloc event, report every stack frame that led to this event
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            bool            EnumerateMallocFrames (MachMallocEventId event_id,
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                   mach_vm_address_t *function_addresses_buffer,
1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                   uint32_t buffer_size,
1130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                   uint32_t *count);
1140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochprotected:
1160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            MachProcess *   m_process;                  // The mach process that owns this MachTask
1170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            task_t          m_task;
1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            MachVMMemory    m_vm_memory;                // Special mach memory reading class that will take care of watching for page and region boundaries
1190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            MachException::PortInfo
1200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                            m_exc_port_info;            // Saved settings for all exception ports
1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            pthread_t       m_exception_thread;         // Thread ID for the exception thread in case we need it
1220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            mach_port_t     m_exception_port;           // Exception port on which we will receive child exceptions
1230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            typedef std::map <mach_vm_address_t, size_t> allocation_collection;
1250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch            allocation_collection m_allocations;
126            std::string m_profile_data;
127
128private:
129    MachTask(const MachTask&); // Outlaw
130    MachTask& operator=(const MachTask& rhs);// Outlaw
131};
132
133#endif  // __MachTask_h__
134