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/Core/Stream.h"
22 #include "lldb/Expression/DiagnosticManager.h"
23 #include "lldb/Expression/IRDynamicChecks.h"
24 #include "lldb/Expression/UserExpression.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Target/LanguageRuntime.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/StopInfo.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Target/ThreadPlanRunToAddress.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 (Thread &thread,
42                                                 Address &function,
43                                                 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 {
49     // User expressions are generally "User generated" so we should set them up to stop when done.
50     SetIsMasterPlan (true);
51     SetOkayToDiscard(false);
52 }
53 
54 ThreadPlanCallUserExpression::~ThreadPlanCallUserExpression ()
55 {
56 }
57 
58 void
59 ThreadPlanCallUserExpression::GetDescription (Stream *s, lldb::DescriptionLevel level)
60 {
61     if (level == eDescriptionLevelBrief)
62         s->Printf("User Expression thread plan");
63     else
64         ThreadPlanCallFunction::GetDescription (s, level);
65 }
66 
67 void
68 ThreadPlanCallUserExpression::WillPop ()
69 {
70     ThreadPlanCallFunction::WillPop();
71     if (m_user_expression_sp)
72         m_user_expression_sp.reset();
73 }
74 
75 bool
76 ThreadPlanCallUserExpression::MischiefManaged ()
77 {
78     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
79 
80     if (IsPlanComplete())
81     {
82         if (log)
83             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
84                         static_cast<void*>(this));
85 
86         if (m_manage_materialization && PlanSucceeded() && m_user_expression_sp)
87         {
88             lldb::addr_t function_stack_top;
89             lldb::addr_t function_stack_bottom;
90             lldb::addr_t function_stack_pointer = GetFunctionStackPointer();
91 
92             function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
93             function_stack_top = function_stack_pointer;
94 
95             DiagnosticManager diagnostics;
96 
97             ExecutionContext exe_ctx(GetThread());
98 
99             m_user_expression_sp->FinalizeJITExecution(diagnostics, exe_ctx, m_result_var_sp, function_stack_bottom,
100                                                        function_stack_top);
101         }
102 
103         ThreadPlan::MischiefManaged();
104         return true;
105     }
106     else
107     {
108         return false;
109     }
110 }
111 
112 StopInfoSP
113 ThreadPlanCallUserExpression::GetRealStopInfo()
114 {
115     StopInfoSP stop_info_sp = ThreadPlanCallFunction::GetRealStopInfo();
116 
117     if (stop_info_sp)
118     {
119         lldb::addr_t addr = GetStopAddress();
120         DynamicCheckerFunctions *checkers = m_thread.GetProcess()->GetDynamicCheckers();
121         StreamString s;
122 
123         if (checkers && checkers->DoCheckersExplainStop(addr, s))
124             stop_info_sp->SetDescription(s.GetData());
125     }
126 
127     return stop_info_sp;
128 }
129