1 //===-- ABI.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/ABI.h"
11 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Core/Value.h"
14 #include "lldb/Core/ValueObjectConstResult.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/TypeSystem.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Thread.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 ABISP
FindPlugin(lldb::ProcessSP process_sp,const ArchSpec & arch)24 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
25   ABISP abi_sp;
26   ABICreateInstance create_callback;
27 
28   for (uint32_t idx = 0;
29        (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
30        nullptr;
31        ++idx) {
32     abi_sp = create_callback(process_sp, arch);
33 
34     if (abi_sp)
35       return abi_sp;
36   }
37   abi_sp.reset();
38   return abi_sp;
39 }
40 
41 ABI::~ABI() = default;
42 
GetRegisterInfoByName(const ConstString & name,RegisterInfo & info)43 bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
44   uint32_t count = 0;
45   const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
46   if (register_info_array) {
47     const char *unique_name_cstr = name.GetCString();
48     uint32_t i;
49     for (i = 0; i < count; ++i) {
50       if (register_info_array[i].name == unique_name_cstr) {
51         info = register_info_array[i];
52         return true;
53       }
54     }
55     for (i = 0; i < count; ++i) {
56       if (register_info_array[i].alt_name == unique_name_cstr) {
57         info = register_info_array[i];
58         return true;
59       }
60     }
61   }
62   return false;
63 }
64 
GetRegisterInfoByKind(RegisterKind reg_kind,uint32_t reg_num,RegisterInfo & info)65 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
66                                 RegisterInfo &info) {
67   if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
68     return false;
69 
70   uint32_t count = 0;
71   const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
72   if (register_info_array) {
73     for (uint32_t i = 0; i < count; ++i) {
74       if (register_info_array[i].kinds[reg_kind] == reg_num) {
75         info = register_info_array[i];
76         return true;
77       }
78     }
79   }
80   return false;
81 }
82 
GetReturnValueObject(Thread & thread,CompilerType & ast_type,bool persistent) const83 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
84                                         bool persistent) const {
85   if (!ast_type.IsValid())
86     return ValueObjectSP();
87 
88   ValueObjectSP return_valobj_sp;
89 
90   return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
91   if (!return_valobj_sp)
92     return return_valobj_sp;
93 
94   // Now turn this into a persistent variable.
95   // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
96   // used in similar form in a couple
97   // of other places.  Figure out the correct Create function to do all this
98   // work.
99 
100   if (persistent) {
101     Target &target = *thread.CalculateTarget();
102     PersistentExpressionState *persistent_expression_state =
103         target.GetPersistentExpressionStateForLanguage(
104             ast_type.GetMinimumLanguage());
105 
106     if (!persistent_expression_state)
107       return ValueObjectSP();
108 
109     auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
110     ConstString persistent_variable_name =
111         persistent_expression_state->GetNextPersistentVariableName(target,
112                                                                    prefix);
113 
114     lldb::ValueObjectSP const_valobj_sp;
115 
116     // Check in case our value is already a constant value
117     if (return_valobj_sp->GetIsConstant()) {
118       const_valobj_sp = return_valobj_sp;
119       const_valobj_sp->SetName(persistent_variable_name);
120     } else
121       const_valobj_sp =
122           return_valobj_sp->CreateConstantValue(persistent_variable_name);
123 
124     lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
125 
126     return_valobj_sp = const_valobj_sp;
127 
128     ExpressionVariableSP clang_expr_variable_sp(
129         persistent_expression_state->CreatePersistentVariable(
130             return_valobj_sp));
131 
132     assert(clang_expr_variable_sp);
133 
134     // Set flags and live data as appropriate
135 
136     const Value &result_value = live_valobj_sp->GetValue();
137 
138     switch (result_value.GetValueType()) {
139     case Value::eValueTypeHostAddress:
140     case Value::eValueTypeFileAddress:
141       // we don't do anything with these for now
142       break;
143     case Value::eValueTypeScalar:
144     case Value::eValueTypeVector:
145       clang_expr_variable_sp->m_flags |=
146           ClangExpressionVariable::EVIsFreezeDried;
147       clang_expr_variable_sp->m_flags |=
148           ClangExpressionVariable::EVIsLLDBAllocated;
149       clang_expr_variable_sp->m_flags |=
150           ClangExpressionVariable::EVNeedsAllocation;
151       break;
152     case Value::eValueTypeLoadAddress:
153       clang_expr_variable_sp->m_live_sp = live_valobj_sp;
154       clang_expr_variable_sp->m_flags |=
155           ClangExpressionVariable::EVIsProgramReference;
156       break;
157     }
158 
159     return_valobj_sp = clang_expr_variable_sp->GetValueObject();
160   }
161   return return_valobj_sp;
162 }
163 
GetReturnValueObject(Thread & thread,llvm::Type & ast_type,bool persistent) const164 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
165                                         bool persistent) const {
166   ValueObjectSP return_valobj_sp;
167   return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
168   return return_valobj_sp;
169 }
170 
171 // specialized to work with llvm IR types
172 //
173 // for now we will specify a default implementation so that we don't need to
174 // modify other ABIs
GetReturnValueObjectImpl(Thread & thread,llvm::Type & ir_type) const175 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
176                                                   llvm::Type &ir_type) const {
177   ValueObjectSP return_valobj_sp;
178 
179   /* this is a dummy and will only be called if an ABI does not override this */
180 
181   return return_valobj_sp;
182 }
183 
PrepareTrivialCall(Thread & thread,lldb::addr_t sp,lldb::addr_t functionAddress,lldb::addr_t returnAddress,llvm::Type & returntype,llvm::ArrayRef<ABI::CallArgument> args) const184 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
185                              lldb::addr_t functionAddress,
186                              lldb::addr_t returnAddress, llvm::Type &returntype,
187                              llvm::ArrayRef<ABI::CallArgument> args) const {
188   // dummy prepare trivial call
189   llvm_unreachable("Should never get here!");
190 }
191 
GetFallbackRegisterLocation(const RegisterInfo * reg_info,UnwindPlan::Row::RegisterLocation & unwind_regloc)192 bool ABI::GetFallbackRegisterLocation(
193     const RegisterInfo *reg_info,
194     UnwindPlan::Row::RegisterLocation &unwind_regloc) {
195   // Did the UnwindPlan fail to give us the caller's stack pointer? The stack
196   // pointer is defined to be the same as THIS frame's CFA, so return the CFA
197   // value as the caller's stack pointer.  This is true on x86-32/x86-64 at
198   // least.
199   if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
200     unwind_regloc.SetIsCFAPlusOffset(0);
201     return true;
202   }
203 
204   // If a volatile register is being requested, we don't want to forward the
205   // next frame's register contents up the stack -- the register is not
206   // retrievable at this frame.
207   if (RegisterIsVolatile(reg_info)) {
208     unwind_regloc.SetUndefined();
209     return true;
210   }
211 
212   return false;
213 }
214