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, y, z; 48 RSCoordinateRSCoordinate49 RSCoordinate() : x(), y(), z(){}; 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) { 175 // TODO Check whether the combiner is an autogenerated name, and track 176 // this 177 } 178 179 void Dump(Stream &strm) const; 180 181 const RSModuleDescriptor *m_module; 182 ConstString m_reduce_name; // This is the name given to the general reduction 183 // as a group as passed to pragma 184 // reduce(m_reduce_name). There is no kernel function with this name 185 ConstString m_init_name; // The name of the initializer name. "." if no 186 // initializer given 187 ConstString m_accum_name; // The accumulator function name. "." if not given 188 ConstString m_comb_name; // The name of the combiner function. If this was not 189 // given, a name is generated by the 190 // compiler. TODO 191 ConstString m_outc_name; // The name of the outconverter 192 193 ConstString m_halter_name; // The name of the halter function. XXX This is not 194 // yet specified by the RenderScript 195 // compiler or runtime, and its semantics and existence is still under 196 // discussion by the 197 // RenderScript Contributors 198 RSSlot m_accum_sig; // metatdata signature for this reduction (bitwise mask of 199 // type information (see 200 // libbcc/include/bcinfo/MetadataExtractor.h 201 uint32_t m_accum_data_size; // Data size of the accumulator function input 202 bool m_comb_name_generated; // Was the combiner name generated by the compiler 203 }; 204 205 class RSModuleDescriptor { 206 std::string m_slang_version; 207 std::string m_bcc_version; 208 209 bool ParseVersionInfo(llvm::StringRef *, size_t n_lines); 210 211 bool ParseExportForeachCount(llvm::StringRef *, size_t n_lines); 212 213 bool ParseExportVarCount(llvm::StringRef *, size_t n_lines); 214 215 bool ParseExportReduceCount(llvm::StringRef *, size_t n_lines); 216 217 bool ParseBuildChecksum(llvm::StringRef *, size_t n_lines); 218 219 bool ParsePragmaCount(llvm::StringRef *, size_t n_lines); 220 221 public: RSModuleDescriptor(const lldb::ModuleSP & module)222 RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {} 223 224 ~RSModuleDescriptor() = default; 225 226 bool ParseRSInfo(); 227 228 void Dump(Stream &strm) const; 229 230 void WarnIfVersionMismatch(Stream *s) const; 231 232 const lldb::ModuleSP m_module; 233 std::vector<RSKernelDescriptor> m_kernels; 234 std::vector<RSGlobalDescriptor> m_globals; 235 std::vector<RSReductionDescriptor> m_reductions; 236 std::map<std::string, std::string> m_pragmas; 237 std::string m_resname; 238 }; 239 240 struct RSScriptGroupDescriptor { 241 struct Kernel { 242 ConstString m_name; 243 lldb::addr_t m_addr; 244 }; 245 ConstString m_name; 246 std::vector<Kernel> m_kernels; 247 }; 248 249 typedef std::vector<RSScriptGroupDescriptorSP> RSScriptGroupList; 250 251 class RSScriptGroupBreakpointResolver : public BreakpointResolver { 252 public: RSScriptGroupBreakpointResolver(const lldb::BreakpointSP & bp,ConstString name,const RSScriptGroupList & groups,bool stop_on_all)253 RSScriptGroupBreakpointResolver(const lldb::BreakpointSP &bp, 254 ConstString name, 255 const RSScriptGroupList &groups, 256 bool stop_on_all) 257 : BreakpointResolver(bp, BreakpointResolver::NameResolver), 258 m_group_name(name), m_script_groups(groups), 259 m_stop_on_all(stop_on_all) {} 260 GetDescription(Stream * strm)261 void GetDescription(Stream *strm) override { 262 if (strm) 263 strm->Printf("RenderScript ScriptGroup breakpoint for '%s'", 264 m_group_name.AsCString()); 265 } 266 Dump(Stream * s)267 void Dump(Stream *s) const override {} 268 269 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 270 SymbolContext &context, 271 Address *addr) override; 272 GetDepth()273 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; } 274 275 lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP & breakpoint)276 CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override { 277 lldb::BreakpointResolverSP ret_sp(new RSScriptGroupBreakpointResolver( 278 breakpoint, m_group_name, m_script_groups, m_stop_on_all)); 279 return ret_sp; 280 } 281 282 protected: 283 const RSScriptGroupDescriptorSP FindScriptGroup(ConstString name)284 FindScriptGroup(ConstString name) const { 285 for (auto sg : m_script_groups) { 286 if (ConstString::Compare(sg->m_name, name) == 0) 287 return sg; 288 } 289 return RSScriptGroupDescriptorSP(); 290 } 291 292 ConstString m_group_name; 293 const RSScriptGroupList &m_script_groups; 294 bool m_stop_on_all; 295 }; 296 } // namespace lldb_renderscript 297 298 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime { 299 public: 300 enum ModuleKind { 301 eModuleKindIgnored, 302 eModuleKindLibRS, 303 eModuleKindDriver, 304 eModuleKindImpl, 305 eModuleKindKernelObj 306 }; 307 308 ~RenderScriptRuntime() override; 309 310 // Static Functions 311 static void Initialize(); 312 313 static void Terminate(); 314 315 static lldb_private::LanguageRuntime * 316 CreateInstance(Process *process, lldb::LanguageType language); 317 318 static lldb::CommandObjectSP 319 GetCommandObject(CommandInterpreter &interpreter); 320 321 static lldb_private::ConstString GetPluginNameStatic(); 322 323 static char ID; 324 isA(const void * ClassID)325 bool isA(const void *ClassID) const override { 326 return ClassID == &ID || CPPLanguageRuntime::isA(ClassID); 327 } 328 classof(const LanguageRuntime * runtime)329 static bool classof(const LanguageRuntime *runtime) { 330 return runtime->isA(&ID); 331 } 332 333 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 334 335 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 336 337 static void ModulesDidLoad(const lldb::ProcessSP &process_sp, 338 const ModuleList &module_list); 339 340 bool GetDynamicTypeAndAddress(ValueObject &in_value, 341 lldb::DynamicValueType use_dynamic, 342 TypeAndOrName &class_type_or_name, 343 Address &address, 344 Value::ValueType &value_type) override; 345 346 TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, 347 ValueObject &static_value) override; 348 349 bool CouldHaveDynamicValue(ValueObject &in_value) override; 350 351 lldb::BreakpointResolverSP 352 CreateExceptionResolver(const lldb::BreakpointSP &bp, 353 bool catch_bp, bool throw_bp) override; 354 355 bool LoadModule(const lldb::ModuleSP &module_sp); 356 357 void DumpModules(Stream &strm) const; 358 359 void DumpContexts(Stream &strm) const; 360 361 void DumpKernels(Stream &strm) const; 362 363 bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id); 364 365 void ListAllocations(Stream &strm, StackFrame *frame_ptr, 366 const uint32_t index); 367 368 bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr); 369 370 bool PlaceBreakpointOnKernel( 371 lldb::TargetSP target, Stream &messages, const char *name, 372 const lldb_renderscript::RSCoordinate *coords = nullptr); 373 374 bool PlaceBreakpointOnReduction( 375 lldb::TargetSP target, Stream &messages, const char *reduce_name, 376 const lldb_renderscript::RSCoordinate *coords = nullptr, 377 int kernel_types = ~(0)); 378 379 bool PlaceBreakpointOnScriptGroup(lldb::TargetSP target, Stream &strm, 380 ConstString name, bool stop_on_all); 381 382 void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 383 384 void DumpStatus(Stream &strm) const; 385 386 void ModulesDidLoad(const ModuleList &module_list) override; 387 388 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, 389 const char *filename, StackFrame *frame_ptr); 390 391 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, 392 const char *filename, StackFrame *frame_ptr); 393 394 void Update(); 395 396 void Initiate(); 397 GetScriptGroups()398 const lldb_renderscript::RSScriptGroupList &GetScriptGroups() const { 399 return m_scriptGroups; 400 }; 401 IsKnownKernel(ConstString name)402 bool IsKnownKernel(ConstString name) { 403 for (const auto &module : m_rsmodules) 404 for (const auto &kernel : module->m_kernels) 405 if (kernel.m_name == name) 406 return true; 407 return false; 408 } 409 410 bool GetOverrideExprOptions(clang::TargetOptions &prototype); 411 412 // PluginInterface protocol 413 lldb_private::ConstString GetPluginName() override; 414 415 uint32_t GetPluginVersion() override; 416 417 static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, 418 Thread *thread_ptr); 419 420 bool ResolveKernelName(lldb::addr_t kernel_address, ConstString &name); 421 422 protected: 423 struct ScriptDetails; 424 struct AllocationDetails; 425 struct Element; 426 427 lldb_renderscript::RSScriptGroupList m_scriptGroups; 428 InitSearchFilter(lldb::TargetSP target)429 void InitSearchFilter(lldb::TargetSP target) { 430 if (!m_filtersp) 431 m_filtersp = 432 std::make_shared<SearchFilterForUnconstrainedSearches>(target); 433 } 434 435 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 436 437 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind); 438 439 bool RefreshAllocation(AllocationDetails *alloc, StackFrame *frame_ptr); 440 441 bool EvalRSExpression(const char *expression, StackFrame *frame_ptr, 442 uint64_t *result); 443 444 lldb::BreakpointSP CreateScriptGroupBreakpoint(ConstString name, 445 bool multi); 446 447 lldb::BreakpointSP CreateKernelBreakpoint(ConstString name); 448 449 lldb::BreakpointSP CreateReductionBreakpoint(ConstString name, 450 int kernel_types); 451 452 void BreakOnModuleKernels( 453 const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 454 455 struct RuntimeHook; 456 typedef void (RenderScriptRuntime::*CaptureStateFn)( 457 RuntimeHook *hook_info, 458 ExecutionContext &context); // Please do this! 459 460 struct HookDefn { 461 const char *name; 462 const char *symbol_name_m32; // mangled name for the 32 bit architectures 463 const char *symbol_name_m64; // mangled name for the 64 bit archs 464 uint32_t version; 465 ModuleKind kind; 466 CaptureStateFn grabber; 467 }; 468 469 struct RuntimeHook { 470 lldb::addr_t address; 471 const HookDefn *defn; 472 lldb::BreakpointSP bp_sp; 473 }; 474 475 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP; 476 477 lldb::ModuleSP m_libRS; 478 lldb::ModuleSP m_libRSDriver; 479 lldb::ModuleSP m_libRSCpuRef; 480 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules; 481 482 std::vector<std::unique_ptr<ScriptDetails>> m_scripts; 483 std::vector<std::unique_ptr<AllocationDetails>> m_allocations; 484 485 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> 486 m_scriptMappings; 487 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks; 488 std::map<lldb::user_id_t, std::unique_ptr<lldb_renderscript::RSCoordinate>> 489 m_conditional_breaks; 490 491 lldb::SearchFilterSP 492 m_filtersp; // Needed to create breakpoints through Target API 493 494 bool m_initiated; 495 bool m_debuggerPresentFlagged; 496 bool m_breakAllKernels; 497 static const HookDefn s_runtimeHookDefns[]; 498 static const size_t s_runtimeHookCount; 499 LLVMUserExpression::IRPasses *m_ir_passes; 500 501 private: 502 RenderScriptRuntime(Process *process); // Call CreateInstance instead. 503 504 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, 505 lldb::user_id_t break_id, 506 lldb::user_id_t break_loc_id); 507 508 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 509 lldb::user_id_t break_id, 510 lldb::user_id_t break_loc_id); 511 512 void HookCallback(RuntimeHook *hook_info, ExecutionContext &context); 513 514 // Callback function when 'debugHintScriptGroup2' executes on the target. 515 void CaptureDebugHintScriptGroup2(RuntimeHook *hook_info, 516 ExecutionContext &context); 517 518 void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context); 519 520 void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context); 521 522 void CaptureAllocationDestroy(RuntimeHook *hook_info, 523 ExecutionContext &context); 524 525 void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context); 526 527 void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, 528 ExecutionContext &context); 529 530 AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id); 531 532 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *alloc, 533 StackFrame *frame_ptr); 534 535 void SetElementSize(Element &elem); 536 537 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, 538 const char *var_name, uint64_t &val); 539 540 void FindStructTypeName(Element &elem, StackFrame *frame_ptr); 541 542 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, 543 size_t offset, const Element &elem); 544 545 size_t CalculateElementHeaderSize(const Element &elem); 546 547 void SetConditional(lldb::BreakpointSP bp, lldb_private::Stream &messages, 548 const lldb_renderscript::RSCoordinate &coord); 549 // 550 // Helper functions for jitting the runtime 551 // 552 553 bool JITDataPointer(AllocationDetails *alloc, StackFrame *frame_ptr, 554 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0); 555 556 bool JITTypePointer(AllocationDetails *alloc, StackFrame *frame_ptr); 557 558 bool JITTypePacked(AllocationDetails *alloc, StackFrame *frame_ptr); 559 560 bool JITElementPacked(Element &elem, const lldb::addr_t context, 561 StackFrame *frame_ptr); 562 563 bool JITAllocationSize(AllocationDetails *alloc, StackFrame *frame_ptr); 564 565 bool JITSubelements(Element &elem, const lldb::addr_t context, 566 StackFrame *frame_ptr); 567 568 bool JITAllocationStride(AllocationDetails *alloc, StackFrame *frame_ptr); 569 570 // Search for a script detail object using a target address. 571 // If a script does not currently exist this function will return nullptr. 572 // If 'create' is true and there is no previous script with this address, 573 // then a new Script detail object will be created for this address and 574 // returned. 575 ScriptDetails *LookUpScript(lldb::addr_t address, bool create); 576 577 // Search for a previously saved allocation detail object using a target 578 // address. 579 // If an allocation does not exist for this address then nullptr will be 580 // returned. 581 AllocationDetails *LookUpAllocation(lldb::addr_t address); 582 583 // Creates a new allocation with the specified address assigning a new ID and 584 // removes 585 // any previous stored allocation which has the same address. 586 AllocationDetails *CreateAllocation(lldb::addr_t address); 587 588 bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override; 589 }; 590 591 } // namespace lldb_private 592 593 #endif // LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_RENDERSCRIPT_RENDERSCRIPTRUNTIME_RENDERSCRIPTRUNTIME_H 594