15ec532a9SColin Riley //===-- RenderScriptRuntime.h -----------------------------------*- C++ -*-===//
25ec532a9SColin Riley //
35ec532a9SColin Riley //                     The LLVM Compiler Infrastructure
45ec532a9SColin Riley //
55ec532a9SColin Riley // This file is distributed under the University of Illinois Open Source
65ec532a9SColin Riley // License. See LICENSE.TXT for details.
75ec532a9SColin Riley //
85ec532a9SColin Riley //===----------------------------------------------------------------------===//
95ec532a9SColin Riley 
105ec532a9SColin Riley #ifndef liblldb_RenderScriptRuntime_h_
115ec532a9SColin Riley #define liblldb_RenderScriptRuntime_h_
125ec532a9SColin Riley 
135ec532a9SColin Riley // C Includes
145ec532a9SColin Riley // C++ Includes
15222b937cSEugene Zelenko #include <array>
16222b937cSEugene Zelenko #include <map>
17222b937cSEugene Zelenko #include <memory>
18222b937cSEugene Zelenko #include <string>
19222b937cSEugene Zelenko #include <vector>
20222b937cSEugene Zelenko 
215ec532a9SColin Riley // Other libraries and framework includes
227f193d69SLuke Drummond #include "llvm/ADT/SmallVector.h"
237f193d69SLuke Drummond #include "llvm/ADT/StringRef.h"
245ec532a9SColin Riley // Project includes
255ec532a9SColin Riley #include "lldb/Core/Module.h"
2619459580SLuke Drummond #include "lldb/Expression/LLVMUserExpression.h"
27b3f7f69dSAidan Dodds #include "lldb/Target/CPPLanguageRuntime.h"
28b3f7f69dSAidan Dodds #include "lldb/Target/LanguageRuntime.h"
29b3f7f69dSAidan Dodds #include "lldb/lldb-private.h"
305ec532a9SColin Riley 
31b9c1b51eSKate Stone namespace lldb_private {
32b9c1b51eSKate Stone namespace lldb_renderscript {
3398156583SEwan Crawford 
345ec532a9SColin Riley typedef uint32_t RSSlot;
355ec532a9SColin Riley class RSModuleDescriptor;
364640cde1SColin Riley struct RSGlobalDescriptor;
374640cde1SColin Riley struct RSKernelDescriptor;
387f193d69SLuke Drummond struct RSReductionDescriptor;
394640cde1SColin Riley 
404640cde1SColin Riley typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP;
414640cde1SColin Riley typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP;
424640cde1SColin Riley typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP;
4300f56eebSLuke Drummond struct RSCoordinate {
4400f56eebSLuke Drummond   uint32_t x, y, z;
4500f56eebSLuke Drummond 
4600f56eebSLuke Drummond   RSCoordinate() : x(), y(), z(){};
4700f56eebSLuke Drummond 
4800f56eebSLuke Drummond   bool operator==(const lldb_renderscript::RSCoordinate &rhs) {
4900f56eebSLuke Drummond     return x == rhs.x && y == rhs.y && z == rhs.z;
5000f56eebSLuke Drummond   }
5100f56eebSLuke Drummond };
524640cde1SColin Riley 
5380af0b9eSLuke Drummond // Breakpoint Resolvers decide where a breakpoint is placed, so having our own
5480af0b9eSLuke Drummond // allows us to limit the search scope to RS kernel modules. As well as check
5580af0b9eSLuke Drummond // for .expand kernels as a fallback.
56b9c1b51eSKate Stone class RSBreakpointResolver : public BreakpointResolver {
5798156583SEwan Crawford public:
5880af0b9eSLuke Drummond   RSBreakpointResolver(Breakpoint *bp, ConstString name)
5980af0b9eSLuke Drummond       : BreakpointResolver(bp, BreakpointResolver::NameResolver),
60b9c1b51eSKate Stone         m_kernel_name(name) {}
6198156583SEwan Crawford 
62b9c1b51eSKate Stone   void GetDescription(Stream *strm) override {
6398156583SEwan Crawford     if (strm)
64b9c1b51eSKate Stone       strm->Printf("RenderScript kernel breakpoint for '%s'",
65b9c1b51eSKate Stone                    m_kernel_name.AsCString());
6698156583SEwan Crawford   }
6798156583SEwan Crawford 
68b9c1b51eSKate Stone   void Dump(Stream *s) const override {}
6998156583SEwan Crawford 
70b9c1b51eSKate Stone   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
71b9c1b51eSKate Stone                                           SymbolContext &context, Address *addr,
72b9c1b51eSKate Stone                                           bool containing) override;
7398156583SEwan Crawford 
74b9c1b51eSKate Stone   Searcher::Depth GetDepth() override { return Searcher::eDepthModule; }
7598156583SEwan Crawford 
7698156583SEwan Crawford   lldb::BreakpointResolverSP
77b9c1b51eSKate Stone   CopyForBreakpoint(Breakpoint &breakpoint) override {
78b9c1b51eSKate Stone     lldb::BreakpointResolverSP ret_sp(
79b9c1b51eSKate Stone         new RSBreakpointResolver(&breakpoint, m_kernel_name));
8098156583SEwan Crawford     return ret_sp;
8198156583SEwan Crawford   }
8298156583SEwan Crawford 
8398156583SEwan Crawford protected:
8498156583SEwan Crawford   ConstString m_kernel_name;
8598156583SEwan Crawford };
865ec532a9SColin Riley 
87*b3bbcb12SLuke Drummond class RSReduceBreakpointResolver : public BreakpointResolver {
88*b3bbcb12SLuke Drummond public:
89*b3bbcb12SLuke Drummond   enum ReduceKernelTypeFlags {
90*b3bbcb12SLuke Drummond     eKernelTypeAll = ~(0),
91*b3bbcb12SLuke Drummond     eKernelTypeNone = 0,
92*b3bbcb12SLuke Drummond     eKernelTypeAccum = (1 << 0),
93*b3bbcb12SLuke Drummond     eKernelTypeInit = (1 << 1),
94*b3bbcb12SLuke Drummond     eKernelTypeComb = (1 << 2),
95*b3bbcb12SLuke Drummond     eKernelTypeOutC = (1 << 3),
96*b3bbcb12SLuke Drummond     eKernelTypeHalter = (1 << 4)
97*b3bbcb12SLuke Drummond   };
98*b3bbcb12SLuke Drummond 
99*b3bbcb12SLuke Drummond   RSReduceBreakpointResolver(
100*b3bbcb12SLuke Drummond       Breakpoint *breakpoint, ConstString reduce_name,
101*b3bbcb12SLuke Drummond       std::vector<lldb_renderscript::RSModuleDescriptorSP> *rs_modules,
102*b3bbcb12SLuke Drummond       int kernel_types = eKernelTypeAll)
103*b3bbcb12SLuke Drummond       : BreakpointResolver(breakpoint, BreakpointResolver::NameResolver),
104*b3bbcb12SLuke Drummond         m_reduce_name(reduce_name), m_rsmodules(rs_modules),
105*b3bbcb12SLuke Drummond         m_kernel_types(kernel_types) {
106*b3bbcb12SLuke Drummond     // The reduce breakpoint resolver handles adding breakpoints for named
107*b3bbcb12SLuke Drummond     // reductions.
108*b3bbcb12SLuke Drummond     // Breakpoints will be resolved for all constituent kernels in the named
109*b3bbcb12SLuke Drummond     // reduction
110*b3bbcb12SLuke Drummond   }
111*b3bbcb12SLuke Drummond 
112*b3bbcb12SLuke Drummond   void GetDescription(Stream *strm) override {
113*b3bbcb12SLuke Drummond     if (strm)
114*b3bbcb12SLuke Drummond       strm->Printf("RenderScript reduce breakpoint for '%s'",
115*b3bbcb12SLuke Drummond                    m_reduce_name.AsCString());
116*b3bbcb12SLuke Drummond   }
117*b3bbcb12SLuke Drummond 
118*b3bbcb12SLuke Drummond   void Dump(Stream *s) const override {}
119*b3bbcb12SLuke Drummond 
120*b3bbcb12SLuke Drummond   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
121*b3bbcb12SLuke Drummond                                           SymbolContext &context, Address *addr,
122*b3bbcb12SLuke Drummond                                           bool containing) override;
123*b3bbcb12SLuke Drummond 
124*b3bbcb12SLuke Drummond   Searcher::Depth GetDepth() override { return Searcher::eDepthModule; }
125*b3bbcb12SLuke Drummond 
126*b3bbcb12SLuke Drummond   lldb::BreakpointResolverSP
127*b3bbcb12SLuke Drummond   CopyForBreakpoint(Breakpoint &breakpoint) override {
128*b3bbcb12SLuke Drummond     lldb::BreakpointResolverSP ret_sp(new RSReduceBreakpointResolver(
129*b3bbcb12SLuke Drummond         &breakpoint, m_reduce_name, m_rsmodules, m_kernel_types));
130*b3bbcb12SLuke Drummond     return ret_sp;
131*b3bbcb12SLuke Drummond   }
132*b3bbcb12SLuke Drummond 
133*b3bbcb12SLuke Drummond private:
134*b3bbcb12SLuke Drummond   ConstString m_reduce_name; // The name of the reduction
135*b3bbcb12SLuke Drummond   std::vector<lldb_renderscript::RSModuleDescriptorSP> *m_rsmodules;
136*b3bbcb12SLuke Drummond   int m_kernel_types;
137*b3bbcb12SLuke Drummond };
138*b3bbcb12SLuke Drummond 
139b9c1b51eSKate Stone struct RSKernelDescriptor {
1405ec532a9SColin Riley public:
1417f193d69SLuke Drummond   RSKernelDescriptor(const RSModuleDescriptor *module, llvm::StringRef name,
142b9c1b51eSKate Stone                      uint32_t slot)
143b9c1b51eSKate Stone       : m_module(module), m_name(name), m_slot(slot) {}
1445ec532a9SColin Riley 
145b9c1b51eSKate Stone   void Dump(Stream &strm) const;
1465ec532a9SColin Riley 
1474640cde1SColin Riley   const RSModuleDescriptor *m_module;
1485ec532a9SColin Riley   ConstString m_name;
1495ec532a9SColin Riley   RSSlot m_slot;
1505ec532a9SColin Riley };
1515ec532a9SColin Riley 
152b9c1b51eSKate Stone struct RSGlobalDescriptor {
1535ec532a9SColin Riley public:
1547f193d69SLuke Drummond   RSGlobalDescriptor(const RSModuleDescriptor *module, llvm::StringRef name)
155b9c1b51eSKate Stone       : m_module(module), m_name(name) {}
1565ec532a9SColin Riley 
157b9c1b51eSKate Stone   void Dump(Stream &strm) const;
1585ec532a9SColin Riley 
1594640cde1SColin Riley   const RSModuleDescriptor *m_module;
1605ec532a9SColin Riley   ConstString m_name;
1615ec532a9SColin Riley };
1625ec532a9SColin Riley 
1637f193d69SLuke Drummond struct RSReductionDescriptor {
1647f193d69SLuke Drummond   RSReductionDescriptor(const RSModuleDescriptor *module, uint32_t sig,
1657f193d69SLuke Drummond                         uint32_t accum_data_size, llvm::StringRef name,
1667f193d69SLuke Drummond                         llvm::StringRef init_name, llvm::StringRef accum_name,
1677f193d69SLuke Drummond                         llvm::StringRef comb_name, llvm::StringRef outc_name,
1687f193d69SLuke Drummond                         llvm::StringRef halter_name = ".")
1697f193d69SLuke Drummond       : m_module(module), m_reduce_name(name), m_init_name(init_name),
1707f193d69SLuke Drummond         m_accum_name(accum_name), m_comb_name(comb_name),
1717f193d69SLuke Drummond         m_outc_name(outc_name), m_halter_name(halter_name) {
1727f193d69SLuke Drummond     // TODO Check whether the combiner is an autogenerated name, and track
1737f193d69SLuke Drummond     // this
1747f193d69SLuke Drummond   }
1757f193d69SLuke Drummond 
1767f193d69SLuke Drummond   void Dump(Stream &strm) const;
1777f193d69SLuke Drummond 
1787f193d69SLuke Drummond   const RSModuleDescriptor *m_module;
1797f193d69SLuke Drummond   ConstString m_reduce_name; // This is the name given to the general reduction
1807f193d69SLuke Drummond                              // as a group as passed to pragma
1817f193d69SLuke Drummond   // reduce(m_reduce_name). There is no kernel function with this name
1827f193d69SLuke Drummond   ConstString m_init_name;  // The name of the initializer name. "." if no
1837f193d69SLuke Drummond                             // initializer given
1847f193d69SLuke Drummond   ConstString m_accum_name; // The accumulator function name. "." if not given
1857f193d69SLuke Drummond   ConstString m_comb_name; // The name of the combiner function. If this was not
1867f193d69SLuke Drummond                            // given, a name is generated by the
1877f193d69SLuke Drummond                            // compiler. TODO
1887f193d69SLuke Drummond   ConstString m_outc_name; // The name of the outconverter
1897f193d69SLuke Drummond 
1907f193d69SLuke Drummond   ConstString m_halter_name; // The name of the halter function. XXX This is not
1917f193d69SLuke Drummond                              // yet specified by the RenderScript
1927f193d69SLuke Drummond   // compiler or runtime, and its semantics and existence is still under
1937f193d69SLuke Drummond   // discussion by the
1947f193d69SLuke Drummond   // RenderScript Contributors
1957f193d69SLuke Drummond   RSSlot m_accum_sig; // metatdata signature for this reduction (bitwise mask of
1967f193d69SLuke Drummond                       // type information (see
1977f193d69SLuke Drummond                       // libbcc/include/bcinfo/MetadataExtractor.h
1987f193d69SLuke Drummond   uint32_t m_accum_data_size; // Data size of the accumulator function input
1997f193d69SLuke Drummond   bool m_comb_name_generated; // Was the combiner name generated by the compiler
2007f193d69SLuke Drummond };
2017f193d69SLuke Drummond 
202b9c1b51eSKate Stone class RSModuleDescriptor {
2037f193d69SLuke Drummond   bool ParseExportForeachCount(llvm::StringRef *, size_t n_lines);
2047f193d69SLuke Drummond 
2057f193d69SLuke Drummond   bool ParseExportVarCount(llvm::StringRef *, size_t n_lines);
2067f193d69SLuke Drummond 
2077f193d69SLuke Drummond   bool ParseExportReduceCount(llvm::StringRef *, size_t n_lines);
2087f193d69SLuke Drummond 
2097f193d69SLuke Drummond   bool ParseBuildChecksum(llvm::StringRef *, size_t n_lines);
2107f193d69SLuke Drummond 
2117f193d69SLuke Drummond   bool ParsePragmaCount(llvm::StringRef *, size_t n_lines);
2127f193d69SLuke Drummond 
2135ec532a9SColin Riley public:
214b3f7f69dSAidan Dodds   RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {}
2155ec532a9SColin Riley 
216222b937cSEugene Zelenko   ~RSModuleDescriptor() = default;
2175ec532a9SColin Riley 
218b9c1b51eSKate Stone   bool ParseRSInfo();
2195ec532a9SColin Riley 
220b9c1b51eSKate Stone   void Dump(Stream &strm) const;
2215ec532a9SColin Riley 
2225ec532a9SColin Riley   const lldb::ModuleSP m_module;
2235ec532a9SColin Riley   std::vector<RSKernelDescriptor> m_kernels;
2245ec532a9SColin Riley   std::vector<RSGlobalDescriptor> m_globals;
2257f193d69SLuke Drummond   std::vector<RSReductionDescriptor> m_reductions;
2264640cde1SColin Riley   std::map<std::string, std::string> m_pragmas;
2274640cde1SColin Riley   std::string m_resname;
2285ec532a9SColin Riley };
2295ec532a9SColin Riley 
230222b937cSEugene Zelenko } // namespace lldb_renderscript
23198156583SEwan Crawford 
232b9c1b51eSKate Stone class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime {
2335ec532a9SColin Riley public:
234b9c1b51eSKate Stone   enum ModuleKind {
235ef20b08fSColin Riley     eModuleKindIgnored,
236ef20b08fSColin Riley     eModuleKindLibRS,
237ef20b08fSColin Riley     eModuleKindDriver,
238ef20b08fSColin Riley     eModuleKindImpl,
239ef20b08fSColin Riley     eModuleKindKernelObj
240ef20b08fSColin Riley   };
241ef20b08fSColin Riley 
242222b937cSEugene Zelenko   ~RenderScriptRuntime() override;
2435ec532a9SColin Riley 
2445ec532a9SColin Riley   //------------------------------------------------------------------
2455ec532a9SColin Riley   // Static Functions
2465ec532a9SColin Riley   //------------------------------------------------------------------
247b9c1b51eSKate Stone   static void Initialize();
2485ec532a9SColin Riley 
249b9c1b51eSKate Stone   static void Terminate();
2505ec532a9SColin Riley 
251b3f7f69dSAidan Dodds   static lldb_private::LanguageRuntime *
252b3f7f69dSAidan Dodds   CreateInstance(Process *process, lldb::LanguageType language);
2535ec532a9SColin Riley 
254b3f7f69dSAidan Dodds   static lldb::CommandObjectSP
255b3f7f69dSAidan Dodds   GetCommandObject(CommandInterpreter &interpreter);
2564640cde1SColin Riley 
257b9c1b51eSKate Stone   static lldb_private::ConstString GetPluginNameStatic();
2585ec532a9SColin Riley 
259b9c1b51eSKate Stone   static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp);
260ef20b08fSColin Riley 
261b9c1b51eSKate Stone   static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp);
262ef20b08fSColin Riley 
263b9c1b51eSKate Stone   static void ModulesDidLoad(const lldb::ProcessSP &process_sp,
264b9c1b51eSKate Stone                              const ModuleList &module_list);
265ef20b08fSColin Riley 
266b9c1b51eSKate Stone   bool IsVTableName(const char *name) override;
2675ec532a9SColin Riley 
268b9c1b51eSKate Stone   bool GetDynamicTypeAndAddress(ValueObject &in_value,
269b9c1b51eSKate Stone                                 lldb::DynamicValueType use_dynamic,
270b9c1b51eSKate Stone                                 TypeAndOrName &class_type_or_name,
271b9c1b51eSKate Stone                                 Address &address,
2725f57b6eeSEnrico Granata                                 Value::ValueType &value_type) override;
273c74275bcSEnrico Granata 
274b9c1b51eSKate Stone   TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name,
275b9c1b51eSKate Stone                                  ValueObject &static_value) override;
2765ec532a9SColin Riley 
277b9c1b51eSKate Stone   bool CouldHaveDynamicValue(ValueObject &in_value) override;
2785ec532a9SColin Riley 
27980af0b9eSLuke Drummond   lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bp,
280b9c1b51eSKate Stone                                                      bool catch_bp,
281b9c1b51eSKate Stone                                                      bool throw_bp) override;
2825ec532a9SColin Riley 
283b9c1b51eSKate Stone   bool LoadModule(const lldb::ModuleSP &module_sp);
2845ec532a9SColin Riley 
285b9c1b51eSKate Stone   void DumpModules(Stream &strm) const;
2865ec532a9SColin Riley 
287b9c1b51eSKate Stone   void DumpContexts(Stream &strm) const;
2884640cde1SColin Riley 
289b9c1b51eSKate Stone   void DumpKernels(Stream &strm) const;
2904640cde1SColin Riley 
291b9c1b51eSKate Stone   bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id);
292a0f08674SEwan Crawford 
293b9c1b51eSKate Stone   void ListAllocations(Stream &strm, StackFrame *frame_ptr,
294b9c1b51eSKate Stone                        const uint32_t index);
29515f2bd95SEwan Crawford 
296b9c1b51eSKate Stone   bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr);
2970d2bfcfbSEwan Crawford 
29800f56eebSLuke Drummond   bool PlaceBreakpointOnKernel(
29900f56eebSLuke Drummond       lldb::TargetSP target, Stream &messages, const char *name,
30000f56eebSLuke Drummond       const lldb_renderscript::RSCoordinate *coords = nullptr);
3014640cde1SColin Riley 
302*b3bbcb12SLuke Drummond   bool PlaceBreakpointOnReduction(
303*b3bbcb12SLuke Drummond       lldb::TargetSP target, Stream &messages, const char *reduce_name,
304*b3bbcb12SLuke Drummond       const lldb_renderscript::RSCoordinate *coords = nullptr,
305*b3bbcb12SLuke Drummond       int kernel_types = ~(0));
306*b3bbcb12SLuke Drummond 
307b9c1b51eSKate Stone   void SetBreakAllKernels(bool do_break, lldb::TargetSP target);
3087dc7771cSEwan Crawford 
309b9c1b51eSKate Stone   void Status(Stream &strm) const;
3104640cde1SColin Riley 
311b9c1b51eSKate Stone   void ModulesDidLoad(const ModuleList &module_list) override;
312ef20b08fSColin Riley 
313b9c1b51eSKate Stone   bool LoadAllocation(Stream &strm, const uint32_t alloc_id,
314b9c1b51eSKate Stone                       const char *filename, StackFrame *frame_ptr);
31555232f09SEwan Crawford 
316b9c1b51eSKate Stone   bool SaveAllocation(Stream &strm, const uint32_t alloc_id,
317b9c1b51eSKate Stone                       const char *filename, StackFrame *frame_ptr);
31855232f09SEwan Crawford 
319b9c1b51eSKate Stone   void Update();
320ef20b08fSColin Riley 
321b9c1b51eSKate Stone   void Initiate();
322ef20b08fSColin Riley 
323222b937cSEugene Zelenko   //------------------------------------------------------------------
324222b937cSEugene Zelenko   // PluginInterface protocol
325222b937cSEugene Zelenko   //------------------------------------------------------------------
326b9c1b51eSKate Stone   lldb_private::ConstString GetPluginName() override;
3274640cde1SColin Riley 
328b9c1b51eSKate Stone   uint32_t GetPluginVersion() override;
329222b937cSEugene Zelenko 
330b9c1b51eSKate Stone   static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord,
331b9c1b51eSKate Stone                                   Thread *thread_ptr);
3324f8817c2SEwan Crawford 
333222b937cSEugene Zelenko protected:
33415f2bd95SEwan Crawford   struct ScriptDetails;
33515f2bd95SEwan Crawford   struct AllocationDetails;
3368b244e21SEwan Crawford   struct Element;
33715f2bd95SEwan Crawford 
338b9c1b51eSKate Stone   void InitSearchFilter(lldb::TargetSP target) {
3397dc7771cSEwan Crawford     if (!m_filtersp)
3407dc7771cSEwan Crawford       m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
3417dc7771cSEwan Crawford   }
3427dc7771cSEwan Crawford 
343b9c1b51eSKate Stone   void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
3444640cde1SColin Riley 
345b9c1b51eSKate Stone   void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
3464640cde1SColin Riley 
34780af0b9eSLuke Drummond   bool RefreshAllocation(AllocationDetails *alloc, StackFrame *frame_ptr);
34815f2bd95SEwan Crawford 
349b9c1b51eSKate Stone   bool EvalRSExpression(const char *expression, StackFrame *frame_ptr,
350b9c1b51eSKate Stone                         uint64_t *result);
35115f2bd95SEwan Crawford 
352b9c1b51eSKate Stone   lldb::BreakpointSP CreateKernelBreakpoint(const ConstString &name);
3537dc7771cSEwan Crawford 
354*b3bbcb12SLuke Drummond   lldb::BreakpointSP CreateReductionBreakpoint(const ConstString &name,
355*b3bbcb12SLuke Drummond                                                int kernel_types);
356*b3bbcb12SLuke Drummond 
357b9c1b51eSKate Stone   void BreakOnModuleKernels(
358b9c1b51eSKate Stone       const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
3597dc7771cSEwan Crawford 
3604640cde1SColin Riley   struct RuntimeHook;
361b9c1b51eSKate Stone   typedef void (RenderScriptRuntime::*CaptureStateFn)(
362b9c1b51eSKate Stone       RuntimeHook *hook_info,
363b3f7f69dSAidan Dodds       ExecutionContext &context); // Please do this!
3644640cde1SColin Riley 
365b9c1b51eSKate Stone   struct HookDefn {
3664640cde1SColin Riley     const char *name;
36782780287SAidan Dodds     const char *symbol_name_m32; // mangled name for the 32 bit architectures
36882780287SAidan Dodds     const char *symbol_name_m64; // mangled name for the 64 bit archs
3694640cde1SColin Riley     uint32_t version;
3704640cde1SColin Riley     ModuleKind kind;
3714640cde1SColin Riley     CaptureStateFn grabber;
3724640cde1SColin Riley   };
3734640cde1SColin Riley 
374b9c1b51eSKate Stone   struct RuntimeHook {
3754640cde1SColin Riley     lldb::addr_t address;
3764640cde1SColin Riley     const HookDefn *defn;
3774640cde1SColin Riley     lldb::BreakpointSP bp_sp;
3784640cde1SColin Riley   };
3794640cde1SColin Riley 
3804640cde1SColin Riley   typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
3814640cde1SColin Riley 
3824640cde1SColin Riley   lldb::ModuleSP m_libRS;
3834640cde1SColin Riley   lldb::ModuleSP m_libRSDriver;
3844640cde1SColin Riley   lldb::ModuleSP m_libRSCpuRef;
38598156583SEwan Crawford   std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
38678f339d1SEwan Crawford 
38778f339d1SEwan Crawford   std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
38878f339d1SEwan Crawford   std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
3894640cde1SColin Riley 
390b9c1b51eSKate Stone   std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP>
391b9c1b51eSKate Stone       m_scriptMappings;
3924640cde1SColin Riley   std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
39300f56eebSLuke Drummond   std::map<lldb::user_id_t, std::unique_ptr<lldb_renderscript::RSCoordinate>>
39400f56eebSLuke Drummond       m_conditional_breaks;
3954640cde1SColin Riley 
396b9c1b51eSKate Stone   lldb::SearchFilterSP
397b9c1b51eSKate Stone       m_filtersp; // Needed to create breakpoints through Target API
3987dc7771cSEwan Crawford 
399ef20b08fSColin Riley   bool m_initiated;
4004640cde1SColin Riley   bool m_debuggerPresentFlagged;
4017dc7771cSEwan Crawford   bool m_breakAllKernels;
4024640cde1SColin Riley   static const HookDefn s_runtimeHookDefns[];
4034640cde1SColin Riley   static const size_t s_runtimeHookCount;
40419459580SLuke Drummond   LLVMUserExpression::IRPasses *m_ir_passes;
4054640cde1SColin Riley 
4065ec532a9SColin Riley private:
4075ec532a9SColin Riley   RenderScriptRuntime(Process *process); // Call CreateInstance instead.
4084640cde1SColin Riley 
409b9c1b51eSKate Stone   static bool HookCallback(void *baton, StoppointCallbackContext *ctx,
410b9c1b51eSKate Stone                            lldb::user_id_t break_id,
4114640cde1SColin Riley                            lldb::user_id_t break_loc_id);
4124640cde1SColin Riley 
413b9c1b51eSKate Stone   static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx,
414b9c1b51eSKate Stone                                   lldb::user_id_t break_id,
415b9c1b51eSKate Stone                                   lldb::user_id_t break_loc_id);
416018f5a7eSEwan Crawford 
417b9c1b51eSKate Stone   void HookCallback(RuntimeHook *hook_info, ExecutionContext &context);
4184640cde1SColin Riley 
419b9c1b51eSKate Stone   void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context);
4204640cde1SColin Riley 
421b9c1b51eSKate Stone   void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context);
422a0f08674SEwan Crawford 
423b9c1b51eSKate Stone   void CaptureAllocationDestroy(RuntimeHook *hook_info,
424b9c1b51eSKate Stone                                 ExecutionContext &context);
425b3f7f69dSAidan Dodds 
426b9c1b51eSKate Stone   void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context);
427b3f7f69dSAidan Dodds 
428b9c1b51eSKate Stone   void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info,
429b9c1b51eSKate Stone                                        ExecutionContext &context);
430b3f7f69dSAidan Dodds 
431b9c1b51eSKate Stone   AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id);
432b3f7f69dSAidan Dodds 
43380af0b9eSLuke Drummond   std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *alloc,
434b9c1b51eSKate Stone                                              StackFrame *frame_ptr);
435b3f7f69dSAidan Dodds 
436b9c1b51eSKate Stone   void SetElementSize(Element &elem);
437b3f7f69dSAidan Dodds 
438b9c1b51eSKate Stone   static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP,
439b9c1b51eSKate Stone                                     const char *var_name, uint64_t &val);
440b3f7f69dSAidan Dodds 
441b9c1b51eSKate Stone   void FindStructTypeName(Element &elem, StackFrame *frame_ptr);
442b3f7f69dSAidan Dodds 
443b9c1b51eSKate Stone   size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer,
444b9c1b51eSKate Stone                                 size_t offset, const Element &elem);
445b9c1b51eSKate Stone 
446b9c1b51eSKate Stone   size_t CalculateElementHeaderSize(const Element &elem);
44726e52a70SEwan Crawford 
44800f56eebSLuke Drummond   void SetConditional(lldb::BreakpointSP bp, lldb_private::Stream &messages,
44900f56eebSLuke Drummond                       const lldb_renderscript::RSCoordinate &coord);
45015f2bd95SEwan Crawford   //
45115f2bd95SEwan Crawford   // Helper functions for jitting the runtime
45215f2bd95SEwan Crawford   //
45315f2bd95SEwan Crawford 
45480af0b9eSLuke Drummond   bool JITDataPointer(AllocationDetails *alloc, StackFrame *frame_ptr,
455b3f7f69dSAidan Dodds                       uint32_t x = 0, uint32_t y = 0, uint32_t z = 0);
45615f2bd95SEwan Crawford 
45780af0b9eSLuke Drummond   bool JITTypePointer(AllocationDetails *alloc, StackFrame *frame_ptr);
45815f2bd95SEwan Crawford 
45980af0b9eSLuke Drummond   bool JITTypePacked(AllocationDetails *alloc, StackFrame *frame_ptr);
46015f2bd95SEwan Crawford 
461b9c1b51eSKate Stone   bool JITElementPacked(Element &elem, const lldb::addr_t context,
462b9c1b51eSKate Stone                         StackFrame *frame_ptr);
4638b244e21SEwan Crawford 
46480af0b9eSLuke Drummond   bool JITAllocationSize(AllocationDetails *alloc, StackFrame *frame_ptr);
465a0f08674SEwan Crawford 
466b9c1b51eSKate Stone   bool JITSubelements(Element &elem, const lldb::addr_t context,
467b9c1b51eSKate Stone                       StackFrame *frame_ptr);
468b3f7f69dSAidan Dodds 
46980af0b9eSLuke Drummond   bool JITAllocationStride(AllocationDetails *alloc, StackFrame *frame_ptr);
470a0f08674SEwan Crawford 
47178f339d1SEwan Crawford   // Search for a script detail object using a target address.
47278f339d1SEwan Crawford   // If a script does not currently exist this function will return nullptr.
47378f339d1SEwan Crawford   // If 'create' is true and there is no previous script with this address,
474b9c1b51eSKate Stone   // then a new Script detail object will be created for this address and
475b9c1b51eSKate Stone   // returned.
476b9c1b51eSKate Stone   ScriptDetails *LookUpScript(lldb::addr_t address, bool create);
47778f339d1SEwan Crawford 
478b9c1b51eSKate Stone   // Search for a previously saved allocation detail object using a target
479b9c1b51eSKate Stone   // address.
480b9c1b51eSKate Stone   // If an allocation does not exist for this address then nullptr will be
481b9c1b51eSKate Stone   // returned.
482b9c1b51eSKate Stone   AllocationDetails *LookUpAllocation(lldb::addr_t address);
4835d057637SLuke Drummond 
484b9c1b51eSKate Stone   // Creates a new allocation with the specified address assigning a new ID and
485b9c1b51eSKate Stone   // removes
4865d057637SLuke Drummond   // any previous stored allocation which has the same address.
487b9c1b51eSKate Stone   AllocationDetails *CreateAllocation(lldb::addr_t address);
48819459580SLuke Drummond 
489b9c1b51eSKate Stone   bool GetOverrideExprOptions(clang::TargetOptions &prototype) override;
49019459580SLuke Drummond 
491b9c1b51eSKate Stone   bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override;
4925ec532a9SColin Riley };
4935ec532a9SColin Riley 
4945ec532a9SColin Riley } // namespace lldb_private
4955ec532a9SColin Riley 
4965ec532a9SColin Riley #endif // liblldb_RenderScriptRuntime_h_
497