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