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