15ec532a9SColin Riley //===-- RenderScriptRuntime.cpp ---------------------------------*- C++ -*-===//
25ec532a9SColin Riley //
35ec532a9SColin Riley //                     The LLVM Compiler Infrastructure
45ec532a9SColin Riley //
55ec532a9SColin Riley // This file is distributed under the University of Illinois Open Source
65ec532a9SColin Riley // License. See LICENSE.TXT for details.
75ec532a9SColin Riley //
85ec532a9SColin Riley //===----------------------------------------------------------------------===//
95ec532a9SColin Riley 
105ec532a9SColin Riley #include "RenderScriptRuntime.h"
115ec532a9SColin Riley 
125ec532a9SColin Riley #include "lldb/Core/ConstString.h"
135ec532a9SColin Riley #include "lldb/Core/Debugger.h"
145ec532a9SColin Riley #include "lldb/Core/Error.h"
155ec532a9SColin Riley #include "lldb/Core/Log.h"
165ec532a9SColin Riley #include "lldb/Core/PluginManager.h"
17*a0f08674SEwan Crawford #include "lldb/Host/StringConvert.h"
185ec532a9SColin Riley #include "lldb/Symbol/Symbol.h"
194640cde1SColin Riley #include "lldb/Symbol/Type.h"
205ec532a9SColin Riley #include "lldb/Target/Process.h"
215ec532a9SColin Riley #include "lldb/Target/Target.h"
225ec532a9SColin Riley #include "lldb/Interpreter/Args.h"
235ec532a9SColin Riley #include "lldb/Interpreter/Options.h"
245ec532a9SColin Riley #include "lldb/Interpreter/CommandInterpreter.h"
255ec532a9SColin Riley #include "lldb/Interpreter/CommandReturnObject.h"
265ec532a9SColin Riley #include "lldb/Interpreter/CommandObjectMultiword.h"
274640cde1SColin Riley #include "lldb/Breakpoint/StoppointCallbackContext.h"
284640cde1SColin Riley #include "lldb/Target/RegisterContext.h"
2915f2bd95SEwan Crawford #include "lldb/Expression/UserExpression.h"
304640cde1SColin Riley #include "lldb/Symbol/VariableList.h"
315ec532a9SColin Riley 
325ec532a9SColin Riley using namespace lldb;
335ec532a9SColin Riley using namespace lldb_private;
3498156583SEwan Crawford using namespace lldb_renderscript;
355ec532a9SColin Riley 
3678f339d1SEwan Crawford namespace {
3778f339d1SEwan Crawford 
3878f339d1SEwan Crawford // The empirical_type adds a basic level of validation to arbitrary data
3978f339d1SEwan Crawford // allowing us to track if data has been discovered and stored or not.
4078f339d1SEwan Crawford // An empirical_type will be marked as valid only if it has been explicitly assigned to.
4178f339d1SEwan Crawford template <typename type_t>
4278f339d1SEwan Crawford class empirical_type
4378f339d1SEwan Crawford {
4478f339d1SEwan Crawford   public:
4578f339d1SEwan Crawford     // Ctor. Contents is invalid when constructed.
4678f339d1SEwan Crawford     empirical_type()
4778f339d1SEwan Crawford         : valid(false)
4878f339d1SEwan Crawford     {}
4978f339d1SEwan Crawford 
5078f339d1SEwan Crawford     // Return true and copy contents to out if valid, else return false.
5178f339d1SEwan Crawford     bool get(type_t& out) const
5278f339d1SEwan Crawford     {
5378f339d1SEwan Crawford         if (valid)
5478f339d1SEwan Crawford             out = data;
5578f339d1SEwan Crawford         return valid;
5678f339d1SEwan Crawford     }
5778f339d1SEwan Crawford 
5878f339d1SEwan Crawford     // Return a pointer to the contents or nullptr if it was not valid.
5978f339d1SEwan Crawford     const type_t* get() const
6078f339d1SEwan Crawford     {
6178f339d1SEwan Crawford         return valid ? &data : nullptr;
6278f339d1SEwan Crawford     }
6378f339d1SEwan Crawford 
6478f339d1SEwan Crawford     // Assign data explicitly.
6578f339d1SEwan Crawford     void set(const type_t in)
6678f339d1SEwan Crawford     {
6778f339d1SEwan Crawford         data = in;
6878f339d1SEwan Crawford         valid = true;
6978f339d1SEwan Crawford     }
7078f339d1SEwan Crawford 
7178f339d1SEwan Crawford     // Mark contents as invalid.
7278f339d1SEwan Crawford     void invalidate()
7378f339d1SEwan Crawford     {
7478f339d1SEwan Crawford         valid = false;
7578f339d1SEwan Crawford     }
7678f339d1SEwan Crawford 
7778f339d1SEwan Crawford     // Returns true if this type contains valid data.
7878f339d1SEwan Crawford     bool isValid() const
7978f339d1SEwan Crawford     {
8078f339d1SEwan Crawford         return valid;
8178f339d1SEwan Crawford     }
8278f339d1SEwan Crawford 
8378f339d1SEwan Crawford     // Assignment operator.
8478f339d1SEwan Crawford     empirical_type<type_t>& operator = (const type_t in)
8578f339d1SEwan Crawford     {
8678f339d1SEwan Crawford         set(in);
8778f339d1SEwan Crawford         return *this;
8878f339d1SEwan Crawford     }
8978f339d1SEwan Crawford 
9078f339d1SEwan Crawford     // Dereference operator returns contents.
9178f339d1SEwan Crawford     // Warning: Will assert if not valid so use only when you know data is valid.
9278f339d1SEwan Crawford     const type_t& operator * () const
9378f339d1SEwan Crawford     {
9478f339d1SEwan Crawford         assert(valid);
9578f339d1SEwan Crawford         return data;
9678f339d1SEwan Crawford     }
9778f339d1SEwan Crawford 
9878f339d1SEwan Crawford   protected:
9978f339d1SEwan Crawford     bool valid;
10078f339d1SEwan Crawford     type_t data;
10178f339d1SEwan Crawford };
10278f339d1SEwan Crawford 
10378f339d1SEwan Crawford } // namespace {}
10478f339d1SEwan Crawford 
10578f339d1SEwan Crawford // The ScriptDetails class collects data associated with a single script instance.
10678f339d1SEwan Crawford struct RenderScriptRuntime::ScriptDetails
10778f339d1SEwan Crawford {
10878f339d1SEwan Crawford     ~ScriptDetails() {};
10978f339d1SEwan Crawford 
11078f339d1SEwan Crawford     enum ScriptType
11178f339d1SEwan Crawford     {
11278f339d1SEwan Crawford         eScript,
11378f339d1SEwan Crawford         eScriptC
11478f339d1SEwan Crawford     };
11578f339d1SEwan Crawford 
11678f339d1SEwan Crawford     // The derived type of the script.
11778f339d1SEwan Crawford     empirical_type<ScriptType> type;
11878f339d1SEwan Crawford     // The name of the original source file.
11978f339d1SEwan Crawford     empirical_type<std::string> resName;
12078f339d1SEwan Crawford     // Path to script .so file on the device.
12178f339d1SEwan Crawford     empirical_type<std::string> scriptDyLib;
12278f339d1SEwan Crawford     // Directory where kernel objects are cached on device.
12378f339d1SEwan Crawford     empirical_type<std::string> cacheDir;
12478f339d1SEwan Crawford     // Pointer to the context which owns this script.
12578f339d1SEwan Crawford     empirical_type<lldb::addr_t> context;
12678f339d1SEwan Crawford     // Pointer to the script object itself.
12778f339d1SEwan Crawford     empirical_type<lldb::addr_t> script;
12878f339d1SEwan Crawford };
12978f339d1SEwan Crawford 
13078f339d1SEwan Crawford // This AllocationDetails class collects data associated with a single
13178f339d1SEwan Crawford // allocation instance.
13278f339d1SEwan Crawford struct RenderScriptRuntime::AllocationDetails
13378f339d1SEwan Crawford {
13415f2bd95SEwan Crawford    // Taken from rsDefines.h
13515f2bd95SEwan Crawford    enum DataKind
13615f2bd95SEwan Crawford    {
13715f2bd95SEwan Crawford        RS_KIND_USER,
13815f2bd95SEwan Crawford        RS_KIND_PIXEL_L = 7,
13915f2bd95SEwan Crawford        RS_KIND_PIXEL_A,
14015f2bd95SEwan Crawford        RS_KIND_PIXEL_LA,
14115f2bd95SEwan Crawford        RS_KIND_PIXEL_RGB,
14215f2bd95SEwan Crawford        RS_KIND_PIXEL_RGBA,
14315f2bd95SEwan Crawford        RS_KIND_PIXEL_DEPTH,
14415f2bd95SEwan Crawford        RS_KIND_PIXEL_YUV,
14515f2bd95SEwan Crawford        RS_KIND_INVALID = 100
14615f2bd95SEwan Crawford    };
14778f339d1SEwan Crawford 
14815f2bd95SEwan Crawford    // Taken from rsDefines.h
14978f339d1SEwan Crawford    enum DataType
15078f339d1SEwan Crawford    {
15115f2bd95SEwan Crawford        RS_TYPE_NONE = 0,
15215f2bd95SEwan Crawford        RS_TYPE_FLOAT_16,
15315f2bd95SEwan Crawford        RS_TYPE_FLOAT_32,
15415f2bd95SEwan Crawford        RS_TYPE_FLOAT_64,
15515f2bd95SEwan Crawford        RS_TYPE_SIGNED_8,
15615f2bd95SEwan Crawford        RS_TYPE_SIGNED_16,
15715f2bd95SEwan Crawford        RS_TYPE_SIGNED_32,
15815f2bd95SEwan Crawford        RS_TYPE_SIGNED_64,
15915f2bd95SEwan Crawford        RS_TYPE_UNSIGNED_8,
16015f2bd95SEwan Crawford        RS_TYPE_UNSIGNED_16,
16115f2bd95SEwan Crawford        RS_TYPE_UNSIGNED_32,
16215f2bd95SEwan Crawford        RS_TYPE_UNSIGNED_64,
16315f2bd95SEwan Crawford        RS_TYPE_BOOLEAN
16478f339d1SEwan Crawford     };
16578f339d1SEwan Crawford 
16615f2bd95SEwan Crawford     struct Dimension
16778f339d1SEwan Crawford     {
16815f2bd95SEwan Crawford         uint32_t dim_1;
16915f2bd95SEwan Crawford         uint32_t dim_2;
17015f2bd95SEwan Crawford         uint32_t dim_3;
17115f2bd95SEwan Crawford         uint32_t cubeMap;
17215f2bd95SEwan Crawford 
17315f2bd95SEwan Crawford         Dimension()
17415f2bd95SEwan Crawford         {
17515f2bd95SEwan Crawford              dim_1 = 0;
17615f2bd95SEwan Crawford              dim_2 = 0;
17715f2bd95SEwan Crawford              dim_3 = 0;
17815f2bd95SEwan Crawford              cubeMap = 0;
17915f2bd95SEwan Crawford         }
18078f339d1SEwan Crawford     };
18178f339d1SEwan Crawford 
18215f2bd95SEwan Crawford     // Monotonically increasing from 1
18315f2bd95SEwan Crawford     static unsigned int ID;
18415f2bd95SEwan Crawford 
18515f2bd95SEwan Crawford     // Maps Allocation DataType enum and vector size to printable strings
18615f2bd95SEwan Crawford     // using mapping from RenderScript numerical types summary documentation
18715f2bd95SEwan Crawford     static const char* RsDataTypeToString[][4];
18815f2bd95SEwan Crawford 
18915f2bd95SEwan Crawford     // Maps Allocation DataKind enum to printable strings
19015f2bd95SEwan Crawford     static const char* RsDataKindToString[];
19115f2bd95SEwan Crawford 
192*a0f08674SEwan Crawford     // Maps allocation types to format sizes for printing.
193*a0f08674SEwan Crawford     static const unsigned int RSTypeToFormat[][3];
194*a0f08674SEwan Crawford 
19515f2bd95SEwan Crawford     // Give each allocation an ID as a way
19615f2bd95SEwan Crawford     // for commands to reference it.
19715f2bd95SEwan Crawford     const unsigned int id;
19815f2bd95SEwan Crawford 
19915f2bd95SEwan Crawford     empirical_type<DataType> type;            // Type of each data pointer stored by the allocation
20015f2bd95SEwan Crawford     empirical_type<DataKind> type_kind;       // Defines pixel type if Allocation is created from an image
20115f2bd95SEwan Crawford     empirical_type<uint32_t> type_vec_size;   // Vector size of each data point, e.g '4' for uchar4
20215f2bd95SEwan Crawford     empirical_type<Dimension> dimension;      // Dimensions of the Allocation
20315f2bd95SEwan Crawford     empirical_type<lldb::addr_t> address;     // Pointer to address of the RS Allocation
20415f2bd95SEwan Crawford     empirical_type<lldb::addr_t> data_ptr;    // Pointer to the data held by the Allocation
20515f2bd95SEwan Crawford     empirical_type<lldb::addr_t> type_ptr;    // Pointer to the RS Type of the Allocation
20615f2bd95SEwan Crawford     empirical_type<lldb::addr_t> element_ptr; // Pointer to the RS Element of the Type
20715f2bd95SEwan Crawford     empirical_type<lldb::addr_t> context;     // Pointer to the RS Context of the Allocation
208*a0f08674SEwan Crawford     empirical_type<uint32_t> size;            // Size of the allocation
209*a0f08674SEwan Crawford     empirical_type<uint32_t> stride;          // Stride between rows of the allocation
21015f2bd95SEwan Crawford 
21115f2bd95SEwan Crawford     // Give each allocation an id, so we can reference it in user commands.
21215f2bd95SEwan Crawford     AllocationDetails(): id(ID++)
21315f2bd95SEwan Crawford     {
21415f2bd95SEwan Crawford     }
21515f2bd95SEwan Crawford 
21615f2bd95SEwan Crawford };
21715f2bd95SEwan Crawford 
21815f2bd95SEwan Crawford unsigned int RenderScriptRuntime::AllocationDetails::ID = 1;
21915f2bd95SEwan Crawford 
22015f2bd95SEwan Crawford const char* RenderScriptRuntime::AllocationDetails::RsDataKindToString[] =
22115f2bd95SEwan Crawford {
22215f2bd95SEwan Crawford    "User",
22315f2bd95SEwan Crawford    "Undefined", "Undefined", "Undefined", // Enum jumps from 0 to 7
22415f2bd95SEwan Crawford    "Undefined", "Undefined", "Undefined",
22515f2bd95SEwan Crawford    "L Pixel",
22615f2bd95SEwan Crawford    "A Pixel",
22715f2bd95SEwan Crawford    "LA Pixel",
22815f2bd95SEwan Crawford    "RGB Pixel",
22915f2bd95SEwan Crawford    "RGBA Pixel",
23015f2bd95SEwan Crawford    "Pixel Depth",
23115f2bd95SEwan Crawford    "YUV Pixel"
23215f2bd95SEwan Crawford };
23315f2bd95SEwan Crawford 
23415f2bd95SEwan Crawford const char* RenderScriptRuntime::AllocationDetails::RsDataTypeToString[][4] =
23515f2bd95SEwan Crawford {
23615f2bd95SEwan Crawford     {"None", "None", "None", "None"},
23715f2bd95SEwan Crawford     {"half", "half2", "half3", "half4"},
23815f2bd95SEwan Crawford     {"float", "float2", "float3", "float4"},
23915f2bd95SEwan Crawford     {"double", "double2", "double3", "double4"},
24015f2bd95SEwan Crawford     {"char", "char2", "char3", "char4"},
24115f2bd95SEwan Crawford     {"short", "short2", "short3", "short4"},
24215f2bd95SEwan Crawford     {"int", "int2", "int3", "int4"},
24315f2bd95SEwan Crawford     {"long", "long2", "long3", "long4"},
24415f2bd95SEwan Crawford     {"uchar", "uchar2", "uchar3", "uchar4"},
24515f2bd95SEwan Crawford     {"ushort", "ushort2", "ushort3", "ushort4"},
24615f2bd95SEwan Crawford     {"uint", "uint2", "uint3", "uint4"},
24715f2bd95SEwan Crawford     {"ulong", "ulong2", "ulong3", "ulong4"},
24815f2bd95SEwan Crawford     {"bool", "bool2", "bool3", "bool4"}
24978f339d1SEwan Crawford };
25078f339d1SEwan Crawford 
251*a0f08674SEwan Crawford // Used as an index into the RSTypeToFormat array elements
252*a0f08674SEwan Crawford enum TypeToFormatIndex {
253*a0f08674SEwan Crawford    eFormatSingle = 0,
254*a0f08674SEwan Crawford    eFormatVector,
255*a0f08674SEwan Crawford    eElementSize
256*a0f08674SEwan Crawford };
257*a0f08674SEwan Crawford 
258*a0f08674SEwan Crawford // { format enum of single element, format enum of element vector, size of element}
259*a0f08674SEwan Crawford const unsigned int RenderScriptRuntime::AllocationDetails::RSTypeToFormat[][3] =
260*a0f08674SEwan Crawford {
261*a0f08674SEwan Crawford     {eFormatHex, eFormatHex, 1}, // RS_TYPE_NONE
262*a0f08674SEwan Crawford     {eFormatFloat, eFormatVectorOfFloat16, 2}, // RS_TYPE_FLOAT_16
263*a0f08674SEwan Crawford     {eFormatFloat, eFormatVectorOfFloat32, sizeof(float)}, // RS_TYPE_FLOAT_32
264*a0f08674SEwan Crawford     {eFormatFloat, eFormatVectorOfFloat64, sizeof(double)}, // RS_TYPE_FLOAT_64
265*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfSInt8, sizeof(int8_t)}, // RS_TYPE_SIGNED_8
266*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfSInt16, sizeof(int16_t)}, // RS_TYPE_SIGNED_16
267*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfSInt32, sizeof(int32_t)}, // RS_TYPE_SIGNED_32
268*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfSInt64, sizeof(int64_t)}, // RS_TYPE_SIGNED_64
269*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfUInt8, sizeof(uint8_t)}, // RS_TYPE_UNSIGNED_8
270*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfUInt16, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_16
271*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfUInt32, sizeof(uint32_t)}, // RS_TYPE_UNSIGNED_32
272*a0f08674SEwan Crawford     {eFormatDecimal, eFormatVectorOfUInt64, sizeof(uint64_t)}, // RS_TYPE_UNSIGNED_64
273*a0f08674SEwan Crawford     {eFormatBoolean, eFormatBoolean, sizeof(bool)} // RS_TYPE_BOOL
274*a0f08674SEwan Crawford };
275*a0f08674SEwan Crawford 
2765ec532a9SColin Riley //------------------------------------------------------------------
2775ec532a9SColin Riley // Static Functions
2785ec532a9SColin Riley //------------------------------------------------------------------
2795ec532a9SColin Riley LanguageRuntime *
2805ec532a9SColin Riley RenderScriptRuntime::CreateInstance(Process *process, lldb::LanguageType language)
2815ec532a9SColin Riley {
2825ec532a9SColin Riley 
2835ec532a9SColin Riley     if (language == eLanguageTypeExtRenderScript)
2845ec532a9SColin Riley         return new RenderScriptRuntime(process);
2855ec532a9SColin Riley     else
2865ec532a9SColin Riley         return NULL;
2875ec532a9SColin Riley }
2885ec532a9SColin Riley 
28998156583SEwan Crawford // Callback with a module to search for matching symbols.
29098156583SEwan Crawford // We first check that the module contains RS kernels.
29198156583SEwan Crawford // Then look for a symbol which matches our kernel name.
29298156583SEwan Crawford // The breakpoint address is finally set using the address of this symbol.
29398156583SEwan Crawford Searcher::CallbackReturn
29498156583SEwan Crawford RSBreakpointResolver::SearchCallback(SearchFilter &filter,
29598156583SEwan Crawford                                      SymbolContext &context,
29698156583SEwan Crawford                                      Address*,
29798156583SEwan Crawford                                      bool)
29898156583SEwan Crawford {
29998156583SEwan Crawford     ModuleSP module = context.module_sp;
30098156583SEwan Crawford 
30198156583SEwan Crawford     if (!module)
30298156583SEwan Crawford         return Searcher::eCallbackReturnContinue;
30398156583SEwan Crawford 
30498156583SEwan Crawford     // Is this a module containing renderscript kernels?
30598156583SEwan Crawford     if (nullptr == module->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData))
30698156583SEwan Crawford         return Searcher::eCallbackReturnContinue;
30798156583SEwan Crawford 
30898156583SEwan Crawford     // Attempt to set a breakpoint on the kernel name symbol within the module library.
30998156583SEwan Crawford     // If it's not found, it's likely debug info is unavailable - try to set a
31098156583SEwan Crawford     // breakpoint on <name>.expand.
31198156583SEwan Crawford 
31298156583SEwan Crawford     const Symbol* kernel_sym = module->FindFirstSymbolWithNameAndType(m_kernel_name, eSymbolTypeCode);
31398156583SEwan Crawford     if (!kernel_sym)
31498156583SEwan Crawford     {
31598156583SEwan Crawford         std::string kernel_name_expanded(m_kernel_name.AsCString());
31698156583SEwan Crawford         kernel_name_expanded.append(".expand");
31798156583SEwan Crawford         kernel_sym = module->FindFirstSymbolWithNameAndType(ConstString(kernel_name_expanded.c_str()), eSymbolTypeCode);
31898156583SEwan Crawford     }
31998156583SEwan Crawford 
32098156583SEwan Crawford     if (kernel_sym)
32198156583SEwan Crawford     {
32298156583SEwan Crawford         Address bp_addr = kernel_sym->GetAddress();
32398156583SEwan Crawford         if (filter.AddressPasses(bp_addr))
32498156583SEwan Crawford             m_breakpoint->AddLocation(bp_addr);
32598156583SEwan Crawford     }
32698156583SEwan Crawford 
32798156583SEwan Crawford     return Searcher::eCallbackReturnContinue;
32898156583SEwan Crawford }
32998156583SEwan Crawford 
3305ec532a9SColin Riley void
3315ec532a9SColin Riley RenderScriptRuntime::Initialize()
3325ec532a9SColin Riley {
3334640cde1SColin Riley     PluginManager::RegisterPlugin(GetPluginNameStatic(), "RenderScript language support", CreateInstance, GetCommandObject);
3345ec532a9SColin Riley }
3355ec532a9SColin Riley 
3365ec532a9SColin Riley void
3375ec532a9SColin Riley RenderScriptRuntime::Terminate()
3385ec532a9SColin Riley {
3395ec532a9SColin Riley     PluginManager::UnregisterPlugin(CreateInstance);
3405ec532a9SColin Riley }
3415ec532a9SColin Riley 
3425ec532a9SColin Riley lldb_private::ConstString
3435ec532a9SColin Riley RenderScriptRuntime::GetPluginNameStatic()
3445ec532a9SColin Riley {
3455ec532a9SColin Riley     static ConstString g_name("renderscript");
3465ec532a9SColin Riley     return g_name;
3475ec532a9SColin Riley }
3485ec532a9SColin Riley 
349ef20b08fSColin Riley RenderScriptRuntime::ModuleKind
350ef20b08fSColin Riley RenderScriptRuntime::GetModuleKind(const lldb::ModuleSP &module_sp)
351ef20b08fSColin Riley {
352ef20b08fSColin Riley     if (module_sp)
353ef20b08fSColin Riley     {
354ef20b08fSColin Riley         // Is this a module containing renderscript kernels?
355ef20b08fSColin Riley         const Symbol *info_sym = module_sp->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData);
356ef20b08fSColin Riley         if (info_sym)
357ef20b08fSColin Riley         {
358ef20b08fSColin Riley             return eModuleKindKernelObj;
359ef20b08fSColin Riley         }
3604640cde1SColin Riley 
3614640cde1SColin Riley         // Is this the main RS runtime library
3624640cde1SColin Riley         const ConstString rs_lib("libRS.so");
3634640cde1SColin Riley         if (module_sp->GetFileSpec().GetFilename() == rs_lib)
3644640cde1SColin Riley         {
3654640cde1SColin Riley             return eModuleKindLibRS;
3664640cde1SColin Riley         }
3674640cde1SColin Riley 
3684640cde1SColin Riley         const ConstString rs_driverlib("libRSDriver.so");
3694640cde1SColin Riley         if (module_sp->GetFileSpec().GetFilename() == rs_driverlib)
3704640cde1SColin Riley         {
3714640cde1SColin Riley             return eModuleKindDriver;
3724640cde1SColin Riley         }
3734640cde1SColin Riley 
37415f2bd95SEwan Crawford         const ConstString rs_cpureflib("libRSCpuRef.so");
3754640cde1SColin Riley         if (module_sp->GetFileSpec().GetFilename() == rs_cpureflib)
3764640cde1SColin Riley         {
3774640cde1SColin Riley             return eModuleKindImpl;
3784640cde1SColin Riley         }
3794640cde1SColin Riley 
380ef20b08fSColin Riley     }
381ef20b08fSColin Riley     return eModuleKindIgnored;
382ef20b08fSColin Riley }
383ef20b08fSColin Riley 
384ef20b08fSColin Riley bool
385ef20b08fSColin Riley RenderScriptRuntime::IsRenderScriptModule(const lldb::ModuleSP &module_sp)
386ef20b08fSColin Riley {
387ef20b08fSColin Riley     return GetModuleKind(module_sp) != eModuleKindIgnored;
388ef20b08fSColin Riley }
389ef20b08fSColin Riley 
390ef20b08fSColin Riley 
391ef20b08fSColin Riley void
392ef20b08fSColin Riley RenderScriptRuntime::ModulesDidLoad(const ModuleList &module_list )
393ef20b08fSColin Riley {
394ef20b08fSColin Riley     Mutex::Locker locker (module_list.GetMutex ());
395ef20b08fSColin Riley 
396ef20b08fSColin Riley     size_t num_modules = module_list.GetSize();
397ef20b08fSColin Riley     for (size_t i = 0; i < num_modules; i++)
398ef20b08fSColin Riley     {
399ef20b08fSColin Riley         auto mod = module_list.GetModuleAtIndex (i);
400ef20b08fSColin Riley         if (IsRenderScriptModule (mod))
401ef20b08fSColin Riley         {
402ef20b08fSColin Riley             LoadModule(mod);
403ef20b08fSColin Riley         }
404ef20b08fSColin Riley     }
405ef20b08fSColin Riley }
406ef20b08fSColin Riley 
407ef20b08fSColin Riley 
4085ec532a9SColin Riley //------------------------------------------------------------------
4095ec532a9SColin Riley // PluginInterface protocol
4105ec532a9SColin Riley //------------------------------------------------------------------
4115ec532a9SColin Riley lldb_private::ConstString
4125ec532a9SColin Riley RenderScriptRuntime::GetPluginName()
4135ec532a9SColin Riley {
4145ec532a9SColin Riley     return GetPluginNameStatic();
4155ec532a9SColin Riley }
4165ec532a9SColin Riley 
4175ec532a9SColin Riley uint32_t
4185ec532a9SColin Riley RenderScriptRuntime::GetPluginVersion()
4195ec532a9SColin Riley {
4205ec532a9SColin Riley     return 1;
4215ec532a9SColin Riley }
4225ec532a9SColin Riley 
4235ec532a9SColin Riley bool
4245ec532a9SColin Riley RenderScriptRuntime::IsVTableName(const char *name)
4255ec532a9SColin Riley {
4265ec532a9SColin Riley     return false;
4275ec532a9SColin Riley }
4285ec532a9SColin Riley 
4295ec532a9SColin Riley bool
4305ec532a9SColin Riley RenderScriptRuntime::GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic,
4310b6003f3SEnrico Granata                                               TypeAndOrName &class_type_or_name, Address &address,
4320b6003f3SEnrico Granata                                               Value::ValueType &value_type)
4335ec532a9SColin Riley {
4345ec532a9SColin Riley     return false;
4355ec532a9SColin Riley }
4365ec532a9SColin Riley 
437c74275bcSEnrico Granata TypeAndOrName
438c74275bcSEnrico Granata RenderScriptRuntime::FixUpDynamicType (const TypeAndOrName& type_and_or_name,
4397eed4877SEnrico Granata                                        ValueObject& static_value)
440c74275bcSEnrico Granata {
441c74275bcSEnrico Granata     return type_and_or_name;
442c74275bcSEnrico Granata }
443c74275bcSEnrico Granata 
4445ec532a9SColin Riley bool
4455ec532a9SColin Riley RenderScriptRuntime::CouldHaveDynamicValue(ValueObject &in_value)
4465ec532a9SColin Riley {
4475ec532a9SColin Riley     return false;
4485ec532a9SColin Riley }
4495ec532a9SColin Riley 
4505ec532a9SColin Riley lldb::BreakpointResolverSP
4515ec532a9SColin Riley RenderScriptRuntime::CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp)
4525ec532a9SColin Riley {
4535ec532a9SColin Riley     BreakpointResolverSP resolver_sp;
4545ec532a9SColin Riley     return resolver_sp;
4555ec532a9SColin Riley }
4565ec532a9SColin Riley 
4574640cde1SColin Riley 
4584640cde1SColin Riley const RenderScriptRuntime::HookDefn RenderScriptRuntime::s_runtimeHookDefns[] =
4594640cde1SColin Riley {
4604640cde1SColin Riley     //rsdScript
46182780287SAidan Dodds     {
46282780287SAidan Dodds         "rsdScriptInit", //name
46382780287SAidan Dodds         "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhjj", // symbol name 32 bit
46482780287SAidan Dodds         "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhmj", // symbol name 64 bit
46582780287SAidan Dodds         0, // version
46682780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
46782780287SAidan Dodds         &lldb_private::RenderScriptRuntime::CaptureScriptInit1 // handler
46882780287SAidan Dodds     },
46982780287SAidan Dodds     {
47082780287SAidan Dodds         "rsdScriptInvokeForEach", // name
47182780287SAidan Dodds         "_Z22rsdScriptInvokeForEachPKN7android12renderscript7ContextEPNS0_6ScriptEjPKNS0_10AllocationEPS6_PKvjPK12RsScriptCall", // symbol name 32bit
47282780287SAidan Dodds         "_Z22rsdScriptInvokeForEachPKN7android12renderscript7ContextEPNS0_6ScriptEjPKNS0_10AllocationEPS6_PKvmPK12RsScriptCall", // symbol name 64bit
47382780287SAidan Dodds         0, // version
47482780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
47582780287SAidan Dodds         nullptr // handler
47682780287SAidan Dodds     },
47782780287SAidan Dodds     {
47882780287SAidan Dodds         "rsdScriptInvokeForEachMulti", // name
47982780287SAidan Dodds         "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEjPS6_PKvjPK12RsScriptCall", // symbol name 32bit
48082780287SAidan Dodds         "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEmPS6_PKvmPK12RsScriptCall", // symbol name 64bit
48182780287SAidan Dodds         0, // version
48282780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
48382780287SAidan Dodds         nullptr // handler
48482780287SAidan Dodds     },
48582780287SAidan Dodds     {
48682780287SAidan Dodds         "rsdScriptInvokeFunction", // name
48782780287SAidan Dodds         "_Z23rsdScriptInvokeFunctionPKN7android12renderscript7ContextEPNS0_6ScriptEjPKvj", // symbol name 32bit
48882780287SAidan Dodds         "_Z23rsdScriptInvokeFunctionPKN7android12renderscript7ContextEPNS0_6ScriptEjPKvm", // symbol name 64bit
48982780287SAidan Dodds         0, // version
49082780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
49182780287SAidan Dodds         nullptr // handler
49282780287SAidan Dodds     },
49382780287SAidan Dodds     {
49482780287SAidan Dodds         "rsdScriptSetGlobalVar", // name
49582780287SAidan Dodds         "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvj", // symbol name 32bit
49682780287SAidan Dodds         "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvm", // symbol name 64bit
49782780287SAidan Dodds         0, // version
49882780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
49982780287SAidan Dodds         &lldb_private::RenderScriptRuntime::CaptureSetGlobalVar1 // handler
50082780287SAidan Dodds     },
5014640cde1SColin Riley 
5024640cde1SColin Riley     //rsdAllocation
50382780287SAidan Dodds     {
50482780287SAidan Dodds         "rsdAllocationInit", // name
50582780287SAidan Dodds         "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", // symbol name 32bit
50682780287SAidan Dodds         "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", // symbol name 64bit
50782780287SAidan Dodds         0, // version
50882780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
50982780287SAidan Dodds         &lldb_private::RenderScriptRuntime::CaptureAllocationInit1 // handler
51082780287SAidan Dodds     },
51182780287SAidan Dodds     {
51282780287SAidan Dodds         "rsdAllocationRead2D", //name
51382780287SAidan Dodds         "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvjj", // symbol name 32bit
51482780287SAidan Dodds         "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvmm", // symbol name 64bit
51582780287SAidan Dodds         0, // version
51682780287SAidan Dodds         RenderScriptRuntime::eModuleKindDriver, // type
51782780287SAidan Dodds         nullptr // handler
51882780287SAidan Dodds     },
5194640cde1SColin Riley };
5204640cde1SColin Riley const size_t RenderScriptRuntime::s_runtimeHookCount = sizeof(s_runtimeHookDefns)/sizeof(s_runtimeHookDefns[0]);
5214640cde1SColin Riley 
5224640cde1SColin Riley 
5234640cde1SColin Riley bool
5244640cde1SColin Riley RenderScriptRuntime::HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
5254640cde1SColin Riley {
5264640cde1SColin Riley     RuntimeHook* hook_info = (RuntimeHook*)baton;
5274640cde1SColin Riley     ExecutionContext context(ctx->exe_ctx_ref);
5284640cde1SColin Riley 
5294640cde1SColin Riley     RenderScriptRuntime *lang_rt = (RenderScriptRuntime *)context.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
5304640cde1SColin Riley 
5314640cde1SColin Riley     lang_rt->HookCallback(hook_info, context);
5324640cde1SColin Riley 
5334640cde1SColin Riley     return false;
5344640cde1SColin Riley }
5354640cde1SColin Riley 
5364640cde1SColin Riley 
5374640cde1SColin Riley void
5384640cde1SColin Riley RenderScriptRuntime::HookCallback(RuntimeHook* hook_info, ExecutionContext& context)
5394640cde1SColin Riley {
5404640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
5414640cde1SColin Riley 
5424640cde1SColin Riley     if (log)
5434640cde1SColin Riley         log->Printf ("RenderScriptRuntime::HookCallback - '%s' .", hook_info->defn->name);
5444640cde1SColin Riley 
5454640cde1SColin Riley     if (hook_info->defn->grabber)
5464640cde1SColin Riley     {
5474640cde1SColin Riley         (this->*(hook_info->defn->grabber))(hook_info, context);
5484640cde1SColin Riley     }
5494640cde1SColin Riley }
5504640cde1SColin Riley 
5514640cde1SColin Riley 
5524640cde1SColin Riley bool
55382780287SAidan Dodds RenderScriptRuntime::GetArgSimple(ExecutionContext &context, uint32_t arg, uint64_t *data)
5544640cde1SColin Riley {
5554640cde1SColin Riley     if (!data)
5564640cde1SColin Riley         return false;
5574640cde1SColin Riley 
55882780287SAidan Dodds     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
5594640cde1SColin Riley     Error error;
5604640cde1SColin Riley     RegisterContext* reg_ctx = context.GetRegisterContext();
5614640cde1SColin Riley     Process* process = context.GetProcessPtr();
56282780287SAidan Dodds     bool success = false; // return value
5634640cde1SColin Riley 
56482780287SAidan Dodds     if (!context.GetTargetPtr())
56582780287SAidan Dodds     {
56682780287SAidan Dodds         if (log)
56782780287SAidan Dodds             log->Printf("RenderScriptRuntime::GetArgSimple - Invalid target");
56882780287SAidan Dodds 
56982780287SAidan Dodds         return false;
57082780287SAidan Dodds     }
57182780287SAidan Dodds 
57282780287SAidan Dodds     switch (context.GetTargetPtr()->GetArchitecture().GetMachine())
57382780287SAidan Dodds     {
57482780287SAidan Dodds         case llvm::Triple::ArchType::x86:
5754640cde1SColin Riley         {
5764640cde1SColin Riley             uint64_t sp = reg_ctx->GetSP();
5774640cde1SColin Riley             uint32_t offset = (1 + arg) * sizeof(uint32_t);
57882780287SAidan Dodds             uint32_t result = 0;
57982780287SAidan Dodds             process->ReadMemory(sp + offset, &result, sizeof(uint32_t), error);
5804640cde1SColin Riley             if (error.Fail())
5814640cde1SColin Riley             {
5824640cde1SColin Riley                 if (log)
58382780287SAidan Dodds                     log->Printf ("RenderScriptRuntime:: GetArgSimple - error reading X86 stack: %s.", error.AsCString());
5844640cde1SColin Riley             }
58582780287SAidan Dodds             else
5864640cde1SColin Riley             {
58782780287SAidan Dodds                 *data = result;
58882780287SAidan Dodds                 success = true;
58982780287SAidan Dodds             }
59082780287SAidan Dodds 
59182780287SAidan Dodds             break;
59282780287SAidan Dodds         }
59382780287SAidan Dodds         case llvm::Triple::ArchType::arm:
59482780287SAidan Dodds         {
59582780287SAidan Dodds             // arm 32 bit
5964640cde1SColin Riley             if (arg < 4)
5974640cde1SColin Riley             {
5984640cde1SColin Riley                 const RegisterInfo* rArg = reg_ctx->GetRegisterInfoAtIndex(arg);
5994640cde1SColin Riley                 RegisterValue rVal;
6004640cde1SColin Riley                 reg_ctx->ReadRegister(rArg, rVal);
6014640cde1SColin Riley                 (*data) = rVal.GetAsUInt32();
60282780287SAidan Dodds                 success = true;
6034640cde1SColin Riley             }
6044640cde1SColin Riley             else
6054640cde1SColin Riley             {
6064640cde1SColin Riley                 uint64_t sp = reg_ctx->GetSP();
6074640cde1SColin Riley                 {
6084640cde1SColin Riley                     uint32_t offset = (arg-4) * sizeof(uint32_t);
6094640cde1SColin Riley                     process->ReadMemory(sp + offset, &data, sizeof(uint32_t), error);
6104640cde1SColin Riley                     if (error.Fail())
6114640cde1SColin Riley                     {
6124640cde1SColin Riley                         if (log)
61382780287SAidan Dodds                             log->Printf ("RenderScriptRuntime:: GetArgSimple - error reading ARM stack: %s.", error.AsCString());
61482780287SAidan Dodds                     }
61582780287SAidan Dodds                     else
61682780287SAidan Dodds                     {
61782780287SAidan Dodds                         success = true;
6184640cde1SColin Riley                     }
6194640cde1SColin Riley                 }
6204640cde1SColin Riley             }
62182780287SAidan Dodds 
62282780287SAidan Dodds             break;
6234640cde1SColin Riley         }
62482780287SAidan Dodds         case llvm::Triple::ArchType::aarch64:
62582780287SAidan Dodds         {
62682780287SAidan Dodds             // arm 64 bit
62782780287SAidan Dodds             // first 8 arguments are in the registers
62882780287SAidan Dodds             if (arg < 8)
62982780287SAidan Dodds             {
63082780287SAidan Dodds                 const RegisterInfo* rArg = reg_ctx->GetRegisterInfoAtIndex(arg);
63182780287SAidan Dodds                 RegisterValue rVal;
63282780287SAidan Dodds                 success = reg_ctx->ReadRegister(rArg, rVal);
63382780287SAidan Dodds                 if (success)
63482780287SAidan Dodds                 {
63582780287SAidan Dodds                     *data = rVal.GetAsUInt64();
63682780287SAidan Dodds                 }
63782780287SAidan Dodds                 else
63882780287SAidan Dodds                 {
63982780287SAidan Dodds                     if (log)
64082780287SAidan Dodds                         log->Printf("RenderScriptRuntime::GetArgSimple() - AARCH64 - Error while reading the argument #%d", arg);
64182780287SAidan Dodds                 }
64282780287SAidan Dodds             }
64382780287SAidan Dodds             else
64482780287SAidan Dodds             {
64582780287SAidan Dodds                 // @TODO: need to find the argument in the stack
64682780287SAidan Dodds                 if (log)
64782780287SAidan Dodds                     log->Printf("RenderScriptRuntime::GetArgSimple - AARCH64 - FOR #ARG >= 8 NOT IMPLEMENTED YET. Argument number: %d", arg);
64882780287SAidan Dodds             }
64982780287SAidan Dodds             break;
65082780287SAidan Dodds         }
65182780287SAidan Dodds         default:
65282780287SAidan Dodds         {
65382780287SAidan Dodds             // invalid architecture
65482780287SAidan Dodds             if (log)
65582780287SAidan Dodds                 log->Printf("RenderScriptRuntime::GetArgSimple - Architecture not supported");
65682780287SAidan Dodds 
65782780287SAidan Dodds         }
65882780287SAidan Dodds     }
65982780287SAidan Dodds 
66082780287SAidan Dodds 
66182780287SAidan Dodds     return success;
6624640cde1SColin Riley }
6634640cde1SColin Riley 
6644640cde1SColin Riley void
6654640cde1SColin Riley RenderScriptRuntime::CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionContext& context)
6664640cde1SColin Riley {
6674640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
6684640cde1SColin Riley 
6694640cde1SColin Riley     //Context, Script, int, data, length
6704640cde1SColin Riley 
67182780287SAidan Dodds     uint64_t rs_context_u64 = 0U;
67282780287SAidan Dodds     uint64_t rs_script_u64 = 0U;
67382780287SAidan Dodds     uint64_t rs_id_u64 = 0U;
67482780287SAidan Dodds     uint64_t rs_data_u64 = 0U;
67582780287SAidan Dodds     uint64_t rs_length_u64 = 0U;
6764640cde1SColin Riley 
67782780287SAidan Dodds     bool success =
67882780287SAidan Dodds         GetArgSimple(context, 0, &rs_context_u64) &&
67982780287SAidan Dodds         GetArgSimple(context, 1, &rs_script_u64) &&
68082780287SAidan Dodds         GetArgSimple(context, 2, &rs_id_u64) &&
68182780287SAidan Dodds         GetArgSimple(context, 3, &rs_data_u64) &&
68282780287SAidan Dodds         GetArgSimple(context, 4, &rs_length_u64);
6834640cde1SColin Riley 
68482780287SAidan Dodds     if (!success)
68582780287SAidan Dodds     {
68682780287SAidan Dodds         if (log)
68782780287SAidan Dodds             log->Printf("RenderScriptRuntime::CaptureSetGlobalVar1 - Error while reading the function parameters");
68882780287SAidan Dodds         return;
68982780287SAidan Dodds     }
6904640cde1SColin Riley 
6914640cde1SColin Riley     if (log)
6924640cde1SColin Riley     {
6934640cde1SColin Riley         log->Printf ("RenderScriptRuntime::CaptureSetGlobalVar1 - 0x%" PRIx64 ",0x%" PRIx64 " slot %" PRIu64 " = 0x%" PRIx64 ":%" PRIu64 "bytes.",
69482780287SAidan Dodds                         rs_context_u64, rs_script_u64, rs_id_u64, rs_data_u64, rs_length_u64);
6954640cde1SColin Riley 
69682780287SAidan Dodds         addr_t script_addr =  (addr_t)rs_script_u64;
6974640cde1SColin Riley         if (m_scriptMappings.find( script_addr ) != m_scriptMappings.end())
6984640cde1SColin Riley         {
6994640cde1SColin Riley             auto rsm = m_scriptMappings[script_addr];
70082780287SAidan Dodds             if (rs_id_u64 < rsm->m_globals.size())
7014640cde1SColin Riley             {
70282780287SAidan Dodds                 auto rsg = rsm->m_globals[rs_id_u64];
7034640cde1SColin Riley                 log->Printf ("RenderScriptRuntime::CaptureSetGlobalVar1 - Setting of '%s' within '%s' inferred", rsg.m_name.AsCString(),
7044640cde1SColin Riley                                 rsm->m_module->GetFileSpec().GetFilename().AsCString());
7054640cde1SColin Riley             }
7064640cde1SColin Riley         }
7074640cde1SColin Riley     }
7084640cde1SColin Riley }
7094640cde1SColin Riley 
7104640cde1SColin Riley void
7114640cde1SColin Riley RenderScriptRuntime::CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionContext& context)
7124640cde1SColin Riley {
7134640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
7144640cde1SColin Riley 
7154640cde1SColin Riley     //Context, Alloc, bool
7164640cde1SColin Riley 
71782780287SAidan Dodds     uint64_t rs_context_u64 = 0U;
71882780287SAidan Dodds     uint64_t rs_alloc_u64 = 0U;
71982780287SAidan Dodds     uint64_t rs_forceZero_u64 = 0U;
7204640cde1SColin Riley 
72182780287SAidan Dodds     bool success =
72282780287SAidan Dodds         GetArgSimple(context, 0, &rs_context_u64) &&
72382780287SAidan Dodds         GetArgSimple(context, 1, &rs_alloc_u64) &&
72482780287SAidan Dodds         GetArgSimple(context, 2, &rs_forceZero_u64);
72582780287SAidan Dodds     if (!success) // error case
72682780287SAidan Dodds     {
72782780287SAidan Dodds         if (log)
72882780287SAidan Dodds             log->Printf("RenderScriptRuntime::CaptureAllocationInit1 - Error while reading the function parameters");
72982780287SAidan Dodds         return; // abort
73082780287SAidan Dodds     }
7314640cde1SColin Riley 
7324640cde1SColin Riley     if (log)
7334640cde1SColin Riley         log->Printf ("RenderScriptRuntime::CaptureAllocationInit1 - 0x%" PRIx64 ",0x%" PRIx64 ",0x%" PRIx64 " .",
73482780287SAidan Dodds                         rs_context_u64, rs_alloc_u64, rs_forceZero_u64);
73578f339d1SEwan Crawford 
73678f339d1SEwan Crawford     AllocationDetails* alloc = LookUpAllocation(rs_alloc_u64, true);
73778f339d1SEwan Crawford     if (alloc)
73878f339d1SEwan Crawford         alloc->context = rs_context_u64;
7394640cde1SColin Riley }
7404640cde1SColin Riley 
7414640cde1SColin Riley void
7424640cde1SColin Riley RenderScriptRuntime::CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext& context)
7434640cde1SColin Riley {
7444640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
7454640cde1SColin Riley 
7464640cde1SColin Riley     //Context, Script, resname Str, cachedir Str
7474640cde1SColin Riley     Error error;
7484640cde1SColin Riley     Process* process = context.GetProcessPtr();
7494640cde1SColin Riley 
75082780287SAidan Dodds     uint64_t rs_context_u64 = 0U;
75182780287SAidan Dodds     uint64_t rs_script_u64 = 0U;
75282780287SAidan Dodds     uint64_t rs_resnameptr_u64 = 0U;
75382780287SAidan Dodds     uint64_t rs_cachedirptr_u64 = 0U;
7544640cde1SColin Riley 
7554640cde1SColin Riley     std::string resname;
7564640cde1SColin Riley     std::string cachedir;
7574640cde1SColin Riley 
75882780287SAidan Dodds     // read the function parameters
75982780287SAidan Dodds     bool success =
76082780287SAidan Dodds         GetArgSimple(context, 0, &rs_context_u64) &&
76182780287SAidan Dodds         GetArgSimple(context, 1, &rs_script_u64) &&
76282780287SAidan Dodds         GetArgSimple(context, 2, &rs_resnameptr_u64) &&
76382780287SAidan Dodds         GetArgSimple(context, 3, &rs_cachedirptr_u64);
7644640cde1SColin Riley 
76582780287SAidan Dodds     if (!success)
76682780287SAidan Dodds     {
76782780287SAidan Dodds         if (log)
76882780287SAidan Dodds             log->Printf("RenderScriptRuntime::CaptureScriptInit1 - Error while reading the function parameters");
76982780287SAidan Dodds         return;
77082780287SAidan Dodds     }
77182780287SAidan Dodds 
77282780287SAidan Dodds     process->ReadCStringFromMemory((lldb::addr_t)rs_resnameptr_u64, resname, error);
7734640cde1SColin Riley     if (error.Fail())
7744640cde1SColin Riley     {
7754640cde1SColin Riley         if (log)
7764640cde1SColin Riley             log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - error reading resname: %s.", error.AsCString());
7774640cde1SColin Riley 
7784640cde1SColin Riley     }
7794640cde1SColin Riley 
78082780287SAidan Dodds     process->ReadCStringFromMemory((lldb::addr_t)rs_cachedirptr_u64, cachedir, error);
7814640cde1SColin Riley     if (error.Fail())
7824640cde1SColin Riley     {
7834640cde1SColin Riley         if (log)
7844640cde1SColin Riley             log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - error reading cachedir: %s.", error.AsCString());
7854640cde1SColin Riley     }
7864640cde1SColin Riley 
7874640cde1SColin Riley     if (log)
7884640cde1SColin Riley         log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - 0x%" PRIx64 ",0x%" PRIx64 " => '%s' at '%s' .",
78982780287SAidan Dodds                      rs_context_u64, rs_script_u64, resname.c_str(), cachedir.c_str());
7904640cde1SColin Riley 
7914640cde1SColin Riley     if (resname.size() > 0)
7924640cde1SColin Riley     {
7934640cde1SColin Riley         StreamString strm;
7944640cde1SColin Riley         strm.Printf("librs.%s.so", resname.c_str());
7954640cde1SColin Riley 
79678f339d1SEwan Crawford         ScriptDetails* script = LookUpScript(rs_script_u64, true);
79778f339d1SEwan Crawford         if (script)
79878f339d1SEwan Crawford         {
79978f339d1SEwan Crawford             script->type = ScriptDetails::eScriptC;
80078f339d1SEwan Crawford             script->cacheDir = cachedir;
80178f339d1SEwan Crawford             script->resName = resname;
80278f339d1SEwan Crawford             script->scriptDyLib = strm.GetData();
80378f339d1SEwan Crawford             script->context = addr_t(rs_context_u64);
80478f339d1SEwan Crawford         }
8054640cde1SColin Riley 
8064640cde1SColin Riley         if (log)
8074640cde1SColin Riley             log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - '%s' tagged with context 0x%" PRIx64 " and script 0x%" PRIx64 ".",
80882780287SAidan Dodds                          strm.GetData(), rs_context_u64, rs_script_u64);
8094640cde1SColin Riley     }
8104640cde1SColin Riley     else if (log)
8114640cde1SColin Riley     {
8124640cde1SColin Riley         log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - resource name invalid, Script not tagged");
8134640cde1SColin Riley     }
8144640cde1SColin Riley 
8154640cde1SColin Riley }
8164640cde1SColin Riley 
8174640cde1SColin Riley void
8184640cde1SColin Riley RenderScriptRuntime::LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind)
8194640cde1SColin Riley {
8204640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
8214640cde1SColin Riley 
8224640cde1SColin Riley     if (!module)
8234640cde1SColin Riley     {
8244640cde1SColin Riley         return;
8254640cde1SColin Riley     }
8264640cde1SColin Riley 
82782780287SAidan Dodds     Target &target = GetProcess()->GetTarget();
82882780287SAidan Dodds     llvm::Triple::ArchType targetArchType = target.GetArchitecture().GetMachine();
82982780287SAidan Dodds 
83082780287SAidan Dodds     if (targetArchType != llvm::Triple::ArchType::x86
83182780287SAidan Dodds         && targetArchType != llvm::Triple::ArchType::arm
83282780287SAidan Dodds         && targetArchType != llvm::Triple::ArchType::aarch64)
8334640cde1SColin Riley     {
8344640cde1SColin Riley         if (log)
8354640cde1SColin Riley             log->Printf ("RenderScriptRuntime::LoadRuntimeHooks - Unable to hook runtime. Only X86, ARM supported currently.");
8364640cde1SColin Riley 
8374640cde1SColin Riley         return;
8384640cde1SColin Riley     }
8394640cde1SColin Riley 
84082780287SAidan Dodds     uint32_t archByteSize = target.GetArchitecture().GetAddressByteSize();
8414640cde1SColin Riley 
8424640cde1SColin Riley     for (size_t idx = 0; idx < s_runtimeHookCount; idx++)
8434640cde1SColin Riley     {
8444640cde1SColin Riley         const HookDefn* hook_defn = &s_runtimeHookDefns[idx];
8454640cde1SColin Riley         if (hook_defn->kind != kind) {
8464640cde1SColin Riley             continue;
8474640cde1SColin Riley         }
8484640cde1SColin Riley 
84982780287SAidan Dodds         const char* symbol_name = (archByteSize == 4) ? hook_defn->symbol_name_m32 : hook_defn->symbol_name_m64;
85082780287SAidan Dodds 
85182780287SAidan Dodds         const Symbol *sym = module->FindFirstSymbolWithNameAndType(ConstString(symbol_name), eSymbolTypeCode);
85282780287SAidan Dodds         if (!sym){
85382780287SAidan Dodds             if (log){
85482780287SAidan Dodds                 log->Printf("RenderScriptRuntime::LoadRuntimeHooks - ERROR: Symbol '%s' related to the function %s not found", symbol_name, hook_defn->name);
85582780287SAidan Dodds             }
85682780287SAidan Dodds             continue;
85782780287SAidan Dodds         }
8584640cde1SColin Riley 
859358cf1eaSGreg Clayton         addr_t addr = sym->GetLoadAddress(&target);
8604640cde1SColin Riley         if (addr == LLDB_INVALID_ADDRESS)
8614640cde1SColin Riley         {
8624640cde1SColin Riley             if (log)
8634640cde1SColin Riley                 log->Printf ("RenderScriptRuntime::LoadRuntimeHooks - Unable to resolve the address of hook function '%s' with symbol '%s'.",
86482780287SAidan Dodds                              hook_defn->name, symbol_name);
8654640cde1SColin Riley             continue;
8664640cde1SColin Riley         }
86782780287SAidan Dodds         else
86882780287SAidan Dodds         {
86982780287SAidan Dodds             if (log)
87082780287SAidan Dodds                 log->Printf("RenderScriptRuntime::LoadRuntimeHooks - Function %s, address resolved at 0x%" PRIx64, hook_defn->name, addr);
87182780287SAidan Dodds         }
8724640cde1SColin Riley 
8734640cde1SColin Riley         RuntimeHookSP hook(new RuntimeHook());
8744640cde1SColin Riley         hook->address = addr;
8754640cde1SColin Riley         hook->defn = hook_defn;
8764640cde1SColin Riley         hook->bp_sp = target.CreateBreakpoint(addr, true, false);
8774640cde1SColin Riley         hook->bp_sp->SetCallback(HookCallback, hook.get(), true);
8784640cde1SColin Riley         m_runtimeHooks[addr] = hook;
8794640cde1SColin Riley         if (log)
8804640cde1SColin Riley         {
8814640cde1SColin Riley             log->Printf ("RenderScriptRuntime::LoadRuntimeHooks - Successfully hooked '%s' in '%s' version %" PRIu64 " at 0x%" PRIx64 ".",
8824640cde1SColin Riley                 hook_defn->name, module->GetFileSpec().GetFilename().AsCString(), (uint64_t)hook_defn->version, (uint64_t)addr);
8834640cde1SColin Riley         }
8844640cde1SColin Riley     }
8854640cde1SColin Riley }
8864640cde1SColin Riley 
8874640cde1SColin Riley void
8884640cde1SColin Riley RenderScriptRuntime::FixupScriptDetails(RSModuleDescriptorSP rsmodule_sp)
8894640cde1SColin Riley {
8904640cde1SColin Riley     if (!rsmodule_sp)
8914640cde1SColin Riley         return;
8924640cde1SColin Riley 
8934640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
8944640cde1SColin Riley 
8954640cde1SColin Riley     const ModuleSP module = rsmodule_sp->m_module;
8964640cde1SColin Riley     const FileSpec& file = module->GetPlatformFileSpec();
8974640cde1SColin Riley 
89878f339d1SEwan Crawford     // Iterate over all of the scripts that we currently know of.
89978f339d1SEwan Crawford     // Note: We cant push or pop to m_scripts here or it may invalidate rs_script.
9004640cde1SColin Riley     for (const auto & rs_script : m_scripts)
9014640cde1SColin Riley     {
90278f339d1SEwan Crawford         // Extract the expected .so file path for this script.
90378f339d1SEwan Crawford         std::string dylib;
90478f339d1SEwan Crawford         if (!rs_script->scriptDyLib.get(dylib))
90578f339d1SEwan Crawford             continue;
90678f339d1SEwan Crawford 
90778f339d1SEwan Crawford         // Only proceed if the module that has loaded corresponds to this script.
90878f339d1SEwan Crawford         if (file.GetFilename() != ConstString(dylib.c_str()))
90978f339d1SEwan Crawford             continue;
91078f339d1SEwan Crawford 
91178f339d1SEwan Crawford         // Obtain the script address which we use as a key.
91278f339d1SEwan Crawford         lldb::addr_t script;
91378f339d1SEwan Crawford         if (!rs_script->script.get(script))
91478f339d1SEwan Crawford             continue;
91578f339d1SEwan Crawford 
91678f339d1SEwan Crawford         // If we have a script mapping for the current script.
91778f339d1SEwan Crawford         if (m_scriptMappings.find(script) != m_scriptMappings.end())
9184640cde1SColin Riley         {
91978f339d1SEwan Crawford             // if the module we have stored is different to the one we just received.
92078f339d1SEwan Crawford             if (m_scriptMappings[script] != rsmodule_sp)
9214640cde1SColin Riley             {
9224640cde1SColin Riley                 if (log)
9234640cde1SColin Riley                     log->Printf ("RenderScriptRuntime::FixupScriptDetails - Error: script %" PRIx64 " wants reassigned to new rsmodule '%s'.",
92478f339d1SEwan Crawford                                     (uint64_t)script, rsmodule_sp->m_module->GetFileSpec().GetFilename().AsCString());
9254640cde1SColin Riley             }
9264640cde1SColin Riley         }
92778f339d1SEwan Crawford         // We don't have a script mapping for the current script.
9284640cde1SColin Riley         else
9294640cde1SColin Riley         {
93078f339d1SEwan Crawford             // Obtain the script resource name.
93178f339d1SEwan Crawford             std::string resName;
93278f339d1SEwan Crawford             if (rs_script->resName.get(resName))
93378f339d1SEwan Crawford                 // Set the modules resource name.
93478f339d1SEwan Crawford                 rsmodule_sp->m_resname = resName;
93578f339d1SEwan Crawford             // Add Script/Module pair to map.
93678f339d1SEwan Crawford             m_scriptMappings[script] = rsmodule_sp;
9374640cde1SColin Riley             if (log)
9384640cde1SColin Riley                 log->Printf ("RenderScriptRuntime::FixupScriptDetails - script %" PRIx64 " associated with rsmodule '%s'.",
93978f339d1SEwan Crawford                                 (uint64_t)script, rsmodule_sp->m_module->GetFileSpec().GetFilename().AsCString());
9404640cde1SColin Riley         }
9414640cde1SColin Riley     }
9424640cde1SColin Riley }
9434640cde1SColin Riley 
94415f2bd95SEwan Crawford // Uses the Target API to evaluate the expression passed as a parameter to the function
94515f2bd95SEwan Crawford // The result of that expression is returned an unsigned 64 bit int, via the result* paramter.
94615f2bd95SEwan Crawford // Function returns true on success, and false on failure
94715f2bd95SEwan Crawford bool
94815f2bd95SEwan Crawford RenderScriptRuntime::EvalRSExpression(const char* expression, StackFrame* frame_ptr, uint64_t* result)
94915f2bd95SEwan Crawford {
95015f2bd95SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
95115f2bd95SEwan Crawford     if (log)
95215f2bd95SEwan Crawford         log->Printf("RenderScriptRuntime::EvalRSExpression(%s)", expression);
95315f2bd95SEwan Crawford 
95415f2bd95SEwan Crawford     ValueObjectSP expr_result;
95515f2bd95SEwan Crawford     // Perform the actual expression evaluation
95615f2bd95SEwan Crawford     GetProcess()->GetTarget().EvaluateExpression(expression, frame_ptr, expr_result);
95715f2bd95SEwan Crawford 
95815f2bd95SEwan Crawford     if (!expr_result)
95915f2bd95SEwan Crawford     {
96015f2bd95SEwan Crawford        if (log)
96115f2bd95SEwan Crawford            log->Printf("RenderScriptRuntime::EvalRSExpression -  Error: Couldn't evaluate expression");
96215f2bd95SEwan Crawford        return false;
96315f2bd95SEwan Crawford     }
96415f2bd95SEwan Crawford 
96515f2bd95SEwan Crawford     // The result of the expression is invalid
96615f2bd95SEwan Crawford     if (!expr_result->GetError().Success())
96715f2bd95SEwan Crawford     {
96815f2bd95SEwan Crawford         Error err = expr_result->GetError();
96915f2bd95SEwan Crawford         if (err.GetError() == UserExpression::kNoResult) // Expression returned void, so this is actually a success
97015f2bd95SEwan Crawford         {
97115f2bd95SEwan Crawford             if (log)
97215f2bd95SEwan Crawford                 log->Printf("RenderScriptRuntime::EvalRSExpression - Expression returned void");
97315f2bd95SEwan Crawford 
97415f2bd95SEwan Crawford             result = nullptr;
97515f2bd95SEwan Crawford             return true;
97615f2bd95SEwan Crawford         }
97715f2bd95SEwan Crawford 
97815f2bd95SEwan Crawford         if (log)
97915f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::EvalRSExpression - Error evaluating expression result: %s", err.AsCString());
98015f2bd95SEwan Crawford         return false;
98115f2bd95SEwan Crawford     }
98215f2bd95SEwan Crawford 
98315f2bd95SEwan Crawford     bool success = false;
98415f2bd95SEwan Crawford     *result = expr_result->GetValueAsUnsigned(0, &success); // We only read the result as an unsigned int.
98515f2bd95SEwan Crawford 
98615f2bd95SEwan Crawford     if (!success)
98715f2bd95SEwan Crawford     {
98815f2bd95SEwan Crawford        if (log)
98915f2bd95SEwan Crawford            log->Printf("RenderScriptRuntime::EvalRSExpression -  Error: Couldn't convert expression result to unsigned int");
99015f2bd95SEwan Crawford        return false;
99115f2bd95SEwan Crawford     }
99215f2bd95SEwan Crawford 
99315f2bd95SEwan Crawford     return true;
99415f2bd95SEwan Crawford }
99515f2bd95SEwan Crawford 
99615f2bd95SEwan Crawford // Used to index expression format strings
99715f2bd95SEwan Crawford enum ExpressionStrings
99815f2bd95SEwan Crawford {
99915f2bd95SEwan Crawford    eExprGetOffsetPtr = 0,
100015f2bd95SEwan Crawford    eExprAllocGetType,
100115f2bd95SEwan Crawford    eExprTypeDimX,
100215f2bd95SEwan Crawford    eExprTypeDimY,
100315f2bd95SEwan Crawford    eExprTypeDimZ,
100415f2bd95SEwan Crawford    eExprTypeElemPtr,
100515f2bd95SEwan Crawford    eExprElementType,
100615f2bd95SEwan Crawford    eExprElementKind,
100715f2bd95SEwan Crawford    eExprElementVec
100815f2bd95SEwan Crawford };
100915f2bd95SEwan Crawford 
101015f2bd95SEwan Crawford // Format strings containing the expressions we may need to evaluate.
101115f2bd95SEwan Crawford const char runtimeExpressions[][256] =
101215f2bd95SEwan Crawford {
101315f2bd95SEwan Crawford  // Mangled GetOffsetPointer(Allocation*, xoff, yoff, zoff, lod, cubemap)
101415f2bd95SEwan Crawford  "(int*)_Z12GetOffsetPtrPKN7android12renderscript10AllocationEjjjj23RsAllocationCubemapFace(0x%lx, %u, %u, %u, 0, 0)",
101515f2bd95SEwan Crawford 
101615f2bd95SEwan Crawford  // Type* rsaAllocationGetType(Context*, Allocation*)
101715f2bd95SEwan Crawford  "(void*)rsaAllocationGetType(0x%lx, 0x%lx)",
101815f2bd95SEwan Crawford 
101915f2bd95SEwan Crawford  // rsaTypeGetNativeData(Context*, Type*, void* typeData, size)
102015f2bd95SEwan Crawford  // Pack the data in the following way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
102115f2bd95SEwan Crawford  // mHal.state.lodCount; mHal.state.faces; mElement; into typeData
102215f2bd95SEwan Crawford  // Need to specify 32 or 64 bit for uint_t since this differs between devices
102315f2bd95SEwan Crawford  "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[0]", // X dim
102415f2bd95SEwan Crawford  "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[1]", // Y dim
102515f2bd95SEwan Crawford  "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[2]", // Z dim
102615f2bd95SEwan Crawford  "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[5]", // Element ptr
102715f2bd95SEwan Crawford 
102815f2bd95SEwan Crawford  // rsaElementGetNativeData(Context*, Element*, uint32_t* elemData,size)
102915f2bd95SEwan Crawford  // Pack mType; mKind; mNormalized; mVectorSize; NumSubElements into elemData
103015f2bd95SEwan Crawford  "uint32_t data[6]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[0]", // Type
103115f2bd95SEwan Crawford  "uint32_t data[6]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[1]", // Kind
103215f2bd95SEwan Crawford  "uint32_t data[6]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[3]"  // Vector Size
103315f2bd95SEwan Crawford };
103415f2bd95SEwan Crawford 
103515f2bd95SEwan Crawford // JITs the RS runtime for the internal data pointer of an allocation.
103615f2bd95SEwan Crawford // Is passed x,y,z coordinates for the pointer to a specific element.
103715f2bd95SEwan Crawford // Then sets the data_ptr member in Allocation with the result.
103815f2bd95SEwan Crawford // Returns true on success, false otherwise
103915f2bd95SEwan Crawford bool
104015f2bd95SEwan Crawford RenderScriptRuntime::JITDataPointer(AllocationDetails* allocation, StackFrame* frame_ptr,
104115f2bd95SEwan Crawford                                     unsigned int x, unsigned int y, unsigned int z)
104215f2bd95SEwan Crawford {
104315f2bd95SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
104415f2bd95SEwan Crawford 
104515f2bd95SEwan Crawford     if (!allocation->address.isValid())
104615f2bd95SEwan Crawford     {
104715f2bd95SEwan Crawford         if (log)
104815f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITDataPointer - Failed to find allocation details");
104915f2bd95SEwan Crawford         return false;
105015f2bd95SEwan Crawford     }
105115f2bd95SEwan Crawford 
105215f2bd95SEwan Crawford     const char* expr_cstr = runtimeExpressions[eExprGetOffsetPtr];
105315f2bd95SEwan Crawford     const int max_expr_size = 512; // Max expression size
105415f2bd95SEwan Crawford     char buffer[max_expr_size];
105515f2bd95SEwan Crawford 
105615f2bd95SEwan Crawford     int chars_written = snprintf(buffer, max_expr_size, expr_cstr, *allocation->address.get(), x, y, z);
105715f2bd95SEwan Crawford     if (chars_written < 0)
105815f2bd95SEwan Crawford     {
105915f2bd95SEwan Crawford         if (log)
106015f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITDataPointer - Encoding error in snprintf()");
106115f2bd95SEwan Crawford         return false;
106215f2bd95SEwan Crawford     }
106315f2bd95SEwan Crawford     else if (chars_written >= max_expr_size)
106415f2bd95SEwan Crawford     {
106515f2bd95SEwan Crawford         if (log)
106615f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITDataPointer - Expression too long");
106715f2bd95SEwan Crawford         return false;
106815f2bd95SEwan Crawford     }
106915f2bd95SEwan Crawford 
107015f2bd95SEwan Crawford     uint64_t result = 0;
107115f2bd95SEwan Crawford     if (!EvalRSExpression(buffer, frame_ptr, &result))
107215f2bd95SEwan Crawford         return false;
107315f2bd95SEwan Crawford 
107415f2bd95SEwan Crawford     addr_t mem_ptr = static_cast<lldb::addr_t>(result);
107515f2bd95SEwan Crawford     allocation->data_ptr = mem_ptr;
107615f2bd95SEwan Crawford 
107715f2bd95SEwan Crawford     return true;
107815f2bd95SEwan Crawford }
107915f2bd95SEwan Crawford 
108015f2bd95SEwan Crawford // JITs the RS runtime for the internal pointer to the RS Type of an allocation
108115f2bd95SEwan Crawford // Then sets the type_ptr member in Allocation with the result.
108215f2bd95SEwan Crawford // Returns true on success, false otherwise
108315f2bd95SEwan Crawford bool
108415f2bd95SEwan Crawford RenderScriptRuntime::JITTypePointer(AllocationDetails* allocation, StackFrame* frame_ptr)
108515f2bd95SEwan Crawford {
108615f2bd95SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
108715f2bd95SEwan Crawford 
108815f2bd95SEwan Crawford     if (!allocation->address.isValid() || !allocation->context.isValid())
108915f2bd95SEwan Crawford     {
109015f2bd95SEwan Crawford         if (log)
109115f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITTypePointer - Failed to find allocation details");
109215f2bd95SEwan Crawford         return false;
109315f2bd95SEwan Crawford     }
109415f2bd95SEwan Crawford 
109515f2bd95SEwan Crawford     const char* expr_cstr = runtimeExpressions[eExprAllocGetType];
109615f2bd95SEwan Crawford     const int max_expr_size = 512; // Max expression size
109715f2bd95SEwan Crawford     char buffer[max_expr_size];
109815f2bd95SEwan Crawford 
109915f2bd95SEwan Crawford     int chars_written = snprintf(buffer, max_expr_size, expr_cstr, *allocation->context.get(), *allocation->address.get());
110015f2bd95SEwan Crawford     if (chars_written < 0)
110115f2bd95SEwan Crawford     {
110215f2bd95SEwan Crawford         if (log)
110315f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITDataPointer - Encoding error in snprintf()");
110415f2bd95SEwan Crawford         return false;
110515f2bd95SEwan Crawford     }
110615f2bd95SEwan Crawford     else if (chars_written >= max_expr_size)
110715f2bd95SEwan Crawford     {
110815f2bd95SEwan Crawford         if (log)
110915f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITTypePointer - Expression too long");
111015f2bd95SEwan Crawford         return false;
111115f2bd95SEwan Crawford     }
111215f2bd95SEwan Crawford 
111315f2bd95SEwan Crawford     uint64_t result = 0;
111415f2bd95SEwan Crawford     if (!EvalRSExpression(buffer, frame_ptr, &result))
111515f2bd95SEwan Crawford         return false;
111615f2bd95SEwan Crawford 
111715f2bd95SEwan Crawford     addr_t type_ptr = static_cast<lldb::addr_t>(result);
111815f2bd95SEwan Crawford     allocation->type_ptr = type_ptr;
111915f2bd95SEwan Crawford 
112015f2bd95SEwan Crawford     return true;
112115f2bd95SEwan Crawford }
112215f2bd95SEwan Crawford 
112315f2bd95SEwan Crawford // JITs the RS runtime for information about the dimensions and type of an allocation
112415f2bd95SEwan Crawford // Then sets dimension and element_ptr members in Allocation with the result.
112515f2bd95SEwan Crawford // Returns true on success, false otherwise
112615f2bd95SEwan Crawford bool
112715f2bd95SEwan Crawford RenderScriptRuntime::JITTypePacked(AllocationDetails* allocation, StackFrame* frame_ptr)
112815f2bd95SEwan Crawford {
112915f2bd95SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
113015f2bd95SEwan Crawford 
113115f2bd95SEwan Crawford     if (!allocation->type_ptr.isValid() || !allocation->context.isValid())
113215f2bd95SEwan Crawford     {
113315f2bd95SEwan Crawford         if (log)
113415f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITTypePacked - Failed to find allocation details");
113515f2bd95SEwan Crawford         return false;
113615f2bd95SEwan Crawford     }
113715f2bd95SEwan Crawford 
113815f2bd95SEwan Crawford     // Expression is different depending on if device is 32 or 64 bit
113915f2bd95SEwan Crawford     uint32_t archByteSize = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize();
114015f2bd95SEwan Crawford     const unsigned int bits = archByteSize == 4 ? 32 : 64;
114115f2bd95SEwan Crawford 
114215f2bd95SEwan Crawford     // We want 4 elements from packed data
114315f2bd95SEwan Crawford     const unsigned int num_exprs = 4;
114415f2bd95SEwan Crawford     assert(num_exprs == (eExprTypeElemPtr - eExprTypeDimX + 1) && "Invalid number of expressions");
114515f2bd95SEwan Crawford 
114615f2bd95SEwan Crawford     const int max_expr_size = 512; // Max expression size
114715f2bd95SEwan Crawford     char buffer[num_exprs][max_expr_size];
114815f2bd95SEwan Crawford     uint64_t results[num_exprs];
114915f2bd95SEwan Crawford 
115015f2bd95SEwan Crawford     for (unsigned int i = 0; i < num_exprs; ++i)
115115f2bd95SEwan Crawford     {
115215f2bd95SEwan Crawford         int chars_written = snprintf(buffer[i], max_expr_size, runtimeExpressions[eExprTypeDimX + i], bits,
115315f2bd95SEwan Crawford                                      *allocation->context.get(), *allocation->type_ptr.get());
115415f2bd95SEwan Crawford         if (chars_written < 0)
115515f2bd95SEwan Crawford         {
115615f2bd95SEwan Crawford             if (log)
115715f2bd95SEwan Crawford                 log->Printf("RenderScriptRuntime::JITDataPointer - Encoding error in snprintf()");
115815f2bd95SEwan Crawford             return false;
115915f2bd95SEwan Crawford         }
116015f2bd95SEwan Crawford         else if (chars_written >= max_expr_size)
116115f2bd95SEwan Crawford         {
116215f2bd95SEwan Crawford             if (log)
116315f2bd95SEwan Crawford                 log->Printf("RenderScriptRuntime::JITTypePacked - Expression too long");
116415f2bd95SEwan Crawford             return false;
116515f2bd95SEwan Crawford         }
116615f2bd95SEwan Crawford 
116715f2bd95SEwan Crawford         // Perform expression evaluation
116815f2bd95SEwan Crawford         if (!EvalRSExpression(buffer[i], frame_ptr, &results[i]))
116915f2bd95SEwan Crawford             return false;
117015f2bd95SEwan Crawford     }
117115f2bd95SEwan Crawford 
117215f2bd95SEwan Crawford     // Assign results to allocation members
117315f2bd95SEwan Crawford     AllocationDetails::Dimension dims;
117415f2bd95SEwan Crawford     dims.dim_1 = static_cast<uint32_t>(results[0]);
117515f2bd95SEwan Crawford     dims.dim_2 = static_cast<uint32_t>(results[1]);
117615f2bd95SEwan Crawford     dims.dim_3 = static_cast<uint32_t>(results[2]);
117715f2bd95SEwan Crawford     allocation->dimension = dims;
117815f2bd95SEwan Crawford 
117915f2bd95SEwan Crawford     addr_t elem_ptr = static_cast<lldb::addr_t>(results[3]);
118015f2bd95SEwan Crawford     allocation->element_ptr = elem_ptr;
118115f2bd95SEwan Crawford 
118215f2bd95SEwan Crawford     if (log)
118315f2bd95SEwan Crawford         log->Printf("RenderScriptRuntime::JITTypePacked - dims (%u, %u, %u) Element*: 0x%" PRIx64,
118415f2bd95SEwan Crawford                     dims.dim_1, dims.dim_2, dims.dim_3, elem_ptr);
118515f2bd95SEwan Crawford 
118615f2bd95SEwan Crawford     return true;
118715f2bd95SEwan Crawford }
118815f2bd95SEwan Crawford 
118915f2bd95SEwan Crawford // JITs the RS runtime for information about the Element of an allocation
119015f2bd95SEwan Crawford // Then sets type, type_vec_size, and type_kind members in Allocation with the result.
119115f2bd95SEwan Crawford // Returns true on success, false otherwise
119215f2bd95SEwan Crawford bool
119315f2bd95SEwan Crawford RenderScriptRuntime::JITElementPacked(AllocationDetails* allocation, StackFrame* frame_ptr)
119415f2bd95SEwan Crawford {
119515f2bd95SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
119615f2bd95SEwan Crawford 
119715f2bd95SEwan Crawford     if (!allocation->element_ptr.isValid() || !allocation->context.isValid())
119815f2bd95SEwan Crawford     {
119915f2bd95SEwan Crawford         if (log)
120015f2bd95SEwan Crawford             log->Printf("RenderScriptRuntime::JITElementPacked - Failed to find allocation details");
120115f2bd95SEwan Crawford         return false;
120215f2bd95SEwan Crawford     }
120315f2bd95SEwan Crawford 
120415f2bd95SEwan Crawford     // We want 3 elements from packed data
120515f2bd95SEwan Crawford     const unsigned int num_exprs = 3;
120615f2bd95SEwan Crawford     assert(num_exprs == (eExprElementVec - eExprElementType + 1) && "Invalid number of expressions");
120715f2bd95SEwan Crawford 
120815f2bd95SEwan Crawford     const int max_expr_size = 512; // Max expression size
120915f2bd95SEwan Crawford     char buffer[num_exprs][max_expr_size];
121015f2bd95SEwan Crawford     uint64_t results[num_exprs];
121115f2bd95SEwan Crawford 
121215f2bd95SEwan Crawford     for (unsigned int i = 0; i < num_exprs; i++)
121315f2bd95SEwan Crawford     {
121415f2bd95SEwan Crawford         int chars_written = snprintf(buffer[i], max_expr_size, runtimeExpressions[eExprElementType + i], *allocation->context.get(), *allocation->element_ptr.get());
121515f2bd95SEwan Crawford         if (chars_written < 0)
121615f2bd95SEwan Crawford         {
121715f2bd95SEwan Crawford             if (log)
121815f2bd95SEwan Crawford                 log->Printf("RenderScriptRuntime::JITDataPointer - Encoding error in snprintf()");
121915f2bd95SEwan Crawford             return false;
122015f2bd95SEwan Crawford         }
122115f2bd95SEwan Crawford         else if (chars_written >= max_expr_size)
122215f2bd95SEwan Crawford         {
122315f2bd95SEwan Crawford             if (log)
122415f2bd95SEwan Crawford                 log->Printf("RenderScriptRuntime::JITElementPacked - Expression too long");
122515f2bd95SEwan Crawford             return false;
122615f2bd95SEwan Crawford         }
122715f2bd95SEwan Crawford 
122815f2bd95SEwan Crawford         // Perform expression evaluation
122915f2bd95SEwan Crawford         if (!EvalRSExpression(buffer[i], frame_ptr, &results[i]))
123015f2bd95SEwan Crawford             return false;
123115f2bd95SEwan Crawford     }
123215f2bd95SEwan Crawford 
123315f2bd95SEwan Crawford     // Assign results to allocation members
123415f2bd95SEwan Crawford     allocation->type = static_cast<RenderScriptRuntime::AllocationDetails::DataType>(results[0]);
123515f2bd95SEwan Crawford     allocation->type_kind = static_cast<RenderScriptRuntime::AllocationDetails::DataKind>(results[1]);
123615f2bd95SEwan Crawford     allocation->type_vec_size = static_cast<uint32_t>(results[2]);
123715f2bd95SEwan Crawford 
123815f2bd95SEwan Crawford     if (log)
123915f2bd95SEwan Crawford         log->Printf("RenderScriptRuntime::JITElementPacked - data type %u, pixel type %u, vector size %u",
124015f2bd95SEwan Crawford                     *allocation->type.get(), *allocation->type_kind.get(), *allocation->type_vec_size.get());
124115f2bd95SEwan Crawford 
124215f2bd95SEwan Crawford     return true;
124315f2bd95SEwan Crawford }
124415f2bd95SEwan Crawford 
1245*a0f08674SEwan Crawford // JITs the RS runtime for the address of the last element in the allocation.
1246*a0f08674SEwan Crawford // The `elem_size` paramter represents the size of a single element, including padding.
1247*a0f08674SEwan Crawford // Which is needed as an offset from the last element pointer.
1248*a0f08674SEwan Crawford // Using this offset minus the starting address we can calculate the size of the allocation.
1249*a0f08674SEwan Crawford // Returns true on success, false otherwise
1250*a0f08674SEwan Crawford bool
1251*a0f08674SEwan Crawford RenderScriptRuntime::JITAllocationSize(AllocationDetails* allocation, StackFrame* frame_ptr,
1252*a0f08674SEwan Crawford                                        const uint32_t elem_size)
1253*a0f08674SEwan Crawford {
1254*a0f08674SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
1255*a0f08674SEwan Crawford 
1256*a0f08674SEwan Crawford     if (!allocation->address.isValid() || !allocation->dimension.isValid()
1257*a0f08674SEwan Crawford         || !allocation->data_ptr.isValid())
1258*a0f08674SEwan Crawford     {
1259*a0f08674SEwan Crawford         if (log)
1260*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::JITAllocationSize - Failed to find allocation details");
1261*a0f08674SEwan Crawford         return false;
1262*a0f08674SEwan Crawford     }
1263*a0f08674SEwan Crawford 
1264*a0f08674SEwan Crawford     const char* expr_cstr = runtimeExpressions[eExprGetOffsetPtr];
1265*a0f08674SEwan Crawford     const int max_expr_size = 512; // Max expression size
1266*a0f08674SEwan Crawford     char buffer[max_expr_size];
1267*a0f08674SEwan Crawford 
1268*a0f08674SEwan Crawford     // Find dimensions
1269*a0f08674SEwan Crawford     unsigned int dim_x = allocation->dimension.get()->dim_1;
1270*a0f08674SEwan Crawford     unsigned int dim_y = allocation->dimension.get()->dim_2;
1271*a0f08674SEwan Crawford     unsigned int dim_z = allocation->dimension.get()->dim_3;
1272*a0f08674SEwan Crawford 
1273*a0f08674SEwan Crawford     // Calculate last element
1274*a0f08674SEwan Crawford     dim_x = dim_x == 0 ? 0 : dim_x - 1;
1275*a0f08674SEwan Crawford     dim_y = dim_y == 0 ? 0 : dim_y - 1;
1276*a0f08674SEwan Crawford     dim_z = dim_z == 0 ? 0 : dim_z - 1;
1277*a0f08674SEwan Crawford 
1278*a0f08674SEwan Crawford     int chars_written = snprintf(buffer, max_expr_size, expr_cstr, *allocation->address.get(),
1279*a0f08674SEwan Crawford                                  dim_x, dim_y, dim_z);
1280*a0f08674SEwan Crawford     if (chars_written < 0)
1281*a0f08674SEwan Crawford     {
1282*a0f08674SEwan Crawford         if (log)
1283*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::JITAllocationSize - Encoding error in snprintf()");
1284*a0f08674SEwan Crawford         return false;
1285*a0f08674SEwan Crawford     }
1286*a0f08674SEwan Crawford     else if (chars_written >= max_expr_size)
1287*a0f08674SEwan Crawford     {
1288*a0f08674SEwan Crawford         if (log)
1289*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::JITAllocationSize - Expression too long");
1290*a0f08674SEwan Crawford         return false;
1291*a0f08674SEwan Crawford     }
1292*a0f08674SEwan Crawford 
1293*a0f08674SEwan Crawford     uint64_t result = 0;
1294*a0f08674SEwan Crawford     if (!EvalRSExpression(buffer, frame_ptr, &result))
1295*a0f08674SEwan Crawford         return false;
1296*a0f08674SEwan Crawford 
1297*a0f08674SEwan Crawford     addr_t mem_ptr = static_cast<lldb::addr_t>(result);
1298*a0f08674SEwan Crawford     // Find pointer to last element and add on size of an element
1299*a0f08674SEwan Crawford     allocation->size = static_cast<uint32_t>(mem_ptr - *allocation->data_ptr.get()) + elem_size;
1300*a0f08674SEwan Crawford 
1301*a0f08674SEwan Crawford     return true;
1302*a0f08674SEwan Crawford }
1303*a0f08674SEwan Crawford 
1304*a0f08674SEwan Crawford // JITs the RS runtime for information about the stride between rows in the allocation.
1305*a0f08674SEwan Crawford // This is done to detect padding, since allocated memory is 16-byte aligned.
1306*a0f08674SEwan Crawford // Returns true on success, false otherwise
1307*a0f08674SEwan Crawford bool
1308*a0f08674SEwan Crawford RenderScriptRuntime::JITAllocationStride(AllocationDetails* allocation, StackFrame* frame_ptr)
1309*a0f08674SEwan Crawford {
1310*a0f08674SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
1311*a0f08674SEwan Crawford 
1312*a0f08674SEwan Crawford     if (!allocation->address.isValid() || !allocation->data_ptr.isValid())
1313*a0f08674SEwan Crawford     {
1314*a0f08674SEwan Crawford         if (log)
1315*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::JITAllocationStride - Failed to find allocation details");
1316*a0f08674SEwan Crawford         return false;
1317*a0f08674SEwan Crawford     }
1318*a0f08674SEwan Crawford 
1319*a0f08674SEwan Crawford     const char* expr_cstr = runtimeExpressions[eExprGetOffsetPtr];
1320*a0f08674SEwan Crawford     const int max_expr_size = 512; // Max expression size
1321*a0f08674SEwan Crawford     char buffer[max_expr_size];
1322*a0f08674SEwan Crawford 
1323*a0f08674SEwan Crawford     int chars_written = snprintf(buffer, max_expr_size, expr_cstr, *allocation->address.get(),
1324*a0f08674SEwan Crawford                                  0, 1, 0);
1325*a0f08674SEwan Crawford     if (chars_written < 0)
1326*a0f08674SEwan Crawford     {
1327*a0f08674SEwan Crawford         if (log)
1328*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::JITAllocationStride - Encoding error in snprintf()");
1329*a0f08674SEwan Crawford         return false;
1330*a0f08674SEwan Crawford     }
1331*a0f08674SEwan Crawford     else if (chars_written >= max_expr_size)
1332*a0f08674SEwan Crawford     {
1333*a0f08674SEwan Crawford         if (log)
1334*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::JITAllocationStride - Expression too long");
1335*a0f08674SEwan Crawford         return false;
1336*a0f08674SEwan Crawford     }
1337*a0f08674SEwan Crawford 
1338*a0f08674SEwan Crawford     uint64_t result = 0;
1339*a0f08674SEwan Crawford     if (!EvalRSExpression(buffer, frame_ptr, &result))
1340*a0f08674SEwan Crawford         return false;
1341*a0f08674SEwan Crawford 
1342*a0f08674SEwan Crawford     addr_t mem_ptr = static_cast<lldb::addr_t>(result);
1343*a0f08674SEwan Crawford     allocation->stride = static_cast<uint32_t>(mem_ptr - *allocation->data_ptr.get());
1344*a0f08674SEwan Crawford 
1345*a0f08674SEwan Crawford     return true;
1346*a0f08674SEwan Crawford }
1347*a0f08674SEwan Crawford 
134815f2bd95SEwan Crawford // JIT all the current runtime info regarding an allocation
134915f2bd95SEwan Crawford bool
135015f2bd95SEwan Crawford RenderScriptRuntime::RefreshAllocation(AllocationDetails* allocation, StackFrame* frame_ptr)
135115f2bd95SEwan Crawford {
135215f2bd95SEwan Crawford     // GetOffsetPointer()
135315f2bd95SEwan Crawford     if (!JITDataPointer(allocation, frame_ptr))
135415f2bd95SEwan Crawford         return false;
135515f2bd95SEwan Crawford 
135615f2bd95SEwan Crawford     // rsaAllocationGetType()
135715f2bd95SEwan Crawford     if (!JITTypePointer(allocation, frame_ptr))
135815f2bd95SEwan Crawford         return false;
135915f2bd95SEwan Crawford 
136015f2bd95SEwan Crawford     // rsaTypeGetNativeData()
136115f2bd95SEwan Crawford     if (!JITTypePacked(allocation, frame_ptr))
136215f2bd95SEwan Crawford         return false;
136315f2bd95SEwan Crawford 
136415f2bd95SEwan Crawford     // rsaElementGetNativeData()
136515f2bd95SEwan Crawford     if (!JITElementPacked(allocation, frame_ptr))
136615f2bd95SEwan Crawford         return false;
136715f2bd95SEwan Crawford 
136815f2bd95SEwan Crawford     return true;
136915f2bd95SEwan Crawford }
137015f2bd95SEwan Crawford 
13715ec532a9SColin Riley bool
13725ec532a9SColin Riley RenderScriptRuntime::LoadModule(const lldb::ModuleSP &module_sp)
13735ec532a9SColin Riley {
13744640cde1SColin Riley     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
13754640cde1SColin Riley 
13765ec532a9SColin Riley     if (module_sp)
13775ec532a9SColin Riley     {
13785ec532a9SColin Riley         for (const auto &rs_module : m_rsmodules)
13795ec532a9SColin Riley         {
13804640cde1SColin Riley             if (rs_module->m_module == module_sp)
13817dc7771cSEwan Crawford             {
13827dc7771cSEwan Crawford                 // Check if the user has enabled automatically breaking on
13837dc7771cSEwan Crawford                 // all RS kernels.
13847dc7771cSEwan Crawford                 if (m_breakAllKernels)
13857dc7771cSEwan Crawford                     BreakOnModuleKernels(rs_module);
13867dc7771cSEwan Crawford 
13875ec532a9SColin Riley                 return false;
13885ec532a9SColin Riley             }
13897dc7771cSEwan Crawford         }
1390ef20b08fSColin Riley         bool module_loaded = false;
1391ef20b08fSColin Riley         switch (GetModuleKind(module_sp))
1392ef20b08fSColin Riley         {
1393ef20b08fSColin Riley             case eModuleKindKernelObj:
1394ef20b08fSColin Riley             {
13954640cde1SColin Riley                 RSModuleDescriptorSP module_desc;
13964640cde1SColin Riley                 module_desc.reset(new RSModuleDescriptor(module_sp));
13974640cde1SColin Riley                 if (module_desc->ParseRSInfo())
13985ec532a9SColin Riley                 {
13995ec532a9SColin Riley                     m_rsmodules.push_back(module_desc);
1400ef20b08fSColin Riley                     module_loaded = true;
14015ec532a9SColin Riley                 }
14024640cde1SColin Riley                 if (module_loaded)
14034640cde1SColin Riley                 {
14044640cde1SColin Riley                     FixupScriptDetails(module_desc);
14054640cde1SColin Riley                 }
1406ef20b08fSColin Riley                 break;
1407ef20b08fSColin Riley             }
1408ef20b08fSColin Riley             case eModuleKindDriver:
14094640cde1SColin Riley             {
14104640cde1SColin Riley                 if (!m_libRSDriver)
14114640cde1SColin Riley                 {
14124640cde1SColin Riley                     m_libRSDriver = module_sp;
14134640cde1SColin Riley                     LoadRuntimeHooks(m_libRSDriver, RenderScriptRuntime::eModuleKindDriver);
14144640cde1SColin Riley                 }
14154640cde1SColin Riley                 break;
14164640cde1SColin Riley             }
1417ef20b08fSColin Riley             case eModuleKindImpl:
14184640cde1SColin Riley             {
14194640cde1SColin Riley                 m_libRSCpuRef = module_sp;
14204640cde1SColin Riley                 break;
14214640cde1SColin Riley             }
1422ef20b08fSColin Riley             case eModuleKindLibRS:
14234640cde1SColin Riley             {
14244640cde1SColin Riley                 if (!m_libRS)
14254640cde1SColin Riley                 {
14264640cde1SColin Riley                     m_libRS = module_sp;
14274640cde1SColin Riley                     static ConstString gDbgPresentStr("gDebuggerPresent");
14284640cde1SColin Riley                     const Symbol* debug_present = m_libRS->FindFirstSymbolWithNameAndType(gDbgPresentStr, eSymbolTypeData);
14294640cde1SColin Riley                     if (debug_present)
14304640cde1SColin Riley                     {
14314640cde1SColin Riley                         Error error;
14324640cde1SColin Riley                         uint32_t flag = 0x00000001U;
14334640cde1SColin Riley                         Target &target = GetProcess()->GetTarget();
1434358cf1eaSGreg Clayton                         addr_t addr = debug_present->GetLoadAddress(&target);
14354640cde1SColin Riley                         GetProcess()->WriteMemory(addr, &flag, sizeof(flag), error);
14364640cde1SColin Riley                         if(error.Success())
14374640cde1SColin Riley                         {
14384640cde1SColin Riley                             if (log)
14394640cde1SColin Riley                                 log->Printf ("RenderScriptRuntime::LoadModule - Debugger present flag set on debugee");
14404640cde1SColin Riley 
14414640cde1SColin Riley                             m_debuggerPresentFlagged = true;
14424640cde1SColin Riley                         }
14434640cde1SColin Riley                         else if (log)
14444640cde1SColin Riley                         {
14454640cde1SColin Riley                             log->Printf ("RenderScriptRuntime::LoadModule - Error writing debugger present flags '%s' ", error.AsCString());
14464640cde1SColin Riley                         }
14474640cde1SColin Riley                     }
14484640cde1SColin Riley                     else if (log)
14494640cde1SColin Riley                     {
14504640cde1SColin Riley                         log->Printf ("RenderScriptRuntime::LoadModule - Error writing debugger present flags - symbol not found");
14514640cde1SColin Riley                     }
14524640cde1SColin Riley                 }
14534640cde1SColin Riley                 break;
14544640cde1SColin Riley             }
1455ef20b08fSColin Riley             default:
1456ef20b08fSColin Riley                 break;
1457ef20b08fSColin Riley         }
1458ef20b08fSColin Riley         if (module_loaded)
1459ef20b08fSColin Riley             Update();
1460ef20b08fSColin Riley         return module_loaded;
14615ec532a9SColin Riley     }
14625ec532a9SColin Riley     return false;
14635ec532a9SColin Riley }
14645ec532a9SColin Riley 
1465ef20b08fSColin Riley void
1466ef20b08fSColin Riley RenderScriptRuntime::Update()
1467ef20b08fSColin Riley {
1468ef20b08fSColin Riley     if (m_rsmodules.size() > 0)
1469ef20b08fSColin Riley     {
1470ef20b08fSColin Riley         if (!m_initiated)
1471ef20b08fSColin Riley         {
1472ef20b08fSColin Riley             Initiate();
1473ef20b08fSColin Riley         }
1474ef20b08fSColin Riley     }
1475ef20b08fSColin Riley }
1476ef20b08fSColin Riley 
1477ef20b08fSColin Riley 
14785ec532a9SColin Riley // The maximum line length of an .rs.info packet
14795ec532a9SColin Riley #define MAXLINE 500
14805ec532a9SColin Riley 
14815ec532a9SColin Riley // The .rs.info symbol in renderscript modules contains a string which needs to be parsed.
14825ec532a9SColin Riley // The string is basic and is parsed on a line by line basis.
14835ec532a9SColin Riley bool
14845ec532a9SColin Riley RSModuleDescriptor::ParseRSInfo()
14855ec532a9SColin Riley {
14865ec532a9SColin Riley     const Symbol *info_sym = m_module->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData);
14875ec532a9SColin Riley     if (info_sym)
14885ec532a9SColin Riley     {
1489358cf1eaSGreg Clayton         const addr_t addr = info_sym->GetAddressRef().GetFileAddress();
14905ec532a9SColin Riley         const addr_t size = info_sym->GetByteSize();
14915ec532a9SColin Riley         const FileSpec fs = m_module->GetFileSpec();
14925ec532a9SColin Riley 
14935ec532a9SColin Riley         DataBufferSP buffer = fs.ReadFileContents(addr, size);
14945ec532a9SColin Riley 
14955ec532a9SColin Riley         if (!buffer)
14965ec532a9SColin Riley             return false;
14975ec532a9SColin Riley 
14985ec532a9SColin Riley         std::string info((const char *)buffer->GetBytes());
14995ec532a9SColin Riley 
15005ec532a9SColin Riley         std::vector<std::string> info_lines;
1501e8433cc1SBruce Mitchener         size_t lpos = info.find('\n');
15025ec532a9SColin Riley         while (lpos != std::string::npos)
15035ec532a9SColin Riley         {
15045ec532a9SColin Riley             info_lines.push_back(info.substr(0, lpos));
15055ec532a9SColin Riley             info = info.substr(lpos + 1);
1506e8433cc1SBruce Mitchener             lpos = info.find('\n');
15075ec532a9SColin Riley         }
15085ec532a9SColin Riley         size_t offset = 0;
15095ec532a9SColin Riley         while (offset < info_lines.size())
15105ec532a9SColin Riley         {
15115ec532a9SColin Riley             std::string line = info_lines[offset];
15125ec532a9SColin Riley             // Parse directives
15135ec532a9SColin Riley             uint32_t numDefns = 0;
15145ec532a9SColin Riley             if (sscanf(line.c_str(), "exportVarCount: %u", &numDefns) == 1)
15155ec532a9SColin Riley             {
15165ec532a9SColin Riley                 while (numDefns--)
15174640cde1SColin Riley                     m_globals.push_back(RSGlobalDescriptor(this, info_lines[++offset].c_str()));
15185ec532a9SColin Riley             }
15195ec532a9SColin Riley             else if (sscanf(line.c_str(), "exportFuncCount: %u", &numDefns) == 1)
15205ec532a9SColin Riley             {
15215ec532a9SColin Riley             }
15225ec532a9SColin Riley             else if (sscanf(line.c_str(), "exportForEachCount: %u", &numDefns) == 1)
15235ec532a9SColin Riley             {
15245ec532a9SColin Riley                 char name[MAXLINE];
15255ec532a9SColin Riley                 while (numDefns--)
15265ec532a9SColin Riley                 {
15275ec532a9SColin Riley                     uint32_t slot = 0;
15285ec532a9SColin Riley                     name[0] = '\0';
15295ec532a9SColin Riley                     if (sscanf(info_lines[++offset].c_str(), "%u - %s", &slot, &name[0]) == 2)
15305ec532a9SColin Riley                     {
15314640cde1SColin Riley                         m_kernels.push_back(RSKernelDescriptor(this, name, slot));
15324640cde1SColin Riley                     }
15334640cde1SColin Riley                 }
15344640cde1SColin Riley             }
15354640cde1SColin Riley             else if (sscanf(line.c_str(), "pragmaCount: %u", &numDefns) == 1)
15364640cde1SColin Riley             {
15374640cde1SColin Riley                 char name[MAXLINE];
15384640cde1SColin Riley                 char value[MAXLINE];
15394640cde1SColin Riley                 while (numDefns--)
15404640cde1SColin Riley                 {
15414640cde1SColin Riley                     name[0] = '\0';
15424640cde1SColin Riley                     value[0] = '\0';
15434640cde1SColin Riley                     if (sscanf(info_lines[++offset].c_str(), "%s - %s", &name[0], &value[0]) != 0
15444640cde1SColin Riley                         && (name[0] != '\0'))
15454640cde1SColin Riley                     {
15464640cde1SColin Riley                         m_pragmas[std::string(name)] = value;
15475ec532a9SColin Riley                     }
15485ec532a9SColin Riley                 }
15495ec532a9SColin Riley             }
15505ec532a9SColin Riley             else if (sscanf(line.c_str(), "objectSlotCount: %u", &numDefns) == 1)
15515ec532a9SColin Riley             {
15525ec532a9SColin Riley             }
15535ec532a9SColin Riley 
15545ec532a9SColin Riley             offset++;
15555ec532a9SColin Riley         }
15565ec532a9SColin Riley         return m_kernels.size() > 0;
15575ec532a9SColin Riley     }
15585ec532a9SColin Riley     return false;
15595ec532a9SColin Riley }
15605ec532a9SColin Riley 
15615ec532a9SColin Riley bool
15625ec532a9SColin Riley RenderScriptRuntime::ProbeModules(const ModuleList module_list)
15635ec532a9SColin Riley {
15645ec532a9SColin Riley     bool rs_found = false;
15655ec532a9SColin Riley     size_t num_modules = module_list.GetSize();
15665ec532a9SColin Riley     for (size_t i = 0; i < num_modules; i++)
15675ec532a9SColin Riley     {
15685ec532a9SColin Riley         auto module = module_list.GetModuleAtIndex(i);
15695ec532a9SColin Riley         rs_found |= LoadModule(module);
15705ec532a9SColin Riley     }
15715ec532a9SColin Riley     return rs_found;
15725ec532a9SColin Riley }
15735ec532a9SColin Riley 
15745ec532a9SColin Riley void
15754640cde1SColin Riley RenderScriptRuntime::Status(Stream &strm) const
15764640cde1SColin Riley {
15774640cde1SColin Riley     if (m_libRS)
15784640cde1SColin Riley     {
15794640cde1SColin Riley         strm.Printf("Runtime Library discovered.");
15804640cde1SColin Riley         strm.EOL();
15814640cde1SColin Riley     }
15824640cde1SColin Riley     if (m_libRSDriver)
15834640cde1SColin Riley     {
15844640cde1SColin Riley         strm.Printf("Runtime Driver discovered.");
15854640cde1SColin Riley         strm.EOL();
15864640cde1SColin Riley     }
15874640cde1SColin Riley     if (m_libRSCpuRef)
15884640cde1SColin Riley     {
15894640cde1SColin Riley         strm.Printf("CPU Reference Implementation discovered.");
15904640cde1SColin Riley         strm.EOL();
15914640cde1SColin Riley     }
15924640cde1SColin Riley 
15934640cde1SColin Riley     if (m_runtimeHooks.size())
15944640cde1SColin Riley     {
15954640cde1SColin Riley         strm.Printf("Runtime functions hooked:");
15964640cde1SColin Riley         strm.EOL();
15974640cde1SColin Riley         for (auto b : m_runtimeHooks)
15984640cde1SColin Riley         {
15994640cde1SColin Riley             strm.Indent(b.second->defn->name);
16004640cde1SColin Riley             strm.EOL();
16014640cde1SColin Riley         }
16024640cde1SColin Riley         strm.EOL();
16034640cde1SColin Riley     }
16044640cde1SColin Riley     else
16054640cde1SColin Riley     {
16064640cde1SColin Riley         strm.Printf("Runtime is not hooked.");
16074640cde1SColin Riley         strm.EOL();
16084640cde1SColin Riley     }
16094640cde1SColin Riley }
16104640cde1SColin Riley 
16114640cde1SColin Riley void
16124640cde1SColin Riley RenderScriptRuntime::DumpContexts(Stream &strm) const
16134640cde1SColin Riley {
16144640cde1SColin Riley     strm.Printf("Inferred RenderScript Contexts:");
16154640cde1SColin Riley     strm.EOL();
16164640cde1SColin Riley     strm.IndentMore();
16174640cde1SColin Riley 
16184640cde1SColin Riley     std::map<addr_t, uint64_t> contextReferences;
16194640cde1SColin Riley 
162078f339d1SEwan Crawford     // Iterate over all of the currently discovered scripts.
162178f339d1SEwan Crawford     // Note: We cant push or pop from m_scripts inside this loop or it may invalidate script.
16224640cde1SColin Riley     for (const auto & script : m_scripts)
16234640cde1SColin Riley     {
162478f339d1SEwan Crawford         if (!script->context.isValid())
162578f339d1SEwan Crawford             continue;
162678f339d1SEwan Crawford         lldb::addr_t context = *script->context;
162778f339d1SEwan Crawford 
162878f339d1SEwan Crawford         if (contextReferences.find(context) != contextReferences.end())
16294640cde1SColin Riley         {
163078f339d1SEwan Crawford             contextReferences[context]++;
16314640cde1SColin Riley         }
16324640cde1SColin Riley         else
16334640cde1SColin Riley         {
163478f339d1SEwan Crawford             contextReferences[context] = 1;
16354640cde1SColin Riley         }
16364640cde1SColin Riley     }
16374640cde1SColin Riley 
16384640cde1SColin Riley     for (const auto& cRef : contextReferences)
16394640cde1SColin Riley     {
16404640cde1SColin Riley         strm.Printf("Context 0x%" PRIx64 ": %" PRIu64 " script instances", cRef.first, cRef.second);
16414640cde1SColin Riley         strm.EOL();
16424640cde1SColin Riley     }
16434640cde1SColin Riley     strm.IndentLess();
16444640cde1SColin Riley }
16454640cde1SColin Riley 
16464640cde1SColin Riley void
16474640cde1SColin Riley RenderScriptRuntime::DumpKernels(Stream &strm) const
16484640cde1SColin Riley {
16494640cde1SColin Riley     strm.Printf("RenderScript Kernels:");
16504640cde1SColin Riley     strm.EOL();
16514640cde1SColin Riley     strm.IndentMore();
16524640cde1SColin Riley     for (const auto &module : m_rsmodules)
16534640cde1SColin Riley     {
16544640cde1SColin Riley         strm.Printf("Resource '%s':",module->m_resname.c_str());
16554640cde1SColin Riley         strm.EOL();
16564640cde1SColin Riley         for (const auto &kernel : module->m_kernels)
16574640cde1SColin Riley         {
16584640cde1SColin Riley             strm.Indent(kernel.m_name.AsCString());
16594640cde1SColin Riley             strm.EOL();
16604640cde1SColin Riley         }
16614640cde1SColin Riley     }
16624640cde1SColin Riley     strm.IndentLess();
16634640cde1SColin Riley }
16644640cde1SColin Riley 
1665*a0f08674SEwan Crawford RenderScriptRuntime::AllocationDetails*
1666*a0f08674SEwan Crawford RenderScriptRuntime::FindAllocByID(Stream &strm, const uint32_t alloc_id)
1667*a0f08674SEwan Crawford {
1668*a0f08674SEwan Crawford     AllocationDetails* alloc = nullptr;
1669*a0f08674SEwan Crawford 
1670*a0f08674SEwan Crawford     // See if we can find allocation using id as an index;
1671*a0f08674SEwan Crawford     if (alloc_id <= m_allocations.size() && alloc_id != 0
1672*a0f08674SEwan Crawford         && m_allocations[alloc_id-1]->id == alloc_id)
1673*a0f08674SEwan Crawford     {
1674*a0f08674SEwan Crawford         alloc = m_allocations[alloc_id-1].get();
1675*a0f08674SEwan Crawford         return alloc;
1676*a0f08674SEwan Crawford     }
1677*a0f08674SEwan Crawford 
1678*a0f08674SEwan Crawford     // Fallback to searching
1679*a0f08674SEwan Crawford     for (const auto & a : m_allocations)
1680*a0f08674SEwan Crawford     {
1681*a0f08674SEwan Crawford        if (a->id == alloc_id)
1682*a0f08674SEwan Crawford        {
1683*a0f08674SEwan Crawford            alloc = a.get();
1684*a0f08674SEwan Crawford            break;
1685*a0f08674SEwan Crawford        }
1686*a0f08674SEwan Crawford     }
1687*a0f08674SEwan Crawford 
1688*a0f08674SEwan Crawford     if (alloc == nullptr)
1689*a0f08674SEwan Crawford     {
1690*a0f08674SEwan Crawford         strm.Printf("Error: Couldn't find allocation with id matching %u", alloc_id);
1691*a0f08674SEwan Crawford         strm.EOL();
1692*a0f08674SEwan Crawford     }
1693*a0f08674SEwan Crawford 
1694*a0f08674SEwan Crawford     return alloc;
1695*a0f08674SEwan Crawford }
1696*a0f08674SEwan Crawford 
1697*a0f08674SEwan Crawford // Prints the contents of an allocation to the output stream, which may be a file
1698*a0f08674SEwan Crawford bool
1699*a0f08674SEwan Crawford RenderScriptRuntime::DumpAllocation(Stream &strm, StackFrame* frame_ptr, const uint32_t id)
1700*a0f08674SEwan Crawford {
1701*a0f08674SEwan Crawford     Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
1702*a0f08674SEwan Crawford 
1703*a0f08674SEwan Crawford     // Check we can find the desired allocation
1704*a0f08674SEwan Crawford     AllocationDetails* alloc = FindAllocByID(strm, id);
1705*a0f08674SEwan Crawford     if (!alloc)
1706*a0f08674SEwan Crawford         return false; // FindAllocByID() will print error message for us here
1707*a0f08674SEwan Crawford 
1708*a0f08674SEwan Crawford     if (log)
1709*a0f08674SEwan Crawford         log->Printf("RenderScriptRuntime::DumpAllocation - Found allocation 0x%" PRIx64, *alloc->address.get());
1710*a0f08674SEwan Crawford 
1711*a0f08674SEwan Crawford     // Check we have information about the allocation, if not calculate it
1712*a0f08674SEwan Crawford     if (!alloc->data_ptr.isValid() || !alloc->type.isValid() ||
1713*a0f08674SEwan Crawford         !alloc->type_vec_size.isValid() || !alloc->dimension.isValid())
1714*a0f08674SEwan Crawford     {
1715*a0f08674SEwan Crawford         if (log)
1716*a0f08674SEwan Crawford             log->Printf("RenderScriptRuntime::DumpAllocation - Allocation details not calculated yet, jitting info");
1717*a0f08674SEwan Crawford 
1718*a0f08674SEwan Crawford         // JIT all the allocation information
1719*a0f08674SEwan Crawford         if (!RefreshAllocation(alloc, frame_ptr))
1720*a0f08674SEwan Crawford         {
1721*a0f08674SEwan Crawford             strm.Printf("Error: Couldn't JIT allocation details");
1722*a0f08674SEwan Crawford             strm.EOL();
1723*a0f08674SEwan Crawford             return false;
1724*a0f08674SEwan Crawford         }
1725*a0f08674SEwan Crawford     }
1726*a0f08674SEwan Crawford 
1727*a0f08674SEwan Crawford     // Establish format and size of each data element
1728*a0f08674SEwan Crawford     const unsigned int vec_size = *alloc->type_vec_size.get();
1729*a0f08674SEwan Crawford     const AllocationDetails::DataType type = *alloc->type.get();
1730*a0f08674SEwan Crawford 
1731*a0f08674SEwan Crawford     assert(type >= AllocationDetails::RS_TYPE_NONE && type <= AllocationDetails::RS_TYPE_BOOLEAN
1732*a0f08674SEwan Crawford                                                    && "Invalid allocation type");
1733*a0f08674SEwan Crawford 
1734*a0f08674SEwan Crawford     lldb::Format format = vec_size == 1 ? static_cast<lldb::Format>(AllocationDetails::RSTypeToFormat[type][eFormatSingle])
1735*a0f08674SEwan Crawford                                         : static_cast<lldb::Format>(AllocationDetails::RSTypeToFormat[type][eFormatVector]);
1736*a0f08674SEwan Crawford 
1737*a0f08674SEwan Crawford     const unsigned int data_size = vec_size * AllocationDetails::RSTypeToFormat[type][eElementSize];
1738*a0f08674SEwan Crawford     // Renderscript pads vector 3 elements to vector 4
1739*a0f08674SEwan Crawford     const unsigned int elem_padding = vec_size == 3 ? AllocationDetails::RSTypeToFormat[type][eElementSize] : 0;
1740*a0f08674SEwan Crawford 
1741*a0f08674SEwan Crawford     if (log)
1742*a0f08674SEwan Crawford         log->Printf("RenderScriptRuntime::DumpAllocation - Element size %u bytes, element padding %u bytes",
1743*a0f08674SEwan Crawford                     data_size, elem_padding);
1744*a0f08674SEwan Crawford 
1745*a0f08674SEwan Crawford     // Calculate stride between rows as there may be padding at end of rows since
1746*a0f08674SEwan Crawford     // allocated memory is 16-byte aligned
1747*a0f08674SEwan Crawford     if (!alloc->stride.isValid())
1748*a0f08674SEwan Crawford     {
1749*a0f08674SEwan Crawford         if (alloc->dimension.get()->dim_2 == 0) // We only have one dimension
1750*a0f08674SEwan Crawford             alloc->stride = 0;
1751*a0f08674SEwan Crawford         else if (!JITAllocationStride(alloc, frame_ptr))
1752*a0f08674SEwan Crawford         {
1753*a0f08674SEwan Crawford             strm.Printf("Error: Couldn't calculate allocation row stride");
1754*a0f08674SEwan Crawford             strm.EOL();
1755*a0f08674SEwan Crawford             return false;
1756*a0f08674SEwan Crawford         }
1757*a0f08674SEwan Crawford     }
1758*a0f08674SEwan Crawford     const unsigned int stride = *alloc->stride.get();
1759*a0f08674SEwan Crawford 
1760*a0f08674SEwan Crawford     // Calculate data size
1761*a0f08674SEwan Crawford     if (!alloc->size.isValid() && !JITAllocationSize(alloc, frame_ptr, data_size + elem_padding))
1762*a0f08674SEwan Crawford     {
1763*a0f08674SEwan Crawford         strm.Printf("Error: Couldn't calculate allocation size");
1764*a0f08674SEwan Crawford         strm.EOL();
1765*a0f08674SEwan Crawford         return false;
1766*a0f08674SEwan Crawford     }
1767*a0f08674SEwan Crawford     const unsigned int size = *alloc->size.get(); //size of last element
1768*a0f08674SEwan Crawford 
1769*a0f08674SEwan Crawford     if (log)
1770*a0f08674SEwan Crawford         log->Printf("RenderScriptRuntime::DumpAllocation - stride %u bytes, size %u bytes", stride, size);
1771*a0f08674SEwan Crawford 
1772*a0f08674SEwan Crawford     // Allocate a buffer to copy data into
1773*a0f08674SEwan Crawford     uint8_t* buffer = new uint8_t[size];
1774*a0f08674SEwan Crawford     if (!buffer)
1775*a0f08674SEwan Crawford     {
1776*a0f08674SEwan Crawford         strm.Printf("Error: Couldn't allocate a %u byte buffer to read memory into", size);
1777*a0f08674SEwan Crawford         strm.EOL();
1778*a0f08674SEwan Crawford         return false;
1779*a0f08674SEwan Crawford     }
1780*a0f08674SEwan Crawford 
1781*a0f08674SEwan Crawford     // Read Memory into buffer
1782*a0f08674SEwan Crawford     Error error;
1783*a0f08674SEwan Crawford     Process* process = GetProcess();
1784*a0f08674SEwan Crawford     const addr_t data_ptr = *alloc->data_ptr.get();
1785*a0f08674SEwan Crawford     const uint32_t archByteSize = process->GetTarget().GetArchitecture().GetAddressByteSize();
1786*a0f08674SEwan Crawford     DataExtractor alloc_data(buffer, size, process->GetByteOrder(), archByteSize);
1787*a0f08674SEwan Crawford 
1788*a0f08674SEwan Crawford     if (log)
1789*a0f08674SEwan Crawford         log->Printf("RenderScriptRuntime::DumpAllocation - Reading %u bytes of allocation data from 0x%" PRIx64,
1790*a0f08674SEwan Crawford                     size, data_ptr);
1791*a0f08674SEwan Crawford 
1792*a0f08674SEwan Crawford     // Read the inferior memory
1793*a0f08674SEwan Crawford     process->ReadMemory(data_ptr, buffer, size, error);
1794*a0f08674SEwan Crawford     if (error.Fail())
1795*a0f08674SEwan Crawford     {
1796*a0f08674SEwan Crawford         strm.Printf("Error: Couldn't read %u bytes of allocation data from 0x%" PRIx64, size, data_ptr);
1797*a0f08674SEwan Crawford         strm.EOL();
1798*a0f08674SEwan Crawford         delete[] buffer; // remember to free memory
1799*a0f08674SEwan Crawford         return false;
1800*a0f08674SEwan Crawford     }
1801*a0f08674SEwan Crawford 
1802*a0f08674SEwan Crawford     // Find dimensions used to index loops, so need to be non-zero
1803*a0f08674SEwan Crawford     unsigned int dim_x = alloc->dimension.get()->dim_1;
1804*a0f08674SEwan Crawford     dim_x = dim_x == 0 ? 1 : dim_x;
1805*a0f08674SEwan Crawford 
1806*a0f08674SEwan Crawford     unsigned int dim_y = alloc->dimension.get()->dim_2;
1807*a0f08674SEwan Crawford     dim_y = dim_y == 0 ? 1 : dim_y;
1808*a0f08674SEwan Crawford 
1809*a0f08674SEwan Crawford     unsigned int dim_z = alloc->dimension.get()->dim_3;
1810*a0f08674SEwan Crawford     dim_z = dim_z == 0 ? 1 : dim_z;
1811*a0f08674SEwan Crawford 
1812*a0f08674SEwan Crawford     unsigned int offset = 0;   // Offset in buffer to next element to be printed
1813*a0f08674SEwan Crawford     unsigned int prev_row = 0; // Offset to the start of the previous row
1814*a0f08674SEwan Crawford 
1815*a0f08674SEwan Crawford     // Iterate over allocation dimensions, printing results to user
1816*a0f08674SEwan Crawford     strm.Printf("Data (X, Y, Z):");
1817*a0f08674SEwan Crawford     for (unsigned int z = 0; z < dim_z; ++z)
1818*a0f08674SEwan Crawford     {
1819*a0f08674SEwan Crawford         for (unsigned int y = 0; y < dim_y; ++y)
1820*a0f08674SEwan Crawford         {
1821*a0f08674SEwan Crawford             // Use stride to index start of next row.
1822*a0f08674SEwan Crawford             if (!(y==0 && z==0))
1823*a0f08674SEwan Crawford                 offset = prev_row + stride;
1824*a0f08674SEwan Crawford             prev_row = offset;
1825*a0f08674SEwan Crawford 
1826*a0f08674SEwan Crawford             // Print each element in the row individually
1827*a0f08674SEwan Crawford             for (unsigned int x = 0; x < dim_x; ++x)
1828*a0f08674SEwan Crawford             {
1829*a0f08674SEwan Crawford                 strm.Printf("\n(%u, %u, %u) = ", x, y, z);
1830*a0f08674SEwan Crawford                 alloc_data.Dump(&strm, offset, format, data_size, 1, 1, LLDB_INVALID_ADDRESS, 0, 0);
1831*a0f08674SEwan Crawford                 offset += data_size + elem_padding;
1832*a0f08674SEwan Crawford             }
1833*a0f08674SEwan Crawford         }
1834*a0f08674SEwan Crawford     }
1835*a0f08674SEwan Crawford     strm.EOL();
1836*a0f08674SEwan Crawford 
1837*a0f08674SEwan Crawford     delete[] buffer;
1838*a0f08674SEwan Crawford     return true;
1839*a0f08674SEwan Crawford }
1840*a0f08674SEwan Crawford 
184115f2bd95SEwan Crawford // Prints infomation regarding all the currently loaded allocations.
184215f2bd95SEwan Crawford // These details are gathered by jitting the runtime, which has as latency.
184315f2bd95SEwan Crawford void
184415f2bd95SEwan Crawford RenderScriptRuntime::ListAllocations(Stream &strm, StackFrame* frame_ptr, bool recompute)
184515f2bd95SEwan Crawford {
184615f2bd95SEwan Crawford     strm.Printf("RenderScript Allocations:");
184715f2bd95SEwan Crawford     strm.EOL();
184815f2bd95SEwan Crawford     strm.IndentMore();
184915f2bd95SEwan Crawford 
185015f2bd95SEwan Crawford     for (auto &alloc : m_allocations)
185115f2bd95SEwan Crawford     {
185215f2bd95SEwan Crawford         // JIT the allocation info if we haven't done it, or the user forces us to.
185315f2bd95SEwan Crawford         bool do_refresh = !alloc->data_ptr.isValid() || recompute;
185415f2bd95SEwan Crawford 
185515f2bd95SEwan Crawford         // JIT current allocation information
185615f2bd95SEwan Crawford         if (do_refresh && !RefreshAllocation(alloc.get(), frame_ptr))
185715f2bd95SEwan Crawford         {
185815f2bd95SEwan Crawford             strm.Printf("Error: Couldn't evaluate details for allocation %u\n", alloc->id);
185915f2bd95SEwan Crawford             continue;
186015f2bd95SEwan Crawford         }
186115f2bd95SEwan Crawford 
186215f2bd95SEwan Crawford         strm.Printf("%u:\n",alloc->id);
186315f2bd95SEwan Crawford         strm.IndentMore();
186415f2bd95SEwan Crawford 
186515f2bd95SEwan Crawford         strm.Indent("Context: ");
186615f2bd95SEwan Crawford         if (!alloc->context.isValid())
186715f2bd95SEwan Crawford             strm.Printf("unknown\n");
186815f2bd95SEwan Crawford         else
186915f2bd95SEwan Crawford             strm.Printf("0x%" PRIx64 "\n", *alloc->context.get());
187015f2bd95SEwan Crawford 
187115f2bd95SEwan Crawford         strm.Indent("Address: ");
187215f2bd95SEwan Crawford         if (!alloc->address.isValid())
187315f2bd95SEwan Crawford             strm.Printf("unknown\n");
187415f2bd95SEwan Crawford         else
187515f2bd95SEwan Crawford             strm.Printf("0x%" PRIx64 "\n", *alloc->address.get());
187615f2bd95SEwan Crawford 
187715f2bd95SEwan Crawford         strm.Indent("Data pointer: ");
187815f2bd95SEwan Crawford         if (!alloc->data_ptr.isValid())
187915f2bd95SEwan Crawford             strm.Printf("unknown\n");
188015f2bd95SEwan Crawford         else
188115f2bd95SEwan Crawford             strm.Printf("0x%" PRIx64 "\n", *alloc->data_ptr.get());
188215f2bd95SEwan Crawford 
188315f2bd95SEwan Crawford         strm.Indent("Dimensions: ");
188415f2bd95SEwan Crawford         if (!alloc->dimension.isValid())
188515f2bd95SEwan Crawford             strm.Printf("unknown\n");
188615f2bd95SEwan Crawford         else
188715f2bd95SEwan Crawford             strm.Printf("(%d, %d, %d)\n", alloc->dimension.get()->dim_1,
188815f2bd95SEwan Crawford                                           alloc->dimension.get()->dim_2,
188915f2bd95SEwan Crawford                                           alloc->dimension.get()->dim_3);
189015f2bd95SEwan Crawford 
189115f2bd95SEwan Crawford         strm.Indent("Data Type: ");
189215f2bd95SEwan Crawford         if (!alloc->type.isValid() || !alloc->type_vec_size.isValid())
189315f2bd95SEwan Crawford             strm.Printf("unknown\n");
189415f2bd95SEwan Crawford         else
189515f2bd95SEwan Crawford         {
189615f2bd95SEwan Crawford             const int vector_size = *alloc->type_vec_size.get();
189715f2bd95SEwan Crawford             const AllocationDetails::DataType type = *alloc->type.get();
189815f2bd95SEwan Crawford 
189915f2bd95SEwan Crawford             if (vector_size > 4 || vector_size < 1 ||
190015f2bd95SEwan Crawford                 type < AllocationDetails::RS_TYPE_NONE || type > AllocationDetails::RS_TYPE_BOOLEAN)
190115f2bd95SEwan Crawford                 strm.Printf("invalid type\n");
190215f2bd95SEwan Crawford             else
190315f2bd95SEwan Crawford                 strm.Printf("%s\n", AllocationDetails::RsDataTypeToString[static_cast<unsigned int>(type)][vector_size-1]);
190415f2bd95SEwan Crawford         }
190515f2bd95SEwan Crawford 
190615f2bd95SEwan Crawford         strm.Indent("Data Kind: ");
190715f2bd95SEwan Crawford         if (!alloc->type_kind.isValid())
190815f2bd95SEwan Crawford             strm.Printf("unknown\n");
190915f2bd95SEwan Crawford         else
191015f2bd95SEwan Crawford         {
191115f2bd95SEwan Crawford             const AllocationDetails::DataKind kind = *alloc->type_kind.get();
191215f2bd95SEwan Crawford             if (kind < AllocationDetails::RS_KIND_USER || kind > AllocationDetails::RS_KIND_PIXEL_YUV)
191315f2bd95SEwan Crawford                 strm.Printf("invalid kind\n");
191415f2bd95SEwan Crawford             else
191515f2bd95SEwan Crawford                 strm.Printf("%s\n", AllocationDetails::RsDataKindToString[static_cast<unsigned int>(kind)]);
191615f2bd95SEwan Crawford         }
191715f2bd95SEwan Crawford 
191815f2bd95SEwan Crawford         strm.EOL();
191915f2bd95SEwan Crawford         strm.IndentLess();
192015f2bd95SEwan Crawford     }
192115f2bd95SEwan Crawford     strm.IndentLess();
192215f2bd95SEwan Crawford }
192315f2bd95SEwan Crawford 
19247dc7771cSEwan Crawford // Set breakpoints on every kernel found in RS module
19257dc7771cSEwan Crawford void
19267dc7771cSEwan Crawford RenderScriptRuntime::BreakOnModuleKernels(const RSModuleDescriptorSP rsmodule_sp)
19277dc7771cSEwan Crawford {
19287dc7771cSEwan Crawford     for (const auto &kernel : rsmodule_sp->m_kernels)
19297dc7771cSEwan Crawford     {
19307dc7771cSEwan Crawford         // Don't set breakpoint on 'root' kernel
19317dc7771cSEwan Crawford         if (strcmp(kernel.m_name.AsCString(), "root") == 0)
19327dc7771cSEwan Crawford             continue;
19337dc7771cSEwan Crawford 
19347dc7771cSEwan Crawford         CreateKernelBreakpoint(kernel.m_name);
19357dc7771cSEwan Crawford     }
19367dc7771cSEwan Crawford }
19377dc7771cSEwan Crawford 
19387dc7771cSEwan Crawford // Method is internally called by the 'kernel breakpoint all' command to
19397dc7771cSEwan Crawford // enable or disable breaking on all kernels.
19407dc7771cSEwan Crawford //
19417dc7771cSEwan Crawford // When do_break is true we want to enable this functionality.
19427dc7771cSEwan Crawford // When do_break is false we want to disable it.
19437dc7771cSEwan Crawford void
19447dc7771cSEwan Crawford RenderScriptRuntime::SetBreakAllKernels(bool do_break, TargetSP target)
19457dc7771cSEwan Crawford {
194654782db7SEwan Crawford     Log* log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS));
19477dc7771cSEwan Crawford 
19487dc7771cSEwan Crawford     InitSearchFilter(target);
19497dc7771cSEwan Crawford 
19507dc7771cSEwan Crawford     // Set breakpoints on all the kernels
19517dc7771cSEwan Crawford     if (do_break && !m_breakAllKernels)
19527dc7771cSEwan Crawford     {
19537dc7771cSEwan Crawford         m_breakAllKernels = true;
19547dc7771cSEwan Crawford 
19557dc7771cSEwan Crawford         for (const auto &module : m_rsmodules)
19567dc7771cSEwan Crawford             BreakOnModuleKernels(module);
19577dc7771cSEwan Crawford 
19587dc7771cSEwan Crawford         if (log)
19597dc7771cSEwan Crawford             log->Printf("RenderScriptRuntime::SetBreakAllKernels(True)"
19607dc7771cSEwan Crawford                         "- breakpoints set on all currently loaded kernels");
19617dc7771cSEwan Crawford     }
19627dc7771cSEwan Crawford     else if (!do_break && m_breakAllKernels) // Breakpoints won't be set on any new kernels.
19637dc7771cSEwan Crawford     {
19647dc7771cSEwan Crawford         m_breakAllKernels = false;
19657dc7771cSEwan Crawford 
19667dc7771cSEwan Crawford         if (log)
19677dc7771cSEwan Crawford             log->Printf("RenderScriptRuntime::SetBreakAllKernels(False) - breakpoints no longer automatically set");
19687dc7771cSEwan Crawford     }
19697dc7771cSEwan Crawford }
19707dc7771cSEwan Crawford 
19717dc7771cSEwan Crawford // Given the name of a kernel this function creates a breakpoint using our
19727dc7771cSEwan Crawford // own breakpoint resolver, and returns the Breakpoint shared pointer.
19737dc7771cSEwan Crawford BreakpointSP
19747dc7771cSEwan Crawford RenderScriptRuntime::CreateKernelBreakpoint(const ConstString& name)
19757dc7771cSEwan Crawford {
197654782db7SEwan Crawford     Log* log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS));
19777dc7771cSEwan Crawford 
19787dc7771cSEwan Crawford     if (!m_filtersp)
19797dc7771cSEwan Crawford     {
19807dc7771cSEwan Crawford         if (log)
19817dc7771cSEwan Crawford             log->Printf("RenderScriptRuntime::CreateKernelBreakpoint - Error: No breakpoint search filter set");
19827dc7771cSEwan Crawford         return nullptr;
19837dc7771cSEwan Crawford     }
19847dc7771cSEwan Crawford 
19857dc7771cSEwan Crawford     BreakpointResolverSP resolver_sp(new RSBreakpointResolver(nullptr, name));
19867dc7771cSEwan Crawford     BreakpointSP bp = GetProcess()->GetTarget().CreateBreakpoint(m_filtersp, resolver_sp, false, false, false);
19877dc7771cSEwan Crawford 
198854782db7SEwan Crawford     // Give RS breakpoints a specific name, so the user can manipulate them as a group.
198954782db7SEwan Crawford     Error err;
199054782db7SEwan Crawford     if (!bp->AddName("RenderScriptKernel", err) && log)
199154782db7SEwan Crawford         log->Printf("RenderScriptRuntime::CreateKernelBreakpoint: Error setting break name, %s", err.AsCString());
199254782db7SEwan Crawford 
19937dc7771cSEwan Crawford     return bp;
19947dc7771cSEwan Crawford }
19957dc7771cSEwan Crawford 
19964640cde1SColin Riley void
199798156583SEwan Crawford RenderScriptRuntime::AttemptBreakpointAtKernelName(Stream &strm, const char* name, Error& error, TargetSP target)
19984640cde1SColin Riley {
19994640cde1SColin Riley     if (!name)
20004640cde1SColin Riley     {
20014640cde1SColin Riley         error.SetErrorString("invalid kernel name");
20024640cde1SColin Riley         return;
20034640cde1SColin Riley     }
20044640cde1SColin Riley 
20057dc7771cSEwan Crawford     InitSearchFilter(target);
200698156583SEwan Crawford 
20074640cde1SColin Riley     ConstString kernel_name(name);
20087dc7771cSEwan Crawford     BreakpointSP bp = CreateKernelBreakpoint(kernel_name);
200998156583SEwan Crawford     if (bp)
201098156583SEwan Crawford         bp->GetDescription(&strm, lldb::eDescriptionLevelInitial, false);
20114640cde1SColin Riley 
20124640cde1SColin Riley     return;
20134640cde1SColin Riley }
20144640cde1SColin Riley 
20154640cde1SColin Riley void
20165ec532a9SColin Riley RenderScriptRuntime::DumpModules(Stream &strm) const
20175ec532a9SColin Riley {
20185ec532a9SColin Riley     strm.Printf("RenderScript Modules:");
20195ec532a9SColin Riley     strm.EOL();
20205ec532a9SColin Riley     strm.IndentMore();
20215ec532a9SColin Riley     for (const auto &module : m_rsmodules)
20225ec532a9SColin Riley     {
20234640cde1SColin Riley         module->Dump(strm);
20245ec532a9SColin Riley     }
20255ec532a9SColin Riley     strm.IndentLess();
20265ec532a9SColin Riley }
20275ec532a9SColin Riley 
202878f339d1SEwan Crawford RenderScriptRuntime::ScriptDetails*
202978f339d1SEwan Crawford RenderScriptRuntime::LookUpScript(addr_t address, bool create)
203078f339d1SEwan Crawford {
203178f339d1SEwan Crawford     for (const auto & s : m_scripts)
203278f339d1SEwan Crawford     {
203378f339d1SEwan Crawford         if (s->script.isValid())
203478f339d1SEwan Crawford             if (*s->script == address)
203578f339d1SEwan Crawford                 return s.get();
203678f339d1SEwan Crawford     }
203778f339d1SEwan Crawford     if (create)
203878f339d1SEwan Crawford     {
203978f339d1SEwan Crawford         std::unique_ptr<ScriptDetails> s(new ScriptDetails);
204078f339d1SEwan Crawford         s->script = address;
204178f339d1SEwan Crawford         m_scripts.push_back(std::move(s));
2042d10ca9deSEwan Crawford         return m_scripts.back().get();
204378f339d1SEwan Crawford     }
204478f339d1SEwan Crawford     return nullptr;
204578f339d1SEwan Crawford }
204678f339d1SEwan Crawford 
204778f339d1SEwan Crawford RenderScriptRuntime::AllocationDetails*
204878f339d1SEwan Crawford RenderScriptRuntime::LookUpAllocation(addr_t address, bool create)
204978f339d1SEwan Crawford {
205078f339d1SEwan Crawford     for (const auto & a : m_allocations)
205178f339d1SEwan Crawford     {
205278f339d1SEwan Crawford         if (a->address.isValid())
205378f339d1SEwan Crawford             if (*a->address == address)
205478f339d1SEwan Crawford                 return a.get();
205578f339d1SEwan Crawford     }
205678f339d1SEwan Crawford     if (create)
205778f339d1SEwan Crawford     {
205878f339d1SEwan Crawford         std::unique_ptr<AllocationDetails> a(new AllocationDetails);
205978f339d1SEwan Crawford         a->address = address;
206078f339d1SEwan Crawford         m_allocations.push_back(std::move(a));
2061d10ca9deSEwan Crawford         return m_allocations.back().get();
206278f339d1SEwan Crawford     }
206378f339d1SEwan Crawford     return nullptr;
206478f339d1SEwan Crawford }
206578f339d1SEwan Crawford 
20665ec532a9SColin Riley void
20675ec532a9SColin Riley RSModuleDescriptor::Dump(Stream &strm) const
20685ec532a9SColin Riley {
20695ec532a9SColin Riley     strm.Indent();
20705ec532a9SColin Riley     m_module->GetFileSpec().Dump(&strm);
20714640cde1SColin Riley     if(m_module->GetNumCompileUnits())
20724640cde1SColin Riley     {
20734640cde1SColin Riley         strm.Indent("Debug info loaded.");
20744640cde1SColin Riley     }
20754640cde1SColin Riley     else
20764640cde1SColin Riley     {
20774640cde1SColin Riley         strm.Indent("Debug info does not exist.");
20784640cde1SColin Riley     }
20795ec532a9SColin Riley     strm.EOL();
20805ec532a9SColin Riley     strm.IndentMore();
20815ec532a9SColin Riley     strm.Indent();
2082189598edSColin Riley     strm.Printf("Globals: %" PRIu64, static_cast<uint64_t>(m_globals.size()));
20835ec532a9SColin Riley     strm.EOL();
20845ec532a9SColin Riley     strm.IndentMore();
20855ec532a9SColin Riley     for (const auto &global : m_globals)
20865ec532a9SColin Riley     {
20875ec532a9SColin Riley         global.Dump(strm);
20885ec532a9SColin Riley     }
20895ec532a9SColin Riley     strm.IndentLess();
20905ec532a9SColin Riley     strm.Indent();
2091189598edSColin Riley     strm.Printf("Kernels: %" PRIu64, static_cast<uint64_t>(m_kernels.size()));
20925ec532a9SColin Riley     strm.EOL();
20935ec532a9SColin Riley     strm.IndentMore();
20945ec532a9SColin Riley     for (const auto &kernel : m_kernels)
20955ec532a9SColin Riley     {
20965ec532a9SColin Riley         kernel.Dump(strm);
20975ec532a9SColin Riley     }
20984640cde1SColin Riley     strm.Printf("Pragmas: %"  PRIu64 , static_cast<uint64_t>(m_pragmas.size()));
20994640cde1SColin Riley     strm.EOL();
21004640cde1SColin Riley     strm.IndentMore();
21014640cde1SColin Riley     for (const auto &key_val : m_pragmas)
21024640cde1SColin Riley     {
21034640cde1SColin Riley         strm.Printf("%s: %s", key_val.first.c_str(), key_val.second.c_str());
21044640cde1SColin Riley         strm.EOL();
21054640cde1SColin Riley     }
21065ec532a9SColin Riley     strm.IndentLess(4);
21075ec532a9SColin Riley }
21085ec532a9SColin Riley 
21095ec532a9SColin Riley void
21105ec532a9SColin Riley RSGlobalDescriptor::Dump(Stream &strm) const
21115ec532a9SColin Riley {
21125ec532a9SColin Riley     strm.Indent(m_name.AsCString());
21134640cde1SColin Riley     VariableList var_list;
21144640cde1SColin Riley     m_module->m_module->FindGlobalVariables(m_name, nullptr, true, 1U, var_list);
21154640cde1SColin Riley     if (var_list.GetSize() == 1)
21164640cde1SColin Riley     {
21174640cde1SColin Riley         auto var = var_list.GetVariableAtIndex(0);
21184640cde1SColin Riley         auto type = var->GetType();
21194640cde1SColin Riley         if(type)
21204640cde1SColin Riley         {
21214640cde1SColin Riley             strm.Printf(" - ");
21224640cde1SColin Riley             type->DumpTypeName(&strm);
21234640cde1SColin Riley         }
21244640cde1SColin Riley         else
21254640cde1SColin Riley         {
21264640cde1SColin Riley             strm.Printf(" - Unknown Type");
21274640cde1SColin Riley         }
21284640cde1SColin Riley     }
21294640cde1SColin Riley     else
21304640cde1SColin Riley     {
21314640cde1SColin Riley         strm.Printf(" - variable identified, but not found in binary");
21324640cde1SColin Riley         const Symbol* s = m_module->m_module->FindFirstSymbolWithNameAndType(m_name, eSymbolTypeData);
21334640cde1SColin Riley         if (s)
21344640cde1SColin Riley         {
21354640cde1SColin Riley             strm.Printf(" (symbol exists) ");
21364640cde1SColin Riley         }
21374640cde1SColin Riley     }
21384640cde1SColin Riley 
21395ec532a9SColin Riley     strm.EOL();
21405ec532a9SColin Riley }
21415ec532a9SColin Riley 
21425ec532a9SColin Riley void
21435ec532a9SColin Riley RSKernelDescriptor::Dump(Stream &strm) const
21445ec532a9SColin Riley {
21455ec532a9SColin Riley     strm.Indent(m_name.AsCString());
21465ec532a9SColin Riley     strm.EOL();
21475ec532a9SColin Riley }
21485ec532a9SColin Riley 
21495ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModuleProbe : public CommandObjectParsed
21505ec532a9SColin Riley {
21515ec532a9SColin Riley   private:
21525ec532a9SColin Riley   public:
21535ec532a9SColin Riley     CommandObjectRenderScriptRuntimeModuleProbe(CommandInterpreter &interpreter)
21545ec532a9SColin Riley         : CommandObjectParsed(interpreter, "renderscript module probe",
21555ec532a9SColin Riley                               "Initiates a Probe of all loaded modules for kernels and other renderscript objects.",
21565ec532a9SColin Riley                               "renderscript module probe",
2157e87764f2SEnrico Granata                               eCommandRequiresTarget | eCommandRequiresProcess | eCommandProcessMustBeLaunched)
21585ec532a9SColin Riley     {
21595ec532a9SColin Riley     }
21605ec532a9SColin Riley 
21615ec532a9SColin Riley     ~CommandObjectRenderScriptRuntimeModuleProbe() {}
21625ec532a9SColin Riley 
21635ec532a9SColin Riley     bool
21645ec532a9SColin Riley     DoExecute(Args &command, CommandReturnObject &result)
21655ec532a9SColin Riley     {
21665ec532a9SColin Riley         const size_t argc = command.GetArgumentCount();
21675ec532a9SColin Riley         if (argc == 0)
21685ec532a9SColin Riley         {
21695ec532a9SColin Riley             Target *target = m_exe_ctx.GetTargetPtr();
21705ec532a9SColin Riley             RenderScriptRuntime *runtime =
21715ec532a9SColin Riley                 (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
21725ec532a9SColin Riley             auto module_list = target->GetImages();
21735ec532a9SColin Riley             bool new_rs_details = runtime->ProbeModules(module_list);
21745ec532a9SColin Riley             if (new_rs_details)
21755ec532a9SColin Riley             {
21765ec532a9SColin Riley                 result.AppendMessage("New renderscript modules added to runtime model.");
21775ec532a9SColin Riley             }
21785ec532a9SColin Riley             result.SetStatus(eReturnStatusSuccessFinishResult);
21795ec532a9SColin Riley             return true;
21805ec532a9SColin Riley         }
21815ec532a9SColin Riley 
21825ec532a9SColin Riley         result.AppendErrorWithFormat("'%s' takes no arguments", m_cmd_name.c_str());
21835ec532a9SColin Riley         result.SetStatus(eReturnStatusFailed);
21845ec532a9SColin Riley         return false;
21855ec532a9SColin Riley     }
21865ec532a9SColin Riley };
21875ec532a9SColin Riley 
21885ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModuleDump : public CommandObjectParsed
21895ec532a9SColin Riley {
21905ec532a9SColin Riley   private:
21915ec532a9SColin Riley   public:
21925ec532a9SColin Riley     CommandObjectRenderScriptRuntimeModuleDump(CommandInterpreter &interpreter)
21935ec532a9SColin Riley         : CommandObjectParsed(interpreter, "renderscript module dump",
21945ec532a9SColin Riley                               "Dumps renderscript specific information for all modules.", "renderscript module dump",
2195e87764f2SEnrico Granata                               eCommandRequiresProcess | eCommandProcessMustBeLaunched)
21965ec532a9SColin Riley     {
21975ec532a9SColin Riley     }
21985ec532a9SColin Riley 
21995ec532a9SColin Riley     ~CommandObjectRenderScriptRuntimeModuleDump() {}
22005ec532a9SColin Riley 
22015ec532a9SColin Riley     bool
22025ec532a9SColin Riley     DoExecute(Args &command, CommandReturnObject &result)
22035ec532a9SColin Riley     {
22045ec532a9SColin Riley         RenderScriptRuntime *runtime =
22055ec532a9SColin Riley             (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
22065ec532a9SColin Riley         runtime->DumpModules(result.GetOutputStream());
22075ec532a9SColin Riley         result.SetStatus(eReturnStatusSuccessFinishResult);
22085ec532a9SColin Riley         return true;
22095ec532a9SColin Riley     }
22105ec532a9SColin Riley };
22115ec532a9SColin Riley 
22125ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModule : public CommandObjectMultiword
22135ec532a9SColin Riley {
22145ec532a9SColin Riley   private:
22155ec532a9SColin Riley   public:
22165ec532a9SColin Riley     CommandObjectRenderScriptRuntimeModule(CommandInterpreter &interpreter)
22175ec532a9SColin Riley         : CommandObjectMultiword(interpreter, "renderscript module", "Commands that deal with renderscript modules.",
22185ec532a9SColin Riley                                  NULL)
22195ec532a9SColin Riley     {
22205ec532a9SColin Riley         LoadSubCommand("probe", CommandObjectSP(new CommandObjectRenderScriptRuntimeModuleProbe(interpreter)));
22215ec532a9SColin Riley         LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeModuleDump(interpreter)));
22225ec532a9SColin Riley     }
22235ec532a9SColin Riley 
22245ec532a9SColin Riley     ~CommandObjectRenderScriptRuntimeModule() {}
22255ec532a9SColin Riley };
22265ec532a9SColin Riley 
22274640cde1SColin Riley class CommandObjectRenderScriptRuntimeKernelList : public CommandObjectParsed
22284640cde1SColin Riley {
22294640cde1SColin Riley   private:
22304640cde1SColin Riley   public:
22314640cde1SColin Riley     CommandObjectRenderScriptRuntimeKernelList(CommandInterpreter &interpreter)
22324640cde1SColin Riley         : CommandObjectParsed(interpreter, "renderscript kernel list",
22334640cde1SColin Riley                               "Lists renderscript kernel names and associated script resources.", "renderscript kernel list",
22344640cde1SColin Riley                               eCommandRequiresProcess | eCommandProcessMustBeLaunched)
22354640cde1SColin Riley     {
22364640cde1SColin Riley     }
22374640cde1SColin Riley 
22384640cde1SColin Riley     ~CommandObjectRenderScriptRuntimeKernelList() {}
22394640cde1SColin Riley 
22404640cde1SColin Riley     bool
22414640cde1SColin Riley     DoExecute(Args &command, CommandReturnObject &result)
22424640cde1SColin Riley     {
22434640cde1SColin Riley         RenderScriptRuntime *runtime =
22444640cde1SColin Riley             (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
22454640cde1SColin Riley         runtime->DumpKernels(result.GetOutputStream());
22464640cde1SColin Riley         result.SetStatus(eReturnStatusSuccessFinishResult);
22474640cde1SColin Riley         return true;
22484640cde1SColin Riley     }
22494640cde1SColin Riley };
22504640cde1SColin Riley 
22517dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpointSet : public CommandObjectParsed
22524640cde1SColin Riley {
22534640cde1SColin Riley   private:
22544640cde1SColin Riley   public:
22557dc7771cSEwan Crawford     CommandObjectRenderScriptRuntimeKernelBreakpointSet(CommandInterpreter &interpreter)
22567dc7771cSEwan Crawford         : CommandObjectParsed(interpreter, "renderscript kernel breakpoint set",
22577dc7771cSEwan Crawford                               "Sets a breakpoint on a renderscript kernel.", "renderscript kernel breakpoint set <kernel_name>",
22584640cde1SColin Riley                               eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused)
22594640cde1SColin Riley     {
22604640cde1SColin Riley     }
22614640cde1SColin Riley 
22627dc7771cSEwan Crawford     ~CommandObjectRenderScriptRuntimeKernelBreakpointSet() {}
22634640cde1SColin Riley 
22644640cde1SColin Riley     bool
22654640cde1SColin Riley     DoExecute(Args &command, CommandReturnObject &result)
22664640cde1SColin Riley     {
22674640cde1SColin Riley         const size_t argc = command.GetArgumentCount();
22684640cde1SColin Riley         if (argc == 1)
22694640cde1SColin Riley         {
22704640cde1SColin Riley             RenderScriptRuntime *runtime =
22714640cde1SColin Riley                 (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
22724640cde1SColin Riley 
22734640cde1SColin Riley             Error error;
227498156583SEwan Crawford             runtime->AttemptBreakpointAtKernelName(result.GetOutputStream(), command.GetArgumentAtIndex(0),
227598156583SEwan Crawford                                                    error, m_exe_ctx.GetTargetSP());
22764640cde1SColin Riley 
22774640cde1SColin Riley             if (error.Success())
22784640cde1SColin Riley             {
22794640cde1SColin Riley                 result.AppendMessage("Breakpoint(s) created");
22804640cde1SColin Riley                 result.SetStatus(eReturnStatusSuccessFinishResult);
22814640cde1SColin Riley                 return true;
22824640cde1SColin Riley             }
22834640cde1SColin Riley             result.SetStatus(eReturnStatusFailed);
22844640cde1SColin Riley             result.AppendErrorWithFormat("Error: %s", error.AsCString());
22854640cde1SColin Riley             return false;
22864640cde1SColin Riley         }
22874640cde1SColin Riley 
22884640cde1SColin Riley         result.AppendErrorWithFormat("'%s' takes 1 argument of kernel name", m_cmd_name.c_str());
22894640cde1SColin Riley         result.SetStatus(eReturnStatusFailed);
22904640cde1SColin Riley         return false;
22914640cde1SColin Riley     }
22924640cde1SColin Riley };
22934640cde1SColin Riley 
22947dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpointAll : public CommandObjectParsed
22957dc7771cSEwan Crawford {
22967dc7771cSEwan Crawford   private:
22977dc7771cSEwan Crawford   public:
22987dc7771cSEwan Crawford     CommandObjectRenderScriptRuntimeKernelBreakpointAll(CommandInterpreter &interpreter)
22997dc7771cSEwan Crawford         : CommandObjectParsed(interpreter, "renderscript kernel breakpoint all",
23007dc7771cSEwan Crawford                               "Automatically sets a breakpoint on all renderscript kernels that are or will be loaded.\n"
23017dc7771cSEwan Crawford                               "Disabling option means breakpoints will no longer be set on any kernels loaded in the future, "
23027dc7771cSEwan Crawford                               "but does not remove currently set breakpoints.",
23037dc7771cSEwan Crawford                               "renderscript kernel breakpoint all <enable/disable>",
23047dc7771cSEwan Crawford                               eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused)
23057dc7771cSEwan Crawford     {
23067dc7771cSEwan Crawford     }
23077dc7771cSEwan Crawford 
23087dc7771cSEwan Crawford     ~CommandObjectRenderScriptRuntimeKernelBreakpointAll() {}
23097dc7771cSEwan Crawford 
23107dc7771cSEwan Crawford     bool
23117dc7771cSEwan Crawford     DoExecute(Args &command, CommandReturnObject &result)
23127dc7771cSEwan Crawford     {
23137dc7771cSEwan Crawford         const size_t argc = command.GetArgumentCount();
23147dc7771cSEwan Crawford         if (argc != 1)
23157dc7771cSEwan Crawford         {
23167dc7771cSEwan Crawford             result.AppendErrorWithFormat("'%s' takes 1 argument of 'enable' or 'disable'", m_cmd_name.c_str());
23177dc7771cSEwan Crawford             result.SetStatus(eReturnStatusFailed);
23187dc7771cSEwan Crawford             return false;
23197dc7771cSEwan Crawford         }
23207dc7771cSEwan Crawford 
23217dc7771cSEwan Crawford         RenderScriptRuntime *runtime =
23227dc7771cSEwan Crawford           static_cast<RenderScriptRuntime *>(m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript));
23237dc7771cSEwan Crawford 
23247dc7771cSEwan Crawford         bool do_break = false;
23257dc7771cSEwan Crawford         const char* argument = command.GetArgumentAtIndex(0);
23267dc7771cSEwan Crawford         if (strcmp(argument, "enable") == 0)
23277dc7771cSEwan Crawford         {
23287dc7771cSEwan Crawford             do_break = true;
23297dc7771cSEwan Crawford             result.AppendMessage("Breakpoints will be set on all kernels.");
23307dc7771cSEwan Crawford         }
23317dc7771cSEwan Crawford         else if (strcmp(argument, "disable") == 0)
23327dc7771cSEwan Crawford         {
23337dc7771cSEwan Crawford             do_break = false;
23347dc7771cSEwan Crawford             result.AppendMessage("Breakpoints will not be set on any new kernels.");
23357dc7771cSEwan Crawford         }
23367dc7771cSEwan Crawford         else
23377dc7771cSEwan Crawford         {
23387dc7771cSEwan Crawford             result.AppendErrorWithFormat("Argument must be either 'enable' or 'disable'");
23397dc7771cSEwan Crawford             result.SetStatus(eReturnStatusFailed);
23407dc7771cSEwan Crawford             return false;
23417dc7771cSEwan Crawford         }
23427dc7771cSEwan Crawford 
23437dc7771cSEwan Crawford         runtime->SetBreakAllKernels(do_break, m_exe_ctx.GetTargetSP());
23447dc7771cSEwan Crawford 
23457dc7771cSEwan Crawford         result.SetStatus(eReturnStatusSuccessFinishResult);
23467dc7771cSEwan Crawford         return true;
23477dc7771cSEwan Crawford     }
23487dc7771cSEwan Crawford };
23497dc7771cSEwan Crawford 
23507dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpoint : public CommandObjectMultiword
23517dc7771cSEwan Crawford {
23527dc7771cSEwan Crawford   private:
23537dc7771cSEwan Crawford   public:
23547dc7771cSEwan Crawford     CommandObjectRenderScriptRuntimeKernelBreakpoint(CommandInterpreter &interpreter)
23557dc7771cSEwan Crawford         : CommandObjectMultiword(interpreter, "renderscript kernel", "Commands that generate breakpoints on renderscript kernels.",
23567dc7771cSEwan Crawford                                  nullptr)
23577dc7771cSEwan Crawford     {
23587dc7771cSEwan Crawford         LoadSubCommand("set", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpointSet(interpreter)));
23597dc7771cSEwan Crawford         LoadSubCommand("all", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpointAll(interpreter)));
23607dc7771cSEwan Crawford     }
23617dc7771cSEwan Crawford 
23627dc7771cSEwan Crawford     ~CommandObjectRenderScriptRuntimeKernelBreakpoint() {}
23637dc7771cSEwan Crawford };
23647dc7771cSEwan Crawford 
23654640cde1SColin Riley class CommandObjectRenderScriptRuntimeKernel : public CommandObjectMultiword
23664640cde1SColin Riley {
23674640cde1SColin Riley   private:
23684640cde1SColin Riley   public:
23694640cde1SColin Riley     CommandObjectRenderScriptRuntimeKernel(CommandInterpreter &interpreter)
23704640cde1SColin Riley         : CommandObjectMultiword(interpreter, "renderscript kernel", "Commands that deal with renderscript kernels.",
23714640cde1SColin Riley                                  NULL)
23724640cde1SColin Riley     {
23734640cde1SColin Riley         LoadSubCommand("list", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelList(interpreter)));
23744640cde1SColin Riley         LoadSubCommand("breakpoint", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpoint(interpreter)));
23754640cde1SColin Riley     }
23764640cde1SColin Riley 
23774640cde1SColin Riley     ~CommandObjectRenderScriptRuntimeKernel() {}
23784640cde1SColin Riley };
23794640cde1SColin Riley 
23804640cde1SColin Riley class CommandObjectRenderScriptRuntimeContextDump : public CommandObjectParsed
23814640cde1SColin Riley {
23824640cde1SColin Riley   private:
23834640cde1SColin Riley   public:
23844640cde1SColin Riley     CommandObjectRenderScriptRuntimeContextDump(CommandInterpreter &interpreter)
23854640cde1SColin Riley         : CommandObjectParsed(interpreter, "renderscript context dump",
23864640cde1SColin Riley                               "Dumps renderscript context information.", "renderscript context dump",
23874640cde1SColin Riley                               eCommandRequiresProcess | eCommandProcessMustBeLaunched)
23884640cde1SColin Riley     {
23894640cde1SColin Riley     }
23904640cde1SColin Riley 
23914640cde1SColin Riley     ~CommandObjectRenderScriptRuntimeContextDump() {}
23924640cde1SColin Riley 
23934640cde1SColin Riley     bool
23944640cde1SColin Riley     DoExecute(Args &command, CommandReturnObject &result)
23954640cde1SColin Riley     {
23964640cde1SColin Riley         RenderScriptRuntime *runtime =
23974640cde1SColin Riley             (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
23984640cde1SColin Riley         runtime->DumpContexts(result.GetOutputStream());
23994640cde1SColin Riley         result.SetStatus(eReturnStatusSuccessFinishResult);
24004640cde1SColin Riley         return true;
24014640cde1SColin Riley     }
24024640cde1SColin Riley };
24034640cde1SColin Riley 
24044640cde1SColin Riley class CommandObjectRenderScriptRuntimeContext : public CommandObjectMultiword
24054640cde1SColin Riley {
24064640cde1SColin Riley   private:
24074640cde1SColin Riley   public:
24084640cde1SColin Riley     CommandObjectRenderScriptRuntimeContext(CommandInterpreter &interpreter)
24094640cde1SColin Riley         : CommandObjectMultiword(interpreter, "renderscript context", "Commands that deal with renderscript contexts.",
24104640cde1SColin Riley                                  NULL)
24114640cde1SColin Riley     {
24124640cde1SColin Riley         LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeContextDump(interpreter)));
24134640cde1SColin Riley     }
24144640cde1SColin Riley 
24154640cde1SColin Riley     ~CommandObjectRenderScriptRuntimeContext() {}
24164640cde1SColin Riley };
24174640cde1SColin Riley 
2418*a0f08674SEwan Crawford 
2419*a0f08674SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationDump : public CommandObjectParsed
2420*a0f08674SEwan Crawford {
2421*a0f08674SEwan Crawford   private:
2422*a0f08674SEwan Crawford   public:
2423*a0f08674SEwan Crawford     CommandObjectRenderScriptRuntimeAllocationDump(CommandInterpreter &interpreter)
2424*a0f08674SEwan Crawford         : CommandObjectParsed(interpreter, "renderscript allocation dump",
2425*a0f08674SEwan Crawford                               "Displays the contents of a particular allocation", "renderscript allocation dump <ID>",
2426*a0f08674SEwan Crawford                               eCommandRequiresProcess | eCommandProcessMustBeLaunched), m_options(interpreter)
2427*a0f08674SEwan Crawford     {
2428*a0f08674SEwan Crawford     }
2429*a0f08674SEwan Crawford 
2430*a0f08674SEwan Crawford     virtual Options*
2431*a0f08674SEwan Crawford     GetOptions()
2432*a0f08674SEwan Crawford     {
2433*a0f08674SEwan Crawford         return &m_options;
2434*a0f08674SEwan Crawford     }
2435*a0f08674SEwan Crawford 
2436*a0f08674SEwan Crawford     class CommandOptions : public Options
2437*a0f08674SEwan Crawford     {
2438*a0f08674SEwan Crawford       public:
2439*a0f08674SEwan Crawford         CommandOptions(CommandInterpreter &interpreter) : Options(interpreter)
2440*a0f08674SEwan Crawford         {
2441*a0f08674SEwan Crawford         }
2442*a0f08674SEwan Crawford 
2443*a0f08674SEwan Crawford         virtual
2444*a0f08674SEwan Crawford         ~CommandOptions()
2445*a0f08674SEwan Crawford         {
2446*a0f08674SEwan Crawford         }
2447*a0f08674SEwan Crawford 
2448*a0f08674SEwan Crawford         virtual Error
2449*a0f08674SEwan Crawford         SetOptionValue(uint32_t option_idx, const char *option_arg)
2450*a0f08674SEwan Crawford         {
2451*a0f08674SEwan Crawford             Error error;
2452*a0f08674SEwan Crawford             const int short_option = m_getopt_table[option_idx].val;
2453*a0f08674SEwan Crawford 
2454*a0f08674SEwan Crawford             switch (short_option)
2455*a0f08674SEwan Crawford             {
2456*a0f08674SEwan Crawford                 case 'f':
2457*a0f08674SEwan Crawford                     m_outfile.SetFile(option_arg, true);
2458*a0f08674SEwan Crawford                     if (m_outfile.Exists())
2459*a0f08674SEwan Crawford                     {
2460*a0f08674SEwan Crawford                         m_outfile.Clear();
2461*a0f08674SEwan Crawford                         error.SetErrorStringWithFormat("file already exists: '%s'", option_arg);
2462*a0f08674SEwan Crawford                     }
2463*a0f08674SEwan Crawford                     break;
2464*a0f08674SEwan Crawford                 default:
2465*a0f08674SEwan Crawford                     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
2466*a0f08674SEwan Crawford                     break;
2467*a0f08674SEwan Crawford             }
2468*a0f08674SEwan Crawford             return error;
2469*a0f08674SEwan Crawford         }
2470*a0f08674SEwan Crawford 
2471*a0f08674SEwan Crawford         void
2472*a0f08674SEwan Crawford         OptionParsingStarting()
2473*a0f08674SEwan Crawford         {
2474*a0f08674SEwan Crawford             m_outfile.Clear();
2475*a0f08674SEwan Crawford         }
2476*a0f08674SEwan Crawford 
2477*a0f08674SEwan Crawford         const OptionDefinition*
2478*a0f08674SEwan Crawford         GetDefinitions()
2479*a0f08674SEwan Crawford         {
2480*a0f08674SEwan Crawford             return g_option_table;
2481*a0f08674SEwan Crawford         }
2482*a0f08674SEwan Crawford 
2483*a0f08674SEwan Crawford         static OptionDefinition g_option_table[];
2484*a0f08674SEwan Crawford         FileSpec m_outfile;
2485*a0f08674SEwan Crawford     };
2486*a0f08674SEwan Crawford 
2487*a0f08674SEwan Crawford     ~CommandObjectRenderScriptRuntimeAllocationDump() {}
2488*a0f08674SEwan Crawford 
2489*a0f08674SEwan Crawford     bool
2490*a0f08674SEwan Crawford     DoExecute(Args &command, CommandReturnObject &result)
2491*a0f08674SEwan Crawford     {
2492*a0f08674SEwan Crawford         const size_t argc = command.GetArgumentCount();
2493*a0f08674SEwan Crawford         if (argc < 1)
2494*a0f08674SEwan Crawford         {
2495*a0f08674SEwan Crawford             result.AppendErrorWithFormat("'%s' takes 1 argument, an allocation ID. As well as an optional -f argument",
2496*a0f08674SEwan Crawford                                          m_cmd_name.c_str());
2497*a0f08674SEwan Crawford             result.SetStatus(eReturnStatusFailed);
2498*a0f08674SEwan Crawford             return false;
2499*a0f08674SEwan Crawford         }
2500*a0f08674SEwan Crawford 
2501*a0f08674SEwan Crawford         RenderScriptRuntime *runtime =
2502*a0f08674SEwan Crawford           static_cast<RenderScriptRuntime *>(m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript));
2503*a0f08674SEwan Crawford 
2504*a0f08674SEwan Crawford         const char* id_cstr = command.GetArgumentAtIndex(0);
2505*a0f08674SEwan Crawford         bool convert_complete = false;
2506*a0f08674SEwan Crawford         const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete);
2507*a0f08674SEwan Crawford         if (!convert_complete)
2508*a0f08674SEwan Crawford         {
2509*a0f08674SEwan Crawford             result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr);
2510*a0f08674SEwan Crawford             result.SetStatus(eReturnStatusFailed);
2511*a0f08674SEwan Crawford             return false;
2512*a0f08674SEwan Crawford         }
2513*a0f08674SEwan Crawford 
2514*a0f08674SEwan Crawford         Stream* output_strm = nullptr;
2515*a0f08674SEwan Crawford         StreamFile outfile_stream;
2516*a0f08674SEwan Crawford         const FileSpec &outfile_spec = m_options.m_outfile; // Dump allocation to file instead
2517*a0f08674SEwan Crawford         if (outfile_spec)
2518*a0f08674SEwan Crawford         {
2519*a0f08674SEwan Crawford             // Open output file
2520*a0f08674SEwan Crawford             char path[256];
2521*a0f08674SEwan Crawford             outfile_spec.GetPath(path, sizeof(path));
2522*a0f08674SEwan Crawford             if (outfile_stream.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate).Success())
2523*a0f08674SEwan Crawford             {
2524*a0f08674SEwan Crawford                 output_strm = &outfile_stream;
2525*a0f08674SEwan Crawford                 result.GetOutputStream().Printf("Results written to '%s'", path);
2526*a0f08674SEwan Crawford                 result.GetOutputStream().EOL();
2527*a0f08674SEwan Crawford             }
2528*a0f08674SEwan Crawford             else
2529*a0f08674SEwan Crawford             {
2530*a0f08674SEwan Crawford                 result.AppendErrorWithFormat("Couldn't open file '%s'", path);
2531*a0f08674SEwan Crawford                 result.SetStatus(eReturnStatusFailed);
2532*a0f08674SEwan Crawford                 return false;
2533*a0f08674SEwan Crawford             }
2534*a0f08674SEwan Crawford         }
2535*a0f08674SEwan Crawford         else
2536*a0f08674SEwan Crawford             output_strm = &result.GetOutputStream();
2537*a0f08674SEwan Crawford 
2538*a0f08674SEwan Crawford         assert(output_strm != nullptr);
2539*a0f08674SEwan Crawford         bool success = runtime->DumpAllocation(*output_strm, m_exe_ctx.GetFramePtr(), id);
2540*a0f08674SEwan Crawford 
2541*a0f08674SEwan Crawford         if (success)
2542*a0f08674SEwan Crawford             result.SetStatus(eReturnStatusSuccessFinishResult);
2543*a0f08674SEwan Crawford         else
2544*a0f08674SEwan Crawford             result.SetStatus(eReturnStatusFailed);
2545*a0f08674SEwan Crawford 
2546*a0f08674SEwan Crawford         return true;
2547*a0f08674SEwan Crawford     }
2548*a0f08674SEwan Crawford 
2549*a0f08674SEwan Crawford     private:
2550*a0f08674SEwan Crawford         CommandOptions m_options;
2551*a0f08674SEwan Crawford };
2552*a0f08674SEwan Crawford 
2553*a0f08674SEwan Crawford OptionDefinition
2554*a0f08674SEwan Crawford CommandObjectRenderScriptRuntimeAllocationDump::CommandOptions::g_option_table[] =
2555*a0f08674SEwan Crawford {
2556*a0f08674SEwan Crawford     { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename,
2557*a0f08674SEwan Crawford       "Print results to specified file instead of command line."},
2558*a0f08674SEwan Crawford     { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
2559*a0f08674SEwan Crawford };
2560*a0f08674SEwan Crawford 
2561*a0f08674SEwan Crawford 
256215f2bd95SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationList : public CommandObjectParsed
256315f2bd95SEwan Crawford {
256415f2bd95SEwan Crawford   public:
256515f2bd95SEwan Crawford     CommandObjectRenderScriptRuntimeAllocationList(CommandInterpreter &interpreter)
256615f2bd95SEwan Crawford         : CommandObjectParsed(interpreter, "renderscript allocation list",
256715f2bd95SEwan Crawford                               "List renderscript allocations and their information.", "renderscript allocation list",
256815f2bd95SEwan Crawford                               eCommandRequiresProcess | eCommandProcessMustBeLaunched), m_options(interpreter)
256915f2bd95SEwan Crawford     {
257015f2bd95SEwan Crawford     }
257115f2bd95SEwan Crawford 
257215f2bd95SEwan Crawford     virtual Options*
257315f2bd95SEwan Crawford     GetOptions()
257415f2bd95SEwan Crawford     {
257515f2bd95SEwan Crawford         return &m_options;
257615f2bd95SEwan Crawford     }
257715f2bd95SEwan Crawford 
257815f2bd95SEwan Crawford     class CommandOptions : public Options
257915f2bd95SEwan Crawford     {
258015f2bd95SEwan Crawford       public:
258115f2bd95SEwan Crawford         CommandOptions(CommandInterpreter &interpreter) : Options(interpreter), m_refresh(false)
258215f2bd95SEwan Crawford         {
258315f2bd95SEwan Crawford         }
258415f2bd95SEwan Crawford 
258515f2bd95SEwan Crawford         virtual
258615f2bd95SEwan Crawford         ~CommandOptions()
258715f2bd95SEwan Crawford         {
258815f2bd95SEwan Crawford         }
258915f2bd95SEwan Crawford 
259015f2bd95SEwan Crawford         virtual Error
259115f2bd95SEwan Crawford         SetOptionValue(uint32_t option_idx, const char *option_arg)
259215f2bd95SEwan Crawford         {
259315f2bd95SEwan Crawford             Error error;
259415f2bd95SEwan Crawford             const int short_option = m_getopt_table[option_idx].val;
259515f2bd95SEwan Crawford 
259615f2bd95SEwan Crawford             switch (short_option)
259715f2bd95SEwan Crawford             {
259815f2bd95SEwan Crawford                 case 'r':
259915f2bd95SEwan Crawford                     m_refresh = true;
260015f2bd95SEwan Crawford                     break;
260115f2bd95SEwan Crawford                 default:
260215f2bd95SEwan Crawford                     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
260315f2bd95SEwan Crawford                     break;
260415f2bd95SEwan Crawford             }
260515f2bd95SEwan Crawford             return error;
260615f2bd95SEwan Crawford         }
260715f2bd95SEwan Crawford 
260815f2bd95SEwan Crawford         void
260915f2bd95SEwan Crawford         OptionParsingStarting()
261015f2bd95SEwan Crawford         {
261115f2bd95SEwan Crawford             m_refresh = false;
261215f2bd95SEwan Crawford         }
261315f2bd95SEwan Crawford 
261415f2bd95SEwan Crawford         const OptionDefinition*
261515f2bd95SEwan Crawford         GetDefinitions()
261615f2bd95SEwan Crawford         {
261715f2bd95SEwan Crawford             return g_option_table;
261815f2bd95SEwan Crawford         }
261915f2bd95SEwan Crawford 
262015f2bd95SEwan Crawford         static OptionDefinition g_option_table[];
262115f2bd95SEwan Crawford         bool m_refresh;
262215f2bd95SEwan Crawford     };
262315f2bd95SEwan Crawford 
262415f2bd95SEwan Crawford     ~CommandObjectRenderScriptRuntimeAllocationList() {}
262515f2bd95SEwan Crawford 
262615f2bd95SEwan Crawford     bool
262715f2bd95SEwan Crawford     DoExecute(Args &command, CommandReturnObject &result)
262815f2bd95SEwan Crawford     {
262915f2bd95SEwan Crawford         RenderScriptRuntime *runtime =
263015f2bd95SEwan Crawford           static_cast<RenderScriptRuntime *>(m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript));
263115f2bd95SEwan Crawford         runtime->ListAllocations(result.GetOutputStream(), m_exe_ctx.GetFramePtr(), m_options.m_refresh);
263215f2bd95SEwan Crawford         result.SetStatus(eReturnStatusSuccessFinishResult);
263315f2bd95SEwan Crawford         return true;
263415f2bd95SEwan Crawford     }
263515f2bd95SEwan Crawford 
263615f2bd95SEwan Crawford   private:
263715f2bd95SEwan Crawford     CommandOptions m_options;
263815f2bd95SEwan Crawford };
263915f2bd95SEwan Crawford 
264015f2bd95SEwan Crawford OptionDefinition
264115f2bd95SEwan Crawford CommandObjectRenderScriptRuntimeAllocationList::CommandOptions::g_option_table[] =
264215f2bd95SEwan Crawford {
264315f2bd95SEwan Crawford     { LLDB_OPT_SET_1, false, "refresh", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone,
264415f2bd95SEwan Crawford       "Recompute allocation details."},
264515f2bd95SEwan Crawford     { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
264615f2bd95SEwan Crawford };
264715f2bd95SEwan Crawford 
264815f2bd95SEwan Crawford 
264915f2bd95SEwan Crawford class CommandObjectRenderScriptRuntimeAllocation : public CommandObjectMultiword
265015f2bd95SEwan Crawford {
265115f2bd95SEwan Crawford   private:
265215f2bd95SEwan Crawford   public:
265315f2bd95SEwan Crawford     CommandObjectRenderScriptRuntimeAllocation(CommandInterpreter &interpreter)
265415f2bd95SEwan Crawford         : CommandObjectMultiword(interpreter, "renderscript allocation", "Commands that deal with renderscript allocations.",
265515f2bd95SEwan Crawford                                  NULL)
265615f2bd95SEwan Crawford     {
265715f2bd95SEwan Crawford         LoadSubCommand("list", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationList(interpreter)));
2658*a0f08674SEwan Crawford         LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationDump(interpreter)));
265915f2bd95SEwan Crawford     }
266015f2bd95SEwan Crawford 
266115f2bd95SEwan Crawford     ~CommandObjectRenderScriptRuntimeAllocation() {}
266215f2bd95SEwan Crawford };
266315f2bd95SEwan Crawford 
266415f2bd95SEwan Crawford 
26654640cde1SColin Riley class CommandObjectRenderScriptRuntimeStatus : public CommandObjectParsed
26664640cde1SColin Riley {
26674640cde1SColin Riley   private:
26684640cde1SColin Riley   public:
26694640cde1SColin Riley     CommandObjectRenderScriptRuntimeStatus(CommandInterpreter &interpreter)
26704640cde1SColin Riley         : CommandObjectParsed(interpreter, "renderscript status",
26714640cde1SColin Riley                               "Displays current renderscript runtime status.", "renderscript status",
26724640cde1SColin Riley                               eCommandRequiresProcess | eCommandProcessMustBeLaunched)
26734640cde1SColin Riley     {
26744640cde1SColin Riley     }
26754640cde1SColin Riley 
26764640cde1SColin Riley     ~CommandObjectRenderScriptRuntimeStatus() {}
26774640cde1SColin Riley 
26784640cde1SColin Riley     bool
26794640cde1SColin Riley     DoExecute(Args &command, CommandReturnObject &result)
26804640cde1SColin Riley     {
26814640cde1SColin Riley         RenderScriptRuntime *runtime =
26824640cde1SColin Riley             (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript);
26834640cde1SColin Riley         runtime->Status(result.GetOutputStream());
26844640cde1SColin Riley         result.SetStatus(eReturnStatusSuccessFinishResult);
26854640cde1SColin Riley         return true;
26864640cde1SColin Riley     }
26874640cde1SColin Riley };
26884640cde1SColin Riley 
26895ec532a9SColin Riley class CommandObjectRenderScriptRuntime : public CommandObjectMultiword
26905ec532a9SColin Riley {
26915ec532a9SColin Riley   public:
26925ec532a9SColin Riley     CommandObjectRenderScriptRuntime(CommandInterpreter &interpreter)
26935ec532a9SColin Riley         : CommandObjectMultiword(interpreter, "renderscript", "A set of commands for operating on renderscript.",
26945ec532a9SColin Riley                                  "renderscript <subcommand> [<subcommand-options>]")
26955ec532a9SColin Riley     {
26965ec532a9SColin Riley         LoadSubCommand("module", CommandObjectSP(new CommandObjectRenderScriptRuntimeModule(interpreter)));
26974640cde1SColin Riley         LoadSubCommand("status", CommandObjectSP(new CommandObjectRenderScriptRuntimeStatus(interpreter)));
26984640cde1SColin Riley         LoadSubCommand("kernel", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernel(interpreter)));
26994640cde1SColin Riley         LoadSubCommand("context", CommandObjectSP(new CommandObjectRenderScriptRuntimeContext(interpreter)));
270015f2bd95SEwan Crawford         LoadSubCommand("allocation", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocation(interpreter)));
27015ec532a9SColin Riley     }
27025ec532a9SColin Riley 
27035ec532a9SColin Riley     ~CommandObjectRenderScriptRuntime() {}
27045ec532a9SColin Riley };
2705ef20b08fSColin Riley 
2706ef20b08fSColin Riley void
2707ef20b08fSColin Riley RenderScriptRuntime::Initiate()
27085ec532a9SColin Riley {
2709ef20b08fSColin Riley     assert(!m_initiated);
27105ec532a9SColin Riley }
2711ef20b08fSColin Riley 
2712ef20b08fSColin Riley RenderScriptRuntime::RenderScriptRuntime(Process *process)
27137dc7771cSEwan Crawford     : lldb_private::CPPLanguageRuntime(process), m_initiated(false), m_debuggerPresentFlagged(false),
27147dc7771cSEwan Crawford       m_breakAllKernels(false)
2715ef20b08fSColin Riley {
27164640cde1SColin Riley     ModulesDidLoad(process->GetTarget().GetImages());
2717ef20b08fSColin Riley }
27184640cde1SColin Riley 
27194640cde1SColin Riley lldb::CommandObjectSP
27204640cde1SColin Riley RenderScriptRuntime::GetCommandObject(lldb_private::CommandInterpreter& interpreter)
27214640cde1SColin Riley {
27224640cde1SColin Riley     static CommandObjectSP command_object;
27234640cde1SColin Riley     if(!command_object)
27244640cde1SColin Riley     {
27254640cde1SColin Riley         command_object.reset(new CommandObjectRenderScriptRuntime(interpreter));
27264640cde1SColin Riley     }
27274640cde1SColin Riley     return command_object;
27284640cde1SColin Riley }
27294640cde1SColin Riley 
273078f339d1SEwan Crawford RenderScriptRuntime::~RenderScriptRuntime() = default;
2731