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 // C Includes 14 // C++ Includes 15 #include <array> 16 #include <map> 17 #include <memory> 18 #include <string> 19 #include <vector> 20 21 // Other libraries and framework includes 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringRef.h" 24 // Project includes 25 #include "lldb/Core/Module.h" 26 #include "lldb/Expression/LLVMUserExpression.h" 27 #include "lldb/Target/CPPLanguageRuntime.h" 28 #include "lldb/Target/LanguageRuntime.h" 29 #include "lldb/lldb-private.h" 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 49 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: 61 RSBreakpointResolver(Breakpoint *bp, ConstString name) 62 : BreakpointResolver(bp, BreakpointResolver::NameResolver), 63 m_kernel_name(name) {} 64 65 void GetDescription(Stream *strm) override { 66 if (strm) 67 strm->Printf("RenderScript kernel breakpoint for '%s'", 68 m_kernel_name.AsCString()); 69 } 70 71 void Dump(Stream *s) const override {} 72 73 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 74 SymbolContext &context, Address *addr, 75 bool containing) override; 76 77 Searcher::Depth GetDepth() override { return Searcher::eDepthModule; } 78 79 lldb::BreakpointResolverSP 80 CopyForBreakpoint(Breakpoint &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 Breakpoint *breakpoint, ConstString reduce_name, 104 std::vector<lldb_renderscript::RSModuleDescriptorSP> *rs_modules, 105 int kernel_types = eKernelTypeAll) 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 115 void GetDescription(Stream *strm) override { 116 if (strm) 117 strm->Printf("RenderScript reduce breakpoint for '%s'", 118 m_reduce_name.AsCString()); 119 } 120 121 void Dump(Stream *s) const override {} 122 123 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 124 SymbolContext &context, Address *addr, 125 bool containing) override; 126 127 Searcher::Depth GetDepth() override { return Searcher::eDepthModule; } 128 129 lldb::BreakpointResolverSP 130 CopyForBreakpoint(Breakpoint &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: 144 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: 157 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 = ".") 172 : 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: 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: 253 RSScriptGroupBreakpointResolver(Breakpoint *bp, const ConstString &name, 254 const RSScriptGroupList &groups, 255 bool stop_on_all) 256 : BreakpointResolver(bp, BreakpointResolver::NameResolver), 257 m_group_name(name), m_script_groups(groups), 258 m_stop_on_all(stop_on_all) {} 259 260 void GetDescription(Stream *strm) override { 261 if (strm) 262 strm->Printf("RenderScript ScriptGroup breakpoint for '%s'", 263 m_group_name.AsCString()); 264 } 265 266 void Dump(Stream *s) const override {} 267 268 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 269 SymbolContext &context, Address *addr, 270 bool containing) override; 271 272 Searcher::Depth GetDepth() override { return Searcher::eDepthModule; } 273 274 lldb::BreakpointResolverSP 275 CopyForBreakpoint(Breakpoint &breakpoint) override { 276 lldb::BreakpointResolverSP ret_sp(new RSScriptGroupBreakpointResolver( 277 &breakpoint, m_group_name, m_script_groups, m_stop_on_all)); 278 return ret_sp; 279 } 280 281 protected: 282 const RSScriptGroupDescriptorSP 283 FindScriptGroup(const ConstString &name) const { 284 for (auto sg : m_script_groups) { 285 if (ConstString::Compare(sg->m_name, name) == 0) 286 return sg; 287 } 288 return RSScriptGroupDescriptorSP(); 289 } 290 291 ConstString m_group_name; 292 const RSScriptGroupList &m_script_groups; 293 bool m_stop_on_all; 294 }; 295 } // namespace lldb_renderscript 296 297 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime { 298 public: 299 enum ModuleKind { 300 eModuleKindIgnored, 301 eModuleKindLibRS, 302 eModuleKindDriver, 303 eModuleKindImpl, 304 eModuleKindKernelObj 305 }; 306 307 ~RenderScriptRuntime() override; 308 309 //------------------------------------------------------------------ 310 // Static Functions 311 //------------------------------------------------------------------ 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 322 static lldb_private::ConstString GetPluginNameStatic(); 323 324 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 325 326 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 327 328 static void ModulesDidLoad(const lldb::ProcessSP &process_sp, 329 const ModuleList &module_list); 330 331 bool IsVTableName(const char *name) override; 332 333 bool GetDynamicTypeAndAddress(ValueObject &in_value, 334 lldb::DynamicValueType use_dynamic, 335 TypeAndOrName &class_type_or_name, 336 Address &address, 337 Value::ValueType &value_type) override; 338 339 TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, 340 ValueObject &static_value) override; 341 342 bool CouldHaveDynamicValue(ValueObject &in_value) override; 343 344 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bp, 345 bool catch_bp, 346 bool throw_bp) override; 347 348 bool LoadModule(const lldb::ModuleSP &module_sp); 349 350 void DumpModules(Stream &strm) const; 351 352 void DumpContexts(Stream &strm) const; 353 354 void DumpKernels(Stream &strm) const; 355 356 bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id); 357 358 void ListAllocations(Stream &strm, StackFrame *frame_ptr, 359 const uint32_t index); 360 361 bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr); 362 363 bool PlaceBreakpointOnKernel( 364 lldb::TargetSP target, Stream &messages, const char *name, 365 const lldb_renderscript::RSCoordinate *coords = nullptr); 366 367 bool PlaceBreakpointOnReduction( 368 lldb::TargetSP target, Stream &messages, const char *reduce_name, 369 const lldb_renderscript::RSCoordinate *coords = nullptr, 370 int kernel_types = ~(0)); 371 372 bool PlaceBreakpointOnScriptGroup(lldb::TargetSP target, Stream &strm, 373 const ConstString &name, bool stop_on_all); 374 375 void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 376 377 void DumpStatus(Stream &strm) const; 378 379 void ModulesDidLoad(const ModuleList &module_list) override; 380 381 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, 382 const char *filename, StackFrame *frame_ptr); 383 384 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, 385 const char *filename, StackFrame *frame_ptr); 386 387 void Update(); 388 389 void Initiate(); 390 391 const lldb_renderscript::RSScriptGroupList &GetScriptGroups() const { 392 return m_scriptGroups; 393 }; 394 395 bool IsKnownKernel(const ConstString &name) { 396 for (const auto &module : m_rsmodules) 397 for (const auto &kernel : module->m_kernels) 398 if (kernel.m_name == name) 399 return true; 400 return false; 401 } 402 403 //------------------------------------------------------------------ 404 // PluginInterface protocol 405 //------------------------------------------------------------------ 406 lldb_private::ConstString GetPluginName() override; 407 408 uint32_t GetPluginVersion() override; 409 410 static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, 411 Thread *thread_ptr); 412 413 bool ResolveKernelName(lldb::addr_t kernel_address, ConstString &name); 414 415 protected: 416 struct ScriptDetails; 417 struct AllocationDetails; 418 struct Element; 419 420 lldb_renderscript::RSScriptGroupList m_scriptGroups; 421 422 void InitSearchFilter(lldb::TargetSP target) { 423 if (!m_filtersp) 424 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target)); 425 } 426 427 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 428 429 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind); 430 431 bool RefreshAllocation(AllocationDetails *alloc, StackFrame *frame_ptr); 432 433 bool EvalRSExpression(const char *expression, StackFrame *frame_ptr, 434 uint64_t *result); 435 436 lldb::BreakpointSP CreateScriptGroupBreakpoint(const ConstString &name, 437 bool multi); 438 439 lldb::BreakpointSP CreateKernelBreakpoint(const ConstString &name); 440 441 lldb::BreakpointSP CreateReductionBreakpoint(const ConstString &name, 442 int kernel_types); 443 444 void BreakOnModuleKernels( 445 const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 446 447 struct RuntimeHook; 448 typedef void (RenderScriptRuntime::*CaptureStateFn)( 449 RuntimeHook *hook_info, 450 ExecutionContext &context); // Please do this! 451 452 struct HookDefn { 453 const char *name; 454 const char *symbol_name_m32; // mangled name for the 32 bit architectures 455 const char *symbol_name_m64; // mangled name for the 64 bit archs 456 uint32_t version; 457 ModuleKind kind; 458 CaptureStateFn grabber; 459 }; 460 461 struct RuntimeHook { 462 lldb::addr_t address; 463 const HookDefn *defn; 464 lldb::BreakpointSP bp_sp; 465 }; 466 467 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP; 468 469 lldb::ModuleSP m_libRS; 470 lldb::ModuleSP m_libRSDriver; 471 lldb::ModuleSP m_libRSCpuRef; 472 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules; 473 474 std::vector<std::unique_ptr<ScriptDetails>> m_scripts; 475 std::vector<std::unique_ptr<AllocationDetails>> m_allocations; 476 477 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> 478 m_scriptMappings; 479 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks; 480 std::map<lldb::user_id_t, std::unique_ptr<lldb_renderscript::RSCoordinate>> 481 m_conditional_breaks; 482 483 lldb::SearchFilterSP 484 m_filtersp; // Needed to create breakpoints through Target API 485 486 bool m_initiated; 487 bool m_debuggerPresentFlagged; 488 bool m_breakAllKernels; 489 static const HookDefn s_runtimeHookDefns[]; 490 static const size_t s_runtimeHookCount; 491 LLVMUserExpression::IRPasses *m_ir_passes; 492 493 private: 494 RenderScriptRuntime(Process *process); // Call CreateInstance instead. 495 496 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, 497 lldb::user_id_t break_id, 498 lldb::user_id_t break_loc_id); 499 500 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 501 lldb::user_id_t break_id, 502 lldb::user_id_t break_loc_id); 503 504 void HookCallback(RuntimeHook *hook_info, ExecutionContext &context); 505 506 // Callback function when 'debugHintScriptGroup2' executes on the target. 507 void CaptureDebugHintScriptGroup2(RuntimeHook *hook_info, 508 ExecutionContext &context); 509 510 void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context); 511 512 void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context); 513 514 void CaptureAllocationDestroy(RuntimeHook *hook_info, 515 ExecutionContext &context); 516 517 void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context); 518 519 void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, 520 ExecutionContext &context); 521 522 AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id); 523 524 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *alloc, 525 StackFrame *frame_ptr); 526 527 void SetElementSize(Element &elem); 528 529 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, 530 const char *var_name, uint64_t &val); 531 532 void FindStructTypeName(Element &elem, StackFrame *frame_ptr); 533 534 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, 535 size_t offset, const Element &elem); 536 537 size_t CalculateElementHeaderSize(const Element &elem); 538 539 void SetConditional(lldb::BreakpointSP bp, lldb_private::Stream &messages, 540 const lldb_renderscript::RSCoordinate &coord); 541 // 542 // Helper functions for jitting the runtime 543 // 544 545 bool JITDataPointer(AllocationDetails *alloc, StackFrame *frame_ptr, 546 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0); 547 548 bool JITTypePointer(AllocationDetails *alloc, StackFrame *frame_ptr); 549 550 bool JITTypePacked(AllocationDetails *alloc, StackFrame *frame_ptr); 551 552 bool JITElementPacked(Element &elem, const lldb::addr_t context, 553 StackFrame *frame_ptr); 554 555 bool JITAllocationSize(AllocationDetails *alloc, StackFrame *frame_ptr); 556 557 bool JITSubelements(Element &elem, const lldb::addr_t context, 558 StackFrame *frame_ptr); 559 560 bool JITAllocationStride(AllocationDetails *alloc, StackFrame *frame_ptr); 561 562 // Search for a script detail object using a target address. 563 // If a script does not currently exist this function will return nullptr. 564 // If 'create' is true and there is no previous script with this address, 565 // then a new Script detail object will be created for this address and 566 // returned. 567 ScriptDetails *LookUpScript(lldb::addr_t address, bool create); 568 569 // Search for a previously saved allocation detail object using a target 570 // address. 571 // If an allocation does not exist for this address then nullptr will be 572 // returned. 573 AllocationDetails *LookUpAllocation(lldb::addr_t address); 574 575 // Creates a new allocation with the specified address assigning a new ID and 576 // removes 577 // any previous stored allocation which has the same address. 578 AllocationDetails *CreateAllocation(lldb::addr_t address); 579 580 bool GetOverrideExprOptions(clang::TargetOptions &prototype) override; 581 582 bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override; 583 }; 584 585 } // namespace lldb_private 586 587 #endif // liblldb_RenderScriptRuntime_h_ 588