Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame^] | 1 | //===-- ObjCTrampolineHandler.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 "ObjCTrampolineHandler.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Module.h" |
| 17 | #include "lldb/Core/ConstString.h" |
| 18 | #include "lldb/Core/FileSpec.h" |
| 19 | #include "lldb/Target/Thread.h" |
| 20 | #include "lldb/Target/RegisterContext.h" |
| 21 | #include "lldb/Target/Target.h" |
| 22 | #include "lldb/Target/Process.h" |
| 23 | #include "lldb/Core/Value.h" |
| 24 | #include "lldb/Symbol/ClangASTContext.h" |
| 25 | #include "lldb/Expression/ClangFunction.h" |
| 26 | #include "lldb/Core/Log.h" |
| 27 | #include "lldb/Target/ExecutionContext.h" |
| 28 | #include "ThreadPlanStepThroughObjCTrampoline.h" |
| 29 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
| 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | const ObjCTrampolineHandler::DispatchFunction |
| 35 | ObjCTrampolineHandler::g_dispatch_functions[] = |
| 36 | { |
| 37 | // NAME STRET SUPER FIXUP TYPE |
| 38 | {"objc_msgSend", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 39 | {"objc_msgSend_fixup", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 40 | {"objc_msgSend_fixedup", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 41 | {"objc_msgSend_stret", true, false, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 42 | {"objc_msgSend_stret_fixup", true, false, ObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 43 | {"objc_msgSend_stret_fixedup", true, false, ObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 44 | {"objc_msgSend_fpret", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 45 | {"objc_msgSend_fpret_fixup", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 46 | {"objc_msgSend_fpret_fixedup", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 47 | {"objc_msgSend_fp2ret", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 48 | {"objc_msgSend_fp2ret_fixup", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 49 | {"objc_msgSend_fp2ret_fixedup", false, false, ObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 50 | {"objc_msgSendSuper", false, true, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 51 | {"objc_msgSendSuper_stret", true, true, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 52 | {"objc_msgSendSuper2", false, true, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 53 | {"objc_msgSendSuper2_fixup", false, true, ObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 54 | {"objc_msgSendSuper2_fixedup", false, true, ObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 55 | {"objc_msgSendSuper2_stret", true, true, ObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 56 | {"objc_msgSendSuper2_stret_fixup", true, true, ObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 57 | {"objc_msgSendSuper2_stret_fixedup", true, true, ObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 58 | {NULL} |
| 59 | }; |
| 60 | |
| 61 | bool |
| 62 | ObjCTrampolineHandler::ModuleIsObjCLibrary (const ModuleSP &module_sp) |
| 63 | { |
| 64 | const FileSpec &module_file_spec = module_sp->GetFileSpec(); |
| 65 | static ConstString ObjCName ("libobjc.A.dylib"); |
| 66 | |
| 67 | if (module_file_spec) |
| 68 | { |
| 69 | if (module_file_spec.GetFilename() == ObjCName) |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | ObjCTrampolineHandler::ObjCTrampolineHandler (ProcessSP process_sp, ModuleSP objc_module) : |
| 77 | m_process_sp (process_sp), |
| 78 | m_objc_module_sp (objc_module), |
| 79 | m_impl_fn_addr (LLDB_INVALID_ADDRESS), |
| 80 | m_impl_stret_fn_addr (LLDB_INVALID_ADDRESS) |
| 81 | { |
| 82 | // Look up the known resolution functions: |
| 83 | |
| 84 | ConstString get_impl_name("class_getMethodImplementation"); |
| 85 | ConstString get_impl_stret_name("class_getMethodImplementation_stret"); |
| 86 | |
| 87 | const Symbol *class_getMethodImplementation = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_name, eSymbolTypeCode); |
| 88 | const Symbol *class_getMethodImplementation_stret = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_stret_name, eSymbolTypeCode); |
| 89 | |
| 90 | if (class_getMethodImplementation) |
| 91 | m_impl_fn_addr = class_getMethodImplementation->GetValue().GetLoadAddress(m_process_sp.get()); |
| 92 | if (class_getMethodImplementation_stret) |
| 93 | m_impl_stret_fn_addr = class_getMethodImplementation_stret->GetValue().GetLoadAddress(m_process_sp.get()); |
| 94 | |
| 95 | // FIXME: Do some kind of logging here. |
| 96 | if (m_impl_fn_addr == LLDB_INVALID_ADDRESS || m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) |
| 97 | return; |
| 98 | |
| 99 | // Look up the addresses for the objc dispatch functions and cache them. For now I'm inspecting the symbol |
| 100 | // names dynamically to figure out how to dispatch to them. If it becomes more complicated than this we can |
| 101 | // turn the g_dispatch_functions char * array into a template table, and populate the DispatchFunction map |
| 102 | // from there. |
| 103 | |
| 104 | for (int i = 0; g_dispatch_functions[i].name != NULL; i++) |
| 105 | { |
| 106 | ConstString name_const_str(g_dispatch_functions[i].name); |
| 107 | const Symbol *msgSend_symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType (name_const_str, eSymbolTypeCode); |
| 108 | if (msgSend_symbol) |
| 109 | { |
| 110 | // FixMe: Make g_dispatch_functions static table of DisptachFunctions, and have the map be address->index. |
| 111 | // Problem is we also need to lookup the dispatch function. For now we could have a side table of stret & non-stret |
| 112 | // dispatch functions. If that's as complex as it gets, we're fine. |
| 113 | |
| 114 | lldb::addr_t sym_addr = msgSend_symbol->GetValue().GetLoadAddress(m_process_sp.get()); |
| 115 | |
| 116 | m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ThreadPlanSP |
| 122 | ObjCTrampolineHandler::GetStepThroughDispatchPlan (Thread &thread, bool stop_others) |
| 123 | { |
| 124 | ThreadPlanSP ret_plan_sp; |
| 125 | lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); |
| 126 | |
| 127 | MsgsendMap::iterator pos; |
| 128 | pos = m_msgSend_map.find (curr_pc); |
| 129 | if (pos != m_msgSend_map.end()) |
| 130 | { |
| 131 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 132 | |
| 133 | const DispatchFunction *this_dispatch = &g_dispatch_functions[(*pos).second]; |
| 134 | |
| 135 | lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); |
| 136 | |
| 137 | Process *process = thread.CalculateProcess(); |
| 138 | const ABI *abi = process->GetABI(); |
| 139 | if (abi == NULL) |
| 140 | return ret_plan_sp; |
| 141 | |
| 142 | Target *target = thread.CalculateTarget(); |
| 143 | |
| 144 | // FIXME: Since neither the value nor the Clang QualType know their ASTContext, |
| 145 | // we have to make sure the type we put in our value list comes from the same ASTContext |
| 146 | // the ABI will use to get the argument values. THis is the bottom-most frame's module. |
| 147 | |
| 148 | ClangASTContext *clang_ast_context = target->GetScratchClangASTContext(); |
| 149 | ValueList argument_values; |
| 150 | Value input_value; |
| 151 | void *clang_void_ptr_type = clang_ast_context->GetVoidPtrType(false); |
| 152 | input_value.SetValueType (Value::eValueTypeScalar); |
| 153 | input_value.SetContext (Value::eContextTypeOpaqueClangQualType, clang_void_ptr_type); |
| 154 | |
| 155 | int obj_index; |
| 156 | int sel_index; |
| 157 | |
| 158 | // If this is a struct return dispatch, then the first argument is the |
| 159 | // return struct pointer, and the object is the second, and the selector is the third. |
| 160 | // Otherwise the object is the first and the selector the second. |
| 161 | if (this_dispatch->stret_return) |
| 162 | { |
| 163 | obj_index = 1; |
| 164 | sel_index = 2; |
| 165 | argument_values.PushValue(input_value); |
| 166 | argument_values.PushValue(input_value); |
| 167 | argument_values.PushValue(input_value); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | obj_index = 0; |
| 172 | sel_index = 1; |
| 173 | argument_values.PushValue(input_value); |
| 174 | argument_values.PushValue(input_value); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | bool success = abi->GetArgumentValues (thread, argument_values); |
| 179 | if (!success) |
| 180 | return ret_plan_sp; |
| 181 | |
| 182 | // Okay, the first value here is the object, we actually want the class of that object. |
| 183 | // For now we're just going with the ISA. |
| 184 | // FIXME: This should really be the return value of [object class] to properly handle KVO interposition. |
| 185 | |
| 186 | Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); |
| 187 | |
| 188 | // This is a little cheesy, but since object->isa is the first field, |
| 189 | // making the object value a load address value and resolving it will get |
| 190 | // the pointer sized data pointed to by that value... |
| 191 | ExecutionContext exec_ctx; |
| 192 | thread.Calculate (exec_ctx); |
| 193 | |
| 194 | isa_value.SetValueType(Value::eValueTypeLoadAddress); |
| 195 | isa_value.ResolveValue(&exec_ctx, clang_ast_context->getASTContext()); |
| 196 | |
| 197 | if (this_dispatch->fixedup == DispatchFunction::eFixUpFixed) |
| 198 | { |
| 199 | // For the FixedUp method the Selector is actually a pointer to a |
| 200 | // structure, the second field of which is the selector number. |
| 201 | Value *sel_value = argument_values.GetValueAtIndex(sel_index); |
| 202 | sel_value->GetScalar() += process->GetAddressByteSize(); |
| 203 | sel_value->SetValueType(Value::eValueTypeLoadAddress); |
| 204 | sel_value->ResolveValue(&exec_ctx, clang_ast_context->getASTContext()); |
| 205 | } |
| 206 | else if (this_dispatch->fixedup == DispatchFunction::eFixUpToFix) |
| 207 | { |
| 208 | // FIXME: If the method dispatch is not "fixed up" then the selector is actually a |
| 209 | // pointer to the string name of the selector. We need to look that up... |
| 210 | // For now I'm going to punt on that and just return no plan. |
| 211 | if (log) |
| 212 | log->Printf ("Punting on stepping into un-fixed-up method dispatch."); |
| 213 | return ret_plan_sp; |
| 214 | } |
| 215 | |
| 216 | // FIXME: If this is a dispatch to the super-class, we need to get the super-class from |
| 217 | // the class, and disaptch to that instead. |
| 218 | // But for now I just punt and return no plan. |
| 219 | if (this_dispatch->is_super) |
| 220 | { |
| 221 | if (log) |
| 222 | log->Printf ("Punting on stepping into super method dispatch."); |
| 223 | return ret_plan_sp; |
| 224 | } |
| 225 | |
| 226 | ValueList dispatch_values; |
| 227 | dispatch_values.PushValue (isa_value); |
| 228 | dispatch_values.PushValue(*(argument_values.GetValueAtIndex(sel_index))); |
| 229 | |
| 230 | if (log) |
| 231 | { |
| 232 | log->Printf("Resolving method call for class - 0x%llx and selector - 0x%llx", |
| 233 | dispatch_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 234 | dispatch_values.GetValueAtIndex(1)->GetScalar().ULongLong()); |
| 235 | } |
| 236 | |
| 237 | lldb::addr_t impl_addr = LookupInCache (dispatch_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 238 | dispatch_values.GetValueAtIndex(1)->GetScalar().ULongLong()); |
| 239 | |
| 240 | if (impl_addr == LLDB_INVALID_ADDRESS) |
| 241 | { |
| 242 | |
| 243 | Address resolve_address(NULL, this_dispatch->stret_return ? m_impl_stret_fn_addr : m_impl_fn_addr); |
| 244 | |
| 245 | StreamString errors; |
| 246 | { |
| 247 | // Scope for mutex locker: |
| 248 | Mutex::Locker (m_impl_function_mutex); |
| 249 | if (!m_impl_function.get()) |
| 250 | { |
| 251 | m_impl_function.reset(new ClangFunction(process->GetTargetTriple().GetCString(), |
| 252 | clang_ast_context, |
| 253 | clang_void_ptr_type, |
| 254 | resolve_address, |
| 255 | dispatch_values)); |
| 256 | |
| 257 | unsigned num_errors = m_impl_function->CompileFunction(errors); |
| 258 | if (num_errors) |
| 259 | { |
| 260 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 261 | if (log) |
| 262 | log->Printf ("Error compiling function: \"%s\".", errors.GetData()); |
| 263 | return ret_plan_sp; |
| 264 | } |
| 265 | |
| 266 | errors.Clear(); |
| 267 | if (!m_impl_function->WriteFunctionWrapper(exec_ctx, errors)) |
| 268 | { |
| 269 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 270 | if (log) |
| 271 | log->Printf ("Error Inserting function: \"%s\".", errors.GetData()); |
| 272 | return ret_plan_sp; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
| 278 | errors.Clear(); |
| 279 | |
| 280 | // Now write down the argument values for this call. |
| 281 | lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; |
| 282 | if (!m_impl_function->WriteFunctionArguments (exec_ctx, args_addr, resolve_address, dispatch_values, errors)) |
| 283 | return ret_plan_sp; |
| 284 | |
| 285 | ret_plan_sp.reset (new ThreadPlanStepThroughObjCTrampoline (thread, this, args_addr, |
| 286 | argument_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 287 | dispatch_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 288 | dispatch_values.GetValueAtIndex(1)->GetScalar().ULongLong(), |
| 289 | stop_others)); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | if (log) |
| 294 | log->Printf ("Found implementation address in cache: 0x%llx", impl_addr); |
| 295 | |
| 296 | ret_plan_sp.reset (new ThreadPlanRunToAddress (thread, impl_addr, stop_others)); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return ret_plan_sp; |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | ObjCTrampolineHandler::AddToCache (lldb::addr_t class_addr, lldb::addr_t selector, lldb::addr_t impl_addr) |
| 305 | { |
| 306 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 307 | if (log) |
| 308 | { |
| 309 | log->Printf ("Caching: class 0x%llx selector 0x%llx implementation 0x%llx.", class_addr, selector, impl_addr); |
| 310 | } |
| 311 | m_impl_cache.insert (std::pair<ClassAndSel,lldb::addr_t> (ClassAndSel(class_addr, selector), impl_addr)); |
| 312 | } |
| 313 | |
| 314 | lldb::addr_t |
| 315 | ObjCTrampolineHandler::LookupInCache (lldb::addr_t class_addr, lldb::addr_t selector) |
| 316 | { |
| 317 | MsgImplMap::iterator pos, end = m_impl_cache.end(); |
| 318 | pos = m_impl_cache.find (ClassAndSel(class_addr, selector)); |
| 319 | if (pos != end) |
| 320 | return (*pos).second; |
| 321 | return LLDB_INVALID_ADDRESS; |
| 322 | } |
| 323 | |
| 324 | ClangFunction * |
| 325 | ObjCTrampolineHandler::GetLookupImplementationWrapperFunction () |
| 326 | { |
| 327 | return m_impl_function.get(); |
| 328 | } |