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 40 typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP; 41 typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP; 42 typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP; 43 typedef std::array<uint32_t, 3> RSCoordinate; 44 45 // Breakpoint Resolvers decide where a breakpoint is placed, 46 // so having our own allows us to limit the search scope to RS kernel modules. 47 // As well as check for .expand kernels as a fallback. 48 class RSBreakpointResolver : public BreakpointResolver { 49 public: 50 RSBreakpointResolver(Breakpoint *bkpt, ConstString name) 51 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver), 52 m_kernel_name(name) {} 53 54 void GetDescription(Stream *strm) override { 55 if (strm) 56 strm->Printf("RenderScript kernel breakpoint for '%s'", 57 m_kernel_name.AsCString()); 58 } 59 60 void Dump(Stream *s) const override {} 61 62 Searcher::CallbackReturn SearchCallback(SearchFilter &filter, 63 SymbolContext &context, Address *addr, 64 bool containing) override; 65 66 Searcher::Depth GetDepth() override { return Searcher::eDepthModule; } 67 68 lldb::BreakpointResolverSP 69 CopyForBreakpoint(Breakpoint &breakpoint) override { 70 lldb::BreakpointResolverSP ret_sp( 71 new RSBreakpointResolver(&breakpoint, m_kernel_name)); 72 return ret_sp; 73 } 74 75 protected: 76 ConstString m_kernel_name; 77 }; 78 79 struct RSKernelDescriptor { 80 public: 81 RSKernelDescriptor(const RSModuleDescriptor *module, llvm::StringRef name, 82 uint32_t slot) 83 : m_module(module), m_name(name), m_slot(slot) {} 84 85 void Dump(Stream &strm) const; 86 87 const RSModuleDescriptor *m_module; 88 ConstString m_name; 89 RSSlot m_slot; 90 }; 91 92 struct RSGlobalDescriptor { 93 public: 94 RSGlobalDescriptor(const RSModuleDescriptor *module, llvm::StringRef name) 95 : m_module(module), m_name(name) {} 96 97 void Dump(Stream &strm) const; 98 99 const RSModuleDescriptor *m_module; 100 ConstString m_name; 101 }; 102 103 struct RSReductionDescriptor { 104 RSReductionDescriptor(const RSModuleDescriptor *module, uint32_t sig, 105 uint32_t accum_data_size, llvm::StringRef name, 106 llvm::StringRef init_name, llvm::StringRef accum_name, 107 llvm::StringRef comb_name, llvm::StringRef outc_name, 108 llvm::StringRef halter_name = ".") 109 : m_module(module), m_reduce_name(name), m_init_name(init_name), 110 m_accum_name(accum_name), m_comb_name(comb_name), 111 m_outc_name(outc_name), m_halter_name(halter_name) { 112 // TODO Check whether the combiner is an autogenerated name, and track 113 // this 114 } 115 116 void Dump(Stream &strm) const; 117 118 const RSModuleDescriptor *m_module; 119 ConstString m_reduce_name; // This is the name given to the general reduction 120 // as a group as passed to pragma 121 // reduce(m_reduce_name). There is no kernel function with this name 122 ConstString m_init_name; // The name of the initializer name. "." if no 123 // initializer given 124 ConstString m_accum_name; // The accumulator function name. "." if not given 125 ConstString m_comb_name; // The name of the combiner function. If this was not 126 // given, a name is generated by the 127 // compiler. TODO 128 ConstString m_outc_name; // The name of the outconverter 129 130 ConstString m_halter_name; // The name of the halter function. XXX This is not 131 // yet specified by the RenderScript 132 // compiler or runtime, and its semantics and existence is still under 133 // discussion by the 134 // RenderScript Contributors 135 RSSlot m_accum_sig; // metatdata signature for this reduction (bitwise mask of 136 // type information (see 137 // libbcc/include/bcinfo/MetadataExtractor.h 138 uint32_t m_accum_data_size; // Data size of the accumulator function input 139 bool m_comb_name_generated; // Was the combiner name generated by the compiler 140 }; 141 142 class RSModuleDescriptor { 143 bool ParseExportForeachCount(llvm::StringRef *, size_t n_lines); 144 145 bool ParseExportVarCount(llvm::StringRef *, size_t n_lines); 146 147 bool ParseExportReduceCount(llvm::StringRef *, size_t n_lines); 148 149 bool ParseBuildChecksum(llvm::StringRef *, size_t n_lines); 150 151 bool ParsePragmaCount(llvm::StringRef *, size_t n_lines); 152 153 public: 154 RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {} 155 156 ~RSModuleDescriptor() = default; 157 158 bool ParseRSInfo(); 159 160 void Dump(Stream &strm) const; 161 162 const lldb::ModuleSP m_module; 163 std::vector<RSKernelDescriptor> m_kernels; 164 std::vector<RSGlobalDescriptor> m_globals; 165 std::vector<RSReductionDescriptor> m_reductions; 166 std::map<std::string, std::string> m_pragmas; 167 std::string m_resname; 168 }; 169 170 } // namespace lldb_renderscript 171 172 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime { 173 public: 174 enum ModuleKind { 175 eModuleKindIgnored, 176 eModuleKindLibRS, 177 eModuleKindDriver, 178 eModuleKindImpl, 179 eModuleKindKernelObj 180 }; 181 182 ~RenderScriptRuntime() override; 183 184 //------------------------------------------------------------------ 185 // Static Functions 186 //------------------------------------------------------------------ 187 static void Initialize(); 188 189 static void Terminate(); 190 191 static lldb_private::LanguageRuntime * 192 CreateInstance(Process *process, lldb::LanguageType language); 193 194 static lldb::CommandObjectSP 195 GetCommandObject(CommandInterpreter &interpreter); 196 197 static lldb_private::ConstString GetPluginNameStatic(); 198 199 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp); 200 201 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp); 202 203 static void ModulesDidLoad(const lldb::ProcessSP &process_sp, 204 const ModuleList &module_list); 205 206 bool IsVTableName(const char *name) override; 207 208 bool GetDynamicTypeAndAddress(ValueObject &in_value, 209 lldb::DynamicValueType use_dynamic, 210 TypeAndOrName &class_type_or_name, 211 Address &address, 212 Value::ValueType &value_type) override; 213 214 TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, 215 ValueObject &static_value) override; 216 217 bool CouldHaveDynamicValue(ValueObject &in_value) override; 218 219 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, 220 bool catch_bp, 221 bool throw_bp) override; 222 223 bool LoadModule(const lldb::ModuleSP &module_sp); 224 225 void DumpModules(Stream &strm) const; 226 227 void DumpContexts(Stream &strm) const; 228 229 void DumpKernels(Stream &strm) const; 230 231 bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id); 232 233 void ListAllocations(Stream &strm, StackFrame *frame_ptr, 234 const uint32_t index); 235 236 bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr); 237 238 void PlaceBreakpointOnKernel(Stream &strm, const char *name, 239 const std::array<int, 3> coords, Error &error, 240 lldb::TargetSP target); 241 242 void SetBreakAllKernels(bool do_break, lldb::TargetSP target); 243 244 void Status(Stream &strm) const; 245 246 void ModulesDidLoad(const ModuleList &module_list) override; 247 248 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, 249 const char *filename, StackFrame *frame_ptr); 250 251 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, 252 const char *filename, StackFrame *frame_ptr); 253 254 void Update(); 255 256 void Initiate(); 257 258 //------------------------------------------------------------------ 259 // PluginInterface protocol 260 //------------------------------------------------------------------ 261 lldb_private::ConstString GetPluginName() override; 262 263 uint32_t GetPluginVersion() override; 264 265 static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, 266 Thread *thread_ptr); 267 268 protected: 269 struct ScriptDetails; 270 struct AllocationDetails; 271 struct Element; 272 273 void InitSearchFilter(lldb::TargetSP target) { 274 if (!m_filtersp) 275 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target)); 276 } 277 278 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 279 280 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind); 281 282 bool RefreshAllocation(AllocationDetails *allocation, StackFrame *frame_ptr); 283 284 bool EvalRSExpression(const char *expression, StackFrame *frame_ptr, 285 uint64_t *result); 286 287 lldb::BreakpointSP CreateKernelBreakpoint(const ConstString &name); 288 289 void BreakOnModuleKernels( 290 const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp); 291 292 struct RuntimeHook; 293 typedef void (RenderScriptRuntime::*CaptureStateFn)( 294 RuntimeHook *hook_info, 295 ExecutionContext &context); // Please do this! 296 297 struct HookDefn { 298 const char *name; 299 const char *symbol_name_m32; // mangled name for the 32 bit architectures 300 const char *symbol_name_m64; // mangled name for the 64 bit archs 301 uint32_t version; 302 ModuleKind kind; 303 CaptureStateFn grabber; 304 }; 305 306 struct RuntimeHook { 307 lldb::addr_t address; 308 const HookDefn *defn; 309 lldb::BreakpointSP bp_sp; 310 }; 311 312 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP; 313 314 lldb::ModuleSP m_libRS; 315 lldb::ModuleSP m_libRSDriver; 316 lldb::ModuleSP m_libRSCpuRef; 317 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules; 318 319 std::vector<std::unique_ptr<ScriptDetails>> m_scripts; 320 std::vector<std::unique_ptr<AllocationDetails>> m_allocations; 321 322 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> 323 m_scriptMappings; 324 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks; 325 std::map<lldb::user_id_t, std::shared_ptr<uint32_t>> m_conditional_breaks; 326 327 lldb::SearchFilterSP 328 m_filtersp; // Needed to create breakpoints through Target API 329 330 bool m_initiated; 331 bool m_debuggerPresentFlagged; 332 bool m_breakAllKernels; 333 static const HookDefn s_runtimeHookDefns[]; 334 static const size_t s_runtimeHookCount; 335 LLVMUserExpression::IRPasses *m_ir_passes; 336 337 private: 338 RenderScriptRuntime(Process *process); // Call CreateInstance instead. 339 340 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, 341 lldb::user_id_t break_id, 342 lldb::user_id_t break_loc_id); 343 344 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, 345 lldb::user_id_t break_id, 346 lldb::user_id_t break_loc_id); 347 348 void HookCallback(RuntimeHook *hook_info, ExecutionContext &context); 349 350 void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context); 351 352 void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context); 353 354 void CaptureAllocationDestroy(RuntimeHook *hook_info, 355 ExecutionContext &context); 356 357 void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context); 358 359 void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, 360 ExecutionContext &context); 361 362 AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id); 363 364 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *allocation, 365 StackFrame *frame_ptr); 366 367 void SetElementSize(Element &elem); 368 369 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, 370 const char *var_name, uint64_t &val); 371 372 void FindStructTypeName(Element &elem, StackFrame *frame_ptr); 373 374 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, 375 size_t offset, const Element &elem); 376 377 size_t CalculateElementHeaderSize(const Element &elem); 378 379 // 380 // Helper functions for jitting the runtime 381 // 382 383 bool JITDataPointer(AllocationDetails *allocation, StackFrame *frame_ptr, 384 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0); 385 386 bool JITTypePointer(AllocationDetails *allocation, StackFrame *frame_ptr); 387 388 bool JITTypePacked(AllocationDetails *allocation, StackFrame *frame_ptr); 389 390 bool JITElementPacked(Element &elem, const lldb::addr_t context, 391 StackFrame *frame_ptr); 392 393 bool JITAllocationSize(AllocationDetails *allocation, StackFrame *frame_ptr); 394 395 bool JITSubelements(Element &elem, const lldb::addr_t context, 396 StackFrame *frame_ptr); 397 398 bool JITAllocationStride(AllocationDetails *allocation, 399 StackFrame *frame_ptr); 400 401 // Search for a script detail object using a target address. 402 // If a script does not currently exist this function will return nullptr. 403 // If 'create' is true and there is no previous script with this address, 404 // then a new Script detail object will be created for this address and 405 // returned. 406 ScriptDetails *LookUpScript(lldb::addr_t address, bool create); 407 408 // Search for a previously saved allocation detail object using a target 409 // address. 410 // If an allocation does not exist for this address then nullptr will be 411 // returned. 412 AllocationDetails *LookUpAllocation(lldb::addr_t address); 413 414 // Creates a new allocation with the specified address assigning a new ID and 415 // removes 416 // any previous stored allocation which has the same address. 417 AllocationDetails *CreateAllocation(lldb::addr_t address); 418 419 bool GetOverrideExprOptions(clang::TargetOptions &prototype) override; 420 421 bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override; 422 }; 423 424 } // namespace lldb_private 425 426 #endif // liblldb_RenderScriptRuntime_h_ 427