1 //===-- RenderScriptRuntime.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_RenderScriptRuntime_h_
11 #define liblldb_RenderScriptRuntime_h_
21 // Other libraries and framework 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"
28 namespace lldb_private
30 namespace lldb_renderscript
33 typedef uint32_t RSSlot;
34 class RSModuleDescriptor;
35 struct RSGlobalDescriptor;
36 struct RSKernelDescriptor;
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;
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
49 RSBreakpointResolver(Breakpoint *bkpt, ConstString name)
50 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver), m_kernel_name(name)
55 GetDescription(Stream *strm) override
58 strm->Printf("RenderScript kernel breakpoint for '%s'", m_kernel_name.AsCString());
62 Dump(Stream *s) const override
66 Searcher::CallbackReturn
67 SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr, bool containing) override;
72 return Searcher::eDepthModule;
75 lldb::BreakpointResolverSP
76 CopyForBreakpoint(Breakpoint &breakpoint) override
78 lldb::BreakpointResolverSP ret_sp(new RSBreakpointResolver(&breakpoint, m_kernel_name));
83 ConstString m_kernel_name;
86 struct RSKernelDescriptor
89 RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, uint32_t slot)
90 : m_module(module), m_name(name), m_slot(slot)
95 Dump(Stream &strm) const;
97 const RSModuleDescriptor *m_module;
102 struct RSGlobalDescriptor
105 RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name) : m_module(module), m_name(name) {}
108 Dump(Stream &strm) const;
110 const RSModuleDescriptor *m_module;
114 class RSModuleDescriptor
117 RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {}
119 ~RSModuleDescriptor() = default;
125 Dump(Stream &strm) const;
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;
134 } // namespace lldb_renderscript
136 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime
148 ~RenderScriptRuntime() override;
150 //------------------------------------------------------------------
152 //------------------------------------------------------------------
159 static lldb_private::LanguageRuntime *
160 CreateInstance(Process *process, lldb::LanguageType language);
162 static lldb::CommandObjectSP
163 GetCommandObject(CommandInterpreter &interpreter);
165 static lldb_private::ConstString
166 GetPluginNameStatic();
169 IsRenderScriptModule(const lldb::ModuleSP &module_sp);
172 GetModuleKind(const lldb::ModuleSP &module_sp);
175 ModulesDidLoad(const lldb::ProcessSP &process_sp, const ModuleList &module_list);
178 IsVTableName(const char *name) override;
181 GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic,
182 TypeAndOrName &class_type_or_name, Address &address,
183 Value::ValueType &value_type) override;
186 FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value) override;
189 CouldHaveDynamicValue(ValueObject &in_value) override;
191 lldb::BreakpointResolverSP
192 CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) override;
195 LoadModule(const lldb::ModuleSP &module_sp);
198 DumpModules(Stream &strm) const;
201 DumpContexts(Stream &strm) const;
204 DumpKernels(Stream &strm) const;
207 DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id);
210 ListAllocations(Stream &strm, StackFrame *frame_ptr, const uint32_t index);
213 RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr);
216 PlaceBreakpointOnKernel(Stream &strm, const char *name, const std::array<int, 3> coords, Error &error,
217 lldb::TargetSP target);
220 SetBreakAllKernels(bool do_break, lldb::TargetSP target);
223 Status(Stream &strm) const;
226 ModulesDidLoad(const ModuleList &module_list) override;
229 LoadAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr);
232 SaveAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr);
240 //------------------------------------------------------------------
241 // PluginInterface protocol
242 //------------------------------------------------------------------
243 lldb_private::ConstString
244 GetPluginName() override;
247 GetPluginVersion() override;
250 GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord, Thread *thread_ptr);
253 struct ScriptDetails;
254 struct AllocationDetails;
258 InitSearchFilter(lldb::TargetSP target)
261 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
265 FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
268 LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
271 RefreshAllocation(AllocationDetails *allocation, StackFrame *frame_ptr);
274 EvalRSExpression(const char *expression, StackFrame *frame_ptr, uint64_t *result);
277 CreateKernelBreakpoint(const ConstString &name);
280 BreakOnModuleKernels(const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
283 typedef void (RenderScriptRuntime::*CaptureStateFn)(RuntimeHook *hook_info,
284 ExecutionContext &context); // Please do this!
289 const char *symbol_name_m32; // mangled name for the 32 bit architectures
290 const char *symbol_name_m64; // mangled name for the 64 bit archs
293 CaptureStateFn grabber;
298 lldb::addr_t address;
299 const HookDefn *defn;
300 lldb::BreakpointSP bp_sp;
303 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
305 lldb::ModuleSP m_libRS;
306 lldb::ModuleSP m_libRSDriver;
307 lldb::ModuleSP m_libRSCpuRef;
308 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
310 std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
311 std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
313 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> m_scriptMappings;
314 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
315 std::map<lldb::user_id_t, std::shared_ptr<uint32_t>> m_conditional_breaks;
317 lldb::SearchFilterSP m_filtersp; // Needed to create breakpoints through Target API
320 bool m_debuggerPresentFlagged;
321 bool m_breakAllKernels;
322 static const HookDefn s_runtimeHookDefns[];
323 static const size_t s_runtimeHookCount;
326 RenderScriptRuntime(Process *process); // Call CreateInstance instead.
329 HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
332 KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
333 lldb::user_id_t break_loc_id);
336 HookCallback(RuntimeHook *hook_info, ExecutionContext &context);
339 CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context);
342 CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context);
345 CaptureAllocationDestroy(RuntimeHook *hook_info, ExecutionContext &context);
348 CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context);
351 CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info, ExecutionContext &context);
354 FindAllocByID(Stream &strm, const uint32_t alloc_id);
356 std::shared_ptr<uint8_t>
357 GetAllocationData(AllocationDetails *allocation, StackFrame *frame_ptr);
360 SetElementSize(Element &elem);
363 GetFrameVarAsUnsigned(const lldb::StackFrameSP, const char *var_name, uint64_t &val);
366 FindStructTypeName(Element &elem, StackFrame *frame_ptr);
369 PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, size_t offset, const Element &elem);
372 CalculateElementHeaderSize(const Element &elem);
375 // Helper functions for jitting the runtime
379 JITDataPointer(AllocationDetails *allocation, StackFrame *frame_ptr,
380 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0);
383 JITTypePointer(AllocationDetails *allocation, StackFrame *frame_ptr);
386 JITTypePacked(AllocationDetails *allocation, StackFrame *frame_ptr);
389 JITElementPacked(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr);
392 JITAllocationSize(AllocationDetails *allocation, StackFrame *frame_ptr);
395 JITSubelements(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr);
398 JITAllocationStride(AllocationDetails *allocation, StackFrame *frame_ptr);
400 // Search for a script detail object using a target address.
401 // If a script does not currently exist this function will return nullptr.
402 // If 'create' is true and there is no previous script with this address,
403 // then a new Script detail object will be created for this address and returned.
405 LookUpScript(lldb::addr_t address, bool create);
407 // Search for a previously saved allocation detail object using a target address.
408 // If an allocation does not exist for this address then nullptr will be returned.
409 // If 'create' is true and there is no previous allocation then a new allocation
410 // detail object will be created for this address and returned.
412 LookUpAllocation(lldb::addr_t address, bool create);
415 } // namespace lldb_private
417 #endif // liblldb_RenderScriptRuntime_h_