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