1 //===-- ThreadPlanCallUserExpression.cpp -------------------------*- C++-*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/ThreadPlanCallUserExpression.h"
10 
11 
12 #include "lldb/Breakpoint/Breakpoint.h"
13 #include "lldb/Breakpoint/BreakpointLocation.h"
14 #include "lldb/Core/Address.h"
15 #include "lldb/Expression/DiagnosticManager.h"
16 #include "lldb/Expression/IRDynamicChecks.h"
17 #include "lldb/Expression/UserExpression.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Target/LanguageRuntime.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/StopInfo.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
25 #include "lldb/Target/ThreadPlanRunToAddress.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/Stream.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 //----------------------------------------------------------------------
33 // ThreadPlanCallUserExpression: Plan to call a single function
34 //----------------------------------------------------------------------
35 
36 ThreadPlanCallUserExpression::ThreadPlanCallUserExpression(
37     Thread &thread, Address &function, llvm::ArrayRef<lldb::addr_t> args,
38     const EvaluateExpressionOptions &options,
39     lldb::UserExpressionSP &user_expression_sp)
40     : ThreadPlanCallFunction(thread, function, CompilerType(), args, options),
41       m_user_expression_sp(user_expression_sp) {
42   // User expressions are generally "User generated" so we should set them up
43   // to stop when done.
44   SetIsMasterPlan(true);
45   SetOkayToDiscard(false);
46 }
47 
48 ThreadPlanCallUserExpression::~ThreadPlanCallUserExpression() {}
49 
50 void ThreadPlanCallUserExpression::GetDescription(
51     Stream *s, lldb::DescriptionLevel level) {
52   if (level == eDescriptionLevelBrief)
53     s->Printf("User Expression thread plan");
54   else
55     ThreadPlanCallFunction::GetDescription(s, level);
56 }
57 
58 void ThreadPlanCallUserExpression::DidPush() {
59   ThreadPlanCallFunction::DidPush();
60   if (m_user_expression_sp)
61     m_user_expression_sp->WillStartExecuting();
62 }
63 
64 void ThreadPlanCallUserExpression::WillPop() {
65   ThreadPlanCallFunction::WillPop();
66   if (m_user_expression_sp)
67     m_user_expression_sp.reset();
68 }
69 
70 bool ThreadPlanCallUserExpression::MischiefManaged() {
71   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
72 
73   if (IsPlanComplete()) {
74     if (log)
75       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
76                   static_cast<void *>(this));
77 
78     if (m_manage_materialization && PlanSucceeded() && m_user_expression_sp) {
79       lldb::addr_t function_stack_top;
80       lldb::addr_t function_stack_bottom;
81       lldb::addr_t function_stack_pointer = GetFunctionStackPointer();
82 
83       function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
84       function_stack_top = function_stack_pointer;
85 
86       DiagnosticManager diagnostics;
87 
88       ExecutionContext exe_ctx(GetThread());
89 
90       m_user_expression_sp->FinalizeJITExecution(
91           diagnostics, exe_ctx, m_result_var_sp, function_stack_bottom,
92           function_stack_top);
93     }
94 
95     ThreadPlan::MischiefManaged();
96     return true;
97   } else {
98     return false;
99   }
100 }
101 
102 StopInfoSP ThreadPlanCallUserExpression::GetRealStopInfo() {
103   StopInfoSP stop_info_sp = ThreadPlanCallFunction::GetRealStopInfo();
104 
105   if (stop_info_sp) {
106     lldb::addr_t addr = GetStopAddress();
107     DynamicCheckerFunctions *checkers =
108         m_thread.GetProcess()->GetDynamicCheckers();
109     StreamString s;
110 
111     if (checkers && checkers->DoCheckersExplainStop(addr, s))
112       stop_info_sp->SetDescription(s.GetData());
113   }
114 
115   return stop_info_sp;
116 }
117 
118 void ThreadPlanCallUserExpression::DoTakedown(bool success) {
119   ThreadPlanCallFunction::DoTakedown(success);
120   m_user_expression_sp->DidFinishExecuting();
121 }
122