14#include "llvm/ADT/ScopeExit.h"
15#include "llvm/Support/ScopedPrinter.h"
16#include "llvm/Support/Threading.h"
78using namespace std::chrono;
82#define ENABLE_MEMORY_CACHING
84#ifdef ENABLE_MEMORY_CACHING
85#define DISABLE_MEM_CACHE_DEFAULT false
87#define DISABLE_MEM_CACHE_DEFAULT true
91 :
public Cloneable<ProcessOptionValueProperties, OptionValueProperties> {
107 if (
this != instance_properties)
119 "Continue tracing the parent process and detach the child.",
124 "Trace the child process and detach the parent.",
128#define LLDB_PROPERTIES_process
129#include "TargetProperties.inc"
132#define LLDB_PROPERTIES_process
133#include "TargetPropertiesEnum.inc"
137#define LLDB_PROPERTIES_process_experimental
138#include "TargetProperties.inc"
141#define LLDB_PROPERTIES_process_experimental
142#include "TargetPropertiesEnum.inc"
146 :
public Cloneable<ProcessExperimentalOptionValueProperties,
147 OptionValueProperties> {
164 if (process ==
nullptr) {
167 std::make_shared<ProcessOptionValueProperties>(
ConstString(
"process"));
176 ePropertyPythonOSPluginPath,
181 std::make_unique<ProcessExperimentalProperties>();
184 ConstString(
"Experimental settings - setting these won't produce "
185 "errors if the setting is not present."),
192 const uint32_t idx = ePropertyDisableMemCache;
194 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
198 const uint32_t idx = ePropertyMemCacheLineSize;
200 nullptr, idx, g_process_properties[idx].default_uint_value);
205 const uint32_t idx = ePropertyExtraStartCommand;
211 const uint32_t idx = ePropertyExtraStartCommand;
216 const uint32_t idx = ePropertyPythonOSPluginPath;
221 const uint32_t idx = ePropertyVirtualAddressableBits;
223 nullptr, idx, g_process_properties[idx].default_uint_value);
227 const uint32_t idx = ePropertyVirtualAddressableBits;
231 const uint32_t idx = ePropertyPythonOSPluginPath;
236 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
238 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
242 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
247 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
249 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
253 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
258 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
260 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
264 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
269 const uint32_t idx = ePropertyDisableLangRuntimeUnwindPlans;
271 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
275 const uint32_t idx = ePropertyDisableLangRuntimeUnwindPlans;
281 const uint32_t idx = ePropertyDetachKeepsStopped;
283 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
287 const uint32_t idx = ePropertyDetachKeepsStopped;
292 const uint32_t idx = ePropertyWarningOptimization;
294 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
298 const uint32_t idx = ePropertyWarningUnsupportedLanguage;
300 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
304 const uint32_t idx = ePropertyStopOnExec;
306 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
310 const uint32_t idx = ePropertyUtilityExpressionTimeout;
312 nullptr, idx, g_process_properties[idx].default_uint_value);
313 return std::chrono::seconds(value);
317 const uint32_t idx = ePropertyInterruptTimeout;
319 nullptr, idx, g_process_properties[idx].default_uint_value);
320 return std::chrono::seconds(value);
324 const uint32_t idx = ePropertySteppingRunsAllThreads;
326 nullptr, idx, g_process_properties[idx].default_uint_value != 0);
330 const bool fail_value =
true;
334 exp_property->
GetValue()->GetAsProperties();
339 nullptr, ePropertyOSPluginReportsAllThreads, fail_value);
346 exp_property->
GetValue()->GetAsProperties();
349 nullptr, ePropertyOSPluginReportsAllThreads, does_report);
353 const uint32_t idx = ePropertyFollowForkMode;
355 nullptr, idx, g_process_properties[idx].default_uint_value);
359 llvm::StringRef plugin_name,
360 ListenerSP listener_sp,
363 static uint32_t g_process_unique_id = 0;
365 ProcessSP process_sp;
366 ProcessCreateInstance create_callback =
nullptr;
367 if (!plugin_name.empty()) {
370 if (create_callback) {
371 process_sp = create_callback(target_sp, listener_sp, crash_file_path,
374 if (process_sp->CanDebug(target_sp,
true)) {
375 process_sp->m_process_unique_id = ++g_process_unique_id;
385 process_sp = create_callback(target_sp, listener_sp, crash_file_path,
388 if (process_sp->CanDebug(target_sp,
false)) {
389 process_sp->m_process_unique_id = ++g_process_unique_id;
411 const UnixSignalsSP &unix_signals_sp)
413 Broadcaster((target_sp->GetDebugger().GetBroadcasterManager()),
414 Process::GetStaticBroadcasterClass().AsCString()),
417 m_private_state_broadcaster(nullptr,
418 "lldb.process.internal_state_broadcaster"),
419 m_private_state_control_broadcaster(
420 nullptr,
"lldb.process.internal_state_control_broadcaster"),
421 m_private_state_listener_sp(
422 Listener::MakeListener(
"lldb.process.internal_state_listener")),
423 m_mod_id(), m_process_unique_id(0), m_thread_index_id(0),
424 m_thread_id_to_index_id_map(), m_exit_status(-1), m_exit_string(),
425 m_exit_status_mutex(), m_thread_mutex(), m_thread_list_real(this),
426 m_thread_list(this), m_thread_plans(*this), m_extended_thread_list(this),
427 m_extended_thread_stop_id(0), m_queue_list(this), m_queue_list_stop_id(0),
428 m_notifications(), m_image_tokens(), m_listener_sp(listener_sp),
429 m_breakpoint_site_list(), m_dynamic_checkers_up(),
430 m_unix_signals_sp(unix_signals_sp), m_abi_sp(), m_process_input_reader(),
431 m_stdio_communication(
"process.stdio"), m_stdio_communication_mutex(),
432 m_stdin_forward(false), m_stdout_data(), m_stderr_data(),
433 m_profile_data_comm_mutex(), m_profile_data(), m_iohandler_sync(0),
434 m_memory_cache(*this), m_allocated_memory_cache(*this),
435 m_should_detach(false), m_next_event_action_up(), m_public_run_lock(),
436 m_private_run_lock(), m_currently_handling_do_on_removals(false),
437 m_resume_requested(false), m_finalizing(false),
438 m_clear_thread_plans_on_stop(false), m_force_next_event_delivery(false),
439 m_last_broadcast_state(
eStateInvalid), m_destroy_in_process(false),
440 m_can_interpret_function_calls(false), m_run_thread_plan_lock(),
441 m_can_jit(eCanJITDontKnow) {
445 LLDB_LOGF(log,
"%p Process::Process()",
static_cast<void *
>(
this));
481 OptionValueSP value_sp =
483 ->GetPropertyAtIndex(
nullptr,
true, ePropertyMemCacheLineSize)
486 target_sp->GetPlatform()->GetDefaultMemoryCacheLineSize();
487 if (!value_sp->OptionWasSet() && platform_cache_line_size != 0)
488 value_sp->SetUInt64Value(platform_cache_line_size);
495 LLDB_LOGF(log,
"%p Process::~Process()",
static_cast<void *
>(
this));
509 return *g_settings_ptr;
541 std::vector<Notifications> empty_notifications;
584 if (pos->baton == callbacks.
baton &&
595 std::vector<Notifications>::iterator notification_pos,
598 notification_pos != notification_end; ++notification_pos) {
599 if (notification_pos->process_state_changed)
600 notification_pos->process_state_changed(notification_pos->baton,
this,
619 std::chrono::seconds(0)) &&
639 "waited from m_iohandler_sync to change from {0}. New value is {1}.",
640 iohandler_id, *Result);
642 LLDB_LOG(log,
"timed out waiting for m_iohandler_sync to change from {0}.",
648 EventSP *event_sp_ptr,
bool wait_always,
649 ListenerSP hijack_listener_sp,
650 Stream *stream,
bool use_run_lock) {
655 event_sp_ptr->reset();
663 LLDB_LOG(log,
"timeout = {0}", timeout);
668 "Process::%s returning without waiting for events; process "
669 "private and public states are already 'stopped'.",
673 if (hijack_listener_sp && use_run_lock)
681 if (event_sp_ptr && event_sp)
682 *event_sp_ptr = event_sp;
684 bool pop_process_io_handler = (hijack_listener_sp.get() !=
nullptr);
686 pop_process_io_handler);
695 if (hijack_listener_sp && use_run_lock)
704 if (hijack_listener_sp && use_run_lock)
717 bool &pop_process_io_handler) {
718 const bool handle_pop = pop_process_io_handler;
720 pop_process_io_handler =
false;
721 ProcessSP process_sp =
732 switch (event_state) {
740 stream->
Printf(
"Process %" PRIu64
" %s\n", process_sp->GetID(),
743 pop_process_io_handler =
true;
753 process_sp->GetStatus(*stream);
754 pop_process_io_handler =
true;
765 if (num_reasons > 0) {
768 if (num_reasons == 1) {
772 stream->
Printf(
"Process %" PRIu64
" stopped and restarted: %s\n",
774 reason ? reason :
"<UNKNOWN REASON>");
776 stream->
Printf(
"Process %" PRIu64
777 " stopped and restarted, reasons:\n",
778 process_sp->GetID());
780 for (
size_t i = 0; i < num_reasons; i++) {
784 stream->
Printf(
"\t%s\n", reason ? reason :
"<UNKNOWN REASON>");
790 StopInfoSP curr_thread_stop_info_sp;
794 ThreadList &thread_list = process_sp->GetThreadList();
795 std::lock_guard<std::recursive_mutex> guard(thread_list.
GetMutex());
800 bool prefer_curr_thread =
false;
801 if (curr_thread && curr_thread->IsValid()) {
802 curr_thread_stop_reason = curr_thread->GetStopReason();
803 switch (curr_thread_stop_reason) {
812 uint64_t signo = curr_thread->GetStopInfo()->GetValue();
813 if (process_sp->GetUnixSignals()->GetShouldStop(signo))
814 prefer_curr_thread =
true;
817 prefer_curr_thread =
true;
820 curr_thread_stop_info_sp = curr_thread->GetStopInfo();
823 if (!prefer_curr_thread) {
826 ThreadSP plan_thread;
827 ThreadSP other_thread;
829 const size_t num_threads = thread_list.
GetSize();
831 for (i = 0; i < num_threads; ++i) {
833 StopReason thread_stop_reason = thread->GetStopReason();
834 switch (thread_stop_reason) {
843 uint64_t signo = thread->GetStopInfo()->GetValue();
844 if (process_sp->GetUnixSignals()->GetShouldStop(signo)) {
846 other_thread = thread;
862 other_thread = thread;
866 plan_thread = thread;
872 else if (other_thread)
875 if (curr_thread && curr_thread->IsValid())
876 thread = curr_thread;
890 Debugger &debugger = process_sp->GetTarget().GetDebugger();
892 &process_sp->GetTarget()) {
893 ThreadSP thread_sp = process_sp->GetThreadList().GetSelectedThread();
895 if (!thread_sp || !thread_sp->IsValid())
898 const bool only_threads_with_stop_reason =
true;
899 const uint32_t start_frame = thread_sp->GetSelectedFrameIndex();
901 const uint32_t num_frames_with_source = 1;
902 const bool stop_format =
true;
904 process_sp->GetStatus(*stream);
905 process_sp->GetThreadStatus(*stream, only_threads_with_stop_reason,
906 start_frame, num_frames,
907 num_frames_with_source,
909 if (curr_thread_stop_info_sp) {
912 curr_thread_stop_info_sp, &crashing_address);
918 valobj_sp->GetExpressionPath(*stream, format);
919 stream->
Printf(
" accessed 0x%" PRIx64
"\n", crashing_address);
924 process_sp->GetTarget().shared_from_this());
926 stream->
Printf(
"Target %d: (", target_idx);
928 stream->
Printf(
"Target <unknown index>: (");
930 stream->
Printf(
") stopped.\n");
935 pop_process_io_handler =
true;
940 if (handle_pop && pop_process_io_handler)
941 process_sp->PopProcessIOHandler();
958 ListenerSP hijack_listener_sp) {
960 LLDB_LOG(log,
"timeout = {0}, event_sp)...", timeout);
962 ListenerSP listener_sp = hijack_listener_sp;
967 if (listener_sp->GetEventForBroadcasterWithType(
973 LLDB_LOG(log,
"got no event or was interrupted.");
976 LLDB_LOG(log,
"timeout = {0}, event_sp) => {1}", timeout, state);
983 LLDB_LOGF(log,
"Process::%s...", __FUNCTION__);
986 event_ptr =
m_listener_sp->PeekAtNextEventForBroadcasterWithType(
990 LLDB_LOGF(log,
"Process::%s (event_ptr) => %s", __FUNCTION__,
993 LLDB_LOGF(log,
"Process::%s no events found", __FUNCTION__);
1003 LLDB_LOG(log,
"timeout = {0}, event_sp)...", timeout);
1013 LLDB_LOG(log,
"timeout = {0}, event_sp) => {1}", timeout,
1020 bool control_only) {
1022 LLDB_LOG(log,
"timeout = {0}, event_sp)...", timeout);
1057 log,
"Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1058 status, status, cstr ?
"\"" :
"", cstr ? cstr :
"NULL", cstr ?
"\"" :
"");
1062 LLDB_LOGF(log,
"Process::SetExitStatus () ignoring exit status because "
1063 "state was already set to eStateExited");
1112 "Process::SetProcessExitStatus (pid=%" PRIu64
1113 ", exited=%i, signal=%i, exit_status=%i)\n",
1114 pid, exited, signo, exit_status);
1119 ProcessSP process_sp(target_sp->GetProcessSP());
1121 const char *signal_cstr =
nullptr;
1123 signal_cstr = process_sp->GetUnixSignals()->GetSignalAsCString(signo);
1125 process_sp->SetExitStatus(exit_status, signal_cstr);
1143 bool clear_unused_threads =
true;
1166 size_t num_old_threads = old_thread_list.
GetSize(
false);
1167 for (
size_t i = 0; i < num_old_threads; ++i)
1200 new_thread_list = real_thread_list;
1240 bool internal,
bool condense_trivial,
1241 bool skip_unreported_plans) {
1243 strm, tid, desc_level, internal, condense_trivial, skip_unreported_plans);
1246 bool internal,
bool condense_trivial,
1247 bool skip_unreported_plans) {
1249 skip_unreported_plans);
1283 std::map<uint64_t, uint32_t>::iterator iterator =
1289 result = iterator->second;
1304 if (new_state_is_stopped) {
1315 LLDB_LOGF(log,
"Process::SetPublicState (state = %s, restarted = %i)",
1326 "Process::SetPublicState (%s) -- unlocking run lock for detach",
1331 if ((old_state_is_stopped != new_state_is_stopped)) {
1332 if (new_state_is_stopped && !restarted) {
1333 LLDB_LOGF(log,
"Process::SetPublicState (%s) -- unlocking run lock",
1344 LLDB_LOGF(log,
"Process::Resume -- locking run lock");
1346 Status error(
"Resume request failed - process still running.");
1347 LLDB_LOGF(log,
"Process::Resume: -- TrySetRunning failed, not resuming.");
1351 if (!
error.Success()) {
1362 LLDB_LOGF(log,
"Process::ResumeSynchronous -- locking run lock");
1364 Status error(
"Resume request failed - process still running.");
1365 LLDB_LOGF(log,
"Process::Resume: -- TrySetRunning failed, not resuming.");
1369 ListenerSP listener_sp(
1374 if (
error.Success()) {
1377 const bool must_be_alive =
1380 error.SetErrorStringWithFormat(
1381 "process not in stopped state after synchronous resume: %s",
1397 if (hijacking_name &&
1407 if (hijacking_name &&
1421 bool state_changed =
false;
1429 state_changed = old_state != new_state;
1433 if (old_state_is_stopped != new_state_is_stopped) {
1434 if (new_state_is_stopped)
1440 if (state_changed) {
1464 LLDB_LOGF(log,
"Process::SetPrivateState (%s) stop_id = %u",
1471 "Process::SetPrivateState (%s) state didn't change. Ignoring...",
1493 std::vector<LanguageRuntime *> language_runtimes;
1496 return language_runtimes;
1506 language_runtimes.emplace_back(runtime);
1509 return language_runtimes;
1519 LanguageRuntimeCollection::iterator pos;
1522 lldb::LanguageRuntimeSP runtime_sp(
1526 runtime = runtime_sp.get();
1528 runtime = pos->second.get();
1554 if (runtime->CouldHaveDynamicValue(in_value))
1583 if (
error.Success())
1593 if (bp_site_sp->IsEnabled())
1596 error.SetErrorStringWithFormat(
"invalid breakpoint site ID: %" PRIu64,
1607 if (!bp_site_sp->IsEnabled())
1610 error.SetErrorStringWithFormat(
"invalid breakpoint site ID: %" PRIu64,
1618 bool use_hardware) {
1621 bool show_error =
true;
1644 owner->SetIsIndirect(
false);
1646 if (owner->ShouldResolveIndirectFunctions()) {
1652 if (!
error.Success() && show_error) {
1654 "warning: failed to resolve indirect function at 0x%" PRIx64
1655 " for breakpoint %i.%i: %s\n",
1657 owner->GetBreakpoint().GetID(), owner->GetID(),
1658 error.AsCString() ?
error.AsCString() :
"unknown error");
1661 Address resolved_address(load_addr);
1663 owner->SetIsIndirect(
true);
1665 load_addr = owner->GetAddress().GetOpcodeLoadAddress(&
GetTarget());
1667 load_addr = owner->GetAddress().GetOpcodeLoadAddress(&
GetTarget());
1670 BreakpointSiteSP bp_site_sp;
1678 bp_site_sp->AddOwner(owner);
1679 owner->SetBreakpointSite(bp_site_sp);
1680 return bp_site_sp->GetID();
1683 load_addr, use_hardware));
1686 if (
error.Success()) {
1687 owner->SetBreakpointSite(bp_site_sp);
1690 if (show_error || use_hardware) {
1693 "warning: failed to set breakpoint site at 0x%" PRIx64
1694 " for breakpoint %i.%i: %s\n",
1695 load_addr, owner->GetBreakpoint().GetID(), owner->GetID(),
1696 error.AsCString() ?
error.AsCString() :
"unknown error");
1708 BreakpointSiteSP &bp_site_sp) {
1709 uint32_t num_owners = bp_site_sp->RemoveOwner(owner_id, owner_loc_id);
1710 if (num_owners == 0) {
1719 uint8_t *buf)
const {
1720 size_t bytes_removed = 0;
1724 bp_sites_in_range)) {
1725 bp_sites_in_range.
ForEach([bp_addr, size,
1728 addr_t intersect_addr;
1729 size_t intersect_size;
1730 size_t opcode_offset;
1731 if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr,
1732 &intersect_size, &opcode_offset)) {
1733 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1734 assert(bp_addr < intersect_addr + intersect_size &&
1735 intersect_addr + intersect_size <= bp_addr + size);
1736 assert(opcode_offset + intersect_size <= bp_site->GetByteSize());
1737 size_t buf_offset = intersect_addr - bp_addr;
1738 ::memcpy(buf + buf_offset,
1739 bp_site->GetSavedOpcodeBytes() + opcode_offset,
1745 return bytes_removed;
1749 PlatformSP platform_sp(
GetTarget().GetPlatform());
1751 return platform_sp->GetSoftwareBreakpointTrapOpcode(
GetTarget(), bp_site);
1757 assert(bp_site !=
nullptr);
1761 log,
"Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64,
1762 bp_site->
GetID(), (uint64_t)bp_addr);
1766 "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1767 " -- already enabled",
1768 bp_site->
GetID(), (uint64_t)bp_addr);
1773 error.SetErrorString(
"BreakpointSite contains an invalid load address.");
1780 if (bp_opcode_size == 0) {
1781 error.SetErrorStringWithFormat(
"Process::GetSoftwareBreakpointTrapOpcode() "
1782 "returned zero, unable to get breakpoint "
1783 "trap for address 0x%" PRIx64,
1788 if (bp_opcode_bytes ==
nullptr) {
1789 error.SetErrorString(
1790 "BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1796 error) == bp_opcode_size) {
1800 uint8_t verify_bp_opcode_bytes[64];
1801 if (
DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size,
1802 error) == bp_opcode_size) {
1803 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes,
1804 bp_opcode_size) == 0) {
1808 "Process::EnableSoftwareBreakpoint (site_id = %d) "
1809 "addr = 0x%" PRIx64
" -- SUCCESS",
1810 bp_site->
GetID(), (uint64_t)bp_addr);
1812 error.SetErrorString(
1813 "failed to verify the breakpoint trap in memory.");
1815 error.SetErrorString(
1816 "Unable to read memory to verify breakpoint trap.");
1818 error.SetErrorString(
"Unable to write breakpoint trap to memory.");
1820 error.SetErrorString(
"Unable to read memory at breakpoint address.");
1822 if (log &&
error.Fail())
1825 "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1827 bp_site->
GetID(), (uint64_t)bp_addr,
error.AsCString());
1833 assert(bp_site !=
nullptr);
1838 "Process::DisableSoftwareBreakpoint (breakID = %" PRIu64
1839 ") addr = 0x%" PRIx64,
1840 breakID, (uint64_t)bp_addr);
1843 error.SetErrorString(
"Breakpoint site is a hardware breakpoint.");
1845 const size_t break_op_size = bp_site->
GetByteSize();
1847 if (break_op_size > 0) {
1849 uint8_t curr_break_op[8];
1850 assert(break_op_size <=
sizeof(curr_break_op));
1851 bool break_op_found =
false;
1856 bool verify =
false;
1858 if (::memcmp(curr_break_op, break_op, break_op_size) == 0) {
1859 break_op_found =
true;
1863 break_op_size,
error) == break_op_size) {
1866 error.SetErrorString(
1867 "Memory write failed when restoring original opcode.");
1869 error.SetErrorString(
1870 "Original breakpoint trap is no longer in memory.");
1877 uint8_t verify_opcode[8];
1878 assert(break_op_size <
sizeof(verify_opcode));
1884 break_op_size) == 0) {
1888 "Process::DisableSoftwareBreakpoint (site_id = %d) "
1889 "addr = 0x%" PRIx64
" -- SUCCESS",
1890 bp_site->
GetID(), (uint64_t)bp_addr);
1894 error.SetErrorString(
"Failed to restore original opcode.");
1897 error.SetErrorString(
"Failed to read memory to verify that "
1898 "breakpoint trap was restored.");
1901 error.SetErrorString(
1902 "Unable to read memory that should contain the breakpoint trap.");
1907 "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1908 " -- already disabled",
1909 bp_site->
GetID(), (uint64_t)bp_addr);
1915 "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1917 bp_site->
GetID(), (uint64_t)bp_addr,
error.AsCString());
1926 if (ABISP abi_sp =
GetABI())
1927 addr = abi_sp->FixAnyAddress(addr);
1931#if defined(VERIFY_MEMORY_READS)
1943 std::string verify_buf(size,
'\0');
1944 assert(verify_buf.size() == size);
1945 const size_t cache_bytes_read =
1948 const size_t verify_bytes_read =
1950 verify_buf.size(), verify_error);
1951 assert(cache_bytes_read == verify_bytes_read);
1952 assert(memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1954 return cache_bytes_read;
1978 out_str.append(buf, length);
1981 if (length ==
sizeof(buf) - 1)
1982 curr_addr += length;
1986 return out_str.size();
1994 size_t total_cstr_len = 0;
1995 if (dst && dst_max_len) {
1996 result_error.
Clear();
1998 memset(dst, 0, dst_max_len);
2002 size_t bytes_left = dst_max_len - 1;
2003 char *curr_dst = dst;
2005 while (bytes_left > 0) {
2006 addr_t cache_line_bytes_left =
2007 cache_line_size - (curr_addr % cache_line_size);
2009 std::min<addr_t>(bytes_left, cache_line_bytes_left);
2010 size_t bytes_read =
ReadMemory(curr_addr, curr_dst, bytes_to_read,
error);
2012 if (bytes_read == 0) {
2013 result_error =
error;
2014 dst[total_cstr_len] =
'\0';
2017 const size_t len = strlen(curr_dst);
2019 total_cstr_len += len;
2021 if (len < bytes_to_read)
2024 curr_dst += bytes_read;
2025 curr_addr += bytes_read;
2026 bytes_left -= bytes_read;
2032 result_error.
Clear();
2034 return total_cstr_len;
2041 if (ABISP abi_sp =
GetABI())
2042 addr = abi_sp->FixAnyAddress(addr);
2044 if (buf ==
nullptr || size == 0)
2047 size_t bytes_read = 0;
2048 uint8_t *bytes = (uint8_t *)buf;
2050 while (bytes_read < size) {
2051 const size_t curr_size = size - bytes_read;
2052 const size_t curr_bytes_read =
2054 bytes_read += curr_bytes_read;
2055 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2067 size_t integer_byte_size,
2068 uint64_t fail_value,
2078 size_t integer_byte_size,
2100 if (addr_byte_size <= 4)
2110 size_t bytes_written = 0;
2111 const uint8_t *bytes = (
const uint8_t *)buf;
2113 while (bytes_written < size) {
2114 const size_t curr_size = size - bytes_written;
2116 addr + bytes_written, bytes + bytes_written, curr_size,
error);
2117 bytes_written += curr_bytes_written;
2118 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2121 return bytes_written;
2126 if (ABISP abi_sp =
GetABI())
2127 addr = abi_sp->FixAnyAddress(addr);
2129#if defined(ENABLE_MEMORY_CACHING)
2133 if (buf ==
nullptr || size == 0)
2147 if (bp_sites_in_range.
IsEmpty())
2150 const uint8_t *ubuf = (
const uint8_t *)buf;
2151 uint64_t bytes_written = 0;
2153 bp_sites_in_range.
ForEach([
this, addr, size, &bytes_written, &ubuf,
2162 size_t intersect_size;
2163 size_t opcode_offset;
2165 addr, size, &intersect_addr, &intersect_size, &opcode_offset);
2168 assert(addr <= intersect_addr && intersect_addr < addr + size);
2169 assert(addr < intersect_addr + intersect_size &&
2170 intersect_addr + intersect_size <= addr + size);
2171 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2174 const addr_t curr_addr = addr + bytes_written;
2175 if (intersect_addr > curr_addr) {
2178 size_t curr_size = intersect_addr - curr_addr;
2179 size_t curr_bytes_written =
2181 bytes_written += curr_bytes_written;
2182 if (curr_bytes_written != curr_size) {
2186 if (
error.Success())
2187 error.SetErrorToGenericError();
2194 bytes_written += intersect_size;
2198 if (bytes_written < size)
2201 size - bytes_written,
error);
2203 return bytes_written;
2210 if (byte_size > 0) {
2212 const size_t mem_size =
2217 error.SetErrorString(
"failed to get scalar as memory data");
2219 error.SetErrorString(
"invalid scalar value");
2225 bool is_signed,
Scalar &scalar,
2228 if (byte_size == 0) {
2229 error.SetErrorString(
"byte size is zero");
2230 }
else if (byte_size & (byte_size - 1)) {
2231 error.SetErrorStringWithFormat(
"byte size %u is not a power of 2",
2233 }
else if (byte_size <=
sizeof(uval)) {
2235 if (bytes_read == byte_size) {
2240 scalar = data.
GetMaxU32(&offset, byte_size);
2242 scalar = data.
GetMaxU64(&offset, byte_size);
2248 error.SetErrorStringWithFormat(
2249 "byte size of %u is too large for integer scalar type", byte_size);
2256 for (
const auto &
Entry : entries) {
2259 if (!
error.Success())
2265#define USE_ALLOCATE_MEMORY_CACHE 1
2269 error.SetErrorToGenericError();
2273#if defined(USE_ALLOCATE_MEMORY_CACHE)
2279 "Process::AllocateMemory(size=%" PRIu64
2280 ", permissions=%s) => 0x%16.16" PRIx64
2281 " (m_stop_id = %u m_memory_id = %u)",
2285 return allocated_addr;
2292 if (
error.Success()) {
2293 std::string buffer(size, 0);
2305 8, ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2311 "Process::%s pid %" PRIu64
2312 " allocation test passed, CanJIT () is true",
2313 __FUNCTION__,
GetID());
2317 "Process::%s pid %" PRIu64
2318 " allocation test failed, CanJIT () is false: %s",
2339#if defined(USE_ALLOCATE_MEMORY_CACHE)
2341 error.SetErrorStringWithFormat(
2342 "deallocation of memory at 0x%" PRIx64
" failed.", (uint64_t)ptr);
2349 "Process::DeallocateMemory(addr=0x%16.16" PRIx64
2350 ") => err = %s (m_stop_id = %u, m_memory_id = %u)",
2359 return *subclass_override;
2361 bool reported_after =
true;
2364 return reported_after;
2367 if (triple.isMIPS() || triple.isPPC64() || triple.isRISCV() ||
2368 triple.isAArch64() || triple.isArmMClass() || triple.isARM())
2369 reported_after =
false;
2371 return reported_after;
2376 size_t size_to_read) {
2380 "Process::ReadModuleFromMemory reading %s binary from memory",
2386 ObjectFile *objfile = module_sp->GetMemoryObjectFile(
2387 shared_from_this(), header_addr,
error, size_to_read);
2399 if (!
error.Success())
2408 permissions |= lldb::ePermissionsReadable;
2411 permissions |= lldb::ePermissionsWritable;
2414 permissions |= lldb::ePermissionsExecutable;
2421 error.SetErrorString(
"watchpoints are not supported");
2427 error.SetErrorString(
"watchpoints are not supported");
2463 EventSP first_stop_event_sp;
2465 LaunchPrivate(launch_info, state_after_launch, first_stop_event_sp);
2487 if (launch_info.
GetFlags().
Test(eLaunchFlagStopAtEntry))
2494 EventSP &event_sp) {
2515 error.SetErrorString(
"executable module does not exist");
2541 std::string local_exec_file_path = exe_spec_to_use.
GetPath();
2542 return Status(
"file doesn't exist: '%s'", local_exec_file_path.c_str());
2545 const bool restarted =
false;
2554 error.SetErrorString(
"failed to acquire process run lock");
2560 const char *error_string =
error.AsCString();
2561 if (error_string ==
nullptr)
2562 error_string =
"launch failed";
2575 error.SetErrorString(
"failed to catch stop after launch");
2617 return Status(
"Unexpected process state after the launch: %s, expected %s, "
2626 if (
error.Success()) {
2627 ListenerSP listener_sp(
2654 lldb::EventSP event_sp;
2660 LLDB_LOGF(log,
"Process::Halt() failed to stop, state is: %s",
2662 error.SetErrorString(
2663 "Did not get stopped event after loading the core file.");
2706 "Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32,
2707 __FUNCTION__,
static_cast<void *
>(process), exec_count);
2716 "Process::AttachCompletionHandler::%s called with state %s (%d)",
2721 return eEventActionSuccess;
2725 return eEventActionRetry;
2736 if (m_exec_count > 0) {
2740 "Process::AttachCompletionHandler::%s state %s: reduced "
2741 "remaining exec count to %" PRIu32
", requesting resume",
2745 return eEventActionRetry;
2748 "Process::AttachCompletionHandler::%s state %s: no more "
2749 "execs expected to start, continuing with attach",
2753 return eEventActionSuccess;
2764 return eEventActionExit;
2769 return eEventActionSuccess;
2792 bool wait_for_launch) {
2810 sizeof(process_name))) {
2813 if (wait_for_launch) {
2815 if (
error.Success()) {
2818 const bool restarted =
false;
2824 error.SetErrorString(
"failed to acquire process run lock");
2830 if (
error.AsCString() ==
nullptr)
2831 error.SetErrorString(
"attach failed");
2844 PlatformSP platform_sp(
GetTarget().GetPlatform());
2850 platform_sp->FindProcesses(match_info, process_infos);
2851 const uint32_t num_matches = process_infos.size();
2852 if (num_matches == 1) {
2853 attach_pid = process_infos[0].GetProcessID();
2857 process_name,
sizeof(process_name));
2858 if (num_matches > 1) {
2861 for (
size_t i = 0; i < num_matches; i++) {
2862 process_infos[i].DumpAsTableRow(
2863 s, platform_sp->GetUserIDResolver(),
true,
false);
2865 error.SetErrorStringWithFormat(
2866 "more than one process named %s:\n%s", process_name,
2869 error.SetErrorStringWithFormat(
2870 "could not find a process named %s", process_name);
2873 error.SetErrorString(
2874 "invalid platform, can't find processes by name");
2879 error.SetErrorString(
"invalid process name");
2885 if (
error.Success()) {
2890 const bool restarted =
false;
2895 error.SetErrorString(
"failed to acquire process run lock");
2898 if (
error.Success()) {
2906 const char *error_string =
error.AsCString();
2907 if (error_string ==
nullptr)
2908 error_string =
"attach failed";
2919 LLDB_LOGF(log,
"Process::%s()", __FUNCTION__);
2929 const char *triple_str = process_arch.
GetTriple().getTriple().c_str();
2931 "Process::%s replacing process architecture with DidAttach() "
2933 __FUNCTION__, triple_str ? triple_str :
"<null>");
2940 PlatformSP platform_sp(
GetTarget().GetPlatform());
2941 assert(platform_sp);
2945 if (target_arch.
IsValid() && !platform_sp->IsCompatibleArchitecture(
2946 target_arch, process_host_arch,
2950 target_arch, process_host_arch, &platform_arch);
2955 "switching platform to {0} and architecture to {1} based on "
2957 platform_sp->GetName(), platform_arch.
GetTriple().getTriple());
2959 }
else if (!process_arch.
IsValid()) {
2969 "Process::%s switching architecture to %s based on info "
2970 "the platform retrieved for pid %" PRIu64,
2971 __FUNCTION__, process_arch.
GetTriple().getTriple().c_str(),
2991 "after DynamicLoader::DidAttach(), target "
2992 "executable is {0} (using {1} plugin)",
2993 exe_module_sp ? exe_module_sp->GetFileSpec() :
FileSpec(),
3001 if (system_runtime) {
3006 "after SystemRuntime::DidAttach(), target "
3007 "executable is {0} (using {1} plugin)",
3008 exe_module_sp ? exe_module_sp->GetFileSpec() :
FileSpec(),
3024 ModuleSP new_executable_module_sp;
3026 if (module_sp && module_sp->IsExecutable()) {
3027 if (
GetTarget().GetExecutableModulePointer() != module_sp.get())
3028 new_executable_module_sp = module_sp;
3032 if (new_executable_module_sp) {
3039 "Process::%s after looping through modules, target executable is %s",
3041 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str()
3055 if (
error.Success()) {
3082 "Process::PrivateResume() m_stop_id = %u, public state: %s "
3083 "private state: %s",
3093 if (
error.Success()) {
3104 error.SetErrorString(
3105 "Process::PrivateResume PreResumeActions failed, not resuming.");
3109 if (
error.Success()) {
3112 LLDB_LOGF(log,
"Process thinks the process has resumed.");
3114 LLDB_LOGF(log,
"Process::PrivateResume() DoResume failed.");
3124 "Process::PrivateResume() asked to simulate a start & stop.");
3130 LLDB_LOGF(log,
"Process::PrivateResume() got an error \"%s\".",
3131 error.AsCString(
"<unknown error>"));
3137 return Status(
"Process is not running.");
3143 ListenerSP halt_listener_sp(
3163 halt_listener_sp,
nullptr, use_run_lock);
3185 LLDB_LOGF(log,
"Process::%s() About to stop.", __FUNCTION__);
3187 ListenerSP listener_sp(
3195 &exit_event_sp,
true, listener_sp);
3205 LLDB_LOGF(log,
"Process::%s() Process exited while waiting to stop.",
3209 exit_event_sp.reset();
3212 LLDB_LOGF(log,
"Process::%s() failed to stop, state is: %s", __FUNCTION__,
3220 "Attempt to stop the target in order to detach timed out. "
3230 EventSP exit_event_sp;
3236 if (
error.Success()) {
3239 if (!
error.Success()) {
3242 }
else if (exit_event_sp) {
3255 if (
error.Success()) {
3266 if (exit_event_sp) {
3301 bool keep_stopped =
false;
3308 if (
error.Success()) {
3309 EventSP exit_event_sp;
3325 if (
error.Success()) {
3341 if (exit_event_sp) {
3361 if (
error.Success()) {
3363 if (
error.Success())
3370 assert(signals_sp &&
"null signals_sp");
3390 bool return_value =
true;
3408 return_value =
true;
3412 return_value =
false;
3424 return_value =
true;
3430 return_value =
false;
3443 return_value =
true;
3446 return_value =
false;
3465 "Process::ShouldBroadcastEvent (%p) stopped due to an "
3466 "interrupt, state: %s",
3471 return_value =
true;
3474 bool should_resume =
false;
3486 "Process::ShouldBroadcastEvent: should_resume: %i state: "
3487 "%s was_restarted: %i report_stop_vote: %d.",
3491 switch (report_stop_vote) {
3493 return_value =
true;
3497 return_value =
false;
3501 if (!was_restarted) {
3503 "Process::ShouldBroadcastEvent (%p) Restarting process "
3510 return_value =
true;
3533 "Process::ShouldBroadcastEvent (%p) => new state: %s, last "
3534 "broadcast state: %s - %s",
3537 return_value ?
"YES" :
"NO");
3538 return return_value;
3545 LLDB_LOGF(log,
"Process::%s()%s ", __FUNCTION__,
3546 already_running ?
" already running"
3547 :
" starting private state thread");
3549 if (!is_secondary_thread && already_running)
3554 char thread_name[1024];
3555 uint32_t max_len = llvm::get_max_thread_name_length();
3556 if (max_len > 0 && max_len <= 30) {
3559 if (already_running)
3560 snprintf(thread_name,
sizeof(thread_name),
"intern-state-OV");
3562 snprintf(thread_name,
sizeof(thread_name),
"intern-state");
3564 if (already_running)
3565 snprintf(thread_name,
sizeof(thread_name),
3566 "<lldb.process.internal-state-override(pid=%" PRIu64
")>",
3569 snprintf(thread_name,
sizeof(thread_name),
3570 "<lldb.process.internal-state(pid=%" PRIu64
")>",
GetID());
3573 llvm::Expected<HostThread> private_state_thread =
3576 [
this, is_secondary_thread] {
3580 if (!private_state_thread) {
3582 llvm::toString(private_state_thread.takeError()));
3586 assert(private_state_thread->IsJoinable());
3607 "Went to stop the private state thread, but it was already invalid.");
3618 LLDB_LOGF(log,
"Process::%s (signal = %d)", __FUNCTION__, signal);
3627 LLDB_LOGF(log,
"Sending control event of type: %d.", signal);
3628 std::shared_ptr<EventDataReceipt> event_receipt_sp(
new EventDataReceipt());
3633 bool receipt_received =
false;
3635 while (!receipt_received) {
3640 if (!receipt_received) {
3657 "Private state thread already dead, no need to signal it to stop.");
3680 LLDB_LOGF(log,
"Ran next event action, result was %d.", action_result);
3682 switch (action_result) {
3708 if (should_broadcast) {
3712 "Process::%s (pid = %" PRIu64
3713 ") broadcasting new state %s (old state %s) to %s",
3716 is_hijacked ?
"hijacked" :
"public");
3723 if (!
GetTarget().GetDebugger().IsForwardingEvents() &&
3728 LLDB_LOGF(log,
"Process::%s updated m_iohandler_sync to %d",
3762 if (is_hijacked || !
GetTarget().GetDebugger().IsHandlingEvents())
3772 "Process::%s (pid = %" PRIu64
3773 ") suppressing state %s (old state %s): should_broadcast == false",
3795 bool control_only =
true;
3798 LLDB_LOGF(log,
"Process::%s (arg = %p, pid = %" PRIu64
") thread starting...",
3799 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
3801 bool exit_now =
false;
3802 bool interrupt_requested =
false;
3808 "Process::%s (arg = %p, pid = %" PRIu64
3809 ") got a control event: %d",
3810 __FUNCTION__,
static_cast<void *
>(
this),
GetID(),
3811 event_sp->GetType());
3813 switch (event_sp->GetType()) {
3819 control_only =
true;
3823 control_only =
false;
3831 "Process::%s (arg = %p, pid = %" PRIu64
3832 ") woke up with an interrupt while attaching - "
3833 "forwarding interrupt.",
3834 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
3838 "Process::%s (arg = %p, pid = %" PRIu64
3839 ") woke up with an interrupt - Halting.",
3840 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
3842 if (
error.Fail() && log)
3844 "Process::%s (arg = %p, pid = %" PRIu64
3845 ") failed to halt the process: %s",
3846 __FUNCTION__,
static_cast<void *
>(
this),
GetID(),
3853 interrupt_requested =
true;
3862 "Process::%s ignoring interrupt as we have already stopped.",
3878 if (interrupt_requested) {
3883 interrupt_requested =
false;
3886 "Process::%s interrupt_requested, but a non-stopped "
3887 "state '%s' received.",
3898 "Process::%s (arg = %p, pid = %" PRIu64
3899 ") about to exit with internal state %s...",
3900 __FUNCTION__,
static_cast<void *
>(
this),
GetID(),
3908 LLDB_LOGF(log,
"Process::%s (arg = %p, pid = %" PRIu64
") thread exiting...",
3909 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
3914 if (!is_secondary_thread)
3925 :
EventData(), m_process_wp(), m_state(state) {
3933 static ConstString g_flavor(
"Process::ProcessEventData");
3942 bool &found_valid_stopinfo) {
3943 found_valid_stopinfo =
false;
3945 ProcessSP process_sp(m_process_wp.lock());
3949 ThreadList &curr_thread_list = process_sp->GetThreadList();
3963 ThreadList not_suspended_thread_list(process_sp.get());
3964 std::vector<uint32_t> thread_index_array(num_threads);
3966 for (idx = 0; idx < num_threads; ++idx) {
3974 not_suspended_thread_list.
AddThread(thread_sp);
3975 thread_index_array[not_suspended_idx] = thread_sp->GetIndexID();
3976 not_suspended_idx++;
3985 bool still_should_stop =
false;
3993 for (idx = 0; idx < not_suspended_thread_list.
GetSize(); ++idx) {
3994 curr_thread_list = process_sp->GetThreadList();
3995 if (curr_thread_list.
GetSize() != num_threads) {
3999 "Number of threads changed from %u to %u while processing event.",
4000 num_threads, curr_thread_list.
GetSize());
4004 lldb::ThreadSP thread_sp = not_suspended_thread_list.
GetThreadAtIndex(idx);
4006 if (thread_sp->GetIndexID() != thread_index_array[idx]) {
4009 "The thread at position %u changed from %u to %u while "
4010 "processing event.",
4011 idx, thread_index_array[idx], thread_sp->GetIndexID());
4015 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
4016 if (stop_info_sp && stop_info_sp->IsValid()) {
4017 found_valid_stopinfo =
true;
4018 bool this_thread_wants_to_stop;
4019 if (stop_info_sp->GetOverrideShouldStop()) {
4020 this_thread_wants_to_stop =
4021 stop_info_sp->GetOverriddenShouldStopValue();
4023 stop_info_sp->PerformAction(event_ptr);
4031 if (stop_info_sp->HasTargetRunSinceMe()) {
4036 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
4039 if (!still_should_stop)
4040 still_should_stop = this_thread_wants_to_stop;
4044 return still_should_stop;
4048 ProcessSP process_sp(m_process_wp.lock());
4061 if (m_update_state != 1)
4064 process_sp->SetPublicState(
4071 process_sp->WillPublicStop();
4086 bool does_anybody_have_an_opinion =
false;
4087 bool still_should_stop = ShouldStop(event_ptr, does_anybody_have_an_opinion);
4089 if (GetRestarted()) {
4093 if (!still_should_stop && does_anybody_have_an_opinion) {
4098 process_sp->PrivateResume();
4101 !process_sp->StateChangedIsHijackedForSynchronousResume();
4109 if (process_sp->GetTarget().RunStopHooks())
4116 ProcessSP process_sp(m_process_wp.lock());
4119 s->
Printf(
" process = %p (pid = %" PRIu64
"), ",
4120 static_cast<void *
>(process_sp.get()), process_sp->GetID());
4140 ProcessSP process_sp;
4149 if (data ==
nullptr)
4157 if (data ==
nullptr)
4167 if (data !=
nullptr)
4175 if (data !=
nullptr)
4186 if (data !=
nullptr)
4193 const char *reason) {
4196 if (data !=
nullptr)
4201 const Event *event_ptr) {
4203 if (data ==
nullptr)
4213 if (data !=
nullptr)
4277 const StructuredDataPluginSP &plugin_sp) {
4283StructuredDataPluginSP
4287 return find_it->second;
4289 return StructuredDataPluginSP();
4298 size_t bytes_available = one_profile_data.size();
4299 if (bytes_available > 0) {
4301 LLDB_LOGF(log,
"Process::GetProfileData (buf = %p, size = %" PRIu64
")",
4302 static_cast<void *
>(buf),
static_cast<uint64_t
>(buf_size));
4303 if (bytes_available > buf_size) {
4304 memcpy(buf, one_profile_data.c_str(), buf_size);
4305 one_profile_data.erase(0, buf_size);
4306 bytes_available = buf_size;
4308 memcpy(buf, one_profile_data.c_str(), bytes_available);
4312 return bytes_available;
4320 if (bytes_available > 0) {
4322 LLDB_LOGF(log,
"Process::GetSTDOUT (buf = %p, size = %" PRIu64
")",
4323 static_cast<void *
>(buf),
static_cast<uint64_t
>(buf_size));
4324 if (bytes_available > buf_size) {
4327 bytes_available = buf_size;
4333 return bytes_available;
4339 if (bytes_available > 0) {
4341 LLDB_LOGF(log,
"Process::GetSTDERR (buf = %p, size = %" PRIu64
")",
4342 static_cast<void *
>(buf),
static_cast<uint64_t
>(buf_size));
4343 if (bytes_available > buf_size) {
4346 bytes_available = buf_size;
4352 return bytes_available;
4358 process->
AppendSTDOUT(
static_cast<const char *
>(src), src_len);
4364 :
IOHandler(process->GetTarget().GetDebugger(),
4367 m_read_file(GetInputFD(),
File::eOpenOptionReadOnly, false),
4368 m_write_file(write_fd,
File::eOpenOptionWriteOnly, false) {
4369 m_pipe.CreateNew(
false);
4375 std::lock_guard<std::mutex> guard(m_mutex);
4376 SetIsDone(!running);
4377 m_is_running = running;
4383 if (!m_read_file.IsValid() || !m_write_file.IsValid() ||
4384 !m_pipe.CanRead() || !m_pipe.CanWrite()) {
4390 const int read_fd = m_read_file.GetDescriptor();
4391 Terminal terminal(read_fd);
4392 TerminalState terminal_state(terminal,
false);
4394 llvm::consumeError(terminal.SetCanonical(
false));
4395 llvm::consumeError(terminal.SetEcho(
false));
4398 const int pipe_read_fd = m_pipe.GetReadFileDescriptor();
4402 std::lock_guard<std::mutex> guard(m_mutex);
4419 if (m_read_file.Read(&ch, n).Success() && n == 1) {
4420 if (m_write_file.Write(&ch, n).Fail() || n != 1)
4430 if (
error.Success()) {
4435 m_process->SendAsyncInterrupt();
4439 SetIsRunning(
false);
4444 std::lock_guard<std::mutex> guard(m_mutex);
4459 size_t bytes_written = 0;
4460 m_pipe.Write(&ch, 1, bytes_written);
4472 size_t bytes_written = 0;
4473 Status result = m_pipe.Write(&ch, 1, bytes_written);
4486 m_process->SendAsyncInterrupt();
4502 bool m_is_running =
false;
4508 std::make_unique<ConnectionFileDescriptor>(fd,
true));
4518 std::make_shared<IOHandlerProcessSTDIO>(
this, fd);
4530 if (io_handler_sp) {
4532 LLDB_LOGF(log,
"Process::%s pushing IO handler", __FUNCTION__);
4534 io_handler_sp->SetIsDone(
false);
4541 cancel_top_handler);
4566class RestorePlanState {
4568 RestorePlanState(lldb::ThreadPlanSP thread_plan_sp)
4569 : m_thread_plan_sp(thread_plan_sp) {
4570 if (m_thread_plan_sp) {
4571 m_private = m_thread_plan_sp->GetPrivate();
4572 m_is_controlling = m_thread_plan_sp->IsControllingPlan();
4573 m_okay_to_discard = m_thread_plan_sp->OkayToDiscard();
4577 ~RestorePlanState() { Clean(); }
4580 if (!m_already_reset && m_thread_plan_sp) {
4581 m_already_reset =
true;
4582 m_thread_plan_sp->SetPrivate(m_private);
4583 m_thread_plan_sp->SetIsControllingPlan(m_is_controlling);
4584 m_thread_plan_sp->SetOkayToDiscard(m_okay_to_discard);
4589 lldb::ThreadPlanSP m_thread_plan_sp;
4590 bool m_already_reset =
false;
4591 bool m_private =
false;
4592 bool m_is_controlling =
false;
4593 bool m_okay_to_discard =
false;
4599 const milliseconds default_one_thread_timeout(250);
4604 : default_one_thread_timeout;
4613 return std::min<microseconds>(default_one_thread_timeout,
4619 bool before_first_timeout) {
4625 if (before_first_timeout)
4629 return std::nullopt;
4634static std::optional<ExpressionResults>
4636 RestorePlanState &restorer,
const EventSP &event_sp,
4637 EventSP &event_to_broadcast_sp,
4639 bool handle_interrupts) {
4642 ThreadSP thread_sp = thread_plan_sp->GetTarget()
4645 .FindThreadByID(thread_id);
4648 "The thread on which we were running the "
4649 "expression: tid = {0}, exited while "
4650 "the expression was running.",
4655 ThreadPlanSP plan = thread_sp->GetCompletedPlan();
4656 if (plan == thread_plan_sp && plan->PlanSucceeded()) {
4657 LLDB_LOG(log,
"execution completed successfully");
4665 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
4667 stop_info_sp->ShouldNotify(event_sp.get())) {
4668 LLDB_LOG(log,
"stopped for breakpoint: {0}.", stop_info_sp->GetDescription());
4675 thread_plan_sp->SetPrivate(
false);
4676 event_to_broadcast_sp = event_sp;
4681 if (!handle_interrupts &&
4683 return std::nullopt;
4685 LLDB_LOG(log,
"thread plan did not successfully complete");
4687 event_to_broadcast_sp = event_sp;
4693 lldb::ThreadPlanSP &thread_plan_sp,
4700 if (!thread_plan_sp) {
4703 "RunThreadPlan called with empty thread plan.");
4707 if (!thread_plan_sp->ValidatePlan(
nullptr)) {
4710 "RunThreadPlan called with an invalid thread plan.");
4716 "RunThreadPlan called on wrong process.");
4721 if (thread ==
nullptr) {
4723 "RunThreadPlan called with invalid thread.");
4734 RestorePlanState thread_plan_restorer(thread_plan_sp);
4741 thread_plan_sp->SetPrivate(
false);
4747 thread_plan_sp->SetIsControllingPlan(
true);
4748 thread_plan_sp->SetOkayToDiscard(
false);
4758 "RunThreadPlan called while the private state was not stopped.");
4765 if (!selected_frame_sp) {
4768 if (!selected_frame_sp) {
4769 diagnostic_manager.
Printf(
4771 "RunThreadPlan called without a selected frame on thread %d",
4782 "RunThreadPlan called with one thread "
4783 "timeout greater than total timeout");
4787 StackID ctx_frame_id = selected_frame_sp->GetStackID();
4797 if (selected_thread_sp) {
4798 selected_tid = selected_thread_sp->GetIndexID();
4799 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
4806 lldb::ThreadPlanSP stopper_base_plan_sp;
4815 LLDB_LOGF(log,
"Running thread plan on private state thread, spinning up "
4816 "another state thread to handle the events.");
4839 thread_plan_sp,
false);
4853 ListenerSP listener_sp(
4856 lldb::EventSP event_to_broadcast_sp;
4872 "Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64
4873 " to run thread plan \"%s\".",
4874 thread_idx_id, expr_thread_id, s.
GetData());
4878 lldb::EventSP event_sp;
4881 bool before_first_timeout =
true;
4883 bool do_resume =
true;
4884 bool handle_running_event =
true;
4893 before_first_timeout =
false;
4895 LLDB_LOGF(log,
"Stop others: %u, try all: %u, before_first: %u.\n",
4897 before_first_timeout);
4904 Event *other_events = listener_sp->PeekAtNextEvent();
4905 if (other_events !=
nullptr) {
4908 "RunThreadPlan called with pending events on the queue.");
4921#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
4929 bool miss_first_event =
true;
4937 "Top of while loop: do_resume: %i handle_running_event: %i "
4938 "before_first_timeout: %i.",
4939 do_resume, handle_running_event, before_first_timeout);
4941 if (do_resume || handle_running_event) {
4948 if (!resume_error.
Success()) {
4949 diagnostic_manager.
Printf(
4951 "couldn't resume inferior the %d time: \"%s\".", num_resumes,
4962 "Process::RunThreadPlan(): didn't get any event after "
4963 "resume %" PRIu32
", exiting.",
4967 "didn't get any event after resume %" PRIu32
4978 bool restarted =
false;
4985 "Process::RunThreadPlan(): didn't get running event after "
4986 "resume %d, got %s instead (restarted: %i, do_resume: %i, "
4987 "handle_running_event: %i).",
4989 handle_running_event);
4996 const bool clear_thread_plans =
false;
4997 const bool use_run_lock =
false;
4998 Halt(clear_thread_plans, use_run_lock);
5001 diagnostic_manager.
Printf(
5003 "didn't get running event after initial resume, got %s instead.",
5010 log->
PutCString(
"Process::RunThreadPlan(): resuming succeeded.");