From 4e1042e1bf97313dab801d43a6aed47204e9524a Mon Sep 17 00:00:00 2001 From: Greg Clayton Date: Wed, 27 May 2015 22:32:39 +0000 Subject: [PATCH] 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 out 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. llvm-svn: 238365 --- lldb/examples/darwin/heap_find/heap.py | 37 +++++++++++++-------- lldb/include/lldb/API/SBExpressionOptions.h | 6 ++++ lldb/include/lldb/Target/Target.h | 45 ++++++++++++++++++-------- lldb/scripts/interface/SBExpressionOptions.i | 7 ++++ lldb/source/API/SBExpressionOptions.cpp | 11 +++++++ lldb/source/Expression/ClangUserExpression.cpp | 17 +++++++++- 6 files changed, 96 insertions(+), 27 deletions(-) diff --git a/lldb/examples/darwin/heap_find/heap.py b/lldb/examples/darwin/heap_find/heap.py index 2ebff60..f13a3bb 100644 --- a/lldb/examples/darwin/heap_find/heap.py +++ b/lldb/examples/darwin/heap_find/heap.py @@ -434,7 +434,7 @@ info''' % (options.max_frames, options.max_history, addr); 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, options, arg_str_description, expr, print_no_ 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, dict): # 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, dict): # 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, options, args): 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, dict): # 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.classes, sizeof(baton.classes)/sizeof(Clas 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: diff --git a/lldb/include/lldb/API/SBExpressionOptions.h b/lldb/include/lldb/API/SBExpressionOptions.h index 6eacd28..ed2f918 100644 --- a/lldb/include/lldb/API/SBExpressionOptions.h +++ b/lldb/include/lldb/API/SBExpressionOptions.h @@ -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); diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h index edd5ffc..1681d21 100644 --- a/lldb/include/lldb/Target/Target.h +++ b/lldb/include/lldb/Target/Target.h @@ -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; diff --git a/lldb/scripts/interface/SBExpressionOptions.i b/lldb/scripts/interface/SBExpressionOptions.i index 6ee3e3b..1f423cf 100644 --- a/lldb/scripts/interface/SBExpressionOptions.i +++ b/lldb/scripts/interface/SBExpressionOptions.i @@ -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: diff --git a/lldb/source/API/SBExpressionOptions.cpp b/lldb/source/API/SBExpressionOptions.cpp index 448ff4c..43b7d03 100644 --- a/lldb/source/API/SBExpressionOptions.cpp +++ b/lldb/source/API/SBExpressionOptions.cpp @@ -185,6 +185,17 @@ SBExpressionOptions::SetSuppressPersistentResult (bool b) 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 diff --git a/lldb/source/Expression/ClangUserExpression.cpp b/lldb/source/Expression/ClangUserExpression.cpp index 2293cbb..38e6ddb 100644 --- a/lldb/source/Expression/ClangUserExpression.cpp +++ b/lldb/source/Expression/ClangUserExpression.cpp @@ -1043,7 +1043,22 @@ ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, 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; -- 2.7.4