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