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/ClangUserExpression.h"
23 #include "lldb/Expression/IRDynamicChecks.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 
33 using namespace lldb;
34 using namespace lldb_private;
35 
36 //----------------------------------------------------------------------
37 // ThreadPlanCallUserExpression: Plan to call a single function
38 //----------------------------------------------------------------------
39 
40 ThreadPlanCallUserExpression::ThreadPlanCallUserExpression (Thread &thread,
41                                                 Address &function,
42                                                 llvm::ArrayRef<lldb::addr_t> args,
43                                                 const EvaluateExpressionOptions &options,
44                                                 lldb::ClangUserExpressionSP &user_expression_sp) :
45     ThreadPlanCallFunction (thread, function, ClangASTType(), args, options),
46     m_user_expression_sp (user_expression_sp)
47 {
48     // User expressions are generally "User generated" so we should set them up to stop when done.
49     SetIsMasterPlan (true);
50     SetOkayToDiscard(false);
51 }
52 
53 ThreadPlanCallUserExpression::~ThreadPlanCallUserExpression ()
54 {
55 }
56 
57 void
58 ThreadPlanCallUserExpression::GetDescription (Stream *s, lldb::DescriptionLevel level)
59 {
60     if (level == eDescriptionLevelBrief)
61         s->Printf("User Expression thread plan");
62     else
63         ThreadPlanCallFunction::GetDescription (s, level);
64 }
65 
66 void
67 ThreadPlanCallUserExpression::WillPop ()
68 {
69     ThreadPlanCallFunction::WillPop();
70     if (m_user_expression_sp)
71         m_user_expression_sp.reset();
72 }
73 
74 bool
75 ThreadPlanCallUserExpression::MischiefManaged ()
76 {
77     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
78 
79     if (IsPlanComplete())
80     {
81         if (log)
82             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
83                         static_cast<void*>(this));
84 
85         if (m_manage_materialization && PlanSucceeded() && m_user_expression_sp)
86         {
87             lldb::addr_t function_stack_top;
88             lldb::addr_t function_stack_bottom;
89             lldb::addr_t function_stack_pointer = GetFunctionStackPointer();
90 
91             function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
92             function_stack_top = function_stack_pointer;
93 
94             StreamString  error_stream;
95 
96             ExecutionContext exe_ctx(GetThread());
97 
98             m_user_expression_sp->FinalizeJITExecution(error_stream, exe_ctx, m_result_var_sp, function_stack_bottom, function_stack_top);
99         }
100 
101         ThreadPlan::MischiefManaged ();
102         return true;
103     }
104     else
105     {
106         return false;
107     }
108 }
109 
110 StopInfoSP
111 ThreadPlanCallUserExpression::GetRealStopInfo()
112 {
113     StopInfoSP stop_info_sp = ThreadPlanCallFunction::GetRealStopInfo();
114 
115     if (stop_info_sp)
116     {
117         lldb::addr_t addr = GetStopAddress();
118         DynamicCheckerFunctions *checkers = m_thread.GetProcess()->GetDynamicCheckers();
119         StreamString s;
120 
121         if (checkers && checkers->DoCheckersExplainStop(addr, s))
122             stop_info_sp->SetDescription(s.GetData());
123     }
124 
125     return stop_info_sp;
126 }
127