1 //===-- ThreadPlanCallFunction.h --------------------------------*- 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 #ifndef liblldb_ThreadPlanCallFunction_h_
11 #define liblldb_ThreadPlanCallFunction_h_
12 
13 #include "lldb/Target/Thread.h"
14 #include "lldb/Target/ThreadPlan.h"
15 #include "lldb/lldb-private.h"
16 
17 #include "llvm/ADT/ArrayRef.h"
18 
19 namespace lldb_private {
20 
21 class ThreadPlanCallFunction : public ThreadPlan {
22   // Create a thread plan to call a function at the address passed in the
23   // "function" argument.  If you plan to call GetReturnValueObject, then pass
24   // in the return type, otherwise just pass in an invalid CompilerType.
25 public:
26   ThreadPlanCallFunction(Thread &thread, const Address &function,
27                          const CompilerType &return_type,
28                          llvm::ArrayRef<lldb::addr_t> args,
29                          const EvaluateExpressionOptions &options);
30 
31   ThreadPlanCallFunction(Thread &thread, const Address &function,
32                          const EvaluateExpressionOptions &options);
33 
34   ~ThreadPlanCallFunction() override;
35 
36   void GetDescription(Stream *s, lldb::DescriptionLevel level) override;
37 
38   bool ValidatePlan(Stream *error) override;
39 
40   bool ShouldStop(Event *event_ptr) override;
41 
42   Vote ShouldReportStop(Event *event_ptr) override;
43 
44   bool StopOthers() override;
45 
46   lldb::StateType GetPlanRunState() override;
47 
48   void DidPush() override;
49 
50   bool WillStop() override;
51 
52   bool MischiefManaged() override;
53 
54   // To get the return value from a function call you must create a
55   // lldb::ValueSP that contains a valid clang type in its context and call
56   // RequestReturnValue. The ValueSP will be stored and when the function is
57   // done executing, the object will check if there is a requested return
58   // value. If there is, the return value will be retrieved using the
59   // ABI::GetReturnValue() for the ABI in the process. Then after the thread
60   // plan is complete, you can call "GetReturnValue()" to retrieve the value
61   // that was extracted.
62 
GetReturnValueObject()63   lldb::ValueObjectSP GetReturnValueObject() override {
64     return m_return_valobj_sp;
65   }
66 
67   // Return the stack pointer that the function received on entry.  Any stack
68   // address below this should be considered invalid after the function has
69   // been cleaned up.
GetFunctionStackPointer()70   lldb::addr_t GetFunctionStackPointer() { return m_function_sp; }
71 
72   // Classes that derive from FunctionCaller, and implement their own WillPop
73   // methods should call this so that the thread state gets restored if the
74   // plan gets discarded.
75   void WillPop() override;
76 
77   // If the thread plan stops mid-course, this will be the stop reason that
78   // interrupted us. Once DoTakedown is called, this will be the real stop
79   // reason at the end of the function call. If it hasn't been set for one or
80   // the other of these reasons, we'll return the PrivateStopReason. This is
81   // needed because we want the CallFunction thread plans not to show up as the
82   // stop reason. But if something bad goes wrong, it is nice to be able to
83   // tell the user what really happened.
84 
GetRealStopInfo()85   lldb::StopInfoSP GetRealStopInfo() override {
86     if (m_real_stop_info_sp)
87       return m_real_stop_info_sp;
88     else
89       return GetPrivateStopInfo();
90   }
91 
GetStopAddress()92   lldb::addr_t GetStopAddress() { return m_stop_address; }
93 
94   bool RestoreThreadState() override;
95 
ThreadDestroyed()96   void ThreadDestroyed() override { m_takedown_done = true; }
97 
98   void SetStopOthers(bool new_value) override;
99 
100 protected:
101   void ReportRegisterState(const char *message);
102 
103   bool DoPlanExplainsStop(Event *event_ptr) override;
104 
105   virtual void SetReturnValue();
106 
107   bool ConstructorSetup(Thread &thread, ABI *&abi,
108                         lldb::addr_t &start_load_addr,
109                         lldb::addr_t &function_load_addr);
110 
111   virtual void DoTakedown(bool success);
112 
113   void SetBreakpoints();
114 
115   void ClearBreakpoints();
116 
117   bool BreakpointsExplainStop();
118 
119   bool m_valid;
120   bool m_stop_other_threads;
121   bool m_unwind_on_error;
122   bool m_ignore_breakpoints;
123   bool m_debug_execution;
124   bool m_trap_exceptions;
125   Address m_function_addr;
126   Address m_start_addr;
127   lldb::addr_t m_function_sp;
128   lldb::ThreadPlanSP m_subplan_sp;
129   LanguageRuntime *m_cxx_language_runtime;
130   LanguageRuntime *m_objc_language_runtime;
131   Thread::ThreadStateCheckpoint m_stored_thread_state;
132   lldb::StopInfoSP
133       m_real_stop_info_sp; // In general we want to hide call function
134                            // thread plans, but for reporting purposes, it's
135                            // nice to know the real stop reason. This gets set
136                            // in DoTakedown.
137   StreamString m_constructor_errors;
138   lldb::ValueObjectSP m_return_valobj_sp; // If this contains a valid pointer,
139                                           // use the ABI to extract values when
140                                           // complete
141   bool m_takedown_done; // We want to ensure we only do the takedown once.  This
142                         // ensures that.
143   bool m_should_clear_objc_exception_bp;
144   bool m_should_clear_cxx_exception_bp;
145   lldb::addr_t m_stop_address; // This is the address we stopped at.  Also set
146                                // in DoTakedown;
147 
148 private:
149   CompilerType m_return_type;
150   DISALLOW_COPY_AND_ASSIGN(ThreadPlanCallFunction);
151 };
152 
153 } // namespace lldb_private
154 
155 #endif // liblldb_ThreadPlanCallFunction_h_
156