[Lldb-commits] [lldb] r238365 - Allow expresions to have unique expression prefixes:

Greg Clayton gclayton at apple.com
Wed May 27 15:32:39 PDT 2015


Author: gclayton
Date: Wed May 27 17:32:39 2015
New Revision: 238365

URL: http://llvm.org/viewvc/llvm-project?rev=238365&view=rev
Log:
Allow expresions to have unique expression prefixes:

expr_options = lldb.SBExpressionOptions()
expr_options.SetPrefix('''
struct Foo {
   int a;
   int b;
   int c;
}
'''
expr_result = frame.EvaluateExpression ("Foo foo = { 1, 2, 3}; foo", expr_options)

This fixed a current issue with ptr_refs, cstr_refs and malloc_info so that they can work. If expressions define their own types and then return expression results that use those types, those types get copied into the target's AST context so they persist and the expression results can be still printed and used in future expressions. Code was added to the expression parser to copy the context in which types are defined if they are used as the expression results. So in the case of types defined by expressions, they get defined in a lldb_expr function and that function and _all_ of its statements get copied. Many types of statements are not supported in this copy (array subscript, lambdas, etc) so this causes expressions to fail as they can't copy the result types. To work around this issue I have added code that allows expressions to specify an expression specific prefix. Then when you evaluate the expression you can pass the "expr_options" and have types that can be correctly copied o
 ut into the target. I added this as a way to work around an issue, but I also think it is nice to be allowed to specify an expression prefix that can be reused by many expressions, so this feature is very useful.

<rdar://problem/21130675>

Modified:
    lldb/trunk/examples/darwin/heap_find/heap.py
    lldb/trunk/include/lldb/API/SBExpressionOptions.h
    lldb/trunk/include/lldb/Target/Target.h
    lldb/trunk/scripts/interface/SBExpressionOptions.i
    lldb/trunk/source/API/SBExpressionOptions.cpp
    lldb/trunk/source/Expression/ClangUserExpression.cpp

Modified: lldb/trunk/examples/darwin/heap_find/heap.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/darwin/heap_find/heap.py?rev=238365&r1=238364&r2=238365&view=diff
==============================================================================
--- lldb/trunk/examples/darwin/heap_find/heap.py (original)
+++ lldb/trunk/examples/darwin/heap_find/heap.py Wed May 27 17:32:39 2015
@@ -434,7 +434,7 @@ info''' % (options.max_frames, options.m
         result.AppendMessage('error: expression failed "%s" => %s' % (expr, expr_sbvalue.error))
 
 
-def display_match_results (result, options, arg_str_description, expr, print_no_matches = True):
+def display_match_results (result, options, arg_str_description, expr, print_no_matches, expr_prefix = None):
     frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
     if not frame:
         result.AppendMessage('error: invalid frame')
@@ -445,6 +445,8 @@ def display_match_results (result, optio
     expr_options.SetTimeoutInMicroSeconds (30*1000*1000) # 30 second timeout
     expr_options.SetTryAllThreads (False)
     expr_options.SetLanguage (lldb.eLanguageTypeObjC_plus_plus)
+    if expr_prefix:
+        expr_options.SetPrefix (expr_prefix)
     expr_sbvalue = frame.EvaluateExpression (expr, expr_options)
     if options.verbose:
         print "expression:"
@@ -621,14 +623,16 @@ def ptr_refs(debugger, command, result,
         # a member named "callback" whose type is "range_callback_t". This
         # will be used by our zone callbacks to call the range callback for
         # each malloc range.
-        user_init_code_format = '''
-#define MAX_MATCHES %u
+        expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+        user_init_code_format = '''
+#define MAX_MATCHES %u
 typedef struct callback_baton_t {
     range_callback_t callback;
     unsigned num_matches;
@@ -667,7 +671,7 @@ baton.matches'''
             user_init_code = user_init_code_format % (options.max_matches, ptr_expr)
             expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code)          
             arg_str_description = 'malloc block containing pointer %s' % ptr_expr
-            display_match_results (result, options, arg_str_description, expr)
+            display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
     else:
         result.AppendMessage('error: no pointer arguments were given')
 
@@ -713,14 +717,16 @@ def cstr_refs(debugger, command, result,
         # a member named "callback" whose type is "range_callback_t". This
         # will be used by our zone callbacks to call the range callback for
         # each malloc range.
-        user_init_code_format = '''
-#define MAX_MATCHES %u
+        expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+        user_init_code_format = '''
+#define MAX_MATCHES %u
 typedef struct callback_baton_t {
     range_callback_t callback;
     unsigned num_matches;
@@ -761,7 +767,7 @@ baton.matches'''
             user_init_code = user_init_code_format % (options.max_matches, cstr)
             expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code)          
             arg_str_description = 'malloc block containing "%s"' % cstr            
-            display_match_results (result, options, arg_str_description, expr)
+            display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
     else:
         result.AppendMessage('error: command takes one or more C string arguments')
 
@@ -798,14 +804,16 @@ def malloc_info_impl (debugger, result,
     if not frame:
         result.AppendMessage('error: invalid frame')
         return
-    
-    user_init_code_format = '''
+    expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+    
+    user_init_code_format = '''
 typedef struct callback_baton_t {
     range_callback_t callback;
     unsigned num_matches;
@@ -836,7 +844,7 @@ baton.matches[1].addr = 0;'''
             user_init_code = user_init_code_format % (ptr_expr)
             expr = get_iterate_memory_expr(options, process, user_init_code, 'baton.matches')          
             arg_str_description = 'malloc block that contains %s' % ptr_expr
-            total_matches += display_match_results (result, options, arg_str_description, expr)
+            total_matches += display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
         return total_matches
     else:
         result.AppendMessage('error: command takes one or more pointer expressions')
@@ -1012,14 +1020,17 @@ def objc_refs(debugger, command, result,
         # a member named "callback" whose type is "range_callback_t". This
         # will be used by our zone callbacks to call the range callback for
         # each malloc range.
-        user_init_code_format = '''
-#define MAX_MATCHES %u
+        expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+
+        user_init_code_format = '''
+#define MAX_MATCHES %u
 typedef int (*compare_callback_t)(const void *a, const void *b);
 typedef struct callback_baton_t {
     range_callback_t callback;
@@ -1106,7 +1117,7 @@ int nc = (int)objc_getClassList(baton.cl
                     user_init_code = user_init_code_format % (options.max_matches, num_objc_classes, isa)
                     expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code)          
                     arg_str_description = 'objective C classes with isa 0x%x' % isa
-                    display_match_results (result, options, arg_str_description, expr)
+                    display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
                 else:
                     result.AppendMessage('error: Can\'t find isa for an ObjC class named "%s"' % (class_name))
             else:

Modified: lldb/trunk/include/lldb/API/SBExpressionOptions.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBExpressionOptions.h?rev=238365&r1=238364&r2=238365&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBExpressionOptions.h (original)
+++ lldb/trunk/include/lldb/API/SBExpressionOptions.h Wed May 27 17:32:39 2015
@@ -105,6 +105,12 @@ public:
     void
     SetSuppressPersistentResult (bool b = false);
 
+    const char *
+    GetPrefix () const;
+
+    void
+    SetPrefix (const char *prefix);
+
 protected:
 
     SBExpressionOptions (lldb_private::EvaluateExpressionOptions &expression_options);

Modified: lldb/trunk/include/lldb/Target/Target.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Target.h?rev=238365&r1=238364&r2=238365&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Target.h (original)
+++ lldb/trunk/include/lldb/Target/Target.h Wed May 27 17:32:39 2015
@@ -240,19 +240,20 @@ public:
     EvaluateExpressionOptions() :
         m_execution_policy(eExecutionPolicyOnlyWhenNeeded),
         m_language (lldb::eLanguageTypeUnknown),
-        m_coerce_to_id(false),
-        m_unwind_on_error(true),
+        m_prefix (), // A prefix specific to this expression that is added after the prefix from the settings (if any)
+        m_coerce_to_id (false),
+        m_unwind_on_error (true),
         m_ignore_breakpoints (false),
-        m_keep_in_memory(false),
-        m_try_others(true),
-        m_stop_others(true),
-        m_debug(false),
-        m_trap_exceptions(true),
-        m_generate_debug_info(false),
-        m_result_is_internal(false),
-        m_use_dynamic(lldb::eNoDynamicValues),
-        m_timeout_usec(default_timeout),
-        m_one_thread_timeout_usec(0),
+        m_keep_in_memory (false),
+        m_try_others (true),
+        m_stop_others (true),
+        m_debug (false),
+        m_trap_exceptions (true),
+        m_generate_debug_info (false),
+        m_result_is_internal (false),
+        m_use_dynamic (lldb::eNoDynamicValues),
+        m_timeout_usec (default_timeout),
+        m_one_thread_timeout_usec (0),
         m_cancel_callback (nullptr),
         m_cancel_callback_baton (nullptr)
     {
@@ -287,7 +288,24 @@ public:
     {
         return m_coerce_to_id;
     }
-    
+
+    const char *
+    GetPrefix () const
+    {
+        if (m_prefix.empty())
+            return NULL;
+        return m_prefix.c_str();
+    }
+
+    void
+    SetPrefix (const char *prefix)
+    {
+        if (prefix && prefix[0])
+            m_prefix = prefix;
+        else
+            m_prefix.clear();
+    }
+
     void
     SetCoerceToId (bool coerce = true)
     {
@@ -459,6 +477,7 @@ public:
 private:
     ExecutionPolicy m_execution_policy;
     lldb::LanguageType m_language;
+    std::string m_prefix;
     bool m_coerce_to_id;
     bool m_unwind_on_error;
     bool m_ignore_breakpoints;

Modified: lldb/trunk/scripts/interface/SBExpressionOptions.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBExpressionOptions.i?rev=238365&r1=238364&r2=238365&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBExpressionOptions.i (original)
+++ lldb/trunk/scripts/interface/SBExpressionOptions.i Wed May 27 17:32:39 2015
@@ -111,6 +111,13 @@ public:
     SetSuppressPersistentResult (bool b = false);
 
 
+    %feature("docstring", "Gets the prefix to use for this expression.") GetPrefix;
+    const char *
+    GetPrefix () const;
+
+    %feature("docstring", "Sets the prefix to use for this expression. This prefix gets inserted after the 'target.expr-prefix' prefix contents, but before the wrapped expression function body.") SetPrefix;
+    void
+    SetPrefix (const char *prefix);
 
 protected:
 

Modified: lldb/trunk/source/API/SBExpressionOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBExpressionOptions.cpp?rev=238365&r1=238364&r2=238365&view=diff
==============================================================================
--- lldb/trunk/source/API/SBExpressionOptions.cpp (original)
+++ lldb/trunk/source/API/SBExpressionOptions.cpp Wed May 27 17:32:39 2015
@@ -185,6 +185,17 @@ SBExpressionOptions::SetSuppressPersiste
     return m_opaque_ap->SetResultIsInternal (b);
 }
 
+const char *
+SBExpressionOptions::GetPrefix () const
+{
+    return m_opaque_ap->GetPrefix();
+}
+
+void
+SBExpressionOptions::SetPrefix (const char *prefix)
+{
+    return m_opaque_ap->SetPrefix(prefix);
+}
 
 EvaluateExpressionOptions *
 SBExpressionOptions::get() const

Modified: lldb/trunk/source/Expression/ClangUserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/ClangUserExpression.cpp?rev=238365&r1=238364&r2=238365&view=diff
==============================================================================
--- lldb/trunk/source/Expression/ClangUserExpression.cpp (original)
+++ lldb/trunk/source/Expression/ClangUserExpression.cpp Wed May 27 17:32:39 2015
@@ -1043,7 +1043,22 @@ ClangUserExpression::Evaluate (Execution
     if (process == NULL || !process->CanJIT())
         execution_policy = eExecutionPolicyNever;
 
-    lldb::ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
+    const char *full_prefix = NULL;
+    const char *option_prefix = options.GetPrefix();
+    std::string full_prefix_storage;
+    if (expr_prefix && option_prefix)
+    {
+        full_prefix_storage.assign(expr_prefix);
+        full_prefix_storage.append(option_prefix);
+        if (!full_prefix_storage.empty())
+            full_prefix = full_prefix_storage.c_str();
+    }
+    else if (expr_prefix)
+        full_prefix = expr_prefix;
+    else
+        full_prefix = option_prefix;
+
+    lldb::ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, full_prefix, language, desired_type));
 
     StreamString error_stream;
 





More information about the lldb-commits mailing list