blob: c911c279993f83e86c43ff3b5a4ebd3526c8b0b7 [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"
Sean Callanane71d5532010-08-27 23:31:21 +000020#include "lldb/Core/Stream.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000021#include "lldb/Core/StreamFile.h"
Sean Callanan9e6ed532010-09-13 21:34:21 +000022#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanane71d5532010-08-27 23:31:21 +000023#include "lldb/Expression/ClangExpressionParser.h"
24#include "lldb/Expression/ClangUtilityFunction.h"
Greg Clayton399107a2013-02-13 23:57:48 +000025#include "lldb/Expression/ExpressionSourceCode.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000026#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanane71d5532010-08-27 23:31:21 +000027#include "lldb/Host/Host.h"
28#include "lldb/Target/ExecutionContext.h"
29#include "lldb/Target/Target.h"
30
31using namespace lldb_private;
32
33//------------------------------------------------------------------
34/// Constructor
35///
36/// @param[in] text
37/// The text of the function. Must be a full translation unit.
38///
39/// @param[in] name
40/// The name of the function, as used in the text.
41//------------------------------------------------------------------
42ClangUtilityFunction::ClangUtilityFunction (const char *text,
43 const char *name) :
Greg Clayton22a939a2011-01-19 23:00:49 +000044 ClangExpression (),
Greg Clayton399107a2013-02-13 23:57:48 +000045 m_function_text (ExpressionSourceCode::g_expression_prefix),
Greg Clayton22a939a2011-01-19 23:00:49 +000046 m_function_name (name)
Sean Callanane71d5532010-08-27 23:31:21 +000047{
Greg Claytona66c4d92013-02-13 22:56:14 +000048 if (text && text[0])
49 m_function_text.append (text);
Sean Callanane71d5532010-08-27 23:31:21 +000050}
51
Greg Clayton5573fde2010-09-24 23:07:41 +000052ClangUtilityFunction::~ClangUtilityFunction ()
53{
54}
55
Sean Callanane71d5532010-08-27 23:31:21 +000056//------------------------------------------------------------------
57/// Install the utility function into a process
58///
59/// @param[in] error_stream
60/// A stream to print parse errors and warnings to.
61///
62/// @param[in] exe_ctx
63/// The execution context to install the utility function to.
64///
65/// @return
66/// True on success (no errors); false otherwise.
67//------------------------------------------------------------------
68bool
69ClangUtilityFunction::Install (Stream &error_stream,
70 ExecutionContext &exe_ctx)
Sean Callanan6961e872010-09-01 00:58:00 +000071{
Greg Clayton22a939a2011-01-19 23:00:49 +000072 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan6961e872010-09-01 00:58:00 +000073 {
74 error_stream.PutCString("error: already installed\n");
75 return false;
76 }
77
Sean Callanane71d5532010-08-27 23:31:21 +000078 ////////////////////////////////////
79 // Set up the target and compiler
80 //
81
Greg Claytonc14ee322011-09-22 04:58:26 +000082 Target *target = exe_ctx.GetTargetPtr();
Sean Callanane71d5532010-08-27 23:31:21 +000083
84 if (!target)
85 {
86 error_stream.PutCString ("error: invalid target\n");
87 return false;
88 }
Sean Callanan79763a42011-05-23 21:40:23 +000089
Greg Claytonc14ee322011-09-22 04:58:26 +000090 Process *process = exe_ctx.GetProcessPtr();
Sean Callanan79763a42011-05-23 21:40:23 +000091
92 if (!process)
93 {
94 error_stream.PutCString ("error: invalid process\n");
95 return false;
96 }
Greg Clayton514487e2011-02-15 21:59:32 +000097
Sean Callanane71d5532010-08-27 23:31:21 +000098 //////////////////////////
99 // Parse the expression
100 //
Sean Callanan9e6ed532010-09-13 21:34:21 +0000101
Sean Callanan92adcac2011-01-13 08:53:35 +0000102 bool keep_result_in_memory = false;
103
Sean Callanan1ee44b72011-10-29 01:58:46 +0000104 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000105
Sean Callanan96d27302013-04-11 00:09:05 +0000106 if (!m_expr_decl_map->WillParse(exe_ctx, NULL))
Sean Callananb9951192011-08-01 18:18:33 +0000107 {
108 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
109 return false;
110 }
Sean Callanane71d5532010-08-27 23:31:21 +0000111
Greg Clayton514487e2011-02-15 21:59:32 +0000112 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this);
Sean Callanane71d5532010-08-27 23:31:21 +0000113
114 unsigned num_errors = parser.Parse (error_stream);
115
116 if (num_errors)
117 {
118 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000119
120 m_expr_decl_map.reset();
121
Sean Callanane71d5532010-08-27 23:31:21 +0000122 return false;
123 }
124
125 //////////////////////////////////
126 // JIT the output of the parser
127 //
Sean Callanan1582ee62013-04-18 22:06:33 +0000128
129 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000130
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000131 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000132 m_jit_end_addr,
133 m_execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000134 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000135 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000136 eExecutionPolicyAlways);
Sean Callanan79763a42011-05-23 21:40:23 +0000137
Greg Claytonc14ee322011-09-22 04:58:26 +0000138 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000139 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanane71d5532010-08-27 23:31:21 +0000140
Greg Claytonc4e411f2011-01-18 19:36:39 +0000141#if 0
142 // jingham: look here
143 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000144 logfile.Printf ("0x%16.16" PRIx64 ": func = %s, source =\n%s\n",
Greg Clayton22a939a2011-01-19 23:00:49 +0000145 m_jit_start_addr,
Greg Claytonc4e411f2011-01-18 19:36:39 +0000146 m_function_name.c_str(),
147 m_function_text.c_str());
148#endif
149
Sean Callanan979f74d2010-12-03 01:38:59 +0000150 m_expr_decl_map->DidParse();
151
Sean Callanan9e6ed532010-09-13 21:34:21 +0000152 m_expr_decl_map.reset();
153
Sean Callanane71d5532010-08-27 23:31:21 +0000154 if (jit_error.Success())
155 {
156 return true;
157 }
158 else
159 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000160 const char *error_cstr = jit_error.AsCString();
161 if (error_cstr && error_cstr[0])
162 error_stream.Printf ("error: %s\n", error_cstr);
163 else
Jason Molendafd54b362011-09-20 21:44:10 +0000164 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanane71d5532010-08-27 23:31:21 +0000165 return false;
166 }
167}
168
169