#include "MachTask.h"
#if defined (__arm__)
#include <CoreFoundation/CoreFoundation.h>
#include <SpringBoardServices/SpringBoardServer.h>
#include <SpringBoardServices/SBSWatchdogAssertion.h>
#endif
#include "lldb/Host/Endian.h"
#include "lldb/Host/Host.h"
#include "lldb/Core/DataExtractor.h"
#include "ProcessMacOSX.h"
#include "ProcessMacOSXLog.h"
using namespace lldb;
using namespace lldb_private;
MachTask::MachTask(ProcessMacOSX *process) :
m_process (process),
m_task (TASK_NULL),
m_vm_memory (),
m_exc_port_info(),
m_exception_thread (0),
m_exception_port (MACH_PORT_NULL)
{
memset(&m_exc_port_info, 0, sizeof(m_exc_port_info));
}
MachTask::~MachTask()
{
Clear();
}
kern_return_t
MachTask::Suspend()
{
Error err;
task_t task = GetTaskPort();
err = ::task_suspend (task);
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_TASK));
if (log || err.Fail())
err.PutToLog(log.get(), "::task_suspend ( target_task = 0x%4.4x )", task);
return err.GetError();
}
kern_return_t
MachTask::Resume()
{
Error err;
task_t task = GetTaskPort();
err = ::task_resume (task);
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_TASK));
if (log || err.Fail())
err.PutToLog(log.get(), "::task_resume ( target_task = 0x%4.4x )", task);
return err.GetError();
}
int32_t
MachTask::GetSuspendCount () const
{
struct task_basic_info task_info;
if (BasicInfo(&task_info) == KERN_SUCCESS)
return task_info.suspend_count;
return -1;
}
mach_port_t
MachTask::ExceptionPort() const
{
return m_exception_port;
}
bool
MachTask::ExceptionPortIsValid() const
{
return MACH_PORT_VALID(m_exception_port);
}
void
MachTask::Clear()
{
m_task = TASK_NULL;
m_exception_thread = 0;
m_exception_port = MACH_PORT_NULL;
}
kern_return_t
MachTask::SaveExceptionPortInfo()
{
return m_exc_port_info.Save(GetTaskPort());
}
kern_return_t
MachTask::RestoreExceptionPortInfo()
{
return m_exc_port_info.Restore(GetTaskPort());
}
size_t
MachTask::ReadMemory (lldb::addr_t addr, void *buf, size_t size, Error& error)
{
size_t n = 0;
task_t task = GetTaskPort();
if (task != TASK_NULL)
{
n = m_vm_memory.Read(task, addr, buf, size, error);
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_MEMORY));
if (log)
{
log->Printf ("MachTask::ReadMemory ( addr = 0x%16.16llx, size = %zu, buf = %8.8p) => %u bytes read", (uint64_t)addr, size, buf, n);
if (log->GetMask().Test(PD_LOG_MEMORY_DATA_LONG) || (log->GetMask().Test(PD_LOG_MEMORY_DATA_SHORT) && size <= 8))
{
DataExtractor data((uint8_t*)buf, n, lldb::endian::InlHostByteOrder(), 4);
data.PutToLog(log.get(), 0, n, addr, 16, DataExtractor::TypeUInt8);
}
}
}
return n;
}
size_t
MachTask::WriteMemory (lldb::addr_t addr, const void *buf, size_t size, Error& error)
{
size_t n = 0;
task_t task = GetTaskPort();
if (task != TASK_NULL)
{
n = m_vm_memory.Write(task, addr, buf, size, error);
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_MEMORY));
if (log)
{
log->Printf ("MachTask::WriteMemory ( addr = 0x%16.16llx, size = %zu, buf = %8.8p) => %u bytes written", (uint64_t)addr, size, buf, n);
if (log->GetMask().Test(PD_LOG_MEMORY_DATA_LONG) || (log->GetMask().Test(PD_LOG_MEMORY_DATA_SHORT) && size <= 8))
{
DataExtractor data((uint8_t*)buf, n, lldb::endian::InlHostByteOrder(), 4);
data.PutToLog(log.get(), 0, n, addr, 16, DataExtractor::TypeUInt8);
}
}
}
return n;
}
lldb::addr_t
MachTask::AllocateMemory (size_t size, uint32_t permissions, Error& error)
{
kern_return_t kret;
mach_vm_address_t addr;
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_MEMORY));
kret = ::mach_vm_allocate (GetTaskPort(), &addr, size, TRUE);
if (kret == KERN_SUCCESS)
{
vm_prot_t mach_prot = 0;
if (permissions & lldb::ePermissionsReadable)
mach_prot |= VM_PROT_READ;
if (permissions & lldb::ePermissionsWritable)
mach_prot |= VM_PROT_WRITE;
if (permissions & lldb::ePermissionsExecutable)
mach_prot |= VM_PROT_EXECUTE;
kret = ::mach_vm_protect (GetTaskPort(), addr, size, 0, mach_prot);
if (kret == KERN_SUCCESS)
{
if (log)
log->Printf("Allocated memory at addr = 0x%16.16llx, size = %zu, prot = 0x%x)", (uint64_t) addr, size, mach_prot);
m_allocations.insert (std::make_pair(addr, size));
return (lldb::addr_t) addr;
}
else
{
if (log)
log->Printf("Failed to set protections on memory at addr = 0x%16.16llx, size = %zu), prot = 0x%x", (uint64_t) addr, size, mach_prot);
kret = ::mach_vm_deallocate (GetTaskPort(), addr, size);
return LLDB_INVALID_ADDRESS;
}
}
else
{
if (log)
log->Printf("Failed to set allocate memory: size = %zu)", size);
return LLDB_INVALID_ADDRESS;
}
}
Error
MachTask::DeallocateMemory (lldb::addr_t ptr)
{
Error error;
allocation_collection::iterator pos, end = m_allocations.end();
for (pos = m_allocations.begin(); pos != end; pos++)
{
if ((*pos).first == ptr)
{
m_allocations.erase (pos);
error = ::mach_vm_deallocate (GetTaskPort(), (vm_address_t) ptr, (*pos).second);
return error;
}
}
error.SetErrorStringWithFormat("no memory allocated at 0x%llx", (uint64_t)ptr);
return error;
}
task_t
MachTask::GetTaskPortForProcessID (Error &err)
{
err.Clear();
if (m_task == TASK_NULL && m_process != NULL)
m_task = MachTask::GetTaskPortForProcessID(m_process->GetID(), err);
return m_task;
}
task_t
MachTask::GetTaskPortForProcessID (lldb::pid_t pid, Error &err)
{
task_t task = TASK_NULL;
if (pid != LLDB_INVALID_PROCESS_ID)
{
mach_port_t task_self = mach_task_self ();
err = ::task_for_pid ( task_self, pid, &task);
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_TASK));
if (log || err.Fail())
{
err.PutToLog(log.get(), "::task_for_pid ( target_tport = 0x%4.4x, pid = %d, task => 0x%4.4x ) %u/%u %u/%u", task_self, pid, task, getuid(), geteuid(), getgid(), getegid());
}
}
return task;
}
kern_return_t
MachTask::BasicInfo(struct task_basic_info *info) const
{
return BasicInfo (GetTaskPort(), info);
}
kern_return_t
MachTask::BasicInfo(task_t task, struct task_basic_info *info)
{
if (info == NULL)
return KERN_INVALID_ARGUMENT;
Error err;
mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
err = ::task_info (task, TASK_BASIC_INFO, (task_info_t)info, &count);
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_TASK));
if (log || err.Fail())
err.PutToLog(log.get(), "::task_info ( target_task = 0x%4.4x, flavor = TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => %u )", task, info, count);
if (log && log->GetMask().Test(PD_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;
log->Printf ("task_basic_info = { suspend_count = %i, virtual_size = 0x%8.8x, resident_size = 0x%8.8x, user_time = %f, system_time = %f }",
info->suspend_count, info->virtual_size, info->resident_size, user, system);
}
return err.GetError();
}
bool
MachTask::IsValid () const
{
return MachTask::IsValid(GetTaskPort());
}
bool
MachTask::IsValid (task_t task)
{
if (task != TASK_NULL)
{
struct task_basic_info task_info;
return BasicInfo(task, &task_info) == KERN_SUCCESS;
}
return false;
}
bool
MachTask::StartExceptionThread(Error &err)
{
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS));
if (log)
log->Printf ("MachTask::%s ( )", __FUNCTION__);
task_t task = GetTaskPortForProcessID(err);
if (MachTask::IsValid(task))
{
mach_port_t task_self = mach_task_self ();
err = ::mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &m_exception_port);
if (log || err.Fail())
err.PutToLog(log.get(), "::mach_port_allocate (task_self=0x%4.4x, MACH_PORT_RIGHT_RECEIVE, &m_exception_port => 0x%4.4x)",
task_self, m_exception_port);
if (err.Fail())
return false;
err = ::mach_port_insert_right (task_self, m_exception_port, m_exception_port, MACH_MSG_TYPE_MAKE_SEND);
if (log || err.Fail())
err.PutToLog(log.get(), "::mach_port_insert_right (task_self=0x%4.4x, m_exception_port=0x%4.4x, m_exception_port=0x%4.4x, MACH_MSG_TYPE_MAKE_SEND)",
task_self, m_exception_port, m_exception_port);
if (err.Fail())
return false;
err = SaveExceptionPortInfo();
err = ::task_set_exception_ports (task, EXC_MASK_ALL, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE);
if (log || err.Fail())
err.PutToLog(log.get(), "::task_set_exception_ports (task, EXC_MASK_ALL, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE)");
if (err.Fail())
return false;
char thread_name[256];
::snprintf (thread_name, sizeof(thread_name), "<lldb.process.process-macosx.mach-exception-%d>", m_process->GetID());
m_exception_thread = Host::ThreadCreate (thread_name, MachTask::ExceptionThread, this, &err);
return err.Success();
}
return false;
}
kern_return_t
MachTask::ShutDownExceptionThread()
{
Error err;
if (m_exception_thread == NULL)
return KERN_SUCCESS;
err = RestoreExceptionPortInfo();
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS));
mach_port_t exception_port = m_exception_port;
m_exception_port = NULL;
Host::ThreadCancel (m_exception_thread, &err);
if (log || err.Fail())
err.PutToLog(log.get(), "Host::ThreadCancel ( thread = %p )", m_exception_thread);
Host::ThreadJoin (m_exception_thread, NULL, &err);
if (log || err.Fail())
err.PutToLog(log.get(), "Host::ThreadJoin ( thread = %p, result_ptr = NULL)", m_exception_thread);
mach_port_t task_self = mach_task_self ();
err = ::mach_port_deallocate (task_self, exception_port);
if (log || err.Fail())
err.PutToLog(log.get(), "::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", task_self, exception_port);
exception_port = NULL;
Clear();
return err.GetError();
}
void *
MachTask::ExceptionThread (void *arg)
{
if (arg == NULL)
return NULL;
MachTask *mach_task = (MachTask*) arg;
ProcessMacOSX *mach_proc = mach_task->Process();
LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS));
if (log)
log->Printf ("MachTask::%s (arg = %p) thread starting...", __FUNCTION__, arg);
uint32_t num_exceptions_received = 0;
Error err;
task_t task = mach_task->GetTaskPort();
mach_msg_timeout_t periodic_timeout = 1000;
#if defined (__arm__)
mach_msg_timeout_t watchdog_elapsed = 0;
mach_msg_timeout_t watchdog_timeout = 60 * 1000;
lldb::pid_t pid = mach_proc->GetID();
CFReleaser<SBSWatchdogAssertionRef> watchdog;
if (mach_proc->ProcessUsingSpringBoard())
{
watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60));
if (log)
log->Printf ("::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", pid, watchdog.get());
if (watchdog.get())
{
::SBSWatchdogAssertionRenew (watchdog.get());
CFTimeInterval watchdogRenewalInterval = ::SBSWatchdogAssertionGetRenewalInterval (watchdog.get());
if (log)
log->Printf ("::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", watchdog.get(), watchdogRenewalInterval);
if (watchdogRenewalInterval > 0.0)
{
watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000;
if (watchdog_timeout > 3000)
watchdog_timeout -= 1000; else if (watchdog_timeout > 1000)
watchdog_timeout -= 250; }
}
if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout)
periodic_timeout = watchdog_timeout;
}
#endif // #if defined (__arm__)
while (mach_task->ExceptionPortIsValid())
{
MachException::Message exception_message;
if (num_exceptions_received > 0)
{
err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 0);
}
else if (periodic_timeout > 0)
{
err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, periodic_timeout);
}
else
{
err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0);
}
if (err.GetError() == MACH_RCV_INTERRUPTED)
{
log = ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS);
if (!mach_task->ExceptionPortIsValid())
{
if (log)
log->Printf ("thread cancelled...");
break;
}
if (MachTask::IsValid(task))
{
if (log)
log->Printf ("interrupted, but task still valid, continuing...");
continue;
}
else
{
if (log)
log->Printf ("task has exited...");
mach_proc->SetPrivateState (eStateExited);
break;
}
}
else if (err.GetError() == MACH_RCV_TIMED_OUT)
{
if (num_exceptions_received > 0)
{
log = ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS);
num_exceptions_received = 0;
mach_proc->ExceptionMessageBundleComplete();
if (MachTask::IsValid(task))
{
if (log)
log->Printf ("got a timeout, continuing...");
continue;
}
else
{
if (log)
log->Printf ("task has exited...");
mach_proc->SetPrivateState (eStateExited);
break;
}
continue;
}
#if defined (__arm__)
if (watchdog.get())
{
watchdog_elapsed += periodic_timeout;
if (watchdog_elapsed >= watchdog_timeout)
{
LogIf(PD_LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", watchdog.get());
::SBSWatchdogAssertionRenew (watchdog.get());
watchdog_elapsed = 0;
}
}
#endif
}
else if (err.GetError() != KERN_SUCCESS)
{
log = ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS);
if (log)
log->Printf ("got some other error, do something about it??? nah, continuing for now...");
}
else
{
if (exception_message.CatchExceptionRaise())
{
++num_exceptions_received;
mach_proc->ExceptionMessageReceived(exception_message);
}
}
}
#if defined (__arm__)
if (watchdog.get())
{
LogIf(PD_LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", watchdog.get());
::SBSWatchdogAssertionRelease (watchdog.get());
}
#endif // #if defined (__arm__)
log = ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_EXCEPTIONS);
if (log)
log->Printf ("MachTask::%s (arg = %p) thread exiting...", __FUNCTION__, arg);
return NULL;
}
lldb::addr_t
MachTask::GetDYLDAllImageInfosAddress ()
{
#ifdef TASK_DYLD_INFO
task_dyld_info_data_t dyld_info;
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
Error err;
kern_return_t kret = ::task_info (GetTaskPortForProcessID (err), TASK_DYLD_INFO, (task_info_t)&dyld_info, &count);
if (kret == KERN_SUCCESS)
{
return dyld_info.all_image_info_addr;
}
#endif
return LLDB_INVALID_ADDRESS;
}