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 115*f4786785SAidan Dodds // ArgItem is used by the GetArgs() function when reading function arguments from the target. 116*f4786785SAidan Dodds struct ArgItem 117*f4786785SAidan Dodds { 118*f4786785SAidan Dodds enum 119*f4786785SAidan Dodds { 120*f4786785SAidan Dodds ePointer, 121*f4786785SAidan Dodds eInt32, 122*f4786785SAidan Dodds eInt64, 123*f4786785SAidan Dodds eLong, 124*f4786785SAidan Dodds eBool 125*f4786785SAidan Dodds } type; 126*f4786785SAidan Dodds 127*f4786785SAidan Dodds uint64_t value; 128*f4786785SAidan Dodds 129*f4786785SAidan Dodds explicit operator uint64_t() const { return value; } 130*f4786785SAidan Dodds }; 131*f4786785SAidan Dodds 132*f4786785SAidan Dodds // Context structure to be passed into GetArgsXXX(), argument reading functions below. 133*f4786785SAidan Dodds struct GetArgsCtx 134*f4786785SAidan Dodds { 135*f4786785SAidan Dodds RegisterContext *reg_ctx; 136*f4786785SAidan Dodds Process *process; 137*f4786785SAidan Dodds }; 138*f4786785SAidan Dodds 139*f4786785SAidan Dodds bool 140*f4786785SAidan Dodds GetArgsX86(const GetArgsCtx &ctx, ArgItem *arg_list, size_t num_args) 141*f4786785SAidan Dodds { 142*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 143*f4786785SAidan Dodds 144*f4786785SAidan Dodds // get the current stack pointer 145*f4786785SAidan Dodds uint64_t sp = ctx.reg_ctx->GetSP(); 146*f4786785SAidan Dodds 147*f4786785SAidan Dodds for (size_t i = 0; i < num_args; ++i) 148*f4786785SAidan Dodds { 149*f4786785SAidan Dodds ArgItem &arg = arg_list[i]; 150*f4786785SAidan Dodds // advance up the stack by one argument 151*f4786785SAidan Dodds sp += sizeof(uint32_t); 152*f4786785SAidan Dodds // get the argument type size 153*f4786785SAidan Dodds size_t arg_size = sizeof(uint32_t); 154*f4786785SAidan Dodds // read the argument from memory 155*f4786785SAidan Dodds arg.value = 0; 156*f4786785SAidan Dodds Error error; 157*f4786785SAidan Dodds size_t read = ctx.process->ReadMemory(sp, &arg.value, sizeof(uint32_t), error); 158*f4786785SAidan Dodds if (read != arg_size || !error.Success()) 159*f4786785SAidan Dodds { 160*f4786785SAidan Dodds if (log) 161*f4786785SAidan Dodds log->Printf("%s - error reading argument: %" PRIu64 " '%s'", __FUNCTION__, uint64_t(i), 162*f4786785SAidan Dodds error.AsCString()); 163*f4786785SAidan Dodds return false; 164*f4786785SAidan Dodds } 165*f4786785SAidan Dodds } 166*f4786785SAidan Dodds return true; 167*f4786785SAidan Dodds } 168*f4786785SAidan Dodds 169*f4786785SAidan Dodds bool 170*f4786785SAidan Dodds GetArgsX86_64(GetArgsCtx &ctx, ArgItem *arg_list, size_t num_args) 171*f4786785SAidan Dodds { 172*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 173*f4786785SAidan Dodds 174*f4786785SAidan Dodds // number of arguments passed in registers 175*f4786785SAidan Dodds static const uint32_t c_args_in_reg = 6; 176*f4786785SAidan Dodds // register passing order 177*f4786785SAidan Dodds static const std::array<const char *, c_args_in_reg> c_reg_names = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"}; 178*f4786785SAidan Dodds // argument type to size mapping 179*f4786785SAidan Dodds static const std::array<size_t, 5> arg_size = { 180*f4786785SAidan Dodds 8, // ePointer, 181*f4786785SAidan Dodds 4, // eInt32, 182*f4786785SAidan Dodds 8, // eInt64, 183*f4786785SAidan Dodds 8, // eLong, 184*f4786785SAidan Dodds 4, // eBool, 185*f4786785SAidan Dodds }; 186*f4786785SAidan Dodds 187*f4786785SAidan Dodds // get the current stack pointer 188*f4786785SAidan Dodds uint64_t sp = ctx.reg_ctx->GetSP(); 189*f4786785SAidan Dodds // step over the return address 190*f4786785SAidan Dodds sp += sizeof(uint64_t); 191*f4786785SAidan Dodds 192*f4786785SAidan Dodds // check the stack alignment was correct (16 byte aligned) 193*f4786785SAidan Dodds if ((sp & 0xf) != 0x0) 194*f4786785SAidan Dodds { 195*f4786785SAidan Dodds if (log) 196*f4786785SAidan Dodds log->Printf("%s - stack misaligned", __FUNCTION__); 197*f4786785SAidan Dodds return false; 198*f4786785SAidan Dodds } 199*f4786785SAidan Dodds 200*f4786785SAidan Dodds // find the start of arguments on the stack 201*f4786785SAidan Dodds uint64_t sp_offset = 0; 202*f4786785SAidan Dodds for (uint32_t i = c_args_in_reg; i < num_args; ++i) 203*f4786785SAidan Dodds { 204*f4786785SAidan Dodds sp_offset += arg_size[arg_list[i].type]; 205*f4786785SAidan Dodds } 206*f4786785SAidan Dodds // round up to multiple of 16 207*f4786785SAidan Dodds sp_offset = (sp_offset + 0xf) & 0xf; 208*f4786785SAidan Dodds sp += sp_offset; 209*f4786785SAidan Dodds 210*f4786785SAidan Dodds for (size_t i = 0; i < num_args; ++i) 211*f4786785SAidan Dodds { 212*f4786785SAidan Dodds bool success = false; 213*f4786785SAidan Dodds ArgItem &arg = arg_list[i]; 214*f4786785SAidan Dodds // arguments passed in registers 215*f4786785SAidan Dodds if (i < c_args_in_reg) 216*f4786785SAidan Dodds { 217*f4786785SAidan Dodds const RegisterInfo *rArg = ctx.reg_ctx->GetRegisterInfoByName(c_reg_names[i]); 218*f4786785SAidan Dodds RegisterValue rVal; 219*f4786785SAidan Dodds if (ctx.reg_ctx->ReadRegister(rArg, rVal)) 220*f4786785SAidan Dodds arg.value = rVal.GetAsUInt64(0, &success); 221*f4786785SAidan Dodds } 222*f4786785SAidan Dodds // arguments passed on the stack 223*f4786785SAidan Dodds else 224*f4786785SAidan Dodds { 225*f4786785SAidan Dodds // get the argument type size 226*f4786785SAidan Dodds const size_t size = arg_size[arg_list[i].type]; 227*f4786785SAidan Dodds // read the argument from memory 228*f4786785SAidan Dodds arg.value = 0; 229*f4786785SAidan Dodds // note: due to little endian layout reading 4 or 8 bytes will give the correct value. 230*f4786785SAidan Dodds Error error; 231*f4786785SAidan Dodds size_t read = ctx.process->ReadMemory(sp, &arg.value, size, error); 232*f4786785SAidan Dodds success = (error.Success() && read==size); 233*f4786785SAidan Dodds // advance past this argument 234*f4786785SAidan Dodds sp -= size; 235*f4786785SAidan Dodds } 236*f4786785SAidan Dodds // fail if we couldn't read this argument 237*f4786785SAidan Dodds if (!success) 238*f4786785SAidan Dodds { 239*f4786785SAidan Dodds if (log) 240*f4786785SAidan Dodds log->Printf("%s - error reading argument: %" PRIu64, __FUNCTION__, uint64_t(i)); 241*f4786785SAidan Dodds return false; 242*f4786785SAidan Dodds } 243*f4786785SAidan Dodds } 244*f4786785SAidan Dodds return true; 245*f4786785SAidan Dodds } 246*f4786785SAidan Dodds 247*f4786785SAidan Dodds bool 248*f4786785SAidan Dodds GetArgsArm(GetArgsCtx &ctx, ArgItem *arg_list, size_t num_args) 249*f4786785SAidan Dodds { 250*f4786785SAidan Dodds // number of arguments passed in registers 251*f4786785SAidan Dodds static const uint32_t c_args_in_reg = 4; 252*f4786785SAidan Dodds 253*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 254*f4786785SAidan Dodds 255*f4786785SAidan Dodds // get the current stack pointer 256*f4786785SAidan Dodds uint64_t sp = ctx.reg_ctx->GetSP(); 257*f4786785SAidan Dodds 258*f4786785SAidan Dodds for (size_t i = 0; i < num_args; ++i) 259*f4786785SAidan Dodds { 260*f4786785SAidan Dodds bool success = false; 261*f4786785SAidan Dodds ArgItem &arg = arg_list[i]; 262*f4786785SAidan Dodds // arguments passed in registers 263*f4786785SAidan Dodds if (i < c_args_in_reg) 264*f4786785SAidan Dodds { 265*f4786785SAidan Dodds const RegisterInfo *rArg = ctx.reg_ctx->GetRegisterInfoAtIndex(i); 266*f4786785SAidan Dodds RegisterValue rVal; 267*f4786785SAidan Dodds if (ctx.reg_ctx->ReadRegister(rArg, rVal)) 268*f4786785SAidan Dodds arg.value = rVal.GetAsUInt32(0, &success); 269*f4786785SAidan Dodds } 270*f4786785SAidan Dodds // arguments passed on the stack 271*f4786785SAidan Dodds else 272*f4786785SAidan Dodds { 273*f4786785SAidan Dodds // get the argument type size 274*f4786785SAidan Dodds const size_t arg_size = sizeof(uint32_t); 275*f4786785SAidan Dodds // clear all 64bits 276*f4786785SAidan Dodds arg.value = 0; 277*f4786785SAidan Dodds // read this argument from memory 278*f4786785SAidan Dodds Error error; 279*f4786785SAidan Dodds size_t bytes_read = ctx.process->ReadMemory(sp, &arg.value, arg_size, error); 280*f4786785SAidan Dodds success = (error.Success() && bytes_read == arg_size); 281*f4786785SAidan Dodds // advance the stack pointer 282*f4786785SAidan Dodds sp += sizeof(uint32_t); 283*f4786785SAidan Dodds } 284*f4786785SAidan Dodds // fail if we couldn't read this argument 285*f4786785SAidan Dodds if (!success) 286*f4786785SAidan Dodds { 287*f4786785SAidan Dodds if (log) 288*f4786785SAidan Dodds log->Printf("%s - error reading argument: %" PRIu64, __FUNCTION__, uint64_t(i)); 289*f4786785SAidan Dodds return false; 290*f4786785SAidan Dodds } 291*f4786785SAidan Dodds } 292*f4786785SAidan Dodds return true; 293*f4786785SAidan Dodds } 294*f4786785SAidan Dodds 295*f4786785SAidan Dodds bool 296*f4786785SAidan Dodds GetArgsAarch64(GetArgsCtx &ctx, ArgItem *arg_list, size_t num_args) 297*f4786785SAidan Dodds { 298*f4786785SAidan Dodds // number of arguments passed in registers 299*f4786785SAidan Dodds static const uint32_t c_args_in_reg = 8; 300*f4786785SAidan Dodds 301*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 302*f4786785SAidan Dodds 303*f4786785SAidan Dodds for (size_t i = 0; i < num_args; ++i) 304*f4786785SAidan Dodds { 305*f4786785SAidan Dodds bool success = false; 306*f4786785SAidan Dodds ArgItem &arg = arg_list[i]; 307*f4786785SAidan Dodds // arguments passed in registers 308*f4786785SAidan Dodds if (i < c_args_in_reg) 309*f4786785SAidan Dodds { 310*f4786785SAidan Dodds const RegisterInfo *rArg = ctx.reg_ctx->GetRegisterInfoAtIndex(i); 311*f4786785SAidan Dodds RegisterValue rVal; 312*f4786785SAidan Dodds if (ctx.reg_ctx->ReadRegister(rArg, rVal)) 313*f4786785SAidan Dodds arg.value = rVal.GetAsUInt64(0, &success); 314*f4786785SAidan Dodds } 315*f4786785SAidan Dodds // arguments passed on the stack 316*f4786785SAidan Dodds else 317*f4786785SAidan Dodds { 318*f4786785SAidan Dodds if (log) 319*f4786785SAidan Dodds log->Printf("%s - reading arguments spilled to stack not implemented", __FUNCTION__); 320*f4786785SAidan Dodds } 321*f4786785SAidan Dodds // fail if we couldn't read this argument 322*f4786785SAidan Dodds if (!success) 323*f4786785SAidan Dodds { 324*f4786785SAidan Dodds if (log) 325*f4786785SAidan Dodds log->Printf("%s - error reading argument: %" PRIu64, __FUNCTION__, 326*f4786785SAidan Dodds uint64_t(i)); 327*f4786785SAidan Dodds return false; 328*f4786785SAidan Dodds } 329*f4786785SAidan Dodds } 330*f4786785SAidan Dodds return true; 331*f4786785SAidan Dodds } 332*f4786785SAidan Dodds 333*f4786785SAidan Dodds bool 334*f4786785SAidan Dodds GetArgsMipsel(GetArgsCtx &ctx, ArgItem *arg_list, size_t num_args) 335*f4786785SAidan Dodds { 336*f4786785SAidan Dodds // number of arguments passed in registers 337*f4786785SAidan Dodds static const uint32_t c_args_in_reg = 4; 338*f4786785SAidan Dodds // register file offset to first argument 339*f4786785SAidan Dodds static const uint32_t c_reg_offset = 4; 340*f4786785SAidan Dodds 341*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 342*f4786785SAidan Dodds 343*f4786785SAidan Dodds for (size_t i = 0; i < num_args; ++i) 344*f4786785SAidan Dodds { 345*f4786785SAidan Dodds bool success = false; 346*f4786785SAidan Dodds ArgItem &arg = arg_list[i]; 347*f4786785SAidan Dodds // arguments passed in registers 348*f4786785SAidan Dodds if (i < c_args_in_reg) 349*f4786785SAidan Dodds { 350*f4786785SAidan Dodds const RegisterInfo *rArg = ctx.reg_ctx->GetRegisterInfoAtIndex(i + c_reg_offset); 351*f4786785SAidan Dodds RegisterValue rVal; 352*f4786785SAidan Dodds if (ctx.reg_ctx->ReadRegister(rArg, rVal)) 353*f4786785SAidan Dodds arg.value = rVal.GetAsUInt64(0, &success); 354*f4786785SAidan Dodds } 355*f4786785SAidan Dodds // arguments passed on the stack 356*f4786785SAidan Dodds else 357*f4786785SAidan Dodds { 358*f4786785SAidan Dodds if (log) 359*f4786785SAidan Dodds log->Printf("%s - reading arguments spilled to stack not implemented.", __FUNCTION__); 360*f4786785SAidan Dodds } 361*f4786785SAidan Dodds // fail if we couldn't read this argument 362*f4786785SAidan Dodds if (!success) 363*f4786785SAidan Dodds { 364*f4786785SAidan Dodds if (log) 365*f4786785SAidan Dodds log->Printf("%s - error reading argument: %" PRIu64, __FUNCTION__, uint64_t(i)); 366*f4786785SAidan Dodds return false; 367*f4786785SAidan Dodds } 368*f4786785SAidan Dodds } 369*f4786785SAidan Dodds return true; 370*f4786785SAidan Dodds } 371*f4786785SAidan Dodds 372*f4786785SAidan Dodds bool 373*f4786785SAidan Dodds GetArgsMips64el(GetArgsCtx &ctx, ArgItem *arg_list, size_t num_args) 374*f4786785SAidan Dodds { 375*f4786785SAidan Dodds // number of arguments passed in registers 376*f4786785SAidan Dodds static const uint32_t c_args_in_reg = 8; 377*f4786785SAidan Dodds // register file offset to first argument 378*f4786785SAidan Dodds static const uint32_t c_reg_offset = 4; 379*f4786785SAidan Dodds 380*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 381*f4786785SAidan Dodds 382*f4786785SAidan Dodds // get the current stack pointer 383*f4786785SAidan Dodds uint64_t sp = ctx.reg_ctx->GetSP(); 384*f4786785SAidan Dodds 385*f4786785SAidan Dodds for (size_t i = 0; i < num_args; ++i) 386*f4786785SAidan Dodds { 387*f4786785SAidan Dodds bool success = false; 388*f4786785SAidan Dodds ArgItem &arg = arg_list[i]; 389*f4786785SAidan Dodds // arguments passed in registers 390*f4786785SAidan Dodds if (i < c_args_in_reg) 391*f4786785SAidan Dodds { 392*f4786785SAidan Dodds const RegisterInfo *rArg = ctx.reg_ctx->GetRegisterInfoAtIndex(i + c_reg_offset); 393*f4786785SAidan Dodds RegisterValue rVal; 394*f4786785SAidan Dodds if (ctx.reg_ctx->ReadRegister(rArg, rVal)) 395*f4786785SAidan Dodds arg.value = rVal.GetAsUInt32(0, &success); 396*f4786785SAidan Dodds } 397*f4786785SAidan Dodds // arguments passed on the stack 398*f4786785SAidan Dodds else 399*f4786785SAidan Dodds { 400*f4786785SAidan Dodds // get the argument type size 401*f4786785SAidan Dodds const size_t arg_size = sizeof(uint64_t); 402*f4786785SAidan Dodds // clear all 64bits 403*f4786785SAidan Dodds arg.value = 0; 404*f4786785SAidan Dodds // read this argument from memory 405*f4786785SAidan Dodds Error error; 406*f4786785SAidan Dodds size_t bytes_read = ctx.process->ReadMemory(sp, &arg.value, arg_size, error); 407*f4786785SAidan Dodds success = (error.Success() && bytes_read == arg_size); 408*f4786785SAidan Dodds // advance the stack pointer 409*f4786785SAidan Dodds sp += arg_size; 410*f4786785SAidan Dodds } 411*f4786785SAidan Dodds // fail if we couldn't read this argument 412*f4786785SAidan Dodds if (!success) 413*f4786785SAidan Dodds { 414*f4786785SAidan Dodds if (log) 415*f4786785SAidan Dodds log->Printf("%s - error reading argument: %" PRIu64, __FUNCTION__, uint64_t(i)); 416*f4786785SAidan Dodds return false; 417*f4786785SAidan Dodds } 418*f4786785SAidan Dodds } 419*f4786785SAidan Dodds return true; 420*f4786785SAidan Dodds } 421*f4786785SAidan Dodds 422*f4786785SAidan Dodds bool 423*f4786785SAidan Dodds GetArgs(ExecutionContext &context, ArgItem *arg_list, size_t num_args) 424*f4786785SAidan Dodds { 425*f4786785SAidan Dodds Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE); 426*f4786785SAidan Dodds 427*f4786785SAidan Dodds // verify that we have a target 428*f4786785SAidan Dodds if (!context.GetTargetPtr()) 429*f4786785SAidan Dodds { 430*f4786785SAidan Dodds if (log) 431*f4786785SAidan Dodds log->Printf("%s - invalid target", __FUNCTION__); 432*f4786785SAidan Dodds return false; 433*f4786785SAidan Dodds } 434*f4786785SAidan Dodds 435*f4786785SAidan Dodds GetArgsCtx ctx = {context.GetRegisterContext(), context.GetProcessPtr()}; 436*f4786785SAidan Dodds assert(ctx.reg_ctx && ctx.process); 437*f4786785SAidan Dodds 438*f4786785SAidan Dodds // dispatch based on architecture 439*f4786785SAidan Dodds switch (context.GetTargetPtr()->GetArchitecture().GetMachine()) 440*f4786785SAidan Dodds { 441*f4786785SAidan Dodds case llvm::Triple::ArchType::x86: 442*f4786785SAidan Dodds return GetArgsX86(ctx, arg_list, num_args); 443*f4786785SAidan Dodds 444*f4786785SAidan Dodds case llvm::Triple::ArchType::x86_64: 445*f4786785SAidan Dodds return GetArgsX86_64(ctx, arg_list, num_args); 446*f4786785SAidan Dodds 447*f4786785SAidan Dodds case llvm::Triple::ArchType::arm: 448*f4786785SAidan Dodds return GetArgsArm(ctx, arg_list, num_args); 449*f4786785SAidan Dodds 450*f4786785SAidan Dodds case llvm::Triple::ArchType::aarch64: 451*f4786785SAidan Dodds return GetArgsAarch64(ctx, arg_list, num_args); 452*f4786785SAidan Dodds 453*f4786785SAidan Dodds case llvm::Triple::ArchType::mipsel: 454*f4786785SAidan Dodds return GetArgsMipsel(ctx, arg_list, num_args); 455*f4786785SAidan Dodds 456*f4786785SAidan Dodds case llvm::Triple::ArchType::mips64el: 457*f4786785SAidan Dodds return GetArgsMips64el(ctx, arg_list, num_args); 458*f4786785SAidan Dodds 459*f4786785SAidan Dodds default: 460*f4786785SAidan Dodds // unsupported architecture 461*f4786785SAidan Dodds if (log) 462*f4786785SAidan Dodds { 463*f4786785SAidan Dodds log->Printf("%s - architecture not supported: '%s'", __FUNCTION__, 464*f4786785SAidan Dodds context.GetTargetRef().GetArchitecture().GetArchitectureName()); 465*f4786785SAidan Dodds } 466*f4786785SAidan Dodds return false; 467*f4786785SAidan Dodds } 468*f4786785SAidan Dodds } 469222b937cSEugene Zelenko } // anonymous namespace 47078f339d1SEwan Crawford 47178f339d1SEwan Crawford // The ScriptDetails class collects data associated with a single script instance. 47278f339d1SEwan Crawford struct RenderScriptRuntime::ScriptDetails 47378f339d1SEwan Crawford { 474222b937cSEugene Zelenko ~ScriptDetails() = default; 47578f339d1SEwan Crawford 47678f339d1SEwan Crawford enum ScriptType 47778f339d1SEwan Crawford { 47878f339d1SEwan Crawford eScript, 47978f339d1SEwan Crawford eScriptC 48078f339d1SEwan Crawford }; 48178f339d1SEwan Crawford 48278f339d1SEwan Crawford // The derived type of the script. 48378f339d1SEwan Crawford empirical_type<ScriptType> type; 48478f339d1SEwan Crawford // The name of the original source file. 48578f339d1SEwan Crawford empirical_type<std::string> resName; 48678f339d1SEwan Crawford // Path to script .so file on the device. 48778f339d1SEwan Crawford empirical_type<std::string> scriptDyLib; 48878f339d1SEwan Crawford // Directory where kernel objects are cached on device. 48978f339d1SEwan Crawford empirical_type<std::string> cacheDir; 49078f339d1SEwan Crawford // Pointer to the context which owns this script. 49178f339d1SEwan Crawford empirical_type<lldb::addr_t> context; 49278f339d1SEwan Crawford // Pointer to the script object itself. 49378f339d1SEwan Crawford empirical_type<lldb::addr_t> script; 49478f339d1SEwan Crawford }; 49578f339d1SEwan Crawford 4968b244e21SEwan Crawford // This Element class represents the Element object in RS, 4978b244e21SEwan Crawford // defining the type associated with an Allocation. 4988b244e21SEwan Crawford struct RenderScriptRuntime::Element 49978f339d1SEwan Crawford { 50015f2bd95SEwan Crawford // Taken from rsDefines.h 50115f2bd95SEwan Crawford enum DataKind 50215f2bd95SEwan Crawford { 50315f2bd95SEwan Crawford RS_KIND_USER, 50415f2bd95SEwan Crawford RS_KIND_PIXEL_L = 7, 50515f2bd95SEwan Crawford RS_KIND_PIXEL_A, 50615f2bd95SEwan Crawford RS_KIND_PIXEL_LA, 50715f2bd95SEwan Crawford RS_KIND_PIXEL_RGB, 50815f2bd95SEwan Crawford RS_KIND_PIXEL_RGBA, 50915f2bd95SEwan Crawford RS_KIND_PIXEL_DEPTH, 51015f2bd95SEwan Crawford RS_KIND_PIXEL_YUV, 51115f2bd95SEwan Crawford RS_KIND_INVALID = 100 51215f2bd95SEwan Crawford }; 51378f339d1SEwan Crawford 51415f2bd95SEwan Crawford // Taken from rsDefines.h 51578f339d1SEwan Crawford enum DataType 51678f339d1SEwan Crawford { 51715f2bd95SEwan Crawford RS_TYPE_NONE = 0, 51815f2bd95SEwan Crawford RS_TYPE_FLOAT_16, 51915f2bd95SEwan Crawford RS_TYPE_FLOAT_32, 52015f2bd95SEwan Crawford RS_TYPE_FLOAT_64, 52115f2bd95SEwan Crawford RS_TYPE_SIGNED_8, 52215f2bd95SEwan Crawford RS_TYPE_SIGNED_16, 52315f2bd95SEwan Crawford RS_TYPE_SIGNED_32, 52415f2bd95SEwan Crawford RS_TYPE_SIGNED_64, 52515f2bd95SEwan Crawford RS_TYPE_UNSIGNED_8, 52615f2bd95SEwan Crawford RS_TYPE_UNSIGNED_16, 52715f2bd95SEwan Crawford RS_TYPE_UNSIGNED_32, 52815f2bd95SEwan Crawford RS_TYPE_UNSIGNED_64, 5292e920715SEwan Crawford RS_TYPE_BOOLEAN, 5302e920715SEwan Crawford 5312e920715SEwan Crawford RS_TYPE_UNSIGNED_5_6_5, 5322e920715SEwan Crawford RS_TYPE_UNSIGNED_5_5_5_1, 5332e920715SEwan Crawford RS_TYPE_UNSIGNED_4_4_4_4, 5342e920715SEwan Crawford 5352e920715SEwan Crawford RS_TYPE_MATRIX_4X4, 5362e920715SEwan Crawford RS_TYPE_MATRIX_3X3, 5372e920715SEwan Crawford RS_TYPE_MATRIX_2X2, 5382e920715SEwan Crawford 5392e920715SEwan Crawford RS_TYPE_ELEMENT = 1000, 5402e920715SEwan Crawford RS_TYPE_TYPE, 5412e920715SEwan Crawford RS_TYPE_ALLOCATION, 5422e920715SEwan Crawford RS_TYPE_SAMPLER, 5432e920715SEwan Crawford RS_TYPE_SCRIPT, 5442e920715SEwan Crawford RS_TYPE_MESH, 5452e920715SEwan Crawford RS_TYPE_PROGRAM_FRAGMENT, 5462e920715SEwan Crawford RS_TYPE_PROGRAM_VERTEX, 5472e920715SEwan Crawford RS_TYPE_PROGRAM_RASTER, 5482e920715SEwan Crawford RS_TYPE_PROGRAM_STORE, 5492e920715SEwan Crawford RS_TYPE_FONT, 5502e920715SEwan Crawford 5512e920715SEwan Crawford RS_TYPE_INVALID = 10000 55278f339d1SEwan Crawford }; 55378f339d1SEwan Crawford 5548b244e21SEwan Crawford std::vector<Element> children; // Child Element fields for structs 5558b244e21SEwan Crawford empirical_type<lldb::addr_t> element_ptr; // Pointer to the RS Element of the Type 5568b244e21SEwan Crawford empirical_type<DataType> type; // Type of each data pointer stored by the allocation 5578b244e21SEwan Crawford empirical_type<DataKind> type_kind; // Defines pixel type if Allocation is created from an image 5588b244e21SEwan Crawford empirical_type<uint32_t> type_vec_size; // Vector size of each data point, e.g '4' for uchar4 5598b244e21SEwan Crawford empirical_type<uint32_t> field_count; // Number of Subelements 5608b244e21SEwan Crawford empirical_type<uint32_t> datum_size; // Size of a single Element with padding 5618b244e21SEwan Crawford empirical_type<uint32_t> padding; // Number of padding bytes 5628b244e21SEwan Crawford empirical_type<uint32_t> array_size; // Number of items in array, only needed for strucrs 5638b244e21SEwan Crawford ConstString type_name; // Name of type, only needed for structs 5648b244e21SEwan Crawford 565b3f7f69dSAidan Dodds static const ConstString & 566b3f7f69dSAidan Dodds GetFallbackStructName(); // Print this as the type name of a struct Element 5678b244e21SEwan Crawford // If we can't resolve the actual struct name 5688b59062aSEwan Crawford 569b3f7f69dSAidan Dodds bool 570b3f7f69dSAidan Dodds shouldRefresh() const 5718b59062aSEwan Crawford { 5728b59062aSEwan Crawford const bool valid_ptr = element_ptr.isValid() && *element_ptr.get() != 0x0; 5738b59062aSEwan Crawford const bool valid_type = type.isValid() && type_vec_size.isValid() && type_kind.isValid(); 5748b59062aSEwan Crawford return !valid_ptr || !valid_type || !datum_size.isValid(); 5758b59062aSEwan Crawford } 5768b244e21SEwan Crawford }; 5778b244e21SEwan Crawford 5788b244e21SEwan Crawford // This AllocationDetails class collects data associated with a single 5798b244e21SEwan Crawford // allocation instance. 5808b244e21SEwan Crawford struct RenderScriptRuntime::AllocationDetails 5818b244e21SEwan Crawford { 58215f2bd95SEwan Crawford struct Dimension 58378f339d1SEwan Crawford { 58415f2bd95SEwan Crawford uint32_t dim_1; 58515f2bd95SEwan Crawford uint32_t dim_2; 58615f2bd95SEwan Crawford uint32_t dim_3; 58715f2bd95SEwan Crawford uint32_t cubeMap; 58815f2bd95SEwan Crawford 58915f2bd95SEwan Crawford Dimension() 59015f2bd95SEwan Crawford { 59115f2bd95SEwan Crawford dim_1 = 0; 59215f2bd95SEwan Crawford dim_2 = 0; 59315f2bd95SEwan Crawford dim_3 = 0; 59415f2bd95SEwan Crawford cubeMap = 0; 59515f2bd95SEwan Crawford } 59678f339d1SEwan Crawford }; 59778f339d1SEwan Crawford 59826e52a70SEwan Crawford // The FileHeader struct specifies the header we use for writing allocations to a binary file. 59926e52a70SEwan Crawford // Our format begins with the ASCII characters "RSAD", identifying the file as an allocation dump. 60026e52a70SEwan Crawford // Member variables dims and hdr_size are then written consecutively, immediately followed by an instance of 60126e52a70SEwan Crawford // the ElementHeader struct. Because Elements can contain subelements, there may be more than one instance 60226e52a70SEwan Crawford // of the ElementHeader struct. With this first instance being the root element, and the other instances being 60326e52a70SEwan Crawford // the root's descendants. To identify which instances are an ElementHeader's children, each struct 60426e52a70SEwan Crawford // is immediately followed by a sequence of consecutive offsets to the start of its child structs. 60526e52a70SEwan Crawford // These offsets are 4 bytes in size, and the 0 offset signifies no more children. 60655232f09SEwan Crawford struct FileHeader 60755232f09SEwan Crawford { 60855232f09SEwan Crawford uint8_t ident[4]; // ASCII 'RSAD' identifying the file 60926e52a70SEwan Crawford uint32_t dims[3]; // Dimensions 61026e52a70SEwan Crawford uint16_t hdr_size; // Header size in bytes, including all element headers 61126e52a70SEwan Crawford }; 61226e52a70SEwan Crawford 61326e52a70SEwan Crawford struct ElementHeader 61426e52a70SEwan Crawford { 61555232f09SEwan Crawford uint16_t type; // DataType enum 61655232f09SEwan Crawford uint32_t kind; // DataKind enum 61755232f09SEwan Crawford uint32_t element_size; // Size of a single element, including padding 61826e52a70SEwan Crawford uint16_t vector_size; // Vector width 61926e52a70SEwan Crawford uint32_t array_size; // Number of elements in array 62055232f09SEwan Crawford }; 62155232f09SEwan Crawford 62215f2bd95SEwan Crawford // Monotonically increasing from 1 623b3f7f69dSAidan Dodds static uint32_t ID; 62415f2bd95SEwan Crawford 62515f2bd95SEwan Crawford // Maps Allocation DataType enum and vector size to printable strings 62615f2bd95SEwan Crawford // using mapping from RenderScript numerical types summary documentation 62715f2bd95SEwan Crawford static const char *RsDataTypeToString[][4]; 62815f2bd95SEwan Crawford 62915f2bd95SEwan Crawford // Maps Allocation DataKind enum to printable strings 63015f2bd95SEwan Crawford static const char *RsDataKindToString[]; 63115f2bd95SEwan Crawford 632a0f08674SEwan Crawford // Maps allocation types to format sizes for printing. 633b3f7f69dSAidan Dodds static const uint32_t RSTypeToFormat[][3]; 634a0f08674SEwan Crawford 63515f2bd95SEwan Crawford // Give each allocation an ID as a way 63615f2bd95SEwan Crawford // for commands to reference it. 637b3f7f69dSAidan Dodds const uint32_t id; 63815f2bd95SEwan Crawford 6398b244e21SEwan Crawford RenderScriptRuntime::Element element; // Allocation Element type 64015f2bd95SEwan Crawford empirical_type<Dimension> dimension; // Dimensions of the Allocation 64115f2bd95SEwan Crawford empirical_type<lldb::addr_t> address; // Pointer to address of the RS Allocation 64215f2bd95SEwan Crawford empirical_type<lldb::addr_t> data_ptr; // Pointer to the data held by the Allocation 64315f2bd95SEwan Crawford empirical_type<lldb::addr_t> type_ptr; // Pointer to the RS Type of the Allocation 64415f2bd95SEwan Crawford empirical_type<lldb::addr_t> context; // Pointer to the RS Context of the Allocation 645a0f08674SEwan Crawford empirical_type<uint32_t> size; // Size of the allocation 646a0f08674SEwan Crawford empirical_type<uint32_t> stride; // Stride between rows of the allocation 64715f2bd95SEwan Crawford 64815f2bd95SEwan Crawford // Give each allocation an id, so we can reference it in user commands. 649b3f7f69dSAidan Dodds AllocationDetails() : id(ID++) {} 6508b59062aSEwan Crawford 651b3f7f69dSAidan Dodds bool 652b3f7f69dSAidan Dodds shouldRefresh() const 6538b59062aSEwan Crawford { 6548b59062aSEwan Crawford bool valid_ptrs = data_ptr.isValid() && *data_ptr.get() != 0x0; 6558b59062aSEwan Crawford valid_ptrs = valid_ptrs && type_ptr.isValid() && *type_ptr.get() != 0x0; 6568b59062aSEwan Crawford return !valid_ptrs || !dimension.isValid() || !size.isValid() || element.shouldRefresh(); 6578b59062aSEwan Crawford } 65815f2bd95SEwan Crawford }; 65915f2bd95SEwan Crawford 660fe06b5adSAdrian McCarthy const ConstString & 661fe06b5adSAdrian McCarthy RenderScriptRuntime::Element::GetFallbackStructName() 662fe06b5adSAdrian McCarthy { 663fe06b5adSAdrian McCarthy static const ConstString FallbackStructName("struct"); 664fe06b5adSAdrian McCarthy return FallbackStructName; 665fe06b5adSAdrian McCarthy } 6668b244e21SEwan Crawford 667b3f7f69dSAidan Dodds uint32_t RenderScriptRuntime::AllocationDetails::ID = 1; 66815f2bd95SEwan Crawford 669b3f7f69dSAidan Dodds const char *RenderScriptRuntime::AllocationDetails::RsDataKindToString[] = { 67015f2bd95SEwan Crawford "User", 671b3f7f69dSAidan Dodds "Undefined", "Undefined", "Undefined", "Undefined", "Undefined", "Undefined", // Enum jumps from 0 to 7 672b3f7f69dSAidan Dodds "L Pixel", "A Pixel", "LA Pixel", "RGB Pixel", 673b3f7f69dSAidan Dodds "RGBA Pixel", "Pixel Depth", "YUV Pixel"}; 67415f2bd95SEwan Crawford 675b3f7f69dSAidan Dodds const char *RenderScriptRuntime::AllocationDetails::RsDataTypeToString[][4] = { 67615f2bd95SEwan Crawford {"None", "None", "None", "None"}, 67715f2bd95SEwan Crawford {"half", "half2", "half3", "half4"}, 67815f2bd95SEwan Crawford {"float", "float2", "float3", "float4"}, 67915f2bd95SEwan Crawford {"double", "double2", "double3", "double4"}, 68015f2bd95SEwan Crawford {"char", "char2", "char3", "char4"}, 68115f2bd95SEwan Crawford {"short", "short2", "short3", "short4"}, 68215f2bd95SEwan Crawford {"int", "int2", "int3", "int4"}, 68315f2bd95SEwan Crawford {"long", "long2", "long3", "long4"}, 68415f2bd95SEwan Crawford {"uchar", "uchar2", "uchar3", "uchar4"}, 68515f2bd95SEwan Crawford {"ushort", "ushort2", "ushort3", "ushort4"}, 68615f2bd95SEwan Crawford {"uint", "uint2", "uint3", "uint4"}, 68715f2bd95SEwan Crawford {"ulong", "ulong2", "ulong3", "ulong4"}, 6882e920715SEwan Crawford {"bool", "bool2", "bool3", "bool4"}, 6892e920715SEwan Crawford {"packed_565", "packed_565", "packed_565", "packed_565"}, 6902e920715SEwan Crawford {"packed_5551", "packed_5551", "packed_5551", "packed_5551"}, 6912e920715SEwan Crawford {"packed_4444", "packed_4444", "packed_4444", "packed_4444"}, 6922e920715SEwan Crawford {"rs_matrix4x4", "rs_matrix4x4", "rs_matrix4x4", "rs_matrix4x4"}, 6932e920715SEwan Crawford {"rs_matrix3x3", "rs_matrix3x3", "rs_matrix3x3", "rs_matrix3x3"}, 6942e920715SEwan Crawford {"rs_matrix2x2", "rs_matrix2x2", "rs_matrix2x2", "rs_matrix2x2"}, 6952e920715SEwan Crawford 6962e920715SEwan Crawford // Handlers 6972e920715SEwan Crawford {"RS Element", "RS Element", "RS Element", "RS Element"}, 6982e920715SEwan Crawford {"RS Type", "RS Type", "RS Type", "RS Type"}, 6992e920715SEwan Crawford {"RS Allocation", "RS Allocation", "RS Allocation", "RS Allocation"}, 7002e920715SEwan Crawford {"RS Sampler", "RS Sampler", "RS Sampler", "RS Sampler"}, 7012e920715SEwan Crawford {"RS Script", "RS Script", "RS Script", "RS Script"}, 7022e920715SEwan Crawford 7032e920715SEwan Crawford // Deprecated 7042e920715SEwan Crawford {"RS Mesh", "RS Mesh", "RS Mesh", "RS Mesh"}, 7052e920715SEwan Crawford {"RS Program Fragment", "RS Program Fragment", "RS Program Fragment", "RS Program Fragment"}, 7062e920715SEwan Crawford {"RS Program Vertex", "RS Program Vertex", "RS Program Vertex", "RS Program Vertex"}, 7072e920715SEwan Crawford {"RS Program Raster", "RS Program Raster", "RS Program Raster", "RS Program Raster"}, 7082e920715SEwan Crawford {"RS Program Store", "RS Program Store", "RS Program Store", "RS Program Store"}, 709b3f7f69dSAidan Dodds {"RS Font", "RS Font", "RS Font", "RS Font"}}; 71078f339d1SEwan Crawford 711a0f08674SEwan Crawford // Used as an index into the RSTypeToFormat array elements 712b3f7f69dSAidan Dodds enum TypeToFormatIndex 713b3f7f69dSAidan Dodds { 714a0f08674SEwan Crawford eFormatSingle = 0, 715a0f08674SEwan Crawford eFormatVector, 716a0f08674SEwan Crawford eElementSize 717a0f08674SEwan Crawford }; 718a0f08674SEwan Crawford 719a0f08674SEwan Crawford // { format enum of single element, format enum of element vector, size of element} 720b3f7f69dSAidan Dodds const uint32_t RenderScriptRuntime::AllocationDetails::RSTypeToFormat[][3] = { 721a0f08674SEwan Crawford {eFormatHex, eFormatHex, 1}, // RS_TYPE_NONE 722a0f08674SEwan Crawford {eFormatFloat, eFormatVectorOfFloat16, 2}, // RS_TYPE_FLOAT_16 723a0f08674SEwan Crawford {eFormatFloat, eFormatVectorOfFloat32, sizeof(float)}, // RS_TYPE_FLOAT_32 724a0f08674SEwan Crawford {eFormatFloat, eFormatVectorOfFloat64, sizeof(double)}, // RS_TYPE_FLOAT_64 725a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt8, sizeof(int8_t)}, // RS_TYPE_SIGNED_8 726a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt16, sizeof(int16_t)}, // RS_TYPE_SIGNED_16 727a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt32, sizeof(int32_t)}, // RS_TYPE_SIGNED_32 728a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfSInt64, sizeof(int64_t)}, // RS_TYPE_SIGNED_64 729a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt8, sizeof(uint8_t)}, // RS_TYPE_UNSIGNED_8 730a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt16, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_16 731a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt32, sizeof(uint32_t)}, // RS_TYPE_UNSIGNED_32 732a0f08674SEwan Crawford {eFormatDecimal, eFormatVectorOfUInt64, sizeof(uint64_t)}, // RS_TYPE_UNSIGNED_64 7332e920715SEwan Crawford {eFormatBoolean, eFormatBoolean, 1}, // RS_TYPE_BOOL 7342e920715SEwan Crawford {eFormatHex, eFormatHex, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_5_6_5 7352e920715SEwan Crawford {eFormatHex, eFormatHex, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_5_5_5_1 7362e920715SEwan Crawford {eFormatHex, eFormatHex, sizeof(uint16_t)}, // RS_TYPE_UNSIGNED_4_4_4_4 7372e920715SEwan Crawford {eFormatVectorOfFloat32, eFormatVectorOfFloat32, sizeof(float) * 16}, // RS_TYPE_MATRIX_4X4 7382e920715SEwan Crawford {eFormatVectorOfFloat32, eFormatVectorOfFloat32, sizeof(float) * 9}, // RS_TYPE_MATRIX_3X3 7392e920715SEwan Crawford {eFormatVectorOfFloat32, eFormatVectorOfFloat32, sizeof(float) * 4} // RS_TYPE_MATRIX_2X2 740a0f08674SEwan Crawford }; 741a0f08674SEwan Crawford 7424f8817c2SEwan Crawford const std::string RenderScriptRuntime::s_runtimeExpandSuffix(".expand"); 743a9759599SPavel Labath const std::array<const char *, 3> RenderScriptRuntime::s_runtimeCoordVars{{"rsIndex", "p->current.y", "p->current.z"}}; 7445ec532a9SColin Riley //------------------------------------------------------------------ 7455ec532a9SColin Riley // Static Functions 7465ec532a9SColin Riley //------------------------------------------------------------------ 7475ec532a9SColin Riley LanguageRuntime * 7485ec532a9SColin Riley RenderScriptRuntime::CreateInstance(Process *process, lldb::LanguageType language) 7495ec532a9SColin Riley { 7505ec532a9SColin Riley 7515ec532a9SColin Riley if (language == eLanguageTypeExtRenderScript) 7525ec532a9SColin Riley return new RenderScriptRuntime(process); 7535ec532a9SColin Riley else 754b3f7f69dSAidan Dodds return nullptr; 7555ec532a9SColin Riley } 7565ec532a9SColin Riley 75798156583SEwan Crawford // Callback with a module to search for matching symbols. 75898156583SEwan Crawford // We first check that the module contains RS kernels. 75998156583SEwan Crawford // Then look for a symbol which matches our kernel name. 76098156583SEwan Crawford // The breakpoint address is finally set using the address of this symbol. 76198156583SEwan Crawford Searcher::CallbackReturn 762b3f7f69dSAidan Dodds RSBreakpointResolver::SearchCallback(SearchFilter &filter, SymbolContext &context, Address *, bool) 76398156583SEwan Crawford { 76498156583SEwan Crawford ModuleSP module = context.module_sp; 76598156583SEwan Crawford 76698156583SEwan Crawford if (!module) 76798156583SEwan Crawford return Searcher::eCallbackReturnContinue; 76898156583SEwan Crawford 76998156583SEwan Crawford // Is this a module containing renderscript kernels? 77098156583SEwan Crawford if (nullptr == module->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData)) 77198156583SEwan Crawford return Searcher::eCallbackReturnContinue; 77298156583SEwan Crawford 77398156583SEwan Crawford // Attempt to set a breakpoint on the kernel name symbol within the module library. 77498156583SEwan Crawford // If it's not found, it's likely debug info is unavailable - try to set a 77598156583SEwan Crawford // breakpoint on <name>.expand. 77698156583SEwan Crawford 77798156583SEwan Crawford const Symbol *kernel_sym = module->FindFirstSymbolWithNameAndType(m_kernel_name, eSymbolTypeCode); 77898156583SEwan Crawford if (!kernel_sym) 77998156583SEwan Crawford { 78098156583SEwan Crawford std::string kernel_name_expanded(m_kernel_name.AsCString()); 78198156583SEwan Crawford kernel_name_expanded.append(".expand"); 78298156583SEwan Crawford kernel_sym = module->FindFirstSymbolWithNameAndType(ConstString(kernel_name_expanded.c_str()), eSymbolTypeCode); 78398156583SEwan Crawford } 78498156583SEwan Crawford 78598156583SEwan Crawford if (kernel_sym) 78698156583SEwan Crawford { 78798156583SEwan Crawford Address bp_addr = kernel_sym->GetAddress(); 78898156583SEwan Crawford if (filter.AddressPasses(bp_addr)) 78998156583SEwan Crawford m_breakpoint->AddLocation(bp_addr); 79098156583SEwan Crawford } 79198156583SEwan Crawford 79298156583SEwan Crawford return Searcher::eCallbackReturnContinue; 79398156583SEwan Crawford } 79498156583SEwan Crawford 7955ec532a9SColin Riley void 7965ec532a9SColin Riley RenderScriptRuntime::Initialize() 7975ec532a9SColin Riley { 798b3f7f69dSAidan Dodds PluginManager::RegisterPlugin(GetPluginNameStatic(), "RenderScript language support", CreateInstance, 799b3f7f69dSAidan Dodds GetCommandObject); 8005ec532a9SColin Riley } 8015ec532a9SColin Riley 8025ec532a9SColin Riley void 8035ec532a9SColin Riley RenderScriptRuntime::Terminate() 8045ec532a9SColin Riley { 8055ec532a9SColin Riley PluginManager::UnregisterPlugin(CreateInstance); 8065ec532a9SColin Riley } 8075ec532a9SColin Riley 8085ec532a9SColin Riley lldb_private::ConstString 8095ec532a9SColin Riley RenderScriptRuntime::GetPluginNameStatic() 8105ec532a9SColin Riley { 8115ec532a9SColin Riley static ConstString g_name("renderscript"); 8125ec532a9SColin Riley return g_name; 8135ec532a9SColin Riley } 8145ec532a9SColin Riley 815ef20b08fSColin Riley RenderScriptRuntime::ModuleKind 816ef20b08fSColin Riley RenderScriptRuntime::GetModuleKind(const lldb::ModuleSP &module_sp) 817ef20b08fSColin Riley { 818ef20b08fSColin Riley if (module_sp) 819ef20b08fSColin Riley { 820ef20b08fSColin Riley // Is this a module containing renderscript kernels? 821ef20b08fSColin Riley const Symbol *info_sym = module_sp->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData); 822ef20b08fSColin Riley if (info_sym) 823ef20b08fSColin Riley { 824ef20b08fSColin Riley return eModuleKindKernelObj; 825ef20b08fSColin Riley } 8264640cde1SColin Riley 8274640cde1SColin Riley // Is this the main RS runtime library 8284640cde1SColin Riley const ConstString rs_lib("libRS.so"); 8294640cde1SColin Riley if (module_sp->GetFileSpec().GetFilename() == rs_lib) 8304640cde1SColin Riley { 8314640cde1SColin Riley return eModuleKindLibRS; 8324640cde1SColin Riley } 8334640cde1SColin Riley 8344640cde1SColin Riley const ConstString rs_driverlib("libRSDriver.so"); 8354640cde1SColin Riley if (module_sp->GetFileSpec().GetFilename() == rs_driverlib) 8364640cde1SColin Riley { 8374640cde1SColin Riley return eModuleKindDriver; 8384640cde1SColin Riley } 8394640cde1SColin Riley 84015f2bd95SEwan Crawford const ConstString rs_cpureflib("libRSCpuRef.so"); 8414640cde1SColin Riley if (module_sp->GetFileSpec().GetFilename() == rs_cpureflib) 8424640cde1SColin Riley { 8434640cde1SColin Riley return eModuleKindImpl; 8444640cde1SColin Riley } 845ef20b08fSColin Riley } 846ef20b08fSColin Riley return eModuleKindIgnored; 847ef20b08fSColin Riley } 848ef20b08fSColin Riley 849ef20b08fSColin Riley bool 850ef20b08fSColin Riley RenderScriptRuntime::IsRenderScriptModule(const lldb::ModuleSP &module_sp) 851ef20b08fSColin Riley { 852ef20b08fSColin Riley return GetModuleKind(module_sp) != eModuleKindIgnored; 853ef20b08fSColin Riley } 854ef20b08fSColin Riley 855ef20b08fSColin Riley void 856ef20b08fSColin Riley RenderScriptRuntime::ModulesDidLoad(const ModuleList &module_list) 857ef20b08fSColin Riley { 858ef20b08fSColin Riley Mutex::Locker locker(module_list.GetMutex()); 859ef20b08fSColin Riley 860ef20b08fSColin Riley size_t num_modules = module_list.GetSize(); 861ef20b08fSColin Riley for (size_t i = 0; i < num_modules; i++) 862ef20b08fSColin Riley { 863ef20b08fSColin Riley auto mod = module_list.GetModuleAtIndex(i); 864ef20b08fSColin Riley if (IsRenderScriptModule(mod)) 865ef20b08fSColin Riley { 866ef20b08fSColin Riley LoadModule(mod); 867ef20b08fSColin Riley } 868ef20b08fSColin Riley } 869ef20b08fSColin Riley } 870ef20b08fSColin Riley 8715ec532a9SColin Riley //------------------------------------------------------------------ 8725ec532a9SColin Riley // PluginInterface protocol 8735ec532a9SColin Riley //------------------------------------------------------------------ 8745ec532a9SColin Riley lldb_private::ConstString 8755ec532a9SColin Riley RenderScriptRuntime::GetPluginName() 8765ec532a9SColin Riley { 8775ec532a9SColin Riley return GetPluginNameStatic(); 8785ec532a9SColin Riley } 8795ec532a9SColin Riley 8805ec532a9SColin Riley uint32_t 8815ec532a9SColin Riley RenderScriptRuntime::GetPluginVersion() 8825ec532a9SColin Riley { 8835ec532a9SColin Riley return 1; 8845ec532a9SColin Riley } 8855ec532a9SColin Riley 8865ec532a9SColin Riley bool 8875ec532a9SColin Riley RenderScriptRuntime::IsVTableName(const char *name) 8885ec532a9SColin Riley { 8895ec532a9SColin Riley return false; 8905ec532a9SColin Riley } 8915ec532a9SColin Riley 8925ec532a9SColin Riley bool 8935ec532a9SColin Riley RenderScriptRuntime::GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, 8940b6003f3SEnrico Granata TypeAndOrName &class_type_or_name, Address &address, 8950b6003f3SEnrico Granata Value::ValueType &value_type) 8965ec532a9SColin Riley { 8975ec532a9SColin Riley return false; 8985ec532a9SColin Riley } 8995ec532a9SColin Riley 900c74275bcSEnrico Granata TypeAndOrName 901b3f7f69dSAidan Dodds RenderScriptRuntime::FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value) 902c74275bcSEnrico Granata { 903c74275bcSEnrico Granata return type_and_or_name; 904c74275bcSEnrico Granata } 905c74275bcSEnrico Granata 9065ec532a9SColin Riley bool 9075ec532a9SColin Riley RenderScriptRuntime::CouldHaveDynamicValue(ValueObject &in_value) 9085ec532a9SColin Riley { 9095ec532a9SColin Riley return false; 9105ec532a9SColin Riley } 9115ec532a9SColin Riley 9125ec532a9SColin Riley lldb::BreakpointResolverSP 9135ec532a9SColin Riley RenderScriptRuntime::CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) 9145ec532a9SColin Riley { 9155ec532a9SColin Riley BreakpointResolverSP resolver_sp; 9165ec532a9SColin Riley return resolver_sp; 9175ec532a9SColin Riley } 9185ec532a9SColin Riley 919b3f7f69dSAidan Dodds const RenderScriptRuntime::HookDefn RenderScriptRuntime::s_runtimeHookDefns[] = { 9204640cde1SColin Riley // rsdScript 92182780287SAidan Dodds { 922b3f7f69dSAidan Dodds "rsdScriptInit", 923b3f7f69dSAidan Dodds "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhjj", 924b3f7f69dSAidan Dodds "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhmj", 925b3f7f69dSAidan Dodds 0, 926b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 927b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureScriptInit 92882780287SAidan Dodds }, 92982780287SAidan Dodds { 930b3f7f69dSAidan Dodds "rsdScriptInvokeForEachMulti", 931b3f7f69dSAidan Dodds "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEjPS6_PKvjPK12RsScriptCall", 932b3f7f69dSAidan Dodds "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEmPS6_PKvmPK12RsScriptCall", 933b3f7f69dSAidan Dodds 0, 934b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 935b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureScriptInvokeForEachMulti 93682780287SAidan Dodds }, 93782780287SAidan Dodds { 938b3f7f69dSAidan Dodds "rsdScriptSetGlobalVar", 939b3f7f69dSAidan Dodds "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvj", 940b3f7f69dSAidan Dodds "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvm", 941b3f7f69dSAidan Dodds 0, 942b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 943b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureSetGlobalVar 94482780287SAidan Dodds }, 9454640cde1SColin Riley 9464640cde1SColin Riley // rsdAllocation 94782780287SAidan Dodds { 948b3f7f69dSAidan Dodds "rsdAllocationInit", 949b3f7f69dSAidan Dodds "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", 950b3f7f69dSAidan Dodds "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", 951b3f7f69dSAidan Dodds 0, 952b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 953b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureAllocationInit 95482780287SAidan Dodds }, 95582780287SAidan Dodds { 956b3f7f69dSAidan Dodds "rsdAllocationRead2D", 957b3f7f69dSAidan Dodds "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvjj", 958b3f7f69dSAidan Dodds "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvmm", 959b3f7f69dSAidan Dodds 0, 960b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 961b3f7f69dSAidan Dodds nullptr 96282780287SAidan Dodds }, 963e69df382SEwan Crawford { 964b3f7f69dSAidan Dodds "rsdAllocationDestroy", 965b3f7f69dSAidan Dodds "_Z20rsdAllocationDestroyPKN7android12renderscript7ContextEPNS0_10AllocationE", 966b3f7f69dSAidan Dodds "_Z20rsdAllocationDestroyPKN7android12renderscript7ContextEPNS0_10AllocationE", 967b3f7f69dSAidan Dodds 0, 968b3f7f69dSAidan Dodds RenderScriptRuntime::eModuleKindDriver, 969b3f7f69dSAidan Dodds &lldb_private::RenderScriptRuntime::CaptureAllocationDestroy 970e69df382SEwan Crawford }, 9714640cde1SColin Riley }; 9724640cde1SColin Riley 973222b937cSEugene Zelenko const size_t RenderScriptRuntime::s_runtimeHookCount = sizeof(s_runtimeHookDefns) / sizeof(s_runtimeHookDefns[0]); 9744640cde1SColin Riley 9754640cde1SColin Riley bool 976b3f7f69dSAidan Dodds RenderScriptRuntime::HookCallback(void *baton, StoppointCallbackContext *ctx, lldb::user_id_t break_id, 977b3f7f69dSAidan Dodds lldb::user_id_t break_loc_id) 9784640cde1SColin Riley { 9794640cde1SColin Riley RuntimeHook *hook_info = (RuntimeHook *)baton; 9804640cde1SColin Riley ExecutionContext context(ctx->exe_ctx_ref); 9814640cde1SColin Riley 982b3f7f69dSAidan Dodds RenderScriptRuntime *lang_rt = 983b3f7f69dSAidan Dodds (RenderScriptRuntime *)context.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 9844640cde1SColin Riley 9854640cde1SColin Riley lang_rt->HookCallback(hook_info, context); 9864640cde1SColin Riley 9874640cde1SColin Riley return false; 9884640cde1SColin Riley } 9894640cde1SColin Riley 9904640cde1SColin Riley void 9914640cde1SColin Riley RenderScriptRuntime::HookCallback(RuntimeHook *hook_info, ExecutionContext &context) 9924640cde1SColin Riley { 9934640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 9944640cde1SColin Riley 9954640cde1SColin Riley if (log) 996b3f7f69dSAidan Dodds log->Printf("%s - '%s'", __FUNCTION__, hook_info->defn->name); 9974640cde1SColin Riley 9984640cde1SColin Riley if (hook_info->defn->grabber) 9994640cde1SColin Riley { 10004640cde1SColin Riley (this->*(hook_info->defn->grabber))(hook_info, context); 10014640cde1SColin Riley } 10024640cde1SColin Riley } 10034640cde1SColin Riley 10044640cde1SColin Riley void 1005e09c44b6SAidan Dodds RenderScriptRuntime::CaptureScriptInvokeForEachMulti(RuntimeHook* hook_info, 1006e09c44b6SAidan Dodds ExecutionContext& context) 1007e09c44b6SAidan Dodds { 1008e09c44b6SAidan Dodds Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1009e09c44b6SAidan Dodds 1010*f4786785SAidan Dodds enum 1011e09c44b6SAidan Dodds { 1012*f4786785SAidan Dodds eRsContext = 0, 1013*f4786785SAidan Dodds eRsScript, 1014*f4786785SAidan Dodds eRsSlot, 1015*f4786785SAidan Dodds eRsAIns, 1016*f4786785SAidan Dodds eRsInLen, 1017*f4786785SAidan Dodds eRsAOut, 1018*f4786785SAidan Dodds eRsUsr, 1019*f4786785SAidan Dodds eRsUsrLen, 1020*f4786785SAidan Dodds eRsSc, 1021*f4786785SAidan Dodds }; 1022e09c44b6SAidan Dodds 1023*f4786785SAidan Dodds std::array<ArgItem, 9> args = { 1024*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // const Context *rsc 1025*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // Script *s 1026*f4786785SAidan Dodds ArgItem{ArgItem::eInt32, 0}, // uint32_t slot 1027*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // const Allocation **aIns 1028*f4786785SAidan Dodds ArgItem{ArgItem::eInt32, 0}, // size_t inLen 1029*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // Allocation *aout 1030*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // const void *usr 1031*f4786785SAidan Dodds ArgItem{ArgItem::eInt32, 0}, // size_t usrLen 1032*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // const RsScriptCall *sc 1033*f4786785SAidan Dodds }; 1034e09c44b6SAidan Dodds 1035*f4786785SAidan Dodds bool success = GetArgs(context, &args[0], args.size()); 1036e09c44b6SAidan Dodds if (!success) 1037e09c44b6SAidan Dodds { 1038e09c44b6SAidan Dodds if (log) 1039b3f7f69dSAidan Dodds log->Printf("%s - Error while reading the function parameters", __FUNCTION__); 1040e09c44b6SAidan Dodds return; 1041e09c44b6SAidan Dodds } 1042e09c44b6SAidan Dodds 1043e09c44b6SAidan Dodds const uint32_t target_ptr_size = m_process->GetAddressByteSize(); 1044e09c44b6SAidan Dodds Error error; 1045e09c44b6SAidan Dodds std::vector<uint64_t> allocs; 1046e09c44b6SAidan Dodds 1047e09c44b6SAidan Dodds // traverse allocation list 1048*f4786785SAidan Dodds for (uint64_t i = 0; i < uint64_t(args[eRsInLen]); ++i) 1049e09c44b6SAidan Dodds { 1050e09c44b6SAidan Dodds // calculate offest to allocation pointer 1051*f4786785SAidan Dodds const addr_t addr = addr_t(args[eRsAIns]) + i * target_ptr_size; 1052e09c44b6SAidan Dodds 1053e09c44b6SAidan Dodds // Note: due to little endian layout, reading 32bits or 64bits into res64 will 1054e09c44b6SAidan Dodds // give the correct results. 1055e09c44b6SAidan Dodds 1056e09c44b6SAidan Dodds uint64_t res64 = 0; 1057e09c44b6SAidan Dodds size_t read = m_process->ReadMemory(addr, &res64, target_ptr_size, error); 1058e09c44b6SAidan Dodds if (read != target_ptr_size || !error.Success()) 1059e09c44b6SAidan Dodds { 1060e09c44b6SAidan Dodds if (log) 1061*f4786785SAidan Dodds log->Printf("%s - Error while reading allocation list argument %" PRIu64, __FUNCTION__, i); 1062e09c44b6SAidan Dodds } 1063e09c44b6SAidan Dodds else 1064e09c44b6SAidan Dodds { 1065e09c44b6SAidan Dodds allocs.push_back(res64); 1066e09c44b6SAidan Dodds } 1067e09c44b6SAidan Dodds } 1068e09c44b6SAidan Dodds 1069e09c44b6SAidan Dodds // if there is an output allocation track it 1070*f4786785SAidan Dodds if (uint64_t aOut = uint64_t(args[eRsAOut])) 1071e09c44b6SAidan Dodds { 1072*f4786785SAidan Dodds allocs.push_back(aOut); 1073e09c44b6SAidan Dodds } 1074e09c44b6SAidan Dodds 1075e09c44b6SAidan Dodds // for all allocations we have found 1076e09c44b6SAidan Dodds for (const uint64_t alloc_addr : allocs) 1077e09c44b6SAidan Dodds { 1078e09c44b6SAidan Dodds AllocationDetails* alloc = LookUpAllocation(alloc_addr, true); 1079e09c44b6SAidan Dodds if (alloc) 1080e09c44b6SAidan Dodds { 1081e09c44b6SAidan Dodds // save the allocation address 1082e09c44b6SAidan Dodds if (alloc->address.isValid()) 1083e09c44b6SAidan Dodds { 1084e09c44b6SAidan Dodds // check the allocation address we already have matches 1085e09c44b6SAidan Dodds assert(*alloc->address.get() == alloc_addr); 1086e09c44b6SAidan Dodds } 1087e09c44b6SAidan Dodds else 1088e09c44b6SAidan Dodds { 1089e09c44b6SAidan Dodds alloc->address = alloc_addr; 1090e09c44b6SAidan Dodds } 1091e09c44b6SAidan Dodds 1092e09c44b6SAidan Dodds // save the context 1093e09c44b6SAidan Dodds if (log) 1094e09c44b6SAidan Dodds { 1095*f4786785SAidan Dodds if (alloc->context.isValid() && *alloc->context.get() != addr_t(args[eRsContext])) 1096b3f7f69dSAidan Dodds log->Printf("%s - Allocation used by multiple contexts", __FUNCTION__); 1097e09c44b6SAidan Dodds } 1098*f4786785SAidan Dodds alloc->context = addr_t(args[eRsContext]); 1099e09c44b6SAidan Dodds } 1100e09c44b6SAidan Dodds } 1101e09c44b6SAidan Dodds 1102e09c44b6SAidan Dodds // make sure we track this script object 1103*f4786785SAidan Dodds if (lldb_private::RenderScriptRuntime::ScriptDetails *script = LookUpScript(addr_t(args[eRsScript]), true)) 1104e09c44b6SAidan Dodds { 1105e09c44b6SAidan Dodds if (log) 1106e09c44b6SAidan Dodds { 1107*f4786785SAidan Dodds if (script->context.isValid() && *script->context.get() != addr_t(args[eRsContext])) 1108b3f7f69dSAidan Dodds log->Printf("%s - Script used by multiple contexts", __FUNCTION__); 1109e09c44b6SAidan Dodds } 1110*f4786785SAidan Dodds script->context = addr_t(args[eRsContext]); 1111e09c44b6SAidan Dodds } 1112e09c44b6SAidan Dodds } 1113e09c44b6SAidan Dodds 1114e09c44b6SAidan Dodds void 1115b3f7f69dSAidan Dodds RenderScriptRuntime::CaptureSetGlobalVar(RuntimeHook *hook_info, ExecutionContext &context) 11164640cde1SColin Riley { 11174640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 11184640cde1SColin Riley 1119*f4786785SAidan Dodds enum 1120*f4786785SAidan Dodds { 1121*f4786785SAidan Dodds eRsContext, 1122*f4786785SAidan Dodds eRsScript, 1123*f4786785SAidan Dodds eRsId, 1124*f4786785SAidan Dodds eRsData, 1125*f4786785SAidan Dodds eRsLength, 1126*f4786785SAidan Dodds }; 11274640cde1SColin Riley 1128*f4786785SAidan Dodds std::array<ArgItem, 5> args = { 1129*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsContext 1130*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsScript 1131*f4786785SAidan Dodds ArgItem{ArgItem::eInt32, 0}, // eRsId 1132*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsData 1133*f4786785SAidan Dodds ArgItem{ArgItem::eInt32, 0}, // eRsLength 1134*f4786785SAidan Dodds }; 11354640cde1SColin Riley 1136*f4786785SAidan Dodds bool success = GetArgs(context, &args[0], args.size()); 113782780287SAidan Dodds if (!success) 113882780287SAidan Dodds { 113982780287SAidan Dodds if (log) 1140b3f7f69dSAidan Dodds log->Printf("%s - error reading the function parameters.", __FUNCTION__); 114182780287SAidan Dodds return; 114282780287SAidan Dodds } 11434640cde1SColin Riley 11444640cde1SColin Riley if (log) 11454640cde1SColin Riley { 1146*f4786785SAidan Dodds log->Printf("%s - 0x%" PRIx64 ",0x%" PRIx64 " slot %" PRIu64 " = 0x%" PRIx64 ":%" PRIu64 "bytes.", __FUNCTION__, 1147*f4786785SAidan Dodds uint64_t(args[eRsContext]), uint64_t(args[eRsScript]), uint64_t(args[eRsId]), 1148*f4786785SAidan Dodds uint64_t(args[eRsData]), uint64_t(args[eRsLength])); 11494640cde1SColin Riley 1150*f4786785SAidan Dodds addr_t script_addr = addr_t(args[eRsScript]); 11514640cde1SColin Riley if (m_scriptMappings.find(script_addr) != m_scriptMappings.end()) 11524640cde1SColin Riley { 11534640cde1SColin Riley auto rsm = m_scriptMappings[script_addr]; 1154*f4786785SAidan Dodds if (uint64_t(args[eRsId]) < rsm->m_globals.size()) 11554640cde1SColin Riley { 1156*f4786785SAidan Dodds auto rsg = rsm->m_globals[uint64_t(args[eRsId])]; 1157*f4786785SAidan Dodds log->Printf("%s - Setting of '%s' within '%s' inferred", __FUNCTION__, rsg.m_name.AsCString(), 1158*f4786785SAidan Dodds rsm->m_module->GetFileSpec().GetFilename().AsCString()); 11594640cde1SColin Riley } 11604640cde1SColin Riley } 11614640cde1SColin Riley } 11624640cde1SColin Riley } 11634640cde1SColin Riley 11644640cde1SColin Riley void 1165b3f7f69dSAidan Dodds RenderScriptRuntime::CaptureAllocationInit(RuntimeHook *hook_info, ExecutionContext &context) 11664640cde1SColin Riley { 11674640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 11684640cde1SColin Riley 1169*f4786785SAidan Dodds enum 1170*f4786785SAidan Dodds { 1171*f4786785SAidan Dodds eRsContext, 1172*f4786785SAidan Dodds eRsAlloc, 1173*f4786785SAidan Dodds eRsForceZero 1174*f4786785SAidan Dodds }; 11754640cde1SColin Riley 1176*f4786785SAidan Dodds std::array<ArgItem, 3> args = { 1177*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsContext 1178*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsAlloc 1179*f4786785SAidan Dodds ArgItem{ArgItem::eBool, 0}, // eRsForceZero 1180*f4786785SAidan Dodds }; 11814640cde1SColin Riley 1182*f4786785SAidan Dodds bool success = GetArgs(context, &args[0], args.size()); 118382780287SAidan Dodds if (!success) // error case 118482780287SAidan Dodds { 118582780287SAidan Dodds if (log) 1186b3f7f69dSAidan Dodds log->Printf("%s - error while reading the function parameters", __FUNCTION__); 118782780287SAidan Dodds return; // abort 118882780287SAidan Dodds } 11894640cde1SColin Riley 11904640cde1SColin Riley if (log) 1191*f4786785SAidan Dodds log->Printf("%s - 0x%" PRIx64 ",0x%" PRIx64 ",0x%" PRIx64 " .", __FUNCTION__, uint64_t(args[eRsContext]), 1192*f4786785SAidan Dodds uint64_t(args[eRsAlloc]), uint64_t(args[eRsForceZero])); 119378f339d1SEwan Crawford 1194*f4786785SAidan Dodds AllocationDetails *alloc = LookUpAllocation(uint64_t(args[eRsAlloc]), true); 119578f339d1SEwan Crawford if (alloc) 1196*f4786785SAidan Dodds alloc->context = uint64_t(args[eRsContext]); 11974640cde1SColin Riley } 11984640cde1SColin Riley 11994640cde1SColin Riley void 1200e69df382SEwan Crawford RenderScriptRuntime::CaptureAllocationDestroy(RuntimeHook *hook_info, ExecutionContext &context) 1201e69df382SEwan Crawford { 1202e69df382SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1203e69df382SEwan Crawford 1204*f4786785SAidan Dodds enum 1205*f4786785SAidan Dodds { 1206*f4786785SAidan Dodds eRsContext, 1207*f4786785SAidan Dodds eRsAlloc, 1208*f4786785SAidan Dodds }; 1209e69df382SEwan Crawford 1210*f4786785SAidan Dodds std::array<ArgItem, 2> args = { 1211*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsContext 1212*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, // eRsAlloc 1213*f4786785SAidan Dodds }; 1214*f4786785SAidan Dodds 1215*f4786785SAidan Dodds bool success = GetArgs(context, &args[0], args.size()); 1216b3f7f69dSAidan Dodds if (!success) 1217e69df382SEwan Crawford { 1218e69df382SEwan Crawford if (log) 1219b3f7f69dSAidan Dodds log->Printf("%s - error while reading the function parameters.", __FUNCTION__); 1220b3f7f69dSAidan Dodds return; 1221e69df382SEwan Crawford } 1222e69df382SEwan Crawford 1223e69df382SEwan Crawford if (log) 1224*f4786785SAidan Dodds log->Printf("%s - 0x%" PRIx64 ", 0x%" PRIx64 ".", __FUNCTION__, uint64_t(args[eRsContext]), 1225*f4786785SAidan Dodds uint64_t(args[eRsAlloc])); 1226e69df382SEwan Crawford 1227e69df382SEwan Crawford for (auto iter = m_allocations.begin(); iter != m_allocations.end(); ++iter) 1228e69df382SEwan Crawford { 1229e69df382SEwan Crawford auto &allocation_ap = *iter; // get the unique pointer 1230*f4786785SAidan Dodds if (allocation_ap->address.isValid() && *allocation_ap->address.get() == addr_t(args[eRsAlloc])) 1231e69df382SEwan Crawford { 1232e69df382SEwan Crawford m_allocations.erase(iter); 1233e69df382SEwan Crawford if (log) 1234b3f7f69dSAidan Dodds log->Printf("%s - deleted allocation entry.", __FUNCTION__); 1235e69df382SEwan Crawford return; 1236e69df382SEwan Crawford } 1237e69df382SEwan Crawford } 1238e69df382SEwan Crawford 1239e69df382SEwan Crawford if (log) 1240b3f7f69dSAidan Dodds log->Printf("%s - couldn't find destroyed allocation.", __FUNCTION__); 1241e69df382SEwan Crawford } 1242e69df382SEwan Crawford 1243e69df382SEwan Crawford void 1244b3f7f69dSAidan Dodds RenderScriptRuntime::CaptureScriptInit(RuntimeHook *hook_info, ExecutionContext &context) 12454640cde1SColin Riley { 12464640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 12474640cde1SColin Riley 12484640cde1SColin Riley Error error; 12494640cde1SColin Riley Process *process = context.GetProcessPtr(); 12504640cde1SColin Riley 1251*f4786785SAidan Dodds enum 1252*f4786785SAidan Dodds { 1253*f4786785SAidan Dodds eRsContext, 1254*f4786785SAidan Dodds eRsScript, 1255*f4786785SAidan Dodds eRsResNamePtr, 1256*f4786785SAidan Dodds eRsCachedDirPtr 1257*f4786785SAidan Dodds }; 12584640cde1SColin Riley 1259*f4786785SAidan Dodds std::array<ArgItem, 4> args = {ArgItem{ArgItem::ePointer, 0}, ArgItem{ArgItem::ePointer, 0}, 1260*f4786785SAidan Dodds ArgItem{ArgItem::ePointer, 0}, ArgItem{ArgItem::ePointer, 0}}; 1261*f4786785SAidan Dodds bool success = GetArgs(context, &args[0], args.size()); 126282780287SAidan Dodds if (!success) 126382780287SAidan Dodds { 126482780287SAidan Dodds if (log) 1265b3f7f69dSAidan Dodds log->Printf("%s - error while reading the function parameters.", __FUNCTION__); 126682780287SAidan Dodds return; 126782780287SAidan Dodds } 126882780287SAidan Dodds 1269*f4786785SAidan Dodds std::string resname; 1270*f4786785SAidan Dodds process->ReadCStringFromMemory(addr_t(args[eRsResNamePtr]), resname, error); 12714640cde1SColin Riley if (error.Fail()) 12724640cde1SColin Riley { 12734640cde1SColin Riley if (log) 1274b3f7f69dSAidan Dodds log->Printf("%s - error reading resname: %s.", __FUNCTION__, error.AsCString()); 12754640cde1SColin Riley } 12764640cde1SColin Riley 1277*f4786785SAidan Dodds std::string cachedir; 1278*f4786785SAidan Dodds process->ReadCStringFromMemory(addr_t(args[eRsCachedDirPtr]), cachedir, error); 12794640cde1SColin Riley if (error.Fail()) 12804640cde1SColin Riley { 12814640cde1SColin Riley if (log) 1282b3f7f69dSAidan Dodds log->Printf("%s - error reading cachedir: %s.", __FUNCTION__, error.AsCString()); 12834640cde1SColin Riley } 12844640cde1SColin Riley 12854640cde1SColin Riley if (log) 1286*f4786785SAidan Dodds log->Printf("%s - 0x%" PRIx64 ",0x%" PRIx64 " => '%s' at '%s' .", __FUNCTION__, uint64_t(args[eRsContext]), 1287*f4786785SAidan Dodds uint64_t(args[eRsScript]), resname.c_str(), cachedir.c_str()); 12884640cde1SColin Riley 12894640cde1SColin Riley if (resname.size() > 0) 12904640cde1SColin Riley { 12914640cde1SColin Riley StreamString strm; 12924640cde1SColin Riley strm.Printf("librs.%s.so", resname.c_str()); 12934640cde1SColin Riley 1294*f4786785SAidan Dodds ScriptDetails *script = LookUpScript(addr_t(args[eRsScript]), true); 129578f339d1SEwan Crawford if (script) 129678f339d1SEwan Crawford { 129778f339d1SEwan Crawford script->type = ScriptDetails::eScriptC; 129878f339d1SEwan Crawford script->cacheDir = cachedir; 129978f339d1SEwan Crawford script->resName = resname; 130078f339d1SEwan Crawford script->scriptDyLib = strm.GetData(); 1301*f4786785SAidan Dodds script->context = addr_t(args[eRsContext]); 130278f339d1SEwan Crawford } 13034640cde1SColin Riley 13044640cde1SColin Riley if (log) 1305*f4786785SAidan Dodds log->Printf("%s - '%s' tagged with context 0x%" PRIx64 " and script 0x%" PRIx64 ".", __FUNCTION__, 1306*f4786785SAidan Dodds strm.GetData(), uint64_t(args[eRsContext]), uint64_t(args[eRsScript])); 13074640cde1SColin Riley } 13084640cde1SColin Riley else if (log) 13094640cde1SColin Riley { 1310b3f7f69dSAidan Dodds log->Printf("%s - resource name invalid, Script not tagged.", __FUNCTION__); 13114640cde1SColin Riley } 13124640cde1SColin Riley } 13134640cde1SColin Riley 13144640cde1SColin Riley void 13154640cde1SColin Riley RenderScriptRuntime::LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind) 13164640cde1SColin Riley { 13174640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 13184640cde1SColin Riley 13194640cde1SColin Riley if (!module) 13204640cde1SColin Riley { 13214640cde1SColin Riley return; 13224640cde1SColin Riley } 13234640cde1SColin Riley 132482780287SAidan Dodds Target &target = GetProcess()->GetTarget(); 132582780287SAidan Dodds llvm::Triple::ArchType targetArchType = target.GetArchitecture().GetMachine(); 132682780287SAidan Dodds 1327b3f7f69dSAidan Dodds if (targetArchType != llvm::Triple::ArchType::x86 && 1328b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::arm && 1329b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::aarch64 && 1330b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::mipsel && 1331b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::mips64el && 1332b3f7f69dSAidan Dodds targetArchType != llvm::Triple::ArchType::x86_64) 13334640cde1SColin Riley { 13344640cde1SColin Riley if (log) 1335b3f7f69dSAidan Dodds log->Printf("%s - unable to hook runtime functions.", __FUNCTION__); 13364640cde1SColin Riley return; 13374640cde1SColin Riley } 13384640cde1SColin Riley 133982780287SAidan Dodds uint32_t archByteSize = target.GetArchitecture().GetAddressByteSize(); 13404640cde1SColin Riley 13414640cde1SColin Riley for (size_t idx = 0; idx < s_runtimeHookCount; idx++) 13424640cde1SColin Riley { 13434640cde1SColin Riley const HookDefn *hook_defn = &s_runtimeHookDefns[idx]; 1344b3f7f69dSAidan Dodds if (hook_defn->kind != kind) 1345b3f7f69dSAidan Dodds { 13464640cde1SColin Riley continue; 13474640cde1SColin Riley } 13484640cde1SColin Riley 134982780287SAidan Dodds const char *symbol_name = (archByteSize == 4) ? hook_defn->symbol_name_m32 : hook_defn->symbol_name_m64; 135082780287SAidan Dodds 135182780287SAidan Dodds const Symbol *sym = module->FindFirstSymbolWithNameAndType(ConstString(symbol_name), eSymbolTypeCode); 1352b3f7f69dSAidan Dodds if (!sym) 1353b3f7f69dSAidan Dodds { 1354b3f7f69dSAidan Dodds if (log) 1355b3f7f69dSAidan Dodds { 1356b3f7f69dSAidan Dodds log->Printf("%s - symbol '%s' related to the function %s not found", 1357b3f7f69dSAidan Dodds __FUNCTION__, symbol_name, hook_defn->name); 135882780287SAidan Dodds } 135982780287SAidan Dodds continue; 136082780287SAidan Dodds } 13614640cde1SColin Riley 1362358cf1eaSGreg Clayton addr_t addr = sym->GetLoadAddress(&target); 13634640cde1SColin Riley if (addr == LLDB_INVALID_ADDRESS) 13644640cde1SColin Riley { 13654640cde1SColin Riley if (log) 1366b3f7f69dSAidan Dodds log->Printf("%s - unable to resolve the address of hook function '%s' with symbol '%s'.", 1367b3f7f69dSAidan Dodds __FUNCTION__, hook_defn->name, symbol_name); 13684640cde1SColin Riley continue; 13694640cde1SColin Riley } 137082780287SAidan Dodds else 137182780287SAidan Dodds { 137282780287SAidan Dodds if (log) 1373b3f7f69dSAidan Dodds log->Printf("%s - function %s, address resolved at 0x%" PRIx64, 1374b3f7f69dSAidan Dodds __FUNCTION__, hook_defn->name, addr); 137582780287SAidan Dodds } 13764640cde1SColin Riley 13774640cde1SColin Riley RuntimeHookSP hook(new RuntimeHook()); 13784640cde1SColin Riley hook->address = addr; 13794640cde1SColin Riley hook->defn = hook_defn; 13804640cde1SColin Riley hook->bp_sp = target.CreateBreakpoint(addr, true, false); 13814640cde1SColin Riley hook->bp_sp->SetCallback(HookCallback, hook.get(), true); 13824640cde1SColin Riley m_runtimeHooks[addr] = hook; 13834640cde1SColin Riley if (log) 13844640cde1SColin Riley { 1385b3f7f69dSAidan Dodds log->Printf("%s - successfully hooked '%s' in '%s' version %" PRIu64 " at 0x%" PRIx64 ".", 1386b3f7f69dSAidan Dodds __FUNCTION__, hook_defn->name, module->GetFileSpec().GetFilename().AsCString(), 1387b3f7f69dSAidan Dodds (uint64_t)hook_defn->version, (uint64_t)addr); 13884640cde1SColin Riley } 13894640cde1SColin Riley } 13904640cde1SColin Riley } 13914640cde1SColin Riley 13924640cde1SColin Riley void 13934640cde1SColin Riley RenderScriptRuntime::FixupScriptDetails(RSModuleDescriptorSP rsmodule_sp) 13944640cde1SColin Riley { 13954640cde1SColin Riley if (!rsmodule_sp) 13964640cde1SColin Riley return; 13974640cde1SColin Riley 13984640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 13994640cde1SColin Riley 14004640cde1SColin Riley const ModuleSP module = rsmodule_sp->m_module; 14014640cde1SColin Riley const FileSpec &file = module->GetPlatformFileSpec(); 14024640cde1SColin Riley 140378f339d1SEwan Crawford // Iterate over all of the scripts that we currently know of. 140478f339d1SEwan Crawford // Note: We cant push or pop to m_scripts here or it may invalidate rs_script. 14054640cde1SColin Riley for (const auto &rs_script : m_scripts) 14064640cde1SColin Riley { 140778f339d1SEwan Crawford // Extract the expected .so file path for this script. 140878f339d1SEwan Crawford std::string dylib; 140978f339d1SEwan Crawford if (!rs_script->scriptDyLib.get(dylib)) 141078f339d1SEwan Crawford continue; 141178f339d1SEwan Crawford 141278f339d1SEwan Crawford // Only proceed if the module that has loaded corresponds to this script. 141378f339d1SEwan Crawford if (file.GetFilename() != ConstString(dylib.c_str())) 141478f339d1SEwan Crawford continue; 141578f339d1SEwan Crawford 141678f339d1SEwan Crawford // Obtain the script address which we use as a key. 141778f339d1SEwan Crawford lldb::addr_t script; 141878f339d1SEwan Crawford if (!rs_script->script.get(script)) 141978f339d1SEwan Crawford continue; 142078f339d1SEwan Crawford 142178f339d1SEwan Crawford // If we have a script mapping for the current script. 142278f339d1SEwan Crawford if (m_scriptMappings.find(script) != m_scriptMappings.end()) 14234640cde1SColin Riley { 142478f339d1SEwan Crawford // if the module we have stored is different to the one we just received. 142578f339d1SEwan Crawford if (m_scriptMappings[script] != rsmodule_sp) 14264640cde1SColin Riley { 14274640cde1SColin Riley if (log) 1428b3f7f69dSAidan Dodds log->Printf("%s - script %" PRIx64 " wants reassigned to new rsmodule '%s'.", __FUNCTION__, 142978f339d1SEwan Crawford (uint64_t)script, rsmodule_sp->m_module->GetFileSpec().GetFilename().AsCString()); 14304640cde1SColin Riley } 14314640cde1SColin Riley } 143278f339d1SEwan Crawford // We don't have a script mapping for the current script. 14334640cde1SColin Riley else 14344640cde1SColin Riley { 143578f339d1SEwan Crawford // Obtain the script resource name. 143678f339d1SEwan Crawford std::string resName; 143778f339d1SEwan Crawford if (rs_script->resName.get(resName)) 143878f339d1SEwan Crawford // Set the modules resource name. 143978f339d1SEwan Crawford rsmodule_sp->m_resname = resName; 144078f339d1SEwan Crawford // Add Script/Module pair to map. 144178f339d1SEwan Crawford m_scriptMappings[script] = rsmodule_sp; 14424640cde1SColin Riley if (log) 1443b3f7f69dSAidan Dodds log->Printf("%s - script %" PRIx64 " associated with rsmodule '%s'.", __FUNCTION__, 144478f339d1SEwan Crawford (uint64_t)script, rsmodule_sp->m_module->GetFileSpec().GetFilename().AsCString()); 14454640cde1SColin Riley } 14464640cde1SColin Riley } 14474640cde1SColin Riley } 14484640cde1SColin Riley 144915f2bd95SEwan Crawford // Uses the Target API to evaluate the expression passed as a parameter to the function 145015f2bd95SEwan Crawford // The result of that expression is returned an unsigned 64 bit int, via the result* paramter. 145115f2bd95SEwan Crawford // Function returns true on success, and false on failure 145215f2bd95SEwan Crawford bool 145315f2bd95SEwan Crawford RenderScriptRuntime::EvalRSExpression(const char *expression, StackFrame *frame_ptr, uint64_t *result) 145415f2bd95SEwan Crawford { 145515f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 145615f2bd95SEwan Crawford if (log) 1457b3f7f69dSAidan Dodds log->Printf("%s(%s)", __FUNCTION__, expression); 145815f2bd95SEwan Crawford 145915f2bd95SEwan Crawford ValueObjectSP expr_result; 146015f2bd95SEwan Crawford // Perform the actual expression evaluation 146115f2bd95SEwan Crawford GetProcess()->GetTarget().EvaluateExpression(expression, frame_ptr, expr_result); 146215f2bd95SEwan Crawford 146315f2bd95SEwan Crawford if (!expr_result) 146415f2bd95SEwan Crawford { 146515f2bd95SEwan Crawford if (log) 1466b3f7f69dSAidan Dodds log->Printf("%s: couldn't evaluate expression.", __FUNCTION__); 146715f2bd95SEwan Crawford return false; 146815f2bd95SEwan Crawford } 146915f2bd95SEwan Crawford 147015f2bd95SEwan Crawford // The result of the expression is invalid 147115f2bd95SEwan Crawford if (!expr_result->GetError().Success()) 147215f2bd95SEwan Crawford { 147315f2bd95SEwan Crawford Error err = expr_result->GetError(); 147415f2bd95SEwan Crawford if (err.GetError() == UserExpression::kNoResult) // Expression returned void, so this is actually a success 147515f2bd95SEwan Crawford { 147615f2bd95SEwan Crawford if (log) 1477b3f7f69dSAidan Dodds log->Printf("%s - expression returned void.", __FUNCTION__); 147815f2bd95SEwan Crawford 147915f2bd95SEwan Crawford result = nullptr; 148015f2bd95SEwan Crawford return true; 148115f2bd95SEwan Crawford } 148215f2bd95SEwan Crawford 148315f2bd95SEwan Crawford if (log) 1484b3f7f69dSAidan Dodds log->Printf("%s - error evaluating expression result: %s", __FUNCTION__, 1485b3f7f69dSAidan Dodds err.AsCString()); 148615f2bd95SEwan Crawford return false; 148715f2bd95SEwan Crawford } 148815f2bd95SEwan Crawford 148915f2bd95SEwan Crawford bool success = false; 1490b3f7f69dSAidan Dodds *result = expr_result->GetValueAsUnsigned(0, &success); // We only read the result as an uint32_t. 149115f2bd95SEwan Crawford 149215f2bd95SEwan Crawford if (!success) 149315f2bd95SEwan Crawford { 149415f2bd95SEwan Crawford if (log) 1495b3f7f69dSAidan Dodds log->Printf("%s - couldn't convert expression result to uint32_t", __FUNCTION__); 149615f2bd95SEwan Crawford return false; 149715f2bd95SEwan Crawford } 149815f2bd95SEwan Crawford 149915f2bd95SEwan Crawford return true; 150015f2bd95SEwan Crawford } 150115f2bd95SEwan Crawford 1502ea0636b5SEwan Crawford namespace 1503ea0636b5SEwan Crawford { 1504836d9651SEwan Crawford // Used to index expression format strings 1505836d9651SEwan Crawford enum ExpressionStrings 150615f2bd95SEwan Crawford { 1507836d9651SEwan Crawford eExprGetOffsetPtr = 0, 1508836d9651SEwan Crawford eExprAllocGetType, 1509836d9651SEwan Crawford eExprTypeDimX, 1510836d9651SEwan Crawford eExprTypeDimY, 1511836d9651SEwan Crawford eExprTypeDimZ, 1512836d9651SEwan Crawford eExprTypeElemPtr, 1513836d9651SEwan Crawford eExprElementType, 1514836d9651SEwan Crawford eExprElementKind, 1515836d9651SEwan Crawford eExprElementVec, 1516836d9651SEwan Crawford eExprElementFieldCount, 1517836d9651SEwan Crawford eExprSubelementsId, 1518836d9651SEwan Crawford eExprSubelementsName, 1519ea0636b5SEwan Crawford eExprSubelementsArrSize, 1520ea0636b5SEwan Crawford 1521ea0636b5SEwan Crawford _eExprLast // keep at the end, implicit size of the array runtimeExpressions 1522836d9651SEwan Crawford }; 152315f2bd95SEwan Crawford 1524ea0636b5SEwan Crawford // max length of an expanded expression 1525ea0636b5SEwan Crawford const int jit_max_expr_size = 512; 1526ea0636b5SEwan Crawford 1527ea0636b5SEwan Crawford // Retrieve the string to JIT for the given expression 1528ea0636b5SEwan Crawford const char* 1529ea0636b5SEwan Crawford JITTemplate(ExpressionStrings e) 153015f2bd95SEwan Crawford { 1531ea0636b5SEwan Crawford // Format strings containing the expressions we may need to evaluate. 1532ea0636b5SEwan Crawford static std::array<const char*, _eExprLast> runtimeExpressions = {{ 153315f2bd95SEwan Crawford // Mangled GetOffsetPointer(Allocation*, xoff, yoff, zoff, lod, cubemap) 153415f2bd95SEwan Crawford "(int*)_Z12GetOffsetPtrPKN7android12renderscript10AllocationEjjjj23RsAllocationCubemapFace(0x%lx, %u, %u, %u, 0, 0)", 153515f2bd95SEwan Crawford 153615f2bd95SEwan Crawford // Type* rsaAllocationGetType(Context*, Allocation*) 153715f2bd95SEwan Crawford "(void*)rsaAllocationGetType(0x%lx, 0x%lx)", 153815f2bd95SEwan Crawford 153915f2bd95SEwan Crawford // rsaTypeGetNativeData(Context*, Type*, void* typeData, size) 154015f2bd95SEwan Crawford // Pack the data in the following way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ; 154115f2bd95SEwan Crawford // mHal.state.lodCount; mHal.state.faces; mElement; into typeData 154215f2bd95SEwan Crawford // Need to specify 32 or 64 bit for uint_t since this differs between devices 154315f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[0]", // X dim 154415f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[1]", // Y dim 154515f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[2]", // Z dim 154615f2bd95SEwan Crawford "uint%u_t data[6]; (void*)rsaTypeGetNativeData(0x%lx, 0x%lx, data, 6); data[5]", // Element ptr 154715f2bd95SEwan Crawford 154815f2bd95SEwan Crawford // rsaElementGetNativeData(Context*, Element*, uint32_t* elemData,size) 154915f2bd95SEwan Crawford // Pack mType; mKind; mNormalized; mVectorSize; NumSubElements into elemData 15508b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[0]", // Type 15518b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[1]", // Kind 15528b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[3]", // Vector Size 15538b244e21SEwan Crawford "uint32_t data[5]; (void*)rsaElementGetNativeData(0x%lx, 0x%lx, data, 5); data[4]", // Field Count 15548b244e21SEwan Crawford 15558b244e21SEwan Crawford // rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids, const char **names, 15568b244e21SEwan Crawford // size_t *arraySizes, uint32_t dataSize) 15578b244e21SEwan Crawford // Needed for Allocations of structs to gather details about fields/Subelements 15588b244e21SEwan Crawford "void *ids[%u]; const char *names[%u]; size_t arr_size[%u];" 15598b244e21SEwan Crawford "(void*)rsaElementGetSubElements(0x%lx, 0x%lx, ids, names, arr_size, %u); ids[%u]", // Element* of field 15608b244e21SEwan Crawford 15618b244e21SEwan Crawford "void *ids[%u]; const char *names[%u]; size_t arr_size[%u];" 15628b244e21SEwan Crawford "(void*)rsaElementGetSubElements(0x%lx, 0x%lx, ids, names, arr_size, %u); names[%u]", // Name of field 15638b244e21SEwan Crawford 15648b244e21SEwan Crawford "void *ids[%u]; const char *names[%u]; size_t arr_size[%u];" 15658b244e21SEwan Crawford "(void*)rsaElementGetSubElements(0x%lx, 0x%lx, ids, names, arr_size, %u); arr_size[%u]" // Array size of field 1566ea0636b5SEwan Crawford }}; 1567ea0636b5SEwan Crawford 1568ea0636b5SEwan Crawford return runtimeExpressions[e]; 1569ea0636b5SEwan Crawford } 1570ea0636b5SEwan Crawford } // end of the anonymous namespace 1571ea0636b5SEwan Crawford 157215f2bd95SEwan Crawford 157315f2bd95SEwan Crawford // JITs the RS runtime for the internal data pointer of an allocation. 157415f2bd95SEwan Crawford // Is passed x,y,z coordinates for the pointer to a specific element. 157515f2bd95SEwan Crawford // Then sets the data_ptr member in Allocation with the result. 157615f2bd95SEwan Crawford // Returns true on success, false otherwise 157715f2bd95SEwan Crawford bool 1578b3f7f69dSAidan Dodds RenderScriptRuntime::JITDataPointer(AllocationDetails *allocation, StackFrame *frame_ptr, uint32_t x, 1579b3f7f69dSAidan Dodds uint32_t y, uint32_t z) 158015f2bd95SEwan Crawford { 158115f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 158215f2bd95SEwan Crawford 158315f2bd95SEwan Crawford if (!allocation->address.isValid()) 158415f2bd95SEwan Crawford { 158515f2bd95SEwan Crawford if (log) 1586b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 158715f2bd95SEwan Crawford return false; 158815f2bd95SEwan Crawford } 158915f2bd95SEwan Crawford 1590ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprGetOffsetPtr); 1591ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 159215f2bd95SEwan Crawford 1593ea0636b5SEwan Crawford int chars_written = snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->address.get(), x, y, z); 159415f2bd95SEwan Crawford if (chars_written < 0) 159515f2bd95SEwan Crawford { 159615f2bd95SEwan Crawford if (log) 1597b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 159815f2bd95SEwan Crawford return false; 159915f2bd95SEwan Crawford } 1600ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 160115f2bd95SEwan Crawford { 160215f2bd95SEwan Crawford if (log) 1603b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 160415f2bd95SEwan Crawford return false; 160515f2bd95SEwan Crawford } 160615f2bd95SEwan Crawford 160715f2bd95SEwan Crawford uint64_t result = 0; 160815f2bd95SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 160915f2bd95SEwan Crawford return false; 161015f2bd95SEwan Crawford 161115f2bd95SEwan Crawford addr_t mem_ptr = static_cast<lldb::addr_t>(result); 161215f2bd95SEwan Crawford allocation->data_ptr = mem_ptr; 161315f2bd95SEwan Crawford 161415f2bd95SEwan Crawford return true; 161515f2bd95SEwan Crawford } 161615f2bd95SEwan Crawford 161715f2bd95SEwan Crawford // JITs the RS runtime for the internal pointer to the RS Type of an allocation 161815f2bd95SEwan Crawford // Then sets the type_ptr member in Allocation with the result. 161915f2bd95SEwan Crawford // Returns true on success, false otherwise 162015f2bd95SEwan Crawford bool 162115f2bd95SEwan Crawford RenderScriptRuntime::JITTypePointer(AllocationDetails *allocation, StackFrame *frame_ptr) 162215f2bd95SEwan Crawford { 162315f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 162415f2bd95SEwan Crawford 162515f2bd95SEwan Crawford if (!allocation->address.isValid() || !allocation->context.isValid()) 162615f2bd95SEwan Crawford { 162715f2bd95SEwan Crawford if (log) 1628b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 162915f2bd95SEwan Crawford return false; 163015f2bd95SEwan Crawford } 163115f2bd95SEwan Crawford 1632ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprAllocGetType); 1633ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 163415f2bd95SEwan Crawford 1635ea0636b5SEwan Crawford int chars_written = 1636ea0636b5SEwan Crawford snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->context.get(), *allocation->address.get()); 163715f2bd95SEwan Crawford if (chars_written < 0) 163815f2bd95SEwan Crawford { 163915f2bd95SEwan Crawford if (log) 1640b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 164115f2bd95SEwan Crawford return false; 164215f2bd95SEwan Crawford } 1643ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 164415f2bd95SEwan Crawford { 164515f2bd95SEwan Crawford if (log) 1646b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 164715f2bd95SEwan Crawford return false; 164815f2bd95SEwan Crawford } 164915f2bd95SEwan Crawford 165015f2bd95SEwan Crawford uint64_t result = 0; 165115f2bd95SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 165215f2bd95SEwan Crawford return false; 165315f2bd95SEwan Crawford 165415f2bd95SEwan Crawford addr_t type_ptr = static_cast<lldb::addr_t>(result); 165515f2bd95SEwan Crawford allocation->type_ptr = type_ptr; 165615f2bd95SEwan Crawford 165715f2bd95SEwan Crawford return true; 165815f2bd95SEwan Crawford } 165915f2bd95SEwan Crawford 166015f2bd95SEwan Crawford // JITs the RS runtime for information about the dimensions and type of an allocation 166115f2bd95SEwan Crawford // Then sets dimension and element_ptr members in Allocation with the result. 166215f2bd95SEwan Crawford // Returns true on success, false otherwise 166315f2bd95SEwan Crawford bool 166415f2bd95SEwan Crawford RenderScriptRuntime::JITTypePacked(AllocationDetails *allocation, StackFrame *frame_ptr) 166515f2bd95SEwan Crawford { 166615f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 166715f2bd95SEwan Crawford 166815f2bd95SEwan Crawford if (!allocation->type_ptr.isValid() || !allocation->context.isValid()) 166915f2bd95SEwan Crawford { 167015f2bd95SEwan Crawford if (log) 1671b3f7f69dSAidan Dodds log->Printf("%s - Failed to find allocation details.", __FUNCTION__); 167215f2bd95SEwan Crawford return false; 167315f2bd95SEwan Crawford } 167415f2bd95SEwan Crawford 167515f2bd95SEwan Crawford // Expression is different depending on if device is 32 or 64 bit 167615f2bd95SEwan Crawford uint32_t archByteSize = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize(); 1677b3f7f69dSAidan Dodds const uint32_t bits = archByteSize == 4 ? 32 : 64; 167815f2bd95SEwan Crawford 167915f2bd95SEwan Crawford // We want 4 elements from packed data 1680b3f7f69dSAidan Dodds const uint32_t num_exprs = 4; 168115f2bd95SEwan Crawford assert(num_exprs == (eExprTypeElemPtr - eExprTypeDimX + 1) && "Invalid number of expressions"); 168215f2bd95SEwan Crawford 1683ea0636b5SEwan Crawford char buffer[num_exprs][jit_max_expr_size]; 168415f2bd95SEwan Crawford uint64_t results[num_exprs]; 168515f2bd95SEwan Crawford 1686b3f7f69dSAidan Dodds for (uint32_t i = 0; i < num_exprs; ++i) 168715f2bd95SEwan Crawford { 1688ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(ExpressionStrings(eExprTypeDimX + i)); 1689ea0636b5SEwan Crawford int chars_written = snprintf(buffer[i], jit_max_expr_size, expr_cstr, bits, *allocation->context.get(), 1690ea0636b5SEwan Crawford *allocation->type_ptr.get()); 169115f2bd95SEwan Crawford if (chars_written < 0) 169215f2bd95SEwan Crawford { 169315f2bd95SEwan Crawford if (log) 1694b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 169515f2bd95SEwan Crawford return false; 169615f2bd95SEwan Crawford } 1697ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 169815f2bd95SEwan Crawford { 169915f2bd95SEwan Crawford if (log) 1700b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 170115f2bd95SEwan Crawford return false; 170215f2bd95SEwan Crawford } 170315f2bd95SEwan Crawford 170415f2bd95SEwan Crawford // Perform expression evaluation 170515f2bd95SEwan Crawford if (!EvalRSExpression(buffer[i], frame_ptr, &results[i])) 170615f2bd95SEwan Crawford return false; 170715f2bd95SEwan Crawford } 170815f2bd95SEwan Crawford 170915f2bd95SEwan Crawford // Assign results to allocation members 171015f2bd95SEwan Crawford AllocationDetails::Dimension dims; 171115f2bd95SEwan Crawford dims.dim_1 = static_cast<uint32_t>(results[0]); 171215f2bd95SEwan Crawford dims.dim_2 = static_cast<uint32_t>(results[1]); 171315f2bd95SEwan Crawford dims.dim_3 = static_cast<uint32_t>(results[2]); 171415f2bd95SEwan Crawford allocation->dimension = dims; 171515f2bd95SEwan Crawford 171615f2bd95SEwan Crawford addr_t elem_ptr = static_cast<lldb::addr_t>(results[3]); 17178b244e21SEwan Crawford allocation->element.element_ptr = elem_ptr; 171815f2bd95SEwan Crawford 171915f2bd95SEwan Crawford if (log) 1720b3f7f69dSAidan Dodds log->Printf("%s - dims (%" PRIu32 ", %" PRIu32 ", %" PRIu32 ") Element*: 0x%" PRIx64 ".", __FUNCTION__, 172115f2bd95SEwan Crawford dims.dim_1, dims.dim_2, dims.dim_3, elem_ptr); 172215f2bd95SEwan Crawford 172315f2bd95SEwan Crawford return true; 172415f2bd95SEwan Crawford } 172515f2bd95SEwan Crawford 172615f2bd95SEwan Crawford // JITs the RS runtime for information about the Element of an allocation 17278b244e21SEwan Crawford // Then sets type, type_vec_size, field_count and type_kind members in Element with the result. 172815f2bd95SEwan Crawford // Returns true on success, false otherwise 172915f2bd95SEwan Crawford bool 17308b244e21SEwan Crawford RenderScriptRuntime::JITElementPacked(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr) 173115f2bd95SEwan Crawford { 173215f2bd95SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 173315f2bd95SEwan Crawford 17348b244e21SEwan Crawford if (!elem.element_ptr.isValid()) 173515f2bd95SEwan Crawford { 173615f2bd95SEwan Crawford if (log) 1737b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 173815f2bd95SEwan Crawford return false; 173915f2bd95SEwan Crawford } 174015f2bd95SEwan Crawford 17418b244e21SEwan Crawford // We want 4 elements from packed data 1742b3f7f69dSAidan Dodds const uint32_t num_exprs = 4; 17438b244e21SEwan Crawford assert(num_exprs == (eExprElementFieldCount - eExprElementType + 1) && "Invalid number of expressions"); 174415f2bd95SEwan Crawford 1745ea0636b5SEwan Crawford char buffer[num_exprs][jit_max_expr_size]; 174615f2bd95SEwan Crawford uint64_t results[num_exprs]; 174715f2bd95SEwan Crawford 1748b3f7f69dSAidan Dodds for (uint32_t i = 0; i < num_exprs; i++) 174915f2bd95SEwan Crawford { 1750ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(ExpressionStrings(eExprElementType + i)); 1751ea0636b5SEwan Crawford int chars_written = snprintf(buffer[i], jit_max_expr_size, expr_cstr, context, *elem.element_ptr.get()); 175215f2bd95SEwan Crawford if (chars_written < 0) 175315f2bd95SEwan Crawford { 175415f2bd95SEwan Crawford if (log) 1755b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 175615f2bd95SEwan Crawford return false; 175715f2bd95SEwan Crawford } 1758ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 175915f2bd95SEwan Crawford { 176015f2bd95SEwan Crawford if (log) 1761b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 176215f2bd95SEwan Crawford return false; 176315f2bd95SEwan Crawford } 176415f2bd95SEwan Crawford 176515f2bd95SEwan Crawford // Perform expression evaluation 176615f2bd95SEwan Crawford if (!EvalRSExpression(buffer[i], frame_ptr, &results[i])) 176715f2bd95SEwan Crawford return false; 176815f2bd95SEwan Crawford } 176915f2bd95SEwan Crawford 177015f2bd95SEwan Crawford // Assign results to allocation members 17718b244e21SEwan Crawford elem.type = static_cast<RenderScriptRuntime::Element::DataType>(results[0]); 17728b244e21SEwan Crawford elem.type_kind = static_cast<RenderScriptRuntime::Element::DataKind>(results[1]); 17738b244e21SEwan Crawford elem.type_vec_size = static_cast<uint32_t>(results[2]); 17748b244e21SEwan Crawford elem.field_count = static_cast<uint32_t>(results[3]); 177515f2bd95SEwan Crawford 177615f2bd95SEwan Crawford if (log) 1777b3f7f69dSAidan Dodds log->Printf("%s - data type %" PRIu32 ", pixel type %" PRIu32 ", vector size %" PRIu32 ", field count %" PRIu32, 1778b3f7f69dSAidan Dodds __FUNCTION__, *elem.type.get(), *elem.type_kind.get(), *elem.type_vec_size.get(), *elem.field_count.get()); 17798b244e21SEwan Crawford 17808b244e21SEwan Crawford // If this Element has subelements then JIT rsaElementGetSubElements() for details about its fields 17818b244e21SEwan Crawford if (*elem.field_count.get() > 0 && !JITSubelements(elem, context, frame_ptr)) 17828b244e21SEwan Crawford return false; 17838b244e21SEwan Crawford 17848b244e21SEwan Crawford return true; 17858b244e21SEwan Crawford } 17868b244e21SEwan Crawford 17878b244e21SEwan Crawford // JITs the RS runtime for information about the subelements/fields of a struct allocation 17888b244e21SEwan Crawford // This is necessary for infering the struct type so we can pretty print the allocation's contents. 17898b244e21SEwan Crawford // Returns true on success, false otherwise 17908b244e21SEwan Crawford bool 17918b244e21SEwan Crawford RenderScriptRuntime::JITSubelements(Element &elem, const lldb::addr_t context, StackFrame *frame_ptr) 17928b244e21SEwan Crawford { 17938b244e21SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 17948b244e21SEwan Crawford 17958b244e21SEwan Crawford if (!elem.element_ptr.isValid() || !elem.field_count.isValid()) 17968b244e21SEwan Crawford { 17978b244e21SEwan Crawford if (log) 1798b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 17998b244e21SEwan Crawford return false; 18008b244e21SEwan Crawford } 18018b244e21SEwan Crawford 18028b244e21SEwan Crawford const short num_exprs = 3; 18038b244e21SEwan Crawford assert(num_exprs == (eExprSubelementsArrSize - eExprSubelementsId + 1) && "Invalid number of expressions"); 18048b244e21SEwan Crawford 1805ea0636b5SEwan Crawford char expr_buffer[jit_max_expr_size]; 18068b244e21SEwan Crawford uint64_t results; 18078b244e21SEwan Crawford 18088b244e21SEwan Crawford // Iterate over struct fields. 18098b244e21SEwan Crawford const uint32_t field_count = *elem.field_count.get(); 1810b3f7f69dSAidan Dodds for (uint32_t field_index = 0; field_index < field_count; ++field_index) 18118b244e21SEwan Crawford { 18128b244e21SEwan Crawford Element child; 1813b3f7f69dSAidan Dodds for (uint32_t expr_index = 0; expr_index < num_exprs; ++expr_index) 18148b244e21SEwan Crawford { 1815ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(ExpressionStrings(eExprSubelementsId + expr_index)); 1816ea0636b5SEwan Crawford int chars_written = snprintf(expr_buffer, jit_max_expr_size, expr_cstr, 18178b244e21SEwan Crawford field_count, field_count, field_count, 18188b244e21SEwan Crawford context, *elem.element_ptr.get(), field_count, field_index); 18198b244e21SEwan Crawford if (chars_written < 0) 18208b244e21SEwan Crawford { 18218b244e21SEwan Crawford if (log) 1822b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 18238b244e21SEwan Crawford return false; 18248b244e21SEwan Crawford } 1825ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 18268b244e21SEwan Crawford { 18278b244e21SEwan Crawford if (log) 1828b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 18298b244e21SEwan Crawford return false; 18308b244e21SEwan Crawford } 18318b244e21SEwan Crawford 18328b244e21SEwan Crawford // Perform expression evaluation 18338b244e21SEwan Crawford if (!EvalRSExpression(expr_buffer, frame_ptr, &results)) 18348b244e21SEwan Crawford return false; 18358b244e21SEwan Crawford 18368b244e21SEwan Crawford if (log) 1837b3f7f69dSAidan Dodds log->Printf("%s - expr result 0x%" PRIx64 ".", __FUNCTION__, results); 18388b244e21SEwan Crawford 18398b244e21SEwan Crawford switch (expr_index) 18408b244e21SEwan Crawford { 18418b244e21SEwan Crawford case 0: // Element* of child 18428b244e21SEwan Crawford child.element_ptr = static_cast<addr_t>(results); 18438b244e21SEwan Crawford break; 18448b244e21SEwan Crawford case 1: // Name of child 18458b244e21SEwan Crawford { 18468b244e21SEwan Crawford lldb::addr_t address = static_cast<addr_t>(results); 18478b244e21SEwan Crawford Error err; 18488b244e21SEwan Crawford std::string name; 18498b244e21SEwan Crawford GetProcess()->ReadCStringFromMemory(address, name, err); 18508b244e21SEwan Crawford if (!err.Fail()) 18518b244e21SEwan Crawford child.type_name = ConstString(name); 18528b244e21SEwan Crawford else 18538b244e21SEwan Crawford { 18548b244e21SEwan Crawford if (log) 1855b3f7f69dSAidan Dodds log->Printf("%s - warning: Couldn't read field name.", __FUNCTION__); 18568b244e21SEwan Crawford } 18578b244e21SEwan Crawford break; 18588b244e21SEwan Crawford } 18598b244e21SEwan Crawford case 2: // Array size of child 18608b244e21SEwan Crawford child.array_size = static_cast<uint32_t>(results); 18618b244e21SEwan Crawford break; 18628b244e21SEwan Crawford } 18638b244e21SEwan Crawford } 18648b244e21SEwan Crawford 18658b244e21SEwan Crawford // We need to recursively JIT each Element field of the struct since 18668b244e21SEwan Crawford // structs can be nested inside structs. 18678b244e21SEwan Crawford if (!JITElementPacked(child, context, frame_ptr)) 18688b244e21SEwan Crawford return false; 18698b244e21SEwan Crawford elem.children.push_back(child); 18708b244e21SEwan Crawford } 18718b244e21SEwan Crawford 18728b244e21SEwan Crawford // Try to infer the name of the struct type so we can pretty print the allocation contents. 18738b244e21SEwan Crawford FindStructTypeName(elem, frame_ptr); 187415f2bd95SEwan Crawford 187515f2bd95SEwan Crawford return true; 187615f2bd95SEwan Crawford } 187715f2bd95SEwan Crawford 1878a0f08674SEwan Crawford // JITs the RS runtime for the address of the last element in the allocation. 1879a0f08674SEwan Crawford // The `elem_size` paramter represents the size of a single element, including padding. 1880a0f08674SEwan Crawford // Which is needed as an offset from the last element pointer. 1881a0f08674SEwan Crawford // Using this offset minus the starting address we can calculate the size of the allocation. 1882a0f08674SEwan Crawford // Returns true on success, false otherwise 1883a0f08674SEwan Crawford bool 18848b244e21SEwan Crawford RenderScriptRuntime::JITAllocationSize(AllocationDetails *allocation, StackFrame *frame_ptr) 1885a0f08674SEwan Crawford { 1886a0f08674SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1887a0f08674SEwan Crawford 1888b3f7f69dSAidan Dodds if (!allocation->address.isValid() || !allocation->dimension.isValid() || !allocation->data_ptr.isValid() || 1889b3f7f69dSAidan Dodds !allocation->element.datum_size.isValid()) 1890a0f08674SEwan Crawford { 1891a0f08674SEwan Crawford if (log) 1892b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 1893a0f08674SEwan Crawford return false; 1894a0f08674SEwan Crawford } 1895a0f08674SEwan Crawford 1896a0f08674SEwan Crawford // Find dimensions 1897b3f7f69dSAidan Dodds uint32_t dim_x = allocation->dimension.get()->dim_1; 1898b3f7f69dSAidan Dodds uint32_t dim_y = allocation->dimension.get()->dim_2; 1899b3f7f69dSAidan Dodds uint32_t dim_z = allocation->dimension.get()->dim_3; 1900a0f08674SEwan Crawford 19018b244e21SEwan Crawford // Our plan of jitting the last element address doesn't seem to work for struct Allocations 19028b244e21SEwan Crawford // Instead try to infer the size ourselves without any inter element padding. 19038b244e21SEwan Crawford if (allocation->element.children.size() > 0) 19048b244e21SEwan Crawford { 19058b244e21SEwan Crawford if (dim_x == 0) dim_x = 1; 19068b244e21SEwan Crawford if (dim_y == 0) dim_y = 1; 19078b244e21SEwan Crawford if (dim_z == 0) dim_z = 1; 19088b244e21SEwan Crawford 19098b244e21SEwan Crawford allocation->size = dim_x * dim_y * dim_z * *allocation->element.datum_size.get(); 19108b244e21SEwan Crawford 19118b244e21SEwan Crawford if (log) 1912b3f7f69dSAidan Dodds log->Printf("%s - infered size of struct allocation %" PRIu32 ".", __FUNCTION__, 1913b3f7f69dSAidan Dodds *allocation->size.get()); 19148b244e21SEwan Crawford return true; 19158b244e21SEwan Crawford } 19168b244e21SEwan Crawford 1917ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprGetOffsetPtr); 1918ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 19198b244e21SEwan Crawford 1920a0f08674SEwan Crawford // Calculate last element 1921a0f08674SEwan Crawford dim_x = dim_x == 0 ? 0 : dim_x - 1; 1922a0f08674SEwan Crawford dim_y = dim_y == 0 ? 0 : dim_y - 1; 1923a0f08674SEwan Crawford dim_z = dim_z == 0 ? 0 : dim_z - 1; 1924a0f08674SEwan Crawford 1925ea0636b5SEwan Crawford int chars_written = snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->address.get(), dim_x, dim_y, dim_z); 1926a0f08674SEwan Crawford if (chars_written < 0) 1927a0f08674SEwan Crawford { 1928a0f08674SEwan Crawford if (log) 1929b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 1930a0f08674SEwan Crawford return false; 1931a0f08674SEwan Crawford } 1932ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 1933a0f08674SEwan Crawford { 1934a0f08674SEwan Crawford if (log) 1935b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 1936a0f08674SEwan Crawford return false; 1937a0f08674SEwan Crawford } 1938a0f08674SEwan Crawford 1939a0f08674SEwan Crawford uint64_t result = 0; 1940a0f08674SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 1941a0f08674SEwan Crawford return false; 1942a0f08674SEwan Crawford 1943a0f08674SEwan Crawford addr_t mem_ptr = static_cast<lldb::addr_t>(result); 1944a0f08674SEwan Crawford // Find pointer to last element and add on size of an element 1945b3f7f69dSAidan Dodds allocation->size = 1946b3f7f69dSAidan Dodds static_cast<uint32_t>(mem_ptr - *allocation->data_ptr.get()) + *allocation->element.datum_size.get(); 1947a0f08674SEwan Crawford 1948a0f08674SEwan Crawford return true; 1949a0f08674SEwan Crawford } 1950a0f08674SEwan Crawford 1951a0f08674SEwan Crawford // JITs the RS runtime for information about the stride between rows in the allocation. 1952a0f08674SEwan Crawford // This is done to detect padding, since allocated memory is 16-byte aligned. 1953a0f08674SEwan Crawford // Returns true on success, false otherwise 1954a0f08674SEwan Crawford bool 1955a0f08674SEwan Crawford RenderScriptRuntime::JITAllocationStride(AllocationDetails *allocation, StackFrame *frame_ptr) 1956a0f08674SEwan Crawford { 1957a0f08674SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 1958a0f08674SEwan Crawford 1959a0f08674SEwan Crawford if (!allocation->address.isValid() || !allocation->data_ptr.isValid()) 1960a0f08674SEwan Crawford { 1961a0f08674SEwan Crawford if (log) 1962b3f7f69dSAidan Dodds log->Printf("%s - failed to find allocation details.", __FUNCTION__); 1963a0f08674SEwan Crawford return false; 1964a0f08674SEwan Crawford } 1965a0f08674SEwan Crawford 1966ea0636b5SEwan Crawford const char *expr_cstr = JITTemplate(eExprGetOffsetPtr); 1967ea0636b5SEwan Crawford char buffer[jit_max_expr_size]; 1968a0f08674SEwan Crawford 1969ea0636b5SEwan Crawford int chars_written = snprintf(buffer, jit_max_expr_size, expr_cstr, *allocation->address.get(), 0, 1, 0); 1970a0f08674SEwan Crawford if (chars_written < 0) 1971a0f08674SEwan Crawford { 1972a0f08674SEwan Crawford if (log) 1973b3f7f69dSAidan Dodds log->Printf("%s - encoding error in snprintf().", __FUNCTION__); 1974a0f08674SEwan Crawford return false; 1975a0f08674SEwan Crawford } 1976ea0636b5SEwan Crawford else if (chars_written >= jit_max_expr_size) 1977a0f08674SEwan Crawford { 1978a0f08674SEwan Crawford if (log) 1979b3f7f69dSAidan Dodds log->Printf("%s - expression too long.", __FUNCTION__); 1980a0f08674SEwan Crawford return false; 1981a0f08674SEwan Crawford } 1982a0f08674SEwan Crawford 1983a0f08674SEwan Crawford uint64_t result = 0; 1984a0f08674SEwan Crawford if (!EvalRSExpression(buffer, frame_ptr, &result)) 1985a0f08674SEwan Crawford return false; 1986a0f08674SEwan Crawford 1987a0f08674SEwan Crawford addr_t mem_ptr = static_cast<lldb::addr_t>(result); 1988a0f08674SEwan Crawford allocation->stride = static_cast<uint32_t>(mem_ptr - *allocation->data_ptr.get()); 1989a0f08674SEwan Crawford 1990a0f08674SEwan Crawford return true; 1991a0f08674SEwan Crawford } 1992a0f08674SEwan Crawford 199315f2bd95SEwan Crawford // JIT all the current runtime info regarding an allocation 199415f2bd95SEwan Crawford bool 199515f2bd95SEwan Crawford RenderScriptRuntime::RefreshAllocation(AllocationDetails *allocation, StackFrame *frame_ptr) 199615f2bd95SEwan Crawford { 199715f2bd95SEwan Crawford // GetOffsetPointer() 199815f2bd95SEwan Crawford if (!JITDataPointer(allocation, frame_ptr)) 199915f2bd95SEwan Crawford return false; 200015f2bd95SEwan Crawford 200115f2bd95SEwan Crawford // rsaAllocationGetType() 200215f2bd95SEwan Crawford if (!JITTypePointer(allocation, frame_ptr)) 200315f2bd95SEwan Crawford return false; 200415f2bd95SEwan Crawford 200515f2bd95SEwan Crawford // rsaTypeGetNativeData() 200615f2bd95SEwan Crawford if (!JITTypePacked(allocation, frame_ptr)) 200715f2bd95SEwan Crawford return false; 200815f2bd95SEwan Crawford 200915f2bd95SEwan Crawford // rsaElementGetNativeData() 20108b244e21SEwan Crawford if (!JITElementPacked(allocation->element, *allocation->context.get(), frame_ptr)) 201115f2bd95SEwan Crawford return false; 201215f2bd95SEwan Crawford 20138b244e21SEwan Crawford // Sets the datum_size member in Element 20148b244e21SEwan Crawford SetElementSize(allocation->element); 20158b244e21SEwan Crawford 201655232f09SEwan Crawford // Use GetOffsetPointer() to infer size of the allocation 20178b244e21SEwan Crawford if (!JITAllocationSize(allocation, frame_ptr)) 201855232f09SEwan Crawford return false; 201955232f09SEwan Crawford 202055232f09SEwan Crawford return true; 202155232f09SEwan Crawford } 202255232f09SEwan Crawford 20238b244e21SEwan Crawford // Function attempts to set the type_name member of the paramaterised Element object. 20248b244e21SEwan Crawford // This string should be the name of the struct type the Element represents. 20258b244e21SEwan Crawford // We need this string for pretty printing the Element to users. 20268b244e21SEwan Crawford void 20278b244e21SEwan Crawford RenderScriptRuntime::FindStructTypeName(Element &elem, StackFrame *frame_ptr) 202855232f09SEwan Crawford { 20298b244e21SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 20308b244e21SEwan Crawford 20318b244e21SEwan Crawford if (!elem.type_name.IsEmpty()) // Name already set 20328b244e21SEwan Crawford return; 20338b244e21SEwan Crawford else 2034fe06b5adSAdrian McCarthy elem.type_name = Element::GetFallbackStructName(); // Default type name if we don't succeed 20358b244e21SEwan Crawford 20368b244e21SEwan Crawford // Find all the global variables from the script rs modules 20378b244e21SEwan Crawford VariableList variable_list; 20388b244e21SEwan Crawford for (auto module_sp : m_rsmodules) 20398b244e21SEwan Crawford module_sp->m_module->FindGlobalVariables(RegularExpression("."), true, UINT32_MAX, variable_list); 20408b244e21SEwan Crawford 20418b244e21SEwan Crawford // Iterate over all the global variables looking for one with a matching type to the Element. 20428b244e21SEwan Crawford // We make the assumption a match exists since there needs to be a global variable to reflect the 20438b244e21SEwan Crawford // struct type back into java host code. 20448b244e21SEwan Crawford for (uint32_t var_index = 0; var_index < variable_list.GetSize(); ++var_index) 20458b244e21SEwan Crawford { 20468b244e21SEwan Crawford const VariableSP var_sp(variable_list.GetVariableAtIndex(var_index)); 20478b244e21SEwan Crawford if (!var_sp) 20488b244e21SEwan Crawford continue; 20498b244e21SEwan Crawford 20508b244e21SEwan Crawford ValueObjectSP valobj_sp = ValueObjectVariable::Create(frame_ptr, var_sp); 20518b244e21SEwan Crawford if (!valobj_sp) 20528b244e21SEwan Crawford continue; 20538b244e21SEwan Crawford 20548b244e21SEwan Crawford // Find the number of variable fields. 20558b244e21SEwan Crawford // If it has no fields, or more fields than our Element, then it can't be the struct we're looking for. 20568b244e21SEwan Crawford // Don't check for equality since RS can add extra struct members for padding. 20578b244e21SEwan Crawford size_t num_children = valobj_sp->GetNumChildren(); 20588b244e21SEwan Crawford if (num_children > elem.children.size() || num_children == 0) 20598b244e21SEwan Crawford continue; 20608b244e21SEwan Crawford 20618b244e21SEwan Crawford // Iterate over children looking for members with matching field names. 20628b244e21SEwan Crawford // If all the field names match, this is likely the struct we want. 20638b244e21SEwan Crawford // 20648b244e21SEwan Crawford // TODO: This could be made more robust by also checking children data sizes, or array size 20658b244e21SEwan Crawford bool found = true; 20668b244e21SEwan Crawford for (size_t child_index = 0; child_index < num_children; ++child_index) 20678b244e21SEwan Crawford { 20688b244e21SEwan Crawford ValueObjectSP child = valobj_sp->GetChildAtIndex(child_index, true); 20698b244e21SEwan Crawford if (!child || (child->GetName() != elem.children[child_index].type_name)) 20708b244e21SEwan Crawford { 20718b244e21SEwan Crawford found = false; 20728b244e21SEwan Crawford break; 20738b244e21SEwan Crawford } 20748b244e21SEwan Crawford } 20758b244e21SEwan Crawford 20768b244e21SEwan Crawford // RS can add extra struct members for padding in the format '#rs_padding_[0-9]+' 20778b244e21SEwan Crawford if (found && num_children < elem.children.size()) 20788b244e21SEwan Crawford { 2079b3f7f69dSAidan Dodds const uint32_t size_diff = elem.children.size() - num_children; 20808b244e21SEwan Crawford if (log) 2081b3f7f69dSAidan Dodds log->Printf("%s - %" PRIu32 " padding struct entries", __FUNCTION__, size_diff); 20828b244e21SEwan Crawford 2083b3f7f69dSAidan Dodds for (uint32_t padding_index = 0; padding_index < size_diff; ++padding_index) 20848b244e21SEwan Crawford { 20858b244e21SEwan Crawford const ConstString &name = elem.children[num_children + padding_index].type_name; 20868b244e21SEwan Crawford if (strcmp(name.AsCString(), "#rs_padding") < 0) 20878b244e21SEwan Crawford found = false; 20888b244e21SEwan Crawford } 20898b244e21SEwan Crawford } 20908b244e21SEwan Crawford 20918b244e21SEwan Crawford // We've found a global var with matching type 20928b244e21SEwan Crawford if (found) 20938b244e21SEwan Crawford { 20948b244e21SEwan Crawford // Dereference since our Element type isn't a pointer. 20958b244e21SEwan Crawford if (valobj_sp->IsPointerType()) 20968b244e21SEwan Crawford { 20978b244e21SEwan Crawford Error err; 20988b244e21SEwan Crawford ValueObjectSP deref_valobj = valobj_sp->Dereference(err); 20998b244e21SEwan Crawford if (!err.Fail()) 21008b244e21SEwan Crawford valobj_sp = deref_valobj; 21018b244e21SEwan Crawford } 21028b244e21SEwan Crawford 21038b244e21SEwan Crawford // Save name of variable in Element. 21048b244e21SEwan Crawford elem.type_name = valobj_sp->GetTypeName(); 21058b244e21SEwan Crawford if (log) 2106b3f7f69dSAidan Dodds log->Printf("%s - element name set to %s", __FUNCTION__, elem.type_name.AsCString()); 21078b244e21SEwan Crawford 21088b244e21SEwan Crawford return; 21098b244e21SEwan Crawford } 21108b244e21SEwan Crawford } 21118b244e21SEwan Crawford } 21128b244e21SEwan Crawford 21138b244e21SEwan Crawford // Function sets the datum_size member of Element. Representing the size of a single instance including padding. 21148b244e21SEwan Crawford // Assumes the relevant allocation information has already been jitted. 21158b244e21SEwan Crawford void 21168b244e21SEwan Crawford RenderScriptRuntime::SetElementSize(Element &elem) 21178b244e21SEwan Crawford { 21188b244e21SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 21198b244e21SEwan Crawford const Element::DataType type = *elem.type.get(); 2120b3f7f69dSAidan Dodds assert(type >= Element::RS_TYPE_NONE && type <= Element::RS_TYPE_FONT && "Invalid allocation type"); 212155232f09SEwan Crawford 2122b3f7f69dSAidan Dodds const uint32_t vec_size = *elem.type_vec_size.get(); 2123b3f7f69dSAidan Dodds uint32_t data_size = 0; 2124b3f7f69dSAidan Dodds uint32_t padding = 0; 212555232f09SEwan Crawford 21268b244e21SEwan Crawford // Element is of a struct type, calculate size recursively. 21278b244e21SEwan Crawford if ((type == Element::RS_TYPE_NONE) && (elem.children.size() > 0)) 21288b244e21SEwan Crawford { 21298b244e21SEwan Crawford for (Element &child : elem.children) 21308b244e21SEwan Crawford { 21318b244e21SEwan Crawford SetElementSize(child); 2132b3f7f69dSAidan Dodds const uint32_t array_size = child.array_size.isValid() ? *child.array_size.get() : 1; 21338b244e21SEwan Crawford data_size += *child.datum_size.get() * array_size; 21348b244e21SEwan Crawford } 21358b244e21SEwan Crawford } 2136b3f7f69dSAidan Dodds // These have been packed already 2137b3f7f69dSAidan Dodds else if (type == Element::RS_TYPE_UNSIGNED_5_6_5 || 2138b3f7f69dSAidan Dodds type == Element::RS_TYPE_UNSIGNED_5_5_5_1 || 2139b3f7f69dSAidan Dodds type == Element::RS_TYPE_UNSIGNED_4_4_4_4) 21402e920715SEwan Crawford { 21412e920715SEwan Crawford data_size = AllocationDetails::RSTypeToFormat[type][eElementSize]; 21422e920715SEwan Crawford } 21432e920715SEwan Crawford else if (type < Element::RS_TYPE_ELEMENT) 21442e920715SEwan Crawford { 21458b244e21SEwan Crawford data_size = vec_size * AllocationDetails::RSTypeToFormat[type][eElementSize]; 21462e920715SEwan Crawford if (vec_size == 3) 21472e920715SEwan Crawford padding = AllocationDetails::RSTypeToFormat[type][eElementSize]; 21482e920715SEwan Crawford } 21492e920715SEwan Crawford else 21502e920715SEwan Crawford data_size = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize(); 21518b244e21SEwan Crawford 21528b244e21SEwan Crawford elem.padding = padding; 21538b244e21SEwan Crawford elem.datum_size = data_size + padding; 21548b244e21SEwan Crawford if (log) 2155b3f7f69dSAidan Dodds log->Printf("%s - element size set to %" PRIu32, __FUNCTION__, data_size + padding); 215655232f09SEwan Crawford } 215755232f09SEwan Crawford 215855232f09SEwan Crawford // Given an allocation, this function copies the allocation contents from device into a buffer on the heap. 215955232f09SEwan Crawford // Returning a shared pointer to the buffer containing the data. 216055232f09SEwan Crawford std::shared_ptr<uint8_t> 216155232f09SEwan Crawford RenderScriptRuntime::GetAllocationData(AllocationDetails *allocation, StackFrame *frame_ptr) 216255232f09SEwan Crawford { 216355232f09SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 216455232f09SEwan Crawford 216555232f09SEwan Crawford // JIT all the allocation details 21668b59062aSEwan Crawford if (allocation->shouldRefresh()) 216755232f09SEwan Crawford { 216855232f09SEwan Crawford if (log) 2169b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info", __FUNCTION__); 217055232f09SEwan Crawford 217155232f09SEwan Crawford if (!RefreshAllocation(allocation, frame_ptr)) 217255232f09SEwan Crawford { 217355232f09SEwan Crawford if (log) 2174b3f7f69dSAidan Dodds log->Printf("%s - couldn't JIT allocation details", __FUNCTION__); 217555232f09SEwan Crawford return nullptr; 217655232f09SEwan Crawford } 217755232f09SEwan Crawford } 217855232f09SEwan Crawford 2179b3f7f69dSAidan Dodds assert(allocation->data_ptr.isValid() && allocation->element.type.isValid() && 2180b3f7f69dSAidan Dodds allocation->element.type_vec_size.isValid() && allocation->size.isValid() && 2181b3f7f69dSAidan Dodds "Allocation information not available"); 218255232f09SEwan Crawford 218355232f09SEwan Crawford // Allocate a buffer to copy data into 2184b3f7f69dSAidan Dodds const uint32_t size = *allocation->size.get(); 218555232f09SEwan Crawford std::shared_ptr<uint8_t> buffer(new uint8_t[size]); 218655232f09SEwan Crawford if (!buffer) 218755232f09SEwan Crawford { 218855232f09SEwan Crawford if (log) 2189b3f7f69dSAidan Dodds log->Printf("%s - couldn't allocate a %" PRIu32 " byte buffer", __FUNCTION__, size); 219055232f09SEwan Crawford return nullptr; 219155232f09SEwan Crawford } 219255232f09SEwan Crawford 219355232f09SEwan Crawford // Read the inferior memory 219455232f09SEwan Crawford Error error; 219555232f09SEwan Crawford lldb::addr_t data_ptr = *allocation->data_ptr.get(); 219655232f09SEwan Crawford GetProcess()->ReadMemory(data_ptr, buffer.get(), size, error); 219755232f09SEwan Crawford if (error.Fail()) 219855232f09SEwan Crawford { 219955232f09SEwan Crawford if (log) 2200b3f7f69dSAidan Dodds log->Printf("%s - '%s' Couldn't read %" PRIu32 " bytes of allocation data from 0x%" PRIx64, 2201b3f7f69dSAidan Dodds __FUNCTION__, error.AsCString(), size, data_ptr); 220255232f09SEwan Crawford return nullptr; 220355232f09SEwan Crawford } 220455232f09SEwan Crawford 220555232f09SEwan Crawford return buffer; 220655232f09SEwan Crawford } 220755232f09SEwan Crawford 220855232f09SEwan Crawford // Function copies data from a binary file into an allocation. 220955232f09SEwan Crawford // There is a header at the start of the file, FileHeader, before the data content itself. 221055232f09SEwan Crawford // Information from this header is used to display warnings to the user about incompatabilities 221155232f09SEwan Crawford bool 221255232f09SEwan Crawford RenderScriptRuntime::LoadAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr) 221355232f09SEwan Crawford { 221455232f09SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 221555232f09SEwan Crawford 221655232f09SEwan Crawford // Find allocation with the given id 221755232f09SEwan Crawford AllocationDetails *alloc = FindAllocByID(strm, alloc_id); 221855232f09SEwan Crawford if (!alloc) 221955232f09SEwan Crawford return false; 222055232f09SEwan Crawford 222155232f09SEwan Crawford if (log) 2222b3f7f69dSAidan Dodds log->Printf("%s - found allocation 0x%" PRIx64, __FUNCTION__, *alloc->address.get()); 222355232f09SEwan Crawford 222455232f09SEwan Crawford // JIT all the allocation details 22258b59062aSEwan Crawford if (alloc->shouldRefresh()) 222655232f09SEwan Crawford { 222755232f09SEwan Crawford if (log) 2228b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info.", __FUNCTION__); 222955232f09SEwan Crawford 223055232f09SEwan Crawford if (!RefreshAllocation(alloc, frame_ptr)) 223155232f09SEwan Crawford { 223255232f09SEwan Crawford if (log) 2233b3f7f69dSAidan Dodds log->Printf("%s - couldn't JIT allocation details", __FUNCTION__); 22344cfc9198SSylvestre Ledru return false; 223555232f09SEwan Crawford } 223655232f09SEwan Crawford } 223755232f09SEwan Crawford 2238b3f7f69dSAidan Dodds assert(alloc->data_ptr.isValid() && alloc->element.type.isValid() && alloc->element.type_vec_size.isValid() && 2239b3f7f69dSAidan Dodds alloc->size.isValid() && alloc->element.datum_size.isValid() && "Allocation information not available"); 224055232f09SEwan Crawford 224155232f09SEwan Crawford // Check we can read from file 224255232f09SEwan Crawford FileSpec file(filename, true); 224355232f09SEwan Crawford if (!file.Exists()) 224455232f09SEwan Crawford { 224555232f09SEwan Crawford strm.Printf("Error: File %s does not exist", filename); 224655232f09SEwan Crawford strm.EOL(); 224755232f09SEwan Crawford return false; 224855232f09SEwan Crawford } 224955232f09SEwan Crawford 225055232f09SEwan Crawford if (!file.Readable()) 225155232f09SEwan Crawford { 225255232f09SEwan Crawford strm.Printf("Error: File %s does not have readable permissions", filename); 225355232f09SEwan Crawford strm.EOL(); 225455232f09SEwan Crawford return false; 225555232f09SEwan Crawford } 225655232f09SEwan Crawford 225755232f09SEwan Crawford // Read file into data buffer 225855232f09SEwan Crawford DataBufferSP data_sp(file.ReadFileContents()); 225955232f09SEwan Crawford 226055232f09SEwan Crawford // Cast start of buffer to FileHeader and use pointer to read metadata 226155232f09SEwan Crawford void *file_buffer = data_sp->GetBytes(); 2262b3f7f69dSAidan Dodds if (file_buffer == nullptr || 2263b3f7f69dSAidan Dodds data_sp->GetByteSize() < (sizeof(AllocationDetails::FileHeader) + sizeof(AllocationDetails::ElementHeader))) 226426e52a70SEwan Crawford { 226526e52a70SEwan Crawford strm.Printf("Error: File %s does not contain enough data for header", filename); 226626e52a70SEwan Crawford strm.EOL(); 226726e52a70SEwan Crawford return false; 226826e52a70SEwan Crawford } 226926e52a70SEwan Crawford const AllocationDetails::FileHeader *file_header = static_cast<AllocationDetails::FileHeader *>(file_buffer); 227055232f09SEwan Crawford 227126e52a70SEwan Crawford // Check file starts with ascii characters "RSAD" 2272b3f7f69dSAidan Dodds if (memcmp(file_header->ident, "RSAD", 4)) 227326e52a70SEwan Crawford { 227426e52a70SEwan Crawford strm.Printf("Error: File doesn't contain identifier for an RS allocation dump. Are you sure this is the correct file?"); 227526e52a70SEwan Crawford strm.EOL(); 227626e52a70SEwan Crawford return false; 227726e52a70SEwan Crawford } 227826e52a70SEwan Crawford 227926e52a70SEwan Crawford // Look at the type of the root element in the header 228026e52a70SEwan Crawford AllocationDetails::ElementHeader root_element_header; 228126e52a70SEwan Crawford memcpy(&root_element_header, static_cast<uint8_t *>(file_buffer) + sizeof(AllocationDetails::FileHeader), 228226e52a70SEwan Crawford sizeof(AllocationDetails::ElementHeader)); 228355232f09SEwan Crawford 228455232f09SEwan Crawford if (log) 2285b3f7f69dSAidan Dodds log->Printf("%s - header type %" PRIu32 ", element size %" PRIu32, __FUNCTION__, 228626e52a70SEwan Crawford root_element_header.type, root_element_header.element_size); 228755232f09SEwan Crawford 228855232f09SEwan Crawford // Check if the target allocation and file both have the same number of bytes for an Element 228926e52a70SEwan Crawford if (*alloc->element.datum_size.get() != root_element_header.element_size) 229055232f09SEwan Crawford { 2291b3f7f69dSAidan Dodds strm.Printf("Warning: Mismatched Element sizes - file %" PRIu32 " bytes, allocation %" PRIu32 " bytes", 229226e52a70SEwan Crawford root_element_header.element_size, *alloc->element.datum_size.get()); 229355232f09SEwan Crawford strm.EOL(); 229455232f09SEwan Crawford } 229555232f09SEwan Crawford 229626e52a70SEwan Crawford // Check if the target allocation and file both have the same type 2297b3f7f69dSAidan Dodds const uint32_t alloc_type = static_cast<uint32_t>(*alloc->element.type.get()); 2298b3f7f69dSAidan Dodds const uint32_t file_type = root_element_header.type; 229926e52a70SEwan Crawford 230026e52a70SEwan Crawford if (file_type > Element::RS_TYPE_FONT) 230126e52a70SEwan Crawford { 230226e52a70SEwan Crawford strm.Printf("Warning: File has unknown allocation type"); 230326e52a70SEwan Crawford strm.EOL(); 230426e52a70SEwan Crawford } 230526e52a70SEwan Crawford else if (alloc_type != file_type) 230655232f09SEwan Crawford { 23072e920715SEwan Crawford // Enum value isn't monotonous, so doesn't always index RsDataTypeToString array 2308b3f7f69dSAidan Dodds uint32_t printable_target_type_index = alloc_type; 2309b3f7f69dSAidan Dodds uint32_t printable_head_type_index = file_type; 231026e52a70SEwan Crawford if (alloc_type >= Element::RS_TYPE_ELEMENT && alloc_type <= Element::RS_TYPE_FONT) 2311b3f7f69dSAidan Dodds printable_target_type_index = static_cast<Element::DataType>((alloc_type - Element::RS_TYPE_ELEMENT) + 2312b3f7f69dSAidan Dodds Element::RS_TYPE_MATRIX_2X2 + 1); 23132e920715SEwan Crawford 231426e52a70SEwan Crawford if (file_type >= Element::RS_TYPE_ELEMENT && file_type <= Element::RS_TYPE_FONT) 2315b3f7f69dSAidan Dodds printable_head_type_index = static_cast<Element::DataType>((file_type - Element::RS_TYPE_ELEMENT) + 2316b3f7f69dSAidan Dodds Element::RS_TYPE_MATRIX_2X2 + 1); 23172e920715SEwan Crawford 23182e920715SEwan Crawford const char *file_type_cstr = AllocationDetails::RsDataTypeToString[printable_head_type_index][0]; 23192e920715SEwan Crawford const char *target_type_cstr = AllocationDetails::RsDataTypeToString[printable_target_type_index][0]; 232055232f09SEwan Crawford 2321b3f7f69dSAidan Dodds strm.Printf("Warning: Mismatched Types - file '%s' type, allocation '%s' type", file_type_cstr, 2322b3f7f69dSAidan Dodds target_type_cstr); 232355232f09SEwan Crawford strm.EOL(); 232455232f09SEwan Crawford } 232555232f09SEwan Crawford 232626e52a70SEwan Crawford // Advance buffer past header 232726e52a70SEwan Crawford file_buffer = static_cast<uint8_t *>(file_buffer) + file_header->hdr_size; 232826e52a70SEwan Crawford 232955232f09SEwan Crawford // Calculate size of allocation data in file 233026e52a70SEwan Crawford size_t length = data_sp->GetByteSize() - file_header->hdr_size; 233155232f09SEwan Crawford 233255232f09SEwan Crawford // Check if the target allocation and file both have the same total data size. 2333b3f7f69dSAidan Dodds const uint32_t alloc_size = *alloc->size.get(); 233455232f09SEwan Crawford if (alloc_size != length) 233555232f09SEwan Crawford { 2336b3f7f69dSAidan Dodds strm.Printf("Warning: Mismatched allocation sizes - file 0x%" PRIx64 " bytes, allocation 0x%" PRIx32 " bytes", 2337eba832beSJason Molenda (uint64_t)length, alloc_size); 233855232f09SEwan Crawford strm.EOL(); 233955232f09SEwan Crawford length = alloc_size < length ? alloc_size : length; // Set length to copy to minimum 234055232f09SEwan Crawford } 234155232f09SEwan Crawford 234255232f09SEwan Crawford // Copy file data from our buffer into the target allocation. 234355232f09SEwan Crawford lldb::addr_t alloc_data = *alloc->data_ptr.get(); 234455232f09SEwan Crawford Error error; 234555232f09SEwan Crawford size_t bytes_written = GetProcess()->WriteMemory(alloc_data, file_buffer, length, error); 234655232f09SEwan Crawford if (!error.Success() || bytes_written != length) 234755232f09SEwan Crawford { 234855232f09SEwan Crawford strm.Printf("Error: Couldn't write data to allocation %s", error.AsCString()); 234955232f09SEwan Crawford strm.EOL(); 235055232f09SEwan Crawford return false; 235155232f09SEwan Crawford } 235255232f09SEwan Crawford 2353b3f7f69dSAidan Dodds strm.Printf("Contents of file '%s' read into allocation %" PRIu32, filename, alloc->id); 235455232f09SEwan Crawford strm.EOL(); 235555232f09SEwan Crawford 235655232f09SEwan Crawford return true; 235755232f09SEwan Crawford } 235855232f09SEwan Crawford 235926e52a70SEwan Crawford // Function takes as parameters a byte buffer, which will eventually be written to file as the element header, 236026e52a70SEwan Crawford // an offset into that buffer, and an Element that will be saved into the buffer at the parametrised offset. 236126e52a70SEwan Crawford // Return value is the new offset after writing the element into the buffer. 2362b3f7f69dSAidan Dodds // Elements are saved to the file as the ElementHeader struct followed by offsets to the structs of all the element's 2363b3f7f69dSAidan Dodds // children. 236426e52a70SEwan Crawford size_t 2365b3f7f69dSAidan Dodds RenderScriptRuntime::PopulateElementHeaders(const std::shared_ptr<uint8_t> header_buffer, size_t offset, 2366b3f7f69dSAidan Dodds const Element &elem) 236726e52a70SEwan Crawford { 236826e52a70SEwan Crawford // File struct for an element header with all the relevant details copied from elem. 236926e52a70SEwan Crawford // We assume members are valid already. 237026e52a70SEwan Crawford AllocationDetails::ElementHeader elem_header; 237126e52a70SEwan Crawford elem_header.type = *elem.type.get(); 237226e52a70SEwan Crawford elem_header.kind = *elem.type_kind.get(); 237326e52a70SEwan Crawford elem_header.element_size = *elem.datum_size.get(); 237426e52a70SEwan Crawford elem_header.vector_size = *elem.type_vec_size.get(); 237526e52a70SEwan Crawford elem_header.array_size = elem.array_size.isValid() ? *elem.array_size.get() : 0; 237626e52a70SEwan Crawford const size_t elem_header_size = sizeof(AllocationDetails::ElementHeader); 237726e52a70SEwan Crawford 237826e52a70SEwan Crawford // Copy struct into buffer and advance offset 2379b3f7f69dSAidan Dodds // We assume that header_buffer has been checked for nullptr before this method is called 238026e52a70SEwan Crawford memcpy(header_buffer.get() + offset, &elem_header, elem_header_size); 238126e52a70SEwan Crawford offset += elem_header_size; 238226e52a70SEwan Crawford 238326e52a70SEwan Crawford // Starting offset of child ElementHeader struct 238426e52a70SEwan Crawford size_t child_offset = offset + ((elem.children.size() + 1) * sizeof(uint32_t)); 238526e52a70SEwan Crawford for (const RenderScriptRuntime::Element &child : elem.children) 238626e52a70SEwan Crawford { 238726e52a70SEwan Crawford // Recursively populate the buffer with the element header structs of children. 238826e52a70SEwan Crawford // Then save the offsets where they were set after the parent element header. 238926e52a70SEwan Crawford memcpy(header_buffer.get() + offset, &child_offset, sizeof(uint32_t)); 239026e52a70SEwan Crawford offset += sizeof(uint32_t); 239126e52a70SEwan Crawford 239226e52a70SEwan Crawford child_offset = PopulateElementHeaders(header_buffer, child_offset, child); 239326e52a70SEwan Crawford } 239426e52a70SEwan Crawford 239526e52a70SEwan Crawford // Zero indicates no more children 239626e52a70SEwan Crawford memset(header_buffer.get() + offset, 0, sizeof(uint32_t)); 239726e52a70SEwan Crawford 239826e52a70SEwan Crawford return child_offset; 239926e52a70SEwan Crawford } 240026e52a70SEwan Crawford 2401b3f7f69dSAidan Dodds // Given an Element object this function returns the total size needed in the file header to store the element's 2402b3f7f69dSAidan Dodds // details. 240326e52a70SEwan Crawford // Taking into account the size of the element header struct, plus the offsets to all the element's children. 240426e52a70SEwan Crawford // Function is recursive so that the size of all ancestors is taken into account. 240526e52a70SEwan Crawford size_t 240626e52a70SEwan Crawford RenderScriptRuntime::CalculateElementHeaderSize(const Element &elem) 240726e52a70SEwan Crawford { 240826e52a70SEwan Crawford size_t size = (elem.children.size() + 1) * sizeof(uint32_t); // Offsets to children plus zero terminator 240926e52a70SEwan Crawford size += sizeof(AllocationDetails::ElementHeader); // Size of header struct with type details 241026e52a70SEwan Crawford 241126e52a70SEwan Crawford // Calculate recursively for all descendants 241226e52a70SEwan Crawford for (const Element &child : elem.children) 241326e52a70SEwan Crawford size += CalculateElementHeaderSize(child); 241426e52a70SEwan Crawford 241526e52a70SEwan Crawford return size; 241626e52a70SEwan Crawford } 241726e52a70SEwan Crawford 241855232f09SEwan Crawford // Function copies allocation contents into a binary file. 241955232f09SEwan Crawford // This file can then be loaded later into a different allocation. 242055232f09SEwan Crawford // There is a header, FileHeader, before the allocation data containing meta-data. 242155232f09SEwan Crawford bool 242255232f09SEwan Crawford RenderScriptRuntime::SaveAllocation(Stream &strm, const uint32_t alloc_id, const char *filename, StackFrame *frame_ptr) 242355232f09SEwan Crawford { 242455232f09SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 242555232f09SEwan Crawford 242655232f09SEwan Crawford // Find allocation with the given id 242755232f09SEwan Crawford AllocationDetails *alloc = FindAllocByID(strm, alloc_id); 242855232f09SEwan Crawford if (!alloc) 242955232f09SEwan Crawford return false; 243055232f09SEwan Crawford 243155232f09SEwan Crawford if (log) 2432b3f7f69dSAidan Dodds log->Printf("%s - found allocation 0x%" PRIx64 ".", __FUNCTION__, *alloc->address.get()); 243355232f09SEwan Crawford 243455232f09SEwan Crawford // JIT all the allocation details 24358b59062aSEwan Crawford if (alloc->shouldRefresh()) 243655232f09SEwan Crawford { 243755232f09SEwan Crawford if (log) 2438b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info.", __FUNCTION__); 243955232f09SEwan Crawford 244055232f09SEwan Crawford if (!RefreshAllocation(alloc, frame_ptr)) 244155232f09SEwan Crawford { 244255232f09SEwan Crawford if (log) 2443b3f7f69dSAidan Dodds log->Printf("%s - couldn't JIT allocation details.", __FUNCTION__); 24444cfc9198SSylvestre Ledru return false; 244555232f09SEwan Crawford } 244655232f09SEwan Crawford } 244755232f09SEwan Crawford 2448b3f7f69dSAidan Dodds assert(alloc->data_ptr.isValid() && alloc->element.type.isValid() && alloc->element.type_vec_size.isValid() && 2449b3f7f69dSAidan Dodds alloc->element.datum_size.get() && alloc->element.type_kind.isValid() && alloc->dimension.isValid() && 2450b3f7f69dSAidan Dodds "Allocation information not available"); 245155232f09SEwan Crawford 245255232f09SEwan Crawford // Check we can create writable file 245355232f09SEwan Crawford FileSpec file_spec(filename, true); 245455232f09SEwan Crawford File file(file_spec, File::eOpenOptionWrite | File::eOpenOptionCanCreate | File::eOpenOptionTruncate); 245555232f09SEwan Crawford if (!file) 245655232f09SEwan Crawford { 245755232f09SEwan Crawford strm.Printf("Error: Failed to open '%s' for writing", filename); 245855232f09SEwan Crawford strm.EOL(); 245955232f09SEwan Crawford return false; 246055232f09SEwan Crawford } 246155232f09SEwan Crawford 246255232f09SEwan Crawford // Read allocation into buffer of heap memory 246355232f09SEwan Crawford const std::shared_ptr<uint8_t> buffer = GetAllocationData(alloc, frame_ptr); 246455232f09SEwan Crawford if (!buffer) 246555232f09SEwan Crawford { 246655232f09SEwan Crawford strm.Printf("Error: Couldn't read allocation data into buffer"); 246755232f09SEwan Crawford strm.EOL(); 246855232f09SEwan Crawford return false; 246955232f09SEwan Crawford } 247055232f09SEwan Crawford 247155232f09SEwan Crawford // Create the file header 247255232f09SEwan Crawford AllocationDetails::FileHeader head; 2473b3f7f69dSAidan Dodds memcpy(head.ident, "RSAD", 4); 24742d62328aSEwan Crawford head.dims[0] = static_cast<uint32_t>(alloc->dimension.get()->dim_1); 24752d62328aSEwan Crawford head.dims[1] = static_cast<uint32_t>(alloc->dimension.get()->dim_2); 24762d62328aSEwan Crawford head.dims[2] = static_cast<uint32_t>(alloc->dimension.get()->dim_3); 247726e52a70SEwan Crawford 247826e52a70SEwan Crawford const size_t element_header_size = CalculateElementHeaderSize(alloc->element); 247926e52a70SEwan Crawford assert((sizeof(AllocationDetails::FileHeader) + element_header_size) < UINT16_MAX && "Element header too large"); 248026e52a70SEwan Crawford head.hdr_size = static_cast<uint16_t>(sizeof(AllocationDetails::FileHeader) + element_header_size); 248155232f09SEwan Crawford 248255232f09SEwan Crawford // Write the file header 248355232f09SEwan Crawford size_t num_bytes = sizeof(AllocationDetails::FileHeader); 248426e52a70SEwan Crawford if (log) 2485b3f7f69dSAidan Dodds log->Printf("%s - writing File Header, 0x%" PRIx64 " bytes", __FUNCTION__, num_bytes); 248626e52a70SEwan Crawford 248726e52a70SEwan Crawford Error err = file.Write(&head, num_bytes); 248826e52a70SEwan Crawford if (!err.Success()) 248926e52a70SEwan Crawford { 249026e52a70SEwan Crawford strm.Printf("Error: '%s' when writing to file '%s'", err.AsCString(), filename); 249126e52a70SEwan Crawford strm.EOL(); 249226e52a70SEwan Crawford return false; 249326e52a70SEwan Crawford } 249426e52a70SEwan Crawford 249526e52a70SEwan Crawford // Create the headers describing the element type of the allocation. 249626e52a70SEwan Crawford std::shared_ptr<uint8_t> element_header_buffer(new uint8_t[element_header_size]); 249726e52a70SEwan Crawford if (element_header_buffer == nullptr) 249826e52a70SEwan Crawford { 2499b3f7f69dSAidan Dodds strm.Printf("Internal Error: Couldn't allocate %" PRIu64 " bytes on the heap", element_header_size); 250026e52a70SEwan Crawford strm.EOL(); 250126e52a70SEwan Crawford return false; 250226e52a70SEwan Crawford } 250326e52a70SEwan Crawford 250426e52a70SEwan Crawford PopulateElementHeaders(element_header_buffer, 0, alloc->element); 250526e52a70SEwan Crawford 250626e52a70SEwan Crawford // Write headers for allocation element type to file 250726e52a70SEwan Crawford num_bytes = element_header_size; 250826e52a70SEwan Crawford if (log) 2509b3f7f69dSAidan Dodds log->Printf("%s - writing element headers, 0x%" PRIx64 " bytes.", __FUNCTION__, num_bytes); 251026e52a70SEwan Crawford 251126e52a70SEwan Crawford err = file.Write(element_header_buffer.get(), num_bytes); 251255232f09SEwan Crawford if (!err.Success()) 251355232f09SEwan Crawford { 251455232f09SEwan Crawford strm.Printf("Error: '%s' when writing to file '%s'", err.AsCString(), filename); 251555232f09SEwan Crawford strm.EOL(); 251655232f09SEwan Crawford return false; 251755232f09SEwan Crawford } 251855232f09SEwan Crawford 251955232f09SEwan Crawford // Write allocation data to file 252055232f09SEwan Crawford num_bytes = static_cast<size_t>(*alloc->size.get()); 252155232f09SEwan Crawford if (log) 2522b3f7f69dSAidan Dodds log->Printf("%s - writing 0x%" PRIx64 " bytes", __FUNCTION__, num_bytes); 252355232f09SEwan Crawford 252455232f09SEwan Crawford err = file.Write(buffer.get(), num_bytes); 252555232f09SEwan Crawford if (!err.Success()) 252655232f09SEwan Crawford { 252755232f09SEwan Crawford strm.Printf("Error: '%s' when writing to file '%s'", err.AsCString(), filename); 252855232f09SEwan Crawford strm.EOL(); 252955232f09SEwan Crawford return false; 253055232f09SEwan Crawford } 253155232f09SEwan Crawford 253255232f09SEwan Crawford strm.Printf("Allocation written to file '%s'", filename); 253355232f09SEwan Crawford strm.EOL(); 253415f2bd95SEwan Crawford return true; 253515f2bd95SEwan Crawford } 253615f2bd95SEwan Crawford 25375ec532a9SColin Riley bool 25385ec532a9SColin Riley RenderScriptRuntime::LoadModule(const lldb::ModuleSP &module_sp) 25395ec532a9SColin Riley { 25404640cde1SColin Riley Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 25414640cde1SColin Riley 25425ec532a9SColin Riley if (module_sp) 25435ec532a9SColin Riley { 25445ec532a9SColin Riley for (const auto &rs_module : m_rsmodules) 25455ec532a9SColin Riley { 25464640cde1SColin Riley if (rs_module->m_module == module_sp) 25477dc7771cSEwan Crawford { 25487dc7771cSEwan Crawford // Check if the user has enabled automatically breaking on 25497dc7771cSEwan Crawford // all RS kernels. 25507dc7771cSEwan Crawford if (m_breakAllKernels) 25517dc7771cSEwan Crawford BreakOnModuleKernels(rs_module); 25527dc7771cSEwan Crawford 25535ec532a9SColin Riley return false; 25545ec532a9SColin Riley } 25557dc7771cSEwan Crawford } 2556ef20b08fSColin Riley bool module_loaded = false; 2557ef20b08fSColin Riley switch (GetModuleKind(module_sp)) 2558ef20b08fSColin Riley { 2559ef20b08fSColin Riley case eModuleKindKernelObj: 2560ef20b08fSColin Riley { 25614640cde1SColin Riley RSModuleDescriptorSP module_desc; 25624640cde1SColin Riley module_desc.reset(new RSModuleDescriptor(module_sp)); 25634640cde1SColin Riley if (module_desc->ParseRSInfo()) 25645ec532a9SColin Riley { 25655ec532a9SColin Riley m_rsmodules.push_back(module_desc); 2566ef20b08fSColin Riley module_loaded = true; 25675ec532a9SColin Riley } 25684640cde1SColin Riley if (module_loaded) 25694640cde1SColin Riley { 25704640cde1SColin Riley FixupScriptDetails(module_desc); 25714640cde1SColin Riley } 2572ef20b08fSColin Riley break; 2573ef20b08fSColin Riley } 2574ef20b08fSColin Riley case eModuleKindDriver: 25754640cde1SColin Riley { 25764640cde1SColin Riley if (!m_libRSDriver) 25774640cde1SColin Riley { 25784640cde1SColin Riley m_libRSDriver = module_sp; 25794640cde1SColin Riley LoadRuntimeHooks(m_libRSDriver, RenderScriptRuntime::eModuleKindDriver); 25804640cde1SColin Riley } 25814640cde1SColin Riley break; 25824640cde1SColin Riley } 2583ef20b08fSColin Riley case eModuleKindImpl: 25844640cde1SColin Riley { 25854640cde1SColin Riley m_libRSCpuRef = module_sp; 25864640cde1SColin Riley break; 25874640cde1SColin Riley } 2588ef20b08fSColin Riley case eModuleKindLibRS: 25894640cde1SColin Riley { 25904640cde1SColin Riley if (!m_libRS) 25914640cde1SColin Riley { 25924640cde1SColin Riley m_libRS = module_sp; 25934640cde1SColin Riley static ConstString gDbgPresentStr("gDebuggerPresent"); 2594b3f7f69dSAidan Dodds const Symbol *debug_present = 2595b3f7f69dSAidan Dodds m_libRS->FindFirstSymbolWithNameAndType(gDbgPresentStr, eSymbolTypeData); 25964640cde1SColin Riley if (debug_present) 25974640cde1SColin Riley { 25984640cde1SColin Riley Error error; 25994640cde1SColin Riley uint32_t flag = 0x00000001U; 26004640cde1SColin Riley Target &target = GetProcess()->GetTarget(); 2601358cf1eaSGreg Clayton addr_t addr = debug_present->GetLoadAddress(&target); 26024640cde1SColin Riley GetProcess()->WriteMemory(addr, &flag, sizeof(flag), error); 26034640cde1SColin Riley if (error.Success()) 26044640cde1SColin Riley { 26054640cde1SColin Riley if (log) 2606b3f7f69dSAidan Dodds log->Printf("%s - debugger present flag set on debugee.", __FUNCTION__); 26074640cde1SColin Riley 26084640cde1SColin Riley m_debuggerPresentFlagged = true; 26094640cde1SColin Riley } 26104640cde1SColin Riley else if (log) 26114640cde1SColin Riley { 2612b3f7f69dSAidan Dodds log->Printf("%s - error writing debugger present flags '%s' ", __FUNCTION__, 2613b3f7f69dSAidan Dodds error.AsCString()); 26144640cde1SColin Riley } 26154640cde1SColin Riley } 26164640cde1SColin Riley else if (log) 26174640cde1SColin Riley { 2618b3f7f69dSAidan Dodds log->Printf("%s - error writing debugger present flags - symbol not found", __FUNCTION__); 26194640cde1SColin Riley } 26204640cde1SColin Riley } 26214640cde1SColin Riley break; 26224640cde1SColin Riley } 2623ef20b08fSColin Riley default: 2624ef20b08fSColin Riley break; 2625ef20b08fSColin Riley } 2626ef20b08fSColin Riley if (module_loaded) 2627ef20b08fSColin Riley Update(); 2628ef20b08fSColin Riley return module_loaded; 26295ec532a9SColin Riley } 26305ec532a9SColin Riley return false; 26315ec532a9SColin Riley } 26325ec532a9SColin Riley 2633ef20b08fSColin Riley void 2634ef20b08fSColin Riley RenderScriptRuntime::Update() 2635ef20b08fSColin Riley { 2636ef20b08fSColin Riley if (m_rsmodules.size() > 0) 2637ef20b08fSColin Riley { 2638ef20b08fSColin Riley if (!m_initiated) 2639ef20b08fSColin Riley { 2640ef20b08fSColin Riley Initiate(); 2641ef20b08fSColin Riley } 2642ef20b08fSColin Riley } 2643ef20b08fSColin Riley } 2644ef20b08fSColin Riley 26455ec532a9SColin Riley // The maximum line length of an .rs.info packet 26465ec532a9SColin Riley #define MAXLINE 500 26475ec532a9SColin Riley 26485ec532a9SColin Riley // The .rs.info symbol in renderscript modules contains a string which needs to be parsed. 26495ec532a9SColin Riley // The string is basic and is parsed on a line by line basis. 26505ec532a9SColin Riley bool 26515ec532a9SColin Riley RSModuleDescriptor::ParseRSInfo() 26525ec532a9SColin Riley { 26535ec532a9SColin Riley const Symbol *info_sym = m_module->FindFirstSymbolWithNameAndType(ConstString(".rs.info"), eSymbolTypeData); 26545ec532a9SColin Riley if (info_sym) 26555ec532a9SColin Riley { 2656358cf1eaSGreg Clayton const addr_t addr = info_sym->GetAddressRef().GetFileAddress(); 26575ec532a9SColin Riley const addr_t size = info_sym->GetByteSize(); 26585ec532a9SColin Riley const FileSpec fs = m_module->GetFileSpec(); 26595ec532a9SColin Riley 26605ec532a9SColin Riley DataBufferSP buffer = fs.ReadFileContents(addr, size); 26615ec532a9SColin Riley 26625ec532a9SColin Riley if (!buffer) 26635ec532a9SColin Riley return false; 26645ec532a9SColin Riley 26655ec532a9SColin Riley std::string info((const char *)buffer->GetBytes()); 26665ec532a9SColin Riley 26675ec532a9SColin Riley std::vector<std::string> info_lines; 2668e8433cc1SBruce Mitchener size_t lpos = info.find('\n'); 26695ec532a9SColin Riley while (lpos != std::string::npos) 26705ec532a9SColin Riley { 26715ec532a9SColin Riley info_lines.push_back(info.substr(0, lpos)); 26725ec532a9SColin Riley info = info.substr(lpos + 1); 2673e8433cc1SBruce Mitchener lpos = info.find('\n'); 26745ec532a9SColin Riley } 26755ec532a9SColin Riley size_t offset = 0; 26765ec532a9SColin Riley while (offset < info_lines.size()) 26775ec532a9SColin Riley { 26785ec532a9SColin Riley std::string line = info_lines[offset]; 26795ec532a9SColin Riley // Parse directives 26805ec532a9SColin Riley uint32_t numDefns = 0; 2681b3f7f69dSAidan Dodds if (sscanf(line.c_str(), "exportVarCount: %" PRIu32 "", &numDefns) == 1) 26825ec532a9SColin Riley { 26835ec532a9SColin Riley while (numDefns--) 26844640cde1SColin Riley m_globals.push_back(RSGlobalDescriptor(this, info_lines[++offset].c_str())); 26855ec532a9SColin Riley } 2686b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "exportFuncCount: %" PRIu32 "", &numDefns) == 1) 26875ec532a9SColin Riley { 26885ec532a9SColin Riley } 2689b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "exportForEachCount: %" PRIu32 "", &numDefns) == 1) 26905ec532a9SColin Riley { 26915ec532a9SColin Riley char name[MAXLINE]; 26925ec532a9SColin Riley while (numDefns--) 26935ec532a9SColin Riley { 26945ec532a9SColin Riley uint32_t slot = 0; 26955ec532a9SColin Riley name[0] = '\0'; 2696b3f7f69dSAidan Dodds if (sscanf(info_lines[++offset].c_str(), "%" PRIu32 " - %s", &slot, &name[0]) == 2) 26975ec532a9SColin Riley { 26984640cde1SColin Riley m_kernels.push_back(RSKernelDescriptor(this, name, slot)); 26994640cde1SColin Riley } 27004640cde1SColin Riley } 27014640cde1SColin Riley } 2702b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "pragmaCount: %" PRIu32 "", &numDefns) == 1) 27034640cde1SColin Riley { 27044640cde1SColin Riley char name[MAXLINE]; 27054640cde1SColin Riley char value[MAXLINE]; 27064640cde1SColin Riley while (numDefns--) 27074640cde1SColin Riley { 27084640cde1SColin Riley name[0] = '\0'; 27094640cde1SColin Riley value[0] = '\0'; 2710b3f7f69dSAidan Dodds if (sscanf(info_lines[++offset].c_str(), "%s - %s", &name[0], &value[0]) != 0 && (name[0] != '\0')) 27114640cde1SColin Riley { 27124640cde1SColin Riley m_pragmas[std::string(name)] = value; 27135ec532a9SColin Riley } 27145ec532a9SColin Riley } 27155ec532a9SColin Riley } 2716b3f7f69dSAidan Dodds else if (sscanf(line.c_str(), "objectSlotCount: %" PRIu32 "", &numDefns) == 1) 27175ec532a9SColin Riley { 27185ec532a9SColin Riley } 27195ec532a9SColin Riley 27205ec532a9SColin Riley offset++; 27215ec532a9SColin Riley } 27225ec532a9SColin Riley return m_kernels.size() > 0; 27235ec532a9SColin Riley } 27245ec532a9SColin Riley return false; 27255ec532a9SColin Riley } 27265ec532a9SColin Riley 27275ec532a9SColin Riley void 27284640cde1SColin Riley RenderScriptRuntime::Status(Stream &strm) const 27294640cde1SColin Riley { 27304640cde1SColin Riley if (m_libRS) 27314640cde1SColin Riley { 27324640cde1SColin Riley strm.Printf("Runtime Library discovered."); 27334640cde1SColin Riley strm.EOL(); 27344640cde1SColin Riley } 27354640cde1SColin Riley if (m_libRSDriver) 27364640cde1SColin Riley { 27374640cde1SColin Riley strm.Printf("Runtime Driver discovered."); 27384640cde1SColin Riley strm.EOL(); 27394640cde1SColin Riley } 27404640cde1SColin Riley if (m_libRSCpuRef) 27414640cde1SColin Riley { 27424640cde1SColin Riley strm.Printf("CPU Reference Implementation discovered."); 27434640cde1SColin Riley strm.EOL(); 27444640cde1SColin Riley } 27454640cde1SColin Riley 27464640cde1SColin Riley if (m_runtimeHooks.size()) 27474640cde1SColin Riley { 27484640cde1SColin Riley strm.Printf("Runtime functions hooked:"); 27494640cde1SColin Riley strm.EOL(); 27504640cde1SColin Riley for (auto b : m_runtimeHooks) 27514640cde1SColin Riley { 27524640cde1SColin Riley strm.Indent(b.second->defn->name); 27534640cde1SColin Riley strm.EOL(); 27544640cde1SColin Riley } 27554640cde1SColin Riley } 27564640cde1SColin Riley else 27574640cde1SColin Riley { 27584640cde1SColin Riley strm.Printf("Runtime is not hooked."); 27594640cde1SColin Riley strm.EOL(); 27604640cde1SColin Riley } 27614640cde1SColin Riley } 27624640cde1SColin Riley 27634640cde1SColin Riley void 27644640cde1SColin Riley RenderScriptRuntime::DumpContexts(Stream &strm) const 27654640cde1SColin Riley { 27664640cde1SColin Riley strm.Printf("Inferred RenderScript Contexts:"); 27674640cde1SColin Riley strm.EOL(); 27684640cde1SColin Riley strm.IndentMore(); 27694640cde1SColin Riley 27704640cde1SColin Riley std::map<addr_t, uint64_t> contextReferences; 27714640cde1SColin Riley 277278f339d1SEwan Crawford // Iterate over all of the currently discovered scripts. 277378f339d1SEwan Crawford // Note: We cant push or pop from m_scripts inside this loop or it may invalidate script. 27744640cde1SColin Riley for (const auto &script : m_scripts) 27754640cde1SColin Riley { 277678f339d1SEwan Crawford if (!script->context.isValid()) 277778f339d1SEwan Crawford continue; 277878f339d1SEwan Crawford lldb::addr_t context = *script->context; 277978f339d1SEwan Crawford 278078f339d1SEwan Crawford if (contextReferences.find(context) != contextReferences.end()) 27814640cde1SColin Riley { 278278f339d1SEwan Crawford contextReferences[context]++; 27834640cde1SColin Riley } 27844640cde1SColin Riley else 27854640cde1SColin Riley { 278678f339d1SEwan Crawford contextReferences[context] = 1; 27874640cde1SColin Riley } 27884640cde1SColin Riley } 27894640cde1SColin Riley 27904640cde1SColin Riley for (const auto &cRef : contextReferences) 27914640cde1SColin Riley { 27924640cde1SColin Riley strm.Printf("Context 0x%" PRIx64 ": %" PRIu64 " script instances", cRef.first, cRef.second); 27934640cde1SColin Riley strm.EOL(); 27944640cde1SColin Riley } 27954640cde1SColin Riley strm.IndentLess(); 27964640cde1SColin Riley } 27974640cde1SColin Riley 27984640cde1SColin Riley void 27994640cde1SColin Riley RenderScriptRuntime::DumpKernels(Stream &strm) const 28004640cde1SColin Riley { 28014640cde1SColin Riley strm.Printf("RenderScript Kernels:"); 28024640cde1SColin Riley strm.EOL(); 28034640cde1SColin Riley strm.IndentMore(); 28044640cde1SColin Riley for (const auto &module : m_rsmodules) 28054640cde1SColin Riley { 28064640cde1SColin Riley strm.Printf("Resource '%s':", module->m_resname.c_str()); 28074640cde1SColin Riley strm.EOL(); 28084640cde1SColin Riley for (const auto &kernel : module->m_kernels) 28094640cde1SColin Riley { 28104640cde1SColin Riley strm.Indent(kernel.m_name.AsCString()); 28114640cde1SColin Riley strm.EOL(); 28124640cde1SColin Riley } 28134640cde1SColin Riley } 28144640cde1SColin Riley strm.IndentLess(); 28154640cde1SColin Riley } 28164640cde1SColin Riley 2817a0f08674SEwan Crawford RenderScriptRuntime::AllocationDetails * 2818a0f08674SEwan Crawford RenderScriptRuntime::FindAllocByID(Stream &strm, const uint32_t alloc_id) 2819a0f08674SEwan Crawford { 2820a0f08674SEwan Crawford AllocationDetails *alloc = nullptr; 2821a0f08674SEwan Crawford 2822a0f08674SEwan Crawford // See if we can find allocation using id as an index; 2823b3f7f69dSAidan Dodds if (alloc_id <= m_allocations.size() && alloc_id != 0 && m_allocations[alloc_id - 1]->id == alloc_id) 2824a0f08674SEwan Crawford { 2825a0f08674SEwan Crawford alloc = m_allocations[alloc_id - 1].get(); 2826a0f08674SEwan Crawford return alloc; 2827a0f08674SEwan Crawford } 2828a0f08674SEwan Crawford 2829a0f08674SEwan Crawford // Fallback to searching 2830a0f08674SEwan Crawford for (const auto &a : m_allocations) 2831a0f08674SEwan Crawford { 2832a0f08674SEwan Crawford if (a->id == alloc_id) 2833a0f08674SEwan Crawford { 2834a0f08674SEwan Crawford alloc = a.get(); 2835a0f08674SEwan Crawford break; 2836a0f08674SEwan Crawford } 2837a0f08674SEwan Crawford } 2838a0f08674SEwan Crawford 2839a0f08674SEwan Crawford if (alloc == nullptr) 2840a0f08674SEwan Crawford { 2841b3f7f69dSAidan Dodds strm.Printf("Error: Couldn't find allocation with id matching %" PRIu32, alloc_id); 2842a0f08674SEwan Crawford strm.EOL(); 2843a0f08674SEwan Crawford } 2844a0f08674SEwan Crawford 2845a0f08674SEwan Crawford return alloc; 2846a0f08674SEwan Crawford } 2847a0f08674SEwan Crawford 2848a0f08674SEwan Crawford // Prints the contents of an allocation to the output stream, which may be a file 2849a0f08674SEwan Crawford bool 2850a0f08674SEwan Crawford RenderScriptRuntime::DumpAllocation(Stream &strm, StackFrame *frame_ptr, const uint32_t id) 2851a0f08674SEwan Crawford { 2852a0f08674SEwan Crawford Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 2853a0f08674SEwan Crawford 2854a0f08674SEwan Crawford // Check we can find the desired allocation 2855a0f08674SEwan Crawford AllocationDetails *alloc = FindAllocByID(strm, id); 2856a0f08674SEwan Crawford if (!alloc) 2857a0f08674SEwan Crawford return false; // FindAllocByID() will print error message for us here 2858a0f08674SEwan Crawford 2859a0f08674SEwan Crawford if (log) 2860b3f7f69dSAidan Dodds log->Printf("%s - found allocation 0x%" PRIx64, __FUNCTION__, *alloc->address.get()); 2861a0f08674SEwan Crawford 2862a0f08674SEwan Crawford // Check we have information about the allocation, if not calculate it 28638b59062aSEwan Crawford if (alloc->shouldRefresh()) 2864a0f08674SEwan Crawford { 2865a0f08674SEwan Crawford if (log) 2866b3f7f69dSAidan Dodds log->Printf("%s - allocation details not calculated yet, jitting info.", __FUNCTION__); 2867a0f08674SEwan Crawford 2868a0f08674SEwan Crawford // JIT all the allocation information 2869a0f08674SEwan Crawford if (!RefreshAllocation(alloc, frame_ptr)) 2870a0f08674SEwan Crawford { 2871a0f08674SEwan Crawford strm.Printf("Error: Couldn't JIT allocation details"); 2872a0f08674SEwan Crawford strm.EOL(); 2873a0f08674SEwan Crawford return false; 2874a0f08674SEwan Crawford } 2875a0f08674SEwan Crawford } 2876a0f08674SEwan Crawford 2877a0f08674SEwan Crawford // Establish format and size of each data element 2878b3f7f69dSAidan Dodds const uint32_t vec_size = *alloc->element.type_vec_size.get(); 28798b244e21SEwan Crawford const Element::DataType type = *alloc->element.type.get(); 2880a0f08674SEwan Crawford 2881b3f7f69dSAidan Dodds assert(type >= Element::RS_TYPE_NONE && type <= Element::RS_TYPE_FONT && "Invalid allocation type"); 2882a0f08674SEwan Crawford 28832e920715SEwan Crawford lldb::Format format; 28842e920715SEwan Crawford if (type >= Element::RS_TYPE_ELEMENT) 28852e920715SEwan Crawford format = eFormatHex; 28862e920715SEwan Crawford else 28872e920715SEwan Crawford format = vec_size == 1 ? static_cast<lldb::Format>(AllocationDetails::RSTypeToFormat[type][eFormatSingle]) 2888a0f08674SEwan Crawford : static_cast<lldb::Format>(AllocationDetails::RSTypeToFormat[type][eFormatVector]); 2889a0f08674SEwan Crawford 2890b3f7f69dSAidan Dodds const uint32_t data_size = *alloc->element.datum_size.get(); 2891a0f08674SEwan Crawford 2892a0f08674SEwan Crawford if (log) 2893b3f7f69dSAidan Dodds log->Printf("%s - element size %" PRIu32 " bytes, including padding", __FUNCTION__, data_size); 2894a0f08674SEwan Crawford 289555232f09SEwan Crawford // Allocate a buffer to copy data into 289655232f09SEwan Crawford std::shared_ptr<uint8_t> buffer = GetAllocationData(alloc, frame_ptr); 289755232f09SEwan Crawford if (!buffer) 289855232f09SEwan Crawford { 28992e920715SEwan Crawford strm.Printf("Error: Couldn't read allocation data"); 290055232f09SEwan Crawford strm.EOL(); 290155232f09SEwan Crawford return false; 290255232f09SEwan Crawford } 290355232f09SEwan Crawford 2904a0f08674SEwan Crawford // Calculate stride between rows as there may be padding at end of rows since 2905a0f08674SEwan Crawford // allocated memory is 16-byte aligned 2906a0f08674SEwan Crawford if (!alloc->stride.isValid()) 2907a0f08674SEwan Crawford { 2908a0f08674SEwan Crawford if (alloc->dimension.get()->dim_2 == 0) // We only have one dimension 2909a0f08674SEwan Crawford alloc->stride = 0; 2910a0f08674SEwan Crawford else if (!JITAllocationStride(alloc, frame_ptr)) 2911a0f08674SEwan Crawford { 2912a0f08674SEwan Crawford strm.Printf("Error: Couldn't calculate allocation row stride"); 2913a0f08674SEwan Crawford strm.EOL(); 2914a0f08674SEwan Crawford return false; 2915a0f08674SEwan Crawford } 2916a0f08674SEwan Crawford } 2917b3f7f69dSAidan Dodds const uint32_t stride = *alloc->stride.get(); 2918b3f7f69dSAidan Dodds const uint32_t size = *alloc->size.get(); // Size of whole allocation 2919b3f7f69dSAidan Dodds const uint32_t padding = alloc->element.padding.isValid() ? *alloc->element.padding.get() : 0; 2920a0f08674SEwan Crawford if (log) 2921b3f7f69dSAidan Dodds log->Printf("%s - stride %" PRIu32 " bytes, size %" PRIu32 " bytes, padding %" PRIu32, 2922b3f7f69dSAidan Dodds __FUNCTION__, stride, size, padding); 2923a0f08674SEwan Crawford 2924a0f08674SEwan Crawford // Find dimensions used to index loops, so need to be non-zero 2925b3f7f69dSAidan Dodds uint32_t dim_x = alloc->dimension.get()->dim_1; 2926a0f08674SEwan Crawford dim_x = dim_x == 0 ? 1 : dim_x; 2927a0f08674SEwan Crawford 2928b3f7f69dSAidan Dodds uint32_t dim_y = alloc->dimension.get()->dim_2; 2929a0f08674SEwan Crawford dim_y = dim_y == 0 ? 1 : dim_y; 2930a0f08674SEwan Crawford 2931b3f7f69dSAidan Dodds uint32_t dim_z = alloc->dimension.get()->dim_3; 2932a0f08674SEwan Crawford dim_z = dim_z == 0 ? 1 : dim_z; 2933a0f08674SEwan Crawford 293455232f09SEwan Crawford // Use data extractor to format output 293555232f09SEwan Crawford const uint32_t archByteSize = GetProcess()->GetTarget().GetArchitecture().GetAddressByteSize(); 293655232f09SEwan Crawford DataExtractor alloc_data(buffer.get(), size, GetProcess()->GetByteOrder(), archByteSize); 293755232f09SEwan Crawford 2938b3f7f69dSAidan Dodds uint32_t offset = 0; // Offset in buffer to next element to be printed 2939b3f7f69dSAidan Dodds uint32_t prev_row = 0; // Offset to the start of the previous row 2940a0f08674SEwan Crawford 2941a0f08674SEwan Crawford // Iterate over allocation dimensions, printing results to user 2942a0f08674SEwan Crawford strm.Printf("Data (X, Y, Z):"); 2943b3f7f69dSAidan Dodds for (uint32_t z = 0; z < dim_z; ++z) 2944a0f08674SEwan Crawford { 2945b3f7f69dSAidan Dodds for (uint32_t y = 0; y < dim_y; ++y) 2946a0f08674SEwan Crawford { 2947a0f08674SEwan Crawford // Use stride to index start of next row. 2948a0f08674SEwan Crawford if (!(y == 0 && z == 0)) 2949a0f08674SEwan Crawford offset = prev_row + stride; 2950a0f08674SEwan Crawford prev_row = offset; 2951a0f08674SEwan Crawford 2952a0f08674SEwan Crawford // Print each element in the row individually 2953b3f7f69dSAidan Dodds for (uint32_t x = 0; x < dim_x; ++x) 2954a0f08674SEwan Crawford { 2955b3f7f69dSAidan Dodds strm.Printf("\n(%" PRIu32 ", %" PRIu32 ", %" PRIu32 ") = ", x, y, z); 29568b244e21SEwan Crawford if ((type == Element::RS_TYPE_NONE) && (alloc->element.children.size() > 0) && 2957fe06b5adSAdrian McCarthy (alloc->element.type_name != Element::GetFallbackStructName())) 29588b244e21SEwan Crawford { 29598b244e21SEwan Crawford // Here we are dumping an Element of struct type. 29608b244e21SEwan Crawford // This is done using expression evaluation with the name of the struct type and pointer to element. 29618b244e21SEwan Crawford 29628b244e21SEwan Crawford // Don't print the name of the resulting expression, since this will be '$[0-9]+' 29638b244e21SEwan Crawford DumpValueObjectOptions expr_options; 29648b244e21SEwan Crawford expr_options.SetHideName(true); 29658b244e21SEwan Crawford 29668b244e21SEwan Crawford // Setup expression as derefrencing a pointer cast to element address. 2967ea0636b5SEwan Crawford char expr_char_buffer[jit_max_expr_size]; 2968ea0636b5SEwan Crawford int chars_written = snprintf(expr_char_buffer, jit_max_expr_size, "*(%s*) 0x%" PRIx64, 29698b244e21SEwan Crawford alloc->element.type_name.AsCString(), *alloc->data_ptr.get() + offset); 29708b244e21SEwan Crawford 2971ea0636b5SEwan Crawford if (chars_written < 0 || chars_written >= jit_max_expr_size) 29728b244e21SEwan Crawford { 29738b244e21SEwan Crawford if (log) 2974b3f7f69dSAidan Dodds log->Printf("%s - error in snprintf().", __FUNCTION__); 29758b244e21SEwan Crawford continue; 29768b244e21SEwan Crawford } 29778b244e21SEwan Crawford 29788b244e21SEwan Crawford // Evaluate expression 29798b244e21SEwan Crawford ValueObjectSP expr_result; 29808b244e21SEwan Crawford GetProcess()->GetTarget().EvaluateExpression(expr_char_buffer, frame_ptr, expr_result); 29818b244e21SEwan Crawford 29828b244e21SEwan Crawford // Print the results to our stream. 29838b244e21SEwan Crawford expr_result->Dump(strm, expr_options); 29848b244e21SEwan Crawford } 29858b244e21SEwan Crawford else 29868b244e21SEwan Crawford { 29878b244e21SEwan Crawford alloc_data.Dump(&strm, offset, format, data_size - padding, 1, 1, LLDB_INVALID_ADDRESS, 0, 0); 29888b244e21SEwan Crawford } 29898b244e21SEwan Crawford offset += data_size; 2990a0f08674SEwan Crawford } 2991a0f08674SEwan Crawford } 2992a0f08674SEwan Crawford } 2993a0f08674SEwan Crawford strm.EOL(); 2994a0f08674SEwan Crawford 2995a0f08674SEwan Crawford return true; 2996a0f08674SEwan Crawford } 2997a0f08674SEwan Crawford 29980d2bfcfbSEwan Crawford // Function recalculates all our cached information about allocations by jitting the 29990d2bfcfbSEwan Crawford // RS runtime regarding each allocation we know about. 30000d2bfcfbSEwan Crawford // Returns true if all allocations could be recomputed, false otherwise. 30010d2bfcfbSEwan Crawford bool 30020d2bfcfbSEwan Crawford RenderScriptRuntime::RecomputeAllAllocations(Stream &strm, StackFrame *frame_ptr) 30030d2bfcfbSEwan Crawford { 30040d2bfcfbSEwan Crawford bool success = true; 30050d2bfcfbSEwan Crawford for (auto &alloc : m_allocations) 30060d2bfcfbSEwan Crawford { 30070d2bfcfbSEwan Crawford // JIT current allocation information 30080d2bfcfbSEwan Crawford if (!RefreshAllocation(alloc.get(), frame_ptr)) 30090d2bfcfbSEwan Crawford { 30100d2bfcfbSEwan Crawford strm.Printf("Error: Couldn't evaluate details for allocation %" PRIu32 "\n", alloc->id); 30110d2bfcfbSEwan Crawford success = false; 30120d2bfcfbSEwan Crawford } 30130d2bfcfbSEwan Crawford } 30140d2bfcfbSEwan Crawford 30150d2bfcfbSEwan Crawford if (success) 30160d2bfcfbSEwan Crawford strm.Printf("All allocations successfully recomputed"); 30170d2bfcfbSEwan Crawford strm.EOL(); 30180d2bfcfbSEwan Crawford 30190d2bfcfbSEwan Crawford return success; 30200d2bfcfbSEwan Crawford } 30210d2bfcfbSEwan Crawford 3022b649b005SEwan Crawford // Prints information regarding currently loaded allocations. 302315f2bd95SEwan Crawford // These details are gathered by jitting the runtime, which has as latency. 3024b649b005SEwan Crawford // Index parameter specifies a single allocation ID to print, or a zero value to print them all 302515f2bd95SEwan Crawford void 3026b649b005SEwan Crawford RenderScriptRuntime::ListAllocations(Stream &strm, StackFrame *frame_ptr, const uint32_t index) 302715f2bd95SEwan Crawford { 302815f2bd95SEwan Crawford strm.Printf("RenderScript Allocations:"); 302915f2bd95SEwan Crawford strm.EOL(); 303015f2bd95SEwan Crawford strm.IndentMore(); 303115f2bd95SEwan Crawford 303215f2bd95SEwan Crawford for (auto &alloc : m_allocations) 303315f2bd95SEwan Crawford { 3034b649b005SEwan Crawford // index will only be zero if we want to print all allocations 3035b649b005SEwan Crawford if (index != 0 && index != alloc->id) 3036b649b005SEwan Crawford continue; 303715f2bd95SEwan Crawford 303815f2bd95SEwan Crawford // JIT current allocation information 3039b649b005SEwan Crawford if (alloc->shouldRefresh() && !RefreshAllocation(alloc.get(), frame_ptr)) 304015f2bd95SEwan Crawford { 3041b3f7f69dSAidan Dodds strm.Printf("Error: Couldn't evaluate details for allocation %" PRIu32, alloc->id); 3042b3f7f69dSAidan Dodds strm.EOL(); 304315f2bd95SEwan Crawford continue; 304415f2bd95SEwan Crawford } 304515f2bd95SEwan Crawford 3046b3f7f69dSAidan Dodds strm.Printf("%" PRIu32 ":", alloc->id); 3047b3f7f69dSAidan Dodds strm.EOL(); 304815f2bd95SEwan Crawford strm.IndentMore(); 304915f2bd95SEwan Crawford 305015f2bd95SEwan Crawford strm.Indent("Context: "); 305115f2bd95SEwan Crawford if (!alloc->context.isValid()) 305215f2bd95SEwan Crawford strm.Printf("unknown\n"); 305315f2bd95SEwan Crawford else 305415f2bd95SEwan Crawford strm.Printf("0x%" PRIx64 "\n", *alloc->context.get()); 305515f2bd95SEwan Crawford 305615f2bd95SEwan Crawford strm.Indent("Address: "); 305715f2bd95SEwan Crawford if (!alloc->address.isValid()) 305815f2bd95SEwan Crawford strm.Printf("unknown\n"); 305915f2bd95SEwan Crawford else 306015f2bd95SEwan Crawford strm.Printf("0x%" PRIx64 "\n", *alloc->address.get()); 306115f2bd95SEwan Crawford 306215f2bd95SEwan Crawford strm.Indent("Data pointer: "); 306315f2bd95SEwan Crawford if (!alloc->data_ptr.isValid()) 306415f2bd95SEwan Crawford strm.Printf("unknown\n"); 306515f2bd95SEwan Crawford else 306615f2bd95SEwan Crawford strm.Printf("0x%" PRIx64 "\n", *alloc->data_ptr.get()); 306715f2bd95SEwan Crawford 306815f2bd95SEwan Crawford strm.Indent("Dimensions: "); 306915f2bd95SEwan Crawford if (!alloc->dimension.isValid()) 307015f2bd95SEwan Crawford strm.Printf("unknown\n"); 307115f2bd95SEwan Crawford else 3072b3f7f69dSAidan Dodds strm.Printf("(%" PRId32 ", %" PRId32 ", %" PRId32 ")\n", 3073b3f7f69dSAidan Dodds alloc->dimension.get()->dim_1, alloc->dimension.get()->dim_2, alloc->dimension.get()->dim_3); 307415f2bd95SEwan Crawford 307515f2bd95SEwan Crawford strm.Indent("Data Type: "); 30768b244e21SEwan Crawford if (!alloc->element.type.isValid() || !alloc->element.type_vec_size.isValid()) 307715f2bd95SEwan Crawford strm.Printf("unknown\n"); 307815f2bd95SEwan Crawford else 307915f2bd95SEwan Crawford { 30808b244e21SEwan Crawford const int vector_size = *alloc->element.type_vec_size.get(); 30812e920715SEwan Crawford Element::DataType type = *alloc->element.type.get(); 308215f2bd95SEwan Crawford 30838b244e21SEwan Crawford if (!alloc->element.type_name.IsEmpty()) 30848b244e21SEwan Crawford strm.Printf("%s\n", alloc->element.type_name.AsCString()); 30852e920715SEwan Crawford else 30862e920715SEwan Crawford { 30872e920715SEwan Crawford // Enum value isn't monotonous, so doesn't always index RsDataTypeToString array 30882e920715SEwan Crawford if (type >= Element::RS_TYPE_ELEMENT && type <= Element::RS_TYPE_FONT) 3089b3f7f69dSAidan Dodds type = static_cast<Element::DataType>((type - Element::RS_TYPE_ELEMENT) + 3090b3f7f69dSAidan Dodds Element::RS_TYPE_MATRIX_2X2 + 1); 30912e920715SEwan Crawford 3092b3f7f69dSAidan Dodds if (type >= (sizeof(AllocationDetails::RsDataTypeToString) / 3093b3f7f69dSAidan Dodds sizeof(AllocationDetails::RsDataTypeToString[0])) || 3094b3f7f69dSAidan Dodds vector_size > 4 || vector_size < 1) 309515f2bd95SEwan Crawford strm.Printf("invalid type\n"); 309615f2bd95SEwan Crawford else 3097b3f7f69dSAidan Dodds strm.Printf("%s\n", AllocationDetails::RsDataTypeToString[static_cast<uint32_t>(type)] 3098b3f7f69dSAidan Dodds [vector_size - 1]); 309915f2bd95SEwan Crawford } 31002e920715SEwan Crawford } 310115f2bd95SEwan Crawford 310215f2bd95SEwan Crawford strm.Indent("Data Kind: "); 31038b244e21SEwan Crawford if (!alloc->element.type_kind.isValid()) 310415f2bd95SEwan Crawford strm.Printf("unknown\n"); 310515f2bd95SEwan Crawford else 310615f2bd95SEwan Crawford { 31078b244e21SEwan Crawford const Element::DataKind kind = *alloc->element.type_kind.get(); 31088b244e21SEwan Crawford if (kind < Element::RS_KIND_USER || kind > Element::RS_KIND_PIXEL_YUV) 310915f2bd95SEwan Crawford strm.Printf("invalid kind\n"); 311015f2bd95SEwan Crawford else 3111b3f7f69dSAidan Dodds strm.Printf("%s\n", AllocationDetails::RsDataKindToString[static_cast<uint32_t>(kind)]); 311215f2bd95SEwan Crawford } 311315f2bd95SEwan Crawford 311415f2bd95SEwan Crawford strm.EOL(); 311515f2bd95SEwan Crawford strm.IndentLess(); 311615f2bd95SEwan Crawford } 311715f2bd95SEwan Crawford strm.IndentLess(); 311815f2bd95SEwan Crawford } 311915f2bd95SEwan Crawford 31207dc7771cSEwan Crawford // Set breakpoints on every kernel found in RS module 31217dc7771cSEwan Crawford void 31227dc7771cSEwan Crawford RenderScriptRuntime::BreakOnModuleKernels(const RSModuleDescriptorSP rsmodule_sp) 31237dc7771cSEwan Crawford { 31247dc7771cSEwan Crawford for (const auto &kernel : rsmodule_sp->m_kernels) 31257dc7771cSEwan Crawford { 31267dc7771cSEwan Crawford // Don't set breakpoint on 'root' kernel 31277dc7771cSEwan Crawford if (strcmp(kernel.m_name.AsCString(), "root") == 0) 31287dc7771cSEwan Crawford continue; 31297dc7771cSEwan Crawford 31307dc7771cSEwan Crawford CreateKernelBreakpoint(kernel.m_name); 31317dc7771cSEwan Crawford } 31327dc7771cSEwan Crawford } 31337dc7771cSEwan Crawford 31347dc7771cSEwan Crawford // Method is internally called by the 'kernel breakpoint all' command to 31357dc7771cSEwan Crawford // enable or disable breaking on all kernels. 31367dc7771cSEwan Crawford // 31377dc7771cSEwan Crawford // When do_break is true we want to enable this functionality. 31387dc7771cSEwan Crawford // When do_break is false we want to disable it. 31397dc7771cSEwan Crawford void 31407dc7771cSEwan Crawford RenderScriptRuntime::SetBreakAllKernels(bool do_break, TargetSP target) 31417dc7771cSEwan Crawford { 314254782db7SEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS)); 31437dc7771cSEwan Crawford 31447dc7771cSEwan Crawford InitSearchFilter(target); 31457dc7771cSEwan Crawford 31467dc7771cSEwan Crawford // Set breakpoints on all the kernels 31477dc7771cSEwan Crawford if (do_break && !m_breakAllKernels) 31487dc7771cSEwan Crawford { 31497dc7771cSEwan Crawford m_breakAllKernels = true; 31507dc7771cSEwan Crawford 31517dc7771cSEwan Crawford for (const auto &module : m_rsmodules) 31527dc7771cSEwan Crawford BreakOnModuleKernels(module); 31537dc7771cSEwan Crawford 31547dc7771cSEwan Crawford if (log) 3155b3f7f69dSAidan Dodds log->Printf("%s(True) - breakpoints set on all currently loaded kernels.", __FUNCTION__); 31567dc7771cSEwan Crawford } 31577dc7771cSEwan Crawford else if (!do_break && m_breakAllKernels) // Breakpoints won't be set on any new kernels. 31587dc7771cSEwan Crawford { 31597dc7771cSEwan Crawford m_breakAllKernels = false; 31607dc7771cSEwan Crawford 31617dc7771cSEwan Crawford if (log) 3162b3f7f69dSAidan Dodds log->Printf("%s(False) - breakpoints no longer automatically set.", __FUNCTION__); 31637dc7771cSEwan Crawford } 31647dc7771cSEwan Crawford } 31657dc7771cSEwan Crawford 31667dc7771cSEwan Crawford // Given the name of a kernel this function creates a breakpoint using our 31677dc7771cSEwan Crawford // own breakpoint resolver, and returns the Breakpoint shared pointer. 31687dc7771cSEwan Crawford BreakpointSP 31697dc7771cSEwan Crawford RenderScriptRuntime::CreateKernelBreakpoint(const ConstString &name) 31707dc7771cSEwan Crawford { 317154782db7SEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS)); 31727dc7771cSEwan Crawford 31737dc7771cSEwan Crawford if (!m_filtersp) 31747dc7771cSEwan Crawford { 31757dc7771cSEwan Crawford if (log) 3176b3f7f69dSAidan Dodds log->Printf("%s - error, no breakpoint search filter set.", __FUNCTION__); 31777dc7771cSEwan Crawford return nullptr; 31787dc7771cSEwan Crawford } 31797dc7771cSEwan Crawford 31807dc7771cSEwan Crawford BreakpointResolverSP resolver_sp(new RSBreakpointResolver(nullptr, name)); 31817dc7771cSEwan Crawford BreakpointSP bp = GetProcess()->GetTarget().CreateBreakpoint(m_filtersp, resolver_sp, false, false, false); 31827dc7771cSEwan Crawford 318354782db7SEwan Crawford // Give RS breakpoints a specific name, so the user can manipulate them as a group. 318454782db7SEwan Crawford Error err; 318554782db7SEwan Crawford if (!bp->AddName("RenderScriptKernel", err) && log) 3186b3f7f69dSAidan Dodds log->Printf("%s - error setting break name, '%s'.", __FUNCTION__, err.AsCString()); 318754782db7SEwan Crawford 31887dc7771cSEwan Crawford return bp; 31897dc7771cSEwan Crawford } 31907dc7771cSEwan Crawford 3191018f5a7eSEwan Crawford // Given an expression for a variable this function tries to calculate the variable's value. 3192018f5a7eSEwan Crawford // If this is possible it returns true and sets the uint64_t parameter to the variables unsigned value. 3193018f5a7eSEwan Crawford // Otherwise function returns false. 3194018f5a7eSEwan Crawford bool 3195018f5a7eSEwan Crawford RenderScriptRuntime::GetFrameVarAsUnsigned(const StackFrameSP frame_sp, const char *var_name, uint64_t &val) 3196018f5a7eSEwan Crawford { 3197018f5a7eSEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 3198018f5a7eSEwan Crawford Error error; 3199018f5a7eSEwan Crawford VariableSP var_sp; 3200018f5a7eSEwan Crawford 3201018f5a7eSEwan Crawford // Find variable in stack frame 3202b3f7f69dSAidan Dodds ValueObjectSP value_sp(frame_sp->GetValueForVariableExpressionPath( 3203b3f7f69dSAidan Dodds var_name, eNoDynamicValues, 3204b3f7f69dSAidan Dodds StackFrame::eExpressionPathOptionCheckPtrVsMember | StackFrame::eExpressionPathOptionsAllowDirectIVarAccess, 3205b3f7f69dSAidan Dodds var_sp, error)); 3206018f5a7eSEwan Crawford if (!error.Success()) 3207018f5a7eSEwan Crawford { 3208018f5a7eSEwan Crawford if (log) 3209b3f7f69dSAidan Dodds log->Printf("%s - error, couldn't find '%s' in frame", __FUNCTION__, var_name); 3210018f5a7eSEwan Crawford return false; 3211018f5a7eSEwan Crawford } 3212018f5a7eSEwan Crawford 3213b3f7f69dSAidan Dodds // Find the uint32_t value for the variable 3214018f5a7eSEwan Crawford bool success = false; 3215018f5a7eSEwan Crawford val = value_sp->GetValueAsUnsigned(0, &success); 3216018f5a7eSEwan Crawford if (!success) 3217018f5a7eSEwan Crawford { 3218018f5a7eSEwan Crawford if (log) 3219b3f7f69dSAidan Dodds log->Printf("%s - error, couldn't parse '%s' as an uint32_t.", __FUNCTION__, var_name); 3220018f5a7eSEwan Crawford return false; 3221018f5a7eSEwan Crawford } 3222018f5a7eSEwan Crawford 3223018f5a7eSEwan Crawford return true; 3224018f5a7eSEwan Crawford } 3225018f5a7eSEwan Crawford 32264f8817c2SEwan Crawford // Function attempts to find the current coordinate of a kernel invocation by investigating the 32274f8817c2SEwan Crawford // values of frame variables in the .expand function. These coordinates are returned via the coord 32284f8817c2SEwan Crawford // array reference parameter. Returns true if the coordinates could be found, and false otherwise. 32294f8817c2SEwan Crawford bool 32304f8817c2SEwan Crawford RenderScriptRuntime::GetKernelCoordinate(RSCoordinate &coord, Thread *thread_ptr) 32314f8817c2SEwan Crawford { 32324f8817c2SEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE)); 32334f8817c2SEwan Crawford 32344f8817c2SEwan Crawford if (!thread_ptr) 32354f8817c2SEwan Crawford { 32364f8817c2SEwan Crawford if (log) 32374f8817c2SEwan Crawford log->Printf("%s - Error, No thread pointer", __FUNCTION__); 32384f8817c2SEwan Crawford 32394f8817c2SEwan Crawford return false; 32404f8817c2SEwan Crawford } 32414f8817c2SEwan Crawford 32424f8817c2SEwan Crawford // Walk the call stack looking for a function whose name has the suffix '.expand' 32434f8817c2SEwan Crawford // and contains the variables we're looking for. 32444f8817c2SEwan Crawford for (uint32_t i = 0; i < thread_ptr->GetStackFrameCount(); ++i) 32454f8817c2SEwan Crawford { 32464f8817c2SEwan Crawford if (!thread_ptr->SetSelectedFrameByIndex(i)) 32474f8817c2SEwan Crawford continue; 32484f8817c2SEwan Crawford 32494f8817c2SEwan Crawford StackFrameSP frame_sp = thread_ptr->GetSelectedFrame(); 32504f8817c2SEwan Crawford if (!frame_sp) 32514f8817c2SEwan Crawford continue; 32524f8817c2SEwan Crawford 32534f8817c2SEwan Crawford // Find the function name 32544f8817c2SEwan Crawford const SymbolContext sym_ctx = frame_sp->GetSymbolContext(false); 32554f8817c2SEwan Crawford const char *func_name_cstr = sym_ctx.GetFunctionName().AsCString(); 32564f8817c2SEwan Crawford if (!func_name_cstr) 32574f8817c2SEwan Crawford continue; 32584f8817c2SEwan Crawford 32594f8817c2SEwan Crawford if (log) 32604f8817c2SEwan Crawford log->Printf("%s - Inspecting function '%s'", __FUNCTION__, func_name_cstr); 32614f8817c2SEwan Crawford 32624f8817c2SEwan Crawford // Check if function name has .expand suffix 32634f8817c2SEwan Crawford std::string func_name(func_name_cstr); 32644f8817c2SEwan Crawford const int length_difference = func_name.length() - RenderScriptRuntime::s_runtimeExpandSuffix.length(); 32654f8817c2SEwan Crawford if (length_difference <= 0) 32664f8817c2SEwan Crawford continue; 32674f8817c2SEwan Crawford 32684f8817c2SEwan Crawford const int32_t has_expand_suffix = func_name.compare(length_difference, 32694f8817c2SEwan Crawford RenderScriptRuntime::s_runtimeExpandSuffix.length(), 32704f8817c2SEwan Crawford RenderScriptRuntime::s_runtimeExpandSuffix); 32714f8817c2SEwan Crawford 32724f8817c2SEwan Crawford if (has_expand_suffix != 0) 32734f8817c2SEwan Crawford continue; 32744f8817c2SEwan Crawford 32754f8817c2SEwan Crawford if (log) 32764f8817c2SEwan Crawford log->Printf("%s - Found .expand function '%s'", __FUNCTION__, func_name_cstr); 32774f8817c2SEwan Crawford 32784f8817c2SEwan Crawford // Get values for variables in .expand frame that tell us the current kernel invocation 32794f8817c2SEwan Crawford bool found_coord_variables = true; 32804f8817c2SEwan Crawford assert(RenderScriptRuntime::s_runtimeCoordVars.size() == coord.size()); 32814f8817c2SEwan Crawford 32824f8817c2SEwan Crawford for (uint32_t i = 0; i < coord.size(); ++i) 32834f8817c2SEwan Crawford { 32844f8817c2SEwan Crawford uint64_t value = 0; 32854f8817c2SEwan Crawford if (!GetFrameVarAsUnsigned(frame_sp, RenderScriptRuntime::s_runtimeCoordVars[i], value)) 32864f8817c2SEwan Crawford { 32874f8817c2SEwan Crawford found_coord_variables = false; 32884f8817c2SEwan Crawford break; 32894f8817c2SEwan Crawford } 32904f8817c2SEwan Crawford coord[i] = value; 32914f8817c2SEwan Crawford } 32924f8817c2SEwan Crawford 32934f8817c2SEwan Crawford if (found_coord_variables) 32944f8817c2SEwan Crawford return true; 32954f8817c2SEwan Crawford } 32964f8817c2SEwan Crawford return false; 32974f8817c2SEwan Crawford } 32984f8817c2SEwan Crawford 3299018f5a7eSEwan Crawford // Callback when a kernel breakpoint hits and we're looking for a specific coordinate. 3300018f5a7eSEwan Crawford // Baton parameter contains a pointer to the target coordinate we want to break on. 3301018f5a7eSEwan Crawford // Function then checks the .expand frame for the current coordinate and breaks to user if it matches. 3302018f5a7eSEwan Crawford // Parameter 'break_id' is the id of the Breakpoint which made the callback. 3303018f5a7eSEwan Crawford // Parameter 'break_loc_id' is the id for the BreakpointLocation which was hit, 3304018f5a7eSEwan Crawford // a single logical breakpoint can have multiple addresses. 3305018f5a7eSEwan Crawford bool 3306b3f7f69dSAidan Dodds RenderScriptRuntime::KernelBreakpointHit(void *baton, StoppointCallbackContext *ctx, user_id_t break_id, 3307b3f7f69dSAidan Dodds user_id_t break_loc_id) 3308018f5a7eSEwan Crawford { 3309018f5a7eSEwan Crawford Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_BREAKPOINTS)); 3310018f5a7eSEwan Crawford 3311018f5a7eSEwan Crawford assert(baton && "Error: null baton in conditional kernel breakpoint callback"); 3312018f5a7eSEwan Crawford 3313018f5a7eSEwan Crawford // Coordinate we want to stop on 33144f8817c2SEwan Crawford const uint32_t *target_coord = static_cast<const uint32_t *>(baton); 3315018f5a7eSEwan Crawford 3316018f5a7eSEwan Crawford if (log) 33174f8817c2SEwan Crawford log->Printf("%s - Break ID %" PRIu64 ", (%" PRIu32 ", %" PRIu32 ", %" PRIu32 ")", __FUNCTION__, break_id, 33184f8817c2SEwan Crawford target_coord[0], target_coord[1], target_coord[2]); 3319018f5a7eSEwan Crawford 33204f8817c2SEwan Crawford // Select current thread 3321018f5a7eSEwan Crawford ExecutionContext context(ctx->exe_ctx_ref); 33224f8817c2SEwan Crawford Thread *thread_ptr = context.GetThreadPtr(); 33234f8817c2SEwan Crawford assert(thread_ptr && "Null thread pointer"); 33244f8817c2SEwan Crawford 33254f8817c2SEwan Crawford // Find current kernel invocation from .expand frame variables 33264f8817c2SEwan Crawford RSCoordinate current_coord{}; // Zero initialise array 33274f8817c2SEwan Crawford if (!GetKernelCoordinate(current_coord, thread_ptr)) 3328018f5a7eSEwan Crawford { 3329018f5a7eSEwan Crawford if (log) 33304f8817c2SEwan Crawford log->Printf("%s - Error, couldn't select .expand stack frame", __FUNCTION__); 3331018f5a7eSEwan Crawford return false; 3332018f5a7eSEwan Crawford } 3333018f5a7eSEwan Crawford 3334018f5a7eSEwan Crawford if (log) 33354f8817c2SEwan Crawford log->Printf("%s - (%" PRIu32 ",%" PRIu32 ",%" PRIu32 ")", __FUNCTION__, current_coord[0], current_coord[1], 33364f8817c2SEwan Crawford current_coord[2]); 3337018f5a7eSEwan Crawford 3338018f5a7eSEwan Crawford // Check if the current kernel invocation coordinate matches our target coordinate 3339b3f7f69dSAidan Dodds if (current_coord[0] == target_coord[0] && 3340b3f7f69dSAidan Dodds current_coord[1] == target_coord[1] && 33414f8817c2SEwan Crawford current_coord[2] == target_coord[2]) 3342018f5a7eSEwan Crawford { 3343018f5a7eSEwan Crawford if (log) 33444f8817c2SEwan Crawford log->Printf("%s, BREAKING (%" PRIu32 ",%" PRIu32 ",%" PRIu32 ")", __FUNCTION__, current_coord[0], 33454f8817c2SEwan Crawford current_coord[1], current_coord[2]); 3346018f5a7eSEwan Crawford 3347018f5a7eSEwan Crawford BreakpointSP breakpoint_sp = context.GetTargetPtr()->GetBreakpointByID(break_id); 3348018f5a7eSEwan Crawford assert(breakpoint_sp != nullptr && "Error: Couldn't find breakpoint matching break id for callback"); 3349018f5a7eSEwan Crawford breakpoint_sp->SetEnabled(false); // Optimise since conditional breakpoint should only be hit once. 3350018f5a7eSEwan Crawford return true; 3351018f5a7eSEwan Crawford } 3352018f5a7eSEwan Crawford 3353018f5a7eSEwan Crawford // No match on coordinate 3354018f5a7eSEwan Crawford return false; 3355018f5a7eSEwan Crawford } 3356018f5a7eSEwan Crawford 3357018f5a7eSEwan Crawford // Tries to set a breakpoint on the start of a kernel, resolved using the kernel name. 3358018f5a7eSEwan Crawford // Argument 'coords', represents a three dimensional coordinate which can be used to specify 3359018f5a7eSEwan Crawford // a single kernel instance to break on. If this is set then we add a callback to the breakpoint. 33604640cde1SColin Riley void 3361018f5a7eSEwan Crawford RenderScriptRuntime::PlaceBreakpointOnKernel(Stream &strm, const char *name, const std::array<int, 3> coords, 3362018f5a7eSEwan Crawford Error &error, TargetSP target) 33634640cde1SColin Riley { 33644640cde1SColin Riley if (!name) 33654640cde1SColin Riley { 33664640cde1SColin Riley error.SetErrorString("invalid kernel name"); 33674640cde1SColin Riley return; 33684640cde1SColin Riley } 33694640cde1SColin Riley 33707dc7771cSEwan Crawford InitSearchFilter(target); 337198156583SEwan Crawford 33724640cde1SColin Riley ConstString kernel_name(name); 33737dc7771cSEwan Crawford BreakpointSP bp = CreateKernelBreakpoint(kernel_name); 3374018f5a7eSEwan Crawford 3375018f5a7eSEwan Crawford // We have a conditional breakpoint on a specific coordinate 3376018f5a7eSEwan Crawford if (coords[0] != -1) 3377018f5a7eSEwan Crawford { 3378b3f7f69dSAidan Dodds strm.Printf("Conditional kernel breakpoint on coordinate %" PRId32 ", %" PRId32 ", %" PRId32, 3379b3f7f69dSAidan Dodds coords[0], coords[1], coords[2]); 3380018f5a7eSEwan Crawford strm.EOL(); 3381018f5a7eSEwan Crawford 3382018f5a7eSEwan Crawford // Allocate memory for the baton, and copy over coordinate 33834f8817c2SEwan Crawford uint32_t *baton = new uint32_t[coords.size()]; 3384018f5a7eSEwan Crawford baton[0] = coords[0]; baton[1] = coords[1]; baton[2] = coords[2]; 3385018f5a7eSEwan Crawford 3386018f5a7eSEwan Crawford // Create a callback that will be invoked everytime the breakpoint is hit. 3387018f5a7eSEwan Crawford // The baton object passed to the handler is the target coordinate we want to break on. 3388018f5a7eSEwan Crawford bp->SetCallback(KernelBreakpointHit, baton, true); 3389018f5a7eSEwan Crawford 3390018f5a7eSEwan Crawford // Store a shared pointer to the baton, so the memory will eventually be cleaned up after destruction 33914f8817c2SEwan Crawford m_conditional_breaks[bp->GetID()] = std::shared_ptr<uint32_t>(baton); 3392018f5a7eSEwan Crawford } 3393018f5a7eSEwan Crawford 339498156583SEwan Crawford if (bp) 339598156583SEwan Crawford bp->GetDescription(&strm, lldb::eDescriptionLevelInitial, false); 33964640cde1SColin Riley } 33974640cde1SColin Riley 33984640cde1SColin Riley void 33995ec532a9SColin Riley RenderScriptRuntime::DumpModules(Stream &strm) const 34005ec532a9SColin Riley { 34015ec532a9SColin Riley strm.Printf("RenderScript Modules:"); 34025ec532a9SColin Riley strm.EOL(); 34035ec532a9SColin Riley strm.IndentMore(); 34045ec532a9SColin Riley for (const auto &module : m_rsmodules) 34055ec532a9SColin Riley { 34064640cde1SColin Riley module->Dump(strm); 34075ec532a9SColin Riley } 34085ec532a9SColin Riley strm.IndentLess(); 34095ec532a9SColin Riley } 34105ec532a9SColin Riley 341178f339d1SEwan Crawford RenderScriptRuntime::ScriptDetails * 341278f339d1SEwan Crawford RenderScriptRuntime::LookUpScript(addr_t address, bool create) 341378f339d1SEwan Crawford { 341478f339d1SEwan Crawford for (const auto &s : m_scripts) 341578f339d1SEwan Crawford { 341678f339d1SEwan Crawford if (s->script.isValid()) 341778f339d1SEwan Crawford if (*s->script == address) 341878f339d1SEwan Crawford return s.get(); 341978f339d1SEwan Crawford } 342078f339d1SEwan Crawford if (create) 342178f339d1SEwan Crawford { 342278f339d1SEwan Crawford std::unique_ptr<ScriptDetails> s(new ScriptDetails); 342378f339d1SEwan Crawford s->script = address; 342478f339d1SEwan Crawford m_scripts.push_back(std::move(s)); 3425d10ca9deSEwan Crawford return m_scripts.back().get(); 342678f339d1SEwan Crawford } 342778f339d1SEwan Crawford return nullptr; 342878f339d1SEwan Crawford } 342978f339d1SEwan Crawford 343078f339d1SEwan Crawford RenderScriptRuntime::AllocationDetails * 343178f339d1SEwan Crawford RenderScriptRuntime::LookUpAllocation(addr_t address, bool create) 343278f339d1SEwan Crawford { 343378f339d1SEwan Crawford for (const auto &a : m_allocations) 343478f339d1SEwan Crawford { 343578f339d1SEwan Crawford if (a->address.isValid()) 343678f339d1SEwan Crawford if (*a->address == address) 343778f339d1SEwan Crawford return a.get(); 343878f339d1SEwan Crawford } 343978f339d1SEwan Crawford if (create) 344078f339d1SEwan Crawford { 344178f339d1SEwan Crawford std::unique_ptr<AllocationDetails> a(new AllocationDetails); 344278f339d1SEwan Crawford a->address = address; 344378f339d1SEwan Crawford m_allocations.push_back(std::move(a)); 3444d10ca9deSEwan Crawford return m_allocations.back().get(); 344578f339d1SEwan Crawford } 344678f339d1SEwan Crawford return nullptr; 344778f339d1SEwan Crawford } 344878f339d1SEwan Crawford 34495ec532a9SColin Riley void 34505ec532a9SColin Riley RSModuleDescriptor::Dump(Stream &strm) const 34515ec532a9SColin Riley { 34525ec532a9SColin Riley strm.Indent(); 34535ec532a9SColin Riley m_module->GetFileSpec().Dump(&strm); 34544640cde1SColin Riley if (m_module->GetNumCompileUnits()) 34554640cde1SColin Riley { 34564640cde1SColin Riley strm.Indent("Debug info loaded."); 34574640cde1SColin Riley } 34584640cde1SColin Riley else 34594640cde1SColin Riley { 34604640cde1SColin Riley strm.Indent("Debug info does not exist."); 34614640cde1SColin Riley } 34625ec532a9SColin Riley strm.EOL(); 34635ec532a9SColin Riley strm.IndentMore(); 34645ec532a9SColin Riley strm.Indent(); 3465189598edSColin Riley strm.Printf("Globals: %" PRIu64, static_cast<uint64_t>(m_globals.size())); 34665ec532a9SColin Riley strm.EOL(); 34675ec532a9SColin Riley strm.IndentMore(); 34685ec532a9SColin Riley for (const auto &global : m_globals) 34695ec532a9SColin Riley { 34705ec532a9SColin Riley global.Dump(strm); 34715ec532a9SColin Riley } 34725ec532a9SColin Riley strm.IndentLess(); 34735ec532a9SColin Riley strm.Indent(); 3474189598edSColin Riley strm.Printf("Kernels: %" PRIu64, static_cast<uint64_t>(m_kernels.size())); 34755ec532a9SColin Riley strm.EOL(); 34765ec532a9SColin Riley strm.IndentMore(); 34775ec532a9SColin Riley for (const auto &kernel : m_kernels) 34785ec532a9SColin Riley { 34795ec532a9SColin Riley kernel.Dump(strm); 34805ec532a9SColin Riley } 34814640cde1SColin Riley strm.Printf("Pragmas: %" PRIu64, static_cast<uint64_t>(m_pragmas.size())); 34824640cde1SColin Riley strm.EOL(); 34834640cde1SColin Riley strm.IndentMore(); 34844640cde1SColin Riley for (const auto &key_val : m_pragmas) 34854640cde1SColin Riley { 34864640cde1SColin Riley strm.Printf("%s: %s", key_val.first.c_str(), key_val.second.c_str()); 34874640cde1SColin Riley strm.EOL(); 34884640cde1SColin Riley } 34895ec532a9SColin Riley strm.IndentLess(4); 34905ec532a9SColin Riley } 34915ec532a9SColin Riley 34925ec532a9SColin Riley void 34935ec532a9SColin Riley RSGlobalDescriptor::Dump(Stream &strm) const 34945ec532a9SColin Riley { 34955ec532a9SColin Riley strm.Indent(m_name.AsCString()); 34964640cde1SColin Riley VariableList var_list; 34974640cde1SColin Riley m_module->m_module->FindGlobalVariables(m_name, nullptr, true, 1U, var_list); 34984640cde1SColin Riley if (var_list.GetSize() == 1) 34994640cde1SColin Riley { 35004640cde1SColin Riley auto var = var_list.GetVariableAtIndex(0); 35014640cde1SColin Riley auto type = var->GetType(); 35024640cde1SColin Riley if (type) 35034640cde1SColin Riley { 35044640cde1SColin Riley strm.Printf(" - "); 35054640cde1SColin Riley type->DumpTypeName(&strm); 35064640cde1SColin Riley } 35074640cde1SColin Riley else 35084640cde1SColin Riley { 35094640cde1SColin Riley strm.Printf(" - Unknown Type"); 35104640cde1SColin Riley } 35114640cde1SColin Riley } 35124640cde1SColin Riley else 35134640cde1SColin Riley { 35144640cde1SColin Riley strm.Printf(" - variable identified, but not found in binary"); 35154640cde1SColin Riley const Symbol *s = m_module->m_module->FindFirstSymbolWithNameAndType(m_name, eSymbolTypeData); 35164640cde1SColin Riley if (s) 35174640cde1SColin Riley { 35184640cde1SColin Riley strm.Printf(" (symbol exists) "); 35194640cde1SColin Riley } 35204640cde1SColin Riley } 35214640cde1SColin Riley 35225ec532a9SColin Riley strm.EOL(); 35235ec532a9SColin Riley } 35245ec532a9SColin Riley 35255ec532a9SColin Riley void 35265ec532a9SColin Riley RSKernelDescriptor::Dump(Stream &strm) const 35275ec532a9SColin Riley { 35285ec532a9SColin Riley strm.Indent(m_name.AsCString()); 35295ec532a9SColin Riley strm.EOL(); 35305ec532a9SColin Riley } 35315ec532a9SColin Riley 35325ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModuleDump : public CommandObjectParsed 35335ec532a9SColin Riley { 35345ec532a9SColin Riley public: 35355ec532a9SColin Riley CommandObjectRenderScriptRuntimeModuleDump(CommandInterpreter &interpreter) 35365ec532a9SColin Riley : CommandObjectParsed(interpreter, "renderscript module dump", 35375ec532a9SColin Riley "Dumps renderscript specific information for all modules.", "renderscript module dump", 3538e87764f2SEnrico Granata eCommandRequiresProcess | eCommandProcessMustBeLaunched) 35395ec532a9SColin Riley { 35405ec532a9SColin Riley } 35415ec532a9SColin Riley 3542222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeModuleDump() override = default; 35435ec532a9SColin Riley 35445ec532a9SColin Riley bool 3545222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 35465ec532a9SColin Riley { 35475ec532a9SColin Riley RenderScriptRuntime *runtime = 35485ec532a9SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 35495ec532a9SColin Riley runtime->DumpModules(result.GetOutputStream()); 35505ec532a9SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 35515ec532a9SColin Riley return true; 35525ec532a9SColin Riley } 35535ec532a9SColin Riley }; 35545ec532a9SColin Riley 35555ec532a9SColin Riley class CommandObjectRenderScriptRuntimeModule : public CommandObjectMultiword 35565ec532a9SColin Riley { 35575ec532a9SColin Riley public: 35585ec532a9SColin Riley CommandObjectRenderScriptRuntimeModule(CommandInterpreter &interpreter) 35595ec532a9SColin Riley : CommandObjectMultiword(interpreter, "renderscript module", "Commands that deal with renderscript modules.", 3560b3f7f69dSAidan Dodds nullptr) 35615ec532a9SColin Riley { 35625ec532a9SColin Riley LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeModuleDump(interpreter))); 35635ec532a9SColin Riley } 35645ec532a9SColin Riley 3565222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeModule() override = default; 35665ec532a9SColin Riley }; 35675ec532a9SColin Riley 35684640cde1SColin Riley class CommandObjectRenderScriptRuntimeKernelList : public CommandObjectParsed 35694640cde1SColin Riley { 35704640cde1SColin Riley public: 35714640cde1SColin Riley CommandObjectRenderScriptRuntimeKernelList(CommandInterpreter &interpreter) 35724640cde1SColin Riley : CommandObjectParsed(interpreter, "renderscript kernel list", 3573b3f7f69dSAidan Dodds "Lists renderscript kernel names and associated script resources.", 3574b3f7f69dSAidan Dodds "renderscript kernel list", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 35754640cde1SColin Riley { 35764640cde1SColin Riley } 35774640cde1SColin Riley 3578222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelList() override = default; 35794640cde1SColin Riley 35804640cde1SColin Riley bool 3581222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 35824640cde1SColin Riley { 35834640cde1SColin Riley RenderScriptRuntime *runtime = 35844640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 35854640cde1SColin Riley runtime->DumpKernels(result.GetOutputStream()); 35864640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 35874640cde1SColin Riley return true; 35884640cde1SColin Riley } 35894640cde1SColin Riley }; 35904640cde1SColin Riley 35917dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpointSet : public CommandObjectParsed 35924640cde1SColin Riley { 35934640cde1SColin Riley public: 35947dc7771cSEwan Crawford CommandObjectRenderScriptRuntimeKernelBreakpointSet(CommandInterpreter &interpreter) 35957dc7771cSEwan Crawford : CommandObjectParsed(interpreter, "renderscript kernel breakpoint set", 3596b3f7f69dSAidan Dodds "Sets a breakpoint on a renderscript kernel.", 3597b3f7f69dSAidan Dodds "renderscript kernel breakpoint set <kernel_name> [-c x,y,z]", 3598b3f7f69dSAidan Dodds eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 3599b3f7f69dSAidan Dodds m_options(interpreter) 36004640cde1SColin Riley { 36014640cde1SColin Riley } 36024640cde1SColin Riley 3603222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelBreakpointSet() override = default; 3604222b937cSEugene Zelenko 3605222b937cSEugene Zelenko Options * 3606222b937cSEugene Zelenko GetOptions() override 3607018f5a7eSEwan Crawford { 3608018f5a7eSEwan Crawford return &m_options; 3609018f5a7eSEwan Crawford } 3610018f5a7eSEwan Crawford 3611018f5a7eSEwan Crawford class CommandOptions : public Options 3612018f5a7eSEwan Crawford { 3613018f5a7eSEwan Crawford public: 3614b3f7f69dSAidan Dodds CommandOptions(CommandInterpreter &interpreter) : Options(interpreter) {} 3615018f5a7eSEwan Crawford 3616222b937cSEugene Zelenko ~CommandOptions() override = default; 3617018f5a7eSEwan Crawford 3618222b937cSEugene Zelenko Error 3619222b937cSEugene Zelenko SetOptionValue(uint32_t option_idx, const char *option_arg) override 3620018f5a7eSEwan Crawford { 3621018f5a7eSEwan Crawford Error error; 3622018f5a7eSEwan Crawford const int short_option = m_getopt_table[option_idx].val; 3623018f5a7eSEwan Crawford 3624018f5a7eSEwan Crawford switch (short_option) 3625018f5a7eSEwan Crawford { 3626018f5a7eSEwan Crawford case 'c': 3627018f5a7eSEwan Crawford if (!ParseCoordinate(option_arg)) 3628b3f7f69dSAidan Dodds error.SetErrorStringWithFormat("Couldn't parse coordinate '%s', should be in format 'x,y,z'.", 3629b3f7f69dSAidan Dodds option_arg); 3630018f5a7eSEwan Crawford break; 3631018f5a7eSEwan Crawford default: 3632018f5a7eSEwan Crawford error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 3633018f5a7eSEwan Crawford break; 3634018f5a7eSEwan Crawford } 3635018f5a7eSEwan Crawford return error; 3636018f5a7eSEwan Crawford } 3637018f5a7eSEwan Crawford 3638018f5a7eSEwan Crawford // -c takes an argument of the form 'num[,num][,num]'. 3639018f5a7eSEwan Crawford // Where 'id_cstr' is this argument with the whitespace trimmed. 3640018f5a7eSEwan Crawford // Missing coordinates are defaulted to zero. 3641018f5a7eSEwan Crawford bool 3642018f5a7eSEwan Crawford ParseCoordinate(const char *id_cstr) 3643018f5a7eSEwan Crawford { 3644018f5a7eSEwan Crawford RegularExpression regex; 3645018f5a7eSEwan Crawford RegularExpression::Match regex_match(3); 3646018f5a7eSEwan Crawford 3647018f5a7eSEwan Crawford bool matched = false; 3648018f5a7eSEwan Crawford if (regex.Compile("^([0-9]+),([0-9]+),([0-9]+)$") && regex.Execute(id_cstr, ®ex_match)) 3649018f5a7eSEwan Crawford matched = true; 3650018f5a7eSEwan Crawford else if (regex.Compile("^([0-9]+),([0-9]+)$") && regex.Execute(id_cstr, ®ex_match)) 3651018f5a7eSEwan Crawford matched = true; 3652018f5a7eSEwan Crawford else if (regex.Compile("^([0-9]+)$") && regex.Execute(id_cstr, ®ex_match)) 3653018f5a7eSEwan Crawford matched = true; 3654018f5a7eSEwan Crawford for (uint32_t i = 0; i < 3; i++) 3655018f5a7eSEwan Crawford { 3656018f5a7eSEwan Crawford std::string group; 3657018f5a7eSEwan Crawford if (regex_match.GetMatchAtIndex(id_cstr, i + 1, group)) 3658b3f7f69dSAidan Dodds m_coord[i] = (uint32_t)strtoul(group.c_str(), nullptr, 0); 3659018f5a7eSEwan Crawford else 3660018f5a7eSEwan Crawford m_coord[i] = 0; 3661018f5a7eSEwan Crawford } 3662018f5a7eSEwan Crawford return matched; 3663018f5a7eSEwan Crawford } 3664018f5a7eSEwan Crawford 3665018f5a7eSEwan Crawford void 3666222b937cSEugene Zelenko OptionParsingStarting() override 3667018f5a7eSEwan Crawford { 3668018f5a7eSEwan Crawford // -1 means the -c option hasn't been set 3669018f5a7eSEwan Crawford m_coord[0] = -1; 3670018f5a7eSEwan Crawford m_coord[1] = -1; 3671018f5a7eSEwan Crawford m_coord[2] = -1; 3672018f5a7eSEwan Crawford } 3673018f5a7eSEwan Crawford 3674018f5a7eSEwan Crawford const OptionDefinition * 3675222b937cSEugene Zelenko GetDefinitions() override 3676018f5a7eSEwan Crawford { 3677018f5a7eSEwan Crawford return g_option_table; 3678018f5a7eSEwan Crawford } 3679018f5a7eSEwan Crawford 3680018f5a7eSEwan Crawford static OptionDefinition g_option_table[]; 3681018f5a7eSEwan Crawford std::array<int, 3> m_coord; 3682018f5a7eSEwan Crawford }; 3683018f5a7eSEwan Crawford 36844640cde1SColin Riley bool 3685222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 36864640cde1SColin Riley { 36874640cde1SColin Riley const size_t argc = command.GetArgumentCount(); 3688018f5a7eSEwan Crawford if (argc < 1) 36894640cde1SColin Riley { 3690b3f7f69dSAidan Dodds result.AppendErrorWithFormat("'%s' takes 1 argument of kernel name, and an optional coordinate.", 3691b3f7f69dSAidan Dodds m_cmd_name.c_str()); 3692018f5a7eSEwan Crawford result.SetStatus(eReturnStatusFailed); 3693018f5a7eSEwan Crawford return false; 3694018f5a7eSEwan Crawford } 3695018f5a7eSEwan Crawford 36964640cde1SColin Riley RenderScriptRuntime *runtime = 36974640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 36984640cde1SColin Riley 36994640cde1SColin Riley Error error; 3700018f5a7eSEwan Crawford runtime->PlaceBreakpointOnKernel(result.GetOutputStream(), command.GetArgumentAtIndex(0), m_options.m_coord, 370198156583SEwan Crawford error, m_exe_ctx.GetTargetSP()); 37024640cde1SColin Riley 37034640cde1SColin Riley if (error.Success()) 37044640cde1SColin Riley { 37054640cde1SColin Riley result.AppendMessage("Breakpoint(s) created"); 37064640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 37074640cde1SColin Riley return true; 37084640cde1SColin Riley } 37094640cde1SColin Riley result.SetStatus(eReturnStatusFailed); 37104640cde1SColin Riley result.AppendErrorWithFormat("Error: %s", error.AsCString()); 37114640cde1SColin Riley return false; 37124640cde1SColin Riley } 37134640cde1SColin Riley 3714018f5a7eSEwan Crawford private: 3715018f5a7eSEwan Crawford CommandOptions m_options; 37164640cde1SColin Riley }; 37174640cde1SColin Riley 3718b3f7f69dSAidan Dodds OptionDefinition CommandObjectRenderScriptRuntimeKernelBreakpointSet::CommandOptions::g_option_table[] = { 3719b3f7f69dSAidan Dodds {LLDB_OPT_SET_1, false, "coordinate", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeValue, 3720018f5a7eSEwan Crawford "Set a breakpoint on a single invocation of the kernel with specified coordinate.\n" 3721018f5a7eSEwan Crawford "Coordinate takes the form 'x[,y][,z] where x,y,z are positive integers representing kernel dimensions. " 3722018f5a7eSEwan Crawford "Any unset dimensions will be defaulted to zero."}, 3723b3f7f69dSAidan Dodds {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}}; 3724018f5a7eSEwan Crawford 37257dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpointAll : public CommandObjectParsed 37267dc7771cSEwan Crawford { 37277dc7771cSEwan Crawford public: 37287dc7771cSEwan Crawford CommandObjectRenderScriptRuntimeKernelBreakpointAll(CommandInterpreter &interpreter) 3729b3f7f69dSAidan Dodds : CommandObjectParsed( 3730b3f7f69dSAidan Dodds interpreter, "renderscript kernel breakpoint all", 37317dc7771cSEwan Crawford "Automatically sets a breakpoint on all renderscript kernels that are or will be loaded.\n" 37327dc7771cSEwan Crawford "Disabling option means breakpoints will no longer be set on any kernels loaded in the future, " 37337dc7771cSEwan Crawford "but does not remove currently set breakpoints.", 37347dc7771cSEwan Crawford "renderscript kernel breakpoint all <enable/disable>", 37357dc7771cSEwan Crawford eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) 37367dc7771cSEwan Crawford { 37377dc7771cSEwan Crawford } 37387dc7771cSEwan Crawford 3739222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelBreakpointAll() override = default; 37407dc7771cSEwan Crawford 37417dc7771cSEwan Crawford bool 3742222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 37437dc7771cSEwan Crawford { 37447dc7771cSEwan Crawford const size_t argc = command.GetArgumentCount(); 37457dc7771cSEwan Crawford if (argc != 1) 37467dc7771cSEwan Crawford { 37477dc7771cSEwan Crawford result.AppendErrorWithFormat("'%s' takes 1 argument of 'enable' or 'disable'", m_cmd_name.c_str()); 37487dc7771cSEwan Crawford result.SetStatus(eReturnStatusFailed); 37497dc7771cSEwan Crawford return false; 37507dc7771cSEwan Crawford } 37517dc7771cSEwan Crawford 3752b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 3753b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 37547dc7771cSEwan Crawford 37557dc7771cSEwan Crawford bool do_break = false; 37567dc7771cSEwan Crawford const char *argument = command.GetArgumentAtIndex(0); 37577dc7771cSEwan Crawford if (strcmp(argument, "enable") == 0) 37587dc7771cSEwan Crawford { 37597dc7771cSEwan Crawford do_break = true; 37607dc7771cSEwan Crawford result.AppendMessage("Breakpoints will be set on all kernels."); 37617dc7771cSEwan Crawford } 37627dc7771cSEwan Crawford else if (strcmp(argument, "disable") == 0) 37637dc7771cSEwan Crawford { 37647dc7771cSEwan Crawford do_break = false; 37657dc7771cSEwan Crawford result.AppendMessage("Breakpoints will not be set on any new kernels."); 37667dc7771cSEwan Crawford } 37677dc7771cSEwan Crawford else 37687dc7771cSEwan Crawford { 37697dc7771cSEwan Crawford result.AppendErrorWithFormat("Argument must be either 'enable' or 'disable'"); 37707dc7771cSEwan Crawford result.SetStatus(eReturnStatusFailed); 37717dc7771cSEwan Crawford return false; 37727dc7771cSEwan Crawford } 37737dc7771cSEwan Crawford 37747dc7771cSEwan Crawford runtime->SetBreakAllKernels(do_break, m_exe_ctx.GetTargetSP()); 37757dc7771cSEwan Crawford 37767dc7771cSEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 37777dc7771cSEwan Crawford return true; 37787dc7771cSEwan Crawford } 37797dc7771cSEwan Crawford }; 37807dc7771cSEwan Crawford 37814f8817c2SEwan Crawford class CommandObjectRenderScriptRuntimeKernelCoordinate : public CommandObjectParsed 37824f8817c2SEwan Crawford { 37834f8817c2SEwan Crawford public: 37844f8817c2SEwan Crawford CommandObjectRenderScriptRuntimeKernelCoordinate(CommandInterpreter &interpreter) 37854f8817c2SEwan Crawford : CommandObjectParsed(interpreter, "renderscript kernel coordinate", 37864f8817c2SEwan Crawford "Shows the (x,y,z) coordinate of the current kernel invocation.", 37874f8817c2SEwan Crawford "renderscript kernel coordinate", 37884f8817c2SEwan Crawford eCommandRequiresProcess | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) 37894f8817c2SEwan Crawford { 37904f8817c2SEwan Crawford } 37914f8817c2SEwan Crawford 37924f8817c2SEwan Crawford ~CommandObjectRenderScriptRuntimeKernelCoordinate() override = default; 37934f8817c2SEwan Crawford 37944f8817c2SEwan Crawford bool 37954f8817c2SEwan Crawford DoExecute(Args &command, CommandReturnObject &result) override 37964f8817c2SEwan Crawford { 37974f8817c2SEwan Crawford RSCoordinate coord{}; // Zero initialize array 37984f8817c2SEwan Crawford bool success = RenderScriptRuntime::GetKernelCoordinate(coord, m_exe_ctx.GetThreadPtr()); 37994f8817c2SEwan Crawford Stream &stream = result.GetOutputStream(); 38004f8817c2SEwan Crawford 38014f8817c2SEwan Crawford if (success) 38024f8817c2SEwan Crawford { 38034f8817c2SEwan Crawford stream.Printf("Coordinate: (%" PRIu32 ", %" PRIu32 ", %" PRIu32 ")", coord[0], coord[1], coord[2]); 38044f8817c2SEwan Crawford stream.EOL(); 38054f8817c2SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 38064f8817c2SEwan Crawford } 38074f8817c2SEwan Crawford else 38084f8817c2SEwan Crawford { 38094f8817c2SEwan Crawford stream.Printf("Error: Coordinate could not be found."); 38104f8817c2SEwan Crawford stream.EOL(); 38114f8817c2SEwan Crawford result.SetStatus(eReturnStatusFailed); 38124f8817c2SEwan Crawford } 38134f8817c2SEwan Crawford return true; 38144f8817c2SEwan Crawford } 38154f8817c2SEwan Crawford }; 38164f8817c2SEwan Crawford 38177dc7771cSEwan Crawford class CommandObjectRenderScriptRuntimeKernelBreakpoint : public CommandObjectMultiword 38187dc7771cSEwan Crawford { 38197dc7771cSEwan Crawford public: 38207dc7771cSEwan Crawford CommandObjectRenderScriptRuntimeKernelBreakpoint(CommandInterpreter &interpreter) 3821b3f7f69dSAidan Dodds : CommandObjectMultiword(interpreter, "renderscript kernel", 3822b3f7f69dSAidan Dodds "Commands that generate breakpoints on renderscript kernels.", nullptr) 38237dc7771cSEwan Crawford { 38247dc7771cSEwan Crawford LoadSubCommand("set", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpointSet(interpreter))); 38257dc7771cSEwan Crawford LoadSubCommand("all", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpointAll(interpreter))); 38267dc7771cSEwan Crawford } 38277dc7771cSEwan Crawford 3828222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernelBreakpoint() override = default; 38297dc7771cSEwan Crawford }; 38307dc7771cSEwan Crawford 38314640cde1SColin Riley class CommandObjectRenderScriptRuntimeKernel : public CommandObjectMultiword 38324640cde1SColin Riley { 38334640cde1SColin Riley public: 38344640cde1SColin Riley CommandObjectRenderScriptRuntimeKernel(CommandInterpreter &interpreter) 38354640cde1SColin Riley : CommandObjectMultiword(interpreter, "renderscript kernel", "Commands that deal with renderscript kernels.", 3836b3f7f69dSAidan Dodds nullptr) 38374640cde1SColin Riley { 38384640cde1SColin Riley LoadSubCommand("list", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelList(interpreter))); 383936175cc0SEwan Crawford LoadSubCommand("coordinate", 384036175cc0SEwan Crawford CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelCoordinate(interpreter))); 3841b3f7f69dSAidan Dodds LoadSubCommand("breakpoint", 3842b3f7f69dSAidan Dodds CommandObjectSP(new CommandObjectRenderScriptRuntimeKernelBreakpoint(interpreter))); 38434640cde1SColin Riley } 38444640cde1SColin Riley 3845222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeKernel() override = default; 38464640cde1SColin Riley }; 38474640cde1SColin Riley 38484640cde1SColin Riley class CommandObjectRenderScriptRuntimeContextDump : public CommandObjectParsed 38494640cde1SColin Riley { 38504640cde1SColin Riley public: 38514640cde1SColin Riley CommandObjectRenderScriptRuntimeContextDump(CommandInterpreter &interpreter) 3852b3f7f69dSAidan Dodds : CommandObjectParsed(interpreter, "renderscript context dump", "Dumps renderscript context information.", 3853b3f7f69dSAidan Dodds "renderscript context dump", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 38544640cde1SColin Riley { 38554640cde1SColin Riley } 38564640cde1SColin Riley 3857222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeContextDump() override = default; 38584640cde1SColin Riley 38594640cde1SColin Riley bool 3860222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 38614640cde1SColin Riley { 38624640cde1SColin Riley RenderScriptRuntime *runtime = 38634640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 38644640cde1SColin Riley runtime->DumpContexts(result.GetOutputStream()); 38654640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 38664640cde1SColin Riley return true; 38674640cde1SColin Riley } 38684640cde1SColin Riley }; 38694640cde1SColin Riley 38704640cde1SColin Riley class CommandObjectRenderScriptRuntimeContext : public CommandObjectMultiword 38714640cde1SColin Riley { 38724640cde1SColin Riley public: 38734640cde1SColin Riley CommandObjectRenderScriptRuntimeContext(CommandInterpreter &interpreter) 38744640cde1SColin Riley : CommandObjectMultiword(interpreter, "renderscript context", "Commands that deal with renderscript contexts.", 3875b3f7f69dSAidan Dodds nullptr) 38764640cde1SColin Riley { 38774640cde1SColin Riley LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeContextDump(interpreter))); 38784640cde1SColin Riley } 38794640cde1SColin Riley 3880222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeContext() override = default; 38814640cde1SColin Riley }; 38824640cde1SColin Riley 3883a0f08674SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationDump : public CommandObjectParsed 3884a0f08674SEwan Crawford { 3885a0f08674SEwan Crawford public: 3886a0f08674SEwan Crawford CommandObjectRenderScriptRuntimeAllocationDump(CommandInterpreter &interpreter) 3887a0f08674SEwan Crawford : CommandObjectParsed(interpreter, "renderscript allocation dump", 3888a0f08674SEwan Crawford "Displays the contents of a particular allocation", "renderscript allocation dump <ID>", 3889b3f7f69dSAidan Dodds eCommandRequiresProcess | eCommandProcessMustBeLaunched), 3890b3f7f69dSAidan Dodds m_options(interpreter) 3891a0f08674SEwan Crawford { 3892a0f08674SEwan Crawford } 3893a0f08674SEwan Crawford 3894222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationDump() override = default; 3895222b937cSEugene Zelenko 3896222b937cSEugene Zelenko Options * 3897222b937cSEugene Zelenko GetOptions() override 3898a0f08674SEwan Crawford { 3899a0f08674SEwan Crawford return &m_options; 3900a0f08674SEwan Crawford } 3901a0f08674SEwan Crawford 3902a0f08674SEwan Crawford class CommandOptions : public Options 3903a0f08674SEwan Crawford { 3904a0f08674SEwan Crawford public: 3905b3f7f69dSAidan Dodds CommandOptions(CommandInterpreter &interpreter) : Options(interpreter) {} 3906a0f08674SEwan Crawford 3907222b937cSEugene Zelenko ~CommandOptions() override = default; 3908a0f08674SEwan Crawford 3909222b937cSEugene Zelenko Error 3910222b937cSEugene Zelenko SetOptionValue(uint32_t option_idx, const char *option_arg) override 3911a0f08674SEwan Crawford { 3912a0f08674SEwan Crawford Error error; 3913a0f08674SEwan Crawford const int short_option = m_getopt_table[option_idx].val; 3914a0f08674SEwan Crawford 3915a0f08674SEwan Crawford switch (short_option) 3916a0f08674SEwan Crawford { 3917a0f08674SEwan Crawford case 'f': 3918a0f08674SEwan Crawford m_outfile.SetFile(option_arg, true); 3919a0f08674SEwan Crawford if (m_outfile.Exists()) 3920a0f08674SEwan Crawford { 3921a0f08674SEwan Crawford m_outfile.Clear(); 3922a0f08674SEwan Crawford error.SetErrorStringWithFormat("file already exists: '%s'", option_arg); 3923a0f08674SEwan Crawford } 3924a0f08674SEwan Crawford break; 3925a0f08674SEwan Crawford default: 3926a0f08674SEwan Crawford error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 3927a0f08674SEwan Crawford break; 3928a0f08674SEwan Crawford } 3929a0f08674SEwan Crawford return error; 3930a0f08674SEwan Crawford } 3931a0f08674SEwan Crawford 3932a0f08674SEwan Crawford void 3933222b937cSEugene Zelenko OptionParsingStarting() override 3934a0f08674SEwan Crawford { 3935a0f08674SEwan Crawford m_outfile.Clear(); 3936a0f08674SEwan Crawford } 3937a0f08674SEwan Crawford 3938a0f08674SEwan Crawford const OptionDefinition * 3939222b937cSEugene Zelenko GetDefinitions() override 3940a0f08674SEwan Crawford { 3941a0f08674SEwan Crawford return g_option_table; 3942a0f08674SEwan Crawford } 3943a0f08674SEwan Crawford 3944a0f08674SEwan Crawford static OptionDefinition g_option_table[]; 3945a0f08674SEwan Crawford FileSpec m_outfile; 3946a0f08674SEwan Crawford }; 3947a0f08674SEwan Crawford 3948a0f08674SEwan Crawford bool 3949222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 3950a0f08674SEwan Crawford { 3951a0f08674SEwan Crawford const size_t argc = command.GetArgumentCount(); 3952a0f08674SEwan Crawford if (argc < 1) 3953a0f08674SEwan Crawford { 3954a0f08674SEwan Crawford result.AppendErrorWithFormat("'%s' takes 1 argument, an allocation ID. As well as an optional -f argument", 3955a0f08674SEwan Crawford m_cmd_name.c_str()); 3956a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3957a0f08674SEwan Crawford return false; 3958a0f08674SEwan Crawford } 3959a0f08674SEwan Crawford 3960b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 3961b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 3962a0f08674SEwan Crawford 3963a0f08674SEwan Crawford const char *id_cstr = command.GetArgumentAtIndex(0); 3964a0f08674SEwan Crawford bool convert_complete = false; 3965a0f08674SEwan Crawford const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete); 3966a0f08674SEwan Crawford if (!convert_complete) 3967a0f08674SEwan Crawford { 3968a0f08674SEwan Crawford result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr); 3969a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3970a0f08674SEwan Crawford return false; 3971a0f08674SEwan Crawford } 3972a0f08674SEwan Crawford 3973a0f08674SEwan Crawford Stream *output_strm = nullptr; 3974a0f08674SEwan Crawford StreamFile outfile_stream; 3975a0f08674SEwan Crawford const FileSpec &outfile_spec = m_options.m_outfile; // Dump allocation to file instead 3976a0f08674SEwan Crawford if (outfile_spec) 3977a0f08674SEwan Crawford { 3978a0f08674SEwan Crawford // Open output file 3979a0f08674SEwan Crawford char path[256]; 3980a0f08674SEwan Crawford outfile_spec.GetPath(path, sizeof(path)); 3981a0f08674SEwan Crawford if (outfile_stream.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate).Success()) 3982a0f08674SEwan Crawford { 3983a0f08674SEwan Crawford output_strm = &outfile_stream; 3984a0f08674SEwan Crawford result.GetOutputStream().Printf("Results written to '%s'", path); 3985a0f08674SEwan Crawford result.GetOutputStream().EOL(); 3986a0f08674SEwan Crawford } 3987a0f08674SEwan Crawford else 3988a0f08674SEwan Crawford { 3989a0f08674SEwan Crawford result.AppendErrorWithFormat("Couldn't open file '%s'", path); 3990a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 3991a0f08674SEwan Crawford return false; 3992a0f08674SEwan Crawford } 3993a0f08674SEwan Crawford } 3994a0f08674SEwan Crawford else 3995a0f08674SEwan Crawford output_strm = &result.GetOutputStream(); 3996a0f08674SEwan Crawford 3997a0f08674SEwan Crawford assert(output_strm != nullptr); 3998a0f08674SEwan Crawford bool success = runtime->DumpAllocation(*output_strm, m_exe_ctx.GetFramePtr(), id); 3999a0f08674SEwan Crawford 4000a0f08674SEwan Crawford if (success) 4001a0f08674SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 4002a0f08674SEwan Crawford else 4003a0f08674SEwan Crawford result.SetStatus(eReturnStatusFailed); 4004a0f08674SEwan Crawford 4005a0f08674SEwan Crawford return true; 4006a0f08674SEwan Crawford } 4007a0f08674SEwan Crawford 4008a0f08674SEwan Crawford private: 4009a0f08674SEwan Crawford CommandOptions m_options; 4010a0f08674SEwan Crawford }; 4011a0f08674SEwan Crawford 4012b3f7f69dSAidan Dodds OptionDefinition CommandObjectRenderScriptRuntimeAllocationDump::CommandOptions::g_option_table[] = { 4013b3f7f69dSAidan Dodds {LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFilename, 4014a0f08674SEwan Crawford "Print results to specified file instead of command line."}, 4015b3f7f69dSAidan Dodds {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}}; 4016a0f08674SEwan Crawford 401715f2bd95SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationList : public CommandObjectParsed 401815f2bd95SEwan Crawford { 401915f2bd95SEwan Crawford public: 402015f2bd95SEwan Crawford CommandObjectRenderScriptRuntimeAllocationList(CommandInterpreter &interpreter) 402115f2bd95SEwan Crawford : CommandObjectParsed(interpreter, "renderscript allocation list", 402215f2bd95SEwan Crawford "List renderscript allocations and their information.", "renderscript allocation list", 4023b3f7f69dSAidan Dodds eCommandRequiresProcess | eCommandProcessMustBeLaunched), 4024b3f7f69dSAidan Dodds m_options(interpreter) 402515f2bd95SEwan Crawford { 402615f2bd95SEwan Crawford } 402715f2bd95SEwan Crawford 4028222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationList() override = default; 4029222b937cSEugene Zelenko 4030222b937cSEugene Zelenko Options * 4031222b937cSEugene Zelenko GetOptions() override 403215f2bd95SEwan Crawford { 403315f2bd95SEwan Crawford return &m_options; 403415f2bd95SEwan Crawford } 403515f2bd95SEwan Crawford 403615f2bd95SEwan Crawford class CommandOptions : public Options 403715f2bd95SEwan Crawford { 403815f2bd95SEwan Crawford public: 4039b649b005SEwan Crawford CommandOptions(CommandInterpreter &interpreter) : Options(interpreter), m_id(0) {} 404015f2bd95SEwan Crawford 4041222b937cSEugene Zelenko ~CommandOptions() override = default; 404215f2bd95SEwan Crawford 4043222b937cSEugene Zelenko Error 4044222b937cSEugene Zelenko SetOptionValue(uint32_t option_idx, const char *option_arg) override 404515f2bd95SEwan Crawford { 404615f2bd95SEwan Crawford Error error; 404715f2bd95SEwan Crawford const int short_option = m_getopt_table[option_idx].val; 404815f2bd95SEwan Crawford 404915f2bd95SEwan Crawford switch (short_option) 405015f2bd95SEwan Crawford { 4051b649b005SEwan Crawford case 'i': 4052b649b005SEwan Crawford bool success; 4053b649b005SEwan Crawford m_id = StringConvert::ToUInt32(option_arg, 0, 0, &success); 4054b649b005SEwan Crawford if (!success) 4055b649b005SEwan Crawford error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option); 405615f2bd95SEwan Crawford break; 405715f2bd95SEwan Crawford default: 405815f2bd95SEwan Crawford error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 405915f2bd95SEwan Crawford break; 406015f2bd95SEwan Crawford } 406115f2bd95SEwan Crawford return error; 406215f2bd95SEwan Crawford } 406315f2bd95SEwan Crawford 406415f2bd95SEwan Crawford void 4065222b937cSEugene Zelenko OptionParsingStarting() override 406615f2bd95SEwan Crawford { 4067b649b005SEwan Crawford m_id = 0; 406815f2bd95SEwan Crawford } 406915f2bd95SEwan Crawford 407015f2bd95SEwan Crawford const OptionDefinition * 4071222b937cSEugene Zelenko GetDefinitions() override 407215f2bd95SEwan Crawford { 407315f2bd95SEwan Crawford return g_option_table; 407415f2bd95SEwan Crawford } 407515f2bd95SEwan Crawford 407615f2bd95SEwan Crawford static OptionDefinition g_option_table[]; 4077b649b005SEwan Crawford uint32_t m_id; 407815f2bd95SEwan Crawford }; 407915f2bd95SEwan Crawford 408015f2bd95SEwan Crawford bool 4081222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 408215f2bd95SEwan Crawford { 4083b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 4084b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 4085b649b005SEwan Crawford runtime->ListAllocations(result.GetOutputStream(), m_exe_ctx.GetFramePtr(), m_options.m_id); 408615f2bd95SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 408715f2bd95SEwan Crawford return true; 408815f2bd95SEwan Crawford } 408915f2bd95SEwan Crawford 409015f2bd95SEwan Crawford private: 409115f2bd95SEwan Crawford CommandOptions m_options; 409215f2bd95SEwan Crawford }; 409315f2bd95SEwan Crawford 4094b649b005SEwan Crawford OptionDefinition CommandObjectRenderScriptRuntimeAllocationList::CommandOptions::g_option_table[] = { 4095b3f7f69dSAidan Dodds {LLDB_OPT_SET_1, false, "id", 'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeIndex, 4096b649b005SEwan Crawford "Only show details of a single allocation with specified id."}, 4097b3f7f69dSAidan Dodds {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr}}; 409815f2bd95SEwan Crawford 409955232f09SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationLoad : public CommandObjectParsed 410055232f09SEwan Crawford { 410155232f09SEwan Crawford public: 410255232f09SEwan Crawford CommandObjectRenderScriptRuntimeAllocationLoad(CommandInterpreter &interpreter) 4103b3f7f69dSAidan Dodds : CommandObjectParsed( 4104b3f7f69dSAidan Dodds interpreter, "renderscript allocation load", "Loads renderscript allocation contents from a file.", 4105b3f7f69dSAidan Dodds "renderscript allocation load <ID> <filename>", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 410655232f09SEwan Crawford { 410755232f09SEwan Crawford } 410855232f09SEwan Crawford 4109222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationLoad() override = default; 411055232f09SEwan Crawford 411155232f09SEwan Crawford bool 4112222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 411355232f09SEwan Crawford { 411455232f09SEwan Crawford const size_t argc = command.GetArgumentCount(); 411555232f09SEwan Crawford if (argc != 2) 411655232f09SEwan Crawford { 4117b3f7f69dSAidan Dodds result.AppendErrorWithFormat("'%s' takes 2 arguments, an allocation ID and filename to read from.", 4118b3f7f69dSAidan Dodds m_cmd_name.c_str()); 411955232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 412055232f09SEwan Crawford return false; 412155232f09SEwan Crawford } 412255232f09SEwan Crawford 4123b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 4124b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 412555232f09SEwan Crawford 412655232f09SEwan Crawford const char *id_cstr = command.GetArgumentAtIndex(0); 412755232f09SEwan Crawford bool convert_complete = false; 412855232f09SEwan Crawford const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete); 412955232f09SEwan Crawford if (!convert_complete) 413055232f09SEwan Crawford { 413155232f09SEwan Crawford result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr); 413255232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 413355232f09SEwan Crawford return false; 413455232f09SEwan Crawford } 413555232f09SEwan Crawford 413655232f09SEwan Crawford const char *filename = command.GetArgumentAtIndex(1); 413755232f09SEwan Crawford bool success = runtime->LoadAllocation(result.GetOutputStream(), id, filename, m_exe_ctx.GetFramePtr()); 413855232f09SEwan Crawford 413955232f09SEwan Crawford if (success) 414055232f09SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 414155232f09SEwan Crawford else 414255232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 414355232f09SEwan Crawford 414455232f09SEwan Crawford return true; 414555232f09SEwan Crawford } 414655232f09SEwan Crawford }; 414755232f09SEwan Crawford 414855232f09SEwan Crawford class CommandObjectRenderScriptRuntimeAllocationSave : public CommandObjectParsed 414955232f09SEwan Crawford { 415055232f09SEwan Crawford public: 415155232f09SEwan Crawford CommandObjectRenderScriptRuntimeAllocationSave(CommandInterpreter &interpreter) 4152b3f7f69dSAidan Dodds : CommandObjectParsed( 4153b3f7f69dSAidan Dodds interpreter, "renderscript allocation save", "Write renderscript allocation contents to a file.", 4154b3f7f69dSAidan Dodds "renderscript allocation save <ID> <filename>", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 415555232f09SEwan Crawford { 415655232f09SEwan Crawford } 415755232f09SEwan Crawford 4158222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocationSave() override = default; 415955232f09SEwan Crawford 416055232f09SEwan Crawford bool 4161222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 416255232f09SEwan Crawford { 416355232f09SEwan Crawford const size_t argc = command.GetArgumentCount(); 416455232f09SEwan Crawford if (argc != 2) 416555232f09SEwan Crawford { 4166b3f7f69dSAidan Dodds result.AppendErrorWithFormat("'%s' takes 2 arguments, an allocation ID and filename to read from.", 4167b3f7f69dSAidan Dodds m_cmd_name.c_str()); 416855232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 416955232f09SEwan Crawford return false; 417055232f09SEwan Crawford } 417155232f09SEwan Crawford 4172b3f7f69dSAidan Dodds RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 4173b3f7f69dSAidan Dodds m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 417455232f09SEwan Crawford 417555232f09SEwan Crawford const char *id_cstr = command.GetArgumentAtIndex(0); 417655232f09SEwan Crawford bool convert_complete = false; 417755232f09SEwan Crawford const uint32_t id = StringConvert::ToUInt32(id_cstr, UINT32_MAX, 0, &convert_complete); 417855232f09SEwan Crawford if (!convert_complete) 417955232f09SEwan Crawford { 418055232f09SEwan Crawford result.AppendErrorWithFormat("invalid allocation id argument '%s'", id_cstr); 418155232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 418255232f09SEwan Crawford return false; 418355232f09SEwan Crawford } 418455232f09SEwan Crawford 418555232f09SEwan Crawford const char *filename = command.GetArgumentAtIndex(1); 418655232f09SEwan Crawford bool success = runtime->SaveAllocation(result.GetOutputStream(), id, filename, m_exe_ctx.GetFramePtr()); 418755232f09SEwan Crawford 418855232f09SEwan Crawford if (success) 418955232f09SEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 419055232f09SEwan Crawford else 419155232f09SEwan Crawford result.SetStatus(eReturnStatusFailed); 419255232f09SEwan Crawford 419355232f09SEwan Crawford return true; 419455232f09SEwan Crawford } 419555232f09SEwan Crawford }; 419655232f09SEwan Crawford 41970d2bfcfbSEwan Crawford class CommandObjectRenderScriptRuntimeAllocationRefresh : public CommandObjectParsed 41980d2bfcfbSEwan Crawford { 41990d2bfcfbSEwan Crawford public: 42000d2bfcfbSEwan Crawford CommandObjectRenderScriptRuntimeAllocationRefresh(CommandInterpreter &interpreter) 42010d2bfcfbSEwan Crawford : CommandObjectParsed(interpreter, "renderscript allocation refresh", 42020d2bfcfbSEwan Crawford "Recomputes the details of all allocations.", "renderscript allocation refresh", 42030d2bfcfbSEwan Crawford eCommandRequiresProcess | eCommandProcessMustBeLaunched) 42040d2bfcfbSEwan Crawford { 42050d2bfcfbSEwan Crawford } 42060d2bfcfbSEwan Crawford 42070d2bfcfbSEwan Crawford ~CommandObjectRenderScriptRuntimeAllocationRefresh() override = default; 42080d2bfcfbSEwan Crawford 42090d2bfcfbSEwan Crawford bool 42100d2bfcfbSEwan Crawford DoExecute(Args &command, CommandReturnObject &result) override 42110d2bfcfbSEwan Crawford { 42120d2bfcfbSEwan Crawford RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>( 42130d2bfcfbSEwan Crawford m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript)); 42140d2bfcfbSEwan Crawford 42150d2bfcfbSEwan Crawford bool success = runtime->RecomputeAllAllocations(result.GetOutputStream(), m_exe_ctx.GetFramePtr()); 42160d2bfcfbSEwan Crawford 42170d2bfcfbSEwan Crawford if (success) 42180d2bfcfbSEwan Crawford { 42190d2bfcfbSEwan Crawford result.SetStatus(eReturnStatusSuccessFinishResult); 42200d2bfcfbSEwan Crawford return true; 42210d2bfcfbSEwan Crawford } 42220d2bfcfbSEwan Crawford else 42230d2bfcfbSEwan Crawford { 42240d2bfcfbSEwan Crawford result.SetStatus(eReturnStatusFailed); 42250d2bfcfbSEwan Crawford return false; 42260d2bfcfbSEwan Crawford } 42270d2bfcfbSEwan Crawford } 42280d2bfcfbSEwan Crawford }; 42290d2bfcfbSEwan Crawford 423015f2bd95SEwan Crawford class CommandObjectRenderScriptRuntimeAllocation : public CommandObjectMultiword 423115f2bd95SEwan Crawford { 423215f2bd95SEwan Crawford public: 423315f2bd95SEwan Crawford CommandObjectRenderScriptRuntimeAllocation(CommandInterpreter &interpreter) 4234b3f7f69dSAidan Dodds : CommandObjectMultiword(interpreter, "renderscript allocation", 4235b3f7f69dSAidan Dodds "Commands that deal with renderscript allocations.", nullptr) 423615f2bd95SEwan Crawford { 423715f2bd95SEwan Crawford LoadSubCommand("list", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationList(interpreter))); 4238a0f08674SEwan Crawford LoadSubCommand("dump", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationDump(interpreter))); 423955232f09SEwan Crawford LoadSubCommand("save", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationSave(interpreter))); 424055232f09SEwan Crawford LoadSubCommand("load", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationLoad(interpreter))); 42410d2bfcfbSEwan Crawford LoadSubCommand("refresh", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocationRefresh(interpreter))); 424215f2bd95SEwan Crawford } 424315f2bd95SEwan Crawford 4244222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeAllocation() override = default; 424515f2bd95SEwan Crawford }; 424615f2bd95SEwan Crawford 42474640cde1SColin Riley class CommandObjectRenderScriptRuntimeStatus : public CommandObjectParsed 42484640cde1SColin Riley { 42494640cde1SColin Riley public: 42504640cde1SColin Riley CommandObjectRenderScriptRuntimeStatus(CommandInterpreter &interpreter) 4251b3f7f69dSAidan Dodds : CommandObjectParsed(interpreter, "renderscript status", "Displays current renderscript runtime status.", 4252b3f7f69dSAidan Dodds "renderscript status", eCommandRequiresProcess | eCommandProcessMustBeLaunched) 42534640cde1SColin Riley { 42544640cde1SColin Riley } 42554640cde1SColin Riley 4256222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntimeStatus() override = default; 42574640cde1SColin Riley 42584640cde1SColin Riley bool 4259222b937cSEugene Zelenko DoExecute(Args &command, CommandReturnObject &result) override 42604640cde1SColin Riley { 42614640cde1SColin Riley RenderScriptRuntime *runtime = 42624640cde1SColin Riley (RenderScriptRuntime *)m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(eLanguageTypeExtRenderScript); 42634640cde1SColin Riley runtime->Status(result.GetOutputStream()); 42644640cde1SColin Riley result.SetStatus(eReturnStatusSuccessFinishResult); 42654640cde1SColin Riley return true; 42664640cde1SColin Riley } 42674640cde1SColin Riley }; 42684640cde1SColin Riley 42695ec532a9SColin Riley class CommandObjectRenderScriptRuntime : public CommandObjectMultiword 42705ec532a9SColin Riley { 42715ec532a9SColin Riley public: 42725ec532a9SColin Riley CommandObjectRenderScriptRuntime(CommandInterpreter &interpreter) 42735ec532a9SColin Riley : CommandObjectMultiword(interpreter, "renderscript", "A set of commands for operating on renderscript.", 42745ec532a9SColin Riley "renderscript <subcommand> [<subcommand-options>]") 42755ec532a9SColin Riley { 42765ec532a9SColin Riley LoadSubCommand("module", CommandObjectSP(new CommandObjectRenderScriptRuntimeModule(interpreter))); 42774640cde1SColin Riley LoadSubCommand("status", CommandObjectSP(new CommandObjectRenderScriptRuntimeStatus(interpreter))); 42784640cde1SColin Riley LoadSubCommand("kernel", CommandObjectSP(new CommandObjectRenderScriptRuntimeKernel(interpreter))); 42794640cde1SColin Riley LoadSubCommand("context", CommandObjectSP(new CommandObjectRenderScriptRuntimeContext(interpreter))); 428015f2bd95SEwan Crawford LoadSubCommand("allocation", CommandObjectSP(new CommandObjectRenderScriptRuntimeAllocation(interpreter))); 42815ec532a9SColin Riley } 42825ec532a9SColin Riley 4283222b937cSEugene Zelenko ~CommandObjectRenderScriptRuntime() override = default; 42845ec532a9SColin Riley }; 4285ef20b08fSColin Riley 4286ef20b08fSColin Riley void 4287ef20b08fSColin Riley RenderScriptRuntime::Initiate() 42885ec532a9SColin Riley { 4289ef20b08fSColin Riley assert(!m_initiated); 42905ec532a9SColin Riley } 4291ef20b08fSColin Riley 4292ef20b08fSColin Riley RenderScriptRuntime::RenderScriptRuntime(Process *process) 4293b3f7f69dSAidan Dodds : lldb_private::CPPLanguageRuntime(process), 4294b3f7f69dSAidan Dodds m_initiated(false), 4295b3f7f69dSAidan Dodds m_debuggerPresentFlagged(false), 42967dc7771cSEwan Crawford m_breakAllKernels(false) 4297ef20b08fSColin Riley { 42984640cde1SColin Riley ModulesDidLoad(process->GetTarget().GetImages()); 4299ef20b08fSColin Riley } 43004640cde1SColin Riley 43014640cde1SColin Riley lldb::CommandObjectSP 43024640cde1SColin Riley RenderScriptRuntime::GetCommandObject(lldb_private::CommandInterpreter &interpreter) 43034640cde1SColin Riley { 43040a66e2f1SEnrico Granata return CommandObjectSP(new CommandObjectRenderScriptRuntime(interpreter)); 43054640cde1SColin Riley } 43064640cde1SColin Riley 430778f339d1SEwan Crawford RenderScriptRuntime::~RenderScriptRuntime() = default; 4308