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 (uint32_t tid) 423 { 424 return SetSelectedThreadByID ((lldb::tid_t)tid); 425 } 426 427 bool 428 SBProcess::SetSelectedThreadByID (lldb::tid_t tid) 429 { 430 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 431 432 bool ret_val = false; 433 ProcessSP process_sp(GetSP()); 434 if (process_sp) 435 { 436 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 437 ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid); 438 } 439 440 if (log) 441 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4llx) => %s", 442 process_sp.get(), tid, (ret_val ? "true" : "false")); 443 444 return ret_val; 445 } 446 447 bool 448 SBProcess::SetSelectedThreadByIndexID (uint32_t index_id) 449 { 450 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 451 452 bool ret_val = false; 453 ProcessSP process_sp(GetSP()); 454 if (process_sp) 455 { 456 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 457 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id); 458 } 459 460 if (log) 461 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s", 462 process_sp.get(), index_id, (ret_val ? "true" : "false")); 463 464 return ret_val; 465 } 466 467 SBThread 468 SBProcess::GetThreadAtIndex (size_t index) 469 { 470 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 471 472 SBThread sb_thread; 473 ThreadSP thread_sp; 474 ProcessSP process_sp(GetSP()); 475 if (process_sp) 476 { 477 Process::StopLocker stop_locker; 478 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 479 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 480 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update); 481 sb_thread.SetThread (thread_sp); 482 } 483 484 if (log) 485 { 486 log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)", 487 process_sp.get(), (uint32_t) index, thread_sp.get()); 488 } 489 490 return sb_thread; 491 } 492 493 StateType 494 SBProcess::GetState () 495 { 496 497 StateType ret_val = eStateInvalid; 498 ProcessSP process_sp(GetSP()); 499 if (process_sp) 500 { 501 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 502 ret_val = process_sp->GetState(); 503 } 504 505 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 506 if (log) 507 log->Printf ("SBProcess(%p)::GetState () => %s", 508 process_sp.get(), 509 lldb_private::StateAsCString (ret_val)); 510 511 return ret_val; 512 } 513 514 515 int 516 SBProcess::GetExitStatus () 517 { 518 int exit_status = 0; 519 ProcessSP process_sp(GetSP()); 520 if (process_sp) 521 { 522 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 523 exit_status = process_sp->GetExitStatus (); 524 } 525 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 526 if (log) 527 log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)", 528 process_sp.get(), exit_status, exit_status); 529 530 return exit_status; 531 } 532 533 const char * 534 SBProcess::GetExitDescription () 535 { 536 const char *exit_desc = NULL; 537 ProcessSP process_sp(GetSP()); 538 if (process_sp) 539 { 540 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 541 exit_desc = process_sp->GetExitDescription (); 542 } 543 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 544 if (log) 545 log->Printf ("SBProcess(%p)::GetExitDescription () => %s", 546 process_sp.get(), exit_desc); 547 return exit_desc; 548 } 549 550 lldb::pid_t 551 SBProcess::GetProcessID () 552 { 553 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID; 554 ProcessSP process_sp(GetSP()); 555 if (process_sp) 556 ret_val = process_sp->GetID(); 557 558 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 559 if (log) 560 log->Printf ("SBProcess(%p)::GetProcessID () => %llu", process_sp.get(), ret_val); 561 562 return ret_val; 563 } 564 565 ByteOrder 566 SBProcess::GetByteOrder () const 567 { 568 ByteOrder byteOrder = eByteOrderInvalid; 569 ProcessSP process_sp(GetSP()); 570 if (process_sp) 571 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder(); 572 573 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 574 if (log) 575 log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder); 576 577 return byteOrder; 578 } 579 580 uint32_t 581 SBProcess::GetAddressByteSize () const 582 { 583 uint32_t size = 0; 584 ProcessSP process_sp(GetSP()); 585 if (process_sp) 586 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize(); 587 588 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 589 if (log) 590 log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size); 591 592 return size; 593 } 594 595 SBError 596 SBProcess::Continue () 597 { 598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 599 600 SBError sb_error; 601 ProcessSP process_sp(GetSP()); 602 603 if (log) 604 log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get()); 605 606 if (process_sp) 607 { 608 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 609 610 Error error (process_sp->Resume()); 611 if (error.Success()) 612 { 613 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false) 614 { 615 if (log) 616 log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get()); 617 process_sp->WaitForProcessToStop (NULL); 618 } 619 } 620 sb_error.SetError(error); 621 } 622 else 623 sb_error.SetErrorString ("SBProcess is invalid"); 624 625 if (log) 626 { 627 SBStream sstr; 628 sb_error.GetDescription (sstr); 629 log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData()); 630 } 631 632 return sb_error; 633 } 634 635 636 SBError 637 SBProcess::Destroy () 638 { 639 SBError sb_error; 640 ProcessSP process_sp(GetSP()); 641 if (process_sp) 642 { 643 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 644 sb_error.SetError(process_sp->Destroy()); 645 } 646 else 647 sb_error.SetErrorString ("SBProcess is invalid"); 648 649 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 650 if (log) 651 { 652 SBStream sstr; 653 sb_error.GetDescription (sstr); 654 log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s", 655 process_sp.get(), 656 sb_error.get(), 657 sstr.GetData()); 658 } 659 660 return sb_error; 661 } 662 663 664 SBError 665 SBProcess::Stop () 666 { 667 SBError sb_error; 668 ProcessSP process_sp(GetSP()); 669 if (process_sp) 670 { 671 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 672 sb_error.SetError (process_sp->Halt()); 673 } 674 else 675 sb_error.SetErrorString ("SBProcess is invalid"); 676 677 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 678 if (log) 679 { 680 SBStream sstr; 681 sb_error.GetDescription (sstr); 682 log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s", 683 process_sp.get(), 684 sb_error.get(), 685 sstr.GetData()); 686 } 687 688 return sb_error; 689 } 690 691 SBError 692 SBProcess::Kill () 693 { 694 SBError sb_error; 695 ProcessSP process_sp(GetSP()); 696 if (process_sp) 697 { 698 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 699 sb_error.SetError (process_sp->Destroy()); 700 } 701 else 702 sb_error.SetErrorString ("SBProcess is invalid"); 703 704 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 705 if (log) 706 { 707 SBStream sstr; 708 sb_error.GetDescription (sstr); 709 log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s", 710 process_sp.get(), 711 sb_error.get(), 712 sstr.GetData()); 713 } 714 715 return sb_error; 716 } 717 718 SBError 719 SBProcess::Detach () 720 { 721 SBError sb_error; 722 ProcessSP process_sp(GetSP()); 723 if (process_sp) 724 { 725 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 726 sb_error.SetError (process_sp->Detach()); 727 } 728 else 729 sb_error.SetErrorString ("SBProcess is invalid"); 730 731 return sb_error; 732 } 733 734 SBError 735 SBProcess::Signal (int signo) 736 { 737 SBError sb_error; 738 ProcessSP process_sp(GetSP()); 739 if (process_sp) 740 { 741 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 742 sb_error.SetError (process_sp->Signal (signo)); 743 } 744 else 745 sb_error.SetErrorString ("SBProcess is invalid"); 746 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 747 if (log) 748 { 749 SBStream sstr; 750 sb_error.GetDescription (sstr); 751 log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s", 752 process_sp.get(), 753 signo, 754 sb_error.get(), 755 sstr.GetData()); 756 } 757 return sb_error; 758 } 759 760 void 761 SBProcess::SendAsyncInterrupt () 762 { 763 ProcessSP process_sp(GetSP()); 764 if (process_sp) 765 { 766 process_sp->SendAsyncInterrupt (); 767 } 768 } 769 770 SBThread 771 SBProcess::GetThreadByID (tid_t tid) 772 { 773 SBThread sb_thread; 774 ThreadSP thread_sp; 775 ProcessSP process_sp(GetSP()); 776 if (process_sp) 777 { 778 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 779 Process::StopLocker stop_locker; 780 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 781 thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update); 782 sb_thread.SetThread (thread_sp); 783 } 784 785 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 786 if (log) 787 { 788 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4llx) => SBThread (%p)", 789 process_sp.get(), 790 tid, 791 thread_sp.get()); 792 } 793 794 return sb_thread; 795 } 796 797 SBThread 798 SBProcess::GetThreadByIndexID (uint32_t index_id) 799 { 800 SBThread sb_thread; 801 ThreadSP thread_sp; 802 ProcessSP process_sp(GetSP()); 803 if (process_sp) 804 { 805 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 806 Process::StopLocker stop_locker; 807 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 808 thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update); 809 sb_thread.SetThread (thread_sp); 810 } 811 812 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 813 if (log) 814 { 815 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)", 816 process_sp.get(), 817 index_id, 818 thread_sp.get()); 819 } 820 821 return sb_thread; 822 } 823 824 StateType 825 SBProcess::GetStateFromEvent (const SBEvent &event) 826 { 827 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 828 829 StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get()); 830 831 if (log) 832 log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(), 833 lldb_private::StateAsCString (ret_val)); 834 835 return ret_val; 836 } 837 838 bool 839 SBProcess::GetRestartedFromEvent (const SBEvent &event) 840 { 841 return Process::ProcessEventData::GetRestartedFromEvent (event.get()); 842 } 843 844 SBProcess 845 SBProcess::GetProcessFromEvent (const SBEvent &event) 846 { 847 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get())); 848 return process; 849 } 850 851 bool 852 SBProcess::EventIsProcessEvent (const SBEvent &event) 853 { 854 return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0; 855 } 856 857 SBBroadcaster 858 SBProcess::GetBroadcaster () const 859 { 860 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 861 862 ProcessSP process_sp(GetSP()); 863 864 SBBroadcaster broadcaster(process_sp.get(), false); 865 866 if (log) 867 log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)", process_sp.get(), 868 broadcaster.get()); 869 870 return broadcaster; 871 } 872 873 const char * 874 SBProcess::GetBroadcasterClass () 875 { 876 return Process::GetStaticBroadcasterClass().AsCString(); 877 } 878 879 size_t 880 SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error) 881 { 882 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 883 884 size_t bytes_read = 0; 885 886 ProcessSP process_sp(GetSP()); 887 888 if (log) 889 { 890 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%llx, dst=%p, dst_len=%llu, SBError (%p))...", 891 process_sp.get(), 892 addr, 893 dst, 894 (uint64_t)dst_len, 895 sb_error.get()); 896 } 897 898 if (process_sp) 899 { 900 Process::StopLocker stop_locker; 901 if (stop_locker.TryLock(&process_sp->GetRunLock())) 902 { 903 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 904 bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref()); 905 } 906 else 907 { 908 if (log) 909 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get()); 910 sb_error.SetErrorString("process is running"); 911 } 912 } 913 else 914 { 915 sb_error.SetErrorString ("SBProcess is invalid"); 916 } 917 918 if (log) 919 { 920 SBStream sstr; 921 sb_error.GetDescription (sstr); 922 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%llx, dst=%p, dst_len=%llu, SBError (%p): %s) => %llu", 923 process_sp.get(), 924 addr, 925 dst, 926 (uint64_t)dst_len, 927 sb_error.get(), 928 sstr.GetData(), 929 (uint64_t)bytes_read); 930 } 931 932 return bytes_read; 933 } 934 935 size_t 936 SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error) 937 { 938 size_t bytes_read = 0; 939 ProcessSP process_sp(GetSP()); 940 if (process_sp) 941 { 942 Process::StopLocker stop_locker; 943 if (stop_locker.TryLock(&process_sp->GetRunLock())) 944 { 945 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 946 bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref()); 947 } 948 else 949 { 950 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 951 if (log) 952 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get()); 953 sb_error.SetErrorString("process is running"); 954 } 955 } 956 else 957 { 958 sb_error.SetErrorString ("SBProcess is invalid"); 959 } 960 return bytes_read; 961 } 962 963 uint64_t 964 SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error) 965 { 966 uint64_t value = 0; 967 ProcessSP process_sp(GetSP()); 968 if (process_sp) 969 { 970 Process::StopLocker stop_locker; 971 if (stop_locker.TryLock(&process_sp->GetRunLock())) 972 { 973 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 974 value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref()); 975 } 976 else 977 { 978 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 979 if (log) 980 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get()); 981 sb_error.SetErrorString("process is running"); 982 } 983 } 984 else 985 { 986 sb_error.SetErrorString ("SBProcess is invalid"); 987 } 988 return value; 989 } 990 991 lldb::addr_t 992 SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error) 993 { 994 lldb::addr_t ptr = LLDB_INVALID_ADDRESS; 995 ProcessSP process_sp(GetSP()); 996 if (process_sp) 997 { 998 Process::StopLocker stop_locker; 999 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1000 { 1001 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1002 ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref()); 1003 } 1004 else 1005 { 1006 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1007 if (log) 1008 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get()); 1009 sb_error.SetErrorString("process is running"); 1010 } 1011 } 1012 else 1013 { 1014 sb_error.SetErrorString ("SBProcess is invalid"); 1015 } 1016 return ptr; 1017 } 1018 1019 size_t 1020 SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error) 1021 { 1022 size_t bytes_written = 0; 1023 1024 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1025 1026 ProcessSP process_sp(GetSP()); 1027 1028 if (log) 1029 { 1030 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%llx, src=%p, src_len=%llu, SBError (%p))...", 1031 process_sp.get(), 1032 addr, 1033 src, 1034 (uint64_t)src_len, 1035 sb_error.get()); 1036 } 1037 1038 if (process_sp) 1039 { 1040 Process::StopLocker stop_locker; 1041 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1042 { 1043 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1044 bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref()); 1045 } 1046 else 1047 { 1048 if (log) 1049 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get()); 1050 sb_error.SetErrorString("process is running"); 1051 } 1052 } 1053 1054 if (log) 1055 { 1056 SBStream sstr; 1057 sb_error.GetDescription (sstr); 1058 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%llx, src=%p, src_len=%llu, SBError (%p): %s) => %llu", 1059 process_sp.get(), 1060 addr, 1061 src, 1062 (uint64_t)src_len, 1063 sb_error.get(), 1064 sstr.GetData(), 1065 (uint64_t)bytes_written); 1066 } 1067 1068 return bytes_written; 1069 } 1070 1071 bool 1072 SBProcess::GetDescription (SBStream &description) 1073 { 1074 Stream &strm = description.ref(); 1075 1076 ProcessSP process_sp(GetSP()); 1077 if (process_sp) 1078 { 1079 char path[PATH_MAX]; 1080 GetTarget().GetExecutable().GetPath (path, sizeof(path)); 1081 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer(); 1082 const char *exe_name = NULL; 1083 if (exe_module) 1084 exe_name = exe_module->GetFileSpec().GetFilename().AsCString(); 1085 1086 strm.Printf ("SBProcess: pid = %llu, state = %s, threads = %d%s%s", 1087 process_sp->GetID(), 1088 lldb_private::StateAsCString (GetState()), 1089 GetNumThreads(), 1090 exe_name ? ", executable = " : "", 1091 exe_name ? exe_name : ""); 1092 } 1093 else 1094 strm.PutCString ("No value"); 1095 1096 return true; 1097 } 1098 1099 uint32_t 1100 SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const 1101 { 1102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1103 1104 uint32_t num = 0; 1105 ProcessSP process_sp(GetSP()); 1106 if (process_sp) 1107 { 1108 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1109 sb_error.SetError(process_sp->GetWatchpointSupportInfo (num)); 1110 if (log) 1111 log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u", 1112 process_sp.get(), num); 1113 } 1114 else 1115 { 1116 sb_error.SetErrorString ("SBProcess is invalid"); 1117 } 1118 return num; 1119 } 1120 1121 uint32_t 1122 SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error) 1123 { 1124 ProcessSP process_sp(GetSP()); 1125 if (process_sp) 1126 { 1127 Process::StopLocker stop_locker; 1128 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1129 { 1130 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1131 return process_sp->LoadImage (*sb_image_spec, sb_error.ref()); 1132 } 1133 else 1134 { 1135 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1136 if (log) 1137 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get()); 1138 sb_error.SetErrorString("process is running"); 1139 } 1140 } 1141 return LLDB_INVALID_IMAGE_TOKEN; 1142 } 1143 1144 lldb::SBError 1145 SBProcess::UnloadImage (uint32_t image_token) 1146 { 1147 lldb::SBError sb_error; 1148 ProcessSP process_sp(GetSP()); 1149 if (process_sp) 1150 { 1151 Process::StopLocker stop_locker; 1152 if (stop_locker.TryLock(&process_sp->GetRunLock())) 1153 { 1154 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex()); 1155 sb_error.SetError (process_sp->UnloadImage (image_token)); 1156 } 1157 else 1158 { 1159 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1160 if (log) 1161 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get()); 1162 sb_error.SetErrorString("process is running"); 1163 } 1164 } 1165 else 1166 sb_error.SetErrorString("invalid process"); 1167 return sb_error; 1168 } 1169