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