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