blob: de5b0c1b03f4244c3ee046ce174be1730e9004b1 [file] [log] [blame]
Sean Callanane71d5532010-08-27 23:31:21 +00001//===-- 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
18#include "lldb/Core/ConstString.h"
Sean Callanan79763a42011-05-23 21:40:23 +000019#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000020#include "lldb/Core/Module.h"
Sean Callanane71d5532010-08-27 23:31:21 +000021#include "lldb/Core/Stream.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000022#include "lldb/Core/StreamFile.h"
Sean Callanan9e6ed532010-09-13 21:34:21 +000023#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanane71d5532010-08-27 23:31:21 +000024#include "lldb/Expression/ClangExpressionParser.h"
25#include "lldb/Expression/ClangUtilityFunction.h"
Greg Clayton399107a2013-02-13 23:57:48 +000026#include "lldb/Expression/ExpressionSourceCode.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000027#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanane71d5532010-08-27 23:31:21 +000028#include "lldb/Host/Host.h"
29#include "lldb/Target/ExecutionContext.h"
30#include "lldb/Target/Target.h"
31
32using namespace lldb_private;
33
34//------------------------------------------------------------------
35/// Constructor
36///
37/// @param[in] text
38/// The text of the function. Must be a full translation unit.
39///
40/// @param[in] name
41/// The name of the function, as used in the text.
42//------------------------------------------------------------------
43ClangUtilityFunction::ClangUtilityFunction (const char *text,
44 const char *name) :
Greg Clayton22a939a2011-01-19 23:00:49 +000045 ClangExpression (),
Greg Clayton23f8c952014-03-24 23:10:19 +000046 m_expr_decl_map (),
47 m_execution_unit_sp (),
48 m_jit_module_wp (),
Greg Clayton399107a2013-02-13 23:57:48 +000049 m_function_text (ExpressionSourceCode::g_expression_prefix),
Greg Clayton22a939a2011-01-19 23:00:49 +000050 m_function_name (name)
Sean Callanane71d5532010-08-27 23:31:21 +000051{
Greg Claytona66c4d92013-02-13 22:56:14 +000052 if (text && text[0])
53 m_function_text.append (text);
Sean Callanane71d5532010-08-27 23:31:21 +000054}
55
Greg Clayton5573fde2010-09-24 23:07:41 +000056ClangUtilityFunction::~ClangUtilityFunction ()
57{
Greg Clayton23f8c952014-03-24 23:10:19 +000058 lldb::ProcessSP process_sp (m_jit_process_wp.lock());
59 if (process_sp)
60 {
61 lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock());
62 if (jit_module_sp)
63 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
64 }
65
Greg Clayton5573fde2010-09-24 23:07:41 +000066}
67
Sean Callanane71d5532010-08-27 23:31:21 +000068//------------------------------------------------------------------
69/// Install the utility function into a process
70///
71/// @param[in] error_stream
72/// A stream to print parse errors and warnings to.
73///
74/// @param[in] exe_ctx
75/// The execution context to install the utility function to.
76///
77/// @return
78/// True on success (no errors); false otherwise.
79//------------------------------------------------------------------
80bool
81ClangUtilityFunction::Install (Stream &error_stream,
82 ExecutionContext &exe_ctx)
Sean Callanan6961e872010-09-01 00:58:00 +000083{
Greg Clayton22a939a2011-01-19 23:00:49 +000084 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan6961e872010-09-01 00:58:00 +000085 {
86 error_stream.PutCString("error: already installed\n");
87 return false;
88 }
89
Sean Callanane71d5532010-08-27 23:31:21 +000090 ////////////////////////////////////
91 // Set up the target and compiler
92 //
93
Greg Claytonc14ee322011-09-22 04:58:26 +000094 Target *target = exe_ctx.GetTargetPtr();
Sean Callanane71d5532010-08-27 23:31:21 +000095
96 if (!target)
97 {
98 error_stream.PutCString ("error: invalid target\n");
99 return false;
100 }
Sean Callanan79763a42011-05-23 21:40:23 +0000101
Greg Claytonc14ee322011-09-22 04:58:26 +0000102 Process *process = exe_ctx.GetProcessPtr();
Sean Callanan79763a42011-05-23 21:40:23 +0000103
104 if (!process)
105 {
106 error_stream.PutCString ("error: invalid process\n");
107 return false;
108 }
Greg Clayton514487e2011-02-15 21:59:32 +0000109
Sean Callanane71d5532010-08-27 23:31:21 +0000110 //////////////////////////
111 // Parse the expression
112 //
Sean Callanan9e6ed532010-09-13 21:34:21 +0000113
Sean Callanan92adcac2011-01-13 08:53:35 +0000114 bool keep_result_in_memory = false;
115
Sean Callanan1ee44b72011-10-29 01:58:46 +0000116 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000117
Sean Callanan96d27302013-04-11 00:09:05 +0000118 if (!m_expr_decl_map->WillParse(exe_ctx, NULL))
Sean Callananb9951192011-08-01 18:18:33 +0000119 {
120 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
121 return false;
122 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000123
124 const bool generate_debug_info = true;
125 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this, generate_debug_info);
Sean Callanane71d5532010-08-27 23:31:21 +0000126
127 unsigned num_errors = parser.Parse (error_stream);
128
129 if (num_errors)
130 {
131 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000132
133 m_expr_decl_map.reset();
134
Sean Callanane71d5532010-08-27 23:31:21 +0000135 return false;
136 }
137
138 //////////////////////////////////
139 // JIT the output of the parser
140 //
Sean Callanan1582ee62013-04-18 22:06:33 +0000141
142 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000143
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000144 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000145 m_jit_end_addr,
Greg Clayton23f8c952014-03-24 23:10:19 +0000146 m_execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000147 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000148 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000149 eExecutionPolicyAlways);
Sean Callanan79763a42011-05-23 21:40:23 +0000150
Greg Claytonc14ee322011-09-22 04:58:26 +0000151 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Greg Clayton23f8c952014-03-24 23:10:19 +0000152 {
153 m_jit_process_wp = process->shared_from_this();
154 if (parser.GetGenerateDebugInfo())
155 {
156 lldb::ModuleSP jit_module_sp ( m_execution_unit_sp->GetJITModule());
157
158 if (jit_module_sp)
159 {
160 ConstString const_func_name(FunctionName());
161 FileSpec jit_file;
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);
166 }
167 }
168 }
Sean Callanane71d5532010-08-27 23:31:21 +0000169
Greg Claytonc4e411f2011-01-18 19:36:39 +0000170#if 0
171 // jingham: look here
172 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000173 logfile.Printf ("0x%16.16" PRIx64 ": func = %s, source =\n%s\n",
Greg Clayton22a939a2011-01-19 23:00:49 +0000174 m_jit_start_addr,
Greg Claytonc4e411f2011-01-18 19:36:39 +0000175 m_function_name.c_str(),
176 m_function_text.c_str());
177#endif
178
Sean Callanan979f74d2010-12-03 01:38:59 +0000179 m_expr_decl_map->DidParse();
180
Sean Callanan9e6ed532010-09-13 21:34:21 +0000181 m_expr_decl_map.reset();
182
Sean Callanane71d5532010-08-27 23:31:21 +0000183 if (jit_error.Success())
184 {
185 return true;
186 }
187 else
188 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000189 const char *error_cstr = jit_error.AsCString();
190 if (error_cstr && error_cstr[0])
191 error_stream.Printf ("error: %s\n", error_cstr);
192 else
Jason Molendafd54b362011-09-20 21:44:10 +0000193 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanane71d5532010-08-27 23:31:21 +0000194 return false;
195 }
196}
197
198