[Lldb-commits] [lldb] r223711 - Add the ability for an SBValue to create a persisted version of itself.

Enrico Granata egranata at apple.com
Mon Dec 8 15:13:57 PST 2014


Author: enrico
Date: Mon Dec  8 17:13:56 2014
New Revision: 223711

URL: http://llvm.org/viewvc/llvm-project?rev=223711&view=rev
Log:
Add the ability for an SBValue to create a persisted version of itself.
Such a persisted version is equivalent to evaluating the value via the expression evaluator, and holding on to the $n result of the expression, except this API can be used on SBValues that do not obviously come from an expression (e.g. are the result of a memory lookup)

Expose this via SBValue::Persist() in our public API layer, and ValueObject::Persist() in the lldb_private layer

Includes testcase

Fixes rdar://19136664

Added:
    lldb/trunk/test/python_api/sbvalue_persist/
    lldb/trunk/test/python_api/sbvalue_persist/Makefile
    lldb/trunk/test/python_api/sbvalue_persist/TestSBValuePersist.py
    lldb/trunk/test/python_api/sbvalue_persist/main.cpp
Modified:
    lldb/trunk/include/lldb/API/SBValue.h
    lldb/trunk/include/lldb/Core/ValueObject.h
    lldb/trunk/include/lldb/Core/ValueObjectConstResult.h
    lldb/trunk/include/lldb/Expression/ClangExpressionVariable.h
    lldb/trunk/scripts/Python/interface/SBValue.i
    lldb/trunk/source/API/SBValue.cpp
    lldb/trunk/source/Core/ValueObject.cpp
    lldb/trunk/source/Core/ValueObjectConstResult.cpp
    lldb/trunk/source/Expression/ClangExpressionVariable.cpp

Modified: lldb/trunk/include/lldb/API/SBValue.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBValue.h?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBValue.h (original)
+++ lldb/trunk/include/lldb/API/SBValue.h Mon Dec  8 17:13:56 2014
@@ -353,6 +353,9 @@ public:
     
     lldb::SBType
     GetType();
+    
+    lldb::SBValue
+    Persist ();
 
     bool
     GetDescription (lldb::SBStream &description);

Modified: lldb/trunk/include/lldb/Core/ValueObject.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ValueObject.h?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ValueObject.h (original)
+++ lldb/trunk/include/lldb/Core/ValueObject.h Mon Dec  8 17:13:56 2014
@@ -814,6 +814,9 @@ public:
                     const DumpValueObjectOptions& options);
 
 
+    lldb::ValueObjectSP
+    Persist ();
+    
     // returns true if this is a char* or a char[]
     // if it is a char* and check_pointer is true,
     // it also checks that the pointer is valid

Modified: lldb/trunk/include/lldb/Core/ValueObjectConstResult.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ValueObjectConstResult.h?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ValueObjectConstResult.h (original)
+++ lldb/trunk/include/lldb/Core/ValueObjectConstResult.h Mon Dec  8 17:13:56 2014
@@ -59,7 +59,8 @@ public:
     static lldb::ValueObjectSP
     Create (ExecutionContextScope *exe_scope,
             Value &value,
-            const ConstString &name);
+            const ConstString &name,
+            Module* module = nullptr);
 
     // When an expression fails to evaluate, we return an error
     static lldb::ValueObjectSP
@@ -172,7 +173,8 @@ private:
 
     ValueObjectConstResult (ExecutionContextScope *exe_scope,
                             const Value &value,
-                            const ConstString &name);
+                            const ConstString &name,
+                            Module* module = nullptr);
 
     ValueObjectConstResult (ExecutionContextScope *exe_scope,
                             const Error& error);

Modified: lldb/trunk/include/lldb/Expression/ClangExpressionVariable.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/ClangExpressionVariable.h?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/ClangExpressionVariable.h (original)
+++ lldb/trunk/include/lldb/Expression/ClangExpressionVariable.h Mon Dec  8 17:13:56 2014
@@ -65,6 +65,11 @@ class ClangExpressionVariable
 public:
     ClangExpressionVariable(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size);
 
+    ClangExpressionVariable (ExecutionContextScope *exe_scope,
+                             Value &value,
+                             const ConstString &name,
+                             uint16_t flags = EVNone);
+    
     ClangExpressionVariable(const lldb::ValueObjectSP &valobj_sp);
 
     //----------------------------------------------------------------------

Modified: lldb/trunk/scripts/Python/interface/SBValue.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/interface/SBValue.i?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/interface/SBValue.i (original)
+++ lldb/trunk/scripts/Python/interface/SBValue.i Mon Dec  8 17:13:56 2014
@@ -413,6 +413,9 @@ public:
 	lldb::SBAddress
 	GetAddress();
     
+    lldb::SBValue
+    Persist ();
+             
     %feature("docstring", "Returns an expression path for this value."
     ) GetExpressionPath;
     bool

Modified: lldb/trunk/source/API/SBValue.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBValue.cpp?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/source/API/SBValue.cpp (original)
+++ lldb/trunk/source/API/SBValue.cpp Mon Dec  8 17:13:56 2014
@@ -1892,3 +1892,16 @@ SBValue::WatchPointee (bool resolve_loca
         sb_watchpoint = Dereference().Watch (resolve_location, read, write, error);
     return sb_watchpoint;
 }
+
+lldb::SBValue
+SBValue::Persist ()
+{
+    ValueLocker locker;
+    lldb::ValueObjectSP value_sp(GetSP(locker));
+    SBValue persisted_sb;
+    if (value_sp)
+    {
+        persisted_sb.SetSP(value_sp->Persist());
+    }
+    return persisted_sb;
+}

Modified: lldb/trunk/source/Core/ValueObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ValueObject.cpp?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/source/Core/ValueObject.cpp (original)
+++ lldb/trunk/source/Core/ValueObject.cpp Mon Dec  8 17:13:56 2014
@@ -37,6 +37,9 @@
 #include "lldb/DataFormatters/StringPrinter.h"
 #include "lldb/DataFormatters/ValueObjectPrinter.h"
 
+#include "lldb/Expression/ClangExpressionVariable.h"
+#include "lldb/Expression/ClangPersistentVariables.h"
+
 #include "lldb/Host/Endian.h"
 
 #include "lldb/Interpreter/CommandInterpreter.h"
@@ -4180,3 +4183,26 @@ ValueObject::CanProvideValue ()
 {
     return (false == GetClangType().IsAggregateType());
 }
+
+ValueObjectSP
+ValueObject::Persist ()
+{
+    if (!UpdateValueIfNeeded())
+        return nullptr;
+    
+    TargetSP target_sp(GetTargetSP());
+    if (!target_sp)
+        return nullptr;
+    
+    ConstString name(target_sp->GetPersistentVariables().GetNextPersistentVariableName());
+    
+    ClangExpressionVariableSP clang_var_sp(new ClangExpressionVariable(target_sp.get(), GetValue(), name));
+    if (clang_var_sp)
+    {
+        clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
+        clang_var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
+        target_sp->GetPersistentVariables().AddVariable(clang_var_sp);
+    }
+    
+    return clang_var_sp->GetValueObject();
+}

Modified: lldb/trunk/source/Core/ValueObjectConstResult.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ValueObjectConstResult.cpp?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/source/Core/ValueObjectConstResult.cpp (original)
+++ lldb/trunk/source/Core/ValueObjectConstResult.cpp Mon Dec  8 17:13:56 2014
@@ -122,9 +122,10 @@ ValueObjectConstResult::Create (Executio
 ValueObjectSP
 ValueObjectConstResult::Create (ExecutionContextScope *exe_scope,
                                 Value &value,
-                                const ConstString &name)
+                                const ConstString &name,
+                                Module *module)
 {
-    return (new ValueObjectConstResult (exe_scope, value, name))->GetSP();
+    return (new ValueObjectConstResult (exe_scope, value, name, module))->GetSP();
 }
 
 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
@@ -222,15 +223,18 @@ ValueObjectConstResult::ValueObjectConst
 
 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
                                                 const Value &value,
-                                                const ConstString &name) :
+                                                const ConstString &name,
+                                                Module *module) :
     ValueObject (exe_scope),
     m_type_name (),
     m_byte_size (0),
     m_impl(this)
 {
     m_value = value;
-    m_value.GetData(m_data);
     m_name = name;
+    ExecutionContext exe_ctx;
+    exe_scope->CalculateExecutionContext(exe_ctx);
+    m_error = m_value.GetValueAsData(&exe_ctx, m_data, 0, module);
 }
 
 ValueObjectConstResult::~ValueObjectConstResult()

Modified: lldb/trunk/source/Expression/ClangExpressionVariable.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/ClangExpressionVariable.cpp?rev=223711&r1=223710&r2=223711&view=diff
==============================================================================
--- lldb/trunk/source/Expression/ClangExpressionVariable.cpp (original)
+++ lldb/trunk/source/Expression/ClangExpressionVariable.cpp Mon Dec  8 17:13:56 2014
@@ -28,6 +28,17 @@ ClangExpressionVariable::ClangExpression
 {
 }
 
+ClangExpressionVariable::ClangExpressionVariable (ExecutionContextScope *exe_scope,
+                                                  Value &value,
+                                                  const ConstString &name,
+                                                  uint16_t flags) :
+    m_parser_vars(),
+    m_jit_vars (),
+    m_flags (flags),
+    m_frozen_sp (ValueObjectConstResult::Create (exe_scope, value, name))
+{
+}
+
 ClangExpressionVariable::ClangExpressionVariable (const lldb::ValueObjectSP &valobj_sp) :
     m_parser_vars(),
     m_jit_vars (),

Added: lldb/trunk/test/python_api/sbvalue_persist/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/sbvalue_persist/Makefile?rev=223711&view=auto
==============================================================================
--- lldb/trunk/test/python_api/sbvalue_persist/Makefile (added)
+++ lldb/trunk/test/python_api/sbvalue_persist/Makefile Mon Dec  8 17:13:56 2014
@@ -0,0 +1,8 @@
+LEVEL = ../../make
+
+CXX_SOURCES := main.cpp
+
+# Clean renamed executable on 'make clean'
+clean: OBJECTS+=no_synth
+
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/test/python_api/sbvalue_persist/TestSBValuePersist.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/sbvalue_persist/TestSBValuePersist.py?rev=223711&view=auto
==============================================================================
--- lldb/trunk/test/python_api/sbvalue_persist/TestSBValuePersist.py (added)
+++ lldb/trunk/test/python_api/sbvalue_persist/TestSBValuePersist.py Mon Dec  8 17:13:56 2014
@@ -0,0 +1,94 @@
+"""Test SBValue::Persist"""
+
+import os, sys, time
+import unittest2
+import lldb
+from lldbtest import *
+import lldbutil
+
+class SBValuePersistTestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    @python_api_test
+    @dsym_test
+    def test_with_dsym(self):
+        """Test SBValue::Persist"""
+        self.buildDsym()
+        self.setTearDownCleanup()
+        self.doTest()
+
+    @python_api_test
+    @dwarf_test
+    def test_with_dwarf(self):
+        """Test SBValue::Persist"""
+        self.buildDwarf()
+        self.setTearDownCleanup()
+        self.doTest()
+
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+
+    def doTest(self):
+        """Test SBValue::Persist"""
+        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
+
+        lldbutil.run_break_set_by_source_regexp (self, "break here")
+
+        self.runCmd("run", RUN_SUCCEEDED)
+
+        # The stop reason of the thread should be breakpoint.
+        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
+            substrs = ['stopped',
+                       'stop reason = breakpoint'])
+        
+        # This is the function to remove the custom formats in order to have a
+        # clean slate for the next test case.
+        def cleanup():
+            self.runCmd('type format clear', check=False)
+            self.runCmd('type summary clear', check=False)
+            self.runCmd('type filter clear', check=False)
+            self.runCmd('type synthetic clear', check=False)
+
+        # Execute the cleanup function during test case tear down.
+        self.addTearDownHook(cleanup)
+
+        foo = self.frame().FindVariable("foo")
+        bar = self.frame().FindVariable("bar")
+        baz = self.frame().FindVariable("baz")
+
+        self.assertTrue(foo.IsValid(), "foo is not valid")
+        self.assertTrue(bar.IsValid(), "bar is not valid")
+        self.assertTrue(baz.IsValid(), "baz is not valid")
+        
+        fooPersist = foo.Persist()
+        barPersist = bar.Persist()
+        bazPersist = baz.Persist()
+
+        self.assertTrue(fooPersist.IsValid(), "fooPersist is not valid")
+        self.assertTrue(barPersist.IsValid(), "barPersist is not valid")
+        self.assertTrue(bazPersist.IsValid(), "bazPersist is not valid")
+
+        self.assertTrue(fooPersist.GetValueAsUnsigned(0) == 10, "fooPersist != 10")
+        self.assertTrue(barPersist.GetPointeeData().sint32[0] == 4, "barPersist != 4")
+        self.assertTrue(bazPersist.GetSummary() == '"85"', "bazPersist != 85")
+        
+        self.runCmd("continue")
+
+        self.assertTrue(fooPersist.IsValid(), "fooPersist is not valid")
+        self.assertTrue(barPersist.IsValid(), "barPersist is not valid")
+        self.assertTrue(bazPersist.IsValid(), "bazPersist is not valid")
+
+        self.assertTrue(fooPersist.GetValueAsUnsigned(0) == 10, "fooPersist != 10")
+        self.assertTrue(barPersist.GetPointeeData().sint32[0] == 4, "barPersist != 4")
+        self.assertTrue(bazPersist.GetSummary() == '"85"', "bazPersist != 85")
+        
+        self.expect("expr *(%s)" % (barPersist.GetName()), substrs = ['= 4'])
+
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()

Added: lldb/trunk/test/python_api/sbvalue_persist/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/sbvalue_persist/main.cpp?rev=223711&view=auto
==============================================================================
--- lldb/trunk/test/python_api/sbvalue_persist/main.cpp (added)
+++ lldb/trunk/test/python_api/sbvalue_persist/main.cpp Mon Dec  8 17:13:56 2014
@@ -0,0 +1,14 @@
+#include <vector>
+#include <string>
+
+void f() {}
+
+int main() {
+    int foo = 10;
+    int *bar = new int(4);
+    std::string baz = "85";
+    
+    f(); // break here
+    f(); // break here
+    return 0;
+}
\ No newline at end of file





More information about the lldb-commits mailing list