AppleObjCRuntimeV1.cpp revision b344843f75ef893762c93fd0a22d2d45712ce74d
1//===-- AppleObjCRuntimeV1.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 "AppleObjCRuntimeV1.h"
11#include "AppleObjCTrampolineHandler.h"
12
13#include "llvm/Support/MachO.h"
14#include "clang/AST/Type.h"
15
16#include "lldb/Breakpoint/BreakpointLocation.h"
17#include "lldb/Core/ConstString.h"
18#include "lldb/Core/Error.h"
19#include "lldb/Core/Log.h"
20#include "lldb/Core/Module.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/Scalar.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Expression/ClangFunction.h"
25#include "lldb/Expression/ClangUtilityFunction.h"
26#include "lldb/Symbol/ClangASTContext.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/RegisterContext.h"
30#include "lldb/Target/StopInfo.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
33
34#include <vector>
35
36using namespace lldb;
37using namespace lldb_private;
38
39static const char *pluginName = "AppleObjCRuntimeV1";
40static const char *pluginDesc = "Apple Objective C Language Runtime - Version 1";
41static const char *pluginShort = "language.apple.objc.v1";
42
43lldb::ValueObjectSP
44AppleObjCRuntimeV1::GetDynamicValue (lldb::ValueObjectSP in_value, ExecutionContextScope *exe_scope)
45{
46    lldb::ValueObjectSP ret_sp;
47    return ret_sp;
48}
49
50//------------------------------------------------------------------
51// Static Functions
52//------------------------------------------------------------------
53lldb_private::LanguageRuntime *
54AppleObjCRuntimeV1::CreateInstance (Process *process, lldb::LanguageType language)
55{
56    // FIXME: This should be a MacOS or iOS process, and we need to look for the OBJC section to make
57    // sure we aren't using the V1 runtime.
58    if (language == eLanguageTypeObjC)
59    {
60        ModuleSP objc_module_sp;
61
62        if (AppleObjCRuntime::GetObjCVersion (process, objc_module_sp) == eAppleObjC_V1)
63            return new AppleObjCRuntimeV1 (process);
64        else
65            return NULL;
66    }
67    else
68        return NULL;
69}
70
71void
72AppleObjCRuntimeV1::Initialize()
73{
74    PluginManager::RegisterPlugin (pluginName,
75                                   pluginDesc,
76                                   CreateInstance);
77}
78
79void
80AppleObjCRuntimeV1::Terminate()
81{
82    PluginManager::UnregisterPlugin (CreateInstance);
83}
84
85//------------------------------------------------------------------
86// PluginInterface protocol
87//------------------------------------------------------------------
88const char *
89AppleObjCRuntimeV1::GetPluginName()
90{
91    return pluginName;
92}
93
94const char *
95AppleObjCRuntimeV1::GetShortPluginName()
96{
97    return pluginShort;
98}
99
100uint32_t
101AppleObjCRuntimeV1::GetPluginVersion()
102{
103    return 1;
104}
105
106void
107AppleObjCRuntimeV1::SetExceptionBreakpoints ()
108{
109    if (!m_process)
110        return;
111
112    if (!m_objc_exception_bp_sp)
113    {
114        m_objc_exception_bp_sp = m_process->GetTarget().CreateBreakpoint (NULL,
115                                                                          "objc_exception_throw",
116                                                                          eFunctionNameTypeBase,
117                                                                          true);
118    }
119}
120
121struct BufStruct {
122    char contents[2048];
123};
124
125ClangUtilityFunction *
126AppleObjCRuntimeV1::CreateObjectChecker(const char *name)
127{
128    std::auto_ptr<BufStruct> buf(new BufStruct);
129
130    assert(snprintf(&buf->contents[0], sizeof(buf->contents),
131                    "struct __objc_class                                                    \n"
132                    "{                                                                      \n"
133                    "   struct __objc_class *isa;                                           \n"
134                    "   struct __objc_class *super_class;                                   \n"
135                    "   const char *name;                                                   \n"
136                    "   // rest of struct elided because unused                             \n"
137                    "};                                                                     \n"
138                    "                                                                       \n"
139                    "struct __objc_object                                                   \n"
140                    "{                                                                      \n"
141                    "   struct __objc_class *isa;                                           \n"
142                    "};                                                                     \n"
143                    "                                                                       \n"
144                    "extern \"C\" void                                                      \n"
145                    "%s(void *$__lldb_arg_obj)                                              \n"
146                    "{                                                                      \n"
147                    "   struct __objc_object *obj = (struct __objc_object*)$__lldb_arg_obj; \n"
148                    "   strlen(obj->isa->name);                                             \n"
149                    "}                                                                      \n",
150                    name) < sizeof(buf->contents));
151
152    return new ClangUtilityFunction(buf->contents, name);
153}
154