1 //===-- SBProcess.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 #include "lldb/lldb-python.h" 11 12 #include "lldb/API/SBProcess.h" 13 14 // C Includes 15 #include <inttypes.h> 16 17 #include "lldb/lldb-defines.h" 18 #include "lldb/lldb-types.h" 19 20 #include "lldb/Interpreter/Args.h" 21 #include "lldb/Core/Debugger.h" 22 #include "lldb/Core/Log.h" 23 #include "lldb/Core/Module.h" 24 #include "lldb/Core/State.h" 25 #include "lldb/Core/Stream.h" 26 #include "lldb/Core/StreamFile.h" 27 #include "lldb/Target/Process.h" 28 #include "lldb/Target/RegisterContext.h" 29 #include "lldb/Target/SystemRuntime.h" 30 #include "lldb/Target/Target.h" 31 #include "lldb/Target/Thread.h" 32 33 // Project includes 34 35 #include "lldb/API/SBBroadcaster.h" 36 #include "lldb/API/SBCommandReturnObject.h" 37 #include "lldb/API/SBDebugger.h" 38 #include "lldb/API/SBEvent.h" 39 #include "lldb/API/SBFileSpec.h" 40 #include "lldb/API/SBThread.h" 41 #include "lldb/API/SBStream.h" 42 #include "lldb/API/SBStringList.h" 43 44 using namespace lldb; 45 using namespace lldb_private; 46 47 48 SBProcess::SBProcess () : 49 m_opaque_wp() 50 { 51 } 52 53 54 //---------------------------------------------------------------------- 55 // SBProcess constructor 56 //---------------------------------------------------------------------- 57 58 SBProcess::SBProcess (const SBProcess& rhs) : 59 m_opaque_wp (rhs.m_opaque_wp) 60 { 61 } 62 63 64 SBProcess::SBProcess (const lldb::ProcessSP &process_sp) : 65 m_opaque_wp (process_sp) 66 { 67 } 68 69 const SBProcess& 70 SBProcess::operator = (const SBProcess& rhs) 71 { 72 if (this != &rhs) 73 m_opaque_wp = rhs.m_opaque_wp; 74 return *this; 75 } 76 77 //---------------------------------------------------------------------- 78 // Destructor 79 //---------------------------------------------------------------------- 80 SBProcess::~SBProcess() 81 { 82 } 83 84 const char * 85 SBProcess::GetBroadcasterClassName () 86 { 87 return Process::GetStaticBroadcasterClass().AsCString(); 88 } 89 90 const char * 91 SBProcess::GetPluginName () 92 { 93 ProcessSP process_sp(GetSP()); 94 if (process_sp) 95 { 96 return process_sp->GetPluginName().GetCString(); 97 } 98 return "<Unknown>"; 99 } 100 101 const char * 102 SBProcess::GetShortPluginName () 103 { 104 ProcessSP process_sp(GetSP()); 105 if (process_sp) 106 { 107 return process_sp->GetPluginName().GetCString(); 108 } 109 return "<Unknown>"; 110 } 111 112 113 lldb::ProcessSP 114 SBProcess::GetSP() const 115 { 116 return m_opaque_wp.lock(); 117 } 118 119 void 120 SBProcess::SetSP (const ProcessSP &process_sp) 121 { 122 m_opaque_wp = process_sp; 123 } 124 125 void 126 SBProcess::Clear () 127 { 128 m_opaque_wp.reset(); 129 } 130 131 132 bool 133 SBProcess::IsValid() const 134 { 135 ProcessSP process_sp(m_opaque_wp.lock()); 136 return ((bool) process_sp && process_sp->IsValid()); 137 } 138 139 bool 140 SBProcess::RemoteLaunch (char const **argv, 141 char const **envp, 142 const char *stdin_path, 143 const char *stdout_path, 144 const char *stderr_path, 145 const char *working_directory, 146 uint32_t launch_flags, 147 bool stop_at_entry, 148 lldb::SBError& error) 149 { 150 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 151 if (log) { 152 log->Printf ("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...", 153 m_opaque_wp.lock().get(), 154 argv, 155 envp, 156 stdin_path ? stdin_path : "NULL", 157 stdout_path ? stdout_path : "NULL", 158 stderr_path ? stderr_path : "NULL", 159 working_directory ? working_directory : "NULL", 160 launch_flags, 161 stop_at_entry, 162 error.get()); 163 } 164 165 ProcessSP process_sp(GetSP()); 166 if (process_sp) 167 { 168 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 169 if (process_sp->GetState() == eStateConnected) 170 { 171 if (stop_at_entry) 172 launch_flags |= eLaunchFlagStopAtEntry; 173 ProcessLaunchInfo launch_info (stdin_path, 174 stdout_path, 175 stderr_path, 176 working_directory, 177 launch_flags); 178 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer(); 179 if (exe_module) 180 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true); 181 if (argv) 182 launch_info.GetArguments().AppendArguments (argv); 183 if (envp) 184 launch_info.GetEnvironmentEntries ().SetArguments (envp); 185 error.SetError (process_sp->Launch (launch_info)); 186 } 187 else 188 { 189 error.SetErrorString ("must be in eStateConnected to call RemoteLaunch"); 190 } 191 } 192 else 193 { 194 error.SetErrorString ("unable to attach pid"); 195 } 196 197 if (log) { 198 SBStream sstr; 199 error.GetDescription (sstr); 200 log->Printf ("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s", process_sp.get(), error.get(), sstr.GetData()); 201 } 202 203 return error.Success(); 204 } 205 206 bool 207 SBProcess::RemoteAttachToProcessWithID (lldb::pid_t pid, lldb::SBError& error) 208 { 209 ProcessSP process_sp(GetSP()); 210 if (process_sp) 211 { 212 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 213 if (process_sp->GetState() == eStateConnected) 214 { 215 ProcessAttachInfo attach_info; 216 attach_info.SetProcessID (pid); 217 error.SetError (process_sp->Attach (attach_info)); 218 } 219 else 220 { 221 error.SetErrorString ("must be in eStateConnected to call RemoteAttachToProcessWithID"); 222 } 223 } 224 else 225 { 226 error.SetErrorString ("unable to attach pid"); 227 } 228 229 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 230 if (log) { 231 SBStream sstr; 232 error.GetDescription (sstr); 233 log->Printf ("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64 ") => SBError (%p): %s", process_sp.get(), pid, error.get(), sstr.GetData()); 234 } 235 236 return error.Success(); 237 } 238 239 240 uint32_t 241 SBProcess::GetNumThreads () 242 { 243 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 244 245 uint32_t num_threads = 0; 246 ProcessSP process_sp(GetSP()); 247 if (process_sp) 248 { 249 Process::StopLocker stop_locker; 250 251 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 252 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 253 num_threads = process_sp->GetThreadList().GetSize(can_update); 254 } 255 256 if (log) 257 log->Printf ("SBProcess(%p)::GetNumThreads () => %d", process_sp.get(), num_threads); 258 259 return num_threads; 260 } 261 262 SBThread 263 SBProcess::GetSelectedThread () const 264 { 265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 266 267 SBThread sb_thread; 268 ThreadSP thread_sp; 269 ProcessSP process_sp(GetSP()); 270 if (process_sp) 271 { 272 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 273 thread_sp = process_sp->GetThreadList().GetSelectedThread(); 274 sb_thread.SetThread (thread_sp); 275 } 276 277 if (log) 278 { 279 log->Printf ("SBProcess(%p)::GetSelectedThread () => SBThread(%p)", process_sp.get(), thread_sp.get()); 280 } 281 282 return sb_thread; 283 } 284 285 SBThread 286 SBProcess::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context) 287 { 288 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 289 290 SBThread sb_thread; 291 ThreadSP thread_sp; 292 ProcessSP process_sp(GetSP()); 293 if (process_sp) 294 { 295 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 296 thread_sp = process_sp->CreateOSPluginThread(tid, context); 297 sb_thread.SetThread (thread_sp); 298 } 299 300 if (log) 301 log->Printf ("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64 ", context=0x%" PRIx64 ") => SBThread(%p)", process_sp.get(), tid, context, thread_sp.get()); 302 303 return sb_thread; 304 } 305 306 SBTarget 307 SBProcess::GetTarget() const 308 { 309 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 310 311 SBTarget sb_target; 312 TargetSP target_sp; 313 ProcessSP process_sp(GetSP()); 314 if (process_sp) 315 { 316 target_sp = process_sp->GetTarget().shared_from_this(); 317 sb_target.SetSP (target_sp); 318 } 319 320 if (log) 321 log->Printf ("SBProcess(%p)::GetTarget () => SBTarget(%p)", process_sp.get(), target_sp.get()); 322 323 return sb_target; 324 } 325 326 327 size_t 328 SBProcess::PutSTDIN (const char *src, size_t src_len) 329 { 330 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 331 332 size_t ret_val = 0; 333 ProcessSP process_sp(GetSP()); 334 if (process_sp) 335 { 336 Error error; 337 ret_val = process_sp->PutSTDIN (src, src_len, error); 338 } 339 340 if (log) 341 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64 ") => %" PRIu64, 342 process_sp.get(), 343 src, 344 (uint64_t) src_len, 345 (uint64_t) ret_val); 346 347 return ret_val; 348 } 349 350 size_t 351 SBProcess::GetSTDOUT (char *dst, size_t dst_len) const 352 { 353 size_t bytes_read = 0; 354 ProcessSP process_sp(GetSP()); 355 if (process_sp) 356 { 357 Error error; 358 bytes_read = process_sp->GetSTDOUT (dst, dst_len, error); 359 } 360 361 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 362 if (log) 363 log->Printf ("SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64, 364 process_sp.get(), 365 (int) bytes_read, 366 dst, 367 (uint64_t)dst_len, 368 (uint64_t)bytes_read); 369 370 return bytes_read; 371 } 372 373 size_t 374 SBProcess::GetSTDERR (char *dst, size_t dst_len) const 375 { 376 size_t bytes_read = 0; 377 ProcessSP process_sp(GetSP()); 378 if (process_sp) 379 { 380 Error error; 381 bytes_read = process_sp->GetSTDERR (dst, dst_len, error); 382 } 383 384 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 385 if (log) 386 log->Printf ("SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64, 387 process_sp.get(), 388 (int) bytes_read, 389 dst, 390 (uint64_t)dst_len, 391 (uint64_t)bytes_read); 392 393 return bytes_read; 394 } 395 396 size_t 397 SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const 398 { 399 size_t bytes_read = 0; 400 ProcessSP process_sp(GetSP()); 401 if (process_sp) 402 { 403 Error error; 404 bytes_read = process_sp->GetAsyncProfileData (dst, dst_len, error); 405 } 406 407 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 408 if (log) 409 log->Printf ("SBProcess(%p)::GetProfileData (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64, 410 process_sp.get(), 411 (int) bytes_read, 412 dst, 413 (uint64_t)dst_len, 414 (uint64_t)bytes_read); 415 416 return bytes_read; 417 } 418 419 void 420 SBProcess::ReportEventState (const SBEvent &event, FILE *out) const 421 { 422 if (out == NULL) 423 return; 424 425 ProcessSP process_sp(GetSP()); 426 if (process_sp) 427 { 428 const StateType event_state = SBProcess::GetStateFromEvent (event); 429 char message[1024]; 430 int message_len = ::snprintf (message, 431 sizeof (message), 432 "Process %" PRIu64 " %s\n", 433 process_sp->GetID(), 434 SBDebugger::StateAsCString (event_state)); 435 436 if (message_len > 0) 437 ::fwrite (message, 1, message_len, out); 438 } 439 } 440 441 void 442 SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result) 443 { 444 ProcessSP process_sp(GetSP()); 445 if (process_sp) 446 { 447 const StateType event_state = SBProcess::GetStateFromEvent (event); 448 char message[1024]; 449 ::snprintf (message, 450 sizeof (message), 451 "Process %" PRIu64 " %s\n", 452 process_sp->GetID(), 453 SBDebugger::StateAsCString (event_state)); 454 455 result.AppendMessage (message); 456 } 457 } 458 459 bool 460 SBProcess::SetSelectedThread (const SBThread &thread) 461 { 462 ProcessSP process_sp(GetSP()); 463 if (process_sp) 464 { 465 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 466 return process_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID()); 467 } 468 return false; 469 } 470 471 bool 472 SBProcess::SetSelectedThreadByID (lldb::tid_t tid) 473 { 474 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 475 476 bool ret_val = false; 477 ProcessSP process_sp(GetSP()); 478 if (process_sp) 479 { 480 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 481 ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid); 482 } 483 484 if (log) 485 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64 ") => %s", 486 process_sp.get(), tid, (ret_val ? "true" : "false")); 487 488 return ret_val; 489 } 490 491 bool 492 SBProcess::SetSelectedThreadByIndexID (uint32_t index_id) 493 { 494 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 495 496 bool ret_val = false; 497 ProcessSP process_sp(GetSP()); 498 if (process_sp) 499 { 500 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 501 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id); 502 } 503 504 if (log) 505 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s", 506 process_sp.get(), index_id, (ret_val ? "true" : "false")); 507 508 return ret_val; 509 } 510 511 SBThread 512 SBProcess::GetThreadAtIndex (size_t index) 513 { 514 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 515 516 SBThread sb_thread; 517 ThreadSP thread_sp; 518 ProcessSP process_sp(GetSP()); 519 if (process_sp) 520 { 521 Process::StopLocker stop_locker; 522 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 523 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 524 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update); 525 sb_thread.SetThread (thread_sp); 526 } 527 528 if (log) 529 { 530 log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)", 531 process_sp.get(), (uint32_t) index, thread_sp.get()); 532 } 533 534 return sb_thread; 535 } 536 537 uint32_t 538 SBProcess::GetNumQueues () 539 { 540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 541 542 uint32_t num_queues = 0; 543 ProcessSP process_sp(GetSP()); 544 if (process_sp) 545 { 546 Process::StopLocker stop_locker; 547 548 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 549 num_queues = process_sp->GetQueueList().GetSize(); 550 } 551 552 if (log) 553 log->Printf ("SBProcess(%p)::GetNumQueues () => %d", process_sp.get(), num_queues); 554 555 return num_queues; 556 } 557 558 SBQueue 559 SBProcess::GetQueueAtIndex (size_t index) 560 { 561 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 562 563 SBQueue sb_queue; 564 QueueSP queue_sp; 565 ProcessSP process_sp(GetSP()); 566 if (process_sp) 567 { 568 Process::StopLocker stop_locker; 569 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 570 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index); 571 sb_queue.SetQueue (queue_sp); 572 } 573 574 if (log) 575 { 576 log->Printf ("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)", 577 process_sp.get(), (uint32_t) index, queue_sp.get()); 578 } 579 580 return sb_queue; 581 } 582 583 584 uint32_t 585 SBProcess::GetStopID(bool include_expression_stops) 586 { 587 ProcessSP process_sp(GetSP()); 588 if (process_sp) 589 { 590 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 591 if (include_expression_stops) 592 return process_sp->GetStopID(); 593 else 594 return process_sp->GetLastNaturalStopID(); 595 } 596 return 0; 597 } 598 599 StateType 600 SBProcess::GetState () 601 { 602 603 StateType ret_val = eStateInvalid; 604 ProcessSP process_sp(GetSP()); 605 if (process_sp) 606 { 607 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 608 ret_val = process_sp->GetState(); 609 } 610 611 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 612 if (log) 613 log->Printf ("SBProcess(%p)::GetState () => %s", 614 process_sp.get(), 615 lldb_private::StateAsCString (ret_val)); 616 617 return ret_val; 618 } 619 620 621 int 622 SBProcess::GetExitStatus () 623 { 624 int exit_status = 0; 625 ProcessSP process_sp(GetSP()); 626 if (process_sp) 627 { 628 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 629 exit_status = process_sp->GetExitStatus (); 630 } 631 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 632 if (log) 633 log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)", 634 process_sp.get(), exit_status, exit_status); 635 636 return exit_status; 637 } 638 639 const char * 640 SBProcess::GetExitDescription () 641 { 642 const char *exit_desc = NULL; 643 ProcessSP process_sp(GetSP()); 644 if (process_sp) 645 { 646 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 647 exit_desc = process_sp->GetExitDescription (); 648 } 649 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 650 if (log) 651 log->Printf ("SBProcess(%p)::GetExitDescription () => %s", 652 process_sp.get(), exit_desc); 653 return exit_desc; 654 } 655 656 lldb::pid_t 657 SBProcess::GetProcessID () 658 { 659 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID; 660 ProcessSP process_sp(GetSP()); 661 if (process_sp) 662 ret_val = process_sp->GetID(); 663 664 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 665 if (log) 666 log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64, process_sp.get(), ret_val); 667 668 return ret_val; 669 } 670 671 uint32_t 672 SBProcess::GetUniqueID() 673 { 674 uint32_t ret_val = 0; 675 ProcessSP process_sp(GetSP()); 676 if (process_sp) 677 ret_val = process_sp->GetUniqueID(); 678 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 679 if (log) 680 log->Printf ("SBProcess(%p)::GetUniqueID () => %" PRIu32, process_sp.get(), ret_val); 681 return ret_val; 682 } 683 684 ByteOrder 685 SBProcess::GetByteOrder () const 686 { 687 ByteOrder byteOrder = eByteOrderInvalid; 688 ProcessSP process_sp(GetSP()); 689 if (process_sp) 690 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder(); 691 692 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 693 if (log) 694 log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder); 695 696 return byteOrder; 697 } 698 699 uint32_t 700 SBProcess::GetAddressByteSize () const 701 { 702 uint32_t size = 0; 703 ProcessSP process_sp(GetSP()); 704 if (process_sp) 705 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize(); 706 707 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 708 if (log) 709 log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size); 710 711 return size; 712 } 713 714 SBError 715 SBProcess::Continue () 716 { 717 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 718 719 SBError sb_error; 720 ProcessSP process_sp(GetSP()); 721 722 if (log) 723 log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get()); 724 725 if (process_sp) 726 { 727 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 728 729 Error error (process_sp->Resume()); 730 if (error.Success()) 731 { 732 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false) 733 { 734 if (log) 735 log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get()); 736 process_sp->WaitForProcessToStop (NULL); 737 } 738 } 739 sb_error.SetError(error); 740 } 741 else 742 sb_error.SetErrorString ("SBProcess is invalid"); 743 744 if (log) 745 { 746 SBStream sstr; 747 sb_error.GetDescription (sstr); 748 log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData()); 749 } 750 751 return sb_error; 752 } 753 754 755 SBError 756 SBProcess::Destroy () 757 { 758 SBError sb_error; 759 ProcessSP process_sp(GetSP()); 760 if (process_sp) 761 { 762 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 763 sb_error.SetError(process_sp->Destroy()); 764 } 765 else 766 sb_error.SetErrorString ("SBProcess is invalid"); 767 768 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 769 if (log) 770 { 771 SBStream sstr; 772 sb_error.GetDescription (sstr); 773 log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s", 774 process_sp.get(), 775 sb_error.get(), 776 sstr.GetData()); 777 } 778 779 return sb_error; 780 } 781 782 783 SBError 784 SBProcess::Stop () 785 { 786 SBError sb_error; 787 ProcessSP process_sp(GetSP()); 788 if (process_sp) 789 { 790 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 791 sb_error.SetError (process_sp->Halt()); 792 } 793 else 794 sb_error.SetErrorString ("SBProcess is invalid"); 795 796 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 797 if (log) 798 { 799 SBStream sstr; 800 sb_error.GetDescription (sstr); 801 log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s", 802 process_sp.get(), 803 sb_error.get(), 804 sstr.GetData()); 805 } 806 807 return sb_error; 808 } 809 810 SBError 811 SBProcess::Kill () 812 { 813 SBError sb_error; 814 ProcessSP process_sp(GetSP()); 815 if (process_sp) 816 { 817 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 818 sb_error.SetError (process_sp->Destroy()); 819 } 820 else 821 sb_error.SetErrorString ("SBProcess is invalid"); 822 823 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 824 if (log) 825 { 826 SBStream sstr; 827 sb_error.GetDescription (sstr); 828 log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s", 829 process_sp.get(), 830 sb_error.get(), 831 sstr.GetData()); 832 } 833 834 return sb_error; 835 } 836 837 SBError 838 SBProcess::Detach () 839 { 840 // FIXME: This should come from a process default. 841 bool keep_stopped = false; 842 return Detach (keep_stopped); 843 } 844 845 SBError 846 SBProcess::Detach (bool keep_stopped) 847 { 848 SBError sb_error; 849 ProcessSP process_sp(GetSP()); 850 if (process_sp) 851 { 852 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 853 sb_error.SetError (process_sp->Detach(keep_stopped)); 854 } 855 else 856 sb_error.SetErrorString ("SBProcess is invalid"); 857 858 return sb_error; 859 } 860 861 SBError 862 SBProcess::Signal (int signo) 863 { 864 SBError sb_error; 865 ProcessSP process_sp(GetSP()); 866 if (process_sp) 867 { 868 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 869 sb_error.SetError (process_sp->Signal (signo)); 870 } 871 else 872 sb_error.SetErrorString ("SBProcess is invalid"); 873 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 874 if (log) 875 { 876 SBStream sstr; 877 sb_error.GetDescription (sstr); 878 log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s", 879 process_sp.get(), 880 signo, 881 sb_error.get(), 882 sstr.GetData()); 883 } 884 return sb_error; 885 } 886 887 void 888 SBProcess::SendAsyncInterrupt () 889 { 890 ProcessSP process_sp(GetSP()); 891 if (process_sp) 892 { 893 process_sp->SendAsyncInterrupt (); 894 } 895 } 896 897 SBThread 898 SBProcess::GetThreadByID (tid_t tid) 899 { 900 SBThread sb_thread; 901 ThreadSP thread_sp; 902 ProcessSP process_sp(GetSP()); 903 if (process_sp) 904 { 905 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 906 Process::StopLocker stop_locker; 907 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 908 thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update); 909 sb_thread.SetThread (thread_sp); 910 } 911 912 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 913 if (log) 914 { 915 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)", 916 process_sp.get(), 917 tid, 918 thread_sp.get()); 919 } 920 921 return sb_thread; 922 } 923 924 SBThread 925 SBProcess::GetThreadByIndexID (uint32_t index_id) 926 { 927 SBThread sb_thread; 928 ThreadSP thread_sp; 929 ProcessSP process_sp(GetSP()); 930 if (process_sp) 931 { 932 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 933 Process::StopLocker stop_locker; 934 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 935 thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update); 936 sb_thread.SetThread (thread_sp); 937 } 938 939 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 940 if (log) 941 { 942 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)", 943 process_sp.get(), 944 index_id, 945 thread_sp.get()); 946 } 947 948 return sb_thread; 949 } 950 951 StateType 952 SBProcess::GetStateFromEvent (const SBEvent &event) 953 { 954 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 955 956 StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get()); 957 958 if (log) 959 log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(), 960 lldb_private::StateAsCString (ret_val)); 961 962 return ret_val; 963 } 964 965 bool 966 SBProcess::GetRestartedFromEvent (const SBEvent &event) 967 { 968 return Process::ProcessEventData::GetRestartedFromEvent (event.get()); 969 } 970 971 size_t 972 SBProcess::GetNumRestartedReasonsFromEvent (const lldb::SBEvent &event) 973 { 974 return Process::ProcessEventData::GetNumRestartedReasons(event.get()); 975 } 976 977 const char * 978 SBProcess::GetRestartedReasonAtIndexFromEvent (const lldb::SBEvent &event, size_t idx) 979 { 980 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx); 981 } 982 983 SBProcess 984 SBProcess::GetProcessFromEvent (const SBEvent &event) 985 { 986 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get())); 987 return process; 988 } 989 990 bool 991 SBProcess::EventIsProcessEvent (const SBEvent &event) 992 { 993 return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0; 994 } 995 996 SBBroadcaster 997 SBProcess::GetBroadcaster () const 998 { 999 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1000 1001 ProcessSP process_sp(GetSP()); 1002 1003 SBBroadcaster broadcaster(process_sp.get(), false); 1004 1005 if (log) 1006 log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)", process_sp.get(), 1007 broadcaster.get()); 1008 1009 return broadcaster; 1010 } 1011 1012 const char * 1013 SBProcess::GetBroadcasterClass () 1014 { 1015 return Process::GetStaticBroadcasterClass().AsCString(); 1016 } 1017 1018 size_t 1019 SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error) 1020 { 1021 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1022 1023 size_t bytes_read = 0; 1024 1025 ProcessSP process_sp(GetSP()); 1026 1027 if (log) 1028 { 1029 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...", 1030 process_sp.get(), 1031 addr, 1032 dst, 1033 (uint64_t)dst_len, 1034 sb_error.get()); 1035 } 1036 1037 if (process_sp) 1038 { 1039 Process::StopLocker stop_locker; 1040 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1041 { 1042 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1043 bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref()); 1044 } 1045 else 1046 { 1047 if (log) 1048 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get()); 1049 sb_error.SetErrorString("process is running"); 1050 } 1051 } 1052 else 1053 { 1054 sb_error.SetErrorString ("SBProcess is invalid"); 1055 } 1056 1057 if (log) 1058 { 1059 SBStream sstr; 1060 sb_error.GetDescription (sstr); 1061 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64, 1062 process_sp.get(), 1063 addr, 1064 dst, 1065 (uint64_t)dst_len, 1066 sb_error.get(), 1067 sstr.GetData(), 1068 (uint64_t)bytes_read); 1069 } 1070 1071 return bytes_read; 1072 } 1073 1074 size_t 1075 SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error) 1076 { 1077 size_t bytes_read = 0; 1078 ProcessSP process_sp(GetSP()); 1079 if (process_sp) 1080 { 1081 Process::StopLocker stop_locker; 1082 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1083 { 1084 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1085 bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref()); 1086 } 1087 else 1088 { 1089 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1090 if (log) 1091 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get()); 1092 sb_error.SetErrorString("process is running"); 1093 } 1094 } 1095 else 1096 { 1097 sb_error.SetErrorString ("SBProcess is invalid"); 1098 } 1099 return bytes_read; 1100 } 1101 1102 uint64_t 1103 SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error) 1104 { 1105 uint64_t value = 0; 1106 ProcessSP process_sp(GetSP()); 1107 if (process_sp) 1108 { 1109 Process::StopLocker stop_locker; 1110 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1111 { 1112 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1113 value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref()); 1114 } 1115 else 1116 { 1117 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1118 if (log) 1119 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get()); 1120 sb_error.SetErrorString("process is running"); 1121 } 1122 } 1123 else 1124 { 1125 sb_error.SetErrorString ("SBProcess is invalid"); 1126 } 1127 return value; 1128 } 1129 1130 lldb::addr_t 1131 SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error) 1132 { 1133 lldb::addr_t ptr = LLDB_INVALID_ADDRESS; 1134 ProcessSP process_sp(GetSP()); 1135 if (process_sp) 1136 { 1137 Process::StopLocker stop_locker; 1138 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1139 { 1140 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1141 ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref()); 1142 } 1143 else 1144 { 1145 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1146 if (log) 1147 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get()); 1148 sb_error.SetErrorString("process is running"); 1149 } 1150 } 1151 else 1152 { 1153 sb_error.SetErrorString ("SBProcess is invalid"); 1154 } 1155 return ptr; 1156 } 1157 1158 size_t 1159 SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error) 1160 { 1161 size_t bytes_written = 0; 1162 1163 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1164 1165 ProcessSP process_sp(GetSP()); 1166 1167 if (log) 1168 { 1169 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...", 1170 process_sp.get(), 1171 addr, 1172 src, 1173 (uint64_t)src_len, 1174 sb_error.get()); 1175 } 1176 1177 if (process_sp) 1178 { 1179 Process::StopLocker stop_locker; 1180 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1181 { 1182 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1183 bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref()); 1184 } 1185 else 1186 { 1187 if (log) 1188 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get()); 1189 sb_error.SetErrorString("process is running"); 1190 } 1191 } 1192 1193 if (log) 1194 { 1195 SBStream sstr; 1196 sb_error.GetDescription (sstr); 1197 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64, 1198 process_sp.get(), 1199 addr, 1200 src, 1201 (uint64_t)src_len, 1202 sb_error.get(), 1203 sstr.GetData(), 1204 (uint64_t)bytes_written); 1205 } 1206 1207 return bytes_written; 1208 } 1209 1210 bool 1211 SBProcess::GetDescription (SBStream &description) 1212 { 1213 Stream &strm = description.ref(); 1214 1215 ProcessSP process_sp(GetSP()); 1216 if (process_sp) 1217 { 1218 char path[PATH_MAX]; 1219 GetTarget().GetExecutable().GetPath (path, sizeof(path)); 1220 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer(); 1221 const char *exe_name = NULL; 1222 if (exe_module) 1223 exe_name = exe_module->GetFileSpec().GetFilename().AsCString(); 1224 1225 strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s", 1226 process_sp->GetID(), 1227 lldb_private::StateAsCString (GetState()), 1228 GetNumThreads(), 1229 exe_name ? ", executable = " : "", 1230 exe_name ? exe_name : ""); 1231 } 1232 else 1233 strm.PutCString ("No value"); 1234 1235 return true; 1236 } 1237 1238 uint32_t 1239 SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const 1240 { 1241 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1242 1243 uint32_t num = 0; 1244 ProcessSP process_sp(GetSP()); 1245 if (process_sp) 1246 { 1247 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1248 sb_error.SetError(process_sp->GetWatchpointSupportInfo (num)); 1249 if (log) 1250 log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u", 1251 process_sp.get(), num); 1252 } 1253 else 1254 { 1255 sb_error.SetErrorString ("SBProcess is invalid"); 1256 } 1257 return num; 1258 } 1259 1260 uint32_t 1261 SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error) 1262 { 1263 ProcessSP process_sp(GetSP()); 1264 if (process_sp) 1265 { 1266 Process::StopLocker stop_locker; 1267 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1268 { 1269 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1270 return process_sp->LoadImage (*sb_image_spec, sb_error.ref()); 1271 } 1272 else 1273 { 1274 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1275 if (log) 1276 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get()); 1277 sb_error.SetErrorString("process is running"); 1278 } 1279 } 1280 return LLDB_INVALID_IMAGE_TOKEN; 1281 } 1282 1283 lldb::SBError 1284 SBProcess::UnloadImage (uint32_t image_token) 1285 { 1286 lldb::SBError sb_error; 1287 ProcessSP process_sp(GetSP()); 1288 if (process_sp) 1289 { 1290 Process::StopLocker stop_locker; 1291 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1292 { 1293 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1294 sb_error.SetError (process_sp->UnloadImage (image_token)); 1295 } 1296 else 1297 { 1298 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1299 if (log) 1300 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get()); 1301 sb_error.SetErrorString("process is running"); 1302 } 1303 } 1304 else 1305 sb_error.SetErrorString("invalid process"); 1306 return sb_error; 1307 } 1308 1309 uint32_t 1310 SBProcess::GetNumExtendedBacktraceTypes () 1311 { 1312 ProcessSP process_sp(GetSP()); 1313 if (process_sp && process_sp->GetSystemRuntime()) 1314 { 1315 SystemRuntime *runtime = process_sp->GetSystemRuntime(); 1316 return runtime->GetExtendedBacktraceTypes().size(); 1317 } 1318 return 0; 1319 } 1320 1321 const char * 1322 SBProcess::GetExtendedBacktraceTypeAtIndex (uint32_t idx) 1323 { 1324 ProcessSP process_sp(GetSP()); 1325 if (process_sp && process_sp->GetSystemRuntime()) 1326 { 1327 SystemRuntime *runtime = process_sp->GetSystemRuntime(); 1328 const std::vector<ConstString> &names = runtime->GetExtendedBacktraceTypes(); 1329 if (idx < names.size()) 1330 { 1331 return names[idx].AsCString(); 1332 } 1333 else 1334 { 1335 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1336 if (log) 1337 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => error: requested extended backtrace name out of bounds", process_sp.get()); 1338 } 1339 } 1340 return NULL; 1341 } 1342