1 //===-- ThreadPlanCallFunctionUsingABI.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/ThreadPlanCallFunctionUsingABI.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Target/Process.h"
13 #include "lldb/Target/RegisterContext.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Stream.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 //--------------------------------------------------------------------------------------------
23 // ThreadPlanCallFunctionUsingABI: Plan to call a single function using the ABI
24 // instead of JIT
25 //-------------------------------------------------------------------------------------------
ThreadPlanCallFunctionUsingABI(Thread & thread,const Address & function,llvm::Type & prototype,llvm::Type & return_type,llvm::ArrayRef<ABI::CallArgument> args,const EvaluateExpressionOptions & options)26 ThreadPlanCallFunctionUsingABI::ThreadPlanCallFunctionUsingABI(
27     Thread &thread, const Address &function, llvm::Type &prototype,
28     llvm::Type &return_type, llvm::ArrayRef<ABI::CallArgument> args,
29     const EvaluateExpressionOptions &options)
30     : ThreadPlanCallFunction(thread, function, options),
31       m_return_type(return_type) {
32   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
33   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
34   ABI *abi = nullptr;
35 
36   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
37     return;
38 
39   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
40                                start_load_addr, prototype, args))
41     return;
42 
43   ReportRegisterState("ABI Function call was set up.  Register state was:");
44 
45   m_valid = true;
46 }
47 
48 ThreadPlanCallFunctionUsingABI::~ThreadPlanCallFunctionUsingABI() = default;
49 
GetDescription(Stream * s,DescriptionLevel level)50 void ThreadPlanCallFunctionUsingABI::GetDescription(Stream *s,
51                                                     DescriptionLevel level) {
52   if (level == eDescriptionLevelBrief) {
53     s->Printf("Function call thread plan using ABI instead of JIT");
54   } else {
55     TargetSP target_sp(m_thread.CalculateTarget());
56     s->Printf("Thread plan to call 0x%" PRIx64 " using ABI instead of JIT",
57               m_function_addr.GetLoadAddress(target_sp.get()));
58   }
59 }
60 
SetReturnValue()61 void ThreadPlanCallFunctionUsingABI::SetReturnValue() {
62   ProcessSP process_sp(m_thread.GetProcess());
63   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
64 
65   // Ask the abi for the return value
66   if (abi) {
67     const bool persistent = false;
68     m_return_valobj_sp =
69         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
70   }
71 }
72