Sean Callanan | 65dafa8 | 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" |
| 23 | #include "lldb/Core/StreamString.h" |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 24 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 26 | #include "lldb/Expression/ClangExpressionParser.h" |
| 27 | #include "lldb/Expression/ClangFunction.h" |
| 28 | #include "lldb/Expression/ASTResultSynthesizer.h" |
| 29 | #include "lldb/Expression/ClangUserExpression.h" |
| 30 | #include "lldb/Host/Host.h" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 31 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 32 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame^] | 33 | #include "lldb/Target/Process.h" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 34 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Target.h" |
| 36 | |
| 37 | using namespace lldb_private; |
| 38 | |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 39 | ClangUserExpression::ClangUserExpression (const char *expr, |
| 40 | const char *expr_prefix) : |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 41 | m_expr_text(expr), |
Johnny Chen | b4c0f02 | 2010-10-29 20:19:44 +0000 | [diff] [blame] | 42 | m_expr_prefix(expr_prefix ? expr_prefix : ""), |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 43 | m_transformed_text(), |
| 44 | m_jit_addr(LLDB_INVALID_ADDRESS), |
| 45 | m_cplusplus(false), |
| 46 | m_objectivec(false), |
| 47 | m_needs_object_ptr(false) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 48 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 51 | ClangUserExpression::~ClangUserExpression () |
| 52 | { |
| 53 | } |
| 54 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 55 | clang::ASTConsumer * |
| 56 | ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough) |
| 57 | { |
| 58 | return new ASTResultSynthesizer(passthrough); |
| 59 | } |
| 60 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 61 | void |
| 62 | ClangUserExpression::ScanContext(ExecutionContext &exe_ctx) |
| 63 | { |
| 64 | if (!exe_ctx.frame) |
| 65 | return; |
| 66 | |
| 67 | VariableList *vars = exe_ctx.frame->GetVariableList(false); |
| 68 | |
| 69 | if (!vars) |
| 70 | return; |
| 71 | |
| 72 | if (vars->FindVariable(ConstString("this")).get()) |
| 73 | m_cplusplus = true; |
| 74 | else if (vars->FindVariable(ConstString("self")).get()) |
| 75 | m_objectivec = true; |
| 76 | } |
| 77 | |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 78 | // This is a really nasty hack, meant to fix Objective-C expressions of the form |
| 79 | // (int)[myArray count]. Right now, because the type information for count is |
| 80 | // not available, [myArray count] returns id, which can't be directly cast to |
| 81 | // int without causing a clang error. |
| 82 | static void |
| 83 | ApplyObjcCastHack(std::string &expr) |
| 84 | { |
| 85 | #define OBJC_CAST_HACK_FROM "(int)[" |
| 86 | #define OBJC_CAST_HACK_TO "(int)(long long)[" |
| 87 | |
| 88 | size_t from_offset; |
| 89 | |
| 90 | while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos) |
| 91 | expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO); |
| 92 | |
| 93 | #undef OBJC_CAST_HACK_TO |
| 94 | #undef OBJC_CAST_HACK_FROM |
| 95 | } |
| 96 | |
Sean Callanan | 3089237 | 2010-10-24 20:45:49 +0000 | [diff] [blame] | 97 | // Another hack, meant to allow use of unichar despite it not being available in |
| 98 | // the type information. Although we could special-case it in type lookup, |
| 99 | // hopefully we'll figure out a way to #include the same environment as is |
| 100 | // present in the original source file rather than try to hack specific type |
| 101 | // definitions in as needed. |
| 102 | static void |
| 103 | ApplyUnicharHack(std::string &expr) |
| 104 | { |
| 105 | #define UNICHAR_HACK_FROM "unichar" |
| 106 | #define UNICHAR_HACK_TO "unsigned short" |
| 107 | |
| 108 | size_t from_offset; |
| 109 | |
| 110 | while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos) |
| 111 | expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO); |
| 112 | |
| 113 | #undef UNICHAR_HACK_TO |
| 114 | #undef UNICHAR_HACK_FROM |
| 115 | } |
| 116 | |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 117 | bool |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 118 | ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx) |
| 119 | { |
| 120 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 121 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 122 | ScanContext(exe_ctx); |
| 123 | |
| 124 | StreamString m_transformed_stream; |
| 125 | |
| 126 | //////////////////////////////////// |
| 127 | // Generate the expression |
| 128 | // |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 129 | |
| 130 | ApplyObjcCastHack(m_expr_text); |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 131 | //ApplyUnicharHack(m_expr_text); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 132 | |
| 133 | if (m_cplusplus) |
| 134 | { |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 135 | m_transformed_stream.Printf("%s \n" |
| 136 | "typedef unsigned short unichar; \n" |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 137 | "void \n" |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 138 | "$__lldb_class::%s(void *$__lldb_arg) \n" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 139 | "{ \n" |
| 140 | " %s; \n" |
| 141 | "} \n", |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 142 | m_expr_prefix.c_str(), |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 143 | FunctionName(), |
| 144 | m_expr_text.c_str()); |
| 145 | |
| 146 | m_needs_object_ptr = true; |
| 147 | } |
| 148 | else |
| 149 | { |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 150 | m_transformed_stream.Printf("%s \n" |
| 151 | "typedef unsigned short unichar;\n" |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 152 | "void \n" |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 153 | "%s(void *$__lldb_arg) \n" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 154 | "{ \n" |
| 155 | " %s; \n" |
| 156 | "} \n", |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 157 | m_expr_prefix.c_str(), |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 158 | FunctionName(), |
| 159 | m_expr_text.c_str()); |
| 160 | } |
| 161 | |
| 162 | m_transformed_text = m_transformed_stream.GetData(); |
| 163 | |
| 164 | |
| 165 | if (log) |
| 166 | log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); |
| 167 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 168 | //////////////////////////////////// |
| 169 | // Set up the target and compiler |
| 170 | // |
| 171 | |
| 172 | Target *target = exe_ctx.target; |
| 173 | |
| 174 | if (!target) |
| 175 | { |
| 176 | error_stream.PutCString ("error: invalid target\n"); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | ConstString target_triple; |
| 181 | |
| 182 | target->GetTargetTriple (target_triple); |
| 183 | |
| 184 | if (!target_triple) |
| 185 | target_triple = Host::GetTargetTriple (); |
| 186 | |
| 187 | if (!target_triple) |
| 188 | { |
| 189 | error_stream.PutCString ("error: invalid target triple\n"); |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | ////////////////////////// |
| 194 | // Parse the expression |
| 195 | // |
| 196 | |
| 197 | m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx)); |
| 198 | |
| 199 | ClangExpressionParser parser(target_triple.GetCString(), *this); |
| 200 | |
| 201 | unsigned num_errors = parser.Parse (error_stream); |
| 202 | |
| 203 | if (num_errors) |
| 204 | { |
| 205 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | /////////////////////////////////////////////// |
| 210 | // Convert the output of the parser to DWARF |
| 211 | // |
| 212 | |
| 213 | m_dwarf_opcodes.reset(new StreamString); |
| 214 | m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost); |
| 215 | m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary); |
| 216 | |
| 217 | m_local_variables.reset(new ClangExpressionVariableStore()); |
| 218 | |
| 219 | Error dwarf_error = parser.MakeDWARF (); |
| 220 | |
| 221 | if (dwarf_error.Success()) |
| 222 | { |
| 223 | if (log) |
| 224 | log->Printf("Code can be interpreted."); |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | ////////////////////////////////// |
| 230 | // JIT the output of the parser |
| 231 | // |
| 232 | |
| 233 | m_dwarf_opcodes.reset(); |
| 234 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 235 | lldb::addr_t jit_end; |
| 236 | |
| 237 | Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 238 | |
| 239 | if (jit_error.Success()) |
| 240 | { |
| 241 | if (log) |
| 242 | { |
| 243 | log->Printf("Code can be run in the target."); |
| 244 | |
| 245 | StreamString disassembly_stream; |
| 246 | |
| 247 | Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx); |
| 248 | |
| 249 | if (!err.Success()) |
| 250 | { |
| 251 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | bool |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 269 | ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | ab06af9 | 2010-10-19 23:57:21 +0000 | [diff] [blame] | 270 | ExecutionContext &exe_ctx, |
| 271 | lldb::addr_t &struct_address, |
| 272 | lldb::addr_t &object_ptr) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 273 | { |
| 274 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 275 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 276 | if (m_jit_addr != LLDB_INVALID_ADDRESS) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 277 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 278 | |
| 279 | Error materialize_error; |
| 280 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 281 | |
| 282 | if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error))) |
| 283 | { |
| 284 | error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString()); |
| 285 | return false; |
| 286 | } |
| 287 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 288 | if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error)) |
| 289 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 290 | error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | |
| 294 | if (log) |
| 295 | { |
| 296 | log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 297 | |
| 298 | if (m_needs_object_ptr) |
| 299 | log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr); |
| 300 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 301 | log->Printf("Structure address : 0x%llx", (uint64_t)struct_address); |
| 302 | |
| 303 | StreamString args; |
| 304 | |
| 305 | Error dump_error; |
| 306 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 307 | if (struct_address) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 308 | { |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 309 | if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error)) |
| 310 | { |
| 311 | log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error")); |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | log->Printf("Structure contents:\n%s", args.GetData()); |
| 316 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 319 | } |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | ThreadPlan * |
| 324 | ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream, |
| 325 | ExecutionContext &exe_ctx) |
| 326 | { |
| 327 | lldb::addr_t struct_address; |
| 328 | |
| 329 | lldb::addr_t object_ptr = NULL; |
| 330 | |
| 331 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr); |
| 332 | |
| 333 | return ClangFunction::GetThreadPlanToCallFunction (exe_ctx, |
| 334 | m_jit_addr, |
| 335 | struct_address, |
| 336 | error_stream, |
| 337 | true, |
| 338 | true, |
| 339 | (m_needs_object_ptr ? &object_ptr : NULL)); |
| 340 | } |
| 341 | |
| 342 | bool |
| 343 | ClangUserExpression::FinalizeJITExecution (Stream &error_stream, |
| 344 | ExecutionContext &exe_ctx, |
| 345 | ClangExpressionVariable *&result) |
| 346 | { |
| 347 | Error expr_error; |
| 348 | |
| 349 | if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error)) |
| 350 | { |
| 351 | error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error")); |
| 352 | return false; |
| 353 | } |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | bool |
| 358 | ClangUserExpression::Execute (Stream &error_stream, |
| 359 | ExecutionContext &exe_ctx, |
| 360 | ClangExpressionVariable *&result) |
| 361 | { |
| 362 | if (m_dwarf_opcodes.get()) |
| 363 | { |
| 364 | // TODO execute the JITted opcodes |
| 365 | |
| 366 | error_stream.Printf("We don't currently support executing DWARF expressions"); |
| 367 | |
| 368 | return false; |
| 369 | } |
| 370 | else if (m_jit_addr != LLDB_INVALID_ADDRESS) |
| 371 | { |
| 372 | lldb::addr_t struct_address; |
| 373 | |
| 374 | lldb::addr_t object_ptr = NULL; |
| 375 | |
| 376 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 377 | |
| 378 | ClangFunction::ExecutionResults execution_result = |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 379 | ClangFunction::ExecuteFunction (exe_ctx, |
| 380 | m_jit_addr, |
| 381 | struct_address, |
| 382 | true, |
| 383 | true, |
Sean Callanan | 0027cec | 2010-10-07 18:17:31 +0000 | [diff] [blame] | 384 | 10000000, |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 385 | error_stream, |
| 386 | (m_needs_object_ptr ? &object_ptr : NULL)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 387 | |
| 388 | if (execution_result != ClangFunction::eExecutionCompleted) |
| 389 | { |
| 390 | const char *result_name; |
| 391 | |
| 392 | switch (execution_result) |
| 393 | { |
| 394 | case ClangFunction::eExecutionCompleted: |
| 395 | result_name = "eExecutionCompleted"; |
| 396 | break; |
| 397 | case ClangFunction::eExecutionDiscarded: |
| 398 | result_name = "eExecutionDiscarded"; |
| 399 | break; |
| 400 | case ClangFunction::eExecutionInterrupted: |
| 401 | result_name = "eExecutionInterrupted"; |
| 402 | break; |
| 403 | case ClangFunction::eExecutionSetupError: |
| 404 | result_name = "eExecutionSetupError"; |
| 405 | break; |
| 406 | case ClangFunction::eExecutionTimedOut: |
| 407 | result_name = "eExecutionTimedOut"; |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | error_stream.Printf ("Couldn't execute function; result was %s\n", result_name); |
| 412 | return false; |
| 413 | } |
| 414 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 415 | return FinalizeJITExecution (error_stream, exe_ctx, result); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | { |
| 419 | error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present"); |
| 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | StreamString & |
| 425 | ClangUserExpression::DwarfOpcodeStream () |
| 426 | { |
| 427 | if (!m_dwarf_opcodes.get()) |
| 428 | m_dwarf_opcodes.reset(new StreamString()); |
| 429 | |
| 430 | return *m_dwarf_opcodes.get(); |
| 431 | } |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 432 | |
| 433 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 434 | lldb::ValueObjectSP |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 435 | ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, |
| 436 | const char *expr_cstr, |
| 437 | const char *expr_prefix) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 438 | { |
| 439 | Error error; |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 440 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame^] | 441 | |
| 442 | if (exe_ctx.process == NULL) |
| 443 | return result_valobj_sp; |
| 444 | |
| 445 | if (!exe_ctx.process->GetDynamicCheckers()) |
| 446 | { |
| 447 | DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions(); |
| 448 | |
| 449 | StreamString install_errors; |
| 450 | |
| 451 | if (!dynamic_checkers->Install(install_errors, exe_ctx)) |
| 452 | return result_valobj_sp; |
| 453 | |
| 454 | exe_ctx.process->SetDynamicCheckers(dynamic_checkers); |
| 455 | } |
| 456 | |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 457 | ClangUserExpression user_expression (expr_cstr, expr_prefix); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 458 | |
| 459 | StreamString error_stream; |
| 460 | |
| 461 | if (!user_expression.Parse (error_stream, exe_ctx)) |
| 462 | { |
| 463 | if (error_stream.GetString().empty()) |
| 464 | error.SetErrorString ("expression failed to parse, unknown error"); |
| 465 | else |
| 466 | error.SetErrorString (error_stream.GetString().c_str()); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | ClangExpressionVariable *expr_result = NULL; |
| 471 | |
| 472 | error_stream.GetString().clear(); |
| 473 | |
| 474 | if (!user_expression.Execute (error_stream, exe_ctx, expr_result)) |
| 475 | { |
| 476 | if (error_stream.GetString().empty()) |
| 477 | error.SetErrorString ("expression failed to execute, unknown error"); |
| 478 | else |
| 479 | error.SetErrorString (error_stream.GetString().c_str()); |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | // TODO: seems weird to get a pointer to a result object back from |
| 484 | // a function. Do we own it? Feels like we do, but from looking at the |
| 485 | // code we don't. Might be best to make this a reference and state |
| 486 | // explicitly that we don't own it when we get a reference back from |
| 487 | // the execute? |
| 488 | if (expr_result) |
| 489 | { |
| 490 | result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx); |
| 491 | } |
| 492 | else |
| 493 | { |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 494 | error.SetErrorString ("Expression did not return a result"); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 498 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 499 | if (result_valobj_sp.get() == NULL) |
| 500 | result_valobj_sp.reset (new ValueObjectConstResult (error)); |
| 501 | |
| 502 | return result_valobj_sp; |
Johnny Chen | b4c0f02 | 2010-10-29 20:19:44 +0000 | [diff] [blame] | 503 | } |