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