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 225ec532a9SColin Riley // Project includes 235ec532a9SColin Riley #include "lldb/Core/Module.h" 2419459580SLuke Drummond #include "lldb/Expression/LLVMUserExpression.h" 25b3f7f69dSAidan Dodds #include "lldb/Target/CPPLanguageRuntime.h" 26b3f7f69dSAidan Dodds #include "lldb/Target/LanguageRuntime.h" 27b3f7f69dSAidan Dodds #include "lldb/lldb-private.h" 285ec532a9SColin Riley 29*b9c1b51eSKate Stone namespace lldb_private { 30*b9c1b51eSKate Stone namespace lldb_renderscript { 3198156583SEwan Crawford 325ec532a9SColin Riley typedef uint32_t RSSlot; 335ec532a9SColin Riley class RSModuleDescriptor; 344640cde1SColin Riley struct RSGlobalDescriptor; 354640cde1SColin Riley struct RSKernelDescriptor; 364640cde1SColin Riley 374640cde1SColin Riley typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP; 384640cde1SColin Riley typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP; 394640cde1SColin Riley typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP; 404f8817c2SEwan Crawford typedef std::array<uint32_t, 3> RSCoordinate; 414640cde1SColin Riley 4298156583SEwan Crawford // Breakpoint Resolvers decide where a breakpoint is placed, 4398156583SEwan Crawford // so having our own allows us to limit the search scope to RS kernel modules. 4498156583SEwan Crawford // As well as check for .expand kernels as a fallback. 45*b9c1b51eSKate Stone class RSBreakpointResolver : public BreakpointResolver { 4698156583SEwan Crawford public: 47b3f7f69dSAidan Dodds RSBreakpointResolver(Breakpoint *bkpt, ConstString name) 48*b9c1b51eSKate Stone : BreakpointResolver(bkpt, BreakpointResolver::NameResolver), 49*b9c1b51eSKate Stone m_kernel_name(name) {} 5098156583SEwan Crawford 51*b9c1b51eSKate Stone void GetDescription(Stream *strm) override { 5298156583SEwan Crawford if (strm) 53*b9c1b51eSKate Stone strm->Printf("RenderScript kernel breakpoint for '%s'", 54*b9c1b51eSKate Stone m_kernel_name.AsCString()); 5598156583SEwan Crawford } 5698156583SEwan Crawford 57*b9c1b51eSKate Stone void Dump(Stream *s) const override {} 5898156583SEwan Crawford 59*b9c1b51eSKate Stone Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 60*b9c1b51eSKate Stone SymbolContext &context, Address *addr, 61*b9c1b51eSKate Stone bool containing) override; 6298156583SEwan Crawford 63*b9c1b51eSKate Stone Searcher::Depth GetDepth() override { return Searcher::eDepthModule; } 6498156583SEwan Crawford 6598156583SEwan Crawford lldb::BreakpointResolverSP 66*b9c1b51eSKate Stone CopyForBreakpoint(Breakpoint &breakpoint) override { 67*b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp( 68*b9c1b51eSKate Stone new RSBreakpointResolver(&breakpoint, m_kernel_name)); 6998156583SEwan Crawford return ret_sp; 7098156583SEwan Crawford } 7198156583SEwan Crawford 7298156583SEwan Crawford protected: 7398156583SEwan Crawford ConstString m_kernel_name; 7498156583SEwan Crawford }; 755ec532a9SColin Riley 76*b9c1b51eSKate Stone struct RSKernelDescriptor { 775ec532a9SColin Riley public: 78*b9c1b51eSKate Stone RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, 79*b9c1b51eSKate Stone uint32_t slot) 80*b9c1b51eSKate Stone : m_module(module), m_name(name), m_slot(slot) {} 815ec532a9SColin Riley 82*b9c1b51eSKate Stone void Dump(Stream &strm) const; 835ec532a9SColin Riley 844640cde1SColin Riley const RSModuleDescriptor *m_module; 855ec532a9SColin Riley ConstString m_name; 865ec532a9SColin Riley RSSlot m_slot; 875ec532a9SColin Riley }; 885ec532a9SColin Riley 89*b9c1b51eSKate Stone struct RSGlobalDescriptor { 905ec532a9SColin Riley public: 91*b9c1b51eSKate Stone RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name) 92*b9c1b51eSKate Stone : m_module(module), m_name(name) {} 935ec532a9SColin Riley 94*b9c1b51eSKate Stone void Dump(Stream &strm) const; 955ec532a9SColin Riley 964640cde1SColin Riley const RSModuleDescriptor *m_module; 975ec532a9SColin Riley ConstString m_name; 985ec532a9SColin Riley }; 995ec532a9SColin Riley 100*b9c1b51eSKate Stone class RSModuleDescriptor { 1015ec532a9SColin Riley public: 102b3f7f69dSAidan Dodds RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {} 1035ec532a9SColin Riley 104222b937cSEugene Zelenko ~RSModuleDescriptor() = default; 1055ec532a9SColin Riley 106*b9c1b51eSKate Stone bool ParseRSInfo(); 1075ec532a9SColin Riley 108*b9c1b51eSKate Stone void Dump(Stream &strm) const; 1095ec532a9SColin Riley 1105ec532a9SColin Riley const lldb::ModuleSP m_module; 1115ec532a9SColin Riley std::vector<RSKernelDescriptor> m_kernels; 1125ec532a9SColin Riley std::vector<RSGlobalDescriptor> m_globals; 1134640cde1SColin Riley std::map<std::string, std::string> m_pragmas; 1144640cde1SColin Riley std::string m_resname; 1155ec532a9SColin Riley }; 1165ec532a9SColin Riley 117222b937cSEugene Zelenko } // namespace lldb_renderscript 11898156583SEwan Crawford 119*b9c1b51eSKate Stone class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime { 1205ec532a9SColin Riley public: 121*b9c1b51eSKate Stone enum ModuleKind { 122ef20b08fSColin Riley eModuleKindIgnored, 123ef20b08fSColin Riley eModuleKindLibRS, 124ef20b08fSColin Riley eModuleKindDriver, 125ef20b08fSColin Riley eModuleKindImpl, 126ef20b08fSColin Riley eModuleKindKernelObj 127ef20b08fSColin Riley }; 128ef20b08fSColin Riley 129222b937cSEugene Zelenko ~RenderScriptRuntime() override; 1305ec532a9SColin Riley 1315ec532a9SColin Riley //------------------------------------------------------------------ 1325ec532a9SColin Riley // Static Functions 1335ec532a9SColin Riley //------------------------------------------------------------------ 134*b9c1b51eSKate Stone static void Initialize(); 1355ec532a9SColin Riley 136*b9c1b51eSKate Stone static void Terminate(); 1375ec532a9SColin Riley 138b3f7f69dSAidan Dodds static lldb_private::LanguageRuntime * 139b3f7f69dSAidan Dodds CreateInstance(Process *process, lldb::LanguageType language); 1405ec532a9SColin Riley 141b3f7f69dSAidan Dodds static lldb::CommandObjectSP 142b3f7f69dSAidan Dodds GetCommandObject(CommandInterpreter &interpreter); 1434640cde1SColin Riley 144*b9c1b51eSKate Stone static lldb_private::ConstString GetPluginNameStatic(); 1455ec532a9SColin Riley 146*b9c1b51eSKate Stone static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 147ef20b08fSColin Riley 148*b9c1b51eSKate Stone static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 149ef20b08fSColin Riley 150*b9c1b51eSKate Stone static void ModulesDidLoad(const lldb::ProcessSP &process_sp, 151*b9c1b51eSKate Stone const ModuleList &module_list); 152ef20b08fSColin Riley 153*b9c1b51eSKate Stone bool IsVTableName(const char *name) override; 1545ec532a9SColin Riley 155*b9c1b51eSKate Stone bool GetDynamicTypeAndAddress(ValueObject &in_value, 156*b9c1b51eSKate Stone lldb::DynamicValueType use_dynamic, 157*b9c1b51eSKate Stone TypeAndOrName &class_type_or_name, 158*b9c1b51eSKate Stone Address &address, 1595f57b6eeSEnrico Granata Value::ValueType &value_type) override; 160c74275bcSEnrico Granata 161*b9c1b51eSKate Stone TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, 162*b9c1b51eSKate Stone ValueObject &static_value) override; 1635ec532a9SColin Riley 164*b9c1b51eSKate Stone bool CouldHaveDynamicValue(ValueObject &in_value) override; 1655ec532a9SColin Riley 166*b9c1b51eSKate Stone lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, 167*b9c1b51eSKate Stone bool catch_bp, 168*b9c1b51eSKate Stone bool throw_bp) override; 1695ec532a9SColin Riley 170*b9c1b51eSKate Stone bool LoadModule(const lldb::ModuleSP &module_sp); 1715ec532a9SColin Riley 172*b9c1b51eSKate Stone void DumpModules(Stream &strm) const; 1735ec532a9SColin Riley 174*b9c1b51eSKate Stone void DumpContexts(Stream &strm) const; 1754640cde1SColin Riley 176*b9c1b51eSKate Stone void DumpKernels(Stream &strm) const; 1774640cde1SColin Riley 178*b9c1b51eSKate Stone bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id); 179a0f08674SEwan Crawford 180*b9c1b51eSKate Stone void ListAllocations(Stream &strm, StackFrame *frame_ptr, 181*b9c1b51eSKate Stone const uint32_t index); 18215f2bd95SEwan Crawford 183*b9c1b51eSKate Stone bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr); 1840d2bfcfbSEwan Crawford 185*b9c1b51eSKate Stone void PlaceBreakpointOnKernel(Stream &strm, const char *name, 186*b9c1b51eSKate Stone const std::array<int, 3> coords, Error &error, 187b3f7f69dSAidan Dodds lldb::TargetSP target); 1884640cde1SColin Riley 189*b9c1b51eSKate Stone void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 1907dc7771cSEwan Crawford 191*b9c1b51eSKate Stone void Status(Stream &strm) const; 1924640cde1SColin Riley 193*b9c1b51eSKate Stone void ModulesDidLoad(const ModuleList &module_list) override; 194ef20b08fSColin Riley 195*b9c1b51eSKate Stone bool LoadAllocation(Stream &strm, const uint32_t alloc_id, 196*b9c1b51eSKate Stone const char *filename, StackFrame *frame_ptr); 19755232f09SEwan Crawford 198*b9c1b51eSKate Stone bool SaveAllocation(Stream &strm, const uint32_t alloc_id, 199*b9c1b51eSKate Stone const char *filename, StackFrame *frame_ptr); 20055232f09SEwan Crawford 201*b9c1b51eSKate Stone void Update(); 202ef20b08fSColin Riley 203*b9c1b51eSKate Stone void Initiate(); 204ef20b08fSColin Riley 205222b937cSEugene Zelenko //------------------------------------------------------------------ 206222b937cSEugene Zelenko // PluginInterface protocol 207222b937cSEugene Zelenko //------------------------------------------------------------------ 208*b9c1b51eSKate Stone lldb_private::ConstString GetPluginName() override; 2094640cde1SColin Riley 210*b9c1b51eSKate Stone uint32_t GetPluginVersion() override; 211222b937cSEugene Zelenko 212*b9c1b51eSKate Stone static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, 213*b9c1b51eSKate Stone Thread *thread_ptr); 2144f8817c2SEwan Crawford 215222b937cSEugene Zelenko protected: 21615f2bd95SEwan Crawford struct ScriptDetails; 21715f2bd95SEwan Crawford struct AllocationDetails; 2188b244e21SEwan Crawford struct Element; 21915f2bd95SEwan Crawford 220*b9c1b51eSKate Stone void InitSearchFilter(lldb::TargetSP target) { 2217dc7771cSEwan Crawford if (!m_filtersp) 2227dc7771cSEwan Crawford m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target)); 2237dc7771cSEwan Crawford } 2247dc7771cSEwan Crawford 225*b9c1b51eSKate Stone void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 2264640cde1SColin Riley 227*b9c1b51eSKate Stone void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind); 2284640cde1SColin Riley 229*b9c1b51eSKate Stone bool RefreshAllocation(AllocationDetails *allocation, StackFrame *frame_ptr); 23015f2bd95SEwan Crawford 231*b9c1b51eSKate Stone bool EvalRSExpression(const char *expression, StackFrame *frame_ptr, 232*b9c1b51eSKate Stone uint64_t *result); 23315f2bd95SEwan Crawford 234*b9c1b51eSKate Stone lldb::BreakpointSP CreateKernelBreakpoint(const ConstString &name); 2357dc7771cSEwan Crawford 236*b9c1b51eSKate Stone void BreakOnModuleKernels( 237*b9c1b51eSKate Stone const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 2387dc7771cSEwan Crawford 2394640cde1SColin Riley struct RuntimeHook; 240*b9c1b51eSKate Stone typedef void (RenderScriptRuntime::*CaptureStateFn)( 241*b9c1b51eSKate Stone RuntimeHook *hook_info, 242b3f7f69dSAidan Dodds ExecutionContext &context); // Please do this! 2434640cde1SColin Riley 244*b9c1b51eSKate Stone struct HookDefn { 2454640cde1SColin Riley const char *name; 24682780287SAidan Dodds const char *symbol_name_m32; // mangled name for the 32 bit architectures 24782780287SAidan Dodds const char *symbol_name_m64; // mangled name for the 64 bit archs 2484640cde1SColin Riley uint32_t version; 2494640cde1SColin Riley ModuleKind kind; 2504640cde1SColin Riley CaptureStateFn grabber; 2514640cde1SColin Riley }; 2524640cde1SColin Riley 253*b9c1b51eSKate Stone struct RuntimeHook { 2544640cde1SColin Riley lldb::addr_t address; 2554640cde1SColin Riley const HookDefn *defn; 2564640cde1SColin Riley lldb::BreakpointSP bp_sp; 2574640cde1SColin Riley }; 2584640cde1SColin Riley 2594640cde1SColin Riley typedef std::shared_ptr<RuntimeHook> RuntimeHookSP; 2604640cde1SColin Riley 2614640cde1SColin Riley lldb::ModuleSP m_libRS; 2624640cde1SColin Riley lldb::ModuleSP m_libRSDriver; 2634640cde1SColin Riley lldb::ModuleSP m_libRSCpuRef; 26498156583SEwan Crawford std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules; 26578f339d1SEwan Crawford 26678f339d1SEwan Crawford std::vector<std::unique_ptr<ScriptDetails>> m_scripts; 26778f339d1SEwan Crawford std::vector<std::unique_ptr<AllocationDetails>> m_allocations; 2684640cde1SColin Riley 269*b9c1b51eSKate Stone std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> 270*b9c1b51eSKate Stone m_scriptMappings; 2714640cde1SColin Riley std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks; 2724f8817c2SEwan Crawford std::map<lldb::user_id_t, std::shared_ptr<uint32_t>> m_conditional_breaks; 2734640cde1SColin Riley 274*b9c1b51eSKate Stone lldb::SearchFilterSP 275*b9c1b51eSKate Stone m_filtersp; // Needed to create breakpoints through Target API 2767dc7771cSEwan Crawford 277ef20b08fSColin Riley bool m_initiated; 2784640cde1SColin Riley bool m_debuggerPresentFlagged; 2797dc7771cSEwan Crawford bool m_breakAllKernels; 2804640cde1SColin Riley static const HookDefn s_runtimeHookDefns[]; 2814640cde1SColin Riley static const size_t s_runtimeHookCount; 28219459580SLuke Drummond LLVMUserExpression::IRPasses *m_ir_passes; 2834640cde1SColin Riley 2845ec532a9SColin Riley private: 2855ec532a9SColin Riley RenderScriptRuntime(Process *process); // Call CreateInstance instead. 2864640cde1SColin Riley 287*b9c1b51eSKate Stone static bool HookCallback(void *baton, StoppointCallbackContext *ctx, 288*b9c1b51eSKate Stone lldb::user_id_t break_id, 2894640cde1SColin Riley lldb::user_id_t break_loc_id); 2904640cde1SColin Riley 291*b9c1b51eSKate Stone static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 292*b9c1b51eSKate Stone lldb::user_id_t break_id, 293*b9c1b51eSKate Stone lldb::user_id_t break_loc_id); 294018f5a7eSEwan Crawford 295*b9c1b51eSKate Stone void HookCallback(RuntimeHook *hook_info, ExecutionContext &context); 2964640cde1SColin Riley 297*b9c1b51eSKate Stone void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context); 2984640cde1SColin Riley 299*b9c1b51eSKate Stone void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context); 300a0f08674SEwan Crawford 301*b9c1b51eSKate Stone void CaptureAllocationDestroy(RuntimeHook *hook_info, 302*b9c1b51eSKate Stone ExecutionContext &context); 303b3f7f69dSAidan Dodds 304*b9c1b51eSKate Stone void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context); 305b3f7f69dSAidan Dodds 306*b9c1b51eSKate Stone void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, 307*b9c1b51eSKate Stone ExecutionContext &context); 308b3f7f69dSAidan Dodds 309*b9c1b51eSKate Stone AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id); 310b3f7f69dSAidan Dodds 311*b9c1b51eSKate Stone std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *allocation, 312*b9c1b51eSKate Stone StackFrame *frame_ptr); 313b3f7f69dSAidan Dodds 314*b9c1b51eSKate Stone void SetElementSize(Element &elem); 315b3f7f69dSAidan Dodds 316*b9c1b51eSKate Stone static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, 317*b9c1b51eSKate Stone const char *var_name, uint64_t &val); 318b3f7f69dSAidan Dodds 319*b9c1b51eSKate Stone void FindStructTypeName(Element &elem, StackFrame *frame_ptr); 320b3f7f69dSAidan Dodds 321*b9c1b51eSKate Stone size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, 322*b9c1b51eSKate Stone size_t offset, const Element &elem); 323*b9c1b51eSKate Stone 324*b9c1b51eSKate Stone size_t CalculateElementHeaderSize(const Element &elem); 32526e52a70SEwan Crawford 32615f2bd95SEwan Crawford // 32715f2bd95SEwan Crawford // Helper functions for jitting the runtime 32815f2bd95SEwan Crawford // 32915f2bd95SEwan Crawford 330*b9c1b51eSKate Stone bool JITDataPointer(AllocationDetails *allocation, StackFrame *frame_ptr, 331b3f7f69dSAidan Dodds uint32_t x = 0, uint32_t y = 0, uint32_t z = 0); 33215f2bd95SEwan Crawford 333*b9c1b51eSKate Stone bool JITTypePointer(AllocationDetails *allocation, StackFrame *frame_ptr); 33415f2bd95SEwan Crawford 335*b9c1b51eSKate Stone bool JITTypePacked(AllocationDetails *allocation, StackFrame *frame_ptr); 33615f2bd95SEwan Crawford 337*b9c1b51eSKate Stone bool JITElementPacked(Element &elem, const lldb::addr_t context, 338*b9c1b51eSKate Stone StackFrame *frame_ptr); 3398b244e21SEwan Crawford 340*b9c1b51eSKate Stone bool JITAllocationSize(AllocationDetails *allocation, StackFrame *frame_ptr); 341a0f08674SEwan Crawford 342*b9c1b51eSKate Stone bool JITSubelements(Element &elem, const lldb::addr_t context, 343*b9c1b51eSKate Stone StackFrame *frame_ptr); 344b3f7f69dSAidan Dodds 345*b9c1b51eSKate Stone bool JITAllocationStride(AllocationDetails *allocation, 346*b9c1b51eSKate Stone StackFrame *frame_ptr); 347a0f08674SEwan Crawford 34878f339d1SEwan Crawford // Search for a script detail object using a target address. 34978f339d1SEwan Crawford // If a script does not currently exist this function will return nullptr. 35078f339d1SEwan Crawford // If 'create' is true and there is no previous script with this address, 351*b9c1b51eSKate Stone // then a new Script detail object will be created for this address and 352*b9c1b51eSKate Stone // returned. 353*b9c1b51eSKate Stone ScriptDetails *LookUpScript(lldb::addr_t address, bool create); 35478f339d1SEwan Crawford 355*b9c1b51eSKate Stone // Search for a previously saved allocation detail object using a target 356*b9c1b51eSKate Stone // address. 357*b9c1b51eSKate Stone // If an allocation does not exist for this address then nullptr will be 358*b9c1b51eSKate Stone // returned. 359*b9c1b51eSKate Stone AllocationDetails *LookUpAllocation(lldb::addr_t address); 3605d057637SLuke Drummond 361*b9c1b51eSKate Stone // Creates a new allocation with the specified address assigning a new ID and 362*b9c1b51eSKate Stone // removes 3635d057637SLuke Drummond // any previous stored allocation which has the same address. 364*b9c1b51eSKate Stone AllocationDetails *CreateAllocation(lldb::addr_t address); 36519459580SLuke Drummond 366*b9c1b51eSKate Stone bool GetOverrideExprOptions(clang::TargetOptions &prototype) override; 36719459580SLuke Drummond 368*b9c1b51eSKate Stone bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override; 3695ec532a9SColin Riley }; 3705ec532a9SColin Riley 3715ec532a9SColin Riley } // namespace lldb_private 3725ec532a9SColin Riley 3735ec532a9SColin Riley #endif // liblldb_RenderScriptRuntime_h_ 374