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