mirror of
https://github.com/intel/llvm.git
synced 2026-02-02 02:00:03 +08:00
This PR introduces a new `ThreadPlanSingleThreadTimeout` that will be used to address potential deadlock during single-thread stepping. While debugging a target with a non-trivial number of threads (around 5000 threads in one example target), we noticed that a simple step over can take as long as 10 seconds. Enabling single-thread stepping mode significantly reduces the stepping time to around 3 seconds. However, this can introduce deadlock if we try to step over a method that depends on other threads to release a lock. To address this issue, we introduce a new `ThreadPlanSingleThreadTimeout` that can be controlled by the `target.process.thread.single-thread-plan-timeout` setting during single-thread stepping mode. The concept involves counting the elapsed time since the last internal stop to detect overall stepping progress. Once a timeout occurs, we assume the target is not making progress due to a potential deadlock, as mentioned above. We then send a new async interrupt, resume all threads, and `ThreadPlanSingleThreadTimeout` completes its task. To support this design, the major changes made in this PR are: 1. `ThreadPlanSingleThreadTimeout` is popped during every internal stop and reset (re-pushed) to the top of the stack (as a leaf node) during resume. This is achieved by always returning `true` from `ThreadPlanSingleThreadTimeout::DoPlanExplainsStop()` and `ThreadPlanSingleThreadTimeout::MischiefManaged()`. 2. A new thread-specific async interrupt stop is introduced, which can be detected/consumed by `ThreadPlanSingleThreadTimeout`. 3. The clearing of branch breakpoints in the range thread plan has been moved from `DoPlanExplainsStop()` to `ShouldStop()`, as it is not guaranteed that it will be called. The detailed design is discussed in the RFC below: [https://discourse.llvm.org/t/improve-single-thread-stepping/74599](https://discourse.llvm.org/t/improve-single-thread-stepping/74599) --------- Co-authored-by: jeffreytan81 <jeffreytan@fb.com>
139 lines
4.3 KiB
C++
139 lines
4.3 KiB
C++
//===-- LLDBUtils.cpp -------------------------------------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "LLDBUtils.h"
|
|
#include "DAP.h"
|
|
|
|
#include <mutex>
|
|
|
|
namespace lldb_dap {
|
|
|
|
bool RunLLDBCommands(llvm::StringRef prefix,
|
|
const llvm::ArrayRef<std::string> &commands,
|
|
llvm::raw_ostream &strm, bool parse_command_directives) {
|
|
if (commands.empty())
|
|
return true;
|
|
|
|
bool did_print_prefix = false;
|
|
|
|
lldb::SBCommandInterpreter interp = g_dap.debugger.GetCommandInterpreter();
|
|
for (llvm::StringRef command : commands) {
|
|
lldb::SBCommandReturnObject result;
|
|
bool quiet_on_success = false;
|
|
bool check_error = false;
|
|
|
|
while (parse_command_directives) {
|
|
if (command.starts_with("?")) {
|
|
command = command.drop_front();
|
|
quiet_on_success = true;
|
|
} else if (command.starts_with("!")) {
|
|
command = command.drop_front();
|
|
check_error = true;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
{
|
|
// Prevent simultaneous calls to HandleCommand, e.g. EventThreadFunction
|
|
// may asynchronously call RunExitCommands when we are already calling
|
|
// RunTerminateCommands.
|
|
static std::mutex handle_command_mutex;
|
|
std::lock_guard<std::mutex> locker(handle_command_mutex);
|
|
interp.HandleCommand(command.str().c_str(), result);
|
|
}
|
|
|
|
const bool got_error = !result.Succeeded();
|
|
// The if statement below is assuming we always print out `!` prefixed
|
|
// lines. The only time we don't print is when we have `quiet_on_success ==
|
|
// true` and we don't have an error.
|
|
if (quiet_on_success ? got_error : true) {
|
|
if (!did_print_prefix && !prefix.empty()) {
|
|
strm << prefix << "\n";
|
|
did_print_prefix = true;
|
|
}
|
|
strm << "(lldb) " << command << "\n";
|
|
auto output_len = result.GetOutputSize();
|
|
if (output_len) {
|
|
const char *output = result.GetOutput();
|
|
strm << output;
|
|
}
|
|
auto error_len = result.GetErrorSize();
|
|
if (error_len) {
|
|
const char *error = result.GetError();
|
|
strm << error;
|
|
}
|
|
}
|
|
if (check_error && got_error)
|
|
return false; // Stop running commands.
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string RunLLDBCommands(llvm::StringRef prefix,
|
|
const llvm::ArrayRef<std::string> &commands,
|
|
bool &required_command_failed,
|
|
bool parse_command_directives) {
|
|
required_command_failed = false;
|
|
std::string s;
|
|
llvm::raw_string_ostream strm(s);
|
|
required_command_failed =
|
|
!RunLLDBCommands(prefix, commands, strm, parse_command_directives);
|
|
strm.flush();
|
|
return s;
|
|
}
|
|
|
|
std::string
|
|
RunLLDBCommandsVerbatim(llvm::StringRef prefix,
|
|
const llvm::ArrayRef<std::string> &commands) {
|
|
bool required_command_failed = false;
|
|
return RunLLDBCommands(prefix, commands, required_command_failed,
|
|
/*parse_command_directives=*/false);
|
|
}
|
|
|
|
bool ThreadHasStopReason(lldb::SBThread &thread) {
|
|
switch (thread.GetStopReason()) {
|
|
case lldb::eStopReasonTrace:
|
|
case lldb::eStopReasonPlanComplete:
|
|
case lldb::eStopReasonBreakpoint:
|
|
case lldb::eStopReasonWatchpoint:
|
|
case lldb::eStopReasonInstrumentation:
|
|
case lldb::eStopReasonSignal:
|
|
case lldb::eStopReasonException:
|
|
case lldb::eStopReasonExec:
|
|
case lldb::eStopReasonProcessorTrace:
|
|
case lldb::eStopReasonFork:
|
|
case lldb::eStopReasonVFork:
|
|
case lldb::eStopReasonVForkDone:
|
|
case lldb::eStopReasonInterrupt:
|
|
return true;
|
|
case lldb::eStopReasonThreadExiting:
|
|
case lldb::eStopReasonInvalid:
|
|
case lldb::eStopReasonNone:
|
|
break;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static uint32_t constexpr THREAD_INDEX_SHIFT = 19;
|
|
|
|
uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id) {
|
|
return dap_frame_id >> THREAD_INDEX_SHIFT;
|
|
}
|
|
|
|
uint32_t GetLLDBFrameID(uint64_t dap_frame_id) {
|
|
return dap_frame_id & ((1u << THREAD_INDEX_SHIFT) - 1);
|
|
}
|
|
|
|
int64_t MakeDAPFrameID(lldb::SBFrame &frame) {
|
|
return ((int64_t)frame.GetThread().GetIndexID() << THREAD_INDEX_SHIFT) |
|
|
frame.GetFrameID();
|
|
}
|
|
|
|
} // namespace lldb_dap
|