Esempio n. 1
0
bool
MachThread::RestoreSuspendCount()
{
    DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
    DNBError err;
    if (ThreadIDIsValid(m_tid) == false)
        return false;
    else if (m_suspendCount > m_basicInfo.suspend_count)
    {
        while (m_suspendCount > m_basicInfo.suspend_count)
        {
            err = ::thread_resume (m_tid);
            if (err.Success())
                --m_suspendCount;
            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
                err.LogThreaded("::thread_resume (%4.4x)", m_tid);
        }
    }
    else if (m_suspendCount < m_basicInfo.suspend_count)
    {
        while (m_suspendCount < m_basicInfo.suspend_count)
        {
            err = ::thread_suspend (m_tid);
            if (err.Success())
                --m_suspendCount;
            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
                err.LogThreaded("::thread_suspend (%4.4x)", m_tid);
        }
    }
    return  m_suspendCount == m_basicInfo.suspend_count;
}
Esempio n. 2
0
//----------------------------------------------------------------------
// MachTask::Resume
//----------------------------------------------------------------------
kern_return_t
MachTask::Resume()
{
    struct task_basic_info task_info;
    task_t task = TaskPort();
	if (task == TASK_NULL)
		return KERN_INVALID_ARGUMENT;

    DNBError err;
    err = BasicInfo(task, &task_info);

    if (err.Success())
    {
		// task_resume isn't counted like task_suspend calls are, are, so if the 
		// task is not suspended, don't try and resume it since it is already 
		// running
		if (task_info.suspend_count > 0)
        {
            err = ::task_resume (task);
            if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
                err.LogThreaded("::task_resume ( target_task = 0x%4.4x )", task);
        }
    }
    return err.Error();
}
Esempio n. 3
0
//----------------------------------------------------------------------
// MachTask::BasicInfo
//----------------------------------------------------------------------
kern_return_t
MachTask::BasicInfo(task_t task, struct task_basic_info *info)
{
    if (info == NULL)
        return KERN_INVALID_ARGUMENT;

    DNBError err;
    mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
    err = ::task_info (task, TASK_BASIC_INFO, (task_info_t)info, &count);
    const bool log_process = DNBLogCheckLogBit(LOG_TASK);
    if (log_process || err.Fail())
        err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => %u )", task, info, count);
    if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && err.Success())
    {
        float user = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f;
        float system = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f;
        DNBLogThreaded ("task_basic_info = { suspend_count = %i, virtual_size = 0x%8.8llx, resident_size = 0x%8.8llx, user_time = %f, system_time = %f }",
                        info->suspend_count, 
                        (uint64_t)info->virtual_size, 
                        (uint64_t)info->resident_size, 
                        user, 
                        system);
    }
    return err.Error();
}
Esempio n. 4
0
bool
MachThread::RestoreSuspendCountAfterStop ()
{
    DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
    DNBError err;
    if (ThreadIDIsValid(m_tid) == false)
        return false;
        
    if (m_suspend_count > 0)
    {
        while (m_suspend_count > 0)
        {
            err = ::thread_resume (m_tid);
            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
                err.LogThreaded("::thread_resume (%4.4x)", m_tid);
            if (err.Success())
                --m_suspend_count;
            else
            {
                if (GetBasicInfo())
                    m_suspend_count = m_basic_info.suspend_count;
                else
                    m_suspend_count = 0;
                return false; // ??? 
            }
        }
    }
    else if (m_suspend_count < 0)
    {
        while (m_suspend_count < 0)
        {
            err = ::thread_suspend (m_tid);
            if (err.Success())
                ++m_suspend_count;
            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
            {
                err.LogThreaded("::thread_suspend (%4.4x)", m_tid);
                return false;
            }
        }
    }
    return true;
}
Esempio n. 5
0
bool
MachTask::StartExceptionThread(DNBError &err)
{
    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__);
    task_t task = TaskPortForProcessID(err);
    if (MachTask::IsValid(task))
    {
        // Got the mach port for the current process
        mach_port_t task_self = mach_task_self ();

        // Allocate an exception port that we will use to track our child process
        err = ::mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &m_exception_port);
        if (err.Fail())
            return false;

        // Add the ability to send messages on the new exception port
        err = ::mach_port_insert_right (task_self, m_exception_port, m_exception_port, MACH_MSG_TYPE_MAKE_SEND);
        if (err.Fail())
            return false;

        // Save the original state of the exception ports for our child process
        SaveExceptionPortInfo();

        // We weren't able to save the info for our exception ports, we must stop...
        if (m_exc_port_info.mask == 0)
        {
            err.SetErrorString("failed to get exception port info");
            return false;
        }

        // Set the ability to get all exceptions on this port
        err = ::task_set_exception_ports (task, m_exc_port_info.mask, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE);
        if (DNBLogCheckLogBit(LOG_EXCEPTIONS) || err.Fail())
        {
            err.LogThreaded("::task_set_exception_ports ( task = 0x%4.4x, exception_mask = 0x%8.8x, new_port = 0x%4.4x, behavior = 0x%8.8x, new_flavor = 0x%8.8x )",
                            task,
                            m_exc_port_info.mask,
                            m_exception_port,
                            (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES),
                            THREAD_STATE_NONE);
        }

        if (err.Fail())
            return false;

        // Create the exception thread
        err = ::pthread_create (&m_exception_thread, NULL, MachTask::ExceptionThread, this);
        return err.Success();
    }
    else
    {
        DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", __FUNCTION__);
    }
    return false;
}
Esempio n. 6
0
nub_addr_t
MachTask::GetDYLDAllImageInfosAddress (DNBError& err)
{
    struct hack_task_dyld_info dyld_info;
    mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
    // Make sure that COUNT isn't bigger than our hacked up struct hack_task_dyld_info.
    // If it is, then make COUNT smaller to match.
    if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)))
        count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t));

    task_t task = TaskPortForProcessID (err);
    if (err.Success())
    {
        err = ::task_info (task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count);
        if (err.Success())
        {
            // We now have the address of the all image infos structure
            return dyld_info.all_image_info_addr;
        }
    }
    return INVALID_NUB_ADDRESS;
}
Esempio n. 7
0
bool
MachThread::SetSuspendCountBeforeResume(bool others_stopped)
{
    DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
    DNBError err;
    if (ThreadIDIsValid(m_tid) == false)
        return false;
        
    size_t times_to_resume;
        
    if (others_stopped)
    {
        if (GetBasicInfo())
        {
            times_to_resume = m_basic_info.suspend_count;
            m_suspend_count = - (times_to_resume - m_suspend_count);
        }
        else
            times_to_resume = 0;
    }
    else
    {
        times_to_resume = m_suspend_count;
        m_suspend_count = 0;
    }

    if (times_to_resume > 0)
    {
        while (times_to_resume > 0)
        {
            err = ::thread_resume (m_tid);
            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
                err.LogThreaded("::thread_resume (%4.4x)", m_tid);
            if (err.Success())
                --times_to_resume;
            else
            {
                if (GetBasicInfo())
                    times_to_resume = m_basic_info.suspend_count;
                else
                    times_to_resume = 0;
            }
        }
    }
    return true;
}
Esempio n. 8
0
bool
MachThread::RestoreSuspendCount()
{
    DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
    DNBError err;
    if (ThreadIDIsValid(m_tid) == false)
        return false;
    if (m_suspendCount > 0)
    {
        while (m_suspendCount > 0)
        {
            err = ::thread_resume (m_tid);
            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
                err.LogThreaded("::thread_resume (%4.4x)", m_tid);
            if (err.Success())
                --m_suspendCount;
            else
            {
                if (GetBasicInfo())
                    m_suspendCount = m_basicInfo.suspend_count;
                else
                    m_suspendCount = 0;
                return false; // ??? 
            }
        }
    }
    // We don't currently really support resuming a thread that was externally
    // suspended. If/when we do, we will need to make the code below work and
    // m_suspendCount will need to become signed instead of unsigned.
//    else if (m_suspendCount < 0)
//    {
//        while (m_suspendCount < 0)
//        {
//            err = ::thread_suspend (m_tid);
//            if (err.Success())
//                ++m_suspendCount;
//            if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
//                err.LogThreaded("::thread_suspend (%4.4x)", m_tid);
//        }
//    }
    return true;
}
Esempio n. 9
0
bool
MachTask::StartExceptionThread(DNBError &err)
{
    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__);
    task_t task = TaskPortForProcessID(err);
    if (MachTask::IsValid(task))
    {
        // Got the mach port for the current process
        mach_port_t task_self = mach_task_self ();

        // Allocate an exception port that we will use to track our child process
        err = ::mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &m_exception_port);
        if (err.Fail())
            return false;

        // Add the ability to send messages on the new exception port
        err = ::mach_port_insert_right (task_self, m_exception_port, m_exception_port, MACH_MSG_TYPE_MAKE_SEND);
        if (err.Fail())
            return false;

        // Save the original state of the exception ports for our child process
        SaveExceptionPortInfo();

        // Set the ability to get all exceptions on this port
        err = ::task_set_exception_ports (task, EXC_MASK_ALL, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE);
        if (err.Fail())
            return false;

        // Create the exception thread
        err = ::pthread_create (&m_exception_thread, NULL, MachTask::ExceptionThread, this);
        return err.Success();
    }
    else
    {
        DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", __FUNCTION__);
    }
    return false;
}
Esempio n. 10
0
//----------------------------------------------------------------------
// MachTask::TaskPortForProcessID
//----------------------------------------------------------------------
task_t
MachTask::TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries, uint32_t usec_interval)
{
	if (pid != INVALID_NUB_PROCESS)
	{
		DNBError err;
		mach_port_t task_self = mach_task_self ();	
		task_t task = TASK_NULL;
		for (uint32_t i=0; i<num_retries; i++)
		{	
			err = ::task_for_pid ( task_self, pid, &task);

            if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
            {
                char str[1024];
                ::snprintf (str,
                            sizeof(str),
                            "::task_for_pid ( target_tport = 0x%4.4x, pid = %d, &task ) => err = 0x%8.8x (%s)",
                            task_self,
                            pid,
                            err.Error(),
                            err.AsString() ? err.AsString() : "success");
                if (err.Fail())
                    err.SetErrorString(str);
                err.LogThreaded(str);
            }

			if (err.Success())
				return task;

			// Sleep a bit and try again
			::usleep (usec_interval);
		}
	}
	return TASK_NULL;
}
Esempio n. 11
0
std::string
MachTask::GetProfileData (DNBProfileDataScanType scanType)
{
    std::string result;
    
    static int32_t numCPU = -1;
    struct host_cpu_load_info host_info;
    if (scanType & eProfileHostCPU)
    {
        int32_t mib[] = {CTL_HW, HW_AVAILCPU};
        size_t len = sizeof(numCPU);
        if (numCPU == -1)
        {
            if (sysctl(mib, sizeof(mib) / sizeof(int32_t), &numCPU, &len, NULL, 0) != 0)
                return result;
        }
        
        mach_port_t localHost = mach_host_self();
        mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
        kern_return_t kr = host_statistics(localHost, HOST_CPU_LOAD_INFO, (host_info_t)&host_info, &count);
        if (kr != KERN_SUCCESS)
            return result;
    }
    
    task_t task = TaskPort();
    if (task == TASK_NULL)
        return result;
    
    struct task_basic_info task_info;
    DNBError err;
    err = BasicInfo(task, &task_info);
    
    if (!err.Success())
        return result;
    
    uint64_t elapsed_usec = 0;
    uint64_t task_used_usec = 0;
    if (scanType & eProfileCPU)
    {
        // Get current used time.
        struct timeval current_used_time;
        struct timeval tv;
        TIME_VALUE_TO_TIMEVAL(&task_info.user_time, &current_used_time);
        TIME_VALUE_TO_TIMEVAL(&task_info.system_time, &tv);
        timeradd(&current_used_time, &tv, &current_used_time);
        task_used_usec = current_used_time.tv_sec * 1000000ULL + current_used_time.tv_usec;
        
        struct timeval current_elapsed_time;
        int res = gettimeofday(&current_elapsed_time, NULL);
        if (res == 0)
        {
            elapsed_usec = current_elapsed_time.tv_sec * 1000000ULL + current_elapsed_time.tv_usec;
        }
    }
    
    std::vector<uint64_t> threads_id;
    std::vector<std::string> threads_name;
    std::vector<uint64_t> threads_used_usec;

    if (scanType & eProfileThreadsCPU)
    {
        get_threads_profile_data(scanType, task, m_process->ProcessID(), threads_id, threads_name, threads_used_usec);
    }
    
    struct vm_statistics vm_stats;
    uint64_t physical_memory;
    mach_vm_size_t rprvt = 0;
    mach_vm_size_t rsize = 0;
    mach_vm_size_t vprvt = 0;
    mach_vm_size_t vsize = 0;
    mach_vm_size_t dirty_size = 0;
    mach_vm_size_t purgeable = 0;
    mach_vm_size_t anonymous = 0;
    if (m_vm_memory.GetMemoryProfile(scanType, task, task_info, m_process->GetCPUType(), m_process->ProcessID(), vm_stats, physical_memory, rprvt, rsize, vprvt, vsize, dirty_size, purgeable, anonymous))
    {
        std::ostringstream profile_data_stream;
        
        if (scanType & eProfileHostCPU)
        {
            profile_data_stream << "num_cpu:" << numCPU << ';';
            profile_data_stream << "host_user_ticks:" << host_info.cpu_ticks[CPU_STATE_USER] << ';';
            profile_data_stream << "host_sys_ticks:" << host_info.cpu_ticks[CPU_STATE_SYSTEM] << ';';
            profile_data_stream << "host_idle_ticks:" << host_info.cpu_ticks[CPU_STATE_IDLE] << ';';
        }
        
        if (scanType & eProfileCPU)
        {
            profile_data_stream << "elapsed_usec:" << elapsed_usec << ';';
            profile_data_stream << "task_used_usec:" << task_used_usec << ';';
        }
        
        if (scanType & eProfileThreadsCPU)
        {
            int num_threads = threads_id.size();
            for (int i=0; i<num_threads; i++)
            {
                profile_data_stream << "thread_used_id:" << std::hex << threads_id[i] << std::dec << ';';
                profile_data_stream << "thread_used_usec:" << threads_used_usec[i] << ';';
                
                if (scanType & eProfileThreadName)
                {
                    profile_data_stream << "thread_used_name:";
                    int len = threads_name[i].size();
                    if (len)
                    {
                        const char *thread_name = threads_name[i].c_str();
                        // Make sure that thread name doesn't interfere with our delimiter.
                        profile_data_stream << RAW_HEXBASE << std::setw(2);
                        const uint8_t *ubuf8 = (const uint8_t *)(thread_name);
                        for (int j=0; j<len; j++)
                        {
                            profile_data_stream << (uint32_t)(ubuf8[j]);
                        }
                        // Reset back to DECIMAL.
                        profile_data_stream << DECIMAL;
                    }
                    profile_data_stream << ';';
                }
            }
        }
        
        if (scanType & eProfileHostMemory)
            profile_data_stream << "total:" << physical_memory << ';';
        
        if (scanType & eProfileMemory)
        {
            static vm_size_t pagesize;
            static bool calculated = false;
            if (!calculated)
            {
                calculated = true;
                pagesize = PageSize();
            }
            
            profile_data_stream << "wired:" << vm_stats.wire_count * pagesize << ';';
            profile_data_stream << "active:" << vm_stats.active_count * pagesize << ';';
            profile_data_stream << "inactive:" << vm_stats.inactive_count * pagesize << ';';
            uint64_t total_used_count = vm_stats.wire_count + vm_stats.inactive_count + vm_stats.active_count;
            profile_data_stream << "used:" << total_used_count * pagesize << ';';
            profile_data_stream << "free:" << vm_stats.free_count * pagesize << ';';
            
            profile_data_stream << "rprvt:" << rprvt << ';';
            profile_data_stream << "rsize:" << rsize << ';';
            profile_data_stream << "vprvt:" << vprvt << ';';
            profile_data_stream << "vsize:" << vsize << ';';
            
            if (scanType & eProfileMemoryDirtyPage)
                profile_data_stream << "dirty:" << dirty_size << ';';

            if (scanType & eProfileMemoryAnonymous)
            {
                profile_data_stream << "purgeable:" << purgeable << ';';
                profile_data_stream << "anonymous:" << anonymous << ';';
            }
        }
        
        profile_data_stream << "--end--;";
        
        result = profile_data_stream.str();
    }
    
    return result;
}