blob: e1d1fdd757a58f116d83b933e8ff80402f551338 [file] [log] [blame]
Alexander Shaposhnikov696bd632016-11-26 05:23:44 +00001//===-- ClangUtilityFunction.cpp ---------------------------------*- C++-*-===//
Sean Callanane71d5532010-08-27 23:31:21 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Callanane71d5532010-08-27 23:31:21 +00006//
7//===----------------------------------------------------------------------===//
8
Kate Stoneb9c1b512016-09-06 20:57:50 +00009#include "ClangUtilityFunction.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000010#include "ClangExpressionDeclMap.h"
11#include "ClangExpressionParser.h"
Jim Inghamea401ec2019-03-06 22:43:25 +000012#include "ClangExpressionSourceCode.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000013
Sean Callanane71d5532010-08-27 23:31:21 +000014#include <stdio.h>
15#if HAVE_SYS_TYPES_H
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include <sys/types.h>
Sean Callanane71d5532010-08-27 23:31:21 +000017#endif
18
Sean Callanane71d5532010-08-27 23:31:21 +000019
Greg Clayton23f8c952014-03-24 23:10:19 +000020#include "lldb/Core/Module.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000021#include "lldb/Core/StreamFile.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000022#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanane71d5532010-08-27 23:31:21 +000023#include "lldb/Host/Host.h"
24#include "lldb/Target/ExecutionContext.h"
25#include "lldb/Target/Target.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000027#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000028#include "lldb/Utility/Stream.h"
Sean Callanane71d5532010-08-27 23:31:21 +000029
30using namespace lldb_private;
31
32//------------------------------------------------------------------
33/// Constructor
34///
35/// @param[in] text
36/// The text of the function. Must be a full translation unit.
37///
38/// @param[in] name
39/// The name of the function, as used in the text.
40//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000041ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope,
42 const char *text, const char *name)
Jim Inghamea401ec2019-03-06 22:43:25 +000043 : UtilityFunction(exe_scope, text, name) {
44 m_function_text.assign(ClangExpressionSourceCode::g_expression_prefix);
45 if (text && text[0])
46 m_function_text.append(text);
47}
Sean Callanane71d5532010-08-27 23:31:21 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049ClangUtilityFunction::~ClangUtilityFunction() {}
Greg Clayton5573fde2010-09-24 23:07:41 +000050
Sean Callanane71d5532010-08-27 23:31:21 +000051//------------------------------------------------------------------
52/// Install the utility function into a process
53///
Sean Callanan579e70c2016-03-19 00:03:59 +000054/// @param[in] diagnostic_manager
55/// A diagnostic manager to report errors and warnings to.
Sean Callanane71d5532010-08-27 23:31:21 +000056///
57/// @param[in] exe_ctx
58/// The execution context to install the utility function to.
59///
60/// @return
61/// True on success (no errors); false otherwise.
62//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000063bool ClangUtilityFunction::Install(DiagnosticManager &diagnostic_manager,
64 ExecutionContext &exe_ctx) {
65 if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {
Zachary Turnere2411fa2016-11-12 19:12:56 +000066 diagnostic_manager.PutString(eDiagnosticSeverityWarning,
67 "already installed");
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 return false;
69 }
70
71 ////////////////////////////////////
72 // Set up the target and compiler
73 //
74
75 Target *target = exe_ctx.GetTargetPtr();
76
77 if (!target) {
Zachary Turnere2411fa2016-11-12 19:12:56 +000078 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 return false;
80 }
81
82 Process *process = exe_ctx.GetProcessPtr();
83
84 if (!process) {
Zachary Turnere2411fa2016-11-12 19:12:56 +000085 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid process");
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 return false;
87 }
88
89 //////////////////////////
90 // Parse the expression
91 //
92
93 bool keep_result_in_memory = false;
94
95 ResetDeclMap(exe_ctx, keep_result_in_memory);
96
97 if (!DeclMap()->WillParse(exe_ctx, NULL)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +000098 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 eDiagnosticSeverityError,
100 "current process state is unsuitable for expression parsing");
101 return false;
102 }
103
104 const bool generate_debug_info = true;
105 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this,
106 generate_debug_info);
107
108 unsigned num_errors = parser.Parse(diagnostic_manager);
109
110 if (num_errors) {
111 ResetDeclMap();
112
113 return false;
114 }
115
116 //////////////////////////////////
117 // JIT the output of the parser
118 //
119
120 bool can_interpret = false; // should stay that way
121
Zachary Turner97206d52017-05-12 04:51:55 +0000122 Status jit_error = parser.PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
124 can_interpret, eExecutionPolicyAlways);
125
126 if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {
127 m_jit_process_wp = process->shared_from_this();
128 if (parser.GetGenerateDebugInfo()) {
129 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
130
131 if (jit_module_sp) {
132 ConstString const_func_name(FunctionName());
133 FileSpec jit_file;
134 jit_file.GetFilename() = const_func_name;
135 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
136 m_jit_module_wp = jit_module_sp;
137 target->GetImages().Append(jit_module_sp);
138 }
Sean Callanan6961e872010-09-01 00:58:00 +0000139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 DeclMap()->DidParse();
143
144 ResetDeclMap();
145
146 if (jit_error.Success()) {
147 return true;
148 } else {
149 const char *error_cstr = jit_error.AsCString();
150 if (error_cstr && error_cstr[0]) {
151 diagnostic_manager.Printf(eDiagnosticSeverityError, "%s", error_cstr);
152 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000153 diagnostic_manager.PutString(eDiagnosticSeverityError,
154 "expression can't be interpreted or run");
Sean Callanane71d5532010-08-27 23:31:21 +0000155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 return false;
157 }
Sean Callanane71d5532010-08-27 23:31:21 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160void ClangUtilityFunction::ClangUtilityFunctionHelper::ResetDeclMap(
161 ExecutionContext &exe_ctx, bool keep_result_in_memory) {
162 m_expr_decl_map_up.reset(
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000163 new ClangExpressionDeclMap(keep_result_in_memory, nullptr, exe_ctx,
164 nullptr));
Jim Ingham151c0322015-09-15 21:13:50 +0000165}