MachTask.cpp revision 4eff6fca7d9209b8a2504e0c1ccae94c76ee9b06
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(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        thread_identifier_info_data_t identifier_info;
248        mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
249        kr = ::thread_info(threads[i], THREAD_IDENTIFIER_INFO, (thread_info_t)&identifier_info, &count);
250        if (kr != KERN_SUCCESS) continue;
251
252        thread_basic_info_data_t basic_info;
253        count = THREAD_BASIC_INFO_COUNT;
254        kr = ::thread_info(threads[i], THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count);
255        if (kr != KERN_SUCCESS) continue;
256
257        if ((basic_info.flags & TH_FLAGS_IDLE) == 0) {
258            nub_thread_t tid = MachThread::GetGloballyUniqueThreadIDForMachPortID (threads[i]);
259
260            threads_id.push_back(tid);
261
262            if (identifier_info.thread_handle != 0) {
263                struct proc_threadinfo proc_threadinfo;
264                int len = ::proc_pidinfo(pid, PROC_PIDTHREADINFO, identifier_info.thread_handle, &proc_threadinfo, PROC_PIDTHREADINFO_SIZE);
265                if (len && proc_threadinfo.pth_name[0]) {
266                    threads_name.push_back(proc_threadinfo.pth_name);
267                }
268                else {
269                    threads_name.push_back("");
270                }
271            }
272            else {
273                threads_name.push_back("");
274            }
275            struct timeval tv;
276            struct timeval thread_tv;
277            TIME_VALUE_TO_TIMEVAL(&basic_info.user_time, &thread_tv);
278            TIME_VALUE_TO_TIMEVAL(&basic_info.system_time, &tv);
279            timeradd(&thread_tv, &tv, &thread_tv);
280            uint64_t used_usec = thread_tv.tv_sec * 1000000ULL + thread_tv.tv_usec;
281            threads_used_usec.push_back(used_usec);
282        }
283
284        kr = mach_port_deallocate(mach_task_self(), threads[i]);
285    }
286    kr = mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)(uintptr_t)threads, tcnt * sizeof(*threads));
287}
288
289#define RAW_HEXBASE     std::setfill('0') << std::hex << std::right
290#define DECIMAL         std::dec << std::setfill(' ')
291std::string
292MachTask::GetProfileData ()
293{
294    std::string result;
295
296    static int32_t numCPU = -1;
297    int32_t mib[] = {CTL_HW, HW_AVAILCPU};
298    size_t len = sizeof(numCPU);
299    if (sysctl(mib, sizeof(mib) / sizeof(int32_t), &numCPU, &len, NULL, 0) != 0)
300        return result;
301
302    mach_port_t localHost = mach_host_self();
303    struct host_cpu_load_info host_info;
304	mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
305	kern_return_t kr = host_statistics(localHost, HOST_CPU_LOAD_INFO, (host_info_t)&host_info, &count);
306    if (kr != KERN_SUCCESS)
307        return result;
308
309    task_t task = TaskPort();
310    if (task == TASK_NULL)
311        return result;
312
313    struct task_basic_info task_info;
314    DNBError err;
315    err = BasicInfo(task, &task_info);
316
317    if (!err.Success())
318        return result;
319
320    uint64_t elapsed_usec = 0;
321    uint64_t task_used_usec = 0;
322    std::vector<uint64_t> threads_id;
323    std::vector<std::string> threads_name;
324    std::vector<uint64_t> threads_used_usec;
325
326    // Get current used time.
327    struct timeval current_used_time;
328    struct timeval tv;
329    TIME_VALUE_TO_TIMEVAL(&task_info.user_time, &current_used_time);
330    TIME_VALUE_TO_TIMEVAL(&task_info.system_time, &tv);
331    timeradd(&current_used_time, &tv, &current_used_time);
332    task_used_usec = current_used_time.tv_sec * 1000000ULL + current_used_time.tv_usec;
333    get_threads_profile_data(task, m_process->ProcessID(), threads_id, threads_name, threads_used_usec);
334
335    struct timeval current_elapsed_time;
336    int res = gettimeofday(&current_elapsed_time, NULL);
337    if (res == 0)
338    {
339        elapsed_usec = current_elapsed_time.tv_sec * 1000000ULL + current_elapsed_time.tv_usec;
340    }
341
342    struct vm_statistics vm_stats;
343    uint64_t physical_memory;
344    mach_vm_size_t rprvt = 0;
345    mach_vm_size_t rsize = 0;
346    mach_vm_size_t vprvt = 0;
347    mach_vm_size_t vsize = 0;
348    mach_vm_size_t dirty_size = 0;
349    if (m_vm_memory.GetMemoryProfile(task, task_info, m_process->GetCPUType(), m_process->ProcessID(), vm_stats, physical_memory, rprvt, rsize, vprvt, vsize, dirty_size))
350    {
351        std::ostringstream profile_data_stream;
352
353        profile_data_stream << "num_cpu:" << numCPU << ';';
354        profile_data_stream << "host_user_ticks:" << host_info.cpu_ticks[CPU_STATE_USER] << ';';
355        profile_data_stream << "host_sys_ticks:" << host_info.cpu_ticks[CPU_STATE_SYSTEM] << ';';
356        profile_data_stream << "host_idle_ticks:" << host_info.cpu_ticks[CPU_STATE_IDLE] << ';';
357
358        profile_data_stream << "elapsed_usec:" << elapsed_usec << ';';
359        profile_data_stream << "task_used_usec:" << task_used_usec << ';';
360
361        int num_threads = threads_id.size();
362        for (int i=0; i<num_threads; i++) {
363            profile_data_stream << "thread_used_id:" << std::hex << threads_id[i] << std::dec << ';';
364            profile_data_stream << "thread_used_usec:" << threads_used_usec[i] << ';';
365
366            profile_data_stream << "thread_used_name:";
367            int len = threads_name[i].size();
368            if (len) {
369                const char *thread_name = threads_name[i].c_str();
370                // Make sure that thread name doesn't interfere with our delimiter.
371                profile_data_stream << RAW_HEXBASE << std::setw(2);
372                const uint8_t *ubuf8 = (const uint8_t *)(thread_name);
373                for (int j=0; j<len; j++)
374                {
375                    profile_data_stream << (uint32_t)(ubuf8[j]);
376                }
377                // Reset back to DECIMAL.
378                profile_data_stream << DECIMAL;
379            }
380            profile_data_stream << ';';
381        }
382
383        profile_data_stream << "wired:" << vm_stats.wire_count * vm_page_size << ';';
384        profile_data_stream << "active:" << vm_stats.active_count * vm_page_size << ';';
385        profile_data_stream << "inactive:" << vm_stats.inactive_count * vm_page_size << ';';
386        uint64_t total_used_count = vm_stats.wire_count + vm_stats.inactive_count + vm_stats.active_count;
387        profile_data_stream << "used:" << total_used_count * vm_page_size << ';';
388        profile_data_stream << "free:" << vm_stats.free_count * vm_page_size << ';';
389        profile_data_stream << "total:" << physical_memory << ';';
390
391        profile_data_stream << "rprvt:" << rprvt << ';';
392        profile_data_stream << "rsize:" << rsize << ';';
393        profile_data_stream << "vprvt:" << vprvt << ';';
394        profile_data_stream << "vsize:" << vsize << ';';
395        profile_data_stream << "dirty:" << dirty_size << ';';
396        profile_data_stream << "--end--;";
397
398        result = profile_data_stream.str();
399    }
400
401    return result;
402}
403
404
405//----------------------------------------------------------------------
406// MachTask::TaskPortForProcessID
407//----------------------------------------------------------------------
408task_t
409MachTask::TaskPortForProcessID (DNBError &err)
410{
411    if (m_task == TASK_NULL && m_process != NULL)
412        m_task = MachTask::TaskPortForProcessID(m_process->ProcessID(), err);
413    return m_task;
414}
415
416//----------------------------------------------------------------------
417// MachTask::TaskPortForProcessID
418//----------------------------------------------------------------------
419task_t
420MachTask::TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries, uint32_t usec_interval)
421{
422    if (pid != INVALID_NUB_PROCESS)
423    {
424        DNBError err;
425        mach_port_t task_self = mach_task_self ();
426        task_t task = TASK_NULL;
427        for (uint32_t i=0; i<num_retries; i++)
428        {
429            err = ::task_for_pid ( task_self, pid, &task);
430
431            if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
432            {
433                char str[1024];
434                ::snprintf (str,
435                            sizeof(str),
436                            "::task_for_pid ( target_tport = 0x%4.4x, pid = %d, &task ) => err = 0x%8.8x (%s)",
437                            task_self,
438                            pid,
439                            err.Error(),
440                            err.AsString() ? err.AsString() : "success");
441                if (err.Fail())
442                    err.SetErrorString(str);
443                err.LogThreaded(str);
444            }
445
446            if (err.Success())
447                return task;
448
449            // Sleep a bit and try again
450            ::usleep (usec_interval);
451        }
452    }
453    return TASK_NULL;
454}
455
456
457//----------------------------------------------------------------------
458// MachTask::BasicInfo
459//----------------------------------------------------------------------
460kern_return_t
461MachTask::BasicInfo(struct task_basic_info *info)
462{
463    return BasicInfo (TaskPort(), info);
464}
465
466//----------------------------------------------------------------------
467// MachTask::BasicInfo
468//----------------------------------------------------------------------
469kern_return_t
470MachTask::BasicInfo(task_t task, struct task_basic_info *info)
471{
472    if (info == NULL)
473        return KERN_INVALID_ARGUMENT;
474
475    DNBError err;
476    mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
477    err = ::task_info (task, TASK_BASIC_INFO, (task_info_t)info, &count);
478    const bool log_process = DNBLogCheckLogBit(LOG_TASK);
479    if (log_process || err.Fail())
480        err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => %u )", task, info, count);
481    if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && err.Success())
482    {
483        float user = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f;
484        float system = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f;
485        DNBLogThreaded ("task_basic_info = { suspend_count = %i, virtual_size = 0x%8.8llx, resident_size = 0x%8.8llx, user_time = %f, system_time = %f }",
486                        info->suspend_count,
487                        (uint64_t)info->virtual_size,
488                        (uint64_t)info->resident_size,
489                        user,
490                        system);
491    }
492    return err.Error();
493}
494
495
496//----------------------------------------------------------------------
497// MachTask::IsValid
498//
499// Returns true if a task is a valid task port for a current process.
500//----------------------------------------------------------------------
501bool
502MachTask::IsValid () const
503{
504    return MachTask::IsValid(TaskPort());
505}
506
507//----------------------------------------------------------------------
508// MachTask::IsValid
509//
510// Returns true if a task is a valid task port for a current process.
511//----------------------------------------------------------------------
512bool
513MachTask::IsValid (task_t task)
514{
515    if (task != TASK_NULL)
516    {
517        struct task_basic_info task_info;
518        return BasicInfo(task, &task_info) == KERN_SUCCESS;
519    }
520    return false;
521}
522
523
524bool
525MachTask::StartExceptionThread(DNBError &err)
526{
527    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__);
528    task_t task = TaskPortForProcessID(err);
529    if (MachTask::IsValid(task))
530    {
531        // Got the mach port for the current process
532        mach_port_t task_self = mach_task_self ();
533
534        // Allocate an exception port that we will use to track our child process
535        err = ::mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &m_exception_port);
536        if (err.Fail())
537            return false;
538
539        // Add the ability to send messages on the new exception port
540        err = ::mach_port_insert_right (task_self, m_exception_port, m_exception_port, MACH_MSG_TYPE_MAKE_SEND);
541        if (err.Fail())
542            return false;
543
544        // Save the original state of the exception ports for our child process
545        SaveExceptionPortInfo();
546
547        // We weren't able to save the info for our exception ports, we must stop...
548        if (m_exc_port_info.mask == 0)
549        {
550            err.SetErrorString("failed to get exception port info");
551            return false;
552        }
553
554        // Set the ability to get all exceptions on this port
555        err = ::task_set_exception_ports (task, m_exc_port_info.mask, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE);
556        if (DNBLogCheckLogBit(LOG_EXCEPTIONS) || err.Fail())
557        {
558            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 )",
559                            task,
560                            m_exc_port_info.mask,
561                            m_exception_port,
562                            (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES),
563                            THREAD_STATE_NONE);
564        }
565
566        if (err.Fail())
567            return false;
568
569        // Create the exception thread
570        err = ::pthread_create (&m_exception_thread, NULL, MachTask::ExceptionThread, this);
571        return err.Success();
572    }
573    else
574    {
575        DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", __FUNCTION__);
576    }
577    return false;
578}
579
580kern_return_t
581MachTask::ShutDownExcecptionThread()
582{
583    DNBError err;
584
585    err = RestoreExceptionPortInfo();
586
587    // NULL our our exception port and let our exception thread exit
588    mach_port_t exception_port = m_exception_port;
589    m_exception_port = NULL;
590
591    err.SetError(::pthread_cancel(m_exception_thread), DNBError::POSIX);
592    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
593        err.LogThreaded("::pthread_cancel ( thread = %p )", m_exception_thread);
594
595    err.SetError(::pthread_join(m_exception_thread, NULL), DNBError::POSIX);
596    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
597        err.LogThreaded("::pthread_join ( thread = %p, value_ptr = NULL)", m_exception_thread);
598
599    // Deallocate our exception port that we used to track our child process
600    mach_port_t task_self = mach_task_self ();
601    err = ::mach_port_deallocate (task_self, exception_port);
602    if (DNBLogCheckLogBit(LOG_TASK) || err.Fail())
603        err.LogThreaded("::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", task_self, exception_port);
604
605    return err.Error();
606}
607
608
609void *
610MachTask::ExceptionThread (void *arg)
611{
612    if (arg == NULL)
613        return NULL;
614
615    MachTask *mach_task = (MachTask*) arg;
616    MachProcess *mach_proc = mach_task->Process();
617    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( arg = %p ) starting thread...", __FUNCTION__, arg);
618
619    // We keep a count of the number of consecutive exceptions received so
620    // we know to grab all exceptions without a timeout. We do this to get a
621    // bunch of related exceptions on our exception port so we can process
622    // then together. When we have multiple threads, we can get an exception
623    // per thread and they will come in consecutively. The main loop in this
624    // thread can stop periodically if needed to service things related to this
625    // process.
626    // flag set in the options, so we will wait forever for an exception on
627    // our exception port. After we get one exception, we then will use the
628    // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current
629    // exceptions for our process. After we have received the last pending
630    // exception, we will get a timeout which enables us to then notify
631    // our main thread that we have an exception bundle avaiable. We then wait
632    // for the main thread to tell this exception thread to start trying to get
633    // exceptions messages again and we start again with a mach_msg read with
634    // infinite timeout.
635    uint32_t num_exceptions_received = 0;
636    DNBError err;
637    task_t task = mach_task->TaskPort();
638    mach_msg_timeout_t periodic_timeout = 0;
639
640#ifdef WITH_SPRINGBOARD
641    mach_msg_timeout_t watchdog_elapsed = 0;
642    mach_msg_timeout_t watchdog_timeout = 60 * 1000;
643    pid_t pid = mach_proc->ProcessID();
644    CFReleaser<SBSWatchdogAssertionRef> watchdog;
645
646    if (mach_proc->ProcessUsingSpringBoard())
647    {
648        // Request a renewal for every 60 seconds if we attached using SpringBoard
649        watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60));
650        DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", pid, watchdog.get());
651
652        if (watchdog.get())
653        {
654            ::SBSWatchdogAssertionRenew (watchdog.get());
655
656            CFTimeInterval watchdogRenewalInterval = ::SBSWatchdogAssertionGetRenewalInterval (watchdog.get());
657            DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", watchdog.get(), watchdogRenewalInterval);
658            if (watchdogRenewalInterval > 0.0)
659            {
660                watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000;
661                if (watchdog_timeout > 3000)
662                    watchdog_timeout -= 1000;   // Give us a second to renew our timeout
663                else if (watchdog_timeout > 1000)
664                    watchdog_timeout -= 250;    // Give us a quarter of a second to renew our timeout
665            }
666        }
667        if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout)
668            periodic_timeout = watchdog_timeout;
669    }
670#endif  // #ifdef WITH_SPRINGBOARD
671
672    while (mach_task->ExceptionPortIsValid())
673    {
674        ::pthread_testcancel ();
675
676        MachException::Message exception_message;
677
678
679        if (num_exceptions_received > 0)
680        {
681            // No timeout, just receive as many exceptions as we can since we already have one and we want
682            // to get all currently available exceptions for this task
683            err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 0);
684        }
685        else if (periodic_timeout > 0)
686        {
687            // We need to stop periodically in this loop, so try and get a mach message with a valid timeout (ms)
688            err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, periodic_timeout);
689        }
690        else
691        {
692            // We don't need to parse all current exceptions or stop periodically,
693            // just wait for an exception forever.
694            err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0);
695        }
696
697        if (err.Error() == MACH_RCV_INTERRUPTED)
698        {
699            // If we have no task port we should exit this thread
700            if (!mach_task->ExceptionPortIsValid())
701            {
702                DNBLogThreadedIf(LOG_EXCEPTIONS, "thread cancelled...");
703                break;
704            }
705
706            // Make sure our task is still valid
707            if (MachTask::IsValid(task))
708            {
709                // Task is still ok
710                DNBLogThreadedIf(LOG_EXCEPTIONS, "interrupted, but task still valid, continuing...");
711                continue;
712            }
713            else
714            {
715                DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited...");
716                mach_proc->SetState(eStateExited);
717                // Our task has died, exit the thread.
718                break;
719            }
720        }
721        else if (err.Error() == MACH_RCV_TIMED_OUT)
722        {
723            if (num_exceptions_received > 0)
724            {
725                // We were receiving all current exceptions with a timeout of zero
726                // it is time to go back to our normal looping mode
727                num_exceptions_received = 0;
728
729                // Notify our main thread we have a complete exception message
730                // bundle available.
731                mach_proc->ExceptionMessageBundleComplete();
732
733                // in case we use a timeout value when getting exceptions...
734                // Make sure our task is still valid
735                if (MachTask::IsValid(task))
736                {
737                    // Task is still ok
738                    DNBLogThreadedIf(LOG_EXCEPTIONS, "got a timeout, continuing...");
739                    continue;
740                }
741                else
742                {
743                    DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited...");
744                    mach_proc->SetState(eStateExited);
745                    // Our task has died, exit the thread.
746                    break;
747                }
748                continue;
749            }
750
751#ifdef WITH_SPRINGBOARD
752            if (watchdog.get())
753            {
754                watchdog_elapsed += periodic_timeout;
755                if (watchdog_elapsed >= watchdog_timeout)
756                {
757                    DNBLogThreadedIf(LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", watchdog.get());
758                    ::SBSWatchdogAssertionRenew (watchdog.get());
759                    watchdog_elapsed = 0;
760                }
761            }
762#endif
763        }
764        else if (err.Error() != KERN_SUCCESS)
765        {
766            DNBLogThreadedIf(LOG_EXCEPTIONS, "got some other error, do something about it??? nah, continuing for now...");
767            // TODO: notify of error?
768        }
769        else
770        {
771            if (exception_message.CatchExceptionRaise(task))
772            {
773                ++num_exceptions_received;
774                mach_proc->ExceptionMessageReceived(exception_message);
775            }
776        }
777    }
778
779#ifdef WITH_SPRINGBOARD
780    if (watchdog.get())
781    {
782        // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel when we
783        // all are up and running on systems that support it. The SBS framework has a #define
784        // that will forward SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel for now
785        // so it should still build either way.
786        DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", watchdog.get());
787        ::SBSWatchdogAssertionRelease (watchdog.get());
788    }
789#endif  // #ifdef WITH_SPRINGBOARD
790
791    DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", __FUNCTION__, arg);
792    return NULL;
793}
794
795
796// So the TASK_DYLD_INFO used to just return the address of the all image infos
797// as a single member called "all_image_info". Then someone decided it would be
798// a good idea to rename this first member to "all_image_info_addr" and add a
799// size member called "all_image_info_size". This of course can not be detected
800// using code or #defines. So to hack around this problem, we define our own
801// version of the TASK_DYLD_INFO structure so we can guarantee what is inside it.
802
803struct hack_task_dyld_info {
804    mach_vm_address_t   all_image_info_addr;
805    mach_vm_size_t      all_image_info_size;
806};
807
808nub_addr_t
809MachTask::GetDYLDAllImageInfosAddress (DNBError& err)
810{
811    struct hack_task_dyld_info dyld_info;
812    mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
813    // Make sure that COUNT isn't bigger than our hacked up struct hack_task_dyld_info.
814    // If it is, then make COUNT smaller to match.
815    if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)))
816        count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t));
817
818    task_t task = TaskPortForProcessID (err);
819    if (err.Success())
820    {
821        err = ::task_info (task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count);
822        if (err.Success())
823        {
824            // We now have the address of the all image infos structure
825            return dyld_info.all_image_info_addr;
826        }
827    }
828    return INVALID_NUB_ADDRESS;
829}
830
831
832//----------------------------------------------------------------------
833// MachTask::AllocateMemory
834//----------------------------------------------------------------------
835nub_addr_t
836MachTask::AllocateMemory (size_t size, uint32_t permissions)
837{
838    mach_vm_address_t addr;
839    task_t task = TaskPort();
840    if (task == TASK_NULL)
841        return INVALID_NUB_ADDRESS;
842
843    DNBError err;
844    err = ::mach_vm_allocate (task, &addr, size, TRUE);
845    if (err.Error() == KERN_SUCCESS)
846    {
847        // Set the protections:
848        vm_prot_t mach_prot = VM_PROT_NONE;
849        if (permissions & eMemoryPermissionsReadable)
850            mach_prot |= VM_PROT_READ;
851        if (permissions & eMemoryPermissionsWritable)
852            mach_prot |= VM_PROT_WRITE;
853        if (permissions & eMemoryPermissionsExecutable)
854            mach_prot |= VM_PROT_EXECUTE;
855
856
857        err = ::mach_vm_protect (task, addr, size, 0, mach_prot);
858        if (err.Error() == KERN_SUCCESS)
859        {
860            m_allocations.insert (std::make_pair(addr, size));
861            return addr;
862        }
863        ::mach_vm_deallocate (task, addr, size);
864    }
865    return INVALID_NUB_ADDRESS;
866}
867
868//----------------------------------------------------------------------
869// MachTask::DeallocateMemory
870//----------------------------------------------------------------------
871nub_bool_t
872MachTask::DeallocateMemory (nub_addr_t addr)
873{
874    task_t task = TaskPort();
875    if (task == TASK_NULL)
876        return false;
877
878    // We have to stash away sizes for the allocations...
879    allocation_collection::iterator pos, end = m_allocations.end();
880    for (pos = m_allocations.begin(); pos != end; pos++)
881    {
882        if ((*pos).first == addr)
883        {
884            m_allocations.erase(pos);
885#define ALWAYS_ZOMBIE_ALLOCATIONS 0
886            if (ALWAYS_ZOMBIE_ALLOCATIONS || getenv ("DEBUGSERVER_ZOMBIE_ALLOCATIONS"))
887            {
888                ::mach_vm_protect (task, (*pos).first, (*pos).second, 0, VM_PROT_NONE);
889                return true;
890            }
891            else
892                return ::mach_vm_deallocate (task, (*pos).first, (*pos).second) == KERN_SUCCESS;
893        }
894
895    }
896    return false;
897}
898
899static void foundStackLog(mach_stack_logging_record_t record, void *context) {
900    *((bool*)context) = true;
901}
902
903bool
904MachTask::HasMallocLoggingEnabled ()
905{
906    bool found = false;
907
908    __mach_stack_logging_enumerate_records(m_task, 0x0, foundStackLog, &found);
909    return found;
910}
911
912struct history_enumerator_impl_data
913{
914    MachMallocEvent *buffer;
915    uint32_t        *position;
916    uint32_t         count;
917};
918
919static void history_enumerator_impl(mach_stack_logging_record_t record, void* enum_obj)
920{
921    history_enumerator_impl_data *data = (history_enumerator_impl_data*)enum_obj;
922
923    if (*data->position >= data->count)
924        return;
925
926    data->buffer[*data->position].m_base_address = record.address;
927    data->buffer[*data->position].m_size = record.argument;
928    data->buffer[*data->position].m_event_id = record.stack_identifier;
929    data->buffer[*data->position].m_event_type = record.type_flags == stack_logging_type_alloc ?   eMachMallocEventTypeAlloc :
930                                                 record.type_flags == stack_logging_type_dealloc ? eMachMallocEventTypeDealloc :
931                                                                                                   eMachMallocEventTypeOther;
932    *data->position+=1;
933}
934
935bool
936MachTask::EnumerateMallocRecords (MachMallocEvent *event_buffer,
937                                  uint32_t buffer_size,
938                                  uint32_t *count)
939{
940    return EnumerateMallocRecords(0,
941                                  event_buffer,
942                                  buffer_size,
943                                  count);
944}
945
946bool
947MachTask::EnumerateMallocRecords (mach_vm_address_t address,
948                                  MachMallocEvent *event_buffer,
949                                  uint32_t buffer_size,
950                                  uint32_t *count)
951{
952    if (!event_buffer || !count)
953        return false;
954
955    if (buffer_size == 0)
956        return false;
957
958    *count = 0;
959    history_enumerator_impl_data data = { event_buffer, count, buffer_size };
960    __mach_stack_logging_enumerate_records(m_task, address, history_enumerator_impl, &data);
961    return (*count > 0);
962}
963
964bool
965MachTask::EnumerateMallocFrames (MachMallocEventId event_id,
966                                 mach_vm_address_t *function_addresses_buffer,
967                                 uint32_t buffer_size,
968                                 uint32_t *count)
969{
970    if (!function_addresses_buffer || !count)
971        return false;
972
973    if (buffer_size == 0)
974        return false;
975
976    __mach_stack_logging_frames_for_uniqued_stack(m_task, event_id, &function_addresses_buffer[0], buffer_size, count);
977    *count -= 1;
978    if (function_addresses_buffer[*count-1] < vm_page_size)
979        *count -= 1;
980    return (*count > 0);
981}
982