Skip to content

[lldb] Store StreamAsynchronousIO in a unique_ptr (NFC) #127961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lldb/include/lldb/Core/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

void RestoreInputTerminalState();

lldb::StreamSP GetAsyncOutputStream();
lldb::StreamUP GetAsyncOutputStream();

lldb::StreamSP GetAsyncErrorStream();
lldb::StreamUP GetAsyncErrorStream();

CommandInterpreter &GetCommandInterpreter() {
assert(m_command_interpreter_up.get());
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/lldb-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ typedef std::unique_ptr<lldb_private::StackFrameRecognizerManager>
StackFrameRecognizerManagerUP;
typedef std::shared_ptr<lldb_private::StopInfo> StopInfoSP;
typedef std::shared_ptr<lldb_private::Stream> StreamSP;
typedef std::unique_ptr<lldb_private::Stream> StreamUP;
typedef std::shared_ptr<lldb_private::StreamFile> StreamFileSP;
typedef std::shared_ptr<lldb_private::LockableStreamFile> LockableStreamFileSP;
typedef std::shared_ptr<lldb_private::StringSummaryFormat>
Expand Down
6 changes: 2 additions & 4 deletions lldb/source/Breakpoint/BreakpointOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,8 @@ bool BreakpointOptions::BreakpointOptionsCallbackFunction(

// Rig up the results secondary output stream to the debugger's, so the
// output will come out synchronously if the debugger is set up that way.
StreamSP output_stream(debugger.GetAsyncOutputStream());
StreamSP error_stream(debugger.GetAsyncErrorStream());
result.SetImmediateOutputStream(output_stream);
result.SetImmediateErrorStream(error_stream);
result.SetImmediateOutputStream(debugger.GetAsyncOutputStream());
result.SetImmediateErrorStream(debugger.GetAsyncErrorStream());

CommandInterpreterRunOptions options;
options.SetStopOnContinue(true);
Expand Down
7 changes: 3 additions & 4 deletions lldb/source/Commands/CommandObjectCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,9 @@ a number follows 'f':"
for (const std::string &line : lines) {
Status error = AppendRegexSubstitution(line, check_only);
if (error.Fail()) {
if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
StreamSP out_stream = GetDebugger().GetAsyncOutputStream();
out_stream->Printf("error: %s\n", error.AsCString());
}
if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode())
GetDebugger().GetAsyncOutputStream()->Printf("error: %s\n",
error.AsCString());
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions lldb/source/Commands/CommandObjectWatchpointCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ are no syntax errors may indicate that a function was declared but never called.
// Rig up the results secondary output stream to the debugger's, so the
// output will come out synchronously if the debugger is set up that
// way.
StreamSP output_stream(debugger.GetAsyncOutputStream());
StreamSP error_stream(debugger.GetAsyncErrorStream());
result.SetImmediateOutputStream(output_stream);
result.SetImmediateErrorStream(error_stream);
result.SetImmediateOutputStream(debugger.GetAsyncOutputStream());
result.SetImmediateErrorStream(debugger.GetAsyncErrorStream());

CommandInterpreterRunOptions options;
options.SetStopOnContinue(true);
Expand Down
85 changes: 38 additions & 47 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
std::list<Status> errors;
StreamString feedback_stream;
if (!target_sp->LoadScriptingResources(errors, feedback_stream)) {
lldb::StreamSP s = GetAsyncErrorStream();
lldb::StreamUP s = GetAsyncErrorStream();
for (auto &error : errors)
s->Printf("%s\n", error.AsCString());
if (feedback_stream.GetSize())
Expand Down Expand Up @@ -1328,13 +1328,13 @@ bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) {
return true;
}

StreamSP Debugger::GetAsyncOutputStream() {
return std::make_shared<StreamAsynchronousIO>(*this,
StreamUP Debugger::GetAsyncOutputStream() {
return std::make_unique<StreamAsynchronousIO>(*this,
StreamAsynchronousIO::STDOUT);
}

StreamSP Debugger::GetAsyncErrorStream() {
return std::make_shared<StreamAsynchronousIO>(*this,
StreamUP Debugger::GetAsyncErrorStream() {
return std::make_unique<StreamAsynchronousIO>(*this,
StreamAsynchronousIO::STDERR);
}

Expand Down Expand Up @@ -1577,8 +1577,7 @@ static void PrivateReportDiagnostic(Debugger &debugger, Severity severity,
// diagnostic directly to the debugger's error stream.
DiagnosticEventData event_data(severity, std::move(message),
debugger_specific);
StreamSP stream = debugger.GetAsyncErrorStream();
event_data.Dump(stream.get());
event_data.Dump(debugger.GetAsyncErrorStream().get());
return;
}
EventSP event_sp = std::make_shared<Event>(
Expand Down Expand Up @@ -1774,12 +1773,11 @@ void Debugger::HandleBreakpointEvent(const EventSP &event_sp) {
if (num_new_locations > 0) {
BreakpointSP breakpoint =
Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
StreamSP output_sp(GetAsyncOutputStream());
if (output_sp) {
output_sp->Printf("%d location%s added to breakpoint %d\n",
if (StreamUP output_up = GetAsyncOutputStream()) {
output_up->Printf("%d location%s added to breakpoint %d\n",
num_new_locations, num_new_locations == 1 ? "" : "s",
breakpoint->GetID());
output_sp->Flush();
output_up->Flush();
}
}
}
Expand Down Expand Up @@ -1823,8 +1821,8 @@ void Debugger::HandleProcessEvent(const EventSP &event_sp) {
? EventDataStructuredData::GetProcessFromEvent(event_sp.get())
: Process::ProcessEventData::GetProcessFromEvent(event_sp.get());

StreamSP output_stream_sp = GetAsyncOutputStream();
StreamSP error_stream_sp = GetAsyncErrorStream();
StreamUP output_stream_up = GetAsyncOutputStream();
StreamUP error_stream_up = GetAsyncErrorStream();
const bool gui_enabled = IsForwardingEvents();

if (!gui_enabled) {
Expand All @@ -1849,7 +1847,7 @@ void Debugger::HandleProcessEvent(const EventSP &event_sp) {
if (got_state_changed && !state_is_stopped) {
// This is a public stop which we are going to announce to the user, so
// we should force the most relevant frame selection here.
Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(),
Process::HandleProcessStateChangedEvent(event_sp, output_stream_up.get(),
SelectMostRelevantFrame,
pop_process_io_handler);
}
Expand All @@ -1865,37 +1863,35 @@ void Debugger::HandleProcessEvent(const EventSP &event_sp) {
if (plugin_sp) {
auto structured_data_sp =
EventDataStructuredData::GetObjectFromEvent(event_sp.get());
if (output_stream_sp) {
StreamString content_stream;
Status error =
plugin_sp->GetDescription(structured_data_sp, content_stream);
if (error.Success()) {
if (!content_stream.GetString().empty()) {
// Add newline.
content_stream.PutChar('\n');
content_stream.Flush();

// Print it.
output_stream_sp->PutCString(content_stream.GetString());
}
} else {
error_stream_sp->Format("Failed to print structured "
"data with plugin {0}: {1}",
plugin_sp->GetPluginName(), error);
StreamString content_stream;
Status error =
plugin_sp->GetDescription(structured_data_sp, content_stream);
if (error.Success()) {
if (!content_stream.GetString().empty()) {
// Add newline.
content_stream.PutChar('\n');
content_stream.Flush();

// Print it.
output_stream_up->PutCString(content_stream.GetString());
}
} else {
error_stream_up->Format("Failed to print structured "
"data with plugin {0}: {1}",
plugin_sp->GetPluginName(), error);
}
}
}

// Now display any stopped state changes after any STDIO
if (got_state_changed && state_is_stopped) {
Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(),
Process::HandleProcessStateChangedEvent(event_sp, output_stream_up.get(),
SelectMostRelevantFrame,
pop_process_io_handler);
}

output_stream_sp->Flush();
error_stream_sp->Flush();
output_stream_up->Flush();
error_stream_up->Flush();

if (pop_process_io_handler)
process_sp->PopProcessIOHandler();
Expand Down Expand Up @@ -1995,22 +1991,18 @@ lldb::thread_result_t Debugger::DefaultEventHandler() {
const char *data = static_cast<const char *>(
EventDataBytes::GetBytesFromEvent(event_sp.get()));
if (data && data[0]) {
StreamSP error_sp(GetAsyncErrorStream());
if (error_sp) {
error_sp->PutCString(data);
error_sp->Flush();
}
StreamUP error_up = GetAsyncErrorStream();
error_up->PutCString(data);
error_up->Flush();
}
} else if (event_type & CommandInterpreter::
eBroadcastBitAsynchronousOutputData) {
const char *data = static_cast<const char *>(
EventDataBytes::GetBytesFromEvent(event_sp.get()));
if (data && data[0]) {
StreamSP output_sp(GetAsyncOutputStream());
if (output_sp) {
output_sp->PutCString(data);
output_sp->Flush();
}
StreamUP output_up = GetAsyncOutputStream();
output_up->PutCString(data);
output_up->Flush();
}
}
} else if (broadcaster == &m_broadcaster) {
Expand Down Expand Up @@ -2125,7 +2117,7 @@ void Debugger::HandleProgressEvent(const lldb::EventSP &event_sp) {
if (!file_sp->GetIsInteractive() || !file_sp->GetIsTerminalWithColors())
return;

StreamSP output = GetAsyncOutputStream();
StreamUP output = GetAsyncOutputStream();

// Print over previous line, if any.
output->Printf("\r");
Expand Down Expand Up @@ -2175,8 +2167,7 @@ void Debugger::HandleDiagnosticEvent(const lldb::EventSP &event_sp) {
if (!data)
return;

StreamSP stream = GetAsyncErrorStream();
data->Dump(stream.get());
data->Dump(GetAsyncErrorStream().get());
}

bool Debugger::HasIOHandlerThread() const {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Core/DynamicLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
}
} else {
if (force_symbol_search) {
lldb::StreamSP s = target.GetDebugger().GetAsyncErrorStream();
lldb::StreamUP s = target.GetDebugger().GetAsyncErrorStream();
s->Printf("Unable to find file");
if (!name.empty())
s->Printf(" %s", name.str().c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
}

if (IsKernel() && m_uuid.IsValid()) {
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
s->Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
s->Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);

Expand Down Expand Up @@ -830,7 +830,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
}

if (IsKernel() && !m_module_sp) {
lldb::StreamSP s = target.GetDebugger().GetAsyncErrorStream();
lldb::StreamUP s = target.GetDebugger().GetAsyncErrorStream();
s->Printf("WARNING: Unable to locate kernel binary on the debugger "
"system.\n");
if (kernel_search_error.Fail() && kernel_search_error.AsCString("") &&
Expand Down Expand Up @@ -974,7 +974,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
bool is_loaded = IsLoaded();

if (is_loaded && m_module_sp && IsKernel()) {
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
if (kernel_object_file) {
addr_t file_address =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
Target &target = process->GetTarget();

if (IsKernel() && m_uuid.IsValid()) {
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
s->Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
s->Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);
}
Expand Down Expand Up @@ -355,9 +355,9 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
if (!m_module_sp)
m_module_sp = target.GetOrCreateModule(module_spec, true);
if (IsKernel() && !m_module_sp) {
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
s->Printf("WARNING: Unable to locate kernel binary on the debugger "
"system.\n");
target.GetDebugger().GetAsyncOutputStream()->Printf(
"WARNING: Unable to locate kernel binary on the debugger "
"system.\n");
}
}

Expand Down Expand Up @@ -464,7 +464,7 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
}

if (IsLoaded() && m_module_sp && IsKernel()) {
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
if (kernel_object_file) {
addr_t file_address =
Expand Down
18 changes: 4 additions & 14 deletions lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,10 @@ Status ProcessKDP::DoConnectRemote(llvm::StringRef remote_url) {
SetID(1);
GetThreadList();
SetPrivateState(eStateStopped);
StreamSP async_strm_sp(target.GetDebugger().GetAsyncOutputStream());
if (async_strm_sp) {
const char *cstr;
if ((cstr = m_comm.GetKernelVersion()) != NULL) {
async_strm_sp->Printf("Version: %s\n", cstr);
async_strm_sp->Flush();
}
// if ((cstr = m_comm.GetImagePath ()) != NULL)
// {
// async_strm_sp->Printf ("Image Path:
// %s\n", cstr);
// async_strm_sp->Flush();
// }
}
const char *cstr;
if ((cstr = m_comm.GetKernelVersion()) != NULL)
target.GetDebugger().GetAsyncOutputStream()->Printf("Version: %s\n",
cstr);
} else {
return Status::FromErrorString("KDP_REATTACH failed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5495,8 +5495,7 @@ class CommandObjectProcessGDBRemoteSpeedTest : public CommandObjectParsed {
if (process) {
StreamSP output_stream_sp = result.GetImmediateOutputStream();
if (!output_stream_sp)
output_stream_sp =
StreamSP(m_interpreter.GetDebugger().GetAsyncOutputStream());
output_stream_sp = m_interpreter.GetDebugger().GetAsyncOutputStream();
result.SetImmediateOutputStream(output_stream_sp);

const uint32_t num_packets =
Expand Down
14 changes: 6 additions & 8 deletions lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2743,10 +2743,9 @@ Status Process::LaunchPrivate(ProcessLaunchInfo &launch_info, StateType &state,

// Now that we know the process type, update its signal responses from the
// ones stored in the Target:
if (m_unix_signals_sp) {
StreamSP warning_strm = GetTarget().GetDebugger().GetAsyncErrorStream();
GetTarget().UpdateSignalsFromDummy(m_unix_signals_sp, warning_strm);
}
if (m_unix_signals_sp)
GetTarget().UpdateSignalsFromDummy(
m_unix_signals_sp, GetTarget().GetDebugger().GetAsyncErrorStream());

DynamicLoader *dyld = GetDynamicLoader();
if (dyld)
Expand Down Expand Up @@ -3131,10 +3130,9 @@ void Process::CompleteAttach() {
}
// Now that we know the process type, update its signal responses from the
// ones stored in the Target:
if (m_unix_signals_sp) {
StreamSP warning_strm = GetTarget().GetDebugger().GetAsyncErrorStream();
GetTarget().UpdateSignalsFromDummy(m_unix_signals_sp, warning_strm);
}
if (m_unix_signals_sp)
GetTarget().UpdateSignalsFromDummy(
m_unix_signals_sp, GetTarget().GetDebugger().GetAsyncErrorStream());

// We have completed the attach, now it is time to find the dynamic loader
// plug-in
Expand Down
8 changes: 3 additions & 5 deletions lldb/source/Target/StopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,9 @@ class StopInfoWatchpoint : public StopInfo {
wp_sp->CaptureWatchedValue(exe_ctx);

Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
StreamSP output_sp = debugger.GetAsyncOutputStream();
if (wp_sp->DumpSnapshots(output_sp.get())) {
output_sp->EOL();
output_sp->Flush();
}
StreamUP output_up = debugger.GetAsyncOutputStream();
if (wp_sp->DumpSnapshots(output_up.get()))
output_up->EOL();
}

} else {
Expand Down