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