1 //===-- RenderScriptRuntime.h -----------------------------------*- 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 #ifndef liblldb_RenderScriptRuntime_h_
11 #define liblldb_RenderScriptRuntime_h_
12 
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project includes
17 #include "lldb/lldb-private.h"
18 #include "lldb/Target/LanguageRuntime.h"
19 #include "lldb/Target/CPPLanguageRuntime.h"
20 #include "lldb/Core/Module.h"
21 
22 namespace lldb_private
23 {
24 
25 typedef uint32_t RSSlot;
26 class RSModuleDescriptor;
27 struct RSGlobalDescriptor;
28 struct RSKernelDescriptor;
29 
30 typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP;
31 typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP;
32 typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP;
33 
34 
35 
36 struct RSKernelDescriptor
37 {
38   public:
39     RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, uint32_t slot)
40         : m_module(module)
41         , m_name(name)
42         , m_slot(slot)
43     {
44     }
45 
46     void Dump(Stream &strm) const;
47 
48     const RSModuleDescriptor *m_module;
49     ConstString m_name;
50     RSSlot m_slot;
51 };
52 
53 struct RSGlobalDescriptor
54 {
55   public:
56     RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name )
57         : m_module(module)
58         , m_name(name)
59     {
60     }
61 
62     void Dump(Stream &strm) const;
63 
64     const RSModuleDescriptor *m_module;
65     ConstString m_name;
66 };
67 
68 class RSModuleDescriptor
69 {
70   public:
71     RSModuleDescriptor(const lldb::ModuleSP &module)
72         : m_module(module)
73     {
74     }
75 
76     ~RSModuleDescriptor() {}
77 
78     bool ParseRSInfo();
79 
80     void Dump(Stream &strm) const;
81 
82     const lldb::ModuleSP m_module;
83     std::vector<RSKernelDescriptor> m_kernels;
84     std::vector<RSGlobalDescriptor> m_globals;
85     std::map<std::string, std::string> m_pragmas;
86     std::string m_resname;
87 };
88 
89 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime
90 {
91   public:
92 
93     enum ModuleKind
94     {
95         eModuleKindIgnored,
96         eModuleKindLibRS,
97         eModuleKindDriver,
98         eModuleKindImpl,
99         eModuleKindKernelObj
100     };
101 
102 
103     ~RenderScriptRuntime() {}
104 
105     //------------------------------------------------------------------
106     // Static Functions
107     //------------------------------------------------------------------
108     static void Initialize();
109 
110     static void Terminate();
111 
112     static lldb_private::LanguageRuntime *CreateInstance(Process *process, lldb::LanguageType language);
113 
114     static lldb::CommandObjectSP GetCommandObject(CommandInterpreter& interpreter);
115 
116     static lldb_private::ConstString GetPluginNameStatic();
117 
118     static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp);
119 
120     static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp);
121 
122     static void ModulesDidLoad(const lldb::ProcessSP& process_sp, const ModuleList &module_list );
123 
124     //------------------------------------------------------------------
125     // PluginInterface protocol
126     //------------------------------------------------------------------
127     virtual lldb_private::ConstString GetPluginName();
128 
129     virtual uint32_t GetPluginVersion();
130 
131     virtual bool IsVTableName(const char *name);
132 
133     virtual bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic,
134                                           TypeAndOrName &class_type_or_name, Address &address);
135 
136     virtual bool CouldHaveDynamicValue(ValueObject &in_value);
137 
138     virtual lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp);
139 
140     bool LoadModule(const lldb::ModuleSP &module_sp);
141 
142     bool ProbeModules(const ModuleList module_list);
143 
144     void DumpModules(Stream &strm) const;
145 
146     void DumpContexts(Stream &strm) const;
147 
148     void DumpKernels(Stream &strm) const;
149 
150     void AttemptBreakpointAtKernelName(Stream &strm, const char *name, Error &error);
151 
152     void Status(Stream &strm) const;
153 
154     virtual size_t GetAlternateManglings(const ConstString &mangled, std::vector<ConstString> &alternates) {
155         return static_cast<size_t>(0);
156     }
157 
158     virtual void ModulesDidLoad(const ModuleList &module_list );
159 
160     void Update();
161 
162     void Initiate();
163 
164   protected:
165 
166     void FixupScriptDetails(RSModuleDescriptorSP rsmodule_sp);
167 
168     void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
169 
170     struct RuntimeHook;
171     typedef void (RenderScriptRuntime::*CaptureStateFn)(RuntimeHook* hook_info, ExecutionContext &context);  // Please do this!
172 
173     struct HookDefn
174     {
175         const char * name;
176         const char * symbol_name;
177         uint32_t version;
178         ModuleKind kind;
179         CaptureStateFn grabber;
180     };
181 
182     struct RuntimeHook
183     {
184         lldb::addr_t address;
185         const HookDefn  *defn;
186         lldb::BreakpointSP bp_sp;
187     };
188 
189     typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
190 
191     struct ScriptDetails
192     {
193         std::string resname;
194         std::string scriptDyLib;
195         std::string cachedir;
196         lldb::addr_t context;
197         lldb::addr_t script;
198     };
199 
200     lldb::ModuleSP m_libRS;
201     lldb::ModuleSP m_libRSDriver;
202     lldb::ModuleSP m_libRSCpuRef;
203     std::vector<RSModuleDescriptorSP> m_rsmodules;
204     std::vector<ScriptDetails> m_scripts;
205 
206     std::map<lldb::addr_t, RSModuleDescriptorSP> m_scriptMappings;
207     std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
208 
209     bool m_initiated;
210     bool m_debuggerPresentFlagged;
211     static const HookDefn s_runtimeHookDefns[];
212     static const size_t s_runtimeHookCount;
213 
214   private:
215     RenderScriptRuntime(Process *process); // Call CreateInstance instead.
216 
217     static bool HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
218                              lldb::user_id_t break_loc_id);
219 
220     void HookCallback(RuntimeHook* hook_info, ExecutionContext& context);
221 
222     bool GetArg32Simple(ExecutionContext& context, uint32_t arg, uint32_t *data);
223 
224     void CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext& context);
225     void CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionContext& context);
226     void CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionContext& context);
227 
228 };
229 
230 } // namespace lldb_private
231 
232 #endif // liblldb_RenderScriptRuntime_h_
233