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/Module.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Expression/ClangExpressionDeclMap.h"
24 #include "lldb/Expression/ClangExpressionParser.h"
25 #include "lldb/Expression/ClangUtilityFunction.h"
26 #include "lldb/Expression/ExpressionSourceCode.h"
27 #include "lldb/Expression/IRExecutionUnit.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Target.h"
32 using namespace lldb_private;
34 //------------------------------------------------------------------
38 /// The text of the function. Must be a full translation unit.
41 /// The name of the function, as used in the text.
42 //------------------------------------------------------------------
43 ClangUtilityFunction::ClangUtilityFunction (const char *text,
47 m_execution_unit_sp (),
49 m_function_text (ExpressionSourceCode::g_expression_prefix),
50 m_function_name (name)
53 m_function_text.append (text);
56 ClangUtilityFunction::~ClangUtilityFunction ()
58 lldb::ProcessSP process_sp (m_jit_process_wp.lock());
61 lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock());
63 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
68 //------------------------------------------------------------------
69 /// Install the utility function into a process
71 /// @param[in] error_stream
72 /// A stream to print parse errors and warnings to.
74 /// @param[in] exe_ctx
75 /// The execution context to install the utility function to.
78 /// True on success (no errors); false otherwise.
79 //------------------------------------------------------------------
81 ClangUtilityFunction::Install (Stream &error_stream,
82 ExecutionContext &exe_ctx)
84 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
86 error_stream.PutCString("error: already installed\n");
90 ////////////////////////////////////
91 // Set up the target and compiler
94 Target *target = exe_ctx.GetTargetPtr();
98 error_stream.PutCString ("error: invalid target\n");
102 Process *process = exe_ctx.GetProcessPtr();
106 error_stream.PutCString ("error: invalid process\n");
110 //////////////////////////
111 // Parse the expression
114 bool keep_result_in_memory = false;
116 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
118 if (!m_expr_decl_map->WillParse(exe_ctx, NULL))
120 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
124 const bool generate_debug_info = true;
125 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this, generate_debug_info);
127 unsigned num_errors = parser.Parse (error_stream);
131 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
133 m_expr_decl_map.reset();
138 //////////////////////////////////
139 // JIT the output of the parser
142 bool can_interpret = false; // should stay that way
144 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
149 eExecutionPolicyAlways);
151 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
153 m_jit_process_wp = process->shared_from_this();
154 if (parser.GetGenerateDebugInfo())
156 lldb::ModuleSP jit_module_sp ( m_execution_unit_sp->GetJITModule());
160 ConstString const_func_name(FunctionName());
162 jit_file.GetFilename() = const_func_name;
163 jit_module_sp->SetFileSpecAndObjectName (jit_file, ConstString());
164 m_jit_module_wp = jit_module_sp;
165 target->GetImages().Append(jit_module_sp);
171 // jingham: look here
172 StreamFile logfile ("/tmp/exprs.txt", "a");
173 logfile.Printf ("0x%16.16" PRIx64 ": func = %s, source =\n%s\n",
175 m_function_name.c_str(),
176 m_function_text.c_str());
179 m_expr_decl_map->DidParse();
181 m_expr_decl_map.reset();
183 if (jit_error.Success())
189 const char *error_cstr = jit_error.AsCString();
190 if (error_cstr && error_cstr[0])
191 error_stream.Printf ("error: %s\n", error_cstr);
193 error_stream.Printf ("error: expression can't be interpreted or run\n");