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