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