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