2010-11-05 02:30:59 +08:00
|
|
|
//===-- AppleObjCRuntime.cpp --------------------------------------*- C++ -*-===//
|
2010-09-23 10:01:19 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-05 02:30:59 +08:00
|
|
|
#include "AppleObjCRuntime.h"
|
2010-09-28 09:25:32 +08:00
|
|
|
#include "AppleObjCTrampolineHandler.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
|
2010-11-04 06:19:38 +08:00
|
|
|
#include "llvm/Support/MachO.h"
|
2010-09-30 08:54:27 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
|
2010-11-04 06:19:38 +08:00
|
|
|
#include "lldb/Breakpoint/BreakpointLocation.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
#include "lldb/Core/ConstString.h"
|
|
|
|
#include "lldb/Core/Error.h"
|
2010-09-28 09:25:32 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
2010-11-05 02:30:59 +08:00
|
|
|
#include "lldb/Core/ModuleList.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
#include "lldb/Core/PluginManager.h"
|
|
|
|
#include "lldb/Core/Scalar.h"
|
2010-11-05 02:30:59 +08:00
|
|
|
#include "lldb/Core/Section.h"
|
2010-09-28 09:25:32 +08:00
|
|
|
#include "lldb/Core/StreamString.h"
|
|
|
|
#include "lldb/Expression/ClangFunction.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
#include "lldb/Symbol/ClangASTContext.h"
|
2010-11-05 02:30:59 +08:00
|
|
|
#include "lldb/Symbol/ObjectFile.h"
|
2010-09-28 09:25:32 +08:00
|
|
|
#include "lldb/Target/ExecutionContext.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
#include "lldb/Target/Process.h"
|
|
|
|
#include "lldb/Target/RegisterContext.h"
|
2010-11-04 06:19:38 +08:00
|
|
|
#include "lldb/Target/StopInfo.h"
|
2010-09-23 10:01:19 +08:00
|
|
|
#include "lldb/Target/Target.h"
|
|
|
|
#include "lldb/Target/Thread.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2010-09-28 09:25:32 +08:00
|
|
|
bool
|
2011-04-01 07:01:21 +08:00
|
|
|
AppleObjCRuntime::GetObjectDescription (Stream &str, ValueObject &object)
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
2011-03-18 08:05:18 +08:00
|
|
|
bool is_signed;
|
|
|
|
// ObjC objects can only be pointers, but we extend this to integer types because an expression might just
|
|
|
|
// result in an address, and we should try that to see if the address is an ObjC object.
|
|
|
|
|
|
|
|
if (!(object.IsPointerType() || object.IsIntegerType(is_signed)))
|
2010-09-30 08:54:27 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Make the argument list: we pass one arg, the address of our pointer, to the print function.
|
|
|
|
Scalar scalar;
|
|
|
|
|
|
|
|
if (!ClangASTType::GetValueAsScalar (object.GetClangAST(),
|
|
|
|
object.GetClangType(),
|
|
|
|
object.GetDataExtractor(),
|
|
|
|
0,
|
|
|
|
object.GetByteSize(),
|
|
|
|
scalar))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Value val(scalar);
|
2011-04-01 07:01:21 +08:00
|
|
|
return GetObjectDescription(str, val, object.GetExecutionContextScope());
|
2010-09-30 08:54:27 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
bool
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::GetObjectDescription (Stream &str, Value &value, ExecutionContextScope *exe_scope)
|
2010-09-30 08:54:27 +08:00
|
|
|
{
|
2010-09-29 00:06:31 +08:00
|
|
|
if (!m_read_objc_library)
|
|
|
|
return false;
|
|
|
|
|
2010-09-28 09:25:32 +08:00
|
|
|
ExecutionContext exe_ctx;
|
2010-10-04 09:05:56 +08:00
|
|
|
exe_scope->CalculateExecutionContext(exe_ctx);
|
2010-09-28 09:25:32 +08:00
|
|
|
|
|
|
|
if (!exe_ctx.process)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We need other parts of the exe_ctx, but the processes have to match.
|
|
|
|
assert (m_process == exe_ctx.process);
|
|
|
|
|
|
|
|
// Get the function address for the print function.
|
|
|
|
const Address *function_address = GetPrintForDebuggerAddr();
|
|
|
|
if (!function_address)
|
|
|
|
return false;
|
|
|
|
|
2010-09-30 08:54:27 +08:00
|
|
|
if (value.GetClangType())
|
|
|
|
{
|
|
|
|
clang::QualType value_type = clang::QualType::getFromOpaquePtr (value.GetClangType());
|
|
|
|
if (!value_type->isObjCObjectPointerType())
|
|
|
|
{
|
|
|
|
str.Printf ("Value doesn't point to an ObjC object.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If it is not a pointer, see if we can make it into a pointer.
|
|
|
|
ClangASTContext *ast_context = exe_ctx.target->GetScratchClangASTContext();
|
|
|
|
void *opaque_type_ptr = ast_context->GetBuiltInType_objc_id();
|
|
|
|
if (opaque_type_ptr == NULL)
|
|
|
|
opaque_type_ptr = ast_context->GetVoidPtrType(false);
|
2010-11-13 11:52:47 +08:00
|
|
|
value.SetContext(Value::eContextTypeClangType, opaque_type_ptr);
|
2010-09-30 08:54:27 +08:00
|
|
|
}
|
|
|
|
|
2010-09-28 09:25:32 +08:00
|
|
|
ValueList arg_value_list;
|
2010-09-30 08:54:27 +08:00
|
|
|
arg_value_list.PushValue(value);
|
2010-09-28 09:25:32 +08:00
|
|
|
|
|
|
|
// This is the return value:
|
|
|
|
ClangASTContext *ast_context = exe_ctx.target->GetScratchClangASTContext();
|
|
|
|
|
|
|
|
void *return_qualtype = ast_context->GetCStringType(true);
|
|
|
|
Value ret;
|
2010-11-13 11:52:47 +08:00
|
|
|
ret.SetContext(Value::eContextTypeClangType, return_qualtype);
|
2010-09-28 09:25:32 +08:00
|
|
|
|
|
|
|
// Now we're ready to call the function:
|
2011-03-18 04:02:56 +08:00
|
|
|
ClangFunction func (*exe_ctx.GetBestExecutionContextScope(),
|
2011-02-16 05:59:32 +08:00
|
|
|
ast_context,
|
|
|
|
return_qualtype,
|
|
|
|
*function_address,
|
|
|
|
arg_value_list);
|
|
|
|
|
2010-09-28 09:25:32 +08:00
|
|
|
StreamString error_stream;
|
|
|
|
|
|
|
|
lldb::addr_t wrapper_struct_addr = LLDB_INVALID_ADDRESS;
|
|
|
|
func.InsertFunction(exe_ctx, wrapper_struct_addr, error_stream);
|
|
|
|
|
2010-11-06 03:25:48 +08:00
|
|
|
bool unwind_on_error = true;
|
|
|
|
bool try_all_threads = true;
|
|
|
|
bool stop_others = true;
|
|
|
|
|
2010-12-14 10:59:59 +08:00
|
|
|
ExecutionResults results = func.ExecuteFunction (exe_ctx,
|
|
|
|
&wrapper_struct_addr,
|
|
|
|
error_stream,
|
|
|
|
stop_others,
|
|
|
|
1000000,
|
|
|
|
try_all_threads,
|
|
|
|
unwind_on_error,
|
|
|
|
ret);
|
2011-03-25 05:19:54 +08:00
|
|
|
if (results != eExecutionCompleted)
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
|
|
|
str.Printf("Error evaluating Print Object function: %d.\n", results);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_t result_ptr = ret.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
|
|
|
|
|
|
|
|
// FIXME: poor man's strcpy - we should have a "read memory as string interface...
|
|
|
|
|
|
|
|
Error error;
|
|
|
|
std::vector<char> desc;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
char byte = '\0';
|
|
|
|
if (exe_ctx.process->ReadMemory(result_ptr + desc.size(), &byte, 1, error) != 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
desc.push_back(byte);
|
|
|
|
|
|
|
|
if (byte == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!desc.empty())
|
|
|
|
{
|
|
|
|
str.PutCString(&desc.front());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Address *
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::GetPrintForDebuggerAddr()
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
|
|
|
if (!m_PrintForDebugger_addr.get())
|
|
|
|
{
|
|
|
|
ModuleList &modules = m_process->GetTarget().GetImages();
|
|
|
|
|
|
|
|
SymbolContextList contexts;
|
|
|
|
SymbolContext context;
|
|
|
|
|
|
|
|
if((!modules.FindSymbolsWithNameAndType(ConstString ("_NSPrintForDebugger"), eSymbolTypeCode, contexts)) &&
|
|
|
|
(!modules.FindSymbolsWithNameAndType(ConstString ("_CFPrintForDebugger"), eSymbolTypeCode, contexts)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
contexts.GetContextAtIndex(0, context);
|
|
|
|
|
|
|
|
m_PrintForDebugger_addr.reset(new Address(context.symbol->GetValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_PrintForDebugger_addr.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
lldb::ValueObjectSP
|
2011-04-01 07:01:21 +08:00
|
|
|
AppleObjCRuntime::GetDynamicValue (lldb::ValueObjectSP in_value)
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
|
|
|
lldb::ValueObjectSP ret_sp;
|
|
|
|
return ret_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::AppleIsModuleObjCLibrary (const ModuleSP &module_sp)
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
|
|
|
const FileSpec &module_file_spec = module_sp->GetFileSpec();
|
|
|
|
static ConstString ObjCName ("libobjc.A.dylib");
|
|
|
|
|
|
|
|
if (module_file_spec)
|
|
|
|
{
|
|
|
|
if (module_file_spec.GetFilename() == ObjCName)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::IsModuleObjCLibrary (const ModuleSP &module_sp)
|
|
|
|
{
|
|
|
|
return AppleIsModuleObjCLibrary(module_sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AppleObjCRuntime::ReadObjCLibrary (const ModuleSP &module_sp)
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
|
|
|
// Maybe check here and if we have a handler already, and the UUID of this module is the same as the one in the
|
|
|
|
// current module, then we don't have to reread it?
|
|
|
|
m_objc_trampoline_handler_ap.reset(new AppleObjCTrampolineHandler (m_process->GetSP(), module_sp));
|
|
|
|
if (m_objc_trampoline_handler_ap.get() != NULL)
|
|
|
|
{
|
|
|
|
m_read_objc_library = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPlanSP
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
|
2010-09-28 09:25:32 +08:00
|
|
|
{
|
|
|
|
ThreadPlanSP thread_plan_sp;
|
2010-09-29 00:06:31 +08:00
|
|
|
if (m_objc_trampoline_handler_ap.get())
|
|
|
|
thread_plan_sp = m_objc_trampoline_handler_ap->GetStepThroughDispatchPlan (thread, stop_others);
|
2010-09-28 09:25:32 +08:00
|
|
|
return thread_plan_sp;
|
|
|
|
}
|
|
|
|
|
2010-09-23 10:01:19 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Static Functions
|
|
|
|
//------------------------------------------------------------------
|
2011-03-25 05:19:54 +08:00
|
|
|
enum ObjCRuntimeVersions
|
2010-11-05 08:57:06 +08:00
|
|
|
AppleObjCRuntime::GetObjCVersion (Process *process, ModuleSP &objc_module_sp)
|
2010-09-23 10:01:19 +08:00
|
|
|
{
|
2010-11-05 02:30:59 +08:00
|
|
|
ModuleList &images = process->GetTarget().GetImages();
|
|
|
|
size_t num_images = images.GetSize();
|
|
|
|
for (size_t i = 0; i < num_images; i++)
|
|
|
|
{
|
|
|
|
ModuleSP module_sp = images.GetModuleAtIndex(i);
|
|
|
|
if (AppleIsModuleObjCLibrary (module_sp))
|
|
|
|
{
|
2010-11-05 08:57:06 +08:00
|
|
|
objc_module_sp = module_sp;
|
2010-11-05 02:30:59 +08:00
|
|
|
ObjectFile *ofile = module_sp->GetObjectFile();
|
|
|
|
if (!ofile)
|
2011-03-25 05:19:54 +08:00
|
|
|
return eObjC_VersionUnknown;
|
2010-11-05 02:30:59 +08:00
|
|
|
|
|
|
|
SectionList *sections = ofile->GetSectionList();
|
|
|
|
if (!sections)
|
2011-03-25 05:19:54 +08:00
|
|
|
return eObjC_VersionUnknown;
|
2010-11-05 02:30:59 +08:00
|
|
|
SectionSP v1_telltale_section_sp = sections->FindSectionByName(ConstString ("__OBJC"));
|
|
|
|
if (v1_telltale_section_sp)
|
|
|
|
{
|
2011-03-25 05:19:54 +08:00
|
|
|
return eAppleObjC_V1;
|
2010-11-05 02:30:59 +08:00
|
|
|
}
|
2011-03-25 05:19:54 +08:00
|
|
|
return eAppleObjC_V2;
|
2010-11-05 02:30:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 05:19:54 +08:00
|
|
|
return eObjC_VersionUnknown;
|
2010-09-23 10:01:19 +08:00
|
|
|
}
|
|
|
|
|
2010-11-04 06:19:38 +08:00
|
|
|
void
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::ClearExceptionBreakpoints ()
|
2010-11-04 06:19:38 +08:00
|
|
|
{
|
|
|
|
if (!m_process)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_objc_exception_bp_sp.get())
|
|
|
|
{
|
|
|
|
m_process->GetTarget().RemoveBreakpointByID(m_objc_exception_bp_sp->GetID());
|
|
|
|
m_objc_exception_bp_sp.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-11-05 02:30:59 +08:00
|
|
|
AppleObjCRuntime::ExceptionBreakpointsExplainStop (lldb::StopInfoSP stop_reason)
|
2010-11-04 06:19:38 +08:00
|
|
|
{
|
|
|
|
if (!m_process)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!stop_reason ||
|
|
|
|
stop_reason->GetStopReason() != eStopReasonBreakpoint)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t break_site_id = stop_reason->GetValue();
|
|
|
|
lldb::BreakpointSiteSP bp_site_sp = m_process->GetBreakpointSiteList().FindByID(break_site_id);
|
|
|
|
|
|
|
|
if (!bp_site_sp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
|
|
|
|
|
|
|
|
bool check_objc_exception = false;
|
|
|
|
break_id_t objc_exception_bid;
|
|
|
|
|
|
|
|
if (m_objc_exception_bp_sp)
|
|
|
|
{
|
|
|
|
check_objc_exception = true;
|
|
|
|
objc_exception_bid = m_objc_exception_bp_sp->GetID();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < num_owners; i++)
|
|
|
|
{
|
|
|
|
break_id_t bid = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().GetID();
|
|
|
|
|
|
|
|
if ((check_objc_exception && (bid == objc_exception_bid)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|