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