1 //===-- RenderScriptRuntime.h -----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_RenderScriptRuntime_h_
10 #define liblldb_RenderScriptRuntime_h_
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Expression/LLVMUserExpression.h"
22 #include "lldb/Target/LanguageRuntime.h"
23 #include "lldb/lldb-private.h"
25 #include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
27 namespace lldb_private {
28 namespace lldb_renderscript {
30 typedef uint32_t RSSlot;
31 class RSModuleDescriptor;
32 struct RSGlobalDescriptor;
33 struct RSKernelDescriptor;
34 struct RSReductionDescriptor;
35 struct RSScriptGroupDescriptor;
37 typedef std::shared_ptr<RSModuleDescriptor> RSModuleDescriptorSP;
38 typedef std::shared_ptr<RSGlobalDescriptor> RSGlobalDescriptorSP;
39 typedef std::shared_ptr<RSKernelDescriptor> RSKernelDescriptorSP;
40 typedef std::shared_ptr<RSScriptGroupDescriptor> RSScriptGroupDescriptorSP;
45 RSCoordinate() : x(), y(), z(){};
47 bool operator==(const lldb_renderscript::RSCoordinate &rhs) {
48 return x == rhs.x && y == rhs.y && z == rhs.z;
52 // Breakpoint Resolvers decide where a breakpoint is placed, so having our own
53 // allows us to limit the search scope to RS kernel modules. As well as check
54 // for .expand kernels as a fallback.
55 class RSBreakpointResolver : public BreakpointResolver {
57 RSBreakpointResolver(Breakpoint *bp, ConstString name)
58 : BreakpointResolver(bp, BreakpointResolver::NameResolver),
59 m_kernel_name(name) {}
61 void GetDescription(Stream *strm) override {
63 strm->Printf("RenderScript kernel breakpoint for '%s'",
64 m_kernel_name.AsCString());
67 void Dump(Stream *s) const override {}
69 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
70 SymbolContext &context, Address *addr,
71 bool containing) override;
73 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
75 lldb::BreakpointResolverSP
76 CopyForBreakpoint(Breakpoint &breakpoint) override {
77 lldb::BreakpointResolverSP ret_sp(
78 new RSBreakpointResolver(&breakpoint, m_kernel_name));
83 ConstString m_kernel_name;
86 class RSReduceBreakpointResolver : public BreakpointResolver {
88 enum ReduceKernelTypeFlags {
89 eKernelTypeAll = ~(0),
91 eKernelTypeAccum = (1 << 0),
92 eKernelTypeInit = (1 << 1),
93 eKernelTypeComb = (1 << 2),
94 eKernelTypeOutC = (1 << 3),
95 eKernelTypeHalter = (1 << 4)
98 RSReduceBreakpointResolver(
99 Breakpoint *breakpoint, ConstString reduce_name,
100 std::vector<lldb_renderscript::RSModuleDescriptorSP> *rs_modules,
101 int kernel_types = eKernelTypeAll)
102 : BreakpointResolver(breakpoint, BreakpointResolver::NameResolver),
103 m_reduce_name(reduce_name), m_rsmodules(rs_modules),
104 m_kernel_types(kernel_types) {
105 // The reduce breakpoint resolver handles adding breakpoints for named
107 // Breakpoints will be resolved for all constituent kernels in the named
111 void GetDescription(Stream *strm) override {
113 strm->Printf("RenderScript reduce breakpoint for '%s'",
114 m_reduce_name.AsCString());
117 void Dump(Stream *s) const override {}
119 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
120 SymbolContext &context, Address *addr,
121 bool containing) override;
123 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
125 lldb::BreakpointResolverSP
126 CopyForBreakpoint(Breakpoint &breakpoint) override {
127 lldb::BreakpointResolverSP ret_sp(new RSReduceBreakpointResolver(
128 &breakpoint, m_reduce_name, m_rsmodules, m_kernel_types));
133 ConstString m_reduce_name; // The name of the reduction
134 std::vector<lldb_renderscript::RSModuleDescriptorSP> *m_rsmodules;
138 struct RSKernelDescriptor {
140 RSKernelDescriptor(const RSModuleDescriptor *module, llvm::StringRef name,
142 : m_module(module), m_name(name), m_slot(slot) {}
144 void Dump(Stream &strm) const;
146 const RSModuleDescriptor *m_module;
151 struct RSGlobalDescriptor {
153 RSGlobalDescriptor(const RSModuleDescriptor *module, llvm::StringRef name)
154 : m_module(module), m_name(name) {}
156 void Dump(Stream &strm) const;
158 const RSModuleDescriptor *m_module;
162 struct RSReductionDescriptor {
163 RSReductionDescriptor(const RSModuleDescriptor *module, uint32_t sig,
164 uint32_t accum_data_size, llvm::StringRef name,
165 llvm::StringRef init_name, llvm::StringRef accum_name,
166 llvm::StringRef comb_name, llvm::StringRef outc_name,
167 llvm::StringRef halter_name = ".")
168 : m_module(module), m_reduce_name(name), m_init_name(init_name),
169 m_accum_name(accum_name), m_comb_name(comb_name),
170 m_outc_name(outc_name), m_halter_name(halter_name) {
171 // TODO Check whether the combiner is an autogenerated name, and track
175 void Dump(Stream &strm) const;
177 const RSModuleDescriptor *m_module;
178 ConstString m_reduce_name; // This is the name given to the general reduction
179 // as a group as passed to pragma
180 // reduce(m_reduce_name). There is no kernel function with this name
181 ConstString m_init_name; // The name of the initializer name. "." if no
183 ConstString m_accum_name; // The accumulator function name. "." if not given
184 ConstString m_comb_name; // The name of the combiner function. If this was not
185 // given, a name is generated by the
187 ConstString m_outc_name; // The name of the outconverter
189 ConstString m_halter_name; // The name of the halter function. XXX This is not
190 // yet specified by the RenderScript
191 // compiler or runtime, and its semantics and existence is still under
193 // RenderScript Contributors
194 RSSlot m_accum_sig; // metatdata signature for this reduction (bitwise mask of
195 // type information (see
196 // libbcc/include/bcinfo/MetadataExtractor.h
197 uint32_t m_accum_data_size; // Data size of the accumulator function input
198 bool m_comb_name_generated; // Was the combiner name generated by the compiler
201 class RSModuleDescriptor {
202 std::string m_slang_version;
203 std::string m_bcc_version;
205 bool ParseVersionInfo(llvm::StringRef *, size_t n_lines);
207 bool ParseExportForeachCount(llvm::StringRef *, size_t n_lines);
209 bool ParseExportVarCount(llvm::StringRef *, size_t n_lines);
211 bool ParseExportReduceCount(llvm::StringRef *, size_t n_lines);
213 bool ParseBuildChecksum(llvm::StringRef *, size_t n_lines);
215 bool ParsePragmaCount(llvm::StringRef *, size_t n_lines);
218 RSModuleDescriptor(const lldb::ModuleSP &module) : m_module(module) {}
220 ~RSModuleDescriptor() = default;
224 void Dump(Stream &strm) const;
226 void WarnIfVersionMismatch(Stream *s) const;
228 const lldb::ModuleSP m_module;
229 std::vector<RSKernelDescriptor> m_kernels;
230 std::vector<RSGlobalDescriptor> m_globals;
231 std::vector<RSReductionDescriptor> m_reductions;
232 std::map<std::string, std::string> m_pragmas;
233 std::string m_resname;
236 struct RSScriptGroupDescriptor {
242 std::vector<Kernel> m_kernels;
245 typedef std::vector<RSScriptGroupDescriptorSP> RSScriptGroupList;
247 class RSScriptGroupBreakpointResolver : public BreakpointResolver {
249 RSScriptGroupBreakpointResolver(Breakpoint *bp, ConstString name,
250 const RSScriptGroupList &groups,
252 : BreakpointResolver(bp, BreakpointResolver::NameResolver),
253 m_group_name(name), m_script_groups(groups),
254 m_stop_on_all(stop_on_all) {}
256 void GetDescription(Stream *strm) override {
258 strm->Printf("RenderScript ScriptGroup breakpoint for '%s'",
259 m_group_name.AsCString());
262 void Dump(Stream *s) const override {}
264 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
265 SymbolContext &context, Address *addr,
266 bool containing) override;
268 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
270 lldb::BreakpointResolverSP
271 CopyForBreakpoint(Breakpoint &breakpoint) override {
272 lldb::BreakpointResolverSP ret_sp(new RSScriptGroupBreakpointResolver(
273 &breakpoint, m_group_name, m_script_groups, m_stop_on_all));
278 const RSScriptGroupDescriptorSP
279 FindScriptGroup(ConstString name) const {
280 for (auto sg : m_script_groups) {
281 if (ConstString::Compare(sg->m_name, name) == 0)
284 return RSScriptGroupDescriptorSP();
287 ConstString m_group_name;
288 const RSScriptGroupList &m_script_groups;
291 } // namespace lldb_renderscript
293 class RenderScriptRuntime : public lldb_private::CPPLanguageRuntime {
303 ~RenderScriptRuntime() override;
306 static void Initialize();
308 static void Terminate();
310 static lldb_private::LanguageRuntime *
311 CreateInstance(Process *process, lldb::LanguageType language);
313 static lldb::CommandObjectSP
314 GetCommandObject(CommandInterpreter &interpreter);
316 static lldb_private::ConstString GetPluginNameStatic();
320 bool isA(const void *ClassID) const override {
321 return ClassID == &ID || CPPLanguageRuntime::isA(ClassID);
324 static bool classof(const LanguageRuntime *runtime) {
325 return runtime->isA(&ID);
328 static bool IsRenderScriptModule(const lldb::ModuleSP &module_sp);
330 static ModuleKind GetModuleKind(const lldb::ModuleSP &module_sp);
332 static void ModulesDidLoad(const lldb::ProcessSP &process_sp,
333 const ModuleList &module_list);
335 bool GetDynamicTypeAndAddress(ValueObject &in_value,
336 lldb::DynamicValueType use_dynamic,
337 TypeAndOrName &class_type_or_name,
339 Value::ValueType &value_type) override;
341 TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name,
342 ValueObject &static_value) override;
344 bool CouldHaveDynamicValue(ValueObject &in_value) override;
346 lldb::BreakpointResolverSP CreateExceptionResolver(Breakpoint *bp,
348 bool throw_bp) override;
350 bool LoadModule(const lldb::ModuleSP &module_sp);
352 void DumpModules(Stream &strm) const;
354 void DumpContexts(Stream &strm) const;
356 void DumpKernels(Stream &strm) const;
358 bool DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id);
360 void ListAllocations(Stream &strm, StackFrame *frame_ptr,
361 const uint32_t index);
363 bool RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr);
365 bool PlaceBreakpointOnKernel(
366 lldb::TargetSP target, Stream &messages, const char *name,
367 const lldb_renderscript::RSCoordinate *coords = nullptr);
369 bool PlaceBreakpointOnReduction(
370 lldb::TargetSP target, Stream &messages, const char *reduce_name,
371 const lldb_renderscript::RSCoordinate *coords = nullptr,
372 int kernel_types = ~(0));
374 bool PlaceBreakpointOnScriptGroup(lldb::TargetSP target, Stream &strm,
375 ConstString name, bool stop_on_all);
377 void SetBreakAllKernels(bool do_break, lldb::TargetSP target);
379 void DumpStatus(Stream &strm) const;
381 void ModulesDidLoad(const ModuleList &module_list) override;
383 bool LoadAllocation(Stream &strm, const uint32_t alloc_id,
384 const char *filename, StackFrame *frame_ptr);
386 bool SaveAllocation(Stream &strm, const uint32_t alloc_id,
387 const char *filename, StackFrame *frame_ptr);
393 const lldb_renderscript::RSScriptGroupList &GetScriptGroups() const {
394 return m_scriptGroups;
397 bool IsKnownKernel(ConstString name) {
398 for (const auto &module : m_rsmodules)
399 for (const auto &kernel : module->m_kernels)
400 if (kernel.m_name == name)
405 // PluginInterface protocol
406 lldb_private::ConstString GetPluginName() override;
408 uint32_t GetPluginVersion() override;
410 static bool GetKernelCoordinate(lldb_renderscript::RSCoordinate &coord,
413 bool ResolveKernelName(lldb::addr_t kernel_address, ConstString &name);
416 struct ScriptDetails;
417 struct AllocationDetails;
420 lldb_renderscript::RSScriptGroupList m_scriptGroups;
422 void InitSearchFilter(lldb::TargetSP target) {
424 m_filtersp.reset(new SearchFilterForUnconstrainedSearches(target));
427 void FixupScriptDetails(lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
429 void LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind);
431 bool RefreshAllocation(AllocationDetails *alloc, StackFrame *frame_ptr);
433 bool EvalRSExpression(const char *expression, StackFrame *frame_ptr,
436 lldb::BreakpointSP CreateScriptGroupBreakpoint(ConstString name,
439 lldb::BreakpointSP CreateKernelBreakpoint(ConstString name);
441 lldb::BreakpointSP CreateReductionBreakpoint(ConstString name,
444 void BreakOnModuleKernels(
445 const lldb_renderscript::RSModuleDescriptorSP rsmodule_sp);
448 typedef void (RenderScriptRuntime::*CaptureStateFn)(
449 RuntimeHook *hook_info,
450 ExecutionContext &context); // Please do this!
454 const char *symbol_name_m32; // mangled name for the 32 bit architectures
455 const char *symbol_name_m64; // mangled name for the 64 bit archs
458 CaptureStateFn grabber;
462 lldb::addr_t address;
463 const HookDefn *defn;
464 lldb::BreakpointSP bp_sp;
467 typedef std::shared_ptr<RuntimeHook> RuntimeHookSP;
469 lldb::ModuleSP m_libRS;
470 lldb::ModuleSP m_libRSDriver;
471 lldb::ModuleSP m_libRSCpuRef;
472 std::vector<lldb_renderscript::RSModuleDescriptorSP> m_rsmodules;
474 std::vector<std::unique_ptr<ScriptDetails>> m_scripts;
475 std::vector<std::unique_ptr<AllocationDetails>> m_allocations;
477 std::map<lldb::addr_t, lldb_renderscript::RSModuleDescriptorSP>
479 std::map<lldb::addr_t, RuntimeHookSP> m_runtimeHooks;
480 std::map<lldb::user_id_t, std::unique_ptr<lldb_renderscript::RSCoordinate>>
481 m_conditional_breaks;
484 m_filtersp; // Needed to create breakpoints through Target API
487 bool m_debuggerPresentFlagged;
488 bool m_breakAllKernels;
489 static const HookDefn s_runtimeHookDefns[];
490 static const size_t s_runtimeHookCount;
491 LLVMUserExpression::IRPasses *m_ir_passes;
494 RenderScriptRuntime(Process *process); // Call CreateInstance instead.
496 static bool HookCallback(void *baton, StoppointCallbackContext *ctx,
497 lldb::user_id_t break_id,
498 lldb::user_id_t break_loc_id);
500 static bool KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx,
501 lldb::user_id_t break_id,
502 lldb::user_id_t break_loc_id);
504 void HookCallback(RuntimeHook *hook_info, ExecutionContext &context);
506 // Callback function when 'debugHintScriptGroup2' executes on the target.
507 void CaptureDebugHintScriptGroup2(RuntimeHook *hook_info,
508 ExecutionContext &context);
510 void CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context);
512 void CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context);
514 void CaptureAllocationDestroy(RuntimeHook *hook_info,
515 ExecutionContext &context);
517 void CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context);
519 void CaptureScriptInvokeForEachMulti(RuntimeHook *hook_info,
520 ExecutionContext &context);
522 AllocationDetails *FindAllocByID(Stream &strm, const uint32_t alloc_id);
524 std::shared_ptr<uint8_t> GetAllocationData(AllocationDetails *alloc,
525 StackFrame *frame_ptr);
527 void SetElementSize(Element &elem);
529 static bool GetFrameVarAsUnsigned(const lldb::StackFrameSP,
530 const char *var_name, uint64_t &val);
532 void FindStructTypeName(Element &elem, StackFrame *frame_ptr);
534 size_t PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer,
535 size_t offset, const Element &elem);
537 size_t CalculateElementHeaderSize(const Element &elem);
539 void SetConditional(lldb::BreakpointSP bp, lldb_private::Stream &messages,
540 const lldb_renderscript::RSCoordinate &coord);
542 // Helper functions for jitting the runtime
545 bool JITDataPointer(AllocationDetails *alloc, StackFrame *frame_ptr,
546 uint32_t x = 0, uint32_t y = 0, uint32_t z = 0);
548 bool JITTypePointer(AllocationDetails *alloc, StackFrame *frame_ptr);
550 bool JITTypePacked(AllocationDetails *alloc, StackFrame *frame_ptr);
552 bool JITElementPacked(Element &elem, const lldb::addr_t context,
553 StackFrame *frame_ptr);
555 bool JITAllocationSize(AllocationDetails *alloc, StackFrame *frame_ptr);
557 bool JITSubelements(Element &elem, const lldb::addr_t context,
558 StackFrame *frame_ptr);
560 bool JITAllocationStride(AllocationDetails *alloc, StackFrame *frame_ptr);
562 // Search for a script detail object using a target address.
563 // If a script does not currently exist this function will return nullptr.
564 // If 'create' is true and there is no previous script with this address,
565 // then a new Script detail object will be created for this address and
567 ScriptDetails *LookUpScript(lldb::addr_t address, bool create);
569 // Search for a previously saved allocation detail object using a target
571 // If an allocation does not exist for this address then nullptr will be
573 AllocationDetails *LookUpAllocation(lldb::addr_t address);
575 // Creates a new allocation with the specified address assigning a new ID and
577 // any previous stored allocation which has the same address.
578 AllocationDetails *CreateAllocation(lldb::addr_t address);
580 bool GetOverrideExprOptions(clang::TargetOptions &prototype) override;
582 bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override;
585 } // namespace lldb_private
587 #endif // liblldb_RenderScriptRuntime_h_