1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker package art; 18*795d594fSAndroid Build Coastguard Worker 19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Executable; 20*795d594fSAndroid Build Coastguard Worker import java.util.Objects; 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker public class Locals { EnableLocalVariableAccess()23*795d594fSAndroid Build Coastguard Worker public static native void EnableLocalVariableAccess(); 24*795d594fSAndroid Build Coastguard Worker 25*795d594fSAndroid Build Coastguard Worker public static class VariableDescription { 26*795d594fSAndroid Build Coastguard Worker public final long start_location; 27*795d594fSAndroid Build Coastguard Worker public final int length; 28*795d594fSAndroid Build Coastguard Worker public final String name; 29*795d594fSAndroid Build Coastguard Worker public final String signature; 30*795d594fSAndroid Build Coastguard Worker public final String generic_signature; 31*795d594fSAndroid Build Coastguard Worker public final int slot; 32*795d594fSAndroid Build Coastguard Worker VariableDescription( long start, int length, String name, String sig, String gen_sig, int slot)33*795d594fSAndroid Build Coastguard Worker public VariableDescription( 34*795d594fSAndroid Build Coastguard Worker long start, int length, String name, String sig, String gen_sig, int slot) { 35*795d594fSAndroid Build Coastguard Worker this.start_location = start; 36*795d594fSAndroid Build Coastguard Worker this.length = length; 37*795d594fSAndroid Build Coastguard Worker this.name = name; 38*795d594fSAndroid Build Coastguard Worker this.signature = sig; 39*795d594fSAndroid Build Coastguard Worker this.generic_signature = gen_sig; 40*795d594fSAndroid Build Coastguard Worker this.slot = slot; 41*795d594fSAndroid Build Coastguard Worker } 42*795d594fSAndroid Build Coastguard Worker 43*795d594fSAndroid Build Coastguard Worker @Override toString()44*795d594fSAndroid Build Coastguard Worker public String toString() { 45*795d594fSAndroid Build Coastguard Worker return String.format( 46*795d594fSAndroid Build Coastguard Worker "VariableDescription { " + 47*795d594fSAndroid Build Coastguard Worker "Sig: '%s', Name: '%s', Gen_sig: '%s', slot: %d, start: %d, len: %d" + 48*795d594fSAndroid Build Coastguard Worker "}", 49*795d594fSAndroid Build Coastguard Worker this.signature, 50*795d594fSAndroid Build Coastguard Worker this.name, 51*795d594fSAndroid Build Coastguard Worker this.generic_signature, 52*795d594fSAndroid Build Coastguard Worker this.slot, 53*795d594fSAndroid Build Coastguard Worker this.start_location, 54*795d594fSAndroid Build Coastguard Worker this.length); 55*795d594fSAndroid Build Coastguard Worker } equals(Object other)56*795d594fSAndroid Build Coastguard Worker public boolean equals(Object other) { 57*795d594fSAndroid Build Coastguard Worker if (!(other instanceof VariableDescription)) { 58*795d594fSAndroid Build Coastguard Worker return false; 59*795d594fSAndroid Build Coastguard Worker } else { 60*795d594fSAndroid Build Coastguard Worker VariableDescription v = (VariableDescription)other; 61*795d594fSAndroid Build Coastguard Worker return Objects.equals(v.signature, signature) && 62*795d594fSAndroid Build Coastguard Worker Objects.equals(v.name, name) && 63*795d594fSAndroid Build Coastguard Worker Objects.equals(v.generic_signature, generic_signature) && 64*795d594fSAndroid Build Coastguard Worker v.slot == slot && 65*795d594fSAndroid Build Coastguard Worker v.start_location == start_location && 66*795d594fSAndroid Build Coastguard Worker v.length == length; 67*795d594fSAndroid Build Coastguard Worker } 68*795d594fSAndroid Build Coastguard Worker } hashCode()69*795d594fSAndroid Build Coastguard Worker public int hashCode() { 70*795d594fSAndroid Build Coastguard Worker return Objects.hash(this.signature, this.name, this.generic_signature, this.slot, 71*795d594fSAndroid Build Coastguard Worker this.start_location, this.length); 72*795d594fSAndroid Build Coastguard Worker } 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker GetLocalVariableTable(Executable e)75*795d594fSAndroid Build Coastguard Worker public static native VariableDescription[] GetLocalVariableTable(Executable e); 76*795d594fSAndroid Build Coastguard Worker GetVariableAtLine( Executable e, String name, String sig, int line)77*795d594fSAndroid Build Coastguard Worker public static VariableDescription GetVariableAtLine( 78*795d594fSAndroid Build Coastguard Worker Executable e, String name, String sig, int line) throws Exception { 79*795d594fSAndroid Build Coastguard Worker return GetVariableAtLocation(e, name, sig, Breakpoint.lineToLocation(e, line)); 80*795d594fSAndroid Build Coastguard Worker } 81*795d594fSAndroid Build Coastguard Worker GetVariableAtLocation( Executable e, String name, String sig, long loc)82*795d594fSAndroid Build Coastguard Worker public static VariableDescription GetVariableAtLocation( 83*795d594fSAndroid Build Coastguard Worker Executable e, String name, String sig, long loc) { 84*795d594fSAndroid Build Coastguard Worker VariableDescription[] vars = GetLocalVariableTable(e); 85*795d594fSAndroid Build Coastguard Worker for (VariableDescription var : vars) { 86*795d594fSAndroid Build Coastguard Worker if (var.start_location <= loc && 87*795d594fSAndroid Build Coastguard Worker var.length + var.start_location > loc && 88*795d594fSAndroid Build Coastguard Worker var.name.equals(name) && 89*795d594fSAndroid Build Coastguard Worker var.signature.equals(sig)) { 90*795d594fSAndroid Build Coastguard Worker return var; 91*795d594fSAndroid Build Coastguard Worker } 92*795d594fSAndroid Build Coastguard Worker } 93*795d594fSAndroid Build Coastguard Worker throw new Error( 94*795d594fSAndroid Build Coastguard Worker "Unable to find variable " + name + " (sig: " + sig + ") in " + e + " at loc " + loc); 95*795d594fSAndroid Build Coastguard Worker } 96*795d594fSAndroid Build Coastguard Worker GetLocalVariableInt(Thread thr, int depth, int slot)97*795d594fSAndroid Build Coastguard Worker public static native int GetLocalVariableInt(Thread thr, int depth, int slot); GetLocalVariableLong(Thread thr, int depth, int slot)98*795d594fSAndroid Build Coastguard Worker public static native long GetLocalVariableLong(Thread thr, int depth, int slot); GetLocalVariableFloat(Thread thr, int depth, int slot)99*795d594fSAndroid Build Coastguard Worker public static native float GetLocalVariableFloat(Thread thr, int depth, int slot); GetLocalVariableDouble(Thread thr, int depth, int slot)100*795d594fSAndroid Build Coastguard Worker public static native double GetLocalVariableDouble(Thread thr, int depth, int slot); GetLocalVariableObject(Thread thr, int depth, int slot)101*795d594fSAndroid Build Coastguard Worker public static native Object GetLocalVariableObject(Thread thr, int depth, int slot); GetLocalInstance(Thread thr, int depth)102*795d594fSAndroid Build Coastguard Worker public static native Object GetLocalInstance(Thread thr, int depth); 103*795d594fSAndroid Build Coastguard Worker SetLocalVariableInt(Thread thr, int depth, int slot, Object val)104*795d594fSAndroid Build Coastguard Worker public static void SetLocalVariableInt(Thread thr, int depth, int slot, Object val) { 105*795d594fSAndroid Build Coastguard Worker SetLocalVariableInt(thr, depth, slot, ((Number)val).intValue()); 106*795d594fSAndroid Build Coastguard Worker } SetLocalVariableLong(Thread thr, int depth, int slot, Object val)107*795d594fSAndroid Build Coastguard Worker public static void SetLocalVariableLong(Thread thr, int depth, int slot, Object val) { 108*795d594fSAndroid Build Coastguard Worker SetLocalVariableLong(thr, depth, slot, ((Number)val).longValue()); 109*795d594fSAndroid Build Coastguard Worker } SetLocalVariableFloat(Thread thr, int depth, int slot, Object val)110*795d594fSAndroid Build Coastguard Worker public static void SetLocalVariableFloat(Thread thr, int depth, int slot, Object val) { 111*795d594fSAndroid Build Coastguard Worker SetLocalVariableFloat(thr, depth, slot, ((Number)val).floatValue()); 112*795d594fSAndroid Build Coastguard Worker } SetLocalVariableDouble(Thread thr, int depth, int slot, Object val)113*795d594fSAndroid Build Coastguard Worker public static void SetLocalVariableDouble(Thread thr, int depth, int slot, Object val) { 114*795d594fSAndroid Build Coastguard Worker SetLocalVariableDouble(thr, depth, slot, ((Number)val).doubleValue()); 115*795d594fSAndroid Build Coastguard Worker } SetLocalVariableInt(Thread thr, int depth, int slot, int val)116*795d594fSAndroid Build Coastguard Worker public static native void SetLocalVariableInt(Thread thr, int depth, int slot, int val); SetLocalVariableLong(Thread thr, int depth, int slot, long val)117*795d594fSAndroid Build Coastguard Worker public static native void SetLocalVariableLong(Thread thr, int depth, int slot, long val); SetLocalVariableFloat(Thread thr, int depth, int slot, float val)118*795d594fSAndroid Build Coastguard Worker public static native void SetLocalVariableFloat(Thread thr, int depth, int slot, float val); SetLocalVariableDouble(Thread thr, int depth, int slot, double val)119*795d594fSAndroid Build Coastguard Worker public static native void SetLocalVariableDouble(Thread thr, int depth, int slot, double val); SetLocalVariableObject(Thread thr, int depth, int slot, Object val)120*795d594fSAndroid Build Coastguard Worker public static native void SetLocalVariableObject(Thread thr, int depth, int slot, Object val); 121*795d594fSAndroid Build Coastguard Worker } 122