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