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