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