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