[Lldb-commits] [lldb] r373135 - Give an error when StepUsingScriptedThreadPlan is passed a bad classname.

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 27 17:53:45 PDT 2019


Author: jingham
Date: Fri Sep 27 17:53:45 2019
New Revision: 373135

URL: http://llvm.org/viewvc/llvm-project?rev=373135&view=rev
Log:
Give an error when StepUsingScriptedThreadPlan is passed a bad classname.

Differential Revision: https://reviews.llvm.org/D68173

Modified:
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
    lldb/trunk/include/lldb/Target/ThreadPlanPython.h
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/step_scripted/TestStepScripted.py
    lldb/trunk/scripts/Python/python-wrapper.swig
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
    lldb/trunk/source/Target/ThreadPlanPython.cpp
    lldb/trunk/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h Fri Sep 27 17:53:45 2019
@@ -208,6 +208,7 @@ public:
 
   virtual StructuredData::ObjectSP
   CreateScriptedThreadPlan(const char *class_name,
+                           std::string &error_str,
                            lldb::ThreadPlanSP thread_plan_sp) {
     return StructuredData::ObjectSP();
   }

Modified: lldb/trunk/include/lldb/Target/ThreadPlanPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/ThreadPlanPython.h?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/ThreadPlanPython.h (original)
+++ lldb/trunk/include/lldb/Target/ThreadPlanPython.h Fri Sep 27 17:53:45 2019
@@ -55,6 +55,7 @@ protected:
 
 private:
   std::string m_class_name;
+  std::string m_error_str;
   StructuredData::ObjectSP m_implementation_sp;
   bool m_did_push;
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/step_scripted/TestStepScripted.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/step_scripted/TestStepScripted.py?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/step_scripted/TestStepScripted.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/step_scripted/TestStepScripted.py Fri Sep 27 17:53:45 2019
@@ -26,10 +26,13 @@ class StepScriptedTestCase(TestBase):
 
     def setUp(self):
         TestBase.setUp(self)
+        self.main_source_file = lldb.SBFileSpec("main.c")
         self.runCmd("command script import Steps.py")
 
     def step_out_with_scripted_plan(self, name):
-        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, "Set a breakpoint here", self.main_source_file)
+        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
+                                                                            "Set a breakpoint here",
+                                                                            self.main_source_file)
 
         frame = thread.GetFrameAtIndex(0)
         self.assertEqual("foo", frame.GetFunctionName())
@@ -39,3 +42,21 @@ class StepScriptedTestCase(TestBase):
 
         frame = thread.GetFrameAtIndex(0)
         self.assertEqual("main", frame.GetFunctionName())
+
+    def test_misspelled_plan_name(self):
+        """Test that we get a useful error if we misspell the plan class name"""
+        self.build()
+        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
+                                                                            "Set a breakpoint here",
+                                                                            self.main_source_file)
+        stop_id = process.GetStopID()
+        # Pass a non-existent class for the plan class:
+        err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan")
+        
+        # Make sure we got a good error:
+        self.assertTrue(err.Fail(), "We got a failure state")
+        msg = err.GetCString()
+        self.assertTrue("NoSuchModule.NoSuchPlan" in msg, "Mentioned missing class")
+        
+        # Make sure we didn't let the process run:
+        self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")

Modified: lldb/trunk/scripts/Python/python-wrapper.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-wrapper.swig?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-wrapper.swig (original)
+++ lldb/trunk/scripts/Python/python-wrapper.swig Fri Sep 27 17:53:45 2019
@@ -250,6 +250,7 @@ LLDBSwigPythonCreateScriptedThreadPlan
 (
     const char *python_class_name,
     const char *session_dictionary_name,
+    std::string &error_string,
     const lldb::ThreadPlanSP& thread_plan_sp
 )
 {
@@ -267,8 +268,11 @@ LLDBSwigPythonCreateScriptedThreadPlan
     auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
     auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
 
-    if (!pfunc.IsAllocated())
+    if (!pfunc.IsAllocated()) {
+        error_string.append("could not find script class: ");
+        error_string.append(python_class_name);
         return nullptr;
+    }
 
     PythonObject tp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(tp_value));
 

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Fri Sep 27 17:53:45 2019
@@ -97,6 +97,7 @@ LLDBSwigPythonCreateCommandObject(const
 
 extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(
     const char *python_class_name, const char *session_dictionary_name,
+    std::string &error_string,
     const lldb::ThreadPlanSP &thread_plan_sp);
 
 extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor,
@@ -1844,12 +1845,13 @@ StructuredData::DictionarySP ScriptInter
 }
 
 StructuredData::ObjectSP ScriptInterpreterPythonImpl::CreateScriptedThreadPlan(
-    const char *class_name, lldb::ThreadPlanSP thread_plan_sp) {
+    const char *class_name, std::string &error_str, 
+    lldb::ThreadPlanSP thread_plan_sp) {
   if (class_name == nullptr || class_name[0] == '\0')
     return StructuredData::ObjectSP();
 
   if (!thread_plan_sp.get())
-    return StructuredData::ObjectSP();
+    return {};
 
   Debugger &debugger = thread_plan_sp->GetTarget().GetDebugger();
   ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
@@ -1857,17 +1859,18 @@ StructuredData::ObjectSP ScriptInterpret
       static_cast<ScriptInterpreterPythonImpl *>(script_interpreter);
 
   if (!script_interpreter)
-    return StructuredData::ObjectSP();
+    return {};
 
   void *ret_val;
 
   {
     Locker py_lock(this,
                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-
     ret_val = LLDBSwigPythonCreateScriptedThreadPlan(
         class_name, python_interpreter->m_dictionary_name.c_str(),
-        thread_plan_sp);
+        error_str, thread_plan_sp);
+    if (!ret_val)
+      return {};
   }
 
   return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h Fri Sep 27 17:53:45 2019
@@ -78,6 +78,7 @@ public:
 
   StructuredData::ObjectSP
   CreateScriptedThreadPlan(const char *class_name,
+                           std::string &error_str,
                            lldb::ThreadPlanSP thread_plan) override;
 
   bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp,

Modified: lldb/trunk/source/Target/ThreadPlanPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ThreadPlanPython.cpp?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/source/Target/ThreadPlanPython.cpp (original)
+++ lldb/trunk/source/Target/ThreadPlanPython.cpp Fri Sep 27 17:53:45 2019
@@ -45,7 +45,9 @@ bool ThreadPlanPython::ValidatePlan(Stre
 
   if (!m_implementation_sp) {
     if (error)
-      error->Printf("Python thread plan does not have an implementation");
+      error->Printf("Error constructing Python ThreadPlan: %s",
+          m_error_str.empty() ? "<unknown error>"
+                                : m_error_str.c_str());
     return false;
   }
 
@@ -63,7 +65,7 @@ void ThreadPlanPython::DidPush() {
                                            .GetScriptInterpreter();
     if (script_interp) {
       m_implementation_sp = script_interp->CreateScriptedThreadPlan(
-          m_class_name.c_str(), this->shared_from_this());
+          m_class_name.c_str(), m_error_str, this->shared_from_this());
     }
   }
 }

Modified: lldb/trunk/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp?rev=373135&r1=373134&r2=373135&view=diff
==============================================================================
--- lldb/trunk/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp (original)
+++ lldb/trunk/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp Fri Sep 27 17:53:45 2019
@@ -95,6 +95,7 @@ LLDBSwigPythonCreateCommandObject(const
 
 extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(
     const char *python_class_name, const char *session_dictionary_name,
+    std::string &error_string,
     const lldb::ThreadPlanSP &thread_plan_sp) {
   return nullptr;
 }




More information about the lldb-commits mailing list