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
155ec532a9SColin Riley // Other libraries and framework includes
165ec532a9SColin Riley // Project includes
175ec532a9SColin Riley #include "lldb/lldb-private.h"
185ec532a9SColin Riley #include "lldb/Target/LanguageRuntime.h"
195ec532a9SColin Riley #include "lldb/Target/CPPLanguageRuntime.h"
205ec532a9SColin Riley #include "lldb/Core/Module.h"
215ec532a9SColin Riley 
225ec532a9SColin Riley namespace lldb_private
235ec532a9SColin Riley {
245ec532a9SColin Riley 
2598156583SEwan Crawford namespace lldb_renderscript
2698156583SEwan Crawford {
2798156583SEwan Crawford 
285ec532a9SColin Riley typedef uint32_t RSSlot;
295ec532a9SColin Riley class RSModuleDescriptor;
304640cde1SColin Riley struct RSGlobalDescriptor;
314640cde1SColin Riley struct RSKernelDescriptor;
324640cde1SColin Riley 
334640cde1SColin Riley typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP;
344640cde1SColin Riley typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP;
354640cde1SColin Riley typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP;
364640cde1SColin Riley 
3798156583SEwan Crawford // Breakpoint Resolvers decide where a breakpoint is placed,
3898156583SEwan Crawford // so having our own allows us to limit the search scope to RS kernel modules.
3998156583SEwan Crawford // As well as check for .expand kernels as a fallback.
4098156583SEwan Crawford class RSBreakpointResolver : public BreakpointResolver
4198156583SEwan Crawford {
4298156583SEwan Crawford   public:
434640cde1SColin Riley 
4498156583SEwan Crawford     RSBreakpointResolver(Breakpoint *bkpt, ConstString name):
4598156583SEwan Crawford                          BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
4698156583SEwan Crawford                          m_kernel_name(name)
4798156583SEwan Crawford     {
4898156583SEwan Crawford     }
4998156583SEwan Crawford 
5098156583SEwan Crawford     void
5198156583SEwan Crawford     GetDescription(Stream *strm) override
5298156583SEwan Crawford     {
5398156583SEwan Crawford         if (strm)
5498156583SEwan Crawford             strm->Printf("RenderScript kernel breakpoint for '%s'", m_kernel_name.AsCString());
5598156583SEwan Crawford     }
5698156583SEwan Crawford 
5798156583SEwan Crawford     void
5898156583SEwan Crawford     Dump(Stream *s) const override
5998156583SEwan Crawford     {
6098156583SEwan Crawford     }
6198156583SEwan Crawford 
6298156583SEwan Crawford     Searcher::CallbackReturn
6398156583SEwan Crawford     SearchCallback(SearchFilter &filter,
6498156583SEwan Crawford                    SymbolContext &context,
6598156583SEwan Crawford                    Address *addr,
6698156583SEwan Crawford                    bool containing) override;
6798156583SEwan Crawford 
6898156583SEwan Crawford     Searcher::Depth
6998156583SEwan Crawford     GetDepth() override
7098156583SEwan Crawford     {
7198156583SEwan Crawford         return Searcher::eDepthModule;
7298156583SEwan Crawford     }
7398156583SEwan Crawford 
7498156583SEwan Crawford     lldb::BreakpointResolverSP
7598156583SEwan Crawford     CopyForBreakpoint(Breakpoint &breakpoint) override
7698156583SEwan Crawford     {
7798156583SEwan Crawford         lldb::BreakpointResolverSP ret_sp(new RSBreakpointResolver(&breakpoint, m_kernel_name));
7898156583SEwan Crawford         return ret_sp;
7998156583SEwan Crawford     }
8098156583SEwan Crawford 
8198156583SEwan Crawford   protected:
8298156583SEwan Crawford     ConstString m_kernel_name;
8398156583SEwan Crawford };
845ec532a9SColin Riley 
855ec532a9SColin Riley struct RSKernelDescriptor
865ec532a9SColin Riley {
875ec532a9SColin Riley   public:
884640cde1SColin Riley     RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, uint32_t slot)
895ec532a9SColin Riley         : m_module(module)
905ec532a9SColin Riley         , m_name(name)
915ec532a9SColin Riley         , m_slot(slot)
925ec532a9SColin Riley     {
935ec532a9SColin Riley     }
945ec532a9SColin Riley 
955ec532a9SColin Riley     void Dump(Stream &strm) const;
965ec532a9SColin Riley 
974640cde1SColin Riley     const RSModuleDescriptor *m_module;
985ec532a9SColin Riley     ConstString m_name;
995ec532a9SColin Riley     RSSlot m_slot;
1005ec532a9SColin Riley };
1015ec532a9SColin Riley 
1025ec532a9SColin Riley struct RSGlobalDescriptor
1035ec532a9SColin Riley {
1045ec532a9SColin Riley   public:
1054640cde1SColin Riley     RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name )
1065ec532a9SColin Riley         : m_module(module)
1075ec532a9SColin Riley         , m_name(name)
1085ec532a9SColin Riley     {
1095ec532a9SColin Riley     }
1105ec532a9SColin Riley 
1115ec532a9SColin Riley     void Dump(Stream &strm) const;
1125ec532a9SColin Riley 
1134640cde1SColin Riley     const RSModuleDescriptor *m_module;
1145ec532a9SColin Riley     ConstString m_name;
1155ec532a9SColin Riley };
1165ec532a9SColin Riley 
1175ec532a9SColin Riley class RSModuleDescriptor
1185ec532a9SColin Riley {
1195ec532a9SColin Riley   public:
1205ec532a9SColin Riley     RSModuleDescriptor(const lldb::ModuleSP &module)
1215ec532a9SColin Riley         : m_module(module)
1225ec532a9SColin Riley     {
1235ec532a9SColin Riley     }
1245ec532a9SColin Riley 
1255ec532a9SColin Riley     ~RSModuleDescriptor() {}
1265ec532a9SColin Riley 
1275ec532a9SColin Riley     bool ParseRSInfo();
1285ec532a9SColin Riley 
1295ec532a9SColin Riley     void Dump(Stream &strm) const;
1305ec532a9SColin Riley 
1315ec532a9SColin Riley     const lldb::ModuleSP m_module;
1325ec532a9SColin Riley     std::vector<RSKernelDescriptor> m_kernels;
1335ec532a9SColin Riley     std::vector<RSGlobalDescriptor> m_globals;
1344640cde1SColin Riley     std::map<std::string, std::string> m_pragmas;
1354640cde1SColin Riley     std::string m_resname;
1365ec532a9SColin Riley };
1375ec532a9SColin Riley 
13898156583SEwan Crawford } // end lldb_renderscript namespace
13998156583SEwan Crawford 
1405ec532a9SColin Riley class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime
1415ec532a9SColin Riley {
1425ec532a9SColin Riley   public:
143ef20b08fSColin Riley 
144ef20b08fSColin Riley     enum ModuleKind
145ef20b08fSColin Riley     {
146ef20b08fSColin Riley         eModuleKindIgnored,
147ef20b08fSColin Riley         eModuleKindLibRS,
148ef20b08fSColin Riley         eModuleKindDriver,
149ef20b08fSColin Riley         eModuleKindImpl,
150ef20b08fSColin Riley         eModuleKindKernelObj
151ef20b08fSColin Riley     };
152ef20b08fSColin Riley 
15378f339d1SEwan Crawford     ~RenderScriptRuntime();
1545ec532a9SColin Riley 
1555ec532a9SColin Riley     //------------------------------------------------------------------
1565ec532a9SColin Riley     // Static Functions
1575ec532a9SColin Riley     //------------------------------------------------------------------
1585ec532a9SColin Riley     static void Initialize();
1595ec532a9SColin Riley 
1605ec532a9SColin Riley     static void Terminate();
1615ec532a9SColin Riley 
1625ec532a9SColin Riley     static lldb_private::LanguageRuntime *CreateInstance(Process *process, lldb::LanguageType language);
1635ec532a9SColin Riley 
1644640cde1SColin Riley     static lldb::CommandObjectSP GetCommandObject(CommandInterpreter& interpreter);
1654640cde1SColin Riley 
1665ec532a9SColin Riley     static lldb_private::ConstString GetPluginNameStatic();
1675ec532a9SColin Riley 
168ef20b08fSColin Riley     static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp);
169ef20b08fSColin Riley 
170ef20b08fSColin Riley     static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp);
171ef20b08fSColin Riley 
172ef20b08fSColin Riley     static void ModulesDidLoad(const lldb::ProcessSP& process_sp, const ModuleList &module_list );
173ef20b08fSColin Riley 
1745ec532a9SColin Riley     //------------------------------------------------------------------
1755ec532a9SColin Riley     // PluginInterface protocol
1765ec532a9SColin Riley     //------------------------------------------------------------------
1775ec532a9SColin Riley     virtual lldb_private::ConstString GetPluginName();
1785ec532a9SColin Riley 
1795ec532a9SColin Riley     virtual uint32_t GetPluginVersion();
1805ec532a9SColin Riley 
1815ec532a9SColin Riley     virtual bool IsVTableName(const char *name);
1825ec532a9SColin Riley 
1835ec532a9SColin Riley     virtual bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic,
1840b6003f3SEnrico Granata                                           TypeAndOrName &class_type_or_name, Address &address,
1850b6003f3SEnrico Granata                                           Value::ValueType &value_type);
186*c74275bcSEnrico Granata 
187*c74275bcSEnrico Granata     virtual TypeAndOrName
188*c74275bcSEnrico Granata     FixUpDynamicType(const TypeAndOrName& type_and_or_name,
189*c74275bcSEnrico Granata                      const CompilerType& static_type);
1905ec532a9SColin Riley 
1915ec532a9SColin Riley     virtual bool CouldHaveDynamicValue(ValueObject &in_value);
1925ec532a9SColin Riley 
1935ec532a9SColin Riley     virtual lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp);
1945ec532a9SColin Riley 
1955ec532a9SColin Riley     bool LoadModule(const lldb::ModuleSP &module_sp);
1965ec532a9SColin Riley 
1975ec532a9SColin Riley     bool ProbeModules(const ModuleList module_list);
1985ec532a9SColin Riley 
1995ec532a9SColin Riley     void DumpModules(Stream &strm) const;
2005ec532a9SColin Riley 
2014640cde1SColin Riley     void DumpContexts(Stream &strm) const;
2024640cde1SColin Riley 
2034640cde1SColin Riley     void DumpKernels(Stream &strm) const;
2044640cde1SColin Riley 
20598156583SEwan Crawford     void AttemptBreakpointAtKernelName(Stream &strm, const char *name, Error &error, lldb::TargetSP target);
2064640cde1SColin Riley 
2077dc7771cSEwan Crawford     void SetBreakAllKernels(bool do_break, lldb::TargetSP target);
2087dc7771cSEwan Crawford 
2094640cde1SColin Riley     void Status(Stream &strm) const;
2104640cde1SColin Riley 
21192935952SSiva Chandra     virtual size_t GetAlternateManglings(const ConstString &mangled, std::vector<ConstString> &alternates) {
21292935952SSiva Chandra         return static_cast<size_t>(0);
21392935952SSiva Chandra     }
21492935952SSiva Chandra 
215ef20b08fSColin Riley     virtual void ModulesDidLoad(const ModuleList &module_list );
216ef20b08fSColin Riley 
217ef20b08fSColin Riley     void Update();
218ef20b08fSColin Riley 
219ef20b08fSColin Riley     void Initiate();
220ef20b08fSColin Riley 
2215ec532a9SColin Riley   protected:
2224640cde1SColin Riley 
2237dc7771cSEwan Crawford     void InitSearchFilter(lldb::TargetSP target)
2247dc7771cSEwan Crawford     {
2257dc7771cSEwan Crawford         if (!m_filtersp)
2267dc7771cSEwan Crawford             m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
2277dc7771cSEwan Crawford     }
2287dc7771cSEwan Crawford 
22998156583SEwan Crawford     void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
2304640cde1SColin Riley 
2314640cde1SColin Riley     void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
2324640cde1SColin Riley 
2337dc7771cSEwan Crawford     lldb::BreakpointSP CreateKernelBreakpoint(const ConstString& name);
2347dc7771cSEwan Crawford 
2357dc7771cSEwan Crawford     void BreakOnModuleKernels(const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
2367dc7771cSEwan Crawford 
2374640cde1SColin Riley     struct RuntimeHook;
2384640cde1SColin Riley     typedef void (RenderScriptRuntime::*CaptureStateFn)(RuntimeHook* hook_info, ExecutionContext &context);  // Please do this!
2394640cde1SColin Riley 
2404640cde1SColin Riley     struct HookDefn
2414640cde1SColin Riley     {
2424640cde1SColin Riley         const char * name;
24382780287SAidan Dodds         const char * symbol_name_m32; // mangled name for the 32 bit architectures
24482780287SAidan Dodds         const char * symbol_name_m64; // mangled name for the 64 bit archs
2454640cde1SColin Riley         uint32_t version;
2464640cde1SColin Riley         ModuleKind kind;
2474640cde1SColin Riley         CaptureStateFn grabber;
2484640cde1SColin Riley     };
2494640cde1SColin Riley 
2504640cde1SColin Riley     struct RuntimeHook
2514640cde1SColin Riley     {
2524640cde1SColin Riley         lldb::addr_t address;
2534640cde1SColin Riley         const HookDefn  *defn;
2544640cde1SColin Riley         lldb::BreakpointSP bp_sp;
2554640cde1SColin Riley     };
2564640cde1SColin Riley 
2574640cde1SColin Riley     typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
2584640cde1SColin Riley 
25978f339d1SEwan Crawford     struct ScriptDetails;
26078f339d1SEwan Crawford     struct AllocationDetails;
2614640cde1SColin Riley 
2624640cde1SColin Riley     lldb::ModuleSP m_libRS;
2634640cde1SColin Riley     lldb::ModuleSP m_libRSDriver;
2644640cde1SColin Riley     lldb::ModuleSP m_libRSCpuRef;
26598156583SEwan Crawford     std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
26678f339d1SEwan Crawford 
26778f339d1SEwan Crawford     std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
26878f339d1SEwan Crawford     std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
2694640cde1SColin Riley 
27098156583SEwan Crawford     std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> m_scriptMappings;
2714640cde1SColin Riley     std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
2724640cde1SColin Riley 
2737dc7771cSEwan Crawford     lldb::SearchFilterSP m_filtersp; // Needed to create breakpoints through Target API
2747dc7771cSEwan Crawford 
275ef20b08fSColin Riley     bool m_initiated;
2764640cde1SColin Riley     bool m_debuggerPresentFlagged;
2777dc7771cSEwan Crawford     bool m_breakAllKernels;
2784640cde1SColin Riley     static const HookDefn s_runtimeHookDefns[];
2794640cde1SColin Riley     static const size_t s_runtimeHookCount;
2804640cde1SColin Riley 
2815ec532a9SColin Riley   private:
2825ec532a9SColin Riley     RenderScriptRuntime(Process *process); // Call CreateInstance instead.
2834640cde1SColin Riley 
2844640cde1SColin Riley     static bool HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
2854640cde1SColin Riley                              lldb::user_id_t break_loc_id);
2864640cde1SColin Riley 
2874640cde1SColin Riley     void HookCallback(RuntimeHook* hook_info, ExecutionContext& context);
2884640cde1SColin Riley 
28982780287SAidan Dodds     bool GetArgSimple(ExecutionContext& context, uint32_t arg, uint64_t* data);
2904640cde1SColin Riley 
2914640cde1SColin Riley     void CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext& context);
2924640cde1SColin Riley     void CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionContext& context);
2934640cde1SColin Riley     void CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionContext& context);
2944640cde1SColin Riley 
29578f339d1SEwan Crawford     // Search for a script detail object using a target address.
29678f339d1SEwan Crawford     // If a script does not currently exist this function will return nullptr.
29778f339d1SEwan Crawford     // If 'create' is true and there is no previous script with this address,
29878f339d1SEwan Crawford     // then a new Script detail object will be created for this address and returned.
29978f339d1SEwan Crawford     ScriptDetails* LookUpScript(lldb::addr_t address, bool create);
30078f339d1SEwan Crawford 
30178f339d1SEwan Crawford     // Search for a previously saved allocation detail object using a target address.
30278f339d1SEwan Crawford     // If an allocation does not exist for this address then nullptr will be returned.
30378f339d1SEwan Crawford     // If 'create' is true and there is no previous allocation then a new allocation
30478f339d1SEwan Crawford     // detail object will be created for this address and returned.
30578f339d1SEwan Crawford     AllocationDetails* LookUpAllocation(lldb::addr_t address, bool create);
3065ec532a9SColin Riley };
3075ec532a9SColin Riley 
3085ec532a9SColin Riley } // namespace lldb_private
3095ec532a9SColin Riley 
3105ec532a9SColin Riley #endif // liblldb_RenderScriptRuntime_h_
311