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