15ffd83dbSDimitry Andric //===-- LLVMUserExpression.cpp --------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric 
100b57cec5SDimitry Andric #include "lldb/Expression/LLVMUserExpression.h"
110b57cec5SDimitry Andric #include "lldb/Core/Module.h"
120b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
130b57cec5SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
149dba64beSDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
150b57cec5SDimitry Andric #include "lldb/Expression/IRExecutionUnit.h"
160b57cec5SDimitry Andric #include "lldb/Expression/IRInterpreter.h"
170b57cec5SDimitry Andric #include "lldb/Expression/Materializer.h"
180b57cec5SDimitry Andric #include "lldb/Host/HostInfo.h"
190b57cec5SDimitry Andric #include "lldb/Symbol/Block.h"
200b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
210b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
220b57cec5SDimitry Andric #include "lldb/Symbol/SymbolVendor.h"
230b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
25fe013be4SDimitry Andric #include "lldb/Target/ABI.h"
260b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
270b57cec5SDimitry Andric #include "lldb/Target/Process.h"
280b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
290b57cec5SDimitry Andric #include "lldb/Target/Target.h"
300b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
310b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanCallUserExpression.h"
320b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
3381ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
340b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
350b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
360b57cec5SDimitry Andric 
37fe013be4SDimitry Andric using namespace lldb;
380b57cec5SDimitry Andric using namespace lldb_private;
390b57cec5SDimitry Andric 
40480093f4SDimitry Andric char LLVMUserExpression::ID;
41480093f4SDimitry Andric 
LLVMUserExpression(ExecutionContextScope & exe_scope,llvm::StringRef expr,llvm::StringRef prefix,lldb::LanguageType language,ResultType desired_type,const EvaluateExpressionOptions & options)420b57cec5SDimitry Andric LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
430b57cec5SDimitry Andric                                        llvm::StringRef expr,
440b57cec5SDimitry Andric                                        llvm::StringRef prefix,
450b57cec5SDimitry Andric                                        lldb::LanguageType language,
460b57cec5SDimitry Andric                                        ResultType desired_type,
47480093f4SDimitry Andric                                        const EvaluateExpressionOptions &options)
48480093f4SDimitry Andric     : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
490b57cec5SDimitry Andric       m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
500b57cec5SDimitry Andric       m_stack_frame_top(LLDB_INVALID_ADDRESS), m_allow_cxx(false),
510b57cec5SDimitry Andric       m_allow_objc(false), m_transformed_text(), m_execution_unit_sp(),
52bdd1243dSDimitry Andric       m_materializer_up(), m_jit_module_wp(), m_target(nullptr),
53bdd1243dSDimitry Andric       m_can_interpret(false), m_materialized_address(LLDB_INVALID_ADDRESS) {}
540b57cec5SDimitry Andric 
~LLVMUserExpression()550b57cec5SDimitry Andric LLVMUserExpression::~LLVMUserExpression() {
560b57cec5SDimitry Andric   if (m_target) {
570b57cec5SDimitry Andric     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
580b57cec5SDimitry Andric     if (jit_module_sp)
590b57cec5SDimitry Andric       m_target->GetImages().Remove(jit_module_sp);
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric lldb::ExpressionResults
DoExecute(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,const EvaluateExpressionOptions & options,lldb::UserExpressionSP & shared_ptr_to_me,lldb::ExpressionVariableSP & result)640b57cec5SDimitry Andric LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
650b57cec5SDimitry Andric                               ExecutionContext &exe_ctx,
660b57cec5SDimitry Andric                               const EvaluateExpressionOptions &options,
670b57cec5SDimitry Andric                               lldb::UserExpressionSP &shared_ptr_to_me,
680b57cec5SDimitry Andric                               lldb::ExpressionVariableSP &result) {
690b57cec5SDimitry Andric   // The expression log is quite verbose, and if you're just tracking the
700b57cec5SDimitry Andric   // execution of the expression, it's quite convenient to have these logs come
710b57cec5SDimitry Andric   // out with the STEP log as well.
7281ad6265SDimitry Andric   Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
730b57cec5SDimitry Andric 
74480093f4SDimitry Andric   if (m_jit_start_addr == LLDB_INVALID_ADDRESS && !m_can_interpret) {
75480093f4SDimitry Andric     diagnostic_manager.PutString(
76480093f4SDimitry Andric         eDiagnosticSeverityError,
77480093f4SDimitry Andric         "Expression can't be run, because there is no JIT compiled function");
78480093f4SDimitry Andric     return lldb::eExpressionSetupError;
79480093f4SDimitry Andric   }
80480093f4SDimitry Andric 
810b57cec5SDimitry Andric   lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
840b57cec5SDimitry Andric                                      struct_address)) {
850b57cec5SDimitry Andric     diagnostic_manager.Printf(
860b57cec5SDimitry Andric         eDiagnosticSeverityError,
870b57cec5SDimitry Andric         "errored out in %s, couldn't PrepareToExecuteJITExpression",
880b57cec5SDimitry Andric         __FUNCTION__);
890b57cec5SDimitry Andric     return lldb::eExpressionSetupError;
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
930b57cec5SDimitry Andric   lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   if (m_can_interpret) {
960b57cec5SDimitry Andric     llvm::Module *module = m_execution_unit_sp->GetModule();
970b57cec5SDimitry Andric     llvm::Function *function = m_execution_unit_sp->GetFunction();
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric     if (!module || !function) {
1000b57cec5SDimitry Andric       diagnostic_manager.PutString(
1010b57cec5SDimitry Andric           eDiagnosticSeverityError,
1020b57cec5SDimitry Andric           "supposed to interpret, but nothing is there");
1030b57cec5SDimitry Andric       return lldb::eExpressionSetupError;
1040b57cec5SDimitry Andric     }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric     Status interpreter_error;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric     std::vector<lldb::addr_t> args;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric     if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
1110b57cec5SDimitry Andric       diagnostic_manager.Printf(eDiagnosticSeverityError,
1120b57cec5SDimitry Andric                                 "errored out in %s, couldn't AddArguments",
1130b57cec5SDimitry Andric                                 __FUNCTION__);
1140b57cec5SDimitry Andric       return lldb::eExpressionSetupError;
1150b57cec5SDimitry Andric     }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric     function_stack_bottom = m_stack_frame_bottom;
1180b57cec5SDimitry Andric     function_stack_top = m_stack_frame_top;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp,
1210b57cec5SDimitry Andric                              interpreter_error, function_stack_bottom,
122*c9157d92SDimitry Andric                              function_stack_top, exe_ctx, options.GetTimeout());
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric     if (!interpreter_error.Success()) {
1250b57cec5SDimitry Andric       diagnostic_manager.Printf(eDiagnosticSeverityError,
1260b57cec5SDimitry Andric                                 "supposed to interpret, but failed: %s",
1270b57cec5SDimitry Andric                                 interpreter_error.AsCString());
1280b57cec5SDimitry Andric       return lldb::eExpressionDiscarded;
1290b57cec5SDimitry Andric     }
1300b57cec5SDimitry Andric   } else {
1310b57cec5SDimitry Andric     if (!exe_ctx.HasThreadScope()) {
1320b57cec5SDimitry Andric       diagnostic_manager.Printf(eDiagnosticSeverityError,
1330b57cec5SDimitry Andric                                 "%s called with no thread selected",
1340b57cec5SDimitry Andric                                 __FUNCTION__);
1350b57cec5SDimitry Andric       return lldb::eExpressionSetupError;
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric 
1385ffd83dbSDimitry Andric     // Store away the thread ID for error reporting, in case it exits
1395ffd83dbSDimitry Andric     // during execution:
1405ffd83dbSDimitry Andric     lldb::tid_t expr_thread_id = exe_ctx.GetThreadRef().GetID();
1415ffd83dbSDimitry Andric 
1420b57cec5SDimitry Andric     Address wrapper_address(m_jit_start_addr);
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric     std::vector<lldb::addr_t> args;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric     if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
1470b57cec5SDimitry Andric       diagnostic_manager.Printf(eDiagnosticSeverityError,
1480b57cec5SDimitry Andric                                 "errored out in %s, couldn't AddArguments",
1490b57cec5SDimitry Andric                                 __FUNCTION__);
1500b57cec5SDimitry Andric       return lldb::eExpressionSetupError;
1510b57cec5SDimitry Andric     }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
1540b57cec5SDimitry Andric         exe_ctx.GetThreadRef(), wrapper_address, args, options,
1550b57cec5SDimitry Andric         shared_ptr_to_me));
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric     StreamString ss;
1580b57cec5SDimitry Andric     if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1590b57cec5SDimitry Andric       diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
1600b57cec5SDimitry Andric       return lldb::eExpressionSetupError;
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric     ThreadPlanCallUserExpression *user_expression_plan =
1640b57cec5SDimitry Andric         static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     lldb::addr_t function_stack_pointer =
1670b57cec5SDimitry Andric         user_expression_plan->GetFunctionStackPointer();
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric     function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
1700b57cec5SDimitry Andric     function_stack_top = function_stack_pointer;
1710b57cec5SDimitry Andric 
172480093f4SDimitry Andric     LLDB_LOGF(log,
1730b57cec5SDimitry Andric               "-- [UserExpression::Execute] Execution of expression begins --");
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric     if (exe_ctx.GetProcessPtr())
1760b57cec5SDimitry Andric       exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric     lldb::ExpressionResults execution_result =
1790b57cec5SDimitry Andric         exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
1800b57cec5SDimitry Andric                                               diagnostic_manager);
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric     if (exe_ctx.GetProcessPtr())
1830b57cec5SDimitry Andric       exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
1840b57cec5SDimitry Andric 
1859dba64beSDimitry Andric     LLDB_LOGF(log, "-- [UserExpression::Execute] Execution of expression "
1860b57cec5SDimitry Andric                    "completed --");
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric     if (execution_result == lldb::eExpressionInterrupted ||
1890b57cec5SDimitry Andric         execution_result == lldb::eExpressionHitBreakpoint) {
1900b57cec5SDimitry Andric       const char *error_desc = nullptr;
1910b57cec5SDimitry Andric 
192fe6060f1SDimitry Andric       if (user_expression_plan) {
193fe6060f1SDimitry Andric         if (auto real_stop_info_sp = user_expression_plan->GetRealStopInfo())
1940b57cec5SDimitry Andric           error_desc = real_stop_info_sp->GetDescription();
1950b57cec5SDimitry Andric       }
1960b57cec5SDimitry Andric       if (error_desc)
1970b57cec5SDimitry Andric         diagnostic_manager.Printf(eDiagnosticSeverityError,
1980b57cec5SDimitry Andric                                   "Execution was interrupted, reason: %s.",
1990b57cec5SDimitry Andric                                   error_desc);
2000b57cec5SDimitry Andric       else
2010b57cec5SDimitry Andric         diagnostic_manager.PutString(eDiagnosticSeverityError,
2020b57cec5SDimitry Andric                                      "Execution was interrupted.");
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric       if ((execution_result == lldb::eExpressionInterrupted &&
2050b57cec5SDimitry Andric            options.DoesUnwindOnError()) ||
2060b57cec5SDimitry Andric           (execution_result == lldb::eExpressionHitBreakpoint &&
2070b57cec5SDimitry Andric            options.DoesIgnoreBreakpoints()))
2080b57cec5SDimitry Andric         diagnostic_manager.AppendMessageToDiagnostic(
2090b57cec5SDimitry Andric             "The process has been returned to the state before expression "
2100b57cec5SDimitry Andric             "evaluation.");
2110b57cec5SDimitry Andric       else {
2120b57cec5SDimitry Andric         if (execution_result == lldb::eExpressionHitBreakpoint)
2130b57cec5SDimitry Andric           user_expression_plan->TransferExpressionOwnership();
2140b57cec5SDimitry Andric         diagnostic_manager.AppendMessageToDiagnostic(
2150b57cec5SDimitry Andric             "The process has been left at the point where it was "
2160b57cec5SDimitry Andric             "interrupted, "
2170b57cec5SDimitry Andric             "use \"thread return -x\" to return to the state before "
2180b57cec5SDimitry Andric             "expression evaluation.");
2190b57cec5SDimitry Andric       }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric       return execution_result;
2220b57cec5SDimitry Andric     } else if (execution_result == lldb::eExpressionStoppedForDebug) {
2230b57cec5SDimitry Andric       diagnostic_manager.PutString(
2240b57cec5SDimitry Andric           eDiagnosticSeverityRemark,
2250b57cec5SDimitry Andric           "Execution was halted at the first instruction of the expression "
2260b57cec5SDimitry Andric           "function because \"debug\" was requested.\n"
2270b57cec5SDimitry Andric           "Use \"thread return -x\" to return to the state before expression "
2280b57cec5SDimitry Andric           "evaluation.");
2290b57cec5SDimitry Andric       return execution_result;
2305ffd83dbSDimitry Andric     } else if (execution_result == lldb::eExpressionThreadVanished) {
2315ffd83dbSDimitry Andric       diagnostic_manager.Printf(
2325ffd83dbSDimitry Andric           eDiagnosticSeverityError,
2335ffd83dbSDimitry Andric           "Couldn't complete execution; the thread "
2345ffd83dbSDimitry Andric           "on which the expression was being run: 0x%" PRIx64
2355ffd83dbSDimitry Andric           " exited during its execution.",
2365ffd83dbSDimitry Andric           expr_thread_id);
2375ffd83dbSDimitry Andric       return execution_result;
2380b57cec5SDimitry Andric     } else if (execution_result != lldb::eExpressionCompleted) {
2390b57cec5SDimitry Andric       diagnostic_manager.Printf(
240480093f4SDimitry Andric           eDiagnosticSeverityError, "Couldn't execute function; result was %s",
2410b57cec5SDimitry Andric           Process::ExecutionResultAsCString(execution_result));
2420b57cec5SDimitry Andric       return execution_result;
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric   }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
2470b57cec5SDimitry Andric                            function_stack_bottom, function_stack_top)) {
2480b57cec5SDimitry Andric     return lldb::eExpressionCompleted;
2490b57cec5SDimitry Andric   } else {
2500b57cec5SDimitry Andric     return lldb::eExpressionResultUnavailable;
2510b57cec5SDimitry Andric   }
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric 
FinalizeJITExecution(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,lldb::ExpressionVariableSP & result,lldb::addr_t function_stack_bottom,lldb::addr_t function_stack_top)2540b57cec5SDimitry Andric bool LLVMUserExpression::FinalizeJITExecution(
2550b57cec5SDimitry Andric     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
2560b57cec5SDimitry Andric     lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
2570b57cec5SDimitry Andric     lldb::addr_t function_stack_top) {
25881ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Expressions);
2590b57cec5SDimitry Andric 
2609dba64beSDimitry Andric   LLDB_LOGF(log, "-- [UserExpression::FinalizeJITExecution] Dematerializing "
2610b57cec5SDimitry Andric                  "after execution --");
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   if (!m_dematerializer_sp) {
2640b57cec5SDimitry Andric     diagnostic_manager.Printf(eDiagnosticSeverityError,
2650b57cec5SDimitry Andric                               "Couldn't apply expression side effects : no "
2660b57cec5SDimitry Andric                               "dematerializer is present");
2670b57cec5SDimitry Andric     return false;
2680b57cec5SDimitry Andric   }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   Status dematerialize_error;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
2730b57cec5SDimitry Andric                                      function_stack_top);
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   if (!dematerialize_error.Success()) {
2760b57cec5SDimitry Andric     diagnostic_manager.Printf(eDiagnosticSeverityError,
2770b57cec5SDimitry Andric                               "Couldn't apply expression side effects : %s",
2780b57cec5SDimitry Andric                               dematerialize_error.AsCString("unknown error"));
2790b57cec5SDimitry Andric     return false;
2800b57cec5SDimitry Andric   }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   result =
2830b57cec5SDimitry Andric       GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   if (result)
2860b57cec5SDimitry Andric     result->TransferAddress();
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   m_dematerializer_sp.reset();
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   return true;
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric 
PrepareToExecuteJITExpression(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,lldb::addr_t & struct_address)2930b57cec5SDimitry Andric bool LLVMUserExpression::PrepareToExecuteJITExpression(
2940b57cec5SDimitry Andric     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
2950b57cec5SDimitry Andric     lldb::addr_t &struct_address) {
2960b57cec5SDimitry Andric   lldb::TargetSP target;
2970b57cec5SDimitry Andric   lldb::ProcessSP process;
2980b57cec5SDimitry Andric   lldb::StackFrameSP frame;
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
3010b57cec5SDimitry Andric     diagnostic_manager.PutString(
3020b57cec5SDimitry Andric         eDiagnosticSeverityError,
3030b57cec5SDimitry Andric         "The context has changed before we could JIT the expression!");
3040b57cec5SDimitry Andric     return false;
3050b57cec5SDimitry Andric   }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
3080b57cec5SDimitry Andric     if (m_materialized_address == LLDB_INVALID_ADDRESS) {
3090b57cec5SDimitry Andric       Status alloc_error;
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric       IRMemoryMap::AllocationPolicy policy =
3120b57cec5SDimitry Andric           m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
3130b57cec5SDimitry Andric                           : IRMemoryMap::eAllocationPolicyMirror;
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric       const bool zero_memory = false;
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric       m_materialized_address = m_execution_unit_sp->Malloc(
3180b57cec5SDimitry Andric           m_materializer_up->GetStructByteSize(),
3190b57cec5SDimitry Andric           m_materializer_up->GetStructAlignment(),
3200b57cec5SDimitry Andric           lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
3210b57cec5SDimitry Andric           zero_memory, alloc_error);
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric       if (!alloc_error.Success()) {
3240b57cec5SDimitry Andric         diagnostic_manager.Printf(
3250b57cec5SDimitry Andric             eDiagnosticSeverityError,
3260b57cec5SDimitry Andric             "Couldn't allocate space for materialized struct: %s",
3270b57cec5SDimitry Andric             alloc_error.AsCString());
3280b57cec5SDimitry Andric         return false;
3290b57cec5SDimitry Andric       }
3300b57cec5SDimitry Andric     }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric     struct_address = m_materialized_address;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric     if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
3350b57cec5SDimitry Andric       Status alloc_error;
3360b57cec5SDimitry Andric 
337fe013be4SDimitry Andric       size_t stack_frame_size = target->GetExprAllocSize();
338fe013be4SDimitry Andric       if (stack_frame_size == 0) {
339fe013be4SDimitry Andric         ABISP abi_sp;
340fe013be4SDimitry Andric         if (process && (abi_sp = process->GetABI()))
341fe013be4SDimitry Andric           stack_frame_size = abi_sp->GetStackFrameSize();
342fe013be4SDimitry Andric         else
343fe013be4SDimitry Andric           stack_frame_size = 512 * 1024;
344fe013be4SDimitry Andric       }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric       const bool zero_memory = false;
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric       m_stack_frame_bottom = m_execution_unit_sp->Malloc(
3490b57cec5SDimitry Andric           stack_frame_size, 8,
3500b57cec5SDimitry Andric           lldb::ePermissionsReadable | lldb::ePermissionsWritable,
3510b57cec5SDimitry Andric           IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric       m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric       if (!alloc_error.Success()) {
3560b57cec5SDimitry Andric         diagnostic_manager.Printf(
3570b57cec5SDimitry Andric             eDiagnosticSeverityError,
3580b57cec5SDimitry Andric             "Couldn't allocate space for the stack frame: %s",
3590b57cec5SDimitry Andric             alloc_error.AsCString());
3600b57cec5SDimitry Andric         return false;
3610b57cec5SDimitry Andric       }
3620b57cec5SDimitry Andric     }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric     Status materialize_error;
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric     m_dematerializer_sp = m_materializer_up->Materialize(
3670b57cec5SDimitry Andric         frame, *m_execution_unit_sp, struct_address, materialize_error);
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric     if (!materialize_error.Success()) {
3700b57cec5SDimitry Andric       diagnostic_manager.Printf(eDiagnosticSeverityError,
3710b57cec5SDimitry Andric                                 "Couldn't materialize: %s",
3720b57cec5SDimitry Andric                                 materialize_error.AsCString());
3730b57cec5SDimitry Andric       return false;
3740b57cec5SDimitry Andric     }
3750b57cec5SDimitry Andric   }
3760b57cec5SDimitry Andric   return true;
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric 
379