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 #include <array> 14 #include <map> 15 #include <memory> 16 #include <string> 17 #include <vector> 18 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Expression/LLVMUserExpression.h" 23 #include "lldb/Target/CPPLanguageRuntime.h" 24 #include "lldb/Target/LanguageRuntime.h" 25 #include "lldb/lldb-private.h" 26 27 namespace lldb_private { 28 namespace lldb_renderscript { 29 30 typedef uint32_t RSSlot; 31 class RSModuleDescriptor; 32 struct RSGlobalDescriptor; 33 struct RSKernelDescriptor; 34 struct RSReductionDescriptor; 35 struct RSScriptGroupDescriptor; 36 37 typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP; 38 typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP; 39 typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP; 40 typedef std::shared_ptr<RSScriptGroupDescriptor> RSScriptGroupDescriptorSP; 41 42 struct RSCoordinate { 43 uint32_t x, y, z; 44 RSCoordinateRSCoordinate45 RSCoordinate() : x(), y(), z(){}; 46 47 bool operator==(const lldb_renderscript::RSCoordinate &rhs) { 48 return x == rhs.x && y == rhs.y && z == rhs.z; 49 } 50 }; 51 52 // Breakpoint Resolvers decide where a breakpoint is placed, so having our own 53 // allows us to limit the search scope to RS kernel modules. As well as check 54 // for .expand kernels as a fallback. 55 class RSBreakpointResolver : public BreakpointResolver { 56 public: RSBreakpointResolver(Breakpoint * bp,ConstString name)57 RSBreakpointResolver(Breakpoint *bp, ConstString name) 58 : BreakpointResolver(bp, BreakpointResolver::NameResolver), 59 m_kernel_name(name) {} 60 GetDescription(Stream * strm)61 void GetDescription(Stream *strm) override { 62 if (strm) 63 strm->Printf("RenderScript kernel breakpoint for '%s'", 64 m_kernel_name.AsCString()); 65 } 66 Dump(Stream * s)67 void Dump(Stream *s) const override {} 68 69 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 70 SymbolContext &context, Address *addr, 71 bool containing) override; 72 GetDepth()73 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; } 74 75 lldb::BreakpointResolverSP CopyForBreakpoint(Breakpoint & breakpoint)76 CopyForBreakpoint(Breakpoint &breakpoint) override { 77 lldb::BreakpointResolverSP ret_sp( 78 new RSBreakpointResolver(&breakpoint, m_kernel_name)); 79 return ret_sp; 80 } 81 82 protected: 83 ConstString m_kernel_name; 84 }; 85 86 class RSReduceBreakpointResolver : public BreakpointResolver { 87 public: 88 enum ReduceKernelTypeFlags { 89 eKernelTypeAll = ~(0), 90 eKernelTypeNone = 0, 91 eKernelTypeAccum = (1 << 0), 92 eKernelTypeInit = (1 << 1), 93 eKernelTypeComb = (1 << 2), 94 eKernelTypeOutC = (1 << 3), 95 eKernelTypeHalter = (1 << 4) 96 }; 97 98 RSReduceBreakpointResolver( 99 Breakpoint *breakpoint, ConstString reduce_name, 100 std::vector<lldb_renderscript::RSModuleDescriptorSP> *rs_modules, 101 int kernel_types = eKernelTypeAll) BreakpointResolver(breakpoint,BreakpointResolver::NameResolver)102 : BreakpointResolver(breakpoint, BreakpointResolver::NameResolver), 103 m_reduce_name(reduce_name), m_rsmodules(rs_modules), 104 m_kernel_types(kernel_types) { 105 // The reduce breakpoint resolver handles adding breakpoints for named 106 // reductions. 107 // Breakpoints will be resolved for all constituent kernels in the named 108 // reduction 109 } 110 GetDescription(Stream * strm)111 void GetDescription(Stream *strm) override { 112 if (strm) 113 strm->Printf("RenderScript reduce breakpoint for '%s'", 114 m_reduce_name.AsCString()); 115 } 116 Dump(Stream * s)117 void Dump(Stream *s) const override {} 118 119 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 120 SymbolContext &context, Address *addr, 121 bool containing) override; 122 GetDepth()123 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; } 124 125 lldb::BreakpointResolverSP CopyForBreakpoint(Breakpoint & breakpoint)126 CopyForBreakpoint(Breakpoint &breakpoint) override { 127 lldb::BreakpointResolverSP ret_sp(new RSReduceBreakpointResolver( 128 &breakpoint, m_reduce_name, m_rsmodules, m_kernel_types)); 129 return ret_sp; 130 } 131 132 private: 133 ConstString m_reduce_name; // The name of the reduction 134 std::vector<lldb_renderscript::RSModuleDescriptorSP> *m_rsmodules; 135 int m_kernel_types; 136 }; 137 138 struct RSKernelDescriptor { 139 public: RSKernelDescriptorRSKernelDescriptor140 RSKernelDescriptor(const RSModuleDescriptor *module, llvm::StringRef name, 141 uint32_t slot) 142 : m_module(module), m_name(name), m_slot(slot) {} 143 144 void Dump(Stream &strm) const; 145 146 const RSModuleDescriptor *m_module; 147 ConstString m_name; 148 RSSlot m_slot; 149 }; 150 151 struct RSGlobalDescriptor { 152 public: RSGlobalDescriptorRSGlobalDescriptor153 RSGlobalDescriptor(const RSModuleDescriptor *module, llvm::StringRef name) 154 : m_module(module), m_name(name) {} 155 156 void Dump(Stream &strm) const; 157 158 const RSModuleDescriptor *m_module; 159 ConstString m_name; 160 }; 161 162 struct RSReductionDescriptor { 163 RSReductionDescriptor(const RSModuleDescriptor *module, uint32_t sig, 164 uint32_t accum_data_size, llvm::StringRef name, 165 llvm::StringRef init_name, llvm::StringRef accum_name, 166 llvm::StringRef comb_name, llvm::StringRef outc_name, 167 llvm::StringRef halter_name = ".") m_moduleRSReductionDescriptor168 : m_module(module), m_reduce_name(name), m_init_name(init_name), 169 m_accum_name(accum_name), m_comb_name(comb_name), 170 m_outc_name(outc_name), m_halter_name(halter_name) { 171 // TODO Check whether the combiner is an autogenerated name, and track 172 // this 173 } 174 175 void Dump(Stream &strm) const; 176 177 const RSModuleDescriptor *m_module; 178 ConstString m_reduce_name; // This is the name given to the general reduction 179 // as a group as passed to pragma 180 // reduce(m_reduce_name). There is no kernel function with this name 181 ConstString m_init_name; // The name of the initializer name. "." if no 182 // initializer given 183 ConstString m_accum_name; // The accumulator function name. "." if not given 184 ConstString m_comb_name; // The name of the combiner function. If this was not 185 // given, a name is generated by the 186 // compiler. TODO 187 ConstString m_outc_name; // The name of the outconverter 188 189 ConstString m_halter_name; // The name of the halter function. XXX This is not 190 // yet specified by the RenderScript 191 // compiler or runtime, and its semantics and existence is still under 192 // discussion by the 193 // RenderScript Contributors 194 RSSlot m_accum_sig; // metatdata signature for this reduction (bitwise mask of 195 // type information (see 196 // libbcc/include/bcinfo/MetadataExtractor.h 197 uint32_t m_accum_data_size; // Data size of the accumulator function input 198 bool m_comb_name_generated; // Was the combiner name generated by the compiler 199 }; 200 201 class RSModuleDescriptor { 202 std::string m_slang_version; 203 std::string m_bcc_version; 204 205 bool ParseVersionInfo(llvm::StringRef *, size_t n_lines); 206 207 bool ParseExportForeachCount(llvm::StringRef *, size_t n_lines); 208 209 bool ParseExportVarCount(llvm::StringRef *, size_t n_lines); 210 211 bool ParseExportReduceCount(llvm::StringRef *, size_t n_lines); 212 213 bool ParseBuildChecksum(llvm::StringRef *, size_t n_lines); 214 215 bool ParsePragmaCount(llvm::StringRef *, size_t n_lines); 216 217 public: RSModuleDescriptor(const lldb::ModuleSP & module)218 RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {} 219 220 ~RSModuleDescriptor() = default; 221 222 bool ParseRSInfo(); 223 224 void Dump(Stream &strm) const; 225 226 void WarnIfVersionMismatch(Stream *s) const; 227 228 const lldb::ModuleSP m_module; 229 std::vector<RSKernelDescriptor> m_kernels; 230 std::vector<RSGlobalDescriptor> m_globals; 231 std::vector<RSReductionDescriptor> m_reductions; 232 std::map<std::string, std::string> m_pragmas; 233 std::string m_resname; 234 }; 235 236 struct RSScriptGroupDescriptor { 237 struct Kernel { 238 ConstString m_name; 239 lldb::addr_t m_addr; 240 }; 241 ConstString m_name; 242 std::vector<Kernel> m_kernels; 243 }; 244 245 typedef std::vector<RSScriptGroupDescriptorSP> RSScriptGroupList; 246 247 class RSScriptGroupBreakpointResolver : public BreakpointResolver { 248 public: RSScriptGroupBreakpointResolver(Breakpoint * bp,const ConstString & name,const RSScriptGroupList & groups,bool stop_on_all)249 RSScriptGroupBreakpointResolver(Breakpoint *bp, const ConstString &name, 250 const RSScriptGroupList &groups, 251 bool stop_on_all) 252 : BreakpointResolver(bp, BreakpointResolver::NameResolver), 253 m_group_name(name), m_script_groups(groups), 254 m_stop_on_all(stop_on_all) {} 255 GetDescription(Stream * strm)256 void GetDescription(Stream *strm) override { 257 if (strm) 258 strm->Printf("RenderScript ScriptGroup breakpoint for '%s'", 259 m_group_name.AsCString()); 260 } 261 Dump(Stream * s)262 void Dump(Stream *s) const override {} 263 264 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 265 SymbolContext &context, Address *addr, 266 bool containing) override; 267 GetDepth()268 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; } 269 270 lldb::BreakpointResolverSP CopyForBreakpoint(Breakpoint & breakpoint)271 CopyForBreakpoint(Breakpoint &breakpoint) override { 272 lldb::BreakpointResolverSP ret_sp(new RSScriptGroupBreakpointResolver( 273 &breakpoint, m_group_name, m_script_groups, m_stop_on_all)); 274 return ret_sp; 275 } 276 277 protected: 278 const RSScriptGroupDescriptorSP FindScriptGroup(const ConstString & name)279 FindScriptGroup(const ConstString &name) const { 280 for (auto sg : m_script_groups) { 281 if (ConstString::Compare(sg->m_name, name) == 0) 282 return sg; 283 } 284 return RSScriptGroupDescriptorSP(); 285 } 286 287 ConstString m_group_name; 288 const RSScriptGroupList &m_script_groups; 289 bool m_stop_on_all; 290 }; 291 } // namespace lldb_renderscript 292 293 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime { 294 public: 295 enum ModuleKind { 296 eModuleKindIgnored, 297 eModuleKindLibRS, 298 eModuleKindDriver, 299 eModuleKindImpl, 300 eModuleKindKernelObj 301 }; 302 303 ~RenderScriptRuntime() override; 304 305 //------------------------------------------------------------------ 306 // Static Functions 307 //------------------------------------------------------------------ 308 static void Initialize(); 309 310 static void Terminate(); 311 312 static lldb_private::LanguageRuntime * 313 CreateInstance(Process *process, lldb::LanguageType language); 314 315 static lldb::CommandObjectSP 316 GetCommandObject(CommandInterpreter &interpreter); 317 318 static lldb_private::ConstString GetPluginNameStatic(); 319 320 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 321 322 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 323 324 static void ModulesDidLoad(const lldb::ProcessSP &process_sp, 325 const ModuleList &module_list); 326 327 bool IsVTableName(const char *name) override; 328 329 bool GetDynamicTypeAndAddress(ValueObject &in_value, 330 lldb::DynamicValueType use_dynamic, 331 TypeAndOrName &class_type_or_name, 332 Address &address, 333 Value::ValueType &value_type) override; 334 335 TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, 336 ValueObject &static_value) override; 337 338 bool CouldHaveDynamicValue(ValueObject &in_value) override; 339 340 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bp, 341 bool catch_bp, 342 bool throw_bp) override; 343 344 bool LoadModule(const lldb::ModuleSP &module_sp); 345 346 void DumpModules(Stream &strm) const; 347 348 void DumpContexts(Stream &strm) const; 349 350 void DumpKernels(Stream &strm) const; 351 352 bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id); 353 354 void ListAllocations(Stream &strm, StackFrame *frame_ptr, 355 const uint32_t index); 356 357 bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr); 358 359 bool PlaceBreakpointOnKernel( 360 lldb::TargetSP target, Stream &messages, const char *name, 361 const lldb_renderscript::RSCoordinate *coords = nullptr); 362 363 bool PlaceBreakpointOnReduction( 364 lldb::TargetSP target, Stream &messages, const char *reduce_name, 365 const lldb_renderscript::RSCoordinate *coords = nullptr, 366 int kernel_types = ~(0)); 367 368 bool PlaceBreakpointOnScriptGroup(lldb::TargetSP target, Stream &strm, 369 const ConstString &name, bool stop_on_all); 370 371 void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 372 373 void DumpStatus(Stream &strm) const; 374 375 void ModulesDidLoad(const ModuleList &module_list) override; 376 377 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, 378 const char *filename, StackFrame *frame_ptr); 379 380 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, 381 const char *filename, StackFrame *frame_ptr); 382 383 void Update(); 384 385 void Initiate(); 386 GetScriptGroups()387 const lldb_renderscript::RSScriptGroupList &GetScriptGroups() const { 388 return m_scriptGroups; 389 }; 390 IsKnownKernel(const ConstString & name)391 bool IsKnownKernel(const ConstString &name) { 392 for (const auto &module : m_rsmodules) 393 for (const auto &kernel : module->m_kernels) 394 if (kernel.m_name == name) 395 return true; 396 return false; 397 } 398 399 //------------------------------------------------------------------ 400 // PluginInterface protocol 401 //------------------------------------------------------------------ 402 lldb_private::ConstString GetPluginName() override; 403 404 uint32_t GetPluginVersion() override; 405 406 static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, 407 Thread *thread_ptr); 408 409 bool ResolveKernelName(lldb::addr_t kernel_address, ConstString &name); 410 411 protected: 412 struct ScriptDetails; 413 struct AllocationDetails; 414 struct Element; 415 416 lldb_renderscript::RSScriptGroupList m_scriptGroups; 417 InitSearchFilter(lldb::TargetSP target)418 void InitSearchFilter(lldb::TargetSP target) { 419 if (!m_filtersp) 420 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target)); 421 } 422 423 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 424 425 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind); 426 427 bool RefreshAllocation(AllocationDetails *alloc, StackFrame *frame_ptr); 428 429 bool EvalRSExpression(const char *expression, StackFrame *frame_ptr, 430 uint64_t *result); 431 432 lldb::BreakpointSP CreateScriptGroupBreakpoint(const ConstString &name, 433 bool multi); 434 435 lldb::BreakpointSP CreateKernelBreakpoint(const ConstString &name); 436 437 lldb::BreakpointSP CreateReductionBreakpoint(const ConstString &name, 438 int kernel_types); 439 440 void BreakOnModuleKernels( 441 const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 442 443 struct RuntimeHook; 444 typedef void (RenderScriptRuntime::*CaptureStateFn)( 445 RuntimeHook *hook_info, 446 ExecutionContext &context); // Please do this! 447 448 struct HookDefn { 449 const char *name; 450 const char *symbol_name_m32; // mangled name for the 32 bit architectures 451 const char *symbol_name_m64; // mangled name for the 64 bit archs 452 uint32_t version; 453 ModuleKind kind; 454 CaptureStateFn grabber; 455 }; 456 457 struct RuntimeHook { 458 lldb::addr_t address; 459 const HookDefn *defn; 460 lldb::BreakpointSP bp_sp; 461 }; 462 463 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP; 464 465 lldb::ModuleSP m_libRS; 466 lldb::ModuleSP m_libRSDriver; 467 lldb::ModuleSP m_libRSCpuRef; 468 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules; 469 470 std::vector<std::unique_ptr<ScriptDetails>> m_scripts; 471 std::vector<std::unique_ptr<AllocationDetails>> m_allocations; 472 473 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> 474 m_scriptMappings; 475 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks; 476 std::map<lldb::user_id_t, std::unique_ptr<lldb_renderscript::RSCoordinate>> 477 m_conditional_breaks; 478 479 lldb::SearchFilterSP 480 m_filtersp; // Needed to create breakpoints through Target API 481 482 bool m_initiated; 483 bool m_debuggerPresentFlagged; 484 bool m_breakAllKernels; 485 static const HookDefn s_runtimeHookDefns[]; 486 static const size_t s_runtimeHookCount; 487 LLVMUserExpression::IRPasses *m_ir_passes; 488 489 private: 490 RenderScriptRuntime(Process *process); // Call CreateInstance instead. 491 492 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, 493 lldb::user_id_t break_id, 494 lldb::user_id_t break_loc_id); 495 496 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 497 lldb::user_id_t break_id, 498 lldb::user_id_t break_loc_id); 499 500 void HookCallback(RuntimeHook *hook_info, ExecutionContext &context); 501 502 // Callback function when 'debugHintScriptGroup2' executes on the target. 503 void CaptureDebugHintScriptGroup2(RuntimeHook *hook_info, 504 ExecutionContext &context); 505 506 void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context); 507 508 void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context); 509 510 void CaptureAllocationDestroy(RuntimeHook *hook_info, 511 ExecutionContext &context); 512 513 void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context); 514 515 void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, 516 ExecutionContext &context); 517 518 AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id); 519 520 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *alloc, 521 StackFrame *frame_ptr); 522 523 void SetElementSize(Element &elem); 524 525 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, 526 const char *var_name, uint64_t &val); 527 528 void FindStructTypeName(Element &elem, StackFrame *frame_ptr); 529 530 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, 531 size_t offset, const Element &elem); 532 533 size_t CalculateElementHeaderSize(const Element &elem); 534 535 void SetConditional(lldb::BreakpointSP bp, lldb_private::Stream &messages, 536 const lldb_renderscript::RSCoordinate &coord); 537 // 538 // Helper functions for jitting the runtime 539 // 540 541 bool JITDataPointer(AllocationDetails *alloc, StackFrame *frame_ptr, 542 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0); 543 544 bool JITTypePointer(AllocationDetails *alloc, StackFrame *frame_ptr); 545 546 bool JITTypePacked(AllocationDetails *alloc, StackFrame *frame_ptr); 547 548 bool JITElementPacked(Element &elem, const lldb::addr_t context, 549 StackFrame *frame_ptr); 550 551 bool JITAllocationSize(AllocationDetails *alloc, StackFrame *frame_ptr); 552 553 bool JITSubelements(Element &elem, const lldb::addr_t context, 554 StackFrame *frame_ptr); 555 556 bool JITAllocationStride(AllocationDetails *alloc, StackFrame *frame_ptr); 557 558 // Search for a script detail object using a target address. 559 // If a script does not currently exist this function will return nullptr. 560 // If 'create' is true and there is no previous script with this address, 561 // then a new Script detail object will be created for this address and 562 // returned. 563 ScriptDetails *LookUpScript(lldb::addr_t address, bool create); 564 565 // Search for a previously saved allocation detail object using a target 566 // address. 567 // If an allocation does not exist for this address then nullptr will be 568 // returned. 569 AllocationDetails *LookUpAllocation(lldb::addr_t address); 570 571 // Creates a new allocation with the specified address assigning a new ID and 572 // removes 573 // any previous stored allocation which has the same address. 574 AllocationDetails *CreateAllocation(lldb::addr_t address); 575 576 bool GetOverrideExprOptions(clang::TargetOptions &prototype) override; 577 578 bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override; 579 }; 580 581 } // namespace lldb_private 582 583 #endif // liblldb_RenderScriptRuntime_h_ 584