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