Skip to content

Commit

Permalink
[ScriptInterpreter] Move ownership into debugger (NFC)
Browse files Browse the repository at this point in the history
This is part two of the change started in r359330. This patch moves the
ownership of the script interpreter from the command interpreter into
the debugger. I would've preferred to remove the lazy initialization,
however the fact that the scripting language is set after the debugger
is created makes that tricky. So for now this does exactly the same
thing as when it was under the command interpreter. The result is that
this patch is fully NFC.

Differential revision: https://reviews.llvm.org/D61211

llvm-svn: 359354
  • Loading branch information
JDevlieghere committed Apr 26, 2019
1 parent 3bf116c commit 2b29b43
Show file tree
Hide file tree
Showing 29 changed files with 76 additions and 104 deletions.
5 changes: 5 additions & 0 deletions lldb/include/lldb/Core/Debugger.h
Expand Up @@ -154,6 +154,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
return *m_command_interpreter_up;
}

ScriptInterpreter *GetScriptInterpreter(bool can_create = true);

lldb::ListenerSP GetListener() { return m_listener_sp; }

// This returns the Debugger's scratch source manager. It won't be able to
Expand Down Expand Up @@ -395,6 +397,9 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
// source file cache.
std::unique_ptr<CommandInterpreter> m_command_interpreter_up;

lldb::ScriptInterpreterSP m_script_interpreter_sp;
std::recursive_mutex m_script_interpreter_mutex;

IOHandlerStack m_input_reader_stack;
llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
Expand Down
6 changes: 0 additions & 6 deletions lldb/include/lldb/Interpreter/CommandInterpreter.h
Expand Up @@ -386,10 +386,6 @@ class CommandInterpreter : public Broadcaster,

int GetOptionArgumentPosition(const char *in_string);

ScriptInterpreter *GetScriptInterpreter(bool can_create = true);

void SetScriptInterpreter();

void SkipLLDBInitFiles(bool skip_lldbinit_files) {
m_skip_lldbinit_files = skip_lldbinit_files;
}
Expand Down Expand Up @@ -573,8 +569,6 @@ class CommandInterpreter : public Broadcaster,
CommandHistory m_command_history;
std::string m_repeat_command; // Stores the command that will be executed for
// an empty command string.
lldb::ScriptInterpreterSP m_script_interpreter_sp;
std::recursive_mutex m_script_interpreter_mutex;
lldb::IOHandlerSP m_command_io_handler_sp;
char m_comment_char;
bool m_batch_command_mode;
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/Interpreter/CommandObject.h
Expand Up @@ -121,6 +121,7 @@ class CommandObject {
GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type);

CommandInterpreter &GetCommandInterpreter() { return m_interpreter; }
Debugger &GetDebugger();

virtual llvm::StringRef GetHelp();

Expand Down
2 changes: 0 additions & 2 deletions lldb/source/API/SBBreakpoint.cpp
Expand Up @@ -602,7 +602,6 @@ void SBBreakpoint::SetScriptCallbackFunction(
BreakpointOptions *bp_options = bkpt_sp->GetOptions();
bkpt_sp->GetTarget()
.GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter()
->SetBreakpointCommandCallbackFunction(bp_options,
callback_function_name);
Expand All @@ -623,7 +622,6 @@ SBError SBBreakpoint::SetScriptCallbackBody(const char *callback_body_text) {
Status error =
bkpt_sp->GetTarget()
.GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter()
->SetBreakpointCommandCallback(bp_options, callback_body_text);
sb_error.SetError(error);
Expand Down
2 changes: 0 additions & 2 deletions lldb/source/API/SBBreakpointLocation.cpp
Expand Up @@ -220,7 +220,6 @@ void SBBreakpointLocation::SetScriptCallbackFunction(
loc_sp->GetBreakpoint()
.GetTarget()
.GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter()
->SetBreakpointCommandCallbackFunction(bp_options,
callback_function_name);
Expand All @@ -243,7 +242,6 @@ SBBreakpointLocation::SetScriptCallbackBody(const char *callback_body_text) {
loc_sp->GetBreakpoint()
.GetTarget()
.GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter()
->SetBreakpointCommandCallback(bp_options, callback_body_text);
sb_error.SetError(error);
Expand Down
2 changes: 0 additions & 2 deletions lldb/source/API/SBBreakpointName.cpp
Expand Up @@ -579,7 +579,6 @@ void SBBreakpointName::SetScriptCallbackFunction(
BreakpointOptions &bp_options = bp_name->GetOptions();
m_impl_up->GetTarget()
->GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter()
->SetBreakpointCommandCallbackFunction(&bp_options,
callback_function_name);
Expand All @@ -603,7 +602,6 @@ SBBreakpointName::SetScriptCallbackBody(const char *callback_body_text) {
Status error =
m_impl_up->GetTarget()
->GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter()
->SetBreakpointCommandCallback(&bp_options, callback_body_text);
sb_error.SetError(error);
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/API/SBTypeCategory.cpp
Expand Up @@ -425,7 +425,7 @@ bool SBTypeCategory::AddTypeSummary(SBTypeNameSpecifier type_name,
DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
if (debugger_sp) {
ScriptInterpreter *interpreter_ptr =
debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
debugger_sp->GetScriptInterpreter();
if (interpreter_ptr) {
std::string output;
if (interpreter_ptr->GenerateTypeScriptFunction(input, output,
Expand Down Expand Up @@ -549,7 +549,7 @@ bool SBTypeCategory::AddTypeSynthetic(SBTypeNameSpecifier type_name,
DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
if (debugger_sp) {
ScriptInterpreter *interpreter_ptr =
debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
debugger_sp->GetScriptInterpreter();
if (interpreter_ptr) {
std::string output;
if (interpreter_ptr->GenerateTypeSynthClass(input, output,
Expand Down
3 changes: 1 addition & 2 deletions lldb/source/Breakpoint/BreakpointOptions.cpp
Expand Up @@ -316,8 +316,7 @@ std::unique_ptr<BreakpointOptions> BreakpointOptions::CreateFromStructuredData(
if (cmd_data_up->interpreter == eScriptLanguageNone)
bp_options->SetCommandDataCallback(cmd_data_up);
else {
ScriptInterpreter *interp =
target.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
ScriptInterpreter *interp = target.GetDebugger().GetScriptInterpreter();
if (!interp) {
error.SetErrorStringWithFormat(
"Can't set script commands - no script interpreter");
Expand Down
5 changes: 1 addition & 4 deletions lldb/source/Breakpoint/BreakpointResolverScripted.cpp
Expand Up @@ -46,7 +46,6 @@ void BreakpointResolverScripted::CreateImplementationIfNeeded() {
if (m_breakpoint) {
TargetSP target_sp = m_breakpoint->GetTargetSP();
ScriptInterpreter *script_interp = target_sp->GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter();
if (!script_interp)
return;
Expand Down Expand Up @@ -105,7 +104,6 @@ BreakpointResolverScripted::CreateFromStructuredData(
}
ScriptInterpreter *script_interp = bkpt->GetTarget()
.GetDebugger()
.GetCommandInterpreter()
.GetScriptInterpreter();
return new BreakpointResolverScripted(bkpt, class_name, depth, args_data_impl,
*script_interp);
Expand All @@ -122,8 +120,7 @@ BreakpointResolverScripted::SerializeToStructuredData() {
}

ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
return m_breakpoint->GetTarget().GetDebugger().GetCommandInterpreter()
.GetScriptInterpreter();
return m_breakpoint->GetTarget().GetDebugger().GetScriptInterpreter();
}

Searcher::CallbackReturn
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Commands/CommandObjectBreakpointCommand.cpp
Expand Up @@ -422,7 +422,7 @@ are no syntax errors may indicate that a function was declared but never called.
// to set or collect command callback. Otherwise, call the methods
// associated with this object.
if (m_options.m_use_script_language) {
ScriptInterpreter *script_interp = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter();
// Special handling for one-liner specified inline.
if (m_options.m_use_one_liner) {
script_interp->SetBreakpointCommandCallback(
Expand Down
19 changes: 9 additions & 10 deletions lldb/source/Commands/CommandObjectCommands.cpp
Expand Up @@ -1246,7 +1246,7 @@ class CommandObjectPythonFunction : public CommandObjectRaw {
if (m_fetched_help_long)
return CommandObjectRaw::GetHelpLong();

ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectRaw::GetHelpLong();

Expand All @@ -1261,7 +1261,7 @@ class CommandObjectPythonFunction : public CommandObjectRaw {
protected:
bool DoExecute(llvm::StringRef raw_command_line,
CommandReturnObject &result) override {
ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();

Status error;

Expand Down Expand Up @@ -1304,7 +1304,7 @@ class CommandObjectScriptingObject : public CommandObjectRaw {
StreamString stream;
stream.Printf("For more information run 'help %s'", name.c_str());
SetHelp(stream.GetString());
if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter())
if (ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter())
GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
}

Expand All @@ -1319,7 +1319,7 @@ class CommandObjectScriptingObject : public CommandObjectRaw {
llvm::StringRef GetHelp() override {
if (m_fetched_help_short)
return CommandObjectRaw::GetHelp();
ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectRaw::GetHelp();
std::string docstring;
Expand All @@ -1335,7 +1335,7 @@ class CommandObjectScriptingObject : public CommandObjectRaw {
if (m_fetched_help_long)
return CommandObjectRaw::GetHelpLong();

ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectRaw::GetHelpLong();

Expand All @@ -1350,7 +1350,7 @@ class CommandObjectScriptingObject : public CommandObjectRaw {
protected:
bool DoExecute(llvm::StringRef raw_command_line,
CommandReturnObject &result) override {
ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();

Status error;

Expand Down Expand Up @@ -1489,7 +1489,7 @@ class CommandObjectCommandsScriptImport : public CommandObjectParsed {
// won't stomp on each other (wrt to execution contents, options, and
// more)
m_exe_ctx.Clear();
if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(
if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule(
entry.c_str(), m_options.m_allow_reload, init_session, error)) {
result.SetStatus(eReturnStatusSuccessFinishNoResult);
} else {
Expand Down Expand Up @@ -1630,7 +1630,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
std::string &data) override {
StreamFileSP error_sp = io_handler.GetErrorStreamFile();

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

StringList lines;
Expand Down Expand Up @@ -1715,8 +1715,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
}
}
} else {
ScriptInterpreter *interpreter =
GetCommandInterpreter().GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (!interpreter) {
result.AppendError("cannot find ScriptInterpreter");
result.SetStatus(eReturnStatusFailed);
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Commands/CommandObjectFrame.cpp
Expand Up @@ -890,7 +890,7 @@ bool CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
return false;
}

ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();

if (interpreter &&
!interpreter->CheckObjectExists(m_options.m_class_name.c_str())) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Commands/CommandObjectThread.cpp
Expand Up @@ -612,7 +612,7 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed {
result.AppendErrorWithFormat("empty class name for scripted step.");
result.SetStatus(eReturnStatusFailed);
return false;
} else if (!m_interpreter.GetScriptInterpreter()->CheckObjectExists(
} else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists(
m_options.m_class_name.c_str())) {
result.AppendErrorWithFormat(
"class for scripted step: \"%s\" does not exist.",
Expand Down
14 changes: 7 additions & 7 deletions lldb/source/Commands/CommandObjectType.cpp
Expand Up @@ -180,7 +180,7 @@ class CommandObjectTypeSummaryAdd : public CommandObjectParsed,
StreamFileSP error_sp = io_handler.GetErrorStreamFile();

#ifndef LLDB_DISABLE_PYTHON
ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (interpreter) {
StringList lines;
lines.SplitIntoLines(data);
Expand All @@ -192,7 +192,7 @@ class CommandObjectTypeSummaryAdd : public CommandObjectParsed,
options_ptr); // this will ensure that we get rid of the pointer
// when going out of scope

ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (interpreter) {
std::string funct_name_str;
if (interpreter->GenerateTypeScriptFunction(lines,
Expand Down Expand Up @@ -425,7 +425,7 @@ class CommandObjectTypeSynthAdd : public CommandObjectParsed,
StreamFileSP error_sp = io_handler.GetErrorStreamFile();

#ifndef LLDB_DISABLE_PYTHON
ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (interpreter) {
StringList lines;
lines.SplitIntoLines(data);
Expand All @@ -437,7 +437,7 @@ class CommandObjectTypeSynthAdd : public CommandObjectParsed,
options_ptr); // this will ensure that we get rid of the pointer
// when going out of scope

ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (interpreter) {
std::string class_name_str;
if (interpreter->GenerateTypeSynthClass(lines, class_name_str)) {
Expand Down Expand Up @@ -1346,7 +1346,7 @@ bool CommandObjectTypeSummaryAdd::Execute_ScriptSummary(
script_format = std::make_shared<ScriptSummaryFormat>(
m_options.m_flags, funct_name, code.c_str());

ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();

if (interpreter && !interpreter->CheckObjectExists(funct_name))
result.AppendWarningWithFormat(
Expand All @@ -1356,7 +1356,7 @@ bool CommandObjectTypeSummaryAdd::Execute_ScriptSummary(
} else if (!m_options.m_python_script
.empty()) // we have a quick 1-line script, just use it
{
ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (!interpreter) {
result.AppendError("script interpreter missing - unable to generate "
"function wrapper.\n");
Expand Down Expand Up @@ -2357,7 +2357,7 @@ bool CommandObjectTypeSynthAdd::Execute_PythonClass(

entry.reset(impl);

ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();

if (interpreter &&
!interpreter->CheckObjectExists(impl->GetPythonClassName()))
Expand Down
7 changes: 4 additions & 3 deletions lldb/source/Commands/CommandObjectWatchpointCommand.cpp
Expand Up @@ -442,7 +442,7 @@ are no syntax errors may indicate that a function was declared but never called.
if (m_options.m_use_script_language) {
// Special handling for one-liner specified inline.
if (m_options.m_use_one_liner) {
m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
GetDebugger().GetScriptInterpreter()->SetWatchpointCommandCallback(
wp_options, m_options.m_one_liner.c_str());
}
// Special handling for using a Python function by name instead of
Expand All @@ -452,10 +452,11 @@ are no syntax errors may indicate that a function was declared but never called.
else if (!m_options.m_function_name.empty()) {
std::string oneliner(m_options.m_function_name);
oneliner += "(frame, wp, internal_dict)";
m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
GetDebugger().GetScriptInterpreter()->SetWatchpointCommandCallback(
wp_options, oneliner.c_str());
} else {
m_interpreter.GetScriptInterpreter()
GetDebugger()
.GetScriptInterpreter()
->CollectDataForWatchpointCommandCallback(wp_options, result);
}
} else {
Expand Down
27 changes: 19 additions & 8 deletions lldb/source/Core/Debugger.cpp
Expand Up @@ -767,8 +767,8 @@ Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton)
m_source_manager_up(), m_source_file_cache(),
m_command_interpreter_up(
llvm::make_unique<CommandInterpreter>(*this, false)),
m_input_reader_stack(), m_instance_name(), m_loaded_plugins(),
m_event_handler_thread(), m_io_handler_thread(),
m_script_interpreter_sp(), m_input_reader_stack(), m_instance_name(),
m_loaded_plugins(), m_event_handler_thread(), m_io_handler_thread(),
m_sync_broadcaster(nullptr, "lldb.debugger.sync"),
m_forward_listener_sp(), m_clear_once() {
char instance_cstr[256];
Expand Down Expand Up @@ -905,12 +905,10 @@ void Debugger::SetOutputFileHandle(FILE *fh, bool tranfer_ownership) {
if (!out_file.IsValid())
out_file.SetStream(stdout, false);

// do not create the ScriptInterpreter just for setting the output file
// handle as the constructor will know how to do the right thing on its own
const bool can_create = false;
ScriptInterpreter *script_interpreter =
GetCommandInterpreter().GetScriptInterpreter(can_create);
if (script_interpreter)
// Do not create the ScriptInterpreter just for setting the output file
// handle as the constructor will know how to do the right thing on its own.
if (ScriptInterpreter *script_interpreter =
GetScriptInterpreter(/*can_create=*/false))
script_interpreter->ResetOutputFileHandle(fh);
}

Expand Down Expand Up @@ -1288,6 +1286,19 @@ bool Debugger::EnableLog(llvm::StringRef channel,
error_stream);
}

ScriptInterpreter *Debugger::GetScriptInterpreter(bool can_create) {
std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex);

if (!m_script_interpreter_sp) {
if (!can_create)
return nullptr;
m_script_interpreter_sp = PluginManager::GetScriptInterpreterForLanguage(
GetScriptLanguage(), *this);
}

return m_script_interpreter_sp.get();
}

SourceManager &Debugger::GetSourceManager() {
if (!m_source_manager_up)
m_source_manager_up = llvm::make_unique<SourceManager>(shared_from_this());
Expand Down

0 comments on commit 2b29b43

Please sign in to comment.