Skip to content

Commit 78d82d3

Browse files
authored
[lldb] Store StreamAsynchronousIO in a unique_ptr (NFC) (#127961)
Make StreamAsynchronousIO an unique_ptr instead of a shared_ptr. I tried passing the class by value, but the llvm::raw_ostream forwarder stored in the Stream parent class isn't movable and I don't think it's worth changing that. Additionally, there's a few places that expect a StreamSP, which are easily created from a StreamUP.
1 parent 6d84fae commit 78d82d3

File tree

13 files changed

+71
-99
lines changed

13 files changed

+71
-99
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
156156

157157
void RestoreInputTerminalState();
158158

159-
lldb::StreamSP GetAsyncOutputStream();
159+
lldb::StreamUP GetAsyncOutputStream();
160160

161-
lldb::StreamSP GetAsyncErrorStream();
161+
lldb::StreamUP GetAsyncErrorStream();
162162

163163
CommandInterpreter &GetCommandInterpreter() {
164164
assert(m_command_interpreter_up.get());

lldb/include/lldb/lldb-forward.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ typedef std::unique_ptr<lldb_private::StackFrameRecognizerManager>
432432
StackFrameRecognizerManagerUP;
433433
typedef std::shared_ptr<lldb_private::StopInfo> StopInfoSP;
434434
typedef std::shared_ptr<lldb_private::Stream> StreamSP;
435+
typedef std::unique_ptr<lldb_private::Stream> StreamUP;
435436
typedef std::shared_ptr<lldb_private::StreamFile> StreamFileSP;
436437
typedef std::shared_ptr<lldb_private::LockableStreamFile> LockableStreamFileSP;
437438
typedef std::shared_ptr<lldb_private::StringSummaryFormat>

lldb/source/Breakpoint/BreakpointOptions.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,8 @@ bool BreakpointOptions::BreakpointOptionsCallbackFunction(
620620

621621
// Rig up the results secondary output stream to the debugger's, so the
622622
// output will come out synchronously if the debugger is set up that way.
623-
StreamSP output_stream(debugger.GetAsyncOutputStream());
624-
StreamSP error_stream(debugger.GetAsyncErrorStream());
625-
result.SetImmediateOutputStream(output_stream);
626-
result.SetImmediateErrorStream(error_stream);
623+
result.SetImmediateOutputStream(debugger.GetAsyncOutputStream());
624+
result.SetImmediateErrorStream(debugger.GetAsyncErrorStream());
627625

628626
CommandInterpreterRunOptions options;
629627
options.SetStopOnContinue(true);

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,10 +815,9 @@ a number follows 'f':"
815815
for (const std::string &line : lines) {
816816
Status error = AppendRegexSubstitution(line, check_only);
817817
if (error.Fail()) {
818-
if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
819-
StreamSP out_stream = GetDebugger().GetAsyncOutputStream();
820-
out_stream->Printf("error: %s\n", error.AsCString());
821-
}
818+
if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode())
819+
GetDebugger().GetAsyncOutputStream()->Printf("error: %s\n",
820+
error.AsCString());
822821
}
823822
}
824823
}

lldb/source/Commands/CommandObjectWatchpointCommand.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,8 @@ are no syntax errors may indicate that a function was declared but never called.
252252
// Rig up the results secondary output stream to the debugger's, so the
253253
// output will come out synchronously if the debugger is set up that
254254
// way.
255-
StreamSP output_stream(debugger.GetAsyncOutputStream());
256-
StreamSP error_stream(debugger.GetAsyncErrorStream());
257-
result.SetImmediateOutputStream(output_stream);
258-
result.SetImmediateErrorStream(error_stream);
255+
result.SetImmediateOutputStream(debugger.GetAsyncOutputStream());
256+
result.SetImmediateErrorStream(debugger.GetAsyncErrorStream());
259257

260258
CommandInterpreterRunOptions options;
261259
options.SetStopOnContinue(true);

lldb/source/Core/Debugger.cpp

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
257257
std::list<Status> errors;
258258
StreamString feedback_stream;
259259
if (!target_sp->LoadScriptingResources(errors, feedback_stream)) {
260-
lldb::StreamSP s = GetAsyncErrorStream();
260+
lldb::StreamUP s = GetAsyncErrorStream();
261261
for (auto &error : errors)
262262
s->Printf("%s\n", error.AsCString());
263263
if (feedback_stream.GetSize())
@@ -1328,13 +1328,13 @@ bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) {
13281328
return true;
13291329
}
13301330

1331-
StreamSP Debugger::GetAsyncOutputStream() {
1332-
return std::make_shared<StreamAsynchronousIO>(*this,
1331+
StreamUP Debugger::GetAsyncOutputStream() {
1332+
return std::make_unique<StreamAsynchronousIO>(*this,
13331333
StreamAsynchronousIO::STDOUT);
13341334
}
13351335

1336-
StreamSP Debugger::GetAsyncErrorStream() {
1337-
return std::make_shared<StreamAsynchronousIO>(*this,
1336+
StreamUP Debugger::GetAsyncErrorStream() {
1337+
return std::make_unique<StreamAsynchronousIO>(*this,
13381338
StreamAsynchronousIO::STDERR);
13391339
}
13401340

@@ -1577,8 +1577,7 @@ static void PrivateReportDiagnostic(Debugger &debugger, Severity severity,
15771577
// diagnostic directly to the debugger's error stream.
15781578
DiagnosticEventData event_data(severity, std::move(message),
15791579
debugger_specific);
1580-
StreamSP stream = debugger.GetAsyncErrorStream();
1581-
event_data.Dump(stream.get());
1580+
event_data.Dump(debugger.GetAsyncErrorStream().get());
15821581
return;
15831582
}
15841583
EventSP event_sp = std::make_shared<Event>(
@@ -1774,12 +1773,11 @@ void Debugger::HandleBreakpointEvent(const EventSP &event_sp) {
17741773
if (num_new_locations > 0) {
17751774
BreakpointSP breakpoint =
17761775
Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
1777-
StreamSP output_sp(GetAsyncOutputStream());
1778-
if (output_sp) {
1779-
output_sp->Printf("%d location%s added to breakpoint %d\n",
1776+
if (StreamUP output_up = GetAsyncOutputStream()) {
1777+
output_up->Printf("%d location%s added to breakpoint %d\n",
17801778
num_new_locations, num_new_locations == 1 ? "" : "s",
17811779
breakpoint->GetID());
1782-
output_sp->Flush();
1780+
output_up->Flush();
17831781
}
17841782
}
17851783
}
@@ -1823,8 +1821,8 @@ void Debugger::HandleProcessEvent(const EventSP &event_sp) {
18231821
? EventDataStructuredData::GetProcessFromEvent(event_sp.get())
18241822
: Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
18251823

1826-
StreamSP output_stream_sp = GetAsyncOutputStream();
1827-
StreamSP error_stream_sp = GetAsyncErrorStream();
1824+
StreamUP output_stream_up = GetAsyncOutputStream();
1825+
StreamUP error_stream_up = GetAsyncErrorStream();
18281826
const bool gui_enabled = IsForwardingEvents();
18291827

18301828
if (!gui_enabled) {
@@ -1849,7 +1847,7 @@ void Debugger::HandleProcessEvent(const EventSP &event_sp) {
18491847
if (got_state_changed && !state_is_stopped) {
18501848
// This is a public stop which we are going to announce to the user, so
18511849
// we should force the most relevant frame selection here.
1852-
Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(),
1850+
Process::HandleProcessStateChangedEvent(event_sp, output_stream_up.get(),
18531851
SelectMostRelevantFrame,
18541852
pop_process_io_handler);
18551853
}
@@ -1865,37 +1863,35 @@ void Debugger::HandleProcessEvent(const EventSP &event_sp) {
18651863
if (plugin_sp) {
18661864
auto structured_data_sp =
18671865
EventDataStructuredData::GetObjectFromEvent(event_sp.get());
1868-
if (output_stream_sp) {
1869-
StreamString content_stream;
1870-
Status error =
1871-
plugin_sp->GetDescription(structured_data_sp, content_stream);
1872-
if (error.Success()) {
1873-
if (!content_stream.GetString().empty()) {
1874-
// Add newline.
1875-
content_stream.PutChar('\n');
1876-
content_stream.Flush();
1877-
1878-
// Print it.
1879-
output_stream_sp->PutCString(content_stream.GetString());
1880-
}
1881-
} else {
1882-
error_stream_sp->Format("Failed to print structured "
1883-
"data with plugin {0}: {1}",
1884-
plugin_sp->GetPluginName(), error);
1866+
StreamString content_stream;
1867+
Status error =
1868+
plugin_sp->GetDescription(structured_data_sp, content_stream);
1869+
if (error.Success()) {
1870+
if (!content_stream.GetString().empty()) {
1871+
// Add newline.
1872+
content_stream.PutChar('\n');
1873+
content_stream.Flush();
1874+
1875+
// Print it.
1876+
output_stream_up->PutCString(content_stream.GetString());
18851877
}
1878+
} else {
1879+
error_stream_up->Format("Failed to print structured "
1880+
"data with plugin {0}: {1}",
1881+
plugin_sp->GetPluginName(), error);
18861882
}
18871883
}
18881884
}
18891885

18901886
// Now display any stopped state changes after any STDIO
18911887
if (got_state_changed && state_is_stopped) {
1892-
Process::HandleProcessStateChangedEvent(event_sp, output_stream_sp.get(),
1888+
Process::HandleProcessStateChangedEvent(event_sp, output_stream_up.get(),
18931889
SelectMostRelevantFrame,
18941890
pop_process_io_handler);
18951891
}
18961892

1897-
output_stream_sp->Flush();
1898-
error_stream_sp->Flush();
1893+
output_stream_up->Flush();
1894+
error_stream_up->Flush();
18991895

19001896
if (pop_process_io_handler)
19011897
process_sp->PopProcessIOHandler();
@@ -1995,22 +1991,18 @@ lldb::thread_result_t Debugger::DefaultEventHandler() {
19951991
const char *data = static_cast<const char *>(
19961992
EventDataBytes::GetBytesFromEvent(event_sp.get()));
19971993
if (data && data[0]) {
1998-
StreamSP error_sp(GetAsyncErrorStream());
1999-
if (error_sp) {
2000-
error_sp->PutCString(data);
2001-
error_sp->Flush();
2002-
}
1994+
StreamUP error_up = GetAsyncErrorStream();
1995+
error_up->PutCString(data);
1996+
error_up->Flush();
20031997
}
20041998
} else if (event_type & CommandInterpreter::
20051999
eBroadcastBitAsynchronousOutputData) {
20062000
const char *data = static_cast<const char *>(
20072001
EventDataBytes::GetBytesFromEvent(event_sp.get()));
20082002
if (data && data[0]) {
2009-
StreamSP output_sp(GetAsyncOutputStream());
2010-
if (output_sp) {
2011-
output_sp->PutCString(data);
2012-
output_sp->Flush();
2013-
}
2003+
StreamUP output_up = GetAsyncOutputStream();
2004+
output_up->PutCString(data);
2005+
output_up->Flush();
20142006
}
20152007
}
20162008
} else if (broadcaster == &m_broadcaster) {
@@ -2125,7 +2117,7 @@ void Debugger::HandleProgressEvent(const lldb::EventSP &event_sp) {
21252117
if (!file_sp->GetIsInteractive() || !file_sp->GetIsTerminalWithColors())
21262118
return;
21272119

2128-
StreamSP output = GetAsyncOutputStream();
2120+
StreamUP output = GetAsyncOutputStream();
21292121

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

2178-
StreamSP stream = GetAsyncErrorStream();
2179-
data->Dump(stream.get());
2170+
data->Dump(GetAsyncErrorStream().get());
21802171
}
21812172

21822173
bool Debugger::HasIOHandlerThread() const {

lldb/source/Core/DynamicLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
328328
}
329329
} else {
330330
if (force_symbol_search) {
331-
lldb::StreamSP s = target.GetDebugger().GetAsyncErrorStream();
331+
lldb::StreamUP s = target.GetDebugger().GetAsyncErrorStream();
332332
s->Printf("Unable to find file");
333333
if (!name.empty())
334334
s->Printf(" %s", name.str().c_str());

lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
738738
}
739739

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

@@ -830,7 +830,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
830830
}
831831

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

976976
if (is_loaded && m_module_sp && IsKernel()) {
977-
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
977+
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
978978
ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
979979
if (kernel_object_file) {
980980
addr_t file_address =

lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
327327
Target &target = process->GetTarget();
328328

329329
if (IsKernel() && m_uuid.IsValid()) {
330-
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
330+
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
331331
s->Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
332332
s->Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);
333333
}
@@ -355,9 +355,9 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
355355
if (!m_module_sp)
356356
m_module_sp = target.GetOrCreateModule(module_spec, true);
357357
if (IsKernel() && !m_module_sp) {
358-
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
359-
s->Printf("WARNING: Unable to locate kernel binary on the debugger "
360-
"system.\n");
358+
target.GetDebugger().GetAsyncOutputStream()->Printf(
359+
"WARNING: Unable to locate kernel binary on the debugger "
360+
"system.\n");
361361
}
362362
}
363363

@@ -464,7 +464,7 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
464464
}
465465

466466
if (IsLoaded() && m_module_sp && IsKernel()) {
467-
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
467+
lldb::StreamUP s = target.GetDebugger().GetAsyncOutputStream();
468468
ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
469469
if (kernel_object_file) {
470470
addr_t file_address =

lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,20 +321,10 @@ Status ProcessKDP::DoConnectRemote(llvm::StringRef remote_url) {
321321
SetID(1);
322322
GetThreadList();
323323
SetPrivateState(eStateStopped);
324-
StreamSP async_strm_sp(target.GetDebugger().GetAsyncOutputStream());
325-
if (async_strm_sp) {
326-
const char *cstr;
327-
if ((cstr = m_comm.GetKernelVersion()) != NULL) {
328-
async_strm_sp->Printf("Version: %s\n", cstr);
329-
async_strm_sp->Flush();
330-
}
331-
// if ((cstr = m_comm.GetImagePath ()) != NULL)
332-
// {
333-
// async_strm_sp->Printf ("Image Path:
334-
// %s\n", cstr);
335-
// async_strm_sp->Flush();
336-
// }
337-
}
324+
const char *cstr;
325+
if ((cstr = m_comm.GetKernelVersion()) != NULL)
326+
target.GetDebugger().GetAsyncOutputStream()->Printf("Version: %s\n",
327+
cstr);
338328
} else {
339329
return Status::FromErrorString("KDP_REATTACH failed");
340330
}

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5495,8 +5495,7 @@ class CommandObjectProcessGDBRemoteSpeedTest : public CommandObjectParsed {
54955495
if (process) {
54965496
StreamSP output_stream_sp = result.GetImmediateOutputStream();
54975497
if (!output_stream_sp)
5498-
output_stream_sp =
5499-
StreamSP(m_interpreter.GetDebugger().GetAsyncOutputStream());
5498+
output_stream_sp = m_interpreter.GetDebugger().GetAsyncOutputStream();
55005499
result.SetImmediateOutputStream(output_stream_sp);
55015500

55025501
const uint32_t num_packets =

lldb/source/Target/Process.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,10 +2743,9 @@ Status Process::LaunchPrivate(ProcessLaunchInfo &launch_info, StateType &state,
27432743

27442744
// Now that we know the process type, update its signal responses from the
27452745
// ones stored in the Target:
2746-
if (m_unix_signals_sp) {
2747-
StreamSP warning_strm = GetTarget().GetDebugger().GetAsyncErrorStream();
2748-
GetTarget().UpdateSignalsFromDummy(m_unix_signals_sp, warning_strm);
2749-
}
2746+
if (m_unix_signals_sp)
2747+
GetTarget().UpdateSignalsFromDummy(
2748+
m_unix_signals_sp, GetTarget().GetDebugger().GetAsyncErrorStream());
27502749

27512750
DynamicLoader *dyld = GetDynamicLoader();
27522751
if (dyld)
@@ -3131,10 +3130,9 @@ void Process::CompleteAttach() {
31313130
}
31323131
// Now that we know the process type, update its signal responses from the
31333132
// ones stored in the Target:
3134-
if (m_unix_signals_sp) {
3135-
StreamSP warning_strm = GetTarget().GetDebugger().GetAsyncErrorStream();
3136-
GetTarget().UpdateSignalsFromDummy(m_unix_signals_sp, warning_strm);
3137-
}
3133+
if (m_unix_signals_sp)
3134+
GetTarget().UpdateSignalsFromDummy(
3135+
m_unix_signals_sp, GetTarget().GetDebugger().GetAsyncErrorStream());
31383136

31393137
// We have completed the attach, now it is time to find the dynamic loader
31403138
// plug-in

lldb/source/Target/StopInfo.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,11 +1016,9 @@ class StopInfoWatchpoint : public StopInfo {
10161016
wp_sp->CaptureWatchedValue(exe_ctx);
10171017

10181018
Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
1019-
StreamSP output_sp = debugger.GetAsyncOutputStream();
1020-
if (wp_sp->DumpSnapshots(output_sp.get())) {
1021-
output_sp->EOL();
1022-
output_sp->Flush();
1023-
}
1019+
StreamUP output_up = debugger.GetAsyncOutputStream();
1020+
if (wp_sp->DumpSnapshots(output_up.get()))
1021+
output_up->EOL();
10241022
}
10251023

10261024
} else {

0 commit comments

Comments
 (0)