[Lldb-commits] [lldb] r144035 - in /lldb/trunk: include/lldb/ include/lldb/Interpreter/ source/Commands/ source/Interpreter/ test/functionalities/command_script/ test/functionalities/command_script/import/

Enrico Granata granata.enrico at gmail.com
Mon Nov 7 14:57:04 PST 2011


Author: enrico
Date: Mon Nov  7 16:57:04 2011
New Revision: 144035

URL: http://llvm.org/viewvc/llvm-project?rev=144035&view=rev
Log:
this patch addresses several issues with "command script" subcommands:
 a) adds a new --synchronicity (-s) setting for "command script add" that allows the user to decide if scripted commands should run synchronously or asynchronously (which can make a difference in how events are handled)
 b) clears up several error messages
 c) adds a new --allow-reload (-r) setting for "command script import" that allows the user to reload a module even if it has already been imported before
 d) allows filename completion for "command script import" (much like what happens for "target create")
 e) prevents "command script add" from replacing built-in commands with scripted commands
 f) changes AddUserCommand() to take an std::string instead of a const char* (for performance reasons)
plus, it fixes an issue in "type summary add" command handling which caused several test suite errors

Added:
    lldb/trunk/test/functionalities/command_script/mysto.py
Modified:
    lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
    lldb/trunk/include/lldb/lldb-enumerations.h
    lldb/trunk/include/lldb/lldb-private-enumerations.h
    lldb/trunk/source/Commands/CommandObjectCommands.cpp
    lldb/trunk/source/Commands/CommandObjectType.cpp
    lldb/trunk/source/Interpreter/CommandInterpreter.cpp
    lldb/trunk/source/Interpreter/CommandObject.cpp
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
    lldb/trunk/test/functionalities/command_script/TestCommandScript.py
    lldb/trunk/test/functionalities/command_script/import/TestImport.py
    lldb/trunk/test/functionalities/command_script/main.cpp
    lldb/trunk/test/functionalities/command_script/py_import
    lldb/trunk/test/functionalities/command_script/welcome.py

Modified: lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h Mon Nov  7 16:57:04 2011
@@ -72,7 +72,7 @@
                 bool can_replace);
     
     bool
-    AddUserCommand (const char *name, 
+    AddUserCommand (std::string name, 
                     const lldb::CommandObjectSP &cmd_sp,
                     bool can_replace);
     

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h Mon Nov  7 16:57:04 2011
@@ -73,8 +73,7 @@
         eScriptReturnTypeChar,
         eScriptReturnTypeCharStrOrNone
     } ScriptReturnType;
-
-
+    
     ScriptInterpreter (CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang);
 
     virtual ~ScriptInterpreter ();
@@ -185,6 +184,7 @@
     virtual bool
     RunScriptBasedCommand (const char* impl_function,
                            const char* args,
+                           ScriptedCommandSynchronicity synchronicity,
                            lldb_private::CommandReturnObject& cmd_retobj,
                            Error& error)
     {
@@ -199,6 +199,7 @@
 
     virtual bool
     LoadScriptingModule (const char* filename,
+                         bool can_reload,
                          lldb_private::Error& error)
     {
         error.SetErrorString("loading unimplemented");

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h Mon Nov  7 16:57:04 2011
@@ -87,6 +87,7 @@
     virtual bool
     RunScriptBasedCommand(const char* impl_function,
                           const char* args,
+                          ScriptedCommandSynchronicity synchronicity,
                           lldb_private::CommandReturnObject& cmd_retobj,
                           Error& error);
     
@@ -118,6 +119,7 @@
     
     virtual bool
     LoadScriptingModule (const char* filename,
+                         bool can_reload,
                          lldb_private::Error& error);
 
     void
@@ -170,6 +172,18 @@
     
 private:
     
+    class SynchronicityHandler
+    {
+    private:
+        lldb::DebuggerSP             m_debugger_sp;
+        ScriptedCommandSynchronicity m_synch_wanted;
+        bool                         m_old_asynch;
+    public:
+        SynchronicityHandler(lldb::DebuggerSP,
+                             ScriptedCommandSynchronicity);
+        ~SynchronicityHandler();
+    };
+    
 	class Locker
 	{
 	public:

Modified: lldb/trunk/include/lldb/lldb-enumerations.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-enumerations.h?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/include/lldb/lldb-enumerations.h (original)
+++ lldb/trunk/include/lldb/lldb-enumerations.h Mon Nov  7 16:57:04 2011
@@ -390,6 +390,7 @@
         eArgTypeRegularExpression,
         eArgTypeRunArgs,
         eArgTypeRunMode,
+        eArgTypeScriptedCommandSynchronicity,
         eArgTypeScriptLang,
         eArgTypeSearchWord,
         eArgTypeSelector,

Modified: lldb/trunk/include/lldb/lldb-private-enumerations.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-private-enumerations.h?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/include/lldb/lldb-private-enumerations.h (original)
+++ lldb/trunk/include/lldb/lldb-private-enumerations.h Mon Nov  7 16:57:04 2011
@@ -238,7 +238,17 @@
     eFormatterChoiceCriterionStrippedBitField =              0x00000020
 } FormatterChoiceCriterion;
 
-} // namespace lldb
+//----------------------------------------------------------------------
+// Synchronicity behavior of scripted commands
+//----------------------------------------------------------------------
+typedef enum ScriptedCommandSynchronicity
+{
+    eScriptedCommandSynchronicitySynchronous,
+    eScriptedCommandSynchronicityAsynchronous,
+    eScriptedCommandSynchronicityCurrentValue // use whatever the current synchronicity is
+} ScriptedCommandSynchronicity;
+        
+} // namespace lldb_private
 
 
 #endif  // LLDB_lldb_private_enumerations_h_

Modified: lldb/trunk/source/Commands/CommandObjectCommands.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectCommands.cpp?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectCommands.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectCommands.cpp Mon Nov  7 16:57:04 2011
@@ -1153,17 +1153,20 @@
 {
 private:
     std::string m_function_name;
+    ScriptedCommandSynchronicity m_synchro;
     
 public:
     
     CommandObjectPythonFunction (CommandInterpreter &interpreter,
                                  std::string name,
-                                 std::string funct) :
+                                 std::string funct,
+                                 ScriptedCommandSynchronicity synch) :
     CommandObject (interpreter,
                    name.c_str(),
                    (std::string("Run Python function ") + funct).c_str(),
                    NULL),
-    m_function_name(funct)
+    m_function_name(funct),
+    m_synchro(synch)
     {
         ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
         if (scripter)
@@ -1188,6 +1191,7 @@
         
         if (!scripter || scripter->RunScriptBasedCommand(m_function_name.c_str(),
                                                          raw_command_line,
+                                                         m_synchro,
                                                          result,
                                                          error) == false)
         {
@@ -1205,7 +1209,7 @@
     {
         return true;
     }
-    
+
     bool
     Execute (Args& command,
              CommandReturnObject &result)
@@ -1214,9 +1218,24 @@
         command.GetCommandString(cmd_string);
         return ExecuteRawCommandString(cmd_string.c_str(), result);
     }
-    
+
     virtual bool
-    IsRemovable() { return true; }
+    IsRemovable ()
+    {
+        return true;
+    }
+
+    const std::string&
+    GetFunctionName ()
+    {
+        return m_function_name;
+    }
+
+    ScriptedCommandSynchronicity
+    GetSynchronicity ()
+    {
+        return m_synchro;
+    }
     
 };
 
@@ -1226,18 +1245,81 @@
 
 class CommandObjectCommandsScriptImport : public CommandObject
 {
+private:
+    
+    class CommandOptions : public Options
+    {
+    public:
+        
+        CommandOptions (CommandInterpreter &interpreter) :
+        Options (interpreter)
+        {
+        }
+        
+        virtual
+        ~CommandOptions (){}
+        
+        virtual Error
+        SetOptionValue (uint32_t option_idx, const char *option_arg)
+        {
+            Error error;
+            char short_option = (char) m_getopt_table[option_idx].val;
+            
+            switch (short_option)
+            {
+                case 'r':
+                    m_allow_reload = true;
+                    break;
+                default:
+                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
+                    break;
+            }
+            
+            return error;
+        }
+        
+        void
+        OptionParsingStarting ()
+        {
+            m_allow_reload = false;
+        }
+        
+        const OptionDefinition*
+        GetDefinitions ()
+        {
+            return g_option_table;
+        }
+        
+        // Options table: Required for subclasses of Options.
+        
+        static OptionDefinition g_option_table[];
+        
+        // Instance variables to hold the values for command options.
+        
+        bool m_allow_reload;
+    };
+    
+    CommandOptions m_options;
+    
+    virtual Options *
+    GetOptions ()
+    {
+        return &m_options;
+    }
+
 public:
     CommandObjectCommandsScriptImport (CommandInterpreter &interpreter) :
     CommandObject (interpreter,
                    "command script import",
                    "Import a scripting module in LLDB.",
-                   NULL)
+                   NULL),
+    m_options(interpreter)
     {
         CommandArgumentEntry arg1;
         CommandArgumentData cmd_arg;
         
         // Define the first (and only) variant of this arg.
-        cmd_arg.arg_type = eArgTypePath;
+        cmd_arg.arg_type = eArgTypeFilename;
         cmd_arg.arg_repetition = eArgRepeatPlain;
         
         // There is only one variant this argument could be; put it into the argument entry.
@@ -1279,6 +1361,7 @@
         Error error;
         
         if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(path.c_str(),
+                                                                      m_options.m_allow_reload,
                                                                       error))
         {
             result.SetStatus (eReturnStatusSuccessFinishNoResult);
@@ -1291,8 +1374,40 @@
         
         return result.Succeeded();
     }
+    
+    int
+    HandleArgumentCompletion (Args &input,
+                              int &cursor_index,
+                              int &cursor_char_position,
+                              OptionElementVector &opt_element_vector,
+                              int match_start_point,
+                              int max_return_elements,
+                              bool &word_complete,
+                              StringList &matches)
+    {
+        std::string completion_str (input.GetArgumentAtIndex(cursor_index));
+        completion_str.erase (cursor_char_position);
+        
+        CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 
+                                                             CommandCompletions::eDiskFileCompletion,
+                                                             completion_str.c_str(),
+                                                             match_start_point,
+                                                             max_return_elements,
+                                                             NULL,
+                                                             word_complete,
+                                                             matches);
+        return matches.GetSize();
+    }
 };
 
+OptionDefinition
+CommandObjectCommandsScriptImport::CommandOptions::g_option_table[] =
+{
+    { LLDB_OPT_SET_1, false, "allow-reload", 'r', no_argument, NULL, 0, eArgTypeNone,        "Allow the script to be loaded even if it was already loaded before (for Python, the __lldb_init_module function will be called again, but the module will not be reloaded from disk)."},
+    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
+};
+
+
 //-------------------------------------------------------------------------
 // CommandObjectCommandsScriptAdd
 //-------------------------------------------------------------------------
@@ -1324,6 +1439,11 @@
                 case 'f':
                     m_funct_name = std::string(option_arg);
                     break;
+                case 's':
+                    m_synchronous = (ScriptedCommandSynchronicity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
+                    if (!error.Success())
+                        error.SetErrorStringWithFormat ("unrecognized value for synchronicity '%s'", option_arg);
+                    break;
                 default:
                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
                     break;
@@ -1336,6 +1456,7 @@
         OptionParsingStarting ()
         {
             m_funct_name = "";
+            m_synchronous = eScriptedCommandSynchronicitySynchronous;
         }
         
         const OptionDefinition*
@@ -1351,6 +1472,7 @@
         // Instance variables to hold the values for command options.
         
         std::string m_funct_name;
+        ScriptedCommandSynchronicity m_synchronous;
     };
     
     CommandOptions m_options;
@@ -1366,15 +1488,18 @@
     private:
         CommandInterpreter& m_interpreter;
         std::string m_cmd_name;
+        ScriptedCommandSynchronicity m_synchronous;
         StringList m_user_input;
         DISALLOW_COPY_AND_ASSIGN (PythonAliasReader);
     public:
         PythonAliasReader(Debugger& debugger,
                           CommandInterpreter& interpreter,
-                          std::string cmd_name) : 
+                          std::string cmd_name,
+                          ScriptedCommandSynchronicity synch) : 
         InputReaderEZ(debugger),
         m_interpreter(interpreter),
         m_cmd_name(cmd_name),
+        m_synchronous(synch),
         m_user_input()
         {}
         
@@ -1427,7 +1552,7 @@
             data.reader.SetIsDone (true);
             if (!batch_mode)
             {
-                out_stream->Printf ("Warning: No command attached to breakpoint.\n");
+                out_stream->Printf ("Warning: No script attached.\n");
                 out_stream->Flush();
             }
         }
@@ -1442,7 +1567,7 @@
             ScriptInterpreter *interpreter = data.reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
             if (!interpreter)
             {
-                out_stream->Printf ("Internal error #1: no script attached.\n");
+                out_stream->Printf ("Script interpreter missing: no script attached.\n");
                 out_stream->Flush();
                 return;
             }
@@ -1450,20 +1575,20 @@
             if (!interpreter->GenerateScriptAliasFunction (m_user_input, 
                                                            funct_name_sl))
             {
-                out_stream->Printf ("Internal error #2: no script attached.\n");
+                out_stream->Printf ("Unable to create function: 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->Printf ("Unable to obtain a function name: 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->Printf ("Invalid function name: no script attached.\n");
                 out_stream->Flush();
                 return;
             }
@@ -1472,11 +1597,12 @@
             
             CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(m_interpreter,
                                                                            m_cmd_name,
-                                                                           funct_name));
+                                                                           funct_name,
+                                                                           m_synchronous));
             
-            if (!m_interpreter.AddUserCommand(m_cmd_name.c_str(), command_obj_sp, true))
+            if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, true))
             {
-                out_stream->Printf ("Internal error #5: no script attached.\n");
+                out_stream->Printf ("Unable to add selected command: no script attached.\n");
                 out_stream->Flush();
                 return;
             }
@@ -1539,7 +1665,8 @@
         {
             InputReaderSP reader_sp (new PythonAliasReader (m_interpreter.GetDebugger(),
                                                             m_interpreter,
-                                                            cmd_name));
+                                                            cmd_name,
+                                                            m_options.m_synchronous));
             
             if (reader_sp)
             {
@@ -1566,8 +1693,11 @@
         }
         else
         {
-            CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter, cmd_name, m_options.m_funct_name));
-            if (m_interpreter.AddUserCommand(cmd_name.c_str(), new_cmd, true))
+            CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter,
+                                                                    cmd_name,
+                                                                    m_options.m_funct_name,
+                                                                    m_options.m_synchronous));
+            if (m_interpreter.AddUserCommand(cmd_name, new_cmd, true))
             {
                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
             }
@@ -1583,10 +1713,19 @@
     }
 };
 
+static OptionEnumValueElement g_script_synchro_type[] =
+{
+    { eScriptedCommandSynchronicitySynchronous,      "synchronous",       "Run synchronous"},
+    { eScriptedCommandSynchronicityAsynchronous,     "asynchronous",      "Run asynchronous"},
+    { eScriptedCommandSynchronicityCurrentValue,     "current",           "Do not alter current setting"},
+    { 0, NULL, NULL }
+};
+
 OptionDefinition
 CommandObjectCommandsScriptAdd::CommandOptions::g_option_table[] =
 {
     { LLDB_OPT_SET_1, false, "function", 'f', required_argument, NULL, 0, eArgTypePythonFunction,        "Name of the Python function to bind to this command name."},
+    { LLDB_OPT_SET_1, false, "synchronicity", 's', required_argument, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity,        "Set the synchronicity of this command's executions with regard to LLDB event system."},
     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
 };
 
@@ -1597,7 +1736,7 @@
 class CommandObjectCommandsScriptList : public CommandObject
 {
 private:
-        
+
 public:
     CommandObjectCommandsScriptList(CommandInterpreter &interpreter) :
     CommandObject (interpreter,

Modified: lldb/trunk/source/Commands/CommandObjectType.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectType.cpp?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectType.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectType.cpp Mon Nov  7 16:57:04 2011
@@ -1189,12 +1189,6 @@
             return false;
         }
     }
-    else
-    {
-        result.AppendError("added to types, but not given a name");
-        result.SetStatus(eReturnStatusFailed);
-        return false;
-    }
     
     return result.Succeeded();
 }
@@ -1286,12 +1280,6 @@
             return false;
         }
     }
-    else
-    {
-        result.AppendError("added to types, but not given a name");
-        result.SetStatus(eReturnStatusFailed);
-        return false;
-    }
     
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
     return result.Succeeded();

Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Mon Nov  7 16:57:04 2011
@@ -516,19 +516,23 @@
 }
 
 bool
-CommandInterpreter::AddUserCommand (const char *name, 
+CommandInterpreter::AddUserCommand (std::string name, 
                                     const lldb::CommandObjectSP &cmd_sp,
                                     bool can_replace)
 {
-    if (name && name[0])
+    if (!name.empty())
     {
-        std::string name_sstr(name);
-        if (!can_replace)
-        {
-            if (m_user_dict.find (name_sstr) != m_user_dict.end())
-                return false;
-        }
-        m_user_dict[name_sstr] = cmd_sp;
+        
+        const char* name_cstr = name.c_str();
+        
+        // do not allow replacement of internal commands
+        if (CommandExists(name_cstr))
+            return false;
+        
+        if (can_replace == false && UserCommandExists(name_cstr))
+            return false;
+
+        m_user_dict[name] = cmd_sp;
         return true;
     }
     return false;

Modified: lldb/trunk/source/Interpreter/CommandObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandObject.cpp?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandObject.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandObject.cpp Mon Nov  7 16:57:04 2011
@@ -881,6 +881,7 @@
     { eArgTypeRegularExpression, "regular-expression", CommandCompletions::eNoCompletion, { NULL, false }, "A regular expression." },
     { eArgTypeRunArgs, "run-args", CommandCompletions::eNoCompletion, { NULL, false }, "Arguments to be passed to the target program when it starts executing." },
     { eArgTypeRunMode, "run-mode", CommandCompletions::eNoCompletion, { NULL, false }, "Help text goes here." },
+    { eArgTypeScriptedCommandSynchronicity, "script-cmd-synchronicity", CommandCompletions::eNoCompletion, { NULL, false }, "The synchronicity to use to run scripted commands with regard to LLDB event system." },
     { eArgTypeScriptLang, "script-language", CommandCompletions::eNoCompletion, { NULL, false }, "The scripting language to be used for script-based commands.  Currently only Python is valid." },
     { eArgTypeSearchWord, "search-word", CommandCompletions::eNoCompletion, { NULL, false }, "The word for which you wish to search for information about." },
     { eArgTypeSelector, "selector", CommandCompletions::eNoCompletion, { NULL, false }, "An Objective-C selector name." },

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Mon Nov  7 16:57:04 2011
@@ -1745,6 +1745,7 @@
 
 bool
 ScriptInterpreterPython::LoadScriptingModule (const char* pathname,
+                                              bool can_reload,
                                               lldb_private::Error& error)
 {
     if (!pathname || !pathname[0])
@@ -1802,8 +1803,9 @@
         int refcount = 0;
         // this call will fail if the module does not exist (because the parameter to it is not a string
         // but an actual Python module object, which is non-existant if the module was not imported before)
-        if (ExecuteOneLineWithReturn(command_stream.GetData(),
-                                                         ScriptInterpreterPython::eScriptReturnTypeInt, &refcount) && refcount > 0)
+        bool was_imported = (ExecuteOneLineWithReturn(command_stream.GetData(),
+                                                      ScriptInterpreterPython::eScriptReturnTypeInt, &refcount) && refcount > 0);
+        if (was_imported == true && can_reload == false)
         {
             error.SetErrorString("module already imported");
             return false;
@@ -1831,9 +1833,28 @@
     }
 }
 
+ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp,
+                                                                     ScriptedCommandSynchronicity synchro) :
+    m_debugger_sp(debugger_sp),
+    m_synch_wanted(synchro),
+    m_old_asynch(debugger_sp->GetAsyncExecution())
+{
+    if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
+        m_debugger_sp->SetAsyncExecution(false);
+    else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
+        m_debugger_sp->SetAsyncExecution(true);
+}
+
+ScriptInterpreterPython::SynchronicityHandler::~SynchronicityHandler()
+{
+    if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
+        m_debugger_sp->SetAsyncExecution(m_old_asynch);
+}
+
 bool
 ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function,
                                                const char* args,
+                                               ScriptedCommandSynchronicity synchronicity,
                                                lldb_private::CommandReturnObject& cmd_retobj,
                                                Error& error)
 {
@@ -1850,13 +1871,22 @@
     }
     
     lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().GetSP();
+
+    if (!debugger_sp.get())
+    {
+        error.SetErrorString("invalid Debugger pointer");
+        return false;
+    }
     
     bool ret_val;
     
     std::string err_msg;
-    
+
     {
         Locker py_lock(this);
+        SynchronicityHandler synch_handler(debugger_sp,
+                                           synchronicity);
+        
         ret_val = g_swig_call_command       (impl_function,
                                              m_dictionary_name.c_str(),
                                              debugger_sp,
@@ -1864,17 +1894,13 @@
                                              err_msg,
                                              cmd_retobj);
     }
-    
+
     if (!ret_val)
         error.SetErrorString(err_msg.c_str());
     else
         error.Clear();
-        
-    return ret_val;
-
-    
-    return true;
     
+    return ret_val;
 }
 
 // in Python, a special attribute __doc__ contains the docstring

Modified: lldb/trunk/test/functionalities/command_script/TestCommandScript.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/TestCommandScript.py?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/command_script/TestCommandScript.py (original)
+++ lldb/trunk/test/functionalities/command_script/TestCommandScript.py Mon Nov  7 16:57:04 2011
@@ -33,6 +33,10 @@
             self.runCmd('command script delete welcome', check=False)
             self.runCmd('command script delete targetname', check=False)
             self.runCmd('command script delete longwait', check=False)
+            self.runCmd('command script delete mysto', check=False)
+            self.runCmd('command script delete tell_sync', check=False)
+            self.runCmd('command script delete tell_async', check=False)
+            self.runCmd('command script delete tell_curr', check=False)
 
         # Execute the cleanup function during test case tear down.
         self.addTearDownHook(cleanup)
@@ -83,12 +87,34 @@
         self.expect("longwait",
                     substrs = ['Done; if you saw the delays I am doing OK'])
 
+        self.runCmd("b main")
+        self.runCmd("run")
+        self.runCmd("mysto 3")
+        self.expect("frame variable array",
+                    substrs = ['[0] = 79630','[1] = 388785018','[2] = 0'])
+        self.runCmd("mysto 3")
+        self.expect("frame variable array",
+                    substrs = ['[0] = 79630','[4] = 388785018','[5] = 0'])
+
+# we cannot use the stepover command to check for async execution mode since LLDB
+# seems to get confused when events start to queue up
+        self.expect("tell_sync",
+                    substrs = ['running sync'])
+        self.expect("tell_async",
+                    substrs = ['running async'])
+        self.expect("tell_curr",
+                    substrs = ['I am running','sync'])
+
+
         self.runCmd("command script clear")
 
         self.expect('command script list', matching=False,
                     substrs = ['targetname',
                                'longwait'])
 
+        self.expect('command script add -f foobar frame', error=True,
+                    substrs = ['cannot add command'])
+
 if __name__ == '__main__':
     import atexit
     lldb.SBDebugger.Initialize()

Modified: lldb/trunk/test/functionalities/command_script/import/TestImport.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/import/TestImport.py?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/command_script/import/TestImport.py (original)
+++ lldb/trunk/test/functionalities/command_script/import/TestImport.py Mon Nov  7 16:57:04 2011
@@ -48,6 +48,7 @@
         self.runCmd("script import dummymodule")
         self.expect("command script import ./dummymodule.py",
                 error=True, startstr='error: module importing failed')
+        self.runCmd("command script import --allow-reload ./dummymodule.py")
 
         self.runCmd("command script add -f foo.foo_function foocmd")
         self.runCmd("command script add -f foobar.foo_function foobarcmd")

Modified: lldb/trunk/test/functionalities/command_script/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/main.cpp?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/command_script/main.cpp (original)
+++ lldb/trunk/test/functionalities/command_script/main.cpp Mon Nov  7 16:57:04 2011
@@ -52,11 +52,18 @@
 main(int argc, char const *argv[])
 {
 
-    int array[3];
+    int array[9];
+	memset(array,0,9*sizeof(int));
 
     array[0] = foo (1238, 78392);
     array[1] = foo (379265, 23674);
     array[2] = foo (872934, 234);
+    array[3] = foo (1238, 78392);
+    array[4] = foo (379265, 23674);
+    array[5] = foo (872934, 234);
+    array[6] = foo (1238, 78392);
+    array[7] = foo (379265, 23674);
+    array[8] = foo (872934, 234);
 
     return 0;
 }

Added: lldb/trunk/test/functionalities/command_script/mysto.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/mysto.py?rev=144035&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/command_script/mysto.py (added)
+++ lldb/trunk/test/functionalities/command_script/mysto.py Mon Nov  7 16:57:04 2011
@@ -0,0 +1,21 @@
+import lldb
+import sys
+import os
+import time
+
+def StepOver(debugger, args, result, dict):
+	"""
+	Step over a given number of times instead of only just once
+	"""
+	arg_split = args.split(" ")
+	print type(arg_split)
+	count = int(arg_split[0])
+	for i in range(0,count):
+		lldb.thread.StepOver(lldb.eOnlyThisThread)
+		print "step<%d>"%i
+
+def __lldb_init_module(debugger, session_dict):
+	# by default, --synchronicity is set to synchronous
+	debugger.HandleCommand("command script add -f mysto.StepOver mysto")
+	return None
+

Modified: lldb/trunk/test/functionalities/command_script/py_import
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/py_import?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/command_script/py_import (original)
+++ lldb/trunk/test/functionalities/command_script/py_import Mon Nov  7 16:57:04 2011
@@ -4,3 +4,7 @@
 command script add welcome --function welcome.welcome_impl
 command script add targetname --function welcome.target_name_impl
 command script add longwait --function welcome.print_wait_impl
+command script import mysto.py --allow-reload
+command script add tell_sync --function welcome.check_for_synchro --synchronicity sync
+command script add tell_async --function welcome.check_for_synchro --synchronicity async
+command script add tell_curr --function welcome.check_for_synchro --synchronicity curr

Modified: lldb/trunk/test/functionalities/command_script/welcome.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/command_script/welcome.py?rev=144035&r1=144034&r2=144035&view=diff
==============================================================================
--- lldb/trunk/test/functionalities/command_script/welcome.py (original)
+++ lldb/trunk/test/functionalities/command_script/welcome.py Mon Nov  7 16:57:04 2011
@@ -24,4 +24,11 @@
     print 'Still doing long task..';
     time.sleep(1)
     result.PutCString('Done; if you saw the delays I am doing OK')
-    return None
\ No newline at end of file
+    return None
+
+def check_for_synchro(debugger, args, result, dict):
+    if debugger.GetAsync() == True:
+        result.PutCString('I am running async')
+    if debugger.GetAsync() == False:
+        result.PutCString('I am running sync')
+    return None





More information about the lldb-commits mailing list