summaryrefslogtreecommitdiffstats
path: root/lldb/source
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source')
-rw-r--r--lldb/source/API/SBCommandInterpreter.cpp14
-rw-r--r--lldb/source/Commands/CommandObjectCommands.cpp218
-rw-r--r--lldb/source/Commands/CommandObjectPythonFunction.cpp88
-rw-r--r--lldb/source/Commands/CommandObjectPythonFunction.h62
-rw-r--r--lldb/source/Interpreter/ScriptInterpreter.cpp6
-rw-r--r--lldb/source/Interpreter/ScriptInterpreterPython.cpp141
6 files changed, 525 insertions, 4 deletions
diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp
index 30fc171e0ed..cd7cefd198d 100644
--- a/lldb/source/API/SBCommandInterpreter.cpp
+++ b/lldb/source/API/SBCommandInterpreter.cpp
@@ -337,6 +337,17 @@ extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *impl
extern "C" lldb::SBValue* LLDBSWIGPython_CastPyObjectToSBValue (void* data);
extern "C" void LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
+extern "C" bool LLDBSwigPythonCallCommand
+(
+ const char *python_function_name,
+ const char *session_dictionary_name,
+ lldb::DebuggerSP& debugger,
+ const char* args,
+ std::string& err_msg,
+ lldb::SBStream& stream
+);
+
+
extern "C" void init_lldb(void);
void
@@ -354,6 +365,7 @@ SBCommandInterpreter::InitializeSWIG ()
LLDBSwigPython_GetChildAtIndex,
LLDBSwigPython_GetIndexOfChildWithName,
LLDBSWIGPython_CastPyObjectToSBValue,
- LLDBSwigPython_UpdateSynthProviderInstance);
+ LLDBSwigPython_UpdateSynthProviderInstance,
+ LLDBSwigPythonCallCommand);
}
}
diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index 779a4ab3831..09ac12b726a 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -15,8 +15,11 @@
#include "llvm/ADT/StringRef.h"
// Project includes
+#include "CommandObjectPythonFunction.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/InputReader.h"
+#include "lldb/Core/InputReaderEZ.h"
+#include "lldb/Core/StringList.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandObjectRegexCommand.h"
@@ -306,8 +309,131 @@ CommandObjectCommandsSource::CommandOptions::g_option_table[] =
// CommandObjectCommandsAlias
//-------------------------------------------------------------------------
+static const char *g_python_command_instructions = "Enter your Python command(s). Type 'DONE' to end.\n"
+ "You must define a Python function with this signature:\n"
+ "def my_command_impl(debugger, args, stream, dict):";
+
+
class CommandObjectCommandsAlias : public CommandObject
{
+
+ class PythonAliasReader : public InputReaderEZ
+ {
+ private:
+ CommandInterpreter& m_interpreter;
+ std::string m_cmd_name;
+ StringList m_user_input;
+ DISALLOW_COPY_AND_ASSIGN (PythonAliasReader);
+ public:
+ PythonAliasReader(Debugger& debugger,
+ CommandInterpreter& interpreter,
+ std::string cmd_name) :
+ InputReaderEZ(debugger),
+ m_interpreter(interpreter),
+ m_cmd_name(cmd_name),
+ m_user_input()
+ {}
+
+ virtual
+ ~PythonAliasReader()
+ {
+ }
+
+ virtual void ActivateHandler(HandlerData& data)
+ {
+ StreamSP out_stream = data.GetOutStream();
+ bool batch_mode = data.GetBatchMode();
+ if (!batch_mode)
+ {
+ out_stream->Printf ("%s\n", g_python_command_instructions);
+ if (data.reader.GetPrompt())
+ out_stream->Printf ("%s", data.reader.GetPrompt());
+ out_stream->Flush();
+ }
+ }
+
+ virtual void ReactivateHandler(HandlerData& data)
+ {
+ StreamSP out_stream = data.GetOutStream();
+ bool batch_mode = data.GetBatchMode();
+ if (data.reader.GetPrompt() && !batch_mode)
+ {
+ out_stream->Printf ("%s", data.reader.GetPrompt());
+ out_stream->Flush();
+ }
+ }
+ virtual void GotTokenHandler(HandlerData& data)
+ {
+ StreamSP out_stream = data.GetOutStream();
+ bool batch_mode = data.GetBatchMode();
+ if (data.bytes && data.bytes_len)
+ {
+ m_user_input.AppendString(data.bytes, data.bytes_len);
+ }
+ if (!data.reader.IsDone() && data.reader.GetPrompt() && !batch_mode)
+ {
+ out_stream->Printf ("%s", data.reader.GetPrompt());
+ out_stream->Flush();
+ }
+ }
+ virtual void InterruptHandler(HandlerData& data)
+ {
+ StreamSP out_stream = data.GetOutStream();
+ bool batch_mode = data.GetBatchMode();
+ data.reader.SetIsDone (true);
+ if (!batch_mode)
+ {
+ out_stream->Printf ("Warning: No command attached to breakpoint.\n");
+ out_stream->Flush();
+ }
+ }
+ virtual void EOFHandler(HandlerData& data)
+ {
+ data.reader.SetIsDone (true);
+ }
+ virtual void DoneHandler(HandlerData& data)
+ {
+ StreamSP out_stream = data.GetOutStream();
+
+ ScriptInterpreter *interpreter = data.reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
+ if (!interpreter)
+ {
+ out_stream->Printf ("Internal error #1: no script attached.\n");
+ out_stream->Flush();
+ return;
+ }
+ StringList funct_name_sl;
+ if (!interpreter->GenerateScriptAliasFunction (m_user_input,
+ funct_name_sl))
+ {
+ out_stream->Printf ("Internal error #2: no script attached.\n");
+ out_stream->Flush();
+ return;
+ }
+ if (funct_name_sl.GetSize() == 0)
+ {
+ out_stream->Printf ("Internal error #3: no script attached.\n");
+ out_stream->Flush();
+ return;
+ }
+ const char *funct_name = funct_name_sl.GetStringAtIndex(0);
+ if (!funct_name || !funct_name[0])
+ {
+ out_stream->Printf ("Internal error #4: no script attached.\n");
+ out_stream->Flush();
+ return;
+ }
+
+ // everything should be fine now, let's add this alias
+
+ CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(m_interpreter,
+ m_cmd_name,
+ funct_name));
+
+ m_interpreter.AddAlias(m_cmd_name.c_str(), command_obj_sp);
+ }
+ };
+
public:
CommandObjectCommandsAlias (CommandInterpreter &interpreter) :
CommandObject (interpreter,
@@ -425,6 +551,98 @@ public:
// Get the alias command.
const std::string alias_command = args.GetArgumentAtIndex (0);
+
+ if (
+ (strcmp("--python",alias_command.c_str()) == 0) ||
+ (strcmp("-P",alias_command.c_str()) == 0)
+ )
+ {
+
+ if (argc < 3)
+ {
+ // this is a definition of the form
+ // command alias --python foo_cmd
+ // and the user will type foo_cmd_impl by hand
+ std::string cmd_name = args.GetArgumentAtIndex(1);
+ // Verify that the command is alias-able.
+ if (m_interpreter.CommandExists (cmd_name.c_str()))
+ {
+ result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
+ cmd_name.c_str());
+ result.SetStatus (eReturnStatusFailed);
+ return false;
+ }
+ if (m_interpreter.AliasExists (cmd_name.c_str())
+ || m_interpreter.UserCommandExists (cmd_name.c_str()))
+ {
+ result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
+ cmd_name.c_str());
+ }
+
+
+ InputReaderSP reader_sp (new PythonAliasReader (m_interpreter.GetDebugger(),
+ m_interpreter,
+ cmd_name));
+
+ if (reader_sp)
+ {
+
+ InputReaderEZ::InitializationParameters ipr;
+
+ Error err (reader_sp->Initialize (ipr.SetBaton(NULL).SetPrompt(" ")));
+ if (err.Success())
+ {
+ m_interpreter.GetDebugger().PushInputReader (reader_sp);
+ result.SetStatus (eReturnStatusSuccessFinishNoResult);
+ }
+ else
+ {
+ result.AppendError (err.AsCString());
+ result.SetStatus (eReturnStatusFailed);
+ }
+ }
+ else
+ {
+ result.AppendError("out of memory");
+ result.SetStatus (eReturnStatusFailed);
+ }
+
+ result.SetStatus (eReturnStatusSuccessFinishNoResult);
+ return result.Succeeded();
+ }
+ else
+ {
+ // this is a definition of the form
+ // command alias --python foo_cmd funct_impl_foo
+ std::string cmd_name = args.GetArgumentAtIndex(1);
+ std::string funct_name = args.GetArgumentAtIndex(2);
+
+ // Verify that the command is alias-able.
+ if (m_interpreter.CommandExists (cmd_name.c_str()))
+ {
+ result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
+ cmd_name.c_str());
+ result.SetStatus (eReturnStatusFailed);
+ return false;
+ }
+
+ CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(m_interpreter,
+ cmd_name,
+ funct_name));
+
+ if (m_interpreter.AliasExists (cmd_name.c_str())
+ || m_interpreter.UserCommandExists (cmd_name.c_str()))
+ {
+ result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
+ cmd_name.c_str());
+ }
+
+ m_interpreter.AddAlias(cmd_name.c_str(), command_obj_sp);
+
+ result.SetStatus (eReturnStatusSuccessFinishNoResult);
+ return result.Succeeded();
+ }
+ }
// Strip the new alias name off 'raw_command_string' (leave it on args, which gets passed to 'Execute', which
// does the stripping itself.
diff --git a/lldb/source/Commands/CommandObjectPythonFunction.cpp b/lldb/source/Commands/CommandObjectPythonFunction.cpp
new file mode 100644
index 00000000000..b1bb2af8a51
--- /dev/null
+++ b/lldb/source/Commands/CommandObjectPythonFunction.cpp
@@ -0,0 +1,88 @@
+//===-- CommandObjectPythonFunction.cpp --------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CommandObjectPythonFunction.h"
+
+// C Includes
+// C++ Includes
+// Other libraries and framework includes
+// Project includes
+
+#include "lldb/API/SBStream.h"
+
+#include "lldb/Core/Debugger.h"
+
+#include "lldb/Interpreter/Args.h"
+#include "lldb/Interpreter/Options.h"
+
+#include "lldb/Interpreter/CommandInterpreter.h"
+#include "lldb/Interpreter/CommandReturnObject.h"
+
+#include "lldb/Interpreter/ScriptInterpreter.h"
+#include "lldb/Interpreter/ScriptInterpreterPython.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+//-------------------------------------------------------------------------
+// CommandObjectApropos
+//-------------------------------------------------------------------------
+
+CommandObjectPythonFunction::CommandObjectPythonFunction (CommandInterpreter &interpreter,
+ std::string name,
+ std::string funct) :
+ CommandObject (interpreter,
+ name.c_str(),
+ (std::string("Run Python function ") + funct).c_str(),
+ NULL),
+ m_function_name(funct)
+{
+ CommandArgumentEntry arg;
+ CommandArgumentData search_word_arg;
+
+ // Define the first (and only) variant of this arg.
+ search_word_arg.arg_type = eArgTypeSearchWord;
+ search_word_arg.arg_repetition = eArgRepeatPlain;
+
+ // There is only one variant this argument could be; put it into the argument entry.
+ arg.push_back (search_word_arg);
+
+ // Push the data for the first argument into the m_arguments vector.
+ m_arguments.push_back (arg);
+}
+
+CommandObjectPythonFunction::~CommandObjectPythonFunction()
+{
+}
+
+bool
+CommandObjectPythonFunction::ExecuteRawCommandString (const char *raw_command_line,
+ CommandReturnObject &result)
+{
+ ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
+
+ Error error;
+
+ lldb::SBStream stream;
+
+ if (scripter->RunScriptBasedCommand(m_function_name.c_str(),
+ raw_command_line,
+ stream,
+ error) == false)
+ {
+ result.AppendError(error.AsCString());
+ result.SetStatus(eReturnStatusFailed);
+ }
+ else
+ result.SetStatus(eReturnStatusSuccessFinishNoResult);
+
+ result.GetOutputStream() << stream.GetData();
+
+ return result.Succeeded();
+}
diff --git a/lldb/source/Commands/CommandObjectPythonFunction.h b/lldb/source/Commands/CommandObjectPythonFunction.h
new file mode 100644
index 00000000000..4af857dccd5
--- /dev/null
+++ b/lldb/source/Commands/CommandObjectPythonFunction.h
@@ -0,0 +1,62 @@
+//===-- CommandObjectPythonFunction.h -----------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_CommandObjectPythonFunction_h_
+#define liblldb_CommandObjectPythonFunction_h_
+
+// C Includes
+// C++ Includes
+// Other libraries and framework includes
+// Project includes
+#include "lldb/Interpreter/CommandObject.h"
+
+namespace lldb_private {
+
+//-------------------------------------------------------------------------
+// CommandObjectApropos
+//-------------------------------------------------------------------------
+
+class CommandObjectPythonFunction : public CommandObject
+{
+private:
+ std::string m_function_name;
+
+public:
+
+ CommandObjectPythonFunction (CommandInterpreter &interpreter,
+ std::string name,
+ std::string funct);
+
+ virtual
+ ~CommandObjectPythonFunction ();
+
+ virtual bool
+ ExecuteRawCommandString (const char *raw_command_line, CommandReturnObject &result);
+
+ virtual bool
+ WantsRawCommandString ()
+ {
+ return true;
+ }
+
+ bool
+ Execute (Args& command,
+ CommandReturnObject &result)
+ {
+ std::string cmd_string;
+ command.GetCommandString(cmd_string);
+ return ExecuteRawCommandString(cmd_string.c_str(), result);
+ }
+
+
+};
+
+} // namespace lldb_private
+
+#endif // liblldb_CommandObjectPythonFunction_h_
diff --git a/lldb/source/Interpreter/ScriptInterpreter.cpp b/lldb/source/Interpreter/ScriptInterpreter.cpp
index 94b04e72944..27bbb67605e 100644
--- a/lldb/source/Interpreter/ScriptInterpreter.cpp
+++ b/lldb/source/Interpreter/ScriptInterpreter.cpp
@@ -99,7 +99,8 @@ ScriptInterpreter::InitializeInterpreter (SWIGInitCallback python_swig_init_call
SWIGPythonGetChildAtIndex python_swig_get_child_index,
SWIGPythonGetIndexOfChildWithName python_swig_get_index_child,
SWIGPythonCastPyObjectToSBValue python_swig_cast_to_sbvalue,
- SWIGPythonUpdateSynthProviderInstance python_swig_update_provider)
+ SWIGPythonUpdateSynthProviderInstance python_swig_update_provider,
+ SWIGPythonCallCommand python_swig_call_command)
{
ScriptInterpreterPython::InitializeInterpreter (python_swig_init_callback,
python_swig_breakpoint_callback,
@@ -109,7 +110,8 @@ ScriptInterpreter::InitializeInterpreter (SWIGInitCallback python_swig_init_call
python_swig_get_child_index,
python_swig_get_index_child,
python_swig_cast_to_sbvalue,
- python_swig_update_provider);
+ python_swig_update_provider,
+ python_swig_call_command);
}
void
diff --git a/lldb/source/Interpreter/ScriptInterpreterPython.cpp b/lldb/source/Interpreter/ScriptInterpreterPython.cpp
index 2db4202d9c8..e0a88cbc338 100644
--- a/lldb/source/Interpreter/ScriptInterpreterPython.cpp
+++ b/lldb/source/Interpreter/ScriptInterpreterPython.cpp
@@ -41,6 +41,7 @@ static ScriptInterpreter::SWIGPythonGetChildAtIndex g_swig_get_child_index = NUL
static ScriptInterpreter::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = NULL;
static ScriptInterpreter::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue = NULL;
static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = NULL;
+static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = NULL;
static int
_check_and_flush (FILE *stream)
@@ -765,6 +766,12 @@ ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string,
success = PyArg_Parse (py_return, format, (char **) &ret_value);
break;
}
+ case eCharStrOrNone: // char* or NULL if py_return == Py_None
+ {
+ const char format[3] = "z";
+ success = PyArg_Parse (py_return, format, (char **) &ret_value);
+ break;
+ }
case eBool:
{
const char format[2] = "b";
@@ -1251,6 +1258,70 @@ ScriptInterpreterPython::GenerateTypeScriptFunction (StringList &user_input, Str
}
bool
+ScriptInterpreterPython::GenerateScriptAliasFunction (StringList &user_input, StringList &output)
+{
+ static int num_created_functions = 0;
+ user_input.RemoveBlankLines ();
+ int num_lines = user_input.GetSize ();
+ StreamString sstr;
+
+ // Check to see if we have any data; if not, just return.
+ if (user_input.GetSize() == 0)
+ return false;
+
+ // Take what the user wrote, wrap it all up inside one big auto-generated Python function, passing in the
+ // ValueObject as parameter to the function.
+
+ sstr.Printf ("lldb_autogen_python_cmd_alias_func_%d", num_created_functions);
+ ++num_created_functions;
+ std::string auto_generated_function_name = sstr.GetData();
+
+ sstr.Clear();
+ StringList auto_generated_function;
+
+ // Create the function name & definition string.
+
+ sstr.Printf ("def %s (debugger, args, dict):", auto_generated_function_name.c_str());
+ auto_generated_function.AppendString (sstr.GetData());
+
+ // Pre-pend code for setting up the session dictionary.
+
+ auto_generated_function.AppendString (" global_dict = globals()"); // Grab the global dictionary
+ auto_generated_function.AppendString (" new_keys = dict.keys()"); // Make a list of keys in the session dict
+ auto_generated_function.AppendString (" old_keys = global_dict.keys()"); // Save list of keys in global dict
+ auto_generated_function.AppendString (" global_dict.update (dict)"); // Add the session dictionary to the
+ // global dictionary.
+
+ // Wrap everything up inside the function, increasing the indentation.
+
+ for (int i = 0; i < num_lines; ++i)
+ {
+ sstr.Clear ();
+ sstr.Printf (" %s", user_input.GetStringAtIndex (i));
+ auto_generated_function.AppendString (sstr.GetData());
+ }
+
+ // Append code to clean up the global dictionary and update the session dictionary (all updates in the function
+ // got written to the values in the global dictionary, not the session dictionary).
+
+ auto_generated_function.AppendString (" for key in new_keys:"); // Iterate over all the keys from session dict
+ auto_generated_function.AppendString (" dict[key] = global_dict[key]"); // Update session dict values
+ auto_generated_function.AppendString (" if key not in old_keys:"); // If key was not originally in global dict
+ auto_generated_function.AppendString (" del global_dict[key]"); // ...then remove key/value from global dict
+
+ // Verify that the results are valid Python.
+
+ if (!ExportFunctionDefinitionToInterpreter (auto_generated_function))
+ return false;
+
+ // Store the name of the auto-generated function to be called.
+
+ output.AppendString (auto_generated_function_name.c_str());
+ return true;
+}
+
+
+bool
ScriptInterpreterPython::GenerateTypeSynthClass (StringList &user_input, StringList &output)
{
static int num_created_classes = 0;
@@ -1835,6 +1906,72 @@ ScriptInterpreterPython::CastPyObjectToSBValue (void* data)
return ret_val;
}
+bool
+ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function,
+ const char* args,
+ lldb::SBStream& stream,
+ Error& error)
+{
+ if (!impl_function)
+ {
+ error.SetErrorString("no function to execute");
+ return false;
+ }
+
+ if (!g_swig_call_command)
+ {
+ error.SetErrorString("no helper function to run scripted commands");
+ return false;
+ }
+
+ ScriptInterpreterPython *python_interpreter = this;
+
+ lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().GetSP();
+
+ bool ret_val;
+
+ std::string err_msg;
+
+ FILE *tmp_fh = (python_interpreter->m_dbg_stdout ? python_interpreter->m_dbg_stdout : stdout);
+ if (CurrentThreadHasPythonLock())
+ {
+ python_interpreter->EnterSession ();
+ ret_val = g_swig_call_command (impl_function,
+ python_interpreter->m_dictionary_name.c_str(),
+ debugger_sp,
+ args,
+ err_msg,
+ stream);
+ python_interpreter->LeaveSession ();
+ }
+ else
+ {
+ while (!GetPythonLock (1))
+ fprintf (tmp_fh,
+ "Python interpreter locked on another thread; waiting to acquire lock...\n");
+ python_interpreter->EnterSession ();
+ ret_val = g_swig_call_command (impl_function,
+ python_interpreter->m_dictionary_name.c_str(),
+ debugger_sp,
+ args,
+ err_msg,
+ stream);
+ python_interpreter->LeaveSession ();
+ ReleasePythonLock ();
+ }
+
+ if (!ret_val)
+ error.SetErrorString(err_msg.c_str());
+ else
+ error.Clear();
+
+ return ret_val;
+
+
+ return true;
+
+}
+
void
ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_init_callback,
@@ -1845,7 +1982,8 @@ ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_ini
SWIGPythonGetChildAtIndex python_swig_get_child_index,
SWIGPythonGetIndexOfChildWithName python_swig_get_index_child,
SWIGPythonCastPyObjectToSBValue python_swig_cast_to_sbvalue,
- SWIGPythonUpdateSynthProviderInstance python_swig_update_provider)
+ SWIGPythonUpdateSynthProviderInstance python_swig_update_provider,
+ SWIGPythonCallCommand python_swig_call_command)
{
g_swig_init_callback = python_swig_init_callback;
g_swig_breakpoint_callback = python_swig_breakpoint_callback;
@@ -1856,6 +1994,7 @@ ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_ini
g_swig_get_index_child = python_swig_get_index_child;
g_swig_cast_to_sbvalue = python_swig_cast_to_sbvalue;
g_swig_update_provider = python_swig_update_provider;
+ g_swig_call_command = python_swig_call_command;
}
void
OpenPOWER on IntegriCloud