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, const 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(const 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 //------------------------------------------------------------------ 305 // Static Functions 306 //------------------------------------------------------------------ 307 static void Initialize(); 308 309 static void Terminate(); 310 311 static lldb_private::LanguageRuntime * 312 CreateInstance(Process *process, lldb::LanguageType language); 313 314 static lldb::CommandObjectSP 315 GetCommandObject(CommandInterpreter &interpreter); 316 317 static lldb_private::ConstString GetPluginNameStatic(); 318 319 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 320 321 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 322 323 static void ModulesDidLoad(const lldb::ProcessSP &process_sp, 324 const ModuleList &module_list); 325 326 bool IsVTableName(const char *name) override; 327 328 bool GetDynamicTypeAndAddress(ValueObject &in_value, 329 lldb::DynamicValueType use_dynamic, 330 TypeAndOrName &class_type_or_name, 331 Address &address, 332 Value::ValueType &value_type) override; 333 334 TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, 335 ValueObject &static_value) override; 336 337 bool CouldHaveDynamicValue(ValueObject &in_value) override; 338 339 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bp, 340 bool catch_bp, 341 bool throw_bp) override; 342 343 bool LoadModule(const lldb::ModuleSP &module_sp); 344 345 void DumpModules(Stream &strm) const; 346 347 void DumpContexts(Stream &strm) const; 348 349 void DumpKernels(Stream &strm) const; 350 351 bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id); 352 353 void ListAllocations(Stream &strm, StackFrame *frame_ptr, 354 const uint32_t index); 355 356 bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr); 357 358 bool PlaceBreakpointOnKernel( 359 lldb::TargetSP target, Stream &messages, const char *name, 360 const lldb_renderscript::RSCoordinate *coords = nullptr); 361 362 bool PlaceBreakpointOnReduction( 363 lldb::TargetSP target, Stream &messages, const char *reduce_name, 364 const lldb_renderscript::RSCoordinate *coords = nullptr, 365 int kernel_types = ~(0)); 366 367 bool PlaceBreakpointOnScriptGroup(lldb::TargetSP target, Stream &strm, 368 const ConstString &name, bool stop_on_all); 369 370 void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 371 372 void DumpStatus(Stream &strm) const; 373 374 void ModulesDidLoad(const ModuleList &module_list) override; 375 376 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, 377 const char *filename, StackFrame *frame_ptr); 378 379 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, 380 const char *filename, StackFrame *frame_ptr); 381 382 void Update(); 383 384 void Initiate(); 385 386 const lldb_renderscript::RSScriptGroupList &GetScriptGroups() const { 387 return m_scriptGroups; 388 }; 389 390 bool IsKnownKernel(const ConstString &name) { 391 for (const auto &module : m_rsmodules) 392 for (const auto &kernel : module->m_kernels) 393 if (kernel.m_name == name) 394 return true; 395 return false; 396 } 397 398 //------------------------------------------------------------------ 399 // PluginInterface protocol 400 //------------------------------------------------------------------ 401 lldb_private::ConstString GetPluginName() override; 402 403 uint32_t GetPluginVersion() override; 404 405 static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, 406 Thread *thread_ptr); 407 408 bool ResolveKernelName(lldb::addr_t kernel_address, ConstString &name); 409 410 protected: 411 struct ScriptDetails; 412 struct AllocationDetails; 413 struct Element; 414 415 lldb_renderscript::RSScriptGroupList m_scriptGroups; 416 417 void InitSearchFilter(lldb::TargetSP target) { 418 if (!m_filtersp) 419 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target)); 420 } 421 422 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 423 424 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind); 425 426 bool RefreshAllocation(AllocationDetails *alloc, StackFrame *frame_ptr); 427 428 bool EvalRSExpression(const char *expression, StackFrame *frame_ptr, 429 uint64_t *result); 430 431 lldb::BreakpointSP CreateScriptGroupBreakpoint(const ConstString &name, 432 bool multi); 433 434 lldb::BreakpointSP CreateKernelBreakpoint(const ConstString &name); 435 436 lldb::BreakpointSP CreateReductionBreakpoint(const ConstString &name, 437 int kernel_types); 438 439 void BreakOnModuleKernels( 440 const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 441 442 struct RuntimeHook; 443 typedef void (RenderScriptRuntime::*CaptureStateFn)( 444 RuntimeHook *hook_info, 445 ExecutionContext &context); // Please do this! 446 447 struct HookDefn { 448 const char *name; 449 const char *symbol_name_m32; // mangled name for the 32 bit architectures 450 const char *symbol_name_m64; // mangled name for the 64 bit archs 451 uint32_t version; 452 ModuleKind kind; 453 CaptureStateFn grabber; 454 }; 455 456 struct RuntimeHook { 457 lldb::addr_t address; 458 const HookDefn *defn; 459 lldb::BreakpointSP bp_sp; 460 }; 461 462 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP; 463 464 lldb::ModuleSP m_libRS; 465 lldb::ModuleSP m_libRSDriver; 466 lldb::ModuleSP m_libRSCpuRef; 467 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules; 468 469 std::vector<std::unique_ptr<ScriptDetails>> m_scripts; 470 std::vector<std::unique_ptr<AllocationDetails>> m_allocations; 471 472 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> 473 m_scriptMappings; 474 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks; 475 std::map<lldb::user_id_t, std::unique_ptr<lldb_renderscript::RSCoordinate>> 476 m_conditional_breaks; 477 478 lldb::SearchFilterSP 479 m_filtersp; // Needed to create breakpoints through Target API 480 481 bool m_initiated; 482 bool m_debuggerPresentFlagged; 483 bool m_breakAllKernels; 484 static const HookDefn s_runtimeHookDefns[]; 485 static const size_t s_runtimeHookCount; 486 LLVMUserExpression::IRPasses *m_ir_passes; 487 488 private: 489 RenderScriptRuntime(Process *process); // Call CreateInstance instead. 490 491 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, 492 lldb::user_id_t break_id, 493 lldb::user_id_t break_loc_id); 494 495 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 496 lldb::user_id_t break_id, 497 lldb::user_id_t break_loc_id); 498 499 void HookCallback(RuntimeHook *hook_info, ExecutionContext &context); 500 501 // Callback function when 'debugHintScriptGroup2' executes on the target. 502 void CaptureDebugHintScriptGroup2(RuntimeHook *hook_info, 503 ExecutionContext &context); 504 505 void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context); 506 507 void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context); 508 509 void CaptureAllocationDestroy(RuntimeHook *hook_info, 510 ExecutionContext &context); 511 512 void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context); 513 514 void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, 515 ExecutionContext &context); 516 517 AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id); 518 519 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *alloc, 520 StackFrame *frame_ptr); 521 522 void SetElementSize(Element &elem); 523 524 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, 525 const char *var_name, uint64_t &val); 526 527 void FindStructTypeName(Element &elem, StackFrame *frame_ptr); 528 529 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, 530 size_t offset, const Element &elem); 531 532 size_t CalculateElementHeaderSize(const Element &elem); 533 534 void SetConditional(lldb::BreakpointSP bp, lldb_private::Stream &messages, 535 const lldb_renderscript::RSCoordinate &coord); 536 // 537 // Helper functions for jitting the runtime 538 // 539 540 bool JITDataPointer(AllocationDetails *alloc, StackFrame *frame_ptr, 541 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0); 542 543 bool JITTypePointer(AllocationDetails *alloc, StackFrame *frame_ptr); 544 545 bool JITTypePacked(AllocationDetails *alloc, StackFrame *frame_ptr); 546 547 bool JITElementPacked(Element &elem, const lldb::addr_t context, 548 StackFrame *frame_ptr); 549 550 bool JITAllocationSize(AllocationDetails *alloc, StackFrame *frame_ptr); 551 552 bool JITSubelements(Element &elem, const lldb::addr_t context, 553 StackFrame *frame_ptr); 554 555 bool JITAllocationStride(AllocationDetails *alloc, StackFrame *frame_ptr); 556 557 // Search for a script detail object using a target address. 558 // If a script does not currently exist this function will return nullptr. 559 // If 'create' is true and there is no previous script with this address, 560 // then a new Script detail object will be created for this address and 561 // returned. 562 ScriptDetails *LookUpScript(lldb::addr_t address, bool create); 563 564 // Search for a previously saved allocation detail object using a target 565 // address. 566 // If an allocation does not exist for this address then nullptr will be 567 // returned. 568 AllocationDetails *LookUpAllocation(lldb::addr_t address); 569 570 // Creates a new allocation with the specified address assigning a new ID and 571 // removes 572 // any previous stored allocation which has the same address. 573 AllocationDetails *CreateAllocation(lldb::addr_t address); 574 575 bool GetOverrideExprOptions(clang::TargetOptions &prototype) override; 576 577 bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override; 578 }; 579 580 } // namespace lldb_private 581 582 #endif // liblldb_RenderScriptRuntime_h_ 583