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