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/lldb-private.h"
24 #include "lldb/Target/LanguageRuntime.h"
25 #include "lldb/Target/CPPLanguageRuntime.h"
26 #include "lldb/Core/Module.h"
28 namespace lldb_private {
29 namespace lldb_renderscript {
31 typedef uint32_t RSSlot;
32 class RSModuleDescriptor;
33 struct RSGlobalDescriptor;
34 struct RSKernelDescriptor;
36 typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP;
37 typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP;
38 typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP;
40 // Breakpoint Resolvers decide where a breakpoint is placed,
41 // so having our own allows us to limit the search scope to RS kernel modules.
42 // As well as check for .expand kernels as a fallback.
43 class RSBreakpointResolver : public BreakpointResolver
46 RSBreakpointResolver(Breakpoint *bkpt, ConstString name):
47 BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
53 GetDescription(Stream *strm) override
56 strm->Printf("RenderScript kernel breakpoint for '%s'", m_kernel_name.AsCString());
60 Dump(Stream *s) const override
64 Searcher::CallbackReturn
65 SearchCallback(SearchFilter &filter,
66 SymbolContext &context,
68 bool containing) override;
73 return Searcher::eDepthModule;
76 lldb::BreakpointResolverSP
77 CopyForBreakpoint(Breakpoint &breakpoint) override
79 lldb::BreakpointResolverSP ret_sp(new RSBreakpointResolver(&breakpoint, m_kernel_name));
84 ConstString m_kernel_name;
87 struct RSKernelDescriptor
90 RSKernelDescriptor(const RSModuleDescriptor *module, const char *name, uint32_t slot)
97 void Dump(Stream &strm) const;
99 const RSModuleDescriptor *m_module;
104 struct RSGlobalDescriptor
107 RSGlobalDescriptor(const RSModuleDescriptor *module, const char *name )
113 void Dump(Stream &strm) const;
115 const RSModuleDescriptor *m_module;
119 class RSModuleDescriptor
122 RSModuleDescriptor(const lldb::ModuleSP &module)
127 ~RSModuleDescriptor() = default;
131 void Dump(Stream &strm) const;
133 const lldb::ModuleSP m_module;
134 std::vector<RSKernelDescriptor> m_kernels;
135 std::vector<RSGlobalDescriptor> m_globals;
136 std::map<std::string, std::string> m_pragmas;
137 std::string m_resname;
140 } // namespace lldb_renderscript
142 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime
154 ~RenderScriptRuntime() override;
156 //------------------------------------------------------------------
158 //------------------------------------------------------------------
159 static void Initialize();
161 static void Terminate();
163 static lldb_private::LanguageRuntime *CreateInstance(Process *process, lldb::LanguageType language);
165 static lldb::CommandObjectSP GetCommandObject(CommandInterpreter& interpreter);
167 static lldb_private::ConstString GetPluginNameStatic();
169 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp);
171 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp);
173 static void ModulesDidLoad(const lldb::ProcessSP& process_sp, const ModuleList &module_list );
175 bool IsVTableName(const char *name) override;
177 bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic,
178 TypeAndOrName &class_type_or_name, Address &address,
179 Value::ValueType &value_type) override;
182 FixUpDynamicType(const TypeAndOrName& type_and_or_name,
183 ValueObject& static_value) override;
185 bool CouldHaveDynamicValue(ValueObject &in_value) override;
187 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) override;
189 bool LoadModule(const lldb::ModuleSP &module_sp);
191 bool ProbeModules(const ModuleList module_list);
193 void DumpModules(Stream &strm) const;
195 void DumpContexts(Stream &strm) const;
197 void DumpKernels(Stream &strm) const;
199 bool DumpAllocation(Stream &strm, StackFrame* frame_ptr, const uint32_t id);
201 void ListAllocations(Stream &strm, StackFrame* frame_ptr, bool recompute);
203 void PlaceBreakpointOnKernel(Stream &strm, const char *name, const std::array<int,3> coords,
204 Error &error, lldb::TargetSP target);
206 void SetBreakAllKernels(bool do_break, lldb::TargetSP target);
208 void Status(Stream &strm) const;
210 void ModulesDidLoad(const ModuleList &module_list) override;
212 bool LoadAllocation(Stream &strm, const uint32_t alloc_id, const char* filename, StackFrame* frame_ptr);
214 bool SaveAllocation(Stream &strm, const uint32_t alloc_id, const char* filename, StackFrame* frame_ptr);
220 //------------------------------------------------------------------
221 // PluginInterface protocol
222 //------------------------------------------------------------------
223 lldb_private::ConstString GetPluginName() override;
225 uint32_t GetPluginVersion() override;
228 struct ScriptDetails;
229 struct AllocationDetails;
232 void InitSearchFilter(lldb::TargetSP target)
235 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
238 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
240 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
242 bool RefreshAllocation(AllocationDetails* allocation, StackFrame* frame_ptr);
244 bool EvalRSExpression(const char* expression, StackFrame* frame_ptr, uint64_t* result);
246 lldb::BreakpointSP CreateKernelBreakpoint(const ConstString& name);
248 void BreakOnModuleKernels(const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
251 typedef void (RenderScriptRuntime::*CaptureStateFn)(RuntimeHook* hook_info, ExecutionContext &context); // Please do this!
256 const char * symbol_name_m32; // mangled name for the 32 bit architectures
257 const char * symbol_name_m64; // mangled name for the 64 bit archs
260 CaptureStateFn grabber;
265 lldb::addr_t address;
266 const HookDefn *defn;
267 lldb::BreakpointSP bp_sp;
270 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
272 lldb::ModuleSP m_libRS;
273 lldb::ModuleSP m_libRSDriver;
274 lldb::ModuleSP m_libRSCpuRef;
275 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
277 std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
278 std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
280 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP> m_scriptMappings;
281 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
282 std::map<lldb::user_id_t, std::shared_ptr<int>> m_conditional_breaks;
284 lldb::SearchFilterSP m_filtersp; // Needed to create breakpoints through Target API
287 bool m_debuggerPresentFlagged;
288 bool m_breakAllKernels;
289 static const HookDefn s_runtimeHookDefns[];
290 static const size_t s_runtimeHookCount;
293 // Used to index expression format strings
294 enum ExpressionStrings
296 eExprGetOffsetPtr = 0,
305 eExprElementFieldCount,
307 eExprSubelementsName,
308 eExprSubelementsArrSize
311 RenderScriptRuntime(Process *process); // Call CreateInstance instead.
313 static bool HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id,
314 lldb::user_id_t break_loc_id);
316 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx,
317 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
319 void HookCallback(RuntimeHook* hook_info, ExecutionContext& context);
321 bool GetArgSimple(ExecutionContext& context, uint32_t arg, uint64_t* data);
323 void CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext& context);
324 void CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionContext& context);
325 void CaptureAllocationDestroy(RuntimeHook* hook_info, ExecutionContext& context);
326 void CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionContext& context);
328 AllocationDetails* FindAllocByID(Stream &strm, const uint32_t alloc_id);
329 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails* allocation, StackFrame* frame_ptr);
330 void SetElementSize(Element& elem);
331 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP, const char* var_name, uint64_t& val);
332 void FindStructTypeName(Element& elem, StackFrame* frame_ptr);
334 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, size_t offset, const Element& elem);
335 size_t CalculateElementHeaderSize(const Element& elem);
338 // Helper functions for jitting the runtime
340 const char* JITTemplate(ExpressionStrings e);
342 bool JITDataPointer(AllocationDetails* allocation, StackFrame* frame_ptr,
343 unsigned int x = 0, unsigned int y = 0, unsigned int z = 0);
345 bool JITTypePointer(AllocationDetails* allocation, StackFrame* frame_ptr);
347 bool JITTypePacked(AllocationDetails* allocation, StackFrame* frame_ptr);
349 bool JITElementPacked(Element& elem, const lldb::addr_t context, StackFrame* frame_ptr);
351 bool JITAllocationSize(AllocationDetails* allocation, StackFrame* frame_ptr);
353 bool JITSubelements(Element& elem, const lldb::addr_t context, StackFrame* frame_ptr);
355 bool JITAllocationStride(AllocationDetails* allocation, StackFrame* frame_ptr);
357 // Search for a script detail object using a target address.
358 // If a script does not currently exist this function will return nullptr.
359 // If 'create' is true and there is no previous script with this address,
360 // then a new Script detail object will be created for this address and returned.
361 ScriptDetails* LookUpScript(lldb::addr_t address, bool create);
363 // Search for a previously saved allocation detail object using a target address.
364 // If an allocation does not exist for this address then nullptr will be returned.
365 // If 'create' is true and there is no previous allocation then a new allocation
366 // detail object will be created for this address and returned.
367 AllocationDetails* LookUpAllocation(lldb::addr_t address, bool create);
370 } // namespace lldb_private
372 #endif // liblldb_RenderScriptRuntime_h_