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 10222b937cSEugene Zelenko // C Includes 11222b937cSEugene Zelenko // C++ Includes 12222b937cSEugene Zelenko // Other libraries and framework includes 13222b937cSEugene Zelenko // Project includes 145ec532a9SColin Riley #include "RenderScriptRuntime.h" 155ec532a9SColin Riley 16b3f7f69dSAidan Dodds #include "lldb/Breakpoint/StoppointCallbackContext.h" 175ec532a9SColin Riley #include "lldb/Core/ConstString.h" 185ec532a9SColin Riley #include "lldb/Core/Debugger.h" 195ec532a9SColin Riley #include "lldb/Core/Error.h" 205ec532a9SColin Riley #include "lldb/Core/Log.h" 215ec532a9SColin Riley #include "lldb/Core/PluginManager.h" 22018f5a7eSEwan Crawford #include "lldb/Core/RegularExpression.h" 23b3f7f69dSAidan Dodds #include "lldb/Core/ValueObjectVariable.h" 248b244e21SEwan Crawford #include "lldb/DataFormatters/DumpValueObjectOptions.h" 25b3f7f69dSAidan Dodds #include "lldb/Expression/UserExpression.h" 26a0f08674SEwan Crawford #include "lldb/Host/StringConvert.h" 27b3f7f69dSAidan Dodds #include "lldb/Interpreter/Args.h" 28b3f7f69dSAidan Dodds #include "lldb/Interpreter/CommandInterpreter.h" 29b3f7f69dSAidan Dodds #include "lldb/Interpreter/CommandObjectMultiword.h" 30b3f7f69dSAidan Dodds #include "lldb/Interpreter/CommandReturnObject.h" 31b3f7f69dSAidan Dodds #include "lldb/Interpreter/Options.h" 325ec532a9SColin Riley #include "lldb/Symbol/Symbol.h" 334640cde1SColin Riley #include "lldb/Symbol/Type.h" 34b3f7f69dSAidan Dodds #include "lldb/Symbol/VariableList.h" 355ec532a9SColin Riley #include "lldb/Target/Process.h" 36b3f7f69dSAidan Dodds #include "lldb/Target/RegisterContext.h" 375ec532a9SColin Riley #include "lldb/Target/Target.h" 38018f5a7eSEwan Crawford #include "lldb/Target/Thread.h" 395ec532a9SColin Riley 405ec532a9SColin Riley using namespace lldb; 415ec532a9SColin Riley using namespace lldb_private; 4298156583SEwan Crawford using namespace lldb_renderscript; 435ec532a9SColin Riley 44b3f7f69dSAidan Dodds namespace 45b3f7f69dSAidan Dodds { 4678f339d1SEwan Crawford 4778f339d1SEwan Crawford // The empirical_type adds a basic level of validation to arbitrary data 4878f339d1SEwan Crawford // allowing us to track if data has been discovered and stored or not. 4978f339d1SEwan Crawford // An empirical_type will be marked as valid only if it has been explicitly assigned to. 50b3f7f69dSAidan Dodds template <typename type_t> class empirical_type 5178f339d1SEwan Crawford { 5278f339d1SEwan Crawford public: 5378f339d1SEwan Crawford // Ctor. Contents is invalid when constructed. 54b3f7f69dSAidan Dodds empirical_type() : valid(false) {} 5578f339d1SEwan Crawford 5678f339d1SEwan Crawford // Return true and copy contents to out if valid, else return false. 57b3f7f69dSAidan Dodds bool 58b3f7f69dSAidan Dodds get(type_t &out) const 5978f339d1SEwan Crawford { 6078f339d1SEwan Crawford if (valid) 6178f339d1SEwan Crawford out = data; 6278f339d1SEwan Crawford return valid; 6378f339d1SEwan Crawford } 6478f339d1SEwan Crawford 6578f339d1SEwan Crawford // Return a pointer to the contents or nullptr if it was not valid. 66b3f7f69dSAidan Dodds const type_t * 67b3f7f69dSAidan Dodds get() const 6878f339d1SEwan Crawford { 6978f339d1SEwan Crawford return valid ? &data : nullptr; 7078f339d1SEwan Crawford } 7178f339d1SEwan Crawford 7278f339d1SEwan Crawford // Assign data explicitly. 73b3f7f69dSAidan Dodds void 74b3f7f69dSAidan Dodds set(const type_t in) 7578f339d1SEwan Crawford { 7678f339d1SEwan Crawford data = in; 7778f339d1SEwan Crawford valid = true; 7878f339d1SEwan Crawford } 7978f339d1SEwan Crawford 8078f339d1SEwan Crawford // Mark contents as invalid. 81b3f7f69dSAidan Dodds void 82b3f7f69dSAidan Dodds invalidate() 8378f339d1SEwan Crawford { 8478f339d1SEwan Crawford valid = false; 8578f339d1SEwan Crawford } 8678f339d1SEwan Crawford 8778f339d1SEwan Crawford // Returns true if this type contains valid data. 88b3f7f69dSAidan Dodds bool 89b3f7f69dSAidan Dodds isValid() const 9078f339d1SEwan Crawford { 9178f339d1SEwan Crawford return valid; 9278f339d1SEwan Crawford } 9378f339d1SEwan Crawford 9478f339d1SEwan Crawford // Assignment operator. 95b3f7f69dSAidan Dodds empirical_type<type_t> & 96b3f7f69dSAidan Dodds operator=(const type_t in) 9778f339d1SEwan Crawford { 9878f339d1SEwan Crawford set(in); 9978f339d1SEwan Crawford return *this; 10078f339d1SEwan Crawford } 10178f339d1SEwan Crawford 10278f339d1SEwan Crawford // Dereference operator returns contents. 10378f339d1SEwan Crawford // Warning: Will assert if not valid so use only when you know data is valid. 10478f339d1SEwan Crawford const type_t &operator*() const 10578f339d1SEwan Crawford { 10678f339d1SEwan Crawford assert(valid); 10778f339d1SEwan Crawford return data; 10878f339d1SEwan Crawford } 10978f339d1SEwan Crawford 11078f339d1SEwan Crawford protected: 11178f339d1SEwan Crawford bool valid; 11278f339d1SEwan Crawford type_t data; 11378f339d1SEwan Crawford }; 11478f339d1SEwan Crawford 115222b937cSEugene Zelenko } // anonymous namespace 11678f339d1SEwan Crawford 11778f339d1SEwan Crawford // The ScriptDetails class collects data associated with a single script instance. 11878f339d1SEwan Crawford struct RenderScriptRuntime::ScriptDetails 11978f339d1SEwan Crawford { 120222b937cSEugene Zelenko ~ScriptDetails() = default; 12178f339d1SEwan Crawford 12278f339d1SEwan Crawford enum ScriptType 12378f339d1SEwan Crawford { 12478f339d1SEwan Crawford eScript, 12578f339d1SEwan Crawford eScriptC 12678f339d1SEwan Crawford }; 12778f339d1SEwan Crawford 12878f339d1SEwan Crawford // The derived type of the script. 12978f339d1SEwan Crawford empirical_type<ScriptType> type; 13078f339d1SEwan Crawford // The name of the original source file. 13178f339d1SEwan Crawford empirical_type<std::string> resName; 13278f339d1SEwan Crawford // Path to script .so file on the device. 13378f339d1SEwan Crawford empirical_type<std::string> scriptDyLib; 13478f339d1SEwan Crawford // Directory where kernel objects are cached on device. 13578f339d1SEwan Crawford empirical_type<std::string> cacheDir; 13678f339d1SEwan Crawford // Pointer to the context which owns this script. 13778f339d1SEwan Crawford empirical_type<lldb::addr_t> context; 13878f339d1SEwan Crawford // Pointer to the script object itself. 13978f339d1SEwan Crawford empirical_type<lldb::addr_t> script; 14078f339d1SEwan Crawford }; 14178f339d1SEwan Crawford 1428b244e21SEwan Crawford // This Element class represents the Element object in RS, 1438b244e21SEwan Crawford // defining the type associated with an Allocation. 1448b244e21SEwan Crawford struct RenderScriptRuntime::Element 14578f339d1SEwan Crawford { 14615f2bd95SEwan Crawford // Taken from rsDefines.h 14715f2bd95SEwan Crawford enum DataKind 14815f2bd95SEwan Crawford { 14915f2bd95SEwan Crawford RS_KIND_USER, 15015f2bd95SEwan Crawford RS_KIND_PIXEL_L = 7, 15115f2bd95SEwan Crawford RS_KIND_PIXEL_A, 15215f2bd95SEwan Crawford RS_KIND_PIXEL_LA, 15315f2bd95SEwan Crawford RS_KIND_PIXEL_RGB, 15415f2bd95SEwan Crawford RS_KIND_PIXEL_RGBA, 15515f2bd95SEwan Crawford RS_KIND_PIXEL_DEPTH, 15615f2bd95SEwan Crawford RS_KIND_PIXEL_YUV, 15715f2bd95SEwan Crawford RS_KIND_INVALID = 100 15815f2bd95SEwan Crawford }; 15978f339d1SEwan Crawford 16015f2bd95SEwan Crawford // Taken from rsDefines.h 16178f339d1SEwan Crawford enum DataType 16278f339d1SEwan Crawford { 16315f2bd95SEwan Crawford RS_TYPE_NONE = 0, 16415f2bd95SEwan Crawford RS_TYPE_FLOAT_16, 16515f2bd95SEwan Crawford RS_TYPE_FLOAT_32, 16615f2bd95SEwan Crawford RS_TYPE_FLOAT_64, 16715f2bd95SEwan Crawford RS_TYPE_SIGNED_8, 16815f2bd95SEwan Crawford RS_TYPE_SIGNED_16, 16915f2bd95SEwan Crawford RS_TYPE_SIGNED_32, 17015f2bd95SEwan Crawford RS_TYPE_SIGNED_64, 17115f2bd95SEwan Crawford RS_TYPE_UNSIGNED_8, 17215f2bd95SEwan Crawford RS_TYPE_UNSIGNED_16, 17315f2bd95SEwan Crawford RS_TYPE_UNSIGNED_32, 17415f2bd95SEwan Crawford RS_TYPE_UNSIGNED_64, 1752e920715SEwan Crawford RS_TYPE_BOOLEAN, 1762e920715SEwan Crawford 1772e920715SEwan Crawford RS_TYPE_UNSIGNED_5_6_5, 1782e920715SEwan Crawford RS_TYPE_UNSIGNED_5_5_5_1, 1792e920715SEwan Crawford RS_TYPE_UNSIGNED_4_4_4_4, 1802e920715SEwan Crawford 1812e920715SEwan Crawford RS_TYPE_MATRIX_4X4, 1822e920715SEwan Crawford RS_TYPE_MATRIX_3X3, 1832e920715SEwan Crawford RS_TYPE_MATRIX_2X2, 1842e920715SEwan Crawford 1852e920715SEwan Crawford RS_TYPE_ELEMENT = 1000, 1862e920715SEwan Crawford RS_TYPE_TYPE, 1872e920715SEwan Crawford RS_TYPE_ALLOCATION, 1882e920715SEwan Crawford RS_TYPE_SAMPLER, 1892e920715SEwan Crawford RS_TYPE_SCRIPT, 1902e920715SEwan Crawford RS_TYPE_MESH, 1912e920715SEwan Crawford RS_TYPE_PROGRAM_FRAGMENT, 1922e920715SEwan Crawford RS_TYPE_PROGRAM_VERTEX, 1932e920715SEwan Crawford RS_TYPE_PROGRAM_RASTER, 1942e920715SEwan Crawford RS_TYPE_PROGRAM_STORE, 1952e920715SEwan Crawford RS_TYPE_FONT, 1962e920715SEwan Crawford 1972e920715SEwan Crawford RS_TYPE_INVALID = 10000 19878f339d1SEwan Crawford }; 19978f339d1SEwan Crawford 2008b244e21SEwan Crawford std::vector<Element> children; // Child Element fields for structs 2018b244e21SEwan Crawford empirical_type<lldb::addr_t> element_ptr; // Pointer to the RS Element of the Type 2028b244e21SEwan Crawford empirical_type<DataType> type; // Type of each data pointer stored by the allocation 2038b244e21SEwan Crawford empirical_type<DataKind> type_kind; // Defines pixel type if Allocation is created from an image 2048b244e21SEwan Crawford empirical_type<uint32_t> type_vec_size; // Vector size of each data point, e.g '4' for uchar4 2058b244e21SEwan Crawford empirical_type<uint32_t> field_count; // Number of Subelements 2068b244e21SEwan Crawford empirical_type<uint32_t> datum_size; // Size of a single Element with padding 2078b244e21SEwan Crawford empirical_type<uint32_t> padding; // Number of padding bytes 2088b244e21SEwan Crawford empirical_type<uint32_t> array_size; // Number of items in array, only needed for strucrs 2098b244e21SEwan Crawford ConstString type_name; // Name of type, only needed for structs 2108b244e21SEwan Crawford 211b3f7f69dSAidan Dodds static const ConstString & 212b3f7f69dSAidan Dodds GetFallbackStructName(); // Print this as the type name of a struct Element 2138b244e21SEwan Crawford // If we can't resolve the actual struct name 2148b59062aSEwan Crawford 215b3f7f69dSAidan Dodds bool 216b3f7f69dSAidan Dodds shouldRefresh() const 2178b59062aSEwan Crawford { 2188b59062aSEwan Crawford const bool valid_ptr = element_ptr.isValid() && *element_ptr.get() != 0x0; 2198b59062aSEwan Crawford const bool valid_type = type.isValid() && type_vec_size.isValid() && type_kind.isValid(); 2208b59062aSEwan Crawford return !valid_ptr || !valid_type || !datum_size.isValid(); 2218b59062aSEwan Crawford } 2228b244e21SEwan Crawford }; 2238b244e21SEwan Crawford 2248b244e21SEwan Crawford // This AllocationDetails class collects data associated with a single 2258b244e21SEwan Crawford // allocation instance. 2268b244e21SEwan Crawford struct RenderScriptRuntime::AllocationDetails 2278b244e21SEwan Crawford { 22815f2bd95SEwan Crawford struct Dimension 22978f339d1SEwan Crawford { 23015f2bd95SEwan Crawford uint32_t dim_1; 23115f2bd95SEwan Crawford uint32_t dim_2; 23215f2bd95SEwan Crawford uint32_t dim_3; 23315f2bd95SEwan Crawford uint32_t cubeMap; 23415f2bd95SEwan Crawford 23515f2bd95SEwan Crawford Dimension() 23615f2bd95SEwan Crawford { 23715f2bd95SEwan Crawford dim_1 = 0; 23815f2bd95SEwan Crawford dim_2 = 0; 23915f2bd95SEwan Crawford dim_3 = 0; 24015f2bd95SEwan Crawford cubeMap = 0; 24115f2bd95SEwan Crawford } 24278f339d1SEwan Crawford }; 24378f339d1SEwan Crawford 24426e52a70SEwan Crawford // The FileHeader struct specifies the header we use for writing allocations to a binary file. 24526e52a70SEwan Crawford // Our format begins with the ASCII characters "RSAD", identifying the file as an allocation dump. 24626e52a70SEwan Crawford // Member variables dims and hdr_size are then written consecutively, immediately followed by an instance of 24726e52a70SEwan Crawford // the ElementHeader struct. Because Elements can contain subelements, there may be more than one instance 24826e52a70SEwan Crawford // of the ElementHeader struct. With this first instance being the root element, and the other instances being 24926e52a70SEwan Crawford // the root's descendants. To identify which instances are an ElementHeader's children, each struct 25026e52a70SEwan Crawford // is immediately followed by a sequence of consecutive offsets to the start of its child structs. 25126e52a70SEwan Crawford // These offsets are 4 bytes in size, and the 0 offset signifies no more children. 25255232f09SEwan Crawford struct FileHeader 25355232f09SEwan Crawford { 25455232f09SEwan Crawford uint8_t ident[4]; // ASCII 'RSAD' identifying the file 25526e52a70SEwan Crawford uint32_t dims[3]; // Dimensions 25626e52a70SEwan Crawford uint16_t hdr_size; // Header size in bytes, including all element headers 25726e52a70SEwan Crawford }; 25826e52a70SEwan Crawford 25926e52a70SEwan Crawford struct ElementHeader 26026e52a70SEwan Crawford { 26155232f09SEwan Crawford uint16_t type; // DataType enum 26255232f09SEwan Crawford uint32_t kind; // DataKind enum 26355232f09SEwan Crawford uint32_t element_size; // Size of a single element, including padding 26426e52a70SEwan Crawford uint16_t vector_size; // Vector width 26526e52a70SEwan Crawford uint32_t array_size; // Number of elements in array 26655232f09SEwan Crawford }; 26755232f09SEwan Crawford 26815f2bd95SEwan Crawford // Monotonically increasing from 1 269b3f7f69dSAidan Dodds static uint32_t ID; 27015f2bd95SEwan Crawford 27115f2bd95SEwan Crawford // Maps Allocation DataType enum and vector size to printable strings 27215f2bd95SEwan Crawford // using mapping from RenderScript numerical types summary documentation 27315f2bd95SEwan Crawford static const char *RsDataTypeToString[][4]; 27415f2bd95SEwan Crawford 27515f2bd95SEwan Crawford // Maps Allocation DataKind enum to printable strings 27615f2bd95SEwan Crawford static const char *RsDataKindToString[]; 27715f2bd95SEwan Crawford 278a0f08674SEwan Crawford // Maps allocation types to format sizes for printing. 279b3f7f69dSAidan Dodds static const uint32_t RSTypeToFormat[][3]; 280a0f08674SEwan Crawford 28115f2bd95SEwan Crawford // Give each allocation an ID as a way 28215f2bd95SEwan Crawford // for commands to reference it. 283b3f7f69dSAidan Dodds const uint32_t id; 28415f2bd95SEwan Crawford 2858b244e21SEwan Crawford RenderScriptRuntime::Element element; // Allocation Element type 28615f2bd95SEwan Crawford empirical_type<Dimension> dimension; // Dimensions of the Allocation 28715f2bd95SEwan Crawford empirical_type<lldb::addr_t> address; // Pointer to address of the RS Allocation 28815f2bd95SEwan Crawford empirical_type<lldb::addr_t> data_ptr; // Pointer to the data held by the Allocation 28915f2bd95SEwan Crawford empirical_type<lldb::addr_t> type_ptr; // Pointer to the RS Type of the Allocation 29015f2bd95SEwan Crawford empirical_type<lldb::addr_t> context; // Pointer to the RS Context of the Allocation 291a0f08674SEwan Crawford empirical_type<uint32_t> size; // Size of the allocation 292a0f08674SEwan Crawford empirical_type<uint32_t> stride; // Stride between rows of the allocation 29315f2bd95SEwan Crawford 29415f2bd95SEwan Crawford // Give each allocation an id, so we can reference it in user commands. 295b3f7f69dSAidan Dodds AllocationDetails() : id(ID++) {} 2968b59062aSEwan Crawford 297b3f7f69dSAidan Dodds bool 298b3f7f69dSAidan Dodds shouldRefresh() const 2998b59062aSEwan Crawford { 3008b59062aSEwan Crawford bool valid_ptrs = data_ptr.isValid() && *data_ptr.get() != 0x0; 3018b59062aSEwan Crawford valid_ptrs = valid_ptrs && type_ptr.isValid() && *type_ptr.get() != 0x0; 3028b59062aSEwan Crawford return !valid_ptrs || !dimension.isValid() || !size.isValid() || element.shouldRefresh(); 3038b59062aSEwan Crawford } 30415f2bd95SEwan Crawford }; 30515f2bd95SEwan Crawford 306fe06b5adSAdrian McCarthy const ConstString & 307fe06b5adSAdrian McCarthy RenderScriptRuntime::Element::GetFallbackStructName() 308fe06b5adSAdrian McCarthy { 309fe06b5adSAdrian McCarthy static const ConstString FallbackStructName("struct"); 310fe06b5adSAdrian McCarthy return FallbackStructName; 311fe06b5adSAdrian McCarthy } 3128b244e21SEwan Crawford 313b3f7f69dSAidan Dodds uint32_t RenderScriptRuntime::AllocationDetails::ID = 1; 31415f2bd95SEwan Crawford 315b3f7f69dSAidan Dodds const char *RenderScriptRuntime::AllocationDetails::RsDataKindToString[] = { 31615f2bd95SEwan Crawford "User", 317b3f7f69dSAidan Dodds "Undefined", "Undefined", "Undefined", "Undefined", "Undefined", "Undefined", // Enum jumps from 0 to 7 318b3f7f69dSAidan Dodds "L Pixel", "A Pixel", "LA Pixel", "RGB Pixel", 319b3f7f69dSAidan Dodds "RGBA Pixel", "Pixel Depth", "YUV Pixel"}; 32015f2bd95SEwan Crawford 321b3f7f69dSAidan Dodds const char *RenderScriptRuntime::AllocationDetails::RsDataTypeToString[][4] = { 32215f2bd95SEwan Crawford {"None", "None", "None", "None"}, 32315f2bd95SEwan Crawford {"half", "half2", "half3", "half4"}, 32415f2bd95SEwan Crawford {"float", "float2", "float3", "float4"}, 32515f2bd95SEwan Crawford {"double", "double2", "double3", "double4"}, 32615f2bd95SEwan Crawford {"char", "char2", "char3", "char4"}, 32715f2bd95SEwan Crawford {"short", "short2", "short3", "short4"}, 32815f2bd95SEwan Crawford {"int", "int2", "int3", "int4"}, 32915f2bd95SEwan Crawford {"long", "long2", "long3", "long4"}, 33015f2bd95SEwan Crawford {"uchar", "uchar2", "uchar3", "uchar4"}, 33115f2bd95SEwan Crawford {"ushort", "ushort2", "ushort3", "ushort4"}, 33215f2bd95SEwan Crawford {"uint", "uint2", "uint3", "uint4"}, 33315f2bd95SEwan Crawford {"ulong", "ulong2", "ulong3", "ulong4"}, 3342e920715SEwan Crawford {"bool", "bool2", "bool3", "bool4"}, 3352e920715SEwan Crawford {"packed_565", "packed_565", "packed_565", "packed_565"}, 3362e920715SEwan Crawford {"packed_5551", "packed_5551", "packed_5551", "packed_5551"}, 3372e920715SEwan Crawford {"packed_4444", "packed_4444", "packed_4444", "packed_4444"}, 3382e920715SEwan Crawford {"rs_matrix4x4", "rs_matrix4x4", "rs_matrix4x4", "rs_matrix4x4"}, 3392e920715SEwan Crawford {"rs_matrix3x3", "rs_matrix3x3", "rs_matrix3x3", "rs_matrix3x3"}, 3402e920715SEwan Crawford {"rs_matrix2x2", "rs_matrix2x2", "rs_matrix2x2", "rs_matrix2x2"}, 3412e920715SEwan Crawford 3422e920715SEwan Crawford // Handlers 3432e920715SEwan Crawford {"RS Element", "RS Element", "RS Element", "RS Element"}, 3442e920715SEwan Crawford {"RS Type", "RS Type", "RS Type", "RS Type"}, 3452e920715SEwan Crawford {"RS Allocation", "RS Allocation", "RS Allocation", "RS Allocation"}, 3462e920715SEwan Crawford {"RS Sampler", "RS Sampler", "RS Sampler", "RS Sampler"}, 3472e920715SEwan Crawford {"RS Script", "RS Script", "RS Script", "RS Script"}, 3482e920715SEwan Crawford 3492e920715SEwan Crawford // Deprecated 3502e920715SEwan Crawford {"RS Mesh", "RS Mesh", "RS Mesh", "RS Mesh"}, 3512e920715SEwan Crawford {"RS Program Fragment", "RS Program Fragment", "RS Program Fragment", "RS Program Fragment"}, 3522e920715SEwan Crawford {"RS Program Vertex", "RS Program Vertex", "RS Program Vertex", "RS Program Vertex"}, 3532e920715SEwan Crawford {"RS Program Raster", "RS Program Raster", "RS Program Raster", "RS Program Raster"}, 3542e920715SEwan Crawford {"RS Program Store", "RS Program Store", "RS Program Store", "RS Program Store"}, 355b3f7f69dSAidan Dodds {"RS Font", "RS Font", "RS Font", "RS Font"}}; 35678f339d1SEwan Crawford 357a0f08674SEwan Crawford // Used as an index into the RSTypeToFormat array elements 358b3f7f69dSAidan Dodds enum TypeToFormatIndex 359b3f7f69dSAidan Dodds { 360a0f08674SEwan Crawford eFormatSingle = 0, 361a0f08674SEwan Crawford eFormatVector, 362a0f08674SEwan Crawford eElementSize 363a0f08674SEwan Crawford }; 364a0f08674SEwan Crawford 365a0f08674SEwan Crawford // { format enum of single element, format enum of element vector, size of element} 366b3f7f69dSAidan Dodds const uint32_t RenderScriptRuntime::AllocationDetails::RSTypeToFormat[][3] = { 367a0f08674SEwan Crawford {eFormatHex, eFormatHex, 1}, // RS_TYPE_NONE 368a0f08674SEwan Crawford {eFormatFloat, eFormatVectorOfFloat16, 2}, // RS_TYPE_FLOAT_16 369a0f08674SEwan Crawford {eFormatFloat, eFormatVectorOfFloat32, sizeof(float)}, // RS_TYPE_FLOAT_32 370a0f08674SEwan Crawford {eFormatFloat, eFormatVectorOfFloat64, sizeof(double)}, // RS_TYPE_FLOAT_64 371a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt8, sizeof(int8_t)}, // RS_TYPE_SIGNED_8 372a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt16, sizeof(int16_t)}, // RS_TYPE_SIGNED_16 373a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt32, sizeof(int32_t)}, // RS_TYPE_SIGNED_32 374a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt64, sizeof(int64_t)}, // RS_TYPE_SIGNED_64 375a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt8, sizeof(uint8_t)}, // RS_TYPE_UNSIGNED_8 376a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt16, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_16 377a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt32, sizeof(uint32_t)}, // RS_TYPE_UNSIGNED_32 378a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt64, sizeof(uint64_t)}, // RS_TYPE_UNSIGNED_64 3792e920715SEwan Crawford {eFormatBoolean, eFormatBoolean, 1}, // RS_TYPE_BOOL 3802e920715SEwan Crawford {eFormatHex, eFormatHex, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_5_6_5 3812e920715SEwan Crawford {eFormatHex, eFormatHex, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_5_5_5_1 3822e920715SEwan Crawford {eFormatHex, eFormatHex, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_4_4_4_4 3832e920715SEwan Crawford {eFormatVectorOfFloat32, eFormatVectorOfFloat32, sizeof(float) * 16}, // RS_TYPE_MATRIX_4X4 3842e920715SEwan Crawford {eFormatVectorOfFloat32, eFormatVectorOfFloat32, sizeof(float) * 9}, // RS_TYPE_MATRIX_3X3 3852e920715SEwan Crawford {eFormatVectorOfFloat32, eFormatVectorOfFloat32, sizeof(float) * 4} // RS_TYPE_MATRIX_2X2 386a0f08674SEwan Crawford }; 387a0f08674SEwan Crawford 3884f8817c2SEwan Crawford const std::string RenderScriptRuntime::s_runtimeExpandSuffix(".expand"); 389a9759599SPavel Labath const std::array<const char *, 3> RenderScriptRuntime::s_runtimeCoordVars{{"rsIndex", "p->current.y", "p->current.z"}}; 3905ec532a9SColin Riley //------------------------------------------------------------------ 3915ec532a9SColin Riley // Static Functions 3925ec532a9SColin Riley //------------------------------------------------------------------ 3935ec532a9SColin Riley LanguageRuntime * 3945ec532a9SColin Riley RenderScriptRuntime::CreateInstance(Process *process, lldb::LanguageType language) 3955ec532a9SColin Riley { 3965ec532a9SColin Riley 3975ec532a9SColin Riley if (language == eLanguageTypeExtRenderScript) 3985ec532a9SColin Riley return new RenderScriptRuntime(process); 3995ec532a9SColin Riley else 400b3f7f69dSAidan Dodds return nullptr; 4015ec532a9SColin Riley } 4025ec532a9SColin Riley 40398156583SEwan Crawford // Callback with a module to search for matching symbols. 40498156583SEwan Crawford // We first check that the module contains RS kernels. 40598156583SEwan Crawford // Then look for a symbol which matches our kernel name. 40698156583SEwan Crawford // The breakpoint address is finally set using the address of this symbol. 40798156583SEwan Crawford Searcher::CallbackReturn 408b3f7f69dSAidan Dodds RSBreakpointResolver::SearchCallback(SearchFilter &filter, SymbolContext &context, Address *, bool) 40998156583SEwan Crawford { 41098156583SEwan Crawford ModuleSP module = context.module_sp; 41198156583SEwan Crawford 41298156583SEwan Crawford if (!module) 41398156583SEwan Crawford return Searcher::eCallbackReturnContinue; 41498156583SEwan Crawford 41598156583SEwan Crawford // Is this a module containing renderscript kernels? 41698156583SEwan Crawford if (nullptr == module->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData)) 41798156583SEwan Crawford return Searcher::eCallbackReturnContinue; 41898156583SEwan Crawford 41998156583SEwan Crawford // Attempt to set a breakpoint on the kernel name symbol within the module library. 42098156583SEwan Crawford // If it's not found, it's likely debug info is unavailable - try to set a 42198156583SEwan Crawford // breakpoint on <name>.expand. 42298156583SEwan Crawford 42398156583SEwan Crawford const Symbol *kernel_sym = module->FindFirstSymbolWithNameAndType(m_kernel_name, eSymbolTypeCode); 42498156583SEwan Crawford if (!kernel_sym) 42598156583SEwan Crawford { 42698156583SEwan Crawford std::string kernel_name_expanded(m_kernel_name.AsCString()); 42798156583SEwan Crawford kernel_name_expanded.append(".expand"); 42898156583SEwan Crawford kernel_sym = module->FindFirstSymbolWithNameAndType(ConstString(kernel_name_expanded.c_str()), eSymbolTypeCode); 42998156583SEwan Crawford } 43098156583SEwan Crawford 43198156583SEwan Crawford if (kernel_sym) 43298156583SEwan Crawford { 43398156583SEwan Crawford Address bp_addr = kernel_sym->GetAddress(); 43498156583SEwan Crawford if (filter.AddressPasses(bp_addr)) 43598156583SEwan Crawford m_breakpoint->AddLocation(bp_addr); 43698156583SEwan Crawford } 43798156583SEwan Crawford 43898156583SEwan Crawford return Searcher::eCallbackReturnContinue; 43998156583SEwan Crawford } 44098156583SEwan Crawford 4415ec532a9SColin Riley void 4425ec532a9SColin Riley RenderScriptRuntime::Initialize() 4435ec532a9SColin Riley { 444b3f7f69dSAidan Dodds PluginManager::RegisterPlugin(GetPluginNameStatic(), "RenderScript language support", CreateInstance, 445b3f7f69dSAidan Dodds GetCommandObject); 4465ec532a9SColin Riley } 4475ec532a9SColin Riley 4485ec532a9SColin Riley void 4495ec532a9SColin Riley RenderScriptRuntime::Terminate() 4505ec532a9SColin Riley { 4515ec532a9SColin Riley PluginManager::UnregisterPlugin(CreateInstance); 4525ec532a9SColin Riley } 4535ec532a9SColin Riley 4545ec532a9SColin Riley lldb_private::ConstString 4555ec532a9SColin Riley RenderScriptRuntime::GetPluginNameStatic() 4565ec532a9SColin Riley { 4575ec532a9SColin Riley static ConstString g_name("renderscript"); 4585ec532a9SColin Riley return g_name; 4595ec532a9SColin Riley } 4605ec532a9SColin Riley 461ef20b08fSColin Riley RenderScriptRuntime::ModuleKind 462ef20b08fSColin Riley RenderScriptRuntime::GetModuleKind(const lldb::ModuleSP &module_sp) 463ef20b08fSColin Riley { 464ef20b08fSColin Riley if (module_sp) 465ef20b08fSColin Riley { 466ef20b08fSColin Riley // Is this a module containing renderscript kernels? 467ef20b08fSColin Riley const Symbol *info_sym = module_sp->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData); 468ef20b08fSColin Riley if (info_sym) 469ef20b08fSColin Riley { 470ef20b08fSColin Riley return eModuleKindKernelObj; 471ef20b08fSColin Riley } 4724640cde1SColin Riley 4734640cde1SColin Riley // Is this the main RS runtime library 4744640cde1SColin Riley const ConstString rs_lib("libRS.so"); 4754640cde1SColin Riley if (module_sp->GetFileSpec().GetFilename() == rs_lib) 4764640cde1SColin Riley { 4774640cde1SColin Riley return eModuleKindLibRS; 4784640cde1SColin Riley } 4794640cde1SColin Riley 4804640cde1SColin Riley const ConstString rs_driverlib("libRSDriver.so"); 4814640cde1SColin Riley if (module_sp->GetFileSpec().GetFilename() == rs_driverlib) 4824640cde1SColin Riley { 4834640cde1SColin Riley return eModuleKindDriver; 4844640cde1SColin Riley } 4854640cde1SColin Riley 48615f2bd95SEwan Crawford const ConstString rs_cpureflib("libRSCpuRef.so"); 4874640cde1SColin Riley if (module_sp->GetFileSpec().GetFilename() == rs_cpureflib) 4884640cde1SColin Riley { 4894640cde1SColin Riley return eModuleKindImpl; 4904640cde1SColin Riley } 491ef20b08fSColin Riley } 492ef20b08fSColin Riley return eModuleKindIgnored; 493ef20b08fSColin Riley } 494ef20b08fSColin Riley 495ef20b08fSColin Riley bool 496ef20b08fSColin Riley RenderScriptRuntime::IsRenderScriptModule(const lldb::ModuleSP &module_sp) 497ef20b08fSColin Riley { 498ef20b08fSColin Riley return GetModuleKind(module_sp) != eModuleKindIgnored; 499ef20b08fSColin Riley } 500ef20b08fSColin Riley 501ef20b08fSColin Riley void 502ef20b08fSColin Riley RenderScriptRuntime::ModulesDidLoad(const ModuleList &module_list) 503ef20b08fSColin Riley { 504ef20b08fSColin Riley Mutex::Locker locker(module_list.GetMutex()); 505ef20b08fSColin Riley 506ef20b08fSColin Riley size_t num_modules = module_list.GetSize(); 507ef20b08fSColin Riley for (size_t i = 0; i < num_modules; i++) 508ef20b08fSColin Riley { 509ef20b08fSColin Riley auto mod = module_list.GetModuleAtIndex(i); 510ef20b08fSColin Riley if (IsRenderScriptModule(mod)) 511ef20b08fSColin Riley { 512ef20b08fSColin Riley LoadModule(mod); 513ef20b08fSColin Riley } 514ef20b08fSColin Riley } 515ef20b08fSColin Riley } 516ef20b08fSColin Riley 5175ec532a9SColin Riley //------------------------------------------------------------------ 5185ec532a9SColin Riley // PluginInterface protocol 5195ec532a9SColin Riley //------------------------------------------------------------------ 5205ec532a9SColin Riley lldb_private::ConstString 5215ec532a9SColin Riley RenderScriptRuntime::GetPluginName() 5225ec532a9SColin Riley { 5235ec532a9SColin Riley return GetPluginNameStatic(); 5245ec532a9SColin Riley } 5255ec532a9SColin Riley 5265ec532a9SColin Riley uint32_t 5275ec532a9SColin Riley RenderScriptRuntime::GetPluginVersion() 5285ec532a9SColin Riley { 5295ec532a9SColin Riley return 1; 5305ec532a9SColin Riley } 5315ec532a9SColin Riley 5325ec532a9SColin Riley bool 5335ec532a9SColin Riley RenderScriptRuntime::IsVTableName(const char *name) 5345ec532a9SColin Riley { 5355ec532a9SColin Riley return false; 5365ec532a9SColin Riley } 5375ec532a9SColin Riley 5385ec532a9SColin Riley bool 5395ec532a9SColin Riley RenderScriptRuntime::GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, 5400b6003f3SEnrico Granata TypeAndOrName &class_type_or_name, Address &address, 5410b6003f3SEnrico Granata Value::ValueType &value_type) 5425ec532a9SColin Riley { 5435ec532a9SColin Riley return false; 5445ec532a9SColin Riley } 5455ec532a9SColin Riley 546c74275bcSEnrico Granata TypeAndOrName 547b3f7f69dSAidan Dodds RenderScriptRuntime::FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value) 548c74275bcSEnrico Granata { 549c74275bcSEnrico Granata return type_and_or_name; 550c74275bcSEnrico Granata } 551c74275bcSEnrico Granata 5525ec532a9SColin Riley bool 5535ec532a9SColin Riley RenderScriptRuntime::CouldHaveDynamicValue(ValueObject &in_value) 5545ec532a9SColin Riley { 5555ec532a9SColin Riley return false; 5565ec532a9SColin Riley } 5575ec532a9SColin Riley 5585ec532a9SColin Riley lldb::BreakpointResolverSP 5595ec532a9SColin Riley RenderScriptRuntime::CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) 5605ec532a9SColin Riley { 5615ec532a9SColin Riley BreakpointResolverSP resolver_sp; 5625ec532a9SColin Riley return resolver_sp; 5635ec532a9SColin Riley } 5645ec532a9SColin Riley 565b3f7f69dSAidan Dodds const RenderScriptRuntime::HookDefn RenderScriptRuntime::s_runtimeHookDefns[] = { 5664640cde1SColin Riley // rsdScript 56782780287SAidan Dodds { 568b3f7f69dSAidan Dodds "rsdScriptInit", 569b3f7f69dSAidan Dodds "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhjj", 570b3f7f69dSAidan Dodds "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhmj", 571b3f7f69dSAidan Dodds 0, 572b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 573b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureScriptInit 57482780287SAidan Dodds }, 57582780287SAidan Dodds { 576b3f7f69dSAidan Dodds "rsdScriptInvokeForEachMulti", 577b3f7f69dSAidan Dodds "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEjPS6_PKvjPK12RsScriptCall", 578b3f7f69dSAidan Dodds "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEmPS6_PKvmPK12RsScriptCall", 579b3f7f69dSAidan Dodds 0, 580b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 581b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureScriptInvokeForEachMulti 58282780287SAidan Dodds }, 58382780287SAidan Dodds { 584b3f7f69dSAidan Dodds "rsdScriptSetGlobalVar", 585b3f7f69dSAidan Dodds "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvj", 586b3f7f69dSAidan Dodds "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvm", 587b3f7f69dSAidan Dodds 0, 588b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 589b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureSetGlobalVar 59082780287SAidan Dodds }, 5914640cde1SColin Riley 5924640cde1SColin Riley // rsdAllocation 59382780287SAidan Dodds { 594b3f7f69dSAidan Dodds "rsdAllocationInit", 595b3f7f69dSAidan Dodds "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", 596b3f7f69dSAidan Dodds "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", 597b3f7f69dSAidan Dodds 0, 598b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 599b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureAllocationInit 60082780287SAidan Dodds }, 60182780287SAidan Dodds { 602b3f7f69dSAidan Dodds "rsdAllocationRead2D", 603b3f7f69dSAidan Dodds "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvjj", 604b3f7f69dSAidan Dodds "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvmm", 605b3f7f69dSAidan Dodds 0, 606b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 607b3f7f69dSAidan Dodds nullptr 60882780287SAidan Dodds }, 609e69df382SEwan Crawford { 610b3f7f69dSAidan Dodds "rsdAllocationDestroy", 611b3f7f69dSAidan Dodds "_Z20rsdAllocationDestroyPKN7android12renderscript7ContextEPNS0_10AllocationE", 612b3f7f69dSAidan Dodds "_Z20rsdAllocationDestroyPKN7android12renderscript7ContextEPNS0_10AllocationE", 613b3f7f69dSAidan Dodds 0, 614b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 615b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureAllocationDestroy 616e69df382SEwan Crawford }, 6174640cde1SColin Riley }; 6184640cde1SColin Riley 619222b937cSEugene Zelenko const size_t RenderScriptRuntime::s_runtimeHookCount = sizeof(s_runtimeHookDefns) / sizeof(s_runtimeHookDefns[0]); 6204640cde1SColin Riley 6214640cde1SColin Riley bool 622b3f7f69dSAidan Dodds RenderScriptRuntime::HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id, 623b3f7f69dSAidan Dodds lldb::user_id_t break_loc_id) 6244640cde1SColin Riley { 6254640cde1SColin Riley RuntimeHook *hook_info = (RuntimeHook *)baton; 6264640cde1SColin Riley ExecutionContext context(ctx->exe_ctx_ref); 6274640cde1SColin Riley 628b3f7f69dSAidan Dodds RenderScriptRuntime *lang_rt = 629b3f7f69dSAidan Dodds (RenderScriptRuntime *)context.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 6304640cde1SColin Riley 6314640cde1SColin Riley lang_rt->HookCallback(hook_info, context); 6324640cde1SColin Riley 6334640cde1SColin Riley return false; 6344640cde1SColin Riley } 6354640cde1SColin Riley 6364640cde1SColin Riley void 6374640cde1SColin Riley RenderScriptRuntime::HookCallback(RuntimeHook *hook_info, ExecutionContext &context) 6384640cde1SColin Riley { 6394640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 6404640cde1SColin Riley 6414640cde1SColin Riley if (log) 642b3f7f69dSAidan Dodds log->Printf("%s - '%s'", __FUNCTION__, hook_info->defn->name); 6434640cde1SColin Riley 6444640cde1SColin Riley if (hook_info->defn->grabber) 6454640cde1SColin Riley { 6464640cde1SColin Riley (this->*(hook_info->defn->grabber))(hook_info, context); 6474640cde1SColin Riley } 6484640cde1SColin Riley } 6494640cde1SColin Riley 6504640cde1SColin Riley bool 65182780287SAidan Dodds RenderScriptRuntime::GetArgSimple(ExecutionContext &context, uint32_t arg, uint64_t *data) 6524640cde1SColin Riley { 653cdfb1485SEwan Crawford // Get a positional integer argument. 654cdfb1485SEwan Crawford // Given an ExecutionContext, ``context`` which should be a RenderScript 655cdfb1485SEwan Crawford // frame, get the value of the positional argument ``arg`` and save its value 656cdfb1485SEwan Crawford // to the address pointed to by ``data``. 657cdfb1485SEwan Crawford // returns true on success, false otherwise. 658cdfb1485SEwan Crawford // If unsuccessful, the value pointed to by ``data`` is undefined. Otherwise, 659cdfb1485SEwan Crawford // ``data`` will be set to the value of the the given ``arg``. 660cdfb1485SEwan Crawford // NOTE: only natural width integer arguments for the machine are supported. 661cdfb1485SEwan Crawford // Behaviour with non primitive arguments is undefined. 662cdfb1485SEwan Crawford 6634640cde1SColin Riley if (!data) 6644640cde1SColin Riley return false; 6654640cde1SColin Riley 66682780287SAidan Dodds Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 6674640cde1SColin Riley Error error; 6684640cde1SColin Riley RegisterContext *reg_ctx = context.GetRegisterContext(); 6694640cde1SColin Riley Process *process = context.GetProcessPtr(); 67082780287SAidan Dodds bool success = false; // return value 6714640cde1SColin Riley 67282780287SAidan Dodds if (!context.GetTargetPtr()) 67382780287SAidan Dodds { 67482780287SAidan Dodds if (log) 675b3f7f69dSAidan Dodds log->Printf("%s - invalid target", __FUNCTION__); 67682780287SAidan Dodds 67782780287SAidan Dodds return false; 67882780287SAidan Dodds } 67982780287SAidan Dodds 68082780287SAidan Dodds switch (context.GetTargetPtr()->GetArchitecture().GetMachine()) 68182780287SAidan Dodds { 68282780287SAidan Dodds case llvm::Triple::ArchType::x86: 6834640cde1SColin Riley { 6844640cde1SColin Riley uint64_t sp = reg_ctx->GetSP(); 6854640cde1SColin Riley uint32_t offset = (1 + arg) * sizeof(uint32_t); 68682780287SAidan Dodds uint32_t result = 0; 68782780287SAidan Dodds process->ReadMemory(sp + offset, &result, sizeof(uint32_t), error); 6884640cde1SColin Riley if (error.Fail()) 6894640cde1SColin Riley { 6904640cde1SColin Riley if (log) 691b3f7f69dSAidan Dodds log->Printf("%s - error reading X86 stack: '%s'.", __FUNCTION__, error.AsCString()); 6924640cde1SColin Riley } 69382780287SAidan Dodds else 6944640cde1SColin Riley { 69582780287SAidan Dodds *data = result; 69682780287SAidan Dodds success = true; 69782780287SAidan Dodds } 69882780287SAidan Dodds break; 69982780287SAidan Dodds } 700cdfb1485SEwan Crawford case llvm::Triple::ArchType::x86_64: 701cdfb1485SEwan Crawford { 702cdfb1485SEwan Crawford // amd64 has 6 integer registers, and 8 XMM registers for parameter passing. 703cdfb1485SEwan Crawford // Surplus args are spilled onto the stack. 704cdfb1485SEwan Crawford // rdi, rsi, rdx, rcx, r8, r9, (zmm0 - 7 for vectors) 705cdfb1485SEwan Crawford // ref: AMD64 ABI Draft 0.99.6 – October 7, 2013 – 10:35; Figure 3.4. Retrieved from 706cdfb1485SEwan Crawford // http://www.x86-64.org/documentation/abi.pdf 707cdfb1485SEwan Crawford if (arg > 5) 708cdfb1485SEwan Crawford { 709cdfb1485SEwan Crawford if (log) 710b3f7f69dSAidan Dodds log->Warning("%s - X86_64 - reading arguments passed on stack not supported yet.", 711b3f7f69dSAidan Dodds __FUNCTION__); 712cdfb1485SEwan Crawford break; 713cdfb1485SEwan Crawford } 714cdfb1485SEwan Crawford const char *regnames[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"}; 715cdfb1485SEwan Crawford assert((sizeof(regnames) / sizeof(const char *)) > arg); 716cdfb1485SEwan Crawford const RegisterInfo *rArg = reg_ctx->GetRegisterInfoByName(regnames[arg]); 717cdfb1485SEwan Crawford RegisterValue rVal; 718cdfb1485SEwan Crawford success = reg_ctx->ReadRegister(rArg, rVal); 719cdfb1485SEwan Crawford if (success) 720cdfb1485SEwan Crawford { 721cdfb1485SEwan Crawford *data = rVal.GetAsUInt64(0u, &success); 722cdfb1485SEwan Crawford } 723cdfb1485SEwan Crawford else 724cdfb1485SEwan Crawford { 725cdfb1485SEwan Crawford if (log) 726b3f7f69dSAidan Dodds log->Printf("%s - error reading x86_64 register: %" PRId32 ".", __FUNCTION__, arg); 727cdfb1485SEwan Crawford } 728cdfb1485SEwan Crawford break; 729cdfb1485SEwan Crawford } 73082780287SAidan Dodds case llvm::Triple::ArchType::arm: 73182780287SAidan Dodds { 73282780287SAidan Dodds // arm 32 bit 73335e7b1adSAidan Dodds // first 4 arguments are passed via registers 7344640cde1SColin Riley if (arg < 4) 7354640cde1SColin Riley { 7364640cde1SColin Riley const RegisterInfo *rArg = reg_ctx->GetRegisterInfoAtIndex(arg); 7374640cde1SColin Riley RegisterValue rVal; 73802f1c5d1SEwan Crawford success = reg_ctx->ReadRegister(rArg, rVal); 73902f1c5d1SEwan Crawford if (success) 74002f1c5d1SEwan Crawford { 741cdfb1485SEwan Crawford (*data) = rVal.GetAsUInt32(0u, &success); 74202f1c5d1SEwan Crawford } 74302f1c5d1SEwan Crawford else 74402f1c5d1SEwan Crawford { 74502f1c5d1SEwan Crawford if (log) 746b3f7f69dSAidan Dodds log->Printf("%s - error reading ARM register: %" PRId32 ".", __FUNCTION__, arg); 74702f1c5d1SEwan Crawford } 7484640cde1SColin Riley } 7494640cde1SColin Riley else 7504640cde1SColin Riley { 7514640cde1SColin Riley uint64_t sp = reg_ctx->GetSP(); 7524640cde1SColin Riley uint32_t offset = (arg - 4) * sizeof(uint32_t); 75335e7b1adSAidan Dodds uint32_t value = 0; 75435e7b1adSAidan Dodds size_t bytes_read = process->ReadMemory(sp + offset, &value, sizeof(value), error); 75535e7b1adSAidan Dodds if (error.Fail() || bytes_read != sizeof(value)) 7564640cde1SColin Riley { 7574640cde1SColin Riley if (log) 758b3f7f69dSAidan Dodds log->Printf("%s - error reading ARM stack: %s.", __FUNCTION__, error.AsCString()); 75982780287SAidan Dodds } 76082780287SAidan Dodds else 76182780287SAidan Dodds { 76235e7b1adSAidan Dodds *data = value; 76382780287SAidan Dodds success = true; 7644640cde1SColin Riley } 7654640cde1SColin Riley } 76682780287SAidan Dodds break; 7674640cde1SColin Riley } 76882780287SAidan Dodds case llvm::Triple::ArchType::aarch64: 76982780287SAidan Dodds { 77082780287SAidan Dodds // arm 64 bit 77182780287SAidan Dodds // first 8 arguments are in the registers 77282780287SAidan Dodds if (arg < 8) 77382780287SAidan Dodds { 77482780287SAidan Dodds const RegisterInfo *rArg = reg_ctx->GetRegisterInfoAtIndex(arg); 77582780287SAidan Dodds RegisterValue rVal; 77682780287SAidan Dodds success = reg_ctx->ReadRegister(rArg, rVal); 77782780287SAidan Dodds if (success) 77882780287SAidan Dodds { 779cdfb1485SEwan Crawford *data = rVal.GetAsUInt64(0u, &success); 78082780287SAidan Dodds } 78182780287SAidan Dodds else 78282780287SAidan Dodds { 78382780287SAidan Dodds if (log) 784b3f7f69dSAidan Dodds log->Printf("%s - AARCH64 - error while reading the argument #%" PRId32 ".", 785b3f7f69dSAidan Dodds __FUNCTION__, arg); 78682780287SAidan Dodds } 78782780287SAidan Dodds } 78882780287SAidan Dodds else 78982780287SAidan Dodds { 79082780287SAidan Dodds // @TODO: need to find the argument in the stack 79182780287SAidan Dodds if (log) 792b3f7f69dSAidan Dodds log->Printf("%s - AARCH64 - reading arguments passed on stack not supported yet.", 793b3f7f69dSAidan Dodds __FUNCTION__); 79482780287SAidan Dodds } 79582780287SAidan Dodds break; 79682780287SAidan Dodds } 79774b396d9SAidan Dodds case llvm::Triple::ArchType::mipsel: 79874b396d9SAidan Dodds { 79974b396d9SAidan Dodds // read from the registers 80035e7b1adSAidan Dodds // first 4 arguments are passed in registers 801b3f7f69dSAidan Dodds if (arg < 4) 802b3f7f69dSAidan Dodds { 80374b396d9SAidan Dodds const RegisterInfo *rArg = reg_ctx->GetRegisterInfoAtIndex(arg + 4); 80474b396d9SAidan Dodds RegisterValue rVal; 80574b396d9SAidan Dodds success = reg_ctx->ReadRegister(rArg, rVal); 80674b396d9SAidan Dodds if (success) 80774b396d9SAidan Dodds { 808cdfb1485SEwan Crawford *data = rVal.GetAsUInt64(0u, &success); 80974b396d9SAidan Dodds } 81074b396d9SAidan Dodds else 81174b396d9SAidan Dodds { 81274b396d9SAidan Dodds if (log) 813b3f7f69dSAidan Dodds log->Printf("%s - Mips - error while reading the argument #%" PRId32 "", 814b3f7f69dSAidan Dodds __FUNCTION__, arg); 81574b396d9SAidan Dodds } 81674b396d9SAidan Dodds } 81735e7b1adSAidan Dodds // arguments > 4 are read from the stack 81874b396d9SAidan Dodds else 81974b396d9SAidan Dodds { 82074b396d9SAidan Dodds uint64_t sp = reg_ctx->GetSP(); 82174b396d9SAidan Dodds uint32_t offset = arg * sizeof(uint32_t); 82235e7b1adSAidan Dodds uint32_t value = 0; 82335e7b1adSAidan Dodds size_t bytes_read = process->ReadMemory(sp + offset, &value, sizeof(value), error); 82435e7b1adSAidan Dodds if (error.Fail() || bytes_read != sizeof(value)) 82574b396d9SAidan Dodds { 82674b396d9SAidan Dodds if (log) 827b3f7f69dSAidan Dodds log->Printf("%s - error reading Mips stack: %s.", 828b3f7f69dSAidan Dodds __FUNCTION__, error.AsCString()); 82974b396d9SAidan Dodds } 83074b396d9SAidan Dodds else 83174b396d9SAidan Dodds { 83235e7b1adSAidan Dodds *data = value; 83374b396d9SAidan Dodds success = true; 83474b396d9SAidan Dodds } 83574b396d9SAidan Dodds } 83674b396d9SAidan Dodds break; 83774b396d9SAidan Dodds } 83802f1c5d1SEwan Crawford case llvm::Triple::ArchType::mips64el: 83902f1c5d1SEwan Crawford { 84002f1c5d1SEwan Crawford // read from the registers 84102f1c5d1SEwan Crawford if (arg < 8) 84202f1c5d1SEwan Crawford { 84302f1c5d1SEwan Crawford const RegisterInfo *rArg = reg_ctx->GetRegisterInfoAtIndex(arg + 4); 84402f1c5d1SEwan Crawford RegisterValue rVal; 84502f1c5d1SEwan Crawford success = reg_ctx->ReadRegister(rArg, rVal); 84602f1c5d1SEwan Crawford if (success) 84702f1c5d1SEwan Crawford { 848cdfb1485SEwan Crawford (*data) = rVal.GetAsUInt64(0u, &success); 84902f1c5d1SEwan Crawford } 85002f1c5d1SEwan Crawford else 85102f1c5d1SEwan Crawford { 85202f1c5d1SEwan Crawford if (log) 853b3f7f69dSAidan Dodds log->Printf("%s - Mips64 - error reading the argument #%" PRId32 "", 854b3f7f69dSAidan Dodds __FUNCTION__, arg); 85502f1c5d1SEwan Crawford } 85602f1c5d1SEwan Crawford } 85735e7b1adSAidan Dodds // arguments > 8 are read from the stack 85802f1c5d1SEwan Crawford else 85902f1c5d1SEwan Crawford { 86002f1c5d1SEwan Crawford uint64_t sp = reg_ctx->GetSP(); 86102f1c5d1SEwan Crawford uint32_t offset = (arg - 8) * sizeof(uint64_t); 86235e7b1adSAidan Dodds uint64_t value = 0; 86335e7b1adSAidan Dodds size_t bytes_read = process->ReadMemory(sp + offset, &value, sizeof(value), error); 86435e7b1adSAidan Dodds if (error.Fail() || bytes_read != sizeof(value)) 86502f1c5d1SEwan Crawford { 86602f1c5d1SEwan Crawford if (log) 867b3f7f69dSAidan Dodds log->Printf("%s - Mips64 - error reading Mips64 stack: %s.", 868b3f7f69dSAidan Dodds __FUNCTION__, error.AsCString()); 86902f1c5d1SEwan Crawford } 87002f1c5d1SEwan Crawford else 87102f1c5d1SEwan Crawford { 87235e7b1adSAidan Dodds *data = value; 87302f1c5d1SEwan Crawford success = true; 87402f1c5d1SEwan Crawford } 87502f1c5d1SEwan Crawford } 87602f1c5d1SEwan Crawford break; 87702f1c5d1SEwan Crawford } 87882780287SAidan Dodds default: 87982780287SAidan Dodds { 88082780287SAidan Dodds // invalid architecture 88182780287SAidan Dodds if (log) 882b3f7f69dSAidan Dodds log->Printf("%s - architecture not supported.", __FUNCTION__); 88382780287SAidan Dodds } 88482780287SAidan Dodds } 88582780287SAidan Dodds 886cdfb1485SEwan Crawford if (!success) 887cdfb1485SEwan Crawford { 888cdfb1485SEwan Crawford if (log) 889b3f7f69dSAidan Dodds log->Printf("%s - failed to get argument at index %" PRIu32 ".", __FUNCTION__, arg); 890cdfb1485SEwan Crawford } 89182780287SAidan Dodds return success; 8924640cde1SColin Riley } 8934640cde1SColin Riley 8944640cde1SColin Riley void 895e09c44b6SAidan Dodds RenderScriptRuntime::CaptureScriptInvokeForEachMulti(RuntimeHook* hook_info, 896e09c44b6SAidan Dodds ExecutionContext& context) 897e09c44b6SAidan Dodds { 898e09c44b6SAidan Dodds Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 899e09c44b6SAidan Dodds 900e09c44b6SAidan Dodds struct args_t 901e09c44b6SAidan Dodds { 902e09c44b6SAidan Dodds uint64_t context; // const Context *rsc 903e09c44b6SAidan Dodds uint64_t script; // Script *s 904e09c44b6SAidan Dodds uint64_t slot; // uint32_t slot 905e09c44b6SAidan Dodds uint64_t aIns; // const Allocation **aIns 906e09c44b6SAidan Dodds uint64_t inLen; // size_t inLen 907e09c44b6SAidan Dodds uint64_t aOut; // Allocation *aout 908e09c44b6SAidan Dodds uint64_t usr; // const void *usr 909e09c44b6SAidan Dodds uint64_t usrLen; // size_t usrLen 910e09c44b6SAidan Dodds uint64_t sc; // const RsScriptCall *sc 911e09c44b6SAidan Dodds } 912e09c44b6SAidan Dodds args; 913e09c44b6SAidan Dodds 914e09c44b6SAidan Dodds bool success = 915e09c44b6SAidan Dodds GetArgSimple(context, 0, &args.context) && 916e09c44b6SAidan Dodds GetArgSimple(context, 1, &args.script) && 917e09c44b6SAidan Dodds GetArgSimple(context, 2, &args.slot) && 918e09c44b6SAidan Dodds GetArgSimple(context, 3, &args.aIns) && 919e09c44b6SAidan Dodds GetArgSimple(context, 4, &args.inLen) && 920e09c44b6SAidan Dodds GetArgSimple(context, 5, &args.aOut) && 921e09c44b6SAidan Dodds GetArgSimple(context, 6, &args.usr) && 922e09c44b6SAidan Dodds GetArgSimple(context, 7, &args.usrLen) && 923e09c44b6SAidan Dodds GetArgSimple(context, 8, &args.sc); 924e09c44b6SAidan Dodds 925e09c44b6SAidan Dodds if (!success) 926e09c44b6SAidan Dodds { 927e09c44b6SAidan Dodds if (log) 928b3f7f69dSAidan Dodds log->Printf("%s - Error while reading the function parameters", __FUNCTION__); 929e09c44b6SAidan Dodds return; 930e09c44b6SAidan Dodds } 931e09c44b6SAidan Dodds 932e09c44b6SAidan Dodds const uint32_t target_ptr_size = m_process->GetAddressByteSize(); 933e09c44b6SAidan Dodds Error error; 934e09c44b6SAidan Dodds std::vector<uint64_t> allocs; 935e09c44b6SAidan Dodds 936e09c44b6SAidan Dodds // traverse allocation list 937e09c44b6SAidan Dodds for (uint64_t i = 0; i < args.inLen; ++i) 938e09c44b6SAidan Dodds { 939e09c44b6SAidan Dodds // calculate offest to allocation pointer 940e09c44b6SAidan Dodds const lldb::addr_t addr = args.aIns + i * target_ptr_size; 941e09c44b6SAidan Dodds 942e09c44b6SAidan Dodds // Note: due to little endian layout, reading 32bits or 64bits into res64 will 943e09c44b6SAidan Dodds // give the correct results. 944e09c44b6SAidan Dodds 945e09c44b6SAidan Dodds uint64_t res64 = 0; 946e09c44b6SAidan Dodds size_t read = m_process->ReadMemory(addr, &res64, target_ptr_size, error); 947e09c44b6SAidan Dodds if (read != target_ptr_size || !error.Success()) 948e09c44b6SAidan Dodds { 949e09c44b6SAidan Dodds if (log) 950b3f7f69dSAidan Dodds log->Printf("%s - Error while reading allocation list argument %" PRId64, __FUNCTION__, i); 951e09c44b6SAidan Dodds } 952e09c44b6SAidan Dodds else 953e09c44b6SAidan Dodds { 954e09c44b6SAidan Dodds allocs.push_back(res64); 955e09c44b6SAidan Dodds } 956e09c44b6SAidan Dodds } 957e09c44b6SAidan Dodds 958e09c44b6SAidan Dodds // if there is an output allocation track it 959e09c44b6SAidan Dodds if (args.aOut) 960e09c44b6SAidan Dodds { 961e09c44b6SAidan Dodds allocs.push_back(args.aOut); 962e09c44b6SAidan Dodds } 963e09c44b6SAidan Dodds 964e09c44b6SAidan Dodds // for all allocations we have found 965e09c44b6SAidan Dodds for (const uint64_t alloc_addr : allocs) 966e09c44b6SAidan Dodds { 967e09c44b6SAidan Dodds AllocationDetails* alloc = LookUpAllocation(alloc_addr, true); 968e09c44b6SAidan Dodds if (alloc) 969e09c44b6SAidan Dodds { 970e09c44b6SAidan Dodds // save the allocation address 971e09c44b6SAidan Dodds if (alloc->address.isValid()) 972e09c44b6SAidan Dodds { 973e09c44b6SAidan Dodds // check the allocation address we already have matches 974e09c44b6SAidan Dodds assert(*alloc->address.get() == alloc_addr); 975e09c44b6SAidan Dodds } 976e09c44b6SAidan Dodds else 977e09c44b6SAidan Dodds { 978e09c44b6SAidan Dodds alloc->address = alloc_addr; 979e09c44b6SAidan Dodds } 980e09c44b6SAidan Dodds 981e09c44b6SAidan Dodds // save the context 982e09c44b6SAidan Dodds if (log) 983e09c44b6SAidan Dodds { 984e09c44b6SAidan Dodds if (alloc->context.isValid() && *alloc->context.get() != args.context) 985b3f7f69dSAidan Dodds log->Printf("%s - Allocation used by multiple contexts", __FUNCTION__); 986e09c44b6SAidan Dodds } 987e09c44b6SAidan Dodds alloc->context = args.context; 988e09c44b6SAidan Dodds } 989e09c44b6SAidan Dodds } 990e09c44b6SAidan Dodds 991e09c44b6SAidan Dodds // make sure we track this script object 992e09c44b6SAidan Dodds if (lldb_private::RenderScriptRuntime::ScriptDetails * script = LookUpScript(args.script, true)) 993e09c44b6SAidan Dodds { 994e09c44b6SAidan Dodds if (log) 995e09c44b6SAidan Dodds { 996e09c44b6SAidan Dodds if (script->context.isValid() && *script->context.get() != args.context) 997b3f7f69dSAidan Dodds log->Printf("%s - Script used by multiple contexts", __FUNCTION__); 998e09c44b6SAidan Dodds } 999e09c44b6SAidan Dodds script->context = args.context; 1000e09c44b6SAidan Dodds } 1001e09c44b6SAidan Dodds } 1002e09c44b6SAidan Dodds 1003e09c44b6SAidan Dodds void 1004b3f7f69dSAidan Dodds RenderScriptRuntime::CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context) 10054640cde1SColin Riley { 10064640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 10074640cde1SColin Riley 10084640cde1SColin Riley // Context, Script, int, data, length 10094640cde1SColin Riley 101082780287SAidan Dodds uint64_t rs_context_u64 = 0U; 101182780287SAidan Dodds uint64_t rs_script_u64 = 0U; 101282780287SAidan Dodds uint64_t rs_id_u64 = 0U; 101382780287SAidan Dodds uint64_t rs_data_u64 = 0U; 101482780287SAidan Dodds uint64_t rs_length_u64 = 0U; 10154640cde1SColin Riley 1016b3f7f69dSAidan Dodds bool success = GetArgSimple(context, 0, &rs_context_u64) && 101782780287SAidan Dodds GetArgSimple(context, 1, &rs_script_u64) && 101882780287SAidan Dodds GetArgSimple(context, 2, &rs_id_u64) && 101982780287SAidan Dodds GetArgSimple(context, 3, &rs_data_u64) && 102082780287SAidan Dodds GetArgSimple(context, 4, &rs_length_u64); 10214640cde1SColin Riley 102282780287SAidan Dodds if (!success) 102382780287SAidan Dodds { 102482780287SAidan Dodds if (log) 1025b3f7f69dSAidan Dodds log->Printf("%s - error reading the function parameters.", __FUNCTION__); 102682780287SAidan Dodds return; 102782780287SAidan Dodds } 10284640cde1SColin Riley 10294640cde1SColin Riley if (log) 10304640cde1SColin Riley { 1031b3f7f69dSAidan Dodds log->Printf("%s - 0x%" PRIx64 ",0x%" PRIx64 " slot %" PRIu64 " = 0x%" PRIx64 ":%" PRIu64 "bytes.", 1032b3f7f69dSAidan Dodds __FUNCTION__, rs_context_u64, rs_script_u64, rs_id_u64, rs_data_u64, rs_length_u64); 10334640cde1SColin Riley 103482780287SAidan Dodds addr_t script_addr = (addr_t)rs_script_u64; 10354640cde1SColin Riley if (m_scriptMappings.find(script_addr) != m_scriptMappings.end()) 10364640cde1SColin Riley { 10374640cde1SColin Riley auto rsm = m_scriptMappings[script_addr]; 103882780287SAidan Dodds if (rs_id_u64 < rsm->m_globals.size()) 10394640cde1SColin Riley { 104082780287SAidan Dodds auto rsg = rsm->m_globals[rs_id_u64]; 1041b3f7f69dSAidan Dodds log->Printf("%s - setting of '%s' within '%s' inferred.", __FUNCTION__, 1042b3f7f69dSAidan Dodds rsg.m_name.AsCString(), rsm->m_module->GetFileSpec().GetFilename().AsCString()); 10434640cde1SColin Riley } 10444640cde1SColin Riley } 10454640cde1SColin Riley } 10464640cde1SColin Riley } 10474640cde1SColin Riley 10484640cde1SColin Riley void 1049b3f7f69dSAidan Dodds RenderScriptRuntime::CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context) 10504640cde1SColin Riley { 10514640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 10524640cde1SColin Riley 10534640cde1SColin Riley // Context, Alloc, bool 10544640cde1SColin Riley 105582780287SAidan Dodds uint64_t rs_context_u64 = 0U; 105682780287SAidan Dodds uint64_t rs_alloc_u64 = 0U; 105782780287SAidan Dodds uint64_t rs_forceZero_u64 = 0U; 10584640cde1SColin Riley 1059b3f7f69dSAidan Dodds bool success = GetArgSimple(context, 0, &rs_context_u64) && 106082780287SAidan Dodds GetArgSimple(context, 1, &rs_alloc_u64) && 106182780287SAidan Dodds GetArgSimple(context, 2, &rs_forceZero_u64); 106282780287SAidan Dodds if (!success) // error case 106382780287SAidan Dodds { 106482780287SAidan Dodds if (log) 1065b3f7f69dSAidan Dodds log->Printf("%s - error while reading the function parameters", __FUNCTION__); 106682780287SAidan Dodds return; // abort 106782780287SAidan Dodds } 10684640cde1SColin Riley 10694640cde1SColin Riley if (log) 1070b3f7f69dSAidan Dodds log->Printf("%s - 0x%" PRIx64 ",0x%" PRIx64 ",0x%" PRIx64 " .", __FUNCTION__, 107182780287SAidan Dodds rs_context_u64, rs_alloc_u64, rs_forceZero_u64); 107278f339d1SEwan Crawford 107378f339d1SEwan Crawford AllocationDetails *alloc = LookUpAllocation(rs_alloc_u64, true); 107478f339d1SEwan Crawford if (alloc) 107578f339d1SEwan Crawford alloc->context = rs_context_u64; 10764640cde1SColin Riley } 10774640cde1SColin Riley 10784640cde1SColin Riley void 1079e69df382SEwan Crawford RenderScriptRuntime::CaptureAllocationDestroy(RuntimeHook *hook_info, ExecutionContext &context) 1080e69df382SEwan Crawford { 1081e69df382SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1082e69df382SEwan Crawford 1083e69df382SEwan Crawford // Context, Alloc 1084e69df382SEwan Crawford uint64_t rs_context_u64 = 0U; 1085e69df382SEwan Crawford uint64_t rs_alloc_u64 = 0U; 1086e69df382SEwan Crawford 1087b3f7f69dSAidan Dodds bool success = GetArgSimple(context, 0, &rs_context_u64) && 1088b3f7f69dSAidan Dodds GetArgSimple(context, 1, &rs_alloc_u64); 1089b3f7f69dSAidan Dodds if (!success) 1090e69df382SEwan Crawford { 1091e69df382SEwan Crawford if (log) 1092b3f7f69dSAidan Dodds log->Printf("%s - error while reading the function parameters.", __FUNCTION__); 1093b3f7f69dSAidan Dodds return; 1094e69df382SEwan Crawford } 1095e69df382SEwan Crawford 1096e69df382SEwan Crawford if (log) 1097b3f7f69dSAidan Dodds log->Printf("%s - 0x%" PRIx64 ", 0x%" PRIx64 ".", __FUNCTION__, rs_context_u64, rs_alloc_u64); 1098e69df382SEwan Crawford 1099e69df382SEwan Crawford for (auto iter = m_allocations.begin(); iter != m_allocations.end(); ++iter) 1100e69df382SEwan Crawford { 1101e69df382SEwan Crawford auto &allocation_ap = *iter; // get the unique pointer 1102e69df382SEwan Crawford if (allocation_ap->address.isValid() && *allocation_ap->address.get() == rs_alloc_u64) 1103e69df382SEwan Crawford { 1104e69df382SEwan Crawford m_allocations.erase(iter); 1105e69df382SEwan Crawford if (log) 1106b3f7f69dSAidan Dodds log->Printf("%s - deleted allocation entry.", __FUNCTION__); 1107e69df382SEwan Crawford return; 1108e69df382SEwan Crawford } 1109e69df382SEwan Crawford } 1110e69df382SEwan Crawford 1111e69df382SEwan Crawford if (log) 1112b3f7f69dSAidan Dodds log->Printf("%s - couldn't find destroyed allocation.", __FUNCTION__); 1113e69df382SEwan Crawford } 1114e69df382SEwan Crawford 1115e69df382SEwan Crawford void 1116b3f7f69dSAidan Dodds RenderScriptRuntime::CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context) 11174640cde1SColin Riley { 11184640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 11194640cde1SColin Riley 11204640cde1SColin Riley // Context, Script, resname Str, cachedir Str 11214640cde1SColin Riley Error error; 11224640cde1SColin Riley Process *process = context.GetProcessPtr(); 11234640cde1SColin Riley 112482780287SAidan Dodds uint64_t rs_context_u64 = 0U; 112582780287SAidan Dodds uint64_t rs_script_u64 = 0U; 112682780287SAidan Dodds uint64_t rs_resnameptr_u64 = 0U; 112782780287SAidan Dodds uint64_t rs_cachedirptr_u64 = 0U; 11284640cde1SColin Riley 11294640cde1SColin Riley std::string resname; 11304640cde1SColin Riley std::string cachedir; 11314640cde1SColin Riley 113282780287SAidan Dodds // read the function parameters 1133b3f7f69dSAidan Dodds bool success = GetArgSimple(context, 0, &rs_context_u64) && 113482780287SAidan Dodds GetArgSimple(context, 1, &rs_script_u64) && 113582780287SAidan Dodds GetArgSimple(context, 2, &rs_resnameptr_u64) && 113682780287SAidan Dodds GetArgSimple(context, 3, &rs_cachedirptr_u64); 11374640cde1SColin Riley 113882780287SAidan Dodds if (!success) 113982780287SAidan Dodds { 114082780287SAidan Dodds if (log) 1141b3f7f69dSAidan Dodds log->Printf("%s - error while reading the function parameters.", __FUNCTION__); 114282780287SAidan Dodds return; 114382780287SAidan Dodds } 114482780287SAidan Dodds 114582780287SAidan Dodds process->ReadCStringFromMemory((lldb::addr_t)rs_resnameptr_u64, resname, error); 11464640cde1SColin Riley if (error.Fail()) 11474640cde1SColin Riley { 11484640cde1SColin Riley if (log) 1149b3f7f69dSAidan Dodds log->Printf("%s - error reading resname: %s.", __FUNCTION__, error.AsCString()); 11504640cde1SColin Riley } 11514640cde1SColin Riley 115282780287SAidan Dodds process->ReadCStringFromMemory((lldb::addr_t)rs_cachedirptr_u64, cachedir, error); 11534640cde1SColin Riley if (error.Fail()) 11544640cde1SColin Riley { 11554640cde1SColin Riley if (log) 1156b3f7f69dSAidan Dodds log->Printf("%s - error reading cachedir: %s.", __FUNCTION__, error.AsCString()); 11574640cde1SColin Riley } 11584640cde1SColin Riley 11594640cde1SColin Riley if (log) 1160b3f7f69dSAidan Dodds log->Printf("%s - 0x%" PRIx64 ",0x%" PRIx64 " => '%s' at '%s' .", __FUNCTION__, 116182780287SAidan Dodds rs_context_u64, rs_script_u64, resname.c_str(), cachedir.c_str()); 11624640cde1SColin Riley 11634640cde1SColin Riley if (resname.size() > 0) 11644640cde1SColin Riley { 11654640cde1SColin Riley StreamString strm; 11664640cde1SColin Riley strm.Printf("librs.%s.so", resname.c_str()); 11674640cde1SColin Riley 116878f339d1SEwan Crawford ScriptDetails *script = LookUpScript(rs_script_u64, true); 116978f339d1SEwan Crawford if (script) 117078f339d1SEwan Crawford { 117178f339d1SEwan Crawford script->type = ScriptDetails::eScriptC; 117278f339d1SEwan Crawford script->cacheDir = cachedir; 117378f339d1SEwan Crawford script->resName = resname; 117478f339d1SEwan Crawford script->scriptDyLib = strm.GetData(); 117578f339d1SEwan Crawford script->context = addr_t(rs_context_u64); 117678f339d1SEwan Crawford } 11774640cde1SColin Riley 11784640cde1SColin Riley if (log) 1179b3f7f69dSAidan Dodds log->Printf("%s - '%s' tagged with context 0x%" PRIx64 " and script 0x%" PRIx64 ".", 1180b3f7f69dSAidan Dodds __FUNCTION__, strm.GetData(), rs_context_u64, rs_script_u64); 11814640cde1SColin Riley } 11824640cde1SColin Riley else if (log) 11834640cde1SColin Riley { 1184b3f7f69dSAidan Dodds log->Printf("%s - resource name invalid, Script not tagged.", __FUNCTION__); 11854640cde1SColin Riley } 11864640cde1SColin Riley } 11874640cde1SColin Riley 11884640cde1SColin Riley void 11894640cde1SColin Riley RenderScriptRuntime::LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind) 11904640cde1SColin Riley { 11914640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 11924640cde1SColin Riley 11934640cde1SColin Riley if (!module) 11944640cde1SColin Riley { 11954640cde1SColin Riley return; 11964640cde1SColin Riley } 11974640cde1SColin Riley 119882780287SAidan Dodds Target &target = GetProcess()->GetTarget(); 119982780287SAidan Dodds llvm::Triple::ArchType targetArchType = target.GetArchitecture().GetMachine(); 120082780287SAidan Dodds 1201b3f7f69dSAidan Dodds if (targetArchType != llvm::Triple::ArchType::x86 && 1202b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::arm && 1203b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::aarch64 && 1204b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::mipsel && 1205b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::mips64el && 1206b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::x86_64) 12074640cde1SColin Riley { 12084640cde1SColin Riley if (log) 1209b3f7f69dSAidan Dodds log->Printf("%s - unable to hook runtime functions.", __FUNCTION__); 12104640cde1SColin Riley return; 12114640cde1SColin Riley } 12124640cde1SColin Riley 121382780287SAidan Dodds uint32_t archByteSize = target.GetArchitecture().GetAddressByteSize(); 12144640cde1SColin Riley 12154640cde1SColin Riley for (size_t idx = 0; idx < s_runtimeHookCount; idx++) 12164640cde1SColin Riley { 12174640cde1SColin Riley const HookDefn *hook_defn = &s_runtimeHookDefns[idx]; 1218b3f7f69dSAidan Dodds if (hook_defn->kind != kind) 1219b3f7f69dSAidan Dodds { 12204640cde1SColin Riley continue; 12214640cde1SColin Riley } 12224640cde1SColin Riley 122382780287SAidan Dodds const char *symbol_name = (archByteSize == 4) ? hook_defn->symbol_name_m32 : hook_defn->symbol_name_m64; 122482780287SAidan Dodds 122582780287SAidan Dodds const Symbol *sym = module->FindFirstSymbolWithNameAndType(ConstString(symbol_name), eSymbolTypeCode); 1226b3f7f69dSAidan Dodds if (!sym) 1227b3f7f69dSAidan Dodds { 1228b3f7f69dSAidan Dodds if (log) 1229b3f7f69dSAidan Dodds { 1230b3f7f69dSAidan Dodds log->Printf("%s - symbol '%s' related to the function %s not found", 1231b3f7f69dSAidan Dodds __FUNCTION__, symbol_name, hook_defn->name); 123282780287SAidan Dodds } 123382780287SAidan Dodds continue; 123482780287SAidan Dodds } 12354640cde1SColin Riley 1236358cf1eaSGreg Clayton addr_t addr = sym->GetLoadAddress(&target); 12374640cde1SColin Riley if (addr == LLDB_INVALID_ADDRESS) 12384640cde1SColin Riley { 12394640cde1SColin Riley if (log) 1240b3f7f69dSAidan Dodds log->Printf("%s - unable to resolve the address of hook function '%s' with symbol '%s'.", 1241b3f7f69dSAidan Dodds __FUNCTION__, hook_defn->name, symbol_name); 12424640cde1SColin Riley continue; 12434640cde1SColin Riley } 124482780287SAidan Dodds else 124582780287SAidan Dodds { 124682780287SAidan Dodds if (log) 1247b3f7f69dSAidan Dodds log->Printf("%s - function %s, address resolved at 0x%" PRIx64, 1248b3f7f69dSAidan Dodds __FUNCTION__, hook_defn->name, addr); 124982780287SAidan Dodds } 12504640cde1SColin Riley 12514640cde1SColin Riley RuntimeHookSP hook(new RuntimeHook()); 12524640cde1SColin Riley hook->address = addr; 12534640cde1SColin Riley hook->defn = hook_defn; 12544640cde1SColin Riley hook->bp_sp = target.CreateBreakpoint(addr, true, false); 12554640cde1SColin Riley hook->bp_sp->SetCallback(HookCallback, hook.get(), true); 12564640cde1SColin Riley m_runtimeHooks[addr] = hook; 12574640cde1SColin Riley if (log) 12584640cde1SColin Riley { 1259b3f7f69dSAidan Dodds log->Printf("%s - successfully hooked '%s' in '%s' version %" PRIu64 " at 0x%" PRIx64 ".", 1260b3f7f69dSAidan Dodds __FUNCTION__, hook_defn->name, module->GetFileSpec().GetFilename().AsCString(), 1261b3f7f69dSAidan Dodds (uint64_t)hook_defn->version, (uint64_t)addr); 12624640cde1SColin Riley } 12634640cde1SColin Riley } 12644640cde1SColin Riley } 12654640cde1SColin Riley 12664640cde1SColin Riley void 12674640cde1SColin Riley RenderScriptRuntime::FixupScriptDetails(RSModuleDescriptorSP rsmodule_sp) 12684640cde1SColin Riley { 12694640cde1SColin Riley if (!rsmodule_sp) 12704640cde1SColin Riley return; 12714640cde1SColin Riley 12724640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 12734640cde1SColin Riley 12744640cde1SColin Riley const ModuleSP module = rsmodule_sp->m_module; 12754640cde1SColin Riley const FileSpec &file = module->GetPlatformFileSpec(); 12764640cde1SColin Riley 127778f339d1SEwan Crawford // Iterate over all of the scripts that we currently know of. 127878f339d1SEwan Crawford // Note: We cant push or pop to m_scripts here or it may invalidate rs_script. 12794640cde1SColin Riley for (const auto &rs_script : m_scripts) 12804640cde1SColin Riley { 128178f339d1SEwan Crawford // Extract the expected .so file path for this script. 128278f339d1SEwan Crawford std::string dylib; 128378f339d1SEwan Crawford if (!rs_script->scriptDyLib.get(dylib)) 128478f339d1SEwan Crawford continue; 128578f339d1SEwan Crawford 128678f339d1SEwan Crawford // Only proceed if the module that has loaded corresponds to this script. 128778f339d1SEwan Crawford if (file.GetFilename() != ConstString(dylib.c_str())) 128878f339d1SEwan Crawford continue; 128978f339d1SEwan Crawford 129078f339d1SEwan Crawford // Obtain the script address which we use as a key. 129178f339d1SEwan Crawford lldb::addr_t script; 129278f339d1SEwan Crawford if (!rs_script->script.get(script)) 129378f339d1SEwan Crawford continue; 129478f339d1SEwan Crawford 129578f339d1SEwan Crawford // If we have a script mapping for the current script. 129678f339d1SEwan Crawford if (m_scriptMappings.find(script) != m_scriptMappings.end()) 12974640cde1SColin Riley { 129878f339d1SEwan Crawford // if the module we have stored is different to the one we just received. 129978f339d1SEwan Crawford if (m_scriptMappings[script] != rsmodule_sp) 13004640cde1SColin Riley { 13014640cde1SColin Riley if (log) 1302b3f7f69dSAidan Dodds log->Printf("%s - script %" PRIx64 " wants reassigned to new rsmodule '%s'.", __FUNCTION__, 130378f339d1SEwan Crawford (uint64_t)script, rsmodule_sp->m_module->GetFileSpec().GetFilename().AsCString()); 13044640cde1SColin Riley } 13054640cde1SColin Riley } 130678f339d1SEwan Crawford // We don't have a script mapping for the current script. 13074640cde1SColin Riley else 13084640cde1SColin Riley { 130978f339d1SEwan Crawford // Obtain the script resource name. 131078f339d1SEwan Crawford std::string resName; 131178f339d1SEwan Crawford if (rs_script->resName.get(resName)) 131278f339d1SEwan Crawford // Set the modules resource name. 131378f339d1SEwan Crawford rsmodule_sp->m_resname = resName; 131478f339d1SEwan Crawford // Add Script/Module pair to map. 131578f339d1SEwan Crawford m_scriptMappings[script] = rsmodule_sp; 13164640cde1SColin Riley if (log) 1317b3f7f69dSAidan Dodds log->Printf("%s - script %" PRIx64 " associated with rsmodule '%s'.", __FUNCTION__, 131878f339d1SEwan Crawford (uint64_t)script, rsmodule_sp->m_module->GetFileSpec().GetFilename().AsCString()); 13194640cde1SColin Riley } 13204640cde1SColin Riley } 13214640cde1SColin Riley } 13224640cde1SColin Riley 132315f2bd95SEwan Crawford // Uses the Target API to evaluate the expression passed as a parameter to the function 132415f2bd95SEwan Crawford // The result of that expression is returned an unsigned 64 bit int, via the result* paramter. 132515f2bd95SEwan Crawford // Function returns true on success, and false on failure 132615f2bd95SEwan Crawford bool 132715f2bd95SEwan Crawford RenderScriptRuntime::EvalRSExpression(const char *expression, StackFrame *frame_ptr, uint64_t *result) 132815f2bd95SEwan Crawford { 132915f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 133015f2bd95SEwan Crawford if (log) 1331b3f7f69dSAidan Dodds log->Printf("%s(%s)", __FUNCTION__, expression); 133215f2bd95SEwan Crawford 133315f2bd95SEwan Crawford ValueObjectSP expr_result; 133415f2bd95SEwan Crawford // Perform the actual expression evaluation 133515f2bd95SEwan Crawford GetProcess()->GetTarget().EvaluateExpression(expression, frame_ptr, expr_result); 133615f2bd95SEwan Crawford 133715f2bd95SEwan Crawford if (!expr_result) 133815f2bd95SEwan Crawford { 133915f2bd95SEwan Crawford if (log) 1340b3f7f69dSAidan Dodds log->Printf("%s: couldn't evaluate expression.", __FUNCTION__); 134115f2bd95SEwan Crawford return false; 134215f2bd95SEwan Crawford } 134315f2bd95SEwan Crawford 134415f2bd95SEwan Crawford // The result of the expression is invalid 134515f2bd95SEwan Crawford if (!expr_result->GetError().Success()) 134615f2bd95SEwan Crawford { 134715f2bd95SEwan Crawford Error err = expr_result->GetError(); 134815f2bd95SEwan Crawford if (err.GetError() == UserExpression::kNoResult) // Expression returned void, so this is actually a success 134915f2bd95SEwan Crawford { 135015f2bd95SEwan Crawford if (log) 1351b3f7f69dSAidan Dodds log->Printf("%s - expression returned void.", __FUNCTION__); 135215f2bd95SEwan Crawford 135315f2bd95SEwan Crawford result = nullptr; 135415f2bd95SEwan Crawford return true; 135515f2bd95SEwan Crawford } 135615f2bd95SEwan Crawford 135715f2bd95SEwan Crawford if (log) 1358b3f7f69dSAidan Dodds log->Printf("%s - error evaluating expression result: %s", __FUNCTION__, 1359b3f7f69dSAidan Dodds err.AsCString()); 136015f2bd95SEwan Crawford return false; 136115f2bd95SEwan Crawford } 136215f2bd95SEwan Crawford 136315f2bd95SEwan Crawford bool success = false; 1364b3f7f69dSAidan Dodds *result = expr_result->GetValueAsUnsigned(0, &success); // We only read the result as an uint32_t. 136515f2bd95SEwan Crawford 136615f2bd95SEwan Crawford if (!success) 136715f2bd95SEwan Crawford { 136815f2bd95SEwan Crawford if (log) 1369b3f7f69dSAidan Dodds log->Printf("%s - couldn't convert expression result to uint32_t", __FUNCTION__); 137015f2bd95SEwan Crawford return false; 137115f2bd95SEwan Crawford } 137215f2bd95SEwan Crawford 137315f2bd95SEwan Crawford return true; 137415f2bd95SEwan Crawford } 137515f2bd95SEwan Crawford 1376*ea0636b5SEwan Crawford namespace 1377*ea0636b5SEwan Crawford { 1378836d9651SEwan Crawford // Used to index expression format strings 1379836d9651SEwan Crawford enum ExpressionStrings 138015f2bd95SEwan Crawford { 1381836d9651SEwan Crawford eExprGetOffsetPtr = 0, 1382836d9651SEwan Crawford eExprAllocGetType, 1383836d9651SEwan Crawford eExprTypeDimX, 1384836d9651SEwan Crawford eExprTypeDimY, 1385836d9651SEwan Crawford eExprTypeDimZ, 1386836d9651SEwan Crawford eExprTypeElemPtr, 1387836d9651SEwan Crawford eExprElementType, 1388836d9651SEwan Crawford eExprElementKind, 1389836d9651SEwan Crawford eExprElementVec, 1390836d9651SEwan Crawford eExprElementFieldCount, 1391836d9651SEwan Crawford eExprSubelementsId, 1392836d9651SEwan Crawford eExprSubelementsName, 1393*ea0636b5SEwan Crawford eExprSubelementsArrSize, 1394*ea0636b5SEwan Crawford 1395*ea0636b5SEwan Crawford _eExprLast // keep at the end, implicit size of the array runtimeExpressions 1396836d9651SEwan Crawford }; 139715f2bd95SEwan Crawford 1398*ea0636b5SEwan Crawford // max length of an expanded expression 1399*ea0636b5SEwan Crawford const int jit_max_expr_size = 512; 1400*ea0636b5SEwan Crawford 1401*ea0636b5SEwan Crawford // Retrieve the string to JIT for the given expression 1402*ea0636b5SEwan Crawford const char* 1403*ea0636b5SEwan Crawford JITTemplate(ExpressionStrings e) 140415f2bd95SEwan Crawford { 1405*ea0636b5SEwan Crawford // Format strings containing the expressions we may need to evaluate. 1406*ea0636b5SEwan Crawford static std::array<const char*, _eExprLast> runtimeExpressions = {{ 140715f2bd95SEwan Crawford // Mangled GetOffsetPointer(Allocation*, xoff, yoff, zoff, lod, cubemap) 140815f2bd95SEwan Crawford "(int*)_Z12GetOffsetPtrPKN7android12renderscript10AllocationEjjjj23RsAllocationCubemapFace(0x%lx, %u, %u, %u, 0, 0)", 140915f2bd95SEwan Crawford 141015f2bd95SEwan Crawford // Type* rsaAllocationGetType(Context*, Allocation*) 141115f2bd95SEwan Crawford "(void*)rsaAllocationGetType(0x%lx, 0x%lx)", 141215f2bd95SEwan Crawford 141315f2bd95SEwan Crawford // rsaTypeGetNativeData(Context*, Type*, void* typeData, size) 141415f2bd95SEwan Crawford // Pack the data in the following way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ; 141515f2bd95SEwan Crawford // mHal.state.lodCount; mHal.state.faces; mElement; into typeData 141615f2bd95SEwan Crawford // Need to specify 32 or 64 bit for uint_t since this differs between devices 141715f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[0]", // X dim 141815f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[1]", // Y dim 141915f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[2]", // Z dim 142015f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[5]", // Element ptr 142115f2bd95SEwan Crawford 142215f2bd95SEwan Crawford // rsaElementGetNativeData(Context*, Element*, uint32_t* elemData,size) 142315f2bd95SEwan Crawford // Pack mType; mKind; mNormalized; mVectorSize; NumSubElements into elemData 14248b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[0]", // Type 14258b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[1]", // Kind 14268b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[3]", // Vector Size 14278b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[4]", // Field Count 14288b244e21SEwan Crawford 14298b244e21SEwan Crawford // rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids, const char **names, 14308b244e21SEwan Crawford // size_t *arraySizes, uint32_t dataSize) 14318b244e21SEwan Crawford // Needed for Allocations of structs to gather details about fields/Subelements 14328b244e21SEwan Crawford "void *ids[%u]; const char *names[%u]; size_t arr_size[%u];" 14338b244e21SEwan Crawford "(void*)rsaElementGetSubElements(0x%lx, 0x%lx, ids, names, arr_size, %u); ids[%u]", // Element* of field 14348b244e21SEwan Crawford 14358b244e21SEwan Crawford "void *ids[%u]; const char *names[%u]; size_t arr_size[%u];" 14368b244e21SEwan Crawford "(void*)rsaElementGetSubElements(0x%lx, 0x%lx, ids, names, arr_size, %u); names[%u]", // Name of field 14378b244e21SEwan Crawford 14388b244e21SEwan Crawford "void *ids[%u]; const char *names[%u]; size_t arr_size[%u];" 14398b244e21SEwan Crawford "(void*)rsaElementGetSubElements(0x%lx, 0x%lx, ids, names, arr_size, %u); arr_size[%u]" // Array size of field 1440*ea0636b5SEwan Crawford }}; 1441*ea0636b5SEwan Crawford 1442*ea0636b5SEwan Crawford return runtimeExpressions[e]; 1443*ea0636b5SEwan Crawford } 1444*ea0636b5SEwan Crawford } // end of the anonymous namespace 1445*ea0636b5SEwan Crawford 144615f2bd95SEwan Crawford 144715f2bd95SEwan Crawford // JITs the RS runtime for the internal data pointer of an allocation. 144815f2bd95SEwan Crawford // Is passed x,y,z coordinates for the pointer to a specific element. 144915f2bd95SEwan Crawford // Then sets the data_ptr member in Allocation with the result. 145015f2bd95SEwan Crawford // Returns true on success, false otherwise 145115f2bd95SEwan Crawford bool 1452b3f7f69dSAidan Dodds RenderScriptRuntime::JITDataPointer(AllocationDetails *allocation, StackFrame *frame_ptr, uint32_t x, 1453b3f7f69dSAidan Dodds uint32_t y, uint32_t z) 145415f2bd95SEwan Crawford { 145515f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 145615f2bd95SEwan Crawford 145715f2bd95SEwan Crawford if (!allocation->address.isValid()) 145815f2bd95SEwan Crawford { 145915f2bd95SEwan Crawford if (log) 1460b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 146115f2bd95SEwan Crawford return false; 146215f2bd95SEwan Crawford } 146315f2bd95SEwan Crawford 1464*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprGetOffsetPtr); 1465*ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 146615f2bd95SEwan Crawford 1467*ea0636b5SEwan Crawford int chars_written = snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->address.get(), x, y, z); 146815f2bd95SEwan Crawford if (chars_written < 0) 146915f2bd95SEwan Crawford { 147015f2bd95SEwan Crawford if (log) 1471b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 147215f2bd95SEwan Crawford return false; 147315f2bd95SEwan Crawford } 1474*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 147515f2bd95SEwan Crawford { 147615f2bd95SEwan Crawford if (log) 1477b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 147815f2bd95SEwan Crawford return false; 147915f2bd95SEwan Crawford } 148015f2bd95SEwan Crawford 148115f2bd95SEwan Crawford uint64_t result = 0; 148215f2bd95SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 148315f2bd95SEwan Crawford return false; 148415f2bd95SEwan Crawford 148515f2bd95SEwan Crawford addr_t mem_ptr = static_cast<lldb::addr_t>(result); 148615f2bd95SEwan Crawford allocation->data_ptr = mem_ptr; 148715f2bd95SEwan Crawford 148815f2bd95SEwan Crawford return true; 148915f2bd95SEwan Crawford } 149015f2bd95SEwan Crawford 149115f2bd95SEwan Crawford // JITs the RS runtime for the internal pointer to the RS Type of an allocation 149215f2bd95SEwan Crawford // Then sets the type_ptr member in Allocation with the result. 149315f2bd95SEwan Crawford // Returns true on success, false otherwise 149415f2bd95SEwan Crawford bool 149515f2bd95SEwan Crawford RenderScriptRuntime::JITTypePointer(AllocationDetails *allocation, StackFrame *frame_ptr) 149615f2bd95SEwan Crawford { 149715f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 149815f2bd95SEwan Crawford 149915f2bd95SEwan Crawford if (!allocation->address.isValid() || !allocation->context.isValid()) 150015f2bd95SEwan Crawford { 150115f2bd95SEwan Crawford if (log) 1502b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 150315f2bd95SEwan Crawford return false; 150415f2bd95SEwan Crawford } 150515f2bd95SEwan Crawford 1506*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprAllocGetType); 1507*ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 150815f2bd95SEwan Crawford 1509*ea0636b5SEwan Crawford int chars_written = 1510*ea0636b5SEwan Crawford snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->context.get(), *allocation->address.get()); 151115f2bd95SEwan Crawford if (chars_written < 0) 151215f2bd95SEwan Crawford { 151315f2bd95SEwan Crawford if (log) 1514b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 151515f2bd95SEwan Crawford return false; 151615f2bd95SEwan Crawford } 1517*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 151815f2bd95SEwan Crawford { 151915f2bd95SEwan Crawford if (log) 1520b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 152115f2bd95SEwan Crawford return false; 152215f2bd95SEwan Crawford } 152315f2bd95SEwan Crawford 152415f2bd95SEwan Crawford uint64_t result = 0; 152515f2bd95SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 152615f2bd95SEwan Crawford return false; 152715f2bd95SEwan Crawford 152815f2bd95SEwan Crawford addr_t type_ptr = static_cast<lldb::addr_t>(result); 152915f2bd95SEwan Crawford allocation->type_ptr = type_ptr; 153015f2bd95SEwan Crawford 153115f2bd95SEwan Crawford return true; 153215f2bd95SEwan Crawford } 153315f2bd95SEwan Crawford 153415f2bd95SEwan Crawford // JITs the RS runtime for information about the dimensions and type of an allocation 153515f2bd95SEwan Crawford // Then sets dimension and element_ptr members in Allocation with the result. 153615f2bd95SEwan Crawford // Returns true on success, false otherwise 153715f2bd95SEwan Crawford bool 153815f2bd95SEwan Crawford RenderScriptRuntime::JITTypePacked(AllocationDetails *allocation, StackFrame *frame_ptr) 153915f2bd95SEwan Crawford { 154015f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 154115f2bd95SEwan Crawford 154215f2bd95SEwan Crawford if (!allocation->type_ptr.isValid() || !allocation->context.isValid()) 154315f2bd95SEwan Crawford { 154415f2bd95SEwan Crawford if (log) 1545b3f7f69dSAidan Dodds log->Printf("%s - Failed to find allocation details.", __FUNCTION__); 154615f2bd95SEwan Crawford return false; 154715f2bd95SEwan Crawford } 154815f2bd95SEwan Crawford 154915f2bd95SEwan Crawford // Expression is different depending on if device is 32 or 64 bit 155015f2bd95SEwan Crawford uint32_t archByteSize = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize(); 1551b3f7f69dSAidan Dodds const uint32_t bits = archByteSize == 4 ? 32 : 64; 155215f2bd95SEwan Crawford 155315f2bd95SEwan Crawford // We want 4 elements from packed data 1554b3f7f69dSAidan Dodds const uint32_t num_exprs = 4; 155515f2bd95SEwan Crawford assert(num_exprs == (eExprTypeElemPtr - eExprTypeDimX + 1) && "Invalid number of expressions"); 155615f2bd95SEwan Crawford 1557*ea0636b5SEwan Crawford char buffer[num_exprs][jit_max_expr_size]; 155815f2bd95SEwan Crawford uint64_t results[num_exprs]; 155915f2bd95SEwan Crawford 1560b3f7f69dSAidan Dodds for (uint32_t i = 0; i < num_exprs; ++i) 156115f2bd95SEwan Crawford { 1562*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(ExpressionStrings(eExprTypeDimX + i)); 1563*ea0636b5SEwan Crawford int chars_written = snprintf(buffer[i], jit_max_expr_size, expr_cstr, bits, *allocation->context.get(), 1564*ea0636b5SEwan Crawford *allocation->type_ptr.get()); 156515f2bd95SEwan Crawford if (chars_written < 0) 156615f2bd95SEwan Crawford { 156715f2bd95SEwan Crawford if (log) 1568b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 156915f2bd95SEwan Crawford return false; 157015f2bd95SEwan Crawford } 1571*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 157215f2bd95SEwan Crawford { 157315f2bd95SEwan Crawford if (log) 1574b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 157515f2bd95SEwan Crawford return false; 157615f2bd95SEwan Crawford } 157715f2bd95SEwan Crawford 157815f2bd95SEwan Crawford // Perform expression evaluation 157915f2bd95SEwan Crawford if (!EvalRSExpression(buffer[i], frame_ptr, &results[i])) 158015f2bd95SEwan Crawford return false; 158115f2bd95SEwan Crawford } 158215f2bd95SEwan Crawford 158315f2bd95SEwan Crawford // Assign results to allocation members 158415f2bd95SEwan Crawford AllocationDetails::Dimension dims; 158515f2bd95SEwan Crawford dims.dim_1 = static_cast<uint32_t>(results[0]); 158615f2bd95SEwan Crawford dims.dim_2 = static_cast<uint32_t>(results[1]); 158715f2bd95SEwan Crawford dims.dim_3 = static_cast<uint32_t>(results[2]); 158815f2bd95SEwan Crawford allocation->dimension = dims; 158915f2bd95SEwan Crawford 159015f2bd95SEwan Crawford addr_t elem_ptr = static_cast<lldb::addr_t>(results[3]); 15918b244e21SEwan Crawford allocation->element.element_ptr = elem_ptr; 159215f2bd95SEwan Crawford 159315f2bd95SEwan Crawford if (log) 1594b3f7f69dSAidan Dodds log->Printf("%s - dims (%" PRIu32 ", %" PRIu32 ", %" PRIu32 ") Element*: 0x%" PRIx64 ".", __FUNCTION__, 159515f2bd95SEwan Crawford dims.dim_1, dims.dim_2, dims.dim_3, elem_ptr); 159615f2bd95SEwan Crawford 159715f2bd95SEwan Crawford return true; 159815f2bd95SEwan Crawford } 159915f2bd95SEwan Crawford 160015f2bd95SEwan Crawford // JITs the RS runtime for information about the Element of an allocation 16018b244e21SEwan Crawford // Then sets type, type_vec_size, field_count and type_kind members in Element with the result. 160215f2bd95SEwan Crawford // Returns true on success, false otherwise 160315f2bd95SEwan Crawford bool 16048b244e21SEwan Crawford RenderScriptRuntime::JITElementPacked(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr) 160515f2bd95SEwan Crawford { 160615f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 160715f2bd95SEwan Crawford 16088b244e21SEwan Crawford if (!elem.element_ptr.isValid()) 160915f2bd95SEwan Crawford { 161015f2bd95SEwan Crawford if (log) 1611b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 161215f2bd95SEwan Crawford return false; 161315f2bd95SEwan Crawford } 161415f2bd95SEwan Crawford 16158b244e21SEwan Crawford // We want 4 elements from packed data 1616b3f7f69dSAidan Dodds const uint32_t num_exprs = 4; 16178b244e21SEwan Crawford assert(num_exprs == (eExprElementFieldCount - eExprElementType + 1) && "Invalid number of expressions"); 161815f2bd95SEwan Crawford 1619*ea0636b5SEwan Crawford char buffer[num_exprs][jit_max_expr_size]; 162015f2bd95SEwan Crawford uint64_t results[num_exprs]; 162115f2bd95SEwan Crawford 1622b3f7f69dSAidan Dodds for (uint32_t i = 0; i < num_exprs; i++) 162315f2bd95SEwan Crawford { 1624*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(ExpressionStrings(eExprElementType + i)); 1625*ea0636b5SEwan Crawford int chars_written = snprintf(buffer[i], jit_max_expr_size, expr_cstr, context, *elem.element_ptr.get()); 162615f2bd95SEwan Crawford if (chars_written < 0) 162715f2bd95SEwan Crawford { 162815f2bd95SEwan Crawford if (log) 1629b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 163015f2bd95SEwan Crawford return false; 163115f2bd95SEwan Crawford } 1632*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 163315f2bd95SEwan Crawford { 163415f2bd95SEwan Crawford if (log) 1635b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 163615f2bd95SEwan Crawford return false; 163715f2bd95SEwan Crawford } 163815f2bd95SEwan Crawford 163915f2bd95SEwan Crawford // Perform expression evaluation 164015f2bd95SEwan Crawford if (!EvalRSExpression(buffer[i], frame_ptr, &results[i])) 164115f2bd95SEwan Crawford return false; 164215f2bd95SEwan Crawford } 164315f2bd95SEwan Crawford 164415f2bd95SEwan Crawford // Assign results to allocation members 16458b244e21SEwan Crawford elem.type = static_cast<RenderScriptRuntime::Element::DataType>(results[0]); 16468b244e21SEwan Crawford elem.type_kind = static_cast<RenderScriptRuntime::Element::DataKind>(results[1]); 16478b244e21SEwan Crawford elem.type_vec_size = static_cast<uint32_t>(results[2]); 16488b244e21SEwan Crawford elem.field_count = static_cast<uint32_t>(results[3]); 164915f2bd95SEwan Crawford 165015f2bd95SEwan Crawford if (log) 1651b3f7f69dSAidan Dodds log->Printf("%s - data type %" PRIu32 ", pixel type %" PRIu32 ", vector size %" PRIu32 ", field count %" PRIu32, 1652b3f7f69dSAidan Dodds __FUNCTION__, *elem.type.get(), *elem.type_kind.get(), *elem.type_vec_size.get(), *elem.field_count.get()); 16538b244e21SEwan Crawford 16548b244e21SEwan Crawford // If this Element has subelements then JIT rsaElementGetSubElements() for details about its fields 16558b244e21SEwan Crawford if (*elem.field_count.get() > 0 && !JITSubelements(elem, context, frame_ptr)) 16568b244e21SEwan Crawford return false; 16578b244e21SEwan Crawford 16588b244e21SEwan Crawford return true; 16598b244e21SEwan Crawford } 16608b244e21SEwan Crawford 16618b244e21SEwan Crawford // JITs the RS runtime for information about the subelements/fields of a struct allocation 16628b244e21SEwan Crawford // This is necessary for infering the struct type so we can pretty print the allocation's contents. 16638b244e21SEwan Crawford // Returns true on success, false otherwise 16648b244e21SEwan Crawford bool 16658b244e21SEwan Crawford RenderScriptRuntime::JITSubelements(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr) 16668b244e21SEwan Crawford { 16678b244e21SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 16688b244e21SEwan Crawford 16698b244e21SEwan Crawford if (!elem.element_ptr.isValid() || !elem.field_count.isValid()) 16708b244e21SEwan Crawford { 16718b244e21SEwan Crawford if (log) 1672b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 16738b244e21SEwan Crawford return false; 16748b244e21SEwan Crawford } 16758b244e21SEwan Crawford 16768b244e21SEwan Crawford const short num_exprs = 3; 16778b244e21SEwan Crawford assert(num_exprs == (eExprSubelementsArrSize - eExprSubelementsId + 1) && "Invalid number of expressions"); 16788b244e21SEwan Crawford 1679*ea0636b5SEwan Crawford char expr_buffer[jit_max_expr_size]; 16808b244e21SEwan Crawford uint64_t results; 16818b244e21SEwan Crawford 16828b244e21SEwan Crawford // Iterate over struct fields. 16838b244e21SEwan Crawford const uint32_t field_count = *elem.field_count.get(); 1684b3f7f69dSAidan Dodds for (uint32_t field_index = 0; field_index < field_count; ++field_index) 16858b244e21SEwan Crawford { 16868b244e21SEwan Crawford Element child; 1687b3f7f69dSAidan Dodds for (uint32_t expr_index = 0; expr_index < num_exprs; ++expr_index) 16888b244e21SEwan Crawford { 1689*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(ExpressionStrings(eExprSubelementsId + expr_index)); 1690*ea0636b5SEwan Crawford int chars_written = snprintf(expr_buffer, jit_max_expr_size, expr_cstr, 16918b244e21SEwan Crawford field_count, field_count, field_count, 16928b244e21SEwan Crawford context, *elem.element_ptr.get(), field_count, field_index); 16938b244e21SEwan Crawford if (chars_written < 0) 16948b244e21SEwan Crawford { 16958b244e21SEwan Crawford if (log) 1696b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 16978b244e21SEwan Crawford return false; 16988b244e21SEwan Crawford } 1699*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 17008b244e21SEwan Crawford { 17018b244e21SEwan Crawford if (log) 1702b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 17038b244e21SEwan Crawford return false; 17048b244e21SEwan Crawford } 17058b244e21SEwan Crawford 17068b244e21SEwan Crawford // Perform expression evaluation 17078b244e21SEwan Crawford if (!EvalRSExpression(expr_buffer, frame_ptr, &results)) 17088b244e21SEwan Crawford return false; 17098b244e21SEwan Crawford 17108b244e21SEwan Crawford if (log) 1711b3f7f69dSAidan Dodds log->Printf("%s - expr result 0x%" PRIx64 ".", __FUNCTION__, results); 17128b244e21SEwan Crawford 17138b244e21SEwan Crawford switch (expr_index) 17148b244e21SEwan Crawford { 17158b244e21SEwan Crawford case 0: // Element* of child 17168b244e21SEwan Crawford child.element_ptr = static_cast<addr_t>(results); 17178b244e21SEwan Crawford break; 17188b244e21SEwan Crawford case 1: // Name of child 17198b244e21SEwan Crawford { 17208b244e21SEwan Crawford lldb::addr_t address = static_cast<addr_t>(results); 17218b244e21SEwan Crawford Error err; 17228b244e21SEwan Crawford std::string name; 17238b244e21SEwan Crawford GetProcess()->ReadCStringFromMemory(address, name, err); 17248b244e21SEwan Crawford if (!err.Fail()) 17258b244e21SEwan Crawford child.type_name = ConstString(name); 17268b244e21SEwan Crawford else 17278b244e21SEwan Crawford { 17288b244e21SEwan Crawford if (log) 1729b3f7f69dSAidan Dodds log->Printf("%s - warning: Couldn't read field name.", __FUNCTION__); 17308b244e21SEwan Crawford } 17318b244e21SEwan Crawford break; 17328b244e21SEwan Crawford } 17338b244e21SEwan Crawford case 2: // Array size of child 17348b244e21SEwan Crawford child.array_size = static_cast<uint32_t>(results); 17358b244e21SEwan Crawford break; 17368b244e21SEwan Crawford } 17378b244e21SEwan Crawford } 17388b244e21SEwan Crawford 17398b244e21SEwan Crawford // We need to recursively JIT each Element field of the struct since 17408b244e21SEwan Crawford // structs can be nested inside structs. 17418b244e21SEwan Crawford if (!JITElementPacked(child, context, frame_ptr)) 17428b244e21SEwan Crawford return false; 17438b244e21SEwan Crawford elem.children.push_back(child); 17448b244e21SEwan Crawford } 17458b244e21SEwan Crawford 17468b244e21SEwan Crawford // Try to infer the name of the struct type so we can pretty print the allocation contents. 17478b244e21SEwan Crawford FindStructTypeName(elem, frame_ptr); 174815f2bd95SEwan Crawford 174915f2bd95SEwan Crawford return true; 175015f2bd95SEwan Crawford } 175115f2bd95SEwan Crawford 1752a0f08674SEwan Crawford // JITs the RS runtime for the address of the last element in the allocation. 1753a0f08674SEwan Crawford // The `elem_size` paramter represents the size of a single element, including padding. 1754a0f08674SEwan Crawford // Which is needed as an offset from the last element pointer. 1755a0f08674SEwan Crawford // Using this offset minus the starting address we can calculate the size of the allocation. 1756a0f08674SEwan Crawford // Returns true on success, false otherwise 1757a0f08674SEwan Crawford bool 17588b244e21SEwan Crawford RenderScriptRuntime::JITAllocationSize(AllocationDetails *allocation, StackFrame *frame_ptr) 1759a0f08674SEwan Crawford { 1760a0f08674SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1761a0f08674SEwan Crawford 1762b3f7f69dSAidan Dodds if (!allocation->address.isValid() || !allocation->dimension.isValid() || !allocation->data_ptr.isValid() || 1763b3f7f69dSAidan Dodds !allocation->element.datum_size.isValid()) 1764a0f08674SEwan Crawford { 1765a0f08674SEwan Crawford if (log) 1766b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 1767a0f08674SEwan Crawford return false; 1768a0f08674SEwan Crawford } 1769a0f08674SEwan Crawford 1770a0f08674SEwan Crawford // Find dimensions 1771b3f7f69dSAidan Dodds uint32_t dim_x = allocation->dimension.get()->dim_1; 1772b3f7f69dSAidan Dodds uint32_t dim_y = allocation->dimension.get()->dim_2; 1773b3f7f69dSAidan Dodds uint32_t dim_z = allocation->dimension.get()->dim_3; 1774a0f08674SEwan Crawford 17758b244e21SEwan Crawford // Our plan of jitting the last element address doesn't seem to work for struct Allocations 17768b244e21SEwan Crawford // Instead try to infer the size ourselves without any inter element padding. 17778b244e21SEwan Crawford if (allocation->element.children.size() > 0) 17788b244e21SEwan Crawford { 17798b244e21SEwan Crawford if (dim_x == 0) dim_x = 1; 17808b244e21SEwan Crawford if (dim_y == 0) dim_y = 1; 17818b244e21SEwan Crawford if (dim_z == 0) dim_z = 1; 17828b244e21SEwan Crawford 17838b244e21SEwan Crawford allocation->size = dim_x * dim_y * dim_z * *allocation->element.datum_size.get(); 17848b244e21SEwan Crawford 17858b244e21SEwan Crawford if (log) 1786b3f7f69dSAidan Dodds log->Printf("%s - infered size of struct allocation %" PRIu32 ".", __FUNCTION__, 1787b3f7f69dSAidan Dodds *allocation->size.get()); 17888b244e21SEwan Crawford return true; 17898b244e21SEwan Crawford } 17908b244e21SEwan Crawford 1791*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprGetOffsetPtr); 1792*ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 17938b244e21SEwan Crawford 1794a0f08674SEwan Crawford // Calculate last element 1795a0f08674SEwan Crawford dim_x = dim_x == 0 ? 0 : dim_x - 1; 1796a0f08674SEwan Crawford dim_y = dim_y == 0 ? 0 : dim_y - 1; 1797a0f08674SEwan Crawford dim_z = dim_z == 0 ? 0 : dim_z - 1; 1798a0f08674SEwan Crawford 1799*ea0636b5SEwan Crawford int chars_written = snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->address.get(), dim_x, dim_y, dim_z); 1800a0f08674SEwan Crawford if (chars_written < 0) 1801a0f08674SEwan Crawford { 1802a0f08674SEwan Crawford if (log) 1803b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 1804a0f08674SEwan Crawford return false; 1805a0f08674SEwan Crawford } 1806*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 1807a0f08674SEwan Crawford { 1808a0f08674SEwan Crawford if (log) 1809b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 1810a0f08674SEwan Crawford return false; 1811a0f08674SEwan Crawford } 1812a0f08674SEwan Crawford 1813a0f08674SEwan Crawford uint64_t result = 0; 1814a0f08674SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 1815a0f08674SEwan Crawford return false; 1816a0f08674SEwan Crawford 1817a0f08674SEwan Crawford addr_t mem_ptr = static_cast<lldb::addr_t>(result); 1818a0f08674SEwan Crawford // Find pointer to last element and add on size of an element 1819b3f7f69dSAidan Dodds allocation->size = 1820b3f7f69dSAidan Dodds static_cast<uint32_t>(mem_ptr - *allocation->data_ptr.get()) + *allocation->element.datum_size.get(); 1821a0f08674SEwan Crawford 1822a0f08674SEwan Crawford return true; 1823a0f08674SEwan Crawford } 1824a0f08674SEwan Crawford 1825a0f08674SEwan Crawford // JITs the RS runtime for information about the stride between rows in the allocation. 1826a0f08674SEwan Crawford // This is done to detect padding, since allocated memory is 16-byte aligned. 1827a0f08674SEwan Crawford // Returns true on success, false otherwise 1828a0f08674SEwan Crawford bool 1829a0f08674SEwan Crawford RenderScriptRuntime::JITAllocationStride(AllocationDetails *allocation, StackFrame *frame_ptr) 1830a0f08674SEwan Crawford { 1831a0f08674SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1832a0f08674SEwan Crawford 1833a0f08674SEwan Crawford if (!allocation->address.isValid() || !allocation->data_ptr.isValid()) 1834a0f08674SEwan Crawford { 1835a0f08674SEwan Crawford if (log) 1836b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 1837a0f08674SEwan Crawford return false; 1838a0f08674SEwan Crawford } 1839a0f08674SEwan Crawford 1840*ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprGetOffsetPtr); 1841*ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 1842a0f08674SEwan Crawford 1843*ea0636b5SEwan Crawford int chars_written = snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->address.get(), 0, 1, 0); 1844a0f08674SEwan Crawford if (chars_written < 0) 1845a0f08674SEwan Crawford { 1846a0f08674SEwan Crawford if (log) 1847b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 1848a0f08674SEwan Crawford return false; 1849a0f08674SEwan Crawford } 1850*ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 1851a0f08674SEwan Crawford { 1852a0f08674SEwan Crawford if (log) 1853b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 1854a0f08674SEwan Crawford return false; 1855a0f08674SEwan Crawford } 1856a0f08674SEwan Crawford 1857a0f08674SEwan Crawford uint64_t result = 0; 1858a0f08674SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 1859a0f08674SEwan Crawford return false; 1860a0f08674SEwan Crawford 1861a0f08674SEwan Crawford addr_t mem_ptr = static_cast<lldb::addr_t>(result); 1862a0f08674SEwan Crawford allocation->stride = static_cast<uint32_t>(mem_ptr - *allocation->data_ptr.get()); 1863a0f08674SEwan Crawford 1864a0f08674SEwan Crawford return true; 1865a0f08674SEwan Crawford } 1866a0f08674SEwan Crawford 186715f2bd95SEwan Crawford // JIT all the current runtime info regarding an allocation 186815f2bd95SEwan Crawford bool 186915f2bd95SEwan Crawford RenderScriptRuntime::RefreshAllocation(AllocationDetails *allocation, StackFrame *frame_ptr) 187015f2bd95SEwan Crawford { 187115f2bd95SEwan Crawford // GetOffsetPointer() 187215f2bd95SEwan Crawford if (!JITDataPointer(allocation, frame_ptr)) 187315f2bd95SEwan Crawford return false; 187415f2bd95SEwan Crawford 187515f2bd95SEwan Crawford // rsaAllocationGetType() 187615f2bd95SEwan Crawford if (!JITTypePointer(allocation, frame_ptr)) 187715f2bd95SEwan Crawford return false; 187815f2bd95SEwan Crawford 187915f2bd95SEwan Crawford // rsaTypeGetNativeData() 188015f2bd95SEwan Crawford if (!JITTypePacked(allocation, frame_ptr)) 188115f2bd95SEwan Crawford return false; 188215f2bd95SEwan Crawford 188315f2bd95SEwan Crawford // rsaElementGetNativeData() 18848b244e21SEwan Crawford if (!JITElementPacked(allocation->element, *allocation->context.get(), frame_ptr)) 188515f2bd95SEwan Crawford return false; 188615f2bd95SEwan Crawford 18878b244e21SEwan Crawford // Sets the datum_size member in Element 18888b244e21SEwan Crawford SetElementSize(allocation->element); 18898b244e21SEwan Crawford 189055232f09SEwan Crawford // Use GetOffsetPointer() to infer size of the allocation 18918b244e21SEwan Crawford if (!JITAllocationSize(allocation, frame_ptr)) 189255232f09SEwan Crawford return false; 189355232f09SEwan Crawford 189455232f09SEwan Crawford return true; 189555232f09SEwan Crawford } 189655232f09SEwan Crawford 18978b244e21SEwan Crawford // Function attempts to set the type_name member of the paramaterised Element object. 18988b244e21SEwan Crawford // This string should be the name of the struct type the Element represents. 18998b244e21SEwan Crawford // We need this string for pretty printing the Element to users. 19008b244e21SEwan Crawford void 19018b244e21SEwan Crawford RenderScriptRuntime::FindStructTypeName(Element &elem, StackFrame *frame_ptr) 190255232f09SEwan Crawford { 19038b244e21SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 19048b244e21SEwan Crawford 19058b244e21SEwan Crawford if (!elem.type_name.IsEmpty()) // Name already set 19068b244e21SEwan Crawford return; 19078b244e21SEwan Crawford else 1908fe06b5adSAdrian McCarthy elem.type_name = Element::GetFallbackStructName(); // Default type name if we don't succeed 19098b244e21SEwan Crawford 19108b244e21SEwan Crawford // Find all the global variables from the script rs modules 19118b244e21SEwan Crawford VariableList variable_list; 19128b244e21SEwan Crawford for (auto module_sp : m_rsmodules) 19138b244e21SEwan Crawford module_sp->m_module->FindGlobalVariables(RegularExpression("."), true, UINT32_MAX, variable_list); 19148b244e21SEwan Crawford 19158b244e21SEwan Crawford // Iterate over all the global variables looking for one with a matching type to the Element. 19168b244e21SEwan Crawford // We make the assumption a match exists since there needs to be a global variable to reflect the 19178b244e21SEwan Crawford // struct type back into java host code. 19188b244e21SEwan Crawford for (uint32_t var_index = 0; var_index < variable_list.GetSize(); ++var_index) 19198b244e21SEwan Crawford { 19208b244e21SEwan Crawford const VariableSP var_sp(variable_list.GetVariableAtIndex(var_index)); 19218b244e21SEwan Crawford if (!var_sp) 19228b244e21SEwan Crawford continue; 19238b244e21SEwan Crawford 19248b244e21SEwan Crawford ValueObjectSP valobj_sp = ValueObjectVariable::Create(frame_ptr, var_sp); 19258b244e21SEwan Crawford if (!valobj_sp) 19268b244e21SEwan Crawford continue; 19278b244e21SEwan Crawford 19288b244e21SEwan Crawford // Find the number of variable fields. 19298b244e21SEwan Crawford // If it has no fields, or more fields than our Element, then it can't be the struct we're looking for. 19308b244e21SEwan Crawford // Don't check for equality since RS can add extra struct members for padding. 19318b244e21SEwan Crawford size_t num_children = valobj_sp->GetNumChildren(); 19328b244e21SEwan Crawford if (num_children > elem.children.size() || num_children == 0) 19338b244e21SEwan Crawford continue; 19348b244e21SEwan Crawford 19358b244e21SEwan Crawford // Iterate over children looking for members with matching field names. 19368b244e21SEwan Crawford // If all the field names match, this is likely the struct we want. 19378b244e21SEwan Crawford // 19388b244e21SEwan Crawford // TODO: This could be made more robust by also checking children data sizes, or array size 19398b244e21SEwan Crawford bool found = true; 19408b244e21SEwan Crawford for (size_t child_index = 0; child_index < num_children; ++child_index) 19418b244e21SEwan Crawford { 19428b244e21SEwan Crawford ValueObjectSP child = valobj_sp->GetChildAtIndex(child_index, true); 19438b244e21SEwan Crawford if (!child || (child->GetName() != elem.children[child_index].type_name)) 19448b244e21SEwan Crawford { 19458b244e21SEwan Crawford found = false; 19468b244e21SEwan Crawford break; 19478b244e21SEwan Crawford } 19488b244e21SEwan Crawford } 19498b244e21SEwan Crawford 19508b244e21SEwan Crawford // RS can add extra struct members for padding in the format '#rs_padding_[0-9]+' 19518b244e21SEwan Crawford if (found && num_children < elem.children.size()) 19528b244e21SEwan Crawford { 1953b3f7f69dSAidan Dodds const uint32_t size_diff = elem.children.size() - num_children; 19548b244e21SEwan Crawford if (log) 1955b3f7f69dSAidan Dodds log->Printf("%s - %" PRIu32 " padding struct entries", __FUNCTION__, size_diff); 19568b244e21SEwan Crawford 1957b3f7f69dSAidan Dodds for (uint32_t padding_index = 0; padding_index < size_diff; ++padding_index) 19588b244e21SEwan Crawford { 19598b244e21SEwan Crawford const ConstString &name = elem.children[num_children + padding_index].type_name; 19608b244e21SEwan Crawford if (strcmp(name.AsCString(), "#rs_padding") < 0) 19618b244e21SEwan Crawford found = false; 19628b244e21SEwan Crawford } 19638b244e21SEwan Crawford } 19648b244e21SEwan Crawford 19658b244e21SEwan Crawford // We've found a global var with matching type 19668b244e21SEwan Crawford if (found) 19678b244e21SEwan Crawford { 19688b244e21SEwan Crawford // Dereference since our Element type isn't a pointer. 19698b244e21SEwan Crawford if (valobj_sp->IsPointerType()) 19708b244e21SEwan Crawford { 19718b244e21SEwan Crawford Error err; 19728b244e21SEwan Crawford ValueObjectSP deref_valobj = valobj_sp->Dereference(err); 19738b244e21SEwan Crawford if (!err.Fail()) 19748b244e21SEwan Crawford valobj_sp = deref_valobj; 19758b244e21SEwan Crawford } 19768b244e21SEwan Crawford 19778b244e21SEwan Crawford // Save name of variable in Element. 19788b244e21SEwan Crawford elem.type_name = valobj_sp->GetTypeName(); 19798b244e21SEwan Crawford if (log) 1980b3f7f69dSAidan Dodds log->Printf("%s - element name set to %s", __FUNCTION__, elem.type_name.AsCString()); 19818b244e21SEwan Crawford 19828b244e21SEwan Crawford return; 19838b244e21SEwan Crawford } 19848b244e21SEwan Crawford } 19858b244e21SEwan Crawford } 19868b244e21SEwan Crawford 19878b244e21SEwan Crawford // Function sets the datum_size member of Element. Representing the size of a single instance including padding. 19888b244e21SEwan Crawford // Assumes the relevant allocation information has already been jitted. 19898b244e21SEwan Crawford void 19908b244e21SEwan Crawford RenderScriptRuntime::SetElementSize(Element &elem) 19918b244e21SEwan Crawford { 19928b244e21SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 19938b244e21SEwan Crawford const Element::DataType type = *elem.type.get(); 1994b3f7f69dSAidan Dodds assert(type >= Element::RS_TYPE_NONE && type <= Element::RS_TYPE_FONT && "Invalid allocation type"); 199555232f09SEwan Crawford 1996b3f7f69dSAidan Dodds const uint32_t vec_size = *elem.type_vec_size.get(); 1997b3f7f69dSAidan Dodds uint32_t data_size = 0; 1998b3f7f69dSAidan Dodds uint32_t padding = 0; 199955232f09SEwan Crawford 20008b244e21SEwan Crawford // Element is of a struct type, calculate size recursively. 20018b244e21SEwan Crawford if ((type == Element::RS_TYPE_NONE) && (elem.children.size() > 0)) 20028b244e21SEwan Crawford { 20038b244e21SEwan Crawford for (Element &child : elem.children) 20048b244e21SEwan Crawford { 20058b244e21SEwan Crawford SetElementSize(child); 2006b3f7f69dSAidan Dodds const uint32_t array_size = child.array_size.isValid() ? *child.array_size.get() : 1; 20078b244e21SEwan Crawford data_size += *child.datum_size.get() * array_size; 20088b244e21SEwan Crawford } 20098b244e21SEwan Crawford } 2010b3f7f69dSAidan Dodds // These have been packed already 2011b3f7f69dSAidan Dodds else if (type == Element::RS_TYPE_UNSIGNED_5_6_5 || 2012b3f7f69dSAidan Dodds type == Element::RS_TYPE_UNSIGNED_5_5_5_1 || 2013b3f7f69dSAidan Dodds type == Element::RS_TYPE_UNSIGNED_4_4_4_4) 20142e920715SEwan Crawford { 20152e920715SEwan Crawford data_size = AllocationDetails::RSTypeToFormat[type][eElementSize]; 20162e920715SEwan Crawford } 20172e920715SEwan Crawford else if (type < Element::RS_TYPE_ELEMENT) 20182e920715SEwan Crawford { 20198b244e21SEwan Crawford data_size = vec_size * AllocationDetails::RSTypeToFormat[type][eElementSize]; 20202e920715SEwan Crawford if (vec_size == 3) 20212e920715SEwan Crawford padding = AllocationDetails::RSTypeToFormat[type][eElementSize]; 20222e920715SEwan Crawford } 20232e920715SEwan Crawford else 20242e920715SEwan Crawford data_size = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize(); 20258b244e21SEwan Crawford 20268b244e21SEwan Crawford elem.padding = padding; 20278b244e21SEwan Crawford elem.datum_size = data_size + padding; 20288b244e21SEwan Crawford if (log) 2029b3f7f69dSAidan Dodds log->Printf("%s - element size set to %" PRIu32, __FUNCTION__, data_size + padding); 203055232f09SEwan Crawford } 203155232f09SEwan Crawford 203255232f09SEwan Crawford // Given an allocation, this function copies the allocation contents from device into a buffer on the heap. 203355232f09SEwan Crawford // Returning a shared pointer to the buffer containing the data. 203455232f09SEwan Crawford std::shared_ptr<uint8_t> 203555232f09SEwan Crawford RenderScriptRuntime::GetAllocationData(AllocationDetails *allocation, StackFrame *frame_ptr) 203655232f09SEwan Crawford { 203755232f09SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 203855232f09SEwan Crawford 203955232f09SEwan Crawford // JIT all the allocation details 20408b59062aSEwan Crawford if (allocation->shouldRefresh()) 204155232f09SEwan Crawford { 204255232f09SEwan Crawford if (log) 2043b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info", __FUNCTION__); 204455232f09SEwan Crawford 204555232f09SEwan Crawford if (!RefreshAllocation(allocation, frame_ptr)) 204655232f09SEwan Crawford { 204755232f09SEwan Crawford if (log) 2048b3f7f69dSAidan Dodds log->Printf("%s - couldn't JIT allocation details", __FUNCTION__); 204955232f09SEwan Crawford return nullptr; 205055232f09SEwan Crawford } 205155232f09SEwan Crawford } 205255232f09SEwan Crawford 2053b3f7f69dSAidan Dodds assert(allocation->data_ptr.isValid() && allocation->element.type.isValid() && 2054b3f7f69dSAidan Dodds allocation->element.type_vec_size.isValid() && allocation->size.isValid() && 2055b3f7f69dSAidan Dodds "Allocation information not available"); 205655232f09SEwan Crawford 205755232f09SEwan Crawford // Allocate a buffer to copy data into 2058b3f7f69dSAidan Dodds const uint32_t size = *allocation->size.get(); 205955232f09SEwan Crawford std::shared_ptr<uint8_t> buffer(new uint8_t[size]); 206055232f09SEwan Crawford if (!buffer) 206155232f09SEwan Crawford { 206255232f09SEwan Crawford if (log) 2063b3f7f69dSAidan Dodds log->Printf("%s - couldn't allocate a %" PRIu32 " byte buffer", __FUNCTION__, size); 206455232f09SEwan Crawford return nullptr; 206555232f09SEwan Crawford } 206655232f09SEwan Crawford 206755232f09SEwan Crawford // Read the inferior memory 206855232f09SEwan Crawford Error error; 206955232f09SEwan Crawford lldb::addr_t data_ptr = *allocation->data_ptr.get(); 207055232f09SEwan Crawford GetProcess()->ReadMemory(data_ptr, buffer.get(), size, error); 207155232f09SEwan Crawford if (error.Fail()) 207255232f09SEwan Crawford { 207355232f09SEwan Crawford if (log) 2074b3f7f69dSAidan Dodds log->Printf("%s - '%s' Couldn't read %" PRIu32 " bytes of allocation data from 0x%" PRIx64, 2075b3f7f69dSAidan Dodds __FUNCTION__, error.AsCString(), size, data_ptr); 207655232f09SEwan Crawford return nullptr; 207755232f09SEwan Crawford } 207855232f09SEwan Crawford 207955232f09SEwan Crawford return buffer; 208055232f09SEwan Crawford } 208155232f09SEwan Crawford 208255232f09SEwan Crawford // Function copies data from a binary file into an allocation. 208355232f09SEwan Crawford // There is a header at the start of the file, FileHeader, before the data content itself. 208455232f09SEwan Crawford // Information from this header is used to display warnings to the user about incompatabilities 208555232f09SEwan Crawford bool 208655232f09SEwan Crawford RenderScriptRuntime::LoadAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr) 208755232f09SEwan Crawford { 208855232f09SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 208955232f09SEwan Crawford 209055232f09SEwan Crawford // Find allocation with the given id 209155232f09SEwan Crawford AllocationDetails *alloc = FindAllocByID(strm, alloc_id); 209255232f09SEwan Crawford if (!alloc) 209355232f09SEwan Crawford return false; 209455232f09SEwan Crawford 209555232f09SEwan Crawford if (log) 2096b3f7f69dSAidan Dodds log->Printf("%s - found allocation 0x%" PRIx64, __FUNCTION__, *alloc->address.get()); 209755232f09SEwan Crawford 209855232f09SEwan Crawford // JIT all the allocation details 20998b59062aSEwan Crawford if (alloc->shouldRefresh()) 210055232f09SEwan Crawford { 210155232f09SEwan Crawford if (log) 2102b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info.", __FUNCTION__); 210355232f09SEwan Crawford 210455232f09SEwan Crawford if (!RefreshAllocation(alloc, frame_ptr)) 210555232f09SEwan Crawford { 210655232f09SEwan Crawford if (log) 2107b3f7f69dSAidan Dodds log->Printf("%s - couldn't JIT allocation details", __FUNCTION__); 21084cfc9198SSylvestre Ledru return false; 210955232f09SEwan Crawford } 211055232f09SEwan Crawford } 211155232f09SEwan Crawford 2112b3f7f69dSAidan Dodds assert(alloc->data_ptr.isValid() && alloc->element.type.isValid() && alloc->element.type_vec_size.isValid() && 2113b3f7f69dSAidan Dodds alloc->size.isValid() && alloc->element.datum_size.isValid() && "Allocation information not available"); 211455232f09SEwan Crawford 211555232f09SEwan Crawford // Check we can read from file 211655232f09SEwan Crawford FileSpec file(filename, true); 211755232f09SEwan Crawford if (!file.Exists()) 211855232f09SEwan Crawford { 211955232f09SEwan Crawford strm.Printf("Error: File %s does not exist", filename); 212055232f09SEwan Crawford strm.EOL(); 212155232f09SEwan Crawford return false; 212255232f09SEwan Crawford } 212355232f09SEwan Crawford 212455232f09SEwan Crawford if (!file.Readable()) 212555232f09SEwan Crawford { 212655232f09SEwan Crawford strm.Printf("Error: File %s does not have readable permissions", filename); 212755232f09SEwan Crawford strm.EOL(); 212855232f09SEwan Crawford return false; 212955232f09SEwan Crawford } 213055232f09SEwan Crawford 213155232f09SEwan Crawford // Read file into data buffer 213255232f09SEwan Crawford DataBufferSP data_sp(file.ReadFileContents()); 213355232f09SEwan Crawford 213455232f09SEwan Crawford // Cast start of buffer to FileHeader and use pointer to read metadata 213555232f09SEwan Crawford void *file_buffer = data_sp->GetBytes(); 2136b3f7f69dSAidan Dodds if (file_buffer == nullptr || 2137b3f7f69dSAidan Dodds data_sp->GetByteSize() < (sizeof(AllocationDetails::FileHeader) + sizeof(AllocationDetails::ElementHeader))) 213826e52a70SEwan Crawford { 213926e52a70SEwan Crawford strm.Printf("Error: File %s does not contain enough data for header", filename); 214026e52a70SEwan Crawford strm.EOL(); 214126e52a70SEwan Crawford return false; 214226e52a70SEwan Crawford } 214326e52a70SEwan Crawford const AllocationDetails::FileHeader *file_header = static_cast<AllocationDetails::FileHeader *>(file_buffer); 214455232f09SEwan Crawford 214526e52a70SEwan Crawford // Check file starts with ascii characters "RSAD" 2146b3f7f69dSAidan Dodds if (memcmp(file_header->ident, "RSAD", 4)) 214726e52a70SEwan Crawford { 214826e52a70SEwan Crawford strm.Printf("Error: File doesn't contain identifier for an RS allocation dump. Are you sure this is the correct file?"); 214926e52a70SEwan Crawford strm.EOL(); 215026e52a70SEwan Crawford return false; 215126e52a70SEwan Crawford } 215226e52a70SEwan Crawford 215326e52a70SEwan Crawford // Look at the type of the root element in the header 215426e52a70SEwan Crawford AllocationDetails::ElementHeader root_element_header; 215526e52a70SEwan Crawford memcpy(&root_element_header, static_cast<uint8_t *>(file_buffer) + sizeof(AllocationDetails::FileHeader), 215626e52a70SEwan Crawford sizeof(AllocationDetails::ElementHeader)); 215755232f09SEwan Crawford 215855232f09SEwan Crawford if (log) 2159b3f7f69dSAidan Dodds log->Printf("%s - header type %" PRIu32 ", element size %" PRIu32, __FUNCTION__, 216026e52a70SEwan Crawford root_element_header.type, root_element_header.element_size); 216155232f09SEwan Crawford 216255232f09SEwan Crawford // Check if the target allocation and file both have the same number of bytes for an Element 216326e52a70SEwan Crawford if (*alloc->element.datum_size.get() != root_element_header.element_size) 216455232f09SEwan Crawford { 2165b3f7f69dSAidan Dodds strm.Printf("Warning: Mismatched Element sizes - file %" PRIu32 " bytes, allocation %" PRIu32 " bytes", 216626e52a70SEwan Crawford root_element_header.element_size, *alloc->element.datum_size.get()); 216755232f09SEwan Crawford strm.EOL(); 216855232f09SEwan Crawford } 216955232f09SEwan Crawford 217026e52a70SEwan Crawford // Check if the target allocation and file both have the same type 2171b3f7f69dSAidan Dodds const uint32_t alloc_type = static_cast<uint32_t>(*alloc->element.type.get()); 2172b3f7f69dSAidan Dodds const uint32_t file_type = root_element_header.type; 217326e52a70SEwan Crawford 217426e52a70SEwan Crawford if (file_type > Element::RS_TYPE_FONT) 217526e52a70SEwan Crawford { 217626e52a70SEwan Crawford strm.Printf("Warning: File has unknown allocation type"); 217726e52a70SEwan Crawford strm.EOL(); 217826e52a70SEwan Crawford } 217926e52a70SEwan Crawford else if (alloc_type != file_type) 218055232f09SEwan Crawford { 21812e920715SEwan Crawford // Enum value isn't monotonous, so doesn't always index RsDataTypeToString array 2182b3f7f69dSAidan Dodds uint32_t printable_target_type_index = alloc_type; 2183b3f7f69dSAidan Dodds uint32_t printable_head_type_index = file_type; 218426e52a70SEwan Crawford if (alloc_type >= Element::RS_TYPE_ELEMENT && alloc_type <= Element::RS_TYPE_FONT) 2185b3f7f69dSAidan Dodds printable_target_type_index = static_cast<Element::DataType>((alloc_type - Element::RS_TYPE_ELEMENT) + 2186b3f7f69dSAidan Dodds Element::RS_TYPE_MATRIX_2X2 + 1); 21872e920715SEwan Crawford 218826e52a70SEwan Crawford if (file_type >= Element::RS_TYPE_ELEMENT && file_type <= Element::RS_TYPE_FONT) 2189b3f7f69dSAidan Dodds printable_head_type_index = static_cast<Element::DataType>((file_type - Element::RS_TYPE_ELEMENT) + 2190b3f7f69dSAidan Dodds Element::RS_TYPE_MATRIX_2X2 + 1); 21912e920715SEwan Crawford 21922e920715SEwan Crawford const char *file_type_cstr = AllocationDetails::RsDataTypeToString[printable_head_type_index][0]; 21932e920715SEwan Crawford const char *target_type_cstr = AllocationDetails::RsDataTypeToString[printable_target_type_index][0]; 219455232f09SEwan Crawford 2195b3f7f69dSAidan Dodds strm.Printf("Warning: Mismatched Types - file '%s' type, allocation '%s' type", file_type_cstr, 2196b3f7f69dSAidan Dodds target_type_cstr); 219755232f09SEwan Crawford strm.EOL(); 219855232f09SEwan Crawford } 219955232f09SEwan Crawford 220026e52a70SEwan Crawford // Advance buffer past header 220126e52a70SEwan Crawford file_buffer = static_cast<uint8_t *>(file_buffer) + file_header->hdr_size; 220226e52a70SEwan Crawford 220355232f09SEwan Crawford // Calculate size of allocation data in file 220426e52a70SEwan Crawford size_t length = data_sp->GetByteSize() - file_header->hdr_size; 220555232f09SEwan Crawford 220655232f09SEwan Crawford // Check if the target allocation and file both have the same total data size. 2207b3f7f69dSAidan Dodds const uint32_t alloc_size = *alloc->size.get(); 220855232f09SEwan Crawford if (alloc_size != length) 220955232f09SEwan Crawford { 2210b3f7f69dSAidan Dodds strm.Printf("Warning: Mismatched allocation sizes - file 0x%" PRIx64 " bytes, allocation 0x%" PRIx32 " bytes", 2211eba832beSJason Molenda (uint64_t)length, alloc_size); 221255232f09SEwan Crawford strm.EOL(); 221355232f09SEwan Crawford length = alloc_size < length ? alloc_size : length; // Set length to copy to minimum 221455232f09SEwan Crawford } 221555232f09SEwan Crawford 221655232f09SEwan Crawford // Copy file data from our buffer into the target allocation. 221755232f09SEwan Crawford lldb::addr_t alloc_data = *alloc->data_ptr.get(); 221855232f09SEwan Crawford Error error; 221955232f09SEwan Crawford size_t bytes_written = GetProcess()->WriteMemory(alloc_data, file_buffer, length, error); 222055232f09SEwan Crawford if (!error.Success() || bytes_written != length) 222155232f09SEwan Crawford { 222255232f09SEwan Crawford strm.Printf("Error: Couldn't write data to allocation %s", error.AsCString()); 222355232f09SEwan Crawford strm.EOL(); 222455232f09SEwan Crawford return false; 222555232f09SEwan Crawford } 222655232f09SEwan Crawford 2227b3f7f69dSAidan Dodds strm.Printf("Contents of file '%s' read into allocation %" PRIu32, filename, alloc->id); 222855232f09SEwan Crawford strm.EOL(); 222955232f09SEwan Crawford 223055232f09SEwan Crawford return true; 223155232f09SEwan Crawford } 223255232f09SEwan Crawford 223326e52a70SEwan Crawford // Function takes as parameters a byte buffer, which will eventually be written to file as the element header, 223426e52a70SEwan Crawford // an offset into that buffer, and an Element that will be saved into the buffer at the parametrised offset. 223526e52a70SEwan Crawford // Return value is the new offset after writing the element into the buffer. 2236b3f7f69dSAidan Dodds // Elements are saved to the file as the ElementHeader struct followed by offsets to the structs of all the element's 2237b3f7f69dSAidan Dodds // children. 223826e52a70SEwan Crawford size_t 2239b3f7f69dSAidan Dodds RenderScriptRuntime::PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, size_t offset, 2240b3f7f69dSAidan Dodds const Element &elem) 224126e52a70SEwan Crawford { 224226e52a70SEwan Crawford // File struct for an element header with all the relevant details copied from elem. 224326e52a70SEwan Crawford // We assume members are valid already. 224426e52a70SEwan Crawford AllocationDetails::ElementHeader elem_header; 224526e52a70SEwan Crawford elem_header.type = *elem.type.get(); 224626e52a70SEwan Crawford elem_header.kind = *elem.type_kind.get(); 224726e52a70SEwan Crawford elem_header.element_size = *elem.datum_size.get(); 224826e52a70SEwan Crawford elem_header.vector_size = *elem.type_vec_size.get(); 224926e52a70SEwan Crawford elem_header.array_size = elem.array_size.isValid() ? *elem.array_size.get() : 0; 225026e52a70SEwan Crawford const size_t elem_header_size = sizeof(AllocationDetails::ElementHeader); 225126e52a70SEwan Crawford 225226e52a70SEwan Crawford // Copy struct into buffer and advance offset 2253b3f7f69dSAidan Dodds // We assume that header_buffer has been checked for nullptr before this method is called 225426e52a70SEwan Crawford memcpy(header_buffer.get() + offset, &elem_header, elem_header_size); 225526e52a70SEwan Crawford offset += elem_header_size; 225626e52a70SEwan Crawford 225726e52a70SEwan Crawford // Starting offset of child ElementHeader struct 225826e52a70SEwan Crawford size_t child_offset = offset + ((elem.children.size() + 1) * sizeof(uint32_t)); 225926e52a70SEwan Crawford for (const RenderScriptRuntime::Element &child : elem.children) 226026e52a70SEwan Crawford { 226126e52a70SEwan Crawford // Recursively populate the buffer with the element header structs of children. 226226e52a70SEwan Crawford // Then save the offsets where they were set after the parent element header. 226326e52a70SEwan Crawford memcpy(header_buffer.get() + offset, &child_offset, sizeof(uint32_t)); 226426e52a70SEwan Crawford offset += sizeof(uint32_t); 226526e52a70SEwan Crawford 226626e52a70SEwan Crawford child_offset = PopulateElementHeaders(header_buffer, child_offset, child); 226726e52a70SEwan Crawford } 226826e52a70SEwan Crawford 226926e52a70SEwan Crawford // Zero indicates no more children 227026e52a70SEwan Crawford memset(header_buffer.get() + offset, 0, sizeof(uint32_t)); 227126e52a70SEwan Crawford 227226e52a70SEwan Crawford return child_offset; 227326e52a70SEwan Crawford } 227426e52a70SEwan Crawford 2275b3f7f69dSAidan Dodds // Given an Element object this function returns the total size needed in the file header to store the element's 2276b3f7f69dSAidan Dodds // details. 227726e52a70SEwan Crawford // Taking into account the size of the element header struct, plus the offsets to all the element's children. 227826e52a70SEwan Crawford // Function is recursive so that the size of all ancestors is taken into account. 227926e52a70SEwan Crawford size_t 228026e52a70SEwan Crawford RenderScriptRuntime::CalculateElementHeaderSize(const Element &elem) 228126e52a70SEwan Crawford { 228226e52a70SEwan Crawford size_t size = (elem.children.size() + 1) * sizeof(uint32_t); // Offsets to children plus zero terminator 228326e52a70SEwan Crawford size += sizeof(AllocationDetails::ElementHeader); // Size of header struct with type details 228426e52a70SEwan Crawford 228526e52a70SEwan Crawford // Calculate recursively for all descendants 228626e52a70SEwan Crawford for (const Element &child : elem.children) 228726e52a70SEwan Crawford size += CalculateElementHeaderSize(child); 228826e52a70SEwan Crawford 228926e52a70SEwan Crawford return size; 229026e52a70SEwan Crawford } 229126e52a70SEwan Crawford 229255232f09SEwan Crawford // Function copies allocation contents into a binary file. 229355232f09SEwan Crawford // This file can then be loaded later into a different allocation. 229455232f09SEwan Crawford // There is a header, FileHeader, before the allocation data containing meta-data. 229555232f09SEwan Crawford bool 229655232f09SEwan Crawford RenderScriptRuntime::SaveAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr) 229755232f09SEwan Crawford { 229855232f09SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 229955232f09SEwan Crawford 230055232f09SEwan Crawford // Find allocation with the given id 230155232f09SEwan Crawford AllocationDetails *alloc = FindAllocByID(strm, alloc_id); 230255232f09SEwan Crawford if (!alloc) 230355232f09SEwan Crawford return false; 230455232f09SEwan Crawford 230555232f09SEwan Crawford if (log) 2306b3f7f69dSAidan Dodds log->Printf("%s - found allocation 0x%" PRIx64 ".", __FUNCTION__, *alloc->address.get()); 230755232f09SEwan Crawford 230855232f09SEwan Crawford // JIT all the allocation details 23098b59062aSEwan Crawford if (alloc->shouldRefresh()) 231055232f09SEwan Crawford { 231155232f09SEwan Crawford if (log) 2312b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info.", __FUNCTION__); 231355232f09SEwan Crawford 231455232f09SEwan Crawford if (!RefreshAllocation(alloc, frame_ptr)) 231555232f09SEwan Crawford { 231655232f09SEwan Crawford if (log) 2317b3f7f69dSAidan Dodds log->Printf("%s - couldn't JIT allocation details.", __FUNCTION__); 23184cfc9198SSylvestre Ledru return false; 231955232f09SEwan Crawford } 232055232f09SEwan Crawford } 232155232f09SEwan Crawford 2322b3f7f69dSAidan Dodds assert(alloc->data_ptr.isValid() && alloc->element.type.isValid() && alloc->element.type_vec_size.isValid() && 2323b3f7f69dSAidan Dodds alloc->element.datum_size.get() && alloc->element.type_kind.isValid() && alloc->dimension.isValid() && 2324b3f7f69dSAidan Dodds "Allocation information not available"); 232555232f09SEwan Crawford 232655232f09SEwan Crawford // Check we can create writable file 232755232f09SEwan Crawford FileSpec file_spec(filename, true); 232855232f09SEwan Crawford File file(file_spec, File::eOpenOptionWrite | File::eOpenOptionCanCreate | File::eOpenOptionTruncate); 232955232f09SEwan Crawford if (!file) 233055232f09SEwan Crawford { 233155232f09SEwan Crawford strm.Printf("Error: Failed to open '%s' for writing", filename); 233255232f09SEwan Crawford strm.EOL(); 233355232f09SEwan Crawford return false; 233455232f09SEwan Crawford } 233555232f09SEwan Crawford 233655232f09SEwan Crawford // Read allocation into buffer of heap memory 233755232f09SEwan Crawford const std::shared_ptr<uint8_t> buffer = GetAllocationData(alloc, frame_ptr); 233855232f09SEwan Crawford if (!buffer) 233955232f09SEwan Crawford { 234055232f09SEwan Crawford strm.Printf("Error: Couldn't read allocation data into buffer"); 234155232f09SEwan Crawford strm.EOL(); 234255232f09SEwan Crawford return false; 234355232f09SEwan Crawford } 234455232f09SEwan Crawford 234555232f09SEwan Crawford // Create the file header 234655232f09SEwan Crawford AllocationDetails::FileHeader head; 2347b3f7f69dSAidan Dodds memcpy(head.ident, "RSAD", 4); 23482d62328aSEwan Crawford head.dims[0] = static_cast<uint32_t>(alloc->dimension.get()->dim_1); 23492d62328aSEwan Crawford head.dims[1] = static_cast<uint32_t>(alloc->dimension.get()->dim_2); 23502d62328aSEwan Crawford head.dims[2] = static_cast<uint32_t>(alloc->dimension.get()->dim_3); 235126e52a70SEwan Crawford 235226e52a70SEwan Crawford const size_t element_header_size = CalculateElementHeaderSize(alloc->element); 235326e52a70SEwan Crawford assert((sizeof(AllocationDetails::FileHeader) + element_header_size) < UINT16_MAX && "Element header too large"); 235426e52a70SEwan Crawford head.hdr_size = static_cast<uint16_t>(sizeof(AllocationDetails::FileHeader) + element_header_size); 235555232f09SEwan Crawford 235655232f09SEwan Crawford // Write the file header 235755232f09SEwan Crawford size_t num_bytes = sizeof(AllocationDetails::FileHeader); 235826e52a70SEwan Crawford if (log) 2359b3f7f69dSAidan Dodds log->Printf("%s - writing File Header, 0x%" PRIx64 " bytes", __FUNCTION__, num_bytes); 236026e52a70SEwan Crawford 236126e52a70SEwan Crawford Error err = file.Write(&head, num_bytes); 236226e52a70SEwan Crawford if (!err.Success()) 236326e52a70SEwan Crawford { 236426e52a70SEwan Crawford strm.Printf("Error: '%s' when writing to file '%s'", err.AsCString(), filename); 236526e52a70SEwan Crawford strm.EOL(); 236626e52a70SEwan Crawford return false; 236726e52a70SEwan Crawford } 236826e52a70SEwan Crawford 236926e52a70SEwan Crawford // Create the headers describing the element type of the allocation. 237026e52a70SEwan Crawford std::shared_ptr<uint8_t> element_header_buffer(new uint8_t[element_header_size]); 237126e52a70SEwan Crawford if (element_header_buffer == nullptr) 237226e52a70SEwan Crawford { 2373b3f7f69dSAidan Dodds strm.Printf("Internal Error: Couldn't allocate %" PRIu64 " bytes on the heap", element_header_size); 237426e52a70SEwan Crawford strm.EOL(); 237526e52a70SEwan Crawford return false; 237626e52a70SEwan Crawford } 237726e52a70SEwan Crawford 237826e52a70SEwan Crawford PopulateElementHeaders(element_header_buffer, 0, alloc->element); 237926e52a70SEwan Crawford 238026e52a70SEwan Crawford // Write headers for allocation element type to file 238126e52a70SEwan Crawford num_bytes = element_header_size; 238226e52a70SEwan Crawford if (log) 2383b3f7f69dSAidan Dodds log->Printf("%s - writing element headers, 0x%" PRIx64 " bytes.", __FUNCTION__, num_bytes); 238426e52a70SEwan Crawford 238526e52a70SEwan Crawford err = file.Write(element_header_buffer.get(), num_bytes); 238655232f09SEwan Crawford if (!err.Success()) 238755232f09SEwan Crawford { 238855232f09SEwan Crawford strm.Printf("Error: '%s' when writing to file '%s'", err.AsCString(), filename); 238955232f09SEwan Crawford strm.EOL(); 239055232f09SEwan Crawford return false; 239155232f09SEwan Crawford } 239255232f09SEwan Crawford 239355232f09SEwan Crawford // Write allocation data to file 239455232f09SEwan Crawford num_bytes = static_cast<size_t>(*alloc->size.get()); 239555232f09SEwan Crawford if (log) 2396b3f7f69dSAidan Dodds log->Printf("%s - writing 0x%" PRIx64 " bytes", __FUNCTION__, num_bytes); 239755232f09SEwan Crawford 239855232f09SEwan Crawford err = file.Write(buffer.get(), num_bytes); 239955232f09SEwan Crawford if (!err.Success()) 240055232f09SEwan Crawford { 240155232f09SEwan Crawford strm.Printf("Error: '%s' when writing to file '%s'", err.AsCString(), filename); 240255232f09SEwan Crawford strm.EOL(); 240355232f09SEwan Crawford return false; 240455232f09SEwan Crawford } 240555232f09SEwan Crawford 240655232f09SEwan Crawford strm.Printf("Allocation written to file '%s'", filename); 240755232f09SEwan Crawford strm.EOL(); 240815f2bd95SEwan Crawford return true; 240915f2bd95SEwan Crawford } 241015f2bd95SEwan Crawford 24115ec532a9SColin Riley bool 24125ec532a9SColin Riley RenderScriptRuntime::LoadModule(const lldb::ModuleSP &module_sp) 24135ec532a9SColin Riley { 24144640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 24154640cde1SColin Riley 24165ec532a9SColin Riley if (module_sp) 24175ec532a9SColin Riley { 24185ec532a9SColin Riley for (const auto &rs_module : m_rsmodules) 24195ec532a9SColin Riley { 24204640cde1SColin Riley if (rs_module->m_module == module_sp) 24217dc7771cSEwan Crawford { 24227dc7771cSEwan Crawford // Check if the user has enabled automatically breaking on 24237dc7771cSEwan Crawford // all RS kernels. 24247dc7771cSEwan Crawford if (m_breakAllKernels) 24257dc7771cSEwan Crawford BreakOnModuleKernels(rs_module); 24267dc7771cSEwan Crawford 24275ec532a9SColin Riley return false; 24285ec532a9SColin Riley } 24297dc7771cSEwan Crawford } 2430ef20b08fSColin Riley bool module_loaded = false; 2431ef20b08fSColin Riley switch (GetModuleKind(module_sp)) 2432ef20b08fSColin Riley { 2433ef20b08fSColin Riley case eModuleKindKernelObj: 2434ef20b08fSColin Riley { 24354640cde1SColin Riley RSModuleDescriptorSP module_desc; 24364640cde1SColin Riley module_desc.reset(new RSModuleDescriptor(module_sp)); 24374640cde1SColin Riley if (module_desc->ParseRSInfo()) 24385ec532a9SColin Riley { 24395ec532a9SColin Riley m_rsmodules.push_back(module_desc); 2440ef20b08fSColin Riley module_loaded = true; 24415ec532a9SColin Riley } 24424640cde1SColin Riley if (module_loaded) 24434640cde1SColin Riley { 24444640cde1SColin Riley FixupScriptDetails(module_desc); 24454640cde1SColin Riley } 2446ef20b08fSColin Riley break; 2447ef20b08fSColin Riley } 2448ef20b08fSColin Riley case eModuleKindDriver: 24494640cde1SColin Riley { 24504640cde1SColin Riley if (!m_libRSDriver) 24514640cde1SColin Riley { 24524640cde1SColin Riley m_libRSDriver = module_sp; 24534640cde1SColin Riley LoadRuntimeHooks(m_libRSDriver, RenderScriptRuntime::eModuleKindDriver); 24544640cde1SColin Riley } 24554640cde1SColin Riley break; 24564640cde1SColin Riley } 2457ef20b08fSColin Riley case eModuleKindImpl: 24584640cde1SColin Riley { 24594640cde1SColin Riley m_libRSCpuRef = module_sp; 24604640cde1SColin Riley break; 24614640cde1SColin Riley } 2462ef20b08fSColin Riley case eModuleKindLibRS: 24634640cde1SColin Riley { 24644640cde1SColin Riley if (!m_libRS) 24654640cde1SColin Riley { 24664640cde1SColin Riley m_libRS = module_sp; 24674640cde1SColin Riley static ConstString gDbgPresentStr("gDebuggerPresent"); 2468b3f7f69dSAidan Dodds const Symbol *debug_present = 2469b3f7f69dSAidan Dodds m_libRS->FindFirstSymbolWithNameAndType(gDbgPresentStr, eSymbolTypeData); 24704640cde1SColin Riley if (debug_present) 24714640cde1SColin Riley { 24724640cde1SColin Riley Error error; 24734640cde1SColin Riley uint32_t flag = 0x00000001U; 24744640cde1SColin Riley Target &target = GetProcess()->GetTarget(); 2475358cf1eaSGreg Clayton addr_t addr = debug_present->GetLoadAddress(&target); 24764640cde1SColin Riley GetProcess()->WriteMemory(addr, &flag, sizeof(flag), error); 24774640cde1SColin Riley if (error.Success()) 24784640cde1SColin Riley { 24794640cde1SColin Riley if (log) 2480b3f7f69dSAidan Dodds log->Printf("%s - debugger present flag set on debugee.", __FUNCTION__); 24814640cde1SColin Riley 24824640cde1SColin Riley m_debuggerPresentFlagged = true; 24834640cde1SColin Riley } 24844640cde1SColin Riley else if (log) 24854640cde1SColin Riley { 2486b3f7f69dSAidan Dodds log->Printf("%s - error writing debugger present flags '%s' ", __FUNCTION__, 2487b3f7f69dSAidan Dodds error.AsCString()); 24884640cde1SColin Riley } 24894640cde1SColin Riley } 24904640cde1SColin Riley else if (log) 24914640cde1SColin Riley { 2492b3f7f69dSAidan Dodds log->Printf("%s - error writing debugger present flags - symbol not found", __FUNCTION__); 24934640cde1SColin Riley } 24944640cde1SColin Riley } 24954640cde1SColin Riley break; 24964640cde1SColin Riley } 2497ef20b08fSColin Riley default: 2498ef20b08fSColin Riley break; 2499ef20b08fSColin Riley } 2500ef20b08fSColin Riley if (module_loaded) 2501ef20b08fSColin Riley Update(); 2502ef20b08fSColin Riley return module_loaded; 25035ec532a9SColin Riley } 25045ec532a9SColin Riley return false; 25055ec532a9SColin Riley } 25065ec532a9SColin Riley 2507ef20b08fSColin Riley void 2508ef20b08fSColin Riley RenderScriptRuntime::Update() 2509ef20b08fSColin Riley { 2510ef20b08fSColin Riley if (m_rsmodules.size() > 0) 2511ef20b08fSColin Riley { 2512ef20b08fSColin Riley if (!m_initiated) 2513ef20b08fSColin Riley { 2514ef20b08fSColin Riley Initiate(); 2515ef20b08fSColin Riley } 2516ef20b08fSColin Riley } 2517ef20b08fSColin Riley } 2518ef20b08fSColin Riley 25195ec532a9SColin Riley // The maximum line length of an .rs.info packet 25205ec532a9SColin Riley #define MAXLINE 500 25215ec532a9SColin Riley 25225ec532a9SColin Riley // The .rs.info symbol in renderscript modules contains a string which needs to be parsed. 25235ec532a9SColin Riley // The string is basic and is parsed on a line by line basis. 25245ec532a9SColin Riley bool 25255ec532a9SColin Riley RSModuleDescriptor::ParseRSInfo() 25265ec532a9SColin Riley { 25275ec532a9SColin Riley const Symbol *info_sym = m_module->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData); 25285ec532a9SColin Riley if (info_sym) 25295ec532a9SColin Riley { 2530358cf1eaSGreg Clayton const addr_t addr = info_sym->GetAddressRef().GetFileAddress(); 25315ec532a9SColin Riley const addr_t size = info_sym->GetByteSize(); 25325ec532a9SColin Riley const FileSpec fs = m_module->GetFileSpec(); 25335ec532a9SColin Riley 25345ec532a9SColin Riley DataBufferSP buffer = fs.ReadFileContents(addr, size); 25355ec532a9SColin Riley 25365ec532a9SColin Riley if (!buffer) 25375ec532a9SColin Riley return false; 25385ec532a9SColin Riley 25395ec532a9SColin Riley std::string info((const char *)buffer->GetBytes()); 25405ec532a9SColin Riley 25415ec532a9SColin Riley std::vector<std::string> info_lines; 2542e8433cc1SBruce Mitchener size_t lpos = info.find('\n'); 25435ec532a9SColin Riley while (lpos != std::string::npos) 25445ec532a9SColin Riley { 25455ec532a9SColin Riley info_lines.push_back(info.substr(0, lpos)); 25465ec532a9SColin Riley info = info.substr(lpos + 1); 2547e8433cc1SBruce Mitchener lpos = info.find('\n'); 25485ec532a9SColin Riley } 25495ec532a9SColin Riley size_t offset = 0; 25505ec532a9SColin Riley while (offset < info_lines.size()) 25515ec532a9SColin Riley { 25525ec532a9SColin Riley std::string line = info_lines[offset]; 25535ec532a9SColin Riley // Parse directives 25545ec532a9SColin Riley uint32_t numDefns = 0; 2555b3f7f69dSAidan Dodds if (sscanf(line.c_str(), "exportVarCount: %" PRIu32 "", &numDefns) == 1) 25565ec532a9SColin Riley { 25575ec532a9SColin Riley while (numDefns--) 25584640cde1SColin Riley m_globals.push_back(RSGlobalDescriptor(this, info_lines[++offset].c_str())); 25595ec532a9SColin Riley } 2560b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "exportFuncCount: %" PRIu32 "", &numDefns) == 1) 25615ec532a9SColin Riley { 25625ec532a9SColin Riley } 2563b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "exportForEachCount: %" PRIu32 "", &numDefns) == 1) 25645ec532a9SColin Riley { 25655ec532a9SColin Riley char name[MAXLINE]; 25665ec532a9SColin Riley while (numDefns--) 25675ec532a9SColin Riley { 25685ec532a9SColin Riley uint32_t slot = 0; 25695ec532a9SColin Riley name[0] = '\0'; 2570b3f7f69dSAidan Dodds if (sscanf(info_lines[++offset].c_str(), "%" PRIu32 " - %s", &slot, &name[0]) == 2) 25715ec532a9SColin Riley { 25724640cde1SColin Riley m_kernels.push_back(RSKernelDescriptor(this, name, slot)); 25734640cde1SColin Riley } 25744640cde1SColin Riley } 25754640cde1SColin Riley } 2576b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "pragmaCount: %" PRIu32 "", &numDefns) == 1) 25774640cde1SColin Riley { 25784640cde1SColin Riley char name[MAXLINE]; 25794640cde1SColin Riley char value[MAXLINE]; 25804640cde1SColin Riley while (numDefns--) 25814640cde1SColin Riley { 25824640cde1SColin Riley name[0] = '\0'; 25834640cde1SColin Riley value[0] = '\0'; 2584b3f7f69dSAidan Dodds if (sscanf(info_lines[++offset].c_str(), "%s - %s", &name[0], &value[0]) != 0 && (name[0] != '\0')) 25854640cde1SColin Riley { 25864640cde1SColin Riley m_pragmas[std::string(name)] = value; 25875ec532a9SColin Riley } 25885ec532a9SColin Riley } 25895ec532a9SColin Riley } 2590b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "objectSlotCount: %" PRIu32 "", &numDefns) == 1) 25915ec532a9SColin Riley { 25925ec532a9SColin Riley } 25935ec532a9SColin Riley 25945ec532a9SColin Riley offset++; 25955ec532a9SColin Riley } 25965ec532a9SColin Riley return m_kernels.size() > 0; 25975ec532a9SColin Riley } 25985ec532a9SColin Riley return false; 25995ec532a9SColin Riley } 26005ec532a9SColin Riley 26015ec532a9SColin Riley void 26024640cde1SColin Riley RenderScriptRuntime::Status(Stream &strm) const 26034640cde1SColin Riley { 26044640cde1SColin Riley if (m_libRS) 26054640cde1SColin Riley { 26064640cde1SColin Riley strm.Printf("Runtime Library discovered."); 26074640cde1SColin Riley strm.EOL(); 26084640cde1SColin Riley } 26094640cde1SColin Riley if (m_libRSDriver) 26104640cde1SColin Riley { 26114640cde1SColin Riley strm.Printf("Runtime Driver discovered."); 26124640cde1SColin Riley strm.EOL(); 26134640cde1SColin Riley } 26144640cde1SColin Riley if (m_libRSCpuRef) 26154640cde1SColin Riley { 26164640cde1SColin Riley strm.Printf("CPU Reference Implementation discovered."); 26174640cde1SColin Riley strm.EOL(); 26184640cde1SColin Riley } 26194640cde1SColin Riley 26204640cde1SColin Riley if (m_runtimeHooks.size()) 26214640cde1SColin Riley { 26224640cde1SColin Riley strm.Printf("Runtime functions hooked:"); 26234640cde1SColin Riley strm.EOL(); 26244640cde1SColin Riley for (auto b : m_runtimeHooks) 26254640cde1SColin Riley { 26264640cde1SColin Riley strm.Indent(b.second->defn->name); 26274640cde1SColin Riley strm.EOL(); 26284640cde1SColin Riley } 26294640cde1SColin Riley } 26304640cde1SColin Riley else 26314640cde1SColin Riley { 26324640cde1SColin Riley strm.Printf("Runtime is not hooked."); 26334640cde1SColin Riley strm.EOL(); 26344640cde1SColin Riley } 26354640cde1SColin Riley } 26364640cde1SColin Riley 26374640cde1SColin Riley void 26384640cde1SColin Riley RenderScriptRuntime::DumpContexts(Stream &strm) const 26394640cde1SColin Riley { 26404640cde1SColin Riley strm.Printf("Inferred RenderScript Contexts:"); 26414640cde1SColin Riley strm.EOL(); 26424640cde1SColin Riley strm.IndentMore(); 26434640cde1SColin Riley 26444640cde1SColin Riley std::map<addr_t, uint64_t> contextReferences; 26454640cde1SColin Riley 264678f339d1SEwan Crawford // Iterate over all of the currently discovered scripts. 264778f339d1SEwan Crawford // Note: We cant push or pop from m_scripts inside this loop or it may invalidate script. 26484640cde1SColin Riley for (const auto &script : m_scripts) 26494640cde1SColin Riley { 265078f339d1SEwan Crawford if (!script->context.isValid()) 265178f339d1SEwan Crawford continue; 265278f339d1SEwan Crawford lldb::addr_t context = *script->context; 265378f339d1SEwan Crawford 265478f339d1SEwan Crawford if (contextReferences.find(context) != contextReferences.end()) 26554640cde1SColin Riley { 265678f339d1SEwan Crawford contextReferences[context]++; 26574640cde1SColin Riley } 26584640cde1SColin Riley else 26594640cde1SColin Riley { 266078f339d1SEwan Crawford contextReferences[context] = 1; 26614640cde1SColin Riley } 26624640cde1SColin Riley } 26634640cde1SColin Riley 26644640cde1SColin Riley for (const auto &cRef : contextReferences) 26654640cde1SColin Riley { 26664640cde1SColin Riley strm.Printf("Context 0x%" PRIx64 ": %" PRIu64 " script instances", cRef.first, cRef.second); 26674640cde1SColin Riley strm.EOL(); 26684640cde1SColin Riley } 26694640cde1SColin Riley strm.IndentLess(); 26704640cde1SColin Riley } 26714640cde1SColin Riley 26724640cde1SColin Riley void 26734640cde1SColin Riley RenderScriptRuntime::DumpKernels(Stream &strm) const 26744640cde1SColin Riley { 26754640cde1SColin Riley strm.Printf("RenderScript Kernels:"); 26764640cde1SColin Riley strm.EOL(); 26774640cde1SColin Riley strm.IndentMore(); 26784640cde1SColin Riley for (const auto &module : m_rsmodules) 26794640cde1SColin Riley { 26804640cde1SColin Riley strm.Printf("Resource '%s':", module->m_resname.c_str()); 26814640cde1SColin Riley strm.EOL(); 26824640cde1SColin Riley for (const auto &kernel : module->m_kernels) 26834640cde1SColin Riley { 26844640cde1SColin Riley strm.Indent(kernel.m_name.AsCString()); 26854640cde1SColin Riley strm.EOL(); 26864640cde1SColin Riley } 26874640cde1SColin Riley } 26884640cde1SColin Riley strm.IndentLess(); 26894640cde1SColin Riley } 26904640cde1SColin Riley 2691a0f08674SEwan Crawford RenderScriptRuntime::AllocationDetails * 2692a0f08674SEwan Crawford RenderScriptRuntime::FindAllocByID(Stream &strm, const uint32_t alloc_id) 2693a0f08674SEwan Crawford { 2694a0f08674SEwan Crawford AllocationDetails *alloc = nullptr; 2695a0f08674SEwan Crawford 2696a0f08674SEwan Crawford // See if we can find allocation using id as an index; 2697b3f7f69dSAidan Dodds if (alloc_id <= m_allocations.size() && alloc_id != 0 && m_allocations[alloc_id - 1]->id == alloc_id) 2698a0f08674SEwan Crawford { 2699a0f08674SEwan Crawford alloc = m_allocations[alloc_id - 1].get(); 2700a0f08674SEwan Crawford return alloc; 2701a0f08674SEwan Crawford } 2702a0f08674SEwan Crawford 2703a0f08674SEwan Crawford // Fallback to searching 2704a0f08674SEwan Crawford for (const auto &a : m_allocations) 2705a0f08674SEwan Crawford { 2706a0f08674SEwan Crawford if (a->id == alloc_id) 2707a0f08674SEwan Crawford { 2708a0f08674SEwan Crawford alloc = a.get(); 2709a0f08674SEwan Crawford break; 2710a0f08674SEwan Crawford } 2711a0f08674SEwan Crawford } 2712a0f08674SEwan Crawford 2713a0f08674SEwan Crawford if (alloc == nullptr) 2714a0f08674SEwan Crawford { 2715b3f7f69dSAidan Dodds strm.Printf("Error: Couldn't find allocation with id matching %" PRIu32, alloc_id); 2716a0f08674SEwan Crawford strm.EOL(); 2717a0f08674SEwan Crawford } 2718a0f08674SEwan Crawford 2719a0f08674SEwan Crawford return alloc; 2720a0f08674SEwan Crawford } 2721a0f08674SEwan Crawford 2722a0f08674SEwan Crawford // Prints the contents of an allocation to the output stream, which may be a file 2723a0f08674SEwan Crawford bool 2724a0f08674SEwan Crawford RenderScriptRuntime::DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id) 2725a0f08674SEwan Crawford { 2726a0f08674SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 2727a0f08674SEwan Crawford 2728a0f08674SEwan Crawford // Check we can find the desired allocation 2729a0f08674SEwan Crawford AllocationDetails *alloc = FindAllocByID(strm, id); 2730a0f08674SEwan Crawford if (!alloc) 2731a0f08674SEwan Crawford return false; // FindAllocByID() will print error message for us here 2732a0f08674SEwan Crawford 2733a0f08674SEwan Crawford if (log) 2734b3f7f69dSAidan Dodds log->Printf("%s - found allocation 0x%" PRIx64, __FUNCTION__, *alloc->address.get()); 2735a0f08674SEwan Crawford 2736a0f08674SEwan Crawford // Check we have information about the allocation, if not calculate it 27378b59062aSEwan Crawford if (alloc->shouldRefresh()) 2738a0f08674SEwan Crawford { 2739a0f08674SEwan Crawford if (log) 2740b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info.", __FUNCTION__); 2741a0f08674SEwan Crawford 2742a0f08674SEwan Crawford // JIT all the allocation information 2743a0f08674SEwan Crawford if (!RefreshAllocation(alloc, frame_ptr)) 2744a0f08674SEwan Crawford { 2745a0f08674SEwan Crawford strm.Printf("Error: Couldn't JIT allocation details"); 2746a0f08674SEwan Crawford strm.EOL(); 2747a0f08674SEwan Crawford return false; 2748a0f08674SEwan Crawford } 2749a0f08674SEwan Crawford } 2750a0f08674SEwan Crawford 2751a0f08674SEwan Crawford // Establish format and size of each data element 2752b3f7f69dSAidan Dodds const uint32_t vec_size = *alloc->element.type_vec_size.get(); 27538b244e21SEwan Crawford const Element::DataType type = *alloc->element.type.get(); 2754a0f08674SEwan Crawford 2755b3f7f69dSAidan Dodds assert(type >= Element::RS_TYPE_NONE && type <= Element::RS_TYPE_FONT && "Invalid allocation type"); 2756a0f08674SEwan Crawford 27572e920715SEwan Crawford lldb::Format format; 27582e920715SEwan Crawford if (type >= Element::RS_TYPE_ELEMENT) 27592e920715SEwan Crawford format = eFormatHex; 27602e920715SEwan Crawford else 27612e920715SEwan Crawford format = vec_size == 1 ? static_cast<lldb::Format>(AllocationDetails::RSTypeToFormat[type][eFormatSingle]) 2762a0f08674SEwan Crawford : static_cast<lldb::Format>(AllocationDetails::RSTypeToFormat[type][eFormatVector]); 2763a0f08674SEwan Crawford 2764b3f7f69dSAidan Dodds const uint32_t data_size = *alloc->element.datum_size.get(); 2765a0f08674SEwan Crawford 2766a0f08674SEwan Crawford if (log) 2767b3f7f69dSAidan Dodds log->Printf("%s - element size %" PRIu32 " bytes, including padding", __FUNCTION__, data_size); 2768a0f08674SEwan Crawford 276955232f09SEwan Crawford // Allocate a buffer to copy data into 277055232f09SEwan Crawford std::shared_ptr<uint8_t> buffer = GetAllocationData(alloc, frame_ptr); 277155232f09SEwan Crawford if (!buffer) 277255232f09SEwan Crawford { 27732e920715SEwan Crawford strm.Printf("Error: Couldn't read allocation data"); 277455232f09SEwan Crawford strm.EOL(); 277555232f09SEwan Crawford return false; 277655232f09SEwan Crawford } 277755232f09SEwan Crawford 2778a0f08674SEwan Crawford // Calculate stride between rows as there may be padding at end of rows since 2779a0f08674SEwan Crawford // allocated memory is 16-byte aligned 2780a0f08674SEwan Crawford if (!alloc->stride.isValid()) 2781a0f08674SEwan Crawford { 2782a0f08674SEwan Crawford if (alloc->dimension.get()->dim_2 == 0) // We only have one dimension 2783a0f08674SEwan Crawford alloc->stride = 0; 2784a0f08674SEwan Crawford else if (!JITAllocationStride(alloc, frame_ptr)) 2785a0f08674SEwan Crawford { 2786a0f08674SEwan Crawford strm.Printf("Error: Couldn't calculate allocation row stride"); 2787a0f08674SEwan Crawford strm.EOL(); 2788a0f08674SEwan Crawford return false; 2789a0f08674SEwan Crawford } 2790a0f08674SEwan Crawford } 2791b3f7f69dSAidan Dodds const uint32_t stride = *alloc->stride.get(); 2792b3f7f69dSAidan Dodds const uint32_t size = *alloc->size.get(); // Size of whole allocation 2793b3f7f69dSAidan Dodds const uint32_t padding = alloc->element.padding.isValid() ? *alloc->element.padding.get() : 0; 2794a0f08674SEwan Crawford if (log) 2795b3f7f69dSAidan Dodds log->Printf("%s - stride %" PRIu32 " bytes, size %" PRIu32 " bytes, padding %" PRIu32, 2796b3f7f69dSAidan Dodds __FUNCTION__, stride, size, padding); 2797a0f08674SEwan Crawford 2798a0f08674SEwan Crawford // Find dimensions used to index loops, so need to be non-zero 2799b3f7f69dSAidan Dodds uint32_t dim_x = alloc->dimension.get()->dim_1; 2800a0f08674SEwan Crawford dim_x = dim_x == 0 ? 1 : dim_x; 2801a0f08674SEwan Crawford 2802b3f7f69dSAidan Dodds uint32_t dim_y = alloc->dimension.get()->dim_2; 2803a0f08674SEwan Crawford dim_y = dim_y == 0 ? 1 : dim_y; 2804a0f08674SEwan Crawford 2805b3f7f69dSAidan Dodds uint32_t dim_z = alloc->dimension.get()->dim_3; 2806a0f08674SEwan Crawford dim_z = dim_z == 0 ? 1 : dim_z; 2807a0f08674SEwan Crawford 280855232f09SEwan Crawford // Use data extractor to format output 280955232f09SEwan Crawford const uint32_t archByteSize = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize(); 281055232f09SEwan Crawford DataExtractor alloc_data(buffer.get(), size, GetProcess()->GetByteOrder(), archByteSize); 281155232f09SEwan Crawford 2812b3f7f69dSAidan Dodds uint32_t offset = 0; // Offset in buffer to next element to be printed 2813b3f7f69dSAidan Dodds uint32_t prev_row = 0; // Offset to the start of the previous row 2814a0f08674SEwan Crawford 2815a0f08674SEwan Crawford // Iterate over allocation dimensions, printing results to user 2816a0f08674SEwan Crawford strm.Printf("Data (X, Y, Z):"); 2817b3f7f69dSAidan Dodds for (uint32_t z = 0; z < dim_z; ++z) 2818a0f08674SEwan Crawford { 2819b3f7f69dSAidan Dodds for (uint32_t y = 0; y < dim_y; ++y) 2820a0f08674SEwan Crawford { 2821a0f08674SEwan Crawford // Use stride to index start of next row. 2822a0f08674SEwan Crawford if (!(y == 0 && z == 0)) 2823a0f08674SEwan Crawford offset = prev_row + stride; 2824a0f08674SEwan Crawford prev_row = offset; 2825a0f08674SEwan Crawford 2826a0f08674SEwan Crawford // Print each element in the row individually 2827b3f7f69dSAidan Dodds for (uint32_t x = 0; x < dim_x; ++x) 2828a0f08674SEwan Crawford { 2829b3f7f69dSAidan Dodds strm.Printf("\n(%" PRIu32 ", %" PRIu32 ", %" PRIu32 ") = ", x, y, z); 28308b244e21SEwan Crawford if ((type == Element::RS_TYPE_NONE) && (alloc->element.children.size() > 0) && 2831fe06b5adSAdrian McCarthy (alloc->element.type_name != Element::GetFallbackStructName())) 28328b244e21SEwan Crawford { 28338b244e21SEwan Crawford // Here we are dumping an Element of struct type. 28348b244e21SEwan Crawford // This is done using expression evaluation with the name of the struct type and pointer to element. 28358b244e21SEwan Crawford 28368b244e21SEwan Crawford // Don't print the name of the resulting expression, since this will be '$[0-9]+' 28378b244e21SEwan Crawford DumpValueObjectOptions expr_options; 28388b244e21SEwan Crawford expr_options.SetHideName(true); 28398b244e21SEwan Crawford 28408b244e21SEwan Crawford // Setup expression as derefrencing a pointer cast to element address. 2841*ea0636b5SEwan Crawford char expr_char_buffer[jit_max_expr_size]; 2842*ea0636b5SEwan Crawford int chars_written = snprintf(expr_char_buffer, jit_max_expr_size, "*(%s*) 0x%" PRIx64, 28438b244e21SEwan Crawford alloc->element.type_name.AsCString(), *alloc->data_ptr.get() + offset); 28448b244e21SEwan Crawford 2845*ea0636b5SEwan Crawford if (chars_written < 0 || chars_written >= jit_max_expr_size) 28468b244e21SEwan Crawford { 28478b244e21SEwan Crawford if (log) 2848b3f7f69dSAidan Dodds log->Printf("%s - error in snprintf().", __FUNCTION__); 28498b244e21SEwan Crawford continue; 28508b244e21SEwan Crawford } 28518b244e21SEwan Crawford 28528b244e21SEwan Crawford // Evaluate expression 28538b244e21SEwan Crawford ValueObjectSP expr_result; 28548b244e21SEwan Crawford GetProcess()->GetTarget().EvaluateExpression(expr_char_buffer, frame_ptr, expr_result); 28558b244e21SEwan Crawford 28568b244e21SEwan Crawford // Print the results to our stream. 28578b244e21SEwan Crawford expr_result->Dump(strm, expr_options); 28588b244e21SEwan Crawford } 28598b244e21SEwan Crawford else 28608b244e21SEwan Crawford { 28618b244e21SEwan Crawford alloc_data.Dump(&strm, offset, format, data_size - padding, 1, 1, LLDB_INVALID_ADDRESS, 0, 0); 28628b244e21SEwan Crawford } 28638b244e21SEwan Crawford offset += data_size; 2864a0f08674SEwan Crawford } 2865a0f08674SEwan Crawford } 2866a0f08674SEwan Crawford } 2867a0f08674SEwan Crawford strm.EOL(); 2868a0f08674SEwan Crawford 2869a0f08674SEwan Crawford return true; 2870a0f08674SEwan Crawford } 2871a0f08674SEwan Crawford 28720d2bfcfbSEwan Crawford // Function recalculates all our cached information about allocations by jitting the 28730d2bfcfbSEwan Crawford // RS runtime regarding each allocation we know about. 28740d2bfcfbSEwan Crawford // Returns true if all allocations could be recomputed, false otherwise. 28750d2bfcfbSEwan Crawford bool 28760d2bfcfbSEwan Crawford RenderScriptRuntime::RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr) 28770d2bfcfbSEwan Crawford { 28780d2bfcfbSEwan Crawford bool success = true; 28790d2bfcfbSEwan Crawford for (auto &alloc : m_allocations) 28800d2bfcfbSEwan Crawford { 28810d2bfcfbSEwan Crawford // JIT current allocation information 28820d2bfcfbSEwan Crawford if (!RefreshAllocation(alloc.get(), frame_ptr)) 28830d2bfcfbSEwan Crawford { 28840d2bfcfbSEwan Crawford strm.Printf("Error: Couldn't evaluate details for allocation %" PRIu32 "\n", alloc->id); 28850d2bfcfbSEwan Crawford success = false; 28860d2bfcfbSEwan Crawford } 28870d2bfcfbSEwan Crawford } 28880d2bfcfbSEwan Crawford 28890d2bfcfbSEwan Crawford if (success) 28900d2bfcfbSEwan Crawford strm.Printf("All allocations successfully recomputed"); 28910d2bfcfbSEwan Crawford strm.EOL(); 28920d2bfcfbSEwan Crawford 28930d2bfcfbSEwan Crawford return success; 28940d2bfcfbSEwan Crawford } 28950d2bfcfbSEwan Crawford 2896b649b005SEwan Crawford // Prints information regarding currently loaded allocations. 289715f2bd95SEwan Crawford // These details are gathered by jitting the runtime, which has as latency. 2898b649b005SEwan Crawford // Index parameter specifies a single allocation ID to print, or a zero value to print them all 289915f2bd95SEwan Crawford void 2900b649b005SEwan Crawford RenderScriptRuntime::ListAllocations(Stream &strm, StackFrame *frame_ptr, const uint32_t index) 290115f2bd95SEwan Crawford { 290215f2bd95SEwan Crawford strm.Printf("RenderScript Allocations:"); 290315f2bd95SEwan Crawford strm.EOL(); 290415f2bd95SEwan Crawford strm.IndentMore(); 290515f2bd95SEwan Crawford 290615f2bd95SEwan Crawford for (auto &alloc : m_allocations) 290715f2bd95SEwan Crawford { 2908b649b005SEwan Crawford // index will only be zero if we want to print all allocations 2909b649b005SEwan Crawford if (index != 0 && index != alloc->id) 2910b649b005SEwan Crawford continue; 291115f2bd95SEwan Crawford 291215f2bd95SEwan Crawford // JIT current allocation information 2913b649b005SEwan Crawford if (alloc->shouldRefresh() && !RefreshAllocation(alloc.get(), frame_ptr)) 291415f2bd95SEwan Crawford { 2915b3f7f69dSAidan Dodds strm.Printf("Error: Couldn't evaluate details for allocation %" PRIu32, alloc->id); 2916b3f7f69dSAidan Dodds strm.EOL(); 291715f2bd95SEwan Crawford continue; 291815f2bd95SEwan Crawford } 291915f2bd95SEwan Crawford 2920b3f7f69dSAidan Dodds strm.Printf("%" PRIu32 ":", alloc->id); 2921b3f7f69dSAidan Dodds strm.EOL(); 292215f2bd95SEwan Crawford strm.IndentMore(); 292315f2bd95SEwan Crawford 292415f2bd95SEwan Crawford strm.Indent("Context: "); 292515f2bd95SEwan Crawford if (!alloc->context.isValid()) 292615f2bd95SEwan Crawford strm.Printf("unknown\n"); 292715f2bd95SEwan Crawford else 292815f2bd95SEwan Crawford strm.Printf("0x%" PRIx64 "\n", *alloc->context.get()); 292915f2bd95SEwan Crawford 293015f2bd95SEwan Crawford strm.Indent("Address: "); 293115f2bd95SEwan Crawford if (!alloc->address.isValid()) 293215f2bd95SEwan Crawford strm.Printf("unknown\n"); 293315f2bd95SEwan Crawford else 293415f2bd95SEwan Crawford strm.Printf("0x%" PRIx64 "\n", *alloc->address.get()); 293515f2bd95SEwan Crawford 293615f2bd95SEwan Crawford strm.Indent("Data pointer: "); 293715f2bd95SEwan Crawford if (!alloc->data_ptr.isValid()) 293815f2bd95SEwan Crawford strm.Printf("unknown\n"); 293915f2bd95SEwan Crawford else 294015f2bd95SEwan Crawford strm.Printf("0x%" PRIx64 "\n", *alloc->data_ptr.get()); 294115f2bd95SEwan Crawford 294215f2bd95SEwan Crawford strm.Indent("Dimensions: "); 294315f2bd95SEwan Crawford if (!alloc->dimension.isValid()) 294415f2bd95SEwan Crawford strm.Printf("unknown\n"); 294515f2bd95SEwan Crawford else 2946b3f7f69dSAidan Dodds strm.Printf("(%" PRId32 ", %" PRId32 ", %" PRId32 ")\n", 2947b3f7f69dSAidan Dodds alloc->dimension.get()->dim_1, alloc->dimension.get()->dim_2, alloc->dimension.get()->dim_3); 294815f2bd95SEwan Crawford 294915f2bd95SEwan Crawford strm.Indent("Data Type: "); 29508b244e21SEwan Crawford if (!alloc->element.type.isValid() || !alloc->element.type_vec_size.isValid()) 295115f2bd95SEwan Crawford strm.Printf("unknown\n"); 295215f2bd95SEwan Crawford else 295315f2bd95SEwan Crawford { 29548b244e21SEwan Crawford const int vector_size = *alloc->element.type_vec_size.get(); 29552e920715SEwan Crawford Element::DataType type = *alloc->element.type.get(); 295615f2bd95SEwan Crawford 29578b244e21SEwan Crawford if (!alloc->element.type_name.IsEmpty()) 29588b244e21SEwan Crawford strm.Printf("%s\n", alloc->element.type_name.AsCString()); 29592e920715SEwan Crawford else 29602e920715SEwan Crawford { 29612e920715SEwan Crawford // Enum value isn't monotonous, so doesn't always index RsDataTypeToString array 29622e920715SEwan Crawford if (type >= Element::RS_TYPE_ELEMENT && type <= Element::RS_TYPE_FONT) 2963b3f7f69dSAidan Dodds type = static_cast<Element::DataType>((type - Element::RS_TYPE_ELEMENT) + 2964b3f7f69dSAidan Dodds Element::RS_TYPE_MATRIX_2X2 + 1); 29652e920715SEwan Crawford 2966b3f7f69dSAidan Dodds if (type >= (sizeof(AllocationDetails::RsDataTypeToString) / 2967b3f7f69dSAidan Dodds sizeof(AllocationDetails::RsDataTypeToString[0])) || 2968b3f7f69dSAidan Dodds vector_size > 4 || vector_size < 1) 296915f2bd95SEwan Crawford strm.Printf("invalid type\n"); 297015f2bd95SEwan Crawford else 2971b3f7f69dSAidan Dodds strm.Printf("%s\n", AllocationDetails::RsDataTypeToString[static_cast<uint32_t>(type)] 2972b3f7f69dSAidan Dodds [vector_size - 1]); 297315f2bd95SEwan Crawford } 29742e920715SEwan Crawford } 297515f2bd95SEwan Crawford 297615f2bd95SEwan Crawford strm.Indent("Data Kind: "); 29778b244e21SEwan Crawford if (!alloc->element.type_kind.isValid()) 297815f2bd95SEwan Crawford strm.Printf("unknown\n"); 297915f2bd95SEwan Crawford else 298015f2bd95SEwan Crawford { 29818b244e21SEwan Crawford const Element::DataKind kind = *alloc->element.type_kind.get(); 29828b244e21SEwan Crawford if (kind < Element::RS_KIND_USER || kind > Element::RS_KIND_PIXEL_YUV) 298315f2bd95SEwan Crawford strm.Printf("invalid kind\n"); 298415f2bd95SEwan Crawford else 2985b3f7f69dSAidan Dodds strm.Printf("%s\n", AllocationDetails::RsDataKindToString[static_cast<uint32_t>(kind)]); 298615f2bd95SEwan Crawford } 298715f2bd95SEwan Crawford 298815f2bd95SEwan Crawford strm.EOL(); 298915f2bd95SEwan Crawford strm.IndentLess(); 299015f2bd95SEwan Crawford } 299115f2bd95SEwan Crawford strm.IndentLess(); 299215f2bd95SEwan Crawford } 299315f2bd95SEwan Crawford 29947dc7771cSEwan Crawford // Set breakpoints on every kernel found in RS module 29957dc7771cSEwan Crawford void 29967dc7771cSEwan Crawford RenderScriptRuntime::BreakOnModuleKernels(const RSModuleDescriptorSP rsmodule_sp) 29977dc7771cSEwan Crawford { 29987dc7771cSEwan Crawford for (const auto &kernel : rsmodule_sp->m_kernels) 29997dc7771cSEwan Crawford { 30007dc7771cSEwan Crawford // Don't set breakpoint on 'root' kernel 30017dc7771cSEwan Crawford if (strcmp(kernel.m_name.AsCString(), "root") == 0) 30027dc7771cSEwan Crawford continue; 30037dc7771cSEwan Crawford 30047dc7771cSEwan Crawford CreateKernelBreakpoint(kernel.m_name); 30057dc7771cSEwan Crawford } 30067dc7771cSEwan Crawford } 30077dc7771cSEwan Crawford 30087dc7771cSEwan Crawford // Method is internally called by the 'kernel breakpoint all' command to 30097dc7771cSEwan Crawford // enable or disable breaking on all kernels. 30107dc7771cSEwan Crawford // 30117dc7771cSEwan Crawford // When do_break is true we want to enable this functionality. 30127dc7771cSEwan Crawford // When do_break is false we want to disable it. 30137dc7771cSEwan Crawford void 30147dc7771cSEwan Crawford RenderScriptRuntime::SetBreakAllKernels(bool do_break, TargetSP target) 30157dc7771cSEwan Crawford { 301654782db7SEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS)); 30177dc7771cSEwan Crawford 30187dc7771cSEwan Crawford InitSearchFilter(target); 30197dc7771cSEwan Crawford 30207dc7771cSEwan Crawford // Set breakpoints on all the kernels 30217dc7771cSEwan Crawford if (do_break && !m_breakAllKernels) 30227dc7771cSEwan Crawford { 30237dc7771cSEwan Crawford m_breakAllKernels = true; 30247dc7771cSEwan Crawford 30257dc7771cSEwan Crawford for (const auto &module : m_rsmodules) 30267dc7771cSEwan Crawford BreakOnModuleKernels(module); 30277dc7771cSEwan Crawford 30287dc7771cSEwan Crawford if (log) 3029b3f7f69dSAidan Dodds log->Printf("%s(True) - breakpoints set on all currently loaded kernels.", __FUNCTION__); 30307dc7771cSEwan Crawford } 30317dc7771cSEwan Crawford else if (!do_break && m_breakAllKernels) // Breakpoints won't be set on any new kernels. 30327dc7771cSEwan Crawford { 30337dc7771cSEwan Crawford m_breakAllKernels = false; 30347dc7771cSEwan Crawford 30357dc7771cSEwan Crawford if (log) 3036b3f7f69dSAidan Dodds log->Printf("%s(False) - breakpoints no longer automatically set.", __FUNCTION__); 30377dc7771cSEwan Crawford } 30387dc7771cSEwan Crawford } 30397dc7771cSEwan Crawford 30407dc7771cSEwan Crawford // Given the name of a kernel this function creates a breakpoint using our 30417dc7771cSEwan Crawford // own breakpoint resolver, and returns the Breakpoint shared pointer. 30427dc7771cSEwan Crawford BreakpointSP 30437dc7771cSEwan Crawford RenderScriptRuntime::CreateKernelBreakpoint(const ConstString &name) 30447dc7771cSEwan Crawford { 304554782db7SEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS)); 30467dc7771cSEwan Crawford 30477dc7771cSEwan Crawford if (!m_filtersp) 30487dc7771cSEwan Crawford { 30497dc7771cSEwan Crawford if (log) 3050b3f7f69dSAidan Dodds log->Printf("%s - error, no breakpoint search filter set.", __FUNCTION__); 30517dc7771cSEwan Crawford return nullptr; 30527dc7771cSEwan Crawford } 30537dc7771cSEwan Crawford 30547dc7771cSEwan Crawford BreakpointResolverSP resolver_sp(new RSBreakpointResolver(nullptr, name)); 30557dc7771cSEwan Crawford BreakpointSP bp = GetProcess()->GetTarget().CreateBreakpoint(m_filtersp, resolver_sp, false, false, false); 30567dc7771cSEwan Crawford 305754782db7SEwan Crawford // Give RS breakpoints a specific name, so the user can manipulate them as a group. 305854782db7SEwan Crawford Error err; 305954782db7SEwan Crawford if (!bp->AddName("RenderScriptKernel", err) && log) 3060b3f7f69dSAidan Dodds log->Printf("%s - error setting break name, '%s'.", __FUNCTION__, err.AsCString()); 306154782db7SEwan Crawford 30627dc7771cSEwan Crawford return bp; 30637dc7771cSEwan Crawford } 30647dc7771cSEwan Crawford 3065018f5a7eSEwan Crawford // Given an expression for a variable this function tries to calculate the variable's value. 3066018f5a7eSEwan Crawford // If this is possible it returns true and sets the uint64_t parameter to the variables unsigned value. 3067018f5a7eSEwan Crawford // Otherwise function returns false. 3068018f5a7eSEwan Crawford bool 3069018f5a7eSEwan Crawford RenderScriptRuntime::GetFrameVarAsUnsigned(const StackFrameSP frame_sp, const char *var_name, uint64_t &val) 3070018f5a7eSEwan Crawford { 3071018f5a7eSEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 3072018f5a7eSEwan Crawford Error error; 3073018f5a7eSEwan Crawford VariableSP var_sp; 3074018f5a7eSEwan Crawford 3075018f5a7eSEwan Crawford // Find variable in stack frame 3076b3f7f69dSAidan Dodds ValueObjectSP value_sp(frame_sp->GetValueForVariableExpressionPath( 3077b3f7f69dSAidan Dodds var_name, eNoDynamicValues, 3078b3f7f69dSAidan Dodds StackFrame::eExpressionPathOptionCheckPtrVsMember | StackFrame::eExpressionPathOptionsAllowDirectIVarAccess, 3079b3f7f69dSAidan Dodds var_sp, error)); 3080018f5a7eSEwan Crawford if (!error.Success()) 3081018f5a7eSEwan Crawford { 3082018f5a7eSEwan Crawford if (log) 3083b3f7f69dSAidan Dodds log->Printf("%s - error, couldn't find '%s' in frame", __FUNCTION__, var_name); 3084018f5a7eSEwan Crawford return false; 3085018f5a7eSEwan Crawford } 3086018f5a7eSEwan Crawford 3087b3f7f69dSAidan Dodds // Find the uint32_t value for the variable 3088018f5a7eSEwan Crawford bool success = false; 3089018f5a7eSEwan Crawford val = value_sp->GetValueAsUnsigned(0, &success); 3090018f5a7eSEwan Crawford if (!success) 3091018f5a7eSEwan Crawford { 3092018f5a7eSEwan Crawford if (log) 3093b3f7f69dSAidan Dodds log->Printf("%s - error, couldn't parse '%s' as an uint32_t.", __FUNCTION__, var_name); 3094018f5a7eSEwan Crawford return false; 3095018f5a7eSEwan Crawford } 3096018f5a7eSEwan Crawford 3097018f5a7eSEwan Crawford return true; 3098018f5a7eSEwan Crawford } 3099018f5a7eSEwan Crawford 31004f8817c2SEwan Crawford // Function attempts to find the current coordinate of a kernel invocation by investigating the 31014f8817c2SEwan Crawford // values of frame variables in the .expand function. These coordinates are returned via the coord 31024f8817c2SEwan Crawford // array reference parameter. Returns true if the coordinates could be found, and false otherwise. 31034f8817c2SEwan Crawford bool 31044f8817c2SEwan Crawford RenderScriptRuntime::GetKernelCoordinate(RSCoordinate &coord, Thread *thread_ptr) 31054f8817c2SEwan Crawford { 31064f8817c2SEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 31074f8817c2SEwan Crawford 31084f8817c2SEwan Crawford if (!thread_ptr) 31094f8817c2SEwan Crawford { 31104f8817c2SEwan Crawford if (log) 31114f8817c2SEwan Crawford log->Printf("%s - Error, No thread pointer", __FUNCTION__); 31124f8817c2SEwan Crawford 31134f8817c2SEwan Crawford return false; 31144f8817c2SEwan Crawford } 31154f8817c2SEwan Crawford 31164f8817c2SEwan Crawford // Walk the call stack looking for a function whose name has the suffix '.expand' 31174f8817c2SEwan Crawford // and contains the variables we're looking for. 31184f8817c2SEwan Crawford for (uint32_t i = 0; i < thread_ptr->GetStackFrameCount(); ++i) 31194f8817c2SEwan Crawford { 31204f8817c2SEwan Crawford if (!thread_ptr->SetSelectedFrameByIndex(i)) 31214f8817c2SEwan Crawford continue; 31224f8817c2SEwan Crawford 31234f8817c2SEwan Crawford StackFrameSP frame_sp = thread_ptr->GetSelectedFrame(); 31244f8817c2SEwan Crawford if (!frame_sp) 31254f8817c2SEwan Crawford continue; 31264f8817c2SEwan Crawford 31274f8817c2SEwan Crawford // Find the function name 31284f8817c2SEwan Crawford const SymbolContext sym_ctx = frame_sp->GetSymbolContext(false); 31294f8817c2SEwan Crawford const char *func_name_cstr = sym_ctx.GetFunctionName().AsCString(); 31304f8817c2SEwan Crawford if (!func_name_cstr) 31314f8817c2SEwan Crawford continue; 31324f8817c2SEwan Crawford 31334f8817c2SEwan Crawford if (log) 31344f8817c2SEwan Crawford log->Printf("%s - Inspecting function '%s'", __FUNCTION__, func_name_cstr); 31354f8817c2SEwan Crawford 31364f8817c2SEwan Crawford // Check if function name has .expand suffix 31374f8817c2SEwan Crawford std::string func_name(func_name_cstr); 31384f8817c2SEwan Crawford const int length_difference = func_name.length() - RenderScriptRuntime::s_runtimeExpandSuffix.length(); 31394f8817c2SEwan Crawford if (length_difference <= 0) 31404f8817c2SEwan Crawford continue; 31414f8817c2SEwan Crawford 31424f8817c2SEwan Crawford const int32_t has_expand_suffix = func_name.compare(length_difference, 31434f8817c2SEwan Crawford RenderScriptRuntime::s_runtimeExpandSuffix.length(), 31444f8817c2SEwan Crawford RenderScriptRuntime::s_runtimeExpandSuffix); 31454f8817c2SEwan Crawford 31464f8817c2SEwan Crawford if (has_expand_suffix != 0) 31474f8817c2SEwan Crawford continue; 31484f8817c2SEwan Crawford 31494f8817c2SEwan Crawford if (log) 31504f8817c2SEwan Crawford log->Printf("%s - Found .expand function '%s'", __FUNCTION__, func_name_cstr); 31514f8817c2SEwan Crawford 31524f8817c2SEwan Crawford // Get values for variables in .expand frame that tell us the current kernel invocation 31534f8817c2SEwan Crawford bool found_coord_variables = true; 31544f8817c2SEwan Crawford assert(RenderScriptRuntime::s_runtimeCoordVars.size() == coord.size()); 31554f8817c2SEwan Crawford 31564f8817c2SEwan Crawford for (uint32_t i = 0; i < coord.size(); ++i) 31574f8817c2SEwan Crawford { 31584f8817c2SEwan Crawford uint64_t value = 0; 31594f8817c2SEwan Crawford if (!GetFrameVarAsUnsigned(frame_sp, RenderScriptRuntime::s_runtimeCoordVars[i], value)) 31604f8817c2SEwan Crawford { 31614f8817c2SEwan Crawford found_coord_variables = false; 31624f8817c2SEwan Crawford break; 31634f8817c2SEwan Crawford } 31644f8817c2SEwan Crawford coord[i] = value; 31654f8817c2SEwan Crawford } 31664f8817c2SEwan Crawford 31674f8817c2SEwan Crawford if (found_coord_variables) 31684f8817c2SEwan Crawford return true; 31694f8817c2SEwan Crawford } 31704f8817c2SEwan Crawford return false; 31714f8817c2SEwan Crawford } 31724f8817c2SEwan Crawford 3173018f5a7eSEwan Crawford // Callback when a kernel breakpoint hits and we're looking for a specific coordinate. 3174018f5a7eSEwan Crawford // Baton parameter contains a pointer to the target coordinate we want to break on. 3175018f5a7eSEwan Crawford // Function then checks the .expand frame for the current coordinate and breaks to user if it matches. 3176018f5a7eSEwan Crawford // Parameter 'break_id' is the id of the Breakpoint which made the callback. 3177018f5a7eSEwan Crawford // Parameter 'break_loc_id' is the id for the BreakpointLocation which was hit, 3178018f5a7eSEwan Crawford // a single logical breakpoint can have multiple addresses. 3179018f5a7eSEwan Crawford bool 3180b3f7f69dSAidan Dodds RenderScriptRuntime::KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, user_id_t break_id, 3181b3f7f69dSAidan Dodds user_id_t break_loc_id) 3182018f5a7eSEwan Crawford { 3183018f5a7eSEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS)); 3184018f5a7eSEwan Crawford 3185018f5a7eSEwan Crawford assert(baton && "Error: null baton in conditional kernel breakpoint callback"); 3186018f5a7eSEwan Crawford 3187018f5a7eSEwan Crawford // Coordinate we want to stop on 31884f8817c2SEwan Crawford const uint32_t *target_coord = static_cast<const uint32_t *>(baton); 3189018f5a7eSEwan Crawford 3190018f5a7eSEwan Crawford if (log) 31914f8817c2SEwan Crawford log->Printf("%s - Break ID %" PRIu64 ", (%" PRIu32 ", %" PRIu32 ", %" PRIu32 ")", __FUNCTION__, break_id, 31924f8817c2SEwan Crawford target_coord[0], target_coord[1], target_coord[2]); 3193018f5a7eSEwan Crawford 31944f8817c2SEwan Crawford // Select current thread 3195018f5a7eSEwan Crawford ExecutionContext context(ctx->exe_ctx_ref); 31964f8817c2SEwan Crawford Thread *thread_ptr = context.GetThreadPtr(); 31974f8817c2SEwan Crawford assert(thread_ptr && "Null thread pointer"); 31984f8817c2SEwan Crawford 31994f8817c2SEwan Crawford // Find current kernel invocation from .expand frame variables 32004f8817c2SEwan Crawford RSCoordinate current_coord{}; // Zero initialise array 32014f8817c2SEwan Crawford if (!GetKernelCoordinate(current_coord, thread_ptr)) 3202018f5a7eSEwan Crawford { 3203018f5a7eSEwan Crawford if (log) 32044f8817c2SEwan Crawford log->Printf("%s - Error, couldn't select .expand stack frame", __FUNCTION__); 3205018f5a7eSEwan Crawford return false; 3206018f5a7eSEwan Crawford } 3207018f5a7eSEwan Crawford 3208018f5a7eSEwan Crawford if (log) 32094f8817c2SEwan Crawford log->Printf("%s - (%" PRIu32 ",%" PRIu32 ",%" PRIu32 ")", __FUNCTION__, current_coord[0], current_coord[1], 32104f8817c2SEwan Crawford current_coord[2]); 3211018f5a7eSEwan Crawford 3212018f5a7eSEwan Crawford // Check if the current kernel invocation coordinate matches our target coordinate 3213b3f7f69dSAidan Dodds if (current_coord[0] == target_coord[0] && 3214b3f7f69dSAidan Dodds current_coord[1] == target_coord[1] && 32154f8817c2SEwan Crawford current_coord[2] == target_coord[2]) 3216018f5a7eSEwan Crawford { 3217018f5a7eSEwan Crawford if (log) 32184f8817c2SEwan Crawford log->Printf("%s, BREAKING (%" PRIu32 ",%" PRIu32 ",%" PRIu32 ")", __FUNCTION__, current_coord[0], 32194f8817c2SEwan Crawford current_coord[1], current_coord[2]); 3220018f5a7eSEwan Crawford 3221018f5a7eSEwan Crawford BreakpointSP breakpoint_sp = context.GetTargetPtr()->GetBreakpointByID(break_id); 3222018f5a7eSEwan Crawford assert(breakpoint_sp != nullptr && "Error: Couldn't find breakpoint matching break id for callback"); 3223018f5a7eSEwan Crawford breakpoint_sp->SetEnabled(false); // Optimise since conditional breakpoint should only be hit once. 3224018f5a7eSEwan Crawford return true; 3225018f5a7eSEwan Crawford } 3226018f5a7eSEwan Crawford 3227018f5a7eSEwan Crawford // No match on coordinate 3228018f5a7eSEwan Crawford return false; 3229018f5a7eSEwan Crawford } 3230018f5a7eSEwan Crawford 3231018f5a7eSEwan Crawford // Tries to set a breakpoint on the start of a kernel, resolved using the kernel name. 3232018f5a7eSEwan Crawford // Argument 'coords', represents a three dimensional coordinate which can be used to specify 3233018f5a7eSEwan Crawford // a single kernel instance to break on. If this is set then we add a callback to the breakpoint. 32344640cde1SColin Riley void 3235018f5a7eSEwan Crawford RenderScriptRuntime::PlaceBreakpointOnKernel(Stream &strm, const char *name, const std::array<int, 3> coords, 3236018f5a7eSEwan Crawford Error &error, TargetSP target) 32374640cde1SColin Riley { 32384640cde1SColin Riley if (!name) 32394640cde1SColin Riley { 32404640cde1SColin Riley error.SetErrorString("invalid kernel name"); 32414640cde1SColin Riley return; 32424640cde1SColin Riley } 32434640cde1SColin Riley 32447dc7771cSEwan Crawford InitSearchFilter(target); 324598156583SEwan Crawford 32464640cde1SColin Riley ConstString kernel_name(name); 32477dc7771cSEwan Crawford BreakpointSP bp = CreateKernelBreakpoint(kernel_name); 3248018f5a7eSEwan Crawford 3249018f5a7eSEwan Crawford // We have a conditional breakpoint on a specific coordinate 3250018f5a7eSEwan Crawford if (coords[0] != -1) 3251018f5a7eSEwan Crawford { 3252b3f7f69dSAidan Dodds strm.Printf("Conditional kernel breakpoint on coordinate %" PRId32 ", %" PRId32 ", %" PRId32, 3253b3f7f69dSAidan Dodds coords[0], coords[1], coords[2]); 3254018f5a7eSEwan Crawford strm.EOL(); 3255018f5a7eSEwan Crawford 3256018f5a7eSEwan Crawford // Allocate memory for the baton, and copy over coordinate 32574f8817c2SEwan Crawford uint32_t *baton = new uint32_t[coords.size()]; 3258018f5a7eSEwan Crawford baton[0] = coords[0]; baton[1] = coords[1]; baton[2] = coords[2]; 3259018f5a7eSEwan Crawford 3260018f5a7eSEwan Crawford // Create a callback that will be invoked everytime the breakpoint is hit. 3261018f5a7eSEwan Crawford // The baton object passed to the handler is the target coordinate we want to break on. 3262018f5a7eSEwan Crawford bp->SetCallback(KernelBreakpointHit, baton, true); 3263018f5a7eSEwan Crawford 3264018f5a7eSEwan Crawford // Store a shared pointer to the baton, so the memory will eventually be cleaned up after destruction 32654f8817c2SEwan Crawford m_conditional_breaks[bp->GetID()] = std::shared_ptr<uint32_t>(baton); 3266018f5a7eSEwan Crawford } 3267018f5a7eSEwan Crawford 326898156583SEwan Crawford if (bp) 326998156583SEwan Crawford bp->GetDescription(&strm, lldb::eDescriptionLevelInitial, false); 32704640cde1SColin Riley } 32714640cde1SColin Riley 32724640cde1SColin Riley void 32735ec532a9SColin Riley RenderScriptRuntime::DumpModules(Stream &strm) const 32745ec532a9SColin Riley { 32755ec532a9SColin Riley strm.Printf("RenderScript Modules:"); 32765ec532a9SColin Riley strm.EOL(); 32775ec532a9SColin Riley strm.IndentMore(); 32785ec532a9SColin Riley for (const auto &module : m_rsmodules) 32795ec532a9SColin Riley { 32804640cde1SColin Riley module->Dump(strm); 32815ec532a9SColin Riley } 32825ec532a9SColin Riley strm.IndentLess(); 32835ec532a9SColin Riley } 32845ec532a9SColin Riley 328578f339d1SEwan Crawford RenderScriptRuntime::ScriptDetails * 328678f339d1SEwan Crawford RenderScriptRuntime::LookUpScript(addr_t address, bool create) 328778f339d1SEwan Crawford { 328878f339d1SEwan Crawford for (const auto &s : m_scripts) 328978f339d1SEwan Crawford { 329078f339d1SEwan Crawford if (s->script.isValid()) 329178f339d1SEwan Crawford if (*s->script == address) 329278f339d1SEwan Crawford return s.get(); 329378f339d1SEwan Crawford } 329478f339d1SEwan Crawford if (create) 329578f339d1SEwan Crawford { 329678f339d1SEwan Crawford std::unique_ptr<ScriptDetails> s(new ScriptDetails); 329778f339d1SEwan Crawford s->script = address; 329878f339d1SEwan Crawford m_scripts.push_back(std::move(s)); 3299d10ca9deSEwan Crawford return m_scripts.back().get(); 330078f339d1SEwan Crawford } 330178f339d1SEwan Crawford return nullptr; 330278f339d1SEwan Crawford } 330378f339d1SEwan Crawford 330478f339d1SEwan Crawford RenderScriptRuntime::AllocationDetails * 330578f339d1SEwan Crawford RenderScriptRuntime::LookUpAllocation(addr_t address, bool create) 330678f339d1SEwan Crawford { 330778f339d1SEwan Crawford for (const auto &a : m_allocations) 330878f339d1SEwan Crawford { 330978f339d1SEwan Crawford if (a->address.isValid()) 331078f339d1SEwan Crawford if (*a->address == address) 331178f339d1SEwan Crawford return a.get(); 331278f339d1SEwan Crawford } 331378f339d1SEwan Crawford if (create) 331478f339d1SEwan Crawford { 331578f339d1SEwan Crawford std::unique_ptr<AllocationDetails> a(new AllocationDetails); 331678f339d1SEwan Crawford a->address = address; 331778f339d1SEwan Crawford m_allocations.push_back(std::move(a)); 3318d10ca9deSEwan Crawford return m_allocations.back().get(); 331978f339d1SEwan Crawford } 332078f339d1SEwan Crawford return nullptr; 332178f339d1SEwan Crawford } 332278f339d1SEwan Crawford 33235ec532a9SColin Riley void 33245ec532a9SColin Riley RSModuleDescriptor::Dump(Stream &strm) const 33255ec532a9SColin Riley { 33265ec532a9SColin Riley strm.Indent(); 33275ec532a9SColin Riley m_module->GetFileSpec().Dump(&strm); 33284640cde1SColin Riley if (m_module->GetNumCompileUnits()) 33294640cde1SColin Riley { 33304640cde1SColin Riley strm.Indent("Debug info loaded."); 33314640cde1SColin Riley } 33324640cde1SColin Riley else 33334640cde1SColin Riley { 33344640cde1SColin Riley strm.Indent("Debug info does not exist."); 33354640cde1SColin Riley } 33365ec532a9SColin Riley strm.EOL(); 33375ec532a9SColin Riley strm.IndentMore(); 33385ec532a9SColin Riley strm.Indent(); 3339189598edSColin Riley strm.Printf("Globals: %" PRIu64, static_cast<uint64_t>(m_globals.size())); 33405ec532a9SColin Riley strm.EOL(); 33415ec532a9SColin Riley strm.IndentMore(); 33425ec532a9SColin Riley for (const auto &global : m_globals) 33435ec532a9SColin Riley { 33445ec532a9SColin Riley global.Dump(strm); 33455ec532a9SColin Riley } 33465ec532a9SColin Riley strm.IndentLess(); 33475ec532a9SColin Riley strm.Indent(); 3348189598edSColin Riley strm.Printf("Kernels: %" PRIu64, static_cast<uint64_t>(m_kernels.size())); 33495ec532a9SColin Riley strm.EOL(); 33505ec532a9SColin Riley strm.IndentMore(); 33515ec532a9SColin Riley for (const auto &kernel : m_kernels) 33525ec532a9SColin Riley { 33535ec532a9SColin Riley kernel.Dump(strm); 33545ec532a9SColin Riley } 33554640cde1SColin Riley strm.Printf("Pragmas: %" PRIu64, static_cast<uint64_t>(m_pragmas.size())); 33564640cde1SColin Riley strm.EOL(); 33574640cde1SColin Riley strm.IndentMore(); 33584640cde1SColin Riley for (const auto &key_val : m_pragmas) 33594640cde1SColin Riley { 33604640cde1SColin Riley strm.Printf("%s: %s", key_val.first.c_str(), key_val.second.c_str()); 33614640cde1SColin Riley strm.EOL(); 33624640cde1SColin Riley } 33635ec532a9SColin Riley strm.IndentLess(4); 33645ec532a9SColin Riley } 33655ec532a9SColin Riley 33665ec532a9SColin Riley void 33675ec532a9SColin Riley RSGlobalDescriptor::Dump(Stream &strm) const 33685ec532a9SColin Riley { 33695ec532a9SColin Riley strm.Indent(m_name.AsCString()); 33704640cde1SColin Riley VariableList var_list; 33714640cde1SColin Riley m_module->m_module->FindGlobalVariables(m_name, nullptr, true, 1U, var_list); 33724640cde1SColin Riley if (var_list.GetSize() == 1) 33734640cde1SColin Riley { 33744640cde1SColin Riley auto var = var_list.GetVariableAtIndex(0); 33754640cde1SColin Riley auto type = var->GetType(); 33764640cde1SColin Riley if (type) 33774640cde1SColin Riley { 33784640cde1SColin Riley strm.Printf(" - "); 33794640cde1SColin Riley type->DumpTypeName(&strm); 33804640cde1SColin Riley } 33814640cde1SColin Riley else 33824640cde1SColin Riley { 33834640cde1SColin Riley strm.Printf(" - Unknown Type"); 33844640cde1SColin Riley } 33854640cde1SColin Riley } 33864640cde1SColin Riley else 33874640cde1SColin Riley { 33884640cde1SColin Riley strm.Printf(" - variable identified, but not found in binary"); 33894640cde1SColin Riley const Symbol *s = m_module->m_module->FindFirstSymbolWithNameAndType(m_name, eSymbolTypeData); 33904640cde1SColin Riley if (s) 33914640cde1SColin Riley { 33924640cde1SColin Riley strm.Printf(" (symbol exists) "); 33934640cde1SColin Riley } 33944640cde1SColin Riley } 33954640cde1SColin Riley 33965ec532a9SColin Riley strm.EOL(); 33975ec532a9SColin Riley } 33985ec532a9SColin Riley 33995ec532a9SColin Riley void 34005ec532a9SColin Riley RSKernelDescriptor::Dump(Stream &strm) const 34015ec532a9SColin Riley { 34025ec532a9SColin Riley strm.Indent(m_name.AsCString()); 34035ec532a9SColin Riley strm.EOL(); 34045ec532a9SColin Riley } 34055ec532a9SColin Riley 34065ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModuleDump : public CommandObjectParsed 34075ec532a9SColin Riley { 34085ec532a9SColin Riley public: 34095ec532a9SColin Riley CommandObjectRenderScriptRuntimeModuleDump(CommandInterpreter &interpreter) 34105ec532a9SColin Riley : CommandObjectParsed(interpreter, "renderscript module dump", 34115ec532a9SColin Riley "Dumps renderscript specific information for all modules.", "renderscript module dump", 3412e87764f2SEnrico Granata eCommandRequiresProcess | eCommandProcessMustBeLaunched) 34135ec532a9SColin Riley { 34145ec532a9SColin Riley } 34155ec532a9SColin Riley 3416222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeModuleDump() override = default; 34175ec532a9SColin Riley 34185ec532a9SColin Riley bool 3419222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 34205ec532a9SColin Riley { 34215ec532a9SColin Riley RenderScriptRuntime *runtime = 34225ec532a9SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 34235ec532a9SColin Riley runtime->DumpModules(result.GetOutputStream()); 34245ec532a9SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 34255ec532a9SColin Riley return true; 34265ec532a9SColin Riley } 34275ec532a9SColin Riley }; 34285ec532a9SColin Riley 34295ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModule : public CommandObjectMultiword 34305ec532a9SColin Riley { 34315ec532a9SColin Riley public: 34325ec532a9SColin Riley CommandObjectRenderScriptRuntimeModule(CommandInterpreter &interpreter) 34335ec532a9SColin Riley : CommandObjectMultiword(interpreter, "renderscript module", "Commands that deal with renderscript modules.", 3434b3f7f69dSAidan Dodds nullptr) 34355ec532a9SColin Riley { 34365ec532a9SColin Riley LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeModuleDump(interpreter))); 34375ec532a9SColin Riley } 34385ec532a9SColin Riley 3439222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeModule() override = default; 34405ec532a9SColin Riley }; 34415ec532a9SColin Riley 34424640cde1SColin Riley class CommandObjectRenderScriptRuntimeKernelList : public CommandObjectParsed 34434640cde1SColin Riley { 34444640cde1SColin Riley public: 34454640cde1SColin Riley CommandObjectRenderScriptRuntimeKernelList(CommandInterpreter &interpreter) 34464640cde1SColin Riley : CommandObjectParsed(interpreter, "renderscript kernel list", 3447b3f7f69dSAidan Dodds "Lists renderscript kernel names and associated script resources.", 3448b3f7f69dSAidan Dodds "renderscript kernel list", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 34494640cde1SColin Riley { 34504640cde1SColin Riley } 34514640cde1SColin Riley 3452222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelList() override = default; 34534640cde1SColin Riley 34544640cde1SColin Riley bool 3455222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 34564640cde1SColin Riley { 34574640cde1SColin Riley RenderScriptRuntime *runtime = 34584640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 34594640cde1SColin Riley runtime->DumpKernels(result.GetOutputStream()); 34604640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 34614640cde1SColin Riley return true; 34624640cde1SColin Riley } 34634640cde1SColin Riley }; 34644640cde1SColin Riley 34657dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpointSet : public CommandObjectParsed 34664640cde1SColin Riley { 34674640cde1SColin Riley public: 34687dc7771cSEwan Crawford CommandObjectRenderScriptRuntimeKernelBreakpointSet(CommandInterpreter &interpreter) 34697dc7771cSEwan Crawford : CommandObjectParsed(interpreter, "renderscript kernel breakpoint set", 3470b3f7f69dSAidan Dodds "Sets a breakpoint on a renderscript kernel.", 3471b3f7f69dSAidan Dodds "renderscript kernel breakpoint set <kernel_name> [-c x,y,z]", 3472b3f7f69dSAidan Dodds eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 3473b3f7f69dSAidan Dodds m_options(interpreter) 34744640cde1SColin Riley { 34754640cde1SColin Riley } 34764640cde1SColin Riley 3477222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelBreakpointSet() override = default; 3478222b937cSEugene Zelenko 3479222b937cSEugene Zelenko Options * 3480222b937cSEugene Zelenko GetOptions() override 3481018f5a7eSEwan Crawford { 3482018f5a7eSEwan Crawford return &m_options; 3483018f5a7eSEwan Crawford } 3484018f5a7eSEwan Crawford 3485018f5a7eSEwan Crawford class CommandOptions : public Options 3486018f5a7eSEwan Crawford { 3487018f5a7eSEwan Crawford public: 3488b3f7f69dSAidan Dodds CommandOptions(CommandInterpreter &interpreter) : Options(interpreter) {} 3489018f5a7eSEwan Crawford 3490222b937cSEugene Zelenko ~CommandOptions() override = default; 3491018f5a7eSEwan Crawford 3492222b937cSEugene Zelenko Error 3493222b937cSEugene Zelenko SetOptionValue(uint32_t option_idx, const char *option_arg) override 3494018f5a7eSEwan Crawford { 3495018f5a7eSEwan Crawford Error error; 3496018f5a7eSEwan Crawford const int short_option = m_getopt_table[option_idx].val; 3497018f5a7eSEwan Crawford 3498018f5a7eSEwan Crawford switch (short_option) 3499018f5a7eSEwan Crawford { 3500018f5a7eSEwan Crawford case 'c': 3501018f5a7eSEwan Crawford if (!ParseCoordinate(option_arg)) 3502b3f7f69dSAidan Dodds error.SetErrorStringWithFormat("Couldn't parse coordinate '%s', should be in format 'x,y,z'.", 3503b3f7f69dSAidan Dodds option_arg); 3504018f5a7eSEwan Crawford break; 3505018f5a7eSEwan Crawford default: 3506018f5a7eSEwan Crawford error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 3507018f5a7eSEwan Crawford break; 3508018f5a7eSEwan Crawford } 3509018f5a7eSEwan Crawford return error; 3510018f5a7eSEwan Crawford } 3511018f5a7eSEwan Crawford 3512018f5a7eSEwan Crawford // -c takes an argument of the form 'num[,num][,num]'. 3513018f5a7eSEwan Crawford // Where 'id_cstr' is this argument with the whitespace trimmed. 3514018f5a7eSEwan Crawford // Missing coordinates are defaulted to zero. 3515018f5a7eSEwan Crawford bool 3516018f5a7eSEwan Crawford ParseCoordinate(const char *id_cstr) 3517018f5a7eSEwan Crawford { 3518018f5a7eSEwan Crawford RegularExpression regex; 3519018f5a7eSEwan Crawford RegularExpression::Match regex_match(3); 3520018f5a7eSEwan Crawford 3521018f5a7eSEwan Crawford bool matched = false; 3522018f5a7eSEwan Crawford if (regex.Compile("^([0-9]+),([0-9]+),([0-9]+)$") && regex.Execute(id_cstr, ®ex_match)) 3523018f5a7eSEwan Crawford matched = true; 3524018f5a7eSEwan Crawford else if (regex.Compile("^([0-9]+),([0-9]+)$") && regex.Execute(id_cstr, ®ex_match)) 3525018f5a7eSEwan Crawford matched = true; 3526018f5a7eSEwan Crawford else if (regex.Compile("^([0-9]+)$") && regex.Execute(id_cstr, ®ex_match)) 3527018f5a7eSEwan Crawford matched = true; 3528018f5a7eSEwan Crawford for (uint32_t i = 0; i < 3; i++) 3529018f5a7eSEwan Crawford { 3530018f5a7eSEwan Crawford std::string group; 3531018f5a7eSEwan Crawford if (regex_match.GetMatchAtIndex(id_cstr, i + 1, group)) 3532b3f7f69dSAidan Dodds m_coord[i] = (uint32_t)strtoul(group.c_str(), nullptr, 0); 3533018f5a7eSEwan Crawford else 3534018f5a7eSEwan Crawford m_coord[i] = 0; 3535018f5a7eSEwan Crawford } 3536018f5a7eSEwan Crawford return matched; 3537018f5a7eSEwan Crawford } 3538018f5a7eSEwan Crawford 3539018f5a7eSEwan Crawford void 3540222b937cSEugene Zelenko OptionParsingStarting() override 3541018f5a7eSEwan Crawford { 3542018f5a7eSEwan Crawford // -1 means the -c option hasn't been set 3543018f5a7eSEwan Crawford m_coord[0] = -1; 3544018f5a7eSEwan Crawford m_coord[1] = -1; 3545018f5a7eSEwan Crawford m_coord[2] = -1; 3546018f5a7eSEwan Crawford } 3547018f5a7eSEwan Crawford 3548018f5a7eSEwan Crawford const OptionDefinition * 3549222b937cSEugene Zelenko GetDefinitions() override 3550018f5a7eSEwan Crawford { 3551018f5a7eSEwan Crawford return g_option_table; 3552018f5a7eSEwan Crawford } 3553018f5a7eSEwan Crawford 3554018f5a7eSEwan Crawford static OptionDefinition g_option_table[]; 3555018f5a7eSEwan Crawford std::array<int, 3> m_coord; 3556018f5a7eSEwan Crawford }; 3557018f5a7eSEwan Crawford 35584640cde1SColin Riley bool 3559222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 35604640cde1SColin Riley { 35614640cde1SColin Riley const size_t argc = command.GetArgumentCount(); 3562018f5a7eSEwan Crawford if (argc < 1) 35634640cde1SColin Riley { 3564b3f7f69dSAidan Dodds result.AppendErrorWithFormat("'%s' takes 1 argument of kernel name, and an optional coordinate.", 3565b3f7f69dSAidan Dodds m_cmd_name.c_str()); 3566018f5a7eSEwan Crawford result.SetStatus(eReturnStatusFailed); 3567018f5a7eSEwan Crawford return false; 3568018f5a7eSEwan Crawford } 3569018f5a7eSEwan Crawford 35704640cde1SColin Riley RenderScriptRuntime *runtime = 35714640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 35724640cde1SColin Riley 35734640cde1SColin Riley Error error; 3574018f5a7eSEwan Crawford runtime->PlaceBreakpointOnKernel(result.GetOutputStream(), command.GetArgumentAtIndex(0), m_options.m_coord, 357598156583SEwan Crawford error, m_exe_ctx.GetTargetSP()); 35764640cde1SColin Riley 35774640cde1SColin Riley if (error.Success()) 35784640cde1SColin Riley { 35794640cde1SColin Riley result.AppendMessage("Breakpoint(s) created"); 35804640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 35814640cde1SColin Riley return true; 35824640cde1SColin Riley } 35834640cde1SColin Riley result.SetStatus(eReturnStatusFailed); 35844640cde1SColin Riley result.AppendErrorWithFormat("Error: %s", error.AsCString()); 35854640cde1SColin Riley return false; 35864640cde1SColin Riley } 35874640cde1SColin Riley 3588018f5a7eSEwan Crawford private: 3589018f5a7eSEwan Crawford CommandOptions m_options; 35904640cde1SColin Riley }; 35914640cde1SColin Riley 3592b3f7f69dSAidan Dodds OptionDefinition CommandObjectRenderScriptRuntimeKernelBreakpointSet::CommandOptions::g_option_table[] = { 3593b3f7f69dSAidan Dodds {LLDB_OPT_SET_1, false, "coordinate", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeValue, 3594018f5a7eSEwan Crawford "Set a breakpoint on a single invocation of the kernel with specified coordinate.\n" 3595018f5a7eSEwan Crawford "Coordinate takes the form 'x[,y][,z] where x,y,z are positive integers representing kernel dimensions. " 3596018f5a7eSEwan Crawford "Any unset dimensions will be defaulted to zero."}, 3597b3f7f69dSAidan Dodds {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}}; 3598018f5a7eSEwan Crawford 35997dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpointAll : public CommandObjectParsed 36007dc7771cSEwan Crawford { 36017dc7771cSEwan Crawford public: 36027dc7771cSEwan Crawford CommandObjectRenderScriptRuntimeKernelBreakpointAll(CommandInterpreter &interpreter) 3603b3f7f69dSAidan Dodds : CommandObjectParsed( 3604b3f7f69dSAidan Dodds interpreter, "renderscript kernel breakpoint all", 36057dc7771cSEwan Crawford "Automatically sets a breakpoint on all renderscript kernels that are or will be loaded.\n" 36067dc7771cSEwan Crawford "Disabling option means breakpoints will no longer be set on any kernels loaded in the future, " 36077dc7771cSEwan Crawford "but does not remove currently set breakpoints.", 36087dc7771cSEwan Crawford "renderscript kernel breakpoint all <enable/disable>", 36097dc7771cSEwan Crawford eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) 36107dc7771cSEwan Crawford { 36117dc7771cSEwan Crawford } 36127dc7771cSEwan Crawford 3613222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelBreakpointAll() override = default; 36147dc7771cSEwan Crawford 36157dc7771cSEwan Crawford bool 3616222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 36177dc7771cSEwan Crawford { 36187dc7771cSEwan Crawford const size_t argc = command.GetArgumentCount(); 36197dc7771cSEwan Crawford if (argc != 1) 36207dc7771cSEwan Crawford { 36217dc7771cSEwan Crawford result.AppendErrorWithFormat("'%s' takes 1 argument of 'enable' or 'disable'", m_cmd_name.c_str()); 36227dc7771cSEwan Crawford result.SetStatus(eReturnStatusFailed); 36237dc7771cSEwan Crawford return false; 36247dc7771cSEwan Crawford } 36257dc7771cSEwan Crawford 3626b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 3627b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 36287dc7771cSEwan Crawford 36297dc7771cSEwan Crawford bool do_break = false; 36307dc7771cSEwan Crawford const char *argument = command.GetArgumentAtIndex(0); 36317dc7771cSEwan Crawford if (strcmp(argument, "enable") == 0) 36327dc7771cSEwan Crawford { 36337dc7771cSEwan Crawford do_break = true; 36347dc7771cSEwan Crawford result.AppendMessage("Breakpoints will be set on all kernels."); 36357dc7771cSEwan Crawford } 36367dc7771cSEwan Crawford else if (strcmp(argument, "disable") == 0) 36377dc7771cSEwan Crawford { 36387dc7771cSEwan Crawford do_break = false; 36397dc7771cSEwan Crawford result.AppendMessage("Breakpoints will not be set on any new kernels."); 36407dc7771cSEwan Crawford } 36417dc7771cSEwan Crawford else 36427dc7771cSEwan Crawford { 36437dc7771cSEwan Crawford result.AppendErrorWithFormat("Argument must be either 'enable' or 'disable'"); 36447dc7771cSEwan Crawford result.SetStatus(eReturnStatusFailed); 36457dc7771cSEwan Crawford return false; 36467dc7771cSEwan Crawford } 36477dc7771cSEwan Crawford 36487dc7771cSEwan Crawford runtime->SetBreakAllKernels(do_break, m_exe_ctx.GetTargetSP()); 36497dc7771cSEwan Crawford 36507dc7771cSEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 36517dc7771cSEwan Crawford return true; 36527dc7771cSEwan Crawford } 36537dc7771cSEwan Crawford }; 36547dc7771cSEwan Crawford 36554f8817c2SEwan Crawford class CommandObjectRenderScriptRuntimeKernelCoordinate : public CommandObjectParsed 36564f8817c2SEwan Crawford { 36574f8817c2SEwan Crawford public: 36584f8817c2SEwan Crawford CommandObjectRenderScriptRuntimeKernelCoordinate(CommandInterpreter &interpreter) 36594f8817c2SEwan Crawford : CommandObjectParsed(interpreter, "renderscript kernel coordinate", 36604f8817c2SEwan Crawford "Shows the (x,y,z) coordinate of the current kernel invocation.", 36614f8817c2SEwan Crawford "renderscript kernel coordinate", 36624f8817c2SEwan Crawford eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) 36634f8817c2SEwan Crawford { 36644f8817c2SEwan Crawford } 36654f8817c2SEwan Crawford 36664f8817c2SEwan Crawford ~CommandObjectRenderScriptRuntimeKernelCoordinate() override = default; 36674f8817c2SEwan Crawford 36684f8817c2SEwan Crawford bool 36694f8817c2SEwan Crawford DoExecute(Args &command, CommandReturnObject &result) override 36704f8817c2SEwan Crawford { 36714f8817c2SEwan Crawford RSCoordinate coord{}; // Zero initialize array 36724f8817c2SEwan Crawford bool success = RenderScriptRuntime::GetKernelCoordinate(coord, m_exe_ctx.GetThreadPtr()); 36734f8817c2SEwan Crawford Stream &stream = result.GetOutputStream(); 36744f8817c2SEwan Crawford 36754f8817c2SEwan Crawford if (success) 36764f8817c2SEwan Crawford { 36774f8817c2SEwan Crawford stream.Printf("Coordinate: (%" PRIu32 ", %" PRIu32 ", %" PRIu32 ")", coord[0], coord[1], coord[2]); 36784f8817c2SEwan Crawford stream.EOL(); 36794f8817c2SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 36804f8817c2SEwan Crawford } 36814f8817c2SEwan Crawford else 36824f8817c2SEwan Crawford { 36834f8817c2SEwan Crawford stream.Printf("Error: Coordinate could not be found."); 36844f8817c2SEwan Crawford stream.EOL(); 36854f8817c2SEwan Crawford result.SetStatus(eReturnStatusFailed); 36864f8817c2SEwan Crawford } 36874f8817c2SEwan Crawford return true; 36884f8817c2SEwan Crawford } 36894f8817c2SEwan Crawford }; 36904f8817c2SEwan Crawford 36917dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpoint : public CommandObjectMultiword 36927dc7771cSEwan Crawford { 36937dc7771cSEwan Crawford public: 36947dc7771cSEwan Crawford CommandObjectRenderScriptRuntimeKernelBreakpoint(CommandInterpreter &interpreter) 3695b3f7f69dSAidan Dodds : CommandObjectMultiword(interpreter, "renderscript kernel", 3696b3f7f69dSAidan Dodds "Commands that generate breakpoints on renderscript kernels.", nullptr) 36977dc7771cSEwan Crawford { 36987dc7771cSEwan Crawford LoadSubCommand("set", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpointSet(interpreter))); 36997dc7771cSEwan Crawford LoadSubCommand("all", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpointAll(interpreter))); 37007dc7771cSEwan Crawford } 37017dc7771cSEwan Crawford 3702222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelBreakpoint() override = default; 37037dc7771cSEwan Crawford }; 37047dc7771cSEwan Crawford 37054640cde1SColin Riley class CommandObjectRenderScriptRuntimeKernel : public CommandObjectMultiword 37064640cde1SColin Riley { 37074640cde1SColin Riley public: 37084640cde1SColin Riley CommandObjectRenderScriptRuntimeKernel(CommandInterpreter &interpreter) 37094640cde1SColin Riley : CommandObjectMultiword(interpreter, "renderscript kernel", "Commands that deal with renderscript kernels.", 3710b3f7f69dSAidan Dodds nullptr) 37114640cde1SColin Riley { 37124640cde1SColin Riley LoadSubCommand("list", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelList(interpreter))); 371336175cc0SEwan Crawford LoadSubCommand("coordinate", 371436175cc0SEwan Crawford CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelCoordinate(interpreter))); 3715b3f7f69dSAidan Dodds LoadSubCommand("breakpoint", 3716b3f7f69dSAidan Dodds CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpoint(interpreter))); 37174640cde1SColin Riley } 37184640cde1SColin Riley 3719222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernel() override = default; 37204640cde1SColin Riley }; 37214640cde1SColin Riley 37224640cde1SColin Riley class CommandObjectRenderScriptRuntimeContextDump : public CommandObjectParsed 37234640cde1SColin Riley { 37244640cde1SColin Riley public: 37254640cde1SColin Riley CommandObjectRenderScriptRuntimeContextDump(CommandInterpreter &interpreter) 3726b3f7f69dSAidan Dodds : CommandObjectParsed(interpreter, "renderscript context dump", "Dumps renderscript context information.", 3727b3f7f69dSAidan Dodds "renderscript context dump", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 37284640cde1SColin Riley { 37294640cde1SColin Riley } 37304640cde1SColin Riley 3731222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeContextDump() override = default; 37324640cde1SColin Riley 37334640cde1SColin Riley bool 3734222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 37354640cde1SColin Riley { 37364640cde1SColin Riley RenderScriptRuntime *runtime = 37374640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 37384640cde1SColin Riley runtime->DumpContexts(result.GetOutputStream()); 37394640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 37404640cde1SColin Riley return true; 37414640cde1SColin Riley } 37424640cde1SColin Riley }; 37434640cde1SColin Riley 37444640cde1SColin Riley class CommandObjectRenderScriptRuntimeContext : public CommandObjectMultiword 37454640cde1SColin Riley { 37464640cde1SColin Riley public: 37474640cde1SColin Riley CommandObjectRenderScriptRuntimeContext(CommandInterpreter &interpreter) 37484640cde1SColin Riley : CommandObjectMultiword(interpreter, "renderscript context", "Commands that deal with renderscript contexts.", 3749b3f7f69dSAidan Dodds nullptr) 37504640cde1SColin Riley { 37514640cde1SColin Riley LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeContextDump(interpreter))); 37524640cde1SColin Riley } 37534640cde1SColin Riley 3754222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeContext() override = default; 37554640cde1SColin Riley }; 37564640cde1SColin Riley 3757a0f08674SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationDump : public CommandObjectParsed 3758a0f08674SEwan Crawford { 3759a0f08674SEwan Crawford public: 3760a0f08674SEwan Crawford CommandObjectRenderScriptRuntimeAllocationDump(CommandInterpreter &interpreter) 3761a0f08674SEwan Crawford : CommandObjectParsed(interpreter, "renderscript allocation dump", 3762a0f08674SEwan Crawford "Displays the contents of a particular allocation", "renderscript allocation dump <ID>", 3763b3f7f69dSAidan Dodds eCommandRequiresProcess | eCommandProcessMustBeLaunched), 3764b3f7f69dSAidan Dodds m_options(interpreter) 3765a0f08674SEwan Crawford { 3766a0f08674SEwan Crawford } 3767a0f08674SEwan Crawford 3768222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationDump() override = default; 3769222b937cSEugene Zelenko 3770222b937cSEugene Zelenko Options * 3771222b937cSEugene Zelenko GetOptions() override 3772a0f08674SEwan Crawford { 3773a0f08674SEwan Crawford return &m_options; 3774a0f08674SEwan Crawford } 3775a0f08674SEwan Crawford 3776a0f08674SEwan Crawford class CommandOptions : public Options 3777a0f08674SEwan Crawford { 3778a0f08674SEwan Crawford public: 3779b3f7f69dSAidan Dodds CommandOptions(CommandInterpreter &interpreter) : Options(interpreter) {} 3780a0f08674SEwan Crawford 3781222b937cSEugene Zelenko ~CommandOptions() override = default; 3782a0f08674SEwan Crawford 3783222b937cSEugene Zelenko Error 3784222b937cSEugene Zelenko SetOptionValue(uint32_t option_idx, const char *option_arg) override 3785a0f08674SEwan Crawford { 3786a0f08674SEwan Crawford Error error; 3787a0f08674SEwan Crawford const int short_option = m_getopt_table[option_idx].val; 3788a0f08674SEwan Crawford 3789a0f08674SEwan Crawford switch (short_option) 3790a0f08674SEwan Crawford { 3791a0f08674SEwan Crawford case 'f': 3792a0f08674SEwan Crawford m_outfile.SetFile(option_arg, true); 3793a0f08674SEwan Crawford if (m_outfile.Exists()) 3794a0f08674SEwan Crawford { 3795a0f08674SEwan Crawford m_outfile.Clear(); 3796a0f08674SEwan Crawford error.SetErrorStringWithFormat("file already exists: '%s'", option_arg); 3797a0f08674SEwan Crawford } 3798a0f08674SEwan Crawford break; 3799a0f08674SEwan Crawford default: 3800a0f08674SEwan Crawford error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 3801a0f08674SEwan Crawford break; 3802a0f08674SEwan Crawford } 3803a0f08674SEwan Crawford return error; 3804a0f08674SEwan Crawford } 3805a0f08674SEwan Crawford 3806a0f08674SEwan Crawford void 3807222b937cSEugene Zelenko OptionParsingStarting() override 3808a0f08674SEwan Crawford { 3809a0f08674SEwan Crawford m_outfile.Clear(); 3810a0f08674SEwan Crawford } 3811a0f08674SEwan Crawford 3812a0f08674SEwan Crawford const OptionDefinition * 3813222b937cSEugene Zelenko GetDefinitions() override 3814a0f08674SEwan Crawford { 3815a0f08674SEwan Crawford return g_option_table; 3816a0f08674SEwan Crawford } 3817a0f08674SEwan Crawford 3818a0f08674SEwan Crawford static OptionDefinition g_option_table[]; 3819a0f08674SEwan Crawford FileSpec m_outfile; 3820a0f08674SEwan Crawford }; 3821a0f08674SEwan Crawford 3822a0f08674SEwan Crawford bool 3823222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 3824a0f08674SEwan Crawford { 3825a0f08674SEwan Crawford const size_t argc = command.GetArgumentCount(); 3826a0f08674SEwan Crawford if (argc < 1) 3827a0f08674SEwan Crawford { 3828a0f08674SEwan Crawford result.AppendErrorWithFormat("'%s' takes 1 argument, an allocation ID. As well as an optional -f argument", 3829a0f08674SEwan Crawford m_cmd_name.c_str()); 3830a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3831a0f08674SEwan Crawford return false; 3832a0f08674SEwan Crawford } 3833a0f08674SEwan Crawford 3834b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 3835b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 3836a0f08674SEwan Crawford 3837a0f08674SEwan Crawford const char *id_cstr = command.GetArgumentAtIndex(0); 3838a0f08674SEwan Crawford bool convert_complete = false; 3839a0f08674SEwan Crawford const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete); 3840a0f08674SEwan Crawford if (!convert_complete) 3841a0f08674SEwan Crawford { 3842a0f08674SEwan Crawford result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr); 3843a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3844a0f08674SEwan Crawford return false; 3845a0f08674SEwan Crawford } 3846a0f08674SEwan Crawford 3847a0f08674SEwan Crawford Stream *output_strm = nullptr; 3848a0f08674SEwan Crawford StreamFile outfile_stream; 3849a0f08674SEwan Crawford const FileSpec &outfile_spec = m_options.m_outfile; // Dump allocation to file instead 3850a0f08674SEwan Crawford if (outfile_spec) 3851a0f08674SEwan Crawford { 3852a0f08674SEwan Crawford // Open output file 3853a0f08674SEwan Crawford char path[256]; 3854a0f08674SEwan Crawford outfile_spec.GetPath(path, sizeof(path)); 3855a0f08674SEwan Crawford if (outfile_stream.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate).Success()) 3856a0f08674SEwan Crawford { 3857a0f08674SEwan Crawford output_strm = &outfile_stream; 3858a0f08674SEwan Crawford result.GetOutputStream().Printf("Results written to '%s'", path); 3859a0f08674SEwan Crawford result.GetOutputStream().EOL(); 3860a0f08674SEwan Crawford } 3861a0f08674SEwan Crawford else 3862a0f08674SEwan Crawford { 3863a0f08674SEwan Crawford result.AppendErrorWithFormat("Couldn't open file '%s'", path); 3864a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3865a0f08674SEwan Crawford return false; 3866a0f08674SEwan Crawford } 3867a0f08674SEwan Crawford } 3868a0f08674SEwan Crawford else 3869a0f08674SEwan Crawford output_strm = &result.GetOutputStream(); 3870a0f08674SEwan Crawford 3871a0f08674SEwan Crawford assert(output_strm != nullptr); 3872a0f08674SEwan Crawford bool success = runtime->DumpAllocation(*output_strm, m_exe_ctx.GetFramePtr(), id); 3873a0f08674SEwan Crawford 3874a0f08674SEwan Crawford if (success) 3875a0f08674SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 3876a0f08674SEwan Crawford else 3877a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3878a0f08674SEwan Crawford 3879a0f08674SEwan Crawford return true; 3880a0f08674SEwan Crawford } 3881a0f08674SEwan Crawford 3882a0f08674SEwan Crawford private: 3883a0f08674SEwan Crawford CommandOptions m_options; 3884a0f08674SEwan Crawford }; 3885a0f08674SEwan Crawford 3886b3f7f69dSAidan Dodds OptionDefinition CommandObjectRenderScriptRuntimeAllocationDump::CommandOptions::g_option_table[] = { 3887b3f7f69dSAidan Dodds {LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFilename, 3888a0f08674SEwan Crawford "Print results to specified file instead of command line."}, 3889b3f7f69dSAidan Dodds {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}}; 3890a0f08674SEwan Crawford 389115f2bd95SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationList : public CommandObjectParsed 389215f2bd95SEwan Crawford { 389315f2bd95SEwan Crawford public: 389415f2bd95SEwan Crawford CommandObjectRenderScriptRuntimeAllocationList(CommandInterpreter &interpreter) 389515f2bd95SEwan Crawford : CommandObjectParsed(interpreter, "renderscript allocation list", 389615f2bd95SEwan Crawford "List renderscript allocations and their information.", "renderscript allocation list", 3897b3f7f69dSAidan Dodds eCommandRequiresProcess | eCommandProcessMustBeLaunched), 3898b3f7f69dSAidan Dodds m_options(interpreter) 389915f2bd95SEwan Crawford { 390015f2bd95SEwan Crawford } 390115f2bd95SEwan Crawford 3902222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationList() override = default; 3903222b937cSEugene Zelenko 3904222b937cSEugene Zelenko Options * 3905222b937cSEugene Zelenko GetOptions() override 390615f2bd95SEwan Crawford { 390715f2bd95SEwan Crawford return &m_options; 390815f2bd95SEwan Crawford } 390915f2bd95SEwan Crawford 391015f2bd95SEwan Crawford class CommandOptions : public Options 391115f2bd95SEwan Crawford { 391215f2bd95SEwan Crawford public: 3913b649b005SEwan Crawford CommandOptions(CommandInterpreter &interpreter) : Options(interpreter), m_id(0) {} 391415f2bd95SEwan Crawford 3915222b937cSEugene Zelenko ~CommandOptions() override = default; 391615f2bd95SEwan Crawford 3917222b937cSEugene Zelenko Error 3918222b937cSEugene Zelenko SetOptionValue(uint32_t option_idx, const char *option_arg) override 391915f2bd95SEwan Crawford { 392015f2bd95SEwan Crawford Error error; 392115f2bd95SEwan Crawford const int short_option = m_getopt_table[option_idx].val; 392215f2bd95SEwan Crawford 392315f2bd95SEwan Crawford switch (short_option) 392415f2bd95SEwan Crawford { 3925b649b005SEwan Crawford case 'i': 3926b649b005SEwan Crawford bool success; 3927b649b005SEwan Crawford m_id = StringConvert::ToUInt32(option_arg, 0, 0, &success); 3928b649b005SEwan Crawford if (!success) 3929b649b005SEwan Crawford error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option); 393015f2bd95SEwan Crawford break; 393115f2bd95SEwan Crawford default: 393215f2bd95SEwan Crawford error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 393315f2bd95SEwan Crawford break; 393415f2bd95SEwan Crawford } 393515f2bd95SEwan Crawford return error; 393615f2bd95SEwan Crawford } 393715f2bd95SEwan Crawford 393815f2bd95SEwan Crawford void 3939222b937cSEugene Zelenko OptionParsingStarting() override 394015f2bd95SEwan Crawford { 3941b649b005SEwan Crawford m_id = 0; 394215f2bd95SEwan Crawford } 394315f2bd95SEwan Crawford 394415f2bd95SEwan Crawford const OptionDefinition * 3945222b937cSEugene Zelenko GetDefinitions() override 394615f2bd95SEwan Crawford { 394715f2bd95SEwan Crawford return g_option_table; 394815f2bd95SEwan Crawford } 394915f2bd95SEwan Crawford 395015f2bd95SEwan Crawford static OptionDefinition g_option_table[]; 3951b649b005SEwan Crawford uint32_t m_id; 395215f2bd95SEwan Crawford }; 395315f2bd95SEwan Crawford 395415f2bd95SEwan Crawford bool 3955222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 395615f2bd95SEwan Crawford { 3957b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 3958b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 3959b649b005SEwan Crawford runtime->ListAllocations(result.GetOutputStream(), m_exe_ctx.GetFramePtr(), m_options.m_id); 396015f2bd95SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 396115f2bd95SEwan Crawford return true; 396215f2bd95SEwan Crawford } 396315f2bd95SEwan Crawford 396415f2bd95SEwan Crawford private: 396515f2bd95SEwan Crawford CommandOptions m_options; 396615f2bd95SEwan Crawford }; 396715f2bd95SEwan Crawford 3968b649b005SEwan Crawford OptionDefinition CommandObjectRenderScriptRuntimeAllocationList::CommandOptions::g_option_table[] = { 3969b3f7f69dSAidan Dodds {LLDB_OPT_SET_1, false, "id", 'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeIndex, 3970b649b005SEwan Crawford "Only show details of a single allocation with specified id."}, 3971b3f7f69dSAidan Dodds {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}}; 397215f2bd95SEwan Crawford 397355232f09SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationLoad : public CommandObjectParsed 397455232f09SEwan Crawford { 397555232f09SEwan Crawford public: 397655232f09SEwan Crawford CommandObjectRenderScriptRuntimeAllocationLoad(CommandInterpreter &interpreter) 3977b3f7f69dSAidan Dodds : CommandObjectParsed( 3978b3f7f69dSAidan Dodds interpreter, "renderscript allocation load", "Loads renderscript allocation contents from a file.", 3979b3f7f69dSAidan Dodds "renderscript allocation load <ID> <filename>", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 398055232f09SEwan Crawford { 398155232f09SEwan Crawford } 398255232f09SEwan Crawford 3983222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationLoad() override = default; 398455232f09SEwan Crawford 398555232f09SEwan Crawford bool 3986222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 398755232f09SEwan Crawford { 398855232f09SEwan Crawford const size_t argc = command.GetArgumentCount(); 398955232f09SEwan Crawford if (argc != 2) 399055232f09SEwan Crawford { 3991b3f7f69dSAidan Dodds result.AppendErrorWithFormat("'%s' takes 2 arguments, an allocation ID and filename to read from.", 3992b3f7f69dSAidan Dodds m_cmd_name.c_str()); 399355232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 399455232f09SEwan Crawford return false; 399555232f09SEwan Crawford } 399655232f09SEwan Crawford 3997b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 3998b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 399955232f09SEwan Crawford 400055232f09SEwan Crawford const char *id_cstr = command.GetArgumentAtIndex(0); 400155232f09SEwan Crawford bool convert_complete = false; 400255232f09SEwan Crawford const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete); 400355232f09SEwan Crawford if (!convert_complete) 400455232f09SEwan Crawford { 400555232f09SEwan Crawford result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr); 400655232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 400755232f09SEwan Crawford return false; 400855232f09SEwan Crawford } 400955232f09SEwan Crawford 401055232f09SEwan Crawford const char *filename = command.GetArgumentAtIndex(1); 401155232f09SEwan Crawford bool success = runtime->LoadAllocation(result.GetOutputStream(), id, filename, m_exe_ctx.GetFramePtr()); 401255232f09SEwan Crawford 401355232f09SEwan Crawford if (success) 401455232f09SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 401555232f09SEwan Crawford else 401655232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 401755232f09SEwan Crawford 401855232f09SEwan Crawford return true; 401955232f09SEwan Crawford } 402055232f09SEwan Crawford }; 402155232f09SEwan Crawford 402255232f09SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationSave : public CommandObjectParsed 402355232f09SEwan Crawford { 402455232f09SEwan Crawford public: 402555232f09SEwan Crawford CommandObjectRenderScriptRuntimeAllocationSave(CommandInterpreter &interpreter) 4026b3f7f69dSAidan Dodds : CommandObjectParsed( 4027b3f7f69dSAidan Dodds interpreter, "renderscript allocation save", "Write renderscript allocation contents to a file.", 4028b3f7f69dSAidan Dodds "renderscript allocation save <ID> <filename>", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 402955232f09SEwan Crawford { 403055232f09SEwan Crawford } 403155232f09SEwan Crawford 4032222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationSave() override = default; 403355232f09SEwan Crawford 403455232f09SEwan Crawford bool 4035222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 403655232f09SEwan Crawford { 403755232f09SEwan Crawford const size_t argc = command.GetArgumentCount(); 403855232f09SEwan Crawford if (argc != 2) 403955232f09SEwan Crawford { 4040b3f7f69dSAidan Dodds result.AppendErrorWithFormat("'%s' takes 2 arguments, an allocation ID and filename to read from.", 4041b3f7f69dSAidan Dodds m_cmd_name.c_str()); 404255232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 404355232f09SEwan Crawford return false; 404455232f09SEwan Crawford } 404555232f09SEwan Crawford 4046b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 4047b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 404855232f09SEwan Crawford 404955232f09SEwan Crawford const char *id_cstr = command.GetArgumentAtIndex(0); 405055232f09SEwan Crawford bool convert_complete = false; 405155232f09SEwan Crawford const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete); 405255232f09SEwan Crawford if (!convert_complete) 405355232f09SEwan Crawford { 405455232f09SEwan Crawford result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr); 405555232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 405655232f09SEwan Crawford return false; 405755232f09SEwan Crawford } 405855232f09SEwan Crawford 405955232f09SEwan Crawford const char *filename = command.GetArgumentAtIndex(1); 406055232f09SEwan Crawford bool success = runtime->SaveAllocation(result.GetOutputStream(), id, filename, m_exe_ctx.GetFramePtr()); 406155232f09SEwan Crawford 406255232f09SEwan Crawford if (success) 406355232f09SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 406455232f09SEwan Crawford else 406555232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 406655232f09SEwan Crawford 406755232f09SEwan Crawford return true; 406855232f09SEwan Crawford } 406955232f09SEwan Crawford }; 407055232f09SEwan Crawford 40710d2bfcfbSEwan Crawford class CommandObjectRenderScriptRuntimeAllocationRefresh : public CommandObjectParsed 40720d2bfcfbSEwan Crawford { 40730d2bfcfbSEwan Crawford public: 40740d2bfcfbSEwan Crawford CommandObjectRenderScriptRuntimeAllocationRefresh(CommandInterpreter &interpreter) 40750d2bfcfbSEwan Crawford : CommandObjectParsed(interpreter, "renderscript allocation refresh", 40760d2bfcfbSEwan Crawford "Recomputes the details of all allocations.", "renderscript allocation refresh", 40770d2bfcfbSEwan Crawford eCommandRequiresProcess | eCommandProcessMustBeLaunched) 40780d2bfcfbSEwan Crawford { 40790d2bfcfbSEwan Crawford } 40800d2bfcfbSEwan Crawford 40810d2bfcfbSEwan Crawford ~CommandObjectRenderScriptRuntimeAllocationRefresh() override = default; 40820d2bfcfbSEwan Crawford 40830d2bfcfbSEwan Crawford bool 40840d2bfcfbSEwan Crawford DoExecute(Args &command, CommandReturnObject &result) override 40850d2bfcfbSEwan Crawford { 40860d2bfcfbSEwan Crawford RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 40870d2bfcfbSEwan Crawford m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 40880d2bfcfbSEwan Crawford 40890d2bfcfbSEwan Crawford bool success = runtime->RecomputeAllAllocations(result.GetOutputStream(), m_exe_ctx.GetFramePtr()); 40900d2bfcfbSEwan Crawford 40910d2bfcfbSEwan Crawford if (success) 40920d2bfcfbSEwan Crawford { 40930d2bfcfbSEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 40940d2bfcfbSEwan Crawford return true; 40950d2bfcfbSEwan Crawford } 40960d2bfcfbSEwan Crawford else 40970d2bfcfbSEwan Crawford { 40980d2bfcfbSEwan Crawford result.SetStatus(eReturnStatusFailed); 40990d2bfcfbSEwan Crawford return false; 41000d2bfcfbSEwan Crawford } 41010d2bfcfbSEwan Crawford } 41020d2bfcfbSEwan Crawford }; 41030d2bfcfbSEwan Crawford 410415f2bd95SEwan Crawford class CommandObjectRenderScriptRuntimeAllocation : public CommandObjectMultiword 410515f2bd95SEwan Crawford { 410615f2bd95SEwan Crawford public: 410715f2bd95SEwan Crawford CommandObjectRenderScriptRuntimeAllocation(CommandInterpreter &interpreter) 4108b3f7f69dSAidan Dodds : CommandObjectMultiword(interpreter, "renderscript allocation", 4109b3f7f69dSAidan Dodds "Commands that deal with renderscript allocations.", nullptr) 411015f2bd95SEwan Crawford { 411115f2bd95SEwan Crawford LoadSubCommand("list", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationList(interpreter))); 4112a0f08674SEwan Crawford LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationDump(interpreter))); 411355232f09SEwan Crawford LoadSubCommand("save", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationSave(interpreter))); 411455232f09SEwan Crawford LoadSubCommand("load", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationLoad(interpreter))); 41150d2bfcfbSEwan Crawford LoadSubCommand("refresh", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationRefresh(interpreter))); 411615f2bd95SEwan Crawford } 411715f2bd95SEwan Crawford 4118222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocation() override = default; 411915f2bd95SEwan Crawford }; 412015f2bd95SEwan Crawford 41214640cde1SColin Riley class CommandObjectRenderScriptRuntimeStatus : public CommandObjectParsed 41224640cde1SColin Riley { 41234640cde1SColin Riley public: 41244640cde1SColin Riley CommandObjectRenderScriptRuntimeStatus(CommandInterpreter &interpreter) 4125b3f7f69dSAidan Dodds : CommandObjectParsed(interpreter, "renderscript status", "Displays current renderscript runtime status.", 4126b3f7f69dSAidan Dodds "renderscript status", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 41274640cde1SColin Riley { 41284640cde1SColin Riley } 41294640cde1SColin Riley 4130222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeStatus() override = default; 41314640cde1SColin Riley 41324640cde1SColin Riley bool 4133222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 41344640cde1SColin Riley { 41354640cde1SColin Riley RenderScriptRuntime *runtime = 41364640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 41374640cde1SColin Riley runtime->Status(result.GetOutputStream()); 41384640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 41394640cde1SColin Riley return true; 41404640cde1SColin Riley } 41414640cde1SColin Riley }; 41424640cde1SColin Riley 41435ec532a9SColin Riley class CommandObjectRenderScriptRuntime : public CommandObjectMultiword 41445ec532a9SColin Riley { 41455ec532a9SColin Riley public: 41465ec532a9SColin Riley CommandObjectRenderScriptRuntime(CommandInterpreter &interpreter) 41475ec532a9SColin Riley : CommandObjectMultiword(interpreter, "renderscript", "A set of commands for operating on renderscript.", 41485ec532a9SColin Riley "renderscript <subcommand> [<subcommand-options>]") 41495ec532a9SColin Riley { 41505ec532a9SColin Riley LoadSubCommand("module", CommandObjectSP(new CommandObjectRenderScriptRuntimeModule(interpreter))); 41514640cde1SColin Riley LoadSubCommand("status", CommandObjectSP(new CommandObjectRenderScriptRuntimeStatus(interpreter))); 41524640cde1SColin Riley LoadSubCommand("kernel", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernel(interpreter))); 41534640cde1SColin Riley LoadSubCommand("context", CommandObjectSP(new CommandObjectRenderScriptRuntimeContext(interpreter))); 415415f2bd95SEwan Crawford LoadSubCommand("allocation", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocation(interpreter))); 41555ec532a9SColin Riley } 41565ec532a9SColin Riley 4157222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntime() override = default; 41585ec532a9SColin Riley }; 4159ef20b08fSColin Riley 4160ef20b08fSColin Riley void 4161ef20b08fSColin Riley RenderScriptRuntime::Initiate() 41625ec532a9SColin Riley { 4163ef20b08fSColin Riley assert(!m_initiated); 41645ec532a9SColin Riley } 4165ef20b08fSColin Riley 4166ef20b08fSColin Riley RenderScriptRuntime::RenderScriptRuntime(Process *process) 4167b3f7f69dSAidan Dodds : lldb_private::CPPLanguageRuntime(process), 4168b3f7f69dSAidan Dodds m_initiated(false), 4169b3f7f69dSAidan Dodds m_debuggerPresentFlagged(false), 41707dc7771cSEwan Crawford m_breakAllKernels(false) 4171ef20b08fSColin Riley { 41724640cde1SColin Riley ModulesDidLoad(process->GetTarget().GetImages()); 4173ef20b08fSColin Riley } 41744640cde1SColin Riley 41754640cde1SColin Riley lldb::CommandObjectSP 41764640cde1SColin Riley RenderScriptRuntime::GetCommandObject(lldb_private::CommandInterpreter &interpreter) 41774640cde1SColin Riley { 41780a66e2f1SEnrico Granata return CommandObjectSP(new CommandObjectRenderScriptRuntime(interpreter)); 41794640cde1SColin Riley } 41804640cde1SColin Riley 418178f339d1SEwan Crawford RenderScriptRuntime::~RenderScriptRuntime() = default; 4182