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.Constructor; 20*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Executable; 21*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 22*795d594fSAndroid Build Coastguard Worker import java.nio.ByteBuffer; 23*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.Semaphore; 24*795d594fSAndroid Build Coastguard Worker import java.util.Arrays; 25*795d594fSAndroid Build Coastguard Worker import java.util.Collection; 26*795d594fSAndroid Build Coastguard Worker import java.util.List; 27*795d594fSAndroid Build Coastguard Worker import java.util.Set; 28*795d594fSAndroid Build Coastguard Worker import java.util.function.Function; 29*795d594fSAndroid Build Coastguard Worker import java.util.function.Predicate; 30*795d594fSAndroid Build Coastguard Worker import java.util.function.Supplier; 31*795d594fSAndroid Build Coastguard Worker import java.util.function.Consumer; 32*795d594fSAndroid Build Coastguard Worker 33*795d594fSAndroid Build Coastguard Worker public class Test1915 { 34*795d594fSAndroid Build Coastguard Worker public static final int SET_VALUE = 1337; 35*795d594fSAndroid Build Coastguard Worker public static final String TARGET_VAR = "TARGET"; 36*795d594fSAndroid Build Coastguard Worker reportValue(Object val)37*795d594fSAndroid Build Coastguard Worker public static void reportValue(Object val) { 38*795d594fSAndroid Build Coastguard Worker System.out.println("\tValue is '" + val + "'"); 39*795d594fSAndroid Build Coastguard Worker } 40*795d594fSAndroid Build Coastguard Worker public static interface ThrowRunnable { run()41*795d594fSAndroid Build Coastguard Worker public void run() throws Exception; 42*795d594fSAndroid Build Coastguard Worker } 43*795d594fSAndroid Build Coastguard Worker IntMethod(ThrowRunnable safepoint)44*795d594fSAndroid Build Coastguard Worker public static void IntMethod(ThrowRunnable safepoint) throws Exception { 45*795d594fSAndroid Build Coastguard Worker int TARGET = 42; 46*795d594fSAndroid Build Coastguard Worker safepoint.run(); 47*795d594fSAndroid Build Coastguard Worker reportValue(TARGET); 48*795d594fSAndroid Build Coastguard Worker } 49*795d594fSAndroid Build Coastguard Worker run()50*795d594fSAndroid Build Coastguard Worker public static void run() throws Exception { 51*795d594fSAndroid Build Coastguard Worker Locals.EnableLocalVariableAccess(); 52*795d594fSAndroid Build Coastguard Worker final Method target = Test1915.class.getDeclaredMethod("IntMethod", ThrowRunnable.class); 53*795d594fSAndroid Build Coastguard Worker // Get Variable. 54*795d594fSAndroid Build Coastguard Worker System.out.println("GetLocalInt on current thread!"); 55*795d594fSAndroid Build Coastguard Worker IntMethod(() -> { 56*795d594fSAndroid Build Coastguard Worker StackTrace.StackFrameData frame = FindStackFrame(target); 57*795d594fSAndroid Build Coastguard Worker int depth = FindExpectedFrameDepth(frame); 58*795d594fSAndroid Build Coastguard Worker int slot = FindSlot(frame); 59*795d594fSAndroid Build Coastguard Worker int value = Locals.GetLocalVariableInt(Thread.currentThread(), depth, slot); 60*795d594fSAndroid Build Coastguard Worker System.out.println("From GetLocalInt(), value is " + value); 61*795d594fSAndroid Build Coastguard Worker }); 62*795d594fSAndroid Build Coastguard Worker // Set Variable. 63*795d594fSAndroid Build Coastguard Worker System.out.println("SetLocalInt on current thread!"); 64*795d594fSAndroid Build Coastguard Worker IntMethod(() -> { 65*795d594fSAndroid Build Coastguard Worker StackTrace.StackFrameData frame = FindStackFrame(target); 66*795d594fSAndroid Build Coastguard Worker int depth = FindExpectedFrameDepth(frame); 67*795d594fSAndroid Build Coastguard Worker int slot = FindSlot(frame); 68*795d594fSAndroid Build Coastguard Worker Locals.SetLocalVariableInt(Thread.currentThread(), depth, slot, SET_VALUE); 69*795d594fSAndroid Build Coastguard Worker }); 70*795d594fSAndroid Build Coastguard Worker } 71*795d594fSAndroid Build Coastguard Worker FindSlot(StackTrace.StackFrameData frame)72*795d594fSAndroid Build Coastguard Worker public static int FindSlot(StackTrace.StackFrameData frame) throws Exception { 73*795d594fSAndroid Build Coastguard Worker long loc = frame.current_location; 74*795d594fSAndroid Build Coastguard Worker for (Locals.VariableDescription var : Locals.GetLocalVariableTable(frame.method)) { 75*795d594fSAndroid Build Coastguard Worker if (var.start_location <= loc && 76*795d594fSAndroid Build Coastguard Worker var.length + var.start_location > loc && 77*795d594fSAndroid Build Coastguard Worker var.name.equals(TARGET_VAR)) { 78*795d594fSAndroid Build Coastguard Worker return var.slot; 79*795d594fSAndroid Build Coastguard Worker } 80*795d594fSAndroid Build Coastguard Worker } 81*795d594fSAndroid Build Coastguard Worker throw new Error( 82*795d594fSAndroid Build Coastguard Worker "Unable to find variable " + TARGET_VAR + " in " + frame.method + " at loc " + loc); 83*795d594fSAndroid Build Coastguard Worker } 84*795d594fSAndroid Build Coastguard Worker FindExpectedFrameDepth(StackTrace.StackFrameData frame)85*795d594fSAndroid Build Coastguard Worker public static int FindExpectedFrameDepth(StackTrace.StackFrameData frame) throws Exception { 86*795d594fSAndroid Build Coastguard Worker // Adjust the 'frame' depth since it is modified by: 87*795d594fSAndroid Build Coastguard Worker // +1 for Get/SetLocalVariableInt in future. 88*795d594fSAndroid Build Coastguard Worker // -1 for FindStackFrame 89*795d594fSAndroid Build Coastguard Worker // -1 for GetStackTrace 90*795d594fSAndroid Build Coastguard Worker // -1 for GetStackTraceNative 91*795d594fSAndroid Build Coastguard Worker // ------------------------------ 92*795d594fSAndroid Build Coastguard Worker // -2 93*795d594fSAndroid Build Coastguard Worker return frame.depth - 2; 94*795d594fSAndroid Build Coastguard Worker } 95*795d594fSAndroid Build Coastguard Worker FindStackFrame(Method target)96*795d594fSAndroid Build Coastguard Worker private static StackTrace.StackFrameData FindStackFrame(Method target) { 97*795d594fSAndroid Build Coastguard Worker for (StackTrace.StackFrameData frame : StackTrace.GetStackTrace(Thread.currentThread())) { 98*795d594fSAndroid Build Coastguard Worker if (frame.method.equals(target)) { 99*795d594fSAndroid Build Coastguard Worker return frame; 100*795d594fSAndroid Build Coastguard Worker } 101*795d594fSAndroid Build Coastguard Worker } 102*795d594fSAndroid Build Coastguard Worker throw new Error("Unable to find stack frame in method " + target); 103*795d594fSAndroid Build Coastguard Worker } 104*795d594fSAndroid Build Coastguard Worker } 105*795d594fSAndroid Build Coastguard Worker 106