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 namespace lldb_renderscript
26 {
27 
28 typedef uint32_t RSSlot;
29 class RSModuleDescriptor;
30 struct RSGlobalDescriptor;
31 struct RSKernelDescriptor;
32 
33 typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP;
34 typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP;
35 typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP;
36 
37 // Breakpoint Resolvers decide where a breakpoint is placed,
38 // so having our own allows us to limit the search scope to RS kernel modules.
39 // As well as check for .expand kernels as a fallback.
40 class RSBreakpointResolver : public BreakpointResolver
41 {
42   public:
43 
44     RSBreakpointResolver(Breakpoint *bkpt, ConstString name):
45                          BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
46                          m_kernel_name(name)
47     {
48     }
49 
50     void
51     GetDescription(Stream *strm) override
52     {
53         if (strm)
54             strm->Printf("RenderScript kernel breakpoint for '%s'", m_kernel_name.AsCString());
55     }
56 
57     void
58     Dump(Stream *s) const override
59     {
60     }
61 
62     Searcher::CallbackReturn
63     SearchCallback(SearchFilter &filter,
64                    SymbolContext &context,
65                    Address *addr,
66                    bool containing) override;
67 
68     Searcher::Depth
69     GetDepth() override
70     {
71         return Searcher::eDepthModule;
72     }
73 
74     lldb::BreakpointResolverSP
75     CopyForBreakpoint(Breakpoint &breakpoint) override
76     {
77         lldb::BreakpointResolverSP ret_sp(new RSBreakpointResolver(&breakpoint, m_kernel_name));
78         return ret_sp;
79     }
80 
81   protected:
82     ConstString m_kernel_name;
83 };
84 
85 struct RSKernelDescriptor
86 {
87   public:
88     RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, uint32_t slot)
89         : m_module(module)
90         , m_name(name)
91         , m_slot(slot)
92     {
93     }
94 
95     void Dump(Stream &strm) const;
96 
97     const RSModuleDescriptor *m_module;
98     ConstString m_name;
99     RSSlot m_slot;
100 };
101 
102 struct RSGlobalDescriptor
103 {
104   public:
105     RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name )
106         : m_module(module)
107         , m_name(name)
108     {
109     }
110 
111     void Dump(Stream &strm) const;
112 
113     const RSModuleDescriptor *m_module;
114     ConstString m_name;
115 };
116 
117 class RSModuleDescriptor
118 {
119   public:
120     RSModuleDescriptor(const lldb::ModuleSP &module)
121         : m_module(module)
122     {
123     }
124 
125     ~RSModuleDescriptor() {}
126 
127     bool ParseRSInfo();
128 
129     void Dump(Stream &strm) const;
130 
131     const lldb::ModuleSP m_module;
132     std::vector<RSKernelDescriptor> m_kernels;
133     std::vector<RSGlobalDescriptor> m_globals;
134     std::map<std::string, std::string> m_pragmas;
135     std::string m_resname;
136 };
137 
138 } // end lldb_renderscript namespace
139 
140 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime
141 {
142   public:
143 
144     enum ModuleKind
145     {
146         eModuleKindIgnored,
147         eModuleKindLibRS,
148         eModuleKindDriver,
149         eModuleKindImpl,
150         eModuleKindKernelObj
151     };
152 
153     ~RenderScriptRuntime();
154 
155     //------------------------------------------------------------------
156     // Static Functions
157     //------------------------------------------------------------------
158     static void Initialize();
159 
160     static void Terminate();
161 
162     static lldb_private::LanguageRuntime *CreateInstance(Process *process, lldb::LanguageType language);
163 
164     static lldb::CommandObjectSP GetCommandObject(CommandInterpreter& interpreter);
165 
166     static lldb_private::ConstString GetPluginNameStatic();
167 
168     static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp);
169 
170     static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp);
171 
172     static void ModulesDidLoad(const lldb::ProcessSP& process_sp, const ModuleList &module_list );
173 
174     //------------------------------------------------------------------
175     // PluginInterface protocol
176     //------------------------------------------------------------------
177     virtual lldb_private::ConstString GetPluginName();
178 
179     virtual uint32_t GetPluginVersion();
180 
181     virtual bool IsVTableName(const char *name);
182 
183     virtual bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic,
184                                           TypeAndOrName &class_type_or_name, Address &address,
185                                           Value::ValueType &value_type);
186 
187     virtual TypeAndOrName
188     FixUpDynamicType (const TypeAndOrName& type_and_or_name,
189                       ValueObject& static_value);
190 
191     virtual bool CouldHaveDynamicValue(ValueObject &in_value);
192 
193     virtual lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp);
194 
195     bool LoadModule(const lldb::ModuleSP &module_sp);
196 
197     bool ProbeModules(const ModuleList module_list);
198 
199     void DumpModules(Stream &strm) const;
200 
201     void DumpContexts(Stream &strm) const;
202 
203     void DumpKernels(Stream &strm) const;
204 
205     void ListAllocations(Stream &strm, StackFrame* frame_ptr, bool recompute);
206 
207     void AttemptBreakpointAtKernelName(Stream &strm, const char *name, Error &error, lldb::TargetSP target);
208 
209     void SetBreakAllKernels(bool do_break, lldb::TargetSP target);
210 
211     void Status(Stream &strm) const;
212 
213     virtual size_t GetAlternateManglings(const ConstString &mangled, std::vector<ConstString> &alternates) {
214         return static_cast<size_t>(0);
215     }
216 
217     virtual void ModulesDidLoad(const ModuleList &module_list );
218 
219     void Update();
220 
221     void Initiate();
222 
223   protected:
224 
225     struct ScriptDetails;
226     struct AllocationDetails;
227 
228     void InitSearchFilter(lldb::TargetSP target)
229     {
230         if (!m_filtersp)
231             m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
232     }
233 
234     void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
235 
236     void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
237 
238     bool RefreshAllocation(AllocationDetails* allocation, StackFrame* frame_ptr);
239 
240     bool EvalRSExpression(const char* expression, StackFrame* frame_ptr, uint64_t* result);
241 
242     lldb::BreakpointSP CreateKernelBreakpoint(const ConstString& name);
243 
244     void BreakOnModuleKernels(const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
245 
246     struct RuntimeHook;
247     typedef void (RenderScriptRuntime::*CaptureStateFn)(RuntimeHook* hook_info, ExecutionContext &context);  // Please do this!
248 
249     struct HookDefn
250     {
251         const char * name;
252         const char * symbol_name_m32; // mangled name for the 32 bit architectures
253         const char * symbol_name_m64; // mangled name for the 64 bit archs
254         uint32_t version;
255         ModuleKind kind;
256         CaptureStateFn grabber;
257     };
258 
259     struct RuntimeHook
260     {
261         lldb::addr_t address;
262         const HookDefn  *defn;
263         lldb::BreakpointSP bp_sp;
264     };
265 
266     typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
267 
268     lldb::ModuleSP m_libRS;
269     lldb::ModuleSP m_libRSDriver;
270     lldb::ModuleSP m_libRSCpuRef;
271     std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
272 
273     std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
274     std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
275 
276     std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> m_scriptMappings;
277     std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
278 
279     lldb::SearchFilterSP m_filtersp; // Needed to create breakpoints through Target API
280 
281     bool m_initiated;
282     bool m_debuggerPresentFlagged;
283     bool m_breakAllKernels;
284     static const HookDefn s_runtimeHookDefns[];
285     static const size_t s_runtimeHookCount;
286 
287   private:
288     RenderScriptRuntime(Process *process); // Call CreateInstance instead.
289 
290     static bool HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
291                              lldb::user_id_t break_loc_id);
292 
293     void HookCallback(RuntimeHook* hook_info, ExecutionContext& context);
294 
295     bool GetArgSimple(ExecutionContext& context, uint32_t arg, uint64_t* data);
296 
297     void CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext& context);
298     void CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionContext& context);
299     void CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionContext& context);
300 
301     //
302     // Helper functions for jitting the runtime
303     //
304     bool JITDataPointer(AllocationDetails* allocation, StackFrame* frame_ptr,
305                         unsigned int x = 0, unsigned int y = 0, unsigned int z = 0);
306 
307     bool JITTypePointer(AllocationDetails* allocation, StackFrame* frame_ptr);
308 
309     bool JITTypePacked(AllocationDetails* allocation, StackFrame* frame_ptr);
310 
311     bool JITElementPacked(AllocationDetails* allocation, StackFrame* frame_ptr);
312 
313     // Search for a script detail object using a target address.
314     // If a script does not currently exist this function will return nullptr.
315     // If 'create' is true and there is no previous script with this address,
316     // then a new Script detail object will be created for this address and returned.
317     ScriptDetails* LookUpScript(lldb::addr_t address, bool create);
318 
319     // Search for a previously saved allocation detail object using a target address.
320     // If an allocation does not exist for this address then nullptr will be returned.
321     // If 'create' is true and there is no previous allocation then a new allocation
322     // detail object will be created for this address and returned.
323     AllocationDetails* LookUpAllocation(lldb::addr_t address, bool create);
324 };
325 
326 } // namespace lldb_private
327 
328 #endif // liblldb_RenderScriptRuntime_h_
329