xref: /src/contrib/llvm-project/lldb/source/Expression/LLVMUserExpression.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- LLVMUserExpression.cpp --------------------------------------------===//
2e81d9d49SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e81d9d49SDimitry Andric //
7e81d9d49SDimitry Andric //===----------------------------------------------------------------------===//
8e81d9d49SDimitry Andric 
9e81d9d49SDimitry Andric 
10e81d9d49SDimitry Andric #include "lldb/Expression/LLVMUserExpression.h"
11e81d9d49SDimitry Andric #include "lldb/Core/Module.h"
12e81d9d49SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
13f3fbd1c0SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
14ead24645SDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
15e81d9d49SDimitry Andric #include "lldb/Expression/IRExecutionUnit.h"
16e81d9d49SDimitry Andric #include "lldb/Expression/IRInterpreter.h"
17e81d9d49SDimitry Andric #include "lldb/Expression/Materializer.h"
18e81d9d49SDimitry Andric #include "lldb/Host/HostInfo.h"
19e81d9d49SDimitry Andric #include "lldb/Symbol/Block.h"
20e81d9d49SDimitry Andric #include "lldb/Symbol/Function.h"
21e81d9d49SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
22e81d9d49SDimitry Andric #include "lldb/Symbol/SymbolVendor.h"
23e81d9d49SDimitry Andric #include "lldb/Symbol/Type.h"
24e81d9d49SDimitry Andric #include "lldb/Symbol/VariableList.h"
257fa27ce4SDimitry Andric #include "lldb/Target/ABI.h"
26e81d9d49SDimitry Andric #include "lldb/Target/ExecutionContext.h"
27e81d9d49SDimitry Andric #include "lldb/Target/Process.h"
28e81d9d49SDimitry Andric #include "lldb/Target/StackFrame.h"
29e81d9d49SDimitry Andric #include "lldb/Target/Target.h"
30e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlan.h"
31e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlanCallUserExpression.h"
3274a628f7SDimitry Andric #include "lldb/Utility/ConstString.h"
33145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
3474a628f7SDimitry Andric #include "lldb/Utility/Log.h"
3574a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
36e81d9d49SDimitry Andric 
377fa27ce4SDimitry Andric using namespace lldb;
38e81d9d49SDimitry Andric using namespace lldb_private;
39e81d9d49SDimitry Andric 
40706b4fc4SDimitry Andric char LLVMUserExpression::ID;
41706b4fc4SDimitry Andric 
LLVMUserExpression(ExecutionContextScope & exe_scope,llvm::StringRef expr,llvm::StringRef prefix,SourceLanguage language,ResultType desired_type,const EvaluateExpressionOptions & options)42e81d9d49SDimitry Andric LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
4314f1b3e8SDimitry Andric                                        llvm::StringRef expr,
4414f1b3e8SDimitry Andric                                        llvm::StringRef prefix,
45ac9a064cSDimitry Andric                                        SourceLanguage language,
46e81d9d49SDimitry Andric                                        ResultType desired_type,
47706b4fc4SDimitry Andric                                        const EvaluateExpressionOptions &options)
48706b4fc4SDimitry Andric     : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
49e81d9d49SDimitry Andric       m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
505f29bb8aSDimitry Andric       m_stack_frame_top(LLDB_INVALID_ADDRESS), m_allow_cxx(false),
515f29bb8aSDimitry Andric       m_allow_objc(false), m_transformed_text(), m_execution_unit_sp(),
52e3b55780SDimitry Andric       m_materializer_up(), m_jit_module_wp(), m_target(nullptr),
53e3b55780SDimitry Andric       m_can_interpret(false), m_materialized_address(LLDB_INVALID_ADDRESS) {}
54e81d9d49SDimitry Andric 
~LLVMUserExpression()5514f1b3e8SDimitry Andric LLVMUserExpression::~LLVMUserExpression() {
5614f1b3e8SDimitry Andric   if (m_target) {
57e81d9d49SDimitry Andric     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
58e81d9d49SDimitry Andric     if (jit_module_sp)
59e81d9d49SDimitry Andric       m_target->GetImages().Remove(jit_module_sp);
60e81d9d49SDimitry Andric   }
61e81d9d49SDimitry Andric }
62e81d9d49SDimitry Andric 
63e81d9d49SDimitry Andric lldb::ExpressionResults
DoExecute(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,const EvaluateExpressionOptions & options,lldb::UserExpressionSP & shared_ptr_to_me,lldb::ExpressionVariableSP & result)6414f1b3e8SDimitry Andric LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
6514f1b3e8SDimitry Andric                               ExecutionContext &exe_ctx,
6614f1b3e8SDimitry Andric                               const EvaluateExpressionOptions &options,
6714f1b3e8SDimitry Andric                               lldb::UserExpressionSP &shared_ptr_to_me,
6814f1b3e8SDimitry Andric                               lldb::ExpressionVariableSP &result) {
6914f1b3e8SDimitry Andric   // The expression log is quite verbose, and if you're just tracking the
70f73363f1SDimitry Andric   // execution of the expression, it's quite convenient to have these logs come
71f73363f1SDimitry Andric   // out with the STEP log as well.
72145449b1SDimitry Andric   Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
73e81d9d49SDimitry Andric 
74706b4fc4SDimitry Andric   if (m_jit_start_addr == LLDB_INVALID_ADDRESS && !m_can_interpret) {
75706b4fc4SDimitry Andric     diagnostic_manager.PutString(
76ac9a064cSDimitry Andric         lldb::eSeverityError,
77706b4fc4SDimitry Andric         "Expression can't be run, because there is no JIT compiled function");
78706b4fc4SDimitry Andric     return lldb::eExpressionSetupError;
79706b4fc4SDimitry Andric   }
80706b4fc4SDimitry Andric 
81e81d9d49SDimitry Andric   lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
82e81d9d49SDimitry Andric 
8314f1b3e8SDimitry Andric   if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
8414f1b3e8SDimitry Andric                                      struct_address)) {
8514f1b3e8SDimitry Andric     diagnostic_manager.Printf(
86ac9a064cSDimitry Andric         lldb::eSeverityError,
8714f1b3e8SDimitry Andric         "errored out in %s, couldn't PrepareToExecuteJITExpression",
8814f1b3e8SDimitry Andric         __FUNCTION__);
89e81d9d49SDimitry Andric     return lldb::eExpressionSetupError;
90e81d9d49SDimitry Andric   }
91e81d9d49SDimitry Andric 
92e81d9d49SDimitry Andric   lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
93e81d9d49SDimitry Andric   lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
94e81d9d49SDimitry Andric 
9514f1b3e8SDimitry Andric   if (m_can_interpret) {
96e81d9d49SDimitry Andric     llvm::Module *module = m_execution_unit_sp->GetModule();
97e81d9d49SDimitry Andric     llvm::Function *function = m_execution_unit_sp->GetFunction();
98e81d9d49SDimitry Andric 
9914f1b3e8SDimitry Andric     if (!module || !function) {
10014f1b3e8SDimitry Andric       diagnostic_manager.PutString(
101ac9a064cSDimitry Andric           lldb::eSeverityError, "supposed to interpret, but nothing is there");
102e81d9d49SDimitry Andric       return lldb::eExpressionSetupError;
103e81d9d49SDimitry Andric     }
104e81d9d49SDimitry Andric 
105b76161e4SDimitry Andric     Status interpreter_error;
106e81d9d49SDimitry Andric 
107e81d9d49SDimitry Andric     std::vector<lldb::addr_t> args;
108e81d9d49SDimitry Andric 
10914f1b3e8SDimitry Andric     if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
110ac9a064cSDimitry Andric       diagnostic_manager.Printf(lldb::eSeverityError,
11114f1b3e8SDimitry Andric                                 "errored out in %s, couldn't AddArguments",
112f3fbd1c0SDimitry Andric                                 __FUNCTION__);
113e81d9d49SDimitry Andric       return lldb::eExpressionSetupError;
114e81d9d49SDimitry Andric     }
115e81d9d49SDimitry Andric 
116e81d9d49SDimitry Andric     function_stack_bottom = m_stack_frame_bottom;
117e81d9d49SDimitry Andric     function_stack_top = m_stack_frame_top;
118e81d9d49SDimitry Andric 
1195f29bb8aSDimitry Andric     IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp,
1205f29bb8aSDimitry Andric                              interpreter_error, function_stack_bottom,
121b1c73532SDimitry Andric                              function_stack_top, exe_ctx, options.GetTimeout());
122e81d9d49SDimitry Andric 
12314f1b3e8SDimitry Andric     if (!interpreter_error.Success()) {
124ac9a064cSDimitry Andric       diagnostic_manager.Printf(lldb::eSeverityError,
12514f1b3e8SDimitry Andric                                 "supposed to interpret, but failed: %s",
126f3fbd1c0SDimitry Andric                                 interpreter_error.AsCString());
127e81d9d49SDimitry Andric       return lldb::eExpressionDiscarded;
128e81d9d49SDimitry Andric     }
12914f1b3e8SDimitry Andric   } else {
13014f1b3e8SDimitry Andric     if (!exe_ctx.HasThreadScope()) {
131ac9a064cSDimitry Andric       diagnostic_manager.Printf(lldb::eSeverityError,
13214f1b3e8SDimitry Andric                                 "%s called with no thread selected",
13314f1b3e8SDimitry Andric                                 __FUNCTION__);
134e81d9d49SDimitry Andric       return lldb::eExpressionSetupError;
135e81d9d49SDimitry Andric     }
136e81d9d49SDimitry Andric 
137cfca06d7SDimitry Andric     // Store away the thread ID for error reporting, in case it exits
138cfca06d7SDimitry Andric     // during execution:
139cfca06d7SDimitry Andric     lldb::tid_t expr_thread_id = exe_ctx.GetThreadRef().GetID();
140cfca06d7SDimitry Andric 
141e81d9d49SDimitry Andric     Address wrapper_address(m_jit_start_addr);
142e81d9d49SDimitry Andric 
143e81d9d49SDimitry Andric     std::vector<lldb::addr_t> args;
144e81d9d49SDimitry Andric 
14514f1b3e8SDimitry Andric     if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
146ac9a064cSDimitry Andric       diagnostic_manager.Printf(lldb::eSeverityError,
14714f1b3e8SDimitry Andric                                 "errored out in %s, couldn't AddArguments",
148f3fbd1c0SDimitry Andric                                 __FUNCTION__);
149e81d9d49SDimitry Andric       return lldb::eExpressionSetupError;
150e81d9d49SDimitry Andric     }
151e81d9d49SDimitry Andric 
15214f1b3e8SDimitry Andric     lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
15314f1b3e8SDimitry Andric         exe_ctx.GetThreadRef(), wrapper_address, args, options,
15414f1b3e8SDimitry Andric         shared_ptr_to_me));
155e81d9d49SDimitry Andric 
156f3fbd1c0SDimitry Andric     StreamString ss;
15714f1b3e8SDimitry Andric     if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
158ac9a064cSDimitry Andric       diagnostic_manager.PutString(lldb::eSeverityError, ss.GetString());
159e81d9d49SDimitry Andric       return lldb::eExpressionSetupError;
160f3fbd1c0SDimitry Andric     }
161e81d9d49SDimitry Andric 
162e81d9d49SDimitry Andric     ThreadPlanCallUserExpression *user_expression_plan =
163e81d9d49SDimitry Andric         static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
164e81d9d49SDimitry Andric 
16514f1b3e8SDimitry Andric     lldb::addr_t function_stack_pointer =
16614f1b3e8SDimitry Andric         user_expression_plan->GetFunctionStackPointer();
167e81d9d49SDimitry Andric 
168e81d9d49SDimitry Andric     function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
169e81d9d49SDimitry Andric     function_stack_top = function_stack_pointer;
170e81d9d49SDimitry Andric 
171706b4fc4SDimitry Andric     LLDB_LOGF(log,
17214f1b3e8SDimitry Andric               "-- [UserExpression::Execute] Execution of expression begins --");
173e81d9d49SDimitry Andric 
174e81d9d49SDimitry Andric     if (exe_ctx.GetProcessPtr())
175e81d9d49SDimitry Andric       exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
176e81d9d49SDimitry Andric 
177e81d9d49SDimitry Andric     lldb::ExpressionResults execution_result =
17814f1b3e8SDimitry Andric         exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
17914f1b3e8SDimitry Andric                                               diagnostic_manager);
180e81d9d49SDimitry Andric 
181e81d9d49SDimitry Andric     if (exe_ctx.GetProcessPtr())
182e81d9d49SDimitry Andric       exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
183e81d9d49SDimitry Andric 
184ead24645SDimitry Andric     LLDB_LOGF(log, "-- [UserExpression::Execute] Execution of expression "
18514f1b3e8SDimitry Andric                    "completed --");
186e81d9d49SDimitry Andric 
18714f1b3e8SDimitry Andric     if (execution_result == lldb::eExpressionInterrupted ||
18814f1b3e8SDimitry Andric         execution_result == lldb::eExpressionHitBreakpoint) {
1895f29bb8aSDimitry Andric       const char *error_desc = nullptr;
190e81d9d49SDimitry Andric 
191344a3780SDimitry Andric       if (user_expression_plan) {
192344a3780SDimitry Andric         if (auto real_stop_info_sp = user_expression_plan->GetRealStopInfo())
193e81d9d49SDimitry Andric           error_desc = real_stop_info_sp->GetDescription();
194e81d9d49SDimitry Andric       }
195e81d9d49SDimitry Andric       if (error_desc)
196ac9a064cSDimitry Andric         diagnostic_manager.Printf(lldb::eSeverityError,
19714f1b3e8SDimitry Andric                                   "Execution was interrupted, reason: %s.",
198f3fbd1c0SDimitry Andric                                   error_desc);
199e81d9d49SDimitry Andric       else
200ac9a064cSDimitry Andric         diagnostic_manager.PutString(lldb::eSeverityError,
20114f1b3e8SDimitry Andric                                      "Execution was interrupted.");
202e81d9d49SDimitry Andric 
20314f1b3e8SDimitry Andric       if ((execution_result == lldb::eExpressionInterrupted &&
20414f1b3e8SDimitry Andric            options.DoesUnwindOnError()) ||
20514f1b3e8SDimitry Andric           (execution_result == lldb::eExpressionHitBreakpoint &&
20614f1b3e8SDimitry Andric            options.DoesIgnoreBreakpoints()))
207f3fbd1c0SDimitry Andric         diagnostic_manager.AppendMessageToDiagnostic(
20814f1b3e8SDimitry Andric             "The process has been returned to the state before expression "
20914f1b3e8SDimitry Andric             "evaluation.");
21014f1b3e8SDimitry Andric       else {
211e81d9d49SDimitry Andric         if (execution_result == lldb::eExpressionHitBreakpoint)
212e81d9d49SDimitry Andric           user_expression_plan->TransferExpressionOwnership();
213f3fbd1c0SDimitry Andric         diagnostic_manager.AppendMessageToDiagnostic(
21414f1b3e8SDimitry Andric             "The process has been left at the point where it was "
21514f1b3e8SDimitry Andric             "interrupted, "
21614f1b3e8SDimitry Andric             "use \"thread return -x\" to return to the state before "
21714f1b3e8SDimitry Andric             "expression evaluation.");
218e81d9d49SDimitry Andric       }
219e81d9d49SDimitry Andric 
220e81d9d49SDimitry Andric       return execution_result;
22114f1b3e8SDimitry Andric     } else if (execution_result == lldb::eExpressionStoppedForDebug) {
22214f1b3e8SDimitry Andric       diagnostic_manager.PutString(
223ac9a064cSDimitry Andric           lldb::eSeverityInfo,
224e81d9d49SDimitry Andric           "Execution was halted at the first instruction of the expression "
225e81d9d49SDimitry Andric           "function because \"debug\" was requested.\n"
22614f1b3e8SDimitry Andric           "Use \"thread return -x\" to return to the state before expression "
22714f1b3e8SDimitry Andric           "evaluation.");
228e81d9d49SDimitry Andric       return execution_result;
229cfca06d7SDimitry Andric     } else if (execution_result == lldb::eExpressionThreadVanished) {
230cfca06d7SDimitry Andric       diagnostic_manager.Printf(
231ac9a064cSDimitry Andric           lldb::eSeverityError,
232cfca06d7SDimitry Andric           "Couldn't complete execution; the thread "
233cfca06d7SDimitry Andric           "on which the expression was being run: 0x%" PRIx64
234cfca06d7SDimitry Andric           " exited during its execution.",
235cfca06d7SDimitry Andric           expr_thread_id);
236cfca06d7SDimitry Andric       return execution_result;
23714f1b3e8SDimitry Andric     } else if (execution_result != lldb::eExpressionCompleted) {
23814f1b3e8SDimitry Andric       diagnostic_manager.Printf(
239ac9a064cSDimitry Andric           lldb::eSeverityError, "Couldn't execute function; result was %s",
240e81d9d49SDimitry Andric           Process::ExecutionResultAsCString(execution_result));
241e81d9d49SDimitry Andric       return execution_result;
242e81d9d49SDimitry Andric     }
243e81d9d49SDimitry Andric   }
244e81d9d49SDimitry Andric 
24514f1b3e8SDimitry Andric   if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
24614f1b3e8SDimitry Andric                            function_stack_bottom, function_stack_top)) {
247e81d9d49SDimitry Andric     return lldb::eExpressionCompleted;
24814f1b3e8SDimitry Andric   } else {
249e81d9d49SDimitry Andric     return lldb::eExpressionResultUnavailable;
250e81d9d49SDimitry Andric   }
251e81d9d49SDimitry Andric }
252e81d9d49SDimitry Andric 
FinalizeJITExecution(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,lldb::ExpressionVariableSP & result,lldb::addr_t function_stack_bottom,lldb::addr_t function_stack_top)25314f1b3e8SDimitry Andric bool LLVMUserExpression::FinalizeJITExecution(
25414f1b3e8SDimitry Andric     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
255e81d9d49SDimitry Andric     lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
25614f1b3e8SDimitry Andric     lldb::addr_t function_stack_top) {
257145449b1SDimitry Andric   Log *log = GetLog(LLDBLog::Expressions);
258e81d9d49SDimitry Andric 
259ead24645SDimitry Andric   LLDB_LOGF(log, "-- [UserExpression::FinalizeJITExecution] Dematerializing "
26014f1b3e8SDimitry Andric                  "after execution --");
261e81d9d49SDimitry Andric 
26214f1b3e8SDimitry Andric   if (!m_dematerializer_sp) {
263ac9a064cSDimitry Andric     diagnostic_manager.Printf(lldb::eSeverityError,
26414f1b3e8SDimitry Andric                               "Couldn't apply expression side effects : no "
26514f1b3e8SDimitry Andric                               "dematerializer is present");
266e81d9d49SDimitry Andric     return false;
267e81d9d49SDimitry Andric   }
268e81d9d49SDimitry Andric 
269b76161e4SDimitry Andric   Status dematerialize_error;
270e81d9d49SDimitry Andric 
27114f1b3e8SDimitry Andric   m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
27214f1b3e8SDimitry Andric                                      function_stack_top);
273e81d9d49SDimitry Andric 
27414f1b3e8SDimitry Andric   if (!dematerialize_error.Success()) {
275ac9a064cSDimitry Andric     diagnostic_manager.Printf(lldb::eSeverityError,
27614f1b3e8SDimitry Andric                               "Couldn't apply expression side effects : %s",
277e81d9d49SDimitry Andric                               dematerialize_error.AsCString("unknown error"));
278e81d9d49SDimitry Andric     return false;
279e81d9d49SDimitry Andric   }
280e81d9d49SDimitry Andric 
28114f1b3e8SDimitry Andric   result =
28214f1b3e8SDimitry Andric       GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
283e81d9d49SDimitry Andric 
284e81d9d49SDimitry Andric   if (result)
285e81d9d49SDimitry Andric     result->TransferAddress();
286e81d9d49SDimitry Andric 
287e81d9d49SDimitry Andric   m_dematerializer_sp.reset();
288e81d9d49SDimitry Andric 
289e81d9d49SDimitry Andric   return true;
290e81d9d49SDimitry Andric }
291e81d9d49SDimitry Andric 
PrepareToExecuteJITExpression(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,lldb::addr_t & struct_address)29214f1b3e8SDimitry Andric bool LLVMUserExpression::PrepareToExecuteJITExpression(
29314f1b3e8SDimitry Andric     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
29414f1b3e8SDimitry Andric     lldb::addr_t &struct_address) {
295e81d9d49SDimitry Andric   lldb::TargetSP target;
296e81d9d49SDimitry Andric   lldb::ProcessSP process;
297e81d9d49SDimitry Andric   lldb::StackFrameSP frame;
298e81d9d49SDimitry Andric 
29914f1b3e8SDimitry Andric   if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
30014f1b3e8SDimitry Andric     diagnostic_manager.PutString(
301ac9a064cSDimitry Andric         lldb::eSeverityError,
302f3fbd1c0SDimitry Andric         "The context has changed before we could JIT the expression!");
303e81d9d49SDimitry Andric     return false;
304e81d9d49SDimitry Andric   }
305e81d9d49SDimitry Andric 
30614f1b3e8SDimitry Andric   if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
30714f1b3e8SDimitry Andric     if (m_materialized_address == LLDB_INVALID_ADDRESS) {
308b76161e4SDimitry Andric       Status alloc_error;
309e81d9d49SDimitry Andric 
310e81d9d49SDimitry Andric       IRMemoryMap::AllocationPolicy policy =
31114f1b3e8SDimitry Andric           m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
31214f1b3e8SDimitry Andric                           : IRMemoryMap::eAllocationPolicyMirror;
313e81d9d49SDimitry Andric 
314e81d9d49SDimitry Andric       const bool zero_memory = false;
315e81d9d49SDimitry Andric 
31614f1b3e8SDimitry Andric       m_materialized_address = m_execution_unit_sp->Malloc(
3175f29bb8aSDimitry Andric           m_materializer_up->GetStructByteSize(),
3185f29bb8aSDimitry Andric           m_materializer_up->GetStructAlignment(),
31914f1b3e8SDimitry Andric           lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
32014f1b3e8SDimitry Andric           zero_memory, alloc_error);
321e81d9d49SDimitry Andric 
32214f1b3e8SDimitry Andric       if (!alloc_error.Success()) {
32314f1b3e8SDimitry Andric         diagnostic_manager.Printf(
324ac9a064cSDimitry Andric             lldb::eSeverityError,
325f3fbd1c0SDimitry Andric             "Couldn't allocate space for materialized struct: %s",
326f3fbd1c0SDimitry Andric             alloc_error.AsCString());
327e81d9d49SDimitry Andric         return false;
328e81d9d49SDimitry Andric       }
329e81d9d49SDimitry Andric     }
330e81d9d49SDimitry Andric 
331e81d9d49SDimitry Andric     struct_address = m_materialized_address;
332e81d9d49SDimitry Andric 
33314f1b3e8SDimitry Andric     if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
334b76161e4SDimitry Andric       Status alloc_error;
335e81d9d49SDimitry Andric 
3367fa27ce4SDimitry Andric       size_t stack_frame_size = target->GetExprAllocSize();
3377fa27ce4SDimitry Andric       if (stack_frame_size == 0) {
3387fa27ce4SDimitry Andric         ABISP abi_sp;
3397fa27ce4SDimitry Andric         if (process && (abi_sp = process->GetABI()))
3407fa27ce4SDimitry Andric           stack_frame_size = abi_sp->GetStackFrameSize();
3417fa27ce4SDimitry Andric         else
3427fa27ce4SDimitry Andric           stack_frame_size = 512 * 1024;
3437fa27ce4SDimitry Andric       }
344e81d9d49SDimitry Andric 
345e81d9d49SDimitry Andric       const bool zero_memory = false;
346e81d9d49SDimitry Andric 
34714f1b3e8SDimitry Andric       m_stack_frame_bottom = m_execution_unit_sp->Malloc(
34814f1b3e8SDimitry Andric           stack_frame_size, 8,
349e81d9d49SDimitry Andric           lldb::ePermissionsReadable | lldb::ePermissionsWritable,
35014f1b3e8SDimitry Andric           IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
351e81d9d49SDimitry Andric 
352e81d9d49SDimitry Andric       m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
353e81d9d49SDimitry Andric 
35414f1b3e8SDimitry Andric       if (!alloc_error.Success()) {
35514f1b3e8SDimitry Andric         diagnostic_manager.Printf(
356ac9a064cSDimitry Andric             lldb::eSeverityError,
35714f1b3e8SDimitry Andric             "Couldn't allocate space for the stack frame: %s",
358f3fbd1c0SDimitry Andric             alloc_error.AsCString());
359e81d9d49SDimitry Andric         return false;
360e81d9d49SDimitry Andric       }
361e81d9d49SDimitry Andric     }
362e81d9d49SDimitry Andric 
363b76161e4SDimitry Andric     Status materialize_error;
364e81d9d49SDimitry Andric 
3655f29bb8aSDimitry Andric     m_dematerializer_sp = m_materializer_up->Materialize(
36614f1b3e8SDimitry Andric         frame, *m_execution_unit_sp, struct_address, materialize_error);
367e81d9d49SDimitry Andric 
36814f1b3e8SDimitry Andric     if (!materialize_error.Success()) {
369ac9a064cSDimitry Andric       diagnostic_manager.Printf(lldb::eSeverityError,
37014f1b3e8SDimitry Andric                                 "Couldn't materialize: %s",
371f3fbd1c0SDimitry Andric                                 materialize_error.AsCString());
372e81d9d49SDimitry Andric       return false;
373e81d9d49SDimitry Andric     }
374e81d9d49SDimitry Andric   }
375e81d9d49SDimitry Andric   return true;
376e81d9d49SDimitry Andric }
377e81d9d49SDimitry Andric 
378