14#include "llvm/ADT/ScopeExit.h"
15#include "llvm/Support/ScopedPrinter.h"
16#include "llvm/Support/Threading.h"
80using namespace std::chrono;
84#define ENABLE_MEMORY_CACHING
86#ifdef ENABLE_MEMORY_CACHING
87#define DISABLE_MEM_CACHE_DEFAULT false
89#define DISABLE_MEM_CACHE_DEFAULT true
93 :
public Cloneable<ProcessOptionValueProperties, OptionValueProperties> {
109 if (
this != instance_properties)
148 "Continue tracing the parent process and detach the child.",
153 "Trace the child process and detach the parent.",
157#define LLDB_PROPERTIES_process
158#include "TargetProperties.inc"
161#define LLDB_PROPERTIES_process
162#include "TargetPropertiesEnum.inc"
166#define LLDB_PROPERTIES_process_experimental
167#include "TargetProperties.inc"
170#define LLDB_PROPERTIES_process_experimental
171#include "TargetPropertiesEnum.inc"
175 :
public Cloneable<ProcessExperimentalOptionValueProperties,
176 OptionValueProperties> {
192 if (process ==
nullptr) {
194 m_collection_sp = std::make_shared<ProcessOptionValueProperties>(
"process");
197 "thread",
"Settings specific to threads.",
true,
203 ePropertyPythonOSPluginPath,
208 std::make_unique<ProcessExperimentalProperties>();
211 "Experimental settings - setting these won't produce "
212 "errors if the setting is not present.",
219 const uint32_t idx = ePropertyDisableMemCache;
220 return GetPropertyAtIndexAs<bool>(
221 idx, g_process_properties[idx].default_uint_value != 0);
225 const uint32_t idx = ePropertyMemCacheLineSize;
226 return GetPropertyAtIndexAs<uint64_t>(
227 idx, g_process_properties[idx].default_uint_value);
232 const uint32_t idx = ePropertyExtraStartCommand;
238 const uint32_t idx = ePropertyExtraStartCommand;
243 const uint32_t idx = ePropertyPythonOSPluginPath;
244 return GetPropertyAtIndexAs<FileSpec>(idx, {});
248 const uint32_t idx = ePropertyVirtualAddressableBits;
249 return GetPropertyAtIndexAs<uint64_t>(
250 idx, g_process_properties[idx].default_uint_value);
254 const uint32_t idx = ePropertyVirtualAddressableBits;
259 const uint32_t idx = ePropertyHighmemVirtualAddressableBits;
260 return GetPropertyAtIndexAs<uint64_t>(
261 idx, g_process_properties[idx].default_uint_value);
265 const uint32_t idx = ePropertyHighmemVirtualAddressableBits;
270 const uint32_t idx = ePropertyPythonOSPluginPath;
275 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
276 return GetPropertyAtIndexAs<bool>(
277 idx, g_process_properties[idx].default_uint_value != 0);
281 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
286 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
287 return GetPropertyAtIndexAs<bool>(
288 idx, g_process_properties[idx].default_uint_value != 0);
292 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
297 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
298 return GetPropertyAtIndexAs<bool>(
299 idx, g_process_properties[idx].default_uint_value != 0);
303 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
308 const uint32_t idx = ePropertyDisableLangRuntimeUnwindPlans;
309 return GetPropertyAtIndexAs<bool>(
310 idx, g_process_properties[idx].default_uint_value != 0);
314 const uint32_t idx = ePropertyDisableLangRuntimeUnwindPlans;
320 const uint32_t idx = ePropertyDetachKeepsStopped;
321 return GetPropertyAtIndexAs<bool>(
322 idx, g_process_properties[idx].default_uint_value != 0);
326 const uint32_t idx = ePropertyDetachKeepsStopped;
331 const uint32_t idx = ePropertyWarningOptimization;
332 return GetPropertyAtIndexAs<bool>(
333 idx, g_process_properties[idx].default_uint_value != 0);
337 const uint32_t idx = ePropertyWarningUnsupportedLanguage;
338 return GetPropertyAtIndexAs<bool>(
339 idx, g_process_properties[idx].default_uint_value != 0);
343 const uint32_t idx = ePropertyStopOnExec;
344 return GetPropertyAtIndexAs<bool>(
345 idx, g_process_properties[idx].default_uint_value != 0);
349 const uint32_t idx = ePropertyUtilityExpressionTimeout;
350 uint64_t value = GetPropertyAtIndexAs<uint64_t>(
351 idx, g_process_properties[idx].default_uint_value);
352 return std::chrono::seconds(value);
356 const uint32_t idx = ePropertyInterruptTimeout;
357 uint64_t value = GetPropertyAtIndexAs<uint64_t>(
358 idx, g_process_properties[idx].default_uint_value);
359 return std::chrono::seconds(value);
363 const uint32_t idx = ePropertySteppingRunsAllThreads;
364 return GetPropertyAtIndexAs<bool>(
365 idx, g_process_properties[idx].default_uint_value != 0);
369 const bool fail_value =
true;
373 exp_property->
GetValue()->GetAsProperties();
379 .value_or(fail_value);
386 exp_property->
GetValue()->GetAsProperties();
393 const uint32_t idx = ePropertyFollowForkMode;
394 return GetPropertyAtIndexAs<FollowForkMode>(
396 g_process_properties[idx].default_uint_value));
400 llvm::StringRef plugin_name,
404 static uint32_t g_process_unique_id = 0;
408 if (!plugin_name.empty()) {
411 if (create_callback) {
412 process_sp = create_callback(target_sp, listener_sp, crash_file_path,
415 if (process_sp->CanDebug(target_sp,
true)) {
416 process_sp->m_process_unique_id = ++g_process_unique_id;
422 for (uint32_t idx = 0;
426 process_sp = create_callback(target_sp, listener_sp, crash_file_path,
429 if (process_sp->CanDebug(target_sp,
false)) {
430 process_sp->m_process_unique_id = ++g_process_unique_id;
441 static constexpr llvm::StringLiteral class_name(
"lldb.process");
454 Broadcaster((target_sp->GetDebugger().GetBroadcasterManager()),
455 Process::GetStaticBroadcasterClass().str()),
458 m_private_state_broadcaster(nullptr,
459 "lldb.process.internal_state_broadcaster"),
460 m_private_state_control_broadcaster(
461 nullptr,
"lldb.process.internal_state_control_broadcaster"),
462 m_private_state_listener_sp(
463 Listener::MakeListener(
"lldb.process.internal_state_listener")),
464 m_mod_id(), m_process_unique_id(0), m_thread_index_id(0),
465 m_thread_id_to_index_id_map(), m_exit_status(-1),
466 m_thread_list_real(*this), m_thread_list(*this), m_thread_plans(*this),
467 m_extended_thread_list(*this), m_extended_thread_stop_id(0),
468 m_queue_list(this), m_queue_list_stop_id(0),
469 m_unix_signals_sp(unix_signals_sp), m_abi_sp(), m_process_input_reader(),
470 m_stdio_communication(
"process.stdio"), m_stdio_communication_mutex(),
471 m_stdin_forward(false), m_stdout_data(), m_stderr_data(),
472 m_profile_data_comm_mutex(), m_profile_data(), m_iohandler_sync(0),
473 m_memory_cache(*this), m_allocated_memory_cache(*this),
474 m_should_detach(false), m_next_event_action_up(), m_public_run_lock(),
475 m_private_run_lock(), m_currently_handling_do_on_removals(false),
477 m_finalizing(false), m_destructing(false),
478 m_clear_thread_plans_on_stop(false), m_force_next_event_delivery(false),
479 m_last_broadcast_state(
eStateInvalid), m_destroy_in_process(false),
480 m_can_interpret_function_calls(false), m_run_thread_plan_lock(),
481 m_can_jit(eCanJITDontKnow),
486 LLDB_LOGF(log,
"%p Process::Process()",
static_cast<void *
>(
this));
524 uint64_t platform_cache_line_size =
525 target_sp->GetPlatform()->GetDefaultMemoryCacheLineSize();
526 if (!value_sp->OptionWasSet() && platform_cache_line_size != 0)
527 value_sp->SetValueAs(platform_cache_line_size);
538 LLDB_LOGF(log,
"%p Process::~Process()",
static_cast<void *
>(
this));
552 return *g_settings_ptr;
587 std::vector<Notifications> empty_notifications;
621 if (pos->baton == callbacks.
baton &&
632 std::vector<Notifications>::iterator notification_pos,
635 notification_pos != notification_end; ++notification_pos) {
636 if (notification_pos->process_state_changed)
637 notification_pos->process_state_changed(notification_pos->baton,
this,
656 std::chrono::seconds(0)) &&
676 "waited from m_iohandler_sync to change from {0}. New value is {1}.",
677 iohandler_id, *Result);
679 LLDB_LOG(log,
"timed out waiting for m_iohandler_sync to change from {0}.",
692 event_sp_ptr->reset();
700 LLDB_LOG(log,
"timeout = {0}", timeout);
705 "Process::%s returning without waiting for events; process "
706 "private and public states are already 'stopped'.",
710 if (hijack_listener_sp && use_run_lock)
718 if (event_sp_ptr && event_sp)
719 *event_sp_ptr = event_sp;
721 bool pop_process_io_handler = (hijack_listener_sp.get() !=
nullptr);
723 event_sp, stream, select_most_relevant, pop_process_io_handler);
732 if (hijack_listener_sp && use_run_lock)
741 if (hijack_listener_sp && use_run_lock)
755 bool &pop_process_io_handler) {
756 const bool handle_pop = pop_process_io_handler;
758 pop_process_io_handler =
false;
770 switch (event_state) {
778 stream->
Printf(
"Process %" PRIu64
" %s\n", process_sp->GetID(),
781 pop_process_io_handler =
true;
791 process_sp->GetStatus(*stream);
792 pop_process_io_handler =
true;
803 if (num_reasons > 0) {
806 if (num_reasons == 1) {
810 stream->
Printf(
"Process %" PRIu64
" stopped and restarted: %s\n",
812 reason ? reason :
"<UNKNOWN REASON>");
814 stream->
Printf(
"Process %" PRIu64
815 " stopped and restarted, reasons:\n",
816 process_sp->GetID());
818 for (
size_t i = 0; i < num_reasons; i++) {
822 stream->
Printf(
"\t%s\n", reason ? reason :
"<UNKNOWN REASON>");
832 ThreadList &thread_list = process_sp->GetThreadList();
833 std::lock_guard<std::recursive_mutex> guard(thread_list.
GetMutex());
838 bool prefer_curr_thread =
false;
839 if (curr_thread && curr_thread->IsValid()) {
840 curr_thread_stop_reason = curr_thread->GetStopReason();
841 switch (curr_thread_stop_reason) {
850 uint64_t signo = curr_thread->GetStopInfo()->GetValue();
851 if (process_sp->GetUnixSignals()->GetShouldStop(signo))
852 prefer_curr_thread =
true;
855 prefer_curr_thread =
true;
858 curr_thread_stop_info_sp = curr_thread->GetStopInfo();
861 if (!prefer_curr_thread) {
867 const size_t num_threads = thread_list.
GetSize();
869 for (i = 0; i < num_threads; ++i) {
871 StopReason thread_stop_reason = thread->GetStopReason();
872 switch (thread_stop_reason) {
881 uint64_t signo = thread->GetStopInfo()->GetValue();
882 if (process_sp->GetUnixSignals()->GetShouldStop(signo)) {
884 other_thread = thread;
901 other_thread = thread;
905 plan_thread = thread;
911 else if (other_thread)
914 if (curr_thread && curr_thread->IsValid())
915 thread = curr_thread;
929 Debugger &debugger = process_sp->GetTarget().GetDebugger();
931 &process_sp->GetTarget()) {
932 ThreadSP thread_sp = process_sp->GetThreadList().GetSelectedThread();
934 if (!thread_sp || !thread_sp->IsValid())
937 const bool only_threads_with_stop_reason =
true;
938 const uint32_t start_frame =
939 thread_sp->GetSelectedFrameIndex(select_most_relevant);
940 const uint32_t num_frames = 1;
941 const uint32_t num_frames_with_source = 1;
942 const bool stop_format =
true;
944 process_sp->GetStatus(*stream);
945 process_sp->GetThreadStatus(*stream, only_threads_with_stop_reason,
946 start_frame, num_frames,
947 num_frames_with_source,
949 if (curr_thread_stop_info_sp) {
952 curr_thread_stop_info_sp, &crashing_address);
958 valobj_sp->GetExpressionPath(*stream, format);
959 stream->
Printf(
" accessed 0x%" PRIx64
"\n", crashing_address);
964 process_sp->GetTarget().shared_from_this());
966 stream->
Printf(
"Target %d: (", target_idx);
968 stream->
Printf(
"Target <unknown index>: (");
970 stream->
Printf(
") stopped.\n");
975 pop_process_io_handler =
true;
980 if (handle_pop && pop_process_io_handler)
981 process_sp->PopProcessIOHandler();
1000 LLDB_LOG(log,
"timeout = {0}, event_sp)...", timeout);
1007 if (listener_sp->GetEventForBroadcasterWithType(
1013 LLDB_LOG(log,
"got no event or was interrupted.");
1016 LLDB_LOG(log,
"timeout = {0}, event_sp) => {1}", timeout, state);
1023 LLDB_LOGF(log,
"Process::%s...", __FUNCTION__);
1030 LLDB_LOGF(log,
"Process::%s (event_ptr) => %s", __FUNCTION__,
1033 LLDB_LOGF(log,
"Process::%s no events found", __FUNCTION__);
1043 LLDB_LOG(log,
"timeout = {0}, event_sp)...", timeout);
1053 LLDB_LOG(log,
"timeout = {0}, event_sp) => {1}", timeout,
1060 bool control_only) {
1062 LLDB_LOG(log,
"timeout = {0}, event_sp)...", timeout);
1096 LLDB_LOG(log,
"(plugin = {0} status = {1} ({1:x8}), description=\"{2}\")",
1103 "(plugin = {0}) ignoring exit status because state was already set "
1110 if (!exit_string.empty())
1154 "Process::SetProcessExitStatus (pid=%" PRIu64
1155 ", exited=%i, signal=%i, exit_status=%i)\n",
1156 pid, exited, signo, exit_status);
1161 ProcessSP process_sp(target_sp->GetProcessSP());
1163 llvm::StringRef signal_str =
1164 process_sp->GetUnixSignals()->GetSignalAsStringRef(signo);
1165 process_sp->SetExitStatus(exit_status, signal_str);
1183 bool clear_unused_threads =
true;
1206 size_t num_old_threads = old_thread_list.
GetSize(
false);
1207 for (
size_t i = 0; i < num_old_threads; ++i)
1240 new_thread_list = real_thread_list;
1280 bool internal,
bool condense_trivial,
1281 bool skip_unreported_plans) {
1283 strm, tid, desc_level, internal, condense_trivial, skip_unreported_plans);
1286 bool internal,
bool condense_trivial,
1287 bool skip_unreported_plans) {
1289 skip_unreported_plans);
1322 uint32_t result = 0;
1323 std::map<uint64_t, uint32_t>::iterator iterator =
1329 result = iterator->second;
1344 if (new_state_is_stopped) {
1355 LLDB_LOGF(log,
"(plugin = %s, state = %s, restarted = %i)",
1366 "(plugin = %s, state = %s) -- unlocking run lock for detach",
1371 if ((old_state_is_stopped != new_state_is_stopped)) {
1372 if (new_state_is_stopped && !restarted) {
1373 LLDB_LOGF(log,
"(plugin = %s, state = %s) -- unlocking run lock",
1386 Status error(
"Resume request failed - process still running.");
1387 LLDB_LOGF(log,
"(plugin = %s) -- TrySetRunning failed, not resuming.",
1392 if (!
error.Success()) {
1401 LLDB_LOGF(log,
"Process::ResumeSynchronous -- locking run lock");
1403 Status error(
"Resume request failed - process still running.");
1404 LLDB_LOGF(log,
"Process::Resume: -- TrySetRunning failed, not resuming.");
1413 if (
error.Success()) {
1417 const bool must_be_alive =
1420 error.SetErrorStringWithFormat(
1421 "process not in stopped state after synchronous resume: %s",
1437 if (!hijacking_name.starts_with(
"lldb.internal"))
1465 bool state_changed =
false;
1474 state_changed = old_state != new_state;
1478 if (old_state_is_stopped != new_state_is_stopped) {
1479 if (new_state_is_stopped)
1485 if (state_changed) {
1509 LLDB_LOGF(log,
"(plugin = %s, state = %s, stop_id = %u",
1516 LLDB_LOGF(log,
"(plugin = %s, state = %s) state didn't change. Ignoring...",
1538 std::vector<LanguageRuntime *> language_runtimes;
1541 return language_runtimes;
1551 language_runtimes.emplace_back(runtime);
1554 return language_runtimes;
1564 LanguageRuntimeCollection::iterator pos;
1571 runtime = runtime_sp.get();
1573 runtime = pos->second.get();
1599 if (runtime->CouldHaveDynamicValue(in_value))
1629 if (
error.Success())
1639 if (bp_site_sp->IsEnabled())
1642 error.SetErrorStringWithFormat(
"invalid breakpoint site ID: %" PRIu64,
1653 if (!bp_site_sp->IsEnabled())
1656 error.SetErrorStringWithFormat(
"invalid breakpoint site ID: %" PRIu64,
1664 bool use_hardware) {
1667 bool show_error =
true;
1690 constituent->SetIsIndirect(
false);
1692 if (constituent->ShouldResolveIndirectFunctions()) {
1698 if (!
error.Success() && show_error) {
1700 "warning: failed to resolve indirect function at 0x%" PRIx64
1701 " for breakpoint %i.%i: %s\n",
1703 constituent->GetBreakpoint().GetID(), constituent->GetID(),
1704 error.AsCString() ?
error.AsCString() :
"unknown error");
1707 Address resolved_address(load_addr);
1709 constituent->SetIsIndirect(
true);
1711 load_addr = constituent->GetAddress().GetOpcodeLoadAddress(&
GetTarget());
1713 load_addr = constituent->GetAddress().GetOpcodeLoadAddress(&
GetTarget());
1724 bp_site_sp->AddConstituent(constituent);
1725 constituent->SetBreakpointSite(bp_site_sp);
1726 return bp_site_sp->GetID();
1732 if (
error.Success()) {
1733 constituent->SetBreakpointSite(bp_site_sp);
1736 if (show_error || use_hardware) {
1739 "warning: failed to set breakpoint site at 0x%" PRIx64
1740 " for breakpoint %i.%i: %s\n",
1741 load_addr, constituent->GetBreakpoint().GetID(),
1742 constituent->GetID(),
1743 error.AsCString() ?
error.AsCString() :
"unknown error");
1756 uint32_t num_constituents =
1757 bp_site_sp->RemoveConstituent(constituent_id, constituent_loc_id);
1758 if (num_constituents == 0) {
1767 uint8_t *buf)
const {
1768 size_t bytes_removed = 0;
1772 bp_sites_in_range)) {
1773 bp_sites_in_range.
ForEach([bp_addr, size,
1776 addr_t intersect_addr;
1777 size_t intersect_size;
1778 size_t opcode_offset;
1779 if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr,
1780 &intersect_size, &opcode_offset)) {
1781 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1782 assert(bp_addr < intersect_addr + intersect_size &&
1783 intersect_addr + intersect_size <= bp_addr + size);
1784 assert(opcode_offset + intersect_size <= bp_site->GetByteSize());
1785 size_t buf_offset = intersect_addr - bp_addr;
1786 ::memcpy(buf + buf_offset,
1787 bp_site->GetSavedOpcodeBytes() + opcode_offset,
1793 return bytes_removed;
1799 return platform_sp->GetSoftwareBreakpointTrapOpcode(
GetTarget(), bp_site);
1805 assert(bp_site !=
nullptr);
1809 log,
"Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64,
1810 bp_site->
GetID(), (uint64_t)bp_addr);
1814 "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1815 " -- already enabled",
1816 bp_site->
GetID(), (uint64_t)bp_addr);
1821 error.SetErrorString(
"BreakpointSite contains an invalid load address.");
1828 if (bp_opcode_size == 0) {
1829 error.SetErrorStringWithFormat(
"Process::GetSoftwareBreakpointTrapOpcode() "
1830 "returned zero, unable to get breakpoint "
1831 "trap for address 0x%" PRIx64,
1836 if (bp_opcode_bytes ==
nullptr) {
1837 error.SetErrorString(
1838 "BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1844 error) == bp_opcode_size) {
1848 uint8_t verify_bp_opcode_bytes[64];
1849 if (
DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size,
1850 error) == bp_opcode_size) {
1851 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes,
1852 bp_opcode_size) == 0) {
1856 "Process::EnableSoftwareBreakpoint (site_id = %d) "
1857 "addr = 0x%" PRIx64
" -- SUCCESS",
1858 bp_site->
GetID(), (uint64_t)bp_addr);
1860 error.SetErrorString(
1861 "failed to verify the breakpoint trap in memory.");
1863 error.SetErrorString(
1864 "Unable to read memory to verify breakpoint trap.");
1866 error.SetErrorString(
"Unable to write breakpoint trap to memory.");
1868 error.SetErrorString(
"Unable to read memory at breakpoint address.");
1870 if (log &&
error.Fail())
1873 "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1875 bp_site->
GetID(), (uint64_t)bp_addr,
error.AsCString());
1881 assert(bp_site !=
nullptr);
1886 "Process::DisableSoftwareBreakpoint (breakID = %" PRIu64
1887 ") addr = 0x%" PRIx64,
1888 breakID, (uint64_t)bp_addr);
1891 error.SetErrorString(
"Breakpoint site is a hardware breakpoint.");
1893 const size_t break_op_size = bp_site->
GetByteSize();
1895 if (break_op_size > 0) {
1897 uint8_t curr_break_op[8];
1898 assert(break_op_size <=
sizeof(curr_break_op));
1899 bool break_op_found =
false;
1904 bool verify =
false;
1906 if (::memcmp(curr_break_op, break_op, break_op_size) == 0) {
1907 break_op_found =
true;
1911 break_op_size,
error) == break_op_size) {
1914 error.SetErrorString(
1915 "Memory write failed when restoring original opcode.");
1917 error.SetErrorString(
1918 "Original breakpoint trap is no longer in memory.");
1925 uint8_t verify_opcode[8];
1926 assert(break_op_size <
sizeof(verify_opcode));
1932 break_op_size) == 0) {
1936 "Process::DisableSoftwareBreakpoint (site_id = %d) "
1937 "addr = 0x%" PRIx64
" -- SUCCESS",
1938 bp_site->
GetID(), (uint64_t)bp_addr);
1942 error.SetErrorString(
"Failed to restore original opcode.");
1945 error.SetErrorString(
"Failed to read memory to verify that "
1946 "breakpoint trap was restored.");
1949 error.SetErrorString(
1950 "Unable to read memory that should contain the breakpoint trap.");
1955 "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1956 " -- already disabled",
1957 bp_site->
GetID(), (uint64_t)bp_addr);
1963 "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64
1965 bp_site->
GetID(), (uint64_t)bp_addr,
error.AsCString());
1975 addr = abi_sp->FixAnyAddress(addr);
1979#if defined(VERIFY_MEMORY_READS)
1991 std::string verify_buf(size,
'\0');
1992 assert(verify_buf.size() == size);
1993 const size_t cache_bytes_read =
1996 const size_t verify_bytes_read =
1998 verify_buf.size(), verify_error);
1999 assert(cache_bytes_read == verify_bytes_read);
2000 assert(memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
2002 return cache_bytes_read;
2018 const uint8_t *buf,
size_t size,
2020 size_t max_matches) {
2022 assert(buf !=
nullptr);
2024 assert(alignment > 0);
2025 assert(max_matches > 0);
2028 assert(start_addr < end_addr);
2030 lldb::addr_t start = llvm::alignTo(start_addr, alignment);
2031 while (matches.size() < max_matches && (start + size) < end_addr) {
2036 if (found_addr % alignment) {
2039 start = llvm::alignTo(start + 1, alignment);
2043 matches.emplace_back(found_addr, size);
2044 start = found_addr + alignment;
2050 size_t alignment,
size_t max_matches,
2053 if (buf ==
nullptr) {
2054 error.SetErrorString(
"buffer is null");
2058 error.SetErrorString(
"buffer size is zero");
2061 if (ranges.empty()) {
2062 error.SetErrorString(
"empty ranges");
2065 if (alignment == 0) {
2066 error.SetErrorString(
"alignment must be greater than zero");
2069 if (max_matches == 0) {
2070 error.SetErrorString(
"max_matches must be greater than zero");
2074 int resolved_ranges = 0;
2076 for (
size_t i = 0; i < ranges.size(); ++i) {
2077 if (matches.size() >= max_matches)
2090 DoFindInMemory(start_addr, end_addr, buf, size, matches, alignment,
2094 if (resolved_ranges > 0)
2097 error.SetErrorString(
"unable to resolve any ranges");
2105 if (buf ==
nullptr) {
2106 error.SetErrorString(
"buffer is null");
2110 error.SetErrorString(
"buffer size is zero");
2114 error.SetErrorString(
"range is invalid");
2117 if (alignment == 0) {
2118 error.SetErrorString(
"alignment must be greater than zero");
2126 error.SetErrorString(
"range load address is invalid");
2132 DoFindInMemory(start_addr, end_addr, buf, size, matches, alignment, 1);
2133 if (matches.empty())
2137 return matches[0].GetBaseAddress().GetLoadAddress(&target);
2149 out_str.append(buf, length);
2152 if (length ==
sizeof(buf) - 1)
2153 curr_addr += length;
2157 return out_str.size();
2165 size_t total_cstr_len = 0;
2166 if (dst && dst_max_len) {
2167 result_error.
Clear();
2169 memset(dst, 0, dst_max_len);
2173 size_t bytes_left = dst_max_len - 1;
2174 char *curr_dst = dst;
2176 while (bytes_left > 0) {
2177 addr_t cache_line_bytes_left =
2178 cache_line_size - (curr_addr % cache_line_size);
2180 std::min<addr_t>(bytes_left, cache_line_bytes_left);
2181 size_t bytes_read =
ReadMemory(curr_addr, curr_dst, bytes_to_read,
error);
2183 if (bytes_read == 0) {
2184 result_error =
error;
2185 dst[total_cstr_len] =
'\0';
2188 const size_t len = strlen(curr_dst);
2190 total_cstr_len += len;
2192 if (len < bytes_to_read)
2195 curr_dst += bytes_read;
2196 curr_addr += bytes_read;
2197 bytes_left -= bytes_read;
2203 result_error.
Clear();
2205 return total_cstr_len;
2213 addr = abi_sp->FixAnyAddress(addr);
2215 if (buf ==
nullptr || size == 0)
2218 size_t bytes_read = 0;
2219 uint8_t *bytes = (uint8_t *)buf;
2221 while (bytes_read < size) {
2222 const size_t curr_size = size - bytes_read;
2223 const size_t curr_bytes_read =
2225 bytes_read += curr_bytes_read;
2226 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2238 size_t integer_byte_size,
2239 uint64_t fail_value,
2249 size_t integer_byte_size,
2271 if (addr_byte_size <= 4)
2272 scalar = (uint32_t)ptr_value;
2281 size_t bytes_written = 0;
2282 const uint8_t *bytes = (
const uint8_t *)buf;
2284 while (bytes_written < size) {
2285 const size_t curr_size = size - bytes_written;
2287 addr + bytes_written, bytes + bytes_written, curr_size,
error);
2288 bytes_written += curr_bytes_written;
2289 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2292 return bytes_written;
2298 addr = abi_sp->FixAnyAddress(addr);
2300#if defined(ENABLE_MEMORY_CACHING)
2304 if (buf ==
nullptr || size == 0)
2318 if (bp_sites_in_range.
IsEmpty())
2321 const uint8_t *ubuf = (
const uint8_t *)buf;
2322 uint64_t bytes_written = 0;
2324 bp_sites_in_range.
ForEach([
this, addr, size, &bytes_written, &ubuf,
2333 size_t intersect_size;
2334 size_t opcode_offset;
2336 addr, size, &intersect_addr, &intersect_size, &opcode_offset);
2339 assert(addr <= intersect_addr && intersect_addr < addr + size);
2340 assert(addr < intersect_addr + intersect_size &&
2341 intersect_addr + intersect_size <= addr + size);
2342 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2345 const addr_t curr_addr = addr + bytes_written;
2346 if (intersect_addr > curr_addr) {
2349 size_t curr_size = intersect_addr - curr_addr;
2350 size_t curr_bytes_written =
2352 bytes_written += curr_bytes_written;
2353 if (curr_bytes_written != curr_size) {
2357 if (
error.Success())
2358 error.SetErrorToGenericError();
2365 bytes_written += intersect_size;
2369 if (bytes_written < size)
2372 size - bytes_written,
error);
2374 return bytes_written;
2381 if (byte_size > 0) {
2383 const size_t mem_size =
2388 error.SetErrorString(
"failed to get scalar as memory data");
2390 error.SetErrorString(
"invalid scalar value");
2396 bool is_signed,
Scalar &scalar,
2399 if (byte_size == 0) {
2400 error.SetErrorString(
"byte size is zero");
2401 }
else if (byte_size & (byte_size - 1)) {
2402 error.SetErrorStringWithFormat(
"byte size %u is not a power of 2",
2404 }
else if (byte_size <=
sizeof(uval)) {
2406 if (bytes_read == byte_size) {
2411 scalar = data.
GetMaxU32(&offset, byte_size);
2413 scalar = data.
GetMaxU64(&offset, byte_size);
2419 error.SetErrorStringWithFormat(
2420 "byte size of %u is too large for integer scalar type", byte_size);
2427 for (
const auto &
Entry : entries) {
2430 if (!
error.Success())
2436#define USE_ALLOCATE_MEMORY_CACHE 1
2440 error.SetErrorToGenericError();
2444#if defined(USE_ALLOCATE_MEMORY_CACHE)
2450 "Process::AllocateMemory(size=%" PRIu64
2451 ", permissions=%s) => 0x%16.16" PRIx64
2452 " (m_stop_id = %u m_memory_id = %u)",
2456 return allocated_addr;
2463 if (
error.Success()) {
2464 std::string buffer(size, 0);
2476 8, ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2482 "Process::%s pid %" PRIu64
2483 " allocation test passed, CanJIT () is true",
2484 __FUNCTION__,
GetID());
2488 "Process::%s pid %" PRIu64
2489 " allocation test failed, CanJIT () is false: %s",
2510#if defined(USE_ALLOCATE_MEMORY_CACHE)
2512 error.SetErrorStringWithFormat(
2513 "deallocation of memory at 0x%" PRIx64
" failed.", (uint64_t)ptr);
2520 "Process::DeallocateMemory(addr=0x%16.16" PRIx64
2521 ") => err = %s (m_stop_id = %u, m_memory_id = %u)",
2530 return *subclass_override;
2532 bool reported_after =
true;
2535 return reported_after;
2538 if (triple.isMIPS() || triple.isPPC64() || triple.isRISCV() ||
2539 triple.isAArch64() || triple.isArmMClass() || triple.isARM())
2540 reported_after =
false;
2542 return reported_after;
2547 size_t size_to_read) {
2551 "Process::ReadModuleFromMemory reading %s binary from memory",
2557 std::unique_ptr<Progress> progress_up;
2562 progress_up = std::make_unique<Progress>(
2565 ObjectFile *objfile = module_sp->GetMemoryObjectFile(
2566 shared_from_this(), header_addr,
error, size_to_read);
2574 uint32_t &permissions) {
2578 if (!
error.Success())
2591 error.SetErrorString(
"watchpoints are not supported");
2597 error.SetErrorString(
"watchpoints are not supported");
2636 LaunchPrivate(launch_info, state_after_launch, first_stop_event_sp);
2658 if (launch_info.
GetFlags().
Test(eLaunchFlagStopAtEntry))
2690 error.SetErrorString(
"executable module does not exist");
2716 std::string local_exec_file_path = exe_spec_to_use.
GetPath();
2717 return Status(
"file doesn't exist: '%s'", local_exec_file_path.c_str());
2720 const bool restarted =
false;
2729 error.SetErrorString(
"failed to acquire process run lock");
2735 const char *error_string =
error.AsCString();
2736 if (error_string ==
nullptr)
2737 error_string =
"launch failed";
2750 error.SetErrorString(
"failed to catch stop after launch");
2792 return Status(
"Unexpected process state after the launch: %s, expected %s, "
2801 if (
error.Success()) {
2836 LLDB_LOGF(log,
"Process::Halt() failed to stop, state is: %s",
2838 error.SetErrorString(
2839 "Did not get stopped event after loading the core file.");
2877 uint32_t exec_count)
2882 "Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32,
2883 __FUNCTION__,
static_cast<void *
>(process), exec_count);
2892 "Process::AttachCompletionHandler::%s called with state %s (%d)",
2897 return eEventActionSuccess;
2901 return eEventActionRetry;
2912 if (m_exec_count > 0) {
2916 "Process::AttachCompletionHandler::%s state %s: reduced "
2917 "remaining exec count to %" PRIu32
", requesting resume",
2921 return eEventActionRetry;
2924 "Process::AttachCompletionHandler::%s state %s: no more "
2925 "execs expected to start, continuing with attach",
2929 return eEventActionSuccess;
2940 return eEventActionExit;
2945 return eEventActionSuccess;
2954 return m_listener_sp;
2968 bool wait_for_launch) {
2989 sizeof(process_name))) {
2992 if (wait_for_launch) {
2994 if (
error.Success()) {
2997 const bool restarted =
false;
3003 error.SetErrorString(
"failed to acquire process run lock");
3009 if (
error.AsCString() ==
nullptr)
3010 error.SetErrorString(
"attach failed");
3029 platform_sp->FindProcesses(match_info, process_infos);
3030 const uint32_t num_matches = process_infos.size();
3031 if (num_matches == 1) {
3032 attach_pid = process_infos[0].GetProcessID();
3036 process_name,
sizeof(process_name));
3037 if (num_matches > 1) {
3040 for (
size_t i = 0; i < num_matches; i++) {
3041 process_infos[i].DumpAsTableRow(
3042 s, platform_sp->GetUserIDResolver(),
true,
false);
3044 error.SetErrorStringWithFormat(
3045 "more than one process named %s:\n%s", process_name,
3048 error.SetErrorStringWithFormat(
3049 "could not find a process named %s", process_name);
3052 error.SetErrorString(
3053 "invalid platform, can't find processes by name");
3058 error.SetErrorString(
"invalid process name");
3064 if (
error.Success()) {
3069 const bool restarted =
false;
3074 error.SetErrorString(
"failed to acquire process run lock");
3077 if (
error.Success()) {
3085 const char *error_string =
error.AsCString();
3086 if (error_string ==
nullptr)
3087 error_string =
"attach failed";
3098 LLDB_LOGF(log,
"Process::%s()", __FUNCTION__);
3107 "Process::{0} replacing process architecture with DidAttach() "
3108 "architecture: \"{1}\"",
3109 __FUNCTION__, process_arch.
GetTriple().getTriple());
3117 assert(platform_sp);
3121 if (target_arch.
IsValid() && !platform_sp->IsCompatibleArchitecture(
3122 target_arch, process_host_arch,
3126 target_arch, process_host_arch, &platform_arch);
3131 "switching platform to {0} and architecture to {1} based on "
3133 platform_sp->GetName(), platform_arch.
GetTriple().getTriple());
3135 }
else if (!process_arch.
IsValid()) {
3145 "Process::%s switching architecture to %s based on info "
3146 "the platform retrieved for pid %" PRIu64,
3147 __FUNCTION__, process_arch.
GetTriple().getTriple().c_str(),
3167 "after DynamicLoader::DidAttach(), target "
3168 "executable is {0} (using {1} plugin)",
3169 exe_module_sp ? exe_module_sp->GetFileSpec() :
FileSpec(),
3177 if (system_runtime) {
3182 "after SystemRuntime::DidAttach(), target "
3183 "executable is {0} (using {1} plugin)",
3184 exe_module_sp ? exe_module_sp->GetFileSpec() :
FileSpec(),
3202 if (module_sp && module_sp->IsExecutable()) {
3203 if (
GetTarget().GetExecutableModulePointer() != module_sp.get())
3204 new_executable_module_sp = module_sp;
3208 if (new_executable_module_sp) {
3215 "Process::%s after looping through modules, target executable is %s",
3217 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str()
3234 if (
error.Success()) {
3261 "Process::PrivateResume() m_stop_id = %u, public state: %s "
3262 "private state: %s",
3271 if (!
GetModID().IsLastResumeForUserExpression())
3276 if (
error.Success()) {
3287 error.SetErrorString(
3288 "Process::PrivateResume PreResumeActions failed, not resuming.");
3292 if (
error.Success()) {
3295 LLDB_LOGF(log,
"Process thinks the process has resumed.");
3297 LLDB_LOGF(log,
"Process::PrivateResume() DoResume failed.");
3307 "Process::PrivateResume() asked to simulate a start & stop.");
3313 LLDB_LOGF(log,
"Process::PrivateResume() got an error \"%s\".",
3314 error.AsCString(
"<unknown error>"));
3320 return Status(
"Process is not running.");
3349 halt_listener_sp,
nullptr,
3350 use_run_lock, select_most_relevant);
3364 const uint8_t *buf,
size_t size) {
3365 const size_t region_size = high - low;
3367 if (region_size < size)
3370 std::vector<size_t> bad_char_heuristic(256, size);
3373 for (
size_t idx = 0; idx < size - 1; idx++) {
3374 decltype(bad_char_heuristic)::size_type bcu_idx = buf[idx];
3375 bad_char_heuristic[bcu_idx] = size - idx - 1;
3377 for (
size_t s = 0; s <= (region_size - size);) {
3378 int64_t j = size - 1;
3379 while (j >= 0 && buf[j] == iterator[s + j])
3384 s += bad_char_heuristic[iterator[s + size - 1]];
3399 LLDB_LOGF(log,
"Process::%s() About to stop.", __FUNCTION__);
3409 &exit_event_sp,
true, listener_sp);
3419 LLDB_LOGF(log,
"Process::%s() Process exited while waiting to stop.",
3423 exit_event_sp.reset();
3426 LLDB_LOGF(log,
"Process::%s() failed to stop, state is: %s", __FUNCTION__,
3434 "Attempt to stop the target in order to detach timed out. "
3450 if (
error.Success()) {
3453 if (!
error.Success()) {
3456 }
else if (exit_event_sp) {
3469 if (
error.Success()) {
3480 if (exit_event_sp) {
3515 bool keep_stopped =
false;
3522 if (
error.Success()) {
3539 if (
error.Success()) {
3558 if (exit_event_sp) {
3578 if (
error.Success()) {
3580 if (
error.Success())
3587 assert(signals_sp &&
"null signals_sp");
3607 bool return_value =
true;
3625 return_value =
true;
3629 return_value =
false;
3641 return_value =
true;
3647 return_value =
false;
3660 return_value =
true;
3663 return_value =
false;
3682 "Process::ShouldBroadcastEvent (%p) stopped due to an "
3683 "interrupt, state: %s",
3688 return_value =
true;
3691 bool should_resume =
false;
3703 "Process::ShouldBroadcastEvent: should_resume: %i state: "
3704 "%s was_restarted: %i report_stop_vote: %d.",
3708 switch (report_stop_vote) {
3710 return_value =
true;
3714 return_value =
false;
3718 if (!was_restarted) {
3720 "Process::ShouldBroadcastEvent (%p) Restarting process "
3727 return_value =
true;
3750 "Process::ShouldBroadcastEvent (%p) => new state: %s, last "
3751 "broadcast state: %s - %s",
3754 return_value ?
"YES" :
"NO");
3755 return return_value;
3762 LLDB_LOGF(log,
"Process::%s()%s ", __FUNCTION__,
3763 already_running ?
" already running"
3764 :
" starting private state thread");
3766 if (!is_secondary_thread && already_running)
3771 char thread_name[1024];
3772 uint32_t max_len = llvm::get_max_thread_name_length();
3773 if (max_len > 0 && max_len <= 30) {
3776 if (already_running)
3777 snprintf(thread_name,
sizeof(thread_name),
"intern-state-OV");
3779 snprintf(thread_name,
sizeof(thread_name),
"intern-state");
3781 if (already_running)
3782 snprintf(thread_name,
sizeof(thread_name),
3783 "<lldb.process.internal-state-override(pid=%" PRIu64
")>",
3786 snprintf(thread_name,
sizeof(thread_name),
3787 "<lldb.process.internal-state(pid=%" PRIu64
")>",
GetID());
3790 llvm::Expected<HostThread> private_state_thread =
3793 [
this, is_secondary_thread] {
3797 if (!private_state_thread) {
3799 "failed to launch host thread: {0}");
3803 assert(private_state_thread->IsJoinable());
3824 "Went to stop the private state thread, but it was already invalid.");
3835 LLDB_LOGF(log,
"Process::%s (signal = %d)", __FUNCTION__, signal);
3844 LLDB_LOGF(log,
"Sending control event of type: %d.", signal);
3845 std::shared_ptr<EventDataReceipt> event_receipt_sp(
new EventDataReceipt());
3850 bool receipt_received =
false;
3852 while (!receipt_received) {
3857 if (!receipt_received) {
3874 "Private state thread already dead, no need to signal it to stop.");
3879 if (thread !=
nullptr)
3901 LLDB_LOGF(log,
"Ran next event action, result was %d.", action_result);
3903 switch (action_result) {
3929 if (should_broadcast) {
3933 "Process::%s (pid = %" PRIu64
3934 ") broadcasting new state %s (old state %s) to %s",
3937 is_hijacked ?
"hijacked" :
"public");
3944 if (!
GetTarget().GetDebugger().IsForwardingEvents() &&
3949 LLDB_LOGF(log,
"Process::%s updated m_iohandler_sync to %d",
3983 if (is_hijacked || !
GetTarget().GetDebugger().IsHandlingEvents())
3993 "Process::%s (pid = %" PRIu64
3994 ") suppressing state %s (old state %s): should_broadcast == false",
4016 bool control_only =
true;
4019 LLDB_LOGF(log,
"Process::%s (arg = %p, pid = %" PRIu64
") thread starting...",
4020 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
4022 bool exit_now =
false;
4023 bool interrupt_requested =
false;
4029 "Process::%s (arg = %p, pid = %" PRIu64
4030 ") got a control event: %d",
4031 __FUNCTION__,
static_cast<void *
>(
this),
GetID(),
4032 event_sp->GetType());
4034 switch (event_sp->GetType()) {
4040 control_only =
true;
4044 control_only =
false;
4052 "Process::%s (arg = %p, pid = %" PRIu64
4053 ") woke up with an interrupt while attaching - "
4054 "forwarding interrupt.",
4055 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
4066 "Process::%s (arg = %p, pid = %" PRIu64
4067 ") woke up with an interrupt - Halting.",
4068 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
4070 if (
error.Fail() && log)
4072 "Process::%s (arg = %p, pid = %" PRIu64
4073 ") failed to halt the process: %s",
4074 __FUNCTION__,
static_cast<void *
>(
this),
GetID(),
4081 interrupt_requested =
true;
4090 "Process::%s ignoring interrupt as we have already stopped.",
4106 if (interrupt_requested) {
4116 interrupt_requested =
false;
4119 "Process::%s interrupt_requested, but a non-stopped "
4120 "state '%s' received.",
4131 "Process::%s (arg = %p, pid = %" PRIu64
4132 ") about to exit with internal state %s...",
4133 __FUNCTION__,
static_cast<void *
>(
this),
GetID(),
4141 LLDB_LOGF(log,
"Process::%s (arg = %p, pid = %" PRIu64
") thread exiting...",
4142 __FUNCTION__,
static_cast<void *
>(
this),
GetID());
4147 if (!is_secondary_thread)
4158 :
EventData(), m_process_wp(), m_state(state) {
4166 return "Process::ProcessEventData";
4174 bool &found_valid_stopinfo) {
4175 found_valid_stopinfo =
false;
4177 ProcessSP process_sp(m_process_wp.lock());
4181 ThreadList &curr_thread_list = process_sp->GetThreadList();
4182 uint32_t num_threads = curr_thread_list.
GetSize();
4194 std::vector<std::pair<ThreadSP, size_t>> not_suspended_threads;
4195 for (uint32_t idx = 0; idx < num_threads; ++idx) {
4203 not_suspended_threads.emplace_back(thread_sp, thread_sp->GetIndexID());
4211 bool still_should_stop =
false;
4219 for (
auto [thread_sp, thread_index] : not_suspended_threads) {
4220 if (curr_thread_list.
GetSize() != num_threads) {
4224 "Number of threads changed from %u to %u while processing event.",
4225 num_threads, curr_thread_list.
GetSize());
4229 if (thread_sp->GetIndexID() != thread_index) {
4232 "The thread {0} changed from {1} to {2} while processing event.",
4233 thread_sp.get(), thread_index, thread_sp->GetIndexID());
4237 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
4238 if (stop_info_sp && stop_info_sp->IsValid()) {
4239 found_valid_stopinfo =
true;
4240 bool this_thread_wants_to_stop;
4241 if (stop_info_sp->GetOverrideShouldStop()) {
4242 this_thread_wants_to_stop =
4243 stop_info_sp->GetOverriddenShouldStopValue();
4245 stop_info_sp->PerformAction(event_ptr);
4253 if (stop_info_sp->HasTargetRunSinceMe()) {
4258 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
4261 if (!still_should_stop)
4262 still_should_stop = this_thread_wants_to_stop;
4266 return still_should_stop;
4277 return m_update_state == 1;
4285 ProcessSP process_sp(m_process_wp.lock());
4298 if (m_update_state != 1)
4301 process_sp->SetPublicState(
4308 process_sp->WillPublicStop();
4323 bool does_anybody_have_an_opinion =
false;
4324 bool still_should_stop = ShouldStop(event_ptr, does_anybody_have_an_opinion);
4326 if (GetRestarted()) {
4330 if (!still_should_stop && does_anybody_have_an_opinion) {
4335 process_sp->PrivateResume();
4338 !process_sp->StateChangedIsHijackedForSynchronousResume();
4346 if (process_sp->GetTarget().RunStopHooks())
4353 ProcessSP process_sp(m_process_wp.lock());
4356 s->
Printf(
" process = %p (pid = %" PRIu64
"), ",
4357 static_cast<void *
>(process_sp.get()), process_sp->GetID());
4386 if (data ==
nullptr)
4394 if (data ==
nullptr)
4404 if (data !=
nullptr)
4412 if (data !=
nullptr)
4423 if (data !=
nullptr)
4430 const char *reason) {
4433 if (data !=
nullptr)
4438 const Event *event_ptr) {
4440 if (data ==
nullptr)
4450 if (data !=
nullptr)
4493 auto event_data_sp =
4494 std::make_shared<ProcessEventData>(shared_from_this(),
GetState());
4495 return std::make_shared<Event>(event_type, event_data_sp);
4521 auto data_sp = std::make_shared<EventDataStructuredData>(
4522 shared_from_this(), object_sp, plugin_sp);
4530 return find_it->second;
4541 size_t bytes_available = one_profile_data.size();
4542 if (bytes_available > 0) {
4544 LLDB_LOGF(log,
"Process::GetProfileData (buf = %p, size = %" PRIu64
")",
4545 static_cast<void *
>(buf),
static_cast<uint64_t
>(buf_size));
4546 if (bytes_available > buf_size) {
4547 memcpy(buf, one_profile_data.c_str(), buf_size);
4548 one_profile_data.erase(0, buf_size);
4549 bytes_available = buf_size;
4551 memcpy(buf, one_profile_data.c_str(), bytes_available);
4555 return bytes_available;
4563 if (bytes_available > 0) {
4565 LLDB_LOGF(log,
"Process::GetSTDOUT (buf = %p, size = %" PRIu64
")",
4566 static_cast<void *
>(buf),
static_cast<uint64_t
>(buf_size));
4567 if (bytes_available > buf_size) {
4570 bytes_available = buf_size;
4576 return bytes_available;
4582 if (bytes_available > 0) {
4584 LLDB_LOGF(log,
"Process::GetSTDERR (buf = %p, size = %" PRIu64
")",
4585 static_cast<void *
>(buf),
static_cast<uint64_t
>(buf_size));
4586 if (bytes_available > buf_size) {
4589 bytes_available = buf_size;
4595 return bytes_available;
4601 process->
AppendSTDOUT(
static_cast<const char *
>(src), src_len);
4607 :
IOHandler(process->GetTarget().GetDebugger(),
4610 m_read_file(GetInputFD(),
File::eOpenOptionReadOnly, false),
4611 m_write_file(write_fd,
File::eOpenOptionWriteOnly, false) {
4612 m_pipe.CreateNew(
false);
4618 std::lock_guard<std::mutex> guard(m_mutex);
4619 SetIsDone(!running);
4620 m_is_running = running;
4626 if (!m_read_file.IsValid() || !m_write_file.IsValid() ||
4627 !m_pipe.CanRead() || !m_pipe.CanWrite()) {
4633 const int read_fd = m_read_file.GetDescriptor();
4638 llvm::consumeError(terminal.
SetEcho(
false));
4641 const int pipe_read_fd = m_pipe.GetReadFileDescriptor();
4645 std::lock_guard<std::mutex> guard(m_mutex);
4662 if (m_read_file.Read(&ch, n).Success() && n == 1) {
4663 if (m_write_file.Write(&ch, n).Fail() || n != 1)
4673 if (
error.Success()) {
4678 m_process->SendAsyncInterrupt();
4682 SetIsRunning(
false);
4687 std::lock_guard<std::mutex> guard(m_mutex);
4702 size_t bytes_written = 0;
4703 m_pipe.Write(&ch, 1, bytes_written);
4715 size_t bytes_written = 0;
4716 Status result = m_pipe.Write(&ch, 1, bytes_written);
4729 m_process->SendAsyncInterrupt();
4745 bool m_is_running =
false;
4751 std::make_unique<ConnectionFileDescriptor>(fd,
true));
4762 std::make_shared<IOHandlerProcessSTDIO>(
this, fd);
4778 if (io_handler_sp) {
4780 LLDB_LOGF(log,
"Process::%s pushing IO handler", __FUNCTION__);
4782 io_handler_sp->SetIsDone(
false);
4789 cancel_top_handler);
4815class RestorePlanState {
4818 : m_thread_plan_sp(thread_plan_sp) {
4819 if (m_thread_plan_sp) {
4820 m_private = m_thread_plan_sp->GetPrivate();
4821 m_is_controlling = m_thread_plan_sp->IsControllingPlan();
4822 m_okay_to_discard = m_thread_plan_sp->OkayToDiscard();
4826 ~RestorePlanState() { Clean(); }
4829 if (!m_already_reset && m_thread_plan_sp) {
4830 m_already_reset =
true;
4831 m_thread_plan_sp->SetPrivate(m_private);
4832 m_thread_plan_sp->SetIsControllingPlan(m_is_controlling);
4833 m_thread_plan_sp->SetOkayToDiscard(m_okay_to_discard);
4839 bool m_already_reset =
false;
4840 bool m_private =
false;
4841 bool m_is_controlling =
false;
4842 bool m_okay_to_discard =
false;
4848 const milliseconds default_one_thread_timeout(250);
4853 : default_one_thread_timeout;
4862 return std::min<microseconds>(default_one_thread_timeout,
4868 bool before_first_timeout) {
4874 if (before_first_timeout)
4878 return std::nullopt;
4883static std::optional<ExpressionResults>
4885 RestorePlanState &restorer,
const EventSP &event_sp,
4886 EventSP &event_to_broadcast_sp,
4888 bool handle_interrupts) {
4891 ThreadSP thread_sp = thread_plan_sp->GetTarget()
4894 .FindThreadByID(thread_id);
4897 "The thread on which we were running the "
4898 "expression: tid = {0}, exited while "
4899 "the expression was running.",
4905 if (plan == thread_plan_sp && plan->PlanSucceeded()) {
4906 LLDB_LOG(log,
"execution completed successfully");
4914 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
4916 stop_info_sp->ShouldNotify(event_sp.get())) {
4917 LLDB_LOG(log,
"stopped for breakpoint: {0}.", stop_info_sp->GetDescription());
4924 thread_plan_sp->SetPrivate(
false);
4925 event_to_broadcast_sp = event_sp;
4930 if (!handle_interrupts &&
4932 return std::nullopt;
4934 LLDB_LOG(log,
"thread plan did not successfully complete");
4936 event_to_broadcast_sp = event_sp;
4949 if (!thread_plan_sp) {
4955 if (!thread_plan_sp->ValidatePlan(
nullptr)) {
4958 "RunThreadPlan called with an invalid thread plan.");
4964 "RunThreadPlan called on wrong process.");
4969 if (thread ==
nullptr) {
4971 "RunThreadPlan called with invalid thread.");
4982 RestorePlanState thread_plan_restorer(thread_plan_sp);