MachTask.cpp revision e8a17a98577d2b144405e68a8380b3473e076d6b
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 25// C++ Includes 26// Other libraries and framework includes 27// Project includes 28#include "CFUtils.h" 29#include "DNB.h" 30#include "DNBError.h" 31#include "DNBLog.h" 32#include "MachProcess.h" 33#include "DNBDataRef.h" 34 35#if defined (__arm__) 36 37#include <CoreFoundation/CoreFoundation.h> 38#include <SpringBoardServices/SpringBoardServer.h> 39#include <SpringBoardServices/SBSWatchdogAssertion.h> 40 41#endif 42 43//---------------------------------------------------------------------- 44// MachTask constructor 45//---------------------------------------------------------------------- 46MachTask::MachTask(MachProcess *process) : 47 m_process (process), 48 m_task (TASK_NULL), 49 m_vm_memory (), 50 m_exception_thread (0), 51 m_exception_port (MACH_PORT_NULL) 52{ 53 memset(&m_exc_port_info, 0, sizeof(m_exc_port_info)); 54 55} 56 57//---------------------------------------------------------------------- 58// Destructor 59//---------------------------------------------------------------------- 60MachTask::~MachTask() 61{ 62 Clear(); 63} 64 65 66//---------------------------------------------------------------------- 67// MachTask::Suspend 68//---------------------------------------------------------------------- 69kern_return_t 70MachTask::Suspend() 71{ 72 DNBError err; 73 task_t task = TaskPort(); 74 err = ::task_suspend (task); 75 if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 76 err.LogThreaded("::task_suspend ( target_task = 0x%4.4x )", task); 77 return err.Error(); 78} 79 80 81//---------------------------------------------------------------------- 82// MachTask::Resume 83//---------------------------------------------------------------------- 84kern_return_t 85MachTask::Resume() 86{ 87 struct task_basic_info task_info; 88 task_t task = TaskPort(); 89 if (task == TASK_NULL) 90 return KERN_INVALID_ARGUMENT; 91 92 DNBError err; 93 err = BasicInfo(task, &task_info); 94 95 if (err.Success()) 96 { 97 // task_resume isn't counted like task_suspend calls are, are, so if the 98 // task is not suspended, don't try and resume it since it is already 99 // running 100 if (task_info.suspend_count > 0) 101 { 102 err = ::task_resume (task); 103 if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 104 err.LogThreaded("::task_resume ( target_task = 0x%4.4x )", task); 105 } 106 } 107 return err.Error(); 108} 109 110//---------------------------------------------------------------------- 111// MachTask::ExceptionPort 112//---------------------------------------------------------------------- 113mach_port_t 114MachTask::ExceptionPort() const 115{ 116 return m_exception_port; 117} 118 119//---------------------------------------------------------------------- 120// MachTask::ExceptionPortIsValid 121//---------------------------------------------------------------------- 122bool 123MachTask::ExceptionPortIsValid() const 124{ 125 return MACH_PORT_VALID(m_exception_port); 126} 127 128 129//---------------------------------------------------------------------- 130// MachTask::Clear 131//---------------------------------------------------------------------- 132void 133MachTask::Clear() 134{ 135 // Do any cleanup needed for this task 136 m_task = TASK_NULL; 137 m_exception_thread = 0; 138 m_exception_port = MACH_PORT_NULL; 139 140} 141 142 143//---------------------------------------------------------------------- 144// MachTask::SaveExceptionPortInfo 145//---------------------------------------------------------------------- 146kern_return_t 147MachTask::SaveExceptionPortInfo() 148{ 149 return m_exc_port_info.Save(TaskPort()); 150} 151 152//---------------------------------------------------------------------- 153// MachTask::RestoreExceptionPortInfo 154//---------------------------------------------------------------------- 155kern_return_t 156MachTask::RestoreExceptionPortInfo() 157{ 158 return m_exc_port_info.Restore(TaskPort()); 159} 160 161 162//---------------------------------------------------------------------- 163// MachTask::ReadMemory 164//---------------------------------------------------------------------- 165nub_size_t 166MachTask::ReadMemory (nub_addr_t addr, nub_size_t size, void *buf) 167{ 168 nub_size_t n = 0; 169 task_t task = TaskPort(); 170 if (task != TASK_NULL) 171 { 172 n = m_vm_memory.Read(task, addr, buf, size); 173 174 DNBLogThreadedIf(LOG_MEMORY, "MachTask::ReadMemory ( addr = 0x%8.8llx, size = %zu, buf = %8.8p) => %u bytes read", (uint64_t)addr, size, buf, n); 175 if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8)) 176 { 177 DNBDataRef data((uint8_t*)buf, n, false); 178 data.Dump(0, n, addr, DNBDataRef::TypeUInt8, 16); 179 } 180 } 181 return n; 182} 183 184 185//---------------------------------------------------------------------- 186// MachTask::WriteMemory 187//---------------------------------------------------------------------- 188nub_size_t 189MachTask::WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf) 190{ 191 nub_size_t n = 0; 192 task_t task = TaskPort(); 193 if (task != TASK_NULL) 194 { 195 n = m_vm_memory.Write(task, addr, buf, size); 196 DNBLogThreadedIf(LOG_MEMORY, "MachTask::WriteMemory ( addr = 0x%8.8llx, size = %zu, buf = %8.8p) => %u bytes written", (uint64_t)addr, size, buf, 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// MachTask::TaskPortForProcessID 208//---------------------------------------------------------------------- 209task_t 210MachTask::TaskPortForProcessID (DNBError &err) 211{ 212 if (m_task == TASK_NULL && m_process != NULL) 213 m_task = MachTask::TaskPortForProcessID(m_process->ProcessID(), err); 214 return m_task; 215} 216 217//---------------------------------------------------------------------- 218// MachTask::TaskPortForProcessID 219//---------------------------------------------------------------------- 220task_t 221MachTask::TaskPortForProcessID (pid_t pid, DNBError &err, uint32_t num_retries, uint32_t usec_interval) 222{ 223 if (pid != INVALID_NUB_PROCESS) 224 { 225 DNBError err; 226 mach_port_t task_self = mach_task_self (); 227 task_t task = TASK_NULL; 228 for (uint32_t i=0; i<num_retries; i++) 229 { 230 err = ::task_for_pid ( task_self, pid, &task); 231 232 if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 233 { 234 char str[1024]; 235 ::snprintf (str, 236 sizeof(str), 237 "::task_for_pid ( target_tport = 0x%4.4x, pid = %d, &task ) => err = 0x%8.8x (%s)", 238 task_self, 239 pid, 240 err.Error(), 241 err.AsString() ? err.AsString() : "success"); 242 if (err.Fail()) 243 err.SetErrorString(str); 244 err.LogThreaded(str); 245 } 246 247 if (err.Success()) 248 return task; 249 250 // Sleep a bit and try again 251 ::usleep (usec_interval); 252 } 253 } 254 return TASK_NULL; 255} 256 257 258//---------------------------------------------------------------------- 259// MachTask::BasicInfo 260//---------------------------------------------------------------------- 261kern_return_t 262MachTask::BasicInfo(struct task_basic_info *info) 263{ 264 return BasicInfo (TaskPort(), info); 265} 266 267//---------------------------------------------------------------------- 268// MachTask::BasicInfo 269//---------------------------------------------------------------------- 270kern_return_t 271MachTask::BasicInfo(task_t task, struct task_basic_info *info) 272{ 273 if (info == NULL) 274 return KERN_INVALID_ARGUMENT; 275 276 DNBError err; 277 mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; 278 err = ::task_info (task, TASK_BASIC_INFO, (task_info_t)info, &count); 279 const bool log_process = DNBLogCheckLogBit(LOG_TASK); 280 if (log_process || err.Fail()) 281 err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => %u )", task, info, count); 282 if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && err.Success()) 283 { 284 float user = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f; 285 float system = (float)info->user_time.seconds + (float)info->user_time.microseconds / 1000000.0f; 286 DNBLogThreaded("task_basic_info = { suspend_count = %i, virtual_size = 0x%8.8x, resident_size = 0x%8.8x, user_time = %f, system_time = %f }", 287 info->suspend_count, info->virtual_size, info->resident_size, user, system); 288 } 289 return err.Error(); 290} 291 292 293//---------------------------------------------------------------------- 294// MachTask::IsValid 295// 296// Returns true if a task is a valid task port for a current process. 297//---------------------------------------------------------------------- 298bool 299MachTask::IsValid () const 300{ 301 return MachTask::IsValid(TaskPort()); 302} 303 304//---------------------------------------------------------------------- 305// MachTask::IsValid 306// 307// Returns true if a task is a valid task port for a current process. 308//---------------------------------------------------------------------- 309bool 310MachTask::IsValid (task_t task) 311{ 312 if (task != TASK_NULL) 313 { 314 struct task_basic_info task_info; 315 return BasicInfo(task, &task_info) == KERN_SUCCESS; 316 } 317 return false; 318} 319 320 321bool 322MachTask::StartExceptionThread(DNBError &err) 323{ 324 DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__); 325 task_t task = TaskPortForProcessID(err); 326 if (MachTask::IsValid(task)) 327 { 328 // Got the mach port for the current process 329 mach_port_t task_self = mach_task_self (); 330 331 // Allocate an exception port that we will use to track our child process 332 err = ::mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &m_exception_port); 333 if (err.Fail()) 334 return false; 335 336 // Add the ability to send messages on the new exception port 337 err = ::mach_port_insert_right (task_self, m_exception_port, m_exception_port, MACH_MSG_TYPE_MAKE_SEND); 338 if (err.Fail()) 339 return false; 340 341 // Save the original state of the exception ports for our child process 342 SaveExceptionPortInfo(); 343 344 // Set the ability to get all exceptions on this port 345 err = ::task_set_exception_ports (task, EXC_MASK_ALL, m_exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE); 346 if (err.Fail()) 347 return false; 348 349 // Create the exception thread 350 err = ::pthread_create (&m_exception_thread, NULL, MachTask::ExceptionThread, this); 351 return err.Success(); 352 } 353 else 354 { 355 DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", __FUNCTION__); 356 } 357 return false; 358} 359 360kern_return_t 361MachTask::ShutDownExcecptionThread() 362{ 363 DNBError err; 364 365 err = RestoreExceptionPortInfo(); 366 367 // NULL our our exception port and let our exception thread exit 368 mach_port_t exception_port = m_exception_port; 369 m_exception_port = NULL; 370 371 err.SetError(::pthread_cancel(m_exception_thread), DNBError::POSIX); 372 if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 373 err.LogThreaded("::pthread_cancel ( thread = %p )", m_exception_thread); 374 375 err.SetError(::pthread_join(m_exception_thread, NULL), DNBError::POSIX); 376 if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 377 err.LogThreaded("::pthread_join ( thread = %p, value_ptr = NULL)", m_exception_thread); 378 379 // Deallocate our exception port that we used to track our child process 380 mach_port_t task_self = mach_task_self (); 381 err = ::mach_port_deallocate (task_self, exception_port); 382 if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 383 err.LogThreaded("::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", task_self, exception_port); 384 exception_port = NULL; 385 386 return err.Error(); 387} 388 389 390void * 391MachTask::ExceptionThread (void *arg) 392{ 393 if (arg == NULL) 394 return NULL; 395 396 MachTask *mach_task = (MachTask*) arg; 397 MachProcess *mach_proc = mach_task->Process(); 398 DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( arg = %p ) starting thread...", __FUNCTION__, arg); 399 400 // We keep a count of the number of consecutive exceptions received so 401 // we know to grab all exceptions without a timeout. We do this to get a 402 // bunch of related exceptions on our exception port so we can process 403 // then together. When we have multiple threads, we can get an exception 404 // per thread and they will come in consecutively. The main loop in this 405 // thread can stop periodically if needed to service things related to this 406 // process. 407 // flag set in the options, so we will wait forever for an exception on 408 // our exception port. After we get one exception, we then will use the 409 // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current 410 // exceptions for our process. After we have received the last pending 411 // exception, we will get a timeout which enables us to then notify 412 // our main thread that we have an exception bundle avaiable. We then wait 413 // for the main thread to tell this exception thread to start trying to get 414 // exceptions messages again and we start again with a mach_msg read with 415 // infinite timeout. 416 uint32_t num_exceptions_received = 0; 417 DNBError err; 418 task_t task = mach_task->TaskPort(); 419 mach_msg_timeout_t periodic_timeout = 0; 420 421#if defined (__arm__) 422 mach_msg_timeout_t watchdog_elapsed = 0; 423 mach_msg_timeout_t watchdog_timeout = 60 * 1000; 424 pid_t pid = mach_proc->ProcessID(); 425 CFReleaser<SBSWatchdogAssertionRef> watchdog; 426 427 if (mach_proc->ProcessUsingSpringBoard()) 428 { 429 // Request a renewal for every 60 seconds if we attached using SpringBoard 430 watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60)); 431 DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", pid, watchdog.get()); 432 433 if (watchdog.get()) 434 { 435 ::SBSWatchdogAssertionRenew (watchdog.get()); 436 437 CFTimeInterval watchdogRenewalInterval = ::SBSWatchdogAssertionGetRenewalInterval (watchdog.get()); 438 DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", watchdog.get(), watchdogRenewalInterval); 439 if (watchdogRenewalInterval > 0.0) 440 { 441 watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000; 442 if (watchdog_timeout > 3000) 443 watchdog_timeout -= 1000; // Give us a second to renew our timeout 444 else if (watchdog_timeout > 1000) 445 watchdog_timeout -= 250; // Give us a quarter of a second to renew our timeout 446 } 447 } 448 if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout) 449 periodic_timeout = watchdog_timeout; 450 } 451#endif // #if defined (__arm__) 452 453 while (mach_task->ExceptionPortIsValid()) 454 { 455 ::pthread_testcancel (); 456 457 MachException::Message exception_message; 458 459 460 if (num_exceptions_received > 0) 461 { 462 // No timeout, just receive as many exceptions as we can since we already have one and we want 463 // to get all currently available exceptions for this task 464 err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 0); 465 } 466 else if (periodic_timeout > 0) 467 { 468 // We need to stop periodically in this loop, so try and get a mach message with a valid timeout (ms) 469 err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, periodic_timeout); 470 } 471 else 472 { 473 // We don't need to parse all current exceptions or stop periodically, 474 // just wait for an exception forever. 475 err = exception_message.Receive(mach_task->ExceptionPort(), MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0); 476 } 477 478 if (err.Error() == MACH_RCV_INTERRUPTED) 479 { 480 // If we have no task port we should exit this thread 481 if (!mach_task->ExceptionPortIsValid()) 482 { 483 DNBLogThreadedIf(LOG_EXCEPTIONS, "thread cancelled..."); 484 break; 485 } 486 487 // Make sure our task is still valid 488 if (MachTask::IsValid(task)) 489 { 490 // Task is still ok 491 DNBLogThreadedIf(LOG_EXCEPTIONS, "interrupted, but task still valid, continuing..."); 492 continue; 493 } 494 else 495 { 496 DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 497 mach_proc->SetState(eStateExited); 498 // Our task has died, exit the thread. 499 break; 500 } 501 } 502 else if (err.Error() == MACH_RCV_TIMED_OUT) 503 { 504 if (num_exceptions_received > 0) 505 { 506 // We were receiving all current exceptions with a timeout of zero 507 // it is time to go back to our normal looping mode 508 num_exceptions_received = 0; 509 510 // Notify our main thread we have a complete exception message 511 // bundle available. 512 mach_proc->ExceptionMessageBundleComplete(); 513 514 // in case we use a timeout value when getting exceptions... 515 // Make sure our task is still valid 516 if (MachTask::IsValid(task)) 517 { 518 // Task is still ok 519 DNBLogThreadedIf(LOG_EXCEPTIONS, "got a timeout, continuing..."); 520 continue; 521 } 522 else 523 { 524 DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 525 mach_proc->SetState(eStateExited); 526 // Our task has died, exit the thread. 527 break; 528 } 529 continue; 530 } 531 532#if defined (__arm__) 533 if (watchdog.get()) 534 { 535 watchdog_elapsed += periodic_timeout; 536 if (watchdog_elapsed >= watchdog_timeout) 537 { 538 DNBLogThreadedIf(LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", watchdog.get()); 539 ::SBSWatchdogAssertionRenew (watchdog.get()); 540 watchdog_elapsed = 0; 541 } 542 } 543#endif 544 } 545 else if (err.Error() != KERN_SUCCESS) 546 { 547 DNBLogThreadedIf(LOG_EXCEPTIONS, "got some other error, do something about it??? nah, continuing for now..."); 548 // TODO: notify of error? 549 } 550 else 551 { 552 if (exception_message.CatchExceptionRaise()) 553 { 554 ++num_exceptions_received; 555 mach_proc->ExceptionMessageReceived(exception_message); 556 } 557 } 558 } 559 560#if defined (__arm__) 561 if (watchdog.get()) 562 { 563 // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel when we 564 // all are up and running on systems that support it. The SBS framework has a #define 565 // that will forward SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel for now 566 // so it should still build either way. 567 DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", watchdog.get()); 568 ::SBSWatchdogAssertionRelease (watchdog.get()); 569 } 570#endif // #if defined (__arm__) 571 572 DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", __FUNCTION__, arg); 573 return NULL; 574} 575 576 577// So the TASK_DYLD_INFO used to just return the address of the all image infos 578// as a single member called "all_image_info". Then someone decided it would be 579// a good idea to rename this first member to "all_image_info_addr" and add a 580// size member called "all_image_info_size". This of course can not be detected 581// using code or #defines. So to hack around this problem, we define our own 582// version of the TASK_DYLD_INFO structure so we can guarantee what is inside it. 583 584struct hack_task_dyld_info { 585 mach_vm_address_t all_image_info_addr; 586 mach_vm_size_t all_image_info_size; 587}; 588 589nub_addr_t 590MachTask::GetDYLDAllImageInfosAddress (DNBError& err) 591{ 592 struct hack_task_dyld_info dyld_info; 593 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; 594 // Make sure that COUNT isn't bigger than our hacked up struct hack_task_dyld_info. 595 // If it is, then make COUNT smaller to match. 596 if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t))) 597 count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)); 598 599 task_t task = TaskPortForProcessID (err); 600 if (err.Success()) 601 { 602 err = ::task_info (task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count); 603 if (err.Success()) 604 { 605 // We now have the address of the all image infos structure 606 return dyld_info.all_image_info_addr; 607 } 608 } 609 return INVALID_NUB_ADDRESS; 610} 611 612 613//---------------------------------------------------------------------- 614// MachTask::AllocateMemory 615//---------------------------------------------------------------------- 616nub_addr_t 617MachTask::AllocateMemory (size_t size, uint32_t permissions) 618{ 619 mach_vm_address_t addr; 620 task_t task = TaskPort(); 621 if (task == TASK_NULL) 622 return INVALID_NUB_ADDRESS; 623 624 DNBError err; 625 err = ::mach_vm_allocate (task, &addr, size, TRUE); 626 if (err.Error() == KERN_SUCCESS) 627 { 628 // Set the protections: 629 vm_prot_t mach_prot = 0; 630 if (permissions & eMemoryPermissionsReadable) 631 mach_prot |= VM_PROT_READ; 632 if (permissions & eMemoryPermissionsWritable) 633 mach_prot |= VM_PROT_WRITE; 634 if (permissions & eMemoryPermissionsExecutable) 635 mach_prot |= VM_PROT_EXECUTE; 636 637 638 err = ::mach_vm_protect (task, addr, size, 0, mach_prot); 639 if (err.Error() == KERN_SUCCESS) 640 { 641 m_allocations.insert (std::make_pair(addr, size)); 642 return addr; 643 } 644 ::mach_vm_deallocate (task, addr, size); 645 } 646 return INVALID_NUB_ADDRESS; 647} 648 649//---------------------------------------------------------------------- 650// MachTask::DeallocateMemory 651//---------------------------------------------------------------------- 652nub_bool_t 653MachTask::DeallocateMemory (nub_addr_t addr) 654{ 655 task_t task = TaskPort(); 656 if (task == TASK_NULL) 657 return false; 658 659 // We have to stash away sizes for the allocations... 660 allocation_collection::iterator pos, end = m_allocations.end(); 661 for (pos = m_allocations.begin(); pos != end; pos++) 662 { 663 if ((*pos).first == addr) 664 { 665 m_allocations.erase(pos); 666 return ::mach_vm_deallocate (task, (*pos).first, (*pos).second) == KERN_SUCCESS; 667 } 668 669 } 670 return false; 671} 672 673