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" |
| 24 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 25 | #include "lldb/Expression/ClangExpressionParser.h" |
| 26 | #include "lldb/Expression/ClangFunction.h" |
| 27 | #include "lldb/Expression/ASTResultSynthesizer.h" |
| 28 | #include "lldb/Expression/ClangUserExpression.h" |
| 29 | #include "lldb/Host/Host.h" |
| 30 | #include "lldb/Target/ExecutionContext.h" |
| 31 | #include "lldb/Target/Target.h" |
| 32 | |
| 33 | using namespace lldb_private; |
| 34 | |
| 35 | ClangUserExpression::ClangUserExpression (const char *expr) : |
| 36 | m_expr_text(expr), |
| 37 | m_jit_addr(LLDB_INVALID_ADDRESS) |
| 38 | { |
| 39 | StreamString m_transformed_stream; |
| 40 | |
| 41 | m_transformed_stream.Printf("extern \"C\" void %s(void *___clang_arg) { %s; }\n", |
| 42 | FunctionName(), |
| 43 | m_expr_text.c_str()); |
| 44 | |
| 45 | m_transformed_text = m_transformed_stream.GetData(); |
| 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 | |
| 58 | bool |
| 59 | ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx) |
| 60 | { |
| 61 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 62 | |
| 63 | //////////////////////////////////// |
| 64 | // Set up the target and compiler |
| 65 | // |
| 66 | |
| 67 | Target *target = exe_ctx.target; |
| 68 | |
| 69 | if (!target) |
| 70 | { |
| 71 | error_stream.PutCString ("error: invalid target\n"); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | ConstString target_triple; |
| 76 | |
| 77 | target->GetTargetTriple (target_triple); |
| 78 | |
| 79 | if (!target_triple) |
| 80 | target_triple = Host::GetTargetTriple (); |
| 81 | |
| 82 | if (!target_triple) |
| 83 | { |
| 84 | error_stream.PutCString ("error: invalid target triple\n"); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | ////////////////////////// |
| 89 | // Parse the expression |
| 90 | // |
| 91 | |
| 92 | m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx)); |
| 93 | |
| 94 | ClangExpressionParser parser(target_triple.GetCString(), *this); |
| 95 | |
| 96 | unsigned num_errors = parser.Parse (error_stream); |
| 97 | |
| 98 | if (num_errors) |
| 99 | { |
| 100 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /////////////////////////////////////////////// |
| 105 | // Convert the output of the parser to DWARF |
| 106 | // |
| 107 | |
| 108 | m_dwarf_opcodes.reset(new StreamString); |
| 109 | m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost); |
| 110 | m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary); |
| 111 | |
| 112 | m_local_variables.reset(new ClangExpressionVariableStore()); |
| 113 | |
| 114 | Error dwarf_error = parser.MakeDWARF (); |
| 115 | |
| 116 | if (dwarf_error.Success()) |
| 117 | { |
| 118 | if (log) |
| 119 | log->Printf("Code can be interpreted."); |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | ////////////////////////////////// |
| 125 | // JIT the output of the parser |
| 126 | // |
| 127 | |
| 128 | m_dwarf_opcodes.reset(); |
| 129 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame^] | 130 | lldb::addr_t jit_end; |
| 131 | |
| 132 | Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 133 | |
| 134 | if (jit_error.Success()) |
| 135 | { |
| 136 | if (log) |
| 137 | { |
| 138 | log->Printf("Code can be run in the target."); |
| 139 | |
| 140 | StreamString disassembly_stream; |
| 141 | |
| 142 | Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx); |
| 143 | |
| 144 | if (!err.Success()) |
| 145 | { |
| 146 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | bool |
| 164 | ClangUserExpression::Execute (Stream &error_stream, |
| 165 | ExecutionContext &exe_ctx, |
| 166 | ClangExpressionVariable *&result) |
| 167 | { |
| 168 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 169 | |
| 170 | if (m_dwarf_opcodes.get()) |
| 171 | { |
| 172 | // TODO execute the JITted opcodes |
| 173 | |
| 174 | error_stream.Printf("We don't currently support executing DWARF expressions"); |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | else if (m_jit_addr != LLDB_INVALID_ADDRESS) |
| 179 | { |
| 180 | lldb::addr_t struct_address; |
| 181 | |
| 182 | Error materialize_error; |
| 183 | |
| 184 | if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error)) |
| 185 | { |
| 186 | error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString("unknown error")); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (log) |
| 191 | { |
| 192 | log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr); |
| 193 | log->Printf("Structure address : 0x%llx", (uint64_t)struct_address); |
| 194 | |
| 195 | StreamString args; |
| 196 | |
| 197 | Error dump_error; |
| 198 | |
| 199 | if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error)) |
| 200 | { |
| 201 | log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error")); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | log->Printf("Structure contents:\n%s", args.GetData()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | ClangFunction::ExecutionResults execution_result = |
| 210 | ClangFunction::ExecuteFunction (exe_ctx, m_jit_addr, struct_address, true, true, 10000, error_stream); |
| 211 | |
| 212 | if (execution_result != ClangFunction::eExecutionCompleted) |
| 213 | { |
| 214 | const char *result_name; |
| 215 | |
| 216 | switch (execution_result) |
| 217 | { |
| 218 | case ClangFunction::eExecutionCompleted: |
| 219 | result_name = "eExecutionCompleted"; |
| 220 | break; |
| 221 | case ClangFunction::eExecutionDiscarded: |
| 222 | result_name = "eExecutionDiscarded"; |
| 223 | break; |
| 224 | case ClangFunction::eExecutionInterrupted: |
| 225 | result_name = "eExecutionInterrupted"; |
| 226 | break; |
| 227 | case ClangFunction::eExecutionSetupError: |
| 228 | result_name = "eExecutionSetupError"; |
| 229 | break; |
| 230 | case ClangFunction::eExecutionTimedOut: |
| 231 | result_name = "eExecutionTimedOut"; |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | error_stream.Printf ("Couldn't execute function; result was %s\n", result_name); |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | Error expr_error; |
| 240 | |
| 241 | if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error)) |
| 242 | { |
| 243 | error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error")); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present"); |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | StreamString & |
| 257 | ClangUserExpression::DwarfOpcodeStream () |
| 258 | { |
| 259 | if (!m_dwarf_opcodes.get()) |
| 260 | m_dwarf_opcodes.reset(new StreamString()); |
| 261 | |
| 262 | return *m_dwarf_opcodes.get(); |
| 263 | } |