1 //===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 # include <sys/types.h>
18 #include "lldb/Core/ConstString.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Expression/ClangExpressionDeclMap.h"
23 #include "lldb/Expression/ClangExpressionParser.h"
24 #include "lldb/Expression/ClangUtilityFunction.h"
25 #include "lldb/Expression/ExpressionSourceCode.h"
26 #include "lldb/Expression/IRExecutionUnit.h"
27 #include "lldb/Host/Host.h"
28 #include "lldb/Target/ExecutionContext.h"
29 #include "lldb/Target/Target.h"
31 using namespace lldb_private;
33 //------------------------------------------------------------------
37 /// The text of the function. Must be a full translation unit.
40 /// The name of the function, as used in the text.
41 //------------------------------------------------------------------
42 ClangUtilityFunction::ClangUtilityFunction (const char *text,
45 m_function_text (ExpressionSourceCode::g_expression_prefix),
46 m_function_name (name)
49 m_function_text.append (text);
52 ClangUtilityFunction::~ClangUtilityFunction ()
56 //------------------------------------------------------------------
57 /// Install the utility function into a process
59 /// @param[in] error_stream
60 /// A stream to print parse errors and warnings to.
62 /// @param[in] exe_ctx
63 /// The execution context to install the utility function to.
66 /// True on success (no errors); false otherwise.
67 //------------------------------------------------------------------
69 ClangUtilityFunction::Install (Stream &error_stream,
70 ExecutionContext &exe_ctx)
72 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
74 error_stream.PutCString("error: already installed\n");
78 ////////////////////////////////////
79 // Set up the target and compiler
82 Target *target = exe_ctx.GetTargetPtr();
86 error_stream.PutCString ("error: invalid target\n");
90 Process *process = exe_ctx.GetProcessPtr();
94 error_stream.PutCString ("error: invalid process\n");
98 //////////////////////////
99 // Parse the expression
102 bool keep_result_in_memory = false;
104 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
106 if (!m_expr_decl_map->WillParse(exe_ctx, NULL))
108 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
112 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this);
114 unsigned num_errors = parser.Parse (error_stream);
118 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
120 m_expr_decl_map.reset();
125 //////////////////////////////////
126 // JIT the output of the parser
129 bool can_interpret = false; // should stay that way
131 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
136 eExecutionPolicyAlways);
138 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
139 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
142 // jingham: look here
143 StreamFile logfile ("/tmp/exprs.txt", "a");
144 logfile.Printf ("0x%16.16" PRIx64 ": func = %s, source =\n%s\n",
146 m_function_name.c_str(),
147 m_function_text.c_str());
150 m_expr_decl_map->DidParse();
152 m_expr_decl_map.reset();
154 if (jit_error.Success())
160 const char *error_cstr = jit_error.AsCString();
161 if (error_cstr && error_cstr[0])
162 error_stream.Printf ("error: %s\n", error_cstr);
164 error_stream.Printf ("error: expression can't be interpreted or run\n");