Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
| 11 | #include <stdio.h> |
| 12 | #if HAVE_SYS_TYPES_H |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | #include <cstdlib> |
| 18 | #include <string> |
| 19 | #include <map> |
| 20 | |
| 21 | #include "lldb/Core/ConstString.h" |
| 22 | #include "lldb/Core/Log.h" |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 24 | #include "lldb/Core/StreamString.h" |
Greg Clayton | b71f384 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 25 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/ASTResultSynthesizer.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 28 | #include "lldb/Expression/ClangExpressionParser.h" |
| 29 | #include "lldb/Expression/ClangFunction.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 30 | #include "lldb/Expression/ClangUserExpression.h" |
| 31 | #include "lldb/Host/Host.h" |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 32 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 33 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 34 | #include "lldb/Target/Process.h" |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 35 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 36 | #include "lldb/Target/Target.h" |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 37 | #include "lldb/Target/ThreadPlan.h" |
| 38 | #include "lldb/Target/ThreadPlanCallUserExpression.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace lldb_private; |
| 41 | |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 42 | ClangUserExpression::ClangUserExpression (const char *expr, |
| 43 | const char *expr_prefix) : |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 44 | ClangExpression (), |
| 45 | m_expr_text (expr), |
| 46 | m_expr_prefix (expr_prefix ? expr_prefix : ""), |
| 47 | m_transformed_text (), |
Stephen Wilson | 71c21d1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 48 | m_desired_type (NULL, NULL), |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 49 | m_cplusplus (false), |
| 50 | m_objectivec (false), |
| 51 | m_needs_object_ptr (false), |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 52 | m_const_object (false), |
| 53 | m_const_result () |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 54 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 57 | ClangUserExpression::~ClangUserExpression () |
| 58 | { |
| 59 | } |
| 60 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 61 | clang::ASTConsumer * |
| 62 | ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough) |
| 63 | { |
Sean Callanan | f7c3e27 | 2010-11-19 02:52:21 +0000 | [diff] [blame] | 64 | return new ASTResultSynthesizer(passthrough, |
| 65 | m_desired_type); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 68 | void |
| 69 | ClangUserExpression::ScanContext(ExecutionContext &exe_ctx) |
| 70 | { |
| 71 | if (!exe_ctx.frame) |
| 72 | return; |
| 73 | |
| 74 | VariableList *vars = exe_ctx.frame->GetVariableList(false); |
| 75 | |
| 76 | if (!vars) |
| 77 | return; |
| 78 | |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 79 | lldb::VariableSP this_var(vars->FindVariable(ConstString("this"))); |
| 80 | lldb::VariableSP self_var(vars->FindVariable(ConstString("self"))); |
| 81 | |
| 82 | if (this_var.get()) |
| 83 | { |
| 84 | Type *this_type = this_var->GetType(); |
| 85 | |
| 86 | lldb::clang_type_t pointer_target_type; |
| 87 | |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 88 | if (ClangASTContext::IsPointerType(this_type->GetClangForwardType(), |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 89 | &pointer_target_type)) |
| 90 | { |
| 91 | TypeFromUser target_ast_type(pointer_target_type, this_type->GetClangAST()); |
| 92 | |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 93 | if (ClangASTContext::IsCXXClassType(target_ast_type.GetOpaqueQualType())) |
Sean Callanan | 9d2127a | 2011-01-24 08:11:45 +0000 | [diff] [blame] | 94 | { |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 95 | m_cplusplus = true; |
| 96 | |
Sean Callanan | 9d2127a | 2011-01-24 08:11:45 +0000 | [diff] [blame] | 97 | if (target_ast_type.IsConst()) |
| 98 | m_const_object = true; |
| 99 | } |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | else if (self_var.get()) |
| 103 | { |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 104 | m_objectivec = true; |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 105 | } |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Sean Callanan | cf5498f | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 108 | // This is a really nasty hack, meant to fix Objective-C expressions of the form |
| 109 | // (int)[myArray count]. Right now, because the type information for count is |
| 110 | // not available, [myArray count] returns id, which can't be directly cast to |
| 111 | // int without causing a clang error. |
| 112 | static void |
| 113 | ApplyObjcCastHack(std::string &expr) |
| 114 | { |
| 115 | #define OBJC_CAST_HACK_FROM "(int)[" |
| 116 | #define OBJC_CAST_HACK_TO "(int)(long long)[" |
| 117 | |
| 118 | size_t from_offset; |
| 119 | |
| 120 | while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos) |
| 121 | expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO); |
| 122 | |
| 123 | #undef OBJC_CAST_HACK_TO |
| 124 | #undef OBJC_CAST_HACK_FROM |
| 125 | } |
| 126 | |
Sean Callanan | 64186e7 | 2010-10-24 20:45:49 +0000 | [diff] [blame] | 127 | // Another hack, meant to allow use of unichar despite it not being available in |
| 128 | // the type information. Although we could special-case it in type lookup, |
| 129 | // hopefully we'll figure out a way to #include the same environment as is |
| 130 | // present in the original source file rather than try to hack specific type |
| 131 | // definitions in as needed. |
| 132 | static void |
| 133 | ApplyUnicharHack(std::string &expr) |
| 134 | { |
| 135 | #define UNICHAR_HACK_FROM "unichar" |
| 136 | #define UNICHAR_HACK_TO "unsigned short" |
| 137 | |
| 138 | size_t from_offset; |
| 139 | |
| 140 | while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos) |
| 141 | expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO); |
| 142 | |
| 143 | #undef UNICHAR_HACK_TO |
| 144 | #undef UNICHAR_HACK_FROM |
| 145 | } |
| 146 | |
Sean Callanan | cf5498f | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 147 | bool |
Sean Callanan | f7c3e27 | 2010-11-19 02:52:21 +0000 | [diff] [blame] | 148 | ClangUserExpression::Parse (Stream &error_stream, |
| 149 | ExecutionContext &exe_ctx, |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 150 | TypeFromUser desired_type, |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 151 | bool keep_result_in_memory) |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 152 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 153 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 154 | |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 155 | ScanContext(exe_ctx); |
| 156 | |
| 157 | StreamString m_transformed_stream; |
| 158 | |
| 159 | //////////////////////////////////// |
| 160 | // Generate the expression |
| 161 | // |
Sean Callanan | cf5498f | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 162 | |
| 163 | ApplyObjcCastHack(m_expr_text); |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 164 | //ApplyUnicharHack(m_expr_text); |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 165 | |
| 166 | if (m_cplusplus) |
| 167 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 168 | m_transformed_stream.Printf("%s \n" |
| 169 | "typedef unsigned short unichar; \n" |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 170 | "void \n" |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 171 | "$__lldb_class::%s(void *$__lldb_arg) %s\n" |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 172 | "{ \n" |
| 173 | " %s; \n" |
| 174 | "} \n", |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 175 | m_expr_prefix.c_str(), |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 176 | FunctionName(), |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 177 | (m_const_object ? "const" : ""), |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 178 | m_expr_text.c_str()); |
| 179 | |
| 180 | m_needs_object_ptr = true; |
| 181 | } |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 182 | else if(m_objectivec) |
| 183 | { |
| 184 | const char *function_name = FunctionName(); |
| 185 | |
| 186 | m_transformed_stream.Printf("%s \n" |
Greg Clayton | f028a1f | 2010-12-17 02:26:24 +0000 | [diff] [blame] | 187 | "typedef unsigned short unichar; \n" |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 188 | "@interface $__lldb_objc_class ($__lldb_category) \n" |
| 189 | "-(void)%s:(void *)$__lldb_arg; \n" |
| 190 | "@end \n" |
| 191 | "@implementation $__lldb_objc_class ($__lldb_category) \n" |
| 192 | "-(void)%s:(void *)$__lldb_arg \n" |
| 193 | "{ \n" |
| 194 | " %s; \n" |
| 195 | "} \n" |
| 196 | "@end \n", |
| 197 | m_expr_prefix.c_str(), |
| 198 | function_name, |
| 199 | function_name, |
| 200 | m_expr_text.c_str()); |
| 201 | |
| 202 | m_needs_object_ptr = true; |
| 203 | } |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 204 | else |
| 205 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 206 | m_transformed_stream.Printf("%s \n" |
| 207 | "typedef unsigned short unichar;\n" |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 208 | "void \n" |
Sean Callanan | cf5498f | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 209 | "%s(void *$__lldb_arg) \n" |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 210 | "{ \n" |
| 211 | " %s; \n" |
| 212 | "} \n", |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 213 | m_expr_prefix.c_str(), |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 214 | FunctionName(), |
| 215 | m_expr_text.c_str()); |
| 216 | } |
| 217 | |
| 218 | m_transformed_text = m_transformed_stream.GetData(); |
| 219 | |
| 220 | |
| 221 | if (log) |
| 222 | log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); |
| 223 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 224 | //////////////////////////////////// |
| 225 | // Set up the target and compiler |
| 226 | // |
| 227 | |
| 228 | Target *target = exe_ctx.target; |
| 229 | |
| 230 | if (!target) |
| 231 | { |
| 232 | error_stream.PutCString ("error: invalid target\n"); |
| 233 | return false; |
| 234 | } |
| 235 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 236 | ////////////////////////// |
| 237 | // Parse the expression |
| 238 | // |
| 239 | |
Sean Callanan | f7c3e27 | 2010-11-19 02:52:21 +0000 | [diff] [blame] | 240 | m_desired_type = desired_type; |
| 241 | |
Sean Callanan | 92adcac | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 242 | m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory)); |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 243 | |
| 244 | m_expr_decl_map->WillParse(exe_ctx); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 245 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 246 | ClangExpressionParser parser(exe_ctx.process, *this); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 247 | |
| 248 | unsigned num_errors = parser.Parse (error_stream); |
| 249 | |
| 250 | if (num_errors) |
| 251 | { |
| 252 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 253 | |
| 254 | m_expr_decl_map->DidParse(); |
| 255 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
| 259 | /////////////////////////////////////////////// |
| 260 | // Convert the output of the parser to DWARF |
| 261 | // |
| 262 | |
| 263 | m_dwarf_opcodes.reset(new StreamString); |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 264 | m_dwarf_opcodes->SetByteOrder (lldb::endian::InlHostByteOrder()); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 265 | m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary); |
| 266 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 267 | m_local_variables.reset(new ClangExpressionVariableList()); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 268 | |
| 269 | Error dwarf_error = parser.MakeDWARF (); |
| 270 | |
| 271 | if (dwarf_error.Success()) |
| 272 | { |
| 273 | if (log) |
| 274 | log->Printf("Code can be interpreted."); |
| 275 | |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 276 | m_expr_decl_map->DidParse(); |
| 277 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | |
| 281 | ////////////////////////////////// |
| 282 | // JIT the output of the parser |
| 283 | // |
| 284 | |
| 285 | m_dwarf_opcodes.reset(); |
| 286 | |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 287 | Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx, m_const_result, true); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 288 | |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 289 | m_expr_decl_map->DidParse(); |
| 290 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 291 | if (jit_error.Success()) |
| 292 | { |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 293 | if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS) |
| 294 | m_jit_process_sp = exe_ctx.process->GetSP(); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
| 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | bool |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 305 | ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | 104a6e9 | 2010-10-19 23:57:21 +0000 | [diff] [blame] | 306 | ExecutionContext &exe_ctx, |
| 307 | lldb::addr_t &struct_address, |
Sean Callanan | 9d48e80 | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 308 | lldb::addr_t &object_ptr, |
| 309 | lldb::addr_t &cmd_ptr) |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 310 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 311 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 312 | |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 313 | if (m_jit_start_addr != LLDB_INVALID_ADDRESS) |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 314 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 315 | Error materialize_error; |
| 316 | |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 317 | if (m_needs_object_ptr) |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 318 | { |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 319 | ConstString object_name; |
| 320 | |
| 321 | if (m_cplusplus) |
| 322 | { |
| 323 | object_name.SetCString("this"); |
| 324 | } |
| 325 | else if (m_objectivec) |
| 326 | { |
| 327 | object_name.SetCString("self"); |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | error_stream.Printf("Need object pointer but don't know the language\n"); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error))) |
| 336 | { |
| 337 | error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString()); |
| 338 | return false; |
| 339 | } |
Sean Callanan | 9d48e80 | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 340 | |
| 341 | if (m_objectivec) |
| 342 | { |
| 343 | ConstString cmd_name("_cmd"); |
| 344 | |
| 345 | if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true))) |
| 346 | { |
| 347 | error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString()); |
| 348 | return false; |
| 349 | } |
| 350 | } |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 353 | if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error)) |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 354 | { |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 355 | error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString()); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 356 | return false; |
| 357 | } |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 358 | |
| 359 | #if 0 |
| 360 | // jingham: look here |
| 361 | StreamFile logfile ("/tmp/exprs.txt", "a"); |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 362 | logfile.Printf("0x%16.16llx: thread = 0x%4.4x, expr = '%s'\n", m_jit_start_addr, exe_ctx.thread ? exe_ctx.thread->GetID() : -1, m_expr_text.c_str()); |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 363 | #endif |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 364 | |
| 365 | if (log) |
| 366 | { |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 367 | log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --"); |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 368 | |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 369 | log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr); |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 370 | |
| 371 | if (m_needs_object_ptr) |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 372 | log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr); |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 373 | |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 374 | log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 375 | |
| 376 | StreamString args; |
| 377 | |
| 378 | Error dump_error; |
| 379 | |
Sean Callanan | 9e6ed53 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 380 | if (struct_address) |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 381 | { |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 382 | if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error)) |
Sean Callanan | 9e6ed53 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 383 | { |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 384 | log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error")); |
Sean Callanan | 9e6ed53 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 385 | } |
| 386 | else |
| 387 | { |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 388 | log->Printf(" Structure contents:\n%s", args.GetData()); |
Sean Callanan | 9e6ed53 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 389 | } |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | ThreadPlan * |
| 397 | ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | 92adcac | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 398 | ExecutionContext &exe_ctx) |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 399 | { |
| 400 | lldb::addr_t struct_address; |
| 401 | |
| 402 | lldb::addr_t object_ptr = NULL; |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 403 | lldb::addr_t cmd_ptr = NULL; |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 404 | |
Sean Callanan | 9d48e80 | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 405 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr); |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 406 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 407 | // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the |
| 408 | // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 409 | // forcing unwind_on_error to be true here, in practical terms that can't happen. |
| 410 | |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 411 | return ClangFunction::GetThreadPlanToCallFunction (exe_ctx, |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 412 | m_jit_start_addr, |
Sean Callanan | 1d47caf | 2010-12-01 01:28:23 +0000 | [diff] [blame] | 413 | struct_address, |
| 414 | error_stream, |
| 415 | true, |
| 416 | true, |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 417 | (m_needs_object_ptr ? &object_ptr : NULL), |
| 418 | (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL); |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | bool |
| 422 | ClangUserExpression::FinalizeJITExecution (Stream &error_stream, |
| 423 | ExecutionContext &exe_ctx, |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 424 | lldb::ClangExpressionVariableSP &result) |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 425 | { |
| 426 | Error expr_error; |
| 427 | |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 428 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 429 | |
| 430 | if (log) |
| 431 | { |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 432 | log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --"); |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 433 | |
| 434 | StreamString args; |
| 435 | |
| 436 | Error dump_error; |
| 437 | |
| 438 | if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error)) |
| 439 | { |
| 440 | log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error")); |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | log->Printf(" Structure contents:\n%s", args.GetData()); |
| 445 | } |
| 446 | } |
| 447 | |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 448 | if (!m_expr_decl_map->Dematerialize(exe_ctx, result, expr_error)) |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 449 | { |
| 450 | error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error")); |
| 451 | return false; |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 456 | ExecutionResults |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 457 | ClangUserExpression::Execute (Stream &error_stream, |
| 458 | ExecutionContext &exe_ctx, |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 459 | bool discard_on_error, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 460 | ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me, |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 461 | lldb::ClangExpressionVariableSP &result) |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 462 | { |
Jim Ingham | b086ff7 | 2011-01-18 22:20:08 +0000 | [diff] [blame] | 463 | // The expression log is quite verbose, and if you're just tracking the execution of the |
| 464 | // expression, it's quite convenient to have these logs come out with the STEP log as well. |
| 465 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 466 | |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 467 | if (m_dwarf_opcodes.get()) |
| 468 | { |
| 469 | // TODO execute the JITted opcodes |
| 470 | |
| 471 | error_stream.Printf("We don't currently support executing DWARF expressions"); |
| 472 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 473 | return eExecutionSetupError; |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 474 | } |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 475 | else if (m_jit_start_addr != LLDB_INVALID_ADDRESS) |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 476 | { |
| 477 | lldb::addr_t struct_address; |
| 478 | |
| 479 | lldb::addr_t object_ptr = NULL; |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 480 | lldb::addr_t cmd_ptr = NULL; |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 481 | |
Sean Callanan | 9d48e80 | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 482 | if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr)) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 483 | return eExecutionSetupError; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 484 | |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 485 | const bool stop_others = true; |
| 486 | const bool try_all_threads = true; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 487 | |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 488 | Address wrapper_address (NULL, m_jit_start_addr); |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 489 | lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread), |
| 490 | wrapper_address, |
| 491 | struct_address, |
| 492 | stop_others, |
| 493 | discard_on_error, |
| 494 | (m_needs_object_ptr ? &object_ptr : NULL), |
| 495 | ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL), |
| 496 | shared_ptr_to_me)); |
| 497 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 498 | if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL)) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 499 | return eExecutionSetupError; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 500 | |
| 501 | call_plan_sp->SetPrivate(true); |
| 502 | |
Greg Clayton | e2d4f0d | 2011-01-19 07:54:15 +0000 | [diff] [blame] | 503 | uint32_t single_thread_timeout_usec = 500000; |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 504 | |
| 505 | if (log) |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 506 | log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --"); |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 507 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 508 | ExecutionResults execution_result = exe_ctx.process->RunThreadPlan (exe_ctx, |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 509 | call_plan_sp, |
| 510 | stop_others, |
| 511 | try_all_threads, |
| 512 | discard_on_error, |
| 513 | single_thread_timeout_usec, |
| 514 | error_stream); |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 515 | |
| 516 | if (log) |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 517 | log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --"); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 518 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 519 | if (execution_result == eExecutionInterrupted) |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 520 | { |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 521 | if (discard_on_error) |
| 522 | error_stream.Printf ("Expression execution was interrupted. The process has been returned to the state before execution."); |
| 523 | else |
| 524 | error_stream.Printf ("Expression execution was interrupted. The process has been left at the point where it was interrupted."); |
| 525 | |
| 526 | return execution_result; |
| 527 | } |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 528 | else if (execution_result != eExecutionCompleted) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 529 | { |
| 530 | error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result)); |
| 531 | return execution_result; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 534 | if (FinalizeJITExecution (error_stream, exe_ctx, result)) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 535 | return eExecutionCompleted; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 536 | else |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 537 | return eExecutionSetupError; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 538 | } |
| 539 | else |
| 540 | { |
Johnny Chen | fec456d | 2010-11-10 19:02:11 +0000 | [diff] [blame] | 541 | error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present"); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 542 | return eExecutionSetupError; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | StreamString & |
| 547 | ClangUserExpression::DwarfOpcodeStream () |
| 548 | { |
| 549 | if (!m_dwarf_opcodes.get()) |
| 550 | m_dwarf_opcodes.reset(new StreamString()); |
| 551 | |
| 552 | return *m_dwarf_opcodes.get(); |
| 553 | } |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 554 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 555 | ExecutionResults |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 556 | ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 557 | bool discard_on_error, |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 558 | const char *expr_cstr, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 559 | const char *expr_prefix, |
| 560 | lldb::ValueObjectSP &result_valobj_sp) |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 561 | { |
Jim Ingham | b086ff7 | 2011-01-18 22:20:08 +0000 | [diff] [blame] | 562 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 563 | |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 564 | Error error; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 565 | ExecutionResults execution_results = eExecutionSetupError; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 566 | |
| 567 | if (exe_ctx.process == NULL) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 568 | { |
| 569 | error.SetErrorString ("Must have a process to evaluate expressions."); |
| 570 | |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 571 | result_valobj_sp = ValueObjectConstResult::Create (NULL, error); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 572 | return eExecutionSetupError; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 575 | if (!exe_ctx.process->GetDynamicCheckers()) |
| 576 | { |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 577 | if (log) |
| 578 | log->Printf("== [ClangUserExpression::Evaluate] Installing dynamic checkers =="); |
| 579 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 580 | DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions(); |
| 581 | |
| 582 | StreamString install_errors; |
| 583 | |
| 584 | if (!dynamic_checkers->Install(install_errors, exe_ctx)) |
Sean Callanan | 2a39652 | 2010-11-05 00:57:06 +0000 | [diff] [blame] | 585 | { |
| 586 | if (install_errors.GetString().empty()) |
| 587 | error.SetErrorString ("couldn't install checkers, unknown error"); |
| 588 | else |
| 589 | error.SetErrorString (install_errors.GetString().c_str()); |
| 590 | |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 591 | result_valobj_sp = ValueObjectConstResult::Create (NULL, error); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 592 | return eExecutionSetupError; |
Sean Callanan | 2a39652 | 2010-11-05 00:57:06 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 595 | exe_ctx.process->SetDynamicCheckers(dynamic_checkers); |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 596 | |
| 597 | if (log) |
| 598 | log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers =="); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 601 | ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix)); |
| 602 | |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 603 | StreamString error_stream; |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 604 | |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 605 | if (log) |
| 606 | log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr); |
| 607 | |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 608 | if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), true)) |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 609 | { |
| 610 | if (error_stream.GetString().empty()) |
| 611 | error.SetErrorString ("expression failed to parse, unknown error"); |
| 612 | else |
| 613 | error.SetErrorString (error_stream.GetString().c_str()); |
| 614 | } |
| 615 | else |
| 616 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 617 | lldb::ClangExpressionVariableSP expr_result; |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 618 | |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 619 | if (user_expression_sp->m_const_result.get()) |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 620 | { |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 621 | if (log) |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 622 | log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant =="); |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 623 | |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame^] | 624 | result_valobj_sp = user_expression_sp->m_const_result->GetValueObject(); |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 625 | } |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 626 | else |
| 627 | { |
| 628 | error_stream.GetString().clear(); |
| 629 | |
| 630 | if (log) |
| 631 | log->Printf("== [ClangUserExpression::Evaluate] Executing expression =="); |
| 632 | |
| 633 | execution_results = user_expression_sp->Execute (error_stream, |
| 634 | exe_ctx, |
Sean Callanan | 92adcac | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 635 | discard_on_error, |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 636 | user_expression_sp, |
| 637 | expr_result); |
| 638 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 639 | if (execution_results != eExecutionCompleted) |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 640 | { |
Sean Callanan | a162eba | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 641 | if (log) |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 642 | log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally =="); |
| 643 | |
| 644 | if (error_stream.GetString().empty()) |
| 645 | error.SetErrorString ("expression failed to execute, unknown error"); |
| 646 | else |
| 647 | error.SetErrorString (error_stream.GetString().c_str()); |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 648 | } |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 649 | else |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 650 | { |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 651 | if (expr_result) |
| 652 | { |
| 653 | result_valobj_sp = expr_result->GetValueObject(); |
| 654 | |
| 655 | if (log) |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 656 | log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString()); |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 657 | } |
| 658 | else |
| 659 | { |
| 660 | if (log) |
| 661 | log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result =="); |
| 662 | |
| 663 | error.SetErrorString ("Expression did not return a result"); |
| 664 | } |
Greg Clayton | 0184f01 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | } |
Sean Callanan | c57f64d | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 668 | |
Greg Clayton | b71f384 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 669 | if (result_valobj_sp.get() == NULL) |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 670 | result_valobj_sp = ValueObjectConstResult::Create (NULL, error); |
Greg Clayton | b71f384 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 671 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 672 | return execution_results; |
Johnny Chen | dabefd0 | 2010-10-29 20:19:44 +0000 | [diff] [blame] | 673 | } |