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/Expression/IRDynamicChecks.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                                                 ClangUserExpression::ClangUserExpressionSP &user_expression_sp) :
46     ThreadPlanCallFunction (thread, function, ClangASTType(), 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             StreamString  error_stream;
96 
97             ExecutionContext exe_ctx(GetThread());
98 
99             m_user_expression_sp->FinalizeJITExecution(error_stream, exe_ctx, m_result_var_sp, function_stack_bottom, function_stack_top);
100         }
101 
102         ThreadPlan::MischiefManaged ();
103         return true;
104     }
105     else
106     {
107         return false;
108     }
109 }
110 
111 StopInfoSP
112 ThreadPlanCallUserExpression::GetRealStopInfo()
113 {
114     StopInfoSP stop_info_sp = ThreadPlanCallFunction::GetRealStopInfo();
115 
116     if (stop_info_sp)
117     {
118         lldb::addr_t addr = GetStopAddress();
119         DynamicCheckerFunctions *checkers = m_thread.GetProcess()->GetDynamicCheckers();
120         StreamString s;
121 
122         if (checkers && checkers->DoCheckersExplainStop(addr, s))
123             stop_info_sp->SetDescription(s.GetData());
124     }
125 
126     return stop_info_sp;
127 }
128