[Lldb-commits] [lldb] r374394 - Implement serializing scripted breakpoints and their extra args.

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 10 10:44:50 PDT 2019


Author: jingham
Date: Thu Oct 10 10:44:50 2019
New Revision: 374394

URL: http://llvm.org/viewvc/llvm-project?rev=374394&view=rev
Log:
Implement serializing scripted breakpoints and their extra args.

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

Added:
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/resolver.py
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/side_effect.py
Modified:
    lldb/trunk/include/lldb/Breakpoint/BreakpointResolverScripted.h
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/TestBreakpointSerialization.py
    lldb/trunk/source/Breakpoint/BreakpointResolver.cpp
    lldb/trunk/source/Breakpoint/BreakpointResolverScripted.cpp
    lldb/trunk/source/Target/Target.cpp

Modified: lldb/trunk/include/lldb/Breakpoint/BreakpointResolverScripted.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Breakpoint/BreakpointResolverScripted.h?rev=374394&r1=374393&r2=374394&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Breakpoint/BreakpointResolverScripted.h (original)
+++ lldb/trunk/include/lldb/Breakpoint/BreakpointResolverScripted.h Thu Oct 10 10:44:50 2019
@@ -26,8 +26,7 @@ public:
   BreakpointResolverScripted(Breakpoint *bkpt,
                              const llvm::StringRef class_name,
                              lldb::SearchDepth depth,
-                             StructuredDataImpl *args_data,
-                             ScriptInterpreter &script_interp);
+                             StructuredDataImpl *args_data);
 
   ~BreakpointResolverScripted() override;
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/TestBreakpointSerialization.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/TestBreakpointSerialization.py?rev=374394&r1=374393&r2=374394&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/TestBreakpointSerialization.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/TestBreakpointSerialization.py Thu Oct 10 10:44:50 2019
@@ -47,7 +47,12 @@ class BreakpointSerialization(TestBase):
         self.build()
         self.setup_targets_and_cleanup()
         self.do_check_names()
-
+        
+    def test_scripted_extra_args(self):
+        self.build()
+        self.setup_targets_and_cleanup()
+        self.do_check_extra_args()
+        
     def setup_targets_and_cleanup(self):
         def cleanup ():
             self.RemoveTempFile(self.bkpts_file_path)
@@ -289,3 +294,85 @@ class BreakpointSerialization(TestBase):
         error = self.copy_target.BreakpointsCreateFromFile(self.bkpts_file_spec, names_list, copy_bps)
         self.assertTrue(error.Success(), "Failed reading breakpoints from file: %s"%(error.GetCString()))
         self.assertTrue(copy_bps.GetSize() == 1, "Found the matching breakpoint.")
+
+    def do_check_extra_args(self):
+
+        import side_effect
+	interp = self.dbg.GetCommandInterpreter()
+	error = lldb.SBError()
+
+	script_name = os.path.join(self.getSourceDir(), "resolver.py")
+
+        command = "command script import " + script_name
+        result = lldb.SBCommandReturnObject()
+        interp.HandleCommand(command, result)
+        self.assertTrue(result.Succeeded(), "com scr imp failed: %s"%(result.GetError()))
+
+        # First make sure a scripted breakpoint with no args works:
+        bkpt = self.orig_target.BreakpointCreateFromScript("resolver.Resolver", lldb.SBStructuredData(),
+                                                           lldb.SBFileSpecList(), lldb.SBFileSpecList())
+        self.assertTrue(bkpt.IsValid(), "Bkpt is valid")
+        write_bps = lldb.SBBreakpointList(self.orig_target)
+
+        error = self.orig_target.BreakpointsWriteToFile(self.bkpts_file_spec, write_bps)
+        self.assertTrue(error.Success(), "Failed writing breakpoints: %s"%(error.GetCString()))
+
+        side_effect.g_extra_args = None
+        copy_bps = lldb.SBBreakpointList(self.copy_target)
+        error = self.copy_target.BreakpointsCreateFromFile(self.bkpts_file_spec, copy_bps)
+        self.assertTrue(error.Success(), "Failed reading breakpoints: %s"%(error.GetCString()))
+
+        self.assertEqual(copy_bps.GetSize(), 1, "Got one breakpoint from file.")
+        no_keys = lldb.SBStringList()
+        side_effect.g_extra_args.GetKeys(no_keys)
+        self.assertEqual(no_keys.GetSize(), 0, "Should have no keys")
+
+        self.orig_target.DeleteAllBreakpoints()
+        self.copy_target.DeleteAllBreakpoints()
+
+        # Now try one with extra args:
+        
+        extra_args = lldb.SBStructuredData()
+        stream = lldb.SBStream()
+        stream.Print('{"first_arg" : "first_value", "second_arg" : "second_value"}')
+        extra_args.SetFromJSON(stream)
+        self.assertTrue(extra_args.IsValid(), "SBStructuredData is valid.")
+        
+        bkpt = self.orig_target.BreakpointCreateFromScript("resolver.Resolver",
+                                                           extra_args, lldb.SBFileSpecList(), lldb.SBFileSpecList())
+        self.assertTrue(bkpt.IsValid(), "Bkpt is valid")
+        write_bps = lldb.SBBreakpointList(self.orig_target)
+
+        error = self.orig_target.BreakpointsWriteToFile(self.bkpts_file_spec, write_bps)
+        self.assertTrue(error.Success(), "Failed writing breakpoints: %s"%(error.GetCString()))
+
+        orig_extra_args = side_effect.g_extra_args
+        self.assertTrue(orig_extra_args.IsValid(), "Extra args originally valid")
+
+        orig_keys = lldb.SBStringList()
+        orig_extra_args.GetKeys(orig_keys)
+        self.assertEqual(2, orig_keys.GetSize(), "Should have two keys")
+
+        side_effect.g_extra_args = None
+
+        copy_bps = lldb.SBBreakpointList(self.copy_target)
+        error = self.copy_target.BreakpointsCreateFromFile(self.bkpts_file_spec, copy_bps)
+        self.assertTrue(error.Success(), "Failed reading breakpoints: %s"%(error.GetCString()))
+
+        self.assertEqual(copy_bps.GetSize(), 1, "Got one breakpoint from file.")
+
+        copy_extra_args = side_effect.g_extra_args
+        copy_keys = lldb.SBStringList()
+        copy_extra_args.GetKeys(copy_keys)
+        self.assertEqual(2, copy_keys.GetSize(), "Copy should have two keys")
+
+        for idx in range(0, orig_keys.GetSize()):
+            key = orig_keys.GetStringAtIndex(idx)
+            copy_value = copy_extra_args.GetValueForKey(key).GetStringValue(100)
+
+            if key == "first_arg":
+                self.assertEqual(copy_value, "first_value")
+            elif key == "second_arg":
+                self.assertEqual(copy_value, "second_value")
+            else:
+                self.Fail("Unknown key: %s"%(key))

Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/resolver.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/resolver.py?rev=374394&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/resolver.py (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/resolver.py Thu Oct 10 10:44:50 2019
@@ -0,0 +1,17 @@
+import lldb
+import side_effect
+
+class Resolver:
+  """This resolver class is just so I can read out the extra_args."""
+  
+  def __init__(self, bkpt, extra_args, dict):
+      self.bkpt = bkpt
+      side_effect.g_extra_args = extra_args
+    
+  def __callback__(self, sym_ctx):
+      """Doesn't actually do anything."""
+      return
+
+  def get_short_help(self):
+      return "I am a python breakpoint resolver that does nothing"
+

Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/side_effect.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/side_effect.py?rev=374394&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/side_effect.py (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/breakpoint/serialize/side_effect.py Thu Oct 10 10:44:50 2019
@@ -0,0 +1 @@
+g_extra_args = None

Modified: lldb/trunk/source/Breakpoint/BreakpointResolver.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/BreakpointResolver.cpp?rev=374394&r1=374393&r2=374394&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/BreakpointResolver.cpp (original)
+++ lldb/trunk/source/Breakpoint/BreakpointResolver.cpp Thu Oct 10 10:44:50 2019
@@ -34,7 +34,8 @@ using namespace lldb;
 // BreakpointResolver:
 const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address",
                                                   "SymbolName",  "SourceRegex",
-                                                  "Exception",   "Unknown"};
+                                                  "Python",   "Exception",
+                                                  "Unknown"};
 
 const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(
     BreakpointResolver::OptionNames::LastOptionName)] = {

Modified: lldb/trunk/source/Breakpoint/BreakpointResolverScripted.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/BreakpointResolverScripted.cpp?rev=374394&r1=374393&r2=374394&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/BreakpointResolverScripted.cpp (original)
+++ lldb/trunk/source/Breakpoint/BreakpointResolverScripted.cpp Thu Oct 10 10:44:50 2019
@@ -29,8 +29,7 @@ BreakpointResolverScripted::BreakpointRe
     Breakpoint *bkpt, 
     const llvm::StringRef class_name,
     lldb::SearchDepth depth,
-    StructuredDataImpl *args_data,
-    ScriptInterpreter &script_interp)
+    StructuredDataImpl *args_data)
     : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),
       m_class_name(class_name), m_depth(depth), m_args_ptr(args_data) {
   CreateImplementationIfNeeded();
@@ -68,45 +67,25 @@ BreakpointResolverScripted::CreateFromSt
   llvm::StringRef class_name;
   bool success;
   
-  if (!bkpt)
-    return nullptr;
-
   success = options_dict.GetValueForKeyAsString(
       GetKey(OptionNames::PythonClassName), class_name);
   if (!success) {
     error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
     return nullptr;
   }
-  lldb::SearchDepth depth;
-  int depth_as_int;
-  success = options_dict.GetValueForKeyAsInteger(
-      GetKey(OptionNames::SearchDepth), depth_as_int);
-  if (!success) {
-    error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
-    return nullptr;
-  }
-  if (depth_as_int >= (int) OptionNames::LastOptionName) {
-    error.SetErrorString("BRFL::CFSD: Invalid value for search depth.");
-    return nullptr;
-  }
-  depth = (lldb::SearchDepth) depth_as_int;
+  // The Python function will actually provide the search depth, this is a
+  // placeholder.
+  lldb::SearchDepth depth = lldb::eSearchDepthTarget;
   
   StructuredDataImpl *args_data_impl = new StructuredDataImpl();
   StructuredData::Dictionary *args_dict = nullptr;
   success = options_dict.GetValueForKeyAsDictionary(
     GetKey(OptionNames::ScriptArgs), args_dict);
   if (success) {
-    // FIXME: The resolver needs a copy of the ARGS dict that it can own,
-    // so I need to make a copy constructor for the Dictionary so I can pass
-    // that to it here.  For now the args are empty.
-    //StructuredData::Dictionary *dict_copy = new StructuredData::Dictionary(args_dict);
-    
+      args_data_impl->SetObjectSP(args_dict->shared_from_this());
   }
-  ScriptInterpreter *script_interp = bkpt->GetTarget()
-                                         .GetDebugger()
-                                         .GetScriptInterpreter();
-  return new BreakpointResolverScripted(bkpt, class_name, depth, args_data_impl,
-                                      *script_interp);
+  return new BreakpointResolverScripted(bkpt, class_name, depth, 
+                                        args_data_impl);
 }
 
 StructuredData::ObjectSP
@@ -116,6 +95,10 @@ BreakpointResolverScripted::SerializeToS
 
   options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),
                                    m_class_name);
+  if (m_args_ptr->IsValid())
+      options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),
+          m_args_ptr->GetObjectSP());
+
   return WrapOptionsDict(options_dict_sp);
 }
 
@@ -171,11 +154,10 @@ void BreakpointResolverScripted::Dump(St
 
 lldb::BreakpointResolverSP
 BreakpointResolverScripted::CopyForBreakpoint(Breakpoint &breakpoint) {
-  ScriptInterpreter *script_interp = GetScriptInterpreter();
   // FIXME: Have to make a copy of the arguments from the m_args_ptr and then
   // pass that to the new resolver.
   lldb::BreakpointResolverSP ret_sp(
-      new BreakpointResolverScripted(&breakpoint, m_class_name, 
-                                   m_depth, nullptr, *script_interp));
+      new BreakpointResolverScripted(&breakpoint, m_class_name, m_depth, 
+                                     nullptr));
   return ret_sp;
 }

Modified: lldb/trunk/source/Target/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Target.cpp?rev=374394&r1=374393&r2=374394&view=diff
==============================================================================
--- lldb/trunk/source/Target/Target.cpp (original)
+++ lldb/trunk/source/Target/Target.cpp Thu Oct 10 10:44:50 2019
@@ -609,8 +609,7 @@ lldb::BreakpointSP Target::CreateScripte
     extra_args_impl->SetObjectSP(extra_args_sp);
 
   BreakpointResolverSP resolver_sp(new BreakpointResolverScripted(
-      nullptr, class_name, depth, extra_args_impl,
-      *GetDebugger().GetScriptInterpreter()));
+      nullptr, class_name, depth, extra_args_impl));
   return CreateBreakpoint(filter_sp, resolver_sp, internal, false, true);
 }
 




More information about the lldb-commits mailing list