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