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 40 // Breakpoint Resolvers decide where a breakpoint is placed, 41 // so having our own allows us to limit the search scope to RS kernel modules. 42 // As well as check for .expand kernels as a fallback. 43 class RSBreakpointResolver : public BreakpointResolver 44 { 45 public: 46 RSBreakpointResolver(Breakpoint *bkpt, ConstString name): 47 BreakpointResolver (bkpt, BreakpointResolver::NameResolver), 48 m_kernel_name(name) 49 { 50 } 51 52 void 53 GetDescription(Stream *strm) override 54 { 55 if (strm) 56 strm->Printf("RenderScript kernel breakpoint for '%s'", m_kernel_name.AsCString()); 57 } 58 59 void 60 Dump(Stream *s) const override 61 { 62 } 63 64 Searcher::CallbackReturn 65 SearchCallback(SearchFilter &filter, 66 SymbolContext &context, 67 Address *addr, 68 bool containing) override; 69 70 Searcher::Depth 71 GetDepth() override 72 { 73 return Searcher::eDepthModule; 74 } 75 76 lldb::BreakpointResolverSP 77 CopyForBreakpoint(Breakpoint &breakpoint) override 78 { 79 lldb::BreakpointResolverSP ret_sp(new RSBreakpointResolver(&breakpoint, m_kernel_name)); 80 return ret_sp; 81 } 82 83 protected: 84 ConstString m_kernel_name; 85 }; 86 87 struct RSKernelDescriptor 88 { 89 public: 90 RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, uint32_t slot) 91 : m_module(module) 92 , m_name(name) 93 , m_slot(slot) 94 { 95 } 96 97 void Dump(Stream &strm) const; 98 99 const RSModuleDescriptor *m_module; 100 ConstString m_name; 101 RSSlot m_slot; 102 }; 103 104 struct RSGlobalDescriptor 105 { 106 public: 107 RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name ) 108 : m_module(module) 109 , m_name(name) 110 { 111 } 112 113 void Dump(Stream &strm) const; 114 115 const RSModuleDescriptor *m_module; 116 ConstString m_name; 117 }; 118 119 class RSModuleDescriptor 120 { 121 public: 122 RSModuleDescriptor(const lldb::ModuleSP &module) 123 : m_module(module) 124 { 125 } 126 127 ~RSModuleDescriptor() = default; 128 129 bool ParseRSInfo(); 130 131 void Dump(Stream &strm) const; 132 133 const lldb::ModuleSP m_module; 134 std::vector<RSKernelDescriptor> m_kernels; 135 std::vector<RSGlobalDescriptor> m_globals; 136 std::map<std::string, std::string> m_pragmas; 137 std::string m_resname; 138 }; 139 140 } // namespace lldb_renderscript 141 142 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime 143 { 144 public: 145 enum ModuleKind 146 { 147 eModuleKindIgnored, 148 eModuleKindLibRS, 149 eModuleKindDriver, 150 eModuleKindImpl, 151 eModuleKindKernelObj 152 }; 153 154 ~RenderScriptRuntime() override; 155 156 //------------------------------------------------------------------ 157 // Static Functions 158 //------------------------------------------------------------------ 159 static void Initialize(); 160 161 static void Terminate(); 162 163 static lldb_private::LanguageRuntime *CreateInstance(Process *process, lldb::LanguageType language); 164 165 static lldb::CommandObjectSP GetCommandObject(CommandInterpreter& interpreter); 166 167 static lldb_private::ConstString GetPluginNameStatic(); 168 169 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 170 171 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 172 173 static void ModulesDidLoad(const lldb::ProcessSP& process_sp, const ModuleList &module_list ); 174 175 bool IsVTableName(const char *name) override; 176 177 bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, 178 TypeAndOrName &class_type_or_name, Address &address, 179 Value::ValueType &value_type) override; 180 181 TypeAndOrName 182 FixUpDynamicType(const TypeAndOrName& type_and_or_name, 183 ValueObject& static_value) override; 184 185 bool CouldHaveDynamicValue(ValueObject &in_value) override; 186 187 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) override; 188 189 bool LoadModule(const lldb::ModuleSP &module_sp); 190 191 bool ProbeModules(const ModuleList module_list); 192 193 void DumpModules(Stream &strm) const; 194 195 void DumpContexts(Stream &strm) const; 196 197 void DumpKernels(Stream &strm) const; 198 199 bool DumpAllocation(Stream &strm, StackFrame* frame_ptr, const uint32_t id); 200 201 void ListAllocations(Stream &strm, StackFrame* frame_ptr, bool recompute); 202 203 void PlaceBreakpointOnKernel(Stream &strm, const char *name, const std::array<int,3> coords, 204 Error &error, lldb::TargetSP target); 205 206 void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 207 208 void Status(Stream &strm) const; 209 210 size_t GetAlternateManglings(const ConstString &mangled, std::vector<ConstString> &alternates) override { 211 return static_cast<size_t>(0); 212 } 213 214 void ModulesDidLoad(const ModuleList &module_list) override; 215 216 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, const char* filename, StackFrame* frame_ptr); 217 218 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, const char* filename, StackFrame* frame_ptr); 219 220 void Update(); 221 222 void Initiate(); 223 224 //------------------------------------------------------------------ 225 // PluginInterface protocol 226 //------------------------------------------------------------------ 227 lldb_private::ConstString GetPluginName() override; 228 229 uint32_t GetPluginVersion() override; 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<int>> 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 296 private: 297 // Used to index expression format strings 298 enum ExpressionStrings 299 { 300 eExprGetOffsetPtr = 0, 301 eExprAllocGetType, 302 eExprTypeDimX, 303 eExprTypeDimY, 304 eExprTypeDimZ, 305 eExprTypeElemPtr, 306 eExprElementType, 307 eExprElementKind, 308 eExprElementVec, 309 eExprElementFieldCount, 310 eExprSubelementsId, 311 eExprSubelementsName, 312 eExprSubelementsArrSize 313 }; 314 315 RenderScriptRuntime(Process *process); // Call CreateInstance instead. 316 317 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id, 318 lldb::user_id_t break_loc_id); 319 320 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 321 lldb::user_id_t break_id, lldb::user_id_t break_loc_id); 322 323 void HookCallback(RuntimeHook* hook_info, ExecutionContext& context); 324 325 bool GetArgSimple(ExecutionContext& context, uint32_t arg, uint64_t* data); 326 327 void CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext& context); 328 void CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionContext& context); 329 void CaptureAllocationDestroy(RuntimeHook* hook_info, ExecutionContext& context); 330 void CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionContext& context); 331 332 AllocationDetails* FindAllocByID(Stream &strm, const uint32_t alloc_id); 333 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails* allocation, StackFrame* frame_ptr); 334 void SetElementSize(Element& elem); 335 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, const char* var_name, uint64_t& val); 336 void FindStructTypeName(Element& elem, StackFrame* frame_ptr); 337 338 // 339 // Helper functions for jitting the runtime 340 // 341 const char* JITTemplate(ExpressionStrings e); 342 343 bool JITDataPointer(AllocationDetails* allocation, StackFrame* frame_ptr, 344 unsigned int x = 0, unsigned int y = 0, unsigned int z = 0); 345 346 bool JITTypePointer(AllocationDetails* allocation, StackFrame* frame_ptr); 347 348 bool JITTypePacked(AllocationDetails* allocation, StackFrame* frame_ptr); 349 350 bool JITElementPacked(Element& elem, const lldb::addr_t context, StackFrame* frame_ptr); 351 352 bool JITAllocationSize(AllocationDetails* allocation, StackFrame* frame_ptr); 353 354 bool JITSubelements(Element& elem, const lldb::addr_t context, StackFrame* frame_ptr); 355 356 bool JITAllocationStride(AllocationDetails* allocation, StackFrame* frame_ptr); 357 358 // Search for a script detail object using a target address. 359 // If a script does not currently exist this function will return nullptr. 360 // If 'create' is true and there is no previous script with this address, 361 // then a new Script detail object will be created for this address and returned. 362 ScriptDetails* LookUpScript(lldb::addr_t address, bool create); 363 364 // Search for a previously saved allocation detail object using a target address. 365 // If an allocation does not exist for this address then nullptr will be returned. 366 // If 'create' is true and there is no previous allocation then a new allocation 367 // detail object will be created for this address and returned. 368 AllocationDetails* LookUpAllocation(lldb::addr_t address, bool create); 369 }; 370 371 } // namespace lldb_private 372 373 #endif // liblldb_RenderScriptRuntime_h_ 374