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