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 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 120 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 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 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | bool |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 251 | ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | ab06af9 | 2010-10-19 23:57:21 +0000 | [diff] [blame] | 252 | ExecutionContext &exe_ctx, |
| 253 | lldb::addr_t &struct_address, |
| 254 | lldb::addr_t &object_ptr) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 255 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 256 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 257 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 258 | if (m_jit_addr != LLDB_INVALID_ADDRESS) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 259 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 260 | |
| 261 | Error materialize_error; |
| 262 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 263 | |
| 264 | if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error))) |
| 265 | { |
| 266 | error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString()); |
| 267 | return false; |
| 268 | } |
| 269 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 270 | if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error)) |
| 271 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 272 | error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | |
| 276 | if (log) |
| 277 | { |
| 278 | log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 279 | |
| 280 | if (m_needs_object_ptr) |
| 281 | log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr); |
| 282 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 283 | log->Printf("Structure address : 0x%llx", (uint64_t)struct_address); |
| 284 | |
| 285 | StreamString args; |
| 286 | |
| 287 | Error dump_error; |
| 288 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 289 | if (struct_address) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 290 | { |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 291 | if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error)) |
| 292 | { |
| 293 | log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error")); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | log->Printf("Structure contents:\n%s", args.GetData()); |
| 298 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 301 | } |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | ThreadPlan * |
| 306 | ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream, |
| 307 | ExecutionContext &exe_ctx) |
| 308 | { |
| 309 | lldb::addr_t struct_address; |
| 310 | |
| 311 | lldb::addr_t object_ptr = NULL; |
| 312 | |
| 313 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr); |
| 314 | |
| 315 | return ClangFunction::GetThreadPlanToCallFunction (exe_ctx, |
| 316 | m_jit_addr, |
| 317 | struct_address, |
| 318 | error_stream, |
| 319 | true, |
| 320 | true, |
| 321 | (m_needs_object_ptr ? &object_ptr : NULL)); |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | ClangUserExpression::FinalizeJITExecution (Stream &error_stream, |
| 326 | ExecutionContext &exe_ctx, |
| 327 | ClangExpressionVariable *&result) |
| 328 | { |
| 329 | Error expr_error; |
| 330 | |
| 331 | if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error)) |
| 332 | { |
| 333 | error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error")); |
| 334 | return false; |
| 335 | } |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | bool |
| 340 | ClangUserExpression::Execute (Stream &error_stream, |
| 341 | ExecutionContext &exe_ctx, |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 342 | bool discard_on_error, |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 343 | ClangExpressionVariable *&result) |
| 344 | { |
| 345 | if (m_dwarf_opcodes.get()) |
| 346 | { |
| 347 | // TODO execute the JITted opcodes |
| 348 | |
| 349 | error_stream.Printf("We don't currently support executing DWARF expressions"); |
| 350 | |
| 351 | return false; |
| 352 | } |
| 353 | else if (m_jit_addr != LLDB_INVALID_ADDRESS) |
| 354 | { |
| 355 | lldb::addr_t struct_address; |
| 356 | |
| 357 | lldb::addr_t object_ptr = NULL; |
| 358 | |
| 359 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 360 | |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 361 | const bool stop_others = true; |
| 362 | const bool try_all_threads = true; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 363 | ClangFunction::ExecutionResults execution_result = |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 364 | ClangFunction::ExecuteFunction (exe_ctx, |
| 365 | m_jit_addr, |
| 366 | struct_address, |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 367 | stop_others, |
| 368 | try_all_threads, |
| 369 | discard_on_error, |
Sean Callanan | 0027cec | 2010-10-07 18:17:31 +0000 | [diff] [blame] | 370 | 10000000, |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 371 | error_stream, |
| 372 | (m_needs_object_ptr ? &object_ptr : NULL)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 373 | |
| 374 | if (execution_result != ClangFunction::eExecutionCompleted) |
| 375 | { |
| 376 | const char *result_name; |
| 377 | |
| 378 | switch (execution_result) |
| 379 | { |
| 380 | case ClangFunction::eExecutionCompleted: |
| 381 | result_name = "eExecutionCompleted"; |
| 382 | break; |
| 383 | case ClangFunction::eExecutionDiscarded: |
| 384 | result_name = "eExecutionDiscarded"; |
| 385 | break; |
| 386 | case ClangFunction::eExecutionInterrupted: |
| 387 | result_name = "eExecutionInterrupted"; |
| 388 | break; |
| 389 | case ClangFunction::eExecutionSetupError: |
| 390 | result_name = "eExecutionSetupError"; |
| 391 | break; |
| 392 | case ClangFunction::eExecutionTimedOut: |
| 393 | result_name = "eExecutionTimedOut"; |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | error_stream.Printf ("Couldn't execute function; result was %s\n", result_name); |
| 398 | return false; |
| 399 | } |
| 400 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 401 | return FinalizeJITExecution (error_stream, exe_ctx, result); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 402 | } |
| 403 | else |
| 404 | { |
Johnny Chen | cb39544 | 2010-11-10 19:02:11 +0000 | [diff] [blame] | 405 | error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present"); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | StreamString & |
| 411 | ClangUserExpression::DwarfOpcodeStream () |
| 412 | { |
| 413 | if (!m_dwarf_opcodes.get()) |
| 414 | m_dwarf_opcodes.reset(new StreamString()); |
| 415 | |
| 416 | return *m_dwarf_opcodes.get(); |
| 417 | } |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 418 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 419 | lldb::ValueObjectSP |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 420 | ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 421 | bool discard_on_error, |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 422 | const char *expr_cstr, |
| 423 | const char *expr_prefix) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 424 | { |
| 425 | Error error; |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 426 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 427 | |
| 428 | if (exe_ctx.process == NULL) |
| 429 | return result_valobj_sp; |
| 430 | |
| 431 | if (!exe_ctx.process->GetDynamicCheckers()) |
| 432 | { |
| 433 | DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions(); |
| 434 | |
| 435 | StreamString install_errors; |
| 436 | |
| 437 | if (!dynamic_checkers->Install(install_errors, exe_ctx)) |
Sean Callanan | f773145 | 2010-11-05 00:57:06 +0000 | [diff] [blame] | 438 | { |
| 439 | if (install_errors.GetString().empty()) |
| 440 | error.SetErrorString ("couldn't install checkers, unknown error"); |
| 441 | else |
| 442 | error.SetErrorString (install_errors.GetString().c_str()); |
| 443 | |
| 444 | result_valobj_sp.reset (new ValueObjectConstResult (error)); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 445 | return result_valobj_sp; |
Sean Callanan | f773145 | 2010-11-05 00:57:06 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 448 | exe_ctx.process->SetDynamicCheckers(dynamic_checkers); |
| 449 | } |
| 450 | |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 451 | ClangUserExpression user_expression (expr_cstr, expr_prefix); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 452 | |
| 453 | StreamString error_stream; |
| 454 | |
| 455 | if (!user_expression.Parse (error_stream, exe_ctx)) |
| 456 | { |
| 457 | if (error_stream.GetString().empty()) |
| 458 | error.SetErrorString ("expression failed to parse, unknown error"); |
| 459 | else |
| 460 | error.SetErrorString (error_stream.GetString().c_str()); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | ClangExpressionVariable *expr_result = NULL; |
| 465 | |
| 466 | error_stream.GetString().clear(); |
| 467 | |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 468 | if (!user_expression.Execute (error_stream, exe_ctx, discard_on_error, expr_result)) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 469 | { |
| 470 | if (error_stream.GetString().empty()) |
| 471 | error.SetErrorString ("expression failed to execute, unknown error"); |
| 472 | else |
| 473 | error.SetErrorString (error_stream.GetString().c_str()); |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | // TODO: seems weird to get a pointer to a result object back from |
| 478 | // a function. Do we own it? Feels like we do, but from looking at the |
| 479 | // code we don't. Might be best to make this a reference and state |
| 480 | // explicitly that we don't own it when we get a reference back from |
| 481 | // the execute? |
| 482 | if (expr_result) |
| 483 | { |
| 484 | result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx); |
| 485 | } |
| 486 | else |
| 487 | { |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 488 | error.SetErrorString ("Expression did not return a result"); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | } |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 492 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 493 | if (result_valobj_sp.get() == NULL) |
| 494 | result_valobj_sp.reset (new ValueObjectConstResult (error)); |
| 495 | |
| 496 | return result_valobj_sp; |
Johnny Chen | b4c0f02 | 2010-10-29 20:19:44 +0000 | [diff] [blame] | 497 | } |