Skip to content

Commit

Permalink
[lldb] Remove Debugger::Get{Output,Error}Stream (NFC)
Browse files Browse the repository at this point in the history
Remove Debugger::GetOutputStream and Debugger::GetErrorStream in
preparation for replacing both with a new variant that needs to be
locked and hence can't be handed out like we do right now.

The patch replaces most uses with GetAsyncOutputStream and
GetAsyncErrorStream respectively. There methods return new StreamSP
objects that automatically get flushed on destruction.

See #126630 for more details.
  • Loading branch information
JDevlieghere committed Feb 12, 2025
1 parent 918848d commit 34602ab
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 109 deletions.
4 changes: 0 additions & 4 deletions lldb/include/lldb/Core/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

File &GetErrorFile() { return m_error_stream_sp->GetFile(); }

StreamFile &GetOutputStream() { return *m_output_stream_sp; }

StreamFile &GetErrorStream() { return *m_error_stream_sp; }

repro::DataRecorder *GetInputRecorder();

Status SetInputString(const char *data);
Expand Down
28 changes: 10 additions & 18 deletions lldb/source/API/SBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,39 +508,31 @@ SBFile SBDebugger::GetInputFile() {

FILE *SBDebugger::GetOutputFileHandle() {
LLDB_INSTRUMENT_VA(this);
if (m_opaque_sp) {
StreamFile &stream_file = m_opaque_sp->GetOutputStream();
return stream_file.GetFile().GetStream();
}
if (m_opaque_sp)
return m_opaque_sp->GetOutputStreamSP()->GetFile().GetStream();
return nullptr;
}

SBFile SBDebugger::GetOutputFile() {
LLDB_INSTRUMENT_VA(this);
if (m_opaque_sp) {
SBFile file(m_opaque_sp->GetOutputStream().GetFileSP());
return file;
}
if (m_opaque_sp)
return SBFile(m_opaque_sp->GetOutputStreamSP()->GetFileSP());
return SBFile();
}

FILE *SBDebugger::GetErrorFileHandle() {
LLDB_INSTRUMENT_VA(this);

if (m_opaque_sp) {
StreamFile &stream_file = m_opaque_sp->GetErrorStream();
return stream_file.GetFile().GetStream();
}
if (m_opaque_sp)
return m_opaque_sp->GetErrorStreamSP()->GetFile().GetStream();
return nullptr;
}

SBFile SBDebugger::GetErrorFile() {
LLDB_INSTRUMENT_VA(this);
SBFile file;
if (m_opaque_sp) {
SBFile file(m_opaque_sp->GetErrorStream().GetFileSP());
return file;
}
if (m_opaque_sp)
return SBFile(m_opaque_sp->GetErrorStreamSP()->GetFileSP());
return SBFile();
}

Expand Down Expand Up @@ -581,8 +573,8 @@ void SBDebugger::HandleCommand(const char *command) {

sb_interpreter.HandleCommand(command, result, false);

result.PutError(m_opaque_sp->GetErrorStream().GetFileSP());
result.PutOutput(m_opaque_sp->GetOutputStream().GetFileSP());
result.PutError(m_opaque_sp->GetErrorStreamSP()->GetFileSP());
result.PutOutput(m_opaque_sp->GetOutputStreamSP()->GetFileSP());

if (!m_opaque_sp->GetAsyncExecution()) {
SBProcess process(GetCommandInterpreter().GetProcess());
Expand Down
9 changes: 4 additions & 5 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,11 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
std::list<Status> errors;
StreamString feedback_stream;
if (!target_sp->LoadScriptingResources(errors, feedback_stream)) {
Stream &s = GetErrorStream();
for (auto &error : errors) {
s.Printf("%s\n", error.AsCString());
}
lldb::StreamSP s = GetAsyncErrorStream();
for (auto &error : errors)
s->Printf("%s\n", error.AsCString());
if (feedback_stream.GetSize())
s.PutCString(feedback_stream.GetString());
s->PutCString(feedback_stream.GetString());
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions lldb/source/Core/DynamicLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
module_sp = std::make_shared<Module>(module_spec);
} else if (force_symbol_search && error.AsCString("") &&
error.AsCString("")[0] != '\0') {
target.GetDebugger().GetErrorStream() << error.AsCString();
*target.GetDebugger().GetAsyncErrorStream() << error.AsCString();
}
}

Expand Down Expand Up @@ -328,19 +328,19 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
}
} else {
if (force_symbol_search) {
Stream &s = target.GetDebugger().GetErrorStream();
s.Printf("Unable to find file");
lldb::StreamSP s = target.GetDebugger().GetAsyncErrorStream();
s->Printf("Unable to find file");
if (!name.empty())
s.Printf(" %s", name.str().c_str());
s->Printf(" %s", name.str().c_str());
if (uuid.IsValid())
s.Printf(" with UUID %s", uuid.GetAsString().c_str());
s->Printf(" with UUID %s", uuid.GetAsString().c_str());
if (value != LLDB_INVALID_ADDRESS) {
if (value_is_offset)
s.Printf(" with slide 0x%" PRIx64, value);
s->Printf(" with slide 0x%" PRIx64, value);
else
s.Printf(" at address 0x%" PRIx64, value);
s->Printf(" at address 0x%" PRIx64, value);
}
s.Printf("\n");
s->Printf("\n");
}
LLDB_LOGF(log,
"Unable to find binary %s with UUID %s and load it at "
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Interpreter/ScriptInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ ScriptInterpreterIORedirect::ScriptInterpreterIORedirect(
if (outfile_handle)
::setbuf(outfile_handle, nullptr);

result->SetImmediateOutputFile(debugger.GetOutputStream().GetFileSP());
result->SetImmediateErrorFile(debugger.GetErrorStream().GetFileSP());
result->SetImmediateOutputFile(debugger.GetOutputStreamSP()->GetFileSP());
result->SetImmediateErrorFile(debugger.GetErrorStreamSP()->GetFileSP());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,9 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
}

if (IsKernel() && m_uuid.IsValid()) {
Stream &s = target.GetDebugger().GetOutputStream();
s.Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
s.Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);
lldb::StreamSP 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);

// Start of a kernel debug session, we have the UUID of the kernel.
// Go through the target's list of modules and if there are any kernel
Expand Down Expand Up @@ -830,12 +830,12 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
}

if (IsKernel() && !m_module_sp) {
Stream &s = target.GetDebugger().GetErrorStream();
s.Printf("WARNING: Unable to locate kernel binary on the debugger "
"system.\n");
lldb::StreamSP 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("") &&
kernel_search_error.AsCString("")[0] != '\0') {
s << kernel_search_error.AsCString();
*s << kernel_search_error.AsCString();
}
}
}
Expand Down Expand Up @@ -974,22 +974,19 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
bool is_loaded = IsLoaded();

if (is_loaded && m_module_sp && IsKernel()) {
Stream &s = target.GetDebugger().GetOutputStream();
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
if (kernel_object_file) {
addr_t file_address =
kernel_object_file->GetBaseAddress().GetFileAddress();
if (m_load_address != LLDB_INVALID_ADDRESS &&
file_address != LLDB_INVALID_ADDRESS) {
s.Printf("Kernel slid 0x%" PRIx64 " in memory.\n",
m_load_address - file_address);
s->Printf("Kernel slid 0x%" PRIx64 " in memory.\n",
m_load_address - file_address);
}
}
{
s.Printf("Loaded kernel file %s\n",
m_module_sp->GetFileSpec().GetPath().c_str());
}
s.Flush();
s->Printf("Loaded kernel file %s\n",
m_module_sp->GetFileSpec().GetPath().c_str());
}

// Notify the target about the module being added;
Expand Down Expand Up @@ -1195,10 +1192,11 @@ bool DynamicLoaderDarwinKernel::ReadKextSummaryHeader() {
lldb::offset_t offset = 0;
m_kext_summary_header.version = data.GetU32(&offset);
if (m_kext_summary_header.version > 128) {
Stream &s = m_process->GetTarget().GetDebugger().GetOutputStream();
s.Printf("WARNING: Unable to read kext summary header, got "
"improbable version number %u\n",
m_kext_summary_header.version);
lldb::StreamSP s =
m_process->GetTarget().GetDebugger().GetOutputStreamSP();
s->Printf("WARNING: Unable to read kext summary header, got "
"improbable version number %u\n",
m_kext_summary_header.version);
// If we get an improbably large version number, we're probably
// getting bad memory.
m_kext_summary_header_addr.Clear();
Expand All @@ -1209,11 +1207,11 @@ bool DynamicLoaderDarwinKernel::ReadKextSummaryHeader() {
if (m_kext_summary_header.entry_size > 4096) {
// If we get an improbably large entry_size, we're probably
// getting bad memory.
Stream &s =
m_process->GetTarget().GetDebugger().GetOutputStream();
s.Printf("WARNING: Unable to read kext summary header, got "
"improbable entry_size %u\n",
m_kext_summary_header.entry_size);
lldb::StreamSP s =
m_process->GetTarget().GetDebugger().GetOutputStreamSP();
s->Printf("WARNING: Unable to read kext summary header, got "
"improbable entry_size %u\n",
m_kext_summary_header.entry_size);
m_kext_summary_header_addr.Clear();
return false;
}
Expand All @@ -1227,10 +1225,11 @@ bool DynamicLoaderDarwinKernel::ReadKextSummaryHeader() {
if (m_kext_summary_header.entry_count > 10000) {
// If we get an improbably large number of kexts, we're probably
// getting bad memory.
Stream &s = m_process->GetTarget().GetDebugger().GetOutputStream();
s.Printf("WARNING: Unable to read kext summary header, got "
"improbable number of kexts %u\n",
m_kext_summary_header.entry_count);
lldb::StreamSP s =
m_process->GetTarget().GetDebugger().GetOutputStreamSP();
s->Printf("WARNING: Unable to read kext summary header, got "
"improbable number of kexts %u\n",
m_kext_summary_header.entry_count);
m_kext_summary_header_addr.Clear();
return false;
}
Expand Down Expand Up @@ -1331,17 +1330,18 @@ bool DynamicLoaderDarwinKernel::ParseKextSummaries(
number_of_old_kexts_being_removed == 0)
return true;

Stream &s = m_process->GetTarget().GetDebugger().GetOutputStream();
lldb::StreamSP s = m_process->GetTarget().GetDebugger().GetOutputStreamSP();
if (load_kexts) {
if (number_of_new_kexts_being_added > 0 &&
number_of_old_kexts_being_removed > 0) {
s.Printf("Loading %d kext modules and unloading %d kext modules ",
number_of_new_kexts_being_added,
number_of_old_kexts_being_removed);
s->Printf("Loading %d kext modules and unloading %d kext modules ",
number_of_new_kexts_being_added,
number_of_old_kexts_being_removed);
} else if (number_of_new_kexts_being_added > 0) {
s.Printf("Loading %d kext modules ", number_of_new_kexts_being_added);
s->Printf("Loading %d kext modules ", number_of_new_kexts_being_added);
} else if (number_of_old_kexts_being_removed > 0) {
s.Printf("Unloading %d kext modules ", number_of_old_kexts_being_removed);
s->Printf("Unloading %d kext modules ",
number_of_old_kexts_being_removed);
}
}

Expand Down Expand Up @@ -1405,7 +1405,7 @@ bool DynamicLoaderDarwinKernel::ParseKextSummaries(
if (image_info.GetModule()) {
unloaded_module_list.AppendIfNeeded(image_info.GetModule());
}
s.Printf(".");
s->Printf(".");
image_info.Clear();
// should pull it out of the KextImageInfos vector but that would
// mutate the list and invalidate the to_be_removed bool vector;
Expand All @@ -1417,11 +1417,11 @@ bool DynamicLoaderDarwinKernel::ParseKextSummaries(
}

if (load_kexts) {
s.Printf(" done.\n");
s->Printf(" done.\n");
if (kexts_failed_to_load.size() > 0 && number_of_new_kexts_being_added > 0) {
s.Printf("Failed to load %d of %d kexts:\n",
(int)kexts_failed_to_load.size(),
number_of_new_kexts_being_added);
s->Printf("Failed to load %d of %d kexts:\n",
(int)kexts_failed_to_load.size(),
number_of_new_kexts_being_added);
// print a sorted list of <kext-name, uuid> kexts which failed to load
unsigned longest_name = 0;
std::sort(kexts_failed_to_load.begin(), kexts_failed_to_load.end());
Expand All @@ -1433,10 +1433,9 @@ bool DynamicLoaderDarwinKernel::ParseKextSummaries(
std::string uuid;
if (ku.second.IsValid())
uuid = ku.second.GetAsString();
s.Printf(" %-*s %s\n", longest_name, ku.first.c_str(), uuid.c_str());
s->Printf(" %-*s %s\n", longest_name, ku.first.c_str(), uuid.c_str());
}
}
s.Flush();
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
Target &target = process->GetTarget();

if (IsKernel() && m_uuid.IsValid()) {
Stream &s = target.GetDebugger().GetOutputStream();
s.Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
s.Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);
lldb::StreamSP 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);
}

// Test if the module is loaded into the taget,
Expand All @@ -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) {
Stream &s = target.GetDebugger().GetOutputStream();
s.Printf("WARNING: Unable to locate kernel binary on the debugger "
"system.\n");
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
s->Printf("WARNING: Unable to locate kernel binary on the debugger "
"system.\n");
}
}

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

if (IsLoaded() && m_module_sp && IsKernel()) {
Stream &s = target.GetDebugger().GetOutputStream();
lldb::StreamSP s = target.GetDebugger().GetAsyncOutputStream();
ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
if (kernel_object_file) {
addr_t file_address =
kernel_object_file->GetBaseAddress().GetFileAddress();
if (m_load_address != LLDB_INVALID_ADDRESS &&
file_address != LLDB_INVALID_ADDRESS) {
s.Printf("Kernel slide 0x%" PRIx64 " in memory.\n",
m_load_address - file_address);
s.Printf("Loaded kernel file %s\n",
m_module_sp->GetFileSpec().GetPath().c_str());
s->Printf("Kernel slide 0x%" PRIx64 " in memory.\n",
m_load_address - file_address);
s->Printf("Loaded kernel file %s\n",
m_module_sp->GetFileSpec().GetPath().c_str());
}
}
s.Flush();
}

return IsLoaded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,13 +864,14 @@ bool InstrumentationRuntimeTSan::NotifyBreakpointHit(
CreateStopReasonWithInstrumentationData(
*thread_sp, stop_reason_description, report));

StreamFile &s = process_sp->GetTarget().GetDebugger().GetOutputStream();
s.Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
"info -s' to get extended information about the "
"report.\n");
lldb::StreamSP s =
process_sp->GetTarget().GetDebugger().GetAsyncOutputStream();
s->Printf("ThreadSanitizer report breakpoint hit. Use 'thread "
"info -s' to get extended information about the "
"report.\n");

return true; // Return true to stop the target
} else
}
return false; // Let target run
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,14 @@ AppleObjCTrampolineHandler::AppleObjCTrampolineHandler(
// step through any method dispatches. Warn to that effect and get out of
// here.
if (process_sp->CanJIT()) {
process_sp->GetTarget().GetDebugger().GetErrorStream().Printf(
process_sp->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
"Could not find implementation lookup function \"%s\""
" step in through ObjC method dispatch will not work.\n",
get_impl_name.AsCString());
}
return;
}

// We will either set the implementation to the _stret or non_stret version,
// so either way it's safe to start filling the m_lookup_..._code here.
m_lookup_implementation_function_code.assign(
Expand Down
Loading

0 comments on commit 34602ab

Please sign in to comment.