MachTask.cpp revision 2a647e4b0eb044cffab5c8c29b6af9ed52129544
1//===-- MachTask.cpp --------------------------------------------*- 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.cpp
12//  debugserver
13//
14//  Created by Greg Clayton on 12/5/08.
15//
16//===----------------------------------------------------------------------===//
17
18#include "MachTask.h"
19
20// C Includes
21
22#include <mach-o/dyld_images.h>
23#include <mach/mach_vm.h>
24#import <sys/sysctl.h>
25
26// C++ Includes
27#include <iomanip>
28#include <sstream>
29
30// Other libraries and framework includes
31// Project includes
32#include "CFUtils.h"
33#include "DNB.h"
34#include "DNBError.h"
35#include "DNBLog.h"
36#include "MachProcess.h"
37#include "DNBDataRef.h"
38#include "stack_logging.h"
39
40#ifdef WITH_SPRINGBOARD
41
42#include <CoreFoundation/CoreFoundation.h>
43#include <SpringBoardServices/SpringBoardServer.h>
44#include <SpringBoardServices/SBSWatchdogAssertion.h>
45
46#endif
47
48//----------------------------------------------------------------------
49// MachTask constructor
50//----------------------------------------------------------------------
51MachTask::MachTask(MachProcess *process) :
52    m_process (process),
53    m_task (TASK_NULL),
54    m_vm_memory (),
55    m_exception_thread (0),
56    m_exception_port (MACH_PORT_NULL)
57{
58    memset(&m_exc_port_info, 0, sizeof(m_exc_port_info));
59}
60
61//----------------------------------------------------------------------
62// Destructor
63//----------------------------------------------------------------------
64MachTask::~MachTask()
65{
66    Clear();
67}
68
69
70//----------------------------------------------------------------------
71// MachTask::Suspend
72//----------------------------------------------------------------------
73kern_return_t
74MachTask::Suspend()
75{
76    DNBError err;
77    task_t task = TaskPort();
78    err = ::task_suspend (task);
79    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
80        err.LogThreaded("::task_suspend ( target_task = 0x%4.4x )", task);
81    return err.Error();
82}
83
84
85//----------------------------------------------------------------------
86// MachTask::Resume
87//----------------------------------------------------------------------
88kern_return_t
89MachTask::Resume()
90{
91    struct task_basic_info task_info;
92    task_t task = TaskPort();
93    if (task == TASK_NULL)
94        return KERN_INVALID_ARGUMENT;
95
96    DNBError err;
97    err = BasicInfo(task, &task_info);
98
99    if (err.Success())
100    {
101        // task_resume isn't counted like task_suspend calls are, are, so if the
102        // task is not suspended, don't try and resume it since it is already
103        // running
104        if (task_info.suspend_count > 0)
105        {
106            err = ::task_resume (task);
107            if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
108                err.LogThreaded("::task_resume ( target_task = 0x%4.4x )", task);
109        }
110    }
111    return err.Error();
112}
113
114//----------------------------------------------------------------------
115// MachTask::ExceptionPort
116//----------------------------------------------------------------------
117mach_port_t
118MachTask::ExceptionPort() const
119{
120    return m_exception_port;
121}
122
123//----------------------------------------------------------------------
124// MachTask::ExceptionPortIsValid
125//----------------------------------------------------------------------
126bool
127MachTask::ExceptionPortIsValid() const
128{
129    return MACH_PORT_VALID(m_exception_port);
130}
131
132
133//----------------------------------------------------------------------
134// MachTask::Clear
135//----------------------------------------------------------------------
136void
137MachTask::Clear()
138{
139    // Do any cleanup needed for this task
140    m_task = TASK_NULL;
141    m_exception_thread = 0;
142    m_exception_port = MACH_PORT_NULL;
143
144}
145
146
147//----------------------------------------------------------------------
148// MachTask::SaveExceptionPortInfo
149//----------------------------------------------------------------------
150kern_return_t
151MachTask::SaveExceptionPortInfo()
152{
153    return m_exc_port_info.Save(TaskPort());
154}
155
156//----------------------------------------------------------------------
157// MachTask::RestoreExceptionPortInfo
158//----------------------------------------------------------------------
159kern_return_t
160MachTask::RestoreExceptionPortInfo()
161{
162    return m_exc_port_info.Restore(TaskPort());
163}
164
165
166//----------------------------------------------------------------------
167// MachTask::ReadMemory
168//----------------------------------------------------------------------
169nub_size_t
170MachTask::ReadMemory (nub_addr_t addr, nub_size_t size, void *buf)
171{
172    nub_size_t n = 0;
173    task_t task = TaskPort();
174    if (task != TASK_NULL)
175    {
176        n = m_vm_memory.Read(task, addr, buf, size);
177
178        DNBLogThreadedIf(LOG_MEMORY, "MachTask::ReadMemory ( addr = 0x%8.8llx, size = %llu, buf = %p) => %llu bytes read", (uint64_t)addr, (uint64_t)size, buf, (uint64_t)n);
179        if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8))
180        {
181            DNBDataRef data((uint8_t*)buf, n, false);
182            data.Dump(0, n, addr, DNBDataRef::TypeUInt8, 16);
183        }
184    }
185    return n;
186}
187
188
189//----------------------------------------------------------------------
190// MachTask::WriteMemory
191//----------------------------------------------------------------------
192nub_size_t
193MachTask::WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf)
194{
195    nub_size_t n = 0;
196    task_t task = TaskPort();
197    if (task != TASK_NULL)
198    {
199        n = m_vm_memory.Write(task, addr, buf, size);
200        DNBLogThreadedIf(LOG_MEMORY, "MachTask::WriteMemory ( addr = 0x%8.8llx, size = %llu, buf = %p) => %llu bytes written", (uint64_t)addr, (uint64_t)size, buf, (uint64_t)n);
201        if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8))
202        {
203            DNBDataRef data((uint8_t*)buf, n, false);
204            data.Dump(0, n, addr, DNBDataRef::TypeUInt8, 16);
205        }
206    }
207    return n;
208}
209
210//----------------------------------------------------------------------
211// MachTask::MemoryRegionInfo
212//----------------------------------------------------------------------
213int
214MachTask::GetMemoryRegionInfo (nub_addr_t addr, DNBRegionInfo *region_info)
215{
216    task_t task = TaskPort();
217    if (task == TASK_NULL)
218        return -1;
219
220    int ret = m_vm_memory.GetMemoryRegionInfo(task, addr, region_info);
221    DNBLogThreadedIf(LOG_MEMORY, "MachTask::MemoryRegionInfo ( addr = 0x%8.8llx ) => %i  (start = 0x%8.8llx, size = 0x%8.8llx, permissions = %u)",
222                     (uint64_t)addr,
223                     ret,
224                     (uint64_t)region_info->addr,
225                     (uint64_t)region_info->size,
226                     region_info->permissions);
227    return ret;
228}
229
230#define TIME_VALUE_TO_TIMEVAL(a, r) do {        \
231(r)->tv_sec = (a)->seconds;                     \
232(r)->tv_usec = (a)->microseconds;               \
233} while (0)
234
235// We should consider moving this into each MacThread.
236static void get_threads_profile_data(DNBProfileDataScanType scanType, task_t task, nub_process_t pid, std::vector<uint64_t> &threads_id, std::vector<std::string> &threads_name, std::vector<uint64_t> &threads_used_usec)
237{
238    kern_return_t kr;
239    thread_act_array_t threads;
240    mach_msg_type_number_t tcnt;
241
242    kr = task_threads(task, &threads, &tcnt);
243    if (kr != KERN_SUCCESS)
244        return;
245
246    for (int i = 0; i < tcnt; i++)
247    {
248        thread_identifier_info_data_t identifier_info;
249        mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
250        kr = ::thread_info(threads[i], THREAD_IDENTIFIER_INFO, (thread_info_t)&identifier_info, &count);
251        if (kr != KERN_SUCCESS) continue;
252
253        thread_basic_info_data_t basic_info;
254        count = THREAD_BASIC_INFO_COUNT;
255        kr = ::thread_info(threads[i], THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count);
256        if (kr != KERN_SUCCESS) continue;
257
258        if ((basic_info.flags & TH_FLAGS_IDLE) == 0)
259        {
260            nub_thread_t tid = MachThread::GetGloballyUniqueThreadIDForMachPortID (threads[i]);
261            threads_id.push_back(tid);
262
263            if ((scanType & eProfileThreadName) && (identifier_info.thread_handle != 0))
264            {
265                struct proc_threadinfo proc_threadinfo;
266                int len = ::proc_pidinfo(pid, PROC_PIDTHREADINFO, identifier_info.thread_handle, &proc_threadinfo, PROC_PIDTHREADINFO_SIZE);
267                if (len && proc_threadinfo.pth_name[0])
268                {
269                    threads_name.push_back(proc_threadinfo.pth_name);
270                }
271                else
272                {
273                    threads_name.push_back("");
274                }
275            }
276            else
277            {
278                threads_name.push_back("");
279            }
280            struct timeval tv;
281            struct timeval thread_tv;
282            TIME_VALUE_TO_TIMEVAL(&basic_info.user_time, &thread_tv);
283            TIME_VALUE_TO_TIMEVAL(&basic_info.system_time, &tv);
284            timeradd(&thread_tv, &tv, &thread_tv);
285            uint64_t used_usec = thread_tv.tv_sec * 1000000ULL + thread_tv.tv_usec;
286            threads_used_usec.push_back(used_usec);
287        }
288
289        kr = mach_port_deallocate(mach_task_self(), threads[i]);
290    }
291    kr = mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)(uintptr_t)threads, tcnt * sizeof(*threads));
292}
293
294#define RAW_HEXBASE     std::setfill('0') << std::hex << std::right
295#define DECIMAL         std::dec << std::setfill(' ')
296std::string
297MachTask::GetProfileData (DNBProfileDataScanType scanType)
298{
299    std::string result;
300
301    static int32_t numCPU = -1;
302    struct host_cpu_load_info host_info;
303    if (scanType & eProfileHostCPU)
304    {
305        int32_t mib[] = {CTL_HW, HW_AVAILCPU};
306        size_t len = sizeof(numCPU);
307        if (numCPU == -1)
308        {
309            if (sysctl(mib, sizeof(mib) / sizeof(int32_t), &numCPU, &len, NULL, 0) != 0)
310                return result;
311        }
312
313        mach_port_t localHost = mach_host_self();
314        mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
315        kern_return_t kr = host_statistics(localHost, HOST_CPU_LOAD_INFO, (host_info_t)&host_info, &count);
316        if (kr != KERN_SUCCESS)
317            return result;
318    }
319
320    task_t task = TaskPort();
321    if (task == TASK_NULL)
322        return result;
323
324    struct task_basic_info task_info;
325    DNBError err;
326    err = BasicInfo(task, &task_info);
327
328    if (!err.Success())
329        return result;
330
331    uint64_t elapsed_usec = 0;
332    uint64_t task_used_usec = 0;
333    if (scanType & eProfileCPU)
334    {
335        // Get current used time.
336        struct timeval current_used_time;
337        struct timeval tv;
338        TIME_VALUE_TO_TIMEVAL(&task_info.user_time, &current_used_time);
339        TIME_VALUE_TO_TIMEVAL(&task_info.system_time, &tv);
340        timeradd(&current_used_time, &tv, &current_used_time);
341        task_used_usec = current_used_time.tv_sec * 1000000ULL + current_used_time.tv_usec;
342
343        struct timeval current_elapsed_time;
344        int res = gettimeofday(&current_elapsed_time, NULL);
345        if (res == 0)
346        {
347            elapsed_usec = current_elapsed_time.tv_sec * 1000000ULL + current_elapsed_time.tv_usec;
348        }
349    }
350
351    std::vector<uint64_t> threads_id;
352    std::vector<std::string> threads_name;
353    std::vector<uint64_t> threads_used_usec;
354
355    if (scanType & eProfileThreadsCPU)
356    {
357        get_threads_profile_data(scanType, task, m_process->ProcessID(), threads_id, threads_name, threads_used_usec);
358    }
359
360    struct vm_statistics vm_stats;
361    uint64_t physical_memory;
362    mach_vm_size_t rprvt = 0;
363    mach_vm_size_t rsize = 0;
364    mach_vm_size_t vprvt = 0;
365    mach_vm_size_t vsize = 0;
366    mach_vm_size_t dirty_size = 0;
367    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))
368    {
369        std::ostringstream profile_data_stream;
370
371        if (scanType & eProfileHostCPU)
372        {
373            profile_data_stream << "num_cpu:" << numCPU << ';';
374            profile_data_stream << "host_user_ticks:" << host_info.cpu_ticks[CPU_STATE_USER] << ';';
375            profile_data_stream << "host_sys_ticks:" << host_info.cpu_ticks[CPU_STATE_SYSTEM] << ';';
376            profile_data_stream << "host_idle_ticks:" << host_info.cpu_ticks[CPU_STATE_IDLE] << ';';
377        }
378
379        if (scanType & eProfileCPU)
380        {
381            profile_data_stream << "elapsed_usec:" << elapsed_usec << ';';
382            profile_data_stream << "task_used_usec:" << task_used_usec << ';';
383        }
384
385        if (scanType & eProfileThreadsCPU)
386        {
387            int num_threads = threads_id.size();
388            for (int i=0; i<num_threads; i++)
389            {
390                profile_data_stream << "thread_used_id:" << std::hex << threads_id[i] << std::dec << ';';
391                profile_data_stream << "thread_used_usec:" << threads_used_usec[i] << ';';
392
393                if (scanType & eProfileThreadName)
394                {
395                    profile_data_stream << "thread_used_name:";
396                    int len = threads_name[i].size();
397                    if (len)
398                    {
399                        const char *thread_name = threads_name[i].c_str();
400                        // Make sure that thread name doesn't interfere with our delimiter.
401                        profile_data_stream << RAW_HEXBASE << std::setw(2);
402                        const uint8_t *ubuf8 = (const uint8_t *)(thread_name);
403                        for (int j=0; j<len; j++)
404                        {
405                            profile_data_stream << (uint32_t)(ubuf8[j]);
406                        }
407                        // Reset back to DECIMAL.
408                        profile_data_stream << DECIMAL;
409                    }
410                    profile_data_stream << ';';
411                }
412            }
413        }
414
415        if (scanType & eProfileHostMemory)
416            profile_data_stream << "total:" << physical_memory << ';';
417
418        if (scanType & eProfileMemory)
419        {
420            static vm_size_t pagesize;
421            static bool calculated = false;
422            if (!calculated)
423            {
424                calculated = true;
425                host_page_size(mach_host_self(), &pagesize);
426            }
427
428            profile_data_stream << "wired:" << vm_stats.wire_count * pagesize << ';';
429            profile_data_stream << "active:" << vm_stats.active_count * pagesize << ';';
430            profile_data_stream << "inactive:" << vm_stats.inactive_count * pagesize << ';';
431            uint64_t total_used_count = vm_stats.wire_count + vm_stats.inactive_count + vm_stats.active_count;
432            profile_data_stream << "used:" << total_used_count * pagesize << ';';
433            profile_data_stream << "free:" << vm_stats.free_count * pagesize << ';';
434
435            profile_data_stream << "rprvt:" << rprvt << ';';
436            profile_data_stream << "rsize:" << rsize << ';';
437            profile_data_stream << "vprvt:" << vprvt << ';';
438            profile_data_stream << "vsize:" << vsize << ';';
439
440            if (scanType & eProfileMemoryDirtyPage)
441                profile_data_stream << "dirty:" << dirty_size << ';';
442        }
443
444        profile_data_stream << "--end--;";
445
446        result = profile_data_stream.str();
447    }
448
449    return result;
450}
451
452
453//----------------------------------------------------------------------
454// MachTask::TaskPortForProcessID
455//----------------------------------------------------------------------
456task_t
457MachTask::TaskPortForProcessID (DNBError &err)
458{
459    if (m_task == TASK_NULL && m_process != NULL)
460        m_task = MachTask::TaskPortForProcessID(m_process->ProcessID(), err);
461    return m_task;
462}
463
464//----------------------------------------------------------------------
465// MachTask::TaskPortForProcessID
466//----------------------------------------------------------------------
467task_t
468MachTask::TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries, uint32_t usec_interval)
469{
470    if (pid != INVALID_NUB_PROCESS)
471    {
472        DNBError err;
473        mach_port_t task_self = mach_task_self ();
474        task_t task = TASK_NULL;
475        for (uint32_t i=0; i<num_retries; i++)
476        {
477            err = ::task_for_pid ( task_self, pid, &task);
478
479            if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
480            {
481                char str[1024];
482                ::snprintf (str,
483                            sizeof(str),
484                            "::task_for_pid ( target_tport = 0x%4.4x, pid = %d, &task ) => err = 0x%8.8x (%s)",
485                            task_self,
486                            pid,
487                            err.Error(),
488                            err.AsString() ? err.AsString() : "success");
489                if (err.Fail())
490                    err.SetErrorString(str);
491                err.LogThreaded(str);
492            }
493
494            if (err.Success())
495                return task;
496
497            // Sleep a bit and try again
498            ::usleep (usec_interval);
499        }
500    }
501    return TASK_NULL;
502}
503
504
505//----------------------------------------------------------------------
506// MachTask::BasicInfo
507//----------------------------------------------------------------------
508kern_return_t
509MachTask::BasicInfo(struct task_basic_info *info)
510{
511    return BasicInfo (TaskPort(), info);
512}
513
514//----------------------------------------------------------------------
515// MachTask::BasicInfo
516//----------------------------------------------------------------------
517kern_return_t
518MachTask::BasicInfo(task_t task, struct task_basic_info *info)
519{
520    if (info == NULL)
521        return KERN_INVALID_ARGUMENT;
522
523    DNBError err;
524    mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
525    err = ::task_info (task, TASK_BASIC_INFO, (task_info_t)info, &count);
526    const bool log_process = DNBLogCheckLogBit(LOG_TASK);
527    if (log_process || err.Fail())
528        err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => %u )", task, info, count);
529    if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && err.Success())
530    {
531        float user = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f;
532        float system = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f;
533        DNBLogThreaded ("task_basic_info = { suspend_count = %i, virtual_size = 0x%8.8llx, resident_size = 0x%8.8llx, user_time = %f, system_time = %f }",
534                        info->suspend_count,
535                        (uint64_t)info->virtual_size,
536                        (uint64_t)info->resident_size,
537                        user,
538                        system);
539    }
540    return err.Error();
541}
542
543
544//----------------------------------------------------------------------
545// MachTask::IsValid
546//
547// Returns true if a task is a valid task port for a current process.
548//----------------------------------------------------------------------
549bool
550MachTask::IsValid () const
551{
552    return MachTask::IsValid(TaskPort());
553}
554
555//----------------------------------------------------------------------
556// MachTask::IsValid
557//
558// Returns true if a task is a valid task port for a current process.
559//----------------------------------------------------------------------
560bool
561MachTask::IsValid (task_t task)
562{
563    if (task != TASK_NULL)
564    {
565        struct task_basic_info task_info;
566        return BasicInfo(task, &task_info) == KERN_SUCCESS;
567    }
568    return false;
569}
570
571
572bool
573MachTask::StartExceptionThread(DNBError &err)
574{
575    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__);
576    task_t task = TaskPortForProcessID(err);
577    if (MachTask::IsValid(task))
578    {
579        // Got the mach port for the current process
580        mach_port_t task_self = mach_task_self ();
581
582        // Allocate an exception port that we will use to track our child process
583        err = ::mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &m_exception_port);
584        if (err.Fail())
585            return false;
586
587        // Add the ability to send messages on the new exception port
588        err = ::mach_port_insert_right (task_self, m_exception_port, m_exception_port, MACH_MSG_TYPE_MAKE_SEND);
589        if (err.Fail())
590            return false;
591
592        // Save the original state of the exception ports for our child process
593        SaveExceptionPortInfo();
594
595        // We weren't able to save the info for our exception ports, we must stop...
596        if (m_exc_port_info.mask == 0)
597        {
598            err.SetErrorString("failed to get exception port info");
599            return false;
600        }
601
602        // Set the ability to get all exceptions on this port
603        err = ::task_set_exception_ports (task, m_exc_port_info.mask, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE);
604        if (DNBLogCheckLogBit(LOG_EXCEPTIONS) || err.Fail())
605        {
606            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 )",
607                            task,
608                            m_exc_port_info.mask,
609                            m_exception_port,
610                            (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES),
611                            THREAD_STATE_NONE);
612        }
613
614        if (err.Fail())
615            return false;
616
617        // Create the exception thread
618        err = ::pthread_create (&m_exception_thread, NULL, MachTask::ExceptionThread, this);
619        return err.Success();
620    }
621    else
622    {
623        DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", __FUNCTION__);
624    }
625    return false;
626}
627
628kern_return_t
629MachTask::ShutDownExcecptionThread()
630{
631    DNBError err;
632
633    err = RestoreExceptionPortInfo();
634
635    // NULL our our exception port and let our exception thread exit
636    mach_port_t exception_port = m_exception_port;
637    m_exception_port = NULL;
638
639    err.SetError(::pthread_cancel(m_exception_thread), DNBError::POSIX);
640    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
641        err.LogThreaded("::pthread_cancel ( thread = %p )", m_exception_thread);
642
643    err.SetError(::pthread_join(m_exception_thread, NULL), DNBError::POSIX);
644    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
645        err.LogThreaded("::pthread_join ( thread = %p, value_ptr = NULL)", m_exception_thread);
646
647    // Deallocate our exception port that we used to track our child process
648    mach_port_t task_self = mach_task_self ();
649    err = ::mach_port_deallocate (task_self, exception_port);
650    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
651        err.LogThreaded("::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", task_self, exception_port);
652
653    return err.Error();
654}
655
656
657void *
658MachTask::ExceptionThread (void *arg)
659{
660    if (arg == NULL)
661        return NULL;
662
663    MachTask *mach_task = (MachTask*) arg;
664    MachProcess *mach_proc = mach_task->Process();
665    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( arg = %p ) starting thread...", __FUNCTION__, arg);
666
667    // We keep a count of the number of consecutive exceptions received so
668    // we know to grab all exceptions without a timeout. We do this to get a
669    // bunch of related exceptions on our exception port so we can process
670    // then together. When we have multiple threads, we can get an exception
671    // per thread and they will come in consecutively. The main loop in this
672    // thread can stop periodically if needed to service things related to this
673    // process.
674    // flag set in the options, so we will wait forever for an exception on
675    // our exception port. After we get one exception, we then will use the
676    // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current
677    // exceptions for our process. After we have received the last pending
678    // exception, we will get a timeout which enables us to then notify
679    // our main thread that we have an exception bundle avaiable. We then wait
680    // for the main thread to tell this exception thread to start trying to get
681    // exceptions messages again and we start again with a mach_msg read with
682    // infinite timeout.
683    uint32_t num_exceptions_received = 0;
684    DNBError err;
685    task_t task = mach_task->TaskPort();
686    mach_msg_timeout_t periodic_timeout = 0;
687
688#ifdef WITH_SPRINGBOARD
689    mach_msg_timeout_t watchdog_elapsed = 0;
690    mach_msg_timeout_t watchdog_timeout = 60 * 1000;
691    pid_t pid = mach_proc->ProcessID();
692    CFReleaser<SBSWatchdogAssertionRef> watchdog;
693
694    if (mach_proc->ProcessUsingSpringBoard())
695    {
696        // Request a renewal for every 60 seconds if we attached using SpringBoard
697        watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60));
698        DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", pid, watchdog.get());
699
700        if (watchdog.get())
701        {
702            ::SBSWatchdogAssertionRenew (watchdog.get());
703
704            CFTimeInterval watchdogRenewalInterval = ::SBSWatchdogAssertionGetRenewalInterval (watchdog.get());
705            DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", watchdog.get(), watchdogRenewalInterval);
706            if (watchdogRenewalInterval > 0.0)
707            {
708                watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000;
709                if (watchdog_timeout > 3000)
710                    watchdog_timeout -= 1000;   // Give us a second to renew our timeout
711                else if (watchdog_timeout > 1000)
712                    watchdog_timeout -= 250;    // Give us a quarter of a second to renew our timeout
713            }
714        }
715        if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout)
716            periodic_timeout = watchdog_timeout;
717    }
718#endif  // #ifdef WITH_SPRINGBOARD
719
720    while (mach_task->ExceptionPortIsValid())
721    {
722        ::pthread_testcancel ();
723
724        MachException::Message exception_message;
725
726
727        if (num_exceptions_received > 0)
728        {
729            // No timeout, just receive as many exceptions as we can since we already have one and we want
730            // to get all currently available exceptions for this task
731            err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 0);
732        }
733        else if (periodic_timeout > 0)
734        {
735            // We need to stop periodically in this loop, so try and get a mach message with a valid timeout (ms)
736            err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, periodic_timeout);
737        }
738        else
739        {
740            // We don't need to parse all current exceptions or stop periodically,
741            // just wait for an exception forever.
742            err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0);
743        }
744
745        if (err.Error() == MACH_RCV_INTERRUPTED)
746        {
747            // If we have no task port we should exit this thread
748            if (!mach_task->ExceptionPortIsValid())
749            {
750                DNBLogThreadedIf(LOG_EXCEPTIONS, "thread cancelled...");
751                break;
752            }
753
754            // Make sure our task is still valid
755            if (MachTask::IsValid(task))
756            {
757                // Task is still ok
758                DNBLogThreadedIf(LOG_EXCEPTIONS, "interrupted, but task still valid, continuing...");
759                continue;
760            }
761            else
762            {
763                DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited...");
764                mach_proc->SetState(eStateExited);
765                // Our task has died, exit the thread.
766                break;
767            }
768        }
769        else if (err.Error() == MACH_RCV_TIMED_OUT)
770        {
771            if (num_exceptions_received > 0)
772            {
773                // We were receiving all current exceptions with a timeout of zero
774                // it is time to go back to our normal looping mode
775                num_exceptions_received = 0;
776
777                // Notify our main thread we have a complete exception message
778                // bundle available.
779                mach_proc->ExceptionMessageBundleComplete();
780
781                // in case we use a timeout value when getting exceptions...
782                // Make sure our task is still valid
783                if (MachTask::IsValid(task))
784                {
785                    // Task is still ok
786                    DNBLogThreadedIf(LOG_EXCEPTIONS, "got a timeout, continuing...");
787                    continue;
788                }
789                else
790                {
791                    DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited...");
792                    mach_proc->SetState(eStateExited);
793                    // Our task has died, exit the thread.
794                    break;
795                }
796                continue;
797            }
798
799#ifdef WITH_SPRINGBOARD
800            if (watchdog.get())
801            {
802                watchdog_elapsed += periodic_timeout;
803                if (watchdog_elapsed >= watchdog_timeout)
804                {
805                    DNBLogThreadedIf(LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", watchdog.get());
806                    ::SBSWatchdogAssertionRenew (watchdog.get());
807                    watchdog_elapsed = 0;
808                }
809            }
810#endif
811        }
812        else if (err.Error() != KERN_SUCCESS)
813        {
814            DNBLogThreadedIf(LOG_EXCEPTIONS, "got some other error, do something about it??? nah, continuing for now...");
815            // TODO: notify of error?
816        }
817        else
818        {
819            if (exception_message.CatchExceptionRaise(task))
820            {
821                ++num_exceptions_received;
822                mach_proc->ExceptionMessageReceived(exception_message);
823            }
824        }
825    }
826
827#ifdef WITH_SPRINGBOARD
828    if (watchdog.get())
829    {
830        // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel when we
831        // all are up and running on systems that support it. The SBS framework has a #define
832        // that will forward SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel for now
833        // so it should still build either way.
834        DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", watchdog.get());
835        ::SBSWatchdogAssertionRelease (watchdog.get());
836    }
837#endif  // #ifdef WITH_SPRINGBOARD
838
839    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", __FUNCTION__, arg);
840    return NULL;
841}
842
843
844// So the TASK_DYLD_INFO used to just return the address of the all image infos
845// as a single member called "all_image_info". Then someone decided it would be
846// a good idea to rename this first member to "all_image_info_addr" and add a
847// size member called "all_image_info_size". This of course can not be detected
848// using code or #defines. So to hack around this problem, we define our own
849// version of the TASK_DYLD_INFO structure so we can guarantee what is inside it.
850
851struct hack_task_dyld_info {
852    mach_vm_address_t   all_image_info_addr;
853    mach_vm_size_t      all_image_info_size;
854};
855
856nub_addr_t
857MachTask::GetDYLDAllImageInfosAddress (DNBError& err)
858{
859    struct hack_task_dyld_info dyld_info;
860    mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
861    // Make sure that COUNT isn't bigger than our hacked up struct hack_task_dyld_info.
862    // If it is, then make COUNT smaller to match.
863    if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)))
864        count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t));
865
866    task_t task = TaskPortForProcessID (err);
867    if (err.Success())
868    {
869        err = ::task_info (task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count);
870        if (err.Success())
871        {
872            // We now have the address of the all image infos structure
873            return dyld_info.all_image_info_addr;
874        }
875    }
876    return INVALID_NUB_ADDRESS;
877}
878
879
880//----------------------------------------------------------------------
881// MachTask::AllocateMemory
882//----------------------------------------------------------------------
883nub_addr_t
884MachTask::AllocateMemory (size_t size, uint32_t permissions)
885{
886    mach_vm_address_t addr;
887    task_t task = TaskPort();
888    if (task == TASK_NULL)
889        return INVALID_NUB_ADDRESS;
890
891    DNBError err;
892    err = ::mach_vm_allocate (task, &addr, size, TRUE);
893    if (err.Error() == KERN_SUCCESS)
894    {
895        // Set the protections:
896        vm_prot_t mach_prot = VM_PROT_NONE;
897        if (permissions & eMemoryPermissionsReadable)
898            mach_prot |= VM_PROT_READ;
899        if (permissions & eMemoryPermissionsWritable)
900            mach_prot |= VM_PROT_WRITE;
901        if (permissions & eMemoryPermissionsExecutable)
902            mach_prot |= VM_PROT_EXECUTE;
903
904
905        err = ::mach_vm_protect (task, addr, size, 0, mach_prot);
906        if (err.Error() == KERN_SUCCESS)
907        {
908            m_allocations.insert (std::make_pair(addr, size));
909            return addr;
910        }
911        ::mach_vm_deallocate (task, addr, size);
912    }
913    return INVALID_NUB_ADDRESS;
914}
915
916//----------------------------------------------------------------------
917// MachTask::DeallocateMemory
918//----------------------------------------------------------------------
919nub_bool_t
920MachTask::DeallocateMemory (nub_addr_t addr)
921{
922    task_t task = TaskPort();
923    if (task == TASK_NULL)
924        return false;
925
926    // We have to stash away sizes for the allocations...
927    allocation_collection::iterator pos, end = m_allocations.end();
928    for (pos = m_allocations.begin(); pos != end; pos++)
929    {
930        if ((*pos).first == addr)
931        {
932            m_allocations.erase(pos);
933#define ALWAYS_ZOMBIE_ALLOCATIONS 0
934            if (ALWAYS_ZOMBIE_ALLOCATIONS || getenv ("DEBUGSERVER_ZOMBIE_ALLOCATIONS"))
935            {
936                ::mach_vm_protect (task, (*pos).first, (*pos).second, 0, VM_PROT_NONE);
937                return true;
938            }
939            else
940                return ::mach_vm_deallocate (task, (*pos).first, (*pos).second) == KERN_SUCCESS;
941        }
942
943    }
944    return false;
945}
946
947static void foundStackLog(mach_stack_logging_record_t record, void *context) {
948    *((bool*)context) = true;
949}
950
951bool
952MachTask::HasMallocLoggingEnabled ()
953{
954    bool found = false;
955
956    __mach_stack_logging_enumerate_records(m_task, 0x0, foundStackLog, &found);
957    return found;
958}
959
960struct history_enumerator_impl_data
961{
962    MachMallocEvent *buffer;
963    uint32_t        *position;
964    uint32_t         count;
965};
966
967static void history_enumerator_impl(mach_stack_logging_record_t record, void* enum_obj)
968{
969    history_enumerator_impl_data *data = (history_enumerator_impl_data*)enum_obj;
970
971    if (*data->position >= data->count)
972        return;
973
974    data->buffer[*data->position].m_base_address = record.address;
975    data->buffer[*data->position].m_size = record.argument;
976    data->buffer[*data->position].m_event_id = record.stack_identifier;
977    data->buffer[*data->position].m_event_type = record.type_flags == stack_logging_type_alloc ?   eMachMallocEventTypeAlloc :
978                                                 record.type_flags == stack_logging_type_dealloc ? eMachMallocEventTypeDealloc :
979                                                                                                   eMachMallocEventTypeOther;
980    *data->position+=1;
981}
982
983bool
984MachTask::EnumerateMallocRecords (MachMallocEvent *event_buffer,
985                                  uint32_t buffer_size,
986                                  uint32_t *count)
987{
988    return EnumerateMallocRecords(0,
989                                  event_buffer,
990                                  buffer_size,
991                                  count);
992}
993
994bool
995MachTask::EnumerateMallocRecords (mach_vm_address_t address,
996                                  MachMallocEvent *event_buffer,
997                                  uint32_t buffer_size,
998                                  uint32_t *count)
999{
1000    if (!event_buffer || !count)
1001        return false;
1002
1003    if (buffer_size == 0)
1004        return false;
1005
1006    *count = 0;
1007    history_enumerator_impl_data data = { event_buffer, count, buffer_size };
1008    __mach_stack_logging_enumerate_records(m_task, address, history_enumerator_impl, &data);
1009    return (*count > 0);
1010}
1011
1012bool
1013MachTask::EnumerateMallocFrames (MachMallocEventId event_id,
1014                                 mach_vm_address_t *function_addresses_buffer,
1015                                 uint32_t buffer_size,
1016                                 uint32_t *count)
1017{
1018    if (!function_addresses_buffer || !count)
1019        return false;
1020
1021    if (buffer_size == 0)
1022        return false;
1023
1024    __mach_stack_logging_frames_for_uniqued_stack(m_task, event_id, &function_addresses_buffer[0], buffer_size, count);
1025    *count -= 1;
1026    if (function_addresses_buffer[*count-1] < vm_page_size)
1027        *count -= 1;
1028    return (*count > 0);
1029}
1030