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