Skip to content

Commit d62fdae

Browse files
committed
[lldb] LockableStreamPair WIP
1 parent 0716b13 commit d62fdae

23 files changed

+331
-226
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,27 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
133133

134134
lldb::FileSP GetInputFileSP() { return m_input_file_sp; }
135135

136-
lldb::LockableStreamFileSP GetOutputStreamSP() { return m_output_stream_sp; }
136+
lldb::LockableStreamFileSP GetOutputStreamSP() {
137+
return m_stream_pair_sp->GetOutputStreamSP();
138+
}
139+
140+
lldb::LockableStreamFileSP GetErrorStreamSP() {
141+
return m_stream_pair_sp->GetErrorStreamSP();
142+
}
137143

138-
lldb::LockableStreamFileSP GetErrorStreamSP() { return m_error_stream_sp; }
144+
lldb::LockableStreamPairSP GetOutputStreamPairSP() {
145+
return m_stream_pair_sp;
146+
}
139147

140148
File &GetInputFile() { return *m_input_file_sp; }
141149

142-
File &GetOutputFile() { return m_output_stream_sp->GetUnlockedFile(); }
150+
File &GetOutputFile() {
151+
return m_stream_pair_sp->GetOutputStreamSP()->GetUnlockedFile();
152+
}
143153

144-
File &GetErrorFile() { return m_error_stream_sp->GetUnlockedFile(); }
154+
File &GetErrorFile() {
155+
return m_stream_pair_sp->GetErrorStreamSP()->GetUnlockedFile();
156+
}
145157

146158
repro::DataRecorder *GetInputRecorder();
147159

@@ -202,8 +214,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
202214
// If any of the streams are not set, set them to the in/out/err stream of
203215
// the top most input reader to ensure they at least have something
204216
void AdoptTopIOHandlerFilesIfInvalid(lldb::FileSP &in,
205-
lldb::LockableStreamFileSP &out,
206-
lldb::LockableStreamFileSP &err);
217+
lldb::LockableStreamPairSP &out_pair);
207218

208219
/// Run the given IO handler and return immediately.
209220
void RunIOHandlerAsync(const lldb::IOHandlerSP &reader_sp,
@@ -687,11 +698,9 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
687698

688699
void InstanceInitialize();
689700

690-
// these should never be NULL
701+
/// Should never be NULL.
691702
lldb::FileSP m_input_file_sp;
692-
lldb::LockableStreamFileSP m_output_stream_sp;
693-
lldb::LockableStreamFileSP m_error_stream_sp;
694-
LockableStreamFile::Mutex m_output_mutex;
703+
lldb::LockableStreamPairSP m_stream_pair_sp;
695704

696705
/// Used for shadowing the input file when capturing a reproducer.
697706
repro::DataRecorder *m_input_recorder;

lldb/include/lldb/Core/IOHandler.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ class IOHandler {
5959

6060
IOHandler(Debugger &debugger, IOHandler::Type type,
6161
const lldb::FileSP &input_sp,
62-
const lldb::LockableStreamFileSP &output_sp,
63-
const lldb::LockableStreamFileSP &error_sp, uint32_t flags);
62+
const lldb::LockableStreamPairSP &stream_pair_sp, uint32_t flags);
6463

6564
virtual ~IOHandler();
6665

@@ -120,9 +119,7 @@ class IOHandler {
120119

121120
lldb::FileSP GetInputFileSP();
122121

123-
lldb::LockableStreamFileSP GetOutputStreamFileSP();
124-
125-
lldb::LockableStreamFileSP GetErrorStreamFileSP();
122+
lldb::LockableStreamPairSP GetOutputStreamPairSP();
126123

127124
Debugger &GetDebugger() { return m_debugger; }
128125

@@ -158,8 +155,7 @@ class IOHandler {
158155
protected:
159156
Debugger &m_debugger;
160157
lldb::FileSP m_input_sp;
161-
lldb::LockableStreamFileSP m_output_sp;
162-
lldb::LockableStreamFileSP m_error_sp;
158+
lldb::LockableStreamPairSP m_stream_pair_sp;
163159
Predicate<bool> m_popped;
164160
Flags m_flags;
165161
Type m_type;
@@ -327,8 +323,8 @@ class IOHandlerEditline : public IOHandler {
327323

328324
IOHandlerEditline(Debugger &debugger, IOHandler::Type type,
329325
const lldb::FileSP &input_sp,
330-
const lldb::LockableStreamFileSP &output_sp,
331-
const lldb::LockableStreamFileSP &error_sp, uint32_t flags,
326+
const lldb::LockableStreamPairSP &stream_pair_sp,
327+
uint32_t flags,
332328
const char *editline_name, // Used for saving history files
333329
llvm::StringRef prompt, llvm::StringRef continuation_prompt,
334330
bool multi_line, bool color,
@@ -342,10 +338,9 @@ class IOHandlerEditline : public IOHandler {
342338
IOHandlerDelegate &) = delete;
343339

344340
IOHandlerEditline(Debugger &, IOHandler::Type, const lldb::FileSP &,
345-
const lldb::LockableStreamFileSP &,
346-
const lldb::LockableStreamFileSP &, uint32_t, const char *,
347-
const char *, const char *, bool, bool, uint32_t,
348-
IOHandlerDelegate &) = delete;
341+
const lldb::LockableStreamPairSP &stream_pair_sp, uint32_t,
342+
const char *, const char *, const char *, bool, bool,
343+
uint32_t, IOHandlerDelegate &) = delete;
349344

350345
~IOHandlerEditline() override;
351346

lldb/include/lldb/Host/Editline.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ using namespace line_editor;
153153
class Editline {
154154
public:
155155
Editline(const char *editor_name, FILE *input_file,
156-
lldb::LockableStreamFileSP output_stream_sp,
157-
lldb::LockableStreamFileSP error_stream_sp, bool color);
156+
lldb::LockableStreamPairSP stream_pair_sp, bool color);
158157

159158
~Editline();
160159

@@ -395,8 +394,7 @@ class Editline {
395394
volatile std::sig_atomic_t m_terminal_size_has_changed = 0;
396395
std::string m_editor_name;
397396
FILE *m_input_file;
398-
lldb::LockableStreamFileSP m_output_stream_sp;
399-
lldb::LockableStreamFileSP m_error_stream_sp;
397+
lldb::LockableStreamPairSP m_stream_pair_sp;
400398
ConnectionFileDescriptor m_input_connection;
401399

402400
IsInputCompleteCallbackType m_is_input_complete_callback;

lldb/include/lldb/Host/StreamFile.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ class LockableStreamFile {
103103
const LockableStreamFile &operator=(const LockableStreamFile &) = delete;
104104
};
105105

106+
class LockableStreamPair {
107+
public:
108+
LockableStreamPair();
109+
LockableStreamPair(std::unique_ptr<File> file);
110+
LockableStreamPair(FILE *fh, bool transfer_ownership);
111+
112+
lldb::LockableStreamFileSP GetOutputStreamSP() { return m_output_stream_sp; }
113+
lldb::LockableStreamFileSP GetErrorStreamSP() { return m_error_stream_sp; }
114+
115+
LockableStreamFile &GetOutputStream() { return *m_output_stream_sp; }
116+
LockableStreamFile &GetErrorStream() { return *m_error_stream_sp; }
117+
118+
void SetOutputFile(lldb::FileSP file_sp);
119+
void SetErrorFile(lldb::FileSP file_sp);
120+
121+
private:
122+
/// Should always point to a valid lockable stream.
123+
/// @{
124+
lldb::LockableStreamFileSP m_output_stream_sp;
125+
lldb::LockableStreamFileSP m_error_stream_sp;
126+
/// @}
127+
LockableStreamFile::Mutex m_mutex;
128+
};
129+
106130
} // namespace lldb_private
107131

108132
#endif // LLDB_HOST_STREAMFILE_H

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "lldb/Utility/Broadcaster.h"
3333
#include "lldb/Utility/Status.h"
3434
#include "lldb/Utility/StructuredData.h"
35+
#include "lldb/lldb-forward.h"
3536
#include "lldb/lldb-private.h"
3637
#include <optional>
3738

@@ -116,11 +117,17 @@ class ScriptInterpreterIORedirect {
116117
~ScriptInterpreterIORedirect();
117118

118119
lldb::FileSP GetInputFile() const { return m_input_file_sp; }
120+
119121
lldb::FileSP GetOutputFile() const {
120-
return m_output_file_sp->GetUnlockedFileSP();
122+
if (m_stream_pair_sp)
123+
return m_stream_pair_sp->GetOutputStream().GetUnlockedFileSP();
124+
return nullptr;
121125
}
126+
122127
lldb::FileSP GetErrorFile() const {
123-
return m_error_file_sp->GetUnlockedFileSP();
128+
if (m_stream_pair_sp)
129+
return m_stream_pair_sp->GetErrorStream().GetUnlockedFileSP();
130+
return nullptr;
124131
}
125132

126133
/// Flush our output and error file handles.
@@ -132,9 +139,7 @@ class ScriptInterpreterIORedirect {
132139
ScriptInterpreterIORedirect(Debugger &debugger, CommandReturnObject *result);
133140

134141
lldb::FileSP m_input_file_sp;
135-
lldb::LockableStreamFileSP m_output_file_sp;
136-
lldb::LockableStreamFileSP m_error_file_sp;
137-
LockableStreamFile::Mutex m_output_mutex;
142+
lldb::LockableStreamPairSP m_stream_pair_sp;
138143
ThreadedCommunication m_communication;
139144
bool m_disconnect;
140145
};
@@ -246,9 +251,9 @@ class ScriptInterpreter : public PluginInterface {
246251
return StructuredData::GenericSP();
247252
}
248253

249-
virtual lldb::ValueObjectListSP GetRecognizedArguments(
250-
const StructuredData::ObjectSP &implementor,
251-
lldb::StackFrameSP frame_sp) {
254+
virtual lldb::ValueObjectListSP
255+
GetRecognizedArguments(const StructuredData::ObjectSP &implementor,
256+
lldb::StackFrameSP frame_sp) {
252257
return lldb::ValueObjectListSP();
253258
}
254259

@@ -264,16 +269,13 @@ class ScriptInterpreter : public PluginInterface {
264269
return StructuredData::GenericSP();
265270
}
266271

267-
virtual bool
268-
ScriptedBreakpointResolverSearchCallback(StructuredData::GenericSP implementor_sp,
269-
SymbolContext *sym_ctx)
270-
{
272+
virtual bool ScriptedBreakpointResolverSearchCallback(
273+
StructuredData::GenericSP implementor_sp, SymbolContext *sym_ctx) {
271274
return false;
272275
}
273276

274-
virtual lldb::SearchDepth
275-
ScriptedBreakpointResolverSearchDepth(StructuredData::GenericSP implementor_sp)
276-
{
277+
virtual lldb::SearchDepth ScriptedBreakpointResolverSearchDepth(
278+
StructuredData::GenericSP implementor_sp) {
277279
return lldb::eSearchDepthModule;
278280
}
279281

@@ -289,8 +291,7 @@ class ScriptInterpreter : public PluginInterface {
289291
}
290292

291293
virtual Status GenerateFunction(const char *signature,
292-
const StringList &input,
293-
bool is_callback) {
294+
const StringList &input, bool is_callback) {
294295
return Status::FromErrorString("not implemented");
295296
}
296297

@@ -411,11 +412,12 @@ class ScriptInterpreter : public PluginInterface {
411412
return false;
412413
}
413414

414-
virtual bool RunScriptBasedParsedCommand(
415-
StructuredData::GenericSP impl_obj_sp, Args& args,
416-
ScriptedCommandSynchronicity synchronicity,
417-
lldb_private::CommandReturnObject &cmd_retobj, Status &error,
418-
const lldb_private::ExecutionContext &exe_ctx) {
415+
virtual bool
416+
RunScriptBasedParsedCommand(StructuredData::GenericSP impl_obj_sp, Args &args,
417+
ScriptedCommandSynchronicity synchronicity,
418+
lldb_private::CommandReturnObject &cmd_retobj,
419+
Status &error,
420+
const lldb_private::ExecutionContext &exe_ctx) {
419421
return false;
420422
}
421423

@@ -500,8 +502,8 @@ class ScriptInterpreter : public PluginInterface {
500502
return false;
501503
}
502504

503-
virtual void OptionParsingStartedForCommandObject(
504-
StructuredData::GenericSP cmd_obj_sp) {
505+
virtual void
506+
OptionParsingStartedForCommandObject(StructuredData::GenericSP cmd_obj_sp) {
505507
return;
506508
}
507509

@@ -532,8 +534,8 @@ class ScriptInterpreter : public PluginInterface {
532534

533535
virtual llvm::Expected<unsigned>
534536
GetMaxPositionalArgumentsForCallable(const llvm::StringRef &callable_name) {
535-
return llvm::createStringError(
536-
llvm::inconvertibleErrorCode(), "Unimplemented function");
537+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
538+
"Unimplemented function");
537539
}
538540

539541
static std::string LanguageToString(lldb::ScriptLanguage language);

lldb/include/lldb/lldb-forward.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class Stream;
216216
class StreamFile;
217217
class StreamString;
218218
class LockableStreamFile;
219+
class LockableStreamPair;
219220
class StringList;
220221
class StringTableReader;
221222
class StructuredDataImpl;
@@ -434,6 +435,7 @@ typedef std::shared_ptr<lldb_private::StopInfo> StopInfoSP;
434435
typedef std::shared_ptr<lldb_private::Stream> StreamSP;
435436
typedef std::shared_ptr<lldb_private::StreamFile> StreamFileSP;
436437
typedef std::shared_ptr<lldb_private::LockableStreamFile> LockableStreamFileSP;
438+
typedef std::shared_ptr<lldb_private::LockableStreamPair> LockableStreamPairSP;
437439
typedef std::shared_ptr<lldb_private::StringSummaryFormat>
438440
StringTypeSummaryImplSP;
439441
typedef std::unique_ptr<lldb_private::StructuredDataImpl> StructuredDataImplUP;

lldb/source/Commands/CommandObjectBreakpointCommand.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ are no syntax errors may indicate that a function was declared but never called.
194194

195195
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
196196
if (interactive) {
197-
if (lldb::LockableStreamFileSP output_sp =
198-
io_handler.GetOutputStreamFileSP()) {
199-
LockedStreamFile locked_stream = output_sp->Lock();
197+
if (lldb::LockableStreamPairSP stream_pair_sp =
198+
io_handler.GetOutputStreamPairSP()) {
199+
LockedStreamFile locked_stream =
200+
stream_pair_sp->GetOutputStream().Lock();
200201
locked_stream.PutCString(g_reader_instructions);
201202
}
202203
}

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "lldb/Interpreter/ScriptInterpreter.h"
2525
#include "lldb/Utility/Args.h"
2626
#include "lldb/Utility/StringList.h"
27+
#include "lldb/lldb-forward.h"
2728
#include "llvm/ADT/StringRef.h"
2829
#include <optional>
2930

@@ -794,9 +795,10 @@ a number follows 'f':"
794795
protected:
795796
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
796797
if (interactive) {
797-
if (lldb::LockableStreamFileSP output_sp =
798-
io_handler.GetOutputStreamFileSP()) {
799-
LockedStreamFile locked_stream = output_sp->Lock();
798+
if (lldb::LockableStreamPairSP stream_pair_sp =
799+
io_handler.GetOutputStreamPairSP()) {
800+
LockedStreamFile locked_stream =
801+
stream_pair_sp->GetOutputStream().Lock();
800802
locked_stream.PutCString(
801803
"Enter one or more sed substitution commands in "
802804
"the form: 's/<regex>/<subst>/'.\nTerminate the "
@@ -2382,17 +2384,19 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
23822384

23832385
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
23842386
if (interactive) {
2385-
if (lldb::LockableStreamFileSP output_sp =
2386-
io_handler.GetOutputStreamFileSP()) {
2387-
LockedStreamFile locked_stream = output_sp->Lock();
2387+
if (lldb::LockableStreamPairSP stream_pair_sp =
2388+
io_handler.GetOutputStreamPairSP()) {
2389+
LockedStreamFile locked_stream =
2390+
stream_pair_sp->GetOutputStream().Lock();
23882391
locked_stream.PutCString(g_python_command_instructions);
23892392
}
23902393
}
23912394
}
23922395

23932396
void IOHandlerInputComplete(IOHandler &io_handler,
23942397
std::string &data) override {
2395-
LockableStreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
2398+
LockableStreamFileSP error_sp =
2399+
io_handler.GetOutputStreamPairSP()->GetErrorStreamSP();
23962400

23972401
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
23982402
if (interpreter) {

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,16 +502,14 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
502502
void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler,
503503
std::string &line) {
504504
io_handler.SetIsDone(true);
505-
LockedStreamFile locked_output_stream =
506-
io_handler.GetOutputStreamFileSP()->Lock();
507-
LockedStreamFile locked_error_stream =
508-
io_handler.GetErrorStreamFileSP()->Lock();
509-
505+
StreamSP output_stream =
506+
GetCommandInterpreter().GetDebugger().GetAsyncOutputStream();
507+
StreamSP error_stream =
508+
GetCommandInterpreter().GetDebugger().GetAsyncErrorStream();
510509
CommandReturnObject return_obj(
511510
GetCommandInterpreter().GetDebugger().GetUseColor());
512-
EvaluateExpression(line.c_str(), locked_output_stream, locked_error_stream,
513-
return_obj);
514-
locked_output_stream << return_obj.GetErrorString();
511+
EvaluateExpression(line.c_str(), *output_stream, *error_stream, return_obj);
512+
*output_stream << return_obj.GetErrorString();
515513
}
516514

517515
bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler,
@@ -543,8 +541,9 @@ void CommandObjectExpression::GetMultilineExpression() {
543541
1, // Show line numbers starting at 1
544542
*this));
545543

546-
if (LockableStreamFileSP output_sp = io_handler_sp->GetOutputStreamFileSP()) {
547-
LockedStreamFile locked_stream = output_sp->Lock();
544+
if (LockableStreamPairSP stream_pair_sp =
545+
io_handler_sp->GetOutputStreamPairSP()) {
546+
LockedStreamFile locked_stream = stream_pair_sp->GetOutputStream().Lock();
548547
locked_stream.PutCString(
549548
"Enter expressions, then terminate with an empty line to evaluate:\n");
550549
}

0 commit comments

Comments
 (0)