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