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, lldb::DynamicValueType use_dynamic,
182                              TypeAndOrName &class_type_or_name, Address &address,
183                              Value::ValueType &value_type) override;
184 
185     TypeAndOrName
186     FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value) override;
187 
188     bool
189     CouldHaveDynamicValue(ValueObject &in_value) override;
190 
191     lldb::BreakpointResolverSP
192     CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) override;
193 
194     bool
195     LoadModule(const lldb::ModuleSP &module_sp);
196 
197     void
198     DumpModules(Stream &strm) const;
199 
200     void
201     DumpContexts(Stream &strm) const;
202 
203     void
204     DumpKernels(Stream &strm) const;
205 
206     bool
207     DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id);
208 
209     void
210     ListAllocations(Stream &strm, StackFrame *frame_ptr, const uint32_t index);
211 
212     bool
213     RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr);
214 
215     void
216     PlaceBreakpointOnKernel(Stream &strm, const char *name, const std::array<int, 3> coords, Error &error,
217                             lldb::TargetSP target);
218 
219     void
220     SetBreakAllKernels(bool do_break, lldb::TargetSP target);
221 
222     void
223     Status(Stream &strm) const;
224 
225     void
226     ModulesDidLoad(const ModuleList &module_list) override;
227 
228     bool
229     LoadAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr);
230 
231     bool
232     SaveAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr);
233 
234     void
235     Update();
236 
237     void
238     Initiate();
239 
240     //------------------------------------------------------------------
241     // PluginInterface protocol
242     //------------------------------------------------------------------
243     lldb_private::ConstString
244     GetPluginName() override;
245 
246     uint32_t
247     GetPluginVersion() override;
248 
249     static bool
250     GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, Thread *thread_ptr);
251 
252 protected:
253     struct ScriptDetails;
254     struct AllocationDetails;
255     struct Element;
256 
257     void
258     InitSearchFilter(lldb::TargetSP target)
259     {
260         if (!m_filtersp)
261             m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
262     }
263 
264     void
265     FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
266 
267     void
268     LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
269 
270     bool
271     RefreshAllocation(AllocationDetails *allocation, StackFrame *frame_ptr);
272 
273     bool
274     EvalRSExpression(const char *expression, StackFrame *frame_ptr, uint64_t *result);
275 
276     lldb::BreakpointSP
277     CreateKernelBreakpoint(const ConstString &name);
278 
279     void
280     BreakOnModuleKernels(const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
281 
282     struct RuntimeHook;
283     typedef void (RenderScriptRuntime::*CaptureStateFn)(RuntimeHook *hook_info,
284                                                         ExecutionContext &context); // Please do this!
285 
286     struct HookDefn
287     {
288         const char *name;
289         const char *symbol_name_m32; // mangled name for the 32 bit architectures
290         const char *symbol_name_m64; // mangled name for the 64 bit archs
291         uint32_t version;
292         ModuleKind kind;
293         CaptureStateFn grabber;
294     };
295 
296     struct RuntimeHook
297     {
298         lldb::addr_t address;
299         const HookDefn *defn;
300         lldb::BreakpointSP bp_sp;
301     };
302 
303     typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
304 
305     lldb::ModuleSP m_libRS;
306     lldb::ModuleSP m_libRSDriver;
307     lldb::ModuleSP m_libRSCpuRef;
308     std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
309 
310     std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
311     std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
312 
313     std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> m_scriptMappings;
314     std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
315     std::map<lldb::user_id_t, std::shared_ptr<uint32_t>> m_conditional_breaks;
316 
317     lldb::SearchFilterSP m_filtersp; // Needed to create breakpoints through Target API
318 
319     bool m_initiated;
320     bool m_debuggerPresentFlagged;
321     bool m_breakAllKernels;
322     static const HookDefn s_runtimeHookDefns[];
323     static const size_t s_runtimeHookCount;
324     static const std::string s_runtimeExpandSuffix;
325     static const std::array<const char *, 3> s_runtimeCoordVars;
326 
327 private:
328     RenderScriptRuntime(Process *process); // Call CreateInstance instead.
329 
330     static bool
331     HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
332 
333     static bool
334     KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
335                         lldb::user_id_t break_loc_id);
336 
337     void
338     HookCallback(RuntimeHook *hook_info, ExecutionContext &context);
339 
340     void
341     CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context);
342 
343     void
344     CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context);
345 
346     void
347     CaptureAllocationDestroy(RuntimeHook *hook_info, ExecutionContext &context);
348 
349     void
350     CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context);
351 
352     void
353     CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, ExecutionContext &context);
354 
355     AllocationDetails *
356     FindAllocByID(Stream &strm, const uint32_t alloc_id);
357 
358     std::shared_ptr<uint8_t>
359     GetAllocationData(AllocationDetails *allocation, StackFrame *frame_ptr);
360 
361     void
362     SetElementSize(Element &elem);
363 
364     static bool
365     GetFrameVarAsUnsigned(const lldb::StackFrameSP, const char *var_name, uint64_t &val);
366 
367     void
368     FindStructTypeName(Element &elem, StackFrame *frame_ptr);
369 
370     size_t
371     PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, size_t offset, const Element &elem);
372 
373     size_t
374     CalculateElementHeaderSize(const Element &elem);
375 
376     //
377     // Helper functions for jitting the runtime
378     //
379 
380     bool
381     JITDataPointer(AllocationDetails *allocation, StackFrame *frame_ptr,
382                    uint32_t x = 0, uint32_t y = 0, uint32_t z = 0);
383 
384     bool
385     JITTypePointer(AllocationDetails *allocation, StackFrame *frame_ptr);
386 
387     bool
388     JITTypePacked(AllocationDetails *allocation, StackFrame *frame_ptr);
389 
390     bool
391     JITElementPacked(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr);
392 
393     bool
394     JITAllocationSize(AllocationDetails *allocation, StackFrame *frame_ptr);
395 
396     bool
397     JITSubelements(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr);
398 
399     bool
400     JITAllocationStride(AllocationDetails *allocation, StackFrame *frame_ptr);
401 
402     // Search for a script detail object using a target address.
403     // If a script does not currently exist this function will return nullptr.
404     // If 'create' is true and there is no previous script with this address,
405     // then a new Script detail object will be created for this address and returned.
406     ScriptDetails *
407     LookUpScript(lldb::addr_t address, bool create);
408 
409     // Search for a previously saved allocation detail object using a target address.
410     // If an allocation does not exist for this address then nullptr will be returned.
411     // If 'create' is true and there is no previous allocation then a new allocation
412     // detail object will be created for this address and returned.
413     AllocationDetails *
414     LookUpAllocation(lldb::addr_t address, bool create);
415 };
416 
417 } // namespace lldb_private
418 
419 #endif // liblldb_RenderScriptRuntime_h_
420