xref: /llvm-project/lldb/source/Target/ThreadPlanCallUserExpression.cpp (revision b9c1b51e45b845debb76d8658edabca70ca56079)
1 //===-- ThreadPlanCallUserExpression.cpp ------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "lldb/Target/ThreadPlanCallUserExpression.h"
12 
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 
17 // Project includes
18 #include "lldb/Breakpoint/Breakpoint.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Expression/DiagnosticManager.h"
24 #include "lldb/Expression/IRDynamicChecks.h"
25 #include "lldb/Expression/UserExpression.h"
26 #include "lldb/Host/HostInfo.h"
27 #include "lldb/Target/LanguageRuntime.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/RegisterContext.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33 #include "lldb/Target/ThreadPlanRunToAddress.h"
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 //----------------------------------------------------------------------
39 // ThreadPlanCallUserExpression: Plan to call a single function
40 //----------------------------------------------------------------------
41 
42 ThreadPlanCallUserExpression::ThreadPlanCallUserExpression(
43     Thread &thread, Address &function, llvm::ArrayRef<lldb::addr_t> args,
44     const EvaluateExpressionOptions &options,
45     lldb::UserExpressionSP &user_expression_sp)
46     : ThreadPlanCallFunction(thread, function, CompilerType(), args, options),
47       m_user_expression_sp(user_expression_sp) {
48   // User expressions are generally "User generated" so we should set them up to
49   // stop when done.
50   SetIsMasterPlan(true);
51   SetOkayToDiscard(false);
52 }
53 
54 ThreadPlanCallUserExpression::~ThreadPlanCallUserExpression() {}
55 
56 void ThreadPlanCallUserExpression::GetDescription(
57     Stream *s, lldb::DescriptionLevel level) {
58   if (level == eDescriptionLevelBrief)
59     s->Printf("User Expression thread plan");
60   else
61     ThreadPlanCallFunction::GetDescription(s, level);
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